1 /*****************************************************************
2 |
3 |    AP4 - avcC Atoms
4 |
5 |    Copyright 2002-2008 Axiomatic Systems, LLC
6 |
7 |
8 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 |    Unless you have obtained Bento4 under a difference license,
11 |    this version of Bento4 is Bento4|GPL.
12 |    Bento4|GPL is free software; you can redistribute it and/or modify
13 |    it under the terms of the GNU General Public License as published by
14 |    the Free Software Foundation; either version 2, or (at your option)
15 |    any later version.
16 |
17 |    Bento4|GPL is distributed in the hope that it will be useful,
18 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
19 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 |    GNU General Public License for more details.
21 |
22 |    You should have received a copy of the GNU General Public License
23 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
24 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 |    02111-1307, USA.
26 |
27 ****************************************************************/
28 
29 /*----------------------------------------------------------------------
30 |   includes
31 +---------------------------------------------------------------------*/
32 #include "Ap4AvccAtom.h"
33 #include "Ap4AtomFactory.h"
34 #include "Ap4Utils.h"
35 #include "Ap4Types.h"
36 
37 /*----------------------------------------------------------------------
38 |   dynamic cast support
39 +---------------------------------------------------------------------*/
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_AvccAtom) const40 AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_AvccAtom)
41 
42 /*----------------------------------------------------------------------
43 |   AP4_AvccAtom::GetProfileName
44 +---------------------------------------------------------------------*/
45 const char*
46 AP4_AvccAtom::GetProfileName(AP4_UI08 profile)
47 {
48     switch (profile) {
49         case AP4_AVC_PROFILE_BASELINE: return "Baseline";
50         case AP4_AVC_PROFILE_MAIN:     return "Main";
51         case AP4_AVC_PROFILE_EXTENDED: return "Extended";
52         case AP4_AVC_PROFILE_HIGH:     return "High";
53         case AP4_AVC_PROFILE_HIGH_10:  return "High 10";
54         case AP4_AVC_PROFILE_HIGH_422: return "High 4:2:2";
55         case AP4_AVC_PROFILE_HIGH_444: return "High 4:4:4";
56     }
57 
58     return NULL;
59 }
60 
61 /*----------------------------------------------------------------------
62 |   AP4_AvccAtom::Create
63 +---------------------------------------------------------------------*/
64 AP4_AvccAtom*
Create(AP4_Size size,AP4_ByteStream & stream)65 AP4_AvccAtom::Create(AP4_Size size, AP4_ByteStream& stream)
66 {
67     // read the raw bytes in a buffer
68     unsigned int payload_size = size-AP4_ATOM_HEADER_SIZE;
69     AP4_DataBuffer payload_data(payload_size);
70     AP4_Result result = stream.Read(payload_data.UseData(), payload_size);
71     if (AP4_FAILED(result)) return NULL;
72 
73     // check the version
74     const AP4_UI08* payload = payload_data.GetData();
75     if (payload[0] != 1) {
76         return NULL;
77     }
78 
79     // check the size
80     if (payload_size < 6) return NULL;
81     unsigned int num_seq_params = payload[5]&31;
82     unsigned int cursor = 6;
83     for (unsigned int i=0; i<num_seq_params; i++) {
84         if (cursor+2 > payload_size) return NULL;
85         cursor += 2+AP4_BytesToInt16BE(&payload[cursor]);
86         if (cursor > payload_size) return NULL;
87     }
88     unsigned int num_pic_params = payload[cursor++];
89     if (cursor > payload_size) return NULL;
90     for (unsigned int i=0; i<num_pic_params; i++) {
91         if (cursor+2 > payload_size) return NULL;
92         cursor += 2+AP4_BytesToInt16BE(&payload[cursor]);
93         if (cursor > payload_size) return NULL;
94     }
95     return new AP4_AvccAtom(size, payload);
96 }
97 
98 /*----------------------------------------------------------------------
99 |   AP4_AvccAtom::AP4_AvccAtom
100 +---------------------------------------------------------------------*/
AP4_AvccAtom()101 AP4_AvccAtom::AP4_AvccAtom() :
102     AP4_Atom(AP4_ATOM_TYPE_AVCC, AP4_ATOM_HEADER_SIZE),
103     m_ConfigurationVersion(1),
104     m_Profile(0),
105     m_Level(0),
106     m_ProfileCompatibility(0),
107     m_NaluLengthSize(0)
108 {
109     UpdateRawBytes();
110     m_Size32 += m_RawBytes.GetDataSize();
111 }
112 
113 /*----------------------------------------------------------------------
114 |   AP4_AvccAtom::AP4_AvccAtom
115 +---------------------------------------------------------------------*/
AP4_AvccAtom(const AP4_AvccAtom & other)116 AP4_AvccAtom::AP4_AvccAtom(const AP4_AvccAtom& other) :
117     AP4_Atom(AP4_ATOM_TYPE_AVCC, other.m_Size32),
118     m_ConfigurationVersion(other.m_ConfigurationVersion),
119     m_Profile(other.m_Profile),
120     m_Level(other.m_Level),
121     m_ProfileCompatibility(other.m_ProfileCompatibility),
122     m_NaluLengthSize(other.m_NaluLengthSize),
123     m_RawBytes(other.m_RawBytes)
124 {
125     // deep copy of the parameters
126     unsigned int i = 0;
127     for (i=0; i<other.m_SequenceParameters.ItemCount(); i++) {
128         m_SequenceParameters.Append(other.m_SequenceParameters[i]);
129     }
130     for (i=0; i<other.m_PictureParameters.ItemCount(); i++) {
131         m_PictureParameters.Append(other.m_PictureParameters[i]);
132     }
133 }
134 
135 /*----------------------------------------------------------------------
136 |   AP4_AvccAtom::AP4_AvccAtom
137 +---------------------------------------------------------------------*/
AP4_AvccAtom(AP4_UI32 size,const AP4_UI08 * payload)138 AP4_AvccAtom::AP4_AvccAtom(AP4_UI32 size, const AP4_UI08* payload) :
139     AP4_Atom(AP4_ATOM_TYPE_AVCC, size)
140 {
141     // make a copy of our configuration bytes
142     unsigned int payload_size = size-AP4_ATOM_HEADER_SIZE;
143     m_RawBytes.SetData(payload, payload_size);
144 
145     // parse the payload
146     m_ConfigurationVersion = payload[0];
147     m_Profile              = payload[1];
148     m_ProfileCompatibility = payload[2];
149     m_Level                = payload[3];
150     m_NaluLengthSize       = 1+(payload[4]&3);
151     AP4_UI08 num_seq_params = payload[5]&31;
152     m_SequenceParameters.EnsureCapacity(num_seq_params);
153     unsigned int cursor = 6;
154     for (unsigned int i=0; i<num_seq_params; i++) {
155         m_SequenceParameters.Append(AP4_DataBuffer());
156         AP4_UI16 param_length = AP4_BytesToInt16BE(&payload[cursor]);
157         m_SequenceParameters[i].SetData(&payload[cursor]+2, param_length);
158         cursor += 2+param_length;
159     }
160     AP4_UI08 num_pic_params = payload[cursor++];
161     m_PictureParameters.EnsureCapacity(num_pic_params);
162     for (unsigned int i=0; i<num_pic_params; i++) {
163         m_PictureParameters.Append(AP4_DataBuffer());
164         AP4_UI16 param_length = AP4_BytesToInt16BE(&payload[cursor]);
165         m_PictureParameters[i].SetData(&payload[cursor]+2, param_length);
166         cursor += 2+param_length;
167     }
168 }
169 
170 
171 /*----------------------------------------------------------------------
172 |   AP4_AvccAtom::AP4_AvccAtom
173 +---------------------------------------------------------------------*/
AP4_AvccAtom(AP4_UI08 profile,AP4_UI08 level,AP4_UI08 profile_compatibility,AP4_UI08 length_size,const AP4_Array<AP4_DataBuffer> & sequence_parameters,const AP4_Array<AP4_DataBuffer> & picture_parameters)174 AP4_AvccAtom::AP4_AvccAtom(AP4_UI08                         profile,
175                            AP4_UI08                         level,
176                            AP4_UI08                         profile_compatibility,
177                            AP4_UI08                         length_size,
178                            const AP4_Array<AP4_DataBuffer>& sequence_parameters,
179                            const AP4_Array<AP4_DataBuffer>& picture_parameters) :
180     AP4_Atom(AP4_ATOM_TYPE_AVCC, AP4_ATOM_HEADER_SIZE),
181     m_ConfigurationVersion(1),
182     m_Profile(profile),
183     m_Level(level),
184     m_ProfileCompatibility(profile_compatibility),
185     m_NaluLengthSize(length_size)
186 {
187     // deep copy of the parameters
188     unsigned int i = 0;
189     for (i=0; i<sequence_parameters.ItemCount(); i++) {
190         m_SequenceParameters.Append(sequence_parameters[i]);
191     }
192     for (i=0; i<picture_parameters.ItemCount(); i++) {
193         m_PictureParameters.Append(picture_parameters[i]);
194     }
195 
196     // compute the raw bytes
197     UpdateRawBytes();
198 
199     // update the size
200     m_Size32 += m_RawBytes.GetDataSize();
201 }
202 
203 /*----------------------------------------------------------------------
204 |   AP4_AvccAtom::UpdateRawBytes
205 +---------------------------------------------------------------------*/
206 void
UpdateRawBytes()207 AP4_AvccAtom::UpdateRawBytes()
208 {
209     // compute the payload size
210     unsigned int payload_size = 6;
211     for (unsigned int i=0; i<m_SequenceParameters.ItemCount(); i++) {
212         payload_size += 2+m_SequenceParameters[i].GetDataSize();
213     }
214     ++payload_size;
215     for (unsigned int i=0; i<m_PictureParameters.ItemCount(); i++) {
216         payload_size += 2+m_PictureParameters[i].GetDataSize();
217     }
218     m_RawBytes.SetDataSize(payload_size);
219     AP4_UI08* payload = m_RawBytes.UseData();
220 
221     payload[0] = m_ConfigurationVersion;
222     payload[1] = m_Profile;
223     payload[2] = m_ProfileCompatibility;
224     payload[3] = m_Level;
225     payload[4] = 0xFC | (m_NaluLengthSize-1);
226     payload[5] = 0xE0 | (AP4_UI08)m_SequenceParameters.ItemCount();
227     unsigned int cursor = 6;
228     for (unsigned int i=0; i<m_SequenceParameters.ItemCount(); i++) {
229         AP4_UI16 param_length = (AP4_UI16)m_SequenceParameters[i].GetDataSize();
230         AP4_BytesFromUInt16BE(&payload[cursor], param_length);
231         cursor += 2;
232         AP4_CopyMemory(&payload[cursor], m_SequenceParameters[i].GetData(), param_length);
233         cursor += param_length;
234     }
235     payload[cursor++] = (AP4_UI08)m_PictureParameters.ItemCount();
236     for (unsigned int i=0; i<m_PictureParameters.ItemCount(); i++) {
237         AP4_UI16 param_length = (AP4_UI16)m_PictureParameters[i].GetDataSize();
238         AP4_BytesFromUInt16BE(&payload[cursor], param_length);
239         cursor += 2;
240         AP4_CopyMemory(&payload[cursor], m_PictureParameters[i].GetData(), param_length);
241         cursor += param_length;
242     }
243 }
244 
245 /*----------------------------------------------------------------------
246 |   AP4_AvccAtom::WriteFields
247 +---------------------------------------------------------------------*/
248 AP4_Result
WriteFields(AP4_ByteStream & stream)249 AP4_AvccAtom::WriteFields(AP4_ByteStream& stream)
250 {
251     return stream.Write(m_RawBytes.GetData(), m_RawBytes.GetDataSize());
252 }
253 
254 /*----------------------------------------------------------------------
255 |   AP4_AvccAtom::InspectFields
256 +---------------------------------------------------------------------*/
257 AP4_Result
InspectFields(AP4_AtomInspector & inspector)258 AP4_AvccAtom::InspectFields(AP4_AtomInspector& inspector)
259 {
260     inspector.AddField("Configuration Version", m_ConfigurationVersion);
261     const char* profile_name = GetProfileName(m_Profile);
262     if (profile_name) {
263         inspector.AddField("Profile", profile_name);
264     } else {
265         inspector.AddField("Profile", m_Profile);
266     }
267     inspector.AddField("Profile Compatibility", m_ProfileCompatibility, AP4_AtomInspector::HINT_HEX);
268     inspector.AddField("Level", m_Level);
269     inspector.AddField("NALU Length Size", m_NaluLengthSize);
270     for (unsigned int i=0; i<m_SequenceParameters.ItemCount(); i++) {
271         inspector.AddField("Sequence Parameter", m_SequenceParameters[i].GetData(), m_SequenceParameters[i].GetDataSize());
272     }
273     for (unsigned int i=0; i<m_SequenceParameters.ItemCount(); i++) {
274         inspector.AddField("Picture Parameter", m_PictureParameters[i].GetData(), m_PictureParameters[i].GetDataSize());
275     }
276     return AP4_SUCCESS;
277 }
278