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