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 // A A C F I L E P A R S E R 22 23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25 /** 26 * @file aacfileparser.h 27 * @brief This file defines the raw AAC file parser. 28 */ 29 30 #ifndef AACFILEPARSER_H_INCLUDED 31 #define AACFILEPARSER_H_INCLUDED 32 33 //---------------------------------------------------------------------------- 34 // INCLUDES 35 //---------------------------------------------------------------------------- 36 37 #ifndef OSCL_BASE_H_INCLUDED 38 #include "oscl_base.h" 39 #endif 40 41 #ifndef OSCL_STRING_CONTAINERS_H_INCLUDED 42 #include "oscl_string_containers.h" 43 #endif 44 45 #ifndef OSCL_FILE_IO_H_INCLUDED 46 #include "oscl_file_io.h" 47 #endif 48 49 #ifndef OSCL_MEM_H_INCLUDED 50 #include "oscl_mem.h" 51 #endif 52 53 #ifndef OSCL_VECTOR_H_INCLUDED 54 #include "oscl_vector.h" 55 #endif 56 57 #ifndef PV_GAU_H 58 #include "pv_gau.h" 59 #endif 60 61 #ifndef PVFILE_H_INCLUDED 62 #include "pvfile.h" 63 #endif 64 65 #ifndef PV_ID3_PARCOM_H_INCLUDED 66 #include "pv_id3_parcom.h" 67 #endif 68 69 #ifndef PVLOGGER_H_INCLUDED 70 #include "pvlogger.h" 71 #endif 72 73 //---------------------------------------------------------------------------- 74 // CONSTANTS 75 //---------------------------------------------------------------------------- 76 77 #define PACKET_INDICATOR_LENGTH 4 78 #define ADIF_HEADER_MINUS_INDICTATOR 16 79 #define ADTS_HEADER_LENGTH 7 80 #define BYTES_PER_SILENCE_FRAME 23 81 #define AAC_DECODER_SPECIFIC_INFO_SIZE 2 82 #define MAX_AAC_FRAME_SIZE 8192 // 8192 = 2^13, 13bit AAC frame size (in bytes) 83 #define MAX_ADTS_PACKET_LENGTH MAX_AAC_FRAME_SIZE 84 #define AAC_DECODER_INPUT_BUFF_SIZE 1536 // 6144 (bits) * 2 (channels) / 8 (bits per byte) 85 86 // ADTS sync parameters 87 88 //#define PERCENTAGE_IN_POW_2 6 // 2^(-6) == 1.56 %, search over 1.56% of file size 89 #define PERCENTAGE_IN_POW_2 5 // 2^(-5) == 3.13 %, search over 3.13% of file size 90 //#define PERCENTAGE_IN_POW_2 4 // 2^(-4) == 6.25 %, search over 6.25% of file size 91 //#define PERCENTAGE_IN_POW_2 3 // 2^(-3) == 12.50 %, search over 12.50% of file size 92 93 #define ADTS_SYNC_SEARCH_LENGTH(m, PERCENTAGE_IN_POW_2) (m>>PERCENTAGE_IN_POW_2) 94 95 96 #define PVMF_AACPARSER_LOGDIAGNOSTICS(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_MLDBG,iDiagnosticLogger,PVLOGMSG_INFO,m); 97 #define PVMF_AACPARSER_LOGERROR(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_REL,iLogger,PVLOGMSG_ERR,m); 98 99 100 #define PV_AAC_FF_NEW(auditCB,T,params,ptr)\ 101 {\ 102 ptr = OSCL_NEW(T,params);\ 103 } 104 105 106 #define PV_AAC_FF_DELETE(auditCB,T,ptr)\ 107 {\ 108 OSCL_DELETE(ptr);\ 109 } 110 111 #define PV_AAC_FF_TEMPLATED_DELETE(auditCB,T,Tsimple,ptr)\ 112 {\ 113 OSCL_DELETE(ptr);\ 114 } 115 116 #define PV_AAC_FF_ARRAY_MALLOC(auditCB,T,count,ptr)\ 117 {\ 118 ptr = (T*)OSCL_MALLOC(count);\ 119 } 120 121 122 #define PV_AAC_ARRAY_FREE(auditCB,ptr)\ 123 {\ 124 OSCL_FREE(ptr);\ 125 } 126 127 #define PV_AAC_FF_ARRAY_NEW(auditCB, T, count, ptr)\ 128 {\ 129 ptr = OSCL_ARRAY_NEW(T, count);\ 130 } 131 132 #define PV_AAC_ARRAY_DELETE(auditCB, ptr)\ 133 {\ 134 OSCL_ARRAY_DELETE(ptr);\ 135 } 136 137 enum ParserErrorCode 138 { 139 GENERIC_ERROR = -4, 140 INSUFFICIENT_DATA = -3, 141 FILE_OPEN_ERROR = -2, 142 MEMORY_ERROR = -1, 143 OK = 1 144 }; 145 146 /* 147 * AAC format types supported 148 */ 149 enum TAACFormat 150 { 151 EAACADTS, 152 EAACADIF, 153 EAACRaw, 154 EAACUnrecognized 155 }; 156 157 /* 158 * Sampling Frequency look up table 159 * The look up index is found in the 160 * header of an ADTS packet 161 */ 162 static const int32 ADTSSampleFreqTable[16] = 163 { 164 96000, /* 96000 Hz */ 165 88200, /* 88200 Hz */ 166 64000, /* 64000 Hz */ 167 48000, /* 48000 Hz */ 168 44100, /* 44100 Hz */ 169 32000, /* 32000 Hz */ 170 24000, /* 24000 Hz */ 171 22050, /* 22050 Hz */ 172 16000, /* 16000 Hz */ 173 12000, /* 12000 Hz */ 174 11025, /* 11025 Hz */ 175 8000, /* 8000 Hz */ 176 7350, /* 7350 Hz */ 177 -1, /* future use */ 178 -1, /* future use */ 179 -1 /* escape value */ 180 }; 181 182 /* 183 * Table containing silence frame to be generated 184 */ 185 static const uint8 SilenceFrameStereo[BYTES_PER_SILENCE_FRAME] = 186 { 187 0x21, 0x10, 0x03, 0x20, 0x64, 0x1B, 188 0xC0, 0x40, 0x00, 0x00, 0x00, 0x00, 189 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 190 0x00, 0x00, 0x00, 0x00, 0x38 191 }; 192 193 typedef struct 194 { 195 int32 iTimescale; 196 int32 iDuration; 197 int32 iSampleFrequency; 198 int32 iBitrate; 199 TAACFormat iFormat; 200 int32 iFileSize; 201 } TPVAacFileInfo; 202 203 204 //---------------------------------------------------------------------------- 205 // FORWARD CLASS DECLARATIONS 206 //---------------------------------------------------------------------------- 207 208 209 /** 210 * @brief The AACBitstreamObject Class is the class used by the AAC parser to 211 * manipulate the bitstream read from the file. 212 */ 213 214 class AACBitstreamObject 215 { 216 public: 217 enum 218 { 219 MAIN_BUFF_SIZE = 8192, 220 221 // error types for GetNextBundledAccessUnits(), 222 // the definition is consistent with MP4_ERROR_CODE in iSucceedFail.h 223 MISC_ERROR = -2, 224 READ_ERROR = -1, 225 EVERYTHING_OK = 0, 226 END_OF_FILE = 62, 227 INSUFFICIENT_DATA = 141 228 }; 229 230 /** 231 * @brief Constructor 232 * 233 * @param pFile Pointer to file pointer containing bitstream 234 * @returns None 235 */ 236 AACBitstreamObject(PVFile* file) 237 { 238 iLogger = PVLogger::GetLoggerObject("pvBitstream object"); 239 oscl_memset(this, 0, sizeof(AACBitstreamObject)); 240 init(file); 241 iBuffer = OSCL_ARRAY_NEW(uint8, AACBitstreamObject::MAIN_BUFF_SIZE); 242 if (!iBuffer) 243 { 244 iStatus = true; 245 } 246 else 247 { 248 iStatus = false; 249 } 250 id3Parser = OSCL_NEW(PVID3ParCom, ()); 251 } 252 253 /** 254 * @brief Destructor 255 * 256 * @param None 257 * @returns None 258 */ 259 ~AACBitstreamObject() 260 { 261 /*init();*/ 262 if (ipAACFile != NULL) 263 { 264 ipAACFile->Close(); 265 ipAACFile = NULL; 266 } 267 if (iBuffer) 268 { 269 OSCL_ARRAY_DELETE(iBuffer); 270 iBuffer = NULL; 271 } 272 273 if (id3Parser) 274 { 275 OSCL_DELETE(id3Parser); 276 id3Parser = NULL; 277 } 278 279 280 } 281 282 /** 283 * @brief Returns current bitstream status 284 * 285 * @param None 286 * @returns true=Bitstream instantiated; false=no bitstream 287 */ 288 inline bool get() 289 { 290 return iStatus; 291 } 292 293 /** 294 * @brief Returns status of CRC 295 * 296 * @param None 297 * @returns true=CRC enabled; false=CRC disabled 298 */ 299 inline bool isCRCEnabled() 300 { 301 return ibCRC_Check; 302 } 303 304 /** 305 * @brief Re-positions the file pointer. Specially used in ResetPlayback() 306 * 307 * @param filePos Position in file to move to. 308 * @returns Result of operation: EVERYTHING_OK, READ_ERROR etc. 309 */ 310 int32 reset(int32 filePos); 311 312 /** 313 * @brief Retrieves clip information: file size, format(ADIF or ADTS), 314 * sampling rate index, bitrate and header length 315 * 316 * @param fileSize Size of file 317 * @param format Format of fikle (ADIF or ADTS) 318 * @param sampleFreqInd Sampling frequency index 319 * @param bitRate Bit rate 320 * @param adifHeaderLen Length of header 321 * @returns Result of operation: EVERYTHING_OK, READ_ERROR etc. 322 */ 323 int32 getFileInfo(int32& fileSize, TAACFormat& format, uint8& sampleFreqIndex, uint32& bitRate, uint32& adifHeaderLen, OSCL_wString&); 324 325 /** 326 * @brief Expanded adts search for file information 327 * sampling rate index, bitrate and header length 328 * 329 * @param fileSize Size of file 330 * @param sampleFreqInd Sampling frequency index 331 * @param bitRate Bit rate 332 * @returns Result of operation: EVERYTHING_OK, READ_ERROR etc. 333 */ 334 int32 extendedAdtsSearchForFileInfo(TAACFormat& format, uint8& sampleFreqIndex); 335 336 /** 337 * @brief Retrieves frame size and number of data blocks for the next frame, in 338 * preparation of getNextFrame() 339 * 340 * @param frame_size Length of next frame 341 * @param numDateBlocks Number of data blocks of next frame 342 * @returns Result of operation: EVERYTHING_OK, READ_ERROR etc. 343 */ 344 int32 getNextFrameInfo(int32& frame_size, int32& numDateBlocks); 345 346 /** 347 * @brief get one frame data plus frame size and number of data blocks, 348 * used in getNextBundledAccessUnits() 349 * 350 * @param frameBuffer Buffer containing frame read 351 * @param frame_size Length of frame 352 * @param bHeaderIncluded Indicates whether header is to be included or not 353 * @returns Result of operation: EVERYTHING_OK, READ_ERROR etc. 354 */ 355 int32 getNextFrame(uint8* frameBuffer, int32 &frame_size, int32& header_size, bool bHeaderIncluded = false); 356 357 /** 358 * @brief Parses the ADTS header and construct MPEG-4 AAC decoder config header 359 * 360 * @param headerBuffer Buffer containing header 361 * @returns Result of operation: EVERYTHING_OK, READ_ERROR etc. 362 */ 363 int32 getDecoderConfigHeader(uint8* headerBuffer); 364 365 /** 366 * @brief Parses the ID3 header 367 * 368 * @param none 369 * @returns none 370 */ 371 void parseID3Header(PVFile&); 372 373 /** 374 * @brief Find ADTS sync word 375 * 376 * @param frameBuffer Buffer containing frame read 377 * @returns Result of operation: offset on bytes on success, -1 when no sync word found. 378 */ 379 int32 find_adts_syncword(uint8 *pBuffer); 380 381 /** 382 * @brief Determines if clip is AAC 383 * 384 * @param PVFile& 385 * @returns Result of operation: EVERYTHING_OK, READ_ERROR etc. 386 */ 387 int32 isAACFile(); 388 389 private: 390 391 /** 392 * @brief Initialization 393 * 394 * @param pFile Pointer to file pointer containing bitstream 395 * @returns None 396 */ 397 inline void init(PVFile* pFile = NULL) 398 { 399 iFileSize = iBytesRead = iBytesProcessed = 0; 400 ipAACFile = pFile; 401 iActual_size = iMax_size = AACBitstreamObject::MAIN_BUFF_SIZE; 402 iPos = AACBitstreamObject::MAIN_BUFF_SIZE; 403 iAACFormat = EAACUnrecognized; 404 if (ipAACFile) 405 { 406 ipAACFile->Seek(0, Oscl_File::SEEKSET); 407 } 408 } 409 410 /** 411 * @brief Reads data from bitstream, this is the only function to read data from file 412 * 413 * @param None 414 * @returns Result of operation: EVERYTHING_OK, READ_ERROR etc. 415 */ 416 int32 refill(); 417 418 private: 419 int32 iPos; // pointer for buffer[] 420 int32 iActual_size; // number of bytes read from a file once <= max_size 421 int32 iMax_size; // max_size = bitstreamStruc::MAIN_BUFF_SIZE 422 int32 iBytesRead; // (cumulative) number of bytes read from a file so far 423 int32 iBytesProcessed; // (cumulative) number of bytes processed so far. 424 int32 iFileSize; // file size of the ipAACFile 425 TAACFormat iAACFormat; // 0 : ADTS 1: ADIF 2 : Raw 426 bool ibCRC_Check; // CRC check flag 427 bool iStatus; // 1: ok for memory allocation in constructor 428 // 0: fail in memory allocation in constructor 429 uint8 iSampleFreqIndex; // index for sampling rate 430 int32 iPosSyncAdtsFound;// pointer for where in buffer[] the search algo. found a 431 // possible match, used for extended search 432 433 uint8 *iBuffer; 434 uint8 iAACHeaderBuffer[PACKET_INDICATOR_LENGTH+12]; 435 PVFile* ipAACFile; // bitstream file 436 437 uint8 iAudioObjectType; // audio object type 438 uint32 iChannelConfig; // channel configuration 439 uint32 iADIFHeaderLen; // variable length 440 uint32 iRawAACHeaderLen; // Audio specific config size in bytes for raw AAC bitstream files 441 uint32 iBitrate; // max bitrate for variable rate bitstream 442 PVID3ParCom* id3Parser; 443 PVLogger *iLogger; 444 445 public: 446 uint32 GetByteOffsetToStartOfAudioFrames() 447 { 448 return id3Parser->GetByteOffsetToStartOfAudioFrames(); 449 450 } 451 452 void ID3MetaData(PvmiKvpSharedPtrVector &id3Frames) 453 { 454 id3Parser->GetID3Frames(id3Frames); 455 456 } 457 458 bool IsID3Frame(const OSCL_String& frameType) 459 { 460 return id3Parser->IsID3FrameAvailable(frameType); 461 } 462 463 void GetID3Frame(const OSCL_String& aFrameType, PvmiKvpSharedPtrVector& aFrame) 464 { 465 id3Parser->GetID3Frame(aFrameType, aFrame); 466 } 467 468 PVID3Version GetID3Version() const 469 { 470 return id3Parser->GetID3Version(); 471 } 472 473 }; 474 475 476 /** 477 * @brief The CAACFileParser Class is the class that will construct and maintain all the 478 * necessary data structures to be able to render a valid AAC file to disk. 479 * 480 * This class supports the following AAC file format specs: 481 * 1) ADIF 482 * 2) ADTS 483 * 3) Raw bitstream with audio specific config 484 */ 485 class CAACFileParser 486 { 487 public: 488 typedef OsclMemAllocator alloc_type; 489 490 /** 491 * @brief Constructor 492 * 493 * @param None 494 * @returns None 495 */ 496 OSCL_IMPORT_REF CAACFileParser(); 497 498 /** 499 * @brief Destructor 500 * 501 * @param None 502 * @returns None 503 */ 504 OSCL_IMPORT_REF ~CAACFileParser(); 505 506 /** 507 * @brief Opens the specified file and performs initialization of the parser 508 * 509 * @param aClip Filename to parse 510 * @param aInitParsingEnable Indicates whether to setup random positioning (true) 511 * or not (false) 512 * @param aFileSession Pointer to opened file server session. Used when opening 513 * and reading the file on certain operating systems. 514 * @returns true if the init succeeds, else false. 515 */ 516 OSCL_IMPORT_REF bool InitAACFile(OSCL_wString& aClip, bool aInitParsingEnable = true, Oscl_FileServer* aFileSession = NULL, PVMFCPMPluginAccessInterfaceFactory* aCPMAccess = NULL, OsclFileHandle* aHandle = NULL); 517 518 /** 519 * @brief Resets the parser variables so playback can be restarted at the 520 * specified time. 521 * 522 * @param aStartTime value as where to start repositioning to 523 * @returns Result of operation: EVERYTHING_OK, 524 */ 525 OSCL_IMPORT_REF int32 ResetPlayback(uint32 aStartTime, uint32& aActualStartTime); 526 527 /** 528 * @brief Returns the actual starting timestamp for a specified start time 529 * 530 * @param aStartTime Time where to start playback from 531 * @returns Timestamp corresponding to the actual start position 532 */ 533 OSCL_IMPORT_REF uint32 SeekPointFromTimestamp(uint32 aStartTime = 0); 534 535 /** 536 * @brief Attempts to read in the number of audio frames specified by aNumSamples 537 * 538 * @param aNumSamples Requested number of frames to be read from file 539 * @param aGau Frame information structure of type GAU 540 * @param bADTSHeaderIncluded, set to true in case ADTS headers need to be included 541 * @returns Result of operation: EVERYTHING_OK, 542 */ 543 OSCL_IMPORT_REF int32 GetNextBundledAccessUnits(uint32 *aNumSamples, 544 GAU *aGau, 545 bool bADTSHeaderIncluded = false); 546 547 /** 548 * @brief Returns the value of the timestamp for the next frame, as would 549 * be returned by GetNextBundledAccessUnits, but does not process any data. 550 * 551 * @param aTimestamp Next timestamp. 552 * @returns Result of operation: EVERYTHING_OK, 553 */ 554 OSCL_IMPORT_REF int32 PeekNextTimestamp(uint32&aTimestamp); 555 556 /** 557 * @brief Retrieves information about the clip such as bit rate, sampling frequency, etc. 558 * 559 * @param aInfo Storage for information retrieved 560 * @returns True if successful, False otherwise. 561 */ 562 OSCL_IMPORT_REF bool RetrieveFileInfo(TPVAacFileInfo& aInfo); 563 564 /** 565 * @brief Retrieves information about the clip from the ID3 tags if any. 566 * 567 * @param aInfo Storage for information retrieved 568 * @returns True if successful, False otherwise. 569 */ 570 OSCL_IMPORT_REF bool RetrieveID3Info(PvmiKvpSharedPtrVector& aID3MetaData); 571 /** 572 * @brief checks if ID3 frame is available. 573 * 574 * @param frameType 575 * @returns True if successful, False otherwise. 576 */ 577 OSCL_IMPORT_REF bool IsID3Frame(const OSCL_String &frameType); 578 579 /** 580 * @brief retrieves ID3 Frame. 581 * 582 * @param frameType ID of the frame to be retrieved 583 * @param aFrame data of the frame of type frametype 584 * @returns True if successful, False otherwise. 585 */ 586 587 OSCL_IMPORT_REF void GetID3Frame(const OSCL_String& aFrameType, PvmiKvpSharedPtrVector& aFrame); 588 589 /** 590 * @brief Returns the size of the decoder specific info 591 * 592 * @param None 593 * @returns Length of decoder specific info 594 */ 595 OSCL_IMPORT_REF int32 GetTrackDecoderSpecificInfoSize(void); 596 597 /** 598 * @brief Returns the content of the decoder specific info 599 * 600 * @param None 601 * @returns Pointer to decoder specific info 602 */ 603 OSCL_IMPORT_REF uint8* GetTrackDecoderSpecificInfoContent(void); 604 605 /** 606 * @brief Returns the format (ADTS, ADIF) of the file 607 * 608 * @param None 609 * @returns Format type 610 */ 611 OSCL_IMPORT_REF TAACFormat GetAACFormat(void); 612 613 OSCL_IMPORT_REF ParserErrorCode getAACHeaderLen(OSCL_wString& aClip, bool aInitParsingEnable, Oscl_FileServer* iFileSession, PVMFCPMPluginAccessInterfaceFactory* aCPMAccess, OsclFileHandle*aHandle, uint32* HeaderLen); 614 615 OSCL_IMPORT_REF PVID3Version GetID3Version() const; 616 617 /** 618 * @brief Returns if file is AAC 619 * 620 * @param 621 * @returns status 622 */ 623 OSCL_IMPORT_REF ParserErrorCode IsAACFile(OSCL_wString& aClip, Oscl_FileServer* aFileSession, PVMFCPMPluginAccessInterfaceFactory* aCPMAccess, OsclFileHandle* aHandle = NULL); 624 625 private: 626 PVFile iAACFile; 627 628 int32 iAACDuration; 629 int32 iAACSampleFrequency; 630 631 int32 iAACBitRate; 632 int32 iAACHeaderLen; 633 bool iFirstTime; 634 635 int32 iAACFileSize; 636 int32 iTotalNumFramesRead; 637 Oscl_Vector<int32, alloc_type> iRPTable; 638 639 TAACFormat iAACFormat; 640 bool iEndOfFileReached; 641 642 // Decoder input buffer for 1 raw encoded speech frame 643 uint8 iAACFrameBuffer[PACKET_INDICATOR_LENGTH + 12]; 644 645 //uint8 iAACHeaderReferenceBuffer[PACKET_INDICATOR_LENGTH]; 646 PVLogger* iLogger; 647 PVLogger* iDiagnosticLogger; 648 649 AACBitstreamObject *ipBSO; 650 651 652 }; 653 654 #endif //AACFILEPARSER_H_INCLUDED 655