[Ffmpeg-devel] [PATCH] generated PTS in MPEGTS wrong

Alexander Kurpiers a.kurpiers
Thu Feb 23 10:55:58 CET 2006

Hi All,

I tried to use ffmpeg (current CVS) to convert an MPEG2 program stream to
transport stream and had a bad issue with the generated PES packets.

The current code in mpegtsenc.c tries to squeeze everything in PES
packets of same size (DEFAULT_PES_PAYLOAD_SIZE), combining packets to
always output complete packets. The PTS is included in every PES
generated and is taken from the packets passed to mpegts_write_packet().

This leads to wrong PES packets.

The MPEG-2 system standard says ( Semantic definition of fields
in PES packet ):

"... In the case of video, if a PTS is present in a PES packet header it
shall refer to the access unit containing the first picture start code
that commences in this PES packet. A picture start code commences in PES
packet if the first byte of the picture start code is present in the PES
packet. "

As there can be many picture start codes in the PES packet written, it
is wrong to repeat the PTS on every packet, it is only valid for the
first packet - that is the first bug. The second bug is that the PTS as
delivered through pkt->pts belongs to the AVpacket passed. As the code
in mpegts_write_packet() keeps filling up PES packets until they are
"full", the new PTS is only written after the beginning of the AVpacket
is already used up (to fill the last PES). So in general PTS information
in the PES packets written lags by some bytes (the corresponding picture
start code is not in the PES with the new PTS).

My patch (attached) fixes the problem: the PTS is only written for the
first PES packet and every delivered AVpacket starts a new PES packet. The
drawback of this change is now that the length of generated PES packets
is not constant anymore. If that is an issue, than I would see no
alternative but to analyze the data and look for start codes (not nice...).

I hope it helps. What I did not check (and what might be broken now) is the
PCR though :-(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: ffmpeg_fix_ts_pts_write.patch
Type: text/x-patch
Size: 1049 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20060223/51b3336d/attachment.bin>

More information about the ffmpeg-devel mailing list