[Libav-user] Need help for decoding AAC with ffmpeg and separated AVCodecContex

lingshan kong konglingshan at duoku.com
Wed Oct 31 03:10:29 CET 2012

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
First  i create AVCodec, AVCOdecContext and encode PCM data to AAC packet:


    AVCodec *encode = avcodec_find_encoder(CODEC_ID_AAC);


    if (!encode) 


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




    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");



    char buffer[2048];

    int size = 0;


    size = avcodec_encode_audio(encodeContext, (uint8_t*)buffer, 2048,

    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 *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;




    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");



    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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20121031/bd3fe5bd/attachment.html>

More information about the Libav-user mailing list