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