[svn] / trunk / xvidcore / src / motion / estimation_pvop.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/motion/estimation_pvop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1441 - (view) (download)

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

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