4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2013 Anand Meher Kotra
7 * This file is part of FFmpeg.
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.
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.
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
26 static const uint8_t l0_l1_cand_idx[12][2] = {
41 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
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);
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;
62 * 6.4.1 Derivation process for z-scan order block availability
64 static int z_scan_block_avail(HEVCContext *s, int xCurr, int yCurr,
67 #define MIN_TB_ADDR_ZS(x, y) \
68 s->pps->min_tb_addr_zs[(y) * (s->sps->tb_mask+2) + (x)]
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;
75 if (xN < 0 || yN < 0 ||
76 xN >= s->sps->width ||
80 if( yN_ctb < yCurr_ctb || xN_ctb < xCurr_ctb )
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);
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)
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);
102 * 6.4.2 Derivation process for prediction block availability
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)
108 HEVCLocalContext *lc = s->HEVClc;
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);
116 return z_scan_block_avail(s, x0, y0, xA1, yA1);
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)
122 uint8_t plevel = s->pps->log2_parallel_merge_level;
124 return xN >> plevel == xP >> plevel &&
125 yN >> plevel == yP >> plevel;
128 #define MATCH(x) (A.x == B.x)
130 // check if the mv's and refidx are the same between A and B
131 static int compareMVrefidx(struct MvField A, struct MvField B)
133 int a_pf = A.pred_flag;
134 int b_pf = B.pred_flag;
137 return MATCH(ref_idx[0]) && MATCH(mv[0].x) && MATCH(mv[0].y) &&
138 MATCH(ref_idx[1]) && MATCH(mv[1].x) && MATCH(mv[1].y);
139 } else if (a_pf == PF_L0) {
140 return MATCH(ref_idx[0]) && MATCH(mv[0].x) && MATCH(mv[0].y);
141 } else if (a_pf == PF_L1) {
142 return MATCH(ref_idx[1]) && MATCH(mv[1].x) && MATCH(mv[1].y);
148 static av_always_inline void mv_scale(Mv *dst, Mv *src, int td, int tb)
150 int tx, scale_factor;
152 td = av_clip_int8(td);
153 tb = av_clip_int8(tb);
154 tx = (0x4000 + abs(td / 2)) / td;
155 scale_factor = av_clip((tb * tx + 32) >> 6, -4096, 4095);
156 dst->x = av_clip_int16((scale_factor * src->x + 127 +
157 (scale_factor * src->x < 0)) >> 8);
158 dst->y = av_clip_int16((scale_factor * src->y + 127 +
159 (scale_factor * src->y < 0)) >> 8);
162 static int check_mvset(Mv *mvLXCol, Mv *mvCol,
164 RefPicList *refPicList, int X, int refIdxLx,
165 RefPicList *refPicList_col, int listCol, int refidxCol)
167 int cur_lt = refPicList[X].isLongTerm[refIdxLx];
168 int col_lt = refPicList_col[listCol].isLongTerm[refidxCol];
169 int col_poc_diff, cur_poc_diff;
171 if (cur_lt != col_lt) {
177 col_poc_diff = colPic - refPicList_col[listCol].list[refidxCol];
178 cur_poc_diff = poc - refPicList[X].list[refIdxLx];
180 if (cur_lt || col_poc_diff == cur_poc_diff || !col_poc_diff) {
181 mvLXCol->x = mvCol->x;
182 mvLXCol->y = mvCol->y;
184 mv_scale(mvLXCol, mvCol, col_poc_diff, cur_poc_diff);
189 #define CHECK_MVSET(l) \
190 check_mvset(mvLXCol, temp_col.mv + l, \
192 refPicList, X, refIdxLx, \
193 refPicList_col, L ## l, temp_col.ref_idx[l])
195 // derive the motion vectors section 8.5.3.1.8
196 static int derive_temporal_colocated_mvs(HEVCContext *s, MvField temp_col,
197 int refIdxLx, Mv *mvLXCol, int X,
198 int colPic, RefPicList *refPicList_col)
200 RefPicList *refPicList = s->ref->refPicList;
202 if (temp_col.pred_flag == PF_INTRA)
205 if (!(temp_col.pred_flag & PF_L0))
206 return CHECK_MVSET(1);
207 else if (temp_col.pred_flag == PF_L0)
208 return CHECK_MVSET(0);
209 else if (temp_col.pred_flag == PF_BI) {
210 int check_diffpicount = 0;
212 for (i = 0; i < refPicList[0].nb_refs; i++) {
213 if (refPicList[0].list[i] > s->poc)
216 for (i = 0; i < refPicList[1].nb_refs; i++) {
217 if (refPicList[1].list[i] > s->poc)
220 if (check_diffpicount == 0 && X == 0)
221 return CHECK_MVSET(0);
222 else if (check_diffpicount == 0 && X == 1)
223 return CHECK_MVSET(1);
225 if (s->sh.collocated_list == L1)
226 return CHECK_MVSET(0);
228 return CHECK_MVSET(1);
235 #define TAB_MVF(x, y) \
236 tab_mvf[(y) * min_pu_width + x]
238 #define TAB_MVF_PU(v) \
239 TAB_MVF(x ## v ## _pu, y ## v ## _pu)
241 #define DERIVE_TEMPORAL_COLOCATED_MVS \
242 derive_temporal_colocated_mvs(s, temp_col, \
243 refIdxLx, mvLXCol, X, colPic, \
244 ff_hevc_get_ref_list(s, ref, x, y))
247 * 8.5.3.1.7 temporal luma motion vector prediction
249 static int temporal_luma_motion_vector(HEVCContext *s, int x0, int y0,
250 int nPbW, int nPbH, int refIdxLx,
255 int x, y, x_pu, y_pu;
256 int min_pu_width = s->sps->min_pu_width;
257 int availableFlagLXCol = 0;
260 HEVCFrame *ref = s->ref->collocated_ref;
265 tab_mvf = ref->tab_mvf;
268 //bottom right collocated motion vector
272 if (s->threads_type == FF_THREAD_FRAME )
273 ff_thread_await_progress(&ref->tf, y, 0);
275 (y0 >> s->sps->log2_ctb_size) == (y >> s->sps->log2_ctb_size) &&
276 y < s->sps->height &&
280 x_pu = x >> s->sps->log2_min_pu_size;
281 y_pu = y >> s->sps->log2_min_pu_size;
282 temp_col = TAB_MVF(x_pu, y_pu);
283 availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS;
286 // derive center collocated motion vector
287 if (tab_mvf && !availableFlagLXCol) {
288 x = x0 + (nPbW >> 1);
289 y = y0 + (nPbH >> 1);
292 x_pu = x >> s->sps->log2_min_pu_size;
293 y_pu = y >> s->sps->log2_min_pu_size;
294 temp_col = TAB_MVF(x_pu, y_pu);
295 availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS;
297 return availableFlagLXCol;
300 #define AVAILABLE(cand, v) \
301 (cand && !(TAB_MVF_PU(v).pred_flag == PF_INTRA))
303 #define PRED_BLOCK_AVAILABLE(v) \
304 check_prediction_block_available(s, log2_cb_size, \
305 x0, y0, nPbW, nPbH, \
306 x ## v, y ## v, part_idx)
308 #define COMPARE_MV_REFIDX(a, b) \
309 compareMVrefidx(TAB_MVF_PU(a), TAB_MVF_PU(b))
312 * 8.5.3.1.2 Derivation process for spatial merging candidates
314 static void derive_spatial_merge_candidates(HEVCContext *s, int x0, int y0,
317 int singleMCLFlag, int part_idx,
318 struct MvField mergecandlist[])
320 HEVCLocalContext *lc = s->HEVClc;
321 RefPicList *refPicList = s->ref->refPicList;
322 MvField *tab_mvf = s->ref->tab_mvf;
324 const int min_pu_width = s->sps->min_pu_width;
326 const int cand_bottom_left = lc->na.cand_bottom_left;
327 const int cand_left = lc->na.cand_left;
328 const int cand_up_left = lc->na.cand_up_left;
329 const int cand_up = lc->na.cand_up;
330 const int cand_up_right = lc->na.cand_up_right_sap;
332 const int xA1 = x0 - 1;
333 const int yA1 = y0 + nPbH - 1;
334 const int xA1_pu = xA1 >> s->sps->log2_min_pu_size;
335 const int yA1_pu = yA1 >> s->sps->log2_min_pu_size;
337 const int xB1 = x0 + nPbW - 1;
338 const int yB1 = y0 - 1;
339 const int xB1_pu = xB1 >> s->sps->log2_min_pu_size;
340 const int yB1_pu = yB1 >> s->sps->log2_min_pu_size;
342 const int xB0 = x0 + nPbW;
343 const int yB0 = y0 - 1;
344 const int xB0_pu = xB0 >> s->sps->log2_min_pu_size;
345 const int yB0_pu = yB0 >> s->sps->log2_min_pu_size;
347 const int xA0 = x0 - 1;
348 const int yA0 = y0 + nPbH;
349 const int xA0_pu = xA0 >> s->sps->log2_min_pu_size;
350 const int yA0_pu = yA0 >> s->sps->log2_min_pu_size;
352 const int xB2 = x0 - 1;
353 const int yB2 = y0 - 1;
354 const int xB2_pu = xB2 >> s->sps->log2_min_pu_size;
355 const int yB2_pu = yB2 >> s->sps->log2_min_pu_size;
357 const int nb_refs = (s->sh.slice_type == P_SLICE) ?
358 s->sh.nb_refs[0] : FFMIN(s->sh.nb_refs[0], s->sh.nb_refs[1]);
364 int nb_merge_cand = 0;
365 int nb_orig_merge_cand = 0;
375 //first left spatial merge candidate
376 is_available_a1 = AVAILABLE(cand_left, A1);
378 if (!singleMCLFlag && part_idx == 1 &&
379 (lc->cu.part_mode == PART_Nx2N ||
380 lc->cu.part_mode == PART_nLx2N ||
381 lc->cu.part_mode == PART_nRx2N) ||
382 isDiffMER(s, xA1, yA1, x0, y0)) {
387 mergecandlist[nb_merge_cand++] = TAB_MVF_PU(A1);
389 // above spatial merge candidate
390 is_available_b1 = AVAILABLE(cand_up, B1);
392 if (!singleMCLFlag && part_idx == 1 &&
393 (lc->cu.part_mode == PART_2NxN ||
394 lc->cu.part_mode == PART_2NxnU ||
395 lc->cu.part_mode == PART_2NxnD) ||
396 isDiffMER(s, xB1, yB1, x0, y0)) {
400 if (is_available_a1 && is_available_b1)
401 check_MER = !COMPARE_MV_REFIDX(B1, A1);
403 if (is_available_b1 && check_MER)
404 mergecandlist[nb_merge_cand++] = TAB_MVF_PU(B1);
406 // above right spatial merge candidate
408 check_B0 = PRED_BLOCK_AVAILABLE(B0);
410 is_available_b0 = check_B0 && AVAILABLE(cand_up_right, B0);
412 if (isDiffMER(s, xB0, yB0, x0, y0))
415 if (is_available_b1 && is_available_b0)
416 check_MER = !COMPARE_MV_REFIDX(B0, B1);
418 if (is_available_b0 && check_MER)
419 mergecandlist[nb_merge_cand++] = TAB_MVF_PU(B0);
421 // left bottom spatial merge candidate
423 check_A0 = PRED_BLOCK_AVAILABLE(A0);
425 is_available_a0 = check_A0 && AVAILABLE(cand_bottom_left, A0);
427 if (isDiffMER(s, xA0, yA0, x0, y0))
430 if (is_available_a1 && is_available_a0)
431 check_MER = !COMPARE_MV_REFIDX(A0, A1);
433 if (is_available_a0 && check_MER)
434 mergecandlist[nb_merge_cand++] = TAB_MVF_PU(A0);
436 // above left spatial merge candidate
439 is_available_b2 = AVAILABLE(cand_up_left, B2);
441 if (isDiffMER(s, xB2, yB2, x0, y0))
444 if (is_available_a1 && is_available_b2)
445 check_MER = !COMPARE_MV_REFIDX(B2, A1);
447 if (is_available_b1 && is_available_b2)
448 check_MER_1 = !COMPARE_MV_REFIDX(B2, B1);
450 if (is_available_b2 && check_MER && check_MER_1 && nb_merge_cand != 4)
451 mergecandlist[nb_merge_cand++] = TAB_MVF_PU(B2);
453 // temporal motion vector candidate
454 if (s->sh.slice_temporal_mvp_enabled_flag &&
455 nb_merge_cand < s->sh.max_num_merge_cand) {
456 Mv mv_l0_col, mv_l1_col;
457 int available_l0 = temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
459 int available_l1 = (s->sh.slice_type == B_SLICE) ?
460 temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
461 0, &mv_l1_col, 1) : 0;
463 if (available_l0 || available_l1) {
464 mergecandlist[nb_merge_cand].pred_flag = available_l0 + (available_l1 << 1);
466 mergecandlist[nb_merge_cand].mv[0] = mv_l0_col;
467 mergecandlist[nb_merge_cand].ref_idx[0] = 0;
470 mergecandlist[nb_merge_cand].mv[1] = mv_l1_col;
471 mergecandlist[nb_merge_cand].ref_idx[1] = 0;
477 nb_orig_merge_cand = nb_merge_cand;
479 // combined bi-predictive merge candidates (applies for B slices)
480 if (s->sh.slice_type == B_SLICE && nb_orig_merge_cand > 1 &&
481 nb_orig_merge_cand < s->sh.max_num_merge_cand) {
484 for (comb_idx = 0; nb_merge_cand < s->sh.max_num_merge_cand &&
485 comb_idx < nb_orig_merge_cand * (nb_orig_merge_cand - 1); comb_idx++) {
486 int l0_cand_idx = l0_l1_cand_idx[comb_idx][0];
487 int l1_cand_idx = l0_l1_cand_idx[comb_idx][1];
488 MvField l0_cand = mergecandlist[l0_cand_idx];
489 MvField l1_cand = mergecandlist[l1_cand_idx];
491 if ((l0_cand.pred_flag & PF_L0) && (l1_cand.pred_flag & PF_L1) &&
492 (refPicList[0].list[l0_cand.ref_idx[0]] !=
493 refPicList[1].list[l1_cand.ref_idx[1]] ||
494 l0_cand.mv[0].x != l1_cand.mv[1].x ||
495 l0_cand.mv[0].y != l1_cand.mv[1].y)) {
496 mergecandlist[nb_merge_cand].ref_idx[0] = l0_cand.ref_idx[0];
497 mergecandlist[nb_merge_cand].ref_idx[1] = l1_cand.ref_idx[1];
498 mergecandlist[nb_merge_cand].pred_flag = PF_BI;
499 mergecandlist[nb_merge_cand].mv[0].x = l0_cand.mv[0].x;
500 mergecandlist[nb_merge_cand].mv[0].y = l0_cand.mv[0].y;
501 mergecandlist[nb_merge_cand].mv[1].x = l1_cand.mv[1].x;
502 mergecandlist[nb_merge_cand].mv[1].y = l1_cand.mv[1].y;
508 // append Zero motion vector candidates
509 while (nb_merge_cand < s->sh.max_num_merge_cand) {
510 mergecandlist[nb_merge_cand].pred_flag = PF_L0 + ((s->sh.slice_type == B_SLICE) << 1);
511 mergecandlist[nb_merge_cand].mv[0].x = 0;
512 mergecandlist[nb_merge_cand].mv[0].y = 0;
513 mergecandlist[nb_merge_cand].mv[1].x = 0;
514 mergecandlist[nb_merge_cand].mv[1].y = 0;
515 mergecandlist[nb_merge_cand].ref_idx[0] = zero_idx < nb_refs ? zero_idx : 0;
516 mergecandlist[nb_merge_cand].ref_idx[1] = zero_idx < nb_refs ? zero_idx : 0;
524 * 8.5.3.1.1 Derivation process of luma Mvs for merge mode
526 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0, int nPbW,
527 int nPbH, int log2_cb_size, int part_idx,
528 int merge_idx, MvField *mv)
530 int singleMCLFlag = 0;
531 int nCS = 1 << log2_cb_size;
532 struct MvField mergecand_list[MRG_MAX_NUM_CANDS] = { { { { 0 } } } };
535 HEVCLocalContext *lc = s->HEVClc;
537 if (s->pps->log2_parallel_merge_level > 2 && nCS == 8) {
546 ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
547 derive_spatial_merge_candidates(s, x0, y0, nPbW, nPbH, log2_cb_size,
548 singleMCLFlag, part_idx, mergecand_list);
550 if (mergecand_list[merge_idx].pred_flag == PF_BI &&
551 (nPbW2 + nPbH2) == 12) {
552 mergecand_list[merge_idx].pred_flag = PF_L0;
555 *mv = mergecand_list[merge_idx];
558 static av_always_inline void dist_scale(HEVCContext *s, Mv *mv,
559 int min_pu_width, int x, int y,
560 int elist, int ref_idx_curr, int ref_idx)
562 RefPicList *refPicList = s->ref->refPicList;
563 MvField *tab_mvf = s->ref->tab_mvf;
564 int ref_pic_elist = refPicList[elist].list[TAB_MVF(x, y).ref_idx[elist]];
565 int ref_pic_curr = refPicList[ref_idx_curr].list[ref_idx];
567 if (ref_pic_elist != ref_pic_curr) {
568 int poc_diff = s->poc - ref_pic_elist;
571 mv_scale(mv, mv, poc_diff, s->poc - ref_pic_curr);
575 static int mv_mp_mode_mx(HEVCContext *s, int x, int y, int pred_flag_index,
576 Mv *mv, int ref_idx_curr, int ref_idx)
578 MvField *tab_mvf = s->ref->tab_mvf;
579 int min_pu_width = s->sps->min_pu_width;
581 RefPicList *refPicList = s->ref->refPicList;
583 if (((TAB_MVF(x, y).pred_flag) & (1 << pred_flag_index)) &&
584 refPicList[pred_flag_index].list[TAB_MVF(x, y).ref_idx[pred_flag_index]] == refPicList[ref_idx_curr].list[ref_idx]) {
585 *mv = TAB_MVF(x, y).mv[pred_flag_index];
591 static int mv_mp_mode_mx_lt(HEVCContext *s, int x, int y, int pred_flag_index,
592 Mv *mv, int ref_idx_curr, int ref_idx)
594 MvField *tab_mvf = s->ref->tab_mvf;
595 int min_pu_width = s->sps->min_pu_width;
597 RefPicList *refPicList = s->ref->refPicList;
599 if ((TAB_MVF(x, y).pred_flag) & (1 << pred_flag_index)) {
600 int currIsLongTerm = refPicList[ref_idx_curr].isLongTerm[ref_idx];
603 refPicList[pred_flag_index].isLongTerm[(TAB_MVF(x, y).ref_idx[pred_flag_index])];
605 if (colIsLongTerm == currIsLongTerm) {
606 *mv = TAB_MVF(x, y).mv[pred_flag_index];
608 dist_scale(s, mv, min_pu_width, x, y,
609 pred_flag_index, ref_idx_curr, ref_idx);
616 #define MP_MX(v, pred, mx) \
617 mv_mp_mode_mx(s, x ## v ## _pu, y ## v ## _pu, pred, \
618 &mx, ref_idx_curr, ref_idx)
620 #define MP_MX_LT(v, pred, mx) \
621 mv_mp_mode_mx_lt(s, x ## v ## _pu, y ## v ## _pu, pred, \
622 &mx, ref_idx_curr, ref_idx)
624 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
625 int nPbH, int log2_cb_size, int part_idx,
626 int merge_idx, MvField *mv,
627 int mvp_lx_flag, int LX)
629 HEVCLocalContext *lc = s->HEVClc;
630 MvField *tab_mvf = s->ref->tab_mvf;
631 int isScaledFlag_L0 = 0;
632 int availableFlagLXA0 = 0;
633 int availableFlagLXB0 = 0;
634 int numMVPCandLX = 0;
635 int min_pu_width = s->sps->min_pu_width;
650 int xB1_pu = 0, yB1_pu = 0;
651 int is_available_b1 = 0;
654 int xB2_pu = 0, yB2_pu = 0;
655 int is_available_b2 = 0;
656 Mv mvpcand_list[2] = { { 0 } };
659 int ref_idx_curr = 0;
661 int pred_flag_index_l0;
662 int pred_flag_index_l1;
663 int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
664 int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
666 int cand_up = (lc->ctb_up_flag || y0b);
667 int cand_left = (lc->ctb_left_flag || x0b);
669 (!x0b && !y0b) ? lc->ctb_up_left_flag : cand_left && cand_up;
671 (x0b + nPbW == (1 << s->sps->log2_ctb_size) ||
672 x0 + nPbW >= lc->end_of_tiles_x) ? lc->ctb_up_right_flag && !y0b
674 int cand_bottom_left = (y0 + nPbH >= lc->end_of_tiles_y) ? 0 : cand_left;
677 ref_idx = mv->ref_idx[LX];
678 pred_flag_index_l0 = LX;
679 pred_flag_index_l1 = !LX;
681 // left bottom spatial candidate
684 xA0_pu = xA0 >> s->sps->log2_min_pu_size;
685 yA0_pu = yA0 >> s->sps->log2_min_pu_size;
687 is_available_a0 = PRED_BLOCK_AVAILABLE(A0) && AVAILABLE(cand_bottom_left, A0);
689 //left spatial merge candidate
692 xA1_pu = xA1 >> s->sps->log2_min_pu_size;
693 yA1_pu = yA1 >> s->sps->log2_min_pu_size;
695 is_available_a1 = AVAILABLE(cand_left, A1);
696 if (is_available_a0 || is_available_a1)
699 if (is_available_a0) {
700 availableFlagLXA0 = MP_MX(A0, pred_flag_index_l0, mxA);
701 if (!availableFlagLXA0)
702 availableFlagLXA0 = MP_MX(A0, pred_flag_index_l1, mxA);
705 if (is_available_a1 && !availableFlagLXA0) {
706 availableFlagLXA0 = MP_MX(A1, pred_flag_index_l0, mxA);
707 if (!availableFlagLXA0)
708 availableFlagLXA0 = MP_MX(A1, pred_flag_index_l1, mxA);
711 if (is_available_a0 && !availableFlagLXA0) {
712 availableFlagLXA0 = MP_MX_LT(A0, pred_flag_index_l0, mxA);
713 if (!availableFlagLXA0)
714 availableFlagLXA0 = MP_MX_LT(A0, pred_flag_index_l1, mxA);
717 if (is_available_a1 && !availableFlagLXA0) {
718 availableFlagLXA0 = MP_MX_LT(A1, pred_flag_index_l0, mxA);
719 if (!availableFlagLXA0)
720 availableFlagLXA0 = MP_MX_LT(A1, pred_flag_index_l1, mxA);
724 // above right spatial merge candidate
727 xB0_pu = xB0 >> s->sps->log2_min_pu_size;
728 yB0_pu = yB0 >> s->sps->log2_min_pu_size;
730 is_available_b0 = PRED_BLOCK_AVAILABLE(B0) && AVAILABLE(cand_up_right, B0);
732 if (is_available_b0) {
733 availableFlagLXB0 = MP_MX(B0, pred_flag_index_l0, mxB);
734 if (!availableFlagLXB0)
735 availableFlagLXB0 = MP_MX(B0, pred_flag_index_l1, mxB);
738 if (!availableFlagLXB0) {
739 // above spatial merge candidate
742 xB1_pu = xB1 >> s->sps->log2_min_pu_size;
743 yB1_pu = yB1 >> s->sps->log2_min_pu_size;
745 is_available_b1 = AVAILABLE(cand_up, B1);
747 if (is_available_b1) {
748 availableFlagLXB0 = MP_MX(B1, pred_flag_index_l0, mxB);
749 if (!availableFlagLXB0)
750 availableFlagLXB0 = MP_MX(B1, pred_flag_index_l1, mxB);
754 if (!availableFlagLXB0) {
755 // above left spatial merge candidate
758 xB2_pu = xB2 >> s->sps->log2_min_pu_size;
759 yB2_pu = yB2 >> s->sps->log2_min_pu_size;
760 is_available_b2 = AVAILABLE(cand_up_left, B2);
762 if (is_available_b2) {
763 availableFlagLXB0 = MP_MX(B2, pred_flag_index_l0, mxB);
764 if (!availableFlagLXB0)
765 availableFlagLXB0 = MP_MX(B2, pred_flag_index_l1, mxB);
769 if (isScaledFlag_L0 == 0) {
770 if (availableFlagLXB0) {
771 availableFlagLXA0 = 1;
774 availableFlagLXB0 = 0;
777 if (is_available_b0) {
778 availableFlagLXB0 = MP_MX_LT(B0, pred_flag_index_l0, mxB);
779 if (!availableFlagLXB0)
780 availableFlagLXB0 = MP_MX_LT(B0, pred_flag_index_l1, mxB);
783 if (is_available_b1 && !availableFlagLXB0) {
784 availableFlagLXB0 = MP_MX_LT(B1, pred_flag_index_l0, mxB);
785 if (!availableFlagLXB0)
786 availableFlagLXB0 = MP_MX_LT(B1, pred_flag_index_l1, mxB);
789 if (is_available_b2 && !availableFlagLXB0) {
790 availableFlagLXB0 = MP_MX_LT(B2, pred_flag_index_l0, mxB);
791 if (!availableFlagLXB0)
792 availableFlagLXB0 = MP_MX_LT(B2, pred_flag_index_l1, mxB);
796 if (availableFlagLXA0)
797 mvpcand_list[numMVPCandLX++] = mxA;
799 if (availableFlagLXB0 && (!availableFlagLXA0 || mxA.x != mxB.x || mxA.y != mxB.y))
800 mvpcand_list[numMVPCandLX++] = mxB;
802 //temporal motion vector prediction candidate
803 if (numMVPCandLX < 2 && s->sh.slice_temporal_mvp_enabled_flag) {
805 int available_col = temporal_luma_motion_vector(s, x0, y0, nPbW,
809 mvpcand_list[numMVPCandLX++] = mv_col;
812 mv->mv[LX] = mvpcand_list[mvp_lx_flag];