drawtext: fix strftime() text expansion

The feature was dropped after the filter was partially rewritten and
recommitted.

Signed-off-by: Anton Khirnov <anton@khirnov.net>
This commit is contained in:
Stefano Sabatini 2011-05-16 20:11:50 +02:00 committed by Anton Khirnov
parent 0793531859
commit 3fd53defaa
1 changed files with 27 additions and 18 deletions

View File

@ -49,9 +49,11 @@ typedef struct {
const AVClass *class; const AVClass *class;
uint8_t *fontfile; ///< font to be used uint8_t *fontfile; ///< font to be used
uint8_t *text; ///< text to be drawn uint8_t *text; ///< text to be drawn
uint8_t *text_priv; ///< used to detect whether text changed uint8_t *expanded_text; ///< used to contain the strftime()-expanded text
size_t expanded_text_size; ///< size in bytes of the expanded_text buffer
int ft_load_flags; ///< flags used for loading fonts, see FT_LOAD_* int ft_load_flags; ///< flags used for loading fonts, see FT_LOAD_*
FT_Vector *positions; ///< positions for each element in the text 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 char *textfile; ///< file with text to be drawn
unsigned int x; ///< x position to start drawing text unsigned int x; ///< x position to start drawing text
unsigned int y; ///< y position to start drawing text unsigned int y; ///< y position to start drawing text
@ -349,6 +351,7 @@ static av_cold void uninit(AVFilterContext *ctx)
av_freep(&dtext->fontfile); av_freep(&dtext->fontfile);
av_freep(&dtext->text); av_freep(&dtext->text);
av_freep(&dtext->expanded_text);
av_freep(&dtext->fontcolor_string); av_freep(&dtext->fontcolor_string);
av_freep(&dtext->boxcolor_string); av_freep(&dtext->boxcolor_string);
av_freep(&dtext->positions); av_freep(&dtext->positions);
@ -517,7 +520,7 @@ static inline int is_newline(uint32_t c)
static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref, 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) int width, int height, const uint8_t rgbcolor[4], const uint8_t yuvcolor[4], int x, int y)
{ {
char *text = dtext->text; char *text = HAVE_LOCALTIME_R ? dtext->expanded_text : dtext->text;
uint32_t code = 0; uint32_t code = 0;
int i; int i;
uint8_t *p; uint8_t *p;
@ -559,45 +562,51 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
uint32_t code = 0, prev_code = 0; uint32_t code = 0, prev_code = 0;
int x = 0, y = 0, i = 0, ret; int x = 0, y = 0, i = 0, ret;
int text_height, baseline; int text_height, baseline;
char *text = dtext->text;
uint8_t *p; uint8_t *p;
int str_w = 0; int str_w = 0, len;
int y_min = 32000, y_max = -32000; int y_min = 32000, y_max = -32000;
FT_Vector delta; FT_Vector delta;
Glyph *glyph = NULL, *prev_glyph = NULL; Glyph *glyph = NULL, *prev_glyph = NULL;
Glyph dummy = { 0 }; Glyph dummy = { 0 };
if (dtext->text != dtext->text_priv) {
#if HAVE_LOCALTIME_R #if HAVE_LOCALTIME_R
time_t now = time(0); time_t now = time(0);
struct tm ltime; struct tm ltime;
uint8_t *buf = NULL; uint8_t *buf = dtext->expanded_text;
int buflen = 2*strlen(dtext->text) + 1, len; int buf_size = dtext->expanded_text_size;
if (!buf) {
buf_size = 2*strlen(dtext->text)+1;
buf = av_malloc(buf_size);
}
localtime_r(&now, &ltime); localtime_r(&now, &ltime);
while ((buf = av_realloc(buf, buflen))) { do {
*buf = 1; *buf = 1;
if ((len = strftime(buf, buflen, dtext->text, &ltime)) != 0 || *buf == 0) if (strftime(buf, buf_size, dtext->text, &ltime) != 0 || *buf == 0)
break; break;
buflen *= 2; buf_size *= 2;
} } while ((buf = av_realloc(buf, buf_size)));
if (!buf) if (!buf)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
av_freep(&dtext->text); text = dtext->expanded_text = buf;
dtext->text = dtext->text_priv = buf; dtext->expanded_text_size = buf_size;
#else
dtext->text_priv = dtext->text;
#endif #endif
if (!(dtext->positions = av_realloc(dtext->positions, if ((len = strlen(text)) > dtext->nb_positions) {
strlen(dtext->text)*sizeof(*dtext->positions)))) if (!(dtext->positions =
av_realloc(dtext->positions, len*sizeof(*dtext->positions))))
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
dtext->nb_positions = len;
} }
x = dtext->x; x = dtext->x;
y = dtext->y; y = dtext->y;
/* load and cache glyphs */ /* load and cache glyphs */
for (i = 0, p = dtext->text; *p; i++) { for (i = 0, p = text; *p; i++) {
GET_UTF8(code, *p++, continue;); GET_UTF8(code, *p++, continue;);
/* get glyph */ /* get glyph */
@ -614,7 +623,7 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
/* compute and save position for each glyph */ /* compute and save position for each glyph */
glyph = NULL; glyph = NULL;
for (i = 0, p = dtext->text; *p; i++) { for (i = 0, p = text; *p; i++) {
GET_UTF8(code, *p++, continue;); GET_UTF8(code, *p++, continue;);
/* skip the \n in the sequence \r\n */ /* skip the \n in the sequence \r\n */