[svn] / branches / release-1_0-branch / xvidcore / src / dct / idct.c Repository:
ViewVC logotype

Annotation of /branches/release-1_0-branch/xvidcore/src/dct/idct.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1382 - (view) (download)
Original Path: trunk/xvidcore/src/dct/idct.c

1 : edgomez 1382 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Inverse DCT -
5 :     *
6 :     * These routines are from Independent JPEG Group's free JPEG software
7 :     * Copyright (C) 1991-1998, Thomas G. Lane (see the file README.IJG)
8 :     *
9 :     * This program is free software ; you can redistribute it and/or modify
10 :     * it under the terms of the GNU General Public License as published by
11 :     * the Free Software Foundation ; either version 2 of the License, or
12 :     * (at your option) any later version.
13 :     *
14 :     * This program is distributed in the hope that it will be useful,
15 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
16 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 :     * GNU General Public License for more details.
18 :     *
19 :     * You should have received a copy of the GNU General Public License
20 :     * along with this program ; if not, write to the Free Software
21 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 :     *
23 :     * $Id: idct.c,v 1.7 2004-03-22 22:36:23 edgomez Exp $
24 :     *
25 :     ****************************************************************************/
26 : edgomez 851
27 :     /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
28 :    
29 :     /*
30 :     * Disclaimer of Warranty
31 : Isibaar 3 *
32 : edgomez 851 * These software programs are available to the user without any license fee or
33 :     * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
34 :     * any and all warranties, whether express, implied, or statuary, including any
35 :     * implied warranties or merchantability or of fitness for a particular
36 :     * purpose. In no event shall the copyright-holder be liable for any
37 :     * incidental, punitive, or consequential damages of any kind whatsoever
38 :     * arising from the use of these programs.
39 : Isibaar 3 *
40 : edgomez 851 * This disclaimer of warranty extends to the user of these programs and user's
41 :     * customers, employees, agents, transferees, successors, and assigns.
42 : Isibaar 3 *
43 : edgomez 851 * The MPEG Software Simulation Group does not represent or warrant that the
44 :     * programs furnished hereunder are free of infringement of any third-party
45 :     * patents.
46 : Isibaar 3 *
47 : edgomez 851 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
48 :     * are subject to royalty fees to patent holders. Many of these patents are
49 :     * general enough such that they are unavoidable regardless of implementation
50 :     * design.
51 : Isibaar 3 *
52 : edgomez 851 * MPEG2AVI
53 :     * --------
54 :     * v0.16B33 renamed the initialization function to init_idct_int32()
55 :     * v0.16B32 removed the unused idct_row() and idct_col() functions
56 :     * v0.16B3 changed var declarations to static, to enforce data align
57 :     * v0.16B22 idct_FAST() renamed to idct_int32()
58 :     * also merged idct_FAST() into a single function, to help VC++
59 :     * optimize it.
60 : edgomez 1382 *
61 : edgomez 851 * v0.14 changed int to long, to avoid confusion when compiling on x86
62 :     * platform ( in VC++ "int" -> 32bits )
63 :     */
64 : Isibaar 3
65 :     /**********************************************************/
66 :     /* inverse two dimensional DCT, Chen-Wang algorithm */
67 :     /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984) */
68 :     /* 32-bit integer arithmetic (8 bit coefficients) */
69 :     /* 11 mults, 29 adds per DCT */
70 :     /* sE, 18.8.91 */
71 :     /**********************************************************/
72 :     /* coefficients extended to 12 bit for IEEE1180-1990 */
73 :     /* compliance sE, 2.1.94 */
74 :     /**********************************************************/
75 :    
76 :     /* this code assumes >> to be a two's-complement arithmetic */
77 :     /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2 */
78 :    
79 :     #include "idct.h"
80 :    
81 : edgomez 195 #define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
82 :     #define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
83 :     #define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
84 :     #define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
85 :     #define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
86 :     #define W7 565 /* 2048*sqrt(2)*cos(7*pi/16) */
87 : Isibaar 3
88 :     /* private data */
89 : edgomez 195 static short iclip[1024]; /* clipping table */
90 : Isibaar 3 static short *iclp;
91 :    
92 :     /* private prototypes */
93 :    
94 :     /* row (horizontal) IDCT
95 :     *
96 :     * 7 pi 1
97 :     * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
98 :     * l=0 8 2
99 :     *
100 :     * where: c[0] = 128
101 :     * c[1..7] = 128*sqrt(2)
102 :     */
103 :    
104 : edgomez 1382 #if 0
105 : Isibaar 3 static void idctrow(blk)
106 :     short *blk;
107 :     {
108 :     int X0, X1, X2, X3, X4, X5, X6, X7, X8;
109 :    
110 : edgomez 1382 /* shortcut */
111 : Isibaar 3 if (!((X1 = blk[4]<<11) | (X2 = blk[6]) | (X3 = blk[2]) |
112 :     (X4 = blk[1]) | (X5 = blk[7]) | (X6 = blk[5]) | (X7 = blk[3])))
113 :     {
114 :     blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
115 :     return;
116 :     }
117 :    
118 : edgomez 1382 X0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
119 : Isibaar 3
120 : edgomez 1382 /* first stage */
121 : Isibaar 3 X8 = W7*(X4+X5);
122 :     X4 = X8 + (W1-W7)*X4;
123 :     X5 = X8 - (W1+W7)*X5;
124 :     X8 = W3*(X6+X7);
125 :     X6 = X8 - (W3-W5)*X6;
126 :     X7 = X8 - (W3+W5)*X7;
127 : edgomez 1382
128 :     /* second stage */
129 : Isibaar 3 X8 = X0 + X1;
130 :     X0 -= X1;
131 :     X1 = W6*(X3+X2);
132 :     X2 = X1 - (W2+W6)*X2;
133 :     X3 = X1 + (W2-W6)*X3;
134 :     X1 = X4 + X6;
135 :     X4 -= X6;
136 :     X6 = X5 + X7;
137 :     X5 -= X7;
138 : edgomez 1382
139 :     /* third stage */
140 : Isibaar 3 X7 = X8 + X3;
141 :     X8 -= X3;
142 :     X3 = X0 + X2;
143 :     X0 -= X2;
144 :     X2 = (181*(X4+X5)+128)>>8;
145 :     X4 = (181*(X4-X5)+128)>>8;
146 : edgomez 1382
147 :     /* fourth stage */
148 : Isibaar 3 blk[0] = (X7+X1)>>8;
149 :     blk[1] = (X3+X2)>>8;
150 :     blk[2] = (X0+X4)>>8;
151 :     blk[3] = (X8+X6)>>8;
152 :     blk[4] = (X8-X6)>>8;
153 :     blk[5] = (X0-X4)>>8;
154 :     blk[6] = (X3-X2)>>8;
155 :     blk[7] = (X7-X1)>>8;
156 : edgomez 1382 }
157 :     #endif
158 : Isibaar 3
159 :     /* column (vertical) IDCT
160 :     *
161 :     * 7 pi 1
162 :     * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
163 :     * l=0 8 2
164 :     *
165 :     * where: c[0] = 1/1024
166 :     * c[1..7] = (1/1024)*sqrt(2)
167 :     */
168 : edgomez 1382
169 :     #if 0
170 : Isibaar 3 static void idctcol(blk)
171 :     short *blk;
172 :     {
173 :     int X0, X1, X2, X3, X4, X5, X6, X7, X8;
174 :    
175 : edgomez 1382 /* shortcut */
176 : Isibaar 3 if (!((X1 = (blk[8*4]<<8)) | (X2 = blk[8*6]) | (X3 = blk[8*2]) |
177 :     (X4 = blk[8*1]) | (X5 = blk[8*7]) | (X6 = blk[8*5]) | (X7 = blk[8*3])))
178 :     {
179 :     blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
180 :     iclp[(blk[8*0]+32)>>6];
181 :     return;
182 :     }
183 :    
184 :     X0 = (blk[8*0]<<8) + 8192;
185 :    
186 : edgomez 1382 /* first stage */
187 : Isibaar 3 X8 = W7*(X4+X5) + 4;
188 :     X4 = (X8+(W1-W7)*X4)>>3;
189 :     X5 = (X8-(W1+W7)*X5)>>3;
190 :     X8 = W3*(X6+X7) + 4;
191 :     X6 = (X8-(W3-W5)*X6)>>3;
192 :     X7 = (X8-(W3+W5)*X7)>>3;
193 : edgomez 1382
194 :     /* second stage */
195 : Isibaar 3 X8 = X0 + X1;
196 :     X0 -= X1;
197 :     X1 = W6*(X3+X2) + 4;
198 :     X2 = (X1-(W2+W6)*X2)>>3;
199 :     X3 = (X1+(W2-W6)*X3)>>3;
200 :     X1 = X4 + X6;
201 :     X4 -= X6;
202 :     X6 = X5 + X7;
203 :     X5 -= X7;
204 : edgomez 1382
205 :     /* third stage */
206 : Isibaar 3 X7 = X8 + X3;
207 :     X8 -= X3;
208 :     X3 = X0 + X2;
209 :     X0 -= X2;
210 :     X2 = (181*(X4+X5)+128)>>8;
211 :     X4 = (181*(X4-X5)+128)>>8;
212 : edgomez 1382
213 :     /* fourth stage */
214 : Isibaar 3 blk[8*0] = iclp[(X7+X1)>>14];
215 :     blk[8*1] = iclp[(X3+X2)>>14];
216 :     blk[8*2] = iclp[(X0+X4)>>14];
217 :     blk[8*3] = iclp[(X8+X6)>>14];
218 :     blk[8*4] = iclp[(X8-X6)>>14];
219 :     blk[8*5] = iclp[(X0-X4)>>14];
220 :     blk[8*6] = iclp[(X3-X2)>>14];
221 :     blk[8*7] = iclp[(X7-X1)>>14];
222 : edgomez 1382 }
223 :     #endif
224 : Isibaar 3
225 : edgomez 1382 /* function pointer */
226 : Isibaar 3 idctFuncPtr idct;
227 :    
228 :     /* two dimensional inverse discrete cosine transform */
229 : edgomez 195 void
230 :     idct_int32(short *const block)
231 : Isibaar 3 {
232 :    
233 : edgomez 1382 /*
234 :     * idct_int32_init() must be called before the first call to this
235 :     * function!
236 :     */
237 : Isibaar 3
238 :    
239 : edgomez 1382 #if 0
240 :     int i;
241 :     long i;
242 : Isibaar 3
243 : edgomez 1382 for (i=0; i<8; i++)
244 :     idctrow(block+8*i);
245 : Isibaar 3
246 : edgomez 1382 for (i=0; i<8; i++)
247 :     idctcol(block+i);
248 :     #endif
249 :    
250 : edgomez 195 static short *blk;
251 :     static long i;
252 :     static long X0, X1, X2, X3, X4, X5, X6, X7, X8;
253 : Isibaar 3
254 :    
255 : edgomez 1382 for (i = 0; i < 8; i++) /* idct rows */
256 : Isibaar 3 {
257 : edgomez 195 blk = block + (i << 3);
258 :     if (!
259 :     ((X1 = blk[4] << 11) | (X2 = blk[6]) | (X3 = blk[2]) | (X4 =
260 :     blk[1]) |
261 :     (X5 = blk[7]) | (X6 = blk[5]) | (X7 = blk[3]))) {
262 :     blk[0] = blk[1] = blk[2] = blk[3] = blk[4] = blk[5] = blk[6] =
263 :     blk[7] = blk[0] << 3;
264 :     continue;
265 :     }
266 : Isibaar 3
267 : edgomez 1382 X0 = (blk[0] << 11) + 128; /* for proper rounding in the fourth stage */
268 : Isibaar 3
269 : edgomez 1382 /* first stage */
270 : edgomez 195 X8 = W7 * (X4 + X5);
271 :     X4 = X8 + (W1 - W7) * X4;
272 :     X5 = X8 - (W1 + W7) * X5;
273 :     X8 = W3 * (X6 + X7);
274 :     X6 = X8 - (W3 - W5) * X6;
275 :     X7 = X8 - (W3 + W5) * X7;
276 : Isibaar 3
277 : edgomez 1382 /* second stage */
278 : edgomez 195 X8 = X0 + X1;
279 :     X0 -= X1;
280 :     X1 = W6 * (X3 + X2);
281 :     X2 = X1 - (W2 + W6) * X2;
282 :     X3 = X1 + (W2 - W6) * X3;
283 :     X1 = X4 + X6;
284 :     X4 -= X6;
285 :     X6 = X5 + X7;
286 :     X5 -= X7;
287 : Isibaar 3
288 : edgomez 1382 /* third stage */
289 : edgomez 195 X7 = X8 + X3;
290 :     X8 -= X3;
291 :     X3 = X0 + X2;
292 :     X0 -= X2;
293 :     X2 = (181 * (X4 + X5) + 128) >> 8;
294 :     X4 = (181 * (X4 - X5) + 128) >> 8;
295 : Isibaar 3
296 : edgomez 1382 /* fourth stage */
297 : Isibaar 3
298 : edgomez 195 blk[0] = (short) ((X7 + X1) >> 8);
299 :     blk[1] = (short) ((X3 + X2) >> 8);
300 :     blk[2] = (short) ((X0 + X4) >> 8);
301 :     blk[3] = (short) ((X8 + X6) >> 8);
302 :     blk[4] = (short) ((X8 - X6) >> 8);
303 :     blk[5] = (short) ((X0 - X4) >> 8);
304 :     blk[6] = (short) ((X3 - X2) >> 8);
305 :     blk[7] = (short) ((X7 - X1) >> 8);
306 :    
307 : edgomez 1382 } /* end for ( i = 0; i < 8; ++i ) IDCT-rows */
308 : edgomez 195
309 :    
310 :    
311 : edgomez 1382 for (i = 0; i < 8; i++) /* idct columns */
312 : Isibaar 3 {
313 : edgomez 195 blk = block + i;
314 : edgomez 1382 /* shortcut */
315 : edgomez 195 if (!
316 :     ((X1 = (blk[8 * 4] << 8)) | (X2 = blk[8 * 6]) | (X3 =
317 :     blk[8 *
318 :     2]) | (X4 =
319 :     blk[8 *
320 :     1])
321 :     | (X5 = blk[8 * 7]) | (X6 = blk[8 * 5]) | (X7 = blk[8 * 3]))) {
322 :     blk[8 * 0] = blk[8 * 1] = blk[8 * 2] = blk[8 * 3] = blk[8 * 4] =
323 :     blk[8 * 5] = blk[8 * 6] = blk[8 * 7] =
324 :     iclp[(blk[8 * 0] + 32) >> 6];
325 :     continue;
326 :     }
327 :    
328 :     X0 = (blk[8 * 0] << 8) + 8192;
329 :    
330 : edgomez 1382 /* first stage */
331 : edgomez 195 X8 = W7 * (X4 + X5) + 4;
332 :     X4 = (X8 + (W1 - W7) * X4) >> 3;
333 :     X5 = (X8 - (W1 + W7) * X5) >> 3;
334 :     X8 = W3 * (X6 + X7) + 4;
335 :     X6 = (X8 - (W3 - W5) * X6) >> 3;
336 :     X7 = (X8 - (W3 + W5) * X7) >> 3;
337 :    
338 : edgomez 1382 /* second stage */
339 : edgomez 195 X8 = X0 + X1;
340 :     X0 -= X1;
341 :     X1 = W6 * (X3 + X2) + 4;
342 :     X2 = (X1 - (W2 + W6) * X2) >> 3;
343 :     X3 = (X1 + (W2 - W6) * X3) >> 3;
344 :     X1 = X4 + X6;
345 :     X4 -= X6;
346 :     X6 = X5 + X7;
347 :     X5 -= X7;
348 :    
349 : edgomez 1382 /* third stage */
350 : edgomez 195 X7 = X8 + X3;
351 :     X8 -= X3;
352 :     X3 = X0 + X2;
353 :     X0 -= X2;
354 :     X2 = (181 * (X4 + X5) + 128) >> 8;
355 :     X4 = (181 * (X4 - X5) + 128) >> 8;
356 :    
357 : edgomez 1382 /* fourth stage */
358 : edgomez 195 blk[8 * 0] = iclp[(X7 + X1) >> 14];
359 :     blk[8 * 1] = iclp[(X3 + X2) >> 14];
360 :     blk[8 * 2] = iclp[(X0 + X4) >> 14];
361 :     blk[8 * 3] = iclp[(X8 + X6) >> 14];
362 :     blk[8 * 4] = iclp[(X8 - X6) >> 14];
363 :     blk[8 * 5] = iclp[(X0 - X4) >> 14];
364 :     blk[8 * 6] = iclp[(X3 - X2) >> 14];
365 :     blk[8 * 7] = iclp[(X7 - X1) >> 14];
366 : Isibaar 3 }
367 :    
368 : edgomez 1382 } /* end function idct_int32(block) */
369 : Isibaar 3
370 :    
371 : edgomez 195 void
372 : edgomez 851 idct_int32_init()
373 : Isibaar 3 {
374 : edgomez 195 int i;
375 : Isibaar 3
376 : edgomez 195 iclp = iclip + 512;
377 :     for (i = -512; i < 512; i++)
378 :     iclp[i] = (i < -256) ? -256 : ((i > 255) ? 255 : i);
379 : Isibaar 3 }

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