1 /* idct.c, inverse fast discrete cosine transform */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
30 /**********************************************************/
31 /* inverse two dimensional DCT, Chen-Wang algorithm */
32 /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984) */
33 /* 32-bit integer arithmetic (8 bit coefficients) */
34 /* 11 mults, 29 adds per DCT */
36 /**********************************************************/
37 /* coefficients extended to 12 bit for IEEE1180-1990 */
38 /* compliance sE, 2.1.94 */
39 /**********************************************************/
41 /* this code assumes >> to be a two's-complement arithmetic */
42 /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2 */
46 #define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
47 #define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
48 #define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
49 #define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
50 #define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
51 #define W7 565 /* 2048*sqrt(2)*cos(7*pi/16) */
53 /* global declarations */
54 void init_idct _ANSI_ARGS_((void));
55 void idct _ANSI_ARGS_((short *block, unsigned char *temp));
58 static short iclip[1024]; /* clipping table */
61 /* private prototypes */
62 static void idctrow _ANSI_ARGS_((short *blk));
63 static void idctcol _ANSI_ARGS_((short *blk));
65 /* row (horizontal) IDCT
68 * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
72 * c[1..7] = 128*sqrt(2)
75 static void idctrow(blk)
78 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
81 if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
82 (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
84 blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
88 x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
102 x2 = x1 - (W2+W6)*x2;
103 x3 = x1 + (W2-W6)*x3;
114 x2 = (181*(x4+x5)+128)>>8;
115 x4 = (181*(x4-x5)+128)>>8;
128 /* column (vertical) IDCT
131 * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
134 * where: c[0] = 1/1024
135 * c[1..7] = (1/1024)*sqrt(2)
137 static void idctcol(blk)
140 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
143 if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
144 (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
146 blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
147 iclp[(blk[8*0]+32)>>6];
151 x0 = (blk[8*0]<<8) + 8192;
155 x4 = (x8+(W1-W7)*x4)>>3;
156 x5 = (x8-(W1+W7)*x5)>>3;
158 x6 = (x8-(W3-W5)*x6)>>3;
159 x7 = (x8-(W3+W5)*x7)>>3;
165 x2 = (x1-(W2+W6)*x2)>>3;
166 x3 = (x1+(W2-W6)*x3)>>3;
177 x2 = (181*(x4+x5)+128)>>8;
178 x4 = (181*(x4-x5)+128)>>8;
181 blk[8*0] = iclp[(x7+x1)>>14];
182 blk[8*1] = iclp[(x3+x2)>>14];
183 blk[8*2] = iclp[(x0+x4)>>14];
184 blk[8*3] = iclp[(x8+x6)>>14];
185 blk[8*4] = iclp[(x8-x6)>>14];
186 blk[8*5] = iclp[(x0-x4)>>14];
187 blk[8*6] = iclp[(x3-x2)>>14];
188 blk[8*7] = iclp[(x7-x1)>>14];
191 /* two dimensional inverse discrete cosine transform */
192 void idct(short *block, unsigned char *temp)
208 for (i= -512; i<512; i++)
209 iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);