Coverage Report

Created: 2026-10-17 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/contrib/contrib-build/bpg/libavcodec/hevc_mvs.c
Line
Count
Source
1
/*
2
 * HEVC video decoder
3
 *
4
 * Copyright (C) 2012 - 2013 Guillaume Martres
5
 * Copyright (C) 2013 Anand Meher Kotra
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23
24
#include "hevc.h"
25
26
#ifdef USE_PRED
27
static const uint8_t l0_l1_cand_idx[12][2] = {
28
    { 0, 1, },
29
    { 1, 0, },
30
    { 0, 2, },
31
    { 2, 0, },
32
    { 1, 2, },
33
    { 2, 1, },
34
    { 0, 3, },
35
    { 3, 0, },
36
    { 1, 3, },
37
    { 3, 1, },
38
    { 2, 3, },
39
    { 3, 2, },
40
};
41
#endif
42
43
void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
44
                                     int nPbW, int nPbH)
45
0
{
46
0
    HEVCLocalContext *lc = s->HEVClc;
47
0
    int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
48
0
    int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
49
50
0
    lc->na.cand_up       = (lc->ctb_up_flag   || y0b);
51
0
    lc->na.cand_left     = (lc->ctb_left_flag || x0b);
52
0
    lc->na.cand_up_left  = (!x0b && !y0b) ? lc->ctb_up_left_flag : lc->na.cand_left && lc->na.cand_up;
53
0
    lc->na.cand_up_right_sap =
54
0
            ((x0b + nPbW) == (1 << s->sps->log2_ctb_size)) ?
55
0
                    lc->ctb_up_right_flag && !y0b : lc->na.cand_up;
56
0
    lc->na.cand_up_right =
57
0
            lc->na.cand_up_right_sap
58
0
                     && (x0 + nPbW) < lc->end_of_tiles_x;
59
0
    lc->na.cand_bottom_left = ((y0 + nPbH) >= lc->end_of_tiles_y) ? 0 : lc->na.cand_left;
60
0
}
61
62
#ifdef USE_PRED
63
/*
64
 * 6.4.1 Derivation process for z-scan order block availability
65
 */
66
static av_always_inline int z_scan_block_avail(HEVCContext *s, int xCurr, int yCurr,
67
                              int xN, int yN)
68
0
{
69
0
#define MIN_TB_ADDR_ZS(x, y)                                            \
70
0
    s->pps->min_tb_addr_zs[(y) * (s->sps->tb_mask+2) + (x)]
71
72
0
    int xCurr_ctb = xCurr >> s->sps->log2_ctb_size;
73
0
    int yCurr_ctb = yCurr >> s->sps->log2_ctb_size;
74
0
    int xN_ctb    = xN    >> s->sps->log2_ctb_size;
75
0
    int yN_ctb    = yN    >> s->sps->log2_ctb_size;
76
0
    if( yN_ctb < yCurr_ctb || xN_ctb < xCurr_ctb )
77
0
        return 1;
78
0
    else {
79
0
        int Curr = MIN_TB_ADDR_ZS((xCurr >> s->sps->log2_min_tb_size) & s->sps->tb_mask,
80
0
                (yCurr >> s->sps->log2_min_tb_size) & s->sps->tb_mask);
81
0
        int N    = MIN_TB_ADDR_ZS((xN >> s->sps->log2_min_tb_size) & s->sps->tb_mask,
82
0
                (yN >> s->sps->log2_min_tb_size) & s->sps->tb_mask);
83
0
        return N <= Curr;
84
0
    }
85
0
}
86
87
//check if the two luma locations belong to the same mostion estimation region
88
static av_always_inline int is_diff_mer(HEVCContext *s, int xN, int yN, int xP, int yP)
89
0
{
90
0
    uint8_t plevel = s->pps->log2_parallel_merge_level;
91
92
0
    return xN >> plevel == xP >> plevel &&
93
0
           yN >> plevel == yP >> plevel;
94
0
}
95
96
0
#define MATCH_MV(x) (AV_RN32A(&A.x) == AV_RN32A(&B.x))
97
0
#define MATCH(x) (A.x == B.x)
98
99
// check if the mv's and refidx are the same between A and B
100
static av_always_inline int compare_mv_ref_idx(struct MvField A, struct MvField B)
101
0
{
102
0
    int a_pf = A.pred_flag;
103
0
    int b_pf = B.pred_flag;
104
0
    if (a_pf == b_pf) {
105
0
        if (a_pf == PF_BI) {
106
0
            return MATCH(ref_idx[0]) && MATCH_MV(mv[0]) &&
107
0
                   MATCH(ref_idx[1]) && MATCH_MV(mv[1]);
108
0
        } else if (a_pf == PF_L0) {
109
0
            return MATCH(ref_idx[0]) && MATCH_MV(mv[0]);
110
0
        } else if (a_pf == PF_L1) {
111
0
            return MATCH(ref_idx[1]) && MATCH_MV(mv[1]);
112
0
        }
113
0
    }
114
0
    return 0;
115
0
}
116
117
static av_always_inline void mv_scale(Mv *dst, Mv *src, int td, int tb)
118
0
{
119
0
    int tx, scale_factor;
120
121
0
    td = av_clip_int8(td);
122
0
    tb = av_clip_int8(tb);
123
0
    tx = (0x4000 + abs(td / 2)) / td;
124
0
    scale_factor = av_clip((tb * tx + 32) >> 6, -4096, 4095);
125
0
    dst->x = av_clip_int16((scale_factor * src->x + 127 +
126
0
                           (scale_factor * src->x < 0)) >> 8);
127
0
    dst->y = av_clip_int16((scale_factor * src->y + 127 +
128
0
                           (scale_factor * src->y < 0)) >> 8);
129
0
}
130
131
static int check_mvset(Mv *mvLXCol, Mv *mvCol,
132
                       int colPic, int poc,
133
                       RefPicList *refPicList, int X, int refIdxLx,
134
                       RefPicList *refPicList_col, int listCol, int refidxCol)
