MP4audioDecoder.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: MP4audioDecoder.cpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #include "MP4audioDecoder.hpp" 00045 #include "ContainerInfo.hpp" 00046 #include "ESInfo.hpp" 00047 #include "UncompressedAudioFrame.hpp" 00048 #include "IO.hpp" 00049 00050 #define readMarkerBit() readMarkerBitFunc(__LINE__) 00051 00052 // Table 6-3: Start code values 00053 #define MPEG_AUDIO_HEADER_START_CODE 0xFFF 00054 00055 static u32 __inline 00056 log2bin(u32 value) 00057 { 00058 /* Changed by Chenm001 */ 00059 // #if !defined(_MSC_VER) 00060 int n = 0; 00061 00062 while (value) { 00063 value >>= 1; 00064 n++; 00065 } 00066 return n; 00067 /* 00068 #else 00069 __asm { 00070 bsr eax, value 00071 inc eax 00072 } 00073 #endif 00074 */ 00075 } 00076 00077 bool MP4audioDecoder::isValidAudioFrame(const u8* payload) { 00078 return (payload && payload[0] == 0xFF && payload[1]>>5 == 0x07); 00079 00080 } 00081 00082 bool MP4audioDecoder::isValidDecoderConfig(const u8* payload) { 00083 return (payload && payload[0] == 0xFF && payload[1]>>5 == 0x07); 00084 } 00085 00086 00090 MP4audioDecoder::MP4audioDecoder(AudioESInfo * pESInfo, bool enableSwitching, meCoderType coderType) { 00091 mpDecoder = NULL; 00092 this->enableSwitching=enableSwitching; 00093 es = pESInfo; 00094 mFrameSize = 0; 00095 decConfig=NULL; 00096 decConfigSize=0; 00097 newDecConfig=NULL; 00098 meCoder = coderType; 00099 initialized = false; 00100 strcpy(name,"MP4audioDecoder"); 00101 } 00102 00103 00104 MP4audioDecoder::~MP4audioDecoder() { 00105 if (initialized) 00106 DecoderClose((char **) (&mpDecoder)); 00107 if(decConfig) 00108 delete[] decConfig; 00109 if(newDecConfig) 00110 delete[] newDecConfig; 00111 } 00112 00113 00114 void MP4audioDecoder::initialize() { 00115 if (!es) { 00116 initialized = false; 00117 return; 00118 } 00119 if(initialized) 00120 return; 00121 00122 mpDecoder = NULL; 00123 muiFrameNumber = 0; 00124 muiLastFrameStart = 0; 00125 muiLastFrameEnd = 0; 00126 decConfig=NULL; 00127 decConfigSize=0; 00128 newDecConfig=NULL; 00129 00130 muiLastFrameEnd = es->getHeaders(&decConfig); 00131 decConfigSize=muiLastFrameEnd; 00132 00133 if (decConfigSize > sizeof(mpBuffer)) { 00134 dprintf_err("MP4audioDecoder::initialize() decConfigSize %u > sizeof(mpBuffer) %u " 00135 "-- initialization FAILED!\n", decConfigSize, sizeof(mpBuffer)); 00136 return; 00137 } 00138 memcpy(mpBuffer,decConfig,decConfigSize); 00139 00140 00141 dprintf_full("MP4audioDecoder::initialize() %s\r\n",es->getEncodedDecoderConfig()); 00142 #ifdef VITOOKI_DEBUG 00143 for(uint i=0;i<(muiLastFrameEnd/4)||i<30;i+=4) { 00144 int res =0; 00145 for(int j = 0; j < 4; j++) { 00146 res = (res << 8) | mpBuffer[j+i]; 00147 } 00148 dprintf_full("%x ", res); 00149 } 00150 dprintf("\r\n"); 00151 fflush(stdout); 00152 #endif 00153 00154 #if VITOOKI_DEBUG_LEVEL >= 2 00155 dprintf_full("MP4audioDecoder::initialize:: "); 00156 for (uint iiii = 0; iiii < muiLastFrameEnd; iiii++) 00157 dprintf_full("%x", mpBuffer[iiii]); 00158 dprintf("\n"); 00159 #endif /* */ 00160 00161 if (muiLastFrameEnd == 0) { 00162 dprintf_err("MP4audioDecoder::initialize: failed to get header for ESInfo object\n"); 00163 } 00164 //initializeMPEGaudioHeaderStruct(); 00165 //initializeParser((u8 *) mpBuffer, muiLastFrameEnd); 00166 /* No need to parse the mp3 header as not a single information is required right now 00167 * we get it all from ffmpeg 00168 * HOWEVER if there are some errors in bitrate, channels etc then uncomment the following two line and 00169 * set the parameters in the "else" portion... and ONLY for #ifndef ISOMP4 00170 */ 00171 if (!parseMPEGaudioHeader((u8 *) mpBuffer, muiLastFrameEnd)) 00172 dprintf_small("MP4audioDecoder::initialize : No Audio Frame Header found\n"); 00173 00174 DecoderInit((char **) (&mpDecoder), es->getCodecID()); 00175 dprintf_small("MP4audioDecoder::initialize : Audio Decoder initialized \n"); 00176 00177 if (mpDecoder == NULL) { 00178 dprintf_err("MP4audioDecoder::initialize: Error creating decoder handle\n"); 00179 exit(-1); 00180 } 00181 else 00182 { 00183 #ifndef ISOMP4 00184 es->setMediaTimeScale(90000); 00185 if(mp4AudioHeader.sampling_rate>0) 00186 es->setSampleRate(mp4AudioHeader.sampling_rate); 00187 else { 00188 dprintf_err("MP4AudioDecoder::initialize: Wrong sampling rate for audio in %s\n",es->getContainerInfo()->getLocalFile()); 00189 es->setSampleRate(44100); 00190 } 00191 dprintf_full("MP4AudioDecoder::initialize: using sampling rate %i Hz\n",es->getSampleRate()); 00192 00193 ((AudioESInfo*)es)->setChannelMod(mp4AudioHeader.channel_mod); 00194 if(mp4AudioHeader.channel_mod == 3) 00195 es->setAudioChannels(1); 00196 else 00197 es->setAudioChannels(2); 00198 ((AudioESInfo*)es)->setAvgBandwidth(mp4AudioHeader.bitrate*1000); 00199 //es->setMaxBandwidth(mp4AudioHeader.bitrate*1000*3/2); //MAXBANDWIDTH for audio is 1.5 times AVGBANDWIDTH 00200 //es->setDuration((((es->getOrigMediaSize()*8)/(mp4AudioHeader.bitrate*1000))*mp4AudioHeader.sampling_rate)); 00201 //Process ONLY colorspaces definded in MP4audioDecoder.hpp 00202 #endif 00203 00204 mFrameSize = 4608; //XXX give it the appropiate value 00205 dprintf_full("MP4audioDecoder::initialize: Esinfo %llu BufferSize %i \n", 00206 es->getStreamId(), es->getBufferSize()); 00207 } 00208 initialized = true; 00209 } 00210 00211 00212 list < Frame * >MP4audioDecoder::adapt(Frame * frm) { 00213 if(es && enableSwitching) { 00214 if(decConfigSize>0) { 00215 if(newDecConfig) { 00216 delete[] newDecConfig; 00217 newDecConfig=NULL; 00218 } 00219 00220 u32 nSize=es->getHeaders(&newDecConfig); 00221 if(nSize != 0 && nSize != decConfigSize) { 00222 // we have a new decConfig 00223 if(decConfig) 00224 delete[] decConfig; 00225 decConfig=newDecConfig; 00226 newDecConfig=NULL; 00227 initialized=false; 00228 } 00229 else if(nSize == decConfigSize) { 00230 // same size but might be different 00231 if(memcmp(decConfig,newDecConfig,decConfigSize)!=0) { 00232 // is different 00233 if(decConfig) 00234 delete[] decConfig; 00235 decConfig=newDecConfig; 00236 newDecConfig=NULL; 00237 initialized=false; 00238 } 00239 } 00240 } 00241 } 00242 00243 if (!initialized) 00244 initialize(); 00245 00246 list < Frame * >frmList; 00247 if (!initialized) 00248 return frmList; 00249 00250 muiFrameNumber++; 00251 00252 #ifndef WINCE 00253 if (dec != NULL) { 00254 #endif //wince 00255 if (DecodeFrame((char *) mpDecoder, (char *) frm->getAU()->payload, frm->getAU()->size, 00256 (char *) mpBuffer, &mFrameSize) <= 0) { 00257 u8 *pload = frm->getAU()->payload; 00258 dprintf_err("MP4audioDecoder::adapt Error decoding frame %d\n", muiFrameNumber); 00259 dprintf_full("MP4audioDecoder::adapt %i %i %i %i\r\n", 00260 pload[0], pload[1],pload[2],pload[3]); 00261 } else { 00262 AU * pAU = new AU(frm->getAU()); 00263 pAU->size = mFrameSize; 00264 pAU->payload = new u8[mFrameSize]; 00265 memcpy(pAU->payload, mpBuffer, mFrameSize); 00266 dprintf_full("MP4audioDecoder::adapt Decode Frame No %d uncompressed size %d\n", 00267 muiFrameNumber, pAU->size); 00268 UncompressedAudioFrame* wavFrame = new UncompressedAudioFrame(Frame::RAW_AUDIO); 00269 wavFrame->setAU(pAU); 00270 frmList.push_back(wavFrame); 00271 } 00272 #ifndef WINCE 00273 } 00274 #endif //wince 00275 return frmList; 00276 } 00277 00278 00279 list < Frame * >MP4audioDecoder::close() { 00280 list < Frame * >frmList; 00281 if (!initialized) 00282 return frmList; 00283 00284 if (initialized) { 00285 DecoderClose((char **) (&mpDecoder)); 00286 initialized = false; 00287 } 00288 00289 return frmList; 00290 } 00291 00292 00293 Adaptor * MP4audioDecoder::clone() { 00294 return new MP4audioDecoder(es,enableSwitching,meCoder); 00295 } 00296 00297 00298 bool MP4audioDecoder::parseMPEGaudioHeader(u8 * pFrame, u32 iSize) { 00299 initializeMPEGaudioHeaderStruct(); 00300 initializeParser((u8 *) pFrame, iSize); 00301 00302 int offset = 0; 00303 if (getBits(11) == 0x07FF) 00304 { 00305 mp4AudioHeader.frame_sync = 0x07FF; 00306 mp4AudioHeader.mpeg_audio_version_ID = (MpegAudioVersionID)getBits(2); 00307 mp4AudioHeader.mpeg_audio_layer_no = (MpegAudioLayerNo)getBits(2); 00308 mp4AudioHeader.protection = getBits(1); 00309 switch(mp4AudioHeader.mpeg_audio_version_ID) { 00310 case MPEG_VERSION_2: 00311 switch(mp4AudioHeader.mpeg_audio_layer_no) { 00312 case LAYER_III: 00313 case LAYER_II : offset = 64; break; 00314 case LAYER_I : offset = 48; break; 00315 default : offset = 64; 00316 }; 00317 break; 00318 case MPEG_VERSION_1: 00319 switch(mp4AudioHeader.mpeg_audio_layer_no) { 00320 case LAYER_III: offset = 32; break; 00321 case LAYER_II : offset = 16; break; 00322 case LAYER_I : offset = 0; break; 00323 default : offset = 32; 00324 }; 00325 break; 00326 default: offset = 64; 00327 }; 00328 mp4AudioHeader.bitrate = mp4AudioHeader.mpeg_audio_bitrate_index_table[offset + getBits(4)]; 00329 switch(mp4AudioHeader.mpeg_audio_version_ID) { 00330 case MPEG_VERSION_2: 00331 offset = 4; break; 00332 case MPEG_VERSION_1: 00333 offset = 0; break; 00334 case MPEG_VESION_2_5: 00335 offset = 8; break; 00336 default: offset = 4; 00337 }; 00338 mp4AudioHeader.sampling_rate = mp4AudioHeader.mpeg_audio_sampling_rate_index_table[offset + getBits(2)]; 00339 mp4AudioHeader.padding = getBits(1); 00340 mp4AudioHeader.private_bit = getBits(1); 00341 mp4AudioHeader.channel_mod = (ChannelMod)getBits(2); 00342 mp4AudioHeader.mode_extension = getBits(2); 00343 mp4AudioHeader.copyright = getBits(1); 00344 mp4AudioHeader.original = getBits(1); 00345 mp4AudioHeader.emphasis = (Emphasis)getBits(2); 00346 00347 switch(mp4AudioHeader.mpeg_audio_layer_no) { 00348 case LAYER_I : 00349 mp4AudioHeader.frame_size_in_bytes = (12 * mp4AudioHeader.bitrate*1000 / mp4AudioHeader.sampling_rate + mp4AudioHeader.padding) * 4; 00350 break; 00351 case LAYER_II : 00352 case LAYER_III: 00353 mp4AudioHeader.frame_size_in_bytes = 144 * mp4AudioHeader.bitrate*1000 / mp4AudioHeader.sampling_rate + mp4AudioHeader.padding; 00354 break; 00355 default: 00356 mp4AudioHeader.frame_size_in_bytes = 418; 00357 }; 00358 00359 } 00360 else 00361 { 00362 return false; 00363 } 00364 return true; 00365 } 00366 00367 00368 void MP4audioDecoder::initializeParser(u8 * pFrame, u32 iSize) { 00369 mcByte = 0; 00370 if (pFrame != NULL) { 00371 mpcData = pFrame; 00372 miSize = iSize; 00373 00374 /* 00375 u8 * bla = new u8[miSize*2+1]; 00376 ISOMP4ContainerFile::encodeDecoderConfig(bla, mpcData, miSize); 00377 dprintf("---Decoder %s\n", bla); 00378 fflush(stdout); 00379 fflush(stderr); 00380 */ 00381 } else { 00382 mpcData = NULL; 00383 miSize = 0; 00384 } 00385 mbEOD = false; 00386 miCurrentNoBytes = 0; 00387 miCurrentBit = -1; 00388 } 00389 00390 00391 void MP4audioDecoder::initializeMPEGaudioHeaderStruct() { 00392 unsigned int mpeg_audio_bitrate_index_table[80] = { 00393 1,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0, 00394 1,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0, 00395 1,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0, 00396 1,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0, 00397 1,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0 00398 }; 00399 00400 /* 00401 unsigned int mpeg_audio_bitrate_index_table[80] = { 00402 1, 1, 1, 1, 1, 00403 32, 32, 32, 32, 8, 00404 64, 48, 40, 48, 16, 00405 96, 56, 48, 56, 24, 00406 128, 64, 56, 64, 32, 00407 160, 80, 64, 80, 40, 00408 192, 96, 80, 96, 48, 00409 224, 112, 96, 112, 56, 00410 256, 128, 112, 128, 64, 00411 288, 160, 128, 144, 80, 00412 320, 192, 160, 160, 96, 00413 352, 224, 192, 176, 112, 00414 384, 256, 224, 192, 128, 00415 416, 320, 256, 224, 144, 00416 448, 384, 320, 256, 160, 00417 0, 0, 0, 0, 0 00418 00419 }; 00420 */ 00421 00422 unsigned int mpeg_audio_sampling_rate_index_table[12] = { 00423 44100, 48000, 32000, 0, 00424 22050, 24000, 16000, 0, 00425 11025, 12000, 8000, 0 00426 }; 00427 00428 /* unsigned int mpeg_audio_sampling_rate_index_table[12] = { 00429 44100, 22050, 11025, 00430 48000, 24000, 12000, 00431 32000, 16000, 8000, 00432 0, 0, 0 00433 }; 00434 */ 00435 00436 mp4AudioHeader.frame_sync = 0; 00437 mp4AudioHeader.mpeg_audio_version_ID = MPEG_VERSION_2; 00438 mp4AudioHeader.mpeg_audio_layer_no = LAYER_III; 00439 mp4AudioHeader.protection = 1; 00440 mp4AudioHeader.bitrate = 0; 00441 mp4AudioHeader.sampling_rate = 44100; 00442 mp4AudioHeader.padding = 0; 00443 mp4AudioHeader.frame_size_in_bytes = 0; 00444 mp4AudioHeader.private_bit = 0; 00445 mp4AudioHeader.channel_mod = STEREO; 00446 mp4AudioHeader.mode_extension = 0; 00447 mp4AudioHeader.copyright = 0; 00448 mp4AudioHeader.original = 0; 00449 mp4AudioHeader.emphasis = NONE_; 00450 00451 memcpy(mp4AudioHeader.mpeg_audio_bitrate_index_table, mpeg_audio_bitrate_index_table, 00452 sizeof(mp4AudioHeader.mpeg_audio_bitrate_index_table)); 00453 memcpy(mp4AudioHeader.mpeg_audio_sampling_rate_index_table, mpeg_audio_sampling_rate_index_table, 00454 sizeof(mp4AudioHeader.mpeg_audio_sampling_rate_index_table)); 00455 } 00456 00457 00458 void MP4audioDecoder::nextByte() { 00459 mcByte = mpcData[miCurrentNoBytes]; 00460 miCurrentBit = 7; 00461 miCurrentNoBytes++; 00462 if (miCurrentNoBytes > miSize) 00463 mbEOD = true; 00464 } 00465 00466 00467 bool MP4audioDecoder::bytealigned() { 00468 if (miCurrentBit == 7) 00469 return true; 00470 else { 00471 nextByte(); 00472 return true; 00473 } 00474 } 00475 00476 00477 int MP4audioDecoder::getBit() { 00478 int ret = 0; 00479 if (miCurrentBit == -1) 00480 nextByte(); 00481 ret = (mcByte >> miCurrentBit) & 0x01; 00482 miCurrentBit--; 00483 if (mbEOD == false) 00484 return ret; 00485 00486 else 00487 return EOF; 00488 } 00489 00490 00491 int MP4audioDecoder::getBits(int iNumber) { 00492 int ret = 0; 00493 int i; 00494 for (i = 0; i < iNumber; i++) 00495 ret = (ret << 1) | getBit(); 00496 if (mbEOD == false) 00497 return ret; 00498 00499 else 00500 return EOF; 00501 } 00502 00503 00504 int MP4audioDecoder::nextBits(int noBits) { 00505 int ret = 0; 00506 bookmark(); 00507 ret = getBits(32); 00508 restore(); 00509 if (mbEOD == false) 00510 return ret; 00511 00512 else 00513 return EOF; 00514 } 00515 00516 00517 void MP4audioDecoder::nextStartCode() { 00518 while (!bytealigned()) 00519 getBit(); 00520 } 00521 00522 00523 void MP4audioDecoder::loadQuantizerMatrix(unsigned int puiMatrix[]) { 00524 int i; // row counter 00525 int j; // column counter 00526 int lastValue = 0; 00527 bool readLastValue = false; 00528 for (i = 0; i < 8; i++) { 00529 for (j = 0; j < 8; j++) { 00530 if (!readLastValue) { 00531 puiMatrix[j + i * 8] = getBits(8); 00532 if (puiMatrix[j + i * 8] == 0) 00533 readLastValue = true; 00534 else 00535 lastValue = puiMatrix[j + i * 8]; 00536 } 00537 else 00538 puiMatrix[j + i * 8] = lastValue; 00539 } 00540 } 00541 } 00542 00543 00544 void MP4audioDecoder::readMarkerBitFunc(int iLine) { 00545 if (getBit() != 1) { 00546 dprintf_small("Marker bit invalid at line %d\n", iLine); 00547 } 00548 } 00549 00550 00551 void MP4audioDecoder::bookmark() { 00552 miBookmarkedCurrentBit = miCurrentBit; 00553 miBookmarkedByte = mcByte; 00554 miBookmarkedCurrentNoBytes = miCurrentNoBytes; 00555 } 00556 00557 00558 void MP4audioDecoder::restore() { 00559 miCurrentBit = miBookmarkedCurrentBit; 00560 mcByte = miBookmarkedByte; 00561 miCurrentNoBytes = miBookmarkedCurrentNoBytes; 00562 } 00563 00564 00565 void MP4audioDecoder::setCoderType(MP4audioDecoder::eCoderType coderType) { 00566 meCoder = coderType; 00567 } 00568 00569 00570 void MP4audioDecoder::DecoderClose(char **pDecoderHandle) { 00571 switch (meCoder) { 00572 case (MP4audioDecoder::XVID): 00573 #ifndef WINCE 00574 case (MP4audioDecoder::FFMPEG): 00575 FFMPEGaudioDecoderClose(pDecoderHandle); 00576 break; 00577 #endif 00578 default: 00579 dprintf_err("Invalid Coder selected\n"); 00580 break; 00581 } 00582 } 00583 00584 00585 void MP4audioDecoder::DecoderInit(char **pDecoderHandle, CodecID codecID) { 00586 switch (meCoder) { 00587 case (MP4audioDecoder::XVID): 00588 #ifndef WINCE 00589 case (MP4audioDecoder::FFMPEG): 00590 FFMPEGaudioDecoderInit(pDecoderHandle, codecID); 00591 break; 00592 #endif 00593 default: 00594 dprintf_err("Invalid Coder selected\n"); 00595 } 00596 } 00597 00598 00599 int MP4audioDecoder::DecodeFrame(char *pDecoderHandle, char *pCompressedFrame, 00600 int iCFSize, char* PCMFrame, u32 *mFrameSize) { 00601 int ret = 0; 00602 switch (meCoder) { 00603 case (MP4audioDecoder::XVID): 00604 #ifndef WINCE 00605 case (MP4audioDecoder::FFMPEG): 00606 dprintf_full("Audio Decoding : using FFMPEG\n"); 00607 ret = FFMPEGaudioDecodeFrame(pDecoderHandle, pCompressedFrame, iCFSize, 00608 PCMFrame, (int*)mFrameSize); 00609 break; 00610 #endif 00611 default: 00612 dprintf_err("Invalid Coder selected\n"); 00613 } 00614 return ret; 00615 } 00616