avutil/opt: do no rely on av_d2q returning higher num/den than allowed when parsing rational opts

Signed-off-by: Marton Balint <cus@passwd.hu>
This commit is contained in:
Marton Balint 2024-11-10 14:25:53 +01:00
parent c3051d94a7
commit e9af7b7b5d

View File

@ -231,6 +231,14 @@ static int opt_set_init(void *obj, const char *name, int search_flags,
return 0; 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) static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
{ {
switch (TYPE_BASE(o->type)) { 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) if ((int) num == num)
*(AVRational *)dst = (AVRational) { num *intnum, den }; *(AVRational *)dst = (AVRational) { num *intnum, den };
else else
*(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24); *(AVRational *)dst = double_to_rational(num * intnum / den);
break; break;
default: default:
return AVERROR(EINVAL); 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) if (num == 1.0 && (int)intnum == intnum)
*out_val = (AVRational){intnum, den}; *out_val = (AVRational){intnum, den};
else else
*out_val = av_d2q(num*intnum/den, 1<<24); *out_val = double_to_rational(num*intnum/den);
return 0; return 0;
} }
@ -2257,7 +2265,7 @@ int av_opt_get_array(void *obj, const char *name, int search_flags,
case AV_OPT_TYPE_RATIONAL: case AV_OPT_TYPE_RATIONAL:
*(AVRational*)dst = (num == 1.0 && (int)intnum == intnum) ? *(AVRational*)dst = (num == 1.0 && (int)intnum == intnum) ?
(AVRational){ intnum, den } : (AVRational){ intnum, den } :
av_d2q(num * intnum / den, 1<<24); double_to_rational(num * intnum / den);
break; break;
default: av_assert0(0); default: av_assert0(0);
} }