Imported Upstream version 2.0.0
[platform/upstream/libmp4v2.git] / src / atom_mvhd.cpp
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 "src/impl.h"
23
24 namespace mp4v2 {
25 namespace impl {
26
27 ///////////////////////////////////////////////////////////////////////////////
28
29 MP4MvhdAtom::MP4MvhdAtom(MP4File &file)
30         : MP4Atom(file, "mvhd")
31 {
32     AddVersionAndFlags();
33 }
34
35 void MP4MvhdAtom::AddProperties(uint8_t version)
36 {
37     if (version == 1) {
38         AddProperty( /* 2 */
39             new MP4Integer64Property(*this, "creationTime"));
40         AddProperty( /* 3 */
41             new MP4Integer64Property(*this, "modificationTime"));
42     } else {
43         AddProperty( /* 2 */
44             new MP4Integer32Property(*this, "creationTime"));
45         AddProperty( /* 3 */
46             new MP4Integer32Property(*this, "modificationTime"));
47     }
48
49     AddProperty( /* 4 */
50         new MP4Integer32Property(*this, "timeScale"));
51
52     if (version == 1) {
53         AddProperty( /* 5 */
54             new MP4Integer64Property(*this, "duration"));
55     } else {
56         AddProperty( /* 5 */
57             new MP4Integer32Property(*this, "duration"));
58     }
59
60     MP4Float32Property* pProp;
61
62     pProp = new MP4Float32Property(*this, "rate");
63     pProp->SetFixed32Format();
64     AddProperty(pProp); /* 6 */
65
66     pProp = new MP4Float32Property(*this, "volume");
67     pProp->SetFixed16Format();
68     AddProperty(pProp); /* 7 */
69
70     AddReserved(*this, "reserved1", 70); /* 8 */
71
72     AddProperty( /* 9 */
73         new MP4Integer32Property(*this, "nextTrackId"));
74 }
75
76 void MP4MvhdAtom::Generate()
77 {
78     uint8_t version = m_File.Use64Bits(GetType()) ? 1 : 0;
79     SetVersion(version);
80     AddProperties(version);
81
82     MP4Atom::Generate();
83
84     // set creation and modification times
85     MP4Timestamp now = MP4GetAbsTimestamp();
86     if (version == 1) {
87         ((MP4Integer64Property*)m_pProperties[2])->SetValue(now);
88         ((MP4Integer64Property*)m_pProperties[3])->SetValue(now);
89     } else {
90         ((MP4Integer32Property*)m_pProperties[2])->SetValue(now);
91         ((MP4Integer32Property*)m_pProperties[3])->SetValue(now);
92     }
93
94     ((MP4Integer32Property*)m_pProperties[4])->SetValue(1000);
95
96     ((MP4Float32Property*)m_pProperties[6])->SetValue(1.0);
97     ((MP4Float32Property*)m_pProperties[7])->SetValue(1.0);
98
99     // property reserved has non-zero fixed values
100     static uint8_t reserved[70] = {
101         0x00, 0x00,
102         0x00, 0x00, 0x00, 0x00,
103         0x00, 0x00, 0x00, 0x00,
104         0x00, 0x01, 0x00, 0x00,
105         0x00, 0x00, 0x00, 0x00,
106         0x00, 0x00, 0x00, 0x00,
107         0x00, 0x00, 0x00, 0x00,
108         0x00, 0x01, 0x00, 0x00,
109         0x00, 0x00, 0x00, 0x00,
110         0x00, 0x00, 0x00, 0x00,
111         0x00, 0x00, 0x00, 0x00,
112         0x40, 0x00, 0x00, 0x00,
113         0x00, 0x00, 0x00, 0x00,
114         0x00, 0x00, 0x00, 0x00,
115         0x00, 0x00, 0x00, 0x00,
116         0x00, 0x00, 0x00, 0x00,
117         0x00, 0x00, 0x00, 0x00,
118         0x00, 0x00, 0x00, 0x00,
119     };
120     m_pProperties[8]->SetReadOnly(false);
121     ((MP4BytesProperty*)m_pProperties[8])->
122     SetValue(reserved, sizeof(reserved));
123     m_pProperties[8]->SetReadOnly(true);
124
125     // set next track id
126     ((MP4Integer32Property*)m_pProperties[9])->SetValue(1);
127 }
128
129 void MP4MvhdAtom::Read()
130 {
131     /* read atom version */
132     ReadProperties(0, 1);
133
134     /* need to create the properties based on the atom version */
135     AddProperties(GetVersion());
136
137     /* now we can read the remaining properties */
138     ReadProperties(1);
139
140     Skip(); // to end of atom
141 }
142
143 ///////////////////////////////////////////////////////////////////////////////
144
145 }
146 } // namespace mp4v2::impl