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.

774 lines
26KB

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