[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 661 - (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 : chl 596 switch ( (((X)&1)<<1) + ((Y)&1) ) \
57 : Isibaar 580 { \
58 : syskin 601 case 0 : REF = (uint8_t *)data->Ref + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \
59 :     case 1 : REF = (uint8_t *)data->RefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \
60 :     case 2 : REF = (uint8_t *)data->RefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \
61 :     default : REF = (uint8_t *)data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \
62 : Isibaar 580 } \
63 :     }
64 : syskin 658 // I hate those macros :/
65 :     #define GET_REFERENCE2(X, Y, REF) { \
66 :     switch ( (((X)&1)<<1) + ((Y)&1) ) \
67 :     { \
68 :     case 0 : REF = (uint8_t *)data->bRef + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \
69 :     case 1 : REF = (uint8_t *)data->bRefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \
70 :     case 2 : REF = (uint8_t *)data->bRefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \
71 :     default : REF = (uint8_t *)data->bRefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \
72 :     } \
73 :     }
74 : Isibaar 580
75 : syskin 658
76 : chl 530 #define iDiamondSize 2
77 : chl 141
78 : chl 530 static __inline int
79 :     d_mv_bits(int x, int y, const uint32_t iFcode)
80 : Isibaar 3 {
81 : chl 530 int xb, yb;
82 :    
83 :     if (x == 0) xb = 1;
84 :     else {
85 :     if (x < 0) x = -x;
86 :     x += (1 << (iFcode - 1)) - 1;
87 :     x >>= (iFcode - 1);
88 :     if (x > 32) x = 32;
89 :     xb = mvtab[x] + iFcode;
90 : edgomez 195 }
91 : Isibaar 3
92 : chl 530 if (y == 0) yb = 1;
93 :     else {
94 :     if (y < 0) y = -y;
95 :     y += (1 << (iFcode - 1)) - 1;
96 :     y >>= (iFcode - 1);
97 :     if (y > 32) y = 32;
98 :     yb = mvtab[y] + iFcode;
99 :     }
100 :     return xb + yb;
101 : Isibaar 3 }
102 :    
103 : syskin 628 static int32_t
104 :     ChromaSAD(int dx, int dy, const SearchData * const data)
105 : Isibaar 3 {
106 : syskin 628 int sad;
107 :     dx = (dx >> 1) + roundtab_79[dx & 0x3];
108 :     dy = (dy >> 1) + roundtab_79[dy & 0x3];
109 : Isibaar 3
110 : syskin 628 switch (((dx & 1) << 1) + (dy & 1)) { // ((dx%2)?2:0)+((dy%2)?1:0)
111 :     case 0:
112 :     sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);
113 :     sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);
114 :     break;
115 :     case 1:
116 :     dx = dx / 2; dy = (dy - 1) / 2;
117 :     sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);
118 :     sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);
119 :     break;
120 :     case 2:
121 :     dx = (dx - 1) / 2; dy = dy / 2;
122 :     sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);
123 :     sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);
124 :     break;
125 :     default:
126 :     dx = (dx - 1) / 2; dy = (dy - 1) / 2;
127 :     interpolate8x8_halfpel_hv(data->RefQ,
128 :     data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,
129 :     data->rounding);
130 :     sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2);
131 :     interpolate8x8_halfpel_hv(data->RefQ,
132 :     data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,
133 :     data->rounding);
134 :     sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2);
135 :     break;
136 : chl 530 }
137 : syskin 628 return sad;
138 :     }
139 : Isibaar 3
140 : Isibaar 600
141 : syskin 628 /* CHECK_CANDIATE FUNCTIONS START */
142 : chl 184
143 :    
144 : chl 530 static void
145 : syskin 628 CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
146 : Isibaar 606 {
147 :     int t;
148 :     const uint8_t * Reference;
149 :    
150 :     if (( x > data->max_dx) || ( x < data->min_dx)
151 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
152 :    
153 :     switch ( ((x&1)<<1) + (y&1) ) {
154 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
155 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
156 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
157 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
158 :     }
159 :    
160 :     data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);
161 :    
162 : syskin 628 if (data->qpel) t = d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode);
163 :     else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
164 : Isibaar 606
165 : syskin 628 data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;
166 :     data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;
167 :    
168 :     if (data->chroma) data->temp[0] += ChromaSAD(x, y, data);
169 :    
170 : Isibaar 606 if (data->temp[0] < data->iMinSAD[0]) {
171 :     data->iMinSAD[0] = data->temp[0];
172 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
173 :     *dir = Direction; }
174 :    
175 :     if (data->temp[1] < data->iMinSAD[1]) {
176 :     data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
177 :     if (data->temp[2] < data->iMinSAD[2]) {
178 :     data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
179 :     if (data->temp[3] < data->iMinSAD[3]) {
180 :     data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
181 :     if (data->temp[4] < data->iMinSAD[4]) {
182 :     data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
183 :    
184 :     }
185 :    
186 :     static void
187 : chl 530 CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
188 :     {
189 :     int32_t sad;
190 :     const uint8_t * Reference;
191 : chl 326
192 : chl 530 if (( x > data->max_dx) || ( x < data->min_dx)
193 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
194 : Isibaar 3
195 : chl 530 switch ( ((x&1)<<1) + (y&1) )
196 :     {
197 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
198 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
199 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
200 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
201 :     }
202 : Isibaar 3
203 : syskin 628 sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR);
204 : syskin 658 if (data->qpel) //only to be used in b-frames' ME
205 :     sad += (data->lambda16 * d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode) * sad)/1000;
206 :     else
207 :     sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000;
208 : Isibaar 3
209 : chl 530 if (sad < *(data->iMinSAD)) {
210 :     *(data->iMinSAD) = sad;
211 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
212 :     *dir = Direction; }
213 : Isibaar 3 }
214 :    
215 : chl 530 static void
216 : Isibaar 580 CheckCandidate16_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
217 :    
218 :     // CheckCandidate16 variant which expects x and y in quarter pixel resolution
219 :     // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)
220 :     // around currentMV!
221 :     {
222 :     int t;
223 : syskin 601 uint8_t * Reference = (uint8_t *)data->RefQ;
224 : Isibaar 580 const uint8_t *ref1, *ref2, *ref3, *ref4;
225 :     VECTOR halfpelMV = *(data->currentMV);
226 :    
227 :     int32_t iEdgedWidth = data->iEdgedWidth;
228 :     uint32_t rounding = data->rounding;
229 :    
230 :     if (( x > data->max_dx) || ( x < data->min_dx)
231 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
232 :    
233 : Isibaar 606 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases
234 : Isibaar 580 switch( ((x&1)<<1) + (y&1) )
235 :     {
236 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
237 : syskin 601 GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);
238 : Isibaar 580 break;
239 :    
240 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
241 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
242 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
243 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);
244 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);
245 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);
246 :     break;
247 :    
248 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
249 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);
250 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
251 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);
252 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);
253 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);
254 :     break;
255 :    
256 :     default: // x and y in qpel resolution - the "corners" (top left/right and
257 :     // bottom left/right) during qpel refinement
258 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
259 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);
260 :     GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);
261 :    
262 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
263 :     interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
264 :     interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
265 :     interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
266 :     break;
267 :     }
268 :    
269 : syskin 594 data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1);
270 : Isibaar 580
271 :     t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);
272 : syskin 628 data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;
273 :     data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;
274 : Isibaar 580
275 : syskin 628 if (data->chroma)
276 :     data->temp[0] += ChromaSAD(x/2, y/2, data);
277 :    
278 : Isibaar 580 if (data->temp[0] < data->iMinSAD[0]) {
279 :     data->iMinSAD[0] = data->temp[0];
280 :     data->currentQMV[0].x = x; data->currentQMV[0].y = y;
281 : syskin 594 /* *dir = Direction;*/ }
282 : Isibaar 580
283 :     if (data->temp[1] < data->iMinSAD[1]) {
284 :     data->iMinSAD[1] = data->temp[1]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; }
285 :     if (data->temp[2] < data->iMinSAD[2]) {
286 :     data->iMinSAD[2] = data->temp[2]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; }
287 :     if (data->temp[3] < data->iMinSAD[3]) {
288 :     data->iMinSAD[3] = data->temp[3]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; }
289 :     if (data->temp[4] < data->iMinSAD[4]) {
290 :     data->iMinSAD[4] = data->temp[4]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; }
291 :     }
292 :    
293 :     static void
294 : syskin 658 CheckCandidate16no4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
295 :    
296 :     // CheckCandidate16no4v variant which expects x and y in quarter pixel resolution
297 :     // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)
298 :     // around currentMV!
299 :     // this function is for B-frames' search only
300 :     {
301 :     uint8_t * Reference = (uint8_t *)data->RefQ;
302 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
303 :     VECTOR halfpelMV = *(data->currentMV);
304 :    
305 :     int32_t iEdgedWidth = data->iEdgedWidth;
306 :     int32_t sad;
307 :    
308 :     if (( x > data->max_dx) || ( x < data->min_dx)
309 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
310 :    
311 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases
312 :     switch( ((x&1)<<1) + (y&1) )
313 :     {
314 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
315 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);
316 :     break;
317 :    
318 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
319 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
320 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0);
321 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0);
322 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
323 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
324 :     break;
325 :    
326 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
327 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);
328 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0);
329 :     interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0);
330 :     interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
331 :     interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
332 :     break;
333 :    
334 :     default: // x and y in qpel resolution - the "corners" (top left/right and
335 :     // bottom left/right) during qpel refinement
336 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
337 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);
338 :     GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);
339 :    
340 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, 0);
341 :     interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);
342 :     interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);
343 :     interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);
344 :     break;
345 :     }
346 :    
347 :     sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
348 :     sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000;
349 :    
350 :     if (sad < data->iMinSAD[0]) {
351 :     data->iMinSAD[0] = sad;
352 :     data->currentQMV[0].x = x; data->currentQMV[0].y = y;
353 :     /* *dir = Direction;*/ }
354 :     }
355 :    
356 :     static void
357 : Isibaar 539 CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
358 :     {
359 : syskin 658 // maximum speed - for P/B/I decision
360 : Isibaar 539 int32_t sad;
361 :    
362 :     if (( x > data->max_dx) || ( x < data->min_dx)
363 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
364 :    
365 : syskin 601 sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),
366 : Isibaar 539 data->iEdgedWidth, 256*4096);
367 :    
368 :     if (sad < *(data->iMinSAD)) {
369 :     *(data->iMinSAD) = sad;
370 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
371 :     *dir = Direction; }
372 :     }
373 :    
374 :    
375 :     static void
376 : chl 530 CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)
377 :     {
378 :     int32_t sad;
379 :     const int xb = data->currentMV[1].x;
380 :     const int yb = data->currentMV[1].y;
381 :     const uint8_t *ReferenceF, *ReferenceB;
382 : Isibaar 3
383 : chl 530 if (( xf > data->max_dx) || ( xf < data->min_dx)
384 :     || ( yf > data->max_dy) || (yf < data->min_dy)) return;
385 : Isibaar 3
386 : chl 530 switch ( ((xf&1)<<1) + (yf&1) ) {
387 :     case 0 : ReferenceF = data->Ref + xf/2 + (yf/2)*(data->iEdgedWidth); break;
388 :     case 1 : ReferenceF = data->RefV + xf/2 + ((yf-1)/2)*(data->iEdgedWidth); break;
389 :     case 2 : ReferenceF = data->RefH + (xf-1)/2 + (yf/2)*(data->iEdgedWidth); break;
390 :     default : ReferenceF = data->RefHV + (xf-1)/2 + ((yf-1)/2)*(data->iEdgedWidth); break;
391 :     }
392 : edgomez 195
393 : chl 530 switch ( ((xb&1)<<1) + (yb&1) ) {
394 :     case 0 : ReferenceB = data->bRef + xb/2 + (yb/2)*(data->iEdgedWidth); break;
395 :     case 1 : ReferenceB = data->bRefV + xb/2 + ((yb-1)/2)*(data->iEdgedWidth); break;
396 :     case 2 : ReferenceB = data->bRefH + (xb-1)/2 + (yb/2)*(data->iEdgedWidth); break;
397 :     default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break;
398 :     }
399 : Isibaar 3
400 : syskin 628 sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
401 :    
402 : syskin 658 if (data->qpel)
403 :     sad += (data->lambda16 *
404 :     ( d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode) +
405 :     d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode)) * sad)/1000;
406 :     else
407 :     sad += (data->lambda16 *
408 : chl 530 ( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) +
409 : syskin 628 d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000;
410 : Isibaar 3
411 : chl 530 if (sad < *(data->iMinSAD)) {
412 :     *(data->iMinSAD) = sad;
413 :     data->currentMV->x = xf; data->currentMV->y = yf;
414 :     *dir = Direction; }
415 : Isibaar 3 }
416 :    
417 : syskin 658
418 : chl 530 static void
419 : syskin 658 CheckCandidateInt_qpel(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)
420 :     {
421 :     // CheckCandidateInt variant which expects x and y in quarter pixel resolution
422 :    
423 :     int32_t sad;
424 :     const int xb = data->currentQMV[1].x;
425 :     const int yb = data->currentQMV[1].y;
426 :     uint8_t * ReferenceF = (uint8_t *)data->RefQ;
427 :     uint8_t * ReferenceB = (uint8_t *)data->RefQ + 16;
428 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
429 :     VECTOR halfpelMV;
430 :     const int32_t iEdgedWidth = data->iEdgedWidth;
431 :    
432 :     if (( xf > data->max_dx) || ( xf < data->min_dx)
433 :     || ( yf > data->max_dy) || (yf < data->min_dy)) return;
434 :    
435 :     halfpelMV.x = xf/2; //forward first
436 :     halfpelMV.y = yf/2;
437 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases
438 :     switch( ((xf&1)<<1) + (yf&1) )
439 :     {
440 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
441 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF);
442 :     break;
443 :    
444 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
445 :     GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2);
446 :     interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);
447 :     interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);
448 :     interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
449 :     interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
450 :     break;
451 :    
452 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
453 :     GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref2);
454 :     interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);
455 :     interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);
456 :     interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
457 :     interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
458 :     break;
459 :    
460 :     default: // x and y in qpel resolution - the "corners" (top left/right and
461 :     // bottom left/right) during qpel refinement
462 :     GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2);
463 :     GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref3);
464 :     GET_REFERENCE(xf - halfpelMV.x, yf - halfpelMV.y, ref4);
465 :    
466 :     interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0);
467 :     interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);
468 :     interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);
469 :     interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);
470 :     break;
471 :     }
472 :    
473 :     halfpelMV.x = xb/2; //backward
474 :     halfpelMV.y = yb/2;
475 :     GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases
476 :     switch( ((xb&1)<<1) + (yb&1) )
477 :     {
478 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
479 :     GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB);
480 :     break;
481 :    
482 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
483 :     GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2);
484 :     interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);
485 :     interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);
486 :     interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
487 :     interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
488 :     break;
489 :    
490 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
491 :     GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref2);
492 :     interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);
493 :     interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);
494 :     interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
495 :     interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
496 :     break;
497 :    
498 :     default: // x and y in qpel resolution - the "corners" (top left/right and
499 :     // bottom left/right) during qpel refinement
500 :     GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2);
501 :     GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref3);
502 :     GET_REFERENCE2(xb - halfpelMV.x, yb - halfpelMV.y, ref4);
503 :    
504 :     interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0);
505 :     interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);
506 :     interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);
507 :     interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);
508 :     break;
509 :     }
510 :    
511 :     sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
512 :    
513 :     sad += (data->lambda16 *
514 :     ( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) +
515 :     d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000;
516 :    
517 :     if (sad < *(data->iMinSAD)) {
518 :     *(data->iMinSAD) = sad;
519 :     data->currentQMV->x = xf; data->currentQMV->y = yf;
520 :     *dir = Direction; }
521 :     }
522 :    
523 :     static void
524 : chl 530 CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
525 : Isibaar 3 {
526 : syskin 628 int32_t sad = 0;
527 : chl 530 int k;
528 :     const uint8_t *ReferenceF;
529 :     const uint8_t *ReferenceB;
530 :     VECTOR mvs, b_mvs;
531 : Isibaar 3
532 : chl 530 if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
533 : Isibaar 3
534 : chl 530 for (k = 0; k < 4; k++) {
535 :     mvs.x = data->directmvF[k].x + x;
536 :     b_mvs.x = ((x == 0) ?
537 :     data->directmvB[k].x
538 :     : mvs.x - data->referencemv[k].x);
539 : Isibaar 3
540 : chl 530 mvs.y = data->directmvF[k].y + y;
541 :     b_mvs.y = ((y == 0) ?
542 :     data->directmvB[k].y
543 :     : mvs.y - data->referencemv[k].y);
544 :    
545 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
546 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
547 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
548 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
549 : Isibaar 3
550 : chl 530 switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {
551 :     case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;
552 :     case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
553 :     case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;
554 :     default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
555 :     }
556 : Isibaar 3
557 : chl 530 switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {
558 :     case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
559 :     case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
560 :     case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
561 :     default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
562 : chl 344 }
563 : chl 530
564 :     sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
565 :     ReferenceF + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
566 :     ReferenceB + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
567 :     data->iEdgedWidth);
568 :     if (sad > *(data->iMinSAD)) return;
569 : edgomez 78 }
570 : chl 530
571 : syskin 628 sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
572 :    
573 : chl 530 if (sad < *(data->iMinSAD)) {
574 :     *(data->iMinSAD) = sad;
575 :     data->currentMV->x = x; data->currentMV->y = y;
576 :     *dir = Direction; }
577 : Isibaar 3 }
578 :    
579 : syskin 658
580 : chl 530 static void
581 : syskin 658 CheckCandidateDirect_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
582 :     {
583 :     int32_t sad = 0;
584 :     int k;
585 :     VECTOR mvs, b_mvs, halfpelMV;
586 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
587 :     uint8_t *ReferenceF, *ReferenceB;
588 :     const uint32_t iEdgedWidth = data->iEdgedWidth;
589 :    
590 :     if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
591 :    
592 :     for (k = 0; k < 4; k++) {
593 :     ReferenceF = (uint8_t *)data->RefQ;
594 :     ReferenceB = (uint8_t *)data->RefQ + 64;
595 :    
596 :     mvs.x = data->directmvF[k].x + x;
597 :     b_mvs.x = ((x == 0) ?
598 :     data->directmvB[k].x
599 :     : mvs.x - data->referencemv[k].x);
600 :    
601 :     mvs.y = data->directmvF[k].y + y;
602 :     b_mvs.y = ((y == 0) ?
603 :     data->directmvB[k].y
604 :     : mvs.y - data->referencemv[k].y);
605 :    
606 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
607 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
608 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
609 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
610 :    
611 :     halfpelMV.x = mvs.x/2; //forward first
612 :     halfpelMV.y = mvs.y/2;
613 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases
614 :     switch( ((mvs.x&1)<<1) + (mvs.y&1) ) {
615 :     case 0: // pure halfpel position
616 :     GET_REFERENCE(halfpelMV.x + 16*(k&1), halfpelMV.y + 16*(k>>1), ReferenceF);
617 :     break;
618 :    
619 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
620 :     GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);
621 :     interpolate8x8_avg2(ReferenceF, ref1+8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
622 :     ref2+ 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);
623 :     break;
624 :    
625 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
626 :     GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2);
627 :     interpolate8x8_avg2(ReferenceF, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
628 :     ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);
629 :     break;
630 :    
631 :     default: // x and y in qpel resolution - the "corners" (top left/right and
632 :     // bottom left/right) during qpel refinement
633 :     GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);
634 :     GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3);
635 :     GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4);
636 :     interpolate8x8_avg4(ReferenceF, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
637 :     ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
638 :     ref3 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
639 :     ref4 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);
640 :     break;
641 :     }
642 :    
643 :     halfpelMV.x = b_mvs.x/2;
644 :     halfpelMV.y = b_mvs.y/2;
645 :     GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in most cases
646 :     switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {
647 :     case 0: // pure halfpel position
648 :     GET_REFERENCE2(halfpelMV.x + 16*(k&1), halfpelMV.y + 16*(k>>1), ReferenceB);
649 :     break;
650 :    
651 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
652 :     GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);
653 :     interpolate8x8_avg2(ReferenceB, ref1+8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
654 :     ref2+ 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);
655 :     break;
656 :    
657 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
658 :     GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2);
659 :     interpolate8x8_avg2(ReferenceB, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
660 :     ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);
661 :     break;
662 :    
663 :     default: // x and y in qpel resolution - the "corners" (top left/right and
664 :     // bottom left/right) during qpel refinement
665 :     GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);
666 :     GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3);
667 :     GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4);
668 :     interpolate8x8_avg4(ReferenceB, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
669 :     ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
670 :     ref3 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
671 :     ref4 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);
672 :     break;
673 :     }
674 :    
675 :     sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
676 :     ReferenceF,
677 :     ReferenceB,
678 :     data->iEdgedWidth);
679 :     if (sad > *(data->iMinSAD)) return;
680 :     }
681 :    
682 :     sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
683 :    
684 :     if (sad < *(data->iMinSAD)) {
685 :     *(data->iMinSAD) = sad;
686 :     data->currentMV->x = x; data->currentMV->y = y;
687 :     *dir = Direction; }
688 :     }
689 :    
690 :     static void
691 :     CheckCandidateDirectno4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
692 :     {
693 :     int32_t sad = 0;
694 :     VECTOR mvs, b_mvs, halfpelMV;
695 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
696 :     const uint32_t iEdgedWidth = data->iEdgedWidth;
697 :     uint8_t * ReferenceF = (uint8_t *)data->RefQ;
698 :     uint8_t * ReferenceB = (uint8_t *)data->RefQ + 64;
699 :    
700 :     if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
701 :    
702 :     mvs.x = data->directmvF[0].x + x;
703 :     b_mvs.x = ((x == 0) ?
704 :     data->directmvB[0].x
705 :     : mvs.x - data->referencemv[0].x);
706 :    
707 :     mvs.y = data->directmvF[0].y + y;
708 :     b_mvs.y = ((y == 0) ?
709 :     data->directmvB[0].y
710 :     : mvs.y - data->referencemv[0].y);
711 :    
712 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
713 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
714 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
715 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
716 :    
717 :     halfpelMV.x = mvs.x/2; //forward first
718 :     halfpelMV.y = mvs.y/2;
719 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases
720 :     switch( ((mvs.x&1)<<1) + (mvs.y&1) ) {
721 :     case 0: // pure halfpel position
722 :     GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF);
723 :     break;
724 :    
725 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
726 :     GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);
727 :     interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);
728 :     interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);
729 :     interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
730 :     interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
731 :     break;
732 :    
733 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
734 :     GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2);
735 :     interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);
736 :     interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);
737 :     interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
738 :     interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
739 :     break;
740 :    
741 :     default: // x and y in qpel resolution
742 :     GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);
743 :     GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3);
744 :     GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4);
745 :    
746 :     interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0);
747 :     interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);
748 :     interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);
749 :     interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);
750 :     break;
751 :     }
752 :    
753 :     halfpelMV.x = b_mvs.x/2; //backward
754 :     halfpelMV.y = b_mvs.y/2;
755 :     GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1);
756 :     switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) )
757 :     {
758 :     case 0: // pure halfpel position
759 :     GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB);
760 :     break;
761 :    
762 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
763 :     GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);
764 :     interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);
765 :     interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);
766 :     interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
767 :     interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
768 :     break;
769 :    
770 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
771 :     GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2);
772 :     interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);
773 :     interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);
774 :     interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);
775 :     interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);
776 :     break;
777 :    
778 :     default: // x and y in qpel resolution - the "corners" (top left/right and
779 :     // bottom left/right) during qpel refinement
780 :     GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);
781 :     GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3);
782 :     GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4);
783 :    
784 :     interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0);
785 :     interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);
786 :     interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);
787 :     interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);
788 :     break;
789 :     }
790 :    
791 :     sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
792 :     sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
793 :    
794 :     if (sad < *(data->iMinSAD)) {
795 :     *(data->iMinSAD) = sad;
796 :     data->currentMV->x = x; data->currentMV->y = y;
797 :     *dir = Direction; }
798 :     }
799 :    
800 :    
801 :     static void
802 : chl 530 CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
803 : chl 96 {
804 : chl 530 int32_t sad;
805 :     const uint8_t *ReferenceF;
806 :     const uint8_t *ReferenceB;
807 :     VECTOR mvs, b_mvs;
808 : chl 96
809 : syskin 576 if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
810 : edgomez 195
811 : chl 530 mvs.x = data->directmvF[0].x + x;
812 :     b_mvs.x = ((x == 0) ?
813 :     data->directmvB[0].x
814 :     : mvs.x - data->referencemv[0].x);
815 : edgomez 195
816 : chl 530 mvs.y = data->directmvF[0].y + y;
817 :     b_mvs.y = ((y == 0) ?
818 :     data->directmvB[0].y
819 :     : mvs.y - data->referencemv[0].y);
820 :    
821 :     if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )
822 :     || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )
823 :     || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
824 :     || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
825 : chl 96
826 : chl 530 switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {
827 :     case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;
828 :     case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
829 :     case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;
830 :     default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;
831 :     }
832 : chl 96
833 : chl 530 switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {
834 :     case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
835 :     case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
836 :     case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;
837 :     default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;
838 :     }
839 :    
840 : syskin 628 sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
841 :     sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
842 : chl 96
843 : chl 530 if (sad < *(data->iMinSAD)) {
844 :     *(data->iMinSAD) = sad;
845 :     data->currentMV->x = x; data->currentMV->y = y;
846 :     *dir = Direction; }
847 :     }
848 : chl 96
849 : chl 530 static void
850 :     CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
851 :     {
852 : syskin 628 int32_t sad; int t;
853 : chl 530 const uint8_t * Reference;
854 : edgomez 195
855 : chl 530 if (( x > data->max_dx) || ( x < data->min_dx)
856 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
857 : chl 96
858 : chl 530 switch ( ((x&1)<<1) + (y&1) )
859 :     {
860 :     case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
861 :     case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
862 :     case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
863 :     default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
864 :     }
865 : edgomez 195
866 : chl 530 sad = sad8(data->Cur, Reference, data->iEdgedWidth);
867 : syskin 628 if (data->qpel) t = d_mv_bits(2 * x - data->predQMV.x, 2 * y - data->predQMV.y, data->iFcode);
868 :     else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
869 : edgomez 195
870 : syskin 628 sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100;
871 : chl 96
872 : Isibaar 606 if (sad < *(data->iMinSAD)) {
873 :     *(data->iMinSAD) = sad;
874 :     data->currentMV->x = x; data->currentMV->y = y;
875 :     *dir = Direction; }
876 :     }
877 :    
878 :     static void
879 : Isibaar 580 CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
880 : syskin 658 // CheckCandidate8 variant which expects x and y in quarter pixel resolution
881 : Isibaar 580 // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)
882 :     // around currentMV!
883 :    
884 :     {
885 :     int32_t sad;
886 :     uint8_t *Reference = (uint8_t *) data->RefQ;
887 :     const uint8_t *ref1, *ref2, *ref3, *ref4;
888 :     VECTOR halfpelMV = *(data->currentMV);
889 :    
890 :     int32_t iEdgedWidth = data->iEdgedWidth;
891 :     uint32_t rounding = data->rounding;
892 :    
893 :     if (( x > data->max_dx) || ( x < data->min_dx)
894 :     || ( y > data->max_dy) || (y < data->min_dy)) return;
895 :    
896 : Isibaar 606 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);
897 : Isibaar 580 switch( ((x&1)<<1) + (y&1) )
898 :     {
899 :     case 0: // pure halfpel position - shouldn't happen during a refinement step
900 : syskin 601 GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);
901 : Isibaar 580 break;
902 :    
903 :     case 1: // x halfpel, y qpel - top or bottom during qpel refinement
904 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
905 :    
906 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
907 :     break;
908 :    
909 :     case 2: // x qpel, y halfpel - left or right during qpel refinement
910 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);
911 :    
912 :     interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);
913 :     break;
914 :    
915 :     default: // x and y in qpel resolution - the "corners" (top left/right and
916 :     // bottom left/right) during qpel refinement
917 :     GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);
918 :     GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);
919 :     GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);
920 :    
921 :     interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
922 :     break;
923 :     }
924 :    
925 :     sad = sad8(data->Cur, Reference, data->iEdgedWidth);
926 : syskin 628 sad += (data->lambda8 * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode) * (sad+NEIGH_8X8_BIAS))/100;
927 : Isibaar 580
928 :     if (sad < *(data->iMinSAD)) {
929 :     *(data->iMinSAD) = sad;
930 :     data->currentQMV->x = x; data->currentQMV->y = y;
931 :     *dir = Direction; }
932 :     }
933 :    
934 : Isibaar 539 /* CHECK_CANDIATE FUNCTIONS END */
935 : chl 96
936 : chl 530 /* MAINSEARCH FUNCTIONS START */
937 : edgomez 195
938 : chl 530 static void
939 :     AdvDiamondSearch(int x, int y, const SearchData * const data, int bDirection)
940 : chl 181 {
941 :    
942 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
943 :    
944 : chl 530 int iDirection;
945 : edgomez 195
946 :     do {
947 : chl 181 iDirection = 0;
948 : chl 530 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
949 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
950 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
951 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
952 : chl 181
953 :     /* now we're doing diagonal checks near our candidate */
954 :    
955 : chl 530 if (iDirection) { //checking if anything found
956 : chl 181 bDirection = iDirection;
957 :     iDirection = 0;
958 : chl 530 x = data->currentMV->x; y = data->currentMV->y;
959 :     if (bDirection & 3) { //our candidate is left or right
960 :     CHECK_CANDIDATE(x, y + iDiamondSize, 8);
961 :     CHECK_CANDIDATE(x, y - iDiamondSize, 4);
962 :     } else { // what remains here is up or down
963 :     CHECK_CANDIDATE(x + iDiamondSize, y, 2);
964 :     CHECK_CANDIDATE(x - iDiamondSize, y, 1); }
965 : chl 181
966 : edgomez 195 if (iDirection) {
967 :     bDirection += iDirection;
968 : chl 530 x = data->currentMV->x; y = data->currentMV->y; }
969 :     } else { //about to quit, eh? not so fast....
970 : edgomez 195 switch (bDirection) {
971 : chl 181 case 2:
972 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
973 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
974 : chl 181 break;
975 :     case 1:
976 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
977 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
978 : chl 181 break;
979 : edgomez 195 case 2 + 4:
980 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
981 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
982 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
983 : chl 181 break;
984 :     case 4:
985 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
986 :     CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
987 : chl 181 break;
988 :     case 8:
989 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
990 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
991 : chl 181 break;
992 : edgomez 195 case 1 + 4:
993 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
994 :     CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
995 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
996 : chl 181 break;
997 : edgomez 195 case 2 + 8:
998 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
999 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
1000 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
1001 : chl 181 break;
1002 : edgomez 195 case 1 + 8:
1003 : chl 530 CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
1004 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
1005 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
1006 : chl 181 break;
1007 : edgomez 195 default: //1+2+4+8 == we didn't find anything at all
1008 : chl 530 CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
1009 :     CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
1010 :     CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
1011 :     CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
1012 : chl 181 break;
1013 :     }
1014 : chl 530 if (!iDirection) break; //ok, the end. really
1015 :     bDirection = iDirection;
1016 :     x = data->currentMV->x; y = data->currentMV->y;
1017 : chl 181 }
1018 :     }
1019 : edgomez 195 while (1); //forever
1020 : chl 181 }
1021 :    
1022 : chl 530 static void
1023 :     SquareSearch(int x, int y, const SearchData * const data, int bDirection)
1024 : chl 326 {
1025 : chl 530 int iDirection;
1026 : chl 326
1027 : chl 530 do {
1028 :     iDirection = 0;
1029 :     if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1+16+64);
1030 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2+32+128);
1031 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4+16+32);
1032 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8+64+128);
1033 :     if (bDirection & 16) CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1+4+16+32+64);
1034 :     if (bDirection & 32) CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2+4+16+32+128);
1035 :     if (bDirection & 64) CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1+8+16+64+128);
1036 :     if (bDirection & 128) CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2+8+32+64+128);
1037 : chl 326
1038 : chl 530 bDirection = iDirection;
1039 :     x = data->currentMV->x; y = data->currentMV->y;
1040 :     } while (iDirection);
1041 : chl 346 }
1042 :    
1043 : chl 530 static void
1044 :     DiamondSearch(int x, int y, const SearchData * const data, int bDirection)
1045 : chl 346 {
1046 :    
1047 : chl 181 /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
1048 :    
1049 : chl 530 int iDirection;
1050 : edgomez 195
1051 :     do {
1052 : chl 181 iDirection = 0;
1053 : chl 530 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
1054 :     if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
1055 :     if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
1056 :     if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
1057 : chl 181
1058 :     /* now we're doing diagonal checks near our candidate */
1059 :    
1060 : chl 530 if (iDirection) { //checking if anything found
1061 : chl 181 bDirection = iDirection;
1062 :     iDirection = 0;
1063 : chl 530 x = data->currentMV->x; y = data->currentMV->y;
1064 :     if (bDirection & 3) { //our candidate is left or right
1065 :     CHECK_CANDIDATE(x, y + iDiamondSize, 8);
1066 :     CHECK_CANDIDATE(x, y - iDiamondSize, 4);
1067 :     } else { // what remains here is up or down
1068 :     CHECK_CANDIDATE(x + iDiamondSize, y, 2);
1069 :     CHECK_CANDIDATE(x - iDiamondSize, y, 1); }
1070 : chl 181
1071 : chl 530 bDirection += iDirection;
1072 :     x = data->currentMV->x; y = data->currentMV->y;
1073 : chl 181 }
1074 :     }
1075 : chl 530 while (iDirection);
1076 : chl 181 }
1077 :    
1078 : chl 530 /* MAINSEARCH FUNCTIONS END */
1079 : chl 181
1080 : chl 530 /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */
1081 :    
1082 :     static void
1083 :     HalfpelRefine(const SearchData * const data)
1084 : chl 96 {
1085 : chl 530 /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1086 : edgomez 195
1087 : chl 530 VECTOR backupMV = *(data->currentMV);
1088 :     int iDirection; //not needed
1089 : chl 96
1090 : chl 530 CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
1091 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
1092 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);
1093 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);
1094 : edgomez 195
1095 : chl 530 CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);
1096 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);
1097 :    
1098 :     CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
1099 :     CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
1100 : chl 96 }
1101 :    
1102 : Isibaar 580
1103 :     static void
1104 :     QuarterpelRefine(const SearchData * const data)
1105 :     {
1106 :     /* Perform quarter pixel refinement*/
1107 :    
1108 :     VECTOR backupMV = *(data->currentQMV);
1109 :     int iDirection; //not needed
1110 :    
1111 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
1112 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
1113 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);
1114 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);
1115 :    
1116 :     CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);
1117 :     CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);
1118 :    
1119 :     CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
1120 :     CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
1121 :    
1122 :     }
1123 :    
1124 : chl 530 static __inline int
1125 :     SkipDecisionP(const IMAGE * current, const IMAGE * reference,
1126 :     const int x, const int y,
1127 :     const uint32_t iEdgedWidth, const uint32_t iQuant)
1128 : chl 96
1129 : Isibaar 3 {
1130 : chl 530 /* keep repeating checks for all b-frames before this P frame,
1131 :     to make sure that SKIP is possible (todo)
1132 :     how: if skip is not possible set sad00 to a very high value */
1133 : Isibaar 3
1134 : chl 530 uint32_t sadC = sad8(current->u + x*8 + y*(iEdgedWidth/2)*8,
1135 :     reference->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2);
1136 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
1137 : h 545 sadC += sad8(current->v + (x + y*(iEdgedWidth/2))*8,
1138 :     reference->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
1139 : chl 530 if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
1140 : edgomez 195
1141 : chl 530 return 1;
1142 :     }
1143 : edgomez 195
1144 : chl 530 static __inline void
1145 :     SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad)
1146 :     {
1147 :     pMB->mode = MODE_NOT_CODED;
1148 : Isibaar 539 pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1149 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1150 : syskin 628
1151 : Isibaar 580 pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0;
1152 :     pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0;
1153 :    
1154 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
1155 : Isibaar 3 }
1156 :    
1157 : syskin 628 bool
1158 : chl 530 MotionEstimation(MBParam * const pParam,
1159 :     FRAMEINFO * const current,
1160 :     FRAMEINFO * const reference,
1161 :     const IMAGE * const pRefH,
1162 :     const IMAGE * const pRefV,
1163 :     const IMAGE * const pRefHV,
1164 :     const uint32_t iLimit)
1165 : Isibaar 3 {
1166 : chl 530 MACROBLOCK *const pMBs = current->mbs;
1167 :     const IMAGE *const pCurrent = &current->image;
1168 :     const IMAGE *const pRef = &reference->image;
1169 : Isibaar 3
1170 : chl 530 const VECTOR zeroMV = { 0, 0 };
1171 : Isibaar 3
1172 : chl 530 uint32_t x, y;
1173 :     uint32_t iIntra = 0;
1174 : syskin 628 int32_t InterBias, quant = current->quant, sad00;
1175 : Isibaar 580 uint8_t *qimage;
1176 : edgomez 195
1177 : Isibaar 539 // some pre-initialized thingies for SearchP
1178 :     int32_t temp[5];
1179 :     VECTOR currentMV[5];
1180 : Isibaar 580 VECTOR currentQMV[5];
1181 : Isibaar 539 int32_t iMinSAD[5];
1182 :     SearchData Data;
1183 :     Data.iEdgedWidth = pParam->edged_width;
1184 :     Data.currentMV = currentMV;
1185 : Isibaar 580 Data.currentQMV = currentQMV;
1186 : Isibaar 539 Data.iMinSAD = iMinSAD;
1187 :     Data.temp = temp;
1188 :     Data.iFcode = current->fcode;
1189 : Isibaar 580 Data.rounding = pParam->m_rounding_type;
1190 : syskin 628 Data.qpel = pParam->m_quarterpel;
1191 :     Data.chroma = current->global_flags & XVID_ME_COLOUR;
1192 : Isibaar 539
1193 : Isibaar 580 if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
1194 : syskin 594 return 1; // allocate some mem for qpel interpolated blocks
1195 : Isibaar 580 // somehow this is dirty since I think we shouldn't use malloc outside
1196 :     // encoder_create() - so please fix me!
1197 : syskin 628 Data.RefQ = qimage;
1198 : chl 530 if (sadInit) (*sadInit) ();
1199 : edgomez 195
1200 : chl 530 for (y = 0; y < pParam->mb_height; y++) {
1201 :     for (x = 0; x < pParam->mb_width; x++) {
1202 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
1203 : syskin 628
1204 :     pMB->sad16
1205 : chl 530 = sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16,
1206 :     pRef->y + (x + y * pParam->edged_width) * 16,
1207 :     pParam->edged_width, pMB->sad8 );
1208 : edgomez 195
1209 : syskin 628 if (Data.chroma) {
1210 :     pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8,
1211 :     pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2);
1212 :    
1213 :     pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8,
1214 :     pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2);
1215 :     }
1216 :    
1217 :     sad00 = pMB->sad16; //if no gmc; else sad00 = (..)
1218 :    
1219 : chl 530 if (!(current->global_flags & XVID_LUMIMASKING)) {
1220 :     pMB->dquant = NO_CHANGE;
1221 : syskin 601 pMB->quant = current->quant;
1222 :     } else {
1223 : syskin 574 if (pMB->dquant != NO_CHANGE) {
1224 :     quant += DQtab[pMB->dquant];
1225 :     if (quant > 31) quant = 31;
1226 :     else if (quant < 1) quant = 1;
1227 :     }
1228 : syskin 601 pMB->quant = quant;
1229 :     }
1230 : chl 181
1231 : syskin 628 //initial skip decision
1232 : chl 619 /* no early skip for GMC (global vector = skip vector is unknown!) */
1233 :     if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */
1234 :     if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH)
1235 : syskin 628 if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {
1236 : chl 619 SkipMacroblockP(pMB, sad00);
1237 :     continue;
1238 :     }
1239 :     }
1240 : syskin 628
1241 :     SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1242 : chl 530 y, current->motion_flags, pMB->quant,
1243 : Isibaar 539 &Data, pParam, pMBs, reference->mbs,
1244 : chl 530 current->global_flags & XVID_INTER4V, pMB);
1245 : edgomez 195
1246 : chl 530 /* final skip decision, a.k.a. "the vector you found, really that good?" */
1247 : chl 619 if (current->coding_type == P_VOP) {
1248 :     if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)
1249 : syskin 601 && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) )
1250 : syskin 628 if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {
1251 : chl 619 SkipMacroblockP(pMB, sad00);
1252 :     continue;
1253 :     }
1254 :     }
1255 : syskin 628
1256 : chl 530 /* finally, intra decision */
1257 : Isibaar 3
1258 : chl 530 InterBias = MV16_INTER_BIAS;
1259 : syskin 639 if (pMB->quant > 8) InterBias += 100 * (pMB->quant - 8); // to make high quants work
1260 : chl 530 if (y != 0)
1261 : syskin 639 if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
1262 : chl 530 if (x != 0)
1263 : syskin 639 if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
1264 : syskin 628
1265 :     if (Data.chroma) InterBias += 50; // to compensate bigger SAD
1266 : Isibaar 3
1267 : chl 530 if (InterBias < pMB->sad16) {
1268 :     const int32_t deviation =
1269 :     dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
1270 :     pParam->edged_width);
1271 : Isibaar 3
1272 : chl 530 if (deviation < (pMB->sad16 - InterBias)) {
1273 : Isibaar 580 if (++iIntra >= iLimit) { free(qimage); return 1; }
1274 : chl 530 pMB->mode = MODE_INTRA;
1275 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =
1276 :     pMB->mvs[3] = zeroMV;
1277 : Isibaar 580 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] =
1278 :     pMB->qmvs[3] = zeroMV;
1279 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =
1280 :     pMB->sad8[3] = 0;
1281 :     }
1282 :     }
1283 :     }
1284 : Isibaar 3 }
1285 : Isibaar 580 free(qimage);
1286 : chl 619
1287 :     if (current->coding_type == S_VOP) /* first GMC step only for S(GMC)-VOPs */
1288 : syskin 628 current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode );
1289 : chl 619 else
1290 :     current->GMC_MV = zeroMV;
1291 :    
1292 : syskin 628 return 0;
1293 : chl 530 }
1294 : Isibaar 3
1295 : edgomez 195
1296 : chl 530 #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
1297 : Isibaar 3
1298 : chl 530 static __inline int
1299 :     make_mask(const VECTOR * const pmv, const int i)
1300 :     {
1301 : Isibaar 539 int mask = 255, j;
1302 : chl 530 for (j = 0; j < i; j++) {
1303 :     if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already
1304 :     if (pmv[i].x == pmv[j].x) {
1305 :     if (pmv[i].y == pmv[j].y + iDiamondSize) { mask &= ~4; continue; }
1306 :     if (pmv[i].y == pmv[j].y - iDiamondSize) { mask &= ~8; continue; }
1307 :     } else
1308 :     if (pmv[i].y == pmv[j].y) {
1309 :     if (pmv[i].x == pmv[j].x + iDiamondSize) { mask &= ~1; continue; }
1310 :     if (pmv[i].x == pmv[j].x - iDiamondSize) { mask &= ~2; continue; }
1311 :     }
1312 : Isibaar 3 }
1313 : chl 530 return mask;
1314 :     }
1315 : edgomez 195
1316 : chl 530 static __inline void
1317 :     PreparePredictionsP(VECTOR * const pmv, int x, int y, const int iWcount,
1318 :     const int iHcount, const MACROBLOCK * const prevMB)
1319 :     {
1320 : edgomez 195
1321 : chl 530 //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself
1322 : edgomez 195
1323 : chl 530 if ( (y != 0) && (x != (iWcount-1)) ) { // [5] top-right neighbour
1324 :     pmv[5].x = EVEN(pmv[3].x);
1325 : Isibaar 539 pmv[5].y = EVEN(pmv[3].y);
1326 :     } else pmv[5].x = pmv[5].y = 0;
1327 : chl 169
1328 : chl 530 if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }// pmv[3] is left neighbour
1329 :     else pmv[3].x = pmv[3].y = 0;
1330 : Isibaar 3
1331 : chl 530 if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }// [4] top neighbour
1332 :     else pmv[4].x = pmv[4].y = 0;
1333 : chl 169
1334 : chl 530 // [1] median prediction
1335 :     pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
1336 : chl 169
1337 : 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
1338 : chl 169
1339 : chl 530 pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame
1340 :     pmv[2].y = EVEN(prevMB->mvs[0].y);
1341 : chl 169
1342 : chl 530 if ((x != iWcount-1) && (y != iHcount-1)) {
1343 :     pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame
1344 : Isibaar 539 pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
1345 :     } else pmv[6].x = pmv[6].y = 0;
1346 : chl 530 }
1347 : chl 169
1348 : chl 530 static void
1349 : syskin 628 SearchP(const IMAGE * const pRef,
1350 : chl 530 const uint8_t * const pRefH,
1351 :     const uint8_t * const pRefV,
1352 :     const uint8_t * const pRefHV,
1353 :     const IMAGE * const pCur,
1354 :     const int x,
1355 :     const int y,
1356 :     const uint32_t MotionFlags,
1357 :     const uint32_t iQuant,
1358 : Isibaar 539 SearchData * const Data,
1359 : chl 530 const MBParam * const pParam,
1360 :     const MACROBLOCK * const pMBs,
1361 :     const MACROBLOCK * const prevMBs,
1362 :     int inter4v,
1363 :     MACROBLOCK * const pMB)
1364 :     {
1365 : chl 169
1366 : chl 530 int i, iDirection = 255, mask, threshA;
1367 : Isibaar 539 VECTOR pmv[7];
1368 : Isibaar 3
1369 : Isibaar 539 get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)()
1370 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1371 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
1372 : Isibaar 3
1373 : Isibaar 539 Data->predMV = pmv[0];
1374 : Isibaar 580
1375 : Isibaar 539 Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
1376 : syskin 628 Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1377 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1378 :    
1379 :     Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;
1380 : Isibaar 539 Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
1381 :     Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
1382 :     Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
1383 : syskin 628 Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1384 :     Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1385 : Isibaar 3
1386 : syskin 601 Data->lambda16 = lambda_vec16[iQuant];
1387 : Isibaar 606 Data->lambda8 = lambda_vec8[iQuant];
1388 : chl 169
1389 : chl 530 if (!(MotionFlags & PMV_HALFPEL16)) {
1390 : Isibaar 539 Data->min_dx = EVEN(Data->min_dx);
1391 :     Data->max_dx = EVEN(Data->max_dx);
1392 :     Data->min_dy = EVEN(Data->min_dy);
1393 :     Data->max_dy = EVEN(Data->max_dy); }
1394 : edgomez 568
1395 :     if (pMB->dquant != NO_CHANGE) inter4v = 0;
1396 : edgomez 195
1397 : Isibaar 580 for(i = 0; i < 5; i++)
1398 :     Data->currentMV[i].x = Data->currentMV[i].y = 0;
1399 : Isibaar 539
1400 : Isibaar 606 if (pParam->m_quarterpel) {
1401 :     Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1402 :     i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode);
1403 :     } else i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);
1404 :    
1405 : syskin 628 Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000;
1406 :     Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100;
1407 : Isibaar 539 Data->iMinSAD[2] = pMB->sad8[1];
1408 :     Data->iMinSAD[3] = pMB->sad8[2];
1409 :     Data->iMinSAD[4] = pMB->sad8[3];
1410 :    
1411 : chl 530 if ((x == 0) && (y == 0)) threshA = 512;
1412 :     else {
1413 : edgomez 568 threshA = Data->temp[0]; // that's when we keep this SAD atm
1414 : chl 530 if (threshA < 512) threshA = 512;
1415 :     if (threshA > 1024) threshA = 1024; }
1416 : Isibaar 3
1417 : chl 530 PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
1418 :     prevMBs + x + y * pParam->mb_width);
1419 : edgomez 195
1420 : syskin 628 if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16;
1421 :     else CheckCandidate = CheckCandidate16no4v;
1422 : edgomez 568
1423 : chl 530 /* main loop. checking all predictions */
1424 :    
1425 :     for (i = 1; i < 7; i++) {
1426 :     if (!(mask = make_mask(pmv, i)) ) continue;
1427 : edgomez 568 (*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1428 : Isibaar 539 if (Data->iMinSAD[0] <= threshA) break;
1429 : chl 530 }
1430 :    
1431 : Isibaar 539 if ((Data->iMinSAD[0] <= threshA) ||
1432 :     (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
1433 :     (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {
1434 : chl 530 inter4v = 0;
1435 : Isibaar 539 } else {
1436 : Isibaar 3
1437 : Isibaar 539 MainSearchFunc * MainSearchPtr;
1438 :     if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1439 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1440 :     else MainSearchPtr = DiamondSearch;
1441 : chl 181
1442 : Isibaar 539 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
1443 : Isibaar 3
1444 : chl 530 /* extended search, diamond starting in 0,0 and in prediction.
1445 :     note that this search is/might be done in halfpel positions,
1446 :     which makes it more different than the diamond above */
1447 : chl 259
1448 : Isibaar 539 if (MotionFlags & PMV_EXTSEARCH16) {
1449 :     int32_t bSAD;
1450 :     VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
1451 :     if (!(MotionFlags & PMV_HALFPELREFINE16)) // who's gonna use extsearch and no halfpel?
1452 :     startMV.x = EVEN(startMV.x); startMV.y = EVEN(startMV.y);
1453 :     if (!(MVequal(startMV, backupMV))) {
1454 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1455 : Isibaar 3
1456 : Isibaar 606 (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);
1457 : Isibaar 539 (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1458 :     if (bSAD < Data->iMinSAD[0]) {
1459 :     Data->currentMV[0] = backupMV;
1460 :     Data->iMinSAD[0] = bSAD; }
1461 :     }
1462 : Isibaar 3
1463 : Isibaar 539 backupMV = Data->currentMV[0];
1464 :     if (MotionFlags & PMV_HALFPELREFINE16) startMV.x = startMV.y = 1;
1465 :     else startMV.x = startMV.y = 0;
1466 :     if (!(MVequal(startMV, backupMV))) {
1467 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1468 : edgomez 195
1469 : Isibaar 606 (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);
1470 : Isibaar 539 (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1471 :     if (bSAD < Data->iMinSAD[0]) {
1472 :     Data->currentMV[0] = backupMV;
1473 :     Data->iMinSAD[0] = bSAD; }
1474 :     }
1475 : Isibaar 3 }
1476 :     }
1477 :    
1478 : Isibaar 539 if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);
1479 : Isibaar 3
1480 : Isibaar 580 for(i = 0; i < 5; i++) {
1481 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1482 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1483 :     }
1484 :    
1485 :     if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1486 :    
1487 : Isibaar 606 CheckCandidate = CheckCandidate16_qpel;
1488 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1489 : syskin 601 pParam->width, pParam->height, Data->iFcode, 0);
1490 :    
1491 : Isibaar 580 QuarterpelRefine(Data);
1492 :     }
1493 :    
1494 : syskin 628 if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0;
1495 : Isibaar 539 if (inter4v) {
1496 :     SearchData Data8;
1497 :     Data8.iFcode = Data->iFcode;
1498 : Isibaar 606 Data8.lambda8 = Data->lambda8;
1499 : Isibaar 539 Data8.iEdgedWidth = Data->iEdgedWidth;
1500 : Isibaar 606 Data8.RefQ = Data->RefQ;
1501 : syskin 628 Data8.qpel = Data->qpel;
1502 : Isibaar 539 Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1503 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1504 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1505 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1506 : syskin 628
1507 :     if (Data->chroma) {
1508 :     int sum, dx, dy;
1509 :    
1510 :     if(pParam->m_quarterpel) {
1511 :     sum = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;
1512 :     } else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1513 :     dy = (sum >> 3) + roundtab_76[sum & 0xf];
1514 :    
1515 :     if(pParam->m_quarterpel) {
1516 :     sum = pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;
1517 :     } else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1518 :     dx = (sum >> 3) + roundtab_76[sum & 0xf];
1519 :    
1520 :     Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1521 :     }
1522 : Isibaar 539 }
1523 : Isibaar 3
1524 : chl 530 if (!(inter4v) ||
1525 : Isibaar 539 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
1526 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
1527 : chl 530 // INTER MODE
1528 :     pMB->mode = MODE_INTER;
1529 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1]
1530 :     = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1531 : Isibaar 3
1532 : Isibaar 580 pMB->qmvs[0] = pMB->qmvs[1]
1533 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1534 :    
1535 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1536 : Isibaar 539 pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1537 : Isibaar 3
1538 : Isibaar 580 if(pParam->m_quarterpel) {
1539 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;
1540 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;
1541 : syskin 628 } else {
1542 : Isibaar 580 pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1543 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1544 :     }
1545 : edgomez 195 } else {
1546 : chl 530 // INTER4V MODE; all other things are already set in Search8
1547 :     pMB->mode = MODE_INTER4V;
1548 : Isibaar 539 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] +
1549 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * iQuant;
1550 : edgomez 78 }
1551 : Isibaar 3 }
1552 :    
1553 : chl 530 static void
1554 :     Search8(const SearchData * const OldData,
1555 :     const int x, const int y,
1556 :     const uint32_t MotionFlags,
1557 :     const MBParam * const pParam,
1558 :     MACROBLOCK * const pMB,
1559 :     const MACROBLOCK * const pMBs,
1560 : Isibaar 539 const int block,
1561 :     SearchData * const Data)
1562 : chl 345 {
1563 : Isibaar 539 Data->iMinSAD = OldData->iMinSAD + 1 + block;
1564 :     Data->currentMV = OldData->currentMV + 1 + block;
1565 : Isibaar 580 Data->currentQMV = OldData->currentQMV + 1 + block;
1566 : chl 345
1567 : syskin 601 if(pParam->m_quarterpel) {
1568 : Isibaar 606 Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1569 : syskin 628 if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
1570 : syskin 601 d_mv_bits( Data->currentQMV->x - Data->predQMV.x,
1571 :     Data->currentQMV->y - Data->predQMV.y,
1572 : syskin 628 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1573 : Isibaar 606 } else {
1574 :     Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1575 : syskin 628 if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
1576 : syskin 601 d_mv_bits( Data->currentMV->x - Data->predMV.x,
1577 :     Data->currentMV->y - Data->predMV.y,
1578 : syskin 628 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1579 : Isibaar 606 }
1580 : chl 345
1581 : chl 530 if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) {
1582 : chl 345
1583 : Isibaar 539 Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1));
1584 :     Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));
1585 :     Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1586 :     Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1587 : chl 345
1588 : Isibaar 539 Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));
1589 : chl 530
1590 : Isibaar 539 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1591 : Isibaar 580 pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);
1592 : syskin 628 CheckCandidate = CheckCandidate8;
1593 : chl 345
1594 : chl 530 if (MotionFlags & PMV_EXTSEARCH8) {
1595 : Isibaar 580 int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1596 :    
1597 : chl 530 MainSearchFunc *MainSearchPtr;
1598 :     if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;
1599 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1600 :     else MainSearchPtr = DiamondSearch;
1601 : chl 345
1602 : Isibaar 580 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1603 : chl 345
1604 : syskin 601 if(*(Data->iMinSAD) < temp_sad) {
1605 : Isibaar 580 Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1606 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1607 :     }
1608 :     }
1609 :    
1610 :     if (MotionFlags & PMV_HALFPELREFINE8) {
1611 :     int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1612 :    
1613 :     HalfpelRefine(Data); // perform halfpel refine of current best vector
1614 :    
1615 :     if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1616 :     Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1617 :     Data->currentQMV->y = 2 * Data->currentMV->y;
1618 :     }
1619 :     }
1620 :    
1621 : syskin 601 if(pParam->m_quarterpel) {
1622 :     if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&
1623 :     (MotionFlags & PMV_QUARTERPELREFINE8)) {
1624 : Isibaar 600 CheckCandidate = CheckCandidate8_qpel;
1625 : Isibaar 606 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1626 :     pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);
1627 :     QuarterpelRefine(Data);
1628 : syskin 601 }
1629 : Isibaar 580 }
1630 : chl 345 }
1631 : chl 530
1632 : Isibaar 580 if(pParam->m_quarterpel) {
1633 :     pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x;
1634 :     pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y;
1635 :     }
1636 :     else {
1637 :     pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
1638 :     pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
1639 :     }
1640 :    
1641 : Isibaar 539 pMB->mvs[block] = *(Data->currentMV);
1642 : Isibaar 580 pMB->qmvs[block] = *(Data->currentQMV);
1643 :    
1644 : syskin 601 pMB->sad8[block] = 4 * (*Data->iMinSAD);
1645 : chl 345 }
1646 :    
1647 : chl 530 /* B-frames code starts here */
1648 : chl 345
1649 : chl 530 static __inline VECTOR
1650 :     ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
1651 : Isibaar 3 {
1652 : chl 530 /* the stupidiest function ever */
1653 :     if (mode == MODE_FORWARD) return pMB->mvs[0];
1654 :     else return pMB->b_mvs[0];
1655 : Isibaar 3 }
1656 :    
1657 : chl 530 static void __inline
1658 :     PreparePredictionsBF(VECTOR * const pmv, const int x, const int y,
1659 :     const uint32_t iWcount,
1660 :     const MACROBLOCK * const pMB,
1661 :     const uint32_t mode_curr)
1662 : Isibaar 3 {
1663 :    
1664 : chl 530 // [0] is prediction
1665 :     pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);
1666 : Isibaar 3
1667 : chl 530 pmv[1].x = pmv[1].y = 0; // [1] is zero
1668 : Isibaar 3
1669 : chl 530 pmv[2] = ChoosePred(pMB, mode_curr);
1670 :     pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);
1671 : edgomez 195
1672 : chl 530 if ((y != 0)&&(x != (int)(iWcount+1))) { // [3] top-right neighbour
1673 :     pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);
1674 : Isibaar 539 pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);
1675 :     } else pmv[3].x = pmv[3].y = 0;
1676 : Isibaar 3
1677 : chl 530 if (y != 0) {
1678 :     pmv[4] = ChoosePred(pMB-iWcount, mode_curr);
1679 :     pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y);
1680 :     } else pmv[4].x = pmv[4].y = 0;
1681 : Isibaar 3
1682 : chl 530 if (x != 0) {
1683 :     pmv[5] = ChoosePred(pMB-1, mode_curr);
1684 :     pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1685 :     } else pmv[5].x = pmv[5].y = 0;
1686 : Isibaar 3
1687 : chl 530 if ((x != 0)&&(y != 0)) {
1688 :     pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);
1689 :     pmv[6].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1690 :     } else pmv[6].x = pmv[6].y = 0;
1691 : edgomez 195
1692 : chl 530 // more?
1693 :     }
1694 : chl 181
1695 : edgomez 170
1696 : chl 530 /* search backward or forward, for b-frames */
1697 :     static void
1698 :     SearchBF( const uint8_t * const pRef,
1699 :     const uint8_t * const pRefH,
1700 :     const uint8_t * const pRefV,
1701 :     const uint8_t * const pRefHV,
1702 :     const IMAGE * const pCur,
1703 :     const int x, const int y,
1704 :     const uint32_t MotionFlags,
1705 :     const uint32_t iFcode,
1706 :     const MBParam * const pParam,
1707 :     MACROBLOCK * const pMB,
1708 :     const VECTOR * const predMV,
1709 :     int32_t * const best_sad,
1710 : h 545 const int32_t mode_current,
1711 :     SearchData * const Data)
1712 : chl 530 {
1713 : Isibaar 3
1714 : chl 530 const int32_t iEdgedWidth = pParam->edged_width;
1715 :    
1716 :     int i, iDirection, mask;
1717 : h 545 VECTOR pmv[7];
1718 : chl 530 MainSearchFunc *MainSearchPtr;
1719 : h 545 *Data->iMinSAD = MV_MAX_ERROR;
1720 :     Data->iFcode = iFcode;
1721 : Isibaar 3
1722 : h 545 Data->Ref = pRef + (x + y * iEdgedWidth) * 16;
1723 :     Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;
1724 :     Data->RefV = pRefV + (x + y * iEdgedWidth) * 16;
1725 :     Data->RefHV = pRefHV + (x + y * iEdgedWidth) * 16;
1726 : Isibaar 3
1727 : h 545 Data->predMV = *predMV;
1728 : edgomez 195
1729 : h 545 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1730 : Isibaar 580 pParam->width, pParam->height, iFcode, pParam->m_quarterpel);
1731 : Isibaar 3
1732 : h 545 pmv[0] = Data->predMV;
1733 : syskin 658 if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }
1734 : h 545 PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);
1735 : edgomez 195
1736 : h 545 Data->currentMV->x = Data->currentMV->y = 0;
1737 : Isibaar 3
1738 : chl 530 CheckCandidate = CheckCandidate16no4v;
1739 : chl 181
1740 : chl 530 // main loop. checking all predictions
1741 :     for (i = 0; i < 8; i++) {
1742 :     if (!(mask = make_mask(pmv, i)) ) continue;
1743 : h 545 CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1744 : edgomez 78 }
1745 : Isibaar 3
1746 : chl 530 if (MotionFlags & PMV_USESQUARES16)
1747 :     MainSearchPtr = SquareSearch;
1748 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1749 :     MainSearchPtr = AdvDiamondSearch;
1750 :     else MainSearchPtr = DiamondSearch;
1751 : chl 96
1752 : h 545 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1753 : chl 169
1754 : h 545 HalfpelRefine(Data);
1755 : syskin 658
1756 :     if (Data->qpel) {
1757 :     Data->currentQMV->x = 2*Data->currentMV->x;
1758 :     Data->currentQMV->y = 2*Data->currentMV->y;
1759 :     CheckCandidate = CheckCandidate16no4v_qpel;
1760 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1761 :     pParam->width, pParam->height, iFcode, pParam->m_quarterpel);
1762 :     QuarterpelRefine(Data);
1763 :     }
1764 : chl 169
1765 : chl 530 // three bits are needed to code backward mode. four for forward
1766 :     // we treat the bits just like they were vector's
1767 : syskin 628 if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16;
1768 :     else *Data->iMinSAD += 3 * Data->lambda16;
1769 : chl 169
1770 : h 545 if (*Data->iMinSAD < *best_sad) {
1771 :     *best_sad = *Data->iMinSAD;
1772 : chl 530 pMB->mode = mode_current;
1773 : syskin 658 if (Data->qpel) {
1774 :     pMB->pmvs[0].x = Data->currentQMV->x - predMV->x;
1775 :     pMB->pmvs[0].y = Data->currentQMV->y - predMV->y;
1776 :     if (mode_current == MODE_FORWARD)
1777 :     pMB->qmvs[0] = *Data->currentQMV;
1778 :     else
1779 :     pMB->b_qmvs[0] = *Data->currentQMV;
1780 :     } else {
1781 :     pMB->pmvs[0].x = Data->currentMV->x - predMV->x;
1782 :     pMB->pmvs[0].y = Data->currentMV->y - predMV->y;
1783 :     }
1784 :     if (mode_current == MODE_FORWARD)
1785 :     pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV;
1786 :     else
1787 :     pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search
1788 :    
1789 : chl 169 }
1790 : chl 530
1791 :     }
1792 : chl 169
1793 : chl 530 static int32_t
1794 : Isibaar 539 SearchDirect(const IMAGE * const f_Ref,
1795 : chl 530 const uint8_t * const f_RefH,
1796 :     const uint8_t * const f_RefV,
1797 :     const uint8_t * const f_RefHV,
1798 : Isibaar 539 const IMAGE * const b_Ref,
1799 : chl 530 const uint8_t * const b_RefH,
1800 :     const uint8_t * const b_RefV,
1801 :     const uint8_t * const b_RefHV,
1802 :     const IMAGE * const pCur,
1803 :     const int x, const int y,
1804 :     const uint32_t MotionFlags,
1805 :     const int32_t TRB, const int32_t TRD,
1806 :     const MBParam * const pParam,
1807 :     MACROBLOCK * const pMB,
1808 :     const MACROBLOCK * const b_mb,
1809 : h 545 int32_t * const best_sad,
1810 :     SearchData * const Data)
1811 : edgomez 195
1812 : chl 530 {
1813 : h 545 int32_t skip_sad;
1814 : chl 530 int k;
1815 : h 545
1816 : chl 530 MainSearchFunc *MainSearchPtr;
1817 : Isibaar 3
1818 : h 545 *Data->iMinSAD = 256*4096;
1819 : chl 140
1820 : h 545 Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1821 :     Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16;
1822 :     Data->RefV = f_RefV + (x + Data->iEdgedWidth*y) * 16;
1823 :     Data->RefHV = f_RefHV + (x + Data->iEdgedWidth*y) * 16;
1824 :     Data->bRef = b_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1825 :     Data->bRefH = b_RefH + (x + Data->iEdgedWidth*y) * 16;
1826 :     Data->bRefV = b_RefV + (x + Data->iEdgedWidth*y) * 16;
1827 :     Data->bRefHV = b_RefHV + (x + Data->iEdgedWidth*y) * 16;
1828 : chl 140
1829 : h 545 Data->max_dx = 2 * pParam->width - 2 * (x) * 16;
1830 :     Data->max_dy = 2 * pParam->height - 2 * (y) * 16;
1831 :     Data->min_dx = -(2 * 16 + 2 * (x) * 16);
1832 :     Data->min_dy = -(2 * 16 + 2 * (y) * 16);
1833 : syskin 658 if (Data->qpel) { //we measure in qpixels
1834 :     Data->max_dx *= 2;
1835 :     Data->max_dy *= 2;
1836 :     Data->min_dx *= 2;
1837 :     Data->min_dy *= 2;
1838 :     Data->referencemv = b_mb->qmvs;
1839 :     } else Data->referencemv = b_mb->mvs;
1840 : Isibaar 3
1841 : chl 530 for (k = 0; k < 4; k++) {
1842 : h 545 pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);
1843 :     pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD;
1844 :     pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);
1845 :     pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;
1846 : Isibaar 3
1847 : h 545 if ( ( pMB->b_mvs[k].x > Data->max_dx ) || ( pMB->b_mvs[k].x < Data->min_dx )
1848 :     || ( pMB->b_mvs[k].y > Data->max_dy ) || ( pMB->b_mvs[k].y < Data->min_dy )) {
1849 : Isibaar 3
1850 : Isibaar 539 *best_sad = 256*4096; // in that case, we won't use direct mode
1851 :     pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"
1852 :     pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;
1853 :     return 0;
1854 :     }
1855 :     if (b_mb->mode != MODE_INTER4V) {
1856 :     pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0];
1857 :     pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0];
1858 : h 545 Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];
1859 :     Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];
1860 : Isibaar 539 break;
1861 :     }
1862 :     }
1863 : Isibaar 3
1864 : syskin 658 if (Data->qpel) {
1865 :     if (b_mb->mode == MODE_INTER4V)
1866 :     CheckCandidate = CheckCandidateDirect_qpel;
1867 :     else CheckCandidate = CheckCandidateDirectno4v_qpel;
1868 :     } else {
1869 :     if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect;
1870 :     else CheckCandidate = CheckCandidateDirectno4v;
1871 :     }
1872 :    
1873 : h 545 (*CheckCandidate)(0, 0, 255, &k, Data);
1874 : Isibaar 3
1875 : chl 530 // skip decision
1876 : syskin 628 if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) {
1877 : syskin 594 //possible skip - checking chroma. everything copied from MC
1878 : Isibaar 539 //this is not full chroma compensation, only it's fullpel approximation. should work though
1879 :     int sum, dx, dy, b_dx, b_dy;
1880 : Isibaar 3
1881 : syskin 658 if (Data->qpel) {
1882 :     sum = pMB->mvs[0].y/2 + pMB->mvs[1].y/2 + pMB->mvs[2].y/2 + pMB->mvs[3].y/2;
1883 :     dy = (sum >> 3) + roundtab_76[sum & 0xf];
1884 :     sum = pMB->mvs[0].x/2 + pMB->mvs[1].x/2 + pMB->mvs[2].x/2 + pMB->mvs[3].x/2;
1885 :     dx = (sum >> 3) + roundtab_76[sum & 0xf];
1886 : Isibaar 539
1887 : syskin 658 sum = pMB->b_mvs[0].y/2 + pMB->b_mvs[1].y/2 + pMB->b_mvs[2].y/2 + pMB->b_mvs[3].y/2;
1888 :     b_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1889 :     sum = pMB->b_mvs[0].x/2 + pMB->b_mvs[1].x/2 + pMB->b_mvs[2].x/2 + pMB->b_mvs[3].x/2;
1890 :     b_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1891 : Isibaar 539
1892 : syskin 658 } else {
1893 :     sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1894 :     dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1895 :     sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1896 :     dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1897 : Isibaar 539
1898 : syskin 658 sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1899 :     b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1900 :     sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1901 :     b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1902 :     }
1903 : h 545 sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2),
1904 :     f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1905 :     b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
1906 :     Data->iEdgedWidth/2);
1907 :     sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2),
1908 :     f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1909 :     b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
1910 :     Data->iEdgedWidth/2);
1911 : Isibaar 539
1912 : syskin 601 if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1913 : Isibaar 539 pMB->mode = MODE_DIRECT_NONE_MV;
1914 : h 545 return *Data->iMinSAD;
1915 : Isibaar 539 }
1916 :     }
1917 :    
1918 : h 545 skip_sad = *Data->iMinSAD;
1919 : edgomez 195
1920 : chl 530 // DIRECT MODE DELTA VECTOR SEARCH.
1921 :     // This has to be made more effective, but at the moment I'm happy it's running at all
1922 : Isibaar 3
1923 : chl 530 if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1924 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1925 :     else MainSearchPtr = DiamondSearch;
1926 : edgomez 195
1927 : h 545 (*MainSearchPtr)(0, 0, Data, 255);
1928 : Isibaar 3
1929 : syskin 658 HalfpelRefine(Data); //or qpel refine, if we're in qpel mode
1930 : Isibaar 3
1931 : syskin 628 *Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode
1932 : h 545 *best_sad = *Data->iMinSAD;
1933 : Isibaar 3
1934 : chl 530 if (b_mb->mode == MODE_INTER4V)
1935 :     pMB->mode = MODE_DIRECT;
1936 :     else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation
1937 : edgomez 195
1938 : h 545 pMB->pmvs[3] = *Data->currentMV;
1939 : edgomez 195
1940 : chl 530 for (k = 0; k < 4; k++) {
1941 : h 545 pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x;
1942 : syskin 658 pMB->b_mvs[k].x = ( (Data->currentMV->x == 0)
1943 : h 545 ? Data->directmvB[k].x
1944 : syskin 658 :pMB->mvs[k].x - Data->referencemv[k].x);
1945 : h 545 pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y);
1946 :     pMB->b_mvs[k].y = ((Data->currentMV->y == 0)
1947 :     ? Data->directmvB[k].y
1948 :     : pMB->mvs[k].y - Data->referencemv[k].y);
1949 : syskin 658 if (Data->qpel) {
1950 :     pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2;
1951 :     pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2;
1952 :     pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2;
1953 :     pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2;
1954 :     }
1955 :    
1956 : chl 530 if (b_mb->mode != MODE_INTER4V) {
1957 :     pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];
1958 :     pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];
1959 : syskin 658 pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0];
1960 :     pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0];
1961 : chl 530 break;
1962 :     }
1963 :     }
1964 : h 545 return skip_sad;
1965 : Isibaar 3 }
1966 : chl 96
1967 : h 545
1968 : chl 530 static __inline void
1969 :     SearchInterpolate(const uint8_t * const f_Ref,
1970 :     const uint8_t * const f_RefH,
1971 :     const uint8_t * const f_RefV,
1972 :     const uint8_t * const f_RefHV,
1973 :     const uint8_t * const b_Ref,
1974 :     const uint8_t * const b_RefH,
1975 :     const uint8_t * const b_RefV,
1976 :     const uint8_t * const b_RefHV,
1977 :     const IMAGE * const pCur,
1978 :     const int x, const int y,
1979 :     const uint32_t fcode,
1980 :     const uint32_t bcode,
1981 :     const uint32_t MotionFlags,
1982 :     const MBParam * const pParam,
1983 :     const VECTOR * const f_predMV,
1984 :     const VECTOR * const b_predMV,
1985 :     MACROBLOCK * const pMB,
1986 : h 545 int32_t * const best_sad,
1987 :     SearchData * const fData)
1988 : chl 530
1989 : chl 96 {
1990 :    
1991 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1992 : chl 530 int iDirection, i, j;
1993 : h 545 SearchData bData;
1994 : chl 96
1995 : syskin 601 *(bData.iMinSAD = fData->iMinSAD) = 4096*256;
1996 : h 545 bData.Cur = fData->Cur;
1997 :     fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth;
1998 : syskin 658 bData.currentMV = fData->currentMV + 1; bData.currentQMV = fData->currentQMV + 1;
1999 : syskin 601 bData.lambda16 = fData->lambda16;
2000 : h 545 fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
2001 : edgomez 195
2002 : h 545 bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16;
2003 :     bData.bRefH = fData->RefH = f_RefH + (x + y * iEdgedWidth) * 16;
2004 :     bData.bRefV = fData->RefV = f_RefV + (x + y * iEdgedWidth) * 16;
2005 :     bData.bRefHV = fData->RefHV = f_RefHV + (x + y * iEdgedWidth) * 16;
2006 :     bData.Ref = fData->bRef = b_Ref + (x + y * iEdgedWidth) * 16;
2007 :     bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16;
2008 :     bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;
2009 :     bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;
2010 : syskin 658 bData.RefQ = fData->RefQ;
2011 : chl 96
2012 : h 545 bData.bpredMV = fData->predMV = *f_predMV;
2013 :     fData->bpredMV = bData.predMV = *b_predMV;
2014 : edgomez 195
2015 : syskin 661 fData->currentMV[0] = fData->currentMV[2];
2016 : 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);
2017 :     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);
2018 : chl 96
2019 : h 545 if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;
2020 :     if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dy;
2021 :     if (fData->currentMV[0].y > fData->max_dy) fData->currentMV[0].y = fData->max_dx;
2022 :     if (fData->currentMV[0].y > fData->min_dy) fData->currentMV[0].y = fData->min_dy;
2023 : Isibaar 539
2024 : h 545 if (fData->currentMV[1].x > bData.max_dx) fData->currentMV[1].x = bData.max_dx;
2025 :     if (fData->currentMV[1].x < bData.min_dx) fData->currentMV[1].x = bData.min_dy;
2026 :     if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dx;
2027 :     if (fData->currentMV[1].y > bData.min_dy) fData->currentMV[1].y = bData.min_dy;
2028 : Isibaar 539
2029 : h 545 CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
2030 : chl 96
2031 : chl 530 //diamond. I wish we could use normal mainsearch functions (square, advdiamond)
2032 : chl 96
2033 : chl 530 do {
2034 :     iDirection = 255;
2035 :     // forward MV moves
2036 : h 545 i = fData->currentMV[0].x; j = fData->currentMV[0].y;
2037 : chl 96
2038 : h 545 CheckCandidateInt(i + 1, j, 0, &iDirection, fData);
2039 :     CheckCandidateInt(i, j + 1, 0, &iDirection, fData);
2040 :     CheckCandidateInt(i - 1, j, 0, &iDirection, fData);
2041 :     CheckCandidateInt(i, j - 1, 0, &iDirection, fData);
2042 : chl 96
2043 : chl 530 // backward MV moves
2044 : h 545 i = fData->currentMV[1].x; j = fData->currentMV[1].y;
2045 :     fData->currentMV[2] = fData->currentMV[0];
2046 : Isibaar 539 CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);
2047 :     CheckCandidateInt(i, j + 1, 0, &iDirection, &bData);
2048 :     CheckCandidateInt(i - 1, j, 0, &iDirection, &bData);
2049 :     CheckCandidateInt(i, j - 1, 0, &iDirection, &bData);
2050 : chl 96
2051 : chl 530 } while (!(iDirection));
2052 : edgomez 195
2053 : syskin 628 *fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode.
2054 : Isibaar 606
2055 : syskin 658 if (fData->qpel) {
2056 :     CheckCandidate = CheckCandidateInt_qpel;
2057 :     get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 0);
2058 :     get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 0);
2059 :     fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;
2060 :     fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;
2061 :     fData->currentQMV[1].x = 2 * fData->currentMV[1].x;
2062 :     fData->currentQMV[1].y = 2 * fData->currentMV[1].y;
2063 : syskin 661 // QuarterpelRefine(fData);
2064 : syskin 658 fData->currentQMV[2] = fData->currentQMV[0];
2065 : syskin 661 // QuarterpelRefine(&bData);
2066 : syskin 658 }
2067 :    
2068 : h 545 if (*fData->iMinSAD < *best_sad) {
2069 :     *best_sad = *fData->iMinSAD;
2070 :     pMB->mvs[0] = fData->currentMV[0];
2071 :     pMB->b_mvs[0] = fData->currentMV[1];
2072 : chl 530 pMB->mode = MODE_INTERPOLATE;
2073 : syskin 658 if (fData->qpel) {
2074 :     pMB->qmvs[0] = fData->currentQMV[0];
2075 :     pMB->b_qmvs[0] = fData->currentQMV[1];
2076 :     pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;
2077 :     pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;
2078 :     pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;
2079 :     pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y;
2080 :     } else {
2081 :     pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;
2082 :     pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;
2083 :     pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;
2084 :     pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;
2085 :     }
2086 : chl 530 }
2087 :     }
2088 : chl 96
2089 : chl 530 void
2090 :     MotionEstimationBVOP(MBParam * const pParam,
2091 :     FRAMEINFO * const frame,
2092 :     const int32_t time_bp,
2093 :     const int32_t time_pp,
2094 :     // forward (past) reference
2095 :     const MACROBLOCK * const f_mbs,
2096 :     const IMAGE * const f_ref,
2097 :     const IMAGE * const f_refH,
2098 :     const IMAGE * const f_refV,
2099 :     const IMAGE * const f_refHV,
2100 :     // backward (future) reference
2101 : syskin 644 const FRAMEINFO * const b_reference,
2102 : chl 530 const IMAGE * const b_ref,
2103 :     const IMAGE * const b_refH,
2104 :     const IMAGE * const b_refV,
2105 :     const IMAGE * const b_refHV)
2106 :     {
2107 :     uint32_t i, j;
2108 :     int32_t best_sad, skip_sad;
2109 :     int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0;
2110 :     static const VECTOR zeroMV={0,0};
2111 : syskin 644 const MACROBLOCK * const b_mbs = b_reference->mbs;
2112 : chl 96
2113 : chl 530 VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/
2114 : chl 96
2115 : chl 530 const int32_t TRB = time_pp - time_bp;
2116 :     const int32_t TRD = time_pp;
2117 : syskin 658 uint8_t * qimage;
2118 : chl 96
2119 : h 545 // some pre-inintialized data for the rest of the search
2120 :    
2121 :     SearchData Data;
2122 :     int32_t iMinSAD;
2123 :     VECTOR currentMV[3];
2124 : syskin 658 VECTOR currentQMV[3];
2125 : h 545 Data.iEdgedWidth = pParam->edged_width;
2126 : syskin 658 Data.currentMV = currentMV; Data.currentQMV = currentQMV;
2127 : h 545 Data.iMinSAD = &iMinSAD;
2128 : syskin 601 Data.lambda16 = lambda_vec16[frame->quant];
2129 : syskin 658 Data.qpel = pParam->m_quarterpel;
2130 : h 545
2131 : syskin 658 if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
2132 :     return; // allocate some mem for qpel interpolated blocks
2133 :     // somehow this is dirty since I think we shouldn't use malloc outside
2134 :     // encoder_create() - so please fix me!
2135 :     Data.RefQ = qimage;
2136 :    
2137 : chl 530 // note: i==horizontal, j==vertical
2138 :     for (j = 0; j < pParam->mb_height; j++) {
2139 : chl 96
2140 : chl 530 f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */
2141 : edgomez 195
2142 : chl 530 for (i = 0; i < pParam->mb_width; i++) {
2143 :     MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
2144 :     const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
2145 : edgomez 195
2146 : chl 619 /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
2147 : syskin 644 if (b_reference->coding_type != S_VOP)
2148 :     if (b_mb->mode == MODE_NOT_CODED) {
2149 :     pMB->mode = MODE_NOT_CODED;
2150 :     continue;
2151 :     }
2152 : chl 96
2153 : h 545 Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
2154 : syskin 601 pMB->quant = frame->quant;
2155 : syskin 628
2156 : chl 530 /* direct search comes first, because it (1) checks for SKIP-mode
2157 :     and (2) sets very good predictions for forward and backward search */
2158 : Isibaar 539 skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y,
2159 :     b_ref, b_refH->y, b_refV->y, b_refHV->y,
2160 : chl 530 &frame->image,
2161 :     i, j,
2162 :     frame->motion_flags,
2163 :     TRB, TRD,
2164 :     pParam,
2165 :     pMB, b_mb,
2166 : h 545 &best_sad,
2167 :     &Data);
2168 : chl 96
2169 : Isibaar 539 if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
2170 : syskin 658
2171 : chl 530 // forward search
2172 :     SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
2173 :     &frame->image, i, j,
2174 :     frame->motion_flags,
2175 : h 545 frame->fcode, pParam,
2176 : chl 530 pMB, &f_predMV, &best_sad,
2177 : h 545 MODE_FORWARD, &Data);
2178 : chl 96
2179 : chl 530 // backward search
2180 :     SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
2181 :     &frame->image, i, j,
2182 :     frame->motion_flags,
2183 : h 545 frame->bcode, pParam,
2184 : chl 530 pMB, &b_predMV, &best_sad,
2185 : h 545 MODE_BACKWARD, &Data);
2186 : chl 96
2187 : chl 530 // interpolate search comes last, because it uses data from forward and backward as prediction
2188 : syskin 661 /*
2189 : chl 530 SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
2190 :     b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
2191 :     &frame->image,
2192 :     i, j,
2193 :     frame->fcode, frame->bcode,
2194 :     frame->motion_flags,
2195 : h 545 pParam,
2196 : chl 530 &f_predMV, &b_predMV,
2197 : h 545 pMB, &best_sad,
2198 :     &Data);
2199 : syskin 661 */
2200 : chl 530 switch (pMB->mode) {
2201 :     case MODE_FORWARD:
2202 :     f_count++;
2203 : syskin 658 if (pParam->m_quarterpel) f_predMV = pMB->qmvs[0];
2204 :     else f_predMV = pMB->mvs[0];
2205 : chl 530 break;
2206 :     case MODE_BACKWARD:
2207 :     b_count++;
2208 : syskin 658 if (pParam->m_quarterpel) b_predMV = pMB->b_qmvs[0];
2209 :     else b_predMV = pMB->b_mvs[0];
2210 : chl 530 break;
2211 :     case MODE_INTERPOLATE:
2212 :     i_count++;
2213 : syskin 658 if (pParam->m_quarterpel) {
2214 :     f_predMV = pMB->qmvs[0];
2215 :     b_predMV = pMB->b_qmvs[0];
2216 :     } else {
2217 :     f_predMV = pMB->mvs[0];
2218 :     b_predMV = pMB->b_mvs[0];
2219 :     }
2220 : chl 530 break;
2221 :     case MODE_DIRECT:
2222 :     case MODE_DIRECT_NO4V:
2223 :     d_count++;
2224 :     break;
2225 :     default:
2226 :     break;
2227 : chl 96 }
2228 :     }
2229 :     }
2230 : syskin 658 free(qimage);
2231 : chl 96 }
2232 :    
2233 : chl 530 /* Hinted ME starts here */
2234 : chl 96
2235 : syskin 594 static void
2236 : syskin 628 SearchPhinted ( const IMAGE * const pRef,
2237 : chl 289 const uint8_t * const pRefH,
2238 :     const uint8_t * const pRefV,
2239 :     const uint8_t * const pRefHV,
2240 :     const IMAGE * const pCur,
2241 :     const int x,
2242 :     const int y,
2243 :     const uint32_t MotionFlags,
2244 :     const uint32_t iQuant,
2245 :     const MBParam * const pParam,
2246 :     const MACROBLOCK * const pMBs,
2247 : chl 530 int inter4v,
2248 : h 545 MACROBLOCK * const pMB,
2249 : edgomez 568 SearchData * const Data)
2250 : chl 289 {
2251 : chl 530
2252 : edgomez 568 int i, t;
2253 : chl 530 MainSearchFunc * MainSearchPtr;
2254 : suxen_drol 118
2255 : h 545 Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
2256 : syskin 628 Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
2257 : h 545 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2258 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
2259 : suxen_drol 118
2260 : syskin 628 Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
2261 :     Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
2262 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
2263 : Isibaar 600
2264 : syskin 628 Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;
2265 :     Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
2266 :     Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
2267 :     Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
2268 :     Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
2269 :     Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
2270 :    
2271 : chl 530 if (!(MotionFlags & PMV_HALFPEL16)) {
2272 : h 545 Data->min_dx = EVEN(Data->min_dx);
2273 :     Data->max_dx = EVEN(Data->max_dx);
2274 :     Data->min_dy = EVEN(Data->min_dy);
2275 :     Data->max_dy = EVEN(Data->max_dy);
2276 : chl 530 }
2277 : chl 289
2278 : h 545 for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
2279 : chl 289
2280 : chl 530 if (pMB->dquant != NO_CHANGE) inter4v = 0;
2281 : chl 289
2282 : syskin 628 if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16;
2283 :     else CheckCandidate = CheckCandidate16no4v;
2284 : chl 289
2285 : chl 530 pMB->mvs[0].x = EVEN(pMB->mvs[0].x);
2286 :     pMB->mvs[0].y = EVEN(pMB->mvs[0].y);
2287 : h 545 if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed
2288 :     if (pMB->mvs[0].x < Data->min_dx) pMB->mvs[0].x = Data->min_dx;
2289 :     if (pMB->mvs[0].y > Data->max_dy) pMB->mvs[0].y = Data->max_dy;
2290 :     if (pMB->mvs[0].y < Data->min_dy) pMB->mvs[0].y = Data->min_dy;
2291 : chl 370
2292 : edgomez 568 (*CheckCandidate)(pMB->mvs[0].x, pMB->mvs[0].y, 0, &t, Data);
2293 : chl 289
2294 : chl 530 if (pMB->mode == MODE_INTER4V)
2295 :     for (i = 1; i < 4; i++) { // all four vectors will be used as four predictions for 16x16 search
2296 :     pMB->mvs[i].x = EVEN(pMB->mvs[i].x);
2297 :     pMB->mvs[i].y = EVEN(pMB->mvs[i].y);
2298 :     if (!(make_mask(pMB->mvs, i)))
2299 : edgomez 568 (*CheckCandidate)(pMB->mvs[i].x, pMB->mvs[i].y, 0, &t, Data);
2300 : chl 289 }
2301 :    
2302 :     if (MotionFlags & PMV_USESQUARES16)
2303 : chl 530 MainSearchPtr = SquareSearch;
2304 : chl 289 else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
2305 : chl 530 MainSearchPtr = AdvDiamondSearch;
2306 :     else MainSearchPtr = DiamondSearch;
2307 : chl 289
2308 : h 545 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
2309 : chl 289
2310 : h 545 if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);
2311 : chl 289
2312 : syskin 594 for(i = 0; i < 5; i++) {
2313 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
2314 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
2315 :     }
2316 : chl 289
2317 : syskin 594 if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
2318 : Isibaar 606 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2319 :     pParam->width, pParam->height, Data->iFcode, 0);
2320 :     CheckCandidate = CheckCandidate16_qpel;
2321 : syskin 594 QuarterpelRefine(Data);
2322 :     }
2323 :    
2324 :     if (inter4v) {
2325 :     SearchData Data8;
2326 :     Data8.iFcode = Data->iFcode;
2327 : Isibaar 606 Data8.lambda8 = Data->lambda8;
2328 : syskin 594 Data8.iEdgedWidth = Data->iEdgedWidth;
2329 : Isibaar 606 Data8.RefQ = Data->RefQ;
2330 : syskin 628 Data8.qpel = Data->qpel;
2331 :     Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
2332 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
2333 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
2334 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
2335 :    
2336 :     if (Data->chroma) {
2337 :     int sum, dx, dy;
2338 :    
2339 :     if(pParam->m_quarterpel)
2340 : syskin 658 sum = (pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2);
2341 : syskin 628 else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
2342 :     dy = (sum ? SIGN(sum) *
2343 :     (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0);
2344 :    
2345 :     if(pParam->m_quarterpel)
2346 : syskin 658 sum = (pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2);
2347 : syskin 628 else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
2348 :     dx = (sum ? SIGN(sum) *
2349 :     (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0);
2350 :     Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
2351 :     }
2352 : syskin 594 }
2353 :    
2354 : chl 530 if (!(inter4v) ||
2355 : h 545 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] +
2356 :     Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
2357 : chl 530 // INTER MODE
2358 :     pMB->mode = MODE_INTER;
2359 : Isibaar 539 pMB->mvs[0] = pMB->mvs[1]
2360 : h 545 = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
2361 : chl 289
2362 : Isibaar 606 pMB->qmvs[0] = pMB->qmvs[1]
2363 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
2364 :    
2365 : chl 530 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
2366 : h 545 pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
2367 : chl 289
2368 : Isibaar 606 if(pParam->m_quarterpel) {
2369 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;
2370 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;
2371 : syskin 628 } else {
2372 : Isibaar 606 pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
2373 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
2374 :     }
2375 : chl 530 } else {
2376 : syskin 628 // INTER4V MODE; all other things are already set in Search8
2377 : chl 530 pMB->mode = MODE_INTER4V;
2378 : h 545 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3]
2379 :     + Data->iMinSAD[4] + IMV16X16 * iQuant;
2380 : chl 289 }
2381 :    
2382 :     }
2383 :    
2384 : edgomez 195 void
2385 : chl 530 MotionEstimationHinted( MBParam * const pParam,
2386 :     FRAMEINFO * const current,
2387 :     FRAMEINFO * const reference,
2388 :     const IMAGE * const pRefH,
2389 :     const IMAGE * const pRefV,
2390 :     const IMAGE * const pRefHV)
2391 : suxen_drol 118 {
2392 : chl 530 MACROBLOCK *const pMBs = current->mbs;
2393 :     const IMAGE *const pCurrent = &current->image;
2394 :     const IMAGE *const pRef = &reference->image;
2395 : suxen_drol 118
2396 : chl 530 uint32_t x, y;
2397 : syskin 601 uint8_t * qimage;
2398 : syskin 574 int32_t temp[5], quant = current->quant;
2399 : h 545 int32_t iMinSAD[5];
2400 : Isibaar 606 VECTOR currentMV[5], currentQMV[5];
2401 : h 545 SearchData Data;
2402 :     Data.iEdgedWidth = pParam->edged_width;
2403 :     Data.currentMV = currentMV;
2404 : Isibaar 606 Data.currentQMV = currentQMV;
2405 : h 545 Data.iMinSAD = iMinSAD;
2406 :     Data.temp = temp;
2407 :     Data.iFcode = current->fcode;
2408 : syskin 594 Data.rounding = pParam->m_rounding_type;
2409 : syskin 628 Data.qpel = pParam->m_quarterpel;
2410 :     Data.chroma = current->global_flags & XVID_ME_COLOUR;
2411 : h 545
2412 : syskin 594 if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
2413 :     return; // allocate some mem for qpel interpolated blocks
2414 :     // somehow this is dirty since I think we shouldn't use malloc outside
2415 :     // encoder_create() - so please fix me!
2416 :    
2417 : syskin 601 Data.RefQ = qimage;
2418 :    
2419 : chl 530 if (sadInit) (*sadInit) ();
2420 : suxen_drol 118
2421 : chl 530 for (y = 0; y < pParam->mb_height; y++) {
2422 :     for (x = 0; x < pParam->mb_width; x++) {
2423 : chl 312
2424 : chl 530 MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
2425 : suxen_drol 118
2426 : chl 530 //intra mode is copied from the first pass. At least for the time being
2427 :     if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue;
2428 : chl 346
2429 : chl 530 if (!(current->global_flags & XVID_LUMIMASKING)) {
2430 :     pMB->dquant = NO_CHANGE;
2431 :     pMB->quant = current->quant; }
2432 : Isibaar 606 else {
2433 : syskin 574 if (pMB->dquant != NO_CHANGE) {
2434 :     quant += DQtab[pMB->dquant];
2435 :     if (quant > 31) quant = 31;
2436 :     else if (quant < 1) quant = 1;
2437 :     }
2438 : Isibaar 606 pMB->quant = quant;
2439 :     }
2440 : chl 341
2441 : syskin 628 SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
2442 : Isibaar 539 y, current->motion_flags, pMB->quant,
2443 : h 545 pParam, pMBs, current->global_flags & XVID_INTER4V, pMB,
2444 :     &Data);
2445 : chl 317
2446 : Isibaar 539 }
2447 :     }
2448 : syskin 594 free(qimage);
2449 : Isibaar 539 }
2450 : suxen_drol 118
2451 : Isibaar 539 static __inline int
2452 :     MEanalyzeMB ( const uint8_t * const pRef,
2453 :     const uint8_t * const pCur,
2454 :     const int x,
2455 :     const int y,
2456 :     const MBParam * const pParam,
2457 :     const MACROBLOCK * const pMBs,
2458 : h 545 MACROBLOCK * const pMB,
2459 :     SearchData * const Data)
2460 : Isibaar 539 {
2461 : chl 317
2462 : Isibaar 606 int i = 255, mask;
2463 : h 545 VECTOR pmv[3];
2464 : syskin 640 *(Data->iMinSAD) = MV_MAX_ERROR;
2465 : suxen_drol 118
2466 : syskin 640 //median is only used as prediction. it doesn't have to be real
2467 :     if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;
2468 :     else
2469 :     if (x == 1) //left macroblock does not have any vector now
2470 :     Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median
2471 :     else if (y == 1) // top macroblock don't have it's vector
2472 :     Data->predMV = (pMB - 1)->mvs[0]; // left instead of median
2473 :     else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median
2474 :    
2475 : h 545 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2476 : Isibaar 580 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);
2477 : chl 326
2478 : h 545 Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2479 :     Data->Ref = pRef + (x + y * pParam->edged_width) * 16;
2480 : Isibaar 539
2481 :     pmv[1].x = EVEN(pMB->mvs[0].x);
2482 :     pmv[1].y = EVEN(pMB->mvs[0].y);
2483 : syskin 640 pmv[2].x = EVEN(Data->predMV.x);
2484 :     pmv[2].y = EVEN(Data->predMV.y);
2485 :     pmv[0].x = pmv[0].y = 0;
2486 : chl 326
2487 : syskin 640 (*CheckCandidate)(0, 0, 255, &i, Data);
2488 :    
2489 :     //early skip for 0,0
2490 :     if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) {
2491 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
2492 :     pMB->mode = MODE_NOT_CODED;
2493 :     return 0;
2494 :     }
2495 :    
2496 : Isibaar 539 if (!(mask = make_mask(pmv, 1)))
2497 : syskin 640 (*CheckCandidate)(pmv[1].x, pmv[1].y, mask, &i, Data);
2498 : Isibaar 539 if (!(mask = make_mask(pmv, 2)))
2499 : syskin 640 (*CheckCandidate)(pmv[2].x, pmv[2].y, mask, &i, Data);
2500 : Isibaar 539
2501 : syskin 640 if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 4) // diamond only if needed
2502 :     DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
2503 : Isibaar 539
2504 : syskin 640 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
2505 : syskin 628 pMB->mode = MODE_INTER;
2506 : h 545 return *(Data->iMinSAD);
2507 : Isibaar 539 }
2508 :    
2509 :     #define INTRA_THRESH 1350
2510 :     #define INTER_THRESH 900
2511 :    
2512 : syskin 644
2513 : Isibaar 539 int
2514 :     MEanalysis( const IMAGE * const pRef,
2515 : syskin 644 FRAMEINFO * const Current,
2516 : Isibaar 539 MBParam * const pParam,
2517 : syskin 644 int maxIntra, //maximum number if non-I frames
2518 :     int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame
2519 :     int bCount) // number if B frames in a row
2520 : Isibaar 539 {
2521 :     uint32_t x, y, intra = 0;
2522 :     int sSAD = 0;
2523 : syskin 644 MACROBLOCK * const pMBs = Current->mbs;
2524 :     const IMAGE * const pCurrent = &Current->image;
2525 :     int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH;
2526 : h 545
2527 :     VECTOR currentMV;
2528 :     int32_t iMinSAD;
2529 :     SearchData Data;
2530 :     Data.iEdgedWidth = pParam->edged_width;
2531 :     Data.currentMV = &currentMV;
2532 :     Data.iMinSAD = &iMinSAD;
2533 : syskin 644 Data.iFcode = Current->fcode;
2534 : syskin 640 CheckCandidate = CheckCandidate16no4vI;
2535 : h 545
2536 : syskin 658 if (intraCount < 10) // we're right after an I frame
2537 :     IntraThresh += 4 * (intraCount - 10) * (intraCount - 10);
2538 : syskin 644 else
2539 :     if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec
2540 :     IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;
2541 :    
2542 :    
2543 :     InterThresh += 300 * (1 - bCount);
2544 :     if (InterThresh < 200) InterThresh = 200;
2545 :    
2546 : Isibaar 539 if (sadInit) (*sadInit) ();
2547 :    
2548 : syskin 601 for (y = 1; y < pParam->mb_height-1; y++) {
2549 :     for (x = 1; x < pParam->mb_width-1; x++) {
2550 : Isibaar 539 int sad, dev;
2551 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
2552 :    
2553 :     sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y,
2554 : h 545 pParam, pMBs, pMB, &Data);
2555 : Isibaar 539
2556 : syskin 644 if (sad > IntraThresh) {
2557 : syskin 601 dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
2558 :     pParam->edged_width);
2559 : syskin 644 if (dev + IntraThresh < sad) {
2560 :     pMB->mode = MODE_INTRA;
2561 :     if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame
2562 : Isibaar 539 }
2563 : syskin 644 }
2564 : syskin 601 sSAD += sad;
2565 : suxen_drol 118 }
2566 :     }
2567 : Isibaar 539 sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);
2568 : syskin 644 if (sSAD > InterThresh ) return 1; //P frame
2569 : Isibaar 539 emms();
2570 :     return 0; // B frame
2571 :    
2572 : suxen_drol 118 }
2573 : syskin 576
2574 :     int
2575 :     FindFcode( const MBParam * const pParam,
2576 :     const FRAMEINFO * const current)
2577 :     {
2578 :     uint32_t x, y;
2579 :     int max = 0, min = 0, i;
2580 :    
2581 :     for (y = 0; y < pParam->mb_height; y++) {
2582 :     for (x = 0; x < pParam->mb_width; x++) {
2583 :    
2584 :     MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
2585 :     for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4:1); i++) {
2586 :     if (pMB->mvs[i].x > max) max = pMB->mvs[i].x;
2587 :     if (pMB->mvs[i].y > max) max = pMB->mvs[i].y;
2588 :    
2589 :     if (pMB->mvs[i].x < min) min = pMB->mvs[i].x;
2590 :     if (pMB->mvs[i].y < min) min = pMB->mvs[i].y;
2591 :     }
2592 :     }
2593 :     }
2594 :    
2595 :     min = -min;
2596 :     max += 1;
2597 :     if (min > max) max = min;
2598 : Isibaar 606 if (pParam->m_quarterpel) max *= 2;
2599 : syskin 576
2600 :     for (i = 1; (max > 32 << (i - 1)); i++);
2601 :     return i;
2602 :     }
2603 : chl 619
2604 : syskin 628 static void
2605 :     CheckGMC(int x, int y, const int dir, int * iDirection,
2606 :     const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC,
2607 :     const MBParam * const pParam)
2608 :     {
2609 :     uint32_t mx, my, a, count = 0;
2610 : chl 619
2611 : syskin 628 for (my = 1; my < pParam->mb_height-1; my++)
2612 :     for (mx = 1; mx < pParam->mb_width-1; mx++) {
2613 :     VECTOR mv;
2614 :     const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];
2615 :     if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue;
2616 :     mv = pMB->mvs[0];
2617 :     a = ABS(mv.x - x) + ABS(mv.y - y);
2618 :     if (a < 6) count += 6 - a;
2619 :     }
2620 : chl 619
2621 : syskin 628 if (count > *bestcount) {
2622 :     *bestcount = count;
2623 :     *iDirection = dir;
2624 :     GMC->x = x; GMC->y = y;
2625 :     }
2626 :     }
2627 :    
2628 :    
2629 : chl 619 static VECTOR
2630 :     GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode)
2631 :     {
2632 :    
2633 : syskin 628 uint32_t count, bestcount = 0;
2634 : chl 619 int x, y;
2635 : syskin 628 VECTOR gmc = {0,0};
2636 : chl 619 int step, min_x, max_x, min_y, max_y;
2637 :     uint32_t mx, my;
2638 : syskin 628 int iDirection, bDirection;
2639 : chl 619
2640 :     min_x = min_y = -32<<iFcode;
2641 :     max_x = max_y = 32<<iFcode;
2642 :    
2643 : syskin 628 //step1: let's find a rough camera panning
2644 : chl 619 for (step = 32; step >= 2; step /= 2) {
2645 :     bestcount = 0;
2646 :     for (y = min_y; y <= max_y; y += step)
2647 :     for (x = min_x ; x <= max_x; x += step) {
2648 :     count = 0;
2649 :     //for all macroblocks
2650 :     for (my = 1; my < pParam->mb_height-1; my++)
2651 :     for (mx = 1; mx < pParam->mb_width-1; mx++) {
2652 :     const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];
2653 :     VECTOR mv;
2654 :    
2655 :     if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)
2656 :     continue;
2657 :    
2658 :     mv = pMB->mvs[0];
2659 :     if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step ) /* GMC translation is always halfpel-res */
2660 :     count++;
2661 :     }
2662 :     if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; }
2663 :     }
2664 :     min_x = gmc.x - step;
2665 :     max_x = gmc.x + step;
2666 :     min_y = gmc.y - step;
2667 :     max_y = gmc.y + step;
2668 :    
2669 :     }
2670 : syskin 628
2671 :     if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10)
2672 :     gmc.x = gmc.y = 0; //no camara pan, no GMC
2673 :    
2674 :     // step2: let's refine camera panning using gradiend-descent approach.
2675 :     // TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond)
2676 :     bestcount = 0;
2677 :     CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam);
2678 :     do {
2679 :     x = gmc.x; y = gmc.y;
2680 :     bDirection = iDirection; iDirection = 0;
2681 :     if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2682 :     if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2683 :     if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam);
2684 :     if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2685 :    
2686 :     } while (iDirection);
2687 :    
2688 : chl 619 if (pParam->m_quarterpel) {
2689 :     gmc.x *= 2;
2690 :     gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */
2691 :     }
2692 :    
2693 :     return gmc;
2694 :     }

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