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 "drawutils.h"
43 static const char *const var_names[] = {
73 static int query_formats(AVFilterContext *ctx)
75 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
81 int w, h; ///< output dimensions, a value of 0 will result in the input size
82 int x, y; ///< offsets of the input area with respect to the padded area
83 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
85 char *w_expr; ///< width expression string
86 char *h_expr; ///< height expression string
87 char *x_expr; ///< width expression string
88 char *y_expr; ///< height expression string
90 uint8_t rgba_color[4]; ///< color for the padding area
95 #define OFFSET(x) offsetof(PadContext, x)
96 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
98 static const AVOption pad_options[] = {
99 { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
100 { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
101 { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
102 { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
103 { "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 },
104 { "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 },
105 { "color", "set the color of the padded area border", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, .flags = FLAGS },
109 AVFILTER_DEFINE_CLASS(pad);
111 static av_cold int init(AVFilterContext *ctx, const char *args)
113 PadContext *pad = ctx->priv;
114 static const char *shorthand[] = { "width", "height", "x", "y", "color", NULL };
117 pad->class = &pad_class;
118 av_opt_set_defaults(pad);
120 if ((ret = av_opt_set_from_string(pad, args, shorthand, "=", ":")) < 0)
123 if (av_parse_color(pad->rgba_color, pad->color_str, -1, ctx) < 0)
124 return AVERROR(EINVAL);
129 static av_cold void uninit(AVFilterContext *ctx)
131 PadContext *pad = ctx->priv;
135 static int config_input(AVFilterLink *inlink)
137 AVFilterContext *ctx = inlink->dst;
138 PadContext *pad = ctx->priv;
140 double var_values[VARS_NB], res;
143 ff_draw_init(&pad->draw, inlink->format, 0);
144 ff_draw_color(&pad->draw, &pad->color, pad->rgba_color);
146 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
147 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
148 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
149 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
150 var_values[VAR_A] = (double) inlink->w / inlink->h;
151 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
152 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
153 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
154 var_values[VAR_HSUB] = 1 << pad->draw.hsub_max;
155 var_values[VAR_VSUB] = 1 << pad->draw.vsub_max;
157 /* evaluate width and height */
158 av_expr_parse_and_eval(&res, (expr = pad->w_expr),
159 var_names, var_values,
160 NULL, NULL, NULL, NULL, NULL, 0, ctx);
161 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
162 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
163 var_names, var_values,
164 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
166 pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
167 /* evaluate the width again, as it may depend on the evaluated output height */
168 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
169 var_names, var_values,
170 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
172 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
174 /* evaluate x and y */
175 av_expr_parse_and_eval(&res, (expr = pad->x_expr),
176 var_names, var_values,
177 NULL, NULL, NULL, NULL, NULL, 0, ctx);
178 pad->x = var_values[VAR_X] = res;
179 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
180 var_names, var_values,
181 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
183 pad->y = var_values[VAR_Y] = res;
184 /* evaluate x again, as it may depend on the evaluated y value */
185 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
186 var_names, var_values,
187 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
189 pad->x = var_values[VAR_X] = res;
191 /* sanity check params */
192 if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
193 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
194 return AVERROR(EINVAL);
202 pad->w = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->w);
203 pad->h = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->h);
204 pad->x = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->x);
205 pad->y = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->y);
206 pad->in_w = ff_draw_round_to_sub(&pad->draw, 0, -1, inlink->w);
207 pad->in_h = ff_draw_round_to_sub(&pad->draw, 1, -1, inlink->h);
209 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",
210 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
211 pad->rgba_color[0], pad->rgba_color[1], pad->rgba_color[2], pad->rgba_color[3]);
213 if (pad->x < 0 || pad->y < 0 ||
214 pad->w <= 0 || pad->h <= 0 ||
215 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
216 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
217 av_log(ctx, AV_LOG_ERROR,
218 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
219 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
220 return AVERROR(EINVAL);
226 av_log(NULL, AV_LOG_ERROR,
227 "Error when evaluating the expression '%s'\n", expr);
232 static int config_output(AVFilterLink *outlink)
234 PadContext *pad = outlink->src->priv;
241 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
243 PadContext *pad = inlink->dst->priv;
244 int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1;
246 AVFilterBufferRef *picref = ff_get_video_buffer(inlink->dst->outputs[0], perms,
247 w + (pad->w - pad->in_w) + 4*align,
248 h + (pad->h - pad->in_h));
254 picref->video->w = w;
255 picref->video->h = h;
257 for (plane = 0; plane < 4 && picref->data[plane]; plane++)
258 picref->data[plane] += FFALIGN(pad->x >> pad->draw.hsub[plane], align) * pad->draw.pixelstep[plane] +
259 (pad->y >> pad->draw.vsub[plane]) * picref->linesize[plane];
264 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
266 int64_t x_in_buf, y_in_buf;
268 x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
269 + (x >> hsub) * pad->draw.pixelstep[plane]
270 + (y >> vsub) * outpicref->linesize[plane];
272 if(x_in_buf < 0 || x_in_buf % pad->draw.pixelstep[plane])
274 x_in_buf /= pad->draw.pixelstep[plane];
276 av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
278 y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
279 x_in_buf %= outpicref->buf->linesize[plane];
281 if( y_in_buf<<vsub >= outpicref->buf->h
282 || x_in_buf<<hsub >= outpicref->buf->w)
287 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
289 PadContext *pad = inlink->dst->priv;
290 AVFilterBufferRef *out = avfilter_ref_buffer(in, ~0);
291 int plane, needs_copy;
294 avfilter_unref_bufferp(&in);
295 return AVERROR(ENOMEM);
298 for (plane = 0; plane < 4 && out->data[plane] && pad->draw.pixelstep[plane]; plane++) {
299 int hsub = pad->draw.hsub[plane];
300 int vsub = pad->draw.vsub[plane];
302 av_assert0(out->buf->w > 0 && out->buf->h > 0);
304 if (out->format != out->buf->format) //unsupported currently
307 out->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane] +
308 (pad->y >> vsub) * out->linesize[plane];
310 if (does_clip(pad, out, plane, hsub, vsub, 0, 0) ||
311 does_clip(pad, out, plane, hsub, vsub, 0, pad->h - 1) ||
312 does_clip(pad, out, plane, hsub, vsub, pad->w - 1, 0) ||
313 does_clip(pad, out, plane, hsub, vsub, pad->w - 1, pad->h - 1))
316 needs_copy = plane < 4 && out->data[plane] || !(out->perms & AV_PERM_WRITE);
318 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
319 avfilter_unref_buffer(out);
320 out = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
321 FFMAX(inlink->w, pad->w),
322 FFMAX(inlink->h, pad->h));
324 avfilter_unref_bufferp(&in);
325 return AVERROR(ENOMEM);
328 avfilter_copy_buffer_ref_props(out, in);
331 out->video->w = pad->w;
332 out->video->h = pad->h;
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->video->h);
353 ff_copy_rectangle2(&pad->draw,
354 out->data, out->linesize, in->data, in->linesize,
355 pad->x, pad->y, 0, 0, in->video->w, in->video->h);
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,
363 avfilter_unref_bufferp(&in);
364 return ff_filter_frame(inlink->dst->outputs[0], out);
367 static const AVFilterPad avfilter_vf_pad_inputs[] = {
370 .type = AVMEDIA_TYPE_VIDEO,
371 .config_props = config_input,
372 .get_video_buffer = get_video_buffer,
373 .filter_frame = filter_frame,
378 static const AVFilterPad avfilter_vf_pad_outputs[] = {
381 .type = AVMEDIA_TYPE_VIDEO,
382 .config_props = config_output,
387 AVFilter avfilter_vf_pad = {
389 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
391 .priv_size = sizeof(PadContext),
394 .query_formats = query_formats,
396 .inputs = avfilter_vf_pad_inputs,
398 .outputs = avfilter_vf_pad_outputs,
399 .priv_class = &pad_class,