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.

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