You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

848 lines
29KB

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