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
|
/*
* VVC CABAC decoder
*
* Copyright (C) 2021 Nuo Mi
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavcodec/cabac_functions.h"
#include "cabac.h"
#include "ctu.h"
#include "data.h"
#define CABAC_MAX_BIN 31
#define CNU 35
enum SyntaxElement {
ALF_CTB_FLAG = 0,
ALF_USE_APS_FLAG = ALF_CTB_FLAG + 9,
ALF_CTB_CC_CB_IDC,
ALF_CTB_CC_CR_IDC = ALF_CTB_CC_CB_IDC + 3,
ALF_CTB_FILTER_ALT_IDX = ALF_CTB_CC_CR_IDC + 3,
SAO_MERGE_FLAG = ALF_CTB_FILTER_ALT_IDX + 2,
SAO_TYPE_IDX,
SPLIT_CU_FLAG,
SPLIT_QT_FLAG = SPLIT_CU_FLAG + 9,
MTT_SPLIT_CU_VERTICAL_FLAG = SPLIT_QT_FLAG + 6,
MTT_SPLIT_CU_BINARY_FLAG = MTT_SPLIT_CU_VERTICAL_FLAG + 5,
NON_INTER_FLAG = MTT_SPLIT_CU_BINARY_FLAG + 4,
CU_SKIP_FLAG = NON_INTER_FLAG + 2,
PRED_MODE_IBC_FLAG = CU_SKIP_FLAG + 3,
PRED_MODE_FLAG = PRED_MODE_IBC_FLAG + 3,
PRED_MODE_PLT_FLAG = PRED_MODE_FLAG + 2,
CU_ACT_ENABLED_FLAG,
INTRA_BDPCM_LUMA_FLAG,
INTRA_BDPCM_LUMA_DIR_FLAG,
INTRA_MIP_FLAG,
INTRA_LUMA_REF_IDX = INTRA_MIP_FLAG + 4,
INTRA_SUBPARTITIONS_MODE_FLAG = INTRA_LUMA_REF_IDX + 2,
INTRA_SUBPARTITIONS_SPLIT_FLAG,
INTRA_LUMA_MPM_FLAG,
INTRA_LUMA_NOT_PLANAR_FLAG,
INTRA_BDPCM_CHROMA_FLAG = INTRA_LUMA_NOT_PLANAR_FLAG + 2,
INTRA_BDPCM_CHROMA_DIR_FLAG,
CCLM_MODE_FLAG,
CCLM_MODE_IDX,
INTRA_CHROMA_PRED_MODE,
GENERAL_MERGE_FLAG,
INTER_PRED_IDC,
INTER_AFFINE_FLAG = INTER_PRED_IDC + 6,
CU_AFFINE_TYPE_FLAG = INTER_AFFINE_FLAG + 3,
SYM_MVD_FLAG,
REF_IDX_LX,
MVP_LX_FLAG = REF_IDX_LX + 2,
AMVR_FLAG,
AMVR_PRECISION_IDX = AMVR_FLAG + 2,
BCW_IDX = AMVR_PRECISION_IDX + 3,
CU_CODED_FLAG,
CU_SBT_FLAG,
CU_SBT_QUAD_FLAG = CU_SBT_FLAG + 2,
CU_SBT_HORIZONTAL_FLAG,
CU_SBT_POS_FLAG = CU_SBT_HORIZONTAL_FLAG + 3,
LFNST_IDX,
MTS_IDX = LFNST_IDX + 3,
COPY_ABOVE_PALETTE_INDICES_FLAG = MTS_IDX + 4,
PALETTE_TRANSPOSE_FLAG,
RUN_COPY_FLAG,
REGULAR_MERGE_FLAG = RUN_COPY_FLAG + 8,
MMVD_MERGE_FLAG = REGULAR_MERGE_FLAG + 2,
MMVD_CAND_FLAG,
MMVD_DISTANCE_IDX,
CIIP_FLAG,
MERGE_SUBBLOCK_FLAG,
MERGE_SUBBLOCK_IDX = MERGE_SUBBLOCK_FLAG + 3,
MERGE_IDX,
ABS_MVD_GREATER0_FLAG,
ABS_MVD_GREATER1_FLAG,
TU_Y_CODED_FLAG,
TU_CB_CODED_FLAG = TU_Y_CODED_FLAG + 4,
TU_CR_CODED_FLAG = TU_CB_CODED_FLAG + 2,
CU_QP_DELTA_ABS = TU_CR_CODED_FLAG + 3,
CU_CHROMA_QP_OFFSET_FLAG = CU_QP_DELTA_ABS + 2,
CU_CHROMA_QP_OFFSET_IDX,
TRANSFORM_SKIP_FLAG,
TU_JOINT_CBCR_RESIDUAL_FLAG = TRANSFORM_SKIP_FLAG + 2,
LAST_SIG_COEFF_X_PREFIX = TU_JOINT_CBCR_RESIDUAL_FLAG + 3,
LAST_SIG_COEFF_Y_PREFIX = LAST_SIG_COEFF_X_PREFIX +23,
SB_CODED_FLAG = LAST_SIG_COEFF_Y_PREFIX +23,
SIG_COEFF_FLAG = SB_CODED_FLAG + 7,
PAR_LEVEL_FLAG = SIG_COEFF_FLAG +63,
ABS_LEVEL_GTX_FLAG = PAR_LEVEL_FLAG +33,
COEFF_SIGN_FLAG = ABS_LEVEL_GTX_FLAG +72,
SYNTAX_ELEMENT_LAST = COEFF_SIGN_FLAG + 6,
};
static const uint8_t init_values[4][SYNTAX_ELEMENT_LAST] = {
{
//alf_ctb_flag
62, 39, 39, 54, 39, 39, 31, 39, 39,
//alf_use_aps_flag
46,
//alf_ctb_cc_cb_idc
18, 30, 31,
//alf_ctb_cc_cr_idc
18, 30, 31,
//alf_ctb_filter_alt_idx
11, 11,
//sao_merge_left_flag and sao_merge_up_flag
60,
//sao_type_idx_luma and sao_type_idx_chroma
13,
//split_cu_flag
19, 28, 38, 27, 29, 38, 20, 30, 31,
//split_qt_flag
27, 6, 15, 25, 19, 37,
//mtt_split_cu_vertical_flag
43, 42, 29, 27, 44,
//mtt_split_cu_binary_flag
36, 45, 36, 45,
//non_inter_flag
CNU, CNU,
//cu_skip_flag
0, 26, 28,
//pred_mode_ibc_flag
17, 42, 36,
//pred_mode_flag
CNU, CNU,
//pred_mode_plt_flag
25,
//cu_act_enabled_flag
52,
//intra_bdpcm_luma_flag
19,
//intra_bdpcm_luma_dir_flag
35,
//intra_mip_flag
33, 49, 50, 25,
//intra_luma_ref_idx
25, 60,
//intra_subpartitions_mode_flag
33,
//intra_subpartitions_split_flag
43,
//intra_luma_mpm_flag
45,
//intra_luma_not_planar_flag
13, 28,
//intra_bdpcm_chroma_flag
1,
//intra_bdpcm_chroma_dir_flag
27,
//cclm_mode_flag
59,
//cclm_mode_idx
27,
//intra_chroma_pred_mode
34,
//general_merge_flag
26,
//inter_pred_idc
CNU, CNU, CNU, CNU, CNU, CNU,
//inter_affine_flag
CNU, CNU, CNU,
//cu_affine_type_flag
CNU,
//sym_mvd_flag
CNU,
//ref_idx_l0 and ref_idx_l1
CNU, CNU,
//mvp_l0_flag and mvp_l1_flag
42,
//amvr_flag
CNU, CNU,
//amvr_precision_idx
35, 34, 35,
//bcw_idx
CNU,
//cu_coded_flag
6,
//cu_sbt_flag
CNU, CNU,
//cu_sbt_quad_flag
CNU,
//cu_sbt_horizontal_flag
CNU, CNU, CNU,
//cu_sbt_pos_flag
CNU,
//lfnst_idx
28, 52, 42,
//mts_idx
29, 0, 28, 0,
//copy_above_palette_indices_flag
42,
//palette_transpose_flag
42,
//run_copy_flag
50, 37, 45, 30, 46, 45, 38, 46,
//regular_merge_flag
CNU, CNU,
//mmvd_merge_flag
CNU,
//mmvd_cand_flag
CNU,
//mmvd_distance_idx
CNU,
//ciip_flag
CNU,
//merge_subblock_flag
CNU, CNU, CNU,
//merge_subblock_idx
CNU,
//merge_idx, merge_gpm_idx0, and merge_gpm_idx1
34,
//abs_mvd_greater0_flag
14,
//abs_mvd_greater1_flag
45,
//tu_y_coded_flag
15, 12, 5, 7,
//tu_cb_coded_flag
12, 21,
//tu_cr_coded_flag
33, 28, 36,
//cu_qp_delta_abs
CNU, CNU,
//cu_chroma_qp_offset_flag
CNU,
//cu_chroma_qp_offset_idx
CNU,
//transform_skip_flag
25, 9,
//tu_joint_cbcr_residual_flag
12, 21, 35,
//last_sig_coeff_x_prefix
13, 5, 4, 21, 14, 4, 6, 14, 21, 11, 14, 7, 14, 5, 11, 21,
30, 22, 13, 42, 12, 4, 3,
//last_sig_coeff_y_prefix
13, 5, 4, 6, 13, 11, 14, 6, 5, 3, 14, 22, 6, 4, 3, 6,
22, 29, 20, 34, 12, 4, 3,
//sb_coded_flag
18, 31, 25, 15, 18, 20, 38,
//sig_coeff_flag
25, 19, 28, 14, 25, 20, 29, 30, 19, 37, 30, 38, 11, 38, 46, 54,
27, 39, 39, 39, 44, 39, 39, 39, 18, 39, 39, 39, 27, 39, 39, 39,
0, 39, 39, 39, 25, 27, 28, 37, 34, 53, 53, 46, 19, 46, 38, 39,
52, 39, 39, 39, 11, 39, 39, 39, 19, 39, 39, 39, 25, 28, 38,
//par_level_flag
33, 25, 18, 26, 34, 27, 25, 26, 19, 42, 35, 33, 19, 27, 35, 35,
34, 42, 20, 43, 20, 33, 25, 26, 42, 19, 27, 26, 50, 35, 20, 43,
11,
//abs_level_gtx_flag
25, 25, 11, 27, 20, 21, 33, 12, 28, 21, 22, 34, 28, 29, 29, 30,
36, 29, 45, 30, 23, 40, 33, 27, 28, 21, 37, 36, 37, 45, 38, 46,
25, 1, 40, 25, 33, 11, 17, 25, 25, 18, 4, 17, 33, 26, 19, 13,
33, 19, 20, 28, 22, 40, 9, 25, 18, 26, 35, 25, 26, 35, 28, 37,
11, 5, 5, 14, 10, 3, 3, 3,
//coeff_sign_flag
12, 17, 46, 28, 25, 46,
},
{
//alf_ctb_flag
13, 23, 46, 4, 61, 54, 19, 46, 54,
//alf_use_aps_flag
46,
//alf_ctb_cc_cb_idc
18, 21, 38,
//alf_ctb_cc_cr_idc
18, 21, 38,
//alf_ctb_filter_alt_idx
20, 12,
//sao_merge_left_flag and sao_merge_up_flag
60,
//sao_type_idx_luma and sao_type_idx_chroma
5,
//split_cu_flag
11, 35, 53, 12, 6, 30, 13, 15, 31,
//split_qt_flag
20, 14, 23, 18, 19, 6,
//mtt_split_cu_vertical_flag
43, 35, 37, 34, 52,
//mtt_split_cu_binary_flag
43, 37, 21, 22,
//non_inter_flag
25, 12,
//cu_skip_flag
57, 59, 45,
//pred_mode_ibc_flag
0, 57, 44,
//pred_mode_flag
40, 35,
//pred_mode_plt_flag
0,
//cu_act_enabled_flag
46,
//intra_bdpcm_luma_flag
40,
//intra_bdpcm_luma_dir_flag
36,
//intra_mip_flag
41, 57, 58, 26,
//intra_luma_ref_idx
25, 58,
//intra_subpartitions_mode_flag
33,
//intra_subpartitions_split_flag
36,
//intra_luma_mpm_flag
36,
//intra_luma_not_planar_flag
12, 20,
//intra_bdpcm_chroma_flag
0,
//intra_bdpcm_chroma_dir_flag
13,
//cclm_mode_flag
34,
//cclm_mode_idx
27,
//intra_chroma_pred_mode
25,
//general_merge_flag
21,
//inter_pred_idc
7, 6, 5, 12, 4, 40,
//inter_affine_flag
12, 13, 14,
//cu_affine_type_flag
35,
//sym_mvd_flag
28,
//ref_idx_l0 and ref_idx_l1
20, 35,
//mvp_l0_flag and mvp_l1_flag
34,
//amvr_flag
59, 58,
//amvr_precision_idx
60, 48, 60,
//bcw_idx
4,
//cu_coded_flag
5,
//cu_sbt_flag
56, 57,
//cu_sbt_quad_flag
42,
//cu_sbt_horizontal_flag
20, 43, 12,
//cu_sbt_pos_flag
28,
//lfnst_idx
37, 45, 27,
//mts_idx
45, 40, 27, 0,
//copy_above_palette_indices_flag
59,
//palette_transpose_flag
42,
//run_copy_flag
51, 30, 30, 38, 23, 38, 53, 46,
//regular_merge_flag
38, 7,
//mmvd_merge_flag
26,
//mmvd_cand_flag
43,
//mmvd_distance_idx
60,
//ciip_flag
57,
//merge_subblock_flag
48, 57, 44,
//merge_subblock_idx
5,
//merge_idx, merge_gpm_idx0, and merge_gpm_idx1
20,
//abs_mvd_greater0_flag
44,
//abs_mvd_greater1_flag
43,
//tu_y_coded_flag
23, 5, 20, 7,
//tu_cb_coded_flag
25, 28,
//tu_cr_coded_flag
25, 29, 45,
//cu_qp_delta_abs
CNU, CNU,
//cu_chroma_qp_offset_flag
CNU,
//cu_chroma_qp_offset_idx
CNU,
//transform_skip_flag
25, 9,
//tu_joint_cbcr_residual_flag
27, 36, 45,
//last_sig_coeff_x_prefix
6, 13, 12, 6, 6, 12, 14, 14, 13, 12, 29, 7, 6, 13, 36, 28,
14, 13, 5, 26, 12, 4, 18,
//last_sig_coeff_y_prefix
5, 5, 12, 6, 6, 4, 6, 14, 5, 12, 14, 7, 13, 5, 13, 21,
14, 20, 12, 34, 11, 4, 18,
//sb_coded_flag
25, 30, 25, 45, 18, 12, 29,
//sig_coeff_flag
17, 41, 42, 29, 25, 49, 43, 37, 33, 58, 51, 30, 19, 38, 38, 46,
34, 54, 54, 39, 6, 39, 39, 39, 19, 39, 54, 39, 19, 39, 39, 39,
56, 39, 39, 39, 17, 34, 35, 21, 41, 59, 60, 38, 35, 45, 53, 54,
44, 39, 39, 39, 34, 38, 62, 39, 26, 39, 39, 39, 40, 35, 44,
//par_level_flag
18, 17, 33, 18, 26, 42, 25, 33, 26, 42, 27, 25, 34, 42, 42, 35,
26, 27, 42, 20, 20, 25, 25, 26, 11, 19, 27, 33, 42, 35, 35, 43,
3,
//abs_level_gtx_flag
0, 17, 26, 19, 35, 21, 25, 34, 20, 28, 29, 33, 27, 28, 29, 22,
34, 28, 44, 37, 38, 0, 25, 19, 20, 13, 14, 57, 44, 30, 30, 23,
17, 0, 1, 17, 25, 18, 0, 9, 25, 33, 34, 9, 25, 18, 26, 20,
25, 18, 19, 27, 29, 17, 9, 25, 10, 18, 4, 17, 33, 19, 20, 29,
18, 11, 4, 28, 2, 10, 3, 3,
//coeff_sign_flag
5, 10, 53, 43, 25, 46,
},
{
//alf_ctb_flag
33, 52, 46, 25, 61, 54, 25, 61, 54,
//alf_use_aps_flag
46,
//alf_ctb_cc_cb_idc
25, 35, 38,
//alf_ctb_cc_cr_idc
25, 28, 38,
//alf_ctb_filter_alt_idx
11, 26,
//sao_merge_left_flag and sao_merge_up_flag
2,
//sao_type_idx_luma and sao_type_idx_chroma
2,
//split_cu_flag
18, 27, 15, 18, 28, 45, 26, 7, 23,
//split_qt_flag
26, 36, 38, 18, 34, 21,
//mtt_split_cu_vertical_flag
43, 42, 37, 42, 44,
//mtt_split_cu_binary_flag
28, 29, 28, 29,
//non_inter_flag
25, 20,
//cu_skip_flag
57, 60, 46,
//pred_mode_ibc_flag
0, 43, 45,
//pred_mode_flag
40, 35,
//pred_mode_plt_flag
17,
//cu_act_enabled_flag
46,
//intra_bdpcm_luma_flag
19,
//intra_bdpcm_luma_dir_flag
21,
//intra_mip_flag
56, 57, 50, 26,
//intra_luma_ref_idx
25, 59,
//intra_subpartitions_mode_flag
33,
//intra_subpartitions_split_flag
43,
//intra_luma_mpm_flag
44,
//intra_luma_not_planar_flag
13, 6,
//intra_bdpcm_chroma_flag
0,
//intra_bdpcm_chroma_dir_flag
28,
//cclm_mode_flag
26,
//cclm_mode_idx
27,
//intra_chroma_pred_mode
25,
//general_merge_flag
6,
//inter_pred_idc
14, 13, 5, 4, 3, 40,
//inter_affine_flag
19, 13, 6,
//cu_affine_type_flag
35,
//sym_mvd_flag
28,
//ref_idx_l0 and ref_idx_l1
5, 35,
//mvp_l0_flag and mvp_l1_flag
34,
//amvr_flag
59, 50,
//amvr_precision_idx
38, 26, 60,
//bcw_idx
5,
//cu_coded_flag
12,
//cu_sbt_flag
41, 57,
//cu_sbt_quad_flag
42,
//cu_sbt_horizontal_flag
35, 51, 27,
//cu_sbt_pos_flag
28,
//lfnst_idx
52, 37, 27,
//mts_idx
45, 25, 27, 0,
//copy_above_palette_indices_flag
50,
//palette_transpose_flag
35,
//run_copy_flag
58, 45, 45, 30, 38, 45, 38, 46,
//regular_merge_flag
46, 15,
//mmvd_merge_flag
25,
//mmvd_cand_flag
43,
//mmvd_distance_idx
59,
//ciip_flag
57,
//merge_subblock_flag
25, 58, 45,
//merge_subblock_idx
4,
//merge_idx, merge_gpm_idx0, and merge_gpm_idx1
18,
//abs_mvd_greater0_flag
51,
//abs_mvd_greater1_flag
36,
//tu_y_coded_flag
15, 6, 5, 14,
//tu_cb_coded_flag
25, 37,
//tu_cr_coded_flag
9, 36, 45,
//cu_qp_delta_abs
CNU, CNU,
//cu_chroma_qp_offset_flag
CNU,
//cu_chroma_qp_offset_idx
CNU,
//transform_skip_flag
25, 17,
//tu_joint_cbcr_residual_flag
42, 43, 52,
//last_sig_coeff_x_prefix
6, 6, 12, 14, 6, 4, 14, 7, 6, 4, 29, 7, 6, 6, 12, 28,
7, 13, 13, 35, 19, 5, 4,
//last_sig_coeff_y_prefix
5, 5, 20, 13, 13, 19, 21, 6, 12, 12, 14, 14, 5, 4, 12, 13,
7, 13, 12, 41, 11, 5, 27,
//sb_coded_flag
25, 45, 25, 14, 18, 35, 45,
//sig_coeff_flag
17, 41, 49, 36, 1, 49, 50, 37, 48, 51, 58, 45, 26, 45, 53, 46,
49, 54, 61, 39, 35, 39, 39, 39, 19, 54, 39, 39, 50, 39, 39, 39,
0, 39, 39, 39, 9, 49, 50, 36, 48, 59, 59, 38, 34, 45, 38, 31,
58, 39, 39, 39, 34, 38, 54, 39, 41, 39, 39, 39, 25, 50, 37,
//par_level_flag
33, 40, 25, 41, 26, 42, 25, 33, 26, 34, 27, 25, 41, 42, 42, 35,
33, 27, 35, 42, 43, 33, 25, 26, 34, 19, 27, 33, 42, 43, 35, 43,
11,
//abs_level_gtx_flag
0, 0, 33, 34, 35, 21, 25, 34, 35, 28, 29, 40, 42, 43, 29, 30,
49, 36, 37, 45, 38, 0, 40, 34, 43, 36, 37, 57, 52, 45, 38, 46,
25, 0, 0, 17, 25, 26, 0, 9, 25, 33, 19, 0, 25, 33, 26, 20,
25, 33, 27, 35, 22, 25, 1, 25, 33, 26, 12, 25, 33, 27, 28, 37,
19, 11, 4, 6, 3, 4, 4, 5,
//coeff_sign_flag
35, 25, 46, 28, 33, 38,
},
//shiftIdx
{
//alf_ctb_flag
0, 0, 0, 4, 0, 0, 1, 0, 0,
//alf_use_aps_flag
0,
//alf_ctb_cc_cb_idc
4, 1, 4,
//alf_ctb_cc_cr_idc
4, 1, 4,
//alf_ctb_filter_alt_idx
0, 0,
//sao_merge_left_flag and sao_merge_up_flag
0,
//sao_type_idx_luma and sao_type_idx_chroma
4,
//split_cu_flag
12, 13, 8, 8, 13, 12, 5, 9, 9,
//split_qt_flag
0, 8, 8, 12, 12, 8,
//mtt_split_cu_vertical_flag
9, 8, 9, 8, 5,
//mtt_split_cu_binary_flag
12, 13, 12, 13,
//non_inter_flag
1, 0,
//cu_skip_flag
5, 4, 8,
//pred_mode_ibc_flag
1, 5, 8,
//pred_mode_flag
5, 1,
//pred_mode_plt_flag
1,
//cu_act_enabled_flag
1,
//intra_bdpcm_luma_flag
1,
//intra_bdpcm_luma_dir_flag
4,
//intra_mip_flag
9, 10, 9, 6,
//intra_luma_ref_idx
5, 8,
//intra_subpartitions_mode_flag
9,
//intra_subpartitions_split_flag
2,
//intra_luma_mpm_flag
6,
//intra_luma_not_planar_flag
1, 5,
//intra_bdpcm_chroma_flag
1,
//intra_bdpcm_chroma_dir_flag
0,
//cclm_mode_flag
4,
//cclm_mode_idx
9,
//intra_chroma_pred_mode
5,
//general_merge_flag
4,
//inter_pred_idc
0, 0, 1, 4, 4, 0,
//inter_affine_flag
4, 0, 0,
//cu_affine_type_flag
4,
//sym_mvd_flag
5,
//ref_idx_l0 and ref_idx_l1
0, 4,
//mvp_l0_flag and mvp_l1_flag
12,
//amvr_flag
0, 0,
//amvr_precision_idx
4, 5, 0,
//bcw_idx
1,
//cu_coded_flag
4,
//cu_sbt_flag
1, 5,
//cu_sbt_quad_flag
10,
//cu_sbt_horizontal_flag
8, 4, 1,
//cu_sbt_pos_flag
13,
//lfnst_idx
9, 9, 10,
//mts_idx
8, 0, 9, 0,
//copy_above_palette_indices_flag
9,
//palette_transpose_flag
5,
//run_copy_flag
9, 6, 9, 10, 5, 0, 9, 5,
//regular_merge_flag
5, 5,
//mmvd_merge_flag
4,
//mmvd_cand_flag
10,
//mmvd_distance_idx
0,
//ciip_flag
1,
//merge_subblock_flag
4, 4, 4,
//merge_subblock_idx
0,
//merge_idx, merge_gpm_idx0, and merge_gpm_idx1
4,
//abs_mvd_greater0_flag
9,
//abs_mvd_greater1_flag
5,
//tu_y_coded_flag
5, 1, 8, 9,
//tu_cb_coded_flag
5, 0,
//tu_cr_coded_flag
2, 1, 0,
//cu_qp_delta_abs
8, 8,
//cu_chroma_qp_offset_flag
8,
//cu_chroma_qp_offset_idx
8,
//transform_skip_flag
1, 1,
//tu_joint_cbcr_residual_flag
1, 1, 0,
//last_sig_coeff_x_prefix
8, 5, 4, 5, 4, 4, 5, 4, 1, 0, 4, 1, 0, 0, 0, 0,
1, 0, 0, 0, 5, 4, 4,
//last_sig_coeff_y_prefix
8, 5, 8, 5, 5, 4, 5, 5, 4, 0, 5, 4, 1, 0, 0, 1,
4, 0, 0, 0, 6, 5, 5,
//sb_coded_flag
8, 5, 5, 8, 5, 8, 8,
//sig_coeff_flag
12, 9, 9, 10, 9, 9, 9, 10, 8, 8, 8, 10, 9, 13, 8, 8,
8, 8, 8, 5, 8, 0, 0, 0, 8, 8, 8, 8, 8, 0, 4, 4,
0, 0, 0, 0, 12, 12, 9, 13, 4, 5, 8, 9, 8, 12, 12, 8,
4, 0, 0, 0, 8, 8, 8, 8, 4, 0, 0, 0, 13, 13, 8,
//par_level_flag
8, 9, 12, 13, 13, 13, 10, 13, 13, 13, 13, 13, 13, 13, 13, 13,
10, 13, 13, 13, 13, 8, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13,
6,
//abs_level_gtx_flag
9, 5, 10, 13, 13, 10, 9, 10, 13, 13, 13, 9, 10, 10, 10, 13,
8, 9, 10, 10, 13, 8, 8, 9, 12, 12, 10, 5, 9, 9, 9, 13,
1, 5, 9, 9, 9, 6, 5, 9, 10, 10, 9, 9, 9, 9, 9, 9,
6, 8, 9, 9, 10, 1, 5, 8, 8, 9, 6, 6, 9, 8, 8, 9,
4, 2, 1, 6, 1, 1, 1, 1,
//coeff_sign_flag
1, 4, 4, 5, 8, 8,
}
};
#define MAX_SUB_BLOCKS 16
#define MAX_SUB_BLOCK_SIZE 4
#define MAX_TB_SIZE 64
typedef struct ResidualCoding {
//common for ts and non ts
TransformBlock *tb;
int log2_sb_w;
int log2_sb_h;
int last_sub_block;
int hist_value;
int update_hist;
int num_sb_coeff;
int rem_bins_pass1;
int width_in_sbs;
int height_in_sbs;
int nb_sbs;
const uint8_t *sb_scan_x_off;
const uint8_t *sb_scan_y_off;
const uint8_t *scan_x_off;
const uint8_t *scan_y_off;
uint8_t sb_coded_flag[MAX_SUB_BLOCKS * MAX_SUB_BLOCKS];
int sig_coeff_flag[MAX_TB_SIZE * MAX_TB_SIZE];
int abs_level_pass1[MAX_TB_SIZE * MAX_TB_SIZE]; ///< AbsLevelPass1[][]
int abs_level[MAX_TB_SIZE * MAX_TB_SIZE];
//for ts only
uint8_t infer_sb_cbf;
int coeff_sign_level[MAX_TB_SIZE * MAX_TB_SIZE]; ///< CoeffSignLevel[][]
//for non ts only
int qstate;
int last_scan_pos;
int last_significant_coeff_x;
int last_significant_coeff_y;
} ResidualCoding;
static int cabac_reinit(VVCLocalContext *lc)
{
return skip_bytes(&lc->ep->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0;
}
static void cabac_init_state(VVCLocalContext *lc)
{
const VVCSPS *sps = lc->fc->ps.sps;
const H266RawSliceHeader *rsh = lc->sc->sh.r;
const int qp = av_clip_uintp2(lc->sc->sh.slice_qp_y, 6);
int init_type = 2 - rsh->sh_slice_type;
av_assert0(VVC_CONTEXTS == SYNTAX_ELEMENT_LAST);
ff_vvc_ep_init_stat_coeff(lc->ep, sps->bit_depth, sps->r->sps_persistent_rice_adaptation_enabled_flag);
if (rsh->sh_cabac_init_flag && !IS_I(rsh))
init_type ^= 3;
for (int i = 0; i < VVC_CONTEXTS; i++) {
VVCCabacState *state = &lc->ep->cabac_state[i];
const int init_value = init_values[init_type][i];
const int shift_idx = init_values[3][i];
const int m = (init_value >> 3) - 4;
const int n = ((init_value & 7) * 18) + 1;
const int pre = av_clip(((m * (qp - 16)) >> 1) + n, 1, 127);
state->state[0] = pre << 3;
state->state[1] = pre << 7;
state->shift[0] = (shift_idx >> 2 ) + 2;
state->shift[1] = (shift_idx & 3 ) + 3 + state->shift[0];
}
}
int ff_vvc_cabac_init(VVCLocalContext *lc,
const int ctu_idx, const int rx, const int ry)
{
int ret = 0;
const VVCPPS *pps = lc->fc->ps.pps;
const int first_ctb_in_slice = !ctu_idx;
const int first_ctb_in_tile = rx == pps->ctb_to_col_bd[rx] && ry == pps->ctb_to_row_bd[ry];
if (first_ctb_in_slice|| first_ctb_in_tile) {
if (lc->sc->nb_eps == 1 && !first_ctb_in_slice)
ret = cabac_reinit(lc);
if (!ret)
cabac_init_state(lc);
}
return ret;
}
//fixme
static void vvc_refill2(CABACContext* c) {
int i;
unsigned x;
#if !HAVE_FAST_CLZ
x = c->low ^ (c->low - 1);
i = 7 - ff_h264_norm_shift[x >> (CABAC_BITS - 1)];
#else
i = ff_ctz(c->low) - CABAC_BITS;
#endif
x = -CABAC_MASK;
#if CABAC_BITS == 16
x += (c->bytestream[0] << 9) + (c->bytestream[1] << 1);
#else
x += c->bytestream[0] << 1;
#endif
c->low += x << i;
#if !UNCHECKED_BITSTREAM_READER
if (c->bytestream < c->bytestream_end)
#endif
c->bytestream += CABAC_BITS / 8;
}
static int inline vvc_get_cabac(CABACContext *c, VVCCabacState* base, const int ctx)
{
VVCCabacState *s = base + ctx;
const int qRangeIdx = c->range >> 5;
const int pState = s->state[1] + (s->state[0] << 4);
const int valMps = pState >> 14;
const int RangeLPS = (qRangeIdx * ((valMps ? 32767 - pState : pState) >> 9 ) >> 1) + 4;
int bit, lps_mask;
c->range -= RangeLPS;
lps_mask = ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
c->range += (RangeLPS - c->range) & lps_mask;
bit = valMps ^ (lps_mask & 1);
lps_mask = ff_h264_norm_shift[c->range];
c->range <<= lps_mask;
c->low <<= lps_mask;
if (!(c->low & CABAC_MASK))
vvc_refill2(c);
s->state[0] = s->state[0] - (s->state[0] >> s->shift[0]) + (1023 * bit >> s->shift[0]);
s->state[1] = s->state[1] - (s->state[1] >> s->shift[1]) + (16383 * bit >> s->shift[1]);
return bit;
}
#define GET_CABAC(ctx) vvc_get_cabac(&lc->ep->cc, lc->ep->cabac_state, ctx)
//9.3.3.4 Truncated binary (TB) binarization process
static int truncated_binary_decode(VVCLocalContext *lc, const int c_max)
{
const int n = c_max + 1;
const int k = av_log2(n);
const int u = (1 << (k+1)) - n;
int v = 0;
for (int i = 0; i < k; i++)
v = (v << 1) | get_cabac_bypass(&lc->ep->cc);
if (v >= u) {
v = (v << 1) | get_cabac_bypass(&lc->ep->cc);
v -= u;
}
return v;
}
// 9.3.3.6 Limited k-th order Exp-Golomb binarization process
static int limited_kth_order_egk_decode(CABACContext *c, const int k, const int max_pre_ext_len, const int trunc_suffix_len)
{
int pre_ext_len = 0;
int escape_length;
int val = 0;
while ((pre_ext_len < max_pre_ext_len) && get_cabac_bypass(c))
pre_ext_len++;
if (pre_ext_len == max_pre_ext_len)
escape_length = trunc_suffix_len;
else
escape_length = pre_ext_len + k;
while (escape_length-- > 0) {
val = (val << 1) + get_cabac_bypass(c);
}
val += ((1 << pre_ext_len) - 1) << k;
return val;
}
static av_always_inline
void get_left_top(const VVCLocalContext *lc, uint8_t *left, uint8_t *top,
const int x0, const int y0, const uint8_t *left_ctx, const uint8_t *top_ctx)
{
const VVCFrameContext *fc = lc->fc;
const VVCSPS *sps = fc->ps.sps;
const int min_cb_width = fc->ps.pps->min_cb_width;
const int x0b = av_mod_uintp2(x0, sps->ctb_log2_size_y);
const int y0b = av_mod_uintp2(y0, sps->ctb_log2_size_y);
const int x_cb = x0 >> sps->min_cb_log2_size_y;
const int y_cb = y0 >> sps->min_cb_log2_size_y;
if (lc->ctb_left_flag || x0b)
*left = SAMPLE_CTB(left_ctx, x_cb - 1, y_cb);
if (lc->ctb_up_flag || y0b)
*top = SAMPLE_CTB(top_ctx, x_cb, y_cb - 1);
}
static av_always_inline
uint8_t get_inc(VVCLocalContext *lc, const uint8_t *ctx)
{
uint8_t left = 0, top = 0;
get_left_top(lc, &left, &top, lc->cu->x0, lc->cu->y0, ctx, ctx);
return left + top;
}
int ff_vvc_sao_merge_flag_decode(VVCLocalContext *lc)
{
return GET_CABAC(SAO_MERGE_FLAG);
}
int ff_vvc_sao_type_idx_decode(VVCLocalContext *lc)
{
if (!GET_CABAC(SAO_TYPE_IDX))
return SAO_NOT_APPLIED;
if (!get_cabac_bypass(&lc->ep->cc))
return SAO_BAND;
return SAO_EDGE;
}
int ff_vvc_sao_band_position_decode(VVCLocalContext *lc)
{
int value = get_cabac_bypass(&lc->ep->cc);
for (int i = 0; i < 4; i++)
value = (value << 1) | get_cabac_bypass(&lc->ep->cc);
return value;
}
int ff_vvc_sao_offset_abs_decode(VVCLocalContext *lc)
{
int i = 0;
const int length = (1 << (FFMIN(lc->fc->ps.sps->bit_depth, 10) - 5)) - 1;
while (i < length && get_cabac_bypass(&lc->ep->cc))
i++;
return i;
}
int ff_vvc_sao_offset_sign_decode(VVCLocalContext *lc)
{
return get_cabac_bypass(&lc->ep->cc);
}
int ff_vvc_sao_eo_class_decode(VVCLocalContext *lc)
{
int ret = get_cabac_bypass(&lc->ep->cc) << 1;
ret |= get_cabac_bypass(&lc->ep->cc);
return ret;
}
int ff_vvc_alf_ctb_flag(VVCLocalContext *lc, const int rx, const int ry, const int c_idx)
{
int inc = c_idx * 3;
const VVCFrameContext *fc = lc->fc;
if (lc->ctb_left_flag) {
const ALFParams *left = &CTB(fc->tab.alf, rx - 1, ry);
inc += left->ctb_flag[c_idx];
}
if (lc->ctb_up_flag) {
const ALFParams *above = &CTB(fc->tab.alf, rx, ry - 1);
inc += above->ctb_flag[c_idx];
}
return GET_CABAC(ALF_CTB_FLAG + inc);
}
int ff_vvc_alf_use_aps_flag(VVCLocalContext *lc)
{
return GET_CABAC(ALF_USE_APS_FLAG);
}
int ff_vvc_alf_luma_prev_filter_idx(VVCLocalContext *lc)
{
return truncated_binary_decode(lc, lc->sc->sh.r->sh_num_alf_aps_ids_luma - 1);
}
int ff_vvc_alf_luma_fixed_filter_idx(VVCLocalContext *lc)
{
return truncated_binary_decode(lc, 15);
}
int ff_vvc_alf_ctb_filter_alt_idx(VVCLocalContext *lc, const int c_idx, const int num_chroma_filters)
{
int i = 0;
const int length = num_chroma_filters - 1;
while (i < length && GET_CABAC(ALF_CTB_FILTER_ALT_IDX + c_idx - 1))
i++;
return i;
}
int ff_vvc_alf_ctb_cc_idc(VVCLocalContext *lc, const int rx, const int ry, const int idx, const int cc_filters_signalled)
{
int inc = !idx ? ALF_CTB_CC_CB_IDC : ALF_CTB_CC_CR_IDC;
int i = 0;
const VVCFrameContext *fc = lc->fc;
if (lc->ctb_left_flag) {
const ALFParams *left = &CTB(fc->tab.alf, rx - 1, ry);
inc += left->ctb_cc_idc[idx] != 0;
}
if (lc->ctb_up_flag) {
const ALFParams *above = &CTB(fc->tab.alf, rx, ry - 1);
inc += above->ctb_cc_idc[idx] != 0;
}
if (!GET_CABAC(inc))
return 0;
i++;
while (i < cc_filters_signalled && get_cabac_bypass(&lc->ep->cc))
i++;
return i;
}
int ff_vvc_split_cu_flag(VVCLocalContext *lc, const int x0, const int y0,
const int cb_width, const int cb_height, const int is_chroma, const VVCAllowedSplit *a)
{
const VVCFrameContext *fc = lc->fc;
const VVCPPS *pps = fc->ps.pps;
const int is_inside = (x0 + cb_width <= pps->width) && (y0 + cb_height <= pps->height);
if ((a->btv || a->bth || a->ttv || a->tth || a->qt) && is_inside)
{
uint8_t inc = 0, left_height = cb_height, top_width = cb_width;
get_left_top(lc, &left_height, &top_width, x0, y0, fc->tab.cb_height[is_chroma], fc->tab.cb_width[is_chroma]);
inc += left_height < cb_height;
inc += top_width < cb_width;
inc += (a->btv + a->bth + a->ttv + a->tth + 2 * a->qt - 1) / 2 * 3;
return GET_CABAC(SPLIT_CU_FLAG + inc);
}
return !is_inside;
}
static int split_qt_flag_decode(VVCLocalContext *lc, const int x0, const int y0, const int ch_type, const int cqt_depth)
{
const VVCFrameContext *fc = lc->fc;
int inc = 0;
uint8_t depth_left = 0, depth_top = 0;
get_left_top(lc, &depth_left, &depth_top, x0, y0, fc->tab.cqt_depth[ch_type], fc->tab.cqt_depth[ch_type]);
inc += depth_left > cqt_depth;
inc += depth_top > cqt_depth;
inc += (cqt_depth >= 2) * 3;
return GET_CABAC(SPLIT_QT_FLAG + inc);
}
static int mtt_split_cu_vertical_flag_decode(VVCLocalContext *lc, const int x0, const int y0,
const int cb_width, const int cb_height, const int ch_type, const VVCAllowedSplit* a)
{
if ((a->bth || a->tth) && (a->btv || a->ttv)) {
int inc;
const int v = a->btv + a->ttv;
const int h = a->bth + a->tth;
if (v > h)
inc = 4;
else if (v < h)
inc = 3;
else {
const VVCFrameContext *fc = lc->fc;
const VVCSPS *sps = fc->ps.sps;
const int min_cb_width = fc->ps.pps->min_cb_width;
const int x0b = av_mod_uintp2(x0, sps->ctb_log2_size_y);
const int y0b = av_mod_uintp2(y0, sps->ctb_log2_size_y);
const int x_cb = x0 >> sps->min_cb_log2_size_y;
const int y_cb = y0 >> sps->min_cb_log2_size_y;
const int available_a = lc->ctb_up_flag || y0b;
const int available_l = lc->ctb_left_flag || x0b;
const int da = cb_width / (available_a ? SAMPLE_CTB(fc->tab.cb_width[ch_type], x_cb, y_cb - 1) : 1);
const int dl = cb_height / (available_l ? SAMPLE_CTB(fc->tab.cb_height[ch_type], x_cb - 1, y_cb) : 1);
if (da == dl || !available_a || !available_l)
inc = 0;
else if (da < dl)
inc = 1;
else
inc = 2;
}
return GET_CABAC(MTT_SPLIT_CU_VERTICAL_FLAG + inc);
}
return !(a->bth || a->tth);
}
static int mtt_split_cu_binary_flag_decode(VVCLocalContext *lc, const int mtt_split_cu_vertical_flag, const int mtt_depth)
{
const int inc = (2 * mtt_split_cu_vertical_flag) + ((mtt_depth <= 1) ? 1 : 0);
return GET_CABAC(MTT_SPLIT_CU_BINARY_FLAG + inc);
}
VVCSplitMode ff_vvc_split_mode(VVCLocalContext *lc, const int x0, const int y0, const int cb_width, const int cb_height,
const int cqt_depth, const int mtt_depth, const int ch_type, const VVCAllowedSplit *a)
{
const int allow_no_qt = a->btv || a->bth || a->ttv || a->tth;
int split_qt_flag;
int mtt_split_cu_vertical_flag;
int mtt_split_cu_binary_flag;
const VVCSplitMode mtt_split_modes[] = {
SPLIT_TT_HOR, SPLIT_BT_HOR, SPLIT_TT_VER, SPLIT_BT_VER,
};
if (allow_no_qt && a->qt) {
split_qt_flag = split_qt_flag_decode(lc, x0, y0, ch_type, cqt_depth);
} else {
split_qt_flag = !allow_no_qt || a->qt;
}
if (split_qt_flag)
return SPLIT_QT;
mtt_split_cu_vertical_flag = mtt_split_cu_vertical_flag_decode(lc, x0, y0, cb_width, cb_height, ch_type, a);
if ((a->btv && a->ttv && mtt_split_cu_vertical_flag) ||
(a->bth && a->tth && !mtt_split_cu_vertical_flag)) {
mtt_split_cu_binary_flag = mtt_split_cu_binary_flag_decode(lc, mtt_split_cu_vertical_flag, mtt_depth);
} else {
if (!a->btv && !a->bth)
mtt_split_cu_binary_flag = 0;
else if (!a->ttv && !a->tth)
mtt_split_cu_binary_flag = 1;
else if (a->bth && a->ttv)
mtt_split_cu_binary_flag = 1 - mtt_split_cu_vertical_flag;
else
mtt_split_cu_binary_flag = mtt_split_cu_vertical_flag;
}
return mtt_split_modes[(mtt_split_cu_vertical_flag << 1) + mtt_split_cu_binary_flag];
}
int ff_vvc_non_inter_flag(VVCLocalContext *lc, const int x0, const int y0, const int ch_type)
{
const VVCFrameContext *fc = lc->fc;
uint8_t inc, left = MODE_INTER, top = MODE_INTER;
get_left_top(lc, &left, &top, x0, y0, fc->tab.cpm[ch_type], fc->tab.cpm[ch_type]);
inc = left == MODE_INTRA || top == MODE_INTRA;
return GET_CABAC(NON_INTER_FLAG + inc);
}
int ff_vvc_pred_mode_flag(VVCLocalContext *lc, const int is_chroma)
{
const VVCFrameContext *fc = lc->fc;
const CodingUnit *cu = lc->cu;
uint8_t inc, left = MODE_INTER, top = MODE_INTER;
get_left_top(lc, &left, &top, cu->x0, cu->y0, fc->tab.cpm[is_chroma], fc->tab.cpm[is_chroma]);
inc = left == MODE_INTRA || top == MODE_INTRA;
return GET_CABAC(PRED_MODE_FLAG + inc);
}
int ff_vvc_pred_mode_plt_flag(VVCLocalContext *lc)
{
return GET_CABAC(PRED_MODE_PLT_FLAG);
}
int ff_vvc_intra_bdpcm_luma_flag(VVCLocalContext *lc)
{
return GET_CABAC(INTRA_BDPCM_LUMA_FLAG);
}
int ff_vvc_intra_bdpcm_luma_dir_flag(VVCLocalContext *lc)
{
return GET_CABAC(INTRA_BDPCM_LUMA_DIR_FLAG);
}
int ff_vvc_intra_bdpcm_chroma_flag(VVCLocalContext *lc)
{
return GET_CABAC(INTRA_BDPCM_CHROMA_FLAG);
}
int ff_vvc_intra_bdpcm_chroma_dir_flag(VVCLocalContext *lc)
{
return GET_CABAC(INTRA_BDPCM_CHROMA_DIR_FLAG);
}
int ff_vvc_cu_skip_flag(VVCLocalContext *lc, const uint8_t *cu_skip_flag)
{
const int inc = get_inc(lc, cu_skip_flag);
return GET_CABAC(CU_SKIP_FLAG + inc);
}
int ff_vvc_pred_mode_ibc_flag(VVCLocalContext *lc, const int is_chroma)
{
const VVCFrameContext *fc = lc->fc;
const CodingUnit *cu = lc->cu;
uint8_t left_mode = MODE_INTER, top_mode = MODE_INTER;
int inc;
get_left_top(lc, &left_mode, &top_mode, cu->x0, cu->y0, fc->tab.cpm[is_chroma], fc->tab.cpm[is_chroma]);
inc = (left_mode == MODE_IBC) + (top_mode == MODE_IBC);
return GET_CABAC(PRED_MODE_IBC_FLAG + inc);
}
int ff_vvc_intra_mip_flag(VVCLocalContext *lc, const uint8_t *intra_mip_flag)
{
const int w = lc->cu->cb_width;
const int h = lc->cu->cb_height;
const int inc = (w > h * 2 || h > w * 2) ? 3 : get_inc(lc, intra_mip_flag);
return GET_CABAC(INTRA_MIP_FLAG + inc);
}
int ff_vvc_intra_mip_transposed_flag(VVCLocalContext *lc)
{
return get_cabac_bypass(&lc->ep->cc);
}
int ff_vvc_intra_mip_mode(VVCLocalContext *lc)
{
const int w = lc->cu->cb_width;
const int h = lc->cu->cb_height;
const int c_max = (w == 4 && h == 4) ? 15 :
((w == 4 || h == 4) || (w == 8 && h == 8)) ? 7: 5;
return truncated_binary_decode(lc, c_max);
}
int ff_vvc_intra_luma_ref_idx(VVCLocalContext *lc)
{
int i;
for (i = 0; i < 2; i++) {
if (!GET_CABAC(INTRA_LUMA_REF_IDX + i))
return i;
}
return i;
}
int ff_vvc_intra_subpartitions_mode_flag(VVCLocalContext *lc)
{
return GET_CABAC(INTRA_SUBPARTITIONS_MODE_FLAG);
}
enum IspType ff_vvc_isp_split_type(VVCLocalContext *lc, const int intra_subpartitions_mode_flag)
{
if (!intra_subpartitions_mode_flag)
return ISP_NO_SPLIT;
return 1 + GET_CABAC(INTRA_SUBPARTITIONS_SPLIT_FLAG);
}
int ff_vvc_intra_luma_mpm_flag(VVCLocalContext *lc)
{
return GET_CABAC(INTRA_LUMA_MPM_FLAG);
}
int ff_vvc_intra_luma_not_planar_flag(VVCLocalContext *lc, const int intra_subpartitions_mode_flag)
{
return GET_CABAC(INTRA_LUMA_NOT_PLANAR_FLAG + !intra_subpartitions_mode_flag);
}
int ff_vvc_intra_luma_mpm_idx(VVCLocalContext *lc)
{
int i;
for (i = 0; i < 4 && get_cabac_bypass(&lc->ep->cc); i++)
/* nothing */;
return i;
}
int ff_vvc_intra_luma_mpm_remainder(VVCLocalContext *lc)
{
return truncated_binary_decode(lc, 60);
}
int ff_vvc_cclm_mode_flag(VVCLocalContext *lc)
{
return GET_CABAC(CCLM_MODE_FLAG);
}
int ff_vvc_cclm_mode_idx(VVCLocalContext *lc)
{
if (!GET_CABAC(CCLM_MODE_IDX))
return 0;
return get_cabac_bypass(&lc->ep->cc) + 1;
}
int ff_vvc_intra_chroma_pred_mode(VVCLocalContext *lc)
{
if (!GET_CABAC(INTRA_CHROMA_PRED_MODE))
return 4;
return (get_cabac_bypass(&lc->ep->cc) << 1) | get_cabac_bypass(&lc->ep->cc);
}
int ff_vvc_general_merge_flag(VVCLocalContext *lc)
{
return GET_CABAC(GENERAL_MERGE_FLAG);
}
static int get_inter_flag_inc(VVCLocalContext *lc, const int x0, const int y0)
{
uint8_t left_merge = 0, top_merge = 0;
uint8_t left_affine = 0, top_affine = 0;
const VVCFrameContext *fc = lc->fc;
get_left_top(lc, &left_merge, &top_merge, x0, y0, fc->tab.msf, fc->tab.msf);
get_left_top(lc, &left_affine, &top_affine, x0, y0, fc->tab.iaf, fc->tab.iaf);
return (left_merge || left_affine) + (top_merge + top_affine);
}
int ff_vvc_merge_subblock_flag(VVCLocalContext *lc)
{
const int inc = get_inter_flag_inc(lc, lc->cu->x0, lc->cu->y0);
return GET_CABAC(MERGE_SUBBLOCK_FLAG + inc);
}
int ff_vvc_merge_subblock_idx(VVCLocalContext *lc, const int max_num_subblock_merge_cand)
{
int i;
if (!GET_CABAC(MERGE_SUBBLOCK_IDX))
return 0;
for (i = 1; i < max_num_subblock_merge_cand - 1 && get_cabac_bypass(&lc->ep->cc); i++)
/* nothing */;
return i;
}
int ff_vvc_regular_merge_flag(VVCLocalContext *lc, const int cu_skip_flag)
{
int inc = !cu_skip_flag;
return GET_CABAC(REGULAR_MERGE_FLAG + inc);
}
int ff_vvc_mmvd_merge_flag(VVCLocalContext *lc)
{
return GET_CABAC(MMVD_MERGE_FLAG);
}
int ff_vvc_mmvd_cand_flag(VVCLocalContext *lc)
{
return GET_CABAC(MMVD_CAND_FLAG);
}
static int mmvd_distance_idx_decode(VVCLocalContext *lc)
{
int i;
if (!GET_CABAC(MMVD_DISTANCE_IDX))
return 0;
for (i = 1; i < 7 && get_cabac_bypass(&lc->ep->cc); i++)
/* nothing */;
return i;
}
static int mmvd_direction_idx_decode(VVCLocalContext *lc)
{
return (get_cabac_bypass(&lc->ep->cc) << 1) | get_cabac_bypass(&lc->ep->cc);
}
void ff_vvc_mmvd_offset_coding(VVCLocalContext *lc, Mv *mmvd_offset, const int ph_mmvd_fullpel_only_flag)
{
const int shift = ph_mmvd_fullpel_only_flag ? 4 : 2;
const int mmvd_distance = 1 << (mmvd_distance_idx_decode(lc) + shift);
const int mmvd_direction_idx = mmvd_direction_idx_decode(lc);
const int mmvd_signs[][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
mmvd_offset->x = mmvd_distance * mmvd_signs[mmvd_direction_idx][0];
mmvd_offset->y = mmvd_distance * mmvd_signs[mmvd_direction_idx][1];
}
static PredMode get_luma_pred_mode(VVCLocalContext *lc)
{
const VVCFrameContext *fc = lc->fc;
const CodingUnit *cu = lc->cu;
PredMode pred_mode;
if (cu->tree_type != DUAL_TREE_CHROMA) {
pred_mode = cu->pred_mode;
} else {
const int x_cb = cu->x0 >> fc->ps.sps->min_cb_log2_size_y;
const int y_cb = cu->y0 >> fc->ps.sps->min_cb_log2_size_y;
const int min_cb_width = fc->ps.pps->min_cb_width;
pred_mode = SAMPLE_CTB(fc->tab.cpm[0], x_cb, y_cb);
}
return pred_mode;
}
int ff_vvc_merge_idx(VVCLocalContext *lc)
{
const VVCSPS *sps = lc->fc->ps.sps;
const int is_ibc = get_luma_pred_mode(lc) == MODE_IBC;
const int c_max = (is_ibc ? sps->max_num_ibc_merge_cand : sps->max_num_merge_cand) - 1;
int i;
if (!GET_CABAC(MERGE_IDX))
return 0;
for (i = 1; i < c_max && get_cabac_bypass(&lc->ep->cc); i++)
/* nothing */;
return i;
}
int ff_vvc_merge_gpm_partition_idx(VVCLocalContext *lc)
{
int i = 0;
for (int j = 0; j < 6; j++)
i = (i << 1) | get_cabac_bypass(&lc->ep->cc);
return i;
}
int ff_vvc_merge_gpm_idx(VVCLocalContext *lc, const int idx)
{
const int c_max = lc->fc->ps.sps->max_num_gpm_merge_cand - idx - 1;
int i;
if (!GET_CABAC(MERGE_IDX))
return 0;
for (i = 1; i < c_max && get_cabac_bypass(&lc->ep->cc); i++)
/* nothing */;
return i;
}
int ff_vvc_ciip_flag(VVCLocalContext *lc)
{
return GET_CABAC(CIIP_FLAG);
}
PredFlag ff_vvc_pred_flag(VVCLocalContext *lc, const int is_b)
{
const int w = lc->cu->cb_width;
const int h = lc->cu->cb_height;
if (!is_b)
return PF_L0;
if (w + h > 12) {
const int log2 = av_log2(w) + av_log2(h);
const int inc = 7 - ((1 + log2)>>1);
if (GET_CABAC(INTER_PRED_IDC + inc))
return PF_BI;
}
return PF_L0 + GET_CABAC(INTER_PRED_IDC + 5);
}
int ff_vvc_inter_affine_flag(VVCLocalContext *lc)
{
const int inc = get_inter_flag_inc(lc, lc->cu->x0, lc->cu->y0);
return GET_CABAC(INTER_AFFINE_FLAG + inc);
}
int ff_vvc_cu_affine_type_flag(VVCLocalContext *lc)
{
return GET_CABAC(CU_AFFINE_TYPE_FLAG);
}
int ff_vvc_sym_mvd_flag(VVCLocalContext *lc)
{
return GET_CABAC(SYM_MVD_FLAG);
}
int ff_vvc_ref_idx_lx(VVCLocalContext *lc, const uint8_t nb_refs)
{
const int c_max = nb_refs - 1;
const int max_ctx = FFMIN(c_max, 2);
int i = 0;
while (i < max_ctx && GET_CABAC(REF_IDX_LX + i))
i++;
if (i == 2) {
while (i < c_max && get_cabac_bypass(&lc->ep->cc))
i++;
}
return i;
}
int ff_vvc_abs_mvd_greater0_flag(VVCLocalContext *lc)
{
return GET_CABAC(ABS_MVD_GREATER0_FLAG);
}
int ff_vvc_abs_mvd_greater1_flag(VVCLocalContext *lc)
{
return GET_CABAC(ABS_MVD_GREATER1_FLAG);
}
int ff_vvc_abs_mvd_minus2(VVCLocalContext *lc)
{
return limited_kth_order_egk_decode(&lc->ep->cc, 1, 15, 17);
}
int ff_vvc_mvd_sign_flag(VVCLocalContext *lc)
{
return get_cabac_bypass(&lc->ep->cc);
}
int ff_vvc_mvp_lx_flag(VVCLocalContext *lc)
{
return GET_CABAC(MVP_LX_FLAG);
}
static int amvr_flag(VVCLocalContext *lc, const int inter_affine_flag)
{
return GET_CABAC(AMVR_FLAG + inter_affine_flag);
}
static int amvr_precision_idx(VVCLocalContext *lc, const int inc, const int c_max)
{
int i = 0;
if (!GET_CABAC(AMVR_PRECISION_IDX + inc))
return 0;
i++;
if (i < c_max && GET_CABAC(AMVR_PRECISION_IDX + 1))
i++;
return i;
}
int ff_vvc_amvr_shift(VVCLocalContext *lc, const int inter_affine_flag,
const PredMode pred_mode, const int has_amvr_flag)
{
int amvr_shift = 2;
if (has_amvr_flag) {
if (pred_mode == MODE_IBC || amvr_flag(lc, inter_affine_flag)) {
int idx;
if (inter_affine_flag) {
idx = amvr_precision_idx(lc, 2, 1);
amvr_shift = idx * 4;
} else if (pred_mode == MODE_IBC) {
idx = amvr_precision_idx(lc, 1, 1);
amvr_shift = 4 + idx * 2;
} else {
static const int shifts[] = {3, 4, 6};
idx = amvr_precision_idx(lc, 0, 2);
amvr_shift = shifts[idx];
}
}
}
return amvr_shift;
}
int ff_vvc_bcw_idx(VVCLocalContext *lc, const int no_backward_pred_flag)
{
const int c_max = no_backward_pred_flag ? 4 : 2;
int i = 1;
if (!GET_CABAC(BCW_IDX))
return 0;
while (i < c_max && get_cabac_bypass(&lc->ep->cc))
i++;
return i;
}
int ff_vvc_tu_cb_coded_flag(VVCLocalContext *lc)
{
return GET_CABAC(TU_CB_CODED_FLAG + lc->cu->bdpcm_flag[1]);
}
int ff_vvc_tu_cr_coded_flag(VVCLocalContext *lc, int tu_cb_coded_flag)
{
return GET_CABAC(TU_CR_CODED_FLAG + (lc->cu->bdpcm_flag[1] ? 2 : tu_cb_coded_flag));
}
int ff_vvc_tu_y_coded_flag(VVCLocalContext *lc)
{
const CodingUnit *cu = lc->cu;
int inc;
if (cu->bdpcm_flag[0])
inc = 1;
else if (cu->isp_split_type == ISP_NO_SPLIT)
inc = 0;
else
inc = 2 + lc->parse.prev_tu_cbf_y;
lc->parse.prev_tu_cbf_y = GET_CABAC(TU_Y_CODED_FLAG + inc);
return lc->parse.prev_tu_cbf_y;
}
int ff_vvc_cu_qp_delta_abs(VVCLocalContext *lc)
{
int v, i, k;
if (!GET_CABAC(CU_QP_DELTA_ABS))
return 0;
// prefixVal
for (v = 1; v < 5 && GET_CABAC(CU_QP_DELTA_ABS + 1); v++)
/* nothing */;
if (v < 5)
return v;
// 9.3.3.5 k-th order Exp-Golomb binarization process
// suffixVal
// CuQpDeltaVal shall in the range of −( 32 + QpBdOffset / 2 ) to +( 31 + QpBdOffset / 2 )
// so k = 6 should enough
for (k = 0; k < 6 && get_cabac_bypass(&lc->ep->cc); k++)
/* nothing */;
i = (1 << k) - 1;
v = 0;
while (k--)
v = (v << 1) + get_cabac_bypass(&lc->ep->cc);
v += i;
return v + 5;
}
int ff_vvc_cu_qp_delta_sign_flag(VVCLocalContext *lc)
{
return get_cabac_bypass(&lc->ep->cc);
}
int ff_vvc_cu_chroma_qp_offset_flag(VVCLocalContext *lc)
{
return GET_CABAC(CU_CHROMA_QP_OFFSET_FLAG);
}
int ff_vvc_cu_chroma_qp_offset_idx(VVCLocalContext *lc)
{
const int c_max = lc->fc->ps.pps->r->pps_chroma_qp_offset_list_len_minus1;
int i;
for (i = 0; i < c_max && GET_CABAC(CU_CHROMA_QP_OFFSET_IDX); i++)
/* nothing */;
return i;
}
static av_always_inline int last_significant_coeff_xy_prefix(VVCLocalContext *lc,
const int log2_tb_size, const int log2_zo_tb_size, const int c_idx, const int ctx)
{
int i = 0;
int max = (log2_zo_tb_size << 1) - 1;
int ctx_offset, ctx_shift;
if (!log2_tb_size)
return 0;
if (!c_idx) {
const int offset_y[] = {0, 0, 3, 6, 10, 15};
ctx_offset = offset_y[log2_tb_size - 1];
ctx_shift = (log2_tb_size + 1) >> 2;
} else {
const int shifts[] = {0, 0, 0, 1, 2, 2, 2};
ctx_offset = 20;
ctx_shift = shifts[log2_tb_size];
}
while (i < max && GET_CABAC(ctx + (i >> ctx_shift) + ctx_offset))
i++;
return i;
}
static av_always_inline int last_significant_coeff_x_prefix_decode(VVCLocalContext *lc,
const int log2_tb_width, const int log2_zo_tb_width, const int c_idx)
{
return last_significant_coeff_xy_prefix(lc, log2_tb_width, log2_zo_tb_width, c_idx, LAST_SIG_COEFF_X_PREFIX);
}
static av_always_inline int last_significant_coeff_y_prefix_decode(VVCLocalContext *lc,
const int log2_tb_height, const int log2_zo_tb_height, const int c_idx)
{
return last_significant_coeff_xy_prefix(lc, log2_tb_height, log2_zo_tb_height, c_idx, LAST_SIG_COEFF_Y_PREFIX);
}
static av_always_inline int last_sig_coeff_suffix_decode(VVCLocalContext *lc,
const int last_significant_coeff_y_prefix)
{
const int length = (last_significant_coeff_y_prefix >> 1) - 1;
int value = get_cabac_bypass(&lc->ep->cc);
for (int i = 1; i < length; i++)
value = (value << 1) | get_cabac_bypass(&lc->ep->cc);
return value;
}
int ff_vvc_tu_joint_cbcr_residual_flag(VVCLocalContext *lc, const int tu_cb_coded_flag, const int tu_cr_coded_flag)
{
return GET_CABAC(TU_JOINT_CBCR_RESIDUAL_FLAG + 2 * tu_cb_coded_flag + tu_cr_coded_flag - 1);
}
int ff_vvc_transform_skip_flag(VVCLocalContext *lc, const int inc)
{
return GET_CABAC(TRANSFORM_SKIP_FLAG + inc);
}
//9.3.4.2.7 Derivation process for the variables locNumSig, locSumAbsPass1
static int get_local_sum(const int *level, const int w, const int h,
const int xc, const int yc, const int hist_value)
{
int loc_sum = 3 * hist_value;
level += w * yc + xc;
if (xc < w - 1) {
loc_sum += level[1];
if (xc < w - 2)
loc_sum += level[2] - hist_value;
if (yc < h - 1)
loc_sum += level[w + 1] - hist_value;
}
if (yc < h - 1) {
loc_sum += level[w];
if (yc < h - 2)
loc_sum += level[w << 1] - hist_value;
}
return loc_sum;
}
//9.3.4.2.7 Derivation process for the variables locNumSig, locSumAbsPass1
static int get_local_sum_ts(const int *level, const int w, const int h, const int xc, const int yc)
{
int loc_sum = 0;
level += w * yc + xc;
if (xc > 0)
loc_sum += level[-1];
if (yc > 0)
loc_sum += level[-w];
return loc_sum;
}
static int get_gtx_flag_inc(const ResidualCoding* rc, const int xc, const int yc, const int last)
{
const TransformBlock *tb = rc->tb;
int inc;
if (last) {
const int incs[] = {0, 21, 21};
inc = incs[tb->c_idx];
} else {
const int d = xc + yc;
const int local_sum_sig = get_local_sum(rc->sig_coeff_flag,
tb->tb_width,tb->tb_height, xc, yc, rc->hist_value);
const int loc_sum_abs_pass1 = get_local_sum(rc->abs_level_pass1,
tb->tb_width, tb->tb_height, xc, yc, rc->hist_value);
const int offset = FFMIN(loc_sum_abs_pass1 - local_sum_sig, 4);
if (!tb->c_idx)
inc = 1 + offset + (!d ? 15 : (d < 3 ? 10 : (d < 10 ? 5 : 0)));
else
inc = 22 + offset + (!d ? 5 : 0);
}
return inc;
}
static int abs_level_gtx_flag_decode(VVCLocalContext *lc, const int inc)
{
return GET_CABAC(ABS_LEVEL_GTX_FLAG + inc);
}
static int par_level_flag_decode(VVCLocalContext *lc, const int inc)
{
return GET_CABAC(PAR_LEVEL_FLAG + inc);
}
static int par_level_flag_ts_decode(VVCLocalContext *lc)
{
const int inc = 32;
return GET_CABAC(PAR_LEVEL_FLAG + inc);
}
static int sb_coded_flag_decode(VVCLocalContext *lc, const uint8_t *sb_coded_flag,
const ResidualCoding *rc, const int xs, const int ys)
{
const H266RawSliceHeader *rsh = lc->sc->sh.r;
const TransformBlock *tb = rc->tb;
const int w = rc->width_in_sbs;
const int h = rc->height_in_sbs;
int inc;
if (tb->ts && !rsh->sh_ts_residual_coding_disabled_flag) {
const int left = xs > 0 ? sb_coded_flag[-1] : 0;
const int above = ys > 0 ? sb_coded_flag[-w] : 0;
inc = left + above + 4;
} else {
const int right = (xs < w - 1) ? sb_coded_flag[1] : 0;
const int bottom = (ys < h - 1) ? sb_coded_flag[w] : 0;
inc = (right | bottom) + (tb->c_idx ? 2 : 0);
}
return GET_CABAC(SB_CODED_FLAG + inc);
}
static int sig_coeff_flag_decode(VVCLocalContext *lc, const ResidualCoding* rc, const int xc, const int yc)
{
const H266RawSliceHeader *rsh = lc->sc->sh.r;
const TransformBlock *tb = rc->tb;
int inc;
if (tb->ts && !rsh->sh_ts_residual_coding_disabled_flag) {
const int local_num_sig = get_local_sum_ts(rc->sig_coeff_flag, tb->tb_width, tb->tb_height, xc, yc);
inc = 60 + local_num_sig;
} else {
const int d = xc + yc;
const int loc_sum_abs_pass1 = get_local_sum(rc->abs_level_pass1,
tb->tb_width, tb->tb_height, xc, yc, 0);
if (!tb->c_idx) {
inc = 12 * FFMAX(0, rc->qstate - 1) + FFMIN((loc_sum_abs_pass1 + 1) >> 1, 3) + ((d < 2) ? 8 : (d < 5 ? 4 : 0));
} else {
inc = 36 + 8 * FFMAX(0, rc->qstate - 1) + FFMIN((loc_sum_abs_pass1 + 1) >> 1, 3) + (d < 2 ? 4 : 0);
}
}
return GET_CABAC(SIG_COEFF_FLAG + inc);
}
static int abs_get_rice_param(VVCLocalContext *lc, const ResidualCoding* rc,
const int xc, const int yc, const int base_level)
{
const VVCSPS *sps = lc->fc->ps.sps;
const TransformBlock* tb = rc->tb;
const int rice_params[] = {
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
};
int loc_sum_abs;
int shift_val;
loc_sum_abs = get_local_sum(rc->abs_level, tb->tb_width, tb->tb_height, xc,
yc, rc->hist_value);
if (!sps->r->sps_rrc_rice_extension_flag) {
shift_val = 0;
} else {
shift_val = (av_log2(FFMAX(FFMIN(loc_sum_abs, 2048), 8)) - 3) & ~1;
}
loc_sum_abs = av_clip_uintp2((loc_sum_abs >> shift_val) - base_level * 5, 5);
return rice_params[loc_sum_abs] + shift_val;
}
static int abs_decode(VVCLocalContext *lc, const int c_rice_param)
{
const VVCSPS *sps = lc->fc->ps.sps;
const int MAX_BIN = 6;
int prefix = 0;
int suffix = 0;
while (prefix < MAX_BIN && get_cabac_bypass(&lc->ep->cc))
prefix++;
if (prefix < MAX_BIN) {
for (int i = 0; i < c_rice_param; i++) {
suffix = (suffix << 1) | get_cabac_bypass(&lc->ep->cc);
}
} else {
suffix = limited_kth_order_egk_decode(&lc->ep->cc,
c_rice_param + 1,
26 - sps->log2_transform_range,
sps->log2_transform_range);
}
return suffix + (prefix << c_rice_param);
}
static int abs_remainder_decode(VVCLocalContext *lc, const ResidualCoding* rc, const int xc, const int yc)
{
const VVCSPS *sps = lc->fc->ps.sps;
const H266RawSliceHeader *rsh = lc->sc->sh.r;
const int base_level[][2][2] = {
{ {4, 4}, {4, 4} },
{ {3, 2}, {2, 1} }
};
const int c_rice_param = abs_get_rice_param(lc, rc, xc, yc,
base_level[sps->r->sps_rrc_rice_extension_flag][sps->bit_depth > 12][IS_I(rsh)]);
const int rem = abs_decode(lc, c_rice_param);
return rem;
}
static int abs_remainder_ts_decode(VVCLocalContext *lc, const ResidualCoding* rc, const int xc, const int yc)
{
const H266RawSliceHeader *rsh = lc->sc->sh.r;
const int c_rice_param = rsh->sh_ts_residual_coding_rice_idx_minus1 + 1;
const int rem = abs_decode(lc, c_rice_param);
return rem;
}
static int coeff_sign_flag_decode(VVCLocalContext *lc)
{
return get_cabac_bypass(&lc->ep->cc);
}
//9.3.4.2.10 Derivation process of ctxInc for the syntax element coeff_sign_flag for transform skip mode
static int coeff_sign_flag_ts_decode(VVCLocalContext *lc, const CodingUnit *cu, const ResidualCoding *rc, const int xc, const int yc)
{
const TransformBlock *tb = rc->tb;
const int w = tb->tb_width;
const int *level = rc->coeff_sign_level + yc * w + xc;
const int left_sign = xc ? level[-1] : 0;
const int above_sign = yc ? level[-w] : 0;
const int bdpcm_flag = cu->bdpcm_flag[tb->c_idx];
int inc;
if (left_sign == -above_sign)
inc = bdpcm_flag ? 3 : 0;
else if (left_sign >= 0 && above_sign >= 0)
inc = bdpcm_flag ? 4 : 1;
else
inc = bdpcm_flag ? 5 : 2;
return GET_CABAC(COEFF_SIGN_FLAG + inc);
}
static int abs_level_gt1_flag_ts_decode(VVCLocalContext *lc, const CodingUnit *cu, const ResidualCoding *rc, const int xc, const int yc)
{
const TransformBlock *tb = rc->tb;
const int *sig_coeff_flag = rc->sig_coeff_flag + yc * tb->tb_width + xc;
int inc;
if (cu->bdpcm_flag[tb->c_idx]) {
inc = 67;
} else {
const int l = xc > 0 ? sig_coeff_flag[-1] : 0;
const int a = yc > 0 ? sig_coeff_flag[-tb->tb_width] : 0;
inc = 64 + a + l;
}
return GET_CABAC(ABS_LEVEL_GTX_FLAG + inc);
}
static int abs_level_gtx_flag_ts_decode(VVCLocalContext *lc, const int j)
{
const int inc = 67 + j;
return GET_CABAC(ABS_LEVEL_GTX_FLAG + inc);
}
static const uint8_t qstate_translate_table[][2] = {
{ 0, 2 }, { 2, 0 }, { 1, 3 }, { 3, 1 }
};
static int dec_abs_level_decode(VVCLocalContext *lc, const ResidualCoding *rc,
const int xc, const int yc, int *abs_level)
{
const int c_rice_param = abs_get_rice_param(lc, rc, xc, yc, 0);
const int dec_abs_level = abs_decode(lc, c_rice_param);
const int zero_pos = (rc->qstate < 2 ? 1 : 2) << c_rice_param;
*abs_level = 0;
if (dec_abs_level != zero_pos) {
*abs_level = dec_abs_level;
if (dec_abs_level < zero_pos)
*abs_level += 1;
}
return dec_abs_level;
}
static void ep_update_hist(EntryPoint *ep, ResidualCoding *rc,
const int remainder, const int addin)
{
int *stat = ep->stat_coeff + rc->tb->c_idx;
if (rc->update_hist && remainder > 0) {
*stat = (*stat + av_log2(remainder) + addin) >> 1;
rc->update_hist = 0;
}
}
static void init_residual_coding(const VVCLocalContext *lc, ResidualCoding *rc,
const int log2_zo_tb_width, const int log2_zo_tb_height,
TransformBlock *tb)
{
const VVCSPS *sps = lc->fc->ps.sps;
int log2_sb_w = (FFMIN(log2_zo_tb_width, log2_zo_tb_height ) < 2 ? 1 : 2 );
int log2_sb_h = log2_sb_w;
if ( log2_zo_tb_width + log2_zo_tb_height > 3 ) {
if ( log2_zo_tb_width < 2 ) {
log2_sb_w = log2_zo_tb_width;
log2_sb_h = 4 - log2_sb_w;
} else if ( log2_zo_tb_height < 2 ) {
log2_sb_h = log2_zo_tb_height;
log2_sb_w = 4 - log2_sb_h;
}
}
rc->log2_sb_w = log2_sb_w;
rc->log2_sb_h = log2_sb_h;
rc->num_sb_coeff = 1 << (log2_sb_w + log2_sb_h);
rc->last_sub_block = ( 1 << ( log2_zo_tb_width + log2_zo_tb_height - (log2_sb_w + log2_sb_h))) - 1;
rc->hist_value = sps->r->sps_persistent_rice_adaptation_enabled_flag ? (1 << lc->ep->stat_coeff[tb->c_idx]) : 0;
rc->update_hist = sps->r->sps_persistent_rice_adaptation_enabled_flag ? 1 : 0;
rc->rem_bins_pass1 = (( 1 << ( log2_zo_tb_width + log2_zo_tb_height)) * 7 ) >> 2;
rc->sb_scan_x_off = ff_vvc_diag_scan_x[log2_zo_tb_width - log2_sb_w][log2_zo_tb_height - log2_sb_h];
rc->sb_scan_y_off = ff_vvc_diag_scan_y[log2_zo_tb_width - log2_sb_w][log2_zo_tb_height - log2_sb_h];
rc->scan_x_off = ff_vvc_diag_scan_x[log2_sb_w][log2_sb_h];
rc->scan_y_off = ff_vvc_diag_scan_y[log2_sb_w][log2_sb_h];
rc->infer_sb_cbf = 1;
rc->width_in_sbs = (1 << (log2_zo_tb_width - log2_sb_w));
rc->height_in_sbs = (1 << (log2_zo_tb_height - log2_sb_h));
rc->nb_sbs = rc->width_in_sbs * rc->height_in_sbs;
rc->last_scan_pos = rc->num_sb_coeff;
rc->qstate = 0;
rc->tb = tb;
}
static int residual_ts_coding_subblock(VVCLocalContext *lc, ResidualCoding* rc, const int i)
{
const CodingUnit *cu = lc->cu;
TransformBlock *tb = rc->tb;
const int bdpcm_flag = cu->bdpcm_flag[tb->c_idx];
const int xs = rc->sb_scan_x_off[i];
const int ys = rc->sb_scan_y_off[i];
uint8_t *sb_coded_flag = rc->sb_coded_flag + ys * rc->width_in_sbs + xs;
int infer_sb_sig_coeff_flag = 1;
int last_scan_pos_pass1 = -1, last_scan_pos_pass2 = -1, n;
int abs_level_gtx_flag[MAX_SUB_BLOCK_SIZE * MAX_SUB_BLOCK_SIZE];
int abs_level_pass2[MAX_SUB_BLOCK_SIZE * MAX_SUB_BLOCK_SIZE]; ///< AbsLevelPass2
if (i != rc->last_sub_block || !rc->infer_sb_cbf)
*sb_coded_flag = sb_coded_flag_decode(lc, sb_coded_flag, rc, xs, ys);
else
*sb_coded_flag = 1;
if (*sb_coded_flag && i < rc->last_sub_block)
rc->infer_sb_cbf = 0;
//first scan pass
for (n = 0; n < rc->num_sb_coeff && rc->rem_bins_pass1 >= 4; n++) {
const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
const int off = yc * tb->tb_width + xc;
int *sig_coeff_flag = rc->sig_coeff_flag + off;
int *abs_level_pass1 = rc->abs_level_pass1 + off;
int *coeff_sign_level = rc->coeff_sign_level + off;
int par_level_flag = 0;
abs_level_gtx_flag[n] = 0;
last_scan_pos_pass1 = n;
if (*sb_coded_flag && (n != rc->num_sb_coeff - 1 || !infer_sb_sig_coeff_flag)) {
*sig_coeff_flag = sig_coeff_flag_decode(lc, rc, xc, yc);
rc->rem_bins_pass1--;
if (*sig_coeff_flag)
infer_sb_sig_coeff_flag = 0;
} else {
*sig_coeff_flag = (n == rc->num_sb_coeff - 1) && infer_sb_sig_coeff_flag && *sb_coded_flag;
}
*coeff_sign_level = 0;
if (*sig_coeff_flag) {
*coeff_sign_level = 1 - 2 * coeff_sign_flag_ts_decode(lc, cu, rc, xc, yc);
abs_level_gtx_flag[n] = abs_level_gt1_flag_ts_decode(lc, cu, rc, xc, yc);
rc->rem_bins_pass1 -= 2;
if (abs_level_gtx_flag[n]) {
par_level_flag = par_level_flag_ts_decode(lc);
rc->rem_bins_pass1--;
}
}
*abs_level_pass1 = *sig_coeff_flag + par_level_flag + abs_level_gtx_flag[n];
}
//greater than x scan pass
for (n = 0; n < rc->num_sb_coeff && rc->rem_bins_pass1 >= 4; n++) {
const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
const int off = yc * tb->tb_width + xc;
abs_level_pass2[n] = rc->abs_level_pass1[off];
for (int j = 1; j < 5 && abs_level_gtx_flag[n]; j++) {
abs_level_gtx_flag[n] = abs_level_gtx_flag_ts_decode(lc, j);
abs_level_pass2[n] += abs_level_gtx_flag[n] << 1;
rc->rem_bins_pass1--;
}
last_scan_pos_pass2 = n;
}
/* remainder scan pass */
for (n = 0; n < rc->num_sb_coeff; n++) {
const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
const int off = yc * tb->tb_width + xc;
const int *abs_level_pass1 = rc->abs_level_pass1 + off;
int *abs_level = rc->abs_level + off;
int *coeff_sign_level = rc->coeff_sign_level + off;
int abs_remainder = 0;
if ((n <= last_scan_pos_pass2 && abs_level_pass2[n] >= 10) ||
(n > last_scan_pos_pass2 && n <= last_scan_pos_pass1 &&
*abs_level_pass1 >= 2) ||
(n > last_scan_pos_pass1 && *sb_coded_flag))
abs_remainder = abs_remainder_ts_decode(lc, rc, xc, yc);
if (n <= last_scan_pos_pass2) {
*abs_level = abs_level_pass2[n] + 2 * abs_remainder;
} else if (n <= last_scan_pos_pass1) {
*abs_level = *abs_level_pass1 + 2 * abs_remainder;
} else {
*abs_level = abs_remainder;
if (abs_remainder) {
//n > lastScanPosPass1
*coeff_sign_level = 1 - 2 * coeff_sign_flag_decode(lc);
}
}
if (!bdpcm_flag && n <= last_scan_pos_pass1) {
const int left = xc > 0 ? abs_level[-1] : 0;
const int above = yc > 0 ? abs_level[-tb->tb_width] : 0;
const int pred = FFMAX(left, above);
if (*abs_level == 1 && pred > 0)
*abs_level = pred;
else if (*abs_level > 0 && *abs_level <= pred)
(*abs_level)--;
}
if (*abs_level) {
tb->coeffs[off] = *coeff_sign_level * *abs_level;
tb->max_scan_x = FFMAX(xc, tb->max_scan_x);
tb->max_scan_y = FFMAX(yc, tb->max_scan_y);
tb->min_scan_x = FFMIN(xc, tb->min_scan_x);
tb->min_scan_y = FFMIN(yc, tb->min_scan_y);
} else {
tb->coeffs[off] = 0;
}
}
return 0;
}
static int hls_residual_ts_coding(VVCLocalContext *lc, TransformBlock *tb)
{
ResidualCoding rc;
tb->min_scan_x = tb->min_scan_y = INT_MAX;
init_residual_coding(lc, &rc, tb->log2_tb_width, tb->log2_tb_height, tb);
for (int i = 0; i <= rc.last_sub_block; i++) {
int ret = residual_ts_coding_subblock(lc, &rc, i);
if (ret < 0)
return ret;
}
return 0;
}
static inline int residual_coding_subblock(VVCLocalContext *lc, ResidualCoding *rc, const int i)
{
const H266RawSliceHeader *rsh = lc->sc->sh.r;
TransformBlock *tb = rc->tb;
int first_sig_scan_pos_sb, last_sig_scan_pos_sb;
int first_pos_mode0, first_pos_mode1;
int infer_sb_dc_sig_coeff_flag = 0;
int n, sig_hidden_flag, sum = 0;
int abs_level_gt2_flag[MAX_SUB_BLOCK_SIZE * MAX_SUB_BLOCK_SIZE];
const int start_qstate_sb = rc->qstate;
const int xs = rc->sb_scan_x_off[i];
const int ys = rc->sb_scan_y_off[i];
uint8_t *sb_coded_flag = rc->sb_coded_flag + ys * rc->width_in_sbs + xs;
av_assert0(rc->num_sb_coeff <= MAX_SUB_BLOCK_SIZE * MAX_SUB_BLOCK_SIZE);
if (i < rc->last_sub_block && i > 0) {
*sb_coded_flag = sb_coded_flag_decode(lc, sb_coded_flag, rc, xs, ys);
infer_sb_dc_sig_coeff_flag = 1;
} else {
*sb_coded_flag = 1;
}
if (*sb_coded_flag && (xs > 3 || ys > 3) && !tb->c_idx)
lc->parse.mts_zero_out_sig_coeff_flag = 0;
if (!*sb_coded_flag)
return 0;
first_sig_scan_pos_sb = rc->num_sb_coeff;
last_sig_scan_pos_sb = -1;
first_pos_mode0 = (i == rc->last_sub_block ? rc->last_scan_pos : rc->num_sb_coeff -1);
first_pos_mode1 = first_pos_mode0;
for (n = first_pos_mode0; n >= 0 && rc->rem_bins_pass1 >= 4; n--) {
const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
const int last = (xc == rc->last_significant_coeff_x && yc == rc->last_significant_coeff_y);
int *abs_level_pass1 = rc->abs_level_pass1 + yc * tb->tb_width + xc;
int *sig_coeff_flag = rc->sig_coeff_flag + yc * tb->tb_width + xc;
if ((n > 0 || !infer_sb_dc_sig_coeff_flag ) && !last) {
*sig_coeff_flag = sig_coeff_flag_decode(lc, rc, xc, yc);
rc->rem_bins_pass1--;
if (*sig_coeff_flag)
infer_sb_dc_sig_coeff_flag = 0;
} else {
*sig_coeff_flag = last || (!rc->scan_x_off[n] && !rc ->scan_y_off[n] &&
infer_sb_dc_sig_coeff_flag);
}
*abs_level_pass1 = 0;
if (*sig_coeff_flag) {
int abs_level_gt1_flag, par_level_flag = 0;
const int inc = get_gtx_flag_inc(rc, xc, yc, last);
abs_level_gt1_flag = abs_level_gtx_flag_decode(lc, inc);
rc->rem_bins_pass1--;
if (abs_level_gt1_flag) {
par_level_flag = par_level_flag_decode(lc, inc);
abs_level_gt2_flag[n] = abs_level_gtx_flag_decode(lc, inc + 32);
rc->rem_bins_pass1 -= 2;
} else {
abs_level_gt2_flag[n] = 0;
}
if (last_sig_scan_pos_sb == -1)
last_sig_scan_pos_sb = n;
first_sig_scan_pos_sb = n;
*abs_level_pass1 =
1 + par_level_flag + abs_level_gt1_flag + (abs_level_gt2_flag[n] << 1);
} else {
abs_level_gt2_flag[n] = 0;
}
if (rsh->sh_dep_quant_used_flag)
rc->qstate = qstate_translate_table[rc->qstate][*abs_level_pass1 & 1];
first_pos_mode1 = n - 1;
}
for (n = first_pos_mode0; n > first_pos_mode1; n--) {
const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
const int *abs_level_pass1 = rc->abs_level_pass1 + yc * tb->tb_width + xc;
int *abs_level = rc->abs_level + yc * tb->tb_width + xc;
*abs_level = *abs_level_pass1;
if (abs_level_gt2_flag[n]) {
const int abs_remainder = abs_remainder_decode(lc, rc, xc, yc);
ep_update_hist(lc->ep, rc, abs_remainder, 2);
*abs_level += 2 * abs_remainder;
}
}
for (n = first_pos_mode1; n >= 0; n--) {
const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
int *abs_level = rc->abs_level + yc * tb->tb_width + xc;
if (*sb_coded_flag) {
const int dec_abs_level = dec_abs_level_decode(lc, rc, xc, yc, abs_level);
ep_update_hist(lc->ep, rc, dec_abs_level, 0);
}
if (*abs_level > 0) {
if (last_sig_scan_pos_sb == -1)
last_sig_scan_pos_sb = n;
first_sig_scan_pos_sb = n;
}
if (rsh->sh_dep_quant_used_flag)
rc->qstate = qstate_translate_table[rc->qstate][*abs_level & 1];
}
sig_hidden_flag = rsh->sh_sign_data_hiding_used_flag &&
(last_sig_scan_pos_sb - first_sig_scan_pos_sb > 3 ? 1 : 0);
if (rsh->sh_dep_quant_used_flag)
rc->qstate = start_qstate_sb;
n = (i == rc->last_sub_block ? rc->last_scan_pos : rc->num_sb_coeff -1);
for (/* nothing */; n >= 0; n--) {
int trans_coeff_level;
const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
const int off = yc * tb->tb_width + xc;
const int *abs_level = rc->abs_level + off;
if (*abs_level > 0) {
int sign = 1;
if (!sig_hidden_flag || (n != first_sig_scan_pos_sb))
sign = 1 - 2 * coeff_sign_flag_decode(lc);
if (rsh->sh_dep_quant_used_flag) {
trans_coeff_level = (2 * *abs_level - (rc->qstate > 1)) * sign;
} else {
trans_coeff_level = *abs_level * sign;
if (sig_hidden_flag) {
sum += *abs_level;
if (n == first_sig_scan_pos_sb && (sum % 2))
trans_coeff_level = -trans_coeff_level;
}
}
tb->coeffs[off] = trans_coeff_level;
tb->max_scan_x = FFMAX(xc, tb->max_scan_x);
tb->max_scan_y = FFMAX(yc, tb->max_scan_y);
}
if (rsh->sh_dep_quant_used_flag)
rc->qstate = qstate_translate_table[rc->qstate][*abs_level & 1];
}
return 0;
}
static void derive_last_scan_pos(ResidualCoding *rc)
{
int xc, yc, xs, ys;
do {
if (!rc->last_scan_pos) {
rc->last_scan_pos = rc->num_sb_coeff;
rc->last_sub_block--;
}
rc->last_scan_pos--;
xs = rc->sb_scan_x_off[rc->last_sub_block];
ys = rc->sb_scan_y_off[rc->last_sub_block];
xc = (xs << rc->log2_sb_w) + rc->scan_x_off[rc->last_scan_pos];
yc = (ys << rc->log2_sb_h) + rc->scan_y_off[rc->last_scan_pos];
} while ((xc != rc->last_significant_coeff_x) || (yc != rc->last_significant_coeff_y));
}
static void last_significant_coeff_x_y_decode(ResidualCoding *rc, VVCLocalContext *lc,
const int log2_zo_tb_width, const int log2_zo_tb_height)
{
const H266RawSliceHeader *rsh = lc->sc->sh.r;
const TransformBlock *tb = rc->tb;
int last_significant_coeff_x, last_significant_coeff_y;
last_significant_coeff_x = last_significant_coeff_x_prefix_decode(lc,
tb->log2_tb_width, log2_zo_tb_width, tb->c_idx);
last_significant_coeff_y = last_significant_coeff_y_prefix_decode(lc,
tb->log2_tb_height, log2_zo_tb_height, tb->c_idx);
if (last_significant_coeff_x > 3) {
int suffix = last_sig_coeff_suffix_decode(lc, last_significant_coeff_x);
last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
(2 + (last_significant_coeff_x & 1)) + suffix;
}
if (last_significant_coeff_y > 3) {
int suffix = last_sig_coeff_suffix_decode(lc, last_significant_coeff_y);
last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
(2 + (last_significant_coeff_y & 1)) + suffix;
}
if (rsh->sh_reverse_last_sig_coeff_flag) {
last_significant_coeff_x = (1 << log2_zo_tb_width) - 1 - last_significant_coeff_x;
last_significant_coeff_y = (1 << log2_zo_tb_height) - 1 - last_significant_coeff_y;
}
rc->last_significant_coeff_x = last_significant_coeff_x;
rc->last_significant_coeff_y = last_significant_coeff_y;
}
static int hls_residual_coding(VVCLocalContext *lc, TransformBlock *tb)
{
const VVCSPS *sps = lc->fc->ps.sps;
const CodingUnit *cu = lc->cu;
const int log2_tb_width = tb->log2_tb_width;
const int log2_tb_height = tb->log2_tb_height;
const int c_idx = tb->c_idx;
int log2_zo_tb_width, log2_zo_tb_height;
ResidualCoding rc;
if (sps->r->sps_mts_enabled_flag && cu->sbt_flag && !c_idx && log2_tb_width == 5 && log2_tb_height < 6)
log2_zo_tb_width = 4;
else
log2_zo_tb_width = FFMIN(log2_tb_width, 5 );
if (sps->r->sps_mts_enabled_flag && cu->sbt_flag && !c_idx && log2_tb_width < 6 && log2_tb_height == 5 )
log2_zo_tb_height = 4;
else
log2_zo_tb_height = FFMIN(log2_tb_height, 5);
init_residual_coding(lc, &rc, log2_zo_tb_width, log2_zo_tb_height, tb);
last_significant_coeff_x_y_decode(&rc, lc, log2_zo_tb_width, log2_zo_tb_height);
derive_last_scan_pos(&rc);
if (!rc.last_sub_block && log2_tb_width >= 2 && log2_tb_height >= 2 && !tb->ts && rc.last_scan_pos > 0)
lc->parse.lfnst_dc_only = 0;
if ((rc.last_sub_block > 0 && log2_tb_width >= 2 && log2_tb_height >= 2 ) ||
(rc.last_scan_pos > 7 && (log2_tb_width == 2 || log2_tb_width == 3 ) &&
log2_tb_width == log2_tb_height))
lc->parse.lfnst_zero_out_sig_coeff_flag = 0;
if ((rc.last_sub_block > 0 || rc.last_scan_pos > 0 ) && !c_idx)
lc->parse.mts_dc_only = 0;
memset(tb->coeffs, 0, tb->tb_width * tb->tb_height * sizeof(*tb->coeffs));
memset(rc.abs_level, 0, tb->tb_width * tb->tb_height * sizeof(rc.abs_level[0]));
memset(rc.sb_coded_flag, 0, rc.nb_sbs);
memset(rc.abs_level_pass1, 0, tb->tb_width * tb->tb_height * sizeof(rc.abs_level_pass1[0]));
memset(rc.sig_coeff_flag, 0, tb->tb_width * tb->tb_height * sizeof(rc.sig_coeff_flag[0]));
for (int i = rc.last_sub_block; i >= 0; i--) {
int ret = residual_coding_subblock(lc, &rc, i);
if (ret < 0)
return ret;
}
return 0;
}
int ff_vvc_residual_coding(VVCLocalContext *lc, TransformBlock *tb)
{
const H266RawSliceHeader *rsh = lc->sc->sh.r;
const int ts = !rsh->sh_ts_residual_coding_disabled_flag && tb->ts;
return ts ? hls_residual_ts_coding(lc, tb) : hls_residual_coding(lc, tb);
}
int ff_vvc_cu_coded_flag(VVCLocalContext *lc)
{
return GET_CABAC(CU_CODED_FLAG);
}
int ff_vvc_sbt_flag(VVCLocalContext *lc)
{
const int w = lc->cu->cb_width;
const int h = lc->cu->cb_height;
const int inc = w * h <= 256;
return GET_CABAC(CU_SBT_FLAG + inc);
}
int ff_vvc_sbt_quad_flag(VVCLocalContext *lc)
{
return GET_CABAC(CU_SBT_QUAD_FLAG);
}
int ff_vvc_sbt_horizontal_flag(VVCLocalContext *lc)
{
const int w = lc->cu->cb_width;
const int h = lc->cu->cb_height;
const int inc = (w == h) ? 0 : ((w < h) ? 1 : 2);
return GET_CABAC(CU_SBT_HORIZONTAL_FLAG + inc);
}
int ff_vvc_sbt_pos_flag(VVCLocalContext *lc)
{
return GET_CABAC(CU_SBT_POS_FLAG);
}
int ff_vvc_lfnst_idx(VVCLocalContext *lc, const int inc)
{
if (!GET_CABAC(LFNST_IDX + inc))
return 0;
if (!GET_CABAC(LFNST_IDX + 2))
return 1;
return 2;
}
int ff_vvc_mts_idx(VVCLocalContext *lc)
{
int i;
for (i = 0; i < 4; i++) {
if (!GET_CABAC(MTS_IDX + i))
return i;
}
return i;
}
int ff_vvc_end_of_slice_flag_decode(VVCLocalContext *lc)
{
return get_cabac_terminate(&lc->ep->cc);
}
int ff_vvc_end_of_tile_one_bit(VVCLocalContext *lc)
{
return get_cabac_terminate(&lc->ep->cc);
}
int ff_vvc_end_of_subset_one_bit(VVCLocalContext *lc)
{
return get_cabac_terminate(&lc->ep->cc);
}
|