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