lin
2025-01-04 be45768b193281917003531c55e9ebda9d2c2f99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/**
******************************************************************************
* @file ihevce_cabac_tu.c
*
* @brief
*  This file contains function definitions for cabac entropy coding of
*  transform units of HEVC syntax
*
* @author
*  ittiam
*
* @List of Functions
*  ihevce_cabac_encode_qp_delta()
*  ihevce_cabac_encode_last_coeff_x_y()
*  ihevce_encode_transform_tree()
*  ihevce_cabac_residue_encode()
*  ihevce_cabac_residue_encode_rdopt()
*  ihevce_cabac_residue_encode_rdoq()
*  ihevce_code_all_sig_coeffs_as_0_explicitly()
*  ihevce_find_new_last_csb()
*  ihevce_copy_backup_ctxt()
*  ihevce_estimate_num_bits_till_next_non_zero_coeff()
*
******************************************************************************
*/
 
/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
 
/* System include files */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <math.h>
 
/* User include files */
#include "ihevc_typedefs.h"
#include "itt_video_api.h"
#include "ihevce_api.h"
 
#include "rc_cntrl_param.h"
#include "rc_frame_info_collector.h"
#include "rc_look_ahead_params.h"
 
#include "ihevc_defs.h"
#include "ihevc_structs.h"
#include "ihevc_platform_macros.h"
#include "ihevc_deblk.h"
#include "ihevc_itrans_recon.h"
#include "ihevc_chroma_itrans_recon.h"
#include "ihevc_chroma_intra_pred.h"
#include "ihevc_intra_pred.h"
#include "ihevc_inter_pred.h"
#include "ihevc_mem_fns.h"
#include "ihevc_padding.h"
#include "ihevc_weighted_pred.h"
#include "ihevc_sao.h"
#include "ihevc_resi_trans.h"
#include "ihevc_quant_iquant_ssd.h"
#include "ihevc_cabac_tables.h"
#include "ihevc_trans_macros.h"
#include "ihevc_trans_tables.h"
 
#include "ihevce_defs.h"
#include "ihevce_lap_enc_structs.h"
#include "ihevce_multi_thrd_structs.h"
#include "ihevce_me_common_defs.h"
#include "ihevce_had_satd.h"
#include "ihevce_error_codes.h"
#include "ihevce_bitstream.h"
#include "ihevce_cabac.h"
#include "ihevce_rdoq_macros.h"
#include "ihevce_function_selector.h"
#include "ihevce_enc_structs.h"
#include "ihevce_entropy_structs.h"
#include "ihevce_cmn_utils_instr_set_router.h"
#include "ihevce_enc_loop_structs.h"
#include "ihevce_bs_compute_ctb.h"
#include "ihevce_global_tables.h"
#include "ihevce_common_utils.h"
#include "ihevce_trace.h"
 
/*****************************************************************************/
/* Globals                                                                   */
/*****************************************************************************/
extern UWORD16 gau2_ihevce_cabac_bin_to_bits[64 * 2];
 
/**
******************************************************************************
* @brief  LUT for deriving of last significant coeff prefix.
*
* @input   : last_significant_coeff
*
* @output  : last_significant_prefix (does not include the
*
* @remarks Look up tables taken frm HM-8.0-dev
******************************************************************************
*/
const UWORD8 gu1_hevce_last_coeff_prefix[32] = { 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
                                                 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9 };
 
/**
*****************************************************************************
* @brief  LUT for deriving of last significant coeff suffix
*
* @input   : last significant prefix
*
* @output  : prefix code that needs to be subtracted from last_pos to get
*           suffix as per equation 7-55 in section 7.4.12.
*
*           It returns the following code for last_significant_prefix > 3
*            ((1 << ((last_significant_coeff_x_prefix >> 1) - 1))  *
*            (2 + (last_significant_coeff_x_prefix & 1))
*
*
* @remarks Look up tables taken frm HM-8.0-dev
*****************************************************************************
*/
const UWORD8 gu1_hevce_last_coeff_prefix_code[10] = { 0, 1, 2, 3, 4, 6, 8, 12, 16, 24 };
 
/**
*****************************************************************************
* @brief  returns raster index of 4x4 block for diag up-right/horz/vert scans
*
* @input   : scan type and scan idx
*
* @output  : packed y pos(msb 4bit) and x pos(lsb 2bit)
*
*****************************************************************************
*/
const UWORD8 gu1_hevce_scan4x4[3][16] = {
    /* diag up right */
    { 0, 4, 1, 8, 5, 2, 12, 9, 6, 3, 13, 10, 7, 14, 11, 15 },
 
    /* horz */
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
 
    /* vert */
    { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 }
};
 
