]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_pad.c
lavfi: make formats API private on next bump.
[ffmpeg] / libavfilter / vf_pad.c
1 /*
2  * Copyright (c) 2008 vmrsss
3  * Copyright (c) 2009 Stefano Sabatini
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * video padding filter
25  */
26
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/colorspace.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/parseutils.h"
36 #include "libavutil/mathematics.h"
37 #include "drawutils.h"
38
39 static const char *const var_names[] = {
40     "PI",
41     "PHI",
42     "E",
43     "in_w",   "iw",
44     "in_h",   "ih",
45     "out_w",  "ow",
46     "out_h",  "oh",
47     "x",
48     "y",
49     "a",
50     "hsub",
51     "vsub",
52     NULL
53 };
54
55 enum var_name {
56     VAR_PI,
57     VAR_PHI,
58     VAR_E,
59     VAR_IN_W,   VAR_IW,
60     VAR_IN_H,   VAR_IH,
61     VAR_OUT_W,  VAR_OW,
62     VAR_OUT_H,  VAR_OH,
63     VAR_X,
64     VAR_Y,
65     VAR_A,
66     VAR_HSUB,
67     VAR_VSUB,
68     VARS_NB
69 };
70
71 static int query_formats(AVFilterContext *ctx)
72 {
73     static const enum PixelFormat pix_fmts[] = {
74         PIX_FMT_ARGB,         PIX_FMT_RGBA,
75         PIX_FMT_ABGR,         PIX_FMT_BGRA,
76         PIX_FMT_RGB24,        PIX_FMT_BGR24,
77
78         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
79         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
80         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
81         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
82         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
83         PIX_FMT_YUVA420P,
84
85         PIX_FMT_NONE
86     };
87
88     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
89     return 0;
90 }
91
92 typedef struct {
93     int w, h;               ///< output dimensions, a value of 0 will result in the input size
94     int x, y;               ///< offsets of the input area with respect to the padded area
95     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
97     char w_expr[256];       ///< width  expression string
98     char h_expr[256];       ///< height expression string
99     char x_expr[256];       ///< width  expression string
100     char y_expr[256];       ///< height expression string
101
102     uint8_t color[4];       ///< color expressed either in YUVA or RGBA colorspace for the padding area
103     uint8_t *line[4];
104     int      line_step[4];
105     int hsub, vsub;         ///< chroma subsampling values
106     int needs_copy;
107 } PadContext;
108
109 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
110 {
111     PadContext *pad = ctx->priv;
112     char color_string[128] = "black";
113
114     av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
115     av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
116     av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
117     av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
118
119     if (args)
120         sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s",
121                pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
122
123     if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
124         return AVERROR(EINVAL);
125
126     return 0;
127 }
128
129 static av_cold void uninit(AVFilterContext *ctx)
130 {
131     PadContext *pad = ctx->priv;
132     int i;
133
134     for (i = 0; i < 4; i++) {
135         av_freep(&pad->line[i]);
136         pad->line_step[i] = 0;
137     }
138 }
139
140 static int config_input(AVFilterLink *inlink)
141 {
142     AVFilterContext *ctx = inlink->dst;
143     PadContext *pad = ctx->priv;
144     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
145     uint8_t rgba_color[4];
146     int ret, is_packed_rgba;
147     double var_values[VARS_NB], res;
148     char *expr;
149
150     pad->hsub = pix_desc->log2_chroma_w;
151     pad->vsub = pix_desc->log2_chroma_h;
152
153     var_values[VAR_PI]    = M_PI;
154     var_values[VAR_PHI]   = M_PHI;
155     var_values[VAR_E]     = M_E;
156     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
157     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
158     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
159     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
160     var_values[VAR_A]     = (float) inlink->w / inlink->h;
161     var_values[VAR_HSUB]  = 1<<pad->hsub;
162     var_values[VAR_VSUB]  = 1<<pad->vsub;
163
164     /* evaluate width and height */
165     av_expr_parse_and_eval(&res, (expr = pad->w_expr),
166                            var_names, var_values,
167                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
168     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
169     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
170                                       var_names, var_values,
171                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
172         goto eval_fail;
173     pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
174     /* evaluate the width again, as it may depend on the evaluated output height */
175     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
176                                       var_names, var_values,
177                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
178         goto eval_fail;
179     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
180
181     /* evaluate x and y */
182     av_expr_parse_and_eval(&res, (expr = pad->x_expr),
183                            var_names, var_values,
184                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
185     pad->x = var_values[VAR_X] = res;
186     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
187                                       var_names, var_values,
188                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
189         goto eval_fail;
190     pad->y = var_values[VAR_Y] = res;
191     /* evaluate x again, as it may depend on the evaluated y value */
192     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
193                                       var_names, var_values,
194                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
195         goto eval_fail;
196     pad->x = var_values[VAR_X] = res;
197
198     /* sanity check params */
199     if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
200         av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
201         return AVERROR(EINVAL);
202     }
203
204     if (!pad->w)
205         pad->w = inlink->w;
206     if (!pad->h)
207         pad->h = inlink->h;
208
209     pad->w &= ~((1 << pad->hsub) - 1);
210     pad->h &= ~((1 << pad->vsub) - 1);
211     pad->x &= ~((1 << pad->hsub) - 1);
212     pad->y &= ~((1 << pad->vsub) - 1);
213
214     pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
215     pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
216
217     memcpy(rgba_color, pad->color, sizeof(rgba_color));
218     ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
219                             inlink->format, rgba_color, &is_packed_rgba, NULL);
220
221     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",
222            inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
223            pad->color[0], pad->color[1], pad->color[2], pad->color[3],
224            is_packed_rgba ? "rgba" : "yuva");
225
226     if (pad->x <  0 || pad->y <  0                      ||
227         pad->w <= 0 || pad->h <= 0                      ||
228         (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
229         (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
230         av_log(ctx, AV_LOG_ERROR,
231                "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
232                pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
233         return AVERROR(EINVAL);
234     }
235
236     return 0;
237
238 eval_fail:
239     av_log(NULL, AV_LOG_ERROR,
240            "Error when evaluating the expression '%s'\n", expr);
241     return ret;
242
243 }
244
245 static int config_output(AVFilterLink *outlink)
246 {
247     PadContext *pad = outlink->src->priv;
248
249     outlink->w = pad->w;
250     outlink->h = pad->h;
251     return 0;
252 }
253
254 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
255 {
256     PadContext *pad = inlink->dst->priv;
257
258     AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
259                                                        w + (pad->w - pad->in_w),
260                                                        h + (pad->h - pad->in_h));
261     int plane;
262
263     picref->video->w = w;
264     picref->video->h = h;
265
266     for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
267         int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
268         int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
269
270         picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
271             (pad->y >> vsub) * picref->linesize[plane];
272     }
273
274     return picref;
275 }
276
277 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
278 {
279     int64_t x_in_buf, y_in_buf;
280
281     x_in_buf =  outpicref->data[plane] - outpicref->buf->data[plane]
282              +  (x >> hsub) * pad      ->line_step[plane]
283              +  (y >> vsub) * outpicref->linesize [plane];
284
285     if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
286         return 1;
287     x_in_buf /= pad->line_step[plane];
288
289     av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
290
291     y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
292     x_in_buf %= outpicref->buf->linesize[plane];
293
294     if(   y_in_buf<<vsub >= outpicref->buf->h
295        || x_in_buf<<hsub >= outpicref->buf->w)
296         return 1;
297     return 0;
298 }
299
300 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
301 {
302     PadContext *pad = inlink->dst->priv;
303     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
304     int plane;
305
306     for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
307         int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
308         int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
309
310         av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
311
312         if(outpicref->format != outpicref->buf->format) //unsupported currently
313             break;
314
315         outpicref->data[plane] -=   (pad->x  >> hsub) * pad      ->line_step[plane]
316                                   + (pad->y  >> vsub) * outpicref->linesize [plane];
317
318         if(   does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
319            || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
320            || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
321            || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
322           )
323             break;
324     }
325     pad->needs_copy= plane < 4 && outpicref->data[plane];
326     if(pad->needs_copy){
327         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
328         avfilter_unref_buffer(outpicref);
329         outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
330                                                        FFMAX(inlink->w, pad->w),
331                                                        FFMAX(inlink->h, pad->h));
332         avfilter_copy_buffer_ref_props(outpicref, inpicref);
333     }
334
335     inlink->dst->outputs[0]->out_buf = outpicref;
336
337     outpicref->video->w = pad->w;
338     outpicref->video->h = pad->h;
339
340     avfilter_start_frame(inlink->dst->outputs[0], outpicref);
341 }
342
343 static void end_frame(AVFilterLink *link)
344 {
345     avfilter_end_frame(link->dst->outputs[0]);
346     avfilter_unref_buffer(link->cur_buf);
347 }
348
349 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
350 {
351     PadContext *pad = link->dst->priv;
352     int bar_y, bar_h = 0;
353
354     if        (slice_dir * before_slice ==  1 && y == pad->y) {
355         /* top bar */
356         bar_y = 0;
357         bar_h = pad->y;
358     } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
359         /* bottom bar */
360         bar_y = pad->y + pad->in_h;
361         bar_h = pad->h - pad->in_h - pad->y;
362     }
363
364     if (bar_h) {
365         ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
366                           link->dst->outputs[0]->out_buf->linesize,
367                           pad->line, pad->line_step, pad->hsub, pad->vsub,
368                           0, bar_y, pad->w, bar_h);
369         avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
370     }
371 }
372
373 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
374 {
375     PadContext *pad = link->dst->priv;
376     AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
377     AVFilterBufferRef *inpic = link->cur_buf;
378
379     y += pad->y;
380
381     y &= ~((1 << pad->vsub) - 1);
382     h &= ~((1 << pad->vsub) - 1);
383
384     if (!h)
385         return;
386     draw_send_bar_slice(link, y, h, slice_dir, 1);
387
388     /* left border */
389     ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
390                       pad->hsub, pad->vsub, 0, y, pad->x, h);
391
392     if(pad->needs_copy){
393         ff_copy_rectangle(outpic->data, outpic->linesize,
394                           inpic->data, inpic->linesize, pad->line_step,
395                           pad->hsub, pad->vsub,
396                           pad->x, y, y-pad->y, inpic->video->w, h);
397     }
398
399     /* right border */
400     ff_draw_rectangle(outpic->data, outpic->linesize,
401                       pad->line, pad->line_step, pad->hsub, pad->vsub,
402                       pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
403     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
404
405     draw_send_bar_slice(link, y, h, slice_dir, -1);
406 }
407
408 AVFilter avfilter_vf_pad = {
409     .name          = "pad",
410     .description   = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
411
412     .priv_size     = sizeof(PadContext),
413     .init          = init,
414     .uninit        = uninit,
415     .query_formats = query_formats,
416
417     .inputs    = (AVFilterPad[]) {{ .name             = "default",
418                                     .type             = AVMEDIA_TYPE_VIDEO,
419                                     .config_props     = config_input,
420                                     .get_video_buffer = get_video_buffer,
421                                     .start_frame      = start_frame,
422                                     .draw_slice       = draw_slice,
423                                     .end_frame        = end_frame, },
424                                   { .name = NULL}},
425
426     .outputs   = (AVFilterPad[]) {{ .name             = "default",
427                                     .type             = AVMEDIA_TYPE_VIDEO,
428                                     .config_props     = config_output, },
429                                   { .name = NULL}},
430 };