[FFmpeg-devel] [PATCH] avfilter/drawtext: Support bidirectional metadata in drawtext filter

Raymond Cheng raycheng100 at hotmail.com
Fri Jul 8 01:45:14 EEST 2022


The drawtext filter supports static bidi text via a function called
shape_text(). Fixed so that it calls shape_text() when rendering
non-static text from metadata (so that bidi text is rendered properly).

As an example, "Hello world" is "مرحبا بالعالم" in Arabic. The
following command line worked just fine before, and still works
after this change:

  ffmpeg -i input -vf drawtext=fontsize=30:x=30:y=30:fontcolor=yellow:text='مرحبا بالعالم' out.mp4

However, this command line did NOT work:

  ffmpeg -i input -vf metadata=mode=add:key=transcription:value='مرحبا بالعالم',drawtext=fontsize=30:x=30:y=30:fontcolor=yellow:text="'From metadata\: %{metadata\:transcription}'" out.mp4

This commit fixes it so that this second command line now works.

NOTE that the above command lines are for example only. They render
the proper text, but improperly justified (left-justified instead of
right-justified). For one-line transcriptions, this is easily fixed
by replacing x=30 with x=\(700-tw\). Two-line transcriptions do not
have a simple solution.

Signed-off-by: Raymond Cheng <raych at microsoft.com>
---
 libavfilter/vf_drawtext.c | 57 +++++++++++++++++++++++++--------------
 1 file changed, 37 insertions(+), 20 deletions(-)

diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index feb6898848..9e4a63b7fd 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -608,7 +608,7 @@ static int load_textfile(AVFilterContext *ctx)
 }
 
 #if CONFIG_LIBFRIBIDI
-static int shape_text(AVFilterContext *ctx)
+static int shape_text_arg(AVFilterContext *ctx, char **ppText)
 {
     DrawTextContext *s = ctx->priv;
     uint8_t *tmp;
@@ -625,12 +625,15 @@ static int shape_text(AVFilterContext *ctx)
     FriBidiCharType *bidi_types = NULL;
     FriBidiStrIndex i,j;
 
-    len = strlen(s->text);
+    if (!s->text_shaping)
+        return 0; // Do nothing
+
+    len = strlen(*ppText);
     if (!(unicodestr = av_malloc_array(len, sizeof(*unicodestr)))) {
         goto out;
     }
     len = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8,
-                                     s->text, len, unicodestr);
+                                     *ppText, len, unicodestr);
 
     bidi_types = av_malloc_array(len, sizeof(*bidi_types));
     if (!bidi_types) {
@@ -676,14 +679,14 @@ static int shape_text(AVFilterContext *ctx)
             unicodestr[j++] = unicodestr[i];
     len = j;
 
-    if (!(tmp = av_realloc(s->text, (len * 4 + 1) * sizeof(*s->text)))) {
+    if (!(tmp = av_realloc(*ppText, (len * 4 + 1) * sizeof(**ppText)))) {
         /* Use len * 4, as a unicode character can be up to 4 bytes in UTF-8 */
         goto out;
     }
 
-    s->text = tmp;
+    *ppText = tmp;
     len = fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8,
-                                     unicodestr, len, s->text);
+                                     unicodestr, len, *ppText);
     ret = 0;
 
 out:
@@ -693,8 +696,19 @@ out:
     av_free(bidi_types);
     return ret;
 }
+#else
+static int shape_text_arg(AVFilterContext *ctx, char **ppText)
+{
+    return 0;
+}
 #endif
 
+static int shape_text(AVFilterContext *ctx)
+{
+    DrawTextContext *s = ctx->priv;
+    return shape_text_arg(ctx, (char **)&s->text);
+}
+
 static enum AVFrameSideDataType text_source_string_parse(const char *text_source_string)
 {
     av_assert0(text_source_string);
@@ -771,11 +785,8 @@ static av_cold int init(AVFilterContext *ctx)
         return AVERROR(EINVAL);
     }
 
-#if CONFIG_LIBFRIBIDI
-    if (s->text_shaping)
-        if ((err = shape_text(ctx)) < 0)
-            return err;
-#endif
+    if ((err = shape_text(ctx)) < 0)
+        return err;
 
     if ((err = FT_Init_FreeType(&(s->library)))) {
         av_log(ctx, AV_LOG_ERROR,
@@ -1034,11 +1045,19 @@ static int func_metadata(AVFilterContext *ctx, AVBPrint *bp,
 {
     DrawTextContext *s = ctx->priv;
     AVDictionaryEntry *e = av_dict_get(s->metadata, argv[0], NULL, 0);
+    int err;
+
+    if (e && e->value) {
+        if ((err = shape_text_arg(ctx, &e->value)) < 0)
+            return err;
 
-    if (e && e->value)
         av_bprintf(bp, "%s", e->value);
-    else if (argc >= 2)
+    } else if (argc >= 2) {
+        if ((err = shape_text_arg(ctx, &argv[1])) < 0)
+            return err;
+
         av_bprintf(bp, "%s", argv[1]);
+    }
     return 0;
 }
 
@@ -1634,13 +1653,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
             av_frame_free(&frame);
             return ret;
         }
-#if CONFIG_LIBFRIBIDI
-        if (s->text_shaping)
-            if ((ret = shape_text(ctx)) < 0) {
-                av_frame_free(&frame);
-                return ret;
-            }
-#endif
+
+        if ((ret = shape_text(ctx)) < 0) {
+            av_frame_free(&frame);
+            return ret;
+        }
     }
 
     s->var_values[VAR_N] = inlink->frame_count_out + s->start_number;
-- 
2.34.1



More information about the ffmpeg-devel mailing list