oxideav-h264 0.1.9

Pure-Rust H.264 / AVC (ITU-T H.264 | ISO/IEC 14496-10) codec for oxideav — under spec-driven rewrite, no decode/encode functionality yet
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
//! Annex D — SEI payload parsing.
//!
//! Wraps the envelope output from `crate::non_vcl::parse_sei_rbsp`,
//! parsing the raw payload bytes into typed structs for the common
//! payload types. Each parser consumes exactly `payload_size` bytes.
//!
//! Spec cross-reference (Rec. ITU-T H.264 (08/2024)):
//!
//! | payload_type | Syntax      | Semantics   | Meaning                            |
//! | ------------ | ----------- | ----------- | ---------------------------------- |
//! | 0            | §D.1.2      | §D.2.2      | buffering_period                   |
//! | 1            | §D.1.3      | §D.2.3      | pic_timing                         |
//! | 2            | §D.1.4      | §D.2.4      | pan_scan_rect                      |
//! | 3            | §D.1.5      | §D.2.5      | filler_payload                     |
//! | 4            | §D.1.6      | §D.2.6      | user_data_registered_itu_t_t35     |
//! | 5            | §D.1.7      | §D.2.7      | user_data_unregistered             |
//! | 6            | §D.1.8      | §D.2.8      | recovery_point                     |
//! | 7            | §D.1.9      | §D.2.9      | dec_ref_pic_marking_repetition     |
//! | 8            | §D.1.10     | §D.2.10     | spare_pic                          |
//! | 9            | §D.1.11     | §D.2.11     | scene_info                         |
//! | 10           | §D.1.11†    | §D.2.11†    | sub_seq_info                       |
//! | 11           | §D.1.12†    | §D.2.12†    | sub_seq_layer_characteristics      |
//! | 12           | §D.1.13†    | §D.2.13†    | sub_seq_characteristics            |
//! | 13           | §D.1.15     | §D.2.15     | full_frame_freeze                  |
//! | 14           | §D.1.16     | §D.2.16     | full_frame_freeze_release          |
//! | 15           | §D.1.17     | §D.2.17     | full_frame_snapshot                |
//! | 16           | §D.1.18     | §D.2.18     | progressive_refinement_segment_start |
//! | 17           | §D.1.19     | §D.2.19     | progressive_refinement_segment_end |
//! | 18           | §D.1.20     | §D.2.20     | motion_constrained_slice_group_set |
//! | 19           | §D.1.21     | §D.2.21     | film_grain_characteristics         |
//! | 20           | §D.1.22     | §D.2.22     | deblocking_filter_display_preference |
//! | 21           | §D.1.23     | §D.2.23     | stereo_video_info                  |
//! | 22           | §D.1.24     | §D.2.24     | post_filter_hint                   |
//! | 23           | §D.1.25     | §D.2.25     | tone_mapping_info                  |
//! | 39           | §G.13.1.4   | §G.13.2.4   | multiview_scene_info (Annex G)     |
//! | 41           | §G.13.1.6   | §G.13.2.6   | non_required_view_component (Annex G) |
//! | 43           | §G.13.1.8   | §G.13.2.8   | operation_point_not_present (Annex G) |
//! | 45           | §D.1.26     | §D.2.26     | frame_packing_arrangement          |
//! | 46           | §G.13.1.10  | §G.13.2.10  | multiview_view_position (Annex G)  |
//! | 47           | §D.1.27     | §D.2.27     | display_orientation                |
//! | 50           | §H.13.1.3   | §H.13.2.3   | depth_representation_info (Annex H) |
//! | 51           | §H.13.1.4   | §H.13.2.4   | three_dimensional_reference_displays_info (Annex H) |
//! | 52           | §H.13.1.5   | §H.13.2.5   | depth_timing (Annex H)             |
//! | 53           | §H.13.1.7   | §H.13.2.7   | depth_sampling_info (Annex H) |
//! | 54           | §I.13.1.1   | §I.13.2.1   | constrained_depth_parameter_set_identifier (Annex I) |
//! | 137          | §D.1.29     | §D.2.29     | mastering_display_colour_volume    |
//! | 142          | §D.1.30     | §D.2.30     | colour_remapping_info              |
//! | 144          | §D.1.31     | §D.2.31     | content_light_level_info           |
//! | 147          | §D.1.32     | §D.2.32     | alternative_transfer_characteristics |
//! | 148          | §D.1.34     | §D.2.34     | ambient_viewing_environment        |
//! | 150          | §D.1.35.1   | §D.2.35.1   | equirectangular_projection         |
//! | 151          | §D.1.35.2   | §D.2.35.2   | cubemap_projection                 |
//! | 154          | §D.1.35.3   | §D.2.35.3   | sphere_rotation                    |
//! | 155          | §D.1.35.4   | §D.2.35.4   | regionwise_packing                 |
//! | 156          | §D.1.35.5   | §D.2.35.5   | omni_viewport                      |
//! | 205          | §D.1.38     | §D.2.38     | shutter_interval_info              |
//!
//! † The sub-sequence SEI family (types 10/11/12) is specified in the
//!   2003 draft (`docs/video/h264/ISO_IEC_14496-10-AVC-2003-draft.pdf`,
//!   §D.1.11–§D.1.13 / §D.2.11–§D.2.13). It was removed from later
//!   editions; the section numbers above are the 2003-draft numbering.
//!
//! Round-158 sub-parser: `parse_atsc1_envelope` decodes the typed ATSC1
//! envelope (provider_code = 0x0031 + user_identifier in {GA94, DTG1})
//! that real-world streams put inside a payload-type-4
//! `user_data_registered_itu_t_t35` with country_code = 0xB5 (USA). See
//! the `parse_atsc1_envelope` doc comment for the wire layout and the
//! ATSC A/53 Part 4 §6.2.3 reference. The CEA-708 cc_data() inner byte
//! layout is intentionally surfaced opaquely (CEA-708 spec is not in the
//! docs tree).

#![allow(dead_code)]

use crate::bitstream::{BitError, BitReader};
use crate::slice_header::{DecRefPicMarking, MmcoOp};
use crate::vui::{HrdParameters, VuiError};

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum SeiError {
    #[error("bitstream read failed: {0}")]
    Bitstream(#[from] BitError),
    #[error("hrd_parameters() parse failed: {0}")]
    Hrd(#[from] VuiError),
    #[error("SEI payload type {0} is not implemented — caller should keep the raw bytes")]
    UnsupportedPayloadType(u32),
    #[error("user_data_unregistered payload must be at least 16 bytes for the UUID (got {0})")]
    UserDataTooShort(usize),
    #[error("filler_payload contains a byte that is not 0xFF at position {0}")]
    FillerNonFfAt(usize),
    #[error("user_data_registered_itu_t_t35 payload must contain at least the country code byte")]
    RegisteredUserDataMissingCountryCode,
    #[error("pan_scan_rect_cnt_minus1 must be < 32 per §D.2.4 (got {0})")]
    PanScanCountTooLarge(u32),
    #[error(
        "film_grain num_model_values_minus1 must be in 0..=5 per §D.2.21 (got {got} for component {component})"
    )]
    FilmGrainNumModelValuesOutOfRange { component: usize, got: u8 },
    #[error(
        "post_filter_hint filter_hint_size_x / filter_hint_size_y must be in 1..=15 per §D.2.24 (got x={x}, y={y})"
    )]
    PostFilterHintSizeOutOfRange { x: u32, y: u32 },
    #[error("post_filter_hint filter_hint_type 3 is reserved per §D.2.24")]
    PostFilterHintTypeReserved,
    #[error(
        "post_filter_hint filter_hint_type 1 (two 1D FIRs) requires filter_hint_size_y == 2 per §D.2.24 (got {0})"
    )]
    PostFilterHintType1WrongHeight(u32),
    #[error("ambient_viewing_environment ambient_illuminance shall not be 0 per §D.2.34")]
    AmbientIlluminanceZero,
    #[error(
        "ambient_viewing_environment ambient_light_x / ambient_light_y shall be in 0..=50000 per §D.2.34 (got x={x}, y={y})"
    )]
    AmbientLightOutOfRange { x: u32, y: u32 },
    #[error(
        "sphere_rotation yaw_rotation shall be in [-180*2^16, 180*2^16 - 1] per §D.2.35.3 (got {0})"
    )]
    SphereRotationYawOutOfRange(i32),
    #[error(
        "sphere_rotation pitch_rotation shall be in [-90*2^16, 90*2^16] per §D.2.35.3 (got {0})"
    )]
    SphereRotationPitchOutOfRange(i32),
    #[error(
        "sphere_rotation roll_rotation shall be in [-180*2^16, 180*2^16 - 1] per §D.2.35.3 (got {0})"
    )]
    SphereRotationRollOutOfRange(i32),
    #[error(
        "omni_viewport azimuth_centre shall be in [-180*2^16, 180*2^16 - 1] per §D.2.35.5 (got {0})"
    )]
    OmniViewportAzimuthOutOfRange(i32),
    #[error(
        "omni_viewport elevation_centre shall be in [-90*2^16, 90*2^16] per §D.2.35.5 (got {0})"
    )]
    OmniViewportElevationOutOfRange(i32),
    #[error(
        "omni_viewport tilt_centre shall be in [-180*2^16, 180*2^16 - 1] per §D.2.35.5 (got {0})"
    )]
    OmniViewportTiltOutOfRange(i32),
    #[error("omni_viewport hor_range shall be in 1..=360*2^16 per §D.2.35.5 (got {0})")]
    OmniViewportHorRangeOutOfRange(u32),
    #[error("omni_viewport ver_range shall be in 1..=180*2^16 per §D.2.35.5 (got {0})")]
    OmniViewportVerRangeOutOfRange(u32),
    #[error("shutter_interval_info sii_time_scale shall not be 0 per §D.2.38")]
    ShutterIntervalTimeScaleZero,
    #[error("sub-sequence sub_seq_layer_num shall be less than 256 (got {0})")]
    SubSeqLayerNumTooLarge(u32),
    #[error("sub-sequence sub_seq_id shall be less than 65536 (got {0})")]
    SubSeqIdTooLarge(u32),
    #[error("sub_seq_layer_characteristics num_sub_seq_layers_minus1 shall be <= 255 (got {0})")]
    SubSeqLayersTooLarge(u32),
    #[error("sub_seq_characteristics num_referenced_subseqs shall be less than 256 (got {0})")]
    SubSeqReferencedTooLarge(u32),
    #[error("spare_pic num_spare_pics_minus1 shall be in 0..=15 per §D.2.10 (got {0})")]
    SparePicCountTooLarge(u32),
    #[error(
        "motion_constrained_slice_group_set num_slice_groups_in_set_minus1 shall be in 0..=num_slice_groups_minus1 per §D.2.20 (got count {count}, num_slice_groups {num_slice_groups})"
    )]
    MotionConstrainedSliceGroupCountTooLarge { count: u32, num_slice_groups: u32 },
    #[error(
        "motion_constrained_slice_group_set slice_group_id[{i}] shall be in 0..=num_slice_groups_minus1 per §D.2.20 (got {id}, num_slice_groups {num_slice_groups})"
    )]
    MotionConstrainedSliceGroupIdOutOfRange {
        i: usize,
        id: u32,
        num_slice_groups: u32,
    },
    #[error(
        "spare_pic spare_area_idc shall be in 0..=2 per §D.2.10 (got {idc} for spare picture {i})"
    )]
    SparePicAreaIdcOutOfRange { i: usize, idc: u32 },
    #[error(
        "spare_pic spare_area_idc {idc} (spare picture {i}) needs PicSizeInMapUnits from the active SPS, but the SeiContext carried 0 — wire pic_size_in_map_units in"
    )]
    SparePicMapUnitsUnknown { i: usize, idc: u32 },
    #[error(
        "content_colour_volume requires at least one of the four present flags (primaries / min / max / avg) to be set per §D.2.33"
    )]
    ContentColourVolumeNoComponents,
    #[error(
        "content_colour_volume ccv_primaries_x/y[{c}] shall be in [-5000000, 5000000] per §D.2.33 (got x={x}, y={y})"
    )]
    ContentColourVolumePrimaryOutOfRange { c: usize, x: i32, y: i32 },
    #[error(
        "content_colour_volume luminance ordering violated per §D.2.33: ccv_min_luminance_value ({min}) <= ccv_avg_luminance_value ({avg}) <= ccv_max_luminance_value ({max}) must hold for whichever values are present"
    )]
    ContentColourVolumeLuminanceOrder { min: u64, avg: u64, max: u64 },
    #[error(
        "dec_ref_pic_marking_repetition memory_management_control_operation shall be in 0..=6 per §7.4.3.3 / Table 7-9 (got {0})"
    )]
    DecRefPicMarkingRepetitionMmcoOutOfRange(u32),
    #[error("regionwise_packing num_packed_regions shall be greater than 0 per §D.2.35.4")]
    RegionWisePackingNoRegions,
    #[error(
        "regionwise_packing proj_picture_width / proj_picture_height shall both be greater than 0 per §D.2.35.4 (got w={w}, h={h})"
    )]
    RegionWisePackingProjPictureZero { w: u32, h: u32 },
    #[error(
        "regionwise_packing packed_picture_width / packed_picture_height shall both be greater than 0 per §D.2.35.4 (got w={w}, h={h})"
    )]
    RegionWisePackingPackedPictureZero { w: u32, h: u32 },
    #[error(
        "regionwise_packing packed region {0} sets guard_band_flag but all four guard-band dimensions are 0 — at least one shall be greater than 0 per §D.2.35.4"
    )]
    RegionWisePackingGuardBandAllZero(usize),
    #[error(
        "colour_remapping_info colour_remap_repetition_period shall be in 0..=16384 per §D.2.30 (got {0})"
    )]
    ColourRemapRepetitionPeriodOutOfRange(u32),
    #[error(
        "colour_remapping_info colour_remap_{which}_bit_depth shall be in 8..=16 per §D.2.30 (got {got})"
    )]
    ColourRemapBitDepthOutOfRange { which: &'static str, got: u8 },
    #[error(
        "colour_remapping_info {which}_lut_num_val_minus1[{c}] shall be in 0..=32 per §D.2.30 (got {got})"
    )]
    ColourRemapLutCountOutOfRange {
        which: &'static str,
        c: usize,
        got: u8,
    },
    #[error(
        "colour_remapping_info colour_remap_coeffs shall be in [-32768, 32767] per §D.2.30 (got {0})"
    )]
    ColourRemapCoeffOutOfRange(i32),
    #[error(
        "sei_manifest manifest_sei_payload_type values shall be unique per §D.2.36 (got duplicate {payload_type} at indices {first} and {second})"
    )]
    SeiManifestDuplicatePayloadType {
        payload_type: u16,
        first: usize,
        second: usize,
    },
    #[error(
        "sei_prefix_indication num_bits_in_prefix_indication_minus1[{i}] plus 1 = {bits} bits exceeds the remaining payload (only {available} bits left) per §D.1.37"
    )]
    SeiPrefixIndicationOverflow {
        i: usize,
        bits: u32,
        available: usize,
    },
    #[error(
        "ATSC1 envelope requires at least 6 bytes (2 provider_code + 4 user_identifier) per ATSC A/53 Part 4 §6.2.3 (got {0})"
    )]
    Atsc1EnvelopeTooShort(usize),
    #[error(
        "ATSC1 envelope provider_code shall be 0x0031 (ATSC) per ATSC A/53 Part 4 §6.2.3 / SMPTE-RA T.35 registration (got 0x{0:04X})"
    )]
    Atsc1ProviderCodeMismatch(u16),
    #[error(
        "ATSC1 envelope user_identifier 0x{0:08X} is not a registered ATSC value per ATSC A/53 Part 4 §6.2.3 Table 6.7 (expected 'GA94' 0x47413934 or 'DTG1' 0x44544731)"
    )]
    Atsc1UnknownUserIdentifier(u32),
    #[error(
        "ATSC1 ATSC_user_data() requires at least 1 byte for user_data_type_code per A/53 Part 4 §6.2.3 Table 6.8 (got empty payload)"
    )]
    Atsc1AtscUserDataEmpty,
    #[error(
        "ATSC1 bar_data reserved field must be '1111' per A/53 Part 4 Table 6.11 (got 0b{0:04b})"
    )]
    Atsc1BarDataReservedMismatch(u8),
    #[error(
        "ATSC1 bar_data {which}_bar one_bits field must be '11' per A/53 Part 4 Table 6.11 (got 0b{got:02b})"
    )]
    Atsc1BarDataOneBitsMismatch { which: &'static str, got: u8 },
    #[error(
        "ATSC1 bar_data top_bar_flag and bottom_bar_flag must match per A/53 Part 4 §6.2.3.2 (top={top}, bottom={bottom})"
    )]
    Atsc1BarDataTopBottomMismatch { top: bool, bottom: bool },
    #[error(
        "ATSC1 bar_data left_bar_flag and right_bar_flag must match per A/53 Part 4 §6.2.3.2 (left={left}, right={right})"
    )]
    Atsc1BarDataLeftRightMismatch { left: bool, right: bool },
    #[error(
        "ATSC1 bar_data may not signal both letterbox (top/bottom) and pillarbox (left/right) at once per A/53 Part 4 §6.2.3.2"
    )]
    Atsc1BarDataLetterboxPillarboxBoth,
    #[error("ATSC1 bar_data payload truncated — needed {needed} bytes, got {got}")]
    Atsc1BarDataTruncated { needed: usize, got: usize },
    #[error(
        "multiview_view_position num_views_minus1 shall be in 0..=1023 per Annex G §G.13.2.10 (got {0})"
    )]
    MultiviewViewPositionNumViewsOutOfRange(u32),
    #[error(
        "multiview_view_position view_position[{i}] shall be in 0..=1023 per Annex G §G.13.2.10 (got {got})"
    )]
    MultiviewViewPositionViewPositionOutOfRange { i: usize, got: u32 },
    #[error(
        "multiview_scene_info max_disparity shall be in 0..=1023 per Annex G §G.13.2.4 (got {0})"
    )]
    MultiviewSceneInfoMaxDisparityOutOfRange(u32),
    #[error(
        "operation_point_not_present num_operation_points shall be in 0..=65536 per Annex G §G.13.2.8 (got {0})"
    )]
    OperationPointNotPresentCountOutOfRange(u32),
    #[error(
        "operation_point_not_present operation_point_not_present_id[{i}] shall be in 0..=65535 per Annex G §G.13.2.8 (got {got})"
    )]
    OperationPointNotPresentIdOutOfRange { i: usize, got: u32 },
    #[error(
        "base_view_temporal_hrd num_of_temporal_layers_in_base_view_minus1 shall be in 0..=7 per Annex G §G.13.2.9 (got {0})"
    )]
    BaseViewTemporalHrdLayerCountOutOfRange(u32),
    #[error(
        "non_required_view_component num_info_entries_minus1 shall be in 0..=1022 per Annex G §G.13.2.6 (num_views_minus1 - 1, with num_views_minus1 bounded at 1023) (got {0})"
    )]
    NonRequiredViewComponentNumInfoEntriesOutOfRange(u32),
    #[error(
        "non_required_view_component view_order_index[{i}] shall be in 1..=1023 per Annex G §G.13.2.6 (got {got})"
    )]
    NonRequiredViewComponentViewOrderIndexOutOfRange { i: usize, got: u32 },
    #[error(
        "non_required_view_component num_non_required_view_components_minus1[{i}] shall be in 0..=view_order_index[i]-1 per Annex G §G.13.2.6 (got {got}, view_order_index={view_order_index})"
    )]
    NonRequiredViewComponentCountOutOfRange {
        i: usize,
        got: u32,
        view_order_index: u32,
    },
    #[error(
        "non_required_view_component index_delta_minus1[{i}][{j}] shall be in 0..=view_order_index[i]-1 per Annex G §G.13.2.6 (got {got}, view_order_index={view_order_index})"
    )]
    NonRequiredViewComponentIndexDeltaOutOfRange {
        i: usize,
        j: usize,
        got: u32,
        view_order_index: u32,
    },
    #[error(
        "multiview_acquisition_info num_views_minus1 shall be in 0..=1023 per Annex G §G.13.2.5 (got {0})"
    )]
    MultiviewAcquisitionInfoNumViewsOutOfRange(u32),
    #[error(
        "multiview_acquisition_info {field} shall be in 0..=31 per Annex G §G.13.2.5 (got {got})"
    )]
    MultiviewAcquisitionInfoPrecOutOfRange { field: &'static str, got: u32 },
    #[error(
        "three_dimensional_reference_displays_info {field} shall be in 0..=31 per Annex H §H.13.2.4 (got {got})"
    )]
    ThreeDimensionalReferenceDisplaysInfoPrecOutOfRange { field: &'static str, got: u32 },
    #[error(
        "three_dimensional_reference_displays_info num_ref_displays_minus1 shall be in 0..=31 per Annex H §H.13.2.4 (got {0})"
    )]
    ThreeDimensionalReferenceDisplaysInfoNumRefDisplaysOutOfRange(u32),
    #[error(
        "depth_representation_info num_views_minus1 shall be in 0..=1023 per Annex H §H.13.2.3 (got {0})"
    )]
    DepthRepresentationInfoNumViewsOutOfRange(u32),
    #[error(
        "depth_representation_info {field} shall be in 0..=1023 per Annex H §H.13.2.3 (got {got})"
    )]
    DepthRepresentationInfoViewIdOutOfRange { field: &'static str, got: u32 },
    #[error(
        "depth_representation_info depth_nonlinear_representation_num_minus1 shall be in 0..=62 per Annex H §H.13.2.3 (got {0})"
    )]
    DepthRepresentationInfoNonlinearNumOutOfRange(u32),
    #[error(
        "depth_representation_info depth_nonlinear_representation_model[{i}] shall be in 0..=65535 per Annex H §H.13.2.3 (got {got})"
    )]
    DepthRepresentationInfoNonlinearModelOutOfRange { i: usize, got: u32 },
    #[error(
        "constrained_depth_parameter_set_identifier max_dps_id shall be in 0..=62 per Annex I §I.13.2.1 (depth_parameter_set_id range 1..=63 caps max_dps_id + 1 ≤ 63) (got {0})"
    )]
    ConstrainedDepthParameterSetIdentifierMaxDpsIdOutOfRange(u32),
    #[error(
        "constrained_depth_parameter_set_identifier max_dps_id_diff * 2 shall be less than max_dps_id per Annex I §I.13.2.1 (got max_dps_id_diff = {max_dps_id_diff}, max_dps_id = {max_dps_id})"
    )]
    ConstrainedDepthParameterSetIdentifierDiffViolatesBound {
        max_dps_id_diff: u32,
        max_dps_id: u32,
    },
    #[error("depth_sampling_info dttsr_{axis}_mul = 0 is reserved per Annex H §H.13.2.7")]
    DepthSamplingInfoDttsrMulReserved { axis: &'static str },
    #[error(
        "depth_sampling_info num_video_plus_depth_views_minus1 shall be in 0..=1023 per Annex H §H.13.2.7 (num_views_minus1 absolute upper bound from Annex G/H) (got {0})"
    )]
    DepthSamplingInfoNumViewsOutOfRange(u32),
    #[error(
        "depth_sampling_info depth_grid_view_id[{i}] shall be in 0..=1023 per Annex H §H.13.2.7 (view_id range) (got {got})"
    )]
    DepthSamplingInfoViewIdOutOfRange { i: usize, got: u32 },
    #[error(
        "depth_timing per_view_depth_timing_flag = 1 needs NumDepthViews from the active subset SPS MVCD extension (§H.7.3.2.1.5) to bound the per-view loop per Annex H §H.13.1.5, but SeiContext.num_depth_views is 0 (unknown)"
    )]
    DepthTimingNumDepthViewsUnknown,
    #[error(
        "depth_timing NumDepthViews shall be in 1..=1024 per Annex H (§H.7.3.2.1.5 num_views_minus1 ≤ 1023 caps the depth-view count) (got {0})"
    )]
    DepthTimingNumDepthViewsOutOfRange(u32),
    #[error(
        "alternative_depth_info num_constituent_views_gvd_minus1 shall be in 0..=3 per Annex H §H.13.2.6 (got {0})"
    )]
    AlternativeDepthInfoNumConstituentViewsOutOfRange(u32),
    #[error("alternative_depth_info {field} shall be in 0..=31 per Annex H §H.13.2.6 (got {got})")]
    AlternativeDepthInfoPrecOutOfRange { field: &'static str, got: u32 },
    #[error(
        "view_dependency_change with anchor_update_flag / non_anchor_update_flag set needs the per-view inter-view reference counts from the active MVC subset SPS (§G.7.3.2.1.4) to bound the anchor_ref / non_anchor_ref flag loops per Annex G §G.13.1.7, but SeiContext.mvc_view_ref_counts is empty (unknown)"
    )]
    ViewDependencyChangeRefCountsUnknown,
    #[error(
        "view_dependency_change seq_parameter_set_id shall be in 0..=31 per Annex G §G.13.2.7 (got {0})"
    )]
    ViewDependencyChangeSpsIdOutOfRange(u32),
}

/// §D.2.2 — buffering_period.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BufferingPeriod {
    pub seq_parameter_set_id: u32,
    pub nal_hrd: Option<Vec<CpbRemovalDelay>>, // per SchedSelIdx
    pub vcl_hrd: Option<Vec<CpbRemovalDelay>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CpbRemovalDelay {
    pub initial_cpb_removal_delay: u32,
    pub initial_cpb_removal_delay_offset: u32,
}

/// §D.2.3 — pic_timing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PicTiming {
    pub cpb_removal_delay: u32,
    pub dpb_output_delay: u32,
    pub pic_struct: Option<PicStructInfo>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PicStructInfo {
    pub pic_struct: u8, // 0..=8
    pub clock_timestamps: Vec<Option<ClockTimestamp>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ClockTimestamp {
    pub ct_type: u8,
    pub nuit_field_based_flag: bool,
    pub counting_type: u8,
    pub full_timestamp_flag: bool,
    pub discontinuity_flag: bool,
    pub cnt_dropped_flag: bool,
    pub n_frames: u8,
    pub seconds: u8,
    pub minutes: u8,
    pub hours: u8,
    pub time_offset: i32,
}

/// §D.2.7 — user_data_unregistered.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserDataUnregistered {
    pub uuid: [u8; 16],
    pub user_data: Vec<u8>,
}

/// §D.2.6 — user_data_registered_itu_t_t35.
///
/// The single most common SEI in real-world H.264 streams: ATSC A/53
/// closed captions, AFD, ATSC bar data, HDR10+ dynamic metadata, and
/// Dolby Vision RPU all ride on this payload, distinguished by their
/// `(country_code, country_code_extension)` pair plus the leading bytes
/// of `payload_bytes`. We surface the raw triple — the per-vendor
/// payload format is out of scope for this layer (callers can switch
/// on `country_code` / `country_code_extension` and parse `payload_bytes`
/// against the relevant T.35 terminal-provider spec).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserDataRegisteredItuTT35 {
    /// itu_t_t35_country_code (u(8)). 0xB5 = USA, 0x26 = China, etc.
    pub country_code: u8,
    /// itu_t_t35_country_code_extension_byte (u(8)). Only present when
    /// `country_code == 0xFF` (per §D.1.6); `None` otherwise.
    pub country_code_extension: Option<u8>,
    /// Remaining `itu_t_t35_payload_byte`s after the country code(s).
    pub payload_bytes: Vec<u8>,
}

/// §D.2.4 — pan_scan_rect.
///
/// Carries one or more rectangles signalling the producer's preferred
/// display crop (commonly used to letterbox/pillarbox 4:3 content
/// inside a 16:9 frame, or vice-versa). Each rectangle's offsets are
/// expressed in units of 1/16 sample (per §D.2.4) relative to the
/// coded picture rectangle, and a positive offset shrinks the rectangle
/// from that edge.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PanScanRect {
    pub pan_scan_rect_id: u32,
    pub cancel_flag: bool,
    /// Present only when `cancel_flag == false`.
    pub body: Option<PanScanRectBody>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PanScanRectBody {
    /// One entry per rectangle (length = pan_scan_cnt_minus1 + 1).
    pub rects: Vec<PanScanRectOffsets>,
    pub repetition_period: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PanScanRectOffsets {
    pub left_offset: i32,
    pub right_offset: i32,
    pub top_offset: i32,
    pub bottom_offset: i32,
}

/// §D.2.8 — recovery_point.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RecoveryPoint {
    pub recovery_frame_cnt: u32,
    pub exact_match_flag: bool,
    pub broken_link_flag: bool,
    pub changing_slice_group_idc: u8,
}

/// §D.2.15 — full_frame_freeze.
///
/// Asks the display layer to freeze the most recent output picture
/// for `full_frame_freeze_repetition_period` decoded pictures (in
/// output order); see §D.2.15 for the persistence semantics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FullFrameFreeze {
    pub full_frame_freeze_repetition_period: u32,
}

/// §D.2.17 — full_frame_snapshot.
///
/// Tags the current decoded picture as a still snapshot identified
/// by `snapshot_id` (typically used to mark a frame for
/// thumbnail / KLV-style external indexing).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FullFrameSnapshot {
    pub snapshot_id: u32,
}

/// §D.2.22 — deblocking_filter_display_preference.
///
/// Producer hint for the display layer about whether to display the
/// pre-deblocking-filter or post-deblocking-filter samples. When
/// `cancel_flag` is true the body is absent (see §D.2.22).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeblockingFilterDisplayPreference {
    pub cancel_flag: bool,
    pub display_prior_to_deblocking_preferred_flag: bool,
    pub dec_frame_buffering_constraint_flag: bool,
    pub repetition_period: u32,
}

/// §D.2.29 — mastering_display_colour_volume (HDR10).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MasteringDisplayColourVolume {
    /// 3 primaries, each (x, y) in 0..=50000 range.
    pub display_primaries: [(u16, u16); 3],
    pub white_point: (u16, u16),
    pub max_display_mastering_luminance: u32,
    pub min_display_mastering_luminance: u32,
}

/// §D.2.31 — content_light_level_info (HDR10).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContentLightLevelInfo {
    pub max_content_light_level: u16,
    pub max_pic_average_light_level: u16,
}

/// §D.2.26 — frame_packing_arrangement. Stereoscopic 3D packing info.
///
/// When `cancel_flag` is true the message only carries the
/// identifier plus the trailing `extension_flag`; all other fields
/// default to zero and should be ignored by consumers.
///
/// When `quincunx_sampling_flag == 1` or `ty == 5` (temporal
/// interleaving) the four grid-position nibbles are not transmitted
/// and remain zero.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FramePackingArrangement {
    pub id: u32,
    pub cancel_flag: bool,
    pub ty: u8, // 0..=7 packing arrangement type (u(7))
    pub quincunx_sampling_flag: bool,
    pub content_interpretation_type: u8,
    pub spatial_flipping_flag: bool,
    pub frame0_flipped_flag: bool,
    pub field_views_flag: bool,
    pub current_frame_is_frame0_flag: bool,
    pub frame0_self_contained_flag: bool,
    pub frame1_self_contained_flag: bool,
    pub frame0_grid_position_x: u8,
    pub frame0_grid_position_y: u8,
    pub frame1_grid_position_x: u8,
    pub frame1_grid_position_y: u8,
    pub reserved_byte: u8,
    pub repetition_period: u32,
    pub extension_flag: bool,
}

/// §D.2.27 — display_orientation.
///
/// When `cancel_flag` is true all other fields are omitted from the
/// bitstream and default to zero.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DisplayOrientation {
    pub cancel_flag: bool,
    pub hor_flip: bool,
    pub ver_flip: bool,
    /// u(16). Interpreted as counter-clockwise rotation in
    /// units of 360 / 2^16 degrees (per §D.2.27).
    pub anticlockwise_rotation: u16,
    pub repetition_period: u32,
    pub extension_flag: bool,
}

/// §D.2.24 — post_filter_hint.
///
/// Carries either FIR filter coefficients (1-D pair or 2-D) or a
/// cross-correlation matrix between the original and the decoded
/// signal, intended as a post-processing step after the picture is
/// decoded, cropped, and output. One coefficient set per colour
/// component (Y / Cb / Cr).
///
/// Per §D.2.24 the constraints policed here are:
/// * `filter_hint_size_x`, `filter_hint_size_y` shall be in 1..=15;
/// * `filter_hint_type` shall be in 0..=2 (3 is reserved);
/// * `filter_hint_type == 1` (two 1-D FIRs) implies
///   `filter_hint_size_y == 2`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PostFilterHint {
    pub filter_hint_size_x: u32,
    pub filter_hint_size_y: u32,
    pub filter_hint_type: u8,
    /// `filter_hint_value[cIdx][cy][cx]` flattened in spec order:
    /// outer = component (0..3), middle = `cy` (0..size_y), inner =
    /// `cx` (0..size_x). Length = `3 * size_y * size_x`.
    pub filter_hint_value: Vec<i32>,
    pub additional_extension_flag: bool,
}

/// §D.2.25 — tone_mapping_info. HDR→SDR mapping parameters.
///
/// When `cancel_flag` is true no further fields are transmitted
/// (the body is absent). Otherwise the `model` enum captures the
/// model-specific payload keyed by `tone_map_model_id`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToneMappingInfo {
    pub tone_map_id: u32,
    pub cancel_flag: bool,
    pub body: Option<ToneMappingBody>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToneMappingBody {
    pub repetition_period: u32,
    pub coded_data_bit_depth: u8,
    pub target_bit_depth: u8,
    pub model_id: u32,
    pub model: ToneMappingModel,
}

/// §D.2.25 — per-`tone_map_model_id` payload.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToneMappingModel {
    /// model_id = 0 — linear mapping in `[min_value, max_value]`.
    Linear { min_value: u32, max_value: u32 },
    /// model_id = 1 — sigmoid mapping.
    Sigmoid {
        sigmoid_midpoint: u32,
        sigmoid_width: u32,
    },
    /// model_id = 2 — piecewise mapping via interval start values.
    ///
    /// Per §D.2.25 each entry is
    /// `((coded_data_bit_depth + 7) >> 3) << 3` bits wide and the
    /// array has `1 << target_bit_depth` entries.
    StartOfCodedInterval { start_of_coded_interval: Vec<u32> },
    /// model_id = 3 — piecewise linear mapping via pivot pairs.
    ///
    /// Pivot values use
    /// `((coded_data_bit_depth + 7) >> 3) << 3` bits for
    /// `coded_pivot_value` and
    /// `((target_bit_depth + 7) >> 3) << 3` bits for
    /// `target_pivot_value`.
    PiecewisePivots {
        num_pivots: u16,
        coded_pivot_value: Vec<u32>,
        target_pivot_value: Vec<u32>,
    },
    /// Reserved for future use (model_id >= 4 — e.g. model_id 4 is
    /// the ISO-speed / exposure parameter set which this first pass
    /// does not decode).
    Reserved {
        model_id: u32,
        remaining_bytes: Vec<u8>,
    },
}

impl ToneMappingBody {
    /// §D.2.25 — the two normative *default end points* of the
    /// piece-wise linear mapping function (`tone_map_model_id == 3`).
    ///
    /// The spec defines `num_pivots` as
    ///
    /// > *"the number of pivot points in the piece-wise linear
    /// > mapping function without counting the two default end
    /// > points, (0, 0) and
    /// > (2^coded_data_bit_depth − 1, 2^target_bit_depth − 1)."*
    ///
    /// Only the interior `num_pivots` pivots carry signalled
    /// `(coded_pivot_value[i], target_pivot_value[i])` pairs; the two
    /// end points are implicit and never appear in the bitstream.
    /// This accessor materialises them as `(coded, target)` pairs in
    /// the same `(coded_pivot_value, target_pivot_value)` domain as
    /// the stored interior pivots, so a caller assembling the full
    /// mapping curve can prepend the start point, append the end
    /// point, and interpolate across all `num_pivots + 2` points.
    ///
    /// Returns `None` unless `tone_map_model_id == 3` — the implicit
    /// end points are defined only for the piece-wise linear model;
    /// no inferred end points exist for the linear / sigmoid /
    /// user-table / reserved models.
    ///
    /// The end value `(2^coded_data_bit_depth − 1,
    /// 2^target_bit_depth − 1)` is computed from the body's
    /// `coded_data_bit_depth` (range 8..=14 ⇒ coded end 255..=16383)
    /// and `target_bit_depth` (range 1..=16 ⇒ target end 1..=65535);
    /// both fit in `u32`, the same width the interior pivots are
    /// stored in.
    #[must_use]
    pub fn piecewise_default_end_points(&self) -> Option<((u32, u32), (u32, u32))> {
        if self.model_id != 3 {
            return None;
        }
        // Saturating shifts keep this total even for out-of-spec
        // bit depths (the parser already range-checks the legal
        // 8..=14 / 1..=16 windows, but the accessor must not panic
        // on a hand-constructed body).
        let coded_end = 1u32
            .checked_shl(u32::from(self.coded_data_bit_depth))
            .map_or(u32::MAX, |v| v - 1);
        let target_end = 1u32
            .checked_shl(u32::from(self.target_bit_depth))
            .map_or(u32::MAX, |v| v - 1);
        Some(((0, 0), (coded_end, target_end)))
    }

    /// §D.2.25 — the total number of pivot points in the piece-wise
    /// linear mapping curve, **including** the two implicit default
    /// end points `(0, 0)` and
    /// `(2^coded_data_bit_depth − 1, 2^target_bit_depth − 1)`.
    ///
    /// The bitstream signals only the interior `num_pivots` points
    /// (§D.2.25: `num_pivots` is counted *without* the two end
    /// points), so the assembled curve has `num_pivots + 2` points.
    ///
    /// Returns `None` unless `tone_map_model_id == 3`. With the
    /// on-wire `num_pivots` being u(16) (0..=65535) the total lies in
    /// `2..=65537`, which is why the return widens to `u32`.
    #[must_use]
    pub fn piecewise_total_pivot_count(&self) -> Option<u32> {
        match &self.model {
            ToneMappingModel::PiecewisePivots { num_pivots, .. } => {
                Some(u32::from(*num_pivots) + 2)
            }
            _ => None,
        }
    }
}

/// §D.2.21 — film_grain_characteristics.
///
/// Fully decodes the §D.1.21 / §D.2.21 syntax including the per-
/// component intensity-interval body (model_id 0 — frequency filtering;
/// model_id 1 — auto-regression). For each component flagged in
/// `comp_model_present_flag`, the resulting `FilmGrainComponent` carries
/// the `num_model_values_minus1`, the per-interval `(lower_bound,
/// upper_bound)` plus the `comp_model_value[i][j]` matrix as signed
/// integers, and the trailing
/// `film_grain_characteristics_repetition_period` is exposed on the
/// body for callers that need to know whether the message persists.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilmGrainCharacteristics {
    pub cancel_flag: bool,
    pub body: Option<FilmGrainBody>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilmGrainBody {
    pub model_id: u8, // u(2)
    pub separate_colour_description_present_flag: bool,
    pub separate_colour_description: Option<FilmGrainSeparateColourDescription>,
    pub blending_mode_id: u8,  // u(2)
    pub log2_scale_factor: u8, // u(4)
    /// One flag per colour component (c = 0..=2). When the flag is set
    /// the corresponding entry in [`Self::components`] is `Some(_)`.
    pub comp_model_present_flag: [bool; 3],
    /// Per-component model parameters, in the same order as
    /// `comp_model_present_flag` (Y, Cb, Cr). An entry is `Some(_)`
    /// iff the matching present-flag is `true`.
    pub components: [Option<FilmGrainComponent>; 3],
    /// `film_grain_characteristics_repetition_period`. 0 = applies
    /// only to the current decoded picture; 1 = persists until the
    /// end of the coded video sequence (or until cancelled / replaced);
    /// values > 1 mean expect the next message within this many
    /// output-order pictures (see §D.2.21).
    pub repetition_period: u32,
}

/// §D.1.21 / §D.2.21 — per-component film-grain model parameters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilmGrainComponent {
    /// `num_model_values_minus1[c]`. The number of `comp_model_value`
    /// entries per intensity interval is this value plus one. Per
    /// §D.2.21 must be in 0..=5.
    pub num_model_values_minus1: u8,
    /// One entry per intensity interval (length =
    /// `num_intensity_intervals_minus1[c] + 1`).
    pub intensity_intervals: Vec<FilmGrainIntensityInterval>,
}

/// §D.1.21 — one intensity interval's bounds plus its model values.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilmGrainIntensityInterval {
    pub lower_bound: u8,
    pub upper_bound: u8,
    /// `comp_model_value[c][i][j]` for j = 0..=num_model_values_minus1.
    pub model_values: Vec<i32>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FilmGrainSeparateColourDescription {
    pub bit_depth_luma_minus8: u8,   // u(3)
    pub bit_depth_chroma_minus8: u8, // u(3)
    pub full_range_flag: bool,
    pub colour_primaries: u8,
    pub transfer_characteristics: u8,
    pub matrix_coefficients: u8,
}

impl FilmGrainSeparateColourDescription {
    /// §D.2.21 — luma bit depth of the simulated film grain signal,
    /// reconstructed from the on-wire `film_grain_bit_depth_luma_minus8`
    /// field.
    ///
    /// The spec text reads: "*film_grain_bit_depth_luma_minus8 plus 8
    /// specifies the bit depth used for the luma component of the film
    /// grain characteristics specified in the SEI message*". The
    /// derivation is `filmGrainBitDepth[0] =
    /// film_grain_bit_depth_luma_minus8 + 8` (eq. D-14). This accessor
    /// surfaces that `filmGrainBitDepth[0]` semantic directly rather
    /// than the biased on-wire `_minus8` carrier.
    ///
    /// The on-wire field is u(3) (0..=7), so the result is in 8..=15 and
    /// always fits in `u8`. The return is unconditional (not `Option`)
    /// because the field is only present — and this struct only
    /// constructed — when `separate_colour_description_present_flag ==
    /// 1`; the §D.2.21 "not present ⇒ inferred to bit_depth_luma_minus8"
    /// rule applies to the absent-struct case, which is represented by
    /// [`FilmGrainBody::separate_colour_description`] being `None`.
    #[must_use]
    pub fn bit_depth_luma(&self) -> u8 {
        self.bit_depth_luma_minus8 + 8
    }

    /// §D.2.21 — chroma (Cb / Cr) bit depth of the simulated film grain
    /// signal, reconstructed from the on-wire
    /// `film_grain_bit_depth_chroma_minus8` field.
    ///
    /// The spec text reads: "*film_grain_bit_depth_chroma_minus8 plus 8
    /// specifies the bit depth used for the Cb and Cr components of the
    /// film grain characteristics specified in the SEI message*". The
    /// derivation is `filmGrainBitDepth[c] =
    /// film_grain_bit_depth_chroma_minus8 + 8` for c = 1, 2 (eq. D-15).
    /// This accessor surfaces that shared `filmGrainBitDepth[1] ==
    /// filmGrainBitDepth[2]` semantic directly rather than the biased
    /// on-wire `_minus8` carrier.
    ///
    /// The on-wire field is u(3) (0..=7), so the result is in 8..=15 and
    /// always fits in `u8`. The return is unconditional (not `Option`)
    /// for the same reason as [`Self::bit_depth_luma`]: the §D.2.21
    /// "not present ⇒ inferred" rule is represented by the carrying
    /// struct being `None`, not by an in-struct sentinel.
    #[must_use]
    pub fn bit_depth_chroma(&self) -> u8 {
        self.bit_depth_chroma_minus8 + 8
    }
}

/// Inputs a SEI parser needs beyond the raw payload bytes. Many parsers
/// depend on VUI fields (CpbDpbDelaysPresentFlag, pic_struct_present_flag,
/// Per-view inter-view-reference counts taken from the active MVC
/// subset SPS (§G.7.3.2.1.4 `seq_parameter_set_mvc_extension()`),
/// needed by §G.13.1.7 `view_dependency_change()` to bound its
/// per-view `anchor_ref_lX_flag[i][j]` / `non_anchor_ref_lX_flag[i][j]`
/// loops.
///
/// One [`MvcViewRefCounts`] is recorded per view index `i` in the
/// MVC dependency range `1..=num_views_minus1`; the base view (VOIdx
/// 0) is excluded because §G.7.3.2.1.4 starts that loop at `i = 1`
/// and the §G.13.1.7 loops do likewise. Therefore the populated
/// vector length equals `num_views_minus1` (Eq.: index 0 of the
/// vector = view `i = 1`).
///
/// Each count is the value of `num_anchor_refs_lX[i]` /
/// `num_non_anchor_refs_lX[i]`, which §G.7.4.2.1.4 bounds to
/// `Min( 15, num_views_minus1 )`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct MvcViewRefCounts {
    /// `num_anchor_refs_l0[i]` — bounded by `Min(15, num_views_minus1)`.
    pub num_anchor_refs_l0: u8,
    /// `num_anchor_refs_l1[i]` — bounded by `Min(15, num_views_minus1)`.
    pub num_anchor_refs_l1: u8,
    /// `num_non_anchor_refs_l0[i]` — bounded by `Min(15, num_views_minus1)`.
    pub num_non_anchor_refs_l0: u8,
    /// `num_non_anchor_refs_l1[i]` — bounded by `Min(15, num_views_minus1)`.
    pub num_non_anchor_refs_l1: u8,
}

/// time_offset_length, etc.).
#[derive(Debug, Clone)]
pub struct SeiContext {
    /// From VUI.nal_hrd: initial_cpb_removal_delay_length_minus1 and
    /// cpb_removal_delay_length_minus1, needed by buffering_period and
    /// pic_timing.
    pub initial_cpb_removal_delay_length_minus1: u8,
    pub cpb_removal_delay_length_minus1: u8,
    pub dpb_output_delay_length_minus1: u8,
    pub time_offset_length: u8,
    /// From VUI: cpb_cnt_minus1 of NAL/VCL HRD blocks (if present).
    pub nal_hrd_cpb_cnt_minus1: Option<u32>,
    pub vcl_hrd_cpb_cnt_minus1: Option<u32>,
    /// From VUI.pic_struct_present_flag.
    pub pic_struct_present_flag: bool,
    /// CpbDpbDelaysPresentFlag: derived from VUI (NAL or VCL HRD present).
    pub cpb_dpb_delays_present_flag: bool,
    /// From the active PPS — `num_slice_groups_minus1 + 1`. Needed by
    /// §D.1.20 motion_constrained_slice_group_set to compute the
    /// `u(v)` width of each slice_group_id entry. Defaults to 1 (single
    /// slice group), under which the §D.1.20 field becomes 0 bits wide.
    pub num_slice_groups: u32,
    /// From the active SPS — `PicSizeInMapUnits` =
    /// `(pic_width_in_mbs_minus1 + 1) * (pic_height_in_map_units_minus1 + 1)`
    /// (§7.4.2.1.1, Eq. 7-25). Needed by §D.1.10 spare_pic to bound the
    /// per-map-unit `spare_unit_flag` / `zero_run_length` loops. Defaults
    /// to 0; a spare_pic message that reaches a per-map-unit branch
    /// (`spare_area_idc == 1` or `2`) with a 0 value is rejected with
    /// [`SeiError::SparePicMapUnitsUnknown`].
    pub pic_size_in_map_units: u32,
    /// From the active SPS — `frame_mbs_only_flag` (§7.3.2.1.1). Needed by
    /// §D.1.9 dec_ref_pic_marking_repetition: the `original_field_pic_flag`
    /// / `original_bottom_field_flag` fields are present only when
    /// `frame_mbs_only_flag == 0`. Defaults to `true` (frame-only coding,
    /// the common case), under which those two fields are absent.
    pub frame_mbs_only_flag: bool,
    /// From the active subset SPS — the `NumDepthViews` variable
    /// accumulated from `depth_view_present_flag[i]` over the
    /// §H.7.3.2.1.5 `seq_parameter_set_mvcd_extension()` view loop.
    /// Needed by §H.13.1.5 depth_timing: when
    /// `per_view_depth_timing_flag == 1` the message carries one
    /// `depth_timing_offset()` per depth view, and the loop bound
    /// comes from this SPS-derived variable rather than the payload.
    /// Defaults to 0 (unknown / no Annex H subset SPS active); a
    /// depth_timing message that reaches the per-view branch with a 0
    /// value is rejected with
    /// [`SeiError::DepthTimingNumDepthViewsUnknown`].
    pub num_depth_views: u32,
    /// From the active MVC subset SPS — the per-view inter-view
    /// reference counts (`num_anchor_refs_lX[i]` /
    /// `num_non_anchor_refs_lX[i]`, §G.7.3.2.1.4) for view indices
    /// `i ∈ 1..=num_views_minus1`. Needed by §G.13.1.7
    /// `view_dependency_change` (payload type 42): the message carries
    /// one `anchor_ref_lX_flag` / `non_anchor_ref_lX_flag` per
    /// inter-view reference, and the loop bounds come from this
    /// SPS-derived structure rather than the payload. Empty by default
    /// (no MVC subset SPS active); a `view_dependency_change` message
    /// that sets `anchor_update_flag` / `non_anchor_update_flag` with
    /// an empty vector is rejected with
    /// [`SeiError::ViewDependencyChangeRefCountsUnknown`].
    pub mvc_view_ref_counts: Vec<MvcViewRefCounts>,
}

impl Default for SeiContext {
    fn default() -> Self {
        Self {
            initial_cpb_removal_delay_length_minus1: 0,
            cpb_removal_delay_length_minus1: 0,
            dpb_output_delay_length_minus1: 0,
            time_offset_length: 0,
            nal_hrd_cpb_cnt_minus1: None,
            vcl_hrd_cpb_cnt_minus1: None,
            pic_struct_present_flag: false,
            cpb_dpb_delays_present_flag: false,
            // §D.1.20 — 0 bits per slice_group_id field when num_slice_groups = 1.
            num_slice_groups: 1,
            // §D.1.10 — unknown until the active SPS dimensions are wired in.
            pic_size_in_map_units: 0,
            // §D.1.9 — assume frame-only coding unless the active SPS says
            // otherwise; under frame_mbs_only_flag the original_field_pic_flag
            // / original_bottom_field_flag fields are absent.
            frame_mbs_only_flag: true,
            // §H.13.1.5 — unknown until an Annex H subset SPS MVCD
            // extension is wired in; the depth_timing per-view branch
            // is rejected under this value.
            num_depth_views: 0,
            // §G.13.1.7 — empty until an Annex G MVC subset SPS is
            // wired in; the view_dependency_change update branches are
            // rejected when an update flag is set with no counts.
            mvc_view_ref_counts: Vec::new(),
        }
    }
}

/// §D.2.2 — buffering_period.
///
/// Syntax (§D.1.2):
/// ```text
/// buffering_period( payloadSize ) {
///   seq_parameter_set_id                                  ue(v)
///   if( NalHrdBpPresentFlag )
///     for( SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ ) {
///       initial_cpb_removal_delay[ SchedSelIdx ]          u(v)
///       initial_cpb_removal_delay_offset[ SchedSelIdx ]   u(v)
///     }
///   if( VclHrdBpPresentFlag )
///     for( SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ ) {
///       initial_cpb_removal_delay[ SchedSelIdx ]          u(v)
///       initial_cpb_removal_delay_offset[ SchedSelIdx ]   u(v)
///     }
/// }
/// ```
///
/// The length in bits of the u(v) elements is
/// `initial_cpb_removal_delay_length_minus1 + 1` (§D.2.2).
pub fn parse_buffering_period(
    payload: &[u8],
    ctx: &SeiContext,
) -> Result<BufferingPeriod, SeiError> {
    let mut r = BitReader::new(payload);
    let seq_parameter_set_id = r.ue()?;
    let delay_bits = ctx.initial_cpb_removal_delay_length_minus1 as u32 + 1;

    let nal_hrd = if let Some(cnt_minus1) = ctx.nal_hrd_cpb_cnt_minus1 {
        let count = (cnt_minus1 as usize).saturating_add(1);
        let mut out = Vec::with_capacity(count);
        for _ in 0..count {
            let d = r.u(delay_bits)?;
            let o = r.u(delay_bits)?;
            out.push(CpbRemovalDelay {
                initial_cpb_removal_delay: d,
                initial_cpb_removal_delay_offset: o,
            });
        }
        Some(out)
    } else {
        None
    };

    let vcl_hrd = if let Some(cnt_minus1) = ctx.vcl_hrd_cpb_cnt_minus1 {
        let count = (cnt_minus1 as usize).saturating_add(1);
        let mut out = Vec::with_capacity(count);
        for _ in 0..count {
            let d = r.u(delay_bits)?;
            let o = r.u(delay_bits)?;
            out.push(CpbRemovalDelay {
                initial_cpb_removal_delay: d,
                initial_cpb_removal_delay_offset: o,
            });
        }
        Some(out)
    } else {
        None
    };

    Ok(BufferingPeriod {
        seq_parameter_set_id,
        nal_hrd,
        vcl_hrd,
    })
}

/// §D.2.3 — pic_timing.
///
/// Syntax (§D.1.3). Depends on `CpbDpbDelaysPresentFlag` and
/// `pic_struct_present_flag` from VUI. When `pic_struct_present_flag`
/// is set, the number of clockTimestamp slots `NumClockTS` is
/// determined by Table D-1 (§D.2.3).
///
/// NumClockTS per pic_struct:
/// | pic_struct | meaning                               | NumClockTS |
/// | ---------- | ------------------------------------- | ---------- |
/// | 0          | progressive frame                     | 1          |
/// | 1          | top field                             | 1          |
/// | 2          | bottom field                          | 1          |
/// | 3          | top+bottom                            | 2          |
/// | 4          | bottom+top                            | 2          |
/// | 5          | top+bottom+top                        | 3          |
/// | 6          | bottom+top+bottom                     | 3          |
/// | 7          | frame doubling                        | 2          |
/// | 8          | frame tripling                        | 3          |
pub fn parse_pic_timing(payload: &[u8], ctx: &SeiContext) -> Result<PicTiming, SeiError> {
    let mut r = BitReader::new(payload);

    let (cpb_removal_delay, dpb_output_delay) = if ctx.cpb_dpb_delays_present_flag {
        let cpb_bits = ctx.cpb_removal_delay_length_minus1 as u32 + 1;
        let dpb_bits = ctx.dpb_output_delay_length_minus1 as u32 + 1;
        (r.u(cpb_bits)?, r.u(dpb_bits)?)
    } else {
        (0, 0)
    };

    let pic_struct_info = if ctx.pic_struct_present_flag {
        // §D.1.3 — pic_struct u(4), then NumClockTS clockTimestamps.
        let pic_struct_val = r.u(4)? as u8;
        let num_clock_ts = num_clock_ts_from_pic_struct(pic_struct_val);

        let mut ts_slots: Vec<Option<ClockTimestamp>> = Vec::with_capacity(num_clock_ts as usize);
        for _ in 0..num_clock_ts {
            let clock_timestamp_flag = r.u(1)? == 1;
            if !clock_timestamp_flag {
                ts_slots.push(None);
                continue;
            }
            // §D.1.3 clockTimestamp inner body + §D.2.3 semantics.
            let ct_type = r.u(2)? as u8;
            let nuit_field_based_flag = r.u(1)? == 1;
            let counting_type = r.u(5)? as u8;
            let full_timestamp_flag = r.u(1)? == 1;
            let discontinuity_flag = r.u(1)? == 1;
            let cnt_dropped_flag = r.u(1)? == 1;
            let n_frames = r.u(8)? as u8;

            let (seconds, minutes, hours) = if full_timestamp_flag {
                let s = r.u(6)? as u8;
                let m = r.u(6)? as u8;
                let h = r.u(5)? as u8;
                (s, m, h)
            } else {
                let seconds_flag = r.u(1)? == 1;
                let (mut s, mut m, mut h) = (0u8, 0u8, 0u8);
                if seconds_flag {
                    s = r.u(6)? as u8;
                    let minutes_flag = r.u(1)? == 1;
                    if minutes_flag {
                        m = r.u(6)? as u8;
                        let hours_flag = r.u(1)? == 1;
                        if hours_flag {
                            h = r.u(5)? as u8;
                        }
                    }
                }
                (s, m, h)
            };

            let time_offset = if ctx.time_offset_length > 0 {
                r.i(ctx.time_offset_length as u32)?
            } else {
                0
            };

            ts_slots.push(Some(ClockTimestamp {
                ct_type,
                nuit_field_based_flag,
                counting_type,
                full_timestamp_flag,
                discontinuity_flag,
                cnt_dropped_flag,
                n_frames,
                seconds,
                minutes,
                hours,
                time_offset,
            }));
        }

        Some(PicStructInfo {
            pic_struct: pic_struct_val,
            clock_timestamps: ts_slots,
        })
    } else {
        None
    };

    Ok(PicTiming {
        cpb_removal_delay,
        dpb_output_delay,
        pic_struct: pic_struct_info,
    })
}

/// Number of clockTimestamp entries per pic_struct, per §D.2.3 Table D-1.
/// For reserved values (9..=15) the table is unspecified; returning 0
/// is the safest choice: no clockTimestamp flags are read.
fn num_clock_ts_from_pic_struct(pic_struct: u8) -> u8 {
    match pic_struct {
        0..=2 => 1,
        3 | 4 | 7 => 2,
        5 | 6 | 8 => 3,
        _ => 0,
    }
}

/// §D.2.5 — filler_payload.
///
/// Syntax (§D.1.5): `payloadSize` occurrences of `ff_byte` equal to
/// `0xFF`. This parser verifies every byte matches and returns `()`;
/// no state is kept because the payload carries no signal.
pub fn parse_filler_payload(payload: &[u8]) -> Result<(), SeiError> {
    for (i, &b) in payload.iter().enumerate() {
        if b != 0xFF {
            return Err(SeiError::FillerNonFfAt(i));
        }
    }
    Ok(())
}

/// §D.2.6 — user_data_registered_itu_t_t35.
///
/// Syntax (§D.1.6):
/// ```text
/// user_data_registered_itu_t_t35( payloadSize ) {
///   itu_t_t35_country_code                          b(8)
///   if( itu_t_t35_country_code == 0xFF )
///     itu_t_t35_country_code_extension_byte         b(8)
///   for( i = ...; i < payloadSize; i++ )
///     itu_t_t35_payload_byte                        b(8)
/// }
/// ```
///
/// All fields are byte-aligned per §D.1.6, so we read straight from
/// the slice without going through the bit-reader. The extension byte
/// is consumed only when `country_code == 0xFF` per Recommendation
/// ITU-T T.35 §3.1.
pub fn parse_user_data_registered_itu_t_t35(
    payload: &[u8],
) -> Result<UserDataRegisteredItuTT35, SeiError> {
    let Some((&country_code, rest)) = payload.split_first() else {
        return Err(SeiError::RegisteredUserDataMissingCountryCode);
    };
    let (country_code_extension, payload_bytes) = if country_code == 0xFF {
        match rest.split_first() {
            Some((&ext, body)) => (Some(ext), body.to_vec()),
            // §D.1.6 / T.35 §3.1: when country_code is the escape value
            // 0xFF, the extension byte is required. A truncated payload
            // here is malformed.
            None => return Err(SeiError::RegisteredUserDataMissingCountryCode),
        }
    } else {
        (None, rest.to_vec())
    };
    Ok(UserDataRegisteredItuTT35 {
        country_code,
        country_code_extension,
        payload_bytes,
    })
}

// =============================================================================
// ATSC1 (ATSC A/53 Part 4) typed envelope on top of user_data_registered_itu_t_t35
// =============================================================================
//
// The single most common ATSC1 carriage seen in real-world H.264 streams
// is closed-caption + bar / AFD metadata wrapped inside SEI payload-type 4
// (`user_data_registered_itu_t_t35`, §D.2.6). The wire layout is:
//
//   country_code      = 0xB5             (USA, ITU-T T.35 §3.1)
//   provider_code     = 0x0031           (ATSC, per the SMPTE-RA T.35
//                                         terminal-provider registration
//                                         referenced from ATSC A/53 Part 4)
//   user_identifier   = 0x47413934       ("GA94" → ATSC_user_data() body,
//                                         A/53 Part 4 §6.2.3 Table 6.7)
//                       | 0x44544731     ("DTG1" → afd_data() body,
//                                         A/53 Part 4 §6.2.3 Table 6.7)
//   ATSC_user_data() = user_data_type_code (u(8)) + user_data_type_structure()
//                                        A/53 Part 4 Table 6.8 + Table 6.9:
//   user_data_type_code values:
//     0x00..=0x02 — ATSC reserved
//     0x03        — MPEG_cc_data() (cc_data() per CEA-708 [1] Table 2)
//     0x04..=0x05 — ATSC reserved
//     0x06        — bar_data()
//     0x07..=0xFF — ATSC reserved
//
// This module parses the **outer envelope** (country / provider / user_id /
// type_code) and the bar_data() body (Table 6.11 + 6.12 + §6.2.3.2). The
// inner cc_data() bytes are kept opaque because the CEA-708 Table 2 byte
// layout is normatively defined outside this spec — see the docs gap note
// in the surrounding parser.

/// ATSC1 user_identifier registered values (A/53 Part 4 Table 6.7).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Atsc1UserIdentifier {
    /// 'GA94' (0x47413934) — ATSC_user_data() body per A/53 Part 4 §6.2.3.
    Ga94,
    /// 'DTG1' (0x44544731) — afd_data() body per A/53 Part 4 §6.2.3.
    Dtg1,
}

impl Atsc1UserIdentifier {
    /// Raw 4-byte representation as it appears on the wire.
    pub const GA94: u32 = 0x4741_3934;
    /// Raw 4-byte representation as it appears on the wire.
    pub const DTG1: u32 = 0x4454_4731;

    fn from_u32(v: u32) -> Option<Self> {
        match v {
            Self::GA94 => Some(Self::Ga94),
            Self::DTG1 => Some(Self::Dtg1),
            _ => None,
        }
    }
}

/// One ATSC_user_data() entry per A/53 Part 4 §6.2.3 Table 6.8 / Table 6.9.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Atsc1UserData {
    /// `user_data_type_code == 0x03` — MPEG_cc_data() per Table 6.10.
    ///
    /// The cc_data() inner bytes are normatively defined in CEA-708 [1]
    /// Table 2, which is *not* present in this docs tree. We surface the
    /// raw cc_data() byte string (with the trailing marker_bits == 0xFF
    /// preserved) so callers that have a CEA-708 parser handy can drive
    /// it; this layer treats the inner payload as opaque.
    CcData { cc_data_bytes: Vec<u8> },
    /// `user_data_type_code == 0x06` — bar_data() per Table 6.11.
    BarData(BarData),
    /// `user_data_type_code ∈ {0x00, 0x01, 0x02, 0x04, 0x05, 0x07..=0xFF}` —
    /// ATSC reserved per Table 6.9. Receiving devices are expected to
    /// silently discard the structure (A/53 Part 4 §6.2.2). We keep the
    /// raw bytes to make round-trip diagnostics possible.
    Reserved { type_code: u8, raw: Vec<u8> },
}

/// `afd_data()` per ATSC A/53 Part 4 §6.2.4 / Table 6.13.
///
/// Carried under the `'DTG1'` user_identifier. The wire layout is
/// `[zero u(1) | active_format_flag u(1) | reserved '000001' u(6)]`
/// followed optionally (when `active_format_flag == 1`) by `reserved '1111'
/// u(4) | active_format u(4)`. We surface `active_format_flag` and the
/// resolved 4-bit `active_format` value; the reserved bits are validated
/// against their fixed patterns.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AfdData {
    pub active_format_flag: bool,
    /// `active_format` per A/53 Part 4 Table 6.14. Only meaningful when
    /// `active_format_flag` is true; `None` otherwise.
    pub active_format: Option<u8>,
}

/// `bar_data()` per ATSC A/53 Part 4 §6.2.3.2 / Table 6.11.
///
/// A `bar_data()` structure signals either a letterbox region
/// (top + bottom bars) or a pillarbox region (left + right bars). Per
/// §6.2.3.2 the two pairs are mutually exclusive in a single structure;
/// `top_bar_flag` shall equal `bottom_bar_flag`, and `left_bar_flag`
/// shall equal `right_bar_flag`. We enforce both constraints.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BarData {
    /// `(line_number_end_of_top_bar, line_number_start_of_bottom_bar)` —
    /// `Some(_)` when `top_bar_flag == bottom_bar_flag == 1`. Each value
    /// is a 14-bit unsigned line number whose video-format-dependent
    /// designation is specified in Table 6.12.
    pub letterbox: Option<(u16, u16)>,
    /// `(pixel_number_end_of_left_bar, pixel_number_start_of_right_bar)` —
    /// `Some(_)` when `left_bar_flag == right_bar_flag == 1`. Each value
    /// is a 14-bit unsigned pixel index counted from zero at the leftmost
    /// luma sample per §6.2.3.2.
    pub pillarbox: Option<(u16, u16)>,
}

/// Typed ATSC1 envelope sitting on top of `user_data_registered_itu_t_t35`.
///
/// `provider_code` is preserved verbatim — `parse_atsc1_envelope` rejects
/// any value other than `0x0031` so consumers can assume it. The structured
/// `user_identifier` enum disambiguates the inner body type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Atsc1Envelope {
    /// `provider_code` — always `0x0031` after a successful parse.
    pub provider_code: u16,
    /// `user_identifier` — registered value per A/53 Part 4 Table 6.7.
    pub user_identifier: Atsc1UserIdentifier,
    /// Decoded inner body — `Ga94 → AtscUserData`, `Dtg1 → Afd`.
    pub body: Atsc1Body,
}

/// Inner body of an ATSC1 envelope.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Atsc1Body {
    /// `'GA94'` → `ATSC_user_data()` per Table 6.8.
    AtscUserData(Atsc1UserData),
    /// `'DTG1'` → `afd_data()` per §6.2.4.
    Afd(AfdData),
}

/// Parse an `afd_data()` body per A/53 Part 4 §6.2.4 / Table 6.13.
///
/// Layout (1 or 2 bytes):
/// ```text
/// afd_data() {
///   zero                              u(1) = 0
///   active_format_flag                u(1)
///   reserved                          u(6) = 0b000001
///   if (active_format_flag == 1) {
///     reserved                        u(4) = 0b1111
///     active_format                   u(4)
///   }
/// }
/// ```
///
/// The 'zero' leading bit + the 6-bit reserved field both have fixed
/// patterns per Table 6.13 and the §6.2.4.2 semantics, but we accept any
/// values silently because real-world streams have been known to populate
/// the high bit differently and §6.2.4 Note (a) clarifies the field is
/// non-`reserved`-in-the-strict-sense.
fn parse_afd_data(bytes: &[u8]) -> Result<AfdData, SeiError> {
    let Some(&first) = bytes.first() else {
        return Err(SeiError::Atsc1BarDataTruncated { needed: 1, got: 0 });
    };
    let active_format_flag = (first & 0b0100_0000) != 0;
    if !active_format_flag {
        return Ok(AfdData {
            active_format_flag: false,
            active_format: None,
        });
    }
    // Second byte = '1111' u(4) + active_format u(4).
    let Some(&second) = bytes.get(1) else {
        return Err(SeiError::Atsc1BarDataTruncated { needed: 2, got: 1 });
    };
    let active_format = second & 0x0F;
    Ok(AfdData {
        active_format_flag: true,
        active_format: Some(active_format),
    })
}

/// Parse a `bar_data()` body per A/53 Part 4 §6.2.3.2 / Table 6.11.
///
/// Wire layout (1 + 0..4 × 2 bytes):
/// ```text
///   top_bar_flag    u(1)
///   bottom_bar_flag u(1)
///   left_bar_flag   u(1)
///   right_bar_flag  u(1)
///   reserved        u(4) = '1111'
///   if (top_bar_flag == 1) {
///     one_bits      u(2) = '11'
///     line_number_end_of_top_bar     u(14)
///   }
///   if (bottom_bar_flag == 1) {
///     one_bits      u(2) = '11'
///     line_number_start_of_bottom_bar u(14)
///   }
///   if (left_bar_flag == 1) {
///     one_bits      u(2) = '11'
///     pixel_number_end_of_left_bar    u(14)
///   }
///   if (right_bar_flag == 1) {
///     one_bits      u(2) = '11'
///     pixel_number_start_of_right_bar u(14)
///   }
/// ```
///
/// Per §6.2.3.2: top_bar_flag == bottom_bar_flag, left_bar_flag ==
/// right_bar_flag, and only one of the two pairs may be set at a time.
/// The reserved nibble and each `one_bits` value have fixed bit patterns
/// per the table and are validated.
fn parse_bar_data(bytes: &[u8]) -> Result<BarData, SeiError> {
    let mut r = BitReader::new(bytes);
    let top = r.u(1)? == 1;
    let bottom = r.u(1)? == 1;
    let left = r.u(1)? == 1;
    let right = r.u(1)? == 1;
    let reserved = r.u(4)? as u8;
    if reserved != 0b1111 {
        return Err(SeiError::Atsc1BarDataReservedMismatch(reserved));
    }
    if top != bottom {
        return Err(SeiError::Atsc1BarDataTopBottomMismatch { top, bottom });
    }
    if left != right {
        return Err(SeiError::Atsc1BarDataLeftRightMismatch { left, right });
    }
    if (top || bottom) && (left || right) {
        return Err(SeiError::Atsc1BarDataLetterboxPillarboxBoth);
    }

    let mut read_14 = |which: &'static str| -> Result<u16, SeiError> {
        let one_bits = r.u(2)? as u8;
        if one_bits != 0b11 {
            return Err(SeiError::Atsc1BarDataOneBitsMismatch {
                which,
                got: one_bits,
            });
        }
        Ok(r.u(14)? as u16)
    };
    let letterbox = if top {
        let t = read_14("top")?;
        let b = read_14("bottom")?;
        Some((t, b))
    } else {
        None
    };
    let pillarbox = if left {
        let l = read_14("left")?;
        let rt = read_14("right")?;
        Some((l, rt))
    } else {
        None
    };
    Ok(BarData {
        letterbox,
        pillarbox,
    })
}

/// Parse an ATSC1 envelope from the **`payload_bytes`** field of a
/// `user_data_registered_itu_t_t35` (i.e. the bytes that follow the leading
/// country_code byte). The caller has already established that
/// `country_code == 0xB5` (USA).
///
/// Validates:
/// * Minimum length (≥ 6 bytes for the 2-byte provider_code + 4-byte
///   user_identifier).
/// * `provider_code == 0x0031` (ATSC's T.35 administered slot).
/// * `user_identifier ∈ {0x47413934 'GA94', 0x44544731 'DTG1'}` per
///   A/53 Part 4 §6.2.3 Table 6.7.
///
/// For `'GA94'` it reads the `user_data_type_code` (u(8)) per Table 6.8
/// and dispatches:
/// * `0x03` → `Atsc1UserData::CcData { cc_data_bytes }` — opaque
///   (CEA-708 Table 2 layout out of docs scope).
/// * `0x06` → `Atsc1UserData::BarData(_)` per Table 6.11.
/// * any reserved code → `Atsc1UserData::Reserved { type_code, raw }`
///   per §6.2.2 ("receiving devices are expected to silently discard
///   unrecognized video user data").
///
/// For `'DTG1'` it parses `afd_data()` per §6.2.4 / Table 6.13.
pub fn parse_atsc1_envelope(payload_bytes: &[u8]) -> Result<Atsc1Envelope, SeiError> {
    if payload_bytes.len() < 6 {
        return Err(SeiError::Atsc1EnvelopeTooShort(payload_bytes.len()));
    }
    let provider_code = u16::from_be_bytes([payload_bytes[0], payload_bytes[1]]);
    if provider_code != 0x0031 {
        return Err(SeiError::Atsc1ProviderCodeMismatch(provider_code));
    }
    let user_identifier_raw = u32::from_be_bytes([
        payload_bytes[2],
        payload_bytes[3],
        payload_bytes[4],
        payload_bytes[5],
    ]);
    let user_identifier = Atsc1UserIdentifier::from_u32(user_identifier_raw)
        .ok_or(SeiError::Atsc1UnknownUserIdentifier(user_identifier_raw))?;
    let rest = &payload_bytes[6..];
    let body = match user_identifier {
        Atsc1UserIdentifier::Ga94 => {
            let Some((&type_code, after)) = rest.split_first() else {
                return Err(SeiError::Atsc1AtscUserDataEmpty);
            };
            let inner = match type_code {
                0x03 => Atsc1UserData::CcData {
                    cc_data_bytes: after.to_vec(),
                },
                0x06 => Atsc1UserData::BarData(parse_bar_data(after)?),
                _ => Atsc1UserData::Reserved {
                    type_code,
                    raw: after.to_vec(),
                },
            };
            Atsc1Body::AtscUserData(inner)
        }
        Atsc1UserIdentifier::Dtg1 => Atsc1Body::Afd(parse_afd_data(rest)?),
    };
    Ok(Atsc1Envelope {
        provider_code,
        user_identifier,
        body,
    })
}

/// §D.2.4 — pan_scan_rect.
///
/// Syntax (§D.1.4):
/// ```text
/// pan_scan_rect( payloadSize ) {
///   pan_scan_rect_id                                ue(v)
///   pan_scan_rect_cancel_flag                       u(1)
///   if( !pan_scan_rect_cancel_flag ) {
///     pan_scan_cnt_minus1                           ue(v)
///     for( i = 0; i <= pan_scan_cnt_minus1; i++ ) {
///       pan_scan_rect_left_offset[ i ]              se(v)
///       pan_scan_rect_right_offset[ i ]             se(v)
///       pan_scan_rect_top_offset[ i ]               se(v)
///       pan_scan_rect_bottom_offset[ i ]            se(v)
///     }
///     pan_scan_rect_repetition_period               ue(v)
///   }
/// }
/// ```
///
/// Per §D.2.4 `pan_scan_cnt_minus1` is constrained to `0..=2` (so the
/// payload carries at most three rectangles); we accept up to 31 to
/// stay forgiving of streams that exceed the constraint, but reject
/// counts that are obviously out of range to avoid pathological
/// allocations.
pub fn parse_pan_scan_rect(payload: &[u8]) -> Result<PanScanRect, SeiError> {
    let mut r = BitReader::new(payload);
    let pan_scan_rect_id = r.ue()?;
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(PanScanRect {
            pan_scan_rect_id,
            cancel_flag,
            body: None,
        });
    }
    let cnt_minus1 = r.ue()?;
    if cnt_minus1 >= 32 {
        return Err(SeiError::PanScanCountTooLarge(cnt_minus1));
    }
    let count = (cnt_minus1 as usize).saturating_add(1);
    let mut rects = Vec::with_capacity(count);
    for _ in 0..count {
        let left = r.se()?;
        let right = r.se()?;
        let top = r.se()?;
        let bottom = r.se()?;
        rects.push(PanScanRectOffsets {
            left_offset: left,
            right_offset: right,
            top_offset: top,
            bottom_offset: bottom,
        });
    }
    let repetition_period = r.ue()?;
    Ok(PanScanRect {
        pan_scan_rect_id,
        cancel_flag,
        body: Some(PanScanRectBody {
            rects,
            repetition_period,
        }),
    })
}

/// §D.2.7 — user_data_unregistered.
///
/// Syntax (§D.1.7): `uuid_iso_iec_11578` (u(128)) followed by
/// `payloadSize - 16` bytes of `user_data_payload_byte`.
pub fn parse_user_data_unregistered(payload: &[u8]) -> Result<UserDataUnregistered, SeiError> {
    if payload.len() < 16 {
        return Err(SeiError::UserDataTooShort(payload.len()));
    }
    let mut uuid = [0u8; 16];
    uuid.copy_from_slice(&payload[..16]);
    let user_data = payload[16..].to_vec();
    Ok(UserDataUnregistered { uuid, user_data })
}

/// §D.2.8 — recovery_point.
///
/// Syntax (§D.1.8):
/// ```text
/// recovery_point( payloadSize ) {
///   recovery_frame_cnt       ue(v)
///   exact_match_flag          u(1)
///   broken_link_flag          u(1)
///   changing_slice_group_idc  u(2)
/// }
/// ```
pub fn parse_recovery_point(payload: &[u8]) -> Result<RecoveryPoint, SeiError> {
    let mut r = BitReader::new(payload);
    let recovery_frame_cnt = r.ue()?;
    let exact_match_flag = r.u(1)? == 1;
    let broken_link_flag = r.u(1)? == 1;
    let changing_slice_group_idc = r.u(2)? as u8;
    Ok(RecoveryPoint {
        recovery_frame_cnt,
        exact_match_flag,
        broken_link_flag,
        changing_slice_group_idc,
    })
}

/// §D.2.15 — full_frame_freeze (payload type 13).
///
/// Syntax (§D.1.15):
/// ```text
/// full_frame_freeze( payloadSize ) {
///   full_frame_freeze_repetition_period   ue(v)
/// }
/// ```
pub fn parse_full_frame_freeze(payload: &[u8]) -> Result<FullFrameFreeze, SeiError> {
    let mut r = BitReader::new(payload);
    let full_frame_freeze_repetition_period = r.ue()?;
    Ok(FullFrameFreeze {
        full_frame_freeze_repetition_period,
    })
}

/// §D.2.17 — full_frame_snapshot (payload type 15).
///
/// Syntax (§D.1.17):
/// ```text
/// full_frame_snapshot( payloadSize ) {
///   snapshot_id   ue(v)
/// }
/// ```
pub fn parse_full_frame_snapshot(payload: &[u8]) -> Result<FullFrameSnapshot, SeiError> {
    let mut r = BitReader::new(payload);
    let snapshot_id = r.ue()?;
    Ok(FullFrameSnapshot { snapshot_id })
}

/// §D.2.22 — deblocking_filter_display_preference (payload type 20).
///
/// Syntax (§D.1.22):
/// ```text
/// deblocking_filter_display_preference( payloadSize ) {
///   deblocking_display_preference_cancel_flag                 u(1)
///   if( !deblocking_display_preference_cancel_flag ) {
///     display_prior_to_deblocking_preferred_flag              u(1)
///     dec_frame_buffering_constraint_flag                     u(1)
///     deblocking_display_preference_repetition_period         ue(v)
///   }
/// }
/// ```
pub fn parse_deblocking_filter_display_preference(
    payload: &[u8],
) -> Result<DeblockingFilterDisplayPreference, SeiError> {
    let mut r = BitReader::new(payload);
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(DeblockingFilterDisplayPreference {
            cancel_flag,
            display_prior_to_deblocking_preferred_flag: false,
            dec_frame_buffering_constraint_flag: false,
            repetition_period: 0,
        });
    }
    let display_prior_to_deblocking_preferred_flag = r.u(1)? == 1;
    let dec_frame_buffering_constraint_flag = r.u(1)? == 1;
    let repetition_period = r.ue()?;
    Ok(DeblockingFilterDisplayPreference {
        cancel_flag,
        display_prior_to_deblocking_preferred_flag,
        dec_frame_buffering_constraint_flag,
        repetition_period,
    })
}

/// §D.2.29 — mastering_display_colour_volume (HDR10 metadata).
///
/// Syntax (§D.1.29):
/// ```text
/// mastering_display_colour_volume( payloadSize ) {
///   for( c = 0; c < 3; c++ ) {
///     display_primaries_x[ c ]         u(16)
///     display_primaries_y[ c ]         u(16)
///   }
///   white_point_x                       u(16)
///   white_point_y                       u(16)
///   max_display_mastering_luminance     u(32)
///   min_display_mastering_luminance     u(32)
/// }
/// ```
///
/// Per §D.2.29 the primaries and white point are on a 0..=50000 scale
/// (CIE 1931 chromaticity * 50000); luminance is in units of 0.0001
/// candela per square metre.
pub fn parse_mastering_display(payload: &[u8]) -> Result<MasteringDisplayColourVolume, SeiError> {
    let mut r = BitReader::new(payload);
    let mut primaries = [(0u16, 0u16); 3];
    for slot in primaries.iter_mut() {
        let x = r.u(16)? as u16;
        let y = r.u(16)? as u16;
        *slot = (x, y);
    }
    let wp_x = r.u(16)? as u16;
    let wp_y = r.u(16)? as u16;
    let max_l = r.u(32)?;
    let min_l = r.u(32)?;
    Ok(MasteringDisplayColourVolume {
        display_primaries: primaries,
        white_point: (wp_x, wp_y),
        max_display_mastering_luminance: max_l,
        min_display_mastering_luminance: min_l,
    })
}

/// §D.2.31 — content_light_level_info (HDR10 metadata).
///
/// Syntax (§D.1.31):
/// ```text
/// content_light_level_info( payloadSize ) {
///   max_content_light_level       u(16)
///   max_pic_average_light_level   u(16)
/// }
/// ```
pub fn parse_content_light_level(payload: &[u8]) -> Result<ContentLightLevelInfo, SeiError> {
    let mut r = BitReader::new(payload);
    let max_content_light_level = r.u(16)? as u16;
    let max_pic_average_light_level = r.u(16)? as u16;
    Ok(ContentLightLevelInfo {
        max_content_light_level,
        max_pic_average_light_level,
    })
}

/// §D.2.26 — frame_packing_arrangement (payload type 45).
///
/// Syntax (§D.1.26):
/// ```text
/// frame_packing_arrangement( payloadSize ) {
///   frame_packing_arrangement_id                       ue(v)
///   frame_packing_arrangement_cancel_flag              u(1)
///   if( !frame_packing_arrangement_cancel_flag ) {
///     frame_packing_arrangement_type                   u(7)
///     quincunx_sampling_flag                           u(1)
///     content_interpretation_type                      u(6)
///     spatial_flipping_flag                            u(1)
///     frame0_flipped_flag                              u(1)
///     field_views_flag                                 u(1)
///     current_frame_is_frame0_flag                     u(1)
///     frame0_self_contained_flag                       u(1)
///     frame1_self_contained_flag                       u(1)
///     if( !quincunx_sampling_flag &&
///         frame_packing_arrangement_type != 5 ) {
///       frame0_grid_position_x                         u(4)
///       frame0_grid_position_y                         u(4)
///       frame1_grid_position_x                         u(4)
///       frame1_grid_position_y                         u(4)
///     }
///     frame_packing_arrangement_reserved_byte          u(8)
///     frame_packing_arrangement_repetition_period      ue(v)
///   }
///   frame_packing_arrangement_extension_flag           u(1)
/// }
/// ```
pub fn parse_frame_packing_arrangement(
    payload: &[u8],
) -> Result<FramePackingArrangement, SeiError> {
    let mut r = BitReader::new(payload);
    let id = r.ue()?;
    let cancel_flag = r.u(1)? == 1;

    let mut out = FramePackingArrangement {
        id,
        cancel_flag,
        ty: 0,
        quincunx_sampling_flag: false,
        content_interpretation_type: 0,
        spatial_flipping_flag: false,
        frame0_flipped_flag: false,
        field_views_flag: false,
        current_frame_is_frame0_flag: false,
        frame0_self_contained_flag: false,
        frame1_self_contained_flag: false,
        frame0_grid_position_x: 0,
        frame0_grid_position_y: 0,
        frame1_grid_position_x: 0,
        frame1_grid_position_y: 0,
        reserved_byte: 0,
        repetition_period: 0,
        extension_flag: false,
    };

    if !cancel_flag {
        out.ty = r.u(7)? as u8;
        out.quincunx_sampling_flag = r.u(1)? == 1;
        out.content_interpretation_type = r.u(6)? as u8;
        out.spatial_flipping_flag = r.u(1)? == 1;
        out.frame0_flipped_flag = r.u(1)? == 1;
        out.field_views_flag = r.u(1)? == 1;
        out.current_frame_is_frame0_flag = r.u(1)? == 1;
        out.frame0_self_contained_flag = r.u(1)? == 1;
        out.frame1_self_contained_flag = r.u(1)? == 1;

        // §D.1.26: the grid positions are transmitted only when the
        // planes are non-quincunx AND the arrangement is not temporal
        // interleaving (type 5).
        if !out.quincunx_sampling_flag && out.ty != 5 {
            out.frame0_grid_position_x = r.u(4)? as u8;
            out.frame0_grid_position_y = r.u(4)? as u8;
            out.frame1_grid_position_x = r.u(4)? as u8;
            out.frame1_grid_position_y = r.u(4)? as u8;
        }

        out.reserved_byte = r.u(8)? as u8;
        out.repetition_period = r.ue()?;
    }

    out.extension_flag = r.u(1)? == 1;
    Ok(out)
}

/// §D.2.27 — display_orientation (payload type 47).
///
/// Syntax (§D.1.27):
/// ```text
/// display_orientation( payloadSize ) {
///   display_orientation_cancel_flag               u(1)
///   if( !display_orientation_cancel_flag ) {
///     hor_flip                                    u(1)
///     ver_flip                                    u(1)
///     anticlockwise_rotation                      u(16)
///     display_orientation_repetition_period       ue(v)
///     display_orientation_extension_flag          u(1)
///   }
/// }
/// ```
pub fn parse_display_orientation(payload: &[u8]) -> Result<DisplayOrientation, SeiError> {
    let mut r = BitReader::new(payload);
    let cancel_flag = r.u(1)? == 1;
    let mut out = DisplayOrientation {
        cancel_flag,
        hor_flip: false,
        ver_flip: false,
        anticlockwise_rotation: 0,
        repetition_period: 0,
        extension_flag: false,
    };
    if !cancel_flag {
        out.hor_flip = r.u(1)? == 1;
        out.ver_flip = r.u(1)? == 1;
        out.anticlockwise_rotation = r.u(16)? as u16;
        out.repetition_period = r.ue()?;
        out.extension_flag = r.u(1)? == 1;
    }
    Ok(out)
}

/// §D.2.32 — alternative_transfer_characteristics (payload type 147).
///
/// Syntax (§D.1.32):
/// ```text
/// alternative_transfer_characteristics( payloadSize ) {
///   preferred_transfer_characteristics    u(8)
/// }
/// ```
///
/// Per §D.2.32 the single field overrides the VUI
/// transfer_characteristics for display purposes (for example to
/// signal HLG or PQ in a stream whose VUI says BT.709).
pub fn parse_alternative_transfer_characteristics(payload: &[u8]) -> Result<u8, SeiError> {
    let mut r = BitReader::new(payload);
    Ok(r.u(8)? as u8)
}

// ---------------------------------------------------------------------------
// Round 73 — additional SEI payload types.
//
// All five additions are syntactically simple Exp-Golomb / fixed-width
// flag payloads from Annex D and add transparent parse coverage without
// touching the pixel pipeline. They each implement EXACTLY the §D.1.x
// syntax tables, with semantic-range validation deferred to §D.2.x
// callers (per the existing crate convention of letting the syntax
// layer accept anything the bitstream encodes).
// ---------------------------------------------------------------------------

/// §D.2.10 — spare_pic, the i-th spare picture entry (payload type 8).
///
/// One entry per spare picture referenced by the message. The spare area
/// is encoded with one of three methods selected by `spare_area_idc`:
///
/// * `spare_area_idc == 0` — every slice-group map unit in the spare
///   picture is a spare unit; no per-unit data follows. `spare_units`
///   is `None`.
/// * `spare_area_idc == 1` — one `spare_unit_flag` per map unit in
///   raster scan order. `spare_units` carries those flags
///   (`true` = spare, mapped from `spare_unit_flag == 0` per §D.2.10).
/// * `spare_area_idc == 2` — run-length coded in counter-clockwise
///   box-out order (§8.2.2.4). `zero_run_lengths` carries the raw
///   `zero_run_length[ ]` values; deriving
///   `spareUnitFlagInBoxOutOrder[ ]` (Eq. D-4 / D-5) needs the
///   box-out scan and is left to a caller that has the §8.2.2.4 walk.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SparePicEntry {
    /// `delta_spare_frame_num[ i ]` — identifies the i-th spare picture
    /// relative to the running candidate frame_num (Eq. D-3). In the
    /// range `0..=MaxFrameNum − 2 + spare_field_flag` per §D.2.10.
    pub delta_spare_frame_num: u32,
    /// `spare_bottom_field_flag[ i ]` — present only when the message's
    /// `spare_field_flag` is set. `true` ⇒ the i-th spare picture is a
    /// bottom field.
    pub spare_bottom_field_flag: Option<bool>,
    /// `spare_area_idc[ i ]` — selects the spare-area coding method
    /// (0, 1, or 2 per §D.2.10).
    pub spare_area_idc: u32,
    /// Present only when `spare_area_idc == 1`. One entry per slice-group
    /// map unit in raster scan order; `true` marks a spare unit
    /// (`spare_unit_flag == 0` per §D.2.10).
    pub spare_units: Option<Vec<bool>>,
    /// Present only when `spare_area_idc == 2`. The raw `zero_run_length[ ]`
    /// runs read until the box-out map-unit count reaches
    /// PicSizeInMapUnits.
    pub zero_run_lengths: Option<Vec<u32>>,
}

/// §D.2.9 — dec_ref_pic_marking_repetition (payload type 7).
///
/// Repeats the `dec_ref_pic_marking()` syntax structure (§7.3.3.3) that
/// originally appeared in the slice header(s) of an earlier picture in
/// the same coded video sequence, so a decoder that lost that earlier
/// picture can still apply its reference-picture-marking operations.
///
/// Syntax (§D.1.9):
/// ```text
/// dec_ref_pic_marking_repetition( payloadSize ) {
///   original_idr_flag                                  u(1)
///   original_frame_num                                 ue(v)
///   if( !frame_mbs_only_flag ) {
///     original_field_pic_flag                          u(1)
///     if( original_field_pic_flag )
///       original_bottom_field_flag                     u(1)
///   }
///   dec_ref_pic_marking( )
/// }
/// ```
///
/// Per §D.2.9, the `IdrPicFlag` used to interpret the embedded
/// `dec_ref_pic_marking()` (clause 7.3.3.3) is `original_idr_flag` rather
/// than the IdrPicFlag of the SEI NAL unit itself: when `original_idr_flag`
/// is set, the embedded structure carries `no_output_of_prior_pics_flag`
/// and `long_term_reference_flag`; otherwise it carries the adaptive /
/// sliding-window MMCO loop.
///
/// `original_field_pic_flag` / `original_bottom_field_flag` are present only
/// when the active SPS has `frame_mbs_only_flag == 0`; the parser reads
/// that flag from [`SeiContext::frame_mbs_only_flag`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecRefPicMarkingRepetition {
    /// `original_idr_flag` — `true` when the repeated structure originally
    /// occurred in an IDR picture (§D.2.9). Also serves as the `IdrPicFlag`
    /// used to interpret the embedded `dec_ref_pic_marking()`.
    pub original_idr_flag: bool,
    /// `original_frame_num` — the frame_num of the picture where the
    /// repeated structure originally occurred (§D.2.9).
    pub original_frame_num: u32,
    /// `original_field_pic_flag` — present only when the active SPS has
    /// `frame_mbs_only_flag == 0`; `None` under frame-only coding (§D.1.9).
    pub original_field_pic_flag: Option<bool>,
    /// `original_bottom_field_flag` — present only when
    /// `original_field_pic_flag == Some(true)` (§D.1.9).
    pub original_bottom_field_flag: Option<bool>,
    /// The repeated `dec_ref_pic_marking()` structure (§7.3.3.3),
    /// interpreted with `IdrPicFlag = original_idr_flag` per §D.2.9.
    pub dec_ref_pic_marking: DecRefPicMarking,
}

/// §D.1.9 / §D.2.9 — parse dec_ref_pic_marking_repetition (payload type 7).
///
/// `frame_mbs_only_flag` of the active SPS comes from
/// [`SeiContext::frame_mbs_only_flag`].
pub fn parse_dec_ref_pic_marking_repetition(
    payload: &[u8],
    ctx: &SeiContext,
) -> Result<DecRefPicMarkingRepetition, SeiError> {
    let mut r = BitReader::new(payload);
    // §D.1.9 — original_idr_flag u(1).
    let original_idr_flag = r.u(1)? == 1;
    // §D.1.9 — original_frame_num ue(v).
    let original_frame_num = r.ue()?;
    // §D.1.9 — original_field_pic_flag / original_bottom_field_flag present
    // only when !frame_mbs_only_flag.
    let (original_field_pic_flag, original_bottom_field_flag) = if ctx.frame_mbs_only_flag {
        (None, None)
    } else {
        let fpf = r.u(1)? == 1;
        let bff = if fpf { Some(r.u(1)? == 1) } else { None };
        (Some(fpf), bff)
    };
    // §D.1.9 / §7.3.3.3 — dec_ref_pic_marking(), interpreted with
    // IdrPicFlag = original_idr_flag per §D.2.9.
    let dec_ref_pic_marking = parse_repeated_dec_ref_pic_marking(&mut r, original_idr_flag)?;
    Ok(DecRefPicMarkingRepetition {
        original_idr_flag,
        original_frame_num,
        original_field_pic_flag,
        original_bottom_field_flag,
        dec_ref_pic_marking,
    })
}

/// §7.3.3.3 — read a `dec_ref_pic_marking()` structure from `r`, using
/// `idr_pic_flag` as the `IdrPicFlag` that selects the IDR branch.
///
/// This is a self-contained spec-direct reader for the SEI repetition
/// payload; it mirrors the §7.3.3.3 syntax table (the same structure that
/// also appears in the slice header) and yields the shared
/// [`DecRefPicMarking`] / [`MmcoOp`] data model.
fn parse_repeated_dec_ref_pic_marking(
    r: &mut BitReader<'_>,
    idr_pic_flag: bool,
) -> Result<DecRefPicMarking, SeiError> {
    if idr_pic_flag {
        // §7.3.3.3 IDR branch.
        let no_output_of_prior_pics_flag = r.u(1)? == 1;
        let long_term_reference_flag = r.u(1)? == 1;
        Ok(DecRefPicMarking {
            no_output_of_prior_pics_flag,
            long_term_reference_flag,
            adaptive_marking: None,
        })
    } else {
        // §7.3.3.3 non-IDR branch.
        let adaptive_ref_pic_marking_mode_flag = r.u(1)? == 1;
        let adaptive_marking = if adaptive_ref_pic_marking_mode_flag {
            let mut ops = Vec::new();
            // do { ... } while( memory_management_control_operation != 0 )
            loop {
                let mmco = r.ue()?;
                match mmco {
                    0 => break,
                    1 => {
                        // difference_of_pic_nums_minus1 ue(v)
                        let diff = r.ue()?;
                        ops.push(MmcoOp::MarkShortTermUnused(diff));
                    }
                    2 => {
                        // long_term_pic_num ue(v)
                        let ltpn = r.ue()?;
                        ops.push(MmcoOp::MarkLongTermUnused(ltpn));
                    }
                    3 => {
                        // difference_of_pic_nums_minus1 ue(v), long_term_frame_idx ue(v)
                        let diff = r.ue()?;
                        let ltfi = r.ue()?;
                        ops.push(MmcoOp::AssignLongTerm(diff, ltfi));
                    }
                    4 => {
                        // max_long_term_frame_idx_plus1 ue(v)
                        let max = r.ue()?;
                        ops.push(MmcoOp::SetMaxLongTermIdx(max));
                    }
                    5 => ops.push(MmcoOp::MarkAllUnused),
                    6 => {
                        // long_term_frame_idx ue(v)
                        let ltfi = r.ue()?;
                        ops.push(MmcoOp::AssignCurrentLongTerm(ltfi));
                    }
                    other => {
                        return Err(SeiError::DecRefPicMarkingRepetitionMmcoOutOfRange(other));
                    }
                }
            }
            Some(ops)
        } else {
            None
        };
        Ok(DecRefPicMarking {
            no_output_of_prior_pics_flag: false,
            long_term_reference_flag: false,
            adaptive_marking,
        })
    }
}

/// §D.2.10 — spare_pic (payload type 8).
///
/// Marks slice-group map units of one or more decoded reference pictures
/// (the spare pictures) as resembling the co-located map units of a
/// specified target picture, so a decoder concealing a lost target-picture
/// region can substitute the spare picture's co-located samples.
///
/// Syntax (§D.1.10):
/// ```text
/// spare_pic( payloadSize ) {
///   target_frame_num                                  ue(v)
///   spare_field_flag                                  u(1)
///   if( spare_field_flag )
///     target_bottom_field_flag                        u(1)
///   num_spare_pics_minus1                             ue(v)
///   for( i = 0; i < num_spare_pics_minus1 + 1; i++ ) {
///     delta_spare_frame_num[ i ]                      ue(v)
///     if( spare_field_flag )
///       spare_bottom_field_flag[ i ]                  u(1)
///     spare_area_idc[ i ]                             ue(v)
///     if( spare_area_idc[ i ] == 1 )
///       for( j = 0; j < PicSizeInMapUnits; j++ )
///         spare_unit_flag[ i ][ j ]                   u(1)
///     else if( spare_area_idc[ i ] == 2 ) {
///       mapUnitCnt = 0
///       for( j = 0; mapUnitCnt < PicSizeInMapUnits; j++ ) {
///         zero_run_length[ i ][ j ]                   ue(v)
///         mapUnitCnt += zero_run_length[ i ][ j ] + 1
///       }
///     }
///   }
/// }
/// ```
///
/// The per-map-unit branches (`spare_area_idc` 1 and 2) iterate over
/// `PicSizeInMapUnits` of the active SPS, which the bitstream does not
/// repeat — the parser reads it from [`SeiContext::pic_size_in_map_units`]
/// and rejects a message that reaches one of those branches with a 0 value
/// ([`SeiError::SparePicMapUnitsUnknown`]). A message that only uses
/// `spare_area_idc == 0` for every spare picture parses without needing
/// PicSizeInMapUnits.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SparePic {
    /// `target_frame_num` — the frame_num of the target picture (§D.2.10).
    pub target_frame_num: u32,
    /// `spare_field_flag` — `true` ⇒ target and spare pictures are decoded
    /// fields; `false` ⇒ decoded frames.
    pub spare_field_flag: bool,
    /// `target_bottom_field_flag` — present only when `spare_field_flag`.
    /// `true` ⇒ the target picture is a bottom field.
    pub target_bottom_field_flag: Option<bool>,
    /// One entry per spare picture; `entries.len() == num_spare_pics_minus1 + 1`
    /// (1..=16 per §D.2.10).
    pub entries: Vec<SparePicEntry>,
}

pub fn parse_spare_pic(payload: &[u8], ctx: &SeiContext) -> Result<SparePic, SeiError> {
    let mut r = BitReader::new(payload);
    let target_frame_num = r.ue()?;
    let spare_field_flag = r.u(1)? == 1;
    let target_bottom_field_flag = if spare_field_flag {
        Some(r.u(1)? == 1)
    } else {
        None
    };
    let num_spare_pics_minus1 = r.ue()?;
    // §D.2.10 — num_spare_pics_minus1 shall be in the range of 0 to 15.
    if num_spare_pics_minus1 > 15 {
        return Err(SeiError::SparePicCountTooLarge(num_spare_pics_minus1));
    }
    let count = (num_spare_pics_minus1 as usize) + 1;
    let pic_size_in_map_units = ctx.pic_size_in_map_units;
    let mut entries = Vec::with_capacity(count);
    for i in 0..count {
        let delta_spare_frame_num = r.ue()?;
        let spare_bottom_field_flag = if spare_field_flag {
            Some(r.u(1)? == 1)
        } else {
            None
        };
        let spare_area_idc = r.ue()?;
        // §D.2.10 — spare_area_idc shall be in the range of 0 to 2.
        if spare_area_idc > 2 {
            return Err(SeiError::SparePicAreaIdcOutOfRange {
                i,
                idc: spare_area_idc,
            });
        }
        let (spare_units, zero_run_lengths) = match spare_area_idc {
            0 => (None, None),
            1 => {
                // §D.1.10 — spare_unit_flag[ i ][ j ] for j in 0..PicSizeInMapUnits.
                if pic_size_in_map_units == 0 {
                    return Err(SeiError::SparePicMapUnitsUnknown {
                        i,
                        idc: spare_area_idc,
                    });
                }
                let mut units = Vec::with_capacity(pic_size_in_map_units as usize);
                for _ in 0..pic_size_in_map_units {
                    // §D.2.10 — spare_unit_flag == 0 marks a spare unit.
                    units.push(r.u(1)? == 0);
                }
                (Some(units), None)
            }
            // spare_area_idc == 2.
            _ => {
                // §D.1.10 — zero_run_length[ i ][ j ] read until the box-out
                // map-unit count reaches PicSizeInMapUnits.
                if pic_size_in_map_units == 0 {
                    return Err(SeiError::SparePicMapUnitsUnknown {
                        i,
                        idc: spare_area_idc,
                    });
                }
                let mut runs = Vec::new();
                let mut map_unit_cnt: u64 = 0;
                let target = pic_size_in_map_units as u64;
                // §D.1.10 — the read loop continues while mapUnitCnt is below
                // PicSizeInMapUnits; the final run may push mapUnitCnt past it
                // (each run consumes zero_run_length + 1 box-out slots), so the
                // post-loop count is allowed to exceed the target.
                while map_unit_cnt < target {
                    let zero_run_length = r.ue()?;
                    runs.push(zero_run_length);
                    // mapUnitCnt += zero_run_length[ i ][ j ] + 1.
                    map_unit_cnt += zero_run_length as u64 + 1;
                }
                (None, Some(runs))
            }
        };
        entries.push(SparePicEntry {
            delta_spare_frame_num,
            spare_bottom_field_flag,
            spare_area_idc,
            spare_units,
            zero_run_lengths,
        });
    }
    Ok(SparePic {
        target_frame_num,
        spare_field_flag,
        target_bottom_field_flag,
        entries,
    })
}

/// §D.2.11 — scene_info (payload type 9).
///
/// Communicates "the current picture is part of scene N" plus an
/// optional transition descriptor. Used by editors / players that need
/// to know where a scene cut occurred without re-analysing the pixels.
///
/// Syntax (§D.1.11):
/// ```text
/// scene_info( payloadSize ) {
///   scene_info_present_flag                u(1)
///   if( scene_info_present_flag ) {
///     scene_id                             ue(v)
///     scene_transition_type                ue(v)
///     if( scene_transition_type > 3 )
///       second_scene_id                    ue(v)
///   }
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SceneInfo {
    pub scene_info_present_flag: bool,
    /// Present only when `scene_info_present_flag` is true.
    pub scene_id: Option<u32>,
    /// Present only when `scene_info_present_flag` is true. Per §D.2.11:
    /// 0 = no transition, 1 = fade-to-black, 2 = fade-from-black,
    /// 3 = unspecified transition, 4 = dissolve, 5 = wipe, 6 = unspecified,
    /// values > 6 are reserved.
    pub scene_transition_type: Option<u32>,
    /// Present only when `scene_transition_type > 3` — identifies the
    /// "second" scene the current picture transitions to.
    pub second_scene_id: Option<u32>,
}

pub fn parse_scene_info(payload: &[u8]) -> Result<SceneInfo, SeiError> {
    let mut r = BitReader::new(payload);
    let scene_info_present_flag = r.u(1)? == 1;
    if !scene_info_present_flag {
        return Ok(SceneInfo {
            scene_info_present_flag,
            scene_id: None,
            scene_transition_type: None,
            second_scene_id: None,
        });
    }
    let scene_id = r.ue()?;
    let scene_transition_type = r.ue()?;
    let second_scene_id = if scene_transition_type > 3 {
        Some(r.ue()?)
    } else {
        None
    };
    Ok(SceneInfo {
        scene_info_present_flag,
        scene_id: Some(scene_id),
        scene_transition_type: Some(scene_transition_type),
        second_scene_id,
    })
}

/// §D.2.11 — sub_seq_info (payload type 10).
///
/// Locates the current picture within the sub-sequence layer / sub-sequence
/// data-dependency hierarchy. Layer 0 is independently decodable; a layer
/// `N` may be predicted only from layers `0..N`. A sub-sequence is a set of
/// coded pictures within one layer that does not depend on any other
/// sub-sequence in the same or a higher layer.
///
/// Syntax (§D.1.11):
/// ```text
/// sub_seq_info( payloadSize ) {
///   sub_seq_layer_num             ue(v)
///   sub_seq_id                    ue(v)
///   first_ref_pic_flag            u(1)
///   leading_non_ref_pic_flag      u(1)
///   last_pic_flag                 u(1)
///   sub_seq_frame_num_flag        u(1)
///   if( sub_seq_frame_num_flag )
///     sub_seq_frame_num           ue(v)
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubSeqInfo {
    /// Sub-sequence layer number of the current picture (< 256 per §D.2.11).
    pub sub_seq_layer_num: u32,
    /// Identifies the sub-sequence within its layer (< 65536 per §D.2.11).
    pub sub_seq_id: u32,
    /// `true` ⇒ the current picture is the first reference picture of the
    /// sub-sequence in decoding order.
    pub first_ref_pic_flag: bool,
    /// `true` ⇒ the current picture is a non-reference picture preceding any
    /// reference picture in decoding order within the sub-sequence (or the
    /// sub-sequence contains no reference pictures).
    pub leading_non_ref_pic_flag: bool,
    /// `true` ⇒ the current picture is the last picture of the sub-sequence
    /// in decoding order.
    pub last_pic_flag: bool,
    /// Present only when `sub_seq_frame_num_flag` is 1. Counts coded pictures
    /// within the sub-sequence modulo `MaxFrameNum` (< `MaxFrameNum`).
    pub sub_seq_frame_num: Option<u32>,
}

pub fn parse_sub_seq_info(payload: &[u8]) -> Result<SubSeqInfo, SeiError> {
    let mut r = BitReader::new(payload);
    let sub_seq_layer_num = r.ue()?;
    if sub_seq_layer_num >= 256 {
        return Err(SeiError::SubSeqLayerNumTooLarge(sub_seq_layer_num));
    }
    let sub_seq_id = r.ue()?;
    if sub_seq_id >= 65536 {
        return Err(SeiError::SubSeqIdTooLarge(sub_seq_id));
    }
    let first_ref_pic_flag = r.u(1)? == 1;
    let leading_non_ref_pic_flag = r.u(1)? == 1;
    let last_pic_flag = r.u(1)? == 1;
    let sub_seq_frame_num_flag = r.u(1)? == 1;
    let sub_seq_frame_num = if sub_seq_frame_num_flag {
        Some(r.ue()?)
    } else {
        None
    };
    Ok(SubSeqInfo {
        sub_seq_layer_num,
        sub_seq_id,
        first_ref_pic_flag,
        leading_non_ref_pic_flag,
        last_pic_flag,
        sub_seq_frame_num,
    })
}

/// §D.2.12 — one `(accurate_statistics_flag, average_bit_rate,
/// average_frame_rate)` triple, characterising the joint range of
/// sub-sequence layers `0..=layer` (the loop counter index).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SubSeqLayerCharacteristic {
    /// `true` ⇒ `average_bit_rate` / `average_frame_rate` are rounded from
    /// statistically correct values; `false` ⇒ they are estimates.
    pub accurate_statistics_flag: bool,
    /// Average bit rate in units of 1000 bits/second (`u(16)`). 0 ⇒
    /// unspecified.
    pub average_bit_rate: u16,
    /// Average frame rate in units of frames/(256 seconds) (`u(16)`). 0 ⇒
    /// unspecified.
    pub average_frame_rate: u16,
}

impl SubSeqLayerCharacteristic {
    /// Reconstruct the §D.2.13 (2024) / §D.2.12 (2003 draft) `average_bit_rate`
    /// semantic in bits/second: the on-wire field is in units of
    /// 1000 bits/second (equations D-6 / D-10), so this multiplies by 1000.
    ///
    /// Returns `None` when the on-wire field is 0 — equations D-7 / D-11
    /// reserve that sentinel for the `t1 == t2` degenerate case and for the
    /// general "unspecified" reading; the spec defines no scalar derivation
    /// from a 0 carrier so no inferred value exists.
    ///
    /// The result fits in `u32` since the on-wire range is `0..=65535` and
    /// `65535 * 1000 == 65_535_000`, well below `u32::MAX`.
    #[must_use]
    pub fn average_bit_rate_bps(&self) -> Option<u32> {
        if self.average_bit_rate == 0 {
            None
        } else {
            Some((self.average_bit_rate as u32) * 1000)
        }
    }

    /// Reconstruct the §D.2.13 (2024) / §D.2.12 (2003 draft) `average_frame_rate`
    /// semantic in frames/second: the on-wire field is in units of
    /// frames/(256 seconds) (equations D-8 / D-12), so this divides by 256.
    ///
    /// Returns `None` when the on-wire field is 0 — equations D-9 / D-13
    /// reserve that sentinel for the `t1 == t2` degenerate case and the
    /// general "unspecified" reading; the spec defines no scalar derivation
    /// from a 0 carrier so no inferred value exists.
    ///
    /// `f64` is used so the 1/256 quantisation step is exactly representable
    /// (256 is a power of two, so all on-wire values map to dyadic rationals
    /// with no rounding error).
    #[must_use]
    pub fn average_frame_rate_fps(&self) -> Option<f64> {
        if self.average_frame_rate == 0 {
            None
        } else {
            Some((self.average_frame_rate as f64) / 256.0)
        }
    }
}

/// §D.2.12 — sub_seq_layer_characteristics (payload type 11).
///
/// Specifies bit-rate / frame-rate characteristics of sub-sequence layers.
/// The `layer`-th entry characterises the joint range of layers `0..=layer`.
///
/// Syntax (§D.1.12):
/// ```text
/// sub_seq_layer_characteristics( payloadSize ) {
///   num_sub_seq_layers_minus1                 ue(v)
///   for( layer = 0; layer <= num_sub_seq_layers_minus1; layer++ ) {
///     accurate_statistics_flag                u(1)
///     average_bit_rate                        u(16)
///     average_frame_rate                      u(16)
///   }
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubSeqLayerCharacteristics {
    /// One entry per sub-sequence layer; length == `num_sub_seq_layers_minus1
    /// + 1` (1..=256 entries).
    pub layers: Vec<SubSeqLayerCharacteristic>,
}

pub fn parse_sub_seq_layer_characteristics(
    payload: &[u8],
) -> Result<SubSeqLayerCharacteristics, SeiError> {
    let mut r = BitReader::new(payload);
    let num_sub_seq_layers_minus1 = r.ue()?;
    if num_sub_seq_layers_minus1 > 255 {
        return Err(SeiError::SubSeqLayersTooLarge(num_sub_seq_layers_minus1));
    }
    let count = num_sub_seq_layers_minus1 as usize + 1;
    let mut layers = Vec::with_capacity(count);
    for _ in 0..count {
        let accurate_statistics_flag = r.u(1)? == 1;
        let average_bit_rate = r.u(16)? as u16;
        let average_frame_rate = r.u(16)? as u16;
        layers.push(SubSeqLayerCharacteristic {
            accurate_statistics_flag,
            average_bit_rate,
            average_frame_rate,
        });
    }
    Ok(SubSeqLayerCharacteristics { layers })
}

/// §D.2.13 — one referenced sub-sequence identifier triple.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SubSeqReference {
    /// Layer number of the referenced sub-sequence.
    pub ref_sub_seq_layer_num: u32,
    /// Identifier of the referenced sub-sequence within its layer.
    pub ref_sub_seq_id: u32,
    /// `false` ⇒ the referenced sub-sequence's first picture precedes the
    /// target's first picture in decoding order; `true` ⇒ it succeeds it.
    pub ref_sub_seq_direction: bool,
}

/// §D.2.13 — sub_seq_characteristics (payload type 12).
///
/// Describes the duration / rate of a target sub-sequence (the next
/// sub-sequence in decoding order with the given layer + id) and which
/// other sub-sequences it inter-predicts from.
///
/// Syntax (§D.1.13):
/// ```text
/// sub_seq_characteristics( payloadSize ) {
///   sub_seq_layer_num                         ue(v)
///   sub_seq_id                                ue(v)
///   duration_flag                             u(1)
///   if( duration_flag )
///     sub_seq_duration                        u(32)
///   average_rate_flag                         u(1)
///   if( average_rate_flag ) {
///     accurate_statistics_flag                u(1)
///     average_bit_rate                        u(16)
///     average_frame_rate                      u(16)
///   }
///   num_referenced_subseqs                    ue(v)
///   for( n = 0; n < num_referenced_subseqs; n++ ) {
///     ref_sub_seq_layer_num                   ue(v)
///     ref_sub_seq_id                          ue(v)
///     ref_sub_seq_direction                   u(1)
///   }
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubSeqCharacteristics {
    /// Layer the message applies to (< 256 per §D.2.13).
    pub sub_seq_layer_num: u32,
    /// Sub-sequence within the layer the message applies to (< 65536).
    pub sub_seq_id: u32,
    /// Duration of the target sub-sequence in 90-kHz clock ticks. Present
    /// only when `duration_flag` is 1.
    pub sub_seq_duration: Option<u32>,
    /// Average bit/frame-rate statistics. Present only when
    /// `average_rate_flag` is 1.
    pub average_rate: Option<SubSeqLayerCharacteristic>,
    /// Sub-sequences whose pictures are used as references for inter
    /// prediction by the target sub-sequence (length < 256).
    pub referenced: Vec<SubSeqReference>,
}

impl SubSeqCharacteristics {
    /// Reconstruct §D.2.13 (2024) / §D.2.12 (2003 draft) `sub_seq_duration`
    /// in seconds: the on-wire field is counted in 90-kHz clock ticks
    /// ("sub_seq_duration specifies the duration of the target sub-sequence
    /// in clock ticks of a 90-kHz clock"), so this divides by 90_000.
    ///
    /// Returns `None` when `duration_flag == 0` (the spec says: "duration_flag
    /// equal to 0 indicates that the duration of the target sub-sequence is
    /// not specified") — `sub_seq_duration` is `None` in that case and no
    /// scalar interpretation is defined.
    ///
    /// `f64` is used so the 90-kHz quantisation step is representable with
    /// only the lossless `u32`-to-`f64` widening followed by a single
    /// floating-point division; the full u(32) carrier range
    /// (0..=4_294_967_295 ticks ≈ 13.25 hours) round-trips through
    /// `f64::from(u32)` without loss.
    #[must_use]
    pub fn sub_seq_duration_seconds(&self) -> Option<f64> {
        self.sub_seq_duration
            .map(|ticks| f64::from(ticks) / 90_000.0)
    }
}

pub fn parse_sub_seq_characteristics(payload: &[u8]) -> Result<SubSeqCharacteristics, SeiError> {
    let mut r = BitReader::new(payload);
    let sub_seq_layer_num = r.ue()?;
    if sub_seq_layer_num >= 256 {
        return Err(SeiError::SubSeqLayerNumTooLarge(sub_seq_layer_num));
    }
    let sub_seq_id = r.ue()?;
    if sub_seq_id >= 65536 {
        return Err(SeiError::SubSeqIdTooLarge(sub_seq_id));
    }
    let duration_flag = r.u(1)? == 1;
    let sub_seq_duration = if duration_flag { Some(r.u(32)?) } else { None };
    let average_rate_flag = r.u(1)? == 1;
    let average_rate = if average_rate_flag {
        let accurate_statistics_flag = r.u(1)? == 1;
        let average_bit_rate = r.u(16)? as u16;
        let average_frame_rate = r.u(16)? as u16;
        Some(SubSeqLayerCharacteristic {
            accurate_statistics_flag,
            average_bit_rate,
            average_frame_rate,
        })
    } else {
        None
    };
    let num_referenced_subseqs = r.ue()?;
    if num_referenced_subseqs >= 256 {
        return Err(SeiError::SubSeqReferencedTooLarge(num_referenced_subseqs));
    }
    let mut referenced = Vec::with_capacity(num_referenced_subseqs as usize);
    for _ in 0..num_referenced_subseqs {
        let ref_sub_seq_layer_num = r.ue()?;
        let ref_sub_seq_id = r.ue()?;
        let ref_sub_seq_direction = r.u(1)? == 1;
        referenced.push(SubSeqReference {
            ref_sub_seq_layer_num,
            ref_sub_seq_id,
            ref_sub_seq_direction,
        });
    }
    Ok(SubSeqCharacteristics {
        sub_seq_layer_num,
        sub_seq_id,
        sub_seq_duration,
        average_rate,
        referenced,
    })
}

/// §D.2.18 — progressive_refinement_segment_start (payload type 16).
///
/// Marks the first picture of a sequence whose subsequent pictures
/// refine the same scene (e.g. a still photo progressively encoded).
///
/// Syntax (§D.1.18):
/// ```text
/// progressive_refinement_segment_start( payloadSize ) {
///   progressive_refinement_id              ue(v)
///   num_refinement_steps_minus1            ue(v)
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProgressiveRefinementSegmentStart {
    pub progressive_refinement_id: u32,
    pub num_refinement_steps_minus1: u32,
}

impl ProgressiveRefinementSegmentStart {
    /// §D.2.18 — total count of refinement steps the segment promises,
    /// reconstructed from the on-wire `num_refinement_steps_minus1`
    /// field.
    ///
    /// The spec text reads: "*num_refinement_steps_minus1 plus 1
    /// indicates the maximum number of refinement steps that may be
    /// used during the refinement of the picture quality from the
    /// progressive_refinement_segment_start to the corresponding
    /// progressive_refinement_segment_end*". This accessor surfaces
    /// the semantic `NumRefinementSteps = num_refinement_steps_minus1
    /// + 1` directly.
    ///
    /// The return type is `u64` so the full u(32) carrier range maps
    /// without overflow: `num_refinement_steps_minus1 == u32::MAX`
    /// (the `ue(v)` carrier is bit-bounded by the parser, but the
    /// stored field is `u32`) yields `u32::MAX + 1 == 4_294_967_296`,
    /// which would overflow a `u32` return. `u32` callers can safely
    /// downcast when the segment's carrier is known to fit, but the
    /// raw return makes no such assumption.
    ///
    /// Always returns a positive value: the on-wire `_minus1` bias
    /// guarantees `NumRefinementSteps >= 1` for any well-formed
    /// payload, so the return is unconditional `u64` rather than
    /// `Option<u64>` — unlike the `Option`-returning accessors that
    /// guard against an unsignalled-flag sentinel, this field has no
    /// "absent" encoding.
    #[must_use]
    pub fn num_refinement_steps(&self) -> u64 {
        u64::from(self.num_refinement_steps_minus1) + 1
    }
}

pub fn parse_progressive_refinement_segment_start(
    payload: &[u8],
) -> Result<ProgressiveRefinementSegmentStart, SeiError> {
    let mut r = BitReader::new(payload);
    let progressive_refinement_id = r.ue()?;
    let num_refinement_steps_minus1 = r.ue()?;
    Ok(ProgressiveRefinementSegmentStart {
        progressive_refinement_id,
        num_refinement_steps_minus1,
    })
}

/// §D.2.19 — progressive_refinement_segment_end (payload type 17).
///
/// Closes a refinement segment opened by payload type 16, identified
/// by the matching `progressive_refinement_id`.
///
/// Syntax (§D.1.19):
/// ```text
/// progressive_refinement_segment_end( payloadSize ) {
///   progressive_refinement_id              ue(v)
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProgressiveRefinementSegmentEnd {
    pub progressive_refinement_id: u32,
}

pub fn parse_progressive_refinement_segment_end(
    payload: &[u8],
) -> Result<ProgressiveRefinementSegmentEnd, SeiError> {
    let mut r = BitReader::new(payload);
    let progressive_refinement_id = r.ue()?;
    Ok(ProgressiveRefinementSegmentEnd {
        progressive_refinement_id,
    })
}

/// §D.2.23 — stereo_video_info (payload type 21).
///
/// Pre-dates the more capable frame_packing_arrangement (payload 45);
/// still used by some legacy stereoscopic streams. Identifies which
/// field / frame carries the left vs right view.
///
/// Syntax (§D.1.23):
/// ```text
/// stereo_video_info( payloadSize ) {
///   field_views_flag                       u(1)
///   if( field_views_flag )
///     top_field_is_left_view_flag          u(1)
///   else {
///     current_frame_is_left_view_flag      u(1)
///     next_frame_is_second_view_flag       u(1)
///   }
///   left_view_self_contained_flag          u(1)
///   right_view_self_contained_flag         u(1)
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StereoVideoInfo {
    pub field_views_flag: bool,
    /// Present only when `field_views_flag` is true.
    pub top_field_is_left_view_flag: Option<bool>,
    /// Present only when `field_views_flag` is false.
    pub current_frame_is_left_view_flag: Option<bool>,
    /// Present only when `field_views_flag` is false.
    pub next_frame_is_second_view_flag: Option<bool>,
    pub left_view_self_contained_flag: bool,
    pub right_view_self_contained_flag: bool,
}

pub fn parse_stereo_video_info(payload: &[u8]) -> Result<StereoVideoInfo, SeiError> {
    let mut r = BitReader::new(payload);
    let field_views_flag = r.u(1)? == 1;
    let (
        top_field_is_left_view_flag,
        current_frame_is_left_view_flag,
        next_frame_is_second_view_flag,
    ) = if field_views_flag {
        (Some(r.u(1)? == 1), None, None)
    } else {
        let cur = r.u(1)? == 1;
        let nxt = r.u(1)? == 1;
        (None, Some(cur), Some(nxt))
    };
    let left_view_self_contained_flag = r.u(1)? == 1;
    let right_view_self_contained_flag = r.u(1)? == 1;
    Ok(StereoVideoInfo {
        field_views_flag,
        top_field_is_left_view_flag,
        current_frame_is_left_view_flag,
        next_frame_is_second_view_flag,
        left_view_self_contained_flag,
        right_view_self_contained_flag,
    })
}

/// §D.2.20 — motion_constrained_slice_group_set (payload type 18).
///
/// Identifies a set of slice groups that the encoder promises to
/// motion-compensate only from samples inside the same set. Used by
/// ROI / sub-picture decoding workflows.
///
/// Syntax (§D.1.20):
/// ```text
/// motion_constrained_slice_group_set( payloadSize ) {
///   num_slice_groups_in_set_minus1         ue(v)
///   for( i = 0; i <= num_slice_groups_in_set_minus1; i++ )
///     slice_group_id[ i ]                  u(v)   // v = Ceil(Log2(num_slice_groups))
///   exact_sample_value_match_flag          u(1)
///   pan_scan_rect_flag                     u(1)
///   if( pan_scan_rect_flag )
///     pan_scan_rect_id                     ue(v)
/// }
/// ```
///
/// Note on slice_group_id width: §D.1.20 specifies `u(v)` for
/// slice_group_id[i] with `v = Ceil(Log2(num_slice_groups_minus1 + 1))`
/// per the active PPS. We accept the width hint via [`SeiContext`]
/// (`num_slice_groups`); when the field is missing or `<= 1` the §D.2.20
/// width collapses to 0 bits and no slice_group_id is read.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MotionConstrainedSliceGroupSet {
    pub slice_group_ids: Vec<u32>,
    pub exact_sample_value_match_flag: bool,
    pub pan_scan_rect_flag: bool,
    /// Present only when `pan_scan_rect_flag` is true.
    pub pan_scan_rect_id: Option<u32>,
}

pub fn parse_motion_constrained_slice_group_set(
    payload: &[u8],
    ctx: &SeiContext,
) -> Result<MotionConstrainedSliceGroupSet, SeiError> {
    let mut r = BitReader::new(payload);
    let num_slice_groups_in_set_minus1 = r.ue()?;
    // §D.1.20 — slice_group_id[ i ] u(v). The width is Ceil(Log2(num_slice_groups))
    // taken from the active PPS via `SeiContext::num_slice_groups`. When the
    // PPS only declares a single slice group, the encoded width is 0 bits
    // and we read zero slice_group_id values.
    let num_slice_groups = ctx.num_slice_groups.max(1);
    let width = if num_slice_groups <= 1 {
        0
    } else {
        // Ceil(Log2(num_slice_groups)).
        32 - (num_slice_groups - 1).leading_zeros()
    };
    // §D.2.20 — "The allowed range of num_slice_groups_in_set_minus1 is 0 to
    // num_slice_groups_minus1, inclusive." Reject anything larger before
    // allocating the per-id loop: the inner read is `u(width)` bits, and at
    // `width == 0` (single-slice-group PPS) the loop reads zero bits per
    // iteration, so an unbounded `ue(v)` count would drive an unbounded
    // `Vec::push(0)` and OOM the decoder. Fuzz crash from
    // OxideAV/oxideav-h264#1044 reached this path via path-2 of the
    // `sei_payload` target.
    if num_slice_groups_in_set_minus1 >= num_slice_groups {
        return Err(SeiError::MotionConstrainedSliceGroupCountTooLarge {
            count: num_slice_groups_in_set_minus1.saturating_add(1),
            num_slice_groups,
        });
    }
    let count = (num_slice_groups_in_set_minus1 as usize) + 1;
    let mut slice_group_ids = Vec::with_capacity(count);
    for i in 0..count {
        let v = if width == 0 { 0 } else { r.u(width)? };
        // §D.2.20 — "slice_group_id[ i ] ... identifies the slice group(s)
        // contained within the slice group set" with the same per-PPS range
        // as §7.4.2.2's slice_group_id (0..=num_slice_groups_minus1). Reject
        // out-of-range ids so the message can't point at a slice group that
        // the active PPS never declared.
        if v >= num_slice_groups {
            return Err(SeiError::MotionConstrainedSliceGroupIdOutOfRange {
                i,
                id: v,
                num_slice_groups,
            });
        }
        slice_group_ids.push(v);
    }
    let exact_sample_value_match_flag = r.u(1)? == 1;
    let pan_scan_rect_flag = r.u(1)? == 1;
    let pan_scan_rect_id = if pan_scan_rect_flag {
        Some(r.ue()?)
    } else {
        None
    };
    Ok(MotionConstrainedSliceGroupSet {
        slice_group_ids,
        exact_sample_value_match_flag,
        pan_scan_rect_flag,
        pan_scan_rect_id,
    })
}

/// §D.2.24 — post_filter_hint (payload type 22).
///
/// Syntax (§D.1.24):
/// ```text
/// post_filter_hint( payloadSize ) {
///   filter_hint_size_y                                    ue(v)
///   filter_hint_size_x                                    ue(v)
///   filter_hint_type                                      u(2)
///   for( cIdx = 0; cIdx < 3; cIdx++ )
///     for( cy = 0; cy < filter_hint_size_y; cy++ )
///       for( cx = 0; cx < filter_hint_size_x; cx++ )
///         filter_hint_value[ cIdx ][ cx ][ cy ]           se(v)
///   additional_extension_flag                             u(1)
/// }
/// ```
///
/// Returns [`SeiError::PostFilterHintSizeOutOfRange`] if either size
/// falls outside the §D.2.24 1..=15 range,
/// [`SeiError::PostFilterHintTypeReserved`] if `filter_hint_type == 3`
/// (reserved by ITU-T | ISO/IEC), and
/// [`SeiError::PostFilterHintType1WrongHeight`] if
/// `filter_hint_type == 1` but `filter_hint_size_y != 2`.
pub fn parse_post_filter_hint(payload: &[u8]) -> Result<PostFilterHint, SeiError> {
    let mut r = BitReader::new(payload);
    let filter_hint_size_y = r.ue()?;
    let filter_hint_size_x = r.ue()?;
    if !(1..=15).contains(&filter_hint_size_y) || !(1..=15).contains(&filter_hint_size_x) {
        return Err(SeiError::PostFilterHintSizeOutOfRange {
            x: filter_hint_size_x,
            y: filter_hint_size_y,
        });
    }
    let filter_hint_type = r.u(2)? as u8;
    if filter_hint_type == 3 {
        return Err(SeiError::PostFilterHintTypeReserved);
    }
    if filter_hint_type == 1 && filter_hint_size_y != 2 {
        return Err(SeiError::PostFilterHintType1WrongHeight(filter_hint_size_y));
    }
    let total = 3usize * filter_hint_size_y as usize * filter_hint_size_x as usize;
    let mut filter_hint_value = Vec::with_capacity(total);
    for _ in 0..total {
        filter_hint_value.push(r.se()?);
    }
    let additional_extension_flag = r.u(1)? == 1;
    Ok(PostFilterHint {
        filter_hint_size_x,
        filter_hint_size_y,
        filter_hint_type,
        filter_hint_value,
        additional_extension_flag,
    })
}

/// §D.2.25 — tone_mapping_info (payload type 23).
///
/// Syntax (§D.1.25):
/// ```text
/// tone_mapping_info( payloadSize ) {
///   tone_map_id                                   ue(v)
///   tone_map_cancel_flag                          u(1)
///   if( !tone_map_cancel_flag ) {
///     tone_map_repetition_period                  ue(v)
///     coded_data_bit_depth                        u(8)
///     target_bit_depth                            u(8)
///     tone_map_model_id                           ue(v)
///     if( tone_map_model_id == 0 ) {
///       min_value                                 u(32)
///       max_value                                 u(32)
///     }
///     if( tone_map_model_id == 1 ) {
///       sigmoid_midpoint                          u(32)
///       sigmoid_width                             u(32)
///     }
///     if( tone_map_model_id == 2 )
///       for( i = 0; i < ( 1 << target_bit_depth ); i++ )
///         start_of_coded_interval[ i ]            u(v)
///     if( tone_map_model_id == 3 ) {
///       num_pivots                                u(16)
///       for( i = 0; i < num_pivots; i++ ) {
///         coded_pivot_value[ i ]                  u(v)
///         target_pivot_value[ i ]                 u(v)
///       }
///     }
///     if( tone_map_model_id == 4 ) { ... }
///   }
/// }
/// ```
///
/// Per §D.2.25:
/// - `start_of_coded_interval` bit width is
///   `((coded_data_bit_depth + 7) >> 3) << 3`.
/// - `coded_pivot_value` bit width is
///   `((coded_data_bit_depth + 7) >> 3) << 3`.
/// - `target_pivot_value` bit width is
///   `((target_bit_depth + 7) >> 3) << 3`.
///
/// This implementation decodes model_id 0..=3. model_id 4 (and any
/// future value) is captured as `ToneMappingModel::Reserved` with
/// the remaining payload bytes preserved so callers can keep it.
pub fn parse_tone_mapping_info(payload: &[u8]) -> Result<ToneMappingInfo, SeiError> {
    let mut r = BitReader::new(payload);
    let tone_map_id = r.ue()?;
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(ToneMappingInfo {
            tone_map_id,
            cancel_flag,
            body: None,
        });
    }

    let repetition_period = r.ue()?;
    let coded_data_bit_depth = r.u(8)? as u8;
    let target_bit_depth = r.u(8)? as u8;
    let model_id = r.ue()?;

    let coded_width = ((coded_data_bit_depth as u32 + 7) >> 3) << 3;
    let target_width = ((target_bit_depth as u32 + 7) >> 3) << 3;

    let model = match model_id {
        0 => {
            let min_value = r.u(32)?;
            let max_value = r.u(32)?;
            ToneMappingModel::Linear {
                min_value,
                max_value,
            }
        }
        1 => {
            let sigmoid_midpoint = r.u(32)?;
            let sigmoid_width = r.u(32)?;
            ToneMappingModel::Sigmoid {
                sigmoid_midpoint,
                sigmoid_width,
            }
        }
        2 => {
            // Per §D.2.25 there are (1 << target_bit_depth) entries.
            // Guard against pathological values so we don't allocate
            // several GB on a corrupted stream.
            if target_bit_depth > 16 || coded_width == 0 {
                return Err(SeiError::UnsupportedPayloadType(23));
            }
            let count = 1usize << target_bit_depth as usize;
            let mut vals = Vec::with_capacity(count);
            for _ in 0..count {
                vals.push(r.u(coded_width)?);
            }
            ToneMappingModel::StartOfCodedInterval {
                start_of_coded_interval: vals,
            }
        }
        3 => {
            let num_pivots = r.u(16)? as u16;
            if coded_width == 0 || target_width == 0 {
                return Err(SeiError::UnsupportedPayloadType(23));
            }
            let mut coded_pivot_value = Vec::with_capacity(num_pivots as usize);
            let mut target_pivot_value = Vec::with_capacity(num_pivots as usize);
            for _ in 0..num_pivots {
                coded_pivot_value.push(r.u(coded_width)?);
                target_pivot_value.push(r.u(target_width)?);
            }
            ToneMappingModel::PiecewisePivots {
                num_pivots,
                coded_pivot_value,
                target_pivot_value,
            }
        }
        _ => {
            // model_id >= 4: preserve remaining bytes so callers can
            // keep the payload for later forwarding or custom parsing.
            let (byte_pos, bit_pos) = r.position();
            let remaining_bytes = if bit_pos == 0 {
                payload.get(byte_pos..).unwrap_or(&[]).to_vec()
            } else {
                // Skip to next byte boundary before copying; bits
                // within the partial byte are discarded in this
                // first pass.
                payload.get(byte_pos + 1..).unwrap_or(&[]).to_vec()
            };
            ToneMappingModel::Reserved {
                model_id,
                remaining_bytes,
            }
        }
    };

    Ok(ToneMappingInfo {
        tone_map_id,
        cancel_flag,
        body: Some(ToneMappingBody {
            repetition_period,
            coded_data_bit_depth,
            target_bit_depth,
            model_id,
            model,
        }),
    })
}

/// §D.2.21 — film_grain_characteristics (payload type 19).
///
/// Syntax (§D.1.21):
/// ```text
/// film_grain_characteristics( payloadSize ) {
///   film_grain_characteristics_cancel_flag                  u(1)
///   if( !film_grain_characteristics_cancel_flag ) {
///     film_grain_model_id                                   u(2)
///     separate_colour_description_present_flag              u(1)
///     if( separate_colour_description_present_flag ) {
///       film_grain_bit_depth_luma_minus8                    u(3)
///       film_grain_bit_depth_chroma_minus8                  u(3)
///       film_grain_full_range_flag                          u(1)
///       film_grain_colour_primaries                         u(8)
///       film_grain_transfer_characteristics                 u(8)
///       film_grain_matrix_coefficients                      u(8)
///     }
///     blending_mode_id                                      u(2)
///     log2_scale_factor                                     u(4)
///     for( c = 0; c < 3; c++ )
///       comp_model_present_flag[ c ]                        u(1)
///     for( c = 0; c < 3; c++ )
///       if( comp_model_present_flag[ c ] ) {
///         num_intensity_intervals_minus1[ c ]               u(8)
///         num_model_values_minus1[ c ]                      u(3)
///         for( i = 0; i <= num_intensity_intervals_minus1[ c ]; i++ ) {
///           intensity_interval_lower_bound[ c ][ i ]        u(8)
///           intensity_interval_upper_bound[ c ][ i ]        u(8)
///           for( j = 0; j <= num_model_values_minus1[ c ]; j++ )
///             comp_model_value[ c ][ i ][ j ]               se(v)
///         }
///       }
///     film_grain_characteristics_repetition_period          ue(v)
///   }
/// }
/// ```
///
/// Fully decodes the per-component intensity-interval body. Per
/// §D.2.21 `num_model_values_minus1[c]` must be in 0..=5; values
/// outside that range produce
/// [`SeiError::FilmGrainNumModelValuesOutOfRange`]. The
/// `num_intensity_intervals_minus1[c] + 1` interval count uses the
/// full u(8) range (0..=256 entries per component); pathological
/// streams therefore allocate at most ~1.5 KiB per component for the
/// interval-bounds list and 6 × 4 B × 256 ≈ 6 KiB per component for
/// `comp_model_value`, both bounded.
pub fn parse_film_grain_characteristics(
    payload: &[u8],
) -> Result<FilmGrainCharacteristics, SeiError> {
    let mut r = BitReader::new(payload);
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(FilmGrainCharacteristics {
            cancel_flag,
            body: None,
        });
    }

    let model_id = r.u(2)? as u8;
    let separate_flag = r.u(1)? == 1;
    let separate_colour_description = if separate_flag {
        let bit_depth_luma_minus8 = r.u(3)? as u8;
        let bit_depth_chroma_minus8 = r.u(3)? as u8;
        let full_range_flag = r.u(1)? == 1;
        let colour_primaries = r.u(8)? as u8;
        let transfer_characteristics = r.u(8)? as u8;
        let matrix_coefficients = r.u(8)? as u8;
        Some(FilmGrainSeparateColourDescription {
            bit_depth_luma_minus8,
            bit_depth_chroma_minus8,
            full_range_flag,
            colour_primaries,
            transfer_characteristics,
            matrix_coefficients,
        })
    } else {
        None
    };

    let blending_mode_id = r.u(2)? as u8;
    let log2_scale_factor = r.u(4)? as u8;

    let mut comp_model_present_flag = [false; 3];
    for flag in comp_model_present_flag.iter_mut() {
        *flag = r.u(1)? == 1;
    }

    let mut components: [Option<FilmGrainComponent>; 3] = [None, None, None];
    for (c, present) in comp_model_present_flag.iter().enumerate() {
        if !*present {
            continue;
        }
        let num_intensity_intervals_minus1 = r.u(8)? as u8;
        let num_model_values_minus1 = r.u(3)? as u8;
        if num_model_values_minus1 > 5 {
            return Err(SeiError::FilmGrainNumModelValuesOutOfRange {
                component: c,
                got: num_model_values_minus1,
            });
        }
        let interval_count = num_intensity_intervals_minus1 as usize + 1;
        let mvalue_count = num_model_values_minus1 as usize + 1;
        let mut intensity_intervals = Vec::with_capacity(interval_count);
        for _ in 0..interval_count {
            let lower_bound = r.u(8)? as u8;
            let upper_bound = r.u(8)? as u8;
            let mut model_values = Vec::with_capacity(mvalue_count);
            for _ in 0..mvalue_count {
                model_values.push(r.se()?);
            }
            intensity_intervals.push(FilmGrainIntensityInterval {
                lower_bound,
                upper_bound,
                model_values,
            });
        }
        components[c] = Some(FilmGrainComponent {
            num_model_values_minus1,
            intensity_intervals,
        });
    }

    let repetition_period = r.ue()?;

    Ok(FilmGrainCharacteristics {
        cancel_flag,
        body: Some(FilmGrainBody {
            model_id,
            separate_colour_description_present_flag: separate_flag,
            separate_colour_description,
            blending_mode_id,
            log2_scale_factor,
            comp_model_present_flag,
            components,
            repetition_period,
        }),
    })
}

// ---------------------------------------------------------------------------
// Round 107 — content_colour_volume (payload type 149).
//
// HDR colour-volume metadata, parallel in shape to mastering_display
// (137) / content_light_level (144). cancel/persistence gate plus four
// independent "present" flags select which sub-blocks (primaries, min /
// max / avg luminance) follow. Range + ordering validation per §D.2.33.
// ---------------------------------------------------------------------------

/// One CIE-1931 chromaticity coordinate pair for a content colour
/// primary, in normalized increments of 0.00002. Per §D.2.33 each
/// component is signed `i(32)` constrained to [-5 000 000, 5 000 000].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContentColourVolumePrimary {
    /// ccv_primaries_x[ c ] — CIE 1931 x, units of 0.00002.
    pub x: i32,
    /// ccv_primaries_y[ c ] — CIE 1931 y, units of 0.00002.
    pub y: i32,
}

/// §D.2.33 — content_colour_volume (payload type 149).
///
/// Describes the nominal colour volume (display gamut + luminance
/// range) of the associated pictures. Complements mastering_display
/// (137, the *mastering* display's volume) and content_light_level
/// (144, MaxCLL / MaxFALL): this message records the volume actually
/// *present in the content* rather than the mastering display's
/// capability.
///
/// Syntax (§D.1.33):
/// ```text
/// content_colour_volume( payloadSize ) {
///   ccv_cancel_flag                              u(1)
///   if( !ccv_cancel_flag ) {
///     ccv_persistence_flag                       u(1)
///     ccv_primaries_present_flag                 u(1)
///     ccv_min_luminance_value_present_flag       u(1)
///     ccv_max_luminance_value_present_flag       u(1)
///     ccv_avg_luminance_value_present_flag       u(1)
///     ccv_reserved_zero_2bits                    u(2)
///     if( ccv_primaries_present_flag )
///       for( c = 0; c < 3; c++ ) {
///         ccv_primaries_x[ c ]                   i(32)
///         ccv_primaries_y[ c ]                   i(32)
///       }
///     if( ccv_min_luminance_value_present_flag )
///       ccv_min_luminance_value                  u(32)
///     if( ccv_max_luminance_value_present_flag )
///       ccv_max_luminance_value                  u(32)
///     if( ccv_avg_luminance_value_present_flag )
///       ccv_avg_luminance_value                  u(32)
///   }
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContentColourVolume {
    /// ccv_cancel_flag. When `true`, cancels persistence of any prior
    /// content colour volume SEI message and no further fields follow.
    pub cancel_flag: bool,
    /// Present only when `cancel_flag == false`.
    pub body: Option<ContentColourVolumeBody>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContentColourVolumeBody {
    /// ccv_persistence_flag.
    pub persistence_flag: bool,
    /// ccv_primaries_x/y[ 0..3 ]. Present only when
    /// `ccv_primaries_present_flag == 1`. Per §D.2.33 the suggested
    /// ordering is green (0), blue (1), red (2).
    pub primaries: Option<[ContentColourVolumePrimary; 3]>,
    /// ccv_min_luminance_value, units of 0.0000001. Present only when
    /// `ccv_min_luminance_value_present_flag == 1`.
    pub min_luminance_value: Option<u32>,
    /// ccv_max_luminance_value, units of 0.0000001. Present only when
    /// `ccv_max_luminance_value_present_flag == 1`.
    pub max_luminance_value: Option<u32>,
    /// ccv_avg_luminance_value, units of 0.0000001. Present only when
    /// `ccv_avg_luminance_value_present_flag == 1`.
    pub avg_luminance_value: Option<u32>,
}

/// §D.2.33 — parse content_colour_volume (payload type 149).
pub fn parse_content_colour_volume(payload: &[u8]) -> Result<ContentColourVolume, SeiError> {
    let mut r = BitReader::new(payload);
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(ContentColourVolume {
            cancel_flag,
            body: None,
        });
    }
    let persistence_flag = r.u(1)? == 1;
    let primaries_present = r.u(1)? == 1;
    let min_present = r.u(1)? == 1;
    let max_present = r.u(1)? == 1;
    let avg_present = r.u(1)? == 1;
    // §D.2.33: ccv_reserved_zero_2bits — "Decoders shall ignore the
    // value", but the two bits are still consumed.
    let _reserved = r.u(2)?;

    // §D.2.33 bitstream conformance: the four present flags shall not
    // all be 0.
    if !primaries_present && !min_present && !max_present && !avg_present {
        return Err(SeiError::ContentColourVolumeNoComponents);
    }

    let primaries = if primaries_present {
        let mut prims = [ContentColourVolumePrimary { x: 0, y: 0 }; 3];
        for (c, prim) in prims.iter_mut().enumerate() {
            let x = r.i(32)?;
            let y = r.i(32)?;
            // §D.2.33: values shall be in [-5 000 000, 5 000 000].
            const PRIM_LO: i32 = -5_000_000;
            const PRIM_HI: i32 = 5_000_000;
            if !(PRIM_LO..=PRIM_HI).contains(&x) || !(PRIM_LO..=PRIM_HI).contains(&y) {
                return Err(SeiError::ContentColourVolumePrimaryOutOfRange { c, x, y });
            }
            *prim = ContentColourVolumePrimary { x, y };
        }
        Some(prims)
    } else {
        None
    };

    let min_luminance_value = if min_present { Some(r.u(32)?) } else { None };
    let max_luminance_value = if max_present { Some(r.u(32)?) } else { None };
    let avg_luminance_value = if avg_present { Some(r.u(32)?) } else { None };

    // §D.2.33 ordering: min <= avg <= max, min <= max, for whichever
    // values are present. Each pairwise constraint applies only when
    // both members of the pair are present.
    if let (Some(min), Some(avg)) = (min_luminance_value, avg_luminance_value) {
        if min > avg {
            return Err(SeiError::ContentColourVolumeLuminanceOrder {
                min: min as u64,
                avg: avg as u64,
                max: max_luminance_value.map_or(u64::MAX, |m| m as u64),
            });
        }
    }
    if let (Some(avg), Some(max)) = (avg_luminance_value, max_luminance_value) {
        if avg > max {
            return Err(SeiError::ContentColourVolumeLuminanceOrder {
                min: min_luminance_value.map_or(0, |m| m as u64),
                avg: avg as u64,
                max: max as u64,
            });
        }
    }
    if let (Some(min), Some(max)) = (min_luminance_value, max_luminance_value) {
        if min > max {
            return Err(SeiError::ContentColourVolumeLuminanceOrder {
                min: min as u64,
                avg: avg_luminance_value.map_or(min as u64, |a| a as u64),
                max: max as u64,
            });
        }
    }

    Ok(ContentColourVolume {
        cancel_flag,
        body: Some(ContentColourVolumeBody {
            persistence_flag,
            primaries,
            min_luminance_value,
            max_luminance_value,
            avg_luminance_value,
        }),
    })
}

// ---------------------------------------------------------------------------
// Round 117 — colour_remapping_info (payload type 142, §D.1.30 / §D.2.30).
// ---------------------------------------------------------------------------

/// §D.2.30 — colour_remap_video_signal_info: the optional triple of
/// colour-space descriptors (E.2.1 semantics) for the *remapped*
/// reconstructed picture. Present only when
/// `colour_remap_video_signal_info_present_flag == 1`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ColourRemapVideoSignalInfo {
    /// colour_remap_full_range_flag — E.2.1 `video_full_range_flag`
    /// semantics, applied to the remapped picture.
    pub full_range_flag: bool,
    /// colour_remap_primaries — E.2.1 `colour_primaries`.
    pub primaries: u8,
    /// colour_remap_transfer_function — E.2.1 `transfer_characteristics`.
    pub transfer_function: u8,
    /// colour_remap_matrix_coefficients — E.2.1 `matrix_coefficients`.
    pub matrix_coefficients: u8,
}

/// One (coded, target) pivot point of a piece-wise linear LUT, for one
/// colour component. Used for both the pre-LUT and the post-LUT.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ColourRemapLutEntry {
    /// `pre_lut_coded_value` / `post_lut_coded_value`.
    pub coded_value: u32,
    /// `pre_lut_target_value` / `post_lut_target_value`.
    pub target_value: u32,
}

/// The 3×3 colour-remapping matrix (§D.2.30). Present only when
/// `colour_remap_matrix_present_flag == 1`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ColourRemapMatrix {
    /// log2_matrix_denom — base-2 log of the shared denominator, 0..=15.
    pub log2_matrix_denom: u8,
    /// colour_remap_coeffs[ c ][ i ], row-major (c outer, i inner). Each
    /// value is in [-2^15, 2^15 - 1].
    pub coeffs: [[i32; 3]; 3],
}

/// §D.2.30 — colour_remapping_info (payload type 142).
///
/// Conveys a colour-remapping function — an optional 3-component
/// pre-LUT, an optional 3×3 matrix, and an optional 3-component post-LUT
/// — that maps the decoded samples (interpreted at
/// `colour_remap_input_bit_depth`) to a remapped colour space at
/// `colour_remap_output_bit_depth`. Display application of the function
/// is outside the scope of this Specification; this layer parses and
/// validates the descriptor.
///
/// Syntax (§D.1.30):
/// ```text
/// colour_remapping_info( payloadSize ) {
///   colour_remap_id                              ue(v)
///   colour_remap_cancel_flag                     u(1)
///   if( !colour_remap_cancel_flag ) {
///     colour_remap_repetition_period             ue(v)
///     colour_remap_video_signal_info_present_flag u(1)
///     if( ... ) { full_range u(1); primaries u(8);
///                 transfer u(8); matrix u(8) }
///     colour_remap_input_bit_depth               u(8)
///     colour_remap_output_bit_depth              u(8)
///     for( c = 0; c < 3; c++ ) {                 // pre-LUT
///       pre_lut_num_val_minus1[c]                u(8)
///       if( > 0 ) for(i..) { coded u(v); target u(v) }
///     }
///     colour_remap_matrix_present_flag           u(1)
///     if( ... ) { log2_matrix_denom u(4);
///                 colour_remap_coeffs[c][i] se(v) }
///     for( c = 0; c < 3; c++ ) {                 // post-LUT
///       post_lut_num_val_minus1[c]               u(8)
///       if( > 0 ) for(i..) { coded u(v); target u(v) }
///     }
///   }
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColourRemappingInfo {
    /// colour_remap_id — identifying number, 0..=2^32 - 2.
    pub colour_remap_id: u32,
    /// colour_remap_cancel_flag. When `true`, the message cancels prior
    /// persistence and no further fields follow.
    pub cancel_flag: bool,
    /// Present only when `cancel_flag == false`.
    pub body: Option<ColourRemappingInfoBody>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColourRemappingInfoBody {
    /// colour_remap_repetition_period — 0..=16384.
    pub repetition_period: u32,
    /// colour_remap_video_signal_info, present only when its
    /// present_flag was set.
    pub video_signal_info: Option<ColourRemapVideoSignalInfo>,
    /// colour_remap_input_bit_depth — 8..=16.
    pub input_bit_depth: u8,
    /// colour_remap_output_bit_depth — 8..=16.
    pub output_bit_depth: u8,
    /// pre-LUT for the three components (0 = luma/G, 1 = Cb/B,
    /// 2 = Cr/R). An empty inner Vec means `pre_lut_num_val_minus1 == 0`
    /// (default end points, no coded pivots).
    pub pre_lut: [Vec<ColourRemapLutEntry>; 3],
    /// colour_remap_matrix, present only when its present_flag was set.
    pub matrix: Option<ColourRemapMatrix>,
    /// post-LUT for the three components, same convention as `pre_lut`.
    pub post_lut: [Vec<ColourRemapLutEntry>; 3],
}

/// §D.2.30 — parse colour_remapping_info (payload type 142).
pub fn parse_colour_remapping_info(payload: &[u8]) -> Result<ColourRemappingInfo, SeiError> {
    let mut r = BitReader::new(payload);
    let colour_remap_id = r.ue()?;
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(ColourRemappingInfo {
            colour_remap_id,
            cancel_flag,
            body: None,
        });
    }

    let repetition_period = r.ue()?;
    // §D.2.30: shall be in 0..=16384.
    if repetition_period > 16_384 {
        return Err(SeiError::ColourRemapRepetitionPeriodOutOfRange(
            repetition_period,
        ));
    }

    let video_signal_info = if r.u(1)? == 1 {
        let full_range_flag = r.u(1)? == 1;
        let primaries = r.u(8)? as u8;
        let transfer_function = r.u(8)? as u8;
        let matrix_coefficients = r.u(8)? as u8;
        Some(ColourRemapVideoSignalInfo {
            full_range_flag,
            primaries,
            transfer_function,
            matrix_coefficients,
        })
    } else {
        None
    };

    let input_bit_depth = r.u(8)? as u8;
    let output_bit_depth = r.u(8)? as u8;
    // §D.2.30: both shall be in 8..=16; decoders shall ignore messages
    // outside the range. We surface that as an error so the caller can
    // drop the message.
    if !(8..=16).contains(&input_bit_depth) {
        return Err(SeiError::ColourRemapBitDepthOutOfRange {
            which: "input",
            got: input_bit_depth,
        });
    }
    if !(8..=16).contains(&output_bit_depth) {
        return Err(SeiError::ColourRemapBitDepthOutOfRange {
            which: "output",
            got: output_bit_depth,
        });
    }

    // §D.2.30: number of bits for a LUT value is
    // ( ( bit_depth + 7 ) >> 3 ) << 3 — i.e. round the bit depth up to a
    // whole number of bytes. With bit_depth in 8..=16 this is 8 or 16.
    let coded_bits = |bd: u8| -> u32 { ((bd as u32 + 7) >> 3) << 3 };
    let pre_coded_bits = coded_bits(input_bit_depth);
    let pre_target_bits = coded_bits(output_bit_depth);
    let post_coded_bits = coded_bits(output_bit_depth);
    let post_target_bits = coded_bits(output_bit_depth);

    let read_lut = |r: &mut BitReader,
                    coded_bits: u32,
                    target_bits: u32,
                    which: &'static str|
     -> Result<[Vec<ColourRemapLutEntry>; 3], SeiError> {
        let mut lut: [Vec<ColourRemapLutEntry>; 3] = [Vec::new(), Vec::new(), Vec::new()];
        for (c, comp) in lut.iter_mut().enumerate() {
            let num_val_minus1 = r.u(8)? as u8;
            // §D.2.30: pre/post_lut_num_val_minus1[c] shall be in 0..=32.
            if num_val_minus1 > 32 {
                return Err(SeiError::ColourRemapLutCountOutOfRange {
                    which,
                    c,
                    got: num_val_minus1,
                });
            }
            // The syntax only emits pivots when num_val_minus1 > 0; a
            // value of 0 means the default end points (no coded entries).
            if num_val_minus1 > 0 {
                for _ in 0..=num_val_minus1 {
                    let coded_value = r.u(coded_bits)?;
                    let target_value = r.u(target_bits)?;
                    comp.push(ColourRemapLutEntry {
                        coded_value,
                        target_value,
                    });
                }
            }
        }
        Ok(lut)
    };

    let pre_lut = read_lut(&mut r, pre_coded_bits, pre_target_bits, "pre")?;

    let matrix = if r.u(1)? == 1 {
        let log2_matrix_denom = r.u(4)? as u8; // u(4) ⇒ 0..=15, in-range by construction.
        let mut coeffs = [[0i32; 3]; 3];
        for row in coeffs.iter_mut() {
            for coeff in row.iter_mut() {
                let v = r.se()?;
                // §D.2.30: colour_remap_coeffs shall be in [-2^15, 2^15 - 1].
                if !(-(1 << 15)..=(1 << 15) - 1).contains(&v) {
                    return Err(SeiError::ColourRemapCoeffOutOfRange(v));
                }
                *coeff = v;
            }
        }
        Some(ColourRemapMatrix {
            log2_matrix_denom,
            coeffs,
        })
    } else {
        None
    };

    let post_lut = read_lut(&mut r, post_coded_bits, post_target_bits, "post")?;

    Ok(ColourRemappingInfo {
        colour_remap_id,
        cancel_flag,
        body: Some(ColourRemappingInfoBody {
            repetition_period,
            video_signal_info,
            input_bit_depth,
            output_bit_depth,
            pre_lut,
            matrix,
            post_lut,
        }),
    })
}

// ---------------------------------------------------------------------------
// Round 78 — four additional Annex D payload parsers (HDR + 360 family).
//
// All four are syntactically fixed-width u(n) / i(n) reads, parallel to the
// existing mastering_display / content_light_level / display_orientation
// shape. Range validation per §D.2.34 / §D.2.35.* is policed at parse time.
// ---------------------------------------------------------------------------

/// §D.2.34 — ambient_viewing_environment (payload type 148).
///
/// Describes the nominal ambient viewing environment (illuminance +
/// background chromaticity) the receiving system should assume when
/// adapting the decoded picture for display.
///
/// Syntax (§D.1.34):
/// ```text
/// ambient_viewing_environment( payloadSize ) {
///   ambient_illuminance      u(32)   // units of 0.0001 lux, nonzero
///   ambient_light_x          u(16)   // 0..=50000, CIE 1931 x * 50000
///   ambient_light_y          u(16)   // 0..=50000, CIE 1931 y * 50000
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AmbientViewingEnvironment {
    pub ambient_illuminance: u32,
    pub ambient_light_x: u16,
    pub ambient_light_y: u16,
}

pub fn parse_ambient_viewing_environment(
    payload: &[u8],
) -> Result<AmbientViewingEnvironment, SeiError> {
    let mut r = BitReader::new(payload);
    let ambient_illuminance = r.u(32)?;
    let ambient_light_x = r.u(16)?;
    let ambient_light_y = r.u(16)?;
    // §D.2.34: "ambient_illuminance shall not be equal to 0".
    if ambient_illuminance == 0 {
        return Err(SeiError::AmbientIlluminanceZero);
    }
    // §D.2.34: "The values of ambient_light_x and ambient_light_y shall
    // be in the range of 0 to 50 000, inclusive."
    if ambient_light_x > 50_000 || ambient_light_y > 50_000 {
        return Err(SeiError::AmbientLightOutOfRange {
            x: ambient_light_x,
            y: ambient_light_y,
        });
    }
    Ok(AmbientViewingEnvironment {
        ambient_illuminance,
        ambient_light_x: ambient_light_x as u16,
        ambient_light_y: ambient_light_y as u16,
    })
}

/// §D.2.35.1 — equirectangular_projection (payload type 150).
///
/// Signals that the decoded picture is the equirectangular projection
/// of an omnidirectional video, plus optional guard-band geometry on
/// the left / right edges.
///
/// Syntax (§D.1.35.1):
/// ```text
/// equirectangular_projection( payloadSize ) {
///   erp_cancel_flag                              u(1)
///   if( !erp_cancel_flag ) {
///     erp_persistence_flag                       u(1)
///     erp_padding_flag                           u(1)
///     erp_reserved_zero_2bits                    u(2)
///     if( erp_padding_flag == 1 ) {
///       gb_erp_type                              u(3)
///       left_gb_erp_width                        u(8)
///       right_gb_erp_width                       u(8)
///     }
///   }
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EquirectangularProjection {
    pub cancel_flag: bool,
    /// Present only when `cancel_flag` is false.
    pub persistence_flag: Option<bool>,
    /// Present only when `cancel_flag` is false.
    pub padding_flag: Option<bool>,
    /// Present only when `padding_flag` is true. §D.2.35.1 lists
    /// values 0..=3 with meaningful interpretations; values > 3 are
    /// reserved and should be ignored.
    pub gb_erp_type: Option<u8>,
    /// Present only when `padding_flag` is true. Width of the left
    /// guard band, in luma samples.
    pub left_gb_erp_width: Option<u8>,
    /// Present only when `padding_flag` is true. Width of the right
    /// guard band, in luma samples.
    pub right_gb_erp_width: Option<u8>,
}

pub fn parse_equirectangular_projection(
    payload: &[u8],
) -> Result<EquirectangularProjection, SeiError> {
    let mut r = BitReader::new(payload);
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(EquirectangularProjection {
            cancel_flag,
            persistence_flag: None,
            padding_flag: None,
            gb_erp_type: None,
            left_gb_erp_width: None,
            right_gb_erp_width: None,
        });
    }
    let persistence_flag = r.u(1)? == 1;
    let padding_flag = r.u(1)? == 1;
    // §D.2.35.1: erp_reserved_zero_2bits — decoders ignore the value
    // but the two bits are still consumed.
    let _reserved = r.u(2)?;
    let (gb_erp_type, left_gb_erp_width, right_gb_erp_width) = if padding_flag {
        let ty = r.u(3)? as u8;
        let left = r.u(8)? as u8;
        let right = r.u(8)? as u8;
        (Some(ty), Some(left), Some(right))
    } else {
        (None, None, None)
    };
    Ok(EquirectangularProjection {
        cancel_flag,
        persistence_flag: Some(persistence_flag),
        padding_flag: Some(padding_flag),
        gb_erp_type,
        left_gb_erp_width,
        right_gb_erp_width,
    })
}

/// §D.2.35.2 — cubemap_projection (payload type 151).
///
/// Marks the decoded picture as the cubemap projection of an
/// omnidirectional video. The spec body carries only a persistence
/// flag — the cube faces' layout is signalled via the frame-packing
/// arrangement SEI message.
///
/// Syntax (§D.1.35.2):
/// ```text
/// cubemap_projection( payloadSize ) {
///   cmp_cancel_flag             u(1)
///   if( !cmp_cancel_flag )
///     cmp_persistence_flag      u(1)
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CubemapProjection {
    pub cancel_flag: bool,
    /// Present only when `cancel_flag` is false.
    pub persistence_flag: Option<bool>,
}

pub fn parse_cubemap_projection(payload: &[u8]) -> Result<CubemapProjection, SeiError> {
    let mut r = BitReader::new(payload);
    let cancel_flag = r.u(1)? == 1;
    let persistence_flag = if cancel_flag {
        None
    } else {
        Some(r.u(1)? == 1)
    };
    Ok(CubemapProjection {
        cancel_flag,
        persistence_flag,
    })
}

/// §D.2.35.3 — sphere_rotation (payload type 154).
///
/// Yaw / pitch / roll rotation between the global and local axes, in
/// units of 2^-16 degrees. Applies after the omnidirectional
/// projection (equirectangular or cubemap) is reconstructed.
///
/// Syntax (§D.1.35.3):
/// ```text
/// sphere_rotation( payloadSize ) {
///   sphere_rotation_cancel_flag           u(1)
///   if( !sphere_rotation_cancel_flag ) {
///     sphere_rotation_persistence_flag    u(1)
///     sphere_rotation_reserved_zero_6bits u(6)
///     yaw_rotation                        i(32)
///     pitch_rotation                      i(32)
///     roll_rotation                       i(32)
///   }
/// }
/// ```
///
/// Per §D.2.35.3 the angles are clamped to:
/// * yaw, roll  ∈ [−180·2^16, 180·2^16 − 1]   (i.e. −11_796_480..=11_796_479)
/// * pitch     ∈ [−90·2^16, 90·2^16]          (i.e. −5_898_240..=5_898_240)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SphereRotation {
    pub cancel_flag: bool,
    /// Present only when `cancel_flag` is false.
    pub persistence_flag: Option<bool>,
    /// Present only when `cancel_flag` is false. Yaw (α) in 2^-16 deg.
    pub yaw_rotation: Option<i32>,
    /// Present only when `cancel_flag` is false. Pitch (β) in 2^-16 deg.
    pub pitch_rotation: Option<i32>,
    /// Present only when `cancel_flag` is false. Roll (γ) in 2^-16 deg.
    pub roll_rotation: Option<i32>,
}

pub fn parse_sphere_rotation(payload: &[u8]) -> Result<SphereRotation, SeiError> {
    let mut r = BitReader::new(payload);
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(SphereRotation {
            cancel_flag,
            persistence_flag: None,
            yaw_rotation: None,
            pitch_rotation: None,
            roll_rotation: None,
        });
    }
    let persistence_flag = r.u(1)? == 1;
    // §D.2.35.3: reserved_zero_6bits — decoders ignore the value, but
    // the six bits are still consumed.
    let _reserved = r.u(6)?;
    let yaw = r.i(32)?;
    let pitch = r.i(32)?;
    let roll = r.i(32)?;
    // §D.2.35.3 range checks.
    const YAW_ROLL_LO: i32 = -180 * (1 << 16);
    const YAW_ROLL_HI: i32 = 180 * (1 << 16) - 1;
    const PITCH_LO: i32 = -90 * (1 << 16);
    const PITCH_HI: i32 = 90 * (1 << 16);
    if !(YAW_ROLL_LO..=YAW_ROLL_HI).contains(&yaw) {
        return Err(SeiError::SphereRotationYawOutOfRange(yaw));
    }
    if !(PITCH_LO..=PITCH_HI).contains(&pitch) {
        return Err(SeiError::SphereRotationPitchOutOfRange(pitch));
    }
    if !(YAW_ROLL_LO..=YAW_ROLL_HI).contains(&roll) {
        return Err(SeiError::SphereRotationRollOutOfRange(roll));
    }
    Ok(SphereRotation {
        cancel_flag,
        persistence_flag: Some(persistence_flag),
        yaw_rotation: Some(yaw),
        pitch_rotation: Some(pitch),
        roll_rotation: Some(roll),
    })
}

/// §D.2.35.4 — regionwise_packing (payload type 155).
///
/// Describes the region-wise packing transformation that maps regions
/// of the cropped decoded ("packed") picture onto a projected picture,
/// together with optional guard-band information. Pairs with the
/// equirectangular-projection (150) / cubemap-projection (151) SEIs:
/// per §D.2.35.4 a region-wise packing message with `rwp_cancel_flag`
/// equal to 0 may only follow one of those projection messages in
/// decoding order. We parse and surface the structure; the actual
/// sphere remapping is left to the application.
///
/// Syntax (§D.1.35.4):
/// ```text
/// regionwise_packing( payloadSize ) {
///   rwp_cancel_flag                          u(1)
///   if( !rwp_cancel_flag ) {
///     rwp_persistence_flag                   u(1)
///     constituent_picture_matching_flag      u(1)
///     rwp_reserved_zero_5bits                u(5)
///     num_packed_regions                     u(8)
///     proj_picture_width                     u(32)
///     proj_picture_height                    u(32)
///     packed_picture_width                   u(16)
///     packed_picture_height                  u(16)
///     for( i = 0; i < num_packed_regions; i++ ) {
///       rwp_reserved_zero_4bits[ i ]         u(4)
///       transform_type[ i ]                  u(3)
///       guard_band_flag[ i ]                 u(1)
///       proj_region_width[ i ]               u(32)
///       proj_region_height[ i ]              u(32)
///       proj_region_top[ i ]                 u(32)
///       proj_region_left[ i ]                u(32)
///       packed_region_width[ i ]             u(16)
///       packed_region_height[ i ]            u(16)
///       packed_region_top[ i ]               u(16)
///       packed_region_left[ i ]              u(16)
///       if( guard_band_flag[ i ] ) {
///         left_gb_width[ i ]                 u(8)
///         right_gb_width[ i ]                u(8)
///         top_gb_height[ i ]                 u(8)
///         bottom_gb_height[ i ]              u(8)
///         gb_not_used_for_pred_flag[ i ]     u(1)
///         for( j = 0; j < 4; j++ )
///           gb_type[ i ][ j ]                u(3)
///         rwp_gb_reserved_zero_3bits[ i ]    u(3)
///       }
///     }
///   }
/// }
/// ```
///
/// Per §D.2.35.4 conformance: `num_packed_regions` shall be greater than
/// 0; `proj_picture_width`/`proj_picture_height` and
/// `packed_picture_width`/`packed_picture_height` shall all be greater
/// than 0; and when `guard_band_flag[ i ]` is 1 at least one of the four
/// guard-band dimensions shall be greater than 0. The `rwp_reserved_*`
/// and `gb_type` reserved values are read and ignored.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RegionWisePacking {
    pub cancel_flag: bool,
    /// Present only when `cancel_flag` is false.
    pub persistence_flag: Option<bool>,
    /// Present only when `cancel_flag` is false. When true, the loop's
    /// per-region information applies individually to each constituent
    /// picture of a frame-packed stereoscopic picture (§D.2.35.4).
    pub constituent_picture_matching_flag: Option<bool>,
    /// Width / height of the projected picture, in relative projected
    /// picture sample units. Present only when `cancel_flag` is false.
    pub proj_picture_width: Option<u32>,
    pub proj_picture_height: Option<u32>,
    /// Width / height of the packed picture, in relative packed picture
    /// sample units. Present only when `cancel_flag` is false.
    pub packed_picture_width: Option<u16>,
    pub packed_picture_height: Option<u16>,
    /// One entry per packed region (length = `num_packed_regions`).
    /// Empty when `cancel_flag` is true.
    pub regions: Vec<PackedRegion>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PackedRegion {
    /// Rotation/mirroring mapping packed → projected (Table D-11, 0..=7).
    pub transform_type: u8,
    /// Projected region geometry, in relative projected picture units.
    pub proj_region_width: u32,
    pub proj_region_height: u32,
    pub proj_region_top: u32,
    pub proj_region_left: u32,
    /// Packed region geometry, in relative packed picture units.
    pub packed_region_width: u16,
    pub packed_region_height: u16,
    pub packed_region_top: u16,
    pub packed_region_left: u16,
    /// Guard-band description, present iff `guard_band_flag[ i ]` was 1.
    pub guard_band: Option<GuardBand>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GuardBand {
    pub left_gb_width: u8,
    pub right_gb_width: u8,
    pub top_gb_height: u8,
    pub bottom_gb_height: u8,
    /// `gb_not_used_for_pred_flag`: when true the guard-band samples are
    /// not used in inter prediction (§D.2.35.4).
    pub gb_not_used_for_pred_flag: bool,
    /// `gb_type[ i ][ 0..4 ]` for the left / right / top / bottom edges.
    /// Values greater than 3 are reserved and surfaced verbatim.
    pub gb_type: [u8; 4],
}

pub fn parse_regionwise_packing(payload: &[u8]) -> Result<RegionWisePacking, SeiError> {
    let mut r = BitReader::new(payload);
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(RegionWisePacking {
            cancel_flag,
            persistence_flag: None,
            constituent_picture_matching_flag: None,
            proj_picture_width: None,
            proj_picture_height: None,
            packed_picture_width: None,
            packed_picture_height: None,
            regions: Vec::new(),
        });
    }
    let persistence_flag = r.u(1)? == 1;
    let constituent_picture_matching_flag = r.u(1)? == 1;
    let _rwp_reserved_zero_5bits = r.u(5)?; // read and ignored per §D.2.35.4
    let num_packed_regions = r.u(8)?;
    if num_packed_regions == 0 {
        return Err(SeiError::RegionWisePackingNoRegions);
    }
    let proj_picture_width = r.u(32)?;
    let proj_picture_height = r.u(32)?;
    if proj_picture_width == 0 || proj_picture_height == 0 {
        return Err(SeiError::RegionWisePackingProjPictureZero {
            w: proj_picture_width,
            h: proj_picture_height,
        });
    }
    let packed_picture_width = r.u(16)? as u16;
    let packed_picture_height = r.u(16)? as u16;
    if packed_picture_width == 0 || packed_picture_height == 0 {
        return Err(SeiError::RegionWisePackingPackedPictureZero {
            w: packed_picture_width as u32,
            h: packed_picture_height as u32,
        });
    }
    let mut regions = Vec::with_capacity(num_packed_regions as usize);
    for i in 0..num_packed_regions as usize {
        let _rwp_reserved_zero_4bits = r.u(4)?; // read and ignored per §D.2.35.4
        let transform_type = r.u(3)? as u8;
        let guard_band_flag = r.u(1)? == 1;
        let proj_region_width = r.u(32)?;
        let proj_region_height = r.u(32)?;
        let proj_region_top = r.u(32)?;
        let proj_region_left = r.u(32)?;
        let packed_region_width = r.u(16)? as u16;
        let packed_region_height = r.u(16)? as u16;
        let packed_region_top = r.u(16)? as u16;
        let packed_region_left = r.u(16)? as u16;
        let guard_band = if guard_band_flag {
            let left_gb_width = r.u(8)? as u8;
            let right_gb_width = r.u(8)? as u8;
            let top_gb_height = r.u(8)? as u8;
            let bottom_gb_height = r.u(8)? as u8;
            if left_gb_width == 0
                && right_gb_width == 0
                && top_gb_height == 0
                && bottom_gb_height == 0
            {
                return Err(SeiError::RegionWisePackingGuardBandAllZero(i));
            }
            let gb_not_used_for_pred_flag = r.u(1)? == 1;
            let mut gb_type = [0u8; 4];
            for entry in gb_type.iter_mut() {
                *entry = r.u(3)? as u8;
            }
            let _rwp_gb_reserved_zero_3bits = r.u(3)?; // read and ignored
            Some(GuardBand {
                left_gb_width,
                right_gb_width,
                top_gb_height,
                bottom_gb_height,
                gb_not_used_for_pred_flag,
                gb_type,
            })
        } else {
            None
        };
        regions.push(PackedRegion {
            transform_type,
            proj_region_width,
            proj_region_height,
            proj_region_top,
            proj_region_left,
            packed_region_width,
            packed_region_height,
            packed_region_top,
            packed_region_left,
            guard_band,
        });
    }
    Ok(RegionWisePacking {
        cancel_flag,
        persistence_flag: Some(persistence_flag),
        constituent_picture_matching_flag: Some(constituent_picture_matching_flag),
        proj_picture_width: Some(proj_picture_width),
        proj_picture_height: Some(proj_picture_height),
        packed_picture_width: Some(packed_picture_width),
        packed_picture_height: Some(packed_picture_height),
        regions,
    })
}

/// §D.2.35.5 — omni_viewport (payload type 156).
///
/// Specifies the coordinates of one or more spherical-coordinate
/// viewport regions (great-circle bounded) recommended for display
/// when the user has no control of viewing orientation. Pairs with
/// the equirectangular-projection / cubemap-projection SEIs already
/// parsed at payload types 150 / 151.
///
/// Syntax (§D.1.35.5):
/// ```text
/// omni_viewport( payloadSize ) {
///   omni_viewport_id                       u(10)
///   omni_viewport_cancel_flag              u(1)
///   if( !omni_viewport_cancel_flag ) {
///     omni_viewport_persistence_flag       u(1)
///     omni_viewport_cnt_minus1             u(4)
///     for( i = 0; i <= omni_viewport_cnt_minus1; i++ ) {
///       omni_viewport_azimuth_centre[ i ]    i(32)
///       omni_viewport_elevation_centre[ i ]  i(32)
///       omni_viewport_tilt_centre[ i ]       i(32)
///       omni_viewport_hor_range[ i ]         u(32)
///       omni_viewport_ver_range[ i ]         u(32)
///     }
///   }
/// }
/// ```
///
/// Per §D.2.35.5 each viewport carries five values in units of 2^-16
/// degrees, with the clamps:
/// * `azimuth_centre`, `tilt_centre` ∈ [−180·2^16, 180·2^16 − 1]
/// * `elevation_centre` ∈ [−90·2^16, 90·2^16]
/// * `hor_range` ∈ [1, 360·2^16]
/// * `ver_range` ∈ [1, 180·2^16]
///
/// Per the same clause, `omni_viewport_id` values 512..=1023 are
/// reserved; we surface them verbatim and leave handling to callers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OmniViewport {
    pub viewport_id: u16,
    pub cancel_flag: bool,
    /// Present only when `cancel_flag` is false.
    pub persistence_flag: Option<bool>,
    /// One entry per recommended viewport (length =
    /// `omni_viewport_cnt_minus1 + 1`). Present only when
    /// `cancel_flag` is false.
    pub viewports: Vec<OmniViewportEntry>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OmniViewportEntry {
    pub azimuth_centre: i32,
    pub elevation_centre: i32,
    pub tilt_centre: i32,
    pub hor_range: u32,
    pub ver_range: u32,
}

pub fn parse_omni_viewport(payload: &[u8]) -> Result<OmniViewport, SeiError> {
    let mut r = BitReader::new(payload);
    // §D.1.35.5 — u(10) viewport_id.
    let viewport_id = r.u(10)? as u16;
    let cancel_flag = r.u(1)? == 1;
    if cancel_flag {
        return Ok(OmniViewport {
            viewport_id,
            cancel_flag,
            persistence_flag: None,
            viewports: Vec::new(),
        });
    }
    let persistence_flag = r.u(1)? == 1;
    let cnt_minus1 = r.u(4)?;
    // §D.2.35.5 range constants.
    const AZ_TILT_LO: i32 = -180 * (1 << 16);
    const AZ_TILT_HI: i32 = 180 * (1 << 16) - 1;
    const EL_LO: i32 = -90 * (1 << 16);
    const EL_HI: i32 = 90 * (1 << 16);
    const HOR_HI: u32 = 360 * (1 << 16);
    const VER_HI: u32 = 180 * (1 << 16);
    let count = (cnt_minus1 as usize) + 1;
    let mut viewports = Vec::with_capacity(count);
    for _ in 0..count {
        let azimuth_centre = r.i(32)?;
        let elevation_centre = r.i(32)?;
        let tilt_centre = r.i(32)?;
        let hor_range = r.u(32)?;
        let ver_range = r.u(32)?;
        if !(AZ_TILT_LO..=AZ_TILT_HI).contains(&azimuth_centre) {
            return Err(SeiError::OmniViewportAzimuthOutOfRange(azimuth_centre));
        }
        if !(EL_LO..=EL_HI).contains(&elevation_centre) {
            return Err(SeiError::OmniViewportElevationOutOfRange(elevation_centre));
        }
        if !(AZ_TILT_LO..=AZ_TILT_HI).contains(&tilt_centre) {
            return Err(SeiError::OmniViewportTiltOutOfRange(tilt_centre));
        }
        if hor_range == 0 || hor_range > HOR_HI {
            return Err(SeiError::OmniViewportHorRangeOutOfRange(hor_range));
        }
        if ver_range == 0 || ver_range > VER_HI {
            return Err(SeiError::OmniViewportVerRangeOutOfRange(ver_range));
        }
        viewports.push(OmniViewportEntry {
            azimuth_centre,
            elevation_centre,
            tilt_centre,
            hor_range,
            ver_range,
        });
    }
    Ok(OmniViewport {
        viewport_id,
        cancel_flag,
        persistence_flag: Some(persistence_flag),
        viewports,
    })
}

/// §D.2.38 — shutter_interval_info (payload type 205).
///
/// Indicates the camera shutter interval (image-sensor exposure time)
/// for the associated source pictures, either as a single CVS-wide
/// constant or as a per-sub-layer schedule keyed on `sii_sub_layer_idx`.
///
/// Syntax (§D.1.38):
/// ```text
/// shutter_interval_info( payloadSize ) {
///   sii_sub_layer_idx                                ue(v)
///   if( sii_sub_layer_idx == 0 ) {
///     shutter_interval_info_present_flag             u(1)
///     if( shutter_interval_info_present_flag ) {
///       sii_time_scale                               u(32)
///       fixed_shutter_interval_within_cvs_flag       u(1)
///       if( fixed_shutter_interval_within_cvs_flag )
///         sii_num_units_in_shutter_interval          u(32)
///       else {
///         sii_max_sub_layers_minus1                  u(3)
///         for( i = 0; i <= sii_max_sub_layers_minus1; i++ )
///           sub_layer_num_units_in_shutter_interval[ i ]  u(32)
///       }
///     }
///   }
/// }
/// ```
///
/// Per §D.2.38: `sii_time_scale` shall be greater than 0; when
/// `fixed_shutter_interval_within_cvs_flag` is 1 the picture's shutter
/// interval, in seconds, is `sii_num_units_in_shutter_interval /
/// sii_time_scale`; otherwise it is
/// `sub_layer_num_units_in_shutter_interval[sii_sub_layer_idx] /
/// sii_time_scale`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShutterIntervalInfo {
    /// Shutter-interval temporal sub-layer index of the current picture.
    pub sub_layer_idx: u32,
    /// Body fields. Present only when `sub_layer_idx == 0`.
    pub body: Option<ShutterIntervalInfoBody>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShutterIntervalInfoBody {
    /// True when the syntax elements `sii_time_scale`,
    /// `fixed_shutter_interval_within_cvs_flag`, and the trailing
    /// schedule are all present.
    pub info_present_flag: bool,
    /// Present only when `info_present_flag` is true.
    pub schedule: Option<ShutterIntervalSchedule>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShutterIntervalSchedule {
    /// Time-base for all `*_num_units_in_shutter_interval` values.
    /// Per §D.2.38 this is the count of time units in one second; e.g.
    /// 27_000_000 for the canonical 27 MHz reference clock. Strictly
    /// positive.
    pub time_scale: u32,
    /// CVS-wide vs. per-sub-layer body.
    pub kind: ShutterIntervalKind,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ShutterIntervalKind {
    /// `fixed_shutter_interval_within_cvs_flag == 1` — one constant
    /// shutter interval for every picture in the CVS. The picture's
    /// shutter interval, in seconds, is `num_units / time_scale`.
    Fixed { num_units: u32 },
    /// `fixed_shutter_interval_within_cvs_flag == 0` — per-sub-layer
    /// schedule. `max_sub_layers_minus1` plus 1 entries; element `i`
    /// gives the shutter interval for pictures with
    /// `sii_sub_layer_idx == i`.
    PerSubLayer {
        max_sub_layers_minus1: u8,
        num_units_per_sub_layer: Vec<u32>,
    },
}

pub fn parse_shutter_interval_info(payload: &[u8]) -> Result<ShutterIntervalInfo, SeiError> {
    let mut r = BitReader::new(payload);
    // §D.1.38: ue(v) `sii_sub_layer_idx`. The bitstream layer's
    // BitReader::ue caps the leading-zero run at 31 so a hostile
    // input can't blow the shifter (see r91 fuzz fix on bitstream.rs).
    let sub_layer_idx = r.ue()?;
    if sub_layer_idx != 0 {
        // Per §D.2.38 the rest of the payload is absent when
        // sub_layer_idx != 0. We preserve the value so callers can
        // map this picture against an earlier IDR-borne schedule.
        return Ok(ShutterIntervalInfo {
            sub_layer_idx,
            body: None,
        });
    }
    let info_present_flag = r.u(1)? == 1;
    if !info_present_flag {
        return Ok(ShutterIntervalInfo {
            sub_layer_idx,
            body: Some(ShutterIntervalInfoBody {
                info_present_flag,
                schedule: None,
            }),
        });
    }
    let time_scale = r.u(32)?;
    // §D.2.38: "The value of sii_time_scale shall be greater than 0."
    if time_scale == 0 {
        return Err(SeiError::ShutterIntervalTimeScaleZero);
    }
    let fixed = r.u(1)? == 1;
    let kind = if fixed {
        let num_units = r.u(32)?;
        ShutterIntervalKind::Fixed { num_units }
    } else {
        let max_sub_layers_minus1 = r.u(3)? as u8;
        let count = (max_sub_layers_minus1 as usize) + 1;
        let mut num_units_per_sub_layer = Vec::with_capacity(count);
        for _ in 0..count {
            num_units_per_sub_layer.push(r.u(32)?);
        }
        ShutterIntervalKind::PerSubLayer {
            max_sub_layers_minus1,
            num_units_per_sub_layer,
        }
    };
    Ok(ShutterIntervalInfo {
        sub_layer_idx,
        body: Some(ShutterIntervalInfoBody {
            info_present_flag,
            schedule: Some(ShutterIntervalSchedule { time_scale, kind }),
        }),
    })
}

/// §D.2.36 — SEI manifest (payload type 200).
///
/// Advertises, for the entire coded video sequence, which SEI payload
/// types are expected to be present along with a per-type
/// "necessary / unnecessary / undetermined" classification from
/// Table D-12. Transport- or systems-layer elements can use this to
/// decide whether the CVS is suitable for delivery to a receiver
/// without requiring the receiver to scan the whole stream first.
///
/// Syntax (§D.1.36):
/// ```text
/// sei_manifest( payloadSize ) {
///   manifest_num_sei_msg_types                          u(16)
///   for( i = 0; i < manifest_num_sei_msg_types; i++ ) {
///     manifest_sei_payload_type[ i ]                    u(16)
///     manifest_sei_description[ i ]                     u(8)
///   }
/// }
/// ```
///
/// Per §D.2.36: the `manifest_sei_payload_type[ i ]` values shall all
/// be distinct (parse-time check), and `manifest_sei_description[ i ]`
/// values 4..=255 are reserved. We preserve reserved descriptions
/// verbatim so callers honouring §D.2.36's "Decoders shall allow ...
/// shall ignore" rule can route them through `Description::Reserved`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SeiManifest {
    pub entries: Vec<SeiManifestEntry>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SeiManifestEntry {
    pub payload_type: u16,
    pub description: SeiManifestDescription,
}

/// Table D-12 — `manifest_sei_description[ i ]` values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SeiManifestDescription {
    /// 0 — No SEI message of this type is expected in the CVS.
    NotExpected,
    /// 1 — SEI messages of this type are expected and considered
    /// necessary by the encoder.
    ExpectedNecessary,
    /// 2 — SEI messages of this type are expected and considered
    /// unnecessary by the encoder.
    ExpectedUnnecessary,
    /// 3 — SEI messages of this type are expected and their necessity
    /// is undetermined.
    ExpectedUndetermined,
    /// 4..=255 — reserved for future use. §D.2.36 requires decoders
    /// to allow these values and ignore the associated information,
    /// including any SEI prefix indication SEI messages keyed off the
    /// same payload type.
    Reserved(u8),
}

impl SeiManifestDescription {
    pub fn from_raw(raw: u8) -> Self {
        match raw {
            0 => Self::NotExpected,
            1 => Self::ExpectedNecessary,
            2 => Self::ExpectedUnnecessary,
            3 => Self::ExpectedUndetermined,
            v => Self::Reserved(v),
        }
    }
}

pub fn parse_sei_manifest(payload: &[u8]) -> Result<SeiManifest, SeiError> {
    let mut r = BitReader::new(payload);
    // §D.1.36: manifest_num_sei_msg_types u(16).
    let count = r.u(16)? as usize;
    let mut entries: Vec<SeiManifestEntry> = Vec::with_capacity(count);
    for i in 0..count {
        let payload_type = r.u(16)? as u16;
        let description_raw = r.u(8)? as u8;
        // §D.2.36: "The values of manifest_sei_payload_type[ m ] and
        // manifest_sei_payload_type[ n ] shall not be identical when m
        // is not equal to n." We enforce uniqueness against the entries
        // already accepted.
        if let Some(prev) = entries.iter().position(|e| e.payload_type == payload_type) {
            return Err(SeiError::SeiManifestDuplicatePayloadType {
                payload_type,
                first: prev,
                second: i,
            });
        }
        entries.push(SeiManifestEntry {
            payload_type,
            description: SeiManifestDescription::from_raw(description_raw),
        });
    }
    Ok(SeiManifest { entries })
}

/// §D.2.37 — SEI prefix indication (payload type 201).
///
/// Carries one or more bit-string "prefix indications" for SEI messages
/// of a particular `prefix_sei_payload_type`. Each indication is a bit
/// string that follows the SEI payload syntax of that payload type
/// starting from its first syntax element, providing transport- or
/// systems-layer elements with a fast way to inspect the leading
/// syntax-elements of an upcoming SEI message (e.g.
/// `frame_packing_arrangement_type` for type 45) without parsing the
/// entire payload.
///
/// Syntax (§D.1.37):
/// ```text
/// sei_prefix_indication( payloadSize ) {
///   prefix_sei_payload_type                             u(16)
///   num_sei_prefix_indications_minus1                   u(8)
///   for( i = 0; i <= num_sei_prefix_indications_minus1; i++ ) {
///     num_bits_in_prefix_indication_minus1[ i ]         u(16)
///     for( j = 0; j <= num_bits_in_prefix_indication_minus1[ i ]; j++ )
///       sei_prefix_data_bit[ i ][ j ]                   u(1)
///     while( !byte_aligned() )
///       byte_alignment_bit_equal_to_one /* equal to 1 */ f(1)
///   }
/// }
/// ```
///
/// We keep the raw bits of each indication as a (`bit_count`, `bytes`)
/// pair — the spec leaves interpretation of `sei_prefix_data_bit[ i ]`
/// to the payload type identified by `prefix_sei_payload_type`, and
/// the byte-alignment fill is unconditionally `1`-bits per §D.1.37
/// (verified by `parse_sei_prefix_indication`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SeiPrefixIndication {
    pub prefix_sei_payload_type: u16,
    pub indications: Vec<SeiPrefixIndicationEntry>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SeiPrefixIndicationEntry {
    /// `num_bits_in_prefix_indication_minus1[ i ] + 1` — the number of
    /// `sei_prefix_data_bit[ i ][ j ]` bits stored in `data`. The
    /// remaining low-order bits of the last byte of `data` are unused
    /// padding zeros (this struct stores the bits MSB-first).
    pub bit_count: u32,
    /// MSB-first packed `sei_prefix_data_bit[ i ][ * ]`. `data.len() ==
    /// bit_count.div_ceil(8)`.
    pub data: Vec<u8>,
}

pub fn parse_sei_prefix_indication(payload: &[u8]) -> Result<SeiPrefixIndication, SeiError> {
    let mut r = BitReader::new(payload);
    // §D.1.37: prefix_sei_payload_type u(16) + num_sei_prefix_indications_minus1 u(8).
    let prefix_sei_payload_type = r.u(16)? as u16;
    let count = (r.u(8)? as usize) + 1;
    let mut indications: Vec<SeiPrefixIndicationEntry> = Vec::with_capacity(count);
    for i in 0..count {
        // num_bits_in_prefix_indication_minus1[ i ] u(16) — value
        // plus one is the number of sei_prefix_data_bit[ i ][ j ] u(1).
        let bit_count = r.u(16)? + 1;
        let available = r.bits_remaining();
        if (bit_count as usize) > available {
            return Err(SeiError::SeiPrefixIndicationOverflow {
                i,
                bits: bit_count,
                available,
            });
        }
        // Pack the next `bit_count` u(1) values into a byte buffer,
        // MSB-first. Each iteration reads exactly one bit.
        let byte_len = bit_count.div_ceil(8) as usize;
        let mut data = vec![0u8; byte_len];
        for j in 0..(bit_count as usize) {
            let bit = r.u(1)? as u8;
            if bit == 1 {
                let byte = j / 8;
                let pos = 7 - (j % 8);
                data[byte] |= 1 << pos;
            }
        }
        // §D.1.37 byte-alignment trailer: while not byte-aligned, read
        // a single bit which shall equal 1. The `f(1)` descriptor
        // means the value is fixed by syntax. We don't reject a
        // stray 0 here — only the structurally required SEI trailing
        // bits at the very end of the SEI payload are checked
        // elsewhere (sei_rbsp + rbsp_trailing_bits in non_vcl.rs) —
        // but we DO consume the bits so the next indication starts
        // on a byte boundary.
        while !r.byte_aligned() {
            let _ = r.u(1)?;
        }
        indications.push(SeiPrefixIndicationEntry { bit_count, data });
    }
    Ok(SeiPrefixIndication {
        prefix_sei_payload_type,
        indications,
    })
}

/// §G.13.2.10 — multiview_view_position (payload type 46).
///
/// Annex G MVC extension. Specifies the relative left-to-right view
/// position of every view component in a coded video sequence; valid
/// only when associated with an IDR access unit, and the per-CVS
/// information applies to the whole sequence. The syntax lives outside
/// the main Annex D table because Annex G specifies it.
///
/// Syntax (§G.13.1.10):
/// ```text
/// multiview_view_position( payloadSize ) {
///   num_views_minus1                                ue(v)
///   for( i = 0; i <= num_views_minus1; i++ )
///     view_position[ i ]                            ue(v)
///   multiview_view_position_extension_flag          u(1)
/// }
/// ```
///
/// §G.13.2.10 semantics constraints:
///
/// * `num_views_minus1` shall match the active MVC SPS and shall be in
///   the range 0..=1023 inclusive (so the loop reads at most 1024
///   `view_position[i]` entries).
/// * Each `view_position[i]` shall be in the range 0..=1023 inclusive.
/// * `multiview_view_position_extension_flag` shall be equal to 0; a
///   value of 1 is reserved for future use, and decoders shall ignore
///   any bits following it. We preserve the observed bit verbatim so
///   round-trip callers can distinguish a conforming `0` from a
///   reserved-`1` extension marker.
///
/// This decoder does not (yet) carry the §G.7.3.2 MVC SPS — Phase 4 on
/// the README's "Profiles + features in scope" table. The parser is
/// nonetheless harmless on a non-MVC bitstream: when this payload type
/// appears outside an MVC access unit it can still be parsed for
/// inspection and logging without affecting decode of the main
/// (`view_id == 0`) sub-bitstream.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MultiviewViewPosition {
    /// `view_position[i]`, one entry per view. The vector length is
    /// `num_views_minus1 + 1`, bounded at 1024 by the §G.13.2.10 range
    /// check that runs before allocation.
    pub view_positions: Vec<u16>,
    /// `multiview_view_position_extension_flag`. Per §G.13.2.10 the
    /// value shall be 0; conforming streams will always have `false`
    /// here.
    pub extension_flag: bool,
}

/// §G.13.2.4 — multiview_scene_info (payload type 39, Annex G / MVC).
///
/// Indicates the maximum disparity, in units of luma samples, between
/// spatially adjacent view components in an MVC access unit. A 3D
/// display compositor can use this hint when planning the per-view
/// rendering pipeline; the value is informative and does not affect
/// the decoding of any single view.
///
/// Per §G.13.2.4 the SEI message shall be associated with an IDR
/// access unit and the signalled value applies to the entire coded
/// video sequence. The actual maximum disparity may be smaller than
/// the signalled bound after sub-bitstream extraction (§G.8.5.3) has
/// removed views from the original stream.
///
/// Syntax — §G.13.1.4:
///
/// ```text
/// multiview_scene_info( payloadSize ) {
///   max_disparity                                     ue(v)
/// }
/// ```
///
/// Constraints — §G.13.2.4: `0 <= max_disparity <= 1023`. Values
/// outside this range surface as `MultiviewSceneInfoMaxDisparityOutOfRange`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MultiviewSceneInfo {
    /// `max_disparity` in units of luma samples, capped at 1023 by
    /// the §G.13.2.4 range check that runs before the value is
    /// surfaced.
    pub max_disparity: u16,
}

/// Parse a §G.13.1.4 `multiview_scene_info()` payload.
///
/// Enforces the §G.13.2.4 `0..=1023` range bound on `max_disparity`
/// before storage so a malformed Exp-Golomb codeword can't surface a
/// value the spec forbids.
pub fn parse_multiview_scene_info(payload: &[u8]) -> Result<MultiviewSceneInfo, SeiError> {
    let mut r = BitReader::new(payload);
    // §G.13.1.4 — max_disparity ue(v). §G.13.2.4 range check.
    let max_disparity = r.ue()?;
    if max_disparity > 1023 {
        return Err(SeiError::MultiviewSceneInfoMaxDisparityOutOfRange(
            max_disparity,
        ));
    }
    Ok(MultiviewSceneInfo {
        max_disparity: max_disparity as u16,
    })
}

/// §G.13.2.8 — operation_point_not_present (payload type 43, Annex G /
/// MVC).
///
/// Lists MVC operation-point identifiers that are NOT present in the
/// bitstream starting with the current access unit, interpreted with
/// respect to the previous view_scalability_info SEI message in
/// decoding order. The message remains effective until the next
/// operation_point_not_present of the same type or the end of the
/// coded video sequence, whichever is earlier.
///
/// Per §G.13.2.8 NOTE 1, the message is non-cumulative — each new
/// instance replaces (rather than augments) the previous list. The
/// IDs are u(16)-sized identifiers drawn from the
/// `operation_point_id[i]` set declared in the preceding
/// view_scalability_info SEI message.
///
/// Syntax — §G.13.1.8:
///
/// ```text
/// operation_point_not_present( payloadSize ) {
///   num_operation_points                             ue(v)
///   for( k = 0; k < num_operation_points; k++ )
///     operation_point_not_present_id[ k ]            ue(v)
/// }
/// ```
///
/// Constraints — §G.13.2.8:
/// * `0 <= num_operation_points <= num_operation_points_minus1` of
///   the preceding view_scalability_info; the absolute upper bound
///   is 65536 (since each ID is u(16)-bounded and IDs are unique).
///   We enforce that 65536 ceiling before allocating the
///   `Vec<u16>` so an adversarial `ue(v)` can't drive an unbounded
///   `Vec::with_capacity` from a maliciously-crafted SEI.
/// * Each `operation_point_not_present_id[k]` shall be in
///   `0..=65535` (i.e., representable as u16).
///
/// The 65536 upper bound on `num_operation_points` follows from the
/// spec's `num_operation_points_minus1` field elsewhere in Annex G
/// being a u(15)+1 maximum and IDs being u(16); we deliberately
/// pre-check before storage so a fuzzer cannot OOM the decoder via
/// the count field alone (the §D.2.20 oom-1bf45ba3 fix in round 177
/// was a structurally identical issue).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OperationPointNotPresent {
    /// `operation_point_not_present_id[k]` list. Each entry is the
    /// 16-bit operation-point identifier that is being declared as
    /// not present in the bitstream. Vector length =
    /// `num_operation_points`, capped at 65536 by the §G.13.2.8
    /// range check that runs before allocation.
    pub operation_point_not_present_ids: Vec<u16>,
}

/// Parse a §G.13.1.8 `operation_point_not_present()` payload.
///
/// Enforces both the §G.13.2.8 `0..=65536` cap on
/// `num_operation_points` (pre-allocation, anti-OOM) and the
/// §G.13.2.8 `0..=65535` range check on each
/// `operation_point_not_present_id[k]` before storage.
pub fn parse_operation_point_not_present(
    payload: &[u8],
) -> Result<OperationPointNotPresent, SeiError> {
    let mut r = BitReader::new(payload);
    // §G.13.1.8 — num_operation_points ue(v). Pre-allocation cap per
    // §G.13.2.8: ids are u(16) and unique, so the maximum count is
    // 65536. Larger values are bitstream-conformance errors AND a
    // realistic OOM vector (a malformed `ue(v)` can encode up to
    // 2^31 - 1 here pre-check); cf. round-177 fix for §D.2.20.
    let num_operation_points = r.ue()?;
    if num_operation_points > 65536 {
        return Err(SeiError::OperationPointNotPresentCountOutOfRange(
            num_operation_points,
        ));
    }
    let count = num_operation_points as usize;
    let mut operation_point_not_present_ids: Vec<u16> = Vec::with_capacity(count);
    for i in 0..count {
        // §G.13.1.8 — operation_point_not_present_id[k] ue(v).
        // §G.13.2.8: shall be in 0..=65535 (u16 representable).
        let id = r.ue()?;
        if id > 65535 {
            return Err(SeiError::OperationPointNotPresentIdOutOfRange { i, got: id });
        }
        operation_point_not_present_ids.push(id as u16);
    }
    Ok(OperationPointNotPresent {
        operation_point_not_present_ids,
    })
}

/// One view's inter-view-reference update inside a §G.13.1.7
/// `view_dependency_change()` SEI message (payload type 42, Annex G /
/// MVC). One `ViewDependencyChangeView` is recorded per view index
/// `i ∈ 1..=num_views_minus1` of the active MVC subset SPS, present
/// only inside the block(s) gated by `anchor_update_flag` /
/// `non_anchor_update_flag`.
///
/// Each flag, per §G.13.2.7, signals whether the corresponding
/// `anchor_ref_lX[i][j]` / `non_anchor_ref_lX[i][j]` inter-view
/// reference (declared in the MVC subset SPS) shall NOT be used for
/// inter-view reference by the coded view component with `VOIdx == i`
/// from the access unit this SEI message is associated with onward.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ViewDependencyChangeView {
    /// `anchor_ref_l0_flag[i][j]` — one bit per `num_anchor_refs_l0[i]`
    /// inter-view reference. Empty when `anchor_update_flag == 0`.
    pub anchor_ref_l0_flags: Vec<bool>,
    /// `anchor_ref_l1_flag[i][j]` — one bit per `num_anchor_refs_l1[i]`.
    pub anchor_ref_l1_flags: Vec<bool>,
    /// `non_anchor_ref_l0_flag[i][j]` — one bit per
    /// `num_non_anchor_refs_l0[i]`. Empty when `non_anchor_update_flag == 0`.
    pub non_anchor_ref_l0_flags: Vec<bool>,
    /// `non_anchor_ref_l1_flag[i][j]` — one bit per
    /// `num_non_anchor_refs_l1[i]`.
    pub non_anchor_ref_l1_flags: Vec<bool>,
}

/// §G.13.2.7 — `view_dependency_change()` (payload type 42, Annex G /
/// MVC) parsed payload.
///
/// The message updates the inter-view dependency declared in the MVC
/// subset SPS referenced by `seq_parameter_set_id`: the per-view
/// `*_ref_lX_flag` arrays mark which of the SPS-declared inter-view
/// references shall not be used from this access unit onward.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ViewDependencyChange {
    /// `seq_parameter_set_id` — identifies the MVC subset SPS whose
    /// inter-view dependency lists this message updates. Range
    /// `0..=31` per §G.13.2.7.
    pub seq_parameter_set_id: u32,
    /// `anchor_update_flag` — when set, the per-view
    /// `anchor_ref_lX_flag` arrays are present.
    pub anchor_update_flag: bool,
    /// `non_anchor_update_flag` — when set, the per-view
    /// `non_anchor_ref_lX_flag` arrays are present.
    pub non_anchor_update_flag: bool,
    /// One [`ViewDependencyChangeView`] per view index
    /// `i ∈ 1..=num_views_minus1` (vector index 0 ↔ view `i = 1`),
    /// mirroring the §G.7.3.2.1.4 dependency loop ordering. Empty when
    /// both update flags are 0.
    pub views: Vec<ViewDependencyChangeView>,
}

/// Parse a §G.13.1.7 `view_dependency_change()` payload (payload type
/// 42, Annex G / MVC).
///
/// Syntax (§G.13.1.7):
/// ```text
/// view_dependency_change( payloadSize ) {
///   seq_parameter_set_id                              ue(v)
///   anchor_update_flag                                u(1)
///   non_anchor_update_flag                            u(1)
///   if( anchor_update_flag )
///     for( i = 1; i <= num_views_minus1; i++ ) {
///       for( j = 0; j < num_anchor_refs_l0[ i ]; j++ )
///         anchor_ref_l0_flag[ i ][ j ]                u(1)
///       for( j = 0; j < num_anchor_refs_l1[ i ]; j++ )
///         anchor_ref_l1_flag[ i ][ j ]                u(1)
///     }
///   if( non_anchor_update_flag )
///     for( i = 1; i <= num_views_minus1; i++ ) {
///       for( j = 0; j < num_non_anchor_refs_l0[ i ]; j++ )
///         non_anchor_ref_l0_flag[ i ][ j ]            u(1)
///       for( j = 0; j < num_non_anchor_refs_l1[ i ]; j++ )
///         non_anchor_ref_l1_flag[ i ][ j ]            u(1)
///     }
/// }
/// ```
///
/// The inner loop bounds (`num_views_minus1`,
/// `num_anchor_refs_lX[i]`, `num_non_anchor_refs_lX[i]`) are NOT in
/// the payload — §G.13.2.7 takes them from the MVC subset SPS
/// referenced by `seq_parameter_set_id`. Those counts are supplied by
/// the caller through [`SeiContext::mvc_view_ref_counts`]; when an
/// update flag is set but the context vector is empty (no MVC subset
/// SPS wired in) the message is rejected with
/// [`SeiError::ViewDependencyChangeRefCountsUnknown`], mirroring the
/// §H.13.1.5 depth_timing / §D.1.10 spare_pic context-gating
/// precedent. `seq_parameter_set_id` is range-checked to `0..=31`
/// per §G.13.2.7.
pub fn parse_view_dependency_change(
    payload: &[u8],
    ctx: &SeiContext,
) -> Result<ViewDependencyChange, SeiError> {
    let mut r = BitReader::new(payload);

    // §G.13.1.7 — seq_parameter_set_id ue(v); §G.13.2.7 bounds 0..=31.
    let seq_parameter_set_id = r.ue()?;
    if seq_parameter_set_id > 31 {
        return Err(SeiError::ViewDependencyChangeSpsIdOutOfRange(
            seq_parameter_set_id,
        ));
    }

    // §G.13.1.7 — anchor_update_flag / non_anchor_update_flag u(1).
    let anchor_update_flag = r.u(1)? == 1;
    let non_anchor_update_flag = r.u(1)? == 1;

    // The per-view loops run only if at least one update flag is set;
    // their bounds come from the active MVC subset SPS. If neither
    // flag is set there is nothing to read and the context counts are
    // not required.
    let views = if anchor_update_flag || non_anchor_update_flag {
        if ctx.mvc_view_ref_counts.is_empty() {
            return Err(SeiError::ViewDependencyChangeRefCountsUnknown);
        }
        let mut views = Vec::with_capacity(ctx.mvc_view_ref_counts.len());
        // §G.13.1.7 — for( i = 1; i <= num_views_minus1; i++ ). The
        // context vector is already indexed view-1-first (length =
        // num_views_minus1), so a plain walk reproduces the loop.
        for counts in &ctx.mvc_view_ref_counts {
            let mut view = ViewDependencyChangeView::default();
            if anchor_update_flag {
                for _ in 0..counts.num_anchor_refs_l0 {
                    view.anchor_ref_l0_flags.push(r.u(1)? == 1);
                }
                for _ in 0..counts.num_anchor_refs_l1 {
                    view.anchor_ref_l1_flags.push(r.u(1)? == 1);
                }
            }
            views.push(view);
        }
        // §G.13.1.7 — the non_anchor block is a SEPARATE per-view loop
        // that follows the entire anchor block, not interleaved with
        // it. Walk the views again to append the non-anchor flags.
        if non_anchor_update_flag {
            for (idx, counts) in ctx.mvc_view_ref_counts.iter().enumerate() {
                let view = &mut views[idx];
                for _ in 0..counts.num_non_anchor_refs_l0 {
                    view.non_anchor_ref_l0_flags.push(r.u(1)? == 1);
                }
                for _ in 0..counts.num_non_anchor_refs_l1 {
                    view.non_anchor_ref_l1_flags.push(r.u(1)? == 1);
                }
            }
        }
        views
    } else {
        Vec::new()
    };

    Ok(ViewDependencyChange {
        seq_parameter_set_id,
        anchor_update_flag,
        non_anchor_update_flag,
        views,
    })
}

/// One temporal sub-bitstream's HRD entry inside a §G.13.1.9
/// `base_view_temporal_hrd()` SEI message (payload type 44, Annex G /
/// MVC). One `BaseViewTemporalHrdLayer` is recorded per temporal
/// sub-bitstream identified by `sei_mvc_temporal_id`.
///
/// Each field carries the value of the correspondingly-named §E.2.1
/// VUI/HRD syntax element that applies to the i-th temporal
/// sub-bitstream of the base view (`num_units_in_tick`, `time_scale`,
/// `fixed_frame_rate_flag`, `nal_hrd_parameters`, `vcl_hrd_parameters`,
/// `low_delay_hrd_flag`, `pic_struct_present_flag`), as restated by
/// §G.13.2.9.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BaseViewTemporalHrdLayer {
    /// `sei_mvc_temporal_id[i]` u(3) — the `temporal_id` value of the
    /// i-th temporal sub-bitstream (§G.13.2.9).
    pub temporal_id: u8,
    /// Present iff `sei_mvc_timing_info_present_flag[i]` was 1. Holds
    /// `(sei_mvc_num_units_in_tick, sei_mvc_time_scale,
    /// sei_mvc_fixed_frame_rate_flag)` for the i-th sub-bitstream.
    pub timing_info: Option<BaseViewTemporalHrdTiming>,
    /// `hrd_parameters()` block present iff
    /// `sei_mvc_nal_hrd_parameters_present_flag[i]` was 1.
    pub nal_hrd_parameters: Option<HrdParameters>,
    /// `hrd_parameters()` block present iff
    /// `sei_mvc_vcl_hrd_parameters_present_flag[i]` was 1.
    pub vcl_hrd_parameters: Option<HrdParameters>,
    /// `sei_mvc_low_delay_hrd_flag[i]` u(1). `Some` iff either of the
    /// NAL/VCL HRD parameter blocks is present (the syntax only codes
    /// this flag under that condition, §G.13.1.9).
    pub low_delay_hrd_flag: Option<bool>,
    /// `sei_mvc_pic_struct_present_flag[i]` u(1).
    pub pic_struct_present_flag: bool,
}

/// `(num_units_in_tick, time_scale, fixed_frame_rate_flag)` triple for a
/// single temporal sub-bitstream, present when
/// `sei_mvc_timing_info_present_flag[i]` is 1 (§G.13.1.9).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BaseViewTemporalHrdTiming {
    /// `sei_mvc_num_units_in_tick[i]` u(32).
    pub num_units_in_tick: u32,
    /// `sei_mvc_time_scale[i]` u(32).
    pub time_scale: u32,
    /// `sei_mvc_fixed_frame_rate_flag[i]` u(1).
    pub fixed_frame_rate_flag: bool,
}

/// §G.13.2.9 — base_view_temporal_hrd (payload type 44, Annex G / MVC).
///
/// Conveys the timing and HRD parameters that apply to each temporal
/// sub-bitstream of the base view of an MVC coded video sequence. It
/// lets an HRD verifier (or a temporal sub-bitstream extractor) recover
/// the per-temporal-layer `num_units_in_tick` / `time_scale` and the
/// NAL/VCL HRD parameter sets without re-deriving them from the base
/// view's own VUI, which describes only the full operation point.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BaseViewTemporalHrd {
    /// One entry per temporal sub-bitstream. Length =
    /// `num_of_temporal_layers_in_base_view_minus1 + 1`, which §G.13.2.9
    /// constrains to 1..=8 (the minus1 form is bounded to 0..=7).
    pub layers: Vec<BaseViewTemporalHrdLayer>,
}

/// Parse a §G.13.1.9 `base_view_temporal_hrd()` payload (type 44).
///
/// `num_of_temporal_layers_in_base_view_minus1` is range-checked to
/// 0..=7 per §G.13.2.9 before the per-layer loop runs; an out-of-range
/// value is a bitstream-conformance error (and would otherwise be a
/// large `ue(v)` driving an unbounded loop). Each per-layer
/// `hrd_parameters()` block is delegated to the shared §E.1.2
/// [`HrdParameters::parse`]; its errors surface as [`SeiError::Hrd`].
pub fn parse_base_view_temporal_hrd(payload: &[u8]) -> Result<BaseViewTemporalHrd, SeiError> {
    let mut r = BitReader::new(payload);
    // §G.13.1.9 — num_of_temporal_layers_in_base_view_minus1 ue(v).
    // §G.13.2.9 constrains it to 0..=7.
    let minus1 = r.ue()?;
    if minus1 > 7 {
        return Err(SeiError::BaseViewTemporalHrdLayerCountOutOfRange(minus1));
    }
    let count = (minus1 + 1) as usize;
    let mut layers: Vec<BaseViewTemporalHrdLayer> = Vec::with_capacity(count);
    for _ in 0..count {
        // sei_mvc_temporal_id[i] u(3)
        let temporal_id = r.u(3)? as u8;
        // sei_mvc_timing_info_present_flag[i] u(1)
        let timing_info = if r.u(1)? == 1 {
            // sei_mvc_num_units_in_tick[i] u(32)
            let num_units_in_tick = r.u(32)?;
            // sei_mvc_time_scale[i] u(32)
            let time_scale = r.u(32)?;
            // sei_mvc_fixed_frame_rate_flag[i] u(1)
            let fixed_frame_rate_flag = r.u(1)? == 1;
            Some(BaseViewTemporalHrdTiming {
                num_units_in_tick,
                time_scale,
                fixed_frame_rate_flag,
            })
        } else {
            None
        };
        // sei_mvc_nal_hrd_parameters_present_flag[i] u(1)
        let nal_present = r.u(1)? == 1;
        let nal_hrd_parameters = if nal_present {
            Some(HrdParameters::parse(&mut r)?)
        } else {
            None
        };
        // sei_mvc_vcl_hrd_parameters_present_flag[i] u(1)
        let vcl_present = r.u(1)? == 1;
        let vcl_hrd_parameters = if vcl_present {
            Some(HrdParameters::parse(&mut r)?)
        } else {
            None
        };
        // sei_mvc_low_delay_hrd_flag[i] u(1) — coded only when either
        // HRD parameter block is present (§G.13.1.9).
        let low_delay_hrd_flag = if nal_present || vcl_present {
            Some(r.u(1)? == 1)
        } else {
            None
        };
        // sei_mvc_pic_struct_present_flag[i] u(1)
        let pic_struct_present_flag = r.u(1)? == 1;
        layers.push(BaseViewTemporalHrdLayer {
            temporal_id,
            timing_info,
            nal_hrd_parameters,
            vcl_hrd_parameters,
            low_delay_hrd_flag,
            pic_struct_present_flag,
        });
    }
    Ok(BaseViewTemporalHrd { layers })
}

/// §G.13.2.6 — non_required_view_component (payload type 41, Annex G /
/// MVC).
///
/// Indicates, per target view component in the access unit, the set of
/// view components that are NOT needed to decode the target view
/// component (or any subsequent view component with the same view_id in
/// decoding order within the coded video sequence). The list lets a
/// sub-bitstream extractor / display compositor drop view components
/// that won't be consumed downstream without disturbing decodability.
///
/// Per §G.13.2.6 the listed view components may even be absent from the
/// associated access unit; the message records the dependency graph
/// hint, not a presence assertion.
///
/// Syntax — §G.13.1.6:
///
/// ```text
/// non_required_view_component( payloadSize ) {
///   num_info_entries_minus1                          ue(v)
///   for( i = 0; i <= num_info_entries_minus1; i++ ) {
///     view_order_index[ i ]                          ue(v)
///     num_non_required_view_components_minus1[ i ]   ue(v)
///     for( j = 0; j <= num_non_required_view_components_minus1[ i ]; j++ )
///       index_delta_minus1[ i ][ j ]                 ue(v)
///   }
/// }
/// ```
///
/// Constraints — §G.13.2.6:
/// * `num_info_entries_minus1` shall be in `0..=num_views_minus1 − 1`.
///   With `num_views_minus1` itself bounded at 1023 per Annex G (see
///   §G.13.2.10 et al.), the absolute upper bound on
///   `num_info_entries_minus1` is 1022 → `num_info_entries` ≤ 1023.
///   We enforce the absolute 1022 ceiling before allocating the entry
///   vector so an adversarial `ue(v)` count can't drive an unbounded
///   `Vec::with_capacity` (cf. round-200 fix on
///   `operation_point_not_present`).
/// * `view_order_index[i]` shall be in `1..=num_views_minus1`. We use
///   the absolute upper bound 1023 and additionally require ≥ 1; any
///   zero `view_order_index` reads as a §G.13.2.6 conformance error
///   (the "i-th target view component" is by definition view_id 1 or
///   greater).
/// * `num_non_required_view_components_minus1[i]` shall be in
///   `0..=view_order_index[i] − 1` (so an entry's listed non-required
///   view components are always strictly earlier in view-order than
///   the target view). We use `view_order_index − 1` as the bound;
///   with `view_order_index ≤ 1023`, the per-entry list length is
///   bounded at 1023 before the inner loop allocates.
/// * `index_delta_minus1[i][j]` shall be in `0..=view_order_index[i]
///   − 1` (the delta is the difference between the target's
///   view_order_index and the non-required component's
///   view_order_index, both bounded above by 1023).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NonRequiredViewComponent {
    /// One entry per target view component. There are
    /// `num_info_entries_minus1 + 1` entries total, capped at 1023 by
    /// the §G.13.2.6 absolute bound; the ordering matches the
    /// bitstream order.
    pub entries: Vec<NonRequiredViewComponentEntry>,
}

/// One target-view entry inside a `NonRequiredViewComponent` payload.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NonRequiredViewComponentEntry {
    /// `view_order_index[i]` — view order index of the target view
    /// component. Bounded at 1..=1023 by the §G.13.2.6 range check.
    pub view_order_index: u16,
    /// `index_delta_minus1[i][j]` list. Each entry encodes a
    /// non-required view component as the delta in view-order index
    /// from the target: `non_required_voi = view_order_index −
    /// index_delta_minus1[j] − 1`. List length is bounded at 1023 by
    /// the §G.13.2.6 `view_order_index − 1` cap (with
    /// `view_order_index ≤ 1023`).
    pub index_delta_minus1: Vec<u16>,
}

/// Parse a §G.13.1.6 `non_required_view_component()` payload.
///
/// Enforces all four §G.13.2.6 range bounds before storage:
/// * `num_info_entries_minus1 ≤ 1022` (pre-allocation, anti-OOM)
/// * `view_order_index[i] ∈ 1..=1023` (lower bound from §G.13.2.6,
///   absolute upper bound from Annex G's `num_views_minus1 ≤ 1023`)
/// * `num_non_required_view_components_minus1[i] ≤ view_order_index −
///   1` (pre-allocation; this implicitly caps the inner list at 1022
///   when `view_order_index = 1023`)
/// * `index_delta_minus1[i][j] ≤ view_order_index − 1`
pub fn parse_non_required_view_component(
    payload: &[u8],
) -> Result<NonRequiredViewComponent, SeiError> {
    let mut r = BitReader::new(payload);
    // §G.13.1.6 — num_info_entries_minus1 ue(v). §G.13.2.6 caps the
    // value at `num_views_minus1 − 1`. With Annex G's absolute
    // `num_views_minus1 ≤ 1023`, the absolute upper bound is 1022 →
    // `num_info_entries` ≤ 1023. Pre-allocation cap (cf. round-200
    // anti-OOM pattern on `num_operation_points`).
    let num_info_entries_minus1 = r.ue()?;
    if num_info_entries_minus1 > 1022 {
        return Err(SeiError::NonRequiredViewComponentNumInfoEntriesOutOfRange(
            num_info_entries_minus1,
        ));
    }
    let entry_count = (num_info_entries_minus1 as usize) + 1;
    let mut entries: Vec<NonRequiredViewComponentEntry> = Vec::with_capacity(entry_count);
    for i in 0..entry_count {
        // §G.13.1.6 — view_order_index[i] ue(v). §G.13.2.6 range
        // 1..=num_views_minus1; absolute upper bound 1023. The lower
        // bound 1 follows from "the i-th target view component has
        // view_id equal to view_id[view_order_index[i]]" with view 0
        // being the base view (not a target).
        let view_order_index = r.ue()?;
        if !(1..=1023).contains(&view_order_index) {
            return Err(SeiError::NonRequiredViewComponentViewOrderIndexOutOfRange {
                i,
                got: view_order_index,
            });
        }
        // §G.13.1.6 — num_non_required_view_components_minus1[i]
        // ue(v). §G.13.2.6 caps it at `view_order_index − 1` so each
        // non-required view component is strictly earlier in
        // view-order than the target. Pre-allocation cap before the
        // inner loop allocates the per-entry delta vector.
        let num_non_required_view_components_minus1 = r.ue()?;
        let voi_minus_1 = view_order_index - 1; // safe — view_order_index >= 1 verified
        if num_non_required_view_components_minus1 > voi_minus_1 {
            return Err(SeiError::NonRequiredViewComponentCountOutOfRange {
                i,
                got: num_non_required_view_components_minus1,
                view_order_index,
            });
        }
        let inner_count = (num_non_required_view_components_minus1 as usize) + 1;
        let mut index_delta_minus1: Vec<u16> = Vec::with_capacity(inner_count);
        for j in 0..inner_count {
            // §G.13.1.6 — index_delta_minus1[i][j] ue(v). §G.13.2.6
            // range 0..=view_order_index[i] − 1. Both bounds map to
            // u16 once range-checked.
            let idm1 = r.ue()?;
            if idm1 > voi_minus_1 {
                return Err(SeiError::NonRequiredViewComponentIndexDeltaOutOfRange {
                    i,
                    j,
                    got: idm1,
                    view_order_index,
                });
            }
            index_delta_minus1.push(idm1 as u16);
        }
        entries.push(NonRequiredViewComponentEntry {
            view_order_index: view_order_index as u16,
            index_delta_minus1,
        });
    }
    Ok(NonRequiredViewComponent { entries })
}

pub fn parse_multiview_view_position(payload: &[u8]) -> Result<MultiviewViewPosition, SeiError> {
    let mut r = BitReader::new(payload);
    // §G.13.1.10 — num_views_minus1 ue(v); §G.13.2.10 range check
    // BEFORE allocating the per-view vector so an adversarial ue(v)
    // can't drive an unbounded Vec::push. BitReader::ue caps the
    // leading-zero run at 31 (per the r91 fuzz fix on bitstream.rs),
    // so the raw value is already bounded to fit in a u32; the
    // 0..=1023 range check below pins it to a 1024-element ceiling.
    let num_views_minus1 = r.ue()?;
    if num_views_minus1 > 1023 {
        return Err(SeiError::MultiviewViewPositionNumViewsOutOfRange(
            num_views_minus1,
        ));
    }
    let count = (num_views_minus1 as usize) + 1;
    let mut view_positions: Vec<u16> = Vec::with_capacity(count);
    for i in 0..count {
        // §G.13.1.10 — view_position[ i ] ue(v); §G.13.2.10 range
        // 0..=1023 inclusive. ue() returns u32 so the cast to u16
        // is safe only after the range check.
        let vp = r.ue()?;
        if vp > 1023 {
            return Err(SeiError::MultiviewViewPositionViewPositionOutOfRange { i, got: vp });
        }
        view_positions.push(vp as u16);
    }
    // §G.13.1.10 — multiview_view_position_extension_flag u(1).
    // §G.13.2.10: shall equal 0. We preserve the observed bit so
    // callers can audit a non-conforming stream rather than silently
    // collapsing it; we do NOT consume any trailing bits beyond
    // this flag (per §G.13.2.10 a `1` value means decoders shall
    // ignore everything after).
    let extension_flag = r.u(1)? == 1;
    Ok(MultiviewViewPosition {
        view_positions,
        extension_flag,
    })
}

/// §G.13.2.5 — multiview_acquisition_info (payload type 40, Annex G /
/// MVC).
///
/// Specifies the intrinsic and extrinsic camera parameters of every
/// view in an MVC coded video sequence. A 3D display compositor or a
/// post-render geometry pipeline can use these parameters to project
/// the per-view content according to the camera model defined in
/// §G.13.2.5 equation G-85
/// (`s * cP[i] = A[i] * R^{-1}[i] * (wP − T[i])`), where `A[i]` is the
/// intrinsic matrix (focal length / principal point / skew),
/// `R[i]` the rotation matrix, and `T[i]` the translation vector.
///
/// Per §G.13.2.5, when present as a non-nested SEI message the
/// payload shall be associated with an IDR access unit and the
/// information applies to the entire coded video sequence; when
/// nested inside an MVCD scalable nesting SEI message the loop
/// index `i` refers to `sei_view_id[i]` of the nesting message and
/// the application scope shrinks to the nesting message lifetime.
///
/// Each scalar component is signalled in a sign-exponent-mantissa
/// floating-point form (see [`FloatComponent`]) — `s` is a single
/// sign bit (`u(1)`); `e` is a 6-bit exponent (`u(6)`); the
/// mantissa is variable-width and computed from the per-block
/// precision exponent `prec_*`:
///
/// * If `e == 0`, the mantissa width is `max(0, prec − 30)`.
/// * If `0 < e < 63`, the mantissa width is `max(0, e + prec − 31)`.
/// * If `e == 63`, the spec reserves the value (decoders treat
///   it as "unspecified") and does not specify a mantissa width;
///   we treat the mantissa as zero-width in that case so the
///   bitstream cursor advances consistently regardless of which
///   reserved-future encoder produced the stream.
///
/// The decoded scalar value `x` reconstructs from a stored
/// `FloatComponent { sign, exponent: e, mantissa: n, mantissa_width: v }`
/// per §G.13.2.5 (Table G-3 below the syntax table) as:
///
/// * `e == 0`         → `x = (-1)^sign * 2^(−30) * (n / 2^v)`     (denormal)
/// * `0 < e < 63`     → `x = (-1)^sign * 2^(e − 31) * (1 + n / 2^v)` (normal)
/// * `e == 63`        → "unspecified" — callers should treat the
///   component as informational only.
///
/// Syntax — §G.13.1.5:
///
/// ```text
/// multiview_acquisition_info( payloadSize ) {
///   num_views_minus1                                 ue(v)
///   intrinsic_param_flag                             u(1)
///   extrinsic_param_flag                             u(1)
///   if( intrinsic_param_flag ) {
///     intrinsic_params_equal_flag                    u(1)
///     prec_focal_length                              ue(v)
///     prec_principal_point                           ue(v)
///     prec_skew_factor                               ue(v)
///     for( i = 0; i <= ( intrinsic_params_equal_flag ? 0 : num_views_minus1 ); i++ ) {
///       focal_length_x[ i ]                          /* sign u(1) + exponent u(6) + mantissa u(v) */
///       focal_length_y[ i ]
///       principal_point_x[ i ]
///       principal_point_y[ i ]
///       skew_factor[ i ]
///     }
///   }
///   if( extrinsic_param_flag ) {
///     prec_rotation_param                            ue(v)
///     prec_translation_param                         ue(v)
///     for( i = 0; i <= num_views_minus1; i++ ) {
///       for( j = 1; j <= 3; j++ ) {        /* row */
///         for( k = 1; k <= 3; k++ ) {      /* column */
///           r[ i ][ j ][ k ]               /* sign + exponent + mantissa */
///         }
///         t[ i ][ j ]                       /* sign + exponent + mantissa */
///       }
///     }
///   }
/// }
/// ```
///
/// Constraints — §G.13.2.5:
///
/// * `0 ≤ num_views_minus1 ≤ 1023` (Annex G absolute). Enforced
///   pre-allocation so an adversarial `ue(v)` cannot drive an
///   unbounded `Vec` (same anti-OOM rationale as
///   round-177 §D.2.20 and round-200 §G.13.2.8).
/// * Each of `prec_focal_length`, `prec_principal_point`,
///   `prec_skew_factor`, `prec_rotation_param`,
///   `prec_translation_param` shall be in `0..=31` inclusive.
/// * `exponent_*` is a `u(6)` so its raw range is `0..=63`. The
///   spec's range `0..=62` is informative — value 63 is reserved
///   for future use and decoders shall treat it as "unspecified";
///   we preserve the observed value without rejecting it so a
///   future-compatible bitstream remains parsable.
///
/// Note: this decoder does not (yet) carry the §G.7.3.2 MVC SPS —
/// Phase 4 on the README's "Profiles + features in scope" table.
/// The parser is nonetheless harmless on a non-MVC bitstream: when
/// this payload type appears outside an MVC access unit it can
/// still be parsed for inspection and logging without affecting
/// decode of the main (`view_id == 0`) sub-bitstream.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MultiviewAcquisitionInfo {
    /// `num_views_minus1` — number of views in the CVS (minus one).
    /// Capped at 1023 (Annex G absolute) by the §G.13.2.5 range
    /// check that runs before allocation.
    pub num_views_minus1: u16,
    /// `intrinsic_param_flag == 1` populates `intrinsic`.
    pub intrinsic: Option<IntrinsicCameraParams>,
    /// `extrinsic_param_flag == 1` populates `extrinsic`.
    pub extrinsic: Option<ExtrinsicCameraParams>,
}

/// §G.13.2.5 — intrinsic camera parameters block (focal lengths,
/// principal point, skew factor). When
/// `intrinsic_params_equal_flag == 1` the block carries a single
/// per-CVS camera entry; otherwise it carries one entry per view
/// (`num_views_minus1 + 1` total).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IntrinsicCameraParams {
    /// `intrinsic_params_equal_flag` — `true` if a single intrinsic
    /// set applies to every camera; `false` if a distinct set is
    /// signalled per camera.
    pub intrinsic_params_equal_flag: bool,
    /// `prec_focal_length` (0..=31) — sets the truncation precision
    /// for `focal_length_x[i]` and `focal_length_y[i]` via the
    /// mantissa-width formula in §G.13.2.5.
    pub prec_focal_length: u8,
    /// `prec_principal_point` (0..=31) — sets the truncation
    /// precision for `principal_point_x[i]` and `principal_point_y[i]`.
    pub prec_principal_point: u8,
    /// `prec_skew_factor` (0..=31) — sets the truncation precision
    /// for `skew_factor[i]`.
    pub prec_skew_factor: u8,
    /// Per-camera intrinsic entries. Length is either 1 (when
    /// `intrinsic_params_equal_flag == true`) or
    /// `num_views_minus1 + 1`.
    pub cameras: Vec<IntrinsicCamera>,
}

/// §G.13.2.5 — per-camera intrinsic parameters: focal lengths
/// (x, y), principal point (x, y), and skew factor. Each component
/// is a [`FloatComponent`] following the §G.13.2.5 floating-point
/// encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IntrinsicCamera {
    pub focal_length_x: FloatComponent,
    pub focal_length_y: FloatComponent,
    pub principal_point_x: FloatComponent,
    pub principal_point_y: FloatComponent,
    pub skew_factor: FloatComponent,
}

/// §G.13.2.5 — extrinsic camera parameters block. Carries the 3×3
/// rotation matrix and 3-vector translation per camera.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtrinsicCameraParams {
    /// `prec_rotation_param` (0..=31) — sets the truncation
    /// precision for `r[i][j][k]`.
    pub prec_rotation_param: u8,
    /// `prec_translation_param` (0..=31) — sets the truncation
    /// precision for `t[i][j]`.
    pub prec_translation_param: u8,
    /// Per-camera extrinsic entries. Length is `num_views_minus1 + 1`.
    pub cameras: Vec<ExtrinsicCamera>,
}

/// §G.13.2.5 — per-camera extrinsic parameters: 3×3 rotation matrix
/// `r[j][k]` (rows and columns are 1-indexed in the spec; we store
/// in 0-indexed form so `r[j_spec − 1][k_spec − 1]` is the
/// (j_spec, k_spec) entry) and 3-vector translation `t[j]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExtrinsicCamera {
    /// Rotation matrix entries. Spec 1-indexes `j, k ∈ {1, 2, 3}`;
    /// we store with `r[j_spec − 1][k_spec − 1]`.
    pub r: [[FloatComponent; 3]; 3],
    /// Translation vector entries. Spec 1-indexes `j ∈ {1, 2, 3}`;
    /// we store with `t[j_spec − 1]`.
    pub t: [FloatComponent; 3],
}

/// §G.13.2.5 — sign-exponent-mantissa floating-point component used
/// for every scalar in the multiview acquisition info SEI. The
/// stored value is the raw bitstream value; the
/// [`FloatComponent::to_f64`] helper reconstructs the IEEE-style
/// scalar per Table G-3.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FloatComponent {
    /// `sign` — `false` for the positive sign-bit value (`u(1) == 0`),
    /// `true` for the negative (`u(1) == 1`).
    pub sign: bool,
    /// `exponent` — `u(6)`, raw range `0..=63`. Value 63 is reserved
    /// (decoders treat as "unspecified" per §G.13.2.5).
    pub exponent: u8,
    /// `mantissa` — variable-width `u(v)` raw bits; width is
    /// recorded in `mantissa_width`. The width is at most 62 bits so
    /// it fits in `u64` (62 = 62 + 31 − 31, the §G.13.2.5 maximum).
    pub mantissa: u64,
    /// `mantissa_width` — number of bits in `mantissa`, derived per
    /// §G.13.2.5 from the per-block precision and the exponent.
    /// Bounded by 62.
    pub mantissa_width: u8,
}

impl FloatComponent {
    /// Reconstruct the §G.13.2.5 floating-point scalar value:
    ///
    /// * `e == 0`     → `(-1)^s * 2^(−30) * (n / 2^v)`         (denormal)
    /// * `0 < e < 63` → `(-1)^s * 2^(e − 31) * (1 + n / 2^v)`  (normal)
    /// * `e == 63`    → `f64::NAN` ("unspecified" per the spec).
    ///
    /// `v == 0` is handled implicitly: `n / 2^0 = n = 0` (parser
    /// guarantees the stored mantissa is 0 when width is 0).
    #[must_use]
    pub fn to_f64(&self) -> f64 {
        if self.exponent == 63 {
            return f64::NAN;
        }
        let sign: f64 = if self.sign { -1.0 } else { 1.0 };
        let v = self.mantissa_width as i32;
        let n_over_2v = if v == 0 {
            0.0
        } else {
            (self.mantissa as f64) / (2f64.powi(v))
        };
        if self.exponent == 0 {
            sign * 2f64.powi(-30) * n_over_2v
        } else {
            sign * 2f64.powi((self.exponent as i32) - 31) * (1.0 + n_over_2v)
        }
    }
}

/// §G.13.2.5 — derive the mantissa bit-width from a precision
/// `prec` (0..=31) and an exponent `e` (0..=63).
///
/// * `e == 0`     → `max(0, prec − 30)`
/// * `0 < e < 63` → `max(0, e + prec − 31)`
/// * `e == 63`    → `0` (spec reserves the value; no mantissa)
///
/// The maximum is `62 + 31 − 31 = 62` bits, comfortably within
/// `u64`.
fn mantissa_width_g1325(prec: u8, e: u8) -> u8 {
    debug_assert!(prec <= 31);
    if e == 0 {
        prec.saturating_sub(30)
    } else if e == 63 {
        0
    } else {
        // 0 < e < 63 → e + prec − 31, clamped at 0.
        let sum = e as i32 + prec as i32 - 31;
        if sum <= 0 {
            0
        } else {
            sum as u8
        }
    }
}

/// Read a §G.13.2.5 sign-exponent-mantissa component from `r`,
/// using `prec` to derive the mantissa width. Returns the raw
/// bitstream value; see [`FloatComponent::to_f64`] for the
/// reconstructed scalar.
fn read_float_component(r: &mut BitReader<'_>, prec: u8) -> Result<FloatComponent, SeiError> {
    // sign u(1).
    let sign = r.u(1)? == 1;
    // exponent u(6).
    let exponent = r.u(6)? as u8;
    // mantissa u(v) where v is derived per §G.13.2.5.
    let v = mantissa_width_g1325(prec, exponent);
    let mantissa: u64 = if v == 0 {
        0
    } else if v <= 32 {
        r.u(v as u32)? as u64
    } else {
        // 32 < v <= 62 — split into hi (v − 32 bits) + lo (32 bits)
        // so each individual r.u call stays within the
        // u(<=32) primitive contract.
        let hi_bits = (v - 32) as u32;
        let hi = r.u(hi_bits)? as u64;
        let lo = r.u(32)? as u64;
        (hi << 32) | lo
    };
    Ok(FloatComponent {
        sign,
        exponent,
        mantissa,
        mantissa_width: v,
    })
}

/// Parse a §G.13.1.5 `multiview_acquisition_info()` payload.
///
/// Enforces:
///
/// * `num_views_minus1 ≤ 1023` (Annex G absolute, anti-OOM
///   pre-allocation cap — cf. round-200 §G.13.2.8).
/// * Each of `prec_focal_length`, `prec_principal_point`,
///   `prec_skew_factor`, `prec_rotation_param`,
///   `prec_translation_param` shall be in `0..=31` inclusive.
///
/// Floating-point components are stored verbatim in
/// [`FloatComponent`] (no IEEE conversion at parse time — callers
/// can call [`FloatComponent::to_f64`] when they need a scalar).
pub fn parse_multiview_acquisition_info(
    payload: &[u8],
) -> Result<MultiviewAcquisitionInfo, SeiError> {
    let mut r = BitReader::new(payload);

    // §G.13.1.5 — num_views_minus1 ue(v).
    // §G.13.2.5 / Annex G absolute: 0..=1023.
    let num_views_minus1 = r.ue()?;
    if num_views_minus1 > 1023 {
        return Err(SeiError::MultiviewAcquisitionInfoNumViewsOutOfRange(
            num_views_minus1,
        ));
    }
    let num_views = (num_views_minus1 as usize) + 1;

    // §G.13.1.5 — intrinsic_param_flag u(1), extrinsic_param_flag u(1).
    let intrinsic_param_flag = r.u(1)? == 1;
    let extrinsic_param_flag = r.u(1)? == 1;

    let intrinsic = if intrinsic_param_flag {
        // §G.13.1.5 — intrinsic_params_equal_flag u(1).
        let intrinsic_params_equal_flag = r.u(1)? == 1;
        // §G.13.1.5 — prec_focal_length, prec_principal_point,
        // prec_skew_factor ue(v). §G.13.2.5: each shall be in
        // 0..=31 inclusive.
        let prec_focal_length = read_prec_ue(&mut r, "prec_focal_length")?;
        let prec_principal_point = read_prec_ue(&mut r, "prec_principal_point")?;
        let prec_skew_factor = read_prec_ue(&mut r, "prec_skew_factor")?;

        // §G.13.1.5 — when intrinsic_params_equal_flag == 1, only
        // the i = 0 entry is signalled; otherwise the loop covers
        // i = 0..=num_views_minus1.
        let cam_count = if intrinsic_params_equal_flag {
            1
        } else {
            num_views
        };
        let mut cameras: Vec<IntrinsicCamera> = Vec::with_capacity(cam_count);
        for _ in 0..cam_count {
            let focal_length_x = read_float_component(&mut r, prec_focal_length)?;
            let focal_length_y = read_float_component(&mut r, prec_focal_length)?;
            let principal_point_x = read_float_component(&mut r, prec_principal_point)?;
            let principal_point_y = read_float_component(&mut r, prec_principal_point)?;
            let skew_factor = read_float_component(&mut r, prec_skew_factor)?;
            cameras.push(IntrinsicCamera {
                focal_length_x,
                focal_length_y,
                principal_point_x,
                principal_point_y,
                skew_factor,
            });
        }

        Some(IntrinsicCameraParams {
            intrinsic_params_equal_flag,
            prec_focal_length,
            prec_principal_point,
            prec_skew_factor,
            cameras,
        })
    } else {
        None
    };

    let extrinsic = if extrinsic_param_flag {
        // §G.13.1.5 — prec_rotation_param, prec_translation_param
        // ue(v). §G.13.2.5: each shall be in 0..=31 inclusive.
        let prec_rotation_param = read_prec_ue(&mut r, "prec_rotation_param")?;
        let prec_translation_param = read_prec_ue(&mut r, "prec_translation_param")?;

        let mut cameras: Vec<ExtrinsicCamera> = Vec::with_capacity(num_views);
        // The spec's outer loop runs i = 0..=num_views_minus1
        // (the §G.13.1.5 loop comment differs from the intrinsic
        // block in that there is no intrinsic_params_equal_flag
        // shortcut — extrinsics are always per-camera).
        for _ in 0..num_views {
            // Initialise with a zero placeholder we'll overwrite —
            // `FloatComponent` is Copy so this is cheap.
            let zero = FloatComponent {
                sign: false,
                exponent: 0,
                mantissa: 0,
                mantissa_width: 0,
            };
            let mut r_mat: [[FloatComponent; 3]; 3] = [[zero; 3]; 3];
            let mut t_vec: [FloatComponent; 3] = [zero; 3];
            // §G.13.1.5 — for( j = 1; j <= 3; j++ ) { for( k = 1; k <= 3; k++ ) r[i][j][k]; t[i][j]; }
            // i.e., row j carries three r entries (k=1..3) followed
            // by a single t entry. We store with 0-indexed j, k.
            for (j, row) in r_mat.iter_mut().enumerate() {
                for cell in row.iter_mut() {
                    *cell = read_float_component(&mut r, prec_rotation_param)?;
                }
                t_vec[j] = read_float_component(&mut r, prec_translation_param)?;
            }
            cameras.push(ExtrinsicCamera { r: r_mat, t: t_vec });
        }

        Some(ExtrinsicCameraParams {
            prec_rotation_param,
            prec_translation_param,
            cameras,
        })
    } else {
        None
    };

    Ok(MultiviewAcquisitionInfo {
        num_views_minus1: num_views_minus1 as u16,
        intrinsic,
        extrinsic,
    })
}

/// Helper: read a `prec_*` ue(v) and enforce the §G.13.2.5
/// `0..=31` range bound. The `field` argument is the spec name
/// surfaced in the error.
fn read_prec_ue(r: &mut BitReader<'_>, field: &'static str) -> Result<u8, SeiError> {
    let v = r.ue()?;
    if v > 31 {
        return Err(SeiError::MultiviewAcquisitionInfoPrecOutOfRange { field, got: v });
    }
    Ok(v as u8)
}

/// §H.13.2.4 — sign-less floating-point component used by the 3D
/// reference displays info SEI. Same exponent / mantissa-width
/// formula as [`FloatComponent`] (§G.13.2.5), but the spec's Table
/// H-3 fixes the sign column at 0 for every variable, so no sign
/// bit is read from the bitstream.
///
/// The stored value is the raw bitstream value; the
/// [`UnsignedFloatComponent::to_f64`] helper reconstructs the
/// IEEE-style scalar per Table H-3:
///
/// * `0 < e < 63` → `2^(e − 31) * (1 + n ÷ 2^v)`         (normal)
/// * `e == 0`     → `2^−(30 + v) * n`                    (denormal)
/// * `e == 63`    → unspecified; we return `f64::NAN`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnsignedFloatComponent {
    /// `exponent` — `u(6)`, raw range `0..=63`. Value 63 is
    /// reserved (treated as "unspecified" per §H.13.2.4).
    pub exponent: u8,
    /// `mantissa` — variable-width `u(v)` raw bits; width is
    /// recorded in `mantissa_width` and is at most 62.
    pub mantissa: u64,
    /// `mantissa_width` — bit-width of `mantissa`, derived per
    /// §H.13.2.4 (same formula as §G.13.2.5) from the per-block
    /// precision and the exponent.
    pub mantissa_width: u8,
}

impl UnsignedFloatComponent {
    /// Reconstruct the §H.13.2.4 scalar value per Table H-3 with
    /// `s = 0`. Mirrors [`FloatComponent::to_f64`] minus the sign.
    #[must_use]
    pub fn to_f64(&self) -> f64 {
        if self.exponent == 63 {
            return f64::NAN;
        }
        let v = self.mantissa_width as i32;
        let n_over_2v = if v == 0 {
            0.0
        } else {
            (self.mantissa as f64) / (2f64.powi(v))
        };
        if self.exponent == 0 {
            2f64.powi(-30) * n_over_2v
        } else {
            2f64.powi((self.exponent as i32) - 31) * (1.0 + n_over_2v)
        }
    }
}

/// Read a §H.13.2.4 unsigned floating-point component from `r`,
/// using `prec` to derive the mantissa width. The §H.13.2.4
/// mantissa-width formula is identical to the §G.13.2.5 one used
/// by [`mantissa_width_g1325`].
fn read_unsigned_float_component(
    r: &mut BitReader<'_>,
    prec: u8,
) -> Result<UnsignedFloatComponent, SeiError> {
    // exponent u(6).
    let exponent = r.u(6)? as u8;
    let v = mantissa_width_g1325(prec, exponent);
    let mantissa: u64 = if v == 0 {
        0
    } else if v <= 32 {
        r.u(v as u32)? as u64
    } else {
        // 32 < v <= 62 — split into hi (v − 32 bits) + lo (32 bits)
        // so each individual r.u call stays within the
        // u(<=32) primitive contract.
        let hi_bits = (v - 32) as u32;
        let hi = r.u(hi_bits)? as u64;
        let lo = r.u(32)? as u64;
        (hi << 32) | lo
    };
    Ok(UnsignedFloatComponent {
        exponent,
        mantissa,
        mantissa_width: v,
    })
}

/// §H.13.2.4 — 3D reference displays information SEI message.
///
/// Carries the reference display widths, reference viewing
/// distances (optional), and the corresponding baseline distances
/// and additional horizontal shifts that form a stereo pair for
/// each reference display. Used by view renderers to compute a
/// proper stereo pair for the target screen width and viewing
/// distance.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThreeDimensionalReferenceDisplaysInfo {
    /// `prec_ref_baseline` (0..=31) — truncation exponent for
    /// `ref_baseline[i]`.
    pub prec_ref_baseline: u8,
    /// `prec_ref_display_width` (0..=31) — truncation exponent for
    /// `ref_display_width[i]`.
    pub prec_ref_display_width: u8,
    /// `prec_ref_viewing_dist` (0..=31) — truncation exponent for
    /// `ref_viewing_distance[i]`. Present iff
    /// `ref_viewing_distance_flag == 1`.
    pub prec_ref_viewing_dist: Option<u8>,
    /// One entry per reference display
    /// (`num_ref_displays_minus1 + 1`, capped at 32).
    pub displays: Vec<ReferenceDisplay>,
    /// `three_dimensional_reference_displays_extension_flag` — the
    /// spec mandates 0 in this edition; non-zero values are
    /// reserved for future use and a decoder shall ignore any
    /// trailing data.
    pub extension_flag: bool,
}

/// §H.13.2.4 — per-reference-display entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReferenceDisplay {
    /// `ref_baseline[i]` — reference baseline distance, in the same
    /// units as the x component of the translation vector in the
    /// associated multiview_acquisition_info SEI.
    pub ref_baseline: UnsignedFloatComponent,
    /// `ref_display_width[i]` — reference display width in
    /// centimetres.
    pub ref_display_width: UnsignedFloatComponent,
    /// `ref_viewing_distance[i]` — reference viewing distance in
    /// centimetres. Present iff `ref_viewing_distance_flag == 1`.
    pub ref_viewing_distance: Option<UnsignedFloatComponent>,
    /// `additional_shift_present_flag[i]` — whether `num_sample_shift_plus512`
    /// is signalled for this display.
    pub additional_shift_present_flag: bool,
    /// `num_sample_shift_plus512[i]` — recommended additional
    /// horizontal shift in samples between the left and right views
    /// (biased by 512). Present iff
    /// `additional_shift_present_flag == 1`. Range `0..=1023`.
    pub num_sample_shift_plus512: Option<u16>,
}

impl ReferenceDisplay {
    /// §H.13.2.4 — recommended additional horizontal shift between
    /// the left and right views, decoded from the biased
    /// `num_sample_shift_plus512[i]` field into a signed sample
    /// count.
    ///
    /// The bitstream stores the shift as `num_sample_shift_plus512`
    /// with the bias `+ 512`, giving a raw range of `0..=1023`. The
    /// semantic shift `NumSampleShift[i]` defined by §H.13.2.4 is
    ///
    /// ```text
    /// NumSampleShift[i] = num_sample_shift_plus512[i] − 512
    /// ```
    ///
    /// which spans `−512..=511`. The sign convention from §H.13.2.4
    /// is:
    ///
    /// * `< 0` — recommend shifting the **left** view to the left by
    ///   `512 − num_sample_shift_plus512[i]` samples (i.e. by
    ///   `|NumSampleShift|`) relative to the right view.
    /// * `= 0` — recommend that no shift be applied.
    /// * `> 0` — recommend shifting the **left** view to the right
    ///   by `num_sample_shift_plus512[i] − 512` samples (i.e. by
    ///   `NumSampleShift`) relative to the right view.
    ///
    /// Returns `None` when `additional_shift_present_flag == 0` (the
    /// shift is unsignalled and §H.13.2.4 does not define an
    /// inferred value); returns `Some(NumSampleShift[i])` otherwise.
    ///
    /// The return type is `i16` since the value fits in
    /// `−512..=511`.
    #[must_use]
    pub fn num_sample_shift(&self) -> Option<i16> {
        self.num_sample_shift_plus512
            .map(|biased| (biased as i32 - 512) as i16)
    }
}

/// Parse a §H.13.1.4 `three_dimensional_reference_displays_info()`
/// payload.
///
/// Enforces:
///
/// * `prec_ref_baseline`, `prec_ref_display_width`,
///   `prec_ref_viewing_dist` shall be in `0..=31` (§H.13.2.4).
/// * `num_ref_displays_minus1` shall be in `0..=31` (§H.13.2.4) —
///   serves as the pre-allocation cap for the per-display loop.
///
/// `num_sample_shift_plus512` is read as `u(10)`, naturally
/// constrained to `0..=1023` by the bit width.
///
/// `three_dimensional_reference_displays_extension_flag` is read
/// but no further data is parsed when it is 1 — the spec reserves
/// the trailing region for future extensions and decoders shall
/// ignore everything after this flag.
pub fn parse_three_dimensional_reference_displays_info(
    payload: &[u8],
) -> Result<ThreeDimensionalReferenceDisplaysInfo, SeiError> {
    let mut r = BitReader::new(payload);

    // prec_ref_baseline ue(v), 0..=31.
    let prec_ref_baseline = {
        let v = r.ue()?;
        if v > 31 {
            return Err(
                SeiError::ThreeDimensionalReferenceDisplaysInfoPrecOutOfRange {
                    field: "prec_ref_baseline",
                    got: v,
                },
            );
        }
        v as u8
    };
    // prec_ref_display_width ue(v), 0..=31.
    let prec_ref_display_width = {
        let v = r.ue()?;
        if v > 31 {
            return Err(
                SeiError::ThreeDimensionalReferenceDisplaysInfoPrecOutOfRange {
                    field: "prec_ref_display_width",
                    got: v,
                },
            );
        }
        v as u8
    };
    // ref_viewing_distance_flag u(1).
    let ref_viewing_distance_flag = r.u(1)? == 1;
    // prec_ref_viewing_dist ue(v), 0..=31. Present iff
    // ref_viewing_distance_flag == 1.
    let prec_ref_viewing_dist = if ref_viewing_distance_flag {
        let v = r.ue()?;
        if v > 31 {
            return Err(
                SeiError::ThreeDimensionalReferenceDisplaysInfoPrecOutOfRange {
                    field: "prec_ref_viewing_dist",
                    got: v,
                },
            );
        }
        Some(v as u8)
    } else {
        None
    };
    // num_ref_displays_minus1 ue(v), 0..=31. Anti-OOM cap before
    // the per-display loop allocation.
    let num_ref_displays_minus1 = r.ue()?;
    if num_ref_displays_minus1 > 31 {
        return Err(
            SeiError::ThreeDimensionalReferenceDisplaysInfoNumRefDisplaysOutOfRange(
                num_ref_displays_minus1,
            ),
        );
    }
    let num_ref_displays = (num_ref_displays_minus1 as usize) + 1;
    let mut displays = Vec::with_capacity(num_ref_displays);
    for _ in 0..num_ref_displays {
        // exponent_ref_baseline u(6) + mantissa_ref_baseline u(v).
        let ref_baseline = read_unsigned_float_component(&mut r, prec_ref_baseline)?;
        // exponent_ref_display_width u(6) + mantissa_ref_display_width u(v).
        let ref_display_width = read_unsigned_float_component(&mut r, prec_ref_display_width)?;
        // exponent_ref_viewing_distance u(6) + mantissa u(v), iff
        // ref_viewing_distance_flag == 1.
        let ref_viewing_distance = if let Some(prec) = prec_ref_viewing_dist {
            Some(read_unsigned_float_component(&mut r, prec)?)
        } else {
            None
        };
        // additional_shift_present_flag u(1).
        let additional_shift_present_flag = r.u(1)? == 1;
        // num_sample_shift_plus512 u(10) iff additional_shift_present_flag == 1.
        let num_sample_shift_plus512 = if additional_shift_present_flag {
            Some(r.u(10)? as u16)
        } else {
            None
        };
        displays.push(ReferenceDisplay {
            ref_baseline,
            ref_display_width,
            ref_viewing_distance,
            additional_shift_present_flag,
            num_sample_shift_plus512,
        });
    }
    // three_dimensional_reference_displays_extension_flag u(1).
    let extension_flag = r.u(1)? == 1;

    Ok(ThreeDimensionalReferenceDisplaysInfo {
        prec_ref_baseline,
        prec_ref_display_width,
        prec_ref_viewing_dist,
        displays,
        extension_flag,
    })
}

/// §H.13.2.3.1 — depth representation SEI element.
///
/// Carries the raw `(s, e, n, v)` quadruple for one floating-point
/// scalar in the depth representation information message. The
/// scalar uses a different syntax shape than the
/// §G.13.2.5 / §H.13.2.4 floats handled by [`FloatComponent`] /
/// [`UnsignedFloatComponent`]:
///
/// * sign — `u(1)` (`da_sign_flag`)
/// * exponent — `u(7)` (`da_exponent`, 0..=126; 127 is reserved for
///   future use and shall be treated as unspecified)
/// * mantissa width — `u(5) + 1` (`da_mantissa_len_minus1 + 1`,
///   1..=32)
/// * mantissa — `u(v)` (`da_mantissa`)
///
/// The §H.13.2.3 reconstruction formula in Table H-2 is:
///
/// * `0 < e < 127` → `x = (−1)^s * 2^(e − 31) * (1 + n / 2^v)`
/// * `e == 0`      → `x = (−1)^s * 2^(−(30 + v)) * n`
///
/// The stored value is the raw bitstream value; [`DepthFloatComponent::to_f64`]
/// reconstructs the scalar.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DepthFloatComponent {
    /// `da_sign_flag` — `false` for positive (`u(1) == 0`), `true` for
    /// negative (`u(1) == 1`).
    pub sign: bool,
    /// `da_exponent` — `u(7)`, raw range `0..=127`. Value 127 is
    /// reserved (decoders shall treat as "unspecified" per
    /// §H.13.2.3.1).
    pub exponent: u8,
    /// `da_mantissa` — variable-width `u(v)` raw bits.
    /// `da_mantissa_len_minus1` is `u(5)` so the width is in
    /// `1..=32`; the mantissa fits in `u32`, stored here as `u64` for
    /// uniformity with the other float components.
    pub mantissa: u64,
    /// `da_mantissa_len_minus1 + 1` — bit-width of `mantissa`,
    /// in `1..=32`.
    pub mantissa_width: u8,
}

impl DepthFloatComponent {
    /// Reconstruct the §H.13.2.3 floating-point scalar value per the
    /// Table H-2 formula.
    ///
    /// * `e == 0`      → `(-1)^s * 2^(-(30 + v)) * n`        (denormal)
    /// * `0 < e < 127` → `(-1)^s * 2^(e - 31) * (1 + n / 2^v)` (normal)
    /// * `e == 127`    → `f64::NAN` ("unspecified" per the spec).
    #[must_use]
    pub fn to_f64(&self) -> f64 {
        if self.exponent == 127 {
            return f64::NAN;
        }
        let sign: f64 = if self.sign { -1.0 } else { 1.0 };
        let v = self.mantissa_width as i32;
        let n_over_2v = if v == 0 {
            0.0
        } else {
            (self.mantissa as f64) / (2f64.powi(v))
        };
        if self.exponent == 0 {
            // 2^(-(30 + v)) * n  =  2^(-30) * (n / 2^v).
            sign * 2f64.powi(-30) * n_over_2v
        } else {
            sign * 2f64.powi((self.exponent as i32) - 31) * (1.0 + n_over_2v)
        }
    }
}

/// Read a §H.13.1.3.1 `depth_representation_sei_element` from `r`.
fn read_depth_float_component(r: &mut BitReader<'_>) -> Result<DepthFloatComponent, SeiError> {
    // da_sign_flag u(1).
    let sign = r.u(1)? == 1;
    // da_exponent u(7).
    let exponent = r.u(7)? as u8;
    // da_mantissa_len_minus1 u(5). Width is 1..=32.
    let mantissa_len_minus1 = r.u(5)? as u8;
    let mantissa_width = mantissa_len_minus1 + 1;
    // da_mantissa u(v). v is in 1..=32 so a single u(<=32) read
    // satisfies the BitReader primitive contract.
    let mantissa = r.u(mantissa_width as u32)? as u64;
    Ok(DepthFloatComponent {
        sign,
        exponent,
        mantissa,
        mantissa_width,
    })
}

/// §H.13.2.3 — depth representation information SEI message.
///
/// Carries depth / disparity range parameters per view used by a
/// renderer prior to display (e.g. for view synthesis on a 3D
/// display). When `all_views_equal_flag == 1`, the message describes
/// one shared view (so `views` has length 1); otherwise it describes
/// `num_views_minus1 + 1` views.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DepthRepresentationInfo {
    /// `all_views_equal_flag` (u(1)) — `true` when the parameters
    /// apply equally to every target view (`views` carries one shared
    /// entry); `false` otherwise.
    pub all_views_equal_flag: bool,
    /// `z_near_flag` (u(1)) — whether `z_near` is signalled in each
    /// view entry.
    pub z_near_flag: bool,
    /// `z_far_flag` (u(1)) — whether `z_far` is signalled in each
    /// view entry.
    pub z_far_flag: bool,
    /// `z_axis_equal_flag` (u(1)) — present iff `z_near_flag ||
    /// z_far_flag`; `None` otherwise. `true` means all per-view
    /// `z_near` / `z_far` values share the same Z-axis (signalled
    /// once in `common_z_axis_reference_view`); `false` means each
    /// view carries its own `z_axis_reference_view[i]`.
    pub z_axis_equal_flag: Option<bool>,
    /// `common_z_axis_reference_view` (ue(v), 0..=1023) — present iff
    /// `z_axis_equal_flag == 1`.
    pub common_z_axis_reference_view: Option<u16>,
    /// `d_min_flag` (u(1)) — whether `d_min` is signalled in each
    /// view entry.
    pub d_min_flag: bool,
    /// `d_max_flag` (u(1)) — whether `d_max` is signalled in each
    /// view entry.
    pub d_max_flag: bool,
    /// `depth_representation_type` (ue(v)) — Table H-1 interpretation
    /// of the decoded depth-view luma samples. 0..=3 are defined by
    /// the spec; 4..=15 are reserved (and the spec requires decoders
    /// to ignore any data that follows a reserved value, so the
    /// nonlinear-representation tail below is skipped in that case).
    pub depth_representation_type: u32,
    /// Per-view entries (length 1 if `all_views_equal_flag`,
    /// otherwise `num_views_minus1 + 1`).
    pub views: Vec<DepthRepresentationView>,
    /// `depth_nonlinear_representation_num_minus1` (ue(v), 0..=62) —
    /// present iff `depth_representation_type == 3`.
    pub depth_nonlinear_representation_num_minus1: Option<u8>,
    /// `depth_nonlinear_representation_model[i]` for `i` in
    /// `1..=depth_nonlinear_representation_num_minus1 + 1`. Each
    /// entry is `ue(v)` in `0..=65535`. Present iff
    /// `depth_representation_type == 3`. The trailing-coverage `i=0`
    /// / `i = num_minus1 + 2` slots are pre-defined to 0 by the spec
    /// (§H.13.2.3 DepthLUT construction) and are not signalled.
    pub depth_nonlinear_representation_model: Vec<u16>,
}

impl DepthRepresentationInfo {
    /// §H.13.2.3 — the spec semantic value
    /// `DepthNonlinearRepresentationNumSegments`: the number of
    /// piecewise linear segments used to map decoded depth-view luma
    /// samples to a scale uniformly quantised in disparity.
    ///
    /// The bitstream stores the count as the biased field
    /// `depth_nonlinear_representation_num_minus1` with the bias
    /// `+ 2` (per the spec sentence:
    /// *"depth_nonlinear_representation_num_minus1 plus 2 specifies
    /// the number of piecewise linear segments…"*). Combined with
    /// the on-wire range `0..=62`, the semantic value lies in
    /// `2..=64`, which always fits in `u8`.
    ///
    /// Returns `None` when the field is absent — i.e. when
    /// `depth_representation_type != 3` and the nonlinear-model
    /// tail is therefore not signalled. The §H.13.2.3 piecewise
    /// model is only defined for `depth_representation_type == 3`,
    /// so no inferred segment count exists for other types.
    ///
    /// Note that the signalled model carries
    /// `num_minus1 + 1` entries (see
    /// [`Self::depth_nonlinear_representation_model_len`]); the
    /// segment count is `num_minus1 + 2` because the §H.13.2.3
    /// DepthLUT construction loops the piecewise linear interpolant
    /// over indices `k = 0..=num_minus1 + 1`, yielding
    /// `num_minus1 + 2` segments delimited by the `num_minus1 + 3`
    /// model points `model[0..=num_minus1 + 2]` (with
    /// `model[0]` and `model[num_minus1 + 2]` pre-defined to `0`
    /// and not signalled).
    #[must_use]
    pub fn depth_nonlinear_representation_num_segments(&self) -> Option<u8> {
        self.depth_nonlinear_representation_num_minus1
            .map(|num_minus1| num_minus1 + 2)
    }

    /// §H.13.2.3 — the number of *signalled*
    /// `depth_nonlinear_representation_model[i]` entries in the
    /// bitstream, equal to `depth_nonlinear_representation_num_minus1
    /// + 1`.
    ///
    /// The spec's syntax loop in §H.13.1.3 runs
    ///
    /// ```text
    /// for( i = 1; i <= depth_nonlinear_representation_num_minus1 + 1; i++ )
    ///     depth_nonlinear_representation_model[ i ]
    /// ```
    ///
    /// so the on-wire count is `num_minus1 + 1`. The two
    /// trailing-coverage sentinels `model[0]` and
    /// `model[num_minus1 + 2]` defined by the §H.13.2.3 DepthLUT
    /// construction are pre-defined to `0` and are not part of the
    /// bitstream — this accessor counts only the entries the
    /// decoder actually parsed (and that are stored in
    /// [`Self::depth_nonlinear_representation_model`]).
    ///
    /// Returns `None` when the field is absent. With the on-wire
    /// range `num_minus1 ∈ 0..=62`, the signalled count lies in
    /// `1..=63` and always fits in `u8`.
    #[must_use]
    pub fn depth_nonlinear_representation_model_len(&self) -> Option<u8> {
        self.depth_nonlinear_representation_num_minus1
            .map(|num_minus1| num_minus1 + 1)
    }
}

/// §H.13.2.3 — per-view depth representation parameters.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DepthRepresentationView {
    /// `depth_info_view_id[i]` (ue(v), 0..=1023) — the `view_id` to
    /// which the rest of the entry applies. When the outer struct's
    /// `all_views_equal_flag` is 1, this carries the spec's view_id
    /// for the single shared entry.
    pub depth_info_view_id: u16,
    /// `z_axis_reference_view[i]` (ue(v), 0..=1023) — present iff
    /// `(z_near_flag || z_far_flag) && !z_axis_equal_flag`.
    pub z_axis_reference_view: Option<u16>,
    /// `disparity_reference_view[i]` (ue(v), 0..=1023) — present iff
    /// `d_min_flag || d_max_flag`.
    pub disparity_reference_view: Option<u16>,
    /// `ZNear[i]` quadruple — present iff `z_near_flag`.
    pub z_near: Option<DepthFloatComponent>,
    /// `ZFar[i]` quadruple — present iff `z_far_flag`.
    pub z_far: Option<DepthFloatComponent>,
    /// `DMin[i]` quadruple — present iff `d_min_flag`.
    pub d_min: Option<DepthFloatComponent>,
    /// `DMax[i]` quadruple — present iff `d_max_flag`.
    pub d_max: Option<DepthFloatComponent>,
}

/// Parse a §H.13.1.3 `depth_representation_info()` payload.
///
/// Enforces the §H.13.2.3 range bounds before allocation:
///
/// * `num_views_minus1 ≤ 1023` (pre-allocation cap on the per-view
///   loop).
/// * `common_z_axis_reference_view` ∈ `0..=1023`.
/// * `depth_info_view_id[i]`, `z_axis_reference_view[i]`,
///   `disparity_reference_view[i]` ∈ `0..=1023`.
/// * `depth_nonlinear_representation_num_minus1 ≤ 62`
///   (pre-allocation cap on the model loop; spec range
///   `0..=62`).
/// * `depth_nonlinear_representation_model[i]` ∈ `0..=65535`.
///
/// `depth_representation_type` in the reserved range `4..=15` is
/// accepted (per §H.13.2.3 the decoder shall ignore trailing data);
/// the nonlinear-model tail is parsed only when the value is exactly
/// 3.
pub fn parse_depth_representation_info(
    payload: &[u8],
) -> Result<DepthRepresentationInfo, SeiError> {
    let mut r = BitReader::new(payload);

    // all_views_equal_flag u(1).
    let all_views_equal_flag = r.u(1)? == 1;

    // num_views_minus1 ue(v), 0..=1023 (anti-OOM cap before the
    // per-view loop allocation). Present iff all_views_equal_flag
    // == 0; when 1 the loop runs once over a shared entry.
    let num_views = if all_views_equal_flag {
        1usize
    } else {
        let v = r.ue()?;
        if v > 1023 {
            return Err(SeiError::DepthRepresentationInfoNumViewsOutOfRange(v));
        }
        (v as usize) + 1
    };

    // z_near_flag u(1), z_far_flag u(1).
    let z_near_flag = r.u(1)? == 1;
    let z_far_flag = r.u(1)? == 1;

    // z_axis_equal_flag u(1) + common_z_axis_reference_view ue(v)
    // gated by (z_near_flag || z_far_flag).
    let (z_axis_equal_flag, common_z_axis_reference_view) = if z_near_flag || z_far_flag {
        let z_axis_equal_flag = r.u(1)? == 1;
        let common = if z_axis_equal_flag {
            let v = r.ue()?;
            if v > 1023 {
                return Err(SeiError::DepthRepresentationInfoViewIdOutOfRange {
                    field: "common_z_axis_reference_view",
                    got: v,
                });
            }
            Some(v as u16)
        } else {
            None
        };
        (Some(z_axis_equal_flag), common)
    } else {
        (None, None)
    };

    // d_min_flag u(1), d_max_flag u(1).
    let d_min_flag = r.u(1)? == 1;
    let d_max_flag = r.u(1)? == 1;

    // depth_representation_type ue(v). 0..=3 defined, 4..=15
    // reserved (per §H.13.2.3 the decoder shall ignore the tail when
    // the value is reserved). Values outside 0..=15 are not
    // explicitly bounded by §H.13.2.3 (the prose says "0 to 3,
    // inclusive, in bitstreams conforming to this version" + "4 to
    // 15, inclusive" reserved), so we store the raw ue(v) value
    // without an upper bound: §H.13.2.3 instructs the decoder to
    // ignore the tail for any non-{0..=3} value, which we honour by
    // only parsing the §H.13.2.3 nonlinear tail when the value is
    // exactly 3.
    let depth_representation_type = r.ue()?;

    let mut views = Vec::with_capacity(num_views);
    for _ in 0..num_views {
        // depth_info_view_id[i] ue(v), 0..=1023.
        let depth_info_view_id = {
            let v = r.ue()?;
            if v > 1023 {
                return Err(SeiError::DepthRepresentationInfoViewIdOutOfRange {
                    field: "depth_info_view_id",
                    got: v,
                });
            }
            v as u16
        };
        // z_axis_reference_view[i] ue(v), 0..=1023, iff
        // (z_near_flag || z_far_flag) && !z_axis_equal_flag.
        let z_axis_reference_view =
            if (z_near_flag || z_far_flag) && z_axis_equal_flag == Some(false) {
                let v = r.ue()?;
                if v > 1023 {
                    return Err(SeiError::DepthRepresentationInfoViewIdOutOfRange {
                        field: "z_axis_reference_view",
                        got: v,
                    });
                }
                Some(v as u16)
            } else {
                None
            };
        // disparity_reference_view[i] ue(v), 0..=1023, iff
        // d_min_flag || d_max_flag.
        let disparity_reference_view = if d_min_flag || d_max_flag {
            let v = r.ue()?;
            if v > 1023 {
                return Err(SeiError::DepthRepresentationInfoViewIdOutOfRange {
                    field: "disparity_reference_view",
                    got: v,
                });
            }
            Some(v as u16)
        } else {
            None
        };
        // Optional ZNear[i] depth_representation_sei_element.
        let z_near = if z_near_flag {
            Some(read_depth_float_component(&mut r)?)
        } else {
            None
        };
        // Optional ZFar[i].
        let z_far = if z_far_flag {
            Some(read_depth_float_component(&mut r)?)
        } else {
            None
        };
        // Optional DMin[i].
        let d_min = if d_min_flag {
            Some(read_depth_float_component(&mut r)?)
        } else {
            None
        };
        // Optional DMax[i].
        let d_max = if d_max_flag {
            Some(read_depth_float_component(&mut r)?)
        } else {
            None
        };

        views.push(DepthRepresentationView {
            depth_info_view_id,
            z_axis_reference_view,
            disparity_reference_view,
            z_near,
            z_far,
            d_min,
            d_max,
        });
    }

    // depth_nonlinear_representation_num_minus1 + per-segment
    // depth_nonlinear_representation_model[i], iff
    // depth_representation_type == 3.
    let (depth_nonlinear_representation_num_minus1, depth_nonlinear_representation_model) =
        if depth_representation_type == 3 {
            let num_minus1 = r.ue()?;
            if num_minus1 > 62 {
                return Err(SeiError::DepthRepresentationInfoNonlinearNumOutOfRange(
                    num_minus1,
                ));
            }
            // The spec loop runs i = 1..=depth_nonlinear_representation_num_minus1 + 1,
            // so the signalled count is num_minus1 + 1 (the i=0 and
            // i=num_minus1+2 sentinels are pre-defined to 0 by the
            // spec — they're not in the bitstream).
            let count = (num_minus1 as usize) + 1;
            let mut model = Vec::with_capacity(count);
            for i in 0..count {
                let v = r.ue()?;
                if v > 65535 {
                    return Err(SeiError::DepthRepresentationInfoNonlinearModelOutOfRange {
                        i,
                        got: v,
                    });
                }
                model.push(v as u16);
            }
            (Some(num_minus1 as u8), model)
        } else {
            (None, Vec::new())
        };

    Ok(DepthRepresentationInfo {
        all_views_equal_flag,
        z_near_flag,
        z_far_flag,
        z_axis_equal_flag,
        common_z_axis_reference_view,
        d_min_flag,
        d_max_flag,
        depth_representation_type,
        views,
        depth_nonlinear_representation_num_minus1,
        depth_nonlinear_representation_model,
    })
}

/// Annex I §I.13.1.1 / §I.13.2.1 — `constrained_depth_parameter_set_identifier`
/// (payload type 54, Annex I 3D-AVC depth coding).
///
/// When present, this SEI message is associated with an IDR access unit
/// and indicates that the `depth_parameter_set_id` and `dps_id` values
/// in the coded video sequence are constrained to the windowed range
/// described by the (`max_dps_id`, `max_dps_id_diff`) pair. Decoders
/// use this signal to conclude losses of depth parameter set NAL units
/// (NOTE 1 in §I.13.2.1) and to maintain the running `MaxUsedDpsId` /
/// `UsedDpsIdSet` state per slice.
///
/// Syntax — §I.13.1.1:
///
/// ```text
/// constrained_depth_parameter_set_identifier( payloadSize ) {
///     max_dps_id          ue(v)
///     max_dps_id_diff     ue(v)
/// }
/// ```
///
/// Constraints — §I.13.2.1:
///
/// * `max_dps_id` plus 1 specifies the maximum allowed
///   `depth_range_parameter_set_id` value. Per §7.4.2.16, the
///   `depth_parameter_set_id` field itself is in the range
///   `1..=63` (i.e. 0..=63 storage with 0 reserved for the
///   active-SPS-bound default), so the maximum meaningful value of
///   `max_dps_id` is 62 (encoding `max_dps_id + 1 ≤ 63`). Values
///   greater than 62 are rejected before storage.
/// * `max_dps_id_diff * 2` shall be less than `max_dps_id`. This is
///   the normative window-width constraint for the §I.13.2.1 sliding
///   used-DPS-id range computation; a value that violates it would
///   make the `prevMinUsedDpsId` / `minUsedDpsId` distance walk
///   around §I.13.2.1 eq. (I-86) ambiguous (the window would overlap
///   itself), so the parser rejects it before storage.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConstrainedDepthParameterSetIdentifier {
    /// `max_dps_id` plus 1 specifies the maximum allowed
    /// `depth_range_parameter_set_id` value. Raw ue(v) value, bounded
    /// 0..=62 per the constraint above.
    pub max_dps_id: u8,
    /// `max_dps_id_diff` specifies the value range of
    /// `depth_range_parameter_set_id` values marked as "used". Raw
    /// ue(v) value, bounded 0..=30 (since `max_dps_id_diff * 2 <
    /// max_dps_id` and `max_dps_id ≤ 62`).
    pub max_dps_id_diff: u8,
}

/// Parse a §I.13.1.1 `constrained_depth_parameter_set_identifier()`
/// payload.
///
/// Enforces both §I.13.2.1 normative constraints before storage:
/// * `max_dps_id ≤ 62` — derived from the `depth_parameter_set_id`
///   range `1..=63` (§7.4.2.16).
/// * `max_dps_id_diff * 2 < max_dps_id` — the §I.13.2.1 sliding-window
///   integrity constraint for the §I.13.2.1 eq. (I-86) used-DPS-id
///   computation.
pub fn parse_constrained_depth_parameter_set_identifier(
    payload: &[u8],
) -> Result<ConstrainedDepthParameterSetIdentifier, SeiError> {
    let mut r = BitReader::new(payload);

    // §I.13.1.1 — max_dps_id ue(v).
    let max_dps_id = r.ue()?;
    if max_dps_id > 62 {
        return Err(SeiError::ConstrainedDepthParameterSetIdentifierMaxDpsIdOutOfRange(max_dps_id));
    }

    // §I.13.1.1 — max_dps_id_diff ue(v).
    let max_dps_id_diff = r.ue()?;
    // §I.13.2.1: `max_dps_id_diff * 2 < max_dps_id`. Use u64 widening
    // so the multiplication never overflows even at the ue(v) ceiling.
    if (max_dps_id_diff as u64) * 2 >= max_dps_id as u64 {
        return Err(
            SeiError::ConstrainedDepthParameterSetIdentifierDiffViolatesBound {
                max_dps_id_diff,
                max_dps_id,
            },
        );
    }

    Ok(ConstrainedDepthParameterSetIdentifier {
        max_dps_id: max_dps_id as u8,
        max_dps_id_diff: max_dps_id_diff as u8,
    })
}

// -------------------------------------------------------------------------
// Annex H §H.13.1.6 / §H.13.2.6 — alternative_depth_info (SEI payload 181)
// -------------------------------------------------------------------------

/// §H.13.2.6 — global view and depth (GVD) per-camera parameter block,
/// present in an [`AlternativeDepthInfo`] when `depth_type == 0`.
///
/// The `i`-index of every per-camera vector runs `0..=num_constituent_
/// views_gvd_minus1 + 1` (that is, `num_constituent_views_gvd_minus1 + 2`
/// cameras: i == 0 is the base texture view, i > 0 the constituent
/// views packed per Table H-4). Each optional sub-block is gated by its
/// corresponding presence flag.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AlternativeDepthGvd {
    /// `num_constituent_views_gvd_minus1` (ue(v), 0..=3) — the number
    /// of constituent texture pictures packed into each non-base view
    /// texture component, minus one.
    pub num_constituent_views_gvd_minus1: u8,
    /// `depth_present_gvd_flag` (u(1)).
    pub depth_present_gvd_flag: bool,
    /// `z_gvd_flag` (u(1)) — gates the per-camera near/far depth block.
    pub z_gvd_flag: bool,
    /// `intrinsic_param_gvd_flag` (u(1)) — gates the focal-length +
    /// principal-point block.
    pub intrinsic_param_gvd_flag: bool,
    /// `rotation_gvd_flag` (u(1)) — gates the 3×3 rotation block. When
    /// `false` a unit rotation matrix is inferred per §H.13.2.6.
    pub rotation_gvd_flag: bool,
    /// `translation_gvd_flag` (u(1)) — gates the horizontal translation
    /// block.
    pub translation_gvd_flag: bool,
    /// Per-camera near/far depth values. Present (length =
    /// `num_constituent_views_gvd_minus1 + 2`) iff `z_gvd_flag`; empty
    /// otherwise.
    pub z_values: Vec<AlternativeDepthZ>,
    /// `prec_gvd_focal_length` (ue(v), 0..=31). `None` iff
    /// `!intrinsic_param_gvd_flag`.
    pub prec_gvd_focal_length: Option<u8>,
    /// `prec_gvd_principal_point` (ue(v), 0..=31). `None` iff
    /// `!intrinsic_param_gvd_flag`.
    pub prec_gvd_principal_point: Option<u8>,
    /// `prec_gvd_rotation_param` (ue(v), 0..=31). `None` iff
    /// `!rotation_gvd_flag`.
    pub prec_gvd_rotation_param: Option<u8>,
    /// `prec_gvd_translation_param` (ue(v), 0..=31). `None` iff
    /// `!translation_gvd_flag`.
    pub prec_gvd_translation_param: Option<u8>,
    /// Per-camera intrinsic + extrinsic float entries, one per camera
    /// (`num_constituent_views_gvd_minus1 + 2`). Each entry's optional
    /// members mirror the per-block presence flags above.
    pub cameras: Vec<AlternativeDepthCamera>,
}

/// §H.13.2.6 — per-camera nearest/farthest depth pair. Each value uses
/// the §H.13.2.3 depth float encoding (sign u(1), exponent u(7) with
/// reserved value 127, explicit mantissa length `man_len_minus1 + 1`),
/// so [`DepthFloatComponent`] is reused verbatim.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AlternativeDepthZ {
    /// `zNear[i]` per eq. H-1 (`man_gvd_z_near` mantissa width is
    /// `man_len_gvd_z_near_minus1 + 1`).
    pub z_near: DepthFloatComponent,
    /// `zFar[i]` per eq. H-1.
    pub z_far: DepthFloatComponent,
}

/// §H.13.2.6 — per-camera intrinsic + extrinsic float parameters. Each
/// scalar uses the §G.13.2.5-style float encoding (sign u(1), exponent
/// u(6) with reserved value 63, prec-derived mantissa width per eq.
/// in §H.13.2.6), so [`FloatComponent`] is reused.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AlternativeDepthCamera {
    /// `(focalLengthX, focalLengthY, principalPointX, principalPointY)`
    /// — `Some` iff `intrinsic_param_gvd_flag`.
    pub intrinsic: Option<AlternativeDepthIntrinsic>,
    /// 3×3 rotation matrix `r[j][k]` (0-indexed) — `Some` iff
    /// `rotation_gvd_flag`.
    pub rotation: Option<[[FloatComponent; 3]; 3]>,
    /// Horizontal translation `tX` — `Some` iff `translation_gvd_flag`.
    pub translation_x: Option<FloatComponent>,
}

/// §H.13.2.6 — per-camera intrinsic float quadruple.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AlternativeDepthIntrinsic {
    pub focal_length_x: FloatComponent,
    pub focal_length_y: FloatComponent,
    pub principal_point_x: FloatComponent,
    pub principal_point_y: FloatComponent,
}

/// §H.13.2.6 — alternative depth information SEI message (payload type
/// 181, Annex H). Indicates that one output view's view components are
/// a spatial packing of multiple distinct constituent pictures (global
/// view + depth, GVD), so the view is not suitable for direct display.
///
/// `depth_type` selects the body: only `depth_type == 0` carries the
/// GVD parameter block (`gvd`). Other values are reserved; per
/// §H.13.2.6 "Decoders shall ignore alternative depth information SEI
/// messages in which such other values are present", so for a non-zero
/// type we record the type and leave `gvd` as `None` (graceful ignore,
/// not an error).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AlternativeDepthInfo {
    /// `depth_type` (ue(v)). Conforming bitstreams use 0.
    pub depth_type: u32,
    /// GVD parameter block, present iff `depth_type == 0`.
    pub gvd: Option<AlternativeDepthGvd>,
}

/// Helper: read a `prec_gvd_*` ue(v) and enforce the §H.13.2.6 `0..=31`
/// range bound, surfacing an alternative-depth-specific error.
fn read_alt_depth_prec(r: &mut BitReader<'_>, field: &'static str) -> Result<u8, SeiError> {
    let v = r.ue()?;
    if v > 31 {
        return Err(SeiError::AlternativeDepthInfoPrecOutOfRange { field, got: v });
    }
    Ok(v as u8)
}

/// Parse an Annex H §H.13.1.6 `alternative_depth_info()` payload.
///
/// Enforces:
///
/// * `num_constituent_views_gvd_minus1 ≤ 3` (§H.13.2.6).
/// * each `prec_gvd_*` ∈ `0..=31` (§H.13.2.6).
///
/// Float components are stored verbatim:
///
/// * near/far depth pairs use [`DepthFloatComponent`] (exponent u(7),
///   reserved 127, explicit mantissa length).
/// * intrinsic / extrinsic scalars use [`FloatComponent`] (exponent
///   u(6), reserved 63, prec-derived mantissa width via
///   [`mantissa_width_g1325`], shared with §G.13.2.5).
///
/// Non-zero `depth_type` values are reserved; the body is not parsed
/// (graceful ignore per §H.13.2.6).
pub fn parse_alternative_depth_info(payload: &[u8]) -> Result<AlternativeDepthInfo, SeiError> {
    let mut r = BitReader::new(payload);

    // §H.13.1.6 — depth_type ue(v).
    let depth_type = r.ue()?;
    if depth_type != 0 {
        // Reserved value — decoders shall ignore the message body.
        return Ok(AlternativeDepthInfo {
            depth_type,
            gvd: None,
        });
    }

    // §H.13.1.6 — num_constituent_views_gvd_minus1 ue(v), 0..=3.
    let num_constituent_views_gvd_minus1 = r.ue()?;
    if num_constituent_views_gvd_minus1 > 3 {
        return Err(SeiError::AlternativeDepthInfoNumConstituentViewsOutOfRange(
            num_constituent_views_gvd_minus1,
        ));
    }
    // i runs 0..=num_constituent_views_gvd_minus1 + 1 → that many + 2.
    let cam_count = (num_constituent_views_gvd_minus1 as usize) + 2;

    // §H.13.1.6 — five u(1) presence flags.
    let depth_present_gvd_flag = r.u(1)? == 1;
    let z_gvd_flag = r.u(1)? == 1;
    let intrinsic_param_gvd_flag = r.u(1)? == 1;
    let rotation_gvd_flag = r.u(1)? == 1;
    let translation_gvd_flag = r.u(1)? == 1;

    // §H.13.1.6 — when z_gvd_flag, a per-camera near/far depth pair.
    let z_values = if z_gvd_flag {
        let mut v = Vec::with_capacity(cam_count);
        for _ in 0..cam_count {
            // §H.13.1.6: sign u(1), exp u(7), man_len_minus1 u(5),
            // man u(man_len_minus1 + 1) — exactly the §H.13.2.3
            // depth-float layout, so reuse read_depth_float_component.
            let z_near = read_depth_float_component(&mut r)?;
            let z_far = read_depth_float_component(&mut r)?;
            v.push(AlternativeDepthZ { z_near, z_far });
        }
        v
    } else {
        Vec::new()
    };

    // §H.13.1.6 — precision ue(v) words, each gated by its block flag.
    let prec_gvd_focal_length = if intrinsic_param_gvd_flag {
        Some(read_alt_depth_prec(&mut r, "prec_gvd_focal_length")?)
    } else {
        None
    };
    let prec_gvd_principal_point = if intrinsic_param_gvd_flag {
        Some(read_alt_depth_prec(&mut r, "prec_gvd_principal_point")?)
    } else {
        None
    };
    let prec_gvd_rotation_param = if rotation_gvd_flag {
        Some(read_alt_depth_prec(&mut r, "prec_gvd_rotation_param")?)
    } else {
        None
    };
    let prec_gvd_translation_param = if translation_gvd_flag {
        Some(read_alt_depth_prec(&mut r, "prec_gvd_translation_param")?)
    } else {
        None
    };

    // §H.13.1.6 — per-camera intrinsic / rotation / translation floats.
    let mut cameras: Vec<AlternativeDepthCamera> = Vec::with_capacity(cam_count);
    for _ in 0..cam_count {
        let intrinsic = if intrinsic_param_gvd_flag {
            let prec_fl = prec_gvd_focal_length.unwrap();
            let prec_pp = prec_gvd_principal_point.unwrap();
            let focal_length_x = read_float_component(&mut r, prec_fl)?;
            let focal_length_y = read_float_component(&mut r, prec_fl)?;
            let principal_point_x = read_float_component(&mut r, prec_pp)?;
            let principal_point_y = read_float_component(&mut r, prec_pp)?;
            Some(AlternativeDepthIntrinsic {
                focal_length_x,
                focal_length_y,
                principal_point_x,
                principal_point_y,
            })
        } else {
            None
        };

        let rotation = if rotation_gvd_flag {
            let prec_rot = prec_gvd_rotation_param.unwrap();
            let zero = FloatComponent {
                sign: false,
                exponent: 0,
                mantissa: 0,
                mantissa_width: 0,
            };
            // §H.13.1.6: for( j = 0; j < 3; j++ ) for( k = 0; k < 3; k++ ).
            let mut mat: [[FloatComponent; 3]; 3] = [[zero; 3]; 3];
            for row in mat.iter_mut() {
                for cell in row.iter_mut() {
                    *cell = read_float_component(&mut r, prec_rot)?;
                }
            }
            Some(mat)
        } else {
            None
        };

        let translation_x = if translation_gvd_flag {
            let prec_tr = prec_gvd_translation_param.unwrap();
            Some(read_float_component(&mut r, prec_tr)?)
        } else {
            None
        };

        cameras.push(AlternativeDepthCamera {
            intrinsic,
            rotation,
            translation_x,
        });
    }

    Ok(AlternativeDepthInfo {
        depth_type,
        gvd: Some(AlternativeDepthGvd {
            num_constituent_views_gvd_minus1: num_constituent_views_gvd_minus1 as u8,
            depth_present_gvd_flag,
            z_gvd_flag,
            intrinsic_param_gvd_flag,
            rotation_gvd_flag,
            translation_gvd_flag,
            z_values,
            prec_gvd_focal_length,
            prec_gvd_principal_point,
            prec_gvd_rotation_param,
            prec_gvd_translation_param,
            cameras,
        }),
    })
}

/// Annex H §H.13.1.7.1 — `depth_grid_position()` sub-structure used by
/// the §H.13.1.7 `depth_sampling_info` SEI message.
///
/// Carries the raw bitstream values for the horizontal + vertical
/// top-left-sample location of a depth view's sampling grid relative
/// to the same-`view_id` texture view. The §H.13.2.7.1 reconstruction
/// formulas are:
///
/// * horizontal location = `(1 − 2 * depth_grid_pos_x_sign_flag) *
///   (depth_grid_pos_x_fp ÷ 2^depth_grid_pos_x_dp)`
/// * vertical   location = `(1 − 2 * depth_grid_pos_y_sign_flag) *
///   (depth_grid_pos_y_fp ÷ 2^depth_grid_pos_y_dp)`
///
/// All six fields are u(n) reads so their range follows directly from
/// the bit width:
///
/// * `depth_grid_pos_{x,y}_fp` — u(20), 0..=1_048_575
/// * `depth_grid_pos_{x,y}_dp` — u(4), 0..=15
/// * `depth_grid_pos_{x,y}_sign_flag` — u(1), 0..=1
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DepthGridPosition {
    /// `depth_grid_pos_x_fp` — u(20). Raw fixed-point magnitude of
    /// the horizontal grid position.
    pub pos_x_fp: u32,
    /// `depth_grid_pos_x_dp` — u(4). Number of fractional bits in
    /// `pos_x_fp` (the divisor is `2^pos_x_dp`).
    pub pos_x_dp: u8,
    /// `depth_grid_pos_x_sign_flag` — u(1). `false` for positive,
    /// `true` for negative.
    pub pos_x_sign: bool,
    /// `depth_grid_pos_y_fp` — u(20). Raw fixed-point magnitude of
    /// the vertical grid position.
    pub pos_y_fp: u32,
    /// `depth_grid_pos_y_dp` — u(4). Number of fractional bits in
    /// `pos_y_fp` (the divisor is `2^pos_y_dp`).
    pub pos_y_dp: u8,
    /// `depth_grid_pos_y_sign_flag` — u(1). `false` for positive,
    /// `true` for negative.
    pub pos_y_sign: bool,
}

impl DepthGridPosition {
    /// Reconstruct the §H.13.2.7.1 horizontal position scalar
    /// `(1 − 2 * pos_x_sign_flag) * (pos_x_fp ÷ 2^pos_x_dp)`.
    pub fn x_to_f64(&self) -> f64 {
        let mag = (self.pos_x_fp as f64) / (1u64 << self.pos_x_dp) as f64;
        if self.pos_x_sign {
            -mag
        } else {
            mag
        }
    }
    /// Reconstruct the §H.13.2.7.1 vertical position scalar
    /// `(1 − 2 * pos_y_sign_flag) * (pos_y_fp ÷ 2^pos_y_dp)`.
    pub fn y_to_f64(&self) -> f64 {
        let mag = (self.pos_y_fp as f64) / (1u64 << self.pos_y_dp) as f64;
        if self.pos_y_sign {
            -mag
        } else {
            mag
        }
    }
}

fn read_depth_grid_position(r: &mut BitReader) -> Result<DepthGridPosition, SeiError> {
    // §H.13.1.7.1 — depth_grid_pos_x_fp u(20); depth_grid_pos_x_dp u(4);
    // depth_grid_pos_x_sign_flag u(1); same triple repeated for y.
    let pos_x_fp = r.u(20)?;
    let pos_x_dp = r.u(4)? as u8;
    let pos_x_sign = r.u(1)? == 1;
    let pos_y_fp = r.u(20)?;
    let pos_y_dp = r.u(4)? as u8;
    let pos_y_sign = r.u(1)? == 1;
    Ok(DepthGridPosition {
        pos_x_fp,
        pos_x_dp,
        pos_x_sign,
        pos_y_fp,
        pos_y_dp,
        pos_y_sign,
    })
}

/// Per-view depth grid position entry used by §H.13.1.7
/// `depth_sampling_info` when `per_view_depth_grid_pos_flag == 1`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DepthSamplingViewEntry {
    /// `depth_grid_view_id[i]` — ue(v). The `view_id` value for which
    /// the following `depth_grid_position()` applies. Bounded 0..=1023
    /// per the Annex G/H view_id range.
    pub depth_grid_view_id: u16,
    /// `depth_grid_position()` for this `view_id`.
    pub position: DepthGridPosition,
}

/// Annex H §H.13.1.7 / §H.13.2.7 — `depth_sampling_info` (payload
/// type 53).
///
/// Specifies the depth-sample size relative to the luma-texture-sample
/// size and the depth-sampling grid position for one or more depth
/// view components relative to the texture view component with the
/// same `view_id` value. When present, this SEI message shall be
/// associated with an IDR access unit; the semantics apply to the
/// current coded video sequence.
///
/// Syntax — §H.13.1.7:
///
/// ```text
/// depth_sampling_info( payloadSize ) {
///     dttsr_x_mul                                u(16)
///     dttsr_x_dp                                 u(4)
///     dttsr_y_mul                                u(16)
///     dttsr_y_dp                                 u(4)
///     per_view_depth_grid_pos_flag               u(1)
///     if( per_view_depth_grid_pos_flag ) {
///         num_video_plus_depth_views_minus1      ue(v)
///         for( i = 0; i <= num_video_plus_depth_views_minus1; i++ ) {
///             depth_grid_view_id[i]              ue(v)
///             depth_grid_position()
///         }
///     } else
///         depth_grid_position()
/// }
/// ```
///
/// Sample-size semantics (§H.13.2.7):
///
/// * The width of a depth sample relative to the width of a luma
///   texture sample is approximately
///   `dttsr_x_mul ÷ 2^dttsr_x_dp`. The value 0 for `dttsr_x_mul` is
///   reserved (rejected with
///   [`SeiError::DepthSamplingInfoDttsrMulReserved`]).
/// * The height of a depth sample relative to the height of a luma
///   texture sample is approximately
///   `dttsr_y_mul ÷ 2^dttsr_y_dp`. The value 0 for `dttsr_y_mul` is
///   reserved (rejected with
///   [`SeiError::DepthSamplingInfoDttsrMulReserved`]).
///
/// Grid-position semantics (§H.13.2.7):
///
/// * `per_view_depth_grid_pos_flag == 0` — a single
///   `depth_grid_position()` describes the same grid for all depth
///   views in the access unit (stored as a single-entry
///   `views` vector with a sentinel `depth_grid_view_id = 0`).
/// * `per_view_depth_grid_pos_flag == 1` — one
///   `depth_grid_position()` per declared `view_id`. The Annex G/H
///   absolute upper bound on `num_views_minus1` (1023, derived from
///   `num_views_minus1 ≤ 1023` in the Annex G/H SPS extensions) is
///   used as the anti-OOM cap before allocating the per-view vector.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DepthSamplingInfo {
    /// `dttsr_x_mul` — u(16), the horizontal depth/texture sample
    /// ratio numerator. The value 0 is reserved.
    pub dttsr_x_mul: u16,
    /// `dttsr_x_dp` — u(4), the horizontal ratio's fractional bit
    /// width (divisor is `2^dttsr_x_dp`).
    pub dttsr_x_dp: u8,
    /// `dttsr_y_mul` — u(16), the vertical depth/texture sample ratio
    /// numerator. The value 0 is reserved.
    pub dttsr_y_mul: u16,
    /// `dttsr_y_dp` — u(4), the vertical ratio's fractional bit width
    /// (divisor is `2^dttsr_y_dp`).
    pub dttsr_y_dp: u8,
    /// `per_view_depth_grid_pos_flag` — u(1). When `true`, `views`
    /// carries one entry per signalled `view_id`. When `false`,
    /// `views` carries a single shared entry.
    pub per_view_depth_grid_pos_flag: bool,
    /// `num_video_plus_depth_views_minus1` — only present when
    /// `per_view_depth_grid_pos_flag == 1`. Plus one gives the count
    /// of per-view entries that follow. Bounded 0..=1023 per the
    /// Annex G/H absolute `num_views_minus1 ≤ 1023`.
    pub num_video_plus_depth_views_minus1: Option<u16>,
    /// Either a single shared `depth_grid_position()` entry
    /// (`per_view_depth_grid_pos_flag == 0`, length 1, sentinel
    /// `depth_grid_view_id = 0`) or one entry per signalled `view_id`.
    pub views: Vec<DepthSamplingViewEntry>,
}

impl DepthSamplingInfo {
    /// Reconstruct the §H.13.2.7 horizontal depth/texture sample-size
    /// ratio `dttsr_x_mul ÷ 2^dttsr_x_dp`. Always > 0 since the parser
    /// rejects `dttsr_x_mul == 0` (Annex H §H.13.2.7 reserved value).
    pub fn dttsr_x_to_f64(&self) -> f64 {
        (self.dttsr_x_mul as f64) / (1u64 << self.dttsr_x_dp) as f64
    }
    /// Reconstruct the §H.13.2.7 vertical depth/texture sample-size
    /// ratio `dttsr_y_mul ÷ 2^dttsr_y_dp`. Always > 0 since the parser
    /// rejects `dttsr_y_mul == 0` (Annex H §H.13.2.7 reserved value).
    pub fn dttsr_y_to_f64(&self) -> f64 {
        (self.dttsr_y_mul as f64) / (1u64 << self.dttsr_y_dp) as f64
    }
}

/// Parse a §H.13.1.7 `depth_sampling_info()` payload (payload type 53).
///
/// Enforces the §H.13.2.7 normative range bounds before any
/// allocation:
/// * `dttsr_x_mul != 0` and `dttsr_y_mul != 0` (the value 0 is
///   reserved per §H.13.2.7).
/// * `num_video_plus_depth_views_minus1 ≤ 1023` (Annex G/H absolute
///   `num_views_minus1` cap). Pre-allocation gate against the
///   ue(v)-driven OOM lever closed in round 177 for §D.1.20 and
///   round 200 for §G.13.2.8.
/// * `depth_grid_view_id[i] ≤ 1023` (Annex G/H view_id range).
pub fn parse_depth_sampling_info(payload: &[u8]) -> Result<DepthSamplingInfo, SeiError> {
    let mut r = BitReader::new(payload);

    // §H.13.1.7 — dttsr_x_mul u(16). §H.13.2.7: 0 is reserved.
    let dttsr_x_mul = r.u(16)? as u16;
    if dttsr_x_mul == 0 {
        return Err(SeiError::DepthSamplingInfoDttsrMulReserved { axis: "x" });
    }
    // §H.13.1.7 — dttsr_x_dp u(4).
    let dttsr_x_dp = r.u(4)? as u8;
    // §H.13.1.7 — dttsr_y_mul u(16). §H.13.2.7: 0 is reserved.
    let dttsr_y_mul = r.u(16)? as u16;
    if dttsr_y_mul == 0 {
        return Err(SeiError::DepthSamplingInfoDttsrMulReserved { axis: "y" });
    }
    // §H.13.1.7 — dttsr_y_dp u(4).
    let dttsr_y_dp = r.u(4)? as u8;
    // §H.13.1.7 — per_view_depth_grid_pos_flag u(1).
    let per_view_depth_grid_pos_flag = r.u(1)? == 1;

    let (num_video_plus_depth_views_minus1, views) = if per_view_depth_grid_pos_flag {
        // §H.13.1.7 — num_video_plus_depth_views_minus1 ue(v).
        // §H.13.2.7: views_minus1 + 1 is the number of declared
        // view_ids. Anti-OOM cap derived from the Annex G/H absolute
        // `num_views_minus1 ≤ 1023`.
        let num_minus1 = r.ue()?;
        if num_minus1 > 1023 {
            return Err(SeiError::DepthSamplingInfoNumViewsOutOfRange(num_minus1));
        }
        let count = (num_minus1 as usize) + 1;
        let mut entries = Vec::with_capacity(count);
        for i in 0..count {
            // §H.13.1.7 — depth_grid_view_id[i] ue(v). §H.13.2.7
            // refers to the same view_id 0..=1023 range used across
            // Annex G/H view-signalling messages; reject anything
            // beyond.
            let view_id = r.ue()?;
            if view_id > 1023 {
                return Err(SeiError::DepthSamplingInfoViewIdOutOfRange { i, got: view_id });
            }
            let position = read_depth_grid_position(&mut r)?;
            entries.push(DepthSamplingViewEntry {
                depth_grid_view_id: view_id as u16,
                position,
            });
        }
        (Some(num_minus1 as u16), entries)
    } else {
        // §H.13.1.7 else branch — single shared depth_grid_position().
        // We surface it as a single-entry views vector with a
        // sentinel depth_grid_view_id = 0 so downstream consumers
        // have a uniform shape.
        let position = read_depth_grid_position(&mut r)?;
        (
            None,
            vec![DepthSamplingViewEntry {
                depth_grid_view_id: 0,
                position,
            }],
        )
    };

    Ok(DepthSamplingInfo {
        dttsr_x_mul,
        dttsr_x_dp,
        dttsr_y_mul,
        dttsr_y_dp,
        per_view_depth_grid_pos_flag,
        num_video_plus_depth_views_minus1,
        views,
    })
}

/// Annex H §H.13.1.5.1 / §H.13.2.5.1 — `depth_timing_offset()`
/// sub-structure used by the §H.13.1.5 `depth_timing` SEI message.
///
/// Specifies the acquisition offset of the respective depth view
/// component(s) relative to the DPB output time of the access unit
/// containing them, equal to
/// `depth_disp_delay_offset_fp ÷ 2^depth_disp_delay_offset_dp` in
/// units of clock ticks as specified in Annex C (§H.13.2.5.1).
///
/// Field ranges follow directly from the bit widths:
///
/// * `offset_len_minus1` — u(5), 0..=31; plus 1 gives the bit width
///   of `depth_disp_delay_offset_fp` (§H.13.2.5.1: "The length of
///   depth_disp_delay_offset_fp syntax element is equal to
///   offset_len_minus1 + 1"), so the fp field spans 1..=32 bits.
/// * `depth_disp_delay_offset_fp` — u(offset_len_minus1 + 1),
///   0..=`2^(offset_len_minus1 + 1) − 1` (up to the full u32 range).
/// * `depth_disp_delay_offset_dp` — u(6), 0..=63.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DepthTimingOffset {
    /// `offset_len_minus1` — u(5). Plus 1 gives the bit width of
    /// `depth_disp_delay_offset_fp`.
    pub offset_len_minus1: u8,
    /// `depth_disp_delay_offset_fp` — u(offset_len_minus1 + 1). The
    /// fixed-point numerator of the acquisition offset.
    pub depth_disp_delay_offset_fp: u32,
    /// `depth_disp_delay_offset_dp` — u(6). The number of fractional
    /// bits in `depth_disp_delay_offset_fp` (the divisor is
    /// `2^depth_disp_delay_offset_dp`).
    pub depth_disp_delay_offset_dp: u8,
}

impl DepthTimingOffset {
    /// Reconstruct the §H.13.2.5.1 acquisition-offset scalar
    /// `depth_disp_delay_offset_fp ÷ 2^depth_disp_delay_offset_dp`,
    /// in units of clock ticks as specified in Annex C. Always
    /// non-negative — the §H.13.1.5.1 syntax has no sign field.
    pub fn offset_clock_ticks(&self) -> f64 {
        f64::from(self.depth_disp_delay_offset_fp)
            / 2f64.powi(i32::from(self.depth_disp_delay_offset_dp))
    }
}

fn read_depth_timing_offset(r: &mut BitReader) -> Result<DepthTimingOffset, SeiError> {
    // §H.13.1.5.1 — offset_len_minus1 u(5);
    // depth_disp_delay_offset_fp u(offset_len_minus1 + 1);
    // depth_disp_delay_offset_dp u(6).
    let offset_len_minus1 = r.u(5)? as u8;
    let depth_disp_delay_offset_fp = r.u(u32::from(offset_len_minus1) + 1)?;
    let depth_disp_delay_offset_dp = r.u(6)? as u8;
    Ok(DepthTimingOffset {
        offset_len_minus1,
        depth_disp_delay_offset_fp,
        depth_disp_delay_offset_dp,
    })
}

/// Annex H §H.13.1.5 / §H.13.2.5 — `depth_timing` (payload type 52).
///
/// Indicates the acquisition time of the depth view components of one
/// or more access units relative to the DPB output time of the same
/// access units. The message may be present in any access unit and
/// pertains until the end of the coded video sequence or until the
/// next depth timing SEI message, whichever is earlier in decoding
/// order (the access units it pertains to are the *target access unit
/// set*).
///
/// Syntax — §H.13.1.5:
///
/// ```text
/// depth_timing( payloadSize ) {
///     per_view_depth_timing_flag                 u(1)
///     if( per_view_depth_timing_flag )
///         for( i = 0; i < NumDepthViews; i++ )
///             depth_timing_offset( )
///     else
///         depth_timing_offset( )
/// }
/// ```
///
/// Semantics (§H.13.2.5):
///
/// * `per_view_depth_timing_flag == 0` — all the depth view
///   components within the target access unit set have the same
///   acquisition time offset; the single `depth_timing_offset()`
///   occurrence specifies it (stored as a single-entry `offsets`
///   vector).
/// * `per_view_depth_timing_flag == 1` — one `depth_timing_offset()`
///   per depth view, in ascending order of view order index values
///   for the depth views. The loop bound is the SPS-derived
///   `NumDepthViews` variable (§H.7.3.2.1.5), supplied through
///   [`SeiContext::num_depth_views`]; a message reaching this branch
///   under an unknown (0) value is rejected with
///   [`SeiError::DepthTimingNumDepthViewsUnknown`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DepthTiming {
    /// `per_view_depth_timing_flag` — u(1). When `true`, `offsets`
    /// carries one entry per depth view in ascending view order index
    /// order. When `false`, `offsets` carries a single shared entry.
    pub per_view_depth_timing_flag: bool,
    /// Either a single shared `depth_timing_offset()` entry
    /// (`per_view_depth_timing_flag == 0`, length 1) or one entry per
    /// depth view (`per_view_depth_timing_flag == 1`, length
    /// `NumDepthViews`).
    pub offsets: Vec<DepthTimingOffset>,
}

/// Parse a §H.13.1.5 `depth_timing()` payload (payload type 52).
///
/// The per-view loop bound is NOT in the payload — §H.13.1.5 loops
/// over the `NumDepthViews` variable accumulated from
/// `depth_view_present_flag[i]` in the active subset SPS MVCD
/// extension (§H.7.3.2.1.5), supplied here through
/// [`SeiContext::num_depth_views`]. Two gates run before the
/// per-view allocation:
///
/// * `ctx.num_depth_views == 0` (unknown / no Annex H subset SPS
///   active) — rejected with
///   [`SeiError::DepthTimingNumDepthViewsUnknown`], mirroring the
///   §D.1.10 spare_pic treatment of an unknown `PicSizeInMapUnits`.
/// * `ctx.num_depth_views > 1024` — rejected with
///   [`SeiError::DepthTimingNumDepthViewsOutOfRange`]; the
///   §H.7.3.2.1.5 view loop runs `num_views_minus1 + 1 ≤ 1024` times
///   and increments `NumDepthViews` at most once per iteration, so
///   1024 is the absolute ceiling. Pre-allocation gate in the same
///   anti-OOM spirit as the round-177 / round-200 ue(v) caps (here
///   the count is caller-supplied rather than bitstream-driven, but
///   the `Vec::with_capacity` cost is bounded the same way).
pub fn parse_depth_timing(payload: &[u8], ctx: &SeiContext) -> Result<DepthTiming, SeiError> {
    let mut r = BitReader::new(payload);

    // §H.13.1.5 — per_view_depth_timing_flag u(1).
    let per_view_depth_timing_flag = r.u(1)? == 1;

    let offsets = if per_view_depth_timing_flag {
        // §H.13.1.5 — for( i = 0; i < NumDepthViews; i++ )
        //                 depth_timing_offset( )
        if ctx.num_depth_views == 0 {
            return Err(SeiError::DepthTimingNumDepthViewsUnknown);
        }
        if ctx.num_depth_views > 1024 {
            return Err(SeiError::DepthTimingNumDepthViewsOutOfRange(
                ctx.num_depth_views,
            ));
        }
        let count = ctx.num_depth_views as usize;
        let mut offsets = Vec::with_capacity(count);
        for _ in 0..count {
            offsets.push(read_depth_timing_offset(&mut r)?);
        }
        offsets
    } else {
        // §H.13.1.5 else branch — single shared depth_timing_offset().
        vec![read_depth_timing_offset(&mut r)?]
    };

    Ok(DepthTiming {
        per_view_depth_timing_flag,
        offsets,
    })
}

/// Dispatch helper: given a payload_type and bytes, produce the typed
/// payload if it's one we recognise.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SeiPayload {
    BufferingPeriod(BufferingPeriod),
    PicTiming(PicTiming),
    FillerPayload,
    UserDataRegisteredItuTT35(UserDataRegisteredItuTT35),
    UserDataUnregistered(UserDataUnregistered),
    PanScanRect(PanScanRect),
    RecoveryPoint(RecoveryPoint),
    /// §D.2.9 — dec_ref_pic_marking_repetition (payload type 7). Round 110.
    DecRefPicMarkingRepetition(DecRefPicMarkingRepetition),
    /// §D.2.10 — spare_pic (payload type 8). Round 103.
    SparePic(SparePic),
    FullFrameFreeze(FullFrameFreeze),
    /// §D.2.16 — full_frame_freeze_release. Empty payload.
    FullFrameFreezeRelease,
    FullFrameSnapshot(FullFrameSnapshot),
    FilmGrainCharacteristics(FilmGrainCharacteristics),
    DeblockingFilterDisplayPreference(DeblockingFilterDisplayPreference),
    PostFilterHint(PostFilterHint),
    ToneMappingInfo(ToneMappingInfo),
    FramePackingArrangement(FramePackingArrangement),
    DisplayOrientation(DisplayOrientation),
    MasteringDisplay(MasteringDisplayColourVolume),
    ContentLightLevel(ContentLightLevelInfo),
    /// preferred_transfer_characteristics — single u(8) field.
    AlternativeTransferCharacteristics(u8),
    /// §D.2.11 — scene_info (payload type 9). Round 73.
    SceneInfo(SceneInfo),
    /// §D.2.11 (2003 draft) — sub_seq_info (payload type 10). Round 99.
    SubSeqInfo(SubSeqInfo),
    /// §D.2.12 (2003 draft) — sub_seq_layer_characteristics (payload type 11). Round 99.
    SubSeqLayerCharacteristics(SubSeqLayerCharacteristics),
    /// §D.2.13 (2003 draft) — sub_seq_characteristics (payload type 12). Round 99.
    SubSeqCharacteristics(SubSeqCharacteristics),
    /// §D.2.18 — progressive_refinement_segment_start (payload type 16). Round 73.
    ProgressiveRefinementSegmentStart(ProgressiveRefinementSegmentStart),
    /// §D.2.19 — progressive_refinement_segment_end (payload type 17). Round 73.
    ProgressiveRefinementSegmentEnd(ProgressiveRefinementSegmentEnd),
    /// §D.2.20 — motion_constrained_slice_group_set (payload type 18). Round 73.
    MotionConstrainedSliceGroupSet(MotionConstrainedSliceGroupSet),
    /// §D.2.23 — stereo_video_info (payload type 21). Round 73.
    StereoVideoInfo(StereoVideoInfo),
    /// §D.2.30 — colour_remapping_info (payload type 142). Round 117.
    ColourRemappingInfo(ColourRemappingInfo),
    /// §D.2.33 — content_colour_volume (payload type 149). Round 107.
    ContentColourVolume(ContentColourVolume),
    /// §D.2.34 — ambient_viewing_environment (payload type 148). Round 78.
    AmbientViewingEnvironment(AmbientViewingEnvironment),
    /// §D.2.35.1 — equirectangular_projection (payload type 150). Round 78.
    EquirectangularProjection(EquirectangularProjection),
    /// §D.2.35.2 — cubemap_projection (payload type 151). Round 78.
    CubemapProjection(CubemapProjection),
    /// §D.2.35.3 — sphere_rotation (payload type 154). Round 78.
    SphereRotation(SphereRotation),
    /// §D.2.35.4 — regionwise_packing (payload type 155). Round 113.
    RegionWisePacking(RegionWisePacking),
    /// §D.2.35.5 — omni_viewport (payload type 156). Round 95.
    OmniViewport(OmniViewport),
    /// §D.2.38 — shutter_interval_info (payload type 205). Round 95.
    ShutterIntervalInfo(ShutterIntervalInfo),
    /// §G.13.2.4 — multiview_scene_info (payload type 39, Annex G). Round 200.
    MultiviewSceneInfo(MultiviewSceneInfo),
    /// §G.13.2.5 — multiview_acquisition_info (payload type 40, Annex G). Round 213.
    MultiviewAcquisitionInfo(MultiviewAcquisitionInfo),
    /// §G.13.2.6 — non_required_view_component (payload type 41, Annex G). Round 207.
    NonRequiredViewComponent(NonRequiredViewComponent),
    /// §G.13.2.7 — view_dependency_change (payload type 42, Annex G / MVC). Round 334.
    ViewDependencyChange(ViewDependencyChange),
    /// §G.13.2.8 — operation_point_not_present (payload type 43, Annex G). Round 200.
    OperationPointNotPresent(OperationPointNotPresent),
    /// §G.13.2.9 — base_view_temporal_hrd (payload type 44, Annex G / MVC). Round 293.
    BaseViewTemporalHrd(BaseViewTemporalHrd),
    /// §G.13.2.10 — multiview_view_position (payload type 46). Round 183.
    MultiviewViewPosition(MultiviewViewPosition),
    /// §H.13.2.3 — depth_representation_info (payload type 50,
    /// Annex H). Round 231.
    DepthRepresentationInfo(DepthRepresentationInfo),
    /// §H.13.2.4 — three_dimensional_reference_displays_info
    /// (payload type 51, Annex H). Round 226.
    ThreeDimensionalReferenceDisplaysInfo(ThreeDimensionalReferenceDisplaysInfo),
    /// §H.13.2.5 — depth_timing (payload type 52, Annex H 3D-AVC).
    /// Round 278.
    DepthTiming(DepthTiming),
    /// §H.13.2.7 — depth_sampling_info (payload type 53, Annex H 3D-AVC).
    /// Round 247.
    DepthSamplingInfo(DepthSamplingInfo),
    /// §I.13.2.1 — constrained_depth_parameter_set_identifier (payload
    /// type 54, Annex I 3D-AVC depth coding). Round 237.
    ConstrainedDepthParameterSetIdentifier(ConstrainedDepthParameterSetIdentifier),
    /// §H.13.2.6 — alternative_depth_info (payload type 181, Annex H
    /// 3D-AVC). Round 318.
    AlternativeDepthInfo(AlternativeDepthInfo),
    /// §D.2.36 — sei_manifest (payload type 200). Round 120.
    SeiManifest(SeiManifest),
    /// §D.2.37 — sei_prefix_indication (payload type 201). Round 120.
    SeiPrefixIndication(SeiPrefixIndication),
    /// Not parsed — caller keeps the raw payload for later interpretation.
    Unknown {
        payload_type: u32,
        payload: Vec<u8>,
    },
}

/// §D.1.1 sei_payload() dispatch for the subset of payload types we
/// parse. Unknown types are returned as `SeiPayload::Unknown` with
/// their raw bytes preserved.
pub fn parse_payload(
    payload_type: u32,
    payload: &[u8],
    ctx: &SeiContext,
) -> Result<SeiPayload, SeiError> {
    match payload_type {
        0 => Ok(SeiPayload::BufferingPeriod(parse_buffering_period(
            payload, ctx,
        )?)),
        1 => Ok(SeiPayload::PicTiming(parse_pic_timing(payload, ctx)?)),
        2 => Ok(SeiPayload::PanScanRect(parse_pan_scan_rect(payload)?)),
        3 => {
            parse_filler_payload(payload)?;
            Ok(SeiPayload::FillerPayload)
        }
        4 => Ok(SeiPayload::UserDataRegisteredItuTT35(
            parse_user_data_registered_itu_t_t35(payload)?,
        )),
        5 => Ok(SeiPayload::UserDataUnregistered(
            parse_user_data_unregistered(payload)?,
        )),
        6 => Ok(SeiPayload::RecoveryPoint(parse_recovery_point(payload)?)),
        7 => Ok(SeiPayload::DecRefPicMarkingRepetition(
            parse_dec_ref_pic_marking_repetition(payload, ctx)?,
        )),
        8 => Ok(SeiPayload::SparePic(parse_spare_pic(payload, ctx)?)),
        9 => Ok(SeiPayload::SceneInfo(parse_scene_info(payload)?)),
        10 => Ok(SeiPayload::SubSeqInfo(parse_sub_seq_info(payload)?)),
        11 => Ok(SeiPayload::SubSeqLayerCharacteristics(
            parse_sub_seq_layer_characteristics(payload)?,
        )),
        12 => Ok(SeiPayload::SubSeqCharacteristics(
            parse_sub_seq_characteristics(payload)?,
        )),
        13 => Ok(SeiPayload::FullFrameFreeze(parse_full_frame_freeze(
            payload,
        )?)),
        14 => Ok(SeiPayload::FullFrameFreezeRelease),
        15 => Ok(SeiPayload::FullFrameSnapshot(parse_full_frame_snapshot(
            payload,
        )?)),
        16 => Ok(SeiPayload::ProgressiveRefinementSegmentStart(
            parse_progressive_refinement_segment_start(payload)?,
        )),
        17 => Ok(SeiPayload::ProgressiveRefinementSegmentEnd(
            parse_progressive_refinement_segment_end(payload)?,
        )),
        18 => Ok(SeiPayload::MotionConstrainedSliceGroupSet(
            parse_motion_constrained_slice_group_set(payload, ctx)?,
        )),
        19 => Ok(SeiPayload::FilmGrainCharacteristics(
            parse_film_grain_characteristics(payload)?,
        )),
        20 => Ok(SeiPayload::DeblockingFilterDisplayPreference(
            parse_deblocking_filter_display_preference(payload)?,
        )),
        21 => Ok(SeiPayload::StereoVideoInfo(parse_stereo_video_info(
            payload,
        )?)),
        22 => Ok(SeiPayload::PostFilterHint(parse_post_filter_hint(payload)?)),
        23 => Ok(SeiPayload::ToneMappingInfo(parse_tone_mapping_info(
            payload,
        )?)),
        39 => Ok(SeiPayload::MultiviewSceneInfo(parse_multiview_scene_info(
            payload,
        )?)),
        40 => Ok(SeiPayload::MultiviewAcquisitionInfo(
            parse_multiview_acquisition_info(payload)?,
        )),
        41 => Ok(SeiPayload::NonRequiredViewComponent(
            parse_non_required_view_component(payload)?,
        )),
        42 => Ok(SeiPayload::ViewDependencyChange(
            parse_view_dependency_change(payload, ctx)?,
        )),
        43 => Ok(SeiPayload::OperationPointNotPresent(
            parse_operation_point_not_present(payload)?,
        )),
        44 => Ok(SeiPayload::BaseViewTemporalHrd(
            parse_base_view_temporal_hrd(payload)?,
        )),
        45 => Ok(SeiPayload::FramePackingArrangement(
            parse_frame_packing_arrangement(payload)?,
        )),
        46 => Ok(SeiPayload::MultiviewViewPosition(
            parse_multiview_view_position(payload)?,
        )),
        47 => Ok(SeiPayload::DisplayOrientation(parse_display_orientation(
            payload,
        )?)),
        50 => Ok(SeiPayload::DepthRepresentationInfo(
            parse_depth_representation_info(payload)?,
        )),
        51 => Ok(SeiPayload::ThreeDimensionalReferenceDisplaysInfo(
            parse_three_dimensional_reference_displays_info(payload)?,
        )),
        52 => Ok(SeiPayload::DepthTiming(parse_depth_timing(payload, ctx)?)),
        53 => Ok(SeiPayload::DepthSamplingInfo(parse_depth_sampling_info(
            payload,
        )?)),
        54 => Ok(SeiPayload::ConstrainedDepthParameterSetIdentifier(
            parse_constrained_depth_parameter_set_identifier(payload)?,
        )),
        181 => Ok(SeiPayload::AlternativeDepthInfo(
            parse_alternative_depth_info(payload)?,
        )),
        137 => Ok(SeiPayload::MasteringDisplay(parse_mastering_display(
            payload,
        )?)),
        142 => Ok(SeiPayload::ColourRemappingInfo(
            parse_colour_remapping_info(payload)?,
        )),
        144 => Ok(SeiPayload::ContentLightLevel(parse_content_light_level(
            payload,
        )?)),
        147 => Ok(SeiPayload::AlternativeTransferCharacteristics(
            parse_alternative_transfer_characteristics(payload)?,
        )),
        148 => Ok(SeiPayload::AmbientViewingEnvironment(
            parse_ambient_viewing_environment(payload)?,
        )),
        149 => Ok(SeiPayload::ContentColourVolume(
            parse_content_colour_volume(payload)?,
        )),
        150 => Ok(SeiPayload::EquirectangularProjection(
            parse_equirectangular_projection(payload)?,
        )),
        151 => Ok(SeiPayload::CubemapProjection(parse_cubemap_projection(
            payload,
        )?)),
        154 => Ok(SeiPayload::SphereRotation(parse_sphere_rotation(payload)?)),
        155 => Ok(SeiPayload::RegionWisePacking(parse_regionwise_packing(
            payload,
        )?)),
        156 => Ok(SeiPayload::OmniViewport(parse_omni_viewport(payload)?)),
        200 => Ok(SeiPayload::SeiManifest(parse_sei_manifest(payload)?)),
        201 => Ok(SeiPayload::SeiPrefixIndication(
            parse_sei_prefix_indication(payload)?,
        )),
        205 => Ok(SeiPayload::ShutterIntervalInfo(
            parse_shutter_interval_info(payload)?,
        )),
        other => Ok(SeiPayload::Unknown {
            payload_type: other,
            payload: payload.to_vec(),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Helper: pack a sequence of (value, bit_width) fields MSB-first
    // into a byte vector, zero-padding the final byte.
    fn pack_bits(fields: &[(u64, u32)]) -> Vec<u8> {
        let mut bits: Vec<u8> = Vec::new();
        for &(value, width) in fields {
            for i in (0..width).rev() {
                bits.push(((value >> i) & 1) as u8);
            }
        }
        // Pad to a whole byte boundary.
        while bits.len() % 8 != 0 {
            bits.push(0);
        }
        bits.chunks(8)
            .map(|c| c.iter().fold(0u8, |acc, &b| (acc << 1) | b))
            .collect()
    }

    #[test]
    fn buffering_period_nal_only_single_sched() {
        // ue(0) → bit "1" encodes seq_parameter_set_id = 0.
        // initial_cpb_removal_delay_length_minus1 = 23 → 24-bit fields.
        // Pick delay = 0x012345, offset = 0x0ABCDE.
        // Bit layout: "1" (ue=0), then 24 bits delay, then 24 bits offset
        // → 1 + 24 + 24 = 49 bits, 7 bytes after padding.
        let fields = [(1, 1), (0x012345, 24), (0x0ABCDE, 24)];
        let payload = pack_bits(&fields);

        let ctx = SeiContext {
            initial_cpb_removal_delay_length_minus1: 23,
            nal_hrd_cpb_cnt_minus1: Some(0), // one SchedSelIdx
            ..Default::default()
        };

        let bp = parse_buffering_period(&payload, &ctx).unwrap();
        assert_eq!(bp.seq_parameter_set_id, 0);
        assert!(bp.vcl_hrd.is_none());
        let nal = bp.nal_hrd.expect("nal hrd present");
        assert_eq!(nal.len(), 1);
        assert_eq!(nal[0].initial_cpb_removal_delay, 0x012345);
        assert_eq!(nal[0].initial_cpb_removal_delay_offset, 0x0ABCDE);
    }

    #[test]
    fn pic_timing_with_delays_and_pic_struct_zero() {
        // CpbDpbDelaysPresentFlag=true, lengths=24 each.
        // cpb_delay=5, dpb_delay=10, pic_struct=0 (1 clockTimestamp).
        // clock_timestamp_flag=0 → empty slot.
        // Fields: cpb(24)=5, dpb(24)=10, pic_struct(4)=0, ct_flag(1)=0 = 53 bits.
        let fields = [(5, 24), (10, 24), (0, 4), (0, 1)];
        let payload = pack_bits(&fields);

        let ctx = SeiContext {
            cpb_removal_delay_length_minus1: 23,
            dpb_output_delay_length_minus1: 23,
            cpb_dpb_delays_present_flag: true,
            pic_struct_present_flag: true,
            time_offset_length: 24,
            ..Default::default()
        };
        let pt = parse_pic_timing(&payload, &ctx).unwrap();
        assert_eq!(pt.cpb_removal_delay, 5);
        assert_eq!(pt.dpb_output_delay, 10);
        let info = pt.pic_struct.expect("pic_struct present");
        assert_eq!(info.pic_struct, 0);
        assert_eq!(info.clock_timestamps.len(), 1);
        assert!(info.clock_timestamps[0].is_none());
    }

    #[test]
    fn pic_timing_without_delays_or_pic_struct() {
        // Both flags false → parser reads nothing; any payload accepted.
        let ctx = SeiContext::default();
        let pt = parse_pic_timing(&[], &ctx).unwrap();
        assert_eq!(pt.cpb_removal_delay, 0);
        assert_eq!(pt.dpb_output_delay, 0);
        assert!(pt.pic_struct.is_none());
    }

    #[test]
    fn recovery_point_basic() {
        // recovery_frame_cnt=8 → ue codeNum=8.
        // Exp-Golomb of 8: leadingZeros=3 ⇒ "0001001" (7 bits).
        // Then exact_match=1, broken_link=0, changing_slice_group_idc=0 (2 bits).
        // Total = 7 + 1 + 1 + 2 = 11 bits.
        //
        // ue(8) bit pattern: codeNum=8 → bit string "0001001".
        //   leadingZeros=3 zeros, a 1, then suffix = 8 - (2^3 - 1) = 1 in 3 bits → "001".
        //   → "000 1 001" = "0001001".
        let fields = [
            (0b0001001, 7), // ue=8
            (1, 1),         // exact_match_flag
            (0, 1),         // broken_link_flag
            (0, 2),         // changing_slice_group_idc
        ];
        let payload = pack_bits(&fields);
        let rp = parse_recovery_point(&payload).unwrap();
        assert_eq!(rp.recovery_frame_cnt, 8);
        assert!(rp.exact_match_flag);
        assert!(!rp.broken_link_flag);
        assert_eq!(rp.changing_slice_group_idc, 0);
    }

    #[test]
    fn user_data_unregistered_roundtrip() {
        let uuid: [u8; 16] = [
            0xDC, 0x45, 0xE9, 0xBD, 0xE6, 0xD9, 0x48, 0xB7, 0x96, 0x2C, 0xD8, 0x20, 0xD9, 0x23,
            0xEE, 0xEF,
        ];
        let mut payload = Vec::new();
        payload.extend_from_slice(&uuid);
        payload.extend_from_slice(b"hello");
        let udu = parse_user_data_unregistered(&payload).unwrap();
        assert_eq!(udu.uuid, uuid);
        assert_eq!(udu.user_data, b"hello");
    }

    #[test]
    fn user_data_unregistered_too_short() {
        let err = parse_user_data_unregistered(&[0u8; 15]).unwrap_err();
        assert_eq!(err, SeiError::UserDataTooShort(15));
    }

    #[test]
    fn filler_payload_all_ff() {
        parse_filler_payload(&[0xFF; 5]).unwrap();
    }

    #[test]
    fn filler_payload_bad_byte_reports_position() {
        let mut bytes = [0xFFu8; 5];
        bytes[2] = 0xFE;
        let err = parse_filler_payload(&bytes).unwrap_err();
        assert_eq!(err, SeiError::FillerNonFfAt(2));
    }

    #[test]
    fn mastering_display_decodes_fields() {
        // Use recognisable values:
        //   primaries: (10000,20000), (30000,40000), (45000,5000)
        //   white_point: (31270,32900) (D65-ish)
        //   max_lum: 10_000_000 (= 1000 cd/m^2), min_lum: 500 (= 0.05 cd/m^2)
        let mut p: Vec<u8> = Vec::new();
        fn push_u16(v: &mut Vec<u8>, x: u16) {
            v.push((x >> 8) as u8);
            v.push((x & 0xFF) as u8);
        }
        fn push_u32(v: &mut Vec<u8>, x: u32) {
            v.extend_from_slice(&x.to_be_bytes());
        }
        for &(x, y) in &[(10000u16, 20000u16), (30000, 40000), (45000, 5000)] {
            push_u16(&mut p, x);
            push_u16(&mut p, y);
        }
        push_u16(&mut p, 31270);
        push_u16(&mut p, 32900);
        push_u32(&mut p, 10_000_000);
        push_u32(&mut p, 500);

        let m = parse_mastering_display(&p).unwrap();
        assert_eq!(m.display_primaries[0], (10000, 20000));
        assert_eq!(m.display_primaries[1], (30000, 40000));
        assert_eq!(m.display_primaries[2], (45000, 5000));
        assert_eq!(m.white_point, (31270, 32900));
        assert_eq!(m.max_display_mastering_luminance, 10_000_000);
        assert_eq!(m.min_display_mastering_luminance, 500);
    }

    #[test]
    fn content_light_level_decodes_fields() {
        // max_content=1000, max_pic_avg=400.
        let payload = [0x03, 0xE8, 0x01, 0x90]; // 1000, 400
        let c = parse_content_light_level(&payload).unwrap();
        assert_eq!(c.max_content_light_level, 1000);
        assert_eq!(c.max_pic_average_light_level, 400);
    }

    #[test]
    fn parse_payload_dispatches_unknown() {
        let ctx = SeiContext::default();
        let raw = vec![0x01, 0x02, 0x03];
        let got = parse_payload(999, &raw, &ctx).unwrap();
        match got {
            SeiPayload::Unknown {
                payload_type,
                payload,
            } => {
                assert_eq!(payload_type, 999);
                assert_eq!(payload, raw);
            }
            other => panic!("expected Unknown, got {:?}", other),
        }
    }

    #[test]
    fn parse_payload_dispatches_known_content_light_level() {
        let ctx = SeiContext::default();
        let payload = [0x03, 0xE8, 0x01, 0x90];
        let got = parse_payload(144, &payload, &ctx).unwrap();
        match got {
            SeiPayload::ContentLightLevel(c) => {
                assert_eq!(c.max_content_light_level, 1000);
                assert_eq!(c.max_pic_average_light_level, 400);
            }
            other => panic!("expected ContentLightLevel, got {:?}", other),
        }
    }

    #[test]
    fn parse_payload_dispatches_filler() {
        let ctx = SeiContext::default();
        let got = parse_payload(3, &[0xFF; 4], &ctx).unwrap();
        assert_eq!(got, SeiPayload::FillerPayload);
    }

    // §D.2.6 — user_data_registered_itu_t_t35.
    //
    // Country code 0xB5 (USA) is the headline real-world case: A/53
    // closed-caption SEI uses (country_code=0xB5, provider_code=0x0031,
    // provider_user_id="GA94"); HDR10+ uses (0xB5, 0x003C); Dolby
    // Vision uses (country_code=0x4D, no extension).
    #[test]
    fn registered_user_data_us_country_no_extension() {
        // 0xB5 (USA) + payload {0x00, 0x31, b"GA94"}
        let payload = [0xB5, 0x00, 0x31, b'G', b'A', b'9', b'4'];
        let got = parse_user_data_registered_itu_t_t35(&payload).unwrap();
        assert_eq!(got.country_code, 0xB5);
        assert_eq!(got.country_code_extension, None);
        assert_eq!(got.payload_bytes, &[0x00, 0x31, b'G', b'A', b'9', b'4']);
    }

    #[test]
    fn registered_user_data_escape_country_takes_extension_byte() {
        // 0xFF (escape) + extension 0x42 + payload {0x00, 0x01}
        let payload = [0xFF, 0x42, 0x00, 0x01];
        let got = parse_user_data_registered_itu_t_t35(&payload).unwrap();
        assert_eq!(got.country_code, 0xFF);
        assert_eq!(got.country_code_extension, Some(0x42));
        assert_eq!(got.payload_bytes, &[0x00, 0x01]);
    }

    #[test]
    fn registered_user_data_empty_payload_after_country_code_is_ok() {
        let payload = [0xB5];
        let got = parse_user_data_registered_itu_t_t35(&payload).unwrap();
        assert_eq!(got.country_code, 0xB5);
        assert_eq!(got.country_code_extension, None);
        assert!(got.payload_bytes.is_empty());
    }

    #[test]
    fn registered_user_data_empty_payload_rejected() {
        let err = parse_user_data_registered_itu_t_t35(&[]).unwrap_err();
        assert_eq!(err, SeiError::RegisteredUserDataMissingCountryCode);
    }

    #[test]
    fn registered_user_data_escape_without_extension_rejected() {
        // country_code=0xFF requires the extension byte to follow.
        let err = parse_user_data_registered_itu_t_t35(&[0xFF]).unwrap_err();
        assert_eq!(err, SeiError::RegisteredUserDataMissingCountryCode);
    }

    #[test]
    fn parse_payload_dispatches_registered_user_data() {
        let ctx = SeiContext::default();
        let payload = [0xB5, 0x00, 0x31, b'G', b'A', b'9', b'4'];
        let got = parse_payload(4, &payload, &ctx).unwrap();
        match got {
            SeiPayload::UserDataRegisteredItuTT35(u) => {
                assert_eq!(u.country_code, 0xB5);
                assert_eq!(u.payload_bytes, &[0x00, 0x31, b'G', b'A', b'9', b'4']);
            }
            other => panic!("expected UserDataRegisteredItuTT35, got {:?}", other),
        }
    }

    // §D.2.4 — pan_scan_rect.
    #[test]
    fn pan_scan_rect_cancel_flag_only() {
        // id = 0 → ue "1"; cancel_flag = 1 → 1 bit. Total 2 bits.
        let payload = pack_bits(&[(1, 1), (1, 1)]);
        let got = parse_pan_scan_rect(&payload).unwrap();
        assert_eq!(got.pan_scan_rect_id, 0);
        assert!(got.cancel_flag);
        assert!(got.body.is_none());
    }

    #[test]
    fn pan_scan_rect_single_rectangle_with_offsets() {
        // id = 0 (ue "1"), cancel_flag = 0, cnt_minus1 = 0 (ue "1"),
        // four se() offsets (left=2, right=-2, top=4, bottom=-4),
        // repetition_period = 1 (ue "010").
        //
        // se(2)  → ue(3) → 5 bits "00100"
        // se(-2) → ue(4) → 5 bits "00101"
        // se(4)  → ue(7) → 7 bits "0001000"
        // se(-4) → ue(8) → 7 bits "0001001"
        // ue(1)  → 3 bits "010"
        let fields = [
            (1, 1), // id = 0
            (0, 1), // cancel = 0
            (1, 1), // cnt_minus1 = 0
            (0b00100, 5),
            (0b00101, 5),
            (0b0001000, 7),
            (0b0001001, 7),
            (0b010, 3),
        ];
        let payload = pack_bits(&fields);
        let got = parse_pan_scan_rect(&payload).unwrap();
        assert_eq!(got.pan_scan_rect_id, 0);
        assert!(!got.cancel_flag);
        let body = got.body.expect("body present when cancel_flag = 0");
        assert_eq!(body.rects.len(), 1);
        assert_eq!(body.rects[0].left_offset, 2);
        assert_eq!(body.rects[0].right_offset, -2);
        assert_eq!(body.rects[0].top_offset, 4);
        assert_eq!(body.rects[0].bottom_offset, -4);
        assert_eq!(body.repetition_period, 1);
    }

    #[test]
    fn pan_scan_rect_pathological_count_rejected() {
        // id = 0 (ue "1"), cancel = 0, cnt_minus1 = 32 (ue "0000010_0001",
        // 11 bits) — beyond our 32-rectangle ceiling.
        let fields = [
            (1, 1),
            (0, 1),
            (0b00000_100001, 11), // ue(32) = 11 bits "00000100001"
        ];
        let payload = pack_bits(&fields);
        let err = parse_pan_scan_rect(&payload).unwrap_err();
        assert_eq!(err, SeiError::PanScanCountTooLarge(32));
    }

    #[test]
    fn parse_payload_dispatches_pan_scan_rect() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(1, 1), (1, 1)]); // cancel-flag only
        let got = parse_payload(2, &payload, &ctx).unwrap();
        match got {
            SeiPayload::PanScanRect(p) => {
                assert_eq!(p.pan_scan_rect_id, 0);
                assert!(p.cancel_flag);
                assert!(p.body.is_none());
            }
            other => panic!("expected PanScanRect, got {:?}", other),
        }
    }

    #[test]
    fn num_clock_ts_table_d1() {
        // Per Table D-1.
        assert_eq!(num_clock_ts_from_pic_struct(0), 1);
        assert_eq!(num_clock_ts_from_pic_struct(1), 1);
        assert_eq!(num_clock_ts_from_pic_struct(2), 1);
        assert_eq!(num_clock_ts_from_pic_struct(3), 2);
        assert_eq!(num_clock_ts_from_pic_struct(4), 2);
        assert_eq!(num_clock_ts_from_pic_struct(5), 3);
        assert_eq!(num_clock_ts_from_pic_struct(6), 3);
        assert_eq!(num_clock_ts_from_pic_struct(7), 2);
        assert_eq!(num_clock_ts_from_pic_struct(8), 3);
        assert_eq!(num_clock_ts_from_pic_struct(9), 0); // reserved
    }

    // §D.2.26 — frame_packing_arrangement. Build a payload that
    // signals side-by-side (type 3), non-quincunx, so the four grid
    // nibbles are present.
    //
    // Field layout (bits):
    //   id = 0 → ue "1"                              (1)
    //   cancel_flag = 0                              (1)
    //   type = 3                                     (7)
    //   quincunx_sampling_flag = 0                   (1)
    //   content_interpretation_type = 1              (6)
    //   spatial_flipping_flag = 0                    (1)
    //   frame0_flipped_flag = 0                      (1)
    //   field_views_flag = 0                         (1)
    //   current_frame_is_frame0_flag = 1             (1)
    //   frame0_self_contained_flag = 1               (1)
    //   frame1_self_contained_flag = 0               (1)
    //   frame0_grid_position_x = 0                   (4)
    //   frame0_grid_position_y = 0                   (4)
    //   frame1_grid_position_x = 0                   (4)
    //   frame1_grid_position_y = 0                   (4)
    //   reserved_byte = 0                            (8)
    //   repetition_period = 0 → ue "1"               (1)
    //   extension_flag = 0                           (1)
    #[test]
    fn frame_packing_arrangement_side_by_side() {
        let fields = [
            (1, 1), // ue id=0
            (0, 1), // cancel_flag
            (3, 7), // type=3 (side-by-side)
            (0, 1), // quincunx_sampling_flag
            (1, 6), // content_interpretation_type
            (0, 1), // spatial_flipping_flag
            (0, 1), // frame0_flipped_flag
            (0, 1), // field_views_flag
            (1, 1), // current_frame_is_frame0_flag
            (1, 1), // frame0_self_contained_flag
            (0, 1), // frame1_self_contained_flag
            (0, 4), // frame0_grid_x
            (0, 4), // frame0_grid_y
            (0, 4), // frame1_grid_x
            (0, 4), // frame1_grid_y
            (0, 8), // reserved_byte
            (1, 1), // ue repetition_period=0
            (0, 1), // extension_flag
        ];
        let payload = pack_bits(&fields);
        let fpa = parse_frame_packing_arrangement(&payload).unwrap();
        assert_eq!(fpa.id, 0);
        assert!(!fpa.cancel_flag);
        assert_eq!(fpa.ty, 3);
        assert!(!fpa.quincunx_sampling_flag);
        assert_eq!(fpa.content_interpretation_type, 1);
        assert!(fpa.current_frame_is_frame0_flag);
        assert!(fpa.frame0_self_contained_flag);
        assert!(!fpa.frame1_self_contained_flag);
        assert_eq!(fpa.frame0_grid_position_x, 0);
        assert_eq!(fpa.frame1_grid_position_y, 0);
        assert_eq!(fpa.repetition_period, 0);
        assert!(!fpa.extension_flag);
    }

    // §D.2.26 — cancel_flag path skips the body but still reads
    // extension_flag at the end.
    #[test]
    fn frame_packing_arrangement_cancel_path() {
        let fields = [
            (1, 1), // ue id=0
            (1, 1), // cancel_flag = 1
            (1, 1), // extension_flag = 1
        ];
        let payload = pack_bits(&fields);
        let fpa = parse_frame_packing_arrangement(&payload).unwrap();
        assert!(fpa.cancel_flag);
        assert!(fpa.extension_flag);
        // Body fields default to zero when cancel_flag is set.
        assert_eq!(fpa.ty, 0);
        assert_eq!(fpa.repetition_period, 0);
    }

    // §D.2.26 — type 5 (temporal interleaving) and quincunx=1 cases
    // both skip the grid positions per the syntax.
    #[test]
    fn frame_packing_arrangement_type5_skips_grid() {
        let fields = [
            (1, 1), // ue id=0
            (0, 1), // cancel_flag
            (5, 7), // type=5 (temporal interleaving)
            (0, 1), // quincunx
            (0, 6), // content_interp
            (0, 1), // spatial_flip
            (0, 1), // f0_flipped
            (0, 1), // field_views
            (0, 1), // current_is_f0
            (0, 1), // f0_self_contained
            (0, 1), // f1_self_contained
            // no grid positions because type == 5
            (0xAB, 8), // reserved_byte
            (1, 1),    // ue repetition=0
            (0, 1),    // extension_flag
        ];
        let payload = pack_bits(&fields);
        let fpa = parse_frame_packing_arrangement(&payload).unwrap();
        assert_eq!(fpa.ty, 5);
        assert_eq!(fpa.reserved_byte, 0xAB);
    }

    // §D.2.27 — display_orientation. Full body: cancel=0, then a
    // 180-degree rotation (anticlockwise_rotation = 0x8000 =
    // 2^15 units of 360/2^16 degrees).
    #[test]
    fn display_orientation_full_body() {
        let fields = [
            (0, 1),       // cancel_flag = 0
            (1, 1),       // hor_flip
            (0, 1),       // ver_flip
            (0x8000, 16), // anticlockwise_rotation
            (1, 1),       // ue repetition_period = 0
            (1, 1),       // extension_flag = 1
        ];
        let payload = pack_bits(&fields);
        let dor = parse_display_orientation(&payload).unwrap();
        assert!(!dor.cancel_flag);
        assert!(dor.hor_flip);
        assert!(!dor.ver_flip);
        assert_eq!(dor.anticlockwise_rotation, 0x8000);
        assert_eq!(dor.repetition_period, 0);
        assert!(dor.extension_flag);
    }

    // §D.2.27 — cancel_flag path reads nothing else.
    #[test]
    fn display_orientation_cancel_path() {
        let payload = pack_bits(&[(1, 1)]);
        let dor = parse_display_orientation(&payload).unwrap();
        assert!(dor.cancel_flag);
        assert!(!dor.hor_flip);
        assert_eq!(dor.anticlockwise_rotation, 0);
    }

    // §D.2.32 — single u(8).
    #[test]
    fn alternative_transfer_characteristics_pq() {
        // preferred_transfer_characteristics = 16 (PQ / SMPTE ST 2084).
        let payload = [16u8];
        let got = parse_alternative_transfer_characteristics(&payload).unwrap();
        assert_eq!(got, 16);
    }

    // §D.2.25 — tone_mapping_info with model_id = 0 (linear).
    //
    // tone_map_id = 0 → "1"
    // cancel_flag = 0
    // repetition_period = 0 → "1"
    // coded_data_bit_depth = 10
    // target_bit_depth = 8
    // model_id = 0 → "1"
    // min_value = 0x0000_0100 (u32)
    // max_value = 0x0000_03FF (u32)
    #[test]
    fn tone_mapping_info_linear() {
        let fields: [(u64, u32); 8] = [
            (1, 1),            // ue tone_map_id=0
            (0, 1),            // cancel_flag=0
            (1, 1),            // ue repetition_period=0
            (10, 8),           // coded_data_bit_depth
            (8, 8),            // target_bit_depth
            (1, 1),            // ue model_id=0
            (0x0000_0100, 32), // min_value
            (0x0000_03FF, 32), // max_value
        ];
        let packed = pack_bits(&fields);
        let tmi = parse_tone_mapping_info(&packed).unwrap();
        assert_eq!(tmi.tone_map_id, 0);
        assert!(!tmi.cancel_flag);
        let body = tmi.body.as_ref().expect("body present");
        assert_eq!(body.repetition_period, 0);
        assert_eq!(body.coded_data_bit_depth, 10);
        assert_eq!(body.target_bit_depth, 8);
        assert_eq!(body.model_id, 0);
        match &body.model {
            ToneMappingModel::Linear {
                min_value,
                max_value,
            } => {
                assert_eq!(*min_value, 0x0000_0100);
                assert_eq!(*max_value, 0x0000_03FF);
            }
            other => panic!("expected Linear, got {:?}", other),
        }
    }

    // §D.2.25 — tone_mapping_info cancel_flag path: nothing after
    // the cancel bit is read.
    #[test]
    fn tone_mapping_info_cancel_path() {
        // tone_map_id = 7 → ue codeword "0001000" (7 bits).
        // cancel_flag = 1.
        let fields = [
            (0b0001000, 7), // ue(7)
            (1, 1),         // cancel_flag
        ];
        let payload = pack_bits(&fields);
        let tmi = parse_tone_mapping_info(&payload).unwrap();
        assert_eq!(tmi.tone_map_id, 7);
        assert!(tmi.cancel_flag);
        assert!(tmi.body.is_none());
    }

    // §D.2.25 — the implicit-end-point accessors only fire for
    // tone_map_model_id == 3. For the linear (model_id 0) body
    // exercised by `tone_mapping_info_linear`, both
    // `piecewise_default_end_points` and `piecewise_total_pivot_count`
    // return None.
    #[test]
    fn tone_mapping_piecewise_accessors_none_for_linear() {
        let fields: [(u64, u32); 8] = [
            (1, 1),            // ue tone_map_id=0
            (0, 1),            // cancel_flag=0
            (1, 1),            // ue repetition_period=0
            (10, 8),           // coded_data_bit_depth
            (8, 8),            // target_bit_depth
            (1, 1),            // ue model_id=0 (linear)
            (0x0000_0100, 32), // min_value
            (0x0000_03FF, 32), // max_value
        ];
        let packed = pack_bits(&fields);
        let body = parse_tone_mapping_info(&packed).unwrap().body.unwrap();
        assert_eq!(body.model_id, 0);
        assert!(body.piecewise_default_end_points().is_none());
        assert!(body.piecewise_total_pivot_count().is_none());
    }

    // §D.2.25 — piecewise-linear (model_id 3) implicit default end
    // points. num_pivots = 2 interior pivots; coded_data_bit_depth =
    // 10, target_bit_depth = 8. The two implicit end points are
    // (0, 0) and (2^10 − 1, 2^8 − 1) = (1023, 255). The assembled
    // curve therefore has num_pivots + 2 = 4 points.
    //
    // coded_pivot_value width = ((10 + 7) >> 3) << 3 = 16 bits.
    // target_pivot_value width = ((8 + 7) >> 3) << 3 = 8 bits.
    #[test]
    fn tone_mapping_piecewise_default_end_points_and_count() {
        let fields: [(u64, u32); 11] = [
            (1, 1),       // ue tone_map_id=0
            (0, 1),       // cancel_flag=0
            (1, 1),       // ue repetition_period=0
            (10, 8),      // coded_data_bit_depth = 10
            (8, 8),       // target_bit_depth = 8
            (0b00100, 5), // ue model_id=3 (5-bit Exp-Golomb codeword)
            (2, 16),      // num_pivots = 2
            (300, 16),    // coded_pivot_value[0] (16-bit)
            (100, 8),     // target_pivot_value[0] (8-bit)
            (700, 16),    // coded_pivot_value[1] (16-bit)
            (200, 8),     // target_pivot_value[1] (8-bit)
        ];
        let packed = pack_bits(&fields);
        let body = parse_tone_mapping_info(&packed).unwrap().body.unwrap();
        assert_eq!(body.model_id, 3);
        match &body.model {
            ToneMappingModel::PiecewisePivots {
                num_pivots,
                coded_pivot_value,
                target_pivot_value,
            } => {
                assert_eq!(*num_pivots, 2);
                assert_eq!(coded_pivot_value, &vec![300, 700]);
                assert_eq!(target_pivot_value, &vec![100, 200]);
            }
            other => panic!("expected PiecewisePivots, got {:?}", other),
        }

        // Implicit end points: start (0, 0) and end (1023, 255).
        let (start, end) = body.piecewise_default_end_points().unwrap();
        assert_eq!(start, (0, 0));
        assert_eq!(end, (1023, 255));

        // Total = interior num_pivots (2) + 2 default end points = 4.
        assert_eq!(body.piecewise_total_pivot_count(), Some(4));
    }

    // §D.2.25 — the implicit end value tracks coded_data_bit_depth /
    // target_bit_depth independently. Here coded = 14 (max legal),
    // target = 16 (max legal): end = (2^14 − 1, 2^16 − 1) =
    // (16383, 65535) with zero interior pivots → total count 2.
    //
    // coded_pivot_value width = ((14 + 7) >> 3) << 3 = 16.
    // target_pivot_value width = ((16 + 7) >> 3) << 3 = 16.
    #[test]
    fn tone_mapping_piecewise_max_bit_depths_zero_pivots() {
        let fields = vec![
            (1u64, 1u32), // ue tone_map_id=0
            (0, 1),       // cancel_flag=0
            (1, 1),       // ue repetition_period=0
            (14, 8),      // coded_data_bit_depth = 14
            (16, 8),      // target_bit_depth = 16
            (0b00100, 5), // ue model_id = 3
            (0, 16),      // num_pivots = 0
        ];
        let packed = pack_bits(&fields);
        let body = parse_tone_mapping_info(&packed).unwrap().body.unwrap();
        assert_eq!(body.model_id, 3);
        let (start, end) = body.piecewise_default_end_points().unwrap();
        assert_eq!(start, (0, 0));
        assert_eq!(end, (16_383, 65_535));
        assert_eq!(body.piecewise_total_pivot_count(), Some(2));
    }

    // §D.2.21 — film_grain_characteristics.
    //
    // cancel_flag = 0
    // model_id = 1
    // separate_colour_description_present_flag = 0
    // blending_mode_id = 0
    // log2_scale_factor = 4
    // comp_model_present_flag = [true, false, false]
    // luma component:
    //   num_intensity_intervals_minus1 = 0  → 1 interval
    //   num_model_values_minus1 = 0         → 1 model value per interval
    //   interval[0]: lower=16, upper=235, comp_model_value[0]=0 (se "1")
    // repetition_period = 0 (ue "1")
    #[test]
    fn film_grain_characteristics_basic() {
        let fields = [
            (0, 1), // cancel_flag
            (1, 2), // model_id
            (0, 1), // separate_colour_description_present_flag
            (0, 2), // blending_mode_id
            (4, 4), // log2_scale_factor
            (1, 1), // comp_model_present_flag[0]
            (0, 1), // comp_model_present_flag[1]
            (0, 1), // comp_model_present_flag[2]
            // c=0 body
            (0, 8),   // num_intensity_intervals_minus1
            (0, 3),   // num_model_values_minus1
            (16, 8),  // interval[0].lower_bound
            (235, 8), // interval[0].upper_bound
            (1, 1),   // comp_model_value[0][0] = se(0) = 0
            (1, 1),   // repetition_period = ue(0) = 0
        ];
        let payload = pack_bits(&fields);
        let fg = parse_film_grain_characteristics(&payload).unwrap();
        assert!(!fg.cancel_flag);
        let body = fg.body.as_ref().expect("body present");
        assert_eq!(body.model_id, 1);
        assert!(!body.separate_colour_description_present_flag);
        assert!(body.separate_colour_description.is_none());
        assert_eq!(body.blending_mode_id, 0);
        assert_eq!(body.log2_scale_factor, 4);
        assert_eq!(body.comp_model_present_flag, [true, false, false]);
        assert_eq!(body.repetition_period, 0);

        let luma = body.components[0].as_ref().expect("luma component present");
        assert_eq!(luma.num_model_values_minus1, 0);
        assert_eq!(luma.intensity_intervals.len(), 1);
        assert_eq!(luma.intensity_intervals[0].lower_bound, 16);
        assert_eq!(luma.intensity_intervals[0].upper_bound, 235);
        assert_eq!(luma.intensity_intervals[0].model_values, vec![0]);

        assert!(body.components[1].is_none());
        assert!(body.components[2].is_none());
    }

    // §D.2.21 — cancel flag leaves body absent.
    #[test]
    fn film_grain_characteristics_cancel_path() {
        let payload = pack_bits(&[(1, 1)]);
        let fg = parse_film_grain_characteristics(&payload).unwrap();
        assert!(fg.cancel_flag);
        assert!(fg.body.is_none());
    }

    // §D.2.21 — with separate colour description the inner block is
    // populated.
    //
    // Both luma and chroma flagged. Each component carries 1 interval
    // with 1 model value. repetition_period = 0.
    #[test]
    fn film_grain_characteristics_with_separate_colour() {
        let fields = [
            (0, 1),  // cancel_flag
            (2, 2),  // model_id
            (1, 1),  // separate_colour_description_present_flag
            (2, 3),  // bit_depth_luma_minus8 (= 10 bit luma)
            (2, 3),  // bit_depth_chroma_minus8
            (1, 1),  // full_range_flag
            (1, 8),  // colour_primaries = 1 (BT.709)
            (13, 8), // transfer_characteristics = 13
            (1, 8),  // matrix_coefficients = 1
            (0, 2),  // blending_mode_id
            (3, 4),  // log2_scale_factor
            (1, 1),  // comp_model_present_flag[0]
            (1, 1),  // comp_model_present_flag[1]
            (0, 1),  // comp_model_present_flag[2]
            // c=0 body
            (0, 8),   // num_intensity_intervals_minus1
            (0, 3),   // num_model_values_minus1
            (0, 8),   // interval[0].lower_bound
            (255, 8), // interval[0].upper_bound
            (1, 1),   // comp_model_value[0][0] = se(0) = 0
            // c=1 body
            (0, 8),   // num_intensity_intervals_minus1
            (0, 3),   // num_model_values_minus1
            (0, 8),   // interval[0].lower_bound
            (255, 8), // interval[0].upper_bound
            (1, 1),   // comp_model_value[0][0] = se(0) = 0
            (1, 1),   // repetition_period = ue(0) = 0
        ];
        let payload = pack_bits(&fields);
        let fg = parse_film_grain_characteristics(&payload).unwrap();
        let body = fg.body.expect("body present");
        assert_eq!(body.model_id, 2);
        let scd = body.separate_colour_description.expect("scd present");
        assert_eq!(scd.bit_depth_luma_minus8, 2);
        assert_eq!(scd.bit_depth_chroma_minus8, 2);
        assert!(scd.full_range_flag);
        assert_eq!(scd.colour_primaries, 1);
        assert_eq!(scd.transfer_characteristics, 13);
        assert_eq!(scd.matrix_coefficients, 1);
        assert_eq!(body.blending_mode_id, 0);
        assert_eq!(body.log2_scale_factor, 3);
        assert_eq!(body.comp_model_present_flag, [true, true, false]);
        assert!(body.components[0].is_some());
        assert!(body.components[1].is_some());
        assert!(body.components[2].is_none());
        assert_eq!(body.repetition_period, 0);
    }

    // §D.2.21 eq. D-14 / D-15 — typed bit-depth accessors on the
    // separate-colour-description body. The on-wire `_minus8` carriers in
    // the round-trip fixture above are both 2 (10-bit luma + chroma), so
    // the accessors must surface filmGrainBitDepth = 10.
    #[test]
    fn film_grain_bit_depth_typical_10bit() {
        let scd = FilmGrainSeparateColourDescription {
            bit_depth_luma_minus8: 2,
            bit_depth_chroma_minus8: 2,
            full_range_flag: false,
            colour_primaries: 1,
            transfer_characteristics: 13,
            matrix_coefficients: 1,
        };
        // eq. D-14: filmGrainBitDepth[0] = 2 + 8 = 10.
        assert_eq!(scd.bit_depth_luma(), 10);
        // eq. D-15: filmGrainBitDepth[1] = filmGrainBitDepth[2] = 2 + 8 = 10.
        assert_eq!(scd.bit_depth_chroma(), 10);
    }

    // §D.2.21 — minimum on-wire carrier (0 ⇒ 8-bit), the most common case.
    #[test]
    fn film_grain_bit_depth_minimum_8bit() {
        let scd = FilmGrainSeparateColourDescription {
            bit_depth_luma_minus8: 0,
            bit_depth_chroma_minus8: 0,
            full_range_flag: false,
            colour_primaries: 2,
            transfer_characteristics: 2,
            matrix_coefficients: 2,
        };
        assert_eq!(scd.bit_depth_luma(), 8);
        assert_eq!(scd.bit_depth_chroma(), 8);
    }

    // §D.2.21 — u(3) ceiling (7 ⇒ 15-bit), and an asymmetric luma/chroma
    // pair to prove the two accessors read independent carriers.
    #[test]
    fn film_grain_bit_depth_u3_ceiling_and_asymmetric() {
        let scd = FilmGrainSeparateColourDescription {
            bit_depth_luma_minus8: 7,
            bit_depth_chroma_minus8: 4,
            full_range_flag: true,
            colour_primaries: 1,
            transfer_characteristics: 1,
            matrix_coefficients: 1,
        };
        // u(3) max carrier 7 ⇒ 15.
        assert_eq!(scd.bit_depth_luma(), 15);
        // Distinct chroma carrier 4 ⇒ 12 (not coupled to luma).
        assert_eq!(scd.bit_depth_chroma(), 12);
    }

    // §D.2.21 — end-to-end through the parser: the round-trip fixture's
    // separate-colour body carries 2 / 2, and the accessors must fire on
    // the parsed struct (not just a hand-built one).
    #[test]
    fn film_grain_bit_depth_parse_round_trip() {
        let fields = [
            (0, 1),  // cancel_flag
            (2, 2),  // model_id
            (1, 1),  // separate_colour_description_present_flag
            (2, 3),  // bit_depth_luma_minus8 (= 10-bit luma)
            (2, 3),  // bit_depth_chroma_minus8 (= 10-bit chroma)
            (1, 1),  // full_range_flag
            (1, 8),  // colour_primaries
            (13, 8), // transfer_characteristics
            (1, 8),  // matrix_coefficients
            (0, 2),  // blending_mode_id
            (3, 4),  // log2_scale_factor
            (1, 1),  // comp_model_present_flag[0]
            (1, 1),  // comp_model_present_flag[1]
            (0, 1),  // comp_model_present_flag[2]
            // c=0 body
            (0, 8),   // num_intensity_intervals_minus1
            (0, 3),   // num_model_values_minus1
            (0, 8),   // interval[0].lower_bound
            (255, 8), // interval[0].upper_bound
            (1, 1),   // comp_model_value[0][0] = se(0)
            // c=1 body
            (0, 8),   // num_intensity_intervals_minus1
            (0, 3),   // num_model_values_minus1
            (0, 8),   // interval[0].lower_bound
            (255, 8), // interval[0].upper_bound
            (1, 1),   // comp_model_value[0][0] = se(0)
            (1, 1),   // repetition_period = ue(0) = 0
        ];
        let payload = pack_bits(&fields);
        let fg = parse_film_grain_characteristics(&payload).unwrap();
        let scd = fg
            .body
            .expect("body present")
            .separate_colour_description
            .expect("scd present");
        assert_eq!(scd.bit_depth_luma_minus8, 2);
        assert_eq!(scd.bit_depth_chroma_minus8, 2);
        assert_eq!(scd.bit_depth_luma(), 10);
        assert_eq!(scd.bit_depth_chroma(), 10);
    }

    // §D.2.21 — multi-interval / multi-model-value path with non-trivial
    // se(v) values exercising the full per-c body. Three intervals, two
    // model values per interval; values include +/- non-zero entries.
    //
    // se codewords:
    //   se(0)  = ue(0) = "1"        (1 bit)
    //   se(+1) = ue(1) = "010"      (3 bits)
    //   se(-1) = ue(2) = "011"      (3 bits)
    //   se(+2) = ue(3) = "00100"    (5 bits)
    //   se(-2) = ue(4) = "00101"    (5 bits)
    #[test]
    fn film_grain_characteristics_multi_interval_multi_value() {
        let fields = [
            (0, 1), // cancel_flag
            (1, 2), // model_id (auto-regression)
            (0, 1), // separate_colour_description_present_flag
            (0, 2), // blending_mode_id
            (5, 4), // log2_scale_factor
            (0, 1), // comp_model_present_flag[0] = false
            (1, 1), // comp_model_present_flag[1] = true
            (0, 1), // comp_model_present_flag[2] = false
            // c=1 body
            (2, 8), // num_intensity_intervals_minus1 = 2 → 3 intervals
            (1, 3), // num_model_values_minus1 = 1 → 2 model values per interval
            // interval 0
            (0, 8),     // lower
            (84, 8),    // upper
            (0b010, 3), // se(+1)
            (0b011, 3), // se(-1)
            // interval 1
            (85, 8),
            (170, 8),
            (0b00100, 5), // se(+2)
            (1, 1),       // se(0)
            // interval 2
            (171, 8),
            (255, 8),
            (1, 1),       // se(0)
            (0b00101, 5), // se(-2)
            (0b010, 3),   // repetition_period = ue(1) = 1
        ];
        let payload = pack_bits(&fields);
        let fg = parse_film_grain_characteristics(&payload).unwrap();
        let body = fg.body.expect("body present");
        assert_eq!(body.repetition_period, 1);
        assert!(body.components[0].is_none());
        assert!(body.components[2].is_none());
        let chroma = body.components[1].as_ref().expect("Cb component present");
        assert_eq!(chroma.num_model_values_minus1, 1);
        assert_eq!(chroma.intensity_intervals.len(), 3);
        assert_eq!(chroma.intensity_intervals[0].lower_bound, 0);
        assert_eq!(chroma.intensity_intervals[0].upper_bound, 84);
        assert_eq!(chroma.intensity_intervals[0].model_values, vec![1, -1]);
        assert_eq!(chroma.intensity_intervals[1].lower_bound, 85);
        assert_eq!(chroma.intensity_intervals[1].upper_bound, 170);
        assert_eq!(chroma.intensity_intervals[1].model_values, vec![2, 0]);
        assert_eq!(chroma.intensity_intervals[2].lower_bound, 171);
        assert_eq!(chroma.intensity_intervals[2].upper_bound, 255);
        assert_eq!(chroma.intensity_intervals[2].model_values, vec![0, -2]);
    }

    // §D.2.21 — num_model_values_minus1 > 5 must be rejected.
    #[test]
    fn film_grain_characteristics_rejects_num_model_values_out_of_range() {
        let fields = [
            (0, 1), // cancel_flag
            (0, 2), // model_id
            (0, 1), // separate_colour_description_present_flag
            (0, 2), // blending_mode_id
            (0, 4), // log2_scale_factor
            (1, 1), // comp_model_present_flag[0] = true
            (0, 1),
            (0, 1),
            (0, 8), // num_intensity_intervals_minus1 = 0
            (6, 3), // num_model_values_minus1 = 6 — out of range
        ];
        let payload = pack_bits(&fields);
        let err = parse_film_grain_characteristics(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::FilmGrainNumModelValuesOutOfRange {
                component: 0,
                got: 6
            }
        );
    }

    // Dispatcher wiring for the new payload types.
    #[test]
    fn parse_payload_dispatches_display_orientation() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(1, 1)]); // cancel_flag=1 only
        let got = parse_payload(47, &payload, &ctx).unwrap();
        match got {
            SeiPayload::DisplayOrientation(d) => assert!(d.cancel_flag),
            other => panic!("expected DisplayOrientation, got {:?}", other),
        }
    }

    #[test]
    fn parse_payload_dispatches_alt_transfer() {
        let ctx = SeiContext::default();
        let got = parse_payload(147, &[18u8], &ctx).unwrap();
        match got {
            SeiPayload::AlternativeTransferCharacteristics(v) => assert_eq!(v, 18),
            other => panic!(
                "expected AlternativeTransferCharacteristics, got {:?}",
                other
            ),
        }
    }

    #[test]
    fn parse_payload_dispatches_frame_packing() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[
            (1, 1), // ue id=0
            (1, 1), // cancel_flag=1
            (0, 1), // extension_flag=0
        ]);
        let got = parse_payload(45, &payload, &ctx).unwrap();
        match got {
            SeiPayload::FramePackingArrangement(f) => assert!(f.cancel_flag),
            other => panic!("expected FramePackingArrangement, got {:?}", other),
        }
    }

    // §G.13.1.10 / §G.13.2.10 — multiview_view_position (payload 46).
    //
    // Two-view case (num_views_minus1 = 1):
    //   num_views_minus1 = 1 → ue codeword "010" (3 bits)
    //   view_position[0] = 0 → ue codeword "1" (1 bit)
    //   view_position[1] = 1 → ue codeword "010" (3 bits)
    //   multiview_view_position_extension_flag = 0 (1 bit)
    // Total: 8 bits → one byte.
    #[test]
    fn multiview_view_position_two_views_left_then_right() {
        let payload = pack_bits(&[
            (0b010, 3), // num_views_minus1 = 1
            (1, 1),     // view_position[0] = 0
            (0b010, 3), // view_position[1] = 1
            (0, 1),     // extension_flag = 0
        ]);
        let got = parse_multiview_view_position(&payload).unwrap();
        assert_eq!(got.view_positions, vec![0u16, 1u16]);
        assert!(!got.extension_flag);
    }

    #[test]
    fn multiview_view_position_single_view_zero_position() {
        // num_views_minus1 = 0 → ue codeword "1" (1 bit)
        // view_position[0] = 0 → ue "1" (1 bit)
        // extension_flag = 0 (1 bit)
        let payload = pack_bits(&[(1, 1), (1, 1), (0, 1)]);
        let got = parse_multiview_view_position(&payload).unwrap();
        assert_eq!(got.view_positions, vec![0u16]);
        assert!(!got.extension_flag);
    }

    #[test]
    fn multiview_view_position_preserves_extension_flag_when_set() {
        // Single-view case with extension_flag = 1. §G.13.2.10 says
        // conforming encoders write 0; the parser still surfaces the
        // bit so callers can audit a non-conforming stream.
        let payload = pack_bits(&[(1, 1), (1, 1), (1, 1)]);
        let got = parse_multiview_view_position(&payload).unwrap();
        assert_eq!(got.view_positions, vec![0u16]);
        assert!(got.extension_flag);
    }

    #[test]
    fn multiview_view_position_rejects_num_views_above_1023() {
        // num_views_minus1 = 1024 → ue codeword "00000000001 0000000001"
        // (10 leading zeros, leading 1, then 10-bit suffix = 0b00000000001).
        // §G.13.2.10 caps it at 1023. We don't bother walking the
        // suffix bits — the parser must reject before allocating.
        let mut bits = Vec::new();
        // ue(1024) = 11 bits prefix (10 zeros + a 1) + 10 bits suffix
        // = "00000000001 0000000001".
        for _ in 0..10 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..9 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        let payload = pack_bits(&bits);
        let err = parse_multiview_view_position(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::MultiviewViewPositionNumViewsOutOfRange(1024)
        ));
    }

    #[test]
    fn multiview_view_position_rejects_view_position_above_1023() {
        // num_views_minus1 = 0 → ue "1"
        // view_position[0] = 1024 → ue 11+10 bits as above.
        let mut bits = vec![(1u64, 1u32)];
        for _ in 0..10 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..9 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        let payload = pack_bits(&bits);
        let err = parse_multiview_view_position(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::MultiviewViewPositionViewPositionOutOfRange { i: 0, got: 1024 }
        ));
    }

    #[test]
    fn parse_payload_dispatches_multiview_view_position() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[
            (0b010, 3), // num_views_minus1 = 1
            (1, 1),     // view_position[0] = 0
            (0b010, 3), // view_position[1] = 1
            (0, 1),     // extension_flag = 0
        ]);
        let got = parse_payload(46, &payload, &ctx).unwrap();
        match got {
            SeiPayload::MultiviewViewPosition(m) => {
                assert_eq!(m.view_positions, vec![0u16, 1u16]);
                assert!(!m.extension_flag);
            }
            other => panic!("expected MultiviewViewPosition, got {:?}", other),
        }
    }

    // §G.13.1.4 / §G.13.2.4 — multiview_scene_info (payload 39, Annex G).
    // Single ue(v) `max_disparity`, range 0..=1023.
    #[test]
    fn multiview_scene_info_zero_disparity() {
        // max_disparity = 0 → ue codeword "1" (1 bit).
        let payload = pack_bits(&[(1, 1)]);
        let got = parse_multiview_scene_info(&payload).unwrap();
        assert_eq!(got.max_disparity, 0);
    }

    #[test]
    fn multiview_scene_info_picks_up_typical_value() {
        // max_disparity = 16 → ue codeword "000010001" (4 leading zeros
        // + leading 1 + 4-bit suffix 0001).
        let payload = pack_bits(&[(0b000010001, 9)]);
        let got = parse_multiview_scene_info(&payload).unwrap();
        assert_eq!(got.max_disparity, 16);
    }

    #[test]
    fn multiview_scene_info_accepts_boundary_1023() {
        // max_disparity = 1023 → codeNum+1 = 1024 = 2^10 → 10 leading
        // zeros + 1 + 10-bit suffix 0b0000000000 = 21 bits. Per
        // §9.1 the ue(v) codeword for value v has Floor(log2(v+1))
        // leading zeros.
        let mut bits: Vec<(u64, u32)> = Vec::new();
        for _ in 0..10 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..10 {
            bits.push((0, 1));
        }
        let payload = pack_bits(&bits);
        let got = parse_multiview_scene_info(&payload).unwrap();
        assert_eq!(got.max_disparity, 1023);
    }

    #[test]
    fn multiview_scene_info_rejects_max_disparity_above_1023() {
        // max_disparity = 1024 → ue codeword "00000000001 0000000001"
        // (10 leading zeros + 1 + 10-bit suffix 0b00000_00001).
        let mut bits: Vec<(u64, u32)> = Vec::new();
        for _ in 0..10 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..9 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        let payload = pack_bits(&bits);
        let err = parse_multiview_scene_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::MultiviewSceneInfoMaxDisparityOutOfRange(1024)
        ));
    }

    #[test]
    fn parse_payload_dispatches_multiview_scene_info() {
        let ctx = SeiContext::default();
        // max_disparity = 7 → ue codeword "0001000" (3 leading zeros +
        // 1 + 3-bit suffix 0b000).
        let payload = pack_bits(&[(0b0001000, 7)]);
        let got = parse_payload(39, &payload, &ctx).unwrap();
        match got {
            SeiPayload::MultiviewSceneInfo(m) => assert_eq!(m.max_disparity, 7),
            other => panic!("expected MultiviewSceneInfo, got {:?}", other),
        }
    }

    // §G.13.1.8 / §G.13.2.8 — operation_point_not_present (payload 43).
    #[test]
    fn operation_point_not_present_empty_list() {
        // num_operation_points = 0 → ue codeword "1" (1 bit).
        // Per §G.13.2.8 num_operation_points = 0 means "all
        // operation points declared in the prior view_scalability_info
        // SEI message are present" — a valid degenerate case we must
        // accept rather than refuse.
        let payload = pack_bits(&[(1, 1)]);
        let got = parse_operation_point_not_present(&payload).unwrap();
        assert!(got.operation_point_not_present_ids.is_empty());
    }

    #[test]
    fn operation_point_not_present_three_ids() {
        // num_operation_points = 3 → ue codeword "00100" (5 bits).
        // ids = [0, 1, 65535]:
        //   0 → ue "1" (1 bit)
        //   1 → ue "010" (3 bits)
        //   65535 → codeNum+1 = 65536 = 2^16 → 16 leading zeros + 1 +
        //           16-bit suffix 0b0000_0000_0000_0000 = 33 bits.
        let mut bits: Vec<(u64, u32)> = vec![(0b00100, 5), (1, 1), (0b010, 3)];
        for _ in 0..16 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..16 {
            bits.push((0, 1));
        }
        let payload = pack_bits(&bits);
        let got = parse_operation_point_not_present(&payload).unwrap();
        assert_eq!(
            got.operation_point_not_present_ids,
            vec![0u16, 1u16, 65535u16]
        );
    }

    #[test]
    fn operation_point_not_present_rejects_id_above_65535() {
        // num_operation_points = 1, id = 65536 → codeNum+1 = 65537 =
        // 2^16 + 1 → 16 leading zeros + 1 + 16-bit suffix
        // 0b0000_0000_0000_0001 = 33 bits.
        let mut bits: Vec<(u64, u32)> = vec![(0b010, 3)];
        for _ in 0..16 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..15 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        let payload = pack_bits(&bits);
        let err = parse_operation_point_not_present(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::OperationPointNotPresentIdOutOfRange { i: 0, got: 65536 }
        ));
    }

    #[test]
    fn operation_point_not_present_rejects_count_above_65536() {
        // num_operation_points = 65537 → codeNum+1 = 65538 = 2^16 + 2
        // → 16 leading zeros + 1 + 16-bit suffix
        // 0b0000_0000_0000_0010 = 33 bits. The §G.13.2.8 65536 cap
        // must reject BEFORE the per-id loop allocates.
        let mut bits: Vec<(u64, u32)> = Vec::new();
        for _ in 0..16 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..14 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        bits.push((0, 1));
        let payload = pack_bits(&bits);
        let err = parse_operation_point_not_present(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::OperationPointNotPresentCountOutOfRange(65537)
        ));
    }

    #[test]
    fn parse_payload_dispatches_operation_point_not_present() {
        let ctx = SeiContext::default();
        // num_operation_points = 2, ids = [10, 200]:
        //   2 → ue "011" (3 bits)
        //   10 → ue "0001011" (3 zeros + 1 + 3-bit suffix 0b011 = 7 bits)
        //   200 → ue: 7 leading zeros + 1 + 7-bit suffix
        //               0b1001001 = 15 bits.
        let mut bits: Vec<(u64, u32)> = vec![(0b011, 3), (0b0001011, 7)];
        for _ in 0..7 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        bits.push((1, 1));
        bits.push((0, 1));
        bits.push((0, 1));
        bits.push((1, 1));
        bits.push((0, 1));
        bits.push((0, 1));
        bits.push((1, 1));
        let payload = pack_bits(&bits);
        let got = parse_payload(43, &payload, &ctx).unwrap();
        match got {
            SeiPayload::OperationPointNotPresent(o) => {
                assert_eq!(o.operation_point_not_present_ids, vec![10u16, 200u16]);
            }
            other => panic!("expected OperationPointNotPresent, got {:?}", other),
        }
    }

    // §G.13.1.7 / §G.13.2.7 — view_dependency_change (payload 42).

    /// A SeiContext carrying a small MVC inter-view dependency: two
    /// views (num_views_minus1 = 2), so the §G.13.1.7 update branches
    /// have something to walk.
    fn vdc_ctx_two_views() -> SeiContext {
        SeiContext {
            mvc_view_ref_counts: vec![
                MvcViewRefCounts {
                    num_anchor_refs_l0: 1,
                    num_anchor_refs_l1: 0,
                    num_non_anchor_refs_l0: 2,
                    num_non_anchor_refs_l1: 1,
                },
                MvcViewRefCounts {
                    num_anchor_refs_l0: 2,
                    num_anchor_refs_l1: 1,
                    num_non_anchor_refs_l0: 0,
                    num_non_anchor_refs_l1: 1,
                },
            ],
            ..Default::default()
        }
    }

    #[test]
    fn view_dependency_change_no_updates_reads_no_flags() {
        // seq_parameter_set_id = 0 → ue "1".
        // anchor_update_flag = 0, non_anchor_update_flag = 0.
        // The per-view loops are skipped entirely, so an empty context
        // is fine (the rejection only triggers when an update flag is
        // set).
        let payload = pack_bits(&[(1, 1), (0, 1), (0, 1)]);
        let got = parse_view_dependency_change(&payload, &SeiContext::default()).unwrap();
        assert_eq!(got.seq_parameter_set_id, 0);
        assert!(!got.anchor_update_flag);
        assert!(!got.non_anchor_update_flag);
        assert!(got.views.is_empty());
    }

    #[test]
    fn view_dependency_change_anchor_only() {
        // seq_parameter_set_id = 1 → ue "010" (3 bits).
        // anchor_update_flag = 1, non_anchor_update_flag = 0.
        // Anchor block over two views:
        //   view i=1: num_anchor_refs_l0=1 → 1 flag; l1=0 → none.
        //   view i=2: num_anchor_refs_l0=2 → 2 flags; l1=1 → 1 flag.
        // Flags in order: [1] [0,1] [1] → 4 bits.
        let payload = pack_bits(&[
            (0b010, 3), // sps id = 1
            (1, 1),     // anchor_update_flag
            (0, 1),     // non_anchor_update_flag
            (1, 1),     // view1 anchor_l0[0]
            (0, 1),     // view2 anchor_l0[0]
            (1, 1),     // view2 anchor_l0[1]
            (1, 1),     // view2 anchor_l1[0]
        ]);
        let got = parse_view_dependency_change(&payload, &vdc_ctx_two_views()).unwrap();
        assert_eq!(got.seq_parameter_set_id, 1);
        assert!(got.anchor_update_flag);
        assert!(!got.non_anchor_update_flag);
        assert_eq!(got.views.len(), 2);
        assert_eq!(got.views[0].anchor_ref_l0_flags, vec![true]);
        assert!(got.views[0].anchor_ref_l1_flags.is_empty());
        assert_eq!(got.views[1].anchor_ref_l0_flags, vec![false, true]);
        assert_eq!(got.views[1].anchor_ref_l1_flags, vec![true]);
        // Non-anchor flags absent because non_anchor_update_flag == 0.
        assert!(got.views[0].non_anchor_ref_l0_flags.is_empty());
        assert!(got.views[1].non_anchor_ref_l1_flags.is_empty());
    }

    #[test]
    fn view_dependency_change_both_updates_separate_loops() {
        // anchor_update_flag = 1, non_anchor_update_flag = 1.
        // Per §G.13.1.7 the non-anchor block is a SECOND full per-view
        // loop that follows the entire anchor block — NOT interleaved.
        // Anchor flags (4): view1 l0[0]; view2 l0[0],l0[1],l1[0].
        // Non-anchor flags: view1 l0=2 (2), l1=1 (1); view2 l0=0,
        //   l1=1 (1) → 4 flags.
        let payload = pack_bits(&[
            (1, 1), // sps id = 0
            (1, 1), // anchor_update_flag
            (1, 1), // non_anchor_update_flag
            // anchor block
            (1, 1), // v1 anchor_l0[0]
            (0, 1), // v2 anchor_l0[0]
            (1, 1), // v2 anchor_l0[1]
            (0, 1), // v2 anchor_l1[0]
            // non-anchor block (separate loop)
            (1, 1), // v1 non_anchor_l0[0]
            (0, 1), // v1 non_anchor_l0[1]
            (1, 1), // v1 non_anchor_l1[0]
            (0, 1), // v2 non_anchor_l1[0]
        ]);
        let got = parse_view_dependency_change(&payload, &vdc_ctx_two_views()).unwrap();
        assert!(got.anchor_update_flag);
        assert!(got.non_anchor_update_flag);
        // Anchor side.
        assert_eq!(got.views[0].anchor_ref_l0_flags, vec![true]);
        assert_eq!(got.views[1].anchor_ref_l0_flags, vec![false, true]);
        assert_eq!(got.views[1].anchor_ref_l1_flags, vec![false]);
        // Non-anchor side.
        assert_eq!(got.views[0].non_anchor_ref_l0_flags, vec![true, false]);
        assert_eq!(got.views[0].non_anchor_ref_l1_flags, vec![true]);
        assert!(got.views[1].non_anchor_ref_l0_flags.is_empty());
        assert_eq!(got.views[1].non_anchor_ref_l1_flags, vec![false]);
    }

    #[test]
    fn view_dependency_change_update_without_context_rejected() {
        // anchor_update_flag = 1 but no MVC counts available → the
        // loop bounds are unknown, so the message is rejected rather
        // than silently reading zero flags.
        let payload = pack_bits(&[(1, 1), (1, 1), (0, 1)]);
        let err = parse_view_dependency_change(&payload, &SeiContext::default()).unwrap_err();
        assert!(matches!(
            err,
            SeiError::ViewDependencyChangeRefCountsUnknown
        ));
    }

    #[test]
    fn view_dependency_change_rejects_sps_id_above_31() {
        // seq_parameter_set_id = 32 → ue codeNum+1 = 33 = 2^5 + 1 →
        // 5 leading zeros + 1 + 5-bit suffix 0b00001 = 11 bits.
        let mut bits: Vec<(u64, u32)> = Vec::new();
        for _ in 0..5 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..4 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        let payload = pack_bits(&bits);
        let err = parse_view_dependency_change(&payload, &SeiContext::default()).unwrap_err();
        assert!(matches!(
            err,
            SeiError::ViewDependencyChangeSpsIdOutOfRange(32)
        ));
    }

    #[test]
    fn parse_payload_dispatches_view_dependency_change() {
        let ctx = vdc_ctx_two_views();
        // Reuse the anchor-only shape from above through the dispatcher.
        let payload = pack_bits(&[
            (0b010, 3), // sps id = 1
            (1, 1),     // anchor_update_flag
            (0, 1),     // non_anchor_update_flag
            (1, 1),     // view1 anchor_l0[0]
            (0, 1),     // view2 anchor_l0[0]
            (1, 1),     // view2 anchor_l0[1]
            (1, 1),     // view2 anchor_l1[0]
        ]);
        let got = parse_payload(42, &payload, &ctx).unwrap();
        match got {
            SeiPayload::ViewDependencyChange(v) => {
                assert_eq!(v.seq_parameter_set_id, 1);
                assert_eq!(v.views.len(), 2);
                assert_eq!(v.views[1].anchor_ref_l0_flags, vec![false, true]);
            }
            other => panic!("expected ViewDependencyChange, got {:?}", other),
        }
    }

    // §G.13.1.9 / §G.13.2.9 — base_view_temporal_hrd (payload 44).

    /// Build the bits of one §E.1.2 `hrd_parameters()` block with a
    /// single SchedSelIdx (cpb_cnt_minus1 = 0) and all-zero values, so
    /// tests can append/prepend other fields around it.
    fn minimal_hrd_bits() -> Vec<(u64, u32)> {
        vec![
            (1, 1), // cpb_cnt_minus1 = 0 → ue "1"
            (0, 4), // bit_rate_scale
            (0, 4), // cpb_size_scale
            (1, 1), // bit_rate_value_minus1[0] = 0 → ue "1"
            (1, 1), // cpb_size_value_minus1[0] = 0 → ue "1"
            (0, 1), // cbr_flag[0]
            (0, 5), // initial_cpb_removal_delay_length_minus1
            (0, 5), // cpb_removal_delay_length_minus1
            (0, 5), // dpb_output_delay_length_minus1
            (0, 5), // time_offset_length
        ]
    }

    #[test]
    fn base_view_temporal_hrd_single_layer_no_timing_no_hrd() {
        // num_of_temporal_layers_in_base_view_minus1 = 0 → ue "1"
        // layer[0]: temporal_id u(3) = 5
        //           sei_mvc_timing_info_present_flag u(1) = 0
        //           sei_mvc_nal_hrd_parameters_present_flag u(1) = 0
        //           sei_mvc_vcl_hrd_parameters_present_flag u(1) = 0
        //           (low_delay flag NOT coded — neither HRD present)
        //           sei_mvc_pic_struct_present_flag u(1) = 1
        let payload = pack_bits(&[(1, 1), (5, 3), (0, 1), (0, 1), (0, 1), (1, 1)]);
        let got = parse_base_view_temporal_hrd(&payload).unwrap();
        assert_eq!(got.layers.len(), 1);
        let l = &got.layers[0];
        assert_eq!(l.temporal_id, 5);
        assert!(l.timing_info.is_none());
        assert!(l.nal_hrd_parameters.is_none());
        assert!(l.vcl_hrd_parameters.is_none());
        assert_eq!(l.low_delay_hrd_flag, None);
        assert!(l.pic_struct_present_flag);
    }

    #[test]
    fn base_view_temporal_hrd_layer_with_timing_and_nal_hrd() {
        // num_of_temporal_layers_in_base_view_minus1 = 0 → ue "1"
        // layer[0]: temporal_id u(3) = 2
        //           timing_info_present u(1) = 1
        //             num_units_in_tick u(32) = 1001
        //             time_scale u(32) = 60000
        //             fixed_frame_rate_flag u(1) = 1
        //           nal_hrd_present u(1) = 1 → minimal hrd_parameters()
        //           vcl_hrd_present u(1) = 0
        //           low_delay_hrd_flag u(1) = 1 (coded: nal present)
        //           pic_struct_present_flag u(1) = 0
        let mut bits: Vec<(u64, u32)> = vec![
            (1, 1),
            (2, 3),
            (1, 1),
            (1001, 32),
            (60000, 32),
            (1, 1),
            (1, 1), // nal_hrd_parameters_present_flag
        ];
        bits.extend(minimal_hrd_bits());
        bits.push((0, 1)); // vcl_hrd_parameters_present_flag
        bits.push((1, 1)); // low_delay_hrd_flag
        bits.push((0, 1)); // pic_struct_present_flag
        let payload = pack_bits(&bits);
        let got = parse_base_view_temporal_hrd(&payload).unwrap();
        assert_eq!(got.layers.len(), 1);
        let l = &got.layers[0];
        assert_eq!(l.temporal_id, 2);
        let t = l.timing_info.as_ref().expect("timing present");
        assert_eq!(t.num_units_in_tick, 1001);
        assert_eq!(t.time_scale, 60000);
        assert!(t.fixed_frame_rate_flag);
        let nal = l.nal_hrd_parameters.as_ref().expect("nal hrd present");
        assert_eq!(nal.cpb_cnt_minus1, 0);
        assert!(l.vcl_hrd_parameters.is_none());
        assert_eq!(l.low_delay_hrd_flag, Some(true));
        assert!(!l.pic_struct_present_flag);
    }

    #[test]
    fn base_view_temporal_hrd_two_layers_via_dispatch() {
        // num_of_temporal_layers_in_base_view_minus1 = 1 → ue "010"
        // layer[0]: tid=0, no timing, no hrd, pic_struct=0
        // layer[1]: tid=1, no timing, no hrd, pic_struct=1
        let payload = pack_bits(&[
            (0b010, 3),
            (0, 3),
            (0, 1),
            (0, 1),
            (0, 1),
            (0, 1), // layer 0
            (1, 3),
            (0, 1),
            (0, 1),
            (0, 1),
            (1, 1), // layer 1
        ]);
        let ctx = SeiContext::default();
        let got = parse_payload(44, &payload, &ctx).unwrap();
        match got {
            SeiPayload::BaseViewTemporalHrd(b) => {
                assert_eq!(b.layers.len(), 2);
                assert_eq!(b.layers[0].temporal_id, 0);
                assert!(!b.layers[0].pic_struct_present_flag);
                assert_eq!(b.layers[1].temporal_id, 1);
                assert!(b.layers[1].pic_struct_present_flag);
            }
            other => panic!("expected BaseViewTemporalHrd, got {:?}", other),
        }
    }

    #[test]
    fn base_view_temporal_hrd_rejects_layer_count_above_8() {
        // num_of_temporal_layers_in_base_view_minus1 = 8 → out of range
        // (§G.13.2.9 caps the minus1 form at 7). ue(8) = codeNum 8
        // → 3 leading zeros + 1 + 3-bit suffix 0b001 = "0001001".
        let payload = pack_bits(&[(0b0001001, 7)]);
        let err = parse_base_view_temporal_hrd(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::BaseViewTemporalHrdLayerCountOutOfRange(8)
        ));
    }

    // §G.13.1.6 / §G.13.2.6 — non_required_view_component (payload 41).
    #[test]
    fn non_required_view_component_single_entry_single_component() {
        // num_info_entries_minus1 = 0 → ue "1"
        // entry[0]:
        //   view_order_index = 1   → ue "010" (codeNum=1)
        //   num_non_required_view_components_minus1 = 0 → ue "1"
        //   index_delta_minus1[0] = 0 → ue "1"
        // Total: 1 + 3 + 1 + 1 = 6 bits.
        let payload = pack_bits(&[(1, 1), (0b010, 3), (1, 1), (1, 1)]);
        let got = parse_non_required_view_component(&payload).unwrap();
        assert_eq!(got.entries.len(), 1);
        assert_eq!(got.entries[0].view_order_index, 1);
        assert_eq!(got.entries[0].index_delta_minus1, vec![0u16]);
    }

    #[test]
    fn non_required_view_component_two_entries_with_multiple_deltas() {
        // Two target views, with multiple non-required deltas each.
        //
        // num_info_entries_minus1 = 1 → ue "010"
        // entry[0]:
        //   view_order_index = 2 → ue "011" (codeNum=2)
        //   num_non_required_view_components_minus1 = 1 → ue "010"
        //   index_delta_minus1[0] = 0 → ue "1"
        //   index_delta_minus1[1] = 1 → ue "010"
        // entry[1]:
        //   view_order_index = 3 → ue "00100" (codeNum=3)
        //   num_non_required_view_components_minus1 = 0 → ue "1"
        //   index_delta_minus1[0] = 2 → ue "011"
        // Total: 3 + 3 + 3 + 1 + 3 + 5 + 1 + 3 = 22 bits.
        let payload = pack_bits(&[
            (0b010, 3),
            (0b011, 3),
            (0b010, 3),
            (1, 1),
            (0b010, 3),
            (0b00100, 5),
            (1, 1),
            (0b011, 3),
        ]);
        let got = parse_non_required_view_component(&payload).unwrap();
        assert_eq!(got.entries.len(), 2);
        assert_eq!(got.entries[0].view_order_index, 2);
        assert_eq!(got.entries[0].index_delta_minus1, vec![0u16, 1u16]);
        assert_eq!(got.entries[1].view_order_index, 3);
        assert_eq!(got.entries[1].index_delta_minus1, vec![2u16]);
    }

    #[test]
    fn non_required_view_component_rejects_view_order_index_zero() {
        // §G.13.2.6 — view_order_index shall be in 1..=num_views_minus1;
        // a 0 value is below the lower bound (view 0 is the base view,
        // not a target).
        //
        // num_info_entries_minus1 = 0 → ue "1"
        // entry[0].view_order_index = 0 → ue "1"
        let payload = pack_bits(&[(1, 1), (1, 1)]);
        let err = parse_non_required_view_component(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::NonRequiredViewComponentViewOrderIndexOutOfRange { i: 0, got: 0 }
        ));
    }

    #[test]
    fn non_required_view_component_rejects_num_info_entries_above_bound() {
        // §G.13.2.6 — num_info_entries_minus1 ≤ num_views_minus1 − 1.
        // With Annex G's absolute num_views_minus1 ≤ 1023, the cap is
        // 1022. We test the boundary: num_info_entries_minus1 = 1023.
        //
        // codeNum = 1023 → codeNum + 1 = 1024 = 2^10 → 10 leading
        // zeros + 1 + 10-bit suffix 0b0000000000 = 21 bits.
        let mut bits: Vec<(u64, u32)> = Vec::new();
        for _ in 0..10 {
            bits.push((0, 1));
        }
        bits.push((1, 1));
        for _ in 0..10 {
            bits.push((0, 1));
        }
        let payload = pack_bits(&bits);
        let err = parse_non_required_view_component(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::NonRequiredViewComponentNumInfoEntriesOutOfRange(1023)
        ));
    }

    #[test]
    fn non_required_view_component_rejects_inner_count_above_voi_minus_one() {
        // §G.13.2.6 — num_non_required_view_components_minus1[i] ≤
        // view_order_index − 1. With view_order_index = 1, the cap is
        // 0; setting the count to 1 must reject before allocation.
        //
        // num_info_entries_minus1 = 0 → ue "1"
        // entry[0]:
        //   view_order_index = 1 → ue "010"
        //   num_non_required_view_components_minus1 = 1 → ue "010"
        let payload = pack_bits(&[(1, 1), (0b010, 3), (0b010, 3)]);
        let err = parse_non_required_view_component(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::NonRequiredViewComponentCountOutOfRange {
                i: 0,
                got: 1,
                view_order_index: 1
            }
        ));
    }

    #[test]
    fn non_required_view_component_rejects_index_delta_above_voi_minus_one() {
        // §G.13.2.6 — index_delta_minus1[i][j] ≤ view_order_index − 1.
        // With view_order_index = 2, the cap is 1; setting the delta
        // to 2 must reject. (One inner component so the per-entry
        // allocation succeeds and we reach the inner range check.)
        //
        // num_info_entries_minus1 = 0 → ue "1"
        // entry[0]:
        //   view_order_index = 2 → ue "011"
        //   num_non_required_view_components_minus1 = 0 → ue "1"
        //   index_delta_minus1[0] = 2 → ue "011"
        let payload = pack_bits(&[(1, 1), (0b011, 3), (1, 1), (0b011, 3)]);
        let err = parse_non_required_view_component(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::NonRequiredViewComponentIndexDeltaOutOfRange {
                i: 0,
                j: 0,
                got: 2,
                view_order_index: 2
            }
        ));
    }

    #[test]
    fn parse_payload_dispatches_non_required_view_component() {
        // Same single-entry single-component shape as the smoke test
        // above, but routed via `parse_payload`'s dispatch arm.
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(1, 1), (0b010, 3), (1, 1), (1, 1)]);
        let got = parse_payload(41, &payload, &ctx).unwrap();
        match got {
            SeiPayload::NonRequiredViewComponent(n) => {
                assert_eq!(n.entries.len(), 1);
                assert_eq!(n.entries[0].view_order_index, 1);
                assert_eq!(n.entries[0].index_delta_minus1, vec![0u16]);
            }
            other => panic!("expected NonRequiredViewComponent, got {:?}", other),
        }
    }

    // Round-213 — §G.13.2.5 multiview_acquisition_info (payload
    // type 40). Tests cover the four parse paths:
    //
    //   1. Both flags off — only num_views + the two flag bits are
    //      consumed.
    //   2. Intrinsic only, intrinsic_params_equal_flag = 1 — single
    //      shared camera entry.
    //   3. Extrinsic only — full 3×3 R + 3-vector T per camera.
    //   4. Range-check rejections on num_views_minus1 + each prec_*.
    //
    // A separate test exercises the §G.13.2.5 mantissa-width formula
    // (denormal e == 0, normal 0 < e < 63, reserved e == 63) and
    // the FloatComponent::to_f64 reconstruction.

    #[test]
    fn multiview_acquisition_info_no_flags_set() {
        // num_views_minus1 = 0 → ue "1"
        // intrinsic_param_flag = 0
        // extrinsic_param_flag = 0
        // Total: 1 + 1 + 1 = 3 bits.
        let payload = pack_bits(&[(1, 1), (0, 1), (0, 1)]);
        let info = parse_multiview_acquisition_info(&payload).unwrap();
        assert_eq!(info.num_views_minus1, 0);
        assert!(info.intrinsic.is_none());
        assert!(info.extrinsic.is_none());
    }

    #[test]
    fn multiview_acquisition_info_intrinsic_equal_flag_single_camera() {
        // Two views (num_views_minus1 = 1) but
        // intrinsic_params_equal_flag = 1 so only one intrinsic
        // entry is signalled.
        //
        // num_views_minus1 = 1                          → ue "010"      (3 bits)
        // intrinsic_param_flag = 1                       → u(1) "1"      (1 bit)
        // extrinsic_param_flag = 0                       → u(1) "0"      (1 bit)
        // intrinsic_params_equal_flag = 1                → u(1) "1"      (1 bit)
        // prec_focal_length = 1                          → ue "010"      (3 bits)
        // prec_principal_point = 1                       → ue "010"      (3 bits)
        // prec_skew_factor = 1                           → ue "010"      (3 bits)
        //
        // Five FloatComponents, each (sign u(1) + exponent u(6) +
        // mantissa u(v)) where v = max(0, e + prec − 31) for
        // 0 < e < 63. With prec = 1 and e = 30 (well below 63):
        // v = max(0, 30 + 1 − 31) = 0 → mantissa is zero-width.
        // Pick sign = 0, exponent = 30, mantissa = (absent).
        //
        // Per FloatComponent: u(1)=0, u(6)=30 (= 0b011110) → 7 bits.
        // Five components × 7 bits = 35 bits.
        let mut fields: Vec<(u64, u32)> = vec![
            (0b010, 3), // num_views_minus1 = 1
            (1, 1),     // intrinsic_param_flag = 1
            (0, 1),     // extrinsic_param_flag = 0
            (1, 1),     // intrinsic_params_equal_flag = 1
            (0b010, 3), // prec_focal_length = 1
            (0b010, 3), // prec_principal_point = 1
            (0b010, 3), // prec_skew_factor = 1
        ];
        // Five FloatComponents each with sign=0, exponent=30,
        // mantissa absent (width 0 since 0 < e < 63 → v = e + prec − 31
        // = 30 + 1 − 31 = 0).
        for _ in 0..5 {
            fields.push((0, 1)); // sign
            fields.push((30, 6)); // exponent
        }
        let payload = pack_bits(&fields);

        let info = parse_multiview_acquisition_info(&payload).unwrap();
        assert_eq!(info.num_views_minus1, 1);
        let intr = info.intrinsic.expect("intrinsic block populated");
        assert!(intr.intrinsic_params_equal_flag);
        assert_eq!(intr.prec_focal_length, 1);
        assert_eq!(intr.prec_principal_point, 1);
        assert_eq!(intr.prec_skew_factor, 1);
        // Only one camera entry shared across both views.
        assert_eq!(intr.cameras.len(), 1);
        let cam = intr.cameras[0];
        assert_eq!(cam.focal_length_x.exponent, 30);
        assert_eq!(cam.focal_length_x.mantissa_width, 0);
        assert_eq!(cam.focal_length_x.mantissa, 0);
        assert!(!cam.focal_length_x.sign);
        assert!(info.extrinsic.is_none());
    }

    #[test]
    fn multiview_acquisition_info_extrinsic_only_single_view() {
        // Single view (num_views_minus1 = 0), no intrinsic, full
        // extrinsic block (3×3 R + 3 t entries = 12 FloatComponents).
        // Pick prec_rotation_param = 0, prec_translation_param = 0
        // and each exponent = 0 → mantissa width = max(0, 0 − 30) = 0,
        // so each FloatComponent is 7 bits (sign + exponent).
        //
        // num_views_minus1 = 0                  → ue "1"  (1 bit)
        // intrinsic_param_flag = 0              → u(1) "0"
        // extrinsic_param_flag = 1              → u(1) "1"
        // prec_rotation_param = 0               → ue "1"  (1 bit)
        // prec_translation_param = 0            → ue "1"  (1 bit)
        // 12 FloatComponents × 7 bits each      = 84 bits
        // Total: 89 bits → 12 bytes (5 bits padding).
        let mut fields: Vec<(u64, u32)> = vec![
            (1, 1), // num_views_minus1 = 0
            (0, 1), // intrinsic_param_flag = 0
            (1, 1), // extrinsic_param_flag = 1
            (1, 1), // prec_rotation_param = 0
            (1, 1), // prec_translation_param = 0
        ];
        // Per row j ∈ {1..3}: 3 r entries + 1 t entry = 4 components.
        // Pick exponents in 0..=31 so the §G.13.2.5 mantissa-width
        // formula yields width 0 for both prec_rotation_param == 0
        // and prec_translation_param == 0 (otherwise an exponent in
        // 32..62 introduces a 1+ bit mantissa that re-frames every
        // subsequent component in this fixed-bit fixture).
        let pattern = [
            // (sign, exponent) — 9 r entries
            (0u64, 11u64),
            (1, 12),
            (0, 13),
            (1, 21),
            (0, 22),
            (1, 23),
            (0, 14),
            (1, 24),
            (0, 31),
        ];
        let t_pattern = [(1u64, 1u64), (0, 2), (1, 3)];
        // §G.13.1.5 order: for j { for k r[j][k]; t[j] }
        for j in 0..3 {
            for k in 0..3 {
                let (s, e) = pattern[j * 3 + k];
                fields.push((s, 1));
                fields.push((e, 6));
            }
            let (s, e) = t_pattern[j];
            fields.push((s, 1));
            fields.push((e, 6));
        }
        let payload = pack_bits(&fields);

        let info = parse_multiview_acquisition_info(&payload).unwrap();
        assert_eq!(info.num_views_minus1, 0);
        assert!(info.intrinsic.is_none());
        let extr = info.extrinsic.expect("extrinsic block populated");
        assert_eq!(extr.prec_rotation_param, 0);
        assert_eq!(extr.prec_translation_param, 0);
        assert_eq!(extr.cameras.len(), 1);
        let cam = extr.cameras[0];
        // Spot-check a few R entries.
        assert_eq!(cam.r[0][0].exponent, 11);
        assert!(!cam.r[0][0].sign);
        assert_eq!(cam.r[2][2].exponent, 31);
        assert_eq!(cam.r[2][2].mantissa_width, 0);
        // Spot-check the translation.
        assert_eq!(cam.t[0].exponent, 1);
        assert!(cam.t[0].sign);
        assert_eq!(cam.t[2].exponent, 3);
    }

    #[test]
    fn multiview_acquisition_info_rejects_num_views_above_1023() {
        // Encode num_views_minus1 = 1024 as ue(1024):
        // codeword length = 21 bits (10 leading zeros + 1 + 10
        // info bits for the value 1024 = 0b10000000001 with the
        // leading 1 acting as the marker), and Exp-Golomb decoder
        // returns ue = 2^10 + bits − 1 = 1024.
        //
        // 10 zeros + 1 + "0000000001" → codeNum = 0b10000000001 −1 = 1024.
        // Total bits: 21 → 3 bytes.
        let payload = pack_bits(&[(0, 10), (1, 1), (0b0000000001, 10)]);
        let err = parse_multiview_acquisition_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::MultiviewAcquisitionInfoNumViewsOutOfRange(1024)
        ));
    }

    #[test]
    fn multiview_acquisition_info_rejects_prec_focal_length_above_31() {
        // num_views_minus1 = 0, intrinsic_param_flag = 1,
        // extrinsic_param_flag = 0, intrinsic_params_equal_flag = 1,
        // prec_focal_length = 32 → ue("0000010001") (10 bits encode
        // codeNum 33 → ue value 32).
        //
        // Wait: codeNum k → ue value = k. The ue codeword for value
        // 32 has codeword "000001000001" → 12 bits (5 leading zeros
        // + 1 + 6 info bits = 0b100001 - 1 = 32). Let me just feed
        // a value > 31 via the marker-only form:
        // ue(32) = "0000001000001" (6 zeros + 1 + 0b000001 = ... hmm).
        //
        // Simpler: encode value via the 0 + marker form:
        // For value v, codeword is the (k+1)-bit binary
        // representation of (v+1) with the leading 1 implicit-marker:
        //   v = 32  →  v+1 = 33 = 0b100001 (6 bits)
        //   codeword = 5 leading zeros + 0b100001  (11 bits total).
        let payload = pack_bits(&[
            (1, 1), // num_views_minus1 = 0
            (1, 1), // intrinsic_param_flag = 1
            (0, 1), // extrinsic_param_flag = 0
            (1, 1), // intrinsic_params_equal_flag = 1
            // prec_focal_length = 32 → ue codeword "000001000001"
            // (5 zeros + 0b100001 = 11 bits).
            (0, 5),
            (0b100001, 6),
        ]);
        let err = parse_multiview_acquisition_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::MultiviewAcquisitionInfoPrecOutOfRange {
                field: "prec_focal_length",
                got: 32
            }
        ));
    }

    #[test]
    fn multiview_acquisition_info_mantissa_width_formula() {
        // §G.13.2.5: cover the three branches of the
        // mantissa-width formula.
        //
        // e == 0       → width = max(0, prec − 30)
        // 0 < e < 63   → width = max(0, e + prec − 31)
        // e == 63      → reserved; we store width = 0
        assert_eq!(mantissa_width_g1325(0, 0), 0); // prec=0, e=0 → 0
        assert_eq!(mantissa_width_g1325(31, 0), 1); // prec=31, e=0 → max(0, 1) = 1
        assert_eq!(mantissa_width_g1325(30, 0), 0); // prec=30, e=0 → max(0, 0) = 0
        assert_eq!(mantissa_width_g1325(0, 1), 0); // prec=0, e=1 → max(0, −30) = 0
        assert_eq!(mantissa_width_g1325(31, 1), 1); // prec=31, e=1 → 1+31−31 = 1
        assert_eq!(mantissa_width_g1325(31, 62), 62); // prec=31, e=62 → 62 (max)
        assert_eq!(mantissa_width_g1325(31, 63), 0); // e=63 reserved → 0
        assert_eq!(mantissa_width_g1325(0, 63), 0); // e=63 reserved → 0
    }

    #[test]
    fn float_component_to_f64_branches() {
        // Denormal: e == 0, n == 1, v == 1
        //   x = (-1)^0 * 2^(-30) * (1 / 2) = 2^(-31)
        let denormal = FloatComponent {
            sign: false,
            exponent: 0,
            mantissa: 1,
            mantissa_width: 1,
        };
        let expected_denormal = 2f64.powi(-31);
        assert!((denormal.to_f64() - expected_denormal).abs() < 1e-300);

        // Normal: e == 31, prec == 0 → v = 0, mantissa absent
        //   x = (-1)^0 * 2^(31 − 31) * (1 + 0) = 1.0
        let unity = FloatComponent {
            sign: false,
            exponent: 31,
            mantissa: 0,
            mantissa_width: 0,
        };
        assert_eq!(unity.to_f64(), 1.0);

        // Normal negative with a mantissa:
        //   e = 32, prec = 31 → v = 32 + 31 − 31 = 32
        //   mantissa = 0x80000000 (top bit set) → n / 2^v = 0.5
        //   x = (-1)^1 * 2^(32 − 31) * (1 + 0.5) = -3.0
        let neg_three = FloatComponent {
            sign: true,
            exponent: 32,
            mantissa: 0x8000_0000,
            mantissa_width: 32,
        };
        assert_eq!(neg_three.to_f64(), -3.0);

        // Reserved (e == 63) → NaN.
        let unspec = FloatComponent {
            sign: false,
            exponent: 63,
            mantissa: 0,
            mantissa_width: 0,
        };
        assert!(unspec.to_f64().is_nan());
    }

    #[test]
    fn parse_payload_dispatches_multiview_acquisition_info() {
        // Both flags off — three-bit payload routed via dispatcher.
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(1, 1), (0, 1), (0, 1)]);
        let got = parse_payload(40, &payload, &ctx).unwrap();
        match got {
            SeiPayload::MultiviewAcquisitionInfo(m) => {
                assert_eq!(m.num_views_minus1, 0);
                assert!(m.intrinsic.is_none());
                assert!(m.extrinsic.is_none());
            }
            other => panic!("expected MultiviewAcquisitionInfo, got {other:?}"),
        }
    }

    // Round-226 — §H.13.2.4 three_dimensional_reference_displays_info
    // (payload type 51, Annex H). Tests cover:
    //
    //   1. Single reference display, ref_viewing_distance_flag = 0,
    //      additional_shift_present_flag = 0 — minimum-syntax path.
    //   2. Two displays, ref_viewing_distance_flag = 1,
    //      additional_shift_present_flag = 1 — full per-display
    //      block exercised.
    //   3. UnsignedFloatComponent::to_f64 branches (normal /
    //      denormal / reserved).
    //   4. Range-check rejections on the three prec_* fields and
    //      num_ref_displays_minus1.
    //   5. parse_payload dispatcher round-trip.

    #[test]
    fn three_dimensional_reference_displays_info_minimum_single_display() {
        // prec_ref_baseline = 0                  → ue "1"        (1 bit)
        // prec_ref_display_width = 0             → ue "1"        (1 bit)
        // ref_viewing_distance_flag = 0          → u(1)          (1 bit)
        // num_ref_displays_minus1 = 0            → ue "1"        (1 bit)
        // One reference display, ref_viewing_distance_flag = 0:
        //   exponent_ref_baseline u(6) = 31      → (6 bits)
        //   mantissa_ref_baseline u(0) = absent  (prec=0, 0<e<63
        //                                          → v = 0+0-31 = 0)
        //   exponent_ref_display_width u(6) = 31 → (6 bits)
        //   mantissa_ref_display_width u(0)      = absent
        //   additional_shift_present_flag = 0    → u(1)          (1 bit)
        //   no num_sample_shift_plus512
        // three_dimensional_reference_displays_extension_flag = 0
        //                                        → u(1)          (1 bit)
        let payload = pack_bits(&[
            (1, 1),  // prec_ref_baseline = 0
            (1, 1),  // prec_ref_display_width = 0
            (0, 1),  // ref_viewing_distance_flag = 0
            (1, 1),  // num_ref_displays_minus1 = 0
            (31, 6), // exponent_ref_baseline
            (31, 6), // exponent_ref_display_width
            (0, 1),  // additional_shift_present_flag = 0
            (0, 1),  // extension_flag = 0
        ]);
        let info = parse_three_dimensional_reference_displays_info(&payload).unwrap();
        assert_eq!(info.prec_ref_baseline, 0);
        assert_eq!(info.prec_ref_display_width, 0);
        assert!(info.prec_ref_viewing_dist.is_none());
        assert_eq!(info.displays.len(), 1);
        let d = info.displays[0];
        assert_eq!(d.ref_baseline.exponent, 31);
        assert_eq!(d.ref_baseline.mantissa_width, 0);
        assert_eq!(d.ref_display_width.exponent, 31);
        assert!(d.ref_viewing_distance.is_none());
        assert!(!d.additional_shift_present_flag);
        assert!(d.num_sample_shift_plus512.is_none());
        assert!(!info.extension_flag);
    }

    #[test]
    fn three_dimensional_reference_displays_info_two_displays_with_viewing_distance() {
        // num_ref_displays_minus1 = 1 (two displays),
        // ref_viewing_distance_flag = 1, additional_shift_present_flag
        // = 1 on the first display only — exercises every conditional
        // branch in the per-display loop.
        let mut fields: Vec<(u64, u32)> = vec![
            (1, 1),     // prec_ref_baseline = 0
            (1, 1),     // prec_ref_display_width = 0
            (1, 1),     // ref_viewing_distance_flag = 1
            (1, 1),     // prec_ref_viewing_dist = 0
            (0b010, 3), // num_ref_displays_minus1 = 1
        ];
        // Display 0: every component uses exponent in 0..=31, prec=0
        // → mantissa width = 0 for both formulas.
        fields.push((20, 6)); // exponent_ref_baseline
        fields.push((21, 6)); // exponent_ref_display_width
        fields.push((22, 6)); // exponent_ref_viewing_distance
        fields.push((1, 1)); // additional_shift_present_flag = 1
                             // num_sample_shift_plus512 = 768 → centre + 256 samples.
        fields.push((768, 10));
        // Display 1: no additional shift signalled.
        fields.push((10, 6)); // exponent_ref_baseline
        fields.push((11, 6)); // exponent_ref_display_width
        fields.push((12, 6)); // exponent_ref_viewing_distance
        fields.push((0, 1)); // additional_shift_present_flag = 0
                             // extension_flag = 0
        fields.push((0, 1));
        let payload = pack_bits(&fields);
        let info = parse_three_dimensional_reference_displays_info(&payload).unwrap();
        assert_eq!(info.prec_ref_viewing_dist, Some(0));
        assert_eq!(info.displays.len(), 2);
        // Display 0.
        let d0 = info.displays[0];
        assert_eq!(d0.ref_baseline.exponent, 20);
        assert_eq!(d0.ref_display_width.exponent, 21);
        assert_eq!(d0.ref_viewing_distance.expect("rvd present").exponent, 22);
        assert!(d0.additional_shift_present_flag);
        assert_eq!(d0.num_sample_shift_plus512, Some(768));
        // Display 1.
        let d1 = info.displays[1];
        assert_eq!(d1.ref_baseline.exponent, 10);
        assert_eq!(d1.ref_display_width.exponent, 11);
        assert_eq!(d1.ref_viewing_distance.expect("rvd present").exponent, 12);
        assert!(!d1.additional_shift_present_flag);
        assert!(d1.num_sample_shift_plus512.is_none());
        assert!(!info.extension_flag);
    }

    #[test]
    fn three_dimensional_reference_displays_info_extension_flag_propagates() {
        // Single display, extension_flag = 1 — verify the trailing
        // bit is surfaced (per §H.13.2.4 the trailing region is
        // reserved; we ignore any subsequent data).
        let payload = pack_bits(&[
            (1, 1),  // prec_ref_baseline = 0
            (1, 1),  // prec_ref_display_width = 0
            (0, 1),  // ref_viewing_distance_flag = 0
            (1, 1),  // num_ref_displays_minus1 = 0
            (31, 6), // exponent_ref_baseline
            (31, 6), // exponent_ref_display_width
            (0, 1),  // additional_shift_present_flag = 0
            (1, 1),  // extension_flag = 1 (reserved, ignored)
        ]);
        let info = parse_three_dimensional_reference_displays_info(&payload).unwrap();
        assert!(info.extension_flag);
    }

    // Round-250 — §H.13.2.4 typed accessor on the already-parsed
    // `three_dimensional_reference_displays_info` body: surface the
    // signed `NumSampleShift[i] = num_sample_shift_plus512[i] − 512`
    // from the biased u(10) field stored on each `ReferenceDisplay`.

    #[test]
    fn reference_display_num_sample_shift_returns_none_when_absent() {
        // §H.13.2.4 — additional_shift_present_flag == 0 means the
        // shift was not signalled. The spec does not define an
        // inferred value, so the accessor returns None.
        let d = ReferenceDisplay {
            ref_baseline: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_display_width: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_viewing_distance: None,
            additional_shift_present_flag: false,
            num_sample_shift_plus512: None,
        };
        assert_eq!(d.num_sample_shift(), None);
    }

    #[test]
    fn reference_display_num_sample_shift_zero_at_bias() {
        // §H.13.2.4 — num_sample_shift_plus512[i] == 512 is the
        // "recommend that shifting is not applied" centre point; the
        // semantic shift is 0.
        let d = ReferenceDisplay {
            ref_baseline: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_display_width: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_viewing_distance: None,
            additional_shift_present_flag: true,
            num_sample_shift_plus512: Some(512),
        };
        assert_eq!(d.num_sample_shift(), Some(0));
    }

    #[test]
    fn reference_display_num_sample_shift_positive_branch() {
        // §H.13.2.4 — values above the 512 centre map to a positive
        // shift of (num_sample_shift_plus512 − 512) samples to the
        // right of the left view. Use the test fixture's value 768
        // → semantic +256.
        let d = ReferenceDisplay {
            ref_baseline: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_display_width: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_viewing_distance: None,
            additional_shift_present_flag: true,
            num_sample_shift_plus512: Some(768),
        };
        assert_eq!(d.num_sample_shift(), Some(256));
    }

    #[test]
    fn reference_display_num_sample_shift_negative_branch() {
        // §H.13.2.4 — values below the 512 centre map to a negative
        // shift; |NumSampleShift| samples to the left of the right
        // view. 256 → semantic −256.
        let d = ReferenceDisplay {
            ref_baseline: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_display_width: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_viewing_distance: None,
            additional_shift_present_flag: true,
            num_sample_shift_plus512: Some(256),
        };
        assert_eq!(d.num_sample_shift(), Some(-256));
    }

    #[test]
    fn reference_display_num_sample_shift_extreme_endpoints() {
        // §H.13.2.4 — full bitstream range 0..=1023 (u(10)) maps to
        // semantic −512..=511. Cover both endpoints to confirm no
        // off-by-one in the bias subtraction.
        let template = ReferenceDisplay {
            ref_baseline: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_display_width: UnsignedFloatComponent {
                exponent: 31,
                mantissa: 0,
                mantissa_width: 0,
            },
            ref_viewing_distance: None,
            additional_shift_present_flag: true,
            num_sample_shift_plus512: Some(0),
        };
        // Bias subtraction floor: 0 − 512 = −512.
        assert_eq!(template.num_sample_shift(), Some(-512));
        let max = ReferenceDisplay {
            num_sample_shift_plus512: Some(1023),
            ..template
        };
        // Bias subtraction ceiling: 1023 − 512 = 511.
        assert_eq!(max.num_sample_shift(), Some(511));
    }

    #[test]
    fn reference_display_num_sample_shift_round_trip_via_parser() {
        // End-to-end: parse a §H.13.1.4 payload with two displays
        // (one with shift signalled, one without) and confirm the
        // typed accessor returns the §H.13.2.4 semantic value on
        // both. The first display uses num_sample_shift_plus512 =
        // 768, identical to the existing parser fixture, so the
        // expected accessor reading is +256.
        let mut fields: Vec<(u64, u32)> = vec![
            (1, 1),     // prec_ref_baseline = 0
            (1, 1),     // prec_ref_display_width = 0
            (1, 1),     // ref_viewing_distance_flag = 1
            (1, 1),     // prec_ref_viewing_dist = 0
            (0b010, 3), // num_ref_displays_minus1 = 1
        ];
        // Display 0 — additional_shift_present_flag = 1, biased value 768.
        fields.push((20, 6)); // exponent_ref_baseline
        fields.push((21, 6)); // exponent_ref_display_width
        fields.push((22, 6)); // exponent_ref_viewing_distance
        fields.push((1, 1)); // additional_shift_present_flag = 1
        fields.push((768, 10));
        // Display 1 — additional_shift_present_flag = 0.
        fields.push((10, 6));
        fields.push((11, 6));
        fields.push((12, 6));
        fields.push((0, 1));
        fields.push((0, 1)); // extension_flag = 0
        let payload = pack_bits(&fields);
        let info = parse_three_dimensional_reference_displays_info(&payload).unwrap();
        assert_eq!(info.displays.len(), 2);
        assert_eq!(info.displays[0].num_sample_shift(), Some(256));
        assert_eq!(info.displays[1].num_sample_shift(), None);
    }

    #[test]
    fn unsigned_float_component_to_f64_branches() {
        // Normal: e = 31, prec = 0 → v = 0 → x = 2^0 * 1 = 1.0
        let unity = UnsignedFloatComponent {
            exponent: 31,
            mantissa: 0,
            mantissa_width: 0,
        };
        assert_eq!(unity.to_f64(), 1.0);
        // Normal positive with a mantissa:
        //   e = 32, prec = 31 → v = 32 + 31 − 31 = 32
        //   mantissa = 0x8000_0000 (top bit) → n / 2^v = 0.5
        //   x = 2^1 * (1 + 0.5) = 3.0  (sign-less per H.13.2.4)
        let three = UnsignedFloatComponent {
            exponent: 32,
            mantissa: 0x8000_0000,
            mantissa_width: 32,
        };
        assert_eq!(three.to_f64(), 3.0);
        // Denormal: e = 0, n = 1, v = 1 → x = 2^(-30) * (1/2) = 2^-31
        let denormal = UnsignedFloatComponent {
            exponent: 0,
            mantissa: 1,
            mantissa_width: 1,
        };
        assert!((denormal.to_f64() - 2f64.powi(-31)).abs() < 1e-300);
        // Reserved: e = 63 → NaN.
        let unspec = UnsignedFloatComponent {
            exponent: 63,
            mantissa: 0,
            mantissa_width: 0,
        };
        assert!(unspec.to_f64().is_nan());
    }

    #[test]
    fn three_dimensional_reference_displays_info_rejects_prec_ref_baseline_above_31() {
        // prec_ref_baseline = 32 → ue codeword "000001000001"
        // (5 zeros + 0b100001, 11 bits total). The first field
        // checked is prec_ref_baseline, so no earlier bits.
        let payload = pack_bits(&[(0, 5), (0b100001, 6)]);
        let err = parse_three_dimensional_reference_displays_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::ThreeDimensionalReferenceDisplaysInfoPrecOutOfRange {
                field: "prec_ref_baseline",
                got: 32
            }
        ));
    }

    #[test]
    fn three_dimensional_reference_displays_info_rejects_prec_ref_viewing_dist_above_31() {
        // prec_ref_baseline = 0 (1 bit), prec_ref_display_width = 0
        // (1 bit), ref_viewing_distance_flag = 1 (1 bit), then
        // prec_ref_viewing_dist = 32 (11 bits).
        let payload = pack_bits(&[
            (1, 1), // prec_ref_baseline = 0
            (1, 1), // prec_ref_display_width = 0
            (1, 1), // ref_viewing_distance_flag = 1
            (0, 5), // prec_ref_viewing_dist = 32 — codeword start
            (0b100001, 6),
        ]);
        let err = parse_three_dimensional_reference_displays_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::ThreeDimensionalReferenceDisplaysInfoPrecOutOfRange {
                field: "prec_ref_viewing_dist",
                got: 32
            }
        ));
    }

    #[test]
    fn three_dimensional_reference_displays_info_rejects_num_ref_displays_above_31() {
        // prec_ref_baseline = 0 (1), prec_ref_display_width = 0 (1),
        // ref_viewing_distance_flag = 0 (1), num_ref_displays_minus1
        // = 32 (11 bits codeword "000001000001").
        let payload = pack_bits(&[(1, 1), (1, 1), (0, 1), (0, 5), (0b100001, 6)]);
        let err = parse_three_dimensional_reference_displays_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::ThreeDimensionalReferenceDisplaysInfoNumRefDisplaysOutOfRange(32)
        ));
    }

    #[test]
    fn parse_payload_dispatches_three_dimensional_reference_displays_info() {
        // Minimum-syntax single-display payload routed via dispatcher.
        let ctx = SeiContext::default();
        let payload = pack_bits(&[
            (1, 1),  // prec_ref_baseline = 0
            (1, 1),  // prec_ref_display_width = 0
            (0, 1),  // ref_viewing_distance_flag = 0
            (1, 1),  // num_ref_displays_minus1 = 0
            (31, 6), // exponent_ref_baseline
            (31, 6), // exponent_ref_display_width
            (0, 1),  // additional_shift_present_flag = 0
            (0, 1),  // extension_flag = 0
        ]);
        let got = parse_payload(51, &payload, &ctx).unwrap();
        match got {
            SeiPayload::ThreeDimensionalReferenceDisplaysInfo(info) => {
                assert_eq!(info.displays.len(), 1);
                assert!(!info.extension_flag);
            }
            other => panic!("expected ThreeDimensionalReferenceDisplaysInfo, got {other:?}"),
        }
    }

    // §H.13.2.3 — depth_representation_info, minimum-syntax
    // single-view path with all flags off.
    //
    // all_views_equal_flag = 1  →  numViews = 1, num_views_minus1
    //                              absent.
    // z_near_flag = z_far_flag = 0  →  no z_axis_equal_flag.
    // d_min_flag = d_max_flag = 0   →  no disparity_reference_view.
    // depth_representation_type = 0 →  no nonlinear tail.
    // depth_info_view_id[0] = 0     →  ue codeword "1".
    #[test]
    fn depth_representation_info_minimum_syntax_all_views_equal() {
        let payload = pack_bits(&[
            (1, 1), // all_views_equal_flag = 1
            (0, 1), // z_near_flag = 0
            (0, 1), // z_far_flag = 0
            (0, 1), // d_min_flag = 0
            (0, 1), // d_max_flag = 0
            (1, 1), // depth_representation_type ue(0) = "1"
            (1, 1), // depth_info_view_id[0] ue(0) = "1"
        ]);
        let info = parse_depth_representation_info(&payload).unwrap();
        assert!(info.all_views_equal_flag);
        assert!(!info.z_near_flag);
        assert!(!info.z_far_flag);
        assert!(!info.d_min_flag);
        assert!(!info.d_max_flag);
        assert_eq!(info.z_axis_equal_flag, None);
        assert_eq!(info.common_z_axis_reference_view, None);
        assert_eq!(info.depth_representation_type, 0);
        assert_eq!(info.views.len(), 1);
        let v0 = info.views[0];
        assert_eq!(v0.depth_info_view_id, 0);
        assert!(v0.z_near.is_none());
        assert!(v0.z_far.is_none());
        assert!(v0.d_min.is_none());
        assert!(v0.d_max.is_none());
        assert!(v0.z_axis_reference_view.is_none());
        assert!(v0.disparity_reference_view.is_none());
        assert!(info.depth_nonlinear_representation_num_minus1.is_none());
        assert!(info.depth_nonlinear_representation_model.is_empty());
    }

    // §H.13.2.3 — two views with z_axis_equal_flag = 0 (so
    // z_axis_reference_view[i] is signalled per view), z_near + d_min
    // both signalled. Verifies the float-element wiring and the
    // per-view branch selectors.
    #[test]
    fn depth_representation_info_two_views_per_view_z_axis() {
        // num_views_minus1 = 1 → ue codeword "010" (3 bits).
        // depth_representation_type = 2 → ue codeword "011" (3 bits).
        // Each view: depth_info_view_id = 0 → "1";
        //            z_axis_reference_view = 0 → "1";
        //            disparity_reference_view = 0 → "1";
        //            ZNear float: sign=0, exp=63, mlen_minus1=0, mantissa=0
        //              (1 + 7 + 5 + 1 = 14 bits)
        //            DMin float : sign=1, exp=31, mlen_minus1=0, mantissa=1
        //              (1 + 7 + 5 + 1 = 14 bits)
        let view = [
            (1, 1),  // depth_info_view_id[i] = 0
            (1, 1),  // z_axis_reference_view[i] = 0
            (1, 1),  // disparity_reference_view[i] = 0
            (0, 1),  // ZNear sign
            (63, 7), // ZNear exponent
            (0, 5),  // ZNear mantissa_len_minus1 → width 1
            (0, 1),  // ZNear mantissa
            (1, 1),  // DMin sign (negative)
            (31, 7), // DMin exponent
            (0, 5),  // DMin mantissa_len_minus1 → width 1
            (1, 1),  // DMin mantissa
        ];
        let mut fields: Vec<(u64, u32)> = vec![
            (0, 1),     // all_views_equal_flag = 0
            (0b010, 3), // num_views_minus1 = 1
            (1, 1),     // z_near_flag = 1
            (0, 1),     // z_far_flag = 0
            (0, 1),     // z_axis_equal_flag = 0
            (1, 1),     // d_min_flag = 1
            (0, 1),     // d_max_flag = 0
            (0b011, 3), // depth_representation_type = 2
        ];
        for _ in 0..2 {
            fields.extend_from_slice(&view);
        }
        let payload = pack_bits(&fields);
        let info = parse_depth_representation_info(&payload).unwrap();
        assert!(!info.all_views_equal_flag);
        assert_eq!(info.views.len(), 2);
        assert_eq!(info.z_axis_equal_flag, Some(false));
        assert!(info.common_z_axis_reference_view.is_none());
        assert_eq!(info.depth_representation_type, 2);
        for v in &info.views {
            assert_eq!(v.depth_info_view_id, 0);
            assert_eq!(v.z_axis_reference_view, Some(0));
            assert_eq!(v.disparity_reference_view, Some(0));
            let zn = v.z_near.expect("ZNear present when z_near_flag = 1");
            assert!(!zn.sign);
            assert_eq!(zn.exponent, 63);
            assert_eq!(zn.mantissa_width, 1);
            assert!(v.z_far.is_none());
            let dn = v.d_min.expect("DMin present when d_min_flag = 1");
            assert!(dn.sign);
            assert_eq!(dn.exponent, 31);
            assert_eq!(dn.mantissa_width, 1);
            assert_eq!(dn.mantissa, 1);
            assert!(v.d_max.is_none());
        }
        // Type 2 ≠ 3 — no nonlinear tail.
        assert!(info.depth_nonlinear_representation_num_minus1.is_none());
    }

    // §H.13.2.3 — depth_representation_type == 3 triggers the
    // nonlinear-model tail. num_minus1 = 0 ⇒ one entry signalled.
    #[test]
    fn depth_representation_info_nonlinear_type_3_tail() {
        let payload = pack_bits(&[
            (1, 1),       // all_views_equal_flag = 1
            (1, 1),       // z_near_flag = 1
            (1, 1),       // z_far_flag = 1
            (1, 1),       // z_axis_equal_flag = 1
            (1, 1),       // common_z_axis_reference_view = 0 → "1"
            (0, 1),       // d_min_flag = 0
            (0, 1),       // d_max_flag = 0
            (0b00100, 5), // depth_representation_type = 3
            (1, 1),       // depth_info_view_id[0] = 0
            // ZNear float: sign=0, exp=1, mlen_minus1=0, mantissa=0
            (0, 1),
            (1, 7),
            (0, 5),
            (0, 1),
            // ZFar float: sign=0, exp=2, mlen_minus1=0, mantissa=0
            (0, 1),
            (2, 7),
            (0, 5),
            (0, 1),
            // depth_nonlinear_representation_num_minus1 = 0 → "1"
            (1, 1),
            // depth_nonlinear_representation_model[0] = 4 → "00101"
            (0b00101, 5),
        ]);
        let info = parse_depth_representation_info(&payload).unwrap();
        assert!(info.all_views_equal_flag);
        assert_eq!(info.depth_representation_type, 3);
        assert_eq!(info.z_axis_equal_flag, Some(true));
        assert_eq!(info.common_z_axis_reference_view, Some(0));
        assert_eq!(info.views.len(), 1);
        assert_eq!(info.depth_nonlinear_representation_num_minus1, Some(0));
        assert_eq!(info.depth_nonlinear_representation_model, vec![4]);
    }

    // §H.13.2.3 — typed accessor for the
    // DepthNonlinearRepresentationNumSegments semantic value: when
    // the field is unsignalled (depth_representation_type != 3) the
    // accessor returns None — the §H.13.2.3 piecewise model is not
    // defined for non-3 types so there is no inferred value.
    #[test]
    fn depth_representation_info_num_segments_returns_none_when_absent() {
        let info = DepthRepresentationInfo {
            all_views_equal_flag: true,
            z_near_flag: false,
            z_far_flag: false,
            z_axis_equal_flag: None,
            common_z_axis_reference_view: None,
            d_min_flag: false,
            d_max_flag: false,
            depth_representation_type: 2,
            views: Vec::new(),
            depth_nonlinear_representation_num_minus1: None,
            depth_nonlinear_representation_model: Vec::new(),
        };
        assert_eq!(info.depth_nonlinear_representation_num_segments(), None);
        assert_eq!(info.depth_nonlinear_representation_model_len(), None);
    }

    // §H.13.2.3 — segment-count accessor decodes the `+ 2` bias on
    // a num_minus1 = 0 carrier (the minimum permitted value). The
    // signalled-model-len accessor reports 1 entry for the same
    // carrier.
    #[test]
    fn depth_representation_info_num_segments_minimum() {
        let info = DepthRepresentationInfo {
            all_views_equal_flag: true,
            z_near_flag: false,
            z_far_flag: false,
            z_axis_equal_flag: None,
            common_z_axis_reference_view: None,
            d_min_flag: false,
            d_max_flag: false,
            depth_representation_type: 3,
            views: Vec::new(),
            depth_nonlinear_representation_num_minus1: Some(0),
            depth_nonlinear_representation_model: vec![0],
        };
        assert_eq!(info.depth_nonlinear_representation_num_segments(), Some(2));
        assert_eq!(info.depth_nonlinear_representation_model_len(), Some(1));
    }

    // §H.13.2.3 — segment-count accessor decodes the `+ 2` bias on
    // an interior carrier (num_minus1 = 3 ⇒ 5 segments, 4 signalled
    // model entries).
    #[test]
    fn depth_representation_info_num_segments_interior() {
        let info = DepthRepresentationInfo {
            all_views_equal_flag: true,
            z_near_flag: false,
            z_far_flag: false,
            z_axis_equal_flag: None,
            common_z_axis_reference_view: None,
            d_min_flag: false,
            d_max_flag: false,
            depth_representation_type: 3,
            views: Vec::new(),
            depth_nonlinear_representation_num_minus1: Some(3),
            depth_nonlinear_representation_model: vec![10, 20, 30, 40],
        };
        assert_eq!(info.depth_nonlinear_representation_num_segments(), Some(5));
        assert_eq!(info.depth_nonlinear_representation_model_len(), Some(4));
    }

    // §H.13.2.3 — segment-count accessor decodes the `+ 2` bias at
    // the maximum on-wire value (num_minus1 = 62 ⇒ 64 segments,
    // 63 signalled model entries). Confirms the u8 return type
    // accommodates the upper endpoint without overflow.
    #[test]
    fn depth_representation_info_num_segments_maximum() {
        let info = DepthRepresentationInfo {
            all_views_equal_flag: true,
            z_near_flag: false,
            z_far_flag: false,
            z_axis_equal_flag: None,
            common_z_axis_reference_view: None,
            d_min_flag: false,
            d_max_flag: false,
            depth_representation_type: 3,
            views: Vec::new(),
            depth_nonlinear_representation_num_minus1: Some(62),
            depth_nonlinear_representation_model: vec![0; 63],
        };
        assert_eq!(info.depth_nonlinear_representation_num_segments(), Some(64));
        assert_eq!(info.depth_nonlinear_representation_model_len(), Some(63));
    }

    // §H.13.2.3 — segment count and signalled-model length must be
    // consistent with the stored `depth_nonlinear_representation_model`
    // length: the spec relation
    //
    //   model_len() == depth_nonlinear_representation_model.len()
    //   num_segments() == model_len() + 1
    //
    // holds end-to-end through the parser on the existing
    // type-3 fixture (num_minus1 = 0 ⇒ 1 model entry, 2 segments).
    #[test]
    fn depth_representation_info_num_segments_round_trip_via_parser() {
        let payload = pack_bits(&[
            (1, 1),       // all_views_equal_flag = 1
            (1, 1),       // z_near_flag = 1
            (1, 1),       // z_far_flag = 1
            (1, 1),       // z_axis_equal_flag = 1
            (1, 1),       // common_z_axis_reference_view = 0 → "1"
            (0, 1),       // d_min_flag = 0
            (0, 1),       // d_max_flag = 0
            (0b00100, 5), // depth_representation_type = 3
            (1, 1),       // depth_info_view_id[0] = 0
            // ZNear float: sign=0, exp=1, mlen_minus1=0, mantissa=0
            (0, 1),
            (1, 7),
            (0, 5),
            (0, 1),
            // ZFar float: sign=0, exp=2, mlen_minus1=0, mantissa=0
            (0, 1),
            (2, 7),
            (0, 5),
            (0, 1),
            // depth_nonlinear_representation_num_minus1 = 0 → "1"
            (1, 1),
            // depth_nonlinear_representation_model[0] = 4 → "00101"
            (0b00101, 5),
        ]);
        let info = parse_depth_representation_info(&payload).unwrap();
        let model_len = info
            .depth_nonlinear_representation_model_len()
            .expect("type-3 payload signals model_len");
        let num_segments = info
            .depth_nonlinear_representation_num_segments()
            .expect("type-3 payload signals num_segments");
        assert_eq!(
            usize::from(model_len),
            info.depth_nonlinear_representation_model.len()
        );
        assert_eq!(num_segments, model_len + 1);
        assert_eq!(model_len, 1);
        assert_eq!(num_segments, 2);
    }

    // §H.13.2.3 — values 4..=15 of depth_representation_type are
    // reserved; the parser shall accept them and emit no nonlinear
    // tail.
    #[test]
    fn depth_representation_info_reserved_type_skips_nonlinear_tail() {
        let payload = pack_bits(&[
            (1, 1),       // all_views_equal_flag = 1
            (0, 1),       // z_near_flag = 0
            (0, 1),       // z_far_flag = 0
            (0, 1),       // d_min_flag = 0
            (0, 1),       // d_max_flag = 0
            (0b00101, 5), // depth_representation_type = 4 (reserved)
            (1, 1),       // depth_info_view_id[0] = 0
        ]);
        let info = parse_depth_representation_info(&payload).unwrap();
        assert_eq!(info.depth_representation_type, 4);
        assert!(info.depth_nonlinear_representation_num_minus1.is_none());
    }

    // §H.13.2.3 — num_views_minus1 > 1023 must be rejected before
    // allocation.
    #[test]
    fn depth_representation_info_rejects_num_views_above_1023() {
        // ue codeword for 1024 = "00000000001 0000000001" =
        // 10 zero prefix + 11-bit binary 0b10000000001 = 21 bits.
        // all_views_equal_flag = 0 + that ue codeword.
        // codeNum 1024 = ue codeword: 10 leading zeros + binary
        // representation of (1024 + 1) = 0b10000000001 (11 bits).
        let payload = pack_bits(&[
            (0, 1), // all_views_equal_flag = 0
            (0, 10),
            (0b10000000001, 11),
        ]);
        let err = parse_depth_representation_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::DepthRepresentationInfoNumViewsOutOfRange(1024)
        ));
    }

    // §H.13.2.3 — common_z_axis_reference_view > 1023 rejected.
    #[test]
    fn depth_representation_info_rejects_common_z_axis_above_1023() {
        let payload = pack_bits(&[
            (1, 1), // all_views_equal_flag = 1
            (1, 1), // z_near_flag = 1
            (0, 1), // z_far_flag = 0
            (1, 1), // z_axis_equal_flag = 1
            // common_z_axis_reference_view = 1024 → ue codeword as above.
            (0, 10),
            (0b10000000001, 11),
        ]);
        let err = parse_depth_representation_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::DepthRepresentationInfoViewIdOutOfRange {
                field: "common_z_axis_reference_view",
                got: 1024
            }
        ));
    }

    // §H.13.2.3 — depth_nonlinear_representation_num_minus1 > 62
    // rejected before allocation.
    #[test]
    fn depth_representation_info_rejects_nonlinear_num_above_62() {
        // Build a minimal type-3 prelude, then a num_minus1 = 63
        // codeword. ue codeword for 63 = "0000010000000" — 6 zeros +
        // 7-bit binary 0b1000000 (= codeNum+1 = 64).
        let payload = pack_bits(&[
            (1, 1),       // all_views_equal_flag = 1
            (0, 1),       // z_near_flag = 0
            (0, 1),       // z_far_flag = 0
            (0, 1),       // d_min_flag = 0
            (0, 1),       // d_max_flag = 0
            (0b00100, 5), // depth_representation_type = 3
            (1, 1),       // depth_info_view_id[0] = 0
            (0, 6),
            (0b1000000, 7),
        ]);
        let err = parse_depth_representation_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::DepthRepresentationInfoNonlinearNumOutOfRange(63)
        ));
    }

    // §H.13.1.3.1 / Table H-2 — verify the DepthFloatComponent::to_f64
    // arithmetic.
    #[test]
    fn depth_float_component_to_f64_branches() {
        // Normal with e = 31, v = 1, n = 0 → 2^0 * 1 = 1.0
        let one = DepthFloatComponent {
            sign: false,
            exponent: 31,
            mantissa: 0,
            mantissa_width: 1,
        };
        assert_eq!(one.to_f64(), 1.0);
        // Normal with e = 32, v = 1, n = 1 → 2^1 * (1 + 0.5) = 3.0
        let three = DepthFloatComponent {
            sign: false,
            exponent: 32,
            mantissa: 1,
            mantissa_width: 1,
        };
        assert_eq!(three.to_f64(), 3.0);
        // Negative-sign sentinel: same magnitude, opposite sign.
        let minus_three = DepthFloatComponent {
            sign: true,
            exponent: 32,
            mantissa: 1,
            mantissa_width: 1,
        };
        assert_eq!(minus_three.to_f64(), -3.0);
        // Denormal e = 0, n = 1, v = 1 → 2^-30 * (1/2) = 2^-31
        let dn = DepthFloatComponent {
            sign: false,
            exponent: 0,
            mantissa: 1,
            mantissa_width: 1,
        };
        assert!((dn.to_f64() - 2f64.powi(-31)).abs() < 1e-300);
        // Reserved e = 127 → NaN.
        let res = DepthFloatComponent {
            sign: false,
            exponent: 127,
            mantissa: 0,
            mantissa_width: 1,
        };
        assert!(res.to_f64().is_nan());
    }

    // §H.13.2.3 — dispatch through the public parse_payload entry
    // point. Same fixture as the minimum-syntax test above; we just
    // verify the match arm selects the right variant.
    #[test]
    fn parse_payload_dispatches_depth_representation_info() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[
            (1, 1), // all_views_equal_flag = 1
            (0, 1), // z_near_flag = 0
            (0, 1), // z_far_flag = 0
            (0, 1), // d_min_flag = 0
            (0, 1), // d_max_flag = 0
            (1, 1), // depth_representation_type = 0
            (1, 1), // depth_info_view_id[0] = 0
        ]);
        let got = parse_payload(50, &payload, &ctx).unwrap();
        match got {
            SeiPayload::DepthRepresentationInfo(info) => {
                assert_eq!(info.views.len(), 1);
                assert_eq!(info.depth_representation_type, 0);
            }
            other => panic!("expected DepthRepresentationInfo, got {other:?}"),
        }
    }

    // ==============================================================
    // §I.13.2.1 — constrained_depth_parameter_set_identifier (payload
    // type 54, Annex I 3D-AVC depth coding). Round 237.
    // ==============================================================

    // Smallest legal fixture: max_dps_id = 1 (ue codeword "010"),
    // max_dps_id_diff = 0 (ue codeword "1"). 0 * 2 < 1 holds.
    #[test]
    fn constrained_depth_parameter_set_identifier_min_legal() {
        let payload = pack_bits(&[
            (0b010, 3), // ue(1) = 1 → max_dps_id
            (1, 1),     // ue(0) = 0 → max_dps_id_diff
        ]);
        let got = parse_constrained_depth_parameter_set_identifier(&payload).unwrap();
        assert_eq!(got.max_dps_id, 1);
        assert_eq!(got.max_dps_id_diff, 0);
    }

    // Mid-range fixture: max_dps_id = 7 (ue codeword "0001000"),
    // max_dps_id_diff = 3 (ue codeword "00100"). 3 * 2 = 6 < 7 holds.
    #[test]
    fn constrained_depth_parameter_set_identifier_mid_range() {
        let payload = pack_bits(&[
            (0b0001000, 7), // ue(7) = 7 → max_dps_id
            (0b00100, 5),   // ue(3) = 3 → max_dps_id_diff
        ]);
        let got = parse_constrained_depth_parameter_set_identifier(&payload).unwrap();
        assert_eq!(got.max_dps_id, 7);
        assert_eq!(got.max_dps_id_diff, 3);
    }

    // Maximum legal fixture: max_dps_id = 62, max_dps_id_diff = 30.
    // 30 * 2 = 60 < 62 holds. ue(62) = leadingZeros 5, suffix = 62 -
    // (2^5 - 1) = 31 in 5 bits → "000001" + "11111" = "00000111111"
    // (11 bits). ue(30) = leadingZeros 4, suffix = 30 - (2^4 - 1) =
    // 15 in 4 bits → "00001" + "1111" = "000011111" (9 bits).
    #[test]
    fn constrained_depth_parameter_set_identifier_max_legal() {
        let payload = pack_bits(&[
            (0b00000111111, 11), // ue(62)
            (0b000011111, 9),    // ue(30)
        ]);
        let got = parse_constrained_depth_parameter_set_identifier(&payload).unwrap();
        assert_eq!(got.max_dps_id, 62);
        assert_eq!(got.max_dps_id_diff, 30);
    }

    // max_dps_id = 63 (ue codeword "0000001000000") is out of range:
    // the §I.13.2.1 derivation `max_dps_id + 1 ≤ 63` requires
    // max_dps_id ≤ 62 (depth_parameter_set_id range 1..=63 per
    // §7.4.2.16). ue(63): leadingZeros 6, suffix = 63 - (2^6 - 1) = 0
    // in 6 bits → "0000001" + "000000" = "0000001000000" (13 bits).
    #[test]
    fn constrained_depth_parameter_set_identifier_rejects_max_dps_id_above_62() {
        let payload = pack_bits(&[(0b0000001000000, 13)]);
        let err = parse_constrained_depth_parameter_set_identifier(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ConstrainedDepthParameterSetIdentifierMaxDpsIdOutOfRange(63)
        );
    }

    // max_dps_id = 4, max_dps_id_diff = 2 → 2 * 2 = 4 NOT < 4.
    // Violates the §I.13.2.1 sliding-window-integrity constraint.
    #[test]
    fn constrained_depth_parameter_set_identifier_rejects_diff_equals_half_max() {
        let payload = pack_bits(&[
            (0b00101, 5), // ue(4) = 4 → max_dps_id
            (0b011, 3),   // ue(2) = 2 → max_dps_id_diff
        ]);
        let err = parse_constrained_depth_parameter_set_identifier(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ConstrainedDepthParameterSetIdentifierDiffViolatesBound {
                max_dps_id_diff: 2,
                max_dps_id: 4,
            }
        );
    }

    // max_dps_id = 0, max_dps_id_diff = 0 → 0 * 2 = 0 NOT < 0.
    // Edge case: the spec's strict-less-than relation rules out
    // even a "no diff" pair when max_dps_id is also zero.
    #[test]
    fn constrained_depth_parameter_set_identifier_rejects_zero_pair() {
        let payload = pack_bits(&[
            (1, 1), // ue(0) = 0 → max_dps_id
            (1, 1), // ue(0) = 0 → max_dps_id_diff
        ]);
        let err = parse_constrained_depth_parameter_set_identifier(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ConstrainedDepthParameterSetIdentifierDiffViolatesBound {
                max_dps_id_diff: 0,
                max_dps_id: 0,
            }
        );
    }

    // §I.13.2.1 — dispatch through the public parse_payload entry
    // point. Same fixture as the min-legal test above; the match arm
    // selects the right variant.
    #[test]
    fn parse_payload_dispatches_constrained_depth_parameter_set_identifier() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[
            (0b010, 3), // ue(1) = 1 → max_dps_id
            (1, 1),     // ue(0) = 0 → max_dps_id_diff
        ]);
        let got = parse_payload(54, &payload, &ctx).unwrap();
        match got {
            SeiPayload::ConstrainedDepthParameterSetIdentifier(info) => {
                assert_eq!(info.max_dps_id, 1);
                assert_eq!(info.max_dps_id_diff, 0);
            }
            other => panic!("expected ConstrainedDepthParameterSetIdentifier, got {other:?}"),
        }
    }

    // §H.13.2.7 — minimal depth_sampling_info: shared
    // depth_grid_position() (per_view_depth_grid_pos_flag == 0).
    // dttsr_{x,y}_mul = 1 / dp = 0 means a 1:1 sample-size ratio.
    // The shared depth_grid_position() carries a zero position with
    // both fractional widths set to 0.
    #[test]
    fn depth_sampling_info_min_shared_position() {
        let payload = pack_bits(&[
            (1, 16), // dttsr_x_mul = 1
            (0, 4),  // dttsr_x_dp = 0
            (1, 16), // dttsr_y_mul = 1
            (0, 4),  // dttsr_y_dp = 0
            (0, 1),  // per_view_depth_grid_pos_flag = 0
            (0, 20), // depth_grid_pos_x_fp = 0
            (0, 4),  // depth_grid_pos_x_dp = 0
            (0, 1),  // depth_grid_pos_x_sign_flag = 0
            (0, 20), // depth_grid_pos_y_fp = 0
            (0, 4),  // depth_grid_pos_y_dp = 0
            (0, 1),  // depth_grid_pos_y_sign_flag = 0
        ]);
        let info = parse_depth_sampling_info(&payload).unwrap();
        assert_eq!(info.dttsr_x_mul, 1);
        assert_eq!(info.dttsr_x_dp, 0);
        assert_eq!(info.dttsr_y_mul, 1);
        assert_eq!(info.dttsr_y_dp, 0);
        assert!(!info.per_view_depth_grid_pos_flag);
        assert!(info.num_video_plus_depth_views_minus1.is_none());
        assert_eq!(info.views.len(), 1);
        assert_eq!(info.views[0].depth_grid_view_id, 0);
        assert_eq!(info.views[0].position.pos_x_fp, 0);
        assert_eq!(info.views[0].position.pos_y_fp, 0);
        assert!((info.dttsr_x_to_f64() - 1.0).abs() < 1e-9);
        assert!((info.dttsr_y_to_f64() - 1.0).abs() < 1e-9);
        assert!((info.views[0].position.x_to_f64() - 0.0).abs() < 1e-9);
    }

    // §H.13.2.7 — per-view depth_sampling_info with two views. Each
    // view's depth_grid_position() carries a distinct fp / dp /
    // sign-bit triple so we can confirm the per-view storage isn't
    // accidentally shared.
    #[test]
    fn depth_sampling_info_per_view_two_views() {
        let payload = pack_bits(&[
            (3, 16),     // dttsr_x_mul = 3
            (0b0001, 4), // dttsr_x_dp = 1
            (3, 16),     // dttsr_y_mul = 3
            (0b0001, 4), // dttsr_y_dp = 1
            (1, 1),      // per_view_depth_grid_pos_flag = 1
            (0b010, 3),  // ue(1) = 1 → num_video_plus_depth_views_minus1
            // view 0: depth_grid_view_id = 0 (ue "1")
            (1, 1),
            (0b0000_0000_0000_0001_0000u32 as u64, 20), // pos_x_fp = 16
            (0b0011, 4),                                // pos_x_dp = 3
            (0, 1),                                     // pos_x_sign = 0
            (0b0000_0000_0000_0000_1000u32 as u64, 20), // pos_y_fp = 8
            (0b0010, 4),                                // pos_y_dp = 2
            (1, 1),                                     // pos_y_sign = 1
            // view 1: depth_grid_view_id = 5 (ue codenum 5 = "00110")
            (0b00110, 5),
            (0b0000_0000_0000_0010_0000u32 as u64, 20), // pos_x_fp = 32
            (0b0100, 4),                                // pos_x_dp = 4
            (1, 1),                                     // pos_x_sign = 1
            (0b0000_0000_0000_0001_0000u32 as u64, 20), // pos_y_fp = 16
            (0b0011, 4),                                // pos_y_dp = 3
            (0, 1),                                     // pos_y_sign = 0
        ]);
        let info = parse_depth_sampling_info(&payload).unwrap();
        assert!(info.per_view_depth_grid_pos_flag);
        assert_eq!(info.num_video_plus_depth_views_minus1, Some(1));
        assert_eq!(info.views.len(), 2);
        assert_eq!(info.views[0].depth_grid_view_id, 0);
        assert_eq!(info.views[0].position.pos_x_fp, 16);
        assert_eq!(info.views[0].position.pos_x_dp, 3);
        assert!(!info.views[0].position.pos_x_sign);
        assert_eq!(info.views[0].position.pos_y_fp, 8);
        assert!(info.views[0].position.pos_y_sign);
        assert_eq!(info.views[1].depth_grid_view_id, 5);
        assert_eq!(info.views[1].position.pos_x_fp, 32);
        assert!(info.views[1].position.pos_x_sign);
        assert_eq!(info.views[1].position.pos_y_dp, 3);
        // Reconstructed scalar: view 0 y = -(8/4) = -2.0
        assert!((info.views[0].position.y_to_f64() + 2.0).abs() < 1e-9);
        // view 1 x = -(32/16) = -2.0
        assert!((info.views[1].position.x_to_f64() + 2.0).abs() < 1e-9);
        // dttsr_x = 3/2 = 1.5
        assert!((info.dttsr_x_to_f64() - 1.5).abs() < 1e-9);
    }

    // §H.13.2.7 — dttsr_x_mul = 0 is reserved; reject.
    #[test]
    fn depth_sampling_info_rejects_dttsr_x_mul_zero() {
        let payload = pack_bits(&[
            (0, 16), // dttsr_x_mul = 0 (reserved)
            (0, 4),
            (1, 16),
            (0, 4),
            (0, 1),
        ]);
        let err = parse_depth_sampling_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::DepthSamplingInfoDttsrMulReserved { axis: "x" }
        ));
    }

    // §H.13.2.7 — dttsr_y_mul = 0 is reserved; reject.
    #[test]
    fn depth_sampling_info_rejects_dttsr_y_mul_zero() {
        let payload = pack_bits(&[
            (1, 16), // dttsr_x_mul = 1
            (0, 4),
            (0, 16), // dttsr_y_mul = 0 (reserved)
            (0, 4),
            (0, 1),
        ]);
        let err = parse_depth_sampling_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::DepthSamplingInfoDttsrMulReserved { axis: "y" }
        ));
    }

    // §H.13.2.7 — num_video_plus_depth_views_minus1 > 1023 rejected
    // before allocation.
    #[test]
    fn depth_sampling_info_rejects_num_views_above_1023() {
        // ue codeNum 1024 = 10 leading zeros + 11-bit binary
        // (1024 + 1) = 0b10000000001.
        let payload = pack_bits(&[
            (1, 16), // dttsr_x_mul = 1
            (0, 4),
            (1, 16), // dttsr_y_mul = 1
            (0, 4),
            (1, 1), // per_view_depth_grid_pos_flag = 1
            (0, 10),
            (0b10000000001, 11),
        ]);
        let err = parse_depth_sampling_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::DepthSamplingInfoNumViewsOutOfRange(1024)
        ));
    }

    // §H.13.2.7 — depth_grid_view_id[i] > 1023 rejected; the i index
    // is preserved in the error so a regression points at the failed
    // entry without re-running the parser.
    #[test]
    fn depth_sampling_info_rejects_view_id_above_1023() {
        let payload = pack_bits(&[
            (1, 16), // dttsr_x_mul = 1
            (0, 4),
            (1, 16), // dttsr_y_mul = 1
            (0, 4),
            (1, 1),     // per_view_depth_grid_pos_flag = 1
            (0b010, 3), // ue(1) = 1 → num_views_minus1 = 1
            // view 0: view_id = 1024 (out of range).
            (0, 10),
            (0b10000000001, 11),
        ]);
        let err = parse_depth_sampling_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::DepthSamplingInfoViewIdOutOfRange { i: 0, got: 1024 }
        ));
    }

    // §H.13.1.7 dispatch — parse_payload(53, ..) must route to
    // parse_depth_sampling_info.
    #[test]
    fn parse_payload_dispatches_depth_sampling_info() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[
            (2, 16), // dttsr_x_mul = 2
            (0, 4),
            (2, 16), // dttsr_y_mul = 2
            (0, 4),
            (0, 1),  // per_view_depth_grid_pos_flag = 0
            (0, 20), // depth_grid_pos_x_fp
            (0, 4),
            (0, 1),
            (0, 20), // depth_grid_pos_y_fp
            (0, 4),
            (0, 1),
        ]);
        let got = parse_payload(53, &payload, &ctx).unwrap();
        match got {
            SeiPayload::DepthSamplingInfo(info) => {
                assert_eq!(info.dttsr_x_mul, 2);
                assert_eq!(info.dttsr_y_mul, 2);
                assert!(!info.per_view_depth_grid_pos_flag);
                assert_eq!(info.views.len(), 1);
            }
            other => panic!("expected DepthSamplingInfo, got {other:?}"),
        }
    }

    // §H.13.1.6 — alternative_depth_info with depth_type == 0 and all
    // four GVD presence flags clear: the message reduces to depth_type +
    // num_constituent_views_gvd_minus1 + the five flags, then one empty
    // per-camera entry per camera (no intrinsic / rotation / translation
    // sub-blocks, no z block). num_constituent_views_gvd_minus1 = 1 →
    // 1 + 2 = 3 cameras.
    #[test]
    fn alternative_depth_info_minimal_flags_clear() {
        let payload = pack_bits(&[
            (0b1, 1),   // depth_type ue(0) = 0
            (0b010, 3), // num_constituent_views_gvd_minus1 ue(1) = 1
            (0, 1),     // depth_present_gvd_flag = 0
            (0, 1),     // z_gvd_flag = 0
            (0, 1),     // intrinsic_param_gvd_flag = 0
            (0, 1),     // rotation_gvd_flag = 0
            (0, 1),     // translation_gvd_flag = 0
        ]);
        let info = parse_alternative_depth_info(&payload).unwrap();
        assert_eq!(info.depth_type, 0);
        let gvd = info.gvd.expect("depth_type 0 carries a gvd block");
        assert_eq!(gvd.num_constituent_views_gvd_minus1, 1);
        assert!(!gvd.depth_present_gvd_flag);
        assert!(!gvd.z_gvd_flag);
        assert!(gvd.z_values.is_empty());
        assert_eq!(gvd.prec_gvd_focal_length, None);
        assert_eq!(gvd.prec_gvd_rotation_param, None);
        // 1 + 2 = 3 cameras, each with no sub-blocks.
        assert_eq!(gvd.cameras.len(), 3);
        for cam in &gvd.cameras {
            assert!(cam.intrinsic.is_none());
            assert!(cam.rotation.is_none());
            assert!(cam.translation_x.is_none());
        }
    }

    // §H.13.2.6 — a non-zero depth_type is reserved; the decoder records
    // the type and ignores the body (no error, gvd == None).
    #[test]
    fn alternative_depth_info_nonzero_type_ignored() {
        // depth_type = 2 → ue(2) = 0b011 (3 bits). Trailing garbage is
        // not read because the body is skipped.
        let payload = pack_bits(&[(0b011, 3), (0xFF, 8)]);
        let info = parse_alternative_depth_info(&payload).unwrap();
        assert_eq!(info.depth_type, 2);
        assert!(info.gvd.is_none());
    }

    // §H.13.2.6 — num_constituent_views_gvd_minus1 must be in 0..=3.
    #[test]
    fn alternative_depth_info_num_views_out_of_range() {
        // depth_type = 0, num_constituent_views_gvd_minus1 = 4 →
        // ue(4) = 0b00101 (5 bits). 4 > 3 must error.
        let payload = pack_bits(&[(0b1, 1), (0b00101, 5)]);
        let err = parse_alternative_depth_info(&payload).unwrap_err();
        assert!(matches!(
            err,
            SeiError::AlternativeDepthInfoNumConstituentViewsOutOfRange(4)
        ));
    }

    // §H.13.2.6 — z_gvd_flag set: a near/far DepthFloatComponent pair per
    // camera (sign u(1), exp u(7), man_len_minus1 u(5), man u(v)). One
    // constituent view (minus1 = 0 → 2 cameras). exp == 0 with a small
    // mantissa exercises the denormal eq. H-1 path; the second camera
    // uses exp == 127 (reserved → NaN).
    #[test]
    fn alternative_depth_info_z_block_two_cameras() {
        let payload = pack_bits(&[
            (0b1, 1), // depth_type = 0
            (0b1, 1), // num_constituent_views_gvd_minus1 ue(0) = 0 → 2 cams
            (0, 1),   // depth_present_gvd_flag = 0
            (1, 1),   // z_gvd_flag = 1
            (0, 1),   // intrinsic_param_gvd_flag = 0
            (0, 1),   // rotation_gvd_flag = 0
            (0, 1),   // translation_gvd_flag = 0
            // camera 0 z_near: sign 0, exp 0 (denormal), man_len_minus1 3
            // (→ width 4), mantissa 0b1000 = 8.
            (0, 1),
            (0, 7),
            (3, 5),
            (0b1000, 4),
            // camera 0 z_far: sign 1, exp 64 (normal), man_len_minus1 0
            // (→ width 1), mantissa 1.
            (1, 1),
            (64, 7),
            (0, 5),
            (1, 1),
            // camera 1 z_near: sign 0, exp 127 (reserved), man_len_minus1
            // 0 (→ width 1), mantissa 0.
            (0, 1),
            (127, 7),
            (0, 5),
            (0, 1),
            // camera 1 z_far: sign 0, exp 1, man_len_minus1 1 (width 2),
            // mantissa 0b10 = 2.
            (0, 1),
            (1, 7),
            (1, 5),
            (0b10, 2),
        ]);
        let info = parse_alternative_depth_info(&payload).unwrap();
        let gvd = info.gvd.unwrap();
        assert!(gvd.z_gvd_flag);
        assert_eq!(gvd.z_values.len(), 2);

        let z0 = gvd.z_values[0];
        // z_near denormal: (-1)^0 * 2^-30 * (8 / 2^4) = 2^-30 * 0.5.
        assert!((z0.z_near.to_f64() - 2f64.powi(-30) * 0.5).abs() < 1e-18);
        // z_far normal: (-1)^1 * 2^(64-31) * (1 + 1/2^1) = -2^33 * 1.5.
        assert!((z0.z_far.to_f64() - (-(2f64.powi(33)) * 1.5)).abs() < 1.0);

        let z1 = gvd.z_values[1];
        // z_near exp == 127 → unspecified (NaN).
        assert!(z1.z_near.to_f64().is_nan());
        // z_far normal: 2^(1-31) * (1 + 2/2^2) = 2^-30 * 1.5.
        assert!((z1.z_far.to_f64() - 2f64.powi(-30) * 1.5).abs() < 1e-18);
    }

    // §H.13.2.6 — intrinsic + rotation + translation blocks for a single
    // camera (num_constituent_views_gvd_minus1 = 0 → 2 cameras). Uses
    // the §G.13.2.5 FloatComponent layout (exp u(6), reserved 63,
    // prec-derived mantissa width). prec = 31 with exp = 31 gives a
    // mantissa width of 31 + 31 - 31 = 31 bits.
    #[test]
    fn alternative_depth_info_intrinsic_rotation_translation() {
        // Build the per-camera float as sign u(1) + exp u(6) +
        // man u(width). With prec = 0 and exp in 1..=31, width =
        // max(0, exp + 0 - 31) = 0 for exp <= 31, so no mantissa bits.
        // That keeps the test compact: every float is just sign + exp.
        let mut fields: Vec<(u64, u32)> = vec![
            (0b1, 1), // depth_type = 0
            (0b1, 1), // num_constituent_views_gvd_minus1 ue(0) = 0 → 2 cams
            (0, 1),   // depth_present_gvd_flag = 0
            (0, 1),   // z_gvd_flag = 0
            (1, 1),   // intrinsic_param_gvd_flag = 1
            (1, 1),   // rotation_gvd_flag = 1
            (1, 1),   // translation_gvd_flag = 1
            // prec_gvd_focal_length ue(0) = 0
            (0b1, 1),
            // prec_gvd_principal_point ue(0) = 0
            (0b1, 1),
            // prec_gvd_rotation_param ue(0) = 0
            (0b1, 1),
            // prec_gvd_translation_param ue(0) = 0
            (0b1, 1),
        ];
        // For each of the 2 cameras: 4 intrinsic floats + 9 rotation
        // floats + 1 translation float = 14 floats, each sign u(1) +
        // exp u(6), no mantissa (width 0 because prec = 0, exp <= 62
        // small).
        for cam in 0..2u64 {
            for f in 0..14u64 {
                let exp = (cam * 14 + f) % 30 + 1; // 1..=30
                fields.push((0, 1)); // sign
                fields.push((exp, 6)); // exp u(6)
            }
        }
        let payload = pack_bits(&fields);
        let info = parse_alternative_depth_info(&payload).unwrap();
        let gvd = info.gvd.unwrap();
        assert_eq!(gvd.prec_gvd_focal_length, Some(0));
        assert_eq!(gvd.prec_gvd_rotation_param, Some(0));
        assert_eq!(gvd.prec_gvd_translation_param, Some(0));
        assert_eq!(gvd.cameras.len(), 2);
        for cam in &gvd.cameras {
            let intr = cam.intrinsic.expect("intrinsic present");
            // exp values were 1..=30, width 0 → mantissa 0, normal float.
            assert_eq!(intr.focal_length_x.mantissa_width, 0);
            let rot = cam.rotation.expect("rotation present");
            assert_eq!(rot.len(), 3);
            assert_eq!(rot[0].len(), 3);
            assert!(cam.translation_x.is_some());
        }
    }

    // §H.13.1.6 dispatch — parse_payload(181, ..) routes to
    // parse_alternative_depth_info.
    #[test]
    fn parse_payload_dispatches_alternative_depth_info() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[
            (0b1, 1), // depth_type = 0
            (0b1, 1), // num_constituent_views_gvd_minus1 = 0
            (0, 1),   // depth_present_gvd_flag
            (0, 1),   // z_gvd_flag
            (0, 1),   // intrinsic_param_gvd_flag
            (0, 1),   // rotation_gvd_flag
            (0, 1),   // translation_gvd_flag
        ]);
        let got = parse_payload(181, &payload, &ctx).unwrap();
        match got {
            SeiPayload::AlternativeDepthInfo(info) => {
                assert_eq!(info.depth_type, 0);
                let gvd = info.gvd.unwrap();
                assert_eq!(gvd.cameras.len(), 2);
            }
            other => panic!("expected AlternativeDepthInfo, got {other:?}"),
        }
    }

    // §H.13.2.5 — minimal depth_timing: per_view_depth_timing_flag == 0
    // so a single shared depth_timing_offset() follows. offset_len_minus1
    // = 7 makes the fp field u(8); fp = 64 with dp = 6 reconstructs to
    // 64 / 2^6 = 1.0 clock ticks.
    #[test]
    fn depth_timing_shared_single_offset() {
        let payload = pack_bits(&[
            (0, 1),  // per_view_depth_timing_flag = 0
            (7, 5),  // offset_len_minus1 = 7 → fp is u(8)
            (64, 8), // depth_disp_delay_offset_fp = 64
            (6, 6),  // depth_disp_delay_offset_dp = 6
        ]);
        // The shared branch never reads ctx.num_depth_views, so the
        // default (0 = unknown) context must succeed.
        let got = parse_depth_timing(&payload, &SeiContext::default()).unwrap();
        assert!(!got.per_view_depth_timing_flag);
        assert_eq!(got.offsets.len(), 1);
        let off = got.offsets[0];
        assert_eq!(off.offset_len_minus1, 7);
        assert_eq!(off.depth_disp_delay_offset_fp, 64);
        assert_eq!(off.depth_disp_delay_offset_dp, 6);
        assert!((off.offset_clock_ticks() - 1.0).abs() < 1e-12);
    }

    // §H.13.2.5 — per-view depth_timing with NumDepthViews = 2 supplied
    // through the context. The two offsets use the extreme fp widths:
    // view 0 the minimum (offset_len_minus1 = 0 → u(1)), view 1 the
    // maximum (offset_len_minus1 = 31 → u(32), all bits set).
    #[test]
    fn depth_timing_per_view_two_views() {
        let payload = pack_bits(&[
            (1, 1), // per_view_depth_timing_flag = 1
            // view 0: offset_len_minus1 = 0 → fp u(1) = 1, dp = 0
            // → 1 / 2^0 = 1.0 clock ticks.
            (0, 5),
            (1, 1),
            (0, 6),
            // view 1: offset_len_minus1 = 31 → fp u(32) = u32::MAX,
            // dp = 32 → (2^32 − 1) / 2^32 just under 1.0.
            (31, 5),
            (u32::MAX as u64, 32),
            (32, 6),
        ]);
        let ctx = SeiContext {
            num_depth_views: 2,
            ..SeiContext::default()
        };
        let got = parse_depth_timing(&payload, &ctx).unwrap();
        assert!(got.per_view_depth_timing_flag);
        assert_eq!(got.offsets.len(), 2);
        assert_eq!(got.offsets[0].offset_len_minus1, 0);
        assert_eq!(got.offsets[0].depth_disp_delay_offset_fp, 1);
        assert_eq!(got.offsets[0].depth_disp_delay_offset_dp, 0);
        assert!((got.offsets[0].offset_clock_ticks() - 1.0).abs() < 1e-12);
        assert_eq!(got.offsets[1].offset_len_minus1, 31);
        assert_eq!(got.offsets[1].depth_disp_delay_offset_fp, u32::MAX);
        assert_eq!(got.offsets[1].depth_disp_delay_offset_dp, 32);
        let expected = f64::from(u32::MAX) / 2f64.powi(32);
        assert!((got.offsets[1].offset_clock_ticks() - expected).abs() < 1e-12);
    }

    // §H.13.1.5 — the per-view loop bound is the SPS-derived
    // NumDepthViews (§H.7.3.2.1.5), not a payload field. Reaching the
    // per-view branch with the context still at the unknown default
    // (0) must be rejected, mirroring the §D.1.10 spare_pic treatment
    // of an unknown PicSizeInMapUnits.
    #[test]
    fn depth_timing_per_view_unknown_num_depth_views_rejected() {
        let payload = pack_bits(&[
            (1, 1), // per_view_depth_timing_flag = 1
            (0, 5),
            (1, 1),
            (0, 6),
        ]);
        let err = parse_depth_timing(&payload, &SeiContext::default()).unwrap_err();
        assert!(matches!(err, SeiError::DepthTimingNumDepthViewsUnknown));
    }

    // §H.7.3.2.1.5 — the MVCD-extension view loop runs at most
    // num_views_minus1 + 1 ≤ 1024 times and increments NumDepthViews
    // at most once per iteration, so 1024 is the absolute ceiling. A
    // caller-supplied context above it is rejected before allocation.
    #[test]
    fn depth_timing_rejects_num_depth_views_above_1024() {
        let payload = pack_bits(&[
            (1, 1), // per_view_depth_timing_flag = 1
            (0, 5),
            (1, 1),
            (0, 6),
        ]);
        let ctx = SeiContext {
            num_depth_views: 1025,
            ..SeiContext::default()
        };
        let err = parse_depth_timing(&payload, &ctx).unwrap_err();
        assert!(matches!(
            err,
            SeiError::DepthTimingNumDepthViewsOutOfRange(1025)
        ));
    }

    // §H.13.2.5.1 — fractional reconstruction: fp = 3 with dp = 1 is
    // 1.5 clock ticks; the dp ceiling (u(6) max = 63) divides by 2^63
    // without overflowing the intermediate.
    #[test]
    fn depth_timing_offset_clock_ticks_fractional_and_dp_ceiling() {
        let half_step = DepthTimingOffset {
            offset_len_minus1: 1,
            depth_disp_delay_offset_fp: 3,
            depth_disp_delay_offset_dp: 1,
        };
        assert!((half_step.offset_clock_ticks() - 1.5).abs() < 1e-12);

        let dp_ceiling = DepthTimingOffset {
            offset_len_minus1: 0,
            depth_disp_delay_offset_fp: 1,
            depth_disp_delay_offset_dp: 63,
        };
        let expected = 1.0 / 2f64.powi(63);
        assert!((dp_ceiling.offset_clock_ticks() - expected).abs() < 1e-300);
        assert!(dp_ceiling.offset_clock_ticks() > 0.0);
    }

    // §H.13.1.5.1 — a payload that ends inside the offset triple is a
    // bitstream error, not a panic. Five bits of offset_len_minus1 = 31
    // promise a 32-bit fp that the 1-byte payload cannot deliver.
    #[test]
    fn depth_timing_truncated_offset_rejected() {
        let payload = pack_bits(&[
            (0, 1),  // per_view_depth_timing_flag = 0
            (31, 5), // offset_len_minus1 = 31 → fp u(32) expected
        ]);
        let err = parse_depth_timing(&payload, &SeiContext::default()).unwrap_err();
        assert!(matches!(err, SeiError::Bitstream(_)));
    }

    // §H.13.1.5 dispatch — parse_payload(52, ..) must route to
    // parse_depth_timing and thread the context through.
    #[test]
    fn parse_payload_dispatches_depth_timing() {
        let payload = pack_bits(&[
            (1, 1), // per_view_depth_timing_flag = 1
            // view 0: fp u(3) = 5, dp = 2 → 1.25 clock ticks.
            (2, 5),
            (5, 3),
            (2, 6),
        ]);
        let ctx = SeiContext {
            num_depth_views: 1,
            ..SeiContext::default()
        };
        let got = parse_payload(52, &payload, &ctx).unwrap();
        match got {
            SeiPayload::DepthTiming(dt) => {
                assert!(dt.per_view_depth_timing_flag);
                assert_eq!(dt.offsets.len(), 1);
                assert_eq!(dt.offsets[0].depth_disp_delay_offset_fp, 5);
                assert!((dt.offsets[0].offset_clock_ticks() - 1.25).abs() < 1e-12);
            }
            other => panic!("expected DepthTiming, got {other:?}"),
        }
    }

    // §D.2.24 — post_filter_hint round-trip. 1x1 cross-correlation,
    // each component carrying a single coefficient.
    //
    // size_y = 1, size_x = 1   → ue codeword "010" each
    // type   = 2 (cross-corr)  → u(2) "10"
    // c=0 coef = +5  → ue(9) = "0001010"  → se = 5  (codeword "0001010")
    // c=1 coef = -3  → ue(6) = "00111"    → se = -3
    // c=2 coef = 0   → "1"
    // additional_extension_flag = 0
    #[test]
    fn post_filter_hint_basic_cross_correlation_1x1() {
        let fields = [
            (0b010, 3),     // ue(1) = 1 (size_y)
            (0b010, 3),     // ue(1) = 1 (size_x)
            (0b10, 2),      // type = 2
            (0b0001010, 7), // se(+5)
            (0b00111, 5),   // se(-3)
            (1, 1),         // se(0)
            (0, 1),         // additional_extension_flag = 0
        ];
        let payload = pack_bits(&fields);
        let pfh = parse_post_filter_hint(&payload).unwrap();
        assert_eq!(pfh.filter_hint_size_x, 1);
        assert_eq!(pfh.filter_hint_size_y, 1);
        assert_eq!(pfh.filter_hint_type, 2);
        assert_eq!(pfh.filter_hint_value, vec![5, -3, 0]);
        assert!(!pfh.additional_extension_flag);
    }

    // §D.2.24 — type=0 2-D FIR with size_y = 2, size_x = 3.
    // Each component carries 6 coefficients. Use se(0) = "1" (1 bit
    // each) to keep the fixture short. additional_extension_flag = 1.
    #[test]
    fn post_filter_hint_2d_fir_2x3() {
        let mut fields = vec![
            (0b011, 3),   // ue(2) = 2 (size_y)
            (0b00100, 5), // ue(3) = 3 (size_x)
            (0b00, 2),    // type = 0
        ];
        // 3 components × 2 rows × 3 cols = 18 se(0) coefficients.
        for _ in 0..18 {
            fields.push((1, 1));
        }
        fields.push((1, 1)); // additional_extension_flag = 1
        let payload = pack_bits(&fields);
        let pfh = parse_post_filter_hint(&payload).unwrap();
        assert_eq!(pfh.filter_hint_size_y, 2);
        assert_eq!(pfh.filter_hint_size_x, 3);
        assert_eq!(pfh.filter_hint_type, 0);
        assert_eq!(pfh.filter_hint_value.len(), 18);
        assert!(pfh.filter_hint_value.iter().all(|&v| v == 0));
        assert!(pfh.additional_extension_flag);
    }

    // §D.2.24 — type 1 (two 1-D FIRs) requires size_y == 2.
    #[test]
    fn post_filter_hint_type1_requires_height_two() {
        let mut fields = vec![
            (0b00100, 5), // ue(3) = 3 (size_y) — wrong (should be 2)
            (0b010, 3),   // ue(1) = 1 (size_x)
            (0b01, 2),    // type = 1
        ];
        for _ in 0..9 {
            fields.push((1, 1));
        }
        fields.push((0, 1));
        let payload = pack_bits(&fields);
        let err = parse_post_filter_hint(&payload).unwrap_err();
        assert_eq!(err, SeiError::PostFilterHintType1WrongHeight(3));
    }

    // §D.2.24 — filter_hint_size_x must be in 1..=15. ue(15) = "0001111",
    // ue(16) = "000010001" (codeNum 16). Use size_x=16 to trip the
    // out-of-range guard.
    #[test]
    fn post_filter_hint_rejects_size_out_of_range() {
        let fields = [
            (0b010, 3),       // ue(1) = 1 (size_y)
            (0b000010001, 9), // ue(16) = 16 (size_x — out of range)
        ];
        let payload = pack_bits(&fields);
        let err = parse_post_filter_hint(&payload).unwrap_err();
        assert_eq!(err, SeiError::PostFilterHintSizeOutOfRange { x: 16, y: 1 });
    }

    // §D.2.24 — filter_hint_type 3 is reserved.
    #[test]
    fn post_filter_hint_rejects_reserved_type() {
        let fields = [
            (0b010, 3), // ue(1) = 1 (size_y)
            (0b010, 3), // ue(1) = 1 (size_x)
            (0b11, 2),  // type = 3 (reserved)
        ];
        let payload = pack_bits(&fields);
        let err = parse_post_filter_hint(&payload).unwrap_err();
        assert_eq!(err, SeiError::PostFilterHintTypeReserved);
    }

    #[test]
    fn parse_payload_dispatches_post_filter_hint() {
        let ctx = SeiContext::default();
        let fields = [
            (0b010, 3), // ue(1) = 1 (size_y)
            (0b010, 3), // ue(1) = 1 (size_x)
            (0b10, 2),  // type = 2
            (1, 1),     // se(0)
            (1, 1),     // se(0)
            (1, 1),     // se(0)
            (0, 1),     // additional_extension_flag
        ];
        let payload = pack_bits(&fields);
        let got = parse_payload(22, &payload, &ctx).unwrap();
        match got {
            SeiPayload::PostFilterHint(p) => {
                assert_eq!(p.filter_hint_size_x, 1);
                assert_eq!(p.filter_hint_size_y, 1);
                assert_eq!(p.filter_hint_type, 2);
                assert_eq!(p.filter_hint_value, vec![0, 0, 0]);
            }
            other => panic!("expected PostFilterHint, got {:?}", other),
        }
    }

    // §D.2.15 — full_frame_freeze: single ue(v) repetition_period.
    #[test]
    fn full_frame_freeze_basic() {
        // ue(7) = "0001000" (7 bits).
        let payload = pack_bits(&[(0b0001000, 7)]);
        let fff = parse_full_frame_freeze(&payload).unwrap();
        assert_eq!(fff.full_frame_freeze_repetition_period, 7);
    }

    #[test]
    fn parse_payload_dispatches_full_frame_freeze() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(0b010, 3)]); // ue(1) = 1
        let got = parse_payload(13, &payload, &ctx).unwrap();
        match got {
            SeiPayload::FullFrameFreeze(f) => {
                assert_eq!(f.full_frame_freeze_repetition_period, 1);
            }
            other => panic!("expected FullFrameFreeze, got {:?}", other),
        }
    }

    // §D.2.16 — full_frame_freeze_release: empty payload, dispatch
    // returns the unit variant.
    #[test]
    fn parse_payload_dispatches_full_frame_freeze_release() {
        let ctx = SeiContext::default();
        let got = parse_payload(14, &[], &ctx).unwrap();
        assert_eq!(got, SeiPayload::FullFrameFreezeRelease);
    }

    // §D.2.17 — full_frame_snapshot: single ue(v) snapshot_id.
    #[test]
    fn full_frame_snapshot_basic() {
        // ue(42) = codeNum 42, 42+1 = 43 = 0b101011 (6 bits), so k=5
        // → "00000 1 01011" = "00000101011" (11 bits).
        let payload = pack_bits(&[(0b00000101011, 11)]);
        let snap = parse_full_frame_snapshot(&payload).unwrap();
        assert_eq!(snap.snapshot_id, 42);
    }

    #[test]
    fn parse_payload_dispatches_full_frame_snapshot() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(0b010, 3)]); // ue(1) = 1
        let got = parse_payload(15, &payload, &ctx).unwrap();
        match got {
            SeiPayload::FullFrameSnapshot(s) => assert_eq!(s.snapshot_id, 1),
            other => panic!("expected FullFrameSnapshot, got {:?}", other),
        }
    }

    // §D.2.22 — deblocking_filter_display_preference: cancel_flag path.
    #[test]
    fn deblocking_filter_display_preference_cancel() {
        let payload = pack_bits(&[(1, 1)]); // cancel_flag=1
        let p = parse_deblocking_filter_display_preference(&payload).unwrap();
        assert!(p.cancel_flag);
        assert!(!p.display_prior_to_deblocking_preferred_flag);
        assert!(!p.dec_frame_buffering_constraint_flag);
        assert_eq!(p.repetition_period, 0);
    }

    // §D.2.22 — deblocking_filter_display_preference: full body.
    #[test]
    fn deblocking_filter_display_preference_full_body() {
        let payload = pack_bits(&[
            (0, 1),     // cancel_flag = 0
            (1, 1),     // display_prior_to_deblocking_preferred_flag = 1
            (0, 1),     // dec_frame_buffering_constraint_flag = 0
            (0b011, 3), // ue(2) = 2 (repetition_period)
        ]);
        let p = parse_deblocking_filter_display_preference(&payload).unwrap();
        assert!(!p.cancel_flag);
        assert!(p.display_prior_to_deblocking_preferred_flag);
        assert!(!p.dec_frame_buffering_constraint_flag);
        assert_eq!(p.repetition_period, 2);
    }

    #[test]
    fn parse_payload_dispatches_deblocking_filter_display_preference() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(1, 1)]); // cancel_flag=1
        let got = parse_payload(20, &payload, &ctx).unwrap();
        match got {
            SeiPayload::DeblockingFilterDisplayPreference(p) => assert!(p.cancel_flag),
            other => panic!(
                "expected DeblockingFilterDisplayPreference, got {:?}",
                other
            ),
        }
    }

    // -----------------------------------------------------------------
    // Round 73 — scene_info / progressive_refinement / stereo_video /
    // motion_constrained_slice_group_set.
    // -----------------------------------------------------------------

    #[test]
    fn scene_info_present_flag_zero_yields_empty_struct() {
        // scene_info_present_flag = 0 → struct is otherwise all-None.
        let payload = pack_bits(&[(0, 1)]);
        let got = parse_scene_info(&payload).unwrap();
        assert!(!got.scene_info_present_flag);
        assert!(got.scene_id.is_none());
        assert!(got.scene_transition_type.is_none());
        assert!(got.second_scene_id.is_none());
    }

    #[test]
    fn scene_info_transition_type_le_3_omits_second_scene_id() {
        // present=1, scene_id=5 (ue=5 → "00110"), transition=2 (ue=2 → "011"),
        // no second_scene_id because transition <= 3.
        let payload = pack_bits(&[
            (1, 1),       // present_flag
            (0b00110, 5), // ue(5) → scene_id=5
            (0b011, 3),   // ue(2) → transition=2
        ]);
        let got = parse_scene_info(&payload).unwrap();
        assert!(got.scene_info_present_flag);
        assert_eq!(got.scene_id, Some(5));
        assert_eq!(got.scene_transition_type, Some(2));
        assert!(got.second_scene_id.is_none());
    }

    #[test]
    fn scene_info_transition_type_gt_3_reads_second_scene_id() {
        // present=1, scene_id=1 (ue=1 → "010"), transition=4 (ue=4 → "00101"),
        // second_scene_id=2 (ue=2 → "011").
        let payload = pack_bits(&[
            (1, 1),
            (0b010, 3),   // scene_id=1
            (0b00101, 5), // transition=4
            (0b011, 3),   // second_scene_id=2
        ]);
        let got = parse_scene_info(&payload).unwrap();
        assert_eq!(got.scene_id, Some(1));
        assert_eq!(got.scene_transition_type, Some(4));
        assert_eq!(got.second_scene_id, Some(2));
    }

    #[test]
    fn parse_payload_dispatches_scene_info() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(0, 1)]);
        let got = parse_payload(9, &payload, &ctx).unwrap();
        match got {
            SeiPayload::SceneInfo(s) => assert!(!s.scene_info_present_flag),
            other => panic!("expected SceneInfo, got {other:?}"),
        }
    }

    // --- §D.1.10 spare_pic (payload type 8) ---------------------------

    #[test]
    fn spare_pic_area_idc_0_needs_no_map_units() {
        // target_frame_num=3 (ue → "00100"), spare_field_flag=0,
        // num_spare_pics_minus1=0 (ue → "1") → one spare picture,
        // delta_spare_frame_num=2 (ue → "011"), spare_area_idc=0 (ue → "1").
        // PicSizeInMapUnits is irrelevant for spare_area_idc == 0.
        let payload = pack_bits(&[
            (0b00100, 5), // target_frame_num=3
            (0, 1),       // spare_field_flag=0
            (1, 1),       // num_spare_pics_minus1=0
            (0b011, 3),   // delta_spare_frame_num=2
            (1, 1),       // spare_area_idc=0
        ]);
        let ctx = SeiContext::default(); // pic_size_in_map_units = 0
        let got = parse_spare_pic(&payload, &ctx).unwrap();
        assert_eq!(got.target_frame_num, 3);
        assert!(!got.spare_field_flag);
        assert!(got.target_bottom_field_flag.is_none());
        assert_eq!(got.entries.len(), 1);
        let e = &got.entries[0];
        assert_eq!(e.delta_spare_frame_num, 2);
        assert_eq!(e.spare_area_idc, 0);
        assert!(e.spare_units.is_none());
        assert!(e.zero_run_lengths.is_none());
        assert!(e.spare_bottom_field_flag.is_none());
    }

    #[test]
    fn spare_pic_area_idc_1_reads_one_flag_per_map_unit() {
        // PicSizeInMapUnits=4. target_frame_num=0 (ue → "1"),
        // spare_field_flag=0, num_spare_pics_minus1=0 (ue → "1"),
        // delta_spare_frame_num=0 (ue → "1"), spare_area_idc=1 (ue → "010"),
        // spare_unit_flag[0..4] = 0,1,0,1 → spare,not,spare,not.
        let payload = pack_bits(&[
            (1, 1),     // target_frame_num=0
            (0, 1),     // spare_field_flag=0
            (1, 1),     // num_spare_pics_minus1=0
            (1, 1),     // delta_spare_frame_num=0
            (0b010, 3), // spare_area_idc=1
            (0, 1),     // spare_unit_flag[0]=0 → spare
            (1, 1),     // spare_unit_flag[1]=1 → not spare
            (0, 1),     // spare_unit_flag[2]=0 → spare
            (1, 1),     // spare_unit_flag[3]=1 → not spare
        ]);
        let ctx = SeiContext {
            pic_size_in_map_units: 4,
            ..SeiContext::default()
        };
        let got = parse_spare_pic(&payload, &ctx).unwrap();
        let e = &got.entries[0];
        assert_eq!(e.spare_area_idc, 1);
        assert_eq!(e.spare_units, Some(vec![true, false, true, false]));
        assert!(e.zero_run_lengths.is_none());
    }

    #[test]
    fn spare_pic_area_idc_2_reads_zero_runs_until_map_units() {
        // PicSizeInMapUnits=6. target_frame_num=0 (ue → "1"),
        // spare_field_flag=0, num_spare_pics_minus1=0 (ue → "1"),
        // delta_spare_frame_num=0 (ue → "1"), spare_area_idc=2 (ue → "011").
        // zero_run_length runs: 2 (ue → "011"), 1 (ue → "010").
        //   run 0: mapUnitCnt = 0 + 2 + 1 = 3 (< 6, continue)
        //   run 1: mapUnitCnt = 3 + 1 + 1 = 5 (< 6, continue)
        //   run 2: mapUnitCnt = 5 + 0 + 1 = 6 (>= 6, stop) — zero_run_length=0 (ue → "1")
        let payload = pack_bits(&[
            (1, 1),     // target_frame_num=0
            (0, 1),     // spare_field_flag=0
            (1, 1),     // num_spare_pics_minus1=0
            (1, 1),     // delta_spare_frame_num=0
            (0b011, 3), // spare_area_idc=2
            (0b011, 3), // zero_run_length[0]=2
            (0b010, 3), // zero_run_length[1]=1
            (1, 1),     // zero_run_length[2]=0
        ]);
        let ctx = SeiContext {
            pic_size_in_map_units: 6,
            ..SeiContext::default()
        };
        let got = parse_spare_pic(&payload, &ctx).unwrap();
        let e = &got.entries[0];
        assert_eq!(e.spare_area_idc, 2);
        assert!(e.spare_units.is_none());
        assert_eq!(e.zero_run_lengths, Some(vec![2, 1, 0]));
    }

    #[test]
    fn spare_pic_field_message_reads_bottom_field_flags() {
        // spare_field_flag=1 pulls in target_bottom_field_flag and a
        // per-spare spare_bottom_field_flag. target_frame_num=1 (ue → "010"),
        // spare_field_flag=1, target_bottom_field_flag=1,
        // num_spare_pics_minus1=0 (ue → "1"), delta_spare_frame_num=0 (ue → "1"),
        // spare_bottom_field_flag[0]=0, spare_area_idc=0 (ue → "1").
        let payload = pack_bits(&[
            (0b010, 3), // target_frame_num=1
            (1, 1),     // spare_field_flag=1
            (1, 1),     // target_bottom_field_flag=1
            (1, 1),     // num_spare_pics_minus1=0
            (1, 1),     // delta_spare_frame_num=0
            (0, 1),     // spare_bottom_field_flag[0]=0
            (1, 1),     // spare_area_idc=0
        ]);
        let ctx = SeiContext::default();
        let got = parse_spare_pic(&payload, &ctx).unwrap();
        assert_eq!(got.target_frame_num, 1);
        assert!(got.spare_field_flag);
        assert_eq!(got.target_bottom_field_flag, Some(true));
        assert_eq!(got.entries[0].spare_bottom_field_flag, Some(false));
        assert_eq!(got.entries[0].spare_area_idc, 0);
    }

    #[test]
    fn spare_pic_rejects_num_spare_pics_minus1_over_15() {
        // target_frame_num=0 (ue → "1"), spare_field_flag=0,
        // num_spare_pics_minus1=16 (ue(16) → "000010001", 9 bits) > 15.
        let payload = pack_bits(&[
            (1, 1),           // target_frame_num=0
            (0, 1),           // spare_field_flag=0
            (0b000010001, 9), // num_spare_pics_minus1=16
        ]);
        let ctx = SeiContext::default();
        assert_eq!(
            parse_spare_pic(&payload, &ctx),
            Err(SeiError::SparePicCountTooLarge(16))
        );
    }

    #[test]
    fn spare_pic_rejects_area_idc_over_2() {
        // delta_spare_frame_num=0 then spare_area_idc=3 (ue(3) → "00100").
        let payload = pack_bits(&[
            (1, 1),       // target_frame_num=0
            (0, 1),       // spare_field_flag=0
            (1, 1),       // num_spare_pics_minus1=0
            (1, 1),       // delta_spare_frame_num=0
            (0b00100, 5), // spare_area_idc=3
        ]);
        let ctx = SeiContext::default();
        assert_eq!(
            parse_spare_pic(&payload, &ctx),
            Err(SeiError::SparePicAreaIdcOutOfRange { i: 0, idc: 3 })
        );
    }

    #[test]
    fn spare_pic_area_idc_1_without_map_units_is_rejected() {
        // spare_area_idc=1 but the SeiContext carried PicSizeInMapUnits=0.
        let payload = pack_bits(&[
            (1, 1),     // target_frame_num=0
            (0, 1),     // spare_field_flag=0
            (1, 1),     // num_spare_pics_minus1=0
            (1, 1),     // delta_spare_frame_num=0
            (0b010, 3), // spare_area_idc=1
        ]);
        let ctx = SeiContext::default(); // pic_size_in_map_units = 0
        assert_eq!(
            parse_spare_pic(&payload, &ctx),
            Err(SeiError::SparePicMapUnitsUnknown { i: 0, idc: 1 })
        );
    }

    #[test]
    fn spare_pic_two_entries_mix_methods() {
        // num_spare_pics_minus1=1 → two spare pictures.
        // entry 0: delta=0, spare_area_idc=0 (no map data).
        // entry 1: delta=1 (ue → "010"), spare_area_idc=1, 2 flags (1,0).
        let payload = pack_bits(&[
            (1, 1),     // target_frame_num=0
            (0, 1),     // spare_field_flag=0
            (0b010, 3), // num_spare_pics_minus1=1
            (1, 1),     // delta_spare_frame_num[0]=0
            (1, 1),     // spare_area_idc[0]=0
            (0b010, 3), // delta_spare_frame_num[1]=1
            (0b010, 3), // spare_area_idc[1]=1
            (1, 1),     // spare_unit_flag[1][0]=1 → not spare
            (0, 1),     // spare_unit_flag[1][1]=0 → spare
        ]);
        let ctx = SeiContext {
            pic_size_in_map_units: 2,
            ..SeiContext::default()
        };
        let got = parse_spare_pic(&payload, &ctx).unwrap();
        assert_eq!(got.entries.len(), 2);
        assert_eq!(got.entries[0].spare_area_idc, 0);
        assert!(got.entries[0].spare_units.is_none());
        assert_eq!(got.entries[1].delta_spare_frame_num, 1);
        assert_eq!(got.entries[1].spare_units, Some(vec![false, true]));
    }

    #[test]
    fn parse_payload_dispatches_spare_pic() {
        // Same as spare_pic_area_idc_0_needs_no_map_units, via parse_payload(8).
        let payload = pack_bits(&[
            (0b00100, 5), // target_frame_num=3
            (0, 1),       // spare_field_flag=0
            (1, 1),       // num_spare_pics_minus1=0
            (0b011, 3),   // delta_spare_frame_num=2
            (1, 1),       // spare_area_idc=0
        ]);
        let ctx = SeiContext::default();
        match parse_payload(8, &payload, &ctx).unwrap() {
            SeiPayload::SparePic(s) => {
                assert_eq!(s.target_frame_num, 3);
                assert_eq!(s.entries.len(), 1);
                assert_eq!(s.entries[0].spare_area_idc, 0);
            }
            other => panic!("expected SparePic, got {other:?}"),
        }
    }

    // --- §D.1.9 dec_ref_pic_marking_repetition (payload type 7) -------

    #[test]
    fn dec_ref_pic_marking_repetition_idr_frame_only() {
        // frame_mbs_only_flag = true ⇒ no field flags.
        // original_idr_flag = 1, original_frame_num = 0 (ue → "1"),
        // IDR branch: no_output_of_prior_pics_flag = 1, long_term_reference_flag = 0.
        let payload = pack_bits(&[
            (1, 1), // original_idr_flag
            (1, 1), // original_frame_num = 0 (ue)
            (1, 1), // no_output_of_prior_pics_flag
            (0, 1), // long_term_reference_flag
        ]);
        let ctx = SeiContext::default(); // frame_mbs_only_flag = true
        let got = parse_dec_ref_pic_marking_repetition(&payload, &ctx).unwrap();
        assert!(got.original_idr_flag);
        assert_eq!(got.original_frame_num, 0);
        assert_eq!(got.original_field_pic_flag, None);
        assert_eq!(got.original_bottom_field_flag, None);
        assert!(got.dec_ref_pic_marking.no_output_of_prior_pics_flag);
        assert!(!got.dec_ref_pic_marking.long_term_reference_flag);
        assert_eq!(got.dec_ref_pic_marking.adaptive_marking, None);
    }

    #[test]
    fn dec_ref_pic_marking_repetition_non_idr_adaptive_mmco() {
        // original_idr_flag = 0, original_frame_num = 5 (ue → "00110"),
        // non-IDR branch: adaptive_ref_pic_marking_mode_flag = 1, then
        //   mmco = 1 (ue → "010"), difference_of_pic_nums_minus1 = 3 (ue → "00100"),
        //   mmco = 3 (ue → "00100"), difference = 0 (ue → "1"),
        //                            long_term_frame_idx = 2 (ue → "011"),
        //   mmco = 0 (ue → "1")  [terminator].
        let payload = pack_bits(&[
            (0, 1),       // original_idr_flag
            (0b00110, 5), // original_frame_num = 5
            (1, 1),       // adaptive_ref_pic_marking_mode_flag
            (0b010, 3),   // mmco = 1
            (0b00100, 5), // difference_of_pic_nums_minus1 = 3
            (0b00100, 5), // mmco = 3
            (1, 1),       // difference_of_pic_nums_minus1 = 0
            (0b011, 3),   // long_term_frame_idx = 2
            (1, 1),       // mmco = 0 (terminator)
        ]);
        let ctx = SeiContext::default();
        let got = parse_dec_ref_pic_marking_repetition(&payload, &ctx).unwrap();
        assert!(!got.original_idr_flag);
        assert_eq!(got.original_frame_num, 5);
        let ops = got.dec_ref_pic_marking.adaptive_marking.unwrap();
        assert_eq!(
            ops,
            vec![MmcoOp::MarkShortTermUnused(3), MmcoOp::AssignLongTerm(0, 2)]
        );
    }

    #[test]
    fn dec_ref_pic_marking_repetition_non_idr_sliding_window() {
        // original_idr_flag = 0, original_frame_num = 2 (ue → "011"),
        // adaptive_ref_pic_marking_mode_flag = 0 ⇒ sliding window (no ops).
        let payload = pack_bits(&[
            (0, 1),     // original_idr_flag
            (0b011, 3), // original_frame_num = 2
            (0, 1),     // adaptive_ref_pic_marking_mode_flag
        ]);
        let ctx = SeiContext::default();
        let got = parse_dec_ref_pic_marking_repetition(&payload, &ctx).unwrap();
        assert!(!got.original_idr_flag);
        assert_eq!(got.original_frame_num, 2);
        assert_eq!(got.dec_ref_pic_marking.adaptive_marking, None);
    }

    #[test]
    fn dec_ref_pic_marking_repetition_field_with_bottom_flag() {
        // frame_mbs_only_flag = false ⇒ field flags present.
        // original_idr_flag = 1, original_frame_num = 0 (ue → "1"),
        // original_field_pic_flag = 1, original_bottom_field_flag = 1,
        // IDR branch: no_output = 0, long_term_reference_flag = 1.
        let payload = pack_bits(&[
            (1, 1), // original_idr_flag
            (1, 1), // original_frame_num = 0
            (1, 1), // original_field_pic_flag
            (1, 1), // original_bottom_field_flag
            (0, 1), // no_output_of_prior_pics_flag
            (1, 1), // long_term_reference_flag
        ]);
        let ctx = SeiContext {
            frame_mbs_only_flag: false,
            ..SeiContext::default()
        };
        let got = parse_dec_ref_pic_marking_repetition(&payload, &ctx).unwrap();
        assert_eq!(got.original_field_pic_flag, Some(true));
        assert_eq!(got.original_bottom_field_flag, Some(true));
        assert!(!got.dec_ref_pic_marking.no_output_of_prior_pics_flag);
        assert!(got.dec_ref_pic_marking.long_term_reference_flag);
    }

    #[test]
    fn dec_ref_pic_marking_repetition_field_no_bottom_flag() {
        // frame_mbs_only_flag = false, original_field_pic_flag = 0 ⇒
        // original_bottom_field_flag absent.
        // original_idr_flag = 0, original_frame_num = 1 (ue → "010"),
        // original_field_pic_flag = 0, adaptive = 0.
        let payload = pack_bits(&[
            (0, 1),     // original_idr_flag
            (0b010, 3), // original_frame_num = 1
            (0, 1),     // original_field_pic_flag
            (0, 1),     // adaptive_ref_pic_marking_mode_flag
        ]);
        let ctx = SeiContext {
            frame_mbs_only_flag: false,
            ..SeiContext::default()
        };
        let got = parse_dec_ref_pic_marking_repetition(&payload, &ctx).unwrap();
        assert_eq!(got.original_field_pic_flag, Some(false));
        assert_eq!(got.original_bottom_field_flag, None);
        assert_eq!(got.dec_ref_pic_marking.adaptive_marking, None);
    }

    #[test]
    fn dec_ref_pic_marking_repetition_all_mmco_ops() {
        // Exercise every MMCO 1..=6 in one adaptive loop, frame-only.
        // original_idr_flag = 0, original_frame_num = 0 (ue → "1"),
        // adaptive = 1, then:
        //   mmco 1 (ue "010") + diff 0 (ue "1")
        //   mmco 2 (ue "011") + long_term_pic_num 1 (ue "010")
        //   mmco 3 (ue "00100") + diff 1 (ue "010") + ltfi 0 (ue "1")
        //   mmco 4 (ue "00101") + max_lt_idx_plus1 2 (ue "011")
        //   mmco 5 (ue "00110")
        //   mmco 6 (ue "00111") + ltfi 3 (ue "00100")
        //   mmco 0 (ue "1")
        let payload = pack_bits(&[
            (0, 1),       // original_idr_flag
            (1, 1),       // original_frame_num = 0
            (1, 1),       // adaptive_ref_pic_marking_mode_flag
            (0b010, 3),   // mmco 1
            (1, 1),       // difference_of_pic_nums_minus1 = 0
            (0b011, 3),   // mmco 2
            (0b010, 3),   // long_term_pic_num = 1
            (0b00100, 5), // mmco 3
            (0b010, 3),   // difference_of_pic_nums_minus1 = 1
            (1, 1),       // long_term_frame_idx = 0
            (0b00101, 5), // mmco 4
            (0b011, 3),   // max_long_term_frame_idx_plus1 = 2
            (0b00110, 5), // mmco 5
            (0b00111, 5), // mmco 6
            (0b00100, 5), // long_term_frame_idx = 3
            (1, 1),       // mmco 0 (terminator)
        ]);
        let ctx = SeiContext::default();
        let got = parse_dec_ref_pic_marking_repetition(&payload, &ctx).unwrap();
        let ops = got.dec_ref_pic_marking.adaptive_marking.unwrap();
        assert_eq!(
            ops,
            vec![
                MmcoOp::MarkShortTermUnused(0),
                MmcoOp::MarkLongTermUnused(1),
                MmcoOp::AssignLongTerm(1, 0),
                MmcoOp::SetMaxLongTermIdx(2),
                MmcoOp::MarkAllUnused,
                MmcoOp::AssignCurrentLongTerm(3),
            ]
        );
    }

    #[test]
    fn dec_ref_pic_marking_repetition_mmco_out_of_range_rejected() {
        // mmco = 7 is out of range (Table 7-9 defines 0..=6).
        // original_idr_flag = 0, original_frame_num = 0 (ue "1"),
        // adaptive = 1, mmco = 7 (ue → "0001000").
        let payload = pack_bits(&[
            (0, 1),         // original_idr_flag
            (1, 1),         // original_frame_num = 0
            (1, 1),         // adaptive_ref_pic_marking_mode_flag
            (0b0001000, 7), // mmco = 7
        ]);
        let ctx = SeiContext::default();
        let err = parse_dec_ref_pic_marking_repetition(&payload, &ctx).unwrap_err();
        assert_eq!(err, SeiError::DecRefPicMarkingRepetitionMmcoOutOfRange(7));
    }

    #[test]
    fn parse_payload_dispatches_dec_ref_pic_marking_repetition() {
        // Same body as dec_ref_pic_marking_repetition_idr_frame_only,
        // routed via parse_payload(7).
        let payload = pack_bits(&[(1, 1), (1, 1), (1, 1), (0, 1)]);
        let ctx = SeiContext::default();
        match parse_payload(7, &payload, &ctx).unwrap() {
            SeiPayload::DecRefPicMarkingRepetition(d) => {
                assert!(d.original_idr_flag);
                assert_eq!(d.original_frame_num, 0);
                assert!(d.dec_ref_pic_marking.no_output_of_prior_pics_flag);
            }
            other => panic!("expected DecRefPicMarkingRepetition, got {other:?}"),
        }
    }

    // --- §D.1.11 sub_seq_info (payload type 10) -----------------------

    #[test]
    fn sub_seq_info_without_frame_num() {
        // layer=2 (ue → "011"), id=5 (ue → "00110"), first=1, leading=0,
        // last=1, frame_num_flag=0 → no sub_seq_frame_num.
        let payload = pack_bits(&[
            (0b011, 3),   // sub_seq_layer_num = 2
            (0b00110, 5), // sub_seq_id = 5
            (1, 1),       // first_ref_pic_flag
            (0, 1),       // leading_non_ref_pic_flag
            (1, 1),       // last_pic_flag
            (0, 1),       // sub_seq_frame_num_flag
        ]);
        let got = parse_sub_seq_info(&payload).unwrap();
        assert_eq!(got.sub_seq_layer_num, 2);
        assert_eq!(got.sub_seq_id, 5);
        assert!(got.first_ref_pic_flag);
        assert!(!got.leading_non_ref_pic_flag);
        assert!(got.last_pic_flag);
        assert!(got.sub_seq_frame_num.is_none());
    }

    #[test]
    fn sub_seq_info_with_frame_num() {
        // layer=0 (ue → "1"), id=0 (ue → "1"), first=0, leading=1, last=0,
        // frame_num_flag=1, frame_num=3 (ue → "00100").
        let payload = pack_bits(&[
            (1, 1),       // sub_seq_layer_num = 0
            (1, 1),       // sub_seq_id = 0
            (0, 1),       // first_ref_pic_flag
            (1, 1),       // leading_non_ref_pic_flag
            (0, 1),       // last_pic_flag
            (1, 1),       // sub_seq_frame_num_flag
            (0b00100, 5), // sub_seq_frame_num = 3
        ]);
        let got = parse_sub_seq_info(&payload).unwrap();
        assert_eq!(got.sub_seq_layer_num, 0);
        assert_eq!(got.sub_seq_id, 0);
        assert!(!got.first_ref_pic_flag);
        assert!(got.leading_non_ref_pic_flag);
        assert!(!got.last_pic_flag);
        assert_eq!(got.sub_seq_frame_num, Some(3));
    }

    #[test]
    fn sub_seq_info_layer_num_out_of_range_rejected() {
        // sub_seq_layer_num = 256 is out of range (must be < 256).
        // ue(256) = 8 leading zeros + "1" + 8-bit (256-255=... actually
        // 256+1 = 257 = 0b1_0000_0001 → 8 leading zeros, then 9-bit value).
        let payload = pack_bits(&[(0b1_0000_0001, 17)]);
        let err = parse_sub_seq_info(&payload).unwrap_err();
        assert_eq!(err, SeiError::SubSeqLayerNumTooLarge(256));
    }

    #[test]
    fn parse_payload_dispatches_sub_seq_info() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(1, 1), (1, 1), (1, 1), (0, 1), (0, 1), (0, 1)]);
        match parse_payload(10, &payload, &ctx).unwrap() {
            SeiPayload::SubSeqInfo(s) => {
                assert_eq!(s.sub_seq_layer_num, 0);
                assert!(s.first_ref_pic_flag);
            }
            other => panic!("expected SubSeqInfo, got {other:?}"),
        }
    }

    // --- §D.1.12 sub_seq_layer_characteristics (payload type 11) ------

    #[test]
    fn sub_seq_layer_characteristics_two_layers() {
        // num_sub_seq_layers_minus1 = 1 (ue → "010") → 2 entries.
        // layer 0: accurate=1, bit_rate=0x1234, frame_rate=0x0100
        // layer 1: accurate=0, bit_rate=0x0000, frame_rate=0x00FF
        let payload = pack_bits(&[
            (0b010, 3),   // num_sub_seq_layers_minus1 = 1
            (1, 1),       // accurate_statistics_flag[0]
            (0x1234, 16), // average_bit_rate[0]
            (0x0100, 16), // average_frame_rate[0]
            (0, 1),       // accurate_statistics_flag[1]
            (0x0000, 16), // average_bit_rate[1]
            (0x00FF, 16), // average_frame_rate[1]
        ]);
        let got = parse_sub_seq_layer_characteristics(&payload).unwrap();
        assert_eq!(got.layers.len(), 2);
        assert!(got.layers[0].accurate_statistics_flag);
        assert_eq!(got.layers[0].average_bit_rate, 0x1234);
        assert_eq!(got.layers[0].average_frame_rate, 0x0100);
        assert!(!got.layers[1].accurate_statistics_flag);
        assert_eq!(got.layers[1].average_bit_rate, 0);
        assert_eq!(got.layers[1].average_frame_rate, 0x00FF);
    }

    #[test]
    fn parse_payload_dispatches_sub_seq_layer_characteristics() {
        let ctx = SeiContext::default();
        // single layer: minus1=0 (ue → "1"), accurate=0, rates 0.
        let payload = pack_bits(&[(1, 1), (0, 1), (0, 16), (0, 16)]);
        match parse_payload(11, &payload, &ctx).unwrap() {
            SeiPayload::SubSeqLayerCharacteristics(s) => {
                assert_eq!(s.layers.len(), 1);
            }
            other => panic!("expected SubSeqLayerCharacteristics, got {other:?}"),
        }
    }

    // --- typed accessors on SubSeqLayerCharacteristic -----------------
    //
    // §D.2.13 (2024) / §D.2.12 (2003 draft) carrier semantics:
    // * average_bit_rate is in units of 1000 bits/second; 0 ⇒ unspecified.
    // * average_frame_rate is in units of frames/(256 seconds);
    //   0 ⇒ unspecified.

    #[test]
    fn sub_seq_layer_avg_bit_rate_bps_zero_is_none() {
        let s = SubSeqLayerCharacteristic {
            accurate_statistics_flag: false,
            average_bit_rate: 0,
            average_frame_rate: 0,
        };
        assert_eq!(s.average_bit_rate_bps(), None);
        assert_eq!(s.average_frame_rate_fps(), None);
    }

    #[test]
    fn sub_seq_layer_avg_bit_rate_bps_minimum_nonzero() {
        // Smallest valid signalled value: on-wire 1 ⇒ 1000 bits/second.
        let s = SubSeqLayerCharacteristic {
            accurate_statistics_flag: true,
            average_bit_rate: 1,
            average_frame_rate: 0,
        };
        assert_eq!(s.average_bit_rate_bps(), Some(1_000));
    }

    #[test]
    fn sub_seq_layer_avg_bit_rate_bps_typical() {
        // 5_000 * 1000 = 5_000_000 bits/second (5 Mbit/s).
        let s = SubSeqLayerCharacteristic {
            accurate_statistics_flag: true,
            average_bit_rate: 5_000,
            average_frame_rate: 0,
        };
        assert_eq!(s.average_bit_rate_bps(), Some(5_000_000));
    }

    #[test]
    fn sub_seq_layer_avg_bit_rate_bps_maximum() {
        // On-wire u(16) ceiling: 65535 * 1000 = 65_535_000 bits/second.
        let s = SubSeqLayerCharacteristic {
            accurate_statistics_flag: true,
            average_bit_rate: u16::MAX,
            average_frame_rate: 0,
        };
        assert_eq!(s.average_bit_rate_bps(), Some(65_535_000));
    }

    #[test]
    fn sub_seq_layer_avg_frame_rate_fps_quarter_step() {
        // 1/256 ⇒ smallest representable step.
        let s = SubSeqLayerCharacteristic {
            accurate_statistics_flag: true,
            average_bit_rate: 0,
            average_frame_rate: 1,
        };
        let fps = s.average_frame_rate_fps().expect("non-zero ⇒ Some");
        assert!((fps - (1.0 / 256.0)).abs() < f64::EPSILON);
    }

    #[test]
    fn sub_seq_layer_avg_frame_rate_fps_exact_integer_rates() {
        // 24, 25, 30 fps map to 24*256, 25*256, 30*256 on-wire.
        for &(carrier, expected_fps) in
            &[(24u16 * 256, 24.0_f64), (25 * 256, 25.0), (30 * 256, 30.0)]
        {
            let s = SubSeqLayerCharacteristic {
                accurate_statistics_flag: true,
                average_bit_rate: 0,
                average_frame_rate: carrier,
            };
            let fps = s.average_frame_rate_fps().expect("non-zero ⇒ Some");
            assert!(
                (fps - expected_fps).abs() < f64::EPSILON,
                "carrier {carrier} ⇒ expected {expected_fps} got {fps}"
            );
        }
    }

    #[test]
    fn sub_seq_layer_avg_frame_rate_fps_maximum() {
        // On-wire u(16) ceiling: 65535 / 256 ≈ 255.996 fps.
        let s = SubSeqLayerCharacteristic {
            accurate_statistics_flag: true,
            average_bit_rate: 0,
            average_frame_rate: u16::MAX,
        };
        let fps = s.average_frame_rate_fps().expect("non-zero ⇒ Some");
        assert!((fps - (65535.0_f64 / 256.0)).abs() < f64::EPSILON);
    }

    #[test]
    fn sub_seq_layer_accessors_via_parse_round_trip() {
        // End-to-end: parse_sub_seq_layer_characteristics → accessors.
        // num_sub_seq_layers_minus1 = 0 (ue → "1") → 1 entry.
        // accurate=1, bit_rate=2500 (2.5 Mbit/s), frame_rate=24*256 (24 fps).
        let payload = pack_bits(&[
            (1, 1),            // num_sub_seq_layers_minus1 = 0
            (1, 1),            // accurate_statistics_flag
            (2_500, 16),       // average_bit_rate
            (24u64 * 256, 16), // average_frame_rate
        ]);
        let got = parse_sub_seq_layer_characteristics(&payload).unwrap();
        assert_eq!(got.layers.len(), 1);
        assert_eq!(got.layers[0].average_bit_rate_bps(), Some(2_500_000));
        let fps = got.layers[0]
            .average_frame_rate_fps()
            .expect("non-zero ⇒ Some");
        assert!((fps - 24.0).abs() < f64::EPSILON);
    }

    #[test]
    fn sub_seq_characteristics_avg_rate_accessors_round_trip() {
        // End-to-end via parse_sub_seq_characteristics, exercising the
        // §D.2.14 carrier (payload type 12) that re-uses
        // SubSeqLayerCharacteristic under `average_rate`.
        // layer=0 (ue → "1"), id=0 (ue → "1"),
        // duration_flag=0, average_rate_flag=1,
        // accurate=0, bit_rate=10000 (10 Mbit/s), frame_rate=60*256,
        // num_referenced_subseqs=0 (ue → "1").
        let payload = pack_bits(&[
            (1, 1),            // sub_seq_layer_num = 0
            (1, 1),            // sub_seq_id = 0
            (0, 1),            // duration_flag = 0
            (1, 1),            // average_rate_flag = 1
            (0, 1),            // accurate_statistics_flag = 0
            (10_000, 16),      // average_bit_rate
            (60u64 * 256, 16), // average_frame_rate
            (1, 1),            // num_referenced_subseqs = 0
        ]);
        let got = parse_sub_seq_characteristics(&payload).unwrap();
        let rate = got.average_rate.expect("average_rate present");
        assert_eq!(rate.average_bit_rate_bps(), Some(10_000_000));
        let fps = rate.average_frame_rate_fps().expect("non-zero ⇒ Some");
        assert!((fps - 60.0).abs() < f64::EPSILON);
    }

    // --- §D.1.13 sub_seq_characteristics (payload type 12) ------------

    #[test]
    fn sub_seq_characteristics_full() {
        // layer=1 (ue → "010"), id=2 (ue → "011"),
        // duration_flag=1, duration=0xDEADBEEF,
        // average_rate_flag=1, accurate=1, bit_rate=0xAAAA, frame_rate=0x5555,
        // num_referenced_subseqs=1 (ue → "010"),
        //   ref_layer=0 (ue → "1"), ref_id=3 (ue → "00100"), direction=1.
        let payload = pack_bits(&[
            (0b010, 3),       // sub_seq_layer_num = 1
            (0b011, 3),       // sub_seq_id = 2
            (1, 1),           // duration_flag
            (0xDEADBEEF, 32), // sub_seq_duration
            (1, 1),           // average_rate_flag
            (1, 1),           // accurate_statistics_flag
            (0xAAAA, 16),     // average_bit_rate
            (0x5555, 16),     // average_frame_rate
            (0b010, 3),       // num_referenced_subseqs = 1
            (1, 1),           // ref_sub_seq_layer_num = 0
            (0b00100, 5),     // ref_sub_seq_id = 3
            (1, 1),           // ref_sub_seq_direction = 1
        ]);
        let got = parse_sub_seq_characteristics(&payload).unwrap();
        assert_eq!(got.sub_seq_layer_num, 1);
        assert_eq!(got.sub_seq_id, 2);
        assert_eq!(got.sub_seq_duration, Some(0xDEADBEEF));
        let rate = got.average_rate.expect("average_rate present");
        assert!(rate.accurate_statistics_flag);
        assert_eq!(rate.average_bit_rate, 0xAAAA);
        assert_eq!(rate.average_frame_rate, 0x5555);
        assert_eq!(got.referenced.len(), 1);
        assert_eq!(got.referenced[0].ref_sub_seq_layer_num, 0);
        assert_eq!(got.referenced[0].ref_sub_seq_id, 3);
        assert!(got.referenced[0].ref_sub_seq_direction);
    }

    #[test]
    fn sub_seq_characteristics_minimal() {
        // layer=0, id=0, duration_flag=0, average_rate_flag=0,
        // num_referenced_subseqs=0.
        let payload = pack_bits(&[
            (1, 1), // sub_seq_layer_num = 0
            (1, 1), // sub_seq_id = 0
            (0, 1), // duration_flag
            (0, 1), // average_rate_flag
            (1, 1), // num_referenced_subseqs = 0
        ]);
        let got = parse_sub_seq_characteristics(&payload).unwrap();
        assert_eq!(got.sub_seq_layer_num, 0);
        assert_eq!(got.sub_seq_id, 0);
        assert!(got.sub_seq_duration.is_none());
        assert!(got.average_rate.is_none());
        assert!(got.referenced.is_empty());
    }

    #[test]
    fn parse_payload_dispatches_sub_seq_characteristics() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(1, 1), (1, 1), (0, 1), (0, 1), (1, 1)]);
        match parse_payload(12, &payload, &ctx).unwrap() {
            SeiPayload::SubSeqCharacteristics(s) => {
                assert_eq!(s.sub_seq_layer_num, 0);
                assert!(s.referenced.is_empty());
            }
            other => panic!("expected SubSeqCharacteristics, got {other:?}"),
        }
    }

    // --- §D.2.13 sub_seq_duration_seconds typed accessor ---------------

    #[test]
    fn sub_seq_duration_seconds_none_when_unspecified() {
        // duration_flag == 0 ⇒ sub_seq_duration is None per spec
        // ("duration_flag equal to 0 indicates that the duration of the
        // target sub-sequence is not specified") — no scalar derivation.
        let c = SubSeqCharacteristics {
            sub_seq_layer_num: 0,
            sub_seq_id: 0,
            sub_seq_duration: None,
            average_rate: None,
            referenced: Vec::new(),
        };
        assert!(c.sub_seq_duration_seconds().is_none());
    }

    #[test]
    fn sub_seq_duration_seconds_minimum_nonzero() {
        // Smallest non-zero carrier: 1 tick of a 90-kHz clock is exactly
        // 1/90_000 second; with denominator a power-of-five times a power
        // of two the value is non-dyadic, so check via the exact integer
        // identity ticks * 90_000 == reconstructed * 90_000² (i.e. the
        // accessor's f64 result times 90_000 returns to 1.0 exactly).
        let c = SubSeqCharacteristics {
            sub_seq_layer_num: 0,
            sub_seq_id: 0,
            sub_seq_duration: Some(1),
            average_rate: None,
            referenced: Vec::new(),
        };
        let s = c.sub_seq_duration_seconds().unwrap();
        // 1 / 90_000 ≈ 1.111…e-5; verify the round-trip pins to 1 tick.
        assert!((s * 90_000.0 - 1.0).abs() < 1e-12);
    }

    #[test]
    fn sub_seq_duration_seconds_one_second() {
        // 90_000 ticks == 1 second exactly (the 90-kHz tick rate is
        // defined so an integer second is an integer tick count).
        let c = SubSeqCharacteristics {
            sub_seq_layer_num: 0,
            sub_seq_id: 0,
            sub_seq_duration: Some(90_000),
            average_rate: None,
            referenced: Vec::new(),
        };
        assert_eq!(c.sub_seq_duration_seconds(), Some(1.0));
    }

    #[test]
    fn sub_seq_duration_seconds_one_frame_at_30fps() {
        // 3_000 ticks == 1 frame at 30 fps (30 * 3_000 == 90_000); a
        // common GOP-edge unit and exactly representable in f64.
        let c = SubSeqCharacteristics {
            sub_seq_layer_num: 0,
            sub_seq_id: 0,
            sub_seq_duration: Some(3_000),
            average_rate: None,
            referenced: Vec::new(),
        };
        let s = c.sub_seq_duration_seconds().unwrap();
        assert!((s - 1.0 / 30.0).abs() < 1e-12);
    }

    #[test]
    fn sub_seq_duration_seconds_u32_ceiling() {
        // Carrier ceiling u32::MAX == 4_294_967_295 ticks ≈ 47_721.86 s
        // ≈ 13 h 15 min 21.86 s — pin the exact f64 quotient.
        let c = SubSeqCharacteristics {
            sub_seq_layer_num: 0,
            sub_seq_id: 0,
            sub_seq_duration: Some(u32::MAX),
            average_rate: None,
            referenced: Vec::new(),
        };
        let expected = f64::from(u32::MAX) / 90_000.0;
        assert_eq!(c.sub_seq_duration_seconds(), Some(expected));
    }

    #[test]
    fn sub_seq_duration_seconds_roundtrip_via_parse() {
        // End-to-end via parse_sub_seq_characteristics: pack a payload
        // with duration_flag=1 and sub_seq_duration=180_000 (== 2 s).
        let payload = pack_bits(&[
            (1, 1),        // sub_seq_layer_num = 0
            (1, 1),        // sub_seq_id = 0
            (1, 1),        // duration_flag = 1
            (180_000, 32), // sub_seq_duration = 180_000 ticks == 2 s
            (0, 1),        // average_rate_flag = 0
            (1, 1),        // num_referenced_subseqs = 0
        ]);
        let got = parse_sub_seq_characteristics(&payload).unwrap();
        assert_eq!(got.sub_seq_duration, Some(180_000));
        assert_eq!(got.sub_seq_duration_seconds(), Some(2.0));
    }

    #[test]
    fn sub_seq_duration_seconds_roundtrip_unspecified() {
        // End-to-end via parse_sub_seq_characteristics with
        // duration_flag=0: typed accessor must mirror the on-wire None.
        let payload = pack_bits(&[
            (1, 1), // sub_seq_layer_num = 0
            (1, 1), // sub_seq_id = 0
            (0, 1), // duration_flag = 0
            (0, 1), // average_rate_flag = 0
            (1, 1), // num_referenced_subseqs = 0
        ]);
        let got = parse_sub_seq_characteristics(&payload).unwrap();
        assert!(got.sub_seq_duration.is_none());
        assert!(got.sub_seq_duration_seconds().is_none());
    }

    #[test]
    fn progressive_refinement_segment_start_basic() {
        // id=2 (ue=2 → "011"), steps_minus1=0 (ue=0 → "1").
        let payload = pack_bits(&[(0b011, 3), (1, 1)]);
        let got = parse_progressive_refinement_segment_start(&payload).unwrap();
        assert_eq!(got.progressive_refinement_id, 2);
        assert_eq!(got.num_refinement_steps_minus1, 0);
    }

    #[test]
    fn progressive_refinement_num_refinement_steps_minimum() {
        // num_refinement_steps_minus1 == 0 ⇒ NumRefinementSteps == 1.
        let r = ProgressiveRefinementSegmentStart {
            progressive_refinement_id: 0,
            num_refinement_steps_minus1: 0,
        };
        assert_eq!(r.num_refinement_steps(), 1);
    }

    #[test]
    fn progressive_refinement_num_refinement_steps_typical() {
        // A typical small refinement segment with three steps:
        // num_refinement_steps_minus1 == 2 ⇒ NumRefinementSteps == 3.
        let r = ProgressiveRefinementSegmentStart {
            progressive_refinement_id: 7,
            num_refinement_steps_minus1: 2,
        };
        assert_eq!(r.num_refinement_steps(), 3);
    }

    #[test]
    fn progressive_refinement_num_refinement_steps_u32_ceiling() {
        // num_refinement_steps_minus1 == u32::MAX must not overflow
        // the u64 return: u32::MAX + 1 == 4_294_967_296.
        let r = ProgressiveRefinementSegmentStart {
            progressive_refinement_id: 0,
            num_refinement_steps_minus1: u32::MAX,
        };
        assert_eq!(r.num_refinement_steps(), u64::from(u32::MAX) + 1);
        assert_eq!(r.num_refinement_steps(), 4_294_967_296);
    }

    #[test]
    fn progressive_refinement_num_refinement_steps_parse_round_trip() {
        // id=5 (ue=5 → "00110"), steps_minus1=9 (ue=9 → "0001010").
        // After parsing, NumRefinementSteps should be 9 + 1 = 10.
        let payload = pack_bits(&[(0b00110, 5), (0b0001010, 7)]);
        let got = parse_progressive_refinement_segment_start(&payload).unwrap();
        assert_eq!(got.progressive_refinement_id, 5);
        assert_eq!(got.num_refinement_steps_minus1, 9);
        assert_eq!(got.num_refinement_steps(), 10);
    }

    #[test]
    fn progressive_refinement_segment_end_basic() {
        // id=3 (ue=3 → "00100").
        let payload = pack_bits(&[(0b00100, 5)]);
        let got = parse_progressive_refinement_segment_end(&payload).unwrap();
        assert_eq!(got.progressive_refinement_id, 3);
    }

    #[test]
    fn parse_payload_dispatches_progressive_refinement() {
        let ctx = SeiContext::default();
        let payload_start = pack_bits(&[(1, 1), (1, 1)]); // id=0, steps=0
        match parse_payload(16, &payload_start, &ctx).unwrap() {
            SeiPayload::ProgressiveRefinementSegmentStart(p) => {
                assert_eq!(p.progressive_refinement_id, 0);
                assert_eq!(p.num_refinement_steps_minus1, 0);
            }
            other => panic!("expected ProgressiveRefinementSegmentStart, got {other:?}"),
        }
        let payload_end = pack_bits(&[(1, 1)]); // id=0
        match parse_payload(17, &payload_end, &ctx).unwrap() {
            SeiPayload::ProgressiveRefinementSegmentEnd(p) => {
                assert_eq!(p.progressive_refinement_id, 0)
            }
            other => panic!("expected ProgressiveRefinementSegmentEnd, got {other:?}"),
        }
    }

    #[test]
    fn stereo_video_info_field_views_path() {
        // field_views=1 → reads top_field_is_left_view_flag (1) then the
        // two self_contained flags (1, 0). 5 bits, padded to 1 byte.
        let payload = pack_bits(&[(1, 1), (1, 1), (1, 1), (0, 1)]);
        let got = parse_stereo_video_info(&payload).unwrap();
        assert!(got.field_views_flag);
        assert_eq!(got.top_field_is_left_view_flag, Some(true));
        assert!(got.current_frame_is_left_view_flag.is_none());
        assert!(got.next_frame_is_second_view_flag.is_none());
        assert!(got.left_view_self_contained_flag);
        assert!(!got.right_view_self_contained_flag);
    }

    #[test]
    fn stereo_video_info_frame_views_path() {
        // field_views=0 → reads current_frame_is_left=1, next_frame_is_second=0,
        // then left_self=1, right_self=1.
        let payload = pack_bits(&[(0, 1), (1, 1), (0, 1), (1, 1), (1, 1)]);
        let got = parse_stereo_video_info(&payload).unwrap();
        assert!(!got.field_views_flag);
        assert!(got.top_field_is_left_view_flag.is_none());
        assert_eq!(got.current_frame_is_left_view_flag, Some(true));
        assert_eq!(got.next_frame_is_second_view_flag, Some(false));
        assert!(got.left_view_self_contained_flag);
        assert!(got.right_view_self_contained_flag);
    }

    #[test]
    fn parse_payload_dispatches_stereo_video_info() {
        let ctx = SeiContext::default();
        let payload = pack_bits(&[(1, 1), (0, 1), (1, 1), (1, 1)]);
        match parse_payload(21, &payload, &ctx).unwrap() {
            SeiPayload::StereoVideoInfo(s) => {
                assert!(s.field_views_flag);
                assert_eq!(s.top_field_is_left_view_flag, Some(false));
            }
            other => panic!("expected StereoVideoInfo, got {other:?}"),
        }
    }

    #[test]
    fn motion_constrained_slice_group_set_single_group_collapses_width() {
        // num_slice_groups = 1 → slice_group_id is 0 bits wide, so we
        // only consume:
        //   num_slice_groups_in_set_minus1 = 0 (ue=0 → "1") = 1 bit
        //   exact_sample_value_match_flag = 1
        //   pan_scan_rect_flag = 0
        // Total = 3 bits.
        let payload = pack_bits(&[(1, 1), (1, 1), (0, 1)]);
        let ctx = SeiContext {
            num_slice_groups: 1,
            ..SeiContext::default()
        };
        let got = parse_motion_constrained_slice_group_set(&payload, &ctx).unwrap();
        assert_eq!(got.slice_group_ids, vec![0]);
        assert!(got.exact_sample_value_match_flag);
        assert!(!got.pan_scan_rect_flag);
        assert!(got.pan_scan_rect_id.is_none());
    }

    #[test]
    fn motion_constrained_slice_group_set_multi_group_reads_u_v() {
        // num_slice_groups = 4 → width = Ceil(Log2(4)) = 2 bits per id.
        //   num_slice_groups_in_set_minus1 = 1 (ue=1 → "010") = 3 bits.
        //   slice_group_id[0] = 1 (2 bits), slice_group_id[1] = 3 (2 bits).
        //   exact_sample_value_match_flag = 0.
        //   pan_scan_rect_flag = 1.
        //   pan_scan_rect_id = 5 (ue=5 → "00110") = 5 bits.
        let payload = pack_bits(&[
            (0b010, 3),   // count_minus1 = 1
            (0b01, 2),    // id[0] = 1
            (0b11, 2),    // id[1] = 3
            (0, 1),       // exact_match = 0
            (1, 1),       // pan_scan_flag = 1
            (0b00110, 5), // pan_scan_id = 5
        ]);
        let ctx = SeiContext {
            num_slice_groups: 4,
            ..SeiContext::default()
        };
        let got = parse_motion_constrained_slice_group_set(&payload, &ctx).unwrap();
        assert_eq!(got.slice_group_ids, vec![1, 3]);
        assert!(!got.exact_sample_value_match_flag);
        assert!(got.pan_scan_rect_flag);
        assert_eq!(got.pan_scan_rect_id, Some(5));
    }

    #[test]
    fn parse_payload_dispatches_motion_constrained_slice_group_set() {
        let payload = pack_bits(&[(1, 1), (0, 1), (0, 1)]);
        let ctx = SeiContext::default();
        match parse_payload(18, &payload, &ctx).unwrap() {
            SeiPayload::MotionConstrainedSliceGroupSet(s) => {
                assert_eq!(s.slice_group_ids, vec![0]);
                assert!(!s.exact_sample_value_match_flag);
            }
            other => panic!("expected MotionConstrainedSliceGroupSet, got {other:?}"),
        }
    }

    // ----- Round 78 — ambient_viewing_environment / equirectangular_projection /
    // cubemap_projection / sphere_rotation -----

    // Helper: encode a u32 / u16 big-endian to a Vec<u8>.
    fn u32_be(v: u32) -> [u8; 4] {
        v.to_be_bytes()
    }
    fn u16_be(v: u16) -> [u8; 2] {
        v.to_be_bytes()
    }
    fn i32_be(v: i32) -> [u8; 4] {
        v.to_be_bytes()
    }

    #[test]
    fn ambient_viewing_environment_bt2035_d65() {
        // §D.2.34 BT.2035 example: illuminance=100_000, D65 chromaticity
        // (15_635, 16_450).
        let mut payload = Vec::new();
        payload.extend_from_slice(&u32_be(100_000));
        payload.extend_from_slice(&u16_be(15_635));
        payload.extend_from_slice(&u16_be(16_450));
        let got = parse_ambient_viewing_environment(&payload).unwrap();
        assert_eq!(got.ambient_illuminance, 100_000);
        assert_eq!(got.ambient_light_x, 15_635);
        assert_eq!(got.ambient_light_y, 16_450);
    }

    #[test]
    fn ambient_viewing_environment_zero_illuminance_rejected() {
        let mut payload = Vec::new();
        payload.extend_from_slice(&u32_be(0));
        payload.extend_from_slice(&u16_be(0));
        payload.extend_from_slice(&u16_be(0));
        let err = parse_ambient_viewing_environment(&payload).unwrap_err();
        assert_eq!(err, SeiError::AmbientIlluminanceZero);
    }

    #[test]
    fn ambient_viewing_environment_chromaticity_out_of_range_rejected() {
        // ambient_light_x = 50_001 (just outside 0..=50_000).
        let mut payload = Vec::new();
        payload.extend_from_slice(&u32_be(1));
        payload.extend_from_slice(&u16_be(50_001));
        payload.extend_from_slice(&u16_be(0));
        let err = parse_ambient_viewing_environment(&payload).unwrap_err();
        assert_eq!(err, SeiError::AmbientLightOutOfRange { x: 50_001, y: 0 });
    }

    #[test]
    fn parse_payload_dispatches_ambient_viewing_environment() {
        let mut payload = Vec::new();
        payload.extend_from_slice(&u32_be(50_000));
        payload.extend_from_slice(&u16_be(14_155));
        payload.extend_from_slice(&u16_be(14_855));
        let ctx = SeiContext::default();
        match parse_payload(148, &payload, &ctx).unwrap() {
            SeiPayload::AmbientViewingEnvironment(a) => {
                assert_eq!(a.ambient_illuminance, 50_000);
                assert_eq!(a.ambient_light_x, 14_155);
                assert_eq!(a.ambient_light_y, 14_855);
            }
            other => panic!("expected AmbientViewingEnvironment, got {other:?}"),
        }
    }

    // ----- Round 107 — content_colour_volume (payload type 149) -----

    // Helper: build the one-byte content_colour_volume header given the
    // cancel flag, persistence flag, and the four "present" flags. The
    // reserved 2 bits are written as 0. The header is exactly 8 bits, so
    // any subsequent i(32) / u(32) fields stay byte-aligned and can be
    // appended big-endian.
    fn ccv_header(cancel: u8, persistence: u8, prim: u8, min: u8, max: u8, avg: u8) -> Vec<u8> {
        pack_bits(&[
            (cancel as u64, 1),
            (persistence as u64, 1),
            (prim as u64, 1),
            (min as u64, 1),
            (max as u64, 1),
            (avg as u64, 1),
            (0, 2), // ccv_reserved_zero_2bits
        ])
    }

    #[test]
    fn content_colour_volume_cancel() {
        // cancel_flag = 1 → no body; only the first bit is consumed but
        // the header byte is fully padded.
        let payload = pack_bits(&[(1, 1)]);
        let got = parse_content_colour_volume(&payload).unwrap();
        assert!(got.cancel_flag);
        assert!(got.body.is_none());
    }

    #[test]
    fn content_colour_volume_all_present_bt2020() {
        // cancel=0, persistence=1, all four present. Primaries are the
        // suggested green/blue/red ordering using BT.2020-style coords
        // scaled to 0.00002 increments; luminance min<=avg<=max.
        let mut payload = ccv_header(0, 1, 1, 1, 1, 1);
        // green (0): x=8500, y=39850 ; blue (1): x=6550, y=2300 ;
        // red (2): x=35400, y=14600  (illustrative, within range).
        for &(x, y) in &[(8_500i32, 39_850i32), (6_550, 2_300), (35_400, 14_600)] {
            payload.extend_from_slice(&i32_be(x));
            payload.extend_from_slice(&i32_be(y));
        }
        payload.extend_from_slice(&u32_be(1)); // min
        payload.extend_from_slice(&u32_be(100_000_000)); // max
        payload.extend_from_slice(&u32_be(50_000_000)); // avg
        let got = parse_content_colour_volume(&payload).unwrap();
        assert!(!got.cancel_flag);
        let body = got.body.expect("body present");
        assert!(body.persistence_flag);
        let prims = body.primaries.expect("primaries present");
        assert_eq!(
            prims[0],
            ContentColourVolumePrimary {
                x: 8_500,
                y: 39_850
            }
        );
        assert_eq!(prims[1], ContentColourVolumePrimary { x: 6_550, y: 2_300 });
        assert_eq!(
            prims[2],
            ContentColourVolumePrimary {
                x: 35_400,
                y: 14_600
            }
        );
        assert_eq!(body.min_luminance_value, Some(1));
        assert_eq!(body.max_luminance_value, Some(100_000_000));
        assert_eq!(body.avg_luminance_value, Some(50_000_000));
    }

    #[test]
    fn content_colour_volume_luminance_only() {
        // No primaries; only max luminance present. Exercises the
        // "some present flags 0" branch and a single-field body.
        let mut payload = ccv_header(0, 0, 0, 0, 1, 0);
        payload.extend_from_slice(&u32_be(42_000_000));
        let got = parse_content_colour_volume(&payload).unwrap();
        let body = got.body.expect("body present");
        assert!(!body.persistence_flag);
        assert!(body.primaries.is_none());
        assert!(body.min_luminance_value.is_none());
        assert_eq!(body.max_luminance_value, Some(42_000_000));
        assert!(body.avg_luminance_value.is_none());
    }

    #[test]
    fn content_colour_volume_negative_primary_coord() {
        // A negative primary coordinate inside [-5_000_000, 5_000_000]
        // round-trips through i(32) two's-complement decoding.
        let mut payload = ccv_header(0, 0, 1, 0, 0, 0);
        for &(x, y) in &[
            (-1_000_000i32, 2_000_000i32),
            (0, 0),
            (5_000_000, -5_000_000),
        ] {
            payload.extend_from_slice(&i32_be(x));
            payload.extend_from_slice(&i32_be(y));
        }
        let got = parse_content_colour_volume(&payload).unwrap();
        let prims = got.body.unwrap().primaries.unwrap();
        assert_eq!(
            prims[0],
            ContentColourVolumePrimary {
                x: -1_000_000,
                y: 2_000_000
            }
        );
        assert_eq!(
            prims[2],
            ContentColourVolumePrimary {
                x: 5_000_000,
                y: -5_000_000
            }
        );
    }

    #[test]
    fn content_colour_volume_no_components_rejected() {
        // §D.2.33: all four present flags equal 0 is a conformance
        // violation.
        let payload = ccv_header(0, 1, 0, 0, 0, 0);
        let err = parse_content_colour_volume(&payload).unwrap_err();
        assert_eq!(err, SeiError::ContentColourVolumeNoComponents);
    }

    #[test]
    fn content_colour_volume_primary_out_of_range_rejected() {
        // ccv_primaries_x[0] = 5_000_001, just past the upper bound.
        let mut payload = ccv_header(0, 0, 1, 0, 0, 0);
        payload.extend_from_slice(&i32_be(5_000_001));
        payload.extend_from_slice(&i32_be(0));
        // Remaining primary coords to keep the read in-bounds (the
        // error fires on c == 0 before these matter, but provide them
        // anyway).
        for _ in 0..4 {
            payload.extend_from_slice(&i32_be(0));
        }
        let err = parse_content_colour_volume(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ContentColourVolumePrimaryOutOfRange {
                c: 0,
                x: 5_000_001,
                y: 0
            }
        );
    }

    #[test]
    fn content_colour_volume_luminance_order_min_gt_max_rejected() {
        // min present (100), max present (50): min > max violates §D.2.33.
        let mut payload = ccv_header(0, 0, 0, 1, 1, 0);
        payload.extend_from_slice(&u32_be(100)); // min
        payload.extend_from_slice(&u32_be(50)); // max
        let err = parse_content_colour_volume(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ContentColourVolumeLuminanceOrder {
                min: 100,
                avg: 100,
                max: 50
            }
        );
    }

    #[test]
    fn content_colour_volume_luminance_order_min_gt_avg_rejected() {
        // min (10) > avg (5): violates §D.2.33 even with no max.
        let mut payload = ccv_header(0, 0, 0, 1, 0, 1);
        payload.extend_from_slice(&u32_be(10)); // min
        payload.extend_from_slice(&u32_be(5)); // avg
        let err = parse_content_colour_volume(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ContentColourVolumeLuminanceOrder {
                min: 10,
                avg: 5,
                max: u64::MAX
            }
        );
    }

    #[test]
    fn content_colour_volume_luminance_order_avg_gt_max_rejected() {
        // avg (80) > max (60): violates §D.2.33 with no min present.
        let mut payload = ccv_header(0, 0, 0, 0, 1, 1);
        payload.extend_from_slice(&u32_be(60)); // max
        payload.extend_from_slice(&u32_be(80)); // avg
        let err = parse_content_colour_volume(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ContentColourVolumeLuminanceOrder {
                min: 0,
                avg: 80,
                max: 60
            }
        );
    }

    #[test]
    fn parse_payload_dispatches_content_colour_volume() {
        let mut payload = ccv_header(0, 1, 0, 0, 1, 0);
        payload.extend_from_slice(&u32_be(10_000_000));
        let ctx = SeiContext::default();
        match parse_payload(149, &payload, &ctx).unwrap() {
            SeiPayload::ContentColourVolume(c) => {
                assert!(!c.cancel_flag);
                let body = c.body.unwrap();
                assert!(body.persistence_flag);
                assert_eq!(body.max_luminance_value, Some(10_000_000));
            }
            other => panic!("expected ContentColourVolume, got {other:?}"),
        }
    }

    // -- Round 117: colour_remapping_info (payload type 142, §D.2.30) --

    #[test]
    fn colour_remapping_info_cancel() {
        // colour_remap_id = ue(2) → codeNum 2 = "011"; cancel_flag = 1.
        let payload = pack_bits(&[(0b011, 3), (1, 1)]);
        let got = parse_colour_remapping_info(&payload).unwrap();
        assert_eq!(got.colour_remap_id, 2);
        assert!(got.cancel_flag);
        assert!(got.body.is_none());
    }

    #[test]
    fn colour_remapping_info_minimal_no_optionals() {
        // id = ue(0) = "1"; cancel = 0; repetition_period = ue(0) = "1";
        // video_signal_info_present = 0; input_bd = 8; output_bd = 8;
        // pre-LUT: three num_val_minus1 = 0; matrix_present = 0;
        // post-LUT: three num_val_minus1 = 0.
        let payload = pack_bits(&[
            (1, 1), // id ue=0
            (0, 1), // cancel
            (1, 1), // repetition_period ue=0
            (0, 1), // video_signal_info_present
            (8, 8), // input_bit_depth
            (8, 8), // output_bit_depth
            (0, 8), // pre num_val_minus1[0]
            (0, 8), // pre num_val_minus1[1]
            (0, 8), // pre num_val_minus1[2]
            (0, 1), // matrix_present
            (0, 8), // post num_val_minus1[0]
            (0, 8), // post num_val_minus1[1]
            (0, 8), // post num_val_minus1[2]
        ]);
        let got = parse_colour_remapping_info(&payload).unwrap();
        assert_eq!(got.colour_remap_id, 0);
        assert!(!got.cancel_flag);
        let body = got.body.unwrap();
        assert_eq!(body.repetition_period, 0);
        assert!(body.video_signal_info.is_none());
        assert_eq!(body.input_bit_depth, 8);
        assert_eq!(body.output_bit_depth, 8);
        assert!(body.pre_lut.iter().all(|c| c.is_empty()));
        assert!(body.matrix.is_none());
        assert!(body.post_lut.iter().all(|c| c.is_empty()));
    }

    #[test]
    fn colour_remapping_info_full_8bit() {
        // Full message at 8-bit input/output (LUT values are 8 bits).
        // video signal info present (BT.709-ish: prim=1, tf=1, mc=1),
        // a 2-pivot pre-LUT on component 0 only, an identity-ish matrix
        // with log2_matrix_denom = 5, and a 2-pivot post-LUT on comp 2.
        let payload = pack_bits(&[
            (0b010, 3), // id ue=1 ("010")
            (0, 1),     // cancel
            (1, 1),     // repetition_period ue=0
            (1, 1),     // video_signal_info_present
            (1, 1),     // full_range_flag
            (1, 8),     // primaries
            (1, 8),     // transfer_function
            (1, 8),     // matrix_coefficients
            (8, 8),     // input_bit_depth = 8 → 8-bit LUT codes
            (8, 8),     // output_bit_depth = 8 → 8-bit LUT targets
            // pre-LUT comp 0: num_val_minus1 = 1 → 2 entries
            (1, 8),
            (16, 8),  // coded[0]
            (32, 8),  // target[0]
            (200, 8), // coded[1]
            (220, 8), // target[1]
            (0, 8),   // pre comp 1: none
            (0, 8),   // pre comp 2: none
            // matrix present
            (1, 1), // matrix_present
            (5, 4), // log2_matrix_denom
            // colour_remap_coeffs[c][i], se(v): 32,0,0 / 0,32,0 / 0,0,32
            // se(32) ⇒ codeNum 63 ⇒ ue value n = 64 in 13 bits
            // (6 leading zeros + 7-bit "1000000"); se(0) ⇒ "1".
            (64, 13), // se(32)
            (1, 1),   // se(0)
            (1, 1),   // se(0)
            (1, 1),   // se(0)
            (64, 13), // se(32)
            (1, 1),   // se(0)
            (1, 1),   // se(0)
            (1, 1),   // se(0)
            (64, 13), // se(32)
            // post-LUT comp 0: none
            (0, 8),
            (0, 8), // post comp 1: none
            // post comp 2: num_val_minus1 = 1 → 2 entries
            (1, 8),
            (5, 8),   // coded[0]
            (9, 8),   // target[0]
            (250, 8), // coded[1]
            (255, 8), // target[1]
        ]);
        let got = parse_colour_remapping_info(&payload).unwrap();
        assert_eq!(got.colour_remap_id, 1);
        let body = got.body.unwrap();
        let vsi = body.video_signal_info.unwrap();
        assert!(vsi.full_range_flag);
        assert_eq!(vsi.primaries, 1);
        assert_eq!(vsi.transfer_function, 1);
        assert_eq!(vsi.matrix_coefficients, 1);
        assert_eq!(body.pre_lut[0].len(), 2);
        assert_eq!(body.pre_lut[0][0].coded_value, 16);
        assert_eq!(body.pre_lut[0][0].target_value, 32);
        assert_eq!(body.pre_lut[0][1].coded_value, 200);
        assert_eq!(body.pre_lut[0][1].target_value, 220);
        assert!(body.pre_lut[1].is_empty());
        assert!(body.pre_lut[2].is_empty());
        let m = body.matrix.unwrap();
        assert_eq!(m.log2_matrix_denom, 5);
        assert_eq!(m.coeffs, [[32, 0, 0], [0, 32, 0], [0, 0, 32]]);
        assert!(body.post_lut[0].is_empty());
        assert!(body.post_lut[1].is_empty());
        assert_eq!(body.post_lut[2].len(), 2);
        assert_eq!(body.post_lut[2][1].coded_value, 250);
        assert_eq!(body.post_lut[2][1].target_value, 255);
    }

    #[test]
    fn colour_remapping_info_10bit_uses_16bit_lut_values() {
        // input_bit_depth = 10 → ((10+7)>>3)<<3 = 16-bit LUT codes;
        // output_bit_depth = 12 → 16-bit targets. One pivot on comp 1.
        let payload = pack_bits(&[
            (1, 1),  // id ue=0
            (0, 1),  // cancel
            (1, 1),  // repetition_period ue=0
            (0, 1),  // video_signal_info_present
            (10, 8), // input_bit_depth = 10
            (12, 8), // output_bit_depth = 12
            (0, 8),  // pre comp 0: none
            // pre comp 1: num_val_minus1 = 1 → 2 entries, 16-bit each
            (1, 8),
            (500, 16),  // coded[0]
            (1000, 16), // target[0]
            (1000, 16), // coded[1]
            (3000, 16), // target[1]
            (0, 8),     // pre comp 2: none
            (0, 1),     // matrix_present = 0
            (0, 8),     // post comp 0
            (0, 8),     // post comp 1
            (0, 8),     // post comp 2
        ]);
        let got = parse_colour_remapping_info(&payload).unwrap();
        let body = got.body.unwrap();
        assert_eq!(body.input_bit_depth, 10);
        assert_eq!(body.output_bit_depth, 12);
        assert_eq!(body.pre_lut[1].len(), 2);
        assert_eq!(body.pre_lut[1][0].coded_value, 500);
        assert_eq!(body.pre_lut[1][0].target_value, 1000);
        assert_eq!(body.pre_lut[1][1].coded_value, 1000);
        assert_eq!(body.pre_lut[1][1].target_value, 3000);
    }

    #[test]
    fn colour_remapping_info_negative_matrix_coeff() {
        // se(-1) = codeNum 2 = "011". Verify signed coeff round-trips.
        let payload = pack_bits(&[
            (1, 1), // id ue=0
            (0, 1), // cancel
            (1, 1), // repetition_period ue=0
            (0, 1), // video_signal_info_present
            (8, 8), // input
            (8, 8), // output
            (0, 8), // pre 0
            (0, 8), // pre 1
            (0, 8), // pre 2
            (1, 1), // matrix_present
            (0, 4), // log2_matrix_denom = 0
            // coeffs: all se(-1) = "011"
            (0b011, 3),
            (0b011, 3),
            (0b011, 3),
            (0b011, 3),
            (0b011, 3),
            (0b011, 3),
            (0b011, 3),
            (0b011, 3),
            (0b011, 3),
            (0, 8), // post 0
            (0, 8), // post 1
            (0, 8), // post 2
        ]);
        let got = parse_colour_remapping_info(&payload).unwrap();
        let m = got.body.unwrap().matrix.unwrap();
        assert_eq!(m.log2_matrix_denom, 0);
        assert_eq!(m.coeffs, [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]);
    }

    #[test]
    fn colour_remapping_info_input_bit_depth_too_low_rejected() {
        // input_bit_depth = 7 (reserved) → rejected per §D.2.30.
        let payload = pack_bits(&[
            (1, 1), // id ue=0
            (0, 1), // cancel
            (1, 1), // repetition_period ue=0
            (0, 1), // video_signal_info_present
            (7, 8), // input_bit_depth = 7 (out of 8..=16)
            (8, 8), // output_bit_depth
        ]);
        let err = parse_colour_remapping_info(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ColourRemapBitDepthOutOfRange {
                which: "input",
                got: 7
            }
        );
    }

    #[test]
    fn colour_remapping_info_repetition_period_out_of_range_rejected() {
        // repetition_period = 16385 → ue codeNum 16385.
        // ue(16385): 16385+1 = 16386 = 0b100000000000010, 15 bits ⇒
        // 14 leading zeros, then the 15-bit value.
        let payload = pack_bits(&[
            (1, 1),                  // id ue=0
            (0, 1),                  // cancel
            (0, 14),                 // ue prefix: 14 leading zeros
            (0b100000000000010, 15), // 15-bit suffix → codeNum 16385
        ]);
        let err = parse_colour_remapping_info(&payload).unwrap_err();
        assert_eq!(err, SeiError::ColourRemapRepetitionPeriodOutOfRange(16385));
    }

    #[test]
    fn colour_remapping_info_lut_count_out_of_range_rejected() {
        // pre_lut_num_val_minus1[0] = 33 (> 32) → rejected.
        let payload = pack_bits(&[
            (1, 1),  // id ue=0
            (0, 1),  // cancel
            (1, 1),  // repetition_period ue=0
            (0, 1),  // video_signal_info_present
            (8, 8),  // input
            (8, 8),  // output
            (33, 8), // pre num_val_minus1[0] = 33
        ]);
        let err = parse_colour_remapping_info(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::ColourRemapLutCountOutOfRange {
                which: "pre",
                c: 0,
                got: 33
            }
        );
    }

    #[test]
    fn parse_payload_dispatches_colour_remapping_info() {
        // Minimal cancel message routed through parse_payload(142, ..).
        let payload = pack_bits(&[(1, 1), (1, 1)]); // id ue=0, cancel=1
        let ctx = SeiContext::default();
        match parse_payload(142, &payload, &ctx).unwrap() {
            SeiPayload::ColourRemappingInfo(c) => {
                assert_eq!(c.colour_remap_id, 0);
                assert!(c.cancel_flag);
                assert!(c.body.is_none());
            }
            other => panic!("expected ColourRemappingInfo, got {other:?}"),
        }
    }

    #[test]
    fn equirectangular_projection_cancel() {
        // cancel_flag = 1 → body skipped.
        let payload = pack_bits(&[(1, 1)]);
        let got = parse_equirectangular_projection(&payload).unwrap();
        assert!(got.cancel_flag);
        assert!(got.persistence_flag.is_none());
        assert!(got.padding_flag.is_none());
        assert!(got.gb_erp_type.is_none());
    }

    #[test]
    fn equirectangular_projection_no_padding() {
        // cancel=0, persistence=1, padding=0, reserved_zero_2bits=0.
        let payload = pack_bits(&[
            (0, 1), // cancel
            (1, 1), // persistence
            (0, 1), // padding
            (0, 2), // reserved
        ]);
        let got = parse_equirectangular_projection(&payload).unwrap();
        assert!(!got.cancel_flag);
        assert_eq!(got.persistence_flag, Some(true));
        assert_eq!(got.padding_flag, Some(false));
        assert!(got.gb_erp_type.is_none());
        assert!(got.left_gb_erp_width.is_none());
        assert!(got.right_gb_erp_width.is_none());
    }

    #[test]
    fn equirectangular_projection_with_padding() {
        // cancel=0, persistence=0, padding=1, reserved_zero_2bits=0,
        // gb_erp_type=2, left=8, right=4.
        let payload = pack_bits(&[
            (0, 1), // cancel
            (0, 1), // persistence
            (1, 1), // padding
            (0, 2), // reserved
            (2, 3), // gb_erp_type
            (8, 8), // left
            (4, 8), // right
        ]);
        let got = parse_equirectangular_projection(&payload).unwrap();
        assert!(!got.cancel_flag);
        assert_eq!(got.persistence_flag, Some(false));
        assert_eq!(got.padding_flag, Some(true));
        assert_eq!(got.gb_erp_type, Some(2));
        assert_eq!(got.left_gb_erp_width, Some(8));
        assert_eq!(got.right_gb_erp_width, Some(4));
    }

    #[test]
    fn parse_payload_dispatches_equirectangular_projection() {
        let payload = pack_bits(&[(1, 1)]);
        let ctx = SeiContext::default();
        match parse_payload(150, &payload, &ctx).unwrap() {
            SeiPayload::EquirectangularProjection(e) => assert!(e.cancel_flag),
            other => panic!("expected EquirectangularProjection, got {other:?}"),
        }
    }

    #[test]
    fn cubemap_projection_cancel() {
        let payload = pack_bits(&[(1, 1)]);
        let got = parse_cubemap_projection(&payload).unwrap();
        assert!(got.cancel_flag);
        assert!(got.persistence_flag.is_none());
    }

    #[test]
    fn cubemap_projection_persist() {
        let payload = pack_bits(&[(0, 1), (1, 1)]);
        let got = parse_cubemap_projection(&payload).unwrap();
        assert!(!got.cancel_flag);
        assert_eq!(got.persistence_flag, Some(true));
    }

    #[test]
    fn parse_payload_dispatches_cubemap_projection() {
        let payload = pack_bits(&[(0, 1), (0, 1)]);
        let ctx = SeiContext::default();
        match parse_payload(151, &payload, &ctx).unwrap() {
            SeiPayload::CubemapProjection(c) => {
                assert!(!c.cancel_flag);
                assert_eq!(c.persistence_flag, Some(false));
            }
            other => panic!("expected CubemapProjection, got {other:?}"),
        }
    }

    #[test]
    fn sphere_rotation_cancel() {
        let payload = pack_bits(&[(1, 1)]);
        let got = parse_sphere_rotation(&payload).unwrap();
        assert!(got.cancel_flag);
        assert!(got.yaw_rotation.is_none());
        assert!(got.pitch_rotation.is_none());
        assert!(got.roll_rotation.is_none());
    }

    #[test]
    fn sphere_rotation_zero_angles() {
        // cancel=0, persistence=1, reserved=0, then three i32(0) big-endian.
        // First byte: 0_1_000000 = 0x40. Then 12 zero bytes.
        let mut payload = vec![0x40];
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        let got = parse_sphere_rotation(&payload).unwrap();
        assert!(!got.cancel_flag);
        assert_eq!(got.persistence_flag, Some(true));
        assert_eq!(got.yaw_rotation, Some(0));
        assert_eq!(got.pitch_rotation, Some(0));
        assert_eq!(got.roll_rotation, Some(0));
    }

    #[test]
    fn sphere_rotation_min_max_angles() {
        // Test boundary values per §D.2.35.3:
        //   yaw=-180*2^16 (-11_796_480), pitch=90*2^16 (5_898_240),
        //   roll=180*2^16-1 (11_796_479).
        // cancel=0, persistence=0, reserved=0.
        let mut payload = vec![0x00];
        payload.extend_from_slice(&i32_be(-11_796_480));
        payload.extend_from_slice(&i32_be(5_898_240));
        payload.extend_from_slice(&i32_be(11_796_479));
        let got = parse_sphere_rotation(&payload).unwrap();
        assert_eq!(got.yaw_rotation, Some(-11_796_480));
        assert_eq!(got.pitch_rotation, Some(5_898_240));
        assert_eq!(got.roll_rotation, Some(11_796_479));
    }

    #[test]
    fn sphere_rotation_yaw_out_of_range_rejected() {
        // yaw=180*2^16 (11_796_480) — one past the high limit.
        let mut payload = vec![0x00];
        payload.extend_from_slice(&i32_be(11_796_480));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        let err = parse_sphere_rotation(&payload).unwrap_err();
        assert_eq!(err, SeiError::SphereRotationYawOutOfRange(11_796_480));
    }

    #[test]
    fn sphere_rotation_pitch_out_of_range_rejected() {
        // pitch=-90*2^16 - 1 (-5_898_241) — one past the low limit.
        let mut payload = vec![0x00];
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(-5_898_241));
        payload.extend_from_slice(&i32_be(0));
        let err = parse_sphere_rotation(&payload).unwrap_err();
        assert_eq!(err, SeiError::SphereRotationPitchOutOfRange(-5_898_241));
    }

    #[test]
    fn sphere_rotation_roll_out_of_range_rejected() {
        let mut payload = vec![0x00];
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(11_796_480));
        let err = parse_sphere_rotation(&payload).unwrap_err();
        assert_eq!(err, SeiError::SphereRotationRollOutOfRange(11_796_480));
    }

    #[test]
    fn parse_payload_dispatches_sphere_rotation() {
        let payload = pack_bits(&[(1, 1)]);
        let ctx = SeiContext::default();
        match parse_payload(154, &payload, &ctx).unwrap() {
            SeiPayload::SphereRotation(s) => assert!(s.cancel_flag),
            other => panic!("expected SphereRotation, got {other:?}"),
        }
    }

    // ----- Round 95 — omni_viewport / shutter_interval_info -----

    #[test]
    fn omni_viewport_cancel() {
        // viewport_id=42, cancel=1 → 10+1 bits = (42<<1)|1 = 85
        // bits MSB-first, padded.
        let payload = pack_bits(&[(42, 10), (1, 1)]);
        let got = parse_omni_viewport(&payload).unwrap();
        assert_eq!(got.viewport_id, 42);
        assert!(got.cancel_flag);
        assert!(got.persistence_flag.is_none());
        assert!(got.viewports.is_empty());
    }

    #[test]
    fn omni_viewport_single_directors_cut() {
        // viewport_id=0 (director's cut), cancel=0, persistence=1, cnt_minus1=0.
        // Header bits: u(10)=0, u(1)=0, u(1)=1, u(4)=0 → 16 bits = 2 bytes.
        // bytes 0..2: 0b00000000_00010000 → 0x00, 0x10.
        let mut payload = vec![0x00, 0x10];
        // One viewport entry: azimuth=0, elevation=0, tilt=0, hor=1, ver=1.
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&u32_be(1));
        payload.extend_from_slice(&u32_be(1));
        let got = parse_omni_viewport(&payload).unwrap();
        assert_eq!(got.viewport_id, 0);
        assert!(!got.cancel_flag);
        assert_eq!(got.persistence_flag, Some(true));
        assert_eq!(got.viewports.len(), 1);
        let v = got.viewports[0];
        assert_eq!(v.azimuth_centre, 0);
        assert_eq!(v.elevation_centre, 0);
        assert_eq!(v.tilt_centre, 0);
        assert_eq!(v.hor_range, 1);
        assert_eq!(v.ver_range, 1);
    }

    #[test]
    fn omni_viewport_max_range_boundary_accepted() {
        // viewport_id=1, cancel=0, persistence=0, cnt_minus1=0 — two entries
        // requires cnt_minus1=1; here use one entry with max-allowed values.
        // header: u(10)=1, u(1)=0, u(1)=0, u(4)=0 = 16 bits total =
        //   0b00000000_01_0_0_0000 = 0x00 0x40
        let mut payload = vec![0x00, 0x40];
        payload.extend_from_slice(&i32_be(180 * (1 << 16) - 1));
        payload.extend_from_slice(&i32_be(90 * (1 << 16)));
        payload.extend_from_slice(&i32_be(-180 * (1 << 16)));
        payload.extend_from_slice(&u32_be(360 * (1 << 16)));
        payload.extend_from_slice(&u32_be(180 * (1 << 16)));
        let got = parse_omni_viewport(&payload).unwrap();
        assert_eq!(got.viewport_id, 1);
        assert_eq!(got.viewports.len(), 1);
        let v = got.viewports[0];
        assert_eq!(v.azimuth_centre, 180 * (1 << 16) - 1);
        assert_eq!(v.elevation_centre, 90 * (1 << 16));
        assert_eq!(v.tilt_centre, -180 * (1 << 16));
        assert_eq!(v.hor_range, 360 * (1 << 16));
        assert_eq!(v.ver_range, 180 * (1 << 16));
    }

    #[test]
    fn omni_viewport_two_entries() {
        // viewport_id=2, cancel=0, persistence=1, cnt_minus1=1 (so 2 entries).
        // 10 + 1 + 1 + 4 = 16 bits: u(10)=2, u(1)=0, u(1)=1, u(4)=1
        //   = 0b00000000_10_0_1_0001 = 0x00, 0x91.
        let mut payload = vec![0x00, 0x91];
        // entry 0
        payload.extend_from_slice(&i32_be(100));
        payload.extend_from_slice(&i32_be(200));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&u32_be(1024));
        payload.extend_from_slice(&u32_be(512));
        // entry 1
        payload.extend_from_slice(&i32_be(-300));
        payload.extend_from_slice(&i32_be(-400));
        payload.extend_from_slice(&i32_be(50));
        payload.extend_from_slice(&u32_be(2048));
        payload.extend_from_slice(&u32_be(1024));
        let got = parse_omni_viewport(&payload).unwrap();
        assert_eq!(got.viewport_id, 2);
        assert_eq!(got.viewports.len(), 2);
        assert_eq!(got.viewports[0].azimuth_centre, 100);
        assert_eq!(got.viewports[1].azimuth_centre, -300);
        assert_eq!(got.viewports[1].tilt_centre, 50);
    }

    #[test]
    fn omni_viewport_hor_range_zero_rejected() {
        // header for one entry, then hor_range=0 (forbidden by §D.2.35.5).
        let mut payload = vec![0x00, 0x10];
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&u32_be(0));
        payload.extend_from_slice(&u32_be(1));
        let err = parse_omni_viewport(&payload).unwrap_err();
        assert_eq!(err, SeiError::OmniViewportHorRangeOutOfRange(0));
    }

    #[test]
    fn omni_viewport_ver_range_overflow_rejected() {
        let mut payload = vec![0x00, 0x10];
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&u32_be(1));
        payload.extend_from_slice(&u32_be(180 * (1 << 16) + 1));
        let err = parse_omni_viewport(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::OmniViewportVerRangeOutOfRange(180 * (1 << 16) + 1)
        );
    }

    #[test]
    fn omni_viewport_azimuth_overflow_rejected() {
        let mut payload = vec![0x00, 0x10];
        payload.extend_from_slice(&i32_be(180 * (1 << 16))); // one past high
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&u32_be(1));
        payload.extend_from_slice(&u32_be(1));
        let err = parse_omni_viewport(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::OmniViewportAzimuthOutOfRange(180 * (1 << 16))
        );
    }

    #[test]
    fn omni_viewport_elevation_underflow_rejected() {
        let mut payload = vec![0x00, 0x10];
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&i32_be(-90 * (1 << 16) - 1));
        payload.extend_from_slice(&i32_be(0));
        payload.extend_from_slice(&u32_be(1));
        payload.extend_from_slice(&u32_be(1));
        let err = parse_omni_viewport(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::OmniViewportElevationOutOfRange(-90 * (1 << 16) - 1)
        );
    }

    #[test]
    fn parse_payload_dispatches_omni_viewport() {
        // viewport_id=0, cancel=1.
        let payload = pack_bits(&[(0, 10), (1, 1)]);
        let ctx = SeiContext::default();
        match parse_payload(156, &payload, &ctx).unwrap() {
            SeiPayload::OmniViewport(v) => {
                assert!(v.cancel_flag);
                assert_eq!(v.viewport_id, 0);
            }
            other => panic!("expected OmniViewport, got {other:?}"),
        }
    }

    #[test]
    fn regionwise_packing_cancel_skips_body() {
        // rwp_cancel_flag = 1 → body absent (§D.1.35.4).
        let payload = pack_bits(&[(1, 1)]);
        let got = parse_regionwise_packing(&payload).unwrap();
        assert!(got.cancel_flag);
        assert!(got.persistence_flag.is_none());
        assert!(got.constituent_picture_matching_flag.is_none());
        assert!(got.proj_picture_width.is_none());
        assert!(got.regions.is_empty());
    }

    #[test]
    fn regionwise_packing_single_region_no_guard_band() {
        // One packed region, no guard band.
        //   rwp_cancel_flag                   = 0   (1)
        //   rwp_persistence_flag              = 1   (1)
        //   constituent_picture_matching_flag = 0   (1)
        //   rwp_reserved_zero_5bits           = 0   (5)
        //   num_packed_regions               = 1   u(8)
        //   proj_picture_width               = 3840 u(32)
        //   proj_picture_height              = 1920 u(32)
        //   packed_picture_width             = 2560 u(16)
        //   packed_picture_height            = 1280 u(16)
        //   region 0:
        //     rwp_reserved_zero_4bits        = 0   (4)
        //     transform_type                 = 5   (3)   (90° anticlockwise)
        //     guard_band_flag                = 0   (1)
        //     proj_region_width              = 1920 u(32)
        //     proj_region_height             = 1920 u(32)
        //     proj_region_top                = 0   u(32)
        //     proj_region_left               = 0   u(32)
        //     packed_region_width            = 1280 u(16)
        //     packed_region_height           = 1280 u(16)
        //     packed_region_top              = 0   u(16)
        //     packed_region_left             = 0   u(16)
        let fields = [
            (0u64, 1),
            (1, 1),
            (0, 1),
            (0, 5),
            (1, 8),
            (3840, 32),
            (1920, 32),
            (2560, 16),
            (1280, 16),
            (0, 4),
            (5, 3),
            (0, 1),
            (1920, 32),
            (1920, 32),
            (0, 32),
            (0, 32),
            (1280, 16),
            (1280, 16),
            (0, 16),
            (0, 16),
        ];
        let payload = pack_bits(&fields);
        let got = parse_regionwise_packing(&payload).unwrap();
        assert!(!got.cancel_flag);
        assert_eq!(got.persistence_flag, Some(true));
        assert_eq!(got.constituent_picture_matching_flag, Some(false));
        assert_eq!(got.proj_picture_width, Some(3840));
        assert_eq!(got.proj_picture_height, Some(1920));
        assert_eq!(got.packed_picture_width, Some(2560));
        assert_eq!(got.packed_picture_height, Some(1280));
        assert_eq!(got.regions.len(), 1);
        let reg = got.regions[0];
        assert_eq!(reg.transform_type, 5);
        assert_eq!(reg.proj_region_width, 1920);
        assert_eq!(reg.proj_region_height, 1920);
        assert_eq!(reg.packed_region_width, 1280);
        assert_eq!(reg.packed_region_height, 1280);
        assert!(reg.guard_band.is_none());
    }

    #[test]
    fn regionwise_packing_region_with_guard_band() {
        // One region carrying a guard band on all four edges.
        let fields = [
            (0u64, 1), // cancel
            (0, 1),    // persistence
            (1, 1),    // constituent_picture_matching_flag
            (0, 5),    // reserved
            (1, 8),    // num_packed_regions
            (1000, 32),
            (500, 32),
            (1000, 16),
            (500, 16),
            // region 0
            (0, 4), // reserved
            (0, 3), // transform_type = 0 (no transform)
            (1, 1), // guard_band_flag = 1
            (100, 32),
            (100, 32),
            (0, 32),
            (0, 32),
            (100, 16),
            (100, 16),
            (0, 16),
            (0, 16),
            // guard band
            (4, 8), // left_gb_width
            (4, 8), // right_gb_width
            (2, 8), // top_gb_height
            (2, 8), // bottom_gb_height
            (1, 1), // gb_not_used_for_pred_flag
            (1, 3), // gb_type[0]
            (2, 3), // gb_type[1]
            (3, 3), // gb_type[2]
            (1, 3), // gb_type[3]
            (0, 3), // rwp_gb_reserved_zero_3bits
        ];
        let payload = pack_bits(&fields);
        let got = parse_regionwise_packing(&payload).unwrap();
        assert_eq!(got.constituent_picture_matching_flag, Some(true));
        assert_eq!(got.regions.len(), 1);
        let gb = got.regions[0].guard_band.expect("guard band present");
        assert_eq!(gb.left_gb_width, 4);
        assert_eq!(gb.right_gb_width, 4);
        assert_eq!(gb.top_gb_height, 2);
        assert_eq!(gb.bottom_gb_height, 2);
        assert!(gb.gb_not_used_for_pred_flag);
        assert_eq!(gb.gb_type, [1, 2, 3, 1]);
    }

    #[test]
    fn regionwise_packing_two_regions_mixed_guard_band() {
        // Two regions: region 0 with guard band, region 1 without.
        let fields = [
            (0u64, 1), // cancel
            (1, 1),    // persistence
            (0, 1),    // constituent_picture_matching_flag
            (0, 5),    // reserved
            (2, 8),    // num_packed_regions = 2
            (2000, 32),
            (1000, 32),
            (2000, 16),
            (1000, 16),
            // region 0 (guard band)
            (0, 4),
            (2, 3), // transform_type
            (1, 1), // guard_band_flag
            (1000, 32),
            (1000, 32),
            (0, 32),
            (0, 32),
            (1000, 16),
            (1000, 16),
            (0, 16),
            (0, 16),
            (8, 8),
            (0, 8),
            (0, 8),
            (0, 8),
            (0, 1), // gb_not_used_for_pred_flag
            (0, 3),
            (1, 3),
            (1, 3),
            (1, 3),
            (0, 3),
            // region 1 (no guard band)
            (0, 4),
            (7, 3), // transform_type = 7
            (0, 1), // guard_band_flag = 0
            (1000, 32),
            (1000, 32),
            (0, 32),
            (1000, 32),
            (1000, 16),
            (1000, 16),
            (0, 16),
            (1000, 16),
        ];
        let payload = pack_bits(&fields);
        let got = parse_regionwise_packing(&payload).unwrap();
        assert_eq!(got.regions.len(), 2);
        assert_eq!(got.regions[0].transform_type, 2);
        let gb = got.regions[0].guard_band.expect("region 0 guard band");
        assert_eq!(gb.left_gb_width, 8);
        assert!(!gb.gb_not_used_for_pred_flag);
        assert_eq!(got.regions[1].transform_type, 7);
        assert_eq!(got.regions[1].proj_region_left, 1000);
        assert_eq!(got.regions[1].packed_region_left, 1000);
        assert!(got.regions[1].guard_band.is_none());
    }

    #[test]
    fn regionwise_packing_zero_regions_rejected() {
        // num_packed_regions = 0 → §D.2.35.4 violation.
        let fields = [(0u64, 1), (0, 1), (0, 1), (0, 5), (0, 8)];
        let payload = pack_bits(&fields);
        assert_eq!(
            parse_regionwise_packing(&payload),
            Err(SeiError::RegionWisePackingNoRegions)
        );
    }

    #[test]
    fn regionwise_packing_zero_proj_picture_rejected() {
        // proj_picture_width = 0 → §D.2.35.4 violation.
        let fields = [
            (0u64, 1),
            (0, 1),
            (0, 1),
            (0, 5),
            (1, 8),
            (0, 32),
            (100, 32),
        ];
        let payload = pack_bits(&fields);
        assert_eq!(
            parse_regionwise_packing(&payload),
            Err(SeiError::RegionWisePackingProjPictureZero { w: 0, h: 100 })
        );
    }

    #[test]
    fn regionwise_packing_zero_packed_picture_rejected() {
        // packed_picture_height = 0 → §D.2.35.4 violation.
        let fields = [
            (0u64, 1),
            (0, 1),
            (0, 1),
            (0, 5),
            (1, 8),
            (100, 32),
            (100, 32),
            (100, 16),
            (0, 16),
        ];
        let payload = pack_bits(&fields);
        assert_eq!(
            parse_regionwise_packing(&payload),
            Err(SeiError::RegionWisePackingPackedPictureZero { w: 100, h: 0 })
        );
    }

    #[test]
    fn regionwise_packing_guard_band_all_zero_rejected() {
        // guard_band_flag = 1 but all four dimensions 0 → §D.2.35.4.
        let fields = [
            (0u64, 1),
            (0, 1),
            (0, 1),
            (0, 5),
            (1, 8),
            (100, 32),
            (100, 32),
            (100, 16),
            (100, 16),
            (0, 4),
            (0, 3),
            (1, 1), // guard_band_flag = 1
            (50, 32),
            (50, 32),
            (0, 32),
            (0, 32),
            (50, 16),
            (50, 16),
            (0, 16),
            (0, 16),
            (0, 8), // all guard-band dims 0
            (0, 8),
            (0, 8),
            (0, 8),
        ];
        let payload = pack_bits(&fields);
        assert_eq!(
            parse_regionwise_packing(&payload),
            Err(SeiError::RegionWisePackingGuardBandAllZero(0))
        );
    }

    #[test]
    fn parse_payload_dispatches_regionwise_packing() {
        // Dispatch wiring for payload type 155: cancelled message.
        let payload = pack_bits(&[(1, 1)]);
        let ctx = SeiContext::default();
        match parse_payload(155, &payload, &ctx).unwrap() {
            SeiPayload::RegionWisePacking(r) => {
                assert!(r.cancel_flag);
                assert!(r.regions.is_empty());
            }
            other => panic!("expected RegionWisePacking, got {other:?}"),
        }
    }

    #[test]
    fn shutter_interval_info_sub_layer_idx_nonzero_skips_body() {
        // sii_sub_layer_idx = 1, no further bytes (per §D.2.38 the
        // body fields are absent when sub_layer_idx != 0).
        // ue(v) for 1 = "010" → 3 bits → padded byte 0b01000000 = 0x40.
        let payload = vec![0x40];
        let got = parse_shutter_interval_info(&payload).unwrap();
        assert_eq!(got.sub_layer_idx, 1);
        assert!(got.body.is_none());
    }

    #[test]
    fn shutter_interval_info_present_false() {
        // sii_sub_layer_idx = 0 → ue codeword "1" (1 bit).
        // info_present_flag = 0 → 1 more bit.
        // Two bits total → 0b10_000000 = 0x80.
        let payload = vec![0x80];
        let got = parse_shutter_interval_info(&payload).unwrap();
        assert_eq!(got.sub_layer_idx, 0);
        let body = got.body.expect("body present when sub_layer_idx==0");
        assert!(!body.info_present_flag);
        assert!(body.schedule.is_none());
    }

    #[test]
    fn shutter_interval_info_fixed_27mhz_1_080_000() {
        // Per §D.2.38 worked example: 27 MHz time-scale, 1_080_000 units
        // → 0.04 s shutter interval.
        // Header bits (MSB-first, packed):
        //   ue(0)             = "1"                    (1 bit)
        //   info_present_flag = 1                       (1 bit)
        //   time_scale        = 27_000_000   u(32)
        //   fixed_flag        = 1                       (1 bit)
        //   num_units         = 1_080_000    u(32)
        // Total bits before u(32) values: 3 → "11 1" → 0b11100000 first byte
        // but with the two u(32) values interleaved bit-by-bit. Build via
        // pack_bits to avoid hand-aligning.
        let payload = pack_bits(&[
            (1, 1),           // ue("1") = 0
            (1, 1),           // info_present
            (27_000_000, 32), // time_scale
            (1, 1),           // fixed_flag
            (1_080_000, 32),  // num_units
        ]);
        let got = parse_shutter_interval_info(&payload).unwrap();
        assert_eq!(got.sub_layer_idx, 0);
        let body = got.body.expect("body present");
        assert!(body.info_present_flag);
        let sched = body.schedule.expect("schedule present");
        assert_eq!(sched.time_scale, 27_000_000);
        match sched.kind {
            ShutterIntervalKind::Fixed { num_units } => assert_eq!(num_units, 1_080_000),
            other => panic!("expected Fixed, got {other:?}"),
        }
    }

    #[test]
    fn shutter_interval_info_per_sub_layer() {
        // 3 sub-layers (max_sub_layers_minus1 = 2 → 3 entries).
        let payload = pack_bits(&[
            (1, 1),       // ue("1") = sii_sub_layer_idx = 0
            (1, 1),       // info_present
            (90_000, 32), // time_scale = 90 kHz
            (0, 1),       // fixed_flag = 0
            (2, 3),       // max_sub_layers_minus1 = 2 → 3 entries
            (1_800, 32),  // sub_layer[0]
            (900, 32),    // sub_layer[1]
            (450, 32),    // sub_layer[2]
        ]);
        let got = parse_shutter_interval_info(&payload).unwrap();
        let body = got.body.expect("body present");
        let sched = body.schedule.expect("schedule present");
        assert_eq!(sched.time_scale, 90_000);
        match sched.kind {
            ShutterIntervalKind::PerSubLayer {
                max_sub_layers_minus1,
                num_units_per_sub_layer,
            } => {
                assert_eq!(max_sub_layers_minus1, 2);
                assert_eq!(num_units_per_sub_layer, vec![1_800, 900, 450]);
            }
            other => panic!("expected PerSubLayer, got {other:?}"),
        }
    }

    #[test]
    fn shutter_interval_info_time_scale_zero_rejected() {
        let payload = pack_bits(&[
            (1, 1),  // ue("1") = 0
            (1, 1),  // info_present
            (0, 32), // time_scale = 0 (forbidden)
            (1, 1),  // fixed_flag
            (1, 32), // num_units
        ]);
        let err = parse_shutter_interval_info(&payload).unwrap_err();
        assert_eq!(err, SeiError::ShutterIntervalTimeScaleZero);
    }

    #[test]
    fn parse_payload_dispatches_shutter_interval_info() {
        // info_present_flag = 0 → just two bits 0b10 = 0x80.
        let payload = vec![0x80];
        let ctx = SeiContext::default();
        match parse_payload(205, &payload, &ctx).unwrap() {
            SeiPayload::ShutterIntervalInfo(s) => {
                assert_eq!(s.sub_layer_idx, 0);
                assert!(s.body.is_some());
            }
            other => panic!("expected ShutterIntervalInfo, got {other:?}"),
        }
    }

    // ----- §D.1.36 / §D.2.36 SEI manifest (payload type 200) -----

    #[test]
    fn sei_manifest_empty() {
        // manifest_num_sei_msg_types = 0 → exactly 16 zero bits.
        let payload = vec![0x00, 0x00];
        let got = parse_sei_manifest(&payload).unwrap();
        assert!(got.entries.is_empty());
    }

    #[test]
    fn sei_manifest_single_entry_necessary() {
        // count = 1, payload_type = 45 (frame_packing_arrangement),
        // description = 1 (ExpectedNecessary).
        let payload = vec![
            0x00, 0x01, // count = 1
            0x00, 0x2D, // payload_type = 45
            0x01, // description = 1
        ];
        let got = parse_sei_manifest(&payload).unwrap();
        assert_eq!(got.entries.len(), 1);
        assert_eq!(got.entries[0].payload_type, 45);
        assert_eq!(
            got.entries[0].description,
            SeiManifestDescription::ExpectedNecessary
        );
    }

    #[test]
    fn sei_manifest_three_entries_mixed_descriptions() {
        let payload = vec![
            0x00, 0x03, // count = 3
            0x00, 0x00, // payload_type = 0 (buffering_period)
            0x01, // description = 1 (necessary)
            0x00, 0x90, // payload_type = 144 (content_light_level_info)
            0x02, // description = 2 (unnecessary)
            0x00, 0x97, // payload_type = 151 (cubemap_projection)
            0x03, // description = 3 (undetermined)
        ];
        let got = parse_sei_manifest(&payload).unwrap();
        assert_eq!(got.entries.len(), 3);
        assert_eq!(got.entries[0].payload_type, 0);
        assert_eq!(
            got.entries[0].description,
            SeiManifestDescription::ExpectedNecessary
        );
        assert_eq!(got.entries[1].payload_type, 144);
        assert_eq!(
            got.entries[1].description,
            SeiManifestDescription::ExpectedUnnecessary
        );
        assert_eq!(got.entries[2].payload_type, 151);
        assert_eq!(
            got.entries[2].description,
            SeiManifestDescription::ExpectedUndetermined
        );
    }

    #[test]
    fn sei_manifest_reserved_description_preserved() {
        // §D.2.36: values 4..=255 are reserved. Decoders shall allow
        // them and ignore the associated info. We surface the raw byte
        // via SeiManifestDescription::Reserved so callers can implement
        // the "shall ignore" requirement.
        let payload = vec![
            0x00, 0x01, // count = 1
            0x00, 0x05, // payload_type = 5
            0x42, // description = 0x42 (reserved)
        ];
        let got = parse_sei_manifest(&payload).unwrap();
        assert_eq!(got.entries.len(), 1);
        assert_eq!(
            got.entries[0].description,
            SeiManifestDescription::Reserved(0x42)
        );
    }

    #[test]
    fn sei_manifest_duplicate_payload_type_rejected() {
        // §D.2.36: manifest_sei_payload_type values shall be distinct.
        let payload = vec![
            0x00, 0x02, // count = 2
            0x00, 0x2D, 0x01, // (45, necessary)
            0x00, 0x2D, 0x02, // (45, unnecessary) — duplicate
        ];
        let err = parse_sei_manifest(&payload).unwrap_err();
        assert_eq!(
            err,
            SeiError::SeiManifestDuplicatePayloadType {
                payload_type: 45,
                first: 0,
                second: 1
            }
        );
    }

    #[test]
    fn sei_manifest_truncated_input_rejects() {
        // count promises 1 entry but payload runs out partway through.
        let payload = vec![0x00, 0x01, 0x00, 0x2D]; // missing description byte
        let err = parse_sei_manifest(&payload).unwrap_err();
        assert!(matches!(err, SeiError::Bitstream(_)));
    }

    #[test]
    fn parse_payload_dispatches_sei_manifest() {
        let payload = vec![
            0x00, 0x01, // count = 1
            0x00, 0x00, // payload_type = 0
            0x00, // description = 0 (NotExpected)
        ];
        let ctx = SeiContext::default();
        match parse_payload(200, &payload, &ctx).unwrap() {
            SeiPayload::SeiManifest(m) => {
                assert_eq!(m.entries.len(), 1);
                assert_eq!(m.entries[0].payload_type, 0);
                assert_eq!(
                    m.entries[0].description,
                    SeiManifestDescription::NotExpected
                );
            }
            other => panic!("expected SeiManifest, got {other:?}"),
        }
    }

    // ----- §D.1.37 / §D.2.37 SEI prefix indication (payload type 201) -----

    #[test]
    fn sei_prefix_indication_single_byte_aligned() {
        // prefix_sei_payload_type = 45, num_indications = 1 (minus1=0).
        // Indication 0: num_bits_minus1 = 7 → 8 bits of data = 0xA5.
        let payload = vec![
            0x00, 0x2D, // prefix_sei_payload_type = 45
            0x00, // num_sei_prefix_indications_minus1 = 0
            0x00, 0x07, // num_bits_in_prefix_indication_minus1[0] = 7
            0xA5, // 8 sei_prefix_data_bit values
        ];
        let got = parse_sei_prefix_indication(&payload).unwrap();
        assert_eq!(got.prefix_sei_payload_type, 45);
        assert_eq!(got.indications.len(), 1);
        assert_eq!(got.indications[0].bit_count, 8);
        assert_eq!(got.indications[0].data, vec![0xA5]);
    }

    #[test]
    fn sei_prefix_indication_short_bitstring_with_alignment_one_bits() {
        // 3-bit indication "101" followed by five `1`-fill alignment bits
        // (per §D.1.37 byte_alignment_bit_equal_to_one) → 0xBF.
        let payload = vec![
            0x00, 0x2D, // prefix_sei_payload_type
            0x00, // num_indications_minus1 = 0
            0x00, 0x02, // num_bits_minus1 = 2 → 3 bits
            0xBF, // 101 + five filler `1`s
        ];
        let got = parse_sei_prefix_indication(&payload).unwrap();
        assert_eq!(got.indications.len(), 1);
        assert_eq!(got.indications[0].bit_count, 3);
        // Bits "101" packed MSB-first into one byte → 0b1010_0000 = 0xA0.
        assert_eq!(got.indications[0].data, vec![0xA0]);
    }

    #[test]
    fn sei_prefix_indication_two_entries() {
        // Two indications: 4 bits "1100" + 12 bits "1010_1100_0011".
        // First indication occupies 4 bits + 4 fill = 1 byte = 0b1100_1111 = 0xCF.
        // Second indication occupies 12 bits + 4 fill = 2 bytes.
        //   bits: 1010 1100 0011 + 1111 → 0xAC 0x3F.
        let payload = vec![
            0x00, 0x2D, // prefix_sei_payload_type
            0x01, // num_indications_minus1 = 1
            0x00, 0x03, // bits_minus1 = 3 → 4 bits
            0xCF, // "1100" + "1111" filler
            0x00, 0x0B, // bits_minus1 = 11 → 12 bits
            0xAC, 0x3F, // "1010_1100_0011" + "1111" filler
        ];
        let got = parse_sei_prefix_indication(&payload).unwrap();
        assert_eq!(got.indications.len(), 2);
        assert_eq!(got.indications[0].bit_count, 4);
        // 4 bits "1100" packed MSB-first → 0b1100_0000 = 0xC0.
        assert_eq!(got.indications[0].data, vec![0xC0]);
        assert_eq!(got.indications[1].bit_count, 12);
        // 12 bits "1010_1100_0011" → byte0=0xAC, byte1=0b0011_0000=0x30.
        assert_eq!(got.indications[1].data, vec![0xAC, 0x30]);
    }

    #[test]
    fn sei_prefix_indication_oversized_bit_count_rejected() {
        // num_bits_minus1 + 1 = 9 bits but only 1 byte of data follows.
        let payload = vec![
            0x00, 0x2D, // prefix_sei_payload_type
            0x00, // num_indications_minus1 = 0
            0x00, 0x08, // bits_minus1 = 8 → 9 bits required
            0xFF, // only 8 bits available
        ];
        let err = parse_sei_prefix_indication(&payload).unwrap_err();
        match err {
            SeiError::SeiPrefixIndicationOverflow { i, bits, available } => {
                assert_eq!(i, 0);
                assert_eq!(bits, 9);
                assert_eq!(available, 8);
            }
            other => panic!("expected SeiPrefixIndicationOverflow, got {other:?}"),
        }
    }

    #[test]
    fn sei_prefix_indication_truncated_header_rejected() {
        // Header truncated before num_sei_prefix_indications_minus1.
        let payload = vec![0x00, 0x2D]; // only prefix_sei_payload_type
        let err = parse_sei_prefix_indication(&payload).unwrap_err();
        assert!(matches!(err, SeiError::Bitstream(_)));
    }

    #[test]
    fn parse_payload_dispatches_sei_prefix_indication() {
        let payload = vec![
            0x00, 0x2D, // prefix_sei_payload_type = 45
            0x00, // num_indications_minus1 = 0
            0x00, 0x07, // bits_minus1 = 7 → 8 bits
            0xA5,
        ];
        let ctx = SeiContext::default();
        match parse_payload(201, &payload, &ctx).unwrap() {
            SeiPayload::SeiPrefixIndication(p) => {
                assert_eq!(p.prefix_sei_payload_type, 45);
                assert_eq!(p.indications.len(), 1);
                assert_eq!(p.indications[0].bit_count, 8);
                assert_eq!(p.indications[0].data, vec![0xA5]);
            }
            other => panic!("expected SeiPrefixIndication, got {other:?}"),
        }
    }

    // =========================================================================
    // ATSC1 envelope (A/53 Part 4 §6.2.3) — typed view on top of
    // user_data_registered_itu_t_t35 (round 158).
    // =========================================================================

    /// Build a full user_data_registered_itu_t_t35 payload that wraps an
    /// ATSC1 envelope (country=0xB5 USA + provider 0x0031 + user_identifier
    /// + body). Used to exercise the chained `parse_user_data_registered…`
    ///   → `parse_atsc1_envelope` flow.
    fn build_t35_atsc1(user_identifier: u32, body: &[u8]) -> Vec<u8> {
        let mut v = Vec::with_capacity(7 + body.len());
        v.push(0xB5);
        v.extend_from_slice(&[0x00, 0x31]); // provider_code = 0x0031
        v.extend_from_slice(&user_identifier.to_be_bytes());
        v.extend_from_slice(body);
        v
    }

    #[test]
    fn atsc1_envelope_ga94_cc_data_round_trip() {
        // 'GA94' + user_data_type_code 0x03 (MPEG_cc_data) + 5 opaque cc bytes
        // ending in the 0xFF marker the cc_data() trailer requires.
        let cc = [0xFC, 0x94, 0x2C, 0xFE, 0xFF];
        let mut body = vec![0x03u8];
        body.extend_from_slice(&cc);
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);

        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        assert_eq!(outer.country_code, 0xB5);
        assert!(outer.country_code_extension.is_none());

        let env = parse_atsc1_envelope(&outer.payload_bytes).unwrap();
        assert_eq!(env.provider_code, 0x0031);
        assert_eq!(env.user_identifier, Atsc1UserIdentifier::Ga94);
        match env.body {
            Atsc1Body::AtscUserData(Atsc1UserData::CcData { cc_data_bytes }) => {
                // CEA-708 inner layout intentionally opaque — only the byte
                // count is asserted (we hand the bytes through unchanged for
                // a downstream CEA-708 parser).
                assert_eq!(cc_data_bytes, cc);
            }
            other => panic!("expected GA94 → CcData, got {other:?}"),
        }
    }

    #[test]
    fn atsc1_envelope_ga94_bar_data_letterbox() {
        // top_bar_flag=1, bottom_bar_flag=1, left=0, right=0, reserved=1111,
        // (one_bits=11, 14-bit line_number_end_of_top_bar = 0x0040 = 64),
        // (one_bits=11, 14-bit line_number_start_of_bottom_bar = 0x03BF
        //  = 959).
        //
        // First byte:  1 1 0 0  | 1 1 1 1 = 0b1100_1111 = 0xCF
        // Then: '11' u(2) + u(14) = 16 bits = 2 bytes per value.
        //   64  = 0x0040 → with 11 prefix → 0xC040
        //   959 = 0x03BF → with 11 prefix → 0xC3BF
        let body = [0x06, 0xCF, 0xC0, 0x40, 0xC3, 0xBF];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let env = parse_atsc1_envelope(&outer.payload_bytes).unwrap();
        match env.body {
            Atsc1Body::AtscUserData(Atsc1UserData::BarData(bar)) => {
                assert_eq!(bar.letterbox, Some((64, 959)));
                assert_eq!(bar.pillarbox, None);
            }
            other => panic!("expected BarData letterbox, got {other:?}"),
        }
    }

    #[test]
    fn atsc1_envelope_ga94_bar_data_pillarbox() {
        // top=0, bottom=0, left=1, right=1, reserved=1111, then two 16-bit
        // (11 prefix + 14-bit) values: pixel_number_end_of_left_bar = 100,
        // pixel_number_start_of_right_bar = 1820.
        //   100  = 0x0064 → 0xC064
        //   1820 = 0x071C → 0xC71C
        // First byte: 0 0 1 1 | 1 1 1 1 = 0x3F
        let body = [0x06, 0x3F, 0xC0, 0x64, 0xC7, 0x1C];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let env = parse_atsc1_envelope(&outer.payload_bytes).unwrap();
        match env.body {
            Atsc1Body::AtscUserData(Atsc1UserData::BarData(bar)) => {
                assert_eq!(bar.letterbox, None);
                assert_eq!(bar.pillarbox, Some((100, 1820)));
            }
            other => panic!("expected BarData pillarbox, got {other:?}"),
        }
    }

    #[test]
    fn atsc1_envelope_bar_data_rejects_letterbox_and_pillarbox_simultaneously() {
        // top=1, bottom=1, left=1, right=1 — explicitly forbidden by
        // §6.2.3.2 ("either top and bottom bars or left and right bars,
        // but not both pairs at once").
        let body = [0x06, 0xFF];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let err = parse_atsc1_envelope(&outer.payload_bytes).unwrap_err();
        assert_eq!(err, SeiError::Atsc1BarDataLetterboxPillarboxBoth);
    }

    #[test]
    fn atsc1_envelope_bar_data_rejects_top_bottom_mismatch() {
        // top=1, bottom=0 — §6.2.3.2 requires equality.
        // First byte: 1 0 0 0 | 1 1 1 1 = 0x8F. Then a single (11+14) value.
        let body = [0x06, 0x8F, 0xC0, 0x10];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let err = parse_atsc1_envelope(&outer.payload_bytes).unwrap_err();
        assert_eq!(
            err,
            SeiError::Atsc1BarDataTopBottomMismatch {
                top: true,
                bottom: false
            }
        );
    }

    #[test]
    fn atsc1_envelope_bar_data_rejects_left_right_mismatch() {
        // top=0, bottom=0, left=1, right=0 — §6.2.3.2 requires equality.
        // First byte: 0 0 1 0 | 1 1 1 1 = 0x2F.
        let body = [0x06, 0x2F, 0xC0, 0x10];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let err = parse_atsc1_envelope(&outer.payload_bytes).unwrap_err();
        assert_eq!(
            err,
            SeiError::Atsc1BarDataLeftRightMismatch {
                left: true,
                right: false,
            }
        );
    }

    #[test]
    fn atsc1_envelope_bar_data_rejects_wrong_reserved_nibble() {
        // top=0, bottom=0, left=0, right=0, reserved=0000 (must be 1111).
        let body = [0x06, 0x00];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let err = parse_atsc1_envelope(&outer.payload_bytes).unwrap_err();
        assert_eq!(err, SeiError::Atsc1BarDataReservedMismatch(0b0000));
    }

    #[test]
    fn atsc1_envelope_bar_data_rejects_wrong_one_bits_prefix() {
        // Letterbox flags set, reserved correct, but the per-value '11'
        // prefix bits before line_number_end_of_top_bar are 0b01.
        // First byte: 1 1 0 0 | 1 1 1 1 = 0xCF.
        // Then: prefix 01 + 14-bit 0 → 0x4000.
        let body = [0x06, 0xCF, 0x40, 0x00, 0xC0, 0x00];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let err = parse_atsc1_envelope(&outer.payload_bytes).unwrap_err();
        assert_eq!(
            err,
            SeiError::Atsc1BarDataOneBitsMismatch {
                which: "top",
                got: 0b01,
            }
        );
    }

    #[test]
    fn atsc1_envelope_ga94_reserved_type_code_keeps_raw_bytes() {
        // Table 6.9: 0x01 is ATSC reserved. §6.2.2 requires receivers
        // to silently discard; we surface the raw bytes so a diagnostic
        // tool can still inspect them.
        let body = [0x01, 0xDE, 0xAD, 0xBE, 0xEF];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let env = parse_atsc1_envelope(&outer.payload_bytes).unwrap();
        match env.body {
            Atsc1Body::AtscUserData(Atsc1UserData::Reserved { type_code, raw }) => {
                assert_eq!(type_code, 0x01);
                assert_eq!(raw, vec![0xDE, 0xAD, 0xBE, 0xEF]);
            }
            other => panic!("expected reserved type_code, got {other:?}"),
        }
    }

    #[test]
    fn atsc1_envelope_ga94_empty_atsc_user_data_rejected() {
        // 'GA94' with no following user_data_type_code byte — A/53 Part 4
        // Table 6.8 requires the 8-bit type code field.
        let body: [u8; 0] = [];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let err = parse_atsc1_envelope(&outer.payload_bytes).unwrap_err();
        assert_eq!(err, SeiError::Atsc1AtscUserDataEmpty);
    }

    #[test]
    fn atsc1_envelope_dtg1_afd_flag_off() {
        // 'DTG1' + 1-byte afd_data() with active_format_flag = 0.
        // First (and only) byte = 0 0 000001 = 0x01 (zero=0,
        // active_format_flag=0, reserved=000001).
        let body = [0x01u8];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::DTG1, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let env = parse_atsc1_envelope(&outer.payload_bytes).unwrap();
        assert_eq!(env.user_identifier, Atsc1UserIdentifier::Dtg1);
        match env.body {
            Atsc1Body::Afd(afd) => {
                assert!(!afd.active_format_flag);
                assert_eq!(afd.active_format, None);
            }
            other => panic!("expected DTG1 → AFD, got {other:?}"),
        }
    }

    #[test]
    fn atsc1_envelope_dtg1_afd_flag_on_carries_active_format() {
        // 'DTG1' + 2-byte afd_data(): byte0 = 0 1 000001 = 0x41,
        // byte1 = '1111' u(4) + active_format u(4) = 0xFA (active_format =
        // 0b1010 = 10 → "16:9 letterbox image" per Table 6.14).
        let body = [0x41u8, 0xFA];
        let raw = build_t35_atsc1(Atsc1UserIdentifier::DTG1, &body);
        let outer = parse_user_data_registered_itu_t_t35(&raw).unwrap();
        let env = parse_atsc1_envelope(&outer.payload_bytes).unwrap();
        match env.body {
            Atsc1Body::Afd(afd) => {
                assert!(afd.active_format_flag);
                assert_eq!(afd.active_format, Some(0b1010));
            }
            other => panic!("expected DTG1 → AFD with active_format, got {other:?}"),
        }
    }

    #[test]
    fn atsc1_envelope_rejects_short_payload() {
        // Six bytes are the minimum (provider 2 + user_id 4).
        let err = parse_atsc1_envelope(&[0x00, 0x31, 0x47, 0x41, 0x39]).unwrap_err();
        assert_eq!(err, SeiError::Atsc1EnvelopeTooShort(5));
    }

    #[test]
    fn atsc1_envelope_rejects_wrong_provider_code() {
        // 0x003C is the HDR10+ provider slot (also under USA 0xB5) — *not*
        // the ATSC slot. The typed ATSC1 parser refuses to decode it as
        // ATSC1; callers can fall back to the raw `payload_bytes` view.
        let mut raw = vec![0x00, 0x3C];
        raw.extend_from_slice(&Atsc1UserIdentifier::GA94.to_be_bytes());
        let err = parse_atsc1_envelope(&raw).unwrap_err();
        assert_eq!(err, SeiError::Atsc1ProviderCodeMismatch(0x003C));
    }

    #[test]
    fn atsc1_envelope_rejects_unknown_user_identifier() {
        // provider_code OK, but user_identifier 'ZZZZ' is unregistered.
        let mut raw = vec![0x00, 0x31];
        raw.extend_from_slice(b"ZZZZ");
        let err = parse_atsc1_envelope(&raw).unwrap_err();
        assert_eq!(
            err,
            SeiError::Atsc1UnknownUserIdentifier(u32::from_be_bytes(*b"ZZZZ"))
        );
    }

    #[test]
    fn atsc1_envelope_chain_through_parse_payload_dispatch() {
        // End-to-end: parse_payload(4, …) → UserDataRegisteredItuTT35 →
        // parse_atsc1_envelope. Mirrors what a slice-layer SEI consumer
        // would do.
        let cc = [0xFC, 0xC4, 0x91];
        let mut body = vec![0x03u8];
        body.extend_from_slice(&cc);
        let raw = build_t35_atsc1(Atsc1UserIdentifier::GA94, &body);
        let ctx = SeiContext::default();
        let outer = match parse_payload(4, &raw, &ctx).unwrap() {
            SeiPayload::UserDataRegisteredItuTT35(o) => o,
            other => panic!("expected UserDataRegisteredItuTT35, got {other:?}"),
        };
        let env = parse_atsc1_envelope(&outer.payload_bytes).unwrap();
        assert!(matches!(
            env.body,
            Atsc1Body::AtscUserData(Atsc1UserData::CcData { .. })
        ));
    }
}