135
0
{
136
0
    int cur_lt = refPicList[X].isLongTerm[refIdxLx];
137
0
    int col_lt = refPicList_col[listCol].isLongTerm[refidxCol];
138
0
    int col_poc_diff, cur_poc_diff;
139
140
0
    if (cur_lt != col_lt) {
141
0
        mvLXCol->x = 0;
142
0
        mvLXCol->y = 0;
143
0
        return 0;
144
0
    }
145
146
0
    col_poc_diff = colPic - refPicList_col[listCol].list[refidxCol];
147
0
    cur_poc_diff = poc    - refPicList[X].list[refIdxLx];
148
149
0
    if (cur_lt || col_poc_diff == cur_poc_diff || !col_poc_diff) {
150
0
        mvLXCol->x = mvCol->x;
151
0
        mvLXCol->y = mvCol->y;
152
0
    } else {
153
0
        mv_scale(mvLXCol, mvCol, col_poc_diff, cur_poc_diff);
154
0
    }
155
0
    return 1;
156
0
}
157
158
#define CHECK_MVSET(l)                                          \
159
0
    check_mvset(mvLXCol, temp_col.mv + l,                       \
160
0
                colPic, s->poc,                                 \
161
0
                refPicList, X, refIdxLx,                        \
162
0
                refPicList_col, L ## l, temp_col.ref_idx[l])
163
164
// derive the motion vectors section 8.5.3.1.8
165
static int derive_temporal_colocated_mvs(HEVCContext *s, MvField temp_col,
166
                                         int refIdxLx, Mv *mvLXCol, int X,
167
                                         int colPic, RefPicList *refPicList_col)
168
0
{
169
0
    RefPicList *refPicList = s->ref->refPicList;
170
171
0
    if (temp_col.pred_flag == PF_INTRA)
172
0
        return 0;
173
174
0
    if (!(temp_col.pred_flag & PF_L0))
175
0
        return CHECK_MVSET(1);
176
0
    else if (temp_col.pred_flag == PF_L0)
177
0
        return CHECK_MVSET(0);
178
0
    else if (temp_col.pred_flag == PF_BI) {
179
0
        int check_diffpicount = 0;
180
0
        int i, j;
181
0
        for (j = 0; j < 2; j++) {
182
0
            for (i = 0; i < refPicList[j].nb_refs; i++) {
183
0
                if (refPicList[j].list[i] > s->poc) {
184
0
                    check_diffpicount++;
185
0
                    break;
186
0
                }
187
0
            }
188
0
        }
189
0
        if (!check_diffpicount) {
190
0
            if (X==0)
191
0
                return CHECK_MVSET(0);
192
0
            else
193
0
                return CHECK_MVSET(1);
194
0
        } else {
195
0
            if (s->sh.collocated_list == L1)
196
0
                return CHECK_MVSET(0);
197
0
            else
198
0
                return CHECK_MVSET(1);
199
0
        }
200
0
    }
201
202
0
    return 0;
203
0
}
204
205
#define TAB_MVF(x, y)                                                   \
206
0
    tab_mvf[(y) * min_pu_width + x]
207
208
#define TAB_MVF_PU(v)                                                   \
209
0
    TAB_MVF(((x ## v) >> s->sps->log2_min_pu_size),                     \
210
0
            ((y ## v) >> s->sps->log2_min_pu_size))
211
212
#define DERIVE_TEMPORAL_COLOCATED_MVS                                   \
213
0
    derive_temporal_colocated_mvs(s, temp_col,                          \
214
0
                                  refIdxLx, mvLXCol, X, colPic,         \
215
0
                                  ff_hevc_get_ref_list(s, ref, x, y))
216
217
/*
218
 * 8.5.3.1.7  temporal luma motion vector prediction
219
 */
220
static int temporal_luma_motion_vector(HEVCContext *s, int x0, int y0,
221
                                       int nPbW, int nPbH, int refIdxLx,
222
                                       Mv *mvLXCol, int X)
223
0
{
224
0
    MvField *tab_mvf;
225
0
    MvField temp_col;
226
0
    int x, y, x_pu, y_pu;
227
0
    int min_pu_width = s->sps->min_pu_width;
228
0
    int availableFlagLXCol = 0;
229
0
    int colPic;
230
231
0
    HEVCFrame *ref = s->ref->collocated_ref;
232
233
0
    if (!ref) {
234
0
        memset(mvLXCol, 0, sizeof(*mvLXCol));
235
0
        return 0;
236
0
    }
237
238
0
    tab_mvf = ref->tab_mvf;
239
0
    colPic  = ref->poc;
240
241
    //bottom right collocated motion vector
242
0
    x = x0 + nPbW;
243
0
    y = y0 + nPbH;
244
245
0
    if (tab_mvf &&
246
0
        (y0 >> s->sps->log2_ctb_size) == (y >> s->sps->log2_ctb_size) &&
247
0
        y < s->sps->height &&
248
0
        x < s->sps->width) {
249
0
        x                 &= ~15;
250
0
        y                 &= ~15;
251
0
        if (s->threads_type == FF_THREAD_FRAME)
252
0
            ff_thread_await_progress(&ref->tf, y, 0);
253
0
        x_pu               = x >> s->sps->log2_min_pu_size;
254
0
        y_pu               = y >> s->sps->log2_min_pu_size;
255
0
        temp_col           = TAB_MVF(x_pu, y_pu);
256
0
        availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS;
257
0
    }
258
259
    // derive center collocated motion vector
260
0
    if (tab_mvf && !availableFlagLXCol) {
261
0
        x                  = x0 + (nPbW >> 1);
262
0
        y                  = y0 + (nPbH >> 1);
263
0
        x                 &= ~15;
264
0
        y                 &= ~15;
265
0
        if (s->threads_type == FF_THREAD_FRAME)
266
0
            ff_thread_await_progress(&ref->tf, y, 0);
267
0
        x_pu               = x >> s->sps->log2_min_pu_size;
268
0
        y_pu               = y >> s->sps->log2_min_pu_size;
269
0
        temp_col           = TAB_MVF(x_pu, y_pu);
270
0
        availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS;
271
0
    }
272
0
    return availableFlagLXCol;
273
0
}
274
275
#define AVAILABLE(cand, v)                                      \
276
0
    (cand && !(TAB_MVF_PU(v).pred_flag == PF_INTRA))
277
278
#define PRED_BLOCK_AVAILABLE(v)                                 \
279
0
    z_scan_block_avail(s, x0, y0, x ## v, y ## v)
280
281
#define COMPARE_MV_REFIDX(a, b)                                 \
282
0
    compare_mv_ref_idx(TAB_MVF_PU(a), TAB_MVF_PU(b))
283
284
/*
285
 * 8.5.3.1.2  Derivation process for spatial merging candidates
286
 */
287
static void derive_spatial_merge_candidates(HEVCContext *s, int x0, int y0,
288
                                            int nPbW, int nPbH,
289
                                            int log2_cb_size,
290
                                            int singleMCLFlag, int part_idx,
291
                                            int merge_idx,
292
                                            struct MvField mergecandlist[])
293
0
{
294
0
    HEVCLocalContext *lc   = s->HEVClc;
295
0
    RefPicList *refPicList = s->ref->refPicList;
296
0
    MvField *tab_mvf       = s->ref->tab_mvf;
297
298
0
    const int min_pu_width = s->sps->min_pu_width;
299
300
0
    const int cand_bottom_left = lc->na.cand_bottom_left;
301
0
    const int cand_left        = lc->na.cand_left;
302
0
    const int cand_up_left     = lc->na.cand_up_left;
303
0
    const int cand_up          = lc->na.cand_up;
304
0
    const int cand_up_right    = lc->na.cand_up_right_sap;
305
306
0
    const int xA1    = x0 - 1;
307
0
    const int yA1    = y0 + nPbH - 1;
308
309
0
    const int xB1    = x0 + nPbW - 1;
310
0
    const int yB1    = y0 - 1;
311
312
0
    const int xB0    = x0 + nPbW;
313
0
    const int yB0    = y0 - 1;
314
315
0
    const int xA0    = x0 - 1;
316
0
    const int yA0    = y0 + nPbH;
317
318
0
    const int xB2    = x0 - 1;
319
0
    const int yB2    = y0 - 1;
320
321
0
    const int nb_refs = (s->sh.slice_type == P_SLICE) ?
322
0
                        s->sh.nb_refs[0] : FFMIN(s->sh.nb_refs[0], s->sh.nb_refs[1]);
323
324
0
    int zero_idx = 0;
325
326
0
    int nb_merge_cand = 0;
327
0
    int nb_orig_merge_cand = 0;
328
329
0
    int is_available_a0;
330
0
    int is_available_a1;
331
0
    int is_available_b0;
332
0
    int is_available_b1;
333
0
    int is_available_b2;
334
335
336
0
    if (!singleMCLFlag && part_idx == 1 &&
337
0
        (lc->cu.part_mode == PART_Nx2N ||
338
0
         lc->cu.part_mode == PART_nLx2N ||
339
0
         lc->cu.part_mode == PART_nRx2N) ||
340
0
        is_diff_mer(s, xA1, yA1, x0, y0)) {
341
0
        is_available_a1 = 0;
342
0
    } else {
343
0
        is_available_a1 = AVAILABLE(cand_left, A1);
344
0
        if (is_available_a1) {
345
0
            mergecandlist[nb_merge_cand] = TAB_MVF_PU(A1);
346
0
            if (merge_idx == 0)
347
0
                return;
348
0
            nb_merge_cand++;
349
0
        }
350
0
    }
351
352
0
    if (!singleMCLFlag && part_idx == 1 &&
353
0
        (lc->cu.part_mode == PART_2NxN ||
354
0
         lc->cu.part_mode == PART_2NxnU ||
355
0
         lc->cu.part_mode == PART_2NxnD) ||
356
0
        is_diff_mer(s, xB1, yB1, x0, y0)) {
357
0
        is_available_b1 = 0;
358
0
    } else {
359
0
        is_available_b1 = AVAILABLE(cand_up, B1);
360
0
        if (is_available_b1 &&
361
0
            !(is_available_a1 && COMPARE_MV_REFIDX(B1, A1))) {
362
0
            mergecandlist[nb_merge_cand] = TAB_MVF_PU(B1);
363
0
            if (merge_idx == nb_merge_cand)
364
0
                return;
365
0
            nb_merge_cand++;
366
0
        }
367
0
    }
368
369
    // above right spatial merge candidate
370
0
    is_available_b0 = AVAILABLE(cand_up_right, B0) &&
371
0
                      xB0 < s->sps->width &&
372
0
                      PRED_BLOCK_AVAILABLE(B0) &&
373
0
                      !is_diff_mer(s, xB0, yB0, x0, y0);
374
375
0
    if (is_available_b0 &&
376
0
        !(is_available_b1 && COMPARE_MV_REFIDX(B0, B1))) {
377
0
        mergecandlist[nb_merge_cand] = TAB_MVF_PU(B0);
378
0
        if (merge_idx == nb_merge_cand)
379
0
            return;
380
0
        nb_merge_cand++;
381
0
    }
382
383
    // left bottom spatial merge candidate
384
0
    is_available_a0 = AVAILABLE(cand_bottom_left, A0) &&
385
0
                      yA0 < s->sps->height &&
386
0
                      PRED_BLOCK_AVAILABLE(A0) &&
387
0
                      !is_diff_mer(s, xA0, yA0, x0, y0);
388
389
0
    if (is_available_a0 &&
390
0
        !(is_available_a1 && COMPARE_MV_REFIDX(A0, A1))) {
391
0
        mergecandlist[nb_merge_cand] = TAB_MVF_PU(A0);
392
0
        if (merge_idx == nb_merge_cand)
393
0
            return;
394
0
        nb_merge_cand++;
395
0
    }
396
397
    // above left spatial merge candidate
398
0
    is_available_b2 = AVAILABLE(cand_up_left, B2) &&
399
0
                      !is_diff_mer(s, xB2, yB2, x0, y0);
400
401
0
    if (is_available_b2 &&
402
0
        !(is_available_a1 && COMPARE_MV_REFIDX(B2, A1)) &&
403
0
        !(is_available_b1 && COMPARE_MV_REFIDX(B2, B1)) &&
404
0
        nb_merge_cand != 4) {
405
0
        mergecandlist[nb_merge_cand] = TAB_MVF_PU(B2);
406
0
        if (merge_idx == nb_merge_cand)
407
0
            return;
408
0
        nb_merge_cand++;
409
0
    }
410
411
    // temporal motion vector candidate
412
0
    if (s->sh.slice_temporal_mvp_enabled_flag &&
413
0
        nb_merge_cand < s->sh.max_num_merge_cand) {
414
0
        Mv mv_l0_col = { 0 }, mv_l1_col = { 0 };
415
0
        int available_l0 = temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
416
0
                                                       0, &mv_l0_col, 0);
417
0
        int available_l1 = (s->sh.slice_type == B_SLICE) ?
418
0
                           temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
419
0
                                                       0, &mv_l1_col, 1) : 0;
420
421
0
        if (available_l0 || available_l1) {
422
0
            mergecandlist[nb_merge_cand].pred_flag = available_l0 + (available_l1 << 1);
423
0
            AV_ZERO16(mergecandlist[nb_merge_cand].ref_idx);
424
0
            mergecandlist[nb_merge_cand].mv[0]      = mv_l0_col;
425
0
            mergecandlist[nb_merge_cand].mv[1]      = mv_l1_col;
426
427
0
            if (merge_idx == nb_merge_cand)
428
0
                return;
429
0
            nb_merge_cand++;
430
0
        }
431
0
    }
432
433
0
    nb_orig_merge_cand = nb_merge_cand;
434
435
    // combined bi-predictive merge candidates  (applies for B slices)
436
0
    if (s->sh.slice_type == B_SLICE && nb_orig_merge_cand > 1 &&
437
0
        nb_orig_merge_cand < s->sh.max_num_merge_cand) {
438
0
        int comb_idx = 0;
439
440
0
        for (comb_idx = 0; nb_merge_cand < s->sh.max_num_merge_cand &&
441
0
                           comb_idx < nb_orig_merge_cand * (nb_orig_merge_cand - 1); comb_idx++) {
442
0
            int l0_cand_idx = l0_l1_cand_idx[comb_idx][0];
443
0
            int l1_cand_idx = l0_l1_cand_idx[comb_idx][1];
444
0
            MvField l0_cand = mergecandlist[l0_cand_idx];
445
0
            MvField l1_cand = mergecandlist[l1_cand_idx];
446
447
0
            if ((l0_cand.pred_flag & PF_L0) && (l1_cand.pred_flag & PF_L1) &&
448
0
                (refPicList[0].list[l0_cand.ref_idx[0]] !=
449
0
                 refPicList[1].list[l1_cand.ref_idx[1]] ||
450
0
                 AV_RN32A(&l0_cand.mv[0]) != AV_RN32A(&l1_cand.mv[1]))) {
451
0
                mergecandlist[nb_merge_cand].ref_idx[0]   = l0_cand.ref_idx[0];
452
0
                mergecandlist[nb_merge_cand].ref_idx[1]   = l1_cand.ref_idx[1];
453
0
                mergecandlist[nb_merge_cand].pred_flag    = PF_BI;
454
0
                AV_COPY32(&mergecandlist[nb_merge_cand].mv[0], &l0_cand.mv[0]);
455
0
                AV_COPY32(&mergecandlist[nb_merge_cand].mv[1], &l1_cand.mv[1]);
456
0
                if (merge_idx == nb_merge_cand)
457
0
                    return;
458
0
                nb_merge_cand++;
459
0
            }
460
0
        }
461
0
    }
462
463
    // append Zero motion vector candidates
464
0
    while (nb_merge_cand < s->sh.max_num_merge_cand) {
465
0
        mergecandlist[nb_merge_cand].pred_flag    = PF_L0 + ((s->sh.slice_type == B_SLICE) << 1);
466
0
        AV_ZERO32(mergecandlist[nb_merge_cand].mv + 0);
467
0
        AV_ZERO32(mergecandlist[nb_merge_cand].mv + 1);
468
0
        mergecandlist[nb_merge_cand].ref_idx[0]   = zero_idx < nb_refs ? zero_idx : 0;
469
0
        mergecandlist[nb_merge_cand].ref_idx[1]   = zero_idx < nb_refs ? zero_idx : 0;
470
471
0
        if (merge_idx == nb_merge_cand)
472
0
            return;
473
0
        nb_merge_cand++;
474
0
        zero_idx++;
475
0
    }
476
0
}
477
478
/*
479
 * 8.5.3.1.1 Derivation process of luma Mvs for merge mode
480
 */
481
void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0, int nPbW,
482
                                int nPbH, int log2_cb_size, int part_idx,
483
                                int merge_idx, MvField *mv)
484
0
{
485
0
    int singleMCLFlag = 0;
486
0
    int nCS = 1 << log2_cb_size;
487
0
    LOCAL_ALIGNED(4, MvField, mergecand_list, [MRG_MAX_NUM_CANDS]);
488
0
    int nPbW2 = nPbW;
489
0
    int nPbH2 = nPbH;
490
0
    HEVCLocalContext *lc = s->HEVClc;
491
492
0
    if (s->pps->log2_parallel_merge_level > 2 && nCS == 8) {
493
0
        singleMCLFlag = 1;
494
0
        x0            = lc->cu.x;
495
0
        y0            = lc->cu.y;
496
0
        nPbW          = nCS;
497
0
        nPbH          = nCS;
498
0
        part_idx      = 0;
499
0
    }
500
501
0
    ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
502
0
    derive_spatial_merge_candidates(s, x0, y0, nPbW, nPbH, log2_cb_size,
503
0
                                    singleMCLFlag, part_idx,
504
0
                                    merge_idx, mergecand_list);
505
506
0
    if (mergecand_list[merge_idx].pred_flag == PF_BI &&
507
0
        (nPbW2 + nPbH2) == 12) {
508
0
        mergecand_list[merge_idx].pred_flag = PF_L0;
509
0
    }
510
511
0
    *mv = mergecand_list[merge_idx];
512
0
}
513
514
static av_always_inline void dist_scale(HEVCContext *s, Mv *mv,
515
                                        int min_pu_width, int x, int y,
516
                                        int elist, int ref_idx_curr, int ref_idx)
517
0
{
518
0
    RefPicList *refPicList = s->ref->refPicList;
519
0
    MvField *tab_mvf       = s->ref->tab_mvf;
520
0
    int ref_pic_elist      = refPicList[elist].list[TAB_MVF(x, y).ref_idx[elist]];
521
0
    int ref_pic_curr       = refPicList[ref_idx_curr].list[ref_idx];
522
523
0
    if (ref_pic_elist != ref_pic_curr) {
524
0
        int poc_diff = s->poc - ref_pic_elist;
525
0
        if (!poc_diff)
526
0
            poc_diff = 1;
527
0
        mv_scale(mv, mv, poc_diff, s->poc - ref_pic_curr);
528
0
    }
529
0
}
530
531
static int mv_mp_mode_mx(HEVCContext *s, int x, int y, int pred_flag_index,
532
                         Mv *mv, int ref_idx_curr, int ref_idx)
533
0
{
534
0
    MvField *tab_mvf = s->ref->tab_mvf;
535
0
    int min_pu_width = s->sps->min_pu_width;
536
537
0
    RefPicList *refPicList = s->ref->refPicList;
538
539
0
    if (((TAB_MVF(x, y).pred_flag) & (1 << pred_flag_index)) &&
540
0
        refPicList[pred_flag_index].list[TAB_MVF(x, y).ref_idx[pred_flag_index]] == refPicList[ref_idx_curr].list[ref_idx]) {
541
0
        *mv = TAB_MVF(x, y).mv[pred_flag_index];
542
0
        return 1;
543
0
    }
544
0
    return 0;
545
0
}
546
547
static int mv_mp_mode_mx_lt(HEVCContext *s, int x, int y, int pred_flag_index,
548
                            Mv *mv, int ref_idx_curr, int ref_idx)
549
0
{
550
0
    MvField *tab_mvf = s->ref->tab_mvf;
551
0
    int min_pu_width = s->sps->min_pu_width;
552
553
0
    RefPicList *refPicList = s->ref->refPicList;
554
555
0
    if ((TAB_MVF(x, y).pred_flag) & (1 << pred_flag_index)) {
556
0
        int currIsLongTerm     = refPicList[ref_idx_curr].isLongTerm[ref_idx];
557
558
0
        int colIsLongTerm =
559
0
            refPicList[pred_flag_index].isLongTerm[(TAB_MVF(x, y).ref_idx[pred_flag_index])];
560
561
0
        if (colIsLongTerm == currIsLongTerm) {
562
0
            *mv = TAB_MVF(x, y).mv[pred_flag_index];
563
0
            if (!currIsLongTerm)
564
0
                dist_scale(s, mv, min_pu_width, x, y,
565
0
                           pred_flag_index, ref_idx_curr, ref_idx);
566
0
            return 1;
567
0
        }
568
0
    }
569
0
    return 0;
570
0
}
571
572
#define MP_MX(v, pred, mx)                                      \
573
0
    mv_mp_mode_mx(s,                                            \
574
0
                  (x ## v) >> s->sps->log2_min_pu_size,         \
575
0
                  (y ## v) >> s->sps->log2_min_pu_size,         \
576
0
                  pred, &mx, ref_idx_curr, ref_idx)
577
578
#define MP_MX_LT(v, pred, mx)                                   \
579
0
    mv_mp_mode_mx_lt(s,                                         \
580
0
                     (x ## v) >> s->sps->log2_min_pu_size,      \
581
0
                     (y ## v) >> s->sps->log2_min_pu_size,      \
582
0
                     pred, &mx, ref_idx_curr, ref_idx)
583
584
void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
585
                              int nPbH, int log2_cb_size, int part_idx,
586
                              int merge_idx, MvField *mv,
587
                              int mvp_lx_flag, int LX)
588
0
{
589
0
    HEVCLocalContext *lc = s->HEVClc;
590
0
    MvField *tab_mvf = s->ref->tab_mvf;
591
0
    int isScaledFlag_L0 = 0;
592
0
    int availableFlagLXA0 = 1;
593
0
    int availableFlagLXB0 = 1;
594
0
    int numMVPCandLX = 0;
595
0
    int min_pu_width = s->sps->min_pu_width;
596
597
0
    int xA0, yA0;
598
0
    int is_available_a0;
599
0
    int xA1, yA1;
600
0
    int is_available_a1;
601
0
    int xB0, yB0;
602
0
    int is_available_b0;
603
0
    int xB1, yB1;
604
0
    int is_available_b1;
605
0
    int xB2, yB2;
606
0
    int is_available_b2;
607
608
0
    Mv mvpcand_list[2] = { { 0 } };
609
0
    Mv mxA;
610
0
    Mv mxB;
611
0
    int ref_idx_curr;
612
0
    int ref_idx = 0;
613
0
    int pred_flag_index_l0;
614
0
    int pred_flag_index_l1;
615
616
0
    const int cand_bottom_left = lc->na.cand_bottom_left;
617
0
    const int cand_left        = lc->na.cand_left;
618
0
    const int cand_up_left     = lc->na.cand_up_left;
619
0
    const int cand_up          = lc->na.cand_up;
620
0
    const int cand_up_right    = lc->na.cand_up_right_sap;
621
0
    ref_idx_curr       = LX;
622
0
    ref_idx            = mv->ref_idx[LX];
623
0
    pred_flag_index_l0 = LX;
624
0
    pred_flag_index_l1 = !LX;
625
626
    // left bottom spatial candidate
627
0
    xA0 = x0 - 1;
628
0
    yA0 = y0 + nPbH;
629
630
0
    is_available_a0 = AVAILABLE(cand_bottom_left, A0) &&
631
0
                      yA0 < s->sps->height &&
632
0
                      PRED_BLOCK_AVAILABLE(A0);
633
634
    //left spatial merge candidate
635
0
    xA1    = x0 - 1;
636
0
    yA1    = y0 + nPbH - 1;
637
638
0
    is_available_a1 = AVAILABLE(cand_left, A1);
639
0
    if (is_available_a0 || is_available_a1)
640
0
        isScaledFlag_L0 = 1;
641
642
0
    if (is_available_a0) {
643
0
        if (MP_MX(A0, pred_flag_index_l0, mxA)) {
644
0
            goto b_candidates;
645
0
        }
646
0
        if (MP_MX(A0, pred_flag_index_l1, mxA)) {
647
0
            goto b_candidates;
648
0
        }
649
0
    }
650
651
0
    if (is_available_a1) {
652
0
        if (MP_MX(A1, pred_flag_index_l0, mxA)) {
653
0
            goto b_candidates;
654
0
        }
655
0
        if (MP_MX(A1, pred_flag_index_l1, mxA)) {
656
0
            goto b_candidates;
657
0
        }
658
0
    }
659
660
0
    if (is_available_a0) {
661
0
        if (MP_MX_LT(A0, pred_flag_index_l0, mxA)) {
662
0
            goto b_candidates;
663
0
        }
664
0
        if (MP_MX_LT(A0, pred_flag_index_l1, mxA)) {
665
0
            goto b_candidates;
666
0
        }
667
0
    }
668
669
0
    if (is_available_a1) {
670
0
        if (MP_MX_LT(A1, pred_flag_index_l0, mxA)) {
671
0
            goto b_candidates;
672
0
        }
673
0
        if (MP_MX_LT(A1, pred_flag_index_l1, mxA)) {
674
0
            goto b_candidates;
675
0
        }
676
0
    }
677
0
    availableFlagLXA0 = 0;
678
679
0
b_candidates:
680
    // B candidates
681
    // above right spatial merge candidate
682
0
    xB0    = x0 + nPbW;
683
0
    yB0    = y0 - 1;
684
685
0
    is_available_b0 =  AVAILABLE(cand_up_right, B0) &&
686
0
                       xB0 < s->sps->width &&
687
0
                       PRED_BLOCK_AVAILABLE(B0);
688
689
    // above spatial merge candidate
690
0
    xB1    = x0 + nPbW - 1;
691
0
    yB1    = y0 - 1;
692
0
    is_available_b1 = AVAILABLE(cand_up, B1);
693
694
    // above left spatial merge candidate
695
0
    xB2 = x0 - 1;
696
0
    yB2 = y0 - 1;
697
0
    is_available_b2 = AVAILABLE(cand_up_left, B2);
698
699
    // above right spatial merge candidate
700
0
    if (is_available_b0) {
701
0
        if (MP_MX(B0, pred_flag_index_l0, mxB)) {
702
0
            goto scalef;
703
0
        }
704
0
        if (MP_MX(B0, pred_flag_index_l1, mxB)) {
705
0
            goto scalef;
706
0
        }
707
0
    }
708
709
    // above spatial merge candidate
710
0
    if (is_available_b1) {
711
0
        if (MP_MX(B1, pred_flag_index_l0, mxB)) {
712
0
            goto scalef;
713
0
        }
714
0
        if (MP_MX(B1, pred_flag_index_l1, mxB)) {
715
0
            goto scalef;
716
0
        }
717
0
    }
718
719
    // above left spatial merge candidate
720
0
    if (is_available_b2) {
721
0
        if (MP_MX(B2, pred_flag_index_l0, mxB)) {
722
0
            goto scalef;
723
0
        }
724
0
        if (MP_MX(B2, pred_flag_index_l1, mxB)) {
725
0
            goto scalef;
726
0
        }
727
0
    }
728
0
    availableFlagLXB0 = 0;
729
730
0
scalef:
731
0
    if (!isScaledFlag_L0) {
732
0
        if (availableFlagLXB0) {
733
0
            availableFlagLXA0 = 1;
734
0
            mxA = mxB;
735
0
        }
736
0
        availableFlagLXB0 = 0;
737
738
        // XB0 and L1
739
0
        if (is_available_b0) {
740
0
            availableFlagLXB0 = MP_MX_LT(B0, pred_flag_index_l0, mxB);
741
0
            if (!availableFlagLXB0)
742
0
                availableFlagLXB0 = MP_MX_LT(B0, pred_flag_index_l1, mxB);
743
0
        }
744
745
0
        if (is_available_b1 && !availableFlagLXB0) {
746
0
            availableFlagLXB0 = MP_MX_LT(B1, pred_flag_index_l0, mxB);
747
0
            if (!availableFlagLXB0)
748
0
                availableFlagLXB0 = MP_MX_LT(B1, pred_flag_index_l1, mxB);
749
0
        }
750
751
0
        if (is_available_b2 && !availableFlagLXB0) {
752
0
            availableFlagLXB0 = MP_MX_LT(B2, pred_flag_index_l0, mxB);
753
0
            if (!availableFlagLXB0)
754
0
                availableFlagLXB0 = MP_MX_LT(B2, pred_flag_index_l1, mxB);
755
0
        }
756
0
    }
757
758
0
    if (availableFlagLXA0)
759
0
        mvpcand_list[numMVPCandLX++] = mxA;
760
761
0
    if (availableFlagLXB0 && (!availableFlagLXA0 || mxA.x != mxB.x || mxA.y != mxB.y))
762
0
        mvpcand_list[numMVPCandLX++] = mxB;
763
764
    //temporal motion vector prediction candidate
765
0
    if (numMVPCandLX < 2 && s->sh.slice_temporal_mvp_enabled_flag &&
766
0
        mvp_lx_flag == numMVPCandLX) {
767
0
        Mv mv_col;
768
0
        int available_col = temporal_luma_motion_vector(s, x0, y0, nPbW,
769
0
                                                        nPbH, ref_idx,
770
0
                                                        &mv_col, LX);
771
0
        if (available_col)
772
0
            mvpcand_list[numMVPCandLX++] = mv_col;
773
0
    }
774
775
0
    mv->mv[LX] = mvpcand_list[mvp_lx_flag];
776
0
}
777
#endif