write: Fix type size for mdcv luminance
[L-SMASH.git] / common / utils.h
blob90365ccdbafa8a478ffd61c397f06e8d4408e7fc
1 /*****************************************************************************
2 * utils.h
3 *****************************************************************************
4 * Copyright (C) 2010-2017 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #ifndef LSMASH_UTIL_H
24 #define LSMASH_UTIL_H
26 #define debug_if(x) if(x)
28 #define LSMASH_MAX( a, b ) ((a) > (b) ? (a) : (b))
29 #define LSMASH_MIN( a, b ) ((a) < (b) ? (a) : (b))
31 #define EXPAND_VA_ARGS( ... ) __VA_ARGS__
33 /* default arguments
34 * Use only CALL_FUNC_DEFAULT_ARGS().
35 * The defined macros can't be passed a macro argument requiring the empty parameter list.
37 * The following is an example.
38 * #define TEMPLATE_A( ... ) CALL_FUNC_DEFAULT_ARGS( TEMPLATE_A, __VA_ARGS__ )
39 * #define TEMPLATE_A_1( _1 ) _1( 1 )
40 * #define TEMPLATE_B( ... ) CALL_FUNC_DEFAULT_ARGS( TEMPLATE_B, __VA_ARGS__ )
41 * #define TEMPLATE_B_2( _1, _2 ) ((_1) + (_2))
42 * #define TEMPLATE_B_1( _1 ) TEMPLATE_B_2( _1, 0 )
43 * #define TEMPLATE_B_0()
44 * int main( void )
45 * {
46 * TEMPLATE_A( TEMPLATE_B_1 ); // OK
47 * TEMPLATE_A( TEMPLATE_B ); // NG
48 * TEMPLATE_B( 1, 2 ); // OK
49 * TEMPLATE_B(); // NG
50 * return 0;
51 * }
52 * */
53 #define NUM_ARGS( _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, ... ) _12
54 #ifdef _MSC_VER
55 #define COUNT_NUM_ARGS( ... ) EXPAND_VA_ARGS( NUM_ARGS( __VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0 ) )
56 #else
57 #define COUNT_NUM_ARGS( ... ) NUM_ARGS( __VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0 )
58 #endif
59 #define GET_FUNC_BY_NUM_ARGS_EXN( func_name, N ) func_name ## _ ## N
60 #define GET_FUNC_BY_NUM_ARGS_EX0( func_name, N ) GET_FUNC_BY_NUM_ARGS_EXN( func_name, N )
61 #define GET_FUNC_BY_NUM_ARGS_EX1( func_name, ... ) GET_FUNC_BY_NUM_ARGS_EX0( func_name, COUNT_NUM_ARGS( __VA_ARGS__ ) )
62 #define CALL_FUNC_DEFAULT_ARGS( func_name, ... ) GET_FUNC_BY_NUM_ARGS_EX1( func_name, __VA_ARGS__ ) ( __VA_ARGS__ )
64 /*---- class ----*/
65 typedef struct
67 char *name;
68 size_t log_level_offset; /* offset in the struct where 'log_level' is placed
69 * If set to 0, 'log_level' is unavailable and implicitly set to LSMASH_LOG_INFO. */
70 } lsmash_class_t;
72 /*---- type ----*/
73 double lsmash_fixed2double( int64_t value, int frac_width );
74 float lsmash_int2float32( uint32_t value );
75 double lsmash_int2float64( uint64_t value );
77 /*---- others ----*/
78 typedef enum
80 LSMASH_LOG_QUIET = 0,
81 LSMASH_LOG_ERROR,
82 LSMASH_LOG_WARNING,
83 LSMASH_LOG_INFO,
84 } lsmash_log_level;
86 typedef struct
88 uint64_t n;
89 uint64_t d;
90 } lsmash_rational_u64_t;
92 typedef struct
94 int64_t n;
95 uint64_t d;
96 } lsmash_rational_s64_t;
98 void lsmash_log
100 const void *class,
101 lsmash_log_level level,
102 const char *message,
106 void lsmash_log_refresh_line
108 const void *class
111 void lsmash_ifprintf
113 FILE *fp,
114 int indent,
115 const char *format, ...
118 static inline uint32_t lsmash_count_bits
120 uint32_t bits
123 bits = (bits & 0x55555555) + ((bits >> 1) & 0x55555555);
124 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
125 bits = (bits & 0x0f0f0f0f) + ((bits >> 4) & 0x0f0f0f0f);
126 bits = (bits & 0x00ff00ff) + ((bits >> 8) & 0x00ff00ff);
127 return (bits & 0x0000ffff) + ((bits >> 16) & 0x0000ffff);
130 static inline size_t lsmash_floor_log2
132 uint64_t value
135 assert( value >= 1 );
136 #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) /* GCC >= 3.4 */
137 return (sizeof(uint64_t) - 1) - __builtin_clzll( value );
138 #else
139 size_t s = 0;
140 while( value )
142 value >>= 1;
143 ++s;
145 return s - 1;
146 #endif
149 static inline size_t lsmash_ceil_log2
151 uint64_t value
154 return lsmash_floor_log2( value ) + ((value & (value - 1)) != 0);
157 int lsmash_compare_dts
159 const lsmash_media_ts_t *a,
160 const lsmash_media_ts_t *b
163 int lsmash_compare_cts
165 const lsmash_media_ts_t *a,
166 const lsmash_media_ts_t *b
169 static inline uint64_t lsmash_get_gcd
171 uint64_t a,
172 uint64_t b
175 if( !b )
176 return a;
177 while( 1 )
179 uint64_t c = a % b;
180 if( !c )
181 return b;
182 a = b;
183 b = c;
187 static inline uint64_t lsmash_get_lcm
189 uint64_t a,
190 uint64_t b
193 if( !a )
194 return 0;
195 return (a / lsmash_get_gcd( a, b )) * b;
198 static inline void lsmash_reduce_fraction
200 uint64_t *a,
201 uint64_t *b
204 if( !a || !b )
205 return;
206 uint64_t gcd = lsmash_get_gcd( *a, *b );
207 if( gcd )
209 *a /= gcd;
210 *b /= gcd;
214 static inline void lsmash_reduce_fraction_su
216 int64_t *a,
217 uint64_t *b
220 if( !a || !b )
221 return;
222 uint64_t c = *a > 0 ? *a : -(*a);
223 uint64_t gcd = lsmash_get_gcd( c, *b );
224 if( gcd )
226 c /= gcd;
227 *b /= gcd;
228 *a = *a > 0 ? (signed)c : -(signed)c;
232 #endif