annotate Input/aac/libmp4v2/mp4util.cpp @ 38:4e0397865a31 trunk

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