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