[FFmpeg-user] Need help for decoding AAC with ffmpeg and separated AVCodecContext

lingshan kong kongglin at gmail.com
Tue Oct 30 09:24:20 CET 2012


hi
I need some help with decoding AAC stream of audio. I get audio data via
socket. It is neccessary to create AVCodecContext separately, not from
AVFormatContext->streams[...]->codec;
First  i create AVCodec, AVCOdecContext and encode PCM data to AAC packet:
    avcodec_init();

    avcodec_register_all();

    AVCodec *encode = avcodec_find_encoder(CODEC_ID_AAC);

    

    if (!encode) 

    {

        DBLog("codec not found codec\n");

        //exit(1);

    }

    

    AVCodecContext *encodeContext= avcodec_alloc_context();

    

    /* put sample parameters*/

    encodeContext->sample_fmt = AV_SAMPLE_FMT_S16;

    encodeContext->bit_rate = 128000;

    encodeContext->sample_rate = 44100.00;

    encodeContext->channels = 2;

    

    if (avcodec_open(encodeContext, encode) < 0) {

        DBLog("could not open codec\n");

        //exit(1);

    }


    char buffer[2048];

    int size = 0;

            

    size = avcodec_encode_audio(encodeContext, (uint8_t*)buffer, 2048,
(short*)pcm_buffer);



    This step works well, I can encode audio data successfully, then I send
the data in buffer and it's size to remote device via socket, at the other
side, I try to decode audio data to PCM:



    avcodec_init();

    avcodec_register_all();

    AVCodec *decode = avcodec_find_decoder(CODEC_ID_AAC);

    AVCodecContext *decodeContext = avcodec_alloc_context();

    decodeContext->sample_fmt = AV_SAMPLE_FMT_S16;

    decodeContext->bit_rate = 128000;

    decodeContext->sample_rate = 44100.00;

    decodeContext->channels = 2;

    

    av_init_packet(&avpkt);

    ……

    AudioData* data = [receivedAudioDataArray
objectAtIndex:index];//received AAC data is in receivedAudioDataArray

    avpkt.data = (uint8_t*)[data.audioData bytes];//

    avpkt.size = data.bytes;

    

    if (avcodec_open(decodeContext, decode) < 0) {

        DBLog("could not open decodec\n");

        assert(0);

    }


    int len = avcodec_decode_audio3(decodeContext, (short *)pcm_buffer,
&out_size, &avpkt);



    When decode received data to PCM, I always get –1, and get console log:

channel element -1073948900.-1073948920 is not allocated



I have been confused for one week, I want to know if my way is correct or
not? Is something wrong with decodeContext's parameter? How can I decode
audio data correctly? It is very appreciate to have your help, thank you.



----

Best Regards

Kong 






More information about the ffmpeg-user mailing list