FFmpeg
rational.h
Go to the documentation of this file.
1 /*
2  * rational numbers
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
29 #ifndef AVUTIL_RATIONAL_H
30 #define AVUTIL_RATIONAL_H
31 
32 #include <stdint.h>
33 #include <limits.h>
34 #include "attributes.h"
35 
58 typedef struct AVRational{
59  int num;
60  int den;
61 } AVRational;
62 
71 static inline AVRational av_make_q(int num, int den)
72 {
73  AVRational r = { num, den };
74  return r;
75 }
76 
89 static inline int av_cmp_q(AVRational a, AVRational b){
90  const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
91 
92  if(tmp) return (int)((tmp ^ a.den ^ b.den)>>63)|1;
93  else if(b.den && a.den) return 0;
94  else if(a.num && b.num) return (a.num>>31) - (b.num>>31);
95  else return INT_MIN;
96 }
97 
104 static inline double av_q2d(AVRational a){
105  return a.num / (double) a.den;
106 }
107 
120 int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max);
121 
129 
137 
145 
153 
159 static av_always_inline AVRational av_inv_q(AVRational q)
160 {
161  AVRational r = { q.den, q.num };
162  return r;
163 }
164 
176 AVRational av_d2q(double d, int max) av_const;
177 
189 
197 int av_find_nearest_q_idx(AVRational q, const AVRational* q_list);
198 
208 uint32_t av_q2intfloat(AVRational q);
209 
214 #endif /* AVUTIL_RATIONAL_H */
AVRational av_div_q(AVRational b, AVRational c) av_const
Divide one rational by another.
Definition: rational.c:88
int num
Numerator.
Definition: rational.h:59
uint32_t av_q2intfloat(AVRational q)
Convert an AVRational to a IEEE 32-bit float expressed in fixed-point format.
Definition: rational.c:152
Macro definitions for various function/variable attributes.
AVRational av_sub_q(AVRational b, AVRational c) av_const
Subtract one rational from another.
Definition: rational.c:101
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int av_find_nearest_q_idx(AVRational q, const AVRational *q_list)
Find the value in a list of rationals nearest a given reference rational.
Definition: rational.c:142
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVRational av_d2q(double d, int max) av_const
Convert a double precision floating point number to a rational.
Definition: rational.c:106
AVRational av_add_q(AVRational b, AVRational c) av_const
Add two rationals.
Definition: rational.c:93
int av_nearer_q(AVRational q, AVRational q1, AVRational q2)
Find which of the two rationals is closer to another rational.
Definition: rational.c:127
int den
Denominator.
Definition: rational.h:60
AVRational av_mul_q(AVRational b, AVRational c) av_const
Multiply two rationals.
Definition: rational.c:80
struct AVRational AVRational
Rational number (pair of numerator and denominator).