[Libav-user] AAC encoding error

Andrey Utkin andrey.krieger.utkin at gmail.com
Sun Apr 14 23:37:04 CEST 2013

Hi Justin,
I have just tried your code. With trivial changes to make it compile
at work fine at my linux installation, it looks as posted below.
I have to say it doesn't fail for me, it runs cleanly and returns
normal retcode 0. Valgrind does not find any failures, too. So i won't
further investigate this, as i am not really into windows.
Good luck investigating your issue.

#include "libavutil/rational.h"
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "libswscale/swscale.h"
#include "libavcodec/avcodec.h"
#include "libswresample/swresample.h"
#include "libavutil/log.h"

// gcc -O0 -g -ggdb justin.c -lavcodec -o justin

 int i, ret, got_output;

 AVCodec *codec;
 AVCodecContext *c;
 AVFrame *frame;
 AVPacket pkt;
 AVDictionary *opts = NULL;
 uint8_t *srcdata[2];

 codec = avcodec_find_encoder_by_name("aac");
 if (!codec)  return 1;
 c = avcodec_alloc_context3(codec);
 if (!c)  return 1;
 c->bit_rate = 32000;
 c->sample_rate    = 22050;
 c->channel_layout = AV_CH_LAYOUT_STEREO;
 c->channels       = av_get_channel_layout_nb_channels(c->channel_layout);
 c->sample_fmt = AV_SAMPLE_FMT_FLTP;

 // open it
 av_dict_set(&opts, "strict", "experimental", 0);
 ret = avcodec_open2(c, codec, &opts);
 if (ret < 0)
  return 1;

 frame = avcodec_alloc_frame();
 if (!frame)
    return 1;
 frame->nb_samples     = c->frame_size;
 frame->format         = c->sample_fmt;
 frame->channel_layout = c->channel_layout;

 srcdata[0] = (uint8_t *)malloc(4*c->frame_size);
 srcdata[1] = (uint8_t *)malloc(4*c->frame_size);
 memset(srcdata[0], 0, 4*c->frame_size);
 memset(srcdata[1], 0, 4*c->frame_size);
 frame->data[0] = srcdata[0];
 frame->data[1] = srcdata[1];

 for(i=0; i<2; i++)

  pkt.data = NULL;    // packet data will be allocated by the encoder
  pkt.size = 0;
  /* encode the samples */
  ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
 return 0;

More information about the Libav-user mailing list