--- mbprediction.c 2004/05/21 14:40:15 1.15 +++ mbprediction.c 2010/08/10 15:00:21 1.19 @@ -20,7 +20,7 @@ * along with this program ; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: mbprediction.c,v 1.15 2004/05/21 14:40:15 edgomez Exp $ + * $Id: mbprediction.c,v 1.19 2010/08/10 15:00:21 Isibaar Exp $ * ****************************************************************************/ @@ -67,8 +67,7 @@ uint32_t current_quant, int32_t iDcScaler, int16_t predictors[8], - const int bound, - const int bsversion) + const int bound) { const int mbpos = (y * mb_width) + x; @@ -85,7 +84,7 @@ int *acpred_direction = &pMBs[index].acpred_directions[block]; uint32_t i; - left = top = diag = current = 0; + left = top = diag = current = NULL; /* grab left,top and diag macroblocks */ @@ -95,7 +94,7 @@ (pMBs[index - 1].mode == MODE_INTRA || pMBs[index - 1].mode == MODE_INTRA_Q)) { - left = pMBs[index - 1].pred_values[0]; + left = (int16_t*)pMBs[index - 1].pred_values[0]; left_quant = pMBs[index - 1].quant; } /* top macroblock */ @@ -104,7 +103,7 @@ (pMBs[index - mb_width].mode == MODE_INTRA || pMBs[index - mb_width].mode == MODE_INTRA_Q)) { - top = pMBs[index - mb_width].pred_values[0]; + top = (int16_t*)pMBs[index - mb_width].pred_values[0]; top_quant = pMBs[index - mb_width].quant; } /* diag macroblock */ @@ -113,10 +112,10 @@ (pMBs[index - 1 - mb_width].mode == MODE_INTRA || pMBs[index - 1 - mb_width].mode == MODE_INTRA_Q)) { - diag = pMBs[index - 1 - mb_width].pred_values[0]; + diag = (int16_t*)pMBs[index - 1 - mb_width].pred_values[0]; } - current = pMBs[index].pred_values[0]; + current = (int16_t*)pMBs[index].pred_values[0]; /* now grab pLeft, pTop, pDiag _blocks_ */ @@ -187,24 +186,15 @@ /* determine ac prediction direction & ac/dc predictor place rescaled ac/dc * predictions into predictors[] for later use */ - - /* Workaround: Bitstream versions <= 32 used to have a wrong predictor - * stored as it wasn't clipped to the [-2048, 2047] range. We only - * use the right predictors for bs versions > 32 */ -#define BUGGY_CLIPPING_BS_VERSION 32 if (abs(pLeft[0] - pDiag[0]) < abs(pDiag[0] - pTop[0])) { *acpred_direction = 1; /* vertical */ predictors[0] = DIV_DIV(pTop[0], iDcScaler); - if (bsversion == 0 || bsversion > BUGGY_CLIPPING_BS_VERSION) - predictors[0] = CLIP(predictors[0], -2048, 2047); for (i = 1; i < 8; i++) { predictors[i] = rescale(top_quant, current_quant, pTop[i]); } } else { *acpred_direction = 2; /* horizontal */ predictors[0] = DIV_DIV(pLeft[0], iDcScaler); - if (bsversion == 0 || bsversion > BUGGY_CLIPPING_BS_VERSION) - predictors[0] = CLIP(predictors[0], -2048, 2047); for (i = 1; i < 8; i++) { predictors[i] = rescale(left_quant, current_quant, pLeft[i + 7]); } @@ -216,22 +206,29 @@ store current coeffs to pred_values[] for future prediction */ +/* Up to this version, no DC clipping was performed, so we try to be backward + * compatible to avoid artifacts */ +#define BS_VERSION_BUGGY_DC_CLIPPING 34 void add_acdc(MACROBLOCK * pMB, uint32_t block, int16_t dct_codes[64], uint32_t iDcScaler, - int16_t predictors[8]) + int16_t predictors[8], + const int bsversion) { uint8_t acpred_direction = pMB->acpred_directions[block]; - int16_t *pCurrent = pMB->pred_values[block]; + int16_t *pCurrent = (int16_t*)pMB->pred_values[block]; uint32_t i; DPRINTF(XVID_DEBUG_COEFF,"predictor[0] %i\n", predictors[0]); dct_codes[0] += predictors[0]; /* dc prediction */ - pCurrent[0] = dct_codes[0] * iDcScaler; + pCurrent[0] = dct_codes[0]*iDcScaler; + if (bsversion > BS_VERSION_BUGGY_DC_CLIPPING) { + pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); + } if (acpred_direction == 1) { for (i = 1; i < 8; i++) { @@ -273,14 +270,14 @@ S2 = sum of all qcoeff */ -int +static int calc_acdc_coeff(MACROBLOCK * pMB, uint32_t block, int16_t qcoeff[64], uint32_t iDcScaler, int16_t predictors[8]) { - int16_t *pCurrent = pMB->pred_values[block]; + int16_t *pCurrent = (int16_t*)pMB->pred_values[block]; uint32_t i; int S1 = 0, S2 = 0; @@ -288,6 +285,7 @@ /* store current coeffs to pred_values[] for future prediction */ pCurrent[0] = qcoeff[0] * iDcScaler; + pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); for (i = 1; i < 8; i++) { pCurrent[i] = qcoeff[i]; pCurrent[i + 7] = qcoeff[i * 8]; @@ -329,7 +327,7 @@ /* returns the bits *saved* if prediction is enabled */ -int +static int calc_acdc_bits(MACROBLOCK * pMB, uint32_t block, int16_t qcoeff[64], @@ -337,13 +335,14 @@ int16_t predictors[8]) { const int direction = pMB->acpred_directions[block]; - int16_t *pCurrent = pMB->pred_values[block]; + int16_t *pCurrent = (int16_t*)pMB->pred_values[block]; int16_t tmp[8]; unsigned int i; int Z1, Z2; /* store current coeffs to pred_values[] for future prediction */ pCurrent[0] = qcoeff[0] * iDcScaler; + pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); for (i = 1; i < 8; i++) { pCurrent[i] = qcoeff[i]; pCurrent[i + 7] = qcoeff[i * 8]; @@ -387,7 +386,7 @@ /* apply predictors[] to qcoeff */ -void +static void apply_acdc(MACROBLOCK * pMB, uint32_t block, int16_t qcoeff[64], @@ -427,7 +426,7 @@ iDcScaler = get_dc_scaler(iQuant, j<4); predict_acdc(frame->mbs, x, y, mb_width, j, &qcoeff[j * 64], - iQuant, iDcScaler, predictors[j], 0, 0); + iQuant, iDcScaler, predictors[j], 0); if ((frame->vop_flags & XVID_VOP_HQACPRED)) S += calc_acdc_bits(pMB, j, &qcoeff[j * 64], iDcScaler, predictors[j]); @@ -525,6 +524,85 @@ return pmv[last_cand]; /* no point calculating median mv */ } +VECTOR get_pmv2_interlaced(const MACROBLOCK * const mbs, + const int mb_width, + const int bound, + const int x, + const int y, + const int block) +{ + int lx, ly, lz; /* left */ + int tx, ty, tz; /* top */ + int rx, ry, rz; /* top-right */ + int lpos, tpos, rpos; + int num_cand = 0, last_cand = 1; + + VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */ + + lx=x-1; ly=y; lz=1; + tx=x; ty=y-1; tz=2; + rx=x+1; ry=y-1; rz=2; + + lpos=lx+ly*mb_width; + rpos=rx+ry*mb_width; + tpos=tx+ty*mb_width; + + if(lx>=0 && lpos>=bound) + { + num_cand++; + if(mbs[lpos].field_pred) + pmv[1] = mbs[lpos].mvs_avg; + else + pmv[1] = mbs[lpos].mvs[lz]; + } + else + { + pmv[1] = zeroMV; + } + + if(tpos>=bound) + { + num_cand++; + last_cand=2; + if(mbs[tpos].field_pred) + pmv[2] = mbs[tpos].mvs_avg; + else + pmv[2] = mbs[tpos].mvs[tz]; + } + else + { + pmv[2] = zeroMV; + } + + if(rx=bound) + { + num_cand++; + last_cand = 3; + if(mbs[rpos].field_pred) + pmv[3] = mbs[rpos].mvs_avg; + else + pmv[3] = mbs[rpos].mvs[rz]; + } + else + { + pmv[3] = zeroMV; + } + + /* If there're more than one candidate, we return the median vector */ + if(num_cand>1) + { + /* set median */ + 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))); + + return pmv[0]; + } + + return pmv[last_cand]; /* no point calculating median mv */ +} + VECTOR get_qpmv2(const MACROBLOCK * const mbs, const int mb_width,