2 * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include "libavutil/avutil.h"
35 #include "img_format.h"
38 #include "libswscale/swscale.h"
42 //===========================================================================//
44 typedef struct FilterParam
{
46 float preFilterRadius
;
49 struct SwsContext
*preFilterContext
;
50 uint8_t *preFilterBuf
;
55 int colorDiffCoeff
[512];
64 /***************************************************************************/
66 //FIXME stupid code duplication
67 static void getSubSampleFactors(int *h
, int *v
, int format
){
93 static int allocStuff(FilterParam
*f
, int width
, int height
){
94 int stride
= (width
+7)&~7;
98 f
->preFilterBuf
= (uint8_t*)memalign(8, stride
*height
);
99 f
->preFilterStride
= stride
;
101 vec
= sws_getGaussianVec(f
->preFilterRadius
, f
->quality
);
102 swsF
.lumH
= swsF
.lumV
= vec
;
103 swsF
.chrH
= swsF
.chrV
= NULL
;
104 f
->preFilterContext
= sws_getContext(
105 width
, height
, PIX_FMT_GRAY8
, width
, height
, PIX_FMT_GRAY8
, get_sws_cpuflags()|SWS_POINT
, &swsF
, NULL
, NULL
);
108 vec
= sws_getGaussianVec(f
->strength
, 5.0);
109 for(i
=0; i
<512; i
++){
111 int index
= i
-256 + vec
->length
/2;
113 if(index
<0 || index
>=vec
->length
) d
= 0.0;
114 else d
= vec
->coeff
[index
];
116 f
->colorDiffCoeff
[i
]= (int)(d
/vec
->coeff
[vec
->length
/2]*(1<<12) + 0.5);
119 vec
= sws_getGaussianVec(f
->radius
, f
->quality
);
120 f
->distWidth
= vec
->length
;
121 f
->distStride
= (vec
->length
+7)&~7;
122 f
->distCoeff
= (int32_t*)memalign(8, f
->distWidth
*f
->distStride
*sizeof(int32_t));
124 for(y
=0; y
<vec
->length
; y
++){
125 for(x
=0; x
<vec
->length
; x
++){
126 double d
= vec
->coeff
[x
] * vec
->coeff
[y
];
128 f
->distCoeff
[x
+ y
*f
->distStride
]= (int)(d
*(1<<10) + 0.5);
129 // if(y==vec->length/2)
130 // printf("%6d ", f->distCoeff[x + y*f->distStride]);
138 static int config(struct vf_instance
*vf
,
139 int width
, int height
, int d_width
, int d_height
,
140 unsigned int flags
, unsigned int outfmt
){
143 //__asm__ volatile("emms\n\t");
144 allocStuff(&vf
->priv
->luma
, width
, height
);
146 getSubSampleFactors(&sw
, &sh
, outfmt
);
147 allocStuff(&vf
->priv
->chroma
, width
>>sw
, height
>>sh
);
149 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
152 static void freeBuffers(FilterParam
*f
){
153 if(f
->preFilterContext
) sws_freeContext(f
->preFilterContext
);
154 f
->preFilterContext
=NULL
;
156 if(f
->preFilterBuf
) free(f
->preFilterBuf
);
157 f
->preFilterBuf
=NULL
;
159 if(f
->distCoeff
) free(f
->distCoeff
);
163 static void uninit(struct vf_instance
*vf
){
164 if(!vf
->priv
) return;
166 freeBuffers(&vf
->priv
->luma
);
167 freeBuffers(&vf
->priv
->chroma
);
173 static inline void blur(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
, FilterParam
*fp
){
176 const int radius
= f
.distWidth
/2;
177 uint8_t *srcArray
[MP_MAX_PLANES
]= {src
};
178 uint8_t *dstArray
[MP_MAX_PLANES
]= {f
.preFilterBuf
};
179 int srcStrideArray
[MP_MAX_PLANES
]= {srcStride
};
180 int dstStrideArray
[MP_MAX_PLANES
]= {f
.preFilterStride
};
182 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
183 sws_scale(f
.preFilterContext
, srcArray
, srcStrideArray
, 0, h
, dstArray
, dstStrideArray
);
190 const int preVal
= f
.preFilterBuf
[x
+ y
*f
.preFilterStride
];
192 const int srcVal
= src
[x
+ y
*srcStride
];
194 dst
[x
+ y
*dstStride
]= srcVal
;
195 if(y
%32==0) dst
[x
+ y
*dstStride
]= 0;
199 if(x
>= radius
&& x
< w
- radius
){
200 for(dy
=0; dy
<radius
*2+1; dy
++){
202 int iy
= y
+dy
- radius
;
204 else if(iy
>=h
) iy
= h
+h
-iy
-1;
206 for(dx
=0; dx
<radius
*2+1; dx
++){
207 const int ix
= x
+dx
- radius
;
210 factor
= f
.colorDiffCoeff
[256+preVal
- f
.preFilterBuf
[ix
+ iy
*f
.preFilterStride
] ]
211 *f
.distCoeff
[dx
+ dy
*f
.distStride
];
212 sum
+= src
[ix
+ iy
*srcStride
] *factor
;
217 for(dy
=0; dy
<radius
*2+1; dy
++){
219 int iy
= y
+dy
- radius
;
221 else if(iy
>=h
) iy
= h
+h
-iy
-1;
223 for(dx
=0; dx
<radius
*2+1; dx
++){
224 int ix
= x
+dx
- radius
;
227 else if(ix
>=w
) ix
= w
+w
-ix
-1;
229 factor
= f
.colorDiffCoeff
[256+preVal
- f
.preFilterBuf
[ix
+ iy
*f
.preFilterStride
] ]
230 *f
.distCoeff
[dx
+ dy
*f
.distStride
];
231 sum
+= src
[ix
+ iy
*srcStride
] *factor
;
236 dst
[x
+ y
*dstStride
]= (sum
+ div
/2)/div
;
241 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
){
242 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
243 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
245 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
246 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
249 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
251 blur(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0], &vf
->priv
->luma
);
252 blur(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1], &vf
->priv
->chroma
);
253 blur(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2], &vf
->priv
->chroma
);
255 return vf_next_put_image(vf
,dmpi
, pts
);
258 //===========================================================================//
260 static int query_format(struct vf_instance
*vf
, unsigned int fmt
){
270 return vf_next_query_format(vf
, fmt
);
275 static int vf_open(vf_instance_t
*vf
, char *args
){
279 vf
->put_image
=put_image
;
280 // vf->get_image=get_image;
281 vf
->query_format
=query_format
;
283 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
284 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
286 if(args
==NULL
) return 0;
288 e
=sscanf(args
, "%f:%f:%f:%f:%f:%f",
289 &vf
->priv
->luma
.radius
,
290 &vf
->priv
->luma
.preFilterRadius
,
291 &vf
->priv
->luma
.strength
,
292 &vf
->priv
->chroma
.radius
,
293 &vf
->priv
->chroma
.preFilterRadius
,
294 &vf
->priv
->chroma
.strength
297 vf
->priv
->luma
.quality
= vf
->priv
->chroma
.quality
= 3.0;
300 vf
->priv
->chroma
.radius
= vf
->priv
->luma
.radius
;
301 vf
->priv
->chroma
.preFilterRadius
= vf
->priv
->luma
.preFilterRadius
;
302 vf
->priv
->chroma
.strength
= vf
->priv
->luma
.strength
;
306 // if(vf->priv->luma.radius < 0) return 0;
307 // if(vf->priv->chroma.radius < 0) return 0;
312 const vf_info_t vf_info_sab
= {
313 "shape adaptive blur",
315 "Michael Niedermayer",
321 //===========================================================================//