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

Annotation of /branches/dev-api-4/xvidcore/src/motion/estimation_pvop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1273 - (view) (download)

1 : edgomez 1142 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Motion Estimation for P- and S- VOPs -
5 :     *
6 :     * Copyright(C) 2002 Christoph Lampert <gruel@web.de>
7 :     * 2002 Michael Militzer <michael@xvid.org>
8 :     * 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>
9 :     *
10 :     * This program is free software ; you can redistribute it and/or modify
11 :     * it under the terms of the GNU General Public License as published by
12 :     * the Free Software Foundation ; either version 2 of the License, or
13 :     * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
17 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 :     *
20 :     * You should have received a copy of the GNU General Public License
21 :     * along with this program ; if not, write to the Free Software
22 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : Isibaar 1273 * $Id: estimation_pvop.c,v 1.1.2.11 2003-12-18 02:02:08 Isibaar Exp $
25 : edgomez 1142 *
26 :     ****************************************************************************/
27 :    
28 :     #include <assert.h>
29 :     #include <stdio.h>
30 :     #include <stdlib.h>
31 :     #include <string.h> /* memcpy */
32 :    
33 :     #include "../encoder.h"
34 :     #include "../prediction/mbprediction.h"
35 :     #include "../global.h"
36 :     #include "../utils/timer.h"
37 :     #include "../image/interpolate8x8.h"
38 :     #include "estimation.h"
39 :     #include "motion.h"
40 :     #include "sad.h"
41 :     #include "motion_inlines.h"
42 :    
43 :     static const int xvid_me_lambda_vec8[32] =
44 :     { 0 ,(int)(1.00235 * NEIGH_TEND_8X8 + 0.5),
45 :     (int)(1.15582 + NEIGH_TEND_8X8 + 0.5), (int)(1.31976*NEIGH_TEND_8X8 + 0.5),
46 :     (int)(1.49591*NEIGH_TEND_8X8 + 0.5), (int)(1.68601*NEIGH_TEND_8X8 + 0.5),
47 :     (int)(1.89187*NEIGH_TEND_8X8 + 0.5), (int)(2.11542*NEIGH_TEND_8X8 + 0.5),
48 :     (int)(2.35878*NEIGH_TEND_8X8 + 0.5), (int)(2.62429*NEIGH_TEND_8X8 + 0.5),
49 :     (int)(2.91455*NEIGH_TEND_8X8 + 0.5), (int)(3.23253*NEIGH_TEND_8X8 + 0.5),
50 :     (int)(3.58158*NEIGH_TEND_8X8 + 0.5), (int)(3.96555*NEIGH_TEND_8X8 + 0.5),
51 :     (int)(4.38887*NEIGH_TEND_8X8 + 0.5), (int)(4.85673*NEIGH_TEND_8X8 + 0.5),
52 :     (int)(5.37519*NEIGH_TEND_8X8 + 0.5), (int)(5.95144*NEIGH_TEND_8X8 + 0.5),
53 :     (int)(6.59408*NEIGH_TEND_8X8 + 0.5), (int)(7.31349*NEIGH_TEND_8X8 + 0.5),
54 :     (int)(8.12242*NEIGH_TEND_8X8 + 0.5), (int)(9.03669*NEIGH_TEND_8X8 + 0.5),
55 :     (int)(10.0763*NEIGH_TEND_8X8 + 0.5), (int)(11.2669*NEIGH_TEND_8X8 + 0.5),
56 :     (int)(12.6426*NEIGH_TEND_8X8 + 0.5), (int)(14.2493*NEIGH_TEND_8X8 + 0.5),
57 :     (int)(16.1512*NEIGH_TEND_8X8 + 0.5), (int)(18.442*NEIGH_TEND_8X8 + 0.5),
58 :     (int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5),
59 :     (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5)
60 :     };
61 :    
62 :     static void
63 : syskin 1215 CheckCandidate16(const int x, const int y, SearchData * const data, const unsigned int Direction)
64 : edgomez 1142 {
65 :     const uint8_t * Reference;
66 :     int32_t sad; uint32_t t;
67 :    
68 :     if ( (x > data->max_dx) || (x < data->min_dx)
69 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
70 :    
71 : syskin 1163 Reference = GetReference(x, y, data);
72 : edgomez 1142
73 :     sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);
74 : syskin 1163 t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel, 0);
75 : edgomez 1142
76 :     sad += (data->lambda16 * t * sad)>>10;
77 :     data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;
78 :    
79 : syskin 1163 if (data->chroma) {
80 :     if (sad >= data->iMinSAD[0]) goto no16;
81 :     sad += xvid_me_ChromaSAD((x >> 1) + roundtab_79[x & 0x3],
82 :     (y >> 1) + roundtab_79[y & 0x3], data);
83 :     }
84 : edgomez 1142
85 :     if (sad < data->iMinSAD[0]) {
86 :     data->iMinSAD[0] = sad;
87 : syskin 1163 data->currentMV[0].x = x; data->currentMV[0].y = y;
88 : syskin 1215 data->dir = Direction;
89 : edgomez 1142 }
90 :    
91 : syskin 1163 no16:
92 : edgomez 1142 if (data->temp[0] < data->iMinSAD[1]) {
93 : syskin 1163 data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
94 : edgomez 1142 if (data->temp[1] < data->iMinSAD[2]) {
95 : syskin 1163 data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
96 : edgomez 1142 if (data->temp[2] < data->iMinSAD[3]) {
97 : syskin 1163 data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
98 : edgomez 1142 if (data->temp[3] < data->iMinSAD[4]) {
99 : syskin 1163 data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
100 : edgomez 1142 }
101 :    
102 :     static void
103 : syskin 1215 CheckCandidate16_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction)
104 : edgomez 1142 {
105 :     const uint8_t *Reference;
106 :     int32_t sad; uint32_t t;
107 :    
108 :     if ( (x > data->max_dx) || (x < data->min_dx)
109 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
110 :    
111 : syskin 1163 Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);
112 : edgomez 1142
113 :     sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);
114 : syskin 1163 t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 0);
115 : edgomez 1142
116 :     sad += (data->lambda16 * t * sad)>>10;
117 :     data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;
118 :    
119 : syskin 1215 if (data->chroma && (sad < data->iMinSAD[0] || sad < data->iMinSAD2) )
120 : syskin 1163 sad += xvid_me_ChromaSAD(((x/2) >> 1) + roundtab_79[(x/2) & 0x3],
121 :     ((y/2) >> 1) + roundtab_79[(y/2) & 0x3], data);
122 : edgomez 1142
123 :     if (data->temp[0] < data->iMinSAD[1]) {
124 : syskin 1163 data->iMinSAD[1] = data->temp[0]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; }
125 : edgomez 1142 if (data->temp[1] < data->iMinSAD[2]) {
126 : syskin 1163 data->iMinSAD[2] = data->temp[1]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; }
127 : edgomez 1142 if (data->temp[2] < data->iMinSAD[3]) {
128 : syskin 1163 data->iMinSAD[3] = data->temp[2]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; }
129 : edgomez 1142 if (data->temp[3] < data->iMinSAD[4]) {
130 : syskin 1163 data->iMinSAD[4] = data->temp[3]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; }
131 : edgomez 1142
132 :     if (sad < data->iMinSAD[0]) {
133 : syskin 1215 data->iMinSAD2 = *(data->iMinSAD);
134 :     data->currentQMV2.x = data->currentQMV->x;
135 :     data->currentQMV2.y = data->currentQMV->y;
136 : edgomez 1142
137 :     data->iMinSAD[0] = sad;
138 : syskin 1163 data->currentQMV[0].x = x; data->currentQMV[0].y = y;
139 : syskin 1215 } else if (sad < data->iMinSAD2) {
140 :     data->iMinSAD2 = sad;
141 :     data->currentQMV2.x = x; data->currentQMV2.y = y;
142 : edgomez 1142 }
143 :     }
144 :    
145 :     static void
146 : syskin 1215 CheckCandidate8(const int x, const int y, SearchData * const data, const unsigned int Direction)
147 : edgomez 1142 {
148 :     int32_t sad; uint32_t t;
149 :     const uint8_t * Reference;
150 :     VECTOR * current;
151 :    
152 :     if ( (x > data->max_dx) || (x < data->min_dx)
153 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
154 :    
155 :     if (!data->qpel_precision) {
156 :     Reference = GetReference(x, y, data);
157 :     current = data->currentMV;
158 :     } else { /* x and y are in 1/4 precision */
159 :     Reference = xvid_me_interpolate8x8qpel(x, y, 0, 0, data);
160 :     current = data->currentQMV;
161 :     }
162 :    
163 :     sad = sad8(data->Cur, Reference, data->iEdgedWidth);
164 :     t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
165 :    
166 :     sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10;
167 :    
168 :     if (sad < *(data->iMinSAD)) {
169 :     *(data->iMinSAD) = sad;
170 :     current->x = x; current->y = y;
171 : syskin 1215 data->dir = Direction;
172 : edgomez 1142 }
173 :     }
174 :    
175 :     static void
176 : syskin 1215 CheckCandidate32(const int x, const int y, SearchData * const data, const unsigned int Direction)
177 : edgomez 1142 {
178 :     uint32_t t;
179 :     const uint8_t * Reference;
180 :     int sad;
181 :    
182 :     if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || /* non-zero even value */
183 :     (x > data->max_dx) || (x < data->min_dx)
184 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
185 :    
186 :     Reference = GetReference(x, y, data);
187 :     t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1);
188 :    
189 :     sad = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp);
190 :    
191 :     sad += (data->lambda16 * t * sad) >> 10;
192 :     data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;
193 :    
194 :     if (sad < data->iMinSAD[0]) {
195 :     data->iMinSAD[0] = sad;
196 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
197 : syskin 1215 data->dir = Direction;
198 : edgomez 1142 }
199 :    
200 :     if (data->temp[0] < data->iMinSAD[1]) {
201 :     data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
202 :     if (data->temp[1] < data->iMinSAD[2]) {
203 :     data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
204 :     if (data->temp[2] < data->iMinSAD[3]) {
205 :     data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
206 :     if (data->temp[3] < data->iMinSAD[4]) {
207 :     data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
208 :     }
209 :    
210 :     int
211 :     xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference,
212 :     const int x, const int y,
213 :     const uint32_t stride, const uint32_t iQuant, int rrv)
214 :     {
215 :     int offset = (x + y*stride)*8;
216 :     if(!rrv) {
217 :     uint32_t sadC = sad8(current->u + offset,
218 :     reference->u + offset, stride);
219 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
220 :     sadC += sad8(current->v + offset,
221 :     reference->v + offset, stride);
222 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
223 :     return 1;
224 :    
225 :     } else {
226 :     uint32_t sadC = sad16(current->u + 2*offset,
227 :     reference->u + 2*offset, stride, 256*4096);
228 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
229 :     sadC += sad16(current->v + 2*offset,
230 :     reference->v + 2*offset, stride, 256*4096);
231 :     if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
232 :     return 1;
233 :     }
234 :     }
235 :    
236 :     /*
237 :     * pmv are filled with:
238 :     * [0]: Median (or whatever is correct in a special case)
239 :     * [1]: left neighbour
240 :     * [2]: top neighbour
241 :     * [3]: topright neighbour
242 :     * psad are filled with:
243 :     * [0]: minimum of [1] to [3]
244 :     * [1]: left neighbour's SAD (NB:[1] to [3] are actually not needed)
245 :     * [2]: top neighbour's SAD
246 :     * [3]: topright neighbour's SAD
247 :     */
248 :    
249 : syskin 1163 static __inline void
250 : edgomez 1142 get_pmvdata2(const MACROBLOCK * const mbs,
251 :     const int mb_width,
252 :     const int bound,
253 :     const int x,
254 :     const int y,
255 :     VECTOR * const pmv,
256 :     int32_t * const psad)
257 :     {
258 :     int lx, ly, lz; /* left */
259 :     int tx, ty, tz; /* top */
260 :     int rx, ry, rz; /* top-right */
261 :     int lpos, tpos, rpos;
262 :     int num_cand = 0, last_cand = 1;
263 :    
264 : syskin 1163 lx = x - 1; ly = y; lz = 1;
265 :     tx = x; ty = y - 1; tz = 2;
266 :     rx = x + 1; ry = y - 1; rz = 2;
267 : edgomez 1142
268 :     lpos = lx + ly * mb_width;
269 :     rpos = rx + ry * mb_width;
270 :     tpos = tx + ty * mb_width;
271 :    
272 :     if (lpos >= bound && lx >= 0) {
273 :     num_cand++;
274 :     last_cand = 1;
275 :     pmv[1] = mbs[lpos].mvs[lz];
276 :     psad[1] = mbs[lpos].sad8[lz];
277 :     } else {
278 :     pmv[1] = zeroMV;
279 :     psad[1] = MV_MAX_ERROR;
280 :     }
281 :    
282 :     if (tpos >= bound) {
283 :     num_cand++;
284 :     last_cand = 2;
285 :     pmv[2]= mbs[tpos].mvs[tz];
286 :     psad[2] = mbs[tpos].sad8[tz];
287 :     } else {
288 :     pmv[2] = zeroMV;
289 :     psad[2] = MV_MAX_ERROR;
290 :     }
291 :    
292 :     if (rpos >= bound && rx < mb_width) {
293 :     num_cand++;
294 :     last_cand = 3;
295 :     pmv[3] = mbs[rpos].mvs[rz];
296 :     psad[3] = mbs[rpos].sad8[rz];
297 :     } else {
298 :     pmv[3] = zeroMV;
299 :     psad[3] = MV_MAX_ERROR;
300 :     }
301 :    
302 :     /* original pmvdata() compatibility hack */
303 : syskin 1163 if (x == 0 && y == 0) {
304 : edgomez 1142 pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
305 :     psad[0] = 0;
306 :     psad[1] = psad[2] = psad[3] = MV_MAX_ERROR;
307 : syskin 1185 return;
308 : edgomez 1142 }
309 :    
310 :     /* if only one valid candidate preictor, the invalid candiates are set to the canidate */
311 :     if (num_cand == 1) {
312 :     pmv[0] = pmv[last_cand];
313 :     psad[0] = psad[last_cand];
314 : syskin 1185 return;
315 : edgomez 1142 }
316 :    
317 :     if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {
318 :     pmv[0] = pmv[1];
319 :     psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
320 : syskin 1185 return;
321 : edgomez 1142 }
322 :    
323 :     /* set median, minimum */
324 :    
325 :     pmv[0].x =
326 :     MIN(MAX(pmv[1].x, pmv[2].x),
327 :     MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
328 :     pmv[0].y =
329 :     MIN(MAX(pmv[1].y, pmv[2].y),
330 :     MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
331 :    
332 :     psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
333 :    
334 :     }
335 :    
336 :    
337 :     static void
338 :     ModeDecision_SAD(SearchData * const Data,
339 :     MACROBLOCK * const pMB,
340 :     const MACROBLOCK * const pMBs,
341 :     const int x, const int y,
342 :     const MBParam * const pParam,
343 :     const uint32_t MotionFlags,
344 :     const uint32_t VopFlags,
345 :     const uint32_t VolFlags,
346 :     const IMAGE * const pCurrent,
347 :     const IMAGE * const pRef,
348 :     const IMAGE * const vGMC,
349 :     const int coding_type)
350 :     {
351 :     int mode = MODE_INTER;
352 :     int mcsel = 0;
353 :     int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
354 :     const uint32_t iQuant = pMB->quant;
355 :    
356 :     const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0);
357 :    
358 :     int sad;
359 :     int InterBias = MV16_INTER_BIAS;
360 :    
361 :     pMB->mcsel = 0;
362 :    
363 :     if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
364 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {
365 :     mode = MODE_INTER;
366 :     sad = Data->iMinSAD[0];
367 :     } else {
368 :     mode = MODE_INTER4V;
369 :     sad = Data->iMinSAD[1] + Data->iMinSAD[2] +
370 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant;
371 :     Data->iMinSAD[0] = sad;
372 :     }
373 :    
374 :     /* final skip decision, a.k.a. "the vector you found, really that good?" */
375 :     if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))
376 :     if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)
377 :     if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {
378 :     mode = MODE_NOT_CODED;
379 :     sad = 0;
380 :     }
381 :    
382 :     /* mcsel */
383 :     if (coding_type == S_VOP) {
384 :    
385 :     int32_t iSAD = sad16(Data->Cur,
386 :     vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536);
387 :    
388 :     if (Data->chroma) {
389 :     iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
390 :     iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
391 :     }
392 :    
393 :     if (iSAD <= sad) { /* mode decision GMC */
394 :     mode = MODE_INTER;
395 :     mcsel = 1;
396 :     sad = iSAD;
397 :     }
398 :     }
399 :    
400 :     /* intra decision */
401 :    
402 : syskin 1237 if (iQuant > 10) InterBias += 60 * (iQuant - 10); /* to make high quants work */
403 : edgomez 1142 if (y != 0)
404 :     if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
405 :     if (x != 0)
406 :     if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
407 :    
408 :     if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */
409 :     if (Data->rrv) InterBias *= 4;
410 :    
411 :     if (InterBias < sad) {
412 :     int32_t deviation;
413 :     if (!Data->rrv)
414 :     deviation = dev16(Data->Cur, Data->iEdgedWidth);
415 :     else
416 :     deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */
417 :     dev16(Data->Cur+16, Data->iEdgedWidth) +
418 :     dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +
419 :     dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);
420 :    
421 :     if (deviation < (sad - InterBias)) mode = MODE_INTRA;
422 :     }
423 :    
424 :     pMB->cbp = 63;
425 :     pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
426 :    
427 :     if (Data->rrv) {
428 :     Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
429 :     Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
430 :     }
431 :    
432 :     if (mode == MODE_INTER && mcsel == 0) {
433 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
434 :    
435 :     if(Data->qpel) {
436 :     pMB->qmvs[0] = pMB->qmvs[1]
437 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
438 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
439 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
440 :     } else {
441 :     pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
442 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
443 :     }
444 :    
445 : edgomez 1160 } else if (mode == MODE_INTER ) { /* but mcsel == 1 */
446 : edgomez 1142
447 :     pMB->mcsel = 1;
448 :     if (Data->qpel) {
449 :     pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
450 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
451 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
452 :     } else
453 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
454 :    
455 :     } else
456 :     if (mode == MODE_INTER4V) ; /* anything here? */
457 :     else /* INTRA, NOT_CODED */
458 :     ZeroMacroblockP(pMB, 0);
459 :    
460 :     pMB->mode = mode;
461 :     }
462 :    
463 :     static __inline void
464 :     PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
465 :     int iHcount, const MACROBLOCK * const prevMB, int rrv)
466 :     {
467 :     /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */
468 :     if (rrv) { iWcount /= 2; iHcount /= 2; }
469 :    
470 :     if ( (y != 0) && (x < (iWcount-1)) ) { /* [5] top-right neighbour */
471 :     pmv[5].x = EVEN(pmv[3].x);
472 :     pmv[5].y = EVEN(pmv[3].y);
473 :     } else pmv[5].x = pmv[5].y = 0;
474 :    
475 :     if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }/* pmv[3] is left neighbour */
476 :     else pmv[3].x = pmv[3].y = 0;
477 :    
478 :     if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }/* [4] top neighbour */
479 :     else pmv[4].x = pmv[4].y = 0;
480 :    
481 :     /* [1] median prediction */
482 :     pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
483 :    
484 :     pmv[0].x = pmv[0].y = 0; /* [0] is zero; not used in the loop (checked before) but needed here for make_mask */
485 :    
486 :     pmv[2].x = EVEN(prevMB->mvs[0].x); /* [2] is last frame */
487 :     pmv[2].y = EVEN(prevMB->mvs[0].y);
488 :    
489 :     if ((x < iWcount-1) && (y < iHcount-1)) {
490 :     pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */
491 :     pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
492 :     } else pmv[6].x = pmv[6].y = 0;
493 :    
494 :     if (rrv) {
495 :     int i;
496 :     for (i = 0; i < 7; i++) {
497 :     pmv[i].x = RRV_MV_SCALEUP(pmv[i].x);
498 :     pmv[i].y = RRV_MV_SCALEUP(pmv[i].y);
499 :     }
500 :     }
501 :     }
502 :    
503 :     static void
504 : syskin 1215 Search8(SearchData * const OldData,
505 : edgomez 1142 const int x, const int y,
506 :     const uint32_t MotionFlags,
507 :     const MBParam * const pParam,
508 :     MACROBLOCK * const pMB,
509 :     const MACROBLOCK * const pMBs,
510 :     const int block,
511 :     SearchData * const Data)
512 :     {
513 :     int i = 0;
514 :     CheckFunc * CheckCandidate;
515 : syskin 1215 *Data->iMinSAD = *(OldData->iMinSAD + 1 + block);
516 :     *Data->currentMV = *(OldData->currentMV + 1 + block);
517 :     *Data->currentQMV = *(OldData->currentQMV + 1 + block);
518 : edgomez 1142
519 :     if(Data->qpel) {
520 :     Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);
521 :     if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y,
522 :     Data->predMV, Data->iFcode, 0, 0);
523 :     } else {
524 :     Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);
525 :     if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y,
526 :     Data->predMV, Data->iFcode, 0, Data->rrv);
527 :     }
528 :    
529 :     *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;
530 :    
531 :     if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
532 :    
533 :     if (Data->rrv) i = 16; else i = 8;
534 :    
535 :     Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
536 :     Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
537 :     Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
538 :     Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
539 :    
540 :     Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));
541 :     Data->qpel_precision = 0;
542 :    
543 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
544 :     pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
545 :    
546 :     if (!Data->rrv) CheckCandidate = CheckCandidate8;
547 :     else CheckCandidate = CheckCandidate16no4v;
548 :    
549 :     if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) {
550 :     int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */
551 :    
552 :     MainSearchFunc *MainSearchPtr;
553 :     if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch;
554 :     else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;
555 :     else MainSearchPtr = xvid_me_DiamondSearch;
556 :    
557 :     MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate);
558 :    
559 :     if(*(Data->iMinSAD) < temp_sad) {
560 :     Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */
561 :     Data->currentQMV->y = 2 * Data->currentMV->y;
562 :     }
563 :     }
564 :    
565 :     if (MotionFlags & XVID_ME_HALFPELREFINE8) {
566 :     int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */
567 :    
568 :     xvid_me_SubpelRefine(Data, CheckCandidate); /* perform halfpel refine of current best vector */
569 :    
570 :     if(*(Data->iMinSAD) < temp_sad) { /* we have found a better match */
571 :     Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */
572 :     Data->currentQMV->y = 2 * Data->currentMV->y;
573 :     }
574 :     }
575 :    
576 :     if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {
577 :     Data->qpel_precision = 1;
578 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
579 :     pParam->width, pParam->height, Data->iFcode, 2, 0);
580 :     xvid_me_SubpelRefine(Data, CheckCandidate);
581 :     }
582 :     }
583 :    
584 :     if (Data->rrv) {
585 :     Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x);
586 :     Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y);
587 :     }
588 :    
589 :     if(Data->qpel) {
590 :     pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;
591 :     pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;
592 :     pMB->qmvs[block] = *Data->currentQMV;
593 :     } else {
594 :     pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
595 :     pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
596 :     }
597 :    
598 : syskin 1215 *(OldData->iMinSAD + 1 + block) = *Data->iMinSAD;
599 :     *(OldData->currentMV + 1 + block) = *Data->currentMV;
600 :     *(OldData->currentQMV + 1 + block) = *Data->currentQMV;
601 :    
602 : edgomez 1142 pMB->mvs[block] = *Data->currentMV;
603 :     pMB->sad8[block] = 4 * *Data->iMinSAD;
604 :     }
605 :    
606 :    
607 :    
608 :     static void
609 :     SearchP(const IMAGE * const pRef,
610 :     const uint8_t * const pRefH,
611 :     const uint8_t * const pRefV,
612 :     const uint8_t * const pRefHV,
613 :     const IMAGE * const pCur,
614 :     const int x,
615 :     const int y,
616 :     const uint32_t MotionFlags,
617 :     const uint32_t VopFlags,
618 :     SearchData * const Data,
619 :     const MBParam * const pParam,
620 :     const MACROBLOCK * const pMBs,
621 :     const MACROBLOCK * const prevMBs,
622 :     MACROBLOCK * const pMB)
623 :     {
624 :    
625 :     int i, threshA;
626 :     VECTOR pmv[7];
627 :     int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
628 :     CheckFunc * CheckCandidate;
629 :    
630 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
631 :     pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
632 :    
633 : syskin 1163 get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp);
634 : edgomez 1142
635 : syskin 1215 Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */
636 : edgomez 1142 i = Data->rrv ? 2 : 1;
637 :     Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;
638 :     Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
639 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
640 :    
641 :     Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;
642 :     Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i;
643 :     Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i;
644 :     Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;
645 :     Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
646 :     Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
647 :    
648 :     Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];
649 :     Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];
650 :     Data->qpel_precision = 0;
651 : syskin 1215 Data->dir = 0;
652 : edgomez 1142
653 :     memset(Data->currentMV, 0, 5*sizeof(VECTOR));
654 :    
655 :     if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
656 :     else Data->predMV = pmv[0];
657 :    
658 :     i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0);
659 :     Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10);
660 :     Data->iMinSAD[1] = pMB->sad8[0] + ((Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS)) >> 10);
661 :     Data->iMinSAD[2] = pMB->sad8[1];
662 :     Data->iMinSAD[3] = pMB->sad8[2];
663 :     Data->iMinSAD[4] = pMB->sad8[3];
664 :    
665 :     if ((!(VopFlags & XVID_VOP_MODEDECISION_RD)) && (x | y)) {
666 :     threshA = Data->temp[0]; /* that's where we keep this SAD atm */
667 :     if (threshA < 512) threshA = 512;
668 :     else if (threshA > 1024) threshA = 1024;
669 :     } else
670 :     threshA = 512;
671 :    
672 :     PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
673 :     prevMBs + x + y * pParam->mb_width, Data->rrv);
674 :    
675 :     if (!Data->rrv) {
676 :     if (inter4v) CheckCandidate = CheckCandidate16;
677 :     else CheckCandidate = CheckCandidate16no4v; /* for extra speed */
678 :     } else CheckCandidate = CheckCandidate32;
679 :    
680 :     /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/
681 :    
682 :     for (i = 1; i < 7; i++)
683 :     if (!vector_repeats(pmv, i)) {
684 :     CheckCandidate(pmv[i].x, pmv[i].y, Data, i);
685 :     if (Data->iMinSAD[0] <= threshA) { i++; break; }
686 :     }
687 :    
688 :     if ((Data->iMinSAD[0] <= threshA) ||
689 :     (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
690 :     (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16)))
691 :     inter4v = 0;
692 :     else {
693 :    
694 :     MainSearchFunc * MainSearchPtr;
695 : syskin 1215 int mask = make_mask(pmv, i, Data->dir); /* all vectors pmv[0..i-1] have been checked */
696 : edgomez 1142
697 :     if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
698 :     else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
699 :     else MainSearchPtr = xvid_me_DiamondSearch;
700 :    
701 :     MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate);
702 :    
703 :     /* extended search, diamond starting in 0,0 and in prediction.
704 :     note that this search is/might be done in halfpel positions,
705 :     which makes it more different than the diamond above */
706 :    
707 :     if (MotionFlags & XVID_ME_EXTSEARCH16) {
708 :     int32_t bSAD;
709 :     VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
710 : syskin 1163 if (Data->qpel) {
711 :     startMV.x /= 2;
712 :     startMV.y /= 2;
713 :     } else if (Data->rrv) {
714 : edgomez 1142 startMV.x = RRV_MV_SCALEUP(startMV.x);
715 :     startMV.y = RRV_MV_SCALEUP(startMV.y);
716 :     }
717 :     if (!(MVequal(startMV, backupMV))) {
718 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
719 :    
720 :     CheckCandidate(startMV.x, startMV.y, Data, 255);
721 : syskin 1237 xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
722 : edgomez 1142 if (bSAD < Data->iMinSAD[0]) {
723 :     Data->currentMV[0] = backupMV;
724 :     Data->iMinSAD[0] = bSAD; }
725 :     }
726 :    
727 :     backupMV = Data->currentMV[0];
728 :     startMV.x = startMV.y = 1;
729 :     if (!(MVequal(startMV, backupMV))) {
730 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
731 :    
732 :     CheckCandidate(startMV.x, startMV.y, Data, 255);
733 : syskin 1237 xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
734 : edgomez 1142 if (bSAD < Data->iMinSAD[0]) {
735 :     Data->currentMV[0] = backupMV;
736 :     Data->iMinSAD[0] = bSAD;
737 :     }
738 :     }
739 :     }
740 :     }
741 :    
742 :     if (MotionFlags & XVID_ME_HALFPELREFINE16)
743 :     xvid_me_SubpelRefine(Data, CheckCandidate);
744 :    
745 :     for(i = 0; i < 5; i++) {
746 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */
747 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
748 :     }
749 :    
750 :     if (Data->qpel) {
751 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
752 :     pParam->width, pParam->height, Data->iFcode, 2, 0);
753 :     Data->qpel_precision = 1;
754 :     if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {
755 :     if(MotionFlags & XVID_ME_FASTREFINE16)
756 : syskin 1163 SubpelRefine_Fast(Data, CheckCandidate16_qpel);
757 : edgomez 1142 else
758 : syskin 1163 xvid_me_SubpelRefine(Data, CheckCandidate16_qpel);
759 : edgomez 1142 }
760 :     }
761 :    
762 :     if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)
763 :     inter4v = 0;
764 :    
765 :     if (inter4v) {
766 :     SearchData Data8;
767 :     memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */
768 :    
769 :     Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
770 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
771 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
772 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
773 :    
774 :     if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) {
775 :     /* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */
776 :     int sumx = 0, sumy = 0;
777 :    
778 :     if (Data->qpel)
779 :     for (i = 1; i < 5; i++) {
780 :     sumx += Data->currentQMV[i].x/2;
781 :     sumy += Data->currentQMV[i].y/2;
782 :     }
783 :     else
784 :     for (i = 1; i < 5; i++) {
785 :     sumx += Data->currentMV[i].x;
786 :     sumy += Data->currentMV[i].y;
787 :     }
788 :    
789 :     Data->iMinSAD[1] += xvid_me_ChromaSAD((sumx >> 3) + roundtab_76[sumx & 0xf],
790 :     (sumy >> 3) + roundtab_76[sumy & 0xf], Data);
791 :     }
792 :     } else Data->iMinSAD[1] = 4096*256;
793 :     }
794 :    
795 :     static __inline uint32_t
796 :     MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags)
797 :     {
798 :     uint32_t Flags = MotionFlags;
799 :    
800 :     if (!(VopFlags & XVID_VOP_MODEDECISION_RD))
801 :     Flags &= ~(XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD+XVID_ME_EXTSEARCH_RD);
802 :    
803 :     if (Flags & XVID_ME_EXTSEARCH_RD)
804 :     Flags |= XVID_ME_HALFPELREFINE16_RD;
805 :    
806 :     if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8)
807 :     Flags |= XVID_ME_HALFPELREFINE8_RD;
808 :    
809 :     if (Flags & XVID_ME_HALFPELREFINE16_RD)
810 :     Flags |= XVID_ME_QUARTERPELREFINE16_RD;
811 :    
812 :     if (Flags & XVID_ME_HALFPELREFINE8_RD) {
813 :     Flags |= XVID_ME_QUARTERPELREFINE8_RD;
814 :     Flags &= ~XVID_ME_HALFPELREFINE8;
815 :     }
816 :    
817 :     if (Flags & XVID_ME_QUARTERPELREFINE8_RD)
818 :     Flags &= ~XVID_ME_QUARTERPELREFINE8;
819 :    
820 :     if (!(VolFlags & XVID_VOL_QUARTERPEL))
821 :     Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD);
822 :    
823 :     if (!(VopFlags & XVID_VOP_HALFPEL))
824 :     Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD);
825 :    
826 :     if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED))
827 :     Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);
828 :    
829 :     return Flags;
830 :     }
831 :    
832 :     bool
833 :     MotionEstimation(MBParam * const pParam,
834 :     FRAMEINFO * const current,
835 :     FRAMEINFO * const reference,
836 :     const IMAGE * const pRefH,
837 :     const IMAGE * const pRefV,
838 :     const IMAGE * const pRefHV,
839 :     const IMAGE * const pGMC,
840 :     const uint32_t iLimit)
841 :     {
842 :     MACROBLOCK *const pMBs = current->mbs;
843 :     const IMAGE *const pCurrent = &current->image;
844 :     const IMAGE *const pRef = &reference->image;
845 :    
846 :     uint32_t mb_width = pParam->mb_width;
847 :     uint32_t mb_height = pParam->mb_height;
848 :     const uint32_t iEdgedWidth = pParam->edged_width;
849 :     const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);
850 :     int stat_thresh = 0;
851 :    
852 :     uint32_t x, y;
853 :     uint32_t iIntra = 0;
854 :     int32_t sad00;
855 :     int skip_thresh = INITIAL_SKIP_THRESH * \
856 :     (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \
857 :     (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
858 :    
859 :     /* some pre-initialized thingies for SearchP */
860 :     DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
861 :     SearchData Data;
862 :     memset(&Data, 0, sizeof(SearchData));
863 :     Data.iEdgedWidth = iEdgedWidth;
864 :     Data.iFcode = current->fcode;
865 :     Data.rounding = pParam->m_rounding_type;
866 :     Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
867 :     Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
868 :     Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0;
869 :     Data.dctSpace = dct_space;
870 :     Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
871 : edgomez 1230 Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
872 : syskin 1215 Data.iMinSAD2 = 0;
873 : edgomez 1142
874 :     if ((current->vop_flags & XVID_VOP_REDUCED)) {
875 :     mb_width = (pParam->width + 31) / 32;
876 :     mb_height = (pParam->height + 31) / 32;
877 :     Data.qpel = 0;
878 :     }
879 :    
880 :     Data.RefQ = pRefV->u; /* a good place, also used in MC (for similar purpose) */
881 :     if (sadInit) (*sadInit) ();
882 :    
883 :     for (y = 0; y < mb_height; y++) {
884 :     for (x = 0; x < mb_width; x++) {
885 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
886 :     MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width];
887 :    
888 :     if (!Data.rrv) pMB->sad16 =
889 :     sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
890 :     pRef->y + (x + y * iEdgedWidth) * 16,
891 :     pParam->edged_width, pMB->sad8 );
892 :    
893 :     else pMB->sad16 =
894 :     sad32v_c(pCurrent->y + (x + y * iEdgedWidth) * 32,
895 :     pRef->y + (x + y * iEdgedWidth) * 32,
896 :     pParam->edged_width, pMB->sad8 );
897 :    
898 :     if (Data.chroma) {
899 : syskin 1215 Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,
900 : edgomez 1142 pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)
901 :     + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,
902 :     pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
903 : syskin 1215 pMB->sad16 += Data.chromaSAD;
904 : edgomez 1142 }
905 :    
906 :     sad00 = pMB->sad16;
907 :    
908 :     /* initial skip decision */
909 :     /* no early skip for GMC (global vector = skip vector is unknown!) */
910 :     if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */
911 :     if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
912 :     if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {
913 :     ZeroMacroblockP(pMB, sad00);
914 :     pMB->mode = MODE_NOT_CODED;
915 :     continue;
916 :     }
917 :     }
918 :    
919 :     if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {
920 :     if(x > 0 && y > 0 && x < pParam->mb_width) {
921 :     if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], zeroMV) &&
922 :     MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&
923 :     MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&
924 :     MVequal(prevMB->mvs[0], zeroMV)) {
925 :     stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
926 :     MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
927 :     MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
928 :     prevMB->sad16)));
929 : edgomez 1195 } else {
930 : Isibaar 1194 stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
931 : edgomez 1142 MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
932 :     MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
933 :     prevMB->sad16)));
934 : Isibaar 1194 }
935 : edgomez 1142 }
936 :     }
937 :    
938 :     /* favorize (0,0) vector for cartoons */
939 :     if ((current->vop_flags & XVID_VOP_CARTOON) &&
940 :     ((sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh))) {
941 :     ZeroMacroblockP(pMB, sad00);
942 :     continue;
943 :     }
944 :    
945 :     SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
946 :     y, MotionFlags, current->vop_flags,
947 :     &Data, pParam, pMBs, reference->mbs, pMB);
948 :    
949 :     if (current->vop_flags & XVID_VOP_MODEDECISION_RD)
950 :     xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam,
951 :     MotionFlags, current->vop_flags, current->vol_flags,
952 :     pCurrent, pRef, pGMC, current->coding_type);
953 :    
954 :     else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD)
955 :     xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam,
956 :     MotionFlags, current->vop_flags, current->vol_flags,
957 :     pCurrent, pRef, pGMC, current->coding_type);
958 :     else
959 :     ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,
960 :     MotionFlags, current->vop_flags, current->vol_flags,
961 :     pCurrent, pRef, pGMC, current->coding_type);
962 :    
963 :    
964 :     if (pMB->mode == MODE_INTRA)
965 :     if (++iIntra > iLimit) return 1;
966 :     }
967 :     }
968 :     return 0;
969 :     }
970 :    
971 :    

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