Repository navigation
isobmff: throw on box sizes smaller than their header - #1939
peaBerberian wants to merge 2 commits into
Conversation
03c7e97 to
b7eb88a
Compare
|
✅ Automated performance checks have passed on commit DetailsPerformance tests 1st run outputNo significative change in performance for tests:
|
b7eb88a to
71425f1
Compare
|
✅ Automated performance checks have passed on commit DetailsPerformance tests 1st run outputNo significative change in performance for tests:
|
71425f1 to
f7629f6
Compare
|
✅ Automated performance checks have passed on commit DetailsPerformance tests 1st run outputNo significative change in performance for tests:
|
f7629f6 to
7e55d71
Compare
|
✅ Automated performance checks have passed on commit DetailsPerformance tests 1st run outputNo significative change in performance for tests:
|
7e55d71 to
b3cb638
Compare
|
✅ Automated performance checks have passed on commit DetailsPerformance tests 1st run outputNo significative change in performance for tests:
|
With SegmentTemplate, SegmentList and SegmentTimeline, the MPD already lists all segments. Still, when the MPD also had an `indexRange`, we requested the index bytes with the initialization segment and parsed the `sidx` in it. The segment list we got from it was then ignored. Since #1939, reading that `sidx` can fail on malformed data, and that error stops playback (or disables subtitles for the Period). So such contents could now fail where they played before, because of a `sidx` we don't even use. I now only keep `indexRange` for SegmentBase, where we need the `sidx` to know the segments. To note that dash.js does the opposite: when an `indexRange` is present, it uses the `sidx` to get the segments, even with SegmentTemplate or SegmentList (see dash.js issue 1457). shaka-player and Media3 ignore `indexRange` outside of SegmentBase, like we now do.
With SegmentTemplate, SegmentList and SegmentTimeline, the MPD already lists all segments. Still, when the MPD also had an `indexRange`, we requested the index bytes with the initialization segment and parsed the `sidx` in it. The segment list we got from it was then ignored. Since #1939, reading that `sidx` can fail on malformed data, and that error stops playback (or disables subtitles for the Period). So such contents could now fail where they played before, because of a `sidx` we don't even use. I now only keep `indexRange` for SegmentBase, where we need the `sidx` to know the segments. To note that dash.js does the opposite: when an `indexRange` is present, it uses the `sidx` to get the segments, even with SegmentTemplate or SegmentList (see dash.js issue 1457). shaka-player and Media3 ignore `indexRange` outside of SegmentBase, like we now do.
I recently ran an LLM on our ISOBMFF-related code, hoping to catch
issues and to make it more resilient and more aligned with what other
tools do with badly-formed data. This led to multiple changes, some
already done (e.g. the infinite loop on a pssh with a size of 0) and
some still to come. This commit is one of them.
The problem
-----------
When a box announces a size smaller than its own header (`8`, or `16`
with a 64-bit size), our functions looking for a box in ISOBMFF data
(`getBoxOffsets` and `getUuidContent`) did not see it as an issue:
- with a 64-bit size of `0` we did not move forward and looped
forever
- for other small sizes, we ended up in the middle of the data and
read garbage as the next boxes, or returned an empty box
They had a check for this, but it only looked for negative sizes,
which never happens as we read sizes as unsigned.
What I did here
---------------
Those functions, and `getNextBoxOffsets`, now throw when the size is
smaller than the header. I chose to throw instead of returning "no
box", as a missing box and data we cannot read are not the same thing
and callers may want to do something different for each.
To note that a 32-bit size of `4` is still skipped: it always worked
here by luck and GPAC accepts it as a 4-byte filler, so I preferred
not breaking such contents.
Then I went through all callers one by one:
- When the information is optional or when we have a fallback (key
id, timing, `emsg`, `mdhd` timescale, Smooth's `tfrf`/`tfxd`,
`senc`, PSSH...), I catch and consider the box as not there, with
a warning log. So no behavior change for those.
- For `emsg` and the `trun` samples of WebVTT embedded in MP4, I keep
what was read before the bad box, like most other players seem to
do. For this, I moved the loop on `moof` boxes of `getTrunSamples`
to the WebVTT code, the per-`traf` part staying in the ISOBMFF
utils.
I did not do the same for segment durations, as a partial
duration would just be wrong without us knowing it.
- When we cannot do anything without it (text `mdat`, WebVTT in
MP4, Smooth's `patchSegment` required boxes, `sidx` when the
container is known to be mp4 and an `indexRange` is announced),
we let it throw.
Other implementations don't agree on what to do here: browsers, GPAC
and ExoPlayer generally fail, where most web players just go on. With
those changes, we go on when we can and fail only when we cannot do
anything without that box.
Behavior changes
----------------
- A malformed text segment now makes the text stream fail for the
whole Period, where before we just had no subtitle for it. I added
a TODO in `period_stream.ts` as I would prefer to only skip that
segment.
- In low-latency mode, such error on a chunked text segment goes
through how we currently handle parsing errors during chunked
requests: it is considered as a request error, and is retried or
another CDN is tried. This is an already-existing issue for any
parsing error, which I will fix separately.
- A 64-bit size of `8`, that ffmpeg reads as "the box goes until the
end of the file", now throws. I added a TODO for it.
With SegmentTemplate, SegmentList and SegmentTimeline, the MPD already lists all segments. Still, when the MPD also had an `indexRange`, we requested the index bytes with the initialization segment and parsed the `sidx` in it. The segment list we got from it was then ignored. Since #1939, reading that `sidx` can fail on malformed data, and that error stops playback (or disables subtitles for the Period). So such contents could now fail where they played before, because of a `sidx` we don't even use. I now only keep `indexRange` for SegmentBase, where we need the `sidx` to know the segments. To note that dash.js does the opposite: when an `indexRange` is present, it uses the `sidx` to get the segments, even with SegmentTemplate or SegmentList (see dash.js issue 1457). shaka-player and Media3 ignore `indexRange` outside of SegmentBase, like we now do.
b3cb638 to
cca7d43
Compare
|
✅ Automated performance checks have passed on commit DetailsPerformance tests 1st run outputNo significative change in performance for tests:
|
I recently ran an LLM on our ISOBMFF-related code, hoping to catch issues and to make it more resilient and more aligned with what other tools do with badly-formed data. This led to multiple changes, some still to come.
The problem
When a box announces a size smaller than its own header (
8, or16with a 64-bit size), our functions looking for a box in ISOBMFF data (getBoxOffsetsandgetUuidContent) did not see it as an issue:0we did not move forward and looped foreverThey had a check for this, but it only looked for negative sizes, which never happens as we read sizes as unsigned.
Other implementations don't agree on what to do here: browsers, GPAC and ExoPlayer generally fail, where most web players just go on. With the changes here, we go on when we can and fail only when we cannot do anything without that box.
What I did here
Those functions, and
getNextBoxOffsets, now throw when the size is smaller than the header. I chose to throw instead of returning "no box", as a missing box and data we cannot read are not the same thing and callers may want to do something different for each.To note that a 32-bit size of
4is still skipped: it always worked here by luck and GPAC accepts it as a 4-byte filler, so I preferred not breaking such contents.Then I went through all callers one by one:
emsg,mdhdtimescale, Smooth'stfrf/tfxd,senc, PSSH...), I catch and consider the box as not there, with a warning log. So no behavior change for those.emsgand thetrunsamples of WebVTT embedded in MP4, I keep what was read before the bad box, like most other players seem to do. For this, I moved the loop onmoofboxes ofgetTrunSamplesto the WebVTT code, the per-trafpart staying in the ISOBMFF utils. I did not do the same for segment durations, as a partial duration would just be wrong without us knowing it.mdat, WebVTT in MP4, Smooth'spatchSegmentrequired boxes,sidxwhen the container is known to be mp4 and anindexRangeis announced), we let it throw.Behavior changes
period_stream.tsas I would prefer to only skip that segment.8, that ffmpeg reads as "the box goes until the end of the file", now throws. I added a TODO for it.NOTE: The diff is huge, but it's mostly unit tests as that area was not unit tested before. Most unit tests were LLM-generated, I controlled and reviewed that each does test the behavior we actually want, not accidental one and that the testing style correspond to ours. For the actual source code change, I've personally chose the change on a case by case basis.