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 "../global.h" |
54 |
#include "../encoder.h" |
#include "../encoder.h" |
55 |
#include "mbprediction.h" |
#include "mbprediction.h" |
56 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
57 |
#include "../bitstream/cbp.h" |
#include "../bitstream/cbp.h" |
58 |
|
#include "../bitstream/mbcoding.h" |
59 |
|
#include "../bitstream/zigzag.h" |
60 |
|
|
61 |
|
|
62 |
#define ABS(X) (((X)>0)?(X):-(X)) |
static int __inline |
63 |
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
rescale(int predict_quant, |
64 |
|
int current_quant, |
65 |
|
int coeff) |
|
static int __inline rescale(int predict_quant, int current_quant, int coeff) |
|
66 |
{ |
{ |
67 |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), (current_quant)) : 0; |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), |
68 |
|
(current_quant)) : 0; |
69 |
} |
} |
70 |
|
|
71 |
|
|
81 |
*/ |
*/ |
82 |
|
|
83 |
|
|
84 |
void predict_acdc(MACROBLOCK *pMBs, |
void |
85 |
uint32_t x, uint32_t y, uint32_t mb_width, |
predict_acdc(MACROBLOCK * pMBs, |
86 |
|
uint32_t x, |
87 |
|
uint32_t y, |
88 |
|
uint32_t mb_width, |
89 |
uint32_t block, |
uint32_t block, |
90 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
91 |
uint32_t current_quant, |
uint32_t current_quant, |
92 |
int32_t iDcScaler, |
int32_t iDcScaler, |
93 |
int16_t predictors[8]) |
int16_t predictors[8], |
94 |
|
const int bound) |
95 |
|
|
96 |
{ |
{ |
97 |
|
const int mbpos = (y * mb_width) + x; |
98 |
int16_t *left, *top, *diag, *current; |
int16_t *left, *top, *diag, *current; |
99 |
|
|
100 |
int32_t left_quant = current_quant; |
int32_t left_quant = current_quant; |
114 |
|
|
115 |
// left macroblock |
// left macroblock |
116 |
|
|
117 |
if(x && (pMBs[index - 1].mode == MODE_INTRA |
if (x && mbpos >= bound + 1 && |
118 |
|| pMBs[index - 1].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1].mode == MODE_INTRA || |
119 |
|
pMBs[index - 1].mode == MODE_INTRA_Q)) { |
120 |
|
|
121 |
left = pMBs[index - 1].pred_values[0]; |
left = pMBs[index - 1].pred_values[0]; |
122 |
left_quant = pMBs[index - 1].quant; |
left_quant = pMBs[index - 1].quant; |
123 |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
124 |
} |
} |
|
|
|
125 |
// top macroblock |
// top macroblock |
126 |
|
|
127 |
if(y && (pMBs[index - mb_width].mode == MODE_INTRA |
if (mbpos >= bound + (int)mb_width && |
128 |
|| pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - mb_width].mode == MODE_INTRA || |
129 |
|
pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
130 |
|
|
131 |
top = pMBs[index - mb_width].pred_values[0]; |
top = pMBs[index - mb_width].pred_values[0]; |
132 |
top_quant = pMBs[index - mb_width].quant; |
top_quant = pMBs[index - mb_width].quant; |
133 |
} |
} |
|
|
|
134 |
// diag macroblock |
// diag macroblock |
135 |
|
|
136 |
if(x && y && (pMBs[index - 1 - mb_width].mode == MODE_INTRA |
if (x && mbpos >= bound + (int)mb_width + 1 && |
137 |
|| pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
138 |
|
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
139 |
|
|
140 |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
141 |
} |
} |
215 |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
216 |
*acpred_direction = 1; // vertical |
*acpred_direction = 1; // vertical |
217 |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
218 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
219 |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
220 |
} |
} |
221 |
} |
} else { |
|
else |
|
|
{ |
|
222 |
*acpred_direction = 2; // horizontal |
*acpred_direction = 2; // horizontal |
223 |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
224 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
225 |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
226 |
} |
} |
227 |
} |
} |
233 |
*/ |
*/ |
234 |
|
|
235 |
|
|
236 |
void add_acdc(MACROBLOCK *pMB, |
void |
237 |
|
add_acdc(MACROBLOCK * pMB, |
238 |
uint32_t block, |
uint32_t block, |
239 |
int16_t dct_codes[64], |
int16_t dct_codes[64], |
240 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
244 |
int16_t * pCurrent = pMB->pred_values[block]; |
int16_t * pCurrent = pMB->pred_values[block]; |
245 |
uint32_t i; |
uint32_t i; |
246 |
|
|
247 |
|
DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]); |
248 |
|
|
249 |
dct_codes[0] += predictors[0]; // dc prediction |
dct_codes[0] += predictors[0]; // dc prediction |
250 |
pCurrent[0] = dct_codes[0] * iDcScaler; |
pCurrent[0] = dct_codes[0] * iDcScaler; |
251 |
|
|
252 |
if (acpred_direction == 1) |
if (acpred_direction == 1) { |
253 |
{ |
for (i = 1; i < 8; i++) { |
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
254 |
int level = dct_codes[i] + predictors[i]; |
int level = dct_codes[i] + predictors[i]; |
255 |
|
|
256 |
|
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]); |
257 |
|
|
258 |
dct_codes[i] = level; |
dct_codes[i] = level; |
259 |
pCurrent[i] = level; |
pCurrent[i] = level; |
260 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
261 |
} |
} |
262 |
} |
} else if (acpred_direction == 2) { |
263 |
else if (acpred_direction == 2) |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
264 |
int level = dct_codes[i*8] + predictors[i]; |
int level = dct_codes[i*8] + predictors[i]; |
265 |
|
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]); |
266 |
|
|
267 |
dct_codes[i*8] = level; |
dct_codes[i*8] = level; |
268 |
pCurrent[i+7] = level; |
pCurrent[i+7] = level; |
269 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
270 |
} |
} |
271 |
} |
} else { |
272 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
273 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
274 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
275 |
} |
} |
283 |
|
|
284 |
/* encoder: subtract predictors from qcoeff[] and calculate S1/S2 |
/* encoder: subtract predictors from qcoeff[] and calculate S1/S2 |
285 |
|
|
286 |
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 |
|
287 |
|
|
|
S1/S2 are used to determine if its worth predicting for AC |
|
288 |
S1 = sum of all (qcoeff - prediction) |
S1 = sum of all (qcoeff - prediction) |
289 |
S2 = sum of all qcoeff |
S2 = sum of all qcoeff |
290 |
*/ |
*/ |
291 |
|
|
292 |
uint32_t calc_acdc(MACROBLOCK *pMB, |
int |
293 |
|
calc_acdc_coeff(MACROBLOCK * pMB, |
294 |
uint32_t block, |
uint32_t block, |
295 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
296 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
298 |
{ |
{ |
299 |
int16_t * pCurrent = pMB->pred_values[block]; |
int16_t * pCurrent = pMB->pred_values[block]; |
300 |
uint32_t i; |
uint32_t i; |
301 |
uint32_t S1 = 0, S2 = 0; |
int S1 = 0, S2 = 0; |
302 |
|
|
303 |
|
|
304 |
/* store current coeffs to pred_values[] for future prediction */ |
/* store current coeffs to pred_values[] for future prediction */ |
313 |
|
|
314 |
qcoeff[0] = qcoeff[0] - predictors[0]; |
qcoeff[0] = qcoeff[0] - predictors[0]; |
315 |
|
|
316 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
|
{ |
|
317 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
318 |
int16_t level; |
int16_t level; |
319 |
|
|
323 |
S1 += ABS(level); |
S1 += ABS(level); |
324 |
predictors[i] = level; |
predictors[i] = level; |
325 |
} |
} |
326 |
} |
} else // acpred_direction == 2 |
|
else // acpred_direction == 2 |
|
327 |
{ |
{ |
328 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
329 |
int16_t level; |
int16_t level; |
342 |
} |
} |
343 |
|
|
344 |
|
|
|
/* apply predictors[] to qcoeff */ |
|
345 |
|
|
346 |
void apply_acdc(MACROBLOCK *pMB, |
/* returns the bits *saved* if prediction is enabled */ |
347 |
|
|
348 |
|
int |
349 |
|
calc_acdc_bits(MACROBLOCK * pMB, |
350 |
uint32_t block, |
uint32_t block, |
351 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
352 |
|
uint32_t iDcScaler, |
353 |
int16_t predictors[8]) |
int16_t predictors[8]) |
354 |
{ |
{ |
355 |
uint32_t i; |
const int direction = pMB->acpred_directions[block]; |
356 |
|
int16_t *pCurrent = pMB->pred_values[block]; |
357 |
|
int16_t tmp[8]; |
358 |
|
unsigned int i; |
359 |
|
int Z1, Z2; |
360 |
|
|
361 |
if (pMB->acpred_directions[block] == 1) |
/* store current coeffs to pred_values[] for future prediction */ |
362 |
{ |
pCurrent[0] = qcoeff[0] * iDcScaler; |
363 |
|
for (i = 1; i < 8; i++) { |
364 |
|
pCurrent[i] = qcoeff[i]; |
365 |
|
pCurrent[i + 7] = qcoeff[i * 8]; |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
|
/* dc prediction */ |
370 |
|
qcoeff[0] = qcoeff[0] - predictors[0]; |
371 |
|
|
372 |
|
/* calc cost before ac prediction */ |
373 |
|
#ifdef BIGLUT |
374 |
|
Z2 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[0], 1); |
375 |
|
#else |
376 |
|
Z2 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
377 |
|
#endif |
378 |
|
|
379 |
|
/* apply ac prediction & calc cost*/ |
380 |
|
if (direction == 1) { |
381 |
|
for (i = 1; i < 8; i++) { |
382 |
|
tmp[i] = qcoeff[i]; |
383 |
|
qcoeff[i] -= predictors[i]; |
384 |
|
predictors[i] = qcoeff[i]; |
385 |
|
} |
386 |
|
}else{ // acpred_direction == 2 |
387 |
|
for (i = 1; i < 8; i++) { |
388 |
|
tmp[i] = qcoeff[i*8]; |
389 |
|
qcoeff[i*8] -= predictors[i]; |
390 |
|
predictors[i] = qcoeff[i*8]; |
391 |
|
} |
392 |
|
} |
393 |
|
|
394 |
|
#ifdef BIGLUT |
395 |
|
Z1 = CodeCoeff_CalcBits(qcoeff, intra_table, scan_tables[direction], 1); |
396 |
|
#else |
397 |
|
Z1 = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
398 |
|
#endif |
399 |
|
|
400 |
|
/* undo prediction */ |
401 |
|
if (direction == 1) { |
402 |
for(i = 1; i < 8; i++) |
for(i = 1; i < 8; i++) |
403 |
{ |
qcoeff[i] = tmp[i]; |
404 |
qcoeff[i] = predictors[i]; |
}else{ // acpred_direction == 2 |
405 |
|
for (i = 1; i < 8; i++) |
406 |
|
qcoeff[i*8] = tmp[i]; |
407 |
} |
} |
408 |
|
|
409 |
|
return Z2-Z1; |
410 |
} |
} |
411 |
else |
|
412 |
|
/* apply predictors[] to qcoeff */ |
413 |
|
|
414 |
|
void |
415 |
|
apply_acdc(MACROBLOCK * pMB, |
416 |
|
uint32_t block, |
417 |
|
int16_t qcoeff[64], |
418 |
|
int16_t predictors[8]) |
419 |
{ |
{ |
420 |
|
unsigned int i; |
421 |
|
|
422 |
|
if (pMB->acpred_directions[block] == 1) { |
423 |
|
for (i = 1; i < 8; i++) |
424 |
|
qcoeff[i] = predictors[i]; |
425 |
|
} else { |
426 |
for(i = 1; i < 8; i++) |
for(i = 1; i < 8; i++) |
|
{ |
|
427 |
qcoeff[i*8] = predictors[i]; |
qcoeff[i*8] = predictors[i]; |
428 |
} |
} |
429 |
} |
} |
|
} |
|
430 |
|
|
431 |
|
|
432 |
void MBPrediction(FRAMEINFO *frame, |
void |
433 |
|
MBPrediction(FRAMEINFO * frame, |
434 |
uint32_t x, |
uint32_t x, |
435 |
uint32_t y, |
uint32_t y, |
436 |
uint32_t mb_width, |
uint32_t mb_width, |
439 |
|
|
440 |
int32_t j; |
int32_t j; |
441 |
int32_t iDcScaler, iQuant = frame->quant; |
int32_t iDcScaler, iQuant = frame->quant; |
442 |
int32_t S = 0; |
int S = 0; |
443 |
int16_t predictors[6][8]; |
int16_t predictors[6][8]; |
444 |
|
|
445 |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
446 |
|
|
447 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
448 |
|
|
449 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
450 |
{ |
iDcScaler = get_dc_scaler(iQuant, j<4); |
|
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
|
451 |
|
|
452 |
predict_acdc(frame->mbs, |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
453 |
x, |
iQuant, iDcScaler, predictors[j], 0); |
454 |
y, |
|
455 |
mb_width, |
if ((frame->global_flags & XVID_HQACPRED)) |
456 |
j, |
S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
457 |
&qcoeff[j*64], |
else |
458 |
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]); |
|
459 |
|
|
460 |
} |
} |
461 |
|
|
462 |
if (S < 0) // dont predict |
if (S<=0) { // dont predict |
|
{ |
|
463 |
for(j = 0; j < 6; j++) |
for(j = 0; j < 6; j++) |
|
{ |
|
464 |
pMB->acpred_directions[j] = 0; |
pMB->acpred_directions[j] = 0; |
465 |
} |
}else{ |
|
} |
|
|
else |
|
|
{ |
|
466 |
for(j = 0; j < 6; j++) |
for(j = 0; j < 6; j++) |
|
{ |
|
467 |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
468 |
} |
} |
469 |
} |
|
470 |
pMB->cbp = calc_cbp(qcoeff); |
pMB->cbp = calc_cbp(qcoeff); |
471 |
} |
} |
472 |
|
|