FFmpeg
rational64.h
Go to the documentation of this file.
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  * @ingroup lavu_math_rational
26  * 64-bit extension of AVRational.
27  * @author Niklas Haas
28  */
29 
30 #ifndef SWSCALE_RATIONAL64_H
31 #define SWSCALE_RATIONAL64_H
32 
33 #include <stdint.h>
34 #include <limits.h>
35 
36 #include "libavutil/attributes.h"
37 
38 /**
39  * @defgroup AVRational64
40  * 64-bit extension of AVRational
41  *
42  * Offers a 64-bit extended version of AVRational. This is less efficient (and
43  * may revolve around emulated 128-bit multiplications internally), but allows
44  * to represent a much larger range of rational numbers without overflow.
45  *
46  * @{
47  */
48 
49 /**
50  * 64-bit Rational number (pair of numerator and denominator).
51  */
52 typedef struct AVRational64 {
53  int64_t num; ///< Numerator
54  int64_t den; ///< Denominator
55 } AVRational64;
56 
57 /**
58  * Create an AVRational64.
59  *
60  * Useful for compilers that do not support compound literals.
61  *
62  * @note The return value is not reduced.
63  */
64 static inline AVRational64 av_make_q64(int64_t num, int64_t den)
65 {
66  AVRational64 r = { num, den };
67  return r;
68 }
69 
70 /**
71  * Compare two 64-bit rationals.
72  *
73  * @param a First rational
74  * @param b Second rational
75  *
76  * @return One of the following values:
77  * - 0 if `a == b`
78  * - 1 if `a > b`
79  * - -1 if `a < b`
80  * - `INT_MIN` if one of the values is of the form `0 / 0`
81  */
83 
84 /**
85  * Convert an AVRational64 to a `double`.
86  * @param a AVRational64 to convert
87  * @return `a` in floating-point form
88  * @see av_d2q()
89  */
90 static inline double av_q2d_64(AVRational64 a){
91  return a.num / (double) a.den;
92 }
93 
94 /**
95  * Multiply two 64-bit rationals.
96  * @param b First multiplicant
97  * @param c Second multiplicant
98  * @return b*c
99  */
101 
102 /**
103  * Divide one 64-bit rational by another.
104  * @param b Dividend
105  * @param c Divisor
106  * @return b/c
107  */
109 
110 /**
111  * Add two 64-bit rationals.
112  * @param b First addend
113  * @param c Second addend
114  * @return b+c
115  */
117 
118 /**
119  * Subtract one 64-bit rational from another.
120  * @param b Minuend
121  * @param c Subtrahend
122  * @return b-c
123  */
125 
126 /**
127  * Invert a 64-bit rational.
128  * @param q value
129  * @return 1 / q
130  */
132 {
133  AVRational64 r = { q.den, q.num };
134  return r;
135 }
136 
137 /**
138  * Return the best rational so that a and b are multiple of it.
139  * If the resulting denominator is larger than max_den, return def.
140  */
142 
143 /**
144  * @}
145  */
146 
147 #endif /* SWSCALE_RATIONAL64_H */
av_gcd_q64
AVRational64 av_gcd_q64(AVRational64 a, AVRational64 b, int max_den, AVRational64 def)
Return the best rational so that a and b are multiple of it.
r
const char * r
Definition: vf_curves.c:127
int64_t
long long int64_t
Definition: coverity.c:34
b
#define b
Definition: input.c:43
av_always_inline
#define av_always_inline
Definition: attributes.h:76
av_const
#define av_const
Definition: attributes.h:113
av_cmp_q64
int av_cmp_q64(AVRational64 a, AVRational64 b)
Compare two 64-bit rationals.
Definition: rational64.c:108
limits.h
av_mul_q64
AVRational64 av_mul_q64(AVRational64 b, AVRational64 c) av_const
Multiply two 64-bit rationals.
Definition: rational64.c:124
double
double
Definition: af_crystalizer.c:132
attributes.h
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_div_q64
AVRational64 av_div_q64(AVRational64 b, AVRational64 c) av_const
Divide one 64-bit rational by another.
Definition: rational64.c:130
av_sub_q64
AVRational64 av_sub_q64(AVRational64 b, AVRational64 c) av_const
Subtract one 64-bit rational from another.
Definition: rational64.c:141
AVRational64
64-bit Rational number (pair of numerator and denominator).
Definition: rational64.h:52
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AVRational64::den
int64_t den
Denominator.
Definition: rational64.h:54
av_q2d_64
static double av_q2d_64(AVRational64 a)
Convert an AVRational64 to a double.
Definition: rational64.h:90
av_make_q64
static AVRational64 av_make_q64(int64_t num, int64_t den)
Create an AVRational64.
Definition: rational64.h:64
AVRational64::num
int64_t num
Numerator.
Definition: rational64.h:53
av_inv_q64
static av_always_inline AVRational64 av_inv_q64(AVRational64 q)
Invert a 64-bit rational.
Definition: rational64.h:131
av_add_q64
AVRational64 av_add_q64(AVRational64 b, AVRational64 c) av_const
Add two 64-bit rationals.
Definition: rational64.c:135