[FFmpeg-soc] libavfilter audio work - qualification task

Stefano Sabatini stefano.sabatini-lala at poste.it
Thu Apr 22 00:35:26 CEST 2010


On date Wednesday 2010-04-21 07:55:16 -0700, S.N. Hemanth Meenakshisundaram encoded:
[...]
> Also fixed alignment and style wherever non standard. Please review
> and let me know if there's anything else to be fixed. I will now
> work on fixing the corruption in vf_yadif. The complete diff for
> drawtext, documentation, Makefile/allfilters changes is attached
> below.

> Index: allfilters.c
> ===================================================================
> --- allfilters.c	(revision 5734)
> +++ allfilters.c	(working copy)
> @@ -37,6 +37,7 @@
>      REGISTER_FILTER (ASPECT,      aspect,      vf);
>      REGISTER_FILTER (CROP,        crop,        vf);
>      REGISTER_FILTER (DRAWBOX,     drawbox,     vf);
> +    REGISTER_FILTER (DRAWTEXT,    drawtext,    vf);
>      REGISTER_FILTER (FIFO,        fifo,        vf);
>      REGISTER_FILTER (FORMAT,      format,      vf);
>      REGISTER_FILTER (FPS,         fps,         vf);
> Index: diffs/03_libavfilter_doc.diff
> ===================================================================
> --- diffs/03_libavfilter_doc.diff	(revision 5734)
> +++ diffs/03_libavfilter_doc.diff	(working copy)
> @@ -1,8 +1,8 @@
> -Index: doc/libavfilter.texi
> +Index: libavfilter.texi
>  ===================================================================
> ---- doc/libavfilter.texi	(revision 22749)
> -+++ doc/libavfilter.texi	(working copy)
> -@@ -139,6 +139,20 @@
> +--- libavfilter.texi	(revision 22749)
> ++++ libavfilter.texi	(working copy)
> +@@ -139,6 +139,101 @@
>   
>   The default value of ``width'' and ``height'' is 0.
>   
> @@ -14,6 +14,87 @@
>  +
>  +Draw a box with x:y:width:height dimensions in a chosen color.
>  +

Please provide a patch against the libavfilter soc trunk, it's so hard
to read a patch of a patch.

> ++ at section drawtext
> ++
> ++Draws text string or text from specified file on top of video.
> ++
> ++It accepts the following parameters:
> ++f=<font file>:t=<text>:T=<text file>:x=<x offset>:y=<y offset>:
> ++c=<foreground color>:C=<background color>:s=<font size>:b=<draw box>:
> ++o=<draw outline>
> ++
> ++f and t are mandatory parameters.
> ++
> ++The description of the accepted parameters follows.
> ++
> ++ at table @option
> ++ at item f
> ++
> ++Specifies the font file to be used for drawing text. Path must be included.
> ++This parameter is mandatory.
> ++
> ++ at item t
> ++
> ++Specifies the text string to be drawn.
> ++This parameter is mandatory and will be used if no file with text is
> ++specified or if the file fails to open.
> ++
> ++ at item T

$var{T}, same for the other vars.

Also I'm never happy about one char variables, think at reading a
script containing such variables, that's why I prefer reasonably short
but *explicative* var names.

> ++
> ++Specifies a text file containing text to be drawn. Max size of 1024
> ++characters.
> ++
> ++ at item x, y
> ++
> ++Specify the offsets where text will be drawn within the video
> ++frame. Relative to the top/left border of the output image.
> ++
> ++The default value of ``x'' and ``y'' is 0.

$var{x} and $var{y} (yes the rest of the docs should be updated as
well).

