]> 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 *var_names[] = {
39     "PI",
40     "PHI",
41     "E",
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_PI,
58     VAR_PHI,
59     VAR_E,
60     VAR_IN_W,   VAR_IW,
61     VAR_IN_H,   VAR_IH,
62     VAR_OUT_W,  VAR_OW,
63     VAR_OUT_H,  VAR_OH,
64     VAR_X,
65     VAR_Y,
66     VAR_A,
67     VAR_SAR,
68     VAR_DAR,
69     VAR_HSUB,
70     VAR_VSUB,
71     VARS_NB
72 };
73
74 static int query_formats(AVFilterContext *ctx)
75 {
76     static const enum PixelFormat pix_fmts[] = {
77         PIX_FMT_ARGB,         PIX_FMT_RGBA,
78         PIX_FMT_ABGR,         PIX_FMT_BGRA,
79         PIX_FMT_RGB24,        PIX_FMT_BGR24,
80
81         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
82         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
83         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
84         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
85         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
86         PIX_FMT_YUVA420P,
87
88         PIX_FMT_NONE
89     };
90
91     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
92     return 0;
93 }
94
95 typedef struct {
96     int w, h;               ///< output dimensions, a value of 0 will result in the input size
97     int x, y;               ///< offsets of the input area with respect to the padded area
98     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
99
100     char w_expr[256];       ///< width  expression string
101     char h_expr[256];       ///< height expression string
102     char x_expr[256];       ///< width  expression string
103     char y_expr[256];       ///< height expression string
104
105     uint8_t color[4];       ///< color expressed either in YUVA or RGBA colorspace for the padding area
106     uint8_t *line[4];
107     int      line_step[4];
108     int hsub, vsub;         ///< chroma subsampling values
109     int needs_copy;
110 } PadContext;
111
112 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
113 {
114     PadContext *pad = ctx->priv;
115     char color_string[128] = "black";
116
117     av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
118     av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
119     av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
120     av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
121
122     if (args)
123         sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s",
124                pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
125
126     if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
127         return AVERROR(EINVAL);
128
129     return 0;
130 }
131
132 static av_cold void uninit(AVFilterContext *ctx)
133 {
134     PadContext *pad = ctx->priv;
135     int i;
136
137     for (i = 0; i < 4; i++) {
138         av_freep(&pad->line[i]);
139         pad->line_step[i] = 0;
140     }
141 }
142
143 static int config_input(AVFilterLink *inlink)
144 {
145     AVFilterContext *ctx = inlink->dst;
146     PadContext *pad = ctx->priv;
147     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
148     uint8_t rgba_color[4];
149     int ret, is_packed_rgba;
150     double var_values[VARS_NB], res;
151     char *expr;
152
153     pad->hsub = pix_desc->log2_chroma_w;
154     pad->vsub = pix_desc->log2_chroma_h;
155
156     var_values[VAR_PI]    = M_PI;
157     var_values[VAR_PHI]   = M_PHI;
158     var_values[VAR_E]     = M_E;
159     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
160     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
161     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
162     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
163     var_values[VAR_A]     = (float) inlink->w / inlink->h;
164     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
165         (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
166     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
167     var_values[VAR_HSUB]  = 1<<pad->hsub;
168     var_values[VAR_VSUB]  = 1<<pad->vsub;
169
170     /* evaluate width and height */
171     av_expr_parse_and_eval(&res, (expr = pad->w_expr),
172                            var_names, var_values,
173                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
174     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
175     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
176                                       var_names, var_values,
177                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
178         goto eval_fail;
179     pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
180     /* evaluate the width again, as it may depend on the evaluated output height */
181     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
182                                       var_names, var_values,
183                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
184         goto eval_fail;
185     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
186
187     /* evaluate x and y */
188     av_expr_parse_and_eval(&res, (expr = pad->x_expr),
189                            var_names, var_values,
190                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
191     pad->x = var_values[VAR_X] = res;
192     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
193                                       var_names, var_values,
194                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
195         goto eval_fail;
196     pad->y = var_values[VAR_Y] = res;
197     /* evaluate x again, as it may depend on the evaluated y value */
198     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
199                                       var_names, var_values,
200                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
201         goto eval_fail;
202     pad->x = var_values[VAR_X] = res;
203
204     /* sanity check params */
205     if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
206         av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
207         return AVERROR(EINVAL);
208     }
209
210     if (!pad->w)
211         pad->w = inlink->w;
212     if (!pad->h)
213         pad->h = inlink->h;
214
215     pad->w &= ~((1 << pad->hsub) - 1);
216     pad->h &= ~((1 << pad->vsub) - 1);
217     pad->x &= ~((1 << pad->hsub) - 1);
218     pad->y &= ~((1 << pad->vsub) - 1);
219
220     pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
221     pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
222
223     memcpy(rgba_color, pad->color, sizeof(rgba_color));
224     ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
225                             inlink->format, rgba_color, &is_packed_rgba, NULL);
226
227     av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
228            inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
229            pad->color[0], pad->color[1], pad->color[2], pad->color[3],
230            is_packed_rgba ? "rgba" : "yuva");
231
232     if (pad->x <  0 || pad->y <  0                      ||
233         pad->w <= 0 || pad->h <= 0                      ||
234         (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
235         (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
236         av_log(ctx, AV_LOG_ERROR,
237                "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
238                pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
239         return AVERROR(EINVAL);
240     }
241
242     return 0;
243
244 eval_fail:
245     av_log(NULL, AV_LOG_ERROR,
246            "Error when evaluating the expression '%s'\n", expr);
247     return ret;
248
249 }
250
251 static int config_output(AVFilterLink *outlink)
252 {
253     PadContext *pad = outlink->src->priv;
254
255     outlink->w = pad->w;
256     outlink->h = pad->h;
257     return 0;
258 }
259
260 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
261 {
262     PadContext *pad = inlink->dst->priv;
263
264     AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
265                                                        w + (pad->w - pad->in_w),
266                                                        h + (pad->h - pad->in_h));
267     int plane;
268
269     picref->video->w = w;
270     picref->video->h = h;
271
272     for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
273         int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
274         int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
275
276         picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
277             (pad->y >> vsub) * picref->linesize[plane];
278     }
279
280     return picref;
281 }
282
283 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
284 {
285     int64_t x_in_buf, y_in_buf;
286
287     x_in_buf =  outpicref->data[plane] - outpicref->buf->data[plane]
288              +  (x >> hsub) * pad      ->line_step[plane]
289              +  (y >> vsub) * outpicref->linesize [plane];
290
291     if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
292         return 1;
293     x_in_buf /= pad->line_step[plane];
294
295     av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
296
297     y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
298     x_in_buf %= outpicref->buf->linesize[plane];
299
300     if(   y_in_buf<<vsub >= outpicref->buf->h
301        || x_in_buf<<hsub >= outpicref->buf->w)
302         return 1;
303     return 0;
304 }
305
306 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
307 {
308     PadContext *pad = inlink->dst->priv;
309     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
310     int plane;
311
312     for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
313         int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
314         int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
315
316         av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
317
318         if(outpicref->format != outpicref->buf->format) //unsupported currently
319             break;
320
321         outpicref->data[plane] -=   (pad->x  >> hsub) * pad      ->line_step[plane]
322                                   + (pad->y  >> vsub) * outpicref->linesize [plane];
323
324         if(   does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
325            || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
326            || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
327            || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
328           )
329             break;
330     }
331     pad->needs_copy= plane < 4 && outpicref->data[plane];
332     if(pad->needs_copy){
333         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
334         avfilter_unref_buffer(outpicref);
335         outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
336                                                        FFMAX(inlink->w, pad->w),
337                                                        FFMAX(inlink->h, pad->h));
338         avfilter_copy_buffer_ref_props(outpicref, inpicref);
339     }
340
341     inlink->dst->outputs[0]->out_buf = outpicref;
342
343     outpicref->video->w = pad->w;
344     outpicref->video->h = pad->h;
345
346     avfilter_start_frame(inlink->dst->outputs[0], outpicref);
347 }
348
349 static void end_frame(AVFilterLink *link)
350 {
351     avfilter_end_frame(link->dst->outputs[0]);
352     avfilter_unref_buffer(link->cur_buf);
353 }
354
355 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
356 {
357     PadContext *pad = link->dst->priv;
358     int bar_y, bar_h = 0;
359
360     if        (slice_dir * before_slice ==  1 && y == pad->y) {
361         /* top bar */
362         bar_y = 0;
363         bar_h = pad->y;
364     } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
365         /* bottom bar */
366         bar_y = pad->y + pad->in_h;
367         bar_h = pad->h - pad->in_h - pad->y;
368     }
369
370     if (bar_h) {
371         ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
372                           link->dst->outputs[0]->out_buf->linesize,
373                           pad->line, pad->line_step, pad->hsub, pad->vsub,
374                           0, bar_y, pad->w, bar_h);
375         avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
376     }
377 }
378
379 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
380 {
381     PadContext *pad = link->dst->priv;
382     AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
383     AVFilterBufferRef *inpic = link->cur_buf;
384
385     y += pad->y;
386
387     y &= ~((1 << pad->vsub) - 1);
388     h &= ~((1 << pad->vsub) - 1);
389
390     if (!h)
391         return;
392     draw_send_bar_slice(link, y, h, slice_dir, 1);
393
394     /* left border */
395     ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
396                       pad->hsub, pad->vsub, 0, y, pad->x, h);
397
398     if(pad->needs_copy){
399         ff_copy_rectangle(outpic->data, outpic->linesize,
400                           inpic->data, inpic->linesize, pad->line_step,
401                           pad->hsub, pad->vsub,
402                           pad->x, y, y-pad->y, inpic->video->w, h);
403     }
404
405     /* right border */
406     ff_draw_rectangle(outpic->data, outpic->linesize,
407                       pad->line, pad->line_step, pad->hsub, pad->vsub,
408                       pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
409     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
410
411     draw_send_bar_slice(link, y, h, slice_dir, -1);
412 }
413
414 AVFilter avfilter_vf_pad = {
415     .name          = "pad",
416     .description   = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
417
418     .priv_size     = sizeof(PadContext),
419     .init          = init,
420     .uninit        = uninit,
421     .query_formats = query_formats,
422
423     .inputs    = (AVFilterPad[]) {{ .name             = "default",
424                                     .type             = AVMEDIA_TYPE_VIDEO,
425                                     .config_props     = config_input,
426                                     .get_video_buffer = get_video_buffer,
427                                     .start_frame      = start_frame,
428                                     .draw_slice       = draw_slice,
429                                     .end_frame        = end_frame, },
430                                   { .name = NULL}},
431
432     .outputs   = (AVFilterPad[]) {{ .name             = "default",
433                                     .type             = AVMEDIA_TYPE_VIDEO,
434                                     .config_props     = config_output, },
435                                   { .name = NULL}},
436 };