read: Skip weird extra bytes of 'chan' box.
[L-SMASH.git] / common / utils.h
blobe399a354124a393b4dd7be68782a00f6b08d5026
1 /*****************************************************************************
2 * utils.h
3 *****************************************************************************
4 * Copyright (C) 2010-2014 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 /* default arguments
32 * Use only CALL_FUNC_DEFAULT_ARGS().
33 * The defined macros can't be passed a macro argument requiring the empty parameter list.
35 * The following is an example.
36 * #define TEMPLATE_A( ... ) CALL_FUNC_DEFAULT_ARGS( TEMPLATE_A, __VA_ARGS__ )
37 * #define TEMPLATE_A_1( _1 ) _1( 1 )
38 * #define TEMPLATE_B( ... ) CALL_FUNC_DEFAULT_ARGS( TEMPLATE_B, __VA_ARGS__ )
39 * #define TEMPLATE_B_2( _1, _2 ) ((_1) + (_2))
40 * #define TEMPLATE_B_1( _1 ) TEMPLATE_B_2( _1, 0 )
41 * #define TEMPLATE_B_0()
42 * int main( void )
43 * {
44 * TEMPLATE_A( TEMPLATE_B_1 ); // OK
45 * TEMPLATE_A( TEMPLATE_B ); // NG
46 * TEMPLATE_B( 1, 2 ); // OK
47 * TEMPLATE_B(); // NG
48 * return 0;
49 * }
50 * */
51 #define NUM_ARGS( _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, ... ) _10
52 #ifdef _MSC_VER
53 #define EXPAND_VA_ARGS( x ) x
54 #define COUNT_NUM_ARGS( ... ) EXPAND_VA_ARGS( NUM_ARGS( __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0 ) )
55 #else
56 #define COUNT_NUM_ARGS( ... ) NUM_ARGS( __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0 )
57 #endif
58 #define GET_FUNC_BY_NUM_ARGS_EXN( func_name, N ) func_name ## _ ## N
59 #define GET_FUNC_BY_NUM_ARGS_EX0( func_name, N ) GET_FUNC_BY_NUM_ARGS_EXN( func_name, N )
60 #define GET_FUNC_BY_NUM_ARGS_EX1( func_name, ... ) GET_FUNC_BY_NUM_ARGS_EX0( func_name, COUNT_NUM_ARGS( __VA_ARGS__ ) )
61 #define CALL_FUNC_DEFAULT_ARGS( func_name, ... ) GET_FUNC_BY_NUM_ARGS_EX1( func_name, __VA_ARGS__ ) ( __VA_ARGS__ )
63 /*---- class ----*/
64 typedef struct
66 char *name;
67 size_t log_level_offset; /* offset in the struct where 'log_level' is placed
68 * If set to 0, 'log_level' is unavailable and implicitly set to LSMASH_LOG_INFO. */
69 } lsmash_class_t;
71 /*---- type ----*/
72 double lsmash_fixed2double( uint64_t value, int frac_width );
73 float lsmash_int2float32( uint32_t value );
74 double lsmash_int2float64( uint64_t value );
76 /*---- others ----*/
77 typedef enum
79 LSMASH_LOG_QUIET = 0,
80 LSMASH_LOG_ERROR,
81 LSMASH_LOG_WARNING,
82 LSMASH_LOG_INFO,
83 } lsmash_log_level;
85 typedef struct
87 uint64_t n;
88 uint64_t d;
89 } lsmash_rational_u64_t;
91 typedef struct
93 int64_t n;
94 uint64_t d;
95 } lsmash_rational_s64_t;
97 void lsmash_log
99 const void *class,
100 lsmash_log_level level,
101 const char *message,
105 void lsmash_log_refresh_line
107 const void *class
110 uint32_t lsmash_count_bits
112 uint32_t bits
115 void lsmash_ifprintf
117 FILE *fp,
118 int indent,
119 const char *format, ...
122 int lsmash_ceil_log2
124 uint64_t value
127 int lsmash_compare_dts
129 const lsmash_media_ts_t *a,
130 const lsmash_media_ts_t *b
133 int lsmash_compare_cts
135 const lsmash_media_ts_t *a,
136 const lsmash_media_ts_t *b
139 static inline uint64_t lsmash_get_gcd
141 uint64_t a,
142 uint64_t b
145 if( !b )
146 return a;
147 while( 1 )
149 uint64_t c = a % b;
150 if( !c )
151 return b;
152 a = b;
153 b = c;
157 static inline uint64_t lsmash_get_lcm
159 uint64_t a,
160 uint64_t b
163 if( !a )
164 return 0;
165 return (a / lsmash_get_gcd( a, b )) * b;
168 static inline void lsmash_reduce_fraction
170 uint64_t *a,
171 uint64_t *b
174 if( !a || !b )
175 return;
176 uint64_t gcd = lsmash_get_gcd( *a, *b );
177 if( gcd )
179 *a /= gcd;
180 *b /= gcd;
184 static inline void lsmash_reduce_fraction_su
186 int64_t *a,
187 uint64_t *b
190 if( !a || !b )
191 return;
192 uint64_t c = *a > 0 ? *a : -(*a);
193 uint64_t gcd = lsmash_get_gcd( c, *b );
194 if( gcd )
196 c /= gcd;
197 *b /= gcd;
198 *a = *a > 0 ? (signed)c : -(signed)c;
202 #endif