> ++
> ++ at item s
> ++
> ++Specifies the font size to be used for drawing text.
> ++
> ++The default value of ``s'' is 16.
> ++
> ++ at item c
> ++
> ++Specifies the foreground color to be used for drawing text.
> ++Specify either as a string (e.g. black or as a hex value e.g. FF0000)
> ++
> ++The default value of ``c'' is black.
> ++
> ++ at item C
> ++
> ++Specifies the background color to be used for drawing box around text
> ++or drawing text outline based on option selected.
> ++Specify either as a string (e.g. black or as a 0xRRGGBB e.g. 0xFF0000)
> ++
> ++The default value of ``C'' is white.
> ++
> ++ at item b
> ++
> ++Specifies whether to draw a box around text using background color.
> ++Value should be either 1(enable) or 0(disable).
> ++
> ++The default value of ``b'' is 0.
> ++
> ++ at item o
> ++
> ++Specifies whether to draw an outline around text using background color.
> ++Value should be either 1(enable) or 0(disable).
> ++
> ++The default value of ``o'' is 0.
> ++
> ++ at end table
> ++ at example
> ++./ffmpeg -i in.avi -vfilters drawtext=f=FreeSerif.ttf:t=TestText
> ++            :x=100:y=50:s=24:c=Yellow:C=Red:b=1 out.avi
> ++ at end example

I generally prefer avoid to mention ff* tools at all, this is the doc
for the filter, the same syntax may be used by a different
library/app.

Also use spaces to improve poor human eye-parsing, I mean something of
the kind:

"drawtext= font=FreeSerif.ttf: text=TestText: x=100: y=50: size=24:
bgcolor=yellow: fgcolor=red : box=1"

> ++
> ++Draw 'TestText' with font FreeSerif of size 24 at (100,50), text color is yellow,
> ++background color is red, draw a box around text.
> ++
>  + at section fifo
>  +
>  + at example
> @@ -23,7 +104,7 @@

[...]
> Index: vf_drawtext.c
> ===================================================================
> --- vf_drawtext.c	(revision 0)
> +++ vf_drawtext.c	(revision 0)
> @@ -0,0 +1,510 @@
> +/*
> + * copyright (c) 2010 S.N. Hemanth Meenakshisundaram
> + * Original vhook author: Gustavo Sverzut Barbieri <gsbarbieri at yahoo.com.br>
> + * Libavfilter version  : S.N. Hemanth Meenakshisundaram <smeenaks at ucsd.edu>
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +/**
> + * @file libavfilter/vf_drawtext.c

"@file" alone should be enough.

> + * video filter to draw text over screen
> + */
> +
> +#define MAXSIZE_TEXT 1024
> +
> +#include "avfilter.h"
> +#include "parseutils.h"
> +#include "libavcodec/colorspace.h"
> +#include "libavutil/pixdesc.h"
> +
> +#undef time
> +#include <sys/time.h>
> +#include <time.h>
> +
> +#include <ft2build.h>
> +#include <freetype/config/ftheader.h>
> +#include FT_FREETYPE_H
> +#include FT_GLYPH_H
> +
> +typedef struct {
> +    unsigned char *text;            ///< text to be drawn
> +    char *file;                     ///< file with text to be drawn
> +    unsigned int x;                 ///< x position to start drawing text
> +    unsigned int y;                 ///< y position to start drawing text
> +    unsigned char bgcolor[3];       ///< foreground color in YUV
> +    unsigned char fgcolor[3];       ///< background/Box color in YUV
> +    short int draw_box;             ///< draw box around text - true or false
> +    short int outline;              ///< draw outline in bg color around text
> +    int text_height;                ///< height of a font symbol
> +    int baseline;                   ///< baseline to draw fonts from
> +    int use_kerning;                ///< font kerning is used - true/false
> +    FT_Library library;             ///< freetype font library handle
> +    FT_Face face;                   ///< freetype font face handle
> +    FT_Glyph glyphs[256];           ///< array holding glyphs of font
> +    FT_Bitmap bitmaps[256];         ///< array holding bitmaps of font
> +    int advance[256];
> +    int bitmap_left[256];
> +    int bitmap_top[256];
> +    unsigned int glyphs_index[256];
> +    int hsub, vsub;                 ///< chroma subsampling values
> +} DrawTextContext;
> +
> +typedef struct {
> +    const AVClass *class;
> +    unsigned char *f;
> +    unsigned char *t;
> +    char *T;
> +    char *c;
> +    char *C;
> +    unsigned int x;
> +    unsigned int y;
> +    unsigned int s;
> +    short int b;
> +    short int o;
> +} InParams;

