aboutsummaryrefslogtreecommitdiff
path: root/encoder/hme_search_algo.c
blob: 82778f094069c2f7e2bc717b1099057a651c2641 (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
/******************************************************************************
 *
 * 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_search_algo.c
*
* @brief
*    Contains various search algorithms to be used by coarse/refinement layers
*
* @author
*    Ittiam
*
*
* List of Functions
* hme_compute_grid_results_step_gt_1()
* hme_compute_grid_results_step_1()
* hme_pred_search_square_stepn()
*
******************************************************************************
*/

/*****************************************************************************/
/* 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                                                      */
/*****************************************************************************/

/**
********************************************************************************
*  @fn     void hme_compute_grid_results_step_1(err_prms_t *ps_err_prms,
result_upd_prms_t *ps_result_prms,
BLK_SIZE_T e_blk_size)
*
*  @brief  Updates results for a grid of step = 1
*
*  @param[in] ps_err_prms: Various parameters to this function
*
*  @param[in] ps_result_prms : Parameters pertaining to result updation
*
*  @param[out] e_blk_size: Block size of the blk being searched for
*
*  @return none
********************************************************************************
*/
void hme_compute_grid_results(
    err_prms_t *ps_err_prms, result_upd_prms_t *ps_result_prms, BLK_SIZE_T e_blk_size)
{
    PF_RESULT_FXN_T pf_hme_result_fxn;
    PF_SAD_FXN_T pf_sad_fxn;
    S32 i4_num_results;
    S32 part_id;

    part_id = ps_result_prms->pi4_valid_part_ids[0];

    i4_num_results = (S32)ps_result_prms->ps_search_results->u1_num_results_per_part;

    pf_sad_fxn = hme_get_sad_fxn(e_blk_size, ps_err_prms->i4_grid_mask, ps_err_prms->i4_part_mask);

    pf_hme_result_fxn =
        hme_get_result_fxn(ps_err_prms->i4_grid_mask, ps_err_prms->i4_part_mask, i4_num_results);

    pf_sad_fxn(ps_err_prms);
    pf_hme_result_fxn(ps_result_prms);
}

/**
********************************************************************************
*  @fn     void hme_pred_search_square_stepn(hme_search_prms_t *ps_search_prms,
*                                   layer_ctxt_t *ps_layer_ctxt)
*
*  @brief  Implements predictive search, with square grid refinement. In this
*          case, we start with a bigger step size, like 4, refining upto a
*          variable number of pts, till we hit end of search range or hit a
*          minima. Then we refine using smaller steps. The bigger step size
*          like 4 or 2, do not use optimized SAD functions, they evaluate
*          SAD for each individual pt.
*
*  @param[in,out]  ps_search_prms: All the params to this function
*
*  @param[in] ps_layer_ctxt: Context for the layer
*
*  @return None
********************************************************************************
*/
void hme_pred_search_square_stepn(
    hme_search_prms_t *ps_search_prms,
    layer_ctxt_t *ps_layer_ctxt,
    wgt_pred_ctxt_t *ps_wt_inp_prms,
    ME_QUALITY_PRESETS_T e_me_quality_preset,
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list

)
{
    /* Stores the SAD for all parts at each pt in the grid */
    S32 ai4_sad_grid[9][TOT_NUM_PARTS];

    S32 ai4_valid_part_ids[TOT_NUM_PARTS + 1];

    /* Atributes of input candidates */
    search_candt_t *ps_search_candts;
    search_node_t s_search_node;

    /* Number of candidates to search */
    S32 i4_num_candts, max_num_iters, i4_num_results;

    /* Input and reference attributes */
    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;

    /* Holds the search results at the end of this fxn */
    search_results_t *ps_search_results;

    /* These control number of parts and number of pts in grid to search */
    S32 i4_part_mask, i4_grid_mask;

    /* Blk width, blk height and blk size are derived from input params */
    BLK_SIZE_T e_blk_size;
    CU_SIZE_T e_cu_size;
    S32 i4_blk_wd, i4_blk_ht, i4_step, i4_candt, i4_iter;
    S32 i4_inp_off;
    S32 i4_min_id;
    /* Points to the range limits for mv */
    range_prms_t *ps_range_prms;

    /*************************************************************************/
    /* These functions pointers for calculating Err and the result update    */
    /* Each carries its own parameters structure, which is generated on the  */
    /* fly in this function                                                  */
    /*************************************************************************/
    err_prms_t s_err_prms;
    result_upd_prms_t s_result_prms;

    max_num_iters = ps_search_prms->i4_max_iters;
    /* Using the member 0 to store for all ref. idx., see in coarsest */
    ps_range_prms = ps_search_prms->aps_mv_range[0];
    i4_inp_stride = ps_search_prms->i4_inp_stride;
    /* Move to the location of the search blk in inp buffer */
    i4_inp_off = ps_search_prms->i4_cu_x_off;
    i4_inp_off += (ps_search_prms->i4_cu_y_off * i4_inp_stride);

    ps_search_results = ps_search_prms->ps_search_results;

    /*************************************************************************/
    /* Depending on flag i4_use_rec, we use either input of previously       */
    /* encoded pictures or we use recon of previously encoded pictures.      */
    /*************************************************************************/
    if(ps_search_prms->i4_use_rec == 1)
    {
        i4_ref_stride = ps_layer_ctxt->i4_rec_stride;
        ppu1_ref = ps_layer_ctxt->ppu1_list_rec_fxfy;
    }
    else
    {
        i4_ref_stride = ps_layer_ctxt->i4_inp_stride;
        ppu1_ref = ps_layer_ctxt->ppu1_list_inp;
    }
    i4_ref_offset = (i4_ref_stride * ps_search_prms->i4_y_off) + ps_search_prms->i4_x_off;

    /*************************************************************************/
    /* Obtain the blk size of the search blk. Assumed here that the search   */
    /* is done on a CU size, rather than any arbitrary blk size.             */
    /*************************************************************************/
    ps_search_results = ps_search_prms->ps_search_results;
    e_blk_size = ps_search_prms->e_blk_size;
    i4_blk_wd = (S32)gau1_blk_size_to_wd[e_blk_size];
    i4_blk_ht = (S32)gau1_blk_size_to_ht[e_blk_size];
    e_cu_size = ps_search_results->e_cu_size;
    i4_num_results = (S32)ps_search_results->u1_num_results_per_part;

    ps_search_candts = ps_search_prms->ps_search_candts;
    i4_num_candts = ps_search_prms->i4_num_init_candts;
    i4_part_mask = ps_search_prms->i4_part_mask;

    /*************************************************************************/
    /* This array stores the ids of the partitions whose                     */
    /* SADs are updated. Since the partitions whose SADs are updated may not */
    /* be in contiguous order, we supply another level of indirection.       */
    /*************************************************************************/
    hme_create_valid_part_ids(i4_part_mask, ai4_valid_part_ids);

    /* Update the parameters used to pass to SAD */
    /* input ptr, strides, SAD Grid, part mask, blk width and ht */
    /* The above are fixed ptrs, only pu1_ref and grid mask are  */
    /* varying params which are updated just before calling fxn  */
    s_err_prms.i4_inp_stride = i4_inp_stride;
    s_err_prms.i4_ref_stride = i4_ref_stride;
    s_err_prms.i4_part_mask = i4_part_mask;
    s_err_prms.pi4_sad_grid = &ai4_sad_grid[0][0];
    s_err_prms.i4_blk_wd = i4_blk_wd;
    s_err_prms.i4_blk_ht = i4_blk_ht;
    s_err_prms.pi4_valid_part_ids = ai4_valid_part_ids;

    s_result_prms.pf_mv_cost_compute = ps_search_prms->pf_mv_cost_compute;
    s_result_prms.ps_search_results = ps_search_results;
    s_result_prms.pi4_valid_part_ids = ai4_valid_part_ids;
    s_result_prms.i1_ref_idx = ps_search_prms->i1_ref_idx;
    s_result_prms.i4_part_mask = ps_search_prms->i4_part_mask;
    s_result_prms.ps_search_node_base = &s_search_node;
    s_result_prms.pi4_sad_grid = &ai4_sad_grid[0][0];

    /* Run through each of the candts in a loop */
    for(i4_candt = 0; i4_candt < i4_num_candts; i4_candt++)
    {
        S32 i4_num_refine;

        i4_step = ps_search_prms->i4_start_step;

        s_search_node = *(ps_search_candts->ps_search_node);

        /* initialize minimum cost for this candidate. As we search around */
        /* this candidate, this is used to check early exit, when in any   */
        /* given iteration, the center pt of the grid is lowest value      */
        s_result_prms.i4_min_cost = MAX_32BIT_VAL;

        /* If we need to do refinements, then we need to evaluate */
        /* neighbouring pts. Before doing so, we have to do       */
        /* basic range checks against max allowed mvs             */
        i4_num_refine = ps_search_candts->u1_num_steps_refine;

        CLIP_MV_WITHIN_RANGE(
            s_search_node.s_mv.i2_mvx, s_search_node.s_mv.i2_mvy, ps_range_prms, 0, 0, 0);

        /* The first time, we search all 8 pts around init candt plus the init candt */
        i4_grid_mask = 0x1ff;
        s_err_prms.pu1_inp = ps_wt_inp_prms->apu1_wt_inp[s_search_node.i1_ref_idx] + i4_inp_off;

        for(i4_iter = 0; i4_iter < max_num_iters; i4_iter++)
        {
            i4_grid_mask &= hme_clamp_grid_by_mvrange(&s_search_node, i4_step, ps_range_prms);

            s_err_prms.i4_grid_mask = i4_grid_mask;
            s_err_prms.pu1_ref = ppu1_ref[s_search_node.i1_ref_idx] + i4_ref_offset;
            s_err_prms.pu1_ref +=
                (s_search_node.s_mv.i2_mvx +
                 (s_search_node.s_mv.i2_mvy * s_err_prms.i4_ref_stride));

            s_result_prms.i4_step = i4_step;
            s_err_prms.i4_step = i4_step;
            s_result_prms.i4_grid_mask = i4_grid_mask;

            /* For Top,TopLeft and Left cand., get only center point SAD    */
            /* and do early exit                                            */
            if(0 == i4_num_refine)
            {
                s_err_prms.i4_grid_mask = 0x1;
                s_result_prms.i4_grid_mask = 0x1;

                /* sad pt fun. populates sad to 0th location, whereas update */
                /* fun. takes it based on part. id                           */
                s_err_prms.pi4_sad_grid =
                    s_result_prms.pi4_sad_grid + (1 * s_result_prms.pi4_valid_part_ids[0]);

                ps_me_optimised_function_list->pf_evalsad_pt_npu_mxn_8bit(&s_err_prms);

                s_err_prms.pi4_sad_grid = s_result_prms.pi4_sad_grid;

                if(ME_XTREME_SPEED_25 == e_me_quality_preset)
                    hme_update_results_grid_pu_bestn_xtreme_speed(&s_result_prms);
                else
                    hme_update_results_grid_pu_bestn(&s_result_prms);

                i4_min_id = (S32)PT_C; /* Center Point         */
                i4_step = 0; /* No further refinment */
                s_result_prms.i4_step = i4_step;
                s_err_prms.i4_step = i4_step;
            }
            else
            {
                if(ME_XTREME_SPEED_25 == e_me_quality_preset)
                {
                    err_prms_t *ps_err_prms = &s_err_prms;
                    ASSERT(ps_err_prms->i4_grid_mask != 1);
                    ASSERT((ps_err_prms->i4_part_mask == 4) || (ps_err_prms->i4_part_mask == 16));

                    /*****************************************************************/
                    /* In this case, there are no partial updates. The blk can be    */
                    /* of any type and need not be a CU. The only thing that matters */
                    /* here is the width of the blk, 4/8/(>=16)                      */
                    /*****************************************************************/
                    ps_me_optimised_function_list->pf_evalsad_grid_npu_MxN(&s_err_prms);

                    hme_update_results_grid_pu_bestn_xtreme_speed(&s_result_prms);
                }
                else
                {
                    /* Obtain SAD for all 9 pts in grid*/
                    hme_compute_grid_results(&s_err_prms, &s_result_prms, e_blk_size);
                }

                /* Early exit in case of centre being local minima */
                i4_min_id = s_result_prms.i4_min_id;
            }

            i4_grid_mask = gai4_opt_grid_mask[i4_min_id];

            s_search_node.s_mv.i2_mvx += (i4_step * gai1_grid_id_to_x[i4_min_id]);
            s_search_node.s_mv.i2_mvy += (i4_step * gai1_grid_id_to_y[i4_min_id]);
            if(i4_min_id == (S32)PT_C)
                break;
        }

        /* Next keep reducing stepsize by factor of 2 */
        i4_step >>= 1;
        while(i4_step)
        {
            i4_grid_mask = 0x1fe &
                           hme_clamp_grid_by_mvrange(&s_search_node, i4_step, ps_range_prms);
            //i4_grid_mask &= 0x1fe;

            s_err_prms.i4_grid_mask = i4_grid_mask;
            s_result_prms.i4_grid_mask = i4_grid_mask;
            s_err_prms.i4_step = i4_step;
            s_result_prms.i4_step = i4_step;
            s_err_prms.pu1_ref = ppu1_ref[s_search_node.i1_ref_idx] + i4_ref_offset;
            s_err_prms.pu1_ref +=
                (s_search_node.s_mv.i2_mvx +
                 (s_search_node.s_mv.i2_mvy * s_err_prms.i4_ref_stride));
            if(ME_XTREME_SPEED_25 == e_me_quality_preset)
            {
                err_prms_t *ps_err_prms = &s_err_prms;
                ASSERT(ps_err_prms->i4_grid_mask != 1);
                ASSERT((ps_err_prms->i4_part_mask == 4) || (ps_err_prms->i4_part_mask == 16));

                /*****************************************************************/
                /* In this case, there are no partial updates. The blk can be    */
                /* of any type and need not be a CU. The only thing that matters */
                /* here is the width of the blk, 4/8/(>=16)                      */
                /*****************************************************************/
                ps_me_optimised_function_list->pf_evalsad_grid_npu_MxN(&s_err_prms);

                hme_update_results_grid_pu_bestn_xtreme_speed(&s_result_prms);
            }
            else
            {
                hme_compute_grid_results(&s_err_prms, &s_result_prms, e_blk_size);
            }

            i4_min_id = s_result_prms.i4_min_id;

            s_search_node.s_mv.i2_mvx += (i4_step * gai1_grid_id_to_x[i4_min_id]);
            s_search_node.s_mv.i2_mvy += (i4_step * gai1_grid_id_to_y[i4_min_id]);

            i4_step >>= 1;
        }

        ps_search_candts++;
    }
}

/**
********************************************************************************
*  @fn     hme_pred_search_square_step1(hme_search_prms_t *ps_search_prms,
*                               layer_ctxt_t *ps_layer_ctxt)
*
*  @brief  Implements predictive search with square grid refinement. In this
*           case, the square grid is of step 1 always. since this is considered
*           to be more of a refinement search
*
*  @param[in,out]  ps_search_prms: All the params to this function
*
*  @param[in] ps_layer_ctxt: All info about this layer
*
*  @return None
********************************************************************************
*/
/**
********************************************************************************
*  @fn     hme_pred_search(hme_search_prms_t *ps_search_prms,
*                               layer_ctxt_t *ps_layer_ctxt)
*
*  @brief  Implements predictive search after removing duplicate candidates
*          from initial list. Each square grid (of step 1) is expanded
*          to nine search pts before the dedeuplication process. one point
*          cost is then evaluated for each unique node after the deduplication
*          process
*
*  @param[in,out]  ps_search_prms: All the params to this function
*
*  @param[in] ps_layer_ctxt: All info about this layer
*
*  @return None
********************************************************************************
*/
void hme_pred_search(
    hme_search_prms_t *ps_search_prms,
    layer_ctxt_t *ps_layer_ctxt,
    wgt_pred_ctxt_t *ps_wt_inp_prms,
    S08 i1_grid_flag,
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list

)
{
    /* Stores the SAD for all parts at each pt in the grid */
    S32 ai4_sad_grid[9 * TOT_NUM_PARTS];

    /* Atributes of input candidates */
    search_node_t *ps_search_node;

    search_results_t *ps_search_results;
    S32 i4_num_nodes, i4_candt;

    /* Input and reference attributes */
    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;

    /* These control number of parts and number of pts in grid to search */
    S32 i4_part_mask, i4_grid_mask;

    S32 shift_for_cu_size;

    /* Blk width, blk height and blk size are derived from input params */
    BLK_SIZE_T e_blk_size;
    CU_SIZE_T e_cu_size;
    S32 i4_blk_wd, i4_blk_ht;

    /*************************************************************************/
    /* These functions pointers for calculating Err and the result update    */
    /* Each carries its own parameters structure, which is generated on the  */
    /* fly in this function                                                  */
    /*************************************************************************/
    PF_RESULT_FXN_T pf_hme_result_fxn;
    PF_SAD_FXN_T pf_sad_fxn;
    PF_CALC_SAD_AND_RESULT pf_calc_sad_and_result;
    err_prms_t s_err_prms;
    result_upd_prms_t s_result_prms;
    S32 i4_num_results;
    S32 i4_inp_off;
    fullpel_refine_ctxt_t *ps_fullpel_refine_ctxt = ps_search_prms->ps_fullpel_refine_ctxt;

    i4_inp_stride = ps_search_prms->i4_inp_stride;

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

    /*************************************************************************/
    /* Depending on flag i4_use_rec, we use either input of previously       */
    /* encoded pictures or we use recon of previously encoded pictures.      */
    /*************************************************************************/
    if(ps_search_prms->i4_use_rec == 1)
    {
        i4_ref_stride = ps_layer_ctxt->i4_rec_stride;
        ppu1_ref = ps_layer_ctxt->ppu1_list_rec_fxfy;
    }
    else
    {
        i4_ref_stride = ps_layer_ctxt->i4_rec_stride;
        ppu1_ref = ps_layer_ctxt->ppu1_list_inp;
    }
    i4_ref_offset = (i4_ref_stride * ps_search_prms->i4_y_off) + ps_search_prms->i4_x_off;
    /* Obtain the blk size of the search blk. Assumed here that the search   */
    /* is done on a CU size, rather than any arbitrary blk size.             */
    ps_search_results = ps_search_prms->ps_search_results;
    e_blk_size = ps_search_prms->e_blk_size;
    i4_blk_wd = gau1_blk_size_to_wd[e_blk_size];
    i4_blk_ht = gau1_blk_size_to_ht[e_blk_size];
    e_cu_size = ps_search_results->e_cu_size;

    /* Assuming cu size of 8x8 as enum 0, the other will be 1, 2, 3 */
    /* This will also set the shift w.r.t. the base cu size of 8x8 */
    shift_for_cu_size = e_cu_size;

    ps_search_node = ps_search_prms->ps_search_nodes;
    i4_num_nodes = ps_search_prms->i4_num_search_nodes;
    i4_part_mask = ps_search_prms->i4_part_mask;

    /* Update the parameters used to pass to SAD */
    /* input ptr, strides, SAD Grid, part mask, blk width and ht */
    /* The above are fixed ptrs, only pu1_ref and grid mask are  */
    /* varying params which are updated just before calling fxn  */
    s_err_prms.i4_inp_stride = i4_inp_stride;
    s_err_prms.i4_ref_stride = i4_ref_stride;
    s_err_prms.i4_part_mask = i4_part_mask;
    s_err_prms.pi4_sad_grid = &ai4_sad_grid[0];
    s_err_prms.i4_blk_wd = i4_blk_wd;
    s_err_prms.i4_blk_ht = i4_blk_ht;
    s_err_prms.i4_step = 1;
    s_err_prms.i4_num_partitions = ps_fullpel_refine_ctxt->i4_num_valid_parts;

    s_result_prms.pf_mv_cost_compute = ps_search_prms->pf_mv_cost_compute;
    s_result_prms.ps_search_results = ps_search_results;
    s_result_prms.i1_ref_idx = (S08)ps_search_prms->i1_ref_idx;
    s_result_prms.pi4_sad_grid = ai4_sad_grid;
    s_result_prms.i4_part_mask = i4_part_mask;
    s_result_prms.i4_step = 1;
    pf_calc_sad_and_result = hme_get_calc_sad_and_result_fxn(
        i1_grid_flag,
        ps_search_prms->u1_is_cu_noisy,
        i4_part_mask,
        ps_fullpel_refine_ctxt->i4_num_valid_parts,
        ps_search_results->u1_num_results_per_part);

    pf_calc_sad_and_result(
        ps_search_prms, ps_wt_inp_prms, &s_err_prms, &s_result_prms, ppu1_ref, i4_ref_stride);
}

static __inline FT_CALC_SAD_AND_RESULT *hme_get_calc_sad_and_result_explicit_fxn(
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list,
    S32 i4_part_mask,
    S32 i4_num_partitions,
    S08 i1_grid_enable,
    U08 u1_num_results_per_part)
{
    FT_CALC_SAD_AND_RESULT *pf_func = NULL;

    if(2 == u1_num_results_per_part)
    {
        if(i4_part_mask == 1)
        {
            ASSERT(i4_num_partitions == 1);

            if(i1_grid_enable == 0)
            {
                pf_func =
                    ps_me_optimised_function_list->pf_calc_pt_sad_and_2_best_results_explicit_8x8;
            }
            else
            {
                pf_func = ps_me_optimised_function_list
                              ->pf_calc_pt_sad_and_2_best_results_explicit_8x8_for_grid;
            }
        }
        else
        {
            ASSERT(i4_num_partitions == 5);

            pf_func =
                ps_me_optimised_function_list->pf_calc_pt_sad_and_2_best_results_explicit_8x8_4x4;
        }
    }
    else if(1 == u1_num_results_per_part)
    {
        if(i4_part_mask == 1)
        {
            ASSERT(i4_num_partitions == 1);

            if(i1_grid_enable == 0)
            {
                pf_func =
                    ps_me_optimised_function_list->pf_calc_pt_sad_and_1_best_result_explicit_8x8;
            }
            else
            {
                pf_func = ps_me_optimised_function_list
                              ->pf_calc_pt_sad_and_1_best_result_explicit_8x8_for_grid;
            }
        }
        else
        {
            ASSERT(i4_num_partitions == 5);

            pf_func =
                ps_me_optimised_function_list->pf_calc_pt_sad_and_1_best_result_explicit_8x8_4x4;
        }
    }

    return pf_func;
}

/**
********************************************************************************
*  @fn     void hme_pred_search_no_encode(hme_search_prms_t *ps_search_prms,
*                                         layer_ctxt_t *ps_layer_ctxt,
*                                         wgt_pred_ctxt_t *ps_wt_inp_prms,
*                                         S32 *pi4_valid_part_ids,
*                                         S32 disable_refine,
*                                         ME_QUALITY_PRESETS_T e_me_quality_preset)
*
*  @brief  Implements predictive search after removing duplicate candidates
*          from initial list. Each square grid (of step 1) is expanded
*          to nine search pts before the dedeuplication process. one point
*          cost is then evaluated for each unique node after the deduplication
*          process
*
*  @param[in,out]  ps_search_prms: All the params to this function
*
*  @param[in] ps_layer_ctxt: All info about this layer
*
*  @return None
********************************************************************************
*/
void hme_pred_search_no_encode(
    hme_search_prms_t *ps_search_prms,
    layer_ctxt_t *ps_layer_ctxt,
    wgt_pred_ctxt_t *ps_wt_inp_prms,
    S32 *pi4_valid_part_ids,
    S32 disable_refine,
    ME_QUALITY_PRESETS_T e_me_quality_preset,
    S08 i1_grid_enable,
    ihevce_me_optimised_function_list_t *ps_me_optimised_function_list)
{
    /* Stores the SAD for all parts at each pt in the grid */
    S32 ai4_sad_grid[9 * TOT_NUM_PARTS];

    /* Atributes of input candidates */
    search_node_t *ps_search_node;
    search_results_t *ps_search_results;
    S32 i4_num_nodes;

    /* Input and reference attributes */
    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;

    /* These control number of parts and number of pts in grid to search */
    S32 i4_part_mask;  // i4_grid_mask;

    S32 shift_for_cu_size;
    /* Blk width, blk height and blk size are derived from input params */
    BLK_SIZE_T e_blk_size;
    CU_SIZE_T e_cu_size;
    S32 i4_blk_wd, i4_blk_ht;

    /*************************************************************************/
    /* These functions pointers for calculating Err and the result update    */
    /* Each carries its own parameters structure, which is generated on the  */
    /* fly in this function                                                  */
    /*************************************************************************/
    PF_CALC_SAD_AND_RESULT pf_calc_sad_and_result;
    err_prms_t s_err_prms;
    result_upd_prms_t s_result_prms;
    S32 i4_num_results;
    S32 i4_search_idx = ps_search_prms->i1_ref_idx;
    S32 i4_inp_off;
    S32 i4_num_partitions;

    i4_inp_stride = ps_search_prms->i4_inp_stride;

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

    /*************************************************************************/
    /* Depending on flag i4_use_rec, we use either input of previously       */
    /* encoded pictures or we use recon of previously encoded pictures.      */
    /*************************************************************************/
    if(ps_search_prms->i4_use_rec == 1)
    {
        i4_ref_stride = ps_layer_ctxt->i4_rec_stride;
        ppu1_ref = ps_layer_ctxt->ppu1_list_rec_fxfy;
    }
    else
    {
        i4_ref_stride = ps_layer_ctxt->i4_inp_stride;
        ppu1_ref = ps_layer_ctxt->ppu1_list_inp;
    }
    i4_ref_offset = (i4_ref_stride * ps_search_prms->i4_y_off) + ps_search_prms->i4_x_off;
    /* Obtain the blk size of the search blk. Assumed here that the search   */
    /* is done on a CU size, rather than any arbitrary blk size.             */
    ps_search_results = ps_search_prms->ps_search_results;
    e_blk_size = ps_search_prms->e_blk_size;
    i4_blk_wd = gau1_blk_size_to_wd[e_blk_size];
    i4_blk_ht = gau1_blk_size_to_ht[e_blk_size];
    e_cu_size = ps_search_results->e_cu_size;

    /* Assuming cu size of 8x8 as enum 0, the other will be 1, 2, 3 */
    /* This will also set the shift w.r.t. the base cu size of 8x8 */
    shift_for_cu_size = e_cu_size;

    ps_search_node = ps_search_prms->ps_search_nodes;
    i4_num_nodes = ps_search_prms->i4_num_search_nodes;
    i4_part_mask = ps_search_prms->i4_part_mask;

    /*************************************************************************/
    /* This array stores the ids of the partitions whose                     */
    /* SADs are updated. Since the partitions whose SADs are updated may not */
    /* be in contiguous order, we supply another level of indirection.       */
    /*************************************************************************/
    i4_num_partitions = hme_create_valid_part_ids(i4_part_mask, pi4_valid_part_ids);

    /* Update the parameters used to pass to SAD */
    /* input ptr, strides, SAD Grid, part mask, blk width and ht */
    /* The above are fixed ptrs, only pu1_ref and grid mask are  */
    /* varying params which are updated just before calling fxn  */
    s_err_prms.i4_inp_stride = i4_inp_stride;
    s_err_prms.i4_ref_stride = i4_ref_stride;
    s_err_prms.i4_part_mask = i4_part_mask;
    s_err_prms.pi4_sad_grid = &ai4_sad_grid[0];
    s_err_prms.i4_blk_wd = i4_blk_wd;
    s_err_prms.i4_blk_ht = i4_blk_ht;
    s_err_prms.i4_step = 1;
    s_err_prms.pi4_valid_part_ids = pi4_valid_part_ids;
    s_err_prms.i4_num_partitions = i4_num_partitions;

    s_result_prms.pf_mv_cost_compute = ps_search_prms->pf_mv_cost_compute;
    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 = (S08)ps_search_prms->i1_ref_idx;
    s_result_prms.pi4_sad_grid = ai4_sad_grid;
    s_result_prms.i4_part_mask = i4_part_mask;
    s_result_prms.i4_step = 1;

    pf_calc_sad_and_result = hme_get_calc_sad_and_result_explicit_fxn(
        ps_me_optimised_function_list,
        i4_part_mask,
        i4_num_partitions,
        i1_grid_enable,
        ps_search_results->u1_num_results_per_part);

    pf_calc_sad_and_result(
        ps_search_prms, ps_wt_inp_prms, &s_err_prms, &s_result_prms, ppu1_ref, i4_ref_stride);
}