comparison libmpeg2/idct_alpha.c @ 9857:89b48bc6c441

Importing libmpeg2 from mpeg2dec-0.3.1
author arpi
date Sun, 06 Apr 2003 16:41:49 +0000
parents
children 18ab5081d37d
comparison
equal deleted inserted replaced
9856:08496327b7ec 9857:89b48bc6c441
1 /*
2 * idct_alpha.c
3 * Copyright (C) 2002 Falk Hueffner <falk@debian.org>
4 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 *
7 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
8 * See http://libmpeg2.sourceforge.net/ for updates.
9 *
10 * mpeg2dec is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * mpeg2dec is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include "config.h"
26
27 #ifdef ARCH_ALPHA
28
29 #include <stdlib.h>
30 #include <inttypes.h>
31
32 #include "alpha_asm.h"
33 #include "attributes.h"
34
35 #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
36 #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
37 #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
38 #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
39 #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
40 #define W7 565 /* 2048*sqrt (2)*cos (7*pi/16) */
41
42 static uint8_t clip_lut[1024];
43 #define CLIP(i) ((clip_lut+384)[(i)])
44
45 #if 0
46 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \
47 do { \
48 t0 = W0*d0 + W1*d1; \
49 t1 = W0*d1 - W1*d0; \
50 } while (0)
51 #else
52 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \
53 do { \
54 int_fast32_t tmp = W0 * (d0 + d1); \
55 t0 = tmp + (W1 - W0) * d1; \
56 t1 = tmp - (W1 + W0) * d0; \
57 } while (0)
58 #endif
59
60 static void inline idct_row (int16_t * const block)
61 {
62 uint64_t l, r;
63 int_fast32_t d0, d1, d2, d3;
64 int_fast32_t a0, a1, a2, a3, b0, b1, b2, b3;
65 int_fast32_t t0, t1, t2, t3;
66
67 l = ldq (block);
68 r = ldq (block + 4);
69
70 /* shortcut */
71 if (likely (!((l & ~0xffffUL) | r))) {
72 uint64_t tmp = (uint16_t) (l << 3);
73 tmp |= tmp << 16;
74 tmp |= tmp << 32;
75 ((int32_t *)block)[0] = tmp;
76 ((int32_t *)block)[1] = tmp;
77 ((int32_t *)block)[2] = tmp;
78 ((int32_t *)block)[3] = tmp;
79 return;
80 }
81
82 d0 = (sextw (l) << 11) + 128;
83 d1 = sextw (extwl (l, 2));
84 d2 = sextw (extwl (l, 4)) << 11;
85 d3 = sextw (extwl (l, 6));
86 t0 = d0 + d2;
87 t1 = d0 - d2;
88 BUTTERFLY (t2, t3, W6, W2, d3, d1);
89 a0 = t0 + t2;
90 a1 = t1 + t3;
91 a2 = t1 - t3;
92 a3 = t0 - t2;
93
94 d0 = sextw (r);
95 d1 = sextw (extwl (r, 2));
96 d2 = sextw (extwl (r, 4));
97 d3 = sextw (extwl (r, 6));
98 BUTTERFLY (t0, t1, W7, W1, d3, d0);
99 BUTTERFLY (t2, t3, W3, W5, d1, d2);
100 b0 = t0 + t2;
101 b3 = t1 + t3;
102 t0 -= t2;
103 t1 -= t3;
104 b1 = ((t0 + t1) * 181) >> 8;
105 b2 = ((t0 - t1) * 181) >> 8;
106
107 block[0] = (a0 + b0) >> 8;
108 block[1] = (a1 + b1) >> 8;
109 block[2] = (a2 + b2) >> 8;
110 block[3] = (a3 + b3) >> 8;
111 block[4] = (a3 - b3) >> 8;
112 block[5] = (a2 - b2) >> 8;
113 block[6] = (a1 - b1) >> 8;
114 block[7] = (a0 - b0) >> 8;
115 }
116
117 static void inline idct_col (int16_t * const block)
118 {
119 int_fast32_t d0, d1, d2, d3;
120 int_fast32_t a0, a1, a2, a3, b0, b1, b2, b3;
121 int_fast32_t t0, t1, t2, t3;
122
123 d0 = (block[8*0] << 11) + 65536;
124 d1 = block[8*1];
125 d2 = block[8*2] << 11;
126 d3 = block[8*3];
127 t0 = d0 + d2;
128 t1 = d0 - d2;
129 BUTTERFLY (t2, t3, W6, W2, d3, d1);
130 a0 = t0 + t2;
131 a1 = t1 + t3;
132 a2 = t1 - t3;
133 a3 = t0 - t2;
134
135 d0 = block[8*4];
136 d1 = block[8*5];
137 d2 = block[8*6];
138 d3 = block[8*7];
139 BUTTERFLY (t0, t1, W7, W1, d3, d0);
140 BUTTERFLY (t2, t3, W3, W5, d1, d2);
141 b0 = t0 + t2;
142 b3 = t1 + t3;
143 t0 = (t0 - t2) >> 8;
144 t1 = (t1 - t3) >> 8;
145 b1 = (t0 + t1) * 181;
146 b2 = (t0 - t1) * 181;
147
148 block[8*0] = (a0 + b0) >> 17;
149 block[8*1] = (a1 + b1) >> 17;
150 block[8*2] = (a2 + b2) >> 17;
151 block[8*3] = (a3 + b3) >> 17;
152 block[8*4] = (a3 - b3) >> 17;
153 block[8*5] = (a2 - b2) >> 17;
154 block[8*6] = (a1 - b1) >> 17;
155 block[8*7] = (a0 - b0) >> 17;
156 }
157
158 void mpeg2_idct_copy_mvi (int16_t * block, uint8_t * dest, const int stride)
159 {
160 uint64_t clampmask;
161 int i;
162
163 for (i = 0; i < 8; i++)
164 idct_row (block + 8 * i);
165
166 for (i = 0; i < 8; i++)
167 idct_col (block + i);
168
169 clampmask = zap (-1, 0xaa); /* 0x00ff00ff00ff00ff */
170 do {
171 uint64_t shorts0, shorts1;
172
173 shorts0 = ldq (block);
174 shorts0 = maxsw4 (shorts0, 0);
175 shorts0 = minsw4 (shorts0, clampmask);
176 stl (pkwb (shorts0), dest);
177
178 shorts1 = ldq (block + 4);
179 shorts1 = maxsw4 (shorts1, 0);
180 shorts1 = minsw4 (shorts1, clampmask);
181 stl (pkwb (shorts1), dest + 4);
182
183 stq (0, block);
184 stq (0, block + 4);
185
186 dest += stride;
187 block += 8;
188 } while (--i);
189 }
190
191 void mpeg2_idct_add_mvi (const int last, int16_t * block,
192 uint8_t * dest, const int stride)
193 {
194 uint64_t clampmask;
195 uint64_t signmask;
196 int i;
197
198 if (last != 129 || (block[0] & 7) == 4) {
199 for (i = 0; i < 8; i++)
200 idct_row (block + 8 * i);
201 for (i = 0; i < 8; i++)
202 idct_col (block + i);
203 clampmask = zap (-1, 0xaa); /* 0x00ff00ff00ff00ff */
204 signmask = zap (-1, 0x33);
205 signmask ^= signmask >> 1; /* 0x8000800080008000 */
206
207 do {
208 uint64_t shorts0, pix0, signs0;
209 uint64_t shorts1, pix1, signs1;
210
211 shorts0 = ldq (block);
212 shorts1 = ldq (block + 4);
213
214 pix0 = unpkbw (ldl (dest));
215 /* signed subword add (MMX paddw). */
216 signs0 = shorts0 & signmask;
217 shorts0 &= ~signmask;
218 shorts0 += pix0;
219 shorts0 ^= signs0;
220 /* clamp. */
221 shorts0 = maxsw4 (shorts0, 0);
222 shorts0 = minsw4 (shorts0, clampmask);
223
224 /* next 4. */
225 pix1 = unpkbw (ldl (dest + 4));
226 signs1 = shorts1 & signmask;
227 shorts1 &= ~signmask;
228 shorts1 += pix1;
229 shorts1 ^= signs1;
230 shorts1 = maxsw4 (shorts1, 0);
231 shorts1 = minsw4 (shorts1, clampmask);
232
233 stl (pkwb (shorts0), dest);
234 stl (pkwb (shorts1), dest + 4);
235 stq (0, block);
236 stq (0, block + 4);
237
238 dest += stride;
239 block += 8;
240 } while (--i);
241 } else {
242 int DC;
243 uint64_t p0, p1, p2, p3, p4, p5, p6, p7;
244 uint64_t DCs;
245
246 DC = (block[0] + 4) >> 3;
247 block[0] = block[63] = 0;
248
249 p0 = ldq (dest + 0 * stride);
250 p1 = ldq (dest + 1 * stride);
251 p2 = ldq (dest + 2 * stride);
252 p3 = ldq (dest + 3 * stride);
253 p4 = ldq (dest + 4 * stride);
254 p5 = ldq (dest + 5 * stride);
255 p6 = ldq (dest + 6 * stride);
256 p7 = ldq (dest + 7 * stride);
257
258 if (DC > 0) {
259 DCs = BYTE_VEC (likely (DC <= 255) ? DC : 255);
260 p0 += minub8 (DCs, ~p0);
261 p1 += minub8 (DCs, ~p1);
262 p2 += minub8 (DCs, ~p2);
263 p3 += minub8 (DCs, ~p3);
264 p4 += minub8 (DCs, ~p4);
265 p5 += minub8 (DCs, ~p5);
266 p6 += minub8 (DCs, ~p6);
267 p7 += minub8 (DCs, ~p7);
268 } else {
269 DCs = BYTE_VEC (likely (-DC <= 255) ? -DC : 255);
270 p0 -= minub8 (DCs, p0);
271 p1 -= minub8 (DCs, p1);
272 p2 -= minub8 (DCs, p2);
273 p3 -= minub8 (DCs, p3);
274 p4 -= minub8 (DCs, p4);
275 p5 -= minub8 (DCs, p5);
276 p6 -= minub8 (DCs, p6);
277 p7 -= minub8 (DCs, p7);
278 }
279
280 stq (p0, dest + 0 * stride);
281 stq (p1, dest + 1 * stride);
282 stq (p2, dest + 2 * stride);
283 stq (p3, dest + 3 * stride);
284 stq (p4, dest + 4 * stride);
285 stq (p5, dest + 5 * stride);
286 stq (p6, dest + 6 * stride);
287 stq (p7, dest + 7 * stride);
288 }
289 }
290
291 void mpeg2_idct_copy_alpha (int16_t * block, uint8_t * dest, const int stride)
292 {
293 int i;
294
295 for (i = 0; i < 8; i++)
296 idct_row (block + 8 * i);
297 for (i = 0; i < 8; i++)
298 idct_col (block + i);
299 do {
300 dest[0] = CLIP (block[0]);
301 dest[1] = CLIP (block[1]);
302 dest[2] = CLIP (block[2]);
303 dest[3] = CLIP (block[3]);
304 dest[4] = CLIP (block[4]);
305 dest[5] = CLIP (block[5]);
306 dest[6] = CLIP (block[6]);
307 dest[7] = CLIP (block[7]);
308
309 stq(0, block);
310 stq(0, block + 4);
311
312 dest += stride;
313 block += 8;
314 } while (--i);
315 }
316
317 void mpeg2_idct_add_alpha (const int last, int16_t * block,
318 uint8_t * dest, const int stride)
319 {
320 int i;
321
322 if (last != 129 || (block[0] & 7) == 4) {
323 for (i = 0; i < 8; i++)
324 idct_row (block + 8 * i);
325 for (i = 0; i < 8; i++)
326 idct_col (block + i);
327 do {
328 dest[0] = CLIP (block[0] + dest[0]);
329 dest[1] = CLIP (block[1] + dest[1]);
330 dest[2] = CLIP (block[2] + dest[2]);
331 dest[3] = CLIP (block[3] + dest[3]);
332 dest[4] = CLIP (block[4] + dest[4]);
333 dest[5] = CLIP (block[5] + dest[5]);
334 dest[6] = CLIP (block[6] + dest[6]);
335 dest[7] = CLIP (block[7] + dest[7]);
336
337 stq(0, block);
338 stq(0, block + 4);
339
340 dest += stride;
341 block += 8;
342 } while (--i);
343 } else {
344 int DC;
345
346 DC = (block[0] + 4) >> 3;
347 block[0] = block[63] = 0;
348 i = 8;
349 do {
350 dest[0] = CLIP (DC + dest[0]);
351 dest[1] = CLIP (DC + dest[1]);
352 dest[2] = CLIP (DC + dest[2]);
353 dest[3] = CLIP (DC + dest[3]);
354 dest[4] = CLIP (DC + dest[4]);
355 dest[5] = CLIP (DC + dest[5]);
356 dest[6] = CLIP (DC + dest[6]);
357 dest[7] = CLIP (DC + dest[7]);
358 dest += stride;
359 } while (--i);
360 }
361 }
362
363 void mpeg2_idct_alpha_init(int no_mvi)
364 {
365 extern uint8_t mpeg2_scan_norm[64];
366 extern uint8_t mpeg2_scan_alt[64];
367 int i, j;
368
369 if (no_mvi)
370 for (i = -384; i < 640; i++)
371 clip_lut[i + 384] = (i < 0) ? 0 : ((i > 255) ? 255 : i);
372 for (i = 0; i < 64; i++) {
373 j = mpeg2_scan_norm[i];
374 mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
375 j = mpeg2_scan_alt[i];
376 mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
377 }
378 }
379
380 #endif /* ARCH_ALPHA */