Improve MP4 seeking performance
Categories
(Core :: Audio/Video: Playback, task)
Tracking
()
People
(Reporter: tnibler, Unassigned)
Details
With Bug 2080453, seeking should be pretty much close to correct no matter how badly the file is muxed. The nearest keyframe before the seek target time is determined by parsing the bitstream, all container info such as stss entries or tfhd flags is ignored. (It's been that way for a long time, recent changes are superficial)
Unfortunately, that leaves everything in a somewhat messy state:
- Layers use different keyframe definitions.
MP4SampleIndexandSampleIteratorare oblivious to used video codecs and rely on container info, whichMP4TrackDemuxerthen mostly ignores.MP4TrackDemuxerends up reimplementing a subset of the functionality of the other two classes, which is not only redundant but also error prone as they can easily diverge. The codec specific logic is at the wrong level: if keyframe criteria depend on the codec,MP4SampleIndexcan not do its job in a codec agnostic way. - When seeking, samples are copied and parsed over and over again to determine if they're keyframes, where one time should obviously suffice. We end up copying tens of kB to look at a handful of header bytes, throw away the result and do it all again next time. With the recent patches to improve seeking correctness, this is also more likely to happen.
- Linear search is used in most places which, even if not copying and parsing every sample, is not optimal when the task is very suited to use lookup tables.
From quick experiments, replacing the seeking loop with a map lookup makes for very noticeable speedups. Scrubbing in a local file looks comparably fluid to Chrome now.
I'm proposing to consolidate the seeking logic, pushing it down a level and make MP4SampleIndex use codec information to find keyframes and build an index of random access points.
It will improve performance as well as simplify the implementation and make it less spread out. In the future mediasource/TrackBuffersManager could also use that keyframe table and replace the linear searches with a lookup, it's already built during demuxing after all.
The question remains though of where to do the bitstream parsing, I'd appreciate people's input there :) Of course there may also be different considerations that I'm missing.
When MP4SampleIndex and MoofParser do their thing, it's not given that data is available in memory, which is what the codec parsers require.
For H264, a small ad-hoc parser that reads only the header bytes it needs is straightforward. Especially for VP9 and AV1 though I can't tell how good of an idea that is.
Building the keyframe LUT eg lazily, during playback when samples are in memory anyway could be nice, but then the initial parsing work in MP4SampleIndex would have to rely on container info. Which might turn out fine, I don't think anything critical relies on that.
You could also assume that VP9/H261/AV1 files are more likely muxed correctly, use keyframe info for those and only look at the bitstream for H264. But that's a made up assumption without anything to back it up.
| Reporter | ||
Comment 1•1 month ago
|
||
Yaknow what that's actually a lot of work for mostly aesthetic improvements. Slotting in some memoization or similar seems more adequate for the time being
| Reporter | ||
Updated•1 month ago
|
| Reporter | ||
Updated•15 days ago
|
Description
•