Add ticker_abs function that returns the number of ticks to get the
[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 }
42
43 static inline INT64 ticker_abs(Ticker *tick, int num)
44 {
45 INT64 n = (INT64) num * tick->div;
46 INT64 value = (INT64) num * tick->mod;
47
48 if (value > 0) {
49 n += (value / tick->inrate);
50 value = value % tick->inrate;
51 if (value > 0) {
52 /* value -= tick->inrate; */
53 n++;
54 }
55 }
56 return n;
57 }