comparison h264_mvpred.h @ 10864:e3f5eb016712 libavcodec

Split motion vector prediction off h264.c/h.
author michael
date Tue, 12 Jan 2010 21:36:26 +0000
parents h264.c@974ac220c93a
children 5d34ab807e91
comparison
equal deleted inserted replaced
10863:974ac220c93a 10864:e3f5eb016712
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 /**
23 * @file libavcodec/h264_mvpred.h
24 * H.264 / AVC / MPEG4 part10 motion vector predicion.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include "internal.h"
29 #include "avcodec.h"
30 #include "h264.h"
31
32 //#undef NDEBUG
33 #include <assert.h>
34
35 static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
36 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
37 MpegEncContext *s = &h->s;
38
39 /* there is no consistent mapping of mvs to neighboring locations that will
40 * make mbaff happy, so we can't move all this logic to fill_caches */
41 if(FRAME_MBAFF){
42 const uint32_t *mb_types = s->current_picture_ptr->mb_type;
43 const int16_t *mv;
44 *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
45 *C = h->mv_cache[list][scan8[0]-2];
46
47 if(!MB_FIELD
48 && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
49 int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
50 if(IS_INTERLACED(mb_types[topright_xy])){
51 #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
52 const int x4 = X4, y4 = Y4;\
53 const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
54 if(!USES_LIST(mb_type,list))\
55 return LIST_NOT_USED;\
56 mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
57 h->mv_cache[list][scan8[0]-2][0] = mv[0];\
58 h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
59 return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
60
61 SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
62 }
63 }
64 if(topright_ref == PART_NOT_AVAILABLE
65 && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
66 && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
67 if(!MB_FIELD
68 && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
69 SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
70 }
71 if(MB_FIELD
72 && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
73 && i >= scan8[0]+8){
74 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
75 SET_DIAG_MV(/2, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
76 }
77 }
78 #undef SET_DIAG_MV
79 }
80
81 if(topright_ref != PART_NOT_AVAILABLE){
82 *C= h->mv_cache[list][ i - 8 + part_width ];
83 return topright_ref;
84 }else{
85 tprintf(s->avctx, "topright MV not available\n");
86
87 *C= h->mv_cache[list][ i - 8 - 1 ];
88 return h->ref_cache[list][ i - 8 - 1 ];
89 }
90 }
91
92 /**
93 * gets the predicted MV.
94 * @param n the block index
95 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
96 * @param mx the x component of the predicted motion vector
97 * @param my the y component of the predicted motion vector
98 */
99 static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
100 const int index8= scan8[n];
101 const int top_ref= h->ref_cache[list][ index8 - 8 ];
102 const int left_ref= h->ref_cache[list][ index8 - 1 ];
103 const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
104 const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
105 const int16_t * C;
106 int diagonal_ref, match_count;
107
108 assert(part_width==1 || part_width==2 || part_width==4);
109
110 /* mv_cache
111 B . . A T T T T
112 U . . L . . , .
113 U . . L . . . .
114 U . . L . . , .
115 . . . L . . . .
116 */
117
118 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
119 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
120 tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
121 if(match_count > 1){ //most common
122 *mx= mid_pred(A[0], B[0], C[0]);
123 *my= mid_pred(A[1], B[1], C[1]);
124 }else if(match_count==1){
125 if(left_ref==ref){
126 *mx= A[0];
127 *my= A[1];
128 }else if(top_ref==ref){
129 *mx= B[0];
130 *my= B[1];
131 }else{
132 *mx= C[0];
133 *my= C[1];
134 }
135 }else{
136 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
137 *mx= A[0];
138 *my= A[1];
139 }else{
140 *mx= mid_pred(A[0], B[0], C[0]);
141 *my= mid_pred(A[1], B[1], C[1]);
142 }
143 }
144
145 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);
146 }
147
148 /**
149 * gets the directionally predicted 16x8 MV.
150 * @param n the block index
151 * @param mx the x component of the predicted motion vector
152 * @param my the y component of the predicted motion vector
153 */
154 static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
155 if(n==0){
156 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
157 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
158
159 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);
160
161 if(top_ref == ref){
162 *mx= B[0];
163 *my= B[1];
164 return;
165 }
166 }else{
167 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
168 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
169
170 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);
171
172 if(left_ref == ref){
173 *mx= A[0];
174 *my= A[1];
175 return;
176 }
177 }
178
179 //RARE
180 pred_motion(h, n, 4, list, ref, mx, my);
181 }
182
183 /**
184 * gets the directionally predicted 8x16 MV.
185 * @param n the block index
186 * @param mx the x component of the predicted motion vector
187 * @param my the y component of the predicted motion vector
188 */
189 static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
190 if(n==0){
191 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
192 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
193
194 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);
195
196 if(left_ref == ref){
197 *mx= A[0];
198 *my= A[1];
199 return;
200 }
201 }else{
202 const int16_t * C;
203 int diagonal_ref;
204
205 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
206
207 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);
208
209 if(diagonal_ref == ref){
210 *mx= C[0];
211 *my= C[1];
212 return;
213 }
214 }
215
216 //RARE
217 pred_motion(h, n, 2, list, ref, mx, my);
218 }
219
220 static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
221 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
222 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
223
224 tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
225
226 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
227 || !( top_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ])
228 || !(left_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ])){
229
230 *mx = *my = 0;
231 return;
232 }
233
234 pred_motion(h, 0, 4, 0, 0, mx, my);
235
236 return;
237 }