4 * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 based upon some outcommented c code from mpeg2dec (idct_mmx.c
22 written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>)
26 #include "simple_idct.h"
29 #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
30 #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
31 #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
32 #define W4 2048 /* 2048*sqrt (2)*cos (4*pi/16) */
33 #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
34 #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
35 #define W7 565 /* 2048*sqrt (2)*cos (7*pi/16) */
39 #define W1 22725 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
40 #define W2 21407 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
41 #define W3 19266 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
42 #define W4 16383 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
43 #define W5 12873 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
44 #define W6 8867 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
45 #define W7 4520 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
47 #define COL_SHIFT 20 // 6
50 #if defined(ARCH_POWERPC_405)
52 /* signed 16x16 -> 32 multiply add accumulate */
53 #define MAC16(rt, ra, rb) \
54 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
56 /* signed 16x16 -> 32 multiply */
57 #define MUL16(rt, ra, rb) \
58 asm ("mullhw %0, %1, %2" : "=r" (rt) : "r" (ra), "r" (rb));
62 /* signed 16x16 -> 32 multiply add accumulate */
63 #define MAC16(rt, ra, rb) rt += (ra) * (rb)
65 /* signed 16x16 -> 32 multiply */
66 #define MUL16(rt, ra, rb) rt = (ra) * (rb)
70 static inline void idctRowCondDC (int16_t * row)
72 int a0, a1, a2, a3, b0, b1, b2, b3;
80 #ifdef WORDS_BIGENDIAN
81 #define ROW0_MASK 0xffff000000000000LL
83 #define ROW0_MASK 0xffffLL
85 if ( ((((uint64_t *)row)[0] & ~ROW0_MASK) |
86 ((uint64_t *)row)[1]) == 0) {
87 temp = (row[0] << 3) & 0xffff;
90 ((uint64_t *)row)[0] = temp;
91 ((uint64_t *)row)[1] = temp;
95 if (!(((uint32_t*)row)[1] |
99 temp = (row[0] << 3) & 0xffff;
101 ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
102 ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
107 a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
112 /* no need to optimize : gcc does it */
118 MUL16(b0, W1, row[1]);
119 MAC16(b0, W3, row[3]);
120 MUL16(b1, W3, row[1]);
121 MAC16(b1, -W7, row[3]);
122 MUL16(b2, W5, row[1]);
123 MAC16(b2, -W1, row[3]);
124 MUL16(b3, W7, row[1]);
125 MAC16(b3, -W5, row[3]);
128 temp = ((uint64_t*)row)[1];
130 temp = ((uint32_t*)row)[2] | ((uint32_t*)row)[3];
133 a0 += W4*row[4] + W6*row[6];
134 a1 += - W4*row[4] - W2*row[6];
135 a2 += - W4*row[4] + W2*row[6];
136 a3 += W4*row[4] - W6*row[6];
138 MAC16(b0, W5, row[5]);
139 MAC16(b0, W7, row[7]);
141 MAC16(b1, -W1, row[5]);
142 MAC16(b1, -W5, row[7]);
144 MAC16(b2, W7, row[5]);
145 MAC16(b2, W3, row[7]);
147 MAC16(b3, W3, row[5]);
148 MAC16(b3, -W1, row[7]);
151 row[0] = (a0 + b0) >> ROW_SHIFT;
152 row[7] = (a0 - b0) >> ROW_SHIFT;
153 row[1] = (a1 + b1) >> ROW_SHIFT;
154 row[6] = (a1 - b1) >> ROW_SHIFT;
155 row[2] = (a2 + b2) >> ROW_SHIFT;
156 row[5] = (a2 - b2) >> ROW_SHIFT;
157 row[3] = (a3 + b3) >> ROW_SHIFT;
158 row[4] = (a3 - b3) >> ROW_SHIFT;
161 static inline void idctSparseColPut (UINT8 *dest, int line_size,
164 int a0, a1, a2, a3, b0, b1, b2, b3;
165 UINT8 *cm = cropTbl + MAX_NEG_CROP;
167 /* XXX: I did that only to give same values as previous code */
168 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
178 MUL16(b0, W1, col[8*1]);
179 MUL16(b1, W3, col[8*1]);
180 MUL16(b2, W5, col[8*1]);
181 MUL16(b3, W7, col[8*1]);
183 MAC16(b0, + W3, col[8*3]);
184 MAC16(b1, - W7, col[8*3]);
185 MAC16(b2, - W1, col[8*3]);
186 MAC16(b3, - W5, col[8*3]);
196 MAC16(b0, + W5, col[8*5]);
197 MAC16(b1, - W1, col[8*5]);
198 MAC16(b2, + W7, col[8*5]);
199 MAC16(b3, + W3, col[8*5]);
210 MAC16(b0, + W7, col[8*7]);
211 MAC16(b1, - W5, col[8*7]);
212 MAC16(b2, + W3, col[8*7]);
213 MAC16(b3, - W1, col[8*7]);
216 dest[0] = cm[(a0 + b0) >> COL_SHIFT];
218 dest[0] = cm[(a1 + b1) >> COL_SHIFT];
220 dest[0] = cm[(a2 + b2) >> COL_SHIFT];
222 dest[0] = cm[(a3 + b3) >> COL_SHIFT];
224 dest[0] = cm[(a3 - b3) >> COL_SHIFT];
226 dest[0] = cm[(a2 - b2) >> COL_SHIFT];
228 dest[0] = cm[(a1 - b1) >> COL_SHIFT];
230 dest[0] = cm[(a0 - b0) >> COL_SHIFT];
233 static inline void idctSparseColAdd (UINT8 *dest, int line_size,
236 int a0, a1, a2, a3, b0, b1, b2, b3;
237 UINT8 *cm = cropTbl + MAX_NEG_CROP;
239 /* XXX: I did that only to give same values as previous code */
240 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
250 MUL16(b0, W1, col[8*1]);
251 MUL16(b1, W3, col[8*1]);
252 MUL16(b2, W5, col[8*1]);
253 MUL16(b3, W7, col[8*1]);
255 MAC16(b0, + W3, col[8*3]);
256 MAC16(b1, - W7, col[8*3]);
257 MAC16(b2, - W1, col[8*3]);
258 MAC16(b3, - W5, col[8*3]);
268 MAC16(b0, + W5, col[8*5]);
269 MAC16(b1, - W1, col[8*5]);
270 MAC16(b2, + W7, col[8*5]);
271 MAC16(b3, + W3, col[8*5]);
282 MAC16(b0, + W7, col[8*7]);
283 MAC16(b1, - W5, col[8*7]);
284 MAC16(b2, + W3, col[8*7]);
285 MAC16(b3, - W1, col[8*7]);
288 dest[0] = cm[dest[0] + ((a0 + b0) >> COL_SHIFT)];
290 dest[0] = cm[dest[0] + ((a1 + b1) >> COL_SHIFT)];
292 dest[0] = cm[dest[0] + ((a2 + b2) >> COL_SHIFT)];
294 dest[0] = cm[dest[0] + ((a3 + b3) >> COL_SHIFT)];
296 dest[0] = cm[dest[0] + ((a3 - b3) >> COL_SHIFT)];
298 dest[0] = cm[dest[0] + ((a2 - b2) >> COL_SHIFT)];
300 dest[0] = cm[dest[0] + ((a1 - b1) >> COL_SHIFT)];
302 dest[0] = cm[dest[0] + ((a0 - b0) >> COL_SHIFT)];
305 static inline void idctSparseCol (int16_t * col)
307 int a0, a1, a2, a3, b0, b1, b2, b3;
309 /* XXX: I did that only to give same values as previous code */
310 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
320 MUL16(b0, W1, col[8*1]);
321 MUL16(b1, W3, col[8*1]);
322 MUL16(b2, W5, col[8*1]);
323 MUL16(b3, W7, col[8*1]);
325 MAC16(b0, + W3, col[8*3]);
326 MAC16(b1, - W7, col[8*3]);
327 MAC16(b2, - W1, col[8*3]);
328 MAC16(b3, - W5, col[8*3]);
338 MAC16(b0, + W5, col[8*5]);
339 MAC16(b1, - W1, col[8*5]);
340 MAC16(b2, + W7, col[8*5]);
341 MAC16(b3, + W3, col[8*5]);
352 MAC16(b0, + W7, col[8*7]);
353 MAC16(b1, - W5, col[8*7]);
354 MAC16(b2, + W3, col[8*7]);
355 MAC16(b3, - W1, col[8*7]);
358 col[0 ] = ((a0 + b0) >> COL_SHIFT);
359 col[8 ] = ((a1 + b1) >> COL_SHIFT);
360 col[16] = ((a2 + b2) >> COL_SHIFT);
361 col[24] = ((a3 + b3) >> COL_SHIFT);
362 col[32] = ((a3 - b3) >> COL_SHIFT);
363 col[40] = ((a2 - b2) >> COL_SHIFT);
364 col[48] = ((a1 - b1) >> COL_SHIFT);
365 col[56] = ((a0 - b0) >> COL_SHIFT);
368 void simple_idct_put(UINT8 *dest, int line_size, INT16 *block)
372 idctRowCondDC(block + i*8);
375 idctSparseColPut(dest + i, line_size, block + i);
378 void simple_idct_add(UINT8 *dest, int line_size, INT16 *block)
382 idctRowCondDC(block + i*8);
385 idctSparseColAdd(dest + i, line_size, block + i);
388 void simple_idct(INT16 *block)
392 idctRowCondDC(block + i*8);
395 idctSparseCol(block + i);
401 #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
402 #define C1 C_FIX(0.6532814824)
403 #define C2 C_FIX(0.2705980501)
405 /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
406 and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
407 #define C_SHIFT (4+1+12)
409 static inline void idct4col(UINT8 *dest, int line_size, const INT16 *col)
411 int c0, c1, c2, c3, a0, a1, a2, a3;
412 const UINT8 *cm = cropTbl + MAX_NEG_CROP;
418 c0 = ((a0 + a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
419 c2 = ((a0 - a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
420 c1 = a1 * C1 + a3 * C2;
421 c3 = a1 * C2 - a3 * C1;
422 dest[0] = cm[(c0 + c1) >> C_SHIFT];
424 dest[0] = cm[(c2 + c3) >> C_SHIFT];
426 dest[0] = cm[(c2 - c3) >> C_SHIFT];
428 dest[0] = cm[(c0 - c1) >> C_SHIFT];
437 ptr[8 + k] = a0 - a1;\
440 /* only used by DV codec. The input must be interlaced. 128 is added
441 to the pixels before clamping to avoid systematic error
442 (1024*sqrt(2)) offset would be needed otherwise. */
443 /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
444 compensate the extra butterfly stage - I don't have the full DV
446 void simple_idct248_put(UINT8 *dest, int line_size, INT16 *block)
465 /* IDCT8 on each line */
467 idctRowCondDC(block + i*8);
470 /* IDCT4 and store */
472 idct4col(dest + i, 2 * line_size, block + i);
473 idct4col(dest + line_size + i, 2 * line_size, block + 8 + i);
477 /* 8x4 & 4x8 WMV2 IDCT */
484 #define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
485 #define C1 C_FIX(0.6532814824)
486 #define C2 C_FIX(0.2705980501)
487 #define C3 C_FIX(0.5)
488 #define C_SHIFT (4+1+12)
489 static inline void idct4col_add(UINT8 *dest, int line_size, const INT16 *col)
491 int c0, c1, c2, c3, a0, a1, a2, a3;
492 const UINT8 *cm = cropTbl + MAX_NEG_CROP;
498 c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
499 c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
500 c1 = a1 * C1 + a3 * C2;
501 c3 = a1 * C2 - a3 * C1;
502 dest[0] = cm[dest[0] + ((c0 + c1) >> C_SHIFT)];
504 dest[0] = cm[dest[0] + ((c2 + c3) >> C_SHIFT)];
506 dest[0] = cm[dest[0] + ((c2 - c3) >> C_SHIFT)];
508 dest[0] = cm[dest[0] + ((c0 - c1) >> C_SHIFT)];
512 #define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
513 #define R1 R_FIX(0.6532814824)
514 #define R2 R_FIX(0.2705980501)
515 #define R3 R_FIX(0.5)
517 static inline void idct4row(INT16 *row)
519 int c0, c1, c2, c3, a0, a1, a2, a3;
520 const UINT8 *cm = cropTbl + MAX_NEG_CROP;
526 c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
527 c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
528 c1 = a1 * R1 + a3 * R2;
529 c3 = a1 * R2 - a3 * R1;
530 row[0]= (c0 + c1) >> R_SHIFT;
531 row[1]= (c2 + c3) >> R_SHIFT;
532 row[2]= (c2 - c3) >> R_SHIFT;
533 row[3]= (c0 - c1) >> R_SHIFT;
536 void simple_idct84_add(UINT8 *dest, int line_size, INT16 *block)
540 /* IDCT8 on each line */
542 idctRowCondDC(block + i*8);
545 /* IDCT4 and store */
547 idct4col_add(dest + i, line_size, block + i);
551 void simple_idct48_add(UINT8 *dest, int line_size, INT16 *block)
555 /* IDCT4 on each line */
557 idct4row(block + i*8);
560 /* IDCT8 and store */
562 idctSparseColAdd(dest + i, line_size, block + i);