aboutsummaryrefslogtreecommitdiff
path: root/encoder/ihevce_rc_interface.c
blob: d17dffc2bd85e3c0fe8c421bbc6de25247dfcfeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
/******************************************************************************
 *
 * 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_rc_interface.c
*
* @brief
*  This file contains function definitions for rc api interface
*
* @author
*  Ittiam
*
* List of Functions
*  <TODO: Update this>
*
******************************************************************************
*/

/*****************************************************************************/
/* 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 "mem_req_and_acq.h"
#include "rate_control_api.h"
#include "var_q_operator.h"

#include "ihevc_defs.h"
#include "ihevc_debug.h"
#include "ihevc_macros.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 "ihevce_defs.h"
#include "ihevce_hle_interface.h"
#include "ihevce_lap_enc_structs.h"
#include "ihevce_lap_interface.h"
#include "ihevce_multi_thrd_structs.h"
#include "ihevce_me_common_defs.h"
#include "ihevce_had_satd.h"
#include "ihevce_function_selector.h"
#include "ihevce_enc_structs.h"
#include "ihevce_cmn_utils_instr_set_router.h"
#include "hme_datatype.h"
#include "hme_interface.h"
#include "hme_common_defs.h"
#include "hme_defs.h"
#include "ihevce_rc_enc_structs.h"
#include "ihevce_rc_structs.h"
#include "ihevce_rc_interface.h"
#include "ihevce_frame_process_utils.h"

/*****************************************************************************/
/* Constant Macros                                                           */
/*****************************************************************************/
#define USE_USER_FIRST_FRAME_QP 0
#define DEBUG_PRINT 0
#define DETERMINISTIC_RC 1
#define USE_QP_OFFSET_POST_SCD 1
#define USE_SQRT 0
#define K_SCALING_FACTOR 8
#define ENABLE_2_PASS_BIT_ALLOC_FRM_1ST 0

#define VBV_THRSH_I_PIC_DELTA_QP_1 (0.85)
#define VBV_THRSH_I_PIC_DELTA_QP_2 (0.75)
#define VBV_THRSH_P_PIC_DELTA_QP_1 (0.80)
#define VBV_THRSH_P_PIC_DELTA_QP_2 (0.70)
#define VBV_THRSH_BR_PIC_DELTA_QP_1 (0.75)
#define VBV_THRSH_BR_PIC_DELTA_QP_2 (0.65)
#define VBV_THRSH_BNR_PIC_DELTA_QP_1 (0.75)
#define VBV_THRSH_BNR_PIC_DELTA_QP_2 (0.65)
#define VBV_THRSH_DELTA_QP (0.6)

#define VBV_THRSH_FRM_PRLL_I_PIC_DELTA_QP_1 (0.70)
#define VBV_THRSH_FRM_PRLL_I_PIC_DELTA_QP_2 (0.60)
#define VBV_THRSH_FRM_PRLL_P_PIC_DELTA_QP_1 (0.65)
#define VBV_THRSH_FRM_PRLL_P_PIC_DELTA_QP_2 (0.55)
#define VBV_THRSH_FRM_PRLL_BR_PIC_DELTA_QP_1 (0.60)
#define VBV_THRSH_FRM_PRLL_BR_PIC_DELTA_QP_2 (0.50)
#define VBV_THRSH_FRM_PRLL_BNR_PIC_DELTA_QP_1 (0.60)
#define VBV_THRSH_FRM_PRLL_BNR_PIC_DELTA_QP_2 (0.50)
#define VBV_THRSH_FRM_PRLL_DELTA_QP (0.45)

#define TRACE_SUPPORT 0

/*****************************************************************************/
/* Globals                                                                   */
/*****************************************************************************/

/*
Modified bpp vs nor satd/act/qp :
=================================

Prestine Quality
-----------------
480p  y = -0.1331x3 - 0.0589x2 + 2.5091x - 0.0626
720p  y = -0.3603x3 + 0.4504x2 + 2.2056x - 0.0411
1080p y = -0.7085x3 + 0.9743x2 + 1.939x - 0.0238
2160p y = -1.2447x3 + 2.1218x2 + 1.4995x - 0.0108

High Quality
-------------
480p  y = -0.1348x3 - 0.0557x2 + 2.5055x - 0.0655
720p  y = -0.0811x3 + 0.1988x2 + 1.246x - 0.0385
1080p y = -0.74x3 + 1.0552x2 + 1.8942x - 0.0251
2160p y = -1.3851x3 + 2.3372x2 + 1.4255x - 0.0113

Medium Speed
-------------
480p  y = -0.143x3 - 0.0452x2 + 2.5581x - 0.0765
720p  y = -0.3997x3 + 0.542x2 + 2.201x - 0.0507
1080p y = -0.816x3 + 1.2048x2 + 1.8689x - 0.0298
2160p y = -1.5169x3 + 2.5857x2 + 1.3478x - 0.0126

High Speed
-----------
480p  y = -0.1472x3 - 0.0341x2 + 2.5605x - 0.0755
720p  y = -0.3967x3 + 0.526x2 + 2.2228x - 0.0504
1080p y = -0.8008x3 + 1.1713x2 + 1.8897x - 0.0297
2160p y = -1.503x3 + 2.576x2 + 1.3476x - 0.0123

Extreme Speed
--------------
480p  y = -0.1379x3 - 0.059x2 + 2.5716x - 0.0756
720p  y = -0.3938x3 + 0.521x2 + 2.2239x - 0.0505
1080p y = -0.8041x3 + 1.1725x2 + 1.8874x - 0.0293
2160p y = -1.4863x3 + 2.556x2 + 1.344x - 0.0122

*/

const double g_offline_i_model_coeff[20][4] = {

    /*ultra_HD*/
    { -1.2447, 2.1218, 1.4995, -0.0108 }, /*Prestine quality*/
    { -1.3851, 2.3372, 1.4255, -0.0113 }, /*High quality*/
    { -1.5169, 2.5857, 1.3478, -0.0126 }, /*Medium speed*/
    { -1.503, 2.576, 1.3476, -0.0123 }, /*high speed*/
    { -1.4863, 2.556, 1.344, -0.0122 }, /*Extreme Speed*/

    /*Full HD*/
    { -0.7085, 0.9743, 1.939, -0.0238 }, /*Prestine quality*/
    { -0.74, 1.0552, 1.8942, -0.0251 }, /*High quality*/
    { -0.816, 1.2048, 1.8689, -0.0298 }, /*Medium speed*/
    { -0.8008, 1.1713, 1.8897, -0.0297 }, /*high speed*/
    { -0.8041, 1.1725, 1.8874, -0.0293 }, /*Extreme Speed*/

    /*720p*/
    { -0.3603, 0.4504, 2.2056, -0.0411 }, /*Prestine quality*/
    // {-0.0811, 0.1988, 1.246, - 0.0385},/*High quality*/
    { -0.3997, 0.542, 2.201, -0.0507 },
    { -0.3997, 0.542, 2.201, -0.0507 }, /*Medium speed*/
    { -0.3967, 0.526, 2.2228, -0.0504 }, /*high speed*/
    { -0.3938, 0.521, 2.2239, -0.0505 }, /*Extreme Speed*/

    /*SD*/
    { -0.1331, -0.0589, 2.5091, -0.0626 }, /*Prestine quality*/
    { -0.1348, -0.0557, 2.5055, -0.0655 }, /*High quality*/
    { -0.143, -0.0452, 2.5581, -0.0765 }, /*Medium speed*/
    { -0.1472, -0.0341, 2.5605, -0.0755 }, /*high speed*/
    { -0.1379, -0.059, 2.5716, -0.0756 } /*Extreme Speed*/

};

/*****************************************************************************/
/* Function Declarations                                                     */
/*****************************************************************************/

picture_type_e ihevce_rc_conv_pic_type(
    IV_PICTURE_CODING_TYPE_T pic_type,
    WORD32 i4_field_pic,
    WORD32 i4_temporal_layer_id,
    WORD32 i4_is_bottom_field,
    WORD32 i4_top_field_first);

WORD32 ihevce_rc_get_scaled_mpeg2_qp(WORD32 i4_frame_qp, rc_quant_t *ps_rc_quant_ctxt);

static WORD32 ihevce_get_offline_index(rc_context_t *ps_rc_ctxt, WORD32 i4_num_pels_in_frame);

static void ihevce_rc_get_pic_param(
    picture_type_e rc_pic_type, WORD32 *pi4_tem_lyr, WORD32 *pi4_is_bottom_field);

static double ihevce_get_frame_lambda_modifier(
    WORD8 slice_type,
    WORD32 i4_rc_temporal_lyr_id,
    WORD32 i4_first_field,
    WORD32 i4_rc_is_ref_pic,
    WORD32 i4_num_b_frms);

static WORD32 ihevce_clip_min_max_qp(
    rc_context_t *ps_rc_ctxt,
    WORD32 i4_hevc_frame_qp,
    picture_type_e rc_pic_type,
    WORD32 i4_rc_temporal_lyr_id);

WORD32 ihevce_ebf_based_rc_correction_to_avoid_overflow(
    rc_context_t *ps_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out, WORD32 *pi4_tot_bits_estimated);

/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/

/*!
************************************************************************
* @brief
*    return number of records used by RC
************************************************************************
*/
WORD32 ihevce_rc_get_num_mem_recs(void)
{
    WORD32 i4_num_rc_mem_tab = 0;

    /*get the number of memtab request from RC*/
    rate_control_handle ps_rate_control_api;
    itt_memtab_t *ps_memtab = NULL;
    i4_num_rc_mem_tab =
        rate_control_num_fill_use_free_memtab(&ps_rate_control_api, ps_memtab, GET_NUM_MEMTAB);

    return ((NUM_RC_MEM_RECS + i4_num_rc_mem_tab));
}

/*!
************************************************************************
* @brief
*    return each record attributes of RC
************************************************************************
*/
WORD32 ihevce_rc_get_mem_recs(
    iv_mem_rec_t *ps_mem_tab,
    ihevce_static_cfg_params_t *ps_init_prms,
    WORD32 mem_space,
    ihevce_sys_api_t *ps_sys_api)
{
    float f_temp;
    WORD32 i4_temp_size;
    WORD32 i4_num_memtab = 0;
    WORD32 i4_num_rc_mem_tab, i;
    rate_control_handle ps_rate_control_api;
    itt_memtab_t *ps_itt_memtab = NULL;
    itt_memtab_t as_rc_mem_tab[30];

    /*memory requirements to store RC context */
    ps_mem_tab[RC_CTXT].i4_mem_size = sizeof(rc_context_t);
    //DBG_PRINTF("size of RC context = %d\n",sizeof(rc_context_t));
    ps_mem_tab[RC_CTXT].e_mem_type = (IV_MEM_TYPE_T)mem_space;

    ps_mem_tab[RC_CTXT].i4_mem_alignment = 64;

    (void)ps_sys_api;
    //i4_temp_size = (51 + ((ps_init_prms->s_src_prms.i4_bit_depth - 8) * 6));
    i4_temp_size = (51 + ((ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8) * 6));

    ps_mem_tab[RC_QP_TO_QSCALE].i4_mem_size = (i4_temp_size + 1) * 4;
    ps_mem_tab[RC_QP_TO_QSCALE].e_mem_type = (IV_MEM_TYPE_T)mem_space;
    ps_mem_tab[RC_QP_TO_QSCALE].i4_mem_alignment = 64;

    ps_mem_tab[RC_QP_TO_QSCALE_Q_FACTOR].i4_mem_size = (i4_temp_size + 1) * 4;
    ps_mem_tab[RC_QP_TO_QSCALE_Q_FACTOR].e_mem_type = (IV_MEM_TYPE_T)mem_space;
    ps_mem_tab[RC_QP_TO_QSCALE_Q_FACTOR].i4_mem_alignment = 64;

    f_temp = (float)(51 + ((ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8) * 6));
    f_temp = ((float)(f_temp - 4) / 6);
    i4_temp_size = (WORD32)((float)pow(2, f_temp) + 0.5);
    i4_temp_size = (i4_temp_size << 3);  // Q3 format is mantained for accuarate calc at lower qp

    ps_mem_tab[RC_QSCALE_TO_QP].i4_mem_size = (i4_temp_size + 1) * sizeof(UWORD32);
    ps_mem_tab[RC_QSCALE_TO_QP].e_mem_type = (IV_MEM_TYPE_T)mem_space;
    ps_mem_tab[RC_QSCALE_TO_QP].i4_mem_alignment = 64;

    /*memory requirements to store RC context */
    ps_mem_tab[RC_MULTI_PASS_GOP_STAT].i4_mem_size = sizeof(gop_level_stat_t);
    ps_mem_tab[RC_MULTI_PASS_GOP_STAT].e_mem_type = (IV_MEM_TYPE_T)mem_space;
    ps_mem_tab[RC_MULTI_PASS_GOP_STAT].i4_mem_alignment = 64;

    i4_num_rc_mem_tab =
        rate_control_num_fill_use_free_memtab(&ps_rate_control_api, ps_itt_memtab, GET_NUM_MEMTAB);

    i4_num_memtab =
        rate_control_num_fill_use_free_memtab(&ps_rate_control_api, as_rc_mem_tab, FILL_MEMTAB);

    for(i = 0; i < i4_num_memtab; i++)
    {
        ps_mem_tab[i + NUM_RC_MEM_RECS].i4_mem_size = as_rc_mem_tab[i].u4_size;
        ps_mem_tab[i + NUM_RC_MEM_RECS].i4_mem_alignment = as_rc_mem_tab[i].i4_alignment;
        ps_mem_tab[i + NUM_RC_MEM_RECS].e_mem_type = (IV_MEM_TYPE_T)mem_space;
    }
    return (i4_num_memtab + NUM_RC_MEM_RECS);
}

/**
******************************************************************************
*
*  @brief Initilizes the rate control module
*
*  @par   Description
*
*  @param[inout]   ps_mem_tab
*  pointer to memory descriptors table
*
*  @param[in]      ps_init_prms
*  Create time static parameters
*
*  @return      void
*
******************************************************************************
*/
void *ihevce_rc_mem_init(
    iv_mem_rec_t *ps_mem_tab,
    ihevce_static_cfg_params_t *ps_init_prms,
    WORD32 i4_bitrate_instance_id,
    rc_quant_t *ps_rc_quant,
    WORD32 i4_resolution_id,
    WORD32 i4_look_ahead_frames_in_first_pass)
{
    rc_context_t *ps_rc_ctxt;
    WORD32 i4_num_memtab, i, j, i4_avg_bitrate, u4_buf_size;
    WORD32 i4_cdr_period = 0, i4_idr_period = 0;
    WORD32 i4_peak_bitrate_factor;
    rate_control_handle ps_rate_control_api;
    itt_memtab_t as_rc_mem_tab[30];
    itt_memtab_t *ps_itt_memtab = NULL;
    ps_rc_ctxt = (rc_context_t *)ps_mem_tab[RC_CTXT].pv_base;
    memset(ps_rc_ctxt, 0, sizeof(rc_context_t));

    ps_rc_ctxt->i4_br_id_for_2pass = i4_bitrate_instance_id;
    if(ps_init_prms->s_coding_tools_prms.i4_max_cra_open_gop_period)
    {
        i4_cdr_period = ps_init_prms->s_coding_tools_prms.i4_max_cra_open_gop_period;
    }
    if(ps_init_prms->s_coding_tools_prms.i4_max_i_open_gop_period)
    {
        i4_cdr_period = ps_init_prms->s_coding_tools_prms.i4_max_i_open_gop_period;
    }
    i4_idr_period = ps_init_prms->s_coding_tools_prms.i4_max_closed_gop_period;

    ps_rc_quant->pi4_qscale_to_qp = (WORD32 *)ps_mem_tab[RC_QSCALE_TO_QP].pv_base;

    ps_rc_quant->pi4_qp_to_qscale_q_factor = (WORD32 *)ps_mem_tab[RC_QP_TO_QSCALE_Q_FACTOR].pv_base;

    ps_rc_quant->pi4_qp_to_qscale = (WORD32 *)ps_mem_tab[RC_QP_TO_QSCALE].pv_base;

    ps_rc_ctxt->pv_gop_stat = (void *)ps_mem_tab[RC_MULTI_PASS_GOP_STAT].pv_base;

    /*assign memtabs to rc module*/
    i4_num_memtab =
        rate_control_num_fill_use_free_memtab(&ps_rate_control_api, ps_itt_memtab, GET_NUM_MEMTAB);

    i4_num_memtab =
        rate_control_num_fill_use_free_memtab(&ps_rate_control_api, as_rc_mem_tab, FILL_MEMTAB);
    for(i = 0; i < i4_num_memtab; i++)
    {
        as_rc_mem_tab[i].pv_base = ps_mem_tab[i + NUM_RC_MEM_RECS].pv_base;
    }
    i4_num_memtab =
        rate_control_num_fill_use_free_memtab(&ps_rate_control_api, as_rc_mem_tab, USE_BASE);

    ps_rc_ctxt->rc_hdl =
        ps_rate_control_api; /*handle to entire RC structure private to RC library*/
    ps_rc_ctxt->i4_field_pic = ps_init_prms->s_src_prms.i4_field_pic;

    ps_rc_ctxt->i4_is_first_frame_encoded = 0;
    /*added for field encoding*/
    ps_rc_ctxt->i4_max_inter_frm_int =
        1 << (ps_init_prms->s_coding_tools_prms.i4_max_temporal_layers + ps_rc_ctxt->i4_field_pic);
    ps_rc_ctxt->i4_max_temporal_lyr = ps_init_prms->s_coding_tools_prms.i4_max_temporal_layers;
    /*Number of picture types used if different models are used for hierarchial B frames*/

    if(i4_idr_period == 1 || i4_cdr_period == 1)
        ps_rc_ctxt->i4_num_active_pic_type = 1;
    else
        ps_rc_ctxt->i4_num_active_pic_type =
            2 + ps_init_prms->s_coding_tools_prms.i4_max_temporal_layers;

    ps_rc_ctxt->i4_quality_preset =
        ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset;

    if(ps_rc_ctxt->i4_quality_preset == IHEVCE_QUALITY_P7)
    {
        ps_rc_ctxt->i4_quality_preset = IHEVCE_QUALITY_P6;
    }

    ps_rc_ctxt->i4_rc_pass = ps_init_prms->s_pass_prms.i4_pass;
    ps_rc_ctxt->i8_num_gop_mem_alloc = 0;

    ps_rc_ctxt->u1_is_mb_level_rc_on = 0; /*no mb level RC*/

    ps_rc_ctxt->i4_is_infinite_gop = 0;
    ps_rc_ctxt->u1_bit_depth = (UWORD8)ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth;

    //ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset = ((ps_init_prms->s_src_prms.i4_bit_depth-8)*6);
    ps_rc_quant->i1_qp_offset = ((ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8) * 6);

    ps_rc_quant->i2_max_qp = MIN(ps_init_prms->s_config_prms.i4_max_frame_qp,
                                 51);  // FOR Encoder
    ps_rc_quant->i2_min_qp =
        MAX(-(ps_rc_quant->i1_qp_offset), ps_init_prms->s_config_prms.i4_min_frame_qp);

    if(ps_init_prms->s_lap_prms.i4_rc_look_ahead_pics)
    {
        ps_rc_ctxt->i4_num_frame_in_lap_window =
            ps_init_prms->s_lap_prms.i4_rc_look_ahead_pics + MIN_L1_L0_STAGGER_NON_SEQ;
    }
    else
        ps_rc_ctxt->i4_num_frame_in_lap_window = 0;

    if(i4_cdr_period > 0 && i4_idr_period > 0)
    {
        /*both IDR and CDR are positive*/
        //WORD32 i4_rem;
        ps_rc_ctxt->u4_intra_frame_interval = i4_cdr_period;
        ps_rc_ctxt->u4_idr_period = i4_idr_period;

        /*Allow configuration where IDR period is multiple of CDR period. Though any configuiration is supported by LAP rate control
        does not handle assymeteric GOPS, Bit-allocation is exposed to CDR or IDR. It treats everything as I pic*/
    }
    else if(!i4_idr_period && i4_cdr_period > 0)
    {
        ps_rc_ctxt->u4_intra_frame_interval = i4_cdr_period;
        ps_rc_ctxt->u4_idr_period = 0;
    }
    else if(!i4_cdr_period && i4_idr_period > 0)
    {
        ps_rc_ctxt->u4_intra_frame_interval = i4_idr_period;
        ps_rc_ctxt->u4_idr_period = i4_idr_period;
    }
    else
    {
        /*ASSERT(0);*/

        ps_rc_ctxt->u4_intra_frame_interval =
            INFINITE_GOP_CDR_TIME_S *
            ((ps_init_prms->s_src_prms.i4_frm_rate_num /
              (ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_frm_rate_scale_factor *
               ps_init_prms->s_src_prms.i4_frm_rate_denom)));
        ps_rc_ctxt->u4_idr_period = 0;
        ps_rc_ctxt->i4_is_infinite_gop = 1;
    }

    /*If cdr period is 0 then only it is closed gop*/
    ps_rc_ctxt->i4_is_gop_closed = 0;
    if(i4_cdr_period == 0)
    {
        ps_rc_ctxt->i4_is_gop_closed = 1;
    }
    /*This is required because the intra sad returned by non I pic is not correct. Use only I pic sad for next I pic qp calculation*/
    ps_rc_ctxt->i4_use_est_intra_sad = 0;
    ps_rc_ctxt->u4_src_ticks = 1000;
    ps_rc_ctxt->u4_tgt_ticks = 1000;
    ps_rc_ctxt->i4_auto_generate_init_qp = 1;

    ps_rc_ctxt->i8_prev_i_frm_cost = 0;

    for(i = 0; i < MAX_PIC_TYPE; i++)
    {
        /* -1 cost indicates the picture type not been encoded*/
        ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[i] = -1;
        ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i] = -1;
        ps_rc_ctxt->ai8_prev_frame_hme_sad[i] = -1;
        ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i] = -1;
        /*L1 state metrics*/
        ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[i] = -1;
        ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[i] = -1;
        ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_sad[i] = -1;
        /* SGI & Enc Loop Parallelism related changes*/
        ps_rc_ctxt->s_l1_state_metric.au4_prev_scene_num[i] = 0;
        ps_rc_ctxt->au4_prev_scene_num_pre_enc[i] = 0xFFFFFFFF;
        ps_rc_ctxt->ai4_qp_for_previous_scene_pre_enc[i] = 0;
    }
    ps_rc_ctxt->u4_scene_num_est_L0_intra_sad_available = 0xFFFFFFFF;

    for(i = 0; i < MAX_NON_REF_B_PICS_IN_QUEUE_SGI; i++)
    {
        ps_rc_ctxt->as_non_ref_b_qp[i].i4_enc_order_num_rc = 0x7FFFFFFF;
        ps_rc_ctxt->as_non_ref_b_qp[i].i4_non_ref_B_pic_qp = 0x7FFFFFFF;
        ps_rc_ctxt->as_non_ref_b_qp[i].u4_scene_num_rc = MAX_SCENE_NUM + 1;
    }
    ps_rc_ctxt->i4_non_ref_B_ctr = 0;
    ps_rc_ctxt->i4_prev_qp_ctr = 0;
    ps_rc_ctxt->i4_cur_scene_num = 0;

    /*init = 0 set to 1 when atleast one frame of each picture type has completed L1 stage*/
    ps_rc_ctxt->i4_is_est_L0_intra_sad_available = 0;

    /*Min and max qp from user*/
    ps_rc_ctxt->i4_min_frame_qp = ps_init_prms->s_config_prms.i4_min_frame_qp;
    ps_rc_ctxt->i4_max_frame_qp = ps_init_prms->s_config_prms.i4_max_frame_qp;
    ASSERT(ps_rc_ctxt->i4_min_frame_qp >= ps_rc_quant->i2_min_qp);
    ASSERT(ps_rc_ctxt->i4_max_frame_qp <= ps_rc_quant->i2_max_qp);
    /*bitrate init*/
    /*take average bitrate from comfig file*/
    i4_avg_bitrate = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
                         .ai4_tgt_bitrate[i4_bitrate_instance_id];

    if((ps_init_prms->s_config_prms.i4_rate_control_mode == VBR_STREAMING) &&
       (ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
            .ai4_peak_bitrate[i4_bitrate_instance_id] < (1050 * (i4_avg_bitrate / 1000))))
    {
        ps_init_prms->s_config_prms.i4_rate_control_mode = CBR_NLDRC;
    }

    ps_rc_ctxt->e_rate_control_type = (rc_type_e)ps_init_prms->s_config_prms.i4_rate_control_mode;
    ps_rc_ctxt->i4_capped_vbr_flag = 0;
    if(1 == ps_init_prms->s_config_prms.i4_rate_control_mode)
    {
        /* The path taken by capped vbr mode is same as normal VBR mode. Only a flag needs to be enabled
           which tells the rc module that encoder is running in capped vbr mode */
        ps_rc_ctxt->e_rate_control_type = VBR_STREAMING;
        ps_rc_ctxt->i4_capped_vbr_flag = 1;
    }
    ASSERT(
        (ps_rc_ctxt->e_rate_control_type == CBR_NLDRC) ||
        (ps_rc_ctxt->e_rate_control_type == CONST_QP) ||
        (ps_rc_ctxt->e_rate_control_type == VBR_STREAMING));

    ps_rc_ctxt->u4_avg_bit_rate = i4_avg_bitrate;
    for(i = 0; i < MAX_PIC_TYPE; i++)
    {
        if(ps_rc_ctxt->e_rate_control_type == VBR_STREAMING)
        {
            ps_rc_ctxt->au4_peak_bit_rate[i] =
                ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
                    .ai4_peak_bitrate[i4_bitrate_instance_id];
        }
        else
        {
            /*peak bitrate parameter is ignored in CBR*/
            ps_rc_ctxt->au4_peak_bit_rate[i] = i4_avg_bitrate;
        }
    }
    ps_rc_ctxt->u4_min_bit_rate = i4_avg_bitrate;

    /*buffer size init*/
    u4_buf_size = (WORD32)(ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
                               .ai4_max_vbv_buffer_size[i4_bitrate_instance_id]);
    ps_rc_ctxt->u4_max_delay = (UWORD32)(
        (float)u4_buf_size / i4_avg_bitrate * 1000); /*delay in milli-seconds based on buffer size*/
    ps_rc_ctxt->u4_max_vbv_buff_size = u4_buf_size; /*buffer size should be in bits*/
    /*This dictates the max deviaiton allowed for file size in VBR mode. */
    ps_rc_ctxt->f_vbr_max_peak_sustain_dur =
        ((float)ps_init_prms->s_config_prms.i4_vbr_max_peak_rate_dur) / 1000;
    ps_rc_ctxt->i8_num_frms_to_encode = (WORD32)ps_init_prms->s_config_prms.i4_num_frms_to_encode;
    i4_peak_bitrate_factor = (ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
                                  .ai4_peak_bitrate[i4_bitrate_instance_id] /
                              i4_avg_bitrate) *
                             1000;
    {
        //float f_delay = ((float)ps_init_prms->s_config_prms.i4_max_vbv_buffer_size*1000)/i4_peak_bitrate_factor;
        float f_delay = ((float)ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
                             .ai4_max_vbv_buffer_size[i4_bitrate_instance_id] *
                         1000) /
                        ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
                            .ai4_peak_bitrate[i4_bitrate_instance_id];
        ps_rc_ctxt->i4_initial_decoder_delay_frames = (WORD32)(
            ((f_delay) * (ps_init_prms->s_src_prms.i4_frm_rate_num /
                          (ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
                               .i4_frm_rate_scale_factor *
                           ps_init_prms->s_src_prms.i4_frm_rate_denom))) /
            1000);
    }
    /*Initial buffer fullness*/
    ps_rc_ctxt->i4_init_vbv_fullness = ps_init_prms->s_config_prms.i4_init_vbv_fullness;

    /*Init Qp updation. This seems to be used for pre enc stage of second frame. Needs to be looked into*/
    ps_rc_ctxt->i4_init_frame_qp_user = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
                                            .ai4_frame_qp[i4_bitrate_instance_id];

    for(i = 0; i < MAX_SCENE_NUM; i++)
    {
        for(j = 0; j < MAX_PIC_TYPE; j++)
            ps_rc_ctxt->ai4_prev_pic_hevc_qp[i][j] = INIT_HEVCE_QP_RC;
    }
    memset(&ps_rc_ctxt->ai4_scene_numbers[0], 0, sizeof(ps_rc_ctxt->ai4_scene_numbers));
    memset(&ps_rc_ctxt->ai4_scene_num_last_pic[0], 0, sizeof(ps_rc_ctxt->ai4_scene_num_last_pic));
    ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[0] = ps_rc_ctxt->i4_min_frame_qp - 1;
    ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[1] = ps_rc_ctxt->i4_min_frame_qp - 1;
    /* SGI & Enc Loop Parallelism related changes*/
    for(i = 0; i < MAX_NUM_ENC_LOOP_PARALLEL; i++)
    {
        ps_rc_ctxt->ai8_cur_frm_intra_cost[i] = 0;
        ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i] = 0;
        ps_rc_ctxt->ai4_I_model_only_reset[i] = 0;
        ps_rc_ctxt->ai4_is_non_I_scd_pic[i] = 0;
        ps_rc_ctxt->ai4_is_pause_to_resume[i] = 0;
        ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i] = 0;
        ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i] = 0;
        /*initialize assuming 30 percent intra and 70 percent inter weightage*/
        ps_rc_ctxt->ai4_lap_complexity_q7[i] = MODERATE_LAP2_COMPLEXITY_Q7;

        ps_rc_ctxt->ai4_lap_f_sim[i] = MODERATE_FSIM_VALUE;
    }

    /*Init variables required to handle entropy and rdopt consumption mismatch*/
    ps_rc_ctxt->i4_rdopt_bit_count = 0;
    ps_rc_ctxt->i4_entropy_bit_count = 0;
    for(i = 0; i < NUM_BUF_RDOPT_ENT_CORRECT; i++)
    {
        ps_rc_ctxt->ai4_rdopt_bit_consumption_estimate[i] =
            -1; /*negative bit signifies that value is not populated*/
        ps_rc_ctxt->ai4_rdopt_bit_consumption_buf_id[i] = -1;
        ps_rc_ctxt->ai4_entropy_bit_consumption[i] = -1;
        ps_rc_ctxt->ai4_entropy_bit_consumption_buf_id[i] = -1;
    }

    /** scd model reset related param init*/
    for(i = 0; i < MAX_NUM_TEMPORAL_LAYERS; i++)
    {
        ps_rc_ctxt->au4_scene_num_temp_id[i] = 0;
    }
    /* SGI & Enc Loop Parallelism related changes*/
    for(i = 0; i < MAX_NUM_ENC_LOOP_PARALLEL; i++)
    {
        ps_rc_ctxt->ai4_is_frame_scd[i] = 0;
    }

    /*Stat file pointer passed from applicaition*/
    ps_rc_ctxt->pf_stat_file = NULL;
    ps_rc_ctxt->i8_num_frame_read = 0;

    return ps_rc_ctxt;
}

