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 "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] = (double) inlink->w / inlink->h;
00155 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
00156 (double) 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
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
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
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
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
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);
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)
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
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
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
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
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 = (const 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 = (const AVFilterPad[]) {{ .name = "default",
00428 .type = AVMEDIA_TYPE_VIDEO,
00429 .config_props = config_output, },
00430 { .name = NULL}},
00431 };