tsdemux: Improve keyframe seeking
Currently keyframe seeking is done by:
- Seeking to 2.5s before requested position
- Guess-timating where the offset would be (based on PCR observations)
- Collecting PES data (including for streams on which we don't do keyframe checking)
- Figure out if a fully collected PES is a keyframe
- If not seek back a bit, and go back to step 3
- If it does contain a keyframe, but not SPS/PPS/... collect keyframe and go back to step 3
This could be improved by:
-
No longer offseting back the position if there is a keyframe handler for the video stream, but instead using the requested position.
-
in SEEKING state, create a dedicated Packetizer mode which allows scanning rapidly for given PIDs (the video one and the PCR one if it's not the same as the video one).
-
Scan for position
- Start from a guesstimated offset (PTS/DTS => PCR => offset) and search for those PID if PUSI bit is set or adaptation field is present. Ignore all other packets.
- If the stream is AVCHD/BDMV, every single packet has the PCR as the first 4 bytes. This can speed up guessing the initial start position to look for those given PID.
-
If the resulting offset is too far astray (or because of PCR jump/discont/gap), recalculate a new offset and go back to 3
-
Every time a video PID with PUSI bit is set, start scanning the content of that packet PES for actual PTS/DTS and keyframe/sps/pps presence. If scanner needs more data from that packet to decide whether it contains or not a keyframe/sps/pps, it can say so and the next packet from that video PID is provided
-
If that packet contains all needed information to start playback (SPS/PPS/keyframe), mark that as the starting position and go back to regular playback mode
-
If that packet doesn't contain the needed information, scan backwards for video PID with PUSI bit set, i.e. go back to step 5.
This will:
- reduce i/o to a minimum and ensure we always hit the target keyframe (and not some several seconds earlier)
- avoid messing up with the state of tsdemux regular playback (positions going completely astray, random stray data being pushed out, ...)
- allow much faster seeking, especially for AVCHD/BDMV content