2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2012 Jeremy Tran
5 * This file is part of FFmpeg.
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.
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.
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.
24 * Apply a smartblur filter to the input video
25 * Ported from MPlayer libmpcodecs/vf_smartblur.c by Michael Niedermayer.
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libswscale/swscale.h"
36 #define RADIUS_MIN 0.1
37 #define RADIUS_MAX 5.0
39 #define STRENGTH_MIN -1.0
40 #define STRENGTH_MAX 1.0
42 #define THRESHOLD_MIN -30
43 #define THRESHOLD_MAX 30
50 struct SwsContext *filter_context;
59 unsigned int sws_flags;
62 #define OFFSET(x) offsetof(SmartblurContext, x)
63 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 },
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 },
83 AVFILTER_DEFINE_CLASS(smartblur);
85 static av_cold int init(AVFilterContext *ctx, const char *args)
87 SmartblurContext *sblur = ctx->priv;
89 /* make chroma default to luma values, if not explicitly set */
90 if (sblur->chroma.radius < RADIUS_MIN)
91 sblur->chroma.radius = sblur->luma.radius;
92 if (sblur->chroma.strength < STRENGTH_MIN)
93 sblur->chroma.strength = sblur->luma.strength;
94 if (sblur->chroma.threshold < THRESHOLD_MIN)
95 sblur->chroma.threshold = sblur->luma.threshold;
97 sblur->luma.quality = sblur->chroma.quality = 3.0;
98 sblur->sws_flags = SWS_BICUBIC;
100 av_log(ctx, AV_LOG_VERBOSE,
101 "luma_radius:%f luma_strength:%f luma_threshold:%d "
102 "chroma_radius:%f chroma_strength:%f chroma_threshold:%d ",
103 sblur->luma.radius, sblur->luma.strength, sblur->luma.threshold,
104 sblur->chroma.radius, sblur->chroma.strength, sblur->chroma.threshold);
109 static av_cold void uninit(AVFilterContext *ctx)
111 SmartblurContext *sblur = ctx->priv;
113 sws_freeContext(sblur->luma.filter_context);
114 sws_freeContext(sblur->chroma.filter_context);
117 static int query_formats(AVFilterContext *ctx)
119 static const enum AVPixelFormat pix_fmts[] = {
120 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
121 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
122 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
127 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
132 static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
135 SwsFilter sws_filter;
137 vec = sws_getGaussianVec(f->radius, f->quality);
140 return AVERROR(EINVAL);
142 sws_scaleVec(vec, f->strength);
143 vec->coeff[vec->length / 2] += 1.0 - f->strength;
144 sws_filter.lumH = sws_filter.lumV = vec;
145 sws_filter.chrH = sws_filter.chrV = NULL;
146 f->filter_context = sws_getCachedContext(NULL,
147 width, height, AV_PIX_FMT_GRAY8,
148 width, height, AV_PIX_FMT_GRAY8,
149 flags, &sws_filter, NULL, NULL);
153 if (!f->filter_context)
154 return AVERROR(EINVAL);
159 static int config_props(AVFilterLink *inlink)
161 SmartblurContext *sblur = inlink->dst->priv;
162 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
164 sblur->hsub = desc->log2_chroma_w;
165 sblur->vsub = desc->log2_chroma_h;
167 alloc_sws_context(&sblur->luma, inlink->w, inlink->h, sblur->sws_flags);
168 alloc_sws_context(&sblur->chroma,
169 inlink->w >> sblur->hsub, inlink->h >> sblur->vsub,
175 static void blur(uint8_t *dst, const int dst_linesize,
176 const uint8_t *src, const int src_linesize,
177 const int w, const int h, const int threshold,
178 struct SwsContext *filter_context)
183 /* Declare arrays of 4 to get aligned data */
184 const uint8_t* const src_array[4] = {src};
185 uint8_t *dst_array[4] = {dst};
186 int src_linesize_array[4] = {src_linesize};
187 int dst_linesize_array[4] = {dst_linesize};
189 sws_scale(filter_context, src_array, src_linesize_array,
190 0, h, dst_array, dst_linesize_array);
193 for (y = 0; y < h; ++y) {
194 for (x = 0; x < w; ++x) {
195 orig = src[x + y * src_linesize];
196 filtered = dst[x + y * dst_linesize];
197 diff = orig - filtered;
200 if (diff > 2 * threshold)
201 dst[x + y * dst_linesize] = orig;
202 else if (diff > threshold)
203 /* add 'diff' and substract 'threshold' from 'filtered' */
204 dst[x + y * dst_linesize] = orig - threshold;
206 if (-diff > 2 * threshold)
207 dst[x + y * dst_linesize] = orig;
208 else if (-diff > threshold)
209 /* add 'diff' and 'threshold' to 'filtered' */
210 dst[x + y * dst_linesize] = orig + threshold;
214 } else if (threshold < 0) {
215 for (y = 0; y < h; ++y) {
216 for (x = 0; x < w; ++x) {
217 orig = src[x + y * src_linesize];
218 filtered = dst[x + y * dst_linesize];
219 diff = orig - filtered;
222 if (diff <= -threshold)
223 dst[x + y * dst_linesize] = orig;
224 else if (diff <= -2 * threshold)
225 /* substract 'diff' and 'threshold' from 'orig' */
226 dst[x + y * dst_linesize] = filtered - threshold;
228 if (diff >= threshold)
229 dst[x + y * dst_linesize] = orig;
230 else if (diff >= 2 * threshold)
231 /* add 'threshold' and substract 'diff' from 'orig' */
232 dst[x + y * dst_linesize] = filtered + threshold;
239 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
241 SmartblurContext *sblur = inlink->dst->priv;
242 AVFilterLink *outlink = inlink->dst->outputs[0];
244 int cw = inlink->w >> sblur->hsub;
245 int ch = inlink->h >> sblur->vsub;
247 outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
249 av_frame_free(&inpic);
250 return AVERROR(ENOMEM);
252 av_frame_copy_props(outpic, inpic);
254 blur(outpic->data[0], outpic->linesize[0],
255 inpic->data[0], inpic->linesize[0],
256 inlink->w, inlink->h, sblur->luma.threshold,
257 sblur->luma.filter_context);
259 if (inpic->data[2]) {
260 blur(outpic->data[1], outpic->linesize[1],
261 inpic->data[1], inpic->linesize[1],
262 cw, ch, sblur->chroma.threshold,
263 sblur->chroma.filter_context);
264 blur(outpic->data[2], outpic->linesize[2],
265 inpic->data[2], inpic->linesize[2],
266 cw, ch, sblur->chroma.threshold,
267 sblur->chroma.filter_context);
270 av_frame_free(&inpic);
271 return ff_filter_frame(outlink, outpic);
274 static const AVFilterPad smartblur_inputs[] = {
277 .type = AVMEDIA_TYPE_VIDEO,
278 .filter_frame = filter_frame,
279 .config_props = config_props,
284 static const AVFilterPad smartblur_outputs[] = {
287 .type = AVMEDIA_TYPE_VIDEO,
292 static const char *const shorthand[] = {
293 "luma_radius", "luma_strength", "luma_threshold",
294 "chroma_radius", "chroma_strength", "chroma_threshold",
298 AVFilter avfilter_vf_smartblur = {
300 .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
302 .priv_size = sizeof(SmartblurContext),
306 .query_formats = query_formats,
307 .inputs = smartblur_inputs,
308 .outputs = smartblur_outputs,
309 .priv_class = &smartblur_class,
310 .shorthand = shorthand,