I\'m currently trying to read small video files sent from a server
In order to read a file using libavformat, you are supposed to call
av_open_input_
It's funny how I always find the solution by myself right after I post the question on this site, even though I've been working on this problem for hours.
In fact you have to initialize avFormatContext->pb before calling av_open_input, and pass to it a fake filename.
This is not written in the documentation but in a commentary directly in the library's source code.
Example code if you want to load from an istream (untested, just so somebody which has the same problem can get the idea)
static int readFunction(void* opaque, uint8_t* buf, int buf_size) {
auto& me = *reinterpret_cast(opaque);
me.read(reinterpret_cast(buf), buf_size);
return me.gcount();
}
std::ifstream stream("file.avi", std::ios::binary);
const std::shared_ptr buffer(reinterpret_cast(av_malloc(8192)), &av_free);
const std::shared_ptr avioContext(avio_alloc_context(buffer.get(), 8192, 0, reinterpret_cast(static_cast(&stream)), &readFunction, nullptr, nullptr), &av_free);
const auto avFormat = std::shared_ptr(avformat_alloc_context(), &avformat_free_context);
auto avFormatPtr = avFormat.get();
avFormat->pb = avioContext.get();
avformat_open_input(&avFormatPtr, "dummyFilename", nullptr, nullptr);