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