4 * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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>)
33 #include "bit_depth_template.c"
50 #define W1 22725 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
51 #define W2 21407 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
52 #define W3 19266 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
53 #define W4 16383 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
54 #define W5 12873 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
55 #define W6 8867 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
56 #define W7 4520 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
62 #define MUL(a, b) MUL16(a, b)
63 #define MAC(a, b, c) MAC16(a, b, c)
79 #define MUL(a, b) ((a) * (b))
80 #define MAC(a, b, c) ((a) += (b) * (c))
84 #error "Unsupported bitdepth"
88 static inline void FUNC(idctRowCondDC)(DCTELEM *row)
90 int a0, a1, a2, a3, b0, b1, b2, b3;
93 #define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
94 if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {
95 uint64_t temp = (row[0] << DC_SHIFT) & 0xffff;
98 ((uint64_t *)row)[0] = temp;
99 ((uint64_t *)row)[1] = temp;
103 if (!(((uint32_t*)row)[1] |
104 ((uint32_t*)row)[2] |
105 ((uint32_t*)row)[3] |
107 uint32_t temp = (row[0] << DC_SHIFT) & 0xffff;
109 ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
110 ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
115 a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
120 /* no need to optimize : gcc does it */
126 b0 = MUL(W1, row[1]);
128 b1 = MUL(W3, row[1]);
129 MAC(b1, -W7, row[3]);
130 b2 = MUL(W5, row[1]);
131 MAC(b2, -W1, row[3]);
132 b3 = MUL(W7, row[1]);
133 MAC(b3, -W5, row[3]);
135 if (AV_RN64A(row + 4)) {
136 a0 += W4*row[4] + W6*row[6];
137 a1 += - W4*row[4] - W2*row[6];
138 a2 += - W4*row[4] + W2*row[6];
139 a3 += W4*row[4] - W6*row[6];
144 MAC(b1, -W1, row[5]);
145 MAC(b1, -W5, row[7]);
151 MAC(b3, -W1, row[7]);
154 row[0] = (a0 + b0) >> ROW_SHIFT;
155 row[7] = (a0 - b0) >> ROW_SHIFT;
156 row[1] = (a1 + b1) >> ROW_SHIFT;
157 row[6] = (a1 - b1) >> ROW_SHIFT;
158 row[2] = (a2 + b2) >> ROW_SHIFT;
159 row[5] = (a2 - b2) >> ROW_SHIFT;
160 row[3] = (a3 + b3) >> ROW_SHIFT;
161 row[4] = (a3 - b3) >> ROW_SHIFT;
164 #define IDCT_COLS do { \
165 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
172 a2 += -W6*col[8*2]; \
173 a3 += -W2*col[8*2]; \
175 b0 = MUL(W1, col[8*1]); \
176 b1 = MUL(W3, col[8*1]); \
177 b2 = MUL(W5, col[8*1]); \
178 b3 = MUL(W7, col[8*1]); \
180 MAC(b0, W3, col[8*3]); \
181 MAC(b1, -W7, col[8*3]); \
182 MAC(b2, -W1, col[8*3]); \
183 MAC(b3, -W5, col[8*3]); \
187 a1 += -W4*col[8*4]; \
188 a2 += -W4*col[8*4]; \
193 MAC(b0, W5, col[8*5]); \
194 MAC(b1, -W1, col[8*5]); \
195 MAC(b2, W7, col[8*5]); \
196 MAC(b3, W3, col[8*5]); \
201 a1 += -W2*col[8*6]; \
203 a3 += -W6*col[8*6]; \
207 MAC(b0, W7, col[8*7]); \
208 MAC(b1, -W5, col[8*7]); \
209 MAC(b2, W3, col[8*7]); \
210 MAC(b3, -W1, col[8*7]); \
214 static inline void FUNC(idctSparseColPut)(pixel *dest, int line_size,
217 int a0, a1, a2, a3, b0, b1, b2, b3;
222 dest[0] = CLIP((a0 + b0) >> COL_SHIFT);
224 dest[0] = CLIP((a1 + b1) >> COL_SHIFT);
226 dest[0] = CLIP((a2 + b2) >> COL_SHIFT);
228 dest[0] = CLIP((a3 + b3) >> COL_SHIFT);
230 dest[0] = CLIP((a3 - b3) >> COL_SHIFT);
232 dest[0] = CLIP((a2 - b2) >> COL_SHIFT);
234 dest[0] = CLIP((a1 - b1) >> COL_SHIFT);
236 dest[0] = CLIP((a0 - b0) >> COL_SHIFT);
239 static inline void FUNC(idctSparseColAdd)(pixel *dest, int line_size,
242 int a0, a1, a2, a3, b0, b1, b2, b3;
247 dest[0] = CLIP(dest[0] + ((a0 + b0) >> COL_SHIFT));
249 dest[0] = CLIP(dest[0] + ((a1 + b1) >> COL_SHIFT));
251 dest[0] = CLIP(dest[0] + ((a2 + b2) >> COL_SHIFT));
253 dest[0] = CLIP(dest[0] + ((a3 + b3) >> COL_SHIFT));
255 dest[0] = CLIP(dest[0] + ((a3 - b3) >> COL_SHIFT));
257 dest[0] = CLIP(dest[0] + ((a2 - b2) >> COL_SHIFT));
259 dest[0] = CLIP(dest[0] + ((a1 - b1) >> COL_SHIFT));
261 dest[0] = CLIP(dest[0] + ((a0 - b0) >> COL_SHIFT));
264 static inline void FUNC(idctSparseCol)(DCTELEM *col)
266 int a0, a1, a2, a3, b0, b1, b2, b3;
270 col[0 ] = ((a0 + b0) >> COL_SHIFT);
271 col[8 ] = ((a1 + b1) >> COL_SHIFT);
272 col[16] = ((a2 + b2) >> COL_SHIFT);
273 col[24] = ((a3 + b3) >> COL_SHIFT);
274 col[32] = ((a3 - b3) >> COL_SHIFT);
275 col[40] = ((a2 - b2) >> COL_SHIFT);
276 col[48] = ((a1 - b1) >> COL_SHIFT);
277 col[56] = ((a0 - b0) >> COL_SHIFT);
280 void FUNC(ff_simple_idct_put)(uint8_t *dest_, int line_size, DCTELEM *block)
282 pixel *dest = (pixel *)dest_;
285 line_size /= sizeof(pixel);
288 FUNC(idctRowCondDC)(block + i*8);
291 FUNC(idctSparseColPut)(dest + i, line_size, block + i);
294 void FUNC(ff_simple_idct_add)(uint8_t *dest_, int line_size, DCTELEM *block)
296 pixel *dest = (pixel *)dest_;
299 line_size /= sizeof(pixel);
302 FUNC(idctRowCondDC)(block + i*8);
305 FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
308 void FUNC(ff_simple_idct)(DCTELEM *block)
312 FUNC(idctRowCondDC)(block + i*8);
315 FUNC(idctSparseCol)(block + i);