Libav 0.7.1
|
00001 /* 00002 * H.26L/H.264/AVC/JVT/14496-10/... motion vector predicion 00003 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 //#undef NDEBUG 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 /* there is no consistent mapping of mvs to neighboring locations that will 00043 * make mbaff happy, so we can't move all this logic to fill_caches */ 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 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK. 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 /* mv_cache 00108 B . . A T T T T 00109 U . . L . . , . 00110 U . . L . . . . 00111 U . . L . . , . 00112 . . . L . . . . 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){ //most common 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 //RARE 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 //RARE 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 /* AVCODEC_H264_MVPRED_H */