Mercurial > audlegacy
annotate Plugins/Input/aac/libmp4v2/mp4util.h @ 790:8f437afc4f4a trunk
[svn] - more sanity checking
author | nenolod |
---|---|
date | Fri, 03 Mar 2006 20:09:52 -0800 |
parents | f2dc045d2327 |
children |
rev | line source |
---|---|
61 | 1 /* |
2 * The contents of this file are subject to the Mozilla Public | |
3 * License Version 1.1 (the "License"); you may not use this file | |
4 * except in compliance with the License. You may obtain a copy of | |
5 * the License at http://www.mozilla.org/MPL/ | |
6 * | |
7 * Software distributed under the License is distributed on an "AS | |
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
9 * implied. See the License for the specific language governing | |
10 * rights and limitations under the License. | |
11 * | |
12 * The Original Code is MPEG4IP. | |
13 * | |
14 * The Initial Developer of the Original Code is Cisco Systems Inc. | |
15 * Portions created by Cisco Systems Inc. are | |
16 * Copyright (C) Cisco Systems Inc. 2001. All Rights Reserved. | |
17 * | |
18 * Contributor(s): | |
19 * Dave Mackie dmackie@cisco.com | |
20 */ | |
21 | |
22 #ifndef __MP4_UTIL_INCLUDED__ | |
23 #define __MP4_UTIL_INCLUDED__ | |
24 #include <assert.h> | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
25 #include <sys/time.h> |
61 | 26 |
27 #ifndef ASSERT | |
28 #ifdef NDEBUG | |
29 #define ASSERT(expr) | |
30 #else | |
31 #define ASSERT(expr) \ | |
32 if (!(expr)) { \ | |
33 fflush(stdout); \ | |
34 assert((expr)); \ | |
35 } | |
36 #endif | |
37 #endif | |
38 #ifdef NDEBUG | |
39 #define WARNING(expr) | |
40 #else | |
41 #define WARNING(expr) \ | |
42 if (expr) { \ | |
43 fflush(stdout); \ | |
44 fprintf(stderr, "Warning (%s) in %s at line %u\n", \ | |
45 __STRING(expr), __FILE__, __LINE__); \ | |
46 } | |
47 #endif | |
48 | |
49 #define VERBOSE(exprverbosity, verbosity, expr) \ | |
50 if (((exprverbosity) & (verbosity)) == (exprverbosity)) { expr; } | |
51 | |
52 #define VERBOSE_ERROR(verbosity, expr) \ | |
53 VERBOSE(MP4_DETAILS_ERROR, verbosity, expr) | |
54 | |
55 #define VERBOSE_WARNING(verbosity, expr) \ | |
56 VERBOSE(MP4_DETAILS_WARNING, verbosity, expr) | |
57 | |
58 #define VERBOSE_READ(verbosity, expr) \ | |
59 VERBOSE(MP4_DETAILS_READ, verbosity, expr) | |
60 | |
61 #define VERBOSE_READ_TABLE(verbosity, expr) \ | |
62 VERBOSE((MP4_DETAILS_READ | MP4_DETAILS_TABLE), verbosity, expr) | |
63 | |
64 #define VERBOSE_READ_SAMPLE(verbosity, expr) \ | |
65 VERBOSE((MP4_DETAILS_READ | MP4_DETAILS_SAMPLE), verbosity, expr) | |
66 | |
67 #define VERBOSE_READ_HINT(verbosity, expr) \ | |
68 VERBOSE((MP4_DETAILS_READ | MP4_DETAILS_HINT), verbosity, expr) | |
69 | |
70 #define VERBOSE_WRITE(verbosity, expr) \ | |
71 VERBOSE(MP4_DETAILS_WRITE, verbosity, expr) | |
72 | |
73 #define VERBOSE_WRITE_TABLE(verbosity, expr) \ | |
74 VERBOSE((MP4_DETAILS_WRITE | MP4_DETAILS_TABLE), verbosity, expr) | |
75 | |
76 #define VERBOSE_WRITE_SAMPLE(verbosity, expr) \ | |
77 VERBOSE((MP4_DETAILS_WRITE | MP4_DETAILS_SAMPLE), verbosity, expr) | |
78 | |
79 #define VERBOSE_WRITE_HINT(verbosity, expr) \ | |
80 VERBOSE((MP4_DETAILS_WRITE | MP4_DETAILS_HINT), verbosity, expr) | |
81 | |
82 #define VERBOSE_FIND(verbosity, expr) \ | |
83 VERBOSE(MP4_DETAILS_FIND, verbosity, expr) | |
84 | |
85 #define VERBOSE_ISMA(verbosity, expr) \ | |
86 VERBOSE(MP4_DETAILS_ISMA, verbosity, expr) | |
87 | |
88 #define VERBOSE_EDIT(verbosity, expr) \ | |
89 VERBOSE(MP4_DETAILS_EDIT, verbosity, expr) | |
90 | |
91 inline void Indent(FILE* pFile, u_int8_t depth) { | |
92 fprintf(pFile, "%*c", depth, ' '); | |
93 } | |
94 | |
95 static inline void MP4Printf(const char* fmt, ...) | |
96 #ifndef _WIN32 | |
97 __attribute__((format(__printf__, 1, 2))) | |
98 #endif | |
99 ; | |
100 | |
101 static inline void MP4Printf(const char* fmt, ...) | |
102 { | |
103 va_list ap; | |
104 va_start(ap, fmt); | |
105 // TBD API call to set error_msg_func instead of just printf | |
106 vprintf(fmt, ap); | |
107 va_end(ap); | |
108 } | |
109 | |
110 class MP4Error { | |
111 public: | |
112 MP4Error() { | |
113 m_errno = 0; | |
114 m_errstring = NULL; | |
115 m_where = NULL; | |
116 m_free = 0; | |
117 } | |
118 ~MP4Error() { | |
119 if (m_free != 0) { | |
120 free((void *)m_errstring); | |
121 } | |
122 } | |
123 MP4Error(int err, const char* where = NULL) { | |
124 m_errno = err; | |
125 m_errstring = NULL; | |
126 m_where = where; | |
127 m_free = 0; | |
128 } | |
129 MP4Error(const char *format, const char *where, ...) { | |
130 char *string; | |
131 m_errno = 0; | |
132 string = (char *)malloc(512); | |
133 m_where = where; | |
134 if (string) { | |
135 va_list ap; | |
136 va_start(ap, where); | |
137 vsnprintf(string, 512, format, ap); | |
138 va_end(ap); | |
139 m_errstring = string; | |
140 m_free = 1; | |
141 } else { | |
142 m_errstring = format; | |
143 m_free = 0; | |
144 } | |
145 } | |
146 MP4Error(int err, const char* format, const char* where, ...) { | |
147 char *string; | |
148 m_errno = err; | |
149 string = (char *)malloc(512); | |
150 m_where = where; | |
151 if (string) { | |
152 va_list ap; | |
153 va_start(ap, where); | |
154 vsnprintf(string, 512, format, ap); | |
155 va_end(ap); | |
156 m_errstring = string; | |
157 m_free = 1; | |
158 } else { | |
159 m_errstring = format; | |
160 m_free = 0; | |
161 } | |
162 } | |
163 | |
164 void Print(FILE* pFile = stderr); | |
165 int m_free; | |
166 int m_errno; | |
167 const char* m_errstring; | |
168 const char* m_where; | |
169 }; | |
170 | |
171 void MP4HexDump( | |
172 u_int8_t* pBytes, u_int32_t numBytes, | |
173 FILE* pFile = stdout, u_int8_t indent = 0); | |
174 | |
175 inline void* MP4Malloc(size_t size) { | |
176 void* p = malloc(size); | |
177 if (p == NULL && size > 0) { | |
178 throw new MP4Error(errno); | |
179 } | |
180 return p; | |
181 } | |
182 | |
183 inline void* MP4Calloc(size_t size) { | |
184 return memset(MP4Malloc(size), 0, size); | |
185 } | |
186 | |
187 inline char* MP4Stralloc(const char* s1) { | |
188 char* s2 = (char*)MP4Malloc(strlen(s1) + 1); | |
189 strcpy(s2, s1); | |
190 return s2; | |
191 } | |
192 | |
193 inline void* MP4Realloc(void* p, u_int32_t newSize) { | |
194 // workaround library bug | |
195 if (p == NULL && newSize == 0) { | |
196 return NULL; | |
197 } | |
198 p = realloc(p, newSize); | |
199 if (p == NULL && newSize > 0) { | |
200 throw new MP4Error(errno); | |
201 } | |
202 return p; | |
203 } | |
204 | |
205 inline void MP4Free(void* p) { | |
206 free(p); | |
207 } | |
208 | |
209 inline u_int32_t STRTOINT32(const char* s) { | |
201 | 210 #ifdef WORDS_BIGENDIAN |
211 return (*(u_int32_t *)s); | |
212 #else | |
213 return htonl(*(uint32_t *)s); | |
214 #endif | |
215 #if 0 | |
61 | 216 return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; |
201 | 217 #endif |
61 | 218 } |
219 | |
220 inline void INT32TOSTR(u_int32_t i, char* s) { | |
201 | 221 #ifdef WORDS_BIGENDIAN |
222 *(uint32_t *)s = i; | |
223 s[4] = 0; | |
224 #else | |
61 | 225 s[0] = ((i >> 24) & 0xFF); s[1] = ((i >> 16) & 0xFF); |
226 s[2] = ((i >> 8) & 0xFF); s[3] = (i & 0xFF); s[4] = 0; | |
201 | 227 #endif |
61 | 228 } |
229 | |
230 inline MP4Timestamp MP4GetAbsTimestamp() { | |
231 struct timeval tv; | |
232 gettimeofday(&tv, NULL); | |
233 MP4Timestamp ret; | |
234 ret = tv.tv_sec; | |
235 ret += 2082844800; | |
236 return ret; // MP4 start date is 1/1/1904 | |
237 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60 | |
238 } | |
239 | |
240 u_int64_t MP4ConvertTime(u_int64_t t, | |
241 u_int32_t oldTimeScale, u_int32_t newTimeScale); | |
242 | |
243 bool MP4NameFirstMatches(const char* s1, const char* s2); | |
244 | |
245 bool MP4NameFirstIndex(const char* s, u_int32_t* pIndex); | |
246 | |
247 char* MP4NameFirst(const char *s); | |
248 | |
249 const char* MP4NameAfterFirst(const char *s); | |
250 | |
251 char* MP4ToBase16(const u_int8_t* pData, u_int32_t dataSize); | |
252 | |
253 char* MP4ToBase64(const u_int8_t* pData, u_int32_t dataSize); | |
254 | |
255 const char* MP4NormalizeTrackType(const char* type, | |
256 uint32_t verbosity); | |
257 | |
258 #endif /* __MP4_UTIL_INCLUDED__ */ |