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 /*
19 This PVA_FF_TrackHeaderAtom Class specifies the characteristics of a single MPEG-4
20 track.
21 */
22
23
24 #define IMPLEMENT_TrackHeaderAtom
25
26 #include "trackheaderatom.h"
27 #include "atomutils.h"
28 #include "a_atomdefs.h"
29
30 #define TIMED_TEXT_WIDTH 176
31 #define TIMED_TEXT_HEIGHT 177
32 // Constructor
PVA_FF_TrackHeaderAtom(int32 type,uint32 trackID,uint8 version,uint32 flags,uint32 fileAuthoringFlags)33 PVA_FF_TrackHeaderAtom::PVA_FF_TrackHeaderAtom(int32 type, uint32 trackID, uint8 version, uint32 flags, uint32 fileAuthoringFlags)
34 : PVA_FF_FullAtom(TRACK_HEADER_ATOM, version, flags)
35 {
36 OSCL_UNUSED_ARG(fileAuthoringFlags);
37
38 _trackID = trackID;
39 _mediaType = type;
40 _width = 176;
41 _height = 144;
42 init(type);
43 }
44
45
46
47 // Destructor
~PVA_FF_TrackHeaderAtom()48 PVA_FF_TrackHeaderAtom::~PVA_FF_TrackHeaderAtom()
49 {
50 // Empty
51 }
52
53 void
init(int32 type)54 PVA_FF_TrackHeaderAtom::init(int32 type)
55 {
56 PVA_FF_AtomUtils::setTime(_creationTime);
57 PVA_FF_AtomUtils::setTime(_modificationTime);
58 setTimeScale(0);
59
60 _duration = 0;
61
62 // Initialize the reserved words based on type
63 _reserved1 = 0;
64 _reserved2[0] = 0;
65 _reserved2[1] = 0;
66 _reserved2[2] = 0;
67 _reserved3 = 0;
68 _reserved4 = 0;
69 _reserved5[0] = 0x00010000;
70 _reserved5[1] = 0;
71 _reserved5[2] = 0;
72 _reserved5[3] = 0;
73 _reserved5[4] = 0x00010000;
74 _reserved5[5] = 0;
75 _reserved5[6] = 0;
76 _reserved5[7] = 0;
77 _reserved5[8] = 0x40000000;
78 _reserved6 = 0;
79 _reserved7 = 0;
80
81 if ((uint32) type == MEDIA_TYPE_AUDIO) // Set flags for audio track
82 {
83 _reserved3 = 0x0100;
84 }
85 else if ((uint32) type == MEDIA_TYPE_VISUAL) // Set flags for visual track
86 {
87 _reserved6 = (_width << 16); ;
88 _reserved7 = (_height << 16);;
89 }
90 else if ((uint32) type == MEDIA_TYPE_TEXT)//added for timed text track height and width
91 {
92 _reserved6 = (TIMED_TEXT_WIDTH << 16);
93 _reserved7 = (TIMED_TEXT_HEIGHT << 16);
94
95 }
96 else
97 {
98 // No other special flags
99 }
100
101 /*
102 * Variables added to ensure that the total track duration includes the duration of the
103 * last sample as well, which in our case fp same as the last but one.
104 */
105 _prevTS = 0;
106 _deltaTS = 0;
107
108 recomputeSize();
109 }
110
setVideoWidthHeight(int16 width,int16 height)111 void PVA_FF_TrackHeaderAtom::setVideoWidthHeight(int16 width, int16 height)
112 {
113 _width = width;
114 _height = height;
115 _reserved6 = (_width << 16); ;
116 _reserved7 = (_height << 16);;
117 }
118
119 void
addSample(uint32 ts)120 PVA_FF_TrackHeaderAtom::addSample(uint32 ts)
121 {
122 // Need to verify that this fp in the movie timescale
123 // Currenly ts for the media samples fp in terms of milliseconds
124 // For rounding
125 uint32 duration = (uint32)((getTimeScale() / 1000.0f) * ts + 0.5f);
126
127 _deltaTS = duration - _prevTS;
128
129 setDuration(duration);
130
131 _prevTS = duration;
132 }
133
134 // in movie fragment mode set the actual duration of
135 // last sample
136 void
updateLastTSEntry(uint32 ts)137 PVA_FF_TrackHeaderAtom::updateLastTSEntry(uint32 ts)
138 {
139 uint32 duration = (uint32)((getTimeScale() / 1000.0f) * ts);
140 setDuration(duration);
141 _deltaTS = 0; // this will not increase the total duration
142 // in movie fragmement mode as done before rendering
143 }
144
145 void
recomputeSize()146 PVA_FF_TrackHeaderAtom::recomputeSize()
147 {
148 int32 size = getDefaultSize(); // From base class
149 size += 4; // _creationTime;
150 size += 4; // _modificationTime
151 size += 4; // _trackID;
152 size += 4; // _reserved1
153 size += 4; // _duration;
154 size += 60; // rest of reserved words
155
156 _size = size;
157
158 // Update the size of the parent atom since this child atom may have changed
159 if (_pparent != NULL)
160 {
161 _pparent->recomputeSize();
162 }
163 }
164
165 // Rendering the PVA_FF_Atom in proper format (bitlengths, etc.) to an ostream.
166 bool
renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP * fp)167 PVA_FF_TrackHeaderAtom::renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP *fp)
168 {
169 int32 rendered = 0; // Keep track of number of bytes rendered
170 uint32 trackID = 0;
171 // Render PVA_FF_Atom type and size
172 if (!renderAtomBaseMembers(fp))
173 {
174 return false;
175 }
176 rendered += getDefaultSize();
177
178 if (!PVA_FF_AtomUtils::render32(fp, getCreationTime()))
179 {
180 return false;
181 }
182 if (!PVA_FF_AtomUtils::render32(fp, getModificationTime()))
183 {
184 return false;
185 }
186
187 trackID = getTrackID();
188
189
190 if (!PVA_FF_AtomUtils::render32(fp, trackID))
191 {
192 return false;
193 }
194 rendered += 12;
195
196 if (!PVA_FF_AtomUtils::render32(fp, _reserved1))
197 {
198 return false;
199 }
200 rendered += 4;
201
202 /*
203 * To ensure that the total track duration includes the duration of the
204 * last sample as well, which in our case is same as the last but one.
205 */
206
207 uint32 totalDuration = getDuration();
208 if (!PVA_FF_AtomUtils::render32(fp, totalDuration))
209 {
210 return false;
211 }
212 rendered += 4;
213
214 if (!PVA_FF_AtomUtils::render32(fp, _reserved2[0]))
215 {
216 return false;
217 }
218 if (!PVA_FF_AtomUtils::render32(fp, _reserved2[1]))
219 {
220 return false;
221 }
222 if (!PVA_FF_AtomUtils::render32(fp, _reserved2[2]))
223 {
224 return false;
225 }
226 if (!PVA_FF_AtomUtils::render16(fp, _reserved3))
227 {
228 return false;
229 }
230 if (!PVA_FF_AtomUtils::render16(fp, _reserved4))
231 {
232 return false;
233 }
234 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[0]))
235 {
236 return false;
237 }
238 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[1]))
239 {
240 return false;
241 }
242 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[2]))
243 {
244 return false;
245 }
246 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[3]))
247 {
248 return false;
249 }
250 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[4]))
251 {
252 return false;
253 }
254 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[5]))
255 {
256 return false;
257 }
258 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[6]))
259 {
260 return false;
261 }
262 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[7]))
263 {
264 return false;
265 }
266 if (!PVA_FF_AtomUtils::render32(fp, _reserved5[8]))
267 {
268 return false;
269 }
270 if (!PVA_FF_AtomUtils::render32(fp, _reserved6))
271 {
272 return false;
273 }
274 if (!PVA_FF_AtomUtils::render32(fp, _reserved7))
275 {
276 return false;
277 }
278 rendered += 60;
279
280 return true;
281 }
282
283