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));
125 b0 = MUL(W1, row[1]);
127 b1 = MUL(W3, row[1]);
128 MAC(b1, -W7, row[3]);
129 b2 = MUL(W5, row[1]);
130 MAC(b2, -W1, row[3]);
131 b3 = MUL(W7, row[1]);
132 MAC(b3, -W5, row[3]);
134 if (AV_RN64A(row + 4)) {
135 a0 += W4*row[4] + W6*row[6];
136 a1 += - W4*row[4] - W2*row[6];
137 a2 += - W4*row[4] + W2*row[6];
138 a3 += W4*row[4] - W6*row[6];
143 MAC(b1, -W1, row[5]);
144 MAC(b1, -W5, row[7]);
150 MAC(b3, -W1, row[7]);
153 row[0] = (a0 + b0) >> ROW_SHIFT;
154 row[7] = (a0 - b0) >> ROW_SHIFT;
155 row[1] = (a1 + b1) >> ROW_SHIFT;
156 row[6] = (a1 - b1) >> ROW_SHIFT;
157 row[2] = (a2 + b2) >> ROW_SHIFT;
158 row[5] = (a2 - b2) >> ROW_SHIFT;
159 row[3] = (a3 + b3) >> ROW_SHIFT;
160 row[4] = (a3 - b3) >> ROW_SHIFT;
163 #define IDCT_COLS do { \
164 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
171 a2 += -W6*col[8*2]; \
172 a3 += -W2*col[8*2]; \
174 b0 = MUL(W1, col[8*1]); \
175 b1 = MUL(W3, col[8*1]); \
176 b2 = MUL(W5, col[8*1]); \
177 b3 = MUL(W7, col[8*1]); \
179 MAC(b0, W3, col[8*3]); \
180 MAC(b1, -W7, col[8*3]); \
181 MAC(b2, -W1, col[8*3]); \
182 MAC(b3, -W5, col[8*3]); \
186 a1 += -W4*col[8*4]; \
187 a2 += -W4*col[8*4]; \
192 MAC(b0, W5, col[8*5]); \
193 MAC(b1, -W1, col[8*5]); \
194 MAC(b2, W7, col[8*5]); \
195 MAC(b3, W3, col[8*5]); \
200 a1 += -W2*col[8*6]; \
202 a3 += -W6*col[8*6]; \
206 MAC(b0, W7, col[8*7]); \
207 MAC(b1, -W5, col[8*7]); \
208 MAC(b2, W3, col[8*7]); \
209 MAC(b3, -W1, col[8*7]); \
213 static inline void FUNC(idctSparseColPut)(pixel *dest, int line_size,
216 int a0, a1, a2, a3, b0, b1, b2, b3;
221 dest[0] = CLIP((a0 + b0) >> COL_SHIFT);
223 dest[0] = CLIP((a1 + b1) >> COL_SHIFT);
225 dest[0] = CLIP((a2 + b2) >> COL_SHIFT);
227 dest[0] = CLIP((a3 + b3) >> COL_SHIFT);
229 dest[0] = CLIP((a3 - b3) >> COL_SHIFT);
231 dest[0] = CLIP((a2 - b2) >> COL_SHIFT);
233 dest[0] = CLIP((a1 - b1) >> COL_SHIFT);
235 dest[0] = CLIP((a0 - b0) >> COL_SHIFT);
238 static inline void FUNC(idctSparseColAdd)(pixel *dest, int line_size,
241 int a0, a1, a2, a3, b0, b1, b2, b3;
246 dest[0] = CLIP(dest[0] + ((a0 + b0) >> COL_SHIFT));
248 dest[0] = CLIP(dest[0] + ((a1 + b1) >> COL_SHIFT));
250 dest[0] = CLIP(dest[0] + ((a2 + b2) >> COL_SHIFT));
252 dest[0] = CLIP(dest[0] + ((a3 + b3) >> COL_SHIFT));
254 dest[0] = CLIP(dest[0] + ((a3 - b3) >> COL_SHIFT));
256 dest[0] = CLIP(dest[0] + ((a2 - b2) >> COL_SHIFT));
258 dest[0] = CLIP(dest[0] + ((a1 - b1) >> COL_SHIFT));
260 dest[0] = CLIP(dest[0] + ((a0 - b0) >> COL_SHIFT));
263 static inline void FUNC(idctSparseCol)(DCTELEM *col)
265 int a0, a1, a2, a3, b0, b1, b2, b3;
269 col[0 ] = ((a0 + b0) >> COL_SHIFT);
270 col[8 ] = ((a1 + b1) >> COL_SHIFT);
271 col[16] = ((a2 + b2) >> COL_SHIFT);
272 col[24] = ((a3 + b3) >> COL_SHIFT);
273 col[32] = ((a3 - b3) >> COL_SHIFT);
274 col[40] = ((a2 - b2) >> COL_SHIFT);
275 col[48] = ((a1 - b1) >> COL_SHIFT);
276 col[56] = ((a0 - b0) >> COL_SHIFT);
279 void FUNC(ff_simple_idct_put)(uint8_t *dest_, int line_size, DCTELEM *block)
281 pixel *dest = (pixel *)dest_;
284 line_size /= sizeof(pixel);
286 for (i = 0; i < 8; i++)
287 FUNC(idctRowCondDC)(block + i*8);
289 for (i = 0; i < 8; i++)
290 FUNC(idctSparseColPut)(dest + i, line_size, block + i);
293 void FUNC(ff_simple_idct_add)(uint8_t *dest_, int line_size, DCTELEM *block)
295 pixel *dest = (pixel *)dest_;
298 line_size /= sizeof(pixel);
300 for (i = 0; i < 8; i++)
301 FUNC(idctRowCondDC)(block + i*8);
303 for (i = 0; i < 8; i++)
304 FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
307 void FUNC(ff_simple_idct)(DCTELEM *block)
311 for (i = 0; i < 8; i++)
312 FUNC(idctRowCondDC)(block + i*8);
314 for (i = 0; i < 8; i++)
315 FUNC(idctSparseCol)(block + i);