[Libav-user] Muxing settings for h264 + mp4

Kalileo kalileo at universalx.net
Thu Feb 23 04:24:51 CET 2012

On Feb 23, 2012, at 00:59 , ALESSANDRO PAGANELLI wrote:

> I'm working on a project (which uses Libav/Ffmpeg) in which I have to transmit an h264 encoded file (with Mp4 file format) over a simulated network (I'm using network simulator 3 for this), to "emulate" a real video streaming.
> Now, I'm facing the problem regarding how to "rebuild" the transmitted file at the receiver side. More in details, this issue regards the settings needed to replicate the configurations for AVFormatContext, AVStream and AVCodecContext, starting from the same structures I have at the transmitter.
> As for now, I just copied the content of the sender's AVCodecContext structure to the receiver's one, but the resulting file is broken. I suspect that other settings have to be replicated too, but it's not clear which ones are those required by h264 and Mp4.
> Is there any document/tutorial about the required settings for each specific codec or format?

If your source file is mp4 and your stream is mpegts then you might need to add the h264_mp4toannexb filter. This filter is available as an option for ffmpeg and you can also use it between av_read_frame and av_write_frame.

> More in general, do you have suggestions about how to approach this problem for a generic combination of codec and format? I planned to implement different possible combinations of them, so it would be great if there is a "smart" way to copy these contexts without doing it "by hand" :) .

If you scan through this list you'll note a lot of questions which basically come back to the same problem you see here  (and yes, one of them I had been asking).

As far as I understood this problem the reason is that h264 (and I think also aac) are packed differently depending of if they are to be saved to a file or if they are to be streamed. h264 frames in a mpegts stream require a specific header for each frame, which is this "annex B". 

This h264_mp4toannexb filter will take the required information from the file header and put it in the annex B frame header. This  is meant to work if you have a mp4 file to begin with. (There is a similar filter for aac.)

If your source file is not in the mp4 format which this filter expects then you're out of luck (this is where I'm stuck too :( ). I have source files which are h264+aac in mpegts format, and there these filters do not work. 

Unfortunately there seems to be no other documentation about how to build a correct annex B header manually except what's in the source code (if there is, please point me to it!).

More information about the Libav-user mailing list