1
0

mp4util.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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-2005. All Rights Reserved.
  17. *
  18. * Contributor(s):
  19. * Dave Mackie [email protected]
  20. * Bill May [email protected]
  21. */
  22. #include "mp4common.h"
  23. void MP4Error::Print(FILE* pFile)
  24. {
  25. fprintf(pFile, "MP4ERROR: ");
  26. if (m_where) {
  27. fprintf(pFile, "%s", m_where);
  28. }
  29. if (m_errstring) {
  30. if (m_where) {
  31. fprintf(pFile, ": ");
  32. }
  33. fprintf(pFile, "%s", m_errstring);
  34. }
  35. if (m_errno) {
  36. if (m_where || m_errstring) {
  37. fprintf(pFile, ": ");
  38. }
  39. fprintf(pFile, "%s", strerror(m_errno));
  40. }
  41. fprintf(pFile, "\n");
  42. }
  43. void MP4HexDump(
  44. u_int8_t* pBytes, u_int32_t numBytes,
  45. FILE* pFile, u_int8_t indent)
  46. {
  47. if (pFile == NULL) {
  48. pFile = stdout;
  49. }
  50. Indent(pFile, indent);
  51. fprintf(pFile, "<%u bytes> ", numBytes);
  52. for (u_int32_t i = 0; i < numBytes; i++) {
  53. if ((i % 16) == 0 && numBytes > 16) {
  54. fprintf(pFile, "\n");
  55. Indent(pFile, indent);
  56. }
  57. fprintf(pFile, "%02x ", pBytes[i]);
  58. }
  59. fprintf(pFile, "\n");
  60. }
  61. bool MP4NameFirstMatches(const char* s1, const char* s2)
  62. {
  63. if (s1 == NULL || *s1 == '\0' || s2 == NULL || *s2 == '\0') {
  64. return false;
  65. }
  66. if (*s2 == '*') {
  67. return true;
  68. }
  69. while (*s1 != '\0') {
  70. if (*s2 == '\0' || strchr("[.", *s2)) {
  71. break;
  72. }
  73. if (tolower(*s1) != tolower(*s2)) {
  74. return false;
  75. }
  76. s1++;
  77. s2++;
  78. }
  79. return true;
  80. }
  81. bool MP4NameFirstIndex(const char* s, u_int32_t* pIndex)
  82. {
  83. if (s == NULL) {
  84. return false;
  85. }
  86. while (*s != '\0' && *s != '.') {
  87. if (*s == '[') {
  88. s++;
  89. ASSERT(pIndex);
  90. if (sscanf(s, "%u", pIndex) != 1) {
  91. return false;
  92. }
  93. return true;
  94. }
  95. s++;
  96. }
  97. return false;
  98. }
  99. char* MP4NameFirst(const char *s)
  100. {
  101. if (s == NULL) {
  102. return NULL;
  103. }
  104. const char* end = s;
  105. while (*end != '\0' && *end != '.') {
  106. end++;
  107. }
  108. char* first = (char*)MP4Calloc((end - s) + 1);
  109. if (first) {
  110. strncpy(first, s, end - s);
  111. }
  112. return first;
  113. }
  114. const char* MP4NameAfterFirst(const char *s)
  115. {
  116. if (s == NULL) {
  117. return NULL;
  118. }
  119. while (*s != '\0') {
  120. if (*s == '.') {
  121. s++;
  122. if (*s == '\0') {
  123. return NULL;
  124. }
  125. return s;
  126. }
  127. s++;
  128. }
  129. return NULL;
  130. }
  131. char* MP4ToBase16(const u_int8_t* pData, u_int32_t dataSize)
  132. {
  133. if (dataSize) {
  134. ASSERT(pData);
  135. }
  136. uint size = 2 * dataSize + 1;
  137. char* s = (char*)MP4Calloc(size);
  138. u_int32_t i, j;
  139. for (i = 0, j = 0; i < dataSize; i++) {
  140. size -= snprintf(&s[j], size, "%02x", pData[i]);
  141. j += 2;
  142. }
  143. return s; /* N.B. caller is responsible for free'ing s */
  144. }
  145. char* MP4ToBase64(const u_int8_t* pData, u_int32_t dataSize)
  146. {
  147. if (pData == NULL || dataSize == 0) return NULL;
  148. static const char encoding[64] = {
  149. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  150. 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
  151. 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
  152. 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
  153. };
  154. char* s = (char*)MP4Calloc((((dataSize + 2) * 4) / 3) + 1);
  155. const u_int8_t* src = pData;
  156. if (pData == NULL) return NULL;
  157. char* dest = s;
  158. u_int32_t numGroups = dataSize / 3;
  159. for (u_int32_t i = 0; i < numGroups; i++) {
  160. *dest++ = encoding[src[0] >> 2];
  161. *dest++ = encoding[((src[0] & 0x03) << 4) | (src[1] >> 4)];
  162. *dest++ = encoding[((src[1] & 0x0F) << 2) | (src[2] >> 6)];
  163. *dest++ = encoding[src[2] & 0x3F];
  164. src += 3;
  165. }
  166. if (dataSize % 3 == 1) {
  167. *dest++ = encoding[src[0] >> 2];
  168. *dest++ = encoding[((src[0] & 0x03) << 4)];
  169. *dest++ = '=';
  170. *dest++ = '=';
  171. } else if (dataSize % 3 == 2) {
  172. *dest++ = encoding[src[0] >> 2];
  173. *dest++ = encoding[((src[0] & 0x03) << 4) | (src[1] >> 4)];
  174. *dest++ = encoding[((src[1] & 0x0F) << 2)];
  175. *dest++ = '=';
  176. }
  177. *dest = '\0';
  178. return s; /* N.B. caller is responsible for free'ing s */
  179. }
  180. static bool convertBase64 (const char data, uint8_t *value)
  181. {
  182. static const uint8_t decodingarr64[128] = {
  183. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  184. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  185. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  186. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  187. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  188. 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
  189. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  190. 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  191. 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  192. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  193. 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
  194. 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
  195. 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
  196. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  197. 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  198. 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  199. };
  200. uint8_t index = (uint8_t)data;
  201. if ((index & 0x80) != 0) return false;
  202. if (decodingarr64[index] == 0xff) return false;
  203. *value = decodingarr64[index];
  204. return true;
  205. }
  206. uint8_t *Base64ToBinary (const char *pData, uint32_t decodeSize, uint32_t *pDataSize)
  207. {
  208. uint8_t *ret;
  209. uint32_t size, ix, groups;
  210. if (pData == NULL || decodeSize == 0 || pDataSize == NULL)
  211. return NULL;
  212. if ((decodeSize % 4) != 0) {
  213. // must be multiples of 4 characters
  214. return NULL;
  215. }
  216. size = (decodeSize * 3) / 4;
  217. groups = decodeSize / 4;
  218. ret = (uint8_t *)MP4Calloc(size);
  219. if (ret == NULL) return NULL;
  220. for (ix = 0; ix < groups; ix++) {
  221. uint8_t value[4];
  222. for (uint8_t jx = 0; jx < 4; jx++) {
  223. if (pData[jx] == '=') {
  224. if (ix != (groups - 1)) {
  225. free(ret);
  226. return NULL;
  227. }
  228. size--;
  229. value[jx] = 0;
  230. } else if (convertBase64(pData[jx], &value[jx]) == false) {
  231. free(ret);
  232. return NULL;
  233. }
  234. }
  235. ret[(ix * 3)] = value[0] << 2 | ((value[1] >> 4) & 0x3);
  236. ret[(ix * 3) + 1] = (value[1] << 4) | (value[2] >> 2 & 0xf);
  237. ret[(ix * 3) + 2] = ((value[2] & 0x3) << 6) | value[3];
  238. pData += 4;
  239. }
  240. *pDataSize = size;
  241. return ret;
  242. }
  243. // log2 of value, rounded up
  244. static u_int8_t ilog2(u_int64_t value)
  245. {
  246. u_int64_t powerOf2 = 1;
  247. for (u_int8_t i = 0; i < 64; i++) {
  248. if (value <= powerOf2) {
  249. return i;
  250. }
  251. powerOf2 <<= 1;
  252. }
  253. return 64;
  254. }
  255. u_int64_t MP4ConvertTime(u_int64_t t,
  256. u_int32_t oldTimeScale, u_int32_t newTimeScale)
  257. {
  258. // avoid float point exception
  259. if (oldTimeScale == 0) {
  260. throw new MP4Error("division by zero", "MP4ConvertTime");
  261. }
  262. if (oldTimeScale == newTimeScale)
  263. return t;
  264. // check if we can safely use integer operations
  265. if (ilog2(t) + ilog2(newTimeScale) <= 64) {
  266. double d = ((double) t / (double) oldTimeScale) * (double) newTimeScale;
  267. return (u_int64_t)d;
  268. }
  269. // final resort is to use floating point
  270. //double d = (double)newTimeScale;
  271. //d *= UINT64_TO_DOUBLE(t);
  272. //d /= (double)oldTimeScale;
  273. //d += 0.5; // round up.
  274. double d = (double)newTimeScale;
  275. d /= (double)oldTimeScale;
  276. d *= UINT64_TO_DOUBLE(t);
  277. d += 0.5; // round up.
  278. return (u_int64_t)d;
  279. }
  280. const char* MP4NormalizeTrackType (const char* type,
  281. uint32_t verbosity)
  282. {
  283. if (!strcasecmp(type, "vide")
  284. || !strcasecmp(type, "video")
  285. || !strcasecmp(type, "mp4v")
  286. || !strcasecmp(type, "avc1")
  287. || !strcasecmp(type, "s263") // 3GPP H.263
  288. || !strcasecmp(type, "encv")) {
  289. return MP4_VIDEO_TRACK_TYPE;
  290. }
  291. if (!strcasecmp(type, "soun")
  292. || !strcasecmp(type, "sound")
  293. || !strcasecmp(type, "audio")
  294. || !strcasecmp(type, "enca")
  295. || !strcasecmp(type, "samr") // 3GPP AMR
  296. || !strcasecmp(type, "sawb") // 3GPP AMR/WB
  297. || !strcasecmp(type, "mp4a")) {
  298. return MP4_AUDIO_TRACK_TYPE;
  299. }
  300. if (!strcasecmp(type, "sdsm")
  301. || !strcasecmp(type, "scene")
  302. || !strcasecmp(type, "bifs")) {
  303. return MP4_SCENE_TRACK_TYPE;
  304. }
  305. if (!strcasecmp(type, "odsm")
  306. || !strcasecmp(type, "od")) {
  307. return MP4_OD_TRACK_TYPE;
  308. }
  309. if (strcasecmp(type, "cntl") == 0) {
  310. return MP4_CNTL_TRACK_TYPE;
  311. }
  312. VERBOSE_WARNING(verbosity,
  313. printf("Attempt to normalize %s did not match\n",
  314. type));
  315. return type;
  316. }