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.

793 lines
31KB

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