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

Diff of /xvidcore/src/motion/estimation_rd_based.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.1, Wed Sep 10 22:18:59 2003 UTC
# Line 0  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - Rate-Distortion Based Motion Estimation for P- and S- VOPs  -
5     *
6     *  Copyright(C) 2003 Radoslaw Czyz <xvid@syskin.cjb.net>
7     *               2003 Michael Militzer <michael@xvid.org>
8     *
9     *  This program is free software ; you can redistribute it and/or modify
10     *  it under the terms of the GNU General Public License as published by
11     *  the Free Software Foundation ; either version 2 of the License, or
12     *  (at your option) any later version.
13     *
14     *  This program is distributed in the hope that it will be useful,
15     *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
16     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     *  GNU General Public License for more details.
18     *
19     *  You should have received a copy of the GNU General Public License
20     *  along with this program ; if not, write to the Free Software
21     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22     *
23     * $Id$
24     *
25     ****************************************************************************/
26    
27    /* RD mode decision and search */
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 "../bitstream/mbcoding.h"
36    #include "../prediction/mbprediction.h"
37    #include "../global.h"
38    #include "../image/interpolate8x8.h"
39    #include "estimation.h"
40    #include "motion.h"
41    #include "sad.h"
42    #include "../bitstream/zigzag.h"
43    #include "../quant/quant_mpeg4.h"
44    #include "../quant/quant_h263.h"
45    #include "../bitstream/vlc_codes.h"
46    #include "../dct/fdct.h"
47    #include "motion_inlines.h"
48    
49    /* rd = BITS_MULT*bits + LAMBDA*distortion */
50    #define LAMBDA          ( (int)(BITS_MULT*1.0) )
51    
52    static __inline unsigned int
53    Block_CalcBits( int16_t * const coeff,
54                                    int16_t * const data,
55                                    int16_t * const dqcoeff,
56                                    const uint32_t quant, const int quant_type,
57                                    uint32_t * cbp,
58                                    const int block)
59    {
60            int sum;
61            int bits;
62            int distortion = 0;
63            int i;
64    
65            fdct(data);
66    
67            if (quant_type) sum = quant_inter(coeff, data, quant);
68            else sum = quant4_inter(coeff, data, quant);
69    
70            if (sum > 0) {
71                    *cbp |= 1 << (5 - block);
72                    bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
73    
74                    if (quant_type) dequant_inter(dqcoeff, coeff, quant);
75                    else dequant4_inter(dqcoeff, coeff, quant);
76    
77                    for (i = 0; i < 64; i++)
78                            distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]);
79    
80            } else {
81                    bits = 0;
82                    for (i = 0; i < 64; i++)
83                            distortion += data[i]*data[i];
84            }
85    
86            return bits + (LAMBDA*distortion)/(quant*quant);
87    }
88    
89    static __inline unsigned int
90    Block_CalcBitsIntra(int16_t * const coeff,
91                                            int16_t * const data,
92                                            int16_t * const dqcoeff,
93                                            const uint32_t quant, const int quant_type,
94                                            uint32_t * cbp,
95                                            const int block,
96                                            int * dcpred)
97    {
98            int bits, i;
99            int distortion = 0;
100            uint32_t iDcScaler = get_dc_scaler(quant, block < 4);
101            int b_dc;
102    
103            fdct(data);
104            data[0] -= 1024;
105    
106            if (quant_type) quant_intra(coeff, data, quant, iDcScaler);
107            else quant4_intra(coeff, data, quant, iDcScaler);
108    
109            b_dc = coeff[0];
110            if (block < 4) {
111                    coeff[0] -= *dcpred;
112                    *dcpred = b_dc;
113            }
114    
115            bits = BITS_MULT*CodeCoeffIntra_CalcBits(coeff, scan_tables[0]);
116            if (bits != 0) *cbp |= 1 << (5 - block);
117    
118            if (block < 4) bits += BITS_MULT*dcy_tab[coeff[0] + 255].len;
119            else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len;
120    
121            coeff[0] = b_dc;
122            if (quant_type) dequant_intra(dqcoeff, coeff, quant, iDcScaler);
123            else dequant4_intra(dqcoeff, coeff, quant, iDcScaler);
124    
125            for (i = 0; i < 64; i++)
126                    distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]);
127    
128            return bits + (LAMBDA*distortion)/(quant*quant);
129    }
130    
131    static void
132    CheckCandidateRD16(const int x, const int y, const SearchData * const data, const unsigned int Direction)
133    {
134    
135            int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
136            int32_t rd = 0;
137            VECTOR * current;
138            const uint8_t * ptr;
139            int i, cbp = 0, t, xc, yc;
140    
141            if ( (x > data->max_dx) || (x < data->min_dx)
142                    || (y > data->max_dy) || (y < data->min_dy) ) return;
143    
144            if (!data->qpel_precision) {
145                    ptr = GetReference(x, y, data);
146                    current = data->currentMV;
147                    xc = x; yc = y;
148            } else { /* x and y are in 1/4 precision */
149                    ptr = xvid_me_interpolate16x16qpel(x, y, 0, data);
150                    current = data->currentQMV;
151                    xc = x/2; yc = y/2;
152            }
153    
154            for(i = 0; i < 4; i++) {
155                    int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);
156                    transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);
157                    rd += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i);
158            }
159    
160            rd += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
161    
162            if (data->temp[0] + t < data->iMinSAD[1]) {
163                    data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; data->cbp[1] = (data->cbp[1]&~32) | (cbp&32); }
164            if (data->temp[1] < data->iMinSAD[2]) {
165                    data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; data->cbp[1] = (data->cbp[1]&~16) | (cbp&16); }
166            if (data->temp[2] < data->iMinSAD[3]) {
167                    data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; data->cbp[1] = (data->cbp[1]&~8) | (cbp&8); }
168            if (data->temp[3] < data->iMinSAD[4]) {
169                    data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; data->cbp[1] = (data->cbp[1]&~4) | (cbp&4); }
170    
171            rd += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
172    
173            if (rd >= data->iMinSAD[0]) return;
174    
175            /* chroma */
176            xc = (xc >> 1) + roundtab_79[xc & 0x3];
177            yc = (yc >> 1) + roundtab_79[yc & 0x3];
178    
179            /* chroma U */
180            ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding);
181            transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2);
182            rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4);
183            if (rd >= data->iMinSAD[0]) return;
184    
185            /* chroma V */
186            ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding);
187            transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2);
188            rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);
189    
190            rd += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
191    
192            if (rd < data->iMinSAD[0]) {
193                    data->iMinSAD[0] = rd;
194                    current[0].x = x; current[0].y = y;
195                    *data->dir = Direction;
196                    *data->cbp = cbp;
197            }
198    }
199    
200    static void
201    CheckCandidateRD8(const int x, const int y, const SearchData * const data, const unsigned int Direction)
202    {
203    
204            int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
205            int32_t rd;
206            VECTOR * current;
207            const uint8_t * ptr;
208            int cbp = 0;
209    
210            if ( (x > data->max_dx) || (x < data->min_dx)
211                    || (y > data->max_dy) || (y < data->min_dy) ) return;
212    
213            if (!data->qpel_precision) {
214                    ptr = GetReference(x, y, data);
215                    current = data->currentMV;
216            } else { /* x and y are in 1/4 precision */
217                    ptr = xvid_me_interpolate8x8qpel(x, y, 0, 0, data);
218                    current = data->currentQMV;
219            }
220    
221            transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);
222            rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);
223            rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
224    
225            if (rd < data->iMinSAD[0]) {
226                    *data->cbp = cbp;
227                    data->iMinSAD[0] = rd;
228                    current[0].x = x; current[0].y = y;
229                    *data->dir = Direction;
230            }
231    }
232    
233    
234    static int
235    findRD_inter(SearchData * const Data,
236                            const int x, const int y,
237                            const MBParam * const pParam,
238                            const uint32_t MotionFlags)
239    {
240            int i;
241            int32_t bsad[5];
242    
243            if (Data->qpel) {
244                    for(i = 0; i < 5; i++) {
245                            Data->currentMV[i].x = Data->currentQMV[i].x/2;
246                            Data->currentMV[i].y = Data->currentQMV[i].y/2;
247                    }
248                    Data->qpel_precision = 1;
249                    CheckCandidateRD16(Data->currentQMV[0].x, Data->currentQMV[0].y, Data, 255);
250    
251                    if (MotionFlags & (XVID_ME_HALFPELREFINE16_RD | XVID_ME_EXTSEARCH_RD)) { /* we have to prepare for halfpixel-precision search */
252                            for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];
253                            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
254                                                    pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
255                            Data->qpel_precision = 0;
256                            if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1)
257                                    CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255);
258                    }
259    
260            } else { /* not qpel */
261    
262                    CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255);
263            }
264    
265            if (MotionFlags&XVID_ME_EXTSEARCH_RD)
266                    xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16);
267    
268            if (MotionFlags&XVID_ME_HALFPELREFINE16_RD)
269                    xvid_me_SubpelRefine(Data, CheckCandidateRD16);
270    
271            if (Data->qpel) {
272                    if (MotionFlags&(XVID_ME_EXTSEARCH_RD | XVID_ME_HALFPELREFINE16_RD)) { /* there was halfpel-precision search */
273                            for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {
274                                    Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */
275                                    Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
276                            }
277    
278                            /* preparing for qpel-precision search */
279                            Data->qpel_precision = 1;
280                            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
281                                            pParam->width, pParam->height, Data->iFcode, 2, 0);
282                    }
283                    if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD)
284                            xvid_me_SubpelRefine(Data, CheckCandidateRD16);
285            }
286    
287            if (MotionFlags&XVID_ME_CHECKPREDICTION_RD) { /* let's check vector equal to prediction */
288                    VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;
289                    if (!MVequal(Data->predMV, *v))
290                            CheckCandidateRD16(Data->predMV.x, Data->predMV.y, Data, 255);
291            }
292            return Data->iMinSAD[0];
293    }
294    
295    static int
296    findRD_inter4v(const SearchData * const Data,
297                                    MACROBLOCK * const pMB, const MACROBLOCK * const pMBs,
298                                    const int x, const int y,
299                                    const MBParam * const pParam, const uint32_t MotionFlags,
300                                    const VECTOR * const backup)
301    {
302    
303            int cbp = 0, bits = 0, t = 0, i;
304            SearchData Data2, *Data8 = &Data2;
305            int sumx = 0, sumy = 0;
306            int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64;
307            uint8_t * ptr;
308    
309            memcpy(Data8, Data, sizeof(SearchData));
310    
311            for (i = 0; i < 4; i++) { /* for all luma blocks */
312    
313                    Data8->iMinSAD = Data->iMinSAD + i + 1;
314                    Data8->currentMV = Data->currentMV + i + 1;
315                    Data8->currentQMV = Data->currentQMV + i + 1;
316                    Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
317                    Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
318                    Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
319                    Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
320                    Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
321                    *Data8->cbp = (Data->cbp[1] & (1<<(5-i))) ? 1:0; // copy corresponding cbp bit
322    
323                    if(Data->qpel) {
324                            Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i);
325                            if (i != 0)     t = d_mv_bits(  Data8->currentQMV->x, Data8->currentQMV->y,
326                                                                                    Data8->predMV, Data8->iFcode, 0, 0);
327                    } else {
328                            Data8->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, i);
329                            if (i != 0)     t = d_mv_bits(  Data8->currentMV->x, Data8->currentMV->y,
330                                                                                    Data8->predMV, Data8->iFcode, 0, 0);
331                    }
332    
333                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3,
334                                            pParam->width, pParam->height, Data8->iFcode, Data8->qpel+1, 0);
335    
336                    *Data8->iMinSAD += BITS_MULT*t;
337    
338                    Data8->qpel_precision = Data8->qpel;
339                    /* checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) */
340                    {
341                            VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV;
342                            if (!MVequal (*v, backup[i+1]) )
343                                    CheckCandidateRD8(backup[i+1].x, backup[i+1].y, Data8, 255);
344                    }
345    
346                    if (Data8->qpel) {
347                            if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */
348                                    int32_t s = *Data8->iMinSAD;
349                                    Data8->currentMV->x = Data8->currentQMV->x/2;
350                                    Data8->currentMV->y = Data8->currentQMV->y/2;
351                                    Data8->qpel_precision = 0;
352                                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3,
353                                                            pParam->width, pParam->height, Data8->iFcode - 1, 1, 0);
354    
355                                    if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)
356                                            CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, Data8, 255);
357    
358                                    if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD)
359                                            xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8);
360    
361                                    if (MotionFlags & XVID_ME_HALFPELREFINE8_RD)
362                                            xvid_me_SubpelRefine(Data8, CheckCandidateRD8);
363    
364                                    if(s > *Data8->iMinSAD) { /* we have found a better match */
365                                            Data8->currentQMV->x = 2*Data8->currentMV->x;
366                                            Data8->currentQMV->y = 2*Data8->currentMV->y;
367                                    }
368    
369                                    Data8->qpel_precision = 1;
370                                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3,
371                                                            pParam->width, pParam->height, Data8->iFcode, 2, 0);
372    
373                            }
374                            if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD)
375                                    xvid_me_SubpelRefine(Data8, CheckCandidateRD8);
376    
377                    } else { /* not qpel */
378    
379                            if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) /* extsearch */
380                                    xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8);
381    
382                            if (MotionFlags & XVID_ME_HALFPELREFINE8_RD)
383                                    xvid_me_SubpelRefine(Data8, CheckCandidateRD8); /* halfpel refinement */
384                    }
385    
386                    /* checking vector equal to predicion */
387                    if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_RD) {
388                            const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;
389                            if (!MVequal(*v, Data8->predMV))
390                                    CheckCandidateRD8(Data8->predMV.x, Data8->predMV.y, Data8, 255);
391                    }
392    
393                    bits += *Data8->iMinSAD;
394                    if (bits >= Data->iMinSAD[0]) return bits; /* no chances for INTER4V */
395    
396                    /* MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else */
397                    if(Data->qpel) {
398                            pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x;
399                            pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y;
400                            pMB->qmvs[i] = *Data8->currentQMV;
401                            sumx += Data8->currentQMV->x/2;
402                            sumy += Data8->currentQMV->y/2;
403                    } else {
404                            pMB->pmvs[i].x = Data8->currentMV->x - Data8->predMV.x;
405                            pMB->pmvs[i].y = Data8->currentMV->y - Data8->predMV.y;
406                            sumx += Data8->currentMV->x;
407                            sumy += Data8->currentMV->y;
408                    }
409                    pMB->mvs[i] = *Data8->currentMV;
410                    pMB->sad8[i] = 4 * *Data8->iMinSAD;
411                    if (Data8->cbp[0]) cbp |= 1 << (5 - i);
412    
413            } /* end - for all luma blocks */
414    
415            bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
416    
417            /* let's check chroma */
418            sumx = (sumx >> 3) + roundtab_76[sumx & 0xf];
419            sumy = (sumy >> 3) + roundtab_76[sumy & 0xf];
420    
421            /* chroma U */
422            ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
423            transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2);
424            bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4);
425    
426            if (bits >= *Data->iMinSAD) return bits;
427    
428            /* chroma V */
429            ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
430            transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2);
431            bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5);
432    
433            bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;
434    
435            *Data->cbp = cbp;
436            return bits;
437    }
438    
439    static int
440    findRD_intra(const SearchData * const Data)
441    {
442            int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */
443            int cbp = 0, i, dc = 0;
444            int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;
445    
446            for(i = 0; i < 4; i++) {
447                    int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);
448                    transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth);
449                    bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, &dc);
450    
451                    if (bits >= Data->iMinSAD[0]) return bits;
452            }
453    
454            bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len;
455    
456            /*chroma U */
457            transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2);
458            bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, &dc);
459    
460            if (bits >= Data->iMinSAD[0]) return bits;
461    
462            /* chroma V */
463            transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);
464            bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, &dc);
465    
466            bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;
467    
468            return bits;
469    }
470    
471    static int
472    findRD_gmc(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y)
473    {
474            int bits = BITS_MULT*1; /* this one is mcsel */
475            int cbp = 0, i;
476            int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;
477    
478            for(i = 0; i < 4; i++) {
479                    int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);
480                    transfer_8to16subro(in, Data->Cur + s, vGMC->y + s + 16*(x+y*Data->iEdgedWidth), Data->iEdgedWidth);
481                    bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i);
482                    if (bits >= Data->iMinSAD[0]) return bits;
483            }
484    
485            bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
486    
487            /*chroma U */
488            transfer_8to16subro(in, Data->CurU, vGMC->u + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2);
489            bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4);
490    
491            if (bits >= Data->iMinSAD[0]) return bits;
492    
493            /* chroma V */
494            transfer_8to16subro(in, Data->CurV , vGMC->v + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2);
495            bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5);
496    
497            bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
498    
499            *Data->cbp = cbp;
500    
501            return bits;
502    }
503    
504    void
505    xvid_me_ModeDecision_RD(SearchData * const Data,
506                                    MACROBLOCK * const pMB,
507                                    const MACROBLOCK * const pMBs,
508                                    const int x, const int y,
509                                    const MBParam * const pParam,
510                                    const uint32_t MotionFlags,
511                                    const uint32_t VopFlags,
512                                    const uint32_t VolFlags,
513                                    const IMAGE * const pCurrent,
514                                    const IMAGE * const pRef,
515                                    const IMAGE * const vGMC,
516                                    const int coding_type)
517    {
518            int mode = MODE_INTER;
519            int mcsel = 0;
520            int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
521            const uint32_t iQuant = pMB->quant;
522    
523            int min_rd, intra_rd, i, cbp, c[2] = {0, 0};
524            VECTOR backup[5], *v;
525            Data->iQuant = iQuant;
526            Data->cbp = c;
527    
528            v = Data->qpel ? Data->currentQMV : Data->currentMV;
529            for (i = 0; i < 5; i++) {
530                    Data->iMinSAD[i] = 256*4096;
531                    backup[i] = v[i];
532            }
533    
534            min_rd = findRD_inter(Data, x, y, pParam, MotionFlags);
535            cbp = *Data->cbp;
536    
537            if (coding_type == S_VOP) {
538                    int gmc_rd;
539                    *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */
540                    gmc_rd = findRD_gmc(Data, vGMC, x, y);
541                    if (gmc_rd < min_rd) {
542                            mcsel = 1;
543                            *Data->iMinSAD = min_rd = gmc_rd;
544                            mode = MODE_INTER;
545                            cbp = *Data->cbp;
546                    }
547            }
548    
549            if (inter4v) {
550                    int v4_rd;
551                    v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
552                    if (v4_rd < min_rd) {
553                            Data->iMinSAD[0] = min_rd = v4_rd;
554                            mode = MODE_INTER4V;
555                            cbp = *Data->cbp;
556                    }
557            }
558    
559            intra_rd = findRD_intra(Data);
560            if (intra_rd < min_rd) {
561                    *Data->iMinSAD = min_rd = intra_rd;
562                    mode = MODE_INTRA;
563            }
564    
565            pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
566            pMB->cbp = cbp;
567    
568    
569            if (Data->rrv) {
570                            Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
571                            Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
572            }
573    
574            if (mode == MODE_INTER && mcsel == 0) {
575                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
576    
577                    if(Data->qpel) {
578                            pMB->qmvs[0] = pMB->qmvs[1]
579                                    = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
580                            pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
581                            pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
582                    } else {
583                            pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
584                            pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
585                    }
586    
587            } else if (mode == MODE_INTER ) { // but mcsel == 1
588    
589                    pMB->mcsel = 1;
590                    if (Data->qpel) {
591                            pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
592                            pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
593                            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
594                    } else
595                            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
596    
597            } else
598                    if (mode == MODE_INTER4V) ; /* anything here? */
599            else    /* INTRA, NOT_CODED */
600                    ZeroMacroblockP(pMB, 0);
601    
602            pMB->mode = mode;
603    }
604    
605    void
606    xvid_me_ModeDecision_Fast(SearchData * const Data,
607                            MACROBLOCK * const pMB,
608                            const MACROBLOCK * const pMBs,
609                            const int x, const int y,
610                            const MBParam * const pParam,
611                            const uint32_t MotionFlags,
612                            const uint32_t VopFlags,
613                            const uint32_t VolFlags,
614                            const IMAGE * const pCurrent,
615                            const IMAGE * const pRef,
616                            const IMAGE * const vGMC,
617                            const int coding_type)
618    {
619            int mode = MODE_INTER;
620            int mcsel = 0;
621            int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
622            const uint32_t iQuant = pMB->quant;
623            const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0);
624        int sad;
625            int min_rd = -1, intra_rd, i, cbp = 63, c[2] = {0, 0};
626            VECTOR backup[5], *v;
627            int sad_backup[5];
628            int InterBias = MV16_INTER_BIAS;
629            int thresh = 0;
630            int top = 0, top_right = 0, left = 0;
631    
632            pMB->mcsel = 0;
633    
634            /* INTER <-> INTER4V decision */
635            if ((Data->iMinSAD[0] + 75 < Data->iMinSAD[1] +
636                    Data->iMinSAD[2] + Data->iMinSAD[3] + Data->iMinSAD[4])) { /* normal, fast, SAD-based mode decision */
637                    if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
638                            Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {
639                            mode = MODE_INTER;
640                            sad = Data->iMinSAD[0];
641                    } else {
642                            mode = MODE_INTER4V;
643                            sad = Data->iMinSAD[1] + Data->iMinSAD[2] +
644                                                    Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant;
645                            Data->iMinSAD[0] = sad;
646                    }
647    
648                    /* final skip decision, a.k.a. "the vector you found, really that good?" */
649                    if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))
650                            if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)
651                                    if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {
652                                            mode = MODE_NOT_CODED;
653                                            sad = 0;  /* Compiler warning */
654                                            goto early_out;
655                                    }
656    
657                    /* mcsel */
658                    if (coding_type == S_VOP) {
659    
660                            int32_t iSAD = sad16(Data->Cur,
661                                    vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536);
662    
663                            if (Data->chroma) {
664                                    iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
665                                    iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
666                            }
667    
668                            if (iSAD <= sad) {              /* mode decision GMC */
669                                    mode = MODE_INTER;
670                                    mcsel = 1;
671                                    sad = iSAD;
672                            }
673    
674                    }
675            } else { /* Rate-Distortion INTER<->INTER4V */
676                    Data->iQuant = iQuant;
677                    Data->cbp = c;
678                    v = Data->qpel ? Data->currentQMV : Data->currentMV;
679    
680                    /* final skip decision, a.k.a. "the vector you found, really that good?" */
681                    if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))
682                            if ( (100*Data->iMinSAD[0])/(pMB->sad16+1) > FINAL_SKIP_THRESH)
683                                    if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {
684                                            mode = MODE_NOT_CODED;
685                                            sad = 0; /* Compiler warning */
686                                            goto early_out;
687                                    }
688    
689                    for (i = 0; i < 5; i++) {
690                            sad_backup[i] = Data->iMinSAD[i];
691                            Data->iMinSAD[i] = 256*4096;
692                            backup[i] = v[i];
693                    }
694    
695                    min_rd = findRD_inter(Data, x, y, pParam, MotionFlags);
696                    cbp = *Data->cbp;
697                    sad = sad_backup[0];
698    
699                    if (coding_type == S_VOP) {
700                            int gmc_rd;
701    
702                            *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */
703                            gmc_rd = findRD_gmc(Data, vGMC, x, y);
704                            if (gmc_rd < min_rd) {
705                                    mcsel = 1;
706                                    *Data->iMinSAD = min_rd = gmc_rd;
707                                    mode = MODE_INTER;
708                                    cbp = *Data->cbp;
709                                    sad = sad16(Data->Cur,
710                                            vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536);
711                                    if (Data->chroma) {
712                                            sad += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
713                                            sad += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
714                                    }
715                            }
716                    }
717    
718                    if (inter4v) {
719                            int v4_rd;
720                            v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
721                            if (v4_rd < min_rd) {
722                                    Data->iMinSAD[0] = min_rd = v4_rd;
723                                    mode = MODE_INTER4V;
724                                    cbp = *Data->cbp;
725                                    sad = sad_backup[1] + sad_backup[2] +
726                                              sad_backup[3] + sad_backup[4] + IMV16X16 * (int32_t)iQuant;
727                            }
728                    }
729            }
730    
731            left = top = top_right = -1;
732            thresh = 0;
733    
734            if((x > 0) && (y > 0) && (x < (int32_t) pParam->mb_width)) {
735                    left = (&pMBs[(x-1) + y * pParam->mb_width])->sad16; // left
736                    top = (&pMBs[x + (y-1) * pParam->mb_width])->sad16; // top
737                    top_right = (&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16; // top right
738    
739                    if(((&pMBs[(x-1) + y * pParam->mb_width])->mode != MODE_INTRA) &&
740                       ((&pMBs[x + (y-1) * pParam->mb_width])->mode != MODE_INTRA) &&
741                       ((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mode != MODE_INTRA)) {
742                            thresh = MAX(MAX(top, left), top_right);
743                    }
744                    else
745                            thresh = MIN(MIN(top, left), top_right);
746            }
747    
748            /* INTRA <-> INTER decision */
749            if (sad < thresh) { /* normal, fast, SAD-based mode decision */
750                    /* intra decision */
751    
752                    if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */
753                    if (y != 0)
754                            if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
755                    if (x != 0)
756                            if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
757    
758                    if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */
759                    if (Data->rrv) InterBias *= 4;
760    
761                    if (InterBias < sad) {
762                            int32_t deviation;
763                            if (!Data->rrv)
764                                    deviation = dev16(Data->Cur, Data->iEdgedWidth);
765                            else
766                                    deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */
767                                                            dev16(Data->Cur+16, Data->iEdgedWidth) +
768                                                            dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +
769                                                            dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);
770    
771                            if (deviation < (sad - InterBias)) mode = MODE_INTRA;
772                    }
773    
774                    pMB->cbp = 63;
775            } else { /* Rate-Distortion INTRA<->INTER */
776                    if(min_rd < 0) {
777                            Data->iQuant = iQuant;
778                            Data->cbp = c;
779                            v = Data->qpel ? Data->currentQMV : Data->currentMV;
780    
781                            for (i = 0; i < 5; i++) {
782                                    Data->iMinSAD[i] = 256*4096;
783                                    backup[i] = v[i];
784                            }
785    
786                            if(mode == MODE_INTER) {
787                                    min_rd = findRD_inter(Data, x, y, pParam, MotionFlags);
788                                    cbp = *Data->cbp;
789    
790                                    if (coding_type == S_VOP) {
791                                            int gmc_rd;
792    
793                                            *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */
794                                            gmc_rd = findRD_gmc(Data, vGMC, x, y);
795                                            if (gmc_rd < min_rd) {
796                                                    mcsel = 1;
797                                                    *Data->iMinSAD = min_rd = gmc_rd;
798                                                    mode = MODE_INTER;
799                                                    cbp = *Data->cbp;
800                                            }
801                                    }
802                            }
803    
804                            if(mode == MODE_INTER4V) {
805                                    int v4_rd;
806                                    v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
807                                    if (v4_rd < min_rd) {
808                                            Data->iMinSAD[0] = min_rd = v4_rd;
809                                            mode = MODE_INTER4V;
810                                            cbp = *Data->cbp;
811                                    }
812                            }
813                    }
814    
815                    intra_rd = findRD_intra(Data);
816                    if (intra_rd < min_rd) {
817                            *Data->iMinSAD = min_rd = intra_rd;
818                            mode = MODE_INTRA;
819                    }
820    
821                    pMB->cbp = cbp;
822            }
823    
824    early_out:
825            pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
826    
827            if (Data->rrv) {
828                            Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
829                            Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
830            }
831    
832            if (mode == MODE_INTER && mcsel == 0) {
833                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
834    
835                    if(Data->qpel) {
836                            pMB->qmvs[0] = pMB->qmvs[1]
837                                    = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
838                            pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
839                            pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
840                    } else {
841                            pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
842                            pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
843                    }
844    
845            } else if (mode == MODE_INTER ) { // but mcsel == 1
846    
847                    pMB->mcsel = 1;
848                    if (Data->qpel) {
849                            pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
850                            pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
851                            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
852                    } else
853                            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
854    
855            } else
856                    if (mode == MODE_INTER4V) ; /* anything here? */
857            else    /* INTRA, NOT_CODED */
858                    ZeroMacroblockP(pMB, 0);
859    
860            pMB->mode = mode;
861    }

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

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