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
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/avassert.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/opt.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 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
83 int w, h; ///< output dimensions, a value of 0 will result in the input size
84 int x, y; ///< offsets of the input area with respect to the padded area
85 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
87 char *w_expr; ///< width expression string
88 char *h_expr; ///< height expression string
89 char *x_expr; ///< width expression string
90 char *y_expr; ///< height expression string
92 uint8_t rgba_color[4]; ///< color for the padding area
97 static av_cold int init(AVFilterContext *ctx, const char *args)
99 PadContext *pad = ctx->priv;
101 if (av_parse_color(pad->rgba_color, pad->color_str, -1, ctx) < 0)
102 return AVERROR(EINVAL);
107 static int config_input(AVFilterLink *inlink)
109 AVFilterContext *ctx = inlink->dst;
110 PadContext *pad = ctx->priv;
112 double var_values[VARS_NB], res;
115 ff_draw_init(&pad->draw, inlink->format, 0);
116 ff_draw_color(&pad->draw, &pad->color, pad->rgba_color);
118 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
119 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
120 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
121 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
122 var_values[VAR_A] = (double) inlink->w / inlink->h;
123 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
124 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
125 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
126 var_values[VAR_HSUB] = 1 << pad->draw.hsub_max;
127 var_values[VAR_VSUB] = 1 << pad->draw.vsub_max;
129 /* evaluate width and height */
130 av_expr_parse_and_eval(&res, (expr = pad->w_expr),
131 var_names, var_values,
132 NULL, NULL, NULL, NULL, NULL, 0, ctx);
133 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
134 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
135 var_names, var_values,
136 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
138 pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
139 /* evaluate the width again, as it may depend on the evaluated output height */
140 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
141 var_names, var_values,
142 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
144 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
146 /* evaluate x and y */
147 av_expr_parse_and_eval(&res, (expr = pad->x_expr),
148 var_names, var_values,
149 NULL, NULL, NULL, NULL, NULL, 0, ctx);
150 pad->x = var_values[VAR_X] = res;
151 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
152 var_names, var_values,
153 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
155 pad->y = var_values[VAR_Y] = res;
156 /* evaluate x again, as it may depend on the evaluated y value */
157 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
158 var_names, var_values,
159 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
161 pad->x = var_values[VAR_X] = res;
163 /* sanity check params */
164 if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
165 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
166 return AVERROR(EINVAL);
174 pad->w = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->w);
175 pad->h = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->h);
176 pad->x = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->x);
177 pad->y = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->y);
178 pad->in_w = ff_draw_round_to_sub(&pad->draw, 0, -1, inlink->w);
179 pad->in_h = ff_draw_round_to_sub(&pad->draw, 1, -1, inlink->h);
181 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",
182 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
183 pad->rgba_color[0], pad->rgba_color[1], pad->rgba_color[2], pad->rgba_color[3]);
185 if (pad->x < 0 || pad->y < 0 ||
186 pad->w <= 0 || pad->h <= 0 ||
187 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
188 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
189 av_log(ctx, AV_LOG_ERROR,
190 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
191 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
192 return AVERROR(EINVAL);
198 av_log(NULL, AV_LOG_ERROR,
199 "Error when evaluating the expression '%s'\n", expr);
204 static int config_output(AVFilterLink *outlink)
206 PadContext *pad = outlink->src->priv;
213 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
215 PadContext *pad = inlink->dst->priv;
217 AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
218 w + (pad->w - pad->in_w),
219 h + (pad->h - pad->in_h));
228 for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
229 int hsub = pad->draw.hsub[plane];
230 int vsub = pad->draw.vsub[plane];
231 frame->data[plane] += (pad->x >> hsub) * pad->draw.pixelstep[plane] +
232 (pad->y >> vsub) * frame->linesize[plane];
238 /* check whether each plane in this buffer can be padded without copying */
239 static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
241 int planes[4] = { -1, -1, -1, -1}, *p = planes;
244 /* get all planes in this buffer */
245 for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
246 if (av_frame_get_plane_buffer(frame, i) == buf)
250 /* for each plane in this buffer, check that it can be padded without
251 * going over buffer bounds or other planes */
252 for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
253 int hsub = s->draw.hsub[planes[i]];
254 int vsub = s->draw.vsub[planes[i]];
256 uint8_t *start = frame->data[planes[i]];
257 uint8_t *end = start + (frame->height >> vsub) *
258 frame->linesize[planes[i]];
260 /* amount of free space needed before the start and after the end
262 ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
263 (s->y >> vsub) * frame->linesize[planes[i]];
264 ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
265 s->draw.pixelstep[planes[i]] +
266 (s->y >> vsub) * frame->linesize[planes[i]];
268 if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
270 if (start - buf->data < req_start ||
271 (buf->data + buf->size) - end < req_end)
274 #define SIGN(x) ((x) > 0 ? 1 : -1)
275 for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
276 int vsub1 = s->draw.vsub[planes[j]];
277 uint8_t *start1 = frame->data[planes[j]];
278 uint8_t *end1 = start1 + (frame->height >> vsub1) *
279 frame->linesize[planes[j]];
283 if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
284 SIGN(end - start1) != SIGN(end - start1 + req_end))
292 static int frame_needs_copy(PadContext *s, AVFrame *frame)
296 if (!av_frame_is_writable(frame))
299 for (i = 0; i < 4 && frame->buf[i]; i++)
300 if (buffer_needs_copy(s, frame, frame->buf[i]))
305 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
307 PadContext *pad = inlink->dst->priv;
309 int needs_copy = frame_needs_copy(pad, in);
312 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
313 out = ff_get_video_buffer(inlink->dst->outputs[0],
314 FFMAX(inlink->w, pad->w),
315 FFMAX(inlink->h, pad->h));
318 return AVERROR(ENOMEM);
321 av_frame_copy_props(out, in);
326 for (i = 0; i < 4 && out->data[i]; i++) {
327 int hsub = pad->draw.hsub[i];
328 int vsub = pad->draw.vsub[i];
329 out->data[i] -= (pad->x >> hsub) * pad->draw.pixelstep[i] +
330 (pad->y >> vsub) * out->linesize[i];
336 ff_fill_rectangle(&pad->draw, &pad->color,
337 out->data, out->linesize,
338 0, 0, pad->w, pad->y);
342 if (pad->h > pad->y + pad->in_h) {
343 ff_fill_rectangle(&pad->draw, &pad->color,
344 out->data, out->linesize,
345 0, pad->y + pad->in_h, pad->w, pad->h - pad->y - pad->in_h);
349 ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
350 0, pad->y, pad->x, in->height);
353 ff_copy_rectangle2(&pad->draw,
354 out->data, out->linesize, in->data, in->linesize,
355 pad->x, pad->y, 0, 0, in->width, in->height);
359 ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
360 pad->x + pad->in_w, pad->y, pad->w - pad->x - pad->in_w,
364 out->height = pad->h;
368 return ff_filter_frame(inlink->dst->outputs[0], out);
371 #define OFFSET(x) offsetof(PadContext, x)
372 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
374 static const AVOption pad_options[] = {
375 { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
376 { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
377 { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
378 { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
379 { "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 },
380 { "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 },
381 { "color", "set the color of the padded area border", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, .flags = FLAGS },
385 AVFILTER_DEFINE_CLASS(pad);
387 static const AVFilterPad avfilter_vf_pad_inputs[] = {
390 .type = AVMEDIA_TYPE_VIDEO,
391 .config_props = config_input,
392 .get_video_buffer = get_video_buffer,
393 .filter_frame = filter_frame,
398 static const AVFilterPad avfilter_vf_pad_outputs[] = {
401 .type = AVMEDIA_TYPE_VIDEO,
402 .config_props = config_output,
407 AVFilter avfilter_vf_pad = {
409 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
411 .priv_size = sizeof(PadContext),
412 .priv_class = &pad_class,
414 .query_formats = query_formats,
416 .inputs = avfilter_vf_pad_inputs,
418 .outputs = avfilter_vf_pad_outputs,