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.

846 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->ps.sps->log2_ctb_size) - 1);
  43. int y0b = y0 & ((1 << s->ps.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->ps.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->ps.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->ps.pps->min_tb_addr_zs[(y) * s->ps.sps->min_tb_width + (x)]
  64. int Curr = MIN_TB_ADDR_ZS(xCurr >> s->ps.sps->log2_min_tb_size,
  65. yCurr >> s->ps.sps->log2_min_tb_size);
  66. int N;
  67. if (xN < 0 || yN < 0 ||
  68. xN >= s->ps.sps->width ||
  69. yN >= s->ps.sps->height)
  70. return 0;
  71. N = MIN_TB_ADDR_ZS(xN >> s->ps.sps->log2_min_tb_size,
  72. yN >> s->ps.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 motion estimation region
  101. static int isDiffMER(HEVCContext *s, int xN, int yN, int xP, int yP)
  102. {
  103. uint8_t plevel = s->ps.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(td);
  125. tb = av_clip_int8(tb);
  126. tx = (0x4000 + abs(td / 2)) / td;
  127. scale_factor = av_clip((tb * tx + 32) >> 6, -4096, 4095);
  128. dst->x = av_clip_int16((scale_factor * src->x + 127 +
  129. (scale_factor * src->x < 0)) >> 8);
  130. dst->y = av_clip_int16((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->ps.sps->min_pu_width;
  221. int availableFlagLXCol = 0;
  222. int colPic;
  223. HEVCFrame *ref = s->ref->collocated_ref;
  224. if (!ref) {
  225. memset(mvLXCol, 0, sizeof(*mvLXCol));
  226. return 0;
  227. }
  228. tab_mvf = ref->tab_mvf;
  229. colPic = ref->poc;
  230. //bottom right collocated motion vector
  231. x = x0 + nPbW;
  232. y = y0 + nPbH;
  233. if (tab_mvf &&
  234. (y0 >> s->ps.sps->log2_ctb_size) == (y >> s->ps.sps->log2_ctb_size) &&
  235. y < s->ps.sps->height &&
  236. x < s->ps.sps->width) {
  237. x &= ~15;
  238. y &= ~15;
  239. ff_thread_await_progress(&ref->tf, y, 0);
  240. x_pu = x >> s->ps.sps->log2_min_pu_size;
  241. y_pu = y >> s->ps.sps->log2_min_pu_size;
  242. temp_col = TAB_MVF(x_pu, y_pu);
  243. availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS;
  244. }
  245. // derive center collocated motion vector
  246. if (tab_mvf && !availableFlagLXCol) {
  247. x = x0 + (nPbW >> 1);
  248. y = y0 + (nPbH >> 1);
  249. x &= ~15;
  250. y &= ~15;
  251. ff_thread_await_progress(&ref->tf, y, 0);
  252. x_pu = x >> s->ps.sps->log2_min_pu_size;
  253. y_pu = y >> s->ps.sps->log2_min_pu_size;
  254. temp_col = TAB_MVF(x_pu, y_pu);
  255. availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS;
  256. }
  257. return availableFlagLXCol;
  258. }
  259. #define AVAILABLE(cand, v) \
  260. (cand && !TAB_MVF_PU(v).is_intra)
  261. #define PRED_BLOCK_AVAILABLE(v) \
  262. check_prediction_block_available(s, log2_cb_size, \
  263. x0, y0, nPbW, nPbH, \
  264. x ## v, y ## v, part_idx)
  265. #define COMPARE_MV_REFIDX(a, b) \
  266. compareMVrefidx(TAB_MVF_PU(a), TAB_MVF_PU(b))
  267. /*
  268. * 8.5.3.1.2 Derivation process for spatial merging candidates
  269. */
  270. static void derive_spatial_merge_candidates(HEVCContext *s, int x0, int y0,
  271. int nPbW, int nPbH,
  272. int log2_cb_size,
  273. int singleMCLFlag, int part_idx,
  274. int merge_idx,
  275. struct MvField mergecandlist[])
  276. {
  277. HEVCLocalContext *lc = &s->HEVClc;
  278. RefPicList *refPicList = s->ref->refPicList;
  279. MvField *tab_mvf = s->ref->tab_mvf;
  280. const int min_pu_width = s->ps.sps->min_pu_width;
  281. const int cand_bottom_left = lc->na.cand_bottom_left;
  282. const int cand_left = lc->na.cand_left;
  283. const int cand_up_left = lc->na.cand_up_left;
  284. const int cand_up = lc->na.cand_up;
  285. const int cand_up_right = lc->na.cand_up_right_sap;
  286. const int xA1 = x0 - 1;
  287. const int yA1 = y0 + nPbH - 1;
  288. const int xA1_pu = xA1 >> s->ps.sps->log2_min_pu_size;
  289. const int yA1_pu = yA1 >> s->ps.sps->log2_min_pu_size;
  290. const int xB1 = x0 + nPbW - 1;
  291. const int yB1 = y0 - 1;
  292. const int xB1_pu = xB1 >> s->ps.sps->log2_min_pu_size;
  293. const int yB1_pu = yB1 >> s->ps.sps->log2_min_pu_size;
  294. const int xB0 = x0 + nPbW;
  295. const int yB0 = y0 - 1;
  296. const int xB0_pu = xB0 >> s->ps.sps->log2_min_pu_size;
  297. const int yB0_pu = yB0 >> s->ps.sps->log2_min_pu_size;
  298. const int xA0 = x0 - 1;
  299. const int yA0 = y0 + nPbH;
  300. const int xA0_pu = xA0 >> s->ps.sps->log2_min_pu_size;
  301. const int yA0_pu = yA0 >> s->ps.sps->log2_min_pu_size;
  302. const int xB2 = x0 - 1;
  303. const int yB2 = y0 - 1;
  304. const int xB2_pu = xB2 >> s->ps.sps->log2_min_pu_size;
  305. const int yB2_pu = yB2 >> s->ps.sps->log2_min_pu_size;
  306. const int nb_refs = (s->sh.slice_type == P_SLICE) ?
  307. s->sh.nb_refs[0] : FFMIN(s->sh.nb_refs[0], s->sh.nb_refs[1]);
  308. int check_MER = 1;
  309. int check_MER_1 = 1;
  310. int zero_idx = 0;
  311. int nb_merge_cand = 0;
  312. int nb_orig_merge_cand = 0;
  313. int is_available_a0;
  314. int is_available_a1;
  315. int is_available_b0;
  316. int is_available_b1;
  317. int is_available_b2;
  318. int check_B0;
  319. int check_A0;
  320. //first left spatial merge candidate
  321. is_available_a1 = AVAILABLE(cand_left, A1);
  322. if (!singleMCLFlag && part_idx == 1 &&
  323. (lc->cu.part_mode == PART_Nx2N ||
  324. lc->cu.part_mode == PART_nLx2N ||
  325. lc->cu.part_mode == PART_nRx2N) ||
  326. isDiffMER(s, xA1, yA1, x0, y0)) {
  327. is_available_a1 = 0;
  328. }
  329. if (is_available_a1) {
  330. mergecandlist[0] = TAB_MVF_PU(A1);
  331. if (merge_idx == 0)
  332. return;
  333. nb_merge_cand++;
  334. }
  335. // above spatial merge candidate
  336. is_available_b1 = AVAILABLE(cand_up, B1);
  337. if (!singleMCLFlag && part_idx == 1 &&
  338. (lc->cu.part_mode == PART_2NxN ||
  339. lc->cu.part_mode == PART_2NxnU ||
  340. lc->cu.part_mode == PART_2NxnD) ||
  341. isDiffMER(s, xB1, yB1, x0, y0)) {
  342. is_available_b1 = 0;
  343. }
  344. if (is_available_a1 && is_available_b1)
  345. check_MER = !COMPARE_MV_REFIDX(B1, A1);
  346. if (is_available_b1 && check_MER)
  347. mergecandlist[nb_merge_cand++] = TAB_MVF_PU(B1);
  348. // above right spatial merge candidate
  349. check_MER = 1;
  350. check_B0 = PRED_BLOCK_AVAILABLE(B0);
  351. is_available_b0 = check_B0 && AVAILABLE(cand_up_right, B0);
  352. if (isDiffMER(s, xB0, yB0, x0, y0))
  353. is_available_b0 = 0;
  354. if (is_available_b1 && is_available_b0)
  355. check_MER = !COMPARE_MV_REFIDX(B0, B1);
  356. if (is_available_b0 && check_MER) {
  357. mergecandlist[nb_merge_cand] = TAB_MVF_PU(B0);
  358. if (merge_idx == nb_merge_cand)
  359. return;
  360. nb_merge_cand++;
  361. }
  362. // left bottom spatial merge candidate
  363. check_MER = 1;
  364. check_A0 = PRED_BLOCK_AVAILABLE(A0);
  365. is_available_a0 = check_A0 && AVAILABLE(cand_bottom_left, A0);
  366. if (isDiffMER(s, xA0, yA0, x0, y0))
  367. is_available_a0 = 0;
  368. if (is_available_a1 && is_available_a0)
  369. check_MER = !COMPARE_MV_REFIDX(A0, A1);
  370. if (is_available_a0 && check_MER) {
  371. mergecandlist[nb_merge_cand] = TAB_MVF_PU(A0);
  372. if (merge_idx == nb_merge_cand)
  373. return;
  374. nb_merge_cand++;
  375. }
  376. // above left spatial merge candidate
  377. check_MER = 1;
  378. is_available_b2 = AVAILABLE(cand_up_left, B2);
  379. if (isDiffMER(s, xB2, yB2, x0, y0))
  380. is_available_b2 = 0;
  381. if (is_available_a1 && is_available_b2)
  382. check_MER = !COMPARE_MV_REFIDX(B2, A1);
  383. if (is_available_b1 && is_available_b2)
  384. check_MER_1 = !COMPARE_MV_REFIDX(B2, B1);
  385. if (is_available_b2 && check_MER && check_MER_1 && nb_merge_cand != 4) {
  386. mergecandlist[nb_merge_cand] = TAB_MVF_PU(B2);
  387. if (merge_idx == nb_merge_cand)
  388. return;
  389. nb_merge_cand++;
  390. }
  391. // temporal motion vector candidate
  392. if (s->sh.slice_temporal_mvp_enabled_flag &&
  393. nb_merge_cand < s->sh.max_num_merge_cand) {
  394. Mv mv_l0_col = { 0 }, mv_l1_col = { 0 };
  395. int available_l0 = temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
  396. 0, &mv_l0_col, 0);
  397. int available_l1 = (s->sh.slice_type == B_SLICE) ?
  398. temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
  399. 0, &mv_l1_col, 1) : 0;
  400. if (available_l0 || available_l1) {
  401. mergecandlist[nb_merge_cand].is_intra = 0;
  402. mergecandlist[nb_merge_cand].pred_flag[0] = available_l0;
  403. mergecandlist[nb_merge_cand].pred_flag[1] = available_l1;
  404. AV_ZERO16(mergecandlist[nb_merge_cand].ref_idx);
  405. mergecandlist[nb_merge_cand].mv[0] = mv_l0_col;
  406. mergecandlist[nb_merge_cand].mv[1] = mv_l1_col;
  407. if (merge_idx == nb_merge_cand)
  408. return;
  409. nb_merge_cand++;
  410. }
  411. }
  412. nb_orig_merge_cand = nb_merge_cand;
  413. // combined bi-predictive merge candidates (applies for B slices)
  414. if (s->sh.slice_type == B_SLICE && nb_orig_merge_cand > 1 &&
  415. nb_orig_merge_cand < s->sh.max_num_merge_cand) {
  416. int comb_idx;
  417. for (comb_idx = 0; nb_merge_cand < s->sh.max_num_merge_cand &&
  418. comb_idx < nb_orig_merge_cand * (nb_orig_merge_cand - 1); comb_idx++) {
  419. int l0_cand_idx = l0_l1_cand_idx[comb_idx][0];
  420. int l1_cand_idx = l0_l1_cand_idx[comb_idx][1];
  421. MvField l0_cand = mergecandlist[l0_cand_idx];
  422. MvField l1_cand = mergecandlist[l1_cand_idx];
  423. if (l0_cand.pred_flag[0] && l1_cand.pred_flag[1] &&
  424. (refPicList[0].list[l0_cand.ref_idx[0]] !=
  425. refPicList[1].list[l1_cand.ref_idx[1]] ||
  426. AV_RN32A(&l0_cand.mv[0]) != AV_RN32A(&l1_cand.mv[1]))) {
  427. mergecandlist[nb_merge_cand].ref_idx[0] = l0_cand.ref_idx[0];
  428. mergecandlist[nb_merge_cand].ref_idx[1] = l1_cand.ref_idx[1];
  429. mergecandlist[nb_merge_cand].pred_flag[0] = 1;
  430. mergecandlist[nb_merge_cand].pred_flag[1] = 1;
  431. AV_COPY32(&mergecandlist[nb_merge_cand].mv[0], &l0_cand.mv[0]);
  432. AV_COPY32(&mergecandlist[nb_merge_cand].mv[1], &l1_cand.mv[1]);
  433. mergecandlist[nb_merge_cand].is_intra = 0;
  434. if (merge_idx == nb_merge_cand)
  435. return;
  436. nb_merge_cand++;
  437. }
  438. }
  439. }
  440. // append Zero motion vector candidates
  441. while (nb_merge_cand < s->sh.max_num_merge_cand) {
  442. mergecandlist[nb_merge_cand].pred_flag[0] = 1;
  443. mergecandlist[nb_merge_cand].pred_flag[1] = s->sh.slice_type == B_SLICE;
  444. AV_ZERO32(mergecandlist[nb_merge_cand].mv + 0);
  445. AV_ZERO32(mergecandlist[nb_merge_cand].mv + 1);
  446. mergecandlist[nb_merge_cand].is_intra = 0;
  447. mergecandlist[nb_merge_cand].ref_idx[0] = zero_idx < nb_refs ? zero_idx : 0;
  448. mergecandlist[nb_merge_cand].ref_idx[1] = zero_idx < nb_refs ? zero_idx : 0;
  449. if (merge_idx == nb_merge_cand)
  450. return;
  451. nb_merge_cand++;
  452. zero_idx++;
  453. }
  454. }
  455. /*
  456. * 8.5.3.1.1 Derivation process of luma Mvs for merge mode
  457. */
  458. void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0, int nPbW,
  459. int nPbH, int log2_cb_size, int part_idx,
  460. int merge_idx, MvField *mv)
  461. {
  462. int singleMCLFlag = 0;
  463. int nCS = 1 << log2_cb_size;
  464. LOCAL_ALIGNED(4, MvField, mergecand_list, [MRG_MAX_NUM_CANDS]);
  465. int nPbW2 = nPbW;
  466. int nPbH2 = nPbH;
  467. HEVCLocalContext *lc = &s->HEVClc;
  468. if (s->ps.pps->log2_parallel_merge_level > 2 && nCS == 8) {
  469. singleMCLFlag = 1;
  470. x0 = lc->cu.x;
  471. y0 = lc->cu.y;
  472. nPbW = nCS;
  473. nPbH = nCS;
  474. part_idx = 0;
  475. }
  476. ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
  477. derive_spatial_merge_candidates(s, x0, y0, nPbW, nPbH, log2_cb_size,
  478. singleMCLFlag, part_idx,
  479. merge_idx, mergecand_list);
  480. if (mergecand_list[merge_idx].pred_flag[0] == 1 &&
  481. mergecand_list[merge_idx].pred_flag[1] == 1 &&
  482. (nPbW2 + nPbH2) == 12) {
  483. mergecand_list[merge_idx].ref_idx[1] = -1;
  484. mergecand_list[merge_idx].pred_flag[1] = 0;
  485. }
  486. *mv = mergecand_list[merge_idx];
  487. }
  488. static av_always_inline void dist_scale(HEVCContext *s, Mv *mv,
  489. int min_pu_width, int x, int y,
  490. int elist, int ref_idx_curr, int ref_idx)
  491. {
  492. RefPicList *refPicList = s->ref->refPicList;
  493. MvField *tab_mvf = s->ref->tab_mvf;
  494. int ref_pic_elist = refPicList[elist].list[TAB_MVF(x, y).ref_idx[elist]];
  495. int ref_pic_curr = refPicList[ref_idx_curr].list[ref_idx];
  496. if (ref_pic_elist != ref_pic_curr) {
  497. int poc_diff = s->poc - ref_pic_elist;
  498. if (!poc_diff)
  499. poc_diff = 1;
  500. mv_scale(mv, mv, poc_diff, s->poc - ref_pic_curr);
  501. }
  502. }
  503. static int mv_mp_mode_mx(HEVCContext *s, int x, int y, int pred_flag_index,
  504. Mv *mv, int ref_idx_curr, int ref_idx)
  505. {
  506. MvField *tab_mvf = s->ref->tab_mvf;
  507. int min_pu_width = s->ps.sps->min_pu_width;
  508. RefPicList *refPicList = s->ref->refPicList;
  509. if (TAB_MVF(x, y).pred_flag[pred_flag_index] == 1 &&
  510. refPicList[pred_flag_index].list[TAB_MVF(x, y).ref_idx[pred_flag_index]] == refPicList[ref_idx_curr].list[ref_idx]) {
  511. *mv = TAB_MVF(x, y).mv[pred_flag_index];
  512. return 1;
  513. }
  514. return 0;
  515. }
  516. static int mv_mp_mode_mx_lt(HEVCContext *s, int x, int y, int pred_flag_index,
  517. Mv *mv, int ref_idx_curr, int ref_idx)
  518. {
  519. MvField *tab_mvf = s->ref->tab_mvf;
  520. int min_pu_width = s->ps.sps->min_pu_width;
  521. RefPicList *refPicList = s->ref->refPicList;
  522. int currIsLongTerm = refPicList[ref_idx_curr].isLongTerm[ref_idx];
  523. int colIsLongTerm =
  524. refPicList[pred_flag_index].isLongTerm[(TAB_MVF(x, y).ref_idx[pred_flag_index])];
  525. if (TAB_MVF(x, y).pred_flag[pred_flag_index] &&
  526. colIsLongTerm == currIsLongTerm) {
  527. *mv = TAB_MVF(x, y).mv[pred_flag_index];
  528. if (!currIsLongTerm)
  529. dist_scale(s, mv, min_pu_width, x, y,
  530. pred_flag_index, ref_idx_curr, ref_idx);
  531. return 1;
  532. }
  533. return 0;
  534. }
  535. #define MP_MX(v, pred, mx) \
  536. mv_mp_mode_mx(s, x ## v ## _pu, y ## v ## _pu, pred, \
  537. &mx, ref_idx_curr, ref_idx)
  538. #define MP_MX_LT(v, pred, mx) \
  539. mv_mp_mode_mx_lt(s, x ## v ## _pu, y ## v ## _pu, pred, \
  540. &mx, ref_idx_curr, ref_idx)
  541. void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
  542. int nPbH, int log2_cb_size, int part_idx,
  543. int merge_idx, MvField *mv,
  544. int mvp_lx_flag, int LX)
  545. {
  546. HEVCLocalContext *lc = &s->HEVClc;
  547. MvField *tab_mvf = s->ref->tab_mvf;
  548. int isScaledFlag_L0 = 0;
  549. int availableFlagLXA0 = 0;
  550. int availableFlagLXB0 = 0;
  551. int numMVPCandLX = 0;
  552. int min_pu_width = s->ps.sps->min_pu_width;
  553. int xA0, yA0;
  554. int xA0_pu, yA0_pu;
  555. int is_available_a0;
  556. int xA1, yA1;
  557. int xA1_pu, yA1_pu;
  558. int is_available_a1;
  559. int xB0, yB0;
  560. int xB0_pu, yB0_pu;
  561. int is_available_b0;
  562. int xB1, yB1;
  563. int xB1_pu = 0, yB1_pu = 0;
  564. int is_available_b1 = 0;
  565. int xB2, yB2;
  566. int xB2_pu = 0, yB2_pu = 0;
  567. int is_available_b2 = 0;
  568. Mv mvpcand_list[2] = { { 0 } };
  569. Mv mxA = { 0 };
  570. Mv mxB = { 0 };
  571. int ref_idx_curr = 0;
  572. int ref_idx = 0;
  573. int pred_flag_index_l0;
  574. int pred_flag_index_l1;
  575. int x0b = x0 & ((1 << s->ps.sps->log2_ctb_size) - 1);
  576. int y0b = y0 & ((1 << s->ps.sps->log2_ctb_size) - 1);
  577. int cand_up = (lc->ctb_up_flag || y0b);
  578. int cand_left = (lc->ctb_left_flag || x0b);
  579. int cand_up_left =
  580. (!x0b && !y0b) ? lc->ctb_up_left_flag : cand_left && cand_up;
  581. int cand_up_right =
  582. (x0b + nPbW == (1 << s->ps.sps->log2_ctb_size) ||
  583. x0 + nPbW >= lc->end_of_tiles_x) ? lc->ctb_up_right_flag && !y0b
  584. : cand_up;
  585. int cand_bottom_left = (y0 + nPbH >= lc->end_of_tiles_y) ? 0 : cand_left;
  586. ref_idx_curr = LX;
  587. ref_idx = mv->ref_idx[LX];
  588. pred_flag_index_l0 = LX;
  589. pred_flag_index_l1 = !LX;
  590. // left bottom spatial candidate
  591. xA0 = x0 - 1;
  592. yA0 = y0 + nPbH;
  593. xA0_pu = xA0 >> s->ps.sps->log2_min_pu_size;
  594. yA0_pu = yA0 >> s->ps.sps->log2_min_pu_size;
  595. is_available_a0 = PRED_BLOCK_AVAILABLE(A0) && AVAILABLE(cand_bottom_left, A0);
  596. //left spatial merge candidate
  597. xA1 = x0 - 1;
  598. yA1 = y0 + nPbH - 1;
  599. xA1_pu = xA1 >> s->ps.sps->log2_min_pu_size;
  600. yA1_pu = yA1 >> s->ps.sps->log2_min_pu_size;
  601. is_available_a1 = AVAILABLE(cand_left, A1);
  602. if (is_available_a0 || is_available_a1)
  603. isScaledFlag_L0 = 1;
  604. if (is_available_a0) {
  605. availableFlagLXA0 = MP_MX(A0, pred_flag_index_l0, mxA);
  606. if (!availableFlagLXA0)
  607. availableFlagLXA0 = MP_MX(A0, pred_flag_index_l1, mxA);
  608. }
  609. if (is_available_a1 && !availableFlagLXA0) {
  610. availableFlagLXA0 = MP_MX(A1, pred_flag_index_l0, mxA);
  611. if (!availableFlagLXA0)
  612. availableFlagLXA0 = MP_MX(A1, pred_flag_index_l1, mxA);
  613. }
  614. if (is_available_a0 && !availableFlagLXA0) {
  615. availableFlagLXA0 = MP_MX_LT(A0, pred_flag_index_l0, mxA);
  616. if (!availableFlagLXA0)
  617. availableFlagLXA0 = MP_MX_LT(A0, pred_flag_index_l1, mxA);
  618. }
  619. if (is_available_a1 && !availableFlagLXA0) {
  620. availableFlagLXA0 = MP_MX_LT(A1, pred_flag_index_l0, mxA);
  621. if (!availableFlagLXA0)
  622. availableFlagLXA0 = MP_MX_LT(A1, pred_flag_index_l1, mxA);
  623. }
  624. if (availableFlagLXA0 && !mvp_lx_flag) {
  625. mv->mv[LX] = mxA;
  626. return;
  627. }
  628. // B candidates
  629. // above right spatial merge candidate
  630. xB0 = x0 + nPbW;
  631. yB0 = y0 - 1;
  632. xB0_pu = xB0 >> s->ps.sps->log2_min_pu_size;
  633. yB0_pu = yB0 >> s->ps.sps->log2_min_pu_size;
  634. is_available_b0 = PRED_BLOCK_AVAILABLE(B0) && AVAILABLE(cand_up_right, B0);
  635. if (is_available_b0) {
  636. availableFlagLXB0 = MP_MX(B0, pred_flag_index_l0, mxB);
  637. if (!availableFlagLXB0)
  638. availableFlagLXB0 = MP_MX(B0, pred_flag_index_l1, mxB);
  639. }
  640. if (!availableFlagLXB0) {
  641. // above spatial merge candidate
  642. xB1 = x0 + nPbW - 1;
  643. yB1 = y0 - 1;
  644. xB1_pu = xB1 >> s->ps.sps->log2_min_pu_size;
  645. yB1_pu = yB1 >> s->ps.sps->log2_min_pu_size;
  646. is_available_b1 = AVAILABLE(cand_up, B1);
  647. if (is_available_b1) {
  648. availableFlagLXB0 = MP_MX(B1, pred_flag_index_l0, mxB);
  649. if (!availableFlagLXB0)
  650. availableFlagLXB0 = MP_MX(B1, pred_flag_index_l1, mxB);
  651. }
  652. }
  653. if (!availableFlagLXB0) {
  654. // above left spatial merge candidate
  655. xB2 = x0 - 1;
  656. yB2 = y0 - 1;
  657. xB2_pu = xB2 >> s->ps.sps->log2_min_pu_size;
  658. yB2_pu = yB2 >> s->ps.sps->log2_min_pu_size;
  659. is_available_b2 = AVAILABLE(cand_up_left, B2);
  660. if (is_available_b2) {
  661. availableFlagLXB0 = MP_MX(B2, pred_flag_index_l0, mxB);
  662. if (!availableFlagLXB0)
  663. availableFlagLXB0 = MP_MX(B2, pred_flag_index_l1, mxB);
  664. }
  665. }
  666. if (isScaledFlag_L0 == 0) {
  667. if (availableFlagLXB0) {
  668. availableFlagLXA0 = 1;
  669. mxA = mxB;
  670. }
  671. availableFlagLXB0 = 0;
  672. // XB0 and L1
  673. if (is_available_b0) {
  674. availableFlagLXB0 = MP_MX_LT(B0, pred_flag_index_l0, mxB);
  675. if (!availableFlagLXB0)
  676. availableFlagLXB0 = MP_MX_LT(B0, pred_flag_index_l1, mxB);
  677. }
  678. if (is_available_b1 && !availableFlagLXB0) {
  679. availableFlagLXB0 = MP_MX_LT(B1, pred_flag_index_l0, mxB);
  680. if (!availableFlagLXB0)
  681. availableFlagLXB0 = MP_MX_LT(B1, pred_flag_index_l1, mxB);
  682. }
  683. if (is_available_b2 && !availableFlagLXB0) {
  684. availableFlagLXB0 = MP_MX_LT(B2, pred_flag_index_l0, mxB);
  685. if (!availableFlagLXB0)
  686. availableFlagLXB0 = MP_MX_LT(B2, pred_flag_index_l1, mxB);
  687. }
  688. }
  689. if (availableFlagLXA0)
  690. mvpcand_list[numMVPCandLX++] = mxA;
  691. if (availableFlagLXB0 && (!availableFlagLXA0 || mxA.x != mxB.x || mxA.y != mxB.y))
  692. mvpcand_list[numMVPCandLX++] = mxB;
  693. //temporal motion vector prediction candidate
  694. if (numMVPCandLX < 2 && s->sh.slice_temporal_mvp_enabled_flag &&
  695. mvp_lx_flag == numMVPCandLX) {
  696. Mv mv_col;
  697. int available_col = temporal_luma_motion_vector(s, x0, y0, nPbW,
  698. nPbH, ref_idx,
  699. &mv_col, LX);
  700. if (available_col)
  701. mvpcand_list[numMVPCandLX++] = mv_col;
  702. }
  703. // insert zero motion vectors when the number of available candidates are less than 2
  704. while (numMVPCandLX < 2)
  705. mvpcand_list[numMVPCandLX++] = (Mv){ 0, 0 };
  706. mv->mv[LX].x = mvpcand_list[mvp_lx_flag].x;
  707. mv->mv[LX].y = mvpcand_list[mvp_lx_flag].y;
  708. }