Coverage Report

Created: 2025-11-24 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_mv_pred.c
Line
Count
Source
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
973k
{
161
973k
    WORD32 is_scaled_flag_list[2] /* Indicates whether A0 or A1 is available */;
162
973k
    WORD32 lb_avail, l_avail, t_avail, tr_avail, tl_avail;
163
973k
    WORD32 avail_a_flag[2];
164
973k
    WORD32 avail_b_flag[2];
165
973k
    mv_t as_mv_a[2];
166
973k
    mv_t as_mv_b[2];
167
973k
    UWORD8 i1_cur_ref_idx_list[2];
168
973k
    WORD32 part_pos_x;
169
973k
    WORD32 part_pos_y;
170
973k
    WORD32 part_wd;
171
973k
    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
973k
    avail_a_flag[0] = 0;
190
973k
    avail_a_flag[1] = 0;
191
973k
    avail_b_flag[0] = 0;
192
973k
    avail_b_flag[1] = 0;
193
194
973k
    as_mv_a[0].i2_mvx = 0;
195
973k
    as_mv_a[0].i2_mvy = 0;
196
973k
    as_mv_a[1].i2_mvx = 0;
197
973k
    as_mv_a[1].i2_mvy = 0;
198
973k
    as_mv_b[0].i2_mvx = 0;
199
973k
    as_mv_b[0].i2_mvy = 0;
200
973k
    as_mv_b[1].i2_mvx = 0;
201
973k
    as_mv_b[1].i2_mvy = 0;
202
203
973k
    lb_avail = ps_avail_flags->u1_bot_lt_avail;
204
973k
    l_avail = ps_avail_flags->u1_left_avail;
205
973k
    tr_avail = ps_avail_flags->u1_top_rt_avail;
206
973k
    t_avail = ps_avail_flags->u1_top_avail;
207
973k
    tl_avail = ps_avail_flags->u1_top_lt_avail;
208
209
973k
    is_scaled_flag_list[0] = 0;
210
973k
    is_scaled_flag_list[1] = 0;
211
212
973k
    part_pos_x = ps_pu->b4_pos_x << 2;
213
973k
    part_pos_y = ps_pu->b4_pos_y << 2;
214
973k
    part_wd = (ps_pu->b4_wd + 1) << 2;
215
973k
    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
973k
    i1_cur_ref_idx_list[0] = (-1 == ps_pu->mv.i1_l0_ref_idx) ? 0 : ps_pu->mv.i1_l0_ref_idx;
220
973k
    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
973k
    {
226
973k
        WORD32 l_x, a;
227
973k
        WORD32 *pi4_avail_flag;
228
973k
        WORD32 nbr_avail[2]; /*[A0/A1] */
229
973k
        WORD8 i1_nbr_ref_idx_list[2][2]; /* [A0/A1][L0/L1] */
230
973k
        UWORD8 u1_nbr_intra_flag[2]; /*[A0/A1] */
231
973k
        UWORD8 u1_nbr_pred_flag[2][2]; /* [A0/A1][L0/L1] */
232
973k
        mv_t *ps_mv;
233
973k
        nbr_4x4_t *ps_a0, *ps_a1;
234
973k
        mv_t *ps_nbr_mv[2][2]; /* [A0/A1][L0/L1] */
235
236
        /* A0 and A1 initializations */
237
973k
        ps_mv = &as_mv_a[0];
238
973k
        pi4_avail_flag = avail_a_flag;
239
240
        /* Pointers to A0 and A1 */
241
973k
        {
242
973k
            WORD32 y_a0, y_a1;
243
            /* TODO: y_a0, y_a1 is coded assuming left nbr pointer starts at PU */
244
973k
            y_a0 = (part_ht >> 2);
245
973k
            y_a1 = ((part_ht - 1) >> 2);
246
247
973k
            ps_a0 = ps_left_nbr_4x4 + (y_a0 * left_nbr_4x4_strd);
248
973k
            ps_a1 = ps_left_nbr_4x4 + (y_a1 * left_nbr_4x4_strd);
249
973k
        }
250
251
973k
        nbr_avail[0] = lb_avail && (!ps_a0->b1_intra_flag);
252
973k
        nbr_avail[1] = l_avail && (!ps_a1->b1_intra_flag);
253
254
        /* Setting is scaled flag based on availability of A0 and A1 */
255
973k
        if((nbr_avail[0] == 1) || (nbr_avail[1]))
256
536k
        {
257
536k
            is_scaled_flag_list[0] = 1;
258
536k
            is_scaled_flag_list[1] = 1;
259
536k
        }
260
261
        /* Initializing A0 variables */
262
973k
        ps_nbr_mv[0][0] = &ps_a0->mv.s_l0_mv;
263
973k
        ps_nbr_mv[0][1] = &ps_a0->mv.s_l1_mv;
264
265
973k
        i1_nbr_ref_idx_list[0][0] = ps_a0->mv.i1_l0_ref_idx;
266
973k
        i1_nbr_ref_idx_list[0][1] = ps_a0->mv.i1_l1_ref_idx;
267
268
973k
        u1_nbr_pred_flag[0][0] = (UWORD8)ps_a0->b1_pred_l0_flag;
269
973k
        u1_nbr_pred_flag[0][1] = (UWORD8)ps_a0->b1_pred_l1_flag;
270
271
973k
        u1_nbr_intra_flag[0] = (UWORD8)ps_a0->b1_intra_flag;
272
273
        /* Initializing A1 variables */
274
973k
        ps_nbr_mv[1][0] = &ps_a1->mv.s_l0_mv;
275
973k
        ps_nbr_mv[1][1] = &ps_a1->mv.s_l1_mv;
276
277
973k
        i1_nbr_ref_idx_list[1][0] = ps_a1->mv.i1_l0_ref_idx;
278
973k
        i1_nbr_ref_idx_list[1][1] = ps_a1->mv.i1_l1_ref_idx;
279
280
973k
        u1_nbr_pred_flag[1][0] = (UWORD8)ps_a1->b1_pred_l0_flag;
281
973k
        u1_nbr_pred_flag[1][1] = (UWORD8)ps_a1->b1_pred_l1_flag;
282
283
973k
        u1_nbr_intra_flag[1] = (UWORD8)ps_a1->b1_intra_flag;
284
285
        /* Derivation of mvL0A and mvL1A from A0 and A1 */
286
2.92M
        for(l_x = 0; l_x < 2; l_x++) /* list 0 and list 1 */
287
1.94M
        {
288
1.94M
            WORD32 l_y;
289
290
1.94M
            l_y = !l_x; /* if i=0, y = L1 else y = L0 */
291
292
4.95M
            for(a = 0; a < 2; a++)
293
3.75M
            {
294
                /* MODE_INTRA check has been taken care in availability check */
295
3.75M
                if((nbr_avail[a] == 1) && (pi4_avail_flag[l_x] == 0))
296
1.12M
                {
297
1.12M
                    if(u1_nbr_pred_flag[a][l_x] == 1)
298
674k
                    {
299
674k
                        WORD32 nbr_ref_poc, cur_ref_poc;
300
674k
                        WORD8 i1_cur_ref_idx, i1_nbr_ref_idx;
301
302
674k
                        i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
303
674k
                        cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
304
674k
                        i1_nbr_ref_idx = i1_nbr_ref_idx_list[a][l_x];
305
674k
                        nbr_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_nbr_ref_idx]->i4_poc;
306
307
674k
                        if(nbr_ref_poc == cur_ref_poc)
308
503k
                        {
309
503k
                            pi4_avail_flag[l_x] = 1;
310
503k
                            ps_mv[l_x] = *ps_nbr_mv[a][l_x];
311
503k
                            break;
312
503k
                        }
313
674k
                    }
314
624k
                    if(u1_nbr_pred_flag[a][l_y] == 1)
315
480k
                    {
316
480k
                        WORD32 nbr_ref_poc, cur_ref_poc;
317
480k
                        WORD8 i1_nbr_ref_idx, i1_cur_ref_idx;
318
319
480k
                        i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
320
480k
                        cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
321
322
480k
                        i1_nbr_ref_idx = i1_nbr_ref_idx_list[a][l_y];
323
480k
                        nbr_ref_poc = ps_ctxt->ps_ref_list[l_y][i1_nbr_ref_idx]->i4_poc;
324
480k
                        if(nbr_ref_poc == cur_ref_poc)
325
241k
                        {
326
241k
                            pi4_avail_flag[l_x] = 1;
327
241k
                            ps_mv[l_x] = *ps_nbr_mv[a][l_y];
328
241k
                            break;
329
241k
                        }
330
480k
                    }
331
624k
                }
332
3.75M
            }
333
1.94M
        }
334
335
2.92M
        for(l_x = 0; l_x < 2; l_x++) /* list 0 and list 1 */
336
1.94M
        {
337
1.94M
            if(pi4_avail_flag[l_x] == 0)
338
1.20M
            {
339
1.20M
                WORD8 i1_nbr_ref_list_idx, i1_nbr_ref_idx;
340
1.20M
                WORD32 l_y;
341
342
1.20M
                l_y = !l_x; /* if i=0, y = L1 else y = L0 */
343
344
3.22M
                for(a = 0; a < 2; a++)
345
2.34M
                {
346
                    /* MODE_INTRA check has been taken care in availability check */
347
2.34M
                    if((nbr_avail[a] == 1) && (pi4_avail_flag[l_x] == 0))
348
327k
                    {
349
                        /* Long term reference check Removed */
350
327k
                        if(u1_nbr_pred_flag[a][l_x] == 1)
351
145k
                        {
352
145k
                            pi4_avail_flag[l_x] = 1;
353
145k
                            ps_mv[l_x] = *ps_nbr_mv[a][l_x];
354
145k
                            i1_nbr_ref_idx = i1_nbr_ref_idx_list[a][l_x];
355
145k
                            i1_nbr_ref_list_idx = l_x;
356
145k
                            break;
357
145k
                        }
358
                        /* Long term reference check Removed */
359
181k
                        else if(u1_nbr_pred_flag[a][l_y] == 1)
360
181k
                        {
361
181k
                            pi4_avail_flag[l_x] = 1;
362
181k
                            ps_mv[l_x] = *ps_nbr_mv[a][l_y];
363
181k
                            i1_nbr_ref_idx = i1_nbr_ref_idx_list[a][l_y];
364
181k
                            i1_nbr_ref_list_idx = l_y;
365
181k
                            break;
366
181k
                        }
367
327k
                    }
368
2.34M
                }
369
370
                /* Long term reference check Removed */
371
1.20M
                if(pi4_avail_flag[l_x] == 1)
372
327k
                {
373
327k
                    WORD8 i1_cur_ref_idx;
374
327k
                    WORD32 cur_ref_poc, nbr_ref_poc;
375
327k
                    WORD32 cur_poc;
376
377
327k
                    i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
378
327k
                    cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
379
380
327k
                    nbr_ref_poc = ps_ctxt->ps_ref_list[i1_nbr_ref_list_idx][i1_nbr_ref_idx]->i4_poc;
381
382
327k
                    cur_poc = ps_ctxt->ps_slice_hdr->i4_abs_pic_order_cnt;
383
384
327k
                    ihevce_scale_mv(&ps_mv[l_x], cur_ref_poc, nbr_ref_poc, cur_poc);
385
327k
                }
386
1.20M
            }
387
1.94M
        }
388
973k
    }
389
390
    /************************************************************/
391
    /* Calculating of motion vector B from neighbors B0 and B1  */
392
    /************************************************************/
393
973k
    {
394
973k
        WORD32 l_x, b;
395
973k
        WORD32 *pi4_avail_flag;
396
973k
        WORD32 nbr_avail[3]; /* [B0/B1/B2] */
397
973k
        WORD8 i1_nbr_ref_idx_list[3][2]; /* [B0/B1/B2][L0/L1] */
398
973k
        UWORD8 u1_nbr_intra_flag[3]; /*[B0/B1/B2] */
399
973k
        UWORD8 u1_nbr_pred_flag[3][2]; /* [B0/B1/B2][L0/L1] */
400
973k
        mv_t *ps_mv;
401
973k
        nbr_4x4_t *ps_b0, *ps_b1, *ps_b2;
402
973k
        mv_t *ps_nbr_mv[3][2]; /* [B0/B1/B2][L0/L1] */
403
404
        /* B0, B1 and B2 initializations */
405
973k
        ps_mv = &as_mv_b[0];
406
973k
        pi4_avail_flag = avail_b_flag;
407
408
        /* Pointers to B0, B1 and B2 */
409
973k
        {
410
973k
            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
973k
            x_b0 = (part_pos_x + part_wd);
415
973k
            x_b1 = (part_pos_x + part_wd - 1);
416
973k
            x_b2 = (part_pos_x - 1);
417
418
            /* Getting offset back to given pointer */
419
973k
            x_b0 = x_b0 - part_pos_x;
420
973k
            x_b1 = x_b1 - part_pos_x;
421
973k
            x_b2 = x_b2 - part_pos_x;
422
423
            /* Below derivation are based on top pointer */
424
            /* is pointing first pixel of PU             */
425
973k
            ps_b0 = ps_top_nbr_4x4 + (x_b0 >> 2);
426
973k
            ps_b1 = ps_top_nbr_4x4 + (x_b1 >> 2);
427
428
            /* At CTB boundary, use top-left passed in */
429
973k
            if(part_pos_y)
430
615k
            {
431
615k
                ps_b2 = ps_top_left_nbr_4x4;
432
615k
            }
433
358k
            else
434
358k
            {
435
                /* Not at CTB boundary, use top and  */
436
                /* add correction to go to top-left */
437
358k
                ps_b2 = (ps_top_nbr_4x4) + (x_b2 >> 2);
438
358k
            }
439
973k
        }
440
973k
        nbr_avail[0] = tr_avail && (!ps_b0->b1_intra_flag);
441
973k
        nbr_avail[1] = t_avail && (!ps_b1->b1_intra_flag);
442
973k
        nbr_avail[2] = tl_avail && (!ps_b2->b1_intra_flag);
443
444
        /* Initializing B0 related variables */
445
973k
        ps_nbr_mv[0][0] = &ps_b0->mv.s_l0_mv;
446
973k
        ps_nbr_mv[0][1] = &ps_b0->mv.s_l1_mv;
447
448
973k
        i1_nbr_ref_idx_list[0][0] = ps_b0->mv.i1_l0_ref_idx;
449
973k
        i1_nbr_ref_idx_list[0][1] = ps_b0->mv.i1_l1_ref_idx;
450
451
973k
        u1_nbr_pred_flag[0][0] = (UWORD8)ps_b0->b1_pred_l0_flag;
452
973k
        u1_nbr_pred_flag[0][1] = (UWORD8)ps_b0->b1_pred_l1_flag;
453
454
973k
        u1_nbr_intra_flag[0] = (UWORD8)ps_b0->b1_intra_flag;
455
456
        /* Initializing B1 related variables */
457
973k
        ps_nbr_mv[1][0] = &ps_b1->mv.s_l0_mv;
458
973k
        ps_nbr_mv[1][1] = &ps_b1->mv.s_l1_mv;
459
460
973k
        i1_nbr_ref_idx_list[1][0] = ps_b1->mv.i1_l0_ref_idx;
461
973k
        i1_nbr_ref_idx_list[1][1] = ps_b1->mv.i1_l1_ref_idx;
462
463
973k
        u1_nbr_pred_flag[1][0] = (UWORD8)ps_b1->b1_pred_l0_flag;
464
973k
        u1_nbr_pred_flag[1][1] = (UWORD8)ps_b1->b1_pred_l1_flag;
465
466
973k
        u1_nbr_intra_flag[1] = (UWORD8)ps_b1->b1_intra_flag;
467
468
        /* Initializing B2 related variables */
469
973k
        ps_nbr_mv[2][0] = &ps_b2->mv.s_l0_mv;
470
973k
        ps_nbr_mv[2][1] = &ps_b2->mv.s_l1_mv;
471
472
973k
        i1_nbr_ref_idx_list[2][0] = ps_b2->mv.i1_l0_ref_idx;
473
973k
        i1_nbr_ref_idx_list[2][1] = ps_b2->mv.i1_l1_ref_idx;
474
475
973k
        u1_nbr_pred_flag[2][0] = (UWORD8)ps_b2->b1_pred_l0_flag;
476
973k
        u1_nbr_pred_flag[2][1] = (UWORD8)ps_b2->b1_pred_l1_flag;
477
478
973k
        u1_nbr_intra_flag[2] = (UWORD8)ps_b2->b1_intra_flag;
479
480
        /* Derivation of mvL0B and mvL1B from B0,B1 and B2 */
481
2.92M
        for(l_x = 0; l_x < 2; l_x++) /* list 0 and list 1 */
482
1.94M
        {
483
1.94M
            WORD32 l_y;
484
485
1.94M
            l_y = !l_x; /* if i=0, y = L1 else y = L0 */
486
487
5.79M
            for(b = 0; b < 3; b++)
488
4.64M
            {
489
4.64M
                if((nbr_avail[b] == 1) && (pi4_avail_flag[l_x] == 0))
490
1.48M
                {
491
1.48M
                    if(u1_nbr_pred_flag[b][l_x] == 1)
492
837k
                    {
493
837k
                        WORD32 nbr_ref_poc, cur_ref_poc;
494
837k
                        WORD8 i1_cur_ref_idx, i1_nbr_ref_idx;
495
496
837k
                        i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
497
837k
                        cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
498
837k
                        i1_nbr_ref_idx = i1_nbr_ref_idx_list[b][l_x];
499
837k
                        nbr_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_nbr_ref_idx]->i4_poc;
500
501
837k
                        if(nbr_ref_poc == cur_ref_poc)
502
535k
                        {
503
535k
                            pi4_avail_flag[l_x] = 1;
504
535k
                            ps_mv[l_x] = *ps_nbr_mv[b][l_x];
505
535k
                            break;
506
535k
                        }
507
837k
                    }
508
950k
                    if(u1_nbr_pred_flag[b][l_y] == 1)
509
699k
                    {
510
699k
                        WORD32 nbr_ref_poc, cur_ref_poc;
511
699k
                        WORD8 i1_nbr_ref_idx, i1_cur_ref_idx;
512
513
699k
                        i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
514
699k
                        cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
515
516
699k
                        i1_nbr_ref_idx = i1_nbr_ref_idx_list[b][l_y];
517
699k
                        nbr_ref_poc = ps_ctxt->ps_ref_list[l_y][i1_nbr_ref_idx]->i4_poc;
518
519
699k
                        if(nbr_ref_poc == cur_ref_poc)
520
263k
                        {
521
263k
                            pi4_avail_flag[l_x] = 1;
522
263k
                            ps_mv[l_x] = *ps_nbr_mv[b][l_y];
523
263k
                            break;
524
263k
                        }
525
699k
                    }
526
950k
                }
527
4.64M
            }
528
1.94M
        }
529
530
973k
        if((is_scaled_flag_list[0] == 0) && (avail_b_flag[0] == 1))
531
151k
        {
532
151k
            avail_a_flag[0] = 1;
533
151k
            as_mv_a[0] = as_mv_b[0];
534
151k
        }
535
973k
        if((is_scaled_flag_list[1] == 0) && (avail_b_flag[1] == 1))
536
160k
        {
537
160k
            avail_a_flag[1] = 1;
538
160k
            as_mv_a[1] = as_mv_b[1];
539
160k
        }
540
541
2.92M
        for(l_x = 0; l_x < 2; l_x++) /* list 0 and list 1 */
542
1.94M
        {
543
1.94M
            if(is_scaled_flag_list[l_x] == 0)
544
875k
            {
545
                /* If isScaledFlagLX == 0, availFlagLXB flag is set to 0 */
546
875k
                pi4_avail_flag[l_x] = 0;
547
875k
                {
548
875k
                    WORD8 i1_nbr_ref_list_idx, i1_nbr_ref_idx;
549
875k
                    WORD32 l_y;
550
551
875k
                    l_y = !l_x; /* if i=0, y = L1 else y = L0 */
552
553
2.33M
                    for(b = 0; b < 3; b++)
554
1.89M
                    {
555
1.89M
                        if((nbr_avail[b] == 1) && (pi4_avail_flag[l_x] == 0))
556
440k
                        {
557
                            /* Long term reference check Removed */
558
440k
                            if(u1_nbr_pred_flag[b][l_x] == 1)
559
256k
                            {
560
256k
                                pi4_avail_flag[l_x] = 1;
561
256k
                                ps_mv[l_x] = *ps_nbr_mv[b][l_x];
562
256k
                                i1_nbr_ref_idx = i1_nbr_ref_idx_list[b][l_x];
563
256k
                                i1_nbr_ref_list_idx = l_x;
564
256k
                                break;
565
256k
                            }
566
                            /* Long term reference check Removed */
567
184k
                            else if(u1_nbr_pred_flag[b][l_y] == 1)
568
184k
                            {
569
184k
                                pi4_avail_flag[l_x] = 1;
570
184k
                                ps_mv[l_x] = *ps_nbr_mv[b][l_y];
571
184k
                                i1_nbr_ref_idx = i1_nbr_ref_idx_list[b][l_y];
572
184k
                                i1_nbr_ref_list_idx = l_y;
573
184k
                                break;
574
184k
                            }
575
440k
                        }
576
1.89M
                    }
577
                    /* Long term reference check Removed */
578
875k
                    if(pi4_avail_flag[l_x] == 1)
579
440k
                    {
580
440k
                        WORD8 i1_cur_ref_idx;
581
440k
                        WORD32 cur_ref_poc, nbr_ref_poc;
582
440k
                        WORD32 cur_poc;
583
584
440k
                        i1_cur_ref_idx = i1_cur_ref_idx_list[l_x];
585
440k
                        cur_ref_poc = ps_ctxt->ps_ref_list[l_x][i1_cur_ref_idx]->i4_poc;
586
587
440k
                        nbr_ref_poc =
588
440k
                            ps_ctxt->ps_ref_list[i1_nbr_ref_list_idx][i1_nbr_ref_idx]->i4_poc;
589
590
440k
                        cur_poc = ps_ctxt->ps_slice_hdr->i4_abs_pic_order_cnt;
591
592
440k
                        if(cur_ref_poc != nbr_ref_poc)
593
130k
                            ihevce_scale_mv(&ps_mv[l_x], cur_ref_poc, nbr_ref_poc, cur_poc);
594
440k
                    }
595
875k
                }
596
875k
            }
597
1.94M
        }
598
973k
    }
599
600
    /* Candidate list */
601
973k
    {
602
973k
        mv_t as_mvp_list_l0[MAX_MVP_LIST_CAND_MEM]; /*[Cand0/Cand1/Cand2] */
603
973k
        mv_t as_mvp_list_l1[MAX_MVP_LIST_CAND_MEM]; /*[Cand0/Cand1/Cand2] */
604
973k
        UWORD8 au1_is_top_used_l0[MAX_MVP_LIST_CAND_MEM];
605
973k
        UWORD8 au1_is_top_used_l1[MAX_MVP_LIST_CAND_MEM];
606
973k
        WORD32 num_mvp_cand_l0;
607
973k
        WORD32 num_mvp_cand_l1;
608
609
        /* L0 candidate list*/
610
973k
        num_mvp_cand_l0 = 0;
611
612
973k
        if(avail_a_flag[0] == 1)
613
688k
        {
614
688k
            as_mvp_list_l0[num_mvp_cand_l0] = as_mv_a[0];
615
688k
            au1_is_top_used_l0[num_mvp_cand_l0] = (is_scaled_flag_list[0] == 0);
616
688k
            num_mvp_cand_l0++;
617
688k
        }
618
973k
        if(avail_b_flag[0] == 1)
619
457k
        {
620
457k
            if(((as_mv_a[0].i2_mvx != as_mv_b[0].i2_mvx) ||
621
390k
                (as_mv_a[0].i2_mvy != as_mv_b[0].i2_mvy)) ||
622
355k
               (0 == num_mvp_cand_l0))
623
126k
            {
624
126k
                as_mvp_list_l0[num_mvp_cand_l0] = as_mv_b[0];
625
126k
                au1_is_top_used_l0[num_mvp_cand_l0] = 1;
626
126k
                num_mvp_cand_l0++;
627
126k
            }
628
457k
        }
629
630
        /* L1 candidate list*/
631
973k
        num_mvp_cand_l1 = 0;
632
633
973k
        if(avail_a_flag[1] == 1)
634
697k
        {
635
697k
            as_mvp_list_l1[num_mvp_cand_l1] = as_mv_a[1];
636
697k
            au1_is_top_used_l1[num_mvp_cand_l1] = (is_scaled_flag_list[1] == 0);
637
697k
            num_mvp_cand_l1++;
638
697k
        }
639
973k
        if(avail_b_flag[1] == 1)
640
469k
        {
641
469k
            if(((as_mv_a[1].i2_mvx != as_mv_b[1].i2_mvx) ||
642
403k
                (as_mv_a[1].i2_mvy != as_mv_b[1].i2_mvy)) ||
643
369k
               (0 == num_mvp_cand_l1))
644
119k
            {
645
119k
                as_mvp_list_l1[num_mvp_cand_l1] = as_mv_b[1];
646
119k
                au1_is_top_used_l1[num_mvp_cand_l1] = 1;
647
119k
                num_mvp_cand_l1++;
648
119k
            }
649
469k
        }
650
651
        /***********************************************************/
652
        /*          Collocated MV prediction                       */
653
        /***********************************************************/
654
973k
        if((MAX_MVP_LIST_CAND > num_mvp_cand_l0) || (MAX_MVP_LIST_CAND > num_mvp_cand_l1))
655
923k
        {
656
923k
            mv_t as_mv_col[2], s_mv_col_l0, s_mv_col_l1;
657
923k
            WORD32 avail_col_flag[2] = { 0 };
658
923k
            WORD32 x_col, y_col, avail_col_l0, avail_col_l1;
659
660
923k
            x_col = part_pos_x + part_wd;
661
923k
            y_col = part_pos_y + part_ht;
662
923k
            ihevce_collocated_mvp(ps_ctxt, ps_pu, as_mv_col, avail_col_flag, 1, x_col, y_col);
663
664
923k
            avail_col_l0 = avail_col_flag[0];
665
923k
            avail_col_l1 = avail_col_flag[1];
666
923k
            if(avail_col_l0 || avail_col_l1)
667
453k
            {
668
453k
                s_mv_col_l0 = as_mv_col[0];
669
453k
                s_mv_col_l1 = as_mv_col[1];
670
453k
            }
671
672
923k
            if(avail_col_l0 == 0 || avail_col_l1 == 0)
673
903k
            {
674
                /* Checking Collocated MV availability at Center of PU */
675
903k
                x_col = part_pos_x + (part_wd >> 1);
676
903k
                y_col = part_pos_y + (part_ht >> 1);
677
903k
                ihevce_collocated_mvp(ps_ctxt, ps_pu, as_mv_col, avail_col_flag, 1, x_col, y_col);
678
679
903k
                if(avail_col_l0 == 0)
680
534k
                {
681
534k
                    s_mv_col_l0 = as_mv_col[0];
682
534k
                }
683
903k
                if(avail_col_l1 == 0)
684
838k
                {
685
838k
                    s_mv_col_l1 = as_mv_col[1];
686
838k
                }
687
688
903k
                avail_col_l0 |= avail_col_flag[0];
689
903k
                avail_col_l1 |= avail_col_flag[1];
690
903k
            }
691
692
            /* Checking if mvp index matches collocated mv */
693
923k
            if(avail_col_l0)
694
563k
            {
695
563k
                if(MAX_MVP_LIST_CAND > num_mvp_cand_l0)
696
558k
                {
697
558k
                    as_mvp_list_l0[num_mvp_cand_l0] = s_mv_col_l0;
698
558k
                    au1_is_top_used_l0[num_mvp_cand_l0] = 0;
699
558k
                    num_mvp_cand_l0++;
700
558k
                }
701
563k
            }
702
923k
            if(avail_col_l1)
703
99.1k
            {
704
99.1k
                if(MAX_MVP_LIST_CAND > num_mvp_cand_l1)
705
96.7k
                {
706
96.7k
                    as_mvp_list_l1[num_mvp_cand_l1] = s_mv_col_l1;
707
96.7k
                    au1_is_top_used_l1[num_mvp_cand_l1] = 0;
708
96.7k
                    num_mvp_cand_l1++;
709
96.7k
                }
710
99.1k
            }
711
923k
        }
712
713
        /* Adding zero if mv candidates are less than 2 */
714
1.54M
        while(num_mvp_cand_l0 < MAX_MVP_LIST_CAND)
715
574k
        {
716
574k
            as_mvp_list_l0[num_mvp_cand_l0].i2_mvx = 0;
717
574k
            as_mvp_list_l0[num_mvp_cand_l0].i2_mvy = 0;
718
574k
            au1_is_top_used_l0[num_mvp_cand_l0] = 0;
719
574k
            num_mvp_cand_l0++;
720
574k
        };
721
2.00M
        while(num_mvp_cand_l1 < MAX_MVP_LIST_CAND)
722
1.03M
        {
723
1.03M
            as_mvp_list_l1[num_mvp_cand_l1].i2_mvx = 0;
724
1.03M
            as_mvp_list_l1[num_mvp_cand_l1].i2_mvy = 0;
725
1.03M
            au1_is_top_used_l1[num_mvp_cand_l1] = 0;
726
1.03M
            num_mvp_cand_l1++;
727
1.03M
        };
728
        /* Removing mvs if candidates are greater than 2 */
729
973k
        if(num_mvp_cand_l0 > MAX_MVP_LIST_CAND)
730
0
        {
731
0
            num_mvp_cand_l0 = MAX_MVP_LIST_CAND;
732
0
        };
733
973k
        if(num_mvp_cand_l1 > MAX_MVP_LIST_CAND)
734
0
        {
735
0
            num_mvp_cand_l1 = MAX_MVP_LIST_CAND;
736
0
        };
737
738
        /* Copying list to output */
739
973k
        {
740
973k
            WORD32 i;
741
2.92M
            for(i = 0; i < num_mvp_cand_l0; i++)
742
1.94M
            {
743
1.94M
                ps_pred_mv[i].s_l0_mv = as_mvp_list_l0[i];
744
1.94M
                pau1_is_top_used[0][i] = au1_is_top_used_l0[i];
745
1.94M
            }
746
747
2.92M
            for(i = 0; i < num_mvp_cand_l1; i++)
748
1.94M
            {
749
1.94M
                ps_pred_mv[i].s_l1_mv = as_mvp_list_l1[i];
750
1.94M
                pau1_is_top_used[1][i] = au1_is_top_used_l1[i];
751
1.94M
            }
752
973k
        }
753
973k
    }
754
973k
}