Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avfilter: allow floating point formatting in expr_int_format (vf_drawtext.c)
@ 2023-12-02 14:54 Lennart Klebl
  2023-12-03 14:52 ` Stefano Sabatini
  0 siblings, 1 reply; 2+ messages in thread
From: Lennart Klebl @ 2023-12-02 14:54 UTC (permalink / raw)
  To: ffmpeg-devel

[-- Attachment #1: Type: text/plain, Size: 278 bytes --]

Since I was trying for quite some time to get proper floating point
formatting of positive and negative numbers working with the existing
drawtext/expr_int_format implementation, I added the f format specifier
that is only applied when used in conjunction with a width.



[-- Attachment #2: 0001-avfilter-allow-floating-point-formatting-in-expr_int.patch --]
[-- Type: text/x-patch, Size: 3454 bytes --]

From 14c438a2bcf2ea17a6818ef5cdc6a7ee9ef30184 Mon Sep 17 00:00:00 2001
From: "lennart.klebl@t-online.de" <lennart.klebl@t-online.de>
Date: Sat, 2 Dec 2023 15:53:20 +0100
Subject: [PATCH] avfilter: allow floating point formatting in expr_int_format
 (vf_drawtext.c)

---
 doc/filters.texi          | 12 ++++++++----
 libavfilter/vf_drawtext.c | 19 ++++++++++++++-----
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index de19d130cc..1b11124434 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -12807,14 +12807,18 @@ the constants @var{text_w} and @var{text_h} will have an undefined
 value.
 
 @item expr_int_format, eif
-Evaluate the expression's value and output as formatted integer.
+Evaluate the expression's value and output as formatted integer or floating
+point number.
 
 The first argument is the expression to be evaluated, just as for the @var{expr} function.
 The second argument specifies the output format. Allowed values are @samp{x},
-@samp{X}, @samp{d} and @samp{u}. They are treated exactly as in the
+@samp{X}, @samp{d}, @samp{u}, and @samp{f}. They are treated exactly as in the
 @code{printf} function.
-The third parameter is optional and sets the number of positions taken by the output.
-It can be used to add padding with zeros from the left.
+
+When specifying @samp{f}, the third parameter is required and specifies the
+number of decimal places for floating point formatting. Otherwise, the third
+parameter is optional and sets the number of positions taken by the output. It
+can be used to add padding with zeros from the left.
 
 @item gmtime
 The time at which the filter is running, expressed in UTC.
diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index c5477cbff1..be0490bbb3 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -1359,6 +1359,7 @@ static int func_eval_expr_int_format(AVFilterContext *ctx, AVBPrint *bp,
     int ret;
     unsigned int positions = 0;
     char fmt_str[30] = "%";
+    char fmt_arg = argv[1][0];
 
     /*
      * argv[0] expression to be converted to `int`
@@ -1376,9 +1377,9 @@ static int func_eval_expr_int_format(AVFilterContext *ctx, AVBPrint *bp,
         return ret;
     }
 
-    if (!strchr("xXdu", argv[1][0])) {
+    if (!strchr("xXduf", fmt_arg)) {
         av_log(ctx, AV_LOG_ERROR, "Invalid format '%c' specified,"
-                " allowed values: 'x', 'X', 'd', 'u'\n", argv[1][0]);
+                " allowed values: 'x', 'X', 'd', 'u', 'f'\n", fmt_arg);
         return AVERROR(EINVAL);
     }
 
@@ -1400,14 +1401,22 @@ static int func_eval_expr_int_format(AVFilterContext *ctx, AVBPrint *bp,
     }
 #endif
 
+    if (fmt_arg == 'f' && argc < 3) fmt_arg = 'd';
+
     if (argc == 3)
-        av_strlcatf(fmt_str, sizeof(fmt_str), "0%u", positions);
-    av_strlcatf(fmt_str, sizeof(fmt_str), "%c", argv[1][0]);
+        if (fmt_arg == 'f')
+            av_strlcatf(fmt_str, sizeof(fmt_str), ".%u", positions);
+        else
+            av_strlcatf(fmt_str, sizeof(fmt_str), "0%u", positions);
+    av_strlcatf(fmt_str, sizeof(fmt_str), "%c", fmt_arg);
 
     av_log(ctx, AV_LOG_DEBUG, "Formatting value %f (expr '%s') with spec '%s'\n",
             res, argv[0], fmt_str);
 
-    av_bprintf(bp, fmt_str, intval);
+    if (fmt_arg == 'f')
+        av_bprintf(bp, fmt_str, res);
+    else
+        av_bprintf(bp, fmt_str, intval);
 
     return 0;
 }
-- 
2.43.0


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-03 14:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-02 14:54 [FFmpeg-devel] [PATCH] avfilter: allow floating point formatting in expr_int_format (vf_drawtext.c) Lennart Klebl
2023-12-03 14:52 ` Stefano Sabatini

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