00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #ifndef AVCODEC_H264_MVPRED_H
00029 #define AVCODEC_H264_MVPRED_H
00030
00031 #include "internal.h"
00032 #include "avcodec.h"
00033 #include "h264.h"
00034
00035
00036 #include <assert.h>
00037
00038 static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
00039 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
00040 MpegEncContext *s = &h->s;
00041
00042
00043
00044 if(FRAME_MBAFF){
00045
00046 #define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4)\
00047 const int xy = XY, y4 = Y4;\
00048 const int mb_type = mb_types[xy+(y4>>2)*s->mb_stride];\
00049 if(!USES_LIST(mb_type,list))\
00050 return LIST_NOT_USED;\
00051 mv = s->current_picture_ptr->motion_val[list][h->mb2b_xy[xy]+3 + y4*h->b_stride];\
00052 h->mv_cache[list][scan8[0]-2][0] = mv[0];\
00053 h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
00054 return s->current_picture_ptr->ref_index[list][4*xy+1 + (y4&~1)] REF_OP;
00055
00056 if(topright_ref == PART_NOT_AVAILABLE
00057 && i >= scan8[0]+8 && (i&7)==4
00058 && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
00059 const uint32_t *mb_types = s->current_picture_ptr->mb_type;
00060 const int16_t *mv;
00061 AV_ZERO32(h->mv_cache[list][scan8[0]-2]);
00062 *C = h->mv_cache[list][scan8[0]-2];
00063
00064 if(!MB_FIELD
00065 && IS_INTERLACED(h->left_type[0])){
00066 SET_DIAG_MV(*2, >>1, h->left_mb_xy[0]+s->mb_stride, (s->mb_y&1)*2+(i>>5));
00067 assert(h->left_mb_xy[0] == h->left_mb_xy[1]);
00068 }
00069 if(MB_FIELD
00070 && !IS_INTERLACED(h->left_type[0])){
00071
00072 SET_DIAG_MV(/2, <<1, h->left_mb_xy[i>=36], ((i>>2))&3);
00073 }
00074 }
00075 #undef SET_DIAG_MV
00076 }
00077
00078 if(topright_ref != PART_NOT_AVAILABLE){
00079 *C= h->mv_cache[list][ i - 8 + part_width ];
00080 return topright_ref;
00081 }else{
00082 tprintf(s->avctx, "topright MV not available\n");
00083
00084 *C= h->mv_cache[list][ i - 8 - 1 ];
00085 return h->ref_cache[list][ i - 8 - 1 ];
00086 }
00087 }
00088
00096 static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
00097 const int index8= scan8[n];
00098 const int top_ref= h->ref_cache[list][ index8 - 8 ];
00099 const int left_ref= h->ref_cache[list][ index8 - 1 ];
00100 const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
00101 const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
00102 const int16_t * C;
00103 int diagonal_ref, match_count;
00104
00105 assert(part_width==1 || part_width==2 || part_width==4);
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
00116 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
00117 tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
00118 if(match_count > 1){
00119 *mx= mid_pred(A[0], B[0], C[0]);
00120 *my= mid_pred(A[1], B[1], C[1]);
00121 }else if(match_count==1){
00122 if(left_ref==ref){
00123 *mx= A[0];
00124 *my= A[1];
00125 }else if(top_ref==ref){
00126 *mx= B[0];
00127 *my= B[1];
00128 }else{
00129 *mx= C[0];
00130 *my= C[1];
00131 }
00132 }else{
00133 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
00134 *mx= A[0];
00135 *my= A[1];
00136 }else{
00137 *mx= mid_pred(A[0], B[0], C[0]);
00138 *my= mid_pred(A[1], B[1], C[1]);
00139 }
00140 }
00141
00142 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);
00143 }
00144
00151 static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
00152 if(n==0){
00153 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
00154 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
00155
00156 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);
00157
00158 if(top_ref == ref){
00159 *mx= B[0];
00160 *my= B[1];
00161 return;
00162 }
00163 }else{
00164 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
00165 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
00166
00167 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);
00168
00169 if(left_ref == ref){
00170 *mx= A[0];
00171 *my= A[1];
00172 return;
00173 }
00174 }
00175
00176
00177 pred_motion(h, n, 4, list, ref, mx, my);
00178 }
00179
00186 static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
00187 if(n==0){
00188 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
00189 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
00190
00191 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);
00192
00193 if(left_ref == ref){
00194 *mx= A[0];
00195 *my= A[1];
00196 return;
00197 }
00198 }else{
00199 const int16_t * C;
00200 int diagonal_ref;
00201
00202 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
00203
00204 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);
00205
00206 if(diagonal_ref == ref){
00207 *mx= C[0];
00208 *my= C[1];
00209 return;
00210 }
00211 }
00212
00213
00214 pred_motion(h, n, 2, list, ref, mx, my);
00215 }
00216
00217 static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
00218 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
00219 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
00220
00221 tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
00222
00223 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
00224 || !( top_ref | AV_RN32A(h->mv_cache[0][ scan8[0] - 8 ]))
00225 || !(left_ref | AV_RN32A(h->mv_cache[0][ scan8[0] - 1 ]))){
00226
00227 *mx = *my = 0;
00228 return;
00229 }
00230
00231 pred_motion(h, 0, 4, 0, 0, mx, my);
00232
00233 return;
00234 }
00235
00236 #endif