libavfilter/vf_pad.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008 vmrsss
00003  * Copyright (c) 2009 Stefano Sabatini
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avfilter.h"
00028 #include "libavutil/avstring.h"
00029 #include "libavutil/eval.h"
00030 #include "libavutil/pixdesc.h"
00031 #include "libavutil/colorspace.h"
00032 #include "libavutil/avassert.h"
00033 #include "libavutil/imgutils.h"
00034 #include "libavutil/parseutils.h"
00035 #include "libavutil/mathematics.h"
00036 #include "drawutils.h"
00037 
00038 static const char * const var_names[] = {
00039     "in_w",   "iw",
00040     "in_h",   "ih",
00041     "out_w",  "ow",
00042     "out_h",  "oh",
00043     "x",
00044     "y",
00045     "a",
00046     "sar",
00047     "dar",
00048     "hsub",
00049     "vsub",
00050     NULL
00051 };
00052 
00053 enum var_name {
00054     VAR_IN_W,   VAR_IW,
00055     VAR_IN_H,   VAR_IH,
00056     VAR_OUT_W,  VAR_OW,
00057     VAR_OUT_H,  VAR_OH,
00058     VAR_X,
00059     VAR_Y,
00060     VAR_A,
00061     VAR_SAR,
00062     VAR_DAR,
00063     VAR_HSUB,
00064     VAR_VSUB,
00065     VARS_NB
00066 };
00067 
00068 static int query_formats(AVFilterContext *ctx)
00069 {
00070     static const enum PixelFormat pix_fmts[] = {
00071         PIX_FMT_ARGB,         PIX_FMT_RGBA,
00072         PIX_FMT_ABGR,         PIX_FMT_BGRA,
00073         PIX_FMT_RGB24,        PIX_FMT_BGR24,
00074 
00075         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
00076         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
00077         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
00078         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
00079         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
00080         PIX_FMT_YUVA420P,
00081 
00082         PIX_FMT_NONE
00083     };
00084 
00085     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00086     return 0;
00087 }
00088 
00089 typedef struct {
00090     int w, h;               
00091     int x, y;               
00092     int in_w, in_h;         
00093 
00094     char w_expr[256];       
00095     char h_expr[256];       
00096     char x_expr[256];       
00097     char y_expr[256];       
00098 
00099     uint8_t color[4];       
00100     uint8_t *line[4];
00101     int      line_step[4];
00102     int hsub, vsub;         
00103     int needs_copy;
00104 } PadContext;
00105 
00106 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00107 {
00108     PadContext *pad = ctx->priv;
00109     char color_string[128] = "black";
00110 
00111     av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
00112     av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
00113     av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
00114     av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
00115 
00116     if (args)
00117         sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%127s",
00118                pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
00119 
00120     if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
00121         return AVERROR(EINVAL);
00122 
00123     return 0;
00124 }
00125 
00126 static av_cold void uninit(AVFilterContext *ctx)
00127 {
00128     PadContext *pad = ctx->priv;
00129     int i;
00130 
00131     for (i = 0; i < 4; i++) {
00132         av_freep(&pad->line[i]);
00133         pad->line_step[i] = 0;
00134     }
00135 }
00136 
00137 static int config_input(AVFilterLink *inlink)
00138 {
00139     AVFilterContext *ctx = inlink->dst;
00140     PadContext *pad = ctx->priv;
00141     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00142     uint8_t rgba_color[4];
00143     int ret, is_packed_rgba;
00144     double var_values[VARS_NB], res;
00145     char *expr;
00146 
00147     pad->hsub = pix_desc->log2_chroma_w;
00148     pad->vsub = pix_desc->log2_chroma_h;
00149 
00150     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
00151     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
00152     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
00153     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
00154     var_values[VAR_A]     = (float) inlink->w / inlink->h;
00155     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
00156         (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
00157     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
00158     var_values[VAR_HSUB]  = 1<<pad->hsub;
00159     var_values[VAR_VSUB]  = 1<<pad->vsub;
00160 
00161     /* evaluate width and height */
00162     av_expr_parse_and_eval(&res, (expr = pad->w_expr),
00163                            var_names, var_values,
00164                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
00165     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00166     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
00167                                       var_names, var_values,
00168                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00169         goto eval_fail;
00170     pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
00171     /* evaluate the width again, as it may depend on the evaluated output height */
00172     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
00173                                       var_names, var_values,
00174                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00175         goto eval_fail;
00176     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00177 
00178     /* evaluate x and y */
00179     av_expr_parse_and_eval(&res, (expr = pad->x_expr),
00180                            var_names, var_values,
00181                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
00182     pad->x = var_values[VAR_X] = res;
00183     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
00184                                       var_names, var_values,
00185                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00186         goto eval_fail;
00187     pad->y = var_values[VAR_Y] = res;
00188     /* evaluate x again, as it may depend on the evaluated y value */
00189     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
00190                                       var_names, var_values,
00191                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00192         goto eval_fail;
00193     pad->x = var_values[VAR_X] = res;
00194 
00195     /* sanity check params */
00196     if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
00197         av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
00198         return AVERROR(EINVAL);
00199     }
00200 
00201     if (!pad->w)
00202         pad->w = inlink->w;
00203     if (!pad->h)
00204         pad->h = inlink->h;
00205 
00206     pad->w &= ~((1 << pad->hsub) - 1);
00207     pad->h &= ~((1 << pad->vsub) - 1);
00208     pad->x &= ~((1 << pad->hsub) - 1);
00209     pad->y &= ~((1 << pad->vsub) - 1);
00210 
00211     pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
00212     pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
00213 
00214     memcpy(rgba_color, pad->color, sizeof(rgba_color));
00215     ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
00216                             inlink->format, rgba_color, &is_packed_rgba, NULL);
00217 
00218     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",
00219            inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
00220            pad->color[0], pad->color[1], pad->color[2], pad->color[3],
00221            is_packed_rgba ? "rgba" : "yuva");
00222 
00223     if (pad->x <  0 || pad->y <  0                      ||
00224         pad->w <= 0 || pad->h <= 0                      ||
00225         (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
00226         (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
00227         av_log(ctx, AV_LOG_ERROR,
00228                "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
00229                pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
00230         return AVERROR(EINVAL);
00231     }
00232 
00233     return 0;
00234 
00235 eval_fail:
00236     av_log(NULL, AV_LOG_ERROR,
00237            "Error when evaluating the expression '%s'\n", expr);
00238     return ret;
00239 
00240 }
00241 
00242 static int config_output(AVFilterLink *outlink)
00243 {
00244     PadContext *pad = outlink->src->priv;
00245 
00246     outlink->w = pad->w;
00247     outlink->h = pad->h;
00248     return 0;
00249 }
00250 
00251 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
00252 {
00253     PadContext *pad = inlink->dst->priv;
00254     int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1;
00255 
00256     AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
00257                                                        w + (pad->w - pad->in_w) + 4*align,
00258                                                        h + (pad->h - pad->in_h));
00259     int plane;
00260 
00261     picref->video->w = w;
00262     picref->video->h = h;
00263 
00264     for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
00265         int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
00266         int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
00267 
00268         picref->data[plane] += FFALIGN(pad->x >> hsub, align) * pad->line_step[plane] +
00269             (pad->y >> vsub) * picref->linesize[plane];
00270     }
00271 
00272     return picref;
00273 }
00274 
00275 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
00276 {
00277     int64_t x_in_buf, y_in_buf;
00278 
00279     x_in_buf =  outpicref->data[plane] - outpicref->buf->data[plane]
00280              +  (x >> hsub) * pad      ->line_step[plane]
00281              +  (y >> vsub) * outpicref->linesize [plane];
00282 
00283     if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
00284         return 1;
00285     x_in_buf /= pad->line_step[plane];
00286 
00287     av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
00288 
00289     y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
00290     x_in_buf %= outpicref->buf->linesize[plane];
00291 
00292     if(   y_in_buf<<vsub >= outpicref->buf->h
00293        || x_in_buf<<hsub >= outpicref->buf->w)
00294         return 1;
00295     return 0;
00296 }
00297 
00298 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00299 {
00300     PadContext *pad = inlink->dst->priv;
00301     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
00302     int plane;
00303 
00304     for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
00305         int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
00306         int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
00307 
00308         av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
00309 
00310         if(outpicref->format != outpicref->buf->format) //unsupported currently
00311             break;
00312 
00313         outpicref->data[plane] -=   (pad->x  >> hsub) * pad      ->line_step[plane]
00314                                   + (pad->y  >> vsub) * outpicref->linesize [plane];
00315 
00316         if(   does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
00317            || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
00318            || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
00319            || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
00320           )
00321             break;
00322     }
00323     pad->needs_copy= plane < 4 && outpicref->data[plane];
00324     if(pad->needs_copy){
00325         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
00326         avfilter_unref_buffer(outpicref);
00327         outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
00328                                                        FFMAX(inlink->w, pad->w),
00329                                                        FFMAX(inlink->h, pad->h));
00330         avfilter_copy_buffer_ref_props(outpicref, inpicref);
00331     }
00332 
00333     inlink->dst->outputs[0]->out_buf = outpicref;
00334 
00335     outpicref->video->w = pad->w;
00336     outpicref->video->h = pad->h;
00337 
00338     avfilter_start_frame(inlink->dst->outputs[0], outpicref);
00339 }
00340 
00341 static void end_frame(AVFilterLink *link)
00342 {
00343     avfilter_end_frame(link->dst->outputs[0]);
00344     avfilter_unref_buffer(link->cur_buf);
00345 }
00346 
00347 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
00348 {
00349     PadContext *pad = link->dst->priv;
00350     int bar_y, bar_h = 0;
00351 
00352     if        (slice_dir * before_slice ==  1 && y == pad->y) {
00353         /* top bar */
00354         bar_y = 0;
00355         bar_h = pad->y;
00356     } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
00357         /* bottom bar */
00358         bar_y = pad->y + pad->in_h;
00359         bar_h = pad->h - pad->in_h - pad->y;
00360     }
00361 
00362     if (bar_h) {
00363         ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
00364                           link->dst->outputs[0]->out_buf->linesize,
00365                           pad->line, pad->line_step, pad->hsub, pad->vsub,
00366                           0, bar_y, pad->w, bar_h);
00367         avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
00368     }
00369 }
00370 
00371 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00372 {
00373     PadContext *pad = link->dst->priv;
00374     AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
00375     AVFilterBufferRef *inpic = link->cur_buf;
00376 
00377     y += pad->y;
00378 
00379     y &= ~((1 << pad->vsub) - 1);
00380     h &= ~((1 << pad->vsub) - 1);
00381 
00382     if (!h)
00383         return;
00384     draw_send_bar_slice(link, y, h, slice_dir, 1);
00385 
00386     /* left border */
00387     ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
00388                       pad->hsub, pad->vsub, 0, y, pad->x, h);
00389 
00390     if(pad->needs_copy){
00391         ff_copy_rectangle(outpic->data, outpic->linesize,
00392                           inpic->data, inpic->linesize, pad->line_step,
00393                           pad->hsub, pad->vsub,
00394                           pad->x, y, y-pad->y, inpic->video->w, h);
00395     }
00396 
00397     /* right border */
00398     ff_draw_rectangle(outpic->data, outpic->linesize,
00399                       pad->line, pad->line_step, pad->hsub, pad->vsub,
00400                       pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
00401     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
00402 
00403     draw_send_bar_slice(link, y, h, slice_dir, -1);
00404 }
00405 
00406 AVFilter avfilter_vf_pad = {
00407     .name          = "pad",
00408     .description   = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
00409 
00410     .priv_size     = sizeof(PadContext),
00411     .init          = init,
00412     .uninit        = uninit,
00413     .query_formats = query_formats,
00414 
00415     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
00416                                     .type             = AVMEDIA_TYPE_VIDEO,
00417                                     .config_props     = config_input,
00418                                     .get_video_buffer = get_video_buffer,
00419                                     .start_frame      = start_frame,
00420                                     .draw_slice       = draw_slice,
00421                                     .end_frame        = end_frame, },
00422                                   { .name = NULL}},
00423 
00424     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
00425                                     .type             = AVMEDIA_TYPE_VIDEO,
00426                                     .config_props     = config_output, },
00427                                   { .name = NULL}},
00428 };