ParseSliceHeader silently drops the sole SPS long-term reference and misreads headers
perfloop-oss/mp4ff · Uncatalogued correctness
https://perfloop.ai/t/oss/case_6fbmm873n3
Verdict
VERIFIED · settled 2026-11-14
What happened: The check fails before this change and passes with it.
Hypothesis
A slice that reuses the only long-term picture in its sequence parameter set (SPS) can be returned with that reference replaced by zero values and no error. The resulting in-use picture count can shift later header fields and the byte boundary used for CBCS encryption. Occurrence in encoder-produced streams and real-stream prevalence remain unknown.
In hevc/slice.go:234-255, ParseSliceHeader initializes lt to zero and copies sps.LongTermRefPicSets[LtIdxSps] only inside the sps.NumLongTermRefPics > 1 condition at lines 237-243. For an inherited entry when the SPS has exactly one long-term picture, neither PocLsbLt nor UsedByCurrPicLtFlag is copied. That flag contributes to NumPicTotalCurr at lines 248-250. The count gates reference-list-modification parsing at lines 289-300, before QpDelta is read at line 334 and Size is calculated at lines 424-427. Readers of LongTermRefPicSets, QpDelta or Size are affected for such inputs. The package's slice-header parsing is documented at README.md:137; mp4/crypto.go:313-320 calls this parser and uses Size as the CBCS clear-header range.
The submitter describes the smallest failing input as an SPS with long_term_ref_pics_present_flag=1 and exactly one long-term entry, lt_ref_pic_poc_lsb_sps[0]=100 and used_by_curr_pic_lt_sps_flag[0]=1. Its hex is 42010101600000030090000003000003005da02081059657aa481ae55a6484. The reported P-slice inputs use num_long_term_sps=1: - With PPS 4401c0718012 and slice 0201d02ca208c0a55a80, the submitter reports a nil error and LongTermRefPicSets[0]={PocLsbLt:0 UsedByCurrPicLtFlag:false}, instead of {PocLsbLt:100 UsedByCurrPicLtFlag:true}. - With PPS 4401c0718032, which enables lists_modification_present_flag, and slice 0201d02ca10f80a55a80, the submitter reports a nil error, QpDelta=-3 and Size=6 bytes, instead of QpDelta=-7 and Size=7 bytes.
A focused unchanged-code probe during anchoring reproduced the zeroed entry in both examples and the reported QpDelta=-3, Size=6, nil-error result in the second example. This does not independently establish the expected values or exposure in real streams.
The submitter cites H.265 7.4.7.1: 'If i is less than num_long_term_sps, PocLsbLt[ i ] is set equal to lt_ref_pic_poc_lsb_sps[ lt_idx_sps[ i ] ]'. They state that lt_idx_sps is not coded when the SPS has one entry and is then inferred as 0. They also cite FFmpeg n7.1 libavcodec/hevc/hevcdec.c:314-320, https://github.com/FFmpeg/FFmpeg/blob/n7.1/libavcodec/hevc/hevcdec.c#L314-L320: 'uint8_t lt_idx_sps = 0; if (sps->num_long_term_ref_pics_sps > 1) lt_idx_sps = get_bits(...)'. These are the reported independent semantic references to check.
The submitter reports that using index 0 for one entry makes all 680 tried header variants parse as expected; that sweep has not been rerun here. They also report that none of the 147 JCT-VC HEVC_v1 conformance streams has exactly one SPS long-term entry. The supplied input is built from the syntax, not from an encoder. The sweep and conformance-coverage reports remain attributed, unconfirmed observations, not evidence of stream prevalence.
A provisional first check is a focused Go regression that parses the supplied SPS/PPS/slice triples and compares inherited long-term fields, QpDelta, Size and errors with an independent syntax-derived oracle, including whether list-modification bits must be consumed. The stated expected results confirm the report if the syntax check agrees; contrary independent results reject those expectations. Then check the reported variant sweep and the downstream CBCS boundary, retaining multiple-entry and invalid-index controls. This proposed test layout, placement and oracle implementation are provisional proof methods, not additional frozen requirements.
Current upstream checks returned no open pull requests at https://github.com/Eyevinn/mp4ff/pulls, and the current https://github.com/Eyevinn/mp4ff/blob/master/hevc/slice.go still has the guarded copy. The inspected recent bounds change, https://github.com/Eyevinn/mp4ff/commit/aa5c752772a2a65be0a0d2a73e1857b0c622ea08, adds count and loop limits rather than fixing single-entry inheritance; this outcome must retain its checks. History coverage is bounded: the history API returned 403, and a 40-commit git deepening supplied only recent history. CONTRIBUTING.md names issues and pull requests as the contribution venue; no separate review venue was identified.
Change to test: Restore SPS-derived long-term picture values and downstream header alignment when the SPS has exactly one long-term entry, while retaining index bounds validation. In ParseSliceHeader's long-term loop, default the index to 0, read lt_idx_sps only when sps.NumLongTermRefPics > 1, and perform the bounds check and sps.LongTermRefPicSets[index] copy outside that condition for every entry with i < num_long_term_sps.
Where it lives
Evidence
The check fails before this change and passes with it: `For the supplied sole-long-term-reference P-slices and syntax-built I/P/B slices with 0, 1, 2, 3 or 32 SPS long-term pictures, ParseSliceHeader returns the syntax-derived long-term reference fields, list entries, QP and byte-aligned header size; supplied CBCS samples keep the seven-byte slice header clear with external and in-band parameter sets, and invalid inherited indexes are rejected.`
The check fails before this change and passes with it: `For syntax-built I/P/B slices with zero, one, two or three SPS short-term reference sets, ParseSliceHeader returns the inline or inferred or signaled set and syntax-derived list entries, QP -5 and aligned size; CBCS ranges exclude the complete header with external or in-band parameter sets, three-set invalid indexes are rejected, repeated calls preserve parameter sets, and combined sole-short/long inheritance returns both reference kinds.`
ParseSliceHeader SPS EmptyI QP0 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
188.5 |
194.6 |
+7.3 |
−8.7 to +32.3 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS EmptyI QP-5 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
209.7 |
219.2 |
+7.5 |
−4.2 to +27.3 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS EmptyP QP0 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
193.9 |
197.7 |
+9 |
−12.1 to +23.1 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS EmptyP QP-5 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
222.1 |
226.3 |
+4.2 |
−7.7 to +12.6 |
loss ≤ stated 0.1 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS EmptyB QP0 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
202.4 |
204.5 |
+3.5 |
−18.5 to +29.9 |
loss ≤ stated 0.65 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS EmptyB QP-5 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
215.6 |
222.9 |
+4.8 |
−7.6 to +34.6 |
loss ≤ stated 0.65 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS TwoP QP0 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
198.7 |
209.9 |
+15.4 |
−3 to +50.5 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS TwoP QP-5 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
210.8 |
229.2 |
+16.2 |
−5.2 to +34.2 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS TwoPLists QP0 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
216.5 |
263.3 |
+21.1% (+45.7) |
+34.5 to +65.4 |
loss ≤ stated 0.6 × |baseline median| |
PASSED |
B/op |
416 |
465 |
+11.8% (+49) |
+49 to +49 |
loss ≤ stated 0.25 × |baseline median| |
PASSED |
allocs/op |
3 |
4 |
+33.3% (+1) |
+1 to +1 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
ParseSliceHeader SPS TwoPLists QP-5 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
201.6 |
282 |
+39.9% (+80.4) |
+66.5 to +103.6 |
loss ≤ stated 0.8 × |baseline median| |
PASSED |
B/op |
400 |
465 |
+16.3% (+65) |
+65 to +65 |
loss ≤ stated 0.25 × |baseline median| |
PASSED |
allocs/op |
2 |
4 |
+100% (+2) |
+2 to +2 |
loss ≤ stated 1.25 × |baseline median| |
PASSED |
ParseSliceHeader SPS 32P QP0 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
196.3 |
206.1 |
+11.3 |
−0.5 to +27.8 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader SPS 32P QP-5 · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
222.7 |
234.6 |
+11.1 |
−7.5 to +51.2 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
B/op |
400 |
400 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2 |
2 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
ParseSliceHeader sole long-term SPS with lists · 21 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
289.4 |
356.1 |
+22.7% (+65.7) |
+34 to +86.6 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |
B/op |
416 |
480 |
+15.4% (+64) |
+64 to +64 |
loss ≤ stated 0.25 × |baseline median| |
PASSED |
allocs/op |
3 |
4 |
+33.3% (+1) |
+1 to +1 |
loss ≤ stated 0.5 × |baseline median| |
PASSED |