/*###############################################*/
/******* END OF RC MEM INIT FUNCTIONS **********/
/*###############################################*/

/*###############################################*/
/******* START OF RC INIT FUNCTIONS **************/
/*###############################################*/
/**
******************************************************************************
*
*  @brief Initialises teh Rate control ctxt
*
*  @par   Description
*
*  @param[inout]   pv_ctxt
*  pointer to memory descriptors table
*
*  @param[in]      ps_run_time_src_param
*  Create time static parameters
*
*  @return      void
*
******************************************************************************
*/
void ihevce_rc_init(
    void *pv_ctxt,
    ihevce_src_params_t *ps_run_time_src_param,
    ihevce_tgt_params_t *ps_tgt_params,
    rc_quant_t *ps_rc_quant,
    ihevce_sys_api_t *ps_sys_api,
    ihevce_lap_params_t *ps_lap_prms,
    WORD32 i4_num_frame_parallel)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    WORD32 i, i_temp, j;
    float f_temp;

    /*run time width and height has to considered*/
    ps_rc_ctxt->i4_frame_height = ps_tgt_params->i4_height;
    ps_rc_ctxt->i4_frame_width = ps_tgt_params->i4_width;
    ps_rc_ctxt->i4_field_pic = ps_run_time_src_param->i4_field_pic;
    ps_rc_ctxt->i8_num_bit_alloc_period = 0;
    ps_rc_ctxt->i8_new_bitrate = -1; /*-1 indicates no dynamic change in bitrate request pending*/
    ps_rc_ctxt->i8_new_peak_bitrate = -1;

    ps_rc_ctxt->i4_is_last_frame_scan = 0;

    memset(ps_rc_ctxt->ai4_offsets, 0, 5 * sizeof(WORD32));

    ps_rc_ctxt->i4_complexity_bin = 5;
    ps_rc_ctxt->i4_last_p_or_i_frame_gop = 0;
    ps_rc_ctxt->i4_qp_at_I_frame_for_skip_sad = 1;
    ps_rc_ctxt->i4_denominator_i_to_avg = 1;
    ps_rc_ctxt->i4_fp_bit_alloc_in_sp = 0;

    ps_rc_ctxt->ai4_offsets[0] = 0;
    ps_rc_ctxt->ai4_offsets[1] = 1;
    ps_rc_ctxt->ai4_offsets[2] = 2;
    ps_rc_ctxt->ai4_offsets[3] = 3;
    ps_rc_ctxt->ai4_offsets[4] = 4;

    ps_rc_ctxt->i4_num_frames_subgop = 0;
    ps_rc_ctxt->i8_total_acc_coarse_me_sad = 0;

    ps_rc_ctxt->i4_L0_frame_qp = 1;

    ps_rc_ctxt->i4_est_text_bits_ctr_get_qp = 0;
    ps_rc_ctxt->i4_est_text_bits_ctr_update_qp = 0;

    /*CAllback functions need to be copied for use inside RC*/
    ps_rc_ctxt->ps_sys_rc_api = ps_sys_api;

    f_temp = ((float)(ps_rc_quant->i2_max_qp + ps_rc_quant->i1_qp_offset - 4) / 6);

    ps_rc_quant->i2_max_qscale = (WORD16)((float)pow(2, f_temp) + 0.5) << 3;

    f_temp = ((float)(ps_rc_quant->i2_min_qp + ps_rc_quant->i1_qp_offset - 4) / 6);

    ps_rc_quant->i2_min_qscale = (WORD16)((float)pow(2, f_temp) + 0.5);

    f_temp =
        ((float)(51 + ps_rc_quant->i1_qp_offset - 4) /
         6);  // default MPEG2 to HEVC and HEVC to MPEG2 Qp conversion tables
    i_temp = (WORD16)((float)pow(2, f_temp) + 0.5);

    i_temp = (i_temp << 3);  // Q3 format is mantained for accuarate calc at lower qp

    for(i = 0; i <= i_temp; i++)
    {
        ps_rc_quant->pi4_qscale_to_qp[i] =
            ihevce_rc_get_scaled_hevce_qp_q3(i, ps_rc_ctxt->u1_bit_depth);
    }

    for(i = (0 - ps_rc_quant->i1_qp_offset); i <= 51; i++)
    {
        ps_rc_quant->pi4_qp_to_qscale_q_factor[i + ps_rc_quant->i1_qp_offset] =
            ihevce_rc_get_scaled_mpeg2_qp_q6(
                i + ps_rc_quant->i1_qp_offset, ps_rc_ctxt->u1_bit_depth);
        ps_rc_quant->pi4_qp_to_qscale[i + ps_rc_quant->i1_qp_offset] =
            ((ps_rc_quant->pi4_qp_to_qscale_q_factor[i + ps_rc_quant->i1_qp_offset] +
              (1 << (QSCALE_Q_FAC_3 - 1))) >>
             QSCALE_Q_FAC_3);
    }

    if(ps_rc_quant->i2_min_qscale < 1)
    {
        ps_rc_quant->i2_min_qscale = 1;
    }

    ps_rc_ctxt->ps_rc_quant_ctxt = ps_rc_quant;

    /*Frame rate init*/
    ps_rc_ctxt->u4_max_frame_rate =
        ps_run_time_src_param->i4_frm_rate_num / ps_tgt_params->i4_frm_rate_scale_factor;
    ps_rc_ctxt->i4_top_field_first = ps_run_time_src_param->i4_topfield_first; /**/
    /*min and max qp initialization*/
    if(ps_rc_ctxt->i4_field_pic == 0)
    {
        WORD32 i4_max_qp = 0;

        if(ps_rc_ctxt->u1_bit_depth == 10)
        {
            i4_max_qp = MAX_HEVC_QP_10bit;
        }
        else if(ps_rc_ctxt->u1_bit_depth == 12)
        {
            i4_max_qp = MAX_HEVC_QP_12bit;
        }
        else
        {
            i4_max_qp = MAX_HEVC_QP;
        }

        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            if((ps_rc_ctxt->i4_init_frame_qp_user + (2 * i) +
                ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset) <=
               i4_max_qp)  //BUG_FIX related to init QP allocation
            {
                ps_rc_ctxt->ai4_init_qp[i] = (ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale
                                                  [(ps_rc_ctxt->i4_init_frame_qp_user + (2 * i)) +
                                                   ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset] +
                                              (1 << (QSCALE_Q_FAC_3 - 1))) >>
                                             QSCALE_Q_FAC_3;
            }
            else
            {
                ps_rc_ctxt->ai4_init_qp[i] =
                    (ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale[i4_max_qp] +
                     (1 << (QSCALE_Q_FAC_3 - 1))) >>
                    QSCALE_Q_FAC_3;  // + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset];
            }
            ps_rc_ctxt->ai4_min_max_qp[i * 2] =
                ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qscale; /*min qp for each picture type*/
            ps_rc_ctxt->ai4_min_max_qp[i * 2 + 1] = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qscale >>
                                                    QSCALE_Q_FAC_3; /*max qp for each picture type*/
        }
    }
    else
    {
        WORD32 i4_num_pic_types = MAX_PIC_TYPE;
        WORD32 i4_max_qp = 0;

        if(ps_rc_ctxt->u1_bit_depth == 10)
        {
            i4_max_qp = MAX_HEVC_QP_10bit;
        }
        else if(ps_rc_ctxt->u1_bit_depth == 12)
        {
            i4_max_qp = MAX_HEVC_QP_12bit;
        }
        else
        {
            i4_max_qp = MAX_HEVC_QP;
        }

        i4_num_pic_types >>= 1;

        for(i = 0; i < i4_num_pic_types; i++)
        {
            if((ps_rc_ctxt->i4_init_frame_qp_user + (2 * i) +
                ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset) <= i4_max_qp)
            {
                ps_rc_ctxt->ai4_init_qp[i] = (ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale
                                                  [(ps_rc_ctxt->i4_init_frame_qp_user + (2 * i)) +
                                                   ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset] +
                                              (1 << (QSCALE_Q_FAC_3 - 1))) >>
                                             QSCALE_Q_FAC_3;

                if(i != 0)
                    ps_rc_ctxt->ai4_init_qp[i + FIELD_OFFSET] = ps_rc_ctxt->ai4_init_qp[i];
            }
            else
            {
                ps_rc_ctxt->ai4_init_qp[i] =
                    (ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale[i4_max_qp] +
                     (1 << (QSCALE_Q_FAC_3 - 1))) >>
                    QSCALE_Q_FAC_3;  // + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset];

                if(i != 0)
                    ps_rc_ctxt->ai4_init_qp[i + FIELD_OFFSET] = ps_rc_ctxt->ai4_init_qp[i];
            }
            ps_rc_ctxt->ai4_min_max_qp[i * 2] =
                ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qscale; /*min qp for each picture type*/
            ps_rc_ctxt->ai4_min_max_qp[i * 2 + 1] = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qscale >>
                                                    QSCALE_Q_FAC_3; /*max qp for each picture type*/
            if(i != 0)
            {
                ps_rc_ctxt->ai4_min_max_qp[(i + FIELD_OFFSET) * 2] =
                    ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qscale; /*min qp for each picture type*/
                ps_rc_ctxt->ai4_min_max_qp[(i + FIELD_OFFSET) * 2 + 1] =
                    ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qscale; /*max qp for each picture type*/
            }
        }
    }

    for(j = 0; i < MAX_NUM_ENC_LOOP_PARALLEL; i++)
    {
        /*initialise the coeffs to 1 in case lap is not used */
        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            ps_rc_ctxt->af_sum_weigh[j][i][0] = 1.0;
            ps_rc_ctxt->af_sum_weigh[j][i][1] = 0.0;
            ps_rc_ctxt->af_sum_weigh[j][i][2] = 0.0;
        }
    }

    ps_rc_ctxt->i4_num_frame_parallel = i4_num_frame_parallel;  //ELP_RC
    i4_num_frame_parallel = (i4_num_frame_parallel > 1) ? i4_num_frame_parallel : 0;

    if(ps_rc_ctxt->i4_num_frame_parallel > 1)
    {
        ps_rc_ctxt->i4_pre_enc_rc_delay = MAX_PRE_ENC_RC_DELAY;
    }
    else
    {
        ps_rc_ctxt->i4_pre_enc_rc_delay = MIN_PRE_ENC_RC_DELAY;
    }
    /*Bitrate and resolutioon based scene cut min qp*/
    {
        /*The min qp for scene cut frame is chosen based on bitrate*/
        float i4_bpp = ((float)ps_rc_ctxt->u4_avg_bit_rate / ps_rc_ctxt->u4_max_frame_rate) * 1000 /
                       (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width);
        if(ps_rc_ctxt->u4_intra_frame_interval == 1)
        {
            /*Ultra High resolution)*/
            if((ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) > 5000000)
            {
                if(i4_bpp > 0.24)
                {
                    ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP_VHBR;
                }
                else if(i4_bpp > 0.16)
                    ps_rc_ctxt->i4_min_scd_hevc_qp =
                        SCD_MIN_HEVC_QP_HBR; /*corresponds to bitrate greater than 40mbps for 4k 30p*/
                else
                    ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP;
            }
            else
            {
                if(i4_bpp > 0.32)
                {
                    ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP_VHBR;
                }
                else if(i4_bpp > 0.24)
                    ps_rc_ctxt->i4_min_scd_hevc_qp =
                        SCD_MIN_HEVC_QP_HBR; /*corresponds to bitrate greater than 15mbps for 1080 30p*/
                else
                    ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP;
            }
        }
        else
        {
            /*Ultra High resolution)*/
            if((ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) > 5000000)
            {
                if(i4_bpp > 0.16)
                {
                    ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP_VHBR;
                }
                else if(i4_bpp > 0.08)
                    ps_rc_ctxt->i4_min_scd_hevc_qp =
                        SCD_MIN_HEVC_QP_HBR; /*corresponds to bitrate greater than 20mbps for 4k 30p*/
                else
                    ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP;
            }
            else
            {
                /*Resolution lesser than full HD (including )*/
                if(i4_bpp > 0.24)
                {
                    ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP_VHBR;
                }
                else if(i4_bpp > 0.16)
                    ps_rc_ctxt->i4_min_scd_hevc_qp =
                        SCD_MIN_HEVC_QP_HBR; /*corresponds to bitrate greater than 10mbps for 1080 30p*/
                else
                    ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP;
            }
        }
    }

    initialise_rate_control(
        ps_rc_ctxt->rc_hdl,
        ps_rc_ctxt->e_rate_control_type,
        ps_rc_ctxt->u1_is_mb_level_rc_on,  //0,/*disabling MB level RC*/
        ps_rc_ctxt->u4_avg_bit_rate,
        ps_rc_ctxt->au4_peak_bit_rate,
        ps_rc_ctxt->u4_min_bit_rate,
        ps_rc_ctxt->u4_max_frame_rate,
        ps_rc_ctxt->u4_max_delay, /*max delay in milli seconds based on buffer size*/
        ps_rc_ctxt->u4_intra_frame_interval,
        ps_rc_ctxt->u4_idr_period,
        ps_rc_ctxt->ai4_init_qp,
        ps_rc_ctxt->u4_max_vbv_buff_size,
        ps_rc_ctxt->i4_max_inter_frm_int,
        ps_rc_ctxt->i4_is_gop_closed,
        ps_rc_ctxt->ai4_min_max_qp, /*min and max qp to be used for each of picture type*/
        ps_rc_ctxt->i4_use_est_intra_sad,
        ps_rc_ctxt->u4_src_ticks,
        ps_rc_ctxt->u4_tgt_ticks,
        ps_rc_ctxt->i4_frame_height, /*pels in frame considering 420 semi planar format*/
        ps_rc_ctxt->i4_frame_width,
        ps_rc_ctxt->i4_num_active_pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_rc_ctxt->i4_quality_preset,
        ps_rc_ctxt->i4_num_frame_in_lap_window,
        ps_rc_ctxt->i4_initial_decoder_delay_frames,
        ps_rc_ctxt->f_vbr_max_peak_sustain_dur,
        ps_rc_ctxt->i8_num_frms_to_encode,
        ps_rc_ctxt->i4_min_scd_hevc_qp,
        ps_rc_ctxt->u1_bit_depth,
        ps_rc_ctxt->pf_stat_file,
        ps_rc_ctxt->i4_rc_pass,
        ps_rc_ctxt->pv_gop_stat,
        ps_rc_ctxt->i8_num_gop_mem_alloc,
        ps_rc_ctxt->i4_is_infinite_gop,
        sizeof(ihevce_lap_output_params_t),
        sizeof(rc_lap_out_params_t),
        (void *)ps_sys_api,
        ps_rc_ctxt->i4_fp_bit_alloc_in_sp,
        i4_num_frame_parallel,
        ps_rc_ctxt->i4_capped_vbr_flag);

    //ps_rc_ctxt->i4_init_vbv_fullness = 500000;
    rc_init_set_ebf(ps_rc_ctxt->rc_hdl, ps_rc_ctxt->i4_init_vbv_fullness);

    /*get init qp based on ebf for rate control*/
    if(ps_rc_ctxt->e_rate_control_type != CONST_QP)
    {
        WORD32 I_frame_qp, I_frame_mpeg2_qp;
        /*assume moderate fsim*/
        WORD32 i4_fsim_global = MODERATE_FSIM_VALUE;
        I_frame_mpeg2_qp = rc_get_bpp_based_scene_cut_qp(
            ps_rc_ctxt->rc_hdl,
            I_PIC,
            ((3 * ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 1),
            i4_fsim_global,
            ps_rc_ctxt->af_sum_weigh[0],
            1);

        I_frame_qp = ihevce_rc_get_scaled_hevc_qp_from_qs_q3(
            I_frame_mpeg2_qp << QSCALE_Q_FAC_3, ps_rc_ctxt->ps_rc_quant_ctxt);

        I_frame_qp = I_frame_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset;

        if(I_frame_qp > 44)
            I_frame_qp = 44;

        ps_rc_ctxt->ai4_init_pre_enc_qp[I_PIC] = I_frame_qp;
        ps_rc_ctxt->ai4_init_pre_enc_qp[P_PIC] = I_frame_qp + 1;
        ps_rc_ctxt->ai4_init_pre_enc_qp[B_PIC] = I_frame_qp + 2;
        ps_rc_ctxt->ai4_init_pre_enc_qp[B1_PIC] = I_frame_qp + 3;
        ps_rc_ctxt->ai4_init_pre_enc_qp[B2_PIC] = I_frame_qp + 4;
        /*Bottom fields*/
        ps_rc_ctxt->ai4_init_pre_enc_qp[P1_PIC] = I_frame_qp + 1;
        ps_rc_ctxt->ai4_init_pre_enc_qp[BB_PIC] = I_frame_qp + 2;
        ps_rc_ctxt->ai4_init_pre_enc_qp[B11_PIC] = I_frame_qp + 3;
        ps_rc_ctxt->ai4_init_pre_enc_qp[B22_PIC] = I_frame_qp + 4;

        ps_rc_ctxt->i4_pre_enc_qp_read_index = 0;
        ps_rc_ctxt->i4_pre_enc_qp_write_index = ps_rc_ctxt->i4_pre_enc_rc_delay - 1;
        for(i = 0; i < ps_rc_ctxt->i4_pre_enc_rc_delay; i++)
        {
            /*initialize it to -1 to indicate it as not produced*/
            ps_rc_ctxt->as_pre_enc_qp_queue[i].i4_is_qp_valid = -1;
        }
        for(i = 0; i < (ps_rc_ctxt->i4_pre_enc_qp_write_index); i++)
        {
            WORD32 j;
            ps_rc_ctxt->as_pre_enc_qp_queue[i].i4_is_qp_valid = 1;
            for(j = 0; j < MAX_PIC_TYPE; j++)
            {
                ps_rc_ctxt->as_pre_enc_qp_queue[i].ai4_quant[j] =
                    ps_rc_ctxt->ai4_init_pre_enc_qp[j];
                ps_rc_ctxt->as_pre_enc_qp_queue[i].i4_scd_qp =
                    ps_rc_ctxt->ai4_init_pre_enc_qp[I_PIC];
            }
        }

        ps_rc_ctxt->i4_use_qp_offset_pre_enc = 1;
        ps_rc_ctxt->i4_num_frms_from_reset = 0;
        /* SGI & Enc Loop Parallelism related changes*/
        ps_rc_ctxt->u4_prev_scene_num = 0;
        //ps_rc_ctxt->i4_use_init_qp_for_pre_enc = 0;
        for(j = 0; j < MAX_NON_REF_B_PICS_IN_QUEUE_SGI; j++)
        {
            ps_rc_ctxt->au4_prev_scene_num_multi_scene[j] = 0x3FFFFFFF;
            for(i = 0; i < MAX_PIC_TYPE; i++)
            {
                ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[j][i] =
                    ps_rc_ctxt->ai4_init_pre_enc_qp[i];
            }
        }

        /* SGI & Enc Loop Parallelism related changes*/
        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            ps_rc_ctxt->ai4_qp_for_previous_scene[i] = ps_rc_ctxt->ai4_init_pre_enc_qp[i];
        }
    }
    else
    {
        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            ps_rc_ctxt->ai4_init_pre_enc_qp[i] = ps_rc_ctxt->i4_init_frame_qp_user;
            ps_rc_ctxt->ai4_qp_for_previous_scene[i] = ps_rc_ctxt->i4_init_frame_qp_user;
        }
    }
}

/**
******************************************************************************
*
*  @brief Populate common params from lap_out structure to rc_lap_out structure
*         Also the init of some rc_lap_out params done here
*  @par   Description
*
*  @param[in]   ps_lap_out
*  pointer to lap_out structure
*
*  @param[out]      ps_rc_lap_out
*  pointer to rc_lap_out structure
*
*  @return      void
*
******************************************************************************
*/

void ihevce_rc_populate_common_params(
    ihevce_lap_output_params_t *ps_lap_out, rc_lap_out_params_t *ps_rc_lap_out)
{
    /* Update common params */

    ps_rc_lap_out->i4_rc_pic_type = ps_lap_out->i4_pic_type;
    ps_rc_lap_out->i4_rc_poc = ps_lap_out->i4_poc;
    ps_rc_lap_out->i4_rc_temporal_lyr_id = ps_lap_out->i4_temporal_lyr_id;
    ps_rc_lap_out->i4_rc_is_ref_pic = ps_lap_out->i4_is_ref_pic;
    ps_rc_lap_out->i4_rc_scene_type = ps_lap_out->i4_scene_type;
    ps_rc_lap_out->u4_rc_scene_num = ps_lap_out->u4_scene_num;
    ps_rc_lap_out->i4_rc_display_num = ps_lap_out->i4_display_num;
    ps_rc_lap_out->i4_rc_quality_preset = ps_lap_out->i4_quality_preset;
    ps_rc_lap_out->i4_rc_first_field = ps_lap_out->i4_first_field;

    /*params populated in LAP-2*/
    ps_rc_lap_out->i8_frame_acc_coarse_me_cost = -1;
    memset(ps_rc_lap_out->ai8_frame_acc_coarse_me_sad, -1, sizeof(WORD32) * 52);

    ps_rc_lap_out->i8_pre_intra_satd = -1;

    ps_rc_lap_out->i8_raw_pre_intra_sad = -1;

    ps_rc_lap_out->i8_raw_l1_coarse_me_sad = -1;

    ps_rc_lap_out->i4_is_rc_model_needs_to_be_updated = 1;
    /* SGI & Enc Loop Parallelism related changes*/
    ps_rc_lap_out->i4_ignore_for_rc_update = 0;

    /*For 1 pass HQ I frames*/

    ps_rc_lap_out->i4_complexity_bin = 5;
    {
        WORD32 ai4_offsets[5] = { 0, 1, 2, 3, 4 };
        memmove(ps_rc_lap_out->ai4_offsets, ai4_offsets, sizeof(WORD32) * 5);
        ps_rc_lap_out->i4_offsets_set_flag = -1;
    }

    ps_rc_lap_out->i4_L1_qp = -1;
    ps_rc_lap_out->i4_L0_qp = -1;
}

/*###############################################*/
/******* END OF RC INIT FUNCTIONS **************/
/*###############################################*/

/*#########################################################*/
/******* START OF PRE-ENC QP QUERY FUNCTIONS **************/
/*#######################################################*/

