1 /*****************************************************************************
2 * utils.h
3 *****************************************************************************
4 * Copyright (C) 2012-2015 L-SMASH Works project
5 *
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7 *
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.
11 *
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 *****************************************************************************/
20
21 /* This file is available under an ISC license. */
22
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <assert.h>
26
27 #define MIN( a, b ) ((a) < (b) ? (a) : (b))
28 #define MAX( a, b ) ((a) > (b) ? (a) : (b))
29 #define CLIP_VALUE( value, min, max ) ((value) > (max) ? (max) : (value) < (min) ? (min) : (value))
30
31 #ifndef _countof
32 #define _countof( _Array ) ( sizeof(_Array) / sizeof(_Array[0]) )
33 #endif
34
35 #define LW_STRINGFY( s ) #s
36
37 typedef enum
38 {
39 LW_LOG_INFO = 0,
40 LW_LOG_WARNING,
41 LW_LOG_ERROR,
42 LW_LOG_FATAL,
43 LW_LOG_QUIET,
44 } lw_log_level;
45
46 typedef struct lw_log_handler_tag lw_log_handler_t;
47
48 struct lw_log_handler_tag
49 {
50 const char *name;
51 lw_log_level level;
52 void *priv;
53 void (*show_log)( lw_log_handler_t *, lw_log_level, const char *message );
54 };
55
56 void *lw_malloc_zero
57 (
58 size_t size
59 );
60
61 void lw_free
62 (
63 void *pointer
64 );
65
66 void lw_freep
67 (
68 void *pointer
69 );
70
71 void *lw_memdup
72 (
73 void *src,
74 size_t size
75 );
76
77 const char **lw_tokenize_string
78 (
79 char *str, /* null-terminated string: separator charactors will be replaced with '\0'. */
80 char separator, /* separator */
81 char **bufs /* If NULL, allocate memory block internally, which can be deallocated by lw_freep(). */
82 );
83
84 /*****************************************************************************
85 * Non-public functions
86 *****************************************************************************/
87 void lw_log_show
88 (
89 lw_log_handler_t *lhp,
90 lw_log_level level,
91 const char *format,
92 ...
93 );
94
get_gcd(uint64_t a,uint64_t b)95 static inline uint64_t get_gcd
96 (
97 uint64_t a,
98 uint64_t b
99 )
100 {
101 if( !b )
102 return a;
103 while( 1 )
104 {
105 uint64_t c = a % b;
106 if( !c )
107 return b;
108 a = b;
109 b = c;
110 }
111 }
112
reduce_fraction(uint64_t * a,uint64_t * b)113 static inline uint64_t reduce_fraction
114 (
115 uint64_t *a,
116 uint64_t *b
117 )
118 {
119 uint64_t reduce = get_gcd( *a, *b );
120 *a /= reduce;
121 *b /= reduce;
122 return reduce;
123 }
124
125 int lw_check_file_extension
126 (
127 const char *file_name,
128 const char *extension
129 );
130
131 int lw_try_rational_framerate
132 (
133 double framerate,
134 int64_t *framerate_num,
135 int64_t *framerate_den,
136 uint64_t timebase
137 );
138