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"
44 //===========================================================================//
46 typedef struct FilterParam
{
48 float preFilterRadius
;
51 struct SwsContext
*preFilterContext
;
52 uint8_t *preFilterBuf
;
57 int colorDiffCoeff
[512];
66 /***************************************************************************/
68 //FIXME stupid code duplication
69 static void getSubSampleFactors(int *h
, int *v
, int format
){
95 static int allocStuff(FilterParam
*f
, int width
, int height
){
96 int stride
= (width
+7)&~7;
100 f
->preFilterBuf
= (uint8_t*)memalign(8, stride
*height
);
101 f
->preFilterStride
= stride
;
103 vec
= sws_getGaussianVec(f
->preFilterRadius
, f
->quality
);
104 swsF
.lumH
= swsF
.lumV
= vec
;
105 swsF
.chrH
= swsF
.chrV
= NULL
;
106 f
->preFilterContext
= sws_getContext(
107 width
, height
, PIX_FMT_GRAY8
, width
, height
, PIX_FMT_GRAY8
, get_sws_cpuflags(), &swsF
, NULL
, NULL
);
110 vec
= sws_getGaussianVec(f
->strength
, 5.0);
111 for(i
=0; i
<512; i
++){
113 int index
= i
-256 + vec
->length
/2;
115 if(index
<0 || index
>=vec
->length
) d
= 0.0;
116 else d
= vec
->coeff
[index
];
118 f
->colorDiffCoeff
[i
]= (int)(d
/vec
->coeff
[vec
->length
/2]*(1<<12) + 0.5);
121 vec
= sws_getGaussianVec(f
->radius
, f
->quality
);
122 f
->distWidth
= vec
->length
;
123 f
->distStride
= (vec
->length
+7)&~7;
124 f
->distCoeff
= (int32_t*)memalign(8, f
->distWidth
*f
->distStride
*sizeof(int32_t));
126 for(y
=0; y
<vec
->length
; y
++){
127 for(x
=0; x
<vec
->length
; x
++){
128 double d
= vec
->coeff
[x
] * vec
->coeff
[y
];
130 f
->distCoeff
[x
+ y
*f
->distStride
]= (int)(d
*(1<<10) + 0.5);
131 // if(y==vec->length/2)
132 // printf("%6d ", f->distCoeff[x + y*f->distStride]);
140 static int config(struct vf_instance_s
* vf
,
141 int width
, int height
, int d_width
, int d_height
,
142 unsigned int flags
, unsigned int outfmt
){
145 //asm volatile("emms\n\t");
146 allocStuff(&vf
->priv
->luma
, width
, height
);
148 getSubSampleFactors(&sw
, &sh
, outfmt
);
149 allocStuff(&vf
->priv
->chroma
, width
>>sw
, height
>>sh
);
151 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
154 static void freeBuffers(FilterParam
*f
){
155 if(f
->preFilterContext
) sws_freeContext(f
->preFilterContext
);
156 f
->preFilterContext
=NULL
;
158 if(f
->preFilterBuf
) free(f
->preFilterBuf
);
159 f
->preFilterBuf
=NULL
;
161 if(f
->distCoeff
) free(f
->distCoeff
);
165 static void uninit(struct vf_instance_s
* vf
){
166 if(!vf
->priv
) return;
168 freeBuffers(&vf
->priv
->luma
);
169 freeBuffers(&vf
->priv
->chroma
);
175 static inline void blur(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
, FilterParam
*fp
){
178 const int radius
= f
.distWidth
/2;
179 uint8_t *srcArray
[3]= {src
, NULL
, NULL
};
180 uint8_t *dstArray
[3]= {f
.preFilterBuf
, NULL
, NULL
};
181 int srcStrideArray
[3]= {srcStride
, 0, 0};
182 int dstStrideArray
[3]= {f
.preFilterStride
, 0, 0};
184 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
185 sws_scale(f
.preFilterContext
, srcArray
, srcStrideArray
, 0, h
, dstArray
, dstStrideArray
);
192 const int preVal
= f
.preFilterBuf
[x
+ y
*f
.preFilterStride
];
194 const int srcVal
= src
[x
+ y
*srcStride
];
196 dst
[x
+ y
*dstStride
]= srcVal
;
197 if(y
%32==0) dst
[x
+ y
*dstStride
]= 0;
201 if(x
>= radius
&& x
< w
- radius
){
202 for(dy
=0; dy
<radius
*2+1; dy
++){
204 int iy
= y
+dy
- radius
;
206 else if(iy
>=h
) iy
= h
-iy
-1;
208 for(dx
=0; dx
<radius
*2+1; dx
++){
209 const int ix
= x
+dx
- radius
;
212 factor
= f
.colorDiffCoeff
[256+preVal
- f
.preFilterBuf
[ix
+ iy
*f
.preFilterStride
] ]
213 *f
.distCoeff
[dx
+ dy
*f
.distStride
];
214 sum
+= src
[ix
+ iy
*srcStride
] *factor
;
219 for(dy
=0; dy
<radius
*2+1; dy
++){
221 int iy
= y
+dy
- radius
;
223 else if(iy
>=h
) iy
= h
-iy
-1;
225 for(dx
=0; dx
<radius
*2+1; dx
++){
226 int ix
= x
+dx
- radius
;
229 else if(ix
>=w
) ix
= w
-ix
-1;
231 factor
= f
.colorDiffCoeff
[256+preVal
- f
.preFilterBuf
[ix
+ iy
*f
.preFilterStride
] ]
232 *f
.distCoeff
[dx
+ dy
*f
.distStride
];
233 sum
+= src
[ix
+ iy
*srcStride
] *factor
;
238 dst
[x
+ y
*dstStride
]= (sum
+ div
/2)/div
;
243 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
244 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
245 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
247 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
248 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
251 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
253 blur(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0], &vf
->priv
->luma
);
254 blur(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1], &vf
->priv
->chroma
);
255 blur(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2], &vf
->priv
->chroma
);
257 return vf_next_put_image(vf
,dmpi
, pts
);
260 //===========================================================================//
262 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
272 return vf_next_query_format(vf
, fmt
);
277 static int open(vf_instance_t
*vf
, char* args
){
281 vf
->put_image
=put_image
;
282 // vf->get_image=get_image;
283 vf
->query_format
=query_format
;
285 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
286 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
288 if(args
==NULL
) return 0;
290 e
=sscanf(args
, "%f:%f:%f:%f:%f:%f",
291 &vf
->priv
->luma
.radius
,
292 &vf
->priv
->luma
.preFilterRadius
,
293 &vf
->priv
->luma
.strength
,
294 &vf
->priv
->chroma
.radius
,
295 &vf
->priv
->chroma
.preFilterRadius
,
296 &vf
->priv
->chroma
.strength
299 vf
->priv
->luma
.quality
= vf
->priv
->chroma
.quality
= 3.0;
302 vf
->priv
->chroma
.radius
= vf
->priv
->luma
.radius
;
303 vf
->priv
->chroma
.preFilterRadius
= vf
->priv
->luma
.preFilterRadius
;
304 vf
->priv
->chroma
.strength
= vf
->priv
->luma
.strength
;
308 // if(vf->priv->luma.radius < 0) return 0;
309 // if(vf->priv->chroma.radius < 0) return 0;
314 const vf_info_t vf_info_sab
= {
315 "shape adaptive blur",
317 "Michael Niedermayer",
323 //===========================================================================//