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