FFmpeg  2.1.1
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 FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 /**
22  * @file
23  * Shape Adaptive Blur filter, ported from MPlayer libmpcodecs/vf_sab.c
24  */
25 
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libswscale/swscale.h"
29 
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 
34 typedef struct {
35  float radius;
37  float strength;
38  float quality;
44  int *dist_coeff;
45 #define COLOR_DIFF_COEFF_SIZE 512
46  int color_diff_coeff[COLOR_DIFF_COEFF_SIZE];
47 } FilterParam;
48 
49 typedef struct {
50  const AVClass *class;
53  int hsub;
54  int vsub;
55  unsigned int sws_flags;
56 } SabContext;
57 
59 {
60  static const enum AVPixelFormat pix_fmts[] = {
67  };
69 
70  return 0;
71 }
72 
73 #define RADIUS_MIN 0.1
74 #define RADIUS_MAX 4.0
75 
76 #define PRE_FILTER_RADIUS_MIN 0.1
77 #define PRE_FILTER_RADIUS_MAX 2.0
78 
79 #define STRENGTH_MIN 0.1
80 #define STRENGTH_MAX 100.0
81 
82 #define OFFSET(x) offsetof(SabContext, x)
83 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
84 
85 static const AVOption sab_options[] = {
86  { "luma_radius", "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
87  { "lr" , "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
88  { "luma_pre_filter_radius", "set luma pre-filter radius", OFFSET(luma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, PRE_FILTER_RADIUS_MIN, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
89  { "lpfr", "set luma pre-filter radius", OFFSET(luma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, PRE_FILTER_RADIUS_MIN, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
90  { "luma_strength", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
91  { "ls", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
92 
93  { "chroma_radius", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
94  { "cr", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
95  { "chroma_pre_filter_radius", "set chroma pre-filter radius", OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
97  { "cpfr", "set chroma pre-filter radius", OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
99  { "chroma_strength", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
100  { "cs", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
101 
102  { NULL }
103 };
104 
106 
107 static av_cold int init(AVFilterContext *ctx)
108 {
109  SabContext *sab = ctx->priv;
110 
111  /* make chroma default to luma values, if not explicitly set */
112  if (sab->chroma.radius < RADIUS_MIN)
113  sab->chroma.radius = sab->luma.radius;
116  if (sab->chroma.strength < STRENGTH_MIN)
117  sab->chroma.strength = sab->luma.strength;
118 
119  sab->luma.quality = sab->chroma.quality = 3.0;
120  sab->sws_flags = SWS_POINT;
121 
122  av_log(ctx, AV_LOG_VERBOSE,
123  "luma_radius:%f luma_pre_filter_radius::%f luma_strength:%f "
124  "chroma_radius:%f chroma_pre_filter_radius:%f chroma_strength:%f\n",
125  sab->luma .radius, sab->luma .pre_filter_radius, sab->luma .strength,
127  return 0;
128 }
129 
131 {
132  if (f->pre_filter_context) {
134  f->pre_filter_context = NULL;
135  }
137  av_freep(&f->dist_coeff);
138 }
139 
140 static av_cold void uninit(AVFilterContext *ctx)
141 {
142  SabContext *sab = ctx->priv;
143 
144  close_filter_param(&sab->luma);
145  close_filter_param(&sab->chroma);
146 }
147 
148 static int open_filter_param(FilterParam *f, int width, int height, unsigned int sws_flags)
149 {
150  SwsVector *vec;
151  SwsFilter sws_f;
152  int i, x, y;
153  int linesize = FFALIGN(width, 8);
154 
155  f->pre_filter_buf = av_malloc(linesize * height);
156  if (!f->pre_filter_buf)
157  return AVERROR(ENOMEM);
158 
159  f->pre_filter_linesize = linesize;
161  sws_f.lumH = sws_f.lumV = vec;
162  sws_f.chrH = sws_f.chrV = NULL;
164  width, height, AV_PIX_FMT_GRAY8,
165  sws_flags, &sws_f, NULL, NULL);
166  sws_freeVec(vec);
167 
168  vec = sws_getGaussianVec(f->strength, 5.0);
169  for (i = 0; i < COLOR_DIFF_COEFF_SIZE; i++) {
170  double d;
171  int index = i-COLOR_DIFF_COEFF_SIZE/2 + vec->length/2;
172 
173  if (index < 0 || index >= vec->length) d = 0.0;
174  else d = vec->coeff[index];
175 
176  f->color_diff_coeff[i] = (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
177  }
178  sws_freeVec(vec);
179 
180  vec = sws_getGaussianVec(f->radius, f->quality);
181  f->dist_width = vec->length;
182  f->dist_linesize = FFALIGN(vec->length, 8);
183  f->dist_coeff = av_malloc(f->dist_width * f->dist_linesize * sizeof(*f->dist_coeff));
184  if (!f->dist_coeff) {
185  sws_freeVec(vec);
186  return AVERROR(ENOMEM);
187  }
188 
189  for (y = 0; y < vec->length; y++) {
190  for (x = 0; x < vec->length; x++) {
191  double d = vec->coeff[x] * vec->coeff[y];
192  f->dist_coeff[x + y*f->dist_linesize] = (int)(d*(1<<10) + 0.5);
193  }
194  }
195  sws_freeVec(vec);
196 
197  return 0;
198 }
199 
200 static int config_props(AVFilterLink *inlink)
201 {
202  SabContext *sab = inlink->dst->priv;
203  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
204  int ret;
205 
206  sab->hsub = desc->log2_chroma_w;
207  sab->vsub = desc->log2_chroma_h;
208 
209  close_filter_param(&sab->luma);
210  ret = open_filter_param(&sab->luma, inlink->w, inlink->h, sab->sws_flags);
211  if (ret < 0)
212  return ret;
213 
214  close_filter_param(&sab->chroma);
215  ret = open_filter_param(&sab->chroma,
216  FF_CEIL_RSHIFT(inlink->w, sab->hsub),
217  FF_CEIL_RSHIFT(inlink->h, sab->vsub), sab->sws_flags);
218  return ret;
219 }
220 
221 #define NB_PLANES 4
222 
223 static void blur(uint8_t *dst, const int dst_linesize,
224  const uint8_t *src, const int src_linesize,
225  const int w, const int h, FilterParam *fp)
226 {
227  int x, y;
228  FilterParam f = *fp;
229  const int radius = f.dist_width/2;
230 
231  const uint8_t * const src2[NB_PLANES] = { src };
232  int src2_linesize[NB_PLANES] = { src_linesize };
233  uint8_t *dst2[NB_PLANES] = { f.pre_filter_buf };
234  int dst2_linesize[NB_PLANES] = { f.pre_filter_linesize };
235 
236  sws_scale(f.pre_filter_context, src2, src2_linesize, 0, h, dst2, dst2_linesize);
237 
238 #define UPDATE_FACTOR do { \
239  int factor; \
240  factor = f.color_diff_coeff[COLOR_DIFF_COEFF_SIZE/2 + pre_val - \
241  f.pre_filter_buf[ix + iy*f.pre_filter_linesize]] * f.dist_coeff[dx + dy*f.dist_linesize]; \
242  sum += src[ix + iy*src_linesize] * factor; \
243  div += factor; \
244  } while (0)
245 
246  for (y = 0; y < h; y++) {
247  for (x = 0; x < w; x++) {
248  int sum = 0;
249  int div = 0;
250  int dy;
251  const int pre_val = f.pre_filter_buf[x + y*f.pre_filter_linesize];
252  if (x >= radius && x < w - radius) {
253  for (dy = 0; dy < radius*2 + 1; dy++) {
254  int dx;
255  int iy = y+dy - radius;
256  if (iy < 0) iy = -iy;
257  else if (iy >= h) iy = h+h-iy-1;
258 
259  for (dx = 0; dx < radius*2 + 1; dx++) {
260  const int ix = x+dx - radius;
262  }
263  }
264  } else {
265  for (dy = 0; dy < radius*2+1; dy++) {
266  int dx;
267  int iy = y+dy - radius;
268  if (iy < 0) iy = -iy;
269  else if (iy >= h) iy = h+h-iy-1;
270 
271  for (dx = 0; dx < radius*2 + 1; dx++) {
272  int ix = x+dx - radius;
273  if (ix < 0) ix = -ix;
274  else if (ix >= w) ix = w+w-ix-1;
276  }
277  }
278  }
279  dst[x + y*dst_linesize] = (sum + div/2) / div;
280  }
281  }
282 }
283 
284 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
285 {
286  SabContext *sab = inlink->dst->priv;
287  AVFilterLink *outlink = inlink->dst->outputs[0];
288  AVFrame *outpic;
289 
290  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
291  if (!outpic) {
292  av_frame_free(&inpic);
293  return AVERROR(ENOMEM);
294  }
295  av_frame_copy_props(outpic, inpic);
296 
297  blur(outpic->data[0], outpic->linesize[0], inpic->data[0], inpic->linesize[0],
298  inlink->w, inlink->h, &sab->luma);
299  if (inpic->data[2]) {
300  int cw = FF_CEIL_RSHIFT(inlink->w, sab->hsub);
301  int ch = FF_CEIL_RSHIFT(inlink->h, sab->vsub);
302  blur(outpic->data[1], outpic->linesize[1], inpic->data[1], inpic->linesize[1], cw, ch, &sab->chroma);
303  blur(outpic->data[2], outpic->linesize[2], inpic->data[2], inpic->linesize[2], cw, ch, &sab->chroma);
304  }
305 
306  av_frame_free(&inpic);
307  return ff_filter_frame(outlink, outpic);
308 }
309 
310 static const AVFilterPad sab_inputs[] = {
311  {
312  .name = "default",
313  .type = AVMEDIA_TYPE_VIDEO,
314  .filter_frame = filter_frame,
315  .config_props = config_props,
316  },
317  { NULL }
318 };
319 
320 static const AVFilterPad sab_outputs[] = {
321  {
322  .name = "default",
323  .type = AVMEDIA_TYPE_VIDEO,
324  },
325  { NULL }
326 };
327 
329  .name = "sab",
330  .description = NULL_IF_CONFIG_SMALL("Apply shape adaptive blur."),
331  .priv_size = sizeof(SabContext),
332  .init = init,
333  .uninit = uninit,
335  .inputs = sab_inputs,
336  .outputs = sab_outputs,
337  .priv_class = &sab_class,
339 };
#define SWS_POINT
Definition: swscale.h:62
void sws_freeVec(SwsVector *a)
Definition: utils.c:1963
float radius
Definition: vf_sab.c:35
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVOption.
Definition: opt.h:253
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
static const AVFilterPad sab_outputs[]
Definition: vf_sab.c:320
planar YUV 4:2:2, 16bpp, (1 Cr &amp; Cb sample per 2x1 Y samples)
Definition: avcodec.h:4538
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static const AVOption sab_options[]
Definition: vf_sab.c:85
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
unsigned int sws_flags
Definition: vf_sab.c:55
Pixel format.
Definition: avcodec.h:4533
#define av_cold
Definition: avcodec.h:653
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:68
Y , 8bpp.
Definition: avcodec.h:4542
int dist_linesize
Definition: vf_sab.c:43
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
static void blur(uint8_t *dst, const int dst_linesize, const uint8_t *src, const int src_linesize, const int w, const int h, FilterParam *fp)
Definition: vf_sab.c:223
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic &quot;enable&quot; expression option that can be used to enable or disable a fil...
Definition: avfilter.h:445
float strength
Definition: vf_sab.c:37
const char * name
Pad name.
Definition: internal.h:66
int pre_filter_linesize
Definition: vf_sab.c:41
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
uint8_t
int length
number of coefficients in the vector
Definition: swscale.h:124
#define PRE_FILTER_RADIUS_MIN
Definition: vf_sab.c:76
static int sws_flags
Definition: muxing.c:49
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only &quot;metadata&quot; fields from src to dst.
Definition: frame.c:446
int vsub
Definition: vf_sab.c:54
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst...
Definition: swscale.c:913
av_frame_free & inpic
Definition: vf_mcdeint.c:280
#define COLOR_DIFF_COEFF_SIZE
Definition: vf_sab.c:45
#define FF_CEIL_RSHIFT(a, b)
Definition: avcodec.h:916
float pre_filter_radius
Definition: vf_sab.c:36
#define AV_LOG_VERBOSE
Detailed information.
Definition: avcodec.h:4163
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:531
AVFilter avfilter_vf_sab
Definition: vf_sab.c:328
#define FLAGS
Definition: vf_sab.c:83
A filter pad used for either input or output.
Definition: internal.h:60
static void close_filter_param(FilterParam *f)
Definition: vf_sab.c:130
SwsVector * chrH
Definition: swscale.h:131
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:77
int hsub
Definition: vf_sab.c:53
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
planar YUV 4:1:1, 12bpp, (1 Cr &amp; Cb sample per 4x1 Y samples)
Definition: avcodec.h:4541
static int query_formats(AVFilterContext *ctx)
Definition: vf_sab.c:58
int color_diff_coeff[COLOR_DIFF_COEFF_SIZE]
Definition: vf_sab.c:46
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
static const AVFilterPad sab_inputs[]
Definition: vf_sab.c:310
#define UPDATE_FACTOR
SwsVector * lumV
Definition: swscale.h:130
#define RADIUS_MIN
Definition: vf_sab.c:73
float y
float quality
Definition: vf_sab.c:38
#define RADIUS_MAX
Definition: vf_sab.c:74
ret
Definition: avfilter.c:961
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static int config_props(AVFilterLink *inlink)
Definition: vf_sab.c:200
FilterParam chroma
Definition: vf_sab.c:52
Main libavfilter public API header.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1938
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
static int width
Definition: utils.c:158
AVS_Value src
Definition: avisynth_c.h:523
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.
Definition: utils.c:1750
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:57
#define fp
Definition: regdef.h:44
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.
Definition: utils.c:1639
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:464
int index
Definition: gxfenc.c:89
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_sab.c:284
planar YUV 4:4:4, 24bpp, (1 Cr &amp; Cb sample per 1x1 Y samples)
Definition: avcodec.h:4539
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
#define STRENGTH_MAX
Definition: vf_sab.c:80
static int flags
Definition: cpu.c:45
uint8_t * pre_filter_buf
Definition: vf_sab.c:40
SwsVector * chrV
Definition: swscale.h:132
double * coeff
pointer to the list of coefficients
Definition: swscale.h:123
struct SwsContext * pre_filter_context
Definition: vf_sab.c:39
#define FFALIGN(x, a)
Definition: avcodec.h:930
#define PRE_FILTER_RADIUS_MAX
Definition: vf_sab.c:77
FilterParam luma
Definition: vf_sab.c:51
#define STRENGTH_MIN
Definition: vf_sab.c:79
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
int dist_width
Definition: vf_sab.c:42
#define AVERROR(e)
An instance of a filter.
Definition: avfilter.h:627
static int open_filter_param(FilterParam *f, int width, int height, unsigned int sws_flags)
Definition: vf_sab.c:148
static av_cold int init(AVFilterContext *ctx)
Definition: vf_sab.c:107
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:1984
int * dist_coeff
Definition: vf_sab.c:44
#define OFFSET(x)
Definition: vf_sab.c:82
internal API functions
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_sab.c:140
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
planar YUV 4:1:0, 9bpp, (1 Cr &amp; Cb sample per 4x4 Y samples)
Definition: avcodec.h:4540
SwsVector * lumH
Definition: swscale.h:129
#define NB_PLANES
Definition: vf_sab.c:221