1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 #define IMPLEMENT_TrackFragmentAtom
19
20 #include "trackfragmentatom.h"
21 #include "trackfragmentrunatom.h"
22 typedef Oscl_Vector<uint32, OsclMemAllocator> uint32VecType;
23 typedef Oscl_Vector<uint8, OsclMemAllocator> uint8VecType;
24
25
26 typedef Oscl_Vector <PVA_FF_TrackFragmentRunAtom*, OsclMemAllocator>
27 PVA_FF_TrackFragmentRunAtomVecType;
28
29 // constructor
PVA_FF_TrackFragmentAtom(uint32 mediaType,uint32 codecType,uint32 trackId,uint32 interleaveDuration,uint32 timescale)30 PVA_FF_TrackFragmentAtom::PVA_FF_TrackFragmentAtom(uint32 mediaType,
31 uint32 codecType,
32 uint32 trackId,
33 uint32 interleaveDuration,
34 uint32 timescale)
35 : PVA_FF_Atom(TRACK_FRAGMENT_ATOM)
36 {
37 _mediaType = mediaType;
38 _codecType = codecType;
39
40 // no interleaving flag, as movie fragments are there in interleaving mode
41 _timescale = timescale;
42 _firstEntry = true;
43 _prevTS = 0;
44 _interleaveDuration = interleaveDuration;
45 _fragmentDuration = 0;
46 _ofirstTrun = true;
47
48 // intialise track fragment header atom
49 PV_MP4_FF_NEW(fp->auditCB, PVA_FF_TrackFragmentHeaderAtom, (trackId), _pTfhdAtom);
50
51 // initialise track run vector
52 PV_MP4_FF_NEW(fp->auditCB, PVA_FF_TrackFragmentRunAtomVecType, (), _pTrunList);
53
54 recomputeSize();
55
56 }
57
58
59 //destructor
~PVA_FF_TrackFragmentAtom()60 PVA_FF_TrackFragmentAtom::~PVA_FF_TrackFragmentAtom()
61 {
62 // delete track fragment header
63 PV_MP4_FF_DELETE(NULL, PVA_FF_TrackFragmentHeaderAtom, _pTfhdAtom);
64
65 // Delete trum atom list
66 for (uint32 ii = 0; ii < _pTrunList->size(); ii++)
67 {
68 PV_MP4_FF_DELETE(NULL, PVA_FF_TrackFragmentRunAtom, (*_pTrunList)[ii]);
69 }
70 PV_MP4_FF_TEMPLATED_DELETE(NULL, PVA_FF_TrackFragmentRunAtomVecType,
71 Oscl_Vector, _pTrunList);
72
73 }
74
75
76 // add new TRUN in the list
77 PVA_FF_TrackFragmentRunAtom*
addTrackRun()78 PVA_FF_TrackFragmentAtom::addTrackRun()
79 {
80 PVA_FF_TrackFragmentRunAtom *pTrunAtom;
81
82 PV_MP4_FF_NEW(fp->auditCB, PVA_FF_TrackFragmentRunAtom, (), pTrunAtom);
83
84 _pTrunList->push_back(pTrunAtom);
85 pTrunAtom->setParent(this);
86
87 recomputeSize();
88
89 return pTrunAtom;
90 }
91
92
93 // add new sample into track fragment inturn into TRUN
94 void
addSampleToFragment(uint32 size,uint32 ts,uint8 flags,uint32 baseOffset,bool otrunStart)95 PVA_FF_TrackFragmentAtom::addSampleToFragment(uint32 size, uint32 ts, uint8 flags,
96 uint32 baseOffset, bool otrunStart)
97 {
98
99 if (otrunStart == false)
100 {
101 // add sample to current Trun
102 PVA_FF_TrackFragmentRunAtom* pTrunAtom;
103 pTrunAtom = _pTrunList->back();
104
105 pTrunAtom->addSample(size, ts, flags);
106 }
107 else
108 {
109 // make new trun
110 PVA_FF_TrackFragmentRunAtom* pTrunAtom = addTrackRun();
111
112 if (_ofirstTrun)
113 {
114 _ofirstTrun = false;
115 setTrackFragmentBaseDataOffset(baseOffset);
116 pTrunAtom->setDataOffset(0);
117 }
118 else
119 {
120 uint32 offset = _pTfhdAtom->getBaseDataOffset();
121
122 pTrunAtom->setDataOffset(baseOffset - offset);
123 }
124 pTrunAtom->addSample(size, ts, flags);
125 }
126
127 // update track fragment duration
128 if (_firstEntry)
129 {
130 _firstEntry = false;
131 _prevTS = ts;
132 }
133 else
134 {
135 uint32 delta = ts - _prevTS;
136 _prevTS = ts;
137 _fragmentDuration = _fragmentDuration + (uint32)((1000.0f / _timescale) * delta + 0.5f);
138 }
139
140 }
141
142
143 void
setTrackFragmentHeaderData(uint32 flags)144 PVA_FF_TrackFragmentAtom::setTrackFragmentHeaderData(uint32 flags)
145 {
146
147 // currently default headers are there, this function arguments will be defined later
148 OSCL_UNUSED_ARG(flags);
149 _pTfhdAtom->setHeaderData();
150
151 }
152
153
154 // set base data offset of track fragment
155 void
setTrackFragmentBaseDataOffset(uint64 offset)156 PVA_FF_TrackFragmentAtom::setTrackFragmentBaseDataOffset(uint64 offset)
157 {
158 _pTfhdAtom->setBaseDataOffset(offset);
159 }
160
161
162 // recompute size of atom
163 void
recomputeSize()164 PVA_FF_TrackFragmentAtom::recomputeSize()
165 {
166 int32 size = getDefaultSize();
167
168 // add size of tfhd atom
169 size += _pTfhdAtom->getSize();
170
171 // add size of Track run atoms
172 if (_pTrunList->size() != 0)
173 {
174 for (uint32 ii = 0; ii < _pTrunList->size(); ii++)
175 {
176 size += ((*_pTrunList)[ii])->getSize();
177 }
178 }
179
180 _size = size;
181
182 // Update the parent atom size
183 if (_pparent != NULL)
184 {
185 _pparent->recomputeSize();
186 }
187 }
188
189
190 // get functions
191 uint32
getTrackId()192 PVA_FF_TrackFragmentAtom::getTrackId()
193 {
194
195 return _pTfhdAtom->getTrackId();
196 }
197
198 uint32
getFragmentDuration()199 PVA_FF_TrackFragmentAtom::getFragmentDuration()
200 {
201
202 return _fragmentDuration;
203 }
204
205 uint32
getTrunNumber()206 PVA_FF_TrackFragmentAtom::getTrunNumber()
207 {
208 return _pTrunList->size();
209 }
210
211
212 // updates the duration of last sample in TRUN
213 void
updateLastTSEntry(uint32 ts)214 PVA_FF_TrackFragmentAtom::updateLastTSEntry(uint32 ts)
215 {
216 int32 size = _pTrunList->size();
217
218 if (size > 0)
219 {
220 (*_pTrunList)[size -1]->updateLastTSEntry(ts);
221 }
222 return;
223 }
224
225 // write atom to target file
226 bool
renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP * fp)227 PVA_FF_TrackFragmentAtom::renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP* fp)
228 {
229 uint32 rendered = 0;
230
231 if (!renderAtomBaseMembers(fp))
232 {
233 return false;
234 }
235 rendered += getDefaultSize();
236
237 // Render the movie fragment header atom
238 if (!_pTfhdAtom->renderToFileStream(fp))
239 {
240 return false;
241 }
242 rendered += _pTfhdAtom->getSize();
243
244
245 // render track extend atoms
246 if (_pTrunList->size() != 0)
247 {
248 for (uint32 ii = 0; ii < _pTrunList->size(); ii++)
249 {
250 if (!((*_pTrunList)[ii])->renderToFileStream(fp))
251 {
252 return false;
253 }
254 rendered += ((*_pTrunList)[ii])->getSize();
255 }
256 }
257
258 return true;
259 }
260