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.

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