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