42 |
* * |
* * |
43 |
* Revision history: * |
* Revision history: * |
44 |
* * |
* * |
45 |
|
* 29.06.2002 predict_acdc() bounding * |
46 |
* 12.12.2001 improved calc_acdc_prediction; removed need for memcpy * |
* 12.12.2001 improved calc_acdc_prediction; removed need for memcpy * |
47 |
* 15.12.2001 moved pmv displacement to motion estimation * |
* 15.12.2001 moved pmv displacement to motion estimation * |
48 |
* 30.11.2001 mmx cbp support * |
* 30.11.2001 mmx cbp support * |
50 |
* * |
* * |
51 |
******************************************************************************/ |
******************************************************************************/ |
52 |
|
|
53 |
|
#include <stdlib.h> |
54 |
|
|
55 |
|
#include "../global.h" |
56 |
#include "../encoder.h" |
#include "../encoder.h" |
57 |
#include "mbprediction.h" |
#include "mbprediction.h" |
58 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
59 |
#include "../bitstream/cbp.h" |
#include "../bitstream/cbp.h" |
60 |
|
#include "../bitstream/mbcoding.h" |
61 |
|
#include "../bitstream/zigzag.h" |
62 |
|
|
63 |
|
|
64 |
#define ABS(X) (((X)>0)?(X):-(X)) |
static int __inline |
65 |
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
rescale(int predict_quant, |
66 |
|
int current_quant, |
67 |
|
int coeff) |
|
static int __inline rescale(int predict_quant, int current_quant, int coeff) |
|
68 |
{ |
{ |
69 |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), (current_quant)) : 0; |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), |
70 |
|
(current_quant)) : 0; |
71 |
} |
} |
72 |
|
|
73 |
|
|
83 |
*/ |
*/ |
84 |
|
|
85 |
|
|
86 |
void predict_acdc(MACROBLOCK *pMBs, |
void |
87 |
uint32_t x, uint32_t y, uint32_t mb_width, |
predict_acdc(MACROBLOCK * pMBs, |
88 |
|
uint32_t x, |
89 |
|
uint32_t y, |
90 |
|
uint32_t mb_width, |
91 |
uint32_t block, |
uint32_t block, |
92 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
93 |
uint32_t current_quant, |
uint32_t current_quant, |
94 |
int32_t iDcScaler, |
int32_t iDcScaler, |
95 |
int16_t predictors[8]) |
int16_t predictors[8], |
96 |
|
const int bound) |
97 |
|
|
98 |
{ |
{ |
99 |
|
const int mbpos = (y * mb_width) + x; |
100 |
int16_t *left, *top, *diag, *current; |
int16_t *left, *top, *diag, *current; |
101 |
|
|
102 |
int32_t left_quant = current_quant; |
int32_t left_quant = current_quant; |
116 |
|
|
117 |
// left macroblock |
// left macroblock |
118 |
|
|
119 |
if(x && (pMBs[index - 1].mode == MODE_INTRA |
if (x && mbpos >= bound + 1 && |
120 |
|| pMBs[index - 1].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1].mode == MODE_INTRA || |
121 |
|
pMBs[index - 1].mode == MODE_INTRA_Q)) { |
122 |
|
|
123 |
left = pMBs[index - 1].pred_values[0]; |
left = pMBs[index - 1].pred_values[0]; |
124 |
left_quant = pMBs[index - 1].quant; |
left_quant = pMBs[index - 1].quant; |
125 |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
126 |
} |
} |
|
|
|
127 |
// top macroblock |
// top macroblock |
128 |
|
|
129 |
if(y && (pMBs[index - mb_width].mode == MODE_INTRA |
if (mbpos >= bound + (int)mb_width && |
130 |
|| pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - mb_width].mode == MODE_INTRA || |
131 |
|
pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
132 |
|
|
133 |
top = pMBs[index - mb_width].pred_values[0]; |
top = pMBs[index - mb_width].pred_values[0]; |
134 |
top_quant = pMBs[index - mb_width].quant; |
top_quant = pMBs[index - mb_width].quant; |
135 |
} |
} |
|
|
|
136 |
// diag macroblock |
// diag macroblock |
137 |
|
|
138 |
if(x && y && (pMBs[index - 1 - mb_width].mode == MODE_INTRA |
if (x && mbpos >= bound + (int)mb_width + 1 && |
139 |
|| pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
140 |
|
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
141 |
|
|
142 |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
143 |
} |
} |
214 |
// determine ac prediction direction & ac/dc predictor |
// determine ac prediction direction & ac/dc predictor |
215 |
// place rescaled ac/dc predictions into predictors[] for later use |
// place rescaled ac/dc predictions into predictors[] for later use |
216 |
|
|
217 |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
if (abs(pLeft[0] - pDiag[0]) < abs(pDiag[0] - pTop[0])) { |
218 |
*acpred_direction = 1; // vertical |
*acpred_direction = 1; // vertical |
219 |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
220 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
221 |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
222 |
} |
} |
223 |
} |
} else { |
|
else |
|
|
{ |
|
224 |
*acpred_direction = 2; // horizontal |
*acpred_direction = 2; // horizontal |
225 |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
226 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
227 |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
228 |
} |
} |
229 |
} |
} |
235 |
*/ |
*/ |
236 |
|
|
237 |
|
|
238 |
void add_acdc(MACROBLOCK *pMB, |
void |
239 |
|
add_acdc(MACROBLOCK * pMB, |
240 |
uint32_t block, |
uint32_t block, |
241 |
int16_t dct_codes[64], |
int16_t dct_codes[64], |
242 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
246 |
int16_t * pCurrent = pMB->pred_values[block]; |
int16_t * pCurrent = pMB->pred_values[block]; |
247 |
uint32_t i; |
uint32_t i; |
248 |
|
|
249 |
|
DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]); |
250 |
|
|
251 |
dct_codes[0] += predictors[0]; // dc prediction |
dct_codes[0] += predictors[0]; // dc prediction |
252 |
pCurrent[0] = dct_codes[0] * iDcScaler; |
pCurrent[0] = dct_codes[0] * iDcScaler; |
253 |
|
|
254 |
if (acpred_direction == 1) |
if (acpred_direction == 1) { |
255 |
{ |
for (i = 1; i < 8; i++) { |
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
256 |
int level = dct_codes[i] + predictors[i]; |
int level = dct_codes[i] + predictors[i]; |
257 |
|
|
258 |
|
DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i, predictors[i]); |
259 |
|
|
260 |
dct_codes[i] = level; |
dct_codes[i] = level; |
261 |
pCurrent[i] = level; |
pCurrent[i] = level; |
262 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
263 |
} |
} |
264 |
} |
} else if (acpred_direction == 2) { |
265 |
else if (acpred_direction == 2) |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
266 |
int level = dct_codes[i*8] + predictors[i]; |
int level = dct_codes[i*8] + predictors[i]; |
267 |
|
DPRINTF(XVID_DEBUG_COEFF,"predictor[%i] %i\n",i*8, predictors[i]); |
268 |
|
|
269 |
dct_codes[i*8] = level; |
dct_codes[i*8] = level; |
270 |
pCurrent[i+7] = level; |
pCurrent[i+7] = level; |
271 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
272 |
} |
} |
273 |
} |
} else { |
274 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
275 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
276 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
277 |
} |
} |
285 |
|
|
286 |
/* encoder: subtract predictors from qcoeff[] and calculate S1/S2 |
/* encoder: subtract predictors from qcoeff[] and calculate S1/S2 |
287 |
|
|
288 |
todo: perform [-127,127] clamping after prediction |
returns sum of coeefficients *saved* if prediction is enabled |
|
clamping must adjust the coeffs, so dequant is done correctly |
|
289 |
|
|
|
S1/S2 are used to determine if its worth predicting for AC |
|
290 |
S1 = sum of all (qcoeff - prediction) |
S1 = sum of all (qcoeff - prediction) |
291 |
S2 = sum of all qcoeff |
S2 = sum of all qcoeff |
292 |
*/ |
*/ |
293 |
|
|
294 |
uint32_t calc_acdc(MACROBLOCK *pMB, |
int |
295 |
|
calc_acdc_coeff(MACROBLOCK * pMB, |
296 |
uint32_t block, |
uint32_t block, |
297 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
298 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
300 |
{ |
{ |
301 |
int16_t * pCurrent = pMB->pred_values[block]; |
int16_t * pCurrent = pMB->pred_values[block]; |
302 |
uint32_t i; |
uint32_t i; |
303 |
uint32_t S1 = 0, S2 = 0; |
int S1 = 0, S2 = 0; |
304 |
|
|
305 |
|
|
306 |
/* store current coeffs to pred_values[] for future prediction */ |
/* store current coeffs to pred_values[] for future prediction */ |
315 |
|
|
316 |
qcoeff[0] = qcoeff[0] - predictors[0]; |
qcoeff[0] = qcoeff[0] - predictors[0]; |
317 |
|
|
318 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
|
{ |
|
319 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
320 |
int16_t level; |
int16_t level; |
321 |
|
|
322 |
level = qcoeff[i]; |
level = qcoeff[i]; |
323 |
S2 += ABS(level); |
S2 += abs(level); |
324 |
level -= predictors[i]; |
level -= predictors[i]; |
325 |
S1 += ABS(level); |
S1 += abs(level); |
326 |
predictors[i] = level; |
predictors[i] = level; |
327 |
} |
} |
328 |
} |
} else // acpred_direction == 2 |
|
else // acpred_direction == 2 |
|
329 |
{ |
{ |
330 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
331 |
int16_t level; |
int16_t level; |
332 |
|
|
333 |
level = qcoeff[i*8]; |
level = qcoeff[i*8]; |
334 |
S2 += ABS(level); |
S2 += abs(level); |
335 |
level -= predictors[i]; |
level -= predictors[i]; |
336 |
S1 += ABS(level); |
S1 += abs(level); |
337 |
predictors[i] = level; |
predictors[i] = level; |
338 |
} |
} |
339 |
|
|
344 |
} |
} |
345 |
|
|
346 |
|
|
|
/* apply predictors[] to qcoeff */ |
|
347 |
|
|
348 |
void apply_acdc(MACROBLOCK *pMB, |
/* returns the bits *saved* if prediction is enabled */ |
349 |
|
|
350 |
|
int |
351 |
|
calc_acdc_bits(MACROBLOCK * pMB, |
352 |
uint32_t block, |
uint32_t block, |
353 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
354 |
|
uint32_t iDcScaler, |
355 |
int16_t predictors[8]) |
int16_t predictors[8]) |
356 |
{ |
{ |
357 |
uint32_t i; |
const int direction = pMB->acpred_directions[block]; |
358 |
|
int16_t *pCurrent = pMB->pred_values[block]; |
359 |
|
int16_t tmp[8]; |
360 |
|
unsigned int i; |
361 |
|
int Z1, Z2; |
362 |
|
|
363 |
if (pMB->acpred_directions[block] == 1) |
/* store current coeffs to pred_values[] for future prediction */ |
364 |
{ |
pCurrent[0] = qcoeff[0] * iDcScaler; |
365 |
|
for (i = 1; i < 8; i++) { |
366 |
|
pCurrent[i] = qcoeff[i]; |
367 |
|
pCurrent[i + 7] = qcoeff[i * 8]; |
368 |
|
} |
369 |
|
|
370 |
|
|
371 |
|
/* dc prediction */ |
372 |
|
qcoeff[0] = qcoeff[0] - predictors[0]; |
373 |
|
|
374 |
|
/* calc cost before ac prediction */ |
375 |
|
#ifdef BIGLUT |
376 |
|
Z2 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[0], 1); |
377 |
|
#else |
378 |
|
Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
379 |
|
#endif |
380 |
|
|
381 |
|
/* apply ac prediction & calc cost*/ |
382 |
|
if (direction == 1) { |
383 |
|
for (i = 1; i < 8; i++) { |
384 |
|
tmp[i] = qcoeff[i]; |
385 |
|
qcoeff[i] -= predictors[i]; |
386 |
|
predictors[i] = qcoeff[i]; |
387 |
|
} |
388 |
|
}else{ // acpred_direction == 2 |
389 |
|
for (i = 1; i < 8; i++) { |
390 |
|
tmp[i] = qcoeff[i*8]; |
391 |
|
qcoeff[i*8] -= predictors[i]; |
392 |
|
predictors[i] = qcoeff[i*8]; |
393 |
|
} |
394 |
|
} |
395 |
|
|
396 |
|
#ifdef BIGLUT |
397 |
|
Z1 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[direction], 1); |
398 |
|
#else |
399 |
|
Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
400 |
|
#endif |
401 |
|
|
402 |
|
/* undo prediction */ |
403 |
|
if (direction == 1) { |
404 |
for(i = 1; i < 8; i++) |
for(i = 1; i < 8; i++) |
405 |
{ |
qcoeff[i] = tmp[i]; |
406 |
qcoeff[i] = predictors[i]; |
}else{ // acpred_direction == 2 |
407 |
|
for (i = 1; i < 8; i++) |
408 |
|
qcoeff[i*8] = tmp[i]; |
409 |
} |
} |
410 |
|
|
411 |
|
return Z2-Z1; |
412 |
} |
} |
413 |
else |
|
414 |
|
/* apply predictors[] to qcoeff */ |
415 |
|
|
416 |
|
void |
417 |
|
apply_acdc(MACROBLOCK * pMB, |
418 |
|
uint32_t block, |
419 |
|
int16_t qcoeff[64], |
420 |
|
int16_t predictors[8]) |
421 |
{ |
{ |
422 |
|
unsigned int i; |
423 |
|
|
424 |
|
if (pMB->acpred_directions[block] == 1) { |
425 |
|
for (i = 1; i < 8; i++) |
426 |
|
qcoeff[i] = predictors[i]; |
427 |
|
} else { |
428 |
for(i = 1; i < 8; i++) |
for(i = 1; i < 8; i++) |
|
{ |
|
429 |
qcoeff[i*8] = predictors[i]; |
qcoeff[i*8] = predictors[i]; |
430 |
} |
} |
431 |
} |
} |
|
} |
|
432 |
|
|
433 |
|
|
434 |
void MBPrediction(FRAMEINFO *frame, |
void |
435 |
|
MBPrediction(FRAMEINFO * frame, |
436 |
uint32_t x, |
uint32_t x, |
437 |
uint32_t y, |
uint32_t y, |
438 |
uint32_t mb_width, |
uint32_t mb_width, |
440 |
{ |
{ |
441 |
|
|
442 |
int32_t j; |
int32_t j; |
443 |
int32_t iDcScaler, iQuant = frame->quant; |
int32_t iDcScaler, iQuant; |
444 |
int32_t S = 0; |
int S = 0; |
445 |
int16_t predictors[6][8]; |
int16_t predictors[6][8]; |
446 |
|
|
447 |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
448 |
|
iQuant = pMB->quant; |
449 |
|
|
450 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
451 |
|
|
452 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
453 |
{ |
iDcScaler = get_dc_scaler(iQuant, j<4); |
|
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
|
454 |
|
|
455 |
predict_acdc(frame->mbs, |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
456 |
x, |
iQuant, iDcScaler, predictors[j], 0); |
457 |
y, |
|
458 |
mb_width, |
if ((frame->vop_flags & XVID_VOP_HQACPRED)) |
459 |
j, |
S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
460 |
&qcoeff[j*64], |
else |
461 |
iQuant, |
S += calc_acdc_coeff(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
|
iDcScaler, |
|
|
predictors[j]); |
|
|
|
|
|
S += calc_acdc(pMB, |
|
|
j, |
|
|
&qcoeff[j*64], |
|
|
iDcScaler, |
|
|
predictors[j]); |
|
462 |
|
|
463 |
} |
} |
464 |
|
|
465 |
if (S < 0) // dont predict |
if (S<=0) { // dont predict |
|
{ |
|
466 |
for(j = 0; j < 6; j++) |
for(j = 0; j < 6; j++) |
|
{ |
|
467 |
pMB->acpred_directions[j] = 0; |
pMB->acpred_directions[j] = 0; |
468 |
} |
}else{ |
|
} |
|
|
else |
|
|
{ |
|
469 |
for(j = 0; j < 6; j++) |
for(j = 0; j < 6; j++) |
|
{ |
|
470 |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
471 |
} |
} |
472 |
} |
|
473 |
pMB->cbp = calc_cbp(qcoeff); |
pMB->cbp = calc_cbp(qcoeff); |
474 |
} |
} |
475 |
|
|