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

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