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