annotate Input/aac/libmp4v2/atom_root.cpp @ 2:6efb9e514224 trunk

[svn] Import AAC stuff.
author nenolod
date Mon, 24 Oct 2005 10:44:27 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
1 /*
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
2 * The contents of this file are subject to the Mozilla Public
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
3 * License Version 1.1 (the "License"); you may not use this file
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
4 * except in compliance with the License. You may obtain a copy of
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
5 * the License at http://www.mozilla.org/MPL/
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
6 *
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
7 * Software distributed under the License is distributed on an "AS
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
9 * implied. See the License for the specific language governing
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
10 * rights and limitations under the License.
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
11 *
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
12 * The Original Code is MPEG4IP.
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
13 *
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
14 * The Initial Developer of the Original Code is Cisco Systems Inc.
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
15 * Portions created by Cisco Systems Inc. are
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
16 * Copyright (C) Cisco Systems Inc. 2001. All Rights Reserved.
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
17 *
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
18 * Contributor(s):
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
19 * Dave Mackie dmackie@cisco.com
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
20 */
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
21
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
22 #include "mp4common.h"
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
23
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
24 MP4RootAtom::MP4RootAtom()
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
25 : MP4Atom(NULL)
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
26 {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
27 ExpectChildAtom("ftyp", Required, OnlyOne);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
28 ExpectChildAtom("moov", Required, OnlyOne);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
29 ExpectChildAtom("mdat", Optional, Many);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
30 ExpectChildAtom("free", Optional, Many);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
31 ExpectChildAtom("skip", Optional, Many);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
32 ExpectChildAtom("udta", Optional, Many);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
33 ExpectChildAtom("moof", Optional, Many);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
34 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
35
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
36 void MP4RootAtom::BeginWrite(bool use64)
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
37 {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
38 // only call under MP4Create() control
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
39 WriteAtomType("ftyp", OnlyOne);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
40
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
41 m_pChildAtoms[GetLastMdatIndex()]->BeginWrite(m_pFile->Use64Bits());
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
42 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
43
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
44 void MP4RootAtom::Write()
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
45 {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
46 // no-op
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
47 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
48
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
49 void MP4RootAtom::FinishWrite(bool use64)
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
50 {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
51 // finish writing last mdat atom
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
52 u_int32_t mdatIndex = GetLastMdatIndex();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
53 m_pChildAtoms[mdatIndex]->FinishWrite(m_pFile->Use64Bits());
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
54
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
55 // write all atoms after last mdat
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
56 u_int32_t size = m_pChildAtoms.Size();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
57 for (u_int32_t i = mdatIndex + 1; i < size; i++) {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
58 m_pChildAtoms[i]->Write();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
59 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
60 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
61
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
62 void MP4RootAtom::BeginOptimalWrite()
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
63 {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
64 WriteAtomType("ftyp", OnlyOne);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
65 WriteAtomType("moov", OnlyOne);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
66 WriteAtomType("udta", Many);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
67
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
68 m_pChildAtoms[GetLastMdatIndex()]->BeginWrite(m_pFile->Use64Bits());
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
69 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
70
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
71 void MP4RootAtom::FinishOptimalWrite()
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
72 {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
73 // finish writing mdat
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
74 m_pChildAtoms[GetLastMdatIndex()]->FinishWrite(m_pFile->Use64Bits());
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
75
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
76 // find moov atom
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
77 u_int32_t size = m_pChildAtoms.Size();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
78 MP4Atom* pMoovAtom = NULL;
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
79
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
80 u_int32_t i;
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
81 for (i = 0; i < size; i++) {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
82 if (!strcmp("moov", m_pChildAtoms[i]->GetType())) {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
83 pMoovAtom = m_pChildAtoms[i];
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
84 break;
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
85 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
86 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
87 ASSERT(i < size);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
88
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
89 // rewrite moov so that updated chunkOffsets are written to disk
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
90 m_pFile->SetPosition(pMoovAtom->GetStart());
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
91 u_int64_t oldSize = pMoovAtom->GetSize();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
92
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
93 pMoovAtom->Write();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
94
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
95 // sanity check
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
96 u_int64_t newSize = pMoovAtom->GetSize();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
97 ASSERT(oldSize == newSize);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
98 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
99
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
100 u_int32_t MP4RootAtom::GetLastMdatIndex()
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
101 {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
102 for (int32_t i = m_pChildAtoms.Size() - 1; i >= 0; i--) {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
103 if (!strcmp("mdat", m_pChildAtoms[i]->GetType())) {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
104 return i;
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
105 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
106 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
107 ASSERT(false);
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
108 return (u_int32_t)-1;
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
109 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
110
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
111 void MP4RootAtom::WriteAtomType(const char* type, bool onlyOne)
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
112 {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
113 u_int32_t size = m_pChildAtoms.Size();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
114
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
115 for (u_int32_t i = 0; i < size; i++) {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
116 if (!strcmp(type, m_pChildAtoms[i]->GetType())) {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
117 m_pChildAtoms[i]->Write();
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
118 if (onlyOne) {
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
119 break;
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
120 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
121 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
122 }
6efb9e514224 [svn] Import AAC stuff.
nenolod
parents:
diff changeset
123 }