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/pixdesc.h"
29 #include "libswscale/swscale.h"
35 #define RADIUS_MIN 0.1
36 #define RADIUS_MAX 5.0
38 #define STRENGTH_MIN -1.0
39 #define STRENGTH_MAX 1.0
41 #define THRESHOLD_MIN -30
42 #define THRESHOLD_MAX 30
49 struct SwsContext *filter_context;
57 unsigned int sws_flags;
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",\
66 ret = AVERROR(EINVAL); \
69 static av_cold int init(AVFilterContext *ctx, const char *args)
71 SmartblurContext *sblur = ctx->priv;
73 float lradius, lstrength, cradius, cstrength;
74 int lthreshold, cthreshold;
77 n = sscanf(args, "%f:%f:%d:%f:%f:%d",
78 &lradius, &lstrength, <hreshold,
79 &cradius, &cstrength, &cthreshold);
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);
89 sblur->luma.radius = lradius;
90 sblur->luma.strength = lstrength;
91 sblur->luma.threshold = lthreshold;
94 sblur->chroma.radius = sblur->luma.radius;
95 sblur->chroma.strength = sblur->luma.strength;
96 sblur->chroma.threshold = sblur->luma.threshold;
98 sblur->chroma.radius = cradius;
99 sblur->chroma.strength = cstrength;
100 sblur->chroma.threshold = cthreshold;
103 sblur->luma.quality = sblur->chroma.quality = 3.0;
104 sblur->sws_flags = SWS_BICUBIC;
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)
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)
119 static av_cold void uninit(AVFilterContext *ctx)
121 SmartblurContext *sblur = ctx->priv;
123 sws_freeContext(sblur->luma.filter_context);
124 sws_freeContext(sblur->chroma.filter_context);
127 static int query_formats(AVFilterContext *ctx)
129 static const enum AVPixelFormat pix_fmts[] = {
130 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
131 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
132 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
137 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
142 static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
145 SwsFilter sws_filter;
147 vec = sws_getGaussianVec(f->radius, f->quality);
150 return AVERROR(EINVAL);
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;
156 f->filter_context = sws_getCachedContext(NULL,
157 width, height, AV_PIX_FMT_GRAY8,
158 width, height, AV_PIX_FMT_GRAY8,
159 flags, &sws_filter, NULL, NULL);
163 if (!f->filter_context)
164 return AVERROR(EINVAL);
169 static int config_props(AVFilterLink *inlink)
171 SmartblurContext *sblur = inlink->dst->priv;
172 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
174 sblur->hsub = desc->log2_chroma_w;
175 sblur->vsub = desc->log2_chroma_h;
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,
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)
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};
199 sws_scale(filter_context, src_array, src_linesize_array,
200 0, h, dst_array, dst_linesize_array);
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;
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;
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;
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;
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;
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;
249 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
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;
257 outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
259 avfilter_unref_bufferp(&inpic);
260 return AVERROR(ENOMEM);
262 avfilter_copy_buffer_ref_props(outpic, inpic);
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);
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);
280 avfilter_unref_bufferp(&inpic);
281 return ff_filter_frame(outlink, outpic);
284 static const AVFilterPad smartblur_inputs[] = {
287 .type = AVMEDIA_TYPE_VIDEO,
288 .filter_frame = filter_frame,
289 .config_props = config_props,
290 .min_perms = AV_PERM_READ,
295 static const AVFilterPad smartblur_outputs[] = {
298 .type = AVMEDIA_TYPE_VIDEO,
303 AVFilter avfilter_vf_smartblur = {
305 .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
307 .priv_size = sizeof(SmartblurContext),
311 .query_formats = query_formats,
312 .inputs = smartblur_inputs,
313 .outputs = smartblur_outputs,