From 3d32b429d3dfbb5f39528a598fe13e88b0543e85 Mon Sep 17 00:00:00 2001 From: Mike Melanson Date: Thu, 10 Jul 2003 05:16:25 +0000 Subject: [PATCH] added the official VP3 IDCT (C implementation) as well as a grayscale decoding mode Originally committed as revision 2027 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/vp3.c | 363 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 322 insertions(+), 41 deletions(-) diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c index 711679000b..c46b0bc6da 100644 --- a/libavcodec/vp3.c +++ b/libavcodec/vp3.c @@ -17,6 +17,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net) + * For more information about the VP3 coding process, visit: + * http://www.pcisys.net/~melanson/codecs/ * */ @@ -287,6 +289,307 @@ typedef struct Vp3DecodeContext { } Vp3DecodeContext; +/************************************************************************ + * VP3 I/DCT + ************************************************************************/ + +#define IdctAdjustBeforeShift 8 +#define xC1S7 64277 +#define xC2S6 60547 +#define xC3S5 54491 +#define xC4S4 46341 +#define xC5S3 36410 +#define xC6S2 25080 +#define xC7S1 12785 + +void vp3_idct_c(int16_t *input_data, int16_t *dequant_matrix, + int16_t *output_data) +{ + int32_t intermediate_data[64]; + int32_t *ip = intermediate_data; + int16_t *op = output_data; + + int32_t _A, _B, _C, _D, _Ad, _Bd, _Cd, _Dd, _E, _F, _G, _H; + int32_t _Ed, _Gd, _Add, _Bdd, _Fd, _Hd; + int32_t t1, t2; + + int i, j; + + debug_idct("raw coefficient block:\n"); + for (i = 0; i < 8; i++) { + for (j = 0; j < 8; j++) { + debug_idct(" %5d", input_data[i * 8 + j]); + } + debug_idct("\n"); + } + debug_idct("\n"); + + for (i = 0; i < 64; i++) { + j = dezigzag_index[i]; + intermediate_data[j] = dequant_matrix[i] * input_data[i]; + } + + debug_idct("dequantized block:\n"); + for (i = 0; i < 8; i++) { + for (j = 0; j < 8; j++) { + debug_idct(" %5d", intermediate_data[i * 8 + j]); + } + debug_idct("\n"); + } + debug_idct("\n"); + + /* Inverse DCT on the rows now */ + for (i = 0; i < 8; i++) { + /* Check for non-zero values */ + if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) { + t1 = (int32_t)(xC1S7 * ip[1]); + t2 = (int32_t)(xC7S1 * ip[7]); + t1 >>= 16; + t2 >>= 16; + _A = t1 + t2; + + t1 = (int32_t)(xC7S1 * ip[1]); + t2 = (int32_t)(xC1S7 * ip[7]); + t1 >>= 16; + t2 >>= 16; + _B = t1 - t2; + + t1 = (int32_t)(xC3S5 * ip[3]); + t2 = (int32_t)(xC5S3 * ip[5]); + t1 >>= 16; + t2 >>= 16; + _C = t1 + t2; + + t1 = (int32_t)(xC3S5 * ip[5]); + t2 = (int32_t)(xC5S3 * ip[3]); + t1 >>= 16; + t2 >>= 16; + _D = t1 - t2; + + + t1 = (int32_t)(xC4S4 * (_A - _C)); + t1 >>= 16; + _Ad = t1; + + t1 = (int32_t)(xC4S4 * (_B - _D)); + t1 >>= 16; + _Bd = t1; + + + _Cd = _A + _C; + _Dd = _B + _D; + + t1 = (int32_t)(xC4S4 * (ip[0] + ip[4])); + t1 >>= 16; + _E = t1; + + t1 = (int32_t)(xC4S4 * (ip[0] - ip[4])); + t1 >>= 16; + _F = t1; + + t1 = (int32_t)(xC2S6 * ip[2]); + t2 = (int32_t)(xC6S2 * ip[6]); + t1 >>= 16; + t2 >>= 16; + _G = t1 + t2; + + t1 = (int32_t)(xC6S2 * ip[2]); + t2 = (int32_t)(xC2S6 * ip[6]); + t1 >>= 16; + t2 >>= 16; + _H = t1 - t2; + + + _Ed = _E - _G; + _Gd = _E + _G; + + _Add = _F + _Ad; + _Bdd = _Bd - _H; + + _Fd = _F - _Ad; + _Hd = _Bd + _H; + + /* Final sequence of operations over-write original inputs. */ + ip[0] = (int16_t)((_Gd + _Cd ) >> 0); + ip[7] = (int16_t)((_Gd - _Cd ) >> 0); + + ip[1] = (int16_t)((_Add + _Hd ) >> 0); + ip[2] = (int16_t)((_Add - _Hd ) >> 0); + + ip[3] = (int16_t)((_Ed + _Dd ) >> 0); + ip[4] = (int16_t)((_Ed - _Dd ) >> 0); + + ip[5] = (int16_t)((_Fd + _Bdd ) >> 0); + ip[6] = (int16_t)((_Fd - _Bdd ) >> 0); + + } + + ip += 8; /* next row */ + } + + ip = intermediate_data; + + for ( i = 0; i < 8; i++) { + /* Check for non-zero values (bitwise or faster than ||) */ + if ( ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] | + ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) { + + t1 = (int32_t)(xC1S7 * ip[1*8]); + t2 = (int32_t)(xC7S1 * ip[7*8]); + t1 >>= 16; + t2 >>= 16; + _A = t1 + t2; + + t1 = (int32_t)(xC7S1 * ip[1*8]); + t2 = (int32_t)(xC1S7 * ip[7*8]); + t1 >>= 16; + t2 >>= 16; + _B = t1 - t2; + + t1 = (int32_t)(xC3S5 * ip[3*8]); + t2 = (int32_t)(xC5S3 * ip[5*8]); + t1 >>= 16; + t2 >>= 16; + _C = t1 + t2; + + t1 = (int32_t)(xC3S5 * ip[5*8]); + t2 = (int32_t)(xC5S3 * ip[3*8]); + t1 >>= 16; + t2 >>= 16; + _D = t1 - t2; + + + t1 = (int32_t)(xC4S4 * (_A - _C)); + t1 >>= 16; + _Ad = t1; + + t1 = (int32_t)(xC4S4 * (_B - _D)); + t1 >>= 16; + _Bd = t1; + + + _Cd = _A + _C; + _Dd = _B + _D; + + t1 = (int32_t)(xC4S4 * (ip[0*8] + ip[4*8])); + t1 >>= 16; + _E = t1; + + t1 = (int32_t)(xC4S4 * (ip[0*8] - ip[4*8])); + t1 >>= 16; + _F = t1; + + t1 = (int32_t)(xC2S6 * ip[2*8]); + t2 = (int32_t)(xC6S2 * ip[6*8]); + t1 >>= 16; + t2 >>= 16; + _G = t1 + t2; + + t1 = (int32_t)(xC6S2 * ip[2*8]); + t2 = (int32_t)(xC2S6 * ip[6*8]); + t1 >>= 16; + t2 >>= 16; + _H = t1 - t2; + + + _Ed = _E - _G; + _Gd = _E + _G; + + _Add = _F + _Ad; + _Bdd = _Bd - _H; + + _Fd = _F - _Ad; + _Hd = _Bd + _H; + + _Gd += IdctAdjustBeforeShift; + _Add += IdctAdjustBeforeShift; + _Ed += IdctAdjustBeforeShift; + _Fd += IdctAdjustBeforeShift; + + /* Final sequence of operations over-write original inputs. */ + op[0*8] = (int16_t)((_Gd + _Cd ) >> 4); + op[7*8] = (int16_t)((_Gd - _Cd ) >> 4); + + op[1*8] = (int16_t)((_Add + _Hd ) >> 4); + op[2*8] = (int16_t)((_Add - _Hd ) >> 4); + + op[3*8] = (int16_t)((_Ed + _Dd ) >> 4); + op[4*8] = (int16_t)((_Ed - _Dd ) >> 4); + + op[5*8] = (int16_t)((_Fd + _Bdd ) >> 4); + op[6*8] = (int16_t)((_Fd - _Bdd ) >> 4); + + } else { + + op[0*8] = 0; + op[7*8] = 0; + op[1*8] = 0; + op[2*8] = 0; + op[3*8] = 0; + op[4*8] = 0; + op[5*8] = 0; + op[6*8] = 0; + } + + ip++; /* next column */ + op++; + } +} + +void vp3_idct_put(int16_t *input_data, int16_t *dequant_matrix, + uint8_t *dest, int stride) +{ + int16_t transformed_data[64]; + int16_t *op; + int i, j; + + vp3_idct_c(input_data, dequant_matrix, transformed_data); + + /* place in final output */ + op = transformed_data; + for (i = 0; i < 8; i++) { + for (j = 0; j < 8; j++) { + if (*op < -128) + *dest = 0; + else if (*op > 127) + *dest = 255; + else + *dest = (uint8_t)(*op + 128); + op++; + dest++; + } + dest += (stride - 8); + } +} + +void vp3_idct_add(int16_t *input_data, int16_t *dequant_matrix, + uint8_t *dest, int stride) +{ + int16_t transformed_data[64]; + int16_t *op; + int i, j; + int16_t sample; + + vp3_idct_c(input_data, dequant_matrix, transformed_data); + + /* place in final output */ + op = transformed_data; + for (i = 0; i < 8; i++) { + for (j = 0; j < 8; j++) { + sample = *dest + *op; + if (sample < 0) + *dest = 0; + else if (sample > 255) + *dest = 255; + else + *dest = (uint8_t)(sample & 0xFF); + op++; + dest++; + } + dest += (stride - 8); + } +} + /************************************************************************ * VP3 specific functions ************************************************************************/ @@ -843,7 +1146,7 @@ static void init_dequantizer(Vp3DecodeContext *s) * * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL. */ -#define SCALER 1 +#define SCALER 4 /* scale DC quantizers */ s->intra_y_dequant[0] = vp31_intra_y_dequant[0] * dc_scale_factor / 100; @@ -1423,7 +1726,6 @@ static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) int current_fragment; debug_vp3(" vp3: unpacking motion vectors\n"); - if (s->keyframe) { debug_vp3(" keyframe-- there are no motion vectors\n"); @@ -2030,10 +2332,7 @@ static void render_fragments(Vp3DecodeContext *s, int x, y; int m, n; int i = first_fragment; - int j; int16_t *dequantizer; - DCTELEM dequant_block[64]; - DCTELEM dequant_block_permuted[64]; unsigned char *output_plane; unsigned char *last_plane; unsigned char *golden_plane; @@ -2122,8 +2421,8 @@ printf (" help! got beefy vector! (%X, %X)\n", motion_x, motion_y); * to render the block */ if ((motion_source < upper_motion_limit) || (motion_source > lower_motion_limit)) { -// printf (" vp3: help! motion source (%d) out of range (%d..%d)\n", -// motion_source, upper_motion_limit, lower_motion_limit); + printf (" vp3: help! motion source (%d) out of range (%d..%d), fragment %d\n", + motion_source, upper_motion_limit, lower_motion_limit, i); continue; } } @@ -2151,34 +2450,16 @@ printf (" help! got beefy vector! (%X, %X)\n", motion_x, motion_y); debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n", i, s->all_fragments[i].coding_method, s->all_fragments[i].coeffs[0], dequantizer[0]); - for (j = 0; j < 64; j++) - dequant_block[dezigzag_index[j]] = - s->all_fragments[i].coeffs[j] * - dequantizer[j]; - for (j = 0; j < 64; j++) - dequant_block_permuted[s->dsp.idct_permutation[j]] = - dequant_block[j]; - - debug_idct("dequantized block:\n"); - for (m = 0; m < 8; m++) { - for (n = 0; n < 8; n++) { - debug_idct(" %5d", dequant_block[m * 8 + n]); - } - debug_idct("\n"); - } - debug_idct("\n"); /* invert DCT and place (or add) in final output */ - if (s->all_fragments[i].coding_method == MODE_INTRA) { - dequant_block_permuted[0] += 1024; - s->dsp.idct_put( + vp3_idct_put(s->all_fragments[i].coeffs, dequantizer, output_plane + s->all_fragments[i].first_pixel, - stride, dequant_block_permuted); + stride); } else { - s->dsp.idct_add( + vp3_idct_add(s->all_fragments[i].coeffs, dequantizer, output_plane + s->all_fragments[i].first_pixel, - stride, dequant_block_permuted); + stride); } debug_idct("block after idct_%s():\n", @@ -2479,19 +2760,19 @@ if (!s->keyframe) { } reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height); - reverse_dc_prediction(s, s->u_fragment_start, - s->fragment_width / 2, s->fragment_height / 2); - reverse_dc_prediction(s, s->v_fragment_start, - s->fragment_width / 2, s->fragment_height / 2); - render_fragments(s, 0, s->width, s->height, 0); -#if 1 - render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1); - render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2); -#else -memset(s->current_frame.data[1], 0x80, s->width * s->height / 4); -memset(s->current_frame.data[2], 0x80, s->width * s->height / 4); -#endif + + if ((avctx->flags & CODEC_FLAG_GRAY) == 0) { + reverse_dc_prediction(s, s->u_fragment_start, + s->fragment_width / 2, s->fragment_height / 2); + reverse_dc_prediction(s, s->v_fragment_start, + s->fragment_width / 2, s->fragment_height / 2); + render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1); + render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2); + } else { + memset(s->current_frame.data[1], 0x80, s->width * s->height / 4); + memset(s->current_frame.data[2], 0x80, s->width * s->height / 4); + } #if KEYFRAMES_ONLY }