You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3KB

  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. #include "avcodec.h"
  8. typedef struct Ticker {
  9. int value;
  10. int inrate;
  11. int outrate;
  12. int div;
  13. int mod;
  14. } Ticker;
  15. extern void ticker_init(Ticker *tick, INT64 inrate, INT64 outrate);
  16. static inline int ticker_tick(Ticker *tick, int num)
  17. {
  18. int n = num * tick->div;
  19. tick->value += num * tick->mod;
  20. #if 1
  21. if (tick->value > 0) {
  22. n += (tick->value / tick->inrate);
  23. tick->value = tick->value % tick->inrate;
  24. if (tick->value > 0) {
  25. tick->value -= tick->inrate;
  26. n++;
  27. }
  28. }
  29. #else
  30. while (tick->value > 0) {
  31. tick->value -= tick->inrate;
  32. n++;
  33. }
  34. #endif
  35. return n;
  36. }
  37. static inline INT64 ticker_abs(Ticker *tick, int num)
  38. {
  39. INT64 n = (INT64) num * tick->div;
  40. INT64 value = (INT64) num * tick->mod;
  41. if (value > 0) {
  42. n += (value / tick->inrate);
  43. value = value % tick->inrate;
  44. if (value > 0) {
  45. /* value -= tick->inrate; */
  46. n++;
  47. }
  48. }
  49. return n;
  50. }