1 /*****************************************************************
2 |
3 | AP4 - Sample Table Interface
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 "Ap4SampleTable.h"
33 #include "Ap4ContainerAtom.h"
34 #include "Ap4StsdAtom.h"
35 #include "Ap4StszAtom.h"
36 #include "Ap4StscAtom.h"
37 #include "Ap4StcoAtom.h"
38 #include "Ap4Co64Atom.h"
39 #include "Ap4SttsAtom.h"
40 #include "Ap4StssAtom.h"
41 #include "Ap4CttsAtom.h"
42 #include "Ap4Sample.h"
43
44 /*----------------------------------------------------------------------
45 | AP4_SampleTable Dynamic Cast Anchor
46 +---------------------------------------------------------------------*/
AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_SampleTable)47 AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_SampleTable)
48
49 /*----------------------------------------------------------------------
50 | AP4_SampleTable::GenerateStblAtom
51 +---------------------------------------------------------------------*/
52 AP4_Result
53 AP4_SampleTable::GenerateStblAtom(AP4_ContainerAtom*& stbl)
54 {
55 // create the stbl container
56 stbl = new AP4_ContainerAtom(AP4_ATOM_TYPE_STBL);
57
58 // create the stsd atom
59 AP4_StsdAtom* stsd = new AP4_StsdAtom(this);
60
61 // create the stts atom
62 AP4_SttsAtom* stts = new AP4_SttsAtom();
63
64 // create the stsc atom
65 AP4_StscAtom* stsc = new AP4_StscAtom();
66
67 // create the stsz atom
68 AP4_StszAtom* stsz = new AP4_StszAtom();
69
70 // create the stss atom
71 AP4_StssAtom* stss = new AP4_StssAtom();
72
73 // declare the ctts atom (may be created later)
74 AP4_CttsAtom* ctts = NULL;
75
76 // start chunk table
77 AP4_Ordinal current_chunk_index = 0;
78 AP4_Size current_chunk_size = 0;
79 AP4_Position current_chunk_offset = 0;
80 AP4_Cardinal current_samples_in_chunk = 0;
81 AP4_Ordinal current_sample_description_index = 0;
82 AP4_UI32 current_duration = 0;
83 AP4_Cardinal current_duration_run = 0;
84 AP4_UI32 current_cts_delta = 0;
85 AP4_Cardinal current_cts_delta_run = 0;
86 AP4_Array<AP4_Position> chunk_offsets;
87
88 // process all the samples
89 bool all_samples_are_sync = false;
90 AP4_Cardinal sample_count = GetSampleCount();
91 for (AP4_Ordinal i=0; i<sample_count; i++) {
92 AP4_Sample sample;
93 GetSample(i, sample);
94
95 // update DTS table
96 AP4_UI32 new_duration = sample.GetDuration();
97 if (new_duration != current_duration && current_duration_run != 0) {
98 // emit a new stts entry
99 stts->AddEntry(current_duration_run, current_duration);
100
101 // reset the run count
102 current_duration_run = 0;
103 }
104 ++current_duration_run;
105 current_duration = new_duration;
106
107 // update CTS table
108 AP4_UI32 new_cts_delta = sample.GetCtsDelta();
109 if (new_cts_delta != current_cts_delta && current_cts_delta_run != 0) {
110 // create a ctts atom if we don't have one
111 if (ctts == NULL) ctts = new AP4_CttsAtom();
112
113 //emit a new ctts entry
114 ctts->AddEntry(current_cts_delta_run, current_cts_delta);
115
116 // reset the run count
117 current_cts_delta_run = 0;
118 }
119 ++current_cts_delta_run;
120 current_cts_delta = new_cts_delta;
121
122 // add an entry into the stsz atom
123 stsz->AddEntry(sample.GetSize());
124
125 // update the sync sample table
126 if (sample.IsSync()) {
127 stss->AddEntry(i+1);
128 if (i==0) all_samples_are_sync = true;
129 } else {
130 all_samples_are_sync = false;
131 }
132
133 // see in which chunk this sample is
134 AP4_Ordinal chunk_index = 0;
135 AP4_Ordinal position_in_chunk = 0;
136 AP4_Result result = GetSampleChunkPosition(i, chunk_index, position_in_chunk);
137 if (AP4_SUCCEEDED(result)) {
138 if (chunk_index != current_chunk_index && current_samples_in_chunk != 0) {
139 // new chunk
140 chunk_offsets.Append(current_chunk_offset);
141 current_chunk_offset += current_chunk_size;
142
143 stsc->AddEntry(1,
144 current_samples_in_chunk,
145 current_sample_description_index+1);
146
147 current_samples_in_chunk = 0;
148 current_chunk_size = 0;
149 }
150 current_chunk_index = chunk_index;
151 }
152
153 // store the sample description index
154 current_sample_description_index = sample.GetDescriptionIndex();
155
156 // adjust the current chunk info
157 current_chunk_size += sample.GetSize();
158 ++current_samples_in_chunk;
159 }
160
161 // finish the stts table
162 if (sample_count) stts->AddEntry(current_duration_run, current_duration);
163
164 // finish the ctts table if we have one
165 if (ctts) {
166 AP4_ASSERT(current_cts_delta_run != 0);
167
168 // add a ctts entry
169 ctts->AddEntry(current_cts_delta_run, current_cts_delta);
170 }
171
172 // process any unfinished chunk
173 if (current_samples_in_chunk != 0) {
174 // new chunk
175 chunk_offsets.Append(current_chunk_offset);
176 stsc->AddEntry(1,
177 current_samples_in_chunk,
178 current_sample_description_index+1);
179 }
180
181 // attach the children of stbl
182 stbl->AddChild(stsd);
183 stbl->AddChild(stts);
184 if (ctts) stbl->AddChild(ctts);
185 stbl->AddChild(stsc);
186 stbl->AddChild(stsz);
187 if (!all_samples_are_sync && stss->GetEntries().ItemCount() != 0) {
188 stbl->AddChild(stss);
189 } else {
190 delete stss;
191 }
192
193 // see if we need a co64 or an stco atom
194 AP4_Size chunk_count = chunk_offsets.ItemCount();
195 if (current_chunk_offset <= 0xFFFFFFFF) {
196 // make an array of 32-bit entries
197 AP4_UI32* chunk_offsets_32 = new AP4_UI32[chunk_count];
198 for (unsigned int i=0; i<chunk_count; i++) {
199 chunk_offsets_32[i] = (AP4_UI32)chunk_offsets[i];
200 }
201 // create the stco atom
202 AP4_StcoAtom* stco = new AP4_StcoAtom(&chunk_offsets_32[0], chunk_count);
203 stbl->AddChild(stco);
204
205 delete[] chunk_offsets_32;
206 } else {
207 // create the co64 atom
208 AP4_Co64Atom* co64 = new AP4_Co64Atom(&chunk_offsets[0], chunk_count);
209 stbl->AddChild(co64);
210 }
211
212
213 return AP4_SUCCESS;
214 }
215