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