]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_drawtext.c
Mark some arrays that never change as const.
[ffmpeg] / libavfilter / vf_drawtext.c
index d954fdf2cda447ea27be77ee489de4630374970b..5668c8e8e0b23e867a0b99f5ca70ea830b38c4b7 100644 (file)
@@ -47,6 +47,7 @@
 #include "libavutil/random_seed.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/time_internal.h"
 #include "libavutil/tree.h"
 #include "libavutil/lfg.h"
 #include "avfilter.h"
@@ -151,6 +152,9 @@ typedef struct DrawTextContext {
     char   *d_expr;
     AVExpr *d_pexpr;
     int draw;                       ///< set to zero to prevent drawing
+    char   *a_expr;
+    AVExpr *a_pexpr;
+    int alpha;
     AVLFG  prng;                    ///< random
 } DrawTextContext;
 
@@ -168,13 +172,14 @@ static const AVOption drawtext_options[]= {
     { "boxcolor",    NULL,                   OFFSET(boxcolor_string),    AV_OPT_TYPE_STRING, { .str = "white" },          .flags = FLAGS },
     { "shadowcolor", NULL,                   OFFSET(shadowcolor_string), AV_OPT_TYPE_STRING, { .str = "black" },          .flags = FLAGS },
     { "box",         NULL,                   OFFSET(draw_box),           AV_OPT_TYPE_INT,    { .i64 = 0       }, 0,       1,       FLAGS },
-    { "fontsize",    NULL,                   OFFSET(fontsize),           AV_OPT_TYPE_INT,    { .i64 = 16      }, 1,       72,      FLAGS },
+    { "fontsize",    NULL,                   OFFSET(fontsize),           AV_OPT_TYPE_INT,    { .i64 = 16      }, 1,       1024,    FLAGS },
     { "x",           NULL,                   OFFSET(x_expr),             AV_OPT_TYPE_STRING, { .str = "0"     },          .flags = FLAGS },
     { "y",           NULL,                   OFFSET(y_expr),             AV_OPT_TYPE_STRING, { .str = "0"     },          .flags = FLAGS },
     { "shadowx",     NULL,                   OFFSET(shadowx),            AV_OPT_TYPE_INT,    { .i64 = 0       }, INT_MIN, INT_MAX, FLAGS },
     { "shadowy",     NULL,                   OFFSET(shadowy),            AV_OPT_TYPE_INT,    { .i64 = 0       }, INT_MIN, INT_MAX, FLAGS },
     { "tabsize",     NULL,                   OFFSET(tabsize),            AV_OPT_TYPE_INT,    { .i64 = 4       }, 0,       INT_MAX, FLAGS },
     { "draw",        "if false do not draw", OFFSET(d_expr),             AV_OPT_TYPE_STRING, { .str = "1"     },          .flags = FLAGS },
+    { "alpha",       "apply alpha while rendering", OFFSET(a_expr),      AV_OPT_TYPE_STRING, { .str = "1"     },          .flags = FLAGS },
     { "fix_bounds",  "if true, check and fix text coords to avoid clipping",
                                             OFFSET(fix_bounds),          AV_OPT_TYPE_INT,    { .i64 = 1       }, 0,       1,       FLAGS },
 
@@ -214,11 +219,10 @@ static const AVClass drawtext_class = {
 #define FT_ERRORDEF(e, v, s) { (e), (s) },
 #define FT_ERROR_END_LIST { 0, NULL } };
 
-struct ft_error
-{
+static const struct ft_error {
     int err;
     const char *err_msg;
-} static ft_errors[] =
+} ft_errors[] =
 #include FT_ERRORS_H
 
 #define FT_ERRMSG(e) ft_errors[e].err_msg
@@ -462,10 +466,6 @@ static av_cold int init(AVFilterContext *ctx)
     }
     s->tabsize *= glyph->advance;
 
-#if !HAVE_LOCALTIME_R
-    av_log(ctx, AV_LOG_WARNING, "strftime() expansion unavailable!\n");
-#endif
-
     return 0;
 }
 
@@ -520,30 +520,15 @@ static inline int is_newline(uint32_t c)
     return c == '\n' || c == '\r' || c == '\f' || c == '\v';
 }
 
