Libav 0.7.1
|
00001 /* 00002 * Copyright (c) 2008 vmrsss 00003 * Copyright (c) 2009 Stefano Sabatini 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; 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_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 int plane; 00303 00304 for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) { 00305 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0; 00306 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0; 00307 00308 av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0); 00309 00310 if(outpicref->format != outpicref->buf->format) //unsupported currently 00311 break; 00312 00313 outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane] 00314 + (pad->y >> vsub) * outpicref->linesize [plane]; 00315 00316 if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0) 00317 || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1) 00318 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0) 00319 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1) 00320 ) 00321 break; 00322 } 00323 pad->needs_copy= plane < 4 && outpicref->data[plane]; 00324 if(pad->needs_copy){ 00325 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n"); 00326 avfilter_unref_buffer(outpicref); 00327 outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES, 00328 FFMAX(inlink->w, pad->w), 00329 FFMAX(inlink->h, pad->h)); 00330 avfilter_copy_buffer_ref_props(outpicref, inpicref); 00331 } 00332 00333 inlink->dst->outputs[0]->out_buf = outpicref; 00334 00335 outpicref->video->w = pad->w; 00336 outpicref->video->h = pad->h; 00337 00338 avfilter_start_frame(inlink->dst->outputs[0], outpicref); 00339 } 00340 00341 static void end_frame(AVFilterLink *link) 00342 { 00343 avfilter_end_frame(link->dst->outputs[0]); 00344 avfilter_unref_buffer(link->cur_buf); 00345 } 00346 00347 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice) 00348 { 00349 PadContext *pad = link->dst->priv; 00350 int bar_y, bar_h = 0; 00351 00352 if (slice_dir * before_slice == 1 && y == pad->y) { 00353 /* top bar */ 00354 bar_y = 0; 00355 bar_h = pad->y; 00356 } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) { 00357 /* bottom bar */ 00358 bar_y = pad->y + pad->in_h; 00359 bar_h = pad->h - pad->in_h - pad->y; 00360 } 00361 00362 if (bar_h) { 00363 ff_draw_rectangle(link->dst->outputs[0]->out_buf->data, 00364 link->dst->outputs[0]->out_buf->linesize, 00365 pad->line, pad->line_step, pad->hsub, pad->vsub, 00366 0, bar_y, pad->w, bar_h); 00367 avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir); 00368 } 00369 } 00370 00371 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir) 00372 { 00373 PadContext *pad = link->dst->priv; 00374 AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf; 00375 AVFilterBufferRef *inpic = link->cur_buf; 00376 00377 y += pad->y; 00378 00379 y &= ~((1 << pad->vsub) - 1); 00380 h &= ~((1 << pad->vsub) - 1); 00381 00382 if (!h) 00383 return; 00384 draw_send_bar_slice(link, y, h, slice_dir, 1); 00385 00386 /* left border */ 00387 ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step, 00388 pad->hsub, pad->vsub, 0, y, pad->x, h); 00389 00390 if(pad->needs_copy){ 00391 ff_copy_rectangle(outpic->data, outpic->linesize, 00392 inpic->data, inpic->linesize, pad->line_step, 00393 pad->hsub, pad->vsub, 00394 pad->x, y, y-pad->y, inpic->video->w, h); 00395 } 00396 00397 /* right border */ 00398 ff_draw_rectangle(outpic->data, outpic->linesize, 00399 pad->line, pad->line_step, pad->hsub, pad->vsub, 00400 pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h); 00401 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir); 00402 00403 draw_send_bar_slice(link, y, h, slice_dir, -1); 00404 } 00405 00406 AVFilter avfilter_vf_pad = { 00407 .name = "pad", 00408 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."), 00409 00410 .priv_size = sizeof(PadContext), 00411 .init = init, 00412 .uninit = uninit, 00413 .query_formats = query_formats, 00414 00415 .inputs = (AVFilterPad[]) {{ .name = "default", 00416 .type = AVMEDIA_TYPE_VIDEO, 00417 .config_props = config_input, 00418 .get_video_buffer = get_video_buffer, 00419 .start_frame = start_frame, 00420 .draw_slice = draw_slice, 00421 .end_frame = end_frame, }, 00422 { .name = NULL}}, 00423 00424 .outputs = (AVFilterPad[]) {{ .name = "default", 00425 .type = AVMEDIA_TYPE_VIDEO, 00426 .config_props = config_output, }, 00427 { .name = NULL}}, 00428 };