#include "typedef.h"
#include "Dct.h"
//void fdct_8x8(
void Dct(
Word16 *dct_data , // 输入原始数据
Word16 *Dct_Coef // DCT系数
)
{
/* -------------------------------------------------------- */
/* Set up the cosine coefficients c0..c7. */
/* -------------------------------------------------------- */
const unsigned short c1 = 0x2C62, c3 = 0x25A0;
const unsigned short c5 = 0x1924, c7 = 0x08D4;
const unsigned short c0 = 0xB505, c2 = 0x29CF;
const unsigned short c6 = 0x1151;
/* -------------------------------------------------------- */
/* Intermediate calculations. */
/* -------------------------------------------------------- */
Word16 f0, f1, f2, f3,
f4, f5, f6, f7; /* Spatial domain samples. */
Word32 g0, g1, h0, h1,
p0, p1; /* Even-half intermediate. */
Word16 r0, r1; /* Even-half intermediate. */
Word32 P0, P1, R0, R1; /* Even-half intermediate. */
Word16 g2, g3, h2, h3; /* Odd-half intermediate. */
Word16 q0a,s0a,q0, q1,
s0, s1; /* Odd-half intermediate. */
Word16 Q0, Q1, S0, S1; /* Odd-half intermediate. */
Word32 F0, F1, F2, F3,
F4, F5, F6, F7; /* Freq. domain results. */
Word32 F0r,F1r,F2r,F3r,
F4r,F5r,F6r,F7r; /* Rounded, truncated results. */
/* -------------------------------------------------------- */
/* Input and output pointers, loop control. */
/* -------------------------------------------------------- */
Word32 i;
/* -------------------------------------------------------- */
/* Outer vertical loop. */
/* -------------------------------------------------------- */
/* -------------------------------------------------------- */
/* Perform Vert 1-D FDCT on columns. */
/* -------------------------------------------------------- */
for (i = 0; i < 8; i++)
{
/* ---------------------------------------------------- */
/* Load the spatial-domain samples. */
/* ---------------------------------------------------- */
f0 = dct_data[i+ 0];
f1 = dct_data[i+ 8];
f2 = dct_data[i+16];
f3 = dct_data[i+24];
f4 = dct_data[i+32];
f5 = dct_data[i+40];
f6 = dct_data[i+48];
f7 = dct_data[i+56];
/* ---------------------------------------------------- */
/* Stage 1: Separate into even and odd halves. */
/* ---------------------------------------------------- */
g0 = f0 + f7; h2 = f0 - f7;
g1 = f1 + f6; h3 = f1 - f6;
h1 = f2 + f5; g3 = f2 - f5;
h0 = f3 + f4; g2 = f3 - f4;
/* ---------------------------------------------------- */
/* Stage 2 */
/* ---------------------------------------------------- */
p0 = g0 + h0; r0 = g0 - h0;
p1 = g1 + h1; r1 = g1 - h1;
q1 = g2; s1 = h2;
s0a= h3 + g3; q0a= h3 - g3;
s0 = ((Word32)s0a * c0 + 0x7FFF) >> 16;
q0 = ((Word32)q0a * c0 + 0x7FFF) >> 16;
/* ---------------------------------------------------- */
/* Stage 3 */
/* ---------------------------------------------------- */
P0 = p0 + p1; P1 = p0 - p1;
R1 = (Word32)c6 * r1 + (Word32)c2 * r0; R0 = (Word32)c6 * r0 - (Word32)c2 * r1;
Q1 = q1 + q0; Q0 = q1 - q0;
S1 = s1 + s0; S0 = s1 - s0;
/* ---------------------------------------------------- */
/* Stage 4 */
/* ---------------------------------------------------- */
F0 = P0; F4 = P1;
F2 = R1; F6 = R0;
F1 = (Word32)c7 * Q1 + (Word32)c1 * S1; F7 = (Word32)c7 * S1 - (Word32)c1 * Q1;
F5 = (Word32)c3 * Q0 + (Word32)c5 * S0; F3 = (Word32)c3 * S0 - (Word32)c5 * Q0;
/* ---------------------------------------------------- */
/* Store the frequency domain results. */
/* ---------------------------------------------------- */
Dct_Coef[i+ 0] = F0;
Dct_Coef[i+ 8] = F1 >> 13;
Dct_Coef[i+16] = F2 >> 13;
Dct_Coef[i+24] = F3 >> 13;
Dct_Coef[i+32] = F4;
Dct_Coef[i+40] = F5 >> 13;
Dct_Coef[i+48] = F6 >> 13;
Dct_Coef[i+56] = F7 >> 13;
}
/* -------------------------------------------------------- */
/* Perform Horizontal 1-D FDCT on each 8x8 block. */
/* -------------------------------------------------------- */
for (i = 0; i < 8; i++)
{
/* ---------------------------------------------------- */
/* Load the spatial-domain samples. */
/* ---------------------------------------------------- */
f0 = Dct_Coef[8*i+0];
f1 = Dct_Coef[8*i+1];
f2 = Dct_Coef[8*i+2];
f3 = Dct_Coef[8*i+3];
f4 = Dct_Coef[8*i+4];
f5 = Dct_Coef[8*i+5];
f6 = Dct_Coef[8*i+6];
f7 = Dct_Coef[8*i+7];
/* ---------------------------------------------------- */
/* Stage 1: Separate into even and odd halves. */
/* ---------------------------------------------------- */
g0 = f0 + f7; h2 = f0 - f7;
g1 = f1 + f6; h3 = f1 - f6;
h1 = f2 + f5; g3 = f2 - f5;
h0 = f3 + f4; g2 = f3 - f4;
/* ---------------------------------------------------- */
/* Stage 2 */
/* ---------------------------------------------------- */
p0 = g0 + h0; r0 = g0 - h0;
p1 = g1 + h1; r1 = g1 - h1;
q1 = g2; s1 = h2;
s0a= h3 + g3; q0a= h3 - g3;
s0 = ((Word32)s0a * c0 + 0x7FFF) >> 16;
q0 = ((Word32)q0a * c0 + 0x7FFF) >> 16;
/* ---------------------------------------------------- */
/* Stage 3 */
/* ---------------------------------------------------- */
P0 = p0 + p1; P1 = p0 - p1;
R1 = (Word32)c6 * r1 + (Word32)c2 * r0; R0 = (Word32)c6 * r0 - (Word32)c2 * r1;
Q1 = q1 + q0; Q0 = q1 - q0;
S1 = s1 + s0; S0 = s1 - s0;
/* ---------------------------------------------------- */
/* Stage 4 */
/* ---------------------------------------------------- */
F0 = P0; F4 = P1;
F2 = R1; F6 = R0;
F1 = (Word32)c7 * Q1 + (Word32)c1 * S1; F7 = (Word32)c7 * S1 - (Word32)c1 * Q1;
F5 = (Word32)c3 * Q0 + (Word32)c5 * S0; F3 = (Word32)c3 * S0 - (Word32)c5 * Q0;
/* ---------------------------------------------------- */
/* Round and truncate values. */
/* */
/* Note: F0 and F4 have different rounding since no */
/* MPYs have been applied to either term. Also, F0's */
/* rounding is slightly different to offset the */
/* truncation effects from the horizontal pass (which */
/* does not round). */
/* ---------------------------------------------------- */
F0r = (F0 + 0x0006) >> 3;
F1r = (F1 + 0x7FFF) >> 16;
F2r = (F2 + 0x7FFF) >> 16;
F3r = (F3 + 0x7FFF) >> 16;
F4r = (F4 + 0x0004) >