From: Raymond Cheng <raycheng100@hotmail.com> To: "ffmpeg-devel@ffmpeg.org" <ffmpeg-devel@ffmpeg.org> Subject: [FFmpeg-devel] [PATCH] avfilter/drawtext: Support bidirectional metadata in drawtext filter Date: Thu, 7 Jul 2022 22:45:14 +0000 Message-ID: <PH0PR13MB5921C40E93D8E30C56F79DD5F3839@PH0PR13MB5921.namprd13.prod.outlook.com> (raw) 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@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 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
reply other threads:[~2022-07-07 22:45 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=PH0PR13MB5921C40E93D8E30C56F79DD5F3839@PH0PR13MB5921.namprd13.prod.outlook.com \ --to=raycheng100@hotmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git