1 /* 2 * copyright (c) 2005-2012 Michael Niedermayer <michaelni@gmx.at> 3 * 4 * This file is part of FFmpeg. 5 * 6 * FFmpeg is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * FFmpeg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with FFmpeg; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 /** 22 * @file 23 * @addtogroup lavu_math 24 * Mathematical utilities for working with timestamp and time base. 25 */ 26 27 module ffmpeg.libavutil.mathematics; 28 29 import ffmpeg.libavutil.rational; 30 31 extern (C): 32 import ffmpeg; @nogc nothrow: 33 34 /* e */ 35 36 /* log_e 2 */ 37 38 /* log_e 10 */ 39 40 enum M_LOG2_10 = 3.32192809488736234787; /* log_2 10 */ 41 42 enum M_PHI = 1.61803398874989484820; /* phi / golden ratio */ 43 44 /* pi */ 45 46 /* pi/2 */ 47 48 /* 1/sqrt(2) */ 49 50 /* sqrt(2) */ 51 52 /** 53 * @addtogroup lavu_math 54 * 55 * @{ 56 */ 57 58 /** 59 * Rounding methods. 60 */ 61 enum AVRounding 62 { 63 AV_ROUND_ZERO = 0, ///< Round toward zero. 64 AV_ROUND_INF = 1, ///< Round away from zero. 65 AV_ROUND_DOWN = 2, ///< Round toward -infinity. 66 AV_ROUND_UP = 3, ///< Round toward +infinity. 67 AV_ROUND_NEAR_INF = 5, ///< Round to nearest and halfway cases away from zero. 68 /** 69 * Flag telling rescaling functions to pass `INT64_MIN`/`MAX` through 70 * unchanged, avoiding special cases for #AV_NOPTS_VALUE. 71 * 72 * Unlike other values of the enumeration AVRounding, this value is a 73 * bitmask that must be used in conjunction with another value of the 74 * enumeration through a bitwise OR, in order to set behavior for normal 75 * cases. 76 * 77 * @code{.c} 78 * av_rescale_rnd(3, 1, 2, AV_ROUND_UP | AV_ROUND_PASS_MINMAX); 79 * // Rescaling 3: 80 * // Calculating 3 * 1 / 2 81 * // 3 / 2 is rounded up to 2 82 * // => 2 83 * 84 * av_rescale_rnd(AV_NOPTS_VALUE, 1, 2, AV_ROUND_UP | AV_ROUND_PASS_MINMAX); 85 * // Rescaling AV_NOPTS_VALUE: 86 * // AV_NOPTS_VALUE == INT64_MIN 87 * // AV_NOPTS_VALUE is passed through 88 * // => AV_NOPTS_VALUE 89 * @endcode 90 */ 91 AV_ROUND_PASS_MINMAX = 8192 92 } 93 94 /** 95 * Compute the greatest common divisor of two integer operands. 96 * 97 * @param a,b Operands 98 * @return GCD of a and b up to sign; if a >= 0 and b >= 0, return value is >= 0; 99 * if a == 0 and b == 0, returns 0. 100 */ 101 long av_gcd (long a, long b); 102 103 /** 104 * Rescale a 64-bit integer with rounding to nearest. 105 * 106 * The operation is mathematically equivalent to `a * b / c`, but writing that 107 * directly can overflow. 108 * 109 * This function is equivalent to av_rescale_rnd() with #AV_ROUND_NEAR_INF. 110 * 111 * @see av_rescale_rnd(), av_rescale_q(), av_rescale_q_rnd() 112 */ 113 long av_rescale (long a, long b, long c); 114 115 /** 116 * Rescale a 64-bit integer with specified rounding. 117 * 118 * The operation is mathematically equivalent to `a * b / c`, but writing that 119 * directly can overflow, and does not support different rounding methods. 120 * 121 * @see av_rescale(), av_rescale_q(), av_rescale_q_rnd() 122 */ 123 long av_rescale_rnd (long a, long b, long c, AVRounding rnd); 124 125 /** 126 * Rescale a 64-bit integer by 2 rational numbers. 127 * 128 * The operation is mathematically equivalent to `a * bq / cq`. 129 * 130 * This function is equivalent to av_rescale_q_rnd() with #AV_ROUND_NEAR_INF. 131 * 132 * @see av_rescale(), av_rescale_rnd(), av_rescale_q_rnd() 133 */ 134 long av_rescale_q (long a, AVRational bq, AVRational cq); 135 136 /** 137 * Rescale a 64-bit integer by 2 rational numbers with specified rounding. 138 * 139 * The operation is mathematically equivalent to `a * bq / cq`. 140 * 141 * @see av_rescale(), av_rescale_rnd(), av_rescale_q() 142 */ 143 long av_rescale_q_rnd (long a, AVRational bq, AVRational cq, AVRounding rnd); 144 145 /** 146 * Compare two timestamps each in its own time base. 147 * 148 * @return One of the following values: 149 * - -1 if `ts_a` is before `ts_b` 150 * - 1 if `ts_a` is after `ts_b` 151 * - 0 if they represent the same position 152 * 153 * @warning 154 * The result of the function is undefined if one of the timestamps is outside 155 * the `int64_t` range when represented in the other's timebase. 156 */ 157 int av_compare_ts (long ts_a, AVRational tb_a, long ts_b, AVRational tb_b); 158 159 /** 160 * Compare the remainders of two integer operands divided by a common divisor. 161 * 162 * In other words, compare the least significant `log2(mod)` bits of integers 163 * `a` and `b`. 164 * 165 * @code{.c} 166 * av_compare_mod(0x11, 0x02, 0x10) < 0 // since 0x11 % 0x10 (0x1) < 0x02 % 0x10 (0x2) 167 * av_compare_mod(0x11, 0x02, 0x20) > 0 // since 0x11 % 0x20 (0x11) > 0x02 % 0x20 (0x02) 168 * @endcode 169 * 170 * @param a,b Operands 171 * @param mod Divisor; must be a power of 2 172 * @return 173 * - a negative value if `a % mod < b % mod` 174 * - a positive value if `a % mod > b % mod` 175 * - zero if `a % mod == b % mod` 176 */ 177 long av_compare_mod (ulong a, ulong b, ulong mod); 178 179 /** 180 * Rescale a timestamp while preserving known durations. 181 * 182 * This function is designed to be called per audio packet to scale the input 183 * timestamp to a different time base. Compared to a simple av_rescale_q() 184 * call, this function is robust against possible inconsistent frame durations. 185 * 186 * The `last` parameter is a state variable that must be preserved for all 187 * subsequent calls for the same stream. For the first call, `*last` should be 188 * initialized to #AV_NOPTS_VALUE. 189 * 190 * @param[in] in_tb Input time base 191 * @param[in] in_ts Input timestamp 192 * @param[in] fs_tb Duration time base; typically this is finer-grained 193 * (greater) than `in_tb` and `out_tb` 194 * @param[in] duration Duration till the next call to this function (i.e. 195 * duration of the current packet/frame) 196 * @param[in,out] last Pointer to a timestamp expressed in terms of 197 * `fs_tb`, acting as a state variable 198 * @param[in] out_tb Output timebase 199 * @return Timestamp expressed in terms of `out_tb` 200 * 201 * @note In the context of this function, "duration" is in term of samples, not 202 * seconds. 203 */ 204 long av_rescale_delta (AVRational in_tb, long in_ts, AVRational fs_tb, int duration, long* last, AVRational out_tb); 205 206 /** 207 * Add a value to a timestamp. 208 * 209 * This function guarantees that when the same value is repeatly added that 210 * no accumulation of rounding errors occurs. 211 * 212 * @param[in] ts Input timestamp 213 * @param[in] ts_tb Input timestamp time base 214 * @param[in] inc Value to be added 215 * @param[in] inc_tb Time base of `inc` 216 */ 217 long av_add_stable (AVRational ts_tb, long ts, AVRational inc_tb, long inc); 218 219 /** 220 * @} 221 */ 222 223 /* AVUTIL_MATHEMATICS_H */