📄 mp4audec.c
字号:
bitBuf = BsAllocBuffer(frameMaxNumBit); if ((sampleBuf=(float**)malloc(numChannel*sizeof(float*)))==NULL) CommonExit(1,"Decode: memory allocation error"); for (ch=0; ch<numChannel; ch++) if ((sampleBuf[ch]=(float*)malloc(frameMaxNumSample*sizeof(float))) ==NULL) CommonExit(1,"Decode: memory allocation error"); /* for speed control (PICOLA) */ if(NULL==(PicolaBuffer=(float **)calloc(numChannel, sizeof(float)))) { CommonExit(1, "Decode: memory allocation error"); } for(ch=0;ch<numChannel;ch++) { if(NULL==(*(PicolaBuffer+ch)=(float *)calloc(frameMaxNumSample, sizeof(float)))) { CommonExit(1, "Decode: memory allocation error"); } } } /* open audio file */ /* (sample format: 16 bit twos complement, uniform quantisation) */ audioFile = AudioOpenWrite(audioFileName,audioFileFormat, numChannel,fSample); if (audioFile==NULL) CommonExit(1,"Decode: error opening audio file %s " "(maybe unknown format \"%s\")", audioFileName,audioFileFormat); /* seek to beginning of first frame (with delay compensation) */ AudioSeek(audioFile,-delayNumSample); } /* if (!testBitStream) */ /* process bit stream frame by frame */ frame = -decStartFrame; totNumSample = 0; totPadNumBit = 0; if(strstr(decPara,"-tvq_sys") != NULL) { /*frameAvailNumBit = frameNumBit - (18*(frameNumBit/(250*8) +1) +7);*/ frameAvailNumBit = frameNumBit; /* T.Ishikawa 980624 */ if (mainDebugLevel >= 3) { printf("frameAvailNumBit %5d , frameNumBit %5d\n", frameAvailNumBit,frameNumBit); } } else { frameAvailNumBit = frameNumBit+bitReservInit; } usedNumBit=0; if (strstr(decPara, "-mp4ff") != NULL || strstr(decPara, "-celp_sys") != NULL || strstr(decPara, "-hvxc_sys") != NULL) { /* AI 990616 */ flexMuxDec(bitStream, numChannel, &frameData, &tfData, &lpcData, &nttData, hvxcData, /* AI 990616 */ audioFile, hFault ); } else {#ifdef FHG_DEBUGPLOT plotInit(frame);#endif /* for constant rate decoders the following applies: to decode one frame we need frameNumBit ; for variale rate decoder (eg. AAC) the following applies: to decode one frame we don't exactly know the minimum number of bit a frame can have, but you in most cases you need at least 8 bit (eg. for the header)*/ decodeNext = (!BsEof ( bitStream, (aacEOF) ? 8 : frameAvailNumBit - 1 ) && (decNumFrame < 0 || frame < decNumFrame)) ; if (decodeNext) { /* initialize buffer with the number of bits needed to decode one frame. for constant rate decoders the following applies: frameAvailNumBit = frameNumBit (which is the average number of bits per frame including overhead for transport) for variale rate decoder (eg. AAC) the following applies: the number of bits for the next frame is not known but the maximum size is: frameAvailNumBit = frameNumBit + bitReservSize in case of ADIF header the maximum size actually is: frameAvailNumBit = frameNumBit + bitReservInit (but this is currently not taken into acount) in case of mpeg4 file format maximum size could be calculated from the timestamps */ BsGetBuffer(bitStream,bitBuf,frameAvailNumBit); } refillBits=0; while ( decodeNext) { decodeFrame = !testBitStream && frame >= 0; if (decodeFrame) { { /* what is this??? HP 990422 */ /*keep the frame buffer full if end of bitstream is not reached */ int statusBitstream; if ( ! EOFreached ) { statusBitstream = BsGetBufferAppend (bitStream, bitBuf, 1, refillBits); if ( statusBitstream == -1 ) { EOFreached = 1; } else { if ( statusBitstream !=0 ) { CommonExit(1,"Decode: error reading bit stream data"); } } } } /* AI 990528 */ /* if number of bits in bitBuf is less than minimum bits to decode, break */ /* replaced BsGetBufferFullness(bitBuf) by bitBuf->numBit ... */ /* ... although bitBuf struct def shouldn't be used here HP 990722 */ if (bitBuf->numBit < frameMinNumBit) break; if (mainDebugLevel == 1) { fprintf(stderr,"\rframe %4d ",frame); fflush(stderr); } if (mainDebugLevel >= 2 && mainDebugLevel <= 3) { printf("\rframe %4d ",frame); fflush(stdout); } if (mainDebugLevel > 3) printf("frame %4d\n",frame); if (mainDebugLevel >= 5) printf("frameAvailNumBit=%d\n",frameAvailNumBit); /* AI 990528 */ /* decode one frame */ switch ( mode ) { case MODE_PAR: if(SpeedControlMode) DecParFrame(bitBuf,sampleBuf,&usedNumBit,&frameBufNumSample, 1,pitchFact ); else DecParFrame(bitBuf,sampleBuf,&usedNumBit,&frameBufNumSample, speedFact,pitchFact ); break; case MODE_HVXC: if(SpeedControlMode) DecHvxcFrame(hvxcData, bitBuf,sampleBuf, &usedNumBit, &frameBufNumSample, 1, pitchFact ); else DecHvxcFrame(hvxcData, bitBuf,sampleBuf, &usedNumBit, &frameBufNumSample, speedFact, pitchFact ); break; case MODE_LPC: DecLpcFrame(bitBuf,sampleBuf,&usedNumBit); frameBufNumSample = frameMaxNumSample; break; case MODE_G729: DecG729Frame(bitBuf,sampleBuf,&usedNumBit,0); frameBufNumSample = frameMaxNumSample; break; case MODE_G723: DecG723Frame(bitBuf,sampleBuf,&usedNumBit); frameBufNumSample = frameMaxNumSample; break; case MODE_TF: DecTfFrame ( bitBuf, sampleBuf, &usedNumBit, numChannel, mainDebugLevel, hFault, hResilience, hVm, hHcrSpecData, hHcrInfo, (frameData.od == NULL ? NULL : &frameData), &tfData, &nttData, hEpInfo, hConcealment); frameBufNumSample = frameMaxNumSample; break; } } else /* if (decodeFrame) */ usedNumBit = frameNumBit; if (mainDebugLevel >= 5) { if (decodeFrame) printf("frameBufNumSample=%d\n",frameBufNumSample); printf("usedNumBit=%d\n",usedNumBit); } if (usedNumBit > frameAvailNumBit) CommonWarning("Decode: more bits used than available in frame+buffer"); if ((usedNumBit != (bitBuf->size - bitBuf->numBit)) && !EOFreached ){ CommonWarning ("\nsome bits are missing!!\n"); usedNumBit = (bitBuf->size - bitBuf->numBit); } /* byte alignment */#if 0 alignBits = 8 - usedNumBit - ((usedNumBit/8)*8); if (alignBits<8){ BsBitStream * stream = BsOpenBufferRead(bitBuf); BsGetSkip(stream,alignBits); usedNumBit += alignBits; BsCloseRemove(stream,1); }#endif refillBits=usedNumBit; #if 0 /* at this level padding bits should not occur: for flexmux or mp4 file format, the padding bits are at the end of an access unit and are handled therefor by the flexmux or mp4 file format framework for raw AAC bitstreams padding bits only occur in 'fillelements' and are decoded in the aac decoder itself. for all other coders padding should not occure in raw bitstreams because padding makes only sense in transprort streams. but neither raw bitstreams nor flexmux or file format bitstremas are actually transport streams. */ /* check for padding bits */ if (frameAvailNumBit-usedNumBit-frameNumBit+frameMinNumBit > bitReservSize) { padNumBit = frameAvailNumBit-usedNumBit-frameNumBit+frameMinNumBit -bitReservSize; if (mainDebugLevel >= 5) printf("padNumBit=%d\n",padNumBit); /* T.Ishikawa 980526 */ if(strstr(decPara,"-tvq_sys")==NULL) { BsBitStream * stream = BsOpenBufferRead(bitBuf); BsGetSkip(stream,padNumBit); usedNumBit += padNumBit; BsCloseRemove(stream,1); } totPadNumBit += padNumBit; }#endif /* advance in bit stream */ if ( frameDecNumBit ) { usedNumBit = frameNumBit; }#if 0 /* we need this HP 990422 */ /* well, actually a decoder should read the bits it decodes from bitBuf. this should be done in the following way: open a bitstream from the bitBuf: decBitStream = BsOpenBufferRead(bitBuf); read the bits with BsGetBit( decBitStream ,&ultmp, 1 ); and finally if decoding of the frame is finished close the bitStream with BsCloseRemove ( decBitStream ,1); so that the bits that are allready read are really removed from the bitBuf. then its no longer needed to call BsGetSkip(bitStream,usedNumBit) here. This model is more similar to the System Decoder Model in Mpeg4 systems, where the Decoder Buffer is actually a FIFO. */ if (BsGetSkip(bitStream,usedNumBit)) CommonExit(1,"Decode: error advancing in bit stream data");#endif frameAvailNumBit -= usedNumBit; /* variable bit rate: don't exceed bit reservoir size */ if (frameAvailNumBit > bitReservSize) frameAvailNumBit = bitReservSize; frameAvailNumBit += frameNumBit; if (decodeFrame) { if(SpeedControlMode) { PicolaDecodeFlag = 0; PicolaNumInputSample = frameBufNumSample; totNumSample += frameBufNumSample; while(0==PicolaDecodeFlag) { mod_picola(sampleBuf[0], PicolaNumInputSample, PicolaBuffer[0], speedFact, &PicolaDecodeFlag, &PicolaOutputFlag); PicolaNumInputSample = 0; if(PicolaOutputFlag) { /* write audio file */ numSample = frameMaxNumSample; AudioWriteData(audioFile,PicolaBuffer,numSample); PicolaNumSample += numSample;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -