[svn] / trunk / xvidcore / src / dct / idct.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/dct/idct.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (view) (download)

1 : Isibaar 3 /* idct.c, inverse fast discrete cosine transform */
2 :    
3 :     /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4 :    
5 :     /*
6 :     * Disclaimer of Warranty
7 :     *
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.
15 :     *
16 :     * This disclaimer of warranty extends to the user of these programs and user's
17 :     * customers, employees, agents, transferees, successors, and assigns.
18 :     *
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.
22 :     *
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.
27 :     *
28 :     * MPEG2AVI
29 :     * --------
30 :     * v0.16B33 renamed the initialization function to init_idct_int32()
31 :     * v0.16B32 removed the unused idct_row() and idct_col() functions
32 :     * v0.16B3 changed var declarations to static, to enforce data align
33 :     * v0.16B22 idct_FAST() renamed to idct_int32()
34 :     * also merged idct_FAST() into a single function, to help VC++
35 :     * optimize it.
36 :     *
37 :     * v0.14 changed int to long, to avoid confusion when compiling on x86
38 :     * platform ( in VC++ "int" -> 32bits )
39 :     */
40 :    
41 :     /**********************************************************/
42 :     /* inverse two dimensional DCT, Chen-Wang algorithm */
43 :     /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984) */
44 :     /* 32-bit integer arithmetic (8 bit coefficients) */
45 :     /* 11 mults, 29 adds per DCT */
46 :     /* sE, 18.8.91 */
47 :     /**********************************************************/
48 :     /* coefficients extended to 12 bit for IEEE1180-1990 */
49 :     /* compliance sE, 2.1.94 */
50 :     /**********************************************************/
51 :    
52 :     /* this code assumes >> to be a two's-complement arithmetic */
53 :     /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2 */
54 :    
55 :     //#include <windows.h>
56 :     #include "idct.h"
57 :    
58 :     #define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
59 :     #define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
60 :     #define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
61 :     #define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
62 :     #define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
63 :     #define W7 565 /* 2048*sqrt(2)*cos(7*pi/16) */
64 :    
65 :    
66 :     /* global declarations */
67 :     //void init_idct_int32 (void);
68 :     //void idct_int32 (short *block);
69 :    
70 :     /* private data */
71 :     static short iclip[1024]; /* clipping table */
72 :     static short *iclp;
73 :    
74 :     /* private prototypes */
75 :     //static void idctrow _ANSI_ARGS_((short *blk));
76 :     //static void idctcol _ANSI_ARGS_((short *blk));
77 :    
78 :     /* row (horizontal) IDCT
79 :     *
80 :     * 7 pi 1
81 :     * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
82 :     * l=0 8 2
83 :     *
84 :     * where: c[0] = 128
85 :     * c[1..7] = 128*sqrt(2)
86 :     */
87 :    
88 :     /*
89 :     static void idctrow(blk)
90 :     short *blk;
91 :     {
92 :     int X0, X1, X2, X3, X4, X5, X6, X7, X8;
93 :    
94 :     // shortcut
95 :     if (!((X1 = blk[4]<<11) | (X2 = blk[6]) | (X3 = blk[2]) |
96 :     (X4 = blk[1]) | (X5 = blk[7]) | (X6 = blk[5]) | (X7 = blk[3])))
97 :     {
98 :     blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
99 :     return;
100 :     }
101 :    
102 :     X0 = (blk[0]<<11) + 128; // for proper rounding in the fourth stage
103 :    
104 :     // first stage
105 :     X8 = W7*(X4+X5);
106 :     X4 = X8 + (W1-W7)*X4;
107 :     X5 = X8 - (W1+W7)*X5;
108 :     X8 = W3*(X6+X7);
109 :     X6 = X8 - (W3-W5)*X6;
110 :     X7 = X8 - (W3+W5)*X7;
111 :    
112 :     // second stage
113 :     X8 = X0 + X1;
114 :     X0 -= X1;
115 :     X1 = W6*(X3+X2);
116 :     X2 = X1 - (W2+W6)*X2;
117 :     X3 = X1 + (W2-W6)*X3;
118 :     X1 = X4 + X6;
119 :     X4 -= X6;
120 :     X6 = X5 + X7;
121 :     X5 -= X7;
122 :    
123 :     // third stage
124 :     X7 = X8 + X3;
125 :     X8 -= X3;
126 :     X3 = X0 + X2;
127 :     X0 -= X2;
128 :     X2 = (181*(X4+X5)+128)>>8;
129 :     X4 = (181*(X4-X5)+128)>>8;
130 :    
131 :     // fourth stage
132 :     blk[0] = (X7+X1)>>8;
133 :     blk[1] = (X3+X2)>>8;
134 :     blk[2] = (X0+X4)>>8;
135 :     blk[3] = (X8+X6)>>8;
136 :     blk[4] = (X8-X6)>>8;
137 :     blk[5] = (X0-X4)>>8;
138 :     blk[6] = (X3-X2)>>8;
139 :     blk[7] = (X7-X1)>>8;
140 :     }*/
141 :    
142 :     /* column (vertical) IDCT
143 :     *
144 :     * 7 pi 1
145 :     * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
146 :     * l=0 8 2
147 :     *
148 :     * where: c[0] = 1/1024
149 :     * c[1..7] = (1/1024)*sqrt(2)
150 :     */
151 :     /*
152 :     static void idctcol(blk)
153 :     short *blk;
154 :     {
155 :     int X0, X1, X2, X3, X4, X5, X6, X7, X8;
156 :    
157 :     // shortcut
158 :     if (!((X1 = (blk[8*4]<<8)) | (X2 = blk[8*6]) | (X3 = blk[8*2]) |
159 :     (X4 = blk[8*1]) | (X5 = blk[8*7]) | (X6 = blk[8*5]) | (X7 = blk[8*3])))
160 :     {
161 :     blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
162 :     iclp[(blk[8*0]+32)>>6];
163 :     return;
164 :     }
165 :    
166 :     X0 = (blk[8*0]<<8) + 8192;
167 :    
168 :     // first stage
169 :     X8 = W7*(X4+X5) + 4;
170 :     X4 = (X8+(W1-W7)*X4)>>3;
171 :     X5 = (X8-(W1+W7)*X5)>>3;
172 :     X8 = W3*(X6+X7) + 4;
173 :     X6 = (X8-(W3-W5)*X6)>>3;
174 :     X7 = (X8-(W3+W5)*X7)>>3;
175 :    
176 :     // second stage
177 :     X8 = X0 + X1;
178 :     X0 -= X1;
179 :     X1 = W6*(X3+X2) + 4;
180 :     X2 = (X1-(W2+W6)*X2)>>3;
181 :     X3 = (X1+(W2-W6)*X3)>>3;
182 :     X1 = X4 + X6;
183 :     X4 -= X6;
184 :     X6 = X5 + X7;
185 :     X5 -= X7;
186 :    
187 :     // third stage
188 :     X7 = X8 + X3;
189 :     X8 -= X3;
190 :     X3 = X0 + X2;
191 :     X0 -= X2;
192 :     X2 = (181*(X4+X5)+128)>>8;
193 :     X4 = (181*(X4-X5)+128)>>8;
194 :    
195 :     // fourth stage
196 :     blk[8*0] = iclp[(X7+X1)>>14];
197 :     blk[8*1] = iclp[(X3+X2)>>14];
198 :     blk[8*2] = iclp[(X0+X4)>>14];
199 :     blk[8*3] = iclp[(X8+X6)>>14];
200 :     blk[8*4] = iclp[(X8-X6)>>14];
201 :     blk[8*5] = iclp[(X0-X4)>>14];
202 :     blk[8*6] = iclp[(X3-X2)>>14];
203 :     blk[8*7] = iclp[(X7-X1)>>14];
204 :     }*/
205 :    
206 :     // function pointer
207 :     idctFuncPtr idct;
208 :    
209 :     /* two dimensional inverse discrete cosine transform */
210 :     //void j_rev_dct(block)
211 :     //short *block;
212 :     void idct_int32(short * const block)
213 :     {
214 :    
215 :     // idct_int32_init() must be called before the first call to this function!
216 :    
217 :    
218 :     /*int i;
219 :     long i;
220 :    
221 :     for (i=0; i<8; i++)
222 :     idctrow(block+8*i);
223 :    
224 :     for (i=0; i<8; i++)
225 :     idctcol(block+i);*/
226 :     static short *blk;
227 :     static long i;
228 :     static long X0, X1, X2, X3, X4, X5, X6, X7, X8;
229 :    
230 :    
231 :     for (i=0; i<8; i++) // idct rows
232 :     {
233 :     blk = block+(i<<3);
234 :     if (!((X1 = blk[4]<<11) | (X2 = blk[6]) | (X3 = blk[2]) |
235 :     (X4 = blk[1]) | (X5 = blk[7]) | (X6 = blk[5]) | (X7 = blk[3])))
236 :     {
237 :     blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
238 :     continue;
239 :     }
240 :    
241 :     X0 = (blk[0]<<11) + 128; // for proper rounding in the fourth stage
242 :    
243 :     // first stage
244 :     X8 = W7*(X4+X5);
245 :     X4 = X8 + (W1-W7)*X4;
246 :     X5 = X8 - (W1+W7)*X5;
247 :     X8 = W3*(X6+X7);
248 :     X6 = X8 - (W3-W5)*X6;
249 :     X7 = X8 - (W3+W5)*X7;
250 :    
251 :     // second stage
252 :     X8 = X0 + X1;
253 :     X0 -= X1;
254 :     X1 = W6*(X3+X2);
255 :     X2 = X1 - (W2+W6)*X2;
256 :     X3 = X1 + (W2-W6)*X3;
257 :     X1 = X4 + X6;
258 :     X4 -= X6;
259 :     X6 = X5 + X7;
260 :     X5 -= X7;
261 :    
262 :     // third stage
263 :     X7 = X8 + X3;
264 :     X8 -= X3;
265 :     X3 = X0 + X2;
266 :     X0 -= X2;
267 :     X2 = (181*(X4+X5)+128)>>8;
268 :     X4 = (181*(X4-X5)+128)>>8;
269 :    
270 :     // fourth stage
271 :    
272 :     blk[0] = (short)((X7+X1)>>8);
273 :     blk[1] = (short)((X3+X2)>>8);
274 :     blk[2] = (short)((X0+X4)>>8);
275 :     blk[3] = (short)((X8+X6)>>8);
276 :     blk[4] = (short)((X8-X6)>>8);
277 :     blk[5] = (short)((X0-X4)>>8);
278 :     blk[6] = (short)((X3-X2)>>8);
279 :     blk[7] = (short)((X7-X1)>>8);
280 :    
281 :     } // end for ( i = 0; i < 8; ++i ) IDCT-rows
282 :    
283 :    
284 :    
285 :     for (i=0; i<8; i++) // idct columns
286 :     {
287 :     blk = block + i;
288 :     // shortcut
289 :     if (!((X1 = (blk[8*4]<<8)) | (X2 = blk[8*6]) | (X3 = blk[8*2]) |
290 :     (X4 = blk[8*1]) | (X5 = blk[8*7]) | (X6 = blk[8*5]) | (X7 = blk[8*3])))
291 :     {
292 :     blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=
293 :     blk[8*5]=blk[8*6]=blk[8*7]=iclp[(blk[8*0]+32)>>6];
294 :     continue;
295 :     }
296 :    
297 :     X0 = (blk[8*0]<<8) + 8192;
298 :    
299 :     // first stage
300 :     X8 = W7*(X4+X5) + 4;
301 :     X4 = (X8+(W1-W7)*X4)>>3;
302 :     X5 = (X8-(W1+W7)*X5)>>3;
303 :     X8 = W3*(X6+X7) + 4;
304 :     X6 = (X8-(W3-W5)*X6)>>3;
305 :     X7 = (X8-(W3+W5)*X7)>>3;
306 :    
307 :     // second stage
308 :     X8 = X0 + X1;
309 :     X0 -= X1;
310 :     X1 = W6*(X3+X2) + 4;
311 :     X2 = (X1-(W2+W6)*X2)>>3;
312 :     X3 = (X1+(W2-W6)*X3)>>3;
313 :     X1 = X4 + X6;
314 :     X4 -= X6;
315 :     X6 = X5 + X7;
316 :     X5 -= X7;
317 :    
318 :     // third stage
319 :     X7 = X8 + X3;
320 :     X8 -= X3;
321 :     X3 = X0 + X2;
322 :     X0 -= X2;
323 :     X2 = (181*(X4+X5)+128)>>8;
324 :     X4 = (181*(X4-X5)+128)>>8;
325 :    
326 :     // fourth stage
327 :     blk[8*0] = iclp[(X7+X1)>>14];
328 :     blk[8*1] = iclp[(X3+X2)>>14];
329 :     blk[8*2] = iclp[(X0+X4)>>14];
330 :     blk[8*3] = iclp[(X8+X6)>>14];
331 :     blk[8*4] = iclp[(X8-X6)>>14];
332 :     blk[8*5] = iclp[(X0-X4)>>14];
333 :     blk[8*6] = iclp[(X3-X2)>>14];
334 :     blk[8*7] = iclp[(X7-X1)>>14];
335 :     }
336 :    
337 :     } // end function idct_int32(block)
338 :    
339 :    
340 :     //void
341 :     //idct_int32_init()
342 :     void idct_int32_init()
343 :     {
344 :     int i;
345 :    
346 :     iclp = iclip+512;
347 :     for (i= -512; i<512; i++)
348 :     iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
349 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4