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

Annotation of /xvidcore/src/motion/estimation_rd_based.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (view) (download)

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

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