Coverage Report

Created: 2026-07-15 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libswscale/rational64.c
Line
Count
Source
1
/*
2
 * 64-bit rational numbers
3
 * Copyright (c) 2025 Niklas Haas
4
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * 64-bit rational numbers
26
 * @author Niklas Haas
27
 */
28
29
#include <limits.h>
30
31
#include "libavutil/int128.h"
32
#include "rational64.h"
33
34
static av_int128 gcd128(av_int128 a, av_int128 b)
35
0
{
36
0
    while (av_test128(b)) {
37
0
        av_int128 tmp = b;
38
0
        b = av_mod128(a, b);
39
0
        a = tmp;
40
0
    }
41
0
    return a;
42
0
}
43
44
static AVRational64 reduce64(av_int128 num, av_int128 den)
45
0
{
46
0
    const av_int128 zero = av_to128i(0);
47
0
    const av_int128 max  = av_to128i(INT64_MAX);
48
0
    const int num_sign = av_cmp128(num, zero) < 0;
49
0
    const int den_sign = av_cmp128(den, zero) < 0;
50
0
    if (num_sign)
51
0
        num = av_sub128(zero, num);
52
0
    if (den_sign)
53
0
        den = av_sub128(zero, den);
54
55
0
    const av_int128 gcd = gcd128(num, den);
56
0
    if (av_test128(gcd)) {
57
0
        num = av_div128(num, gcd);
58
0
        den = av_div128(den, gcd);
59
0
    }
60
61
0
    av_uint128 a0n = av_to128u(0), a0d = av_to128u(1);
62
0
    av_uint128 a1n = av_to128u(1), a1d = av_to128u(0);
63
0
    if (av_cmp128(num, max) <= 0 && av_cmp128(den, max) <= 0) {
64
0
        a1n = num;
65
0
        a1d = den;
66
0
        goto done;
67
0
    }
68
69
0
    while (av_test128(den)) {
70
0
        av_int128 x        = av_div128(num, den);
71
0
        av_int128 next_den = av_sub128(num, av_mul128(den, x));
72
0
        av_uint128 a2n     = av_add128(av_mul128(x, a1n), a0n);
73
0
        av_uint128 a2d     = av_add128(av_mul128(x, a1d), a0d);
74
75
0
        if (av_cmp128(a2n, max) > 0 || av_cmp128(a2d, max) > 0) {
76
0
            if (av_test128(a1n))
77
0
                x = av_div128(av_sub128(max, a0n), a1n);
78
0
            if (av_test128(a1d)) {
79
0
                av_uint128 tmp = av_div128(av_sub128(max, a0d), a1d);
80
0
                x = av_min128(x, tmp);
81
0
            }
82
83
0
            av_uint128 x1d = av_mul128(x, a1d);
84
0
            av_uint128 a = av_mul128(den, av_add128(av_add128(x1d, x1d), a0d));
85
0
            av_uint128 b = av_mul128(num, a1d);
86
0
            if (av_cmp128(a, b) > 0) {
87
0
                a1n = av_add128(av_mul128(x, a1n), a0n);
88
0
                a1d = av_add128(x1d, a0d);
89
0
            }
90
0
            break;
91
0
        }
92
93
0
        a0n = a1n;
94
0
        a0d = a1d;
95
0
        a1n = a2n;
96
0
        a1d = a2d;
97
0
        num = den;
98
0
        den = next_den;
99
0
    }
100
101
0
done:;
102
0
    AVRational64 res = { av_from128i(a1n), av_from128i(a1d) };
103
0
    if (num_sign ^ den_sign)
104
0
        res.num = -res.num;
105
0
    return res;
106
0
}
107
108
int av_cmp_q64(AVRational64 a, AVRational64 b)
109
0
{
110
0
    const av_int128 p = av_mul128(av_to128i(a.num), av_to128i(b.den));
111
0
    const av_int128 q = av_mul128(av_to128i(b.num), av_to128i(a.den));
112
0
    const int test = av_cmp128(p, q);
113
114
0
    if (test)
115
0
        return (a.den < 0) ^ (b.den < 0) ? -test : test;
116
0
    else if (b.den && a.den)
117
0
        return 0;
118
0
    else if (a.num && b.num)
119
0
        return (a.num >> 63) - (b.num >> 63);
120
0
    else
121
0
        return INT_MIN;
122
0
}
123
124
AVRational64 av_mul_q64(AVRational64 b, AVRational64 c)
125
0
{
126
0
    return reduce64(av_mul128(av_to128i(b.num), av_to128i(c.num)),
127
0
                    av_mul128(av_to128i(b.den), av_to128i(c.den)));
128
0
}
129
130
AVRational64 av_div_q64(AVRational64 b, AVRational64 c)
131
0
{
132
0
    return av_mul_q64(b, av_inv_q64(c));
133
0
}
134
135
0
AVRational64 av_add_q64(AVRational64 b, AVRational64 c) {
136
0
    return reduce64(av_add128(av_mul128(av_to128i(b.num), av_to128i(c.den)),
137
0
                              av_mul128(av_to128i(c.num), av_to128i(b.den))),
138
0
                    av_mul128(av_to128i(b.den), av_to128i(c.den)));
139
0
}
140
141
AVRational64 av_sub_q64(AVRational64 b, AVRational64 c)
142
0
{
143
0
    return reduce64(av_sub128(av_mul128(av_to128i(b.num), av_to128i(c.den)),
144
0
                              av_mul128(av_to128i(c.num), av_to128i(b.den))),
145
0
                    av_mul128(av_to128i(b.den), av_to128i(c.den)));
146
0
}