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 * |
60 |
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
61 |
|
|
62 |
|
|
63 |
static int __inline rescale(int predict_quant, int current_quant, int coeff) |
static int __inline |
64 |
|
rescale(int predict_quant, |
65 |
|
int current_quant, |
66 |
|
int coeff) |
67 |
{ |
{ |
68 |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), (current_quant)) : 0; |
return (coeff != 0) ? DIV_DIV((coeff) * (predict_quant), |
69 |
|
(current_quant)) : 0; |
70 |
} |
} |
71 |
|
|
72 |
|
|
82 |
*/ |
*/ |
83 |
|
|
84 |
|
|
85 |
void predict_acdc(MACROBLOCK *pMBs, |
void |
86 |
uint32_t x, uint32_t y, uint32_t mb_width, |
predict_acdc(MACROBLOCK * pMBs, |
87 |
|
uint32_t x, |
88 |
|
uint32_t y, |
89 |
|
uint32_t mb_width, |
90 |
uint32_t block, |
uint32_t block, |
91 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
92 |
uint32_t current_quant, |
uint32_t current_quant, |
93 |
int32_t iDcScaler, |
int32_t iDcScaler, |
94 |
int16_t predictors[8]) |
int16_t predictors[8], |
95 |
|
const int bound) |
96 |
|
|
97 |
{ |
{ |
98 |
|
const int mbpos = (y * mb_width) + x; |
99 |
int16_t *left, *top, *diag, *current; |
int16_t *left, *top, *diag, *current; |
100 |
|
|
101 |
int32_t left_quant = current_quant; |
int32_t left_quant = current_quant; |
115 |
|
|
116 |
// left macroblock |
// left macroblock |
117 |
|
|
118 |
if(x && (pMBs[index - 1].mode == MODE_INTRA |
if (x && mbpos >= bound + 1 && |
119 |
|| pMBs[index - 1].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1].mode == MODE_INTRA || |
120 |
|
pMBs[index - 1].mode == MODE_INTRA_Q)) { |
121 |
|
|
122 |
left = pMBs[index - 1].pred_values[0]; |
left = pMBs[index - 1].pred_values[0]; |
123 |
left_quant = pMBs[index - 1].quant; |
left_quant = pMBs[index - 1].quant; |
124 |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
125 |
} |
} |
|
|
|
126 |
// top macroblock |
// top macroblock |
127 |
|
|
128 |
if(y && (pMBs[index - mb_width].mode == MODE_INTRA |
if (mbpos >= bound + (int)mb_width && |
129 |
|| pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - mb_width].mode == MODE_INTRA || |
130 |
|
pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
131 |
|
|
132 |
top = pMBs[index - mb_width].pred_values[0]; |
top = pMBs[index - mb_width].pred_values[0]; |
133 |
top_quant = pMBs[index - mb_width].quant; |
top_quant = pMBs[index - mb_width].quant; |
134 |
} |
} |
|
|
|
135 |
// diag macroblock |
// diag macroblock |
136 |
|
|
137 |
if(x && y && (pMBs[index - 1 - mb_width].mode == MODE_INTRA |
if (x && mbpos >= bound + (int)mb_width + 1 && |
138 |
|| pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
139 |
|
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
140 |
|
|
141 |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
142 |
} |
} |
216 |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
217 |
*acpred_direction = 1; // vertical |
*acpred_direction = 1; // vertical |
218 |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
219 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
220 |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
221 |
} |
} |
222 |
} |
} else { |
|
else |
|
|
{ |
|
223 |
*acpred_direction = 2; // horizontal |
*acpred_direction = 2; // horizontal |
224 |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
225 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
226 |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
227 |
} |
} |
228 |
} |
} |
234 |
*/ |
*/ |
235 |
|
|
236 |
|
|
237 |
void add_acdc(MACROBLOCK *pMB, |
void |
238 |
|
add_acdc(MACROBLOCK * pMB, |
239 |
uint32_t block, |
uint32_t block, |
240 |
int16_t dct_codes[64], |
int16_t dct_codes[64], |
241 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
245 |
int16_t * pCurrent = pMB->pred_values[block]; |
int16_t * pCurrent = pMB->pred_values[block]; |
246 |
uint32_t i; |
uint32_t i; |
247 |
|
|
248 |
|
DPRINTF(DPRINTF_COEFF,"predictor[0] %i", predictors[0]); |
249 |
|
|
250 |
dct_codes[0] += predictors[0]; // dc prediction |
dct_codes[0] += predictors[0]; // dc prediction |
251 |
pCurrent[0] = dct_codes[0] * iDcScaler; |
pCurrent[0] = dct_codes[0] * iDcScaler; |
252 |
|
|
253 |
if (acpred_direction == 1) |
if (acpred_direction == 1) { |
254 |
{ |
for (i = 1; i < 8; i++) { |
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
255 |
int level = dct_codes[i] + predictors[i]; |
int level = dct_codes[i] + predictors[i]; |
256 |
|
|
257 |
|
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i, predictors[i]); |
258 |
|
|
259 |
dct_codes[i] = level; |
dct_codes[i] = level; |
260 |
pCurrent[i] = level; |
pCurrent[i] = level; |
261 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
262 |
} |
} |
263 |
} |
} else if (acpred_direction == 2) { |
264 |
else if (acpred_direction == 2) |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
265 |
int level = dct_codes[i*8] + predictors[i]; |
int level = dct_codes[i*8] + predictors[i]; |
266 |
|
DPRINTF(DPRINTF_COEFF,"predictor[%i] %i",i*8, predictors[i]); |
267 |
|
|
268 |
dct_codes[i*8] = level; |
dct_codes[i*8] = level; |
269 |
pCurrent[i+7] = level; |
pCurrent[i+7] = level; |
270 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
271 |
} |
} |
272 |
} |
} else { |
273 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
274 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
275 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
276 |
} |
} |
292 |
S2 = sum of all qcoeff |
S2 = sum of all qcoeff |
293 |
*/ |
*/ |
294 |
|
|
295 |
uint32_t calc_acdc(MACROBLOCK *pMB, |
uint32_t |
296 |
|
calc_acdc(MACROBLOCK * pMB, |
297 |
uint32_t block, |
uint32_t block, |
298 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
299 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
316 |
|
|
317 |
qcoeff[0] = qcoeff[0] - predictors[0]; |
qcoeff[0] = qcoeff[0] - predictors[0]; |
318 |
|
|
319 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
|
{ |
|
320 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
321 |
int16_t level; |
int16_t level; |
322 |
|
|
326 |
S1 += ABS(level); |
S1 += ABS(level); |
327 |
predictors[i] = level; |
predictors[i] = level; |
328 |
} |
} |
329 |
} |
} else // acpred_direction == 2 |
|
else // acpred_direction == 2 |
|
330 |
{ |
{ |
331 |
for(i = 1; i < 8; i++) { |
for(i = 1; i < 8; i++) { |
332 |
int16_t level; |
int16_t level; |
347 |
|
|
348 |
/* apply predictors[] to qcoeff */ |
/* apply predictors[] to qcoeff */ |
349 |
|
|
350 |
void apply_acdc(MACROBLOCK *pMB, |
void |
351 |
|
apply_acdc(MACROBLOCK * pMB, |
352 |
uint32_t block, |
uint32_t block, |
353 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
354 |
int16_t predictors[8]) |
int16_t predictors[8]) |
355 |
{ |
{ |
356 |
uint32_t i; |
uint32_t i; |
357 |
|
|
358 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
359 |
{ |
for (i = 1; i < 8; i++) { |
|
for(i = 1; i < 8; i++) |
|
|
{ |
|
360 |
qcoeff[i] = predictors[i]; |
qcoeff[i] = predictors[i]; |
361 |
} |
} |
362 |
} |
} else { |
363 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for(i = 1; i < 8; i++) |
|
|
{ |
|
364 |
qcoeff[i*8] = predictors[i]; |
qcoeff[i*8] = predictors[i]; |
365 |
} |
} |
366 |
} |
} |
367 |
} |
} |
368 |
|
|
369 |
|
|
370 |
void MBPrediction(MBParam *pParam, |
void |
371 |
|
MBPrediction(FRAMEINFO * frame, |
372 |
uint32_t x, |
uint32_t x, |
373 |
uint32_t y, |
uint32_t y, |
374 |
uint32_t mb_width, |
uint32_t mb_width, |
375 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6 * 64]) |
|
MACROBLOCK *mbs) |
|
376 |
{ |
{ |
377 |
|
|
378 |
int32_t j; |
int32_t j; |
379 |
int32_t iDcScaler, iQuant = pParam->quant; |
int32_t iDcScaler, iQuant = frame->quant; |
380 |
int32_t S = 0; |
int32_t S = 0; |
381 |
int16_t predictors[6][8]; |
int16_t predictors[6][8]; |
382 |
|
|
383 |
MACROBLOCK *pMB = &mbs[x + y * mb_width]; |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
384 |
|
|
385 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
386 |
|
|
387 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
|
{ |
|
388 |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
389 |
|
|
390 |
predict_acdc(mbs, |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
391 |
x, |
iQuant, iDcScaler, predictors[j], 0); |
392 |
y, |
|
393 |
mb_width, |
S += calc_acdc(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
|
j, |
|
|
&qcoeff[j*64], |
|
|
iQuant, |
|
|
iDcScaler, |
|
|
predictors[j]); |
|
|
|
|
|
S += calc_acdc(pMB, |
|
|
j, |
|
|
&qcoeff[j*64], |
|
|
iDcScaler, |
|
|
predictors[j]); |
|
394 |
|
|
395 |
} |
} |
396 |
|
|
397 |
if (S < 0) // dont predict |
if (S < 0) // dont predict |
398 |
{ |
{ |
399 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
|
{ |
|
400 |
pMB->acpred_directions[j] = 0; |
pMB->acpred_directions[j] = 0; |
401 |
} |
} |
402 |
} |
} else { |
403 |
else |
for (j = 0; j < 6; j++) { |
|
{ |
|
|
for(j = 0; j < 6; j++) |
|
|
{ |
|
404 |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
405 |
} |
} |
406 |
} |
} |