/**
*****************************************************************************
* @brief  returns context increment for sig coeff based on csbf neigbour
*         flags (bottom and right) and current coeff postion in 4x4 block
*         See section 9.3.3.1.4 for details on this context increment
*
* @input   : neigbour csbf flags(bit0:rightcsbf, bit1:bottom csbf)
*           coeff idx in raster order (0-15)
*
* @output  : context increment for sig coeff flag
*
*****************************************************************************
*/
const UWORD8 gu1_hevce_sigcoeff_ctxtinc[4][16] = {
    /* nbr csbf = 0:  sigCtx = (xP+yP == 0) ? 2 : (xP+yP < 3) ? 1: 0 */
    { 2, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
 
    /* nbr csbf = 1:  sigCtx = (yP == 0) ? 2 : (yP == 1) ? 1: 0      */
    { 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
 
    /* nbr csbf = 2:  sigCtx = (xP == 0) ? 2 : (xP == 1) ? 1: 0      */
    { 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0 },
 
    /* nbr csbf = 3:  sigCtx = 2                                     */
    { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }
};
 
const UWORD8 gu1_hevce_sigcoeff_ctxtinc_00[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
/**
*****************************************************************************
* @brief  returns context increment for sig coeff for 4x4 tranform size as
*         per Table 9-39 in section 9.3.3.1.4
*
* @input   : coeff idx in raster order (0-15)
*
* @output  : context increment for sig coeff flag
*
*****************************************************************************
*/
const UWORD8 gu1_hevce_sigcoeff_ctxtinc_tr4[16] = { 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 0 };
 
#define DISABLE_ZCSBF 0
 
#define TEST_CABAC_BITESTIMATE 0
 
/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/
/**
******************************************************************************
*
*  @brief Entropy encoding of qp_delta in a tu as per sec 9.3.2 Table 9-32
*
*  @par   Description
*  trunacted unary binarization is done based upto abs_delta of 5 and the rest
*  is coded as 0th order Exponential Golomb code
*
*  @param[inout]   ps_cabac
*  pointer to cabac encoding context (handle)
*
*  @param[in]      qp_delta
*  delta qp that needs to be encoded
*
*  @return      success or failure error code
*
******************************************************************************
*/
WORD32 ihevce_cabac_encode_qp_delta(cab_ctxt_t *ps_cabac, WORD32 qp_delta)
{
    WORD32 qp_delta_abs = ABS(qp_delta);
    WORD32 c_max = TU_MAX_QP_DELTA_ABS;
    WORD32 ctxt_inc = IHEVC_CAB_QP_DELTA_ABS;
    WORD32 ctxt_inc_max = CTXT_MAX_QP_DELTA_ABS;
    WORD32 ret = IHEVCE_SUCCESS;
 
    /* qp_delta_abs is coded as combination of tunary and eg0 code  */
    /* See Table 9-32 and Table 9-37 for details on cu_qp_delta_abs */
    ret |= ihevce_cabac_encode_tunary(
        ps_cabac, MIN(qp_delta_abs, c_max), c_max, ctxt_inc, 0, ctxt_inc_max);
    if(qp_delta_abs >= c_max)
    {
        ret |= ihevce_cabac_encode_egk(ps_cabac, qp_delta_abs - c_max, 0);
    }
    AEV_TRACE("cu_qp_delta_abs", qp_delta_abs, ps_cabac->u4_range);
 
    /* code the qp delta sign flag */
    if(qp_delta_abs)
    {
        WORD32 sign = (qp_delta < 0) ? 1 : 0;
        ret |= ihevce_cabac_encode_bypass_bin(ps_cabac, sign);
        AEV_TRACE("cu_qp_delta_sign", sign, ps_cabac->u4_range);
    }
 
    return (ret);
}
 
/**
******************************************************************************
*
*  @brief Encodes position of the last coded coeff (in scan order) of TU
*
*  @par   Description
*  Entropy encode of last coded coeff of a TU as per section:7.3.13
*
*  @param[inout]   ps_cabac
*  pointer to cabac context (handle)
*
*  @param[in]      last_coeff_x
*  x co-ordinate of the last coded coeff of TU(in scan order)
*
*  @param[in]      last_coeff_y
*  x co-ordinate of the last coded coeff of TU (in scan order
*
*  @param[in]      log2_tr_size
*  transform block size corresponding to this node in quad tree
*
*  @param[in]      is_luma
*  indicates if residual block corresponds to luma or chroma block
*
*  @return      success or failure error code
*
******************************************************************************
*/
WORD32 ihevce_cabac_encode_last_coeff_x_y(
    cab_ctxt_t *ps_cabac,
    WORD32 last_coeff_x,
    WORD32 last_coeff_y,
    WORD32 log2_tr_size,
    WORD32 is_luma)
{
    WORD32 ret = IHEVCE_SUCCESS;
 
    WORD32 last_coeff_x_prefix;
    WORD32 last_coeff_y_prefix;
    WORD32 suffix, suf_length;
    WORD32 c_max;
    WORD32 ctxt_idx_x, ctxt_idx_y, ctx_shift;
 
    /* derive the prefix code */
    last_coeff_x_prefix = gu1_hevce_last_coeff_prefix[last_coeff_x];
    last_coeff_y_prefix = gu1_hevce_last_coeff_prefix[last_coeff_y];
 
    c_max = gu1_hevce_last_coeff_prefix[(1 << log2_tr_size) - 1];
 
    /* context increment as per section 9.3.3.1.2 */
    if(is_luma)
    {
        WORD32 ctx_offset = (3 * (log2_tr_size - 2)) + ((log2_tr_size - 1) >> 2);
 
        ctxt_idx_x = IHEVC_CAB_COEFFX_PREFIX + ctx_offset;
        ctxt_idx_y = IHEVC_CAB_COEFFY_PREFIX + ctx_offset;
        ctx_shift = (log2_tr_size + 1) >> 2;
    }
    else
    {
        ctxt_idx_x = IHEVC_CAB_COEFFX_PREFIX + 15;
        ctxt_idx_y = IHEVC_CAB_COEFFY_PREFIX + 15;
        ctx_shift = log2_tr_size - 2;
    }
 
    /* code the last_coeff_x_prefix as tunary binarized code */
    ret |= ihevce_cabac_encode_tunary(
        ps_cabac, last_coeff_x_prefix, c_max, ctxt_idx_x, ctx_shift, c_max);
 
    AEV_TRACE("last_coeff_x_prefix", last_coeff_x_prefix, ps_cabac->u4_range);
 
    /* code the last_coeff_y_prefix as tunary binarized code */
    ret |= ihevce_cabac_encode_tunary(
        ps_cabac, last_coeff_y_prefix, c_max, ctxt_idx_y, ctx_shift, c_max);
 
    AEV_TRACE("last_coeff_y_prefix", last_coeff_y_prefix, ps_cabac->u4_range);
 
    if(last_coeff_x_prefix > 3)
    {
        /* code the last_coeff_x_suffix as FLC bypass code */
        suffix = last_coeff_x - gu1_hevce_last_coeff_prefix_code[last_coeff_x_prefix];
 
        suf_length = ((last_coeff_x_prefix - 2) >> 1);
 
        ret |= ihevce_cabac_encode_bypass_bins(ps_cabac, suffix, suf_length);
 
        AEV_TRACE("last_coeff_x_suffix", suffix, ps_cabac->u4_range);
    }
 
    if(last_coeff_y_prefix > 3)
    {
        /* code the last_coeff_y_suffix as FLC bypass code */
        suffix = last_coeff_y - gu1_hevce_last_coeff_prefix_code[last_coeff_y_prefix];
 
        suf_length = ((last_coeff_y_prefix - 2) >> 1);
 
        ret |= ihevce_cabac_encode_bypass_bins(ps_cabac, suffix, suf_length);
 
        AEV_TRACE("last_coeff_y_suffix", suffix, ps_cabac->u4_range);
    }
 
    return (ret);
}
 
/**
******************************************************************************
*
*  @brief Encodes a transform tree as per section 7.3.11
*
*  @par   Description
*  Uses recursion till a leaf node is reached where a transform unit
*  is coded. While recursing split_transform_flag and parent chroma cbf flags
*  are coded before recursing to leaf node
*
*  @param[inout]   ps_entropy_ctxt
*  pointer to entropy context (handle)
*
*  @param[in]      x0_ctb
*  x co-ordinate w.r.t ctb start of current tu node of coding tree
*
*  @param[in]      y0_ctb
*  y co-ordinate w.r.t ctb start of current cu node of coding tree
*
*  @param[in]      log2_tr_size
*  transform block size corresponding to this node in quad tree
*
*  @param[in]      tr_depth
*  current depth of the tree
*
*  @param[in]      tr_depth
*  current depth of the tree
*
*  @param[in]      blk_num
*  current block number in the quad tree (required for chorma 4x4 coding)
*
*  @return      success or failure error code
*
******************************************************************************
*/
WORD32 ihevce_encode_transform_tree(
    entropy_context_t *ps_entropy_ctxt,
    WORD32 x0_ctb,
    WORD32 y0_ctb,
    WORD32 log2_tr_size,
    WORD32 tr_depth,
    WORD32 blk_num,
    cu_enc_loop_out_t *ps_enc_cu)
{
    WORD32 ret = IHEVCE_SUCCESS;
    sps_t *ps_sps = ps_entropy_ctxt->ps_sps;
    WORD32 split_tr_flag;
 
    WORD32 tu_idx = ps_entropy_ctxt->i4_tu_idx;
    tu_enc_loop_out_t *ps_enc_tu = ps_enc_cu->ps_enc_tu + tu_idx;
 
    /* TU size in pels */
    WORD32 tu_size = 4 << ps_enc_tu->s_tu.b3_size;
 
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
 
    WORD32 max_tr_depth;
    WORD32 is_intra = (ps_enc_cu->b1_pred_mode_flag == PRED_MODE_INTRA);
    WORD32 log2_min_trafo_size, log2_max_trafo_size;
    UWORD32 u4_bits_estimated_prev;
 
    WORD32 intra_nxn_pu = 0;
    WORD32 ctxt_inc;
    WORD32 cbf_luma = 0;
    WORD32 ai4_cbf_cb[2] = { 0, 0 };
    WORD32 ai4_cbf_cr[2] = { 0, 0 };
    UWORD32 tu_split_bits = 0;
    UWORD8 u1_is_422 = (ps_sps->i1_chroma_format_idc == 2);
 
    tu_split_bits = ps_cabac->u4_bits_estimated_q12;
    /* intialize min / max transform sizes based on sps */
    log2_min_trafo_size = ps_sps->i1_log2_min_transform_block_size;
 
    log2_max_trafo_size = log2_min_trafo_size + ps_sps->i1_log2_diff_max_min_transform_block_size;
 
    /* intialize max transform depth for intra / inter signalled in sps */
    if(is_intra)
    {
        max_tr_depth = ps_sps->i1_max_transform_hierarchy_depth_intra;
        intra_nxn_pu = ps_enc_cu->b3_part_mode == PART_NxN;
    }
    else
    {
        max_tr_depth = ps_sps->i1_max_transform_hierarchy_depth_inter;
    }
 
    /* Sanity checks */
    ASSERT(tr_depth <= 4);
    ASSERT(log2_min_trafo_size >= 2);
    ASSERT(log2_max_trafo_size <= 5);
    ASSERT((tu_idx >= 0) && (tu_idx < ps_enc_cu->u2_num_tus_in_cu));
    ASSERT((tu_size >= 4) && (tu_size <= (1 << log2_tr_size)));
 
    /* Encode split transform flag based on following conditions; sec 7.3.11 */
    if((log2_tr_size <= log2_max_trafo_size) && (log2_tr_size > log2_min_trafo_size) &&
       (tr_depth < max_tr_depth) && (!(intra_nxn_pu && (tr_depth == 0))))
    {
        /* encode the split transform flag, context derived as per Table9-37 */
        ctxt_inc = IHEVC_CAB_SPLIT_TFM + (5 - log2_tr_size);
 
        /* split if actual tu size is smaller than target tu size */
        split_tr_flag = tu_size < (1 << log2_tr_size);
        u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
        ret |= ihevce_cabac_encode_bin(ps_cabac, split_tr_flag, ctxt_inc);
 
        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
        {  // clang-format off
            /*PIC INFO : populate cu split flag*/
            ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_split_tu_flag +=
                (ps_cabac->u4_bits_estimated_q12 - u4_bits_estimated_prev);
        }  // clang-format on
 
        AEV_TRACE("split_transform_flag", split_tr_flag, ps_cabac->u4_range);
    }
    else
    {
        WORD32 inter_split;
        /*********************************************************************/
        /*                                                                   */
        /* split tr is implicitly derived as 1 if  (see section 7.4.10)      */
        /*  a. log2_tr_size > log2_max_trafo_size                            */
        /*  b. intra cu has NXN pu                                           */
        /*  c. inter cu is not 2Nx2N && max_transform_hierarchy_depth_inter=0*/
        /*                                                                   */
        /* split tu is implicitly derived as 0 otherwise                     */
        /*********************************************************************/
        inter_split = (!is_intra) && (max_tr_depth == 0) && (tr_depth == 0) &&
                      (ps_enc_cu->b3_part_mode != PART_2Nx2N);
 
        if((log2_tr_size > log2_max_trafo_size) || (intra_nxn_pu && (tr_depth == 0)) ||
           (inter_split))
        {
            split_tr_flag = 1;
        }
        else
        {
            split_tr_flag = 0;
        }
    }
    /*accumulate only tu tree bits*/
    ps_cabac->u4_true_tu_split_flag_q12 += ps_cabac->u4_bits_estimated_q12 - tu_split_bits;
 
    /* Encode the cbf flags for chroma before the split as per sec 7.3.11   */
    if(log2_tr_size > 2)
    {
        /* encode the cbf cb, context derived as per Table 9-37 */
        ctxt_inc = IHEVC_CAB_CBCR_IDX + tr_depth;
 
        /* Note chroma cbf is coded for depth=0 or if parent cbf was coded */
        if((tr_depth == 0) || (ps_entropy_ctxt->apu1_cbf_cb[0][tr_depth - 1]) ||
           (ps_entropy_ctxt->apu1_cbf_cb[1][tr_depth - 1]))
        {
#if CABAC_BIT_EFFICIENT_CHROMA_PARENT_CBF
            /*************************************************************/
            /* Bit-Efficient chroma cbf signalling                       */
            /* if children nodes have 0 cbf parent cbf can be coded as 0 */
            /* peeking through all the child nodes for cb to check if    */
            /* parent can be coded as 0                                  */
            /*************************************************************/
            WORD32 tu_cnt = 0;
            while(1)
            {
                WORD32 trans_size = 1 << (ps_enc_tu[tu_cnt].s_tu.b3_size + 2);
                WORD32 tu_x = (ps_enc_tu[tu_cnt].s_tu.b4_pos_x << 2);
                WORD32 tu_y = (ps_enc_tu[tu_cnt].s_tu.b4_pos_y << 2);
 
                ASSERT(tu_cnt < ps_enc_cu->u2_num_tus_in_cu);
 
                if((ps_enc_tu[tu_cnt].s_tu.b1_cb_cbf) || (ps_enc_tu[tu_cnt].s_tu.b1_cb_cbf_subtu1))
                {
                    ai4_cbf_cb[0] = ps_enc_tu[tu_cnt].s_tu.b1_cb_cbf;
                    ai4_cbf_cb[1] = ps_enc_tu[tu_cnt].s_tu.b1_cb_cbf_subtu1;
                    break;
                }
 
                /* 8x8 parent has only one 4x4 valid chroma block for 420 */
                if(3 == log2_tr_size)
                    break;
 
                if((tu_x + trans_size == (x0_ctb + (1 << log2_tr_size))) &&
                   (tu_y + trans_size == (y0_ctb + (1 << log2_tr_size))))
                {
                    ai4_cbf_cb[0] = ps_enc_tu[tu_cnt].s_tu.b1_cb_cbf;
                    ai4_cbf_cb[1] = ps_enc_tu[tu_cnt].s_tu.b1_cb_cbf_subtu1;
                    ASSERT(
                        (0 == ps_enc_tu[tu_cnt].s_tu.b1_cb_cbf) &&
                        (0 == ps_enc_tu[tu_cnt].s_tu.b1_cb_cbf_subtu1));
                    break;
                }
 
                tu_cnt++;
            }
#else
            /* read cbf only when split is 0 (child node) else force cbf=1 */
            ai4_cbf_cb[0] = (split_tr_flag && (log2_tr_size > 3)) ? 1 : ps_enc_tu->s_tu.b1_cb_cbf;
            ai4_cbf_cb[1] =
                (split_tr_flag && (log2_tr_size > 3)) ? 1 : ps_enc_tu->s_tu.b1_cb_cbf_subtu1;
 
#endif
            if((u1_is_422) && ((!split_tr_flag) || (3 == log2_tr_size)))
            {
                u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
                ret |= ihevce_cabac_encode_bin(ps_cabac, ai4_cbf_cb[0], ctxt_inc);
 
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                {  // clang-format off
                    /*PIC INFO : Populate CBF cr bits*/
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cbf_chroma_bits +=
                        (ps_cabac->u4_bits_estimated_q12 -
                            u4_bits_estimated_prev);
                }  // clang-format on
 
                AEV_TRACE("cbf_cb", ai4_cbf_cb[0], ps_cabac->u4_range);
 
                u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
                ret |= ihevce_cabac_encode_bin(ps_cabac, ai4_cbf_cb[1], ctxt_inc);
 
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                {  // clang-format off
                    /*PIC INFO : Populate CBF cr bits*/
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cbf_chroma_bits +=
                        (ps_cabac->u4_bits_estimated_q12 -
                            u4_bits_estimated_prev);
                }  // clang-format on
 
                AEV_TRACE("cbf_cb", ai4_cbf_cb[1], ps_cabac->u4_range);
            }
            else
            {
                u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
                ret |= ihevce_cabac_encode_bin(ps_cabac, ai4_cbf_cb[0] || ai4_cbf_cb[1], ctxt_inc);
 
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                {  // clang-format off
                    /*PIC INFO : Populate CBF cr bits*/
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cbf_chroma_bits +=
                        (ps_cabac->u4_bits_estimated_q12 -
                            u4_bits_estimated_prev);
                }  // clang-format on
 
                AEV_TRACE("cbf_cb", ai4_cbf_cb[0] || ai4_cbf_cb[1], ps_cabac->u4_range);
            }
        }
        else
        {
            ai4_cbf_cb[0] = ps_entropy_ctxt->apu1_cbf_cb[0][tr_depth - 1];
            ai4_cbf_cb[1] = ps_entropy_ctxt->apu1_cbf_cb[1][tr_depth - 1];
        }
 
        if((tr_depth == 0) || (ps_entropy_ctxt->apu1_cbf_cr[0][tr_depth - 1]) ||
           (ps_entropy_ctxt->apu1_cbf_cr[1][tr_depth - 1]))
        {
#if CABAC_BIT_EFFICIENT_CHROMA_PARENT_CBF
            /*************************************************************/
            /* Bit-Efficient chroma cbf signalling                       */
            /* if children nodes have 0 cbf parent cbf can be coded as 0 */
            /* peeking through all the child nodes for cr to check if    */
            /* parent can be coded as 0                                  */
            /*************************************************************/
            WORD32 tu_cnt = 0;
            while(1)
            {
                WORD32 trans_size = 1 << (ps_enc_tu[tu_cnt].s_tu.b3_size + 2);
                WORD32 tu_x = (ps_enc_tu[tu_cnt].s_tu.b4_pos_x << 2);
                WORD32 tu_y = (ps_enc_tu[tu_cnt].s_tu.b4_pos_y << 2);
 
                ASSERT(tu_cnt < ps_enc_cu->u2_num_tus_in_cu);
 
                if((ps_enc_tu[tu_cnt].s_tu.b1_cr_cbf) || (ps_enc_tu[tu_cnt].s_tu.b1_cr_cbf_subtu1))
                {
                    ai4_cbf_cr[0] = ps_enc_tu[tu_cnt].s_tu.b1_cr_cbf;
                    ai4_cbf_cr[1] = ps_enc_tu[tu_cnt].s_tu.b1_cr_cbf_subtu1;
                    break;
                }
 
                /* 8x8 parent has only one 4x4 valid chroma block for 420 */
                if(3 == log2_tr_size)
                    break;
 
                if((tu_x + trans_size == (x0_ctb + (1 << log2_tr_size))) &&
                   (tu_y + trans_size == (y0_ctb + (1 << log2_tr_size))))
                {
                    ai4_cbf_cr[0] = ps_enc_tu[tu_cnt].s_tu.b1_cr_cbf;
                    ai4_cbf_cr[1] = ps_enc_tu[tu_cnt].s_tu.b1_cr_cbf_subtu1;
                    ASSERT(
                        (0 == ps_enc_tu[tu_cnt].s_tu.b1_cr_cbf) &&
                        (0 == ps_enc_tu[tu_cnt].s_tu.b1_cr_cbf_subtu1));
                    break;
                }
 
                tu_cnt++;
            }
#else
            /* read cbf only when split is 0 (child node) else force cbf=1 */
            ai4_cbf_cr[0] = (split_tr_flag && (log2_tr_size > 3)) ? 1 : ps_enc_tu->s_tu.b1_cr_cbf;
            ai4_cbf_cr[1] =
                (split_tr_flag && (log2_tr_size > 3)) ? 1 : ps_enc_tu->s_tu.b1_cr_cbf_subtu1;
#endif
 
            if((u1_is_422) && ((!split_tr_flag) || (3 == log2_tr_size)))
            {
                u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
                ret |= ihevce_cabac_encode_bin(ps_cabac, ai4_cbf_cr[0], ctxt_inc);
 
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                {  // clang-format off
                    /*PIC INFO : Populate CBF cr bits*/
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cbf_chroma_bits +=
                        (ps_cabac->u4_bits_estimated_q12 -
                            u4_bits_estimated_prev);
                }  // clang-format on
 
                AEV_TRACE("cbf_cr", ai4_cbf_cr[0], ps_cabac->u4_range);
 
                u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
                ret |= ihevce_cabac_encode_bin(ps_cabac, ai4_cbf_cr[1], ctxt_inc);
 
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                {  // clang-format off
                    /*PIC INFO : Populate CBF cr bits*/
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cbf_chroma_bits +=
                        (ps_cabac->u4_bits_estimated_q12 -
                            u4_bits_estimated_prev);
                }  // clang-format on
 
                AEV_TRACE("cbf_cr", ai4_cbf_cr[1], ps_cabac->u4_range);
            }
            else
            {
                u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
                ret |= ihevce_cabac_encode_bin(ps_cabac, ai4_cbf_cr[0] || ai4_cbf_cr[1], ctxt_inc);
 
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                {  // clang-format off
                    /*PIC INFO : Populate CBF cr bits*/
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cbf_chroma_bits +=
                        (ps_cabac->u4_bits_estimated_q12 -
                            u4_bits_estimated_prev);
                }  // clang-format on
 
                AEV_TRACE("cbf_cr", ai4_cbf_cr[0] || ai4_cbf_cr[1], ps_cabac->u4_range);
            }
        }
        else
        {
            ai4_cbf_cr[0] = ps_entropy_ctxt->apu1_cbf_cr[0][tr_depth - 1];
            ai4_cbf_cr[1] = ps_entropy_ctxt->apu1_cbf_cr[1][tr_depth - 1];
        }
 
        ps_entropy_ctxt->apu1_cbf_cb[0][tr_depth] = ai4_cbf_cb[0];
        ps_entropy_ctxt->apu1_cbf_cr[0][tr_depth] = ai4_cbf_cr[0];
        ps_entropy_ctxt->apu1_cbf_cb[1][tr_depth] = ai4_cbf_cb[1];
        ps_entropy_ctxt->apu1_cbf_cr[1][tr_depth] = ai4_cbf_cr[1];
    }
    else
    {
        ai4_cbf_cb[0] = ps_entropy_ctxt->apu1_cbf_cb[0][tr_depth - 1];
        ai4_cbf_cr[0] = ps_entropy_ctxt->apu1_cbf_cr[0][tr_depth - 1];
        ai4_cbf_cb[1] = ps_entropy_ctxt->apu1_cbf_cb[1][tr_depth - 1];
        ai4_cbf_cr[1] = ps_entropy_ctxt->apu1_cbf_cr[1][tr_depth - 1];
    }
 
    if(split_tr_flag)
    {
        /* recurse into quad child nodes till a leaf node is reached */
        WORD32 x1_ctb = x0_ctb + ((1 << log2_tr_size) >> 1);
        WORD32 y1_ctb = y0_ctb + ((1 << log2_tr_size) >> 1);
 
        /* node0 of quad tree */
        ret |= ihevce_encode_transform_tree(
            ps_entropy_ctxt,
            x0_ctb,
            y0_ctb,
            log2_tr_size - 1,
            tr_depth + 1,
            0, /* block 0 */
            ps_enc_cu);
 
        /* node1 of quad tree */
        ret |= ihevce_encode_transform_tree(
            ps_entropy_ctxt,
            x1_ctb,
            y0_ctb,
            log2_tr_size - 1,
            tr_depth + 1,
            1, /* block 1 */
            ps_enc_cu);
 
        /* node2 of quad tree */
        ret |= ihevce_encode_transform_tree(
            ps_entropy_ctxt,
            x0_ctb,
            y1_ctb,
            log2_tr_size - 1,
            tr_depth + 1,
            2, /* block 2 */
            ps_enc_cu);
 
        /* node3 of quad tree */
        ret |= ihevce_encode_transform_tree(
            ps_entropy_ctxt,
            x1_ctb,
            y1_ctb,
            log2_tr_size - 1,
            tr_depth + 1,
            3, /* block 3 */
            ps_enc_cu);
    }
    else
    {
        /* leaf node is reached! Encode the TU */
        WORD32 encode_delta_qp;
        void *pv_coeff;
        void *pv_cu_coeff = ps_enc_cu->pv_coeff;
 
        /* condition to encode qp of cu in first coded tu */
        encode_delta_qp = ps_entropy_ctxt->i1_encode_qp_delta &&
                          (ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS);
 
        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
        {  // clang-format off
            /*PIC INFO : Tota TUs based on size*/
            if(32 == tu_size)
            {
                ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3]++;
            }
            else
            {
                ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[tu_size >> 3]++;
            }
        }  // clang-format on
 
        /* sanity checks */
        ASSERT(ps_entropy_ctxt->i1_ctb_num_pcm_blks == 0);
        ASSERT((ps_enc_tu->s_tu.b4_pos_x << 2) == x0_ctb);
        ASSERT((ps_enc_tu->s_tu.b4_pos_y << 2) == y0_ctb);
        ASSERT(tu_size == (1 << log2_tr_size));
 
        /********************************************************************/
        /* encode luma cbf if any of following conditions are true          */
        /* intra cu | transform depth > 0 | any of chroma cbfs are coded    */
        /*                                                                  */
        /* Note that these conditions mean that cbf_luma need not be        */
        /* signalled and implicitly derived as 1 for inter cu whose tfr size*/
        /* is same as cu size and cbf for cb+cr are zero as no_residue_flag */
        /* at cu level = 1 indicated cbf luma is coded                      */
        /********************************************************************/
        if(is_intra || (tr_depth != 0) || ai4_cbf_cb[0] || ai4_cbf_cr[0] ||
           ((u1_is_422) && (ai4_cbf_cb[1] || ai4_cbf_cr[1])))
        {
            /* encode  cbf luma, context derived as per Table 9-37 */
            cbf_luma = ps_enc_tu->s_tu.b1_y_cbf;
 
            ctxt_inc = IHEVC_CAB_CBF_LUMA_IDX;
            ctxt_inc += (tr_depth == 0) ? 1 : 0;
 
            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
            {
                if(1 == cbf_luma)
                {
                    // clang-format off
                    /*PIC INFO: Populated coded Intra/Inter TUs in CU*/
                    if(1 == is_intra)
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_intra_coded_tu++;
                    else
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_inter_coded_tu++;
                    // clang-format on
                }
                else
                { /*PIC INFO: Populated coded non-coded TUs in CU*/
                    ps_entropy_ctxt->ps_pic_level_info->i8_total_non_coded_tu++;
                }
            }
            u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
            ret |= ihevce_cabac_encode_bin(ps_cabac, cbf_luma, ctxt_inc);
 
            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
            {  // clang-format off
                /*PIC INFO : Populate CBF luma bits*/
                ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cbf_luma_bits +=
                    (ps_cabac->u4_bits_estimated_q12 - u4_bits_estimated_prev);
            }  // clang-format on
            AEV_TRACE("cbf_luma", cbf_luma, ps_cabac->u4_range);
        }
        else
        {
            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
            {
                /*PIC INFO: Populated coded Inter TUs in CU*/
                ps_entropy_ctxt->ps_pic_level_info->i8_total_inter_coded_tu++;
            }
 
            /* shall be 1 as no_residue_flag was encoded as 1 in inter cu */
            ASSERT(1 == ps_enc_tu->s_tu.b1_y_cbf);
            cbf_luma = ps_enc_tu->s_tu.b1_y_cbf;
        }
 
        /*******************************************************************/
        /* code qp delta conditionally if following conditions are true    */
        /* any cbf coded (luma/cb/cr) and qp_delta_coded is 0 for this cu  */
        /* see section 7.3.12 Transform unit Syntax                        */
        /*******************************************************************/
        {
            WORD32 cbf_chroma = (ai4_cbf_cb[0] || ai4_cbf_cr[0]) ||
                                (u1_is_422 && (ai4_cbf_cb[1] || ai4_cbf_cr[1]));
 
            if((cbf_luma || cbf_chroma) && encode_delta_qp)
            {
                WORD32 tu_qp = ps_enc_tu->s_tu.b7_qp;
                WORD32 qp_pred, qp_left, qp_top;
                WORD32 qp_delta = tu_qp - ps_entropy_ctxt->i1_cur_qp;
                WORD32 x_nbr_indx, y_nbr_indx;
 
                /* Added code for handling the QP neighbour population depending
                   on the diff_cu_qp_delta_depth: Lokesh  */
                /* minus 2 becoz the pos_x and pos_y are given in the order of
                 * 8x8 blocks rather than pixels */
                WORD32 log2_min_cu_qp_delta_size =
                    ps_entropy_ctxt->i1_log2_ctb_size -
                    ps_entropy_ctxt->ps_pps->i1_diff_cu_qp_delta_depth;
                //WORD32 min_cu_qp_delta_size = 1 << log2_min_cu_qp_delta_size;
 
                //WORD32 curr_pos_x = ps_enc_cu->b3_cu_pos_x << 3;
                //WORD32 curr_pos_y = ps_enc_cu->b3_cu_pos_y << 3;
 
                WORD32 block_addr_align = 15 << (log2_min_cu_qp_delta_size - 3);
 
                ps_entropy_ctxt->i4_qg_pos_x = ps_enc_cu->b3_cu_pos_x & block_addr_align;
                ps_entropy_ctxt->i4_qg_pos_y = ps_enc_cu->b3_cu_pos_y & block_addr_align;
 
                x_nbr_indx = ps_entropy_ctxt->i4_qg_pos_x - 1;
                y_nbr_indx = ps_entropy_ctxt->i4_qg_pos_y - 1;
 
                if(ps_entropy_ctxt->i4_qg_pos_x > 0)
                {
                    // clang-format off
                    qp_left =
                        ps_entropy_ctxt->ai4_8x8_cu_qp[x_nbr_indx +
                                            (ps_entropy_ctxt->i4_qg_pos_y * 8)];
                    // clang-format on
                }
                if(ps_entropy_ctxt->i4_qg_pos_y > 0)
                {
                    // clang-format off
                    qp_top = ps_entropy_ctxt->ai4_8x8_cu_qp[ps_entropy_ctxt->i4_qg_pos_x +
                                                 y_nbr_indx * 8];
                    // clang-format on
                }
                if(ps_entropy_ctxt->i4_qg_pos_x == 0)
                {
                    /*previous coded Qp*/
                    qp_left = ps_entropy_ctxt->i1_cur_qp;
                }
                if(ps_entropy_ctxt->i4_qg_pos_y == 0)
                {
                    /*previous coded Qp*/
                    qp_top = ps_entropy_ctxt->i1_cur_qp;
                }
 
                qp_pred = (qp_left + qp_top + 1) >> 1;
                // clang-format off
                /* start of every frame encode qp delta wrt slice qp when entrop
                 * sync is enabled */
                if(ps_entropy_ctxt->i4_ctb_x == 0 &&
                    ps_entropy_ctxt->i4_qg_pos_x == 0 &&
                    ps_entropy_ctxt->i4_qg_pos_y == 0 &&
                    ps_entropy_ctxt->s_cabac_ctxt.i1_entropy_coding_sync_enabled_flag)
                // clang-format on
                {
                    qp_pred = ps_entropy_ctxt->ps_slice_hdr->i1_slice_qp_delta +
                              ps_entropy_ctxt->ps_pps->i1_pic_init_qp;
                }
                qp_delta = tu_qp - qp_pred;
 
                /*PIC INFO : Populate QP delta bits*/
                u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
 
                /* code the qp delta */
                ret |= ihevce_cabac_encode_qp_delta(ps_cabac, qp_delta);
 
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                {
                    // clang-format off
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_qp_delta_bits +=
                        (ps_cabac->u4_bits_estimated_q12 -
                            u4_bits_estimated_prev);
                    // clang-format on
                }
 
                ps_entropy_ctxt->i1_cur_qp = tu_qp;
                //ps_entropy_ctxt->i1_cur_qp = Qp_pred;
                ps_entropy_ctxt->i1_encode_qp_delta = 0;
                //ps_entropy_ctxt->i4_is_cu_cbf_zero = 0;
            }
 
            if(cbf_luma || cbf_chroma)
            {
                ps_entropy_ctxt->i4_is_cu_cbf_zero = 0;
            }
 
            /* code the residue of for luma and chroma tu based on cbf */
            if((cbf_luma) && (1 == ps_entropy_ctxt->i4_enable_res_encode))
            {
                u4_bits_estimated_prev = ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12;
                /* code the luma residue */
                pv_coeff = (void *)((UWORD8 *)pv_cu_coeff + ps_enc_tu->i4_luma_coeff_offset);
 
                ret |= ihevce_cabac_residue_encode(ps_entropy_ctxt, pv_coeff, log2_tr_size, 1);
 
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                {  // clang-format off
                    /*PIC INFO : Populate Residue Luma Bits*/
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_res_luma_bits +=
                        (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 -
                            u4_bits_estimated_prev);
                }  // clang-format on
            }
 
            /* code chroma residue based on tranform size                  */
            /* For Inta 4x4 pu chroma is coded after all 4 luma blks coded */
            /* Note: chroma not encoded in rdopt mode                      */
            if(((log2_tr_size > 2) || (3 == blk_num)) /* &&
                (CABAC_MODE_ENCODE_BITS == ps_cabac->e_cabac_op_mode) */
            )
            {
                WORD32 log2_chroma_tr_size;
                WORD32 i4_subtu_idx;
                void *pv_coeff_cb, *pv_coeff_cr;
 
                WORD32 i4_num_subtus = u1_is_422 + 1;
 
                if(1 == ps_entropy_ctxt->i4_enable_res_encode)
                {
                    for(i4_subtu_idx = 0; i4_subtu_idx < i4_num_subtus; i4_subtu_idx++)
                    {
                        if(ai4_cbf_cb[i4_subtu_idx])
                        {
                            /* initailize chroma transform size and coeff based
                             * on luma size */
                            if(2 == log2_tr_size)
                            {
                                /*********************************************************/
                                /* For Intra 4x4, chroma transform size is 4 and chroma  */
                                /* coeff offset is present  in the first Luma block      */
                                /*********************************************************/
                                log2_chroma_tr_size = 2;
 
                                /* -3 is for going to first luma tu of the 4 TUs in min CU */
                                pv_coeff_cb =
                                    (void
                                         *)((UWORD8 *)pv_cu_coeff + ps_enc_tu[-3].ai4_cb_coeff_offset[i4_subtu_idx]);
                            }
                            else
                            {
                                log2_chroma_tr_size = (log2_tr_size - 1);
 
                                pv_coeff_cb =
                                    (void
                                         *)((UWORD8 *)pv_cu_coeff + ps_enc_tu->ai4_cb_coeff_offset[i4_subtu_idx]);
                            }
                            // clang-format off
                            u4_bits_estimated_prev =
                                ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12;
                            // clang-format on
                            /* code the cb residue */
                            ret |= ihevce_cabac_residue_encode(
                                ps_entropy_ctxt, pv_coeff_cb, log2_chroma_tr_size, 0);
 
                            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                            {  // clang-format off
                                /*PIC INFO : Populate Residue Chroma cr Bits*/
                                ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_res_chroma_bits +=
                                    (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 -
                                        u4_bits_estimated_prev);
                            }  // clang-format on
                        }
                    }
                }
 
                if(1 == ps_entropy_ctxt->i4_enable_res_encode)
                {
                    for(i4_subtu_idx = 0; i4_subtu_idx < i4_num_subtus; i4_subtu_idx++)
                    {
                        if(ai4_cbf_cr[i4_subtu_idx])
                        {
                            /* initailize chroma transform size and coeff based on luma size */
                            if(2 == log2_tr_size)
                            {
                                /*********************************************************/
                                /* For Intra 4x4, chroma transform size is 4 and chroma  */
                                /* coeff offset is present  in the first Luma block      */
                                /*********************************************************/
                                log2_chroma_tr_size = 2;
 
                                pv_coeff_cr =
                                    (void
                                         *)((UWORD8 *)pv_cu_coeff + ps_enc_tu[-3].ai4_cr_coeff_offset[i4_subtu_idx]);
                            }
                            else
                            {
                                log2_chroma_tr_size = (log2_tr_size - 1);
 
                                pv_coeff_cr =
                                    (void
                                         *)((UWORD8 *)pv_cu_coeff + ps_enc_tu->ai4_cr_coeff_offset[i4_subtu_idx]);
                            }
                            // clang-format off
                            u4_bits_estimated_prev =
                                ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12;
                            // clang-format on
                            /* code the cb residue */
                            ret |= ihevce_cabac_residue_encode(
                                ps_entropy_ctxt, pv_coeff_cr, log2_chroma_tr_size, 0);
                            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
                            {  // clang-format off
                                /*PIC INFO : Populate Residue Chroma cr Bits*/
                                ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_res_chroma_bits +=
                                    (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 -
                                        u4_bits_estimated_prev);
                            }  // clang-format on
                        }
                    }
                }
            }
        }
 
        /* update tu_idx after encoding current tu */
        ps_entropy_ctxt->i4_tu_idx++;
    }
 
    return ret;
}
 
/**
******************************************************************************
*
*  @brief Encodes a transform residual block as per section 7.3.13
*
*  @par   Description
*   The residual block is read from a compressed coeff buffer populated during
*   the scanning of the quantized coeffs. The contents of the buffer are
*   breifly explained in param description of pv_coeff
*
*  @remarks Does not support sign data hiding and transform skip flag currently
*
*  @remarks Need to resolve the differences between JVT-J1003_d7 spec and
*           HM.8.0-dev for related abs_greater_than_1 context initialization
*           and rice_max paramtere used for coeff abs level remaining
*
*  @param[inout]   ps_entropy_ctxt
*  pointer to entropy context (handle)
*
*  @param[in]      pv_coeff
*  Compressed residue buffer containing following information:
*
*  HEADER(4 bytes) : last_coeff_x, last_coeff_y, scantype, last_subblock_num
*
*  For each 4x4 subblock starting from last_subblock_num (in scan order)
*     Read 2 bytes  : MSB 12bits (0xBAD marker), bit0 cur_csbf, bit1-2 nbr csbf
*
*    `If cur_csbf
*      Read 2 bytes : sig_coeff_map (16bits in scan_order 1:coded, 0:not coded)
*      Read 2 bytes : abs_gt1_flags (max of 8 only)
*      Read 2 bytes : coeff_sign_flags
*
*      Based on abs_gt1_flags and sig_coeff_map read remaining abs levels
*      Read 2 bytes : remaining_abs_coeffs_minus1 (this is in a loop)
*
*  @param[in]      log2_tr_size
*  transform size of the current TU
*
*  @param[in]      is_luma
*  boolean indicating if the texture type is luma / chroma
*
*
*  @return      success or failure error code
*
******************************************************************************
*/
WORD32 ihevce_cabac_residue_encode(
    entropy_context_t *ps_entropy_ctxt, void *pv_coeff, WORD32 log2_tr_size, WORD32 is_luma)
{
    WORD32 ret = IHEVCE_SUCCESS;
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
    WORD32 i4_sign_data_hiding_flag, cu_tq_bypass_flag;
 
    UWORD8 *pu1_coeff_buf_hdr = (UWORD8 *)pv_coeff;
    UWORD16 *pu2_sig_coeff_buf = (UWORD16 *)pv_coeff;
 
    /* last sig coeff indices in scan order */
    WORD32 last_sig_coeff_x = pu1_coeff_buf_hdr[0];
    WORD32 last_sig_coeff_y = pu1_coeff_buf_hdr[1];
 
    /* read the scan type : upright diag / horz / vert */
    WORD32 scan_type = pu1_coeff_buf_hdr[2];
 
    /************************************************************************/
    /* position of the last coded sub block. This sub block contains coeff  */
    /* corresponding to last_sig_coeff_x, last_sig_coeff_y. Althoug this can*/
    /* be derived here it better to be populated by scanning module         */
    /************************************************************************/
    WORD32 last_csb = pu1_coeff_buf_hdr[3];
 
    WORD32 cur_csbf = 0, nbr_csbf;
    WORD32 sig_coeff_base_ctxt; /* cabac context for sig coeff flag    */
    WORD32 abs_gt1_base_ctxt; /* cabac context for abslevel > 1 flag */
 
    WORD32 gt1_ctxt = 1; /* required for abs_gt1_ctxt modelling */
 
    WORD32 i;
 
    /* sanity checks */
    /* transform skip not supported */
    ASSERT(0 == ps_entropy_ctxt->ps_pps->i1_transform_skip_enabled_flag);
 
    cu_tq_bypass_flag = ps_entropy_ctxt->ps_pps->i1_transform_skip_enabled_flag;
 
    i4_sign_data_hiding_flag = ps_entropy_ctxt->ps_pps->i1_sign_data_hiding_flag;
 
    if(SCAN_VERT == scan_type)
    {
        /* last coeff x and y are swapped for vertical scan */
        SWAP(last_sig_coeff_x, last_sig_coeff_y);
    }
 
    /* Encode the last_sig_coeff_x and last_sig_coeff_y */
    ret |= ihevce_cabac_encode_last_coeff_x_y(
        ps_cabac, last_sig_coeff_x, last_sig_coeff_y, log2_tr_size, is_luma);
 
    /*************************************************************************/
    /* derive base context index for sig coeff as per section 9.3.3.1.4      */
    /* TODO; convert to look up based on luma/chroma, scan type and tfr size */
    /*************************************************************************/
    if(is_luma)
    {
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG;
        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG;
 
        if(3 == log2_tr_size)
        {
            /* 8x8 transform size */
            sig_coeff_base_ctxt += (scan_type == SCAN_DIAG_UPRIGHT) ? 9 : 15;
        }
        else if(3 < log2_tr_size)
        {
            /* larger transform sizes */
            sig_coeff_base_ctxt += 21;
        }
    }
    else
    {
        /* chroma context initializations */
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG + 27;
        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG + 16;
 
        if(3 == log2_tr_size)
        {
            /* 8x8 transform size */
            sig_coeff_base_ctxt += 9;
        }
        else if(3 < log2_tr_size)
        {
            /* larger transform sizes */
            sig_coeff_base_ctxt += 12;
        }
    }
 
    /* go to csbf flags */
    pu2_sig_coeff_buf = (UWORD16 *)(pu1_coeff_buf_hdr + COEFF_BUF_HEADER_LEN);
 
    /************************************************************************/
    /* encode the csbf, sig_coeff_map, abs_grt1_flags, abs_grt2_flag, sign  */
    /* and abs_coeff_remaining for each 4x4 starting from last scan to first*/
    /************************************************************************/
    for(i = last_csb; i >= 0; i--)
    {
        UWORD16 u2_marker_csbf;
        WORD32 ctxt_idx;
 
        u2_marker_csbf = *pu2_sig_coeff_buf;
        pu2_sig_coeff_buf++;
 
        /* sanity checks for marker present in every csbf flag */
        ASSERT((u2_marker_csbf >> 4) == 0xBAD);
 
        /* extract the current and neigbour csbf flags */
        cur_csbf = u2_marker_csbf & 0x1;
        nbr_csbf = (u2_marker_csbf >> 1) & 0x3;
 
        /*********************************************************************/
        /* code the csbf flags; last and first csb not sent as it is derived */
        /*********************************************************************/
        if((i < last_csb) && (i > 0))
        {
            ctxt_idx = IHEVC_CAB_CODED_SUBLK_IDX;
 
            /* ctxt based on right / bottom avail csbf, section 9.3.3.1.3 */
            ctxt_idx += nbr_csbf ? 1 : 0;
            ctxt_idx += is_luma ? 0 : 2;
 
            ret |= ihevce_cabac_encode_bin(ps_cabac, cur_csbf, ctxt_idx);
            AEV_TRACE("coded_sub_block_flag", cur_csbf, ps_cabac->u4_range);
        }
        else
        {
            /* sanity check, this csb contains the last_sig_coeff */
            if(i == last_csb)
            {
                ASSERT(cur_csbf == 1);
            }
        }
 
        if(cur_csbf)
        {
            /*****************************************************************/
            /* encode the sig coeff map as per section 7.3.13                */
            /* significant_coeff_flags: msb=coeff15-lsb=coeff0 in scan order */
            /*****************************************************************/
 
            /* Added for Sign bit data hiding*/
            WORD32 first_scan_pos = 16;
            WORD32 last_scan_pos = -1;
            WORD32 sign_hidden = 0;
 
            UWORD16 u2_gt0_flags = *pu2_sig_coeff_buf;
            WORD32 gt1_flags = *(pu2_sig_coeff_buf + 1);
            WORD32 sign_flags = *(pu2_sig_coeff_buf + 2);
 
            WORD32 sig_coeff_map = u2_gt0_flags;
 
            WORD32 gt1_bins = 0; /* bins for coeffs with abslevel > 1 */
 
            WORD32 sign_bins = 0; /* bins for sign flags of coded coeffs  */
            WORD32 num_coded = 0; /* total coeffs coded in 4x4            */
 
            WORD32 infer_coeff; /* infer when 0,0 is the only coded coeff */
            WORD32 bit; /* temp boolean */
 
            /* total count of coeffs to be coded as abs level remaining */
            WORD32 num_coeffs_remaining = 0;
 
            /* count of coeffs to be coded as  abslevel-1 */
            WORD32 num_coeffs_base1 = 0;
            WORD32 scan_pos;
            WORD32 first_gt1_coeff = 0;
 
            if((i != 0) || (0 == last_csb))
            {
                /* sanity check, atleast one coeff is coded as csbf is set */
                ASSERT(sig_coeff_map != 0);
            }
 
            pu2_sig_coeff_buf += 3;
 
            scan_pos = 15;
            if(i == last_csb)
            {
                /*************************************************************/
                /* clear last_scan_pos for last block in scan order as this  */
                /* is communicated  throught last_coeff_x and last_coeff_y   */
                /*************************************************************/
                WORD32 next_sig = CLZ(sig_coeff_map) + 1;
 
                scan_pos = WORD_SIZE - next_sig;
 
                /* prepare the bins for gt1 flags */
                EXTRACT_BIT(bit, gt1_flags, scan_pos);
 
                /* insert gt1 bin in lsb */
                gt1_bins |= bit;
 
                /* prepare the bins for sign flags */
                EXTRACT_BIT(bit, sign_flags, scan_pos);
 
                /* insert sign bin in lsb */
                sign_bins |= bit;
 
                sig_coeff_map = CLEAR_BIT(sig_coeff_map, scan_pos);
 
                if(-1 == last_scan_pos)
                    last_scan_pos = scan_pos;
 
                scan_pos--;
                num_coded++;
            }
 
            /* infer 0,0 coeff for all 4x4 blocks except fitst and last */
            infer_coeff = (i < last_csb) && (i > 0);
 
            /* encode the required sigcoeff flags (abslevel > 0)   */
            while(scan_pos >= 0)
            {
                WORD32 y_pos_x_pos;
                WORD32 sig_ctxinc = 0; /* 0 is default inc for DC coeff */
 
                WORD32 sig_coeff;
 
                EXTRACT_BIT(sig_coeff, sig_coeff_map, scan_pos);
 
                /* derive the x,y pos */
                y_pos_x_pos = gu1_hevce_scan4x4[scan_type][scan_pos];
 
                /* derive the context inc as per section 9.3.3.1.4 */
                if(2 == log2_tr_size)
                {
                    /* 4x4 transform size increment uses lookup */
                    sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc_tr4[y_pos_x_pos];
                }
                else if(scan_pos || i)
                {
                    /* ctxt for AC coeff depends on curpos and neigbour csbf */
                    sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc[nbr_csbf][y_pos_x_pos];
 
                    /* based on luma subblock pos */
                    sig_ctxinc += (i && is_luma) ? 3 : 0;
                }
                else
                {
                    /* DC coeff has fixed context for luma and chroma */
                    sig_coeff_base_ctxt = is_luma ? IHEVC_CAB_COEFF_FLAG
                                                  : IHEVC_CAB_COEFF_FLAG + 27;
                }
 
                /*************************************************************/
                /* encode sig coeff only if required                         */
                /* decoder infers 0,0 coeff when all the other coeffs are 0  */
                /*************************************************************/
                if(scan_pos || (!infer_coeff))
                {
                    ctxt_idx = sig_ctxinc + sig_coeff_base_ctxt;
                    ret |= ihevce_cabac_encode_bin(ps_cabac, sig_coeff, ctxt_idx);
                    AEV_TRACE("significant_coeff_flag", sig_coeff, ps_cabac->u4_range);
                }
 
                if(sig_coeff)
                {
                    /* prepare the bins for gt1 flags */
                    EXTRACT_BIT(bit, gt1_flags, scan_pos);
 
                    /* shift and insert gt1 bin in lsb */
                    gt1_bins <<= 1;
                    gt1_bins |= bit;
 
                    /* prepare the bins for sign flags */
                    EXTRACT_BIT(bit, sign_flags, scan_pos);
 
                    /* shift and insert sign bin in lsb */
                    sign_bins <<= 1;
                    sign_bins |= bit;
 
                    num_coded++;
 
                    /* 0,0 coeff can no more be inferred :( */
                    infer_coeff = 0;
 
                    if(-1 == last_scan_pos)
                        last_scan_pos = scan_pos;
 
                    first_scan_pos = scan_pos;
                }
 
                scan_pos--;
            }
 
            /* Added for sign bit hiding*/
            sign_hidden = ((last_scan_pos - first_scan_pos) > 3 && !cu_tq_bypass_flag);
 
            /****************************************************************/
            /* encode the abs level greater than 1 bins; Section 7.3.13     */
            /* These have already been prepared during sig_coeff_map encode */
            /* Context modelling done as per section 9.3.3.1.5              */
            /****************************************************************/
            {
                WORD32 j;
 
                /* context set based on luma subblock pos */
                WORD32 ctxt_set = (i && is_luma) ? 2 : 0;
 
                /* count of coeffs with abslevel > 1; max of 8 to be coded */
                WORD32 num_gt1_bins = MIN(8, num_coded);
 
                if(num_coded > 8)
                {
                    /* pull back the bins to required number */
                    gt1_bins >>= (num_coded - 8);
 
                    num_coeffs_remaining += (num_coded - 8);
                    num_coeffs_base1 = (num_coded - 8);
                }
 
                /* See section 9.3.3.1.5           */
                ctxt_set += (0 == gt1_ctxt) ? 1 : 0;
 
                gt1_ctxt = 1;
 
                for(j = num_gt1_bins - 1; j >= 0; j--)
                {
                    /* Encodet the abs level gt1 bins */
                    ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
 
                    EXTRACT_BIT(bit, gt1_bins, j);
 
                    ret |= ihevce_cabac_encode_bin(ps_cabac, bit, ctxt_idx);
 
                    AEV_TRACE("coeff_abs_level_greater1_flag", bit, ps_cabac->u4_range);
 
                    if(bit)
                    {
                        gt1_ctxt = 0;
                        num_coeffs_remaining++;
                    }
                    else if(gt1_ctxt && (gt1_ctxt < 3))
                    {
                        gt1_ctxt++;
                    }
                }
 
                /*************************************************************/
                /* encode abs level greater than 2 bin; Section 7.3.13       */
                /*************************************************************/
                if(gt1_bins)
                {
                    WORD32 gt2_bin;
 
                    first_gt1_coeff = pu2_sig_coeff_buf[0] + 1;
                    gt2_bin = (first_gt1_coeff > 2);
 
                    /* atleast one level > 2 */
                    ctxt_idx = IHEVC_CAB_COEFABS_GRTR2_FLAG;
 
                    ctxt_idx += (is_luma) ? ctxt_set : (ctxt_set + 4);
 
                    ret |= ihevce_cabac_encode_bin(ps_cabac, gt2_bin, ctxt_idx);
 
                    if(!gt2_bin)
                    {
                        /* sanity check */
                        ASSERT(first_gt1_coeff == 2);
 
                        /* no need to send this coeff as bypass bins */
                        pu2_sig_coeff_buf++;
                        num_coeffs_remaining--;
                    }
 
                    AEV_TRACE("coeff_abs_level_greater2_flag", gt2_bin, ps_cabac->u4_range);
                }
            }
 
            /*************************************************************/
            /* encode the coeff signs and abs remaing levels             */
            /*************************************************************/
            if(num_coded)
            {
                WORD32 base_level;
                WORD32 rice_param = 0;
                WORD32 j;
 
                /*************************************************************/
                /* encode the coeff signs populated in sign_bins             */
                /*************************************************************/
 
                if(sign_hidden && i4_sign_data_hiding_flag)
                {
                    sign_bins >>= 1;
                    num_coded--;
                }
 
                if(num_coded > 0)
                {
                    ret |= ihevce_cabac_encode_bypass_bins(ps_cabac, sign_bins, num_coded);
                }
 
                AEV_TRACE("sign_flags", sign_bins, ps_cabac->u4_range);
 
                /*************************************************************/
                /* encode the coeff_abs_level_remaining as TR / EGK bins     */
                /* See section 9.3.2.7 for details                           */
                /*************************************************************/
 
                /* first remaining coeff baselevel */
                if(first_gt1_coeff > 2)
                {
                    base_level = 3;
                }
                else if(num_coeffs_remaining > num_coeffs_base1)
                {
                    /* atleast one coeff in first 8 is gt > 1 */
                    base_level = 2;
                }
                else
                {
                    /* all coeffs have base of 1 */
                    base_level = 1;
                }
 
                for(j = 0; j < num_coeffs_remaining; j++)
                {
                    WORD32 abs_coeff = pu2_sig_coeff_buf[0] + 1;
                    WORD32 abs_coeff_rem;
                    WORD32 rice_max = (4 << rice_param);
 
                    pu2_sig_coeff_buf++;
 
                    /* sanity check */
                    ASSERT(abs_coeff >= base_level);
 
                    abs_coeff_rem = (abs_coeff - base_level);
 
                    /* TODO://HM-8.0-dev uses (3 << rice_param) as rice_max */
                    /* TODO://HM-8.0-dev does either TR or EGK but not both */
                    if(abs_coeff_rem >= rice_max)
                    {
                        UWORD32 u4_suffix = (abs_coeff_rem - rice_max);
 
                        /* coeff exceeds max rice limit                    */
                        /* encode the TR prefix as tunary code             */
                        /* prefix = 1111 as (rice_max >> rice_praram) = 4  */
                        ret |= ihevce_cabac_encode_bypass_bins(ps_cabac, 0xF, 4);
 
                        /* encode the exponential golomb code suffix */
                        ret |= ihevce_cabac_encode_egk(ps_cabac, u4_suffix, (rice_param + 1));
                    }
                    else
                    {
                        /* code coeff as truncated rice code  */
                        ret |= ihevce_cabac_encode_trunc_rice(
                            ps_cabac, abs_coeff_rem, rice_param, rice_max);
                    }
 
                    AEV_TRACE("coeff_abs_level_remaining", abs_coeff_rem, ps_cabac->u4_range);
 
                    /* update the rice param based on coeff level */
                    if((abs_coeff > (3 << rice_param)) && (rice_param < 4))
                    {
                        rice_param++;
                    }
 
                    /* change base level to 1 if more than 8 coded coeffs */
                    if((j + 1) < (num_coeffs_remaining - num_coeffs_base1))
                    {
                        base_level = 2;
                    }
                    else
                    {
                        base_level = 1;
                    }
                }
            }
        }
    }
    /*tap texture bits*/
    if(ps_cabac->e_cabac_op_mode == CABAC_MODE_COMPUTE_BITS)
    {  // clang-format off
        ps_cabac->u4_texture_bits_estimated_q12 +=
            (ps_cabac->u4_bits_estimated_q12 -
                ps_cabac->u4_header_bits_estimated_q12);  //(ps_cabac->u4_bits_estimated_q12 - temp_tex_bits_q12);
    }  // clang-format on
 
    return (ret);
}
 
/**
******************************************************************************
*
*  @brief Get the bits estimate for a transform residual block as per section
*   7.3.13
*
*  @par   Description
*   The residual block is read from a compressed coeff buffer populated during
*   the scanning of the quantized coeffs. The contents of the buffer are
*   breifly explained in param description of pv_coeff
*
*  @remarks Does not support sign data hiding and transform skip flag currently
*
*  @remarks Need to resolve the differences between JVT-J1003_d7 spec and
*           HM.8.0-dev for related abs_greater_than_1 context initialization
*           and rice_max paramtere used for coeff abs level remaining
*
*  @param[inout]   ps_entropy_ctxt
*  pointer to entropy context (handle)
*
*  @param[in]      pv_coeff
*  Compressed residue buffer containing following information:
*
*  HEADER(4 bytes) : last_coeff_x, last_coeff_y, scantype, last_subblock_num
*
*  For each 4x4 subblock starting from last_subblock_num (in scan order)
*     Read 2 bytes  : MSB 12bits (0xBAD marker), bit0 cur_csbf, bit1-2 nbr csbf
*
*    `If cur_csbf
*      Read 2 bytes : sig_coeff_map (16bits in scan_order 1:coded, 0:not coded)
*      Read 2 bytes : abs_gt1_flags (max of 8 only)
*      Read 2 bytes : coeff_sign_flags
*
*      Based on abs_gt1_flags and sig_coeff_map read remaining abs levels
*      Read 2 bytes : remaining_abs_coeffs_minus1 (this is in a loop)
*
*  @param[in]      log2_tr_size
*  transform size of the current TU
*
*  @param[in]      is_luma
*  boolean indicating if the texture type is luma / chroma
*
*
*  @return      success or failure error code
*
******************************************************************************
*/
WORD32 ihevce_cabac_residue_encode_rdopt(
    entropy_context_t *ps_entropy_ctxt,
    void *pv_coeff,
    WORD32 log2_tr_size,
    WORD32 is_luma,
    WORD32 perform_sbh)
{
    WORD32 ret = IHEVCE_SUCCESS;
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
    UWORD32 temp_tex_bits_q12;
    WORD32 i4_sign_data_hiding_flag, cu_tq_bypass_flag;
 
    UWORD8 *pu1_coeff_buf_hdr = (UWORD8 *)pv_coeff;
    UWORD16 *pu2_sig_coeff_buf = (UWORD16 *)pv_coeff;
 
    /* last sig coeff indices in scan order */
    WORD32 last_sig_coeff_x = pu1_coeff_buf_hdr[0];
    WORD32 last_sig_coeff_y = pu1_coeff_buf_hdr[1];
 
    /* read the scan type : upright diag / horz / vert */
    WORD32 scan_type = pu1_coeff_buf_hdr[2];
 
    /************************************************************************/
    /* position of the last coded sub block. This sub block contains coeff  */
    /* corresponding to last_sig_coeff_x, last_sig_coeff_y. Althoug this can*/
    /* be derived here it better to be populated by scanning module         */
    /************************************************************************/
    WORD32 last_csb = pu1_coeff_buf_hdr[3];
 
    WORD32 cur_csbf = 0, nbr_csbf;
    WORD32 sig_coeff_base_ctxt; /* cabac context for sig coeff flag    */
    WORD32 abs_gt1_base_ctxt; /* cabac context for abslevel > 1 flag */
 
    WORD32 gt1_ctxt = 1; /* required for abs_gt1_ctxt modelling */
 
    WORD32 i;
 
    UWORD8 *pu1_ctxt_model = &ps_cabac->au1_ctxt_models[0];
 
    /* sanity checks */
    /* transform skip not supported */
    ASSERT(0 == ps_entropy_ctxt->ps_pps->i1_transform_skip_enabled_flag);
 
    cu_tq_bypass_flag = ps_entropy_ctxt->ps_pps->i1_transform_skip_enabled_flag;
 
    i4_sign_data_hiding_flag = ps_entropy_ctxt->ps_pps->i1_sign_data_hiding_flag;
 
    {
        temp_tex_bits_q12 = ps_cabac->u4_bits_estimated_q12;
    }
 
    if(SCAN_VERT == scan_type)
    {
        /* last coeff x and y are swapped for vertical scan */
        SWAP(last_sig_coeff_x, last_sig_coeff_y);
    }
 
    /* Encode the last_sig_coeff_x and last_sig_coeff_y */
    ret |= ihevce_cabac_encode_last_coeff_x_y(
        ps_cabac, last_sig_coeff_x, last_sig_coeff_y, log2_tr_size, is_luma);
 
    /*************************************************************************/
    /* derive base context index for sig coeff as per section 9.3.3.1.4      */
    /* TODO; convert to look up based on luma/chroma, scan type and tfr size */
    /*************************************************************************/
    if(is_luma)
    {
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG;
        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG;
 
        if(3 == log2_tr_size)
        {
            /* 8x8 transform size */
            sig_coeff_base_ctxt += (scan_type == SCAN_DIAG_UPRIGHT) ? 9 : 15;
        }
        else if(3 < log2_tr_size)
        {
            /* larger transform sizes */
            sig_coeff_base_ctxt += 21;
        }
    }
    else
    {
        /* chroma context initializations */
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG + 27;
        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG + 16;
 
        if(3 == log2_tr_size)
        {
            /* 8x8 transform size */
            sig_coeff_base_ctxt += 9;
        }
        else if(3 < log2_tr_size)
        {
            /* larger transform sizes */
            sig_coeff_base_ctxt += 12;
        }
    }
 
    /* go to csbf flags */
    pu2_sig_coeff_buf = (UWORD16 *)(pu1_coeff_buf_hdr + COEFF_BUF_HEADER_LEN);
 
    /************************************************************************/
    /* encode the csbf, sig_coeff_map, abs_grt1_flags, abs_grt2_flag, sign  */
    /* and abs_coeff_remaining for each 4x4 starting from last scan to first*/
    /************************************************************************/
    for(i = last_csb; i >= 0; i--)
    {
        UWORD16 u2_marker_csbf;
        WORD32 ctxt_idx;
 
        u2_marker_csbf = *pu2_sig_coeff_buf;
        pu2_sig_coeff_buf++;
 
        /* sanity checks for marker present in every csbf flag */
        ASSERT((u2_marker_csbf >> 4) == 0xBAD);
 
        /* extract the current and neigbour csbf flags */
        cur_csbf = u2_marker_csbf & 0x1;
        nbr_csbf = (u2_marker_csbf >> 1) & 0x3;
 
        /*********************************************************************/
        /* code the csbf flags; last and first csb not sent as it is derived */
        /*********************************************************************/
        if((i < last_csb) && (i > 0))
        {
            ctxt_idx = IHEVC_CAB_CODED_SUBLK_IDX;
 
            /* ctxt based on right / bottom avail csbf, section 9.3.3.1.3 */
            ctxt_idx += nbr_csbf ? 1 : 0;
            ctxt_idx += is_luma ? 0 : 2;
 
            {
                WORD32 state_mps = pu1_ctxt_model[ctxt_idx];
 
                /* increment bits generated based on state and bin encoded */
                ps_cabac->u4_bits_estimated_q12 +=
                    gau2_ihevce_cabac_bin_to_bits[state_mps ^ cur_csbf];
 
                /* update the context model from state transition LUT */
                pu1_ctxt_model[ctxt_idx] = gau1_ihevc_next_state[(state_mps << 1) | cur_csbf];
            }
        }
        else
        {
            /* sanity check, this csb contains the last_sig_coeff */
            if(i == last_csb)
            {
                ASSERT(cur_csbf == 1);
            }
        }
 
        if(cur_csbf)
        {
            /*****************************************************************/
            /* encode the sig coeff map as per section 7.3.13                */
            /* significant_coeff_flags: msb=coeff15-lsb=coeff0 in scan order */
            /*****************************************************************/
 
            /* Added for Sign bit data hiding*/
            WORD32 first_scan_pos = 16;
            WORD32 last_scan_pos = -1;
            WORD32 sign_hidden;
 
            UWORD16 u2_gt0_flags = *pu2_sig_coeff_buf;
            WORD32 gt1_flags = *(pu2_sig_coeff_buf + 1);
            WORD32 sign_flags = *(pu2_sig_coeff_buf + 2);
 
            WORD32 sig_coeff_map = u2_gt0_flags;
 
            WORD32 gt1_bins = 0; /* bins for coeffs with abslevel > 1 */
 
            WORD32 sign_bins = 0; /* bins for sign flags of coded coeffs  */
            WORD32 num_coded = 0; /* total coeffs coded in 4x4            */
 
            WORD32 infer_coeff; /* infer when 0,0 is the only coded coeff */
            WORD32 bit; /* temp boolean */
 
            /* total count of coeffs to be coded as abs level remaining */
            WORD32 num_coeffs_remaining = 0;
 
            /* count of coeffs to be coded as  abslevel-1 */
            WORD32 num_coeffs_base1 = 0;
            WORD32 scan_pos;
            WORD32 first_gt1_coeff = 0;
 
            if((i != 0) || (0 == last_csb))
            {
                /* sanity check, atleast one coeff is coded as csbf is set */
                ASSERT(sig_coeff_map != 0);
            }
 
            pu2_sig_coeff_buf += 3;
 
            scan_pos = 15;
            if(i == last_csb)
            {
                /*************************************************************/
                /* clear last_scan_pos for last block in scan order as this  */
                /* is communicated  throught last_coeff_x and last_coeff_y   */
                /*************************************************************/
                WORD32 next_sig = CLZ(sig_coeff_map) + 1;
 
                scan_pos = WORD_SIZE - next_sig;
 
                /* prepare the bins for gt1 flags */
                EXTRACT_BIT(bit, gt1_flags, scan_pos);
 
                /* insert gt1 bin in lsb */
                gt1_bins |= bit;
 
                /* prepare the bins for sign flags */
                EXTRACT_BIT(bit, sign_flags, scan_pos);
 
                /* insert sign bin in lsb */
                sign_bins |= bit;
 
                sig_coeff_map = CLEAR_BIT(sig_coeff_map, scan_pos);
 
                if(-1 == last_scan_pos)
                    last_scan_pos = scan_pos;
 
                scan_pos--;
                num_coded++;
            }
 
            /* infer 0,0 coeff for all 4x4 blocks except fitst and last */
            infer_coeff = (i < last_csb) && (i > 0);
 
            /* encode the required sigcoeff flags (abslevel > 0)   */
            while(scan_pos >= 0)
            {
                WORD32 y_pos_x_pos;
                WORD32 sig_ctxinc = 0; /* 0 is default inc for DC coeff */
 
                WORD32 sig_coeff;
 
                EXTRACT_BIT(sig_coeff, sig_coeff_map, scan_pos);
 
                /* derive the x,y pos */
                y_pos_x_pos = gu1_hevce_scan4x4[scan_type][scan_pos];
 
                /* derive the context inc as per section 9.3.3.1.4 */
                if(2 == log2_tr_size)
                {
                    /* 4x4 transform size increment uses lookup */
                    sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc_tr4[y_pos_x_pos];
                }
                else if(scan_pos || i)
                {
                    /* ctxt for AC coeff depends on curpos and neigbour csbf */
                    sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc[nbr_csbf][y_pos_x_pos];
 
                    /* based on luma subblock pos */
                    sig_ctxinc += (i && is_luma) ? 3 : 0;
                }
                else
                {
                    /* DC coeff has fixed context for luma and chroma */
                    sig_coeff_base_ctxt = is_luma ? IHEVC_CAB_COEFF_FLAG
                                                  : IHEVC_CAB_COEFF_FLAG + 27;
                }
 
                /*************************************************************/
                /* encode sig coeff only if required                         */
                /* decoder infers 0,0 coeff when all the other coeffs are 0  */
                /*************************************************************/
                if(scan_pos || (!infer_coeff))
                {
                    ctxt_idx = sig_ctxinc + sig_coeff_base_ctxt;
 
                    //ret |= ihevce_cabac_encode_bin(ps_cabac, sig_coeff, ctxt_idx);
                    {
                        WORD32 state_mps = pu1_ctxt_model[ctxt_idx];
 
                        /* increment bits generated based on state and bin encoded */
                        ps_cabac->u4_bits_estimated_q12 +=
                            gau2_ihevce_cabac_bin_to_bits[state_mps ^ sig_coeff];
 
                        /* update the context model from state transition LUT */
                        pu1_ctxt_model[ctxt_idx] =
                            gau1_ihevc_next_state[(state_mps << 1) | sig_coeff];
                    }
                }
 
                if(sig_coeff)
                {
                    /* prepare the bins for gt1 flags */
                    EXTRACT_BIT(bit, gt1_flags, scan_pos);
 
                    /* shift and insert gt1 bin in lsb */
                    gt1_bins <<= 1;
                    gt1_bins |= bit;
 
                    /* prepare the bins for sign flags */
                    EXTRACT_BIT(bit, sign_flags, scan_pos);
 
                    /* shift and insert sign bin in lsb */
                    sign_bins <<= 1;
                    sign_bins |= bit;
 
                    num_coded++;
 
                    /* 0,0 coeff can no more be inferred :( */
                    infer_coeff = 0;
 
                    if(-1 == last_scan_pos)
                        last_scan_pos = scan_pos;
 
                    first_scan_pos = scan_pos;
                }
 
                scan_pos--;
            }
 
            /* Added for sign bit hiding*/
            sign_hidden =
                (((last_scan_pos - first_scan_pos) > 3 && !cu_tq_bypass_flag) && (perform_sbh));
 
            /****************************************************************/
            /* encode the abs level greater than 1 bins; Section 7.3.13     */
            /* These have already been prepared during sig_coeff_map encode */
            /* Context modelling done as per section 9.3.3.1.5              */
            /****************************************************************/
            {
                WORD32 j;
 
                /* context set based on luma subblock pos */
                WORD32 ctxt_set = (i && is_luma) ? 2 : 0;
 
                /* count of coeffs with abslevel > 1; max of 8 to be coded */
                WORD32 num_gt1_bins = MIN(8, num_coded);
 
                if(num_coded > 8)
                {
                    /* pull back the bins to required number */
                    gt1_bins >>= (num_coded - 8);
 
                    num_coeffs_remaining += (num_coded - 8);
                    num_coeffs_base1 = (num_coded - 8);
                }
 
                /* See section 9.3.3.1.5           */
                ctxt_set += (0 == gt1_ctxt) ? 1 : 0;
 
                gt1_ctxt = 1;
 
                for(j = num_gt1_bins - 1; j >= 0; j--)
                {
                    /* Encodet the abs level gt1 bins */
                    ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
 
                    EXTRACT_BIT(bit, gt1_bins, j);
 
                    //ret |= ihevce_cabac_encode_bin(ps_cabac, bit, ctxt_idx);
                    {
                        WORD32 state_mps = pu1_ctxt_model[ctxt_idx];
 
                        /* increment bits generated based on state and bin encoded */
                        ps_cabac->u4_bits_estimated_q12 +=
                            gau2_ihevce_cabac_bin_to_bits[state_mps ^ bit];
 
                        /* update the context model from state transition LUT */
                        pu1_ctxt_model[ctxt_idx] = gau1_ihevc_next_state[(state_mps << 1) | bit];
                    }
 
                    if(bit)
                    {
                        gt1_ctxt = 0;
                        num_coeffs_remaining++;
                    }
                    else if(gt1_ctxt && (gt1_ctxt < 3))
                    {
                        gt1_ctxt++;
                    }
                }
 
                /*************************************************************/
                /* encode abs level greater than 2 bin; Section 7.3.13       */
                /*************************************************************/
                if(gt1_bins)
                {
                    WORD32 gt2_bin;
 
                    first_gt1_coeff = pu2_sig_coeff_buf[0] + 1;
                    gt2_bin = (first_gt1_coeff > 2);
 
                    /* atleast one level > 2 */
                    ctxt_idx = IHEVC_CAB_COEFABS_GRTR2_FLAG;
 
                    ctxt_idx += (is_luma) ? ctxt_set : (ctxt_set + 4);
 
                    //ret |= ihevce_cabac_encode_bin(ps_cabac, gt2_bin, ctxt_idx);
                    {
                        WORD32 state_mps = pu1_ctxt_model[ctxt_idx];
 
                        /* increment bits generated based on state and bin encoded */
                        ps_cabac->u4_bits_estimated_q12 +=
                            gau2_ihevce_cabac_bin_to_bits[state_mps ^ gt2_bin];
 
                        /* update the context model from state transition LUT */
                        pu1_ctxt_model[ctxt_idx] =
                            gau1_ihevc_next_state[(state_mps << 1) | gt2_bin];
                    }
 
                    if(!gt2_bin)
                    {
                        /* sanity check */
                        ASSERT(first_gt1_coeff == 2);
 
                        /* no need to send this coeff as bypass bins */
                        pu2_sig_coeff_buf++;
                        num_coeffs_remaining--;
                    }
                }
            }
 
            /*************************************************************/
            /* encode the coeff signs and abs remaing levels             */
            /*************************************************************/
            if(num_coded)
            {
                WORD32 base_level;
                WORD32 rice_param = 0;
                WORD32 j;
 
                /*************************************************************/
                /* encode the coeff signs populated in sign_bins             */
                /*************************************************************/
                if(sign_hidden && i4_sign_data_hiding_flag)
                {
                    sign_bins >>= 1;
                    num_coded--;
                }
 
                if(num_coded > 0)
                {
                    /* ret |= ihevce_cabac_encode_bypass_bins(ps_cabac,
                                                       sign_bins,
                                                       num_coded);
                    */
 
                    /* increment bits generated based on num bypass bins */
                    ps_cabac->u4_bits_estimated_q12 += (num_coded << CABAC_FRAC_BITS_Q);
                }
 
                /*************************************************************/
                /* encode the coeff_abs_level_remaining as TR / EGK bins     */
                /* See section 9.3.2.7 for details                           */
                /*************************************************************/
 
                /* first remaining coeff baselevel */
                if(first_gt1_coeff > 2)
                {
                    base_level = 3;
                }
                else if(num_coeffs_remaining > num_coeffs_base1)
                {
                    /* atleast one coeff in first 8 is gt > 1 */
                    base_level = 2;
                }
                else
                {
                    /* all coeffs have base of 1 */
                    base_level = 1;
                }
 
                for(j = 0; j < num_coeffs_remaining; j++)
                {
                    WORD32 abs_coeff = pu2_sig_coeff_buf[0] + 1;
                    WORD32 abs_coeff_rem;
                    WORD32 rice_max = (4 << rice_param);
                    WORD32 num_bins, unary_length;
                    UWORD32 u4_sym_shiftk_plus1;
 
                    pu2_sig_coeff_buf++;
 
                    /* sanity check */
                    ASSERT(abs_coeff >= base_level);
 
                    abs_coeff_rem = (abs_coeff - base_level);
 
                    /* TODO://HM-8.0-dev uses (3 << rice_param) as rice_max */
                    /* TODO://HM-8.0-dev does either TR or EGK but not both */
                    if(abs_coeff_rem >= rice_max)
                    {
                        UWORD32 u4_suffix = (abs_coeff_rem - rice_max);
 
                        /* coeff exceeds max rice limit                    */
                        /* encode the TR prefix as tunary code             */
                        /* prefix = 1111 as (rice_max >> rice_praram) = 4  */
                        /* ret |= ihevce_cabac_encode_bypass_bins(ps_cabac, 0xF, 4); */
 
                        /* increment bits generated based on num bypass bins */
                        ps_cabac->u4_bits_estimated_q12 += (4 << CABAC_FRAC_BITS_Q);
 
                        /* encode the exponential golomb code suffix */
                        /*ret |= ihevce_cabac_encode_egk(ps_cabac,
                                                       u4_suffix,
                                                       (rice_param+1)
                                                      ); */
 
                        /* k = rice_param+1 */
                        /************************************************************************/
                        /* shift symbol by k bits to find unary code prefix (111110)            */
                        /* Use GETRANGE to elminate the while loop in sec 9.3.2.4 of HEVC spec  */
                        /************************************************************************/
                        u4_sym_shiftk_plus1 = (u4_suffix >> (rice_param + 1)) + 1;
 
                        /* GETRANGE(unary_length, (u4_sym_shiftk_plus1 + 1)); */
                        GETRANGE(unary_length, u4_sym_shiftk_plus1);
 
                        /* length of the code = 2 *(unary_length - 1) + 1 + k */
                        num_bins = (2 * unary_length) + rice_param;
 
                        /* increment bits generated based on num bypass bins */
                        ps_cabac->u4_bits_estimated_q12 += (num_bins << CABAC_FRAC_BITS_Q);
                    }
                    else
                    {
                        /* code coeff as truncated rice code  */
                        /* ret |= ihevce_cabac_encode_trunc_rice(ps_cabac,
                                                              abs_coeff_rem,
                                                              rice_param,
                                                              rice_max);
                                                              */
 
                        /************************************************************************/
                        /* shift symbol by c_rice_param bits to find unary code prefix (111.10) */
                        /************************************************************************/
                        unary_length = (abs_coeff_rem >> rice_param) + 1;
 
                        /* length of the code */
                        num_bins = unary_length + rice_param;
 
                        /* increment bits generated based on num bypass bins */
                        ps_cabac->u4_bits_estimated_q12 += (num_bins << CABAC_FRAC_BITS_Q);
                    }
 
                    /* update the rice param based on coeff level */
                    if((abs_coeff > (3 << rice_param)) && (rice_param < 4))
                    {
                        rice_param++;
                    }
 
                    /* change base level to 1 if more than 8 coded coeffs */
                    if((j + 1) < (num_coeffs_remaining - num_coeffs_base1))
                    {
                        base_level = 2;
                    }
                    else
                    {
                        base_level = 1;
                    }
                }
            }
        }
    }
    /*tap texture bits*/
    {
        ps_cabac->u4_texture_bits_estimated_q12 +=
            (ps_cabac->u4_bits_estimated_q12 - temp_tex_bits_q12);
    }
 
    return (ret);
}
 
/**
******************************************************************************
*
*  @brief Encodes a transform residual block as per section 7.3.13
*
*  @par   Description
*  RDOQ optimization is carried out here. When sub-blk RDOQ is turned on, we calculate
*  the distortion(D) and bits(R) for when the sub blk is coded and when not coded. We
*  then use the D+lambdaR metric to decide whether the sub-blk should be coded or not, and
*  aprropriately signal it. When coeff RDOQ is turned on, we traverse through the TU to
*  find all non-zero coeffs. If the non zero coeff is a 1, then we make a decision(based on D+lambdaR)
*  metric as to whether to code it as a 0 or 1. In case the coeff is > 1(say L where L>1) we choose betweem
*  L and L+1
*
*  @remarks Does not support sign data hiding and transform skip flag currently
*
*  @remarks Need to resolve the differences between JVT-J1003_d7 spec and
*           HM.8.0-dev for related abs_greater_than_1 context initialization
*           and rice_max paramtere used for coeff abs level remaining
*
*  @param[inout]   ps_entropy_ctxt
*  pointer to entropy context (handle)
*
*  @param[in]      pv_coeff
*  Compressed residue buffer containing following information:
*
*
*  HEADER(4 bytes) : last_coeff_x, last_coeff_y, scantype, last_subblock_num
*
*  For each 4x4 subblock starting from last_subblock_num (in scan order)
*     Read 2 bytes  : MSB 12bits (0xBAD marker), bit0 cur_csbf, bit1-2 nbr csbf
*
*    `If cur_csbf
*      Read 2 bytes : sig_coeff_map (16bits in scan_order 1:coded, 0:not coded)
*      Read 2 bytes : abs_gt1_flags (max of 8 only)
*      Read 2 bytes : coeff_sign_flags
*
*      Based on abs_gt1_flags and sig_coeff_map read remaining abs levels
*      Read 2 bytes : remaining_abs_coeffs_minus1 (this is in a loop)
*
*  @param[in]      log2_tr_size
*  transform size of the current TU
*
*  @param[in]      is_luma
*  boolean indicating if the texture type is luma / chroma
*
*  @param[out]    pi4_tu_coded_dist
*  The distortion when the TU is coded(not all coeffs are set to 0) is stored here
*
*  @param[out]    pi4_tu_not_coded_dist
*  The distortion when the entire TU is not coded(all coeffs are set to 0) is stored here
*
*
*  @return      success or failure error code
*
******************************************************************************
*/
 
WORD32 ihevce_cabac_residue_encode_rdoq(
    entropy_context_t *ps_entropy_ctxt,
    void *pv_coeff,
    WORD32 log2_tr_size,
    WORD32 is_luma,
    void *pv_rdoq_ctxt,
    LWORD64 *pi8_tu_coded_dist,
    LWORD64 *pi8_tu_not_coded_dist,
    WORD32 perform_sbh)
{
    WORD32 *pi4_subBlock2csbfId_map;
 
    WORD32 ret = IHEVCE_SUCCESS;
 
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
    cab_ctxt_t s_sub_blk_not_coded_cabac_ctxt;
    backup_ctxt_t s_backup_ctxt;
    backup_ctxt_t s_backup_ctxt_sub_blk_not_coded;
 
    UWORD32 temp_tex_bits_q12;
 
    UWORD8 *pu1_coeff_buf_hdr = (UWORD8 *)pv_coeff;
    UWORD16 *pu2_sig_coeff_buf = (UWORD16 *)pv_coeff;
 
    LWORD64 i8_sub_blk_not_coded_dist = 0, i8_sub_blk_coded_dist = 0;
    WORD32 i4_sub_blk_not_coded_bits = 0, i4_sub_blk_coded_bits = 0;
    LWORD64 i8_sub_blk_not_coded_metric, i8_sub_blk_coded_metric;
    LWORD64 i8_tu_not_coded_dist = 0, i8_tu_coded_dist = 0;
    WORD32 i4_tu_coded_bits = 0;
    WORD32 temp_zero_col = 0, temp_zero_row = 0;
 
    UWORD8 *pu1_last_sig_coeff_x;
    UWORD8 *pu1_last_sig_coeff_y;
    WORD32 scan_type;
    WORD32 last_csb;
 
    WORD32 cur_csbf = 0, nbr_csbf;
    // WORD32 i4_temp_bits;
 
    WORD32 sig_coeff_base_ctxt; /* cabac context for sig coeff flag    */
    WORD32 abs_gt1_base_ctxt; /* cabac context for abslevel > 1 flag */
 
    UWORD8 *pu1_ctxt_model = &ps_cabac->au1_ctxt_models[0];
 
    rdoq_sbh_ctxt_t *ps_rdoq_ctxt = (rdoq_sbh_ctxt_t *)pv_rdoq_ctxt;
    WORD16 *pi2_coeffs = ps_rdoq_ctxt->pi2_quant_coeffs;
    WORD16 *pi2_tr_coeffs = ps_rdoq_ctxt->pi2_trans_values;
    WORD32 trans_size = ps_rdoq_ctxt->i4_trans_size;
    WORD32 i4_round_val = ps_rdoq_ctxt->i4_round_val_ssd_in_td;
    WORD32 i4_shift_val = ps_rdoq_ctxt->i4_shift_val_ssd_in_td;
    WORD32 scan_idx = ps_rdoq_ctxt->i4_scan_idx;
 
    UWORD8 *pu1_csb_table, *pu1_trans_table;
    WORD32 shift_value, mask_value;
 
    WORD32 gt1_ctxt = 1; /* required for abs_gt1_ctxt modelling */
    WORD32 temp_gt1_ctxt = gt1_ctxt;
 
    WORD32 i;
#if DISABLE_ZCSBF
    WORD32 i4_skip_zero_cbf = 0;
    WORD32 i4_skip_zero_csbf = 0;
    WORD32 i4_num_abs_1_coeffs = 0;
#endif
    (void)perform_sbh;
    pi4_subBlock2csbfId_map = ps_rdoq_ctxt->pi4_subBlock2csbfId_map;
 
    /* scan order inside a csb */
    pu1_csb_table = (UWORD8 *)&(g_u1_scan_table_4x4[scan_idx][0]);
    /*Initializing the backup_ctxt structures*/
    s_backup_ctxt.i4_num_bits = 0;
    s_backup_ctxt_sub_blk_not_coded.i4_num_bits = 0;
 
    memset(&s_backup_ctxt.au1_ctxt_to_backup, 0, MAX_NUM_CONTEXT_ELEMENTS);
    memset(&s_backup_ctxt_sub_blk_not_coded.au1_ctxt_to_backup, 0, MAX_NUM_CONTEXT_ELEMENTS);
 
    pu1_coeff_buf_hdr = (UWORD8 *)pv_coeff;
    pu2_sig_coeff_buf = (UWORD16 *)pv_coeff;
 
    /* last sig coeff indices in scan order */
    pu1_last_sig_coeff_x = &pu1_coeff_buf_hdr[0];
    pu1_last_sig_coeff_y = &pu1_coeff_buf_hdr[1];
 
    /* read the scan type : upright diag / horz / vert */
    scan_type = pu1_coeff_buf_hdr[2];
 
    /************************************************************************/
    /* position of the last coded sub block. This sub block contains coeff  */
    /* corresponding to last_sig_coeff_x, last_sig_coeff_y. Althoug this can*/
    /* be derived here it better to be populated by scanning module         */
    /************************************************************************/
    last_csb = pu1_coeff_buf_hdr[3];
 
    shift_value = ps_rdoq_ctxt->i4_log2_trans_size + 1;
    /* for finding. row no. from scan index */
    shift_value = shift_value - 3;
    /*for finding the col. no. from scan index*/
    mask_value = (ps_rdoq_ctxt->i4_trans_size / 4) - 1;
 
    switch(ps_rdoq_ctxt->i4_trans_size)
    {
    case 32:
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_8x8[scan_idx][0]);
        break;
    case 16:
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_4x4[scan_idx][0]);
        break;
    case 8:
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_2x2[scan_idx][0]);
        break;
    case 4:
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_1x1[0]);
        break;
    default:
        DBG_PRINTF("Invalid Trans Size\n");
        return -1;
        break;
    }
 
    /* sanity checks */
    /* transform skip not supported */
    ASSERT(0 == ps_entropy_ctxt->ps_pps->i1_transform_skip_enabled_flag);
    {
        temp_tex_bits_q12 = ps_cabac->u4_bits_estimated_q12;
    }
    /*************************************************************************/
    /* derive base context index for sig coeff as per section 9.3.3.1.4      */
    /* TODO; convert to look up based on luma/chroma, scan type and tfr size */
    /*************************************************************************/
    if(is_luma)
    {
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG;
        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG;
 
        if(3 == log2_tr_size)
        {
            /* 8x8 transform size */
            sig_coeff_base_ctxt += (scan_type == SCAN_DIAG_UPRIGHT) ? 9 : 15;
        }
        else if(3 < log2_tr_size)
        {
            /* larger transform sizes */
            sig_coeff_base_ctxt += 21;
        }
    }
    else
    {
        /* chroma context initializations */
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG + 27;
        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG + 16;
 
        if(3 == log2_tr_size)
        {
            /* 8x8 transform size */
            sig_coeff_base_ctxt += 9;
        }
        else if(3 < log2_tr_size)
        {
            /* larger transform sizes */
            sig_coeff_base_ctxt += 12;
        }
    }
 
    /* go to csbf flags */
    pu2_sig_coeff_buf = (UWORD16 *)(pu1_coeff_buf_hdr + COEFF_BUF_HEADER_LEN);
 
    /*Calculating the distortion produced by all the zero coeffs in the TU*/
    for(i = (trans_size * trans_size) - 1; i >= 0; i--)
    {
        WORD32 i4_dist;
        WORD16 *pi2_orig_coeff = ps_rdoq_ctxt->pi2_trans_values;
 
        if(pi2_coeffs[i] == 0)
        {
            i4_dist = CALC_SSD_IN_TRANS_DOMAIN(pi2_orig_coeff[i], 0, 0, 0);
            i8_tu_not_coded_dist += i4_dist;
            i8_tu_coded_dist += i4_dist;
        }
    }
 
    /*Backup of the various cabac ctxts*/
    memcpy(&s_sub_blk_not_coded_cabac_ctxt, ps_cabac, sizeof(cab_ctxt_t));
    /************************************************************************/
    /* encode the csbf, sig_coeff_map, abs_grt1_flags, abs_grt2_flag, sign  */
    /* and abs_coeff_remaining for each 4x4 starting from last scan to first*/
    /************************************************************************/
 
    for(i = last_csb; i >= 0; i--)
    {
        UWORD16 u2_marker_csbf;
        WORD32 ctxt_idx;
        WORD32 i4_sub_blk_is_coded = 0;
        WORD32 blk_row, blk_col;
        WORD32 scaled_blk_row;
        WORD32 scaled_blk_col;
        WORD32 infer_coeff;
 
        gt1_ctxt = temp_gt1_ctxt;
#if DISABLE_ZCSBF
        /*Initialize skip zero cbf flag to 0*/
        i4_skip_zero_csbf = 0;
        i4_num_abs_1_coeffs = 0;
#endif
 
#if OPT_MEMCPY
        ihevce_copy_backup_ctxt(
            (void *)&s_sub_blk_not_coded_cabac_ctxt,
            (void *)ps_cabac,
            (void *)&s_backup_ctxt_sub_blk_not_coded,
            (void *)&s_backup_ctxt);
        memset(s_backup_ctxt_sub_blk_not_coded.au1_ctxt_to_backup, 0, 5);
        memset(s_backup_ctxt.au1_ctxt_to_backup, 0, 5);
#else
        memcpy(&s_sub_blk_not_coded_cabac_ctxt, ps_cabac, sizeof(cab_ctxt_t));
#endif
        // i4_temp_bits = s_sub_blk_not_coded_cabac_ctxt.u4_bits_estimated_q12;
 
        blk_row = pu1_trans_table[i] >> shift_value; /*row of csb*/
        blk_col = pu1_trans_table[i] & mask_value; /*col of csb*/
 
        scaled_blk_row = blk_row << 2;
        scaled_blk_col = blk_col << 2;
 
        infer_coeff = (i < last_csb) && (i > 0);
        u2_marker_csbf = *pu2_sig_coeff_buf;
 
        if((blk_col + 1 < trans_size / 4)) /* checking right boundary */
        {
            if(!ps_rdoq_ctxt
                    ->pu1_csbf_buf[pi4_subBlock2csbfId_map[blk_row * trans_size / 4 + blk_col + 1]])
            {
                /* clear the 2nd bit if the right csb is 0 */
                u2_marker_csbf = u2_marker_csbf & (~(1 << 1));
            }
        }
        if((blk_row + 1 < trans_size / 4)) /* checking bottom boundary */
        {
            if(!ps_rdoq_ctxt
                    ->pu1_csbf_buf[pi4_subBlock2csbfId_map[(blk_row + 1) * trans_size / 4 + blk_col]])
            {
                /* clear the 3rd bit if the bottom csb is 0*/
                u2_marker_csbf = u2_marker_csbf & (~(1 << 2));
            }
        }
        pu2_sig_coeff_buf++;
 
        /* sanity checks for marker present in every csbf flag */
        ASSERT((u2_marker_csbf >> 4) == 0xBAD);
 
        /* extract the current and neigbour csbf flags */
        cur_csbf = u2_marker_csbf & 0x1;
        nbr_csbf = (u2_marker_csbf >> 1) & 0x3;
 
        if((i < last_csb) && (i > 0))
        {
            ctxt_idx = IHEVC_CAB_CODED_SUBLK_IDX;
 
            /* ctxt based on right / bottom avail csbf, section 9.3.3.1.3 */
            ctxt_idx += nbr_csbf ? 1 : 0;
            ctxt_idx += is_luma ? 0 : 2;
 
            ret |= ihevce_cabac_encode_bin(ps_cabac, cur_csbf, ctxt_idx);
 
            s_backup_ctxt.au1_ctxt_to_backup[SUB_BLK_CODED_FLAG] = 1;
 
            if(cur_csbf)
            {
                ret |= ihevce_cabac_encode_bin(&s_sub_blk_not_coded_cabac_ctxt, 0, ctxt_idx);
                // clang-format off
                i4_sub_blk_not_coded_bits =
                    s_sub_blk_not_coded_cabac_ctxt.u4_bits_estimated_q12;  // - i4_temp_bits;
                s_backup_ctxt_sub_blk_not_coded.au1_ctxt_to_backup[SUB_BLK_CODED_FLAG] = 1;
                // clang-format on
            }
        }
        else
        {
            /* sanity check, this csb contains the last_sig_coeff */
            if(i == last_csb)
            {
                ASSERT(cur_csbf == 1);
            }
        }
        /*If any block in the TU is coded and the 0th block is not coded, the 0th
          block is still signalled as csbf = 1, and with all sig_coeffs sent as
          0(HEVC requirement)*/
        if((ps_rdoq_ctxt->i1_tu_is_coded == 1) && (i == 0))
        {
            i4_sub_blk_not_coded_bits = ihevce_code_all_sig_coeffs_as_0_explicitly(
                (void *)ps_rdoq_ctxt,
                i,
                pu1_trans_table,
                is_luma,
                scan_type,
                infer_coeff,
                nbr_csbf,
                &s_sub_blk_not_coded_cabac_ctxt);
        }
 
        if(i == last_csb)
        {
            WORD32 i4_last_x = *pu1_last_sig_coeff_x;
            WORD32 i4_last_y = *pu1_last_sig_coeff_y;
            if(SCAN_VERT == scan_type)
            {
                /* last coeff x and y are swapped for vertical scan */
                SWAP(i4_last_x, i4_last_y);
            }
            /* Encode the last_sig_coeff_x and last_sig_coeff_y */
            ret |= ihevce_cabac_encode_last_coeff_x_y(
                ps_cabac, i4_last_x, i4_last_y, log2_tr_size, is_luma);
            s_backup_ctxt.au1_ctxt_to_backup[LASTXY] = 1;
        }
 
        if(cur_csbf)
        {
            /*****************************************************************/
            /* encode the sig coeff map as per section 7.3.13                */
            /* significant_coeff_flags: msb=coeff15-lsb=coeff0 in scan order */
            /*****************************************************************/
 
            WORD32 i4_bit_depth;
            WORD32 i4_shift_iq;
            WORD32 i4_dequant_val;
            WORD32 bit; /* temp boolean */
 
            UWORD16 u2_gt0_flags = *pu2_sig_coeff_buf;
            WORD32 sig_coeff_map = u2_gt0_flags;
            WORD32 gt1_flags = *(pu2_sig_coeff_buf + 1);
            WORD32 sign_flags = *(pu2_sig_coeff_buf + 2);
 
            WORD32 gt1_bins = 0; /* bins for coeffs with abslevel > 1 */
 
            WORD16 *pi2_dequant_coeff = ps_rdoq_ctxt->pi2_dequant_coeff;
            WORD16 i2_qp_rem = ps_rdoq_ctxt->i2_qp_rem;
            WORD32 i4_qp_div = ps_rdoq_ctxt->i4_qp_div;
 
            WORD32 sign_bins = 0; /* bins for sign flags of coded coeffs  */
            WORD32 num_coded = 0; /* total coeffs coded in 4x4            */
 
            /* total count of coeffs to be coded as abs level remaining */
            WORD32 num_coeffs_remaining = 0;
 
            /* count of coeffs to be coded as  abslevel-1 */
            WORD32 num_coeffs_base1 = 0;
            WORD32 scan_pos;
            WORD32 first_gt1_coeff = 0;
 
            i4_bit_depth = ps_entropy_ctxt->ps_sps->i1_bit_depth_luma_minus8 + 8;
            i4_shift_iq = i4_bit_depth + ps_rdoq_ctxt->i4_log2_trans_size - 5;
 
            i4_sub_blk_is_coded = 1;
 
            if((i != 0) || (0 == last_csb))
            {
                /* sanity check, atleast one coeff is coded as csbf is set */
                ASSERT(sig_coeff_map != 0);
            }
            /*Calculating the distortions produced*/
            {
                WORD32 k, j;
                WORD16 *pi2_temp_coeff =
                    &pi2_coeffs[scaled_blk_col + (scaled_blk_row * trans_size)];
                WORD16 *pi2_temp_tr_coeff =
                    &pi2_tr_coeffs[scaled_blk_col + (scaled_blk_row * trans_size)];
                WORD16 *pi2_temp_dequant_coeff =
                    &pi2_dequant_coeff[scaled_blk_col + (scaled_blk_row * trans_size)];
 
                for(k = 0; k < 4; k++)
                {
                    for(j = 0; j < 4; j++)
                    {
                        if(*pi2_temp_coeff)
                        {
                            /*Inverse quantizing for distortion calculation*/
                            if(ps_rdoq_ctxt->i4_trans_size != 4)
                            {
                                IQUANT(
                                    i4_dequant_val,
                                    *pi2_temp_coeff,
                                    *pi2_temp_dequant_coeff * g_ihevc_iquant_scales[i2_qp_rem],
                                    i4_shift_iq,
                                    i4_qp_div);
                            }
                            else
                            {
                                IQUANT_4x4(
                                    i4_dequant_val,
                                    *pi2_temp_coeff,
                                    *pi2_temp_dequant_coeff * g_ihevc_iquant_scales[i2_qp_rem],
                                    i4_shift_iq,
                                    i4_qp_div);
                            }
 
                            i8_sub_blk_coded_dist +=
                                CALC_SSD_IN_TRANS_DOMAIN(*pi2_temp_tr_coeff, i4_dequant_val, 0, 0);
 
                            i8_sub_blk_not_coded_dist +=
                                CALC_SSD_IN_TRANS_DOMAIN(*pi2_temp_tr_coeff, 0, 0, 0);
                        }
#if DISABLE_ZCSBF
                        if(abs(*pi2_temp_coeff) > 1)
                        {
                            i4_skip_zero_csbf = 1;
                        }
                        else if(abs(*pi2_temp_coeff) == 1)
                        {
                            i4_num_abs_1_coeffs++;
                        }
#endif
                        pi2_temp_coeff++;
                        pi2_temp_tr_coeff++;
                        pi2_temp_dequant_coeff++;
                    }
                    pi2_temp_tr_coeff += ps_rdoq_ctxt->i4_trans_size - 4;
                    pi2_temp_coeff += ps_rdoq_ctxt->i4_q_data_strd - 4;
                    pi2_dequant_coeff += ps_rdoq_ctxt->i4_trans_size - 4;
                }
            }
 
#if DISABLE_ZCSBF
            i4_skip_zero_csbf = i4_skip_zero_csbf || (i4_num_abs_1_coeffs > 3);
#endif
            pu2_sig_coeff_buf += 3;
 
            scan_pos = 15;
            if(i == last_csb)
            {
                /*************************************************************/
                /* clear last_scan_pos for last block in scan order as this  */
                /* is communicated  throught last_coeff_x and last_coeff_y   */
                /*************************************************************/
                WORD32 next_sig = CLZ(sig_coeff_map) + 1;
 
                scan_pos = WORD_SIZE - next_sig;
 
                /* prepare the bins for gt1 flags */
                EXTRACT_BIT(bit, gt1_flags, scan_pos);
 
                /* insert gt1 bin in lsb */
                gt1_bins |= bit;
 
                /* prepare the bins for sign flags */
                EXTRACT_BIT(bit, sign_flags, scan_pos);
 
                /* insert sign bin in lsb */
                sign_bins |= bit;
 
                sig_coeff_map = CLEAR_BIT(sig_coeff_map, scan_pos);
 
                scan_pos--;
                num_coded++;
            }
 
            /* encode the required sigcoeff flags (abslevel > 0)   */
            while(scan_pos >= 0)
            {
                WORD32 y_pos_x_pos;
                WORD32 sig_ctxinc = 0; /* 0 is default inc for DC coeff */
 
                WORD32 sig_coeff;
 
                EXTRACT_BIT(sig_coeff, sig_coeff_map, scan_pos);
 
                /* derive the x,y pos */
                y_pos_x_pos = gu1_hevce_scan4x4[scan_type][scan_pos];
 
                /* derive the context inc as per section 9.3.3.1.4 */
                if(2 == log2_tr_size)
                {
                    /* 4x4 transform size increment uses lookup */
                    sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc_tr4[y_pos_x_pos];
                }
                else if(scan_pos || i)
                {
                    /* ctxt for AC coeff depends on curpos and neigbour csbf */
                    sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc[nbr_csbf][y_pos_x_pos];
 
                    /* based on luma subblock pos */
                    sig_ctxinc += (i && is_luma) ? 3 : 0;
                }
                else
                {
                    /* DC coeff has fixed context for luma and chroma */
                    sig_coeff_base_ctxt = is_luma ? IHEVC_CAB_COEFF_FLAG
                                                  : IHEVC_CAB_COEFF_FLAG + 27;
                }
 
                /*************************************************************/
                /* encode sig coeff only if required                         */
                /* decoder infers 0,0 coeff when all the other coeffs are 0  */
                /*************************************************************/
                if(scan_pos || (!infer_coeff))
                {
                    ctxt_idx = sig_ctxinc + sig_coeff_base_ctxt;
                    //ret |= ihevce_cabac_encode_bin(ps_cabac, sig_coeff, ctxt_idx);
                    {
                        WORD32 state_mps = pu1_ctxt_model[ctxt_idx];
 
                        /* increment bits generated based on state and bin encoded */
                        ps_cabac->u4_bits_estimated_q12 +=
                            gau2_ihevce_cabac_bin_to_bits[state_mps ^ sig_coeff];
 
                        /* update the context model from state transition LUT */
                        pu1_ctxt_model[ctxt_idx] =
                            gau1_ihevc_next_state[(state_mps << 1) | sig_coeff];
                    }
                }
 
                if(sig_coeff)
                {
                    /* prepare the bins for gt1 flags */
                    EXTRACT_BIT(bit, gt1_flags, scan_pos);
 
                    /* shift and insert gt1 bin in lsb */
                    gt1_bins <<= 1;
                    gt1_bins |= bit;
 
                    /* prepare the bins for sign flags */
                    EXTRACT_BIT(bit, sign_flags, scan_pos);
 
                    /* shift and insert sign bin in lsb */
                    sign_bins <<= 1;
                    sign_bins |= bit;
 
                    num_coded++;
 
                    /* 0,0 coeff can no more be inferred :( */
                    infer_coeff = 0;
                }
 
                scan_pos--;
            }
 
            s_backup_ctxt.au1_ctxt_to_backup[SIG_COEFF] = 1;
 
            /****************************************************************/
            /* encode the abs level greater than 1 bins; Section 7.3.13     */
            /* These have already been prepared during sig_coeff_map encode */
            /* Context modelling done as per section 9.3.3.1.5              */
            /****************************************************************/
            {
                WORD32 j;
 
                /* context set based on luma subblock pos */
                WORD32 ctxt_set = (i && is_luma) ? 2 : 0;
 
                /* count of coeffs with abslevel > 1; max of 8 to be coded */
                WORD32 num_gt1_bins = MIN(8, num_coded);
 
                if(num_coded > 8)
                {
                    /* pull back the bins to required number */
                    gt1_bins >>= (num_coded - 8);
 
                    num_coeffs_remaining += (num_coded - 8);
                    num_coeffs_base1 = (num_coded - 8);
                }
 
                /* See section 9.3.3.1.5           */
                ctxt_set += (0 == gt1_ctxt) ? 1 : 0;
 
                gt1_ctxt = 1;
 
                for(j = num_gt1_bins - 1; j >= 0; j--)
                {
                    /* Encodet the abs level gt1 bins */
                    ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
 
                    EXTRACT_BIT(bit, gt1_bins, j);
 
                    //ret |= ihevce_cabac_encode_bin(ps_cabac, bit, ctxt_idx);
                    {
                        WORD32 state_mps = pu1_ctxt_model[ctxt_idx];
 
                        /* increment bits generated based on state and bin encoded */
                        ps_cabac->u4_bits_estimated_q12 +=
                            gau2_ihevce_cabac_bin_to_bits[state_mps ^ bit];
 
                        /* update the context model from state transition LUT */
                        pu1_ctxt_model[ctxt_idx] = gau1_ihevc_next_state[(state_mps << 1) | bit];
                    }
 
                    if(bit)
                    {
                        gt1_ctxt = 0;
                        num_coeffs_remaining++;
                    }
                    else if(gt1_ctxt && (gt1_ctxt < 3))
                    {
                        gt1_ctxt++;
                    }
                }
                s_backup_ctxt.au1_ctxt_to_backup[GRTR_THAN_1] = 1;
                /*************************************************************/
                /* encode abs level greater than 2 bin; Section 7.3.13       */
                /*************************************************************/
                if(gt1_bins)
                {
                    WORD32 gt2_bin;
 
                    first_gt1_coeff = pu2_sig_coeff_buf[0] + 1;
                    gt2_bin = (first_gt1_coeff > 2);
 
                    /* atleast one level > 2 */
                    ctxt_idx = IHEVC_CAB_COEFABS_GRTR2_FLAG;
 
                    ctxt_idx += (is_luma) ? ctxt_set : (ctxt_set + 4);
 
                    //ret |= ihevce_cabac_encode_bin(ps_cabac, gt2_bin, ctxt_idx);
                    {
                        WORD32 state_mps = pu1_ctxt_model[ctxt_idx];
 
                        /* increment bits generated based on state and bin encoded */
                        ps_cabac->u4_bits_estimated_q12 +=
                            gau2_ihevce_cabac_bin_to_bits[state_mps ^ gt2_bin];
 
                        /* update the context model from state transition LUT */
                        pu1_ctxt_model[ctxt_idx] =
                            gau1_ihevc_next_state[(state_mps << 1) | gt2_bin];
                    }
 
                    if(!gt2_bin)
                    {
                        /* sanity check */
                        ASSERT(first_gt1_coeff == 2);
 
                        /* no need to send this coeff as bypass bins */
                        pu2_sig_coeff_buf++;
                        num_coeffs_remaining--;
                    }
                    s_backup_ctxt.au1_ctxt_to_backup[GRTR_THAN_2] = 1;
                }
            }
 
            /*************************************************************/
            /* encode the coeff signs and abs remaing levels             */
            /*************************************************************/
            if(num_coded)
            {
                WORD32 base_level;
                WORD32 rice_param = 0;
                WORD32 j;
 
                /*************************************************************/
                /* encode the coeff signs populated in sign_bins             */
                /*************************************************************/
                if(num_coded > 0)
                {
                    ret |= ihevce_cabac_encode_bypass_bins(ps_cabac, sign_bins, num_coded);
                }
                /*************************************************************/
                /* encode the coeff_abs_level_remaining as TR / EGK bins     */
                /* See section 9.3.2.7 for details                           */
                /*************************************************************/
 
                /* first remaining coeff baselevel */
                if(first_gt1_coeff > 2)
                {
                    base_level = 3;
                }
                else if(num_coeffs_remaining > num_coeffs_base1)
                {
                    /* atleast one coeff in first 8 is gt > 1 */
                    base_level = 2;
                }
                else
                {
                    /* all coeffs have base of 1 */
                    base_level = 1;
                }
 
                for(j = 0; j < num_coeffs_remaining; j++)
                {
                    WORD32 abs_coeff = pu2_sig_coeff_buf[0] + 1;
                    WORD32 abs_coeff_rem;
                    WORD32 rice_max = (4 << rice_param);
 
                    pu2_sig_coeff_buf++;
 
                    /* sanity check */
                    ASSERT(abs_coeff >= base_level);
 
                    abs_coeff_rem = (abs_coeff - base_level);
 
                    /* TODO://HM-8.0-dev uses (3 << rice_param) as rice_max */
                    /* TODO://HM-8.0-dev does either TR or EGK but not both */
                    if(abs_coeff_rem >= rice_max)
                    {
                        UWORD32 u4_suffix = (abs_coeff_rem - rice_max);
 
                        /* coeff exceeds max rice limit                    */
                        /* encode the TR prefix as tunary code             */
                        /* prefix = 1111 as (rice_max >> rice_praram) = 4  */
                        ret |= ihevce_cabac_encode_bypass_bins(ps_cabac, 0xF, 4);
 
                        /* encode the exponential golomb code suffix */
                        ret |= ihevce_cabac_encode_egk(ps_cabac, u4_suffix, (rice_param + 1));
                    }
                    else
                    {
                        /* code coeff as truncated rice code  */
                        ret |= ihevce_cabac_encode_trunc_rice(
                            ps_cabac, abs_coeff_rem, rice_param, rice_max);
                    }
 
                    /* update the rice param based on coeff level */
                    if((abs_coeff > (3 << rice_param)) && (rice_param < 4))
                    {
                        rice_param++;
                    }
 
                    /* change base level to 1 if more than 8 coded coeffs */
                    if((j + 1) < (num_coeffs_remaining - num_coeffs_base1))
                    {
                        base_level = 2;
                    }
                    else
                    {
                        base_level = 1;
                    }
                }
            }
 
            i4_sub_blk_coded_bits = ps_cabac->u4_bits_estimated_q12;
            /**********************************************************/
            /**********************************************************/
            /**********************************************************/
            /*Decide whether sub block should be coded or not*/
            /**********************************************************/
            /**********************************************************/
            /**********************************************************/
            i8_sub_blk_coded_metric = CALC_CUMMUL_SSD_IN_TRANS_DOMAIN(
                                          i8_sub_blk_coded_dist, 0, i4_round_val, i4_shift_val) +
                                      COMPUTE_RATE_COST_CLIP30_RDOQ(
                                          i4_sub_blk_coded_bits,
                                          ps_rdoq_ctxt->i8_cl_ssd_lambda_qf,
                                          (LAMBDA_Q_SHIFT + CABAC_FRAC_BITS_Q));
            i8_sub_blk_not_coded_metric =
                CALC_CUMMUL_SSD_IN_TRANS_DOMAIN(
                    i8_sub_blk_not_coded_dist, 0, i4_round_val, i4_shift_val) +
                COMPUTE_RATE_COST_CLIP30_RDOQ(
                    i4_sub_blk_not_coded_bits,
                    ps_rdoq_ctxt->i8_cl_ssd_lambda_qf,
                    (LAMBDA_Q_SHIFT + CABAC_FRAC_BITS_Q));
 
#if DISABLE_ZCSBF
            if(((i8_sub_blk_not_coded_metric < i8_sub_blk_coded_metric) ||
                (i4_sub_blk_is_coded == 0)) &&
               (i4_skip_zero_csbf == 0))
#else
            if((i8_sub_blk_not_coded_metric < i8_sub_blk_coded_metric) ||
               (i4_sub_blk_is_coded == 0))
#endif
            {
#if OPT_MEMCPY
                ihevce_copy_backup_ctxt(
                    (void *)ps_cabac,
                    (void *)&s_sub_blk_not_coded_cabac_ctxt,
                    (void *)&s_backup_ctxt,
                    (void *)&s_backup_ctxt_sub_blk_not_coded);
#else
                memcpy(ps_cabac, &s_sub_blk_not_coded_cabac_ctxt, sizeof(cab_ctxt_t));
#endif
                scan_pos = 15;
                i4_sub_blk_is_coded = 0;
 
                {
                    WORD32 k, j;
                    WORD16 *pi2_temp_coeff =
                        &pi2_coeffs[scaled_blk_col + (scaled_blk_row * ps_rdoq_ctxt->i4_q_data_strd)];
                    WORD16 *pi2_temp_iquant_coeff =
                        &ps_rdoq_ctxt->pi2_iquant_coeffs
                             [scaled_blk_col + (scaled_blk_row * ps_rdoq_ctxt->i4_iq_data_strd)];
                    for(k = 0; k < 4; k++)
                    {
                        for(j = 0; j < 4; j++)
                        {
                            *pi2_temp_coeff = 0;
                            *pi2_temp_iquant_coeff = 0;
 
                            pi2_temp_coeff++;
                            pi2_temp_iquant_coeff++;
                        }
                        pi2_temp_coeff += ps_rdoq_ctxt->i4_q_data_strd - 4;
                        pi2_temp_iquant_coeff += ps_rdoq_ctxt->i4_iq_data_strd - 4;
                    }
                }
 
                /* If the csb to be masked is the last csb, then we should
                 * signal last x and last y from the next coded sub_blk */
                if(i == last_csb)
                {
                    pu1_coeff_buf_hdr = (UWORD8 *)pu2_sig_coeff_buf;
 
                    ps_rdoq_ctxt->pu1_csbf_buf[pi4_subBlock2csbfId_map[pu1_trans_table[i]]] = 0;
                    last_csb = ihevce_find_new_last_csb(
                        pi4_subBlock2csbfId_map,
                        i,
                        (void *)ps_rdoq_ctxt,
                        pu1_trans_table,
                        pu1_csb_table,
                        pi2_coeffs,
                        shift_value,
                        mask_value,
                        &pu1_coeff_buf_hdr);
                    /*We are in a for loop. This means that the decrement to i happens immediately right
                      at the end of the for loop. This would decrement the value of i to (last_csb - 1).
                      Hence we increment i by 1, so that after the decrement i becomes last_csb.*/
                    i = last_csb + 1;
                    pu1_last_sig_coeff_x = &pu1_coeff_buf_hdr[0];
                    pu1_last_sig_coeff_y = &pu1_coeff_buf_hdr[1];
                    scan_type = pu1_coeff_buf_hdr[2];
                    pu2_sig_coeff_buf = (UWORD16 *)(pu1_coeff_buf_hdr + 4);
                }
                i8_tu_coded_dist += i8_sub_blk_not_coded_dist;
                i4_tu_coded_bits += i4_sub_blk_not_coded_bits;
            }
            else
            {
                ps_rdoq_ctxt->i1_tu_is_coded = 1;
                temp_gt1_ctxt = gt1_ctxt;
 
                i8_tu_coded_dist += i8_sub_blk_coded_dist;
                i4_tu_coded_bits += i4_sub_blk_coded_bits;
            }
#if DISABLE_ZCSBF
            i4_skip_zero_cbf = i4_skip_zero_cbf || i4_skip_zero_csbf;
#endif
            /*Cumulating the distortion for the entire TU*/
            i8_tu_not_coded_dist += i8_sub_blk_not_coded_dist;
            //i4_tu_coded_dist                += i4_sub_blk_coded_dist;
            //i4_tu_coded_bits                += i4_sub_blk_coded_bits;
            i8_sub_blk_not_coded_dist = 0;
            i4_sub_blk_not_coded_bits = 0;
            i8_sub_blk_coded_dist = 0;
            i4_sub_blk_coded_bits = 0;
 
            if(i4_sub_blk_is_coded)
            {
                ps_rdoq_ctxt->pu1_csbf_buf[pi4_subBlock2csbfId_map[pu1_trans_table[i]]] = 1;
                temp_zero_col = (temp_zero_col) | (0xF << scaled_blk_col);
                temp_zero_row = (temp_zero_row) | (0xF << scaled_blk_row);
            }
            else
            {
                if(!((ps_rdoq_ctxt->i1_tu_is_coded == 1) && (i == 0)))
                {
                    ps_rdoq_ctxt->pu1_csbf_buf[pi4_subBlock2csbfId_map[pu1_trans_table[i]]] = 0;
                }
            }
        }
    }
 
    /*tap texture bits*/
    {
        ps_cabac->u4_texture_bits_estimated_q12 +=
            (ps_cabac->u4_bits_estimated_q12 - temp_tex_bits_q12);
    }
 
    i8_tu_not_coded_dist =
        CALC_CUMMUL_SSD_IN_TRANS_DOMAIN(i8_tu_not_coded_dist, 0, i4_round_val, i4_shift_val);
 
    /* i4_tu_coded_dist = CALC_CUMMUL_SSD_IN_TRANS_DOMAIN(
        i4_tu_coded_dist, 0, i4_round_val, i4_shift_val); */
    *pi8_tu_coded_dist = i8_tu_coded_dist;
    *pi8_tu_not_coded_dist = i8_tu_not_coded_dist;
