comparison libmpeg2/idct.c @ 12932:d0a8810e155c

Importing libmpeg2 from mpeg2dec-0.4.0b
author henry
date Mon, 02 Aug 2004 11:26:43 +0000
parents ec04f41e2480
children b43e5e6430e6
comparison
equal deleted inserted replaced
12931:0aecf9be9817 12932:d0a8810e155c
1 /* 1 /*
2 * idct.c 2 * idct.c
3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org> 3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> 4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5 * 5 *
6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. 6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7 * See http://libmpeg2.sourceforge.net/ for updates. 7 * See http://libmpeg2.sourceforge.net/ for updates.
8 * 8 *
25 25
26 #include <stdlib.h> 26 #include <stdlib.h>
27 #include <inttypes.h> 27 #include <inttypes.h>
28 28
29 #include "mpeg2.h" 29 #include "mpeg2.h"
30 #include "attributes.h"
30 #include "mpeg2_internal.h" 31 #include "mpeg2_internal.h"
31 #include "attributes.h" 32
32 33 #define W1 2841 /* 2048 * sqrt (2) * cos (1 * pi / 16) */
33 #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */ 34 #define W2 2676 /* 2048 * sqrt (2) * cos (2 * pi / 16) */
34 #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */ 35 #define W3 2408 /* 2048 * sqrt (2) * cos (3 * pi / 16) */
35 #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */ 36 #define W5 1609 /* 2048 * sqrt (2) * cos (5 * pi / 16) */
36 #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */ 37 #define W6 1108 /* 2048 * sqrt (2) * cos (6 * pi / 16) */
37 #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */ 38 #define W7 565 /* 2048 * sqrt (2) * cos (7 * pi / 16) */
38 #define W7 565 /* 2048*sqrt (2)*cos (7*pi/16) */
39 39
40 /* idct main entry point */ 40 /* idct main entry point */
41 void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride); 41 void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride);
42 void (* mpeg2_idct_add) (int last, int16_t * block, 42 void (* mpeg2_idct_add) (int last, int16_t * block,
43 uint8_t * dest, int stride); 43 uint8_t * dest, int stride);
44 44
45 static uint8_t clip_lut[1024]; 45 /*
46 #define CLIP(i) ((clip_lut+384)[(i)]) 46 * In legal streams, the IDCT output should be between -384 and +384.
47 * In corrupted streams, it is possible to force the IDCT output to go
48 * to +-3826 - this is the worst case for a column IDCT where the
49 * column inputs are 16-bit values.
50 */
51 uint8_t mpeg2_clip[3840 * 2 + 256];
52 #define CLIP(i) ((mpeg2_clip + 3840)[i])
47 53
48 #if 0 54 #if 0
49 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \ 55 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \
50 do { \ 56 do { \
51 t0 = W0*d0 + W1*d1; \ 57 t0 = W0 * d0 + W1 * d1; \
52 t1 = W0*d1 - W1*d0; \ 58 t1 = W0 * d1 - W1 * d0; \
53 } while (0) 59 } while (0)
54 #else 60 #else
55 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \ 61 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \
56 do { \ 62 do { \
57 int tmp = W0 * (d0 + d1); \ 63 int tmp = W0 * (d0 + d1); \
67 int t0, t1, t2, t3; 73 int t0, t1, t2, t3;
68 74
69 /* shortcut */ 75 /* shortcut */
70 if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] | 76 if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] |
71 ((int32_t *)block)[3]))) { 77 ((int32_t *)block)[3]))) {
72 uint32_t tmp = (uint16_t) (block[0] << 3); 78 uint32_t tmp = (uint16_t) (block[0] >> 1);
73 tmp |= tmp << 16; 79 tmp |= tmp << 16;
74 ((int32_t *)block)[0] = tmp; 80 ((int32_t *)block)[0] = tmp;
75 ((int32_t *)block)[1] = tmp; 81 ((int32_t *)block)[1] = tmp;
76 ((int32_t *)block)[2] = tmp; 82 ((int32_t *)block)[2] = tmp;
77 ((int32_t *)block)[3] = tmp; 83 ((int32_t *)block)[3] = tmp;
78 return; 84 return;
79 } 85 }
80 86
81 d0 = (block[0] << 11) + 128; 87 d0 = (block[0] << 11) + 2048;
82 d1 = block[1]; 88 d1 = block[1];
83 d2 = block[2] << 11; 89 d2 = block[2] << 11;
84 d3 = block[3]; 90 d3 = block[3];
85 t0 = d0 + d2; 91 t0 = d0 + d2;
86 t1 = d0 - d2; 92 t1 = d0 - d2;
98 BUTTERFLY (t2, t3, W3, W5, d1, d2); 104 BUTTERFLY (t2, t3, W3, W5, d1, d2);
99 b0 = t0 + t2; 105 b0 = t0 + t2;
100 b3 = t1 + t3; 106 b3 = t1 + t3;
101 t0 -= t2; 107 t0 -= t2;
102 t1 -= t3; 108 t1 -= t3;
103 b1 = ((t0 + t1) * 181) >> 8; 109 b1 = ((t0 + t1) >> 8) * 181;
104 b2 = ((t0 - t1) * 181) >> 8; 110 b2 = ((t0 - t1) >> 8) * 181;
105 111
106 block[0] = (a0 + b0) >> 8; 112 block[0] = (a0 + b0) >> 12;
107 block[1] = (a1 + b1) >> 8; 113 block[1] = (a1 + b1) >> 12;
108 block[2] = (a2 + b2) >> 8; 114 block[2] = (a2 + b2) >> 12;
109 block[3] = (a3 + b3) >> 8; 115 block[3] = (a3 + b3) >> 12;
110 block[4] = (a3 - b3) >> 8; 116 block[4] = (a3 - b3) >> 12;
111 block[5] = (a2 - b2) >> 8; 117 block[5] = (a2 - b2) >> 12;
112 block[6] = (a1 - b1) >> 8; 118 block[6] = (a1 - b1) >> 12;
113 block[7] = (a0 - b0) >> 8; 119 block[7] = (a0 - b0) >> 12;
114 } 120 }
115 121
116 static inline void idct_col (int16_t * const block) 122 static inline void idct_col (int16_t * const block)
117 { 123 {
118 int d0, d1, d2, d3; 124 int d0, d1, d2, d3;
137 d3 = block[8*7]; 143 d3 = block[8*7];
138 BUTTERFLY (t0, t1, W7, W1, d3, d0); 144 BUTTERFLY (t0, t1, W7, W1, d3, d0);
139 BUTTERFLY (t2, t3, W3, W5, d1, d2); 145 BUTTERFLY (t2, t3, W3, W5, d1, d2);
140 b0 = t0 + t2; 146 b0 = t0 + t2;
141 b3 = t1 + t3; 147 b3 = t1 + t3;
142 t0 = (t0 - t2) >> 8; 148 t0 -= t2;
143 t1 = (t1 - t3) >> 8; 149 t1 -= t3;
144 b1 = (t0 + t1) * 181; 150 b1 = ((t0 + t1) >> 8) * 181;
145 b2 = (t0 - t1) * 181; 151 b2 = ((t0 - t1) >> 8) * 181;
146 152
147 block[8*0] = (a0 + b0) >> 17; 153 block[8*0] = (a0 + b0) >> 17;
148 block[8*1] = (a1 + b1) >> 17; 154 block[8*1] = (a1 + b1) >> 17;
149 block[8*2] = (a2 + b2) >> 17; 155 block[8*2] = (a2 + b2) >> 17;
150 block[8*3] = (a3 + b3) >> 17; 156 block[8*3] = (a3 + b3) >> 17;
171 dest[4] = CLIP (block[4]); 177 dest[4] = CLIP (block[4]);
172 dest[5] = CLIP (block[5]); 178 dest[5] = CLIP (block[5]);
173 dest[6] = CLIP (block[6]); 179 dest[6] = CLIP (block[6]);
174 dest[7] = CLIP (block[7]); 180 dest[7] = CLIP (block[7]);
175 181
176 block[0] = 0; block[1] = 0; block[2] = 0; block[3] = 0; 182 ((int32_t *)block)[0] = 0; ((int32_t *)block)[1] = 0;
177 block[4] = 0; block[5] = 0; block[6] = 0; block[7] = 0; 183 ((int32_t *)block)[2] = 0; ((int32_t *)block)[3] = 0;
178 184
179 dest += stride; 185 dest += stride;
180 block += 8; 186 block += 8;
181 } while (--i); 187 } while (--i);
182 } 188 }
184 static void mpeg2_idct_add_c (const int last, int16_t * block, 190 static void mpeg2_idct_add_c (const int last, int16_t * block,
185 uint8_t * dest, const int stride) 191 uint8_t * dest, const int stride)
186 { 192 {
187 int i; 193 int i;
188 194
189 if (last != 129 || (block[0] & 7) == 4) { 195 if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) {
190 for (i = 0; i < 8; i++) 196 for (i = 0; i < 8; i++)
191 idct_row (block + 8 * i); 197 idct_row (block + 8 * i);
192 for (i = 0; i < 8; i++) 198 for (i = 0; i < 8; i++)
193 idct_col (block + i); 199 idct_col (block + i);
194 do { 200 do {
199 dest[4] = CLIP (block[4] + dest[4]); 205 dest[4] = CLIP (block[4] + dest[4]);
200 dest[5] = CLIP (block[5] + dest[5]); 206 dest[5] = CLIP (block[5] + dest[5]);
201 dest[6] = CLIP (block[6] + dest[6]); 207 dest[6] = CLIP (block[6] + dest[6]);
202 dest[7] = CLIP (block[7] + dest[7]); 208 dest[7] = CLIP (block[7] + dest[7]);
203 209
204 block[0] = 0; block[1] = 0; block[2] = 0; block[3] = 0; 210 ((int32_t *)block)[0] = 0; ((int32_t *)block)[1] = 0;
205 block[4] = 0; block[5] = 0; block[6] = 0; block[7] = 0; 211 ((int32_t *)block)[2] = 0; ((int32_t *)block)[3] = 0;
206 212
207 dest += stride; 213 dest += stride;
208 block += 8; 214 block += 8;
209 } while (--i); 215 } while (--i);
210 } else { 216 } else {
211 int DC; 217 int DC;
212 218
213 DC = (block[0] + 4) >> 3; 219 DC = (block[0] + 64) >> 7;
214 block[0] = block[63] = 0; 220 block[0] = block[63] = 0;
215 i = 8; 221 i = 8;
216 do { 222 do {
217 dest[0] = CLIP (DC + dest[0]); 223 dest[0] = CLIP (DC + dest[0]);
218 dest[1] = CLIP (DC + dest[1]); 224 dest[1] = CLIP (DC + dest[1]);
239 mpeg2_idct_add = mpeg2_idct_add_mmx; 245 mpeg2_idct_add = mpeg2_idct_add_mmx;
240 mpeg2_idct_mmx_init (); 246 mpeg2_idct_mmx_init ();
241 } else 247 } else
242 #endif 248 #endif
243 #ifdef ARCH_PPC 249 #ifdef ARCH_PPC
244 #ifdef HAVE_ALTIVEC
245 if (accel & MPEG2_ACCEL_PPC_ALTIVEC) { 250 if (accel & MPEG2_ACCEL_PPC_ALTIVEC) {
246 mpeg2_idct_copy = mpeg2_idct_copy_altivec; 251 mpeg2_idct_copy = mpeg2_idct_copy_altivec;
247 mpeg2_idct_add = mpeg2_idct_add_altivec; 252 mpeg2_idct_add = mpeg2_idct_add_altivec;
248 mpeg2_idct_altivec_init (); 253 mpeg2_idct_altivec_init ();
249 } else 254 } else
250 #endif
251 #endif 255 #endif
252 #ifdef ARCH_ALPHA 256 #ifdef ARCH_ALPHA
253 #ifdef CAN_COMPILE_ALPHA_MVI 257 #ifdef CAN_COMPILE_ALPHA_MVI
254 if (accel & MPEG2_ACCEL_ALPHA_MVI) { 258 if (accel & MPEG2_ACCEL_ALPHA_MVI) {
255 mpeg2_idct_copy = mpeg2_idct_copy_mvi; 259 mpeg2_idct_copy = mpeg2_idct_copy_mvi;
256 mpeg2_idct_add = mpeg2_idct_add_mvi; 260 mpeg2_idct_add = mpeg2_idct_add_mvi;
257 mpeg2_idct_alpha_init (0); 261 mpeg2_idct_alpha_init ();
258 } else 262 } else
259 #endif 263 #endif
260 if (accel & MPEG2_ACCEL_ALPHA) { 264 if (accel & MPEG2_ACCEL_ALPHA) {
265 int i;
266
261 mpeg2_idct_copy = mpeg2_idct_copy_alpha; 267 mpeg2_idct_copy = mpeg2_idct_copy_alpha;
262 mpeg2_idct_add = mpeg2_idct_add_alpha; 268 mpeg2_idct_add = mpeg2_idct_add_alpha;
263 mpeg2_idct_alpha_init (1); 269 mpeg2_idct_alpha_init ();
264 } else 270 for (i = -3840; i < 3840 + 256; i++)
265 #endif 271 CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);
266 #ifdef LIBMPEG2_MLIB
267 if (accel & MPEG2_ACCEL_MLIB) {
268 mpeg2_idct_copy = mpeg2_idct_copy_mlib_non_ieee;
269 mpeg2_idct_add = (getenv ("MLIB_NON_IEEE") ?
270 mpeg2_idct_add_mlib_non_ieee : mpeg2_idct_add_mlib);
271 } else 272 } else
272 #endif 273 #endif
273 { 274 {
274 extern uint8_t mpeg2_scan_norm[64]; 275 extern uint8_t mpeg2_scan_norm[64];
275 extern uint8_t mpeg2_scan_alt[64]; 276 extern uint8_t mpeg2_scan_alt[64];
276 int i, j; 277 int i, j;
277 278
278 mpeg2_idct_copy = mpeg2_idct_copy_c; 279 mpeg2_idct_copy = mpeg2_idct_copy_c;
279 mpeg2_idct_add = mpeg2_idct_add_c; 280 mpeg2_idct_add = mpeg2_idct_add_c;
280 for (i = -384; i < 640; i++) 281 for (i = -3840; i < 3840 + 256; i++)
281 clip_lut[i+384] = (i < 0) ? 0 : ((i > 255) ? 255 : i); 282 CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);
282 for (i = 0; i < 64; i++) { 283 for (i = 0; i < 64; i++) {
283 j = mpeg2_scan_norm[i]; 284 j = mpeg2_scan_norm[i];
284 mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2); 285 mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
285 j = mpeg2_scan_alt[i]; 286 j = mpeg2_scan_alt[i];
286 mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2); 287 mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);