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