2 * Copyright (c) 2008 vmrsss
3 * Copyright (c) 2009 Stefano Sabatini
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 "libavutil/opt.h"
42 #include "drawutils.h"
44 static const char *const var_names[] = {
76 static int query_formats(AVFilterContext *ctx)
78 static const enum AVPixelFormat pix_fmts[] = {
79 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
80 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
81 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
83 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
84 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
85 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
86 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
87 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
93 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
97 typedef struct PadContext {
99 int w, h; ///< output dimensions, a value of 0 will result in the input size
100 int x, y; ///< offsets of the input area with respect to the padded area
101 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
103 char *w_expr; ///< width expression string
104 char *h_expr; ///< height expression string
105 char *x_expr; ///< width expression string
106 char *y_expr; ///< height expression string
109 uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
112 int hsub, vsub; ///< chroma subsampling values
115 static av_cold int init(AVFilterContext *ctx)
117 PadContext *s = ctx->priv;
119 if (av_parse_color(s->color, s->color_str, -1, ctx) < 0)
120 return AVERROR(EINVAL);
125 static av_cold void uninit(AVFilterContext *ctx)
127 PadContext *s = ctx->priv;
130 for (i = 0; i < 4; i++) {
131 av_freep(&s->line[i]);
136 static int config_input(AVFilterLink *inlink)
138 AVFilterContext *ctx = inlink->dst;
139 PadContext *s = ctx->priv;
140 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
141 uint8_t rgba_color[4];
142 int ret, is_packed_rgba;
143 double var_values[VARS_NB], res;
146 s->hsub = pix_desc->log2_chroma_w;
147 s->vsub = pix_desc->log2_chroma_h;
149 var_values[VAR_PI] = M_PI;
150 var_values[VAR_PHI] = M_PHI;
151 var_values[VAR_E] = M_E;
152 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
153 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
154 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
155 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
156 var_values[VAR_A] = (double) inlink->w / inlink->h;
157 var_values[VAR_HSUB] = 1<<s->hsub;
158 var_values[VAR_VSUB] = 1<<s->vsub;
160 /* evaluate width and height */
161 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
162 var_names, var_values,
163 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
165 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
166 if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
167 var_names, var_values,
168 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
170 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
171 /* evaluate the width again, as it may depend on the evaluated output height */
172 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
173 var_names, var_values,
174 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
176 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
178 /* evaluate x and y */
179 if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
180 var_names, var_values,
181 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
183 s->x = var_values[VAR_X] = res;
184 if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
185 var_names, var_values,
186 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
188 s->y = var_values[VAR_Y] = res;
189 /* evaluate x again, as it may depend on the evaluated y value */
190 if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
191 var_names, var_values,
192 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
194 s->x = var_values[VAR_X] = res;
196 /* sanity check params */
197 if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
198 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
199 return AVERROR(EINVAL);
207 s->w &= ~((1 << s->hsub) - 1);
208 s->h &= ~((1 << s->vsub) - 1);
209 s->x &= ~((1 << s->hsub) - 1);
210 s->y &= ~((1 << s->vsub) - 1);
212 s->in_w = inlink->w & ~((1 << s->hsub) - 1);
213 s->in_h = inlink->h & ~((1 << s->vsub) - 1);
215 memcpy(rgba_color, s->color, sizeof(rgba_color));
216 ff_fill_line_with_color(s->line, s->line_step, s->w, s->color,
217 inlink->format, rgba_color, &is_packed_rgba, NULL);
219 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
220 inlink->w, inlink->h, s->w, s->h, s->x, s->y,
221 s->color[0], s->color[1], s->color[2], s->color[3],
222 is_packed_rgba ? "rgba" : "yuva");
224 if (s->x < 0 || s->y < 0 ||
225 s->w <= 0 || s->h <= 0 ||
226 (unsigned)s->x + (unsigned)inlink->w > s->w ||
227 (unsigned)s->y + (unsigned)inlink->h > s->h) {
228 av_log(ctx, AV_LOG_ERROR,
229 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
230 s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
231 return AVERROR(EINVAL);
237 av_log(NULL, AV_LOG_ERROR,
238 "Error when evaluating the expression '%s'\n", expr);
243 static int config_output(AVFilterLink *outlink)
245 PadContext *s = outlink->src->priv;
252 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
254 PadContext *s = inlink->dst->priv;
256 AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
257 w + (s->w - s->in_w),
258 h + (s->h - s->in_h));
267 for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
268 int hsub = (plane == 1 || plane == 2) ? s->hsub : 0;
269 int vsub = (plane == 1 || plane == 2) ? s->vsub : 0;
271 frame->data[plane] += (s->x >> hsub) * s->line_step[plane] +
272 (s->y >> vsub) * frame->linesize[plane];
278 /* check whether each plane in this buffer can be padded without copying */
279 static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
281 int planes[4] = { -1, -1, -1, -1}, *p = planes;
284 /* get all planes in this buffer */
285 for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
286 if (av_frame_get_plane_buffer(frame, i) == buf)
290 /* for each plane in this buffer, check that it can be padded without
291 * going over buffer bounds or other planes */
292 for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
293 int hsub = (planes[i] == 1 || planes[i] == 2) ? s->hsub : 0;
294 int vsub = (planes[i] == 1 || planes[i] == 2) ? s->vsub : 0;
296 uint8_t *start = frame->data[planes[i]];
297 uint8_t *end = start + (frame->height >> hsub) *
298 frame->linesize[planes[i]];
300 /* amount of free space needed before the start and after the end
302 ptrdiff_t req_start = (s->x >> hsub) * s->line_step[planes[i]] +
303 (s->y >> vsub) * frame->linesize[planes[i]];
304 ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
305 s->line_step[planes[i]] +
306 (s->y >> vsub) * frame->linesize[planes[i]];
308 if (frame->linesize[planes[i]] < (s->w >> hsub) * s->line_step[planes[i]])
310 if (start - buf->data < req_start ||
311 (buf->data + buf->size) - end < req_end)
314 #define SIGN(x) ((x) > 0 ? 1 : -1)
315 for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
316 int hsub1 = (planes[j] == 1 || planes[j] == 2) ? s->hsub : 0;
317 uint8_t *start1 = frame->data[planes[j]];
318 uint8_t *end1 = start1 + (frame->height >> hsub1) *
319 frame->linesize[planes[j]];
323 if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
324 SIGN(end - start1) != SIGN(end - start1 + req_end))
332 static int frame_needs_copy(PadContext *s, AVFrame *frame)
336 if (!av_frame_is_writable(frame))
339 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
340 if (buffer_needs_copy(s, frame, frame->buf[i]))
345 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
347 PadContext *s = inlink->dst->priv;
349 int 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 < FF_ARRAY_ELEMS(out->data) && out->data[i]; i++) {
367 int hsub = (i == 1 || i == 2) ? s->hsub : 0;
368 int vsub = (i == 1 || i == 2) ? s->vsub : 0;
369 out->data[i] -= (s->x >> hsub) * s->line_step[i] +
370 (s->y >> vsub) * out->linesize[i];
376 ff_draw_rectangle(out->data, out->linesize,
377 s->line, s->line_step, s->hsub, s->vsub,
382 if (s->h > s->y + s->in_h) {
383 ff_draw_rectangle(out->data, out->linesize,
384 s->line, s->line_step, s->hsub, s->vsub,
385 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
389 ff_draw_rectangle(out->data, out->linesize, s->line, s->line_step,
390 s->hsub, s->vsub, 0, s->y, s->x, in->height);
393 ff_copy_rectangle(out->data, out->linesize, in->data, in->linesize,
394 s->line_step, s->hsub, s->vsub,
395 s->x, s->y, 0, in->width, in->height);
399 ff_draw_rectangle(out->data, out->linesize,
400 s->line, s->line_step, s->hsub, s->vsub,
401 s->x + s->in_w, s->y, s->w - s->x - s->in_w,
409 return ff_filter_frame(inlink->dst->outputs[0], out);
412 #define OFFSET(x) offsetof(PadContext, x)
413 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
414 static const AVOption options[] = {
415 { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
416 { "height", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
417 { "x", "Horizontal position of the left edge of the input video in the "
418 "output video", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
419 { "y", "Vertical position of the top edge of the input video in the "
420 "output video", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
421 { "color", "Color of the padded area", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, .flags = FLAGS },
425 static const AVClass pad_class = {
427 .item_name = av_default_item_name,
429 .version = LIBAVUTIL_VERSION_INT,
432 static const AVFilterPad avfilter_vf_pad_inputs[] = {
435 .type = AVMEDIA_TYPE_VIDEO,
436 .config_props = config_input,
437 .get_video_buffer = get_video_buffer,
438 .filter_frame = filter_frame,
443 static const AVFilterPad avfilter_vf_pad_outputs[] = {
446 .type = AVMEDIA_TYPE_VIDEO,
447 .config_props = config_output,
452 AVFilter ff_vf_pad = {
454 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
456 .priv_size = sizeof(PadContext),
457 .priv_class = &pad_class,
460 .query_formats = query_formats,
462 .inputs = avfilter_vf_pad_inputs,
464 .outputs = avfilter_vf_pad_outputs,