--- trunk/xvidcore/src/bitstream/mbcoding.c 2002/04/23 00:05:31 133 +++ trunk/xvidcore/src/bitstream/mbcoding.c 2002/05/06 03:51:43 161 @@ -29,9 +29,9 @@ /****************************************************************************** * * - * bitstream.c * + * mbcoding.c * * * - * Copyright (C) 2001 - Peter Ross * + * Copyright (C) 2002 - Michael Militzer * * * * For more information visit the XviD homepage: http://www.xvid.org * * * @@ -41,7 +41,7 @@ * * * Revision history: * * * - * 14.04.2002 bframe encoding + * 14.04.2002 bframe encoding * * 08.03.2002 initial version; isibaar * * * ******************************************************************************/ @@ -60,14 +60,12 @@ #define ABS(X) (((X)>0)?(X):-(X)) #define CLIP(X,A) (X > A) ? (A) : (X) -VLC intra_table[65536]; -VLC inter_table[65536]; +VLC intra_table[524032]; +VLC inter_table[524032]; VLC DCT3Dintra[4096]; VLC DCT3Dinter[4096]; -static int16_t clip_table[4096]; - void init_vlc_tables(void) { @@ -82,23 +80,15 @@ vlc[0] = intra_table; vlc[1] = inter_table; - // initialize the clipping table - for(i = -2048; i < 2048; i++) { - clip_table[i + 2048] = i; - if(i < -255) - clip_table[i + 2048] = -255; - if(i > 255) - clip_table[i + 2048] = 255; - } - // generate encoding vlc lookup tables + // the lookup table idea is taken from the excellent fame project by Vivien Chapellier for(i = 0; i < 4; i++) { intra = i % 2; last = i / 2; coeff_ptr = coeff_vlc[last + 2 * intra]; - - for(k = -255; k < 256; k++) { // level + + for(k = -2047; k < 2048; k++) { // level int8_t *max_level_ptr = max_level[last + 2 * intra]; int8_t *max_run_ptr = max_run[last + 2 * intra]; @@ -193,8 +183,8 @@ } static __inline void CodeVector(Bitstream *bs, - int16_t value, - int16_t f_code, + int32_t value, + int32_t f_code, Statistics *pStat) { @@ -246,7 +236,7 @@ static __inline void CodeCoeff(Bitstream *bs, - int16_t qcoeff[64], + const int16_t qcoeff[64], VLC *table, const uint16_t *zigzag, uint16_t intra) @@ -259,19 +249,20 @@ j = intra; last = intra; - while((v = qcoeff[zigzag[j]]) == 0) j++; + while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; do { - // count zeroes - vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last; + vlc = table + 64 * 2047 + (v << 6) + j - last; last = ++j; + + // count zeroes while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; // write code if(j != 64) { BitstreamPutBits(bs, vlc->code, vlc->len); } else { - vlc += 64*511; + vlc += 64 * 4095; BitstreamPutBits(bs, vlc->code, vlc->len); break; } @@ -280,7 +271,7 @@ } -static void CodeBlockIntra(const MBParam * pParam, +static void CodeBlockIntra(const FRAMEINFO * frame, const MACROBLOCK *pMB, int16_t qcoeff[6*64], Bitstream * bs, @@ -292,7 +283,7 @@ cbpy = pMB->cbp >> 2; // write mcbpc - if(pParam->coding_type == I_VOP) { + if(frame->coding_type == I_VOP) { mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); } @@ -315,7 +306,7 @@ BitstreamPutBits(bs, pMB->dquant, 2); // write interlacing - if (pParam->global_flags & XVID_INTERLACING) + if (frame->global_flags & XVID_INTERLACING) { BitstreamPutBit(bs, pMB->field_dct); } @@ -350,7 +341,7 @@ } -static void CodeBlockInter(const MBParam * pParam, +static void CodeBlockInter(const FRAMEINFO * frame, const MACROBLOCK *pMB, int16_t qcoeff[6*64], Bitstream * bs, @@ -374,7 +365,7 @@ BitstreamPutBits(bs, pMB->dquant, 2); // interlacing - if (pParam->global_flags & XVID_INTERLACING) + if (frame->global_flags & XVID_INTERLACING) { BitstreamPutBit(bs, pMB->field_dct); DEBUG1("codep: field_dct: ", pMB->field_dct); @@ -397,8 +388,8 @@ // code motion vector(s) for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { - CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); - CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); + CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); + CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); } bits = BitstreamPos(bs); @@ -414,7 +405,7 @@ } -void MBCoding(const MBParam * pParam, +void MBCoding(const FRAMEINFO * frame, MACROBLOCK *pMB, int16_t qcoeff[6*64], Bitstream * bs, @@ -423,7 +414,7 @@ int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); - if(pParam->coding_type == P_VOP) { + if(frame->coding_type == P_VOP) { if(pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) { @@ -435,9 +426,9 @@ } if(intra) - CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); + CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); else - CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); + CodeBlockInter(frame, pMB, qcoeff, bs, pStat); } @@ -511,8 +502,8 @@ void MBCodingBVOP(const MACROBLOCK * mb, const int16_t qcoeff[6*64], - const int16_t fcode, - const int16_t bcode, + const int32_t fcode, + const int32_t bcode, Bitstream * bs, Statistics * pStat) { @@ -778,6 +769,7 @@ break; } p += run; + block[ scan[p] ] = level; if (level < -127 || level > 127) {