mp4util.h 6.3 KB

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