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.

66 lines
2.0KB

  1. /*
  2. * Rational numbers
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file rational.c
  22. * Rational numbers
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. //#include <math.h>
  26. #include <limits.h>
  27. #include "common.h"
  28. #include "avcodec.h"
  29. #include "rational.h"
  30. AVRational av_mul_q(AVRational b, AVRational c){
  31. av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
  32. return b;
  33. }
  34. AVRational av_div_q(AVRational b, AVRational c){
  35. av_reduce(&b.num, &b.den, b.num * (int64_t)c.den, b.den * (int64_t)c.num, INT_MAX);
  36. return b;
  37. }
  38. AVRational av_add_q(AVRational b, AVRational c){
  39. av_reduce(&b.num, &b.den, b.num * (int64_t)c.den + c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX);
  40. return b;
  41. }
  42. AVRational av_sub_q(AVRational b, AVRational c){
  43. av_reduce(&b.num, &b.den, b.num * (int64_t)c.den - c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX);
  44. return b;
  45. }
  46. /**
  47. * Converts a double precission floating point number to a AVRational.
  48. * @param max the maximum allowed numerator and denominator
  49. */
  50. AVRational av_d2q(double d, int max){
  51. AVRational a;
  52. int exponent= FFMAX( (int)(log(ABS(d) + 1e-20)/log(2)), 0);
  53. int64_t den= 1LL << (61 - exponent);
  54. av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
  55. return a;
  56. }