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.

815 lines
27KB

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