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.

238 lines
8.4KB

  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 libavcodec/h264_mvpred.h
  23. * H.264 / AVC / MPEG4 part10 motion vector predicion.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "internal.h"
  27. #include "avcodec.h"
  28. #include "h264.h"
  29. //#undef NDEBUG
  30. #include <assert.h>
  31. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  32. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  33. MpegEncContext *s = &h->s;
  34. /* there is no consistent mapping of mvs to neighboring locations that will
  35. * make mbaff happy, so we can't move all this logic to fill_caches */
  36. if(FRAME_MBAFF){
  37. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  38. const int16_t *mv;
  39. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  40. *C = h->mv_cache[list][scan8[0]-2];
  41. if(!MB_FIELD
  42. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  43. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  44. if(IS_INTERLACED(mb_types[topright_xy])){
  45. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  46. const int x4 = X4, y4 = Y4;\
  47. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  48. if(!USES_LIST(mb_type,list))\
  49. return LIST_NOT_USED;\
  50. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  51. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  52. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  53. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  54. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  55. }
  56. }
  57. if(topright_ref == PART_NOT_AVAILABLE
  58. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  59. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  60. if(!MB_FIELD
  61. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  62. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  63. }
  64. if(MB_FIELD
  65. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  66. && i >= scan8[0]+8){
  67. // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
  68. SET_DIAG_MV(/2, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  69. }
  70. }
  71. #undef SET_DIAG_MV
  72. }
  73. if(topright_ref != PART_NOT_AVAILABLE){
  74. *C= h->mv_cache[list][ i - 8 + part_width ];
  75. return topright_ref;
  76. }else{
  77. tprintf(s->avctx, "topright MV not available\n");
  78. *C= h->mv_cache[list][ i - 8 - 1 ];
  79. return h->ref_cache[list][ i - 8 - 1 ];
  80. }
  81. }
  82. /**
  83. * gets the predicted MV.
  84. * @param n the block index
  85. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  86. * @param mx the x component of the predicted motion vector
  87. * @param my the y component of the predicted motion vector
  88. */
  89. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  90. const int index8= scan8[n];
  91. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  92. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  93. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  94. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  95. const int16_t * C;
  96. int diagonal_ref, match_count;
  97. assert(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, &C, index8, list, part_width);
  106. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  107. tprintf(h->s.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 && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  124. *mx= A[0];
  125. *my= A[1];
  126. }else{
  127. *mx= mid_pred(A[0], B[0], C[0]);
  128. *my= mid_pred(A[1], B[1], C[1]);
  129. }
  130. }
  131. tprintf(h->s.avctx, "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);
  132. }
  133. /**
  134. * gets the directionally predicted 16x8 MV.
  135. * @param n the block index
  136. * @param mx the x component of the predicted motion vector
  137. * @param my the y component of the predicted motion vector
  138. */
  139. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  140. if(n==0){
  141. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  142. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  143. 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);
  144. if(top_ref == ref){
  145. *mx= B[0];
  146. *my= B[1];
  147. return;
  148. }
  149. }else{
  150. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  151. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  152. 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);
  153. if(left_ref == ref){
  154. *mx= A[0];
  155. *my= A[1];
  156. return;
  157. }
  158. }
  159. //RARE
  160. pred_motion(h, n, 4, list, ref, mx, my);
  161. }
  162. /**
  163. * gets the directionally predicted 8x16 MV.
  164. * @param n the block index
  165. * @param mx the x component of the predicted motion vector
  166. * @param my the y component of the predicted motion vector
  167. */
  168. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  169. if(n==0){
  170. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  171. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  172. 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);
  173. if(left_ref == ref){
  174. *mx= A[0];
  175. *my= A[1];
  176. return;
  177. }
  178. }else{
  179. const int16_t * C;
  180. int diagonal_ref;
  181. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  182. 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);
  183. if(diagonal_ref == ref){
  184. *mx= C[0];
  185. *my= C[1];
  186. return;
  187. }
  188. }
  189. //RARE
  190. pred_motion(h, n, 2, list, ref, mx, my);
  191. }
  192. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  193. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  194. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  195. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  196. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  197. || !( top_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ])
  198. || !(left_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ])){
  199. *mx = *my = 0;
  200. return;
  201. }
  202. pred_motion(h, 0, 4, 0, 0, mx, my);
  203. return;
  204. }