twice call avformat_find_stream_info() crashed

Viewed 141

My version of FFmpeg is 4.4. There is a logic in my code that calls avformat_find_stream_info() twice continuously, but I don't understand why it crashed here. I tried single-step debugging but it didn't work out. Here is my simply code that can be run directly:

#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>

int main()
{
    av_log_set_level(AV_LOG_DEBUG);
    const char* in_filename_a = "aoutput.aac";
    AVFormatContext* ifmt_ctx_a = NULL;
    int ret = avformat_open_input(&ifmt_ctx_a, in_filename_a, 0, 0);
    if (ret < 0)
    {
        fprintf(stderr, "Could not open input_a %s", in_filename_a);
        return -1;
    }
    fprintf(stderr, "before ifmt_ctx_a=0x%x\n", ifmt_ctx_a->streams[0]);
    ret = avformat_find_stream_info(ifmt_ctx_a, 0);
    if (ret < 0)
    {
        fprintf(stderr, "Could not find input_a stream info");
        return -1;
    }
    fprintf(stderr, "after ifmt_ctx_a=0x%x\n", ifmt_ctx_a->streams[0]);
    /// crashed here
    ret = avformat_find_stream_info(ifmt_ctx_a, 0);
    if (ret < 0)
    {
        fprintf(stderr, "Could not find input_a stream info");
        return -1;
    }

}

call stack

1 Answers

There are few hints that imply that we shouldn't execute avformat_find_stream_info twice.

  • The documentation says: "examined packets may be buffered for later processing."
    There is a change that the first execution buffers few packets, and the second execution tries to buffer the packets again without allocating additional space.
  • The console prints logging messages as:

Before avformat_find_stream_info() pos: 0 bytes read:65696 seeks:4 nb_streams:1
After avformat_find_stream_info() pos: 27420 bytes read:65696 seeks:4 frames:50

       The messages imply that the position advances from 0 to 27420. The address of ifmt_ctx_a->streams[0] is the same, but the execution of the first avformat_find_stream_info did some seeking (so the second execution is not the same as the first one).

  • Using the debugger, we can see that ifmt_ctx_a->pb[0].buf_ptr is increased after the first execution of avformat_find_stream_info.

Note:

  • I don't know if the crash is normal behavior or a bug in Libavformat library.
    I didn't try looking at the source code of Libavformat.

For reading twice, you may close and reopen ifmt_ctx_a:

avformat_close_input(&ifmt_ctx_a);   
ret = avformat_open_input(&ifmt_ctx_a, in_filename_a, 0, 0);

I don't see any reason to do that...


Other option is opening another AVFormatContext:

AVFormatContext* ifmt_ctx_a = NULL;
int ret = avformat_open_input(&ifmt_ctx_a, in_filename_a, 0, 0);
if (ret < 0)
{
    fprintf(stderr, "Could not open input_a %s", in_filename_a);
    return -1;
}

ret = avformat_find_stream_info(ifmt_ctx_a, NULL);
if (ret < 0)
{
    fprintf(stderr, "Could not find input_a stream info");
    return -1;
}

//Opening another AVFormatContext:
////////////////////////////////////////////////////////////////////////////
AVFormatContext* ifmt_ctx_a2 = NULL;
ret = avformat_open_input(&ifmt_ctx_a2, in_filename_a, 0, 0);
if (ret < 0)
{
    fprintf(stderr, "Could not open input_a %s", in_filename_a);
    return -1;
}

ret = avformat_find_stream_info(ifmt_ctx_a2, NULL);
if (ret < 0)
{
    fprintf(stderr, "Could not find input_a stream info");
    return -1;
}
////////////////////////////////////////////////////////////////////////////
Related