]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_pad.c
swresample/resample: fix return value of build_filter
[ffmpeg] / libavfilter / vf_pad.c
1 /*
2  * Copyright (c) 2008 vmrsss
3  * Copyright (c) 2009 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/common.h"
33 #include "libavutil/eval.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/colorspace.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/opt.h"
40
41 #include "drawutils.h"
42
43 static const char *const var_names[] = {
44     "in_w",   "iw",
45     "in_h",   "ih",
46     "out_w",  "ow",
47     "out_h",  "oh",
48     "x",
49     "y",
50     "a",
51     "sar",
52     "dar",
53     "hsub",
54     "vsub",
55     NULL
56 };
57
58 enum var_name {
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_SAR,
67     VAR_DAR,
68     VAR_HSUB,
69     VAR_VSUB,
70     VARS_NB
71 };
72
73 static int query_formats(AVFilterContext *ctx)
74 {
75     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
76 }
77
78 typedef struct PadContext {
79     const AVClass *class;
80     int w, h;               ///< output dimensions, a value of 0 will result in the input size
81     int x, y;               ///< offsets of the input area with respect to the padded area
82     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
83
84     char *w_expr;           ///< width  expression string
85     char *h_expr;           ///< height expression string
86     char *x_expr;           ///< width  expression string
87     char *y_expr;           ///< height expression string
88     uint8_t rgba_color[4];  ///< color for the padding area
89     FFDrawContext draw;
90     FFDrawColor color;
91 } PadContext;
92
93 static int config_input(AVFilterLink *inlink)
94 {
95     AVFilterContext *ctx = inlink->dst;
96     PadContext *s = ctx->priv;
97     int ret;
98     double var_values[VARS_NB], res;
99     char *expr;
100
101     ff_draw_init(&s->draw, inlink->format, 0);
102     ff_draw_color(&s->draw, &s->color, s->rgba_color);
103
104     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
105     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
106     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
107     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
108     var_values[VAR_A]     = (double) inlink->w / inlink->h;
109     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
110         (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
111     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
112     var_values[VAR_HSUB]  = 1 << s->draw.hsub_max;
113     var_values[VAR_VSUB]  = 1 << s->draw.vsub_max;
114
115     /* evaluate width and height */
116     av_expr_parse_and_eval(&res, (expr = s->w_expr),
117                            var_names, var_values,
118                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
119     s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
120     if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
121                                       var_names, var_values,
122                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
123         goto eval_fail;
124     s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
125     if (!s->h)
126         var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h;
127
128     /* evaluate the width again, as it may depend on the evaluated output height */
129     if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
130                                       var_names, var_values,
131                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
132         goto eval_fail;
133     s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
134     if (!s->w)
135         var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w;
136
137     /* evaluate x and y */
138     av_expr_parse_and_eval(&res, (expr = s->x_expr),
139                            var_names, var_values,
140                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
141     s->x = var_values[VAR_X] = res;
142     if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
143                                       var_names, var_values,
144                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
145         goto eval_fail;
146     s->y = var_values[VAR_Y] = res;
147     /* evaluate x again, as it may depend on the evaluated y value */
148     if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
149                                       var_names, var_values,
150                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
151         goto eval_fail;
152     s->x = var_values[VAR_X] = res;
153
154     /* sanity check params */
155     if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
156         av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
157         return AVERROR(EINVAL);
158     }
159
160     s->w    = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
161     s->h    = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
162     s->x    = ff_draw_round_to_sub(&s->draw, 0, -1, s->x);
163     s->y    = ff_draw_round_to_sub(&s->draw, 1, -1, s->y);
164     s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w);
165     s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h);
166
167     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n",
168            inlink->w, inlink->h, s->w, s->h, s->x, s->y,
169            s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]);
170
171     if (s->x <  0 || s->y <  0                      ||
172         s->w <= 0 || s->h <= 0                      ||
173         (unsigned)s->x + (unsigned)inlink->w > s->w ||
174         (unsigned)s->y + (unsigned)inlink->h > s->h) {
175         av_log(ctx, AV_LOG_ERROR,
176                "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
177                s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
178         return AVERROR(EINVAL);
179     }
180
181     return 0;
182
183 eval_fail:
184     av_log(NULL, AV_LOG_ERROR,
185            "Error when evaluating the expression '%s'\n", expr);
186     return ret;
187
188 }
189
190 static int config_output(AVFilterLink *outlink)
191 {
192     PadContext *s = outlink->src->priv;
193
194     outlink->w = s->w;
195     outlink->h = s->h;
196     return 0;
197 }
198
199 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
200 {
201     PadContext *s = inlink->dst->priv;
202
203     AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
204                                          w + (s->w - s->in_w),
205                                          h + (s->h - s->in_h) + (s->x > 0));
206     int plane;
207
208     if (!frame)
209         return NULL;
210
211     frame->width  = w;
212     frame->height = h;
213
214     for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
215         int hsub = s->draw.hsub[plane];
216         int vsub = s->draw.vsub[plane];
217         frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] +
218                               (s->y >> vsub) * frame->linesize[plane];
219     }
220
221     return frame;
222 }
223
224 /* check whether each plane in this buffer can be padded without copying */
225 static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
226 {
227     int planes[4] = { -1, -1, -1, -1}, *p = planes;
228     int i, j;
229
230     /* get all planes in this buffer */
231     for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
232         if (av_frame_get_plane_buffer(frame, i) == buf)
233             *p++ = i;
234     }
235
236     /* for each plane in this buffer, check that it can be padded without
237      * going over buffer bounds or other planes */
238     for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
239         int hsub = s->draw.hsub[planes[i]];
240         int vsub = s->draw.vsub[planes[i]];
241
242         uint8_t *start = frame->data[planes[i]];
243         uint8_t *end   = start + (frame->height >> vsub) *
244                                  frame->linesize[planes[i]];
245
246         /* amount of free space needed before the start and after the end
247          * of the plane */
248         ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
249                               (s->y >> vsub) * frame->linesize[planes[i]];
250         ptrdiff_t req_end   = ((s->w - s->x - frame->width) >> hsub) *
251                               s->draw.pixelstep[planes[i]] +
252                               ((s->h - s->y - frame->height) >> vsub) * frame->linesize[planes[i]];
253
254         if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
255             return 1;
256         if (start - buf->data < req_start ||
257             (buf->data + buf->size) - end < req_end)
258             return 1;
259
260         for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
261             int vsub1 = s->draw.vsub[planes[j]];
262             uint8_t *start1 = frame->data[planes[j]];
263             uint8_t *end1   = start1 + (frame->height >> vsub1) *
264                                        frame->linesize[planes[j]];
265             if (i == j)
266                 continue;
267
268             if (FFSIGN(start - end1) != FFSIGN(start - end1 - req_start) ||
269                 FFSIGN(end - start1) != FFSIGN(end - start1 + req_end))
270                 return 1;
271         }
272     }
273
274     return 0;
275 }
276
277 static int frame_needs_copy(PadContext *s, AVFrame *frame)
278 {
279     int i;
280
281     if (!av_frame_is_writable(frame))
282         return 1;
283
284     for (i = 0; i < 4 && frame->buf[i]; i++)
285         if (buffer_needs_copy(s, frame, frame->buf[i]))
286             return 1;
287     return 0;
288 }
289
290 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
291 {
292     PadContext *s = inlink->dst->priv;
293     AVFrame *out;
294     int needs_copy = frame_needs_copy(s, in);
295
296     if (needs_copy) {
297         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
298         out = ff_get_video_buffer(inlink->dst->outputs[0],
299                                   FFMAX(inlink->w, s->w),
300                                   FFMAX(inlink->h, s->h));
301         if (!out) {
302             av_frame_free(&in);
303             return AVERROR(ENOMEM);
304         }
305
306         av_frame_copy_props(out, in);
307     } else {
308         int i;
309
310         out = in;
311         for (i = 0; i < 4 && out->data[i] && out->linesize[i]; i++) {
312             int hsub = s->draw.hsub[i];
313             int vsub = s->draw.vsub[i];
314             out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] +
315                             (s->y >> vsub) * out->linesize[i];
316         }
317     }
318
319     /* top bar */
320     if (s->y) {
321         ff_fill_rectangle(&s->draw, &s->color,
322                           out->data, out->linesize,
323                           0, 0, s->w, s->y);
324     }
325
326     /* bottom bar */
327     if (s->h > s->y + s->in_h) {
328         ff_fill_rectangle(&s->draw, &s->color,
329                           out->data, out->linesize,
330                           0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
331     }
332
333     /* left border */
334     ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
335                       0, s->y, s->x, in->height);
336
337     if (needs_copy) {
338         ff_copy_rectangle2(&s->draw,
339                           out->data, out->linesize, in->data, in->linesize,
340                           s->x, s->y, 0, 0, in->width, in->height);
341     }
342
343     /* right border */
344     ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
345                       s->x + s->in_w, s->y, s->w - s->x - s->in_w,
346                       in->height);
347
348     out->width  = s->w;
349     out->height = s->h;
350
351     if (in != out)
352         av_frame_free(&in);
353     return ff_filter_frame(inlink->dst->outputs[0], out);
354 }
355
356 #define OFFSET(x) offsetof(PadContext, x)
357 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
358
359 static const AVOption pad_options[] = {
360     { "width",  "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
361     { "w",      "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
362     { "height", "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
363     { "h",      "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
364     { "x",      "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
365     { "y",      "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
366     { "color",  "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
367     { NULL }
368 };
369
370 AVFILTER_DEFINE_CLASS(pad);
371
372 static const AVFilterPad avfilter_vf_pad_inputs[] = {
373     {
374         .name             = "default",
375         .type             = AVMEDIA_TYPE_VIDEO,
376         .config_props     = config_input,
377         .get_video_buffer = get_video_buffer,
378         .filter_frame     = filter_frame,
379     },
380     { NULL }
381 };
382
383 static const AVFilterPad avfilter_vf_pad_outputs[] = {
384     {
385         .name         = "default",
386         .type         = AVMEDIA_TYPE_VIDEO,
387         .config_props = config_output,
388     },
389     { NULL }
390 };
391
392 AVFilter ff_vf_pad = {
393     .name          = "pad",
394     .description   = NULL_IF_CONFIG_SMALL("Pad the input video."),
395     .priv_size     = sizeof(PadContext),
396     .priv_class    = &pad_class,
397     .query_formats = query_formats,
398     .inputs        = avfilter_vf_pad_inputs,
399     .outputs       = avfilter_vf_pad_outputs,
400 };