-static int dtext_prepare_text(AVFilterContext *ctx)
+static int expand_strftime(DrawTextContext *s)
 {
-    DrawTextContext *s = ctx->priv;
-    uint32_t code = 0, prev_code = 0;
-    int x = 0, y = 0, i = 0, ret;
-    int text_height, baseline;
-    char *text = s->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 };
-    int width  = ctx->inputs[0]->w;
-    int height = ctx->inputs[0]->h;
-
-#if HAVE_LOCALTIME_R
-    time_t now = time(0);
     struct tm ltime;
+    time_t now   = time(0);
     uint8_t *buf = s->expanded_text;
     int buf_size = s->expanded_text_size;
 
     if (!buf)
-        buf_size = 2*strlen(s->text)+1;
+        buf_size = 2 * strlen(s->text) + 1;
 
     localtime_r(&now, &ltime);
 
@@ -556,9 +541,33 @@ static int dtext_prepare_text(AVFilterContext *ctx)
 
     if (!buf)
         return AVERROR(ENOMEM);
-    text = s->expanded_text = buf;
+    s->expanded_text      = buf;
     s->expanded_text_size = buf_size;
-#endif
+
+    return 0;
+}
+
+static int dtext_prepare_text(AVFilterContext *ctx)
+{
+    DrawTextContext *s = ctx->priv;
+    uint32_t code = 0, prev_code = 0;
+    int x = 0, y = 0, i = 0, ret;
+    int text_height, baseline;
+    char *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 };
+    int width  = ctx->inputs[0]->w;
+    int height = ctx->inputs[0]->h;
+
+    ret = expand_strftime(s);
+    if (ret < 0)
+        return ret;
+
+    text = s->expanded_text ? s->expanded_text : s->text;
 
     if ((len = strlen(text)) > s->nb_positions) {
         FT_Vector *p = av_realloc(s->positions,
@@ -680,6 +689,8 @@ static int config_input(AVFilterLink *inlink)
         (ret = av_expr_parse(&s->y_pexpr, s->y_expr, var_names,
                              NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
         (ret = av_expr_parse(&s->d_pexpr, s->d_expr, var_names,
+                             NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
+        (ret = av_expr_parse(&s->a_pexpr, s->a_expr, var_names,
                              NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
         return AVERROR(EINVAL);
 
@@ -715,7 +726,7 @@ static int config_input(AVFilterLink *inlink)
 
 #define SET_PIXEL_YUV(frame, yuva_color, val, x, y, hsub, vsub) {           \
     luma_pos    = ((x)          ) + ((y)          ) * frame->linesize[0]; \
-    alpha = yuva_color[3] * (val) * 129;                               \
+    alpha = yuva_color[3] * alpha_mul * (val) * 129 / 255; \
     frame->data[0][luma_pos]    = (alpha * yuva_color[0] + (255*255*129 - alpha) * frame->data[0][luma_pos]   ) >> 23; \
     if (((x) & ((1<<(hsub)) - 1)) == 0 && ((y) & ((1<<(vsub)) - 1)) == 0) {\
         chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * frame->linesize[1]; \
@@ -727,7 +738,8 @@ static int config_input(AVFilterLink *inlink)
 
 static inline int draw_glyph_yuv(AVFrame *frame, FT_Bitmap *bitmap, unsigned int x,
                                  unsigned int y, unsigned int width, unsigned int height,
-                                 const uint8_t yuva_color[4], int hsub, int vsub)
+                                 const uint8_t yuva_color[4], int hsub, int vsub,
+                                 int alpha_mul)
 {
     int r, c, alpha;
     unsigned int luma_pos, chroma_pos1, chroma_pos2;
@@ -749,7 +761,7 @@ static inline int draw_glyph_yuv(AVFrame *frame, FT_Bitmap *bitmap, unsigned int
 
 #define SET_PIXEL_RGB(frame, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off) { \
     p   = frame->data[0] + (x) * pixel_step + ((y) * frame->linesize[0]); \
-    alpha = rgba_color[3] * (val) * 129;                              \
+    alpha = rgba_color[3] * alpha_mul * (val) * 129 / 255;                           \
     *(p+r_off) = (alpha * rgba_color[0] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
     *(p+g_off) = (alpha * rgba_color[1] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
     *(p+b_off) = (alpha * rgba_color[2] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
@@ -758,7 +770,8 @@ static inline int draw_glyph_yuv(AVFrame *frame, FT_Bitmap *bitmap, unsigned int
 static inline int draw_glyph_rgb(AVFrame *frame, FT_Bitmap *bitmap,
                                  unsigned int x, unsigned int y,
                                  unsigned int width, unsigned int height, int pixel_step,
-                                 const uint8_t rgba_color[4], const uint8_t rgba_map[4])
+                                 const uint8_t rgba_color[4], const uint8_t rgba_map[4],
+                                 int alpha_mul)
 {
     int r, c, alpha;
     uint8_t *p;
@@ -782,11 +795,12 @@ static inline int draw_glyph_rgb(AVFrame *frame, FT_Bitmap *bitmap,
 static inline void drawbox(AVFrame *frame, unsigned int x, unsigned int y,
                            unsigned int width, unsigned int height,
                            uint8_t *line[4], int pixel_step[4], uint8_t color[4],
-                           int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4])
+                           int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4],
+                           int alpha_mul)
 {
     int i, j, alpha;
 
-    if (color[3] != 0xFF) {
+    if (color[3] != 0xFF || alpha_mul != 0xFF) {
         if (is_rgba_packed) {
             uint8_t *p;
             for (j = 0; j < height; j++)
@@ -807,9 +821,11 @@ static inline void drawbox(AVFrame *frame, unsigned int x, unsigned int y,
 }
 
 static int draw_glyphs(DrawTextContext *s, AVFrame *frame,
-                       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 = HAVE_LOCALTIME_R ? s->expanded_text : s->text;
+    char *text = s->expanded_text;
     uint32_t code = 0;
     int i;
     uint8_t *p;
@@ -824,7 +840,7 @@ static int draw_glyphs(DrawTextContext *s, AVFrame *frame,
             continue;
 
         dummy.code = code;
-        glyph = av_tree_find(s->glyphs, &dummy, (void *)glyph_cmp, NULL);
+        glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
 
         if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
             glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
@@ -833,11 +849,11 @@ static int draw_glyphs(DrawTextContext *s, AVFrame *frame,
         if (s->is_packed_rgb) {
             draw_glyph_rgb(frame, &glyph->bitmap,
                            s->positions[i].x+x, s->positions[i].y+y, width, height,
-                           s->pixel_step[0], rgbcolor, s->rgba_map);
+                           s->pixel_step[0], rgbcolor, s->rgba_map, s->alpha);
         } else {
             draw_glyph_yuv(frame, &glyph->bitmap,
                            s->positions[i].x+x, s->positions[i].y+y, width, height,
-                           yuvcolor, s->hsub, s->vsub);
+                           yuvcolor, s->hsub, s->vsub, s->alpha);
         }
     }
 
@@ -855,7 +871,7 @@ static int draw_text(AVFilterContext *ctx, AVFrame *frame,
         drawbox(frame, s->x, s->y, s->w, s->h,
                 s->box_line, s->pixel_step, s->boxcolor,
                 s->hsub, s->vsub, s->is_packed_rgb,
-                s->rgba_map);
+                s->rgba_map, s->alpha);
 
     if (s->shadowx || s->shadowy) {
         if ((ret = draw_glyphs(s, frame, width, height,
@@ -891,6 +907,21 @@ static inline int normalize_double(int *n, double d)
     return ret;
 }
 
+static void update_alpha(DrawTextContext *s)
+{
+    double alpha = av_expr_eval(s->a_pexpr, s->var_values, &s->prng);
+
+    if (isnan(alpha))
+        return;
+
+    if (alpha >= 1.0)
+        s->alpha = 255;
+    else if (alpha <= 0)
+        s->alpha = 0;
+    else
+        s->alpha = 256 * alpha;
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
@@ -914,6 +945,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 
     s->draw = av_expr_eval(s->d_pexpr, s->var_values, &s->prng);
 
+    update_alpha(s);
+
     normalize_double(&s->x, s->var_values[VAR_X]);
     normalize_double(&s->y, s->var_values[VAR_Y]);
 
@@ -929,7 +962,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     s->x &= ~((1 << s->hsub) - 1);
     s->y &= ~((1 << s->vsub) - 1);
 
-    av_dlog(ctx, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
+    av_log(ctx, AV_LOG_TRACE, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
             (int)s->var_values[VAR_N], s->var_values[VAR_T],
             s->x, s->y, s->x+s->w, s->y+s->h);