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/parseutils.h"
39 #include "libavutil/mathematics.h"
40 #include "drawutils.h"
42 static const char *const var_names[] = {
72 static int query_formats(AVFilterContext *ctx)
74 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
79 int w, h; ///< output dimensions, a value of 0 will result in the input size
80 int x, y; ///< offsets of the input area with respect to the padded area
81 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
83 char w_expr[256]; ///< width expression string
84 char h_expr[256]; ///< height expression string
85 char x_expr[256]; ///< width expression string
86 char y_expr[256]; ///< height expression string
88 uint8_t rgba_color[4]; ///< color for the padding area
93 static av_cold int init(AVFilterContext *ctx, const char *args)
95 PadContext *pad = ctx->priv;
96 char color_string[128] = "black";
98 av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
99 av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
100 av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
101 av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
104 sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%127s",
105 pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
107 if (av_parse_color(pad->rgba_color, color_string, -1, ctx) < 0)
108 return AVERROR(EINVAL);
113 static int config_input(AVFilterLink *inlink)
115 AVFilterContext *ctx = inlink->dst;
116 PadContext *pad = ctx->priv;
118 double var_values[VARS_NB], res;
121 ff_draw_init(&pad->draw, inlink->format, 0);
122 ff_draw_color(&pad->draw, &pad->color, pad->rgba_color);
124 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
125 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
126 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
127 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
128 var_values[VAR_A] = (double) inlink->w / inlink->h;
129 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
130 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
131 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
132 var_values[VAR_HSUB] = 1 << pad->draw.hsub_max;
133 var_values[VAR_VSUB] = 1 << pad->draw.vsub_max;
135 /* evaluate width and height */
136 av_expr_parse_and_eval(&res, (expr = pad->w_expr),
137 var_names, var_values,
138 NULL, NULL, NULL, NULL, NULL, 0, ctx);
139 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
140 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
141 var_names, var_values,
142 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
144 pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
145 /* evaluate the width again, as it may depend on the evaluated output height */
146 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
147 var_names, var_values,
148 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
150 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
152 /* evaluate x and y */
153 av_expr_parse_and_eval(&res, (expr = pad->x_expr),
154 var_names, var_values,
155 NULL, NULL, NULL, NULL, NULL, 0, ctx);
156 pad->x = var_values[VAR_X] = res;
157 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
158 var_names, var_values,
159 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
161 pad->y = var_values[VAR_Y] = res;
162 /* evaluate x again, as it may depend on the evaluated y value */
163 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
164 var_names, var_values,
165 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
167 pad->x = var_values[VAR_X] = res;
169 /* sanity check params */
170 if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
171 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
172 return AVERROR(EINVAL);
180 pad->w = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->w);
181 pad->h = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->h);
182 pad->x = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->x);
183 pad->y = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->y);
184 pad->in_w = ff_draw_round_to_sub(&pad->draw, 0, -1, inlink->w);
185 pad->in_h = ff_draw_round_to_sub(&pad->draw, 1, -1, inlink->h);
187 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",
188 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
189 pad->rgba_color[0], pad->rgba_color[1], pad->rgba_color[2], pad->rgba_color[3]);
191 if (pad->x < 0 || pad->y < 0 ||
192 pad->w <= 0 || pad->h <= 0 ||
193 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
194 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
195 av_log(ctx, AV_LOG_ERROR,
196 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
197 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
198 return AVERROR(EINVAL);
204 av_log(NULL, AV_LOG_ERROR,
205 "Error when evaluating the expression '%s'\n", expr);
210 static int config_output(AVFilterLink *outlink)
212 PadContext *pad = outlink->src->priv;
219 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
221 PadContext *pad = inlink->dst->priv;
222 int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1;
224 AVFilterBufferRef *picref = ff_get_video_buffer(inlink->dst->outputs[0], perms,
225 w + (pad->w - pad->in_w) + 4*align,
226 h + (pad->h - pad->in_h));
232 picref->video->w = w;
233 picref->video->h = h;
235 for (plane = 0; plane < 4 && picref->data[plane]; plane++)
236 picref->data[plane] += FFALIGN(pad->x >> pad->draw.hsub[plane], align) * pad->draw.pixelstep[plane] +
237 (pad->y >> pad->draw.vsub[plane]) * picref->linesize[plane];
242 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
244 int64_t x_in_buf, y_in_buf;
246 x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
247 + (x >> hsub) * pad->draw.pixelstep[plane]
248 + (y >> vsub) * outpicref->linesize[plane];
250 if(x_in_buf < 0 || x_in_buf % pad->draw.pixelstep[plane])
252 x_in_buf /= pad->draw.pixelstep[plane];
254 av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
256 y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
257 x_in_buf %= outpicref->buf->linesize[plane];
259 if( y_in_buf<<vsub >= outpicref->buf->h
260 || x_in_buf<<hsub >= outpicref->buf->w)
265 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
267 PadContext *pad = inlink->dst->priv;
268 AVFilterBufferRef *out = avfilter_ref_buffer(in, ~0);
269 int plane, needs_copy;
272 avfilter_unref_bufferp(&in);
273 return AVERROR(ENOMEM);
276 for (plane = 0; plane < 4 && out->data[plane] && pad->draw.pixelstep[plane]; plane++) {
277 int hsub = pad->draw.hsub[plane];
278 int vsub = pad->draw.vsub[plane];
280 av_assert0(out->buf->w > 0 && out->buf->h > 0);
282 if (out->format != out->buf->format) //unsupported currently
285 out->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane] +
286 (pad->y >> vsub) * out->linesize[plane];
288 if (does_clip(pad, out, plane, hsub, vsub, 0, 0) ||
289 does_clip(pad, out, plane, hsub, vsub, 0, pad->h - 1) ||
290 does_clip(pad, out, plane, hsub, vsub, pad->w - 1, 0) ||
291 does_clip(pad, out, plane, hsub, vsub, pad->w - 1, pad->h - 1))
294 needs_copy = plane < 4 && out->data[plane] || !(out->perms & AV_PERM_WRITE);
296 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
297 avfilter_unref_buffer(out);
298 out = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
299 FFMAX(inlink->w, pad->w),
300 FFMAX(inlink->h, pad->h));
302 avfilter_unref_bufferp(&in);
303 return AVERROR(ENOMEM);
306 avfilter_copy_buffer_ref_props(out, in);
309 out->video->w = pad->w;
310 out->video->h = pad->h;
314 ff_fill_rectangle(&pad->draw, &pad->color,
315 out->data, out->linesize,
316 0, 0, pad->w, pad->y);
320 if (pad->h > pad->y + pad->in_h) {
321 ff_fill_rectangle(&pad->draw, &pad->color,
322 out->data, out->linesize,
323 0, pad->y + pad->in_h, pad->w, pad->h - pad->y - pad->in_h);
327 ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
328 0, pad->y, pad->x, in->video->h);
331 ff_copy_rectangle2(&pad->draw,
332 out->data, out->linesize, in->data, in->linesize,
333 pad->x, pad->y, 0, 0, in->video->w, in->video->h);
337 ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
338 pad->x + pad->in_w, pad->y, pad->w - pad->x - pad->in_w,
341 avfilter_unref_bufferp(&in);
342 return ff_filter_frame(inlink->dst->outputs[0], out);
345 static const AVFilterPad avfilter_vf_pad_inputs[] = {
348 .type = AVMEDIA_TYPE_VIDEO,
349 .config_props = config_input,
350 .get_video_buffer = get_video_buffer,
351 .filter_frame = filter_frame,
356 static const AVFilterPad avfilter_vf_pad_outputs[] = {
359 .type = AVMEDIA_TYPE_VIDEO,
360 .config_props = config_output,
365 AVFilter avfilter_vf_pad = {
367 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
369 .priv_size = sizeof(PadContext),
371 .query_formats = query_formats,
373 .inputs = avfilter_vf_pad_inputs,
375 .outputs = avfilter_vf_pad_outputs,