Main Page | File List

IDCT.C

00001 /* idct.c, inverse fast discrete cosine transform                           */
00002 
00003 /*
00004  * tmndecode 
00005  * Copyright (C) 1995 Telenor R&D
00006  *                    Karl Olav Lillevold <kol@nta.no>                    
00007  *
00008  * based on mpeg2decode, (C) 1994, MPEG Software Simulation Group
00009  * and mpeg2play, (C) 1994 Stefan Eckart
00010  *                         <stefan@lis.e-technik.tu-muenchen.de>
00011  *
00012  */
00013 
00014 
00015 /**********************************************************/
00016 /* inverse two dimensional DCT, Chen-Wang algorithm       */
00017 /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984)             */
00018 /* 32-bit integer arithmetic (8 bit coefficients)         */
00019 /* 11 mults, 29 adds per DCT                              */
00020 /*                                      sE, 18.8.91       */
00021 /**********************************************************/
00022 /* coefficients extended to 12 bit for IEEE1180-1990      */
00023 /* compliance                           sE,  2.1.94       */
00024 /**********************************************************/
00025 
00026 /* this code assumes >> to be a two's-complement arithmetic */
00027 /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2               */
00028 
00029 #include "config.h"
00030 
00031 #define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
00032 #define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
00033 #define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
00034 #define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
00035 #define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
00036 #define W7 565  /* 2048*sqrt(2)*cos(7*pi/16) */
00037 
00038 /* global declarations */
00039 void init_idct _ANSI_ARGS_((void));
00040 void idct _ANSI_ARGS_((short *block));
00041 
00042 /* private data */
00043 static short iclip[1024]; /* clipping table */
00044 static short *iclp;
00045 
00046 /* private prototypes */
00047 static void idctrow _ANSI_ARGS_((short *blk));
00048 static void idctcol _ANSI_ARGS_((short *blk));
00049 
00050 /* row (horizontal) IDCT
00051  *
00052  *           7                       pi         1
00053  * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
00054  *          l=0                      8          2
00055  *
00056  * where: c[0]    = 128
00057  *        c[1..7] = 128*sqrt(2)
00058  */
00059 
00060 static void idctrow(blk)
00061 short *blk;
00062 {
00063   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
00064 
00065   /* shortcut */
00066   if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
00067         (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
00068   {
00069     blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
00070     return;
00071   }
00072 
00073   x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
00074 
00075   /* first stage */
00076   x8 = W7*(x4+x5);
00077   x4 = x8 + (W1-W7)*x4;
00078   x5 = x8 - (W1+W7)*x5;
00079   x8 = W3*(x6+x7);
00080   x6 = x8 - (W3-W5)*x6;
00081   x7 = x8 - (W3+W5)*x7;
00082   
00083   /* second stage */
00084   x8 = x0 + x1;
00085   x0 -= x1;
00086   x1 = W6*(x3+x2);
00087   x2 = x1 - (W2+W6)*x2;
00088   x3 = x1 + (W2-W6)*x3;
00089   x1 = x4 + x6;
00090   x4 -= x6;
00091   x6 = x5 + x7;
00092   x5 -= x7;
00093   
00094   /* third stage */
00095   x7 = x8 + x3;
00096   x8 -= x3;
00097   x3 = x0 + x2;
00098   x0 -= x2;
00099   x2 = (181*(x4+x5)+128)>>8;
00100   x4 = (181*(x4-x5)+128)>>8;
00101   
00102   /* fourth stage */
00103   blk[0] = (x7+x1)>>8;
00104   blk[1] = (x3+x2)>>8;
00105   blk[2] = (x0+x4)>>8;
00106   blk[3] = (x8+x6)>>8;
00107   blk[4] = (x8-x6)>>8;
00108   blk[5] = (x0-x4)>>8;
00109   blk[6] = (x3-x2)>>8;
00110   blk[7] = (x7-x1)>>8;
00111 }
00112 
00113 /* column (vertical) IDCT
00114  *
00115  *             7                         pi         1
00116  * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
00117  *            l=0                        8          2
00118  *
00119  * where: c[0]    = 1/1024
00120  *        c[1..7] = (1/1024)*sqrt(2)
00121  */
00122 static void idctcol(blk)
00123 short *blk;
00124 {
00125   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
00126 
00127   /* shortcut */
00128   if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
00129         (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
00130   {
00131     blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
00132       iclp[(blk[8*0]+32)>>6];
00133     return;
00134   }
00135 
00136   x0 = (blk[8*0]<<8) + 8192;
00137 
00138   /* first stage */
00139   x8 = W7*(x4+x5) + 4;
00140   x4 = (x8+(W1-W7)*x4)>>3;
00141   x5 = (x8-(W1+W7)*x5)>>3;
00142   x8 = W3*(x6+x7) + 4;
00143   x6 = (x8-(W3-W5)*x6)>>3;
00144   x7 = (x8-(W3+W5)*x7)>>3;
00145   
00146   /* second stage */
00147   x8 = x0 + x1;
00148   x0 -= x1;
00149   x1 = W6*(x3+x2) + 4;
00150   x2 = (x1-(W2+W6)*x2)>>3;
00151   x3 = (x1+(W2-W6)*x3)>>3;
00152   x1 = x4 + x6;
00153   x4 -= x6;
00154   x6 = x5 + x7;
00155   x5 -= x7;
00156   
00157   /* third stage */
00158   x7 = x8 + x3;
00159   x8 -= x3;
00160   x3 = x0 + x2;
00161   x0 -= x2;
00162   x2 = (181*(x4+x5)+128)>>8;
00163   x4 = (181*(x4-x5)+128)>>8;
00164   
00165   /* fourth stage */
00166   blk[8*0] = iclp[(x7+x1)>>14];
00167   blk[8*1] = iclp[(x3+x2)>>14];
00168   blk[8*2] = iclp[(x0+x4)>>14];
00169   blk[8*3] = iclp[(x8+x6)>>14];
00170   blk[8*4] = iclp[(x8-x6)>>14];
00171   blk[8*5] = iclp[(x0-x4)>>14];
00172   blk[8*6] = iclp[(x3-x2)>>14];
00173   blk[8*7] = iclp[(x7-x1)>>14];
00174 }
00175 
00176 /* two dimensional inverse discrete cosine transform */
00177 void idct(block)
00178 short *block;
00179 {
00180   int i;
00181 
00182   for (i=0; i<8; i++)
00183     idctrow(block+8*i);
00184 
00185   for (i=0; i<8; i++)
00186     idctcol(block+i);
00187 }
00188 
00189 void init_idct()
00190 {
00191   int i;
00192 
00193   iclp = iclip+512;
00194   for (i= -512; i<512; i++)
00195     iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
00196 }

Generated on Mon May 8 22:27:08 2006 by  doxygen 1.3.9.1