📄 mp4auenc.c
字号:
CommonWarning("Encode: error opening audio file %s",audioFileName); return 1; } fclose(tmpFile); /* open audio file */ audioFile = AudioOpenRead(audioFileName,&numChannel,&fSample, &fileNumSample); if (audioFile==NULL) CommonExit(1,"Encode: error opening audio file %s " "(maybe unknown format)", audioFileName); startSample = (long)(regionStart*fSample+0.5); if (regionDurat >= 0) encNumSample = (long)(regionDurat*fSample+0.5); else if (fileNumSample == 0) encNumSample = -1; else encNumSample = max(0,fileNumSample-startSample); /* init encoder */ bitHeader = BsAllocBuffer(BITHEADERBUFSIZE); bitRateLong = (long)(bitRate+0.5); fSampleLong = (long)(fSample+0.5); numChannelBS = (numChannelOut==0) ? numChannel : numChannelOut; fSampleLongBS = (fSampleOut==0) ? fSampleLong : (long)(fSampleOut+0.5); if ((strstr(encPara, "-aac_sys") != NULL)|| (strstr(encPara, "-aac_sys_bsac") != NULL)|| (strstr(encPara, "-tvq_sys") != NULL)|| (strstr(encPara, "-celp_sys") != NULL)|| (strstr(encPara, "-hvxc_sys") != NULL)|| /* AI 990616 */ (strstr(encPara, "-mp4ff") != NULL)) { frameData= &encFrameData; frameData->od= &objDescr; } switch (mode) { case MODE_PAR: EncParInit(numChannel,fSample,bitRate,encPara, &frameNumSample,&delayNumSample,bitHeader); break; case MODE_HVXC: EncHvxcInit(numChannel,fSample,bitRate,encPara, &frameNumSample,&delayNumSample,bitHeader, frameData); /* AI 990616 */ break; case MODE_LPC: EncLpcInit(numChannel,fSample,bitRate,encPara, varBitRate, &frameNumSample,&delayNumSample,bitHeader,frameData);#if 0 /* what is this??? HP 990120 990422 */ frameNumBit = (int)(bitRate*frameNumSample/fSample+0.5); bitRate = (float) (frameNumBit) * fSample / (float) (frameNumSample); bitRateLong = (long) (bitRate + 0.5F);#endif break; case MODE_G729:#if 0 /* what is this??? 990422 */ bitRate=8000; bitRateLong = (long)bitRate;#endif if (fSample == 48000){ lowpassFilt=initFirLowPass(48000/4000,120) ; downsamplFac=6; } EncG729Init(numChannel,fSample/downsamplFac,bitRate,encPara, &frameNumSample,&delayNumSample,bitHeader); frameNumSample=frameNumSample*downsamplFac; break; case MODE_G723:#if 0 /* what is this??? 990422 */ bitRate=6400; bitRateLong = (long) bitRate; #endif if (fSample == 48000){ lowpassFilt=initFirLowPass(48000/4000,120) ; downsamplFac=6; } EncG723Init(numChannel,fSample/downsamplFac,bitRate,encPara,encNumSample, &frameNumSample,&delayNumSample,bitHeader); frameNumSample=frameNumSample*downsamplFac; break; case MODE_TF: EncTfInit(numChannel,fSample,bitRate,encPara,quantDebugLevel, &frameNumSample,&delayNumSample,bitHeader,frameData, &tfData, &nttData); break; } frameNumBit = (int)(bitRate*frameNumSample/fSample+0.5); /* variable bit rate: minimum 8 bit/frame (1 byte) */ /* to allow end_of_bitstream detection in decoder */ frameMinNumBit = varBitRate ? 8 : frameNumBit; frameMaxNumBit = frameNumBit+bitReservSize; /* AI 990616 */ if ((strstr(encPara, "-hvxc_sys") != NULL)) { frameMaxNumBit += 24; /* for flexmux header */ /* this idea is not good, but necessary for bitBuf allocation */ } if ((strstr(encPara, "-celp_sys") != NULL)) { celp_sys_align = 8 - (frameNumBit % 8); if (celp_sys_align == 8) celp_sys_align = 0; frameMaxNumBit += 24+celp_sys_align;/* for flexmux header and byte align*/ } if (mainDebugLevel >= 3) { printf("mode=%d\n",mode); printf("fSample=%.3f Hz (int=%ld)\n",fSample,fSampleLong); printf("bitRate=%.3f bit/sec (int=%ld)\n",bitRate,bitRateLong); printf("bitReservSize=%d bit (%.6f sec)\n", bitReservSize,bitReservSize/bitRate); printf("bitReservInit=%d bit\n",bitReservInit); printf("frameNumSample=%d (%.6f sec/frame)\n", frameNumSample,frameNumSample/fSample); printf("delayNumSample=%d (%.6f sec)\n", delayNumSample,delayNumSample/fSample); printf("frameNumBit=%d\n",frameNumBit); printf("frameMinNumBit=%d\n",frameMinNumBit); printf("frameMaxNumBit=%d\n",frameMaxNumBit); printf("bitHeaderNumBit=%ld\n",BsBufferNumBit(bitHeader)); printf("fileNumSample=%ld (%.3f sec %.3f frames)\n", fileNumSample,fileNumSample/fSample, fileNumSample/(float)frameNumSample); printf("startSample=%ld\n",startSample); printf("encNumSample=%ld (%.3f sec %.3f frames)\n", encNumSample,encNumSample/fSample, encNumSample/(float)frameNumSample); } /* allocate buffers */ bitBuf = BsAllocBuffer(frameMaxNumBit); if ((sampleBuf=(float**)malloc(numChannel*sizeof(float*)))==NULL) CommonExit(1,"Encode: memory allocation error"); for (ch=0; ch<numChannel; ch++) if ((sampleBuf[ch]=(float*)malloc(frameNumSample*sizeof(float)))==NULL) CommonExit(1,"Encode: memory allocation error"); if ((tmpBuff=(float*)malloc(frameNumSample*sizeof(float)))==NULL) CommonExit(1,"Encode: memory allocation error"); /* if we have aac_raw , we should treat it, as with no header, if we have scaleable aac the header is will be written in aacScaleableEncode*/ if ((strstr(encPara, "-aac_raw") != NULL)|| (strstr(encPara, "-aac_sca") != NULL) ) noHeader=1; /* T.Ishikawa 980525 */ if ((strstr(encPara, "-aac_sys") != NULL) || (strstr(encPara, "-aac_sys_bsac") != NULL) || (strstr(encPara, "-tvq_sys") != NULL) || (strstr(encPara, "-celp_sys") != NULL) || (strstr(encPara, "-hvxc_sys") != NULL) || /* AI 990616 */ (strstr(encPara, "-mp4ff") != NULL)) noHeader=0; /* open bit stream file */ if (!noHeader) bitStream = BsOpenFileWrite(bitFileName,magicString,info); else bitStream = BsOpenFileWrite(bitFileName,NULL, NULL); if (bitStream==NULL) CommonExit(1,"Encode: error opening bit stream file %s",bitFileName); /* write bit stream header */ if ((strstr(encPara, "-aac_sys") == NULL) && (strstr(encPara, "-aac_sys_bsac") == NULL) && (strstr(encPara,"-tvq_sys") == NULL) && (strstr(encPara,"-celp_sys") == NULL) && (strstr(encPara,"-hvxc_sys") == NULL) && /* AI 990616 */ (strstr(encPara, "-mp4ff") == NULL)) { if (!noHeader) if (BsPutBit(bitStream,MP4_BS_VERSION,16) || BsPutBit(bitStream,numChannelBS,8) || BsPutBit(bitStream,fSampleLongBS,32) || BsPutBit(bitStream,bitRateLong,32) || BsPutBit(bitStream,frameNumBit,16) || BsPutBit(bitStream,frameMinNumBit,16) || BsPutBit(bitStream,bitReservSize,16) || BsPutBit(bitStream,bitReservInit,16) || BsPutBit(bitStream,mode,8) || BsPutBit(bitStream,BsBufferNumBit(bitHeader),16)) CommonExit(1,"Encode: error writing bit stream header (frame)"); if (BsPutBuffer(bitStream,bitHeader)) CommonExit(1,"Encode: error writing bit stream header (core)"); BsFreeBuffer(bitHeader); headerNumBit = BsCurrentBit(bitStream); } else { /* write object descriptor length */ int length= BsBufferNumBit(bitHeader)/8; int align = 8 - BsBufferNumBit(bitHeader) % 8; if (align == 8) align = 0; if (align != 0) { length += 1; } BsPutBit(bitStream,length,32); if (BsPutBuffer(bitStream,bitHeader)) CommonExit(1,"Encode: error writing bit stream header (core)"); BsPutBit(bitStream,0,align); BsFreeBuffer(bitHeader); headerNumBit = BsCurrentBit(bitStream); } if (mainDebugLevel >= 3) printf("headerNumBit=%d\n",headerNumBit); /* num frames to start up encoder due to delay compensation */ startupNumFrame = (delayNumSample+frameNumSample-1)/frameNumSample; /* seek to beginning of first (startup) frame (with delay compensation) */ AudioSeek(audioFile, startSample+delayNumSample-startupNumFrame*frameNumSample); if (mainDebugLevel >= 3) printf("startupNumFrame=%d\n",startupNumFrame); /* process audio file frame by frame */ frame = -startupNumFrame; totNumSample = 0; totPadNumBit = 0; frameAvailNumBit = bitReservInit; minUsedNumBit = minPadNumBit = minReservNumBit = frameMaxNumBit; maxUsedNumBit = maxPadNumBit = maxReservNumBit = 0; totReservNumBit = 0; do { 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); /* check for startup frame */ startupFrame = frame < 0; /* read audio file */ numSample = AudioReadData(audioFile,sampleBuf,frameNumSample); totNumSample += numSample; if (numSample != frameNumSample && encNumSample == -1) encNumSample = totNumSample; /* encode one frame */ if (!startupFrame) { /* variable bit rate: don't exceed bit reservoir size */ if (frameAvailNumBit > bitReservSize) frameAvailNumBit = bitReservSize; if (frameData!=NULL) { /* T.Ishikawa 980701 */ if(strstr(encPara,"-tvq_sys")) { float Fflexnum; int flexnum; Fflexnum = (float)(frameNumBit)/(float)(254*8)+1.0; flexnum = (int)(frameNumBit/(254*8)+1); /*fprintf(stderr," %f %d\n",Fflexnum,flexnum);*/ if(Fflexnum>(float)flexnum) { frameAvailNumBit += frameNumBit+ 24*(flexnum+1); } else { frameAvailNumBit += frameNumBit+ 24*flexnum; } /*fprintf(stderr,"frameAvailNumBit %d , frameNumBit %d\n", frameAvailNumBit,frameNumBit);*/ } else if (strstr(encPara, "-hvxc_sys")) { /* AI 990616 */ frameAvailNumBit = frameNumBit + 24; /* there must be better way ... */ } else if (strstr(encPara, "-celp_sys")) { frameAvailNumBit += frameNumBit + 24 + celp_sys_align; } else { frameAvailNumBit += frameNumBit-(18* (frameNumBit/(250*8)+1) + 7); /* minus flexmux overhead: 18 bits per flexmux packet plus max 7 align bits*/ } } else { frameAvailNumBit += frameNumBit; } if (mainDebugLevel >= 5) printf("frameAvailNumBit=%d\n",frameAvailNumBit); } switch (mode) { case MODE_PAR: EncParFrame(sampleBuf, startupFrame ? (BsBitBuffer*)NULL : bitBuf, startupFrame ? 0 : frameAvailNumBit, frameNumBit,frameMaxNumBit); break; case MODE_HVXC: EncHvxcFrame(sampleBuf, startupFrame ? (BsBitBuffer*)NULL : bitBuf, startupFrame ? 0 : frameAvailNumBit, frameNumBit,frameMaxNumBit, frameData); /* AI 990616 */ break; case MODE_LPC: EncLpcFrame(sampleBuf, startupFrame ? (BsBitBuffer*)NULL : bitBuf, startupFrame ? 0 : frameAvailNumBit, frameNumBit,frameMaxNumBit); break; case MODE_G729: /* I'd suggest just to use frameNumSample! */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -