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

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