00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avfilter.h"
00028 #include "formats.h"
00029 #include "internal.h"
00030 #include "video.h"
00031 #include "libavutil/avstring.h"
00032 #include "libavutil/common.h"
00033 #include "libavutil/eval.h"
00034 #include "libavutil/pixdesc.h"
00035 #include "libavutil/colorspace.h"
00036 #include "libavutil/avassert.h"
00037 #include "libavutil/imgutils.h"
00038 #include "libavutil/parseutils.h"
00039 #include "libavutil/mathematics.h"
00040 #include "drawutils.h"
00041
00042 static const char *const var_names[] = {
00043 "in_w", "iw",
00044 "in_h", "ih",
00045 "out_w", "ow",
00046 "out_h", "oh",
00047 "x",
00048 "y",
00049 "a",
00050 "sar",
00051 "dar",
00052 "hsub",
00053 "vsub",
00054 NULL
00055 };
00056
00057 enum var_name {
00058 VAR_IN_W, VAR_IW,
00059 VAR_IN_H, VAR_IH,
00060 VAR_OUT_W, VAR_OW,
00061 VAR_OUT_H, VAR_OH,
00062 VAR_X,
00063 VAR_Y,
00064 VAR_A,
00065 VAR_SAR,
00066 VAR_DAR,
00067 VAR_HSUB,
00068 VAR_VSUB,
00069 VARS_NB
00070 };
00071
00072 static int query_formats(AVFilterContext *ctx)
00073 {
00074 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
00075 return 0;
00076 }
00077
00078 typedef struct {
00079 int w, h;
00080 int x, y;
00081 int in_w, in_h;
00082
00083 char w_expr[256];
00084 char h_expr[256];
00085 char x_expr[256];
00086 char y_expr[256];
00087
00088 uint8_t rgba_color[4];
00089 FFDrawContext draw;
00090 FFDrawColor color;
00091 } PadContext;
00092
00093 static av_cold int init(AVFilterContext *ctx, const char *args)
00094 {
00095 PadContext *pad = ctx->priv;
00096 char color_string[128] = "black";
00097
00098 av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
00099 av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
00100 av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
00101 av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
00102
00103 if (args)
00104 sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%127s",
00105 pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
00106
00107 if (av_parse_color(pad->rgba_color, color_string, -1, ctx) < 0)
00108 return AVERROR(EINVAL);
00109
00110 return 0;
00111 }
00112
00113 static int config_input(AVFilterLink *inlink)
00114 {
00115 AVFilterContext *ctx = inlink->dst;
00116 PadContext *pad = ctx->priv;
00117 int ret;
00118 double var_values[VARS_NB], res;
00119 char *expr;
00120
00121 ff_draw_init(&pad->draw, inlink->format, 0);
00122 ff_draw_color(&pad->draw, &pad->color, pad->rgba_color);
00123
00124 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
00125 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
00126 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
00127 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
00128 var_values[VAR_A] = (double) inlink->w / inlink->h;
00129 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
00130 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
00131 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
00132 var_values[VAR_HSUB] = 1 << pad->draw.hsub_max;
00133 var_values[VAR_VSUB] = 1 << pad->draw.vsub_max;
00134
00135
00136 av_expr_parse_and_eval(&res, (expr = pad->w_expr),
00137 var_names, var_values,
00138 NULL, NULL, NULL, NULL, NULL, 0, ctx);
00139 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00140 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
00141 var_names, var_values,
00142 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00143 goto eval_fail;
00144 pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
00145
00146 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
00147 var_names, var_values,
00148 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00149 goto eval_fail;
00150 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00151
00152
00153 av_expr_parse_and_eval(&res, (expr = pad->x_expr),
00154 var_names, var_values,
00155 NULL, NULL, NULL, NULL, NULL, 0, ctx);
00156 pad->x = var_values[VAR_X] = res;
00157 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
00158 var_names, var_values,
00159 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00160 goto eval_fail;
00161 pad->y = var_values[VAR_Y] = res;
00162
00163 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
00164 var_names, var_values,
00165 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00166 goto eval_fail;
00167 pad->x = var_values[VAR_X] = res;
00168
00169
00170 if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
00171 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
00172 return AVERROR(EINVAL);
00173 }
00174
00175 if (!pad->w)
00176 pad->w = inlink->w;
00177 if (!pad->h)
00178 pad->h = inlink->h;
00179
00180 pad->w = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->w);
00181 pad->h = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->h);
00182 pad->x = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->x);
00183 pad->y = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->y);
00184 pad->in_w = ff_draw_round_to_sub(&pad->draw, 0, -1, inlink->w);
00185 pad->in_h = ff_draw_round_to_sub(&pad->draw, 1, -1, inlink->h);
00186
00187 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n",
00188 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
00189 pad->rgba_color[0], pad->rgba_color[1], pad->rgba_color[2], pad->rgba_color[3]);
00190
00191 if (pad->x < 0 || pad->y < 0 ||
00192 pad->w <= 0 || pad->h <= 0 ||
00193 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
00194 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
00195 av_log(ctx, AV_LOG_ERROR,
00196 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
00197 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
00198 return AVERROR(EINVAL);
00199 }
00200
00201 return 0;
00202
00203 eval_fail:
00204 av_log(NULL, AV_LOG_ERROR,
00205 "Error when evaluating the expression '%s'\n", expr);
00206 return ret;
00207
00208 }
00209
00210 static int config_output(AVFilterLink *outlink)
00211 {
00212 PadContext *pad = outlink->src->priv;
00213
00214 outlink->w = pad->w;
00215 outlink->h = pad->h;
00216 return 0;
00217 }
00218
00219 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
00220 {
00221 PadContext *pad = inlink->dst->priv;
00222 int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1;
00223
00224 AVFilterBufferRef *picref = ff_get_video_buffer(inlink->dst->outputs[0], perms,
00225 w + (pad->w - pad->in_w) + 4*align,
00226 h + (pad->h - pad->in_h));
00227 int plane;
00228
00229 if (!picref)
00230 return NULL;
00231
00232 picref->video->w = w;
00233 picref->video->h = h;
00234
00235 for (plane = 0; plane < 4 && picref->data[plane]; plane++)
00236 picref->data[plane] += FFALIGN(pad->x >> pad->draw.hsub[plane], align) * pad->draw.pixelstep[plane] +
00237 (pad->y >> pad->draw.vsub[plane]) * picref->linesize[plane];
00238
00239 return picref;
00240 }
00241
00242 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
00243 {
00244 int64_t x_in_buf, y_in_buf;
00245
00246 x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
00247 + (x >> hsub) * pad->draw.pixelstep[plane]
00248 + (y >> vsub) * outpicref->linesize[plane];
00249
00250 if(x_in_buf < 0 || x_in_buf % pad->draw.pixelstep[plane])
00251 return 1;
00252 x_in_buf /= pad->draw.pixelstep[plane];
00253
00254 av_assert0(outpicref->buf->linesize[plane]>0);
00255
00256 y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
00257 x_in_buf %= outpicref->buf->linesize[plane];
00258
00259 if( y_in_buf<<vsub >= outpicref->buf->h
00260 || x_in_buf<<hsub >= outpicref->buf->w)
00261 return 1;
00262 return 0;
00263 }
00264
00265 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
00266 {
00267 PadContext *pad = inlink->dst->priv;
00268 AVFilterBufferRef *out = avfilter_ref_buffer(in, ~0);
00269 int plane, needs_copy;
00270
00271 if (!out) {
00272 avfilter_unref_bufferp(&in);
00273 return AVERROR(ENOMEM);
00274 }
00275
00276 for (plane = 0; plane < 4 && out->data[plane] && pad->draw.pixelstep[plane]; plane++) {
00277 int hsub = pad->draw.hsub[plane];
00278 int vsub = pad->draw.vsub[plane];
00279
00280 av_assert0(out->buf->w > 0 && out->buf->h > 0);
00281
00282 if (out->format != out->buf->format)
00283 break;
00284
00285 out->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane] +
00286 (pad->y >> vsub) * out->linesize[plane];
00287
00288 if (does_clip(pad, out, plane, hsub, vsub, 0, 0) ||
00289 does_clip(pad, out, plane, hsub, vsub, 0, pad->h - 1) ||
00290 does_clip(pad, out, plane, hsub, vsub, pad->w - 1, 0) ||
00291 does_clip(pad, out, plane, hsub, vsub, pad->w - 1, pad->h - 1))
00292 break;
00293 }
00294 needs_copy = plane < 4 && out->data[plane] || !(out->perms & AV_PERM_WRITE);
00295 if (needs_copy) {
00296 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
00297 avfilter_unref_buffer(out);
00298 out = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
00299 FFMAX(inlink->w, pad->w),
00300 FFMAX(inlink->h, pad->h));
00301 if (!out) {
00302 avfilter_unref_bufferp(&in);
00303 return AVERROR(ENOMEM);
00304 }
00305
00306 avfilter_copy_buffer_ref_props(out, in);
00307 }
00308
00309 out->video->w = pad->w;
00310 out->video->h = pad->h;
00311
00312
00313 if (pad->y) {
00314 ff_fill_rectangle(&pad->draw, &pad->color,
00315 out->data, out->linesize,
00316 0, 0, pad->w, pad->y);
00317 }
00318
00319
00320 if (pad->h > pad->y + pad->in_h) {
00321 ff_fill_rectangle(&pad->draw, &pad->color,
00322 out->data, out->linesize,
00323 0, pad->y + pad->in_h, pad->w, pad->h - pad->y - pad->in_h);
00324 }
00325
00326
00327 ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
00328 0, pad->y, pad->x, in->video->h);
00329
00330 if (needs_copy) {
00331 ff_copy_rectangle2(&pad->draw,
00332 out->data, out->linesize, in->data, in->linesize,
00333 pad->x, pad->y, 0, 0, in->video->w, in->video->h);
00334 }
00335
00336
00337 ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
00338 pad->x + pad->in_w, pad->y, pad->w - pad->x - pad->in_w,
00339 in->video->h);
00340
00341 avfilter_unref_bufferp(&in);
00342 return ff_filter_frame(inlink->dst->outputs[0], out);
00343 }
00344
00345 static const AVFilterPad avfilter_vf_pad_inputs[] = {
00346 {
00347 .name = "default",
00348 .type = AVMEDIA_TYPE_VIDEO,
00349 .config_props = config_input,
00350 .get_video_buffer = get_video_buffer,
00351 .filter_frame = filter_frame,
00352 },
00353 { NULL }
00354 };
00355
00356 static const AVFilterPad avfilter_vf_pad_outputs[] = {
00357 {
00358 .name = "default",
00359 .type = AVMEDIA_TYPE_VIDEO,
00360 .config_props = config_output,
00361 },
00362 { NULL }
00363 };
00364
00365 AVFilter avfilter_vf_pad = {
00366 .name = "pad",
00367 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
00368
00369 .priv_size = sizeof(PadContext),
00370 .init = init,
00371 .query_formats = query_formats,
00372
00373 .inputs = avfilter_vf_pad_inputs,
00374
00375 .outputs = avfilter_vf_pad_outputs,
00376 };