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 "libavutil/mem.h"
36 #include "img_format.h"
37 #include "mp_image.h"
38 #include "vf.h"
39 #include "libswscale/swscale.h"
40 #include "vf_scale.h"
41 
42 
43 //===========================================================================//
44 
45 typedef struct FilterParam{
46  float radius;
48  float strength;
49  float quality;
53  int distWidth;
55  int *distCoeff;
56  int colorDiffCoeff[512];
58 
59 struct vf_priv_s {
62 };
63 
64 
65 /***************************************************************************/
66 
67 //FIXME stupid code duplication
68 static void getSubSampleFactors(int *h, int *v, int format){
69  switch(format){
70  default:
71  assert(0);
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;
93  }
94 }
95 
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= av_malloc(stride*height);
103 
105  swsF.lumH= swsF.lumV= vec;
106  swsF.chrH= swsF.chrV= NULL;
108  width, height, AV_PIX_FMT_GRAY8, width, height, AV_PIX_FMT_GRAY8, SWS_POINT, &swsF, NULL, NULL);
109 
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;
115 
116  if(index<0 || index>=vec->length) d= 0.0;
117  else d= vec->coeff[index];
118 
119  f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
120  }
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= av_malloc(f->distWidth*f->distStride*sizeof(int32_t));
126 
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];
130 
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]);
134  }
135  }
136  sws_freeVec(vec);
137 
138  return 0;
139 }
140 
141 static int config(struct vf_instance *vf,
142  int width, int height, int d_width, int d_height,
143  unsigned int flags, unsigned int outfmt){
144 
145  int sw, sh;
146 //__asm__ volatile("emms\n\t");
147  allocStuff(&vf->priv->luma, width, height);
148 
149  getSubSampleFactors(&sw, &sh, outfmt);
150  allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
151 
152  return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
153 }
154 
155 static void freeBuffers(FilterParam *f){
158 
159  av_free(f->preFilterBuf);
160  f->preFilterBuf=NULL;
161 
162  av_free(f->distCoeff);
163  f->distCoeff=NULL;
164 }
165 
166 static void uninit(struct vf_instance *vf){
167  if(!vf->priv) return;
168 
169  freeBuffers(&vf->priv->luma);
170  freeBuffers(&vf->priv->chroma);
171 
172  free(vf->priv);
173  vf->priv=NULL;
174 }
175 
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  const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
181  uint8_t *dstArray[MP_MAX_PLANES]= {f.preFilterBuf};
182  int srcStrideArray[MP_MAX_PLANES]= {srcStride};
183  int dstStrideArray[MP_MAX_PLANES]= {f.preFilterStride};
184 
185 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
186  sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
187 
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;
200 }
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+h-iy-1;
208 
209  for(dx=0; dx<radius*2+1; dx++){
210  const int ix= x+dx - radius;
211  int factor;
212 
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;
217  }
218  }
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+h-iy-1;
225 
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+w-ix-1;
231 
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;
236  }
237  }
238  }
239  dst[x + y*dstStride]= (sum + div/2)/div;
240  }
241  }
242 }
243 
244 static int put_image(struct vf_instance *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;
247 
248  mp_image_t *dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
250  mpi->w,mpi->h);
251 
252  assert(mpi->flags&MP_IMGFLAG_PLANAR);
253 
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);
257 
258  return ff_vf_next_put_image(vf,dmpi, pts);
259 }
260 
261 //===========================================================================//
262 
263 static int query_format(struct vf_instance *vf, unsigned int fmt){
264  switch(fmt)
265  {
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 ff_vf_next_query_format(vf, fmt);
274  }
275  return 0;
276 }
277 
278 static int vf_open(vf_instance_t *vf, char *args){
279  int e;
280 
281  vf->config=config;
282  vf->put_image=put_image;
283 // vf->get_image=get_image;
285  vf->uninit=uninit;
286  vf->priv=malloc(sizeof(struct vf_priv_s));
287  memset(vf->priv, 0, sizeof(struct vf_priv_s));
288 
289  if(args==NULL) return 0;
290 
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,
297  &vf->priv->chroma.strength
298  );
299 
300  vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
301 
302  if(e==3){
303  vf->priv->chroma.radius= vf->priv->luma.radius;
305  vf->priv->chroma.strength= vf->priv->luma.strength;
306  }else if(e!=6)
307  return 0;
308 
309 // if(vf->priv->luma.radius < 0) return 0;
310 // if(vf->priv->chroma.radius < 0) return 0;
311 
312  return 1;
313 }
314 
316  "shape adaptive blur",
317  "sab",
318  "Michael Niedermayer",
319  "",
320  vf_open,
321  NULL
322 };
323 
324 //===========================================================================//
SwsVector * chrV
Definition: swscale.h:132
unsigned int imgfmt
Definition: mp_image.h:130
float v
#define SWS_POINT
Definition: swscale.h:62
void sws_freeVec(SwsVector *a)
float radius
Definition: vf_sab.c:46
int preFilterStride
Definition: vf_sab.c:52
FilterParam luma
Definition: vf_sab.c:60
const char * fmt
Definition: avisynth_c.h:669
SwsVector * lumV
Definition: swscale.h:130
memory handling functions
int distStride
Definition: vf_sab.c:54
Sinusoidal phase f
mp_image_t * ff_vf_get_image(vf_instance_t *vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h)
Definition: vf_mp.c:380
external API header
struct SwsContext * preFilterContext
Definition: vf_sab.c:50
static int query_format(struct vf_instance *vf, unsigned int fmt)
Definition: vf_sab.c:263
#define IMGFMT_YVU9
Definition: img_format.h:117
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
#define IMGFMT_YV12
Definition: img_format.h:119
int stride
Definition: mace.c:144
void(* uninit)(struct vf_instance *vf)
Definition: vf.h:74
output residual component w
int ff_vf_next_config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt)
Definition: vf_mp.c:584
set threshold d
float strength
Definition: vf_sab.c:48
int(* put_image)(struct vf_instance *vf, mp_image_t *mpi, double pts)
Definition: vf.h:68
uint8_t
int length
number of coefficients in the vector
Definition: swscale.h:124
int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
swscale wrapper, so we don&#39;t need to export the SwsContext.
Definition: swscale.c:798
Definition: vf.h:31
unsigned char * planes[MP_MAX_PLANES]
Definition: mp_image.h:133
int colorDiffCoeff[512]
Definition: vf_sab.c:56
int stride[MP_MAX_PLANES]
Definition: mp_image.h:134
external API header
static void freeBuffers(FilterParam *f)
Definition: vf_sab.c:155
Discrete Time axis x
static void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp)
Definition: vf_sab.c:176
static int allocStuff(FilterParam *f, int width, int height)
Definition: vf_sab.c:96
static void getSubSampleFactors(int *h, int *v, int format)
Definition: vf_sab.c:68
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
SwsVector * sws_getGaussianVec(double variance, double quality)
Return a normalized Gaussian curve used to filter stuff quality = 3 is high quality, lower is lower quality.
int distWidth
Definition: vf_sab.c:53
#define MP_IMGTYPE_TEMP
Definition: mp_image.h:104
int(* query_format)(struct vf_instance *vf, unsigned int fmt)
Definition: vf.h:64
static int vf_open(vf_instance_t *vf, char *args)
Definition: vf_sab.c:278
#define IMGFMT_IYUV
Definition: img_format.h:121
SwsVector * lumH
Definition: swscale.h:129
float quality
Definition: vf_sab.c:49
SwsVector * chrH
Definition: swscale.h:131
int32_t
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
int chroma_y_shift
Definition: mp_image.h:145
struct vf_instance * next
Definition: vf.h:84
NULL
Definition: eval.c:55
double * coeff
pointer to the list of coefficients
Definition: swscale.h:123
static int width
Definition: tests/utils.c:158
AVS_Value src
Definition: avisynth_c.h:523
normalize analysis window sw
int * distCoeff
Definition: vf_sab.c:55
struct FilterParam FilterParam
#define fp
Definition: regdef.h:44
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
uint8_t * preFilterBuf
Definition: vf_sab.c:51
int index
Definition: gxfenc.c:89
synthesis window for stochastic i
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
Definition: vf_sab.c:244
static const int factor[16]
Definition: vf_pp7.c:202
#define IMGFMT_422P
Definition: img_format.h:131
static void uninit(struct vf_instance *vf)
Definition: vf_sab.c:166
int w
Definition: mp_image.h:132
int(* config)(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt)
Definition: vf.h:59
static int flags
Definition: cpu.c:23
#define MP_MAX_PLANES
Definition: mp_image.h:116
#define IMGFMT_444P
Definition: img_format.h:130
Y , 8bpp.
Definition: pixfmt.h:76
#define MP_IMGFLAG_PLANAR
Definition: mp_image.h:76
function y
Definition: D.m:1
int ff_vf_next_put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
Definition: vf_mp.c:539
static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt)
Definition: vf_sab.c:141
float preFilterRadius
Definition: vf_sab.c:47
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
int chroma_x_shift
Definition: mp_image.h:144
#define MP_IMGFLAG_ACCEPT_STRIDE
Definition: mp_image.h:63
struct vf_priv_s * priv
Definition: vf.h:86
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
#define IMGFMT_I420
Definition: img_format.h:120
int h
Definition: mp_image.h:132
const vf_info_t ff_vf_info_sab
Definition: vf_sab.c:315
FilterParam chroma
Definition: vf_sab.c:61
int ff_vf_next_query_format(struct vf_instance *vf, unsigned int fmt)
Definition: vf_mp.c:371
Definition: vf.h:56
unsigned int flags
Definition: mp_image.h:126
#define IMGFMT_411P
Definition: img_format.h:132
MUSIC TECHNOLOGY GROUP UNIVERSITAT POMPEU FABRA Free Non Commercial Binary License Agreement UNIVERSITAT POMPEU OR INDICATING ACCEPTANCE BY SELECTING THE ACCEPT BUTTON ON ANY DOWNLOAD OR INSTALL YOU ACCEPT THE TERMS OF THE LICENSE SUMMARY TABLE Software MELODIA Melody Extraction vamp plug in Licensor Music Technology Group Universitat Pompeu Plaça de la Spain Permitted purposes Non commercial internal research and validation and educational purposes only All commercial uses in a production either internal or are prohibited by this license and require an additional commercial exploitation license TERMS AND CONDITIONS SOFTWARE Software means the software programs identified herein in binary any other machine readable any updates or error corrections provided by and any user programming guides and other documentation provided to you by UPF under this Agreement LICENSE Subject to the terms and conditions of this UPF grants you a royalty free