#if DISABLE_ZCSBF
    if(i4_skip_zero_cbf == 1)
    {
        *pi8_tu_not_coded_dist = 0x7FFFFFFF;
    }
#endif
 
    *ps_rdoq_ctxt->pi4_zero_col = ~temp_zero_col;
    *ps_rdoq_ctxt->pi4_zero_row = ~temp_zero_row;
 
    return (ret);
}
 
/**
******************************************************************************
*
*  @brief Codes all the sig coeffs as 0
*
*  @param[in]   i
*  Index of the current csb
*
*  @param[in]   pu1_trans_table
*  Pointer to the trans table
*
*  @param[in]  scan_type
*  Determines the scan order
*
*  @param[in]  infer_coeff
*  Indicates whether the 0,0 coeff can be inferred or not
*
*  @param[in]   nbr_csbf
*  Talks about if the neighboour csbs(right and bottom) are coded or not
*
*  @param[in]    ps_cabac
*  Cabac state
*
*  @param[out]    pi4_tu_not_coded_dist
*  The distortion when the entire TU is not coded(all coeffs are set to 0) is stored here
*
*  @return    The number of bits generated when the 0th sub blk is coded as all 0s
*             This is the cumulate bits(i.e. for all blocks in the TU), and not only
*             the bits generated for this block
*
******************************************************************************
*/
WORD32 ihevce_code_all_sig_coeffs_as_0_explicitly(
    void *pv_rdoq_ctxt,
    WORD32 i,
    UWORD8 *pu1_trans_table,
    WORD32 is_luma,
    WORD32 scan_type,
    WORD32 infer_coeff,
    WORD32 nbr_csbf,
    cab_ctxt_t *ps_cabac)
{
    WORD32 sig_coeff_base_ctxt;
    WORD32 scan_pos = 15;
    WORD32 ctxt_idx;
    WORD32 ret = 0;
 
    rdoq_sbh_ctxt_t *ps_rdoq_ctxt = (rdoq_sbh_ctxt_t *)pv_rdoq_ctxt;
 
    WORD32 log2_tr_size = ps_rdoq_ctxt->i4_log2_trans_size;
 
    (void)pu1_trans_table;
    if(is_luma)
    {
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG;
        if(3 == log2_tr_size)
        {
            /* 8x8 transform size */
            sig_coeff_base_ctxt += (scan_type == SCAN_DIAG_UPRIGHT) ? 9 : 15;
        }
        else if(3 < log2_tr_size)
        {
            /* larger transform sizes */
            sig_coeff_base_ctxt += 21;
        }
    }
    else
    {
        /* chroma context initializations */
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG + 27;
 
        if(3 == log2_tr_size)
        {
            /* 8x8 transform size */
            sig_coeff_base_ctxt += 9;
        }
        else if(3 < log2_tr_size)
        {
            /* larger transform sizes */
            sig_coeff_base_ctxt += 12;
        }
    }
    while(scan_pos >= 0)
    {
        WORD32 sig_ctxinc = 0; /* 0 is default inc for DC coeff */
        WORD32 sig_coeff = 0;
        /* derive the x,y pos */
        WORD32 y_pos_x_pos = gu1_hevce_scan4x4[scan_type][scan_pos];
 
        /* derive the context inc as per section 9.3.3.1.4 */
        if(2 == log2_tr_size)
        {
            /* 4x4 transform size increment uses lookup */
            sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc_tr4[y_pos_x_pos];
        }
        else if(scan_pos || i)
        {
            /* ctxt for AC coeff depends on curpos and neigbour csbf */
            sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc[nbr_csbf][y_pos_x_pos];
 
            /* based on luma subblock pos */
            sig_ctxinc += (i && is_luma) ? 3 : 0;
        }
        else
        {
            /* DC coeff has fixed context for luma and chroma */
            sig_coeff_base_ctxt = is_luma ? IHEVC_CAB_COEFF_FLAG : IHEVC_CAB_COEFF_FLAG + 27;
        }
 
        if(scan_pos || (!infer_coeff))
        {
            ctxt_idx = sig_ctxinc + sig_coeff_base_ctxt;
            ret |= ihevce_cabac_encode_bin(ps_cabac, sig_coeff, ctxt_idx);
            AEV_TRACE("significant_coeff_flag", sig_coeff, ps_cabac->u4_range);
        }
        scan_pos--;
    }
    return (ps_cabac->u4_bits_estimated_q12);  // - i4_temp_bits);
}
 
