justdan96 / tsMuxer

tsMuxer is a transport stream muxer for remuxing/muxing elementary streams, EVO/VOB/MPG, MKV/MKA, MP4/MOV, TS, M2TS to TS to M2TS. Supported video codecs H.264/AVC, H.265/HEVC, VC-1, MPEG2. Supported audio codecs AAC, AC3 / E-AC3(DD+), DTS/ DTS-HD.
Apache License 2.0
860 stars 144 forks source link

PGS demuxing: set DTS equal to PTS for packets with PTS_DTS_flags=0b10 #833

Closed cubicibo closed 6 months ago

cubicibo commented 7 months ago

This is a tiny change in demuxing. I think this is more logical than a sporadically null DTS in the output .SUP file.

jcdr428 commented 6 months ago

@cubicibo not sure about this: in the below links, dts should be zero. If you can find a reference to a more official source...

https://blog.thescorpius.com/index.php/2017/07/15/presentation-graphic-stream-sup-files-bluray-subtitle-format/

https://video.stackexchange.com/questions/16261/split-pgs-subtitles-sup-file

https://github.com/robjtede/sup-decode/blob/69ad5154e0eb62360edf6cc3549febca6e51a2c4/src/main.rs#L156

https://github.com/Sec-ant/BDSupReader/blob/c4895be8345d2857ef8f4dc3c79c4b03b9ef7241/src/bdsupreader.py#L969

cubicibo commented 6 months ago

Hi @jcdr428, unfortunately this is a widespread misconception originating from DGDemux/eac3to suites. Many files were produced with these programs that always set DTS to zero, and people got the impression the field is never used.

However, when drmpeg designed the 'PG' SUP file format, he made sure to convey both PTS and DTS (truncated from 33 to 32 bits). Since the DTS is irrelevant for software players, it has been discarded by some tools like DGDemux. If the SUP file is muxed back to a transport stream, the incorrect DTS value will cause issue to some decoders. It can range from PTS-DTS difference being larger than one second, memory overruns in the PG decoder (too many PCS buffered at once, overwriting ODS when it should be read-only). Or not enough decoding time if tsMuxer uses PTS for scheduling when DTS is zero.

If you take professionally authored discs and analyze the transport stream, you will see actual DTS values to ensure decoder memory and bandwidth are dully respected. Here is a good PG stream but with DTS=0 played on PS3. As you can see, the effect is catastrophic. https://github.com/justdan96/tsMuxer/assets/55701024/a634d8c4-a654-4d8a-874c-a8523f6d9d60

When muxing, tsMuxer and other professional muxers uses the "not permitted" case of PTS=DTS to know when a PES packet should have PTS_DTS_Flags equal to 0b10 or 0b11: https://github.com/justdan96/tsMuxer/blob/94cafe7244213870aaab37035c827ef839a15929/tsMuxer/tsMuxer.cpp#L609C1-L612C51

This proposed code change makes tsMuxer muxing and demuxing operations bijective for PG streams.

jcdr428 commented 6 months ago

Ok, I've pushed the commit.