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 AtomUtils Class */ 21 /* ------------------------------------------------------------------- */ 22 /*********************************************************************************/ 23 /* 24 This AtomUtils Class contains sime useful methods for operating on Atoms 25 */ 26 27 28 #ifndef ATOMUTILS_H_INCLUDED 29 #define ATOMUTILS_H_INCLUDED 30 31 #ifndef OSCL_BASE_H_INCLUDE 32 #include "oscl_base.h" 33 #endif 34 35 #ifndef OSCL_FILE_IO_H_INCLUDED 36 #include "oscl_file_io.h" 37 #endif 38 39 #ifndef OSCL_STRING_CONTAINERS_H_INCLUDED 40 #include "oscl_string_containers.h" 41 #endif 42 43 #ifndef OSCL_STRING_H_INCLUDED 44 #include "oscl_string.h" 45 #endif 46 47 #ifndef PVLOGGER_H_INCLUDED 48 #include "pvlogger.h" 49 #endif 50 51 #ifndef OSCL_MEM_BASIC_FUNCTIONS_H 52 #include "oscl_mem_basic_functions.h" 53 #endif 54 55 #ifndef ATOMDEFS_H_INCLUDED 56 #include "atomdefs.h" 57 #endif 58 59 /* CPM Related Header Files */ 60 #ifndef CPM_H_INCLUDED 61 #include "cpm.h" 62 #endif 63 #ifndef PVFILE_H_INCLUDED 64 #include "pvfile.h" 65 #endif 66 67 typedef Oscl_File* MP4_FF_FILE_REFERENCE; 68 69 #define DEFAULT_ATOM_SIZE 8 //need to change when they use various size field 70 #define PVMF_MP4FFPARSER_LOGDIAGNOSTICS(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_LLDBG,iDiagnosticsLogger,PVLOGMSG_INFO,m); 71 #define PVMF_MP4FFPARSER_LOGMEDIASAMPELSTATEVARIABLES(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_MLDBG,iStateVarLogger,PVLOGMSG_INFO,m); 72 #define PVMF_MP4FFPARSER_LOGPARSEDINFO(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_MLDBG,iParsedDataLogger,PVLOGMSG_INFO,m); 73 #define PVMF_MP4FFPARSER_LOGERROR(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_REL,iLogger,PVLOGMSG_ERR,m); 74 75 #define PV_MP4_FF_NEW(auditCB,T,params,ptr)\ 76 {\ 77 ptr = OSCL_NEW(T,params);\ 78 } 79 80 81 #define PV_MP4_FF_DELETE(auditCB,T,ptr)\ 82 {\ 83 OSCL_DELETE(ptr);\ 84 } 85 86 #define PV_MP4_FF_TEMPLATED_DELETE(auditCB,T,Tsimple,ptr)\ 87 {\ 88 OSCL_DELETE(ptr);\ 89 } 90 91 92 #define PV_MP4_FF_ARRAY_MALLOC(auditCB,T,count,ptr)\ 93 {\ 94 ptr = (T*)OSCL_MALLOC(count);\ 95 } 96 97 98 #define PV_MP4_ARRAY_FREE(auditCB,ptr)\ 99 {\ 100 OSCL_FREE(ptr);\ 101 } 102 103 #define PV_MP4_FF_ARRAY_NEW(auditCB, T, count, ptr)\ 104 {\ 105 ptr = OSCL_ARRAY_NEW(T, count);\ 106 } 107 108 #define PV_MP4_ARRAY_DELETE(auditCB, ptr)\ 109 {\ 110 OSCL_ARRAY_DELETE(ptr);\ 111 } 112 113 114 class MP4_FF_FILE 115 { 116 public: MP4_FF_FILE()117 MP4_FF_FILE() 118 : _fileSize(0) 119 , _fileServSession(NULL) 120 { 121 } MP4_FF_FILE(const MP4_FF_FILE & a)122 MP4_FF_FILE(const MP4_FF_FILE& a) 123 : _fileSize(a._fileSize) 124 , _fileServSession(a._fileServSession) 125 , _pvfile(a._pvfile) 126 { 127 } IsOpen()128 bool IsOpen() 129 { 130 return _pvfile.IsOpen(); 131 } 132 133 int32 _fileSize; 134 Oscl_FileServer* _fileServSession; 135 PVFile _pvfile; 136 }; 137 138 class AtomUtils 139 { 140 141 public: 142 // Methods for reading in data from a file stream 143 OSCL_IMPORT_REF static bool read64(MP4_FF_FILE *fp, uint64 &data); 144 OSCL_IMPORT_REF static bool read32(MP4_FF_FILE *fp, uint32 &data); 145 OSCL_IMPORT_REF static bool read32read32(MP4_FF_FILE *fp, uint32 &data1, uint32 &data2); 146 OSCL_IMPORT_REF static bool read24(MP4_FF_FILE *fp, uint32 &data); 147 OSCL_IMPORT_REF static bool read16(MP4_FF_FILE *fp, uint16 &data); 148 OSCL_IMPORT_REF static bool read16read16(MP4_FF_FILE *fp, uint16 &data1, uint16 &data2); 149 OSCL_IMPORT_REF static bool read8(MP4_FF_FILE *fp, uint8 &data); 150 OSCL_IMPORT_REF static bool read8read8(MP4_FF_FILE *fp, uint8 &data1, uint8 &data2); 151 OSCL_IMPORT_REF static bool readNullTerminatedString(MP4_FF_FILE *fp, OSCL_wString& data); 152 153 OSCL_IMPORT_REF static bool readNullTerminatedUnicodeString(MP4_FF_FILE *fp, OSCL_wString& data); 154 OSCL_IMPORT_REF static bool readNullTerminatedAsciiString(MP4_FF_FILE *fp, OSCL_wString& data); 155 156 OSCL_IMPORT_REF static bool readString(MP4_FF_FILE *fp, uint32 inLength, MP4FFParserOriginalCharEnc &CharType, OSCL_wString& data); 157 OSCL_IMPORT_REF static bool readUTF8String(MP4_FF_FILE *fp, uint32 inLength, OSCL_wString& data); 158 OSCL_IMPORT_REF static bool readAsciiString(MP4_FF_FILE *fp, uint32 inLength, OSCL_wString& data); 159 OSCL_IMPORT_REF static bool readUnicodeString(MP4_FF_FILE *fp, uint32 inLength, OSCL_wString& data); 160 161 OSCL_IMPORT_REF static bool readByteData(MP4_FF_FILE *fp, uint32 length, uint8 *data); 162 OSCL_IMPORT_REF static bool readUnicodeData(MP4_FF_FILE *fp, uint32 length, uint16 *data); 163 164 // Method to set time value in seconds since 1904 165 OSCL_IMPORT_REF static void setTime(uint32& ulTime); 166 167 // This method is used to calculate the number of bytes needed to store the 168 // overall size of the class - the value contentSize is the size of the class 169 // NOT including the actual _sizeOfClass field (since this is a variable-length 170 // field). This is used when determining the actual _sizeOfClass value for 171 // all the Descriptor and Command classes. 172 OSCL_IMPORT_REF static uint32 getNumberOfBytesUsedToStoreSizeOfClass(uint32 contentSize); 173 174 // Returns the atom type from parsing the input stream 175 OSCL_IMPORT_REF static void getNextAtomType(MP4_FF_FILE *fp, uint32 &size, uint32 &type); 176 OSCL_IMPORT_REF static int32 getNextAtomSize(MP4_FF_FILE *fp); 177 OSCL_IMPORT_REF static uint32 getMediaTypeFromHandlerType(uint32 handlerType); 178 OSCL_IMPORT_REF static uint32 getNumberOfBytesUsedToStoreContent(uint32 sizeOfClass); 179 180 // Peeks and returns the next Nth byte from the file 181 OSCL_IMPORT_REF static uint32 peekNextNthBytes(MP4_FF_FILE *fp, int32 n); 182 OSCL_IMPORT_REF static uint8 peekNextByte(MP4_FF_FILE *fp); 183 OSCL_IMPORT_REF static void seekFromCurrPos(MP4_FF_FILE *fp, uint32 n); 184 OSCL_IMPORT_REF static void seekFromStart(MP4_FF_FILE *fp, uint32 n); 185 OSCL_IMPORT_REF static void seekToEnd(MP4_FF_FILE *fp); 186 OSCL_IMPORT_REF static void rewindFilePointerByN(MP4_FF_FILE *fp, uint32 n); 187 OSCL_IMPORT_REF static int32 getCurrentFilePosition(MP4_FF_FILE *fp); 188 OSCL_IMPORT_REF static int32 OpenMP4File(OSCL_wString& filename, uint32 mode, MP4_FF_FILE *fp); 189 OSCL_IMPORT_REF static int32 CloseMP4File(MP4_FF_FILE *fp); 190 OSCL_IMPORT_REF static int32 Flush(MP4_FF_FILE *fp); 191 OSCL_IMPORT_REF static bool getCurrentFileSize(MP4_FF_FILE *fp, uint32& aCurrentSize); 192 193 OSCL_IMPORT_REF static bool read32(uint8 *&buf, uint32 &data); 194 OSCL_IMPORT_REF static bool read32read32(uint8 *&buf, uint32 &data1, uint32 &data2); 195 OSCL_IMPORT_REF static bool read16(uint8 *&buf, uint16 &data); 196 OSCL_IMPORT_REF static bool read8(uint8 *&buf, uint8 &data); 197 OSCL_IMPORT_REF static bool readByteData(uint8 *&buf, uint32 length, uint8 *data); 198 OSCL_IMPORT_REF static uint32 getNextAtomType(uint8 *buf); 199 OSCL_IMPORT_REF static int32 getNextAtomSize(uint8 *buf); 200 201 // returns content length 202 OSCL_IMPORT_REF static uint32 getContentLength(MP4_FF_FILE *fp); 203 // returns the data stream cache size 204 OSCL_IMPORT_REF static uint32 getFileBufferingCapacity(MP4_FF_FILE *fp); 205 // peek to a new offset (MBDS only), read pointer does not change 206 // this is used to trigger a http request 207 OSCL_IMPORT_REF static void skipFromStart(MP4_FF_FILE *fp, uint32 n); 208 // for progressive playback seeking 209 // returns the range of available bytes in MBDS 210 // first + last byte offset inclusive 211 OSCL_IMPORT_REF static void getCurrentByteRange(MP4_FF_FILE *fp, uint32& aCurrentFirstByteOffset, uint32& aCurrentLastByteOffset); 212 213 214 }; 215 216 #endif // ATOMUTILS_H_INCLUDED 217 218 219