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 int needs_copy;
00092 } PadContext;
00093
00094 static av_cold int init(AVFilterContext *ctx, const char *args)
00095 {
00096 PadContext *pad = ctx->priv;
00097 char color_string[128] = "black";
00098
00099 av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
00100 av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
00101 av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
00102 av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
00103
00104 if (args)
00105 sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%127s",
00106 pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
00107
00108 if (av_parse_color(pad->rgba_color, color_string, -1, ctx) < 0)
00109 return AVERROR(EINVAL);
00110
00111 return 0;
00112 }
00113
00114 static int config_input(AVFilterLink *inlink)
00115 {
00116 AVFilterContext *ctx = inlink->dst;
00117 PadContext *pad = ctx->priv;
00118 int ret;
00119 double var_values[VARS_NB], res;
00120 char *expr;
00121
00122 ff_draw_init(&pad->draw, inlink->format, 0);
00123 ff_draw_color(&pad->draw, &pad->color, pad->rgba_color);
00124
00125 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
00126 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
00127 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
00128 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
00129 var_values[VAR_A] = (float) inlink->w / inlink->h;
00130 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
00131 (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
00132 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
00133 var_values[VAR_HSUB] = 1 << pad->draw.hsub_max;
00134 var_values[VAR_VSUB] = 1 << pad->draw.vsub_max;
00135
00136
00137 av_expr_parse_and_eval(&res, (expr = pad->w_expr),
00138 var_names, var_values,
00139 NULL, NULL, NULL, NULL, NULL, 0, ctx);
00140 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00141 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
00142 var_names, var_values,
00143 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00144 goto eval_fail;
00145 pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
00146
00147 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
00148 var_names, var_values,
00149 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00150 goto eval_fail;
00151 pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00152
00153
00154 av_expr_parse_and_eval(&res, (expr = pad->x_expr),
00155 var_names, var_values,
00156 NULL, NULL, NULL, NULL, NULL, 0, ctx);
00157 pad->x = var_values[VAR_X] = res;
00158 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
00159 var_names, var_values,
00160 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00161 goto eval_fail;
00162 pad->y = var_values[VAR_Y] = res;
00163
00164 if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
00165 var_names, var_values,
00166 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00167 goto eval_fail;
00168 pad->x = var_values[VAR_X] = res;
00169
00170
00171 if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
00172 av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
00173 return AVERROR(EINVAL);
00174 }
00175
00176 if (!pad->w)
00177 pad->w = inlink->w;
00178 if (!pad->h)
00179 pad->h = inlink->h;
00180
00181 pad->w = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->w);
00182 pad->h = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->h);
00183 pad->x = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->x);
00184 pad->y = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->y);
00185 pad->in_w = ff_draw_round_to_sub(&pad->draw, 0, -1, inlink->w);
00186 pad->in_h = ff_draw_round_to_sub(&pad->draw, 1, -1, inlink->h);
00187
00188 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",
00189 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
00190 pad->rgba_color[0], pad->rgba_color[1], pad->rgba_color[2], pad->rgba_color[3]);
00191
00192 if (pad->x < 0 || pad->y < 0 ||
00193 pad->w <= 0 || pad->h <= 0 ||
00194 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
00195 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
00196 av_log(ctx, AV_LOG_ERROR,
00197 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
00198 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
00199 return AVERROR(EINVAL);
00200 }
00201
00202 return 0;
00203
00204 eval_fail:
00205 av_log(NULL, AV_LOG_ERROR,
00206 "Error when evaluating the expression '%s'\n", expr);
00207 return ret;
00208
00209 }
00210
00211 static int config_output(AVFilterLink *outlink)
00212 {
00213 PadContext *pad = outlink->src->priv;
00214
00215 outlink->w = pad->w;
00216 outlink->h = pad->h;
00217 return 0;
00218 }
00219
00220 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
00221 {
00222 PadContext *pad = inlink->dst->priv;
00223 int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1;
00224
00225 AVFilterBufferRef *picref = ff_get_video_buffer(inlink->dst->outputs[0], perms,
00226 w + (pad->w - pad->in_w) + 4*align,
00227 h + (pad->h - pad->in_h));
00228 int plane;
00229
00230 if (!picref)
00231 return NULL;
00232
00233 picref->video->w = w;
00234 picref->video->h = h;
00235
00236 for (plane = 0; plane < 4 && picref->data[plane]; plane++)
00237 picref->data[plane] += FFALIGN(pad->x >> pad->draw.hsub[plane], align) * pad->draw.pixelstep[plane] +
00238 (pad->y >> pad->draw.vsub[plane]) * picref->linesize[plane];
00239
00240 return picref;
00241 }
00242
00243 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
00244 {
00245 int64_t x_in_buf, y_in_buf;
00246
00247 x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
00248 + (x >> hsub) * pad->draw.pixelstep[plane]
00249 + (y >> vsub) * outpicref->linesize[plane];
00250
00251 if(x_in_buf < 0 || x_in_buf % pad->draw.pixelstep[plane])
00252 return 1;
00253 x_in_buf /= pad->draw.pixelstep[plane];
00254
00255 av_assert0(outpicref->buf->linesize[plane]>0);
00256
00257 y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
00258 x_in_buf %= outpicref->buf->linesize[plane];
00259
00260 if( y_in_buf<<vsub >= outpicref->buf->h
00261 || x_in_buf<<hsub >= outpicref->buf->w)
00262 return 1;
00263 return 0;
00264 }
00265
00266 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00267 {
00268 PadContext *pad = inlink->dst->priv;
00269 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
00270 AVFilterBufferRef *for_next_filter;
00271 int plane, ret = 0;
00272
00273 if (!outpicref)
00274 return AVERROR(ENOMEM);
00275
00276 for (plane = 0; plane < 4 && outpicref->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(outpicref->buf->w>0 && outpicref->buf->h>0);
00281
00282 if(outpicref->format != outpicref->buf->format)
00283 break;
00284
00285 outpicref->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane]
00286 + (pad->y >> vsub) * outpicref->linesize[plane];
00287
00288 if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
00289 || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
00290 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
00291 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
00292 )
00293 break;
00294 }
00295 pad->needs_copy= plane < 4 && outpicref->data[plane] || !(outpicref->perms & AV_PERM_WRITE);
00296 if(pad->needs_copy){
00297 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
00298 avfilter_unref_buffer(outpicref);
00299 outpicref = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
00300 FFMAX(inlink->w, pad->w),
00301 FFMAX(inlink->h, pad->h));
00302 if (!outpicref)
00303 return AVERROR(ENOMEM);
00304
00305 avfilter_copy_buffer_ref_props(outpicref, inpicref);
00306 }
00307
00308 outpicref->video->w = pad->w;
00309 outpicref->video->h = pad->h;
00310
00311 for_next_filter = avfilter_ref_buffer(outpicref, ~0);
00312 if (!for_next_filter) {
00313 ret = AVERROR(ENOMEM);
00314 goto fail;
00315 }
00316
00317 ret = ff_start_frame(inlink->dst->outputs[0], for_next_filter);
00318 if (ret < 0)
00319 goto fail;
00320
00321 inlink->dst->outputs[0]->out_buf = outpicref;
00322 return 0;
00323
00324 fail:
00325 avfilter_unref_bufferp(&outpicref);
00326 return ret;
00327 }
00328
00329 static int draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
00330 {
00331 PadContext *pad = link->dst->priv;
00332 int bar_y, bar_h = 0, ret = 0;
00333
00334 if (slice_dir * before_slice == 1 && y == pad->y) {
00335
00336 bar_y = 0;
00337 bar_h = pad->y;
00338 } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
00339
00340 bar_y = pad->y + pad->in_h;
00341 bar_h = pad->h - pad->in_h - pad->y;
00342 }
00343
00344 if (bar_h) {
00345 ff_fill_rectangle(&pad->draw, &pad->color,
00346 link->dst->outputs[0]->out_buf->data,
00347 link->dst->outputs[0]->out_buf->linesize,
00348 0, bar_y, pad->w, bar_h);
00349 ret = ff_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
00350 }
00351 return ret;
00352 }
00353
00354 static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00355 {
00356 PadContext *pad = link->dst->priv;
00357 AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
00358 AVFilterBufferRef *inpic = link->cur_buf;
00359 int ret;
00360
00361 y += pad->y;
00362
00363 y = ff_draw_round_to_sub(&pad->draw, 1, -1, y);
00364 h = ff_draw_round_to_sub(&pad->draw, 1, -1, h);
00365
00366 if (!h)
00367 return 0;
00368 draw_send_bar_slice(link, y, h, slice_dir, 1);
00369
00370
00371 ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
00372 0, y, pad->x, h);
00373
00374 if(pad->needs_copy){
00375 ff_copy_rectangle2(&pad->draw,
00376 outpic->data, outpic->linesize,
00377 inpic ->data, inpic ->linesize,
00378 pad->x, y, 0, y - pad->y, inpic->video->w, h);
00379 }
00380
00381
00382 ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
00383 pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
00384 ret = ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
00385 if (ret < 0)
00386 return ret;
00387
00388 return draw_send_bar_slice(link, y, h, slice_dir, -1);
00389 }
00390
00391 AVFilter avfilter_vf_pad = {
00392 .name = "pad",
00393 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
00394
00395 .priv_size = sizeof(PadContext),
00396 .init = init,
00397 .query_formats = query_formats,
00398
00399 .inputs = (const AVFilterPad[]) {{ .name = "default",
00400 .type = AVMEDIA_TYPE_VIDEO,
00401 .config_props = config_input,
00402 .get_video_buffer = get_video_buffer,
00403 .start_frame = start_frame,
00404 .draw_slice = draw_slice, },
00405 { .name = NULL}},
00406
00407 .outputs = (const AVFilterPad[]) {{ .name = "default",
00408 .type = AVMEDIA_TYPE_VIDEO,
00409 .config_props = config_output, },
00410 { .name = NULL}},
00411 };