00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <math.h>
00041
00042
00043
00044 #ifndef PI
00045 # ifdef M_PI
00046 # define PI M_PI
00047 # else
00048 # define PI 3.14159265358979323846
00049 # endif
00050 #endif
00051
00052
00053 static double c[8][8];
00054
00055 void fdct_enc(block)
00056 short *block;
00057 {
00058 int i, j, k;
00059 double s;
00060 double tmp[64];
00061
00062 for (i=0; i<8; i++)
00063 for (j=0; j<8; j++)
00064 {
00065 s = 0.0;
00066
00067 for (k=0; k<8; k++)
00068 s += c[j][k] * block[8*i+k];
00069
00070 tmp[8*i+j] = s;
00071 }
00072
00073 for (j=0; j<8; j++)
00074 for (i=0; i<8; i++)
00075 {
00076 s = 0.0;
00077
00078 for (k=0; k<8; k++)
00079 s += c[i][k] * tmp[8*k+j];
00080
00081 block[8*i+j] = (int)floor(s+0.499999);
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 }
00092 }
00093
00094 void init_fdct_enc()
00095 {
00096 int i, j;
00097 double s;
00098
00099 for (i=0; i<8; i++)
00100 {
00101 s = (i==0) ? sqrt(0.125) : 0.5;
00102
00103 for (j=0; j<8; j++)
00104 c[i][j] = s * cos((PI/8.0)*i*(j+0.5));
00105 }
00106 }
00107
00108
00109
00110
00111
00112
00113 #define W1 2841
00114 #define W2 2676
00115 #define W3 2408
00116 #define W5 1609
00117 #define W6 1108
00118 #define W7 565
00119
00120
00121 static short iclip[1024];
00122 static short *iclp;
00123
00124
00125 static void idctrow_enc (short *blk);
00126 static void idctcol_enc (short *blk);
00127
00128
00129 void idct_enc(block)
00130 short *block;
00131 {
00132 int i;
00133
00134 for (i=0; i<8; i++)
00135 idctrow_enc(block+8*i);
00136
00137 for (i=0; i<8; i++)
00138 idctcol_enc(block+i);
00139 }
00140
00141 void init_idct_enc()
00142 {
00143 int i;
00144
00145 iclp = iclip+512;
00146 for (i= -512; i<512; i++)
00147 iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 static void idctrow_enc(blk)
00161 short *blk;
00162 {
00163 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
00164
00165
00166 if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
00167 (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
00168 {
00169 blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
00170 return;
00171 }
00172
00173 x0 = (blk[0]<<11) + 128;
00174
00175
00176 x8 = W7*(x4+x5);
00177 x4 = x8 + (W1-W7)*x4;
00178 x5 = x8 - (W1+W7)*x5;
00179 x8 = W3*(x6+x7);
00180 x6 = x8 - (W3-W5)*x6;
00181 x7 = x8 - (W3+W5)*x7;
00182
00183
00184 x8 = x0 + x1;
00185 x0 -= x1;
00186 x1 = W6*(x3+x2);
00187 x2 = x1 - (W2+W6)*x2;
00188 x3 = x1 + (W2-W6)*x3;
00189 x1 = x4 + x6;
00190 x4 -= x6;
00191 x6 = x5 + x7;
00192 x5 -= x7;
00193
00194
00195 x7 = x8 + x3;
00196 x8 -= x3;
00197 x3 = x0 + x2;
00198 x0 -= x2;
00199 x2 = (181*(x4+x5)+128)>>8;
00200 x4 = (181*(x4-x5)+128)>>8;
00201
00202
00203 blk[0] = (x7+x1)>>8;
00204 blk[1] = (x3+x2)>>8;
00205 blk[2] = (x0+x4)>>8;
00206 blk[3] = (x8+x6)>>8;
00207 blk[4] = (x8-x6)>>8;
00208 blk[5] = (x0-x4)>>8;
00209 blk[6] = (x3-x2)>>8;
00210 blk[7] = (x7-x1)>>8;
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 static void idctcol_enc(blk)
00223 short *blk;
00224 {
00225 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
00226
00227
00228 if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
00229 (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
00230 {
00231 blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
00232 iclp[(blk[8*0]+32)>>6];
00233 return;
00234 }
00235
00236 x0 = (blk[8*0]<<8) + 8192;
00237
00238
00239 x8 = W7*(x4+x5) + 4;
00240 x4 = (x8+(W1-W7)*x4)>>3;
00241 x5 = (x8-(W1+W7)*x5)>>3;
00242 x8 = W3*(x6+x7) + 4;
00243 x6 = (x8-(W3-W5)*x6)>>3;
00244 x7 = (x8-(W3+W5)*x7)>>3;
00245
00246
00247 x8 = x0 + x1;
00248 x0 -= x1;
00249 x1 = W6*(x3+x2) + 4;
00250 x2 = (x1-(W2+W6)*x2)>>3;
00251 x3 = (x1+(W2-W6)*x3)>>3;
00252 x1 = x4 + x6;
00253 x4 -= x6;
00254 x6 = x5 + x7;
00255 x5 -= x7;
00256
00257
00258 x7 = x8 + x3;
00259 x8 -= x3;
00260 x3 = x0 + x2;
00261 x0 -= x2;
00262 x2 = (181*(x4+x5)+128)>>8;
00263 x4 = (181*(x4-x5)+128)>>8;
00264
00265
00266 blk[8*0] = iclp[(x7+x1)>>14];
00267 blk[8*1] = iclp[(x3+x2)>>14];
00268 blk[8*2] = iclp[(x0+x4)>>14];
00269 blk[8*3] = iclp[(x8+x6)>>14];
00270 blk[8*4] = iclp[(x8-x6)>>14];
00271 blk[8*5] = iclp[(x0-x4)>>14];
00272 blk[8*6] = iclp[(x3-x2)>>14];
00273 blk[8*7] = iclp[(x7-x1)>>14];
00274 }
00275
00276
00277