Avoid passing test invalid arguments when string is empty.
[mplayer/glamo.git] / libmpcodecs / vf_sab.c
blobdcd45647e24d4420097ec9d435e394c1c4994566
1 /*
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <assert.h>
25 #include "config.h"
26 #include "mp_msg.h"
28 #ifdef HAVE_MALLOC_H
29 #include <malloc.h>
30 #endif
32 #ifdef USE_LIBAVUTIL_SO
33 #include <ffmpeg/avutil.h>
34 #else
35 #include "avutil.h"
36 #endif
37 #include "img_format.h"
38 #include "mp_image.h"
39 #include "vf.h"
40 #include "libvo/fastmemcpy.h"
41 #include "libswscale/swscale.h"
42 #include "vf_scale.h"
45 //===========================================================================//
47 typedef struct FilterParam{
48 float radius;
49 float preFilterRadius;
50 float strength;
51 float quality;
52 struct SwsContext *preFilterContext;
53 uint8_t *preFilterBuf;
54 int preFilterStride;
55 int distWidth;
56 int distStride;
57 int *distCoeff;
58 int colorDiffCoeff[512];
59 }FilterParam;
61 struct vf_priv_s {
62 FilterParam luma;
63 FilterParam chroma;
67 /***************************************************************************/
69 //FIXME stupid code duplication
70 static void getSubSampleFactors(int *h, int *v, int format){
71 switch(format){
72 case IMGFMT_YV12:
73 case IMGFMT_I420:
74 *h=1;
75 *v=1;
76 break;
77 case IMGFMT_YVU9:
78 *h=2;
79 *v=2;
80 break;
81 case IMGFMT_444P:
82 *h=0;
83 *v=0;
84 break;
85 case IMGFMT_422P:
86 *h=1;
87 *v=0;
88 break;
89 case IMGFMT_411P:
90 *h=2;
91 *v=0;
92 break;
96 static int allocStuff(FilterParam *f, int width, int height){
97 int stride= (width+7)&~7;
98 SwsVector *vec;
99 SwsFilter swsF;
100 int i,x,y;
101 f->preFilterBuf= (uint8_t*)memalign(8, stride*height);
102 f->preFilterStride= stride;
104 vec = sws_getGaussianVec(f->preFilterRadius, f->quality);
105 swsF.lumH= swsF.lumV= vec;
106 swsF.chrH= swsF.chrV= NULL;
107 f->preFilterContext= sws_getContext(
108 width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, get_sws_cpuflags(), &swsF, NULL, NULL);
110 sws_freeVec(vec);
111 vec = sws_getGaussianVec(f->strength, 5.0);
112 for(i=0; i<512; i++){
113 double d;
114 int index= i-256 + vec->length/2;
116 if(index<0 || index>=vec->length) d= 0.0;
117 else d= vec->coeff[index];
119 f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
121 sws_freeVec(vec);
122 vec = sws_getGaussianVec(f->radius, f->quality);
123 f->distWidth= vec->length;
124 f->distStride= (vec->length+7)&~7;
125 f->distCoeff= (int32_t*)memalign(8, f->distWidth*f->distStride*sizeof(int32_t));
127 for(y=0; y<vec->length; y++){
128 for(x=0; x<vec->length; x++){
129 double d= vec->coeff[x] * vec->coeff[y];
131 f->distCoeff[x + y*f->distStride]= (int)(d*(1<<10) + 0.5);
132 // if(y==vec->length/2)
133 // printf("%6d ", f->distCoeff[x + y*f->distStride]);
136 sws_freeVec(vec);
138 return 0;
141 static int config(struct vf_instance_s* vf,
142 int width, int height, int d_width, int d_height,
143 unsigned int flags, unsigned int outfmt){
145 int sw, sh;
146 //asm volatile("emms\n\t");
147 allocStuff(&vf->priv->luma, width, height);
149 getSubSampleFactors(&sw, &sh, outfmt);
150 allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
152 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
155 static void freeBuffers(FilterParam *f){
156 if(f->preFilterContext) sws_freeContext(f->preFilterContext);
157 f->preFilterContext=NULL;
159 if(f->preFilterBuf) free(f->preFilterBuf);
160 f->preFilterBuf=NULL;
162 if(f->distCoeff) free(f->distCoeff);
163 f->distCoeff=NULL;
166 static void uninit(struct vf_instance_s* vf){
167 if(!vf->priv) return;
169 freeBuffers(&vf->priv->luma);
170 freeBuffers(&vf->priv->chroma);
172 free(vf->priv);
173 vf->priv=NULL;
176 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
177 int x, y;
178 FilterParam f= *fp;
179 const int radius= f.distWidth/2;
180 uint8_t *srcArray[3]= {src, NULL, NULL};
181 uint8_t *dstArray[3]= {f.preFilterBuf, NULL, NULL};
182 int srcStrideArray[3]= {srcStride, 0, 0};
183 int dstStrideArray[3]= {f.preFilterStride, 0, 0};
185 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
186 sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
188 for(y=0; y<h; y++){
189 for(x=0; x<w; x++){
190 int sum=0;
191 int div=0;
192 int dy;
193 const int preVal= f.preFilterBuf[x + y*f.preFilterStride];
194 #if 0
195 const int srcVal= src[x + y*srcStride];
196 if((x/32)&1){
197 dst[x + y*dstStride]= srcVal;
198 if(y%32==0) dst[x + y*dstStride]= 0;
199 continue;
201 #endif
202 if(x >= radius && x < w - radius){
203 for(dy=0; dy<radius*2+1; dy++){
204 int dx;
205 int iy= y+dy - radius;
206 if (iy<0) iy= -iy;
207 else if(iy>=h) iy= h-iy-1;
209 for(dx=0; dx<radius*2+1; dx++){
210 const int ix= x+dx - radius;
211 int factor;
213 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
214 *f.distCoeff[dx + dy*f.distStride];
215 sum+= src[ix + iy*srcStride] *factor;
216 div+= factor;
219 }else{
220 for(dy=0; dy<radius*2+1; dy++){
221 int dx;
222 int iy= y+dy - radius;
223 if (iy<0) iy= -iy;
224 else if(iy>=h) iy= h-iy-1;
226 for(dx=0; dx<radius*2+1; dx++){
227 int ix= x+dx - radius;
228 int factor;
229 if (ix<0) ix= -ix;
230 else if(ix>=w) ix= w-ix-1;
232 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
233 *f.distCoeff[dx + dy*f.distStride];
234 sum+= src[ix + iy*srcStride] *factor;
235 div+= factor;
239 dst[x + y*dstStride]= (sum + div/2)/div;
244 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
245 int cw= mpi->w >> mpi->chroma_x_shift;
246 int ch= mpi->h >> mpi->chroma_y_shift;
248 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
249 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
250 mpi->w,mpi->h);
252 assert(mpi->flags&MP_IMGFLAG_PLANAR);
254 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
255 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
256 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
258 return vf_next_put_image(vf,dmpi, pts);
261 //===========================================================================//
263 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
264 switch(fmt)
266 case IMGFMT_YV12:
267 case IMGFMT_I420:
268 case IMGFMT_IYUV:
269 case IMGFMT_YVU9:
270 case IMGFMT_444P:
271 case IMGFMT_422P:
272 case IMGFMT_411P:
273 return vf_next_query_format(vf, fmt);
275 return 0;
278 static int open(vf_instance_t *vf, char* args){
279 int e;
281 vf->config=config;
282 vf->put_image=put_image;
283 // vf->get_image=get_image;
284 vf->query_format=query_format;
285 vf->uninit=uninit;
286 vf->priv=malloc(sizeof(struct vf_priv_s));
287 memset(vf->priv, 0, sizeof(struct vf_priv_s));
289 if(args==NULL) return 0;
291 e=sscanf(args, "%f:%f:%f:%f:%f:%f",
292 &vf->priv->luma.radius,
293 &vf->priv->luma.preFilterRadius,
294 &vf->priv->luma.strength,
295 &vf->priv->chroma.radius,
296 &vf->priv->chroma.preFilterRadius,
297 &vf->priv->chroma.strength
300 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
302 if(e==3){
303 vf->priv->chroma.radius= vf->priv->luma.radius;
304 vf->priv->chroma.preFilterRadius = vf->priv->luma.preFilterRadius;
305 vf->priv->chroma.strength= vf->priv->luma.strength;
306 }else if(e!=6)
307 return 0;
309 // if(vf->priv->luma.radius < 0) return 0;
310 // if(vf->priv->chroma.radius < 0) return 0;
312 return 1;
315 vf_info_t vf_info_sab = {
316 "shape adaptive blur",
317 "sab",
318 "Michael Niedermayer",
320 open,
321 NULL
324 //===========================================================================//