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