OSDN Git Service

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