FFmpeg
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/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libswscale/swscale.h"
31 
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "video.h"
35 
36 #define RADIUS_MIN 0.1
37 #define RADIUS_MAX 5.0
38 
39 #define STRENGTH_MIN -1.0
40 #define STRENGTH_MAX 1.0
41 
42 #define THRESHOLD_MIN -30
43 #define THRESHOLD_MAX 30
44 
45 typedef struct FilterParam {
46  float radius;
47  float strength;
48  int threshold;
49  float quality;
51 } FilterParam;
52 
53 typedef struct SmartblurContext {
54  const AVClass *class;
57  int hsub;
58  int vsub;
59  unsigned int sws_flags;
61 
62 #define OFFSET(x) offsetof(SmartblurContext, x)
63 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
64 
65 static const AVOption smartblur_options[] = {
66  { "luma_radius", "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
67  { "lr" , "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
68  { "luma_strength", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
69  { "ls", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
70  { "luma_threshold", "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT, {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS },
71  { "lt", "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT, {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS },
72 
73  { "chroma_radius", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
74  { "cr", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
75  { "chroma_strength", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
76  { "cs", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
77  { "chroma_threshold", "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT, {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS },
78  { "ct", "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT, {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS },
79 
80  { NULL }
81 };
82 
83 AVFILTER_DEFINE_CLASS(smartblur);
84 
86 {
87  SmartblurContext *s = ctx->priv;
88 
89  /* make chroma default to luma values, if not explicitly set */
90  if (s->chroma.radius < RADIUS_MIN)
91  s->chroma.radius = s->luma.radius;
92  if (s->chroma.strength < STRENGTH_MIN)
93  s->chroma.strength = s->luma.strength;
94  if (s->chroma.threshold < THRESHOLD_MIN)
95  s->chroma.threshold = s->luma.threshold;
96 
97  s->luma.quality = s->chroma.quality = 3.0;
98  s->sws_flags = SWS_BICUBIC;
99 
101  "luma_radius:%f luma_strength:%f luma_threshold:%d "
102  "chroma_radius:%f chroma_strength:%f chroma_threshold:%d\n",
103  s->luma.radius, s->luma.strength, s->luma.threshold,
104  s->chroma.radius, s->chroma.strength, s->chroma.threshold);
105 
106  return 0;
107 }
108 
110 {
111  SmartblurContext *s = ctx->priv;
112 
113  sws_freeContext(s->luma.filter_context);
114  sws_freeContext(s->chroma.filter_context);
115 }
116 
117 static const enum AVPixelFormat pix_fmts[] = {
123 };
124 
125 static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
126 {
127  SwsVector *vec;
128  SwsFilter sws_filter;
129 
130  vec = sws_getGaussianVec(f->radius, f->quality);
131 
132  if (!vec)
133  return AVERROR(EINVAL);
134 
135  sws_scaleVec(vec, f->strength);
136  vec->coeff[vec->length / 2] += 1.0 - f->strength;
137  sws_filter.lumH = sws_filter.lumV = vec;
138  sws_filter.chrH = sws_filter.chrV = NULL;
139  f->filter_context = sws_getCachedContext(f->filter_context,
142  flags, &sws_filter, NULL, NULL);
143 
144  sws_freeVec(vec);
145 
146  if (!f->filter_context)
147  return AVERROR(EINVAL);
148 
149  return 0;
150 }
151 
153 {
154  SmartblurContext *s = inlink->dst->priv;
156 
157  s->hsub = desc->log2_chroma_w;
158  s->vsub = desc->log2_chroma_h;
159 
160  alloc_sws_context(&s->luma, inlink->w, inlink->h, s->sws_flags);
161  alloc_sws_context(&s->chroma,
162  AV_CEIL_RSHIFT(inlink->w, s->hsub),
163  AV_CEIL_RSHIFT(inlink->h, s->vsub),
164  s->sws_flags);
165 
166  return 0;
167 }
168 
169 static void blur(uint8_t *dst, const int dst_linesize,
170  const uint8_t *src, const int src_linesize,
171  const int w, const int h, const int threshold,
172  struct SwsContext *filter_context)
173 {
174  int x, y;
175  int orig, filtered;
176  int diff;
177  /* Declare arrays of 4 to get aligned data */
178  const uint8_t* const src_array[4] = {src};
179  uint8_t *dst_array[4] = {dst};
180  int src_linesize_array[4] = {src_linesize};
181  int dst_linesize_array[4] = {dst_linesize};
182 
183  sws_scale(filter_context, src_array, src_linesize_array,
184  0, h, dst_array, dst_linesize_array);
185 
186  if (threshold > 0) {
187  for (y = 0; y < h; ++y) {
188  for (x = 0; x < w; ++x) {
189  orig = src[x + y * src_linesize];
190  filtered = dst[x + y * dst_linesize];
191  diff = orig - filtered;
192 
193  if (diff > 0) {
194  if (diff > 2 * threshold)
195  dst[x + y * dst_linesize] = orig;
196  else if (diff > threshold)
197  /* add 'diff' and subtract 'threshold' from 'filtered' */
198  dst[x + y * dst_linesize] = orig - threshold;
199  } else {
200  if (-diff > 2 * threshold)
201  dst[x + y * dst_linesize] = orig;
202  else if (-diff > threshold)
203  /* add 'diff' and 'threshold' to 'filtered' */
204  dst[x + y * dst_linesize] = orig + threshold;
205  }
206  }
207  }
208  } else if (threshold < 0) {
209  for (y = 0; y < h; ++y) {
210  for (x = 0; x < w; ++x) {
211  orig = src[x + y * src_linesize];
212  filtered = dst[x + y * dst_linesize];
213  diff = orig - filtered;
214 
215  if (diff > 0) {
216  if (diff <= -threshold)
217  dst[x + y * dst_linesize] = orig;
218  else if (diff <= -2 * threshold)
219  /* subtract 'diff' and 'threshold' from 'orig' */
220  dst[x + y * dst_linesize] = filtered - threshold;
221  } else {
222  if (diff >= threshold)
223  dst[x + y * dst_linesize] = orig;
224  else if (diff >= 2 * threshold)
225  /* add 'threshold' and subtract 'diff' from 'orig' */
226  dst[x + y * dst_linesize] = filtered + threshold;
227  }
228  }
229  }
230  }
231 }
232 
234 {
235  SmartblurContext *s = inlink->dst->priv;
236  AVFilterLink *outlink = inlink->dst->outputs[0];
237  AVFrame *outpic;
238  int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub);
239  int ch = AV_CEIL_RSHIFT(inlink->h, s->vsub);
240 
241  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
242  if (!outpic) {
244  return AVERROR(ENOMEM);
245  }
246  av_frame_copy_props(outpic, inpic);
247 
248  blur(outpic->data[0], outpic->linesize[0],
249  inpic->data[0], inpic->linesize[0],
250  inlink->w, inlink->h, s->luma.threshold,
251  s->luma.filter_context);
252 
253  if (inpic->data[2]) {
254  blur(outpic->data[1], outpic->linesize[1],
255  inpic->data[1], inpic->linesize[1],
256  cw, ch, s->chroma.threshold,
257  s->chroma.filter_context);
258  blur(outpic->data[2], outpic->linesize[2],
259  inpic->data[2], inpic->linesize[2],
260  cw, ch, s->chroma.threshold,
261  s->chroma.filter_context);
262  }
263 
265  return ff_filter_frame(outlink, outpic);
266 }
267 
268 static const AVFilterPad smartblur_inputs[] = {
269  {
270  .name = "default",
271  .type = AVMEDIA_TYPE_VIDEO,
272  .filter_frame = filter_frame,
273  .config_props = config_props,
274  },
275 };
276 
278  .name = "smartblur",
279  .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
280  .priv_size = sizeof(SmartblurContext),
281  .init = init,
282  .uninit = uninit,
286  .priv_class = &smartblur_class,
288 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:112
sws_getCachedContext
struct SwsContext * sws_getCachedContext(struct SwsContext *context, int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Check if context can be reused, otherwise reallocate a new one.
Definition: utils.c:2508
ff_vf_smartblur
const AVFilter ff_vf_smartblur
Definition: vf_smartblur.c:277
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
config_props
static int config_props(AVFilterLink *inlink)
Definition: vf_smartblur.c:152
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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 they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(smartblur)
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
RADIUS_MAX
#define RADIUS_MAX
Definition: vf_smartblur.c:37
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:162
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
alloc_sws_context
static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
Definition: vf_smartblur.c:125
smartblur_options
static const AVOption smartblur_options[]
Definition: vf_smartblur.c:65
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
OFFSET
#define OFFSET(x)
Definition: vf_smartblur.c:62
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:346
chroma
static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
Definition: vf_waveform.c:1639
RADIUS_MIN
#define RADIUS_MIN
Definition: vf_smartblur.c:36
FilterParam::threshold
int threshold
Definition: vf_smartblur.c:48
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_smartblur.c:117
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
SmartblurContext::chroma
FilterParam chroma
Definition: vf_smartblur.c:56
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:106
sws_scale
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't need to export the SwsContext.
Definition: swscale.c:1206
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
FilterParam::radius
int radius
Definition: boxblur.h:32
sws_freeVec
void sws_freeVec(SwsVector *a)
Definition: utils.c:2329
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:395
blur
static void blur(uint8_t *dst, const int dst_linesize, const uint8_t *src, const int src_linesize, const int w, const int h, const int threshold, struct SwsContext *filter_context)
Definition: vf_smartblur.c:169
SmartblurContext::hsub
int hsub
Definition: vf_smartblur.c:57
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_smartblur.c:85
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:59
SwsVector::length
int length
number of coefficients in the vector
Definition: swscale.h:118
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SmartblurContext::sws_flags
unsigned int sws_flags
Definition: vf_smartblur.c:59
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
sws_getGaussianVec
SwsVector * sws_getGaussianVec(double variance, double quality)
Return a normalized Gaussian curve used to filter stuff quality = 3 is high quality,...
Definition: utils.c:2156
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:709
inpic
av_frame_free & inpic
Definition: vf_mcdeint.c:285
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_smartblur.c:233
THRESHOLD_MIN
#define THRESHOLD_MIN
Definition: vf_smartblur.c:42
SwsVector::coeff
double * coeff
pointer to the list of coefficients
Definition: swscale.h:117
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
sws_scaleVec
void sws_scaleVec(SwsVector *a, double scalar)
Scale all the coefficients of a by the scalar value.
Definition: utils.c:2222
STRENGTH_MIN
#define STRENGTH_MIN
Definition: vf_smartblur.c:39
SwsFilter::chrV
SwsVector * chrV
Definition: swscale.h:126
smartblur_inputs
static const AVFilterPad smartblur_inputs[]
Definition: vf_smartblur.c:268
f
f
Definition: af_crystalizer.c:121
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_smartblur.c:109
SwsVector
Definition: swscale.h:116
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:165
SwsFilter
Definition: swscale.h:122
FLAGS
#define FLAGS
Definition: vf_smartblur.c:63
height
#define height
SwsFilter::lumV
SwsVector * lumV
Definition: swscale.h:124
FilterParam
Definition: boxblur.h:31
internal.h
SmartblurContext::luma
FilterParam luma
Definition: vf_smartblur.c:55
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:147
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:238
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
AVFilter
Filter definition.
Definition: avfilter.h:166
THRESHOLD_MAX
#define THRESHOLD_MAX
Definition: vf_smartblur.c:43
STRENGTH_MAX
#define STRENGTH_MAX
Definition: vf_smartblur.c:40
FilterParam::quality
float quality
Definition: vf_sab.c:39
FilterParam::strength
float strength
Definition: vf_sab.c:38
sws_freeContext
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2433
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avfilter.h
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
SmartblurContext
Definition: vf_smartblur.c:53
desc
const char * desc
Definition: libsvtav1.c:75
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
FilterParam::filter_context
struct SwsContext * filter_context
Definition: vf_smartblur.c:50
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
SwsFilter::lumH
SwsVector * lumH
Definition: swscale.h:123
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:80
SmartblurContext::vsub
int vsub
Definition: vf_smartblur.c:58
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:419
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:79
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2038
SwsContext
Definition: swscale_internal.h:301
SwsFilter::chrH
SwsVector * chrH
Definition: swscale.h:125
SWS_BICUBIC
#define SWS_BICUBIC
Definition: swscale.h:67
swscale.h