h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_pad.c
CommitLineData
3c940173 1/*
3fa77bde
SS
2 * Copyright (c) 2008 vmrsss
3 * Copyright (c) 2009 Stefano Sabatini
3c940173 4 *
2912e87a 5 * This file is part of Libav.
3c940173 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
3c940173
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
3c940173
MN
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
3c940173
MN
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
aeaa9630 23 * @file
753890d0 24 * video padding filter
3c940173
MN
25 */
26
27#include "avfilter.h"
b74a1da4 28#include "formats.h"
9d0bfc50 29#include "internal.h"
803391f7 30#include "video.h"
73a4f7c2 31#include "libavutil/avstring.h"
1d9c2dc8 32#include "libavutil/common.h"
73a4f7c2 33#include "libavutil/eval.h"
3c940173 34#include "libavutil/pixdesc.h"
2b4abbd6 35#include "libavutil/colorspace.h"
737eb597
RT
36#include "libavutil/imgutils.h"
37#include "libavutil/parseutils.h"
0ebcdf5c 38#include "libavutil/mathematics.h"
40c885c5
AK
39#include "libavutil/opt.h"
40
5ad06110 41#include "drawutils.h"
e3331706 42
b0f29db5 43static const char *const var_names[] = {
73a4f7c2
SS
44 "PI",
45 "PHI",
46 "E",
47 "in_w", "iw",
48 "in_h", "ih",
49 "out_w", "ow",
50 "out_h", "oh",
51 "x",
52 "y",
53 "a",
54 "hsub",
55 "vsub",
56 NULL
57};
58
59enum var_name {
60 VAR_PI,
61 VAR_PHI,
62 VAR_E,
63 VAR_IN_W, VAR_IW,
64 VAR_IN_H, VAR_IH,
65 VAR_OUT_W, VAR_OW,
66 VAR_OUT_H, VAR_OH,
67 VAR_X,
68 VAR_Y,
69 VAR_A,
70 VAR_HSUB,
71 VAR_VSUB,
72 VARS_NB
73};
74
1b364fd2
SS
75static int query_formats(AVFilterContext *ctx)
76{
716d413c
AK
77 static const enum AVPixelFormat pix_fmts[] = {
78 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
79 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
80 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
1b364fd2 81
716d413c
AK
82 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
83 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
84 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
85 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
86 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
87 AV_PIX_FMT_YUVA420P,
1b364fd2 88
716d413c 89 AV_PIX_FMT_NONE
1b364fd2
SS
90 };
91
b74a1da4 92 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
1b364fd2
SS
93 return 0;
94}
95
58400ac1 96typedef struct PadContext {
40c885c5 97 const AVClass *class;
3c940173
MN
98 int w, h; ///< output dimensions, a value of 0 will result in the input size
99 int x, y; ///< offsets of the input area with respect to the padded area
100 int in_w, in_h; ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues
101
40c885c5
AK
102 char *w_expr; ///< width expression string
103 char *h_expr; ///< height expression string
104 char *x_expr; ///< width expression string
105 char *y_expr; ///< height expression string
106 char *color_str;
73a4f7c2 107
3c940173
MN
108 uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
109 uint8_t *line[4];
110 int line_step[4];
111 int hsub, vsub; ///< chroma subsampling values
112} PadContext;
113
c43a7eca 114static av_cold int init(AVFilterContext *ctx)
3c940173 115{
3062ac4c 116 PadContext *s = ctx->priv;
73a4f7c2 117
3062ac4c 118 if (av_parse_color(s->color, s->color_str, -1, ctx) < 0)
622e6ce5 119 return AVERROR(EINVAL);
3c940173 120
3c940173
MN
121 return 0;
122}
123
124static av_cold void uninit(AVFilterContext *ctx)
125{
3062ac4c 126 PadContext *s = ctx->priv;
3c940173
MN
127 int i;
128
129 for (i = 0; i < 4; i++) {
3062ac4c
AK
130 av_freep(&s->line[i]);
131 s->line_step[i] = 0;
3c940173
MN
132 }
133}
134
3c940173
MN
135static int config_input(AVFilterLink *inlink)
136{
137 AVFilterContext *ctx = inlink->dst;
3062ac4c 138 PadContext *s = ctx->priv;
59ee9f78 139 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
3c940173 140 uint8_t rgba_color[4];
73a4f7c2
SS
141 int ret, is_packed_rgba;
142 double var_values[VARS_NB], res;
143 char *expr;
3c940173 144
3062ac4c
AK
145 s->hsub = pix_desc->log2_chroma_w;
146 s->vsub = pix_desc->log2_chroma_h;
3c940173 147
73a4f7c2
SS
148 var_values[VAR_PI] = M_PI;
149 var_values[VAR_PHI] = M_PHI;
150 var_values[VAR_E] = M_E;
151 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
152 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
153 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
154 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
ba04177e 155 var_values[VAR_A] = (double) inlink->w / inlink->h;
3062ac4c
AK
156 var_values[VAR_HSUB] = 1<<s->hsub;
157 var_values[VAR_VSUB] = 1<<s->vsub;
73a4f7c2
SS
158
159 /* evaluate width and height */
3735b5c6
AK
160 av_expr_parse_and_eval(&res, (expr = s->w_expr),
161 var_names, var_values,
162 NULL, NULL, NULL, NULL, NULL, 0, ctx);
3062ac4c
AK
163 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
164 if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
73a4f7c2
SS
165 var_names, var_values,
166 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
167 goto eval_fail;
3062ac4c 168 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
2ec112f7
JS
169 if (!s->h)
170 var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h;
171
73a4f7c2 172 /* evaluate the width again, as it may depend on the evaluated output height */
3062ac4c 173 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
73a4f7c2
SS
174 var_names, var_values,
175 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
176 goto eval_fail;
3062ac4c 177 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
2ec112f7
JS
178 if (!s->w)
179 var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w;
73a4f7c2
SS
180
181 /* evaluate x and y */
3735b5c6
AK
182 av_expr_parse_and_eval(&res, (expr = s->x_expr),
183 var_names, var_values,
184 NULL, NULL, NULL, NULL, NULL, 0, ctx);
3062ac4c
AK
185 s->x = var_values[VAR_X] = res;
186 if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
73a4f7c2
SS
187 var_names, var_values,
188 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
189 goto eval_fail;
3062ac4c 190 s->y = var_values[VAR_Y] = res;
73a4f7c2 191 /* evaluate x again, as it may depend on the evaluated y value */
3062ac4c 192 if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
73a4f7c2
SS
193 var_names, var_values,
194 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
195 goto eval_fail;
3062ac4c 196 s->x = var_values[VAR_X] = res;
73a4f7c2
SS
197
198 /* sanity check params */
3062ac4c 199 if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
73a4f7c2
SS
200 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
201 return AVERROR(EINVAL);
202 }
203
3062ac4c
AK
204 s->w &= ~((1 << s->hsub) - 1);
205 s->h &= ~((1 << s->vsub) - 1);
206 s->x &= ~((1 << s->hsub) - 1);
207 s->y &= ~((1 << s->vsub) - 1);
3c940173 208
3062ac4c
AK
209 s->in_w = inlink->w & ~((1 << s->hsub) - 1);
210 s->in_h = inlink->h & ~((1 << s->vsub) - 1);
3c940173 211
3062ac4c
AK
212 memcpy(rgba_color, s->color, sizeof(rgba_color));
213 ff_fill_line_with_color(s->line, s->line_step, s->w, s->color,
5ad06110 214 inlink->format, rgba_color, &is_packed_rgba, NULL);
3c940173 215
1a49a169 216 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
3062ac4c
AK
217 inlink->w, inlink->h, s->w, s->h, s->x, s->y,
218 s->color[0], s->color[1], s->color[2], s->color[3],
039baa78 219 is_packed_rgba ? "rgba" : "yuva");
3c940173 220
3062ac4c
AK
221 if (s->x < 0 || s->y < 0 ||
222 s->w <= 0 || s->h <= 0 ||
223 (unsigned)s->x + (unsigned)inlink->w > s->w ||
224 (unsigned)s->y + (unsigned)inlink->h > s->h) {
3c940173
MN
225 av_log(ctx, AV_LOG_ERROR,
226 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
3062ac4c 227 s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
622e6ce5 228 return AVERROR(EINVAL);
3c940173
MN
229 }
230
231 return 0;
73a4f7c2
SS
232
233eval_fail:
234 av_log(NULL, AV_LOG_ERROR,
235 "Error when evaluating the expression '%s'\n", expr);
236 return ret;
237
3c940173
MN
238}
239
240static int config_output(AVFilterLink *outlink)
241{
3062ac4c 242 PadContext *s = outlink->src->priv;
3c940173 243
3062ac4c
AK
244 outlink->w = s->w;
245 outlink->h = s->h;
3c940173
MN
246 return 0;
247}
248
7e350379 249static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
3c940173 250{
3062ac4c 251 PadContext *s = inlink->dst->priv;
3c940173 252
7e350379 253 AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
3062ac4c
AK
254 w + (s->w - s->in_w),
255 h + (s->h - s->in_h));
f66b3905 256 int plane;
3c940173 257
7e350379 258 if (!frame)
8f3a3ce7
AK
259 return NULL;
260
7e350379
AK
261 frame->width = w;
262 frame->height = h;
c71e9640 263
7e350379 264 for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
3062ac4c
AK
265 int hsub = (plane == 1 || plane == 2) ? s->hsub : 0;
266 int vsub = (plane == 1 || plane == 2) ? s->vsub : 0;
3c940173 267
3062ac4c
AK
268 frame->data[plane] += (s->x >> hsub) * s->line_step[plane] +
269 (s->y >> vsub) * frame->linesize[plane];
3c940173
MN
270 }
271
7e350379 272 return frame;
3c940173
MN
273}
274
7e350379
AK
275/* check whether each plane in this buffer can be padded without copying */
276static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
e3331706 277{
7e350379
AK
278 int planes[4] = { -1, -1, -1, -1}, *p = planes;
279 int i, j;
e3331706 280
7e350379
AK
281 /* get all planes in this buffer */
282 for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
283 if (av_frame_get_plane_buffer(frame, i) == buf)
284 *p++ = i;
285 }
e3331706 286
7e350379
AK
287 /* for each plane in this buffer, check that it can be padded without
288 * going over buffer bounds or other planes */
289 for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
290 int hsub = (planes[i] == 1 || planes[i] == 2) ? s->hsub : 0;
291 int vsub = (planes[i] == 1 || planes[i] == 2) ? s->vsub : 0;
292
293 uint8_t *start = frame->data[planes[i]];
294 uint8_t *end = start + (frame->height >> hsub) *
295 frame->linesize[planes[i]];
296
297 /* amount of free space needed before the start and after the end
298 * of the plane */
299 ptrdiff_t req_start = (s->x >> hsub) * s->line_step[planes[i]] +
300 (s->y >> vsub) * frame->linesize[planes[i]];
301 ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
302 s->line_step[planes[i]] +
303 (s->y >> vsub) * frame->linesize[planes[i]];
304
305 if (frame->linesize[planes[i]] < (s->w >> hsub) * s->line_step[planes[i]])
306 return 1;
307 if (start - buf->data < req_start ||
308 (buf->data + buf->size) - end < req_end)
309 return 1;
310
311#define SIGN(x) ((x) > 0 ? 1 : -1)
5d639b2b 312 for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
7e350379
AK
313 int hsub1 = (planes[j] == 1 || planes[j] == 2) ? s->hsub : 0;
314 uint8_t *start1 = frame->data[planes[j]];
315 uint8_t *end1 = start1 + (frame->height >> hsub1) *
316 frame->linesize[planes[j]];
317 if (i == j)
318 continue;
319
320 if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
321 SIGN(end - start1) != SIGN(end - start1 + req_end))
322 return 1;
323 }
324 }
e3331706 325
7e350379
AK
326 return 0;
327}
e3331706 328
7e350379
AK
329static int frame_needs_copy(PadContext *s, AVFrame *frame)
330{
331 int i;
e3331706 332
7e350379 333 if (!av_frame_is_writable(frame))
e3331706 334 return 1;
7e350379
AK
335
336 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
337 if (buffer_needs_copy(s, frame, frame->buf[i]))
338 return 1;
e3331706
MN
339 return 0;
340}
341
7e350379 342static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3c940173 343{
3062ac4c 344 PadContext *s = inlink->dst->priv;
7e350379 345 AVFrame *out;
3062ac4c 346 int needs_copy = frame_needs_copy(s, in);
f66b3905 347
565e4993 348 if (needs_copy) {
e3331706 349 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
7e350379 350 out = ff_get_video_buffer(inlink->dst->outputs[0],
3062ac4c
AK
351 FFMAX(inlink->w, s->w),
352 FFMAX(inlink->h, s->h));
565e4993 353 if (!out) {
7e350379 354 av_frame_free(&in);
ebc8d974 355 return AVERROR(ENOMEM);
565e4993 356 }
ebc8d974 357
7e350379
AK
358 av_frame_copy_props(out, in);
359 } else {
360 int i;
ebc8d974 361
7e350379
AK
362 out = in;
363 for (i = 0; i < FF_ARRAY_ELEMS(out->data) && out->data[i]; i++) {
3062ac4c
AK
364 int hsub = (i == 1 || i == 2) ? s->hsub : 0;
365 int vsub = (i == 1 || i == 2) ? s->vsub : 0;
366 out->data[i] -= (s->x >> hsub) * s->line_step[i] +
367 (s->y >> vsub) * out->linesize[i];
7e350379
AK
368 }
369 }
3c940173 370
565e4993 371 /* top bar */
3062ac4c 372 if (s->y) {
565e4993 373 ff_draw_rectangle(out->data, out->linesize,
3062ac4c
AK
374 s->line, s->line_step, s->hsub, s->vsub,
375 0, 0, s->w, s->y);
3c940173
MN
376 }
377
565e4993 378 /* bottom bar */
3062ac4c 379 if (s->h > s->y + s->in_h) {
565e4993 380 ff_draw_rectangle(out->data, out->linesize,
3062ac4c
AK
381 s->line, s->line_step, s->hsub, s->vsub,
382 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
3c940173 383 }
3c940173
MN
384
385 /* left border */
3062ac4c
AK
386 ff_draw_rectangle(out->data, out->linesize, s->line, s->line_step,
387 s->hsub, s->vsub, 0, s->y, s->x, in->height);
565e4993
AK
388
389 if (needs_copy) {
390 ff_copy_rectangle(out->data, out->linesize, in->data, in->linesize,
3062ac4c
AK
391 s->line_step, s->hsub, s->vsub,
392 s->x, s->y, 0, in->width, in->height);
e3331706
MN
393 }
394
3c940173 395 /* right border */
565e4993 396 ff_draw_rectangle(out->data, out->linesize,
3062ac4c
AK
397 s->line, s->line_step, s->hsub, s->vsub,
398 s->x + s->in_w, s->y, s->w - s->x - s->in_w,
7e350379
AK
399 in->height);
400
3062ac4c
AK
401 out->width = s->w;
402 out->height = s->h;
3c940173 403
7e350379
AK
404 if (in != out)
405 av_frame_free(&in);
565e4993 406 return ff_filter_frame(inlink->dst->outputs[0], out);
3c940173
MN
407}
408
40c885c5
AK
409#define OFFSET(x) offsetof(PadContext, x)
410#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
411static const AVOption options[] = {
412 { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
413 { "height", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
414 { "x", "Horizontal position of the left edge of the input video in the "
415 "output video", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
416 { "y", "Vertical position of the top edge of the input video in the "
417 "output video", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
418 { "color", "Color of the padded area", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, .flags = FLAGS },
419 { NULL },
420};
421
422static const AVClass pad_class = {
423 .class_name = "pad",
424 .item_name = av_default_item_name,
425 .option = options,
426 .version = LIBAVUTIL_VERSION_INT,
427};
428
568c70e7
MR
429static const AVFilterPad avfilter_vf_pad_inputs[] = {
430 {
431 .name = "default",
432 .type = AVMEDIA_TYPE_VIDEO,
433 .config_props = config_input,
434 .get_video_buffer = get_video_buffer,
565e4993 435 .filter_frame = filter_frame,
568c70e7
MR
436 },
437 { NULL }
438};
439
440static const AVFilterPad avfilter_vf_pad_outputs[] = {
441 {
442 .name = "default",
443 .type = AVMEDIA_TYPE_VIDEO,
444 .config_props = config_output,
445 },
446 { NULL }
447};
448
cd43ca04 449AVFilter ff_vf_pad = {
3c940173 450 .name = "pad",
478b9d74 451 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
3c940173
MN
452
453 .priv_size = sizeof(PadContext),
40c885c5 454 .priv_class = &pad_class,
3c940173
MN
455 .init = init,
456 .uninit = uninit,
457 .query_formats = query_formats,
458
568c70e7
MR
459 .inputs = avfilter_vf_pad_inputs,
460
461 .outputs = avfilter_vf_pad_outputs,
3c940173 462};