]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_smartblur.c
lavfi/scale: accept named options, make parsing more robust
[ffmpeg] / libavfilter / vf_smartblur.c
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;
49     struct SwsContext *filter_context;
50 } FilterParam;
51
52 typedef struct {
53     FilterParam  luma;
54     FilterParam  chroma;
55     int          hsub;
56     int          vsub;
57     unsigned int sws_flags;
58 } SmartblurContext;
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
123     sws_freeContext(sblur->luma.filter_context);
124     sws_freeContext(sblur->chroma.filter_context);
125 }
126
127 static int query_formats(AVFilterContext *ctx)
128 {
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,
133         AV_PIX_FMT_GRAY8,
134         AV_PIX_FMT_NONE
135     };
136
137     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
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;
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);
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 end_frame(AVFilterLink *inlink)
250 {
251     SmartblurContext  *sblur  = inlink->dst->priv;
252     AVFilterBufferRef *inpic  = inlink->cur_buf;
253     AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
254     int cw = inlink->w >> sblur->hsub;
255     int ch = inlink->h >> sblur->vsub;
256
257     blur(outpic->data[0], outpic->linesize[0],
258          inpic->data[0],  inpic->linesize[0],
259          inlink->w, inlink->h, sblur->luma.threshold,
260          sblur->luma.filter_context);
261
262     if (inpic->data[2]) {
263         blur(outpic->data[1], outpic->linesize[1],
264              inpic->data[1],  inpic->linesize[1],
265              cw, ch, sblur->chroma.threshold,
266              sblur->chroma.filter_context);
267         blur(outpic->data[2], outpic->linesize[2],
268              inpic->data[2],  inpic->linesize[2],
269              cw, ch, sblur->chroma.threshold,
270              sblur->chroma.filter_context);
271     }
272
273     return ff_end_frame(inlink->dst->outputs[0]);
274 }
275
276 AVFilter avfilter_vf_smartblur = {
277     .name        = "smartblur",
278     .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
279
280     .priv_size = sizeof(SmartblurContext),
281
282     .init          = init,
283     .uninit        = uninit,
284     .query_formats = query_formats,
285
286     .inputs = (const AVFilterPad[]) {
287         {
288             .name         = "default",
289             .type         = AVMEDIA_TYPE_VIDEO,
290             .end_frame    = end_frame,
291             .config_props = config_props,
292             .min_perms    = AV_PERM_READ,
293         },
294         { .name = NULL }
295     },
296     .outputs = (const AVFilterPad[]) {
297         {
298             .name         = "default",
299             .type         = AVMEDIA_TYPE_VIDEO,
300         },
301         { .name = NULL }
302     }
303 };