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

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