h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_pad.c
1 /*
2 * Copyright (c) 2008 vmrsss
3 * Copyright (c) 2009 Stefano Sabatini
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
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 *
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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * video padding filter
25 */
26
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/common.h"
33 #include "libavutil/eval.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/colorspace.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/opt.h"
40
41 #include "drawutils.h"
42
43 static const char *const var_names[] = {
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
59 enum 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
75 static int query_formats(AVFilterContext *ctx)
76 {
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,
81
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,
88
89 AV_PIX_FMT_NONE
90 };
91
92 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
93 return 0;
94 }
95
96 typedef struct PadContext {
97 const AVClass *class;
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
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;
107
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
114 static av_cold int init(AVFilterContext *ctx)
115 {
116 PadContext *s = ctx->priv;
117
118 if (av_parse_color(s->color, s->color_str, -1, ctx) < 0)
119 return AVERROR(EINVAL);
120
121 return 0;
122 }
123
124 static av_cold void uninit(AVFilterContext *ctx)
125 {
126 PadContext *s = ctx->priv;
127 int i;
128
129 for (i = 0; i < 4; i++) {
130 av_freep(&s->line[i]);
131 s->line_step[i] = 0;
132 }
133 }
134
135 static int config_input(AVFilterLink *inlink)
136 {
137 AVFilterContext *ctx = inlink->dst;
138 PadContext *s = ctx->priv;
139 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
140 uint8_t rgba_color[4];
141 int ret, is_packed_rgba;
142 double var_values[VARS_NB], res;
143 char *expr;
144
145 s->hsub = pix_desc->log2_chroma_w;
146 s->vsub = pix_desc->log2_chroma_h;
147
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;
155 var_values[VAR_A] = (double) inlink->w / inlink->h;
156 var_values[VAR_HSUB] = 1<<s->hsub;
157 var_values[VAR_VSUB] = 1<<s->vsub;
158
159 /* evaluate width and height */
160 av_expr_parse_and_eval(&res, (expr = s->w_expr),
161 var_names, var_values,
162 NULL, NULL, NULL, NULL, NULL, 0, ctx);
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),
165 var_names, var_values,
166 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
167 goto eval_fail;
168 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
169 if (!s->h)
170 var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h;
171
172 /* evaluate the width again, as it may depend on the evaluated output height */
173 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
174 var_names, var_values,
175 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
176 goto eval_fail;
177 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
178 if (!s->w)
179 var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w;
180
181 /* evaluate x and y */
182 av_expr_parse_and_eval(&res, (expr = s->x_expr),
183 var_names, var_values,
184 NULL, NULL, NULL, NULL, NULL, 0, ctx);
185 s->x = var_values[VAR_X] = res;
186 if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
187 var_names, var_values,
188 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
189 goto eval_fail;
190 s->y = var_values[VAR_Y] = res;
191 /* evaluate x again, as it may depend on the evaluated y value */
192 if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
193 var_names, var_values,
194 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
195 goto eval_fail;
196 s->x = var_values[VAR_X] = res;
197
198 /* sanity check params */
199 if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
200 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
201 return AVERROR(EINVAL);
202 }
203
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);
208
209 s->in_w = inlink->w & ~((1 << s->hsub) - 1);
210 s->in_h = inlink->h & ~((1 << s->vsub) - 1);
211
212 memcpy(rgba_color, s->color, sizeof(rgba_color));
213 ff_fill_line_with_color(s->line, s->line_step, s->w, s->color,
214 inlink->format, rgba_color, &is_packed_rgba, NULL);
215
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",
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],
219 is_packed_rgba ? "rgba" : "yuva");
220
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) {
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",
227 s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
228 return AVERROR(EINVAL);
229 }
230
231 return 0;
232
233 eval_fail:
234 av_log(NULL, AV_LOG_ERROR,
235 "Error when evaluating the expression '%s'\n", expr);
236 return ret;
237
238 }
239
240 static int config_output(AVFilterLink *outlink)
241 {
242 PadContext *s = outlink->src->priv;
243
244 outlink->w = s->w;
245 outlink->h = s->h;
246 return 0;
247 }
248
249 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
250 {
251 PadContext *s = inlink->dst->priv;
252
253 AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
254 w + (s->w - s->in_w),
255 h + (s->h - s->in_h));
256 int plane;
257
258 if (!frame)
259 return NULL;
260
261 frame->width = w;
262 frame->height = h;
263
264 for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
265 int hsub = (plane == 1 || plane == 2) ? s->hsub : 0;
266 int vsub = (plane == 1 || plane == 2) ? s->vsub : 0;
267
268 frame->data[plane] += (s->x >> hsub) * s->line_step[plane] +
269 (s->y >> vsub) * frame->linesize[plane];
270 }
271
272 return frame;
273 }
274
275 /* check whether each plane in this buffer can be padded without copying */
276 static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
277 {
278 int planes[4] = { -1, -1, -1, -1}, *p = planes;
279 int i, j;
280
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 }
286
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)
312 for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
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 }
325
326 return 0;
327 }
328
329 static int frame_needs_copy(PadContext *s, AVFrame *frame)
330 {
331 int i;
332
333 if (!av_frame_is_writable(frame))
334 return 1;
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;
339 return 0;
340 }
341
342 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
343 {
344 PadContext *s = inlink->dst->priv;
345 AVFrame *out;
346 int needs_copy = frame_needs_copy(s, in);
347
348 if (needs_copy) {
349 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
350 out = ff_get_video_buffer(inlink->dst->outputs[0],
351 FFMAX(inlink->w, s->w),
352 FFMAX(inlink->h, s->h));
353 if (!out) {
354 av_frame_free(&in);
355 return AVERROR(ENOMEM);
356 }
357
358 av_frame_copy_props(out, in);
359 } else {
360 int i;
361
362 out = in;
363 for (i = 0; i < FF_ARRAY_ELEMS(out->data) && out->data[i]; i++) {
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];
368 }
369 }
370
371 /* top bar */
372 if (s->y) {
373 ff_draw_rectangle(out->data, out->linesize,
374 s->line, s->line_step, s->hsub, s->vsub,
375 0, 0, s->w, s->y);
376 }
377
378 /* bottom bar */
379 if (s->h > s->y + s->in_h) {
380 ff_draw_rectangle(out->data, out->linesize,
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);
383 }
384
385 /* left border */
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);
388
389 if (needs_copy) {
390 ff_copy_rectangle(out->data, out->linesize, in->data, in->linesize,
391 s->line_step, s->hsub, s->vsub,
392 s->x, s->y, 0, in->width, in->height);
393 }
394
395 /* right border */
396 ff_draw_rectangle(out->data, out->linesize,
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,
399 in->height);
400
401 out->width = s->w;
402 out->height = s->h;
403
404 if (in != out)
405 av_frame_free(&in);
406 return ff_filter_frame(inlink->dst->outputs[0], out);
407 }
408
409 #define OFFSET(x) offsetof(PadContext, x)
410 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
411 static 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
422 static 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
429 static 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,
435 .filter_frame = filter_frame,
436 },
437 { NULL }
438 };
439
440 static 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
449 AVFilter ff_vf_pad = {
450 .name = "pad",
451 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
452
453 .priv_size = sizeof(PadContext),
454 .priv_class = &pad_class,
455 .init = init,
456 .uninit = uninit,
457 .query_formats = query_formats,
458
459 .inputs = avfilter_vf_pad_inputs,
460
461 .outputs = avfilter_vf_pad_outputs,
462 };