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
/*
 * Copyright (c) Radzivon Bartoshyk 6/2026. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1.  Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3.  Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

use std::sync::Arc;

use crate::env::get_poc_diff;
use crate::error::TealdustError;
use crate::getbits::GetBits;
use crate::headers::*;
use crate::internal::{DecoderContext, RefState, TileGroup};
use crate::intops::{iclip, imax, imin, ulog2};
use crate::warpmv::resolve_divisor_32;

type Result<T> = std::result::Result<T, TealdustError>;

/// Resolve a reference-bank slot by index, rejecting out-of-range indices.
///
/// `hdr.refidx` entries are read from the bitstream, and on the
/// RAS/open-loop-keyframe path they hold long-term frame ids that can exceed
/// the 8-slot reference bank. The C reference indexes `c->refs` with these
/// directly and relies on a later null check; a memory-safe port must bounds
/// check first. A negative `i8` widens to a huge `usize`, so `get` rejects it.
#[inline]
fn ref_slot(refs: &[RefState; 8], idx: i32) -> Result<&RefState> {
    usize::try_from(idx)
        .ok()
        .and_then(|i| refs.get(i))
        .ok_or(TealdustError::InvalidReferenceFrame)
}

static LAYOUTS: [PixelLayout; 4] = [
    PixelLayout::I420,
    PixelLayout::I400,
    PixelLayout::I444,
    PixelLayout::I422,
];

fn check_trailing_bits(gb: &mut GetBits, strict: bool) -> Result<()> {
    let trailing_one = gb.get_bit();

    if gb.has_error() {
        return Err(TealdustError::InvalidTrailingBits);
    }

    if !strict {
        return Ok(());
    }

    if trailing_one == 0 {
        return Err(TealdustError::InvalidTrailingBits);
    }

    Ok(())
}

#[inline]
fn tile_log2(sz: i32, tgt: i32) -> i32 {
    let mut k = 0;
    while (sz << k) < tgt {
        k += 1;
    }
    k
}

fn parse_seg_info(seg: &mut SegmentationDataSet, gb: &mut GetBits, n_seg: usize) {
    let mut m: u16 = 1;
    for n in 0..n_seg {
        if gb.get_bit() != 0 {
            seg.delta_q_mask |= m;
            seg.delta_q[n] = gb.get_sbits(10).clamp(-351, 351) as i16;
        }
        seg.skip_mask |= m * gb.get_bit() as u16;
        seg.globalmv_mask |= m * gb.get_bit() as u16;
        m <<= 1;
    }
}

fn parse_tile_info(
    thdr: &mut TileInfo,
    gb: &mut GetBits,
    sbmul: i32,
    sb128: u8,
    seq_sb128: u8,
    w: i32,
    h: i32,
    level: u8,
    tier: u8,
) -> Result<()> {
    thdr.uniform = gb.get_bit() != 0;

    let sbsz_log2 = 6 + sb128 as i32;
    let sbsz_min1 = (64 << sb128) - 1;
    let sbw = (w + sbsz_min1) >> sbsz_log2;
    let sbh = (h + sbsz_min1) >> sbsz_log2;
    let w_adj = (level >= 18) as i32 + ((level >= 14 && tier != 0) as i32);
    let max_tile_width_sb = 4096 >> (sbsz_log2 - w_adj);
    let sz_adj = (level >= 14) as i32 + (level >= 18) as i32 + ((level >= 14 && tier != 0) as i32);
    let max_tile_area_sb = (4096 * 2304) >> (2 * sbsz_log2 - sz_adj);
    thdr.min_log2_cols = tile_log2(max_tile_width_sb, sbw) as u8;
    thdr.max_log2_cols = tile_log2(1, imin(sbw, MAX_TILE_COLS as i32)) as u8;
    thdr.max_log2_rows = tile_log2(1, imin(sbh, MAX_TILE_ROWS as i32)) as u8;
    let min_log2_tiles = imax(
        tile_log2(max_tile_area_sb, sbw * sbh),
        thdr.min_log2_cols as i32,
    );

    if thdr.uniform {
        let seq_sbsz_log2 = 6 + seq_sb128 as i32;
        // width/height in full SBs (rounded down, +7 for luma 8px units)
        let fsbw = imax(1, (w + 7) >> seq_sbsz_log2);
        let fsbh = imax(1, (h + 7) >> seq_sbsz_log2);
        // width/height in extended SBs (rounded up)
        let ssbw = 1 + ((w - 1) >> seq_sbsz_log2);
        let ssbh = 1 + ((h - 1) >> seq_sbsz_log2);

        thdr.log2_cols = thdr.min_log2_cols;
        while thdr.log2_cols < thdr.max_log2_cols && gb.get_bit() != 0 {
            thdr.log2_cols += 1;
        }
        let tile_w = fsbw >> thdr.log2_cols;
        let mut extra = imax(0, fsbw - (tile_w << thdr.log2_cols));
        if tile_w == 0 {
            extra += ssbw - fsbw;
        }
        thdr.cols = 0;
        let max_cols = 1i32 << thdr.log2_cols;
        let mut sbx = 0;
        // log2_cols is capped at max_log2_cols so this is already bounded for
        // valid streams; the explicit MAX_TILE_COLS guard protects against any
        // residual overflow of col_start_sb on malformed input.
        while sbx < ssbw
            && (thdr.cols as i32) < max_cols
            && (thdr.cols as usize) < crate::headers::MAX_TILE_COLS
        {
            thdr.col_start_sb[thdr.cols as usize] = (sbx * sbmul) as u16;
            let add = tile_w + if extra > 0 { 1 } else { 0 };
            sbx += add;
            thdr.cols += 1;
            extra -= 1;
        }
        if sbx < fsbw {
            return Err(TealdustError::InvalidTileInfo);
        }

        thdr.min_log2_rows = imax(min_log2_tiles - thdr.log2_cols as i32, 0) as u8;
        thdr.log2_rows = thdr.min_log2_rows;
        while thdr.log2_rows < thdr.max_log2_rows && gb.get_bit() != 0 {
            thdr.log2_rows += 1;
        }
        let tile_h = fsbh >> thdr.log2_rows;
        let mut extra = imax(0, fsbh - (tile_h << thdr.log2_rows));
        if tile_h == 0 {
            extra += ssbh - fsbh;
        }
        thdr.rows = 0;
        let max_rows = 1i32 << thdr.log2_rows;
        let mut sby = 0;
        while sby < ssbh
            && (thdr.rows as i32) < max_rows
            && (thdr.rows as usize) < crate::headers::MAX_TILE_ROWS
        {
            thdr.row_start_sb[thdr.rows as usize] = (sby * sbmul) as u16;
            let add = tile_h + if extra > 0 { 1 } else { 0 };
            sby += add;
            thdr.rows += 1;
            extra -= 1;
        }
        if sby < fsbh {
            return Err(TealdustError::InvalidTileInfo);
        }
    } else {
        let mut widest_tile = 0;
        thdr.cols = 0;
        let mut sbx = 0;
        // malformed frame size lets the loop run past the col_start_sb array.
        while sbx < sbw && (thdr.cols as usize) < crate::headers::MAX_TILE_COLS {
            thdr.col_start_sb[thdr.cols as usize] = sbx as u16;
            let max_width = imin(sbw - sbx, max_tile_width_sb);
            let w_tile = if max_width > 1 {
                gb.get_uniform(max_width as u32) as i32 + 1
            } else {
                1
            };
            widest_tile = imax(widest_tile, w_tile);
            sbx += w_tile;
            thdr.cols += 1;
        }
        if sbx < sbw {
            return Err(TealdustError::InvalidTileInfo);
        }
        thdr.log2_cols = tile_log2(1, thdr.cols as i32) as u8;

        let max_tile_area_sb_here = if min_log2_tiles > 0 {
            (sbw * sbh) >> (min_log2_tiles + 1)
        } else {
            sbw * sbh
        };
        let max_tile_height_sb = imax(max_tile_area_sb_here / widest_tile, 1);

        thdr.rows = 0;
        let mut sby = 0;
        while sby < sbh && (thdr.rows as usize) < crate::headers::MAX_TILE_ROWS {
            thdr.row_start_sb[thdr.rows as usize] = sby as u16;
            let max_height = imin(sbh - sby, max_tile_height_sb);
            let h_tile = if max_height > 1 {
                gb.get_uniform(max_height as u32) as i32 + 1
            } else {
                1
            };
            sby += h_tile;
            thdr.rows += 1;
        }
        if sby < sbh {
            return Err(TealdustError::InvalidTileInfo);
        }
        thdr.log2_rows = tile_log2(1, thdr.rows as i32) as u8;
    }
    if thdr.cols == 0 || thdr.rows == 0 {
        return Err(TealdustError::InvalidTileInfo);
    }
    thdr.col_start_sb[thdr.cols as usize] = sbw as u16;
    thdr.row_start_sb[thdr.rows as usize] = sbh as u16;
    Ok(())
}
pub fn parse_tile_info_frmhdr(
    hdr: &mut FrameHeader,
    seqhdr: &SequenceHeader,
    gb: &mut GetBits,
) -> Result<()> {
    hdr.sb128 = if hdr.is_inter_or_switch() {
        seqhdr.sb128
    } else {
        if seqhdr.sb128 != 0 { 1 } else { 0 }
    };

    let mut reuse_allowed = false;
    if seqhdr.tiling.present != AdaptiveBoolean::Off {
        let sbsz_min1 = (64i32 << hdr.sb128) - 1;
        let sbsz_log2 = 6 + hdr.sb128 as i32;
        let sbw = (hdr.width + sbsz_min1) >> sbsz_log2;
        let sbh = (hdr.height + sbsz_min1) >> sbsz_log2;
        if !seqhdr.tiling.t.uniform {
            let seq_sbsz_min1 = (64i32 << seqhdr.sb128) - 1;
            let seq_sbsz_log2 = 6 + seqhdr.sb128 as i32;
            let seq_sbw = (seqhdr.max_width + seq_sbsz_min1) >> seq_sbsz_log2;
            let seq_sbh = (seqhdr.max_height + seq_sbsz_min1) >> seq_sbsz_log2;
            reuse_allowed = seq_sbw == sbw && seq_sbh == sbh;
        } else {
            let log2_cols = seqhdr.tiling.t.log2_cols as i32;
            let log2_rows = seqhdr.tiling.t.log2_rows as i32;
            let (cols, rows) = (1i32 << log2_cols, 1i32 << log2_rows);
            let tile_w = (sbw + cols - 1) >> log2_cols;
            let tile_h = (sbh + rows - 1) >> log2_rows;
            reuse_allowed = tile_w * (cols - 1) < sbw && tile_h * (rows - 1) < sbh;
        }
    }

    let sbmul;
    if reuse_allowed
        && (seqhdr.tiling.present == AdaptiveBoolean::On
            || (seqhdr.tiling.present == AdaptiveBoolean::Adaptive && gb.get_bit() != 0))
    {
        hdr.tiling.t = seqhdr.tiling.t.clone();
        if hdr.sb128 != seqhdr.sb128 {
            debug_assert!(hdr.sb128 == 1 && seqhdr.sb128 == 2 && hdr.is_key_or_intra());
            sbmul = 2;
            for n in 0..hdr.tiling.t.rows as usize {
                hdr.tiling.t.row_start_sb[n] *= 2;
            }
            for n in 0..hdr.tiling.t.cols as usize {
                hdr.tiling.t.col_start_sb[n] *= 2;
            }
        } else {
            sbmul = 1;
        }
    } else {
        sbmul = if seqhdr.sb128 == 2 && hdr.is_key_or_intra() {
            2
        } else {
            1
        };
        parse_tile_info(
            &mut hdr.tiling.t,
            gb,
            sbmul,
            hdr.sb128,
            seqhdr.sb128,
            hdr.width,
            hdr.height,
            seqhdr.level,
            seqhdr.tier,
        )?;
    }

    if sbmul == 2 {
        hdr.tiling.t.row_start_sb[hdr.tiling.t.rows as usize] = ((hdr.height + 127) >> 7) as u16;
        hdr.tiling.t.col_start_sb[hdr.tiling.t.cols as usize] = ((hdr.width + 127) >> 7) as u16;
    }
    Ok(())
}

pub fn parse_film_grain_data(gb: &mut GetBits, layout: PixelLayout) -> Result<FilmGrainData> {
    let mut fgd = FilmGrainData::default();

    let mut num_pl = 1;
    if layout != PixelLayout::I400 {
        fgd.chroma_scaling_from_luma = gb.get_bit() != 0;
        if !fgd.chroma_scaling_from_luma {
            num_pl = 3;
        }
    }

    for pl in 0..num_pl {
        fgd.num_points[pl] = gb.get_bits(4) as i32;
        if fgd.num_points[pl] > 14 {
            return Err(TealdustError::InvalidFilmGrainData);
        }
        if fgd.num_points[pl] == 0 {
            continue;
        }
        let index_bits = 1 + gb.get_bits(3) as i32;
        let scaling_bits = 5 + gb.get_bits(2) as i32;
        let mut base = 0u32;
        for i in 0..fgd.num_points[pl] as usize {
            base += gb.get_bits(index_bits);
            if base > 255 {
                return Err(TealdustError::InvalidFilmGrainData);
            }
            fgd.points[pl][i][0] = base as u8;
            fgd.points[pl][i][1] = gb.get_bits(scaling_bits) as u8;
        }
    }

    if layout == PixelLayout::I420 && (fgd.num_points[1] != 0) != (fgd.num_points[2] != 0) {
        return Err(TealdustError::InvalidFilmGrainData);
    }

    fgd.scaling_shift = gb.get_bits(2) as i32 + 8;
    fgd.ar_coeff_lag = gb.get_bits(2) as i32;
    let num_pos = 2 * fgd.ar_coeff_lag * (fgd.ar_coeff_lag + 1);
    for pl in 0..3 {
        if fgd.num_points[pl] == 0 && (pl == 0 || !fgd.chroma_scaling_from_luma) {
            continue;
        }
        let num_pl_pos = num_pos + (pl != 0 && fgd.num_points[0] != 0) as i32;
        let coef_bits = 5 + gb.get_bits(2) as i32;
        let half = 1 << (coef_bits - 1);
        for i in 0..num_pl_pos as usize {
            fgd.ar_coeffs[pl][i] = (gb.get_bits(coef_bits) as i32 - half) as i8;
        }
    }
    fgd.ar_coeff_shift = gb.get_bits(2) as u64 + 6;
    fgd.grain_scale_shift = gb.get_bits(2) as i32;
    for pl in 0..2 {
        if fgd.num_points[1 + pl] == 0 {
            continue;
        }
        fgd.uv_mult[pl] = gb.get_bits(8) as i32 - 128;
        fgd.uv_luma_mult[pl] = gb.get_bits(8) as i32 - 128;
        fgd.uv_offset[pl] = gb.get_bits(9) as i32 - 256;
    }
    fgd.overlap_flag = gb.get_bit() != 0;
    fgd.clip_to_restricted_range = gb.get_bit() != 0;
    if fgd.clip_to_restricted_range {
        fgd.mc_identity = gb.get_bit() != 0;
    }
    fgd.block_size = gb.get_bit() as i32;

    if gb.has_error() {
        return Err(TealdustError::InvalidFilmGrainData);
    }

    Ok(fgd)
}

pub fn rescale_matrix(dm: &mut [i32; 6], sm: &[i32; 6], in_dist: i32, out_dist: i32) {
    let mut shift = 0i32;
    let mut inv = resolve_divisor_32(in_dist.unsigned_abs(), &mut shift);
    if inv >= 512 {
        inv >>= 1;
        shift -= 1;
    }
    if in_dist < 0 {
        inv = -inv;
    }
    let rnd = (1 << shift) >> 1;
    for n in 0..2 {
        let r = iclip(sm[n], -0x400000, 0x400000) * inv;
        let t = ((r + rnd - (r < 0) as i32) >> shift) * out_dist;
        let d = (t + 0x1000 - (t < 0) as i32) & !0x1fff;
        dm[n] = iclip(d, -0x7ffe000, 0x7ffe000);
    }
    for n in 2..6 {
        let b = 0x10000 * (((n as u32).wrapping_sub(3)) > 1) as i32;
        let r = (sm[n] - b) * inv;
        let t = ((r + rnd - (r < 0) as i32) >> shift) * out_dist;
        let d = (t + 32 - (t < 0) as i32) & !63;
        dm[n] = b + iclip(d, -0x7fc0, 0x7fc0);
    }
}

pub fn parse_seq_hdr(gb: &mut GetBits, strict: bool) -> Result<SequenceHeader> {
    let mut hdr = SequenceHeader::default();

    hdr.id = gb.get_vlc() as u8;
    hdr.profile = gb.get_bits(5) as u8;
    // AV2 defines profiles 0–8; the original check (> 2) matches AV1 which only
    // has profiles 0-2. Relax to 8 so real AV2 encoders (e.g. profile 4) pass.
    if hdr.profile > 8 && hdr.profile != 31 {
        return Err(TealdustError::InvalidSequenceHeader);
    }
    hdr.reduced_still_picture_header = gb.get_bit() != 0;
    hdr.level = gb.get_bits(5) as u8;
    if hdr.level >= 4 && !hdr.reduced_still_picture_header {
        hdr.tier = gb.get_bit() as u8;
    }

    let layout_idx = gb.get_vlc();
    if layout_idx > 3 {
        return Err(TealdustError::InvalidSequenceHeader);
    }
    hdr.layout = LAYOUTS[layout_idx as usize];
    match hdr.layout {
        PixelLayout::I420 | PixelLayout::I400 => {
            hdr.ss_hor = 1;
            hdr.ss_ver = 1;
        }
        PixelLayout::I422 => {
            hdr.ss_hor = 1;
            hdr.ss_ver = 0;
        }
        _ => {}
    }

    hdr.hbd = gb.get_vlc() as u8;
    if hdr.hbd > 2 {
        return Err(TealdustError::InvalidSequenceHeader);
    }
    if hdr.hbd < 2 {
        hdr.hbd ^= 1;
    }

    if hdr.reduced_still_picture_header {
        hdr.still_picture = true;
        hdr.monotonic = true;
    } else {
        hdr.lcr_id = gb.get_bits(3) as u8;
        hdr.still_picture = gb.get_bit() != 0;
        hdr.max_tlayer_id = gb.get_bits(2) as u8;
        hdr.max_mlayer_id = gb.get_bits(3) as u8;
        hdr.monotonic = gb.get_bit() != 0;
    }

    hdr.width_n_bits = gb.get_bits(4) as u8 + 1;
    hdr.height_n_bits = gb.get_bits(4) as u8 + 1;
    hdr.max_width = gb.get_bits(hdr.width_n_bits as i32) as i32 + 1;
    hdr.max_height = gb.get_bits(hdr.height_n_bits as i32) as i32 + 1;

    hdr.crop.enabled = gb.get_bit() != 0;
    if hdr.crop.enabled {
        hdr.crop.left = gb.get_vlc();
        hdr.crop.right = gb.get_vlc();
        hdr.crop.top = gb.get_vlc();
        hdr.crop.bottom = gb.get_vlc();
    }

    if !hdr.reduced_still_picture_header {
        if gb.get_bit() != 0 {
            // max_display_model_info_present
            let _max_initial_display_delay = gb.get_bits(4);
        }
        let decoder_model_info_present = gb.get_bit() != 0;
        if decoder_model_info_present {
            let _num_units = gb.get_bits(32);
            let _max_dec_buf = gb.get_vlc();
            let _max_enc_buf = gb.get_vlc();
        }
    }

    if hdr.max_tlayer_id > 0 {
        hdr.tlayer_dependency_present = gb.get_bit() != 0;
        if hdr.tlayer_dependency_present {
            for n in 1..hdr.max_tlayer_id as usize {
                hdr.tlayer_dependencies[n] = gb.get_bits(n as i32) as u8;
            }
        } else {
            let mut mask = !0u32;
            for n in 1..hdr.max_tlayer_id as usize {
                hdr.tlayer_dependencies[n] = (!mask) as u8;
                mask <<= 1;
            }
        }
    }

    if hdr.max_mlayer_id > 0 {
        hdr.mlayer_dependency_present = gb.get_bit() != 0;
        if hdr.mlayer_dependency_present {
            for n in 1..hdr.max_mlayer_id as usize {
                hdr.mlayer_dependencies[n] = gb.get_bits(n as i32) as u8;
            }
        } else {
            let mut mask = !0u32;
            for n in 1..hdr.max_mlayer_id as usize {
                hdr.mlayer_dependencies[n] = (!mask) as u8;
                mask <<= 1;
            }
        }
    }

    hdr.sb128 = if gb.get_bit() != 0 {
        2
    } else {
        gb.get_bit() as u8
    };

    if hdr.layout != PixelLayout::I400 {
        hdr.sdp = gb.get_bit() != 0;
        if hdr.sdp && !hdr.reduced_still_picture_header {
            hdr.ext_sdp = gb.get_bit() != 0;
        }
    }
    hdr.ext_partitions = gb.get_bit() != 0;
    if hdr.ext_partitions {
        hdr.uneven_4way_partitions = gb.get_bit() != 0;
    }
    hdr.max_pb_aspect_ratio_log2 = if gb.get_bit() != 0 {
        1 + gb.get_bit() as u8
    } else {
        3
    };

    hdr.segmentation.ext = gb.get_bit() != 0;
    hdr.segmentation.info_present = gb.get_bit() != 0;
    if hdr.segmentation.info_present {
        hdr.segmentation.adaptive = gb.get_bit() != 0;
        parse_seg_info(
            &mut hdr.segmentation.d,
            gb,
            8 << (hdr.segmentation.ext as usize),
        );
    }

    hdr.intra_dip = gb.get_bit() != 0;
    hdr.intra_edge_filter = gb.get_bit() != 0;
    hdr.mrls = gb.get_bit() != 0;
    hdr.cfl = gb.get_bit() != 0;
    if hdr.layout != PixelLayout::I400 {
        hdr.cfl_ds_filter_index = gb.get_bits(2) as u8;
        // index 3 is reserved; AVM treats it as 0
        if hdr.cfl_ds_filter_index == 3 {
            hdr.cfl_ds_filter_index = 0;
        }
    }
    hdr.mhccp = gb.get_bit() != 0;
    hdr.ibp = gb.get_bit() != 0;

    if hdr.reduced_still_picture_header {
        hdr.motion_modes = 1;
    } else {
        hdr.motion_modes = 1; // MM_TRANSLATION = bit 0
        for shift in [1, 2, 3, 4] {
            hdr.motion_modes |= (gb.get_bit() as u8) << shift;
        }
        if hdr.motion_modes & !1 != 0 {
            hdr.frame_motion_modes_present = gb.get_bit() != 0;
        }
        if hdr.motion_modes & (1 << 3) != 0 {
            // MM_WARP_DELTA
            hdr.six_param_warp_delta = gb.get_bit() != 0;
        }
        hdr.masked_compound = gb.get_bit() != 0;
        hdr.ref_frame_mvs = gb.get_bit() != 0;
        if hdr.ref_frame_mvs {
            hdr.reduced_ref_frame_mvs_mode = gb.get_bit() as u8;
        }
        hdr.order_hint_n_bits = gb.get_bits(4) as u8 + 1;
    }

    hdr.refmv_bank = gb.get_bit() != 0;
    // 0 = off, 2 = always (threshold 2), 1 = constraint (threshold 4).
    hdr.drl_reorder = if gb.get_bit() != 0 {
        0
    } else {
        2 - gb.get_bit() as u8
    };

    if hdr.reduced_still_picture_header {
        hdr.ref_frames = 2;
        hdr.def_max_drl_bits = 1;
    } else {
        hdr.explicit_ref_frame_map = gb.get_bit() != 0;
        hdr.ref_frames = if gb.get_bit() != 0 {
            gb.get_bits(4) as u8 + 1
        } else {
            8
        };
        if hdr.ref_frames > 8 {
            return Err(TealdustError::InvalidSequenceHeader);
        }
        hdr.ref_frames_log2 = if hdr.ref_frames <= 2 {
            hdr.ref_frames - 1
        } else {
            1 + ulog2(hdr.ref_frames as u32 - 1) as u8
        };
        hdr.number_of_bits_for_lt_frame_id = gb.get_bits(3) as u8;
        hdr.def_max_drl_bits = gb.get_uniform(5) as u8 + 1;
        hdr.allow_frame_max_drl_bits = gb.get_bit() != 0;
    }
    hdr.def_max_bvp_drl_bits = gb.get_uniform(3) as u8 + 1;
    hdr.allow_max_bvp_drl_bits = gb.get_bit() != 0;
    if !hdr.reduced_still_picture_header {
        hdr.num_same_ref_comp = gb.get_bits(2) as u8;
    }

    if !hdr.reduced_still_picture_header {
        let tip_val = gb.get_bit();
        hdr.tip = tip_val != 0 && (1 + gb.get_bit() as u8) > 0;
        if hdr.tip {
            hdr.tip_hole_fill = gb.get_bit() != 0;
        }
        hdr.mv_traj = gb.get_bit() != 0;
    }
    hdr.bawp = gb.get_bit() != 0;
    if !hdr.reduced_still_picture_header {
        hdr.cwp = gb.get_bit() != 0;
        hdr.imp_msk_bld = gb.get_bit() != 0;
        hdr.db_sub_pu = gb.get_bit() != 0;
        if hdr.tip && hdr.db_sub_pu {
            hdr.tip_explicit_qp = gb.get_bit() != 0;
        }
    }

    if !hdr.reduced_still_picture_header {
        hdr.opfl_refine = gb.get_bits(2) != 0;
        hdr.refine_mv = gb.get_bit() != 0;
        if hdr.tip && (hdr.opfl_refine || hdr.refine_mv) {
            hdr.tip_refine_mv = gb.get_bit() != 0;
        }
        hdr.bru = gb.get_bit() != 0;
        hdr.adaptive_mvd = gb.get_bit() != 0;
        hdr.mvd_sign_derive = gb.get_bit() != 0;
        hdr.flex_mvres = gb.get_bit() != 0;
        hdr.global_motion = gb.get_bit() != 0;
        hdr.short_refresh_frame_flags = gb.get_bit() != 0;
    }

    if hdr.reduced_still_picture_header {
        hdr.screen_content_tools = AdaptiveBoolean::Adaptive;
        hdr.force_integer_mv = AdaptiveBoolean::Adaptive;
    } else {
        hdr.screen_content_tools = if gb.get_bit() != 0 {
            AdaptiveBoolean::Adaptive
        } else {
            if gb.get_bit() != 0 {
                AdaptiveBoolean::On
            } else {
                AdaptiveBoolean::Off
            }
        };
        hdr.force_integer_mv = if hdr.screen_content_tools != AdaptiveBoolean::Off {
            if gb.get_bit() != 0 {
                AdaptiveBoolean::Adaptive
            } else {
                if gb.get_bit() != 0 {
                    AdaptiveBoolean::On
                } else {
                    AdaptiveBoolean::Off
                }
            }
        } else {
            AdaptiveBoolean::Adaptive
        };
    }

    hdr.fsc = gb.get_bit() != 0;
    hdr.idtx_intra = hdr.fsc || gb.get_bit() != 0;
    hdr.ist[0] = gb.get_bit() != 0;
    hdr.ist[1] = gb.get_bit() != 0;
    if hdr.layout != PixelLayout::I400 {
        hdr.chroma_dctonly = gb.get_bit() != 0;
    }
    if !hdr.reduced_still_picture_header {
        hdr.inter_ddt = gb.get_bit() != 0;
    }
    hdr.reduced_tx_part_set = gb.get_bit() != 0;
    if hdr.layout != PixelLayout::I400 {
        hdr.cctx = gb.get_bit() != 0;
    }

    let tcq_bit = gb.get_bit();
    hdr.tcq = if tcq_bit != 0 {
        if !hdr.reduced_still_picture_header && gb.get_bit() != 0 {
            AdaptiveBoolean::Adaptive
        } else {
            AdaptiveBoolean::On
        }
    } else {
        AdaptiveBoolean::Off
    };
    if hdr.tcq != AdaptiveBoolean::On {
        hdr.parity_hiding = gb.get_bit() != 0;
    }

    hdr.avg_cdf = hdr.reduced_still_picture_header || gb.get_bit() != 0;
    if hdr.avg_cdf {
        hdr.avg_cdf_type = if hdr.reduced_still_picture_header || gb.get_bit() != 0 {
            1
        } else {
            0
        };
    }

    if hdr.layout != PixelLayout::I400 {
        hdr.separate_uv_delta_q = gb.get_bit() != 0;
    }
    hdr.equal_ac_dc_q = gb.get_bit() != 0;
    if !hdr.equal_ac_dc_q {
        hdr.base_ydc_dq = gb.get_bits(5) as i8 - 23;
        hdr.ydc_dq_enabled = gb.get_bit() != 0;
    }
    if hdr.layout != PixelLayout::I400 {
        if !hdr.equal_ac_dc_q {
            hdr.base_uvdc_dq = (gb.get_bits(5) as i32 - 23) as u8;
            hdr.uvdc_dq_enabled = gb.get_bit() != 0;
        }
        hdr.base_uvac_dq = (gb.get_bits(5) as i32 - 23) as u8;
        hdr.uvac_dq_enabled = gb.get_bit() != 0;
        if hdr.equal_ac_dc_q {
            hdr.base_uvdc_dq = hdr.base_uvac_dq;
        }
    }

    hdr.disable_loopfilters_across_tiles = gb.get_bit() != 0;
    hdr.cdef = gb.get_bit() != 0;
    hdr.gdf = gb.get_bit() != 0;
    if hdr.gdf && hdr.sb128 == 0 {
        hdr.gdf_unit_matches_sbsz = gb.get_bit() != 0;
    }
    hdr.restoration = gb.get_bit() != 0;
    if hdr.restoration {
        let no_pc_wiener = gb.get_bit() as u8;
        let no_ns_wiener_y = gb.get_bit() as u8;
        hdr.rst_disable_mask[0] = (no_ns_wiener_y << 1) | no_pc_wiener;
        if gb.get_bit() != 0 {
            hdr.rst_disable_mask[1] = (gb.get_bit() as u8) << 1 | 1;
        } else {
            hdr.rst_disable_mask[1] = hdr.rst_disable_mask[0] | 1;
        }
    }
    hdr.ccso = gb.get_bit() != 0;
    if hdr.ccso {
        hdr.ccso_unit_matches_sbsz = gb.get_bit() != 0;
    }
    hdr.cdef_on_skiptx = if hdr.reduced_still_picture_header {
        AdaptiveBoolean::Adaptive
    } else if gb.get_bit() != 0 {
        AdaptiveBoolean::On
    } else if gb.get_bit() != 0 {
        AdaptiveBoolean::Off
    } else {
        AdaptiveBoolean::Adaptive
    };
    hdr.df_par_bits = 2 + gb.get_bits(2) as u8;

    let tiling_present = gb.get_bit();
    if tiling_present != 0 {
        let tiling_type = gb.get_bit();
        hdr.tiling.present = if tiling_type != 0 {
            AdaptiveBoolean::Adaptive
        } else {
            AdaptiveBoolean::On
        };
        parse_tile_info(
            &mut hdr.tiling.t,
            gb,
            1,
            hdr.sb128,
            hdr.sb128,
            hdr.max_width,
            hdr.max_height,
            hdr.level,
            hdr.tier,
        )?;
    }

    hdr.film_grain_present = gb.get_bit() != 0;

    if gb.has_error() {
        return Err(TealdustError::InvalidSequenceHeader);
    }

    if !strict {
        return Ok(hdr);
    }

    // extension handling — skip for non-strict mode
    let has_extension = gb.get_bit() != 0;
    if has_extension {
        // skip extension bits (we don't parse them)
    }

    check_trailing_bits(gb, strict)?;
    Ok(hdr)
}

pub fn parse_frame_hdr(
    seqhdr: &SequenceHeader,
    refs: &[RefState; 8],
    obu_type: ObuType,
    gb: &mut GetBits,
) -> Result<FrameHeader> {
    use crate::levels::MotionMode;
    use crate::tables::{
        CCSO_QUANT_SZ, DEFAULT_WM_PARAMS, NS_WIENER_COEF_RANGE_UV, NS_WIENER_COEF_RANGE_Y,
        SUBSET_MASKS_UV, SUBSET_MASKS_Y, WIENER_NS_FILTERS,
    };

    let mut hdr = FrameHeader::default();

    hdr.id = gb.get_vlc() as u8;
    // hdr.id is the frame_parameter_set_id; not used downstream, so only stored.
    let _seqhdr_idx = gb.get_vlc() as u8;
    // _seqhdr_idx identifies which buffered sequence header this frame references.
    // A strict check against seqhdr.id breaks real-world encoders that encode a
    // non-zero sequence-header ID but always emit 0 in frame headers (common in
    // AVIF still-image encoders).  Since the decoder keeps exactly one active
    // sequence header in c.seq_hdr, the reference is unambiguous regardless of
    // the numeric ID values, so we skip the equality assertion.

    hdr.show_existing_frame = (obu_type == ObuType::Sef) as u8;
    if hdr.show_existing_frame != 0 {
        hdr.existing_frame_idx = gb.get_bits(seqhdr.ref_frames_log2 as i32) as i8;
        if hdr.existing_frame_idx as u8 >= seqhdr.ref_frames {
            return Err(TealdustError::InvalidFrameHeader);
        }
        // consumed by the reference at this point; match it.
        return Ok(hdr);
    }

    if seqhdr.reduced_still_picture_header {
        hdr.frame_type = FrameType::Key;
        hdr.show_immediate = 1;
    } else {
        match obu_type {
            ObuType::ClosedLoopKf | ObuType::OpenLoopKf => {
                hdr.frame_type = FrameType::Key;
                hdr.closed_loop_key = (obu_type == ObuType::ClosedLoopKf) as u8;
            }
            ObuType::Ras | ObuType::Switch => {
                hdr.frame_type = FrameType::Switch;
            }
            ObuType::LeadingTip | ObuType::Tip | ObuType::Bridge => {
                hdr.frame_type = FrameType::Inter;
            }
            _ => {
                if gb.get_bit() == 0 {
                    hdr.frame_type = FrameType::Intra;
                } else {
                    hdr.frame_type = FrameType::Inter;
                }
            }
        }
        hdr.ltr_id = -1;
        if hdr.frame_type == FrameType::Key {
            if seqhdr.number_of_bits_for_lt_frame_id > 0 {
                hdr.ltr_id = gb.get_bits(seqhdr.number_of_bits_for_lt_frame_id as i32) as i8 - 1;
            }
        } else if (obu_type == ObuType::Ras || obu_type == ObuType::OpenLoopKf)
            && seqhdr.number_of_bits_for_lt_frame_id > 0
        {
            hdr.n_ref_frames = gb.get_bits(3) as u8;
            if hdr.n_ref_frames == 0 || hdr.n_ref_frames as usize > REFS_PER_FRAME {
                return Err(TealdustError::InvalidFrameHeader);
            }
            for n in 0..hdr.n_ref_frames as usize {
                hdr.refidx[n] = gb.get_bits(seqhdr.number_of_bits_for_lt_frame_id as i32) as i8;
            }
        }
        if obu_type != ObuType::Bridge {
            if obu_type != ObuType::OpenLoopKf {
                hdr.show_immediate = gb.get_bit() as u8;
            }
            if hdr.show_immediate == 0 && !seqhdr.monotonic {
                hdr.show_implicit = gb.get_bit() as u8;
            }
        }
    }

    hdr.primary_ref_frame = PRIMARY_REF_NONE;
    if !seqhdr.reduced_still_picture_header {
        hdr.frame_size_override = if hdr.frame_type == FrameType::Switch {
            1
        } else {
            gb.get_bit() as u8
        };
        hdr.frame_offset = gb.get_bits(seqhdr.order_hint_n_bits as i32) as u8;
        if hdr.frame_type == FrameType::Inter {
            hdr.primary_ref_signaled = gb.get_bit() as u8;
            if obu_type != ObuType::LeadingTip && obu_type != ObuType::Tip {
                hdr.cross_frame_context = gb.get_bit() as u8;
            }
            if hdr.primary_ref_signaled != 0 {
                hdr.primary_ref_frame = gb.get_bits(3) as u8;
            }
        }
    }

    // refresh_frame_flags
    if obu_type == ObuType::ClosedLoopKf && seqhdr.max_mlayer_id == 0 {
        hdr.refresh_frame_flags = ((1u32 << seqhdr.ref_frames) - 1) as u8;
    } else if obu_type == ObuType::OpenLoopKf || seqhdr.max_mlayer_id > 0 {
        if seqhdr.short_refresh_frame_flags {
            hdr.refresh_frame_flags = 1 << gb.get_bits(seqhdr.ref_frames_log2 as i32);
        } else {
            hdr.refresh_frame_flags = gb.get_bits(seqhdr.ref_frames as i32) as u8;
        }
    } else if hdr.frame_type != FrameType::Switch && seqhdr.short_refresh_frame_flags {
        let refresh = gb.get_bit() != 0;
        if refresh {
            let refresh_idx = gb.get_bits(seqhdr.ref_frames_log2 as i32);
            if refresh_idx >= seqhdr.ref_frames as u32 {
                return Err(TealdustError::InvalidFrameHeader);
            }
            hdr.refresh_frame_flags = 1 << refresh_idx;
        }
    } else {
        hdr.refresh_frame_flags = gb.get_bits(seqhdr.ref_frames as i32) as u8;
    }

    let mut tip_output_frame = false;

    if hdr.is_inter_or_switch() {
        if hdr.frame_type == FrameType::Switch || seqhdr.explicit_ref_frame_map {
            hdr.n_ref_frames = gb.get_bits(3) as u8;
            if hdr.n_ref_frames as i32 > imin(7, seqhdr.ref_frames as i32) {
                return Err(TealdustError::InvalidFrameHeader);
            }
            if hdr.n_ref_frames == 0 || hdr.n_ref_frames as usize > REFS_PER_FRAME {
                return Err(TealdustError::InvalidFrameHeader);
            }
            for n in 0..hdr.n_ref_frames as usize {
                hdr.refidx[n] = gb.get_bits(seqhdr.ref_frames_log2 as i32) as i8;
                if hdr.refidx[n] as u8 >= seqhdr.ref_frames {
                    return Err(TealdustError::InvalidFrameHeader);
                }
            }
        } else {
            hdr.n_ref_frames = get_ref_frames(&mut hdr, seqhdr, refs, false) as u8;
            if hdr.n_ref_frames == 0 {
                return Err(TealdustError::InvalidFrameHeader);
            }
        }
        let poc = hdr.frame_offset as i32;
        if hdr.primary_ref_frame != PRIMARY_REF_NONE && hdr.primary_ref_frame >= hdr.n_ref_frames {
            return Err(TealdustError::InvalidFrameHeader);
        }

        for n in 0..hdr.n_ref_frames as usize {
            let refhdr = ref_slot(refs, hdr.refidx[n] as i32)?
                .p
                .frame_hdr
                .as_ref()
                .ok_or(TealdustError::InvalidFrameHeader)?;
            let pocdiff = get_poc_diff(
                seqhdr.order_hint_n_bits as i32,
                poc,
                refhdr.frame_offset as i32,
            );
            hdr.has_future_refs |= (pocdiff < 0) as u8;
            hdr.has_past_refs |= (pocdiff > 0) as u8;
        }
        hdr.has_bothside_refs = (hdr.has_future_refs != 0 && hdr.has_past_refs != 0) as u8;
    }

    read_frame_size(&mut hdr, seqhdr, refs, gb)?;

    if hdr.is_inter_or_switch() {
        if hdr.frame_type == FrameType::Inter && !seqhdr.explicit_ref_frame_map {
            hdr.n_ref_frames = get_ref_frames(&mut hdr, seqhdr, refs, true) as u8;
            if hdr.n_ref_frames == 0 {
                return Err(TealdustError::InvalidFrameHeader);
            }
        }

        // base_resolution_update (AV2 §5.9.6): not parsed by the reference decoder

        if seqhdr.ref_frame_mvs {
            hdr.use_ref_frame_mvs = gb.get_bit() as u8;
        }
        hdr.tmvp_sample_step = 1
            + (hdr.use_ref_frame_mvs != 0
                && hdr.n_ref_frames > 1
                && seqhdr.sb128 != 0
                && gb.get_bit() != 0) as u8;

        hdr.tip.subpel_filter = FilterMode::Sharp8Tap as u8;
        if seqhdr.tip && hdr.n_ref_frames > 1 && hdr.use_ref_frame_mvs != 0 {
            if obu_type == ObuType::Tip || obu_type == ObuType::LeadingTip {
                hdr.tip.frame_mode = 2; // output
                hdr.opfl_refine_type = 2 * (seqhdr.opfl_refine && seqhdr.tip_refine_mv) as u8;
            } else {
                hdr.tip.frame_mode = gb.get_bit() as u8;
                hdr.opfl_refine_type = if (seqhdr.opfl_refine as u8) < 3 {
                    seqhdr.opfl_refine as u8
                } else if gb.get_bit() != 0 {
                    1
                } else {
                    2 * gb.get_bit() as u8
                };
            }
            if hdr.tip.frame_mode != 0 {
                if seqhdr.tip_hole_fill {
                    hdr.tip.hole_fill = gb.get_bit() as u8;
                }
                if hdr.has_bothside_refs == 0
                    || !seqhdr.tip_refine_mv
                    || (!seqhdr.opfl_refine && !seqhdr.refine_mv)
                {
                    hdr.tip.global_wtd_idx = gb.get_bits(3) as u8;
                }
                if hdr.tip.frame_mode == 2 {
                    if gb.get_bit() == 0 {
                        hdr.tip.gmv_y = gb.get_bits(4) as i8;
                        hdr.tip.gmv_x = gb.get_bits(4) as i8;
                        if hdr.tip.gmv_y != 0 && gb.get_bit() != 0 {
                            hdr.tip.gmv_y = -hdr.tip.gmv_y;
                        }
                        if hdr.tip.gmv_x != 0 && gb.get_bit() != 0 {
                            hdr.tip.gmv_x = -hdr.tip.gmv_x;
                        }
                    }
                    hdr.tip.subpel_filter = if gb.get_bit() != 0 {
                        FilterMode::Sharp8Tap as u8
                    } else if gb.get_bit() != 0 {
                        FilterMode::Regular8Tap as u8
                    } else {
                        FilterMode::Smooth8Tap as u8
                    };
                }
            }
            find_tip_ref_frames(&mut hdr, seqhdr, refs)?;
        } else {
            hdr.opfl_refine_type = if (seqhdr.opfl_refine as u8) < 3 {
                seqhdr.opfl_refine as u8
            } else if gb.get_bit() != 0 {
                1
            } else {
                2 * gb.get_bit() as u8
            };
        }

        if hdr.tip.frame_mode == 2 {
            if seqhdr.db_sub_pu {
                hdr.deblock.sub_pu = gb.get_bit() as u8;
                if hdr.deblock.sub_pu != 0 {
                    hdr.tip.apply_filter = gb.get_bit() as u8;
                    if hdr.tip.apply_filter != 0 {
                        hdr.deblock.level_y[0] = 1;
                        hdr.deblock.level_y[1] = 1;
                        hdr.deblock.level_u = 1;
                        hdr.deblock.level_v = 1;
                    }
                }
            }
            if seqhdr.tip_explicit_qp {
                // TIP explicit QP: yac_delta and (sometimes) u/v ac delta (AV2
                // No bits are consumed by the reference here; match it. When
                // tip_explicit_qp is unset, quant.yac is derived from the two TIP
            } else {
                let tip_ref0 = *hdr
                    .refidx
                    .get(hdr.tip.reference[0] as usize)
                    .ok_or(TealdustError::InvalidFrameHeader)?;
                let tip_ref1 = *hdr
                    .refidx
                    .get(hdr.tip.reference[1] as usize)
                    .ok_or(TealdustError::InvalidFrameHeader)?;
                let ref1hdr = ref_slot(refs, tip_ref0 as i32)?
                    .p
                    .frame_hdr
                    .as_ref()
                    .ok_or(TealdustError::InvalidFrameHeader)?;
                let ref2hdr = ref_slot(refs, tip_ref1 as i32)?
                    .p
                    .frame_hdr
                    .as_ref()
                    .ok_or(TealdustError::InvalidFrameHeader)?;
                hdr.quant.yac = (ref1hdr.quant.yac + ref2hdr.quant.yac + 1) >> 1;
            }

            if hdr.tip.apply_filter != 0 {
                parse_tile_info_frmhdr(&mut hdr, seqhdr, gb)?;
            } else {
                hdr.sb128 = if hdr.is_inter_or_switch() {
                    seqhdr.sb128
                } else {
                    if seqhdr.sb128 != 0 { 1 } else { 0 }
                };
                hdr.tiling.t.rows = 1;
                hdr.tiling.t.cols = 1;
                let shift = 6 + hdr.sb128 as i32;
                hdr.tiling.t.col_start_sb[0] = 0;
                hdr.tiling.t.col_start_sb[1] = ((hdr.width + ((1 << shift) - 1)) >> shift) as u16;
                hdr.tiling.t.row_start_sb[0] = 0;
                hdr.tiling.t.row_start_sb[1] = ((hdr.height + ((1 << shift) - 1)) >> shift) as u16;
            }

            hdr.disable_cdf_update = 1;
            let pri_sec = derive_pri_sec_ref(&hdr, seqhdr, refs);
            hdr.primary_ref_frame = pri_sec[0] as u8;
            hdr.secondary_ref_frame = pri_sec[1] as u8;
            tip_output_frame = true;
        }
    }

    if !tip_output_frame {
        // screen content tools
        hdr.allow_screen_content_tools = if seqhdr.screen_content_tools == AdaptiveBoolean::Adaptive
        {
            gb.get_bit() as u8
        } else {
            seqhdr.screen_content_tools as u8
        };
        if hdr.allow_screen_content_tools != 0 {
            hdr.force_integer_mv = if seqhdr.force_integer_mv == AdaptiveBoolean::Adaptive {
                gb.get_bit() as u8
            } else {
                seqhdr.force_integer_mv as u8
            };
        }

        hdr.allow_intrabc = gb.get_bit() as u8;
        if hdr.allow_intrabc != 0 {
            if hdr.is_key_or_intra() {
                hdr.allow_global_intrabc = gb.get_bit() as u8;
            }
            hdr.allow_local_intrabc = (hdr.allow_global_intrabc == 0 || gb.get_bit() != 0) as u8;
        }
        // Coded as a reference-uniform value over [MIN, MAX], relative to the
        // sequence default and offset by MIN (1), like AVM.
        if hdr.allow_intrabc != 0 {
            hdr.max_bvp_drl_bits = if seqhdr.allow_max_bvp_drl_bits {
                gb.get_ref_uniform(3, (seqhdr.def_max_bvp_drl_bits as u32).saturating_sub(1)) as u8
                    + 1
            } else {
                seqhdr.def_max_bvp_drl_bits
            };
        }

        if hdr.is_inter_or_switch() {
            hdr.max_drl_bits = if seqhdr.allow_frame_max_drl_bits {
                gb.get_ref_uniform(5, (seqhdr.def_max_drl_bits as u32).saturating_sub(1)) as u8 + 1
            } else {
                seqhdr.def_max_drl_bits
            };
            if hdr.force_integer_mv == 0 {
                hdr.mv_precision = if gb.get_bit() != 0 {
                    2
                } else {
                    1 + 2 * gb.get_bit() as u8
                };
            }
            hdr.subpel_filter_mode = if gb.get_bit() != 0 {
                FilterMode::Switchable
            } else {
                match gb.get_bits(2) {
                    0 => FilterMode::Regular8Tap,
                    1 => FilterMode::Smooth8Tap,
                    2 => FilterMode::Sharp8Tap,
                    _ => FilterMode::Bilinear,
                }
            };
            if seqhdr.frame_motion_modes_present {
                hdr.motion_modes = 1;
                let mut n = 2u8;
                while n > 0 {
                    if (seqhdr.motion_modes & n) != 0 && gb.get_bit() != 0 {
                        hdr.motion_modes |= n;
                    }
                    if n == 16 {
                        break;
                    }
                    n <<= 1;
                }
            } else {
                hdr.motion_modes = seqhdr.motion_modes;
            }
        }

        hdr.disable_cdf_update = gb.get_bit() as u8;

        parse_tile_info_frmhdr(&mut hdr, seqhdr, gb)?;
        if hdr.tiling.t.log2_cols != 0 || hdr.tiling.t.log2_rows != 0 {
            if seqhdr.avg_cdf_type == 0 {
                hdr.tiling.update = gb
                    .get_bits(hdr.tiling.t.log2_cols as i32 + hdr.tiling.t.log2_rows as i32)
                    as u16;
            }
            if hdr.tiling.update >= hdr.tiling.t.cols as u16 * hdr.tiling.t.rows as u16 {
                return Err(TealdustError::InvalidFrameHeader);
            }
            hdr.tiling.n_bytes = gb.get_bits(2) as u8 + 1;
        }

        // quant
        hdr.quant.yac = gb.get_bits(8 + (seqhdr.hbd != 0) as i32) as u16;
        if seqhdr.ydc_dq_enabled && gb.get_bit() != 0 {
            hdr.quant.ydc_delta = gb.get_sbits(7) as i8;
        }
        if seqhdr.layout != PixelLayout::I400 && (seqhdr.uvdc_dq_enabled || seqhdr.uvac_dq_enabled)
        {
            let diff_uv_delta = if seqhdr.separate_uv_delta_q {
                gb.get_bit() != 0
            } else {
                false
            };
            if seqhdr.uvdc_dq_enabled && gb.get_bit() != 0 {
                hdr.quant.udc_delta = gb.get_sbits(7) as i8;
            }
            if seqhdr.uvac_dq_enabled && gb.get_bit() != 0 {
                hdr.quant.uac_delta = gb.get_sbits(7) as i8;
            }
            if diff_uv_delta {
                if seqhdr.uvdc_dq_enabled && gb.get_bit() != 0 {
                    hdr.quant.vdc_delta = gb.get_sbits(7) as i8;
                }
                if seqhdr.uvac_dq_enabled && gb.get_bit() != 0 {
                    hdr.quant.vac_delta = gb.get_sbits(7) as i8;
                }
            } else {
                hdr.quant.vdc_delta = hdr.quant.udc_delta;
                hdr.quant.vac_delta = hdr.quant.uac_delta;
            }
        }

        hdr.secondary_ref_frame = PRIMARY_REF_NONE;
        if hdr.is_inter_or_switch() {
            let pri_sec = derive_pri_sec_ref(&hdr, seqhdr, refs);
            if hdr.primary_ref_signaled == 0 {
                hdr.primary_ref_frame = pri_sec[0] as u8;
            }
            if hdr.primary_ref_frame != PRIMARY_REF_NONE {
                hdr.secondary_ref_frame =
                    pri_sec[(pri_sec[1] != hdr.primary_ref_frame as i32) as usize] as u8;
            }
        }

        // segmentation
        hdr.segmentation.enabled = gb.get_bit() as u8;
        if hdr.segmentation.enabled != 0 {
            if seqhdr.segmentation.info_present
                && (!seqhdr.segmentation.adaptive || gb.get_bit() != 0)
            {
                hdr.segmentation.d = seqhdr.segmentation.d;
            } else {
                parse_seg_info(
                    &mut hdr.segmentation.d,
                    gb,
                    8 << seqhdr.segmentation.ext as u32,
                );
            }
            if hdr.primary_ref_frame == PRIMARY_REF_NONE {
                hdr.segmentation.update_map = 1;
            } else {
                hdr.segmentation.update_map = gb.get_bit() as u8;
                if hdr.segmentation.update_map != 0 && hdr.frame_type != FrameType::Switch {
                    hdr.segmentation.temporal = gb.get_bit() as u8;
                }
            }
            let mut m = hdr.segmentation.d.skip_mask | hdr.segmentation.d.globalmv_mask;
            hdr.segmentation.preskip = (m != 0) as u8;
            m |= hdr.segmentation.d.delta_q_mask;
            hdr.segmentation.last_active_segid = if m != 0 { ulog2(m as u32) as i8 } else { -1 };
        }

        // qm
        hdr.quant.qm.enabled = gb.get_bit() as u8;
        if hdr.quant.qm.enabled != 0 {
            hdr.quant.qm.num = if hdr.segmentation.enabled != 0 {
                gb.get_bits(2) as u8 + 1
            } else {
                1
            };
            for n in 0..hdr.quant.qm.num as usize {
                hdr.quant.qm.y[n] = gb.get_bits(4) as u8;
                if seqhdr.layout != PixelLayout::I400 {
                    if gb.get_bit() != 0 {
                        hdr.quant.qm.u[n] = hdr.quant.qm.y[n];
                        hdr.quant.qm.v[n] = hdr.quant.qm.y[n];
                    } else {
                        hdr.quant.qm.u[n] = gb.get_bits(4) as u8;
                        hdr.quant.qm.v[n] = if seqhdr.separate_uv_delta_q {
                            gb.get_bits(4) as u8
                        } else {
                            hdr.quant.qm.u[n]
                        };
                    }
                }
            }
        }

        // delta q
        if hdr.quant.yac != 0 {
            hdr.delta.q.present = gb.get_bit() as u8;
            if hdr.delta.q.present != 0 {
                hdr.delta.q.res_log2 = gb.get_bits(2) as u8;
            }
        }

        // lossless
        let delta_lossless = hdr.quant.ydc_delta == 0
            && hdr.quant.udc_delta == 0
            && hdr.quant.uac_delta == 0
            && hdr.quant.vdc_delta == 0
            && hdr.quant.vac_delta == 0;
        hdr.all_lossless = 1;
        hdr.any_lossless = 0;
        let qmax = 255 + 48 * seqhdr.hbd as i32;
        for i in 0..MAX_SEGMENTS {
            hdr.segmentation.qidx[i] = if hdr.segmentation.enabled != 0 {
                iclip(
                    hdr.quant.yac as i32 + hdr.segmentation.d.delta_q[i] as i32,
                    0,
                    qmax,
                ) as u16
            } else {
                hdr.quant.yac.min(qmax as u16)
            };
            hdr.segmentation.lossless[i] = (hdr.segmentation.qidx[i] == 0 && delta_lossless) as u8;
            hdr.all_lossless &= hdr.segmentation.lossless[i];
            hdr.any_lossless |= hdr.segmentation.lossless[i];
        }

        if hdr.all_lossless == 0 {
            hdr.tcq = if seqhdr.tcq == AdaptiveBoolean::Adaptive {
                gb.get_bit() as u8
            } else {
                seqhdr.tcq as u8
            };
        }
        if hdr.all_lossless == 0 && hdr.tcq == 0 && seqhdr.parity_hiding {
            hdr.parity_hiding = gb.get_bit() as u8;
        }

        // deblock
        if hdr.all_lossless == 0 {
            if hdr.frame_type == FrameType::Inter && seqhdr.db_sub_pu {
                hdr.deblock.sub_pu = gb.get_bit() as u8;
            }
            hdr.deblock.level_y[0] = gb.get_bit() as u8;
            hdr.deblock.level_y[1] = gb.get_bit() as u8;
            if seqhdr.layout != PixelLayout::I400
                && (hdr.deblock.level_y[0] != 0 || hdr.deblock.level_y[1] != 0)
            {
                hdr.deblock.level_u = gb.get_bit() as u8;
                hdr.deblock.level_v = gb.get_bit() as u8;
            }
            let bits = seqhdr.df_par_bits as i32;
            let off = 1i32 << (bits - 1);
            if hdr.deblock.level_y[0] != 0 && gb.get_bit() != 0 {
                hdr.deblock.delta_q_y[0] = (gb.get_bits(bits) as i32 - off) as i8;
            }
            if hdr.deblock.level_y[1] != 0 {
                hdr.deblock.delta_q_y[1] = if gb.get_bit() != 0 {
                    (gb.get_bits(bits) as i32 - off) as i8
                } else {
                    hdr.deblock.delta_q_y[0]
                };
            }
            if hdr.deblock.level_u != 0 && gb.get_bit() != 0 {
                hdr.deblock.delta_q_u = (gb.get_bits(bits) as i32 - off) as i8;
            }
            if hdr.deblock.level_v != 0 && gb.get_bit() != 0 {
                hdr.deblock.delta_q_v = (gb.get_bits(bits) as i32 - off) as i8;
            }
        }

        // gdf
        if hdr.all_lossless == 0 && seqhdr.gdf {
            let gdf_bs = 128 << (hdr.sb128 == 2) as i32;
            let mut gdf_val: u8 = (seqhdr.reduced_still_picture_header || gb.get_bit() != 0) as u8;
            if gdf_val != 0 {
                if imax(hdr.width, hdr.height) > gdf_bs {
                    gdf_val += gb.get_bit() as u8;
                }
                let qp_base = if hdr.is_key_or_intra() { 85 } else { 110 };
                let qp_diff = hdr.quant.yac as i32 - qp_base - 48 * seqhdr.hbd as i32;
                let qp_idx_offset = gb.get_bits(2) as i32;
                hdr.gdf.qp_idx = iclip((qp_diff - 37) / 25, 0, 2) as u8 + qp_idx_offset as u8;
                hdr.gdf.scale = gb.get_bits(2) as u8 + 1;
            }
            hdr.gdf.enabled = match gdf_val {
                0 => AdaptiveBoolean::Off,
                1 => AdaptiveBoolean::On,
                _ => AdaptiveBoolean::Adaptive,
            };
        }

        // cdef
        if hdr.all_lossless == 0 && seqhdr.cdef {
            hdr.cdef.enabled = (seqhdr.reduced_still_picture_header || gb.get_bit() != 0) as u8;
            if hdr.cdef.enabled != 0 {
                hdr.cdef.damping = gb.get_bits(2) as u8 + 3;
                hdr.cdef.n_strengths = gb.get_bits(3) as u8 + 1;
                hdr.cdef.on_skiptx = if seqhdr.cdef_on_skiptx == AdaptiveBoolean::Adaptive {
                    gb.get_bit() as u8
                } else {
                    seqhdr.cdef_on_skiptx as u8
                };
                for i in 0..hdr.cdef.n_strengths as usize {
                    let b = gb.get_bit() as i32;
                    hdr.cdef.y_strength[i] = gb.get_bits(6 - 4 * b) as u8;
                    if seqhdr.layout != PixelLayout::I400 {
                        let b = gb.get_bit() as i32;
                        hdr.cdef.uv_strength[i] = gb.get_bits(6 - 4 * b) as u8;
                    }
                }
            }
        }

        let n_bits_ref = if hdr.n_ref_frames <= 2 {
            hdr.n_ref_frames as i32 - 1
        } else {
            1 + ulog2(hdr.n_ref_frames as u32 - 1)
        };

        // restoration
        if hdr.all_lossless == 0 && seqhdr.restoration {
            for p in 0..3usize {
                let disable_mask = seqhdr.rst_disable_mask[if p != 0 { 1 } else { 0 }];
                hdr.restoration.p[p].restoration_type = if disable_mask == 0 {
                    gb.get_bits(2) as u8
                } else if disable_mask == 3 {
                    RestorationType::None as u8
                } else {
                    gb.get_bit() as u8 * (3 - disable_mask)
                };

                if hdr.restoration.p[p].restoration_type >= RestorationType::NsWiener as u8 {
                    let is_inter = hdr.is_inter_or_switch();
                    let pd = &mut hdr.restoration.p[p].ns;
                    pd.frame_filters_on = gb.get_bit() as u8;
                    if pd.frame_filters_on != 0 {
                        if is_inter {
                            pd.temporal = gb.get_bit() as u8;
                        }
                        if pd.temporal != 0 {
                            let mut reference = 0u8;
                            if n_bits_ref > 0 {
                                reference = gb.get_bits(n_bits_ref) as u8;
                                pd.refidx = reference;
                                if reference >= hdr.n_ref_frames {
                                    return Err(TealdustError::InvalidFrameHeader);
                                }
                            }
                            let refhdr = ref_slot(refs, hdr.refidx[reference as usize] as i32)?
                                .p
                                .frame_hdr
                                .as_ref()
                                .ok_or(TealdustError::InvalidFrameHeader)?;
                            let mut rpd = &refhdr.restoration.p[p].ns;
                            if rpd.frame_filters_on == 0 && p != 0 {
                                rpd = &refhdr.restoration.p[3 - p].ns;
                            }
                            if rpd.frame_filters_on == 0 {
                                return Err(TealdustError::InvalidFrameHeader);
                            }
                            pd.num_classes_idx = rpd.num_classes_idx;
                            pd.num_classes = rpd.num_classes;
                        } else {
                            let val = gb.get_bits(3) as u8;
                            pd.num_classes_idx = val;
                            pd.num_classes = 1
                                + val
                                + imax(val as i32 - 3, 0) as u8
                                + imax(val as i32 - 5, 0) as u8 * 2;
                        }
                    } else {
                        pd.num_classes_idx = 0;
                        pd.num_classes = 1;
                    }
                }
            }

            hdr.restoration.unit_size[0] = 9;
            if hdr.restoration.p[0].restoration_type != 0 {
                if gb.get_bit() != 0 {
                    hdr.restoration.unit_size[0] -= 1;
                } else if hdr.sb128 < 2 && gb.get_bit() == 0 {
                    hdr.restoration.unit_size[0] -= 2 + (hdr.sb128 == 0 && gb.get_bit() == 0) as u8;
                }
            }

            let ss = (seqhdr.layout != PixelLayout::I444) as u8;
            hdr.restoration.unit_size[1] = 9 - ss;
            if hdr.restoration.p[1].restoration_type != 0
                || hdr.restoration.p[2].restoration_type != 0
            {
                if gb.get_bit() != 0 {
                    hdr.restoration.unit_size[1] -= 1;
                } else if hdr.sb128 < 2 && gb.get_bit() == 0 {
                    hdr.restoration.unit_size[1] -= 2 + (hdr.sb128 == 0 && gb.get_bit() == 0) as u8;
                }
                if hdr.restoration.unit_size[1] < 6 - seqhdr.ss_ver {
                    return Err(TealdustError::InvalidFrameHeader);
                }
            }

            // NS wiener filter parsing
            for p in 0..3usize {
                let mut ref_filters = [[0i8; 18]; 48];
                if hdr.restoration.p[p].ns.frame_filters_on == 0 {
                    continue;
                }
                let n_feat = 16 + 2 * (p != 0) as usize;
                let n_ref_filters = if seqhdr.rst_disable_mask[if p != 0 { 1 } else { 0 }] & 1 != 0
                {
                    16
                } else {
                    48 - hdr.restoration.p[p].ns.num_classes as usize
                };

                if hdr.restoration.p[p].ns.temporal != 0 {
                    let ref_idx = *hdr
                        .refidx
                        .get(hdr.restoration.p[p].ns.refidx as usize)
                        .ok_or(TealdustError::InvalidFrameHeader)?;
                    let ref_hdr = ref_slot(refs, ref_idx as i32)?
                        .p
                        .frame_hdr
                        .as_ref()
                        .ok_or(TealdustError::InvalidReferenceFrame)?;
                    let mut rpd = &ref_hdr.restoration.p[p].ns;
                    if rpd.frame_filters_on == 0 {
                        rpd = &ref_hdr.restoration.p[3 - p].ns;
                    }
                    let nc = hdr.restoration.p[p].ns.num_classes as usize;
                    for n in 0..nc {
                        hdr.restoration.p[p].ns.filter[n][..n_feat]
                            .copy_from_slice(&rpd.filter[n][..n_feat]);
                    }
                    continue;
                }

                let mut i = 0usize;
                for r in 0..hdr.n_ref_frames as usize {
                    let ref_hdr = ref_slot(refs, hdr.refidx[r] as i32)?
                        .p
                        .frame_hdr
                        .as_ref()
                        .ok_or(TealdustError::InvalidFrameHeader)?;
                    let dirs: &[i8] = &[0, 1, -1];
                    let mut dir = dirs[if p == 0 {
                        0
                    } else if p == 1 {
                        1
                    } else {
                        2
                    }];
                    let mut p2 = p as i32;
                    loop {
                        let rpd = &ref_hdr.restoration.p[p2 as usize].ns;
                        if rpd.frame_filters_on != 0 {
                            let n_classes =
                                imin(n_ref_filters as i32 - i as i32, rpd.num_classes as i32)
                                    as usize;
                            for n in 0..n_classes {
                                ref_filters[i][..n_feat].copy_from_slice(&rpd.filter[n][..n_feat]);
                                i += 1;
                            }
                        }
                        if dir == 0 {
                            break;
                        }
                        p2 += dir as i32;
                        dir = 0;
                    }
                }

                let n_filters = if seqhdr.rst_disable_mask[if p != 0 { 1 } else { 0 }] & 1 != 0 {
                    16usize
                } else {
                    64
                };
                let n_classes = hdr.restoration.p[p].ns.num_classes as usize;
                let mut grp_cnt = [0u8; 3];
                let mut grp_ref_cnt = [0u8; 3];
                grp_cnt[0] = n_classes as u8;
                grp_cnt[1] = i as u8;
                grp_cnt[2] = (n_filters - n_classes - i) as u8;
                let mut filter_refs = [0u8; 64];
                let mut pred_grp: usize = 2 - (grp_cnt[1] > 2) as usize;
                let nnz_grps = 1 + (grp_cnt[1] != 0) as i32 + (grp_cnt[2] != 0) as i32;
                for n in 0..n_classes {
                    let group = if nnz_grps == 1 || gb.get_bit() == 0 {
                        pred_grp
                    } else if nnz_grps == 2 {
                        2 - (grp_cnt[2] == 0) as usize - pred_grp
                    } else if gb.get_bit() != 0 {
                        2 - (pred_grp == 2) as usize
                    } else {
                        (pred_grp == 0) as usize
                    };
                    grp_ref_cnt[group] += 1;
                    if grp_ref_cnt[group] as usize + (group < pred_grp) as usize
                        > grp_ref_cnt[pred_grp] as usize
                    {
                        pred_grp = group;
                    }
                    let base = grp_cnt[0] as usize * (group != 0) as usize
                        + grp_cnt[1] as usize * (group == 2) as usize;
                    let range = if group != 0 {
                        grp_cnt[group] as u32
                    } else {
                        n as u32 + 1
                    };
                    filter_refs[n] = (base as u32
                        + if range == 1 {
                            0
                        } else {
                            gb.get_bits_subexp_u(range >> 1, range, 4)
                        }) as u8;
                }
                let mut exact_match_mask: u32 = 0;
                for n in 0..n_classes {
                    exact_match_mask |= gb.get_bit() << n;
                }
                let masks: &[u32] = if p != 0 {
                    &SUBSET_MASKS_UV
                } else {
                    &SUBSET_MASKS_Y
                };
                let cf_range: &[[i8; 2]] = if p != 0 {
                    &NS_WIENER_COEF_RANGE_UV
                } else {
                    &NS_WIENER_COEF_RANGE_Y
                };
                static SHUFFLED_INDEX: [u8; 64] = [
                    16, 7, 58, 21, 12, 61, 26, 38, 18, 30, 50, 45, 23, 49, 43, 62, 42, 54, 27, 36,
                    17, 44, 32, 34, 4, 24, 52, 31, 37, 11, 33, 19, 35, 6, 22, 53, 63, 25, 41, 47,
                    1, 59, 0, 28, 40, 55, 48, 8, 5, 51, 9, 46, 56, 60, 15, 2, 13, 14, 57, 29, 3,
                    20, 39, 10,
                ];
                static ZERO: [i8; 18] = [0; 18];
                for n in 0..n_classes {
                    let r = filter_refs[n] as usize;
                    let ref_filter: [i8; 18] = if r == 0 {
                        ZERO
                    } else if r < n_classes {
                        hdr.restoration.p[p].ns.filter[r - 1]
                    } else if r < n_classes + grp_cnt[1] as usize {
                        ref_filters[r - n_classes]
                    } else {
                        let idx = SHUFFLED_INDEX[r - n_classes - grp_cnt[1] as usize] as usize;
                        let mut tmp = [0i8; 18];
                        tmp[..16].copy_from_slice(&WIENER_NS_FILTERS[idx]);
                        tmp
                    };
                    if exact_match_mask & (1 << n) != 0 {
                        hdr.restoration.p[p].ns.filter[n][..n_feat]
                            .copy_from_slice(&ref_filter[..n_feat]);
                        continue;
                    }
                    hdr.restoration.p[p].ns.filter[n][..n_feat].fill(0);
                    let mut s = 0usize;
                    while s < 3 - (p != 0) as usize {
                        if gb.get_bit() == 0 {
                            break;
                        }
                        s += 1;
                    }
                    let mask = masks[s];
                    let mut m = mask;
                    for ii in 0..n_feat {
                        if m & 1 != 0 {
                            let nbits = cf_range[ii][0] as i32;
                            hdr.restoration.p[p].ns.filter[n][ii] = gb.get_bits_subexp_u(
                                (ref_filter[ii] - cf_range[ii][1]) as u32,
                                1 << nbits,
                                nbits - 3,
                            )
                                as i8
                                + cf_range[ii][1];
                        }
                        m >>= 1;
                    }
                }
            }
        }

        // ccso
        if hdr.all_lossless == 0 && seqhdr.ccso {
            hdr.ccso.enabled = (seqhdr.reduced_still_picture_header || gb.get_bit() != 0) as u8;
            if hdr.ccso.enabled != 0 {
                let n_planes = if seqhdr.layout == PixelLayout::I400 {
                    1
                } else {
                    3
                };
                for p in 0..n_planes {
                    hdr.ccso.p[p].enabled = gb.get_bit() as u8;
                    if hdr.ccso.p[p].enabled == 0 {
                        continue;
                    }
                    if hdr.is_inter_or_switch() {
                        hdr.ccso.p[p].reuse = gb.get_bit() as u8;
                        hdr.ccso.p[p].sb_reuse = gb.get_bit() as u8;
                        if hdr.ccso.p[p].reuse != 0 || hdr.ccso.p[p].sb_reuse != 0 {
                            let mut reference = 0u8;
                            if n_bits_ref > 0 {
                                reference = gb.get_bits(n_bits_ref) as u8;
                                hdr.ccso.p[p].refidx = reference;
                                if reference >= hdr.n_ref_frames {
                                    return Err(TealdustError::InvalidFrameHeader);
                                }
                            }
                            let refhdr = ref_slot(refs, hdr.refidx[reference as usize] as i32)?
                                .p
                                .frame_hdr
                                .as_ref()
                                .ok_or(TealdustError::InvalidFrameHeader)?;
                            if hdr.ccso.p[p].reuse != 0 {
                                let w4 = (hdr.width + 3) >> 2;
                                let h4 = (hdr.height + 3) >> 2;
                                let rw4 = (refhdr.width + 3) >> 2;
                                let rh4 = (refhdr.height + 3) >> 2;
                                if w4 != rw4 || h4 != rh4 || refhdr.ccso.p[p].enabled == 0 {
                                    return Err(TealdustError::InvalidFrameHeader);
                                }
                            }
                        }
                    }
                    if hdr.ccso.p[p].reuse == 0 {
                        hdr.ccso.p[p].bo_only = gb.get_bit() as u8;
                        hdr.ccso.p[p].scale_idx = gb.get_bits(2) as u8;
                        if hdr.ccso.p[p].bo_only != 0 {
                            hdr.ccso.p[p].max_band_log2 = gb.get_bits(3) as u8;
                        } else {
                            hdr.ccso.p[p].quant_idx = gb.get_bits(2) as u8;
                            hdr.ccso.p[p].ext_filter_support = gb.get_bits(3) as u8;
                            if hdr.ccso.p[p].ext_filter_support == 7 {
                                return Err(TealdustError::InvalidFrameHeader);
                            }
                            let si = hdr.ccso.p[p].scale_idx as usize;
                            let qi = hdr.ccso.p[p].quant_idx as usize;
                            if CCSO_QUANT_SZ[si][qi] != 0 {
                                hdr.ccso.p[p].edge_clf = gb.get_bit() as u8;
                            }
                            hdr.ccso.p[p].max_band_log2 = gb.get_bits(2) as u8;
                        }
                        let n_edge_off_intervals = if hdr.ccso.p[p].bo_only != 0 {
                            1
                        } else {
                            3 - hdr.ccso.p[p].edge_clf as usize
                        };
                        let max_band = 1usize << hdr.ccso.p[p].max_band_log2;
                        hdr.ccso.p[p].filter_off = [0; 64];
                        for n in 0..n_edge_off_intervals {
                            for m_idx in 0..n_edge_off_intervals {
                                let fo_base = n * 16 + m_idx * 4;
                                for o in 0..max_band {
                                    let mut off = 0u8;
                                    while off < 7 {
                                        if gb.get_bit() == 0 {
                                            break;
                                        }
                                        off += 1;
                                    }
                                    hdr.ccso.p[p].filter_off[fo_base + (o >> 1)] |=
                                        off << (4 * (o & 1));
                                }
                            }
                        }
                    } else {
                        let ccso_ref = *hdr
                            .refidx
                            .get(hdr.ccso.p[p].refidx as usize)
                            .ok_or(TealdustError::InvalidFrameHeader)?;
                        let refhdr = ref_slot(refs, ccso_ref as i32)?
                            .p
                            .frame_hdr
                            .as_ref()
                            .ok_or(TealdustError::InvalidFrameHeader)?;
                        let rp = &refhdr.ccso.p[p];
                        hdr.ccso.p[p].bo_only = rp.bo_only;
                        hdr.ccso.p[p].scale_idx = rp.scale_idx;
                        hdr.ccso.p[p].quant_idx = rp.quant_idx;
                        hdr.ccso.p[p].ext_filter_support = rp.ext_filter_support;
                        hdr.ccso.p[p].edge_clf = rp.edge_clf;
                        hdr.ccso.p[p].max_band_log2 = rp.max_band_log2;
                        hdr.ccso.p[p].filter_off = rp.filter_off;
                    }
                }
            }
        }

        if hdr.all_lossless == 0 {
            hdr.txfm_mode = if gb.get_bit() != 0 {
                TxfmMode::Switchable
            } else {
                TxfmMode::Largest
            };
        }

        // AVM: features.enable_intra_bawp = seq.enable_bawp (sequence-level),
        // set unconditionally and independent of the inter-only frame `bawp` bit.
        hdr.intra_bawp = seqhdr.bawp;
        if hdr.is_inter_or_switch() {
            hdr.switchable_comp_refs = gb.get_bit() as u8;
            hdr.skip_mode_enabled = gb.get_bit() as u8;
            if seqhdr.bawp {
                hdr.bawp = gb.get_bit() as u8;
            }
            if seqhdr.motion_modes & (1 << MotionMode::WarpDelta as u8) != 0 {
                hdr.warp_motion = gb.get_bit() as u8;
            }
        }

        hdr.reduced_txtp_set = gb.get_bits(2) as u8;

        for i in 0..7 {
            hdr.gmv.m[i] = DEFAULT_WM_PARAMS;
        }
        if hdr.is_inter_or_switch() && seqhdr.global_motion && gb.get_bit() != 0 {
            if hdr.n_ref_frames == 0 {
                return Err(TealdustError::InvalidFrameHeader);
            }
            hdr.gmv.reference = gb.get_uniform(hdr.n_ref_frames as u32 + 1) as u8;
            let (ref_base_mat, in_dist);
            if hdr.gmv.reference == hdr.n_ref_frames {
                ref_base_mat = DEFAULT_WM_PARAMS.matrix;
                in_dist = 1;
            } else {
                let refidx = *hdr
                    .refidx
                    .get(hdr.gmv.reference as usize)
                    .ok_or(TealdustError::InvalidFrameHeader)?;
                let refhdr = ref_slot(refs, refidx as i32)?
                    .p
                    .frame_hdr
                    .as_ref()
                    .ok_or(TealdustError::InvalidFrameHeader)?;
                if refhdr.n_ref_frames == 0 {
                    ref_base_mat = DEFAULT_WM_PARAMS.matrix;
                    in_dist = 1;
                } else {
                    hdr.gmv.refref = if refhdr.n_ref_frames == 1 {
                        0
                    } else {
                        gb.get_uniform(refhdr.n_ref_frames as u32) as u8
                    };
                    ref_base_mat = refhdr.gmv.m[hdr.gmv.refref as usize].matrix;
                    in_dist = get_poc_diff(
                        seqhdr.order_hint_n_bits as i32,
                        refhdr.frame_offset as i32,
                        ref_slot(refs, refidx as i32)?.refpoc[hdr.gmv.refref as usize] as i32,
                    );
                }
            }
            for i in 0..hdr.n_ref_frames as usize {
                hdr.gmv.m[i].wm_type = if gb.get_bit() == 0 {
                    WarpedMotionType::Identity
                } else if gb.get_bit() != 0 {
                    WarpedMotionType::RotZoom
                } else {
                    WarpedMotionType::Affine
                };
                if hdr.gmv.m[i].wm_type == WarpedMotionType::Identity {
                    continue;
                }
                let mat = &mut hdr.gmv.m[i].matrix;
                let mut ref_mat = [0i32; 6];
                let out_dist = get_poc_diff(
                    seqhdr.order_hint_n_bits as i32,
                    hdr.frame_offset as i32,
                    ref_slot(refs, hdr.refidx[i] as i32)?
                        .p
                        .frame_hdr
                        .as_ref()
                        .ok_or(TealdustError::InvalidFrameHeader)?
                        .frame_offset as i32,
                );
                rescale_matrix(&mut ref_mat, &ref_base_mat, in_dist, out_dist);

                if hdr.gmv.m[i].wm_type >= WarpedMotionType::RotZoom {
                    mat[2] =
                        (1 << 16) + 64 * gb.get_bits_subexp((ref_mat[2] - (1 << 16)) >> 6, 512);
                    mat[3] = 64 * gb.get_bits_subexp(ref_mat[3] >> 6, 512);
                }
                if hdr.gmv.m[i].wm_type == WarpedMotionType::Affine {
                    mat[4] = 64 * gb.get_bits_subexp(ref_mat[4] >> 6, 512);
                    mat[5] =
                        (1 << 16) + 64 * gb.get_bits_subexp((ref_mat[5] - (1 << 16)) >> 6, 512);
                } else {
                    mat[4] = -mat[3];
                    mat[5] = mat[2];
                }
                mat[0] = gb.get_bits_subexp(ref_mat[0] >> 13, 0x4000) * 8192;
                mat[1] = gb.get_bits_subexp(ref_mat[1] >> 13, 0x4000) * 8192;
            }
        }
    } // end !tip_output_frame

    // grain
    if seqhdr.film_grain_present && (hdr.show_immediate != 0 || hdr.show_implicit != 0) {
        hdr.film_grain.present = (seqhdr.reduced_still_picture_header || gb.get_bit() != 0) as u8;
        if hdr.film_grain.present != 0 {
            hdr.film_grain.id = gb.get_bits(3) as u8;
            hdr.film_grain.seed = gb.get_bits(16);
        }
    }

    if gb.has_error() {
        return Err(TealdustError::InvalidFrameHeader);
    }

    Ok(hdr)
}

pub(crate) fn parse_tile_hdr(
    hdr: &FrameHeader,
    tile: &mut TileGroup,
    gb: &mut GetBits,
) -> Result<()> {
    let n_tiles = hdr.tiling.t.cols as i32 * hdr.tiling.t.rows as i32;
    if n_tiles <= 0 {
        return Err(TealdustError::InvalidTileInfo);
    }
    let have_tile_pos = if n_tiles > 1 {
        gb.get_bit() != 0
    } else {
        false
    };
    if have_tile_pos {
        let n_bits = hdr.tiling.t.log2_cols as i32 + hdr.tiling.t.log2_rows as i32;
        tile.start = gb.get_bits(n_bits) as i32;
        tile.end = gb.get_bits(n_bits) as i32;
    } else {
        tile.start = 0;
        tile.end = n_tiles - 1;
    }

    if tile.start < 0 || tile.end < tile.start || tile.end >= n_tiles {
        return Err(TealdustError::InvalidTileInfo);
    }

    Ok(())
}

pub fn parse_fgm_hdr(
    gb: &mut GetBits,
    seq_layout: PixelLayout,
) -> Result<[Option<FilmGrainData>; 8]> {
    let mask = gb.get_bits(8) as u8;
    let layout_idx = gb.get_vlc();
    if layout_idx > 3 {
        return Err(TealdustError::InvalidFilmGrainData);
    }
    let layout = LAYOUTS[layout_idx as usize];
    if layout != seq_layout {
        return Err(TealdustError::InvalidFilmGrainData);
    }

    let mut result: [Option<FilmGrainData>; 8] = Default::default();
    for idx in 0..8 {
        if mask & (1 << idx) == 0 {
            continue;
        }
        result[idx] = Some(parse_film_grain_data(gb, layout)?);
    }

    if gb.has_error() {
        return Err(TealdustError::InvalidFilmGrainData);
    }

    Ok(result)
}

pub(crate) fn parse_cll(gb: &mut GetBits) -> ContentLightLevel {
    ContentLightLevel {
        max_content_light_level: gb.get_bits(16) as u16,
        max_frame_average_light_level: gb.get_bits(16) as u16,
    }
}

pub(crate) fn parse_mdcv(gb: &mut GetBits) -> MasteringDisplay {
    let mut md = MasteringDisplay::default();
    for i in 0..3 {
        md.primaries[i][0] = gb.get_bits(16) as u16;
        md.primaries[i][1] = gb.get_bits(16) as u16;
    }
    md.white_point[0] = gb.get_bits(16) as u16;
    md.white_point[1] = gb.get_bits(16) as u16;
    md.max_luminance = gb.get_bits(32);
    md.min_luminance = gb.get_bits(32);
    md
}

pub fn parse_ci_hdr(ci: &mut ContentInterpretation, gb: &mut GetBits) -> Result<()> {
    ci.scan_type = match gb.get_bits(2) {
        0 => ScanType::Unknown,
        1 => ScanType::Progressive,
        2 => ScanType::Interlace,
        3 => ScanType::InterlaceComplementary,
        _ => unreachable!(),
    };
    ci.color_description_present = gb.get_bit() != 0;
    ci.chroma_sample_position_present = gb.get_bit() != 0;
    ci.aspect_ratio_info_present = gb.get_bit() != 0;
    ci.timing_info_present = gb.get_bit() != 0;
    ci.extension_present = gb.get_bit() != 0;
    let _ = gb.get_bit(); // reserved

    if ci.color_description_present {
        let desc_type = gb.get_golomb(2);
        ci.color.desc_type = match desc_type {
            0 => ColorDescription::Explicit,
            1 => ColorDescription::Bt709Sdr,
            2 => ColorDescription::Bt2100Pq,
            3 => ColorDescription::Bt2100Hlg,
            4 => ColorDescription::Srgb,
            5 => ColorDescription::SrgbSycc,
            _ => ColorDescription::Explicit, // unknown → treat as explicit with unknown values
        };
        match ci.color.desc_type {
            ColorDescription::Explicit => {
                if desc_type == 0 {
                    ci.color.pri = u8_to_color_pri(gb.get_bits(8) as u8);
                    ci.color.trc = u8_to_trc(gb.get_bits(8) as u8);
                    ci.color.mtrx = u8_to_mc(gb.get_bits(8) as u8);
                } else {
                    ci.color.pri = ColorPrimaries::Unknown;
                    ci.color.trc = TransferCharacteristics::Unknown;
                    ci.color.mtrx = MatrixCoefficients::Unknown;
                }
            }
            ColorDescription::Bt709Sdr => {
                ci.color.pri = ColorPrimaries::Bt709;
                ci.color.trc = TransferCharacteristics::Bt709;
                ci.color.mtrx = MatrixCoefficients::Bt470Bg;
            }
            ColorDescription::Bt2100Pq => {
                ci.color.pri = ColorPrimaries::Bt2020;
                ci.color.trc = TransferCharacteristics::Smpte2084;
                ci.color.mtrx = MatrixCoefficients::Bt2020Ncl;
            }
            ColorDescription::Bt2100Hlg => {
                ci.color.pri = ColorPrimaries::Bt2020;
                ci.color.trc = TransferCharacteristics::Bt2020_10Bit;
                ci.color.mtrx = MatrixCoefficients::Bt2020Ncl;
            }
            ColorDescription::Srgb => {
                ci.color.pri = ColorPrimaries::Bt709;
                ci.color.trc = TransferCharacteristics::Srgb;
                ci.color.mtrx = MatrixCoefficients::Identity;
            }
            ColorDescription::SrgbSycc => {
                ci.color.pri = ColorPrimaries::Bt709;
                ci.color.trc = TransferCharacteristics::Srgb;
                ci.color.mtrx = MatrixCoefficients::Bt470Bg;
            }
        }
        ci.color.range = gb.get_bit() as u8;
    } else {
        ci.color.pri = ColorPrimaries::Unknown;
        ci.color.trc = TransferCharacteristics::Unknown;
        ci.color.mtrx = MatrixCoefficients::Unknown;
    }

    if ci.chroma_sample_position_present {
        ci.chr[0] = u32_to_chr(gb.get_vlc());
        ci.chr[1] = if ci.scan_type == ScanType::Progressive {
            ci.chr[0]
        } else {
            u32_to_chr(gb.get_vlc())
        };
    } else {
        ci.chr[0] = ChromaSamplePosition::Unknown;
        ci.chr[1] = ChromaSamplePosition::Unknown;
    }

    if ci.aspect_ratio_info_present {
        let sar_type = gb.get_bits(8) as u8;
        match sar_type {
            0 => ci.sar.sar_type = AspectRatio::Unknown,
            1 => {
                ci.sar.sar_type = AspectRatio::Sar1_1;
                ci.sar.w = 1;
                ci.sar.h = 1;
            }
            2 => {
                ci.sar.sar_type = AspectRatio::Sar12_11;
                ci.sar.w = 12;
                ci.sar.h = 11;
            }
            3 => {
                ci.sar.sar_type = AspectRatio::Sar10_11;
                ci.sar.w = 10;
                ci.sar.h = 11;
            }
            4 => {
                ci.sar.sar_type = AspectRatio::Sar16_11;
                ci.sar.w = 16;
                ci.sar.h = 11;
            }
            5 => {
                ci.sar.sar_type = AspectRatio::Sar40_33;
                ci.sar.w = 40;
                ci.sar.h = 33;
            }
            6 => {
                ci.sar.sar_type = AspectRatio::Sar24_11;
                ci.sar.w = 24;
                ci.sar.h = 11;
            }
            7 => {
                ci.sar.sar_type = AspectRatio::Sar20_11;
                ci.sar.w = 20;
                ci.sar.h = 11;
            }
            8 => {
                ci.sar.sar_type = AspectRatio::Sar32_11;
                ci.sar.w = 32;
                ci.sar.h = 11;
            }
            9 => {
                ci.sar.sar_type = AspectRatio::Sar80_33;
                ci.sar.w = 80;
                ci.sar.h = 33;
            }
            10 => {
                ci.sar.sar_type = AspectRatio::Sar18_11;
                ci.sar.w = 18;
                ci.sar.h = 11;
            }
            11 => {
                ci.sar.sar_type = AspectRatio::Sar15_11;
                ci.sar.w = 15;
                ci.sar.h = 11;
            }
            12 => {
                ci.sar.sar_type = AspectRatio::Sar64_33;
                ci.sar.w = 64;
                ci.sar.h = 33;
            }
            13 => {
                ci.sar.sar_type = AspectRatio::Sar160_99;
                ci.sar.w = 160;
                ci.sar.h = 99;
            }
            14 => {
                ci.sar.sar_type = AspectRatio::Sar4_3;
                ci.sar.w = 4;
                ci.sar.h = 3;
            }
            15 => {
                ci.sar.sar_type = AspectRatio::Sar3_2;
                ci.sar.w = 3;
                ci.sar.h = 2;
            }
            16 => {
                ci.sar.sar_type = AspectRatio::Sar2_1;
                ci.sar.w = 2;
                ci.sar.h = 1;
            }
            255 => {
                ci.sar.sar_type = AspectRatio::Explicit;
                ci.sar.w = gb.get_vlc();
                ci.sar.h = gb.get_vlc();
            }
            // 17–254 are reserved by the spec; treat as unspecified rather than
            // hard-failing so real-world encoders that emit reserved SAR values
            // still decode successfully.
            _ => {
                ci.sar.sar_type = AspectRatio::Unknown;
            }
        }
    }

    if ci.timing_info_present {
        ci.timing.num_units_in_display_tick = gb.get_bits(32);
        ci.timing.time_scale = gb.get_bits(32);
        if ci.timing.num_units_in_display_tick == 0 || ci.timing.time_scale == 0 {
            return Err(TealdustError::InvalidContentInterpretation);
        }
        ci.timing.equal_elemental_interval = gb.get_bit() as u8;
        if ci.timing.equal_elemental_interval != 0 {
            let t = gb.get_vlc();
            if t == u32::MAX {
                return Err(TealdustError::InvalidContentInterpretation);
            }
            ci.timing.num_ticks_per_elemental_duration = t + 1;
        }
    }

    if gb.has_error() {
        return Err(TealdustError::InvalidContentInterpretation);
    }

    Ok(())
}

fn u8_to_color_pri(v: u8) -> ColorPrimaries {
    match v {
        1 => ColorPrimaries::Bt709,
        2 => ColorPrimaries::Unknown,
        4 => ColorPrimaries::Bt470M,
        5 => ColorPrimaries::Bt470Bg,
        6 => ColorPrimaries::Bt601,
        7 => ColorPrimaries::Smpte240,
        8 => ColorPrimaries::Film,
        9 => ColorPrimaries::Bt2020,
        10 => ColorPrimaries::Xyz,
        11 => ColorPrimaries::Smpte431,
        12 => ColorPrimaries::Smpte432,
        22 => ColorPrimaries::Ebu3213,
        _ => ColorPrimaries::Unknown,
    }
}

fn u8_to_trc(v: u8) -> TransferCharacteristics {
    match v {
        1 => TransferCharacteristics::Bt709,
        2 => TransferCharacteristics::Unknown,
        4 => TransferCharacteristics::Bt470M,
        5 => TransferCharacteristics::Bt470Bg,
        6 => TransferCharacteristics::Bt601,
        7 => TransferCharacteristics::Smpte240,
        8 => TransferCharacteristics::Linear,
        9 => TransferCharacteristics::Log100,
        10 => TransferCharacteristics::Log100Sqrt10,
        11 => TransferCharacteristics::Iec61966,
        12 => TransferCharacteristics::Bt1361,
        13 => TransferCharacteristics::Srgb,
        14 => TransferCharacteristics::Bt2020_10Bit,
        15 => TransferCharacteristics::Bt2020_12Bit,
        16 => TransferCharacteristics::Smpte2084,
        17 => TransferCharacteristics::Smpte428,
        18 => TransferCharacteristics::Hlg,
        _ => TransferCharacteristics::Unknown,
    }
}

fn u8_to_mc(v: u8) -> MatrixCoefficients {
    match v {
        0 => MatrixCoefficients::Identity,
        1 => MatrixCoefficients::Bt709,
        2 => MatrixCoefficients::Unknown,
        4 => MatrixCoefficients::Fcc,
        5 => MatrixCoefficients::Bt470Bg,
        6 => MatrixCoefficients::Bt601,
        7 => MatrixCoefficients::Smpte240,
        8 => MatrixCoefficients::YCgCo,
        9 => MatrixCoefficients::Bt2020Ncl,
        10 => MatrixCoefficients::Bt2020Cl,
        11 => MatrixCoefficients::Smpte2085,
        12 => MatrixCoefficients::ChromatNcl,
        13 => MatrixCoefficients::ChromatCl,
        14 => MatrixCoefficients::Ictcp,
        15 => MatrixCoefficients::IptC2,
        16 => MatrixCoefficients::YcgcoRe,
        17 => MatrixCoefficients::YcgcoRo,
        _ => MatrixCoefficients::Unknown,
    }
}

fn u32_to_chr(v: u32) -> ChromaSamplePosition {
    match v {
        0 => ChromaSamplePosition::Left,
        1 => ChromaSamplePosition::Center,
        2 => ChromaSamplePosition::TopLeft,
        3 => ChromaSamplePosition::Top,
        4 => ChromaSamplePosition::BottomLeft,
        5 => ChromaSamplePosition::Bottom,
        _ => ChromaSamplePosition::Unknown,
    }
}

pub fn read_frame_size(
    hdr: &mut FrameHeader,
    seqhdr: &SequenceHeader,
    refs: &[RefState; 8],
    gb: &mut GetBits,
) -> Result<()> {
    if hdr.frame_size_override != 0 && hdr.is_inter_or_switch() {
        for i in 0..hdr.n_ref_frames as usize {
            if gb.get_bit() != 0 {
                let refhdr = ref_slot(refs, hdr.refidx[i] as i32)?
                    .p
                    .frame_hdr
                    .as_ref()
                    .ok_or(TealdustError::InvalidReferenceFrame)?;
                hdr.width = refhdr.width;
                hdr.height = refhdr.height;
                return Ok(());
            }
        }
    }
    if hdr.frame_size_override != 0 {
        hdr.width = gb.get_bits(seqhdr.width_n_bits as i32) as i32 + 1;
        hdr.height = gb.get_bits(seqhdr.height_n_bits as i32) as i32 + 1;
        // The field width (ceil(log2(max))) can encode a value past the
        // sequence maximum; the spec requires width <= max_width and
        // height <= max_height. Reject otherwise so an over-large dimension
        // cannot flow into downstream buffer sizing.
        if hdr.width > seqhdr.max_width || hdr.height > seqhdr.max_height {
            return Err(TealdustError::InvalidFrameHeader);
        }
    } else {
        hdr.width = seqhdr.max_width;
        hdr.height = seqhdr.max_height;
    }
    Ok(())
}

pub fn get_ref_frames(
    hdr: &mut FrameHeader,
    seqhdr: &SequenceHeader,
    refs: &[RefState; 8],
    have_resolution: bool,
) -> i32 {
    struct Score {
        score: i32,
        poc: u8,
        pocdiff: i8,
        qidx: u16,
        mlayer: u8,
    }
    let mut ref_info: [Score; 8] = std::array::from_fn(|_| Score {
        score: 0,
        poc: 0,
        pocdiff: 0,
        qidx: 0,
        mlayer: 0,
    });
    let mut sort_idx = [0u8; 8];
    let mut n_refs = 0i32;
    let mut have_fwd_refs = false;
    let poc = hdr.frame_offset as i32;
    let nbits = seqhdr.order_hint_n_bits as i32;

    for n in 0..8 {
        if have_fwd_refs {
            break;
        }
        if let Some(refhdr) = refs[n].p.frame_hdr.as_ref() {
            have_fwd_refs = get_poc_diff(nbits, poc, refhdr.frame_offset as i32) < 0;
        }
    }

    let mlayer = hdr.mlayer_id as i32;
    let tlayer = hdr.tlayer_id as i32;
    let w = hdr.width;
    let h = hdr.height;
    let mut minq = 512i32;
    let mut maxq = -1i32;
    let mut last_refhdr_ptr: Option<*const FrameHeader> = None;

    for n in 0..8usize {
        let refhdr_arc = match refs[n].p.frame_hdr.as_ref() {
            Some(fh) => fh,
            None => continue,
        };
        let refhdr_ptr = Arc::as_ptr(refhdr_arc);
        if last_refhdr_ptr == Some(refhdr_ptr) {
            continue;
        }
        let refhdr = refhdr_arc.as_ref();

        if seqhdr.tlayer_dependency_present {
            if seqhdr.tlayer_dependencies[tlayer as usize] & (1 << refhdr.tlayer_id) == 0 {
                continue;
            }
        } else if tlayer < refhdr.tlayer_id as i32 {
            continue;
        }

        let ref_mlayer = refhdr.mlayer_id;
        if seqhdr.mlayer_dependency_present {
            if seqhdr.mlayer_dependencies[mlayer as usize] & (1 << ref_mlayer) == 0 {
                continue;
            }
        } else if mlayer < ref_mlayer as i32 {
            continue;
        }

        if have_resolution
            && (2 * w < refhdr.width
                || 2 * h < refhdr.height
                || w > 16 * refhdr.width
                || h > 16 * refhdr.height)
        {
            continue;
        }

        let ref_poc = refhdr.frame_offset;
        let pocdiff = get_poc_diff(nbits, poc, ref_poc as i32) as i8;
        let ref_qidx = refhdr.quant.yac;
        let res_ratio = -(ulog2((refhdr.width * refhdr.height) as u32) as i8);
        let tdist = (pocdiff as i32).abs() + mlayer - ref_mlayer as i32;
        let mut score = if have_fwd_refs {
            tdist << 6
        } else {
            128 - (128 >> imin(tdist, 6)) + imax(tdist - 6, 0)
        };
        score += res_ratio as i32 * (1 << 5) + ref_qidx as i32;

        ref_info[n] = Score {
            score,
            poc: ref_poc,
            pocdiff,
            qidx: ref_qidx,
            mlayer: ref_mlayer,
        };

        let mut m = 0usize;
        while m < n_refs as usize {
            let r2 = &ref_info[sort_idx[m] as usize];
            if score == r2.score && ref_poc == r2.poc && ref_mlayer == r2.mlayer {
                break;
            }
            m += 1;
        }
        if (m as i32) < n_refs {
            continue;
        }

        maxq = imax(ref_qidx as i32, maxq);
        minq = imin(ref_qidx as i32, minq);

        while m > 0 {
            let idx = sort_idx[m - 1] as usize;
            if ref_info[idx].score <= score {
                break;
            }
            sort_idx[m] = idx as u8;
            m -= 1;
        }
        sort_idx[m] = n as u8;
        n_refs += 1;
        last_refhdr_ptr = Some(refhdr_ptr);
    }

    if n_refs == 8 {
        let q_thr = (maxq + minq + 1) >> 1;
        let mut maxpocdiff = [0i32; 2];
        let mut num = [0i32; 2];
        let mut furthest_idx = [0usize; 2];
        for n in 0..8usize {
            let r = &ref_info[sort_idx[n] as usize];
            if (r.qidx as i32) < q_thr {
                continue;
            }
            if r.pocdiff > 0 {
                if (r.pocdiff as i32) > maxpocdiff[0] {
                    maxpocdiff[0] = r.pocdiff as i32;
                    furthest_idx[0] = n;
                }
                num[0] += 1;
            } else if r.pocdiff < 0 {
                if (r.pocdiff as i32) < maxpocdiff[1] {
                    maxpocdiff[1] = r.pocdiff as i32;
                    furthest_idx[1] = n;
                }
                num[1] += 1;
            }
        }
        let idx = if num[0] > num[1] {
            furthest_idx[0]
        } else if num[0] < num[1] {
            furthest_idx[1]
        } else {
            furthest_idx[if maxpocdiff[0] < -maxpocdiff[1] { 1 } else { 0 }]
        };
        if idx < 7 {
            sort_idx.copy_within(idx + 1..8, idx);
            sort_idx[7] = idx as u8;
        }
    }

    for n in 0..7usize {
        hdr.refidx[n] = sort_idx[if (n as i32) < n_refs { n } else { 0 }] as i8;
    }

    imin(7, n_refs)
}

pub fn find_tip_ref_frames(
    hdr: &mut FrameHeader,
    seqhdr: &SequenceHeader,
    refs: &[RefState; 8],
) -> Result<()> {
    let n_refs = hdr.n_ref_frames as usize;
    // n_refs >= 2 is required to pick two TIP references; the index arithmetic
    // below underflows for 0 and is degenerate for 1.
    if n_refs < 2 {
        hdr.tip.reference[0] = 0;
        hdr.tip.reference[1] = 0;
        return Ok(());
    }

    let poc = hdr.frame_offset as i32;
    let nbits = seqhdr.order_hint_n_bits as i32;
    let mut order = [0u8; 7];
    let mut refdist = [0i8; 7];
    let mut n_past = 0usize;

    for n in 0..n_refs {
        let refpoc = ref_slot(refs, hdr.refidx[n] as i32)?
            .p
            .frame_hdr
            .as_ref()
            .ok_or(TealdustError::InvalidReferenceFrame)?
            .frame_offset;
        let dist = get_poc_diff(nbits, refpoc as i32, poc);
        refdist[n] = dist as i8;
        let mut m = n;
        while m > 0 && (refdist[order[m - 1] as usize] as i32) > dist {
            order[m] = order[m - 1];
            m -= 1;
        }
        order[m] = n as u8;
        if dist < 0 {
            n_past += 1;
        }
    }

    if n_past == n_refs {
        hdr.tip.reference[0] = order[n_refs - 1] as i8;
        hdr.tip.reference[1] = order[n_refs - 2] as i8;
    } else if n_past == 0 {
        hdr.tip.reference[0] = order[0] as i8;
        hdr.tip.reference[1] = order[1] as i8;
    } else {
        hdr.tip.reference[0] = order[n_past - 1] as i8;
        hdr.tip.reference[1] = order[n_past] as i8;
    }
    Ok(())
}

pub fn derive_pri_sec_ref(
    hdr: &FrameHeader,
    seqhdr: &SequenceHeader,
    refs: &[RefState; 8],
) -> [i32; 2] {
    let mut result = [PRIMARY_REF_NONE as i32, PRIMARY_REF_NONE as i32];
    let mut best_qdiff = [0i32; 2];
    let mut best_pocdiff = [0i32; 2];
    let mut best_poc = [0i32; 2];
    let mut best = 0usize;
    let qidx = hdr.quant.yac as i32;
    let poc = hdr.frame_offset as i32;
    let nbits = seqhdr.order_hint_n_bits as i32;

    for i in 0..hdr.n_ref_frames as usize {
        let refhdr = match ref_slot(refs, hdr.refidx[i] as i32)
            .ok()
            .and_then(|r| r.p.frame_hdr.as_ref())
        {
            Some(fh) => fh,
            None => continue,
        };
        if refhdr.is_key_or_intra() {
            continue;
        }
        let ref_qidx = refhdr.quant.yac as i32;
        let qdiff = (ref_qidx - qidx).abs();
        let ref_poc = refhdr.frame_offset as i32;
        let pocdiff = get_poc_diff(nbits, poc, ref_poc).abs();
        for n in 0..2usize {
            let m = if n == 0 { best } else { 1 - best };
            if result[m] == PRIMARY_REF_NONE as i32
                || qdiff < best_qdiff[m]
                || (qdiff == best_qdiff[m]
                    && (pocdiff < best_pocdiff[m]
                        || (pocdiff == best_pocdiff[m]
                            && get_poc_diff(nbits, best_poc[m], ref_poc) < 0)))
            {
                let slot = 1 - best;
                result[slot] = i as i32;
                best_pocdiff[slot] = pocdiff;
                best_qdiff[slot] = qdiff;
                best_poc[slot] = ref_poc;
                if n == 0 {
                    best = 1 - best;
                }
                break;
            }
        }
    }

    if best != 0 {
        result.swap(0, 1);
    }
    result
}

fn u32_to_obu_type(v: u32) -> Option<ObuType> {
    match v {
        1 => Some(ObuType::SeqHdr),
        2 => Some(ObuType::Td),
        3 => Some(ObuType::MultiFrameHdr),
        4 => Some(ObuType::ClosedLoopKf),
        5 => Some(ObuType::OpenLoopKf),
        6 => Some(ObuType::LeadingTileGrp),
        7 => Some(ObuType::TileGrp),
        8 => Some(ObuType::Metadata),
        9 => Some(ObuType::MetadataGrp),
        10 => Some(ObuType::Switch),
        11 => Some(ObuType::LeadingSef),
        12 => Some(ObuType::Sef),
        13 => Some(ObuType::LeadingTip),
        14 => Some(ObuType::Tip),
        15 => Some(ObuType::BufRmTiming),
        16 => Some(ObuType::LayerCfgRec),
        17 => Some(ObuType::AtlasSeg),
        18 => Some(ObuType::OpPtSet),
        19 => Some(ObuType::Bridge),
        20 => Some(ObuType::Msdo),
        21 => Some(ObuType::Ras),
        22 => Some(ObuType::Qm),
        23 => Some(ObuType::Fgm),
        24 => Some(ObuType::ContentInterp),
        25 => Some(ObuType::Padding),
        _ => None,
    }
}

pub fn parse_obus(c: &mut DecoderContext, data: &[u8]) -> Result<usize> {
    use crate::levels::ObuMetaType;

    let mut hdr_gb = GetBits::new(data);

    let len = hdr_gb.get_uleb128() as usize;
    hdr_gb.bytealign();
    if hdr_gb.has_error() || len > hdr_gb.remaining_bytes() {
        return Err(TealdustError::InvalidObu);
    }
    let body_start = hdr_gb.byte_pos();
    let total_consumed = body_start + len;

    let body = &data[body_start..body_start + len];
    let mut gb = GetBits::new(if body.is_empty() { &[0u8] } else { body });

    let has_extension = gb.get_bit() != 0;
    let obu_type_raw = gb.get_bits(5);
    let tlayer_id = gb.get_bits(2) as i32;

    let mut mlayer_id = 0i32;
    let mut xlayer_id = 0i32;
    if has_extension {
        mlayer_id = gb.get_bits(3) as i32;
        xlayer_id = gb.get_bits(5) as i32;
    }

    if gb.has_error() {
        return Err(TealdustError::InvalidObu);
    }

    let obu_type = u32_to_obu_type(obu_type_raw);

    // skip OBUs not belonging to selected operating point
    if obu_type != Some(ObuType::SeqHdr)
        && obu_type != Some(ObuType::Td)
        && has_extension
        && c.operating_point_idc != 0
    {
        return Ok(total_consumed);
    }

    match obu_type {
        Some(ObuType::SeqHdr) => {
            let seq_hdr = parse_seq_hdr(&mut gb, c.strict_std_compliance)?;

            c.operating_point_idc = 0;
            let spatial_mask = c.operating_point_idc >> 8;
            c.max_spatial_id = if spatial_mask != 0 {
                ulog2(spatial_mask)
            } else {
                0
            };

            if c.seq_hdr.is_none() {
                c.frame_hdr = None;
            } else if c.seq_hdr.as_ref().is_none_or(|old| **old != seq_hdr) {
                c.frame_hdr = None;
                c.content_light = None;
                c.mastering_display = None;
                for i in 0..8 {
                    c.refs[i] = RefState::default();
                    c.fgm[i] = None;
                }
                c.ci = None;
            }
            c.seq_hdr = Some(Arc::new(seq_hdr));
        }

        Some(
            ObuType::OpenLoopKf
            | ObuType::ClosedLoopKf
            | ObuType::LeadingTileGrp
            | ObuType::TileGrp
            | ObuType::Switch
            | ObuType::LeadingSef
            | ObuType::Sef
            | ObuType::LeadingTip
            | ObuType::Tip
            | ObuType::Bridge
            | ObuType::Ras,
        ) => {
            let obu_type = obu_type.unwrap();
            let seqhdr = c
                .seq_hdr
                .as_ref()
                .ok_or(TealdustError::MissingSequenceHeader)?
                .clone();

            let first_tile = matches!(obu_type, ObuType::Sef | ObuType::Tip | ObuType::Bridge)
                || gb.get_bit() != 0;
            let has_hdr = first_tile || gb.get_bit() != 0;

            if has_hdr {
                let mut hdr = parse_frame_hdr(&seqhdr, &c.refs, obu_type, &mut gb)?;
                hdr.tlayer_id = tlayer_id as u8;
                hdr.mlayer_id = mlayer_id as u8;
                hdr.xlayer_id = xlayer_id as u8;
                c.frame_hdr = Some(Arc::new(hdr));
            }

            c.tile.clear();
            c.n_tile_data = 0;
            c.n_tiles = 0;

            if matches!(obu_type, ObuType::Sef | ObuType::Tip | ObuType::Bridge) {
                check_trailing_bits(&mut gb, c.strict_std_compliance)?;
            }

            if let Some(ref fh) = c.frame_hdr {
                if c.frame_size_limit > 0
                    && (fh.width as u64) * (fh.height as u64) > c.frame_size_limit as u64
                {
                    c.frame_hdr = None;
                    return Err(TealdustError::FrameTooLarge);
                }
            }

            if matches!(obu_type, ObuType::Sef | ObuType::Tip | ObuType::Bridge) {
                // frame header only OBU, no tile data
            } else {
                let fh = c
                    .frame_hdr
                    .as_ref()
                    .ok_or(TealdustError::MissingFrameHeader)?
                    .clone();
                let mut tg = TileGroup {
                    data: Vec::new(),
                    start: 0,
                    end: 0,
                };
                parse_tile_hdr(&fh, &mut tg, &mut gb)?;
                gb.bytealign();
                if gb.has_error() {
                    return Err(TealdustError::InvalidTileInfo);
                }
                let rem = gb.remaining_slice();
                tg.data
                    .try_reserve_exact(rem.len())
                    .map_err(|_| TealdustError::OutOfMemory)?;
                tg.data.extend_from_slice(rem);

                if tg.start > tg.end || tg.start != c.n_tiles {
                    c.tile.clear();
                    c.n_tile_data = 0;
                    c.n_tiles = 0;
                    return Err(TealdustError::InvalidTileInfo);
                }
                c.n_tiles += 1 + tg.end - tg.start;
                c.tile
                    .try_reserve_exact(1)
                    .map_err(|_| TealdustError::OutOfMemory)?;
                c.tile.push(tg);
                c.n_tile_data += 1;
            }
        }

        Some(ObuType::Fgm) => {
            let seqhdr = c
                .seq_hdr
                .as_ref()
                .ok_or(TealdustError::MissingSequenceHeader)?
                .clone();
            let fgm = parse_fgm_hdr(&mut gb, seqhdr.layout)?;
            for (i, entry) in fgm.into_iter().enumerate() {
                if entry.is_some() {
                    c.fgm[i] = entry;
                }
            }
            check_trailing_bits(&mut gb, c.strict_std_compliance)?;
        }

        Some(ObuType::ContentInterp) => {
            let mut ci = ContentInterpretation::default();
            parse_ci_hdr(&mut ci, &mut gb)?;
            check_trailing_bits(&mut gb, c.strict_std_compliance)?;
            c.ci = Some(ci);
        }

        Some(ObuType::Metadata) => {
            let meta_type = gb.get_uleb128();
            if gb.has_error() {
                return Err(TealdustError::InvalidObu);
            }
            match meta_type {
                v if v == ObuMetaType::HdrCll as u32 => {
                    let cll = parse_cll(&mut gb);
                    check_trailing_bits(&mut gb, c.strict_std_compliance)?;
                    c.content_light = Some(cll);
                }
                v if v == ObuMetaType::HdrMdcv as u32 => {
                    let md = parse_mdcv(&mut gb);
                    check_trailing_bits(&mut gb, c.strict_std_compliance)?;
                    c.mastering_display = Some(md);
                }
                _ => {} // ignore unknown metadata
            }
        }

        Some(ObuType::Td) => {
            // temporal delimiter — no action needed
        }

        Some(ObuType::Padding) => {
            // ignore
        }

        _ => {
            // unknown OBU type — ignore
        }
    }

    // post-processing: check if frame is ready to submit
    if c.seq_hdr.is_some() && c.frame_hdr.is_some() {
        let fh = c.frame_hdr.as_ref().unwrap().clone();
        if fh.show_existing_frame != 0 {
            let idx = fh.existing_frame_idx as usize;
            if c.refs[idx].p.frame_hdr.is_none() {
                return Err(TealdustError::InvalidReferenceFrame);
            }
            // (`c->refs[idx].p.p.data[0]`) to be present before it can be queued.
            if !c.refs[idx].p.pic.as_ref().is_some_and(|p| p.has_data()) {
                return Err(TealdustError::InvalidReferenceFrame);
            }
            if c.strict_std_compliance && !c.refs[idx].p.showable {
                return Err(TealdustError::InvalidReferenceFrame);
            }
            let trigger = c.refs[idx].p.clone();
            // A shown key frame refreshes every other slot: output the frames
            // waiting there first (AVM `update_frame_buffers` order).
            if c.refs[idx]
                .p
                .frame_hdr
                .as_ref()
                .is_some_and(|h| h.frame_type == FrameType::Key)
            {
                let r = idx;
                for i in 0..8 {
                    if i != r {
                        crate::output_order::output_before_overwrite(c, i);
                    }
                }
                crate::output_order::output_frame_buffers(c, &trigger);
                c.refs[r].p.showable = false;
                for i in 0..8 {
                    if i == r {
                        continue;
                    }
                    c.refs[i].p = c.refs[r].p.clone();
                    c.refs[i].cdf = c.refs[r].cdf.clone();
                    c.refs[i].segmap = c.refs[r].segmap.clone();
                    // motion field for the overwritten slot (no inc back).
                    c.refs[i].refmvs = None;
                }
            } else {
                crate::output_order::output_frame_buffers(c, &trigger);
            }
            c.frame_hdr = None;
        } else {
            let total_tiles = fh.tiling.t.cols as i32 * fh.tiling.t.rows as i32;
            let frame_without_data = fh.tip.frame_mode == 2;
            if c.n_tiles == total_tiles || frame_without_data {
                if !frame_without_data && c.n_tile_data == 0 {
                    return Err(TealdustError::InvalidTileData);
                }
                // Run the single-threaded frame decode (entropy pass; recon,
                // filters and output are wired in subsequent milestones). Gated
                // during bring-up; errors are non-fatal so header parsing still
                // succeeds.
                if c.run_decode {
                    // Best-effort during bring-up: a frame that cannot be decoded
                    // yet (e.g. inter frames) must not abort header parsing.
                    let n_tc = c.n_tc as i32;
                    crate::decode::submit_frame(c, n_tc)?;
                }
                c.frame_hdr = None;
                c.n_tiles = 0;
            }
        }
    }

    Ok(total_consumed)
}