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.

835 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "libavutil/avassert.h"
  32. static av_always_inline int fetch_diagonal_mv(H264Context *h, const int16_t **C,
  33. int i, int list, int part_width)
  34. {
  35. const int topright_ref = h->ref_cache[list][i - 8 + part_width];
  36. MpegEncContext *s = &h->s;
  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) {
  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) * s->mb_stride]; \
  43. if (!USES_LIST(mb_type, list)) \
  44. return LIST_NOT_USED; \
  45. mv = s->current_picture_ptr->f.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 s->current_picture_ptr->f.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 = s->current_picture_ptr->f.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 && IS_INTERLACED(h->left_type[0])) {
  57. SET_DIAG_MV(* 2, >> 1, h->left_mb_xy[0] + s->mb_stride,
  58. (s->mb_y & 1) * 2 + (i >> 5));
  59. }
  60. if (MB_FIELD && !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(s->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. av_assert2(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->s.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->s.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->s.mb_x, h->s.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->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
  149. top_ref, B[0], B[1], h->s.mb_x, h->s.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->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
  159. left_ref, A[0], A[1], h->s.mb_x, h->s.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->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
  183. left_ref, A[0], A[1], h->s.mb_x, h->s.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->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
  194. diagonal_ref, C[0], C[1], h->s.mb_x, h->s.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) { \
  206. if (MB_FIELD) { \
  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. MpegEncContext *const s = &h->s;
  227. int8_t *ref = s->current_picture.f.ref_index[0];
  228. int16_t(*mv)[2] = s->current_picture.f.motion_val[0];
  229. int top_ref, left_ref, diagonal_ref, match_count, mx, my;
  230. const int16_t *A, *B, *C;
  231. int b_stride = h->b_stride;
  232. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  233. /* To avoid doing an entire fill_decode_caches, we inline the relevant
  234. * parts here.
  235. * FIXME: this is a partial duplicate of the logic in fill_decode_caches,
  236. * but it's faster this way. Is there a way to avoid this duplication?
  237. */
  238. if (USES_LIST(h->left_type[LTOP], 0)) {
  239. left_ref = ref[4 * h->left_mb_xy[LTOP] + 1 + (h->left_block[0] & ~1)];
  240. A = mv[h->mb2b_xy[h->left_mb_xy[LTOP]] + 3 + b_stride * h->left_block[0]];
  241. FIX_MV_MBAFF(h->left_type[LTOP], left_ref, A, 0);
  242. if (!(left_ref | AV_RN32A(A)))
  243. goto zeromv;
  244. } else if (h->left_type[LTOP]) {
  245. left_ref = LIST_NOT_USED;
  246. A = zeromv;
  247. } else {
  248. goto zeromv;
  249. }
  250. if (USES_LIST(h->top_type, 0)) {
  251. top_ref = ref[4 * h->top_mb_xy + 2];
  252. B = mv[h->mb2b_xy[h->top_mb_xy] + 3 * b_stride];
  253. FIX_MV_MBAFF(h->top_type, top_ref, B, 1);
  254. if (!(top_ref | AV_RN32A(B)))
  255. goto zeromv;
  256. } else if (h->top_type) {
  257. top_ref = LIST_NOT_USED;
  258. B = zeromv;
  259. } else {
  260. goto zeromv;
  261. }
  262. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n",
  263. top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  264. if (USES_LIST(h->topright_type, 0)) {
  265. diagonal_ref = ref[4 * h->topright_mb_xy + 2];
  266. C = mv[h->mb2b_xy[h->topright_mb_xy] + 3 * b_stride];
  267. FIX_MV_MBAFF(h->topright_type, diagonal_ref, C, 2);
  268. } else if (h->topright_type) {
  269. diagonal_ref = LIST_NOT_USED;
  270. C = zeromv;
  271. } else {
  272. if (USES_LIST(h->topleft_type, 0)) {
  273. diagonal_ref = ref[4 * h->topleft_mb_xy + 1 +
  274. (h->topleft_partition & 2)];
  275. C = mv[h->mb2b_xy[h->topleft_mb_xy] + 3 + b_stride +
  276. (h->topleft_partition & 2 * b_stride)];
  277. FIX_MV_MBAFF(h->topleft_type, diagonal_ref, C, 2);
  278. } else if (h->topleft_type) {
  279. diagonal_ref = LIST_NOT_USED;
  280. C = zeromv;
  281. } else {
  282. diagonal_ref = PART_NOT_AVAILABLE;
  283. C = zeromv;
  284. }
  285. }
  286. match_count = !diagonal_ref + !top_ref + !left_ref;
  287. tprintf(h->s.avctx, "pred_pskip_motion match_count=%d\n", match_count);
  288. if (match_count > 1) {
  289. mx = mid_pred(A[0], B[0], C[0]);
  290. my = mid_pred(A[1], B[1], C[1]);
  291. } else if (match_count == 1) {
  292. if (!left_ref) {
  293. mx = A[0];
  294. my = A[1];
  295. } else if (!top_ref) {
  296. mx = B[0];
  297. my = B[1];
  298. } else {
  299. mx = C[0];
  300. my = C[1];
  301. }
  302. } else {
  303. mx = mid_pred(A[0], B[0], C[0]);
  304. my = mid_pred(A[1], B[1], C[1]);
  305. }
  306. fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx, my), 4);
  307. return;
  308. zeromv:
  309. fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  310. return;
  311. }
  312. static void fill_decode_neighbors(H264Context *h, int mb_type)
  313. {
  314. MpegEncContext *const s = &h->s;
  315. const int mb_xy = h->mb_xy;
  316. int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
  317. static const uint8_t left_block_options[4][32] = {
  318. { 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 },
  319. { 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 },
  320. { 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 },
  321. { 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 }
  322. };
  323. h->topleft_partition = -1;
  324. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  325. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  326. * stuff, I can't imagine that these complex rules are worth it. */
  327. topleft_xy = top_xy - 1;
  328. topright_xy = top_xy + 1;
  329. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  330. h->left_block = left_block_options[0];
  331. if (FRAME_MBAFF) {
  332. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
  333. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  334. if (s->mb_y & 1) {
  335. if (left_mb_field_flag != curr_mb_field_flag) {
  336. left_xy[LBOT] = left_xy[LTOP] = mb_xy - s->mb_stride - 1;
  337. if (curr_mb_field_flag) {
  338. left_xy[LBOT] += s->mb_stride;
  339. h->left_block = left_block_options[3];
  340. } else {
  341. topleft_xy += s->mb_stride;
  342. /* take top left mv from the middle of the mb, as opposed
  343. * to all other modes which use the bottom right partition */
  344. h->topleft_partition = 0;
  345. h->left_block = left_block_options[1];
  346. }
  347. }
  348. } else {
  349. if (curr_mb_field_flag) {
  350. topleft_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy - 1] >> 7) & 1) - 1);
  351. topright_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy + 1] >> 7) & 1) - 1);
  352. top_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
  353. }
  354. if (left_mb_field_flag != curr_mb_field_flag) {
  355. if (curr_mb_field_flag) {
  356. left_xy[LBOT] += s->mb_stride;
  357. h->left_block = left_block_options[3];
  358. } else {
  359. h->left_block = left_block_options[2];
  360. }
  361. }
  362. }
  363. }
  364. h->topleft_mb_xy = topleft_xy;
  365. h->top_mb_xy = top_xy;
  366. h->topright_mb_xy = topright_xy;
  367. h->left_mb_xy[LTOP] = left_xy[LTOP];
  368. h->left_mb_xy[LBOT] = left_xy[LBOT];
  369. //FIXME do we need all in the context?
  370. h->topleft_type = s->current_picture.f.mb_type[topleft_xy];
  371. h->top_type = s->current_picture.f.mb_type[top_xy];
  372. h->topright_type = s->current_picture.f.mb_type[topright_xy];
  373. h->left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
  374. h->left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
  375. if (FMO) {
  376. if (h->slice_table[topleft_xy] != h->slice_num)
  377. h->topleft_type = 0;
  378. if (h->slice_table[top_xy] != h->slice_num)
  379. h->top_type = 0;
  380. if (h->slice_table[left_xy[LTOP]] != h->slice_num)
  381. h->left_type[LTOP] = h->left_type[LBOT] = 0;
  382. } else {
  383. if (h->slice_table[topleft_xy] != h->slice_num) {
  384. h->topleft_type = 0;
  385. if (h->slice_table[top_xy] != h->slice_num)
  386. h->top_type = 0;
  387. if (h->slice_table[left_xy[LTOP]] != h->slice_num)
  388. h->left_type[LTOP] = h->left_type[LBOT] = 0;
  389. }
  390. }
  391. if (h->slice_table[topright_xy] != h->slice_num)
  392. h->topright_type = 0;
  393. }
  394. static void fill_decode_caches(H264Context *h, int mb_type)
  395. {
  396. MpegEncContext *const s = &h->s;
  397. int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
  398. int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
  399. const uint8_t *left_block = h->left_block;
  400. int i;
  401. uint8_t *nnz;
  402. uint8_t *nnz_cache;
  403. topleft_xy = h->topleft_mb_xy;
  404. top_xy = h->top_mb_xy;
  405. topright_xy = h->topright_mb_xy;
  406. left_xy[LTOP] = h->left_mb_xy[LTOP];
  407. left_xy[LBOT] = h->left_mb_xy[LBOT];
  408. topleft_type = h->topleft_type;
  409. top_type = h->top_type;
  410. topright_type = h->topright_type;
  411. left_type[LTOP] = h->left_type[LTOP];
  412. left_type[LBOT] = h->left_type[LBOT];
  413. if (!IS_SKIP(mb_type)) {
  414. if (IS_INTRA(mb_type)) {
  415. int type_mask = h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
  416. h->topleft_samples_available =
  417. h->top_samples_available =
  418. h->left_samples_available = 0xFFFF;
  419. h->topright_samples_available = 0xEEEA;
  420. if (!(top_type & type_mask)) {
  421. h->topleft_samples_available = 0xB3FF;
  422. h->top_samples_available = 0x33FF;
  423. h->topright_samples_available = 0x26EA;
  424. }
  425. if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {
  426. if (IS_INTERLACED(mb_type)) {
  427. if (!(left_type[LTOP] & type_mask)) {
  428. h->topleft_samples_available &= 0xDFFF;
  429. h->left_samples_available &= 0x5FFF;
  430. }
  431. if (!(left_type[LBOT] & type_mask)) {
  432. h->topleft_samples_available &= 0xFF5F;
  433. h->left_samples_available &= 0xFF5F;
  434. }
  435. } else {
  436. int left_typei = s->current_picture.f.mb_type[left_xy[LTOP] + s->mb_stride];
  437. av_assert2(left_xy[LTOP] == left_xy[LBOT]);
  438. if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
  439. h->topleft_samples_available &= 0xDF5F;
  440. h->left_samples_available &= 0x5F5F;
  441. }
  442. }
  443. } else {
  444. if (!(left_type[LTOP] & type_mask)) {
  445. h->topleft_samples_available &= 0xDF5F;
  446. h->left_samples_available &= 0x5F5F;
  447. }
  448. }
  449. if (!(topleft_type & type_mask))
  450. h->topleft_samples_available &= 0x7FFF;
  451. if (!(topright_type & type_mask))
  452. h->topright_samples_available &= 0xFBFF;
  453. if (IS_INTRA4x4(mb_type)) {
  454. if (IS_INTRA4x4(top_type)) {
  455. AV_COPY32(h->intra4x4_pred_mode_cache + 4 + 8 * 0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
  456. } else {
  457. h->intra4x4_pred_mode_cache[4 + 8 * 0] =
  458. h->intra4x4_pred_mode_cache[5 + 8 * 0] =
  459. h->intra4x4_pred_mode_cache[6 + 8 * 0] =
  460. h->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
  461. }
  462. for (i = 0; i < 2; i++) {
  463. if (IS_INTRA4x4(left_type[LEFT(i)])) {
  464. int8_t *mode = h->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
  465. h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
  466. h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
  467. } else {
  468. h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
  469. h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);
  470. }
  471. }
  472. }
  473. }
  474. /*
  475. * 0 . T T. T T T T
  476. * 1 L . .L . . . .
  477. * 2 L . .L . . . .
  478. * 3 . T TL . . . .
  479. * 4 L . .L . . . .
  480. * 5 L . .. . . . .
  481. */
  482. /* FIXME: constraint_intra_pred & partitioning & nnz
  483. * (let us hope this is just a typo in the spec) */
  484. nnz_cache = h->non_zero_count_cache;
  485. if (top_type) {
  486. nnz = h->non_zero_count[top_xy];
  487. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
  488. if (!s->chroma_y_shift) {
  489. AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);
  490. AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
  491. } else {
  492. AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);
  493. AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
  494. }
  495. } else {
  496. uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
  497. AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
  498. AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
  499. AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
  500. }
  501. for (i = 0; i < 2; i++) {
  502. if (left_type[LEFT(i)]) {
  503. nnz = h->non_zero_count[left_xy[LEFT(i)]];
  504. nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
  505. nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
  506. if (CHROMA444) {
  507. nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
  508. nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
  509. nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
  510. nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
  511. } else if (CHROMA422) {
  512. nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
  513. nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
  514. nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
  515. nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
  516. } else {
  517. nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
  518. nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
  519. }
  520. } else {
  521. nnz_cache[3 + 8 * 1 + 2 * 8 * i] =
  522. nnz_cache[3 + 8 * 2 + 2 * 8 * i] =
  523. nnz_cache[3 + 8 * 6 + 2 * 8 * i] =
  524. nnz_cache[3 + 8 * 7 + 2 * 8 * i] =
  525. nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
  526. nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC && !IS_INTRA(mb_type) ? 0 : 64;
  527. }
  528. }
  529. if (CABAC) {
  530. // top_cbp
  531. if (top_type)
  532. h->top_cbp = h->cbp_table[top_xy];
  533. else
  534. h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
  535. // left_cbp
  536. if (left_type[LTOP]) {
  537. h->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) |
  538. ((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
  539. (((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
  540. } else {
  541. h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
  542. }
  543. }
  544. }
  545. if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)) {
  546. int list;
  547. int b_stride = h->b_stride;
  548. for (list = 0; list < h->list_count; list++) {
  549. int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
  550. int8_t *ref = s->current_picture.f.ref_index[list];
  551. int16_t(*mv_cache)[2] = &h->mv_cache[list][scan8[0]];
  552. int16_t(*mv)[2] = s->current_picture.f.motion_val[list];
  553. if (!USES_LIST(mb_type, list))
  554. continue;
  555. av_assert2(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));
  556. if (USES_LIST(top_type, list)) {
  557. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  558. AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);
  559. ref_cache[0 - 1 * 8] =
  560. ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
  561. ref_cache[2 - 1 * 8] =
  562. ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
  563. } else {
  564. AV_ZERO128(mv_cache[0 - 1 * 8]);
  565. AV_WN32A(&ref_cache[0 - 1 * 8],
  566. ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);
  567. }
  568. if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {
  569. for (i = 0; i < 2; i++) {
  570. int cache_idx = -1 + i * 2 * 8;
  571. if (USES_LIST(left_type[LEFT(i)], list)) {
  572. const int b_xy = h->mb2b_xy[left_xy[LEFT(i)]] + 3;
  573. const int b8_xy = 4 * left_xy[LEFT(i)] + 1;
  574. AV_COPY32(mv_cache[cache_idx],
  575. mv[b_xy + b_stride * left_block[0 + i * 2]]);
  576. AV_COPY32(mv_cache[cache_idx + 8],
  577. mv[b_xy + b_stride * left_block[1 + i * 2]]);
  578. ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
  579. ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
  580. } else {
  581. AV_ZERO32(mv_cache[cache_idx]);
  582. AV_ZERO32(mv_cache[cache_idx + 8]);
  583. ref_cache[cache_idx] =
  584. ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED
  585. : PART_NOT_AVAILABLE;
  586. }
  587. }
  588. } else {
  589. if (USES_LIST(left_type[LTOP], list)) {
  590. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  591. const int b8_xy = 4 * left_xy[LTOP] + 1;
  592. AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);
  593. ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
  594. } else {
  595. AV_ZERO32(mv_cache[-1]);
  596. ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED
  597. : PART_NOT_AVAILABLE;
  598. }
  599. }
  600. if (USES_LIST(topright_type, list)) {
  601. const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;
  602. AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);
  603. ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
  604. } else {
  605. AV_ZERO32(mv_cache[4 - 1 * 8]);
  606. ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED
  607. : PART_NOT_AVAILABLE;
  608. }
  609. if(ref_cache[2 - 1*8] < 0 || ref_cache[4 - 1*8] < 0){
  610. if (USES_LIST(topleft_type, list)) {
  611. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride +
  612. (h->topleft_partition & 2 * b_stride);
  613. const int b8_xy = 4 * topleft_xy + 1 + (h->topleft_partition & 2);
  614. AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);
  615. ref_cache[-1 - 1 * 8] = ref[b8_xy];
  616. } else {
  617. AV_ZERO32(mv_cache[-1 - 1 * 8]);
  618. ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED
  619. : PART_NOT_AVAILABLE;
  620. }
  621. }
  622. if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF)
  623. continue;
  624. if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
  625. uint8_t(*mvd_cache)[2] = &h->mvd_cache[list][scan8[0]];
  626. uint8_t(*mvd)[2] = h->mvd_table[list];
  627. ref_cache[2 + 8 * 0] =
  628. ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
  629. AV_ZERO32(mv_cache[2 + 8 * 0]);
  630. AV_ZERO32(mv_cache[2 + 8 * 2]);
  631. if (CABAC) {
  632. if (USES_LIST(top_type, list)) {
  633. const int b_xy = h->mb2br_xy[top_xy];
  634. AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
  635. } else {
  636. AV_ZERO64(mvd_cache[0 - 1 * 8]);
  637. }
  638. if (USES_LIST(left_type[LTOP], list)) {
  639. const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
  640. AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
  641. AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
  642. } else {
  643. AV_ZERO16(mvd_cache[-1 + 0 * 8]);
  644. AV_ZERO16(mvd_cache[-1 + 1 * 8]);
  645. }
  646. if (USES_LIST(left_type[LBOT], list)) {
  647. const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
  648. AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
  649. AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
  650. } else {
  651. AV_ZERO16(mvd_cache[-1 + 2 * 8]);
  652. AV_ZERO16(mvd_cache[-1 + 3 * 8]);
  653. }
  654. AV_ZERO16(mvd_cache[2 + 8 * 0]);
  655. AV_ZERO16(mvd_cache[2 + 8 * 2]);
  656. if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
  657. uint8_t *direct_cache = &h->direct_cache[scan8[0]];
  658. uint8_t *direct_table = h->direct_table;
  659. fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);
  660. if (IS_DIRECT(top_type)) {
  661. AV_WN32A(&direct_cache[-1 * 8],
  662. 0x01010101u * (MB_TYPE_DIRECT2 >> 1));
  663. } else if (IS_8X8(top_type)) {
  664. int b8_xy = 4 * top_xy;
  665. direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
  666. direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
  667. } else {
  668. AV_WN32A(&direct_cache[-1 * 8],
  669. 0x01010101 * (MB_TYPE_16x16 >> 1));
  670. }
  671. if (IS_DIRECT(left_type[LTOP]))
  672. direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
  673. else if (IS_8X8(left_type[LTOP]))
  674. direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
  675. else
  676. direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
  677. if (IS_DIRECT(left_type[LBOT]))
  678. direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
  679. else if (IS_8X8(left_type[LBOT]))
  680. direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
  681. else
  682. direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
  683. }
  684. }
  685. }
  686. #define MAP_MVS \
  687. MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \
  688. MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \
  689. MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \
  690. MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \
  691. MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \
  692. MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \
  693. MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \
  694. MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \
  695. MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \
  696. MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
  697. if (FRAME_MBAFF) {
  698. if (MB_FIELD) {
  699. #define MAP_F2F(idx, mb_type) \
  700. if (!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
  701. h->ref_cache[list][idx] <<= 1; \
  702. h->mv_cache[list][idx][1] /= 2; \
  703. h->mvd_cache[list][idx][1] >>= 1; \
  704. }
  705. MAP_MVS
  706. } else {
  707. #undef MAP_F2F
  708. #define MAP_F2F(idx, mb_type) \
  709. if (IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
  710. h->ref_cache[list][idx] >>= 1; \
  711. h->mv_cache[list][idx][1] <<= 1; \
  712. h->mvd_cache[list][idx][1] <<= 1; \
  713. }
  714. MAP_MVS
  715. #undef MAP_F2F
  716. }
  717. }
  718. }
  719. }
  720. h->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
  721. }
  722. /**
  723. * decodes a P_SKIP or B_SKIP macroblock
  724. */
  725. static void av_unused decode_mb_skip(H264Context *h)
  726. {
  727. MpegEncContext *const s = &h->s;
  728. const int mb_xy = h->mb_xy;
  729. int mb_type = 0;
  730. memset(h->non_zero_count[mb_xy], 0, 48);
  731. if (MB_FIELD)
  732. mb_type |= MB_TYPE_INTERLACED;
  733. if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
  734. // just for fill_caches. pred_direct_motion will set the real mb_type
  735. mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 | MB_TYPE_SKIP;
  736. if (h->direct_spatial_mv_pred) {
  737. fill_decode_neighbors(h, mb_type);
  738. fill_decode_caches(h, mb_type); //FIXME check what is needed and what not ...
  739. }
  740. ff_h264_pred_direct_motion(h, &mb_type);
  741. mb_type |= MB_TYPE_SKIP;
  742. } else {
  743. mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_SKIP;
  744. fill_decode_neighbors(h, mb_type);
  745. pred_pskip_motion(h);
  746. }
  747. write_back_motion(h, mb_type);
  748. s->current_picture.f.mb_type[mb_xy] = mb_type;
  749. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  750. h->slice_table[mb_xy] = h->slice_num;
  751. h->prev_mb_skipped = 1;
  752. }
  753. #endif /* AVCODEC_H264_MVPRED_H */