8a8e42b81814e7efcd0cfefa5eba92a45ea554fc
[libav.git] / libav / tick.h
1 /* tick.h - Compute successive integer multiples of a rational
2 * number without long-term rounding error.
3 * (c)2002 by Lennert Buytenhek <buytenh@gnu.org>
4 * File licensed under the GPL, see http://www.fsf.org/ for more info.
5 * Dedicated to Marija Kulikova.
6 */
7
8 #include "avcodec.h"
9
10 typedef struct Ticker {
11 int value;
12 int inrate;
13 int outrate;
14 int div;
15 int mod;
16 } Ticker;
17
18 extern void ticker_init(Ticker *tick, INT64 inrate, INT64 outrate);
19
20 static inline int ticker_tick(Ticker *tick, int num)
21 {
22 int n = num * tick->div;
23
24 tick->value += num * tick->mod;
25 #if 1
26 if (tick->value > 0) {
27 n += (tick->value / tick->inrate);
28 tick->value = tick->value % tick->inrate;
29 if (tick->value > 0) {
30 tick->value -= tick->inrate;
31 n++;
32 }
33 }
34 #else
35 while (tick->value > 0) {
36 tick->value -= tick->inrate;
37 n++;
38 }
39 #endif
40 return n;
41 }