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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1988 - (view) (download)

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

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