[13] | 1 | |
---|
| 2 | #include "stdio.h" |
---|
| 3 | #include "jpeg.h" |
---|
| 4 | |
---|
| 5 | /* Useful constants: */ |
---|
| 6 | |
---|
| 7 | /* ck = cos(k*pi/16) = s8-k = sin((8-k)*pi/16) times 1 << C_BITS and rounded */ |
---|
| 8 | #define c0_1 16384 |
---|
| 9 | #define c0_s2 23170 |
---|
| 10 | #define c1_1 16069 |
---|
| 11 | #define c1_s2 22725 |
---|
| 12 | #define c2_1 15137 |
---|
| 13 | #define c2_s2 21407 |
---|
| 14 | #define c3_1 13623 |
---|
| 15 | #define c3_s2 19266 |
---|
| 16 | #define c4_1 11585 |
---|
| 17 | #define c4_s2 16384 |
---|
| 18 | #define c5_1 9102 |
---|
| 19 | #define c5_s2 12873 |
---|
| 20 | #define c6_1 6270 |
---|
| 21 | #define c6_s2 8867 |
---|
| 22 | #define c7_1 3196 |
---|
| 23 | #define c7_s2 4520 |
---|
| 24 | #define c8_1 0 |
---|
| 25 | #define c8_s2 0 |
---|
| 26 | #define sqrt2 c0_s2 |
---|
| 27 | |
---|
| 28 | /* The number of bits of accuracy in all (signed) integer operations: |
---|
| 29 | May lie between 1 and 32 (bounds inclusive). |
---|
| 30 | */ |
---|
| 31 | #define ARITH_BITS 16 |
---|
| 32 | |
---|
| 33 | /* The minimum signed integer value that fits in ARITH_BITS: */ |
---|
| 34 | #define ARITH_MIN (-1 << (ARITH_BITS-1)) |
---|
| 35 | #define ARITH_MAX (~ARITH_MIN) |
---|
| 36 | |
---|
| 37 | /* The number of bits coefficients are scaled up before 2-D idct: */ |
---|
| 38 | #define S_BITS 3 |
---|
| 39 | /* The number of bits in the fractional part of a fixed point constant: */ |
---|
| 40 | #define C_BITS 14 |
---|
| 41 | |
---|
| 42 | /* This version is vital in passing overall mean error test. */ |
---|
| 43 | #define descale(x, n) (((x) + (1 << ((n) - 1)) - ((x) < 0)) >> (n)) |
---|
| 44 | |
---|
| 45 | const int COS[2][8] = { |
---|
| 46 | {c0_1, c1_1, c2_1, c3_1, c4_1, c5_1, c6_1, c7_1}, |
---|
| 47 | {c0_s2, c1_s2, c2_s2, c3_s2, c4_s2, c5_s2, c6_s2, c7_s2} |
---|
| 48 | }; |
---|
| 49 | |
---|
| 50 | void rot(int f, int k, int x, int y, int*rx, int*ry) |
---|
| 51 | { |
---|
| 52 | #define Cos(k) COS[f][k] |
---|
| 53 | #define Sin(k) Cos(8-k) |
---|
| 54 | *rx = (Cos(k) * x - Sin(k) * y) >> C_BITS; |
---|
| 55 | // r = (r + (1 << (C_BITS - 1))) >> C_BITS; |
---|
| 56 | *ry = (Sin(k) * x + Cos(k) * y) >> C_BITS; |
---|
| 57 | // r = (r + (1 << (C_BITS - 1))) >> C_BITS; |
---|
| 58 | #undef Cos |
---|
| 59 | #undef Sin |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /* Butterfly: but(a,b,x,y) = rot(sqrt(2),4,a,b,x,y) */ |
---|
| 63 | #define but(a,b,x,y) do { x = a - b; y = a + b; } while(0) |
---|
| 64 | |
---|
| 65 | /* Inverse 1-D Discrete Cosine Transform. |
---|
| 66 | Result Y is scaled up by factor sqrt(8). |
---|
| 67 | Original Loeffler algorithm. |
---|
| 68 | */ |
---|
| 69 | void idct_1d(int *Y) |
---|
| 70 | { |
---|
| 71 | int z1[8], z2[8], z3[8]; |
---|
| 72 | |
---|
| 73 | /* Stage 1: */ |
---|
| 74 | but(Y[0], Y[4], z1[1], z1[0]); |
---|
| 75 | rot(1, 6, Y[2], Y[6], &z1[2], &z1[3]); |
---|
| 76 | but(Y[1], Y[7], z1[4], z1[7]); |
---|
| 77 | z1[5] = (sqrt2 * Y[3]) >> C_BITS; |
---|
| 78 | // r = (r + (1 << (C_BITS - 1))) >> C_BITS; |
---|
| 79 | z1[6] = (sqrt2 * Y[5]) >> C_BITS; |
---|
| 80 | // r = (r + (1 << (C_BITS - 1))) >> C_BITS; |
---|
| 81 | |
---|
| 82 | /* Stage 2: */ |
---|
| 83 | but(z1[0], z1[3], z2[3], z2[0]); |
---|
| 84 | but(z1[1], z1[2], z2[2], z2[1]); |
---|
| 85 | but(z1[4], z1[6], z2[6], z2[4]); |
---|
| 86 | but(z1[7], z1[5], z2[5], z2[7]); |
---|
| 87 | |
---|
| 88 | /* Stage 3: */ |
---|
| 89 | z3[0] = z2[0]; |
---|
| 90 | z3[1] = z2[1]; |
---|
| 91 | z3[2] = z2[2]; |
---|
| 92 | z3[3] = z2[3]; |
---|
| 93 | rot(0, 3, z2[4], z2[7], &z3[4], &z3[7]); |
---|
| 94 | rot(0, 1, z2[5], z2[6], &z3[5], &z3[6]); |
---|
| 95 | |
---|
| 96 | /* Final stage 4: */ |
---|
| 97 | but(z3[0], z3[7], Y[7], Y[0]); |
---|
| 98 | but(z3[1], z3[6], Y[6], Y[1]); |
---|
| 99 | but(z3[2], z3[5], Y[5], Y[2]); |
---|
| 100 | but(z3[3], z3[4], Y[4], Y[3]); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | int main() |
---|
| 104 | { |
---|
| 105 | #define Y(i,j) Y[8*i+j] |
---|
| 106 | #define Idct(i,j) Idct[8*i+j] |
---|
| 107 | int Y[BLOCK_SIZE]; |
---|
| 108 | int row, column; |
---|
| 109 | int *in; |
---|
| 110 | int *Idct; |
---|
| 111 | int block; |
---|
| 112 | unsigned int i=0; |
---|
| 113 | unsigned int begin, end; |
---|
| 114 | int Yc[BLOCK_HEIGHT], r; |
---|
| 115 | |
---|
| 116 | tty_printf("IDCT thread is alive!\n"); |
---|
| 117 | |
---|
| 118 | begin = proctime(); |
---|
| 119 | |
---|
| 120 | for ( block=0; block<NBLOCKS; ++block ) { |
---|
| 121 | // uint begin, end; |
---|
| 122 | |
---|
| 123 | in = bloc[i]; |
---|
| 124 | Idct = out; |
---|
| 125 | i = (i+1) % 6; |
---|
| 126 | tty_printf("IDCT processing block %d/%d\n", |
---|
| 127 | block, NBLOCKS); |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | begin = proctime(); |
---|
| 131 | //srl_mwmr_read( input, in, BLOCK_SIZE*sizeof(*in) ); |
---|
| 132 | |
---|
| 133 | for (row = 0; row < BLOCK_HEIGHT; row++) { |
---|
| 134 | for (column = 0; column < BLOCK_WIDTH; column++) |
---|
| 135 | Y(row, column) = in[row*BLOCK_WIDTH+column] << S_BITS; |
---|
| 136 | idct_1d(&Y(row, 0)); |
---|
| 137 | /* Result Y is scaled up by factor sqrt(8)*2^S_BITS. */ |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | for (column = 0; column < BLOCK_WIDTH; column++) { |
---|
| 141 | |
---|
| 142 | for (row = 0; row < BLOCK_HEIGHT; row++) |
---|
| 143 | Yc[row] = Y(row, column); |
---|
| 144 | |
---|
| 145 | idct_1d(Yc); |
---|
| 146 | for (row = 0; row < BLOCK_HEIGHT; row++) { |
---|
| 147 | /* Result is once more scaled up by a factor sqrt(8). */ |
---|
| 148 | r = 128 + descale(Yc[row], 2*S_BITS); |
---|
| 149 | /* Clip to 8 bits unsigned: */ |
---|
| 150 | r = r > 0 ? (r < 255 ? r : 255) : 0; |
---|
| 151 | Idct(row, column) = r; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | //srl_mwmr_write( output, Idct, BLOCK_SIZE*sizeof(*Idct) ); |
---|
| 155 | end = proctime(); |
---|
| 156 | tty_printf("IDCT: %d blocks in %d cycles\n", block, end-begin); |
---|
| 157 | } // end for block = fin image |
---|
| 158 | |
---|
| 159 | end = proctime(); |
---|
| 160 | tty_printf("+++ IDCT total time = %d cycles\n", end ); |
---|
| 161 | |
---|
| 162 | exit(); |
---|
| 163 | return 0; |
---|
| 164 | } // end main |
---|