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

Diff of /xvidcore/src/motion/estimation_bvop.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.6, Wed Dec 3 11:51:28 2003 UTC
# Line 0  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - Motion Estimation for B-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    
29    #include <assert.h>
30    #include <stdio.h>
31    #include <stdlib.h>
32    #include <string.h>     /* memcpy */
33    
34    #include "../encoder.h"
35    #include "../global.h"
36    #include "../image/interpolate8x8.h"
37    #include "estimation.h"
38    #include "motion.h"
39    #include "sad.h"
40    #include "motion_inlines.h"
41    
42    
43    static int32_t
44    ChromaSAD2(const int fx, const int fy, const int bx, const int by,
45                            SearchData * const data)
46    {
47            int sad;
48            const uint32_t stride = data->iEdgedWidth/2;
49            uint8_t *f_refu, *f_refv, *b_refu, *b_refv;
50    
51            const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = {
52                    NULL,
53                    interpolate8x8_halfpel_v,
54                    interpolate8x8_halfpel_h,
55                    interpolate8x8_halfpel_hv
56            };
57    
58            int offset = (fx>>1) + (fy>>1)*stride;
59            int filter = ((fx & 1) << 1) | (fy & 1);
60    
61            if (filter != 0) {
62                    f_refu = data->RefQ;
63                    f_refv = data->RefQ + 8;
64                    interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding);
65                    interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding);
66            } else {
67                    f_refu = (uint8_t*)data->RefP[4] + offset;
68                    f_refv = (uint8_t*)data->RefP[5] + offset;
69            }
70    
71            offset = (bx>>1) + (by>>1)*stride;
72            filter = ((bx & 1) << 1) | (by & 1);
73    
74            if (filter != 0) {
75                    b_refu = data->RefQ + 16;
76                    b_refv = data->RefQ + 24;
77                    interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding);
78                    interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding);
79            } else {
80                    b_refu = (uint8_t*)data->b_RefP[4] + offset;
81                    b_refv = (uint8_t*)data->b_RefP[5] + offset;
82            }
83    
84            sad = sad8bi(data->CurU, b_refu, f_refu, stride);
85            sad += sad8bi(data->CurV, b_refv, f_refv, stride);
86    
87            return sad;
88    }
89    
90    static void
91    CheckCandidateInt(const int x, const int y, SearchData * const data, const unsigned int Direction)
92    {
93            int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb;
94            uint32_t t;
95    
96            const uint8_t *ReferenceF, *ReferenceB;
97            VECTOR *current;
98    
99            if ((x > data->max_dx) || (x < data->min_dx) ||
100                    (y > data->max_dy) || (y < data->min_dy))
101                    return;
102    
103            if (Direction == 1) { /* x and y mean forward vector */
104                    VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1];
105                    xb = backward.x;
106                    yb = backward.y;
107                    xf = x; yf = y;
108            } else { /* x and y mean backward vector */
109                    VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0];
110                    xf = forward.x;
111                    yf = forward.y;
112                    xb = x; yb = y;
113            }
114    
115            if (!data->qpel_precision) {
116                    ReferenceF = GetReference(xf, yf, data);
117                    ReferenceB = GetReferenceB(xb, yb, 1, data);
118                    current = data->currentMV + Direction - 1;
119                    xcf = xf; ycf = yf;
120                    xcb = xb; ycb = yb;
121            } else {
122                    ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data);
123                    current = data->currentQMV + Direction - 1;
124                    ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data);
125                    xcf = xf/2; ycf = yf/2;
126                    xcb = xb/2; ycb = yb/2;
127            }
128    
129            t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0)
130                     + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0);
131    
132            sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
133            sad += (data->lambda16 * t * sad)>>10;
134    
135            if (data->chroma && sad < *data->iMinSAD)
136                    sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3],
137                                                            (ycf >> 1) + roundtab_79[ycf & 0x3],
138                                                            (xcb >> 1) + roundtab_79[xcb & 0x3],
139                                                            (ycb >> 1) + roundtab_79[ycb & 0x3], data);
140    
141            if (sad < *(data->iMinSAD)) {
142                    *data->iMinSAD = sad;
143                    current->x = x; current->y = y;
144                    data->dir = Direction;
145            }
146    }
147    
148    static void
149    CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction)
150    {
151            int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0;
152            uint32_t k;
153            const uint8_t *ReferenceF;
154            const uint8_t *ReferenceB;
155            VECTOR mvs, b_mvs;
156    
157            if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
158    
159            for (k = 0; k < 4; k++) {
160                    mvs.x = data->directmvF[k].x + x;
161                    b_mvs.x = ((x == 0) ?
162                            data->directmvB[k].x
163                            : mvs.x - data->referencemv[k].x);
164    
165                    mvs.y = data->directmvF[k].y + y;
166                    b_mvs.y = ((y == 0) ?
167                            data->directmvB[k].y
168                            : mvs.y - data->referencemv[k].y);
169    
170                    if ((mvs.x > data->max_dx)   || (mvs.x < data->min_dx)   ||
171                            (mvs.y > data->max_dy)   || (mvs.y < data->min_dy)   ||
172                            (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) ||
173                            (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) )
174                            return;
175    
176                    if (data->qpel) {
177                            xcf += mvs.x/2; ycf += mvs.y/2;
178                            xcb += b_mvs.x/2; ycb += b_mvs.y/2;
179                    } else {
180                            xcf += mvs.x; ycf += mvs.y;
181                            xcb += b_mvs.x; ycb += b_mvs.y;
182                            mvs.x *= 2; mvs.y *= 2; /* we move to qpel precision anyway */
183                            b_mvs.x *= 2; b_mvs.y *= 2;
184                    }
185    
186                    ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
187                    ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
188    
189                    sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
190                                                    ReferenceF, ReferenceB, data->iEdgedWidth);
191                    if (sad > *(data->iMinSAD)) return;
192            }
193    
194            sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10;
195    
196            if (data->chroma && sad < *data->iMinSAD)
197                    sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
198                                                            (ycf >> 3) + roundtab_76[ycf & 0xf],
199                                                            (xcb >> 3) + roundtab_76[xcb & 0xf],
200                                                            (ycb >> 3) + roundtab_76[ycb & 0xf], data);
201    
202            if (sad < *(data->iMinSAD)) {
203                    data->iMinSAD[0] = sad;
204                    data->currentMV->x = x; data->currentMV->y = y;
205                    data->dir = Direction;
206            }
207    }
208    
209    static void
210    CheckCandidateDirectno4v(const int x, const int y, SearchData * const data, const unsigned int Direction)
211    {
212            int32_t sad, xcf, ycf, xcb, ycb;
213            const uint8_t *ReferenceF;
214            const uint8_t *ReferenceB;
215            VECTOR mvs, b_mvs;
216    
217            if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
218    
219            mvs.x = data->directmvF[0].x + x;
220            b_mvs.x = ((x == 0) ?
221                    data->directmvB[0].x
222                    : mvs.x - data->referencemv[0].x);
223    
224            mvs.y = data->directmvF[0].y + y;
225            b_mvs.y = ((y == 0) ?
226                    data->directmvB[0].y
227                    : mvs.y - data->referencemv[0].y);
228    
229            if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx)
230                    || (mvs.y > data->max_dy) || (mvs.y < data->min_dy)
231                    || (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx)
232                    || (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return;
233    
234            if (data->qpel) {
235                    xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);
236                    xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2);
237                    ReferenceF = xvid_me_interpolate16x16qpel(mvs.x, mvs.y, 0, data);
238                    ReferenceB = xvid_me_interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);
239            } else {
240                    xcf = 4*mvs.x; ycf = 4*mvs.y;
241                    xcb = 4*b_mvs.x; ycb = 4*b_mvs.y;
242                    ReferenceF = GetReference(mvs.x, mvs.y, data);
243                    ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data);
244            }
245    
246            sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
247            sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10;
248    
249            if (data->chroma && sad < *data->iMinSAD)
250                    sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
251                                                            (ycf >> 3) + roundtab_76[ycf & 0xf],
252                                                            (xcb >> 3) + roundtab_76[xcb & 0xf],
253                                                            (ycb >> 3) + roundtab_76[ycb & 0xf], data);
254    
255            if (sad < *(data->iMinSAD)) {
256                    *(data->iMinSAD) = sad;
257                    data->currentMV->x = x; data->currentMV->y = y;
258                    data->dir = Direction;
259            }
260    }
261    
262    void
263    CheckCandidate16no4v(const int x, const int y, SearchData * const data, const unsigned int Direction)
264    {
265            int32_t sad, xc, yc;
266            const uint8_t * Reference;
267            uint32_t t;
268            VECTOR * current;
269    
270            if ( (x > data->max_dx) || ( x < data->min_dx)
271                    || (y > data->max_dy) || (y < data->min_dy) ) return;
272    
273            if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */
274    
275            if (data->qpel_precision) { /* x and y are in 1/4 precision */
276                    Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);
277                    current = data->currentQMV;
278                    xc = x/2; yc = y/2;
279            } else {
280                    Reference = GetReference(x, y, data);
281                    current = data->currentMV;
282                    xc = x; yc = y;
283            }
284            t = d_mv_bits(x, y, data->predMV, data->iFcode,
285                                            data->qpel^data->qpel_precision, data->rrv);
286    
287            sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
288            sad += (data->lambda16 * t * sad)>>10;
289    
290            if (data->chroma && sad < *data->iMinSAD)
291                    sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
292                                                                    (yc >> 1) + roundtab_79[yc & 0x3], data);
293    
294            if (sad < *(data->iMinSAD)) {
295                    *(data->iMinSAD) = sad;
296                    current->x = x; current->y = y;
297                    data->dir = Direction;
298            }
299    }
300    
301    static __inline VECTOR
302    ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
303    {
304    /* the stupidiest function ever */
305            return (mode == MODE_FORWARD ? pMB->mvs[0] : pMB->b_mvs[0]);
306    }
307    
308    static void __inline
309    PreparePredictionsBF(VECTOR * const pmv, const int x, const int y,
310                                                            const uint32_t iWcount,
311                                                            const MACROBLOCK * const pMB,
312                                                            const uint32_t mode_curr)
313    {
314    
315            /* [0] is prediction */
316            pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);
317    
318            pmv[1].x = pmv[1].y = 0; /* [1] is zero */
319    
320            pmv[2] = ChoosePred(pMB, mode_curr);
321            pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);
322    
323            if ((y != 0)&&(x != (int)(iWcount+1))) {                        /* [3] top-right neighbour */
324                    pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);
325                    pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);
326            } else pmv[3].x = pmv[3].y = 0;
327    
328            if (y != 0) {
329                    pmv[4] = ChoosePred(pMB-iWcount, mode_curr);
330                    pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y);
331            } else pmv[4].x = pmv[4].y = 0;
332    
333            if (x != 0) {
334                    pmv[5] = ChoosePred(pMB-1, mode_curr);
335                    pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
336            } else pmv[5].x = pmv[5].y = 0;
337    
338            if (x != 0 && y != 0) {
339                    pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);
340                    pmv[6].x = EVEN(pmv[6].x); pmv[6].y = EVEN(pmv[6].y);
341            } else pmv[6].x = pmv[6].y = 0;
342    }
343    
344    
345    /* search backward or forward */
346    static void
347    SearchBF(       const IMAGE * const pRef,
348                            const uint8_t * const pRefH,
349                            const uint8_t * const pRefV,
350                            const uint8_t * const pRefHV,
351                            const int x, const int y,
352                            const uint32_t MotionFlags,
353                            const uint32_t iFcode,
354                            const MBParam * const pParam,
355                            MACROBLOCK * const pMB,
356                            const VECTOR * const predMV,
357                            int32_t * const best_sad,
358                            const int32_t mode_current,
359                            SearchData * const Data)
360    {
361    
362            int i;
363            VECTOR pmv[7];
364            MainSearchFunc *MainSearchPtr;
365            *Data->iMinSAD = MV_MAX_ERROR;
366            Data->iFcode = iFcode;
367            Data->qpel_precision = 0;
368            Data->chromaX = Data->chromaY = Data->chromaSAD = 256*4096; /* reset chroma-sad cache */
369    
370            Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
371            Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
372            Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16;
373            Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16;
374            Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
375            Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
376    
377            Data->predMV = *predMV;
378    
379            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
380                                    pParam->width, pParam->height, iFcode - Data->qpel, 1, 0);
381    
382            pmv[0] = Data->predMV;
383            if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }
384    
385            PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);
386    
387            Data->currentMV->x = Data->currentMV->y = 0;
388    
389            /* main loop. checking all predictions */
390            for (i = 0; i < 7; i++)
391                    if (!vector_repeats(pmv, i) )
392                            CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i);
393    
394            if (*Data->iMinSAD > 512) {
395                    unsigned int mask = make_mask(pmv, 7, Data->dir);
396    
397                    MainSearchFunc *MainSearchPtr;
398                    if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
399                    else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
400                    else MainSearchPtr = xvid_me_DiamondSearch;
401    
402                    MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v);
403            }
404    
405            xvid_me_SubpelRefine(Data, CheckCandidate16no4v);
406    
407            if (Data->qpel && *Data->iMinSAD < *best_sad + 300) {
408                    Data->currentQMV->x = 2*Data->currentMV->x;
409                    Data->currentQMV->y = 2*Data->currentMV->y;
410                    Data->qpel_precision = 1;
411                    get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
412                                            pParam->width, pParam->height, iFcode, 2, 0);
413                    xvid_me_SubpelRefine(Data, CheckCandidate16no4v);
414            }
415    
416            /* three bits are needed to code backward mode. four for forward */
417    
418            if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16;
419            else *Data->iMinSAD += 3 * Data->lambda16;
420    
421            if (*Data->iMinSAD < *best_sad) {
422                    *best_sad = *Data->iMinSAD;
423                    pMB->mode = mode_current;
424                    if (Data->qpel) {
425                            pMB->pmvs[0].x = Data->currentQMV->x - predMV->x;
426                            pMB->pmvs[0].y = Data->currentQMV->y - predMV->y;
427                            if (mode_current == MODE_FORWARD)
428                                    pMB->qmvs[0] = *Data->currentQMV;
429                            else
430                                    pMB->b_qmvs[0] = *Data->currentQMV;
431                    } else {
432                            pMB->pmvs[0].x = Data->currentMV->x - predMV->x;
433                            pMB->pmvs[0].y = Data->currentMV->y - predMV->y;
434                    }
435                    if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV;
436                    else pMB->b_mvs[0] = *Data->currentMV;
437            }
438    
439            if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV;
440            else *(Data->currentMV+1) = *Data->currentMV; /* we store currmv for interpolate search */
441    }
442    
443    static void
444    SkipDecisionB(const IMAGE * const pCur,
445                                    const IMAGE * const f_Ref,
446                                    const IMAGE * const b_Ref,
447                                    MACROBLOCK * const pMB,
448                                    const uint32_t x, const uint32_t y,
449                                    const SearchData * const Data)
450    {
451            int k;
452    
453            if (!Data->chroma) {
454                    int dx = 0, dy = 0, b_dx = 0, b_dy = 0;
455                    int32_t sum;
456                    const uint32_t stride = Data->iEdgedWidth/2;
457                    /* this is not full chroma compensation, only it's fullpel approximation. should work though */
458    
459                    for (k = 0; k < 4; k++) {
460                            dy += Data->directmvF[k].y >> Data->qpel;
461                            dx += Data->directmvF[k].x >> Data->qpel;
462                            b_dy += Data->directmvB[k].y >> Data->qpel;
463                            b_dx += Data->directmvB[k].x >> Data->qpel;
464                    }
465    
466                    dy = (dy >> 3) + roundtab_76[dy & 0xf];
467                    dx = (dx >> 3) + roundtab_76[dx & 0xf];
468                    b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf];
469                    b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf];
470    
471                    sum = sad8bi(pCur->u + 8 * x + 8 * y * stride,
472                                                    f_Ref->u + (y*8 + dy/2) * stride + x*8 + dx/2,
473                                                    b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
474                                                    stride);
475    
476                    if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */
477    
478                    sum += sad8bi(pCur->v + 8*x + 8 * y * stride,
479                                                    f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2,
480                                                    b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
481                                                    stride);
482    
483                    if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */
484            }
485    
486            /* skip */
487            pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */
488            for (k = 0; k < 4; k++) {
489                    pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k];
490                    pMB->b_qmvs[k] = pMB->b_mvs[k] =  Data->directmvB[k];
491            }
492    }
493    
494    static uint32_t
495    SearchDirect(const IMAGE * const f_Ref,
496                                    const uint8_t * const f_RefH,
497                                    const uint8_t * const f_RefV,
498                                    const uint8_t * const f_RefHV,
499                                    const IMAGE * const b_Ref,
500                                    const uint8_t * const b_RefH,
501                                    const uint8_t * const b_RefV,
502                                    const uint8_t * const b_RefHV,
503                                    const IMAGE * const pCur,
504                                    const int x, const int y,
505                                    const uint32_t MotionFlags,
506                                    const int32_t TRB, const int32_t TRD,
507                                    const MBParam * const pParam,
508                                    MACROBLOCK * const pMB,
509                                    const MACROBLOCK * const b_mb,
510                                    int32_t * const best_sad,
511                                    SearchData * const Data)
512    
513    {
514            int32_t skip_sad;
515            int k = (x + Data->iEdgedWidth*y) * 16;
516            MainSearchFunc *MainSearchPtr;
517            CheckFunc * CheckCandidate;
518    
519            *Data->iMinSAD = 256*4096;
520            Data->RefP[0] = f_Ref->y + k;
521            Data->RefP[2] = f_RefH + k;
522            Data->RefP[1] = f_RefV + k;
523            Data->RefP[3] = f_RefHV + k;
524            Data->b_RefP[0] = b_Ref->y + k;
525            Data->b_RefP[2] = b_RefH + k;
526            Data->b_RefP[1] = b_RefV + k;
527            Data->b_RefP[3] = b_RefHV + k;
528            Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
529            Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
530            Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
531            Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
532    
533            k = Data->qpel ? 4 : 2;
534            Data->max_dx = k * (pParam->width - x * 16);
535            Data->max_dy = k * (pParam->height - y * 16);
536            Data->min_dx = -k * (16 + x * 16);
537            Data->min_dy = -k * (16 + y * 16);
538    
539            Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs;
540            Data->qpel_precision = 0;
541    
542            for (k = 0; k < 4; k++) {
543                    pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);
544                    pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD;
545                    pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);
546                    pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;
547    
548                    if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx)
549                            | (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) {
550    
551                            *best_sad = 256*4096; /* in that case, we won't use direct mode */
552                            pMB->mode = MODE_DIRECT; /* just to make sure it doesn't say "MODE_DIRECT_NONE_MV" */
553                            pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;
554                            return 256*4096;
555                    }
556                    if (b_mb->mode != MODE_INTER4V) {
557                            pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0];
558                            pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0];
559                            Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];
560                            Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];
561                            break;
562                    }
563            }
564    
565            CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v;
566    
567            CheckCandidate(0, 0, Data, 255);
568    
569            /* initial (fast) skip decision */
570            if (*Data->iMinSAD < (int)Data->iQuant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) {
571                    /* possible skip */
572                    SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);
573                    if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skipped */
574            }
575    
576            *Data->iMinSAD += Data->lambda16;
577            skip_sad = *Data->iMinSAD;
578    
579            if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
580                    else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
581                            else MainSearchPtr = xvid_me_DiamondSearch;
582    
583            MainSearchPtr(0, 0, Data, 255, CheckCandidate);
584    
585            xvid_me_SubpelRefine(Data, CheckCandidate);
586    
587            *best_sad = *Data->iMinSAD;
588    
589            if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT;
590            else pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */
591    
592            pMB->pmvs[3] = *Data->currentMV;
593    
594            for (k = 0; k < 4; k++) {
595                    pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x;
596                    pMB->b_mvs[k].x = (     (Data->currentMV->x == 0)
597                                                            ? Data->directmvB[k].x
598                                                            :pMB->mvs[k].x - Data->referencemv[k].x);
599                    pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y);
600                    pMB->b_mvs[k].y = ((Data->currentMV->y == 0)
601                                                            ? Data->directmvB[k].y
602                                                            : pMB->mvs[k].y - Data->referencemv[k].y);
603                    if (Data->qpel) {
604                            pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2;
605                            pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2;
606                            pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2;
607                            pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2;
608                    }
609    
610                    if (b_mb->mode != MODE_INTER4V) {
611                            pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];
612                            pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];
613                            pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0];
614                            pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0];
615                            break;
616                    }
617            }
618            return skip_sad;
619    }
620    
621    
622    static void set_range(int * range, SearchData * Data)
623    {
624            Data->min_dx = range[0];
625            Data->max_dx = range[1];
626            Data->min_dy = range[2];
627            Data->max_dy = range[3];
628    }
629    
630    static void
631    SubpelRefine_dir(SearchData * const data, CheckFunc * const CheckCandidate, const int dir)
632    {
633    /* Do a half-pel or q-pel refinement */
634            const VECTOR centerMV = data->qpel_precision ?
635                    data->currentQMV[dir-1] : data->currentMV[dir-1];
636    
637            CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, dir);
638            CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, dir);
639            CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, dir);
640            CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, dir);
641            CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, dir);
642            CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, dir);
643            CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, dir);
644            CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir);
645    }
646    
647    static void
648    SearchInterpolate(const IMAGE * const f_Ref,
649                                    const uint8_t * const f_RefH,
650                                    const uint8_t * const f_RefV,
651                                    const uint8_t * const f_RefHV,
652                                    const IMAGE * const b_Ref,
653                                    const uint8_t * const b_RefH,
654                                    const uint8_t * const b_RefV,
655                                    const uint8_t * const b_RefHV,
656                                    const int x, const int y,
657                                    const uint32_t fcode,
658                                    const uint32_t bcode,
659                                    const uint32_t MotionFlags,
660                                    const MBParam * const pParam,
661                                    const VECTOR * const f_predMV,
662                                    const VECTOR * const b_predMV,
663                                    MACROBLOCK * const pMB,
664                                    int32_t * const best_sad,
665                                    SearchData * const Data)
666    
667    {
668            int i, j;
669            int b_range[4], f_range[4];
670    
671            Data->qpel_precision = 0;
672            *Data->iMinSAD = 4096*256;
673            Data->iFcode = fcode; Data->bFcode = bcode;
674    
675            i = (x + y * Data->iEdgedWidth) * 16;
676    
677            Data->RefP[0] = f_Ref->y + i;
678            Data->RefP[2] = f_RefH + i;
679            Data->RefP[1] = f_RefV + i;
680            Data->RefP[3] = f_RefHV + i;
681            Data->b_RefP[0] = b_Ref->y + i;
682            Data->b_RefP[2] = b_RefH + i;
683            Data->b_RefP[1] = b_RefV + i;
684            Data->b_RefP[3] = b_RefHV + i;
685            Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
686            Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
687            Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
688            Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
689    
690            Data->predMV = *f_predMV;
691            Data->bpredMV = *b_predMV;
692    
693            Data->currentMV[0] = Data->currentMV[2]; /* forward search left its vector here */
694    
695            get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, fcode - Data->qpel, 1, 0);
696            get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, bcode - Data->qpel, 1, 0);
697    
698            if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1];
699            if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0];
700            if (Data->currentMV[0].y > f_range[3]) Data->currentMV[0].y = f_range[3];
701            if (Data->currentMV[0].y < f_range[2]) Data->currentMV[0].y = f_range[2];
702    
703            if (Data->currentMV[1].x > b_range[1]) Data->currentMV[1].x = b_range[1];
704            if (Data->currentMV[1].x < b_range[0]) Data->currentMV[1].x = b_range[0];
705            if (Data->currentMV[1].y > b_range[3]) Data->currentMV[1].y = b_range[3];
706            if (Data->currentMV[1].y < b_range[2]) Data->currentMV[1].y = b_range[2];
707    
708            set_range(f_range, Data);
709    
710            CheckCandidateInt(Data->currentMV[0].x, Data->currentMV[0].y, Data, 1);
711    
712            /* diamond */
713            do {
714                    Data->dir = 0;
715                    /* forward MV moves */
716                    i = Data->currentMV[0].x; j = Data->currentMV[0].y;
717    
718                    CheckCandidateInt(i + 1, j, Data, 1);
719                    CheckCandidateInt(i, j + 1, Data, 1);
720                    CheckCandidateInt(i - 1, j, Data, 1);
721                    CheckCandidateInt(i, j - 1, Data, 1);
722    
723                    /* backward MV moves */
724                    set_range(b_range, Data);
725                    i = Data->currentMV[1].x; j = Data->currentMV[1].y;
726    
727                    CheckCandidateInt(i + 1, j, Data, 2);
728                    CheckCandidateInt(i, j + 1, Data, 2);
729                    CheckCandidateInt(i - 1, j, Data, 2);
730                    CheckCandidateInt(i, j - 1, Data, 2);
731    
732                    set_range(f_range, Data);
733    
734            } while (Data->dir != 0);
735    
736            /* qpel refinement */
737            if (Data->qpel) {
738                    if (*Data->iMinSAD > *best_sad + 500) return;
739                    Data->qpel_precision = 1;
740                    get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0);
741    
742                    Data->currentQMV[0].x = 2 * Data->currentMV[0].x;
743                    Data->currentQMV[0].y = 2 * Data->currentMV[0].y;
744                    Data->currentQMV[1].x = 2 * Data->currentMV[1].x;
745                    Data->currentQMV[1].y = 2 * Data->currentMV[1].y;
746                    SubpelRefine_dir(Data, CheckCandidateInt, 1);
747                    if (*Data->iMinSAD > *best_sad + 300) return;
748                    get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0);
749                    SubpelRefine_dir(Data, CheckCandidateInt, 2);
750            }
751    
752            *Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */
753    
754            if (*Data->iMinSAD < *best_sad) {
755                    *best_sad = *Data->iMinSAD;
756                    pMB->mvs[0] = Data->currentMV[0];
757                    pMB->b_mvs[0] = Data->currentMV[1];
758                    pMB->mode = MODE_INTERPOLATE;
759                    if (Data->qpel) {
760                            pMB->qmvs[0] = Data->currentQMV[0];
761                            pMB->b_qmvs[0] = Data->currentQMV[1];
762                            pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;
763                            pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;
764                            pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;
765                            pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y;
766                    } else {
767                            pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;
768                            pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;
769                            pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;
770                            pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;
771                    }
772            }
773    }
774    
775    void
776    MotionEstimationBVOP(MBParam * const pParam,
777                                             FRAMEINFO * const frame,
778                                             const int32_t time_bp,
779                                             const int32_t time_pp,
780                                             /* forward (past) reference */
781                                             const MACROBLOCK * const f_mbs,
782                                             const IMAGE * const f_ref,
783                                             const IMAGE * const f_refH,
784                                             const IMAGE * const f_refV,
785                                             const IMAGE * const f_refHV,
786                                             /* backward (future) reference */
787                                             const FRAMEINFO * const b_reference,
788                                             const IMAGE * const b_ref,
789                                             const IMAGE * const b_refH,
790                                             const IMAGE * const b_refV,
791                                             const IMAGE * const b_refHV)
792    {
793            uint32_t i, j;
794            int32_t best_sad;
795            uint32_t skip_sad;
796    
797            const MACROBLOCK * const b_mbs = b_reference->mbs;
798    
799            VECTOR f_predMV, b_predMV;
800    
801            const int32_t TRB = time_pp - time_bp;
802            const int32_t TRD = time_pp;
803    
804            /* some pre-inintialized data for the rest of the search */
805    
806            SearchData Data;
807            memset(&Data, 0, sizeof(SearchData));
808    
809            Data.iEdgedWidth = pParam->edged_width;
810            Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;
811            Data.rounding = 0;
812            Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;
813            Data.iQuant = frame->quant;
814    
815            Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */
816    
817            /* note: i==horizontal, j==vertical */
818            for (j = 0; j < pParam->mb_height; j++) {
819    
820                    f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */
821    
822                    for (i = 0; i < pParam->mb_width; i++) {
823                            MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
824                            const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
825    
826    /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
827                            if (b_reference->coding_type != S_VOP)
828                                    if (b_mb->mode == MODE_NOT_CODED) {
829                                            pMB->mode = MODE_NOT_CODED;
830                                            pMB->mvs[0] = pMB->b_mvs[0] = zeroMV;
831                                            continue;
832                                    }
833    
834                            Data.lambda16 = xvid_me_lambda_vec16[b_mb->quant];
835    
836                            Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
837                            Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;
838                            Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;
839    
840    /* direct search comes first, because it (1) checks for SKIP-mode
841            and (2) sets very good predictions for forward and backward search */
842                            skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y,
843                                                                            b_ref, b_refH->y, b_refV->y, b_refHV->y,
844                                                                            &frame->image,
845                                                                            i, j,
846                                                                            frame->motion_flags,
847                                                                            TRB, TRD,
848                                                                            pParam,
849                                                                            pMB, b_mb,
850                                                                            &best_sad,
851                                                                            &Data);
852    
853                            if (pMB->mode == MODE_DIRECT_NONE_MV) continue;
854    
855                            /* forward search */
856                            SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,
857                                                    i, j,
858                                                    frame->motion_flags,
859                                                    frame->fcode, pParam,
860                                                    pMB, &f_predMV, &best_sad,
861                                                    MODE_FORWARD, &Data);
862    
863                            /* backward search */
864                            SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y,
865                                                    i, j,
866                                                    frame->motion_flags,
867                                                    frame->bcode, pParam,
868                                                    pMB, &b_predMV, &best_sad,
869                                                    MODE_BACKWARD, &Data);
870    
871                            /* interpolate search comes last, because it uses data from forward and backward as prediction */
872                            SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,
873                                                    b_ref, b_refH->y, b_refV->y, b_refHV->y,
874                                                    i, j,
875                                                    frame->fcode, frame->bcode,
876                                                    frame->motion_flags,
877                                                    pParam,
878                                                    &f_predMV, &b_predMV,
879                                                    pMB, &best_sad,
880                                                    &Data);
881    
882                            /* final skip decision */
883                            if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2) )
884                                    && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )
885    
886                                    SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);
887    
888                            switch (pMB->mode) {
889                                    case MODE_FORWARD:
890                                            f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];
891                                            break;
892                                    case MODE_BACKWARD:
893                                            b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];
894                                            break;
895                                    case MODE_INTERPOLATE:
896                                            f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];
897                                            b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];
898                                            break;
899                                    default:
900                                            break;
901                            }
902                    }
903            }
904    }
905    

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

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