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