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

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