aboutsummaryrefslogtreecommitdiff
path: root/encoder/hme_coarse.c
blob: f5cf9f81a661ee025aa92ced0de94db32ae8a6e7 (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
/******************************************************************************
 *
 * 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_coarse.c
*
* @brief
*    Contains ME algorithm for the coarse layer.
*
* @author
*    Ittiam
*
*
* List of Functions
* hme_update_mv_bank_coarse()
* hme_coarse()
******************************************************************************
*/

/*****************************************************************************/
/* 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_refine.h"
#include "hme_err_compute.h"
#include "hme_common_utils.h"
#include "hme_search_algo.h"

/*******************************************************************************
*                             MACROS
*******************************************************************************/
#define COPY_SEARCH_RESULT(ps_mv, pi1_ref_idx, ps_search_node, shift)                              \
    {                                                                                              \
        ps_mv->i2_mv_x = ps_search_node->s_mv.i2_mvx >> (shift);                                   \
        ps_mv->i2_mv_y = ps_search_node->s_mv.i2_mvy >> (shift);                                   \
        *pi1_ref_idx = ps_search_node->i1_ref_idx;                                                 \
    }

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

/**
********************************************************************************
*  @fn     void hme_update_mv_bank_coarse(search_results_t *ps_search_results,
*                                   layer_mv_t *ps_layer_mv,
*                                   S32 i4_blk_x,
*                                   S32 i4_blk_y,
*                                   search_node_t *ps_search_node_4x8_l,
*                                   search_node_t *ps_search_node_8x4_t,
*                                   S08 i1_ref_idx,
*                                   mvbank_update_prms_t *ps_prms
*
*  @brief  Updates the coarse layer MV Bank for a given ref id and blk pos
*
*  @param[in]  ps_search_results: Search results data structure
*
*  @param[in, out]  ps_layer_mv : MV Bank for this layer
*
*  @param[in]  i4_search_blk_x: column number of the 4x4 blk searched
*
*  @param[in]  i4_search_blk_y: row number of the 4x4 blk searched
*
*  @param[in]  ps_search_node_4x8_t: Best MV of the 4x8T blk
*
*  @param[in]  ps_search_node_8x4_l: Best MV of the 8x4L blk
*
*  @param[in]  i1_ref_idx : Reference ID that has been searched
*
*  @param[in]  ps_prms : Parameters pertaining to the MV Bank update
*
*  @return None
********************************************************************************
*/
void hme_update_mv_bank_coarse(
    search_results_t *ps_search_results,
    layer_mv_t *ps_layer_mv,
    S32 i4_search_blk_x,
    S32 i4_search_blk_y,
    search_node_t *ps_search_node_4x8_t,
    search_node_t *ps_search_node_8x4_l,
    S08 i1_ref_idx,
    mvbank_update_prms_t *ps_prms)
{
    /* These point to the MV and ref idx posn to be udpated */
    hme_mv_t *ps_mv;
    S08 *pi1_ref_idx;

    /* Offset within the bank */
    S32 i4_offset;

    S32 i, j, i4_blk_x, i4_blk_y;

    /* Best results for 8x4R and 4x8B blocks */
    search_node_t *ps_search_node_8x4_r, *ps_search_node_4x8_b;

    /* Number of MVs in a block */
    S32 num_mvs = ps_layer_mv->i4_num_mvs_per_ref;

    search_node_t *aps_search_nodes[4];

    /* The search blk may be different in size from the blk used to hold MV */
    i4_blk_x = i4_search_blk_x << ps_prms->i4_shift;
    i4_blk_y = i4_search_blk_y << ps_prms->i4_shift;

    /* Compute the offset in the MV bank */
    i4_offset = i4_blk_x + i4_blk_y * ps_layer_mv->i4_num_blks_per_row;
    i4_offset *= ps_layer_mv->i4_num_mvs_per_blk;

    /* Identify the correct offset in the mvbank and the reference id buf */
    ps_mv = ps_layer_mv->ps_mv + (i4_offset + (num_mvs * i1_ref_idx));
    pi1_ref_idx = ps_layer_mv->pi1_ref_idx + (i4_offset + (num_mvs * i1_ref_idx));

    /*************************************************************************/
    /* We have atleast 4 distinct results: the 4x8 top (coming from top blk) */
    /* 8x4 left (coming from left blk), 8x4 and 4x8 right and bot resp.      */
    /* If number of results to be stored is 4, then we store all these 4     */
    /* results, else we pick best ones                                       */
    /*************************************************************************/
    ps_search_node_8x4_r = ps_search_results->aps_part_results[i1_ref_idx][PART_ID_2NxN_B];
    ps_search_node_4x8_b = ps_search_results->aps_part_results[i1_ref_idx][PART_ID_Nx2N_R];

    ASSERT(num_mvs <= 4);

    /* Doing this to sort best results */
    aps_search_nodes[0] = ps_search_node_8x4_r;
    aps_search_nodes[1] = ps_search_node_4x8_b;
    aps_search_nodes[2] = ps_search_node_8x4_l;
    aps_search_nodes[3] = ps_search_node_4x8_t;
    if(num_mvs == 4)
    {
        COPY_SEARCH_RESULT(ps_mv, pi1_ref_idx, aps_search_nodes[0], 0);
        ps_mv++;
        pi1_ref_idx++;
        COPY_SEARCH_RESULT(ps_mv, pi1_ref_idx, aps_search_nodes[1], 0);
        ps_mv++;
        pi1_ref_idx++;
        COPY_SEARCH_RESULT(ps_mv, pi1_ref_idx, aps_search_nodes[2], 0);
        ps_mv++;
        pi1_ref_idx++;
        COPY_SEARCH_RESULT(ps_mv, pi1_ref_idx, aps_search_nodes[3], 0);
        ps_mv++;
        pi1_ref_idx++;
        return;
    }

    /* Run through the results, store them in best to worst order */
    for(i = 0; i < num_mvs; i++)
    {
        for(j = i + 1; j < 4; j++)
        {
            if(aps_search_nodes[j]->i4_tot_cost < aps_search_nodes[i]->i4_tot_cost)
            {
                SWAP_HME(aps_search_nodes[j], aps_search_nodes[i], search_node_t *);
            }
        }
        COPY_SEARCH_RESULT(ps_mv, pi1_ref_idx, aps_search_nodes[i], 0);
        ps_mv++;
        pi1_ref_idx++;
    }
}

/**
********************************************************************************
*  @fn     void hme_coarse_frm_init(me_ctxt_t *ps_ctxt, coarse_prms_t *ps_coarse_prms)
*
*  @brief  Frame init entry point Coarse ME.
*
*  @param[in,out]  ps_ctxt: ME Handle
*
*  @param[in]  ps_coarse_prms : Coarse layer config params
*
*  @return None
********************************************************************************
*/
void hme_coarse_frm_init(coarse_me_ctxt_t *ps_ctxt, coarse_prms_t *ps_coarse_prms)
{
    layer_ctxt_t *ps_curr_layer;

    S32 i4_pic_wd, i4_pic_ht;

    S32 num_blks_in_pic, num_blks_in_row;

    BLK_SIZE_T e_search_blk_size = BLK_4x4;

    S32 blk_size_shift = 2, blk_wd = 4, blk_ht = 4;

    /* Number of references to search */
    S32 i4_num_ref;

    ps_curr_layer = ps_ctxt->ps_curr_descr->aps_layers[ps_coarse_prms->i4_layer_id];
    i4_num_ref = ps_coarse_prms->i4_num_ref;

    i4_pic_wd = ps_curr_layer->i4_wd;
    i4_pic_ht = ps_curr_layer->i4_ht;
    /* Macro updates num_blks_in_pic and num_blks_in_row*/
    GET_NUM_BLKS_IN_PIC(i4_pic_wd, i4_pic_ht, blk_size_shift, num_blks_in_row, num_blks_in_pic);

    /************************************************************************/
    /* Initialize the mv bank that holds results of this layer.             */
    /************************************************************************/
    hme_init_mv_bank(
        ps_curr_layer,
        BLK_4x4,
        i4_num_ref,
        ps_coarse_prms->num_results,
        ps_ctxt->u1_encode[ps_coarse_prms->i4_layer_id]);

    return;
}

/**
********************************************************************************
*  @fn    void hme_derive_worst_case_search_range(range_prms_t *ps_range,
*                                   range_prms_t *ps_pic_limit,
*                                   range_prms_t *ps_mv_limit,
*                                   S32 i4_x,
*                                   S32 i4_y,
*                                   S32 blk_wd,
*                                   S32 blk_ht)
*
*  @brief  given picture limits and blk dimensions and mv search limits, obtains
*          teh valid search range such that the blk stays within pic boundaries,
*          where picture boundaries include padded portions of picture
*
*  @param[out] ps_range: updated with actual search range
*
*  @param[in] ps_pic_limit : picture boundaries
*
*  @param[in] ps_mv_limit: Search range limits for the mvs
*
*  @param[in] i4_x : x coordinate of the blk
*
*  @param[in] i4_y : y coordinate of the blk
*
*  @param[in] blk_wd : blk width
*
*  @param[in] blk_ht : blk height
*
*  @return void
********************************************************************************
*/
void hme_derive_worst_case_search_range(
    range_prms_t *ps_range,
    range_prms_t *ps_pic_limit,
    range_prms_t *ps_mv_limit,
    S32 i4_x,
    S32 i4_y,
    S32 blk_wd,
    S32 blk_ht)
{
    /* Taking max x of left block, min x of current block */
    ps_range->i2_max_x =
        MIN((ps_pic_limit->i2_max_x - (S16)blk_wd - (S16)(i4_x - 4)), ps_mv_limit->i2_max_x);
    ps_range->i2_min_x = MAX((ps_pic_limit->i2_min_x - (S16)i4_x), ps_mv_limit->i2_min_x);
    /* Taking max y of top block, min y of current block */
    ps_range->i2_max_y =
        MIN((ps_pic_limit->i2_max_y - (S16)blk_ht - (S16)(i4_y - 4)), ps_mv_limit->i2_max_y);
    ps_range->i2_min_y = MAX((ps_pic_limit->i2_min_y - (S16)i4_y), ps_mv_limit->i2_min_y);
}

/**
********************************************************************************
* @fn void hme_combine_4x4_sads_and_compute_cost(S08 i1_ref_idx,
*                                           range_prms_t *ps_mv_range,
*                                           range_prms_t *ps_mv_limit,
*                                           hme_mv_t *ps_best_mv_4x8,
*                                           hme_mv_t *ps_best_mv_8x4,
*                                           pred_ctxt_t *ps_pred_ctxt,
*                                           PF_MV_COST_FXN pf_mv_cost_compute,
*                                           ME_QUALITY_PRESETS_T e_me_quality_preset,
*                                           S16 *pi2_sads_4x4_current,
*                                           S16 *pi2_sads_4x4_east,
*                                           S16 *pi2_sads_4x4_south,
*                                           FILE *fp_dump_sad)
*
*  @brief  Does a full search on entire srch window with a given step size in coarse layer
*
*  @param[in] i1_ref_idx : Cur ref idx
*
*  @param[in] ps_layer_ctxt: All info about this layer
*
*  @param[out] ps_best_mv  : type hme_mv_t contains best mv x and y
*
*  @param[in] ps_pred_ctxt : Prediction ctxt for cost computation
*
*  @param[in] pf_mv_cost_compute : mv cost computation function
*
*  @return void
********************************************************************************
*/
void hme_combine_4x4_sads_and_compute_cost_high_quality(
    S08 i1_ref_idx,
    range_prms_t *ps_mv_range,
    range_prms_t *ps_mv_limit,
    hme_mv_t *ps_best_mv_4x8,
    hme_mv_t *ps_best_mv_8x4,
    pred_ctxt_t *ps_pred_ctxt,
    PF_MV_COST_FXN pf_mv_cost_compute,
    S16 *pi2_sads_4x4_current,
    S16 *pi2_sads_4x4_east,
    S16 *pi2_sads_4x4_south)
{
    /* These control number of parts and number of pts in grid to search */
    S32 stepy, stepx, best_mv_y_4x8, best_mv_x_4x8, best_mv_y_8x4, best_mv_x_8x4;
    S32 step_shift_x, step_shift_y;
    S32 mvx, mvy, mv_x_offset, mv_y_offset, mv_x_range, mv_y_range;

    S32 min_cost_4x8 = MAX_32BIT_VAL;
    S32 min_cost_8x4 = MAX_32BIT_VAL;

    search_node_t s_search_node;
    s_search_node.i1_ref_idx = i1_ref_idx;

    stepx = stepy = HME_COARSE_STEP_SIZE_HIGH_QUALITY;
    /*TODO: Calculate Step shift from the #define HME_COARSE_STEP_SIZE_HIGH_QUALITY */
    step_shift_x = step_shift_y = 1;

    mv_x_offset = (-ps_mv_limit->i2_min_x >> step_shift_x);
    mv_y_offset = (-ps_mv_limit->i2_min_y >> step_shift_y);
    mv_x_range = (-ps_mv_limit->i2_min_x + ps_mv_limit->i2_max_x) >> step_shift_x;
    mv_y_range = (-ps_mv_limit->i2_min_y + ps_mv_limit->i2_max_y) >> step_shift_y;

    /* Run 2loops to sweep over the reference area */
    for(mvy = ps_mv_range->i2_min_y; mvy < ps_mv_range->i2_max_y; mvy += stepy)
    {
        for(mvx = ps_mv_range->i2_min_x; mvx < ps_mv_range->i2_max_x; mvx += stepx)
        {
            S32 sad_4x8, cost_4x8, sad_8x4, cost_8x4;
            S32 sad_pos = ((mvx >> step_shift_x) + mv_x_offset) +
                          ((mvy >> step_shift_y) + mv_y_offset) * mv_x_range;

            /* Get SAD by adding SAD for current and neighbour S  */
            sad_4x8 = pi2_sads_4x4_current[sad_pos] + pi2_sads_4x4_south[sad_pos];
            sad_8x4 = pi2_sads_4x4_current[sad_pos] + pi2_sads_4x4_east[sad_pos];

            //          fprintf(fp_dump_sad,"%d\t",sad);
            s_search_node.s_mv.i2_mvx = mvx;
            s_search_node.s_mv.i2_mvy = mvy;

            cost_4x8 = cost_8x4 =
                pf_mv_cost_compute(&s_search_node, ps_pred_ctxt, PART_ID_2Nx2N, MV_RES_FPEL);

            cost_4x8 += sad_4x8;
            cost_8x4 += sad_8x4;

            if(cost_4x8 < min_cost_4x8)
            {
                best_mv_x_4x8 = mvx;
                best_mv_y_4x8 = mvy;
                min_cost_4x8 = cost_4x8;
            }
            if(cost_8x4 < min_cost_8x4)
            {
                best_mv_x_8x4 = mvx;
                best_mv_y_8x4 = mvy;
                min_cost_8x4 = cost_8x4;
            }
        }
    }

    ps_best_mv_4x8->i2_mv_x = best_mv_x_4x8;
    ps_best_mv_4x8->i2_mv_y = best_mv_y_4x8;

    ps_best_mv_8x4->i2_mv_x = best_mv_x_8x4;
    ps_best_mv_8x4->i2_mv_y = best_mv_y_8x4;
}

void hme_combine_4x4_sads_and_compute_cost_high_speed(
    S08 i1_ref_idx,
    range_prms_t *ps_mv_range,
    range_prms_t *ps_mv_limit,
    hme_mv_t *ps_best_mv_4x8,
    hme_mv_t *ps_best_mv_8x4,
    pred_ctxt_t *ps_pred_ctxt,
    PF_MV_COST_FXN pf_mv_cost_compute,
    S16 *pi2_sads_4x4_current,
    S16 *pi2_sads_4x4_east,
    S16 *pi2_sads_4x4_south)
{
    /* These control number of parts and number of pts in grid to search */
    S32 stepy, stepx, best_mv_y_4x8, best_mv_x_4x8, best_mv_y_8x4, best_mv_x_8x4;
    S32 step_shift_x, step_shift_y;
    S32 mvx, mvy, mv_x_offset, mv_y_offset, mv_x_range, mv_y_range;

    S32 rnd, lambda, lambda_q_shift;

    S32 min_cost_4x8 = MAX_32BIT_VAL;
    S32 min_cost_8x4 = MAX_32BIT_VAL;

    (void)pf_mv_cost_compute;
    stepx = stepy = HME_COARSE_STEP_SIZE_HIGH_SPEED;
    /*TODO: Calculate Step shift from the #define HME_COARSE_STEP_SIZE_HIGH_SPEED */
    step_shift_x = step_shift_y = 2;

    mv_x_offset = (-ps_mv_limit->i2_min_x >> step_shift_x);
    mv_y_offset = (-ps_mv_limit->i2_min_y >> step_shift_y);
    mv_x_range = (-ps_mv_limit->i2_min_x + ps_mv_limit->i2_max_x) >> step_shift_x;
    mv_y_range = (-ps_mv_limit->i2_min_y + ps_mv_limit->i2_max_y) >> step_shift_y;

    lambda = ps_pred_ctxt->lambda;
    lambda_q_shift = ps_pred_ctxt->lambda_q_shift;
    rnd = 1 << (lambda_q_shift - 1);

    ASSERT(MAX_MVX_SUPPORTED_IN_COARSE_LAYER >= ABS(ps_mv_range->i2_max_x));
    ASSERT(MAX_MVY_SUPPORTED_IN_COARSE_LAYER >= ABS(ps_mv_range->i2_max_y));

    /* Run 2loops to sweep over the reference area */
    for(mvy = ps_mv_range->i2_min_y; mvy < ps_mv_range->i2_max_y; mvy += stepy)
    {
        for(mvx = ps_mv_range->i2_min_x; mvx < ps_mv_range->i2_max_x; mvx += stepx)
        {
            S32 sad_4x8, cost_4x8, sad_8x4, cost_8x4;

            S32 sad_pos = ((mvx >> step_shift_x) + mv_x_offset) +
                          ((mvy >> step_shift_y) + mv_y_offset) * mv_x_range;

            /* Get SAD by adding SAD for current and neighbour S  */
            sad_4x8 = pi2_sads_4x4_current[sad_pos] + pi2_sads_4x4_south[sad_pos];
            sad_8x4 = pi2_sads_4x4_current[sad_pos] + pi2_sads_4x4_east[sad_pos];

            //          fprintf(fp_dump_sad,"%d\t",sad);

            cost_4x8 = cost_8x4 =
                (2 * hme_get_range(ABS(mvx)) - 1) + (2 * hme_get_range(ABS(mvy)) - 1) + i1_ref_idx;

            cost_4x8 += (mvx != 0) ? 1 : 0;
            cost_4x8 += (mvy != 0) ? 1 : 0;
            cost_4x8 = (cost_4x8 * lambda + rnd) >> lambda_q_shift;

            cost_8x4 += (mvx != 0) ? 1 : 0;
            cost_8x4 += (mvy != 0) ? 1 : 0;
            cost_8x4 = (cost_8x4 * lambda + rnd) >> lambda_q_shift;

            cost_4x8 += sad_4x8;
            cost_8x4 += sad_8x4;

            if(cost_4x8 < min_cost_4x8)
            {
                best_mv_x_4x8 = mvx;
                best_mv_y_4x8 = mvy;
                min_cost_4x8 = cost_4x8;
            }
            if(cost_8x4 < min_cost_8x4)
            {
                best_mv_x_8x4 = mvx;
                best_mv_y_8x4 = mvy;
                min_cost_8x4 = cost_8x4;
            }
        }
    }

    ps_best_mv_4x8->i2_mv_x = best_mv_x_4x8;
    ps_best_mv_4x8->i2_mv_y = best_mv_y_4x8;

    ps_best_mv_8x4->i2_mv_x = best_mv_x_8x4;
    ps_best_mv_8x4->i2_mv_y = best_mv_y_8x4;
}

/**
********************************************************************************
*  @fn     hme_store_4x4_sads(hme_search_prms_t *ps_search_prms,
*                               layer_ctxt_t *ps_layer_ctxt)
*
*  @brief  Does a 4x4 sad computation on a given range and stores it in memory
*
*  @param[in] ps_search_prms : Search prms structure containing info like
*               blk dimensions, search range etc
*
*  @param[in] ps_layer_ctxt: All info about this layer
*
*  @param[in] ps_wt_inp_prms: All info about weighted input
*
*  @param[in] e_me_quality_preset: motion estimation quality preset
*
*  @param[in] pi2_sads_4x4: Memory to store all 4x4 SADs for given range
*
*  @return void
********************************************************************************
*/

void hme_store_4x4_sads_high_quality(
    hme_search_prms_t *ps_search_prms,
    layer_ctxt_t *ps_layer_ctxt,
    range_prms_t *ps_mv_limit,
    wgt_pred_ctxt_t *ps_wt_inp_prms,
    S16 *pi2_sads_4x4)
{
    S32 sad, i, j;

    /* Input and reference attributes */
    U08 *pu1_inp, *pu1_inp_orig, *pu1_ref;
    S32 i4_inp_stride, i4_ref_stride, i4_ref_offset;

    /* The reference is actually an array of ptrs since there are several    */
    /* reference id. So an array gets passed form calling function           */
    U08 **ppu1_ref, *pu1_ref_coloc;

    S32 stepy, stepx, step_shift_x, step_shift_y;
    S32 mvx, mvy, mv_x_offset, mv_y_offset, mv_x_range, mv_y_range;

    /* Points to the range limits for mv */
    range_prms_t *ps_range_prms;

    /* Reference index to be searched */
    S32 i4_search_idx = ps_search_prms->i1_ref_idx;
    /* Using the member 0 to store for all ref. idx. */
    ps_range_prms = ps_search_prms->aps_mv_range[0];
    pu1_inp_orig = ps_wt_inp_prms->apu1_wt_inp[i4_search_idx];
    i4_inp_stride = ps_search_prms->i4_inp_stride;

    /* Move to the location of the search blk in inp buffer */
    pu1_inp_orig += ps_search_prms->i4_cu_x_off;
    pu1_inp_orig += ps_search_prms->i4_cu_y_off * i4_inp_stride;

    /*************************************************************************/
    /* we use either input of previously encoded pictures as reference       */
    /* in coarse layer                                                       */
    /*************************************************************************/
    i4_ref_stride = ps_layer_ctxt->i4_inp_stride;
    ppu1_ref = ps_layer_ctxt->ppu1_list_inp;

    /* colocated position in reference picture */
    i4_ref_offset = (i4_ref_stride * ps_search_prms->i4_y_off) + ps_search_prms->i4_x_off;
    pu1_ref_coloc = ppu1_ref[i4_search_idx] + i4_ref_offset;

    stepx = stepy = HME_COARSE_STEP_SIZE_HIGH_QUALITY;
    /*TODO: Calculate Step shift from the #define HME_COARSE_STEP_SIZE_HIGH_QUALITY */
    step_shift_x = step_shift_y = 1;

    mv_x_offset = -(ps_mv_limit->i2_min_x >> step_shift_x);
    mv_y_offset = -(ps_mv_limit->i2_min_y >> step_shift_y);
    mv_x_range = (-ps_mv_limit->i2_min_x + ps_mv_limit->i2_max_x) >> step_shift_x;
    mv_y_range = (-ps_mv_limit->i2_min_y + ps_mv_limit->i2_max_y) >> step_shift_y;

    /* Run 2loops to sweep over the reference area */
    for(mvy = ps_range_prms->i2_min_y; mvy < ps_range_prms->i2_max_y; mvy += stepy)
    {
        for(mvx = ps_range_prms->i2_min_x; mvx < ps_range_prms->i2_max_x; mvx += stepx)
        {
            /* Set up the reference and inp ptr */
            pu1_ref = pu1_ref_coloc + mvx + (mvy * i4_ref_stride);
            pu1_inp = pu1_inp_orig;
            /* SAD computation */
            {
                sad = 0;
                for(i = 0; i < 4; i++)
                {
                    for(j = 0; j < 4; j++)
                    {
                        sad += (ABS(((S32)pu1_inp[j] - (S32)pu1_ref[j])));
                    }
                    pu1_inp += i4_inp_stride;
                    pu1_ref += i4_ref_stride;
                }
            }

            pi2_sads_4x4
                [((mvx >> step_shift_x) + mv_x_offset) +
                 ((mvy >> step_shift_y) + mv_y_offset) * mv_x_range] = sad;
        }
    }
}

void hme_store_4x4_sads_high_speed(
    hme_search_prms_t *ps_search_prms,
    layer_ctxt_t *ps_layer_ctxt,
    range_prms_t *ps_mv_limit,
    wgt_pred_ctxt_t *ps_wt_inp_prms,
    S16 *pi2_sads_4x4)
{
    S32 sad, i, j;

    /* Input and reference attributes */
    U08 *pu1_inp, *pu1_inp_orig, *pu1_ref;
    S32 i4_inp_stride, i4_ref_stride, i4_ref_offset;

    /* The reference is actually an array of ptrs since there are several    */
    /* reference id. So an array gets passed form calling function           */
    U08 **ppu1_ref, *pu1_ref_coloc;

    S32 stepy, stepx, step_shift_x, step_shift_y;
    S32 mvx, mvy, mv_x_offset, mv_y_offset, mv_x_range, mv_y_range;

    /* Points to the range limits for mv */
    range_prms_t *ps_range_prms;

    /* Reference index to be searched */
    S32 i4_search_idx = ps_search_prms->i1_ref_idx;

    /* Using the member 0 for all ref. idx */
    ps_range_prms = ps_search_prms->aps_mv_range[0];
    pu1_inp_orig = ps_wt_inp_prms->apu1_wt_inp[i4_search_idx];
    i4_inp_stride = ps_search_prms->i4_inp_stride;

    /* Move to the location of the search blk in inp buffer */
    pu1_inp_orig += ps_search_prms->i4_cu_x_off;
    pu1_inp_orig += ps_search_prms->i4_cu_y_off * i4_inp_stride;

    /*************************************************************************/
    /* we use either input of previously encoded pictures as reference       */
    /* in coarse layer                                                       */
    /*************************************************************************/
    i4_ref_stride = ps_layer_ctxt->i4_inp_stride;
    ppu1_ref = ps_layer_ctxt->ppu1_list_inp;

    /* colocated position in reference picture */
    i4_ref_offset = (i4_ref_stride * ps_search_prms->i4_y_off) + ps_search_prms->i4_x_off;
    pu1_ref_coloc = ppu1_ref[i4_search_idx] + i4_ref_offset;

    stepx = stepy = HME_COARSE_STEP_SIZE_HIGH_SPEED;
    /*TODO: Calculate Step shift from the #define HME_COARSE_STEP_SIZE_HIGH_SPEED */
    step_shift_x = step_shift_y = 2;

    mv_x_offset = -(ps_mv_limit->i2_min_x >> step_shift_x);
    mv_y_offset = -(ps_mv_limit->i2_min_y >> step_shift_y);
    mv_x_range = (-ps_mv_limit->i2_min_x + ps_mv_limit->i2_max_x) >> step_shift_x;
    mv_y_range = (-ps_mv_limit->i2_min_y + ps_mv_limit->i2_max_y) >> step_shift_y;

    /* Run 2loops to sweep over the reference area */
    for(mvy = ps_range_prms->i2_min_y; mvy < ps_range_prms->i2_max_y; mvy += stepy)
    {
        for(mvx = ps_range_prms->i2_min_x; mvx < ps_range_prms->i2_max_x; mvx += stepx)
        {
            /* Set up the reference and inp ptr */
            pu1_ref = pu1_ref_coloc + mvx + (mvy * i4_ref_stride);
            pu1_inp = pu1_inp_orig;
            /* SAD computation */
            {
                sad = 0;
                for(i = 0; i < 4; i++)
                {
                    for(j = 0; j < 4; j++)
                    {
                        sad += (ABS(((S32)pu1_inp[j] - (S32)pu1_ref[j])));
                    }
                    pu1_inp += i4_inp_stride;
                    pu1_ref += i4_ref_stride;
                }
            }

            pi2_sads_4x4
                [((mvx >> step_shift_x) + mv_x_offset) +
                 ((mvy >> step_shift_y) + mv_y_offset) * mv_x_range] = sad;
        }
    }
}
/**
********************************************************************************
*  @fn     void hme_coarsest(me_ctxt_t *ps_ctxt, coarse_prms_t *ps_coarse_prms)
*
*  @brief  Top level entry point for Coarse ME. Runs across blks and searches
*          at a 4x4 blk granularity by using 4x8 and 8x4 patterns.
*
*  @param[in,out]  ps_ctxt: ME Handle
*
*  @param[in]  ps_coarse_prms : Coarse layer config params
*
*  @param[in]  ps_multi_thrd_ctxt : Multi thread context
*
*  @return None
********************************************************************************
*/
void hme_coarsest(
    coarse_me_ctxt_t *ps_ctxt,
    coarse_prms_t *ps_coarse_prms,
    multi_thrd_ctxt_t *ps_multi_thrd_ctxt,
    WORD32 i4_ping_pong,
    void **ppv_dep_mngr_hme_sync)
{
    S16 *pi2_cur_ref_sads_4x4;
    S32 ai4_sad_4x4_block_size[MAX_NUM_REF], ai4_sad_4x4_block_stride[MAX_NUM_REF];
    S32 num_rows_coarse;
    S32 sad_top_offset, sad_current_offset;
    S32 search_node_top_offset, search_node_left_offset;

    ME_QUALITY_PRESETS_T e_me_quality_preset =
        ps_ctxt->s_init_prms.s_me_coding_tools.e_me_quality_presets;

    search_results_t *ps_search_results;
    mvbank_update_prms_t s_mv_update_prms;
    BLK_SIZE_T e_search_blk_size = BLK_4x4;
    hme_search_prms_t s_search_prms_4x8, s_search_prms_8x4, s_search_prms_4x4;

    S32 global_id_8x4, global_id_4x8;

    /*************************************************************************/
    /* These directly point to the best search result nodes that will be     */
    /* updated by the search algorithm, rather than have to go through an    */
    /* elaborate structure                                                   */
    /*************************************************************************/
    search_node_t *aps_best_search_node_8x4[MAX_NUM_REF];
    search_node_t *aps_best_search_node_4x8[MAX_NUM_REF];

    /* These point to various spatial candts */
    search_node_t *ps_candt_8x4_l, *ps_candt_8x4_t, *ps_candt_8x4_tl;
    search_node_t *ps_candt_4x8_l, *ps_candt_4x8_t, *ps_candt_4x8_tl;
    search_node_t *ps_candt_zeromv_8x4, *ps_candt_zeromv_4x8;
    search_node_t *ps_candt_fs_8x4, *ps_candt_fs_4x8;
    search_node_t as_top_neighbours[4], as_left_neighbours[3];

    /* Holds the global mv for a given ref index */
    search_node_t s_candt_global[MAX_NUM_REF];

    /* All the search candidates */
    search_candt_t as_search_candts_8x4[MAX_INIT_CANDTS];
    search_candt_t as_search_candts_4x8[MAX_INIT_CANDTS];
    search_candt_t *ps_search_candts_8x4, *ps_search_candts_4x8;

    /* Actual range per blk and the pic level boundaries */
    range_prms_t s_range_prms, s_pic_limit, as_mv_limit[MAX_NUM_REF];

    /* Current and prev pic layer ctxt at the coarsest layer */
    layer_ctxt_t *ps_curr_layer, *ps_prev_layer;

    /* best mv of full search */
    hme_mv_t best_mv_4x8, best_mv_8x4;

    /* Book keeping at blk level */
    S32 blk_x, num_blks_in_pic, num_blks_in_row, num_4x4_blks_in_row;

    S32 blk_y;

    /* Block dimensions */
    S32 blk_size_shift = 2, blk_wd = 4, blk_ht = 4;

    S32 lambda = ps_coarse_prms->lambda;

    /* Number of references to search */
    S32 i4_num_ref;

    S32 i4_i, id, i;
    S08 i1_ref_idx;

    S32 i4_pic_wd, i4_pic_ht;
    S32 i4_layer_id;

    S32 end_of_frame;

    pf_get_wt_inp fp_get_wt_inp;

    /* Maximum search iterations around any candidate */
    S32 i4_max_iters = ps_coarse_prms->i4_max_iters;

    ps_curr_layer = ps_ctxt->ps_curr_descr->aps_layers[ps_coarse_prms->i4_layer_id];
    ps_prev_layer = hme_coarse_get_past_layer_ctxt(ps_ctxt, ps_coarse_prms->i4_layer_id);

    /* We need only one instance of search results structure */
    ps_search_results = &ps_ctxt->s_search_results_8x8;

    ps_search_candts_8x4 = &as_search_candts_8x4[0];
    ps_search_candts_4x8 = &as_search_candts_4x8[0];

    end_of_frame = 0;

    i4_pic_wd = ps_curr_layer->i4_wd;
    i4_pic_ht = ps_curr_layer->i4_ht;

    fp_get_wt_inp = ((ihevce_me_optimised_function_list_t *)ps_ctxt->pv_me_optimised_function_list)
                        ->pf_get_wt_inp_8x8;

    num_rows_coarse = ps_ctxt->i4_num_row_bufs;

    /*************************************************************************/
    /* Coarse Layer always does explicit search. Number of reference frames  */
    /* to search is a configurable parameter supplied by the application     */
    /*************************************************************************/
    i4_num_ref = ps_coarse_prms->i4_num_ref;
    i4_layer_id = ps_coarse_prms->i4_layer_id;

    /*************************************************************************/
    /*  The search algorithm goes as follows:                                */
    /*                                                                       */
    /*          ___                                                          */
    /*         | e |                                                         */
    /*      ___|___|___                                                      */
    /*     | c | a | b |                                                     */
    /*     |___|___|___|                                                     */
    /*         | d |                                                         */
    /*         |___|                                                         */
    /*                                                                       */
    /* For the target block a, we collect best results from 2 8x4 blks       */
    /* These are c-a and a-b. The 4x8 blks are e-a and a-d                   */
    /* c-a result is already available from results of blk c. a-b is         */
    /* evaluated in this blk. Likewise e-a result is stored in a row buffer  */
    /* a-d is evaluated this blk                                             */
    /* So we store a row buffer which stores best 4x8 results of all top blk */
    /*************************************************************************/

    /************************************************************************/
    /* Initialize the pointers to the best node.                            */
    /************************************************************************/
    for(i4_i = 0; i4_i < i4_num_ref; i4_i++)
    {
        aps_best_search_node_8x4[i4_i] = ps_search_results->aps_part_results[i4_i][PART_ID_2NxN_B];
        aps_best_search_node_4x8[i4_i] = ps_search_results->aps_part_results[i4_i][PART_ID_Nx2N_R];
    }

    /************************************************************************/
    /* Initialize the "searchresults" structure. This will set up the number*/
    /* of search types, result updates etc                                  */
    /************************************************************************/
    {
        S32 num_results_per_part;
        /* We evaluate 4 types of results per 4x4 blk. 8x4L and 8x4R and     */
        /* 4x8 T and 4x8B. So if we are to give 4 results, then we need to   */
        /* only evaluate 1 result per part. In the coarse layer, we are      */
        /* limited to 2 results max per part, and max of 8 results.          */
        num_results_per_part = (ps_coarse_prms->num_results + 3) >> 2;
        hme_init_search_results(
            ps_search_results,
            i4_num_ref,
            ps_coarse_prms->num_results,
            num_results_per_part,
            BLK_8x8,
            0,
            0,
            ps_ctxt->au1_is_past);
    }

    /* Macro updates num_blks_in_pic and num_blks_in_row*/
    GET_NUM_BLKS_IN_PIC(i4_pic_wd, i4_pic_ht, blk_size_shift, num_blks_in_row, num_blks_in_pic);

    num_4x4_blks_in_row = num_blks_in_row + 1;

    s_mv_update_prms.e_search_blk_size = e_search_blk_size;
    s_mv_update_prms.i4_num_ref = i4_num_ref;
    s_mv_update_prms.i4_shift = 0;

    /* For full search, support 2 or 4 step size */
    if(ps_coarse_prms->do_full_search)
    {
        ASSERT((ps_coarse_prms->full_search_step == 2) || (ps_coarse_prms->full_search_step == 4));
    }

    for(i4_i = 0; i4_i < i4_num_ref; i4_i++)
    {
        S32 blk, delta_poc;
        S32 mv_x_clip, mv_y_clip;
        /* Initialize only the first row */
        for(blk = 0; blk < num_blks_in_row; blk++)
        {
            INIT_SEARCH_NODE(&ps_ctxt->aps_best_search_nodes_4x8_n_rows[i4_i][blk], i4_i);
        }

        delta_poc = ABS(ps_curr_layer->i4_poc - ps_curr_layer->ai4_ref_id_to_poc_lc[i4_i]);

        /* Setting search range for different references based on the delta poc */
        /*************************************************************************/
        /* set the MV limit per ref. pic.                                        */
        /*    - P pic. : Based on the config params.                             */
        /*    - B/b pic: Based on the Max/Min MV from prev. P and config. param. */
        /*************************************************************************/
        {
            /* TO DO : Remove hard coding of P-P dist. of 4 */
            mv_x_clip = (ps_curr_layer->i2_max_mv_x * delta_poc) / 4;

            /* Only for B/b pic. */
            if(1 == ps_ctxt->s_frm_prms.bidir_enabled)
            {
                WORD16 i2_mv_y_per_poc;

                /* Get abs MAX for symmetric search */
                i2_mv_y_per_poc =
                    MAX(ps_ctxt->s_coarse_dyn_range_prms.i2_dyn_max_y_per_poc[i4_layer_id],
                        (ABS(ps_ctxt->s_coarse_dyn_range_prms.i2_dyn_min_y_per_poc[i4_layer_id])));

                mv_y_clip = i2_mv_y_per_poc * delta_poc;
            }
            /* Set the Config. File Params for P pic. */
            else
            {
                /* TO DO : Remove hard coding of P-P dist. of 4 */
                mv_y_clip = (ps_curr_layer->i2_max_mv_y * delta_poc) / 4;
            }

            /* Making mv_x and mv_y range multiple of 4 */
            mv_x_clip = (((mv_x_clip + 3) >> 2) << 2);
            mv_y_clip = (((mv_y_clip + 3) >> 2) << 2);
            /* Clipping the range of mv_x and mv_y */
            mv_x_clip = CLIP3(mv_x_clip, 4, MAX_MVX_SUPPORTED_IN_COARSE_LAYER);
            mv_y_clip = CLIP3(mv_y_clip, 4, MAX_MVY_SUPPORTED_IN_COARSE_LAYER);

            as_mv_limit[i4_i].i2_min_x = -mv_x_clip;
            as_mv_limit[i4_i].i2_min_y = -mv_y_clip;
            as_mv_limit[i4_i].i2_max_x = mv_x_clip;
            as_mv_limit[i4_i].i2_max_y = mv_y_clip;
        }
        /*Populating SAD block size based on search range */
        ai4_sad_4x4_block_size[i4_i] = ((2 * mv_x_clip) / ps_coarse_prms->full_search_step) *
                                       ((2 * mv_y_clip) / ps_coarse_prms->full_search_step);
        ai4_sad_4x4_block_stride[i4_i] = (num_blks_in_row + 1) * ai4_sad_4x4_block_size[i4_i];
    }

    for(i = 0; i < 2 * MAX_INIT_CANDTS; i++)
    {
        search_node_t *ps_search_node;
        ps_search_node = &ps_ctxt->s_init_search_node[i];
        INIT_SEARCH_NODE(ps_search_node, 0);
    }
    for(i = 0; i < 3; i++)
    {
        search_node_t *ps_search_node;
        ps_search_node = &as_left_neighbours[i];
        INIT_SEARCH_NODE(ps_search_node, 0);
        ps_search_node = &as_top_neighbours[i];
        INIT_SEARCH_NODE(ps_search_node, 0);
    }
    INIT_SEARCH_NODE(&as_top_neighbours[3], 0);
    /* Set up place holders to hold the search nodes of each initial candt */
    for(i = 0; i < MAX_INIT_CANDTS; i++)
    {
        ps_search_candts_8x4[i].ps_search_node = &ps_ctxt->s_init_search_node[i];

        ps_search_candts_4x8[i].ps_search_node = &ps_ctxt->s_init_search_node[MAX_INIT_CANDTS + i];

        ps_search_candts_8x4[i].u1_num_steps_refine = (U08)i4_max_iters;
        ps_search_candts_4x8[i].u1_num_steps_refine = (U08)i4_max_iters;
    }

    /* For Top,TopLeft and Left cand., no need for refinement */
    id = 0;
    if((ps_coarse_prms->do_full_search) && (ME_XTREME_SPEED_25 == e_me_quality_preset))
    {
        /* This search candt has the full search result */
        ps_candt_fs_8x4 = ps_search_candts_8x4[id].ps_search_node;
        id++;
    }

    ps_candt_8x4_l = ps_search_candts_8x4[id].ps_search_node;
    ps_search_candts_8x4[id].u1_num_steps_refine = 0;
    id++;
    ps_candt_8x4_t = ps_search_candts_8x4[id].ps_search_node;
    ps_search_candts_8x4[id].u1_num_steps_refine = 0;
    id++;
    ps_candt_8x4_tl = ps_search_candts_8x4[id].ps_search_node;
    ps_search_candts_8x4[id].u1_num_steps_refine = 0;
    id++;
    /* This search candt stores the global candt */
    global_id_8x4 = id;
    id++;

    if((ps_coarse_prms->do_full_search) && (ME_XTREME_SPEED_25 != e_me_quality_preset))
    {
        /* This search candt has the full search result */
        ps_candt_fs_8x4 = ps_search_candts_8x4[id].ps_search_node;
        id++;
    }
    /* Don't increment id as (0,0) is removed from cand. list. Initializing */
    /* the pointer for hme_init_pred_ctxt_no_encode()                       */
    ps_candt_zeromv_8x4 = ps_search_candts_8x4[id].ps_search_node;

    /* For Top,TopLeft and Left cand., no need for refinement */
    id = 0;
    if((ps_coarse_prms->do_full_search) && (ME_XTREME_SPEED_25 == e_me_quality_preset))
    {
        /* This search candt has the full search result */
        ps_candt_fs_4x8 = ps_search_candts_4x8[id].ps_search_node;
        id++;
    }

    ps_candt_4x8_l = ps_search_candts_4x8[id].ps_search_node;
    ps_search_candts_4x8[id].u1_num_steps_refine = 0;
    id++;
    ps_candt_4x8_t = ps_search_candts_4x8[id].ps_search_node;
    ps_search_candts_4x8[id].u1_num_steps_refine = 0;
    id++;
    ps_candt_4x8_tl = ps_search_candts_4x8[id].ps_search_node;
    ps_search_candts_4x8[id].u1_num_steps_refine = 0;
    id++;
    /* This search candt stores the global candt */
    global_id_4x8 = id;
    id++;
    if((ps_coarse_prms->do_full_search) && (ME_XTREME_SPEED_25 != e_me_quality_preset))
    {
        /* This search candt has the full search result */
        ps_candt_fs_4x8 = ps_search_candts_4x8[id].ps_search_node;
        id++;
    }
    /* Don't increment id4as (0,0) is removed from cand. list. Initializing */
    /* the pointer for hme_init_pred_ctxt_no_encode()                       */
    ps_candt_zeromv_4x8 = ps_search_candts_4x8[id].ps_search_node;

    /* Zero mv always has 0 mvx and y componnent, ref idx initialized inside */
    ps_candt_zeromv_8x4->s_mv.i2_mvx = 0;
    ps_candt_zeromv_8x4->s_mv.i2_mvy = 0;
    ps_candt_zeromv_4x8->s_mv.i2_mvx = 0;
    ps_candt_zeromv_4x8->s_mv.i2_mvy = 0;

    /* SET UP THE PRED CTXT FOR L0 AND L1 */
    {
        S32 pred_lx;

        /* Bottom left always not available */
        as_left_neighbours[2].u1_is_avail = 0;

        for(pred_lx = 0; pred_lx < 2; pred_lx++)
        {
            pred_ctxt_t *ps_pred_ctxt;

            ps_pred_ctxt = &ps_search_results->as_pred_ctxt[pred_lx];
            hme_init_pred_ctxt_no_encode(
                ps_pred_ctxt,
                ps_search_results,
                as_top_neighbours,
                as_left_neighbours,
                NULL,
                ps_candt_zeromv_8x4,
                ps_candt_zeromv_8x4,
                pred_lx,
                lambda,
                ps_coarse_prms->lambda_q_shift,
                ps_ctxt->apu1_ref_bits_tlu_lc,
                ps_ctxt->ai2_ref_scf);
        }
    }

    /*************************************************************************/
    /* Initialize the search parameters for search algo with the following   */
    /* parameters: No SATD, calculated number of initial candidates,         */
    /* No post refinement, initial step size and number of iterations as     */
    /* passed by the calling function.                                       */
    /* Also, we use input for this layer search, and not recon.              */
    /*************************************************************************/
    if(e_me_quality_preset == ME_XTREME_SPEED_25)
        s_search_prms_8x4.i4_num_init_candts = 1;
    else
        s_search_prms_8x4.i4_num_init_candts = id;
    s_search_prms_8x4.i4_use_satd = 0;
    s_search_prms_8x4.i4_start_step = ps_coarse_prms->i4_start_step;
    s_search_prms_8x4.i4_num_steps_post_refine = 0;
    s_search_prms_8x4.i4_use_rec = 0;
    s_search_prms_8x4.ps_search_candts = ps_search_candts_8x4;
    s_search_prms_8x4.e_blk_size = BLK_8x4;
    s_search_prms_8x4.i4_max_iters = ps_coarse_prms->i4_max_iters;
    /* Coarse layer is always explicit */
    if(ME_MEDIUM_SPEED > e_me_quality_preset)
    {
        s_search_prms_8x4.pf_mv_cost_compute = compute_mv_cost_coarse;
    }
    else
    {
        s_search_prms_8x4.pf_mv_cost_compute = compute_mv_cost_coarse_high_speed;
    }

    s_search_prms_8x4.i4_inp_stride = 8;
    s_search_prms_8x4.i4_cu_x_off = s_search_prms_8x4.i4_cu_y_off = 0;
    if(ps_coarse_prms->do_full_search)
        s_search_prms_8x4.i4_max_iters = 1;
    s_search_prms_8x4.i4_part_mask = (1 << PART_ID_2NxN_B);
    /* Using the member 0 to store for all ref. idx. */
    s_search_prms_8x4.aps_mv_range[0] = &s_range_prms;
    s_search_prms_8x4.ps_search_results = ps_search_results;
    s_search_prms_8x4.full_search_step = ps_coarse_prms->full_search_step;

    s_search_prms_4x8 = s_search_prms_8x4;
    s_search_prms_4x8.ps_search_candts = ps_search_candts_4x8;
    s_search_prms_4x8.e_blk_size = BLK_4x8;
    s_search_prms_4x8.i4_part_mask = (1 << PART_ID_Nx2N_R);

    s_search_prms_4x4 = s_search_prms_8x4;
    /* Since s_search_prms_4x4 is used only to computer sad at 4x4 level, search candidate is not used */
    s_search_prms_4x4.ps_search_candts = ps_search_candts_4x8;
    s_search_prms_4x4.e_blk_size = BLK_4x4;
    s_search_prms_4x4.i4_part_mask = (1 << PART_ID_2Nx2N);
    /*************************************************************************/
    /* Picture limit on all 4 sides. This will be used to set mv limits for  */
    /* every block given its coordinate.                                     */
    /*************************************************************************/
    SET_PIC_LIMIT(
        s_pic_limit,
        ps_curr_layer->i4_pad_x_inp,
        ps_curr_layer->i4_pad_y_inp,
        ps_curr_layer->i4_wd,
        ps_curr_layer->i4_ht,
        s_search_prms_4x4.i4_num_steps_post_refine);

    /* Pick the global mv from previous reference */
    for(i1_ref_idx = 0; i1_ref_idx < i4_num_ref; i1_ref_idx++)
    {
        if(ME_XTREME_SPEED_25 != e_me_quality_preset)
        {
            /* Distance of current pic from reference */
            S32 i4_delta_poc;

            hme_mv_t s_mv;
            i4_delta_poc = ps_curr_layer->i4_poc - ps_curr_layer->ai4_ref_id_to_poc_lc[i1_ref_idx];

            hme_get_global_mv(ps_prev_layer, &s_mv, i4_delta_poc);

            s_candt_global[i1_ref_idx].s_mv.i2_mvx = s_mv.i2_mv_x;
            s_candt_global[i1_ref_idx].s_mv.i2_mvy = s_mv.i2_mv_y;
            s_candt_global[i1_ref_idx].i1_ref_idx = i1_ref_idx;

            /*********************************************************************/
            /* Initialize the histogram for each reference index in current      */
            /* layer ctxt                                                        */
            /*********************************************************************/
            hme_init_histogram(
                ps_ctxt->aps_mv_hist[i1_ref_idx],
                (S32)as_mv_limit[i1_ref_idx].i2_max_x,
                (S32)as_mv_limit[i1_ref_idx].i2_max_y);
        }

        /*********************************************************************/
        /* Initialize the dyn. search range params. for each reference index */
        /* in current layer ctxt                                             */
        /*********************************************************************/
        /* Only for P pic. For P, both are 0, I&B has them mut. exclusive */
        if(ps_ctxt->s_frm_prms.is_i_pic == ps_ctxt->s_frm_prms.bidir_enabled)
        {
            INIT_DYN_SEARCH_PRMS(
                &ps_ctxt->s_coarse_dyn_range_prms.as_dyn_range_prms[i4_layer_id][i1_ref_idx],
                ps_curr_layer->ai4_ref_id_to_poc_lc[i1_ref_idx]);
        }
    }

    /*************************************************************************/
    /* if exhaustive algorithmm then we use only 1 candt 0, 0                */
    /* else we use a lot of causal and non causal candts                     */
    /* finally set number to the configured number of candts                 */
    /*************************************************************************/

    /* Loop in raster order over each 4x4 blk in a given row till end of frame */
    while(0 == end_of_frame)
    {
        job_queue_t *ps_job;
        void *pv_hme_dep_mngr;
        WORD32 offset_val, check_dep_pos, set_dep_pos;

        /* Get the current layer HME Dep Mngr       */
        /* Note : Use layer_id - 1 in HME layers    */
        pv_hme_dep_mngr = ppv_dep_mngr_hme_sync[ps_coarse_prms->i4_layer_id - 1];

        /* Get the current row from the job queue */
        ps_job = (job_queue_t *)ihevce_pre_enc_grp_get_next_job(
            ps_multi_thrd_ctxt, ps_multi_thrd_ctxt->i4_me_coarsest_lyr_type, 1, i4_ping_pong);

        /* If all rows are done, set the end of process flag to 1, */
        /* and the current row to -1 */
        if(NULL == ps_job)
        {
            blk_y = -1;
            end_of_frame = 1;
        }
        else
        {
            ASSERT(ps_multi_thrd_ctxt->i4_me_coarsest_lyr_type == ps_job->i4_pre_enc_task_type);

            /* Obtain the current row's details from the job */
            blk_y = ps_job->s_job_info.s_me_job_info.i4_vert_unit_row_no;

            if(1 == ps_ctxt->s_frm_prms.is_i_pic)
            {
                /* set the output dependency of current row */
                ihevce_pre_enc_grp_job_set_out_dep(ps_multi_thrd_ctxt, ps_job, i4_ping_pong);
                continue;
            }

            /* Set Variables for Dep. Checking and Setting */
            set_dep_pos = blk_y + 1;
            if(blk_y > 0)
            {
                offset_val = 2;
                check_dep_pos = blk_y - 1;
            }
            else
            {
                /* First row should run without waiting */
                offset_val = -1;
                check_dep_pos = 0;
            }

            /* Loop over all the blocks in current row */
            /* One block extra, since the last block in a row needs East block */
            for(blk_x = 0; blk_x < (num_blks_in_row + 1); blk_x++)
            {
                /* Wait till top row block is processed   */
                /* Currently checking till top right block*/
                if(blk_x < (num_blks_in_row))
                {
                    ihevce_dmgr_chk_row_row_sync(
                        pv_hme_dep_mngr,
                        blk_x,
                        offset_val,
                        check_dep_pos,
                        0, /* Col Tile No. : Not supported in PreEnc*/
                        ps_ctxt->thrd_id);
                }

                /***************************************************************/
                /* Get Weighted input for all references                       */
                /***************************************************************/
                fp_get_wt_inp(
                    ps_curr_layer,
                    &ps_ctxt->s_wt_pred,
                    1 << (blk_size_shift + 1),
                    blk_x << blk_size_shift,
                    (blk_y - 1) << blk_size_shift,
                    1 << (blk_size_shift + 1),
                    i4_num_ref,
                    ps_ctxt->i4_wt_pred_enable_flag);

                /* RESET ALL SEARCH RESULTS FOR THE NEW BLK */
                hme_reset_search_results(
                    ps_search_results,
                    s_search_prms_8x4.i4_part_mask | s_search_prms_4x8.i4_part_mask,
                    MV_RES_FPEL);

                /* Compute the search node offsets */
                /* MAX is used to clip when left and top neighbours are not availbale at coarse boundaries  */
                search_node_top_offset =
                    blk_x + ps_ctxt->ai4_row_index[MAX((blk_y - 2), 0)] * num_blks_in_row;
                search_node_left_offset =
                    MAX((blk_x - 1), 0) +
                    ps_ctxt->ai4_row_index[MAX((blk_y - 1), 0)] * num_blks_in_row;

                /* Input offset: wrt CU start. Offset for South block */
                s_search_prms_4x4.i4_cu_x_off = 0;
                s_search_prms_4x4.i4_cu_y_off = 4;
                s_search_prms_4x4.i4_inp_stride = 8;
                s_search_prms_4x4.i4_x_off = blk_x << blk_size_shift;
                s_search_prms_4x4.i4_y_off = blk_y << blk_size_shift;

                s_search_prms_4x8.i4_x_off = s_search_prms_8x4.i4_x_off = blk_x << blk_size_shift;
                s_search_prms_4x8.i4_y_off = s_search_prms_8x4.i4_y_off = (blk_y - 1)
                                                                          << blk_size_shift;

                /* This layer will always use explicit ME */
                /* Loop across different Ref IDx */
                for(i1_ref_idx = 0; i1_ref_idx < i4_num_ref; i1_ref_idx++)
                {
                    sad_top_offset = (blk_x * ai4_sad_4x4_block_size[i1_ref_idx]) +
                                     ps_ctxt->ai4_row_index[MAX((blk_y - 1), 0)] *
                                         ai4_sad_4x4_block_stride[i1_ref_idx];
                    sad_current_offset =
                        (blk_x * ai4_sad_4x4_block_size[i1_ref_idx]) +
                        ps_ctxt->ai4_row_index[blk_y] * ai4_sad_4x4_block_stride[i1_ref_idx];

                    /* Initialize search node if blk_x == 0, as it doesn't have left neighbours */
                    if(0 == blk_x)
                        INIT_SEARCH_NODE(
                            &ps_ctxt->aps_best_search_nodes_8x4_n_rows[i1_ref_idx][blk_x],
                            i1_ref_idx);

                    pi2_cur_ref_sads_4x4 = ps_ctxt->api2_sads_4x4_n_rows[i1_ref_idx];

                    /* Initialize changing params here */
                    s_search_prms_8x4.i1_ref_idx = i1_ref_idx;
                    s_search_prms_4x8.i1_ref_idx = i1_ref_idx;
                    s_search_prms_4x4.i1_ref_idx = i1_ref_idx;

                    if(num_blks_in_row == blk_x)
                    {
                        S16 *pi2_sads_4x4_current;
                        /* Since the current 4x4 block will be a padded region, which may not match with any of the reference  */
                        pi2_sads_4x4_current = pi2_cur_ref_sads_4x4 + sad_current_offset;

                        memset(pi2_sads_4x4_current, 0, ai4_sad_4x4_block_size[i1_ref_idx]);
                    }

                    /* SAD to be computed and stored for the 4x4 block in 1st row and the last block of all rows*/
                    if((0 == blk_y) || (num_blks_in_row == blk_x))
                    {
                        S16 *pi2_sads_4x4_current;
                        /* Computer 4x4 SADs for current block */
                        /* Pointer to store SADs */
                        pi2_sads_4x4_current = pi2_cur_ref_sads_4x4 + sad_current_offset;

                        hme_derive_worst_case_search_range(
                            &s_range_prms,
                            &s_pic_limit,
                            &as_mv_limit[i1_ref_idx],
                            blk_x << blk_size_shift,
                            blk_y << blk_size_shift,
                            blk_wd,
                            blk_ht);

                        if(ME_PRISTINE_QUALITY >= e_me_quality_preset)
                        {
                            ((ihevce_me_optimised_function_list_t *)
                                 ps_ctxt->pv_me_optimised_function_list)
                                ->pf_store_4x4_sads_high_quality(
                                    &s_search_prms_4x4,
                                    ps_curr_layer,
                                    &as_mv_limit[i1_ref_idx],
                                    &ps_ctxt->s_wt_pred,
                                    pi2_sads_4x4_current);
                        }
                        else
                        {
                            ((ihevce_me_optimised_function_list_t *)
                                 ps_ctxt->pv_me_optimised_function_list)
                                ->pf_store_4x4_sads_high_speed(
                                    &s_search_prms_4x4,
                                    ps_curr_layer,
                                    &as_mv_limit[i1_ref_idx],
                                    &ps_ctxt->s_wt_pred,
                                    pi2_sads_4x4_current);
                        }
                    }
                    else
                    {
                        /* For the zero mv candt, the ref idx to be modified */
                        ps_candt_zeromv_8x4->i1_ref_idx = i1_ref_idx;
                        ps_candt_zeromv_4x8->i1_ref_idx = i1_ref_idx;

                        if(ME_XTREME_SPEED_25 != e_me_quality_preset)
                        {
                            /* For the global mvs alone, the search node points to a local variable */
                            ps_search_candts_8x4[global_id_8x4].ps_search_node =
                                &s_candt_global[i1_ref_idx];
                            ps_search_candts_4x8[global_id_4x8].ps_search_node =
                                &s_candt_global[i1_ref_idx];
                        }

                        hme_get_spatial_candt(
                            ps_curr_layer,
                            BLK_4x4,
                            blk_x,
                            blk_y - 1,
                            i1_ref_idx,
                            as_top_neighbours,
                            as_left_neighbours,
                            0,
                            1,
                            0,
                            0);
                        /* set up the various candts */
                        *ps_candt_4x8_l = as_left_neighbours[0];
                        *ps_candt_4x8_t = as_top_neighbours[1];
                        *ps_candt_4x8_tl = as_top_neighbours[0];
                        *ps_candt_8x4_l = *ps_candt_4x8_l;
                        *ps_candt_8x4_tl = *ps_candt_4x8_tl;
                        *ps_candt_8x4_t = *ps_candt_4x8_t;

                        {
                            S32 pred_lx;
                            S16 *pi2_sads_4x4_current, *pi2_sads_4x4_top;
                            pred_ctxt_t *ps_pred_ctxt;
                            PF_MV_COST_FXN pf_mv_cost_compute;

                            /* Computer 4x4 SADs for current block */
                            /* Pointer to store SADs */
                            pi2_sads_4x4_current = pi2_cur_ref_sads_4x4 + sad_current_offset;

                            hme_derive_worst_case_search_range(
                                &s_range_prms,
                                &s_pic_limit,
                                &as_mv_limit[i1_ref_idx],
                                blk_x << blk_size_shift,
                                blk_y << blk_size_shift,
                                blk_wd,
                                blk_ht);
                            if(i4_pic_ht == blk_y)
                            {
                                memset(pi2_sads_4x4_current, 0, ai4_sad_4x4_block_size[i1_ref_idx]);
                            }
                            else
                            {
                                if(ME_PRISTINE_QUALITY >= e_me_quality_preset)
                                {
                                    ((ihevce_me_optimised_function_list_t *)
                                         ps_ctxt->pv_me_optimised_function_list)
                                        ->pf_store_4x4_sads_high_quality(
                                            &s_search_prms_4x4,
                                            ps_curr_layer,
                                            &as_mv_limit[i1_ref_idx],
                                            &ps_ctxt->s_wt_pred,
                                            pi2_sads_4x4_current);
                                }
                                else
                                {
                                    ((ihevce_me_optimised_function_list_t *)
                                         ps_ctxt->pv_me_optimised_function_list)
                                        ->pf_store_4x4_sads_high_speed(
                                            &s_search_prms_4x4,
                                            ps_curr_layer,
                                            &as_mv_limit[i1_ref_idx],
                                            &ps_ctxt->s_wt_pred,
                                            pi2_sads_4x4_current);
                                }
                            }
                            /* Set pred direction to L0 or L1 */
                            pred_lx = 1 - ps_search_results->pu1_is_past[i1_ref_idx];

                            /* Suitable context (L0 or L1) */
                            ps_pred_ctxt = &ps_search_results->as_pred_ctxt[pred_lx];

                            /* Coarse layer is always explicit */
                            if(ME_PRISTINE_QUALITY > e_me_quality_preset)
                            {
                                pf_mv_cost_compute = compute_mv_cost_coarse;
                            }
                            else
                            {
                                /* Cost function is not called in high speed case. Below one is just a dummy function */
                                pf_mv_cost_compute = compute_mv_cost_coarse_high_speed;
                            }

                            /*********************************************************************/
                            /* Now, compute the mv for the top block                             */
                            /*********************************************************************/
                            pi2_sads_4x4_top = pi2_cur_ref_sads_4x4 + sad_top_offset;

                            /*********************************************************************/
                            /* For every blk in the picture, the search range needs to be derived*/
                            /* Any blk can have any mv, but practical search constraints are     */
                            /* imposed by the picture boundary and amt of padding.               */
                            /*********************************************************************/
                            hme_derive_search_range(
                                &s_range_prms,
                                &s_pic_limit,
                                &as_mv_limit[i1_ref_idx],
                                blk_x << blk_size_shift,
                                (blk_y - 1) << blk_size_shift,
                                blk_wd,
                                blk_ht);

                            /* Computer the mv for the top block */
                            if(ME_PRISTINE_QUALITY >= e_me_quality_preset)
                            {
                                ((ihevce_me_optimised_function_list_t *)
                                     ps_ctxt->pv_me_optimised_function_list)
                                    ->pf_combine_4x4_sads_and_compute_cost_high_quality(
                                        i1_ref_idx,
                                        &s_range_prms, /* Both 4x8 and 8x4 has same search range */
                                        &as_mv_limit[i1_ref_idx],
                                        &best_mv_4x8,
                                        &best_mv_8x4,
                                        ps_pred_ctxt,
                                        pf_mv_cost_compute,
                                        pi2_sads_4x4_top, /* Current SAD block */
                                        (pi2_sads_4x4_top +
                                         ai4_sad_4x4_block_size[i1_ref_idx]), /* East SAD block */
                                        pi2_sads_4x4_current); /* South SAD block */
                            }
                            else
                            {
                                ((ihevce_me_optimised_function_list_t *)
                                     ps_ctxt->pv_me_optimised_function_list)
                                    ->pf_combine_4x4_sads_and_compute_cost_high_speed(
                                        i1_ref_idx,
                                        &s_range_prms, /* Both 4x8 and 8x4 has same search range */
                                        &as_mv_limit[i1_ref_idx],
                                        &best_mv_4x8,
                                        &best_mv_8x4,
                                        ps_pred_ctxt,
                                        pf_mv_cost_compute,
                                        pi2_sads_4x4_top, /* Current SAD block */
                                        (pi2_sads_4x4_top +
                                         ai4_sad_4x4_block_size[i1_ref_idx]), /* East SAD block */
                                        pi2_sads_4x4_current); /* South SAD block */
                            }

                            ps_candt_fs_4x8->s_mv.i2_mvx = best_mv_4x8.i2_mv_x;
                            ps_candt_fs_4x8->s_mv.i2_mvy = best_mv_4x8.i2_mv_y;
                            ps_candt_fs_4x8->i1_ref_idx = i1_ref_idx;

                            ps_candt_fs_8x4->s_mv.i2_mvx = best_mv_8x4.i2_mv_x;
                            ps_candt_fs_8x4->s_mv.i2_mvy = best_mv_8x4.i2_mv_y;
                            ps_candt_fs_8x4->i1_ref_idx = i1_ref_idx;
                        }

                        /* call the appropriate Search Algo for 4x8S. The 4x8N would  */
                        /* have already been called by top block */
                        hme_pred_search_square_stepn(
                            &s_search_prms_8x4,
                            ps_curr_layer,
                            &ps_ctxt->s_wt_pred,
                            e_me_quality_preset,
                            (ihevce_me_optimised_function_list_t *)
                                ps_ctxt->pv_me_optimised_function_list

                        );

                        /* Call the appropriate search algo for 8x4E */
                        hme_pred_search_square_stepn(
                            &s_search_prms_4x8,
                            ps_curr_layer,
                            &ps_ctxt->s_wt_pred,
                            e_me_quality_preset,
                            (ihevce_me_optimised_function_list_t *)
                                ps_ctxt->pv_me_optimised_function_list);

                        if(ME_XTREME_SPEED_25 != e_me_quality_preset)
                        {
                            /* Histogram updates across different Ref ID for global MV */
                            hme_update_histogram(
                                ps_ctxt->aps_mv_hist[i1_ref_idx],
                                aps_best_search_node_8x4[i1_ref_idx]->s_mv.i2_mvx,
                                aps_best_search_node_8x4[i1_ref_idx]->s_mv.i2_mvy);
                            hme_update_histogram(
                                ps_ctxt->aps_mv_hist[i1_ref_idx],
                                aps_best_search_node_4x8[i1_ref_idx]->s_mv.i2_mvx,
                                aps_best_search_node_4x8[i1_ref_idx]->s_mv.i2_mvy);
                        }

                        /* update the best results to the mv bank */
                        hme_update_mv_bank_coarse(
                            ps_search_results,
                            ps_curr_layer->ps_layer_mvbank,
                            blk_x,
                            (blk_y - 1),
                            ps_ctxt->aps_best_search_nodes_4x8_n_rows[i1_ref_idx] +
                                search_node_top_offset, /* Top Candidate */
                            ps_ctxt->aps_best_search_nodes_8x4_n_rows[i1_ref_idx] +
                                search_node_left_offset, /* Left candidate */
                            i1_ref_idx,
                            &s_mv_update_prms);

                        /* Copy the best search result to 5 row array for future use */
                        *(ps_ctxt->aps_best_search_nodes_4x8_n_rows[i1_ref_idx] + blk_x +
                          ps_ctxt->ai4_row_index[blk_y - 1] * num_blks_in_row) =
                            *(aps_best_search_node_4x8[i1_ref_idx]);

                        *(ps_ctxt->aps_best_search_nodes_8x4_n_rows[i1_ref_idx] + blk_x +
                          ps_ctxt->ai4_row_index[blk_y - 1] * num_blks_in_row) =
                            *(aps_best_search_node_8x4[i1_ref_idx]);

                        /* UPDATE the MIN and MAX MVs for Dynamical Search Range for each ref. pic. */
                        /* Only for P pic. For P, both are 0, I&B has them mut. exclusive */
                        if(ps_ctxt->s_frm_prms.is_i_pic == ps_ctxt->s_frm_prms.bidir_enabled)
                        {
                            WORD32 num_mvs, i, j;
                            search_node_t *aps_search_nodes[4];
                            /* Best results for 8x4R and 4x8B blocks */
                            search_node_t *ps_search_node_8x4_r, *ps_search_node_4x8_b;

                            num_mvs = ps_curr_layer->ps_layer_mvbank->i4_num_mvs_per_ref;

                            /*************************************************************************/
                            /* We have atleast 4 distinct results: the 4x8 top (coming from top blk) */
                            /* 8x4 left (coming from left blk), 8x4 and 4x8 right and bot resp.      */
                            /* If number of results to be stored is 4, then we store all these 4     */
                            /* results, else we pick best ones                                       */
                            /*************************************************************************/
                            ps_search_node_8x4_r =
                                ps_search_results->aps_part_results[i1_ref_idx][PART_ID_2NxN_B];
                            ps_search_node_4x8_b =
                                ps_search_results->aps_part_results[i1_ref_idx][PART_ID_Nx2N_R];

                            ASSERT(num_mvs <= 4);

                            /* Doing this to sort best results */
                            aps_search_nodes[0] = ps_search_node_8x4_r;
                            aps_search_nodes[1] = ps_search_node_4x8_b;
                            aps_search_nodes[2] =
                                ps_ctxt->aps_best_search_nodes_8x4_n_rows[i1_ref_idx] +
                                search_node_left_offset; /* Left candidate */
                            aps_search_nodes[3] =
                                ps_ctxt->aps_best_search_nodes_4x8_n_rows[i1_ref_idx] +
                                search_node_top_offset; /* Top Candidate */

                            /* Note : Need to be resolved!!! */
                            /* Added this to match with "hme_update_mv_bank_coarse" */
                            if(num_mvs != 4)
                            {
                                /* Run through the results, store them in best to worst order */
                                for(i = 0; i < num_mvs; i++)
                                {
                                    for(j = i + 1; j < 4; j++)
                                    {
                                        if(aps_search_nodes[j]->i4_tot_cost <
                                           aps_search_nodes[i]->i4_tot_cost)
                                        {
                                            SWAP_HME(
                                                aps_search_nodes[j],
                                                aps_search_nodes[i],
                                                search_node_t *);
                                        }
                                    }
                                }
                            }

                            /* UPDATE the MIN and MAX MVs for Dynamical Search Range for each ref. pic. */
                            for(i = 0; i < num_mvs; i++)
                            {
                                hme_update_dynamic_search_params(
                                    &ps_ctxt->s_coarse_dyn_range_prms
                                         .as_dyn_range_prms[i4_layer_id][i1_ref_idx],
                                    aps_search_nodes[i]->s_mv.i2_mvy);
                            }
                        }
                    }
                }

                /* Update the number of blocks processed in the current row */
                ihevce_dmgr_set_row_row_sync(
                    pv_hme_dep_mngr,
                    (blk_x + 1),
                    blk_y,
                    0 /* Col Tile No. : Not supported in PreEnc*/);
            }

            /* set the output dependency after completion of row */
            ihevce_pre_enc_grp_job_set_out_dep(ps_multi_thrd_ctxt, ps_job, i4_ping_pong);
        }
    }

    return;
}