| Line | Branch | Exec | 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 | * @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 | 209850188 | static inline AVRational64 av_make_q64(int64_t num, int64_t den) | |
| 65 | { | ||
| 66 | 209850188 | AVRational64 r = { num, den }; | |
| 67 | 209850188 | 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 | */ | ||
| 82 | int av_cmp_q64(AVRational64 a, AVRational64 b); | ||
| 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 | 5915115 | static inline double av_q2d_64(AVRational64 a){ | |
| 91 | 5915115 | 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 | */ | ||
| 100 | AVRational64 av_mul_q64(AVRational64 b, AVRational64 c) av_const; | ||
| 101 | |||
| 102 | /** | ||
| 103 | * Divide one 64-bit rational by another. | ||
| 104 | * @param b Dividend | ||
| 105 | * @param c Divisor | ||
| 106 | * @return b/c | ||
| 107 | */ | ||
| 108 | AVRational64 av_div_q64(AVRational64 b, AVRational64 c) av_const; | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Add two 64-bit rationals. | ||
| 112 | * @param b First addend | ||
| 113 | * @param c Second addend | ||
| 114 | * @return b+c | ||
| 115 | */ | ||
| 116 | AVRational64 av_add_q64(AVRational64 b, AVRational64 c) av_const; | ||
| 117 | |||
| 118 | /** | ||
| 119 | * Subtract one 64-bit rational from another. | ||
| 120 | * @param b Minuend | ||
| 121 | * @param c Subtrahend | ||
| 122 | * @return b-c | ||
| 123 | */ | ||
| 124 | AVRational64 av_sub_q64(AVRational64 b, AVRational64 c) av_const; | ||
| 125 | |||
| 126 | /** | ||
| 127 | * Invert a 64-bit rational. | ||
| 128 | * @param q value | ||
| 129 | * @return 1 / q | ||
| 130 | */ | ||
| 131 | 3182500 | static av_always_inline AVRational64 av_inv_q64(AVRational64 q) | |
| 132 | { | ||
| 133 | 3182500 | AVRational64 r = { q.den, q.num }; | |
| 134 | 3182500 | return r; | |
| 135 | } | ||
| 136 | |||
| 137 | /** | ||
| 138 | * @} | ||
| 139 | */ | ||
| 140 | |||
| 141 | #endif /* SWSCALE_RATIONAL64_H */ | ||
| 142 |