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 and color source
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 *var_names[] = {
70 static int query_formats(AVFilterContext *ctx)
72 static const enum PixelFormat pix_fmts[] = {
73 PIX_FMT_ARGB, PIX_FMT_RGBA,
74 PIX_FMT_ABGR, PIX_FMT_BGRA,
75 PIX_FMT_RGB24, PIX_FMT_BGR24,
77 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
78 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
79 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
80 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
81 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
87 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
92 int w, h; ///< output dimensions, a value of 0 will result in the input size
93 int x, y; ///< offsets of the input area with respect to the padded area
94 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
96 char w_expr[256]; ///< width expression string
97 char h_expr[256]; ///< height expression string
98 char x_expr[256]; ///< width expression string
99 char y_expr[256]; ///< height expression string
101 uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
104 int hsub, vsub; ///< chroma subsampling values
108 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
110 PadContext *pad = ctx->priv;
111 char color_string[128] = "black";
113 av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
114 av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
115 av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
116 av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
119 sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s",
120 pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
122 if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
123 return AVERROR(EINVAL);
128 static av_cold void uninit(AVFilterContext *ctx)
130 PadContext *pad = ctx->priv;
133 for (i = 0; i < 4; i++) {
134 av_freep(&pad->line[i]);
135 pad->line_step[i] = 0;
139 static int config_input(AVFilterLink *inlink)
141 AVFilterContext *ctx = inlink->dst;
142 PadContext *pad = ctx->priv;
143 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
144 uint8_t rgba_color[4];
145 int ret, is_packed_rgba;
146 double var_values[VARS_NB], res;
149 pad->hsub = pix_desc->log2_chroma_w;
150 pad->vsub = pix_desc->log2_chroma_h;
152 var_values[VAR_PI] = M_PI;
153 var_values[VAR_PHI] = M_PHI;
154 var_values[VAR_E] = M_E;
155 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
156 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
157 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
158 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
159 var_values[VAR_A] = (float) inlink->w / inlink->h;
160 var_values[VAR_HSUB] = 1<<pad->hsub;
161 var_values[VAR_VSUB] = 2<<pad->vsub;
163 /* evaluate width and height */
164 av_expr_parse_and_eval(&res, (expr = pad->w_expr),
165 var_names, var_values,
166 NULL, NULL, NULL, NULL, NULL, 0, ctx);
167 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
168 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
169 var_names, var_values,
170 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
172 pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
173 /* evaluate the width again, as it may depend on the evaluated output height */
174 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
175 var_names, var_values,
176 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
178 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
180 /* evaluate x and y */
181 av_expr_parse_and_eval(&res, (expr = pad->x_expr),
182 var_names, var_values,
183 NULL, NULL, NULL, NULL, NULL, 0, ctx);
184 pad->x = var_values[VAR_X] = res;
185 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
186 var_names, var_values,
187 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
189 pad->y = var_values[VAR_Y] = res;
190 /* evaluate x again, as it may depend on the evaluated y value */
191 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
192 var_names, var_values,
193 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
195 pad->x = var_values[VAR_X] = res;
197 /* sanity check params */
198 if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
199 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
200 return AVERROR(EINVAL);
208 pad->w &= ~((1 << pad->hsub) - 1);
209 pad->h &= ~((1 << pad->vsub) - 1);
210 pad->x &= ~((1 << pad->hsub) - 1);
211 pad->y &= ~((1 << pad->vsub) - 1);
213 pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
214 pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
216 memcpy(rgba_color, pad->color, sizeof(rgba_color));
217 ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
218 inlink->format, rgba_color, &is_packed_rgba, NULL);
220 av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
221 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
222 pad->color[0], pad->color[1], pad->color[2], pad->color[3],
223 is_packed_rgba ? "rgba" : "yuva");
225 if (pad->x < 0 || pad->y < 0 ||
226 pad->w <= 0 || pad->h <= 0 ||
227 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
228 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
229 av_log(ctx, AV_LOG_ERROR,
230 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
231 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
232 return AVERROR(EINVAL);
238 av_log(NULL, AV_LOG_ERROR,
239 "Error when evaluating the expression '%s'\n", expr);
244 static int config_output(AVFilterLink *outlink)
246 PadContext *pad = outlink->src->priv;
253 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
255 PadContext *pad = inlink->dst->priv;
257 AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
258 w + (pad->w - pad->in_w),
259 h + (pad->h - pad->in_h));
262 picref->video->w = w;
263 picref->video->h = h;
265 for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
266 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
267 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
269 picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
270 (pad->y >> vsub) * picref->linesize[plane];
276 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
278 int64_t x_in_buf, y_in_buf;
280 x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
281 + (x >> hsub) * pad ->line_step[plane]
282 + (y >> vsub) * outpicref->linesize [plane];
284 if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
286 x_in_buf /= pad->line_step[plane];
288 av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
290 y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
291 x_in_buf %= outpicref->buf->linesize[plane];
293 if( y_in_buf<<vsub >= outpicref->buf->h
294 || x_in_buf<<hsub >= outpicref->buf->w)
299 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
301 PadContext *pad = inlink->dst->priv;
302 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
305 for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
306 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
307 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
309 av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
311 if(outpicref->format != outpicref->buf->format) //unsupported currently
314 outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane]
315 + (pad->y >> vsub) * outpicref->linesize [plane];
317 if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
318 || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
319 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
320 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
324 pad->needs_copy= plane < 4 && outpicref->data[plane];
326 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
327 avfilter_unref_buffer(outpicref);
328 outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
329 FFMAX(inlink->w, pad->w),
330 FFMAX(inlink->h, pad->h));
331 avfilter_copy_buffer_ref_props(outpicref, inpicref);
334 inlink->dst->outputs[0]->out_buf = outpicref;
336 outpicref->video->w = pad->w;
337 outpicref->video->h = pad->h;
339 avfilter_start_frame(inlink->dst->outputs[0], outpicref);
342 static void end_frame(AVFilterLink *link)
344 avfilter_end_frame(link->dst->outputs[0]);
345 avfilter_unref_buffer(link->cur_buf);
348 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
350 PadContext *pad = link->dst->priv;
351 int bar_y, bar_h = 0;
353 if (slice_dir * before_slice == 1 && y == pad->y) {
357 } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
359 bar_y = pad->y + pad->in_h;
360 bar_h = pad->h - pad->in_h - pad->y;
364 ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
365 link->dst->outputs[0]->out_buf->linesize,
366 pad->line, pad->line_step, pad->hsub, pad->vsub,
367 0, bar_y, pad->w, bar_h);
368 avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
372 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
374 PadContext *pad = link->dst->priv;
375 AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
376 AVFilterBufferRef *inpic = link->cur_buf;
380 y &= ~((1 << pad->vsub) - 1);
381 h &= ~((1 << pad->vsub) - 1);
385 draw_send_bar_slice(link, y, h, slice_dir, 1);
388 ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
389 pad->hsub, pad->vsub, 0, y, pad->x, h);
392 ff_copy_rectangle(outpic->data, outpic->linesize,
393 inpic->data, inpic->linesize, pad->line_step,
394 pad->hsub, pad->vsub,
395 pad->x, y, y-pad->y, inpic->video->w, h);
399 ff_draw_rectangle(outpic->data, outpic->linesize,
400 pad->line, pad->line_step, pad->hsub, pad->vsub,
401 pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
402 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
404 draw_send_bar_slice(link, y, h, slice_dir, -1);
407 AVFilter avfilter_vf_pad = {
409 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
411 .priv_size = sizeof(PadContext),
414 .query_formats = query_formats,
416 .inputs = (AVFilterPad[]) {{ .name = "default",
417 .type = AVMEDIA_TYPE_VIDEO,
418 .config_props = config_input,
419 .get_video_buffer = get_video_buffer,
420 .start_frame = start_frame,
421 .draw_slice = draw_slice,
422 .end_frame = end_frame, },
425 .outputs = (AVFilterPad[]) {{ .name = "default",
426 .type = AVMEDIA_TYPE_VIDEO,
427 .config_props = config_output, },