[FFmpeg-devel] Muxing h.264 to MP4 gives broken files

Thorsten Jordan tjordan
Fri Mar 14 13:47:46 CET 2008

Baptiste Coudurier schrieb:
> Hi,
> Thorsten Jordan wrote:
>> Hi there,
>> There is some interesting additional information though:
>> If you encode video with b-frames (h264), but give linear increasing
>> values as pts (decoding order, so to say) instead of what the encoder
>> gives as pts AND if you state that has_b_frames and max_b_frames of the
>> context are zero before calling av_interleaved_write_frame, then the
>> resulting MP4 file seems ok and mplayer doesn't report the "no keyframe"
>> line.
> IMHO broken files are declared based on specs, and not
> player/decoder/demuxer behaviour.
> Can you tell exactly in where generated mp4 is broken ?
i am no expert for MP4 files, what shall i say here? do you want such a
file as test material? or the elementary stream from it?

My category of "broken" is that mplayer can't playback them correctly. I
also had trouble when playing these files on Windows/quicktime and a
X-Box 360 too, so it doesn't seem mplayer's fault.

It seems that for the first frame, which is a key frame (IDR picture),
this is somehow stored wrong inside the MP4. The first frame is not
displayed in mplayer, only the following frames, leading to colored
moving artefacts. Same for every seek. From the second played GOP on
after start or seek it's ok.

I tried to modify three things: AVCodecContext.has_b_frames,
AVCodecContext.max_b_frames in the writing context and AVPacket.pts when
writing the frames, nothing helped.
I can either set has_b_frames = max_b_frames = 0 and use linear values
for AVPacket.pts then the generated files are ok, or do it like ffmpeg.c
does it and they are broken. Of course i did not modify the same context
that the encoder uses, but a copy (changing max_b_frames while encoding
could be a bad idea). I had the very same problem by using ffmpeg.c
direktly as transcoder with the only modification to use h264 with
b-frames (=2), so this is also no problem of the usage of libavformat.

Regards, Thorsten

More information about the ffmpeg-devel mailing list