annotate lib/libavutil/rational.h @ 2134:992d2e6f907d

preparation for libavcodec
author zipi
date Tue, 31 Dec 2013 14:52:14 +0000
parents
children
rev   line source
2134
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
1 /*
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
2 * rational numbers
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
4 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
5 * This file is part of FFmpeg.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
6 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
11 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
15 * Lesser General Public License for more details.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
16 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
20 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
21
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
22 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
23 * @file
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
24 * rational numbers
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
25 * @author Michael Niedermayer <michaelni@gmx.at>
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
26 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
27
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
28 #ifndef AVUTIL_RATIONAL_H
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
29 #define AVUTIL_RATIONAL_H
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
30
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
31 #include <stdint.h>
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
32 #include <limits.h>
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
33 #include "attributes.h"
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
34
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
35 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
36 * @addtogroup lavu_math
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
37 * @{
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
38 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
39
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
40 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
41 * rational number numerator/denominator
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
42 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
43 typedef struct AVRational{
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
44 int num; ///< numerator
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
45 int den; ///< denominator
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
46 } AVRational;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
47
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
48 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
49 * Compare two rationals.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
50 * @param a first rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
51 * @param b second rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
52 * @return 0 if a==b, 1 if a>b, -1 if a<b, and INT_MIN if one of the
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
53 * values is of the form 0/0
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
54 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
55 static inline int av_cmp_q(AVRational a, AVRational b){
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
56 const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
57
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
58 if(tmp) return ((tmp ^ a.den ^ b.den)>>63)|1;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
59 else if(b.den && a.den) return 0;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
60 else if(a.num && b.num) return (a.num>>31) - (b.num>>31);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
61 else return INT_MIN;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
62 }
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
63
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
64 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
65 * Convert rational to double.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
66 * @param a rational to convert
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
67 * @return (double) a
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
68 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
69 static inline double av_q2d(AVRational a){
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
70 return a.num / (double) a.den;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
71 }
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
72
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
73 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
74 * Reduce a fraction.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
75 * This is useful for framerate calculations.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
76 * @param dst_num destination numerator
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
77 * @param dst_den destination denominator
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
78 * @param num source numerator
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
79 * @param den source denominator
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
80 * @param max the maximum allowed for dst_num & dst_den
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
81 * @return 1 if exact, 0 otherwise
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
82 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
83 int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
84
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
85 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
86 * Multiply two rationals.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
87 * @param b first rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
88 * @param c second rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
89 * @return b*c
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
90 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
91 AVRational av_mul_q(AVRational b, AVRational c) av_const;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
92
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
93 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
94 * Divide one rational by another.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
95 * @param b first rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
96 * @param c second rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
97 * @return b/c
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
98 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
99 AVRational av_div_q(AVRational b, AVRational c) av_const;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
100
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
101 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
102 * Add two rationals.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
103 * @param b first rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
104 * @param c second rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
105 * @return b+c
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
106 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
107 AVRational av_add_q(AVRational b, AVRational c) av_const;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
108
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
109 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
110 * Subtract one rational from another.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
111 * @param b first rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
112 * @param c second rational
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
113 * @return b-c
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
114 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
115 AVRational av_sub_q(AVRational b, AVRational c) av_const;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
116
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
117 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
118 * Invert a rational.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
119 * @param q value
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
120 * @return 1 / q
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
121 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
122 static av_always_inline AVRational av_inv_q(AVRational q)
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
123 {
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
124 AVRational r = { q.den, q.num };
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
125 return r;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
126 }
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
127
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
128 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
129 * Convert a double precision floating point number to a rational.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
130 * inf is expressed as {1,0} or {-1,0} depending on the sign.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
131 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
132 * @param d double to convert
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
133 * @param max the maximum allowed numerator and denominator
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
134 * @return (AVRational) d
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
135 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
136 AVRational av_d2q(double d, int max) av_const;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
137
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
138 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
139 * @return 1 if q1 is nearer to q than q2, -1 if q2 is nearer
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
140 * than q1, 0 if they have the same distance.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
141 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
142 int av_nearer_q(AVRational q, AVRational q1, AVRational q2);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
143
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
144 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
145 * Find the nearest value in q_list to q.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
146 * @param q_list an array of rationals terminated by {0, 0}
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
147 * @return the index of the nearest value found in the array
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
148 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
149 int av_find_nearest_q_idx(AVRational q, const AVRational* q_list);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
150
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
151 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
152 * @}
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
153 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
154
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
155 #endif /* AVUTIL_RATIONAL_H */