/**
******************************************************************************
*
*  @brief Finds the next csb with a non-zero coeff
*
*  @paramp[in]  cur_last_csb_pos
*  The index of the current csb with a non-zero coeff
*
*  @param[inout]   pv_rdoq_ctxt
*  RODQ context structure
*
*  @param[in]   pu1_trans_table
*  Pointer to the trans table
*
*  @param[in]   pi2_coeffs
*  Pointer to all the quantized coefficients
*
*  @param[in]  shift_value
*  Determines the shifting value for determining appropriate position of coeff
*
*  @param[in]  mask_value
*  Determines the masking value for determining appropriate position of coeff
*
*  @param[in]   nbr_csbf
*  Talks about if the neighboour csbs(right and bottom) are coded or not
*
*  @param[in]    ps_cabac
*  Cabac state
*
*  @param[inout] ppu1_addr
*  Pointer to the header(i.e. pointer used for traversing the ecd data generated
*  in ihevce_scan_coeffs)
*
*  @return    The index of the csb with the next non-zero coeff
*
******************************************************************************
*/
WORD32 ihevce_find_new_last_csb(
    WORD32 *pi4_subBlock2csbfId_map,
    WORD32 cur_last_csb_pos,
    void *pv_rdoq_ctxt,
    UWORD8 *pu1_trans_table,
    UWORD8 *pu1_csb_table,
    WORD16 *pi2_coeffs,
    WORD32 shift_value,
    WORD32 mask_value,
    UWORD8 **ppu1_addr)
{
    WORD32 blk_row;
    WORD32 blk_col;
    WORD32 x_pos;
    WORD32 y_pos;
    WORD32 i;
    WORD32 j;
    UWORD16 *pu2_out_data_coeff;
    rdoq_sbh_ctxt_t *ps_rdoq_ctxt = (rdoq_sbh_ctxt_t *)pv_rdoq_ctxt;
    WORD32 trans_size = ps_rdoq_ctxt->i4_trans_size;
    UWORD8 *pu1_out_data_header = *ppu1_addr;
 
    for(i = cur_last_csb_pos - 1; i >= 0; i--)
    {
        /* check for the first csb flag in our scan order */
        if(ps_rdoq_ctxt->pu1_csbf_buf[pi4_subBlock2csbfId_map[pu1_trans_table[i]]])
        {
            UWORD8 u1_last_x, u1_last_y;
            WORD32 quant_coeff;
 
            pu1_out_data_header -= 4;  //To move the pointer back to the appropriate position
            /* row of csb */
            blk_row = pu1_trans_table[i] >> shift_value;
            /* col of csb */
            blk_col = pu1_trans_table[i] & mask_value;
 
            /*check for the 1st non-0 values inside the csb in our scan order*/
            for(j = 15; j >= 0; j--)
            {
                x_pos = (pu1_csb_table[j] & 0x3) + blk_col * 4;
                y_pos = (pu1_csb_table[j] >> 2) + blk_row * 4;
 
                quant_coeff = pi2_coeffs[x_pos + (y_pos * trans_size)];
 
                if(quant_coeff != 0)
                    break;
            }
 
            ASSERT(j >= 0);
 
            u1_last_x = x_pos;
            u1_last_y = y_pos;
 
            /* storing last_x and last_y */
            *(pu1_out_data_header) = u1_last_x;
            *(pu1_out_data_header + 1) = u1_last_y;
 
            /* storing the scan order */
            *(pu1_out_data_header + 2) = ps_rdoq_ctxt->i4_scan_idx;
 
            /* storing last_sub_block pos. in scan order count */
            *(pu1_out_data_header + 3) = i;
 
            /*stored the first 4 bytes, now all are word16. So word16 pointer*/
            pu2_out_data_coeff = (UWORD16 *)(pu1_out_data_header + 4);
 
            *pu2_out_data_coeff = 0xBAD0 | 1; /*since right&bottom csbf is 0*/
            *ppu1_addr = pu1_out_data_header;
 
            break; /*We just need this loop for finding 1st non-zero csb only*/
        }
        else
            pu1_out_data_header += 2;
    }
    return i;
}
 
