[FFmpeg-devel] [PATCH 2/3] lavfi/drawtext: use bprint for the expanded text.

Clément Bœsch ubitux at gmail.com
Sat Nov 10 21:49:30 CET 2012


On Sat, Nov 10, 2012 at 07:49:46PM +0100, Nicolas George wrote:
> 
> Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
> ---
>  libavfilter/vf_drawtext.c |   37 +++++++++++++++----------------------
>  1 file changed, 15 insertions(+), 22 deletions(-)
> 
> diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
> index 849bf13..f5db13c 100644
> --- a/libavfilter/vf_drawtext.c
> +++ b/libavfilter/vf_drawtext.c
> @@ -31,6 +31,7 @@
>  
>  #include "config.h"
>  #include "libavutil/avstring.h"
> +#include "libavutil/bprint.h"
>  #include "libavutil/common.h"
>  #include "libavutil/file.h"
>  #include "libavutil/eval.h"
> @@ -117,8 +118,7 @@ typedef struct {
>      int reinit;                     ///< tells if the filter is being reinited
>      uint8_t *fontfile;              ///< font to be used
>      uint8_t *text;                  ///< text to be drawn
> -    uint8_t *expanded_text;         ///< used to contain the strftime()-expanded text
> -    size_t   expanded_text_size;    ///< size in bytes of the expanded_text buffer
> +    AVBPrint expanded_text;         ///< used to contain the expanded text
>      int ft_load_flags;              ///< flags used for loading fonts, see FT_LOAD_*
>      FT_Vector *positions;           ///< positions for each element in the text
>      size_t nb_positions;            ///< number of elements of positions array
> @@ -484,6 +484,8 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
>      }
>      dtext->tabsize *= glyph->advance;
>  
> +    av_bprint_init(&dtext->expanded_text, 0, -1);

nit: isn't explicit "UNLIMITED" macro better?

> +
>      return 0;
>  }
>  
> @@ -521,6 +523,8 @@ static av_cold void uninit(AVFilterContext *ctx)
>  
>      FT_Done_Face(dtext->face);
>      FT_Done_FreeType(dtext->library);
> +
> +    av_bprint_finalize(&dtext->expanded_text, NULL);
>  }
>  
>  static inline int is_newline(uint32_t c)
> @@ -584,7 +588,7 @@ static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char
>  static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref,
>                         int width, int height, const uint8_t rgbcolor[4], FFDrawColor *color, int x, int y)
>  {
> -    char *text = dtext->expanded_text;
> +    char *text = dtext->expanded_text.str;
>      uint32_t code = 0;
>      int i, x1, y1;
>      uint8_t *p;
> @@ -637,42 +641,31 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
>  
>      time_t now = time(0);
>      struct tm ltime;
> -    uint8_t *buf = dtext->expanded_text;
> -    int buf_size = dtext->expanded_text_size;
> +    AVBPrint *bp = &dtext->expanded_text;
> +
> +    av_bprint_clear(bp);
>  
>      if(dtext->basetime != AV_NOPTS_VALUE)
>          now= picref->pts*av_q2d(ctx->inputs[0]->time_base) + dtext->basetime/1000000;
>  
> -    if (!buf) {
> -        buf_size = 2*strlen(dtext->text)+1;
> -        buf = av_malloc(buf_size);
> -    }
> -

I was pretty annoyed by that buf variable, thanks for taking care of it :)

>  #if HAVE_LOCALTIME_R
>      localtime_r(&now, &ltime);
>  #else
>      if(strchr(dtext->text, '%'))
>          ltime= *localtime(&now);
>  #endif
> -
> -    do {
> -        *buf = 1;
> -        if (strftime(buf, buf_size, dtext->text, &ltime) != 0 || *buf == 0)
> -            break;
> -        buf_size *= 2;
> -    } while ((buf = av_realloc(buf, buf_size)));
> +    av_bprint_strftime(bp, dtext->text, &ltime);
>  
>      if (dtext->tc_opt_string) {
>          char tcbuf[AV_TIMECODE_STR_SIZE];
>          av_timecode_make_string(&dtext->tc, tcbuf, dtext->frame_id++);
> -        av_free(buf);
> -        buf = av_asprintf("%s%s", dtext->text, tcbuf);
> +        av_bprint_clear(bp);
> +        av_bprintf(bp, "%s%s", dtext->text, tcbuf);
>      }
>  
> -    if (!buf)
> +    if (!av_bprint_is_complete(bp))
>          return AVERROR(ENOMEM);
> -    text = dtext->expanded_text = buf;
> -    dtext->expanded_text_size = buf_size;
> +    text = dtext->expanded_text.str;
>      if ((len = strlen(text)) > dtext->nb_positions) {

dtext->expanded_text.len?

[...]

OK overall.

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121110/570194a3/attachment.asc>


More information about the ffmpeg-devel mailing list