/**
******************************************************************************
*
*  @name  ihevce_rc_get_bpp_based_frame_qp
*
*  @par   Description
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*               ps_rc_lap_out
*  @return      frame qp
*
******************************************************************************
*/
WORD32 ihevce_rc_get_bpp_based_frame_qp(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    WORD32 i4_frame_qs_q3, i4_hevc_frame_qp, i;
    frame_info_t *ps_frame_info;
    picture_type_e rc_pic_type = ihevce_rc_conv_pic_type(
        (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_rc_lap_out->i4_rc_temporal_lyr_id,
        ps_rc_lap_out->i4_is_bottom_field,
        ps_rc_ctxt->i4_top_field_first);
    /*initialise the coeffs to 1 in case lap is not used */
    for(i = 0; i < MAX_PIC_TYPE; i++)
    {
        ps_rc_ctxt->af_sum_weigh[0][i][0] = 1.0;
        ps_rc_ctxt->af_sum_weigh[0][i][1] = 0.0;
        ps_rc_ctxt->af_sum_weigh[0][i][2] = 0.0;
    }
    {
        /*scene cut handling during pre-enc stage*/
        /*assume lap fsim as 117. not used since ratio is direclt sent*/
        if(ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_SCENE_CUT)
        {
            for(i = 0; i < MAX_PIC_TYPE; i++)
            {
                ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i] = -1;
                ps_rc_ctxt->ai8_prev_frame_hme_sad[i] = -1;
                ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i] = -1;
            }
            ps_rc_ctxt->i4_is_est_L0_intra_sad_available = 0;
        }

        if(ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_SCENE_CUT ||
           !ps_rc_ctxt->i4_is_est_L0_intra_sad_available)
        {
            /*compute bpp based qp if current frame is scene cut or data is not sufficient*/
            i4_frame_qs_q3 = rc_get_bpp_based_scene_cut_qp(
                ps_rc_ctxt->rc_hdl,
                I_PIC,
                ((3 * ps_rc_lap_out->i4_num_pels_in_frame_considered) >> 1),
                117,
                ps_rc_ctxt->af_sum_weigh[0],
                0);
            i4_frame_qs_q3 = i4_frame_qs_q3 << QSCALE_Q_FAC_3;
        }
        else
        {
            /*using previous one sub-gop data calculate i to rest ratio and qp assuming it is I frame*/
            WORD32 i4_num_b, i, ai4_pic_dist[MAX_PIC_TYPE], index, i4_total_bits;
            LWORD64 i8_average_pre_intra_sad = 0, i8_average_est_l0_satd_by_act = 0;
            double lambda_modifier[MAX_PIC_TYPE], complexity[MAX_PIC_TYPE], den = 0.0f,
                                                                            i_to_rest_bit_ratio;
            WORD32 i4_curr_bits_estimated = 0;

            for(i = 0; i < MAX_PIC_TYPE; i++)
            {
                complexity[i] = 0;
                lambda_modifier[i] = 0;
                ai4_pic_dist[i] = 0;
            }

            index = ihevce_get_offline_index(
                ps_rc_ctxt, ps_rc_lap_out->i4_num_pels_in_frame_considered);
            if(ps_rc_ctxt->i4_max_temporal_lyr)
            {
                i4_num_b = ((WORD32)pow((float)2, ps_rc_ctxt->i4_max_temporal_lyr)) - 1;
            }
            else
            {
                i4_num_b = 0;
            }

            lambda_modifier[I_PIC] =
                ihevce_get_frame_lambda_modifier((WORD8)I_PIC, 0, 1, 1, i4_num_b);
            lambda_modifier[P_PIC] =
                ihevce_get_frame_lambda_modifier((WORD8)P_PIC, 0, 1, 1, i4_num_b) *
                pow((float)1.125, 1);
            lambda_modifier[B_PIC] =
                ihevce_get_frame_lambda_modifier(
                    (WORD8)B_PIC, 1, (ps_rc_ctxt->i4_max_temporal_lyr > 1), 1, i4_num_b) *
                pow((float)1.125, 2);
            lambda_modifier[B1_PIC] =
                ihevce_get_frame_lambda_modifier(
                    (WORD8)B1_PIC, 2, 1, (ps_rc_ctxt->i4_max_temporal_lyr > 2), i4_num_b) *
                pow((float)1.125, 3);
            lambda_modifier[B2_PIC] =
                ihevce_get_frame_lambda_modifier((WORD8)B2_PIC, 3, 1, 0, i4_num_b) *
                pow((float)1.125, 4);

            /*consider average of one sub-gop for intra sad*/

            if(ps_rc_ctxt->i4_quality_preset == IHEVCE_QUALITY_P6)
            {
                for(i = 0; i < 2; i++)
                {
                    i8_average_pre_intra_sad += ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i];
                    i8_average_est_l0_satd_by_act += ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i];
                    if(ps_rc_ctxt->i4_field_pic == 1 && i != 0)
                    {
                        i8_average_pre_intra_sad +=
                            ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i + FIELD_OFFSET];
                        i8_average_est_l0_satd_by_act +=
                            ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i + FIELD_OFFSET];
                    }
                }
                if(ps_rc_ctxt->i4_field_pic == 1)
                {
                    i8_average_pre_intra_sad /= 3;
                    i8_average_est_l0_satd_by_act /= 3;
                }
                else
                {
                    i8_average_pre_intra_sad <<= 1;
                    i8_average_est_l0_satd_by_act <<= 1;
                }
            }
            else
            {
                for(i = 0; i < ps_rc_ctxt->i4_num_active_pic_type; i++)
                {
                    i8_average_pre_intra_sad += ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i];
                    i8_average_est_l0_satd_by_act += ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i];
                    if(ps_rc_ctxt->i4_field_pic == 1 && i != 0)
                    {
                        i8_average_pre_intra_sad +=
                            ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i + FIELD_OFFSET];
                        i8_average_est_l0_satd_by_act +=
                            ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i + FIELD_OFFSET];
                    }
                }
                if(ps_rc_ctxt->i4_field_pic == 1)
                {
                    i8_average_pre_intra_sad /= ((i << 1) - 1);
                    i8_average_est_l0_satd_by_act /= ((i << 1) - 1);
                }
                else
                {
                    i8_average_pre_intra_sad /= i;
                    i8_average_est_l0_satd_by_act /= i;
                }
            }

            /*no lambda modifier is considered for I pic as other lambda are scaled according to I frame lambda*/
            complexity[I_PIC] = (double)i8_average_pre_intra_sad;

            for(i = 1; i < ps_rc_ctxt->i4_num_active_pic_type; i++)
            {
#if !USE_SQRT
                complexity[i] = ps_rc_ctxt->ai8_prev_frame_hme_sad[i] / pow(1.125, i);

                if(ps_rc_ctxt->i4_field_pic == 1)
                {
                    complexity[i + FIELD_OFFSET] =
                        ps_rc_ctxt->ai8_prev_frame_hme_sad[i + FIELD_OFFSET] / pow(1.125, i);
                }
#else
                complexity[i] = ps_rc_ctxt->ai8_prev_frame_hme_sad[i] /
                                (sqrt(lambda_modifier[i] / lambda_modifier[I_PIC]) * pow(1.125, i));
#endif
            }
            /*get picture type distribution in LAP*/
            rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]);

            for(i = 0; i < MAX_PIC_TYPE; i++)
            {
                den += complexity[i] * ai4_pic_dist[i];
            }
            /*subtract I frame complexity to get I to rest ratio*/
            {
                WORD32 num_inter_pic = 0;
                for(i = 1; i < MAX_PIC_TYPE; i++)
                {
                    num_inter_pic += ai4_pic_dist[i];
                }
                if(num_inter_pic > 0)
                    den = (den - (complexity[I_PIC] * ai4_pic_dist[I_PIC])) / num_inter_pic;
                else
                    den = complexity[I_PIC];
            }

            if(den > 0)
                i_to_rest_bit_ratio = (float)((complexity[I_PIC]) / den);
            else
                i_to_rest_bit_ratio = 15;

            /*get qp for scene cut frame based on offline data*/
            i4_frame_qs_q3 = rc_get_qp_for_scd_frame(
                ps_rc_ctxt->rc_hdl,
                I_PIC,
                i8_average_est_l0_satd_by_act,
                ps_rc_lap_out->i4_num_pels_in_frame_considered,
                -1,
                MODERATE_FSIM_VALUE,
                (void *)&g_offline_i_model_coeff[index][0],
                (float)i_to_rest_bit_ratio,
                0,
                ps_rc_ctxt->af_sum_weigh[0],
                ps_rc_lap_out->ps_frame_info,
                ps_rc_ctxt->i4_rc_pass,
                0,
                0,
                0,
                &i4_total_bits,
                &i4_curr_bits_estimated,
                ps_rc_lap_out->i4_use_offline_model_2pass,
                0,
                0,
                -1,
                NULL);
        }

        i4_hevc_frame_qp =
            ihevce_rc_get_scaled_hevc_qp_from_qs_q3(i4_frame_qs_q3, ps_rc_ctxt->ps_rc_quant_ctxt);

        i4_hevc_frame_qp = i4_hevc_frame_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset;

        if(i4_hevc_frame_qp > ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp)
            i4_hevc_frame_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp;

        /*offset depending on current picture type*/
        if(rc_pic_type != I_PIC)
            i4_hevc_frame_qp += ps_rc_lap_out->i4_rc_temporal_lyr_id + 1;
        /*clip min and max qp to be within range*/
        i4_hevc_frame_qp = ihevce_clip_min_max_qp(
            ps_rc_ctxt, i4_hevc_frame_qp, rc_pic_type, ps_rc_lap_out->i4_rc_temporal_lyr_id);

        ps_rc_ctxt->ai4_qp_for_previous_scene_pre_enc[rc_pic_type] = i4_hevc_frame_qp;
        ps_rc_ctxt->au4_prev_scene_num_pre_enc[rc_pic_type] = ps_rc_lap_out->u4_rc_scene_num;
    }

    return i4_hevc_frame_qp;
}
/**
******************************************************************************
*
*  @name  ihevce_rc_get_pre_enc_pic_quant
*
*  @par   Description - Called from ihevce_rc_cal_pre_enc_qp. updates frame qp
*                       which will be used by next frame of same pic type in
*                       pre-enc stage
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*  @return      void
*
******************************************************************************
*/
WORD32
    ihevce_rc_get_pre_enc_pic_quant(void *pv_ctxt, picture_type_e rc_pic_type, WORD32 *pi4_scd_qp)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    WORD32 i4_frame_qp, i4_frame_qp_q6, i4_hevc_frame_qp = -1;
    WORD32 i4_max_frame_bits = (1 << 30);
    WORD32 i4_temporal_layer_id, i4_is_bottom_field, i4_cur_est_texture_bits;

    ihevce_rc_get_pic_param(rc_pic_type, &i4_temporal_layer_id, &i4_is_bottom_field);

    {
        WORD32 is_scd_ref_frame = 0, i4_num_scd_in_lap_window = 0, num_frames_b4_scd = 0;

        /*treat even first frame as scd frame*/
        if(!ps_rc_ctxt->i4_is_first_frame_encoded)
        {
            is_scd_ref_frame = 1;
        }

        {
            /*Only I frames are considered as scd pic during pre-enc*/
            is_scd_ref_frame &= (rc_pic_type == I_PIC);
        }

        rc_set_num_scd_in_lap_window(
            ps_rc_ctxt->rc_hdl, i4_num_scd_in_lap_window, num_frames_b4_scd);

        /** Pre-enc thread as of now SCD handling is not present */
        //if(!(is_scd_ref_frame || ps_rc_ctxt->i4_is_pause_to_resume) || call_type == PRE_ENC_GET_QP)
        {
            WORD32 i4_is_first_frame_coded;
            /*Once first frame has been encoded use prev frame intra satd and cur frame satd to alter est intra sad for cur frame*/
            i4_is_first_frame_coded = is_first_frame_coded(ps_rc_ctxt->rc_hdl);
            {
                int i;
                WORD32 i4_curr_bits_estimated, i4_is_model_valid;
                /*initialise the coeffs to 1 and 0in case lap is not used */
                for(i = 0; i < MAX_PIC_TYPE; i++)
                {
                    ps_rc_ctxt->af_sum_weigh[0][i][0] = 1.0;
                    ps_rc_ctxt->af_sum_weigh[0][i][1] = 0.0;
                }

                i4_frame_qp_q6 = get_frame_level_qp(
                    ps_rc_ctxt->rc_hdl,
                    rc_pic_type,
                    i4_max_frame_bits,
                    &i4_cur_est_texture_bits,  //this value is returned by rc
                    ps_rc_ctxt->af_sum_weigh[0],
                    0,
                    8.0f,
                    NULL,
                    ps_rc_ctxt->i4_complexity_bin,
                    ps_rc_ctxt->i4_scene_num_latest, /*no pause resume concept*/
                    &i4_curr_bits_estimated,
                    &i4_is_model_valid,
                    NULL,
                    NULL,
                    NULL,
                    NULL,
                    NULL,
                    NULL);

                /** The usage of global table will truncate the input given as qp format and hence will not return very low qp values desirable at very
                low bitrate. Hence on the fly calculation is enabled*/
                i4_hevc_frame_qp =
                    ihevce_rc_get_scaled_hevce_qp_q6(i4_frame_qp_q6, ps_rc_ctxt->u1_bit_depth);

                if(rc_pic_type == I_PIC)
                {
                    /*scene cut handling during pre-enc stage*/
                    i4_frame_qp = rc_get_bpp_based_scene_cut_qp(
                        ps_rc_ctxt->rc_hdl,
                        rc_pic_type,
                        ((3 * ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 1),
                        ps_rc_ctxt->ai4_lap_f_sim[0],
                        ps_rc_ctxt->af_sum_weigh[0],
                        0);

                    *pi4_scd_qp = ihevce_rc_get_scaled_hevc_qp_from_qs_q3(
                        i4_frame_qp << QSCALE_Q_FAC_3, ps_rc_ctxt->ps_rc_quant_ctxt);
                    *pi4_scd_qp = *pi4_scd_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset;
                    if(*pi4_scd_qp > ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp)
                        *pi4_scd_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp;
                }
                else
                {
                    /*scene cut qp is only valid when queried for I_PIC*/
                    *pi4_scd_qp = i4_hevc_frame_qp;
                }
            }
        }

        ASSERT(i4_hevc_frame_qp >= (-ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset));

        /*constraint qp swing based on neighbour frames*/
        if(is_first_frame_coded(ps_rc_ctxt->rc_hdl))
        {
            if(ps_rc_ctxt->i4_field_pic == 0)
            {
                if((rc_pic_type != I_PIC && rc_pic_type != P_PIC) &&
                   i4_hevc_frame_qp >
                       ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                       [rc_pic_type - 1] +
                           3)
                {
                    /*allow max of +3 compared to previous frame*/
                    i4_hevc_frame_qp =
                        ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                        [rc_pic_type - 1] +
                        3;
                }
                if((rc_pic_type != I_PIC && rc_pic_type != P_PIC) &&
                   i4_hevc_frame_qp <
                       (ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                        [rc_pic_type - 1]))
                {
                    i4_hevc_frame_qp =
                        ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                        [rc_pic_type - 1];
                }

                /** Force non-ref B pic qp to be ref_B_PIC_qp - 1. This is not valid for when max teporla later is less than 2*/
                if(i4_temporal_layer_id == ps_rc_ctxt->i4_max_temporal_lyr &&
                   ps_rc_ctxt->i4_max_temporal_lyr > 1)
                {
                    i4_hevc_frame_qp =
                        ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                        [rc_pic_type - 1] +
                        1;
                }
            }
            else /*for field case*/
            {
                if(i4_temporal_layer_id >= 1)
                {
                    /*To make the comparison of qp with the top field's of previous layer tempor layer id matches with the pic type. */
                    if(i4_hevc_frame_qp >
                       ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                       [i4_temporal_layer_id] +
                           3)
                    {
                        /*allow max of +3 compared to previous frame*/
                        i4_hevc_frame_qp =
                            ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                            [i4_temporal_layer_id] +
                            3;
                    }
                    if(i4_hevc_frame_qp <
                       ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                       [i4_temporal_layer_id])
                    {
                        i4_hevc_frame_qp =
                            ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                            [i4_temporal_layer_id];
                    }
                    /** Force non-ref B pic qp to be ref_B_PIC_qp - 1. This is not valid for when max teporla later is less than 2*/
                    if(i4_temporal_layer_id == ps_rc_ctxt->i4_max_temporal_lyr &&
                       ps_rc_ctxt->i4_max_temporal_lyr > 1)
                    {
                        i4_hevc_frame_qp =
                            ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest]
                                                            [i4_temporal_layer_id] +
                            1;
                    }
                }
            }
        }

#if USE_USER_FIRST_FRAME_QP
        /*I_PIC check is necessary coz pre-enc can query for qp even before first frame update has happened*/
        if(!ps_rc_ctxt->i4_is_first_frame_encoded && rc_pic_type == I_PIC)
        {
            i4_hevc_frame_qp = ps_rc_ctxt->i4_init_frame_qp_user;
            DBG_PRINTF("FIXED START QP PATH *************************\n");
        }
#endif
        /**clip to min qp which is user configurable*/
        i4_hevc_frame_qp =
            ihevce_clip_min_max_qp(ps_rc_ctxt, i4_hevc_frame_qp, rc_pic_type, i4_temporal_layer_id);

        return i4_hevc_frame_qp;
    }
}
/**
******************************************************************************
*
*  @name  ihevce_rc_cal_pre_enc_qp
*
*  @par   Description - Called from enc_loop_init. updates frame qp which will
                        be used by next frame of same pic type in pre-enc stage
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*  @return      void
*
******************************************************************************
*/
void ihevce_rc_cal_pre_enc_qp(void *pv_rc_ctxt)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    WORD32 i, i4_frame_qp, i4_scd_qp;
    WORD32 i4_delay_l0_enc = 0;

    i4_delay_l0_enc = ps_rc_ctxt->i4_pre_enc_rc_delay;

    if(ps_rc_ctxt->e_rate_control_type != CONST_QP)
    {
        //DBG_PRINTF("\ncheck query read = %d write = %d",ps_rc_ctxt->i4_pre_enc_qp_read_index,ps_rc_ctxt->i4_pre_enc_qp_write_index);
#if DETERMINISTIC_RC
        ASSERT(
            ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index].i4_is_qp_valid ==
            -1);
#endif
        for(i = 0; i < ps_rc_ctxt->i4_num_active_pic_type; i++)
        {
            i4_frame_qp =
                ihevce_rc_get_pre_enc_pic_quant(ps_rc_ctxt, (picture_type_e)i, &i4_scd_qp);

            ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index].ai4_quant[i] =
                i4_frame_qp;
            /*returns valid scene cut qp only when queried as I_PIC*/
            if(i == 0)
            {
                ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index].i4_scd_qp =
                    i4_scd_qp;
            }

            if(ps_rc_ctxt->i4_field_pic && i > 0)
            {
                i4_frame_qp = ihevce_rc_get_pre_enc_pic_quant(
                    ps_rc_ctxt, (picture_type_e)(i + FIELD_OFFSET), &i4_scd_qp);

                ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index]
                    .ai4_quant[i + FIELD_OFFSET] = i4_frame_qp;
            }
        }
        /*mark index as populated*/
        ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index].i4_is_qp_valid = 1;

        ps_rc_ctxt->i4_pre_enc_qp_write_index =
            (ps_rc_ctxt->i4_pre_enc_qp_write_index + 1) % i4_delay_l0_enc;
    }
}
/**
******************************************************************************
*
*  @brief function to get updated qp after L1 analysis for L0. '
*           This uses estimated L0 satd based on L1 satd/act
*
*  @par   Description
*
*  @param[in]   pv_rc_ctxt
*               void pointer to rc ctxt
*  @param[in]   rc_lap_out_params_t *
pointer to lap out structure
*   @param[in]  i8_est_L0_satd_act
*               estimated L0 satd/act based on L1 satd/act
*  @return      void
*
******************************************************************************
*/
WORD32 ihevce_get_L0_est_satd_based_scd_qp(
    void *pv_rc_ctxt,
    rc_lap_out_params_t *ps_rc_lap_out,
    LWORD64 i8_est_L0_satd_act,
    float i_to_avg_rest_ratio)
{
    rc_context_t *ps_ctxt = (rc_context_t *)pv_rc_ctxt;
    WORD32 i4_frame_qs_q3, i4_hevc_qp, i4_est_header_bits, index, i, i4_total_bits;
    picture_type_e rc_pic_type;

    rc_pic_type = ihevce_rc_conv_pic_type(
        (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type,
        ps_ctxt->i4_field_pic,
        ps_rc_lap_out->i4_rc_temporal_lyr_id,
        ps_rc_lap_out->i4_is_bottom_field,
        ps_ctxt->i4_top_field_first);

    /*initialise the coeffs to 1 in case lap is not used */

    for(i = 0; i < MAX_PIC_TYPE; i++)
    {
        ps_ctxt->af_sum_weigh[0][i][0] = 1.0;
        ps_ctxt->af_sum_weigh[0][i][1] = 0.0;
    }

    /*get bits to find estimate of header bits*/
    i4_est_header_bits = rc_get_scene_change_est_header_bits(
        ps_ctxt->rc_hdl,
        ps_rc_lap_out->i4_num_pels_in_frame_considered,
        ps_ctxt->ai4_lap_f_sim[0],
        ps_ctxt->af_sum_weigh[0],
        i_to_avg_rest_ratio);

    index = ihevce_get_offline_index(ps_ctxt, ps_rc_lap_out->i4_num_pels_in_frame_considered);
    {
        WORD32 i4_true_scd = 0;
        WORD32 i4_curr_bits_estimated;

        i4_frame_qs_q3 = rc_get_qp_for_scd_frame(
            ps_ctxt->rc_hdl,
            I_PIC,
            i8_est_L0_satd_act,
            ps_rc_lap_out->i4_num_pels_in_frame_considered,
            i4_est_header_bits,
            ps_ctxt->ai4_lap_f_sim[0],
            (void *)&g_offline_i_model_coeff[index][0],
            i_to_avg_rest_ratio,
            i4_true_scd,
            ps_ctxt->af_sum_weigh[0],
            ps_rc_lap_out->ps_frame_info,
            ps_ctxt->i4_rc_pass,
            0,
            0,
            0,
            &i4_total_bits,
            &i4_curr_bits_estimated,
            ps_rc_lap_out->i4_use_offline_model_2pass,
            0,
            0,
            -1,
            NULL);
    }
    i4_hevc_qp = ihevce_rc_get_scaled_hevc_qp_from_qs_q3(i4_frame_qs_q3, ps_ctxt->ps_rc_quant_ctxt);
    i4_hevc_qp = i4_hevc_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset;

    if(i4_hevc_qp > ps_ctxt->ps_rc_quant_ctxt->i2_max_qp)
        i4_hevc_qp = ps_ctxt->ps_rc_quant_ctxt->i2_max_qp;

    if(i4_hevc_qp < (SCD_MIN_HEVC_QP -
                     ps_ctxt->ps_rc_quant_ctxt
                         ->i1_qp_offset))  // since outside RC the QP range is -12 to 51 for 10 bit
    {
        i4_hevc_qp = (SCD_MIN_HEVC_QP - ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset);
    }
    else if(i4_hevc_qp > SCD_MAX_HEVC_QP)
    {
        i4_hevc_qp = SCD_MAX_HEVC_QP;
    }
    /*this is done outside loop*/

    return i4_hevc_qp;
}
/**
******************************************************************************
*
*  @name  ihevce_rc_pre_enc_qp_query
*
*  @par   Description - Called from pre enc thrd for getting the qp of non scd
                        frames. updates frame qp from reverse queue from enc loop
                        when its available
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*  @param[in]   i4_update_delay : The Delay in the update. This can happen for dist. case!
*               All decision should consider this delay for updation!
*
*  @return      void
*
******************************************************************************
*/

WORD32 ihevce_rc_pre_enc_qp_query(
    void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out, WORD32 i4_update_delay)
{
    WORD32 scene_type, i4_is_scd = 0, i4_frame_qp, slice_type;
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    rc_type_e e_rc_type = ps_rc_ctxt->e_rate_control_type;
    IV_PICTURE_CODING_TYPE_T pic_type = (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type;
    picture_type_e rc_pic_type = ihevce_rc_conv_pic_type(
        (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_rc_lap_out->i4_rc_temporal_lyr_id,
        ps_rc_lap_out->i4_is_bottom_field,
        ps_rc_ctxt->i4_top_field_first);
    WORD32 i4_use_offset_flag = 0, k = 0;
    WORD32 i4_inter_frame_interval = rc_get_inter_frame_interval(ps_rc_ctxt->rc_hdl);
    WORD32 ai4_offsets[5] = { 0, 1, 2, 3, 4 };
    rc_lap_out_params_t *ps_rc_lap_out_temp = ps_rc_lap_out;

    /* The window for which your update is guaranteed */
    WORD32 updated_window = ps_rc_ctxt->i4_num_frame_in_lap_window - i4_update_delay;

    k = 0;
    if((updated_window >= i4_inter_frame_interval) && (ps_rc_ctxt->i4_rc_pass != 2) &&
       ((rc_pic_type == I_PIC) || (rc_pic_type == P_PIC)))
    {
        WORD32 i4_count = 0;

        for(i4_count = 0; i4_count < updated_window; i4_count++)
        {
            picture_type_e rc_pic_type_temp = ihevce_rc_conv_pic_type(
                (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out_temp->i4_rc_pic_type,
                ps_rc_ctxt->i4_field_pic,
                ps_rc_lap_out_temp->i4_rc_temporal_lyr_id,
                ps_rc_lap_out_temp->i4_is_bottom_field,
                ps_rc_ctxt->i4_top_field_first);

            if((rc_pic_type_temp == I_PIC) || (rc_pic_type_temp == P_PIC))
                ihevce_compute_temporal_complexity_reset_Kp_Kb(ps_rc_lap_out_temp, pv_rc_ctxt, 0);

            ps_rc_lap_out_temp =
                (rc_lap_out_params_t *)ps_rc_lap_out_temp->ps_rc_lap_out_next_encode;

            if(ps_rc_lap_out_temp == NULL)
                break;
        }
    }

    if(updated_window >= i4_inter_frame_interval)
    {
        i4_use_offset_flag = 1;
        memmove(ai4_offsets, ps_rc_lap_out->ai4_offsets, sizeof(WORD32) * 5);
    }

    if(CONST_QP == e_rc_type)
    {
        switch(pic_type)
        {
        case IV_I_FRAME:
        case IV_IDR_FRAME:
        {
            slice_type = ISLICE;
            break;
        }
        case IV_P_FRAME:
        {
            slice_type = PSLICE;
            break;
        }
        case IV_B_FRAME:
        {
            slice_type = BSLICE;
            break;
        }
        }

        i4_frame_qp = ihevce_get_cur_frame_qp(
            ps_rc_ctxt->i4_init_frame_qp_user,
            slice_type,
            ps_rc_lap_out->i4_rc_temporal_lyr_id,
            ps_rc_ctxt->i4_min_frame_qp,
            ps_rc_ctxt->i4_max_frame_qp,
            ps_rc_ctxt->ps_rc_quant_ctxt);

        return i4_frame_qp;
    }
    else
    {
        /*check scene type*/
        scene_type = ihevce_rc_lap_get_scene_type(ps_rc_lap_out);

        if(scene_type == SCENE_TYPE_SCENE_CUT)
        {
            i4_is_scd = 1;
            ps_rc_ctxt->i4_num_frms_from_reset = 0;
#if USE_QP_OFFSET_POST_SCD
            ps_rc_ctxt->i4_use_qp_offset_pre_enc = 1;
#else
            ps_rc_ctxt->i4_use_qp_offset_pre_enc = 0;
#endif
        }
        ASSERT(
            ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index].i4_is_qp_valid ==
                1 ||
            ps_rc_lap_out->i4_rc_poc < 20);

        if(ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index].i4_is_qp_valid ==
           1)
        {
            if(i4_is_scd || ps_rc_ctxt->i4_use_qp_offset_pre_enc)
            {
#if 1  //The qp will be populated assuming the frame is I_PIC. Adjust according to current pic type
                i4_frame_qp =
                    ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index].i4_scd_qp;
                if(rc_pic_type == P_PIC)
                    i4_frame_qp++;
                else
                    i4_frame_qp = i4_frame_qp + ps_rc_lap_out->i4_rc_temporal_lyr_id;
#endif
                if(i4_use_offset_flag)
                {
                    if(rc_pic_type > B2_PIC)
                        i4_frame_qp = ps_rc_ctxt->i4_L0_frame_qp + ai4_offsets[rc_pic_type - 4];
                    else
                        i4_frame_qp = ps_rc_ctxt->i4_L0_frame_qp + ai4_offsets[rc_pic_type];
                }
            }
            else
            {
#if DETERMINISTIC_RC
                i4_frame_qp = ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index]
                                  .ai4_quant[rc_pic_type];
#else
                /*read the latest qp updated by enc*/
                i4_frame_qp =
                    ps_rc_ctxt
                        ->as_pre_enc_qp_queue
                            [(ps_rc_ctxt->i4_pre_enc_qp_write_index + MAX_PRE_ENC_RC_DELAY - 1) %
                             MAX_PRE_ENC_RC_DELAY]
                        .ai4_quant[rc_pic_type];
#endif
            }

            ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index].i4_is_qp_valid =
                -1;
            /*once encoder starts reading from qp queue it should always read from qp queue*/
            //ps_rc_ctxt->i4_use_init_qp_for_pre_enc = 0;
        }
        else
        {
            i4_frame_qp = ps_rc_ctxt->ai4_init_pre_enc_qp[rc_pic_type];
        }
        {
            WORD32 i4_delay_l0_enc = ps_rc_ctxt->i4_pre_enc_rc_delay;
            ps_rc_ctxt->i4_pre_enc_qp_read_index =
                (ps_rc_ctxt->i4_pre_enc_qp_read_index + 1) % i4_delay_l0_enc;

            if(ps_rc_ctxt->i4_num_frms_from_reset < i4_delay_l0_enc)
            {
                ps_rc_ctxt->i4_num_frms_from_reset++;
                if(ps_rc_ctxt->i4_num_frms_from_reset >= i4_delay_l0_enc)
                    ps_rc_ctxt->i4_use_qp_offset_pre_enc = 0;
            }
        }

        i4_frame_qp = CLIP3(i4_frame_qp, ps_rc_ctxt->i4_min_frame_qp, ps_rc_ctxt->i4_max_frame_qp);
        return i4_frame_qp;
    }
}
/**
******************************************************************************
*
*  @brief function to estimate L0 satd based on L1 satd. '
*
*
*  @par   Description
*
*  @param[in]   pv_rc_ctxt
*               void pointer to rc ctxt
*  @param[in]   rc_lap_out_params_t *
pointer to lap out structure
*   @param[in]  i8_est_L0_satd_act
*               estimated L0 satd/act based on L1 satd/act
*  @return      void
*
******************************************************************************
*/
LWORD64 ihevce_get_L0_satd_based_on_L1(
    LWORD64 i8_satd_by_act_L1, WORD32 i4_num_pixel, WORD32 i4_cur_q_scale)
{
    LWORD64 est_L0_satd_by_act;
    float m, c;
    /** choose coeff based on resolution*/
    if(i4_num_pixel > 5184000)
    {
        m = (float)2.3911;
        c = (float)86329;
    }
    else if(i4_num_pixel > 1497600)
    {
        m = (float)2.7311;
        c = (float)-1218.9;
    }
    else if(i4_num_pixel > 633600)
    {
        m = (float)3.1454;
        c = (float)-5836.1;
    }
    else
    {
        m = (float)3.5311;
        c = (float)-2377.2;
    }
    /*due to qp difference between I and  P, For P pic for same */
    est_L0_satd_by_act = (LWORD64)(i8_satd_by_act_L1 / i4_cur_q_scale * m + c) * i4_cur_q_scale;

    {
        if(est_L0_satd_by_act < (i4_num_pixel >> 3))
            est_L0_satd_by_act = (i4_num_pixel >> 3);
    }
    return est_L0_satd_by_act;
}
/**
******************************************************************************
*
*  @name  ihevce_rc_register_L1_analysis_data
*
*  @par   Description
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*               ps_rc_lap_out
*               i8_est_L0_satd_by_act
*               i8_pre_intra_sad
*               i8_l1_hme_sad
*  @return      void
*
******************************************************************************
*/
void ihevce_rc_register_L1_analysis_data(
    void *pv_rc_ctxt,
    rc_lap_out_params_t *ps_rc_lap_out,
    LWORD64 i8_est_L0_satd_by_act,
    LWORD64 i8_pre_intra_sad,
    LWORD64 i8_l1_hme_sad)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    WORD32 i, data_available = 1;
    picture_type_e rc_pic_type = ihevce_rc_conv_pic_type(
        (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_rc_lap_out->i4_rc_temporal_lyr_id,
        ps_rc_lap_out->i4_is_bottom_field,
        ps_rc_ctxt->i4_top_field_first);

    //if( ps_rc_ctxt->u4_rc_scene_num_est_L0_intra_sad_available == ps_rc_lap_out->u4_rc_scene_num)
    {
        /*update current frame's data*/
        ps_rc_ctxt->ai8_prev_frame_est_L0_satd[rc_pic_type] = i8_est_L0_satd_by_act;
        ps_rc_ctxt->ai8_prev_frame_hme_sad[rc_pic_type] = i8_l1_hme_sad;
        ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[rc_pic_type] = i8_pre_intra_sad;
    }
    /*check if data is available for all picture type*/
    if(!ps_rc_ctxt->i4_is_est_L0_intra_sad_available)
    {
        for(i = 0; i < ps_rc_ctxt->i4_num_active_pic_type; i++)
        {
            data_available &= (ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i] >= 0);
            if(ps_rc_ctxt->i4_field_pic == 1 && i != 0)
                data_available &= (ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i + FIELD_OFFSET] >= 0);
        }
        ps_rc_ctxt->i4_is_est_L0_intra_sad_available = data_available;
    }
}

/*#######################################################*/
/******* END OF PRE-ENC QP QUERY FUNCTIONS **************/
/*#####################################################*/

/*##########################################################*/
/******* START OF ENC THRD QP QUERY FUNCTIONS **************/
/*########################################################*/

/**
******************************************************************************
*
*  @brief function to get ihevce_rc_get_pic_quant
*
*  @par   Description
*  @param[in]   i4_update_delay : The Delay in the update. This can happen for dist. case!
*               All decision should consider this delay for updation!
******************************************************************************
*/

WORD32 ihevce_rc_get_pic_quant(
    void *pv_ctxt,
    rc_lap_out_params_t *ps_rc_lap_out,
    IHEVCE_RC_CALL_TYPE call_type,
    WORD32 i4_enc_frm_id,
    WORD32 i4_update_delay,
    WORD32 *pi4_tot_bits_estimated)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    WORD32 i4_frame_qp, i4_frame_qp_q6, i4_hevc_frame_qp = -1, i4_deltaQP = 0;
    WORD32 i4_max_frame_bits = (1 << 30);
    rc_type_e e_rc_type = ps_rc_ctxt->e_rate_control_type;
    WORD32 slice_type, index, i4_num_frames_in_cur_gop, i4_cur_est_texture_bits;
    WORD32 temporal_layer_id = ps_rc_lap_out->i4_rc_temporal_lyr_id;
    IV_PICTURE_CODING_TYPE_T pic_type = (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type;
    picture_type_e rc_pic_type = ihevce_rc_conv_pic_type(
        pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_rc_lap_out->i4_rc_temporal_lyr_id,
        ps_rc_lap_out->i4_is_bottom_field,
        ps_rc_ctxt->i4_top_field_first);
    float i_to_avg_bit_ratio;
    frame_info_t s_frame_info_temp;
    WORD32 i4_scene_num = ps_rc_lap_out->u4_rc_scene_num % MAX_SCENE_NUM;
    WORD32 i4_vbv_buf_max_bits;
    WORD32 i4_est_tex_bits;
    WORD32 i4_cur_est_header_bits, i4_fade_scene;
    WORD32 i4_model_available, i4_is_no_model_scd;
    WORD32 i4_estimate_to_calc_frm_error;

    /* The window for which your update is guaranteed */
    WORD32 updated_window = ps_rc_ctxt->i4_num_frame_in_lap_window - i4_update_delay;

    ps_rc_ctxt->i4_scene_num_latest = i4_scene_num;

    ps_rc_ctxt->s_rc_high_lvl_stat.i4_modelQP = INVALID_QP;
    ps_rc_ctxt->s_rc_high_lvl_stat.i4_finalQP = INVALID_QP;
    ps_rc_ctxt->s_rc_high_lvl_stat.i4_maxEbfQP = INVALID_QP;

    ps_rc_ctxt->i4_quality_preset = ps_rc_lap_out->i4_rc_quality_preset;
    ps_rc_ctxt->s_rc_high_lvl_stat.i4_finalQP = INVALID_QP;

    if(1 == ps_rc_ctxt->i4_bitrate_changed)
    {
        ps_rc_ctxt->i4_bitrate_changed = 0;
    }
    if(CONST_QP == e_rc_type)
    {
        switch(pic_type)
        {
        case IV_I_FRAME:
        case IV_IDR_FRAME:
        {
            slice_type = ISLICE;
            break;
        }
        case IV_P_FRAME:
        {
            slice_type = PSLICE;
            break;
        }
        case IV_B_FRAME:
        {
            slice_type = BSLICE;
            break;
        }
        }

        i4_frame_qp = ihevce_get_cur_frame_qp(
            ps_rc_ctxt->i4_init_frame_qp_user,
            slice_type,
            temporal_layer_id,
            ps_rc_ctxt->i4_min_frame_qp,
            ps_rc_ctxt->i4_max_frame_qp,
            ps_rc_ctxt->ps_rc_quant_ctxt);
        return i4_frame_qp;
    }
    else
    {
        WORD32 is_scd_ref_frame = 0, i4_num_scd_in_lap_window = 0, num_frames_b4_scd = 0,
               scene_type = 0, i;
        //ihevce_lap_output_params_t *ps_cur_rc_lap_out;

        if(ps_rc_ctxt->ai4_scene_num_last_pic[rc_pic_type] !=
           (WORD32)ps_rc_lap_out->u4_rc_scene_num)
        {
            rc_reset_pic_model(ps_rc_ctxt->rc_hdl, rc_pic_type);
            rc_reset_first_frame_coded_flag(ps_rc_ctxt->rc_hdl, rc_pic_type);
        }
        ps_rc_ctxt->ai4_scene_num_last_pic[rc_pic_type] = ps_rc_lap_out->u4_rc_scene_num;

        if(call_type == ENC_GET_QP)
        {
            i4_model_available = model_availability(ps_rc_ctxt->rc_hdl, rc_pic_type);

            ps_rc_lap_out->i8_est_text_bits = -1;
        }

        if((rc_pic_type == I_PIC) || (rc_pic_type == P_PIC) || (rc_pic_type == P1_PIC))
        {
            ps_rc_ctxt->i4_cur_scene_num = ps_rc_lap_out->u4_rc_scene_num;
        }

        {
            if(!(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME))
            {
                ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i4_enc_frm_id] =
                    ps_rc_lap_out->i8_frame_acc_coarse_me_cost;
            }
            /*check if frame is scene cut*/
            /* If scd do not query the model. obtain qp from offline data model*/
            scene_type = ihevce_rc_lap_get_scene_type(ps_rc_lap_out);

            if(ps_rc_ctxt->ai4_scene_numbers[ps_rc_lap_out->u4_rc_scene_num] == 0 &&
               (scene_type != SCENE_TYPE_SCENE_CUT))
            {
                scene_type = SCENE_TYPE_SCENE_CUT;
            }

            if(ps_rc_ctxt->ai4_scene_numbers[ps_rc_lap_out->u4_rc_scene_num] > 0 &&
               (scene_type == SCENE_TYPE_SCENE_CUT))
            {
                scene_type = SCENE_TYPE_NORMAL;
            }
            if(scene_type == SCENE_TYPE_SCENE_CUT)
            {
                if((ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6) &&
                   (rc_pic_type > P_PIC))
                {
                    is_scd_ref_frame = 0;
                }
                else
                {
                    is_scd_ref_frame = 1;
                }
            }
            else if(scene_type == SCENE_TYPE_PAUSE_TO_RESUME)
            {
                /*pause to resume flag will only be set in layer 0 frames( I and P pic)*/
                /*I PIC can handle this by detecting I_only SCD which is based on open loop SATD hence explicit handling for pause to resume is required only for P_PIC*/

                if(ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6)
                {
                    if(call_type == ENC_GET_QP && rc_pic_type == P_PIC)
                    {
                        ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] = 1;
                    }
                }
                else
                {
                    if(call_type == ENC_GET_QP && rc_pic_type != I_PIC)
                    {
                        ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] = 1;
                    }
                }
            }

            ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id] =
                ps_rc_lap_out->i4_is_cmplx_change_reset_model;
            ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i4_enc_frm_id] =
                ps_rc_lap_out->i4_is_cmplx_change_reset_bits;

            /*initialise the coeffs to 1 in case lap is not used */
            for(i = 0; i < MAX_PIC_TYPE; i++)
            {
                ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][0] = 1.0;
                ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][1] = 0.0;
                ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][2] = 0.0;
            }

            /*treat even first frame as scd frame*/
            if(!ps_rc_ctxt->i4_is_first_frame_encoded)
            {
                is_scd_ref_frame = 1;
            }

            /*special case SCD handling for Non-I pic*/
            if(!(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) && call_type == ENC_GET_QP)
            {
                if(is_scd_ref_frame)
                {
                    /*A non-I pic will only be marked as scene cut only if there is another SCD follows within another subgop*/
                    ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] = 1;
                }
                /*check if current sad is very different from previous SAD and */
                else if(
                    !ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] &&
                    ps_rc_lap_out->i4_is_non_I_scd)
                {
                    ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] = 1;
                    is_scd_ref_frame = 1;
                }
            }

            if(call_type == PRE_ENC_GET_QP)
            {
                /*Only I frames are considered as scd pic during pre-enc*/
                is_scd_ref_frame &= (pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME);
            }

            /*special case SCD handling for I pic*/
            if((pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) && !is_scd_ref_frame)
            {
                /*If open loop SATD's of two I picture are very different then treat the I pic as SCD and reset only model as this can
                happen during fade-in and fade-out where other picture types would have learnt. Reset is required only for I.*/

                if(ps_rc_lap_out->i4_is_I_only_scd)
                {
                    is_scd_ref_frame = 1;
                    ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id] = 1;
                }
            }
            /*should be recalculated for every picture*/
            if((updated_window) > 0 && (call_type == ENC_GET_QP) && (ps_rc_ctxt->i4_rc_pass != 2))
            {
                rc_lap_out_params_t *ps_cur_rc_lap_out;

                UWORD32 u4_L1_based_lap_complexity_q7;
                WORD32 i = 0, k = 0, i4_f_sim = 0, i4_h_sim = 0, i4_var_sum = 0,
                       i4_num_pic_metric_count = 0, i4_is_first_frm = 1,
                       i4_intra_frame_interval = 0;
                LWORD64 i8_l1_analysis_lap_comp = 0;
                LWORD64 nor_frm_hme_sad_q10;
                picture_type_e curr_rc_pic_type;
                WORD32 ai4_pic_dist[MAX_PIC_TYPE] = { 0 };
                LWORD64 i8_sad_first_frame_pic_type[MAX_PIC_TYPE] = { 0 },
                        i8_total_sad_pic_type[MAX_PIC_TYPE] = { 0 };
                LWORD64 i8_last_frame_pic_type[MAX_PIC_TYPE] = { 0 }, i8_esti_consum_bits = 0;
                WORD32 i4_num_pic_type[MAX_PIC_TYPE] = { 0 }, i4_frames_in_lap_end = 0,
                       i4_first_frame_coded_flag, i4_gop_end_flag = 1, i4_num_frame_for_ebf = 0;
                i4_first_frame_coded_flag = is_first_frame_coded(ps_rc_ctxt->rc_hdl);

                /*Setting the next scene cut as well as pic distribution for the gop*/

                ps_cur_rc_lap_out = (rc_lap_out_params_t *)ps_rc_lap_out;
                i4_intra_frame_interval = rc_get_intra_frame_interval(ps_rc_ctxt->rc_hdl);

                /*Set the rc sc i next*/
                if(ps_cur_rc_lap_out != NULL)
                {
                    WORD32 i4_count = 0;
                    do
                    {
                        if(((rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode ==
                            NULL))  //||((( (ihevce_lap_output_params_t*)ps_cur_rc_lap_out->ps_lap_out_next)->i8_pre_intra_sad == -1) || (  ((ihevce_lap_output_params_t*)ps_cur_rc_lap_out->ps_lap_out_next)->i8_raw_pre_intra_sad == -1) ||(  ((ihevce_lap_output_params_t*)ps_cur_rc_lap_out->ps_lap_out_next)->i8_raw_l1_coarse_me_sad == -1) ||(((ihevce_lap_output_params_t*)ps_cur_rc_lap_out->ps_lap_out_next)->i8_frame_acc_coarse_me_sad == -1)))
                            break;

                        ps_cur_rc_lap_out =
                            (rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode;
                        i4_count++;

                    } while((i4_count + 1) < updated_window);

                    rc_set_next_sc_i_in_rc_look_ahead(
                        ps_rc_ctxt->rc_hdl, ps_cur_rc_lap_out->i4_next_sc_i_in_rc_look_ahead);
                    rc_update_pic_distn_lap_to_rc(
                        ps_rc_ctxt->rc_hdl, ps_cur_rc_lap_out->ai4_num_pic_type);

                    ps_rc_ctxt->i4_next_sc_i_in_rc_look_ahead =
                        ps_cur_rc_lap_out->i4_next_sc_i_in_rc_look_ahead;
                }

                ps_cur_rc_lap_out = (rc_lap_out_params_t *)ps_rc_lap_out;
                if(ps_cur_rc_lap_out != NULL)
                {
                    /*initialise the coeffs to 1 in case lap is not used */
                    for(i = 0; i < MAX_PIC_TYPE; i++)
                    {
                        ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][0] = 0.0;
                        ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][1] = 0.0;
                        ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][2] = 0.0;
                    }
                    i = 0;
                    k = 0;

                    //ASSERT(ps_cur_rc_lap_out != NULL);
                    do
                    {
                        curr_rc_pic_type = ihevce_rc_conv_pic_type(
                            (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out->i4_rc_pic_type,
                            ps_rc_ctxt->i4_field_pic,
                            ps_cur_rc_lap_out->i4_rc_temporal_lyr_id,
                            ps_cur_rc_lap_out->i4_is_bottom_field,
                            ps_rc_ctxt->i4_top_field_first);
                        if(ps_rc_ctxt->i4_is_first_frame_encoded || !i4_is_first_frm)
                        {
                            /*Ignore first frame Fsim as it is not valid for first frame*/
                            i4_f_sim += ps_cur_rc_lap_out->s_pic_metrics.i4_fsim;
                            i4_h_sim += ps_cur_rc_lap_out->s_pic_metrics.ai4_hsim[0];
                            i4_var_sum += (WORD32)ps_cur_rc_lap_out->s_pic_metrics.i8_8x8_var_lum;
                            i4_num_pic_metric_count++;
                            //DBG_PRINTF("\n fsim = %d i = %d",ps_cur_rc_lap_out->s_pic_metrics.i4_fsim,i);
                            //ASSERT(ps_cur_rc_lap_out->s_pic_metrics.i4_fsim <= 128);
                        }

                        /*accumulate complexity from LAP2*/
                        if(curr_rc_pic_type == I_PIC)
                        {
                            i8_l1_analysis_lap_comp +=
                                (LWORD64)(1.17 * ps_cur_rc_lap_out->i8_raw_pre_intra_sad);
                        }
                        else
                        {
                            if(curr_rc_pic_type <= B2_PIC)
                                i8_l1_analysis_lap_comp += (LWORD64)(
                                    (float)ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad /
                                    pow(1.125f, curr_rc_pic_type));
                            else
                                i8_l1_analysis_lap_comp += (LWORD64)(
                                    (float)ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad /
                                    pow(1.125f, curr_rc_pic_type - B2_PIC));
                        }
                        i++;
                        i4_is_first_frm = 0;

                        /*CAll the function for predictting the ebf and stuffing condition check*/
                        /*rd model pass lapout l1 pass ebf return estimated ebf and signal*/

                        {
                            if(i4_first_frame_coded_flag && (i4_gop_end_flag != 0))
                            {
                                if(curr_rc_pic_type == 0)
                                    i4_gop_end_flag = 0;

                                if(i4_gop_end_flag)
                                {
                                    WORD32 prev_frm_cl_sad =
                                        rc_get_prev_frame_sad(ps_rc_ctxt->rc_hdl, curr_rc_pic_type);
                                    WORD32 cur_frm_est_cl_sad = (WORD32)(
                                        (ps_cur_rc_lap_out->i8_frame_acc_coarse_me_cost *
                                         prev_frm_cl_sad) /
                                        ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[curr_rc_pic_type]);
                                    i8_esti_consum_bits += bit_alloc_get_estimated_bits_for_pic(
                                        ps_rc_ctxt->rc_hdl,
                                        cur_frm_est_cl_sad,
                                        prev_frm_cl_sad,
                                        curr_rc_pic_type);
                                    i4_num_frame_for_ebf++;
                                }
                            }
                        }
                        ps_cur_rc_lap_out =
                            (rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode;
                        /*The scene cut is lap window other than current frame is used to reduce bit alloc window for I pic*/
                        if(ps_cur_rc_lap_out != NULL &&
                           ps_cur_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_SCENE_CUT)
                        {
                            i4_num_scd_in_lap_window++;
                            if(i4_num_scd_in_lap_window == 1)
                            {
                                /*Note how many frames are parsed before first scd is hit*/
                                num_frames_b4_scd = i + 1;
                            }
                        }

                        if((ps_cur_rc_lap_out == NULL ||
                            (i >=
                             (updated_window -
                              k))))  //||((( -1 == ps_cur_rc_lap_out->i8_pre_intra_sad ) || ( -1 == ps_cur_rc_lap_out->i8_raw_pre_intra_sad ) ||( -1 == ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad) ||(-1 == ps_cur_rc_lap_out->i8_frame_acc_coarse_me_sad))))
                            break;
                        if(0)  //(( -1 == ps_cur_rc_lap_out->i8_pre_intra_sad ) || ( -1 == ps_cur_rc_lap_out->i8_raw_pre_intra_sad ) ||( -1 == ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad) ||(-1 == ps_cur_rc_lap_out->i8_frame_acc_coarse_me_sad)))
                        {
                            k++;
                            ps_cur_rc_lap_out =
                                (rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode;
                            if(ps_cur_rc_lap_out == NULL)
                                break;
                            continue;
                        }

                    } while(1);
                    ;
                }
                /*For the first subgop we cant have underflow prevention logic
                since once picture of each type is not encoded also happens for static contents thants high i_to avg_ratio */
                if(i4_first_frame_coded_flag &&
                   (ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id] > I_TO_REST_SLOW))
                {
                    if(!(i4_num_frame_for_ebf < ps_rc_ctxt->i4_max_inter_frm_int))
                        rc_bit_alloc_detect_ebf_stuff_scenario(
                            ps_rc_ctxt->rc_hdl,
                            i4_num_frame_for_ebf,
                            i8_esti_consum_bits,
                            ps_rc_ctxt->i4_max_inter_frm_int);
                }

                k = 0;

                i4_frames_in_lap_end = 0;
                {
                    rc_lap_out_params_t *ps_cur_rc_lap_out1;

                    ps_cur_rc_lap_out1 = (rc_lap_out_params_t *)ps_rc_lap_out;
                    do
                    {
                        curr_rc_pic_type = ihevce_rc_conv_pic_type(
                            (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out1->i4_rc_pic_type,
                            ps_rc_ctxt->i4_field_pic,
                            ps_cur_rc_lap_out1->i4_rc_temporal_lyr_id,
                            ps_cur_rc_lap_out1->i4_is_bottom_field,
                            ps_rc_ctxt->i4_top_field_first);
                        /*accumulate complexity from LAP2*/

                        if(curr_rc_pic_type == I_PIC)
                        {
                            i8_total_sad_pic_type[I_PIC] +=
                                ps_cur_rc_lap_out1->i8_raw_pre_intra_sad;
                            i8_last_frame_pic_type[I_PIC] =
                                ps_cur_rc_lap_out1->i8_raw_pre_intra_sad;
                        }
                        else
                        {
                            i8_total_sad_pic_type[curr_rc_pic_type] +=
                                ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad;
                            i8_last_frame_pic_type[curr_rc_pic_type] =
                                ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad;
                        }
                        if(i4_num_pic_type[curr_rc_pic_type] == 0)
                        {
                            if(curr_rc_pic_type == I_PIC)
                            {
                                i8_sad_first_frame_pic_type[I_PIC] =
                                    ps_cur_rc_lap_out1->i8_raw_pre_intra_sad;
                            }
                            else
                            {
                                i8_sad_first_frame_pic_type[curr_rc_pic_type] =
                                    ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad;
                            }
                        }
                        i4_num_pic_type[curr_rc_pic_type]++;

                        i4_frames_in_lap_end++;

                        ps_cur_rc_lap_out1 =
                            (rc_lap_out_params_t *)ps_cur_rc_lap_out1->ps_rc_lap_out_next_encode;
                        if((ps_cur_rc_lap_out1 == NULL ||
                            (i4_frames_in_lap_end >=
                             (updated_window -
                              k))))  //||((( -1 == ps_cur_rc_lap_out1->i8_pre_intra_sad ) || ( -1 == ps_cur_rc_lap_out1->i8_raw_pre_intra_sad ) ||( -1 == ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad) ||(-1 == ps_cur_rc_lap_out1->i8_frame_acc_coarse_me_sad))))
                        {
                            break;
                        }
                        if(0)  //((( -1 == ps_cur_rc_lap_out1->i8_pre_intra_sad ) || ( -1 == ps_cur_rc_lap_out1->i8_raw_pre_intra_sad ) ||( -1 == ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad) ||(-1 == ps_cur_rc_lap_out1->i8_frame_acc_coarse_me_sad))))
                        {
                            k++;
                            ps_cur_rc_lap_out1 = (rc_lap_out_params_t *)
                                                     ps_cur_rc_lap_out1->ps_rc_lap_out_next_encode;
                            if(ps_cur_rc_lap_out1 == NULL)
                                break;
                            continue;
                        }

                    } while(i4_frames_in_lap_end < (ps_rc_ctxt->i4_next_sc_i_in_rc_look_ahead - k));
                }

                /*get picture type distribution in LAP*/
                rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]);

                {
                    float f_prev_comp;
                    WORD32 j;
                    float af_sum_weigh[MAX_PIC_TYPE], af_nume_weight[MAX_PIC_TYPE];
                    float af_average_sad_pic_type[MAX_PIC_TYPE] = { 0 };
                    for(j = 0; j < MAX_PIC_TYPE; j++)
                    {
                        if(i4_num_pic_type[j] > 0)
                        {
                            af_average_sad_pic_type[j] =
                                (float)i8_total_sad_pic_type[j] / i4_num_pic_type[j];
                        }

                        f_prev_comp = 1.;

                        i4_num_pic_type[j] = (i4_num_pic_type[j] > ai4_pic_dist[j])
                                                 ? ai4_pic_dist[j]
                                                 : i4_num_pic_type[j];

                        af_sum_weigh[j] = (float)i4_num_pic_type[j];
                        af_nume_weight[j] = 1.0;

                        if(i4_num_pic_type[j] > 1 && (af_average_sad_pic_type[j] > 0))
                        {
                            af_nume_weight[j] =
                                (float)i8_sad_first_frame_pic_type[j] / af_average_sad_pic_type[j];

                            f_prev_comp =
                                (float)i8_last_frame_pic_type[j] / af_average_sad_pic_type[j];
                        }
                        //if(rc_pic_type != I_PIC)
                        {
                            af_sum_weigh[j] += f_prev_comp * (ai4_pic_dist[j] - i4_num_pic_type[j]);
                        }
                        ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][0] = af_nume_weight[j];
                        ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][1] = af_sum_weigh[j];
                        ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][2] = af_average_sad_pic_type[j];

                        /*Disabling steady state complexity based bit movement*/
                        /*Enable it in CBR and not in VBR since VBR already has complexity based bit movement*/

                        if(0) /*i4_frames_in_lap_end < (updated_window) || ps_rc_ctxt->e_rate_control_type == VBR_STREAMING)*/
                        {
                            ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][0] = 1.0;
                            ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][1] =
                                0;  //(float)ai4_pic_dist[j];
                        }
                    }
                    memmove(
                        ps_rc_lap_out->ps_frame_info->af_sum_weigh,
                        ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id],
                        sizeof(float) * MAX_PIC_TYPE * 3);
                }

                if(i4_num_pic_metric_count > 0)
                {
                    i4_f_sim = i4_f_sim / i4_num_pic_metric_count;
                    i4_h_sim = i4_h_sim / i4_num_pic_metric_count;
                    i4_var_sum = i4_var_sum / i4_num_pic_metric_count;
                }
                else
                {
                    i4_f_sim = MODERATE_FSIM_VALUE;
                    i4_h_sim = MODERATE_FSIM_VALUE;
                }

                if(i > 0)
                {
                    float lap_L1_comp =
                        (float)i8_l1_analysis_lap_comp /
                        (i * ps_rc_ctxt->i4_frame_height *
                         ps_rc_ctxt->i4_frame_width);  //per frame per pixel complexity

                    lap_L1_comp = rc_get_offline_normalized_complexity(
                        ps_rc_ctxt->u4_intra_frame_interval,
                        ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width,
                        lap_L1_comp,
                        ps_rc_ctxt->i4_rc_pass);

                    u4_L1_based_lap_complexity_q7 = (WORD32)((lap_L1_comp * (1 << 7)) + .05f);
                }
                else
                {
                    u4_L1_based_lap_complexity_q7 = 25;
                }
                ps_rc_ctxt->ai4_lap_complexity_q7[i4_enc_frm_id] =
                    (WORD32)u4_L1_based_lap_complexity_q7;
                /*clip f_sim to 0.3 for better stability*/
                if(i4_f_sim < 38)
                    i4_f_sim = 128 - MAX_LAP_COMPLEXITY_Q7;
                ps_rc_ctxt->ai4_lap_f_sim[i4_enc_frm_id] = i4_f_sim;

                /*calculate normalized per pixel sad*/
                nor_frm_hme_sad_q10 = (ps_rc_lap_out->i8_frame_acc_coarse_me_cost << 10) /
                                      (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width);
                /*if(rc_pic_type == P_PIC)
                DBG_PRINTF("\n P frm hme sad = %f  ",((float)nor_frm_hme_sad_q10/ (1 << 10)));  */
                rc_put_temp_comp_lap(
                    ps_rc_ctxt->rc_hdl, i4_f_sim, nor_frm_hme_sad_q10, rc_pic_type);

                rc_set_num_scd_in_lap_window(
                    ps_rc_ctxt->rc_hdl, i4_num_scd_in_lap_window, num_frames_b4_scd);

                if(rc_pic_type == I_PIC && updated_window > (ps_rc_ctxt->i4_max_inter_frm_int << 1))
                {
                    float i_to_avg_bit_ratio = ihevce_get_i_to_avg_ratio(
                        (void *)ps_rc_ctxt,
                        ps_rc_lap_out,
                        1,
                        1,
                        1,
                        ps_rc_lap_out->ai4_offsets,
                        i4_update_delay);
                    i_to_avg_bit_ratio = i_to_avg_bit_ratio * 1;
                }

                /* accumulation of the hme sad over next sub gop to find the temporal comlexity of the sub GOP*/
                if((rc_pic_type == I_PIC) || (rc_pic_type == P_PIC))
                {
                    ihevce_compute_temporal_complexity_reset_Kp_Kb(
                        ps_rc_lap_out, (void *)ps_rc_ctxt, 1);
                }

                if(i4_var_sum > MAX_LAP_VAR)
                {
                    i4_var_sum = MAX_LAP_VAR;
                }

                {
                    /*Filling for dumping data */

                    ps_rc_ctxt->ai4_num_scd_in_lap_window[i4_enc_frm_id] = i4_num_scd_in_lap_window;
                    ps_rc_ctxt->ai4_num_frames_b4_scd[i4_enc_frm_id] = num_frames_b4_scd;
                }
            }
        }

        if((ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6) && (rc_pic_type > P_PIC))
        {
            ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] = 0;
            is_scd_ref_frame = 0;
        }
        i4_fade_scene = 0;
        /*Scene type fade is marked only for P pics which are in fade regions*/
        if((ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_FADE_IN ||
            ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_FADE_OUT) &&
           (ps_rc_lap_out->i4_rc_temporal_lyr_id == 0))
        {
            is_scd_ref_frame = 1;
            i4_fade_scene = 1;
        }

        if((!(is_scd_ref_frame || ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id])) &&
           (((is_first_frame_coded(ps_rc_ctxt->rc_hdl)) && (pic_type == IV_I_FRAME)) ||
            (pic_type != IV_I_FRAME)))
        {
            WORD32 i4_is_first_frame_coded = is_first_frame_coded(ps_rc_ctxt->rc_hdl);
            i4_is_no_model_scd = 0;
            if(call_type == ENC_GET_QP)
            {
                if(((0 == i4_model_available) || (!i4_is_first_frame_coded)))
                {
                    /*No scene change but model not available*/
                    i4_is_no_model_scd = 1;
                }
            }
        }
        else
        {
            /*actual scene changes*/
            i4_is_no_model_scd = 2;
        }
        /** Pre-enc thread as of now SCD handling is not present */
        if(!i4_is_no_model_scd)
        {
            WORD32 i4_is_first_frame_coded, i4_prev_I_frm_sad, i4_cur_I_frm_sad;
            /*Once first frame has been encoded use prev frame intra satd and cur frame satd to alter est intra sad for cur frame*/
            i4_is_first_frame_coded = is_first_frame_coded(ps_rc_ctxt->rc_hdl);

            /*prev I frame sad i changes only in enc stage. For pre enc cur and prev will be same*/
            if(ps_rc_ctxt->i8_prev_i_frm_cost > 0)
            {
                if(i4_is_first_frame_coded && (pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME))
                {
                    i4_prev_I_frm_sad = rc_get_prev_frame_intra_sad(ps_rc_ctxt->rc_hdl);
                    i4_cur_I_frm_sad = (WORD32)(
                        (ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id] * i4_prev_I_frm_sad) /
                        ps_rc_ctxt->i8_prev_i_frm_cost);
                    rc_update_prev_frame_intra_sad(ps_rc_ctxt->rc_hdl, i4_cur_I_frm_sad);
                }
            }
            /*scale previous frame closed loop SAD with current frame HME SAD to be considered as current frame SAD*/
            if(i4_is_first_frame_coded && !(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) &&
               call_type == ENC_GET_QP)
            {
                if(ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] > 0)
                {
                    WORD32 prev_frm_cl_sad = rc_get_prev_frame_sad(ps_rc_ctxt->rc_hdl, rc_pic_type);
                    WORD32 cur_frm_est_cl_sad = (WORD32)(
                        (ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i4_enc_frm_id] *
                         prev_frm_cl_sad) /
                        ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type]);
                    rc_update_prev_frame_sad(ps_rc_ctxt->rc_hdl, cur_frm_est_cl_sad, rc_pic_type);
                }
            }

            if(rc_pic_type == I_PIC && updated_window > (ps_rc_ctxt->i4_max_inter_frm_int << 1))
            {
                ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id] = ihevce_get_i_to_avg_ratio(
                    (void *)ps_rc_ctxt,
                    ps_rc_lap_out,
                    1,
                    0,
                    1,
                    ps_rc_lap_out->ai4_offsets,
                    i4_update_delay);
            }

            ps_rc_ctxt->s_rc_high_lvl_stat.i8_bits_from_finalQP = -1;
            i4_frame_qp_q6 = get_frame_level_qp(
                ps_rc_ctxt->rc_hdl,
                rc_pic_type,
                i4_max_frame_bits,
                &i4_cur_est_texture_bits,  //this value is returned by rc
                ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id],
                1,
                ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id],
                ps_rc_lap_out->ps_frame_info,
                ps_rc_lap_out->i4_complexity_bin,
                i4_scene_num, /*no pause resume concept*/
                pi4_tot_bits_estimated,
                &ps_rc_lap_out->i4_is_model_valid,
                &i4_vbv_buf_max_bits,
                &i4_est_tex_bits,
                &i4_cur_est_header_bits,
                &ps_rc_ctxt->s_rc_high_lvl_stat.i4_maxEbfQP,
                &ps_rc_ctxt->s_rc_high_lvl_stat.i4_modelQP,
                &i4_estimate_to_calc_frm_error);
            ASSERT(*pi4_tot_bits_estimated != 0);
            /** The usage of global table will truncate the input given as qp format and hence will not return very low qp values desirable at very
            low bitrate. Hence on the fly calculation is enabled*/

            i4_hevc_frame_qp =
                ihevce_rc_get_scaled_hevce_qp_q6(i4_frame_qp_q6, ps_rc_ctxt->u1_bit_depth);

            if(1 == ps_rc_lap_out->i4_is_model_valid)
                ps_rc_lap_out->i4_is_steady_state = 1;
            else
                ps_rc_lap_out->i4_is_steady_state = 0;

            ps_rc_ctxt->s_rc_high_lvl_stat.i4_is_offline_model_used = 0;
            ps_rc_ctxt->i8_est_I_pic_header_bits = i4_cur_est_header_bits;
        }
        else
        {
            WORD32 i4_count = 0, i4_total_bits, i4_min_error_hevc_qp = 0;
            float f_percent_error = 0.0f, f_min_error = 10000.0f;
            WORD32 i4_current_bits_estimated = 0;
            float i4_i_to_rest_ratio_final;
            WORD32 i4_best_br_id = 0;
            float af_i_qs[2];
            LWORD64 ai8_i_tex_bits[2];
            WORD32 i4_ref_qscale = ihevce_rc_get_scaled_mpeg2_qp(
                ps_rc_lap_out->i4_L0_qp, ps_rc_ctxt->ps_rc_quant_ctxt);
            WORD32 ai4_header_bits[2];

            ps_rc_lap_out->i4_is_steady_state = 0;

            if(ps_rc_lap_out->i4_L0_qp > 44)
                ps_rc_lap_out->i4_L0_qp = 44;
            if(ps_rc_lap_out->i4_L0_qp < 7 - ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset)
                ps_rc_lap_out->i4_L0_qp = 7 - ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset;

            ps_rc_lap_out->i4_L0_qp = ps_rc_lap_out->i4_L0_qp - 9;
            ps_rc_lap_out->i4_is_model_valid = 0;
            ps_rc_ctxt->s_rc_high_lvl_stat.i4_is_offline_model_used = 1;
            ps_rc_ctxt->s_rc_high_lvl_stat.i8_bits_from_finalQP = -1;

            ps_rc_ctxt->i4_normal_inter_pic = (i4_is_no_model_scd == 1);
            while(1)
            {
                WORD32 i4_frame_qs_q3;
                WORD32 i4_estimate_to_calc_frm_error_temp;

                i_to_avg_bit_ratio = ihevce_get_i_to_avg_ratio(
                    (void *)ps_rc_ctxt,
                    ps_rc_lap_out,
                    1,
                    0,
                    1,
                    ps_rc_lap_out->ai4_offsets,
                    i4_update_delay);

                ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id] = i_to_avg_bit_ratio;

                /** Use estimate of header bits from pre-enc*/
                if(1 == i4_is_no_model_scd)
                {
                    ps_rc_ctxt->i8_est_I_pic_header_bits =
                        get_est_hdr_bits(ps_rc_ctxt->rc_hdl, rc_pic_type);
                }
                else
                {
                    WORD32 i4_curr_qscale = ihevce_rc_get_scaled_mpeg2_qp(
                        ps_rc_lap_out->i4_L0_qp, ps_rc_ctxt->ps_rc_quant_ctxt);
                    /*Assume that 30% of header bits are constant and remaining are dependent on Qp
                    and map them accordingly*/
                    ps_rc_ctxt->i8_est_I_pic_header_bits = (LWORD64)(
                        (.3 * ps_rc_lap_out->i8_est_I_pic_header_bits +
                         (1. - .3) * ps_rc_lap_out->i8_est_I_pic_header_bits * i4_ref_qscale) /
                        i4_curr_qscale);
                }

                /*get qp for scene cut frame based on offline data*/
                index = ihevce_get_offline_index(
                    ps_rc_ctxt, ps_rc_lap_out->i4_num_pels_in_frame_considered);

                /*Sub pic rC bits extraction */
                i4_frame_qs_q3 = rc_get_qp_for_scd_frame(
                    ps_rc_ctxt->rc_hdl,
                    I_PIC,
                    ps_rc_lap_out->i8_frame_satd_act_accum,
                    ps_rc_lap_out->i4_num_pels_in_frame_considered,
                    (WORD32)ps_rc_ctxt->i8_est_I_pic_header_bits,
                    ps_rc_ctxt->ai4_lap_f_sim[i4_enc_frm_id],
                    (void *)&g_offline_i_model_coeff[index][0],
                    i_to_avg_bit_ratio,
                    1,
                    ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id],
                    ps_rc_lap_out->ps_frame_info,
                    ps_rc_ctxt->i4_rc_pass,
                    (rc_pic_type != I_PIC),
                    ((ps_rc_lap_out->i4_rc_temporal_lyr_id != ps_rc_ctxt->i4_max_temporal_lyr) ||
                     (!ps_rc_ctxt->i4_max_temporal_lyr)),
                    1,
                    &i4_total_bits,
                    &i4_current_bits_estimated,
                    ps_rc_lap_out->i4_use_offline_model_2pass,
                    ai8_i_tex_bits,
                    af_i_qs,
                    i4_best_br_id,
                    &i4_estimate_to_calc_frm_error_temp);

                i4_hevc_frame_qp = ihevce_rc_get_scaled_hevc_qp_from_qs_q3(
                    i4_frame_qs_q3, ps_rc_ctxt->ps_rc_quant_ctxt);

                /*Get corresponding q scale*/
                i4_frame_qp =
                    ihevce_rc_get_scaled_mpeg2_qp(i4_hevc_frame_qp, ps_rc_ctxt->ps_rc_quant_ctxt);

                if(i4_hevc_frame_qp > ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp)
                    i4_hevc_frame_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp;

                {
                    WORD32 i4_init_qscale = ihevce_rc_get_scaled_mpeg2_qp(
                        ps_rc_lap_out->i4_L0_qp, ps_rc_ctxt->ps_rc_quant_ctxt);
                    f_percent_error = (float)(abs(i4_init_qscale - i4_frame_qp)) / i4_init_qscale;
                    if(f_percent_error < f_min_error)
                    {
                        f_min_error = f_percent_error;
                        i4_min_error_hevc_qp = i4_hevc_frame_qp;
                        i4_i_to_rest_ratio_final = i_to_avg_bit_ratio;
                        /*Get the bits estimated for least error*/
                        *pi4_tot_bits_estimated = i4_current_bits_estimated;
                        i4_estimate_to_calc_frm_error = i4_estimate_to_calc_frm_error_temp;
                    }
                    else
                    {}
                    ASSERT(*pi4_tot_bits_estimated != 0);
                }
                i4_count++;
                if(/*(ps_rc_lap_out->i4_L0_qp == i4_hevc_frame_qp) ||*/ (i4_count > 17))
                    break;
                ps_rc_lap_out->i4_L0_qp++;
            }
            ps_rc_lap_out->i4_L0_qp = i4_min_error_hevc_qp;

            i4_hevc_frame_qp = i4_min_error_hevc_qp;
            if(2 == i4_is_no_model_scd)
            {
                /* SGI & Enc Loop Parallelism related changes*/

                /*model reset not required if it is first frame*/
                if(ps_rc_ctxt->i4_is_first_frame_encoded && !i4_fade_scene &&
                   !ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id] &&
                   !ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] &&
                   !ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] &&
                   !ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id])
                {
                    ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id] = 1;
                    /*reset all pic type is first frame encoded flag*/

                    ASSERT(pic_type == IV_IDR_FRAME || pic_type == IV_I_FRAME);
                }
                else if(ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id])
                {
                    rc_reset_first_frame_coded_flag(ps_rc_ctxt->rc_hdl, I_PIC);
                    ASSERT(rc_pic_type == I_PIC);
                    ASSERT(ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] == 0);
                }
                else if(
                    ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] ||
                    ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] ||
                    ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id] || i4_fade_scene)
                {
                    /*Only when there are back to back scene cuts we need a non- Ipic will be marked as scene cut*/
                    /* Same path can also be followed during pause to resume detection to determine cur frame qp however handling during update is different*/
                    WORD32 i4_prev_qp, i, i4_new_qp_hevc_qp, I_hevc_qp, cur_hevc_qp;

                    /*both cannot be set at same time since lap cannot mark same frame as both scene cut and pause to resume flag*/
                    ASSERT(
                        (ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] &&
                         ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id]) == 0);

                    I_hevc_qp = i4_hevc_frame_qp;

                    /*alter ai4_prev_pic_hevc_qp so that qp restriction ll not let even other pictures temporary scd are thrashed*/
                    //if(ps_rc_lap_out->i4_rc_temporal_lyr_id != ps_rc_ctxt->i4_max_temporal_lyr)
                    {
                        if(ps_rc_ctxt->i4_field_pic == 0)
                        {
                            for(i = 1; i < ps_rc_ctxt->i4_num_active_pic_type; i++)
                            {
                                i4_prev_qp = ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i];
                                i4_new_qp_hevc_qp = I_hevc_qp + i;
                                i4_new_qp_hevc_qp = ihevce_clip_min_max_qp(
                                    ps_rc_ctxt, i4_new_qp_hevc_qp, (picture_type_e)i, i - 1);
                                if(i4_prev_qp < i4_new_qp_hevc_qp)
                                {
                                    ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i] =
                                        i4_new_qp_hevc_qp;
                                }
                            }
                        }
                        else
                        { /*field case*/

                            for(i = 1; i < ps_rc_ctxt->i4_num_active_pic_type; i++)
                            {
                                i4_prev_qp = ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i];
                                i4_new_qp_hevc_qp = I_hevc_qp + i;
                                i4_new_qp_hevc_qp = ihevce_clip_min_max_qp(
                                    ps_rc_ctxt, i4_new_qp_hevc_qp, (picture_type_e)i, i - 1);
                                if(i4_prev_qp < i4_new_qp_hevc_qp)
                                {
                                    ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i] =
                                        i4_new_qp_hevc_qp;
                                }

                                i4_prev_qp =
                                    ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i + FIELD_OFFSET];
                                i4_new_qp_hevc_qp = I_hevc_qp + i;
                                i4_new_qp_hevc_qp = ihevce_clip_min_max_qp(
                                    ps_rc_ctxt, i4_new_qp_hevc_qp, (picture_type_e)i, i - 1);
                                if(i4_prev_qp < i4_new_qp_hevc_qp)
                                {
                                    ps_rc_ctxt
                                        ->ai4_prev_pic_hevc_qp[i4_scene_num][i + FIELD_OFFSET] =
                                        i4_new_qp_hevc_qp;
                                }
                            }
                        }
                    }
                    {
                        WORD32 i4_updated_qp = ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i];
                        WORD32 i4_scale;

                        if(I_hevc_qp == i4_updated_qp)
                            i4_scale = 16;
                        else if(I_hevc_qp == (i4_updated_qp - 1))
                            i4_scale = 14;
                        else if(I_hevc_qp == (i4_updated_qp - 2))
                            i4_scale = 12;
                        else
                            i4_scale = 10;

                        *pi4_tot_bits_estimated = (i4_scale * (*pi4_tot_bits_estimated)) >> 4;
                        i4_estimate_to_calc_frm_error =
                            (i4_scale * i4_estimate_to_calc_frm_error) >> 4;
                    }
                    if(call_type == ENC_GET_QP)
                    {
                        ps_rc_lap_out->i8_est_text_bits = *pi4_tot_bits_estimated;
                    }
                    ASSERT(*pi4_tot_bits_estimated != 0);

                    /*use previous frame qp of same pic type or SCD i frame qp with offset whichever is maximum*/
                    /*For field case adding of  grater than 4 results in the qp increasing greatly when compared to previous pics/fields*/
                    if(rc_pic_type <= FIELD_OFFSET)
                        cur_hevc_qp = I_hevc_qp + rc_pic_type;
                    else
                        cur_hevc_qp = I_hevc_qp + (rc_pic_type - FIELD_OFFSET);

                    i4_prev_qp = ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type];

                    if((cur_hevc_qp < i4_prev_qp) && (ps_rc_ctxt->i4_num_active_pic_type > 2) &&
                       (is_first_frame_coded(ps_rc_ctxt->rc_hdl)) && (!i4_fade_scene))
                    {
                        cur_hevc_qp = i4_prev_qp;
                    }
                    i4_frame_qp =
                        ihevce_rc_get_scaled_mpeg2_qp(cur_hevc_qp, ps_rc_ctxt->ps_rc_quant_ctxt);
                    i4_hevc_frame_qp = cur_hevc_qp;
                    //ps_rc_ctxt->i4_is_non_I_scd_pic = 0;

                    rc_reset_first_frame_coded_flag(ps_rc_ctxt->rc_hdl, rc_pic_type);
                }
                else
                {}
            }
            if((1 == i4_is_no_model_scd) && (call_type == ENC_GET_QP))
            {
                WORD32 i4_clip_QP;
                i4_frame_qp_q6 =
                    clip_qp_based_on_prev_ref(ps_rc_ctxt->rc_hdl, rc_pic_type, 1, i4_scene_num);
                i4_clip_QP =
                    ihevce_rc_get_scaled_hevce_qp_q6(i4_frame_qp_q6, ps_rc_ctxt->u1_bit_depth);
                if(ps_rc_ctxt->i4_rc_pass != 2)
                {
                    i4_hevc_frame_qp = i4_clip_QP;
                }
                if((rc_pic_type == P_PIC) || (rc_pic_type == P1_PIC))
                {
                    *pi4_tot_bits_estimated = (*pi4_tot_bits_estimated * 11) >> 4; /* P picture*/
                    i4_estimate_to_calc_frm_error = (i4_estimate_to_calc_frm_error * 11) >> 4;
                }
                else if((rc_pic_type == B_PIC) || (rc_pic_type == BB_PIC))
                {
                    *pi4_tot_bits_estimated = (*pi4_tot_bits_estimated * 9) >> 4; /* B layer 1*/
                    i4_estimate_to_calc_frm_error = (i4_estimate_to_calc_frm_error * 9) >> 4;
                }
                else if((rc_pic_type == B1_PIC) || (rc_pic_type == B11_PIC))
                {
                    *pi4_tot_bits_estimated = (*pi4_tot_bits_estimated * 7) >> 4; /* B layer 2*/
                    i4_estimate_to_calc_frm_error = (i4_estimate_to_calc_frm_error * 7) >> 4;
                }
                else if((rc_pic_type == B2_PIC) || (rc_pic_type == B22_PIC))
                {
                    *pi4_tot_bits_estimated = (*pi4_tot_bits_estimated * 5) >> 4; /* B layer 3*/
                    i4_estimate_to_calc_frm_error = (i4_estimate_to_calc_frm_error * 5) >> 4;
                }
            }
            rc_add_est_tot(ps_rc_ctxt->rc_hdl, *pi4_tot_bits_estimated);
        }

        ASSERT(i4_hevc_frame_qp >= -ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset);

        /*constraint qp swing based on neighbour frames*/
        if(is_first_frame_coded(ps_rc_ctxt->rc_hdl))
        {
            if(ps_rc_ctxt->i4_field_pic == 0)
            {
                /*In dissolve case the p frame comes before an I pic and ref b comes after then what
                happens is b frame qp is restricted by the p frame qp so changed it to prev ref pic type*/
                if(rc_pic_type != I_PIC && rc_pic_type != P_PIC)
                {
                    if(ps_rc_lap_out->i4_rc_temporal_lyr_id == 1)
                    {
                        picture_type_e prev_ref_pic_type =
                            rc_getprev_ref_pic_type(ps_rc_ctxt->rc_hdl);

                        if(i4_hevc_frame_qp >
                           ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][prev_ref_pic_type] + 3)
                        {
                            if(ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][prev_ref_pic_type] >
                               0)
                                i4_hevc_frame_qp =
                                    ps_rc_ctxt
                                        ->ai4_prev_pic_hevc_qp[i4_scene_num][prev_ref_pic_type] +
                                    3;
                        }
                    }
                    else if(
                        i4_hevc_frame_qp >
                        (ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1] + 3))
                    {
                        /*allow max of +3 compared to previous frame*/
                        if(ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1] > 0)
                            i4_hevc_frame_qp =
                                ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1] + 3;
                    }
                }

                if((rc_pic_type != I_PIC && rc_pic_type != P_PIC) &&
                   (i4_hevc_frame_qp <
                    ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1]))
                {
                    i4_hevc_frame_qp =
                        ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1];
                }

                /** Force non-ref B pic qp to be ref_B_PIC_qp - 1. This is not valid for when max teporla later is less than 2*/
                if(temporal_layer_id == ps_rc_ctxt->i4_max_temporal_lyr &&
                   ps_rc_ctxt->i4_max_temporal_lyr > 1)
                {
                    i4_hevc_frame_qp =
                        ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1] + 1;
                }
            }
            else /*for field case*/
            {
                if(ps_rc_lap_out->i4_rc_temporal_lyr_id >= 1)
                {
                    /*To make the comparison of qp with the top field's of previous layer tempor layer id matches with the pic type. */
                    if(i4_hevc_frame_qp >
                       ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num]
                                                       [ps_rc_lap_out->i4_rc_temporal_lyr_id] +
                           3)
                    {
                        /*allow max of +3 compared to previous frame*/
                        if(0 <
                           ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num]
                                                           [ps_rc_lap_out->i4_rc_temporal_lyr_id])
                            i4_hevc_frame_qp =
                                ps_rc_ctxt
                                    ->ai4_prev_pic_hevc_qp[i4_scene_num]
                                                          [ps_rc_lap_out->i4_rc_temporal_lyr_id] +
                                3;
                    }
                    if(i4_hevc_frame_qp <
                       ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num]
                                                       [ps_rc_lap_out->i4_rc_temporal_lyr_id])
                    {
                        i4_hevc_frame_qp =
                            ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num]
                                                            [ps_rc_lap_out->i4_rc_temporal_lyr_id];
                    }

                    /** Force non-ref B pic qp to be ref_B_PIC_qp - 1. This is not valid for when max teporla later is less than 2*/
                    if(temporal_layer_id == ps_rc_ctxt->i4_max_temporal_lyr &&
                       ps_rc_ctxt->i4_max_temporal_lyr > 1)
                    {
                        i4_hevc_frame_qp =
                            ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num]
                                                            [ps_rc_lap_out->i4_rc_temporal_lyr_id] +
                            1;
                    }
                }
                /** At lower range qp swing for same pic type is also imposed to make sure
                    qp does not fall from 10 to 4 since they differ by only one q scale*/
            }
        }

        /**clip to min qp which is user configurable*/
        i4_hevc_frame_qp = ihevce_clip_min_max_qp(
            ps_rc_ctxt, i4_hevc_frame_qp, rc_pic_type, ps_rc_lap_out->i4_rc_temporal_lyr_id);

#if 1  //FRAME_PARALLEL_LVL
        ps_rc_ctxt->i4_est_text_bits_ctr_get_qp++;  //ELP_RC
        ps_rc_ctxt->i4_est_text_bits_ctr_get_qp =
            (ps_rc_ctxt->i4_est_text_bits_ctr_get_qp % (ps_rc_ctxt->i4_num_frame_parallel));
#endif
        /** the estimates are reset only duing enc call*/

#if USE_USER_FIRST_FRAME_QP
        /*I_PIC check is necessary coz pre-enc can query for qp even before first frame update has happened*/
        if(!ps_rc_ctxt->i4_is_first_frame_encoded && rc_pic_type == I_PIC)
        {
            i4_hevc_frame_qp = ps_rc_ctxt->i4_init_frame_qp_user;
            DBG_PRINTF("FIXED START QP PATH *************************\n");
        }
#endif
    }

    if(CONST_QP != e_rc_type)
    {
        ASSERT(*pi4_tot_bits_estimated != 0);
    }

    ps_rc_ctxt->s_rc_high_lvl_stat.i4_finalQP = i4_hevc_frame_qp;
    if(ps_rc_lap_out->i4_is_model_valid)
    {
        get_bits_for_final_qp(
            ps_rc_ctxt->rc_hdl,
            &ps_rc_ctxt->s_rc_high_lvl_stat.i4_modelQP,
            &ps_rc_ctxt->s_rc_high_lvl_stat.i4_maxEbfQP,
            &ps_rc_ctxt->s_rc_high_lvl_stat.i8_bits_from_finalQP,
            i4_hevc_frame_qp,
            ihevce_rc_get_scaled_mpeg2_qp_q6(
                i4_hevc_frame_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset,
                ps_rc_ctxt->u1_bit_depth),
            i4_cur_est_header_bits,
            i4_est_tex_bits,
            i4_vbv_buf_max_bits,
            rc_pic_type,
            ps_rc_lap_out->i4_rc_display_num);
    }
    i4_deltaQP = ihevce_ebf_based_rc_correction_to_avoid_overflow(
        ps_rc_ctxt, ps_rc_lap_out, pi4_tot_bits_estimated);
    i4_hevc_frame_qp += i4_deltaQP;

    /**clip to min qp which is user configurable*/
    i4_hevc_frame_qp = ihevce_clip_min_max_qp(
        ps_rc_ctxt, i4_hevc_frame_qp, rc_pic_type, ps_rc_lap_out->i4_rc_temporal_lyr_id);

    /*set estimate status for frame level error calculation*/
    if(i4_estimate_to_calc_frm_error > 0)
    {
        rc_set_estimate_status(
            ps_rc_ctxt->rc_hdl,
            i4_estimate_to_calc_frm_error - ps_rc_ctxt->i8_est_I_pic_header_bits,
            ps_rc_ctxt->i8_est_I_pic_header_bits,
            ps_rc_ctxt->i4_est_text_bits_ctr_get_qp);
    }
    else
    {
        rc_set_estimate_status(
            ps_rc_ctxt->rc_hdl,
            -1,
            ps_rc_ctxt->i8_est_I_pic_header_bits,
            ps_rc_ctxt->i4_est_text_bits_ctr_get_qp);
    }

    ps_rc_lap_out->i8_est_text_bits = *pi4_tot_bits_estimated;

    /*B pictures which are in fades will take the highest QP of either side of P pics*/
    if(ps_rc_lap_out->i4_rc_pic_type == IV_B_FRAME &&
       (ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_FADE_IN ||
        ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_FADE_OUT))
    {
        i4_hevc_frame_qp =
            MAX(ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[0], ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[1]);
    }

    /*saving the last two pics of layer 0*/
    if(0 == ps_rc_lap_out->i4_rc_temporal_lyr_id)
    {
        ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[1] = ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[0];
        ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[0] = i4_hevc_frame_qp;
    }

    return i4_hevc_frame_qp;
}

/*##########################################################*/
/******* END OF ENC THRD QP QUERY FUNCTIONS ****************/
/*########################################################*/

/*####################################################*/
/******* START OF I2AVG RATIO FUNCTIONS **************/
/*##################################################*/

/**
******************************************************************************
*
*  @brief function to get i_to_avg_rest at scene cut frame based on data available from LAP
*
*  @par   Description
*
*  @param[in]   pv_rc_ctxt
*               void pointer to rc ctxt
*  @param[in]   ps_rc_lap_out : pointer to lap out structure
*  @param[in]   i4_update_delay : The Delay in the update. This can happen for dist. case!
*               All decision should consider this delay for updation!
*  @return      WORD32 i_to_rest bit ratio
*
******************************************************************************
*/
float ihevce_get_i_to_avg_ratio(
    void *pv_rc_ctxt,
    rc_lap_out_params_t *ps_rc_lap_out,
    WORD32 i_to_p_qp_offset,
    WORD32 i4_offset_flag,
    WORD32 i4_call_type,
    WORD32 ai4_qp_offsets[4],
    WORD32 i4_update_delay)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    WORD32 i = 0, k = 0, num_frames_in_lap[MAX_PIC_TYPE] = { 0 }, ai4_pic_dist[MAX_PIC_TYPE],
           ai4_pic_dist_in_cur_gop[MAX_PIC_TYPE] = { 0 };
    WORD32 i4_num_b, i4_num_frms_traversed_in_lap = 0, total_frms_considered = 0,
                     i4_flag_i_frame_exit = 0, u4_rc_scene_number;
    rc_lap_out_params_t *ps_cur_rc_lap_out = ps_rc_lap_out;

    rc_lap_out_params_t *ps_cur_rc_lap_out_I = ps_rc_lap_out;
    double complexity[MAX_PIC_TYPE] = { 0 }, d_first_i_complexity = 0, d_first_p_complexity = 0.0f,
           cur_lambda_modifer, den = 0, average_intra_complexity = 0;
    double i_frm_lambda_modifier;
    float i_to_rest_bit_ratio = 8.00;
    picture_type_e curr_rc_pic_type;
    LWORD64 i8_l1_analysis_lap_comp = 0;
    WORD32 i4_intra_frame_interval = rc_get_intra_frame_interval(ps_rc_ctxt->rc_hdl);
    UWORD32 u4_L1_based_lap_complexity_q7 = 0;
    WORD32 i4_frame_qp = 0, i4_I_frame_qp = 0;

    WORD32 ai4_lambda_offsets[5] = { -3, -2, 2, 6, 7 };
    /* The window for which your update is guaranteed */
    WORD32 updated_window = ps_rc_ctxt->i4_num_frame_in_lap_window - i4_update_delay;

    ASSERT(ps_rc_ctxt->i4_rc_pass != 2);
    rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]);

    if(ps_rc_ctxt->i4_max_temporal_lyr)
    {
        i4_num_b = ((WORD32)pow((float)2, ps_rc_ctxt->i4_max_temporal_lyr)) - 1;
    }
    else
    {
        i4_num_b = 0;
    }
    i_frm_lambda_modifier = ihevce_get_frame_lambda_modifier((WORD8)I_PIC, 0, 1, 1, i4_num_b);
    /* check should be wrt inter frame interval*/
    /*If lap frames are not sufficient return default ratio*/
    u4_rc_scene_number = ps_cur_rc_lap_out_I->u4_rc_scene_num;

    if(updated_window < 4)
    {
        return i_to_rest_bit_ratio;
    }

    k = 0;
    if(ps_cur_rc_lap_out != NULL)
    {
        WORD32 i4_temp_frame_qp;

        if(ps_cur_rc_lap_out->i4_L0_qp == -1)
        {
            i4_frame_qp = ps_cur_rc_lap_out->i4_L1_qp;
            i4_I_frame_qp = ps_cur_rc_lap_out->i4_L1_qp - 3;
        }
        else
        {
            i4_frame_qp = ps_cur_rc_lap_out->i4_L0_qp;
            i4_I_frame_qp = ps_cur_rc_lap_out->i4_L0_qp - 3;
        }

        do
        {
            curr_rc_pic_type = ihevce_rc_conv_pic_type(
                (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out->i4_rc_pic_type,
                ps_rc_ctxt->i4_field_pic,
                ps_cur_rc_lap_out->i4_rc_temporal_lyr_id,
                ps_cur_rc_lap_out->i4_is_bottom_field,
                ps_rc_ctxt->i4_top_field_first);
            cur_lambda_modifer = ihevce_get_frame_lambda_modifier(
                (WORD8)curr_rc_pic_type,
                ps_cur_rc_lap_out->i4_rc_temporal_lyr_id,
                1,
                ps_cur_rc_lap_out->i4_rc_is_ref_pic,
                i4_num_b);
            if(curr_rc_pic_type == I_PIC)
            {
                i4_temp_frame_qp = i4_frame_qp + ai4_lambda_offsets[curr_rc_pic_type];
            }
            else
            {
                i4_temp_frame_qp =
                    i4_frame_qp + ai4_lambda_offsets[ps_cur_rc_lap_out->i4_rc_temporal_lyr_id + 1];
                i4_temp_frame_qp =
                    i4_temp_frame_qp +
                    ps_cur_rc_lap_out->ai4_offsets[ps_cur_rc_lap_out->i4_rc_temporal_lyr_id + 1];
            }

            i4_temp_frame_qp = CLIP3(i4_temp_frame_qp, 1, 51);
            i4_I_frame_qp = CLIP3(i4_I_frame_qp, 1, 51);

            if(curr_rc_pic_type == I_PIC)
            {
                complexity[I_PIC] += (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp];
                if(total_frms_considered == 0)
                    d_first_i_complexity =
                        (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp];

                num_frames_in_lap[I_PIC]++;
                i8_l1_analysis_lap_comp +=
                    (LWORD64)(1.17 * ps_cur_rc_lap_out->i8_raw_pre_intra_sad);
            }
            else
            {
                if((num_frames_in_lap[P_PIC] == 0) && (curr_rc_pic_type == P_PIC))
                    d_first_p_complexity =
                        (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp];

                if(total_frms_considered == 0)
                {
                    num_frames_in_lap[I_PIC]++;
                    {
                        complexity[I_PIC] +=
                            (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp];
                        d_first_i_complexity =
                            (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp];
                    }
                }
                else
                {
                    /*SAD is scaled according the lambda parametrs use to make it proportional to bits consumed in the end*/
#if !USE_SQRT
                    //complexity[curr_rc_pic_type] += (double)(MIN(ps_cur_rc_lap_out->ai8_frame_acc_coarse_me_sad[i4_temp_frame_qp],ps_cur_rc_lap_out->i8_pre_intra_sad)/(/*(cur_lambda_modifer/i_frm_lambda_modifier) * */pow(1.125,(ps_rc_lap_out->i4_rc_temporal_lyr_id + 1/*i_to_p_qp_offset*/))));
                    if((curr_rc_pic_type > P_PIC) &&
                       (ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6))
                        complexity[curr_rc_pic_type] +=
                            (double)(ps_cur_rc_lap_out->ai8_frame_acc_coarse_me_sad
                                         [i4_temp_frame_qp]);  // /(/*(cur_lambda_modifer/i_frm_lambda_modifier) * */pow(1.125,(ps_rc_lap_out->i4_rc_temporal_lyr_id + 1/*i_to_p_qp_offset*/))));
                    else
                        complexity[curr_rc_pic_type] += (double)(MIN(
                            ps_cur_rc_lap_out->ai8_frame_acc_coarse_me_sad[i4_temp_frame_qp],
                            ps_cur_rc_lap_out->ai8_pre_intra_sad
                                [i4_temp_frame_qp]));  ///(/*(cur_lambda_modifer/i_frm_lambda_modifier) * */pow(1.125,(ps_rc_lap_out->i4_rc_temporal_lyr_id + 1/*i_to_p_qp_offset*/))));

#else
                    complexity[curr_rc_pic_type] +=
                        MIN(ps_cur_rc_lap_out->ai8_frame_acc_coarse_me_sad[i4_temp_frame_qp],
                            ps_cur_rc_lap_out->i8_pre_intra_sad) /
                        (sqrt(cur_lambda_modifer / i_frm_lambda_modifier) *
                         pow(1.125, (ps_rc_lap_out->i4_rc_temporal_lyr_id + 1)));
#endif
                    num_frames_in_lap[curr_rc_pic_type]++;
                }
                i8_l1_analysis_lap_comp += (LWORD64)(
                    (float)ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad /
                    pow(1.125, curr_rc_pic_type));
            }

            if(ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6)
            {
                if(curr_rc_pic_type < B_PIC)
                {
                    /*accumulate average intra sad*/
                    average_intra_complexity +=
                        ps_cur_rc_lap_out
                            ->ai8_pre_intra_sad[i4_I_frame_qp] /*/i_frm_lambda_modifier*/;
                    i4_num_frms_traversed_in_lap++;
                }
            }
            else
            {
                /*accumulate average intra sad*/
                average_intra_complexity +=
                    ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp] /*/i_frm_lambda_modifier*/;
                i4_num_frms_traversed_in_lap++;
            }

            ai4_pic_dist_in_cur_gop[curr_rc_pic_type]++;
            i++;
            total_frms_considered++;
            i4_num_frms_traversed_in_lap++;
            ps_cur_rc_lap_out = (rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode;

            if((ps_cur_rc_lap_out == NULL) ||
               ((total_frms_considered + k) == i4_intra_frame_interval) || (i >= updated_window))
            {
                break;
            }

            if((i >= (ps_rc_ctxt->i4_next_sc_i_in_rc_look_ahead - k) ||
                (ps_cur_rc_lap_out->i4_rc_pic_type == IV_I_FRAME) ||
                (ps_cur_rc_lap_out->i4_rc_pic_type == IV_IDR_FRAME)) &&
               (i4_offset_flag == 1))
            {
                break;
            }
            /*If an I frame enters the lookahead it can cause bit allocation to go bad
            if corresponding p/b frames are absent*/
            if(((total_frms_considered + k) > (WORD32)(0.75f * i4_intra_frame_interval)) &&
               ((ps_cur_rc_lap_out->i4_rc_pic_type == IV_I_FRAME) ||
                (ps_cur_rc_lap_out->i4_rc_pic_type == IV_IDR_FRAME)))
            {
                i4_flag_i_frame_exit = 1;
                break;
            }

        } while(1);

        if(total_frms_considered > 0)
        {
            float lap_L1_comp =
                (float)i8_l1_analysis_lap_comp /
                (total_frms_considered * ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width);

            lap_L1_comp = rc_get_offline_normalized_complexity(
                ps_rc_ctxt->u4_intra_frame_interval,
                ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width,
                lap_L1_comp,
                ps_rc_ctxt->i4_rc_pass);

            u4_L1_based_lap_complexity_q7 = (WORD32)((lap_L1_comp * (1 << 7)) + .05f);
        }
        else
        {
            u4_L1_based_lap_complexity_q7 = 25;
        }

        if(i4_call_type == 1)
        {
            if(num_frames_in_lap[0] > 0)
            {
                float f_curr_i_to_sum = (float)(d_first_i_complexity / complexity[0]);
                f_curr_i_to_sum = CLIP3(f_curr_i_to_sum, 0.1f, 100.0f);
                rc_set_i_to_sum_api_ba(ps_rc_ctxt->rc_hdl, f_curr_i_to_sum);
            }
        }

        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            if(num_frames_in_lap[i] > 0)
            {
                complexity[i] = complexity[i] / num_frames_in_lap[i];
            }
        }
        /*for non - I scd case it is possible that entire LAP window might not have intra picture. Consider average intra sad when
        atleast one I pic is not available*/
        if(num_frames_in_lap[I_PIC] == 0)
        {
            ASSERT(i4_num_frms_traversed_in_lap);
            complexity[I_PIC] = average_intra_complexity / i4_num_frms_traversed_in_lap;
        }
        /*get picture type distribution in LAP*/
        if(num_frames_in_lap[I_PIC] == 0)
        {
            rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]);
        }
        else
        {
            memmove(ai4_pic_dist, num_frames_in_lap, sizeof(WORD32) * MAX_PIC_TYPE);
        }

        {
            WORD32 num_inter_pic = 0;
            for(i = 1; i < MAX_PIC_TYPE; i++)
            {
                den += complexity[i] * ai4_pic_dist[i];
            }

            for(i = 1; i < MAX_PIC_TYPE; i++)
            {
                num_inter_pic += ai4_pic_dist[i];
            }
            if(num_inter_pic > 0)
                den = den / num_inter_pic;
            else
                den = 0.0;
        }

        if(den > 0)
            i_to_rest_bit_ratio = (float)((complexity[I_PIC]) / den);
        else
            i_to_rest_bit_ratio = 15;

        if((total_frms_considered < (WORD32)(0.75f * i4_intra_frame_interval)) &&
           (total_frms_considered < (updated_window - 1)) &&
           ((UWORD32)total_frms_considered < ((ps_rc_ctxt->u4_max_frame_rate / 1000))))
        {
            /*This GOP will only sustain for few frames hence have strict restriction for I to rest ratio*/
            if(i_to_rest_bit_ratio > 12)
                i_to_rest_bit_ratio = 12;

            if(i_to_rest_bit_ratio > 8 &&
               total_frms_considered < (ps_rc_ctxt->i4_max_inter_frm_int * 2))
                i_to_rest_bit_ratio = 8;
        }
    }

    if((i4_call_type == 1) && (i_to_rest_bit_ratio < I_TO_REST_VVFAST) && (i4_offset_flag == 1))
    {
        float f_p_to_i_ratio = (float)(d_first_p_complexity / d_first_i_complexity);
        if(ps_rc_lap_out->i8_frame_satd_act_accum <
           (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width * 1.5f))
            rc_set_p_to_i_complexity_ratio(ps_rc_ctxt->rc_hdl, f_p_to_i_ratio);
    }

    /*Reset the pic distribution if I frame exit was encountered*/

    if(ps_rc_ctxt->e_rate_control_type != CONST_QP)
    {
        rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]);
        if((ai4_pic_dist_in_cur_gop[I_PIC] > 1) && (ai4_pic_dist[0] == 1))
        {
            i4_flag_i_frame_exit = 1;
        }
        if(i4_flag_i_frame_exit && (i4_call_type == 1))
        {
            if(ai4_pic_dist_in_cur_gop[I_PIC] == 0)
                memmove(ai4_pic_dist_in_cur_gop, num_frames_in_lap, sizeof(WORD32) * MAX_PIC_TYPE);

            rc_update_pic_distn_lap_to_rc(ps_rc_ctxt->rc_hdl, ai4_pic_dist_in_cur_gop);
            rc_set_bits_based_on_complexity(
                ps_rc_ctxt->rc_hdl, u4_L1_based_lap_complexity_q7, total_frms_considered);
        }
    }

    return i_to_rest_bit_ratio;
}

/*##################################################*/
/******* END OF I2AVG RATIO FUNCTIONS **************/
/*################################################*/

/*#########################################################*/
/******* START OF QSCALE CONVERSION FUNCTIONS *************/
/*########################################################*/

/**
******************************************************************************
*
*  @brief function to convert from qscale to qp
*
*  @par   Description
*  @param[in]   i4_frame_qs_q3 : QP value in qscale
*   return      frame qp
******************************************************************************
*/

WORD32 ihevce_rc_get_scaled_hevc_qp_from_qs_q3(WORD32 i4_frame_qs_q3, rc_quant_t *ps_rc_quant_ctxt)
{
    if(i4_frame_qs_q3 > ps_rc_quant_ctxt->i2_max_qscale)
    {
        i4_frame_qs_q3 = ps_rc_quant_ctxt->i2_max_qscale;
    }
    else if(i4_frame_qs_q3 < ps_rc_quant_ctxt->i2_min_qscale)
    {
        i4_frame_qs_q3 = ps_rc_quant_ctxt->i2_min_qscale;
    }

    return (ps_rc_quant_ctxt->pi4_qscale_to_qp[i4_frame_qs_q3]);
}

/**
******************************************************************************
*
*  @brief function to convert from qp to qscale
*
*  @par   Description
*  @param[in]   i4_frame_qp : QP value
*   return      value in qscale
******************************************************************************
*/
WORD32 ihevce_rc_get_scaled_mpeg2_qp(WORD32 i4_frame_qp, rc_quant_t *ps_rc_quant_ctxt)
{
    //i4_frame_qp = i4_frame_qp >> 3; // Q3 format is mantained for accuarate calc at lower qp
    WORD32 i4_qscale;
    if(i4_frame_qp > ps_rc_quant_ctxt->i2_max_qp)
    {
        i4_frame_qp = ps_rc_quant_ctxt->i2_max_qp;
    }
    else if(i4_frame_qp < ps_rc_quant_ctxt->i2_min_qp)
    {
        i4_frame_qp = ps_rc_quant_ctxt->i2_min_qp;
    }

    i4_qscale = (ps_rc_quant_ctxt->pi4_qp_to_qscale[i4_frame_qp + ps_rc_quant_ctxt->i1_qp_offset] +
                 (1 << (QSCALE_Q_FAC_3 - 1))) >>
                QSCALE_Q_FAC_3;
    return i4_qscale;
}

/**
******************************************************************************
*
*  @brief function to convert from qp to qscale
*
*  @par Description : This function maps logarithmic QP values to linear QP
*   values. The linear values are represented in Q6 format.
*
*  @param[in]   i4_frame_qp : QP value (log scale)
*
*  @return value in QP (linear scale)
*
******************************************************************************
*/
WORD32 ihevce_rc_get_scaled_mpeg2_qp_q6(WORD32 i4_frame_qp, UWORD8 u1_bit_depth)
{
    WORD32 i4_frame_qp_q6;
    number_t s_frame_qp;
    float f_qp;

    (void)u1_bit_depth;
    ASSERT(i4_frame_qp >= 0);
    ASSERT(i4_frame_qp <= 51 + ((u1_bit_depth - 8) * 6));
    f_qp = (float)pow((float)2, ((float)(i4_frame_qp - 4) / 6));
    convert_float_to_fix(f_qp, &s_frame_qp);
    convert_varq_to_fixq(s_frame_qp, &i4_frame_qp_q6, QSCALE_Q_FAC);

    if(i4_frame_qp_q6 < (1 << QSCALE_Q_FAC))
        i4_frame_qp_q6 = 1 << QSCALE_Q_FAC;

    return i4_frame_qp_q6;
}

/**
******************************************************************************
*
*  @brief function to convert from qscale to qp
*
*  @par   Description
*  @param[in]   i4_frame_qp_q6 : QP value in qscale. the input is assumed to be in q6 format
*   return      frame qp
******************************************************************************
*/
WORD32 ihevce_rc_get_scaled_hevce_qp_q6(WORD32 i4_frame_qp_q6, UWORD8 u1_bit_depth)
{
    WORD32 i4_hevce_qp;
    number_t s_hevce_qp, s_temp;
    float f_mpeg2_qp, f_hevce_qp;
    f_mpeg2_qp = (float)i4_frame_qp_q6 / (1 << QSCALE_Q_FAC);
    f_hevce_qp = (6 * ((float)log(f_mpeg2_qp) / (float)log((float)2))) + 4;
    convert_float_to_fix(f_hevce_qp, &s_hevce_qp);

    /*rounf off to nearest integer*/
    s_temp.sm = 1;
    s_temp.e = 1;
    add32_var_q(s_hevce_qp, s_temp, &s_hevce_qp);
    number_t_to_word32(s_hevce_qp, &i4_hevce_qp);
    if(i4_frame_qp_q6 == 0)
    {
        i4_hevce_qp = 0;
    }

    i4_hevce_qp -= ((u1_bit_depth - 8) * 6);

    return i4_hevce_qp;
}

/**
******************************************************************************
*
*  @brief function to convert from qp scale to qp
*
*  @par Description : This function maps linear QP values to logarithimic QP
*   values. The linear values are represented in Q3 format.
*
*  @param[in]   i4_frame_qp : QP value (linear scale, Q3 mode)
*
*  @return value in QP (log scale)
*
******************************************************************************
*/
WORD32 ihevce_rc_get_scaled_hevce_qp_q3(WORD32 i4_frame_qp, UWORD8 u1_bit_depth)
{
    WORD32 i4_hevce_qp;
    number_t s_hevce_qp, s_temp;

    if(i4_frame_qp == 0)
    {
        i4_hevce_qp = 0;
    }
    else
    {
        float f_mpeg2_qp, f_hevce_qp;

        f_mpeg2_qp = (float)i4_frame_qp;
        f_hevce_qp = (6 * ((float)log(f_mpeg2_qp) / (float)log((float)2) - 3)) + 4;
        convert_float_to_fix(f_hevce_qp, &s_hevce_qp);

        /*rounf off to nearest integer*/
        s_temp.sm = 1;
        s_temp.e = 1;
        add32_var_q(s_hevce_qp, s_temp, &s_hevce_qp);
        number_t_to_word32(s_hevce_qp, &i4_hevce_qp);
    }
    i4_hevce_qp -= ((u1_bit_depth - 8) * 6);

    return i4_hevce_qp;
}

/*#######################################################*/
/******* END OF QSCALE CONVERSION FUNCTIONS *************/
/*######################################################*/

/*###############################################*/
/******* START OF SET,GET FUNCTIONS *************/
/*#############################################*/

/**
******************************************************************************
*
*  @brief Convert pic type to rc pic type
*
*  @par   Description
*
*
*  @param[in]      pic_type
*  Pic type
*
*  @return      rc_pic_type
*
******************************************************************************
*/
picture_type_e ihevce_rc_conv_pic_type(
    IV_PICTURE_CODING_TYPE_T pic_type,
    WORD32 i4_field_pic,
    WORD32 i4_temporal_layer_id,
    WORD32 i4_is_bottom_field,
    WORD32 i4_top_field_first)
{
    picture_type_e rc_pic_type = (picture_type_e)pic_type;
    /*interlaced pictype are not supported*/
    if(pic_type > 9 && i4_temporal_layer_id > 3) /**/
    {
        DBG_PRINTF("unsupported picture type or temporal id\n");
        exit(0);
    }

    if(i4_field_pic == 0) /*Progressive Source*/
    {
        if(pic_type == IV_IDR_FRAME)
        {
            rc_pic_type = I_PIC;
        }
        else
        {
            rc_pic_type = (picture_type_e)pic_type;

            /*return different picture type based on temporal layer*/
            if(i4_temporal_layer_id > 1)
            {
                rc_pic_type = (picture_type_e)(pic_type + (i4_temporal_layer_id - 1));
            }
        }
    }

    else if(i4_field_pic == 1)
    {
        if(pic_type == IV_IDR_FRAME || pic_type == IV_I_FRAME)
        {
            rc_pic_type = I_PIC;
        }

        else if(i4_top_field_first == 1)
        {
            rc_pic_type = (picture_type_e)pic_type;

            if(i4_temporal_layer_id <= 1)

            {
                if(i4_is_bottom_field == 1)
                    rc_pic_type = (picture_type_e)(pic_type + 4);
            }
            /*return different picture type based on temporal layer*/
            if(i4_temporal_layer_id > 1)
            {
                if(i4_is_bottom_field == 0)
                    rc_pic_type = (picture_type_e)(pic_type + (i4_temporal_layer_id - 1));
                else
                    rc_pic_type = (picture_type_e)(
                        pic_type + (i4_temporal_layer_id - 1) +
                        4); /*Offset of 4 for the bottomfield*/
            }
        }
        else if(i4_top_field_first == 0)
        {
            rc_pic_type = (picture_type_e)pic_type;

            if(i4_temporal_layer_id <= 1)
            {
                if(i4_is_bottom_field == 1)
                    rc_pic_type = (picture_type_e)(pic_type + 4);
            }
            /*return different picture type based on temporal layer*/
            if(i4_temporal_layer_id > 1)
            {
                if(i4_is_bottom_field == 0)
                    rc_pic_type = (picture_type_e)(pic_type + (i4_temporal_layer_id - 1));
                else
                    rc_pic_type = (picture_type_e)(
                        pic_type + (i4_temporal_layer_id - 1) + 4); /*Offset of 4 for the topfield*/
            }
        }
    }

    return rc_pic_type;
}

/**
******************************************************************************
*
*  @brief function to update current frame intra cost
*
*  @par   Description
*  @param[inout] ps_rc_ctxt
*  @param[in]    i8_cur_frm_intra_cost
******************************************************************************
*/
void ihevce_rc_update_cur_frm_intra_satd(
    void *pv_ctxt, LWORD64 i8_cur_frm_intra_cost, WORD32 i4_enc_frm_id)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id] = i8_cur_frm_intra_cost;
}
/**
******************************************************************************
*
*  @brief function to return scene type
*
*  @par   Description
*  @param[inout] ps_rc_lap_out
*  @return       i4_rc_scene_type
******************************************************************************
*/
/* Functions dependent on lap input*/
WORD32 ihevce_rc_lap_get_scene_type(rc_lap_out_params_t *ps_rc_lap_out)
{
    return (WORD32)ps_rc_lap_out->i4_rc_scene_type;
}

/**
******************************************************************************
*
*  @name  ihevce_rc_get_pic_param
*
*  @par   Description
*
*  @param[in]   rc_pic_type
*
*  @return      void
*
******************************************************************************
*/
static void ihevce_rc_get_pic_param(
    picture_type_e rc_pic_type, WORD32 *pi4_tem_lyr, WORD32 *pi4_is_bottom_field)
{
    /*bottom field determination*/
    if(rc_pic_type >= P1_PIC)
        *pi4_is_bottom_field = 1;
    else
        *pi4_is_bottom_field = 0;

    /*temporal lyr id determination*/
    if(rc_pic_type == I_PIC || rc_pic_type == P_PIC || rc_pic_type == P1_PIC)
    {
        *pi4_tem_lyr = 0;
    }
    else if(rc_pic_type == B_PIC || rc_pic_type == BB_PIC)
    {
        *pi4_tem_lyr = 1;
    }
    else if(rc_pic_type == B1_PIC || rc_pic_type == B11_PIC)
    {
        *pi4_tem_lyr = 2;
    }
    else if(rc_pic_type == B2_PIC || rc_pic_type == B22_PIC)
    {
        *pi4_tem_lyr = 3;
    }
    else
    {
        ASSERT(0);
    }
}
/**
******************************************************************************
*
*  @name  ihevce_get_offline_index
*
*  @par   Description
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*
*  @return      index
*
******************************************************************************
*/
static WORD32 ihevce_get_offline_index(rc_context_t *ps_rc_ctxt, WORD32 i4_num_pels_in_frame)
{
    WORD32 i4_rc_quality_preset = ps_rc_ctxt->i4_quality_preset;
    WORD32 base = 1;
    if(i4_num_pels_in_frame > 5000000) /*ultra HD*/
    {
        base = 0;
    }
    else if(i4_num_pels_in_frame > 1500000) /*Full HD*/
    {
        base = 5;
    }
    else if(i4_num_pels_in_frame > 600000) /*720p*/
    {
        base = 10;
    }
    else /*SD*/
    {
        base = 15;
    }
    /*based on preset choose coeff*/
    if(i4_rc_quality_preset == IHEVCE_QUALITY_P0) /*Pristine quality*/
    {
        return base;
    }
    else if(i4_rc_quality_preset == IHEVCE_QUALITY_P2) /*High quality*/
    {
        return base + 1;
    }
    else if(
        (i4_rc_quality_preset == IHEVCE_QUALITY_P5) ||
        (i4_rc_quality_preset == IHEVCE_QUALITY_P6) ||
        (i4_rc_quality_preset == IHEVCE_QUALITY_P7)) /*Extreme speed */
    {
        return base + 4;
    }
    else if(i4_rc_quality_preset == IHEVCE_QUALITY_P4) /*High speed */
    {
        return base + 3;
    }
    else if(i4_rc_quality_preset == IHEVCE_QUALITY_P3) /*default assume Medium speed*/
    {
        return base + 2;
    }
    else
    {
        ASSERT(0);
    }
    return base + 2;
}

/**
******************************************************************************
*
*  @name  ihevce_get_frame_lambda_modifier
*
*  @par   Description
*
*  @param[in]   pic_type
*               i4_rc_temporal_lyr_id
*  @param[in]   i4_first_field
*  @param[in]   i4_rc_is_ref_pic
*  @return      lambda_modifier
*
******************************************************************************
*/
static double ihevce_get_frame_lambda_modifier(
    WORD8 pic_type,
    WORD32 i4_rc_temporal_lyr_id,
    WORD32 i4_first_field,
    WORD32 i4_rc_is_ref_pic,
    WORD32 i4_num_b_frms)
{
    double lambda_modifier;
    WORD32 num_b_frms = i4_num_b_frms, first_field = i4_first_field;

    if(I_PIC == pic_type)
    {
        double temporal_correction_islice = 1.0 - 0.05 * num_b_frms;
        temporal_correction_islice = MAX(0.5, temporal_correction_islice);

        lambda_modifier = 0.57 * temporal_correction_islice;
    }
    else if(P_PIC == pic_type)
    {
        if(first_field)
            lambda_modifier = 0.442;  //0.442*0.8;
        else
            lambda_modifier = 0.442;

        //lambda_modifier *= pow(2.00,(double)(1.00/3.00));
    }
    else
    {
        /* BSLICE */
        if(1 == i4_rc_is_ref_pic)
        {
            lambda_modifier = 0.3536;
        }
        else if(2 == i4_rc_is_ref_pic)
        {
            lambda_modifier = 0.45;
        }
        else
        {
            lambda_modifier = 0.68;
        }

        /* TODO: Disable lambda modification for interlace encode to match HM runs */
        //if(0 == ps_enc_ctxt->s_runtime_src_prms.i4_field_pic)
        {
            /* modify b lambda further based on temporal id */
            if(i4_rc_temporal_lyr_id)
            {
                lambda_modifier *= 3.00;
            }
        }
        //lambda_modifier *= pow(2.00,(double)((1.00/3.00) * (i4_rc_temporal_lyr_id + 1)));
    }

    /* modify the base lambda according to lambda modifier */
    lambda_modifier = sqrt(lambda_modifier);
    return lambda_modifier;
}

/*!
******************************************************************************
* \if Function name : get_avg_bitrate_bufsize
*
* \brief
*
* \param[in] *pv_ctxt -> rc context
*
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void get_avg_bitrate_bufsize(void *pv_ctxt, LWORD64 *pi8_bitrate, LWORD64 *pi8_ebf)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    *pi8_bitrate = rc_get_bit_rate(ps_rc_ctxt->rc_hdl);
    *pi8_ebf = rc_get_vbv_buf_size(ps_rc_ctxt->rc_hdl);
}

/**
******************************************************************************
*
*  @name  ihevce_get_dbf_buffer_size
*
*  @par   Description
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*
*  @return      qp
*
******************************************************************************
*/
void ihevce_get_dbf_buffer_size(
    void *pv_rc_ctxt, UWORD32 *pi4_buffer_size, UWORD32 *pi4_dbf, UWORD32 *pi4_bit_rate)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;

    pi4_buffer_size[0] = (WORD32)ps_rc_ctxt->s_vbv_compliance.f_buffer_size;
    pi4_dbf[0] = (WORD32)(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level);
    ASSERT(
        ps_rc_ctxt->s_vbv_compliance.f_buffer_size >=
        ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level);

    pi4_bit_rate[0] = (WORD32)ps_rc_ctxt->s_vbv_compliance.f_bit_rate;
}

/*!
******************************************************************************
* \if Function name : ihevce_set_L0_scd_qp
*
* \brief
*
* \param[in] *pv_ctxt -> rc context
*
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_set_L0_scd_qp(void *pv_rc_ctxt, WORD32 i4_scd_qp)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;

    ps_rc_ctxt->i4_L0_frame_qp = i4_scd_qp;
}

/**
******************************************************************************
*
*  @name  rc_get_buffer_level_unclip
*
*  @par   Description
*
*  @param[in]      pv_rc_ctxt
*
*
*  @return      void
*
******************************************************************************
*/
float rc_get_buffer_level_unclip(void *pv_rc_ctxt)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    return (ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip);
}

/**
******************************************************************************
*
*  @brief Clip QP based on min and max frame qp
*
*  @par   Description
*
*  @param[inout]   ps_rc_ctxt
*  pointer to rc context
*
*  @param[in]      rc_pic_type
*  Pic type
*
*  @return      i4_hevc_frame_qp
*
******************************************************************************
*/
static WORD32 ihevce_clip_min_max_qp(
    rc_context_t *ps_rc_ctxt,
    WORD32 i4_hevc_frame_qp,
    picture_type_e rc_pic_type,
    WORD32 i4_rc_temporal_lyr_id)
{
    ASSERT(i4_rc_temporal_lyr_id >= 0);
    /**clip to min qp which is user configurable*/
    if(rc_pic_type == I_PIC && i4_hevc_frame_qp < ps_rc_ctxt->i4_min_frame_qp)
    {
        i4_hevc_frame_qp = ps_rc_ctxt->i4_min_frame_qp;
    }
    else if(rc_pic_type == P_PIC && i4_hevc_frame_qp < (ps_rc_ctxt->i4_min_frame_qp + 1))
    {
        i4_hevc_frame_qp = ps_rc_ctxt->i4_min_frame_qp + 1;
    }
    else if(i4_hevc_frame_qp < (ps_rc_ctxt->i4_min_frame_qp + i4_rc_temporal_lyr_id + 1))
    {
        /** For B frame max qp is set based on temporal reference*/
        i4_hevc_frame_qp = ps_rc_ctxt->i4_min_frame_qp + i4_rc_temporal_lyr_id + 1;
    }
    /* clip the Qp to MAX QP */
    if(i4_hevc_frame_qp < ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qp)
    {
        i4_hevc_frame_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qp;
    }
    /**clip to max qp based on pic type*/
    if(rc_pic_type == I_PIC && i4_hevc_frame_qp > ps_rc_ctxt->i4_max_frame_qp)
    {
        i4_hevc_frame_qp = ps_rc_ctxt->i4_max_frame_qp;
    }
    else if(rc_pic_type == P_PIC && i4_hevc_frame_qp > (ps_rc_ctxt->i4_max_frame_qp + 1))
    {
        i4_hevc_frame_qp = ps_rc_ctxt->i4_max_frame_qp + 1;
    }
    else if(i4_hevc_frame_qp > (ps_rc_ctxt->i4_max_frame_qp + i4_rc_temporal_lyr_id + 1))
    {
        /** For B frame max qp is set based on temporal reference*/
        i4_hevc_frame_qp = ps_rc_ctxt->i4_max_frame_qp + i4_rc_temporal_lyr_id + 1;
    }
    /* clip the Qp to MAX QP */
    if(i4_hevc_frame_qp > ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp)
    {
        i4_hevc_frame_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp;
    }
    return i4_hevc_frame_qp;
}

/*#############################################*/
/******* END OF SET,GET FUNCTIONS *************/
/*###########################################*/

/*#################################################*/
/******* START OF RC UPDATE FUNCTIONS **************/
/*#################################################*/

/**
******************************************************************************
*
*  @brief updates the picture level information like bits consumed and
*
*  @par   Description
*
*  @param[inout]   ps_mem_tab
*  pointer to memory descriptors table
*
*  @param[in]      ps_init_prms
*  Create time static parameters
*
*  @return      void
*
******************************************************************************
*/

void ihevce_rc_update_pic_info(
    void *pv_ctxt,
    UWORD32 u4_total_bits_consumed,
    UWORD32 u4_total_header_bits,
    UWORD32 u4_frame_sad,
    UWORD32 u4_frame_intra_sad,
    IV_PICTURE_CODING_TYPE_T pic_type,
    WORD32 i4_avg_frame_hevc_qp,
    WORD32 i4_suppress_bpic_update,
    WORD32 *pi4_qp_normalized_8x8_cu_sum,
    WORD32 *pi4_8x8_cu_sum,
    LWORD64 *pi8_sad_by_qscale,
    ihevce_lap_output_params_t *ps_lap_out,
    rc_lap_out_params_t *ps_rc_lap_out,
    WORD32 i4_buf_id,
    UWORD32 u4_open_loop_intra_sad,
    LWORD64 i8_total_ssd_frame,
    WORD32 i4_enc_frm_id)
{
    LWORD64 a_mb_type_sad[2];
    WORD32 a_mb_type_tex_bits[2];
    /*dummy variables not used*/
    WORD32 a_mb_in_type[2] = { 0, 0 };
    LWORD64 a_mb_type_qp_q6[2] = { 0, 0 };
    /*qp accumulation at */
    WORD32 i4_avg_activity = 250;  //hardcoding to usual value
    WORD32 i4_intra_cost, i4_avg_frame_qp_q6, i;
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    WORD32 i4_frame_complexity, i4_bits_to_be_stuffed = 0, i4_is_last_frm_period = 0;
    picture_type_e rc_pic_type = ihevce_rc_conv_pic_type(
        pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_rc_lap_out->i4_rc_temporal_lyr_id,
        ps_rc_lap_out->i4_is_bottom_field,
        ps_rc_ctxt->i4_top_field_first);
    frame_info_t s_frame_info;
    WORD32 i4_ctr = -1, i4_i, i4_j;
    WORD32 i4_scene_num = ps_rc_lap_out->u4_rc_scene_num % MAX_SCENE_NUM;

    /*update bit consumption. used only in rdopt*/
    //ASSERT(ps_rc_ctxt->ai4_rdopt_bit_consumption_estimate[ps_rc_ctxt->i4_rdopt_bit_count] == -1);
    //ASSERT(i4_buf_id>=0);
    ps_rc_ctxt->ai4_rdopt_bit_consumption_estimate[ps_rc_ctxt->i4_rdopt_bit_count] =
        u4_total_bits_consumed;
    ps_rc_ctxt->ai4_rdopt_bit_consumption_buf_id[ps_rc_ctxt->i4_rdopt_bit_count] = i4_buf_id;
    ps_rc_ctxt->i4_rdopt_bit_count =
        (ps_rc_ctxt->i4_rdopt_bit_count + 1) % NUM_BUF_RDOPT_ENT_CORRECT;

    {
        LWORD64 i8_texture_bits = u4_total_bits_consumed - u4_total_header_bits;
        ps_rc_lap_out->i4_use_offline_model_2pass = 0;

        /*flag to guide whether 2nd pass can use offline model or not*/
        if((abs(ps_rc_lap_out->i4_orig_rc_qp - i4_avg_frame_hevc_qp) < 2) &&
           (i8_texture_bits <= (ps_rc_lap_out->i8_est_text_bits * 2.0f)) &&
           (i8_texture_bits >= (ps_rc_lap_out->i8_est_text_bits * 0.5f)))

        {
            ps_rc_lap_out->i4_use_offline_model_2pass = 1;
        }
    }
    /*Counter of number of bit alloction periods*/
    if(rc_pic_type == I_PIC)
        ps_rc_ctxt
            ->i8_num_bit_alloc_period++;  //Currently only I frame periods are considerd as bit allocation period (Ignoring non- I scd and complexity reset flag
    /*initialze frame info*/
    init_frame_info(&s_frame_info);
    s_frame_info.i4_rc_hevc_qp = i4_avg_frame_hevc_qp;
    s_frame_info.i4_num_entries++;
    s_frame_info.i8_L1_me_sad = ps_rc_lap_out->i8_raw_l1_coarse_me_sad;
    s_frame_info.i8_L1_ipe_raw_sad = ps_rc_lap_out->i8_raw_pre_intra_sad;
    s_frame_info.i4_num_entries++;
    s_frame_info.i4_num_entries++;
    s_frame_info.i8_L0_open_cost = (LWORD64)u4_open_loop_intra_sad;
    s_frame_info.i4_num_entries++;

    if(rc_pic_type == I_PIC)
        s_frame_info.i8_L1_me_or_ipe_raw_sad = ps_rc_lap_out->i8_raw_pre_intra_sad;
    else
        s_frame_info.i8_L1_me_or_ipe_raw_sad = ps_rc_lap_out->i8_raw_l1_coarse_me_sad;
    s_frame_info.i4_num_entries++;
    s_frame_info.i4_poc = ps_rc_lap_out->i4_rc_poc;
    s_frame_info.i4_num_entries++;
    s_frame_info.i4_scene_type = ps_rc_lap_out->i4_rc_scene_type;
    s_frame_info.i4_num_entries++;
    s_frame_info.i4_non_i_scd = ps_rc_lap_out->i4_is_non_I_scd || ps_rc_lap_out->i4_is_I_only_scd;
    s_frame_info.i4_num_entries++;
    s_frame_info.i8_cl_sad = u4_frame_sad;
    s_frame_info.i4_num_entries++;
    s_frame_info.i8_header_bits = u4_total_header_bits;
    s_frame_info.i4_num_entries++;
    s_frame_info.i8_tex_bits = u4_total_bits_consumed - u4_total_header_bits;
    s_frame_info.i4_num_entries++;
    s_frame_info.e_pic_type = rc_pic_type;
    s_frame_info.i4_num_entries++;
    s_frame_info.i8_est_texture_bits = ps_rc_lap_out->i8_est_text_bits;
    s_frame_info.i4_num_entries++;
    s_frame_info.i4_lap_complexity_q7 = ps_rc_ctxt->ai4_lap_complexity_q7[i4_enc_frm_id];
    s_frame_info.i4_num_entries++;
    s_frame_info.i4_lap_f_sim = ps_rc_ctxt->ai4_lap_f_sim[i4_enc_frm_id];
    s_frame_info.i4_num_entries++;
    s_frame_info.i8_frame_acc_coarse_me_cost = ps_rc_lap_out->i8_frame_acc_coarse_me_cost;
    s_frame_info.i4_num_entries++;
    s_frame_info.i_to_avg_bit_ratio = ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id];
    s_frame_info.i4_num_entries++;
    s_frame_info.i4_num_scd_in_lap_window = ps_rc_ctxt->ai4_num_scd_in_lap_window[i4_enc_frm_id];
    s_frame_info.i4_num_entries++;
    s_frame_info.i4_num_frames_b4_scd = ps_rc_ctxt->ai4_num_frames_b4_scd[i4_enc_frm_id];
    s_frame_info.i4_num_entries++;
    s_frame_info.i8_num_bit_alloc_period = ps_rc_ctxt->i8_num_bit_alloc_period;
    s_frame_info.i4_num_entries++;
    s_frame_info.i1_is_complexity_based_bits_reset =
        (WORD8)ps_rc_lap_out->i4_is_cmplx_change_reset_bits;
    s_frame_info.i4_num_entries++;
    /*For the complexity based movement in 2nd pass*/
    memmove(
        (void *)s_frame_info.af_sum_weigh,
        ps_rc_lap_out->ps_frame_info->af_sum_weigh,
        sizeof(float) * MAX_PIC_TYPE * 3);
    s_frame_info.i4_num_entries++;

    /*store frame qp to clip qp accordingly*/
    if(ps_rc_lap_out->i4_is_rc_model_needs_to_be_updated)
    {
        ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type] = i4_avg_frame_hevc_qp;
    }

    for(i4_i = 0; i4_i < MAX_NON_REF_B_PICS_IN_QUEUE_SGI; i4_i++)
    {
        if(ps_rc_lap_out->u4_rc_scene_num == ps_rc_ctxt->au4_prev_scene_num_multi_scene[i4_i])
        {
            i4_ctr = i4_i;
            break;
        }
    }
    if(-1 == i4_ctr)
    {
        ps_rc_ctxt->i4_prev_qp_ctr++;
        ps_rc_ctxt->i4_prev_qp_ctr = ps_rc_ctxt->i4_prev_qp_ctr % MAX_NON_REF_B_PICS_IN_QUEUE_SGI;
        i4_ctr = ps_rc_ctxt->i4_prev_qp_ctr;
        ps_rc_ctxt->au4_prev_scene_num_multi_scene[i4_ctr] = ps_rc_lap_out->u4_rc_scene_num;
        for(i4_j = 0; i4_j < MAX_PIC_TYPE; i4_j++)
        {
            ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[i4_ctr][i4_j] = 0;
        }
    }

    {
        ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[i4_ctr][rc_pic_type] =
            i4_avg_frame_hevc_qp;
    }
    if(i4_scene_num < HALF_MAX_SCENE_ARRAY_QP)
    {
        WORD32 i4_i;
        ps_rc_ctxt->ai4_scene_numbers[i4_scene_num + HALF_MAX_SCENE_ARRAY_QP] = 0;
        for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
            ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num + HALF_MAX_SCENE_ARRAY_QP][i4_i] =
                INIT_HEVCE_QP_RC;
    }
    else
    {
        WORD32 i4_i;
        ps_rc_ctxt->ai4_scene_numbers[i4_scene_num - HALF_MAX_SCENE_ARRAY_QP] = 0;
        for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
            ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num - HALF_MAX_SCENE_ARRAY_QP][i4_i] =
                INIT_HEVCE_QP_RC;
    }

    /*update will have HEVC qp, convert it back to mpeg2 range qp for all internal calculations of RC*/

    i4_avg_frame_qp_q6 = ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale_q_factor
                             [i4_avg_frame_hevc_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset];

    if(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME)
    {
        /*TODO : Take care of precision of a_mb_type_sad*/
        a_mb_type_sad[0] =
            (((pi8_sad_by_qscale[1] * i4_avg_frame_qp_q6) +
              (((LWORD64)1) << (SAD_BY_QSCALE_Q + QSCALE_Q_FAC - 1))) >>
             (SAD_BY_QSCALE_Q + QSCALE_Q_FAC));  //u4_frame_sad;

        a_mb_type_sad[1] =
            (((pi8_sad_by_qscale[0] * i4_avg_frame_qp_q6) +
              (((LWORD64)1) << (SAD_BY_QSCALE_Q + QSCALE_Q_FAC - 1))) >>
             (SAD_BY_QSCALE_Q + QSCALE_Q_FAC));
        a_mb_type_tex_bits[0] =
            u4_total_bits_consumed - u4_total_header_bits;  //(u4_total_bits_consumed >> 3);
        a_mb_type_tex_bits[1] = 0;
        a_mb_in_type[0] = (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 8;
        a_mb_in_type[1] = 0;
    }
    else
    {
        /*TODO : Take care of precision of a_mb_type_sad*/
        a_mb_type_sad[1] =
            (((pi8_sad_by_qscale[0] * i4_avg_frame_qp_q6) +
              (((LWORD64)1) << (SAD_BY_QSCALE_Q + QSCALE_Q_FAC - 1))) >>
             (SAD_BY_QSCALE_Q + QSCALE_Q_FAC));

        a_mb_type_tex_bits[0] =
            u4_total_bits_consumed - u4_total_header_bits;  //(u4_total_bits_consumed >> 3);
        a_mb_type_sad[0] =
            (((pi8_sad_by_qscale[1] * i4_avg_frame_qp_q6) +
              (((LWORD64)1) << (SAD_BY_QSCALE_Q + QSCALE_Q_FAC - 1))) >>
             (SAD_BY_QSCALE_Q + QSCALE_Q_FAC));  //u4_frame_sad;
        a_mb_type_tex_bits[1] =
            u4_total_bits_consumed - u4_total_header_bits;  //(u4_total_bits_consumed >> 3);
        a_mb_type_tex_bits[0] = 0;
        a_mb_in_type[1] = (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 8;
        a_mb_in_type[0] = 0;
    }
    ASSERT(a_mb_type_sad[0] >= 0);
    ASSERT(a_mb_type_sad[1] >= 0);
    /*THis calclates sum of Qps of all MBs as per the corresponding mb type*/
    /*THis is different from a_mb_in_type,a_mb_type_sad and a_mb_type_tex_bits*/
    a_mb_type_qp_q6[0] = ((LWORD64)i4_avg_frame_qp_q6) * a_mb_in_type[0];
    a_mb_type_qp_q6[1] = ((LWORD64)i4_avg_frame_qp_q6) * a_mb_in_type[1];
    {
        WORD32 i4_avg_qp_q6_without_offset = 0, i4_hevc_qp_rc = i4_avg_frame_hevc_qp;
        WORD32 i4_rc_pic_type_rc_for_offset = rc_pic_type;
        if(i4_rc_pic_type_rc_for_offset > B2_PIC)
            i4_rc_pic_type_rc_for_offset = i4_rc_pic_type_rc_for_offset - B2_PIC;
        i4_hevc_qp_rc = i4_hevc_qp_rc - ps_rc_lap_out->ai4_offsets[i4_rc_pic_type_rc_for_offset] +
                        ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset;

        i4_hevc_qp_rc =
            CLIP3(i4_hevc_qp_rc, 1, MAX_HEVC_QP + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset);
        i4_avg_qp_q6_without_offset =
            ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale_q_factor[i4_hevc_qp_rc];

        /*Store the HBD qscale with and without accounting for offset*/
        s_frame_info.f_hbd_q_scale_without_offset =
            (float)i4_avg_qp_q6_without_offset / (1 << QSCALE_Q_FAC);
        s_frame_info.f_hbd_q_scale = (float)i4_avg_frame_qp_q6 / (1 << QSCALE_Q_FAC);
        s_frame_info.i4_num_entries++;
        s_frame_info.i4_num_entries++;

        /*Store the 8 bit qscale with and without accounting for offset*/
        /*Can be useful for pre-enc stage*/
        if(ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset != 0)
        {
            s_frame_info.f_8bit_q_scale_without_offset =
                s_frame_info.f_hbd_q_scale_without_offset / (1 << (ps_rc_ctxt->u1_bit_depth - 8));
            s_frame_info.f_8bit_q_scale =
                s_frame_info.f_hbd_q_scale / (1 << (ps_rc_ctxt->u1_bit_depth - 8));
        }
        else
        {
            s_frame_info.f_8bit_q_scale_without_offset = s_frame_info.f_hbd_q_scale_without_offset;
            s_frame_info.f_8bit_q_scale = s_frame_info.f_hbd_q_scale;
        }
        s_frame_info.i4_num_entries++;
        s_frame_info.i4_num_entries++;
    }

    /*making intra cost same as ssd as of now*/
    i4_intra_cost = u4_frame_intra_sad;

    /* Handling bits stuffing and skips */
    {
        WORD32 i4_num_bits_to_prevent_vbv_underflow;
        vbv_buf_status_e vbv_buffer_status;
        vbv_buffer_status = get_buffer_status(
            ps_rc_ctxt->rc_hdl,
            u4_total_bits_consumed,
            rc_pic_type,  //the picture type convention is different in buffer handling
            &i4_num_bits_to_prevent_vbv_underflow);

        if(vbv_buffer_status == VBV_UNDERFLOW)
        {
        }
        if(vbv_buffer_status == VBV_OVERFLOW)
        {
            i4_bits_to_be_stuffed =
                get_bits_to_stuff(ps_rc_ctxt->rc_hdl, u4_total_bits_consumed, rc_pic_type);
            //i4_bits_to_be_stuffed = 0;/*STORAGE_RC*/
        }
    }
    {
        WORD32 ai4_sad[MAX_PIC_TYPE], i4_valid_sad_entry = 0;
        UWORD32 u4_avg_sad = 0;

        /*calculate frame complexity. Given same content frame complexity should not vary across I,P and Bpic. Hence frame complexity is calculated
        based on average of all pic types SAD*/
        if(rc_pic_type == I_PIC)
        {
            ai4_sad[I_PIC] = u4_frame_intra_sad;
        }
        else
        {
            /*call to get previous I-PIC sad*/
            rc_get_sad(ps_rc_ctxt->rc_hdl, &ai4_sad[0]);
        }

        /*since intra sad is not available for every frame use previous I pic intra frame SAD*/
        rc_put_sad(ps_rc_ctxt->rc_hdl, ai4_sad[I_PIC], u4_frame_sad, rc_pic_type);
        rc_get_sad(ps_rc_ctxt->rc_hdl, &ai4_sad[0]);
        /*for first few frame valid SAD is not available. This will make sure invalid data is not used*/
        if(ps_rc_ctxt->i4_field_pic == 0)
        {
            for(i = 0; i < ps_rc_ctxt->i4_num_active_pic_type; i++)
            {
                if(ai4_sad[i] >= 0)
                {
                    u4_avg_sad += ai4_sad[i];
                    i4_valid_sad_entry++;
                }
            }
        }
        else /*for field case*/
        {
            if(ai4_sad[0] >= 0)
            {
                u4_avg_sad += ai4_sad[0];
                i4_valid_sad_entry++;
            }

            for(i = 1; i < ps_rc_ctxt->i4_num_active_pic_type; i++)
            {
                if(ai4_sad[i] >= 0)
                {
                    u4_avg_sad += ai4_sad[i];
                    i4_valid_sad_entry++;
                }

                if(ai4_sad[i + FIELD_OFFSET] >= 0)
                {
                    u4_avg_sad += ai4_sad[i + FIELD_OFFSET];
                    i4_valid_sad_entry++;
                }
            }
        }

        if(i4_valid_sad_entry > 0)
        {
            i4_frame_complexity =
                (u4_avg_sad) /
                (i4_valid_sad_entry * (ps_rc_ctxt->i4_frame_width * ps_rc_ctxt->i4_frame_height));
        }
        else
        {
            i4_frame_complexity = 1;
        }
    }
    ASSERT(i4_frame_complexity >= 0);
    /*I_model only reset In case of fade-in and fade-out*/
    if(ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id])
    {
        ASSERT(rc_pic_type == I_PIC);
        rc_reset_pic_model(ps_rc_ctxt->rc_hdl, I_PIC);
        ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id] = 0;
    }

    /*check if next picture is I frame, both scene cuts and I pictures are treated as end of period*/
    {
        if(ps_rc_lap_out->i4_rc_pic_type != -1 && ps_rc_lap_out->i4_rc_scene_type != -1)
        {
            if(ps_rc_ctxt->u4_intra_frame_interval != 1)
            {
                /*TBD: For second pass this should be only criteria, While merging to latest verison make sure non - I SCD is not considered as one of the condition*/
                i4_is_last_frm_period = (WORD32)(
                    ps_rc_lap_out->i4_next_pic_type == IV_IDR_FRAME ||
                    ps_rc_lap_out->i4_next_pic_type == IV_I_FRAME);
            }
            else
            {
                i4_is_last_frm_period =
                    (WORD32)(ps_rc_lap_out->i4_next_scene_type == SCENE_TYPE_SCENE_CUT);
            }
        }

        /*In two pass only I frame ending should be considered end of period, otherwise complexity changes should be allowed to reset model in CBR and VBR modes*/
        if(ps_rc_ctxt->i4_rc_pass != 2)
            i4_is_last_frm_period = i4_is_last_frm_period ||
                                    ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i4_enc_frm_id];
    }

#if 1  //FRAME_PARALLEL_LVL //ELP_RC
    ps_rc_ctxt->i4_est_text_bits_ctr_update_qp++;
    ps_rc_ctxt->i4_est_text_bits_ctr_update_qp =
        (ps_rc_ctxt->i4_est_text_bits_ctr_update_qp % (ps_rc_ctxt->i4_num_frame_parallel));
#endif

    update_frame_level_info(
        ps_rc_ctxt->rc_hdl,
        rc_pic_type,
        a_mb_type_sad,
        u4_total_bits_consumed, /*total bits consumed by frame*/
        u4_total_header_bits,
        a_mb_type_tex_bits,
        a_mb_type_qp_q6, /*sum of qp of all mb in frame, since no ctb level modulation*/
        a_mb_in_type,
        i4_avg_activity,
        ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id], /*currenlty SCD is not enabled*/
        0, /*not a pre encode skip*/
        i4_intra_cost,
        0,
        ps_rc_lap_out
            ->i4_ignore_for_rc_update, /*HEVC_hierarchy: do not supress update for non-ref B pic*/
        i4_bits_to_be_stuffed,
        (ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] ||
         ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] ||
         ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id]),
        ps_rc_ctxt->ai4_lap_complexity_q7[i4_enc_frm_id],
        i4_is_last_frm_period,
        ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i4_enc_frm_id],
        &s_frame_info,
        ps_rc_lap_out->i4_is_rc_model_needs_to_be_updated,
        ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset,
        i4_scene_num,
        ps_rc_ctxt->ai4_scene_numbers[i4_scene_num],
        ps_rc_ctxt->i4_est_text_bits_ctr_update_qp);
    /** reset flags valid for only one frame*/
    ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id] = 0;
    ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] = 0;
    ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] = 0;
    ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id] = 0;
    ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i4_enc_frm_id] = 0;

    ps_rc_ctxt->i4_is_first_frame_encoded = 1;

    /** update the scene num for current frame*/
    ps_rc_ctxt->au4_scene_num_temp_id[ps_rc_lap_out->i4_rc_temporal_lyr_id] =
        ps_rc_lap_out->u4_rc_scene_num;

    if(ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id])
    {
        /*reset pre-enc SAD whenever SCD is detected so that it does not detect scene cut for other pictures*/
        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[i] = -1;
        }
    }

    /*remember i frame's cost metric to scale SAD of next of I frame*/
    if(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME)
    {
        ps_rc_ctxt->i8_prev_i_frm_cost = ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id];
        ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] =
            ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id];
    }
    /*for other picture types update hme cost*/
    else
    {
        ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] =
            ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i4_enc_frm_id];
    }
}
/*!
******************************************************************************
* \if Function name : ihevce_rc_interface_update \endif
*
* \brief
*   Updating rate control interface parameters after the query call.
*
* \param[in] Rate control interface context,
*            Picture Type
*            Lap out structure pointer
*
*
* \return
*    None
*
* \author Ittiam
*  Ittiam
*
*****************************************************************************
*/
void ihevce_rc_interface_update(
    void *pv_ctxt,
    IV_PICTURE_CODING_TYPE_T pic_type,
    rc_lap_out_params_t *ps_rc_lap_out,
    WORD32 i4_avg_frame_hevc_qp,
    WORD32 i4_enc_frm_id)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    picture_type_e rc_pic_type = ihevce_rc_conv_pic_type(
        pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_rc_lap_out->i4_rc_temporal_lyr_id,
        ps_rc_lap_out->i4_is_bottom_field,
        ps_rc_ctxt->i4_top_field_first);
    WORD32 i;
    WORD32 i4_avg_frame_qp_q6, i4_ctr = -1, i4_i, i4_j;
    WORD32 i4_scene_num = ps_rc_lap_out->u4_rc_scene_num % MAX_SCENE_NUM;

    /*store frame qp to clip qp accordingly*/
    if(ps_rc_lap_out->i4_is_rc_model_needs_to_be_updated)
    {
        WORD32 i4_i, i4_temp_i_qp, i4_temp_qp;
        ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type] = i4_avg_frame_hevc_qp;
        ps_rc_ctxt->ai4_scene_numbers[i4_scene_num]++;

        if(rc_pic_type < P1_PIC)
            i4_temp_i_qp = i4_avg_frame_hevc_qp - rc_pic_type;
        else
            i4_temp_i_qp = i4_avg_frame_hevc_qp - rc_pic_type + 4;

        i4_temp_i_qp = ihevce_clip_min_max_qp(ps_rc_ctxt, i4_temp_i_qp, I_PIC, 0);

        if(ps_rc_ctxt->ai4_scene_numbers[i4_scene_num] == 1)
        {
            for(i4_i = 0; i4_i < 5; i4_i++)
            {
                if(ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i4_i] == INIT_HEVCE_QP_RC)
                {
                    i4_temp_qp = i4_temp_i_qp + i4_i;
                    i4_temp_qp = ihevce_clip_min_max_qp(
                        ps_rc_ctxt, i4_temp_qp, (picture_type_e)i4_i, MAX(i4_i - 1, 0));
                    ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i4_i] = i4_temp_qp;

                    if(i4_i > 0)
                        ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i4_i + 4] = i4_temp_qp;
                }
            }
        }
    }

    for(i4_i = 0; i4_i < MAX_NON_REF_B_PICS_IN_QUEUE_SGI; i4_i++)
    {
        if(ps_rc_lap_out->u4_rc_scene_num == ps_rc_ctxt->au4_prev_scene_num_multi_scene[i4_i])
        {
            i4_ctr = i4_i;
            break;
        }
    }
    if(-1 == i4_ctr)
    {
        ps_rc_ctxt->i4_prev_qp_ctr++;
        ps_rc_ctxt->i4_prev_qp_ctr = ps_rc_ctxt->i4_prev_qp_ctr % MAX_NON_REF_B_PICS_IN_QUEUE_SGI;
        i4_ctr = ps_rc_ctxt->i4_prev_qp_ctr;
        ps_rc_ctxt->au4_prev_scene_num_multi_scene[i4_ctr] = ps_rc_lap_out->u4_rc_scene_num;
        for(i4_j = 0; i4_j < MAX_PIC_TYPE; i4_j++)
        {
            ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[i4_ctr][i4_j] = 0;
        }
    }

    {
        ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[i4_ctr][rc_pic_type] =
            i4_avg_frame_hevc_qp;
    }

    /*I_model only reset In case of fade-in and fade-out*/
    if(ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id])
    {
        ASSERT(rc_pic_type == I_PIC);
        rc_reset_pic_model(ps_rc_ctxt->rc_hdl, I_PIC);
        ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id] = 0;
    }

    i4_avg_frame_qp_q6 = ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale_q_factor
                             [i4_avg_frame_hevc_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset];

    update_frame_rc_get_frame_qp_info(
        ps_rc_ctxt->rc_hdl,
        rc_pic_type,
        ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id],
        (ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] ||
         ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] ||
         ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id]),
        i4_avg_frame_qp_q6,
        ps_rc_lap_out->i4_ignore_for_rc_update,
        i4_scene_num,
        ps_rc_ctxt->ai4_scene_numbers[i4_scene_num]);

    /** update the scene num for current frame*/
    ps_rc_ctxt->au4_scene_num_temp_id[ps_rc_lap_out->i4_rc_temporal_lyr_id] =
        ps_rc_lap_out->u4_rc_scene_num;

    if(ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id])
    {
        /*reset pre-enc SAD whenever SCD is detected so that it does not detect scene cut for other pictures*/
        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[i] = -1;
        }
    }

    /*remember i frame's cost metric to scale SAD of next of I frame*/
    if(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME)
    {
        ps_rc_ctxt->i8_prev_i_frm_cost = ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id];
        ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] =
            ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id];
    }
    /*for other picture types update hme cost*/
    else
    {
        ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] =
            ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i4_enc_frm_id];
    }

    ps_rc_ctxt->i4_is_first_frame_encoded = 1;
}

/****************************************************************************
Function Name : ihevce_rc_store_retrive_update_info
Description   : for storing and retrieving the data in case of the Enc Loop Parallelism.
Inputs        :
Globals       :
Processing    :
Outputs       :
Returns       :
Issues        :
Revision History:
DD MM YYYY   Author(s)       Changes (Describe the changes made)
*****************************************************************************/

void ihevce_rc_store_retrive_update_info(
    void *pv_ctxt,
    rc_bits_sad_t *ps_rc_frame_stat,
    WORD32 i4_enc_frm_id_rc,
    WORD32 bit_rate_id,
    WORD32 i4_store_retrive,
    WORD32 *pout_buf_id,
    WORD32 *pi4_rc_pic_type,
    WORD32 *pcur_qp,
    void *ps_lap_out,
    void *ps_rc_lap_out)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    if(1 == i4_store_retrive)
    {
        memcpy(
            &ps_rc_ctxt->as_rc_frame_stat_store[i4_enc_frm_id_rc][bit_rate_id],
            ps_rc_frame_stat,
            sizeof(rc_bits_sad_t));
        memcpy(&ps_rc_ctxt->out_buf_id[i4_enc_frm_id_rc][bit_rate_id], pout_buf_id, sizeof(WORD32));
        memcpy(&ps_rc_ctxt->i4_pic_type[i4_enc_frm_id_rc], pi4_rc_pic_type, sizeof(WORD32));
        memcpy(&ps_rc_ctxt->cur_qp[i4_enc_frm_id_rc][bit_rate_id], pcur_qp, sizeof(WORD32));
        memcpy(
            &ps_rc_ctxt->as_lap_out[i4_enc_frm_id_rc],
            ps_lap_out,
            sizeof(ihevce_lap_output_params_t));
        memcpy(
            &ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc],
            ps_rc_lap_out,
            sizeof(rc_lap_out_params_t));

        {
            rc_lap_out_params_t *ps_rc_lap_out_curr = (rc_lap_out_params_t *)ps_rc_lap_out;
            rc_lap_out_params_t *ps_rc_lap_out_next_encode =
                (rc_lap_out_params_t *)ps_rc_lap_out_curr->ps_rc_lap_out_next_encode;

            if(NULL != ps_rc_lap_out_next_encode)
            {
                ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_pic_type =
                    ps_rc_lap_out_next_encode->i4_rc_pic_type;
                ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_scene_type =
                    ps_rc_lap_out_next_encode->i4_rc_scene_type;
            }
            else
            {
                if(ps_rc_ctxt->u4_intra_frame_interval <= 1 ||
                   (ps_rc_lap_out_curr->i4_rc_display_num &&
                    (ps_rc_lap_out_curr->i4_rc_display_num %
                     (ps_rc_ctxt->u4_intra_frame_interval - 1)) == 0))
                {
                    ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_pic_type = IV_I_FRAME;
                }
                else
                {
                    ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_pic_type = -1;
                }
                ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_scene_type = -1;
            }
            ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].ps_rc_lap_out_next_encode = NULL;
        }
    }
    else if(2 == i4_store_retrive)
    {
        memcpy(
            ps_rc_frame_stat,
            &ps_rc_ctxt->as_rc_frame_stat_store[i4_enc_frm_id_rc][bit_rate_id],
            sizeof(rc_bits_sad_t));
        memcpy(pout_buf_id, &ps_rc_ctxt->out_buf_id[i4_enc_frm_id_rc][bit_rate_id], sizeof(WORD32));
        memcpy(pi4_rc_pic_type, &ps_rc_ctxt->i4_pic_type[i4_enc_frm_id_rc], sizeof(WORD32));
        memcpy(pcur_qp, &ps_rc_ctxt->cur_qp[i4_enc_frm_id_rc][bit_rate_id], sizeof(WORD32));
        memcpy(
            ps_lap_out,
            &ps_rc_ctxt->as_lap_out[i4_enc_frm_id_rc],
            sizeof(ihevce_lap_output_params_t));
        memcpy(
            ps_rc_lap_out,
            &ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc],
            sizeof(rc_lap_out_params_t));
    }
    else
    {
        ASSERT(0);
    }
}

/*###############################################*/
/******* END OF RC UPDATE FUNCTIONS **************/
/*###############################################*/

/*#################################################*/
/******* START OF RC UTILS FUNCTIONS **************/
/*#################################################*/

/**
******************************************************************************
*
*  @brief function to account for error correction between bits rdopt estimate
*           and actual entropy bit generation
*
*  @par   Description
*
*  @param[in]   pv_rc_ctxt
*               void pointer to rc ctxt
*  @param[in]   i4_rdopt_bits_gen_error
*               WODd32 variable with error correction between rdopt and entropy bytes gen
*
*  @return      void
*
******************************************************************************
*/

void ihevce_rc_rdopt_entropy_bit_correct(
    void *pv_rc_ctxt, WORD32 i4_cur_entropy_consumption, WORD32 i4_buf_id)
{
    rc_context_t *ps_ctxt = (rc_context_t *)pv_rc_ctxt;
    WORD32 i4_error;
    WORD32 i, count = 0;
    ASSERT(i4_buf_id >= 0);
    ps_ctxt->ai4_entropy_bit_consumption[ps_ctxt->i4_entropy_bit_count] =
        i4_cur_entropy_consumption;
    ps_ctxt->ai4_entropy_bit_consumption_buf_id[ps_ctxt->i4_entropy_bit_count] = i4_buf_id;
    ps_ctxt->i4_entropy_bit_count = (ps_ctxt->i4_entropy_bit_count + 1) % NUM_BUF_RDOPT_ENT_CORRECT;

    for(i = 0; i < NUM_BUF_RDOPT_ENT_CORRECT; i++)
    {
        if(ps_ctxt->ai4_rdopt_bit_consumption_buf_id[i] >= 0 &&
           (ps_ctxt->ai4_rdopt_bit_consumption_buf_id[i] ==
            ps_ctxt->ai4_entropy_bit_consumption_buf_id[i]))
        {
            i4_error = ps_ctxt->ai4_rdopt_bit_consumption_estimate[i] -
                       ps_ctxt->ai4_entropy_bit_consumption[i];
            //DBG_PRINTF("entropy mismatch error = %d\n",i4_error/ps_ctxt->ai4_rdopt_bit_consumption_estimate[i]);
            ps_ctxt->ai4_rdopt_bit_consumption_estimate[i] = -1;
            ps_ctxt->ai4_rdopt_bit_consumption_buf_id[i] = -1;
            ps_ctxt->ai4_entropy_bit_consumption[i] = -1;
            ps_ctxt->ai4_entropy_bit_consumption_buf_id[i] = -1;
            /*accumulate mismatch along with gop level bit error that is propogated to next frame*/
            /*error = rdopt - entropy so it is expected to be negative*/
            rc_update_mismatch_error(ps_ctxt->rc_hdl, i4_error);
            count++;
        }
    }
}

/**
******************************************************************************
*
*  @name  ihevce_rc_check_non_lap_scd
*
*  @par   Description Detects SCD frames as I_only_scds or non_I_scds based
                      on intrasatd & ME costs. Updates scd flags
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*               ps_rc_lap_out
*  @return      void
*
******************************************************************************
*/
void ihevce_rc_check_non_lap_scd(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    picture_type_e rc_pic_type = ihevce_rc_conv_pic_type(
        (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_rc_lap_out->i4_rc_temporal_lyr_id,
        ps_rc_lap_out->i4_is_bottom_field,
        ps_rc_ctxt->i4_top_field_first);

    /*Init to normal frames*/
    ps_rc_lap_out->i4_is_I_only_scd = 0;
    ps_rc_lap_out->i4_is_non_I_scd = 0;

    /*None of the above check is valid if marked as scene cut*/
    if(ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_SCENE_CUT)
    {
        WORD32 i;
        /*reset all older data*/
        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[i] = -1;
            ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[i] = -1;
            ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_sad[i] = -1;
        }
    }
    else
    {
        /*Check if it is I only reset case, lap_out is assumed to have latest data which is used to set the corresponding flags*/
        /*For I pic check for I only reset case and for other pictures check for non-I scd case*/
        if(rc_pic_type == I_PIC)
        {
            if(ps_rc_lap_out->i8_pre_intra_satd <
                   (ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[rc_pic_type] >> 1) ||
               ps_rc_lap_out->i8_pre_intra_satd >
                   (ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[rc_pic_type] << 1))
            {
                /*Check if atleast one frame data is available*/
                if(ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[rc_pic_type] >= 0)
                    ps_rc_lap_out->i4_is_I_only_scd = 1;
            }
        }
        else if(
            ((rc_pic_type == P_PIC) &&
             (ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6)) ||
            (ps_rc_lap_out->i4_rc_quality_preset < IHEVCE_QUALITY_P6))
        {
#define SAD_THREASHOLD_30FPS (2.5)
            /*Choose threshold as 2.5 for 30 fps content and 1.75 for 60 fps. Scale accordingly for intermediate framerate*/
            WORD32 i4_non_simple_repeat_prev_frame_detect = 0;
            float sad_change_threshold =
                (float)(-0.8f * ((float)ps_rc_ctxt->u4_max_frame_rate / 30000) + 3.05f); /*Change of SAD threshold for 30 fps content, this should be lowered for 60 fps*/
            if(sad_change_threshold < 1.5f)
                sad_change_threshold = 1.5f;
            if(sad_change_threshold > 3.0f)
                sad_change_threshold = 3.0f;
            ASSERT(ps_rc_lap_out->i8_raw_l1_coarse_me_sad >= 0);

            /*block variance computed at 4x4 level in w/4*h/4,
                percent dc blks is how many block's variance are less than or equal to 16*/
            if(ps_rc_lap_out->i4_perc_dc_blks < 85)
            {
                /*me sad is expected to be zero for repeat frames*/
                if((ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_sad[rc_pic_type] ==
                    0) &&
                   (ps_rc_lap_out->i4_rc_temporal_lyr_id == ps_rc_ctxt->i4_max_temporal_lyr))
                {
                    i4_non_simple_repeat_prev_frame_detect = 1;
                }
            }
            if(ps_rc_lap_out->i8_frame_acc_coarse_me_cost >
                   (sad_change_threshold *
                    ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[rc_pic_type]) &&
               (ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[rc_pic_type] >= 0) &&
               (!i4_non_simple_repeat_prev_frame_detect))
            {
                WORD32 one_per_pixel_sad_L1;
                /*per pixel sad has to be greater than 1 to avoid repeat frames influence non-I scd detection*/
                if((ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) < 4000000)
                {
                    /*1080*/
                    one_per_pixel_sad_L1 =
                        (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 2;
                }
                else
                {
                    /*4k*/
                    one_per_pixel_sad_L1 =
                        (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 4;
                }
                if(ps_rc_lap_out->i8_frame_acc_coarse_me_cost > one_per_pixel_sad_L1)
                {
                    {
                        ps_rc_lap_out->i4_is_non_I_scd = 1;
                    }
                }
            }

            if(rc_pic_type == P_PIC)
            {
                if(ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[rc_pic_type] < 0)
                {
                    if(ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[I_PIC] > 0)
                    {
                        if(ps_rc_lap_out->i8_pre_intra_satd >
                           ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[I_PIC] << 1)
                        {
                            ps_rc_lap_out->i4_is_non_I_scd = 1;
                        }
                    }
                }
            }
        }
    }

    /*remember the previous frame stats*/
    ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[rc_pic_type] =
        ps_rc_lap_out->i8_pre_intra_satd;  //ps_rc_lap_out->i8_pre_intra_satd;
    ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[rc_pic_type] =
        ps_rc_lap_out->i8_frame_acc_coarse_me_cost;  //ps_rc_lap_out->i8_frame_acc_coarse_me_sad;
    ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_sad[rc_pic_type] =
        ps_rc_lap_out->i8_raw_l1_coarse_me_sad;
}

/**
******************************************************************************
*
*  @name  ihevce_rc_check_is_pre_enc_qp_valid
*
*  @par   Description  checking whether enc thread has updated qp in reverse queue
*
*  @param[in]   ps_rc_ctxt  - pointer to rc context
*
*  @return      zero on success
*
******************************************************************************
*/
/**only function accessed by encoder without using mutex lock*/
WORD32 ihevce_rc_check_is_pre_enc_qp_valid(void *pv_rc_ctxt, volatile WORD32 *pi4_force_end_flag)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;

    volatile WORD32 i4_is_qp_valid;
    volatile WORD32 *pi4_is_qp_valid;

    pi4_is_qp_valid =
        (volatile WORD32 *)&ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index]
            .i4_is_qp_valid;
    i4_is_qp_valid = *pi4_is_qp_valid;

    /*Due to stagger between L1 IPE and L0 IPE, towards the end (when encoder is in flush mode) L0 IPE can race ahead of enc
    since it will suddenly get stagger between L1 and L0 worth of free buffers. It could try to start L0 even before enc has
    populated qp for such frames. qp = -1 is returned in such case which implies encoder should wait for qp to be pop*/

    while(i4_is_qp_valid == -1)
    {
        /*this rate control call is outside mutex lock to avoid deadlock. If this acquires mutex lock enc will not be able to
        populate qp*/
        i4_is_qp_valid = *pi4_is_qp_valid;

        if(1 == (*pi4_force_end_flag))
        {
            *pi4_is_qp_valid = 1;
            i4_is_qp_valid = 1;
        }
    }
    return 0;
}

/*!
******************************************************************************
* \if Function name : ihevce_compute_temporal_complexity_reset_Kp_Kb
*
* \brief
*
* \param[in] *pv_ctxt -> rc context
*
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_compute_temporal_complexity_reset_Kp_Kb(
    rc_lap_out_params_t *ps_rc_lap_out, void *pv_rc_ctxt, WORD32 i4_Kp_Kb_reset_flag)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    rc_lap_out_params_t *ps_cur_rc_lap_out_temporal_offset,
        *ps_cur_rc_lap_out_temporal_offset_scd_detect;
    picture_type_e curr_rc_pic_type;
    LWORD64 i8_total_acc_coarse_me_sad = 0, i8_avg_acc_coarse_me_sad = 0;
    WORD8 i1_num_frames_in_Sub_GOP = 0, i = 0, i1_no_reset = 0;
    WORD32 i4_inter_frame_interval = rc_get_inter_frame_interval(ps_rc_ctxt->rc_hdl);
    WORD32 i4_frame_qp = 0, i4_temp_frame_qp = 0;
    WORD32 ai4_offsets[5] = { -3, -2, 2, 6, 7 };
    ps_cur_rc_lap_out_temporal_offset = ps_rc_lap_out;
    ps_cur_rc_lap_out_temporal_offset_scd_detect = ps_rc_lap_out;

    curr_rc_pic_type = ihevce_rc_conv_pic_type(
        (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out_temporal_offset->i4_rc_pic_type,
        ps_rc_ctxt->i4_field_pic,
        ps_cur_rc_lap_out_temporal_offset->i4_rc_temporal_lyr_id,
        ps_cur_rc_lap_out_temporal_offset->i4_is_bottom_field,
        ps_rc_ctxt->i4_top_field_first);

    if(curr_rc_pic_type == I_PIC)
    {
        ps_cur_rc_lap_out_temporal_offset_scd_detect =
            (rc_lap_out_params_t *)ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode;
        ps_cur_rc_lap_out_temporal_offset =
            (rc_lap_out_params_t *)ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode;

        if(NULL != ps_cur_rc_lap_out_temporal_offset)
        {
            curr_rc_pic_type = ihevce_rc_conv_pic_type(
                (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out_temporal_offset->i4_rc_pic_type,
                ps_rc_ctxt->i4_field_pic,
                ps_cur_rc_lap_out_temporal_offset->i4_rc_temporal_lyr_id,
                ps_cur_rc_lap_out_temporal_offset->i4_is_bottom_field,
                ps_rc_ctxt->i4_top_field_first);
        }
        else
            return;
    }

    if(ps_cur_rc_lap_out_temporal_offset->i4_L1_qp == -1)
        return;

    if(ps_cur_rc_lap_out_temporal_offset->i4_L0_qp == -1)
        i4_frame_qp = ps_cur_rc_lap_out_temporal_offset->i4_L1_qp;
    else
        i4_frame_qp = ps_cur_rc_lap_out_temporal_offset->i4_L0_qp;

    i1_num_frames_in_Sub_GOP = 0;
    i = 0;

    i1_no_reset = 0;
    do
    {
        if(ps_cur_rc_lap_out_temporal_offset != NULL)
        {
            if(curr_rc_pic_type != I_PIC)
                i4_temp_frame_qp =
                    i4_frame_qp + ps_cur_rc_lap_out_temporal_offset->i4_rc_temporal_lyr_id + 1;

            i4_temp_frame_qp += ai4_offsets[curr_rc_pic_type];
            i4_temp_frame_qp = CLIP3(i4_temp_frame_qp, 1, 51);

            {
                if(curr_rc_pic_type != I_PIC)
                {
                    i8_total_acc_coarse_me_sad +=
                        ps_cur_rc_lap_out_temporal_offset
                            ->ai8_frame_acc_coarse_me_sad[i4_temp_frame_qp];
                    i1_num_frames_in_Sub_GOP++;
                    i++;
                }
                else
                {
                    break;
                }
            }

            ps_cur_rc_lap_out_temporal_offset =
                (rc_lap_out_params_t *)ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode;

            if(ps_cur_rc_lap_out_temporal_offset == NULL)
            {
                break;
            }
            curr_rc_pic_type = ihevce_rc_conv_pic_type(
                (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out_temporal_offset->i4_rc_pic_type,
                ps_rc_ctxt->i4_field_pic,
                ps_cur_rc_lap_out_temporal_offset->i4_rc_temporal_lyr_id,
                ps_cur_rc_lap_out_temporal_offset->i4_is_bottom_field,
                ps_rc_ctxt->i4_top_field_first);
        }
        else
        {
            i1_num_frames_in_Sub_GOP = 0;
            break;
        }
    } while(
        ((((curr_rc_pic_type != P_PIC) && ((curr_rc_pic_type != I_PIC))) ||
          (curr_rc_pic_type == P_PIC)) &&
         (i1_num_frames_in_Sub_GOP < i4_inter_frame_interval)));

    if((i1_num_frames_in_Sub_GOP) && (i1_no_reset == 0))
    {
        float f_hme_sad_per_pixel;
        i8_avg_acc_coarse_me_sad = (i8_total_acc_coarse_me_sad / i1_num_frames_in_Sub_GOP);
        f_hme_sad_per_pixel =
            ((float)i8_avg_acc_coarse_me_sad /
             (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width));
        f_hme_sad_per_pixel = CLIP3(f_hme_sad_per_pixel, 0.01f, 5.0f);
        /*reset the QP offsets for the next sub GOP depending on the offline model based on the temporal complexity */
        if(i4_Kp_Kb_reset_flag)
        {
            WORD32 i4_bin;

            rc_reset_Kp_Kb(
                ps_rc_ctxt->rc_hdl,
                8.00,
                ps_rc_ctxt->i4_num_active_pic_type,
                f_hme_sad_per_pixel,
                &i4_bin,
                ps_rc_ctxt->i4_rc_pass);
        }
        else
        {
            rc_ba_get_qp_offset_offline_data(
                ps_rc_ctxt->rc_hdl,
                ps_rc_lap_out->ai4_offsets,
                f_hme_sad_per_pixel,
                ps_rc_ctxt->i4_num_active_pic_type,
                &ps_rc_lap_out->i4_complexity_bin);

            ps_cur_rc_lap_out_temporal_offset = ps_rc_lap_out;
            ps_cur_rc_lap_out_temporal_offset->i4_offsets_set_flag = 1;

            curr_rc_pic_type = ihevce_rc_conv_pic_type(
                (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type,
                ps_rc_ctxt->i4_field_pic,
                ps_rc_lap_out->i4_rc_temporal_lyr_id,
                ps_rc_lap_out->i4_is_bottom_field,
                ps_rc_ctxt->i4_top_field_first);

            if((curr_rc_pic_type == I_PIC) &&
               ((rc_lap_out_params_t *)ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode)
                       ->i4_rc_pic_type == P_PIC)
                i1_num_frames_in_Sub_GOP++;

            for(i = 1; i < i1_num_frames_in_Sub_GOP; i++)
            {
                ps_cur_rc_lap_out_temporal_offset =
                    (rc_lap_out_params_t *)
                        ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode;
                memmove(
                    ps_cur_rc_lap_out_temporal_offset->ai4_offsets,
                    ps_rc_lap_out->ai4_offsets,
                    sizeof(WORD32) * 5);
                ps_cur_rc_lap_out_temporal_offset->i4_complexity_bin =
                    ps_rc_lap_out->i4_complexity_bin;
                ps_cur_rc_lap_out_temporal_offset->i4_offsets_set_flag = 1;
            }
        }
    }
}

/**
******************************************************************************
*
*  @brief function to get delta QP or In frame RC bits estimate to avoid buffer underflow
*
*  @par   Description
*  @param[in]
******************************************************************************
*/

WORD32 ihevce_ebf_based_rc_correction_to_avoid_overflow(
    rc_context_t *ps_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out, WORD32 *pi4_tot_bits_estimated)
{
    WORD32 i4_modelQP, i4_clipQP, i4_maxEbfQP, i4_diffQP, i4_is_model_valid, i4_deltaQP = 0;
    LWORD64 i8_bitsClipQP, i8_grwEbf;  // i8_bitsComp;
    WORD32 i4_is_offline_model_used;
    WORD32 i4_vbv_buffer_size, i4_drain_rate, i4_currEbf, i4_maxEbf;
    WORD32 i4_case = -1;
    float f_thrsh_i_pic_delta_qp_1, f_thrsh_i_pic_delta_qp_2, f_thrsh_p_pic_delta_qp_1,
        f_thrsh_p_pic_delta_qp_2;
    float f_thrsh_br_pic_delta_qp_1, f_thrsh_br_pic_delta_qp_2, f_thrsh_bnr_pic_delta_qp_1,
        f_thrsh_bnr_pic_delta_qp_2;
    float f_vbv_thrsh_delta_qp;

    /*initialization of all the variables*/
    rc_init_buffer_info(
        ps_rc_ctxt->rc_hdl, &i4_vbv_buffer_size, &i4_currEbf, &i4_maxEbf, &i4_drain_rate);

    i4_is_model_valid = ps_rc_lap_out->i4_is_model_valid;
    i4_modelQP = ps_rc_ctxt->s_rc_high_lvl_stat.i4_modelQP;
    i4_clipQP = ps_rc_ctxt->s_rc_high_lvl_stat.i4_finalQP;
    i4_maxEbfQP = ps_rc_ctxt->s_rc_high_lvl_stat.i4_maxEbfQP;
    i8_bitsClipQP = ps_rc_ctxt->s_rc_high_lvl_stat.i8_bits_from_finalQP;
    i4_is_offline_model_used = ps_rc_ctxt->s_rc_high_lvl_stat.i4_is_offline_model_used;
    ASSERT(i4_clipQP != INVALID_QP);

    if(ps_rc_ctxt->i4_num_frame_parallel > 1)
    {
        f_thrsh_i_pic_delta_qp_1 = (float)VBV_THRSH_FRM_PRLL_I_PIC_DELTA_QP_1;
        f_thrsh_i_pic_delta_qp_2 = (float)VBV_THRSH_FRM_PRLL_I_PIC_DELTA_QP_2;
        f_thrsh_p_pic_delta_qp_1 = (float)VBV_THRSH_FRM_PRLL_P_PIC_DELTA_QP_1;
        f_thrsh_p_pic_delta_qp_2 = (float)VBV_THRSH_FRM_PRLL_P_PIC_DELTA_QP_2;
        f_thrsh_br_pic_delta_qp_1 = (float)VBV_THRSH_FRM_PRLL_BR_PIC_DELTA_QP_1;
        f_thrsh_br_pic_delta_qp_2 = (float)VBV_THRSH_FRM_PRLL_BR_PIC_DELTA_QP_2;
        f_thrsh_bnr_pic_delta_qp_1 = (float)VBV_THRSH_FRM_PRLL_BNR_PIC_DELTA_QP_1;
        f_thrsh_bnr_pic_delta_qp_2 = (float)VBV_THRSH_FRM_PRLL_BNR_PIC_DELTA_QP_2;
        f_vbv_thrsh_delta_qp = (float)VBV_THRSH_FRM_PRLL_DELTA_QP;
    }
    else
    {
        f_thrsh_i_pic_delta_qp_1 = (float)VBV_THRSH_I_PIC_DELTA_QP_1;
        f_thrsh_i_pic_delta_qp_2 = (float)VBV_THRSH_I_PIC_DELTA_QP_2;
        f_thrsh_p_pic_delta_qp_1 = (float)VBV_THRSH_P_PIC_DELTA_QP_1;
        f_thrsh_p_pic_delta_qp_2 = (float)VBV_THRSH_P_PIC_DELTA_QP_2;
        f_thrsh_br_pic_delta_qp_1 = (float)VBV_THRSH_BR_PIC_DELTA_QP_1;
        f_thrsh_br_pic_delta_qp_2 = (float)VBV_THRSH_BR_PIC_DELTA_QP_2;
        f_thrsh_bnr_pic_delta_qp_1 = (float)VBV_THRSH_BNR_PIC_DELTA_QP_1;
        f_thrsh_bnr_pic_delta_qp_2 = (float)VBV_THRSH_BNR_PIC_DELTA_QP_2;
        f_vbv_thrsh_delta_qp = (float)VBV_THRSH_DELTA_QP;
    }

    /* function logic starts */
    if(i4_is_model_valid)
    {
        ASSERT(i4_modelQP != INVALID_QP);
        i8_grwEbf = i8_bitsClipQP - (LWORD64)i4_drain_rate;
        if(((i4_currEbf + i8_grwEbf) > (0.6*i4_vbv_buffer_size)) /*&&
                 i4_modelQP >= i4_clipQP*/)
        {
            /* part of existing scene (i.e. no new scene)
            In which case this is not first I/P/Bref/Bnref etc
            The models for I/P/Bref/Bnref are all valid*/
            if(((i4_currEbf + i8_grwEbf) <
                i4_maxEbf)) /* does not matter whether this is 2pass, 1 pass, VBR, CBR etc*/
            {
                /* clipQP has been determined keeping in view certain other quality constraints like pusling etc.
                So better to honour it if possible*/
                //if (i8_bitsClipQP > i8_drain_rate)
                {
                    LWORD64 i8_thrsh_for_deltaQP_2 = i4_vbv_buffer_size,
                            i8_thrsh_for_deltaQP_1 = i4_vbv_buffer_size;
                    /*even when (modelQP - clipQP) = 0, we intend to QP increase as expected ebf is above 60%*/
                    i4_diffQP = MAX(i4_modelQP - i4_clipQP, 1);
                    switch(ps_rc_lap_out->i4_rc_pic_type)
                    {
                    case IV_I_FRAME:
                    case IV_IDR_FRAME:
                    {
                        i8_thrsh_for_deltaQP_1 =
                            (LWORD64)(f_thrsh_i_pic_delta_qp_1 * i4_vbv_buffer_size);
                        i8_thrsh_for_deltaQP_2 =
                            (LWORD64)(f_thrsh_i_pic_delta_qp_2 * i4_vbv_buffer_size);
                        break;
                    }
                    case IV_P_FRAME:
                    {
                        i8_thrsh_for_deltaQP_1 =
                            (LWORD64)(f_thrsh_p_pic_delta_qp_1 * i4_vbv_buffer_size);
                        i8_thrsh_for_deltaQP_2 =
                            (LWORD64)(f_thrsh_p_pic_delta_qp_2 * i4_vbv_buffer_size);
                        break;
                    }
                    case IV_B_FRAME:
                    {
                        if(ps_rc_lap_out->i4_rc_is_ref_pic)
                        {
                            i8_thrsh_for_deltaQP_1 =
                                (LWORD64)(f_thrsh_br_pic_delta_qp_1 * i4_vbv_buffer_size);
                            i8_thrsh_for_deltaQP_2 =
                                (LWORD64)(f_thrsh_br_pic_delta_qp_2 * i4_vbv_buffer_size);
                        }
                        else
                        {
                            /*as of now using the same thresholds as B reference, later may have to tune if required*/
                            i8_thrsh_for_deltaQP_1 =
                                (LWORD64)(f_thrsh_bnr_pic_delta_qp_1 * i4_vbv_buffer_size);
                            i8_thrsh_for_deltaQP_2 =
                                (LWORD64)(f_thrsh_bnr_pic_delta_qp_2 * i4_vbv_buffer_size);
                        }
                        break;
                    }
                    default:
                        break;
                    }

                    if((i4_currEbf + i8_grwEbf) > i8_thrsh_for_deltaQP_1)
                    {
                        /*For more than 2 QP chnage this means a larger scale issue and probably needs to be handled elsewhere ?*/
                        i4_deltaQP =
                            MIN(2, i4_diffQP); /* we dont intend to change QP by more than 2 */
                        i4_case = 0;
                    }
                    else if((i4_currEbf + i8_grwEbf) > i8_thrsh_for_deltaQP_2)
                    {
                        i4_deltaQP = MIN(1, i4_diffQP);
                        i4_case = 1;
                    }
                }
                /* else  if (i8_bitsClipQP > i8_drain_rate)
        {
                we have no correection, buffer will be healthy after this.
                However, there could be one problem if the currEbf is already close to say 80% of EBF.
                This means we have not reacted well early - needs to be handled?

                This could be the case where it is a simple scene immediately following a complex scene
                and is the I picture (not the first I since model is valid).
                Is this possible - maybe, what to do - dont know?
        }
                */
            }
            else /*(i4_clipQP < i4_maxEbfQP)*/
            {
                i4_deltaQP = 2;
                i4_case = 2;
            }
        }
        if((i4_currEbf + i8_grwEbf) < (0.6 * i4_vbv_buffer_size))
        {
            *pi4_tot_bits_estimated = i8_bitsClipQP;
        }
    }
    else
    {
        if(i4_is_offline_model_used)
        {
            /* this can be only for non-I SCD, where we reset RC */
            WORD32 i4_bits_est_for_in_frm_rc = *pi4_tot_bits_estimated;
            i8_grwEbf = i4_bits_est_for_in_frm_rc - i4_drain_rate;
            if((i4_currEbf + i8_grwEbf) > (f_vbv_thrsh_delta_qp * i4_vbv_buffer_size))
            {
                i4_bits_est_for_in_frm_rc =
                    i4_drain_rate + (WORD32)(0.85 * i4_vbv_buffer_size) - i4_currEbf;
                /* if pi4_tot_bits_estimated becomes less than zero or less than drain rate this indiactes that we are near or above 85% of the buffer */
                /* this needs a reaction */
                if(i4_bits_est_for_in_frm_rc < i4_drain_rate)
                {
                    *pi4_tot_bits_estimated =
                        MAX((i4_drain_rate + (WORD32)(0.95 * i4_vbv_buffer_size) - i4_currEbf),
                            i4_drain_rate);
                    i4_deltaQP = 2; /* this needs some review, needs to be handled well */
                }
            }
            i4_case = 3;
        }
        else
        {
            i8_bitsClipQP = *pi4_tot_bits_estimated;
            i8_grwEbf = i8_bitsClipQP - i4_drain_rate;

            if(((i4_currEbf + i8_grwEbf) <
                i4_maxEbf)) /* does not matter whether this is 2pass, 1 pass, VBR, CBR etc*/
            {
                /* clipQP has been determined keeping in view certain other quality constraints like pusling etc.
                    So better to honour it if possible*/
                //if (i8_bitsClipQP > i8_drain_rate)
                {
                    LWORD64 i8_thrsh_for_deltaQP_2 = i4_vbv_buffer_size,
                            i8_thrsh_for_deltaQP_1 = i4_vbv_buffer_size;

                    switch(ps_rc_lap_out->i4_rc_pic_type)
                    {
                    case IV_I_FRAME:
                    case IV_IDR_FRAME:
                    {
                        i8_thrsh_for_deltaQP_1 =
                            (LWORD64)(f_thrsh_i_pic_delta_qp_1 * i4_vbv_buffer_size);
                        i8_thrsh_for_deltaQP_2 =
                            (LWORD64)(f_thrsh_i_pic_delta_qp_2 * i4_vbv_buffer_size);
                        break;
                    }
                    case IV_P_FRAME:
                    {
                        i8_thrsh_for_deltaQP_1 =
                            (LWORD64)(f_thrsh_p_pic_delta_qp_1 * i4_vbv_buffer_size);
                        i8_thrsh_for_deltaQP_2 =
                            (LWORD64)(f_thrsh_p_pic_delta_qp_2 * i4_vbv_buffer_size);
                        break;
                    }
                    case IV_B_FRAME:
                    {
                        if(ps_rc_lap_out->i4_rc_is_ref_pic)
                        {
                            i8_thrsh_for_deltaQP_1 =
                                (LWORD64)(f_thrsh_br_pic_delta_qp_1 * i4_vbv_buffer_size);
                            i8_thrsh_for_deltaQP_2 =
                                (LWORD64)(f_thrsh_br_pic_delta_qp_2 * i4_vbv_buffer_size);
                        }
                        else
                        {
                            /*as of now using the same thresholds as B reference, later may have to tune if required*/
                            i8_thrsh_for_deltaQP_1 =
                                (LWORD64)(f_thrsh_bnr_pic_delta_qp_1 * i4_vbv_buffer_size);
                            i8_thrsh_for_deltaQP_2 =
                                (LWORD64)(f_thrsh_bnr_pic_delta_qp_2 * i4_vbv_buffer_size);
                        }
                        break;
                    }
                    default:
                        break;
                    }

                    if((i4_currEbf + i8_grwEbf) > i8_thrsh_for_deltaQP_1)
                    {
                        /*For more than 2 QP chnage this means a larger scale issue and probably needs to be handled elsewhere ?*/
                        i4_deltaQP = 2; /* we dont intend to change QP by more than 2 */
                        i4_case = 5;
                    }
                    else if((i4_currEbf + i8_grwEbf) > i8_thrsh_for_deltaQP_2)
                    {
                        i4_deltaQP = 1;
                        i4_case = 6;
                    }
                }
            }
            else
            {
                i4_deltaQP = 2;
                i4_case = 7;
            }
        }
    }
    return i4_deltaQP;
}

/*###############################################*/
/******* END OF RC UTILS FUNCTIONS ***************/
/*###############################################*/

/*########################################################*/
/******* START OF VBV COMPLIANCE FUNCTIONS ***************/
/*#######################################################*/

/*!
******************************************************************************
* \if Function name : ihevce_vbv_compliance_frame_level_update
*
* \brief
*    this function initializes the hrd buffer level to be used for vbv compliance testing using the parameters feeded in VUI parameters
*
* \param[in] *pv_ctxt -> rc context
*           i4_bits_generated -> bits generated from entropy
*           i4_resolution_id -> info needed for log Dump
*           i4_appln_bitrate_inst -> info needed for log Dump
*           u4_cur_cpb_removal_delay_minus1 -> cbp removal delay of present frame
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/

void ihevce_vbv_compliance_frame_level_update(
    void *pv_rc_ctxt,
    WORD32 i4_bits_generated,
    WORD32 i4_resolution_id,
    WORD32 i4_appln_bitrate_inst,
    UWORD32 u4_cur_cpb_removal_delay_minus1)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt;
    float f_max_vbv_buff_size = (float)ps_rc_ctxt->s_vbv_compliance.f_buffer_size;
    WORD32 i4_cbp_removal_delay_diff = 1;

    if((ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1 > 0) &&
       (u4_cur_cpb_removal_delay_minus1 >
        ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1))
        i4_cbp_removal_delay_diff =
            (u4_cur_cpb_removal_delay_minus1 -
             ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1);

    ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level =
        ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level - (float)i4_bits_generated +
        (i4_cbp_removal_delay_diff * ps_rc_ctxt->s_vbv_compliance.f_drain_rate);

    ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip =
        ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level;

    if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level < 0)
    {
        ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level = 0;
    }

    if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level >
       ps_rc_ctxt->s_vbv_compliance.f_buffer_size)
    {
        ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level =
            ps_rc_ctxt->s_vbv_compliance.f_buffer_size;
        ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip -=
            ps_rc_ctxt->s_vbv_compliance.f_buffer_size;
    }
    else if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip > 0)
    {
        ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip = 0;
    }

    if(ps_rc_ctxt->e_rate_control_type == VBR_STREAMING)
    {
        if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip > 0)
            ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip = 0;
    }
    ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1 = u4_cur_cpb_removal_delay_minus1;
}

/*!
******************************************************************************
* \if Function name : ihevce_vbv_complaince_init_level
*
* \brief
*    this function initializes the hrd buffer level to be used for vbv compliance testing using the parameters feeded in VUI parameters
*
* \param[in] *pv_ctxt -> rc context
*            *ps_vui      -> VUI parameters
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/

void ihevce_vbv_complaince_init_level(void *pv_ctxt, vui_t *ps_vui)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;

    ps_rc_ctxt->s_vbv_compliance.f_frame_rate =
        (float)((float)ps_vui->u4_vui_time_scale / ps_vui->u4_vui_num_units_in_tick);  //rc_get_frame_rate(ps_rc_ctxt->rc_hdl);

    if(1 == ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag)
    {
        ASSERT(1 == ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag);

        ps_rc_ctxt->s_vbv_compliance.f_bit_rate = (float)((
            (ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[0].au4_bit_rate_du_value_minus1[0] +
             1)
            << (6 + ps_vui->s_vui_hrd_parameters
                        .u4_bit_rate_scale)));  //rc_get_bit_rate(ps_rc_ctxt->rc_hdl);

        ps_rc_ctxt->s_vbv_compliance.f_buffer_size = (float)((
            (ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[0].au4_cpb_size_du_value_minus1[0] +
             1)
            << (4 + ps_vui->s_vui_hrd_parameters
                        .u4_cpb_size_du_scale)));  //ps_rc_ctxt->u4_max_vbv_buff_size;
    }
    else
    {
        ps_rc_ctxt->s_vbv_compliance.f_bit_rate = (float)((
            (ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[0].au4_bit_rate_value_minus1[0] +
             1)
            << (6 + ps_vui->s_vui_hrd_parameters
                        .u4_bit_rate_scale)));  //rc_get_bit_rate(ps_rc_ctxt->rc_hdl);

        ps_rc_ctxt->s_vbv_compliance.f_buffer_size = (float)((
            (ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[0].au4_cpb_size_value_minus1[0] +
             1)
            << (4 + ps_vui->s_vui_hrd_parameters
                        .u4_cpb_size_scale)));  //ps_rc_ctxt->u4_max_vbv_buff_size;
    }
    ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level =
        (float)ps_rc_ctxt->s_vbv_compliance.f_buffer_size;  //ps_rc_ctxt->u4_max_vbv_buff_size;

    ps_rc_ctxt->s_vbv_compliance.f_drain_rate =
        ((ps_rc_ctxt->s_vbv_compliance.f_bit_rate) / ps_rc_ctxt->s_vbv_compliance.f_frame_rate);

    ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1 = 0;
}

/*########################################################*/
/******* END OF VBV COMPLIANCE FUNCTIONS *****************/
/*#######################################################*/

/*################################################################*/
/******* START OF DYN CHANGE iN BITRATE FUNCTIONS *****************/
/*################################################################*/
/*!
******************************************************************************
* \if Function name : change_bitrate_vbv_complaince
*
* \brief
*    this function updates the new bitrate and re calculates the drain rate
*
* \param[in] *pv_ctxt -> rc context
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void change_bitrate_vbv_complaince(void *pv_ctxt, LWORD64 i8_new_bitrate, LWORD64 i8_buffer_size)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    ps_rc_ctxt->s_vbv_compliance.f_buffer_size = (float)i8_buffer_size;
    ps_rc_ctxt->s_vbv_compliance.f_bit_rate = (float)i8_new_bitrate;
    if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level > i8_buffer_size)
        ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level = (float)i8_buffer_size;
    ps_rc_ctxt->s_vbv_compliance.f_drain_rate =
        ps_rc_ctxt->s_vbv_compliance.f_bit_rate / ps_rc_ctxt->s_vbv_compliance.f_frame_rate;
}
/*!
******************************************************************************
* \if Function name : ihevce_rc_register_dyn_change_bitrate
*
* \brief
*    this function registers call to change bitrate dynamically.
*
* \param[in] *pv_ctxt -> rc context
*
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/

void ihevce_rc_register_dyn_change_bitrate(
    void *pv_ctxt, LWORD64 i8_new_bitrate, LWORD64 i8_new_peak_bitrate)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    ps_rc_ctxt->i8_new_bitrate = i8_new_bitrate;
    ps_rc_ctxt->i8_new_peak_bitrate = i8_new_peak_bitrate;
    ps_rc_ctxt->i4_bitrate_changed = 1;
    ASSERT(ps_rc_ctxt->i8_new_bitrate > 0);
    ASSERT(ps_rc_ctxt->i8_new_peak_bitrate > 0);
}

/*!
******************************************************************************
* \if Function name : ihevce_rc_get_new_bitrate
*
* \brief
*    get new bitrate
*
* \param[in] *pv_ctxt -> rc context
*
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/
LWORD64 ihevce_rc_get_new_bitrate(void *pv_ctxt)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    return ps_rc_ctxt->i8_new_bitrate;
}
/*!
******************************************************************************
* \if Function name : ihevce_rc_get_new_peak_bitrate
*
* \brief
*    get new peak rate
*
* \param[in] *pv_ctxt -> rc context
*
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/
LWORD64 ihevce_rc_get_new_peak_bitrate(void *pv_ctxt)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    return ps_rc_ctxt->i8_new_peak_bitrate;
}

/*!
******************************************************************************
* \if Function name : ihevce_rc_change_avg_bitrate
*
* \brief
*    change average bitrate configured based on new bitrate
*
* \param[in] *pv_ctxt -> rc context
*
* \return
*
* \author
*  Ittiam
*
*****************************************************************************
*/
LWORD64 ihevce_rc_change_avg_bitrate(void *pv_ctxt)
{
    rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt;
    LWORD64 vbv_buffer_level_b4_change;

    ASSERT(ps_rc_ctxt->i8_new_bitrate != -1);
    ASSERT(ps_rc_ctxt->i8_new_peak_bitrate != -1);
    /*Get the VBV buffer level just before forcing bitrate change*/
    vbv_buffer_level_b4_change = (LWORD64)rc_get_ebf(ps_rc_ctxt->rc_hdl);

    change_avg_bit_rate(
        ps_rc_ctxt->rc_hdl,
        (UWORD32)ps_rc_ctxt->i8_new_bitrate,
        (UWORD32)ps_rc_ctxt->i8_new_peak_bitrate);
    /*Once the request is serviced set new bitrate to -1*/
    ps_rc_ctxt->i8_new_bitrate = -1;
    ps_rc_ctxt->i8_new_peak_bitrate = -1;
    return vbv_buffer_level_b4_change;
}

/*##############################################################*/
/******* END OF DYN CHNAGE iN BITRATE FUNCTIONS *****************/
/*##############################################################*/