• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 "drawutils.h"
00036 
00037 static const char *var_names[] = {
00038     "PI",
00039     "PHI",
00040     "E",
00041     "in_w",   "iw",
00042     "in_h",   "ih",
00043     "out_w",  "ow",
00044     "out_h",  "oh",
00045     "x",
00046     "y",
00047     "a",
00048     "hsub",
00049     "vsub",
00050     NULL
00051 };
00052 
00053 enum var_name {
00054     VAR_PI,
00055     VAR_PHI,
00056     VAR_E,
00057     VAR_IN_W,   VAR_IW,
00058     VAR_IN_H,   VAR_IH,
00059     VAR_OUT_W,  VAR_OW,
00060     VAR_OUT_H,  VAR_OH,
00061     VAR_X,
00062     VAR_Y,
00063     VAR_A,
00064     VAR_HSUB,
00065     VAR_VSUB,
00066     VARS_NB
00067 };
00068 
00069 static int query_formats(AVFilterContext *ctx)
00070 {
00071     static const enum PixelFormat pix_fmts[] = {
00072         PIX_FMT_ARGB,         PIX_FMT_RGBA,
00073         PIX_FMT_ABGR,         PIX_FMT_BGRA,
00074         PIX_FMT_RGB24,        PIX_FMT_BGR24,
00075 
00076         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
00077         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
00078         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
00079         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
00080         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
00081         PIX_FMT_YUVA420P,
00082 
00083         PIX_FMT_NONE
00084     };
00085 
00086     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00087     return 0;
00088 }
00089 
00090 typedef struct {
00091     int w, h;               
00092     int x, y;               
00093     int in_w, in_h;         
00094 
00095     char w_expr[256];       
00096     char h_expr[256];       
00097     char x_expr[256];       
00098     char y_expr[256];       
00099 
00100     uint8_t color[4];       
00101     uint8_t *line[4];
00102     int      line_step[4];
00103     int hsub, vsub;         
00104     int needs_copy;
00105 } PadContext;
00106 
00107 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00108 {
00109     PadContext *pad = ctx->priv;
00110     char color_string[128] = "black";
00111 
00112     av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
00113     av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
00114     av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
00115     av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
00116 
00117     if (args)
00118         sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s",
00119                pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
00120 
00121     if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
00122         return AVERROR(EINVAL);
00123 
00124     return 0;
00125 }
00126 
00127 static av_cold void uninit(AVFilterContext *ctx)
00128 {
00129     PadContext *pad = ctx->priv;
00130     int i;
00131 
00132     for (i = 0; i < 4; i++) {
00133         av_freep(&pad->line[i]);
00134         pad->line_step[i] = 0;
00135     }
00136 }
00137 
00138 static int config_input(AVFilterLink *inlink)
00139 {
00140     AVFilterContext *ctx = inlink->dst;
00141     PadContext *pad = ctx->priv;
00142     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00143     uint8_t rgba_color[4];
00144     int ret, is_packed_rgba;
00145     double var_values[VARS_NB], res;
00146     char *expr;
00147 
00148     pad->hsub = pix_desc->log2_chroma_w;
00149     pad->vsub = pix_desc->log2_chroma_h;
00150 
00151     var_values[VAR_PI]    = M_PI;
00152     var_values[VAR_PHI]   = M_PHI;
00153     var_values[VAR_E]     = M_E;
00154     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
00155     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
00156     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
00157     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
00158     var_values[VAR_A]     = (float) inlink->w / inlink->h;
00159     var_values[VAR_HSUB]  = 1<<pad->hsub;
00160     var_values[VAR_VSUB]  = 1<<pad->vsub;
00161 
00162     /* evaluate width and height */
00163     av_expr_parse_and_eval(&res, (expr = pad->w_expr),
00164                            var_names, var_values,
00165                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
00166     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00167     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
00168                                       var_names, var_values,
00169                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00170         goto eval_fail;
00171     pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
00172     /* evaluate the width again, as it may depend on the evaluated output height */
00173     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
00174                                       var_names, var_values,
00175                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00176         goto eval_fail;
00177     pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00178 
00179     /* evaluate x and y */
00180     av_expr_parse_and_eval(&res, (expr = pad->x_expr),
00181                            var_names, var_values,
00182                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
00183     pad->x = var_values[VAR_X] = res;
00184     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
00185                                       var_names, var_values,
00186                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00187         goto eval_fail;
00188     pad->y = var_values[VAR_Y] = res;
00189     /* evaluate x again, as it may depend on the evaluated y value */
00190     if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
00191                                       var_names, var_values,
00192                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00193         goto eval_fail;
00194     pad->x = var_values[VAR_X] = res;
00195 
00196     /* sanity check params */
00197     if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
00198         av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
00199         return AVERROR(EINVAL);
00200     }
00201 
00202     if (!pad->w)
00203         pad->w = inlink->w;
00204     if (!pad->h)
00205         pad->h = inlink->h;
00206 
00207     pad->w &= ~((1 << pad->hsub) - 1);
00208     pad->h &= ~((1 << pad->vsub) - 1);
00209     pad->x &= ~((1 << pad->hsub) - 1);
00210     pad->y &= ~((1 << pad->vsub) - 1);
00211 
00212     pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
00213     pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
00214 
00215     memcpy(rgba_color, pad->color, sizeof(rgba_color));
00216     ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
00217                             inlink->format, rgba_color, &is_packed_rgba, NULL);
00218 
00219     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",
00220            inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
00221            pad->color[0], pad->color[1], pad->color[2], pad->color[3],
00222            is_packed_rgba ? "rgba" : "yuva");
00223 
00224     if (pad->x <  0 || pad->y <  0                      ||
00225         pad->w <= 0 || pad->h <= 0                      ||
00226         (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
00227         (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
00228         av_log(ctx, AV_LOG_ERROR,
00229                "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
00230                pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
00231         return AVERROR(EINVAL);
00232     }
00233 
00234     return 0;
00235 
00236 eval_fail:
00237     av_log(NULL, AV_LOG_ERROR,
00238            "Error when evaluating the expression '%s'\n", expr);
00239     return ret;
00240 
00241 }
00242 
00243 static int config_output(AVFilterLink *outlink)
00244 {
00245     PadContext *pad = outlink->src->priv;
00246 
00247     outlink->w = pad->w;
00248     outlink->h = pad->h;
00249     return 0;
00250 }
00251 
00252 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
00253 {
00254     PadContext *pad = inlink->dst->priv;
00255 
00256     AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
00257                                                        w + (pad->w - pad->in_w),
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] += (pad->x >> hsub) * 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     AVFilterBufferRef *for_next_filter;
00303     int plane;
00304 
00305     for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
00306         int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
00307         int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
00308 
00309         av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
00310 
00311         if(outpicref->format != outpicref->buf->format) //unsupported currently
00312             break;
00313 
00314         outpicref->data[plane] -=   (pad->x  >> hsub) * pad      ->line_step[plane]
00315                                   + (pad->y  >> vsub) * outpicref->linesize [plane];
00316 
00317         if(   does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
00318            || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
00319            || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
00320            || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
00321           )
00322             break;
00323     }
00324     pad->needs_copy= plane < 4 && outpicref->data[plane];
00325     if(pad->needs_copy){
00326         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
00327         avfilter_unref_buffer(outpicref);
00328         outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
00329                                                        FFMAX(inlink->w, pad->w),
00330                                                        FFMAX(inlink->h, pad->h));
00331         avfilter_copy_buffer_ref_props(outpicref, inpicref);
00332     }
00333 
00334     inlink->dst->outputs[0]->out_buf = outpicref;
00335 
00336     outpicref->video->w = pad->w;
00337     outpicref->video->h = pad->h;
00338 
00339     for_next_filter = avfilter_ref_buffer(outpicref, ~0);
00340     avfilter_start_frame(inlink->dst->outputs[0], for_next_filter);
00341 }
00342 
00343 static void end_frame(AVFilterLink *link)
00344 {
00345     avfilter_end_frame(link->dst->outputs[0]);
00346     avfilter_unref_buffer(link->dst->outputs[0]->out_buf);
00347     avfilter_unref_buffer(link->cur_buf);
00348 }
00349 
00350 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
00351 {
00352     PadContext *pad = link->dst->priv;
00353     int bar_y, bar_h = 0;
00354 
00355     if        (slice_dir * before_slice ==  1 && y == pad->y) {
00356         /* top bar */
00357         bar_y = 0;
00358         bar_h = pad->y;
00359     } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
00360         /* bottom bar */
00361         bar_y = pad->y + pad->in_h;
00362         bar_h = pad->h - pad->in_h - pad->y;
00363     }
00364 
00365     if (bar_h) {
00366         ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
00367                           link->dst->outputs[0]->out_buf->linesize,
00368                           pad->line, pad->line_step, pad->hsub, pad->vsub,
00369                           0, bar_y, pad->w, bar_h);
00370         avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
00371     }
00372 }
00373 
00374 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00375 {
00376     PadContext *pad = link->dst->priv;
00377     AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
00378     AVFilterBufferRef *inpic = link->cur_buf;
00379 
00380     y += pad->y;
00381 
00382     y &= ~((1 << pad->vsub) - 1);
00383     h &= ~((1 << pad->vsub) - 1);
00384 
00385     if (!h)
00386         return;
00387     draw_send_bar_slice(link, y, h, slice_dir, 1);
00388 
00389     /* left border */
00390     ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
00391                       pad->hsub, pad->vsub, 0, y, pad->x, h);
00392 
00393     if(pad->needs_copy){
00394         ff_copy_rectangle(outpic->data, outpic->linesize,
00395                           inpic->data, inpic->linesize, pad->line_step,
00396                           pad->hsub, pad->vsub,
00397                           pad->x, y, y-pad->y, inpic->video->w, h);
00398     }
00399 
00400     /* right border */
00401     ff_draw_rectangle(outpic->data, outpic->linesize,
00402                       pad->line, pad->line_step, pad->hsub, pad->vsub,
00403                       pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
00404     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
00405 
00406     draw_send_bar_slice(link, y, h, slice_dir, -1);
00407 }
00408 
00409 AVFilter avfilter_vf_pad = {
00410     .name          = "pad",
00411     .description   = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
00412 
00413     .priv_size     = sizeof(PadContext),
00414     .init          = init,
00415     .uninit        = uninit,
00416     .query_formats = query_formats,
00417 
00418     .inputs    = (AVFilterPad[]) {{ .name             = "default",
00419                                     .type             = AVMEDIA_TYPE_VIDEO,
00420                                     .config_props     = config_input,
00421                                     .get_video_buffer = get_video_buffer,
00422                                     .start_frame      = start_frame,
00423                                     .draw_slice       = draw_slice,
00424                                     .end_frame        = end_frame, },
00425                                   { .name = NULL}},
00426 
00427     .outputs   = (AVFilterPad[]) {{ .name             = "default",
00428                                     .type             = AVMEDIA_TYPE_VIDEO,
00429                                     .config_props     = config_output, },
00430                                   { .name = NULL}},
00431 };

Generated on Fri Feb 22 2013 07:24:31 for FFmpeg by  doxygen 1.7.1