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