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