2 * Copyright (c) 2008 vmrsss
3 * Copyright (c) 2009 Stefano Sabatini
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * video padding filter
27 #include <float.h> /* DBL_MAX */
33 #include "libavutil/avstring.h"
34 #include "libavutil/common.h"
35 #include "libavutil/eval.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/parseutils.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/opt.h"
43 #include "drawutils.h"
45 static const char *const var_names[] = {
75 static int query_formats(AVFilterContext *ctx)
77 return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
86 typedef struct PadContext {
88 int w, h; ///< output dimensions, a value of 0 will result in the input size
89 int x, y; ///< offsets of the input area with respect to the padded area
90 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
91 int inlink_w, inlink_h;
94 char *w_expr; ///< width expression string
95 char *h_expr; ///< height expression string
96 char *x_expr; ///< width expression string
97 char *y_expr; ///< height expression string
98 uint8_t rgba_color[4]; ///< color for the padding area
102 int eval_mode; ///< expression evaluation mode
105 static int config_input(AVFilterLink *inlink)
107 AVFilterContext *ctx = inlink->dst;
108 PadContext *s = ctx->priv;
109 AVRational adjusted_aspect = s->aspect;
111 double var_values[VARS_NB], res;
114 ff_draw_init(&s->draw, inlink->format, 0);
115 ff_draw_color(&s->draw, &s->color, s->rgba_color);
117 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
118 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
119 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
120 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
121 var_values[VAR_A] = (double) inlink->w / inlink->h;
122 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
123 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
124 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
125 var_values[VAR_HSUB] = 1 << s->draw.hsub_max;
126 var_values[VAR_VSUB] = 1 << s->draw.vsub_max;
128 /* evaluate width and height */
129 av_expr_parse_and_eval(&res, (expr = s->w_expr),
130 var_names, var_values,
131 NULL, NULL, NULL, NULL, NULL, 0, ctx);
132 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
133 if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
134 var_names, var_values,
135 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
137 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
139 var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h;
141 /* evaluate the width again, as it may depend on the evaluated output height */
142 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
143 var_names, var_values,
144 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
146 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
148 var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w;
150 if (adjusted_aspect.num && adjusted_aspect.den) {
151 adjusted_aspect = av_div_q(adjusted_aspect, inlink->sample_aspect_ratio);
152 if (s->h < av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num)) {
153 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num);
155 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = av_rescale(s->h, adjusted_aspect.num, adjusted_aspect.den);
159 /* evaluate x and y */
160 av_expr_parse_and_eval(&res, (expr = s->x_expr),
161 var_names, var_values,
162 NULL, NULL, NULL, NULL, NULL, 0, ctx);
163 s->x = var_values[VAR_X] = res;
164 if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
165 var_names, var_values,
166 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
168 s->y = var_values[VAR_Y] = res;
169 /* evaluate x again, as it may depend on the evaluated y value */
170 if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
171 var_names, var_values,
172 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
174 s->x = var_values[VAR_X] = res;
176 /* sanity check params */
177 if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
178 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
179 return AVERROR(EINVAL);
182 s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
183 s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
184 s->x = ff_draw_round_to_sub(&s->draw, 0, -1, s->x);
185 s->y = ff_draw_round_to_sub(&s->draw, 1, -1, s->y);
186 s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w);
187 s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h);
188 s->inlink_w = inlink->w;
189 s->inlink_h = inlink->h;
191 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n",
192 inlink->w, inlink->h, s->w, s->h, s->x, s->y,
193 s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]);
195 if (s->x < 0 || s->y < 0 ||
196 s->w <= 0 || s->h <= 0 ||
197 (unsigned)s->x + (unsigned)inlink->w > s->w ||
198 (unsigned)s->y + (unsigned)inlink->h > s->h) {
199 av_log(ctx, AV_LOG_ERROR,
200 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
201 s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
202 return AVERROR(EINVAL);
208 av_log(NULL, AV_LOG_ERROR,
209 "Error when evaluating the expression '%s'\n", expr);
214 static int config_output(AVFilterLink *outlink)
216 PadContext *s = outlink->src->priv;
223 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
225 PadContext *s = inlink->dst->priv;
229 if (s->inlink_w <= 0)
232 frame = ff_get_video_buffer(inlink->dst->outputs[0],
233 w + (s->w - s->in_w),
234 h + (s->h - s->in_h) + (s->x > 0));
242 for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
243 int hsub = s->draw.hsub[plane];
244 int vsub = s->draw.vsub[plane];
245 frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] +
246 (s->y >> vsub) * frame->linesize[plane];
252 /* check whether each plane in this buffer can be padded without copying */
253 static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
255 int planes[4] = { -1, -1, -1, -1}, *p = planes;
258 /* get all planes in this buffer */
259 for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
260 if (av_frame_get_plane_buffer(frame, i) == buf)
264 /* for each plane in this buffer, check that it can be padded without
265 * going over buffer bounds or other planes */
266 for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
267 int hsub = s->draw.hsub[planes[i]];
268 int vsub = s->draw.vsub[planes[i]];
270 uint8_t *start = frame->data[planes[i]];
271 uint8_t *end = start + (frame->height >> vsub) *
272 frame->linesize[planes[i]];
274 /* amount of free space needed before the start and after the end
276 ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
277 (s->y >> vsub) * frame->linesize[planes[i]];
278 ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
279 s->draw.pixelstep[planes[i]] +
280 ((s->h - s->y - frame->height) >> vsub) * frame->linesize[planes[i]];
282 if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
284 if (start - buf->data < req_start ||
285 (buf->data + buf->size) - end < req_end)
288 for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
289 int vsub1 = s->draw.vsub[planes[j]];
290 uint8_t *start1 = frame->data[planes[j]];
291 uint8_t *end1 = start1 + (frame->height >> vsub1) *
292 frame->linesize[planes[j]];
296 if (FFSIGN(start - end1) != FFSIGN(start - end1 - req_start) ||
297 FFSIGN(end - start1) != FFSIGN(end - start1 + req_end))
305 static int frame_needs_copy(PadContext *s, AVFrame *frame)
309 if (!av_frame_is_writable(frame))
312 for (i = 0; i < 4 && frame->buf[i]; i++)
313 if (buffer_needs_copy(s, frame, frame->buf[i]))
318 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
320 PadContext *s = inlink->dst->priv;
321 AVFilterLink *outlink = inlink->dst->outputs[0];
324 if(s->eval_mode == EVAL_MODE_FRAME && (
325 in->width != s->inlink_w
326 || in->height != s->inlink_h
327 || in->format != outlink->format
328 || in->sample_aspect_ratio.den != outlink->sample_aspect_ratio.den || in->sample_aspect_ratio.num != outlink->sample_aspect_ratio.num)) {
331 inlink->dst->inputs[0]->format = in->format;
332 inlink->dst->inputs[0]->w = in->width;
333 inlink->dst->inputs[0]->h = in->height;
335 inlink->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
336 inlink->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
339 if ((ret = config_input(inlink)) < 0) {
343 if ((ret = config_output(outlink)) < 0) {
349 needs_copy = frame_needs_copy(s, in);
352 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
353 out = ff_get_video_buffer(inlink->dst->outputs[0],
354 FFMAX(inlink->w, s->w),
355 FFMAX(inlink->h, s->h));
358 return AVERROR(ENOMEM);
361 av_frame_copy_props(out, in);
366 for (i = 0; i < 4 && out->data[i] && out->linesize[i]; i++) {
367 int hsub = s->draw.hsub[i];
368 int vsub = s->draw.vsub[i];
369 out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] +
370 (s->y >> vsub) * out->linesize[i];
376 ff_fill_rectangle(&s->draw, &s->color,
377 out->data, out->linesize,
382 if (s->h > s->y + s->in_h) {
383 ff_fill_rectangle(&s->draw, &s->color,
384 out->data, out->linesize,
385 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
389 ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
390 0, s->y, s->x, in->height);
393 ff_copy_rectangle2(&s->draw,
394 out->data, out->linesize, in->data, in->linesize,
395 s->x, s->y, 0, 0, in->width, in->height);
399 ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
400 s->x + s->in_w, s->y, s->w - s->x - s->in_w,
408 return ff_filter_frame(inlink->dst->outputs[0], out);
411 #define OFFSET(x) offsetof(PadContext, x)
412 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
414 static const AVOption pad_options[] = {
415 { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
416 { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
417 { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
418 { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
419 { "x", "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
420 { "y", "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
421 { "color", "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
422 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
423 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
424 { "frame", "eval expressions during initialization and per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
425 { "aspect", "pad to fit an aspect instead of a resolution", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, DBL_MAX, FLAGS },
429 AVFILTER_DEFINE_CLASS(pad);
431 static const AVFilterPad avfilter_vf_pad_inputs[] = {
434 .type = AVMEDIA_TYPE_VIDEO,
435 .config_props = config_input,
436 .get_video_buffer = get_video_buffer,
437 .filter_frame = filter_frame,
442 static const AVFilterPad avfilter_vf_pad_outputs[] = {
445 .type = AVMEDIA_TYPE_VIDEO,
446 .config_props = config_output,
451 AVFilter ff_vf_pad = {
453 .description = NULL_IF_CONFIG_SMALL("Pad the input video."),
454 .priv_size = sizeof(PadContext),
455 .priv_class = &pad_class,
456 .query_formats = query_formats,
457 .inputs = avfilter_vf_pad_inputs,
458 .outputs = avfilter_vf_pad_outputs,