• 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  * This PVA_FF_AtomUtils Class contains sime useful methods for operating on Atoms
20  */
21 
22 
23 #define IMPLEMENT_AtomUtils
24 
25 #include "atomutils.h"
26 #include "a_atomdefs.h"
27 #include <time.h>
28 
29 // **** FILE STREAM RENDERING METHODS ****
30 
31 // Render the 64 bits byte by byte and take most significant byte first
32 bool
render64(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint64 data)33 PVA_FF_AtomUtils::render64(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint64 data)
34 {
35     if (fp != NULL)
36     {
37         for (int32 i = 0; i < 64; i += 8)
38         {
39 
40             uint8 byte = (uint8)((data >> (56 - i)) & 0x000000ff);
41             if (fp->_filePtr->Write(&byte, 1, 1) != 1)
42             {
43                 return false;
44             }
45         }
46         return true;
47     }
48     return false;
49 }
50 
51 // Render the 32 bits byte by byte and take most significant byte first
52 bool
render32(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint32 data)53 PVA_FF_AtomUtils::render32(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint32 data)
54 {
55     if (fp != NULL)
56     {
57         for (int32 i = 0; i < 32; i += 8)
58         {
59             uint8 byte = (uint8)((data >> (24 - i)) & 0x000000ff);
60             if (fp->_filePtr->Write(&byte, 1, 1) != 1)
61             {
62                 return false;
63             }
64         }
65         return true;
66     }
67     return false;
68 }
69 
70 // Render the 24 bits byte by byte and take most significant byte first
71 bool
render24(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint32 data)72 PVA_FF_AtomUtils::render24(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint32 data)
73 {
74     if (fp != NULL)
75     {
76         for (int32 i = 0; i < 24; i += 8)
77         {
78             uint8 byte = (uint8)((data >> (16 - i)) & 0x0000ff);
79             if (fp->_filePtr->Write(&byte, 1, 1) != 1)
80             {
81                 return false;
82             }
83         }
84         return true;
85     }
86     return false;
87 }
88 
89 // Render the 16 bits byte by byte and take most significant byte first
90 bool
render16(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint16 data)91 PVA_FF_AtomUtils::render16(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint16 data)
92 {
93     if (fp != NULL)
94     {
95         for (int32 i = 0; i < 16; i += 8)
96         {
97             uint8 byte = (uint8)((data >> (8 - i)) & 0x00ff);
98             if (fp->_filePtr->Write(&byte, 1, 1) != 1)
99             {
100                 return false;
101             }
102         }
103         return true;
104     }
105     return false;
106 }
107 
108 // Render the 8 bits as a single byte
109 bool
render8(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint8 data)110 PVA_FF_AtomUtils::render8(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint8 data)
111 {
112     if (fp != NULL)
113     {
114         if (fp->_filePtr->Write(&data, 1, 1) != 1)
115         {
116             return false;
117         }
118         return true;
119     }
120     return false;
121 }
122 
123 // Render the a string byte by byte
124 bool
renderString(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,PVA_FF_UTF8_STRING_PARAM data)125 PVA_FF_AtomUtils::renderString(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, PVA_FF_UTF8_STRING_PARAM data)
126 {
127     if (fp != NULL)
128     {
129         uint8 byte = 0;
130         for (uint32 i = 0; i < data.get_size(); i++)
131         {
132             byte = data[i];
133             if (fp->_filePtr->Write(&byte, 1, 1) != 1)
134             {
135                 return false;
136             }
137         }
138         return true;
139     }
140     return false;
141 }
142 
143 // Render the a string byte by byte
144 bool
renderUnicodeString(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,PVA_FF_UNICODE_STRING_PARAM data)145 PVA_FF_AtomUtils::renderUnicodeString(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, PVA_FF_UNICODE_STRING_PARAM data)
146 {
147     if (fp != NULL)
148     {
149         OSCL_TCHAR *stringptr = (OSCL_TCHAR *)data.get_cstr();
150 
151         uint16 temp = 0;
152         uint8 lower_byte = 0;
153         uint8 upper_byte = 0;
154         for (uint32 i = 0; i < data.get_size(); i++)
155         {
156             // Watch byte ordering in the stored format
157             temp = (uint16)(stringptr[i] & 0xFF00);
158             temp = (uint8)((temp >> 8) & 0xFF);
159             lower_byte = (uint8)(temp);
160 
161             if (fp->_filePtr->Write(&lower_byte, 1, 1) != 1)
162             {
163                 return false;
164             }
165 
166             upper_byte = (uint8)(stringptr[i] & 0x00FF);
167             if (fp->_filePtr->Write(&upper_byte, 1, 1) != 1)
168             {
169                 return false;
170             }
171         }
172         return true;
173     }
174     return false;
175 }
176 
177 // Render the a string byte by byte PLUS render the trailing \0
178 bool
renderNullTerminatedString(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,PVA_FF_UTF8_STRING_PARAM data)179 PVA_FF_AtomUtils::renderNullTerminatedString(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, PVA_FF_UTF8_STRING_PARAM data)
180 {
181     if (fp != NULL)
182     {
183         PVA_FF_AtomUtils::renderString(fp, data);
184         // put the trailing 0
185         uint8 byte = 0;
186         if (fp->_filePtr->Write(&byte, 1, 1) != 1)
187         {
188             return false;
189         }
190         return true;
191     }
192     return false;
193 }
194 
195 // Render the a string byte by byte PLUS render the trailing \0
196 bool
renderNullTerminatedUnicodeString(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,PVA_FF_UNICODE_STRING_PARAM data)197 PVA_FF_AtomUtils::renderNullTerminatedUnicodeString(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, PVA_FF_UNICODE_STRING_PARAM data)
198 {
199     if (fp != NULL)
200     {
201         PVA_FF_AtomUtils::renderUnicodeString(fp, data);
202         // Need two bytes of zeros for UNICODE null termination
203         uint16 data = 0;
204         if (fp->_filePtr->Write(&data, 2, 1) != 1)
205         {
206             return false;
207         }
208         return true;
209     }
210     return false;
211 }
212 
213 // Render a chunk of uint8 data
214 bool
renderByteData(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint32 length,uint8 * data)215 PVA_FF_AtomUtils::renderByteData(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint32 length, uint8 *data)
216 {
217     bool retVal = true;
218 
219     if (fp != NULL)
220     {
221         if (fp->_filePtr->Write(data, 1, length) != length)
222         {
223             retVal = false;
224         }
225     }
226     else
227     {
228         retVal = false;
229     }
230 
231     return (retVal);
232 }
233 
234 // Setting the current time value - seconds since 1/1/1904
235 void
setTime(uint32 & ulTime)236 PVA_FF_AtomUtils::setTime(uint32 &ulTime)
237 {
238     // 2082844800 is number of seconds from Jan 1, 1904 to Jan 1, 1970.
239     // time() returns the seconds since Jan 1, 1970.
240     ulTime = uint32(time(NULL) + 2082844800 );
241 }
242 
243 
244 uint32
getNumberOfBytesUsedToStoreContent(uint32 sizeOfClass)245 PVA_FF_AtomUtils::getNumberOfBytesUsedToStoreContent(uint32 sizeOfClass)
246 {
247     // The content in a descriptor class fp stored immediately after the descriptor tag
248     if (sizeOfClass <= 0x7f) return sizeOfClass - 2; // _sizeOfClass field fp 1 byte (7 LS bits)
249     else if (sizeOfClass <= 0x3fff) return sizeOfClass - 3; // _sizeOfClass fp 2 bytes (7 LS bits each)
250     else if (sizeOfClass <= 0x1fffff) return sizeOfClass - 4; // _sizeOfClass fp 3 bytes (7 LS bits each)
251     else if (sizeOfClass <= 0x0fffffff) return sizeOfClass - 5; // _sizeOfClass fp 4 bytes (7 LS bits each)
252     else return 0; // ERROR condition
253 }
254 
255 uint32
getNumberOfBytesUsedToStoreSizeOfClass(uint32 contentSize)256 PVA_FF_AtomUtils::getNumberOfBytesUsedToStoreSizeOfClass(uint32 contentSize)
257 {
258     // The actual _sizeOfClass value includes the size of the class's contents PLUS
259     // the number of bytes needed to store the _sizeOfClass field. The parameter
260     // contentSize represents the number of bytes needed to store ONLY the members
261     // of the class NOT including the _sizeOfClass field.
262     if (contentSize <= 0x7e) return 1; // _sizeOfClass field can be rendered in 1 byte (7 LS bits)
263     else if (contentSize <= 0x3ffd) return 2; // _sizeOfClass field can be rendered in 2 bytes (7 LS bits each)
264     else if (contentSize <= 0x1ffffc) return 3; // _sizeOfClass field can be rendered in 3 bytes (7 LS bits each)
265     else if (contentSize <= 0xfffffffb) return 4; // _sizeOfClass field can be rendered in 4 bytes (7 LS bits each)
266     else return 0; // ERROR condition
267 }
268 
seekFromCurrPos(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint32 n)269 void PVA_FF_AtomUtils::seekFromCurrPos(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint32 n)
270 {
271     fp->_filePtr->Seek(n, Oscl_File::SEEKCUR);
272 }
273 
seekFromStart(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint32 n)274 void PVA_FF_AtomUtils::seekFromStart(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint32 n)
275 {
276     fp->_filePtr->Seek(n, Oscl_File::SEEKSET);
277 }
278 
seekToEnd(MP4_AUTHOR_FF_FILE_IO_WRAP * fp)279 void PVA_FF_AtomUtils::seekToEnd(MP4_AUTHOR_FF_FILE_IO_WRAP *fp)
280 {
281     fp->_filePtr->Seek(0, Oscl_File::SEEKEND);
282 }
283 
rewindFilePointerByN(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint32 n)284 void PVA_FF_AtomUtils::rewindFilePointerByN(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint32 n)
285 {
286     fp->_filePtr->Seek((-1 *(int32) n), Oscl_File::SEEKCUR);
287 }
288 
getCurrentFilePosition(MP4_AUTHOR_FF_FILE_IO_WRAP * fp)289 int32 PVA_FF_AtomUtils::getCurrentFilePosition(MP4_AUTHOR_FF_FILE_IO_WRAP *fp)
290 {
291     return (fp->_filePtr->Tell());
292 }
293 
294 bool
openFile(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,PVA_FF_UNICODE_STRING_PARAM fileName,int32 mode,uint32 aCacheSize)295 PVA_FF_AtomUtils::openFile(MP4_AUTHOR_FF_FILE_IO_WRAP *fp,
296                            PVA_FF_UNICODE_STRING_PARAM fileName,
297                            int32 mode, uint32 aCacheSize)
298 {
299     if (!fp->_filePtr)
300     {
301         // Only need to create new file object if it's not passed in from createMp4File
302         //fp->_filePtr = OSCL_NEW(Oscl_File, ());
303         PV_MP4_FF_NEW(fp->auditCB, Oscl_File, (), fp->_filePtr);
304     }
305 
306     if (fp->_osclFileServerSession->Connect() != 0)
307         return false;
308 
309     fp->_filePtr->SetPVCacheSize(aCacheSize);
310     return (fp->_filePtr->Open(fileName.get_cstr(), mode, *(fp->_osclFileServerSession)) == 0);
311 }
312 
313 void
closeFile(MP4_AUTHOR_FF_FILE_IO_WRAP * fp)314 PVA_FF_AtomUtils::closeFile(MP4_AUTHOR_FF_FILE_IO_WRAP *fp)
315 {
316     fp->_filePtr->Close();
317     //OSCL_DELETE(fp->_filePtr);
318     PV_MP4_FF_DELETE(NULL, Oscl_File, fp->_filePtr);
319 
320     fp->_filePtr = NULL;
321 }
322 
closeFileSession(Oscl_FileServer * fs)323 void PVA_FF_AtomUtils::closeFileSession(Oscl_FileServer* fs)
324 {
325     if (fs)
326         fs->Close();
327 }
328 
329 bool
readByteData(MP4_AUTHOR_FF_FILE_IO_WRAP * fp,uint32 length,uint8 * data)330 PVA_FF_AtomUtils::readByteData(MP4_AUTHOR_FF_FILE_IO_WRAP *fp, uint32 length, uint8 *data)
331 {
332     uint32 bytesRead;
333     bytesRead = fp->_filePtr->Read(data, 1, length);
334 
335     if (bytesRead < (uint32)length) // read byte data failed
336         return false;
337     return true;
338 }
339 
340 
341