[svn] / branches / dev-api-4 / xvidcore / src / motion / estimation_rd_based.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/motion/estimation_rd_based.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1169 - (view) (download)

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

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