There are a few articles on the internet about this topic, but they are kind of outdated so I'm writing a new one.
Here is the source code for the impatient.
The idea is simple, just define your own callback methods and buffers, then tell ffmpeg to use them. In my code, I've wrapped up the variables into a class to pass things around easily.
There are a few things to pay attention to. Use av_malloc()
and av_free()
to allocate and free your buffers, ffmpeg aligns the memory it allocates which should speed things up. Free the buffer first then free AVIOContext
, ffmpeg does something funny with the buffer, don't rely on ffmpeg to free it otherwise you will run into troubles (I got double free errors). It's also necessary to discover the input format yourself, either probe it or set it. And even if you do your own IO, don't count on calling avformat_open_input()
from multiple threads at the same time.
Below is how to open a file using the class I wrote. Note that an empty filename is passed into avformat_open_input()
.
MyIOContext *ioCtx = new MyIOContext("random_file");
AVFormatContext *avFormatCtx = avformat_alloc_context();
ioCtx->initAVFormatContext(avFormatCtx);
if (avformat_open_input(&avFormatCtx, "", NULL, NULL) != 0) {
// handle error
}
// ...
avformat_close_input(&avFormatCtx);
References: