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" |
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
|
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
|
60 |
|
|
61 |
|
|
62 |
static int __inline |
static int __inline |
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 |
int |
293 |
calc_acdc(MACROBLOCK * pMB, |
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 */ |
342 |
} |
} |
343 |
|
|
344 |
|
|
345 |
|
|
346 |
|
/* returns the bits *saved* if prediction is enabled */ |
347 |
|
|
348 |
|
int |
349 |
|
calc_acdc_bits(MACROBLOCK * pMB, |
350 |
|
uint32_t block, |
351 |
|
int16_t qcoeff[64], |
352 |
|
uint32_t iDcScaler, |
353 |
|
int16_t predictors[8]) |
354 |
|
{ |
355 |
|
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 |
|
/* 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++) |
403 |
|
qcoeff[i] = tmp[i]; |
404 |
|
}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 |
|
|
412 |
/* apply predictors[] to qcoeff */ |
/* apply predictors[] to qcoeff */ |
413 |
|
|
414 |
void |
void |
417 |
int16_t qcoeff[64], |
int16_t qcoeff[64], |
418 |
int16_t predictors[8]) |
int16_t predictors[8]) |
419 |
{ |
{ |
420 |
uint32_t i; |
unsigned int i; |
421 |
|
|
422 |
if (pMB->acpred_directions[block] == 1) { |
if (pMB->acpred_directions[block] == 1) { |
423 |
for (i = 1; i < 8; i++) { |
for (i = 1; i < 8; i++) |
424 |
qcoeff[i] = predictors[i]; |
qcoeff[i] = predictors[i]; |
|
} |
|
425 |
} else { |
} 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 |
void |
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]; |
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) ? 1 : 0); |
iDcScaler = get_dc_scaler(iQuant, j<4); |
451 |
|
|
452 |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], |
453 |
iQuant, iDcScaler, predictors[j], 0); |
iQuant, iDcScaler, predictors[j], 0); |
454 |
|
|
455 |
S += calc_acdc(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
if ((frame->global_flags & XVID_HQACPRED)) |
456 |
|
S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); |
457 |
|
else |
458 |
|
S += calc_acdc_coeff(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 |
} |
} |
|
} |
|
|
pMB->cbp = calc_cbp(qcoeff); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
get_pmvdata2: get_pmvdata with bounding |
|
|
*/ |
|
|
#define OFFSET(x,y,stride) ((x)+((y)*(stride))) |
|
|
|
|
|
int |
|
|
get_pmvdata2(const MACROBLOCK * const pMBs, |
|
|
const uint32_t x, |
|
|
const uint32_t y, |
|
|
const uint32_t x_dim, |
|
|
const uint32_t block, |
|
|
VECTOR * const pmv, |
|
|
int32_t * const psad, |
|
|
const int bound) |
|
|
{ |
|
|
const int mbpos = OFFSET(x, y ,x_dim); |
|
|
|
|
|
/* |
|
|
* pmv are filled with: |
|
|
* [0]: Median (or whatever is correct in a special case) |
|
|
* [1]: left neighbour |
|
|
* [2]: top neighbour |
|
|
* [3]: topright neighbour |
|
|
* psad are filled with: |
|
|
* [0]: minimum of [1] to [3] |
|
|
* [1]: left neighbour's SAD (NB:[1] to [3] are actually not needed) |
|
|
* [2]: top neighbour's SAD |
|
|
* [3]: topright neighbour's SAD |
|
|
*/ |
|
|
|
|
|
int xin1, xin2, xin3; |
|
|
int yin1, yin2, yin3; |
|
|
int vec1, vec2, vec3; |
|
|
|
|
|
int pos1, pos2, pos3; |
|
|
int num_cand = 0; // number of candidates |
|
|
int last_cand; // last candidate |
|
|
|
|
|
uint32_t index = x + y * x_dim; |
|
|
const VECTOR zeroMV = { 0, 0 }; |
|
|
|
|
|
/* |
|
|
* MODE_INTER, vm18 page 48 |
|
|
* MODE_INTER4V vm18 page 51 |
|
|
* |
|
|
* (x,y-1) (x+1,y-1) |
|
|
* [ | ] [ | ] |
|
|
* [ 2 | 3 ] [ 2 | ] |
|
|
* |
|
|
* (x-1,y) (x,y) (x+1,y) |
|
|
* [ | 1 ] [ 0 | 1 ] [ 0 | ] |
|
|
* [ | 3 ] [ 2 | 3 ] [ | ] |
|
|
*/ |
|
|
|
|
|
switch (block) { |
|
|
case 0: |
|
|
xin1 = x - 1; |
|
|
yin1 = y; |
|
|
vec1 = 1; /* left */ |
|
|
xin2 = x; |
|
|
yin2 = y - 1; |
|
|
vec2 = 2; /* top */ |
|
|
xin3 = x + 1; |
|
|
yin3 = y - 1; |
|
|
vec3 = 2; /* top right */ |
|
|
break; |
|
|
case 1: |
|
|
xin1 = x; |
|
|
yin1 = y; |
|
|
vec1 = 0; |
|
|
xin2 = x; |
|
|
yin2 = y - 1; |
|
|
vec2 = 3; |
|
|
xin3 = x + 1; |
|
|
yin3 = y - 1; |
|
|
vec3 = 2; |
|
|
break; |
|
|
case 2: |
|
|
xin1 = x - 1; |
|
|
yin1 = y; |
|
|
vec1 = 3; |
|
|
xin2 = x; |
|
|
yin2 = y; |
|
|
vec2 = 0; |
|
|
xin3 = x; |
|
|
yin3 = y; |
|
|
vec3 = 1; |
|
|
break; |
|
|
default: |
|
|
xin1 = x; |
|
|
yin1 = y; |
|
|
vec1 = 2; |
|
|
xin2 = x; |
|
|
yin2 = y; |
|
|
vec2 = 0; |
|
|
xin3 = x; |
|
|
yin3 = y; |
|
|
vec3 = 1; |
|
|
} |
|
|
|
|
|
pos1 = OFFSET(xin1, yin1, x_dim); |
|
|
pos2 = OFFSET(xin2, yin2, x_dim); |
|
|
pos3 = OFFSET(xin3, yin3, x_dim); |
|
|
|
|
|
// left |
|
|
if (xin1 < 0 || pos1 < bound) { |
|
|
pmv[1] = zeroMV; |
|
|
psad[1] = MV_MAX_ERROR; |
|
|
} else { |
|
|
pmv[1] = pMBs[xin1 + yin1 * x_dim].mvs[vec1]; |
|
|
psad[1] = pMBs[xin1 + yin1 * x_dim].sad8[vec1]; |
|
|
num_cand++; |
|
|
last_cand = 1; |
|
|
} |
|
|
|
|
|
// top |
|
|
if (yin2 < 0 || pos2 < bound) { |
|
|
pmv[2] = zeroMV; |
|
|
psad[2] = MV_MAX_ERROR; |
|
|
} else { |
|
|
pmv[2] = pMBs[xin2 + yin2 * x_dim].mvs[vec2]; |
|
|
psad[2] = pMBs[xin2 + yin2 * x_dim].sad8[vec2]; |
|
|
num_cand++; |
|
|
last_cand = 2; |
|
|
} |
|
|
|
|
|
|
|
|
// top right |
|
|
if (yin3 < 0 || pos3 < bound || xin3 >= (int)x_dim) { |
|
|
pmv[3] = zeroMV; |
|
|
psad[3] = MV_MAX_ERROR; |
|
|
//DPRINTF(DPRINTF_MV, "top-right"); |
|
|
} else { |
|
|
pmv[3] = pMBs[xin3 + yin3 * x_dim].mvs[vec3]; |
|
|
psad[3] = pMBs[xin2 + yin2 * x_dim].sad8[vec3]; |
|
|
num_cand++; |
|
|
last_cand = 3; |
|
|
} |
|
|
|
|
|
if (num_cand == 1) |
|
|
{ |
|
|
/* DPRINTF(DPRINTF_MV,"cand0=(%i,%i), cand1=(%i,%i) cand2=(%i,%i) last=%i", |
|
|
pmv[1].x, pmv[1].y, |
|
|
pmv[2].x, pmv[2].y, |
|
|
pmv[3].x, pmv[3].y, last_cand - 1); |
|
|
*/ |
|
469 |
|
|
470 |
pmv[0] = pmv[last_cand]; |
pMB->cbp = calc_cbp(qcoeff); |
|
psad[0] = psad[last_cand]; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
/* DPRINTF(DPRINTF_MV,"cand0=(%i,%i), cand1=(%i,%i) cand2=(%i,%i)", |
|
|
pmv[1].x, pmv[1].y, |
|
|
pmv[2].x, pmv[2].y, |
|
|
pmv[3].x, pmv[3].y);*/ |
|
|
|
|
|
if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) { |
|
|
pmv[0] = pmv[1]; |
|
|
psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]); |
|
|
return 1; |
|
471 |
} |
} |
472 |
|
|
|
/* median,minimum */ |
|
|
|
|
|
pmv[0].x = |
|
|
MIN(MAX(pmv[1].x, pmv[2].x), |
|
|
MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x))); |
|
|
pmv[0].y = |
|
|
MIN(MAX(pmv[1].y, pmv[2].y), |
|
|
MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y))); |
|
|
psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]); |
|
|
|
|
|
return 0; |
|
473 |
} |
} |