2 * H.26L/H.264/AVC/JVT/14496-10/... motion vector predicion
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 part10 motion vector predicion.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #ifndef AVCODEC_H264_MVPRED_H
29 #define AVCODEC_H264_MVPRED_H
38 static av_always_inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
39 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
40 MpegEncContext *s = &h->s;
42 /* there is no consistent mapping of mvs to neighboring locations that will
43 * make mbaff happy, so we can't move all this logic to fill_caches */
46 #define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4)\
47 const int xy = XY, y4 = Y4;\
48 const int mb_type = mb_types[xy+(y4>>2)*s->mb_stride];\
49 if(!USES_LIST(mb_type,list))\
50 return LIST_NOT_USED;\
51 mv = s->current_picture_ptr->motion_val[list][h->mb2b_xy[xy]+3 + y4*h->b_stride];\
52 h->mv_cache[list][scan8[0]-2][0] = mv[0];\
53 h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
54 return s->current_picture_ptr->ref_index[list][4*xy+1 + (y4&~1)] REF_OP;
56 if(topright_ref == PART_NOT_AVAILABLE
57 && i >= scan8[0]+8 && (i&7)==4
58 && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
59 const uint32_t *mb_types = s->current_picture_ptr->mb_type;
61 AV_ZERO32(h->mv_cache[list][scan8[0]-2]);
62 *C = h->mv_cache[list][scan8[0]-2];
65 && IS_INTERLACED(h->left_type[0])){
66 SET_DIAG_MV(*2, >>1, h->left_mb_xy[0]+s->mb_stride, (s->mb_y&1)*2+(i>>5));
69 && !IS_INTERLACED(h->left_type[0])){
70 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
71 SET_DIAG_MV(/2, <<1, h->left_mb_xy[i>=36], ((i>>2))&3);
77 if(topright_ref != PART_NOT_AVAILABLE){
78 *C= h->mv_cache[list][ i - 8 + part_width ];
81 tprintf(s->avctx, "topright MV not available\n");
83 *C= h->mv_cache[list][ i - 8 - 1 ];
84 return h->ref_cache[list][ i - 8 - 1 ];
89 * gets the predicted MV.
90 * @param n the block index
91 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
92 * @param mx the x component of the predicted motion vector
93 * @param my the y component of the predicted motion vector
95 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){
96 const int index8= scan8[n];
97 const int top_ref= h->ref_cache[list][ index8 - 8 ];
98 const int left_ref= h->ref_cache[list][ index8 - 1 ];
99 const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
100 const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
102 int diagonal_ref, match_count;
104 assert(part_width==1 || part_width==2 || part_width==4);
114 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
115 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
116 tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
117 if(match_count > 1){ //most common
118 *mx= mid_pred(A[0], B[0], C[0]);
119 *my= mid_pred(A[1], B[1], C[1]);
120 }else if(match_count==1){
124 }else if(top_ref==ref){
132 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
136 *mx= mid_pred(A[0], B[0], C[0]);
137 *my= mid_pred(A[1], B[1], C[1]);
141 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);
145 * gets the directionally predicted 16x8 MV.
146 * @param n the block index
147 * @param mx the x component of the predicted motion vector
148 * @param my the y component of the predicted motion vector
150 static av_always_inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
152 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
153 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
155 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);
163 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
164 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
166 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);
176 pred_motion(h, n, 4, list, ref, mx, my);
180 * gets the directionally predicted 8x16 MV.
181 * @param n the block index
182 * @param mx the x component of the predicted motion vector
183 * @param my the y component of the predicted motion vector
185 static av_always_inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
187 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
188 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
190 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);
201 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
203 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);
205 if(diagonal_ref == ref){
213 pred_motion(h, n, 2, list, ref, mx, my);
216 #define FIX_MV_MBAFF(type, refn, mvn, idx)\
219 if(!IS_INTERLACED(type)){\
221 AV_COPY32(mvbuf[idx], mvn);\
226 if(IS_INTERLACED(type)){\
228 AV_COPY32(mvbuf[idx], mvn);\
229 mvbuf[idx][1] <<= 1;\
235 static av_always_inline void pred_pskip_motion(H264Context * const h){
236 DECLARE_ALIGNED(4, static const int16_t, zeromv)[2] = {0};
237 DECLARE_ALIGNED(4, int16_t, mvbuf)[3][2];
238 MpegEncContext * const s = &h->s;
239 int8_t *ref = s->current_picture.ref_index[0];
240 int16_t (*mv)[2] = s->current_picture.motion_val[0];
241 int top_ref, left_ref, diagonal_ref, match_count, mx, my;
242 const int16_t *A, *B, *C;
243 int b_stride = h->b_stride;
245 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
247 /* To avoid doing an entire fill_decode_caches, we inline the relevant parts here.
248 * FIXME: this is a partial duplicate of the logic in fill_decode_caches, but it's
249 * faster this way. Is there a way to avoid this duplication?
251 if(USES_LIST(h->left_type[LTOP], 0)){
252 left_ref = ref[4*h->left_mb_xy[LTOP] + 1 + (h->left_block[0]&~1)];
253 A = mv[h->mb2b_xy[h->left_mb_xy[LTOP]] + 3 + b_stride*h->left_block[0]];
254 FIX_MV_MBAFF(h->left_type[LTOP], left_ref, A, 0);
255 if(!(left_ref | AV_RN32A(A))){
258 }else if(h->left_type[LTOP]){
259 left_ref = LIST_NOT_USED;
265 if(USES_LIST(h->top_type, 0)){
266 top_ref = ref[4*h->top_mb_xy + 2];
267 B = mv[h->mb2b_xy[h->top_mb_xy] + 3*b_stride];
268 FIX_MV_MBAFF(h->top_type, top_ref, B, 1);
269 if(!(top_ref | AV_RN32A(B))){
272 }else if(h->top_type){
273 top_ref = LIST_NOT_USED;
279 tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
281 if(USES_LIST(h->topright_type, 0)){
282 diagonal_ref = ref[4*h->topright_mb_xy + 2];
283 C = mv[h->mb2b_xy[h->topright_mb_xy] + 3*b_stride];
284 FIX_MV_MBAFF(h->topright_type, diagonal_ref, C, 2);
285 }else if(h->topright_type){
286 diagonal_ref = LIST_NOT_USED;
289 if(USES_LIST(h->topleft_type, 0)){
290 diagonal_ref = ref[4*h->topleft_mb_xy + 1 + (h->topleft_partition & 2)];
291 C = mv[h->mb2b_xy[h->topleft_mb_xy] + 3 + b_stride + (h->topleft_partition & 2*b_stride)];
292 FIX_MV_MBAFF(h->topleft_type, diagonal_ref, C, 2);
293 }else if(h->topleft_type){
294 diagonal_ref = LIST_NOT_USED;
297 diagonal_ref = PART_NOT_AVAILABLE;
302 match_count= !diagonal_ref + !top_ref + !left_ref;
303 tprintf(h->s.avctx, "pred_pskip_motion match_count=%d\n", match_count);
305 mx = mid_pred(A[0], B[0], C[0]);
306 my = mid_pred(A[1], B[1], C[1]);
307 }else if(match_count==1){
319 mx = mid_pred(A[0], B[0], C[0]);
320 my = mid_pred(A[1], B[1], C[1]);
323 fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
326 fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
330 #endif /* AVCODEC_H264_MVPRED_H */