2 Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "img_format.h"
35 #include "libvo/fastmemcpy.h"
36 #include "postproc/swscale.h"
39 //===========================================================================//
41 typedef struct FilterParam
{
46 struct SwsContext
*filterContext
;
55 /***************************************************************************/
57 //FIXME stupid code duplication
58 static void getSubSampleFactors(int *h
, int *v
, int format
){
84 static int allocStuff(FilterParam
*f
, int width
, int height
){
88 vec
= sws_getGaussianVec(f
->radius
, f
->quality
);
89 sws_scaleVec(vec
, f
->strength
);
90 vec
->coeff
[vec
->length
/2]+= 1.0 - f
->strength
;
91 swsF
.lumH
= swsF
.lumV
= vec
;
92 swsF
.chrH
= swsF
.chrV
= NULL
;
93 f
->filterContext
= sws_getContext(
94 width
, height
, IMGFMT_Y8
, width
, height
, IMGFMT_Y8
, get_sws_cpuflags(), &swsF
, NULL
, NULL
);
101 static int config(struct vf_instance_s
* vf
,
102 int width
, int height
, int d_width
, int d_height
,
103 unsigned int flags
, unsigned int outfmt
){
107 allocStuff(&vf
->priv
->luma
, width
, height
);
109 getSubSampleFactors(&sw
, &sh
, outfmt
);
110 allocStuff(&vf
->priv
->chroma
, width
>>sw
, height
>>sh
);
112 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
115 static void freeBuffers(FilterParam
*f
){
116 if(f
->filterContext
) sws_freeContext(f
->filterContext
);
117 f
->filterContext
=NULL
;
120 static void uninit(struct vf_instance_s
* vf
){
121 if(!vf
->priv
) return;
123 freeBuffers(&vf
->priv
->luma
);
124 freeBuffers(&vf
->priv
->chroma
);
130 static inline void blur(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
, FilterParam
*fp
){
133 uint8_t *srcArray
[3]= {src
, NULL
, NULL
};
134 uint8_t *dstArray
[3]= {dst
, NULL
, NULL
};
135 int srcStrideArray
[3]= {srcStride
, 0, 0};
136 int dstStrideArray
[3]= {dstStride
, 0, 0};
138 sws_scale(f
.filterContext
, srcArray
, srcStrideArray
, 0, h
, dstArray
, dstStrideArray
);
143 const int orig
= src
[x
+ y
*srcStride
];
144 const int filtered
= dst
[x
+ y
*dstStride
];
145 const int diff
= orig
- filtered
;
148 if(diff
> 2*f
.threshold
){
149 dst
[x
+ y
*dstStride
]= orig
;
150 }else if(diff
> f
.threshold
){
151 dst
[x
+ y
*dstStride
]= filtered
+ diff
- f
.threshold
;
154 if(-diff
> 2*f
.threshold
){
155 dst
[x
+ y
*dstStride
]= orig
;
156 }else if(-diff
> f
.threshold
){
157 dst
[x
+ y
*dstStride
]= filtered
+ diff
+ f
.threshold
;
162 }else if(f
.threshold
< 0){
165 const int orig
= src
[x
+ y
*srcStride
];
166 const int filtered
= dst
[x
+ y
*dstStride
];
167 const int diff
= orig
- filtered
;
170 if(diff
> -2*f
.threshold
){
171 }else if(diff
> -f
.threshold
){
172 dst
[x
+ y
*dstStride
]= orig
- diff
- f
.threshold
;
174 dst
[x
+ y
*dstStride
]= orig
;
176 if(diff
< 2*f
.threshold
){
177 }else if(diff
< f
.threshold
){
178 dst
[x
+ y
*dstStride
]= orig
- diff
+ f
.threshold
;
180 dst
[x
+ y
*dstStride
]= orig
;
187 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
188 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
189 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
190 FilterParam
*f
= &vf
->priv
;
192 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
193 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
|
194 (f
->threshold
) ? MP_IMGFLAG_READABLE
: 0,
197 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
199 blur(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0], &vf
->priv
->luma
);
200 blur(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1], &vf
->priv
->chroma
);
201 blur(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2], &vf
->priv
->chroma
);
203 return vf_next_put_image(vf
,dmpi
, pts
);
206 //===========================================================================//
208 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
218 return vf_next_query_format(vf
, fmt
);
223 static int open(vf_instance_t
*vf
, char* args
){
227 vf
->put_image
=put_image
;
228 // vf->get_image=get_image;
229 vf
->query_format
=query_format
;
231 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
232 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
234 if(args
==NULL
) return 0;
236 e
=sscanf(args
, "%f:%f:%d:%f:%f:%d",
237 &vf
->priv
->luma
.radius
,
238 &vf
->priv
->luma
.strength
,
239 &vf
->priv
->luma
.threshold
,
240 &vf
->priv
->chroma
.radius
,
241 &vf
->priv
->chroma
.strength
,
242 &vf
->priv
->chroma
.threshold
245 vf
->priv
->luma
.quality
= vf
->priv
->chroma
.quality
= 3.0;
248 vf
->priv
->chroma
.radius
= vf
->priv
->luma
.radius
;
249 vf
->priv
->chroma
.strength
= vf
->priv
->luma
.strength
;
250 vf
->priv
->chroma
.threshold
= vf
->priv
->luma
.threshold
;
257 vf_info_t vf_info_smartblur
= {
260 "Michael Niedermayer",
266 //===========================================================================//