[FFmpeg-devel] [PATCH 1/3] avutil/opt: do no rely on av_d2q returning higher num/den than allowed when parsing rational opts

Marton Balint cus at passwd.hu
Sun Nov 10 16:50:12 EET 2024


Signed-off-by: Marton Balint <cus at passwd.hu>
---
 libavutil/opt.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/libavutil/opt.c b/libavutil/opt.c
index ee451ae023..44b507dbd4 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -231,6 +231,14 @@ static int opt_set_init(void *obj, const char *name, int search_flags,
     return 0;
 }
 
+static AVRational double_to_rational(double d)
+{
+     AVRational r = av_d2q(d, 1 << 24);
+     if ((!r.num || !r.den) && d)
+         r = av_d2q(d, INT_MAX);
+     return r;
+}
+
 static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
 {
     switch (TYPE_BASE(o->type)) {
@@ -339,7 +347,7 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int
         if ((int) num == num)
             *(AVRational *)dst = (AVRational) { num *intnum, den };
         else
-            *(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24);
+            *(AVRational *)dst = double_to_rational(num * intnum / den);
         break;
     default:
         return AVERROR(EINVAL);
@@ -1301,7 +1309,7 @@ int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_
     if (num == 1.0 && (int)intnum == intnum)
         *out_val = (AVRational){intnum, den};
     else
-        *out_val = av_d2q(num*intnum/den, 1<<24);
+        *out_val = double_to_rational(num*intnum/den);
     return 0;
 }
 
@@ -2257,7 +2265,7 @@ int av_opt_get_array(void *obj, const char *name, int search_flags,
             case AV_OPT_TYPE_RATIONAL:
                 *(AVRational*)dst = (num == 1.0 && (int)intnum == intnum) ?
                                     (AVRational){ intnum, den }           :
-                                    av_d2q(num * intnum / den, 1<<24);
+                                    double_to_rational(num * intnum / den);
                 break;
             default: av_assert0(0);
             }
-- 
2.43.0



More information about the ffmpeg-devel mailing list