[Libav-user] Choppy dshow audio playback with ffplay

Taha Ansari mtaha.ansari at gmail.com
Mon Dec 24 11:55:17 CET 2012


Hi!

I have a small test application that sends microphone audio over network.
But the audio playback is sometimes very choppy/lossy, and also I initially
need to 'seek' ffplay back to hear audio with minimum latency. I do this in
Windows, using dshow, zeranoe ffmpeg builds, MSVS; and here is custom code
of relevance (output file is in extension .mp2, and packets are sent on
udp. I tried AAC extension as well, but results are somewhat the same):

***********  +  Decoding part:   +  *************
if(this->packet.stream_index == this->audioStream)
    {
        unsigned int samples_size= 0;
        AVCodecContext *c = outputCodecCtxAudio;
        int finalPTS = 0;
        samples = (short *) av_fast_realloc(samples, &samples_size,
FFMAX(packet.size, AVCODEC_MAX_AUDIO_FRAME_SIZE));
        finalPTS =  packet.pts;
        audiobufsize = AVCODEC_MAX_AUDIO_FRAME_SIZE*2;
        avcodec_decode_audio3(pCodecCtxAudio, samples, &audiobufsize,
&packet);


        if(pCodecCtxAudio->sample_rate != c->sample_rate ||
pCodecCtxAudio->channels != c->channels )
        {
            if ( rs == NULL)
            {
                rs = av_audio_resample_init(c->channels,
pCodecCtxAudio->channels, c->sample_rate, pCodecCtxAudio->sample_rate,
AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16, 0,0,0,0);
            }
        }
        if(pCodecCtxAudio->sample_rate != c->sample_rate ||
pCodecCtxAudio->channels != c->channels)
        {
            int size_out = audio_resample(rs, (short *)buffer_resample,
samples, audiobufsize/ (pCodecCtxAudio->channels * 2) );
            av_fifo_generic_write(fifo, (uint8_t *)buffer_resample,
size_out * c->channels * 2, NULL );
        }
        else
        {
            av_fifo_generic_write(fifo, (uint8_t *)samples, audiobufsize,
NULL );
        }
    }
***********  -  Decoding part:   -  *************

***********  + Encoding part:   + ***************
if ( decoderData->audiobufsize )
    {
        AVPacket pkt;
        av_init_packet(&pkt);

        AVCodecContext* c = encoderData->audio_st->codec;

        int frame_bytes = c->frame_size * 2 * c->channels;

        while( av_fifo_size(decoderData->fifo) >= frame_bytes )
        {
            int ret = av_fifo_generic_read( decoderData->fifo, data_buf,
frame_bytes, NULL );
            /* encode the samples */
            pkt.size= avcodec_encode_audio(c, audio_out, frame_bytes
/*packet.size*/, (short *)data_buf);

            pkt.stream_index= encoderData->audio_st->index;
            pkt.data= audio_out;
            pkt.flags |= AV_PKT_FLAG_KEY;

            pkt.pts = pkt.dts = 0;
            /* write the compressed frame in the media file */
            if (av_interleaved_write_frame(encoderData->ocAud, &pkt) != 0)
            {
                fprintf(stderr, "Error while writing audio frame\n");
                exit(1);
            }
        }
    }
***********  - Encoding part:   - ***************

Other code is similar to the muxing.c example that comes with the builds. I
know the functions used above are kind of outdated, but that is the best
working source I could find from the internet.

Can anyone kindly highlight how I could improve my code, or do I need to
tweak ffplay somehow for better results?

Thanks for your time,

Best regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20121224/732e4d94/attachment.html>


More information about the Libav-user mailing list