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
28 #include "libavutil/avstring.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/colorspace.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/parseutils.h"
35 #include "libavutil/mathematics.h"
36 #include "drawutils.h"
38 static const char *const var_names[] = {
68 static int query_formats(AVFilterContext *ctx)
70 avfilter_set_common_pixel_formats(ctx, ff_draw_supported_pixel_formats(0));
75 int w, h; ///< output dimensions, a value of 0 will result in the input size
76 int x, y; ///< offsets of the input area with respect to the padded area
77 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
79 char w_expr[256]; ///< width expression string
80 char h_expr[256]; ///< height expression string
81 char x_expr[256]; ///< width expression string
82 char y_expr[256]; ///< height expression string
84 uint8_t rgba_color[4]; ///< color for the padding area
90 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
92 PadContext *pad = ctx->priv;
93 char color_string[128] = "black";
95 av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
96 av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
97 av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
98 av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
101 sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%127s",
102 pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
104 if (av_parse_color(pad->rgba_color, color_string, -1, ctx) < 0)
105 return AVERROR(EINVAL);
110 static int config_input(AVFilterLink *inlink)
112 AVFilterContext *ctx = inlink->dst;
113 PadContext *pad = ctx->priv;
115 double var_values[VARS_NB], res;
118 ff_draw_init(&pad->draw, inlink->format, 0);
119 ff_draw_color(&pad->draw, &pad->color, pad->rgba_color);
121 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
122 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
123 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
124 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
125 var_values[VAR_A] = (float) inlink->w / inlink->h;
126 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
127 (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
128 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
129 var_values[VAR_HSUB] = 1 << pad->draw.hsub_max;
130 var_values[VAR_VSUB] = 1 << pad->draw.vsub_max;
132 /* evaluate width and height */
133 av_expr_parse_and_eval(&res, (expr = pad->w_expr),
134 var_names, var_values,
135 NULL, NULL, NULL, NULL, NULL, 0, ctx);
136 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
137 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
138 var_names, var_values,
139 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
141 pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
142 /* evaluate the width again, as it may depend on the evaluated output height */
143 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
144 var_names, var_values,
145 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
147 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
149 /* evaluate x and y */
150 av_expr_parse_and_eval(&res, (expr = pad->x_expr),
151 var_names, var_values,
152 NULL, NULL, NULL, NULL, NULL, 0, ctx);
153 pad->x = var_values[VAR_X] = res;
154 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
155 var_names, var_values,
156 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
158 pad->y = var_values[VAR_Y] = res;
159 /* evaluate x again, as it may depend on the evaluated y value */
160 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
161 var_names, var_values,
162 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
164 pad->x = var_values[VAR_X] = res;
166 /* sanity check params */
167 if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
168 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
169 return AVERROR(EINVAL);
177 pad->w = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->w);
178 pad->h = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->h);
179 pad->x = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->x);
180 pad->y = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->y);
181 pad->in_w = ff_draw_round_to_sub(&pad->draw, 0, -1, inlink->w);
182 pad->in_h = ff_draw_round_to_sub(&pad->draw, 1, -1, inlink->h);
184 av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n",
185 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
186 pad->rgba_color[0], pad->rgba_color[1], pad->rgba_color[2], pad->rgba_color[3]);
188 if (pad->x < 0 || pad->y < 0 ||
189 pad->w <= 0 || pad->h <= 0 ||
190 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
191 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
192 av_log(ctx, AV_LOG_ERROR,
193 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
194 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
195 return AVERROR(EINVAL);
201 av_log(NULL, AV_LOG_ERROR,
202 "Error when evaluating the expression '%s'\n", expr);
207 static int config_output(AVFilterLink *outlink)
209 PadContext *pad = outlink->src->priv;
216 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
218 PadContext *pad = inlink->dst->priv;
219 int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1;
221 AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
222 w + (pad->w - pad->in_w) + 4*align,
223 h + (pad->h - pad->in_h));
226 picref->video->w = w;
227 picref->video->h = h;
229 for (plane = 0; plane < 4 && picref->data[plane]; plane++)
230 picref->data[plane] += FFALIGN(pad->x >> pad->draw.hsub[plane], align) * pad->draw.pixelstep[plane] +
231 (pad->y >> pad->draw.vsub[plane]) * picref->linesize[plane];
236 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
238 int64_t x_in_buf, y_in_buf;
240 x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
241 + (x >> hsub) * pad->draw.pixelstep[plane]
242 + (y >> vsub) * outpicref->linesize[plane];
244 if(x_in_buf < 0 || x_in_buf % pad->draw.pixelstep[plane])
246 x_in_buf /= pad->draw.pixelstep[plane];
248 av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
250 y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
251 x_in_buf %= outpicref->buf->linesize[plane];
253 if( y_in_buf<<vsub >= outpicref->buf->h
254 || x_in_buf<<hsub >= outpicref->buf->w)
259 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
261 PadContext *pad = inlink->dst->priv;
262 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
265 for (plane = 0; plane < 4 && outpicref->data[plane] && pad->draw.pixelstep[plane]; plane++) {
266 int hsub = pad->draw.hsub[plane];
267 int vsub = pad->draw.vsub[plane];
269 av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
271 if(outpicref->format != outpicref->buf->format) //unsupported currently
274 outpicref->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane]
275 + (pad->y >> vsub) * outpicref->linesize[plane];
277 if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
278 || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
279 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
280 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
284 pad->needs_copy= plane < 4 && outpicref->data[plane];
286 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
287 avfilter_unref_buffer(outpicref);
288 outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
289 FFMAX(inlink->w, pad->w),
290 FFMAX(inlink->h, pad->h));
291 avfilter_copy_buffer_ref_props(outpicref, inpicref);
294 inlink->dst->outputs[0]->out_buf = outpicref;
296 outpicref->video->w = pad->w;
297 outpicref->video->h = pad->h;
299 avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(outpicref, ~0));
302 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
304 PadContext *pad = link->dst->priv;
305 int bar_y, bar_h = 0;
307 if (slice_dir * before_slice == 1 && y == pad->y) {
311 } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
313 bar_y = pad->y + pad->in_h;
314 bar_h = pad->h - pad->in_h - pad->y;
318 ff_fill_rectangle(&pad->draw, &pad->color,
319 link->dst->outputs[0]->out_buf->data,
320 link->dst->outputs[0]->out_buf->linesize,
321 0, bar_y, pad->w, bar_h);
322 avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
326 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
328 PadContext *pad = link->dst->priv;
329 AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
330 AVFilterBufferRef *inpic = link->cur_buf;
334 y = ff_draw_round_to_sub(&pad->draw, 1, -1, y);
335 h = ff_draw_round_to_sub(&pad->draw, 1, -1, h);
339 draw_send_bar_slice(link, y, h, slice_dir, 1);
342 ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
346 ff_copy_rectangle2(&pad->draw,
347 outpic->data, outpic->linesize,
348 inpic ->data, inpic ->linesize,
349 pad->x, y, 0, y - pad->y, inpic->video->w, h);
353 ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
354 pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
355 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
357 draw_send_bar_slice(link, y, h, slice_dir, -1);
360 AVFilter avfilter_vf_pad = {
362 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
364 .priv_size = sizeof(PadContext),
366 .query_formats = query_formats,
368 .inputs = (const AVFilterPad[]) {{ .name = "default",
369 .type = AVMEDIA_TYPE_VIDEO,
370 .config_props = config_input,
371 .get_video_buffer = get_video_buffer,
372 .start_frame = start_frame,
373 .draw_slice = draw_slice, },
376 .outputs = (const AVFilterPad[]) {{ .name = "default",
377 .type = AVMEDIA_TYPE_VIDEO,
378 .config_props = config_output, },