• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*     -------------------------------------------------------------------       */
20 /*                         MPEG-4 MovieHeaderAtom Class                          */
21 /*     -------------------------------------------------------------------       */
22 /*********************************************************************************/
23 /*
24     This MovieHeaderAtom Class defines the overall media-independent information
25     relevant to the MPEG-4 presentation as a whole.
26 */
27 
28 
29 #define IMPLEMENT_MovieHeaderAtom
30 
31 #ifndef OSCL_SNPRINTF_H_INCLUDED
32 #include "oscl_snprintf.h"
33 #endif
34 #ifndef OSCL_UTF8CONV_H
35 #include "oscl_utf8conv.h"
36 #endif
37 #include "movieheaderatom.h"
38 #include "atomutils.h"
39 #include "atomdefs.h"
40 
41 #define ADJUST_DAY_COUNT_USER_PERSPECTIVE_FOR_FIRST_DAY_OF_YEAR 1
42 // Stream-in Constructor
MovieHeaderAtom(MP4_FF_FILE * fp,uint32 size,uint32 type)43 MovieHeaderAtom::MovieHeaderAtom(MP4_FF_FILE *fp, uint32 size, uint32 type)
44         : FullAtom(fp, size, type)
45 {
46     if (_success)
47     {
48 
49         _pparent = NULL;
50 
51         if (getVersion() == 1)
52         {
53             if (!AtomUtils::read64(fp, _creationTime64))
54             {
55                 _success = false;
56             }
57             if (!AtomUtils::read64(fp, _modificationTime64))
58             {
59                 _success = false;
60             }
61             if (!AtomUtils::read32(fp, _timeScale))
62             {
63                 _success = false;
64             }
65             if (!AtomUtils::read64(fp, _duration64))
66             {
67                 _success = false;
68             }
69         }
70         else
71         {
72             if (!AtomUtils::read32(fp, _creationTime))
73             {
74                 _success = false;
75             }
76             if (!AtomUtils::read32(fp, _modificationTime))
77             {
78                 _success = false;
79             }
80             if (!AtomUtils::read32(fp, _timeScale))
81             {
82                 _success = false;
83             }
84             if (!AtomUtils::read32(fp, _duration))
85             {
86                 _success = false;
87             }
88 
89         }
90 
91         // Read all defaulted reserved members - don't care about values
92         uint32 junk;
93         for (int32 i = 0; i < 19; i++)
94         {
95             if (!AtomUtils::read32(fp, junk))
96             {
97                 _success = false;
98             }
99         }
100 
101         if (!AtomUtils::read32(fp, _nextTrackID))
102             _success = false;
103 
104         if (!_success)
105             _mp4ErrorCode = READ_MOVIE_HEADER_ATOM_FAILED;
106     }
107     else
108     {
109         if (_mp4ErrorCode != ATOM_VERSION_NOT_SUPPORTED)
110             _mp4ErrorCode = READ_MOVIE_HEADER_ATOM_FAILED;
111     }
112 
113 }
114 
115 // Destructor
~MovieHeaderAtom()116 MovieHeaderAtom::~MovieHeaderAtom()
117 {
118     // Empty
119 }
120 
convertTimeToDate(uint32 time)121 OSCL_wHeapString<OsclMemAllocator> MovieHeaderAtom::convertTimeToDate(uint32 time)
122 {
123     OSCL_HeapString<OsclMemAllocator> date;
124 
125     char buf[64];
126 
127     int32 numDaysInMonth[12] =
128         {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
129 
130     int32 numDaysInMonthLeap[12] =
131         {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
132 
133     int32 refYear = 1904;
134 
135     // (365*4 + 1) * 24 * 3600
136     int32 numSecsInABlkofFourYears = 126230400;
137 
138     // 365*24*3600
139     int32 numSecInYear = 31536000;
140 
141     int32 numBlksOfFour = (time / numSecsInABlkofFourYears);
142 
143     int32 leftOverSecs = (time - (numBlksOfFour * numSecsInABlkofFourYears));
144 
145     int32 leftOverYears = 0;
146     int32 year    = 0;
147     int32 numDays = 0;
148     int32 numHrs  = 0;
149     int32 numMins = 0;
150     int32 month   = 0;
151     int32 milliSecs = 0;
152 
153     if (leftOverSecs > numSecInYear)
154     {
155         /*To take in to consideration at least 1 year among the number of years
156          as leap among leftOverYear*/
157 
158         leftOverSecs -= (24 * 3600);
159 
160         leftOverYears = (leftOverSecs / numSecInYear);
161 
162         leftOverSecs -= (leftOverYears * numSecInYear);
163 
164 
165         numDays = (uint16)(leftOverSecs / (24 * 3600));
166 
167         leftOverSecs -= (numDays * 24 * 3600);
168 
169 
170         for (uint32 i = 0; i < 12; i++)
171         {
172             if (numDays >= numDaysInMonth[i])
173             {
174                 numDays = (numDays - numDaysInMonth[i]);
175             }
176             else
177             {
178                 month = (i + 1);
179                 break;
180             }
181         }
182 
183 
184     }
185     else
186     {
187         numDays = (leftOverSecs / (24 * 3600));
188 
189         leftOverSecs -= (numDays * 24 * 3600);
190 
191         for (int32 i = 0; i < 12; i++)
192         {
193             if (numDays >= numDaysInMonthLeap[i])
194             {
195                 numDays = (numDays - numDaysInMonthLeap[i]);
196             }
197             else
198             {
199                 month = (i + 1);
200                 break;
201             }
202         }
203 
204     }
205 
206     // Considering a situation where by the date is 1st Jan of any year
207     // the leftOverSecs would be less than the sec available in a day resulting numDays
208     // as 0, however that is 1st Jan from user perspective. So a day adjustment factor
209     // is added into numDays.
210 
211 
212     numDays += ADJUST_DAY_COUNT_USER_PERSPECTIVE_FOR_FIRST_DAY_OF_YEAR;
213 
214 
215 
216     numHrs = (leftOverSecs / 3600);
217 
218     leftOverSecs -= (numHrs * 3600);
219 
220     numMins = (leftOverSecs / 60);
221 
222     leftOverSecs -= (numMins * 60);
223 
224     year = (refYear + (numBlksOfFour * 4) + leftOverYears);
225 
226     oscl_snprintf(buf,
227                   256,
228                   "%04d%02d%02dT%02d%02d%02d.%03dZ",
229                   year, month, numDays, numHrs,
230                   numMins, leftOverSecs, milliSecs);
231 
232     date += buf;
233 
234     /* convert to unicode */
235     uint32 wDateBufLen = 64 * sizeof(oscl_wchar);
236     oscl_wchar wDate[64*sizeof(oscl_wchar)];
237     oscl_memset((OsclAny*)wDate, 0, wDateBufLen);
238     oscl_UTF8ToUnicode(date.get_cstr(),
239                        (int32)(date.get_size()),
240                        wDate,
241                        (int32)(wDateBufLen));
242 
243     OSCL_wHeapString<OsclMemAllocator> wDateStr;
244     wDateStr += wDate;
245 
246     return (wDateStr);
247 }
248