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

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