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

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

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