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 #ifdef USE_LIBAVUTIL_SO
33 #include <ffmpeg/avutil.h>
37 #include "img_format.h"
40 #include "libswscale/swscale.h"
43 //===========================================================================//
45 typedef struct FilterParam
{
50 struct SwsContext
*filterContext
;
59 /***************************************************************************/
61 //FIXME stupid code duplication
62 static void getSubSampleFactors(int *h
, int *v
, int format
){
88 static int allocStuff(FilterParam
*f
, int width
, int height
){
92 vec
= sws_getGaussianVec(f
->radius
, f
->quality
);
93 sws_scaleVec(vec
, f
->strength
);
94 vec
->coeff
[vec
->length
/2]+= 1.0 - f
->strength
;
95 swsF
.lumH
= swsF
.lumV
= vec
;
96 swsF
.chrH
= swsF
.chrV
= NULL
;
97 f
->filterContext
= sws_getContext(
98 width
, height
, PIX_FMT_GRAY8
, width
, height
, PIX_FMT_GRAY8
, get_sws_cpuflags(), &swsF
, NULL
, NULL
);
105 static int config(struct vf_instance_s
* vf
,
106 int width
, int height
, int d_width
, int d_height
,
107 unsigned int flags
, unsigned int outfmt
){
111 allocStuff(&vf
->priv
->luma
, width
, height
);
113 getSubSampleFactors(&sw
, &sh
, outfmt
);
114 allocStuff(&vf
->priv
->chroma
, width
>>sw
, height
>>sh
);
116 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
119 static void freeBuffers(FilterParam
*f
){
120 if(f
->filterContext
) sws_freeContext(f
->filterContext
);
121 f
->filterContext
=NULL
;
124 static void uninit(struct vf_instance_s
* vf
){
125 if(!vf
->priv
) return;
127 freeBuffers(&vf
->priv
->luma
);
128 freeBuffers(&vf
->priv
->chroma
);
134 static inline void blur(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
, FilterParam
*fp
){
137 uint8_t *srcArray
[3]= {src
, NULL
, NULL
};
138 uint8_t *dstArray
[3]= {dst
, NULL
, NULL
};
139 int srcStrideArray
[3]= {srcStride
, 0, 0};
140 int dstStrideArray
[3]= {dstStride
, 0, 0};
142 sws_scale(f
.filterContext
, srcArray
, srcStrideArray
, 0, h
, dstArray
, dstStrideArray
);
147 const int orig
= src
[x
+ y
*srcStride
];
148 const int filtered
= dst
[x
+ y
*dstStride
];
149 const int diff
= orig
- filtered
;
152 if(diff
> 2*f
.threshold
){
153 dst
[x
+ y
*dstStride
]= orig
;
154 }else if(diff
> f
.threshold
){
155 dst
[x
+ y
*dstStride
]= filtered
+ diff
- f
.threshold
;
158 if(-diff
> 2*f
.threshold
){
159 dst
[x
+ y
*dstStride
]= orig
;
160 }else if(-diff
> f
.threshold
){
161 dst
[x
+ y
*dstStride
]= filtered
+ diff
+ f
.threshold
;
166 }else if(f
.threshold
< 0){
169 const int orig
= src
[x
+ y
*srcStride
];
170 const int filtered
= dst
[x
+ y
*dstStride
];
171 const int diff
= orig
- filtered
;
174 if(diff
> -2*f
.threshold
){
175 }else if(diff
> -f
.threshold
){
176 dst
[x
+ y
*dstStride
]= orig
- diff
- f
.threshold
;
178 dst
[x
+ y
*dstStride
]= orig
;
180 if(diff
< 2*f
.threshold
){
181 }else if(diff
< f
.threshold
){
182 dst
[x
+ y
*dstStride
]= orig
- diff
+ f
.threshold
;
184 dst
[x
+ y
*dstStride
]= orig
;
191 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
192 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
193 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
194 FilterParam
*f
= &vf
->priv
;
196 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
197 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
|
198 (f
->threshold
) ? MP_IMGFLAG_READABLE
: 0,
201 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
203 blur(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0], &vf
->priv
->luma
);
204 blur(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1], &vf
->priv
->chroma
);
205 blur(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2], &vf
->priv
->chroma
);
207 return vf_next_put_image(vf
,dmpi
, pts
);
210 //===========================================================================//
212 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
222 return vf_next_query_format(vf
, fmt
);
227 static int open(vf_instance_t
*vf
, char* args
){
231 vf
->put_image
=put_image
;
232 // vf->get_image=get_image;
233 vf
->query_format
=query_format
;
235 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
236 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
238 if(args
==NULL
) return 0;
240 e
=sscanf(args
, "%f:%f:%d:%f:%f:%d",
241 &vf
->priv
->luma
.radius
,
242 &vf
->priv
->luma
.strength
,
243 &vf
->priv
->luma
.threshold
,
244 &vf
->priv
->chroma
.radius
,
245 &vf
->priv
->chroma
.strength
,
246 &vf
->priv
->chroma
.threshold
249 vf
->priv
->luma
.quality
= vf
->priv
->chroma
.quality
= 3.0;
252 vf
->priv
->chroma
.radius
= vf
->priv
->luma
.radius
;
253 vf
->priv
->chroma
.strength
= vf
->priv
->luma
.strength
;
254 vf
->priv
->chroma
.threshold
= vf
->priv
->luma
.threshold
;
261 const vf_info_t vf_info_smartblur
= {
264 "Michael Niedermayer",
270 //===========================================================================//