h264_mvpred.h 35 KB

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