繁体   English   中英

如何使用C ++一次解码一个AAC帧?

[英]How to decode one AAC frame at a time using C++?

我想连续解码AAC帧流,一次解码一帧。

我浏览了ffmpeg示例(正确答案不一定需要使用ffmpeg),而我仅找到使用完整AAC文件和批处理算法的示例。 但是我想解码一个连续的AAC流。 我怎样才能做到这一点?

更新:按照注释并在android上使用ffmpeg将AAC解码为PCM ,我能够使用ffmpeg解码为PCM,但是输出的声音非常嘈杂。 当为每个AAC帧调用此方法时,我在做什么错:

...
/*loop that receives frame in buffer*/
 while(1){
   /*receive frame*/
   input = receive_one_buffer();

   /*decode frame*/
   decodeBuffer(input,strlen(input),Outfile);
 }

...

/*decode frame*/
void decodeBuffer(char * input, int numBytes, ofstream& Outfile) {
    /*"input" contains one AAC-LC frame*/
    //copy bytes from buffer
    uint8_t inputBytes[numBytes + FF_INPUT_BUFFER_PADDING_SIZE];
    memset(inputBytes, 0, numBytes + FF_INPUT_BUFFER_PADDING_SIZE);
    memcpy(inputBytes, input, numBytes);

    av_register_all();

    AVCodec *codec = avcodec_find_decoder(CODEC_ID_AAC);

    AVCodecContext *avCtx = avcodec_alloc_context();
    avCtx->channels = 1;
    avCtx->sample_rate = 44100;

    //the input buffer
    AVPacket avPacket;
    av_init_packet(&avPacket);

    avPacket.size = numBytes; //input buffer size
    avPacket.data = inputBytes; // the input buffer

    int outSize;
    int len;
    uint8_t *outbuf = static_cast<uint8_t *>(malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE));

    while (avPacket.size > 0) {
        outSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
        len = avcodec_decode_audio3(avCtx, (short *) outbuf, &outSize,
                &avPacket);

    Outfile.write((char*)outbuf, outSize);

        avPacket.size -= len;
        avPacket.data += len;
    }

    av_free_packet(&avPacket);
    avcodec_close(avCtx);
    //av_free(avCtx);

    return;
}

您必须在随后的解码调用之间保持解码器处于活动状态。 AAC解码器必须解码先前的缓冲区才能正确地“启动”。

请检查详细信息:

https://developer.apple.com/library/mac/technotes/tn2258/_index.html

以下代码假定“ ReceiveBuffer”函数恰好返回一个完整的AAC访问单元。

(顺便说一句:您不能在二进制缓冲区上使用strlen;您将获得到第一个零的距离,而不是缓冲区长度)

#include <iostream>
#include <fstream>

#include "libavcodec\avcodec.h"
#include "libavformat\avformat.h"
#include "libavdevice\avdevice.h"
#include "libavfilter\avfilter.h"

AVCodecContext * CreateContext()
{
    av_register_all();

    AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_AAC);

    AVCodecContext *avCtx = avcodec_alloc_context3(codec);

    return avCtx;
}

int32_t DecodeBuffer
(
    std::ostream   & output,
    uint8_t        * pInput,
    uint32_t         cbInputSize,
    AVCodecContext * pAVContext
)
{
    int32_t cbDecoded = 0;

    //the input buffer
    AVPacket avPacket;
    av_init_packet(&avPacket);

    avPacket.size = cbInputSize; //input buffer size
    avPacket.data = pInput; // the input bufferra

    AVFrame * pDecodedFrame = av_frame_alloc();

    int nGotFrame = 0;

    cbDecoded = avcodec_decode_audio4(    pAVContext,
                                          pDecodedFrame,
                                        & nGotFrame,
                                        & avPacket);

    int data_size = av_samples_get_buffer_size( NULL,
                                                pAVContext->channels,
                                                pDecodedFrame->nb_samples,
                                                pAVContext->sample_fmt,
                                                1);

    output.write((const char*)pDecodedFrame->data[0],data_size);


    av_frame_free(&pDecodedFrame);

    return cbDecoded;
}


uint8_t * ReceiveBuffer( uint32_t * cbBufferSize)
{
    // TODO implement

    return NULL;
}

int main
(
    int argc,
    char *argv[]
)
{
    int nResult = 0;

    AVCodecContext * pAVContext = CreateContext();

    std::ofstream myOutputFile("audio.pcm",std::ios::binary);

    while(1)
    {
        uint32_t cbBufferSize = 0;
        uint8_t *pCompressedAudio = ReceiveBuffer( &cbBufferSize);

        if(cbBufferSize && pCompressedAudio)
        {
            DecodeBuffer(   myOutputFile,
                            pCompressedAudio,
                            cbBufferSize,
                            pAVContext);
        }
        else
        {
            break;
        }
    }

    avcodec_close(pAVContext);
    av_free(pAVContext);

    return nResult;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM