aboutsummaryrefslogtreecommitdiff
path: root/encoder/hme_subpel.c
blob: 6d853b37a8a779dec5fc45f05144370fe381018f (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
/******************************************************************************
 *
 * 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 hme_subpel.c
*
* @brief
*    Subpel refinement modules for ME algo
*
* @author
*    Ittiam
*
*
* List of Functions
* hme_qpel_interp_avg()
* hme_subpel_refine_ctblist_bck()
* hme_subpel_refine_ctblist_fwd()
* hme_refine_bidirect()
* hme_subpel_refinement()
* hme_subpel_refine_ctb_fwd()
* hme_subpel_refine_ctb_bck()
* hme_create_bck_inp()
* hme_subpel_refine_search_node()
******************************************************************************
*/

/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
/* System include files */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <math.h>
#include <limits.h>

/* User include files */
#include "ihevc_typedefs.h"
#include "itt_video_api.h"
#include "ihevce_api.h"

#include "rc_cntrl_param.h"
#include "rc_frame_info_collector.h"
#include "rc_look_ahead_params.h"

#include "ihevc_defs.h"
#include "ihevc_structs.h"
#include "ihevc_platform_macros.h"
#include "ihevc_deblk.h"
#include "ihevc_itrans_recon.h"
#include "ihevc_chroma_itrans_recon.h"
#include "ihevc_chroma_intra_pred.h"
#include "ihevc_intra_pred.h"
#include "ihevc_inter_pred.h"
#include "ihevc_mem_fns.h"
#include "ihevc_padding.h"
#include "ihevc_weighted_pred.h"
#include "ihevc_sao.h"
#include "ihevc_resi_trans.h"
#include "ihevc_quant_iquant_ssd.h"
#include "ihevc_cabac_tables.h"

#include "ihevce_defs.h"
#include "ihevce_lap_enc_structs.h"
#include "ihevce_multi_thrd_structs.h"
#include "ihevce_multi_thrd_funcs.h"
#include "ihevce_me_common_defs.h"
#include "ihevce_had_satd.h"
#include "ihevce_error_codes.h"
#include "ihevce_bitstream.h"
#include "ihevce_cabac.h"
#include "ihevce_rdoq_macros.h"
#include "ihevce_function_selector.h"
#include "ihevce_enc_structs.h"
#include "ihevce_entropy_structs.h"
#include "ihevce_cmn_utils_instr_set_router.h"
#include "ihevce_enc_loop_structs.h"
#include "ihevce_bs_compute_ctb.h"
#include "ihevce_global_tables.h"
#include "ihevce_dep_mngr_interface.h"
#include "hme_datatype.h"
#include "hme_interface.h"
#include "hme_common_defs.h"
#include "hme_defs.h"
#include "ihevce_me_instr_set_router.h"
#include "hme_globals.h"
#include "hme_utils.h"
#include "hme_coarse.h"
#include "hme_fullpel.h"
#include "hme_subpel.h"
#include "hme_refine.h"
#include "hme_err_compute.h"
#include "hme_common_utils.h"
#include "hme_search_algo.h"
#include "ihevce_stasino_helpers.h"
#include "ihevce_common_utils.h"

/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/
void hme_qpel_interp_avg(interp_prms_t *ps_prms, S32 i4_mv_x, S32 i4_mv_y, S32 i4_buf_id)
{
    U08 *pu1_src1, *pu1_src2, *pu1_dst;
    qpel_input_buf_cfg_t *ps_inp_cfg;
    S32 i4_mv_x_frac, i4_mv_y_frac, i4_offset;

    /*************************************************************************/
    /* For a given QPEL pt, we need to determine the 2 source pts that are   */
    /* needed to do the QPEL averaging. The logic to do this is as follows   */
    /* i4_mv_x and i4_mv_y are the motion vectors in QPEL units that are     */
    /* pointing to the pt of interest. Obviously, they are w.r.t. the 0,0    */
    /* pt of th reference blk that is colocated to the inp blk.              */
    /*    A j E k B                                                          */
    /*    l m n o p                                                          */
    /*    F q G r H                                                          */
    /*    s t u v w                                                          */
    /*    C x I y D                                                          */
    /* In above diagram, A. B, C, D are full pts at offsets (0,0),(1,0),(0,1)*/
    /* and (1,1) respectively in the fpel buffer (id = 0)                    */
    /* E and I are hxfy pts in offsets (0,0),(0,1) respectively in hxfy buf  */
    /* F and H are fxhy pts in offsets (0,0),(1,0) respectively in fxhy buf  */
    /* G is hxhy pt in offset 0,0 in hxhy buf                                */
    /* All above offsets are computed w.r.t. motion displaced pt in          */
    /* respective bufs. This means that A corresponds to (i4_mv_x >> 2) and  */
    /* (i4_mv_y >> 2) in fxfy buf. Ditto with E, F and G                     */
    /* fxfy buf is buf id 0, hxfy is buf id 1, fxhy is buf id 2, hxhy is 3   */
    /* If we consider pt v to be derived. v has a fractional comp of 3, 3    */
    /* v is avg of H and I. So the table look up of v should give following  */
    /* buf 1 (H) : offset = (1, 0) buf id = 2.                               */
    /* buf 2 (I) : offset = 0 , 1) buf id = 1.                               */
    /* NOTE: For pts that are fxfy/hxfy/fxhy/hxhy, bufid 1 will be -1.       */
    /*************************************************************************/
    i4_mv_x_frac = i4_mv_x & 3;
    i4_mv_y_frac = i4_mv_y & 3;

    i4_offset = (i4_mv_x >> 2) + (i4_mv_y >> 2) * ps_prms->i4_ref_stride;

    /* Derive the descriptor that has all offset and size info */
    ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

    if(ps_inp_cfg->i1_buf_id1 == ps_inp_cfg->i1_buf_id2)
    {
        /* This is case for fxfy/hxfy/fxhy/hxhy */
        ps_prms->pu1_final_out = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ps_prms->pu1_final_out += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ps_prms->pu1_final_out += (ps_inp_cfg->i1_buf_yoff1 * ps_prms->i4_ref_stride);
        ps_prms->i4_final_out_stride = ps_prms->i4_ref_stride;

        return;
    }

    pu1_src1 = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
    pu1_src1 += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
    pu1_src1 += (ps_inp_cfg->i1_buf_yoff1 * ps_prms->i4_ref_stride);

    pu1_src2 = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id2];
    pu1_src2 += ps_inp_cfg->i1_buf_xoff2 + i4_offset;
    pu1_src2 += (ps_inp_cfg->i1_buf_yoff2 * ps_prms->i4_ref_stride);

    pu1_dst = ps_prms->apu1_interp_out[i4_buf_id];
    hevc_avg_2d(
        pu1_src1,
        pu1_src2,
        ps_prms->i4_ref_stride,
        ps_prms->i4_ref_stride,
        ps_prms->i4_blk_wd,
        ps_prms->i4_blk_ht,
        pu1_dst,
        ps_prms->i4_out_stride);
    ps_prms->pu1_final_out = pu1_dst;
    ps_prms->i4_final_out_stride = ps_prms->i4_out_stride;
}

static __inline void hme_qpel_interp_avg_2pt_vert_no_reuse(
    interp_prms_t *ps_prms,
    S32 i4_mv_x,
    S32 i4_mv_y,
    U08 **ppu1_final,
    S32 *pi4_final_stride,
    FT_QPEL_INTERP_AVG_1PT *pf_qpel_interp_avg_1pt)
{
    pf_qpel_interp_avg_1pt(ps_prms, i4_mv_x, i4_mv_y + 1, 3, ppu1_final, pi4_final_stride);

    pf_qpel_interp_avg_1pt(ps_prms, i4_mv_x, i4_mv_y - 1, 1, ppu1_final, pi4_final_stride);
}

static __inline void hme_qpel_interp_avg_2pt_horz_no_reuse(
    interp_prms_t *ps_prms,
    S32 i4_mv_x,
    S32 i4_mv_y,
    U08 **ppu1_final,
    S32 *pi4_final_stride,
    FT_QPEL_INTERP_AVG_1PT *pf_qpel_interp_avg_1pt)
{
    pf_qpel_interp_avg_1pt(ps_prms, i4_mv_x + 1, i4_mv_y, 2, ppu1_final, pi4_final_stride);

    pf_qpel_interp_avg_1pt(ps_prms, i4_mv_x - 1, i4_mv_y, 0, ppu1_final, pi4_final_stride);
}

/********************************************************************************
*  @fn     hme_qpel_interp_comprehensive
*
*  @brief  Interpolates 2 qpel points by hpel averaging
*
*  @param[in,out]  ps_prms: Both input buffer ptrs and location of output
*
*  @param[in]  i4_mv_x : x component of motion vector in QPEL units
*
*  @param[in]  i4_mv_y : y component of motion vector in QPEL units
*
*  @param[in]  i4_grid_mask : mask which determines qpels to be computed
*
*  @param[out]  ppu1_final : storage for final buffer pointers
*
*  @param[out]  pi4_final_stride : storage for final buffer strides
*
*  @return None
********************************************************************************
*/
static __inline void hme_qpel_interp_comprehensive(
    interp_prms_t *ps_prms,
    U08 **ppu1_final,
    S32 *pi4_final_stride,
    S32 i4_mv_x,
    S32 i4_mv_y,
    S32 i4_grid_mask,
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list)
{
    S32 pt_select_for_TB, pt_select_for_LR;
    S32 dx, dy, dydx;
    S32 vert_func_selector, horz_func_selector;

    S32 i4_ref_stride = ps_prms->i4_ref_stride;

    pt_select_for_TB =
        ((i4_grid_mask & (1 << PT_B)) >> PT_B) + ((i4_grid_mask & (1 << PT_T)) >> (PT_T - 1));

    pt_select_for_LR =
        ((i4_grid_mask & (1 << PT_R)) >> PT_R) + ((i4_grid_mask & (1 << PT_L)) >> (PT_L - 1));

    dx = (i4_mv_x & 3);
    dy = (i4_mv_y & 3);
    dydx = (dx + (dy << 2));

    vert_func_selector = gai4_select_qpel_function_vert[pt_select_for_TB][dydx];
    horz_func_selector = gai4_select_qpel_function_horz[pt_select_for_LR][dydx];

    /* case descriptions */
    /* Let T = (gridmask & T) & B = (gridmask & B) */
    /* & hp = pt is an hpel or an fpel */
    /* & r = reuse possible */
    /* 0 => T || B = 0 */
    /* 1 => (!T) && (B) && hp */
    /* 2 => (T) && (!B) && hp */
    /* 3 => (!T) && (B) && !hp */
    /* 4 => (T) && (!B) && !hp */
    /* 5 => (T) && (B) && !hp && r */
    /* 6 => (T) && (B) && !hp && !r */
    /* 7 => (T) && (B) && hp */

    switch(vert_func_selector)
    {
    case 0:
    {
        break;
    }
    case 1:
    {
        S32 i4_mv_x_frac, i4_mv_y_frac, i4_offset;
        qpel_input_buf_cfg_t *ps_inp_cfg;
        S32 i4_mvyp1 = (i4_mv_y + 1);

        i4_mv_x_frac = dx;
        i4_mv_y_frac = i4_mvyp1 & 3;

        i4_offset = (i4_mv_x >> 2) + (i4_mvyp1 >> 2) * i4_ref_stride;

        /* Derive the descriptor that has all offset and size info */
        ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

        ppu1_final[3] = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ppu1_final[3] += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ppu1_final[3] += (ps_inp_cfg->i1_buf_yoff1 * i4_ref_stride);
        pi4_final_stride[3] = i4_ref_stride;

        break;
    }
    case 2:
    {
        S32 i4_mv_x_frac, i4_mv_y_frac, i4_offset;
        qpel_input_buf_cfg_t *ps_inp_cfg;
        S32 i4_mvym1 = (i4_mv_y - 1);

        i4_mv_x_frac = dx;
        i4_mv_y_frac = i4_mvym1 & 3;

        i4_offset = (i4_mv_x >> 2) + (i4_mvym1 >> 2) * i4_ref_stride;

        /* Derive the descriptor that has all offset and size info */
        ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

        ppu1_final[1] = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ppu1_final[1] += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ppu1_final[1] += (ps_inp_cfg->i1_buf_yoff1 * i4_ref_stride);
        pi4_final_stride[1] = i4_ref_stride;

        break;
    }
    case 3:
    {
        ps_me_optimised_function_list->pf_qpel_interp_avg_1pt(
            ps_prms, i4_mv_x, i4_mv_y + 1, 3, ppu1_final, pi4_final_stride);

        break;
    }
    case 4:
    {
        ps_me_optimised_function_list->pf_qpel_interp_avg_1pt(
            ps_prms, i4_mv_x, i4_mv_y - 1, 1, ppu1_final, pi4_final_stride);

        break;
    }
    case 5:
    {
        ps_me_optimised_function_list->pf_qpel_interp_avg_2pt_vert_with_reuse(
            ps_prms, i4_mv_x, i4_mv_y, ppu1_final, pi4_final_stride);
        break;
    }
    case 6:
    {
        hme_qpel_interp_avg_2pt_vert_no_reuse(
            ps_prms,
            i4_mv_x,
            i4_mv_y,
            ppu1_final,
            pi4_final_stride,
            ps_me_optimised_function_list->pf_qpel_interp_avg_1pt);
        break;
    }
    case 7:
    {
        S32 i4_mv_x_frac, i4_mv_y_frac, i4_offset;
        qpel_input_buf_cfg_t *ps_inp_cfg;

        S32 i4_mvyp1 = (i4_mv_y + 1);
        S32 i4_mvym1 = (i4_mv_y - 1);

        i4_mv_x_frac = dx;
        i4_mv_y_frac = i4_mvyp1 & 3;

        i4_offset = (i4_mv_x >> 2) + (i4_mvyp1 >> 2) * i4_ref_stride;

        /* Derive the descriptor that has all offset and size info */
        ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

        ppu1_final[3] = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ppu1_final[3] += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ppu1_final[3] += (ps_inp_cfg->i1_buf_yoff1 * i4_ref_stride);
        pi4_final_stride[3] = i4_ref_stride;

        i4_mv_y_frac = i4_mvym1 & 3;

        i4_offset = (i4_mv_x >> 2) + (i4_mvym1 >> 2) * i4_ref_stride;

        /* Derive the descriptor that has all offset and size info */
        ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

        ppu1_final[1] = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ppu1_final[1] += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ppu1_final[1] += (ps_inp_cfg->i1_buf_yoff1 * i4_ref_stride);
        pi4_final_stride[1] = i4_ref_stride;

        break;
    }
    }

    /* case descriptions */
    /* Let L = (gridmask & L) & R = (gridmask & R) */
    /* & hp = pt is an hpel or an fpel */
    /* & r = reuse possible */
    /* 0 => L || R = 0 */
    /* 1 => (!L) && (R) && hp */
    /* 2 => (L) && (!R) && hp */
    /* 3 => (!L) && (R) && !hp */
    /* 4 => (L) && (!R) && !hp */
    /* 5 => (L) && (R) && !hp && r */
    /* 6 => (L) && (R) && !hp && !r */
    /* 7 => (L) && (R) && hp */

    switch(horz_func_selector)
    {
    case 0:
    {
        break;
    }
    case 1:
    {
        S32 i4_mv_x_frac, i4_mv_y_frac, i4_offset;
        qpel_input_buf_cfg_t *ps_inp_cfg;
        S32 i4_mvxp1 = (i4_mv_x + 1);

        i4_mv_x_frac = i4_mvxp1 & 3;
        i4_mv_y_frac = dy;

        i4_offset = (i4_mvxp1 >> 2) + (i4_mv_y >> 2) * i4_ref_stride;

        /* Derive the descriptor that has all offset and size info */
        ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

        ppu1_final[2] = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ppu1_final[2] += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ppu1_final[2] += (ps_inp_cfg->i1_buf_yoff1 * i4_ref_stride);
        pi4_final_stride[2] = i4_ref_stride;

        break;
    }
    case 2:
    {
        S32 i4_mv_x_frac, i4_mv_y_frac, i4_offset;
        qpel_input_buf_cfg_t *ps_inp_cfg;
        S32 i4_mvxm1 = (i4_mv_x - 1);

        i4_mv_x_frac = i4_mvxm1 & 3;
        i4_mv_y_frac = dy;

        i4_offset = (i4_mvxm1 >> 2) + (i4_mv_y >> 2) * i4_ref_stride;

        /* Derive the descriptor that has all offset and size info */
        ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

        ppu1_final[0] = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ppu1_final[0] += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ppu1_final[0] += (ps_inp_cfg->i1_buf_yoff1 * i4_ref_stride);
        pi4_final_stride[0] = i4_ref_stride;

        break;
    }
    case 3:
    {
        ps_me_optimised_function_list->pf_qpel_interp_avg_1pt(
            ps_prms, i4_mv_x + 1, i4_mv_y, 2, ppu1_final, pi4_final_stride);

        break;
    }
    case 4:
    {
        ps_me_optimised_function_list->pf_qpel_interp_avg_1pt(
            ps_prms, i4_mv_x - 1, i4_mv_y, 0, ppu1_final, pi4_final_stride);

        break;
    }
    case 5:
    {
        ps_me_optimised_function_list->pf_qpel_interp_avg_2pt_horz_with_reuse(
            ps_prms, i4_mv_x, i4_mv_y, ppu1_final, pi4_final_stride);
        break;
    }
    case 6:
    {
        hme_qpel_interp_avg_2pt_horz_no_reuse(
            ps_prms,
            i4_mv_x,
            i4_mv_y,
            ppu1_final,
            pi4_final_stride,
            ps_me_optimised_function_list->pf_qpel_interp_avg_1pt);
        break;
    }
    case 7:
    {
        S32 i4_mv_x_frac, i4_mv_y_frac, i4_offset;
        qpel_input_buf_cfg_t *ps_inp_cfg;

        S32 i4_mvxp1 = (i4_mv_x + 1);
        S32 i4_mvxm1 = (i4_mv_x - 1);

        i4_mv_x_frac = i4_mvxp1 & 3;
        i4_mv_y_frac = dy;

        i4_offset = (i4_mvxp1 >> 2) + (i4_mv_y >> 2) * i4_ref_stride;

        /* Derive the descriptor that has all offset and size info */
        ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

        ppu1_final[2] = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ppu1_final[2] += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ppu1_final[2] += (ps_inp_cfg->i1_buf_yoff1 * i4_ref_stride);
        pi4_final_stride[2] = i4_ref_stride;

        i4_mv_x_frac = i4_mvxm1 & 3;

        i4_offset = (i4_mvxm1 >> 2) + (i4_mv_y >> 2) * i4_ref_stride;

        /* Derive the descriptor that has all offset and size info */
        ps_inp_cfg = &gas_qpel_inp_buf_cfg[i4_mv_y_frac][i4_mv_x_frac];

        ppu1_final[0] = ps_prms->ppu1_ref[ps_inp_cfg->i1_buf_id1];
        ppu1_final[0] += ps_inp_cfg->i1_buf_xoff1 + i4_offset;
        ppu1_final[0] += (ps_inp_cfg->i1_buf_yoff1 * i4_ref_stride);
        pi4_final_stride[0] = i4_ref_stride;

        break;
    }
    }
}

/**
********************************************************************************
*  @fn     S32 hme_compute_pred_and_evaluate_bi(hme_subpel_prms_t *ps_prms,
*                                   search_results_t *ps_search_results,
*                                   layer_ctxt_t *ps_curr_layer,
*                                   U08 **ppu1_pred)
*
*
*  @brief  Evaluates the best bipred cost as avg(P0, P1) where P0 and P1 are
*          best L0 and L1 bufs respectively for the entire CU
*
*  @param[in]  ps_prms: subpel prms input to this function
*
*  @param[in] ps_curr_layer: points to the current layer ctxt
*
*  @return The best BI cost of best uni cost, whichever better
********************************************************************************
*/
void hme_compute_pred_and_evaluate_bi(
    inter_cu_results_t *ps_cu_results,
    inter_pu_results_t *ps_pu_results,
    inter_ctb_prms_t *ps_inter_ctb_prms,
    part_type_results_t *ps_part_type_result,
    ULWORD64 *pu8_winning_pred_sigmaXSquare,
    ULWORD64 *pu8_winning_pred_sigmaX,
    ihevce_cmn_opt_func_t *ps_cmn_utils_optimised_function_list,
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list)
{
    /* Idx0 - Uni winner */
    /* Idx1 - Uni runner-up */
    /* Idx2 - Bi winner */
    hme_pred_buf_info_t as_pred_buf_data[3][NUM_INTER_PU_PARTS];
    err_prms_t s_err_prms;
    interp_prms_t s_interp_prms;

    PF_SAD_FXN_T pf_err_compute;

    S32 i, j;
    S32 x_off, y_off, x_pic, y_pic;
    S32 i4_sad_grid;
    U08 e_cu_size;
    S32 i4_part_type;
    U08 u1_cu_size;
    S32 shift;
    S32 x_part, y_part, num_parts;
    S32 inp_stride, ref_stride;
    U08 au1_pred_buf_array_indixes[3];
    S32 cur_iter_best_cost;
    S32 uni_cost, bi_cost, best_cost, tot_cost;
    /* Idx0 - Uni winner */
    /* Idx1 - Bi winner */
    ULWORD64 au8_sigmaX[2][NUM_INTER_PU_PARTS];
    ULWORD64 au8_sigmaXSquared[2][NUM_INTER_PU_PARTS];
#if USE_NOISE_TERM_DURING_BICAND_SEARCH
    S32 i4_noise_term;
#endif

    interp_prms_t *ps_interp_prms = &s_interp_prms;

    S32 best_cand_in_opp_dir_idx = 0;
    S32 is_best_cand_an_intra = 0;
    U08 u1_is_cu_noisy = ps_inter_ctb_prms->u1_is_cu_noisy;
#if USE_NOISE_TERM_DURING_BICAND_SEARCH
    const S32 i4_default_src_wt = ((1 << 15) + (WGHT_DEFAULT >> 1)) / WGHT_DEFAULT;
#endif
    tot_cost = 0;

    /* Start of the CU w.r.t. CTB */
    x_off = ps_cu_results->u1_x_off;
    y_off = ps_cu_results->u1_y_off;

    inp_stride = ps_inter_ctb_prms->i4_inp_stride;
    ref_stride = ps_inter_ctb_prms->i4_rec_stride;

    ps_interp_prms->i4_ref_stride = ref_stride;

    /* Start of the CU w.r.t. Pic 0,0 */
    x_pic = x_off + ps_inter_ctb_prms->i4_ctb_x_off;
    y_pic = y_off + ps_inter_ctb_prms->i4_ctb_y_off;

    u1_cu_size = ps_cu_results->u1_cu_size;
    e_cu_size = u1_cu_size;
    shift = (S32)e_cu_size;
    i4_part_type = ps_part_type_result->u1_part_type;
    num_parts = gau1_num_parts_in_part_type[i4_part_type];

    for(i = 0; i < 3; i++)
    {
        hme_init_pred_buf_info(
            &as_pred_buf_data[i],
            &ps_inter_ctb_prms->s_pred_buf_mngr,
            (ps_part_type_result->as_pu_results->pu.b4_wd + 1) << 2,
            (ps_part_type_result->as_pu_results->pu.b4_ht + 1) << 2,
            (PART_TYPE_T)i4_part_type);

        au1_pred_buf_array_indixes[i] = as_pred_buf_data[i][0].u1_pred_buf_array_id;
    }

    for(j = 0; j < num_parts; j++)
    {
        UWORD8 *apu1_hpel_ref[2][4];
        PART_ID_T e_part_id;
        BLK_SIZE_T e_blk_size;
        WORD8 i1_ref_idx;
        UWORD8 pred_dir;
        WORD32 ref_offset, inp_offset, wd, ht;
        pu_result_t *ps_pu_node1, *ps_pu_node2, *ps_pu_result;
        mv_t *aps_mv[2];
        UWORD8 num_active_ref_opp;
        UWORD8 num_results_per_part;
        WORD32 luma_weight_ref1, luma_offset_ref1;
        WORD32 luma_weight_ref2, luma_offset_ref2;
        WORD32 pu_node2_found = 0;

        e_part_id = ge_part_type_to_part_id[i4_part_type][j];
        e_blk_size = ge_part_id_to_blk_size[e_cu_size][e_part_id];

        x_part = gas_part_attr_in_cu[e_part_id].u1_x_start << shift;
        y_part = gas_part_attr_in_cu[e_part_id].u1_y_start << shift;

        ref_offset = (x_part + x_pic) + (y_pic + y_part) * ref_stride;
        inp_offset = (x_part + y_part * inp_stride) + ps_cu_results->i4_inp_offset;

        pred_dir = ps_part_type_result->as_pu_results[j].pu.b2_pred_mode;

        ps_pu_node1 = &(ps_part_type_result->as_pu_results[j]);

        if(PRED_L0 == pred_dir)
        {
            i1_ref_idx = ps_pu_node1->pu.mv.i1_l0_ref_idx;
            aps_mv[0] = &(ps_pu_node1->pu.mv.s_l0_mv);

            num_active_ref_opp =
                ps_inter_ctb_prms->u1_num_active_ref_l1 * (ps_inter_ctb_prms->i4_bidir_enabled);
            num_results_per_part = ps_pu_results->u1_num_results_per_part_l0[e_part_id];

            ps_pu_result = ps_pu_results->aps_pu_results[PRED_L0][e_part_id];

            ASSERT(i1_ref_idx >= 0);

            apu1_hpel_ref[0][0] =
                (UWORD8 *)(ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]->s_yuv_buf_desc.pv_y_buf) +
                ref_offset;
            apu1_hpel_ref[0][1] =
                ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]->apu1_y_sub_pel_planes[0] +
                ref_offset;
            apu1_hpel_ref[0][2] =
                ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]->apu1_y_sub_pel_planes[1] +
                ref_offset;
            apu1_hpel_ref[0][3] =
                ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]->apu1_y_sub_pel_planes[2] +
                ref_offset;

            luma_weight_ref1 = (WORD32)ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]
                                   ->s_weight_offset.i2_luma_weight;
            luma_offset_ref1 = (WORD32)ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]
                                   ->s_weight_offset.i2_luma_offset;
        }
        else
        {
            i1_ref_idx = ps_pu_node1->pu.mv.i1_l1_ref_idx;
            aps_mv[0] = &(ps_pu_node1->pu.mv.s_l1_mv);

            ASSERT(i1_ref_idx >= 0);

            num_active_ref_opp =
                ps_inter_ctb_prms->u1_num_active_ref_l0 * (ps_inter_ctb_prms->i4_bidir_enabled);
            num_results_per_part = ps_pu_results->u1_num_results_per_part_l1[e_part_id];

            ps_pu_result = ps_pu_results->aps_pu_results[PRED_L1][e_part_id];

            apu1_hpel_ref[0][0] =
                (UWORD8 *)(ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]->s_yuv_buf_desc.pv_y_buf) +
                ref_offset;
            apu1_hpel_ref[0][1] =
                ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]->apu1_y_sub_pel_planes[0] +
                ref_offset;
            apu1_hpel_ref[0][2] =
                ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]->apu1_y_sub_pel_planes[1] +
                ref_offset;
            apu1_hpel_ref[0][3] =
                ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]->apu1_y_sub_pel_planes[2] +
                ref_offset;

            luma_weight_ref1 = (WORD32)ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]
                                   ->s_weight_offset.i2_luma_weight;
            luma_offset_ref1 = (WORD32)ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]
                                   ->s_weight_offset.i2_luma_offset;
        }

        if(aps_mv[0]->i2_mvx == INTRA_MV)
        {
            uni_cost = ps_pu_node1->i4_tot_cost;
            cur_iter_best_cost = ps_pu_node1->i4_tot_cost;
            best_cost = MIN(uni_cost, cur_iter_best_cost);
            tot_cost += best_cost;
            continue;
        }

        ps_interp_prms->i4_blk_wd = wd = gau1_blk_size_to_wd[e_blk_size];
        ps_interp_prms->i4_blk_ht = ht = gau1_blk_size_to_ht[e_blk_size];
        ps_interp_prms->i4_out_stride = MAX_CU_SIZE;

        if(num_active_ref_opp)
        {
            if(PRED_L0 == pred_dir)
            {
                if(ps_pu_results->u1_num_results_per_part_l1[e_part_id])
                {
                    ps_pu_node2 = ps_pu_results->aps_pu_results[1][e_part_id];
                    pu_node2_found = 1;
                }
            }
            else
            {
                if(ps_pu_results->u1_num_results_per_part_l0[e_part_id])
                {
                    ps_pu_node2 = ps_pu_results->aps_pu_results[0][e_part_id];
                    pu_node2_found = 1;
                }
            }
        }

        if(!pu_node2_found)
        {
            bi_cost = INT_MAX >> 1;

            s_interp_prms.apu1_interp_out[0] = as_pred_buf_data[0][j].pu1_pred;
            ps_interp_prms->ppu1_ref = &apu1_hpel_ref[0][0];

            ps_me_optimised_function_list->pf_qpel_interp_avg_generic(
                ps_interp_prms, aps_mv[0]->i2_mvx, aps_mv[0]->i2_mvy, 0);

            if(ps_interp_prms->pu1_final_out != s_interp_prms.apu1_interp_out[0])
            {
                as_pred_buf_data[0][j].u1_pred_buf_array_id = UCHAR_MAX;
                as_pred_buf_data[0][j].pu1_pred = ps_interp_prms->pu1_final_out;
                as_pred_buf_data[0][j].i4_pred_stride = ps_interp_prms->i4_final_out_stride;
            }

            if(u1_is_cu_noisy && ps_inter_ctb_prms->i4_alpha_stim_multiplier)
            {
                hme_compute_sigmaX_and_sigmaXSquared(
                    as_pred_buf_data[0][j].pu1_pred,
                    as_pred_buf_data[0][j].i4_pred_stride,
                    &au8_sigmaX[0][j],
                    &au8_sigmaXSquared[0][j],
                    ps_interp_prms->i4_blk_wd,
                    ps_interp_prms->i4_blk_ht,
                    ps_interp_prms->i4_blk_wd,
                    ps_interp_prms->i4_blk_ht,
                    0,
                    1);
            }
        }
        else
        {
            i = 0;
            bi_cost = MAX_32BIT_VAL;
            is_best_cand_an_intra = 0;
            best_cand_in_opp_dir_idx = 0;

            pred_dir = ps_pu_node2[i].pu.b2_pred_mode;

            if(PRED_L0 == pred_dir)
            {
                i1_ref_idx = ps_pu_node2[i].pu.mv.i1_l0_ref_idx;
                aps_mv[1] = &(ps_pu_node2[i].pu.mv.s_l0_mv);

                ASSERT(i1_ref_idx >= 0);

                apu1_hpel_ref[1][0] =
                    (UWORD8 *)(ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]
                                   ->s_yuv_buf_desc.pv_y_buf) +
                    ref_offset;  //>ppu1_list_rec_fxfy[0][i1_ref_idx] + ref_offset;
                apu1_hpel_ref[1][1] =
                    ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]->apu1_y_sub_pel_planes[0] +
                    ref_offset;
                apu1_hpel_ref[1][2] =
                    ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]->apu1_y_sub_pel_planes[1] +
                    ref_offset;
                apu1_hpel_ref[1][3] =
                    ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]->apu1_y_sub_pel_planes[2] +
                    ref_offset;

                luma_weight_ref2 = (WORD32)ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]
                                       ->s_weight_offset.i2_luma_weight;
                luma_offset_ref2 = (WORD32)ps_inter_ctb_prms->pps_rec_list_l0[i1_ref_idx]
                                       ->s_weight_offset.i2_luma_offset;
            }
            else
            {
                i1_ref_idx = ps_pu_node2[i].pu.mv.i1_l1_ref_idx;
                aps_mv[1] = &(ps_pu_node2[i].pu.mv.s_l1_mv);

                ASSERT(i1_ref_idx >= 0);

                apu1_hpel_ref[1][0] =
                    (UWORD8 *)(ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]
                                   ->s_yuv_buf_desc.pv_y_buf) +
                    ref_offset;  //>ppu1_list_rec_fxfy[0][i1_ref_idx] + ref_offset;
                apu1_hpel_ref[1][1] =
                    ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]->apu1_y_sub_pel_planes[0] +
                    ref_offset;
                apu1_hpel_ref[1][2] =
                    ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]->apu1_y_sub_pel_planes[1] +
                    ref_offset;
                apu1_hpel_ref[1][3] =
                    ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]->apu1_y_sub_pel_planes[2] +
                    ref_offset;

                luma_weight_ref2 = (WORD32)ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]
                                       ->s_weight_offset.i2_luma_weight;
                luma_offset_ref2 = (WORD32)ps_inter_ctb_prms->pps_rec_list_l1[i1_ref_idx]
                                       ->s_weight_offset.i2_luma_offset;
            }

            if(aps_mv[1]->i2_mvx == INTRA_MV)
            {
                uni_cost = ps_pu_node1->i4_tot_cost;
                cur_iter_best_cost = ps_pu_node2[i].i4_tot_cost;

                if(cur_iter_best_cost < bi_cost)
                {
                    bi_cost = cur_iter_best_cost;
                    best_cand_in_opp_dir_idx = i;
                    is_best_cand_an_intra = 1;
                }

                best_cost = MIN(uni_cost, bi_cost);
                tot_cost += best_cost;
                continue;
            }

            s_interp_prms.apu1_interp_out[0] = as_pred_buf_data[0][j].pu1_pred;
            ps_interp_prms->ppu1_ref = &apu1_hpel_ref[0][0];

            ps_me_optimised_function_list->pf_qpel_interp_avg_generic(
                ps_interp_prms, aps_mv[0]->i2_mvx, aps_mv[0]->i2_mvy, 0);

            if(ps_interp_prms->pu1_final_out != s_interp_prms.apu1_interp_out[0])
            {
                as_pred_buf_data[0][j].u1_pred_buf_array_id = UCHAR_MAX;
                as_pred_buf_data[0][j].pu1_pred = ps_interp_prms->pu1_final_out;
                as_pred_buf_data[0][j].i4_pred_stride = ps_interp_prms->i4_final_out_stride;
            }

            if(u1_is_cu_noisy && ps_inter_ctb_prms->i4_alpha_stim_multiplier)
            {
                hme_compute_sigmaX_and_sigmaXSquared(
                    as_pred_buf_data[0][j].pu1_pred,
                    as_pred_buf_data[0][j].i4_pred_stride,
                    &au8_sigmaX[0][j],
                    &au8_sigmaXSquared[0][j],
                    ps_interp_prms->i4_blk_wd,
                    ps_interp_prms->i4_blk_ht,
                    ps_interp_prms->i4_blk_wd,
                    ps_interp_prms->i4_blk_ht,
                    0,
                    1);
            }

            s_interp_prms.apu1_interp_out[0] = as_pred_buf_data[1][j].pu1_pred;
            ps_interp_prms->ppu1_ref = &apu1_hpel_ref[1][0];

            ps_me_optimised_function_list->pf_qpel_interp_avg_generic(
                ps_interp_prms, aps_mv[1]->i2_mvx, aps_mv[1]->i2_mvy, 0);

            if(ps_interp_prms->pu1_final_out != s_interp_prms.apu1_interp_out[0])
            {
                as_pred_buf_data[1][j].u1_pred_buf_array_id = UCHAR_MAX;
                as_pred_buf_data[1][j].pu1_pred = ps_interp_prms->pu1_final_out;
                as_pred_buf_data[1][j].i4_pred_stride = ps_interp_prms->i4_final_out_stride;
            }

            ps_cmn_utils_optimised_function_list->pf_wt_avg_2d(
                as_pred_buf_data[0][j].pu1_pred,
                as_pred_buf_data[1][j].pu1_pred,
                as_pred_buf_data[0][j].i4_pred_stride,
                as_pred_buf_data[1][j].i4_pred_stride,
                wd,
                ht,
                as_pred_buf_data[2][j].pu1_pred,
                as_pred_buf_data[2][j].i4_pred_stride,
                luma_weight_ref1,
                luma_weight_ref2,
                luma_offset_ref1,
                luma_offset_ref2,
                ps_inter_ctb_prms->wpred_log_wdc);

            if(u1_is_cu_noisy && ps_inter_ctb_prms->i4_alpha_stim_multiplier)
            {
                hme_compute_sigmaX_and_sigmaXSquared(
                    as_pred_buf_data[2][j].pu1_pred,
                    as_pred_buf_data[2][j].i4_pred_stride,
                    &au8_sigmaX[1][j],
                    &au8_sigmaXSquared[1][j],
                    ps_interp_prms->i4_blk_wd,
                    ps_interp_prms->i4_blk_ht,
                    ps_interp_prms->i4_blk_wd,
                    ps_interp_prms->i4_blk_ht,
                    0,
                    1);
            }

            s_err_prms.pu1_inp = (U08 *)ps_inter_ctb_prms->pu1_non_wt_inp + inp_offset;
            s_err_prms.i4_inp_stride = inp_stride;
            s_err_prms.i4_ref_stride = as_pred_buf_data[2][j].i4_pred_stride;
            s_err_prms.i4_part_mask = (ENABLE_2Nx2N);
            s_err_prms.i4_grid_mask = 1;
            s_err_prms.pi4_sad_grid = &i4_sad_grid;
            s_err_prms.i4_blk_wd = wd;
            s_err_prms.i4_blk_ht = ht;
            s_err_prms.pu1_ref = as_pred_buf_data[2][j].pu1_pred;
            s_err_prms.ps_cmn_utils_optimised_function_list = ps_cmn_utils_optimised_function_list;

            if(ps_inter_ctb_prms->u1_use_satd)
            {
                pf_err_compute = compute_satd_8bit;
            }
            else
            {
                pf_err_compute = ps_me_optimised_function_list->pf_evalsad_pt_npu_mxn_8bit;
            }

            pf_err_compute(&s_err_prms);

#if USE_NOISE_TERM_DURING_BICAND_SEARCH
            if(u1_is_cu_noisy && ps_inter_ctb_prms->i4_alpha_stim_multiplier)
            {
                unsigned long u4_shift_val;
                ULWORD64 u8_src_variance, u8_pred_variance, u8_pred_sigmaSquareX;
                ULWORD64 u8_temp_var, u8_temp_var1;
                S32 i4_bits_req;

                S32 i4_q_level = STIM_Q_FORMAT + ALPHA_Q_FORMAT;

                u8_pred_sigmaSquareX = (au8_sigmaX[1][j] * au8_sigmaX[1][j]);
                u8_pred_variance = au8_sigmaXSquared[1][j] - u8_pred_sigmaSquareX;

                if(e_cu_size == CU_8x8)
                {
                    PART_ID_T e_part_id =
                        (PART_ID_T)((PART_ID_NxN_TL) + (x_off & 1) + ((y_off & 1) << 1));

                    u4_shift_val = ihevce_calc_stim_injected_variance(
                        ps_inter_ctb_prms->pu8_part_src_sigmaX,
                        ps_inter_ctb_prms->pu8_part_src_sigmaXSquared,
                        &u8_src_variance,
                        i4_default_src_wt,
                        0,
                        ps_inter_ctb_prms->wpred_log_wdc,
                        e_part_id);
                }
                else
                {
                    u4_shift_val = ihevce_calc_stim_injected_variance(
                        ps_inter_ctb_prms->pu8_part_src_sigmaX,
                        ps_inter_ctb_prms->pu8_part_src_sigmaXSquared,
                        &u8_src_variance,
                        i4_default_src_wt,
                        0,
                        ps_inter_ctb_prms->wpred_log_wdc,
                        e_part_id);
                }

                u8_pred_variance = u8_pred_variance >> u4_shift_val;

                GETRANGE64(i4_bits_req, u8_pred_variance);

                if(i4_bits_req > 27)
                {
                    u8_pred_variance = u8_pred_variance >> (i4_bits_req - 27);
                    u8_src_variance = u8_src_variance >> (i4_bits_req - 27);
                }

                if(u8_src_variance == u8_pred_variance)
                {
                    u8_temp_var = (1 << STIM_Q_FORMAT);
                }
                else
                {
                    u8_temp_var = (2 * u8_src_variance * u8_pred_variance);
                    u8_temp_var = (u8_temp_var * (1 << STIM_Q_FORMAT));
                    u8_temp_var1 =
                        (u8_src_variance * u8_src_variance) + (u8_pred_variance * u8_pred_variance);
                    u8_temp_var = (u8_temp_var + (u8_temp_var1 / 2));
                    u8_temp_var = (u8_temp_var / u8_temp_var1);
                }

                i4_noise_term = (UWORD32)u8_temp_var;

                i4_noise_term *= ps_inter_ctb_prms->i4_alpha_stim_multiplier;

                ASSERT(i4_noise_term >= 0);

                u8_temp_var = i4_sad_grid;
                u8_temp_var *= ((1 << (i4_q_level)) - (i4_noise_term));
                u8_temp_var += (1 << ((i4_q_level)-1));
                i4_sad_grid = (UWORD32)(u8_temp_var >> (i4_q_level));
            }
#endif

            cur_iter_best_cost = i4_sad_grid;
            cur_iter_best_cost += ps_pu_node1->i4_mv_cost;
            cur_iter_best_cost += ps_pu_node2[i].i4_mv_cost;

            if(cur_iter_best_cost < bi_cost)
            {
                bi_cost = cur_iter_best_cost;
                best_cand_in_opp_dir_idx = i;
                is_best_cand_an_intra = 0;
            }
        }

        uni_cost = ps_pu_node1->i4_tot_cost;

#if USE_NOISE_TERM_DURING_BICAND_SEARCH
        if(u1_is_cu_noisy && ps_inter_ctb_prms->i4_alpha_stim_multiplier)
        {
            unsigned long u4_shift_val;
            ULWORD64 u8_src_variance, u8_pred_variance, u8_pred_sigmaSquareX;
            ULWORD64 u8_temp_var, u8_temp_var1;
            S32 i4_bits_req;

            S32 i4_q_level = STIM_Q_FORMAT + ALPHA_Q_FORMAT;

            S08 i1_ref_idx =
                (PRED_L0 == ps_pu_node1->pu.b2_pred_mode)
                    ? ps_inter_ctb_prms->pi1_past_list[ps_pu_node1->pu.mv.i1_l0_ref_idx]
                    : ps_inter_ctb_prms->pi1_future_list[ps_pu_node1->pu.mv.i1_l1_ref_idx];
            S32 i4_sad = ps_pu_node1->i4_tot_cost - ps_pu_node1->i4_mv_cost;

            u8_pred_sigmaSquareX = (au8_sigmaX[0][j] * au8_sigmaX[0][j]);
            u8_pred_variance = au8_sigmaXSquared[0][j] - u8_pred_sigmaSquareX;

            if(e_cu_size == CU_8x8)
            {
                PART_ID_T e_part_id =
                    (PART_ID_T)((PART_ID_NxN_TL) + (x_off & 1) + ((y_off & 1) << 1));

                u4_shift_val = ihevce_calc_stim_injected_variance(
                    ps_inter_ctb_prms->pu8_part_src_sigmaX,
                    ps_inter_ctb_prms->pu8_part_src_sigmaXSquared,
                    &u8_src_variance,
                    ps_inter_ctb_prms->pi4_inv_wt[i1_ref_idx],
                    ps_inter_ctb_prms->pi4_inv_wt_shift_val[i1_ref_idx],
                    ps_inter_ctb_prms->wpred_log_wdc,
                    e_part_id);
            }
            else
            {
                u4_shift_val = ihevce_calc_stim_injected_variance(
                    ps_inter_ctb_prms->pu8_part_src_sigmaX,
                    ps_inter_ctb_prms->pu8_part_src_sigmaXSquared,
                    &u8_src_variance,
                    ps_inter_ctb_prms->pi4_inv_wt[i1_ref_idx],
                    ps_inter_ctb_prms->pi4_inv_wt_shift_val[i1_ref_idx],
                    ps_inter_ctb_prms->wpred_log_wdc,
                    e_part_id);
            }

            u8_pred_variance = u8_pred_variance >> (u4_shift_val);

            GETRANGE64(i4_bits_req, u8_pred_variance);

            if(i4_bits_req > 27)
            {
                u8_pred_variance = u8_pred_variance >> (i4_bits_req - 27);
                u8_src_variance = u8_src_variance >> (i4_bits_req - 27);
            }

            if(u8_src_variance == u8_pred_variance)
            {
                u8_temp_var = (1 << STIM_Q_FORMAT);
            }
            else
            {
                u8_temp_var = (2 * u8_src_variance * u8_pred_variance);
                u8_temp_var = (u8_temp_var * (1 << STIM_Q_FORMAT));
                u8_temp_var1 =
                    (u8_src_variance * u8_src_variance) + (u8_pred_variance * u8_pred_variance);
                u8_temp_var = (u8_temp_var + (u8_temp_var1 / 2));
                u8_temp_var = (u8_temp_var / u8_temp_var1);
            }

            i4_noise_term = (UWORD32)u8_temp_var;

            i4_noise_term *= ps_inter_ctb_prms->i4_alpha_stim_multiplier;

            ASSERT(i4_noise_term >= 0);

            u8_temp_var = i4_sad;
            u8_temp_var *= ((1 << (i4_q_level)) - (i4_noise_term));
            u8_temp_var += (1 << ((i4_q_level)-1));
            i4_sad = (UWORD32)(u8_temp_var >> (i4_q_level));

            uni_cost = i4_sad + ps_pu_node1->i4_mv_cost;

            pu8_winning_pred_sigmaX[j] = au8_sigmaX[0][j];
            pu8_winning_pred_sigmaXSquare[j] = au8_sigmaXSquared[0][j];
        }
#endif

        if((bi_cost < uni_cost) && (!is_best_cand_an_intra))
        {
            if(u1_is_cu_noisy && ps_inter_ctb_prms->i4_alpha_stim_multiplier)
            {
                pu8_winning_pred_sigmaX[j] = au8_sigmaX[1][j];
                pu8_winning_pred_sigmaXSquare[j] = au8_sigmaXSquared[1][j];
            }

            if(PRED_L0 == ps_pu_node1->pu.b2_pred_mode)
            {
                ps_pu_node1->pu.b2_pred_mode = PRED_BI;

                if(PRED_L0 == ps_pu_node2[best_cand_in_opp_dir_idx].pu.b2_pred_mode)
                {
                    ps_pu_node1->pu.mv.i1_l1_ref_idx =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.i1_l0_ref_idx;
                    ps_pu_node1->pu.mv.s_l1_mv.i2_mvx =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.s_l0_mv.i2_mvx;
                    ps_pu_node1->pu.mv.s_l1_mv.i2_mvy =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.s_l0_mv.i2_mvy;
                }
                else
                {
                    ps_pu_node1->pu.mv.i1_l1_ref_idx =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.i1_l1_ref_idx;
                    ps_pu_node1->pu.mv.s_l1_mv.i2_mvx =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.s_l1_mv.i2_mvx;
                    ps_pu_node1->pu.mv.s_l1_mv.i2_mvy =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.s_l1_mv.i2_mvy;
                }
            }
            else
            {
                ps_pu_node1->pu.b2_pred_mode = PRED_BI;

                if(PRED_L0 == ps_pu_node2[best_cand_in_opp_dir_idx].pu.b2_pred_mode)
                {
                    ps_pu_node1->pu.mv.i1_l0_ref_idx =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.i1_l0_ref_idx;
                    ps_pu_node1->pu.mv.s_l0_mv.i2_mvx =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.s_l0_mv.i2_mvx;
                    ps_pu_node1->pu.mv.s_l0_mv.i2_mvy =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.s_l0_mv.i2_mvy;
                }
                else
                {
                    ps_pu_node1->pu.mv.i1_l0_ref_idx =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.i1_l1_ref_idx;
                    ps_pu_node1->pu.mv.s_l0_mv.i2_mvx =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.s_l1_mv.i2_mvx;
                    ps_pu_node1->pu.mv.s_l0_mv.i2_mvy =
                        ps_pu_node2[best_cand_in_opp_dir_idx].pu.mv.s_l1_mv.i2_mvy;
                }
            }

            ps_part_type_result->as_pu_results[j].i4_tot_cost = bi_cost;
        }

        best_cost = MIN(uni_cost, bi_cost);
        tot_cost += best_cost;
    }

    hme_debrief_bipred_eval(
        ps_part_type_result,
        as_pred_buf_data,
        &ps_inter_ctb_prms->s_pred_buf_mngr,
        au1_pred_buf_array_indixes,
        ps_cmn_utils_optimised_function_list);

    ps_part_type_result->i4_tot_cost = tot_cost;
}

WORD32 hme_evalsatd_pt_pu_8x8_tu_rec(
    err_prms_t *ps_prms,
    WORD32 lambda,
    WORD32 lambda_q_shift,
    WORD32 i4_frm_qstep,
    me_func_selector_t *ps_func_selector)
{
    S32 ai4_satd_4x4[4]; /* num 4x4s in a 8x8 */
    S32 i4_satd_8x8;
    S16 *pi2_had_out;
    S32 i4_tu_split_flag = 0;
    S32 i4_tu_early_cbf = 0;

    S32 i4_early_cbf = 1;
    //  S32 i4_i, i4_k;
    S32 i4_total_satd_cost = 0;
    S32 best_cost_tu_split;

    /* Initialize array of ptrs to hold partial SATDs at all levels of 16x16 */
    S32 *api4_satd_pu[HAD_32x32 + 1];
    S32 *api4_tu_split[HAD_32x32 + 1];
    S32 *api4_tu_early_cbf[HAD_32x32 + 1];

    S32 *pi4_sad_grid = ps_prms->pi4_sad_grid;
    S32 *pi4_tu_split = ps_prms->pi4_tu_split_flags;
    S32 *pi4_early_cbf = ps_prms->pi4_tu_early_cbf;

    U08 *pu1_inp = ps_prms->pu1_inp;
    U08 *pu1_ref = ps_prms->pu1_ref;

    S32 inp_stride = ps_prms->i4_inp_stride;
    S32 ref_stride = ps_prms->i4_ref_stride;

    /* Initialize tu_split_cost to "0" */
    ps_prms->i4_tu_split_cost = 0;
    pi2_had_out = (S16 *)ps_prms->pu1_wkg_mem;

    api4_satd_pu[HAD_4x4] = &ai4_satd_4x4[0];
    api4_satd_pu[HAD_8x8] = &i4_satd_8x8;
    api4_satd_pu[HAD_16x16] = NULL;
    api4_satd_pu[HAD_32x32] = NULL; /* 32x32 not used for 16x16 subpel refine */

    api4_tu_split[HAD_4x4] = NULL;
    api4_tu_split[HAD_8x8] = &i4_tu_split_flag;
    api4_tu_split[HAD_16x16] = NULL;
    api4_tu_split[HAD_32x32] = NULL; /* 32x32 not used for 16x16 subpel refine */

    api4_tu_early_cbf[HAD_4x4] = NULL;
    api4_tu_early_cbf[HAD_8x8] = &i4_tu_early_cbf;
    api4_tu_early_cbf[HAD_16x16] = NULL;
    api4_tu_early_cbf[HAD_32x32] = NULL; /* 32x32 not used for 16x16 subpel refine */

    /* Call recursive 16x16 HAD module; updates satds for 4x4, 8x8 and 16x16 */

    /* Return value is merge of both best_stad_cost and tu_split_flags */
    best_cost_tu_split = ps_func_selector->pf_had_8x8_using_4_4x4_r(
        pu1_inp,
        inp_stride,
        pu1_ref,
        ref_stride,
        pi2_had_out,
        8,
        api4_satd_pu,
        api4_tu_split,
        api4_tu_early_cbf,
        0,
        2,
        0,
        0,
        i4_frm_qstep,
        0,
        ps_prms->u1_max_tr_depth,
        ps_prms->u1_max_tr_size,
        &(ps_prms->i4_tu_split_cost),
        NULL);

    /* For SATD computation following TU size are assumed for a 8x8 CU */
    /* 8 for 2Nx2N, 4 for Nx2N,2NxN                                    */

    i4_total_satd_cost = best_cost_tu_split >> 2;

    /* Second last bit has the tu pslit flag */
    i4_tu_split_flag = (best_cost_tu_split & 0x3) >> 1;

    /* Last bit corrsponds to the Early CBF flag */
    i4_early_cbf = (best_cost_tu_split & 0x1);

    /* Update 8x8 SATDs */
    pi4_sad_grid[PART_ID_2Nx2N] = i4_satd_8x8;
    pi4_tu_split[PART_ID_2Nx2N] = i4_tu_split_flag;
    pi4_early_cbf[PART_ID_2Nx2N] = i4_early_cbf;

    return i4_total_satd_cost;
}
//#endif
/**
********************************************************************************
*  @fn     S32 hme_evalsatd_update_1_best_result_pt_pu_16x16
*
*  @brief  Evaluates the SATD with partial updates for all the best partitions
*          of a 16x16 CU based on recursive Hadamard 16x16, 8x8 and 4x4 satds
*
*  @param[inout]  ps_prms: error prms containg current and ref ptr, strides,
*                 pointer to sad grid of each partitions
*
*  @return     None
********************************************************************************
*/

void hme_evalsatd_update_2_best_results_pt_pu_16x16(
    err_prms_t *ps_prms, result_upd_prms_t *ps_result_prms)
{
    S32 ai4_satd_4x4[16]; /* num 4x4s in a 16x16 */
    S32 ai4_satd_8x8[4]; /* num 8x8s in a 16x16 */
    S32 i4_satd_16x16; /* 16x16 satd cost     */
    S32 i;
    S16 ai2_8x8_had[256];
    S16 *pi2_y0;
    U08 *pu1_src, *pu1_pred;
    S32 pos_x_y_4x4_0, pos_x_y_4x4 = 0;
    S32 *ppi4_hsad;

    /* Initialize array of ptrs to hold partial SATDs at all levels of 16x16 */
    S32 *api4_satd_pu[HAD_32x32 + 1];
    S32 *pi4_sad_grid = ps_prms->pi4_sad_grid;

    U08 *pu1_inp = ps_prms->pu1_inp;
    U08 *pu1_ref = ps_prms->pu1_ref;

    S32 inp_stride = ps_prms->i4_inp_stride;
    S32 ref_stride = ps_prms->i4_ref_stride;

    api4_satd_pu[HAD_4x4] = &ai4_satd_4x4[0];
    api4_satd_pu[HAD_8x8] = &ai4_satd_8x8[0];
    api4_satd_pu[HAD_16x16] = &i4_satd_16x16;
    api4_satd_pu[HAD_32x32] = NULL; /* 32x32 not used for 16x16 subpel refine */

    ppi4_hsad = api4_satd_pu[HAD_16x16];

    /* Call recursive 16x16 HAD module; updates satds for 4x4, 8x8 and 16x16 */
    for(i = 0; i < 4; i++)
    {
        pu1_src = pu1_inp + (i & 0x01) * 8 + (i >> 1) * inp_stride * 8;
        pu1_pred = pu1_ref + (i & 0x01) * 8 + (i >> 1) * ref_stride * 8;
        pi2_y0 = ai2_8x8_had + (i & 0x01) * 8 + (i >> 1) * 16 * 8;
        pos_x_y_4x4_0 = pos_x_y_4x4 + (i & 0x01) * 2 + (i >> 1) * (2 << 16);

        ihevce_had_8x8_using_4_4x4(
            pu1_src, inp_stride, pu1_pred, ref_stride, pi2_y0, 16, api4_satd_pu, pos_x_y_4x4_0, 4);
    }

    /* For SATD computation following TU size are assumed for a 16x16 CU */
    /* 16 for 2Nx2N, 8 for NxN/Nx2N,2NxN and mix of 4 and 8 for AMPs     */

    /* Update 8x8 SATDs */
    /* Modified to cost calculation using only 4x4 SATD */

    //  ai4_satd_8x8[0] = ai4_satd_4x4[0] + ai4_satd_4x4[1] + ai4_satd_4x4[4] + ai4_satd_4x4[5];
    //  ai4_satd_8x8[1] = ai4_satd_4x4[2] + ai4_satd_4x4[3] + ai4_satd_4x4[6] + ai4_satd_4x4[7];
    //  ai4_satd_8x8[2] = ai4_satd_4x4[8] + ai4_satd_4x4[9] + ai4_satd_4x4[12] + ai4_satd_4x4[13];
    //  ai4_satd_8x8[3] = ai4_satd_4x4[10] + ai4_satd_4x4[11] + ai4_satd_4x4[14] + ai4_satd_4x4[15];

    /* Update 16x16 SATDs */
    pi4_sad_grid[PART_ID_2Nx2N] =
        ai4_satd_8x8[0] + ai4_satd_8x8[1] + ai4_satd_8x8[2] + ai4_satd_8x8[3];

    pi4_sad_grid[PART_ID_NxN_TL] = ai4_satd_8x8[0];
    pi4_sad_grid[PART_ID_NxN_TR] = ai4_satd_8x8[1];
    pi4_sad_grid[PART_ID_NxN_BL] = ai4_satd_8x8[2];
    pi4_sad_grid[PART_ID_NxN_BR] = ai4_satd_8x8[3];

    /* Update 8x16 / 16x8 SATDs */
    pi4_sad_grid[PART_ID_Nx2N_L] = ai4_satd_8x8[0] + ai4_satd_8x8[2];
    pi4_sad_grid[PART_ID_Nx2N_R] = ai4_satd_8x8[1] + ai4_satd_8x8[3];
    pi4_sad_grid[PART_ID_2NxN_T] = ai4_satd_8x8[0] + ai4_satd_8x8[1];
    pi4_sad_grid[PART_ID_2NxN_B] = ai4_satd_8x8[2] + ai4_satd_8x8[3];

    /* Update AMP SATDs 16x12,16x4, 12x16,4x16  */
    pi4_sad_grid[PART_ID_nLx2N_L] =
        ai4_satd_4x4[0] + ai4_satd_4x4[4] + ai4_satd_4x4[8] + ai4_satd_4x4[12];

    pi4_sad_grid[PART_ID_nLx2N_R] = ai4_satd_4x4[1] + ai4_satd_4x4[5] + ai4_satd_4x4[9] +
                                    ai4_satd_4x4[13] + pi4_sad_grid[PART_ID_Nx2N_R];

    pi4_sad_grid[PART_ID_nRx2N_L] = ai4_satd_4x4[2] + ai4_satd_4x4[6] + ai4_satd_4x4[10] +
                                    ai4_satd_4x4[14] + pi4_sad_grid[PART_ID_Nx2N_L];

    pi4_sad_grid[PART_ID_nRx2N_R] =
        ai4_satd_4x4[3] + ai4_satd_4x4[7] + ai4_satd_4x4[11] + ai4_satd_4x4[15];

    pi4_sad_grid[PART_ID_2NxnU_T] =
        ai4_satd_4x4[0] + ai4_satd_4x4[1] + ai4_satd_4x4[2] + ai4_satd_4x4[3];

    pi4_sad_grid[PART_ID_2NxnU_B] = ai4_satd_4x4[4] + ai4_satd_4x4[5] + ai4_satd_4x4[6] +
                                    ai4_satd_4x4[7] + pi4_sad_grid[PART_ID_2NxN_B];

    pi4_sad_grid[PART_ID_2NxnD_T] = ai4_satd_4x4[8] + ai4_satd_4x4[9] + ai4_satd_4x4[10] +
                                    ai4_satd_4x4[11] + pi4_sad_grid[PART_ID_2NxN_T];

    pi4_sad_grid[PART_ID_2NxnD_B] =
        ai4_satd_4x4[12] + ai4_satd_4x4[13] + ai4_satd_4x4[14] + ai4_satd_4x4[15];

    /* Call the update results function */
    {
        S32 i4_count = 0, i4_sad, i4_mv_cost, i4_tot_cost;
        mv_refine_ctxt_t *ps_subpel_refine_ctxt = ps_result_prms->ps_subpel_refine_ctxt;
        S32 *pi4_valid_part_ids = &ps_subpel_refine_ctxt->ai4_part_id[0];
        S32 best_node_cost;
        S32 second_best_node_cost;

        /*For each valid partition, update the refine_prm structure to reflect the best and second
        best candidates for that partition*/

        for(i4_count = 0; i4_count < ps_subpel_refine_ctxt->i4_num_valid_parts; i4_count++)
        {
            S32 update_required = 0;
            S32 part_id = pi4_valid_part_ids[i4_count];
            S32 index = (ps_subpel_refine_ctxt->i4_num_valid_parts > 8) ? part_id : i4_count;

            /* Use a pre-computed cost instead of freshly evaluating subpel cost */
            i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[0][index];

            /*Calculate total cost*/
            i4_sad = CLIP3(pi4_sad_grid[part_id], 0, 0x7fff);
            i4_tot_cost = CLIP_S16(i4_sad + i4_mv_cost);

            /*****************************************************************/
            /* We do not labor through the results if the total cost worse   */
            /* than the last of the results.                                 */
            /*****************************************************************/
            best_node_cost = CLIP_S16(ps_subpel_refine_ctxt->i2_tot_cost[0][index]);
            second_best_node_cost = CLIP_S16(ps_subpel_refine_ctxt->i2_tot_cost[1][index]);

            if(i4_tot_cost < second_best_node_cost)
            {
                update_required = 2;

                /*************************************************************/
                /* Identify where the current result isto be placed.Basically*/
                /* find the node which has cost just higher thannodeundertest*/
                /*************************************************************/
                if(i4_tot_cost < best_node_cost)
                {
                    update_required = 1;
                }
                else if(i4_tot_cost == ps_subpel_refine_ctxt->i2_tot_cost[0][index])
                {
                    update_required = 0;
                }
                if(update_required == 2)
                {
                    ps_subpel_refine_ctxt->i2_tot_cost[1][index] = i4_tot_cost;
                    ps_subpel_refine_ctxt->i2_mv_cost[1][index] = i4_mv_cost;
                    ps_subpel_refine_ctxt->i2_mv_x[1][index] = ps_result_prms->i2_mv_x;
                    ps_subpel_refine_ctxt->i2_mv_y[1][index] = ps_result_prms->i2_mv_y;
                    ps_subpel_refine_ctxt->i2_ref_idx[1][index] = ps_result_prms->i1_ref_idx;
                }
                else if(update_required == 1)
                {
                    ps_subpel_refine_ctxt->i2_tot_cost[1][index] =
                        ps_subpel_refine_ctxt->i2_tot_cost[0][index];
                    ps_subpel_refine_ctxt->i2_mv_cost[1][index] =
                        ps_subpel_refine_ctxt->i2_mv_cost[0][index];
                    ps_subpel_refine_ctxt->i2_mv_x[1][index] =
                        ps_subpel_refine_ctxt->i2_mv_x[0][index];
                    ps_subpel_refine_ctxt->i2_mv_y[1][index] =
                        ps_subpel_refine_ctxt->i2_mv_y[0][index];
                    ps_subpel_refine_ctxt->i2_ref_idx[1][index] =
                        ps_subpel_refine_ctxt->i2_ref_idx[0][index];

                    ps_subpel_refine_ctxt->i2_tot_cost[0][index] = i4_tot_cost;
                    ps_subpel_refine_ctxt->i2_mv_cost[0][index] = i4_mv_cost;
                    ps_subpel_refine_ctxt->i2_mv_x[0][index] = ps_result_prms->i2_mv_x;
                    ps_subpel_refine_ctxt->i2_mv_y[0][index] = ps_result_prms->i2_mv_y;
                    ps_subpel_refine_ctxt->i2_ref_idx[0][index] = ps_result_prms->i1_ref_idx;
                }
            }
        }
    }
}

//#if COMPUTE_16x16_R == C
void hme_evalsatd_update_1_best_result_pt_pu_16x16(
    err_prms_t *ps_prms, result_upd_prms_t *ps_result_prms)
{
    S32 ai4_satd_4x4[16]; /* num 4x4s in a 16x16 */
    S32 ai4_satd_8x8[4]; /* num 8x8s in a 16x16 */
    S32 i4_satd_16x16; /* 16x16 satd cost     */
    S32 i;
    S16 ai2_8x8_had[256];
    S16 *pi2_y0;
    U08 *pu1_src, *pu1_pred;
    S32 pos_x_y_4x4_0, pos_x_y_4x4 = 0;
    S32 *ppi4_hsad;

    /* Initialize array of ptrs to hold partial SATDs at all levels of 16x16 */
    S32 *api4_satd_pu[HAD_32x32 + 1];
    S32 *pi4_sad_grid = ps_prms->pi4_sad_grid;

    U08 *pu1_inp = ps_prms->pu1_inp;
    U08 *pu1_ref = ps_prms->pu1_ref;

    S32 inp_stride = ps_prms->i4_inp_stride;
    S32 ref_stride = ps_prms->i4_ref_stride;

    api4_satd_pu[HAD_4x4] = &ai4_satd_4x4[0];
    api4_satd_pu[HAD_8x8] = &ai4_satd_8x8[0];
    api4_satd_pu[HAD_16x16] = &i4_satd_16x16;
    api4_satd_pu[HAD_32x32] = NULL; /* 32x32 not used for 16x16 subpel refine */

    ppi4_hsad = api4_satd_pu[HAD_16x16];

    /* Call recursive 16x16 HAD module; updates satds for 4x4, 8x8 and 16x16 */
    for(i = 0; i < 4; i++)
    {
        pu1_src = pu1_inp + (i & 0x01) * 8 + (i >> 1) * inp_stride * 8;
        pu1_pred = pu1_ref + (i & 0x01) * 8 + (i >> 1) * ref_stride * 8;
        pi2_y0 = ai2_8x8_had + (i & 0x01) * 8 + (i >> 1) * 16 * 8;
        pos_x_y_4x4_0 = pos_x_y_4x4 + (i & 0x01) * 2 + (i >> 1) * (2 << 16);

        ihevce_had_8x8_using_4_4x4(
            pu1_src, inp_stride, pu1_pred, ref_stride, pi2_y0, 16, api4_satd_pu, pos_x_y_4x4_0, 4);
    }

    /* For SATD computation following TU size are assumed for a 16x16 CU */
    /* 16 for 2Nx2N, 8 for NxN/Nx2N,2NxN and mix of 4 and 8 for AMPs     */

    /* Update 8x8 SATDs */
    /* Modified to cost calculation using only 4x4 SATD */

    //  ai4_satd_8x8[0] = ai4_satd_4x4[0] + ai4_satd_4x4[1] + ai4_satd_4x4[4] + ai4_satd_4x4[5];
    //  ai4_satd_8x8[1] = ai4_satd_4x4[2] + ai4_satd_4x4[3] + ai4_satd_4x4[6] + ai4_satd_4x4[7];
    //  ai4_satd_8x8[2] = ai4_satd_4x4[8] + ai4_satd_4x4[9] + ai4_satd_4x4[12] + ai4_satd_4x4[13];
    //  ai4_satd_8x8[3] = ai4_satd_4x4[10] + ai4_satd_4x4[11] + ai4_satd_4x4[14] + ai4_satd_4x4[15];

    /* Update 16x16 SATDs */
    pi4_sad_grid[PART_ID_2Nx2N] =
        ai4_satd_8x8[0] + ai4_satd_8x8[1] + ai4_satd_8x8[2] + ai4_satd_8x8[3];

    pi4_sad_grid[PART_ID_NxN_TL] = ai4_satd_8x8[0];
    pi4_sad_grid[PART_ID_NxN_TR] = ai4_satd_8x8[1];
    pi4_sad_grid[PART_ID_NxN_BL] = ai4_satd_8x8[2];
    pi4_sad_grid[PART_ID_NxN_BR] = ai4_satd_8x8[3];

    /* Update 8x16 / 16x8 SATDs */
    pi4_sad_grid[PART_ID_Nx2N_L] = ai4_satd_8x8[0] + ai4_satd_8x8[2];
    pi4_sad_grid[PART_ID_Nx2N_R] = ai4_satd_8x8[1] + ai4_satd_8x8[3];
    pi4_sad_grid[PART_ID_2NxN_T] = ai4_satd_8x8[0] + ai4_satd_8x8[1];
    pi4_sad_grid[PART_ID_2NxN_B] = ai4_satd_8x8[2] + ai4_satd_8x8[3];

    /* Update AMP SATDs 16x12,16x4, 12x16,4x16  */
    pi4_sad_grid[PART_ID_nLx2N_L] =
        ai4_satd_4x4[0] + ai4_satd_4x4[2] + ai4_satd_4x4[8] + ai4_satd_4x4[10];
    pi4_sad_grid[PART_ID_nRx2N_R] =
        ai4_satd_4x4[5] + ai4_satd_4x4[7] + ai4_satd_4x4[13] + ai4_satd_4x4[15];
    pi4_sad_grid[PART_ID_2NxnU_T] =
        ai4_satd_4x4[0] + ai4_satd_4x4[1] + ai4_satd_4x4[4] + ai4_satd_4x4[5];
    pi4_sad_grid[PART_ID_2NxnD_B] =
        ai4_satd_4x4[10] + ai4_satd_4x4[11] + ai4_satd_4x4[14] + ai4_satd_4x4[15];

    pi4_sad_grid[PART_ID_nLx2N_R] = pi4_sad_grid[PART_ID_2Nx2N] - pi4_sad_grid[PART_ID_nLx2N_L];
    pi4_sad_grid[PART_ID_nRx2N_L] = pi4_sad_grid[PART_ID_2Nx2N] - pi4_sad_grid[PART_ID_nRx2N_R];
    pi4_sad_grid[PART_ID_2NxnU_B] = pi4_sad_grid[PART_ID_2Nx2N] - pi4_sad_grid[PART_ID_2NxnU_T];
    pi4_sad_grid[PART_ID_2NxnD_T] = pi4_sad_grid[PART_ID_2Nx2N] - pi4_sad_grid[PART_ID_2NxnD_B];

    /* Call the update results function */
    {
        S32 i4_count = 0, i4_sad, i4_mv_cost, i4_tot_cost;
        mv_refine_ctxt_t *ps_subpel_refine_ctxt = ps_result_prms->ps_subpel_refine_ctxt;
        S32 *pi4_valid_part_ids = &ps_subpel_refine_ctxt->ai4_part_id[0];
        S32 best_node_cost;
        S32 second_best_node_cost;

        /*For each valid partition, update the refine_prm structure to reflect the best and second
        best candidates for that partition*/

        for(i4_count = 0; i4_count < ps_subpel_refine_ctxt->i4_num_valid_parts; i4_count++)
        {
            S32 update_required = 0;
            S32 part_id = pi4_valid_part_ids[i4_count];
            S32 index = (ps_subpel_refine_ctxt->i4_num_valid_parts > 8) ? part_id : i4_count;

            /* Use a pre-computed cost instead of freshly evaluating subpel cost */
            i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[0][index];

            /*Calculate total cost*/
            i4_sad = CLIP3(pi4_sad_grid[part_id], 0, 0x7fff);
            i4_tot_cost = CLIP_S16(i4_sad + i4_mv_cost);

            /*****************************************************************/
            /* We do not labor through the results if the total cost worse   */
            /* than the last of the results.                                 */
            /*****************************************************************/
            best_node_cost = CLIP_S16(ps_subpel_refine_ctxt->i2_tot_cost[0][index]);
            second_best_node_cost = SHRT_MAX;

            if(i4_tot_cost < second_best_node_cost)
            {
                update_required = 0;

                /*************************************************************/
                /* Identify where the current result isto be placed.Basically*/
                /* find the node which has cost just higher thannodeundertest*/
                /*************************************************************/
                if(i4_tot_cost < best_node_cost)
                {
                    update_required = 1;
                }
                else if(i4_tot_cost == ps_subpel_refine_ctxt->i2_tot_cost[0][index])
                {
                    update_required = 0;
                }
                if(update_required == 2)
                {
                    ps_subpel_refine_ctxt->i2_tot_cost[1][index] = i4_tot_cost;
                    ps_subpel_refine_ctxt->i2_mv_cost[1][index] = i4_mv_cost;
                    ps_subpel_refine_ctxt->i2_mv_x[1][index] = ps_result_prms->i2_mv_x;
                    ps_subpel_refine_ctxt->i2_mv_y[1][index] = ps_result_prms->i2_mv_y;
                    ps_subpel_refine_ctxt->i2_ref_idx[1][index] = ps_result_prms->i1_ref_idx;
                }
                else if(update_required == 1)
                {
                    ps_subpel_refine_ctxt->i2_tot_cost[0][index] = i4_tot_cost;
                    ps_subpel_refine_ctxt->i2_mv_cost[0][index] = i4_mv_cost;
                    ps_subpel_refine_ctxt->i2_mv_x[0][index] = ps_result_prms->i2_mv_x;
                    ps_subpel_refine_ctxt->i2_mv_y[0][index] = ps_result_prms->i2_mv_y;
                    ps_subpel_refine_ctxt->i2_ref_idx[0][index] = ps_result_prms->i1_ref_idx;
                }
            }
        }
    }
}

WORD32 hme_evalsatd_pt_pu_16x16_tu_rec(
    err_prms_t *ps_prms,
    WORD32 lambda,
    WORD32 lambda_q_shift,
    WORD32 i4_frm_qstep,
    me_func_selector_t *ps_func_selector)
{
    S32 ai4_satd_4x4[16]; /* num 4x4s in a 16x16 */
    S32 ai4_satd_8x8[4]; /* num 8x8s in a 16x16 */
    S32 ai4_tu_split_8x8[16];
    S32 i4_satd_16x16; /* 16x16 satd cost     */

    S32 ai4_tu_early_cbf_8x8[16];

    //S16 ai2_had_out[256];
    S16 *pi2_had_out;
    S32 tu_split_flag = 0;
    S32 early_cbf_flag = 0;
    S32 total_satd_cost = 0;

    /* Initialize array of ptrs to hold partial SATDs at all levels of 16x16 */
    S32 *api4_satd_pu[HAD_32x32 + 1];
    S32 *api4_tu_split[HAD_32x32 + 1];
    S32 *api4_tu_early_cbf[HAD_32x32 + 1];

    U08 *pu1_inp = ps_prms->pu1_inp;
    U08 *pu1_ref = ps_prms->pu1_ref;

    S32 inp_stride = ps_prms->i4_inp_stride;
    S32 ref_stride = ps_prms->i4_ref_stride;

    /* Initialize tu_split_cost to "0" */
    ps_prms->i4_tu_split_cost = 0;

    pi2_had_out = (S16 *)ps_prms->pu1_wkg_mem;

    api4_satd_pu[HAD_4x4] = &ai4_satd_4x4[0];
    api4_satd_pu[HAD_8x8] = &ai4_satd_8x8[0];
    api4_satd_pu[HAD_16x16] = &i4_satd_16x16;
    api4_satd_pu[HAD_32x32] = NULL; /* 32x32 not used for 16x16 subpel refine */

    api4_tu_split[HAD_4x4] = NULL;
    api4_tu_split[HAD_8x8] = &ai4_tu_split_8x8[0];
    api4_tu_split[HAD_16x16] = &tu_split_flag;
    api4_tu_split[HAD_32x32] = NULL; /* 32x32 not used for 16x16 subpel refine */

    api4_tu_early_cbf[HAD_4x4] = NULL;
    api4_tu_early_cbf[HAD_8x8] = &ai4_tu_early_cbf_8x8[0];
    api4_tu_early_cbf[HAD_16x16] = &early_cbf_flag;
    api4_tu_early_cbf[HAD_32x32] = NULL; /* 32x32 not used for 16x16 subpel refine */

    /* Call recursive 16x16 HAD module; updates satds for 4x4, 8x8 and 16x16 */
    ps_func_selector->pf_had_16x16_r(
        pu1_inp,
        inp_stride,
        pu1_ref,
        ref_stride,
        pi2_had_out,
        16,
        api4_satd_pu,
        api4_tu_split,
        api4_tu_early_cbf,
        0,
        4,
        lambda,
        lambda_q_shift,
        i4_frm_qstep,
        0,
        ps_prms->u1_max_tr_depth,
        ps_prms->u1_max_tr_size,
        &(ps_prms->i4_tu_split_cost),
        NULL);

    total_satd_cost = i4_satd_16x16;

    ps_prms->pi4_tu_split_flags[0] = tu_split_flag;

    ps_prms->pi4_tu_early_cbf[0] = early_cbf_flag;

    return total_satd_cost;
}

/**
********************************************************************************
*  @fn     S32 hme_evalsatd_pt_pu_32x32
*
*  @brief  Evaluates the SATD with partial updates for all the best partitions
*          of a 32x32 CU based on recursive Hadamard 16x16, 8x8 and 4x4 satds
*
*  @param[inout]  ps_prms: error prms containg current and ref ptr, strides,
*                 pointer to sad grid of each partitions
*
*  @return     None
********************************************************************************
*/
void hme_evalsatd_pt_pu_32x32(err_prms_t *ps_prms)
{
    //S32 ai4_satd_4x4[64];   /* num 4x4s in a 32x32 */
    S32 ai4_satd_8x8[16]; /* num 8x8s in a 32x32 */
    S32 ai4_satd_16x16[4]; /* num 16x16 in a 32x32 */
    S32 i4_satd_32x32;
    //    S16 ai2_had_out[32*32];
    U08 *pu1_src;
    U08 *pu1_pred;
    S32 i;

    /* Initialize array of ptrs to hold partial SATDs at all levels of 16x16 */
    S32 *api4_satd_pu[HAD_32x32 + 1];
    S32 *pi4_sad_grid = ps_prms->pi4_sad_grid;

    U08 *pu1_inp = ps_prms->pu1_inp;
    U08 *pu1_ref = ps_prms->pu1_ref;

    S32 inp_stride = ps_prms->i4_inp_stride;
    S32 ref_stride = ps_prms->i4_ref_stride;

    //api4_satd_pu[HAD_4x4]   = &ai4_satd_4x4[0];
    api4_satd_pu[HAD_8x8] = &ai4_satd_8x8[0];
    api4_satd_pu[HAD_16x16] = &ai4_satd_16x16[0];
    api4_satd_pu[HAD_32x32] = &i4_satd_32x32;

    /* 32x32 SATD is calculates as the sum of the 4 8x8's in the block */
    for(i = 0; i < 16; i++)
    {
        pu1_src = pu1_inp + ((i & 0x3) << 3) + ((i >> 2) * inp_stride * 8);

        pu1_pred = pu1_ref + ((i & 0x3) << 3) + ((i >> 2) * ref_stride * 8);

        ai4_satd_8x8[i] = ps_prms->ps_cmn_utils_optimised_function_list->pf_HAD_8x8_8bit(
            pu1_src, inp_stride, pu1_pred, ref_stride, NULL, 1);
    }

    /* Modified to cost calculation using only 8x8 SATD for 32x32*/
    ai4_satd_16x16[0] = ai4_satd_8x8[0] + ai4_satd_8x8[1] + ai4_satd_8x8[4] + ai4_satd_8x8[5];
    ai4_satd_16x16[1] = ai4_satd_8x8[2] + ai4_satd_8x8[3] + ai4_satd_8x8[6] + ai4_satd_8x8[7];
    ai4_satd_16x16[2] = ai4_satd_8x8[8] + ai4_satd_8x8[9] + ai4_satd_8x8[12] + ai4_satd_8x8[13];
    ai4_satd_16x16[3] = ai4_satd_8x8[10] + ai4_satd_8x8[11] + ai4_satd_8x8[14] + ai4_satd_8x8[15];

    /* Update 32x32 SATD */
    pi4_sad_grid[PART_ID_2Nx2N] =
        ai4_satd_16x16[0] + ai4_satd_16x16[1] + ai4_satd_16x16[2] + ai4_satd_16x16[3];

    /* Update 16x16 SATDs */
    pi4_sad_grid[PART_ID_NxN_TL] = ai4_satd_16x16[0];
    pi4_sad_grid[PART_ID_NxN_TR] = ai4_satd_16x16[1];
    pi4_sad_grid[PART_ID_NxN_BL] = ai4_satd_16x16[2];
    pi4_sad_grid[PART_ID_NxN_BR] = ai4_satd_16x16[3];

    /* Update 16x32 / 32x16 SATDs */
    pi4_sad_grid[PART_ID_Nx2N_L] = ai4_satd_16x16[0] + ai4_satd_16x16[2];
    pi4_sad_grid[PART_ID_Nx2N_R] = ai4_satd_16x16[1] + ai4_satd_16x16[3];
    pi4_sad_grid[PART_ID_2NxN_T] = ai4_satd_16x16[0] + ai4_satd_16x16[1];
    pi4_sad_grid[PART_ID_2NxN_B] = ai4_satd_16x16[2] + ai4_satd_16x16[3];

    /* Update AMP SATDs 32x24,32x8, 24x32,8x32  */
    pi4_sad_grid[PART_ID_nLx2N_L] =
        ai4_satd_8x8[0] + ai4_satd_8x8[4] + ai4_satd_8x8[8] + ai4_satd_8x8[12];

    pi4_sad_grid[PART_ID_nLx2N_R] = ai4_satd_8x8[1] + ai4_satd_8x8[5] + ai4_satd_8x8[9] +
                                    ai4_satd_8x8[13] + pi4_sad_grid[PART_ID_Nx2N_R];

    pi4_sad_grid[PART_ID_nRx2N_L] = ai4_satd_8x8[2] + ai4_satd_8x8[6] + ai4_satd_8x8[10] +
                                    ai4_satd_8x8[14] + pi4_sad_grid[PART_ID_Nx2N_L];

    pi4_sad_grid[PART_ID_nRx2N_R] =
        ai4_satd_8x8[3] + ai4_satd_8x8[7] + ai4_satd_8x8[11] + ai4_satd_8x8[15];

    pi4_sad_grid[PART_ID_2NxnU_T] =
        ai4_satd_8x8[0] + ai4_satd_8x8[1] + ai4_satd_8x8[2] + ai4_satd_8x8[3];

    pi4_sad_grid[PART_ID_2NxnU_B] = ai4_satd_8x8[4] + ai4_satd_8x8[5] + ai4_satd_8x8[6] +
                                    ai4_satd_8x8[7] + pi4_sad_grid[PART_ID_2NxN_B];

    pi4_sad_grid[PART_ID_2NxnD_T] = ai4_satd_8x8[8] + ai4_satd_8x8[9] + ai4_satd_8x8[10] +
                                    ai4_satd_8x8[11] + pi4_sad_grid[PART_ID_2NxN_T];

    pi4_sad_grid[PART_ID_2NxnD_B] =
        ai4_satd_8x8[12] + ai4_satd_8x8[13] + ai4_satd_8x8[14] + ai4_satd_8x8[15];
}

WORD32 hme_evalsatd_pt_pu_32x32_tu_rec(
    err_prms_t *ps_prms,
    WORD32 lambda,
    WORD32 lambda_q_shift,
    WORD32 i4_frm_qstep,
    me_func_selector_t *ps_func_selector)
{
    S32 ai4_satd_4x4[64]; /* num 4x4s in a 32x32 */
    S32 ai4_satd_8x8[16]; /* num 8x8s in a 32x32 */
    S32 ai4_tu_split_8x8[16];
    S32 ai4_satd_16x16[4]; /* num 16x16 in a 32x32 */
    S32 ai4_tu_split_16x16[4];
    S32 i4_satd_32x32;

    S32 ai4_tu_early_cbf_8x8[16];
    S32 ai4_tu_early_cbf_16x16[4];
    S32 early_cbf_flag;

    S16 *pi2_had_out;

    /* Initialize array of ptrs to hold partial SATDs at all levels of 16x16 */
    S32 *api4_satd_pu[HAD_32x32 + 1];
    S32 *api4_tu_split[HAD_32x32 + 1];
    S32 *api4_tu_early_cbf[HAD_32x32 + 1];

    S32 *pi4_sad_grid = ps_prms->pi4_sad_grid;
    S32 *pi4_tu_split_flag = ps_prms->pi4_tu_split_flags;
    S32 *pi4_tu_early_cbf = ps_prms->pi4_tu_early_cbf;

    S32 tu_split_flag = 0;
    S32 total_satd_cost = 0;

    U08 *pu1_inp = ps_prms->pu1_inp;
    U08 *pu1_ref = ps_prms->pu1_ref;

    S32 inp_stride = ps_prms->i4_inp_stride;
    S32 ref_stride = ps_prms->i4_ref_stride;

    /* Initialize tu_split_cost to "0" */
    ps_prms->i4_tu_split_cost = 0;

    pi2_had_out = (S16 *)ps_prms->pu1_wkg_mem;

    api4_satd_pu[HAD_4x4] = &ai4_satd_4x4[0];
    api4_satd_pu[HAD_8x8] = &ai4_satd_8x8[0];
    api4_satd_pu[HAD_16x16] = &ai4_satd_16x16[0];
    api4_satd_pu[HAD_32x32] = &i4_satd_32x32;

    api4_tu_split[HAD_4x4] = NULL;
    api4_tu_split[HAD_8x8] = &ai4_tu_split_8x8[0];
    api4_tu_split[HAD_16x16] = &ai4_tu_split_16x16[0];
    api4_tu_split[HAD_32x32] = &tu_split_flag;

    api4_tu_early_cbf[HAD_4x4] = NULL;
    api4_tu_early_cbf[HAD_8x8] = &ai4_tu_early_cbf_8x8[0];
    api4_tu_early_cbf[HAD_16x16] = &ai4_tu_early_cbf_16x16[0];
    api4_tu_early_cbf[HAD_32x32] = &early_cbf_flag;

    /* Call recursive 32x32 HAD module; updates satds for 4x4, 8x8, 16x16 and 32x32 */
    ihevce_had_32x32_r(
        pu1_inp,
        inp_stride,
        pu1_ref,
        ref_stride,
        pi2_had_out,
        32,
        api4_satd_pu,
        api4_tu_split,
        api4_tu_early_cbf,
        0,
        8,
        lambda,
        lambda_q_shift,
        i4_frm_qstep,
        0,
        ps_prms->u1_max_tr_depth,
        ps_prms->u1_max_tr_size,
        &(ps_prms->i4_tu_split_cost),
        ps_func_selector);

    total_satd_cost = i4_satd_32x32;

    /*The structure of the TU_SPLIT flag for the current 32x32 is as follows
    TL_16x16 - 5bits (4 for child and LSBit for 16x16 split)
    TR_16x16 - 5bits (4 for child and LSBit for 16x16 split)
    BL_16x16 - 5bits (4 for child and LSBit for 16x16 split)
    BR_16x16 - 5bits (4 for child and LSBit for 16x16 split)
    32x32_split - 1bit (LSBit)

    TU_SPLIT : (TL_16x16)_(TR_16x16)_(BL_16x16)_(BR_16x16)_32x32_split (21bits)*/

    pi4_sad_grid[PART_ID_2Nx2N] = total_satd_cost;
    pi4_tu_split_flag[PART_ID_2Nx2N] = tu_split_flag;
    pi4_tu_early_cbf[PART_ID_2Nx2N] = early_cbf_flag;

    return total_satd_cost;
}

/**
********************************************************************************
*  @fn     S32 hme_evalsatd_pt_pu_64x64
*
*  @brief  Evaluates the SATD with partial updates for all the best partitions
*          of a 64x64 CU based on accumulated Hadamard 32x32 and 16x16 satds
*
*           Note : 64x64 SATD does not do hadamard Transform using 32x32 hadamard
*                  outputs but directly uses four 32x32 SATD and 16 16x16 SATDS as
*                  TU size of 64 is not supported in HEVC
*
*  @param[inout]  ps_prms: error prms containg current and ref ptr, strides,
*                 pointer to sad grid of each partitions
*
*  @return     None
********************************************************************************
*/

void hme_evalsatd_pt_pu_64x64(err_prms_t *ps_prms)
{
    //S32 ai4_satd_4x4[4][64];   /* num 4x4s in a 32x32 * num 32x32 in 64x64 */
    S32 ai4_satd_8x8[4][16]; /* num 8x8s in a 32x32 * num 32x32 in 64x64 */
    S32 ai4_satd_16x16[4][4]; /* num 16x16 in a 32x32* num 32x32 in 64x64 */
    S32 ai4_satd_32x32[4]; /* num 32x32 in 64x64 */
    //    S16 ai2_had_out[32*32];
    S32 i, j;

    //  S32 ai4_tu_split_8x8[4][16];
    //  S32 ai4_tu_split_16x16[4][4];
    //  S32 ai4_tu_split_32x32[4];

    /* Initialize array of ptrs to hold partial SATDs at all levels of 16x16 */
    S32 *api4_satd_pu[HAD_32x32 + 1];
    //  S32 *api4_tu_split[HAD_32x32 + 1];

    S32 *pi4_sad_grid = ps_prms->pi4_sad_grid;

    U08 *pu1_inp = ps_prms->pu1_inp;
    U08 *pu1_ref = ps_prms->pu1_ref;
    U08 *pu1_src;
    U08 *pu1_pred;

    S32 inp_stride = ps_prms->i4_inp_stride;
    S32 ref_stride = ps_prms->i4_ref_stride;

    for(i = 0; i < 4; i++)
    {
        S32 blkx = (i & 0x1);
        S32 blky = (i >> 1);
        U08 *pu1_pi0, *pu1_pi1;

        //api4_satd_pu[HAD_4x4]   = &ai4_satd_4x4[i][0];
        api4_satd_pu[HAD_8x8] = &ai4_satd_8x8[i][0];
        api4_satd_pu[HAD_16x16] = &ai4_satd_16x16[i][0];
        api4_satd_pu[HAD_32x32] = &ai4_satd_32x32[i];

        pu1_pi0 = pu1_inp + (blkx * 32) + (blky * 32 * inp_stride);
        pu1_pi1 = pu1_ref + (blkx * 32) + (blky * 32 * ref_stride);

        /* 64x64 SATD is calculates as the sum of the 4 16x16's in the block */
        for(j = 0; j < 16; j++)
        {
            pu1_src = pu1_pi0 + ((j & 0x3) << 3) + ((j >> 2) * inp_stride * 8);

            pu1_pred = pu1_pi1 + ((j & 0x3) << 3) + ((j >> 2) * ref_stride * 8);

            ai4_satd_8x8[i][j] = ps_prms->ps_cmn_utils_optimised_function_list->pf_HAD_8x8_8bit(
                pu1_src, inp_stride, pu1_pred, ref_stride, NULL, 1);
        }

        /* Modified to cost calculation using only 8x8 SATD for 32x32*/
        ai4_satd_16x16[i][0] =
            ai4_satd_8x8[i][0] + ai4_satd_8x8[i][1] + ai4_satd_8x8[i][4] + ai4_satd_8x8[i][5];
        ai4_satd_16x16[i][1] =
            ai4_satd_8x8[i][2] + ai4_satd_8x8[i][3] + ai4_satd_8x8[i][6] + ai4_satd_8x8[i][7];
        ai4_satd_16x16[i][2] =
            ai4_satd_8x8[i][8] + ai4_satd_8x8[i][9] + ai4_satd_8x8[i][12] + ai4_satd_8x8[i][13];
        ai4_satd_16x16[i][3] =
            ai4_satd_8x8[i][10] + ai4_satd_8x8[i][11] + ai4_satd_8x8[i][14] + ai4_satd_8x8[i][15];
    }

    /* Modified to cost calculation using only 8x8 SATD for 32x32*/

    ai4_satd_32x32[0] =
        ai4_satd_16x16[0][0] + ai4_satd_16x16[0][1] + ai4_satd_16x16[0][2] + ai4_satd_16x16[0][3];
    ai4_satd_32x32[1] =
        ai4_satd_16x16[1][0] + ai4_satd_16x16[1][1] + ai4_satd_16x16[1][2] + ai4_satd_16x16[1][3];
    ai4_satd_32x32[2] =
        ai4_satd_16x16[2][0] + ai4_satd_16x16[2][1] + ai4_satd_16x16[2][2] + ai4_satd_16x16[2][3];
    ai4_satd_32x32[3] =
        ai4_satd_16x16[3][0] + ai4_satd_16x16[3][1] + ai4_satd_16x16[3][2] + ai4_satd_16x16[3][3];

    /* Update 64x64 SATDs */
    pi4_sad_grid[PART_ID_2Nx2N] =
        ai4_satd_32x32[0] + ai4_satd_32x32[1] + ai4_satd_32x32[2] + ai4_satd_32x32[3];

    /* Update 32x32 SATDs */
    pi4_sad_grid[PART_ID_NxN_TL] = ai4_satd_32x32[0];
    pi4_sad_grid[PART_ID_NxN_TR] = ai4_satd_32x32[1];
    pi4_sad_grid[PART_ID_NxN_BL] = ai4_satd_32x32[2];
    pi4_sad_grid[PART_ID_NxN_BR] = ai4_satd_32x32[3];

    /* Update 32x64 / 64x32 SATDs */
    pi4_sad_grid[PART_ID_Nx2N_L] = ai4_satd_32x32[0] + ai4_satd_32x32[2];
    pi4_sad_grid[PART_ID_Nx2N_R] = ai4_satd_32x32[1] + ai4_satd_32x32[3];
    pi4_sad_grid[PART_ID_2NxN_T] = ai4_satd_32x32[0] + ai4_satd_32x32[1];
    pi4_sad_grid[PART_ID_2NxN_B] = ai4_satd_32x32[2] + ai4_satd_32x32[3];

    /* Update AMP SATDs 64x48,64x16, 48x64,16x64  */
    pi4_sad_grid[PART_ID_nLx2N_L] =
        ai4_satd_16x16[0][0] + ai4_satd_16x16[0][2] + ai4_satd_16x16[2][0] + ai4_satd_16x16[2][2];

    pi4_sad_grid[PART_ID_nLx2N_R] = ai4_satd_16x16[0][1] + ai4_satd_16x16[0][3] +
                                    ai4_satd_16x16[2][1] + ai4_satd_16x16[2][3] +
                                    pi4_sad_grid[PART_ID_Nx2N_R];

    pi4_sad_grid[PART_ID_nRx2N_L] = ai4_satd_16x16[1][0] + ai4_satd_16x16[1][2] +
                                    ai4_satd_16x16[3][0] + ai4_satd_16x16[3][2] +
                                    pi4_sad_grid[PART_ID_Nx2N_L];

    pi4_sad_grid[PART_ID_nRx2N_R] =
        ai4_satd_16x16[1][1] + ai4_satd_16x16[1][3] + ai4_satd_16x16[3][1] + ai4_satd_16x16[3][3];

    pi4_sad_grid[PART_ID_2NxnU_T] =
        ai4_satd_16x16[0][0] + ai4_satd_16x16[0][1] + ai4_satd_16x16[1][0] + ai4_satd_16x16[1][1];

    pi4_sad_grid[PART_ID_2NxnU_B] = ai4_satd_16x16[0][2] + ai4_satd_16x16[0][3] +
                                    ai4_satd_16x16[1][2] + ai4_satd_16x16[1][3] +
                                    pi4_sad_grid[PART_ID_2NxN_B];

    pi4_sad_grid[PART_ID_2NxnD_T] = ai4_satd_16x16[2][0] + ai4_satd_16x16[2][1] +
                                    ai4_satd_16x16[3][0] + ai4_satd_16x16[3][1] +
                                    pi4_sad_grid[PART_ID_2NxN_T];

    pi4_sad_grid[PART_ID_2NxnD_B] =
        ai4_satd_16x16[2][2] + ai4_satd_16x16[2][3] + ai4_satd_16x16[3][2] + ai4_satd_16x16[3][3];
}

WORD32 hme_evalsatd_pt_pu_64x64_tu_rec(
    err_prms_t *ps_prms,
    WORD32 lambda,
    WORD32 lambda_q_shift,
    WORD32 i4_frm_qstep,
    me_func_selector_t *ps_func_selector)
{
    S32 ai4_satd_4x4[64]; /* num 4x4s in a 32x32 * num 32x32 in 64x64 */
    S32 ai4_satd_8x8[16]; /* num 8x8s in a 32x32 * num 32x32 in 64x64 */
    S32 ai4_satd_16x16[4]; /* num 16x16 in a 32x32* num 32x32 in 64x64 */
    S32 ai4_satd_32x32[4]; /* num 32x32 in 64x64 */

    S32 ai4_tu_split_8x8[16];
    S32 ai4_tu_split_16x16[4];

    S32 ai4_tu_early_cbf_8x8[16];
    S32 ai4_tu_early_cbf_16x16[4];

    S16 *pi2_had_out;
    S32 i;

    /* Initialize array of ptrs to hold partial SATDs at all levels of 16x16 */
    S32 *api4_satd_pu[HAD_32x32 + 1];
    S32 *api4_tu_split[HAD_32x32 + 1];
    S32 *api4_tu_early_cbf[HAD_32x32 + 1];

    S32 *pi4_sad_grid = ps_prms->pi4_sad_grid;

    S32 tu_split_flag = 0;
    S32 total_satd_cost = 0;

    U08 *pu1_inp = ps_prms->pu1_inp;
    U08 *pu1_ref = ps_prms->pu1_ref;

    S32 inp_stride = ps_prms->i4_inp_stride;
    S32 ref_stride = ps_prms->i4_ref_stride;

    /* Initialize tu_split_cost to "0" */
    ps_prms->i4_tu_split_cost = 0;

    pi2_had_out = (S16 *)ps_prms->pu1_wkg_mem;

    for(i = 0; i < 4; i++)
    {
        S32 blkx = (i & 0x1);
        S32 blky = (i >> 1);
        U08 *pu1_pi0, *pu1_pi1;
        tu_split_flag = 0;

        api4_satd_pu[HAD_4x4] = &ai4_satd_4x4[0];
        api4_satd_pu[HAD_8x8] = &ai4_satd_8x8[0];
        api4_satd_pu[HAD_16x16] = &ai4_satd_16x16[0];
        api4_satd_pu[HAD_32x32] = &ai4_satd_32x32[i];

        api4_tu_split[HAD_4x4] = NULL;
        api4_tu_split[HAD_8x8] = &ai4_tu_split_8x8[0];
        api4_tu_split[HAD_16x16] = &ai4_tu_split_16x16[0];
        api4_tu_split[HAD_32x32] = &ps_prms->pi4_tu_split_flags[i];

        api4_tu_early_cbf[HAD_4x4] = NULL;
        api4_tu_early_cbf[HAD_8x8] = &ai4_tu_early_cbf_8x8[0];
        api4_tu_early_cbf[HAD_16x16] = &ai4_tu_early_cbf_16x16[0];
        api4_tu_early_cbf[HAD_32x32] = &ps_prms->pi4_tu_early_cbf[i];

        pu1_pi0 = pu1_inp + (blkx * 32) + (blky * 32 * inp_stride);
        pu1_pi1 = pu1_ref + (blkx * 32) + (blky * 32 * ref_stride);

        /* Call recursive 32x32 HAD module; updates satds for 4x4, 8x8, 16x16 and 32x32 */
        ihevce_had_32x32_r(
            pu1_pi0,
            inp_stride,
            pu1_pi1,
            ref_stride,
            pi2_had_out,
            32,
            api4_satd_pu,
            api4_tu_split,
            api4_tu_early_cbf,
            0,
            8,
            lambda,
            lambda_q_shift,
            i4_frm_qstep,
            1,
            ps_prms->u1_max_tr_depth,
            ps_prms->u1_max_tr_size,
            &(ps_prms->i4_tu_split_cost),
            ps_func_selector);
    }

    total_satd_cost = ai4_satd_32x32[0] + ai4_satd_32x32[1] + ai4_satd_32x32[2] + ai4_satd_32x32[3];

    /* Update 64x64 SATDs */
    pi4_sad_grid[PART_ID_2Nx2N] =
        ai4_satd_32x32[0] + ai4_satd_32x32[1] + ai4_satd_32x32[2] + ai4_satd_32x32[3];

    return total_satd_cost;
}

/**
********************************************************************************
*  @fn     void hme_subpel_refine_search_node(search_node_t *ps_search_node,
*                                   hme_subpel_prms_t *ps_prms,
*                                   layer_ctxt_t *ps_curr_layer,
*                                   BLK_SIZE_T e_blk_size,
*                                   S32 x_off,
*                                   S32 y_off)
*
*  @brief  Refines a given partition within a CU
*
*  @param[in,out]  ps_search_node: supplies starting mv and also ref id.
*                   updated with the accurate subpel mv
*
*  @param[in]  ps_prms: subpel prms input to this function
*
*  @param[in]  ps_curr_layer : layer context
*
*  @param[in]  e_blk_size : Block size enumeration
*
*  @param[in]  x_off : x offset of the partition w.r.t. pic start
*
*  @param[in]  y_off : y offset of the partition w.r.t. pic start
*
*  @return None
********************************************************************************
*/

static __inline PF_SAD_RESULT_FXN_T hme_get_calc_sad_and_result_subpel_fxn(
    me_func_selector_t *ps_func_selector,
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list,
    S32 i4_part_mask,
    U08 u1_use_satd,
    U08 u1_num_parts,
    U08 u1_num_results)
{
    PF_SAD_RESULT_FXN_T pf_err_compute;

    ASSERT((1 == u1_num_results) || (2 == u1_num_results));

    if(1 == u1_num_results)
    {
        if(u1_use_satd)
        {
            if(u1_num_parts == 1)
            {
                pf_err_compute =
                    ps_func_selector->pf_evalsatd_update_1_best_result_pt_pu_16x16_num_part_eq_1;
            }
            else if((u1_num_parts > 1) && (u1_num_parts <= 8))
            {
                pf_err_compute =
                    ps_func_selector->pf_evalsatd_update_1_best_result_pt_pu_16x16_num_part_lt_9;
            }
            else
            {
                pf_err_compute =
                    ps_func_selector->pf_evalsatd_update_1_best_result_pt_pu_16x16_num_part_lt_17;
            }
        }
        else
        {
            if(u1_num_parts == 1)
            {
                pf_err_compute = ps_me_optimised_function_list
                                     ->pf_calc_sad_and_1_best_result_subpel_num_part_eq_1;
            }
            else if(((i4_part_mask & ENABLE_SQUARE_PARTS) != 0) && (u1_num_parts == 5))
            {
                pf_err_compute =
                    ps_me_optimised_function_list->pf_calc_sad_and_1_best_result_subpel_square_parts;
            }
            else if((u1_num_parts > 1) && (u1_num_parts <= 8))
            {
                pf_err_compute = ps_me_optimised_function_list
                                     ->pf_calc_sad_and_1_best_result_subpel_num_part_lt_9;
            }
            else
            {
                pf_err_compute = ps_me_optimised_function_list
                                     ->pf_calc_sad_and_1_best_result_subpel_num_part_lt_17;
            }
        }
    }
    else
    {
        if(u1_use_satd)
        {
            if(u1_num_parts == 1)
            {
                pf_err_compute =
                    ps_func_selector->pf_evalsatd_update_2_best_results_pt_pu_16x16_num_part_eq_1;
            }
            else if((u1_num_parts > 1) && (u1_num_parts <= 8))
            {
                pf_err_compute =
                    ps_func_selector->pf_evalsatd_update_2_best_results_pt_pu_16x16_num_part_lt_9;
            }
            else
            {
                pf_err_compute =
                    ps_func_selector->pf_evalsatd_update_2_best_results_pt_pu_16x16_num_part_lt_17;
            }
        }
        else
        {
            if(u1_num_parts == 1)
            {
                pf_err_compute = ps_me_optimised_function_list
                                     ->pf_calc_sad_and_2_best_results_subpel_num_part_eq_1;
            }
            else if(((i4_part_mask & ENABLE_SQUARE_PARTS) != 0) && (u1_num_parts == 5))
            {
                pf_err_compute = ps_me_optimised_function_list
                                     ->pf_calc_sad_and_2_best_results_subpel_square_parts;
            }
            else if((u1_num_parts > 1) && (u1_num_parts <= 8))
            {
                pf_err_compute = ps_me_optimised_function_list
                                     ->pf_calc_sad_and_2_best_results_subpel_num_part_lt_9;
            }
            else
            {
                pf_err_compute = ps_me_optimised_function_list
                                     ->pf_calc_sad_and_2_best_results_subpel_num_part_lt_17;
            }
        }
    }

    return pf_err_compute;
}

#if DIAMOND_GRID == 1
S32 hme_subpel_refine_search_node_high_speed(
    search_node_t *ps_search_node,
    hme_subpel_prms_t *ps_prms,
    layer_ctxt_t *ps_curr_layer,
    BLK_SIZE_T e_blk_size,
    S32 x_off,
    S32 y_off,
    search_results_t *ps_search_results,
    S32 pred_lx,
    S32 i4_part_mask,
    S32 *pi4_valid_part_ids,
    S32 search_idx,
    subpel_dedup_enabler_t *ps_dedup_enabler,
    me_func_selector_t *ps_func_selector,
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list)
{
    S32 i4_num_hpel_refine, i4_num_qpel_refine;
    S32 i4_offset, i4_grid_mask;
    S08 i1_ref_idx;
    S32 i4_blk_wd, i4_blk_ht;
    S32 i4_ref_stride, i4_i;
    pred_ctxt_t *ps_pred_ctxt = &ps_search_results->as_pred_ctxt[pred_lx];
    result_upd_prms_t s_result_prms;
    search_node_t s_temp_search_node;

    /*************************************************************************/
    /* Tracks current MV with the fractional component.                      */
    /*************************************************************************/
    S32 i4_mv_x, i4_mv_y;
    S32 i4_frac_x, i4_frac_y;

    /*************************************************************************/
    /* Function pointer for SAD/SATD, array and prms structure to pass to    */
    /* This function                                                         */
    /*************************************************************************/
    PF_SAD_RESULT_FXN_T pf_err_compute;

    S32 ai4_sad_grid[17], i4_tot_cost;
    err_prms_t s_err_prms;

    /*************************************************************************/
    /* Allowed MV RANGE                                                      */
    /*************************************************************************/
    range_prms_t *ps_range_prms;

    /*************************************************************************/
    /* stores min id in grid with associated min cost.                       */
    /*************************************************************************/
    S32 i4_min_cost, i4_min_sad;
    GRID_PT_T e_min_id;

    PF_INTERP_FXN_T pf_qpel_interp;
    /*************************************************************************/
    /* For hpel and qpel we move in diamonds and hence each point in the     */
    /* diamond will belong to a completely different plane. To simplify the  */
    /* look up of the ref ptr, we declare a 2x2 array of ref ptrs for the    */
    /* hpel planes which are interpolated during recon.                      */
    /*************************************************************************/
    U08 *apu1_hpel_ref[4], *pu1_ref;

    interp_prms_t s_interp_prms;

    /*************************************************************************/
    /* Maintains the minimum id of interpolated buffers, and the pointer that*/
    /* points to the corresponding predicted buf with its stride.            */
    /* Note that the pointer cannot be derived just from the id, since the   */
    /* pointer may also point to the hpel buffer (in case we request interp  */
    /* of a hpel pt, which already exists in the recon hpel planes)          */
    /*************************************************************************/
    U08 *pu1_final_out;
    S32 i4_final_out_stride;
    S32 part_id;
    S32 check_for_duplicate = 0;

    subpel_refine_ctxt_t *ps_subpel_refine_ctxt = ps_prms->ps_subpel_refine_ctxt;

    S32 mvx_qpel;
    S32 mvy_qpel;

    pf_err_compute = hme_get_calc_sad_and_result_subpel_fxn(
        ps_func_selector,
        ps_me_optimised_function_list,
        i4_part_mask,
        ps_prms->i4_use_satd,
        ps_subpel_refine_ctxt->i4_num_valid_parts,
        ps_search_results->u1_num_results_per_part);

    i4_num_hpel_refine = ps_prms->i4_num_steps_hpel_refine;
    i4_num_qpel_refine = ps_prms->i4_num_steps_qpel_refine;

    /* Prediction contet should now deal with qpel units */
    HME_SET_MVPRED_RES(ps_pred_ctxt, MV_RES_QPEL);

    /* Buffer allocation for subpel */
    /* Current design is that there may be many partitions and different mvs */
    /* that attempt subpel refinemnt. While there is possibility of overlap, the */
    /* hashing to detect and avoid overlap may be very complex. So, currently,   */
    /* the only thing done is to store the eventual predicted buffer with every  */
    /* ctb node that holds the result of hte best subpel search */

    /* Compute the base pointer for input, interpolated buffers */
    /* The base pointers point as follows: */
    /* fx fy : 0, 0 :: fx, hy : 0, 0.5, hx, fy: 0.5, 0, hx, fy: 0.5, 0.5 */
    /* To these, we need to add the offset of the current node */
    i4_ref_stride = ps_curr_layer->i4_rec_stride;
    i4_offset = x_off + (y_off * i4_ref_stride);
    i1_ref_idx = ps_search_node->i1_ref_idx;

    apu1_hpel_ref[0] = ps_curr_layer->ppu1_list_rec_fxfy[i1_ref_idx] + i4_offset;
    apu1_hpel_ref[1] = ps_curr_layer->ppu1_list_rec_hxfy[i1_ref_idx] + i4_offset;
    apu1_hpel_ref[2] = ps_curr_layer->ppu1_list_rec_fxhy[i1_ref_idx] + i4_offset;
    apu1_hpel_ref[3] = ps_curr_layer->ppu1_list_rec_hxhy[i1_ref_idx] + i4_offset;

    /* Initialize result params used for partition update */
    s_result_prms.pf_mv_cost_compute = NULL;
    s_result_prms.ps_search_results = ps_search_results;
    s_result_prms.pi4_valid_part_ids = pi4_valid_part_ids;
    s_result_prms.i1_ref_idx = ps_search_node->i1_ref_idx;
    s_result_prms.u1_pred_lx = search_idx;
    s_result_prms.i4_part_mask = i4_part_mask;
    s_result_prms.ps_search_node_base = ps_search_node;
    s_result_prms.pi4_sad_grid = &ai4_sad_grid[0];
    s_result_prms.i4_grid_mask = 1;
    s_result_prms.ps_search_node = &s_temp_search_node;
    s_temp_search_node.i1_ref_idx = ps_search_node->i1_ref_idx;

    /* convert to hpel units */
    i4_mv_x = ps_search_node->s_mv.i2_mvx >> 1;
    i4_mv_y = ps_search_node->s_mv.i2_mvy >> 1;

    /* for first pt, we compute at all locations in the grid, 4 + 1 centre */
    ps_range_prms = ps_prms->aps_mv_range_qpel[i1_ref_idx];
    i4_grid_mask = (GRID_DIAMOND_ENABLE_ALL);
    i4_grid_mask &= hme_clamp_grid_by_mvrange(ps_search_node, 2, ps_range_prms);

    i4_min_cost = MAX_32BIT_VAL;
    i4_min_sad = MAX_32BIT_VAL;

    /*************************************************************************/
    /* Prepare the input params to SAD/SATD function. Note that input is     */
    /* passed from the calling funcion since it may be I (normal subpel      */
    /* refinement) or 2I - P0 in case of bidirect subpel refinement.         */
    /* Both cases are handled here.                                          */
    /*************************************************************************/
    s_err_prms.pu1_inp = (U08 *)ps_prms->pv_inp;
    s_err_prms.i4_inp_stride = ps_prms->i4_inp_stride;
    s_err_prms.i4_ref_stride = i4_ref_stride;
    s_err_prms.i4_part_mask = (ENABLE_2Nx2N);
    s_err_prms.i4_grid_mask = 1;
    s_err_prms.pi4_sad_grid = &ai4_sad_grid[0];
    s_err_prms.i4_blk_wd = i4_blk_wd = gau1_blk_size_to_wd[e_blk_size];
    s_err_prms.i4_blk_ht = i4_blk_ht = gau1_blk_size_to_ht[e_blk_size];

    s_result_prms.ps_subpel_refine_ctxt = ps_subpel_refine_ctxt;

    part_id = ps_search_node->u1_part_id;
    for(i4_i = 0; i4_i < i4_num_hpel_refine; i4_i++)
    {
        e_min_id = PT_C;

        mvx_qpel = i4_mv_x << 1;
        mvy_qpel = i4_mv_y << 1;

        /* Central pt */
        if(i4_grid_mask & BIT_EN(PT_C))
        {
            //ps_search_node->i2_mv_x = (S16)i4_mv_x;
            //ps_search_node->i2_mv_x = (S16)i4_mv_y;
            /* central pt is i4_mv_x, i4_mv_y */
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, 1, mvx_qpel, mvy_qpel, check_for_duplicate);

            i4_frac_x = i4_mv_x & 1;
            i4_frac_y = i4_mv_y & 1;
            pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
            s_err_prms.pu1_ref = pu1_ref + (i4_mv_x >> 1) + ((i4_mv_y >> 1) * i4_ref_stride);

            /* Update the mv's with the current candt motion vectors */
            s_result_prms.i2_mv_x = mvx_qpel;
            s_result_prms.i2_mv_y = mvy_qpel;
            s_temp_search_node.s_mv.i2_mvx = mvx_qpel;
            s_temp_search_node.s_mv.i2_mvy = mvy_qpel;

            pf_err_compute(&s_err_prms, &s_result_prms);

            i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
            if(i4_tot_cost < i4_min_cost)
            {
                i4_min_cost = i4_tot_cost;
                i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                e_min_id = PT_C;
                pu1_final_out = s_err_prms.pu1_ref;
            }
        }

        /* left pt */
        if(i4_grid_mask & BIT_EN(PT_L))
        {
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, 1, mvx_qpel - 2, mvy_qpel, check_for_duplicate);

            if(!check_for_duplicate)
            {
                /* search node mv is stored in qpel units */
                ps_search_node->s_mv.i2_mvx = (S16)((i4_mv_x - 1) << 1);
                ps_search_node->s_mv.i2_mvy = (S16)(i4_mv_y << 1);
                /* central pt is i4_mv_x - 1, i4_mv_y */
                i4_frac_x = (i4_mv_x - 1) & 1;  // same as (x-1)&1
                i4_frac_y = i4_mv_y & 1;
                pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                s_err_prms.pu1_ref =
                    pu1_ref + ((i4_mv_x - 1) >> 1) + ((i4_mv_y >> 1) * i4_ref_stride);

                /* Update the mv's with the current candt motion vectors */
                s_result_prms.i2_mv_x = mvx_qpel - 2;
                s_result_prms.i2_mv_y = mvy_qpel;
                s_temp_search_node.s_mv.i2_mvx = mvx_qpel - 2;
                s_temp_search_node.s_mv.i2_mvy = mvy_qpel;

                pf_err_compute(&s_err_prms, &s_result_prms);
                //hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
                i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                if(i4_tot_cost < i4_min_cost)
                {
                    i4_min_cost = i4_tot_cost;
                    i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    e_min_id = PT_L;
                    pu1_final_out = s_err_prms.pu1_ref;
                }
            }
        }
        /* top pt */
        if(i4_grid_mask & BIT_EN(PT_T))
        {
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, 1, mvx_qpel, mvy_qpel - 2, check_for_duplicate);

            if(!check_for_duplicate)
            {
                /* search node mv is stored in qpel units */
                ps_search_node->s_mv.i2_mvx = (S16)(i4_mv_x << 1);
                ps_search_node->s_mv.i2_mvy = (S16)((i4_mv_y - 1) << 1);
                /* top pt is i4_mv_x, i4_mv_y - 1 */
                i4_frac_x = i4_mv_x & 1;
                i4_frac_y = (i4_mv_y - 1) & 1;
                pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                s_err_prms.pu1_ref =
                    pu1_ref + (i4_mv_x >> 1) + (((i4_mv_y - 1) >> 1) * i4_ref_stride);

                /* Update the mv's with the current candt motion vectors */
                s_result_prms.i2_mv_x = mvx_qpel;
                s_result_prms.i2_mv_y = mvy_qpel - 2;
                s_temp_search_node.s_mv.i2_mvx = mvx_qpel;
                s_temp_search_node.s_mv.i2_mvy = mvy_qpel - 2;

                pf_err_compute(&s_err_prms, &s_result_prms);
                //hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
                i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                if(i4_tot_cost < i4_min_cost)
                {
                    i4_min_cost = i4_tot_cost;
                    i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    e_min_id = PT_T;
                    pu1_final_out = s_err_prms.pu1_ref;
                }
            }
        }
        /* right pt */
        if(i4_grid_mask & BIT_EN(PT_R))
        {
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, num_unique_nodes, mvx_qpel + 2, mvy_qpel, check_for_duplicate);
            if(!check_for_duplicate)
            {
                /* search node mv is stored in qpel units */
                ps_search_node->s_mv.i2_mvx = (S16)((i4_mv_x + 1) << 1);
                ps_search_node->s_mv.i2_mvy = (S16)(i4_mv_y << 1);
                /* right pt is i4_mv_x + 1, i4_mv_y */
                i4_frac_x = (i4_mv_x + 1) & 1;
                i4_frac_y = i4_mv_y & 1;

                pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                s_err_prms.pu1_ref =
                    pu1_ref + ((i4_mv_x + 1) >> 1) + ((i4_mv_y >> 1) * i4_ref_stride);

                /* Update the mv's with the current candt motion vectors */
                s_result_prms.i2_mv_x = mvx_qpel + 2;
                s_result_prms.i2_mv_y = mvy_qpel;
                s_temp_search_node.s_mv.i2_mvx = mvx_qpel + 2;
                s_temp_search_node.s_mv.i2_mvy = mvy_qpel;

                pf_err_compute(&s_err_prms, &s_result_prms);
                //hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
                i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                if(i4_tot_cost < i4_min_cost)
                {
                    i4_min_cost = i4_tot_cost;
                    i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    e_min_id = PT_R;
                    pu1_final_out = s_err_prms.pu1_ref;
                }
            }
        }
        /* bottom pt */
        if(i4_grid_mask & BIT_EN(PT_B))
        {
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, num_unique_nodes, mvx_qpel, mvy_qpel + 2, check_for_duplicate);
            if(!check_for_duplicate)
            {
                /* search node mv is stored in qpel units */
                ps_search_node->s_mv.i2_mvx = ((S16)i4_mv_x << 1);
                ps_search_node->s_mv.i2_mvy = ((S16)(i4_mv_y + 1) << 1);
                i4_frac_x = i4_mv_x & 1;
                i4_frac_y = (i4_mv_y + 1) & 1;
                pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                s_err_prms.pu1_ref =
                    pu1_ref + (i4_mv_x >> 1) + (((i4_mv_y + 1) >> 1) * i4_ref_stride);

                /* Update the mv's with the current candt motion vectors */
                s_result_prms.i2_mv_x = mvx_qpel;
                s_result_prms.i2_mv_y = mvy_qpel + 2;
                s_temp_search_node.s_mv.i2_mvx = mvx_qpel;
                s_temp_search_node.s_mv.i2_mvy = mvy_qpel + 2;

                pf_err_compute(&s_err_prms, &s_result_prms);
                //hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
                i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                if(i4_tot_cost < i4_min_cost)
                {
                    i4_min_cost = i4_tot_cost;
                    i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    e_min_id = PT_B;
                    pu1_final_out = s_err_prms.pu1_ref;
                }
            }
        }
        /* Early exit in case of central point */
        if(e_min_id == PT_C)
            break;

        /*********************************************************************/
        /* Depending on the best result location, we may be able to skip     */
        /* atleast two pts, centre pt and one more pt. E.g. if right pt is   */
        /* the best result, the next iteration need not do centre, left pts  */
        /*********************************************************************/
        i4_grid_mask = gai4_opt_grid_mask_diamond[e_min_id];
        i4_mv_x += gai1_grid_id_to_x[e_min_id];
        i4_mv_y += gai1_grid_id_to_y[e_min_id];
        ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
        ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;
        i4_grid_mask &= hme_clamp_grid_by_mvrange(ps_search_node, 2, ps_range_prms);
    }

    /* Convert to QPEL units */
    i4_mv_x <<= 1;
    i4_mv_y <<= 1;

    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;

    /* Exact interpolation or averaging chosen here */
    pf_qpel_interp = ps_prms->pf_qpel_interp;

    /* Next QPEL ME */
    /* In this case, we have option of doing exact QPEL interpolation or avg */
    /*************************************************************************/
    /*        x                                                              */
    /*    A b C d                                                            */
    /*    e f g h                                                            */
    /*    I j K l                                                            */
    /*    m n o p                                                            */
    /*    Q r S t                                                            */
    /*                                                                       */
    /*    Approximate QPEL logic                                             */
    /*    b = avg(A,C) f = avg(I,C), g= avg(C,K) j=avg(I,K)                  */
    /*    for any given pt, we can get all the information required about    */
    /*    the surrounding 4 pts. For example, given point C (0.5, 0)         */
    /*     surrounding pts info:                                             */
    /*     b : qpel offset: 1, 0, generated by averaging. buffer1: fpel buf  */
    /*           buffer 2: hxfy, offsets for both are 0, 0                   */
    /*    similarly for other pts the info can be gotten                     */
    /*************************************************************************/
    i4_grid_mask = GRID_DIAMOND_ENABLE_ALL ^ (BIT_EN(PT_C));
    i4_grid_mask &= hme_clamp_grid_by_mvrange(ps_search_node, 1, ps_range_prms);

    /*************************************************************************/
    /* One time preparation of non changing interpolation params. These      */
    /* include a set of ping pong result buf ptrs, input buf ptrs and some   */
    /* working memory (not used though in case of averaging).                */
    /*************************************************************************/
    s_interp_prms.ppu1_ref = &apu1_hpel_ref[0];
    s_interp_prms.i4_ref_stride = i4_ref_stride;
    s_interp_prms.i4_blk_wd = i4_blk_wd;
    s_interp_prms.i4_blk_ht = i4_blk_ht;

    i4_final_out_stride = i4_ref_stride;

    {
        U08 *pu1_mem;
        /*********************************************************************/
        /* Allocation of working memory for interpolated buffers. We maintain*/
        /* an intermediate working buffer, and 2 ping pong interpolated out  */
        /* buffers, purpose of ping pong explained later below               */
        /*********************************************************************/
        pu1_mem = ps_prms->pu1_wkg_mem;
        s_interp_prms.pu1_wkg_mem = pu1_mem;

        //pu1_mem += (INTERP_INTERMED_BUF_SIZE);
        s_interp_prms.apu1_interp_out[0] = pu1_mem;

        pu1_mem += (INTERP_OUT_BUF_SIZE);
        s_interp_prms.apu1_interp_out[1] = pu1_mem;

        pu1_mem += (INTERP_OUT_BUF_SIZE);
        s_interp_prms.apu1_interp_out[2] = pu1_mem;

        pu1_mem += (INTERP_OUT_BUF_SIZE);
        s_interp_prms.apu1_interp_out[3] = pu1_mem;

        pu1_mem += (INTERP_OUT_BUF_SIZE);
        s_interp_prms.apu1_interp_out[4] = pu1_mem;

        /*********************************************************************/
        /* Stride of interpolated output is just a function of blk width of  */
        /* this partition and hence remains constant for this partition      */
        /*********************************************************************/
        s_interp_prms.i4_out_stride = (i4_blk_wd);
    }

    {
        UWORD8 *apu1_final[4];
        WORD32 ai4_ref_stride[4];
        /*************************************************************************/
        /* Ping pong design for interpolated buffers. We use a min id, which     */
        /* tracks the id of the ppu1_interp_out that stores the best result.     */
        /* When new interp to be done, it uses 1 - bes result id to do the interp*/
        /* min id is toggled when any new result becomes the best result.        */
        /*************************************************************************/

        for(i4_i = 0; i4_i < i4_num_qpel_refine; i4_i++)
        {
            e_min_id = PT_C;

            mvx_qpel = i4_mv_x;
            mvy_qpel = i4_mv_y;
            hme_qpel_interp_comprehensive(
                &s_interp_prms,
                apu1_final,
                ai4_ref_stride,
                i4_mv_x,
                i4_mv_y,
                i4_grid_mask,
                ps_me_optimised_function_list);
            if(i4_grid_mask & BIT_EN(PT_L))
            {
                CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                    ps_dedup_enabler,
                    num_unique_nodes,
                    mvx_qpel - 1,
                    mvy_qpel - 0,
                    check_for_duplicate);

                if(!check_for_duplicate)
                {
                    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x - 1;
                    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;

                    s_err_prms.pu1_ref = apu1_final[0];
                    s_err_prms.i4_ref_stride = ai4_ref_stride[0];

                    /* Update the mv's with the current candt motion vectors */
                    s_result_prms.i2_mv_x = mvx_qpel - 1;
                    s_result_prms.i2_mv_y = mvy_qpel;
                    s_temp_search_node.s_mv.i2_mvx = mvx_qpel - 1;
                    s_temp_search_node.s_mv.i2_mvy = mvy_qpel;

                    pf_err_compute(&s_err_prms, &s_result_prms);
                    //hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                    i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                    if(i4_tot_cost < i4_min_cost)
                    {
                        e_min_id = PT_L;
                        i4_min_cost = i4_tot_cost;
                        i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    }
                }
            }
            if(i4_grid_mask & BIT_EN(PT_T))
            {
                CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                    ps_dedup_enabler,
                    num_unique_nodes,
                    mvx_qpel - 0,
                    mvy_qpel - 1,
                    check_for_duplicate);

                if(!check_for_duplicate)
                {
                    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
                    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y - 1;

                    s_err_prms.pu1_ref = apu1_final[1];
                    s_err_prms.i4_ref_stride = ai4_ref_stride[1];

                    /* Update the mv's with the current candt motion vectors */
                    s_result_prms.i2_mv_x = mvx_qpel;
                    s_result_prms.i2_mv_y = mvy_qpel - 1;

                    s_temp_search_node.s_mv.i2_mvx = mvx_qpel;
                    s_temp_search_node.s_mv.i2_mvy = mvy_qpel - 1;

                    pf_err_compute(&s_err_prms, &s_result_prms);

                    //hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
                    i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                    if(i4_tot_cost < i4_min_cost)
                    {
                        e_min_id = PT_T;
                        i4_min_cost = i4_tot_cost;
                        i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    }
                }
            }
            if(i4_grid_mask & BIT_EN(PT_R))
            {
                CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                    ps_dedup_enabler, num_unique_nodes, mvx_qpel + 1, mvy_qpel, check_for_duplicate);

                if(!check_for_duplicate)
                {
                    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x + 1;
                    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;

                    s_err_prms.pu1_ref = apu1_final[2];
                    s_err_prms.i4_ref_stride = ai4_ref_stride[2];

                    /* Update the mv's with the current candt motion vectors */
                    s_result_prms.i2_mv_x = mvx_qpel + 1;
                    s_result_prms.i2_mv_y = mvy_qpel;

                    s_temp_search_node.s_mv.i2_mvx = mvx_qpel + 1;
                    s_temp_search_node.s_mv.i2_mvy = mvy_qpel;

                    pf_err_compute(&s_err_prms, &s_result_prms);

                    //hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                    i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                    if(i4_tot_cost < i4_min_cost)
                    {
                        e_min_id = PT_R;
                        i4_min_cost = i4_tot_cost;
                        i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    }
                }
            }
            /* i4_mv_x and i4_mv_y will always be the centre pt */
            /* for qpel we  start with least hpel, and hence compute of center pt never reqd */
            if(i4_grid_mask & BIT_EN(PT_B))
            {
                CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                    ps_dedup_enabler, num_unique_nodes, mvx_qpel, mvy_qpel + 1, check_for_duplicate);

                if(!check_for_duplicate)
                {
                    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
                    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y + 1;

                    s_err_prms.pu1_ref = apu1_final[3];
                    s_err_prms.i4_ref_stride = ai4_ref_stride[3];

                    /* Update the mv's with the current candt motion vectors */
                    s_result_prms.i2_mv_x = mvx_qpel;
                    s_result_prms.i2_mv_y = mvy_qpel + 1;

                    s_temp_search_node.s_mv.i2_mvx = mvx_qpel;
                    s_temp_search_node.s_mv.i2_mvy = mvy_qpel + 1;

                    pf_err_compute(&s_err_prms, &s_result_prms);

                    //hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
                    i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                    if(i4_tot_cost < i4_min_cost)
                    {
                        e_min_id = PT_B;
                        i4_min_cost = i4_tot_cost;
                        i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    }
                }
            }

            /* New QPEL mv x and y */
            if(e_min_id == PT_C)
                break;
            i4_grid_mask = gai4_opt_grid_mask_diamond[e_min_id];
            i4_mv_x += gai1_grid_id_to_x[e_min_id];
            i4_mv_y += gai1_grid_id_to_y[e_min_id];
            ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
            ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;
            i4_grid_mask &= hme_clamp_grid_by_mvrange(ps_search_node, 1, ps_range_prms);
        }
    }

    /* update modified motion vectors and cost at end of subpel */
    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;
    ps_search_node->i4_tot_cost = i4_min_cost;
    ps_search_node->i4_sad = i4_min_sad;

    /********************************************************************************/
    /* TODO: Restoring back Sad lambda from Hadamard lambda                         */
    /* Need to pass the had/satd lambda in more cleaner way for subpel cost compute */
    /********************************************************************************/
    //ps_pred_ctxt->lambda >>= 1;

    return (i4_min_cost);
}
#elif DIAMOND_GRID == 0
S32 hme_subpel_refine_search_node_high_speed(
    search_node_t *ps_search_node,
    hme_subpel_prms_t *ps_prms,
    layer_ctxt_t *ps_curr_layer,
    BLK_SIZE_T e_blk_size,
    S32 x_off,
    S32 y_off,
    search_results_t *ps_search_results,
    S32 pred_lx,
    S32 i4_part_mask,
    S32 *pi4_valid_part_ids,
    S32 search_idx,
    subpel_dedup_enabler_t *ps_dedup_enabler,
    me_func_selector_t *ps_func_selector)
{
    S32 i4_num_hpel_refine, i4_num_qpel_refine;
    S32 i4_offset, i4_grid_mask;
    S08 i1_ref_idx;
    S32 i4_blk_wd, i4_blk_ht;
    S32 i4_ref_stride, i4_i;
    pred_ctxt_t *ps_pred_ctxt = &ps_search_results->as_pred_ctxt[pred_lx];
    result_upd_prms_t s_result_prms;

    /*************************************************************************/
    /* Tracks current MV with the fractional component.                      */
    /*************************************************************************/
    S32 i4_mv_x, i4_mv_y;
    S32 i4_frac_x, i4_frac_y;

    /*************************************************************************/
    /* Function pointer for SAD/SATD, array and prms structure to pass to    */
    /* This function                                                         */
    /*************************************************************************/
    PF_SAD_FXN_T pf_err_compute;
    S32 ai4_sad_grid[9][17], i4_tot_cost;
    err_prms_t s_err_prms;

    /*************************************************************************/
    /* Allowed MV RANGE                                                      */
    /*************************************************************************/
    range_prms_t *ps_range_prms;

    /*************************************************************************/
    /* stores min id in grid with associated min cost.                       */
    /*************************************************************************/
    S32 i4_min_cost, i4_min_sad;
    GRID_PT_T e_min_id;

    PF_INTERP_FXN_T pf_qpel_interp;
    /*************************************************************************/
    /* For hpel and qpel we move in diamonds and hence each point in the     */
    /* diamond will belong to a completely different plane. To simplify the  */
    /* look up of the ref ptr, we declare a 2x2 array of ref ptrs for the    */
    /* hpel planes which are interpolated during recon.                      */
    /*************************************************************************/
    U08 *apu1_hpel_ref[4], *pu1_ref;

    interp_prms_t s_interp_prms;

    /*************************************************************************/
    /* Maintains the minimum id of interpolated buffers, and the pointer that*/
    /* points to the corresponding predicted buf with its stride.            */
    /* Note that the pointer cannot be derived just from the id, since the   */
    /* pointer may also point to the hpel buffer (in case we request interp  */
    /* of a hpel pt, which already exists in the recon hpel planes)          */
    /*************************************************************************/
    U08 *pu1_final_out;
    S32 i4_final_out_stride;
    S32 part_id;
    S32 check_for_duplicate = 0;

    S32 mvx_qpel;
    S32 mvy_qpel;

    /*************************************************************************/
    /* Appropriate Err compute fxn, depends on SAD/SATD, blk size and remains*/
    /* fixed through this subpel refinement for this partition.              */
    /* Note, we do not enable grid sads since each pt is different buffers.  */
    /* Hence, part mask is also nearly dont care and we use 2Nx2N enabled.   */
    /*************************************************************************/
    if(ps_prms->i4_use_satd)
    {
        pf_err_compute = hme_evalsatd_update_1_best_result_pt_pu_16x16;
    }
    else
    {
        pf_err_compute = hme_evalsad_grid_pu_16x16; /* hme_evalsad_pt_pu_16x16; */
    }

    i4_num_hpel_refine = ps_prms->i4_num_steps_hpel_refine;
    i4_num_qpel_refine = ps_prms->i4_num_steps_qpel_refine;

    /* Prediction contet should now deal with qpel units */
    HME_SET_MVPRED_RES(ps_pred_ctxt, MV_RES_QPEL);

    /* Buffer allocation for subpel */
    /* Current design is that there may be many partitions and different mvs */
    /* that attempt subpel refinemnt. While there is possibility of overlap, the */
    /* hashing to detect and avoid overlap may be very complex. So, currently,   */
    /* the only thing done is to store the eventual predicted buffer with every  */
    /* ctb node that holds the result of hte best subpel search */

    /* Compute the base pointer for input, interpolated buffers */
    /* The base pointers point as follows:
    /* fx fy : 0, 0 :: fx, hy : 0, 0.5, hx, fy: 0.5, 0, hx, fy: 0.5, 0.5 */
    /* To these, we need to add the offset of the current node */
    i4_ref_stride = ps_curr_layer->i4_rec_stride;
    i4_offset = x_off + (y_off * i4_ref_stride);
    i1_ref_idx = ps_search_node->i1_ref_idx;

    apu1_hpel_ref[0] = ps_curr_layer->ppu1_list_rec_fxfy[i1_ref_idx] + i4_offset;
    apu1_hpel_ref[1] = ps_curr_layer->ppu1_list_rec_hxfy[i1_ref_idx] + i4_offset;
    apu1_hpel_ref[2] = ps_curr_layer->ppu1_list_rec_fxhy[i1_ref_idx] + i4_offset;
    apu1_hpel_ref[3] = ps_curr_layer->ppu1_list_rec_hxhy[i1_ref_idx] + i4_offset;

    /* Initialize result params used for partition update */
    s_result_prms.pf_mv_cost_compute = NULL;
    s_result_prms.ps_search_results = ps_search_results;
    s_result_prms.pi4_valid_part_ids = pi4_valid_part_ids;
    s_result_prms.i1_ref_idx = search_idx;
    s_result_prms.i4_part_mask = i4_part_mask;
    s_result_prms.ps_search_node_base = ps_search_node;
    s_result_prms.pi4_sad_grid = &ai4_sad_grid[0][0];
    s_result_prms.i4_grid_mask = 1;

    /* convert to hpel units */
    i4_mv_x = ps_search_node->s_mv.i2_mvx >> 1;
    i4_mv_y = ps_search_node->s_mv.i2_mvy >> 1;

    /* for first pt, we compute at all locations in the grid, 4 + 1 centre */
    ps_range_prms = ps_prms->ps_mv_range_qpel;
    i4_grid_mask = (GRID_ALL_PTS_VALID);
    i4_grid_mask &= hme_clamp_grid_by_mvrange(ps_search_node, 2, ps_range_prms);

    i4_min_cost = MAX_32BIT_VAL;
    i4_min_sad = MAX_32BIT_VAL;

    /*************************************************************************/
    /* Prepare the input params to SAD/SATD function. Note that input is     */
    /* passed from the calling funcion since it may be I (normal subpel      */
    /* refinement) or 2I - P0 in case of bidirect subpel refinement.         */
    /* Both cases are handled here.                                          */
    /*************************************************************************/
    s_err_prms.pu1_inp = (U08 *)ps_prms->pv_inp;
    s_err_prms.i4_inp_stride = ps_prms->i4_inp_stride;
    s_err_prms.i4_ref_stride = i4_ref_stride;
    s_err_prms.i4_part_mask = (ENABLE_2Nx2N);
    s_err_prms.i4_grid_mask = 1;
    s_err_prms.pi4_sad_grid = &ai4_sad_grid[0][0];
    s_err_prms.i4_blk_wd = i4_blk_wd = gau1_blk_size_to_wd[e_blk_size];
    s_err_prms.i4_blk_ht = i4_blk_ht = gau1_blk_size_to_ht[e_blk_size];

    /* TODO: Currently doubling lambda for Hadamard Sad instead of 1.9*sadlambda */
    //ps_pred_ctxt->lambda <<= 1;
    part_id = ps_search_node->u1_part_id;
    for(i4_i = 0; i4_i < i4_num_hpel_refine; i4_i++)
    {
        e_min_id = PT_C;

        mvx_qpel = i4_mv_x << 1;
        mvy_qpel = i4_mv_y << 1;

        /* Central pt */
        if(i4_grid_mask & BIT_EN(PT_C))
        {
            //ps_search_node->i2_mv_x = (S16)i4_mv_x;
            //ps_search_node->i2_mv_x = (S16)i4_mv_y;
            /* central pt is i4_mv_x, i4_mv_y */
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, 1, mvx_qpel, mvy_qpel, check_for_duplicate);

            i4_frac_x = i4_mv_x & 1;
            i4_frac_y = i4_mv_y & 1;
            pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
            s_err_prms.pu1_ref = pu1_ref + (i4_mv_x >> 1) + ((i4_mv_y >> 1) * i4_ref_stride);
            pf_err_compute(&s_err_prms);
            /* Update the mv's with the current candt motion vectors */
            s_result_prms.i2_mv_x = mvx_qpel;
            s_result_prms.i2_mv_y = mvy_qpel;
            hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
            i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
            if(i4_tot_cost < i4_min_cost)
            {
                i4_min_cost = i4_tot_cost;
                i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                e_min_id = PT_C;
                pu1_final_out = s_err_prms.pu1_ref;
            }
        }

        /* left pt */
        if(i4_grid_mask & BIT_EN(PT_L))
        {
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, 1, mvx_qpel - 2, mvy_qpel, check_for_duplicate);

            if(!check_for_duplicate)
            {
                /* search node mv is stored in qpel units */
                ps_search_node->s_mv.i2_mvx = (S16)((i4_mv_x - 1) << 1);
                ps_search_node->s_mv.i2_mvy = (S16)(i4_mv_y << 1);
                /* central pt is i4_mv_x - 1, i4_mv_y */
                i4_frac_x = (i4_mv_x - 1) & 1;  // same as (x-1)&1
                i4_frac_y = i4_mv_y & 1;
                pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                s_err_prms.pu1_ref =
                    pu1_ref + ((i4_mv_x - 1) >> 1) + ((i4_mv_y >> 1) * i4_ref_stride);

                pf_err_compute(&s_err_prms);
                /* Update the mv's with the current candt motion vectors */
                s_result_prms.i2_mv_x = mvx_qpel;
                s_result_prms.i2_mv_y = mvy_qpel;
                hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                if(i4_tot_cost < i4_min_cost)
                {
                    i4_min_cost = i4_tot_cost;
                    i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    e_min_id = PT_L;
                    pu1_final_out = s_err_prms.pu1_ref;
                }
            }
        }
        /* top pt */
        if(i4_grid_mask & BIT_EN(PT_T))
        {
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, 1, mvx_qpel, mvy_qpel - 2, check_for_duplicate);

            if(!check_for_duplicate)
            {
                /* search node mv is stored in qpel units */
                ps_search_node->s_mv.i2_mvx = (S16)(i4_mv_x << 1);
                ps_search_node->s_mv.i2_mvy = (S16)((i4_mv_y - 1) << 1);
                /* top pt is i4_mv_x, i4_mv_y - 1 */
                i4_frac_x = i4_mv_x & 1;
                i4_frac_y = (i4_mv_y - 1) & 1;
                pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                s_err_prms.pu1_ref =
                    pu1_ref + (i4_mv_x >> 1) + (((i4_mv_y - 1) >> 1) * i4_ref_stride);
                pf_err_compute(&s_err_prms);
                /* Update the mv's with the current candt motion vectors */
                s_result_prms.i2_mv_x = mvx_qpel;
                s_result_prms.i2_mv_y = mvy_qpel - 2;
                hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                if(i4_tot_cost < i4_min_cost)
                {
                    i4_min_cost = i4_tot_cost;
                    i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    e_min_id = PT_T;
                    pu1_final_out = s_err_prms.pu1_ref;
                }
            }
        }
        /* right pt */
        if(i4_grid_mask & BIT_EN(PT_R))
        {
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, 1, mvx_qpel + 2, mvy_qpel, check_for_duplicate);

            if(!check_for_duplicate)
            {
                /* search node mv is stored in qpel units */
                ps_search_node->s_mv.i2_mvx = (S16)((i4_mv_x + 1) << 1);
                ps_search_node->s_mv.i2_mvy = (S16)(i4_mv_y << 1);
                /* right pt is i4_mv_x + 1, i4_mv_y */
                i4_frac_x = (i4_mv_x + 1) & 1;
                i4_frac_y = i4_mv_y & 1;

                pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                s_err_prms.pu1_ref =
                    pu1_ref + ((i4_mv_x + 1) >> 1) + ((i4_mv_y >> 1) * i4_ref_stride);
                pf_err_compute(&s_err_prms);
                /* Update the mv's with the current candt motion vectors */
                s_result_prms.i2_mv_x = mvx_qpel + 2;
                s_result_prms.i2_mv_y = mvy_qpel;
                hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                if(i4_tot_cost < i4_min_cost)
                {
                    i4_min_cost = i4_tot_cost;
                    i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    e_min_id = PT_R;
                    pu1_final_out = s_err_prms.pu1_ref;
                }
            }
        }
        /* bottom pt */
        if(i4_grid_mask & BIT_EN(PT_B))
        {
            CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                ps_dedup_enabler, 1, mvx_qpel, mvy_qpel + 2, check_for_duplicate);

            if(!check_for_duplicate)
            {
                /* search node mv is stored in qpel units */
                ps_search_node->s_mv.i2_mvx = ((S16)i4_mv_x << 1);
                ps_search_node->s_mv.i2_mvy = ((S16)(i4_mv_y + 1) << 1);
                i4_frac_x = i4_mv_x & 1;
                i4_frac_y = (i4_mv_y + 1) & 1;
                pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                s_err_prms.pu1_ref =
                    pu1_ref + (i4_mv_x >> 1) + (((i4_mv_y + 1) >> 1) * i4_ref_stride);

                pf_err_compute(&s_err_prms);
                /* Update the mv's with the current candt motion vectors */
                s_result_prms.i2_mv_x = mvx_qpel;
                s_result_prms.i2_mv_y = mvy_qpel + 2;
                hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                if(i4_tot_cost < i4_min_cost)
                {
                    i4_min_cost = i4_tot_cost;
                    i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    e_min_id = PT_B;
                    pu1_final_out = s_err_prms.pu1_ref;
                }
            }
        }
        if(e_min_id == PT_C)
        {
            if(!i4_i)
            {
                /* TL pt */
                if(i4_grid_mask & BIT_EN(PT_TL))
                {
                    S32 mvx_minus_1 = (i4_mv_x - 1);
                    S32 mvy_minus_1 = (i4_mv_y - 1);

                    CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                        ps_dedup_enabler, 1, mvx_qpel - 2, mvy_qpel - 2, check_for_duplicate);

                    if(!check_for_duplicate)
                    {
                        /* search node mv is stored in qpel units */
                        ps_search_node->s_mv.i2_mvx = ((S16)mvx_minus_1 << 1);
                        ps_search_node->s_mv.i2_mvy = ((S16)mvy_minus_1 << 1);
                        i4_frac_x = mvx_minus_1 & 1;
                        i4_frac_y = mvy_minus_1 & 1;
                        pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                        s_err_prms.pu1_ref =
                            pu1_ref + (mvx_minus_1 >> 1) + ((mvy_minus_1 >> 1) * i4_ref_stride);

                        pf_err_compute(&s_err_prms);
                        /* Update the mv's with the current candt motion vectors */
                        s_result_prms.i2_mv_x = mvx_qpel - 2;
                        s_result_prms.i2_mv_y = mvy_qpel - 2;
                        hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                        i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                        if(i4_tot_cost < i4_min_cost)
                        {
                            i4_min_cost = i4_tot_cost;
                            i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                            e_min_id = PT_TL;
                            pu1_final_out = s_err_prms.pu1_ref;
                        }
                    }
                }
                /* TR pt */
                if(i4_grid_mask & BIT_EN(PT_TR))
                {
                    S32 mvx_plus_1 = (i4_mv_x + 1);
                    S32 mvy_minus_1 = (i4_mv_y - 1);

                    CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                        ps_dedup_enabler, 1, mvx_qpel + 2, mvy_qpel - 2, check_for_duplicate);

                    if(!check_for_duplicate)
                    {
                        /* search node mv is stored in qpel units */
                        ps_search_node->s_mv.i2_mvx = ((S16)mvx_plus_1 << 1);
                        ps_search_node->s_mv.i2_mvy = ((S16)mvy_minus_1 << 1);
                        i4_frac_x = mvx_plus_1 & 1;
                        i4_frac_y = mvy_minus_1 & 1;
                        pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                        s_err_prms.pu1_ref =
                            pu1_ref + (mvx_plus_1 >> 1) + ((mvy_minus_1 >> 1) * i4_ref_stride);

                        pf_err_compute(&s_err_prms);
                        /* Update the mv's with the current candt motion vectors */
                        s_result_prms.i2_mv_x = mvx_qpel + 2;
                        s_result_prms.i2_mv_y = mvy_qpel - 2;
                        hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                        i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                        if(i4_tot_cost < i4_min_cost)
                        {
                            i4_min_cost = i4_tot_cost;
                            i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                            e_min_id = PT_TR;
                            pu1_final_out = s_err_prms.pu1_ref;
                        }
                    }
                }
                /* BL pt */
                if(i4_grid_mask & BIT_EN(PT_BL))
                {
                    S32 mvx_minus_1 = (i4_mv_x - 1);
                    S32 mvy_plus_1 = (i4_mv_y + 1);

                    CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                        ps_dedup_enabler, 1, mvx_qpel - 2, mvy_qpel + 2, check_for_duplicate);

                    if(!check_for_duplicate)
                    {
                        /* search node mv is stored in qpel units */
                        ps_search_node->s_mv.i2_mvx = ((S16)mvx_minus_1 << 1);
                        ps_search_node->s_mv.i2_mvy = ((S16)mvy_plus_1 << 1);
                        i4_frac_x = mvx_minus_1 & 1;
                        i4_frac_y = mvy_plus_1 & 1;
                        pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                        s_err_prms.pu1_ref =
                            pu1_ref + (mvx_minus_1 >> 1) + ((mvy_plus_1 >> 1) * i4_ref_stride);

                        pf_err_compute(&s_err_prms);
                        /* Update the mv's with the current candt motion vectors */
                        s_result_prms.i2_mv_x = mvx_qpel - 2;
                        s_result_prms.i2_mv_y = mvy_qpel + 2;
                        hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                        i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                        if(i4_tot_cost < i4_min_cost)
                        {
                            i4_min_cost = i4_tot_cost;
                            i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                            e_min_id = PT_BL;
                            pu1_final_out = s_err_prms.pu1_ref;
                        }
                    }
                }
                /* BR pt */
                if(i4_grid_mask & BIT_EN(PT_BR))
                {
                    S32 mvx_plus_1 = (i4_mv_x + 1);
                    S32 mvy_plus_1 = (i4_mv_y + 1);
                    CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                        ps_dedup_enabler, 1, mvx_qpel + 2, mvy_qpel + 2, check_for_duplicate);

                    if(!check_for_duplicate)
                    {
                        /* search node mv is stored in qpel units */
                        ps_search_node->s_mv.i2_mvx = ((S16)mvx_plus_1 << 1);
                        ps_search_node->s_mv.i2_mvy = ((S16)mvy_plus_1 << 1);
                        i4_frac_x = mvx_plus_1 & 1;
                        i4_frac_y = mvy_plus_1 & 1;
                        pu1_ref = apu1_hpel_ref[i4_frac_y * 2 + i4_frac_x];
                        s_err_prms.pu1_ref =
                            pu1_ref + (mvx_plus_1 >> 1) + ((mvy_plus_1 >> 1) * i4_ref_stride);

                        pf_err_compute(&s_err_prms);
                        /* Update the mv's with the current candt motion vectors */
                        s_result_prms.i2_mv_x = mvx_qpel + 2;
                        s_result_prms.i2_mv_y = mvy_qpel + 2;
                        hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                        i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                        if(i4_tot_cost < i4_min_cost)
                        {
                            i4_min_cost = i4_tot_cost;
                            i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                            e_min_id = PT_BR;
                            pu1_final_out = s_err_prms.pu1_ref;
                        }
                    }
                }
                if(e_min_id == PT_C)
                {
                    break;
                }
            }
            else
            {
                break;
            }
        }

        /*********************************************************************/
        /* Depending on the best result location, we may be able to skip     */
        /* atleast two pts, centre pt and one more pt. E.g. if right pt is   */
        /* the best result, the next iteration need not do centre, left pts  */
        /*********************************************************************/
        if(i4_i)
        {
            i4_grid_mask = gai4_opt_grid_mask_diamond[e_min_id];
        }
        else
        {
            i4_grid_mask = gai4_opt_grid_mask_conventional[e_min_id];
        }
        i4_mv_x += gai1_grid_id_to_x[e_min_id];
        i4_mv_y += gai1_grid_id_to_y[e_min_id];
        ps_search_node->s_mv.i2_mvx = (S16)(i4_mv_x << 1);
        ps_search_node->s_mv.i2_mvy = (S16)(i4_mv_y << 1);
        i4_grid_mask &= hme_clamp_grid_by_mvrange(ps_search_node, 2, ps_range_prms);
    }

    /* Convert to QPEL units */
    i4_mv_x <<= 1;
    i4_mv_y <<= 1;

    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;

    /* Early exit if this partition is visiting same hpel mv again */
    /* Assumption : Checkin for early exit in best result of partition */
    if((ps_search_results->aps_part_results[search_idx][part_id][0].i2_best_hpel_mv_x ==
        ps_search_node->s_mv.i2_mvx) &&
       (ps_search_results->aps_part_results[search_idx][part_id][0].i2_best_hpel_mv_y ==
        ps_search_node->s_mv.i2_mvy))
    {
        return (ps_search_results->aps_part_results[search_idx][part_id][0].i4_tot_cost);
    }
    else
    {
        /* Store the best hpel mv for future early exit checks */
        ps_search_results->aps_part_results[search_idx][part_id][0].i2_best_hpel_mv_x =
            (S16)i4_mv_x;
        ps_search_results->aps_part_results[search_idx][part_id][0].i2_best_hpel_mv_y =
            (S16)i4_mv_y;
    }

    /* Early exit if this partition is visiting same hpel mv again */
    /* Assumption : Checkin for early exit in second best result of partition */
    if((ps_search_results->aps_part_results[search_idx][part_id][1].i2_best_hpel_mv_x ==
        ps_search_node->s_mv.i2_mvx) &&
       (ps_search_results->aps_part_results[search_idx][part_id][1].i2_best_hpel_mv_y ==
        ps_search_node->s_mv.i2_mvy))
    {
        return (ps_search_results->aps_part_results[search_idx][part_id][1].i4_tot_cost);
    }
    else
    {
        /* Store the best hpel mv for future early exit checks */
        ps_search_results->aps_part_results[search_idx][part_id][1].i2_best_hpel_mv_x =
            (S16)i4_mv_x;
        ps_search_results->aps_part_results[search_idx][part_id][1].i2_best_hpel_mv_y =
            (S16)i4_mv_y;
    }

    /* Exact interpolation or averaging chosen here */
    pf_qpel_interp = ps_prms->pf_qpel_interp;

    /* Next QPEL ME */
    /* In this case, we have option of doing exact QPEL interpolation or avg */
    /*************************************************************************/
    /*        x                                                              */
    /*    A b C d                                                            */
    /*    e f g h                                                            */
    /*    I j K l                                                            */
    /*    m n o p                                                            */
    /*    Q r S t                                                            */
    /*                                                                       */
    /*    Approximate QPEL logic                                             */
    /*    b = avg(A,C) f = avg(I,C), g= avg(C,K) j=avg(I,K)                  */
    /*    for any given pt, we can get all the information required about    */
    /*    the surrounding 4 pts. For example, given point C (0.5, 0)         */
    /*     surrounding pts info:                                             */
    /*     b : qpel offset: 1, 0, generated by averaging. buffer1: fpel buf  */
    /*           buffer 2: hxfy, offsets for both are 0, 0                   */
    /*    similarly for other pts the info can be gotten                     */
    /*************************************************************************/
    i4_grid_mask = GRID_ALL_PTS_VALID ^ (BIT_EN(PT_C));
    i4_grid_mask &= hme_clamp_grid_by_mvrange(ps_search_node, 1, ps_range_prms);

    /*************************************************************************/
    /* One time preparation of non changing interpolation params. These      */
    /* include a set of ping pong result buf ptrs, input buf ptrs and some   */
    /* working memory (not used though in case of averaging).                */
    /*************************************************************************/
    s_interp_prms.ppu1_ref = &apu1_hpel_ref[0];
    s_interp_prms.i4_ref_stride = i4_ref_stride;
    s_interp_prms.i4_blk_wd = i4_blk_wd;
    s_interp_prms.i4_blk_ht = i4_blk_ht;

    i4_final_out_stride = i4_ref_stride;

    {
        U08 *pu1_mem;
        /*********************************************************************/
        /* Allocation of working memory for interpolated buffers. We maintain*/
        /* an intermediate working buffer, and 2 ping pong interpolated out  */
        /* buffers, purpose of ping pong explained later below               */
        /*********************************************************************/
        pu1_mem = ps_prms->pu1_wkg_mem;
        s_interp_prms.pu1_wkg_mem = pu1_mem;

        //pu1_mem += (INTERP_INTERMED_BUF_SIZE);
        s_interp_prms.apu1_interp_out[0] = pu1_mem;

        pu1_mem += (INTERP_OUT_BUF_SIZE);
        s_interp_prms.apu1_interp_out[1] = pu1_mem;

        pu1_mem += (INTERP_OUT_BUF_SIZE);
        s_interp_prms.apu1_interp_out[2] = pu1_mem;

        pu1_mem += (INTERP_OUT_BUF_SIZE);
        s_interp_prms.apu1_interp_out[3] = pu1_mem;

        pu1_mem += (INTERP_OUT_BUF_SIZE);
        s_interp_prms.apu1_interp_out[4] = pu1_mem;

        /*********************************************************************/
        /* Stride of interpolated output is just a function of blk width of  */
        /* this partition and hence remains constant for this partition      */
        /*********************************************************************/
        s_interp_prms.i4_out_stride = (i4_blk_wd);
    }

    {
        UWORD8 *apu1_final[4];
        WORD32 ai4_ref_stride[4];
        /*************************************************************************/
        /* Ping pong design for interpolated buffers. We use a min id, which     */
        /* tracks the id of the ppu1_interp_out that stores the best result.     */
        /* When new interp to be done, it uses 1 - bes result id to do the interp*/
        /* min id is toggled when any new result becomes the best result.        */
        /*************************************************************************/

        for(i4_i = 0; i4_i < i4_num_qpel_refine; i4_i++)
        {
            e_min_id = PT_C;

            hme_qpel_interp_comprehensive(
                &s_interp_prms, apu1_final, ai4_ref_stride, i4_mv_x, i4_mv_y, i4_grid_mask);

            mvx_qpel = i4_mv_x;
            mvy_qpel = i4_mv_y;

            if(i4_grid_mask & BIT_EN(PT_L))
            {
                CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                    ps_dedup_enabler, 1, mvx_qpel - 1, mvy_qpel - 0, check_for_duplicate);

                if(!check_for_duplicate)
                {
                    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x - 1;
                    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;

                    s_err_prms.pu1_ref = apu1_final[0];
                    s_err_prms.i4_ref_stride = ai4_ref_stride[0];

                    pf_err_compute(&s_err_prms);
                    /* Update the mv's with the current candt motion vectors */
                    s_result_prms.i2_mv_x = mvx_qpel - 1;
                    s_result_prms.i2_mv_y = mvy_qpel;
                    hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                    i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                    if(i4_tot_cost < i4_min_cost)
                    {
                        e_min_id = PT_L;
                        i4_min_cost = i4_tot_cost;
                        i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    }
                }
            }
            if(i4_grid_mask & BIT_EN(PT_T))
            {
                CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                    ps_dedup_enabler, 1, mvx_qpel - 0, mvy_qpel - 1, check_for_duplicate);

                if(!check_for_duplicate)
                {
                    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
                    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y - 1;

                    s_err_prms.pu1_ref = apu1_final[1];
                    s_err_prms.i4_ref_stride = ai4_ref_stride[1];

                    pf_err_compute(&s_err_prms);
                    /* Update the mv's with the current candt motion vectors */
                    s_result_prms.i2_mv_x = mvx_qpel;
                    s_result_prms.i2_mv_y = mvy_qpel - 1;
                    hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
                    i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                    if(i4_tot_cost < i4_min_cost)
                    {
                        e_min_id = PT_T;
                        i4_min_cost = i4_tot_cost;
                        i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    }
                }
            }
            if(i4_grid_mask & BIT_EN(PT_R))
            {
                CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                    ps_dedup_enabler, 1, mvx_qpel + 1, mvy_qpel, check_for_duplicate);

                if(!check_for_duplicate)
                {
                    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x + 1;
                    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;

                    s_err_prms.pu1_ref = apu1_final[2];
                    s_err_prms.i4_ref_stride = ai4_ref_stride[2];

                    pf_err_compute(&s_err_prms);
                    /* Update the mv's with the current candt motion vectors */
                    s_result_prms.i2_mv_x = mvx_qpel + 1;
                    s_result_prms.i2_mv_y = mvy_qpel;
                    hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                    i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                    if(i4_tot_cost < i4_min_cost)
                    {
                        e_min_id = PT_R;
                        i4_min_cost = i4_tot_cost;
                        i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    }
                }
            }
            /* i4_mv_x and i4_mv_y will always be the centre pt */
            /* for qpel we  start with least hpel, and hence compute of center pt never reqd */
            if(i4_grid_mask & BIT_EN(PT_B))
            {
                CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                    ps_dedup_enabler, 1, mvx_qpel, mvy_qpel + 1, check_for_duplicate);

                if(!check_for_duplicate)
                {
                    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
                    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y + 1;

                    s_err_prms.pu1_ref = apu1_final[3];
                    s_err_prms.i4_ref_stride = ai4_ref_stride[3];

                    pf_err_compute(&s_err_prms);
                    /* Update the mv's with the current candt motion vectors */
                    s_result_prms.i2_mv_x = mvx_qpel;
                    s_result_prms.i2_mv_y = mvy_qpel + 1;
                    hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);
                    i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];
                    if(i4_tot_cost < i4_min_cost)
                    {
                        e_min_id = PT_B;
                        i4_min_cost = i4_tot_cost;
                        i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                    }
                }
            }

            if(e_min_id == PT_C)
            {
                if(!i4_i)
                {
                    S32 i4_interp_buf_id = 0;

                    if(i4_grid_mask & BIT_EN(PT_TL))
                    {
                        CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                            ps_dedup_enabler, 1, mvx_qpel - 1, mvy_qpel - 1, check_for_duplicate);

                        if(!check_for_duplicate)
                        {
                            ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x - 1;
                            ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y - 1;

                            /* Carry out the interpolation */
                            pf_qpel_interp(
                                &s_interp_prms, i4_mv_x - 1, i4_mv_y - 1, i4_interp_buf_id);

                            s_err_prms.pu1_ref = s_interp_prms.pu1_final_out;
                            s_err_prms.i4_ref_stride = s_interp_prms.i4_final_out_stride;

                            pf_err_compute(&s_err_prms);
                            /* Update the mv's with the current candt motion vectors */
                            s_result_prms.i2_mv_x = mvx_qpel - 1;
                            s_result_prms.i2_mv_y = mvy_qpel - 1;
                            hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                            i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                            if(i4_tot_cost < i4_min_cost)
                            {
                                e_min_id = PT_TL;
                                i4_min_cost = i4_tot_cost;
                                i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                            }
                        }
                    }
                    if(i4_grid_mask & BIT_EN(PT_TR))
                    {
                        CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                            ps_dedup_enabler, 1, mvx_qpel + 1, mvy_qpel - 1, check_for_duplicate);

                        if(!check_for_duplicate)
                        {
                            ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x + 1;
                            ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y - 1;

                            /* Carry out the interpolation */
                            pf_qpel_interp(
                                &s_interp_prms, i4_mv_x + 1, i4_mv_y - 1, i4_interp_buf_id);

                            s_err_prms.pu1_ref = s_interp_prms.pu1_final_out;
                            s_err_prms.i4_ref_stride = s_interp_prms.i4_final_out_stride;

                            pf_err_compute(&s_err_prms);
                            /* Update the mv's with the current candt motion vectors */
                            s_result_prms.i2_mv_x = mvx_qpel + 1;
                            s_result_prms.i2_mv_y = mvy_qpel - 1;
                            hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                            i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                            if(i4_tot_cost < i4_min_cost)
                            {
                                e_min_id = PT_TR;
                                i4_min_cost = i4_tot_cost;
                                i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                            }
                        }
                    }
                    if(i4_grid_mask & BIT_EN(PT_BL))
                    {
                        CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                            ps_dedup_enabler, 1, mvx_qpel - 1, mvy_qpel + 1, check_for_duplicate);

                        if(!check_for_duplicate)
                        {
                            ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x - 1;
                            ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y + 1;

                            /* Carry out the interpolation */
                            pf_qpel_interp(
                                &s_interp_prms, i4_mv_x - 1, i4_mv_y + 1, i4_interp_buf_id);

                            s_err_prms.pu1_ref = s_interp_prms.pu1_final_out;
                            s_err_prms.i4_ref_stride = s_interp_prms.i4_final_out_stride;

                            pf_err_compute(&s_err_prms);
                            /* Update the mv's with the current candt motion vectors */
                            s_result_prms.i2_mv_x = mvx_qpel - 1;
                            s_result_prms.i2_mv_y = mvy_qpel + 1;
                            hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                            i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                            if(i4_tot_cost < i4_min_cost)
                            {
                                e_min_id = PT_BL;
                                i4_min_cost = i4_tot_cost;
                                i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                            }
                        }
                    }
                    /* i4_mv_x and i4_mv_y will always be the centre pt */
                    /* for qpel we  start with least hpel, and hence compute of center pt never reqd */
                    if(i4_grid_mask & BIT_EN(PT_BR))
                    {
                        CHECK_FOR_DUPES_AND_INSERT_UNIQUE_NODES(
                            ps_dedup_enabler, 1, mvx_qpel + 1, mvy_qpel + 1, check_for_duplicate);

                        if(!check_for_duplicate)
                        {
                            ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x + 1;
                            ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y + 1;

                            /* Carry out the interpolation */
                            pf_qpel_interp(
                                &s_interp_prms, i4_mv_x + 1, i4_mv_y + 1, i4_interp_buf_id);

                            s_err_prms.pu1_ref = s_interp_prms.pu1_final_out;
                            s_err_prms.i4_ref_stride = s_interp_prms.i4_final_out_stride;

                            pf_err_compute(&s_err_prms);
                            /* Update the mv's with the current candt motion vectors */
                            s_result_prms.i2_mv_x = mvx_qpel + 1;
                            s_result_prms.i2_mv_y = mvy_qpel + 1;
                            hme_update_results_pt_pu_best1_subpel_hs(&s_result_prms);

                            i4_tot_cost = s_err_prms.pi4_sad_grid[part_id];

                            if(i4_tot_cost < i4_min_cost)
                            {
                                e_min_id = PT_BR;
                                i4_min_cost = i4_tot_cost;
                                i4_min_sad = s_err_prms.pi4_sad_grid[part_id];
                            }
                        }
                    }
                    if(e_min_id == PT_C)
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            if(i4_i)
            {
                i4_grid_mask = gai4_opt_grid_mask_diamond[e_min_id];
            }
            else
            {
                i4_grid_mask = gai4_opt_grid_mask_conventional[e_min_id];
            }
            i4_mv_x += gai1_grid_id_to_x[e_min_id];
            i4_mv_y += gai1_grid_id_to_y[e_min_id];
            ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
            ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;
            i4_grid_mask &= hme_clamp_grid_by_mvrange(ps_search_node, 1, ps_range_prms);
        }
    }

    /* update modified motion vectors and cost at end of subpel */
    ps_search_node->s_mv.i2_mvx = (S16)i4_mv_x;
    ps_search_node->s_mv.i2_mvy = (S16)i4_mv_y;
    ps_search_node->i4_tot_cost = i4_min_cost;
    ps_search_node->i4_sad = i4_min_sad;

    /********************************************************************************/
    /* TODO: Restoring back Sad lambda from Hadamard lambda                         */
    /* Need to pass the had/satd lambda in more cleaner way for subpel cost compute */
    /********************************************************************************/
    //ps_pred_ctxt->lambda >>= 1;

    return (i4_min_cost);
}
#endif

static void hme_subpel_refine_struct_to_search_results_struct_converter(
    subpel_refine_ctxt_t *ps_subpel_refine_ctxt,
    search_results_t *ps_search_results,
    U08 u1_pred_dir,
    ME_QUALITY_PRESETS_T e_quality_preset)
{
    U08 i;

    U08 u1_num_results_per_part = ps_search_results->u1_num_results_per_part;

    for(i = 0; i < ps_subpel_refine_ctxt->i4_num_valid_parts; i++)
    {
        S32 index;
        S32 i4_sad;

        S32 part_id = ps_subpel_refine_ctxt->ai4_part_id[i];

        search_node_t *ps_best_node = ps_search_results->aps_part_results[u1_pred_dir][part_id];

        if(ps_subpel_refine_ctxt->i4_num_valid_parts > 8)
        {
            index = part_id;
        }
        else
        {
            index = i;
        }

        if(!ps_best_node->u1_subpel_done)
        {
            i4_sad = ps_subpel_refine_ctxt->i2_tot_cost[0][index] -
                     ps_subpel_refine_ctxt->i2_mv_cost[0][index];
            ps_best_node[0].i4_sdi = 0;
            ASSERT((e_quality_preset == ME_PRISTINE_QUALITY) ? (ps_best_node[0].i4_sdi >= 0) : 1);
            ps_best_node[0].i4_tot_cost = ps_subpel_refine_ctxt->i2_tot_cost[0][index];

            if(ps_subpel_refine_ctxt->i2_tot_cost[0][index] == MAX_SIGNED_16BIT_VAL)
            {
                i4_sad = MAX_SIGNED_16BIT_VAL;
            }

            ps_best_node[0].i4_sad = i4_sad;
            ps_best_node[0].i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[0][index];
            ps_best_node[0].s_mv.i2_mvx = ps_subpel_refine_ctxt->i2_mv_x[0][index];
            ps_best_node[0].s_mv.i2_mvy = ps_subpel_refine_ctxt->i2_mv_y[0][index];
            ps_best_node[0].i1_ref_idx = (WORD8)ps_subpel_refine_ctxt->i2_ref_idx[0][index];
            ps_best_node->u1_subpel_done = 1;

            if(2 == u1_num_results_per_part)
            {
                i4_sad = ps_subpel_refine_ctxt->i2_tot_cost[1][index] -
                         ps_subpel_refine_ctxt->i2_mv_cost[1][index];
                ps_best_node[1].i4_sdi = 0;
                ps_best_node[1].i4_tot_cost = ps_subpel_refine_ctxt->i2_tot_cost[1][index];

                if(ps_subpel_refine_ctxt->i2_tot_cost[1][index] == MAX_SIGNED_16BIT_VAL)
                {
                    i4_sad = MAX_SIGNED_16BIT_VAL;
                }

                ps_best_node[1].i4_sad = i4_sad;
                ps_best_node[1].i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[1][index];
                ps_best_node[1].s_mv.i2_mvx = ps_subpel_refine_ctxt->i2_mv_x[1][index];
                ps_best_node[1].s_mv.i2_mvy = ps_subpel_refine_ctxt->i2_mv_y[1][index];
                ps_best_node[1].i1_ref_idx = (WORD8)ps_subpel_refine_ctxt->i2_ref_idx[1][index];
                ps_best_node[1].u1_subpel_done = 1;
            }
        }
        else if(
            (2 == u1_num_results_per_part) &&
            (ps_subpel_refine_ctxt->i2_tot_cost[0][index] < ps_best_node[1].i4_tot_cost))
        {
            if(ps_subpel_refine_ctxt->i2_tot_cost[1][index] < ps_best_node[0].i4_tot_cost)
            {
                i4_sad = ps_subpel_refine_ctxt->i2_tot_cost[0][index] -
                         ps_subpel_refine_ctxt->i2_mv_cost[0][index];
                ps_best_node[0].i4_sdi = 0;
                ps_best_node[0].i4_tot_cost = ps_subpel_refine_ctxt->i2_tot_cost[0][index];

                if(ps_subpel_refine_ctxt->i2_tot_cost[0][index] == MAX_SIGNED_16BIT_VAL)
                {
                    i4_sad = MAX_SIGNED_16BIT_VAL;
                }

                ps_best_node[0].i4_sad = i4_sad;
                ps_best_node[0].i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[0][index];
                ps_best_node[0].s_mv.i2_mvx = ps_subpel_refine_ctxt->i2_mv_x[0][index];
                ps_best_node[0].s_mv.i2_mvy = ps_subpel_refine_ctxt->i2_mv_y[0][index];
                ps_best_node[0].i1_ref_idx = (S08)ps_subpel_refine_ctxt->i2_ref_idx[0][index];

                i4_sad = ps_subpel_refine_ctxt->i2_tot_cost[1][index] -
                         ps_subpel_refine_ctxt->i2_mv_cost[1][index];
                ps_best_node[1].i4_sdi = 0;
                ps_best_node[1].i4_tot_cost = ps_subpel_refine_ctxt->i2_tot_cost[1][index];

                if(ps_subpel_refine_ctxt->i2_tot_cost[1][index] == MAX_SIGNED_16BIT_VAL)
                {
                    i4_sad = MAX_SIGNED_16BIT_VAL;
                }

                ps_best_node[1].i4_sad = i4_sad;
                ps_best_node[1].i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[1][index];
                ps_best_node[1].s_mv.i2_mvx = ps_subpel_refine_ctxt->i2_mv_x[1][index];
                ps_best_node[1].s_mv.i2_mvy = ps_subpel_refine_ctxt->i2_mv_y[1][index];
                ps_best_node[1].i1_ref_idx = (S08)ps_subpel_refine_ctxt->i2_ref_idx[1][index];
            }
            else if(ps_subpel_refine_ctxt->i2_tot_cost[1][index] > ps_best_node[0].i4_tot_cost)
            {
                if(ps_subpel_refine_ctxt->i2_tot_cost[0][index] >= ps_best_node[0].i4_tot_cost)
                {
                    i4_sad = ps_subpel_refine_ctxt->i2_tot_cost[0][index] -
                             ps_subpel_refine_ctxt->i2_mv_cost[0][index];
                    ps_best_node[1].i4_sdi = 0;
                    ps_best_node[1].i4_tot_cost = ps_subpel_refine_ctxt->i2_tot_cost[0][index];

                    if(ps_subpel_refine_ctxt->i2_tot_cost[0][index] == MAX_SIGNED_16BIT_VAL)
                    {
                        i4_sad = MAX_SIGNED_16BIT_VAL;
                    }

                    ps_best_node[1].i4_sad = i4_sad;
                    ps_best_node[1].i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[0][index];
                    ps_best_node[1].s_mv.i2_mvx = ps_subpel_refine_ctxt->i2_mv_x[0][index];
                    ps_best_node[1].s_mv.i2_mvy = ps_subpel_refine_ctxt->i2_mv_y[0][index];
                    ps_best_node[1].i1_ref_idx = (S08)ps_subpel_refine_ctxt->i2_ref_idx[0][index];
                }
                else if(ps_subpel_refine_ctxt->i2_tot_cost[0][index] < ps_best_node[0].i4_tot_cost)
                {
                    memmove(&ps_best_node[1], &ps_best_node[0], sizeof(search_node_t));

                    i4_sad = ps_subpel_refine_ctxt->i2_tot_cost[0][index] -
                             ps_subpel_refine_ctxt->i2_mv_cost[0][index];
                    ps_best_node[0].i4_sdi = 0;
                    ps_best_node[0].i4_tot_cost = ps_subpel_refine_ctxt->i2_tot_cost[0][index];

                    if(ps_subpel_refine_ctxt->i2_tot_cost[0][index] == MAX_SIGNED_16BIT_VAL)
                    {
                        i4_sad = MAX_SIGNED_16BIT_VAL;
                    }

                    ps_best_node[0].i4_sad = i4_sad;
                    ps_best_node[0].i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[0][index];
                    ps_best_node[0].s_mv.i2_mvx = ps_subpel_refine_ctxt->i2_mv_x[0][index];
                    ps_best_node[0].s_mv.i2_mvy = ps_subpel_refine_ctxt->i2_mv_y[0][index];
                    ps_best_node[0].i1_ref_idx = (S08)ps_subpel_refine_ctxt->i2_ref_idx[0][index];
                }
            }
        }
        else if(
            (1 == u1_num_results_per_part) &&
            (ps_subpel_refine_ctxt->i2_tot_cost[0][index] < ps_best_node[0].i4_tot_cost))
        {
            i4_sad = ps_subpel_refine_ctxt->i2_tot_cost[0][index] -
                     ps_subpel_refine_ctxt->i2_mv_cost[0][index];
            ps_best_node[0].i4_sdi = 0;
            ps_best_node[0].i4_tot_cost = ps_subpel_refine_ctxt->i2_tot_cost[0][index];

            if(ps_subpel_refine_ctxt->i2_tot_cost[0][index] == MAX_SIGNED_16BIT_VAL)
            {
                i4_sad = MAX_SIGNED_16BIT_VAL;
            }

            ps_best_node[0].i4_sad = i4_sad;
            ps_best_node[0].i4_mv_cost = ps_subpel_refine_ctxt->i2_mv_cost[0][index];
            ps_best_node[0].s_mv.i2_mvx = ps_subpel_refine_ctxt->i2_mv_x[0][index];
            ps_best_node[0].s_mv.i2_mvy = ps_subpel_refine_ctxt->i2_mv_y[0][index];
            ps_best_node[0].i1_ref_idx = (S08)ps_subpel_refine_ctxt->i2_ref_idx[0][index];
        }
    }
}

/**
********************************************************************************
*  @fn     S32 hme_subpel_refine_cu_hs
*
*  @brief  Evaluates the best subpel mvs for active partitions of an MB in L0
*          layer for the high speed preset. Recursive hadamard SATD / SAD
*          and mv cost is used for 2NxN and NxN partitions with active partition
*          update
*
*  @param[in]  ps_prms: subpel prms input to this function
*
*  @param[in]  ps_curr_layer: points to the current layer ctxt
*
*  @param[out] ps_search_results: points to the search resutls that get updated
*              with best results
*
*  @param[in]  search_idx:  ref id of the frame for which results get updated
*
*  @param[in]  ps_wt_inp_prms:  current frame input params
*
*  @return     None
********************************************************************************
*/
void hme_subpel_refine_cu_hs(
    hme_subpel_prms_t *ps_prms,
    layer_ctxt_t *ps_curr_layer,
    search_results_t *ps_search_results,
    S32 search_idx,
    wgt_pred_ctxt_t *ps_wt_inp_prms,
    WORD32 blk_8x8_mask,
    me_func_selector_t *ps_func_selector,
    ihevce_cmn_opt_func_t *ps_cmn_utils_optimised_function_list,
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list)
{
    /* Unique search node list for 2nx2n and nxn partitions */
    search_node_t as_nodes_2nx2n[MAX_RESULTS_PER_PART * 5];
    subpel_dedup_enabler_t as_subpel_dedup_enabler[MAX_NUM_REF];
    search_node_t *ps_search_node;

    S32 i, i4_part_mask, j;
    S32 i4_sad_grid;
    S32 max_subpel_cand;
    WORD32 index;
    S32 num_unique_nodes_2nx2n;
    S32 part_id;
    S32 x_off, y_off;
    S32 i4_inp_off;

    CU_SIZE_T e_cu_size;
    BLK_SIZE_T e_blk_size;

    subpel_refine_ctxt_t *ps_subpel_refine_ctxt = ps_prms->ps_subpel_refine_ctxt;

    S32 i4_use_satd = ps_prms->i4_use_satd;
    S32 i4_num_act_refs = ps_prms->i4_num_act_ref_l0 + ps_prms->i4_num_act_ref_l1;

    ASSERT(ps_search_results->u1_num_results_per_part <= MAX_RESULTS_PER_PART);

    if(!DISABLE_SUBPEL_REFINEMENT_WHEN_SRC_IS_NOISY || !ps_prms->u1_is_cu_noisy)
    {
        e_cu_size = ps_search_results->e_cu_size;
        i4_part_mask = ps_search_results->i4_part_mask;

        ps_prms->i4_inp_type = sizeof(U08);

        num_unique_nodes_2nx2n = 0;

        for(i = 0; i < i4_num_act_refs; i++)
        {
            as_subpel_dedup_enabler[i].u1_ref_idx = MAX_NUM_REF;
        }

        /************************************************************************/
        /*                                                                      */
        /*  Initialize SATD cost for each valid partition id.one time before    */
        /*  doing full pel time. This is because of the following reasons:      */
        /*   1. Full pel cost was done in  SAD while subpel is in SATD mode     */
        /*   2. Partitions like AMP, Nx2N and 2NxN are refined on the fly while */
        /*      doing Diamond search for 2Nx2N and NxN. This partitions are     */
        /*      not explicitly refine in high speed mode                        */
        /*                                                                      */
        /************************************************************************/
        for(i = 0; i < ps_subpel_refine_ctxt->i4_num_valid_parts; i++)
        {
            S32 enable_subpel = 0;
            S32 part_type;

            /* Derive the x and y offsets of this part id */
            part_id = ps_subpel_refine_ctxt->ai4_part_id[i];
            if(ps_subpel_refine_ctxt->i4_num_valid_parts > 8)
            {
                index = part_id;
            }
            else
            {
                index = i;
            }

            part_type = ge_part_id_to_part_type[part_id];
            x_off = gas_part_attr_in_cu[part_id].u1_x_start << e_cu_size;
            y_off = gas_part_attr_in_cu[part_id].u1_y_start << e_cu_size;
            x_off += ps_search_results->u1_x_off;
            y_off += ps_search_results->u1_y_off;
            i4_inp_off = x_off + y_off * ps_prms->i4_inp_stride;
            e_blk_size = ge_part_id_to_blk_size[e_cu_size][part_id];

            x_off += ps_prms->i4_ctb_x_off;
            y_off += ps_prms->i4_ctb_y_off;

            max_subpel_cand = 0;

            /* Choose the minimum number of candidates to be used for Sub pel refinement */
            if(PART_ID_2Nx2N == part_type)
            {
                max_subpel_cand =
                    MIN(ps_prms->u1_max_subpel_candts_2Nx2N,
                        ps_search_results->u1_num_results_per_part);
            }
            else if(PRT_NxN == part_type)
            {
                max_subpel_cand = MIN(
                    ps_prms->u1_max_subpel_candts_NxN, ps_search_results->u1_num_results_per_part);
            }

            /* If incomplete CTB, NxN num candidates should be forced to min 1 */
            if((0 == max_subpel_cand) && (blk_8x8_mask != 15))
            {
                max_subpel_cand = 1;
            }

            if((PART_ID_2Nx2N == part_type) || (PRT_NxN == part_type))
            {
                enable_subpel = 1;
            }

            /* Compute full pel SATD for each result per partition before subpel */
            /* refinement starts.                                                */
            /* Also prepare unique candidate list for 2Nx2N and NxN partitions   */
            for(j = 0; j < ps_search_results->u1_num_results_per_part; j++)
            {
                err_prms_t s_err_prms;
                S32 i4_satd = 0;
                S32 i1_ref_idx;
                U08 *pu1_ref_base;
                S32 i4_ref_stride = ps_curr_layer->i4_rec_stride;
                S32 i4_mv_x, i4_mv_y;

                ps_search_node = ps_search_results->aps_part_results[search_idx][part_id] + j;

                if(ps_subpel_refine_ctxt->i2_mv_x[j][index] == INTRA_MV)
                {
                    ps_search_node->u1_subpel_done = 1;
                    continue;
                }

                i1_ref_idx = ps_subpel_refine_ctxt->i2_ref_idx[j][index];
                ps_prms->pv_inp = (void *)(ps_wt_inp_prms->apu1_wt_inp[i1_ref_idx] + i4_inp_off);
                pu1_ref_base = ps_curr_layer->ppu1_list_rec_fxfy[i1_ref_idx];

                i4_mv_x = ps_subpel_refine_ctxt->i2_mv_x[j][index];
                i4_mv_y = ps_subpel_refine_ctxt->i2_mv_y[j][index];

                if(i4_use_satd)
                {
                    s_err_prms.pu1_inp = (U08 *)ps_prms->pv_inp;
                    s_err_prms.i4_inp_stride = ps_prms->i4_inp_stride;
                    s_err_prms.pu1_ref = pu1_ref_base + x_off + (y_off * i4_ref_stride) + i4_mv_x +
                                         (i4_mv_y * i4_ref_stride);

                    s_err_prms.i4_ref_stride = i4_ref_stride;
                    s_err_prms.i4_part_mask = (ENABLE_2Nx2N);
                    s_err_prms.i4_grid_mask = 1;
                    s_err_prms.pi4_sad_grid = &i4_sad_grid;
                    s_err_prms.i4_blk_wd = gau1_blk_size_to_wd[e_blk_size];
                    s_err_prms.i4_blk_ht = gau1_blk_size_to_ht[e_blk_size];

                    s_err_prms.ps_cmn_utils_optimised_function_list =
                        ps_cmn_utils_optimised_function_list;

                    compute_satd_8bit(&s_err_prms);

                    i4_satd = s_err_prms.pi4_sad_grid[0];

                    ps_subpel_refine_ctxt->i2_tot_cost[j][index] =
                        CLIP_S16(ps_subpel_refine_ctxt->i2_mv_cost[j][index] + i4_satd);
                    ps_subpel_refine_ctxt->ai2_fullpel_satd[j][index] = i4_satd;
                }

                /* Sub-pel candidate filtration */
                if(j)
                {
                    S16 i2_best_sad;
                    S32 i4_best_mvx;
                    S32 i4_best_mvy;

                    search_node_t *ps_node =
                        ps_search_results->aps_part_results[search_idx][part_id];

                    U08 u1_is_subpel_done = ps_node->u1_subpel_done;
                    S16 i2_curr_sad = ps_subpel_refine_ctxt->ai2_fullpel_satd[j][index];
                    S32 i4_curr_mvx = i4_mv_x << 2;
                    S32 i4_curr_mvy = i4_mv_y << 2;

                    if(u1_is_subpel_done)
                    {
                        i2_best_sad = ps_node->i4_sad;

                        if(ps_node->i1_ref_idx == i1_ref_idx)
                        {
                            i4_best_mvx = ps_node->s_mv.i2_mvx;
                            i4_best_mvy = ps_node->s_mv.i2_mvy;
                        }
                        else if(i1_ref_idx == ps_subpel_refine_ctxt->i2_ref_idx[0][index])
                        {
                            i4_best_mvx = ps_subpel_refine_ctxt->i2_mv_x[0][index];
                            i4_best_mvy = ps_subpel_refine_ctxt->i2_mv_y[0][index];
                        }
                        else
                        {
                            i4_best_mvx = INTRA_MV;
                            i4_best_mvy = INTRA_MV;
                        }
                    }
                    else
                    {
                        i2_best_sad = ps_subpel_refine_ctxt->i2_tot_cost[0][index] -
                                      ps_subpel_refine_ctxt->i2_mv_cost[0][index];

                        if(i1_ref_idx == ps_subpel_refine_ctxt->i2_ref_idx[0][index])
                        {
                            i4_best_mvx = ps_subpel_refine_ctxt->i2_mv_x[0][index];
                            i4_best_mvy = ps_subpel_refine_ctxt->i2_mv_y[0][index];
                        }
                        else
                        {
                            i4_best_mvx = INTRA_MV;
                            i4_best_mvy = INTRA_MV;
                        }
                    }

                    i2_best_sad += (i2_best_sad >> ps_prms->u1_subpel_candt_threshold);

                    if(((ABS(i4_curr_mvx - i4_best_mvx) < 2) &&
                        (ABS(i4_curr_mvy - i4_best_mvy) < 2)) ||
                       (i2_curr_sad > i2_best_sad))
                    {
                        enable_subpel = 0;
                    }
                }

                ps_search_node->u1_part_id = part_id;

                /* Convert mvs in part results from FPEL to QPEL units */
                ps_subpel_refine_ctxt->i2_mv_x[j][index] <<= 2;
                ps_subpel_refine_ctxt->i2_mv_y[j][index] <<= 2;

                /* If the candidate number is more than the number of candts
                set initally, do not add those candts for refinement */
                if(j >= max_subpel_cand)
                {
                    enable_subpel = 0;
                }

                if(enable_subpel)
                {
                    if(num_unique_nodes_2nx2n == 0)
                    {
                        S32 i4_index = ps_subpel_refine_ctxt->i2_ref_idx[j][index];

                        as_subpel_dedup_enabler[i4_index].i2_mv_x =
                            ps_subpel_refine_ctxt->i2_mv_x[j][index];
                        as_subpel_dedup_enabler[i4_index].i2_mv_y =
                            ps_subpel_refine_ctxt->i2_mv_y[j][index];
                        as_subpel_dedup_enabler[i4_index].u1_ref_idx =
                            (U08)ps_subpel_refine_ctxt->i2_ref_idx[j][index];
                        memset(
                            as_subpel_dedup_enabler[i4_index].au4_node_map,
                            0,
                            sizeof(U32) * 2 * MAP_X_MAX);
                    }
                    INSERT_NEW_NODE_NOMAP_ALTERNATE(
                        as_nodes_2nx2n, num_unique_nodes_2nx2n, ps_subpel_refine_ctxt, j, i);
                }
            }

            /*********************************************************************************************/
            /* If sad_1 < sad_2, then satd_1 need not be lesser than satd_2. Therefore, after conversion */
            /* to satd, tot_cost_1 may not be lesser than tot_cost_2. So we need to sort the search nodes*/
            /* for each partition again, based on the new costs                                          */
            /*********************************************************************************************/
            /*********************************************************************************************/
            /* Because right now, we store only the two best candidates for each partition, the sort will*/
            /* converge to a simple swap.                                                                */
            /* ASSUMPTION : We store only two best results per partition                                 */
            /*********************************************************************************************/
            if(ps_search_results->u1_num_results_per_part == 2)
            {
                if(ps_subpel_refine_ctxt->i2_tot_cost[0][index] >
                   ps_subpel_refine_ctxt->i2_tot_cost[1][index])
                {
                    SWAP(
                        ps_subpel_refine_ctxt->i2_tot_cost[0][index],
                        ps_subpel_refine_ctxt->i2_tot_cost[1][index]);

                    SWAP(
                        ps_subpel_refine_ctxt->i2_mv_cost[0][index],
                        ps_subpel_refine_ctxt->i2_mv_cost[1][index]);

                    SWAP(
                        ps_subpel_refine_ctxt->i2_mv_x[0][index],
                        ps_subpel_refine_ctxt->i2_mv_x[1][index]);

                    SWAP(
                        ps_subpel_refine_ctxt->i2_mv_y[0][index],
                        ps_subpel_refine_ctxt->i2_mv_y[1][index]);

                    SWAP(
                        ps_subpel_refine_ctxt->i2_ref_idx[0][index],
                        ps_subpel_refine_ctxt->i2_ref_idx[1][index]);

                    SWAP(
                        ps_subpel_refine_ctxt->ai2_fullpel_satd[0][index],
                        ps_subpel_refine_ctxt->ai2_fullpel_satd[1][index]);
                }
            }
        }

        if(blk_8x8_mask == 0xf)
        {
            num_unique_nodes_2nx2n =
                MIN(num_unique_nodes_2nx2n, ps_prms->u1_max_num_subpel_refine_centers);
        }
        {
            x_off = gas_part_attr_in_cu[0].u1_x_start << e_cu_size;
            y_off = gas_part_attr_in_cu[0].u1_y_start << e_cu_size;
            x_off += ps_search_results->u1_x_off;
            y_off += ps_search_results->u1_y_off;
            i4_inp_off = x_off + y_off * ps_prms->i4_inp_stride;
            e_blk_size = ge_part_id_to_blk_size[e_cu_size][0];

            for(j = 0; j < num_unique_nodes_2nx2n; j++)
            {
                S32 pred_lx;
                ps_search_node = &as_nodes_2nx2n[j];

                if(ps_search_node->s_mv.i2_mvx == INTRA_MV)
                {
                    continue;
                }

                {
                    S08 i1_ref_idx = ps_search_node->i1_ref_idx;
                    subpel_dedup_enabler_t *ps_dedup_enabler =
                        &(as_subpel_dedup_enabler[i1_ref_idx]);

                    if(ps_dedup_enabler->u1_ref_idx == MAX_NUM_REF)
                    {
                        as_subpel_dedup_enabler[i1_ref_idx].i2_mv_x = ps_search_node->s_mv.i2_mvx;
                        as_subpel_dedup_enabler[i1_ref_idx].i2_mv_y = ps_search_node->s_mv.i2_mvy;
                        as_subpel_dedup_enabler[i1_ref_idx].u1_ref_idx = i1_ref_idx;
                        memset(
                            as_subpel_dedup_enabler[i1_ref_idx].au4_node_map,
                            0,
                            sizeof(U32) * 2 * MAP_X_MAX);
                    }
                }

                pred_lx = search_idx;
                ps_prms->pv_inp =
                    (void *)(ps_wt_inp_prms->apu1_wt_inp[ps_search_node->i1_ref_idx] + i4_inp_off);

                hme_subpel_refine_search_node_high_speed(
                    ps_search_node,
                    ps_prms,
                    ps_curr_layer,
                    e_blk_size,
                    x_off + ps_prms->i4_ctb_x_off,
                    y_off + ps_prms->i4_ctb_y_off,
                    ps_search_results,
                    pred_lx,
                    i4_part_mask,
                    &ps_subpel_refine_ctxt->ai4_part_id[0],
                    search_idx,
                    &(as_subpel_dedup_enabler[ps_search_node->i1_ref_idx]),
                    ps_func_selector,
                    ps_me_optimised_function_list);
            }
        }
    }
    else
    {
        for(i = 0; i < ps_subpel_refine_ctxt->i4_num_valid_parts; i++)
        {
            S32 i4_index;

            S32 i4_part_id = ps_subpel_refine_ctxt->ai4_part_id[i];

            if(ps_subpel_refine_ctxt->i4_num_valid_parts > 8)
            {
                i4_index = i4_part_id;
            }
            else
            {
                i4_index = i;
            }

            for(j = 0; j < ps_search_results->u1_num_results_per_part; j++)
            {
                ps_subpel_refine_ctxt->i2_mv_x[j][i4_index] <<= 2;
                ps_subpel_refine_ctxt->i2_mv_y[j][i4_index] <<= 2;
            }
        }
    }

    hme_subpel_refine_struct_to_search_results_struct_converter(
        ps_subpel_refine_ctxt, ps_search_results, search_idx, ps_prms->e_me_quality_presets);
}