FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_smartblur.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (c) 2012 Jeremy Tran
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 /**
23  * @file
24  * Apply a smartblur filter to the input video
25  * Ported from MPlayer libmpcodecs/vf_smartblur.c by Michael Niedermayer.
26  */
27 
28 #include "libavutil/pixdesc.h"
29 #include "libswscale/swscale.h"
30 
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 
35 #define RADIUS_MIN 0.1
36 #define RADIUS_MAX 5.0
37 
38 #define STRENGTH_MIN -1.0
39 #define STRENGTH_MAX 1.0
40 
41 #define THRESHOLD_MIN -30
42 #define THRESHOLD_MAX 30
43 
44 typedef struct {
45  float radius;
46  float strength;
47  int threshold;
48  float quality;
50 } FilterParam;
51 
52 typedef struct {
55  int hsub;
56  int vsub;
57  unsigned int sws_flags;
59 
60 #define CHECK_PARAM(param, name, min, max, format, ret) \
61  if (param < min || param > max) { \
62  av_log(ctx, AV_LOG_ERROR, \
63  "Invalid " #name " value " #format ": " \
64  "must be included between range " #format " and " #format "\n",\
65  param, min, max); \
66  ret = AVERROR(EINVAL); \
67  }
68 
69 static av_cold int init(AVFilterContext *ctx, const char *args)
70 {
71  SmartblurContext *sblur = ctx->priv;
72  int n = 0, ret = 0;
73  float lradius, lstrength, cradius, cstrength;
74  int lthreshold, cthreshold;
75 
76  if (args)
77  n = sscanf(args, "%f:%f:%d:%f:%f:%d",
78  &lradius, &lstrength, &lthreshold,
79  &cradius, &cstrength, &cthreshold);
80 
81  if (n != 3 && n != 6) {
82  av_log(ctx, AV_LOG_ERROR,
83  "Incorrect number of parameters or invalid syntax: "
84  "must be luma_radius:luma_strength:luma_threshold"
85  "[:chroma_radius:chroma_strength:chroma_threshold]\n");
86  return AVERROR(EINVAL);
87  }
88 
89  sblur->luma.radius = lradius;
90  sblur->luma.strength = lstrength;
91  sblur->luma.threshold = lthreshold;
92 
93  if (n == 3) {
94  sblur->chroma.radius = sblur->luma.radius;
95  sblur->chroma.strength = sblur->luma.strength;
96  sblur->chroma.threshold = sblur->luma.threshold;
97  } else {
98  sblur->chroma.radius = cradius;
99  sblur->chroma.strength = cstrength;
100  sblur->chroma.threshold = cthreshold;
101  }
102 
103  sblur->luma.quality = sblur->chroma.quality = 3.0;
104  sblur->sws_flags = SWS_BICUBIC;
105 
106  CHECK_PARAM(lradius, luma radius, RADIUS_MIN, RADIUS_MAX, %0.1f, ret)
107  CHECK_PARAM(lstrength, luma strength, STRENGTH_MIN, STRENGTH_MAX, %0.1f, ret)
108  CHECK_PARAM(lthreshold, luma threshold, THRESHOLD_MIN, THRESHOLD_MAX, %d, ret)
109 
110  if (n != 3) {
111  CHECK_PARAM(sblur->chroma.radius, chroma radius, RADIUS_MIN, RADIUS_MAX, %0.1f, ret)
112  CHECK_PARAM(sblur->chroma.strength, chroma strength, STRENGTH_MIN, STRENGTH_MAX, %0.1f, ret)
113  CHECK_PARAM(sblur->chroma.threshold, chroma threshold, THRESHOLD_MIN,THRESHOLD_MAX, %d, ret)
114  }
115 
116  return ret;
117 }
118 
119 static av_cold void uninit(AVFilterContext *ctx)
120 {
121  SmartblurContext *sblur = ctx->priv;
122 
125 }
126 
128 {
129  static const enum AVPixelFormat pix_fmts[] = {
135  };
136 
138 
139  return 0;
140 }
141 
142 static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
143 {
144  SwsVector *vec;
145  SwsFilter sws_filter;
146 
147  vec = sws_getGaussianVec(f->radius, f->quality);
148 
149  if (!vec)
150  return AVERROR(EINVAL);
151 
152  sws_scaleVec(vec, f->strength);
153  vec->coeff[vec->length / 2] += 1.0 - f->strength;
154  sws_filter.lumH = sws_filter.lumV = vec;
155  sws_filter.chrH = sws_filter.chrV = NULL;
157  width, height, AV_PIX_FMT_GRAY8,
158  width, height, AV_PIX_FMT_GRAY8,
159  flags, &sws_filter, NULL, NULL);
160 
161  sws_freeVec(vec);
162 
163  if (!f->filter_context)
164  return AVERROR(EINVAL);
165 
166  return 0;
167 }
168 
169 static int config_props(AVFilterLink *inlink)
170 {
171  SmartblurContext *sblur = inlink->dst->priv;
172  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
173 
174  sblur->hsub = desc->log2_chroma_w;
175  sblur->vsub = desc->log2_chroma_h;
176 
177  alloc_sws_context(&sblur->luma, inlink->w, inlink->h, sblur->sws_flags);
178  alloc_sws_context(&sblur->chroma,
179  inlink->w >> sblur->hsub, inlink->h >> sblur->vsub,
180  sblur->sws_flags);
181 
182  return 0;
183 }
184 
185 static void blur(uint8_t *dst, const int dst_linesize,
186  const uint8_t *src, const int src_linesize,
187  const int w, const int h, const int threshold,
188  struct SwsContext *filter_context)
189 {
190  int x, y;
191  int orig, filtered;
192  int diff;
193  /* Declare arrays of 4 to get aligned data */
194  const uint8_t* const src_array[4] = {src};
195  uint8_t *dst_array[4] = {dst};
196  int src_linesize_array[4] = {src_linesize};
197  int dst_linesize_array[4] = {dst_linesize};
198 
199  sws_scale(filter_context, src_array, src_linesize_array,
200  0, h, dst_array, dst_linesize_array);
201 
202  if (threshold > 0) {
203  for (y = 0; y < h; ++y) {
204  for (x = 0; x < w; ++x) {
205  orig = src[x + y * src_linesize];
206  filtered = dst[x + y * dst_linesize];
207  diff = orig - filtered;
208 
209  if (diff > 0) {
210  if (diff > 2 * threshold)
211  dst[x + y * dst_linesize] = orig;
212  else if (diff > threshold)
213  /* add 'diff' and substract 'threshold' from 'filtered' */
214  dst[x + y * dst_linesize] = orig - threshold;
215  } else {
216  if (-diff > 2 * threshold)
217  dst[x + y * dst_linesize] = orig;
218  else if (-diff > threshold)
219  /* add 'diff' and 'threshold' to 'filtered' */
220  dst[x + y * dst_linesize] = orig + threshold;
221  }
222  }
223  }
224  } else if (threshold < 0) {
225  for (y = 0; y < h; ++y) {
226  for (x = 0; x < w; ++x) {
227  orig = src[x + y * src_linesize];
228  filtered = dst[x + y * dst_linesize];
229  diff = orig - filtered;
230 
231  if (diff > 0) {
232  if (diff <= -threshold)
233  dst[x + y * dst_linesize] = orig;
234  else if (diff <= -2 * threshold)
235  /* substract 'diff' and 'threshold' from 'orig' */
236  dst[x + y * dst_linesize] = filtered - threshold;
237  } else {
238  if (diff >= threshold)
239  dst[x + y * dst_linesize] = orig;
240  else if (diff >= 2 * threshold)
241  /* add 'threshold' and substract 'diff' from 'orig' */
242  dst[x + y * dst_linesize] = filtered + threshold;
243  }
244  }
245  }
246  }
247 }
248 
249 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
250 {
251  SmartblurContext *sblur = inlink->dst->priv;
252  AVFilterLink *outlink = inlink->dst->outputs[0];
253  AVFilterBufferRef *outpic;
254  int cw = inlink->w >> sblur->hsub;
255  int ch = inlink->h >> sblur->vsub;
256 
257  outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
258  if (!outpic) {
259  avfilter_unref_bufferp(&inpic);
260  return AVERROR(ENOMEM);
261  }
262  avfilter_copy_buffer_ref_props(outpic, inpic);
263 
264  blur(outpic->data[0], outpic->linesize[0],
265  inpic->data[0], inpic->linesize[0],
266  inlink->w, inlink->h, sblur->luma.threshold,
267  sblur->luma.filter_context);
268 
269  if (inpic->data[2]) {
270  blur(outpic->data[1], outpic->linesize[1],
271  inpic->data[1], inpic->linesize[1],
272  cw, ch, sblur->chroma.threshold,
273  sblur->chroma.filter_context);
274  blur(outpic->data[2], outpic->linesize[2],
275  inpic->data[2], inpic->linesize[2],
276  cw, ch, sblur->chroma.threshold,
277  sblur->chroma.filter_context);
278  }
279 
280  avfilter_unref_bufferp(&inpic);
281  return ff_filter_frame(outlink, outpic);
282 }
283 
284 static const AVFilterPad smartblur_inputs[] = {
285  {
286  .name = "default",
287  .type = AVMEDIA_TYPE_VIDEO,
288  .filter_frame = filter_frame,
289  .config_props = config_props,
290  .min_perms = AV_PERM_READ,
291  },
292  { NULL }
293 };
294 
295 static const AVFilterPad smartblur_outputs[] = {
296  {
297  .name = "default",
298  .type = AVMEDIA_TYPE_VIDEO,
299  },
300  { NULL }
301 };
302 
304  .name = "smartblur",
305  .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
306 
307  .priv_size = sizeof(SmartblurContext),
308 
309  .init = init,
310  .uninit = uninit,
312  .inputs = smartblur_inputs,
313  .outputs = smartblur_outputs,
314 };