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.

78 lines
2.1KB

  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. /**
  31. * returns b*c.
  32. */
  33. AVRational av_mul_q(AVRational b, AVRational c){
  34. av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
  35. return b;
  36. }
  37. /**
  38. * returns b/c.
  39. */
  40. AVRational av_div_q(AVRational b, AVRational c){
  41. av_reduce(&b.num, &b.den, b.num * (int64_t)c.den, b.den * (int64_t)c.num, INT_MAX);
  42. return b;
  43. }
  44. /**
  45. * returns b+c.
  46. */
  47. AVRational av_add_q(AVRational b, AVRational c){
  48. 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);
  49. return b;
  50. }
  51. /**
  52. * returns b-c.
  53. */
  54. AVRational av_sub_q(AVRational b, AVRational c){
  55. 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);
  56. return b;
  57. }
  58. /**
  59. * Converts a double precission floating point number to a AVRational.
  60. * @param max the maximum allowed numerator and denominator
  61. */
  62. AVRational av_d2q(double d, int max){
  63. AVRational a;
  64. int exponent= FFMAX( (int)(log(ABS(d) + 1e-20)/log(2)), 0);
  65. int64_t den= 1LL << (61 - exponent);
  66. av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
  67. return a;
  68. }