[FFmpeg-user] libavcodec aac encoding with AV_SAMPLE_FMT_FLT

Harald Jordan harald.jordan at redstream.at
Mon Aug 27 09:19:16 CEST 2012

Hey there!

I am using libavcodec within a cpp project. Basically the program runs 
without any problem, as long as i can use AV_SAMPLE_FMT_S16 for the audio 
encoding part.
Now when it comes to aac, the aac codec only supports the FLT Sample format 
and here comes the problem: at S16, the frame_size of the audio codec is 
automatically set to bitrate/framerate.
At 8000 Samples/sec input this means for S16 a framesize of 320. (dec)
The initial frame_size for AV_SAMPLE_FMT_FLT a framesize of 1024.
I tried setting the framesize to 320, but this didnt work. So the question 
is: why is the initial framesize of FMT_FLT 1024, i mean 1024 has nothing to 
do with my Sample Rate of 8000, nor with any other known sample rate usually 
After Resampling the S16 to FLT, the number of samples did not increase, so 
what is the catch here?

Here some code:

void put_audio_frame(char* audioframe,int samplesavaliable){

//Audiobuffer is recieved as a char*, it is a multiple of bitrate/framerate 
(multiple audio frames within one call

samples = (int16_t *)audioframe;

//do resampling
ReSampleContext *ctx = av_audio_resample_init(1, 1, 8000, 8000, 
    AV_SAMPLE_FMT_S16, 0, 0, 0, 0);
    samplesavaliable = audio_resample(ctx, (short *)resamples, (short 
*)samples,  samplesavaliable*2);

while(samplesavaliable >= c->frame_size){//dont write frame if it is smaller 
than codec frame(TODO: padding?)
    pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, 
    /* write the compressed frame in the media file */

if (av_interleaved_write_frame(oc, &pkt) != 0) {
            fprintf(stderr, "Error while writing audio frame\n");

samplesavaliable -= c->frame_size;
resamples = (char*)samples + c->frame_size;


More information about the ffmpeg-user mailing list