comparison libmpeg2/idct.c @ 1:3b5f5d1c5041

Initial revision
author arpi_esp
date Sat, 24 Feb 2001 20:28:24 +0000
parents
children 846535ace7a2
comparison
equal deleted inserted replaced
0:c1bb2c071d63 1:3b5f5d1c5041
1 /*
2 * idct.c
3 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
4 *
5 * Portions of this code are from the MPEG software simulation group
6 * idct implementation. This code will be replaced with a new
7 * implementation soon.
8 *
9 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
10 *
11 * mpeg2dec is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * mpeg2dec is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /**********************************************************/
27 /* inverse two dimensional DCT, Chen-Wang algorithm */
28 /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984) */
29 /* 32-bit integer arithmetic (8 bit coefficients) */
30 /* 11 mults, 29 adds per DCT */
31 /* sE, 18.8.91 */
32 /**********************************************************/
33 /* coefficients extended to 12 bit for IEEE1180-1990 */
34 /* compliance sE, 2.1.94 */
35 /**********************************************************/
36
37 /* this code assumes >> to be a two's-complement arithmetic */
38 /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2 */
39
40 #include "config.h"
41
42 #include <stdio.h>
43 #include <inttypes.h>
44
45 #include "mpeg2_internal.h"
46 #include "mm_accel.h"
47
48 #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
49 #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
50 #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
51 #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
52 #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
53 #define W7 565 /* 2048*sqrt (2)*cos (7*pi/16) */
54
55
56 // idct main entry point
57 void (*idct_block_copy) (int16_t * block, uint8_t * dest, int stride);
58 void (*idct_block_add) (int16_t * block, uint8_t * dest, int stride);
59
60 static void idct_block_copy_c (int16_t *block, uint8_t * dest, int stride);
61 static void idct_block_add_c (int16_t *block, uint8_t * dest, int stride);
62
63 static uint8_t clip_lut[1024];
64 #define CLIP(i) ((clip_lut+384)[ (i)])
65
66 void idct_init (void)
67 {
68 #ifdef ARCH_X86
69 if (config.flags & MM_ACCEL_X86_MMXEXT) {
70 fprintf (stderr, "Using MMXEXT for IDCT transform\n");
71 idct_block_copy = idct_block_copy_mmxext;
72 idct_block_add = idct_block_add_mmxext;
73 idct_mmx_init ();
74 } else if (config.flags & MM_ACCEL_X86_MMX) {
75 fprintf (stderr, "Using MMX for IDCT transform\n");
76 idct_block_copy = idct_block_copy_mmx;
77 idct_block_add = idct_block_add_mmx;
78 idct_mmx_init ();
79 } else
80 #endif
81 #ifdef LIBMPEG2_MLIB
82 if (config.flags & MM_ACCEL_MLIB) {
83 fprintf (stderr, "Using mlib for IDCT transform\n");
84 idct_block_copy = idct_block_copy_mlib;
85 idct_block_add = idct_block_add_mlib;
86 } else
87 #endif
88 {
89 int i;
90
91 fprintf (stderr, "No accelerated IDCT transform found\n");
92 idct_block_copy = idct_block_copy_c;
93 idct_block_add = idct_block_add_c;
94 for (i = -384; i < 640; i++)
95 clip_lut[i+384] = (i < 0) ? 0 : ((i > 255) ? 255 : i);
96 }
97 }
98
99 /* row (horizontal) IDCT
100 *
101 * 7 pi 1
102 * dst[k] = sum c[l] * src[l] * cos ( -- * ( k + - ) * l )
103 * l=0 8 2
104 *
105 * where: c[0] = 128
106 * c[1..7] = 128*sqrt (2)
107 */
108
109 static void inline idct_row (int16_t * block)
110 {
111 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
112
113 x1 = block[4] << 11;
114 x2 = block[6];
115 x3 = block[2];
116 x4 = block[1];
117 x5 = block[7];
118 x6 = block[5];
119 x7 = block[3];
120
121 /* shortcut */
122 if (! (x1 | x2 | x3 | x4 | x5 | x6 | x7 )) {
123 block[0] = block[1] = block[2] = block[3] = block[4] =
124 block[5] = block[6] = block[7] = block[0]<<3;
125 return;
126 }
127
128 x0 = (block[0] << 11) + 128; /* for proper rounding in the fourth stage */
129
130 /* first stage */
131 x8 = W7 * (x4 + x5);
132 x4 = x8 + (W1 - W7) * x4;
133 x5 = x8 - (W1 + W7) * x5;
134 x8 = W3 * (x6 + x7);
135 x6 = x8 - (W3 - W5) * x6;
136 x7 = x8 - (W3 + W5) * x7;
137
138 /* second stage */
139 x8 = x0 + x1;
140 x0 -= x1;
141 x1 = W6 * (x3 + x2);
142 x2 = x1 - (W2 + W6) * x2;
143 x3 = x1 + (W2 - W6) * x3;
144 x1 = x4 + x6;
145 x4 -= x6;
146 x6 = x5 + x7;
147 x5 -= x7;
148
149 /* third stage */
150 x7 = x8 + x3;
151 x8 -= x3;
152 x3 = x0 + x2;
153 x0 -= x2;
154 x2 = (181 * (x4 + x5) + 128) >> 8;
155 x4 = (181 * (x4 - x5) + 128) >> 8;
156
157 /* fourth stage */
158 block[0] = (x7 + x1) >> 8;
159 block[1] = (x3 + x2) >> 8;
160 block[2] = (x0 + x4) >> 8;
161 block[3] = (x8 + x6) >> 8;
162 block[4] = (x8 - x6) >> 8;
163 block[5] = (x0 - x4) >> 8;
164 block[6] = (x3 - x2) >> 8;
165 block[7] = (x7 - x1) >> 8;
166 }
167
168 /* column (vertical) IDCT
169 *
170 * 7 pi 1
171 * dst[8*k] = sum c[l] * src[8*l] * cos ( -- * ( k + - ) * l )
172 * l=0 8 2
173 *
174 * where: c[0] = 1/1024
175 * c[1..7] = (1/1024)*sqrt (2)
176 */
177
178 static void inline idct_col (int16_t *block)
179 {
180 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
181
182 /* shortcut */
183 x1 = block [8*4] << 8;
184 x2 = block [8*6];
185 x3 = block [8*2];
186 x4 = block [8*1];
187 x5 = block [8*7];
188 x6 = block [8*5];
189 x7 = block [8*3];
190
191 #if 0
192 if (! (x1 | x2 | x3 | x4 | x5 | x6 | x7 )) {
193 block[8*0] = block[8*1] = block[8*2] = block[8*3] = block[8*4] =
194 block[8*5] = block[8*6] = block[8*7] = (block[8*0] + 32) >> 6;
195 return;
196 }
197 #endif
198
199 x0 = (block[8*0] << 8) + 8192;
200
201 /* first stage */
202 x8 = W7 * (x4 + x5) + 4;
203 x4 = (x8 + (W1 - W7) * x4) >> 3;
204 x5 = (x8 - (W1 + W7) * x5) >> 3;
205 x8 = W3 * (x6 + x7) + 4;
206 x6 = (x8 - (W3 - W5) * x6) >> 3;
207 x7 = (x8 - (W3 + W5) * x7) >> 3;
208
209 /* second stage */
210 x8 = x0 + x1;
211 x0 -= x1;
212 x1 = W6 * (x3 + x2) + 4;
213 x2 = (x1 - (W2 + W6) * x2) >> 3;
214 x3 = (x1 + (W2 - W6) * x3) >> 3;
215 x1 = x4 + x6;
216 x4 -= x6;
217 x6 = x5 + x7;
218 x5 -= x7;
219
220 /* third stage */
221 x7 = x8 + x3;
222 x8 -= x3;
223 x3 = x0 + x2;
224 x0 -= x2;
225 x2 = (181 * (x4 + x5) + 128) >> 8;
226 x4 = (181 * (x4 - x5) + 128) >> 8;
227
228 /* fourth stage */
229 block[8*0] = (x7 + x1) >> 14;
230 block[8*1] = (x3 + x2) >> 14;
231 block[8*2] = (x0 + x4) >> 14;
232 block[8*3] = (x8 + x6) >> 14;
233 block[8*4] = (x8 - x6) >> 14;
234 block[8*5] = (x0 - x4) >> 14;
235 block[8*6] = (x3 - x2) >> 14;
236 block[8*7] = (x7 - x1) >> 14;
237 }
238
239 void idct_block_copy_c (int16_t * block, uint8_t * dest, int stride)
240 {
241 int i;
242
243 for (i = 0; i < 8; i++)
244 idct_row (block + 8 * i);
245
246 for (i = 0; i < 8; i++)
247 idct_col (block + i);
248
249 i = 8;
250 do {
251 dest[0] = CLIP (block[0]);
252 dest[1] = CLIP (block[1]);
253 dest[2] = CLIP (block[2]);
254 dest[3] = CLIP (block[3]);
255 dest[4] = CLIP (block[4]);
256 dest[5] = CLIP (block[5]);
257 dest[6] = CLIP (block[6]);
258 dest[7] = CLIP (block[7]);
259
260 dest += stride;
261 block += 8;
262 } while (--i);
263 }
264
265 void idct_block_add_c (int16_t * block, uint8_t * dest, int stride)
266 {
267 int i;
268
269 for (i = 0; i < 8; i++)
270 idct_row (block + 8 * i);
271
272 for (i = 0; i < 8; i++)
273 idct_col (block + i);
274
275 i = 8;
276 do {
277 dest[0] = CLIP (block[0] + dest[0]);
278 dest[1] = CLIP (block[1] + dest[1]);
279 dest[2] = CLIP (block[2] + dest[2]);
280 dest[3] = CLIP (block[3] + dest[3]);
281 dest[4] = CLIP (block[4] + dest[4]);
282 dest[5] = CLIP (block[5] + dest[5]);
283 dest[6] = CLIP (block[6] + dest[6]);
284 dest[7] = CLIP (block[7] + dest[7]);
285
286 dest += stride;
287 block += 8;
288 } while (--i);
289 }