Home | History | Annotate | Download | only in encoder
      1 /******************************************************************************
      2  *
      3  * Copyright (C) 2018 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 /**
     21  *******************************************************************************
     22  * @file
     23  *  ihevcd_mv_pred.c
     24  *
     25  * @brief
     26  *  Contains functions for motion vector prediction
     27  *
     28  * @author
     29  *  Ittiam
     30  *
     31  * @par List of Functions:
     32  * - ihevcd_mvp_spatial_cand()
     33  *
     34  * @remarks
     35  *  None
     36  *
     37  *******************************************************************************
     38  */
     39 /*****************************************************************************/
     40 /* File Includes                                                             */
     41 /*****************************************************************************/
     42 /* System include files */
     43 #include <stdio.h>
     44 #include <string.h>
     45 #include <stdlib.h>
     46 #include <assert.h>
     47 #include <stdarg.h>
     48 #include <math.h>
     49 
     50 /* User include files */
     51 #include "ihevc_typedefs.h"
     52 #include "itt_video_api.h"
     53 #include "ihevce_api.h"
     54 
     55 #include "rc_cntrl_param.h"
     56 #include "rc_frame_info_collector.h"
     57 #include "rc_look_ahead_params.h"
     58 
     59 #include "ihevc_defs.h"
     60 #include "ihevc_macros.h"
     61 #include "ihevc_debug.h"
     62 #include "ihevc_structs.h"
     63 #include "ihevc_platform_macros.h"
     64 #include "ihevc_deblk.h"
     65 #include "ihevc_itrans_recon.h"
     66 #include "ihevc_chroma_itrans_recon.h"
     67 #include "ihevc_chroma_intra_pred.h"
     68 #include "ihevc_intra_pred.h"
     69 #include "ihevc_inter_pred.h"
     70 #include "ihevc_mem_fns.h"
     71 #include "ihevc_padding.h"
     72 #include "ihevc_weighted_pred.h"
     73 #include "ihevc_sao.h"
     74 #include "ihevc_resi_trans.h"
     75 #include "ihevc_quant_iquant_ssd.h"
     76 #include "ihevc_cabac_tables.h"
     77 #include "ihevc_common_tables.h"
     78 
     79 #include "ihevce_defs.h"
     80 #include "ihevce_hle_interface.h"
     81 #include "ihevce_lap_enc_structs.h"
     82 #include "ihevce_multi_thrd_structs.h"
     83 #include "ihevce_me_common_defs.h"
     84 #include "ihevce_had_satd.h"
     85 #include "ihevce_error_codes.h"
     86 #include "ihevce_bitstream.h"
     87 #include "ihevce_cabac.h"
     88 #include "ihevce_rdoq_macros.h"
     89 #include "ihevce_function_selector.h"
     90 #include "ihevce_enc_structs.h"
     91 #include "ihevce_entropy_structs.h"
     92 #include "ihevce_cmn_utils_instr_set_router.h"
     93 #include "ihevce_enc_loop_structs.h"
     94 #include "hme_datatype.h"
     95 #include "hme_interface.h"
     96 #include "hme_common_defs.h"
     97 #include "hme_defs.h"
     98 #include "ihevce_mv_pred.h"
     99 #include "ihevce_mv_pred_merge.h"
    100 #include "ihevce_common_utils.h"
    101 
    102 /*****************************************************************************/
    103 /* Function Definitions                                                      */
    104 /*****************************************************************************/
    105 
    106 /**
    107  *******************************************************************************
    108  *
    109  * @brief
    110  * This function performs Motion Vector prediction and return a list of mv
    111  *
    112  * @par Description:
    113  *  MV predictor list is computed using neighbor mvs and colocated mv
    114  *
    115  * @param[in] ps_ctxt
    116  * pointer to mv predictor context
    117  *
    118  * @param[in] ps_top_nbr_4x4
    119  * pointer to top 4x4 nbr structure
    120  *
    121  * @param[in] ps_left_nbr_4x4
    122  * pointer to left 4x4 nbr structure
    123  *
    124  * @param[in] ps_top_left_nbr_4x4
    125  * pointer to top left 4x4 nbr structure
    126  *
    127  * @param[in] left_nbr_4x4_strd
    128  * left nbr buffer stride in terms of 4x4 units
    129  *
    130  * @param[in] ps_avail_flags
    131  * Neighbor availability flags container
    132  *
    133  * @param[in] ps_col_mv
    134  * Colocated MV pointer
    135  *
    136  * @param[in] ps_pu
    137  * Current Partition PU strucrture pointer
    138  *
    139  * @param[inout] ps_pred_mv
    140  * pointer to store predicted MV list
    141  *
    142  * @returns
    143  * None
    144  * @remarks
    145  *
    146  *
    147  *******************************************************************************
    148  */
    149 void ihevce_mv_pred(
    150     mv_pred_ctxt_t *ps_ctxt,
    151     nbr_4x4_t *ps_top_nbr_4x4,
    152     nbr_4x4_t *ps_left_nbr_4x4,
    153     nbr_4x4_t *ps_top_left_nbr_4x4,
    154     WORD32 left_nbr_4x4_strd,
    155     nbr_avail_flags_t *ps_avail_flags,
    156     pu_mv_t *ps_col_mv,
    157     pu_t *ps_pu,
    158     pu_mv_t *ps_pred_mv,
    159     UWORD8 (*pau1_is_top_used)[MAX_MVP_LIST_CAND])
    160 {
    161     WORD32 is_scaled_flag_list[2] /* Indicates whether A0 or A1 is available */;
    162     WORD32 lb_avail, l_avail, t_avail, tr_avail, tl_avail;
    163     WORD32 avail_a_flag[2];
    164     WORD32 avail_b_flag[2];
    165     mv_t as_mv_a[2];
    166     mv_t as_mv_b[2];
    167     UWORD8 i1_cur_ref_idx_list[2];
    168     WORD32 part_pos_x;
    169     WORD32 part_pos_y;
    170     WORD32 part_wd;
    171     WORD32 part_ht;
    172 
    173     /*******************************************/
    174     /* Neighbor location: Graphical indication */
    175     /*                                         */
    176     /*          B2 _____________B1 B0          */
    177     /*            |               |            */
    178     /*            |               |            */
    179     /*            |               |            */
    180     /*            |      PU     ht|            */
    181     /*            |               |            */
    182     /*            |               |            */
    183     /*          A1|______wd_______|            */
    184     /*          A0                             */
    185     /*                                         */
    186     /*******************************************/
    187 
    188     /* Initialization */
    189     avail_a_flag[0] = 0;
    190     avail_a_flag[1] = 0;
    191     avail_b_flag[0] = 0;
    192     avail_b_flag[1] = 0;
    193 
    194     as_mv_a[0].i2_mvx = 0;
    195     as_mv_a[0].i2_mvy = 0;
    196     as_mv_a[1].i2_mvx = 0;
    197     as_mv_a[1].i2_mvy = 0;
    198     as_mv_b[0].i2_mvx = 0;
    199     as_mv_b[0].i2_mvy = 0;
    200     as_mv_b[1].i2_mvx = 0;
    201     as_mv_b[1].i2_mvy = 0;
    202 
    203     lb_avail = ps_avail_flags->u1_bot_lt_avail;
    204     l_avail = ps_avail_flags->u1_left_avail;
    205     tr_avail = ps_avail_flags->u1_top_rt_avail;
    206     t_avail = ps_avail_flags->u1_top_avail;
    207     tl_avail = ps_avail_flags->u1_top_lt_avail;
    208 
    209     is_scaled_flag_list[0] = 0;
    210     is_scaled_flag_list[1] = 0;
    211 
    212     part_pos_x = ps_pu->b4_pos_x << 2;
    213     part_pos_y = ps_pu->b4_pos_y << 2;
    214     part_wd = (ps_pu->b4_wd + 1) << 2;
    215     part_ht = (ps_pu->b4_ht + 1) << 2;
    216 
    217     /* Initializing current PU reference index     */
    218     /* if -1 is set then that direction is invalid */
    219     i1_cur_ref_idx_list[0] = (-1 == ps_pu->mv.i1_l0_ref_idx) ? 0 : ps_pu->mv.i1_l0_ref_idx;
    220     i1_cur_ref_idx_list[1] = (-1 == ps_pu->mv.i1_l1_ref_idx) ? 0 : ps_pu->mv.i1_l1_ref_idx;
    221 
    222     /************************************************************/
    223     /* Calculating of motion vector A from neighbors A0 and A1  */
    224     /************************************************************/
    225     {
    226         WORD32 l_x, a;
    227         WORD32 *pi4_avail_flag;
    228         WORD32 nbr_avail[2]; /*[A0/A1] */
    229         WORD8 i1_nbr_ref_idx_list[2][2]; /* [A0/A1][L0/L1] */
    230         UWORD8 u1_nbr_intra_flag[2]; /*[A0/A1] */
    231         UWORD8 u1_nbr_pred_flag[2][2]; /* [A0/A1][L0/L1] */
    232         mv_t *ps_mv;
    233         nbr_4x4_t *ps_a0, *ps_a1;
    234         mv_t *ps_nbr_mv[2][2]; /* [A0/A1][L0/L1] */
    235 
    236         /* A0 and A1 initializations */
    237         ps_mv = &as_mv_a[0];
    238         pi4_avail_flag = avail_a_flag;
    239 
    240         /* Pointers to A0 and A1 */
    241         {
    242             WORD32 y_a0, y_a1;
    243             /* TODO: y_a0, y_a1 is coded assuming left nbr pointer starts at PU */
    244             y_a0 = (part_ht >> 2);
    245             y_a1 = ((part_ht - 1) >> 2);
    246 
    247             ps_a0 = ps_left_nbr_4x4 + (y_a0 * left_nbr_4x4_strd);
    248             ps_a1 = ps_left_nbr_4x4 + (y_a1 * left_nbr_4x4_strd);
    249         }
    250 
    251         nbr_avail[0] = lb_avail && (!ps_a0->b1_intra_flag);
    252         nbr_avail[1] = l_avail && (!ps_a1->b1_intra_flag);
    253 
    254         /* Setting is scaled flag based on availability of A0 and A1 */
    255         if((nbr_avail[0] == 1) || (nbr_avail[1]))
    256         {
    257             is_scaled_flag_list[0] = 1;
    258             is_scaled_flag_list[1] = 1;
    259         }
    260 
    261         /* Initializing A0 variables */
    262         ps_nbr_mv[0][0] = &ps_a0->mv.s_l0_mv;
    263         ps_nbr_mv[0][1] = &ps_a0->mv.s_l1_mv;
    264 
    265         i1_nbr_ref_idx_list[0][0] = ps_a0->mv.i1_l0_ref_idx;
    266         i1_nbr_ref_idx_list[0][1] = ps_a0->mv.i1_l1_ref_idx;
    267 
    268         u1_nbr_pred_flag[0][0] = (UWORD8)ps_a0->b1_pred_l0_flag;
    269         u1_nbr_pred_flag[0][1] = (UWORD8)ps_a0->b1_pred_l1_flag;
    270 
    271         u1_nbr_intra_flag[0] = (UWORD8)ps_a0->b1_intra_flag;
    272 
    273         /* Initializing A1 variables */
    274         ps_nbr_mv[1][0] = &ps_a1->mv.s_l0_mv;
    275         ps_nbr_mv[1][1] = &ps_a1->mv.s_l1_mv;
    276 
    277         i1_nbr_ref_idx_list[1][0] = ps_a1->mv.i1_l0_ref_idx;
    278         i1_nbr_ref_idx_list[1][1] = ps_a1->mv.i1_l1_ref_idx;
    279 
    280         u1_nbr_pred_flag[1][0] = (UWORD8)ps_a1->b1_pred_l0_flag;
    281         u1_nbr_pred_flag[1][1] = (UWORD8)ps_a1->b1_pred_l1_flag;
    282 
    283         u1_nbr_intra_flag[1] = (UWORD8)ps_a1->b1_intra_flag;
    284 
    285         /* Derivation of mvL0A and mvL1A from A0 and A1 */
    286         for(l_x = 0; l_x < 2; l_x++) /* list 0 and list 1 */
    287         {
    288             WORD32 l_y;
    289 
    290             l_y = !l_x; /* if i=0, y = L1 else y = L0 */
    291 
    292             for(a = 0; a < 2; a++)
    293             {
    294                 /* MODE_INTRA check has been taken care in availability check */
    295                 if((nbr_avail[a] == 1) && (pi4_avail_flag[l_x] == 0))
    296                 {
    297                     if(u1_nbr_pred_flag[a][l_x] == 1)
    298                     {
    299                         WORD32 nbr_ref_poc, cur_ref_poc;
    300                         WORD8 i1_cur_ref_idx, i1_nbr_ref_idx;
    301 
    302                         i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
    303                         cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
    304                         i1_nbr_ref_idx = i1_nbr_ref_idx_list[a][l_x];
    305                         nbr_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_nbr_ref_idx]->i4_poc;
    306 
    307                         if(nbr_ref_poc == cur_ref_poc)
    308                         {
    309                             pi4_avail_flag[l_x] = 1;
    310                             ps_mv[l_x] = *ps_nbr_mv[a][l_x];
    311                             break;
    312                         }
    313                     }
    314                     if(u1_nbr_pred_flag[a][l_y] == 1)
    315                     {
    316                         WORD32 nbr_ref_poc, cur_ref_poc;
    317                         WORD8 i1_nbr_ref_idx, i1_cur_ref_idx;
    318 
    319                         i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
    320                         cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
    321 
    322                         i1_nbr_ref_idx = i1_nbr_ref_idx_list[a][l_y];
    323                         nbr_ref_poc = ps_ctxt->ps_ref_list[l_y][i1_nbr_ref_idx]->i4_poc;
    324                         if(nbr_ref_poc == cur_ref_poc)
    325                         {
    326                             pi4_avail_flag[l_x] = 1;
    327                             ps_mv[l_x] = *ps_nbr_mv[a][l_y];
    328                             break;
    329                         }
    330                     }
    331                 }
    332             }
    333         }
    334 
    335         for(l_x = 0; l_x < 2; l_x++) /* list 0 and list 1 */
    336         {
    337             if(pi4_avail_flag[l_x] == 0)
    338             {
    339                 WORD8 i1_nbr_ref_list_idx, i1_nbr_ref_idx;
    340                 WORD32 l_y;
    341 
    342                 l_y = !l_x; /* if i=0, y = L1 else y = L0 */
    343 
    344                 for(a = 0; a < 2; a++)
    345                 {
    346                     /* MODE_INTRA check has been taken care in availability check */
    347                     if((nbr_avail[a] == 1) && (pi4_avail_flag[l_x] == 0))
    348                     {
    349                         /* Long term reference check Removed */
    350                         if(u1_nbr_pred_flag[a][l_x] == 1)
    351                         {
    352                             pi4_avail_flag[l_x] = 1;
    353                             ps_mv[l_x] = *ps_nbr_mv[a][l_x];
    354                             i1_nbr_ref_idx = i1_nbr_ref_idx_list[a][l_x];
    355                             i1_nbr_ref_list_idx = l_x;
    356                             break;
    357                         }
    358                         /* Long term reference check Removed */
    359                         else if(u1_nbr_pred_flag[a][l_y] == 1)
    360                         {
    361                             pi4_avail_flag[l_x] = 1;
    362                             ps_mv[l_x] = *ps_nbr_mv[a][l_y];
    363                             i1_nbr_ref_idx = i1_nbr_ref_idx_list[a][l_y];
    364                             i1_nbr_ref_list_idx = l_y;
    365                             break;
    366                         }
    367                     }
    368                 }
    369 
    370                 /* Long term reference check Removed */
    371                 if(pi4_avail_flag[l_x] == 1)
    372                 {
    373                     WORD8 i1_cur_ref_idx;
    374                     WORD32 cur_ref_poc, nbr_ref_poc;
    375                     WORD32 cur_poc;
    376 
    377                     i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
    378                     cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
    379 
    380                     nbr_ref_poc = ps_ctxt->ps_ref_list[i1_nbr_ref_list_idx][i1_nbr_ref_idx]->i4_poc;
    381 
    382                     cur_poc = ps_ctxt->ps_slice_hdr->i4_abs_pic_order_cnt;
    383 
    384                     ihevce_scale_mv(&ps_mv[l_x], cur_ref_poc, nbr_ref_poc, cur_poc);
    385                 }
    386             }
    387         }
    388     }
    389 
    390     /************************************************************/
    391     /* Calculating of motion vector B from neighbors B0 and B1  */
    392     /************************************************************/
    393     {
    394         WORD32 l_x, b;
    395         WORD32 *pi4_avail_flag;
    396         WORD32 nbr_avail[3]; /* [B0/B1/B2] */
    397         WORD8 i1_nbr_ref_idx_list[3][2]; /* [B0/B1/B2][L0/L1] */
    398         UWORD8 u1_nbr_intra_flag[3]; /*[B0/B1/B2] */
    399         UWORD8 u1_nbr_pred_flag[3][2]; /* [B0/B1/B2][L0/L1] */
    400         mv_t *ps_mv;
    401         nbr_4x4_t *ps_b0, *ps_b1, *ps_b2;
    402         mv_t *ps_nbr_mv[3][2]; /* [B0/B1/B2][L0/L1] */
    403 
    404         /* B0, B1 and B2 initializations */
    405         ps_mv = &as_mv_b[0];
    406         pi4_avail_flag = avail_b_flag;
    407 
    408         /* Pointers to B0, B1 and B2 */
    409         {
    410             WORD32 x_b0, x_b1, x_b2;
    411 
    412             /* Relative co-ordiante of Xp,Yp w.r.t CTB start will work */
    413             /* as long as minCTB = 16                                  */
    414             x_b0 = (part_pos_x + part_wd);
    415             x_b1 = (part_pos_x + part_wd - 1);
    416             x_b2 = (part_pos_x - 1);
    417 
    418             /* Getting offset back to given pointer */
    419             x_b0 = x_b0 - part_pos_x;
    420             x_b1 = x_b1 - part_pos_x;
    421             x_b2 = x_b2 - part_pos_x;
    422 
    423             /* Below derivation are based on top pointer */
    424             /* is pointing first pixel of PU             */
    425             ps_b0 = ps_top_nbr_4x4 + (x_b0 >> 2);
    426             ps_b1 = ps_top_nbr_4x4 + (x_b1 >> 2);
    427 
    428             /* At CTB boundary, use top-left passed in */
    429             if(part_pos_y)
    430             {
    431                 ps_b2 = ps_top_left_nbr_4x4;
    432             }
    433             else
    434             {
    435                 /* Not at CTB boundary, use top and  */
    436                 /* add correction to go to top-left */
    437                 ps_b2 = (ps_top_nbr_4x4) + (x_b2 >> 2);
    438             }
    439         }
    440         nbr_avail[0] = tr_avail && (!ps_b0->b1_intra_flag);
    441         nbr_avail[1] = t_avail && (!ps_b1->b1_intra_flag);
    442         nbr_avail[2] = tl_avail && (!ps_b2->b1_intra_flag);
    443 
    444         /* Initializing B0 related variables */
    445         ps_nbr_mv[0][0] = &ps_b0->mv.s_l0_mv;
    446         ps_nbr_mv[0][1] = &ps_b0->mv.s_l1_mv;
    447 
    448         i1_nbr_ref_idx_list[0][0] = ps_b0->mv.i1_l0_ref_idx;
    449         i1_nbr_ref_idx_list[0][1] = ps_b0->mv.i1_l1_ref_idx;
    450 
    451         u1_nbr_pred_flag[0][0] = (UWORD8)ps_b0->b1_pred_l0_flag;
    452         u1_nbr_pred_flag[0][1] = (UWORD8)ps_b0->b1_pred_l1_flag;
    453 
    454         u1_nbr_intra_flag[0] = (UWORD8)ps_b0->b1_intra_flag;
    455 
    456         /* Initializing B1 related variables */
    457         ps_nbr_mv[1][0] = &ps_b1->mv.s_l0_mv;
    458         ps_nbr_mv[1][1] = &ps_b1->mv.s_l1_mv;
    459 
    460         i1_nbr_ref_idx_list[1][0] = ps_b1->mv.i1_l0_ref_idx;
    461         i1_nbr_ref_idx_list[1][1] = ps_b1->mv.i1_l1_ref_idx;
    462 
    463         u1_nbr_pred_flag[1][0] = (UWORD8)ps_b1->b1_pred_l0_flag;
    464         u1_nbr_pred_flag[1][1] = (UWORD8)ps_b1->b1_pred_l1_flag;
    465 
    466         u1_nbr_intra_flag[1] = (UWORD8)ps_b1->b1_intra_flag;
    467 
    468         /* Initializing B2 related variables */
    469         ps_nbr_mv[2][0] = &ps_b2->mv.s_l0_mv;
    470         ps_nbr_mv[2][1] = &ps_b2->mv.s_l1_mv;
    471 
    472         i1_nbr_ref_idx_list[2][0] = ps_b2->mv.i1_l0_ref_idx;
    473         i1_nbr_ref_idx_list[2][1] = ps_b2->mv.i1_l1_ref_idx;
    474 
    475         u1_nbr_pred_flag[2][0] = (UWORD8)ps_b2->b1_pred_l0_flag;
    476         u1_nbr_pred_flag[2][1] = (UWORD8)ps_b2->b1_pred_l1_flag;
    477 
    478         u1_nbr_intra_flag[2] = (UWORD8)ps_b2->b1_intra_flag;
    479 
    480         /* Derivation of mvL0B and mvL1B from B0,B1 and B2 */
    481         for(l_x = 0; l_x < 2; l_x++) /* list 0 and list 1 */
    482         {
    483             WORD32 l_y;
    484 
    485             l_y = !l_x; /* if i=0, y = L1 else y = L0 */
    486 
    487             for(b = 0; b < 3; b++)
    488             {
    489                 if((nbr_avail[b] == 1) && (pi4_avail_flag[l_x] == 0))
    490                 {
    491                     if(u1_nbr_pred_flag[b][l_x] == 1)
    492                     {
    493                         WORD32 nbr_ref_poc, cur_ref_poc;
    494                         WORD8 i1_cur_ref_idx, i1_nbr_ref_idx;
    495 
    496                         i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
    497                         cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
    498                         i1_nbr_ref_idx = i1_nbr_ref_idx_list[b][l_x];
    499                         nbr_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_nbr_ref_idx]->i4_poc;
    500 
    501                         if(nbr_ref_poc == cur_ref_poc)
    502                         {
    503                             pi4_avail_flag[l_x] = 1;
    504                             ps_mv[l_x] = *ps_nbr_mv[b][l_x];
    505                             break;
    506                         }
    507                     }
    508                     if(u1_nbr_pred_flag[b][l_y] == 1)
    509                     {
    510                         WORD32 nbr_ref_poc, cur_ref_poc;
    511                         WORD8 i1_nbr_ref_idx, i1_cur_ref_idx;
    512 
    513                         i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
    514                         cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
    515 
    516                         i1_nbr_ref_idx = i1_nbr_ref_idx_list[b][l_y];
    517                         nbr_ref_poc = ps_ctxt->ps_ref_list[l_y][i1_nbr_ref_idx]->i4_poc;
    518 
    519                         if(nbr_ref_poc == cur_ref_poc)
    520                         {
    521                             pi4_avail_flag[l_x] = 1;
    522                             ps_mv[l_x] = *ps_nbr_mv[b][l_y];
    523                             break;
    524                         }
    525                     }
    526                 }
    527             }
    528         }
    529 
    530         if((is_scaled_flag_list[0] == 0) && (avail_b_flag[0] == 1))
    531         {
    532             avail_a_flag[0] = 1;
    533             as_mv_a[0] = as_mv_b[0];
    534         }
    535         if((is_scaled_flag_list[1] == 0) && (avail_b_flag[1] == 1))
    536         {
    537             avail_a_flag[1] = 1;
    538             as_mv_a[1] = as_mv_b[1];
    539         }
    540 
    541         for(l_x = 0; l_x < 2; l_x++) /* list 0 and list 1 */
    542         {
    543             if(is_scaled_flag_list[l_x] == 0)
    544             {
    545                 /* If isScaledFlagLX == 0, availFlagLXB flag is set to 0 */
    546                 pi4_avail_flag[l_x] = 0;
    547                 {
    548                     WORD8 i1_nbr_ref_list_idx, i1_nbr_ref_idx;
    549                     WORD32 l_y;
    550 
    551                     l_y = !l_x; /* if i=0, y = L1 else y = L0 */
    552 
    553                     for(b = 0; b < 3; b++)
    554                     {
    555                         if((nbr_avail[b] == 1) && (pi4_avail_flag[l_x] == 0))
    556                         {
    557                             /* Long term reference check Removed */
    558                             if(u1_nbr_pred_flag[b][l_x] == 1)
    559                             {
    560                                 pi4_avail_flag[l_x] = 1;
    561                                 ps_mv[l_x] = *ps_nbr_mv[b][l_x];
    562                                 i1_nbr_ref_idx = i1_nbr_ref_idx_list[b][l_x];
    563                                 i1_nbr_ref_list_idx = l_x;
    564                                 break;
    565                             }
    566                             /* Long term reference check Removed */
    567                             else if(u1_nbr_pred_flag[b][l_y] == 1)
    568                             {
    569                                 pi4_avail_flag[l_x] = 1;
    570                                 ps_mv[l_x] = *ps_nbr_mv[b][l_y];
    571                                 i1_nbr_ref_idx = i1_nbr_ref_idx_list[b][l_y];
    572                                 i1_nbr_ref_list_idx = l_y;
    573                                 break;
    574                             }
    575                         }
    576                     }
    577                     /* Long term reference check Removed */
    578                     if(pi4_avail_flag[l_x] == 1)
    579                     {
    580                         WORD8 i1_cur_ref_idx;
    581                         WORD32 cur_ref_poc, nbr_ref_poc;
    582                         WORD32 cur_poc;
    583 
    584                         i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
    585                         cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
    586 
    587                         nbr_ref_poc =
    588                             ps_ctxt->ps_ref_list[i1_nbr_ref_list_idx][i1_nbr_ref_idx]->i4_poc;
    589 
    590                         cur_poc = ps_ctxt->ps_slice_hdr->i4_abs_pic_order_cnt;
    591 
    592                         if(cur_ref_poc != nbr_ref_poc)
    593                             ihevce_scale_mv(&ps_mv[l_x], cur_ref_poc, nbr_ref_poc, cur_poc);
    594                     }
    595                 }
    596             }
    597         }
    598     }
    599 
    600     /* Candidate list */
    601     {
    602         mv_t as_mvp_list_l0[MAX_MVP_LIST_CAND_MEM]; /*[Cand0/Cand1/Cand2] */
    603         mv_t as_mvp_list_l1[MAX_MVP_LIST_CAND_MEM]; /*[Cand0/Cand1/Cand2] */
    604         UWORD8 au1_is_top_used_l0[MAX_MVP_LIST_CAND_MEM];
    605         UWORD8 au1_is_top_used_l1[MAX_MVP_LIST_CAND_MEM];
    606         WORD32 num_mvp_cand_l0;
    607         WORD32 num_mvp_cand_l1;
    608 
    609         /* L0 candidate list*/
    610         num_mvp_cand_l0 = 0;
    611 
    612         if(avail_a_flag[0] == 1)
    613         {
    614             as_mvp_list_l0[num_mvp_cand_l0] = as_mv_a[0];
    615             au1_is_top_used_l0[num_mvp_cand_l0] = (is_scaled_flag_list[0] == 0);
    616             num_mvp_cand_l0++;
    617         }
    618         if(avail_b_flag[0] == 1)
    619         {
    620             if(((as_mv_a[0].i2_mvx != as_mv_b[0].i2_mvx) ||
    621                 (as_mv_a[0].i2_mvy != as_mv_b[0].i2_mvy)) ||
    622                (0 == num_mvp_cand_l0))
    623             {
    624                 as_mvp_list_l0[num_mvp_cand_l0] = as_mv_b[0];
    625                 au1_is_top_used_l0[num_mvp_cand_l0] = 1;
    626                 num_mvp_cand_l0++;
    627             }
    628         }
    629 
    630         /* L1 candidate list*/
    631         num_mvp_cand_l1 = 0;
    632 
    633         if(avail_a_flag[1] == 1)
    634         {
    635             as_mvp_list_l1[num_mvp_cand_l1] = as_mv_a[1];
    636             au1_is_top_used_l1[num_mvp_cand_l1] = (is_scaled_flag_list[1] == 0);
    637             num_mvp_cand_l1++;
    638         }
    639         if(avail_b_flag[1] == 1)
    640         {
    641             if(((as_mv_a[1].i2_mvx != as_mv_b[1].i2_mvx) ||
    642                 (as_mv_a[1].i2_mvy != as_mv_b[1].i2_mvy)) ||
    643                (0 == num_mvp_cand_l1))
    644             {
    645                 as_mvp_list_l1[num_mvp_cand_l1] = as_mv_b[1];
    646                 au1_is_top_used_l1[num_mvp_cand_l1] = 1;
    647                 num_mvp_cand_l1++;
    648             }
    649         }
    650 
    651         /***********************************************************/
    652         /*          Collocated MV prediction                       */
    653         /***********************************************************/
    654         if((MAX_MVP_LIST_CAND > num_mvp_cand_l0) || (MAX_MVP_LIST_CAND > num_mvp_cand_l1))
    655         {
    656             mv_t as_mv_col[2], s_mv_col_l0, s_mv_col_l1;
    657             WORD32 avail_col_flag[2] = { 0 };
    658             WORD32 x_col, y_col, avail_col_l0, avail_col_l1;
    659 
    660             x_col = part_pos_x + part_wd;
    661             y_col = part_pos_y + part_ht;
    662             ihevce_collocated_mvp(ps_ctxt, ps_pu, as_mv_col, avail_col_flag, 1, x_col, y_col);
    663 
    664             avail_col_l0 = avail_col_flag[0];
    665             avail_col_l1 = avail_col_flag[1];
    666             if(avail_col_l0 || avail_col_l1)
    667             {
    668                 s_mv_col_l0 = as_mv_col[0];
    669                 s_mv_col_l1 = as_mv_col[1];
    670             }
    671 
    672             if(avail_col_l0 == 0 || avail_col_l1 == 0)
    673             {
    674                 /* Checking Collocated MV availability at Center of PU */
    675                 x_col = part_pos_x + (part_wd >> 1);
    676                 y_col = part_pos_y + (part_ht >> 1);
    677                 ihevce_collocated_mvp(ps_ctxt, ps_pu, as_mv_col, avail_col_flag, 1, x_col, y_col);
    678 
    679                 if(avail_col_l0 == 0)
    680                 {
    681                     s_mv_col_l0 = as_mv_col[0];
    682                 }
    683                 if(avail_col_l1 == 0)
    684                 {
    685                     s_mv_col_l1 = as_mv_col[1];
    686                 }
    687 
    688                 avail_col_l0 |= avail_col_flag[0];
    689                 avail_col_l1 |= avail_col_flag[1];
    690             }
    691 
    692             /* Checking if mvp index matches collocated mv */
    693             if(avail_col_l0)
    694             {
    695                 if(MAX_MVP_LIST_CAND > num_mvp_cand_l0)
    696                 {
    697                     as_mvp_list_l0[num_mvp_cand_l0] = s_mv_col_l0;
    698                     au1_is_top_used_l0[num_mvp_cand_l0] = 0;
    699                     num_mvp_cand_l0++;
    700                 }
    701             }
    702             if(avail_col_l1)
    703             {
    704                 if(MAX_MVP_LIST_CAND > num_mvp_cand_l1)
    705                 {
    706                     as_mvp_list_l1[num_mvp_cand_l1] = s_mv_col_l1;
    707                     au1_is_top_used_l1[num_mvp_cand_l1] = 0;
    708                     num_mvp_cand_l1++;
    709                 }
    710             }
    711         }
    712 
    713         /* Adding zero if mv candidates are less than 2 */
    714         while(num_mvp_cand_l0 < MAX_MVP_LIST_CAND)
    715         {
    716             as_mvp_list_l0[num_mvp_cand_l0].i2_mvx = 0;
    717             as_mvp_list_l0[num_mvp_cand_l0].i2_mvy = 0;
    718             au1_is_top_used_l0[num_mvp_cand_l0] = 0;
    719             num_mvp_cand_l0++;
    720         };
    721         while(num_mvp_cand_l1 < MAX_MVP_LIST_CAND)
    722         {
    723             as_mvp_list_l1[num_mvp_cand_l1].i2_mvx = 0;
    724             as_mvp_list_l1[num_mvp_cand_l1].i2_mvy = 0;
    725             au1_is_top_used_l1[num_mvp_cand_l1] = 0;
    726             num_mvp_cand_l1++;
    727         };
    728         /* Removing mvs if candidates are greater than 2 */
    729         if(num_mvp_cand_l0 > MAX_MVP_LIST_CAND)
    730         {
    731             num_mvp_cand_l0 = MAX_MVP_LIST_CAND;
    732         };
    733         if(num_mvp_cand_l1 > MAX_MVP_LIST_CAND)
    734         {
    735             num_mvp_cand_l1 = MAX_MVP_LIST_CAND;
    736         };
    737 
    738         /* Copying list to output */
    739         {
    740             WORD32 i;
    741             for(i = 0; i < num_mvp_cand_l0; i++)
    742             {
    743                 ps_pred_mv[i].s_l0_mv = as_mvp_list_l0[i];
    744                 pau1_is_top_used[0][i] = au1_is_top_used_l0[i];
    745             }
    746 
    747             for(i = 0; i < num_mvp_cand_l1; i++)
    748             {
    749                 ps_pred_mv[i].s_l1_mv = as_mvp_list_l1[i];
    750                 pau1_is_top_used[1][i] = au1_is_top_used_l1[i];
    751             }
    752         }
    753     }
    754 }
    755