I really cannot understand why two structs, also please avoid one char
variables.

> +#define OFFSET(x) offsetof(InParams, x)
> +
> +static const AVOption inparams_options[]= {
> +{"f", "set f", OFFSET(f), FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
> +{"t", "set t", OFFSET(t), FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
> +{"T", "set T", OFFSET(T), FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
> +{"c", "set c", OFFSET(c), FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
> +{"C", "set C", OFFSET(C), FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
> +{"b", "set b", OFFSET(b), FF_OPT_TYPE_INT,    0,         0,        1 },
> +{"o", "set o", OFFSET(o), FF_OPT_TYPE_INT,    0,         0,        1 },
> +{"s", "set s", OFFSET(s), FF_OPT_TYPE_INT,   16,         1,       72 },
> +{"x", "set x", OFFSET(x), FF_OPT_TYPE_INT,    0,         0,  INT_MAX },
> +{"y", "set y", OFFSET(y), FF_OPT_TYPE_INT,    0,         0,  INT_MAX },
> +{NULL},
> +};
> +
> +static const char *inparams_get_name(void *ctx)
> +{
> +    return "inparams";
> +}
> +
> +static const AVClass inparams_class = {
> +    "InParams",
> +    inparams_get_name,
> +    inparams_options
> +};

Again possibly use just one context.

> +static int query_formats(AVFilterContext *ctx)
> +{
> +    /* FIXME : Supports only YUV420P now. Add support for other formats */

Simpler: "FIXME:  Add support for other formats"

or skip the note altogheter.

> +    enum PixelFormat pix_fmts[] = {
> +        PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P,
> +        PIX_FMT_YUV411P, PIX_FMT_YUV410P,
> +        PIX_FMT_YUV440P, PIX_FMT_NONE
> +    };
> +
> +    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
> +    return 0;
> +}
> +
> +#undef __FTERRORS_H__
> +#define FT_ERROR_START_LIST {
> +#define FT_ERRORDEF(e, v, s) { (e), (s) },
> +#define FT_ERROR_END_LIST { 0, NULL } };
> +
> +struct ft_error
> +{
> +    int err;
> +    char *err_msg;
> +} ft_errors[] =
> +#include FT_ERRORS_H
> +
> +static const char *ft_err_msg(int err)
> +{
> +    const struct ft_error *fterr;
> +
> +    for (fterr = ft_errors; fterr->err_msg != NULL; fterr++)
> +        if (fterr->err == err)
> +            return fterr->err_msg;
> +
> +    return "Unknown Freetype Error";
> +}

Why not simply use
#define FT_ERRMSG(e) ft_errors[e].msg
?

> +static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
> +{
> +    unsigned short int c;
> +    uint8_t rgba[4];
> +    uint8_t err, file_valid = 0;
> +    int y_max, y_min;
> +    FT_BBox bbox;
> +    DrawTextContext *dtext = ctx->priv;
> +    InParams *in = av_malloc(sizeof(InParams));
> +
> +    in->class = &inparams_class;
> +    av_opt_set_defaults2(in, 0, 0);
> +    in->c = av_strdup("black");
> +    in->C = av_strdup("white");
> +
> +    if (av_set_options_string(in, args, "=", ":") < 0) {
> +        av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
> +        goto fail;
> +    }
> +    dtext->text = in->t;
> +    dtext->file = in->T;
> +    dtext->draw_box = in->b;
> +    dtext->outline = in->o;
> +    dtext->x = in->x;
> +    dtext->y = in->y;

That wouldn't be necessary if you were using just one context.

> +    if (!in->f || *(in->f) == 0) {
> +        av_log(ctx, AV_LOG_ERROR, "No font file provided! (=f:filename)\n");
> +        goto fail;
> +    }
> +
> +    if (!in->t || *(in->t) == 0) {
> +        av_log(ctx, AV_LOG_ERROR, "No text provided (=t:text)\n");
> +        goto fail;
> +    }
> +
> +    if (in->T && *(in->T) != 0) {
> +        FILE *fp;
> +        if (!(fp = fopen(in->T, "r"))) {
> +            av_log(ctx, AV_LOG_INFO, "WARNING: The file could not be opened.\
> +                   Using text provided with =t switch: %s", in->T);

AV_LOG_WARNING

> +        } else {
> +            fclose(fp);
> +            file_valid = 1;
> +        }
> +    }

Also why is the user supposed to provide both text and text file? Just
one should be enough, and no need to warn in that case.

> +    if ((in->c) && (av_parse_color(rgba, in->c, ctx) != 0)) {
> +        av_log(ctx, AV_LOG_ERROR, "Invalid foreground color: '%s'.\n", in->c);
> +        goto fail;
> +    }
> +    dtext->fgcolor[0] = RGB_TO_Y(rgba[0], rgba[1], rgba[2]);
> +    dtext->fgcolor[1] = RGB_TO_U(rgba[0], rgba[1], rgba[2], 0);
> +    dtext->fgcolor[2] = RGB_TO_V(rgba[0], rgba[1], rgba[2], 0);

****1****

> +    if ((in->C) && (av_parse_color(rgba, in->C, ctx) != 0)) {
> +        av_log(ctx, AV_LOG_ERROR, "Invalid background color: '%s'.\n", in->C);
> +        goto fail;
> +    }
> +
> +    dtext->bgcolor[0] = RGB_TO_Y(rgba[0], rgba[1], rgba[2]);
> +    dtext->bgcolor[1] = RGB_TO_U(rgba[0], rgba[1], rgba[2], 0);
> +    dtext->bgcolor[2] = RGB_TO_V(rgba[0], rgba[1], rgba[2], 0);

****2****

This may be factorized.

> +
> +    if ((err = FT_Init_FreeType(&(dtext->library))) != 0) {

!= is not necessary.

> +        av_log(ctx, AV_LOG_ERROR, "Could not load FreeType (%s).\n", ft_err_msg(err));
> +        goto fail;
> +    }
> +
> +    if ((err = FT_New_Face( dtext->library, in->f, 0, &(dtext->face) )) != 0) {
> +        av_log(ctx, AV_LOG_ERROR, "Could not load face: %s  (%s).\n", in->f, ft_err_msg(err));
> +        goto fail;
> +    }
> +    if ((err = FT_Set_Pixel_Sizes( dtext->face, 0, in->s)) != 0) {
> +        av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels (%s).\n", in->s, ft_err_msg(err));
> +        goto fail;
> +    }
> +
> +    dtext->use_kerning = FT_HAS_KERNING(dtext->face);
> +
> +    /* load and cache glyphs */
> +    y_max = -32000;
> +    y_min =  32000;
> +    /* FIXME : Supports only ASCII text now. Add Unicode support */
> +    for (c=0; c <= 255; c++) {
> +
> +        if (!file_valid && !strrchr(dtext->text,(unsigned char)c))
> +            continue;
> +
> +        /* Load char */
> +        err = FT_Load_Char( dtext->face, (unsigned char) c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
> +        if (err) continue;  /* ignore errors */
> +
> +        dtext->bitmaps[c]     = dtext->face->glyph->bitmap;
> +        dtext->bitmap_left[c] = dtext->face->glyph->bitmap_left;
> +        dtext->bitmap_top[c]  = dtext->face->glyph->bitmap_top;
> +        dtext->advance[c]     = dtext->face->glyph->advance.x >> 6;
> +
> +        err = FT_Get_Glyph( dtext->face->glyph, &(dtext->glyphs[c]) );
> +        if (err) continue;  /* ignore errors */
> +        dtext->glyphs_index[c] = FT_Get_Char_Index( dtext->face, (unsigned char) c );
> +
> +        /* Measure text height to calculate text_height (or the maximum text height) */
> +        FT_Glyph_Get_CBox( dtext->glyphs[ c ], ft_glyph_bbox_pixels, &bbox );
> +        if (bbox.yMax > y_max)
> +          y_max = bbox.yMax;
> +        if (bbox.yMin < y_min)
> +          y_min = bbox.yMin;
> +    }
> +
> +    dtext->text_height = y_max - y_min;
> +    dtext->baseline = y_max;
> +
> +    return 0;
> +
> +fail:
> +    av_free(in->f);
> +    av_free(in->c);
> +    av_free(in->C);
> +    return AVERROR(EINVAL);
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> +    DrawTextContext *dtext = ctx->priv;
> +    av_freep(&dtext->text);
> +    av_freep(&dtext->file);
> +    FT_Done_Face(dtext->face);
> +    FT_Done_FreeType(dtext->library);
> +}
> +
> +static int config_input(AVFilterLink *link)
> +{
> +    DrawTextContext *dtext = link->dst->priv;
> +    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[link->format];
> +    dtext->hsub = pix_desc->log2_chroma_w;
> +    dtext->vsub = pix_desc->log2_chroma_h;
> +    return 0;
> +}
> +
> +#define SET_PIXEL(pic_ref, yuv_color, x, y, hsub, vsub) { \

> +    pic_ref->data[0][ (x) + (y)*pic_ref->linesize[0] ] = yuv_color[0]; \

Can be vertically aligned like:
       pic_ref->data[0][  x           +  y         *pic_ref->linesize[0]  ] = yuv_color[0]; \

Same below.

> +    pic_ref->data[1][ ((x >> hsub) + (y >> vsub)*pic_ref->linesize[1]) ] = yuv_color[1]; \
> +    pic_ref->data[2][ ((x >> hsub) + (y >> vsub)*pic_ref->linesize[2]) ] = yuv_color[2]; \
> +}
> +
> +#define GET_PIXEL(pic_ref, yuv_color, x, y, hsub, vsub) { \
> +    yuv_color[0] = pic_ref->data[0][ (x) + (y)*pic_ref->linesize[0] ]; \
> +    yuv_color[1] = pic_ref->data[1][ (x >> hsub) + (y >> vsub)*pic_ref->linesize[1] ]; \
> +    yuv_color[2] = pic_ref->data[2][ (x >> hsub) + (y >> vsub)*pic_ref->linesize[2] ]; \
> +}
> +
> +static inline void draw_glyph(AVFilterPicRef *pic_ref, FT_Bitmap *bitmap, unsigned int x,
> +                              unsigned int y, unsigned int width, unsigned int height,
> +                              unsigned char yuv_fgcolor[3], unsigned char yuv_color[3],
> +                              short int outline, int hsub, int vsub)
> +{
> +    int r, c;
> +    uint8_t spixel, dpixel[3], in_glyph=0;
> +
> +    if (bitmap->pixel_mode == ft_pixel_mode_mono) {
> +        in_glyph = 0;
> +        for (r=0; (r < bitmap->rows) && (r+y < height); r++) {
> +            for (c=0; (c < bitmap->width) && (c+x < width); c++) {
> +                /* pixel in the pic_ref (destination) */
> +                GET_PIXEL(pic_ref, dpixel, (c+x), (y+r), hsub, vsub);
> +  
> +                /* pixel in the glyph bitmap (source) */
> +                spixel = bitmap->buffer[r*bitmap->pitch +c/8] & (0x80>>(c%8));
> +
> +                if (spixel)
> +                    memcpy(dpixel, yuv_fgcolor, 3);
> +
> +                if (outline) {
> +                    /* border detection: */
> +                    if ( !in_glyph && spixel ) {
> +                        /* left border detected */
> +                        in_glyph = 1;
> +                        /* draw left pixel border */
> +                        if (c-1 >= 0)
> +                            SET_PIXEL(pic_ref, yuv_color, (c+x-1), (y+r), hsub, vsub);
> +                    } else if ( in_glyph && !spixel ) {
> +                    /* right border detected */
> +                        in_glyph = 0;
> +                        /* 'draw' right pixel border */
> +                        memcpy(dpixel, yuv_color, 3);
> +                    }
> +
> +                    if (in_glyph) {
> +                    /* see if we have a top/bottom border */
> +                        /* top */
> +                        if ((r-1 >= 0) && (! bitmap->buffer[(r-1)*bitmap->pitch +c/8] & (0x80>>(c%8))))
> +                            /* we have a top border */
> +                            SET_PIXEL(pic_ref, yuv_color, (c+x), (y+r-1), hsub, vsub);
> +
> +                        /* bottom border detection */
> +                        if ((r+1 < height) && (! bitmap->buffer[(r+1)*bitmap->pitch +c/8] & (0x80>>(c%8))))
> +                            /* draw bottom border */
> +                            SET_PIXEL(pic_ref, yuv_color, (c+x), (y+r+1), hsub, vsub);
> +                    }
> +                }
> +                SET_PIXEL(pic_ref, dpixel, (c+x), (y+r), hsub, vsub);
> +            }
> +        }
> +    }
> +}
> +
> +static inline void drawbox(AVFilterPicRef *pic_ref, unsigned int x, unsigned int y,
> +            unsigned int width, unsigned int height, unsigned char yuv_color[3], int hsub, int vsub)

Long line, weird indent.

> +{
> +    int i, plane;
> +    uint8_t *p;
> +
> +    for (plane = 0; plane < 3 && pic_ref->data[plane]; plane++) {
> +        int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
> +        int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
> +
> +        p = pic_ref->data[plane] + (y >> vsub1) * pic_ref->linesize[plane] + (x >> hsub1);
> +        for (i = 0; i < (height >> vsub1); i++) {
> +            memset(p, yuv_color[plane], (width >> hsub1));
> +            p += pic_ref->linesize[plane];
> +        }
> +    }
> +}

Is there is some problem at supporting alpha channel here?

> +static void draw_text(AVFilterContext *ctx, AVFilterPicRef *pic_ref, int width, int height)
> +{
> +    DrawTextContext *dtext = ctx->priv;
> +    FT_Face face = dtext->face;
> +    FT_GlyphSlot  slot = face->glyph;
> +    FILE *fd = NULL;
> +    unsigned char *text = dtext->text;
> +    unsigned char c;
> +    int x = 0, y = 0, i = 0, size = 0;
> +    unsigned char buff[MAXSIZE_TEXT];
> +    unsigned char tbuff[MAXSIZE_TEXT];
> +    time_t now = time(0);
> +    int str_w, str_w_max;
> +    FT_Vector pos[MAXSIZE_TEXT];
> +    FT_Vector delta;
> +
> +    if (dtext->file) {
> +        if (!(fd = fopen(dtext->file, "r"))) {
> +            text = dtext->text;
> +            av_log(ctx, AV_LOG_INFO, "WARNING: The file could not be opened.\
> +                   Using text provided with -t switch");
> +        } else {
> +            int l = fread(tbuff, sizeof(char), MAXSIZE_TEXT-1, fd);
> +            if (l >= 0) {
> +                tbuff[l] = 0;
> +                text = tbuff;
> +            } else {
> +                text = dtext->text;
> +                av_log(ctx, AV_LOG_INFO, "WARNING: The file could not be read.\
> +                       Using text provided with -t switch");
> +            }
> +            fclose(fd);
> +        }
> +    }  else {
> +        text = dtext->text;
> +    }

Uh, what's the point of reading every time the file, and not just once
at the init stage?

> +    strftime(buff, sizeof(buff), text, localtime(&now));
> +    text = buff;
> +    size = strlen(text);

Please document this behavior in the user docs.

Also localtime is deprecated and shouldn't be used, from GNU libc
manual:

|   Using the `localtime' function is a big problem in multi-threaded
|programs.  The result is returned in a static buffer and this is used in
|all threads.  POSIX.1c introduced a variant of this function.

> +    /* measure string size and save glyphs position*/
> +    str_w = str_w_max = 0;
> +    x = dtext->x;
> +    y = dtext->y;
> +    for (i=0; i < size; i++) {
> +        c = text[i];
> +        /* kerning */
> +        if ( (dtext->use_kerning) && (i > 0) && (dtext->glyphs_index[c]) ) {
> +            FT_Get_Kerning(dtext->face, dtext->glyphs_index[text[i-1]],
> +                           dtext->glyphs_index[c], ft_kerning_default, &delta);
> +            x += delta.x >> 6;
> +        }
> +
> +        if (( (x + dtext->advance[ c ]) >= width ) || ( c == '\n' )) {
> +            if (c != '\n')
> +                str_w_max = width - dtext->x - 1;
> +            y += dtext->text_height;
> +            x = dtext->x;
> +        }
> +
> +        /* save position */
> +        pos[i].x = x + dtext->bitmap_left[c];
> +        pos[i].y = y - dtext->bitmap_top[c] + dtext->baseline;
> +        x += dtext->advance[c];
> +        str_w += dtext->advance[c];
> +    }
> +    y += dtext->text_height;
> +    if (str_w_max == 0)
> +        str_w_max = str_w;
> +    if (dtext->draw_box) {
> +        /* Check if it doesn't pass the limits */
> +        if ( str_w_max + dtext->x >= width )
> +            str_w_max = width - dtext->x - 1;
> +        if ( y >= height )
> +            y = height - 1;
> +
> +        av_log(ctx, AV_LOG_ERROR, "Entry : %d, %d \n", y, dtext->text_height);

AV_LOG_DEBUG

> +        /* Draw Background */
> +        drawbox( pic_ref, dtext->x, dtext->y, str_w_max, y-dtext->y,
> +                 dtext->bgcolor, dtext->hsub, dtext->vsub );
> +    }
> +
> +    /* Draw Glyphs */
> +    for (i=0; i < size; i++) {
> +        c = text[i];
> +
> +        /* skip '_' (consider as space) if text was specified in cmd line and
> +         * skip new line char, just go to new line */
> +        if (( (c == '_') && (text == dtext->text) ) || ( c == '\n' ) )
> +            continue;
> +
> +        /* now, draw to our target surface */
> +        draw_glyph( pic_ref,
> +                    &(dtext->bitmaps[ c ]),
> +                    pos[i].x,
> +                    pos[i].y,
> +                    width,
> +                    height,
> +                    dtext->fgcolor,
> +                    dtext->bgcolor,
> +                    dtext->outline,
> +                    dtext->hsub,
> +                    dtext->vsub );
> +
> +        /* increment pen position */
> +        x += slot->advance.x >> 6;
> +    }
> +}
> +
> +static void end_frame(AVFilterLink *link)
> +{
> +    AVFilterLink *output = link->dst->outputs[0];
> +    AVFilterPicRef *pic_ref = link->cur_pic;
> +
> +    draw_text(link->dst, pic_ref, pic_ref->w, pic_ref->h);
> +
> +    avfilter_draw_slice(output, 0, pic_ref->h, 1);
> +    avfilter_end_frame(output);
> +}
> +
> +AVFilter avfilter_vf_drawtext = {
> +    .name      = "drawtext",
> +    .description = "Draw text on top of video frames, requires libfreetype library.",

I'd prefer "using libfreetype" rather than "requires libfreetype library".

> +    .priv_size = sizeof(DrawTextContext),
> +    .init      = init,
> +    .uninit      = uninit,
> +
> +    .query_formats   = query_formats,
> +    .inputs    = (AVFilterPad[]) {{ .name            = "default",
> +                                    .type            = AVMEDIA_TYPE_VIDEO,
> +                                    .get_video_buffer= avfilter_null_get_video_buffer,
> +                                    .start_frame     = avfilter_null_start_frame,
> +                                    .end_frame       = end_frame,
> +                                    .config_props    = config_input,
> +                                    .min_perms       = AV_PERM_WRITE |
> +                                                       AV_PERM_READ,
> +                                    .rej_perms       = AV_PERM_PRESERVE },
> +                                  { .name = NULL}},
> +    .outputs   = (AVFilterPad[]) {{ .name            = "default",
> +                                    .type            = AVMEDIA_TYPE_VIDEO, },
> +                                  { .name = NULL}},
> +};

Regards.


More information about the FFmpeg-soc mailing list