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.

331 lines
11KB

  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->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->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->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. * gets 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. * gets 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. * gets 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.ref_index[0];
  209. int16_t (*mv)[2] = s->current_picture.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. #endif /* AVCODEC_H264_MVPRED_H */