[Libav-user] aac encoder in real time scenario

Gerard C.L. gerardcl at gmail.com
Thu Mar 14 13:25:58 CET 2013


Hi all,

I'm developing an AAC encoder in a real time environment.

The scene is:
- Capture format -> PCM: 48kHz, stereo, 16b/sample.  at 25fps  -> so, per
frame, 7680Bytes have to be encoded.

The first problem become when I realised that the encoder works on fixed
chunk sizes (in this case, for the audio configuration, the size is
4096Bytes per chunk). So, working like a file encoder, I was only encoding
4096bytes of the 7680 per frame.
The solution was implementing FIFOs, using the av_fifo_.. methods. So now,
I can hear the entire captured sound per frame, but I hear some garbage and
I don't know if it's because of the encoder or how I work with the fifo or
if I have conceptual errors in my mind. To note that I'm playing the sound
after saving it to a file, could it be also the problem?

I'm copying the piece of code I've implemented right now, I'd love if some
one gets the error... I'm so noob...

-----------------------------------8<------------------------------------------------------------------
int audio_avcodec_encode(struct audio_avcodec_encode_state *aavces,
unsigned char *inbuf, unsigned char *outbuf, int inbufsize) {
    AVPacket pkt;
    int frameBytes;
    int outsize = 0;
    int packetSize = 0;
    int ret;
    int nfifoBytes;
    int encBytes = 0;
    int sizeTmp = 0;

    frameBytes = aavces->c->frame_size * aavces->c->channels * 2;
    av_fifo_realloc2(aavces->fifo_buf,av_fifo_size(aavces->fifo_buf) +
inbufsize);

    // Put the raw audio samples into the FIFO.
    ret = av_fifo_generic_write(aavces->fifo_buf, /*(int8_t*)*/inbuf,
inbufsize, NULL );

    printf("\n[avcodec encode] raw buffer intput size: %d ; fifo size:
%d",inbufsize, ret);

    //encoding each frameByte block
    while ((ret = av_fifo_size(aavces->fifo_buf)) >= frameBytes) {
        ret = av_fifo_generic_read(aavces->fifo_buf,
aavces->fifo_outbuf,frameBytes, NULL );

        av_init_packet(&pkt);

        pkt.size = avcodec_encode_audio(aavces->c,
aavces->outbuf,aavces->outbuf_size, (int16_t*) aavces->fifo_outbuf);

        if (pkt.size < 0) {
            printf("FFmpeg : ERROR - Can't encode audio frame.");
        }
        // Rescale from the codec time_base to the AVStream time_base.
        if (aavces->c->coded_frame && aavces->c->coded_frame->pts !=
(int64_t) (AV_NOPTS_VALUE ))
            pkt.pts =
av_rescale_q(aavces->c->coded_frame->pts,aavces->c->time_base,
aavces->c->time_base);

        printf("\nFFmpeg : (%d) Writing audio frame with PTS:
%lld.",aavces->c->frame_number, pkt.pts);
        printf("\n[avcodec - audio - encode] Encoder returned %d bytes of
data",pkt.size);

        pkt.data = aavces->outbuf;
        pkt.flags |= AV_PKT_FLAG_KEY;

        memcpy(outbuf, pkt.data, pkt.size);
    }

    // any bytes left in audio FIFO to encode?
    nfifoBytes = av_fifo_size(aavces->fifo_buf);

    printf("\n[avcodec encode] raw buffer intput size: %d", nfifoBytes);

    if (nfifoBytes > 0) {
        memset(aavces->fifo_outbuf, 0, frameBytes);
        if (aavces->c->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
            int nFrameSizeTmp = aavces->c->frame_size;
            if (aavces->c->frame_size != 1 &&
(aavces->c->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME))
                aavces->c->frame_size = nfifoBytes / (aavces->c->channels *
2);

            if (av_fifo_generic_read(aavces->fifo_buf,
aavces->fifo_outbuf,nfifoBytes, NULL ) == 0) {
                if (aavces->c->frame_size != 1)
                    encBytes = avcodec_encode_audio(aavces->c,
aavces->outbuf,aavces->outbuf_size,(int16_t*) aavces->fifo_outbuf);
                else
                    encBytes = avcodec_encode_audio(aavces->c,
aavces->outbuf,nfifoBytes, (int16_t*) aavces->fifo_outbuf);
            }
            aavces->c->frame_size = nFrameSizeTmp;// restore the native
frame size
        } else
            printf("\n[audio encoder] codec does not support small frames");
    }

    // Now flush the encoder.
    if (encBytes <= 0){
        encBytes = avcodec_encode_audio(aavces->c,
aavces->outbuf,aavces->outbuf_size, NULL );
        printf("\nFFmpeg : flushing the encoder");
    }
    if (encBytes < 0) {
        printf("\nFFmpeg : ERROR - Can't encode LAST audio frame.");
    }
    av_init_packet(&pkt);

    sizeTmp = pkt.size;

    pkt.size = encBytes;
    pkt.data = aavces->outbuf;
    pkt.flags |= AV_PKT_FLAG_KEY;

    // Rescale from the codec time_base to the AVStream time_base.
    if (aavces->c->coded_frame && aavces->c->coded_frame->pts != (int64_t)
(AV_NOPTS_VALUE ))
        pkt.pts =
av_rescale_q(aavces->c->coded_frame->pts,aavces->c->time_base,
aavces->c->time_base);

    printf("\nFFmpeg : (%d) Writing audio frame with PTS:
%lld.",aavces->c->frame_number, pkt.pts);
    printf("\n[avcodec - audio - encode] Encoder returned %d bytes of
data\n",pkt.size);

    memcpy(outbuf + sizeTmp, pkt.data, pkt.size);

    outsize = sizeTmp + pkt.size;

    return outsize;
}
-------------------------------------------------->8-------------------------------------------------


Then, I'm saving outbuf with outsize per frame encoded.

Any idea of what I'm doing wrong?

Thanks in advance!
--------------------
  Gerard C.L.
--------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130314/69855b5b/attachment.html>


More information about the Libav-user mailing list