[Libav-user] av_read_frame() stops returning audio packets after avcodec_decode_audio2() returns <0

Reuben Scratton reuben.scratton at gmail.com
Fri Nov 18 22:47:10 CET 2011


Hi,


I have a video player whose main decoding loop is mostly pasted below.

My problem is that with a certain mp4 file (H264 video, AAC audio),
avcodec_decode_audio2() returns -1 partway through, after which point
I never get any more audio packets. Video continues as normal, but
audio just ends.

However VLC is able to play the same file without issue. There is an
audible click at the point that my player gets the -1 from
avcodec_decode_audio2() suggesting there is indeed some error in the
audio stream, but VLC is clearly recovering from it much better than
mine is.

What might I be doing wrong? In the event of avcodec_decode_audio2()
returning -1, I advance the input pointer anyway, in the hope it will
recover, which indeed it seems to after 3 failed calls to
avcodec_decode_audio2()... the 4th call is successful (returns 253).
But after that, there is no more audio packets.

Very many thanks for any help, I really am tearing my hair out here!


    // Fetch a compressed frame/packet from the stream.
    i=0;
    while(av_read_frame(strm->pFormatCtx, &packet)>=0) {
        pts = 0;

        // Is this a packet from the video stream?
        if(packet.stream_index==strm->videoStream) {
             ... decode picture via avcodec_decode_video() ...
        }

        // Or if it's an audio stream packet...
        else if(packet.stream_index==strm->audioStream) {
            uint8_t *packetData = packet.data;
            int packetSize = packet.size;
            while (packetSize > 0) {
                 int outbuf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
                 int lenRead = avcodec_decode_audio2(strm->aCodecCtx,
outbuf, &outbuf_size, packetData, packetSize);
                 if (lenRead<=0) {
                     packetSize--;
                     packetData++;
                 }
                 else {
                     if (outbuf_size > 0) {
                          ... add decoded bytes to output audio queue ...
                     }
                     packetSize -= lenRead;
                 }
            }
        }


-- Reuben Scratton


More information about the Libav-user mailing list