r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / mpeg2enc / idct.c
blobe0bab11d485b137a4a879df4b612dafc095d8aa4
1 /* idct.c, inverse fast discrete cosine transform */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
5 /*
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
21 * patents.
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
26 * design.
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 */
35 /* sE, 18.8.91 */
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 */
44 #include "config.h"
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));
57 /* private data */
58 static short iclip[1024]; /* clipping table */
59 static short *iclp;
61 /* private prototypes */
62 static void idctrow _ANSI_ARGS_((short *blk));
63 static void idctcol _ANSI_ARGS_((short *blk));
65 /* row (horizontal) IDCT
67 * 7 pi 1
68 * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
69 * l=0 8 2
71 * where: c[0] = 128
72 * c[1..7] = 128*sqrt(2)
75 static void idctrow(blk)
76 short *blk;
78 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
80 /* shortcut */
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;
85 return;
88 x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
90 /* first stage */
91 x8 = W7*(x4+x5);
92 x4 = x8 + (W1-W7)*x4;
93 x5 = x8 - (W1+W7)*x5;
94 x8 = W3*(x6+x7);
95 x6 = x8 - (W3-W5)*x6;
96 x7 = x8 - (W3+W5)*x7;
98 /* second stage */
99 x8 = x0 + x1;
100 x0 -= x1;
101 x1 = W6*(x3+x2);
102 x2 = x1 - (W2+W6)*x2;
103 x3 = x1 + (W2-W6)*x3;
104 x1 = x4 + x6;
105 x4 -= x6;
106 x6 = x5 + x7;
107 x5 -= x7;
109 /* third stage */
110 x7 = x8 + x3;
111 x8 -= x3;
112 x3 = x0 + x2;
113 x0 -= x2;
114 x2 = (181*(x4+x5)+128)>>8;
115 x4 = (181*(x4-x5)+128)>>8;
117 /* fourth stage */
118 blk[0] = (x7+x1)>>8;
119 blk[1] = (x3+x2)>>8;
120 blk[2] = (x0+x4)>>8;
121 blk[3] = (x8+x6)>>8;
122 blk[4] = (x8-x6)>>8;
123 blk[5] = (x0-x4)>>8;
124 blk[6] = (x3-x2)>>8;
125 blk[7] = (x7-x1)>>8;
128 /* column (vertical) IDCT
130 * 7 pi 1
131 * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
132 * l=0 8 2
134 * where: c[0] = 1/1024
135 * c[1..7] = (1/1024)*sqrt(2)
137 static void idctcol(blk)
138 short *blk;
140 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
142 /* shortcut */
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];
148 return;
151 x0 = (blk[8*0]<<8) + 8192;
153 /* first stage */
154 x8 = W7*(x4+x5) + 4;
155 x4 = (x8+(W1-W7)*x4)>>3;
156 x5 = (x8-(W1+W7)*x5)>>3;
157 x8 = W3*(x6+x7) + 4;
158 x6 = (x8-(W3-W5)*x6)>>3;
159 x7 = (x8-(W3+W5)*x7)>>3;
161 /* second stage */
162 x8 = x0 + x1;
163 x0 -= x1;
164 x1 = W6*(x3+x2) + 4;
165 x2 = (x1-(W2+W6)*x2)>>3;
166 x3 = (x1+(W2-W6)*x3)>>3;
167 x1 = x4 + x6;
168 x4 -= x6;
169 x6 = x5 + x7;
170 x5 -= x7;
172 /* third stage */
173 x7 = x8 + x3;
174 x8 -= x3;
175 x3 = x0 + x2;
176 x0 -= x2;
177 x2 = (181*(x4+x5)+128)>>8;
178 x4 = (181*(x4-x5)+128)>>8;
180 /* fourth stage */
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)
194 int i;
196 for (i=0; i<8; i++)
197 idctrow(block+8*i);
199 for (i=0; i<8; i++)
200 idctcol(block+i);
203 void init_idct()
205 int i;
207 iclp = iclip+512;
208 for (i= -512; i<512; i++)
209 iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);