]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_pad.c
AVFrame: deprecate all now unused fields
[ffmpeg] / libavfilter / vf_pad.c
1 /*
2  * Copyright (c) 2008 vmrsss
3  * Copyright (c) 2009 Stefano Sabatini
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/common.h"
33 #include "libavutil/eval.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/colorspace.h"
36 #include "libavutil/avassert.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/parseutils.h"
39 #include "libavutil/mathematics.h"
40 #include "drawutils.h"
41
42 static const char *const var_names[] = {
43     "PI",
44     "PHI",
45     "E",
46     "in_w",   "iw",
47     "in_h",   "ih",
48     "out_w",  "ow",
49     "out_h",  "oh",
50     "x",
51     "y",
52     "a",
53     "hsub",
54     "vsub",
55     NULL
56 };
57
58 enum var_name {
59     VAR_PI,
60     VAR_PHI,
61     VAR_E,
62     VAR_IN_W,   VAR_IW,
63     VAR_IN_H,   VAR_IH,
64     VAR_OUT_W,  VAR_OW,
65     VAR_OUT_H,  VAR_OH,
66     VAR_X,
67     VAR_Y,
68     VAR_A,
69     VAR_HSUB,
70     VAR_VSUB,
71     VARS_NB
72 };
73
74 static int query_formats(AVFilterContext *ctx)
75 {
76     static const enum AVPixelFormat pix_fmts[] = {
77         AV_PIX_FMT_ARGB,         AV_PIX_FMT_RGBA,
78         AV_PIX_FMT_ABGR,         AV_PIX_FMT_BGRA,
79         AV_PIX_FMT_RGB24,        AV_PIX_FMT_BGR24,
80
81         AV_PIX_FMT_YUV444P,      AV_PIX_FMT_YUV422P,
82         AV_PIX_FMT_YUV420P,      AV_PIX_FMT_YUV411P,
83         AV_PIX_FMT_YUV410P,      AV_PIX_FMT_YUV440P,
84         AV_PIX_FMT_YUVJ444P,     AV_PIX_FMT_YUVJ422P,
85         AV_PIX_FMT_YUVJ420P,     AV_PIX_FMT_YUVJ440P,
86         AV_PIX_FMT_YUVA420P,
87
88         AV_PIX_FMT_NONE
89     };
90
91     ff_set_common_formats(ctx, ff_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 } PadContext;
110
111 static av_cold int init(AVFilterContext *ctx, const char *args)
112 {
113     PadContext *pad = ctx->priv;
114     char color_string[128] = "black";
115
116     av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
117     av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
118     av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
119     av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
120
121     if (args)
122         sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s",
123                pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
124
125     if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
126         return AVERROR(EINVAL);
127
128     return 0;
129 }
130
131 static av_cold void uninit(AVFilterContext *ctx)
132 {
133     PadContext *pad = ctx->priv;
134     int i;
135
136     for (i = 0; i < 4; i++) {
137         av_freep(&pad->line[i]);
138         pad->line_step[i] = 0;
139     }
140 }
141
142 static int config_input(AVFilterLink *inlink)
143 {
144     AVFilterContext *ctx = inlink->dst;
145     PadContext *pad = ctx->priv;
146     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
147     uint8_t rgba_color[4];
148     int ret, is_packed_rgba;
149     double var_values[VARS_NB], res;
150     char *expr;
151
152     pad->hsub = pix_desc->log2_chroma_w;
153     pad->vsub = pix_desc->log2_chroma_h;
154
155     var_values[VAR_PI]    = M_PI;
156     var_values[VAR_PHI]   = M_PHI;
157     var_values[VAR_E]     = M_E;
158     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
159     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
160     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
161     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
162     var_values[VAR_A]     = (double) inlink->w / inlink->h;
163     var_values[VAR_HSUB]  = 1<<pad->hsub;
164     var_values[VAR_VSUB]  = 1<<pad->vsub;
165
166     /* evaluate width and height */
167     av_expr_parse_and_eval(&res, (expr = pad->w_expr),
168                            var_names, var_values,
169                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
170     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
171     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
172                                       var_names, var_values,
173                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
174         goto eval_fail;
175     pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
176     /* evaluate the width again, as it may depend on the evaluated output height */
177     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
178                                       var_names, var_values,
179                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
180         goto eval_fail;
181     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
182
183     /* evaluate x and y */
184     av_expr_parse_and_eval(&res, (expr = pad->x_expr),
185                            var_names, var_values,
186                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
187     pad->x = var_values[VAR_X] = res;
188     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
189                                       var_names, var_values,
190                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
191         goto eval_fail;
192     pad->y = var_values[VAR_Y] = res;
193     /* evaluate x again, as it may depend on the evaluated y value */
194     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
195                                       var_names, var_values,
196                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
197         goto eval_fail;
198     pad->x = var_values[VAR_X] = res;
199
200     /* sanity check params */
201     if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
202         av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
203         return AVERROR(EINVAL);
204     }
205
206     if (!pad->w)
207         pad->w = inlink->w;
208     if (!pad->h)
209         pad->h = inlink->h;
210
211     pad->w &= ~((1 << pad->hsub) - 1);
212     pad->h &= ~((1 << pad->vsub) - 1);
213     pad->x &= ~((1 << pad->hsub) - 1);
214     pad->y &= ~((1 << pad->vsub) - 1);
215
216     pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
217     pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
218
219     memcpy(rgba_color, pad->color, sizeof(rgba_color));
220     ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
221                             inlink->format, rgba_color, &is_packed_rgba, NULL);
222
223     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
224            inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
225            pad->color[0], pad->color[1], pad->color[2], pad->color[3],
226            is_packed_rgba ? "rgba" : "yuva");
227
228     if (pad->x <  0 || pad->y <  0                      ||
229         pad->w <= 0 || pad->h <= 0                      ||
230         (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
231         (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
232         av_log(ctx, AV_LOG_ERROR,
233                "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
234                pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
235         return AVERROR(EINVAL);
236     }
237
238     return 0;
239
240 eval_fail:
241     av_log(NULL, AV_LOG_ERROR,
242            "Error when evaluating the expression '%s'\n", expr);
243     return ret;
244
245 }
246
247 static int config_output(AVFilterLink *outlink)
248 {
249     PadContext *pad = outlink->src->priv;
250
251     outlink->w = pad->w;
252     outlink->h = pad->h;
253     return 0;
254 }
255
256 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
257 {
258     PadContext *pad = inlink->dst->priv;
259
260     AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
261                                          w + (pad->w - pad->in_w),
262                                          h + (pad->h - pad->in_h));
263     int plane;
264
265     if (!frame)
266         return NULL;
267
268     frame->width  = w;
269     frame->height = h;
270
271     for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
272         int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
273         int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
274
275         frame->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
276             (pad->y >> vsub) * frame->linesize[plane];
277     }
278
279     return frame;
280 }
281
282 /* check whether each plane in this buffer can be padded without copying */
283 static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
284 {
285     int planes[4] = { -1, -1, -1, -1}, *p = planes;
286     int i, j;
287
288     /* get all planes in this buffer */
289     for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
290         if (av_frame_get_plane_buffer(frame, i) == buf)
291             *p++ = i;
292     }
293
294     /* for each plane in this buffer, check that it can be padded without
295      * going over buffer bounds or other planes */
296     for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
297         int hsub = (planes[i] == 1 || planes[i] == 2) ? s->hsub : 0;
298         int vsub = (planes[i] == 1 || planes[i] == 2) ? s->vsub : 0;
299
300         uint8_t *start = frame->data[planes[i]];
301         uint8_t *end   = start + (frame->height >> hsub) *
302                                  frame->linesize[planes[i]];
303
304         /* amount of free space needed before the start and after the end
305          * of the plane */
306         ptrdiff_t req_start = (s->x >> hsub) * s->line_step[planes[i]] +
307                               (s->y >> vsub) * frame->linesize[planes[i]];
308         ptrdiff_t req_end   = ((s->w - s->x - frame->width) >> hsub) *
309                               s->line_step[planes[i]] +
310                               (s->y >> vsub) * frame->linesize[planes[i]];
311
312         if (frame->linesize[planes[i]] < (s->w >> hsub) * s->line_step[planes[i]])
313             return 1;
314         if (start - buf->data < req_start ||
315             (buf->data + buf->size) - end < req_end)
316             return 1;
317
318 #define SIGN(x) ((x) > 0 ? 1 : -1)
319         for (j = 0; j < FF_ARRAY_ELEMS(planes) & planes[j] >= 0; j++) {
320             int hsub1 = (planes[j] == 1 || planes[j] == 2) ? s->hsub : 0;
321             uint8_t *start1 = frame->data[planes[j]];
322             uint8_t *end1   = start1 + (frame->height >> hsub1) *
323                                        frame->linesize[planes[j]];
324             if (i == j)
325                 continue;
326
327             if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
328                 SIGN(end - start1) != SIGN(end - start1 + req_end))
329                 return 1;
330         }
331     }
332
333     return 0;
334 }
335
336 static int frame_needs_copy(PadContext *s, AVFrame *frame)
337 {
338     int i;
339
340     if (!av_frame_is_writable(frame))
341         return 1;
342
343     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
344         if (buffer_needs_copy(s, frame, frame->buf[i]))
345             return 1;
346     return 0;
347 }
348
349 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
350 {
351     PadContext *pad = inlink->dst->priv;
352     AVFrame *out;
353     int needs_copy = frame_needs_copy(pad, in);
354
355     if (needs_copy) {
356         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
357         out = ff_get_video_buffer(inlink->dst->outputs[0],
358                                   FFMAX(inlink->w, pad->w),
359                                   FFMAX(inlink->h, pad->h));
360         if (!out) {
361             av_frame_free(&in);
362             return AVERROR(ENOMEM);
363         }
364
365         av_frame_copy_props(out, in);
366     } else {
367         int i;
368
369         out = in;
370         for (i = 0; i < FF_ARRAY_ELEMS(out->data) && out->data[i]; i++) {
371             int hsub = (i == 1 || i == 2) ? pad->hsub : 0;
372             int vsub = (i == 1 || i == 2) ? pad->vsub : 0;
373             out->data[i] -= (pad->x >> hsub) * pad->line_step[i] +
374                             (pad->y >> vsub) * out->linesize[i];
375         }
376     }
377
378     /* top bar */
379     if (pad->y) {
380         ff_draw_rectangle(out->data, out->linesize,
381                           pad->line, pad->line_step, pad->hsub, pad->vsub,
382                           0, 0, pad->w, pad->y);
383     }
384
385     /* bottom bar */
386     if (pad->h > pad->y + pad->in_h) {
387         ff_draw_rectangle(out->data, out->linesize,
388                           pad->line, pad->line_step, pad->hsub, pad->vsub,
389                           0, pad->y + pad->in_h, pad->w, pad->h - pad->y - pad->in_h);
390     }
391
392     /* left border */
393     ff_draw_rectangle(out->data, out->linesize, pad->line, pad->line_step,
394                       pad->hsub, pad->vsub, 0, pad->y, pad->x, in->height);
395
396     if (needs_copy) {
397         ff_copy_rectangle(out->data, out->linesize, in->data, in->linesize,
398                           pad->line_step, pad->hsub, pad->vsub,
399                           pad->x, pad->y, 0, in->width, in->height);
400     }
401
402     /* right border */
403     ff_draw_rectangle(out->data, out->linesize,
404                       pad->line, pad->line_step, pad->hsub, pad->vsub,
405                       pad->x + pad->in_w, pad->y, pad->w - pad->x - pad->in_w,
406                       in->height);
407
408     out->width  = pad->w;
409     out->height = pad->h;
410
411     if (in != out)
412         av_frame_free(&in);
413     return ff_filter_frame(inlink->dst->outputs[0], out);
414 }
415
416 static const AVFilterPad avfilter_vf_pad_inputs[] = {
417     {
418         .name             = "default",
419         .type             = AVMEDIA_TYPE_VIDEO,
420         .config_props     = config_input,
421         .get_video_buffer = get_video_buffer,
422         .filter_frame     = filter_frame,
423     },
424     { NULL }
425 };
426
427 static const AVFilterPad avfilter_vf_pad_outputs[] = {
428     {
429         .name         = "default",
430         .type         = AVMEDIA_TYPE_VIDEO,
431         .config_props = config_output,
432     },
433     { NULL }
434 };
435
436 AVFilter avfilter_vf_pad = {
437     .name          = "pad",
438     .description   = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
439
440     .priv_size     = sizeof(PadContext),
441     .init          = init,
442     .uninit        = uninit,
443     .query_formats = query_formats,
444
445     .inputs    = avfilter_vf_pad_inputs,
446
447     .outputs   = avfilter_vf_pad_outputs,
448 };