diff options
Diffstat (limited to 'apps/plugins/mpegplayer/idct.c')
-rw-r--r-- | apps/plugins/mpegplayer/idct.c | 287 |
1 files changed, 287 insertions, 0 deletions
diff --git a/apps/plugins/mpegplayer/idct.c b/apps/plugins/mpegplayer/idct.c new file mode 100644 index 0000000000..8a0b56ea22 --- /dev/null +++ b/apps/plugins/mpegplayer/idct.c | |||
@@ -0,0 +1,287 @@ | |||
1 | /* | ||
2 | * idct.c | ||
3 | * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org> | ||
4 | * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> | ||
5 | * | ||
6 | * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. | ||
7 | * See http://libmpeg2.sourceforge.net/ for updates. | ||
8 | * | ||
9 | * mpeg2dec 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 | * mpeg2dec 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 | |||
24 | #include "plugin.h" | ||
25 | |||
26 | #include "mpeg2dec_config.h" | ||
27 | |||
28 | #include "mpeg2.h" | ||
29 | #include "attributes.h" | ||
30 | #include "mpeg2_internal.h" | ||
31 | |||
32 | #define W1 2841 /* 2048 * sqrt (2) * cos (1 * pi / 16) */ | ||
33 | #define W2 2676 /* 2048 * sqrt (2) * cos (2 * pi / 16) */ | ||
34 | #define W3 2408 /* 2048 * sqrt (2) * cos (3 * pi / 16) */ | ||
35 | #define W5 1609 /* 2048 * sqrt (2) * cos (5 * pi / 16) */ | ||
36 | #define W6 1108 /* 2048 * sqrt (2) * cos (6 * pi / 16) */ | ||
37 | #define W7 565 /* 2048 * sqrt (2) * cos (7 * pi / 16) */ | ||
38 | |||
39 | /* idct main entry point */ | ||
40 | void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride); | ||
41 | void (* mpeg2_idct_add) (int last, int16_t * block, | ||
42 | uint8_t * dest, int stride); | ||
43 | |||
44 | /* | ||
45 | * In legal streams, the IDCT output should be between -384 and +384. | ||
46 | * In corrupted streams, it is possible to force the IDCT output to go | ||
47 | * to +-3826 - this is the worst case for a column IDCT where the | ||
48 | * column inputs are 16-bit values. | ||
49 | */ | ||
50 | uint8_t mpeg2_clip[3840 * 2 + 256] IBSS_ATTR; | ||
51 | #define CLIP(i) ((mpeg2_clip + 3840)[i]) | ||
52 | |||
53 | #if 0 | ||
54 | #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \ | ||
55 | do { \ | ||
56 | t0 = W0 * d0 + W1 * d1; \ | ||
57 | t1 = W0 * d1 - W1 * d0; \ | ||
58 | } while (0) | ||
59 | #else | ||
60 | #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \ | ||
61 | do { \ | ||
62 | int tmp = W0 * (d0 + d1); \ | ||
63 | t0 = tmp + (W1 - W0) * d1; \ | ||
64 | t1 = tmp - (W1 + W0) * d0; \ | ||
65 | } while (0) | ||
66 | #endif | ||
67 | |||
68 | static inline void idct_row (int16_t * const block) | ||
69 | { | ||
70 | int d0, d1, d2, d3; | ||
71 | int a0, a1, a2, a3, b0, b1, b2, b3; | ||
72 | int t0, t1, t2, t3; | ||
73 | |||
74 | /* shortcut */ | ||
75 | if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] | | ||
76 | ((int32_t *)block)[3]))) { | ||
77 | uint32_t tmp = (uint16_t) (block[0] >> 1); | ||
78 | tmp |= tmp << 16; | ||
79 | ((int32_t *)block)[0] = tmp; | ||
80 | ((int32_t *)block)[1] = tmp; | ||
81 | ((int32_t *)block)[2] = tmp; | ||
82 | ((int32_t *)block)[3] = tmp; | ||
83 | return; | ||
84 | } | ||
85 | |||
86 | d0 = (block[0] << 11) + 2048; | ||
87 | d1 = block[1]; | ||
88 | d2 = block[2] << 11; | ||
89 | d3 = block[3]; | ||
90 | t0 = d0 + d2; | ||
91 | t1 = d0 - d2; | ||
92 | BUTTERFLY (t2, t3, W6, W2, d3, d1); | ||
93 | a0 = t0 + t2; | ||
94 | a1 = t1 + t3; | ||
95 | a2 = t1 - t3; | ||
96 | a3 = t0 - t2; | ||
97 | |||
98 | d0 = block[4]; | ||
99 | d1 = block[5]; | ||
100 | d2 = block[6]; | ||
101 | d3 = block[7]; | ||
102 | BUTTERFLY (t0, t1, W7, W1, d3, d0); | ||
103 | BUTTERFLY (t2, t3, W3, W5, d1, d2); | ||
104 | b0 = t0 + t2; | ||
105 | b3 = t1 + t3; | ||
106 | t0 -= t2; | ||
107 | t1 -= t3; | ||
108 | b1 = ((t0 + t1) >> 8) * 181; | ||
109 | b2 = ((t0 - t1) >> 8) * 181; | ||
110 | |||
111 | block[0] = (a0 + b0) >> 12; | ||
112 | block[1] = (a1 + b1) >> 12; | ||
113 | block[2] = (a2 + b2) >> 12; | ||
114 | block[3] = (a3 + b3) >> 12; | ||
115 | block[4] = (a3 - b3) >> 12; | ||
116 | block[5] = (a2 - b2) >> 12; | ||
117 | block[6] = (a1 - b1) >> 12; | ||
118 | block[7] = (a0 - b0) >> 12; | ||
119 | } | ||
120 | |||
121 | static inline void idct_col (int16_t * const block) | ||
122 | { | ||
123 | int d0, d1, d2, d3; | ||
124 | int a0, a1, a2, a3, b0, b1, b2, b3; | ||
125 | int t0, t1, t2, t3; | ||
126 | |||
127 | d0 = (block[8*0] << 11) + 65536; | ||
128 | d1 = block[8*1]; | ||
129 | d2 = block[8*2] << 11; | ||
130 | d3 = block[8*3]; | ||
131 | t0 = d0 + d2; | ||
132 | t1 = d0 - d2; | ||
133 | BUTTERFLY (t2, t3, W6, W2, d3, d1); | ||
134 | a0 = t0 + t2; | ||
135 | a1 = t1 + t3; | ||
136 | a2 = t1 - t3; | ||
137 | a3 = t0 - t2; | ||
138 | |||
139 | d0 = block[8*4]; | ||
140 | d1 = block[8*5]; | ||
141 | d2 = block[8*6]; | ||
142 | d3 = block[8*7]; | ||
143 | BUTTERFLY (t0, t1, W7, W1, d3, d0); | ||
144 | BUTTERFLY (t2, t3, W3, W5, d1, d2); | ||
145 | b0 = t0 + t2; | ||
146 | b3 = t1 + t3; | ||
147 | t0 -= t2; | ||
148 | t1 -= t3; | ||
149 | b1 = ((t0 + t1) >> 8) * 181; | ||
150 | b2 = ((t0 - t1) >> 8) * 181; | ||
151 | |||
152 | block[8*0] = (a0 + b0) >> 17; | ||
153 | block[8*1] = (a1 + b1) >> 17; | ||
154 | block[8*2] = (a2 + b2) >> 17; | ||
155 | block[8*3] = (a3 + b3) >> 17; | ||
156 | block[8*4] = (a3 - b3) >> 17; | ||
157 | block[8*5] = (a2 - b2) >> 17; | ||
158 | block[8*6] = (a1 - b1) >> 17; | ||
159 | block[8*7] = (a0 - b0) >> 17; | ||
160 | } | ||
161 | |||
162 | static void mpeg2_idct_copy_c (int16_t * block, uint8_t * dest, | ||
163 | const int stride) | ||
164 | { | ||
165 | int i; | ||
166 | |||
167 | for (i = 0; i < 8; i++) | ||
168 | idct_row (block + 8 * i); | ||
169 | for (i = 0; i < 8; i++) | ||
170 | idct_col (block + i); | ||
171 | do { | ||
172 | dest[0] = CLIP (block[0]); | ||
173 | dest[1] = CLIP (block[1]); | ||
174 | dest[2] = CLIP (block[2]); | ||
175 | dest[3] = CLIP (block[3]); | ||
176 | dest[4] = CLIP (block[4]); | ||
177 | dest[5] = CLIP (block[5]); | ||
178 | dest[6] = CLIP (block[6]); | ||
179 | dest[7] = CLIP (block[7]); | ||
180 | |||
181 | ((int32_t *)block)[0] = 0; ((int32_t *)block)[1] = 0; | ||
182 | ((int32_t *)block)[2] = 0; ((int32_t *)block)[3] = 0; | ||
183 | |||
184 | dest += stride; | ||
185 | block += 8; | ||
186 | } while (--i); | ||
187 | } | ||
188 | |||
189 | static void mpeg2_idct_add_c (const int last, int16_t * block, | ||
190 | uint8_t * dest, const int stride) | ||
191 | { | ||
192 | int i; | ||
193 | |||
194 | if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) { | ||
195 | for (i = 0; i < 8; i++) | ||
196 | idct_row (block + 8 * i); | ||
197 | for (i = 0; i < 8; i++) | ||
198 | idct_col (block + i); | ||
199 | do { | ||
200 | dest[0] = CLIP (block[0] + dest[0]); | ||
201 | dest[1] = CLIP (block[1] + dest[1]); | ||
202 | dest[2] = CLIP (block[2] + dest[2]); | ||
203 | dest[3] = CLIP (block[3] + dest[3]); | ||
204 | dest[4] = CLIP (block[4] + dest[4]); | ||
205 | dest[5] = CLIP (block[5] + dest[5]); | ||
206 | dest[6] = CLIP (block[6] + dest[6]); | ||
207 | dest[7] = CLIP (block[7] + dest[7]); | ||
208 | |||
209 | ((int32_t *)block)[0] = 0; ((int32_t *)block)[1] = 0; | ||
210 | ((int32_t *)block)[2] = 0; ((int32_t *)block)[3] = 0; | ||
211 | |||
212 | dest += stride; | ||
213 | block += 8; | ||
214 | } while (--i); | ||
215 | } else { | ||
216 | int DC; | ||
217 | |||
218 | DC = (block[0] + 64) >> 7; | ||
219 | block[0] = block[63] = 0; | ||
220 | i = 8; | ||
221 | do { | ||
222 | dest[0] = CLIP (DC + dest[0]); | ||
223 | dest[1] = CLIP (DC + dest[1]); | ||
224 | dest[2] = CLIP (DC + dest[2]); | ||
225 | dest[3] = CLIP (DC + dest[3]); | ||
226 | dest[4] = CLIP (DC + dest[4]); | ||
227 | dest[5] = CLIP (DC + dest[5]); | ||
228 | dest[6] = CLIP (DC + dest[6]); | ||
229 | dest[7] = CLIP (DC + dest[7]); | ||
230 | dest += stride; | ||
231 | } while (--i); | ||
232 | } | ||
233 | } | ||
234 | |||
235 | void mpeg2_idct_init (uint32_t accel) | ||
236 | { | ||
237 | (void)accel; | ||
238 | #ifdef ARCH_X86 | ||
239 | if (accel & MPEG2_ACCEL_X86_MMXEXT) { | ||
240 | mpeg2_idct_copy = mpeg2_idct_copy_mmxext; | ||
241 | mpeg2_idct_add = mpeg2_idct_add_mmxext; | ||
242 | mpeg2_idct_mmx_init (); | ||
243 | } else if (accel & MPEG2_ACCEL_X86_MMX) { | ||
244 | mpeg2_idct_copy = mpeg2_idct_copy_mmx; | ||
245 | mpeg2_idct_add = mpeg2_idct_add_mmx; | ||
246 | mpeg2_idct_mmx_init (); | ||
247 | } else | ||
248 | #endif | ||
249 | #ifdef ARCH_PPC | ||
250 | if (accel & MPEG2_ACCEL_PPC_ALTIVEC) { | ||
251 | mpeg2_idct_copy = mpeg2_idct_copy_altivec; | ||
252 | mpeg2_idct_add = mpeg2_idct_add_altivec; | ||
253 | mpeg2_idct_altivec_init (); | ||
254 | } else | ||
255 | #endif | ||
256 | #ifdef ARCH_ALPHA | ||
257 | if (accel & MPEG2_ACCEL_ALPHA_MVI) { | ||
258 | mpeg2_idct_copy = mpeg2_idct_copy_mvi; | ||
259 | mpeg2_idct_add = mpeg2_idct_add_mvi; | ||
260 | mpeg2_idct_alpha_init (); | ||
261 | } else if (accel & MPEG2_ACCEL_ALPHA) { | ||
262 | int i; | ||
263 | |||
264 | mpeg2_idct_copy = mpeg2_idct_copy_alpha; | ||
265 | mpeg2_idct_add = mpeg2_idct_add_alpha; | ||
266 | mpeg2_idct_alpha_init (); | ||
267 | for (i = -3840; i < 3840 + 256; i++) | ||
268 | CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i); | ||
269 | } else | ||
270 | #endif | ||
271 | { | ||
272 | extern uint8_t mpeg2_scan_norm[64]; | ||
273 | extern uint8_t mpeg2_scan_alt[64]; | ||
274 | int i, j; | ||
275 | |||
276 | mpeg2_idct_copy = mpeg2_idct_copy_c; | ||
277 | mpeg2_idct_add = mpeg2_idct_add_c; | ||
278 | for (i = -3840; i < 3840 + 256; i++) | ||
279 | CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i); | ||
280 | for (i = 0; i < 64; i++) { | ||
281 | j = mpeg2_scan_norm[i]; | ||
282 | mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2); | ||
283 | j = mpeg2_scan_alt[i]; | ||
284 | mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2); | ||
285 | } | ||
286 | } | ||
287 | } | ||