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 av_expr_parse_and_eval(&res, (expr = s->w_expr),
162 var_names, var_values,
163 NULL, NULL, NULL, NULL, NULL, 0, ctx);
164 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
165 if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
166 var_names, var_values,
167 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
169 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
170 /* evaluate the width again, as it may depend on the evaluated output height */
171 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
172 var_names, var_values,
173 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
175 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
177 /* evaluate x and y */
178 av_expr_parse_and_eval(&res, (expr = s->x_expr),
179 var_names, var_values,
180 NULL, NULL, NULL, NULL, NULL, 0, ctx);
181 s->x = var_values[VAR_X] = res;
182 if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
183 var_names, var_values,
184 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
186 s->y = var_values[VAR_Y] = res;
187 /* evaluate x again, as it may depend on the evaluated y value */
188 if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
189 var_names, var_values,
190 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
192 s->x = var_values[VAR_X] = res;
194 /* sanity check params */
195 if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
196 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
197 return AVERROR(EINVAL);
205 s->w &= ~((1 << s->hsub) - 1);
206 s->h &= ~((1 << s->vsub) - 1);
207 s->x &= ~((1 << s->hsub) - 1);
208 s->y &= ~((1 << s->vsub) - 1);
210 s->in_w = inlink->w & ~((1 << s->hsub) - 1);
211 s->in_h = inlink->h & ~((1 << s->vsub) - 1);
213 memcpy(rgba_color, s->color, sizeof(rgba_color));
214 ff_fill_line_with_color(s->line, s->line_step, s->w, s->color,
215 inlink->format, rgba_color, &is_packed_rgba, NULL);
217 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",
218 inlink->w, inlink->h, s->w, s->h, s->x, s->y,
219 s->color[0], s->color[1], s->color[2], s->color[3],
220 is_packed_rgba ? "rgba" : "yuva");
222 if (s->x < 0 || s->y < 0 ||
223 s->w <= 0 || s->h <= 0 ||
224 (unsigned)s->x + (unsigned)inlink->w > s->w ||
225 (unsigned)s->y + (unsigned)inlink->h > s->h) {
226 av_log(ctx, AV_LOG_ERROR,
227 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
228 s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
229 return AVERROR(EINVAL);
235 av_log(NULL, AV_LOG_ERROR,
236 "Error when evaluating the expression '%s'\n", expr);
241 static int config_output(AVFilterLink *outlink)
243 PadContext *s = outlink->src->priv;
250 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
252 PadContext *s = inlink->dst->priv;
254 AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
255 w + (s->w - s->in_w),
256 h + (s->h - s->in_h));
265 for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
266 int hsub = (plane == 1 || plane == 2) ? s->hsub : 0;
267 int vsub = (plane == 1 || plane == 2) ? s->vsub : 0;
269 frame->data[plane] += (s->x >> hsub) * s->line_step[plane] +
270 (s->y >> vsub) * frame->linesize[plane];
276 /* check whether each plane in this buffer can be padded without copying */
277 static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
279 int planes[4] = { -1, -1, -1, -1}, *p = planes;
282 /* get all planes in this buffer */
283 for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
284 if (av_frame_get_plane_buffer(frame, i) == buf)
288 /* for each plane in this buffer, check that it can be padded without
289 * going over buffer bounds or other planes */
290 for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
291 int hsub = (planes[i] == 1 || planes[i] == 2) ? s->hsub : 0;
292 int vsub = (planes[i] == 1 || planes[i] == 2) ? s->vsub : 0;
294 uint8_t *start = frame->data[planes[i]];
295 uint8_t *end = start + (frame->height >> hsub) *
296 frame->linesize[planes[i]];
298 /* amount of free space needed before the start and after the end
300 ptrdiff_t req_start = (s->x >> hsub) * s->line_step[planes[i]] +
301 (s->y >> vsub) * frame->linesize[planes[i]];
302 ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
303 s->line_step[planes[i]] +
304 (s->y >> vsub) * frame->linesize[planes[i]];
306 if (frame->linesize[planes[i]] < (s->w >> hsub) * s->line_step[planes[i]])
308 if (start - buf->data < req_start ||
309 (buf->data + buf->size) - end < req_end)
312 #define SIGN(x) ((x) > 0 ? 1 : -1)
313 for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
314 int hsub1 = (planes[j] == 1 || planes[j] == 2) ? s->hsub : 0;
315 uint8_t *start1 = frame->data[planes[j]];
316 uint8_t *end1 = start1 + (frame->height >> hsub1) *
317 frame->linesize[planes[j]];
321 if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
322 SIGN(end - start1) != SIGN(end - start1 + req_end))
330 static int frame_needs_copy(PadContext *s, AVFrame *frame)
334 if (!av_frame_is_writable(frame))
337 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
338 if (buffer_needs_copy(s, frame, frame->buf[i]))
343 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
345 PadContext *s = inlink->dst->priv;
347 int needs_copy = frame_needs_copy(s, in);
350 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
351 out = ff_get_video_buffer(inlink->dst->outputs[0],
352 FFMAX(inlink->w, s->w),
353 FFMAX(inlink->h, s->h));
356 return AVERROR(ENOMEM);
359 av_frame_copy_props(out, in);
364 for (i = 0; i < FF_ARRAY_ELEMS(out->data) && out->data[i]; i++) {
365 int hsub = (i == 1 || i == 2) ? s->hsub : 0;
366 int vsub = (i == 1 || i == 2) ? s->vsub : 0;
367 out->data[i] -= (s->x >> hsub) * s->line_step[i] +
368 (s->y >> vsub) * out->linesize[i];
374 ff_draw_rectangle(out->data, out->linesize,
375 s->line, s->line_step, s->hsub, s->vsub,
380 if (s->h > s->y + s->in_h) {
381 ff_draw_rectangle(out->data, out->linesize,
382 s->line, s->line_step, s->hsub, s->vsub,
383 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
387 ff_draw_rectangle(out->data, out->linesize, s->line, s->line_step,
388 s->hsub, s->vsub, 0, s->y, s->x, in->height);
391 ff_copy_rectangle(out->data, out->linesize, in->data, in->linesize,
392 s->line_step, s->hsub, s->vsub,
393 s->x, s->y, 0, in->width, in->height);
397 ff_draw_rectangle(out->data, out->linesize,
398 s->line, s->line_step, s->hsub, s->vsub,
399 s->x + s->in_w, s->y, s->w - s->x - s->in_w,
407 return ff_filter_frame(inlink->dst->outputs[0], out);
410 #define OFFSET(x) offsetof(PadContext, x)
411 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
412 static const AVOption options[] = {
413 { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
414 { "height", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
415 { "x", "Horizontal position of the left edge of the input video in the "
416 "output video", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
417 { "y", "Vertical position of the top edge of the input video in the "
418 "output video", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
419 { "color", "Color of the padded area", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, .flags = FLAGS },
423 static const AVClass pad_class = {
425 .item_name = av_default_item_name,
427 .version = LIBAVUTIL_VERSION_INT,
430 static const AVFilterPad avfilter_vf_pad_inputs[] = {
433 .type = AVMEDIA_TYPE_VIDEO,
434 .config_props = config_input,
435 .get_video_buffer = get_video_buffer,
436 .filter_frame = filter_frame,
441 static const AVFilterPad avfilter_vf_pad_outputs[] = {
444 .type = AVMEDIA_TYPE_VIDEO,
445 .config_props = config_output,
450 AVFilter ff_vf_pad = {
452 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
454 .priv_size = sizeof(PadContext),
455 .priv_class = &pad_class,
458 .query_formats = query_formats,
460 .inputs = avfilter_vf_pad_inputs,
462 .outputs = avfilter_vf_pad_outputs,