From 84994421bc8af27a7ee5bb80ca944aada237e34a Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Sat, 3 Dec 2011 01:11:12 +0100 Subject: [PATCH 1/7] doc: remove space between variable and post increment in example code --- doc/developer.texi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/developer.texi b/doc/developer.texi index 128b46e830..fe1f1a2652 100644 --- a/doc/developer.texi +++ b/doc/developer.texi @@ -52,7 +52,7 @@ The code is written in K&R C style. That means the following: The control statements are formatted by putting space betwen the statement and parenthesis in the following way: @example -for (i = 0; i < filter->input_count; i ++) @{ +for (i = 0; i < filter->input_count; i++) @{ @end example @item The case statement is always located at the same level as the switch itself: From a2fb4bcb0189f6421608e0dec1a38c65910763f6 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Thu, 1 Dec 2011 11:14:54 +0100 Subject: [PATCH 2/7] drawtext: refactor draw_text Split the memory allocation from the actual drawing. --- libavfilter/vf_drawtext.c | 71 ++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c index c145874fa2..3d6b7b0486 100644 --- a/libavfilter/vf_drawtext.c +++ b/libavfilter/vf_drawtext.c @@ -55,8 +55,8 @@ typedef struct { FT_Vector *positions; ///< positions for each element in the text size_t nb_positions; ///< number of elements of positions array char *textfile; ///< file with text to be drawn - unsigned int x; ///< x position to start drawing text - unsigned int y; ///< y position to start drawing text + int x, y; ///< position to start drawing text + int w, h; ///< dimension of the text block int shadowx, shadowy; unsigned int fontsize; ///< font size to use char *fontcolor_string; ///< font color as string @@ -542,8 +542,7 @@ static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref, return 0; } -static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, - int width, int height) +static int dtext_prepare_text(AVFilterContext *ctx, int width, int height) { DrawTextContext *dtext = ctx->priv; uint32_t code = 0, prev_code = 0; @@ -582,15 +581,19 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, text = dtext->expanded_text = buf; dtext->expanded_text_size = buf_size; #endif - if ((len = strlen(text)) > dtext->nb_positions) { - if (!(dtext->positions = - av_realloc(dtext->positions, len*sizeof(*dtext->positions)))) - return AVERROR(ENOMEM); - dtext->nb_positions = len; - } - x = dtext->x; - y = dtext->y; + if ((len = strlen(text)) > dtext->nb_positions) { + FT_Vector *p = av_realloc(dtext->positions, + len * sizeof(*dtext->positions)); + if (!p) { + av_freep(dtext->positions); + dtext->nb_positions = 0; + return AVERROR(ENOMEM); + } else { + dtext->positions = p; + dtext->nb_positions = len; + } + } /* load and cache glyphs */ for (i = 0, p = text; *p; i++) { @@ -600,7 +603,8 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, dummy.code = code; glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL); if (!glyph) - load_glyph(ctx, &glyph, code); + ret = load_glyph(ctx, &glyph, code); + if (ret) return ret; y_min = FFMIN(glyph->bbox.yMin, y_min); y_max = FFMAX(glyph->bbox.yMax, y_max); @@ -621,7 +625,7 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, if (is_newline(code)) { str_w = FFMAX(str_w, x - dtext->x); y += text_height; - x = dtext->x; + x = 0; continue; } @@ -638,9 +642,9 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, } if (x + glyph->bbox.xMax >= width) { - str_w = FFMAX(str_w, x - dtext->x); + str_w = FFMAX(str_w, x); y += text_height; - x = dtext->x; + x = 0; } /* save position */ @@ -650,23 +654,43 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, else x += glyph->advance; } - str_w = FFMIN(width - dtext->x - 1, FFMAX(str_w, x - dtext->x)); + str_w = FFMIN(width - 1, FFMAX(str_w, x)); y = FFMIN(y + text_height, height - 1); + dtext->w = str_w; + dtext->h = y; + + return 0; +} + + +static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, + int width, int height) +{ + DrawTextContext *dtext = ctx->priv; + int ret; + /* draw box */ if (dtext->draw_box) - drawbox(picref, dtext->x, dtext->y, str_w, y-dtext->y, + drawbox(picref, dtext->x, dtext->y, dtext->w, dtext->h, dtext->box_line, dtext->pixel_step, dtext->boxcolor, - dtext->hsub, dtext->vsub, dtext->is_packed_rgb, dtext->rgba_map); + dtext->hsub, dtext->vsub, dtext->is_packed_rgb, + dtext->rgba_map); if (dtext->shadowx || dtext->shadowy) { - if ((ret = draw_glyphs(dtext, picref, width, height, dtext->shadowcolor_rgba, - dtext->shadowcolor, dtext->shadowx, dtext->shadowy)) < 0) + if ((ret = draw_glyphs(dtext, picref, width, height, + dtext->shadowcolor_rgba, + dtext->shadowcolor, + dtext->x + dtext->shadowx, + dtext->y + dtext->shadowy)) < 0) return ret; } - if ((ret = draw_glyphs(dtext, picref, width, height, dtext->fontcolor_rgba, - dtext->fontcolor, 0, 0)) < 0) + if ((ret = draw_glyphs(dtext, picref, width, height, + dtext->fontcolor_rgba, + dtext->fontcolor, + dtext->x, + dtext->y)) < 0) return ret; return 0; @@ -679,6 +703,7 @@ static void end_frame(AVFilterLink *inlink) AVFilterLink *outlink = inlink->dst->outputs[0]; AVFilterBufferRef *picref = inlink->cur_buf; + dtext_prepare_text(inlink->dst, picref->video->w, picref->video->h); draw_text(inlink->dst, picref, picref->video->w, picref->video->h); avfilter_draw_slice(outlink, 0, picref->video->h, 1); From ec11ff8407eadc65f4225f83e778c504c00ce037 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Thu, 1 Dec 2011 11:27:19 +0100 Subject: [PATCH 3/7] drawtext: manage memory allocation better Call dtext_prepare_text as early as possible Do not draw if the memory allocation failed --- libavfilter/vf_drawtext.c | 264 +++++++++++++++++++------------------- 1 file changed, 132 insertions(+), 132 deletions(-) diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c index 3d6b7b0486..06b5dc0ba1 100644 --- a/libavfilter/vf_drawtext.c +++ b/libavfilter/vf_drawtext.c @@ -369,9 +369,135 @@ static av_cold void uninit(AVFilterContext *ctx) } +static inline int is_newline(uint32_t c) +{ + return (c == '\n' || c == '\r' || c == '\f' || c == '\v'); +} + +static int dtext_prepare_text(AVFilterContext *ctx, int width, int height) +{ + DrawTextContext *dtext = ctx->priv; + uint32_t code = 0, prev_code = 0; + int x = 0, y = 0, i = 0, ret; + int text_height, baseline; + char *text = dtext->text; + uint8_t *p; + int str_w = 0, len; + int y_min = 32000, y_max = -32000; + FT_Vector delta; + Glyph *glyph = NULL, *prev_glyph = NULL; + Glyph dummy = { 0 }; + +#if HAVE_LOCALTIME_R + time_t now = time(0); + struct tm ltime; + uint8_t *buf = dtext->expanded_text; + int buf_size = dtext->expanded_text_size; + + if (!buf) + buf_size = 2*strlen(dtext->text)+1; + + localtime_r(&now, <ime); + + while ((buf = av_realloc(buf, buf_size))) { + *buf = 1; + if (strftime(buf, buf_size, dtext->text, <ime) != 0 || *buf == 0) + break; + buf_size *= 2; + } + + if (!buf) + return AVERROR(ENOMEM); + text = dtext->expanded_text = buf; + dtext->expanded_text_size = buf_size; +#endif + + if ((len = strlen(text)) > dtext->nb_positions) { + FT_Vector *p = av_realloc(dtext->positions, + len * sizeof(*dtext->positions)); + if (!p) { + av_freep(dtext->positions); + dtext->nb_positions = 0; + return AVERROR(ENOMEM); + } else { + dtext->positions = p; + dtext->nb_positions = len; + } + } + + /* load and cache glyphs */ + for (i = 0, p = text; *p; i++) { + GET_UTF8(code, *p++, continue;); + + /* get glyph */ + dummy.code = code; + glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL); + if (!glyph) + ret = load_glyph(ctx, &glyph, code); + if (ret) return ret; + + y_min = FFMIN(glyph->bbox.yMin, y_min); + y_max = FFMAX(glyph->bbox.yMax, y_max); + } + text_height = y_max - y_min; + baseline = y_max; + + /* compute and save position for each glyph */ + glyph = NULL; + for (i = 0, p = text; *p; i++) { + GET_UTF8(code, *p++, continue;); + + /* skip the \n in the sequence \r\n */ + if (prev_code == '\r' && code == '\n') + continue; + + prev_code = code; + if (is_newline(code)) { + str_w = FFMAX(str_w, x - dtext->x); + y += text_height; + x = 0; + continue; + } + + /* get glyph */ + prev_glyph = glyph; + dummy.code = code; + glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL); + + /* kerning */ + if (dtext->use_kerning && prev_glyph && glyph->code) { + FT_Get_Kerning(dtext->face, prev_glyph->code, glyph->code, + ft_kerning_default, &delta); + x += delta.x >> 6; + } + + if (x + glyph->bbox.xMax >= width) { + str_w = FFMAX(str_w, x); + y += text_height; + x = 0; + } + + /* save position */ + dtext->positions[i].x = x + glyph->bitmap_left; + dtext->positions[i].y = y - glyph->bitmap_top + baseline; + if (code == '\t') x = (x / dtext->tabsize + 1)*dtext->tabsize; + else x += glyph->advance; + } + + str_w = FFMIN(width - 1, FFMAX(str_w, x)); + y = FFMIN(y + text_height, height - 1); + + dtext->w = str_w; + dtext->h = y; + + return 0; +} + + static int config_input(AVFilterLink *inlink) { - DrawTextContext *dtext = inlink->dst->priv; + AVFilterContext *ctx = inlink->dst; + DrawTextContext *dtext = ctx->priv; const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format]; int ret; @@ -398,7 +524,7 @@ static int config_input(AVFilterLink *inlink) dtext->shadowcolor[3] = rgba[3]; } - return 0; + return dtext_prepare_text(ctx, ctx->inputs[0]->w, ctx->inputs[0]->h); } #define GET_BITMAP_VAL(r, c) \ @@ -499,11 +625,6 @@ static inline void drawbox(AVFilterBufferRef *picref, unsigned int x, unsigned i } } -static inline int is_newline(uint32_t c) -{ - return (c == '\n' || c == '\r' || c == '\f' || c == '\v'); -} - static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref, int width, int height, const uint8_t rgbcolor[4], const uint8_t yuvcolor[4], int x, int y) { @@ -542,128 +663,6 @@ static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref, return 0; } -static int dtext_prepare_text(AVFilterContext *ctx, int width, int height) -{ - DrawTextContext *dtext = ctx->priv; - uint32_t code = 0, prev_code = 0; - int x = 0, y = 0, i = 0, ret; - int text_height, baseline; - char *text = dtext->text; - uint8_t *p; - int str_w = 0, len; - int y_min = 32000, y_max = -32000; - FT_Vector delta; - Glyph *glyph = NULL, *prev_glyph = NULL; - Glyph dummy = { 0 }; - -#if HAVE_LOCALTIME_R - time_t now = time(0); - struct tm ltime; - uint8_t *buf = dtext->expanded_text; - int buf_size = dtext->expanded_text_size; - - if (!buf) { - buf_size = 2*strlen(dtext->text)+1; - buf = av_malloc(buf_size); - } - - localtime_r(&now, <ime); - - do { - *buf = 1; - if (strftime(buf, buf_size, dtext->text, <ime) != 0 || *buf == 0) - break; - buf_size *= 2; - } while ((buf = av_realloc(buf, buf_size))); - - if (!buf) - return AVERROR(ENOMEM); - text = dtext->expanded_text = buf; - dtext->expanded_text_size = buf_size; -#endif - - if ((len = strlen(text)) > dtext->nb_positions) { - FT_Vector *p = av_realloc(dtext->positions, - len * sizeof(*dtext->positions)); - if (!p) { - av_freep(dtext->positions); - dtext->nb_positions = 0; - return AVERROR(ENOMEM); - } else { - dtext->positions = p; - dtext->nb_positions = len; - } - } - - /* load and cache glyphs */ - for (i = 0, p = text; *p; i++) { - GET_UTF8(code, *p++, continue;); - - /* get glyph */ - dummy.code = code; - glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL); - if (!glyph) - ret = load_glyph(ctx, &glyph, code); - if (ret) return ret; - - y_min = FFMIN(glyph->bbox.yMin, y_min); - y_max = FFMAX(glyph->bbox.yMax, y_max); - } - text_height = y_max - y_min; - baseline = y_max; - - /* compute and save position for each glyph */ - glyph = NULL; - for (i = 0, p = text; *p; i++) { - GET_UTF8(code, *p++, continue;); - - /* skip the \n in the sequence \r\n */ - if (prev_code == '\r' && code == '\n') - continue; - - prev_code = code; - if (is_newline(code)) { - str_w = FFMAX(str_w, x - dtext->x); - y += text_height; - x = 0; - continue; - } - - /* get glyph */ - prev_glyph = glyph; - dummy.code = code; - glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL); - - /* kerning */ - if (dtext->use_kerning && prev_glyph && glyph->code) { - FT_Get_Kerning(dtext->face, prev_glyph->code, glyph->code, - ft_kerning_default, &delta); - x += delta.x >> 6; - } - - if (x + glyph->bbox.xMax >= width) { - str_w = FFMAX(str_w, x); - y += text_height; - x = 0; - } - - /* save position */ - dtext->positions[i].x = x + glyph->bitmap_left; - dtext->positions[i].y = y - glyph->bitmap_top + baseline; - if (code == '\t') x = (x / dtext->tabsize + 1)*dtext->tabsize; - else x += glyph->advance; - } - - str_w = FFMIN(width - 1, FFMAX(str_w, x)); - y = FFMIN(y + text_height, height - 1); - - dtext->w = str_w; - dtext->h = y; - - return 0; -} - - static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, int width, int height) { @@ -702,9 +701,10 @@ static void end_frame(AVFilterLink *inlink) { AVFilterLink *outlink = inlink->dst->outputs[0]; AVFilterBufferRef *picref = inlink->cur_buf; - - dtext_prepare_text(inlink->dst, picref->video->w, picref->video->h); - draw_text(inlink->dst, picref, picref->video->w, picref->video->h); + int err = dtext_prepare_text(inlink->dst, + picref->video->w, picref->video->h); + if (!err) + draw_text(inlink->dst, picref, picref->video->w, picref->video->h); avfilter_draw_slice(outlink, 0, picref->video->h, 1); avfilter_end_frame(outlink); From 2cf74eca70759320200f06efe4a09bd69e169dec Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Thu, 1 Dec 2011 11:43:11 +0100 Subject: [PATCH 4/7] drawtext: make x and y parametric Introduce variables "E", "PHI", "PI", "main_w"/"W", "main_h"/"H", "text_w"/"w", "text_h"/"h", "x", "y", "n" and "t" in line with vf_overlay and refactor the code accordingly. --- doc/filters.texi | 27 ++++++++ libavfilter/vf_drawtext.c | 133 +++++++++++++++++++++++++++++++++++--- 2 files changed, 152 insertions(+), 8 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index 4b38813fed..101bec6d88 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -505,6 +505,32 @@ If both text and textfile are specified, an error is thrown. @item x, y The offsets where text will be drawn within the video frame. Relative to the top/left border of the output image. +They accept expressions similar to the @ref{overlay} filter: +@table @option + +@item x, y +the computed values for @var{x} and @var{y}. They are evaluated for +each new frame. + +@item main_w, main_h +main input width and height + +@item W, H +same as @var{main_w} and @var{main_h} + +@item text_w, text_h +rendered text width and height + +@item w, h +same as @var{text_w} and @var{text_h} + +@item n +the number of frames processed, starting from 0 + +@item t +timestamp expressed in seconds, NAN if the input timestamp is unknown + +@end table The default value of @var{x} and @var{y} is 0. @@ -1048,6 +1074,7 @@ other parameters is 0. These parameters correspond to the parameters assigned to the libopencv function @code{cvSmooth}. +@anchor{overlay} @section overlay Overlay one video on top of another. diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c index 06b5dc0ba1..b7ba89c944 100644 --- a/libavfilter/vf_drawtext.c +++ b/libavfilter/vf_drawtext.c @@ -31,7 +31,9 @@ #include "libavutil/colorspace.h" #include "libavutil/file.h" +#include "libavutil/eval.h" #include "libavutil/opt.h" +#include "libavutil/mathematics.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "libavutil/tree.h" @@ -45,6 +47,36 @@ #include FT_FREETYPE_H #include FT_GLYPH_H +static const char *var_names[] = { + "E", + "PHI", + "PI", + "main_w", "W", ///< width of the main video + "main_h", "H", ///< height of the main video + "text_w", "w", ///< width of the overlay text + "text_h", "h", ///< height of the overlay text + "x", + "y", + "n", ///< number of processed frames + "t", ///< timestamp expressed in seconds + NULL +}; + +enum var_name { + VAR_E, + VAR_PHI, + VAR_PI, + VAR_MAIN_W, VAR_MW, + VAR_MAIN_H, VAR_MH, + VAR_TEXT_W, VAR_TW, + VAR_TEXT_H, VAR_TH, + VAR_X, + VAR_Y, + VAR_N, + VAR_T, + VAR_VARS_NB +}; + typedef struct { const AVClass *class; uint8_t *fontfile; ///< font to be used @@ -81,6 +113,10 @@ typedef struct { int pixel_step[4]; ///< distance in bytes between the component of each pixel uint8_t rgba_map[4]; ///< map RGBA offsets to the positions in the packed RGBA format uint8_t *box_line[4]; ///< line used for filling the box background + char *x_expr, *y_expr; + AVExpr *x_pexpr, *y_pexpr; ///< parsed expressions for x and y + double var_values[VAR_VARS_NB]; + int draw; ///< set to zero to prevent drawing } DrawTextContext; #define OFFSET(x) offsetof(DrawTextContext, x) @@ -94,8 +130,8 @@ static const AVOption drawtext_options[]= { {"shadowcolor", "set shadow color", OFFSET(shadowcolor_string), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX }, {"box", "set box", OFFSET(draw_box), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 }, {"fontsize", "set font size", OFFSET(fontsize), AV_OPT_TYPE_INT, {.dbl=16}, 1, 72 }, -{"x", "set x", OFFSET(x), AV_OPT_TYPE_INT, {.dbl=0}, 0, INT_MAX }, -{"y", "set y", OFFSET(y), AV_OPT_TYPE_INT, {.dbl=0}, 0, INT_MAX }, +{"x", "set x", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX }, +{"y", "set y", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX }, {"shadowx", "set x", OFFSET(shadowx), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX }, {"shadowy", "set y", OFFSET(shadowy), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX }, {"tabsize", "set tab size", OFFSET(tabsize), AV_OPT_TYPE_INT, {.dbl=4}, 0, INT_MAX }, @@ -374,7 +410,7 @@ static inline int is_newline(uint32_t c) return (c == '\n' || c == '\r' || c == '\f' || c == '\v'); } -static int dtext_prepare_text(AVFilterContext *ctx, int width, int height) +static int dtext_prepare_text(AVFilterContext *ctx) { DrawTextContext *dtext = ctx->priv; uint32_t code = 0, prev_code = 0; @@ -387,6 +423,8 @@ static int dtext_prepare_text(AVFilterContext *ctx, int width, int height) FT_Vector delta; Glyph *glyph = NULL, *prev_glyph = NULL; Glyph dummy = { 0 }; + int width = ctx->inputs[0]->w; + int height = ctx->inputs[0]->h; #if HAVE_LOCALTIME_R time_t now = time(0); @@ -504,6 +542,27 @@ static int config_input(AVFilterLink *inlink) dtext->hsub = pix_desc->log2_chroma_w; dtext->vsub = pix_desc->log2_chroma_h; + dtext->var_values[VAR_E ] = M_E; + dtext->var_values[VAR_PHI] = M_PHI; + dtext->var_values[VAR_PI ] = M_PI; + + dtext->var_values[VAR_MAIN_W] = + dtext->var_values[VAR_MW] = ctx->inputs[0]->w; + dtext->var_values[VAR_MAIN_H] = + dtext->var_values[VAR_MH] = ctx->inputs[0]->h; + + dtext->var_values[VAR_X] = 0; + dtext->var_values[VAR_Y] = 0; + dtext->var_values[VAR_N] = 0; + dtext->var_values[VAR_T] = NAN; + + + if ((ret = av_expr_parse(&dtext->x_pexpr, dtext->x_expr, var_names, + NULL, NULL, NULL, NULL, 0, ctx)) < 0 || + (ret = av_expr_parse(&dtext->y_pexpr, dtext->y_expr, var_names, + NULL, NULL, NULL, NULL, 0, ctx)) < 0) + return AVERROR(EINVAL); + if ((ret = ff_fill_line_with_color(dtext->box_line, dtext->pixel_step, inlink->w, dtext->boxcolor, @@ -524,7 +583,9 @@ static int config_input(AVFilterLink *inlink) dtext->shadowcolor[3] = rgba[3]; } - return dtext_prepare_text(ctx, ctx->inputs[0]->w, ctx->inputs[0]->h); + dtext->draw = 1; + + return dtext_prepare_text(ctx); } #define GET_BITMAP_VAL(r, c) \ @@ -697,15 +758,71 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { } +static inline int normalize_double(int *n, double d) +{ + int ret = 0; + + if (isnan(d)) { + ret = AVERROR(EINVAL); + } else if (d > INT_MAX || d < INT_MIN) { + *n = d > INT_MAX ? INT_MAX : INT_MIN; + ret = AVERROR(EINVAL); + } else + *n = round(d); + + return ret; +} + +static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref) +{ + AVFilterContext *ctx = inlink->dst; + DrawTextContext *dtext = ctx->priv; + + if (dtext_prepare_text(ctx) < 0) { + av_log(ctx, AV_LOG_ERROR, "Can't draw text\n"); + dtext->draw = 0; + } + + dtext->var_values[VAR_T] = inpicref->pts == AV_NOPTS_VALUE ? + NAN : inpicref->pts * av_q2d(inlink->time_base); + dtext->var_values[VAR_X] = + av_expr_eval(dtext->x_pexpr, dtext->var_values, NULL); + dtext->var_values[VAR_Y] = + av_expr_eval(dtext->y_pexpr, dtext->var_values, NULL); + dtext->var_values[VAR_X] = + av_expr_eval(dtext->x_pexpr, dtext->var_values, NULL); + + normalize_double(&dtext->x, dtext->var_values[VAR_X]); + normalize_double(&dtext->y, dtext->var_values[VAR_Y]); + + if (dtext->x < 0) dtext->x = 0; + if (dtext->y < 0) dtext->y = 0; + if ((unsigned)dtext->x + (unsigned)dtext->w > inlink->w) + dtext->x = inlink->w - dtext->w; + if ((unsigned)dtext->y + (unsigned)dtext->h > inlink->h) + dtext->y = inlink->h - dtext->h; + + dtext->x &= ~((1 << dtext->hsub) - 1); + dtext->y &= ~((1 << dtext->vsub) - 1); + + av_dlog(ctx, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n", + (int)dtext->var_values[VAR_N], dtext->var_values[VAR_T], + dtext->x, dtext->y, dtext->x+dtext->w, dtext->y+dtext->h); + + avfilter_start_frame(inlink->dst->outputs[0], inpicref); +} + static void end_frame(AVFilterLink *inlink) { AVFilterLink *outlink = inlink->dst->outputs[0]; AVFilterBufferRef *picref = inlink->cur_buf; - int err = dtext_prepare_text(inlink->dst, - picref->video->w, picref->video->h); - if (!err) + DrawTextContext *dtext = inlink->dst->priv; + + if (dtext->draw) draw_text(inlink->dst, picref, picref->video->w, picref->video->h); + dtext->var_values[VAR_N] += 1.0; + avfilter_draw_slice(outlink, 0, picref->video->h, 1); avfilter_end_frame(outlink); } @@ -721,7 +838,7 @@ AVFilter avfilter_vf_drawtext = { .inputs = (AVFilterPad[]) {{ .name = "default", .type = AVMEDIA_TYPE_VIDEO, .get_video_buffer = avfilter_null_get_video_buffer, - .start_frame = avfilter_null_start_frame, + .start_frame = start_frame, .draw_slice = null_draw_slice, .end_frame = end_frame, .config_props = config_input, From a0338b598b123864cbb814f3de88080ec9215a09 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sat, 3 Dec 2011 01:18:28 +0100 Subject: [PATCH 5/7] doc: break some long lines in developer.texi --- doc/developer.texi | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/doc/developer.texi b/doc/developer.texi index fe1f1a2652..a63bea746c 100644 --- a/doc/developer.texi +++ b/doc/developer.texi @@ -49,8 +49,8 @@ mailing list. The code is written in K&R C style. That means the following: @itemize @bullet @item -The control statements are formatted by putting space betwen the statement and parenthesis -in the following way: +The control statements are formatted by putting space betwen the statement +and parenthesis in the following way: @example for (i = 0; i < filter->input_count; i++) @{ @end example @@ -79,7 +79,8 @@ if (!pic || !picref) goto fail; @end example @item -Do not put spaces immediately inside parenthesis. @samp{if (ret)} is a valid style; @samp{if ( ret )} is not. +Do not put spaces immediately inside parenthesis. @samp{if (ret)} is +a valid style; @samp{if ( ret )} is not. @end itemize There are the following guidelines regarding the indentation in files: @@ -91,7 +92,8 @@ The TAB character is forbidden outside of Makefiles as is any form of trailing whitespace. Commits containing either will be rejected by the git repository. @item -You should try to limit your code lines to 80 characters; however, do so if and only if this improves readability. +You should try to limit your code lines to 80 characters; however, do so if +and only if this improves readability. @end itemize The presentation is one inspired by 'indent -i4 -kr -nut'. @@ -167,9 +169,10 @@ GCC statement expressions (@samp{(x = (@{ int y = 4; y; @})}). @end itemize @subsection Naming conventions -All names are using underscores (_), not CamelCase. For example, @samp{avfilter_get_video_buffer} is -a valid function name and @samp{AVFilterGetVideo} is not. The only exception from this are structure names; -they should always be in the CamelCase +All names are using underscores (_), not CamelCase. For example, +@samp{avfilter_get_video_buffer} is a valid function name and +@samp{AVFilterGetVideo} is not. The only exception from this are structure +names; they should always be in the CamelCase There are following conventions for naming variables and functions: @itemize @bullet @@ -178,13 +181,15 @@ For local variables no prefix is required. @item For variables and functions declared as @code{static} no prefixes are required. @item -For variables and functions used internally by the library, @code{ff_} prefix should be used. +For variables and functions used internally by the library, @code{ff_} prefix +should be used. For example, @samp{ff_w64_demuxer}. @item -For variables and functions used internally across multiple libraries, use @code{avpriv_}. For example, -@samp{avpriv_aac_parse_header}. +For variables and functions used internally across multiple libraries, use +@code{avpriv_}. For example, @samp{avpriv_aac_parse_header}. @item -For exported names, each library has its own prefixes. Just check the existing code and name accordingly. +For exported names, each library has its own prefixes. Just check the existing +code and name accordingly. @end itemize @subsection Miscellanous conventions From 55a280569f0e886d62076a60888601ab9a7a4b42 Mon Sep 17 00:00:00 2001 From: Derek Buitenhuis Date: Thu, 1 Dec 2011 20:24:08 +0000 Subject: [PATCH 6/7] fate: Add a test for the VBLE decoder Signed-off-by: Derek Buitenhuis Signed-off-by: Janne Grunau --- tests/fate2.mak | 3 +++ tests/ref/fate/vble | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 tests/ref/fate/vble diff --git a/tests/fate2.mak b/tests/fate2.mak index 376a0196b6..2881eb15b8 100644 --- a/tests/fate2.mak +++ b/tests/fate2.mak @@ -226,3 +226,6 @@ fate-musepack7: FUZZ = 1 FATE_TESTS += fate-iirfilter fate-iirfilter: libavcodec/iirfilter-test$(EXESUF) fate-iirfilter: CMD = run libavcodec/iirfilter-test + +FATE_TESTS += fate-vble +fate-vble: CMD = framecrc -i $(SAMPLES)/vble/flowers-partial-2MB.avi diff --git a/tests/ref/fate/vble b/tests/ref/fate/vble new file mode 100644 index 0000000000..748052e3c4 --- /dev/null +++ b/tests/ref/fate/vble @@ -0,0 +1,4 @@ +0, 0, 1382400, 0x5e1bc307 +0, 3003, 1382400, 0x198795f7 +0, 6006, 1382400, 0xa9102ac2 +0, 9009, 1382400, 0x9e347932 From adedd840e20cbcf6b23b41d415581dc03bcce4c6 Mon Sep 17 00:00:00 2001 From: "Ronald S. Bultje" Date: Tue, 29 Nov 2011 22:22:44 -0800 Subject: [PATCH 7/7] h264: fix frame reordering code. Fixes fate-h264-conformance-{mr2_tandberg_e,mr3_tandberg_b} without requiring -strict 1. --- libavcodec/h264.c | 67 +++++++++++++++++++++++++++++++----------- libavcodec/h264.h | 1 + libavcodec/h264_refs.c | 6 +--- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/libavcodec/h264.c b/libavcodec/h264.c index 96062b7806..acd7179cc0 100644 --- a/libavcodec/h264.c +++ b/libavcodec/h264.c @@ -1348,6 +1348,7 @@ static void decode_postinit(H264Context *h, int setup_finished){ Picture *out = s->current_picture_ptr; Picture *cur = s->current_picture_ptr; int i, pics, out_of_order, out_idx; + int invalid = 0, cnt = 0; s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_H264; s->current_picture_ptr->f.pict_type = s->pict_type; @@ -1451,31 +1452,54 @@ static void decode_postinit(H264Context *h, int setup_finished){ if (cur->f.reference == 0) cur->f.reference = DELAYED_PIC_REF; + /* Frame reordering. This code takes pictures from coding order and sorts + * them by their incremental POC value into display order. It supports POC + * gaps, MMCO reset codes and random resets. + * A "display group" can start either with a IDR frame (f.key_frame = 1), + * and/or can be closed down with a MMCO reset code. In sequences where + * there is no delay, we can't detect that (since the frame was already + * output to the user), so we also set h->mmco_reset to detect the MMCO + * reset code. + * FIXME: if we detect insufficient delays (as per s->avctx->has_b_frames), + * we increase the delay between input and output. All frames affected by + * the lag (e.g. those that should have been output before another frame + * that we already returned to the user) will be dropped. This is a bug + * that we will fix later. */ + for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) { + cnt += out->poc < h->last_pocs[i]; + invalid += out->poc == INT_MIN; + } + if (!h->mmco_reset && !cur->f.key_frame && cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) { + h->mmco_reset = 2; + if (pics > 1) + h->delayed_pic[pics - 2]->mmco_reset = 2; + } + if (h->mmco_reset || cur->f.key_frame) { + for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) + h->last_pocs[i] = INT_MIN; + cnt = 0; + invalid = MAX_DELAYED_PIC_COUNT; + } out = h->delayed_pic[0]; out_idx = 0; - for (i = 1; h->delayed_pic[i] && !h->delayed_pic[i]->f.key_frame && !h->delayed_pic[i]->mmco_reset; i++) + for (i = 1; i < MAX_DELAYED_PIC_COUNT && h->delayed_pic[i] && + !h->delayed_pic[i-1]->mmco_reset && !h->delayed_pic[i]->f.key_frame; i++) + { if(h->delayed_pic[i]->poc < out->poc){ out = h->delayed_pic[i]; out_idx = i; } - if (s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) - h->next_outputed_poc= INT_MIN; - out_of_order = out->poc < h->next_outputed_poc; + } + if (s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->f.key_frame || h->mmco_reset)) + h->next_outputed_poc = INT_MIN; + out_of_order = !out->f.key_frame && !h->mmco_reset && (out->poc < h->next_outputed_poc); if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames) { } else if (out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) { - int cnt = 0, invalid = 0; - for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) { - cnt += out->poc < h->last_pocs[i]; - invalid += h->last_pocs[i] == INT_MIN; - } if (invalid + cnt < MAX_DELAYED_PIC_COUNT) { s->avctx->has_b_frames = FFMAX(s->avctx->has_b_frames, cnt); - } else if (cnt) { - for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) - h->last_pocs[i] = INT_MIN; } s->low_delay = 0; } else if (s->low_delay && @@ -1485,7 +1509,7 @@ static void decode_postinit(H264Context *h, int setup_finished){ s->avctx->has_b_frames++; } - if(out_of_order || pics > s->avctx->has_b_frames){ + if(pics > s->avctx->has_b_frames){ out->f.reference &= ~DELAYED_PIC_REF; out->owner2 = s; // for frame threading, the owner must be the second field's thread // or else the first thread can release the picture and reuse it unsafely @@ -1493,13 +1517,20 @@ static void decode_postinit(H264Context *h, int setup_finished){ h->delayed_pic[i] = h->delayed_pic[i+1]; } memmove(h->last_pocs, &h->last_pocs[1], sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1)); - h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = out->poc; + h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc; if(!out_of_order && pics > s->avctx->has_b_frames){ h->next_output_pic = out; - if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) { - h->next_outputed_poc = INT_MIN; - } else + if (out->mmco_reset) { + if (out_idx > 0) { + h->next_outputed_poc = out->poc; + h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset; + } else { + h->next_outputed_poc = INT_MIN; + } + } else { h->next_outputed_poc = out->poc; + } + h->mmco_reset = 0; }else{ av_log(s->avctx, AV_LOG_DEBUG, "no picture\n"); } @@ -2353,6 +2384,8 @@ static void flush_dpb(AVCodecContext *avctx){ h->delayed_pic[i]->f.reference = 0; h->delayed_pic[i]= NULL; } + for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) + h->last_pocs[i] = INT_MIN; h->outputed_poc=h->next_outputed_poc= INT_MIN; h->prev_interlaced_frame = 1; idr(h); diff --git a/libavcodec/h264.h b/libavcodec/h264.h index caea7ba7eb..5280e5155a 100644 --- a/libavcodec/h264.h +++ b/libavcodec/h264.h @@ -498,6 +498,7 @@ typedef struct H264Context{ */ MMCO mmco[MAX_MMCO_COUNT]; int mmco_index; + int mmco_reset; int long_ref_count; ///< number of actual long term references int short_ref_count; ///< number of actual short term references diff --git a/libavcodec/h264_refs.c b/libavcodec/h264_refs.c index 370b5c312d..273c52b475 100644 --- a/libavcodec/h264_refs.c +++ b/libavcodec/h264_refs.c @@ -582,13 +582,9 @@ int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){ for(j = 0; j < 16; j++) { remove_long(h, j, 0); } - s->current_picture_ptr->poc= - s->current_picture_ptr->field_poc[0]= - s->current_picture_ptr->field_poc[1]= - h->poc_lsb= - h->poc_msb= h->frame_num= s->current_picture_ptr->frame_num= 0; + h->mmco_reset = 1; s->current_picture_ptr->mmco_reset=1; break; default: assert(0);