FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_sab.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of MPlayer.
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <assert.h>
26 
27 #include "config.h"
28 #include "mp_msg.h"
29 
30 #if HAVE_MALLOC_H
31 #include <malloc.h>
32 #endif
33 
34 #include "libavutil/avutil.h"
35 #include "img_format.h"
36 #include "mp_image.h"
37 #include "vf.h"
38 #include "libswscale/swscale.h"
39 #include "vf_scale.h"
40 
41 
42 //===========================================================================//
43 
44 typedef struct FilterParam{
45  float radius;
47  float strength;
48  float quality;
52  int distWidth;
54  int *distCoeff;
55  int colorDiffCoeff[512];
57 
58 struct vf_priv_s {
61 };
62 
63 
64 /***************************************************************************/
65 
66 //FIXME stupid code duplication
67 static void getSubSampleFactors(int *h, int *v, int format){
68  switch(format){
69  default:
70  assert(0);
71  case IMGFMT_YV12:
72  case IMGFMT_I420:
73  *h=1;
74  *v=1;
75  break;
76  case IMGFMT_YVU9:
77  *h=2;
78  *v=2;
79  break;
80  case IMGFMT_444P:
81  *h=0;
82  *v=0;
83  break;
84  case IMGFMT_422P:
85  *h=1;
86  *v=0;
87  break;
88  case IMGFMT_411P:
89  *h=2;
90  *v=0;
91  break;
92  }
93 }
94 
95 static int allocStuff(FilterParam *f, int width, int height){
96  int stride= (width+7)&~7;
97  SwsVector *vec;
98  SwsFilter swsF;
99  int i,x,y;
100  f->preFilterBuf= av_malloc(stride*height);
102 
104  swsF.lumH= swsF.lumV= vec;
105  swsF.chrH= swsF.chrV= NULL;
107  width, height, AV_PIX_FMT_GRAY8, width, height, AV_PIX_FMT_GRAY8, SWS_POINT, &swsF, NULL, NULL);
108 
109  sws_freeVec(vec);
110  vec = sws_getGaussianVec(f->strength, 5.0);
111  for(i=0; i<512; i++){
112  double d;
113  int index= i-256 + vec->length/2;
114 
115  if(index<0 || index>=vec->length) d= 0.0;
116  else d= vec->coeff[index];
117 
118  f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
119  }
120  sws_freeVec(vec);
121  vec = sws_getGaussianVec(f->radius, f->quality);
122  f->distWidth= vec->length;
123  f->distStride= (vec->length+7)&~7;
124  f->distCoeff= av_malloc(f->distWidth*f->distStride*sizeof(int32_t));
125 
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];
129 
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]);
133  }
134  }
135  sws_freeVec(vec);
136 
137  return 0;
138 }
139 
140 static int config(struct vf_instance *vf,
141  int width, int height, int d_width, int d_height,
142  unsigned int flags, unsigned int outfmt){
143 
144  int sw, sh;
145 //__asm__ volatile("emms\n\t");
146  allocStuff(&vf->priv->luma, width, height);
147 
148  getSubSampleFactors(&sw, &sh, outfmt);
149  allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
150 
151  return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
152 }
153 
154 static void freeBuffers(FilterParam *f){
157 
158  av_free(f->preFilterBuf);
159  f->preFilterBuf=NULL;
160 
161  av_free(f->distCoeff);
162  f->distCoeff=NULL;
163 }
164 
165 static void uninit(struct vf_instance *vf){
166  if(!vf->priv) return;
167 
168  freeBuffers(&vf->priv->luma);
169  freeBuffers(&vf->priv->chroma);
170 
171  free(vf->priv);
172  vf->priv=NULL;
173 }
174 
175 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
176  int x, y;
177  FilterParam f= *fp;
178  const int radius= f.distWidth/2;
179  const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
180  uint8_t *dstArray[MP_MAX_PLANES]= {f.preFilterBuf};
181  int srcStrideArray[MP_MAX_PLANES]= {srcStride};
182  int dstStrideArray[MP_MAX_PLANES]= {f.preFilterStride};
183 
184 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
185  sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
186 
187  for(y=0; y<h; y++){
188  for(x=0; x<w; x++){
189  int sum=0;
190  int div=0;
191  int dy;
192  const int preVal= f.preFilterBuf[x + y*f.preFilterStride];
193 #if 0
194  const int srcVal= src[x + y*srcStride];
195 if((x/32)&1){
196  dst[x + y*dstStride]= srcVal;
197  if(y%32==0) dst[x + y*dstStride]= 0;
198  continue;
199 }
200 #endif
201  if(x >= radius && x < w - radius){
202  for(dy=0; dy<radius*2+1; dy++){
203  int dx;
204  int iy= y+dy - radius;
205  if (iy<0) iy= -iy;
206  else if(iy>=h) iy= h+h-iy-1;
207 
208  for(dx=0; dx<radius*2+1; dx++){
209  const int ix= x+dx - radius;
210  int factor;
211 
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;
215  div+= factor;
216  }
217  }
218  }else{
219  for(dy=0; dy<radius*2+1; dy++){
220  int dx;
221  int iy= y+dy - radius;
222  if (iy<0) iy= -iy;
223  else if(iy>=h) iy= h+h-iy-1;
224 
225  for(dx=0; dx<radius*2+1; dx++){
226  int ix= x+dx - radius;
227  int factor;
228  if (ix<0) ix= -ix;
229  else if(ix>=w) ix= w+w-ix-1;
230 
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;
234  div+= factor;
235  }
236  }
237  }
238  dst[x + y*dstStride]= (sum + div/2)/div;
239  }
240  }
241 }
242 
243 static int put_image(struct vf_instance *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;
246 
247  mp_image_t *dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
249  mpi->w,mpi->h);
250 
251  assert(mpi->flags&MP_IMGFLAG_PLANAR);
252 
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);
256 
257  return ff_vf_next_put_image(vf,dmpi, pts);
258 }
259 
260 //===========================================================================//
261 
262 static int query_format(struct vf_instance *vf, unsigned int fmt){
263  switch(fmt)
264  {
265  case IMGFMT_YV12:
266  case IMGFMT_I420:
267  case IMGFMT_IYUV:
268  case IMGFMT_YVU9:
269  case IMGFMT_444P:
270  case IMGFMT_422P:
271  case IMGFMT_411P:
272  return ff_vf_next_query_format(vf, fmt);
273  }
274  return 0;
275 }
276 
277 static int vf_open(vf_instance_t *vf, char *args){
278  int e;
279 
280  vf->config=config;
281  vf->put_image=put_image;
282 // vf->get_image=get_image;
284  vf->uninit=uninit;
285  vf->priv=malloc(sizeof(struct vf_priv_s));
286  memset(vf->priv, 0, sizeof(struct vf_priv_s));
287 
288  if(args==NULL) return 0;
289 
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,
296  &vf->priv->chroma.strength
297  );
298 
299  vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
300 
301  if(e==3){
302  vf->priv->chroma.radius= vf->priv->luma.radius;
304  vf->priv->chroma.strength= vf->priv->luma.strength;
305  }else if(e!=6)
306  return 0;
307 
308 // if(vf->priv->luma.radius < 0) return 0;
309 // if(vf->priv->chroma.radius < 0) return 0;
310 
311  return 1;
312 }
313 
315  "shape adaptive blur",
316  "sab",
317  "Michael Niedermayer",
318  "",
319  vf_open,
320  NULL
321 };
322 
323 //===========================================================================//