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 // -*- c++ -*- 19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 20 21 // M P 3 F I L E I O 22 23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25 26 /** 27 * @file mp3fileio.cpp 28 * @brief This include file contains the implementation of the MP3 file 29 * IO utility functions. 30 */ 31 32 //---------------------------------------------------------------------- 33 // Defines 34 //---------------------------------------------------------------------- 35 #define IMPLEMENT_MP3FileIO 36 37 //---------------------------------------------------------------------- 38 // Include Files 39 //---------------------------------------------------------------------- 40 #include "mp3fileio.h" 41 #include "oscl_utf8conv.h" 42 43 44 //---------------------------------------------------------------------- 45 // Type Declarations 46 //---------------------------------------------------------------------- 47 48 //---------------------------------------------------------------------- 49 // Global Constant Definitions 50 //---------------------------------------------------------------------- 51 52 //---------------------------------------------------------------------- 53 // Global Data Definitions 54 //---------------------------------------------------------------------- 55 56 //---------------------------------------------------------------------- 57 // Static Variable Definitions 58 //---------------------------------------------------------------------- 59 60 /*====================================================================== 61 FUNCTION: **** FILE POINTER READING METHODS 62 63 DESCRIPTION: 64 65 INPUT/OUTPUT PARAMETERS: 66 67 RETURN VALUE: 68 69 SIDE EFFECTS: 70 71 ======================================================================*/ 72 73 // Read in the 64 bits byte by byte and take most significant byte first 74 bool MP3FileIO::read64(PVFile *fp, uint64 &data) 75 { 76 const int32 N = 8; 77 uint8 bytes[N]; 78 data = 0; 79 80 int32 retVal = (int32)(fp->Read((void*)bytes, 1, N)); 81 82 if (retVal < N) 83 return false; 84 85 for (int32 i = 0; i < N; i++) 86 { 87 // data = (data<<8) | bytes[i]; 88 } 89 90 return true; 91 } 92 93 // Read in the 32 bits byte by byte and take most significant byte first 94 bool MP3FileIO::read32(PVFile *fp, uint32 &data) 95 { 96 const int32 N = 4; 97 uint8 bytes[N]; 98 data = 0; 99 100 int32 retVal = (int32)(fp->Read((void*)bytes, 1, N)); 101 102 if (retVal < N) 103 return false; 104 105 for (int32 i = 0; i < N; i++) 106 data = (data << 8) | bytes[i]; 107 108 return true; 109 } 110 111 // Read in the 32 bits byte by byte and take most significant byte first. 112 // This is equivalent to two read32 calls. 113 bool MP3FileIO::read32read32(PVFile *fp, uint32 &data1, uint32 &data2) 114 { 115 const int32 N = 8; 116 uint8 bytes[N]; 117 data1 = 0; 118 data2 = 0; 119 120 int32 retVal = (int32)(fp->Read((void*)bytes, 1, N)); 121 122 if (retVal < N) 123 return false; 124 125 int32 i; 126 for (i = 0; i < 4; i++) 127 data1 = (data1 << 8) | bytes[i]; 128 129 for (i = 4; i < 8; i++) 130 data2 = (data2 << 8) | bytes[i]; 131 132 return true; 133 } 134 135 // Read in the 24 bits byte by byte and take most significant byte first 136 bool MP3FileIO::read24(PVFile *fp, uint32 &data) 137 { 138 const int32 N = 3; 139 uint8 bytes[N]; 140 data = 0; 141 142 int32 retVal = (int32)(fp->Read((void*)bytes, 1, N)); 143 144 if (retVal < N) 145 return false; 146 147 for (int32 i = 0; i < N; i++) 148 data = (data << 8) | bytes[i]; 149 150 return true; 151 } 152 153 // Read in the 16 bits byte by byte and take most significant byte first 154 bool MP3FileIO::read16(PVFile *fp, uint16 &data) 155 { 156 const int32 N = 2; 157 uint8 bytes[N]; 158 data = 0; 159 160 int32 retVal = (int32)(fp->Read((void*)bytes, 1, N)); 161 162 if (retVal < N) 163 return false; 164 165 for (int32 i = 0; i < N; i++) 166 data = (uint16)((data << 8) | (uint16)bytes[i]); 167 168 return true; 169 } 170 171 // Read in the 16 bits byte by byte and take most significant byte first 172 // This is equivalent to two read16 calls 173 bool MP3FileIO::read16read16(PVFile *fp, uint16 &data1, uint16 &data2) 174 { 175 const int32 N = 4; 176 uint8 bytes[N]; 177 data1 = 0; 178 data2 = 0; 179 180 int32 retVal = (int32)(fp->Read((void*)bytes, 1, N)); 181 182 if (retVal < N) 183 return false; 184 185 int32 i; 186 for (i = 0; i < 2; i++) 187 data1 = (uint16)((data1 << 8) | (uint16) bytes[i]); 188 189 for (i = 2; i < 4; i++) 190 data2 = (uint16)((data2 << 8) | (uint16) bytes[i]); 191 192 return true; 193 } 194 195 // Read in the 8 bit byte 196 bool MP3FileIO::read8(PVFile *fp, uint8 &data) 197 { 198 data = 0; 199 200 int32 retVal = (int32)(fp->Read((void*) & data, 1, 1)); 201 202 if (retVal < 1) 203 return false; 204 205 /* 206 int8 byte; 207 is.get(byte); 208 data = (uint8)byte; 209 */ 210 return true; 211 } 212 213 // Read in the 8 bit byte 214 // This is equivalent to two read8 calls 215 bool MP3FileIO::read8read8(PVFile *fp, uint8 &data1, uint8 &data2) 216 { 217 const int32 N = 2; 218 uint8 bytes[N]; 219 data1 = 0; 220 data2 = 0; 221 222 int32 retVal = (int32)(fp->Read((void*)bytes, 1, N)); 223 224 if (retVal < N) 225 return false; 226 227 data1 = bytes[0]; 228 data2 = bytes[1]; 229 230 return true; 231 } 232 233 // Read in byte data and take most significant byte first 234 bool MP3FileIO::readByteData(PVFile *fp, uint32 length, uint8 *data, uint32 *numbytes) 235 { 236 uint32 bytesRead; 237 bytesRead = fp->Read(data, 1, length); 238 239 if (numbytes) 240 *numbytes = bytesRead; 241 242 if (bytesRead < (uint32)length) // read byte data failed 243 { 244 int32 seekback = 0; 245 // bytes to seek backwards 246 seekback -= (int32) bytesRead; 247 fp->Seek(seekback, Oscl_File::SEEKCUR); 248 return false; 249 } 250 251 return true; 252 } 253 254 // Peeks and returns the next Nth tag (32 bits) from the file 255 uint32 MP3FileIO::peekNextNthBytes(PVFile *fp, int32 n) 256 { 257 uint32 tag = 0; 258 for (int32 i = 0; i < n; i++) 259 { 260 MP3FileIO::read32(fp, tag); 261 } 262 fp->Seek(-4*n, Oscl_File::SEEKCUR); 263 return tag; 264 } 265 266 // Peeks and returns the next Nth bytes (8 bits) from the file 267 uint8 MP3FileIO::peekNextByte(PVFile *fp) 268 { 269 uint8 tag = 0; 270 MP3FileIO::read8(fp, tag); 271 272 fp->Seek(-1, Oscl_File::SEEKCUR); 273 274 return tag; 275 } 276 277 // Returns the content length if known 278 // return 0 if unknown 279 uint32 MP3FileIO::getContentLength(PVFile *fp) 280 { 281 return fp->GetContentLength(); 282 } 283