h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_boxblur.c
CommitLineData
ce6b6ef6
SS
1/*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2011 Stefano Sabatini
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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 boxblur filter to the input video.
25 * Ported from MPlayer libmpcodecs/vf_boxblur.c.
26 */
27
28#include "libavutil/avstring.h"
1d9c2dc8 29#include "libavutil/common.h"
ce6b6ef6 30#include "libavutil/eval.h"
51def31d 31#include "libavutil/opt.h"
ce6b6ef6
SS
32#include "libavutil/pixdesc.h"
33#include "avfilter.h"
b74a1da4 34#include "formats.h"
9d0bfc50 35#include "internal.h"
803391f7 36#include "video.h"
ce6b6ef6 37
b0f29db5 38static const char *const var_names[] = {
ce6b6ef6
SS
39 "w",
40 "h",
41 "cw",
42 "ch",
43 "hsub",
44 "vsub",
45 NULL
46};
47
48enum var_name {
49 VAR_W,
50 VAR_H,
51 VAR_CW,
52 VAR_CH,
53 VAR_HSUB,
54 VAR_VSUB,
55 VARS_NB
56};
57
58400ac1 58typedef struct FilterParam {
ce6b6ef6
SS
59 int radius;
60 int power;
61} FilterParam;
62
58400ac1 63typedef struct BoxBlurContext {
51def31d 64 const AVClass *class;
ce6b6ef6
SS
65 FilterParam luma_param;
66 FilterParam chroma_param;
67 FilterParam alpha_param;
51def31d
AK
68 char *luma_radius_expr;
69 char *chroma_radius_expr;
70 char *alpha_radius_expr;
ce6b6ef6
SS
71
72 int hsub, vsub;
73 int radius[4];
74 int power[4];
75 uint8_t *temp[2]; ///< temporary buffer used in blur_power()
76} BoxBlurContext;
77
78#define Y 0
79#define U 1
80#define V 2
81#define A 3
82
c43a7eca 83static av_cold int init(AVFilterContext *ctx)
ce6b6ef6 84{
762df56e 85 BoxBlurContext *s = ctx->priv;
ce6b6ef6 86
762df56e 87 if (!s->luma_radius_expr) {
51def31d 88 av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
ce6b6ef6
SS
89 return AVERROR(EINVAL);
90 }
91
762df56e
AK
92 if (!s->chroma_radius_expr) {
93 s->chroma_radius_expr = av_strdup(s->luma_radius_expr);
94 if (!s->chroma_radius_expr)
51def31d 95 return AVERROR(ENOMEM);
762df56e 96 s->chroma_param.power = s->luma_param.power;
ce6b6ef6 97 }
762df56e
AK
98 if (!s->alpha_radius_expr) {
99 s->alpha_radius_expr = av_strdup(s->luma_radius_expr);
100 if (!s->alpha_radius_expr)
51def31d 101 return AVERROR(ENOMEM);
762df56e 102 s->alpha_param.power = s->luma_param.power;
ce6b6ef6
SS
103 }
104
105 return 0;
106}
107
108static av_cold void uninit(AVFilterContext *ctx)
109{
762df56e 110 BoxBlurContext *s = ctx->priv;
ce6b6ef6 111
762df56e
AK
112 av_freep(&s->temp[0]);
113 av_freep(&s->temp[1]);
ce6b6ef6
SS
114}
115
116static int query_formats(AVFilterContext *ctx)
117{
716d413c
AK
118 enum AVPixelFormat pix_fmts[] = {
119 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
120 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
121 AV_PIX_FMT_YUV440P, AV_PIX_FMT_GRAY8,
122 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
123 AV_PIX_FMT_YUVJ440P,
124 AV_PIX_FMT_NONE
ce6b6ef6
SS
125 };
126
b74a1da4 127 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
ce6b6ef6
SS
128 return 0;
129}
130
131static int config_input(AVFilterLink *inlink)
132{
59ee9f78 133 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
ce6b6ef6 134 AVFilterContext *ctx = inlink->dst;
762df56e 135 BoxBlurContext *s = ctx->priv;
ce6b6ef6
SS
136 int w = inlink->w, h = inlink->h;
137 int cw, ch;
138 double var_values[VARS_NB], res;
139 char *expr;
140 int ret;
141
762df56e
AK
142 av_freep(&s->temp[0]);
143 av_freep(&s->temp[1]);
144 if (!(s->temp[0] = av_malloc(FFMAX(w, h))))
ce6b6ef6 145 return AVERROR(ENOMEM);
762df56e
AK
146 if (!(s->temp[1] = av_malloc(FFMAX(w, h)))) {
147 av_freep(&s->temp[0]);
ce6b6ef6
SS
148 return AVERROR(ENOMEM);
149 }
150
762df56e
AK
151 s->hsub = desc->log2_chroma_w;
152 s->vsub = desc->log2_chroma_h;
ce6b6ef6
SS
153
154 var_values[VAR_W] = inlink->w;
155 var_values[VAR_H] = inlink->h;
762df56e
AK
156 var_values[VAR_CW] = cw = w>>s->hsub;
157 var_values[VAR_CH] = ch = h>>s->vsub;
158 var_values[VAR_HSUB] = 1<<s->hsub;
159 var_values[VAR_VSUB] = 1<<s->vsub;
ce6b6ef6
SS
160
161#define EVAL_RADIUS_EXPR(comp) \
762df56e 162 expr = s->comp##_radius_expr; \
ce6b6ef6
SS
163 ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
164 NULL, NULL, NULL, NULL, NULL, 0, ctx); \
762df56e 165 s->comp##_param.radius = res; \
ce6b6ef6
SS
166 if (ret < 0) { \
167 av_log(NULL, AV_LOG_ERROR, \
168 "Error when evaluating " #comp " radius expression '%s'\n", expr); \
169 return ret; \
170 }
171 EVAL_RADIUS_EXPR(luma);
172 EVAL_RADIUS_EXPR(chroma);
173 EVAL_RADIUS_EXPR(alpha);
174
175 av_log(ctx, AV_LOG_DEBUG,
176 "luma_radius:%d luma_power:%d "
177 "chroma_radius:%d chroma_power:%d "
178 "alpha_radius:%d alpha_power:%d "
179 "w:%d chroma_w:%d h:%d chroma_h:%d\n",
762df56e
AK
180 s->luma_param .radius, s->luma_param .power,
181 s->chroma_param.radius, s->chroma_param.power,
182 s->alpha_param .radius, s->alpha_param .power,
ce6b6ef6
SS
183 w, cw, h, ch);
184
185#define CHECK_RADIUS_VAL(w_, h_, comp) \
762df56e
AK
186 if (s->comp##_param.radius < 0 || \
187 2*s->comp##_param.radius > FFMIN(w_, h_)) { \
ce6b6ef6
SS
188 av_log(ctx, AV_LOG_ERROR, \
189 "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
762df56e 190 s->comp##_param.radius, FFMIN(w_, h_)/2); \
ce6b6ef6
SS
191 return AVERROR(EINVAL); \
192 }
193 CHECK_RADIUS_VAL(w, h, luma);
194 CHECK_RADIUS_VAL(cw, ch, chroma);
195 CHECK_RADIUS_VAL(w, h, alpha);
196
762df56e
AK
197 s->radius[Y] = s->luma_param.radius;
198 s->radius[U] = s->radius[V] = s->chroma_param.radius;
199 s->radius[A] = s->alpha_param.radius;
ce6b6ef6 200
762df56e
AK
201 s->power[Y] = s->luma_param.power;
202 s->power[U] = s->power[V] = s->chroma_param.power;
203 s->power[A] = s->alpha_param.power;
ce6b6ef6
SS
204
205 return 0;
206}
207
208static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
209 int len, int radius)
210{
211 /* Naive boxblur would sum source pixels from x-radius .. x+radius
212 * for destination pixel x. That would be O(radius*width).
213 * If you now look at what source pixels represent 2 consecutive
214 * output pixels, then you see they are almost identical and only
215 * differ by 2 pixels, like:
216 * src0 111111111
217 * dst0 1
218 * src1 111111111
219 * dst1 1
220 * src0-src1 1 -1
221 * so when you know one output pixel you can find the next by just adding
222 * and subtracting 1 input pixel.
223 * The following code adopts this faster variant.
224 */
225 const int length = radius*2 + 1;
226 const int inv = ((1<<16) + length/2)/length;
227 int x, sum = 0;
228
229 for (x = 0; x < radius; x++)
230 sum += src[x*src_step]<<1;
231 sum += src[radius*src_step];
232
233 for (x = 0; x <= radius; x++) {
234 sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
235 dst[x*dst_step] = (sum*inv + (1<<15))>>16;
236 }
237
238 for (; x < len-radius; x++) {
239 sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
240 dst[x*dst_step] = (sum*inv + (1<<15))>>16;
241 }
242
243 for (; x < len; x++) {
244 sum += src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
245 dst[x*dst_step] = (sum*inv + (1<<15))>>16;
246 }
247}
248
249static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
250 int len, int radius, int power, uint8_t *temp[2])
251{
252 uint8_t *a = temp[0], *b = temp[1];
253
254 if (radius && power) {
255 blur(a, 1, src, src_step, len, radius);
256 for (; power > 2; power--) {
257 uint8_t *c;
258 blur(b, 1, a, 1, len, radius);
259 c = a; a = b; b = c;
260 }
261 if (power > 1) {
262 blur(dst, dst_step, a, 1, len, radius);
263 } else {
264 int i;
265 for (i = 0; i < len; i++)
266 dst[i*dst_step] = a[i];
267 }
268 } else {
269 int i;
270 for (i = 0; i < len; i++)
271 dst[i*dst_step] = src[i*src_step];
272 }
273}
274
275static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
276 int w, int h, int radius, int power, uint8_t *temp[2])
277{
278 int y;
279
280 if (radius == 0 && dst == src)
281 return;
282
283 for (y = 0; y < h; y++)
284 blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
285 w, radius, power, temp);
286}
287
288static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
289 int w, int h, int radius, int power, uint8_t *temp[2])
290{
291 int x;
292
293 if (radius == 0 && dst == src)
294 return;
295
296 for (x = 0; x < w; x++)
297 blur_power(dst + x, dst_linesize, src + x, src_linesize,
298 h, radius, power, temp);
299}
300
7e350379 301static int filter_frame(AVFilterLink *inlink, AVFrame *in)
ce6b6ef6
SS
302{
303 AVFilterContext *ctx = inlink->dst;
762df56e 304 BoxBlurContext *s = ctx->priv;
ce6b6ef6 305 AVFilterLink *outlink = inlink->dst->outputs[0];
7e350379 306 AVFrame *out;
ce6b6ef6 307 int plane;
762df56e 308 int cw = inlink->w >> s->hsub, ch = in->height >> s->vsub;
ce6b6ef6 309 int w[4] = { inlink->w, cw, cw, inlink->w };
7e350379 310 int h[4] = { in->height, ch, ch, in->height };
ce6b6ef6 311
7e350379 312 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
565e4993 313 if (!out) {
7e350379 314 av_frame_free(&in);
565e4993
AK
315 return AVERROR(ENOMEM);
316 }
7e350379 317 av_frame_copy_props(out, in);
565e4993
AK
318
319 for (plane = 0; in->data[plane] && plane < 4; plane++)
320 hblur(out->data[plane], out->linesize[plane],
321 in ->data[plane], in ->linesize[plane],
762df56e
AK
322 w[plane], h[plane], s->radius[plane], s->power[plane],
323 s->temp);
ce6b6ef6 324
565e4993
AK
325 for (plane = 0; in->data[plane] && plane < 4; plane++)
326 vblur(out->data[plane], out->linesize[plane],
327 out->data[plane], out->linesize[plane],
762df56e
AK
328 w[plane], h[plane], s->radius[plane], s->power[plane],
329 s->temp);
ce6b6ef6 330
7e350379 331 av_frame_free(&in);
565e4993
AK
332
333 return ff_filter_frame(outlink, out);
ce6b6ef6
SS
334}
335
51def31d
AK
336#define OFFSET(x) offsetof(BoxBlurContext, x)
337#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
338static const AVOption options[] = {
339 { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_radius_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
340 { "luma_power", "How many times should the boxblur be applied to luma",
341 OFFSET(luma_param.power), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
342 { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_radius_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
343 { "chroma_power", "How many times should the boxblur be applied to chroma",
344 OFFSET(chroma_param.power), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
345 { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_radius_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
346 { "alpha_power", "How many times should the boxblur be applied to alpha",
347 OFFSET(alpha_param.power), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
348 { NULL },
349};
350
351static const AVClass boxblur_class = {
352 .class_name = "boxblur",
353 .item_name = av_default_item_name,
354 .option = options,
355 .version = LIBAVUTIL_VERSION_INT,
356};
357
568c70e7
MR
358static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
359 {
360 .name = "default",
361 .type = AVMEDIA_TYPE_VIDEO,
362 .config_props = config_input,
565e4993 363 .filter_frame = filter_frame,
568c70e7
MR
364 },
365 { NULL }
366};
367
368static const AVFilterPad avfilter_vf_boxblur_outputs[] = {
369 {
370 .name = "default",
371 .type = AVMEDIA_TYPE_VIDEO,
372 },
373 { NULL }
374};
375
cd43ca04 376AVFilter ff_vf_boxblur = {
ce6b6ef6
SS
377 .name = "boxblur",
378 .description = NULL_IF_CONFIG_SMALL("Blur the input."),
379 .priv_size = sizeof(BoxBlurContext),
51def31d 380 .priv_class = &boxblur_class,
ce6b6ef6
SS
381 .init = init,
382 .uninit = uninit,
383 .query_formats = query_formats,
384
568c70e7
MR
385 .inputs = avfilter_vf_boxblur_inputs,
386 .outputs = avfilter_vf_boxblur_outputs,
ce6b6ef6 387};