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.

1008 lines
32KB

  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_transform_block_size,
  64. yCurr >> s->sps->log2_min_transform_block_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_transform_block_size,
  71. yN >> s->sps->log2_min_transform_block_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) * pic_width_in_min_pu + x]
  202. #define TAB_MVF_PU(v) \
  203. TAB_MVF(x##v##_pu, y##v##_pu)
  204. #define DERIVE_TEMPORAL_COLOCATED_MVS(v) \
  205. derive_temporal_colocated_mvs(s, temp_col, \
  206. refIdxLx, mvLXCol, X, colPic, \
  207. ff_hevc_get_ref_list(s, ref, \
  208. x##v, y##v))
  209. /*
  210. * 8.5.3.1.7 temporal luma motion vector prediction
  211. */
  212. static int temporal_luma_motion_vector(HEVCContext *s, int x0, int y0,
  213. int nPbW, int nPbH, int refIdxLx,
  214. Mv* mvLXCol, int X)
  215. {
  216. MvField *tab_mvf;
  217. MvField temp_col;
  218. int xPRb, yPRb;
  219. int xPRb_pu;
  220. int yPRb_pu;
  221. int xPCtr, yPCtr;
  222. int xPCtr_pu;
  223. int yPCtr_pu;
  224. int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
  225. int availableFlagLXCol = 0;
  226. int colPic;
  227. HEVCFrame *ref = s->ref->collocated_ref;
  228. if (!ref)
  229. return 0;
  230. tab_mvf = ref->tab_mvf;
  231. colPic = ref->poc;
  232. //bottom right collocated motion vector
  233. xPRb = x0 + nPbW;
  234. yPRb = y0 + nPbH;
  235. ff_thread_await_progress(&ref->tf, INT_MAX, 0);
  236. if (tab_mvf &&
  237. y0 >> s->sps->log2_ctb_size == yPRb >> s->sps->log2_ctb_size &&
  238. yPRb < s->sps->height &&
  239. xPRb < s->sps->width) {
  240. xPRb = ((xPRb >> 4) << 4);
  241. yPRb = ((yPRb >> 4) << 4);
  242. xPRb_pu = xPRb >> s->sps->log2_min_pu_size;
  243. yPRb_pu = yPRb >> s->sps->log2_min_pu_size;
  244. temp_col = TAB_MVF_PU(PRb);
  245. availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS(PRb);
  246. } else {
  247. mvLXCol->x = 0;
  248. mvLXCol->y = 0;
  249. availableFlagLXCol = 0;
  250. }
  251. // derive center collocated motion vector
  252. if (tab_mvf && availableFlagLXCol == 0) {
  253. xPCtr = x0 + (nPbW >> 1);
  254. yPCtr = y0 + (nPbH >> 1);
  255. xPCtr = ((xPCtr >> 4) << 4);
  256. yPCtr = ((yPCtr >> 4) << 4);
  257. xPCtr_pu = xPCtr >> s->sps->log2_min_pu_size;
  258. yPCtr_pu = yPCtr >> s->sps->log2_min_pu_size;
  259. temp_col = TAB_MVF_PU(PCtr);
  260. availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS(PCtr);
  261. }
  262. return availableFlagLXCol;
  263. }
  264. #define AVAILABLE(cand, v) \
  265. (cand && !TAB_MVF_PU(v).is_intra)
  266. #define PRED_BLOCK_AVAILABLE(v) \
  267. check_prediction_block_available(s, log2_cb_size, \
  268. x0, y0, nPbW, nPbH, \
  269. x##v, y##v, part_idx)
  270. #define COMPARE_MV_REFIDX(a, b) \
  271. compareMVrefidx(TAB_MVF_PU(a), TAB_MVF_PU(b))
  272. /*
  273. * 8.5.3.1.2 Derivation process for spatial merging candidates
  274. */
  275. static void derive_spatial_merge_candidates(HEVCContext *s, int x0, int y0,
  276. int nPbW, int nPbH, int log2_cb_size,
  277. int singleMCLFlag, int part_idx,
  278. struct MvField mergecandlist[])
  279. {
  280. HEVCLocalContext *lc = &s->HEVClc;
  281. RefPicList *refPicList = s->ref->refPicList;
  282. MvField *tab_mvf = s->ref->tab_mvf;
  283. int available_a1_flag = 0;
  284. int available_b1_flag = 0;
  285. int available_b0_flag = 0;
  286. int available_a0_flag = 0;
  287. int available_b2_flag = 0;
  288. struct MvField spatialCMVS[MRG_MAX_NUM_CANDS];
  289. struct MvField combCand = { { { 0 } } };
  290. struct MvField TMVPCand = { { { 0 } } };
  291. struct Mv mvL0Col = { 0 };
  292. struct Mv mvL1Col = { 0 };
  293. //first left spatial merge candidate
  294. int xA1 = x0 - 1;
  295. int yA1 = y0 + nPbH - 1;
  296. int is_available_a1;
  297. int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
  298. int check_MER = 1;
  299. int check_MER_1 = 1;
  300. int xB1, yB1;
  301. int is_available_b1;
  302. int xB1_pu;
  303. int yB1_pu;
  304. int check_B0;
  305. int xB0, yB0;
  306. int isAvailableB0;
  307. int xB0_pu;
  308. int yB0_pu;
  309. int check_A0;
  310. int xA0, yA0;
  311. int is_available_a0;
  312. int xA0_pu;
  313. int yA0_pu;
  314. int xB2, yB2;
  315. int isAvailableB2;
  316. int xB2_pu;
  317. int yB2_pu;
  318. int mergearray_index = 0;
  319. struct MvField zerovector;
  320. int numRefIdx = 0;
  321. int zeroIdx = 0;
  322. int numMergeCand = 0;
  323. int numOrigMergeCand = 0;
  324. int sumcandidates = 0;
  325. int combIdx = 0;
  326. int combStop = 0;
  327. int l0CandIdx = 0;
  328. int l1CandIdx = 0;
  329. int refIdxL0Col = 0;
  330. int refIdxL1Col = 0;
  331. int availableFlagLXCol = 0;
  332. int cand_bottom_left = lc->na.cand_bottom_left;
  333. int cand_left = lc->na.cand_left;
  334. int cand_up_left = lc->na.cand_up_left;
  335. int cand_up = lc->na.cand_up;
  336. int cand_up_right = lc->na.cand_up_right_sap;
  337. int xA1_pu = xA1 >> s->sps->log2_min_pu_size;
  338. int yA1_pu = yA1 >> s->sps->log2_min_pu_size;
  339. int availableFlagL0Col = 0;
  340. int availableFlagL1Col = 0;
  341. is_available_a1 = AVAILABLE(cand_left, A1);
  342. if (!singleMCLFlag && part_idx == 1 &&
  343. (lc->cu.part_mode == PART_Nx2N ||
  344. lc->cu.part_mode == PART_nLx2N ||
  345. lc->cu.part_mode == PART_nRx2N) ||
  346. isDiffMER(s, xA1, yA1, x0, y0)) {
  347. is_available_a1 = 0;
  348. }
  349. if (is_available_a1) {
  350. available_a1_flag = 1;
  351. spatialCMVS[0] = TAB_MVF_PU(A1);
  352. } else {
  353. available_a1_flag = 0;
  354. spatialCMVS[0].ref_idx[0] = -1;
  355. spatialCMVS[0].ref_idx[1] = -1;
  356. spatialCMVS[0].mv[0].x = 0;
  357. spatialCMVS[0].mv[0].y = 0;
  358. spatialCMVS[0].mv[1].x = 0;
  359. spatialCMVS[0].mv[1].y = 0;
  360. spatialCMVS[0].pred_flag[0] = 0;
  361. spatialCMVS[0].pred_flag[1] = 0;
  362. spatialCMVS[0].is_intra = 0;
  363. }
  364. // above spatial merge candidate
  365. xB1 = x0 + nPbW - 1;
  366. yB1 = y0 - 1;
  367. xB1_pu = xB1 >> s->sps->log2_min_pu_size;
  368. yB1_pu = yB1 >> s->sps->log2_min_pu_size;
  369. is_available_b1 = AVAILABLE(cand_up, B1);
  370. if (!singleMCLFlag && part_idx == 1 &&
  371. (lc->cu.part_mode == PART_2NxN ||
  372. lc->cu.part_mode == PART_2NxnU ||
  373. lc->cu.part_mode == PART_2NxnD) ||
  374. isDiffMER(s, xB1, yB1, x0, y0)) {
  375. is_available_b1 = 0;
  376. }
  377. if (is_available_a1 && is_available_b1)
  378. check_MER = !COMPARE_MV_REFIDX(B1, A1);
  379. if (is_available_b1 && check_MER) {
  380. available_b1_flag = 1;
  381. spatialCMVS[1] = TAB_MVF_PU(B1);
  382. } else {
  383. available_b1_flag = 0;
  384. spatialCMVS[1].ref_idx[0] = -1;
  385. spatialCMVS[1].ref_idx[1] = -1;
  386. spatialCMVS[1].mv[0].x = 0;
  387. spatialCMVS[1].mv[0].y = 0;
  388. spatialCMVS[1].mv[1].x = 0;
  389. spatialCMVS[1].mv[1].y = 0;
  390. spatialCMVS[1].pred_flag[0] = 0;
  391. spatialCMVS[1].pred_flag[1] = 0;
  392. spatialCMVS[1].is_intra = 0;
  393. }
  394. // above right spatial merge candidate
  395. xB0 = x0 + nPbW;
  396. yB0 = y0 - 1;
  397. check_MER = 1;
  398. xB0_pu = xB0 >> s->sps->log2_min_pu_size;
  399. yB0_pu = yB0 >> s->sps->log2_min_pu_size;
  400. check_B0 = PRED_BLOCK_AVAILABLE(B0);
  401. isAvailableB0 = check_B0 && AVAILABLE(cand_up_right, B0);
  402. if (isDiffMER(s, xB0, yB0, x0, y0))
  403. isAvailableB0 = 0;
  404. if (is_available_b1 && isAvailableB0)
  405. check_MER = !COMPARE_MV_REFIDX(B0, B1);
  406. if (isAvailableB0 && check_MER) {
  407. available_b0_flag = 1;
  408. spatialCMVS[2] = TAB_MVF_PU(B0);
  409. } else {
  410. available_b0_flag = 0;
  411. spatialCMVS[2].ref_idx[0] = -1;
  412. spatialCMVS[2].ref_idx[1] = -1;
  413. spatialCMVS[2].mv[0].x = 0;
  414. spatialCMVS[2].mv[0].y = 0;
  415. spatialCMVS[2].mv[1].x = 0;
  416. spatialCMVS[2].mv[1].y = 0;
  417. spatialCMVS[2].pred_flag[0] = 0;
  418. spatialCMVS[2].pred_flag[1] = 0;
  419. spatialCMVS[2].is_intra = 0;
  420. }
  421. // left bottom spatial merge candidate
  422. xA0 = x0 - 1;
  423. yA0 = y0 + nPbH;
  424. check_MER = 1;
  425. xA0_pu = xA0 >> s->sps->log2_min_pu_size;
  426. yA0_pu = yA0 >> s->sps->log2_min_pu_size;
  427. check_A0 = PRED_BLOCK_AVAILABLE(A0);
  428. is_available_a0 = check_A0 && AVAILABLE(cand_bottom_left, A0);
  429. if (isDiffMER(s, xA0, yA0, x0, y0))
  430. is_available_a0 = 0;
  431. if (is_available_a1 && is_available_a0)
  432. check_MER = !COMPARE_MV_REFIDX(A0, A1);
  433. if (is_available_a0 && check_MER) {
  434. available_a0_flag = 1;
  435. spatialCMVS[3] = TAB_MVF_PU(A0);
  436. } else {
  437. available_a0_flag = 0;
  438. spatialCMVS[3].ref_idx[0] = -1;
  439. spatialCMVS[3].ref_idx[1] = -1;
  440. spatialCMVS[3].mv[0].x = 0;
  441. spatialCMVS[3].mv[0].y = 0;
  442. spatialCMVS[3].mv[1].x = 0;
  443. spatialCMVS[3].mv[1].y = 0;
  444. spatialCMVS[3].pred_flag[0] = 0;
  445. spatialCMVS[3].pred_flag[1] = 0;
  446. spatialCMVS[3].is_intra = 0;
  447. }
  448. // above left spatial merge candidate
  449. xB2 = x0 - 1;
  450. yB2 = y0 - 1;
  451. check_MER = 1;
  452. xB2_pu = xB2 >> s->sps->log2_min_pu_size;
  453. yB2_pu = yB2 >> s->sps->log2_min_pu_size;
  454. isAvailableB2 = AVAILABLE(cand_up_left, B2);
  455. if (isDiffMER(s, xB2, yB2, x0, y0))
  456. isAvailableB2 = 0;
  457. if (is_available_a1 && isAvailableB2)
  458. check_MER = !COMPARE_MV_REFIDX(B2, A1);
  459. if (is_available_b1 && isAvailableB2)
  460. check_MER_1 = !COMPARE_MV_REFIDX(B2, B1);
  461. sumcandidates = available_a1_flag + available_b1_flag + available_b0_flag
  462. + available_a0_flag;
  463. if (isAvailableB2 && check_MER && check_MER_1 && sumcandidates != 4) {
  464. available_b2_flag = 1;
  465. spatialCMVS[4] = TAB_MVF_PU(B2);
  466. } else {
  467. available_b2_flag = 0;
  468. spatialCMVS[4].ref_idx[0] = -1;
  469. spatialCMVS[4].ref_idx[1] = -1;
  470. spatialCMVS[4].mv[0].x = 0;
  471. spatialCMVS[4].mv[0].y = 0;
  472. spatialCMVS[4].mv[1].x = 0;
  473. spatialCMVS[4].mv[1].y = 0;
  474. spatialCMVS[4].pred_flag[0] = 0;
  475. spatialCMVS[4].pred_flag[1] = 0;
  476. spatialCMVS[4].is_intra = 0;
  477. }
  478. // temporal motion vector candidate
  479. // one optimization is that do temporal checking only if the number of
  480. // available candidates < MRG_MAX_NUM_CANDS
  481. if (s->sh.slice_temporal_mvp_enabled_flag == 0) {
  482. availableFlagLXCol = 0;
  483. } else {
  484. availableFlagL0Col = temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
  485. refIdxL0Col, &mvL0Col, 0);
  486. // one optimization is that l1 check can be done only when the current slice type is B_SLICE
  487. if (s->sh.slice_type == B_SLICE) {
  488. availableFlagL1Col = temporal_luma_motion_vector(s, x0, y0, nPbW,
  489. nPbH, refIdxL1Col, &mvL1Col, 1);
  490. }
  491. availableFlagLXCol = availableFlagL0Col || availableFlagL1Col;
  492. if (availableFlagLXCol) {
  493. TMVPCand.is_intra = 0;
  494. TMVPCand.pred_flag[0] = availableFlagL0Col;
  495. TMVPCand.pred_flag[1] = availableFlagL1Col;
  496. if (TMVPCand.pred_flag[0]) {
  497. TMVPCand.mv[0] = mvL0Col;
  498. TMVPCand.ref_idx[0] = refIdxL0Col;
  499. }
  500. if (TMVPCand.pred_flag[1]) {
  501. TMVPCand.mv[1] = mvL1Col;
  502. TMVPCand.ref_idx[1] = refIdxL1Col;
  503. }
  504. }
  505. }
  506. if (available_a1_flag) {
  507. mergecandlist[mergearray_index] = spatialCMVS[0];
  508. mergearray_index++;
  509. }
  510. if (available_b1_flag) {
  511. mergecandlist[mergearray_index] = spatialCMVS[1];
  512. mergearray_index++;
  513. }
  514. if (available_b0_flag) {
  515. mergecandlist[mergearray_index] = spatialCMVS[2];
  516. mergearray_index++;
  517. }
  518. if (available_a0_flag) {
  519. mergecandlist[mergearray_index] = spatialCMVS[3];
  520. mergearray_index++;
  521. }
  522. if (available_b2_flag) {
  523. mergecandlist[mergearray_index] = spatialCMVS[4];
  524. mergearray_index++;
  525. }
  526. if (availableFlagLXCol && mergearray_index < s->sh.max_num_merge_cand) {
  527. mergecandlist[mergearray_index] = TMVPCand;
  528. mergearray_index++;
  529. }
  530. numMergeCand = mergearray_index;
  531. numOrigMergeCand = mergearray_index;
  532. // combined bi-predictive merge candidates (applies for B slices)
  533. if (s->sh.slice_type == B_SLICE) {
  534. if (numOrigMergeCand > 1 &&
  535. numOrigMergeCand < s->sh.max_num_merge_cand) {
  536. combIdx = 0;
  537. combStop = 0;
  538. while (combStop != 1) {
  539. MvField l0Cand;
  540. MvField l1Cand;
  541. l0CandIdx = l0_l1_cand_idx[combIdx][0];
  542. l1CandIdx = l0_l1_cand_idx[combIdx][1];
  543. l0Cand = mergecandlist[l0CandIdx];
  544. l1Cand = mergecandlist[l1CandIdx];
  545. if (l0Cand.pred_flag[0] == 1 &&
  546. l1Cand.pred_flag[1] == 1 &&
  547. (refPicList[0].list[l0Cand.ref_idx[0]] !=
  548. refPicList[1].list[l1Cand.ref_idx[1]] ||
  549. l0Cand.mv[0].x != l1Cand.mv[1].x ||
  550. l0Cand.mv[0].y != l1Cand.mv[1].y)) {
  551. combCand.ref_idx[0] = l0Cand.ref_idx[0];
  552. combCand.ref_idx[1] = l1Cand.ref_idx[1];
  553. combCand.pred_flag[0] = 1;
  554. combCand.pred_flag[1] = 1;
  555. combCand.mv[0].x = l0Cand.mv[0].x;
  556. combCand.mv[0].y = l0Cand.mv[0].y;
  557. combCand.mv[1].x = l1Cand.mv[1].x;
  558. combCand.mv[1].y = l1Cand.mv[1].y;
  559. combCand.is_intra = 0;
  560. mergecandlist[numMergeCand] = combCand;
  561. numMergeCand++;
  562. }
  563. combIdx++;
  564. if (combIdx == numOrigMergeCand * (numOrigMergeCand - 1) ||
  565. numMergeCand == s->sh.max_num_merge_cand)
  566. combStop = 1;
  567. }
  568. }
  569. }
  570. /*
  571. * append Zero motion vector candidates
  572. */
  573. if (s->sh.slice_type == P_SLICE) {
  574. numRefIdx = s->sh.nb_refs[0];
  575. } else if (s->sh.slice_type == B_SLICE) {
  576. numRefIdx = FFMIN(s->sh.nb_refs[0],
  577. s->sh.nb_refs[1]);
  578. }
  579. while (numMergeCand < s->sh.max_num_merge_cand) {
  580. if (s->sh.slice_type == P_SLICE) {
  581. zerovector.ref_idx[0] = (zeroIdx < numRefIdx) ? zeroIdx : 0;
  582. zerovector.ref_idx[1] = -1;
  583. zerovector.pred_flag[0] = 1;
  584. zerovector.pred_flag[1] = 0;
  585. zerovector.mv[0].x = 0;
  586. zerovector.mv[0].y = 0;
  587. zerovector.mv[1].x = 0;
  588. zerovector.mv[1].y = 0;
  589. zerovector.is_intra = 0;
  590. } else {
  591. zerovector.ref_idx[0] = (zeroIdx < numRefIdx) ? zeroIdx : 0;
  592. zerovector.ref_idx[1] = (zeroIdx < numRefIdx) ? zeroIdx : 0;
  593. zerovector.pred_flag[0] = 1;
  594. zerovector.pred_flag[1] = 1;
  595. zerovector.mv[0].x = 0;
  596. zerovector.mv[0].y = 0;
  597. zerovector.mv[1].x = 0;
  598. zerovector.mv[1].y = 0;
  599. zerovector.is_intra = 0;
  600. }
  601. mergecandlist[numMergeCand] = zerovector;
  602. numMergeCand++;
  603. zeroIdx++;
  604. }
  605. }
  606. /*
  607. * 8.5.3.1.1 Derivation process of luma Mvs for merge mode
  608. */
  609. void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0, int nPbW,
  610. int nPbH, int log2_cb_size, int part_idx,
  611. int merge_idx, MvField *mv)
  612. {
  613. int singleMCLFlag = 0;
  614. int nCS = 1 << log2_cb_size;
  615. struct MvField mergecand_list[MRG_MAX_NUM_CANDS] = { { { { 0 } } } };
  616. int nPbW2 = nPbW;
  617. int nPbH2 = nPbH;
  618. HEVCLocalContext *lc = &s->HEVClc;
  619. if (s->pps->log2_parallel_merge_level > 2 && nCS == 8) {
  620. singleMCLFlag = 1;
  621. x0 = lc->cu.x;
  622. y0 = lc->cu.y;
  623. nPbW = nCS;
  624. nPbH = nCS;
  625. part_idx = 0;
  626. }
  627. ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
  628. derive_spatial_merge_candidates(s, x0, y0, nPbW, nPbH, log2_cb_size,
  629. singleMCLFlag, part_idx, mergecand_list);
  630. if (mergecand_list[merge_idx].pred_flag[0] == 1 &&
  631. mergecand_list[merge_idx].pred_flag[1] == 1 &&
  632. (nPbW2 + nPbH2) == 12) {
  633. mergecand_list[merge_idx].ref_idx[1] = -1;
  634. mergecand_list[merge_idx].pred_flag[1] = 0;
  635. }
  636. *mv = mergecand_list[merge_idx];
  637. }
  638. static av_always_inline void dist_scale(HEVCContext *s, Mv * mv,
  639. int pic_width_in_min_pu, int x, int y,
  640. int elist, int ref_idx_curr, int ref_idx)
  641. {
  642. RefPicList *refPicList = s->ref->refPicList;
  643. MvField *tab_mvf = s->ref->tab_mvf;
  644. int ref_pic_elist = refPicList[elist].list[TAB_MVF(x, y).ref_idx[elist]];
  645. int ref_pic_curr = refPicList[ref_idx_curr].list[ref_idx];
  646. if (ref_pic_elist != ref_pic_curr)
  647. mv_scale(mv, mv, s->poc - ref_pic_elist, s->poc - ref_pic_curr);
  648. }
  649. static int mv_mp_mode_mx(HEVCContext *s, int x, int y, int pred_flag_index,
  650. Mv *mv, int ref_idx_curr, int ref_idx)
  651. {
  652. MvField *tab_mvf = s->ref->tab_mvf;
  653. int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
  654. RefPicList *refPicList = s->ref->refPicList;
  655. if (TAB_MVF(x, y).pred_flag[pred_flag_index] == 1 &&
  656. refPicList[pred_flag_index].list[TAB_MVF(x, y).ref_idx[pred_flag_index]] == refPicList[ref_idx_curr].list[ref_idx]) {
  657. *mv = TAB_MVF(x, y).mv[pred_flag_index];
  658. return 1;
  659. }
  660. return 0;
  661. }
  662. static int mv_mp_mode_mx_lt(HEVCContext *s, int x, int y, int pred_flag_index,
  663. Mv *mv, int ref_idx_curr, int ref_idx)
  664. {
  665. MvField *tab_mvf = s->ref->tab_mvf;
  666. int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
  667. RefPicList *refPicList = s->ref->refPicList;
  668. int currIsLongTerm = refPicList[ref_idx_curr].isLongTerm[ref_idx];
  669. int colIsLongTerm =
  670. refPicList[pred_flag_index].isLongTerm[(TAB_MVF(x, y).ref_idx[pred_flag_index])];
  671. if (TAB_MVF(x, y).pred_flag[pred_flag_index] && colIsLongTerm == currIsLongTerm) {
  672. *mv = TAB_MVF(x, y).mv[pred_flag_index];
  673. if (!currIsLongTerm)
  674. dist_scale(s, mv, pic_width_in_min_pu, x, y, pred_flag_index, ref_idx_curr, ref_idx);
  675. return 1;
  676. }
  677. return 0;
  678. }
  679. #define MP_MX(v, pred, mx) \
  680. mv_mp_mode_mx(s, x##v##_pu, y##v##_pu, pred, &mx, ref_idx_curr, ref_idx)
  681. #define MP_MX_LT(v, pred, mx) \
  682. mv_mp_mode_mx_lt(s, x##v##_pu, y##v##_pu, pred, &mx, ref_idx_curr, ref_idx)
  683. void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
  684. int nPbH, int log2_cb_size, int part_idx,
  685. int merge_idx, MvField *mv,
  686. int mvp_lx_flag, int LX)
  687. {
  688. HEVCLocalContext *lc = &s->HEVClc;
  689. MvField *tab_mvf = s->ref->tab_mvf;
  690. int isScaledFlag_L0 = 0;
  691. int availableFlagLXA0 = 0;
  692. int availableFlagLXB0 = 0;
  693. int availableFlagLXCol = 0;
  694. int numMVPCandLX = 0;
  695. int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
  696. int xA0, yA0;
  697. int xA0_pu, yA0_pu;
  698. int is_available_a0;
  699. int xA1, yA1;
  700. int xA1_pu, yA1_pu;
  701. int is_available_a1;
  702. int xB0, yB0;
  703. int xB0_pu, yB0_pu;
  704. int is_available_b0;
  705. int xB1, yB1;
  706. int xB1_pu = 0, yB1_pu = 0;
  707. int is_available_b1 = 0;
  708. int xB2, yB2;
  709. int xB2_pu = 0, yB2_pu = 0;
  710. int is_available_b2 = 0;
  711. Mv mvpcand_list[2] = { { 0 } };
  712. Mv mxA = { 0 };
  713. Mv mxB = { 0 };
  714. Mv mvLXCol = { 0 };
  715. int ref_idx_curr = 0;
  716. int ref_idx = 0;
  717. int pred_flag_index_l0;
  718. int pred_flag_index_l1;
  719. int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
  720. int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
  721. int cand_up = (lc->ctb_up_flag || y0b);
  722. int cand_left = (lc->ctb_left_flag || x0b);
  723. int cand_up_left =
  724. (!x0b && !y0b) ? lc->ctb_up_left_flag : cand_left && cand_up;
  725. int cand_up_right =
  726. (x0b + nPbW == (1 << s->sps->log2_ctb_size) ||
  727. x0 + nPbW >= lc->end_of_tiles_x) ? lc->ctb_up_right_flag && !y0b
  728. : cand_up;
  729. int cand_bottom_left = (y0 + nPbH >= lc->end_of_tiles_y) ? 0 : cand_left;
  730. ref_idx_curr = LX;
  731. ref_idx = mv->ref_idx[LX];
  732. pred_flag_index_l0 = LX;
  733. pred_flag_index_l1 = !LX;
  734. // left bottom spatial candidate
  735. xA0 = x0 - 1;
  736. yA0 = y0 + nPbH;
  737. xA0_pu = xA0 >> s->sps->log2_min_pu_size;
  738. yA0_pu = yA0 >> s->sps->log2_min_pu_size;
  739. is_available_a0 = PRED_BLOCK_AVAILABLE(A0) && AVAILABLE(cand_bottom_left, A0);
  740. //left spatial merge candidate
  741. xA1 = x0 - 1;
  742. yA1 = y0 + nPbH - 1;
  743. xA1_pu = xA1 >> s->sps->log2_min_pu_size;
  744. yA1_pu = yA1 >> s->sps->log2_min_pu_size;
  745. is_available_a1 = AVAILABLE(cand_left, A1);
  746. if (is_available_a0 || is_available_a1) {
  747. isScaledFlag_L0 = 1;
  748. }
  749. if (is_available_a0) {
  750. availableFlagLXA0 = MP_MX(A0, pred_flag_index_l0, mxA);
  751. if (!availableFlagLXA0)
  752. availableFlagLXA0 = MP_MX(A0, pred_flag_index_l1, mxA);
  753. }
  754. if (is_available_a1 && !availableFlagLXA0) {
  755. availableFlagLXA0 = MP_MX(A1, pred_flag_index_l0, mxA);
  756. if (!availableFlagLXA0)
  757. availableFlagLXA0 = MP_MX(A1, pred_flag_index_l1, mxA);
  758. }
  759. if (is_available_a0 && !availableFlagLXA0) {
  760. availableFlagLXA0 = MP_MX_LT(A0, pred_flag_index_l0, mxA);
  761. if (!availableFlagLXA0)
  762. availableFlagLXA0 = MP_MX_LT(A0, pred_flag_index_l1, mxA);
  763. }
  764. if (is_available_a1 && !availableFlagLXA0) {
  765. availableFlagLXA0 = MP_MX_LT(A1, pred_flag_index_l0, mxA);
  766. if (!availableFlagLXA0)
  767. availableFlagLXA0 = MP_MX_LT(A1, pred_flag_index_l1, mxA);
  768. }
  769. // B candidates
  770. // above right spatial merge candidate
  771. xB0 = x0 + nPbW;
  772. yB0 = y0 - 1;
  773. xB0_pu = xB0 >> s->sps->log2_min_pu_size;
  774. yB0_pu = yB0 >> s->sps->log2_min_pu_size;
  775. is_available_b0 = PRED_BLOCK_AVAILABLE(B0) && AVAILABLE(cand_up_right, B0);
  776. if (is_available_b0) {
  777. availableFlagLXB0 = MP_MX(B0, pred_flag_index_l0, mxB);
  778. if (!availableFlagLXB0)
  779. availableFlagLXB0 = MP_MX(B0, pred_flag_index_l1, mxB);
  780. }
  781. if (!availableFlagLXB0) {
  782. // above spatial merge candidate
  783. xB1 = x0 + nPbW - 1;
  784. yB1 = y0 - 1;
  785. xB1_pu = xB1 >> s->sps->log2_min_pu_size;
  786. yB1_pu = yB1 >> s->sps->log2_min_pu_size;
  787. is_available_b1 = AVAILABLE(cand_up, B1);
  788. if (is_available_b1) {
  789. availableFlagLXB0 = MP_MX(B1, pred_flag_index_l0, mxB);
  790. if (!availableFlagLXB0)
  791. availableFlagLXB0 = MP_MX(B1, pred_flag_index_l1, mxB);
  792. }
  793. }
  794. if (!availableFlagLXB0) {
  795. // above left spatial merge candidate
  796. xB2 = x0 - 1;
  797. yB2 = y0 - 1;
  798. xB2_pu = xB2 >> s->sps->log2_min_pu_size;
  799. yB2_pu = yB2 >> s->sps->log2_min_pu_size;
  800. is_available_b2 = AVAILABLE(cand_up_left, B2);
  801. if (is_available_b2) {
  802. availableFlagLXB0 = MP_MX(B2, pred_flag_index_l0, mxB);
  803. if (!availableFlagLXB0)
  804. availableFlagLXB0 = MP_MX(B2, pred_flag_index_l1, mxB);
  805. }
  806. }
  807. if (isScaledFlag_L0 == 0) {
  808. if (availableFlagLXB0) {
  809. availableFlagLXA0 = 1;
  810. mxA = mxB;
  811. }
  812. availableFlagLXB0 = 0;
  813. // XB0 and L1
  814. if (is_available_b0) {
  815. availableFlagLXB0 = MP_MX_LT(B0, pred_flag_index_l0, mxB);
  816. if (!availableFlagLXB0)
  817. availableFlagLXB0 = MP_MX_LT(B0, pred_flag_index_l1, mxB);
  818. }
  819. if (is_available_b1 && !availableFlagLXB0) {
  820. availableFlagLXB0 = MP_MX_LT(B1, pred_flag_index_l0, mxB);
  821. if (!availableFlagLXB0)
  822. availableFlagLXB0 = MP_MX_LT(B1, pred_flag_index_l1, mxB);
  823. }
  824. if (is_available_b2 && !availableFlagLXB0) {
  825. availableFlagLXB0 = MP_MX_LT(B2, pred_flag_index_l0, mxB);
  826. if (!availableFlagLXB0)
  827. availableFlagLXB0 = MP_MX_LT(B2, pred_flag_index_l1, mxB);
  828. }
  829. }
  830. if (availableFlagLXA0 && availableFlagLXB0 &&
  831. (mxA.x != mxB.x || mxA.y != mxB.y)) {
  832. availableFlagLXCol = 0;
  833. } else {
  834. //temporal motion vector prediction candidate
  835. if (s->sh.slice_temporal_mvp_enabled_flag == 0) {
  836. availableFlagLXCol = 0;
  837. } else {
  838. availableFlagLXCol = temporal_luma_motion_vector(s, x0, y0, nPbW,
  839. nPbH, ref_idx, &mvLXCol, LX);
  840. }
  841. }
  842. if (availableFlagLXA0) {
  843. mvpcand_list[numMVPCandLX] = mxA;
  844. numMVPCandLX++;
  845. }
  846. if (availableFlagLXB0) {
  847. mvpcand_list[numMVPCandLX] = mxB;
  848. numMVPCandLX++;
  849. }
  850. if (availableFlagLXA0 && availableFlagLXB0 &&
  851. mxA.x == mxB.x && mxA.y == mxB.y) {
  852. numMVPCandLX--;
  853. }
  854. if (availableFlagLXCol && numMVPCandLX < 2) {
  855. mvpcand_list[numMVPCandLX] = mvLXCol;
  856. numMVPCandLX++;
  857. }
  858. while (numMVPCandLX < 2) { // insert zero motion vectors when the number of available candidates are less than 2
  859. mvpcand_list[numMVPCandLX].x = 0;
  860. mvpcand_list[numMVPCandLX].y = 0;
  861. numMVPCandLX++;
  862. }
  863. mv->mv[LX].x = mvpcand_list[mvp_lx_flag].x;
  864. mv->mv[LX].y = mvpcand_list[mvp_lx_flag].y;
  865. }