/**
******************************************************************************
*
*  @brief Used to optimize the memcpy of cabac states. It copies only those
*  states in the cabac context which have been altered.
*
*  @paramp[inout]  pv_dest
*  Pointer to desitination cabac state.
*
*  @param[inout]   pv_backup_ctxt_dest
*  Pointer to destination backup context
*
*  @param[inout]   pv_backup_ctxt_src
*  Pointer to source backup context
*
*  @Desc:
*  We go through each element in the backup_ctxt structure which will tell us
*  if the states corresponding to lastxlasty, sigcoeffs, grtr_than_1_bins,
*  grtr_than_2_bins and sub_blk_coded_flag(i.e. 0xBAD0) context elements
*  have been altered. If they have been altered, we will memcpy the states
*  corresponding to these context elements alone
*
*  @return  Nothing
*
******************************************************************************
*/
void ihevce_copy_backup_ctxt(
    void *pv_dest, void *pv_src, void *pv_backup_ctxt_dest, void *pv_backup_ctxt_src)
{
    UWORD8 *pu1_dest = (UWORD8 *)(((cab_ctxt_t *)pv_dest)->au1_ctxt_models);
    UWORD8 *pu1_src = (UWORD8 *)(((cab_ctxt_t *)pv_src)->au1_ctxt_models);
    backup_ctxt_t *ps_backup_dest_ctxt = ((backup_ctxt_t *)pv_backup_ctxt_dest);
    backup_ctxt_t *ps_backup_src_ctxt = ((backup_ctxt_t *)pv_backup_ctxt_src);
    WORD32 i4_i;
 
    /*
    0       IHEVC_CAB_COEFFX_PREFIX         lastx last y has been coded
    1       IHEVC_CAB_CODED_SUBLK_IDX       sub-blk coded or not flag has been coded
    2       IHEVC_CAB_COEFF_FLAG            sigcoeff has been coded
    3       IHEVC_CAB_COEFABS_GRTR1_FLAG    greater than 1 bin has been coded
    4       IHEVC_CAB_COEFABS_GRTR2_FLAG    greater than 2 bin has been coded*/
    assert(MAX_NUM_CONTEXT_ELEMENTS == 5);
    for(i4_i = 0; i4_i < MAX_NUM_CONTEXT_ELEMENTS; i4_i++)
    {
        if((ps_backup_src_ctxt->au1_ctxt_to_backup[SIG_COEFF]) ||
           (ps_backup_dest_ctxt->au1_ctxt_to_backup[SIG_COEFF]))
        {
            memcpy(&pu1_dest[IHEVC_CAB_COEFF_FLAG], &pu1_src[IHEVC_CAB_COEFF_FLAG], 42);
            ps_backup_dest_ctxt->au1_ctxt_to_backup[SIG_COEFF] = 0;
            ps_backup_src_ctxt->au1_ctxt_to_backup[SIG_COEFF] = 0;
        }
        if((ps_backup_src_ctxt->au1_ctxt_to_backup[GRTR_THAN_1]) ||
           (ps_backup_dest_ctxt->au1_ctxt_to_backup[GRTR_THAN_1]))
        {
            memcpy(
                &pu1_dest[IHEVC_CAB_COEFABS_GRTR1_FLAG],
                &pu1_src[IHEVC_CAB_COEFABS_GRTR1_FLAG],
                24);
            ps_backup_dest_ctxt->au1_ctxt_to_backup[GRTR_THAN_1] = 0;
            ps_backup_src_ctxt->au1_ctxt_to_backup[GRTR_THAN_1] = 0;
        }
        if((ps_backup_src_ctxt->au1_ctxt_to_backup[GRTR_THAN_2]) ||
           (ps_backup_dest_ctxt->au1_ctxt_to_backup[GRTR_THAN_2]))
        {
            memcpy(
                &pu1_dest[IHEVC_CAB_COEFABS_GRTR2_FLAG], &pu1_src[IHEVC_CAB_COEFABS_GRTR2_FLAG], 6);
            ps_backup_dest_ctxt->au1_ctxt_to_backup[GRTR_THAN_2] = 0;
            ps_backup_src_ctxt->au1_ctxt_to_backup[GRTR_THAN_2] = 0;
        }
        if((ps_backup_src_ctxt->au1_ctxt_to_backup[SUB_BLK_CODED_FLAG]) ||
           (ps_backup_dest_ctxt->au1_ctxt_to_backup[SUB_BLK_CODED_FLAG]))
        {
            memcpy(&pu1_dest[IHEVC_CAB_CODED_SUBLK_IDX], &pu1_src[IHEVC_CAB_CODED_SUBLK_IDX], 4);
            ps_backup_dest_ctxt->au1_ctxt_to_backup[SUB_BLK_CODED_FLAG] = 0;
            ps_backup_src_ctxt->au1_ctxt_to_backup[SUB_BLK_CODED_FLAG] = 0;
        }
        if((ps_backup_src_ctxt->au1_ctxt_to_backup[LASTXY]) ||
           (ps_backup_dest_ctxt->au1_ctxt_to_backup[LASTXY]))
        {
            memcpy(&pu1_dest[IHEVC_CAB_COEFFX_PREFIX], &pu1_src[IHEVC_CAB_COEFFX_PREFIX], 36);
            ps_backup_dest_ctxt->au1_ctxt_to_backup[LASTXY] = 0;
            ps_backup_src_ctxt->au1_ctxt_to_backup[LASTXY] = 0;
        }
    }
    ((cab_ctxt_t *)pv_dest)->u4_bits_estimated_q12 = ((cab_ctxt_t *)pv_src)->u4_bits_estimated_q12;
}