[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 1444 - (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 1444 * $Id: estimation_pvop.c,v 1.6 2004-04-23 11:57:47 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 : syskin 1443 const int coding_type,
316 :     const int skip_sad)
317 : edgomez 1382 {
318 :     int mode = MODE_INTER;
319 :     int mcsel = 0;
320 :     int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
321 :     const uint32_t iQuant = pMB->quant;
322 :    
323 :     const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0);
324 :    
325 :     int sad;
326 :     int InterBias = MV16_INTER_BIAS;
327 :    
328 :     pMB->mcsel = 0;
329 :    
330 :     if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
331 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {
332 :     mode = MODE_INTER;
333 :     sad = Data->iMinSAD[0];
334 :     } else {
335 :     mode = MODE_INTER4V;
336 :     sad = Data->iMinSAD[1] + Data->iMinSAD[2] +
337 :     Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant;
338 :     Data->iMinSAD[0] = sad;
339 :     }
340 :    
341 :     /* final skip decision, a.k.a. "the vector you found, really that good?" */
342 : syskin 1443 if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP))
343 :     if ( (100*skip_sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)
344 : edgomez 1382 if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {
345 :     mode = MODE_NOT_CODED;
346 :     sad = 0;
347 :     }
348 :    
349 :     /* mcsel */
350 :     if (coding_type == S_VOP) {
351 :    
352 :     int32_t iSAD = sad16(Data->Cur,
353 :     vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536);
354 :    
355 :     if (Data->chroma) {
356 :     iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
357 :     iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
358 :     }
359 :    
360 :     if (iSAD <= sad) { /* mode decision GMC */
361 :     mode = MODE_INTER;
362 :     mcsel = 1;
363 :     sad = iSAD;
364 :     }
365 :     }
366 :    
367 :     /* intra decision */
368 :    
369 :     if (iQuant > 10) InterBias += 60 * (iQuant - 10); /* to make high quants work */
370 :     if (y != 0)
371 :     if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
372 :     if (x != 0)
373 :     if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
374 :    
375 :     if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */
376 :     if (Data->rrv) InterBias *= 4;
377 :    
378 :     if (InterBias < sad) {
379 :     int32_t deviation;
380 :     if (!Data->rrv)
381 :     deviation = dev16(Data->Cur, Data->iEdgedWidth);
382 :     else
383 :     deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */
384 :     dev16(Data->Cur+16, Data->iEdgedWidth) +
385 :     dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +
386 :     dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);
387 :    
388 :     if (deviation < (sad - InterBias)) mode = MODE_INTRA;
389 :     }
390 :    
391 :     pMB->cbp = 63;
392 :     pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
393 :    
394 :     if (Data->rrv) {
395 :     Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
396 :     Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
397 :     }
398 :    
399 :     if (mode == MODE_INTER && mcsel == 0) {
400 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
401 :    
402 :     if(Data->qpel) {
403 :     pMB->qmvs[0] = pMB->qmvs[1]
404 :     = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
405 :     pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
406 :     pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
407 :     } else {
408 :     pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
409 :     pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
410 :     }
411 :    
412 :     } else if (mode == MODE_INTER ) { /* but mcsel == 1 */
413 :    
414 :     pMB->mcsel = 1;
415 :     if (Data->qpel) {
416 :     pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
417 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
418 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
419 :     } else
420 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
421 :    
422 :     } else
423 :     if (mode == MODE_INTER4V) ; /* anything here? */
424 :     else /* INTRA, NOT_CODED */
425 :     ZeroMacroblockP(pMB, 0);
426 :    
427 :     pMB->mode = mode;
428 :     }
429 :    
430 :     static __inline void
431 :     PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
432 :     int iHcount, const MACROBLOCK * const prevMB, int rrv)
433 :     {
434 :     /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */
435 :     if (rrv) { iWcount /= 2; iHcount /= 2; }
436 :    
437 :     if ( (y != 0) && (x < (iWcount-1)) ) { /* [5] top-right neighbour */
438 :     pmv[5].x = EVEN(pmv[3].x);
439 :     pmv[5].y = EVEN(pmv[3].y);
440 :     } else pmv[5].x = pmv[5].y = 0;
441 :    
442 :     if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }/* pmv[3] is left neighbour */
443 :     else pmv[3].x = pmv[3].y = 0;
444 :    
445 :     if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }/* [4] top neighbour */
446 :     else pmv[4].x = pmv[4].y = 0;
447 :    
448 :     /* [1] median prediction */
449 :     pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
450 :    
451 :     pmv[0].x = pmv[0].y = 0; /* [0] is zero; not used in the loop (checked before) but needed here for make_mask */
452 :    
453 :     pmv[2].x = EVEN(prevMB->mvs[0].x); /* [2] is last frame */
454 :     pmv[2].y = EVEN(prevMB->mvs[0].y);
455 :    
456 :     if ((x < iWcount-1) && (y < iHcount-1)) {
457 :     pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */
458 :     pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
459 :     } else pmv[6].x = pmv[6].y = 0;
460 :    
461 :     if (rrv) {
462 :     int i;
463 :     for (i = 0; i < 7; i++) {
464 :     pmv[i].x = RRV_MV_SCALEUP(pmv[i].x);
465 :     pmv[i].y = RRV_MV_SCALEUP(pmv[i].y);
466 :     }
467 :     }
468 :     }
469 :    
470 :     static void
471 :     Search8(SearchData * const OldData,
472 :     const int x, const int y,
473 :     const uint32_t MotionFlags,
474 :     const MBParam * const pParam,
475 :     MACROBLOCK * const pMB,
476 :     const MACROBLOCK * const pMBs,
477 :     const int block,
478 :     SearchData * const Data)
479 :     {
480 :     int i = 0;
481 : syskin 1441 VECTOR vbest_q; int32_t sbest_q;
482 : edgomez 1382 CheckFunc * CheckCandidate;
483 :     *Data->iMinSAD = *(OldData->iMinSAD + 1 + block);
484 :     *Data->currentMV = *(OldData->currentMV + 1 + block);
485 :     *Data->currentQMV = *(OldData->currentQMV + 1 + block);
486 :    
487 :     if(Data->qpel) {
488 :     Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);
489 :     if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y,
490 :     Data->predMV, Data->iFcode, 0, 0);
491 :     } else {
492 :     Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);
493 :     if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y,
494 :     Data->predMV, Data->iFcode, 0, Data->rrv);
495 :     }
496 :    
497 :     *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;
498 :    
499 :     if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
500 :    
501 : syskin 1441 vbest_q = Data->currentQMV[0];
502 :     sbest_q = Data->iMinSAD[0];
503 :    
504 : edgomez 1382 if (Data->rrv) i = 16; else i = 8;
505 :    
506 :     Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
507 :     Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
508 :     Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
509 :     Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
510 :    
511 :     Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));
512 :     Data->qpel_precision = 0;
513 :    
514 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
515 :     pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
516 :    
517 :     if (!Data->rrv) CheckCandidate = CheckCandidate8;
518 :     else CheckCandidate = CheckCandidate16no4v;
519 :    
520 :     if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) {
521 :    
522 :     MainSearchFunc *MainSearchPtr;
523 :     if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch;
524 :     else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;
525 :     else MainSearchPtr = xvid_me_DiamondSearch;
526 :    
527 :     MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate);
528 :     }
529 :    
530 : syskin 1441 if(!Data->qpel) {
531 :     /* halfpel mode */
532 :     if (MotionFlags & XVID_ME_HALFPELREFINE8)
533 :     xvid_me_SubpelRefine(Data, CheckCandidate, 0); /* perform halfpel refine of current best vector */
534 :     } else {
535 :     /* qpel mode */
536 :     Data->currentQMV->x = 2*Data->currentMV->x;
537 :     Data->currentQMV->y = 2*Data->currentMV->y;
538 :    
539 :     if(MotionFlags & XVID_ME_FASTREFINE8) {
540 :     /* fast */
541 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
542 :     pParam->width, pParam->height, Data->iFcode, 2, 0);
543 :     FullRefine_Fast(Data, CheckCandidate8, 0);
544 :     } else if(MotionFlags & XVID_ME_QUARTERPELREFINE8) {
545 :     /* full */
546 :     if (MotionFlags & XVID_ME_HALFPELREFINE8) {
547 :     xvid_me_SubpelRefine(Data, CheckCandidate8, 0); /* hpel part */
548 :     Data->currentQMV->x = 2*Data->currentMV->x;
549 :     Data->currentQMV->y = 2*Data->currentMV->y;
550 :     }
551 : edgomez 1382
552 : syskin 1441 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
553 :     pParam->width, pParam->height, Data->iFcode, 2, 0);
554 :     Data->qpel_precision = 1;
555 : edgomez 1382
556 : syskin 1441 xvid_me_SubpelRefine(Data, CheckCandidate8, 0); /* qpel part */
557 : edgomez 1382 }
558 :     }
559 :    
560 : syskin 1441 if (sbest_q <= Data->iMinSAD[0]) /* we have not found a better match */
561 :     Data->currentQMV[0] = vbest_q;
562 : edgomez 1382
563 :     }
564 :    
565 :     if (Data->rrv) {
566 :     Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x);
567 :     Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y);
568 : syskin 1441 } else if(Data->qpel) {
569 : edgomez 1382 pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;
570 :     pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;
571 :     pMB->qmvs[block] = *Data->currentQMV;
572 :     } else {
573 :     pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
574 :     pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
575 :     }
576 :    
577 :     *(OldData->iMinSAD + 1 + block) = *Data->iMinSAD;
578 :     *(OldData->currentMV + 1 + block) = *Data->currentMV;
579 :     *(OldData->currentQMV + 1 + block) = *Data->currentQMV;
580 :    
581 :     pMB->mvs[block] = *Data->currentMV;
582 :     pMB->sad8[block] = 4 * *Data->iMinSAD;
583 :     }
584 :    
585 :    
586 :    
587 :     static void
588 :     SearchP(const IMAGE * const pRef,
589 :     const uint8_t * const pRefH,
590 :     const uint8_t * const pRefV,
591 :     const uint8_t * const pRefHV,
592 :     const IMAGE * const pCur,
593 :     const int x,
594 :     const int y,
595 :     const uint32_t MotionFlags,
596 :     const uint32_t VopFlags,
597 :     SearchData * const Data,
598 :     const MBParam * const pParam,
599 :     const MACROBLOCK * const pMBs,
600 :     const MACROBLOCK * const prevMBs,
601 :     MACROBLOCK * const pMB)
602 :     {
603 :    
604 :     int i, threshA;
605 :     VECTOR pmv[7];
606 :     int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
607 :     CheckFunc * CheckCandidate;
608 :    
609 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
610 :     pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
611 :    
612 :     get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp);
613 :    
614 :     Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */
615 :     i = Data->rrv ? 2 : 1;
616 :     Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;
617 :     Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
618 :     Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
619 :    
620 :     Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;
621 :     Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i;
622 :     Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i;
623 :     Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;
624 :     Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
625 :     Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
626 :    
627 :     Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];
628 :     Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];
629 :     Data->qpel_precision = 0;
630 :     Data->dir = 0;
631 :    
632 :     memset(Data->currentMV, 0, 5*sizeof(VECTOR));
633 :    
634 :     if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
635 :     else Data->predMV = pmv[0];
636 :    
637 :     i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0);
638 :     Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10);
639 :     Data->iMinSAD[1] = pMB->sad8[0] + ((Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS)) >> 10);
640 :     Data->iMinSAD[2] = pMB->sad8[1];
641 :     Data->iMinSAD[3] = pMB->sad8[2];
642 :     Data->iMinSAD[4] = pMB->sad8[3];
643 :    
644 :     if ((!(VopFlags & XVID_VOP_MODEDECISION_RD)) && (x | y)) {
645 :     threshA = Data->temp[0]; /* that's where we keep this SAD atm */
646 :     if (threshA < 512) threshA = 512;
647 :     else if (threshA > 1024) threshA = 1024;
648 :     } else
649 :     threshA = 512;
650 :    
651 :     PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
652 :     prevMBs + x + y * pParam->mb_width, Data->rrv);
653 :    
654 :     if (!Data->rrv) {
655 :     if (inter4v) CheckCandidate = CheckCandidate16;
656 :     else CheckCandidate = CheckCandidate16no4v; /* for extra speed */
657 :     } else CheckCandidate = CheckCandidate32;
658 :    
659 :     /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/
660 :    
661 :     for (i = 1; i < 7; i++)
662 :     if (!vector_repeats(pmv, i)) {
663 :     CheckCandidate(pmv[i].x, pmv[i].y, Data, i);
664 :     if (Data->iMinSAD[0] <= threshA) { i++; break; }
665 :     }
666 :    
667 :     if ((Data->iMinSAD[0] <= threshA) ||
668 :     (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
669 :     (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16)))
670 :     inter4v = 0;
671 :     else {
672 :    
673 :     MainSearchFunc * MainSearchPtr;
674 :     int mask = make_mask(pmv, i, Data->dir); /* all vectors pmv[0..i-1] have been checked */
675 :    
676 :     if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
677 :     else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
678 :     else MainSearchPtr = xvid_me_DiamondSearch;
679 :    
680 :     MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate);
681 :    
682 :     /* extended search, diamond starting in 0,0 and in prediction.
683 :     note that this search is/might be done in halfpel positions,
684 :     which makes it more different than the diamond above */
685 :    
686 :     if (MotionFlags & XVID_ME_EXTSEARCH16) {
687 :     int32_t bSAD;
688 :     VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
689 :     if (Data->qpel) {
690 :     startMV.x /= 2;
691 :     startMV.y /= 2;
692 :     } else if (Data->rrv) {
693 :     startMV.x = RRV_MV_SCALEUP(startMV.x);
694 :     startMV.y = RRV_MV_SCALEUP(startMV.y);
695 :     }
696 :     if (!(MVequal(startMV, backupMV))) {
697 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
698 :    
699 :     CheckCandidate(startMV.x, startMV.y, Data, 255);
700 :     xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
701 :     if (bSAD < Data->iMinSAD[0]) {
702 :     Data->currentMV[0] = backupMV;
703 :     Data->iMinSAD[0] = bSAD; }
704 :     }
705 :    
706 :     backupMV = Data->currentMV[0];
707 :     startMV.x = startMV.y = 1;
708 :     if (!(MVequal(startMV, backupMV))) {
709 :     bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
710 :    
711 :     CheckCandidate(startMV.x, startMV.y, Data, 255);
712 :     xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
713 :     if (bSAD < Data->iMinSAD[0]) {
714 :     Data->currentMV[0] = backupMV;
715 :     Data->iMinSAD[0] = bSAD;
716 :     }
717 :     }
718 :     }
719 :     }
720 :    
721 :    
722 : syskin 1441 if(!Data->qpel) {
723 :     /* halfpel mode */
724 :     if (MotionFlags & XVID_ME_HALFPELREFINE16)
725 :     xvid_me_SubpelRefine(Data, CheckCandidate, 0);
726 :     } else {
727 :     /* qpel mode */
728 :    
729 :     for(i = 0; i < 5; i++) {
730 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */
731 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
732 :     }
733 :     if(MotionFlags & XVID_ME_FASTREFINE16 && MotionFlags & XVID_ME_QUARTERPELREFINE16) {
734 :     /* fast */
735 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
736 :     pParam->width, pParam->height, Data->iFcode, 2, 0);
737 :     FullRefine_Fast(Data, CheckCandidate, 0);
738 :     } else {
739 :     if(MotionFlags & (XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE16_RD)) {
740 :     /* full */
741 :     if (MotionFlags & XVID_ME_HALFPELREFINE16) {
742 :     xvid_me_SubpelRefine(Data, CheckCandidate, 0); /* hpel part */
743 :     for(i = 0; i < 5; i++) {
744 :     Data->currentQMV[i].x = 2 * Data->currentMV[i].x;
745 :     Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
746 :     }
747 :     }
748 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
749 :     pParam->width, pParam->height, Data->iFcode, 2, 0);
750 :     Data->qpel_precision = 1;
751 :     if(MotionFlags & XVID_ME_QUARTERPELREFINE16)
752 :     xvid_me_SubpelRefine(Data, CheckCandidate, 0); /* qpel part */
753 :     }
754 : edgomez 1382 }
755 :     }
756 :    
757 : syskin 1441 if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30* ((MotionFlags & XVID_ME_FASTREFINE16) ? 8 : 1))
758 : edgomez 1382 inter4v = 0;
759 :    
760 :     if (inter4v) {
761 :     SearchData Data8;
762 :     memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */
763 :    
764 :     Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
765 :     Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
766 :     Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
767 :     Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
768 :    
769 :     if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) {
770 : syskin 1441 /* chroma is only used for comparison to INTER. if the comparison will be done in RD domain, it will not be used */
771 : edgomez 1382 int sumx = 0, sumy = 0;
772 :    
773 :     if (Data->qpel)
774 :     for (i = 1; i < 5; i++) {
775 :     sumx += Data->currentQMV[i].x/2;
776 :     sumy += Data->currentQMV[i].y/2;
777 :     }
778 :     else
779 :     for (i = 1; i < 5; i++) {
780 :     sumx += Data->currentMV[i].x;
781 :     sumy += Data->currentMV[i].y;
782 :     }
783 :    
784 :     Data->iMinSAD[1] += xvid_me_ChromaSAD((sumx >> 3) + roundtab_76[sumx & 0xf],
785 :     (sumy >> 3) + roundtab_76[sumy & 0xf], Data);
786 :     }
787 :     } else Data->iMinSAD[1] = 4096*256;
788 :     }
789 :    
790 :     static __inline uint32_t
791 :     MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags)
792 :     {
793 :     uint32_t Flags = MotionFlags;
794 :    
795 :     if (!(VopFlags & XVID_VOP_MODEDECISION_RD))
796 :     Flags &= ~(XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD+XVID_ME_EXTSEARCH_RD);
797 :    
798 :     if (Flags & XVID_ME_EXTSEARCH_RD)
799 :     Flags |= XVID_ME_HALFPELREFINE16_RD;
800 :    
801 :     if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8)
802 :     Flags |= XVID_ME_HALFPELREFINE8_RD;
803 :    
804 :     if (Flags & XVID_ME_HALFPELREFINE16_RD)
805 :     Flags |= XVID_ME_QUARTERPELREFINE16_RD;
806 :    
807 :     if (Flags & XVID_ME_HALFPELREFINE8_RD) {
808 :     Flags |= XVID_ME_QUARTERPELREFINE8_RD;
809 :     Flags &= ~XVID_ME_HALFPELREFINE8;
810 :     }
811 :    
812 :     if (Flags & XVID_ME_QUARTERPELREFINE8_RD)
813 :     Flags &= ~XVID_ME_QUARTERPELREFINE8;
814 :    
815 : edgomez 1423 if (Flags & XVID_ME_QUARTERPELREFINE16_RD)
816 :     Flags &= ~XVID_ME_QUARTERPELREFINE16;
817 :    
818 : edgomez 1382 if (!(VolFlags & XVID_VOL_QUARTERPEL))
819 :     Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD);
820 :    
821 :     if (!(VopFlags & XVID_VOP_HALFPEL))
822 :     Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD);
823 :    
824 :     if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED))
825 :     Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);
826 :    
827 : syskin 1441 if (Flags & XVID_ME_FASTREFINE8)
828 :     Flags &= ~XVID_ME_HALFPELREFINE8_RD;
829 :    
830 :     if (Flags & XVID_ME_FASTREFINE16)
831 :     Flags &= ~XVID_ME_HALFPELREFINE16_RD;
832 :    
833 : edgomez 1382 return Flags;
834 :     }
835 :    
836 :     bool
837 :     MotionEstimation(MBParam * const pParam,
838 :     FRAMEINFO * const current,
839 :     FRAMEINFO * const reference,
840 :     const IMAGE * const pRefH,
841 :     const IMAGE * const pRefV,
842 :     const IMAGE * const pRefHV,
843 :     const IMAGE * const pGMC,
844 :     const uint32_t iLimit)
845 :     {
846 :     MACROBLOCK *const pMBs = current->mbs;
847 :     const IMAGE *const pCurrent = &current->image;
848 :     const IMAGE *const pRef = &reference->image;
849 :    
850 :     uint32_t mb_width = pParam->mb_width;
851 :     uint32_t mb_height = pParam->mb_height;
852 :     const uint32_t iEdgedWidth = pParam->edged_width;
853 :     const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);
854 :     int stat_thresh = 0;
855 :    
856 :     uint32_t x, y;
857 :     uint32_t iIntra = 0;
858 :     int32_t sad00;
859 :     int skip_thresh = INITIAL_SKIP_THRESH * \
860 :     (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \
861 :     (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
862 :    
863 :     /* some pre-initialized thingies for SearchP */
864 :     DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
865 :     SearchData Data;
866 :     memset(&Data, 0, sizeof(SearchData));
867 :     Data.iEdgedWidth = iEdgedWidth;
868 :     Data.iFcode = current->fcode;
869 :     Data.rounding = pParam->m_rounding_type;
870 :     Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
871 :     Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
872 :     Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0;
873 :     Data.dctSpace = dct_space;
874 :     Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
875 :     Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
876 :    
877 :     if ((current->vop_flags & XVID_VOP_REDUCED)) {
878 :     mb_width = (pParam->width + 31) / 32;
879 :     mb_height = (pParam->height + 31) / 32;
880 :     Data.qpel = 0;
881 :     }
882 :    
883 :     Data.RefQ = pRefV->u; /* a good place, also used in MC (for similar purpose) */
884 :     if (sadInit) (*sadInit) ();
885 :    
886 :     for (y = 0; y < mb_height; y++) {
887 :     for (x = 0; x < mb_width; x++) {
888 :     MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
889 :     MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width];
890 :    
891 :     if (!Data.rrv) pMB->sad16 =
892 :     sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
893 :     pRef->y + (x + y * iEdgedWidth) * 16,
894 :     pParam->edged_width, pMB->sad8 );
895 :    
896 :     else pMB->sad16 =
897 :     sad32v_c(pCurrent->y + (x + y * iEdgedWidth) * 32,
898 :     pRef->y + (x + y * iEdgedWidth) * 32,
899 :     pParam->edged_width, pMB->sad8 );
900 : syskin 1443
901 :     sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3]));
902 : edgomez 1382
903 :     if (Data.chroma) {
904 :     Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,
905 :     pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)
906 :     + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,
907 :     pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
908 :     pMB->sad16 += Data.chromaSAD;
909 : syskin 1443 sad00 += Data.chromaSAD;
910 : edgomez 1382 }
911 :    
912 :     /* initial skip decision */
913 :     /* no early skip for GMC (global vector = skip vector is unknown!) */
914 :     if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */
915 : syskin 1443 if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
916 : edgomez 1382 if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {
917 :     ZeroMacroblockP(pMB, sad00);
918 :     pMB->mode = MODE_NOT_CODED;
919 :     continue;
920 :     }
921 :     }
922 :    
923 :     if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {
924 :     if(x > 0 && y > 0 && x < pParam->mb_width) {
925 :     if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], zeroMV) &&
926 :     MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&
927 :     MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&
928 :     MVequal(prevMB->mvs[0], zeroMV)) {
929 :     stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
930 :     MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
931 :     MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
932 :     prevMB->sad16)));
933 :     } else {
934 :     stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
935 :     MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
936 :     MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
937 :     prevMB->sad16)));
938 :     }
939 :     }
940 :     }
941 :    
942 :     /* favorize (0,0) vector for cartoons */
943 :     if ((current->vop_flags & XVID_VOP_CARTOON) &&
944 :     ((sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh))) {
945 :     ZeroMacroblockP(pMB, sad00);
946 :     continue;
947 :     }
948 :    
949 :     SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
950 :     y, MotionFlags, current->vop_flags,
951 :     &Data, pParam, pMBs, reference->mbs, pMB);
952 :    
953 :     if (current->vop_flags & XVID_VOP_MODEDECISION_RD)
954 :     xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam,
955 :     MotionFlags, current->vop_flags, current->vol_flags,
956 :     pCurrent, pRef, pGMC, current->coding_type);
957 :    
958 :     else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD)
959 :     xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam,
960 :     MotionFlags, current->vop_flags, current->vol_flags,
961 :     pCurrent, pRef, pGMC, current->coding_type);
962 :     else
963 :     ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,
964 :     MotionFlags, current->vop_flags, current->vol_flags,
965 : syskin 1443 pCurrent, pRef, pGMC, current->coding_type, sad00);
966 : edgomez 1382
967 :    
968 :     if (pMB->mode == MODE_INTRA)
969 :     if (++iIntra > iLimit) return 1;
970 :     }
971 :     }
972 :     return 0;
973 :     }
974 :    
975 :    

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