--- mbcoding.c 2003/01/26 01:44:44 1.25.2.13 +++ mbcoding.c 2003/02/15 15:22:18 1.42 @@ -72,7 +72,8 @@ #ifdef BIGLUT static VLC coeff_VLC[2][2][4096][64]; -static VLC *intra_table, *inter_table; +VLC *intra_table; +static VLC *inter_table; #else static VLC coeff_VLC[2][2][64][64]; #endif @@ -109,7 +110,7 @@ void init_vlc_tables(void) { - ptr_t i, j, k, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; + uint32_t i, j, k, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; int32_t l; #ifdef BIGLUT @@ -126,7 +127,7 @@ for (last = 0; last < 2; last++) { for (run = 0; run < 63 + last; run++) - for (level = 0; level < 32 << intra; level++) + for (level = 0; level < (uint32_t)(32 << intra); level++) { #ifdef BIGLUT offset = LEVELOFFSET; @@ -145,7 +146,7 @@ #else offset = !intra * LEVELOFFSET; #endif - for (j = 0; j < 1 << (12 - coeff_tab[intra][i].vlc.len); j++) + for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++) { DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len = coeff_tab[intra][i].vlc.len; DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event; @@ -170,7 +171,7 @@ for (last = 0; last < 2; last++) for (run = 0; run < 63 + last; run++) { - for (level = 1; level < 32 << intra; level++) + for (level = 1; level < (uint32_t)(32 << intra); level++) { if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level]) continue; @@ -195,7 +196,7 @@ } else { - if (level <= max_level[intra][last][run_esc] && run_esc <= max_run[intra][last][level]) + if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc]) { escape = ESCAPE2; escape_len = 7 + 2; @@ -373,6 +374,50 @@ } + + +/* returns the number of bits required to encode qcoeff */ +int +CodeCoeff_CalcBits(const int16_t qcoeff[64], + VLC * table, + const uint16_t * zigzag, + uint16_t intra) +{ + int bits = 0; + uint32_t j, last; + short v; + VLC *vlc; + + j = intra; + last = intra; + + while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) + j++; + + if (j >= 64) return 0; /* empty block */ + + do { + vlc = table + 64 * 2048 + (v << 6) + j - last; + last = ++j; + + /* count zeroes */ + while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) + j++; + + /* write code */ + if (j != 64) { + bits += vlc->len; + } else { + vlc += 64 * 4096; + bits += vlc->len; + break; + } + } while (1); + + return bits; +} + + #else static __inline void @@ -442,7 +487,7 @@ i = 1; run = 0; - while (!(level = qcoeff[zigzag[i++]])) + while (i<64 && !(level = qcoeff[zigzag[i++]])) run++; prev_level = level; @@ -487,6 +532,99 @@ BitstreamPutBits(bs, code, len); } + + +/* returns the number of bits required to encode qcoeff */ + +int +CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag) +{ + int bits = 0; + uint32_t i, abs_level, run, prev_run, len; + int32_t level, prev_level; + + i = 1; + run = 0; + + while (i<64 && !(level = qcoeff[zigzag[i++]])) + run++; + + if (i >= 64) return 0; /* empty block */ + + prev_level = level; + prev_run = run; + run = 0; + + while (i < 64) + { + if ((level = qcoeff[zigzag[i++]]) != 0) + { + abs_level = ABS(prev_level); + abs_level = abs_level < 64 ? abs_level : 0; + len = coeff_VLC[1][0][abs_level][prev_run].len; + bits += len!=128 ? len : 30; + + prev_level = level; + prev_run = run; + run = 0; + } + else + run++; + } + + abs_level = ABS(prev_level); + abs_level = abs_level < 64 ? abs_level : 0; + len = coeff_VLC[1][1][abs_level][prev_run].len; + bits += len!=128 ? len : 30; + + return bits; +} + +int +CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag) +{ + uint32_t i, run, prev_run, len; + int32_t level, prev_level, level_shifted; + int bits = 0; + + i = 0; + run = 0; + + while (!(level = qcoeff[zigzag[i++]])) + run++; + + prev_level = level; + prev_run = run; + run = 0; + + while (i < 64) { + if ((level = qcoeff[zigzag[i++]]) != 0) { + level_shifted = prev_level + 32; + if (!(level_shifted & -64)) + len = coeff_VLC[0][0][level_shifted][prev_run].len; + else + len = 30; + + bits += len; + prev_level = level; + prev_run = run; + run = 0; + } + else + run++; + } + + level_shifted = prev_level + 32; + if (!(level_shifted & -64)) + len = coeff_VLC[0][1][level_shifted][prev_run].len; + else + len = 30; + bits += len; + + return bits; +} + + #endif static __inline void