h264: remove assert() immediately following return statement
[libav.git] / libavcodec / h264_mvpred.h
CommitLineData
188d3c51
MN
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... motion vector predicion
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
2912e87a 5 * This file is part of Libav.
188d3c51 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
188d3c51
MN
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
188d3c51
MN
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
188d3c51
MN
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
188d3c51
MN
24 * H.264 / AVC / MPEG4 part10 motion vector predicion.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
e2dd8586
MN
28#ifndef AVCODEC_H264_MVPRED_H
29#define AVCODEC_H264_MVPRED_H
30
188d3c51
MN
31#include "internal.h"
32#include "avcodec.h"
33#include "h264.h"
34
35//#undef NDEBUG
36#include <assert.h>
37
38static 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;
41
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 */
44 if(FRAME_MBAFF){
188d3c51 45
358b5b1a
MN
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];\
188d3c51
MN
49 if(!USES_LIST(mb_type,list))\
50 return LIST_NOT_USED;\
358b5b1a 51 mv = s->current_picture_ptr->motion_val[list][h->mb2b_xy[xy]+3 + y4*h->b_stride];\
188d3c51
MN
52 h->mv_cache[list][scan8[0]-2][0] = mv[0];\
53 h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
358b5b1a 54 return s->current_picture_ptr->ref_index[list][4*xy+1 + (y4&~1)] REF_OP;
188d3c51 55
188d3c51 56 if(topright_ref == PART_NOT_AVAILABLE
580920f8 57 && i >= scan8[0]+8 && (i&7)==4
188d3c51 58 && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
26c83a41
MN
59 const uint32_t *mb_types = s->current_picture_ptr->mb_type;
60 const int16_t *mv;
19769ece 61 AV_ZERO32(h->mv_cache[list][scan8[0]-2]);
26c83a41
MN
62 *C = h->mv_cache[list][scan8[0]-2];
63
188d3c51 64 if(!MB_FIELD
aa5ee59e
MN
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));
188d3c51
MN
67 }
68 if(MB_FIELD
aa5ee59e 69 && !IS_INTERLACED(h->left_type[0])){
188d3c51 70 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
aa5ee59e 71 SET_DIAG_MV(/2, <<1, h->left_mb_xy[i>=36], ((i>>2))&3);
188d3c51
MN
72 }
73 }
74#undef SET_DIAG_MV
75 }
76
77 if(topright_ref != PART_NOT_AVAILABLE){
78 *C= h->mv_cache[list][ i - 8 + part_width ];
79 return topright_ref;
80 }else{
81 tprintf(s->avctx, "topright MV not available\n");
82
83 *C= h->mv_cache[list][ i - 8 - 1 ];
84 return h->ref_cache[list][ i - 8 - 1 ];
85 }
86}
87
88/**
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
94 */
95static 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 ];
101 const int16_t * C;
102 int diagonal_ref, match_count;
103
104 assert(part_width==1 || part_width==2 || part_width==4);
105
106/* mv_cache
107 B . . A T T T T
108 U . . L . . , .
109 U . . L . . . .
110 U . . L . . , .
111 . . . L . . . .
112*/
113
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){
121 if(left_ref==ref){
122 *mx= A[0];
123 *my= A[1];
124 }else if(top_ref==ref){
125 *mx= B[0];
126 *my= B[1];
127 }else{
128 *mx= C[0];
129 *my= C[1];
130 }
131 }else{
132 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
133 *mx= A[0];
134 *my= A[1];
135 }else{
136 *mx= mid_pred(A[0], B[0], C[0]);
137 *my= mid_pred(A[1], B[1], C[1]);
138 }
139 }
140
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);
142}
143
144/**
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
149 */
150static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
151 if(n==0){
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 ];
154
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);
156
157 if(top_ref == ref){
158 *mx= B[0];
159 *my= B[1];
160 return;
161 }
162 }else{
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 ];
165
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);
167
168 if(left_ref == ref){
169 *mx= A[0];
170 *my= A[1];
171 return;
172 }
173 }
174
175 //RARE
176 pred_motion(h, n, 4, list, ref, mx, my);
177}
178
179/**
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
184 */
185static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
186 if(n==0){
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 ];
189
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);
191
192 if(left_ref == ref){
193 *mx= A[0];
194 *my= A[1];
195 return;
196 }
197 }else{
198 const int16_t * C;
199 int diagonal_ref;
200
201 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
202
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);
204
205 if(diagonal_ref == ref){
206 *mx= C[0];
207 *my= C[1];
208 return;
209 }
210 }
211
212 //RARE
213 pred_motion(h, n, 2, list, ref, mx, my);
214}
215
216static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
217 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
218 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
219
220 tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
221
222 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
19769ece
MR
223 || !( top_ref | AV_RN32A(h->mv_cache[0][ scan8[0] - 8 ]))
224 || !(left_ref | AV_RN32A(h->mv_cache[0][ scan8[0] - 1 ]))){
188d3c51
MN
225
226 *mx = *my = 0;
227 return;
228 }
229
230 pred_motion(h, 0, 4, 0, 0, mx, my);
231
232 return;
233}
e2dd8586
MN
234
235#endif /* AVCODEC_H264_MVPRED_H */