4 * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 based upon some outcommented c code from mpeg2dec (idct_mmx.c
30 written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>)
34 #include "simple_idct.h"
37 #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
38 #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
39 #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
40 #define W4 2048 /* 2048*sqrt (2)*cos (4*pi/16) */
41 #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
42 #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
43 #define W7 565 /* 2048*sqrt (2)*cos (7*pi/16) */
47 #define W1 22725 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
48 #define W2 21407 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
49 #define W3 19266 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
50 #define W4 16383 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
51 #define W5 12873 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
52 #define W6 8867 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
53 #define W7 4520 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
55 #define COL_SHIFT 20 // 6
58 #if defined(ARCH_POWERPC_405)
60 /* signed 16x16 -> 32 multiply add accumulate */
61 #define MAC16(rt, ra, rb) \
62 __asm__ ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
64 /* signed 16x16 -> 32 multiply */
65 #define MUL16(rt, ra, rb) \
66 __asm__ ("mullhw %0, %1, %2" : "=r" (rt) : "r" (ra), "r" (rb));
70 /* signed 16x16 -> 32 multiply add accumulate */
71 #define MAC16(rt, ra, rb) rt += (ra) * (rb)
73 /* signed 16x16 -> 32 multiply */
74 #define MUL16(rt, ra, rb) rt = (ra) * (rb)
78 static inline void idctRowCondDC (DCTELEM * row)
80 int a0, a1, a2, a3, b0, b1, b2, b3;
81 #ifdef HAVE_FAST_64BIT
87 #ifdef HAVE_FAST_64BIT
88 #ifdef WORDS_BIGENDIAN
89 #define ROW0_MASK 0xffff000000000000LL
91 #define ROW0_MASK 0xffffLL
93 if(sizeof(DCTELEM)==2){
94 if ( ((((uint64_t *)row)[0] & ~ROW0_MASK) |
95 ((uint64_t *)row)[1]) == 0) {
96 temp = (row[0] << 3) & 0xffff;
99 ((uint64_t *)row)[0] = temp;
100 ((uint64_t *)row)[1] = temp;
104 if (!(row[1]|row[2]|row[3]|row[4]|row[5]|row[6]|row[7])) {
105 row[0]=row[1]=row[2]=row[3]=row[4]=row[5]=row[6]=row[7]= row[0] << 3;
110 if(sizeof(DCTELEM)==2){
111 if (!(((uint32_t*)row)[1] |
112 ((uint32_t*)row)[2] |
113 ((uint32_t*)row)[3] |
115 temp = (row[0] << 3) & 0xffff;
117 ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
118 ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
122 if (!(row[1]|row[2]|row[3]|row[4]|row[5]|row[6]|row[7])) {
123 row[0]=row[1]=row[2]=row[3]=row[4]=row[5]=row[6]=row[7]= row[0] << 3;
129 a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
134 /* no need to optimize : gcc does it */
140 MUL16(b0, W1, row[1]);
141 MAC16(b0, W3, row[3]);
142 MUL16(b1, W3, row[1]);
143 MAC16(b1, -W7, row[3]);
144 MUL16(b2, W5, row[1]);
145 MAC16(b2, -W1, row[3]);
146 MUL16(b3, W7, row[1]);
147 MAC16(b3, -W5, row[3]);
149 #ifdef HAVE_FAST_64BIT
150 temp = ((uint64_t*)row)[1];
152 temp = ((uint32_t*)row)[2] | ((uint32_t*)row)[3];
155 a0 += W4*row[4] + W6*row[6];
156 a1 += - W4*row[4] - W2*row[6];
157 a2 += - W4*row[4] + W2*row[6];
158 a3 += W4*row[4] - W6*row[6];
160 MAC16(b0, W5, row[5]);
161 MAC16(b0, W7, row[7]);
163 MAC16(b1, -W1, row[5]);
164 MAC16(b1, -W5, row[7]);
166 MAC16(b2, W7, row[5]);
167 MAC16(b2, W3, row[7]);
169 MAC16(b3, W3, row[5]);
170 MAC16(b3, -W1, row[7]);
173 row[0] = (a0 + b0) >> ROW_SHIFT;
174 row[7] = (a0 - b0) >> ROW_SHIFT;
175 row[1] = (a1 + b1) >> ROW_SHIFT;
176 row[6] = (a1 - b1) >> ROW_SHIFT;
177 row[2] = (a2 + b2) >> ROW_SHIFT;
178 row[5] = (a2 - b2) >> ROW_SHIFT;
179 row[3] = (a3 + b3) >> ROW_SHIFT;
180 row[4] = (a3 - b3) >> ROW_SHIFT;
183 static inline void idctSparseColPut (uint8_t *dest, int line_size,
186 int a0, a1, a2, a3, b0, b1, b2, b3;
187 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
189 /* XXX: I did that only to give same values as previous code */
190 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
200 MUL16(b0, W1, col[8*1]);
201 MUL16(b1, W3, col[8*1]);
202 MUL16(b2, W5, col[8*1]);
203 MUL16(b3, W7, col[8*1]);
205 MAC16(b0, + W3, col[8*3]);
206 MAC16(b1, - W7, col[8*3]);
207 MAC16(b2, - W1, col[8*3]);
208 MAC16(b3, - W5, col[8*3]);
218 MAC16(b0, + W5, col[8*5]);
219 MAC16(b1, - W1, col[8*5]);
220 MAC16(b2, + W7, col[8*5]);
221 MAC16(b3, + W3, col[8*5]);
232 MAC16(b0, + W7, col[8*7]);
233 MAC16(b1, - W5, col[8*7]);
234 MAC16(b2, + W3, col[8*7]);
235 MAC16(b3, - W1, col[8*7]);
238 dest[0] = cm[(a0 + b0) >> COL_SHIFT];
240 dest[0] = cm[(a1 + b1) >> COL_SHIFT];
242 dest[0] = cm[(a2 + b2) >> COL_SHIFT];
244 dest[0] = cm[(a3 + b3) >> COL_SHIFT];
246 dest[0] = cm[(a3 - b3) >> COL_SHIFT];
248 dest[0] = cm[(a2 - b2) >> COL_SHIFT];
250 dest[0] = cm[(a1 - b1) >> COL_SHIFT];
252 dest[0] = cm[(a0 - b0) >> COL_SHIFT];
255 static inline void idctSparseColAdd (uint8_t *dest, int line_size,
258 int a0, a1, a2, a3, b0, b1, b2, b3;
259 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
261 /* XXX: I did that only to give same values as previous code */
262 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
272 MUL16(b0, W1, col[8*1]);
273 MUL16(b1, W3, col[8*1]);
274 MUL16(b2, W5, col[8*1]);
275 MUL16(b3, W7, col[8*1]);
277 MAC16(b0, + W3, col[8*3]);
278 MAC16(b1, - W7, col[8*3]);
279 MAC16(b2, - W1, col[8*3]);
280 MAC16(b3, - W5, col[8*3]);
290 MAC16(b0, + W5, col[8*5]);
291 MAC16(b1, - W1, col[8*5]);
292 MAC16(b2, + W7, col[8*5]);
293 MAC16(b3, + W3, col[8*5]);
304 MAC16(b0, + W7, col[8*7]);
305 MAC16(b1, - W5, col[8*7]);
306 MAC16(b2, + W3, col[8*7]);
307 MAC16(b3, - W1, col[8*7]);
310 dest[0] = cm[dest[0] + ((a0 + b0) >> COL_SHIFT)];
312 dest[0] = cm[dest[0] + ((a1 + b1) >> COL_SHIFT)];
314 dest[0] = cm[dest[0] + ((a2 + b2) >> COL_SHIFT)];
316 dest[0] = cm[dest[0] + ((a3 + b3) >> COL_SHIFT)];
318 dest[0] = cm[dest[0] + ((a3 - b3) >> COL_SHIFT)];
320 dest[0] = cm[dest[0] + ((a2 - b2) >> COL_SHIFT)];
322 dest[0] = cm[dest[0] + ((a1 - b1) >> COL_SHIFT)];
324 dest[0] = cm[dest[0] + ((a0 - b0) >> COL_SHIFT)];
327 static inline void idctSparseCol (DCTELEM * col)
329 int a0, a1, a2, a3, b0, b1, b2, b3;
331 /* XXX: I did that only to give same values as previous code */
332 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
342 MUL16(b0, W1, col[8*1]);
343 MUL16(b1, W3, col[8*1]);
344 MUL16(b2, W5, col[8*1]);
345 MUL16(b3, W7, col[8*1]);
347 MAC16(b0, + W3, col[8*3]);
348 MAC16(b1, - W7, col[8*3]);
349 MAC16(b2, - W1, col[8*3]);
350 MAC16(b3, - W5, col[8*3]);
360 MAC16(b0, + W5, col[8*5]);
361 MAC16(b1, - W1, col[8*5]);
362 MAC16(b2, + W7, col[8*5]);
363 MAC16(b3, + W3, col[8*5]);
374 MAC16(b0, + W7, col[8*7]);
375 MAC16(b1, - W5, col[8*7]);
376 MAC16(b2, + W3, col[8*7]);
377 MAC16(b3, - W1, col[8*7]);
380 col[0 ] = ((a0 + b0) >> COL_SHIFT);
381 col[8 ] = ((a1 + b1) >> COL_SHIFT);
382 col[16] = ((a2 + b2) >> COL_SHIFT);
383 col[24] = ((a3 + b3) >> COL_SHIFT);
384 col[32] = ((a3 - b3) >> COL_SHIFT);
385 col[40] = ((a2 - b2) >> COL_SHIFT);
386 col[48] = ((a1 - b1) >> COL_SHIFT);
387 col[56] = ((a0 - b0) >> COL_SHIFT);
390 void ff_simple_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
394 idctRowCondDC(block + i*8);
397 idctSparseColPut(dest + i, line_size, block + i);
400 void ff_simple_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
404 idctRowCondDC(block + i*8);
407 idctSparseColAdd(dest + i, line_size, block + i);
410 void ff_simple_idct(DCTELEM *block)
414 idctRowCondDC(block + i*8);
417 idctSparseCol(block + i);
423 #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
424 #define C1 C_FIX(0.6532814824)
425 #define C2 C_FIX(0.2705980501)
427 /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
428 and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
429 #define C_SHIFT (4+1+12)
431 static inline void idct4col_put(uint8_t *dest, int line_size, const DCTELEM *col)
433 int c0, c1, c2, c3, a0, a1, a2, a3;
434 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
440 c0 = ((a0 + a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
441 c2 = ((a0 - a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
442 c1 = a1 * C1 + a3 * C2;
443 c3 = a1 * C2 - a3 * C1;
444 dest[0] = cm[(c0 + c1) >> C_SHIFT];
446 dest[0] = cm[(c2 + c3) >> C_SHIFT];
448 dest[0] = cm[(c2 - c3) >> C_SHIFT];
450 dest[0] = cm[(c0 - c1) >> C_SHIFT];
459 ptr[8 + k] = a0 - a1;\
462 /* only used by DV codec. The input must be interlaced. 128 is added
463 to the pixels before clamping to avoid systematic error
464 (1024*sqrt(2)) offset would be needed otherwise. */
465 /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
466 compensate the extra butterfly stage - I don't have the full DV
468 void ff_simple_idct248_put(uint8_t *dest, int line_size, DCTELEM *block)
487 /* IDCT8 on each line */
489 idctRowCondDC(block + i*8);
492 /* IDCT4 and store */
494 idct4col_put(dest + i, 2 * line_size, block + i);
495 idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i);
499 /* 8x4 & 4x8 WMV2 IDCT */
506 #define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
507 #define C1 C_FIX(0.6532814824)
508 #define C2 C_FIX(0.2705980501)
509 #define C3 C_FIX(0.5)
510 #define C_SHIFT (4+1+12)
511 static inline void idct4col_add(uint8_t *dest, int line_size, const DCTELEM *col)
513 int c0, c1, c2, c3, a0, a1, a2, a3;
514 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
520 c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
521 c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
522 c1 = a1 * C1 + a3 * C2;
523 c3 = a1 * C2 - a3 * C1;
524 dest[0] = cm[dest[0] + ((c0 + c1) >> C_SHIFT)];
526 dest[0] = cm[dest[0] + ((c2 + c3) >> C_SHIFT)];
528 dest[0] = cm[dest[0] + ((c2 - c3) >> C_SHIFT)];
530 dest[0] = cm[dest[0] + ((c0 - c1) >> C_SHIFT)];
534 #define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
535 #define R1 R_FIX(0.6532814824)
536 #define R2 R_FIX(0.2705980501)
537 #define R3 R_FIX(0.5)
539 static inline void idct4row(DCTELEM *row)
541 int c0, c1, c2, c3, a0, a1, a2, a3;
542 //const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
548 c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
549 c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
550 c1 = a1 * R1 + a3 * R2;
551 c3 = a1 * R2 - a3 * R1;
552 row[0]= (c0 + c1) >> R_SHIFT;
553 row[1]= (c2 + c3) >> R_SHIFT;
554 row[2]= (c2 - c3) >> R_SHIFT;
555 row[3]= (c0 - c1) >> R_SHIFT;
558 void ff_simple_idct84_add(uint8_t *dest, int line_size, DCTELEM *block)
562 /* IDCT8 on each line */
564 idctRowCondDC(block + i*8);
567 /* IDCT4 and store */
569 idct4col_add(dest + i, line_size, block + i);
573 void ff_simple_idct48_add(uint8_t *dest, int line_size, DCTELEM *block)
577 /* IDCT4 on each line */
579 idct4row(block + i*8);
582 /* IDCT8 and store */
584 idctSparseColAdd(dest + i, line_size, block + i);
588 void ff_simple_idct44_add(uint8_t *dest, int line_size, DCTELEM *block)
592 /* IDCT4 on each line */
594 idct4row(block + i*8);
597 /* IDCT4 and store */
599 idct4col_add(dest + i, line_size, block + i);