[Libav-user] Avformat context takes much data before returning data

Jesper Taxbøl jesper at taxboel.dk
Fri May 22 16:52:11 EEST 2020

I am using a *AVFormatContext* in conjunction with an *avio_ctx_buffer* to
read streaming data into a demuxer.

The source data is received via a socket and injected via the read_packet()
calback. When no data is available, I let the callback sleep. I have
verified that the data is read by the callback function, so it is

My video track is always at the same index so I have no need for searching
for streams.

When receiving the AVPackets I deal with them in my own custom functions.

I am basically replacing a flaky homebrewed demuxer with a more standard

The problem is that when I start it up, the context reads almost 3MB before
it returns the first AVPacket.

I see various topics online on how to reduce the analysis time, but I am
unable to reduce mine.

Would any of you guys know where to look?

Kind regards


static int read_packet(void *opaque, uint8_t *buf, int buf_size)
//sleep while no data available
//Read bytes into buf
return buf_size;

void* DemuxThread(void*pParam)
printf("Starting demux thread\r\n");

static AVFormatContext *fmt_ctx = NULL;
if (!(fmt_ctx = avformat_alloc_context())) {
fprintf(stderr, "Could not allocate fmt_ctx\n");

size_t avio_ctx_buffer_size = 4096;
uint8_t* avio_ctx_buffer = (uint8_t*)av_malloc(avio_ctx_buffer_size);
if (!avio_ctx_buffer) {
printf("error allocating avio_ctx_buffer\r\n");
AVIOContext* avio_ctx = avio_alloc_context(avio_ctx_buffer,
avio_ctx_buffer_size, 0, NULL, &read_packet, NULL, NULL);

if (!avio_ctx) {
printf("error allocating avio_ctx\r\n");
fmt_ctx->pb = avio_ctx;

if (avformat_open_input(&fmt_ctx, NULL, NULL, NULL) < 0)
fprintf(stderr, "Could not open source\n");
AVPacket pkt;
pkt.data = NULL;
pkt.size = 0;
while(av_read_frame(fmt_ctx, &pkt) >= 0 && running)
case 0: //video
printf("Demuxer got video, %d\r\n", readbytes);
case 1: //Audio
printf("Demuxer got audio, %d\r\n", readbytes);
printf("Unknown payload\r\n");
printf("Ending demux thread\r\n");
return 0;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200522/ab00a378/attachment.html>

More information about the Libav-user mailing list