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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1506 - (view) (download)

1 : syskin 1506
2 :     #include <assert.h>
3 :     #include <stdio.h>
4 :     #include <stdlib.h>
5 :     #include <string.h> /* memcpy */
6 :    
7 :     #include "../encoder.h"
8 :     #include "../bitstream/mbcoding.h"
9 :     #include "../prediction/mbprediction.h"
10 :     #include "../global.h"
11 :     #include "../image/interpolate8x8.h"
12 :     #include "estimation.h"
13 :     #include "motion.h"
14 :     #include "sad.h"
15 :     #include "../bitstream/zigzag.h"
16 :     #include "../quant/quant.h"
17 :     #include "../bitstream/vlc_codes.h"
18 :     #include "../dct/fdct.h"
19 :     #include "motion_inlines.h"
20 :     #include "../plugins/HVS_base.h"
21 :    
22 :    
23 :     /* rd = BITS_MULT*bits + LAMBDA*distortion */
24 :     #define LAMBDA ( (int)(BITS_MULT*1.0) )
25 :    
26 :    
27 :     static __inline unsigned int
28 :     Block_CalcBits_BVOP(int16_t * const coeff,
29 :     int16_t * const data,
30 :     int16_t * const dqcoeff,
31 :     const uint32_t quant, const int quant_type,
32 :     uint32_t * cbp,
33 :     const int block,
34 :     const uint16_t * scan_table,
35 :     const unsigned int lambda,
36 :     const uint16_t * mpeg_quant_matrices)
37 :     {
38 :     int sum;
39 :     int bits;
40 :     int distortion = 0;
41 :    
42 :     fdct(data);
43 :    
44 :     if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices);
45 :     else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices);
46 :    
47 :     if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] != 0)) {
48 :     *cbp |= 1 << (5 - block);
49 :     bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table);
50 :    
51 :     if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices);
52 :     else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices);
53 :    
54 :     distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t));
55 :     } else {
56 :     const static int16_t zero_block[64] =
57 :     {
58 :     0, 0, 0, 0, 0, 0, 0, 0,
59 :     0, 0, 0, 0, 0, 0, 0, 0,
60 :     0, 0, 0, 0, 0, 0, 0, 0,
61 :     0, 0, 0, 0, 0, 0, 0, 0,
62 :     0, 0, 0, 0, 0, 0, 0, 0,
63 :     0, 0, 0, 0, 0, 0, 0, 0,
64 :     0, 0, 0, 0, 0, 0, 0, 0,
65 :     0, 0, 0, 0, 0, 0, 0, 0,
66 :     };
67 :     bits = 0;
68 :     distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t));
69 :     }
70 :    
71 :     return bits + (lambda*distortion)/(quant*quant);
72 :     }
73 :    
74 :    
75 :     static __inline unsigned int
76 :     Block_CalcBits_BVOP_direct(int16_t * const coeff,
77 :     int16_t * const data,
78 :     int16_t * const dqcoeff,
79 :     const uint32_t quant, const int quant_type,
80 :     uint32_t * cbp,
81 :     const int block,
82 :     const uint16_t * scan_table,
83 :     const unsigned int lambda,
84 :     const uint16_t * mpeg_quant_matrices)
85 :     {
86 :     int sum;
87 :     int bits;
88 :     int distortion = 0;
89 :    
90 :     fdct(data);
91 :    
92 :     if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices);
93 :     else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices);
94 :    
95 :     if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] > 0) || (coeff[0] < -1)) {
96 :     *cbp |= 1 << (5 - block);
97 :     bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table);
98 :    
99 :     if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices);
100 :     else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices);
101 :    
102 :     distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t));
103 :     } else {
104 :     const static int16_t zero_block[64] =
105 :     {
106 :     0, 0, 0, 0, 0, 0, 0, 0,
107 :     0, 0, 0, 0, 0, 0, 0, 0,
108 :     0, 0, 0, 0, 0, 0, 0, 0,
109 :     0, 0, 0, 0, 0, 0, 0, 0,
110 :     0, 0, 0, 0, 0, 0, 0, 0,
111 :     0, 0, 0, 0, 0, 0, 0, 0,
112 :     0, 0, 0, 0, 0, 0, 0, 0,
113 :     0, 0, 0, 0, 0, 0, 0, 0,
114 :     };
115 :     bits = 0;
116 :     distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t));
117 :     }
118 :    
119 :     return bits + (lambda*distortion)/(quant*quant);
120 :     }
121 :    
122 :    
123 :     static void
124 :     transfer_8to16sub2ro(int16_t * const dct,
125 :     const uint8_t * const cur,
126 :     const uint8_t * ref1,
127 :     const uint8_t * ref2,
128 :     const uint32_t stride)
129 :     {
130 :     uint32_t i, j;
131 :    
132 :     for (j = 0; j < 8; j++) {
133 :     for (i = 0; i < 8; i++) {
134 :     uint8_t c = cur[j * stride + i];
135 :     int r = (ref1[j * stride + i] + ref2[j * stride + i] + 1) / 2;
136 :     dct[j * 8 + i] = (int16_t) c - (int16_t) r;
137 :     }
138 :     }
139 :     }
140 :    
141 :     static void
142 :     CheckCandidateRDBF(const int x, const int y, SearchData * const data, const unsigned int Direction)
143 :     {
144 :    
145 :     int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
146 :     int32_t rd = 3*BITS_MULT; /* note to self: 3 bits minimum, but maybe 4 if it's forward mode */
147 :     VECTOR * current;
148 :     const uint8_t * ptr;
149 :     int i, xc, yc;
150 :     unsigned cbp = 0;
151 :    
152 :     if ( (x > data->max_dx) || (x < data->min_dx)
153 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
154 :    
155 :     if (!data->qpel_precision) {
156 :     ptr = GetReference(x, y, data);
157 :     current = data->currentMV;
158 :     xc = x; yc = y;
159 :     } else { /* x and y are in 1/4 precision */
160 :     ptr = xvid_me_interpolate16x16qpel(x, y, 0, data);
161 :     current = data->currentQMV;
162 :     xc = x/2; yc = y/2;
163 :     }
164 :    
165 :     rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
166 :    
167 :     for(i = 0; i < 4; i++) {
168 :     int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);
169 :     transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);
170 :     rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices);
171 :     if (rd >= data->iMinSAD[0]) return;
172 :     }
173 :    
174 :     /* chroma */
175 :     xc = (xc >> 1) + roundtab_79[xc & 0x3];
176 :     yc = (yc >> 1) + roundtab_79[yc & 0x3];
177 :    
178 :     /* chroma U */
179 :     ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding);
180 :     transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2);
181 :     rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices);
182 :     if (rd >= data->iMinSAD[0]) return;
183 :    
184 :     /* chroma V */
185 :     ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding);
186 :     transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2);
187 :     rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices);
188 :    
189 :     if (cbp) rd += BITS_MULT * 7;
190 :    
191 :     if (rd < data->iMinSAD[0]) {
192 :     data->iMinSAD[0] = rd;
193 :     current[0].x = x; current[0].y = y;
194 :     data->dir = Direction;
195 :     *data->cbp = cbp;
196 :     }
197 :     }
198 :    
199 :    
200 :     static void
201 :     CheckCandidateRDDirect(const int x, const int y, SearchData * const data, const unsigned int Direction)
202 :     {
203 :     int32_t xcf = 0, ycf = 0, xcb = 0, ycb = 0;
204 :     int32_t rd = 1*BITS_MULT;
205 :     int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
206 :     unsigned int cbp = 0;
207 :     unsigned int k;
208 :     VECTOR mvs, b_mvs;
209 :    
210 :     const uint8_t *ReferenceF, *ReferenceB;
211 :    
212 :     if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
213 :    
214 :     for (k = 0; k < 4; k++) {
215 :     int s = 8*((k&1) + (k>>1)*data->iEdgedWidth);
216 :    
217 :     mvs.x = data->directmvF[k].x + x;
218 :     b_mvs.x = ((x == 0) ?
219 :     data->directmvB[k].x
220 :     : mvs.x - data->referencemv[k].x);
221 :    
222 :     mvs.y = data->directmvF[k].y + y;
223 :     b_mvs.y = ((y == 0) ?
224 :     data->directmvB[k].y
225 :     : mvs.y - data->referencemv[k].y);
226 :    
227 :     if ((mvs.x > data->max_dx) || (mvs.x < data->min_dx) ||
228 :     (mvs.y > data->max_dy) || (mvs.y < data->min_dy) ||
229 :     (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) ||
230 :     (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) )
231 :     return;
232 :    
233 :     if (data->qpel) {
234 :     xcf += mvs.x/2; ycf += mvs.y/2;
235 :     xcb += b_mvs.x/2; ycb += b_mvs.y/2;
236 :     ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
237 :     ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
238 :     } else {
239 :     xcf += mvs.x; ycf += mvs.y;
240 :     xcb += b_mvs.x; ycb += b_mvs.y;
241 :     ReferenceF = GetReference(mvs.x, mvs.y, data) + s;
242 :     ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data) + s;
243 :     }
244 :    
245 :     transfer_8to16sub2ro(in, data->Cur + s, ReferenceF, ReferenceB, data->iEdgedWidth);
246 :     rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, k, data->scan_table, data->lambda[k], data->mpeg_quant_matrices);
247 :     if (rd > *(data->iMinSAD)) return;
248 :     }
249 :    
250 :     /* chroma */
251 :     xcf = (xcf >> 3) + roundtab_76[xcf & 0xf];
252 :     ycf = (ycf >> 3) + roundtab_76[ycf & 0xf];
253 :     xcb = (xcb >> 3) + roundtab_76[xcb & 0xf];
254 :     ycb = (ycb >> 3) + roundtab_76[ycb & 0xf];
255 :    
256 :     /* chroma U */
257 :     ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding);
258 :     ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding);
259 :     transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2);
260 :     rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices);
261 :     if (rd >= data->iMinSAD[0]) return;
262 :    
263 :     /* chroma V */
264 :     ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding);
265 :     ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding);
266 :     transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2);
267 :     rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices);
268 :    
269 :     if (cbp)
270 :     rd += BITS_MULT * 6;
271 :     if (cbp || x != 0 || y != 0)
272 :     rd += BITS_MULT * d_mv_bits(x, y, zeroMV, 1, 0, 0);
273 :    
274 :     if (rd < *(data->iMinSAD)) {
275 :     *data->iMinSAD = rd;
276 :     data->currentMV->x = x; data->currentMV->y = y;
277 :     data->dir = Direction;
278 :     *data->cbp = cbp;
279 :     }
280 :     }
281 :    
282 :    
283 :    
284 :    
285 :     static void
286 :     CheckCandidateRDInt(const int x, const int y, SearchData * const data, const unsigned int Direction)
287 :     {
288 :     int32_t xf, yf, xb, yb, xcf, ycf, xcb, ycb;
289 :     int32_t rd = 2*BITS_MULT;
290 :     int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
291 :     unsigned int cbp = 0;
292 :     unsigned int i;
293 :    
294 :     const uint8_t *ReferenceF, *ReferenceB;
295 :     VECTOR *current;
296 :    
297 :     if ((x > data->max_dx) || (x < data->min_dx) ||
298 :     (y > data->max_dy) || (y < data->min_dy))
299 :     return;
300 :    
301 :     if (Direction == 1) { /* x and y mean forward vector */
302 :     VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1];
303 :     xb = backward.x;
304 :     yb = backward.y;
305 :     xf = x; yf = y;
306 :     } else { /* x and y mean backward vector */
307 :     VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0];
308 :     xf = forward.x;
309 :     yf = forward.y;
310 :     xb = x; yb = y;
311 :     }
312 :    
313 :     if (!data->qpel_precision) {
314 :     ReferenceF = GetReference(xf, yf, data);
315 :     ReferenceB = GetReferenceB(xb, yb, 1, data);
316 :     current = data->currentMV + Direction - 1;
317 :     xcf = xf; ycf = yf;
318 :     xcb = xb; ycb = yb;
319 :     } else {
320 :     ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data);
321 :     current = data->currentQMV + Direction - 1;
322 :     ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data);
323 :     xcf = xf/2; ycf = yf/2;
324 :     xcb = xb/2; ycb = yb/2;
325 :     }
326 :    
327 :     rd += BITS_MULT * (d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0)
328 :     + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0));
329 :    
330 :    
331 :     for(i = 0; i < 4; i++) {
332 :     int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);
333 :     if (rd >= *data->iMinSAD) return;
334 :     transfer_8to16sub2ro(in, data->Cur + s, ReferenceF + s, ReferenceB + s, data->iEdgedWidth);
335 :     rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices);
336 :     }
337 :    
338 :     /* chroma */
339 :     xcf = (xcf >> 1) + roundtab_79[xcf & 0x3];
340 :     ycf = (ycf >> 1) + roundtab_79[ycf & 0x3];
341 :     xcb = (xcb >> 1) + roundtab_79[xcb & 0x3];
342 :     ycb = (ycb >> 1) + roundtab_79[ycb & 0x3];
343 :    
344 :     /* chroma U */
345 :     ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding);
346 :     ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding);
347 :     transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2);
348 :     rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices);
349 :     if (rd >= data->iMinSAD[0]) return;
350 :    
351 :    
352 :     /* chroma V */
353 :     ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding);
354 :     ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding);
355 :     transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2);
356 :     rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices);
357 :    
358 :     if (cbp) rd += BITS_MULT * 7;
359 :    
360 :     if (rd < *(data->iMinSAD)) {
361 :     *data->iMinSAD = rd;
362 :     current->x = x; current->y = y;
363 :     data->dir = Direction;
364 :     *data->cbp = cbp;
365 :     }
366 :     }
367 :    
368 :     static int
369 :     SearchInterpolate_RD(const int x, const int y,
370 :     const uint32_t MotionFlags,
371 :     const MBParam * const pParam,
372 :     int32_t * const best_sad,
373 :     SearchData * const Data)
374 :     {
375 :     int i, j;
376 :    
377 :     Data->iMinSAD[0] = *best_sad;
378 :    
379 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy,
380 :     x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel, 0);
381 :    
382 :     Data->qpel_precision = Data->qpel;
383 :    
384 :     if (Data->qpel) {
385 :     i = Data->currentQMV[0].x; j = Data->currentQMV[0].y;
386 :     } else {
387 :     i = Data->currentMV[0].x; j = Data->currentMV[0].y;
388 :     }
389 :    
390 :     CheckCandidateRDInt(i, j, Data, 1);
391 :    
392 :     return Data->iMinSAD[0];
393 :     }
394 :    
395 :     static int
396 :     SearchDirect_RD(const int x, const int y,
397 :     const uint32_t MotionFlags,
398 :     const MBParam * const pParam,
399 :     int32_t * const best_sad,
400 :     SearchData * const Data)
401 :     {
402 :     Data->iMinSAD[0] = *best_sad;
403 :    
404 :     Data->qpel_precision = Data->qpel;
405 :    
406 :     CheckCandidateRDDirect(Data->currentMV->x, Data->currentMV->y, Data, 255);
407 :    
408 :     return Data->iMinSAD[0];
409 :     }
410 :    
411 :     static int
412 :     SearchBF_RD(const int x, const int y,
413 :     const uint32_t MotionFlags,
414 :     const MBParam * const pParam,
415 :     int32_t * const best_sad,
416 :     SearchData * const Data)
417 :     {
418 :     int i, j;
419 :    
420 :     Data->iMinSAD[0] = *best_sad;
421 :    
422 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy,
423 :     x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel, 0);
424 :    
425 :     Data->qpel_precision = Data->qpel;
426 :    
427 :     if (Data->qpel) {
428 :     i = Data->currentQMV[0].x; j = Data->currentQMV[0].y;
429 :     } else {
430 :     i = Data->currentMV[0].x; j = Data->currentMV[0].y;
431 :     }
432 :    
433 :     CheckCandidateRDBF(i, j, Data, 1);
434 :    
435 :     return Data->iMinSAD[0];
436 :     }
437 :    
438 :     static int
439 :     get_sad_for_mode(int mode,
440 :     SearchData * const Data_d,
441 :     SearchData * const Data_b,
442 :     SearchData * const Data_f,
443 :     SearchData * const Data_i)
444 :     {
445 :     switch(mode) {
446 :     case MODE_DIRECT: return Data_d->iMinSAD[0];
447 :     case MODE_FORWARD: return Data_f->iMinSAD[0];
448 :     case MODE_BACKWARD: return Data_b->iMinSAD[0];
449 :     default:
450 :     case MODE_INTERPOLATE: return Data_i->iMinSAD[0];
451 :     }
452 :     }
453 :    
454 :     void
455 :     ModeDecision_BVOP_RD(SearchData * const Data_d,
456 :     SearchData * const Data_b,
457 :     SearchData * const Data_f,
458 :     SearchData * const Data_i,
459 :     MACROBLOCK * const pMB,
460 :     const MACROBLOCK * const b_mb,
461 :     VECTOR * f_predMV,
462 :     VECTOR * b_predMV,
463 :     const uint32_t MotionFlags,
464 :     const MBParam * const pParam,
465 :     int x, int y)
466 :     {
467 :     int mode = MODE_DIRECT, k;
468 :     int f_rd, b_rd, i_rd, d_rd, best_rd;
469 :     const int qpel = Data_d->qpel;
470 :     const uint32_t iQuant = Data_d->iQuant;
471 :     int i;
472 :     int ref_quant = b_mb->quant;
473 :    
474 :     int order[4] = {MODE_DIRECT, MODE_FORWARD, MODE_BACKWARD, MODE_INTERPOLATE};
475 :    
476 :     Data_d->scan_table = Data_b->scan_table = Data_f->scan_table = Data_i->scan_table
477 :     = /*VopFlags & XVID_VOP_ALTERNATESCAN ? scan_tables[2] : */scan_tables[0];
478 :    
479 :     f_rd = b_rd = i_rd = d_rd = best_rd = 256*4096;
480 :    
481 :     for (i = 0; i < 6; i++) {
482 :     int lam = (LAMBDA*iQuant*iQuant)/(ref_quant*(ref_quant+1)); /* re-calculate as if it was p-frame's quant +.5 */
483 :     Data_d->lambda[i] = lam;
484 :     Data_b->lambda[i] = lam;
485 :     Data_f->lambda[i] = lam;
486 :     Data_i->lambda[i] = lam;
487 :     }
488 :    
489 :     /* find the best order of evaluation - smallest SAD comes first, because *if* it means smaller RD,
490 :     early-stops will activate sooner */
491 :    
492 :     for (i = 3; i >= 0; i--) {
493 :     int j;
494 :     for (j = 0; j < i; j++) {
495 :     int sad1 = get_sad_for_mode(order[j], Data_d, Data_b, Data_f, Data_i);
496 :     int sad2 = get_sad_for_mode(order[j+1], Data_d, Data_b, Data_f, Data_i);
497 :     if (sad1 > sad2) {
498 :     int t = order[j];
499 :     order[j] = order[j+1];
500 :     order[j+1] = t;
501 :     }
502 :     }
503 :     }
504 :    
505 :     /* evaluate cost of all modes */
506 :     for (i = 0; i < 4; i++) {
507 :     int rd;
508 :    
509 :     switch (order[i]) {
510 :     case MODE_DIRECT:
511 :     rd = d_rd = SearchDirect_RD(x, y, MotionFlags, pParam, &best_rd, Data_d);
512 :     break;
513 :     case MODE_FORWARD:
514 :     rd = f_rd = SearchBF_RD(x, y, MotionFlags, pParam, &best_rd, Data_f) + 1*BITS_MULT; /* extra one bit for FORWARD vs BACKWARD */
515 :     break;
516 :     case MODE_BACKWARD:
517 :     rd = b_rd = SearchBF_RD(x, y, MotionFlags, pParam, &best_rd, Data_b);
518 :     break;
519 :     case MODE_INTERPOLATE:
520 :     rd = i_rd = SearchInterpolate_RD(x, y, MotionFlags, pParam, &best_rd, Data_i);
521 :     break;
522 :     }
523 :     if (rd < best_rd) {
524 :     mode = order[i];
525 :     best_rd = rd;
526 :     }
527 :     }
528 :    
529 :     pMB->sad16 = best_rd;
530 :     pMB->mode = mode;
531 :    
532 :     switch (mode) {
533 :    
534 :     case MODE_DIRECT:
535 :     if (!qpel && b_mb->mode != MODE_INTER4V) pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */
536 :    
537 :     pMB->pmvs[3] = Data_d->currentMV[0];
538 :    
539 :     pMB->cbp = *Data_d->cbp;
540 :    
541 :     for (k = 0; k < 4; k++) {
542 :     pMB->mvs[k].x = Data_d->directmvF[k].x + Data_d->currentMV->x;
543 :     pMB->b_mvs[k].x = ( (Data_d->currentMV->x == 0)
544 :     ? Data_d->directmvB[k].x
545 :     :pMB->mvs[k].x - Data_d->referencemv[k].x);
546 :     pMB->mvs[k].y = (Data_d->directmvF[k].y + Data_d->currentMV->y);
547 :     pMB->b_mvs[k].y = ((Data_d->currentMV->y == 0)
548 :     ? Data_d->directmvB[k].y
549 :     : pMB->mvs[k].y - Data_d->referencemv[k].y);
550 :     if (qpel) {
551 :     pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2;
552 :     pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2;
553 :     pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2;
554 :     pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2;
555 :     }
556 :    
557 :     if (b_mb->mode != MODE_INTER4V) {
558 :     pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];
559 :     pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];
560 :     pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0];
561 :     pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0];
562 :     break;
563 :     }
564 :     }
565 :     break;
566 :    
567 :     case MODE_FORWARD:
568 :     if (qpel) {
569 :     pMB->pmvs[0].x = Data_f->currentQMV->x - f_predMV->x;
570 :     pMB->pmvs[0].y = Data_f->currentQMV->y - f_predMV->y;
571 :     pMB->qmvs[0] = *Data_f->currentQMV;
572 :     *f_predMV = Data_f->currentQMV[0];
573 :     } else {
574 :     pMB->pmvs[0].x = Data_f->currentMV->x - f_predMV->x;
575 :     pMB->pmvs[0].y = Data_f->currentMV->y - f_predMV->y;
576 :     *f_predMV = Data_f->currentMV[0];
577 :     }
578 :     pMB->mvs[0] = *Data_f->currentMV;
579 :     pMB->cbp = *Data_f->cbp;
580 :     break;
581 :    
582 :     case MODE_BACKWARD:
583 :     if (qpel) {
584 :     pMB->pmvs[0].x = Data_b->currentQMV->x - b_predMV->x;
585 :     pMB->pmvs[0].y = Data_b->currentQMV->y - b_predMV->y;
586 :     pMB->b_qmvs[0] = *Data_b->currentQMV;
587 :     *b_predMV = Data_b->currentQMV[0];
588 :     } else {
589 :     pMB->pmvs[0].x = Data_b->currentMV->x - b_predMV->x;
590 :     pMB->pmvs[0].y = Data_b->currentMV->y - b_predMV->y;
591 :     *b_predMV = Data_b->currentMV[0];
592 :     }
593 :     pMB->b_mvs[0] = *Data_b->currentMV;
594 :     pMB->cbp = *Data_b->cbp;
595 :     break;
596 :    
597 :    
598 :     case MODE_INTERPOLATE:
599 :     pMB->mvs[0] = Data_i->currentMV[0];
600 :     pMB->b_mvs[0] = Data_i->currentMV[1];
601 :     if (qpel) {
602 :     pMB->qmvs[0] = Data_i->currentQMV[0];
603 :     pMB->b_qmvs[0] = Data_i->currentQMV[1];
604 :     pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;
605 :     pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;
606 :     pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;
607 :     pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y;
608 :     *f_predMV = Data_i->currentQMV[0];
609 :     *b_predMV = Data_i->currentQMV[1];
610 :     } else {
611 :     pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;
612 :     pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;
613 :     pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;
614 :     pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;
615 :     *f_predMV = Data_i->currentMV[0];
616 :     *b_predMV = Data_i->currentMV[1];
617 :     }
618 :     pMB->cbp = *Data_i->cbp;
619 :     break;
620 :     }
621 :     }

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