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

Diff of /xvidcore/src/motion/estimation_pvop.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.3

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