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.

831 lines
34KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... motion vector predicion
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 motion vector predicion.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #ifndef AVCODEC_H264_MVPRED_H
  27. #define AVCODEC_H264_MVPRED_H
  28. #include "internal.h"
  29. #include "avcodec.h"
  30. #include "h264.h"
  31. #include "mpegutils.h"
  32. #include <assert.h>
  33. static av_always_inline int fetch_diagonal_mv(H264Context *h, const int16_t **C,
  34. int i, int list, int part_width)
  35. {
  36. const int topright_ref = h->ref_cache[list][i - 8 + part_width];
  37. /* there is no consistent mapping of mvs to neighboring locations that will
  38. * make mbaff happy, so we can't move all this logic to fill_caches */
  39. if (FRAME_MBAFF(h)) {
  40. #define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4) \
  41. const int xy = XY, y4 = Y4; \
  42. const int mb_type = mb_types[xy + (y4 >> 2) * h->mb_stride]; \
  43. if (!USES_LIST(mb_type, list)) \
  44. return LIST_NOT_USED; \
  45. mv = h->cur_pic_ptr->motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
  46. h->mv_cache[list][scan8[0] - 2][0] = mv[0]; \
  47. h->mv_cache[list][scan8[0] - 2][1] = mv[1] MV_OP; \
  48. return h->cur_pic_ptr->ref_index[list][4 * xy + 1 + (y4 & ~1)] REF_OP;
  49. if (topright_ref == PART_NOT_AVAILABLE
  50. && i >= scan8[0] + 8 && (i & 7) == 4
  51. && h->ref_cache[list][scan8[0] - 1] != PART_NOT_AVAILABLE) {
  52. const uint32_t *mb_types = h->cur_pic_ptr->mb_type;
  53. const int16_t *mv;
  54. AV_ZERO32(h->mv_cache[list][scan8[0] - 2]);
  55. *C = h->mv_cache[list][scan8[0] - 2];
  56. if (!MB_FIELD(h) && IS_INTERLACED(h->left_type[0])) {
  57. SET_DIAG_MV(* 2, >> 1, h->left_mb_xy[0] + h->mb_stride,
  58. (h->mb_y & 1) * 2 + (i >> 5));
  59. }
  60. if (MB_FIELD(h) && !IS_INTERLACED(h->left_type[0])) {
  61. // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
  62. SET_DIAG_MV(/ 2, << 1, h->left_mb_xy[i >= 36], ((i >> 2)) & 3);
  63. }
  64. }
  65. #undef SET_DIAG_MV
  66. }
  67. if (topright_ref != PART_NOT_AVAILABLE) {
  68. *C = h->mv_cache[list][i - 8 + part_width];
  69. return topright_ref;
  70. } else {
  71. tprintf(h->avctx, "topright MV not available\n");
  72. *C = h->mv_cache[list][i - 8 - 1];
  73. return h->ref_cache[list][i - 8 - 1];
  74. }
  75. }
  76. /**
  77. * Get the predicted MV.
  78. * @param n the block index
  79. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  80. * @param mx the x component of the predicted motion vector
  81. * @param my the y component of the predicted motion vector
  82. */
  83. static av_always_inline void pred_motion(H264Context *const h, int n,
  84. int part_width, int list, int ref,
  85. int *const mx, int *const my)
  86. {
  87. const int index8 = scan8[n];
  88. const int top_ref = h->ref_cache[list][index8 - 8];
  89. const int left_ref = h->ref_cache[list][index8 - 1];
  90. const int16_t *const A = h->mv_cache[list][index8 - 1];
  91. const int16_t *const B = h->mv_cache[list][index8 - 8];
  92. const int16_t *C;
  93. int diagonal_ref, match_count;
  94. assert(part_width == 1 || part_width == 2 || part_width == 4);
  95. /* mv_cache
  96. * B . . A T T T T
  97. * U . . L . . , .
  98. * U . . L . . . .
  99. * U . . L . . , .
  100. * . . . L . . . .
  101. */
  102. diagonal_ref = fetch_diagonal_mv(h, &C, index8, list, part_width);
  103. match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
  104. tprintf(h->avctx, "pred_motion match_count=%d\n", match_count);
  105. if (match_count > 1) { //most common
  106. *mx = mid_pred(A[0], B[0], C[0]);
  107. *my = mid_pred(A[1], B[1], C[1]);
  108. } else if (match_count == 1) {
  109. if (left_ref == ref) {
  110. *mx = A[0];
  111. *my = A[1];
  112. } else if (top_ref == ref) {
  113. *mx = B[0];
  114. *my = B[1];
  115. } else {
  116. *mx = C[0];
  117. *my = C[1];
  118. }
  119. } else {
  120. if (top_ref == PART_NOT_AVAILABLE &&
  121. diagonal_ref == PART_NOT_AVAILABLE &&
  122. left_ref != PART_NOT_AVAILABLE) {
  123. *mx = A[0];
  124. *my = A[1];
  125. } else {
  126. *mx = mid_pred(A[0], B[0], C[0]);
  127. *my = mid_pred(A[1], B[1], C[1]);
  128. }
  129. }
  130. tprintf(h->avctx,
  131. "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n",
  132. top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref,
  133. A[0], A[1], ref, *mx, *my, h->mb_x, h->mb_y, n, list);
  134. }
  135. /**
  136. * Get the directionally predicted 16x8 MV.
  137. * @param n the block index
  138. * @param mx the x component of the predicted motion vector
  139. * @param my the y component of the predicted motion vector
  140. */
  141. static av_always_inline void pred_16x8_motion(H264Context *const h,
  142. int n, int list, int ref,
  143. int *const mx, int *const my)
  144. {
  145. if (n == 0) {
  146. const int top_ref = h->ref_cache[list][scan8[0] - 8];
  147. const int16_t *const B = h->mv_cache[list][scan8[0] - 8];
  148. tprintf(h->avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
  149. top_ref, B[0], B[1], h->mb_x, h->mb_y, n, list);
  150. if (top_ref == ref) {
  151. *mx = B[0];
  152. *my = B[1];
  153. return;
  154. }
  155. } else {
  156. const int left_ref = h->ref_cache[list][scan8[8] - 1];
  157. const int16_t *const A = h->mv_cache[list][scan8[8] - 1];
  158. tprintf(h->avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
  159. left_ref, A[0], A[1], h->mb_x, h->mb_y, n, list);
  160. if (left_ref == ref) {
  161. *mx = A[0];
  162. *my = A[1];
  163. return;
  164. }
  165. }
  166. //RARE
  167. pred_motion(h, n, 4, list, ref, mx, my);
  168. }
  169. /**
  170. * Get the directionally predicted 8x16 MV.
  171. * @param n the block index
  172. * @param mx the x component of the predicted motion vector
  173. * @param my the y component of the predicted motion vector
  174. */
  175. static av_always_inline void pred_8x16_motion(H264Context *const h,
  176. int n, int list, int ref,
  177. int *const mx, int *const my)
  178. {
  179. if (n == 0) {
  180. const int left_ref = h->ref_cache[list][scan8[0] - 1];
  181. const int16_t *const A = h->mv_cache[list][scan8[0] - 1];
  182. tprintf(h->avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
  183. left_ref, A[0], A[1], h->mb_x, h->mb_y, n, list);
  184. if (left_ref == ref) {
  185. *mx = A[0];
  186. *my = A[1];
  187. return;
  188. }
  189. } else {
  190. const int16_t *C;
  191. int diagonal_ref;
  192. diagonal_ref = fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  193. tprintf(h->avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
  194. diagonal_ref, C[0], C[1], h->mb_x, h->mb_y, n, list);
  195. if (diagonal_ref == ref) {
  196. *mx = C[0];
  197. *my = C[1];
  198. return;
  199. }
  200. }
  201. //RARE
  202. pred_motion(h, n, 2, list, ref, mx, my);
  203. }
  204. #define FIX_MV_MBAFF(type, refn, mvn, idx) \
  205. if (FRAME_MBAFF(h)) { \
  206. if (MB_FIELD(h)) { \
  207. if (!IS_INTERLACED(type)) { \
  208. refn <<= 1; \
  209. AV_COPY32(mvbuf[idx], mvn); \
  210. mvbuf[idx][1] /= 2; \
  211. mvn = mvbuf[idx]; \
  212. } \
  213. } else { \
  214. if (IS_INTERLACED(type)) { \
  215. refn >>= 1; \
  216. AV_COPY32(mvbuf[idx], mvn); \
  217. mvbuf[idx][1] <<= 1; \
  218. mvn = mvbuf[idx]; \
  219. } \
  220. } \
  221. }
  222. static av_always_inline void pred_pskip_motion(H264Context *const h)
  223. {
  224. DECLARE_ALIGNED(4, static const int16_t, zeromv)[2] = { 0 };
  225. DECLARE_ALIGNED(4, int16_t, mvbuf)[3][2];
  226. int8_t *ref = h->cur_pic.ref_index[0];
  227. int16_t(*mv)[2] = h->cur_pic.motion_val[0];
  228. int top_ref, left_ref, diagonal_ref, match_count, mx, my;
  229. const int16_t *A, *B, *C;
  230. int b_stride = h->b_stride;
  231. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  232. /* To avoid doing an entire fill_decode_caches, we inline the relevant
  233. * parts here.
  234. * FIXME: this is a partial duplicate of the logic in fill_decode_caches,
  235. * but it's faster this way. Is there a way to avoid this duplication?
  236. */
  237. if (USES_LIST(h->left_type[LTOP], 0)) {
  238. left_ref = ref[4 * h->left_mb_xy[LTOP] + 1 + (h->left_block[0] & ~1)];
  239. A = mv[h->mb2b_xy[h->left_mb_xy[LTOP]] + 3 + b_stride * h->left_block[0]];
  240. FIX_MV_MBAFF(h->left_type[LTOP], left_ref, A, 0);
  241. if (!(left_ref | AV_RN32A(A)))
  242. goto zeromv;
  243. } else if (h->left_type[LTOP]) {
  244. left_ref = LIST_NOT_USED;
  245. A = zeromv;
  246. } else {
  247. goto zeromv;
  248. }
  249. if (USES_LIST(h->top_type, 0)) {
  250. top_ref = ref[4 * h->top_mb_xy + 2];
  251. B = mv[h->mb2b_xy[h->top_mb_xy] + 3 * b_stride];
  252. FIX_MV_MBAFF(h->top_type, top_ref, B, 1);
  253. if (!(top_ref | AV_RN32A(B)))
  254. goto zeromv;
  255. } else if (h->top_type) {
  256. top_ref = LIST_NOT_USED;
  257. B = zeromv;
  258. } else {
  259. goto zeromv;
  260. }
  261. tprintf(h->avctx, "pred_pskip: (%d) (%d) at %2d %2d\n",
  262. top_ref, left_ref, h->mb_x, h->mb_y);
  263. if (USES_LIST(h->topright_type, 0)) {
  264. diagonal_ref = ref[4 * h->topright_mb_xy + 2];
  265. C = mv[h->mb2b_xy[h->topright_mb_xy] + 3 * b_stride];
  266. FIX_MV_MBAFF(h->topright_type, diagonal_ref, C, 2);
  267. } else if (h->topright_type) {
  268. diagonal_ref = LIST_NOT_USED;
  269. C = zeromv;
  270. } else {
  271. if (USES_LIST(h->topleft_type, 0)) {
  272. diagonal_ref = ref[4 * h->topleft_mb_xy + 1 +
  273. (h->topleft_partition & 2)];
  274. C = mv[h->mb2b_xy[h->topleft_mb_xy] + 3 + b_stride +
  275. (h->topleft_partition & 2 * b_stride)];
  276. FIX_MV_MBAFF(h->topleft_type, diagonal_ref, C, 2);
  277. } else if (h->topleft_type) {
  278. diagonal_ref = LIST_NOT_USED;
  279. C = zeromv;
  280. } else {
  281. diagonal_ref = PART_NOT_AVAILABLE;
  282. C = zeromv;
  283. }
  284. }
  285. match_count = !diagonal_ref + !top_ref + !left_ref;
  286. tprintf(h->avctx, "pred_pskip_motion match_count=%d\n", match_count);
  287. if (match_count > 1) {
  288. mx = mid_pred(A[0], B[0], C[0]);
  289. my = mid_pred(A[1], B[1], C[1]);
  290. } else if (match_count == 1) {
  291. if (!left_ref) {
  292. mx = A[0];
  293. my = A[1];
  294. } else if (!top_ref) {
  295. mx = B[0];
  296. my = B[1];
  297. } else {
  298. mx = C[0];
  299. my = C[1];
  300. }
  301. } else {
  302. mx = mid_pred(A[0], B[0], C[0]);
  303. my = mid_pred(A[1], B[1], C[1]);
  304. }
  305. fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx, my), 4);
  306. return;
  307. zeromv:
  308. fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  309. return;
  310. }
  311. static void fill_decode_neighbors(H264Context *h, int mb_type)
  312. {
  313. const int mb_xy = h->mb_xy;
  314. int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
  315. static const uint8_t left_block_options[4][32] = {
  316. { 0, 1, 2, 3, 7, 10, 8, 11, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 5 * 4, 1 + 9 * 4 },
  317. { 2, 2, 3, 3, 8, 11, 8, 11, 3 + 2 * 4, 3 + 2 * 4, 3 + 3 * 4, 3 + 3 * 4, 1 + 5 * 4, 1 + 9 * 4, 1 + 5 * 4, 1 + 9 * 4 },
  318. { 0, 0, 1, 1, 7, 10, 7, 10, 3 + 0 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 1 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 },
  319. { 0, 2, 0, 2, 7, 10, 7, 10, 3 + 0 * 4, 3 + 2 * 4, 3 + 0 * 4, 3 + 2 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 }
  320. };
  321. h->topleft_partition = -1;
  322. top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
  323. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  324. * stuff, I can't imagine that these complex rules are worth it. */
  325. topleft_xy = top_xy - 1;
  326. topright_xy = top_xy + 1;
  327. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  328. h->left_block = left_block_options[0];
  329. if (FRAME_MBAFF(h)) {
  330. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  331. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  332. if (h->mb_y & 1) {
  333. if (left_mb_field_flag != curr_mb_field_flag) {
  334. left_xy[LBOT] = left_xy[LTOP] = mb_xy - h->mb_stride - 1;
  335. if (curr_mb_field_flag) {
  336. left_xy[LBOT] += h->mb_stride;
  337. h->left_block = left_block_options[3];
  338. } else {
  339. topleft_xy += h->mb_stride;
  340. /* take top left mv from the middle of the mb, as opposed
  341. * to all other modes which use the bottom right partition */
  342. h->topleft_partition = 0;
  343. h->left_block = left_block_options[1];
  344. }
  345. }
  346. } else {
  347. if (curr_mb_field_flag) {
  348. topleft_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy - 1] >> 7) & 1) - 1);
  349. topright_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy + 1] >> 7) & 1) - 1);
  350. top_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  351. }
  352. if (left_mb_field_flag != curr_mb_field_flag) {
  353. if (curr_mb_field_flag) {
  354. left_xy[LBOT] += h->mb_stride;
  355. h->left_block = left_block_options[3];
  356. } else {
  357. h->left_block = left_block_options[2];
  358. }
  359. }
  360. }
  361. }
  362. h->topleft_mb_xy = topleft_xy;
  363. h->top_mb_xy = top_xy;
  364. h->topright_mb_xy = topright_xy;
  365. h->left_mb_xy[LTOP] = left_xy[LTOP];
  366. h->left_mb_xy[LBOT] = left_xy[LBOT];
  367. //FIXME do we need all in the context?
  368. h->topleft_type = h->cur_pic.mb_type[topleft_xy];
  369. h->top_type = h->cur_pic.mb_type[top_xy];
  370. h->topright_type = h->cur_pic.mb_type[topright_xy];
  371. h->left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  372. h->left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  373. if (FMO) {
  374. if (h->slice_table[topleft_xy] != h->slice_num)
  375. h->topleft_type = 0;
  376. if (h->slice_table[top_xy] != h->slice_num)
  377. h->top_type = 0;
  378. if (h->slice_table[left_xy[LTOP]] != h->slice_num)
  379. h->left_type[LTOP] = h->left_type[LBOT] = 0;
  380. } else {
  381. if (h->slice_table[topleft_xy] != h->slice_num) {
  382. h->topleft_type = 0;
  383. if (h->slice_table[top_xy] != h->slice_num)
  384. h->top_type = 0;
  385. if (h->slice_table[left_xy[LTOP]] != h->slice_num)
  386. h->left_type[LTOP] = h->left_type[LBOT] = 0;
  387. }
  388. }
  389. if (h->slice_table[topright_xy] != h->slice_num)
  390. h->topright_type = 0;
  391. }
  392. static void fill_decode_caches(H264Context *h, int mb_type)
  393. {
  394. int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
  395. int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
  396. const uint8_t *left_block = h->left_block;
  397. int i;
  398. uint8_t *nnz;
  399. uint8_t *nnz_cache;
  400. topleft_xy = h->topleft_mb_xy;
  401. top_xy = h->top_mb_xy;
  402. topright_xy = h->topright_mb_xy;
  403. left_xy[LTOP] = h->left_mb_xy[LTOP];
  404. left_xy[LBOT] = h->left_mb_xy[LBOT];
  405. topleft_type = h->topleft_type;
  406. top_type = h->top_type;
  407. topright_type = h->topright_type;
  408. left_type[LTOP] = h->left_type[LTOP];
  409. left_type[LBOT] = h->left_type[LBOT];
  410. if (!IS_SKIP(mb_type)) {
  411. if (IS_INTRA(mb_type)) {
  412. int type_mask = h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
  413. h->topleft_samples_available =
  414. h->top_samples_available =
  415. h->left_samples_available = 0xFFFF;
  416. h->topright_samples_available = 0xEEEA;
  417. if (!(top_type & type_mask)) {
  418. h->topleft_samples_available = 0xB3FF;
  419. h->top_samples_available = 0x33FF;
  420. h->topright_samples_available = 0x26EA;
  421. }
  422. if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {
  423. if (IS_INTERLACED(mb_type)) {
  424. if (!(left_type[LTOP] & type_mask)) {
  425. h->topleft_samples_available &= 0xDFFF;
  426. h->left_samples_available &= 0x5FFF;
  427. }
  428. if (!(left_type[LBOT] & type_mask)) {
  429. h->topleft_samples_available &= 0xFF5F;
  430. h->left_samples_available &= 0xFF5F;
  431. }
  432. } else {
  433. int left_typei = h->cur_pic.mb_type[left_xy[LTOP] + h->mb_stride];
  434. assert(left_xy[LTOP] == left_xy[LBOT]);
  435. if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
  436. h->topleft_samples_available &= 0xDF5F;
  437. h->left_samples_available &= 0x5F5F;
  438. }
  439. }
  440. } else {
  441. if (!(left_type[LTOP] & type_mask)) {
  442. h->topleft_samples_available &= 0xDF5F;
  443. h->left_samples_available &= 0x5F5F;
  444. }
  445. }
  446. if (!(topleft_type & type_mask))
  447. h->topleft_samples_available &= 0x7FFF;
  448. if (!(topright_type & type_mask))
  449. h->topright_samples_available &= 0xFBFF;
  450. if (IS_INTRA4x4(mb_type)) {
  451. if (IS_INTRA4x4(top_type)) {
  452. AV_COPY32(h->intra4x4_pred_mode_cache + 4 + 8 * 0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
  453. } else {
  454. h->intra4x4_pred_mode_cache[4 + 8 * 0] =
  455. h->intra4x4_pred_mode_cache[5 + 8 * 0] =
  456. h->intra4x4_pred_mode_cache[6 + 8 * 0] =
  457. h->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
  458. }
  459. for (i = 0; i < 2; i++) {
  460. if (IS_INTRA4x4(left_type[LEFT(i)])) {
  461. int8_t *mode = h->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
  462. h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
  463. h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
  464. } else {
  465. h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
  466. h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);
  467. }
  468. }
  469. }
  470. }
  471. /*
  472. * 0 . T T. T T T T
  473. * 1 L . .L . . . .
  474. * 2 L . .L . . . .
  475. * 3 . T TL . . . .
  476. * 4 L . .L . . . .
  477. * 5 L . .. . . . .
  478. */
  479. /* FIXME: constraint_intra_pred & partitioning & nnz
  480. * (let us hope this is just a typo in the spec) */
  481. nnz_cache = h->non_zero_count_cache;
  482. if (top_type) {
  483. nnz = h->non_zero_count[top_xy];
  484. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
  485. if (!h->chroma_y_shift) {
  486. AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);
  487. AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
  488. } else {
  489. AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);
  490. AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
  491. }
  492. } else {
  493. uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 0x40404040;
  494. AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
  495. AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
  496. AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
  497. }
  498. for (i = 0; i < 2; i++) {
  499. if (left_type[LEFT(i)]) {
  500. nnz = h->non_zero_count[left_xy[LEFT(i)]];
  501. nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
  502. nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
  503. if (CHROMA444(h)) {
  504. nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
  505. nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
  506. nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
  507. nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
  508. } else if (CHROMA422(h)) {
  509. nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
  510. nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
  511. nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
  512. nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
  513. } else {
  514. nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
  515. nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
  516. }
  517. } else {
  518. nnz_cache[3 + 8 * 1 + 2 * 8 * i] =
  519. nnz_cache[3 + 8 * 2 + 2 * 8 * i] =
  520. nnz_cache[3 + 8 * 6 + 2 * 8 * i] =
  521. nnz_cache[3 + 8 * 7 + 2 * 8 * i] =
  522. nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
  523. nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 64;
  524. }
  525. }
  526. if (CABAC(h)) {
  527. // top_cbp
  528. if (top_type)
  529. h->top_cbp = h->cbp_table[top_xy];
  530. else
  531. h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
  532. // left_cbp
  533. if (left_type[LTOP]) {
  534. h->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) |
  535. ((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
  536. (((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
  537. } else {
  538. h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
  539. }
  540. }
  541. }
  542. if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)) {
  543. int list;
  544. int b_stride = h->b_stride;
  545. for (list = 0; list < h->list_count; list++) {
  546. int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
  547. int8_t *ref = h->cur_pic.ref_index[list];
  548. int16_t(*mv_cache)[2] = &h->mv_cache[list][scan8[0]];
  549. int16_t(*mv)[2] = h->cur_pic.motion_val[list];
  550. if (!USES_LIST(mb_type, list))
  551. continue;
  552. assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));
  553. if (USES_LIST(top_type, list)) {
  554. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  555. AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);
  556. ref_cache[0 - 1 * 8] =
  557. ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
  558. ref_cache[2 - 1 * 8] =
  559. ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
  560. } else {
  561. AV_ZERO128(mv_cache[0 - 1 * 8]);
  562. AV_WN32A(&ref_cache[0 - 1 * 8],
  563. ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);
  564. }
  565. if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {
  566. for (i = 0; i < 2; i++) {
  567. int cache_idx = -1 + i * 2 * 8;
  568. if (USES_LIST(left_type[LEFT(i)], list)) {
  569. const int b_xy = h->mb2b_xy[left_xy[LEFT(i)]] + 3;
  570. const int b8_xy = 4 * left_xy[LEFT(i)] + 1;
  571. AV_COPY32(mv_cache[cache_idx],
  572. mv[b_xy + b_stride * left_block[0 + i * 2]]);
  573. AV_COPY32(mv_cache[cache_idx + 8],
  574. mv[b_xy + b_stride * left_block[1 + i * 2]]);
  575. ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
  576. ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
  577. } else {
  578. AV_ZERO32(mv_cache[cache_idx]);
  579. AV_ZERO32(mv_cache[cache_idx + 8]);
  580. ref_cache[cache_idx] =
  581. ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED
  582. : PART_NOT_AVAILABLE;
  583. }
  584. }
  585. } else {
  586. if (USES_LIST(left_type[LTOP], list)) {
  587. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  588. const int b8_xy = 4 * left_xy[LTOP] + 1;
  589. AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);
  590. ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
  591. } else {
  592. AV_ZERO32(mv_cache[-1]);
  593. ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED
  594. : PART_NOT_AVAILABLE;
  595. }
  596. }
  597. if (USES_LIST(topright_type, list)) {
  598. const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;
  599. AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);
  600. ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
  601. } else {
  602. AV_ZERO32(mv_cache[4 - 1 * 8]);
  603. ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED
  604. : PART_NOT_AVAILABLE;
  605. }
  606. if (ref_cache[4 - 1 * 8] < 0) {
  607. if (USES_LIST(topleft_type, list)) {
  608. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride +
  609. (h->topleft_partition & 2 * b_stride);
  610. const int b8_xy = 4 * topleft_xy + 1 + (h->topleft_partition & 2);
  611. AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);
  612. ref_cache[-1 - 1 * 8] = ref[b8_xy];
  613. } else {
  614. AV_ZERO32(mv_cache[-1 - 1 * 8]);
  615. ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED
  616. : PART_NOT_AVAILABLE;
  617. }
  618. }
  619. if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF(h))
  620. continue;
  621. if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
  622. uint8_t(*mvd_cache)[2] = &h->mvd_cache[list][scan8[0]];
  623. uint8_t(*mvd)[2] = h->mvd_table[list];
  624. ref_cache[2 + 8 * 0] =
  625. ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
  626. AV_ZERO32(mv_cache[2 + 8 * 0]);
  627. AV_ZERO32(mv_cache[2 + 8 * 2]);
  628. if (CABAC(h)) {
  629. if (USES_LIST(top_type, list)) {
  630. const int b_xy = h->mb2br_xy[top_xy];
  631. AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
  632. } else {
  633. AV_ZERO64(mvd_cache[0 - 1 * 8]);
  634. }
  635. if (USES_LIST(left_type[LTOP], list)) {
  636. const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
  637. AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
  638. AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
  639. } else {
  640. AV_ZERO16(mvd_cache[-1 + 0 * 8]);
  641. AV_ZERO16(mvd_cache[-1 + 1 * 8]);
  642. }
  643. if (USES_LIST(left_type[LBOT], list)) {
  644. const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
  645. AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
  646. AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
  647. } else {
  648. AV_ZERO16(mvd_cache[-1 + 2 * 8]);
  649. AV_ZERO16(mvd_cache[-1 + 3 * 8]);
  650. }
  651. AV_ZERO16(mvd_cache[2 + 8 * 0]);
  652. AV_ZERO16(mvd_cache[2 + 8 * 2]);
  653. if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
  654. uint8_t *direct_cache = &h->direct_cache[scan8[0]];
  655. uint8_t *direct_table = h->direct_table;
  656. fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);
  657. if (IS_DIRECT(top_type)) {
  658. AV_WN32A(&direct_cache[-1 * 8],
  659. 0x01010101u * (MB_TYPE_DIRECT2 >> 1));
  660. } else if (IS_8X8(top_type)) {
  661. int b8_xy = 4 * top_xy;
  662. direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
  663. direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
  664. } else {
  665. AV_WN32A(&direct_cache[-1 * 8],
  666. 0x01010101 * (MB_TYPE_16x16 >> 1));
  667. }
  668. if (IS_DIRECT(left_type[LTOP]))
  669. direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
  670. else if (IS_8X8(left_type[LTOP]))
  671. direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
  672. else
  673. direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
  674. if (IS_DIRECT(left_type[LBOT]))
  675. direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
  676. else if (IS_8X8(left_type[LBOT]))
  677. direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
  678. else
  679. direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
  680. }
  681. }
  682. }
  683. #define MAP_MVS \
  684. MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \
  685. MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \
  686. MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \
  687. MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \
  688. MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \
  689. MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \
  690. MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \
  691. MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \
  692. MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \
  693. MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
  694. if (FRAME_MBAFF(h)) {
  695. if (MB_FIELD(h)) {
  696. #define MAP_F2F(idx, mb_type) \
  697. if (!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
  698. h->ref_cache[list][idx] <<= 1; \
  699. h->mv_cache[list][idx][1] /= 2; \
  700. h->mvd_cache[list][idx][1] >>= 1; \
  701. }
  702. MAP_MVS
  703. } else {
  704. #undef MAP_F2F
  705. #define MAP_F2F(idx, mb_type) \
  706. if (IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
  707. h->ref_cache[list][idx] >>= 1; \
  708. h->mv_cache[list][idx][1] <<= 1; \
  709. h->mvd_cache[list][idx][1] <<= 1; \
  710. }
  711. MAP_MVS
  712. #undef MAP_F2F
  713. }
  714. }
  715. }
  716. }
  717. h->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
  718. }
  719. /**
  720. * decodes a P_SKIP or B_SKIP macroblock
  721. */
  722. static void av_unused decode_mb_skip(H264Context *h)
  723. {
  724. const int mb_xy = h->mb_xy;
  725. int mb_type = 0;
  726. memset(h->non_zero_count[mb_xy], 0, 48);
  727. if (MB_FIELD(h))
  728. mb_type |= MB_TYPE_INTERLACED;
  729. if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
  730. // just for fill_caches. pred_direct_motion will set the real mb_type
  731. mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 | MB_TYPE_SKIP;
  732. if (h->direct_spatial_mv_pred) {
  733. fill_decode_neighbors(h, mb_type);
  734. fill_decode_caches(h, mb_type); //FIXME check what is needed and what not ...
  735. }
  736. ff_h264_pred_direct_motion(h, &mb_type);
  737. mb_type |= MB_TYPE_SKIP;
  738. } else {
  739. mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_SKIP;
  740. fill_decode_neighbors(h, mb_type);
  741. pred_pskip_motion(h);
  742. }
  743. write_back_motion(h, mb_type);
  744. h->cur_pic.mb_type[mb_xy] = mb_type;
  745. h->cur_pic.qscale_table[mb_xy] = h->qscale;
  746. h->slice_table[mb_xy] = h->slice_num;
  747. h->prev_mb_skipped = 1;
  748. }
  749. #endif /* AVCODEC_H264_MVPRED_H */