16
|
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>
|
|
25
|
|
26 #ifndef ASSERT
|
|
27 #ifdef NDEBUG
|
|
28 #define ASSERT(expr)
|
|
29 #else
|
|
30 #define ASSERT(expr) \
|
|
31 if (!(expr)) { \
|
|
32 fflush(stdout); \
|
|
33 assert((expr)); \
|
|
34 }
|
|
35 #endif
|
|
36 #endif
|
|
37 #ifdef NDEBUG
|
|
38 #define WARNING(expr)
|
|
39 #else
|
|
40 #define WARNING(expr) \
|
|
41 if (expr) { \
|
|
42 fflush(stdout); \
|
|
43 fprintf(stderr, "Warning (%s) in %s at line %u\n", \
|
|
44 __STRING(expr), __FILE__, __LINE__); \
|
|
45 }
|
|
46 #endif
|
|
47
|
|
48 #define VERBOSE(exprverbosity, verbosity, expr) \
|
|
49 if (((exprverbosity) & (verbosity)) == (exprverbosity)) { expr; }
|
|
50
|
|
51 #define VERBOSE_ERROR(verbosity, expr) \
|
|
52 VERBOSE(MP4_DETAILS_ERROR, verbosity, expr)
|
|
53
|
|
54 #define VERBOSE_WARNING(verbosity, expr) \
|
|
55 VERBOSE(MP4_DETAILS_WARNING, verbosity, expr)
|
|
56
|
|
57 #define VERBOSE_READ(verbosity, expr) \
|
|
58 VERBOSE(MP4_DETAILS_READ, verbosity, expr)
|
|
59
|
|
60 #define VERBOSE_READ_TABLE(verbosity, expr) \
|
|
61 VERBOSE((MP4_DETAILS_READ | MP4_DETAILS_TABLE), verbosity, expr)
|
|
62
|
|
63 #define VERBOSE_READ_SAMPLE(verbosity, expr) \
|
|
64 VERBOSE((MP4_DETAILS_READ | MP4_DETAILS_SAMPLE), verbosity, expr)
|
|
65
|
|
66 #define VERBOSE_READ_HINT(verbosity, expr) \
|
|
67 VERBOSE((MP4_DETAILS_READ | MP4_DETAILS_HINT), verbosity, expr)
|
|
68
|
|
69 #define VERBOSE_WRITE(verbosity, expr) \
|
|
70 VERBOSE(MP4_DETAILS_WRITE, verbosity, expr)
|
|
71
|
|
72 #define VERBOSE_WRITE_TABLE(verbosity, expr) \
|
|
73 VERBOSE((MP4_DETAILS_WRITE | MP4_DETAILS_TABLE), verbosity, expr)
|
|
74
|
|
75 #define VERBOSE_WRITE_SAMPLE(verbosity, expr) \
|
|
76 VERBOSE((MP4_DETAILS_WRITE | MP4_DETAILS_SAMPLE), verbosity, expr)
|
|
77
|
|
78 #define VERBOSE_WRITE_HINT(verbosity, expr) \
|
|
79 VERBOSE((MP4_DETAILS_WRITE | MP4_DETAILS_HINT), verbosity, expr)
|
|
80
|
|
81 #define VERBOSE_FIND(verbosity, expr) \
|
|
82 VERBOSE(MP4_DETAILS_FIND, verbosity, expr)
|
|
83
|
|
84 #define VERBOSE_ISMA(verbosity, expr) \
|
|
85 VERBOSE(MP4_DETAILS_ISMA, verbosity, expr)
|
|
86
|
|
87 #define VERBOSE_EDIT(verbosity, expr) \
|
|
88 VERBOSE(MP4_DETAILS_EDIT, verbosity, expr)
|
|
89
|
|
90 inline void Indent(FILE* pFile, u_int8_t depth) {
|
|
91 fprintf(pFile, "%*c", depth, ' ');
|
|
92 }
|
|
93
|
|
94 static inline void MP4Printf(const char* fmt, ...)
|
|
95 #ifndef _WIN32
|
|
96 __attribute__((format(__printf__, 1, 2)))
|
|
97 #endif
|
|
98 ;
|
|
99
|
|
100 static inline void MP4Printf(const char* fmt, ...)
|
|
101 {
|
|
102 va_list ap;
|
|
103 va_start(ap, fmt);
|
|
104 // TBD API call to set error_msg_func instead of just printf
|
|
105 vprintf(fmt, ap);
|
|
106 va_end(ap);
|
|
107 }
|
|
108
|
|
109 class MP4Error {
|
|
110 public:
|
|
111 MP4Error() {
|
|
112 m_errno = 0;
|
|
113 m_errstring = NULL;
|
|
114 m_where = NULL;
|
|
115 m_free = 0;
|
|
116 }
|
|
117 ~MP4Error() {
|
|
118 if (m_free != 0) {
|
|
119 free((void *)m_errstring);
|
|
120 }
|
|
121 }
|
|
122 MP4Error(int err, const char* where = NULL) {
|
|
123 m_errno = err;
|
|
124 m_errstring = NULL;
|
|
125 m_where = where;
|
|
126 m_free = 0;
|
|
127 }
|
|
128 MP4Error(const char *format, const char *where, ...) {
|
|
129 char *string;
|
|
130 m_errno = 0;
|
|
131 string = (char *)malloc(512);
|
|
132 m_where = where;
|
|
133 if (string) {
|
|
134 va_list ap;
|
|
135 va_start(ap, where);
|
|
136 vsnprintf(string, 512, format, ap);
|
|
137 va_end(ap);
|
|
138 m_errstring = string;
|
|
139 m_free = 1;
|
|
140 } else {
|
|
141 m_errstring = format;
|
|
142 m_free = 0;
|
|
143 }
|
|
144 }
|
|
145 MP4Error(int err, const char* format, const char* where, ...) {
|
|
146 char *string;
|
|
147 m_errno = err;
|
|
148 string = (char *)malloc(512);
|
|
149 m_where = where;
|
|
150 if (string) {
|
|
151 va_list ap;
|
|
152 va_start(ap, where);
|
|
153 vsnprintf(string, 512, format, ap);
|
|
154 va_end(ap);
|
|
155 m_errstring = string;
|
|
156 m_free = 1;
|
|
157 } else {
|
|
158 m_errstring = format;
|
|
159 m_free = 0;
|
|
160 }
|
|
161 }
|
|
162
|
|
163 void Print(FILE* pFile = stderr);
|
|
164 int m_free;
|
|
165 int m_errno;
|
|
166 const char* m_errstring;
|
|
167 const char* m_where;
|
|
168 };
|
|
169
|
|
170 void MP4HexDump(
|
|
171 u_int8_t* pBytes, u_int32_t numBytes,
|
|
172 FILE* pFile = stdout, u_int8_t indent = 0);
|
|
173
|
|
174 inline void* MP4Malloc(size_t size) {
|
|
175 void* p = malloc(size);
|
|
176 if (p == NULL && size > 0) {
|
|
177 throw new MP4Error(errno);
|
|
178 }
|
|
179 return p;
|
|
180 }
|
|
181
|
|
182 inline void* MP4Calloc(size_t size) {
|
|
183 return memset(MP4Malloc(size), 0, size);
|
|
184 }
|
|
185
|
|
186 inline char* MP4Stralloc(const char* s1) {
|
|
187 char* s2 = (char*)MP4Malloc(strlen(s1) + 1);
|
|
188 strcpy(s2, s1);
|
|
189 return s2;
|
|
190 }
|
|
191
|
|
192 inline void* MP4Realloc(void* p, u_int32_t newSize) {
|
|
193 // workaround library bug
|
|
194 if (p == NULL && newSize == 0) {
|
|
195 return NULL;
|
|
196 }
|
|
197 p = realloc(p, newSize);
|
|
198 if (p == NULL && newSize > 0) {
|
|
199 throw new MP4Error(errno);
|
|
200 }
|
|
201 return p;
|
|
202 }
|
|
203
|
|
204 inline void MP4Free(void* p) {
|
|
205 free(p);
|
|
206 }
|
|
207
|
|
208 inline u_int32_t STRTOINT32(const char* s) {
|
|
209 #ifdef WORDS_BIGENDIAN
|
|
210 return (*(u_int32_t *)s);
|
|
211 #else
|
|
212 return htonl(*(uint32_t *)s);
|
|
213 #endif
|
|
214 #if 0
|
|
215 return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
|
|
216 #endif
|
|
217 }
|
|
218
|
|
219 inline void INT32TOSTR(u_int32_t i, char* s) {
|
|
220 #ifdef WORDS_BIGENDIAN
|
|
221 *(uint32_t *)s = i;
|
|
222 s[4] = 0;
|
|
223 #else
|
|
224 s[0] = ((i >> 24) & 0xFF); s[1] = ((i >> 16) & 0xFF);
|
|
225 s[2] = ((i >> 8) & 0xFF); s[3] = (i & 0xFF); s[4] = 0;
|
|
226 #endif
|
|
227 }
|
|
228
|
|
229 inline MP4Timestamp MP4GetAbsTimestamp() {
|
|
230 struct timeval tv;
|
|
231 gettimeofday(&tv, NULL);
|
|
232 MP4Timestamp ret;
|
|
233 ret = tv.tv_sec;
|
|
234 ret += 2082844800;
|
|
235 return ret; // MP4 start date is 1/1/1904
|
|
236 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60
|
|
237 }
|
|
238
|
|
239 u_int64_t MP4ConvertTime(u_int64_t t,
|
|
240 u_int32_t oldTimeScale, u_int32_t newTimeScale);
|
|
241
|
|
242 bool MP4NameFirstMatches(const char* s1, const char* s2);
|
|
243
|
|
244 bool MP4NameFirstIndex(const char* s, u_int32_t* pIndex);
|
|
245
|
|
246 char* MP4NameFirst(const char *s);
|
|
247
|
|
248 const char* MP4NameAfterFirst(const char *s);
|
|
249
|
|
250 char* MP4ToBase16(const u_int8_t* pData, u_int32_t dataSize);
|
|
251
|
|
252 char* MP4ToBase64(const u_int8_t* pData, u_int32_t dataSize);
|
|
253
|
|
254 const char* MP4NormalizeTrackType(const char* type,
|
|
255 uint32_t verbosity);
|
|
256
|
|
257 #endif /* __MP4_UTIL_INCLUDED__ */
|