I'm making a C++ application that retrieves frames from a camera and then encodes each frame with a H264 encoder (not using libav). This encoded H264 frame is then kept in memory as a void *mem as I need to do several things with the encoded frame.
One of the things I need to do, is store the frames (so the void *mem pointers) in a .mp4 container using libavcodec/libavformat. I do NOT want to transcode each frame, I just want to store them directly into the mp4 container.
Preferably for each individual frame that I push through, I get the resulting data as a return type from the function (not sure if this is possible?). If this is not possible, then writing to a file directly is OK as well.
How does one go about doing this with libav?
The only part I have got so far, and where I'm getting stuck, is this:
/*
some private fields accessible in MP4Muxer:
int frameWidth_, frameHeight_, frameRate_, srcBitRate_;
*/
void MP4Muxer::muxFrame(void *mem, size_t len, int64_t timestamp, bool keyFrame) {
const AVOutputFormat* outputFormat = av_guess_format("mp4", NULL, NULL);
AVFormatContext* outputFormatContext = avformat_alloc_context();
outputFormatContext->oformat = outputFormat;
AVStream* videoStream = avformat_new_stream(outputFormatContext, NULL);
videoStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
videoStream->codecpar->codec_id = AV_CODEC_ID_H264;
videoStream->codecpar->width = frameWidth_;
videoStream->codecpar->height = frameHeight_;
videoStream->avg_frame_rate = (AVRational) {frameRate_, 1};
videoStream->time_base = (AVRational) {1, 90000};
}
How do I continue from here? Are there any good resources I can follow? There are some resources I found online, but all of them either write the output directly to a file, read input directly from streams/files etc. so I have a hard time translating them to my needs.
After a ton of reading, reverse engineering FFMPEG etc., I came up with the following solution. I'm not sure if the code is 100% correct as it is based on a lot of assumptions.
MP4Muxer.hpp:
MP4Muxer.cpp: