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 unsigned int bound_x, |
96 |
|
const unsigned int bound_y) |
97 |
|
|
98 |
{ |
{ |
99 |
|
const unsigned bound = (bound_y * mb_width) + bound_x; |
100 |
|
const unsigned mbpos = (y * mb_width) + x; |
101 |
int16_t *left, *top, *diag, *current; |
int16_t *left, *top, *diag, *current; |
102 |
|
|
103 |
int32_t left_quant = current_quant; |
int32_t left_quant = current_quant; |
117 |
|
|
118 |
// left macroblock |
// left macroblock |
119 |
|
|
120 |
if(x && (pMBs[index - 1].mode == MODE_INTRA |
if (x && mbpos >= bound + 1 && |
121 |
|| pMBs[index - 1].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1].mode == MODE_INTRA || |
122 |
|
pMBs[index - 1].mode == MODE_INTRA_Q)) { |
123 |
|
|
124 |
left = pMBs[index - 1].pred_values[0]; |
left = pMBs[index - 1].pred_values[0]; |
125 |
left_quant = pMBs[index - 1].quant; |
left_quant = pMBs[index - 1].quant; |
126 |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
//DEBUGI("LEFT", *(left+MBPRED_SIZE)); |
127 |
} |
} |
|
|
|
128 |
// top macroblock |
// top macroblock |
129 |
|
|
130 |
if(y && (pMBs[index - mb_width].mode == MODE_INTRA |
if (mbpos >= bound + mb_width && |
131 |
|| pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - mb_width].mode == MODE_INTRA || |
132 |
|
pMBs[index - mb_width].mode == MODE_INTRA_Q)) { |
133 |
|
|
134 |
top = pMBs[index - mb_width].pred_values[0]; |
top = pMBs[index - mb_width].pred_values[0]; |
135 |
top_quant = pMBs[index - mb_width].quant; |
top_quant = pMBs[index - mb_width].quant; |
136 |
} |
} |
|
|
|
137 |
// diag macroblock |
// diag macroblock |
138 |
|
|
139 |
if(x && y && (pMBs[index - 1 - mb_width].mode == MODE_INTRA |
if (x && mbpos >= bound + mb_width + 1 && |
140 |
|| pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
(pMBs[index - 1 - mb_width].mode == MODE_INTRA || |
141 |
|
pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { |
142 |
|
|
143 |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
diag = pMBs[index - 1 - mb_width].pred_values[0]; |
144 |
} |
} |
218 |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
if(ABS(pLeft[0] - pDiag[0]) < ABS(pDiag[0] - pTop[0])) { |
219 |
*acpred_direction = 1; // vertical |
*acpred_direction = 1; // vertical |
220 |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
predictors[0] = DIV_DIV(pTop[0], iDcScaler); |
221 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
222 |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
predictors[i] = rescale(top_quant, current_quant, pTop[i]); |
223 |
} |
} |
224 |
} |
} else { |
|
else |
|
|
{ |
|
225 |
*acpred_direction = 2; // horizontal |
*acpred_direction = 2; // horizontal |
226 |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
predictors[0] = DIV_DIV(pLeft[0], iDcScaler); |
227 |
for (i = 1; i < 8; i++) |
for (i = 1; i < 8; i++) { |
|
{ |
|
228 |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); |
229 |
} |
} |
230 |
} |
} |
236 |
*/ |
*/ |
237 |
|
|
238 |
|
|
239 |
void add_acdc(MACROBLOCK *pMB, |
void |
240 |
|
add_acdc(MACROBLOCK * pMB, |
241 |
uint32_t block, |
uint32_t block, |
242 |
int16_t dct_codes[64], |
int16_t dct_codes[64], |
243 |
uint32_t iDcScaler, |
uint32_t iDcScaler, |
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 |
dct_codes[i] = level; |
dct_codes[i] = level; |
258 |
pCurrent[i] = level; |
pCurrent[i] = level; |
259 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
260 |
} |
} |
261 |
} |
} else if (acpred_direction == 2) { |
262 |
else if (acpred_direction == 2) |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
263 |
int level = dct_codes[i*8] + predictors[i]; |
int level = dct_codes[i*8] + predictors[i]; |
264 |
|
|
265 |
dct_codes[i*8] = level; |
dct_codes[i*8] = level; |
266 |
pCurrent[i+7] = level; |
pCurrent[i+7] = level; |
267 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
268 |
} |
} |
269 |
} |
} else { |
270 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for (i = 1; i < 8; i++) |
|
|
{ |
|
271 |
pCurrent[i] = dct_codes[i]; |
pCurrent[i] = dct_codes[i]; |
272 |
pCurrent[i+7] = dct_codes[i*8]; |
pCurrent[i+7] = dct_codes[i*8]; |
273 |
} |
} |
289 |
S2 = sum of all qcoeff |
S2 = sum of all qcoeff |
290 |
*/ |
*/ |
291 |
|
|
292 |
uint32_t calc_acdc(MACROBLOCK *pMB, |
uint32_t |
293 |
|
calc_acdc(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, |
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; |
344 |
|
|
345 |
/* apply predictors[] to qcoeff */ |
/* apply predictors[] to qcoeff */ |
346 |
|
|
347 |
void apply_acdc(MACROBLOCK *pMB, |
void |
348 |
|
apply_acdc(MACROBLOCK * pMB, |
349 |
uint32_t block, |
uint32_t block, |
350 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
351 |
int16_t predictors[8]) |
int16_t predictors[8]) |
352 |
{ |
{ |
353 |
uint32_t i; |
uint32_t i; |
354 |
|
|
355 |
if (pMB->acpred_directions[block] == 1) |
if (pMB->acpred_directions[block] == 1) { |
356 |
{ |
for (i = 1; i < 8; i++) { |
|
for(i = 1; i < 8; i++) |
|
|
{ |
|
357 |
qcoeff[i] = predictors[i]; |
qcoeff[i] = predictors[i]; |
358 |
} |
} |
359 |
} |
} else { |
360 |
else |
for (i = 1; i < 8; i++) { |
|
{ |
|
|
for(i = 1; i < 8; i++) |
|
|
{ |
|
361 |
qcoeff[i*8] = predictors[i]; |
qcoeff[i*8] = predictors[i]; |
362 |
} |
} |
363 |
} |
} |
364 |
} |
} |
365 |
|
|
366 |
|
|
367 |
void MBPrediction(MBParam *pParam, |
void |
368 |
|
MBPrediction(FRAMEINFO * frame, |
369 |
uint32_t x, |
uint32_t x, |
370 |
uint32_t y, |
uint32_t y, |
371 |
uint32_t mb_width, |
uint32_t mb_width, |
372 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6 * 64]) |
|
MACROBLOCK *mbs) |
|
373 |
{ |
{ |
374 |
|
|
375 |
int32_t j; |
int32_t j; |
376 |
int32_t iDcScaler, iQuant = pParam->quant; |
int32_t iDcScaler, iQuant = frame->quant; |
377 |
int32_t S = 0; |
int32_t S = 0; |
378 |
int16_t predictors[6][8]; |
int16_t predictors[6][8]; |
379 |
|
|
380 |
MACROBLOCK *pMB = &mbs[x + y * mb_width]; |
MACROBLOCK *pMB = &frame->mbs[x + y * mb_width]; |
381 |
|
|
382 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q)) { |
383 |
|
|
384 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
|
{ |
|
385 |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
iDcScaler = get_dc_scaler(iQuant, (j < 4) ? 1 : 0); |
386 |
|
|
387 |
predict_acdc(mbs, |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
388 |
x, |
iQuant, iDcScaler, predictors[j], 0, 0); |
389 |
y, |
|
390 |
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]); |
|
391 |
|
|
392 |
} |
} |
393 |
|
|
394 |
if (S < 0) // dont predict |
if (S < 0) // dont predict |
395 |
{ |
{ |
396 |
for(j = 0; j < 6; j++) |
for (j = 0; j < 6; j++) { |
|
{ |
|
397 |
pMB->acpred_directions[j] = 0; |
pMB->acpred_directions[j] = 0; |
398 |
} |
} |
399 |
} |
} else { |
400 |
else |
for (j = 0; j < 6; j++) { |
|
{ |
|
|
for(j = 0; j < 6; j++) |
|
|
{ |
|
401 |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
apply_acdc(pMB, j, &qcoeff[j*64], predictors[j]); |
402 |
} |
} |
403 |
} |
} |