comparison Input/aac/libmp4v2/mp4util.h @ 2:6efb9e514224 trunk

[svn] Import AAC stuff.
author nenolod
date Mon, 24 Oct 2005 10:44:27 -0700
parents
children
comparison
equal deleted inserted replaced
1:cc6293c827ec 2:6efb9e514224
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 inline void MP4Printf(const char* fmt, ...) {
95 va_list ap;
96 va_start(ap, fmt);
97 // TBD API call to set error_msg_func instead of just printf
98 fprintf(stdout, fmt, ap);
99 va_end(ap);
100 }
101
102 class MP4Error {
103 public:
104 MP4Error() {
105 m_errno = 0;
106 m_errstring = NULL;
107 m_where = NULL;
108 m_free = 0;
109 }
110 ~MP4Error() {
111 if (m_free != 0) {
112 free((void *)m_errstring);
113 }
114 }
115 MP4Error(int err, const char* where = NULL) {
116 m_errno = err;
117 m_errstring = NULL;
118 m_where = where;
119 m_free = 0;
120 }
121 MP4Error(const char *format, const char *where, ...) {
122 char *string;
123 m_errno = 0;
124 string = (char *)malloc(512);
125 m_where = where;
126 if (string) {
127 va_list ap;
128 va_start(ap, where);
129 vsnprintf(string, 512, format, ap);
130 va_end(ap);
131 m_errstring = string;
132 m_free = 1;
133 } else {
134 m_errstring = format;
135 m_free = 0;
136 }
137 }
138 MP4Error(int err, const char* format, const char* where, ...) {
139 char *string;
140 m_errno = err;
141 string = (char *)malloc(512);
142 m_where = where;
143 if (string) {
144 va_list ap;
145 va_start(ap, where);
146 vsnprintf(string, 512, format, ap);
147 va_end(ap);
148 m_errstring = string;
149 m_free = 1;
150 } else {
151 m_errstring = format;
152 m_free = 0;
153 }
154 }
155
156 void Print(FILE* pFile = stderr);
157 int m_free;
158 int m_errno;
159 const char* m_errstring;
160 const char* m_where;
161 };
162
163 void MP4HexDump(
164 u_int8_t* pBytes, u_int32_t numBytes,
165 FILE* pFile = stdout, u_int8_t indent = 0);
166
167 inline void* MP4Malloc(size_t size) {
168 void* p = malloc(size);
169 if (p == NULL && size > 0) {
170 throw new MP4Error(errno);
171 }
172 return p;
173 }
174
175 inline void* MP4Calloc(size_t size) {
176 return memset(MP4Malloc(size), 0, size);
177 }
178
179 inline char* MP4Stralloc(const char* s1) {
180 char* s2 = (char*)MP4Malloc(strlen(s1) + 1);
181 strcpy(s2, s1);
182 return s2;
183 }
184
185 inline void* MP4Realloc(void* p, u_int32_t newSize) {
186 // workaround library bug
187 if (p == NULL && newSize == 0) {
188 return NULL;
189 }
190 p = realloc(p, newSize);
191 if (p == NULL && newSize > 0) {
192 throw new MP4Error(errno);
193 }
194 return p;
195 }
196
197 inline void MP4Free(void* p) {
198 free(p);
199 }
200
201 inline u_int32_t STRTOINT32(const char* s) {
202 return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
203 }
204
205 inline void INT32TOSTR(u_int32_t i, char* s) {
206 s[0] = ((i >> 24) & 0xFF); s[1] = ((i >> 16) & 0xFF);
207 s[2] = ((i >> 8) & 0xFF); s[3] = (i & 0xFF); s[4] = 0;
208 }
209
210 inline MP4Timestamp MP4GetAbsTimestamp() {
211 struct timeval tv;
212 gettimeofday(&tv, NULL);
213 return tv.tv_sec + 209606400; // MP4 start date is 1/1/1904
214 }
215
216 u_int64_t MP4ConvertTime(u_int64_t t,
217 u_int32_t oldTimeScale, u_int32_t newTimeScale);
218
219 bool MP4NameFirstMatches(const char* s1, const char* s2);
220
221 bool MP4NameFirstIndex(const char* s, u_int32_t* pIndex);
222
223 char* MP4NameFirst(const char *s);
224
225 const char* MP4NameAfterFirst(const char *s);
226
227 char* MP4ToBase16(const u_int8_t* pData, u_int32_t dataSize);
228
229 char* MP4ToBase64(const u_int8_t* pData, u_int32_t dataSize);
230
231 #endif /* __MP4_UTIL_INCLUDED__ */