[svn] / branches / dev-api-3 / xvidcore / src / motion / motion_est.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-3/xvidcore/src/motion/motion_est.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 594 - (view) (download)

1 : Isibaar 3 /**************************************************************************
2 :     *
3 : chl 259 * XVID MPEG-4 VIDEO CODEC
4 :     * motion estimation
5 :     *
6 :     * This program is an implementation of a part of one or more MPEG-4
7 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
8 :     * to use this software module in hardware or software products are
9 :     * advised that its use may infringe existing patents or copyrights, and
10 :     * any such use would be at such party's own risk. The original
11 :     * developer of this software module and his/her company, and subsequent
12 :     * editors and their companies, will have no liability for use of this
13 :     * software or modifications or derivatives thereof.
14 :     *
15 :     * This program is free software; you can redistribute it and/or modify
16 :     * it under the terms of the GNU General Public License as published by
17 :     * the Free Software Foundation; either version 2 of the License, or
18 :     * (at your option) any later version.
19 :     *
20 :     * This program is distributed in the hope that it will be useful,
21 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 :     * GNU General Public License for more details.
24 :     *
25 :     * You should have received a copy of the GNU General Public License
26 :     * along with this program; if not, write to the Free Software
27 :     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 :     *
29 :     *************************************************************************/
30 :    
31 : Isibaar 3 #include <assert.h>
32 :     #include <stdio.h>
33 : chl 96 #include <stdlib.h>
34 : Isibaar 3
35 :     #include "../encoder.h"
36 :     #include "../utils/mbfunctions.h"
37 :     #include "../prediction/mbprediction.h"
38 :     #include "../global.h"
39 :     #include "../utils/timer.h"
40 : Isibaar 580 #include "../image/interpolate8x8.h"
41 : chl 530 #include "motion_est.h"
42 : suxen_drol 118 #include "motion.h"
43 : Isibaar 3 #include "sad.h"
44 : Isibaar 539 #include "../utils/emms.h"
45 : Isibaar 3
46 : chl 530 #define INITIAL_SKIP_THRESH (10)
47 :     #define FINAL_SKIP_THRESH (50)
48 :     #define MAX_SAD00_FOR_SKIP (20)
49 : Isibaar 539 #define MAX_CHROMA_SAD_FOR_SKIP (22)
50 :     #define SKIP_THRESH_B (25)
51 : Isibaar 3
52 : chl 530 #define CHECK_CANDIDATE(X,Y,D) { \
53 :     (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }
54 : Isibaar 3
55 : Isibaar 580 #define GET_REFERENCE(X, Y, REF) { \
56 :     switch ( ((X&1)<<1) + (Y&1) ) \
57 :     { \
58 :     case 0 : REF = data->Ref + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \
59 :     case 1 : REF = data->RefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \
60 :     case 2 : REF = data->RefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \
61 :     default : REF = data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \
62 :     } \
63 :     }
64 :    
65 : chl 530 #define iDiamondSize 2
66 : chl 141
67 : chl 530 static __inline int
68 :     d_mv_bits(int x, int y, const uint32_t iFcode)
69 : Isibaar 3 {
70 : chl 530 int xb, yb;
71 :    
72 :     if (x == 0) xb = 1;
73 :     else {
74 :     if (x < 0) x = -x;
75 :     x += (1 << (iFcode - 1)) - 1;
76 :     x >>= (iFcode - 1);
77 :     if (x > 32) x = 32;
78 :     xb = mvtab[x] + iFcode;
79 : edgomez 195 }
80 : Isibaar 3
81 : chl 530 if (y == 0) yb = 1;
82 :     else {
83 :     if (y < 0) y = -y;
84 :     y += (1 << (iFcode - 1)) - 1;
85 :     y >>= (iFcode - 1);
86 :     if (y > 32) y = 32;
87 :     yb = mvtab[y] + iFcode;
88 :     }
89 :     return xb + yb;
90 : Isibaar 3 }
91 :    
92 :    
93 : Isibaar 539 /* CHECK_CANDIATE FUNCTIONS START */
94 :    
95 : chl 530 static void
96 :     CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
97 : Isibaar 3 {
98 : chl 530 int t;
99 :     const uint8_t * Reference;
100 : Isibaar 3
101 : chl 530 if (( x > data->max_dx) || ( x < data->min_dx)
102 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
103 : suxen_drol 136
104 : chl 530 switch ( ((x&1)<<1) + (y&1) ) {
105 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
106 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
107 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
108 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
109 :     }
110 : chl 259
111 : syskin 594 data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);
112 : Isibaar 3
113 : chl 530 t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
114 :     data->temp[0] += lambda_vec16[data->iQuant] * t;
115 :     data->temp[1] += lambda_vec8[data->iQuant] * t;
116 : chl 326
117 : chl 530 if (data->temp[0] < data->iMinSAD[0]) {
118 :     data->iMinSAD[0] = data->temp[0];
119 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
120 :     *dir = Direction; }
121 : chl 184
122 : chl 530 if (data->temp[1] < data->iMinSAD[1]) {
123 :     data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
124 :     if (data->temp[2] < data->iMinSAD[2]) {
125 :     data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
126 :     if (data->temp[3] < data->iMinSAD[3]) {
127 :     data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
128 :     if (data->temp[4] < data->iMinSAD[4]) {
129 :     data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
130 : chl 184
131 : Isibaar 3 }
132 :    
133 : chl 530 static void
134 :     CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
135 :     {
136 :     int32_t sad;
137 :     const uint8_t * Reference;
138 : chl 326
139 : chl 530 if (( x > data->max_dx) || ( x < data->min_dx)
140 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
141 : Isibaar 3
142 : chl 530 switch ( ((x&1)<<1) + (y&1) )
143 :     {
144 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
145 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
146 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
147 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
148 :     }
149 : Isibaar 3
150 : chl 530 sad = lambda_vec16[data->iQuant] *
151 :     d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
152 : Isibaar 580 sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR);
153 : Isibaar 3
154 : chl 530 if (sad < *(data->iMinSAD)) {
155 :     *(data->iMinSAD) = sad;
156 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
157 :     *dir = Direction; }
158 : Isibaar 3 }
159 :    
160 : chl 530 static void
161 : Isibaar 580 CheckCandidate16_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
162 :    
163 :     // CheckCandidate16 variant which expects x and y in quarter pixel resolution
164 :     // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)
165 :     // around currentMV!
166 :     {
167 :     int t;
168 :     uint8_t * Reference = (uint8_t *) data->RefQ;
169 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
170 :     VECTOR halfpelMV = *(data->currentMV);
171 :    
172 :     int32_t iEdgedWidth = data->iEdgedWidth;
173 :     uint32_t rounding = data->rounding;
174 :    
175 :     if (( x > data->max_dx) || ( x < data->min_dx)
176 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
177 :    
178 :     switch( ((x&1)<<1) + (y&1) )
179 :     {
180 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
181 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference);
182 :     break;
183 :    
184 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
185 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
186 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
187 :    
188 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
189 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);
190 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);
191 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);
192 :     break;
193 :    
194 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
195 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
196 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);
197 :    
198 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
199 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);
200 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);
201 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);
202 :     break;
203 :    
204 :     default: // x and y in qpel resolution - the "corners" (top left/right and
205 :     // bottom left/right) during qpel refinement
206 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
207 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
208 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);
209 :     GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);
210 :    
211 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
212 :     interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
213 :     interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
214 :     interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
215 :     break;
216 :     }
217 :    
218 : syskin 594 data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1);
219 : Isibaar 580
220 :     t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);
221 :     data->temp[0] += lambda_vec16[data->iQuant] * t;
222 :     data->temp[1] += lambda_vec8[data->iQuant] * t;
223 :    
224 :     if (data->temp[0] < data->iMinSAD[0]) {
225 :     data->iMinSAD[0] = data->temp[0];
226 :     data->currentQMV[0].x = x; data->currentQMV[0].y = y;
227 : syskin 594 /* *dir = Direction;*/ }
228 : Isibaar 580
229 :     if (data->temp[1] < data->iMinSAD[1]) {
230 :     data->iMinSAD[1] = data->temp[1]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; }
231 :     if (data->temp[2] < data->iMinSAD[2]) {
232 :     data->iMinSAD[2] = data->temp[2]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; }
233 :     if (data->temp[3] < data->iMinSAD[3]) {
234 :     data->iMinSAD[3] = data->temp[3]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; }
235 :     if (data->temp[4] < data->iMinSAD[4]) {
236 :     data->iMinSAD[4] = data->temp[4]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; }
237 :     }
238 :    
239 :     static void
240 :     CheckCandidate16no4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
241 :    
242 :     // CheckCandidate16no4v variant which expects x and y in quarter pixel resolution
243 :     // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)
244 :     // around currentMV!
245 :     {
246 :     int32_t sad;
247 :     uint8_t * Reference = (uint8_t *) data->RefQ;
248 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
249 :     VECTOR halfpelMV = *(data->currentMV);
250 :    
251 :     int32_t iEdgedWidth = data->iEdgedWidth;
252 :     uint32_t rounding = data->rounding;
253 :    
254 :     if (( x > data->max_dx) || ( x < data->min_dx)
255 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
256 :    
257 :     switch( ((x&1)<<1) + (y&1) )
258 :     {
259 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
260 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference);
261 :     break;
262 :    
263 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
264 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
265 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
266 :    
267 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
268 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);
269 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);
270 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);
271 :     break;
272 :    
273 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
274 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
275 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);
276 :    
277 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
278 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);
279 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);
280 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);
281 :     break;
282 :    
283 :     default: // x and y in qpel resolution - the "corners" (top left/right and
284 :     // bottom left/right) during qpel refinement
285 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
286 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
287 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);
288 :     GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);
289 :    
290 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
291 :     interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
292 :     interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
293 :     interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
294 :     break;
295 :     }
296 :    
297 :     sad = lambda_vec16[data->iQuant] *
298 :     d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);
299 :     sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR);
300 :    
301 :     if (sad < *(data->iMinSAD)) {
302 :     *(data->iMinSAD) = sad;
303 :     data->currentQMV[0].x = x; data->currentQMV[0].y = y;
304 : syskin 594 // *dir = Direction;
305 : Isibaar 580 }
306 :     }
307 :    
308 :     static void
309 : Isibaar 539 CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
310 :     {
311 :     int32_t sad;
312 :    
313 :     if (( x > data->max_dx) || ( x < data->min_dx)
314 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
315 :    
316 :     sad = lambda_vec16[data->iQuant] *
317 :     d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
318 :    
319 :     sad += sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),
320 :     data->iEdgedWidth, 256*4096);
321 :    
322 :     if (sad < *(data->iMinSAD)) {
323 :     *(data->iMinSAD) = sad;
324 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
325 :     *dir = Direction; }
326 :     }
327 :    
328 :    
329 :     static void
330 : chl 530 CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)
331 :     {
332 :     int32_t sad;
333 :     const int xb = data->currentMV[1].x;
334 :     const int yb = data->currentMV[1].y;
335 :     const uint8_t *ReferenceF, *ReferenceB;
336 : Isibaar 3
337 : chl 530 if (( xf > data->max_dx) || ( xf < data->min_dx)
338 :     || ( yf > data->max_dy) || (yf < data->min_dy)) return;
339 : Isibaar 3
340 : chl 530 switch ( ((xf&1)<<1) + (yf&1) ) {
341 :     case 0 : ReferenceF = data->Ref + xf/2 + (yf/2)*(data->iEdgedWidth); break;
342 :     case 1 : ReferenceF = data->RefV + xf/2 + ((yf-1)/2)*(data->iEdgedWidth); break;
343 :     case 2 : ReferenceF = data->RefH + (xf-1)/2 + (yf/2)*(data->iEdgedWidth); break;
344 :     default : ReferenceF = data->RefHV + (xf-1)/2 + ((yf-1)/2)*(data->iEdgedWidth); break;
345 :     }
346 : edgomez 195
347 : chl 530 switch ( ((xb&1)<<1) + (yb&1) ) {
348 :     case 0 : ReferenceB = data->bRef + xb/2 + (yb/2)*(data->iEdgedWidth); break;
349 :     case 1 : ReferenceB = data->bRefV + xb/2 + ((yb-1)/2)*(data->iEdgedWidth); break;
350 :     case 2 : ReferenceB = data->bRefH + (xb-1)/2 + (yb/2)*(data->iEdgedWidth); break;
351 :     default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break;
352 :     }
353 : Isibaar 3
354 : chl 530 sad = lambda_vec16[data->iQuant] *
355 :     ( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) +
356 :     d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode) );
357 : Isibaar 3
358 : chl 530 sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
359 : Isibaar 3
360 : chl 530 if (sad < *(data->iMinSAD)) {
361 :     *(data->iMinSAD) = sad;
362 :     data->currentMV->x = xf; data->currentMV->y = yf;
363 :     *dir = Direction; }
364 : Isibaar 3 }
365 :    
366 : chl 530 static void
367 :     CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
368 : Isibaar 3 {
369 : chl 530 int32_t sad;
370 :     int k;
371 :     const uint8_t *ReferenceF;
372 :     const uint8_t *ReferenceB;
373 :     VECTOR mvs, b_mvs;
374 : Isibaar 3
375 : chl 530 if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
376 : Isibaar 3
377 : chl 530 sad = lambda_vec16[data->iQuant] * d_mv_bits(x, y, 1);
378 : Isibaar 3
379 : chl 530 for (k = 0; k < 4; k++) {
380 :     mvs.x = data->directmvF[k].x + x;
381 :     b_mvs.x = ((x == 0) ?
382 :     data->directmvB[k].x
383 :     : mvs.x - data->referencemv[k].x);
384 : Isibaar 3
385 : chl 530 mvs.y = data->directmvF[k].y + y;
386 :     b_mvs.y = ((y == 0) ?
387 :     data->directmvB[k].y
388 :     : mvs.y - data->referencemv[k].y);
389 :    
390 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
391 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
392 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
393 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
394 : Isibaar 3
395 : chl 530 switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {
396 :     case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;
397 :     case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
398 :     case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;
399 :     default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
400 :     }
401 : Isibaar 3
402 : chl 530 switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {
403 :     case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
404 :     case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
405 :     case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
406 :     default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
407 : chl 344 }
408 : chl 530
409 :     sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
410 :     ReferenceF + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
411 :     ReferenceB + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
412 :     data->iEdgedWidth);
413 :     if (sad > *(data->iMinSAD)) return;
414 : edgomez 78 }
415 : chl 530
416 :     if (sad < *(data->iMinSAD)) {
417 :     *(data->iMinSAD) = sad;
418 :     data->currentMV->x = x; data->currentMV->y = y;
419 :     *dir = Direction; }
420 : Isibaar 3 }
421 :    
422 : chl 530 static void
423 :     CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
424 : chl 96 {
425 : chl 530 int32_t sad;
426 :     const uint8_t *ReferenceF;
427 :     const uint8_t *ReferenceB;
428 :     VECTOR mvs, b_mvs;
429 : chl 96
430 : syskin 576 if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
431 : chl 530
432 :     sad = lambda_vec16[data->iQuant] * d_mv_bits(x, y, 1);
433 : edgomez 195
434 : chl 530 mvs.x = data->directmvF[0].x + x;
435 :     b_mvs.x = ((x == 0) ?
436 :     data->directmvB[0].x
437 :     : mvs.x - data->referencemv[0].x);
438 : edgomez 195
439 : chl 530 mvs.y = data->directmvF[0].y + y;
440 :     b_mvs.y = ((y == 0) ?
441 :     data->directmvB[0].y
442 :     : mvs.y - data->referencemv[0].y);
443 :    
444 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
445 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
446 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
447 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
448 : chl 96
449 : chl 530 switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {
450 :     case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;
451 :     case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
452 :     case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;
453 :     default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
454 :     }
455 : chl 96
456 : chl 530 switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {
457 :     case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
458 :     case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
459 :     case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
460 :     default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
461 :     }
462 :    
463 :     sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
464 : chl 96
465 : chl 530 if (sad < *(data->iMinSAD)) {
466 :     *(data->iMinSAD) = sad;
467 :     data->currentMV->x = x; data->currentMV->y = y;
468 :     *dir = Direction; }
469 :     }
470 : chl 96
471 : chl 530 static void
472 :     CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
473 :     {
474 :     int32_t sad;
475 :     const uint8_t * Reference;
476 : edgomez 195
477 : chl 530 if (( x > data->max_dx) || ( x < data->min_dx)
478 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
479 : chl 96
480 : chl 530 switch ( ((x&1)<<1) + (y&1) )
481 :     {
482 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
483 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
484 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
485 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
486 :     }
487 : edgomez 195
488 : chl 530 sad = sad8(data->Cur, Reference, data->iEdgedWidth);
489 :     sad += lambda_vec8[data->iQuant] * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
490 : edgomez 195
491 : chl 530 if (sad < *(data->iMinSAD)) {
492 :     *(data->iMinSAD) = sad;
493 :     data->currentMV->x = x; data->currentMV->y = y;
494 :     *dir = Direction; }
495 : chl 96 }
496 :    
497 : Isibaar 580 static void
498 :     CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
499 :     // CheckCandidate16no4v variant which expects x and y in quarter pixel resolution
500 :     // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)
501 :     // around currentMV!
502 :    
503 :     {
504 :     int32_t sad;
505 :     uint8_t *Reference = (uint8_t *) data->RefQ;
506 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
507 :     VECTOR halfpelMV = *(data->currentMV);
508 :    
509 :     int32_t iEdgedWidth = data->iEdgedWidth;
510 :     uint32_t rounding = data->rounding;
511 :    
512 :     if (( x > data->max_dx) || ( x < data->min_dx)
513 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
514 :    
515 :     switch( ((x&1)<<1) + (y&1) )
516 :     {
517 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
518 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference);
519 :     break;
520 :    
521 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
522 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
523 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
524 :    
525 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
526 :     break;
527 :    
528 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
529 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
530 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);
531 :    
532 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
533 :     break;
534 :    
535 :     default: // x and y in qpel resolution - the "corners" (top left/right and
536 :     // bottom left/right) during qpel refinement
537 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
538 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
539 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);
540 :     GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);
541 :    
542 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
543 :     break;
544 :     }
545 :    
546 :     sad = sad8(data->Cur, Reference, data->iEdgedWidth);
547 :     sad += lambda_vec8[data->iQuant] * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);
548 :    
549 :     if (sad < *(data->iMinSAD)) {
550 :     *(data->iMinSAD) = sad;
551 :     data->currentQMV->x = x; data->currentQMV->y = y;
552 :     *dir = Direction; }
553 :     }
554 :    
555 : Isibaar 539 /* CHECK_CANDIATE FUNCTIONS END */
556 : chl 96
557 : chl 530 /* MAINSEARCH FUNCTIONS START */
558 : edgomez 195
559 : chl 530 static void
560 :     AdvDiamondSearch(int x, int y, const SearchData * const data, int bDirection)
561 : chl 181 {
562 :    
563 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
564 :    
565 : chl 530 int iDirection;
566 : edgomez 195
567 :     do {
568 : chl 181 iDirection = 0;
569 : chl 530 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
570 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
571 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
572 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
573 : chl 181
574 :     /* now we're doing diagonal checks near our candidate */
575 :    
576 : chl 530 if (iDirection) { //checking if anything found
577 : chl 181 bDirection = iDirection;
578 :     iDirection = 0;
579 : chl 530 x = data->currentMV->x; y = data->currentMV->y;
580 :     if (bDirection & 3) { //our candidate is left or right
581 :     CHECK_CANDIDATE(x, y + iDiamondSize, 8);
582 :     CHECK_CANDIDATE(x, y - iDiamondSize, 4);
583 :     } else { // what remains here is up or down
584 :     CHECK_CANDIDATE(x + iDiamondSize, y, 2);
585 :     CHECK_CANDIDATE(x - iDiamondSize, y, 1); }
586 : chl 181
587 : edgomez 195 if (iDirection) {
588 :     bDirection += iDirection;
589 : chl 530 x = data->currentMV->x; y = data->currentMV->y; }
590 :     } else { //about to quit, eh? not so fast....
591 : edgomez 195 switch (bDirection) {
592 : chl 181 case 2:
593 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
594 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
595 : chl 181 break;
596 :     case 1:
597 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
598 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
599 : chl 181 break;
600 : edgomez 195 case 2 + 4:
601 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
602 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
603 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
604 : chl 181 break;
605 :     case 4:
606 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
607 :     CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
608 : chl 181 break;
609 :     case 8:
610 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
611 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
612 : chl 181 break;
613 : edgomez 195 case 1 + 4:
614 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
615 :     CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
616 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
617 : chl 181 break;
618 : edgomez 195 case 2 + 8:
619 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
620 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
621 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
622 : chl 181 break;
623 : edgomez 195 case 1 + 8:
624 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
625 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
626 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
627 : chl 181 break;
628 : edgomez 195 default: //1+2+4+8 == we didn't find anything at all
629 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
630 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
631 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
632 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
633 : chl 181 break;
634 :     }
635 : chl 530 if (!iDirection) break; //ok, the end. really
636 :     bDirection = iDirection;
637 :     x = data->currentMV->x; y = data->currentMV->y;
638 : chl 181 }
639 :     }
640 : edgomez 195 while (1); //forever
641 : chl 181 }
642 :    
643 : chl 530 static void
644 :     SquareSearch(int x, int y, const SearchData * const data, int bDirection)
645 : chl 326 {
646 : chl 530 int iDirection;
647 : chl 326
648 : chl 530 do {
649 :     iDirection = 0;
650 :     if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1+16+64);
651 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2+32+128);
652 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4+16+32);
653 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8+64+128);
654 :     if (bDirection & 16) CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1+4+16+32+64);
655 :     if (bDirection & 32) CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2+4+16+32+128);
656 :     if (bDirection & 64) CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1+8+16+64+128);
657 :     if (bDirection & 128) CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2+8+32+64+128);
658 : chl 326
659 : chl 530 bDirection = iDirection;
660 :     x = data->currentMV->x; y = data->currentMV->y;
661 :     } while (iDirection);
662 : chl 346 }
663 :    
664 : chl 530 static void
665 :     DiamondSearch(int x, int y, const SearchData * const data, int bDirection)
666 : chl 346 {
667 :    
668 : chl 181 /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
669 :    
670 : chl 530 int iDirection;
671 : edgomez 195
672 :     do {
673 : chl 181 iDirection = 0;
674 : chl 530 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
675 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
676 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
677 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
678 : chl 181
679 :     /* now we're doing diagonal checks near our candidate */
680 :    
681 : chl 530 if (iDirection) { //checking if anything found
682 : chl 181 bDirection = iDirection;
683 :     iDirection = 0;
684 : chl 530 x = data->currentMV->x; y = data->currentMV->y;
685 :     if (bDirection & 3) { //our candidate is left or right
686 :     CHECK_CANDIDATE(x, y + iDiamondSize, 8);
687 :     CHECK_CANDIDATE(x, y - iDiamondSize, 4);
688 :     } else { // what remains here is up or down
689 :     CHECK_CANDIDATE(x + iDiamondSize, y, 2);
690 :     CHECK_CANDIDATE(x - iDiamondSize, y, 1); }
691 : chl 181
692 : chl 530 bDirection += iDirection;
693 :     x = data->currentMV->x; y = data->currentMV->y;
694 : chl 181 }
695 :     }
696 : chl 530 while (iDirection);
697 : chl 181 }
698 :    
699 : chl 530 /* MAINSEARCH FUNCTIONS END */
700 : chl 181
701 : chl 530 /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */
702 :    
703 :     static void
704 :     HalfpelRefine(const SearchData * const data)
705 : chl 96 {
706 : chl 530 /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
707 : edgomez 195
708 : chl 530 VECTOR backupMV = *(data->currentMV);
709 :     int iDirection; //not needed
710 : chl 96
711 : chl 530 CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
712 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
713 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);
714 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);
715 : edgomez 195
716 : chl 530 CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);
717 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);
718 :    
719 :     CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
720 :     CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
721 : chl 96 }
722 :    
723 : Isibaar 580
724 :     static void
725 :     QuarterpelRefine(const SearchData * const data)
726 :     {
727 :     /* Perform quarter pixel refinement*/
728 :    
729 :     VECTOR backupMV = *(data->currentQMV);
730 :     int iDirection; //not needed
731 :    
732 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
733 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
734 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);
735 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);
736 :    
737 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);
738 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);
739 :    
740 :     CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
741 :     CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
742 :    
743 :     }
744 :    
745 : chl 530 static __inline int
746 :     SkipDecisionP(const IMAGE * current, const IMAGE * reference,
747 :     const int x, const int y,
748 :     const uint32_t iEdgedWidth, const uint32_t iQuant)
749 : chl 96
750 : Isibaar 3 {
751 : chl 530 /* keep repeating checks for all b-frames before this P frame,
752 :     to make sure that SKIP is possible (todo)
753 :     how: if skip is not possible set sad00 to a very high value */
754 : Isibaar 3
755 : chl 530 uint32_t sadC = sad8(current->u + x*8 + y*(iEdgedWidth/2)*8,
756 :     reference->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2);
757 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
758 : h 545 sadC += sad8(current->v + (x + y*(iEdgedWidth/2))*8,
759 :     reference->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
760 : chl 530 if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
761 : edgomez 195
762 : chl 530 return 1;
763 :     }
764 : edgomez 195
765 : chl 530 static __inline void
766 :     SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad)
767 :     {
768 :     pMB->mode = MODE_NOT_CODED;
769 : Isibaar 539 pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
770 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
771 : Isibaar 580
772 :     pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0;
773 :     pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0;
774 :    
775 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
776 : Isibaar 3 }
777 :    
778 : chl 530 bool
779 :     MotionEstimation(MBParam * const pParam,
780 :     FRAMEINFO * const current,
781 :     FRAMEINFO * const reference,
782 :     const IMAGE * const pRefH,
783 :     const IMAGE * const pRefV,
784 :     const IMAGE * const pRefHV,
785 :     const uint32_t iLimit)
786 : Isibaar 3 {
787 : chl 530 MACROBLOCK *const pMBs = current->mbs;
788 :     const IMAGE *const pCurrent = &current->image;
789 :     const IMAGE *const pRef = &reference->image;
790 : Isibaar 3
791 : chl 530 const VECTOR zeroMV = { 0, 0 };
792 : Isibaar 3
793 : chl 530 uint32_t x, y;
794 :     uint32_t iIntra = 0;
795 : syskin 574 int32_t InterBias, quant = current->quant;
796 : Isibaar 580 uint8_t *qimage;
797 : edgomez 195
798 : Isibaar 539 // some pre-initialized thingies for SearchP
799 :     int32_t temp[5];
800 :     VECTOR currentMV[5];
801 : Isibaar 580 VECTOR currentQMV[5];
802 : Isibaar 539 int32_t iMinSAD[5];
803 :     SearchData Data;
804 :     Data.iEdgedWidth = pParam->edged_width;
805 :     Data.currentMV = currentMV;
806 : Isibaar 580 Data.currentQMV = currentQMV;
807 : Isibaar 539 Data.iMinSAD = iMinSAD;
808 :     Data.temp = temp;
809 :     Data.iFcode = current->fcode;
810 : Isibaar 580 Data.rounding = pParam->m_rounding_type;
811 : Isibaar 539
812 : Isibaar 580 if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
813 : syskin 594 return 1; // allocate some mem for qpel interpolated blocks
814 : Isibaar 580 // somehow this is dirty since I think we shouldn't use malloc outside
815 :     // encoder_create() - so please fix me!
816 :    
817 : chl 530 if (sadInit) (*sadInit) ();
818 : edgomez 195
819 : chl 530 for (y = 0; y < pParam->mb_height; y++) {
820 :     for (x = 0; x < pParam->mb_width; x++) {
821 : Isibaar 3
822 : chl 530 MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
823 :     int32_t sad00 = pMB->sad16
824 :     = sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16,
825 :     pRef->y + (x + y * pParam->edged_width) * 16,
826 :     pParam->edged_width, pMB->sad8 );
827 : edgomez 195
828 : chl 530 if (!(current->global_flags & XVID_LUMIMASKING)) {
829 :     pMB->dquant = NO_CHANGE;
830 :     pMB->quant = current->quant; }
831 : syskin 574 else
832 :     if (pMB->dquant != NO_CHANGE) {
833 :     quant += DQtab[pMB->dquant];
834 :     if (quant > 31) quant = 31;
835 :     else if (quant < 1) quant = 1;
836 :     pMB->quant = quant;
837 :     }
838 : chl 181
839 : chl 530 //initial skip decision
840 : Isibaar 3
841 : chl 530 if ((pMB->dquant == NO_CHANGE) && (sad00 <= MAX_SAD00_FOR_SKIP * pMB->quant)
842 :     && (SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) ) {
843 :     if (pMB->sad16 < pMB->quant * INITIAL_SKIP_THRESH) {
844 :     SkipMacroblockP(pMB, sad00);
845 :     continue;
846 : Isibaar 580 sad00 = 256 * 4096;
847 : chl 530 }
848 :     } else sad00 = 256*4096; // skip not allowed - for final skip decision
849 : Isibaar 3
850 : Isibaar 580 SearchP(pRef->y, pRefH->y, pRefV->y, pRefHV->y, qimage, pCurrent, x,
851 : chl 530 y, current->motion_flags, pMB->quant,
852 : Isibaar 539 &Data, pParam, pMBs, reference->mbs,
853 : chl 530 current->global_flags & XVID_INTER4V, pMB);
854 : edgomez 195
855 : chl 530 /* final skip decision, a.k.a. "the vector you found, really that good?" */
856 :     if (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)
857 :     if ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH)
858 :     { SkipMacroblockP(pMB, sad00); continue; }
859 :    
860 :     /* finally, intra decision */
861 : Isibaar 3
862 : chl 530 InterBias = MV16_INTER_BIAS;
863 :     if (pMB->quant > 8) InterBias += 50 * (pMB->quant - 8); // to make high quants work
864 :     if (y != 0)
865 :     if ((pMB - pParam->mb_width)->mode == MODE_INTER ) InterBias -= 50;
866 :     if (x != 0)
867 :     if ((pMB - 1)->mode == MODE_INTER ) InterBias -= 50;
868 : Isibaar 3
869 : chl 530 if (InterBias < pMB->sad16) {
870 :     const int32_t deviation =
871 :     dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
872 :     pParam->edged_width);
873 : Isibaar 3
874 : chl 530 if (deviation < (pMB->sad16 - InterBias)) {
875 : Isibaar 580 if (++iIntra >= iLimit) { free(qimage); return 1; }
876 : chl 530 pMB->mode = MODE_INTRA;
877 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =
878 :     pMB->mvs[3] = zeroMV;
879 : Isibaar 580 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] =
880 :     pMB->qmvs[3] = zeroMV;
881 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =
882 :     pMB->sad8[3] = 0;
883 :     }
884 :     }
885 :     }
886 : Isibaar 3 }
887 : Isibaar 580 free(qimage);
888 : chl 530 return 0;
889 :     }
890 : Isibaar 3
891 : edgomez 195
892 : chl 530 #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
893 : Isibaar 3
894 : chl 530 static __inline int
895 :     make_mask(const VECTOR * const pmv, const int i)
896 :     {
897 : Isibaar 539 int mask = 255, j;
898 : chl 530 for (j = 0; j < i; j++) {
899 :     if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already
900 :     if (pmv[i].x == pmv[j].x) {
901 :     if (pmv[i].y == pmv[j].y + iDiamondSize) { mask &= ~4; continue; }
902 :     if (pmv[i].y == pmv[j].y - iDiamondSize) { mask &= ~8; continue; }
903 :     } else
904 :     if (pmv[i].y == pmv[j].y) {
905 :     if (pmv[i].x == pmv[j].x + iDiamondSize) { mask &= ~1; continue; }
906 :     if (pmv[i].x == pmv[j].x - iDiamondSize) { mask &= ~2; continue; }
907 :     }
908 : Isibaar 3 }
909 : chl 530 return mask;
910 :     }
911 : edgomez 195
912 : chl 530 static __inline void
913 :     PreparePredictionsP(VECTOR * const pmv, int x, int y, const int iWcount,
914 :     const int iHcount, const MACROBLOCK * const prevMB)
915 :     {
916 : edgomez 195
917 : chl 530 //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself
918 : edgomez 195
919 : chl 530 if ( (y != 0) && (x != (iWcount-1)) ) { // [5] top-right neighbour
920 :     pmv[5].x = EVEN(pmv[3].x);
921 : Isibaar 539 pmv[5].y = EVEN(pmv[3].y);
922 :     } else pmv[5].x = pmv[5].y = 0;
923 : chl 169
924 : chl 530 if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }// pmv[3] is left neighbour
925 :     else pmv[3].x = pmv[3].y = 0;
926 : Isibaar 3
927 : chl 530 if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }// [4] top neighbour
928 :     else pmv[4].x = pmv[4].y = 0;
929 : chl 169
930 : chl 530 // [1] median prediction
931 :     pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
932 : chl 169
933 : chl 530 pmv[0].x = pmv[0].y = 0; // [0] is zero; not used in the loop (checked before) but needed here for make_mask
934 : chl 169
935 : chl 530 pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame
936 :     pmv[2].y = EVEN(prevMB->mvs[0].y);
937 : chl 169
938 : chl 530 if ((x != iWcount-1) && (y != iHcount-1)) {
939 :     pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame
940 : Isibaar 539 pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
941 :     } else pmv[6].x = pmv[6].y = 0;
942 : chl 530 }
943 : chl 169
944 : chl 530 static void
945 :     SearchP(const uint8_t * const pRef,
946 :     const uint8_t * const pRefH,
947 :     const uint8_t * const pRefV,
948 :     const uint8_t * const pRefHV,
949 : Isibaar 580 const uint8_t * const pRefQ,
950 : chl 530 const IMAGE * const pCur,
951 :     const int x,
952 :     const int y,
953 :     const uint32_t MotionFlags,
954 :     const uint32_t iQuant,
955 : Isibaar 539 SearchData * const Data,
956 : chl 530 const MBParam * const pParam,
957 :     const MACROBLOCK * const pMBs,
958 :     const MACROBLOCK * const prevMBs,
959 :     int inter4v,
960 :     MACROBLOCK * const pMB)
961 :     {
962 : chl 169
963 : chl 530 int i, iDirection = 255, mask, threshA;
964 : Isibaar 539 VECTOR pmv[7];
965 : Isibaar 3
966 : Isibaar 580 Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
967 :    
968 : Isibaar 539 get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)()
969 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
970 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
971 : Isibaar 3
972 : Isibaar 539 Data->predMV = pmv[0];
973 : Isibaar 580
974 : Isibaar 539 Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
975 :     Data->Ref = pRef + (x + Data->iEdgedWidth*y)*16;
976 :     Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
977 :     Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
978 :     Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
979 : Isibaar 580 Data->RefQ = pRefQ;
980 : Isibaar 3
981 : Isibaar 539 Data->iQuant = iQuant;
982 : chl 169
983 : chl 530 if (!(MotionFlags & PMV_HALFPEL16)) {
984 : Isibaar 539 Data->min_dx = EVEN(Data->min_dx);
985 :     Data->max_dx = EVEN(Data->max_dx);
986 :     Data->min_dy = EVEN(Data->min_dy);
987 :     Data->max_dy = EVEN(Data->max_dy); }
988 : edgomez 568
989 :     if (pMB->dquant != NO_CHANGE) inter4v = 0;
990 : edgomez 195
991 : Isibaar 539 if (inter4v) CheckCandidate = CheckCandidate16;
992 :     else CheckCandidate = CheckCandidate16no4v;
993 : chl 169
994 : Isibaar 580 for(i = 0; i < 5; i++)
995 :     Data->currentMV[i].x = Data->currentMV[i].y = 0;
996 : Isibaar 539
997 :     i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);
998 :     Data->iMinSAD[0] = pMB->sad16 + lambda_vec16[iQuant] * i;
999 :     Data->iMinSAD[1] = pMB->sad8[0] + lambda_vec8[iQuant] * i;
1000 :     Data->iMinSAD[2] = pMB->sad8[1];
1001 :     Data->iMinSAD[3] = pMB->sad8[2];
1002 :     Data->iMinSAD[4] = pMB->sad8[3];
1003 :    
1004 : chl 530 if ((x == 0) && (y == 0)) threshA = 512;
1005 :     else {
1006 : edgomez 568 threshA = Data->temp[0]; // that's when we keep this SAD atm
1007 : chl 530 if (threshA < 512) threshA = 512;
1008 :     if (threshA > 1024) threshA = 1024; }
1009 : Isibaar 3
1010 : chl 530 PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
1011 :     prevMBs + x + y * pParam->mb_width);
1012 : edgomez 195
1013 : edgomez 568 if (inter4v) CheckCandidate = CheckCandidate16;
1014 :     else CheckCandidate = CheckCandidate16no4v;
1015 :    
1016 :    
1017 : chl 530 /* main loop. checking all predictions */
1018 :    
1019 :     for (i = 1; i < 7; i++) {
1020 :     if (!(mask = make_mask(pmv, i)) ) continue;
1021 : edgomez 568 (*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1022 : Isibaar 539 if (Data->iMinSAD[0] <= threshA) break;
1023 : chl 530 }
1024 :    
1025 : Isibaar 539 if ((Data->iMinSAD[0] <= threshA) ||
1026 :     (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
1027 :     (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {
1028 : chl 530 inter4v = 0;
1029 : Isibaar 539 } else {
1030 : Isibaar 3
1031 : Isibaar 539 MainSearchFunc * MainSearchPtr;
1032 :     if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1033 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1034 :     else MainSearchPtr = DiamondSearch;
1035 : chl 181
1036 : Isibaar 539 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
1037 : Isibaar 3
1038 : chl 530 /* extended search, diamond starting in 0,0 and in prediction.
1039 :     note that this search is/might be done in halfpel positions,
1040 :     which makes it more different than the diamond above */
1041 : chl 259
1042 : Isibaar 539 if (MotionFlags & PMV_EXTSEARCH16) {
1043 :     int32_t bSAD;
1044 :     VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
1045 :     if (!(MotionFlags & PMV_HALFPELREFINE16)) // who's gonna use extsearch and no halfpel?
1046 :     startMV.x = EVEN(startMV.x); startMV.y = EVEN(startMV.y);
1047 :     if (!(MVequal(startMV, backupMV))) {
1048 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1049 : Isibaar 3
1050 : Isibaar 539 CheckCandidate16(startMV.x, startMV.y, 255, &iDirection, Data);
1051 :     (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1052 :     if (bSAD < Data->iMinSAD[0]) {
1053 :     Data->currentMV[0] = backupMV;
1054 :     Data->iMinSAD[0] = bSAD; }
1055 :     }
1056 : Isibaar 3
1057 : Isibaar 539 backupMV = Data->currentMV[0];
1058 :     if (MotionFlags & PMV_HALFPELREFINE16) startMV.x = startMV.y = 1;
1059 :     else startMV.x = startMV.y = 0;
1060 :     if (!(MVequal(startMV, backupMV))) {
1061 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1062 : edgomez 195
1063 : Isibaar 539 CheckCandidate16(startMV.x, startMV.y, 255, &iDirection, Data);
1064 :     (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1065 :     if (bSAD < Data->iMinSAD[0]) {
1066 :     Data->currentMV[0] = backupMV;
1067 :     Data->iMinSAD[0] = bSAD; }
1068 :     }
1069 : Isibaar 3 }
1070 :     }
1071 :    
1072 : Isibaar 539 if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);
1073 : Isibaar 3
1074 : Isibaar 580 for(i = 0; i < 5; i++) {
1075 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1076 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1077 :     }
1078 :    
1079 :     if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1080 :    
1081 :     if(inter4v)
1082 :     CheckCandidate = CheckCandidate16_qpel;
1083 :     else
1084 :     CheckCandidate = CheckCandidate16no4v_qpel;
1085 : syskin 594
1086 :     Data->iMinSAD[0] -= lambda_vec16[iQuant] *
1087 :     d_mv_bits(Data->predMV.x - Data->currentMV[0].x, Data->predMV.y - Data->currentMV[0].y, Data->iFcode);
1088 :     Data->iMinSAD[1] -= lambda_vec8[iQuant] *
1089 :     d_mv_bits(Data->predMV.x - Data->currentMV[1].x, Data->predMV.y - Data->currentMV[1].y, Data->iFcode);
1090 :    
1091 :     Data->iMinSAD[0] += lambda_vec16[iQuant] *
1092 :     d_mv_bits(Data->predQMV.x - Data->currentQMV[0].x, Data->predMV.y - Data->currentQMV[0].y, Data->iFcode);
1093 :     Data->iMinSAD[1] += lambda_vec8[iQuant] *
1094 :     d_mv_bits(Data->predQMV.x - Data->currentQMV[1].x, Data->predMV.y - Data->currentQMV[1].y, Data->iFcode);
1095 :    
1096 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1097 :     pParam->width, pParam->height, Data->iFcode, 0);
1098 :    
1099 : Isibaar 580 QuarterpelRefine(Data);
1100 :     }
1101 :    
1102 : Isibaar 539 if (inter4v) {
1103 :     SearchData Data8;
1104 :     Data8.iFcode = Data->iFcode;
1105 :     Data8.iQuant = Data->iQuant;
1106 :     Data8.iEdgedWidth = Data->iEdgedWidth;
1107 :     Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1108 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1109 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1110 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1111 :     }
1112 : Isibaar 3
1113 : chl 530 if (!(inter4v) ||
1114 : Isibaar 539 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
1115 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
1116 : chl 530 // INTER MODE
1117 :     pMB->mode = MODE_INTER;
1118 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1]
1119 :     = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1120 : Isibaar 3
1121 : Isibaar 580 pMB->qmvs[0] = pMB->qmvs[1]
1122 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1123 :    
1124 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1125 : Isibaar 539 pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1126 : Isibaar 3
1127 : Isibaar 580 if(pParam->m_quarterpel) {
1128 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;
1129 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;
1130 :     }
1131 :     else {
1132 :     pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1133 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1134 :     }
1135 : edgomez 195 } else {
1136 : chl 530 // INTER4V MODE; all other things are already set in Search8
1137 :     pMB->mode = MODE_INTER4V;
1138 : Isibaar 539 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] +
1139 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * iQuant;
1140 : edgomez 78 }
1141 : Isibaar 3 }
1142 :    
1143 : chl 530 static void
1144 :     Search8(const SearchData * const OldData,
1145 :     const int x, const int y,
1146 :     const uint32_t MotionFlags,
1147 :     const MBParam * const pParam,
1148 :     MACROBLOCK * const pMB,
1149 :     const MACROBLOCK * const pMBs,
1150 : Isibaar 539 const int block,
1151 :     SearchData * const Data)
1152 : chl 345 {
1153 : Isibaar 539 Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1154 : Isibaar 580 Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1155 : Isibaar 539 Data->iMinSAD = OldData->iMinSAD + 1 + block;
1156 :     Data->currentMV = OldData->currentMV + 1 + block;
1157 : Isibaar 580 Data->currentQMV = OldData->currentQMV + 1 + block;
1158 : chl 345
1159 : syskin 594 if(pParam->m_quarterpel) {
1160 :     //it is qpel. substract d_mv_bits[qpel] from 0, add d_mv_bits[hpel] everywhere
1161 :     if (block == 0)
1162 :     *(Data->iMinSAD) -= lambda_vec8[Data->iQuant] *
1163 : Isibaar 580 d_mv_bits( Data->currentQMV->x - Data->predQMV.x,
1164 :     Data->currentQMV->y - Data->predQMV.y,
1165 :     Data->iFcode);
1166 : syskin 594
1167 :     *(Data->iMinSAD) += lambda_vec8[Data->iQuant] *
1168 : Isibaar 580 d_mv_bits( Data->currentMV->x - Data->predMV.x,
1169 :     Data->currentMV->y - Data->predMV.y,
1170 :     Data->iFcode);
1171 : syskin 594 } else //it is not qpel. add d_mv_bits[hpel] everywhere but not in 0 (it's already there)
1172 :     if (block != 0) *(Data->iMinSAD) += lambda_vec8[Data->iQuant] *
1173 :     d_mv_bits( Data->currentMV->x - Data->predMV.x,
1174 :     Data->currentMV->y - Data->predMV.y,
1175 :     Data->iFcode);
1176 : chl 345
1177 : Isibaar 580
1178 : chl 530 if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) {
1179 : chl 345
1180 : Isibaar 539 Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1));
1181 :     Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));
1182 :     Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1183 :     Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1184 : Isibaar 580 Data->RefQ = OldData->RefQ;
1185 : chl 345
1186 : Isibaar 539 Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));
1187 : chl 530
1188 : Isibaar 539 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1189 : Isibaar 580 pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);
1190 : chl 345
1191 : chl 530 CheckCandidate = CheckCandidate8;
1192 : chl 345
1193 : chl 530 if (MotionFlags & PMV_EXTSEARCH8) {
1194 : Isibaar 580 int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1195 :    
1196 : chl 530 MainSearchFunc *MainSearchPtr;
1197 :     if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;
1198 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1199 :     else MainSearchPtr = DiamondSearch;
1200 : chl 345
1201 : Isibaar 580 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1202 : chl 345
1203 : Isibaar 580 if(*(Data->iMinSAD) < temp_sad) {
1204 :     Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1205 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1206 :     }
1207 :     }
1208 :    
1209 :     if (MotionFlags & PMV_HALFPELREFINE8) {
1210 :     int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1211 :    
1212 :     HalfpelRefine(Data); // perform halfpel refine of current best vector
1213 :    
1214 :     if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1215 :     Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1216 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1217 :     }
1218 :     }
1219 :    
1220 :     if(pParam->m_quarterpel) {
1221 :     if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&
1222 :     (MotionFlags & PMV_QUARTERPELREFINE8)) {
1223 :    
1224 :     CheckCandidate = CheckCandidate8_qpel;
1225 : syskin 594 Data->iMinSAD[0] -= lambda_vec8[Data->iQuant] *
1226 :     d_mv_bits(Data->predMV.x - Data->currentMV[0].x, Data->predMV.y - Data->currentMV[0].y, Data->iFcode);
1227 :    
1228 :     Data->iMinSAD[0] += lambda_vec8[Data->iQuant] *
1229 :     d_mv_bits(Data->predQMV.x - Data->currentQMV[0].x, Data->predQMV.y - Data->currentQMV[0].y, Data->iFcode);
1230 :    
1231 : Isibaar 580 QuarterpelRefine(Data);
1232 :     }
1233 :     }
1234 : chl 345 }
1235 : chl 530
1236 : Isibaar 580 if(pParam->m_quarterpel) {
1237 :     pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x;
1238 :     pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y;
1239 :     }
1240 :     else {
1241 :     pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
1242 :     pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
1243 :     }
1244 :    
1245 : Isibaar 539 pMB->mvs[block] = *(Data->currentMV);
1246 : Isibaar 580 pMB->qmvs[block] = *(Data->currentQMV);
1247 :    
1248 : Isibaar 539 pMB->sad8[block] = 4 * (*Data->iMinSAD);
1249 : chl 345 }
1250 :    
1251 : chl 530 /* B-frames code starts here */
1252 : chl 345
1253 : chl 530 static __inline VECTOR
1254 :     ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
1255 : Isibaar 3 {
1256 : chl 530 /* the stupidiest function ever */
1257 :     if (mode == MODE_FORWARD) return pMB->mvs[0];
1258 :     else return pMB->b_mvs[0];
1259 : Isibaar 3 }
1260 :    
1261 : chl 530 static void __inline
1262 :     PreparePredictionsBF(VECTOR * const pmv, const int x, const int y,
1263 :     const uint32_t iWcount,
1264 :     const MACROBLOCK * const pMB,
1265 :     const uint32_t mode_curr)
1266 : Isibaar 3 {
1267 :    
1268 : chl 530 // [0] is prediction
1269 :     pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);
1270 : Isibaar 3
1271 : chl 530 pmv[1].x = pmv[1].y = 0; // [1] is zero
1272 : Isibaar 3
1273 : chl 530 pmv[2] = ChoosePred(pMB, mode_curr);
1274 :     pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);
1275 : edgomez 195
1276 : chl 530 if ((y != 0)&&(x != (int)(iWcount+1))) { // [3] top-right neighbour
1277 :     pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);
1278 : Isibaar 539 pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);
1279 :     } else pmv[3].x = pmv[3].y = 0;
1280 : Isibaar 3
1281 : chl 530 if (y != 0) {
1282 :     pmv[4] = ChoosePred(pMB-iWcount, mode_curr);
1283 :     pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y);
1284 :     } else pmv[4].x = pmv[4].y = 0;
1285 : Isibaar 3
1286 : chl 530 if (x != 0) {
1287 :     pmv[5] = ChoosePred(pMB-1, mode_curr);
1288 :     pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1289 :     } else pmv[5].x = pmv[5].y = 0;
1290 : Isibaar 3
1291 : chl 530 if ((x != 0)&&(y != 0)) {
1292 :     pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);
1293 :     pmv[6].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1294 :     } else pmv[6].x = pmv[6].y = 0;
1295 : edgomez 195
1296 : chl 530 // more?
1297 :     }
1298 : chl 181
1299 : edgomez 170
1300 : chl 530 /* search backward or forward, for b-frames */
1301 :     static void
1302 :     SearchBF( const uint8_t * const pRef,
1303 :     const uint8_t * const pRefH,
1304 :     const uint8_t * const pRefV,
1305 :     const uint8_t * const pRefHV,
1306 :     const IMAGE * const pCur,
1307 :     const int x, const int y,
1308 :     const uint32_t MotionFlags,
1309 :     const uint32_t iFcode,
1310 :     const MBParam * const pParam,
1311 :     MACROBLOCK * const pMB,
1312 :     const VECTOR * const predMV,
1313 :     int32_t * const best_sad,
1314 : h 545 const int32_t mode_current,
1315 :     SearchData * const Data)
1316 : chl 530 {
1317 : Isibaar 3
1318 : chl 530 const int32_t iEdgedWidth = pParam->edged_width;
1319 :    
1320 :     int i, iDirection, mask;
1321 : h 545 VECTOR pmv[7];
1322 : chl 530 MainSearchFunc *MainSearchPtr;
1323 : h 545 *Data->iMinSAD = MV_MAX_ERROR;
1324 :     Data->iFcode = iFcode;
1325 : Isibaar 3
1326 : h 545 Data->Ref = pRef + (x + y * iEdgedWidth) * 16;
1327 :     Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;
1328 :     Data->RefV = pRefV + (x + y * iEdgedWidth) * 16;
1329 :     Data->RefHV = pRefHV + (x + y * iEdgedWidth) * 16;
1330 : Isibaar 3
1331 : h 545 Data->predMV = *predMV;
1332 : edgomez 195
1333 : h 545 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1334 : Isibaar 580 pParam->width, pParam->height, iFcode, pParam->m_quarterpel);
1335 : Isibaar 3
1336 : h 545 pmv[0] = Data->predMV;
1337 :     PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);
1338 : edgomez 195
1339 : h 545 Data->currentMV->x = Data->currentMV->y = 0;
1340 : Isibaar 3
1341 : chl 530 CheckCandidate = CheckCandidate16no4v;
1342 : chl 181
1343 : chl 530 // main loop. checking all predictions
1344 :     for (i = 0; i < 8; i++) {
1345 :     if (!(mask = make_mask(pmv, i)) ) continue;
1346 : h 545 CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1347 : edgomez 78 }
1348 : Isibaar 3
1349 : chl 530 if (MotionFlags & PMV_USESQUARES16)
1350 :     MainSearchPtr = SquareSearch;
1351 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1352 :     MainSearchPtr = AdvDiamondSearch;
1353 :     else MainSearchPtr = DiamondSearch;
1354 : chl 96
1355 : h 545 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1356 : chl 169
1357 : h 545 HalfpelRefine(Data);
1358 : chl 169
1359 : chl 530 // three bits are needed to code backward mode. four for forward
1360 :     // we treat the bits just like they were vector's
1361 : h 545 if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * lambda_vec16[Data->iQuant];
1362 :     else *Data->iMinSAD += 3 * lambda_vec16[Data->iQuant];
1363 : chl 169
1364 :    
1365 : h 545 if (*Data->iMinSAD < *best_sad) {
1366 :     *best_sad = *Data->iMinSAD;
1367 : chl 530 pMB->mode = mode_current;
1368 : h 545 pMB->pmvs[0].x = Data->currentMV->x - predMV->x;
1369 :     pMB->pmvs[0].y = Data->currentMV->y - predMV->y;
1370 :     if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV;
1371 :     else pMB->b_mvs[0] = *Data->currentMV;
1372 : chl 169 }
1373 : chl 530
1374 :     }
1375 : chl 169
1376 : chl 530 static int32_t
1377 : Isibaar 539 SearchDirect(const IMAGE * const f_Ref,
1378 : chl 530 const uint8_t * const f_RefH,
1379 :     const uint8_t * const f_RefV,
1380 :     const uint8_t * const f_RefHV,
1381 : Isibaar 539 const IMAGE * const b_Ref,
1382 : chl 530 const uint8_t * const b_RefH,
1383 :     const uint8_t * const b_RefV,
1384 :     const uint8_t * const b_RefHV,
1385 :     const IMAGE * const pCur,
1386 :     const int x, const int y,
1387 :     const uint32_t MotionFlags,
1388 :     const int32_t TRB, const int32_t TRD,
1389 :     const MBParam * const pParam,
1390 :     MACROBLOCK * const pMB,
1391 :     const MACROBLOCK * const b_mb,
1392 : h 545 int32_t * const best_sad,
1393 :     SearchData * const Data)
1394 : edgomez 195
1395 : chl 530 {
1396 : h 545 int32_t skip_sad;
1397 : chl 530 int k;
1398 : h 545
1399 : chl 530 MainSearchFunc *MainSearchPtr;
1400 : Isibaar 3
1401 : h 545 *Data->iMinSAD = 256*4096;
1402 :     Data->referencemv = b_mb->mvs;
1403 : chl 140
1404 : h 545 Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1405 :     Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16;
1406 :     Data->RefV = f_RefV + (x + Data->iEdgedWidth*y) * 16;
1407 :     Data->RefHV = f_RefHV + (x + Data->iEdgedWidth*y) * 16;
1408 :     Data->bRef = b_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1409 :     Data->bRefH = b_RefH + (x + Data->iEdgedWidth*y) * 16;
1410 :     Data->bRefV = b_RefV + (x + Data->iEdgedWidth*y) * 16;
1411 :     Data->bRefHV = b_RefHV + (x + Data->iEdgedWidth*y) * 16;
1412 : chl 140
1413 : h 545 Data->max_dx = 2 * pParam->width - 2 * (x) * 16;
1414 :     Data->max_dy = 2 * pParam->height - 2 * (y) * 16;
1415 :     Data->min_dx = -(2 * 16 + 2 * (x) * 16);
1416 :     Data->min_dy = -(2 * 16 + 2 * (y) * 16);
1417 : Isibaar 3
1418 : chl 530 for (k = 0; k < 4; k++) {
1419 : h 545 pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);
1420 :     pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD;
1421 :     pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);
1422 :     pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;
1423 : Isibaar 3
1424 : h 545 if ( ( pMB->b_mvs[k].x > Data->max_dx ) || ( pMB->b_mvs[k].x < Data->min_dx )
1425 :     || ( pMB->b_mvs[k].y > Data->max_dy ) || ( pMB->b_mvs[k].y < Data->min_dy )) {
1426 : Isibaar 3
1427 : Isibaar 539 *best_sad = 256*4096; // in that case, we won't use direct mode
1428 :     pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"
1429 :     pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;
1430 :     return 0;
1431 :     }
1432 :     if (b_mb->mode != MODE_INTER4V) {
1433 :     pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0];
1434 :     pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0];
1435 : h 545 Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];
1436 :     Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];
1437 : Isibaar 539 break;
1438 :     }
1439 :     }
1440 : Isibaar 3
1441 : Isibaar 539 if (b_mb->mode == MODE_INTER4V)
1442 :     CheckCandidate = CheckCandidateDirect;
1443 :     else CheckCandidate = CheckCandidateDirectno4v;
1444 : edgomez 195
1445 : h 545 (*CheckCandidate)(0, 0, 255, &k, Data);
1446 : Isibaar 3
1447 : chl 530 // skip decision
1448 : h 545 if (*Data->iMinSAD - 2 * lambda_vec16[Data->iQuant] < (int32_t)Data->iQuant * SKIP_THRESH_B) {
1449 : syskin 594 //possible skip - checking chroma. everything copied from MC
1450 : Isibaar 539 //this is not full chroma compensation, only it's fullpel approximation. should work though
1451 :     int sum, dx, dy, b_dx, b_dy;
1452 : Isibaar 3
1453 : Isibaar 539 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1454 :     dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1455 :    
1456 :     sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1457 :     dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1458 :    
1459 :     sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1460 :     b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1461 :    
1462 :     sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1463 :     b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1464 :    
1465 : h 545 sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2),
1466 :     f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1467 :     b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
1468 :     Data->iEdgedWidth/2);
1469 :     sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2),
1470 :     f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1471 :     b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
1472 :     Data->iEdgedWidth/2);
1473 : Isibaar 539
1474 : h 545 if ((uint32_t) sum < MAX_CHROMA_SAD_FOR_SKIP * Data->iQuant) {
1475 : Isibaar 539 pMB->mode = MODE_DIRECT_NONE_MV;
1476 : h 545 return *Data->iMinSAD;
1477 : Isibaar 539 }
1478 :     }
1479 :    
1480 : h 545 skip_sad = *Data->iMinSAD;
1481 : edgomez 195
1482 : chl 530 // DIRECT MODE DELTA VECTOR SEARCH.
1483 :     // This has to be made more effective, but at the moment I'm happy it's running at all
1484 : Isibaar 3
1485 : chl 530 if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1486 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1487 :     else MainSearchPtr = DiamondSearch;
1488 : edgomez 195
1489 : h 545 (*MainSearchPtr)(0, 0, Data, 255);
1490 : Isibaar 3
1491 : h 545 HalfpelRefine(Data);
1492 : Isibaar 3
1493 : h 545 *Data->iMinSAD += 1 * lambda_vec16[Data->iQuant]; // one bit is needed to code direct mode. we treat this bit just like it was vector's
1494 :     *best_sad = *Data->iMinSAD;
1495 : Isibaar 3
1496 : chl 530 if (b_mb->mode == MODE_INTER4V)
1497 :     pMB->mode = MODE_DIRECT;
1498 :     else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation
1499 : edgomez 195
1500 : h 545 pMB->pmvs[3] = *Data->currentMV;
1501 : edgomez 195
1502 : chl 530 for (k = 0; k < 4; k++) {
1503 : h 545 pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x;
1504 :     pMB->b_mvs[k].x = ((Data->currentMV->x == 0)
1505 :     ? Data->directmvB[k].x
1506 :     : pMB->mvs[k].x - Data->referencemv[k].x);
1507 :     pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y);
1508 :     pMB->b_mvs[k].y = ((Data->currentMV->y == 0)
1509 :     ? Data->directmvB[k].y
1510 :     : pMB->mvs[k].y - Data->referencemv[k].y);
1511 : chl 530 if (b_mb->mode != MODE_INTER4V) {
1512 :     pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];
1513 :     pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];
1514 :     break;
1515 :     }
1516 :     }
1517 : h 545 return skip_sad;
1518 : Isibaar 3 }
1519 : chl 96
1520 : h 545
1521 : chl 530 static __inline void
1522 :     SearchInterpolate(const uint8_t * const f_Ref,
1523 :     const uint8_t * const f_RefH,
1524 :     const uint8_t * const f_RefV,
1525 :     const uint8_t * const f_RefHV,
1526 :     const uint8_t * const b_Ref,
1527 :     const uint8_t * const b_RefH,
1528 :     const uint8_t * const b_RefV,
1529 :     const uint8_t * const b_RefHV,
1530 :     const IMAGE * const pCur,
1531 :     const int x, const int y,
1532 :     const uint32_t fcode,
1533 :     const uint32_t bcode,
1534 :     const uint32_t MotionFlags,
1535 :     const MBParam * const pParam,
1536 :     const VECTOR * const f_predMV,
1537 :     const VECTOR * const b_predMV,
1538 :     MACROBLOCK * const pMB,
1539 : h 545 int32_t * const best_sad,
1540 :     SearchData * const fData)
1541 : chl 530
1542 : chl 96 {
1543 :    
1544 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1545 : chl 96
1546 : chl 530 int iDirection, i, j;
1547 : h 545 SearchData bData;
1548 : chl 96
1549 : h 545 bData.iMinSAD = fData->iMinSAD;
1550 :     *bData.iMinSAD = 4096*256;
1551 :     bData.Cur = fData->Cur;
1552 :     fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth;
1553 :     bData.currentMV = fData->currentMV + 1;
1554 :     bData.iQuant = fData->iQuant;
1555 :     fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
1556 : edgomez 195
1557 : h 545 bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16;
1558 :     bData.bRefH = fData->RefH = f_RefH + (x + y * iEdgedWidth) * 16;
1559 :     bData.bRefV = fData->RefV = f_RefV + (x + y * iEdgedWidth) * 16;
1560 :     bData.bRefHV = fData->RefHV = f_RefHV + (x + y * iEdgedWidth) * 16;
1561 :     bData.Ref = fData->bRef = b_Ref + (x + y * iEdgedWidth) * 16;
1562 :     bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16;
1563 :     bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;
1564 :     bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;
1565 : chl 96
1566 : h 545 bData.bpredMV = fData->predMV = *f_predMV;
1567 :     fData->bpredMV = bData.predMV = *b_predMV;
1568 : edgomez 195
1569 : h 545 fData->currentMV[0] = pMB->mvs[0];
1570 :     fData->currentMV[1] = pMB->b_mvs[0];
1571 : Isibaar 580 get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel);
1572 :     get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel);
1573 : chl 96
1574 : h 545 if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;
1575 :     if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dy;
1576 :     if (fData->currentMV[0].y > fData->max_dy) fData->currentMV[0].y = fData->max_dx;
1577 :     if (fData->currentMV[0].y > fData->min_dy) fData->currentMV[0].y = fData->min_dy;
1578 : Isibaar 539
1579 : h 545 if (fData->currentMV[1].x > bData.max_dx) fData->currentMV[1].x = bData.max_dx;
1580 :     if (fData->currentMV[1].x < bData.min_dx) fData->currentMV[1].x = bData.min_dy;
1581 :     if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dx;
1582 :     if (fData->currentMV[1].y > bData.min_dy) fData->currentMV[1].y = bData.min_dy;
1583 : Isibaar 539
1584 : h 545 CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
1585 : chl 96
1586 : chl 530 //diamond. I wish we could use normal mainsearch functions (square, advdiamond)
1587 : chl 96
1588 : chl 530 do {
1589 :     iDirection = 255;
1590 :     // forward MV moves
1591 : h 545 i = fData->currentMV[0].x; j = fData->currentMV[0].y;
1592 : chl 96
1593 : h 545 CheckCandidateInt(i + 1, j, 0, &iDirection, fData);
1594 :     CheckCandidateInt(i, j + 1, 0, &iDirection, fData);
1595 :     CheckCandidateInt(i - 1, j, 0, &iDirection, fData);
1596 :     CheckCandidateInt(i, j - 1, 0, &iDirection, fData);
1597 : chl 96
1598 : chl 530 // backward MV moves
1599 : h 545 i = fData->currentMV[1].x; j = fData->currentMV[1].y;
1600 :     fData->currentMV[2] = fData->currentMV[0];
1601 : chl 326
1602 : Isibaar 539 CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);
1603 :     CheckCandidateInt(i, j + 1, 0, &iDirection, &bData);
1604 :     CheckCandidateInt(i - 1, j, 0, &iDirection, &bData);
1605 :     CheckCandidateInt(i, j - 1, 0, &iDirection, &bData);
1606 : chl 96
1607 : chl 530 } while (!(iDirection));
1608 : edgomez 195
1609 : chl 530 // two bits are needed to code interpolate mode. we treat the bits just like they were vector's
1610 : h 545 *fData->iMinSAD += 2 * lambda_vec16[fData->iQuant];
1611 :     if (*fData->iMinSAD < *best_sad) {
1612 :     *best_sad = *fData->iMinSAD;
1613 :     pMB->mvs[0] = fData->currentMV[0];
1614 :     pMB->b_mvs[0] = fData->currentMV[1];
1615 : chl 530 pMB->mode = MODE_INTERPOLATE;
1616 : chl 96
1617 : chl 530 pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;
1618 :     pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;
1619 :     pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;
1620 :     pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;
1621 :     }
1622 :     }
1623 : chl 96
1624 : h 545
1625 : chl 530 void
1626 :     MotionEstimationBVOP(MBParam * const pParam,
1627 :     FRAMEINFO * const frame,
1628 :     const int32_t time_bp,
1629 :     const int32_t time_pp,
1630 :     // forward (past) reference
1631 :     const MACROBLOCK * const f_mbs,
1632 :     const IMAGE * const f_ref,
1633 :     const IMAGE * const f_refH,
1634 :     const IMAGE * const f_refV,
1635 :     const IMAGE * const f_refHV,
1636 :     // backward (future) reference
1637 :     const MACROBLOCK * const b_mbs,
1638 :     const IMAGE * const b_ref,
1639 :     const IMAGE * const b_refH,
1640 :     const IMAGE * const b_refV,
1641 :     const IMAGE * const b_refHV)
1642 :     {
1643 :     uint32_t i, j;
1644 :     int32_t best_sad, skip_sad;
1645 :     int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0;
1646 :     static const VECTOR zeroMV={0,0};
1647 : chl 96
1648 : chl 530 VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/
1649 : chl 96
1650 : chl 530 const int32_t TRB = time_pp - time_bp;
1651 :     const int32_t TRD = time_pp;
1652 : chl 96
1653 : h 545 // some pre-inintialized data for the rest of the search
1654 :    
1655 :     SearchData Data;
1656 :     int32_t iMinSAD;
1657 :     VECTOR currentMV[3];
1658 :     Data.iEdgedWidth = pParam->edged_width;
1659 :     Data.currentMV = currentMV;
1660 :     Data.iMinSAD = &iMinSAD;
1661 :     Data.iQuant = frame->quant;
1662 :    
1663 : chl 530 // note: i==horizontal, j==vertical
1664 : chl 96
1665 : chl 530 for (j = 0; j < pParam->mb_height; j++) {
1666 : chl 96
1667 : chl 530 f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */
1668 : edgomez 195
1669 : chl 530 for (i = 0; i < pParam->mb_width; i++) {
1670 :     MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
1671 :     const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
1672 : edgomez 195
1673 : chl 530 /* special case, if collocated block is SKIPed: encoding is forward (0,0), cpb=0 without further ado */
1674 :     if (b_mb->mode == MODE_NOT_CODED) {
1675 :     pMB->mode = MODE_NOT_CODED;
1676 :     continue;
1677 : chl 96 }
1678 :    
1679 : h 545 Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
1680 : chl 530 /* direct search comes first, because it (1) checks for SKIP-mode
1681 :     and (2) sets very good predictions for forward and backward search */
1682 : chl 96
1683 : Isibaar 539 skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y,
1684 :     b_ref, b_refH->y, b_refV->y, b_refHV->y,
1685 : chl 530 &frame->image,
1686 :     i, j,
1687 :     frame->motion_flags,
1688 :     TRB, TRD,
1689 :     pParam,
1690 :     pMB, b_mb,
1691 : h 545 &best_sad,
1692 :     &Data);
1693 : chl 96
1694 : Isibaar 539 if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
1695 : chl 96
1696 : chl 530 // best_sad = 256*4096; //uncomment to disable Directsearch.
1697 :     // To disable any other mode, just comment the function call
1698 : chl 96
1699 : chl 530 // forward search
1700 :     SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
1701 :     &frame->image, i, j,
1702 :     frame->motion_flags,
1703 : h 545 frame->fcode, pParam,
1704 : chl 530 pMB, &f_predMV, &best_sad,
1705 : h 545 MODE_FORWARD, &Data);
1706 : chl 96
1707 : chl 530 // backward search
1708 :     SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
1709 :     &frame->image, i, j,
1710 :     frame->motion_flags,
1711 : h 545 frame->bcode, pParam,
1712 : chl 530 pMB, &b_predMV, &best_sad,
1713 : h 545 MODE_BACKWARD, &Data);
1714 : chl 96
1715 : chl 530 // interpolate search comes last, because it uses data from forward and backward as prediction
1716 : chl 96
1717 : chl 530 SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
1718 :     b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
1719 :     &frame->image,
1720 :     i, j,
1721 :     frame->fcode, frame->bcode,
1722 :     frame->motion_flags,
1723 : h 545 pParam,
1724 : chl 530 &f_predMV, &b_predMV,
1725 : h 545 pMB, &best_sad,
1726 :     &Data);
1727 : chl 96
1728 : chl 530 switch (pMB->mode) {
1729 :     case MODE_FORWARD:
1730 :     f_count++;
1731 :     f_predMV = pMB->mvs[0];
1732 :     break;
1733 :     case MODE_BACKWARD:
1734 :     b_count++;
1735 :     b_predMV = pMB->b_mvs[0];
1736 :     break;
1737 :     case MODE_INTERPOLATE:
1738 :     i_count++;
1739 :     f_predMV = pMB->mvs[0];
1740 :     b_predMV = pMB->b_mvs[0];
1741 :     break;
1742 :     case MODE_DIRECT:
1743 :     case MODE_DIRECT_NO4V:
1744 :     d_count++;
1745 :     break;
1746 :     default:
1747 :     break;
1748 : chl 96 }
1749 :     }
1750 :     }
1751 :    
1752 : chl 530 // fprintf(debug,"B-Stat: F: %04d B: %04d I: %04d D: %04d, N: %04d\n",
1753 :     // f_count,b_count,i_count,d_count,n_count);
1754 : chl 96
1755 :     }
1756 :    
1757 : chl 530 /* Hinted ME starts here */
1758 : chl 96
1759 : syskin 594 static void
1760 :     Search8hinted(const SearchData * const OldData,
1761 :     const int x, const int y,
1762 :     const uint32_t MotionFlags,
1763 :     const MBParam * const pParam,
1764 :     MACROBLOCK * const pMB,
1765 :     const MACROBLOCK * const pMBs,
1766 :     const int block,
1767 :     SearchData * const Data)
1768 : chl 96 {
1769 : syskin 594 int32_t temp_sad;
1770 : chl 530 MainSearchFunc *MainSearchPtr;
1771 : syskin 594 Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1772 :     Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1773 :     Data->iMinSAD = OldData->iMinSAD + 1 + block;
1774 :     Data->currentMV = OldData->currentMV + 1 + block;
1775 :     Data->currentQMV = OldData->currentQMV + 1 + block;
1776 : chl 141
1777 : syskin 594 if (block != 0) {
1778 :     if(pParam->m_quarterpel) {
1779 :     *(Data->iMinSAD) += lambda_vec8[Data->iQuant] *
1780 :     d_mv_bits( Data->currentQMV->x - Data->predQMV.x,
1781 :     Data->currentQMV->y - Data->predQMV.y,
1782 :     Data->iFcode);
1783 :     }
1784 :     else {
1785 :     *(Data->iMinSAD) += lambda_vec8[Data->iQuant] *
1786 :     d_mv_bits( Data->currentMV->x - Data->predMV.x,
1787 :     Data->currentMV->y - Data->predMV.y,
1788 :     Data->iFcode);
1789 :     }
1790 :     }
1791 : chl 96
1792 :    
1793 : syskin 594 Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1));
1794 :     Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));
1795 :     Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1796 :     Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1797 :     Data->RefQ = OldData->RefQ;
1798 : edgomez 195
1799 : syskin 594 Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));
1800 :    
1801 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1802 : Isibaar 580 pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);
1803 : chl 96
1804 : syskin 594 CheckCandidate = CheckCandidate8;
1805 : chl 96
1806 : syskin 594 temp_sad = *(Data->iMinSAD); // store current MinSAD
1807 :    
1808 : chl 530 if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;
1809 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1810 :     else MainSearchPtr = DiamondSearch;
1811 : chl 96
1812 : syskin 594 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1813 : edgomez 195
1814 : syskin 594 if(*(Data->iMinSAD) < temp_sad) {
1815 :     Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1816 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1817 :     }
1818 : chl 96
1819 : syskin 594 if (MotionFlags & PMV_HALFPELREFINE8) {
1820 :     temp_sad = *(Data->iMinSAD); // store current MinSAD
1821 :    
1822 :     HalfpelRefine(Data); // perform halfpel refine of current best vector
1823 :    
1824 :     if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1825 :     Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1826 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1827 :     }
1828 :     }
1829 :    
1830 :     if(pParam->m_quarterpel) {
1831 :     if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&
1832 :     (MotionFlags & PMV_QUARTERPELREFINE8)) {
1833 :     CheckCandidate = CheckCandidate8_qpel;
1834 :     QuarterpelRefine(Data);
1835 :     }
1836 :     }
1837 :    
1838 :     if(pParam->m_quarterpel) {
1839 :     pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x;
1840 :     pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y;
1841 :     }
1842 :     else {
1843 :     pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
1844 :     pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
1845 :     }
1846 :    
1847 :     pMB->mvs[block] = *(Data->currentMV);
1848 :     pMB->qmvs[block] = *(Data->currentQMV);
1849 :    
1850 :     pMB->sad8[block] = 4 * (*Data->iMinSAD);
1851 : chl 96 }
1852 :    
1853 : suxen_drol 118
1854 : chl 530 static void
1855 :     SearchPhinted ( const uint8_t * const pRef,
1856 : chl 289 const uint8_t * const pRefH,
1857 :     const uint8_t * const pRefV,
1858 :     const uint8_t * const pRefHV,
1859 :     const IMAGE * const pCur,
1860 :     const int x,
1861 :     const int y,
1862 :     const uint32_t MotionFlags,
1863 :     const uint32_t iQuant,
1864 :     const MBParam * const pParam,
1865 :     const MACROBLOCK * const pMBs,
1866 : chl 530 int inter4v,
1867 : h 545 MACROBLOCK * const pMB,
1868 : edgomez 568 SearchData * const Data)
1869 : chl 289 {
1870 : chl 530
1871 : chl 289 const int32_t iEdgedWidth = pParam->edged_width;
1872 : chl 530
1873 : edgomez 568 int i, t;
1874 : chl 530 MainSearchFunc * MainSearchPtr;
1875 : suxen_drol 118
1876 : h 545 Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1877 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1878 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
1879 : suxen_drol 118
1880 : h 545 Data->Cur = pCur->y + (x + y * iEdgedWidth) * 16;
1881 :     Data->Ref = pRef + (x + iEdgedWidth*y)*16;
1882 :     Data->RefH = pRefH + (x + iEdgedWidth*y) * 16;
1883 :     Data->RefV = pRefV + (x + iEdgedWidth*y) * 16;
1884 :     Data->RefHV = pRefHV + (x + iEdgedWidth*y) * 16;
1885 :     Data->iQuant = iQuant;
1886 : edgomez 568
1887 : chl 530 if (!(MotionFlags & PMV_HALFPEL16)) {
1888 : h 545 Data->min_dx = EVEN(Data->min_dx);
1889 :     Data->max_dx = EVEN(Data->max_dx);
1890 :     Data->min_dy = EVEN(Data->min_dy);
1891 :     Data->max_dy = EVEN(Data->max_dy);
1892 : chl 530 }
1893 : chl 289
1894 : h 545 for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
1895 : chl 289
1896 : chl 530 if (pMB->dquant != NO_CHANGE) inter4v = 0;
1897 : chl 289
1898 : chl 530 if (inter4v)
1899 :     CheckCandidate = CheckCandidate16;
1900 :     else CheckCandidate = CheckCandidate16no4v;
1901 : chl 289
1902 :    
1903 : chl 530 pMB->mvs[0].x = EVEN(pMB->mvs[0].x);
1904 :     pMB->mvs[0].y = EVEN(pMB->mvs[0].y);
1905 : h 545 if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed
1906 :     if (pMB->mvs[0].x < Data->min_dx) pMB->mvs[0].x = Data->min_dx;
1907 :     if (pMB->mvs[0].y > Data->max_dy) pMB->mvs[0].y = Data->max_dy;
1908 :     if (pMB->mvs[0].y < Data->min_dy) pMB->mvs[0].y = Data->min_dy;
1909 : chl 370
1910 : edgomez 568 (*CheckCandidate)(pMB->mvs[0].x, pMB->mvs[0].y, 0, &t, Data);
1911 : chl 289
1912 : chl 530 if (pMB->mode == MODE_INTER4V)
1913 :     for (i = 1; i < 4; i++) { // all four vectors will be used as four predictions for 16x16 search
1914 :     pMB->mvs[i].x = EVEN(pMB->mvs[i].x);
1915 :     pMB->mvs[i].y = EVEN(pMB->mvs[i].y);
1916 :     if (!(make_mask(pMB->mvs, i)))
1917 : edgomez 568 (*CheckCandidate)(pMB->mvs[i].x, pMB->mvs[i].y, 0, &t, Data);
1918 : chl 289 }
1919 :    
1920 :     if (MotionFlags & PMV_USESQUARES16)
1921 : chl 530 MainSearchPtr = SquareSearch;
1922 : chl 289 else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1923 : chl 530 MainSearchPtr = AdvDiamondSearch;
1924 :     else MainSearchPtr = DiamondSearch;
1925 : chl 289
1926 : h 545 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1927 : chl 289
1928 : h 545 if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);
1929 : chl 289
1930 : syskin 594 for(i = 0; i < 5; i++) {
1931 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1932 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1933 :     }
1934 : chl 289
1935 : syskin 594 if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1936 :    
1937 :     if(inter4v)
1938 :     CheckCandidate = CheckCandidate16_qpel;
1939 :     else
1940 :     CheckCandidate = CheckCandidate16no4v_qpel;
1941 :    
1942 :     QuarterpelRefine(Data);
1943 :     }
1944 :    
1945 :    
1946 :     if (inter4v) {
1947 :     SearchData Data8;
1948 :     Data8.iFcode = Data->iFcode;
1949 :     Data8.iQuant = Data->iQuant;
1950 :     Data8.iEdgedWidth = Data->iEdgedWidth;
1951 :     Search8hinted(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1952 :     Search8hinted(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1953 :     Search8hinted(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1954 :     Search8hinted(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1955 :     }
1956 :    
1957 : chl 530 if (!(inter4v) ||
1958 : h 545 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] +
1959 :     Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
1960 : chl 530 // INTER MODE
1961 : chl 289
1962 : chl 530 pMB->mode = MODE_INTER;
1963 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1]
1964 : h 545 = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1965 : chl 289
1966 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1967 : h 545 pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1968 : chl 289
1969 : h 545 pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1970 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1971 : chl 530 } else {
1972 :     // INTER4V MODE; all other things are already set in Search8hinted
1973 :     pMB->mode = MODE_INTER4V;
1974 : h 545 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3]
1975 :     + Data->iMinSAD[4] + IMV16X16 * iQuant;
1976 : chl 289 }
1977 :    
1978 :     }
1979 :    
1980 : edgomez 195 void
1981 : chl 530 MotionEstimationHinted( MBParam * const pParam,
1982 :     FRAMEINFO * const current,
1983 :     FRAMEINFO * const reference,
1984 :     const IMAGE * const pRefH,
1985 :     const IMAGE * const pRefV,
1986 :     const IMAGE * const pRefHV)
1987 : suxen_drol 118 {
1988 : chl 530 MACROBLOCK *const pMBs = current->mbs;
1989 :     const IMAGE *const pCurrent = &current->image;
1990 :     const IMAGE *const pRef = &reference->image;
1991 : suxen_drol 118
1992 : chl 530 uint32_t x, y;
1993 : syskin 594 int8_t * qimage;
1994 : syskin 574 int32_t temp[5], quant = current->quant;
1995 : h 545 int32_t iMinSAD[5];
1996 :     VECTOR currentMV[5];
1997 :     SearchData Data;
1998 :     Data.iEdgedWidth = pParam->edged_width;
1999 :     Data.currentMV = currentMV;
2000 :     Data.iMinSAD = iMinSAD;
2001 :     Data.temp = temp;
2002 :     Data.iFcode = current->fcode;
2003 : syskin 594 Data.rounding = pParam->m_rounding_type;
2004 : h 545
2005 : syskin 594 if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
2006 :     return; // allocate some mem for qpel interpolated blocks
2007 :     // somehow this is dirty since I think we shouldn't use malloc outside
2008 :     // encoder_create() - so please fix me!
2009 :    
2010 :     Data.RefQ = qimage;
2011 :    
2012 : chl 530 if (sadInit) (*sadInit) ();
2013 : suxen_drol 118
2014 : chl 530 for (y = 0; y < pParam->mb_height; y++) {
2015 :     for (x = 0; x < pParam->mb_width; x++) {
2016 : chl 312
2017 : chl 530 MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
2018 : suxen_drol 118
2019 : chl 530 //intra mode is copied from the first pass. At least for the time being
2020 :     if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue;
2021 : chl 346
2022 : syskin 574
2023 : chl 530 if (!(current->global_flags & XVID_LUMIMASKING)) {
2024 :     pMB->dquant = NO_CHANGE;
2025 :     pMB->quant = current->quant; }
2026 : syskin 574 else
2027 :     if (pMB->dquant != NO_CHANGE) {
2028 :     quant += DQtab[pMB->dquant];
2029 :     if (quant > 31) quant = 31;
2030 :     else if (quant < 1) quant = 1;
2031 :     pMB->quant = quant;
2032 :     }
2033 : chl 341
2034 : Isibaar 539 SearchPhinted(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
2035 :     y, current->motion_flags, pMB->quant,
2036 : h 545 pParam, pMBs, current->global_flags & XVID_INTER4V, pMB,
2037 :     &Data);
2038 : chl 317
2039 : Isibaar 539 }
2040 :     }
2041 : syskin 594 free(qimage);
2042 : Isibaar 539 }
2043 : suxen_drol 118
2044 : Isibaar 539 static __inline int
2045 :     MEanalyzeMB ( const uint8_t * const pRef,
2046 :     const uint8_t * const pCur,
2047 :     const int x,
2048 :     const int y,
2049 :     const MBParam * const pParam,
2050 :     const MACROBLOCK * const pMBs,
2051 : h 545 MACROBLOCK * const pMB,
2052 :     SearchData * const Data)
2053 : Isibaar 539 {
2054 : chl 317
2055 : Isibaar 539 int i, mask;
2056 : h 545 VECTOR pmv[3];
2057 : suxen_drol 118
2058 : h 545 *(Data->iMinSAD) = MV_MAX_ERROR;
2059 :     Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
2060 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2061 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
2062 : chl 326
2063 : h 545 Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2064 :     Data->Ref = pRef + (x + y * pParam->edged_width) * 16;
2065 : Isibaar 539
2066 :     CheckCandidate = CheckCandidate16no4vI;
2067 : chl 326
2068 : Isibaar 539 pmv[1].x = EVEN(pMB->mvs[0].x);
2069 :     pmv[1].y = EVEN(pMB->mvs[0].y);
2070 : h 545 pmv[0].x = EVEN(Data->predMV.x);
2071 :     pmv[0].y = EVEN(Data->predMV.y);
2072 : Isibaar 539 pmv[2].x = pmv[2].y = 0;
2073 : chl 326
2074 : h 545 CheckCandidate16no4vI(pmv[0].x, pmv[0].y, 255, &i, Data);
2075 : Isibaar 539 if (!(mask = make_mask(pmv, 1)))
2076 : h 545 CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data);
2077 : Isibaar 539 if (!(mask = make_mask(pmv, 2)))
2078 : h 545 CheckCandidate16no4vI(0, 0, mask, &i, Data);
2079 : Isibaar 539
2080 : h 545 DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
2081 : Isibaar 539
2082 :     pMB->mvs[0] = pMB->mvs[1]
2083 : h 545 = pMB->mvs[2] = pMB->mvs[3] = *Data->currentMV; // all, for future get_pmv()
2084 : Isibaar 539
2085 : h 545 return *(Data->iMinSAD);
2086 : Isibaar 539 }
2087 :    
2088 :     #define INTRA_THRESH 1350
2089 :     #define INTER_THRESH 900
2090 :    
2091 :     int
2092 :     MEanalysis( const IMAGE * const pRef,
2093 :     const IMAGE * const pCurrent,
2094 :     MBParam * const pParam,
2095 :     MACROBLOCK * const pMBs,
2096 :     const uint32_t iFcode)
2097 :     {
2098 :     uint32_t x, y, intra = 0;
2099 :     int sSAD = 0;
2100 : h 545
2101 :     VECTOR currentMV;
2102 :     int32_t iMinSAD;
2103 :     SearchData Data;
2104 :     Data.iEdgedWidth = pParam->edged_width;
2105 :     Data.currentMV = &currentMV;
2106 :     Data.iMinSAD = &iMinSAD;
2107 :     Data.iFcode = iFcode;
2108 :     Data.iQuant = 2;
2109 :    
2110 : Isibaar 539 if (sadInit) (*sadInit) ();
2111 :    
2112 :     for (y = 0; y < pParam->mb_height-1; y++) {
2113 :     for (x = 0; x < pParam->mb_width; x++) {
2114 :     int sad, dev;
2115 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
2116 :    
2117 :     sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y,
2118 : h 545 pParam, pMBs, pMB, &Data);
2119 : Isibaar 539
2120 :     if ( x != 0 && y != 0 && x != pParam->mb_width-1 ) { //no edge macroblocks, they just don't work
2121 :     if (sad > INTRA_THRESH) {
2122 :     dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
2123 :     pParam->edged_width);
2124 :     if (dev + INTRA_THRESH < sad) intra++;
2125 :     if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame
2126 :     }
2127 :     sSAD += sad;
2128 :     }
2129 :    
2130 : suxen_drol 118 }
2131 :     }
2132 : Isibaar 539 sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);
2133 :     if (sSAD > INTER_THRESH ) return 1; //P frame
2134 :     emms();
2135 :     return 0; // B frame
2136 :    
2137 : suxen_drol 118 }
2138 : syskin 576
2139 :     int
2140 :     FindFcode( const MBParam * const pParam,
2141 :     const FRAMEINFO * const current)
2142 :     {
2143 :     uint32_t x, y;
2144 :     int max = 0, min = 0, i;
2145 :    
2146 :     for (y = 0; y < pParam->mb_height; y++) {
2147 :     for (x = 0; x < pParam->mb_width; x++) {
2148 :    
2149 :     MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
2150 :     for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4:1); i++) {
2151 :     if (pMB->mvs[i].x > max) max = pMB->mvs[i].x;
2152 :     if (pMB->mvs[i].y > max) max = pMB->mvs[i].y;
2153 :    
2154 :     if (pMB->mvs[i].x < min) min = pMB->mvs[i].x;
2155 :     if (pMB->mvs[i].y < min) min = pMB->mvs[i].y;
2156 :     }
2157 :     }
2158 :     }
2159 :    
2160 :     min = -min;
2161 :     max += 1;
2162 :     if (min > max) max = min;
2163 :    
2164 :     for (i = 1; (max > 32 << (i - 1)); i++);
2165 :     return i;
2166 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4