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.

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