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