Browse code

lavfi/drawtext: use bprint for the expanded text.

Nicolas George authored on 2012/11/11 00:06:32
Showing 1 changed files
... ...
@@ -31,6 +31,7 @@
31 31
 
32 32
 #include "config.h"
33 33
 #include "libavutil/avstring.h"
34
+#include "libavutil/bprint.h"
34 35
 #include "libavutil/common.h"
35 36
 #include "libavutil/file.h"
36 37
 #include "libavutil/eval.h"
... ...
@@ -117,8 +118,7 @@ typedef struct {
117 117
     int reinit;                     ///< tells if the filter is being reinited
118 118
     uint8_t *fontfile;              ///< font to be used
119 119
     uint8_t *text;                  ///< text to be drawn
120
-    uint8_t *expanded_text;         ///< used to contain the strftime()-expanded text
121
-    size_t   expanded_text_size;    ///< size in bytes of the expanded_text buffer
120
+    AVBPrint expanded_text;         ///< used to contain the expanded text
122 121
     int ft_load_flags;              ///< flags used for loading fonts, see FT_LOAD_*
123 122
     FT_Vector *positions;           ///< positions for each element in the text
124 123
     size_t nb_positions;            ///< number of elements of positions array
... ...
@@ -484,6 +484,8 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
484 484
     }
485 485
     dtext->tabsize *= glyph->advance;
486 486
 
487
+    av_bprint_init(&dtext->expanded_text, 0, AV_BPRINT_SIZE_UNLIMITED);
488
+
487 489
     return 0;
488 490
 }
489 491
 
... ...
@@ -521,6 +523,8 @@ static av_cold void uninit(AVFilterContext *ctx)
521 521
 
522 522
     FT_Done_Face(dtext->face);
523 523
     FT_Done_FreeType(dtext->library);
524
+
525
+    av_bprint_finalize(&dtext->expanded_text, NULL);
524 526
 }
525 527
 
526 528
 static inline int is_newline(uint32_t c)
... ...
@@ -584,7 +588,7 @@ static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char
584 584
 static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref,
585 585
                        int width, int height, const uint8_t rgbcolor[4], FFDrawColor *color, int x, int y)
586 586
 {
587
-    char *text = dtext->expanded_text;
587
+    char *text = dtext->expanded_text.str;
588 588
     uint32_t code = 0;
589 589
     int i, x1, y1;
590 590
     uint8_t *p;
... ...
@@ -637,43 +641,32 @@ static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
637 637
 
638 638
     time_t now = time(0);
639 639
     struct tm ltime;
640
-    uint8_t *buf = dtext->expanded_text;
641
-    int buf_size = dtext->expanded_text_size;
640
+    AVBPrint *bp = &dtext->expanded_text;
641
+
642
+    av_bprint_clear(bp);
642 643
 
643 644
     if(dtext->basetime != AV_NOPTS_VALUE)
644 645
         now= picref->pts*av_q2d(ctx->inputs[0]->time_base) + dtext->basetime/1000000;
645 646
 
646
-    if (!buf) {
647
-        buf_size = 2*strlen(dtext->text)+1;
648
-        buf = av_malloc(buf_size);
649
-    }
650
-
651 647
 #if HAVE_LOCALTIME_R
652 648
     localtime_r(&now, &ltime);
653 649
 #else
654 650
     if(strchr(dtext->text, '%'))
655 651
         ltime= *localtime(&now);
656 652
 #endif
657
-
658
-    do {
659
-        *buf = 1;
660
-        if (strftime(buf, buf_size, dtext->text, &ltime) != 0 || *buf == 0)
661
-            break;
662
-        buf_size *= 2;
663
-    } while ((buf = av_realloc(buf, buf_size)));
653
+    av_bprint_strftime(bp, dtext->text, &ltime);
664 654
 
665 655
     if (dtext->tc_opt_string) {
666 656
         char tcbuf[AV_TIMECODE_STR_SIZE];
667 657
         av_timecode_make_string(&dtext->tc, tcbuf, dtext->frame_id++);
668
-        av_free(buf);
669
-        buf = av_asprintf("%s%s", dtext->text, tcbuf);
658
+        av_bprint_clear(bp);
659
+        av_bprintf(bp, "%s%s", dtext->text, tcbuf);
670 660
     }
671 661
 
672
-    if (!buf)
662
+    if (!av_bprint_is_complete(bp))
673 663
         return AVERROR(ENOMEM);
674
-    text = dtext->expanded_text = buf;
675
-    dtext->expanded_text_size = buf_size;
676
-    if ((len = strlen(text)) > dtext->nb_positions) {
664
+    text = dtext->expanded_text.str;
665
+    if ((len = dtext->expanded_text.len) > dtext->nb_positions) {
677 666
         if (!(dtext->positions =
678 667
               av_realloc(dtext->positions, len*sizeof(*dtext->positions))))
679 668
             return AVERROR(ENOMEM);