Mercurial > audlegacy
annotate Plugins/Input/aac/libmp4v2/mp4descriptor.cpp @ 199:0a2ad94e8607 trunk
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
author | chainsaw |
---|---|
date | Wed, 16 Nov 2005 16:21:11 -0800 |
parents | fa848bd484d8 |
children | f2dc045d2327 |
rev | line source |
---|---|
61 | 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 #include "mp4common.h" | |
23 | |
24 MP4Descriptor::MP4Descriptor(u_int8_t tag) { | |
25 m_tag = tag; | |
26 m_pParentAtom = NULL; | |
27 m_start = 0; | |
28 m_size = 0; | |
29 m_readMutatePoint = 0; | |
30 } | |
31 | |
32 MP4Descriptor::~MP4Descriptor() | |
33 { | |
34 for (u_int32_t i = 0; i < m_pProperties.Size(); i++) { | |
35 delete m_pProperties[i]; | |
36 } | |
37 } | |
38 | |
39 void MP4Descriptor::AddProperty(MP4Property* pProperty) | |
40 { | |
41 ASSERT(pProperty); | |
42 m_pProperties.Add(pProperty); | |
43 pProperty->SetParentAtom(m_pParentAtom); | |
44 } | |
45 | |
46 bool MP4Descriptor::FindContainedProperty(const char *name, | |
47 MP4Property** ppProperty, u_int32_t* pIndex) | |
48 { | |
49 u_int32_t numProperties = m_pProperties.Size(); | |
50 | |
51 for (u_int32_t i = 0; i < numProperties; i++) { | |
52 if (m_pProperties[i]->FindProperty(name, ppProperty, pIndex)) { | |
53 return true; | |
54 } | |
55 } | |
56 return false; | |
57 } | |
58 | |
59 void MP4Descriptor::Generate() | |
60 { | |
61 // generate properties | |
62 for (u_int32_t i = 0; i < m_pProperties.Size(); i++) { | |
63 m_pProperties[i]->Generate(); | |
64 } | |
65 } | |
66 | |
67 void MP4Descriptor::Read(MP4File* pFile) | |
68 { | |
69 ReadHeader(pFile); | |
70 | |
71 ReadProperties(pFile, 0, m_readMutatePoint); | |
72 | |
73 Mutate(); | |
74 | |
75 ReadProperties(pFile, m_readMutatePoint); | |
76 | |
77 // flush any leftover read bits | |
78 pFile->FlushReadBits(); | |
79 } | |
80 | |
81 void MP4Descriptor::ReadHeader(MP4File* pFile) | |
82 { | |
83 VERBOSE_READ(pFile->GetVerbosity(), | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
84 printf("ReadDescriptor: pos = 0x"X64"\n", |
61 | 85 pFile->GetPosition())); |
86 | |
87 // read tag and length | |
88 u_int8_t tag = pFile->ReadUInt8(); | |
89 if (m_tag) { | |
90 ASSERT(tag == m_tag); | |
91 } else { | |
92 m_tag = tag; | |
93 } | |
94 m_size = pFile->ReadMpegLength(); | |
95 m_start = pFile->GetPosition(); | |
96 | |
97 VERBOSE_READ(pFile->GetVerbosity(), | |
98 printf("ReadDescriptor: tag 0x%02x data size %u (0x%x)\n", | |
99 m_tag, m_size, m_size)); | |
100 } | |
101 | |
102 void MP4Descriptor::ReadProperties(MP4File* pFile, | |
103 u_int32_t propStartIndex, u_int32_t propCount) | |
104 { | |
105 u_int32_t numProperties = MIN(propCount, | |
106 m_pProperties.Size() - propStartIndex); | |
107 | |
108 for (u_int32_t i = propStartIndex; | |
109 i < propStartIndex + numProperties; i++) { | |
110 | |
111 MP4Property* pProperty = m_pProperties[i]; | |
112 | |
113 int32_t remaining = m_size - (pFile->GetPosition() - m_start); | |
114 | |
115 if (pProperty->GetType() == DescriptorProperty) { | |
116 if (remaining > 0) { | |
117 // place a limit on how far this sub-descriptor looks | |
118 ((MP4DescriptorProperty*)pProperty)->SetSizeLimit(remaining); | |
119 pProperty->Read(pFile); | |
120 } // else do nothing, empty descriptor | |
121 } else { | |
122 // non-descriptor property | |
123 if (remaining >= 0) { | |
124 pProperty->Read(pFile); | |
125 | |
126 if (pProperty->GetType() == TableProperty) { | |
127 VERBOSE_READ_TABLE(pFile->GetVerbosity(), | |
128 printf("Read: "); pProperty->Dump(stdout, 0, true)); | |
129 } else { | |
130 VERBOSE_READ(pFile->GetVerbosity(), | |
131 printf("Read: "); pProperty->Dump(stdout, 0, true)); | |
132 } | |
133 } else { | |
134 VERBOSE_ERROR(pFile->GetVerbosity(), | |
135 printf("Overran descriptor, tag %u data size %u property %u\n", | |
136 m_tag, m_size, i)); | |
137 throw new MP4Error("overran descriptor", | |
138 "MP4Descriptor::ReadProperties"); | |
139 } | |
140 } | |
141 } | |
142 } | |
143 | |
144 void MP4Descriptor::Write(MP4File* pFile) | |
145 { | |
146 // call virtual function to adapt properties before writing | |
147 Mutate(); | |
148 | |
149 u_int32_t numProperties = m_pProperties.Size(); | |
150 | |
151 if (numProperties == 0) { | |
152 WARNING(numProperties == 0); | |
153 return; | |
154 } | |
155 | |
156 // write tag and length placeholder | |
157 pFile->WriteUInt8(m_tag); | |
158 u_int64_t lengthPos = pFile->GetPosition(); | |
159 pFile->WriteMpegLength(0); | |
160 u_int64_t startPos = pFile->GetPosition(); | |
161 | |
162 for (u_int32_t i = 0; i < numProperties; i++) { | |
163 m_pProperties[i]->Write(pFile); | |
164 } | |
165 | |
166 // align with byte boundary (rarely necessary) | |
167 pFile->PadWriteBits(); | |
168 | |
169 // go back and write correct length | |
170 u_int64_t endPos = pFile->GetPosition(); | |
171 pFile->SetPosition(lengthPos); | |
172 pFile->WriteMpegLength(endPos - startPos); | |
173 pFile->SetPosition(endPos); | |
174 } | |
175 | |
176 void MP4Descriptor::WriteToMemory(MP4File* pFile, | |
177 u_int8_t** ppBytes, u_int64_t* pNumBytes) | |
178 { | |
179 // use memory buffer to save descriptor in memory | |
180 // instead of going directly to disk | |
181 | |
182 pFile->EnableMemoryBuffer(); | |
183 | |
184 Write(pFile); | |
185 | |
186 pFile->DisableMemoryBuffer(ppBytes, pNumBytes); | |
187 } | |
188 | |
189 void MP4Descriptor::Dump(FILE* pFile, u_int8_t indent, bool dumpImplicits) | |
190 { | |
191 // call virtual function to adapt properties before dumping | |
192 Mutate(); | |
193 | |
194 u_int32_t numProperties = m_pProperties.Size(); | |
195 | |
196 if (numProperties == 0) { | |
197 WARNING(numProperties == 0); | |
198 return; | |
199 } | |
200 for (u_int32_t i = 0; i < numProperties; i++) { | |
201 m_pProperties[i]->Dump(pFile, indent, dumpImplicits); | |
202 } | |
203 } | |
204 | |
205 u_int8_t MP4Descriptor::GetDepth() | |
206 { | |
207 if (m_pParentAtom) { | |
208 return m_pParentAtom->GetDepth(); | |
209 } | |
210 return 0; | |
211 } |