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>)
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)
65 #elif BIT_DEPTH == 10 || BIT_DEPTH == 12
93 #define MUL(a, b) ((a) * (b))
94 #define MAC(a, b, c) ((a) += (b) * (c))
98 #error "Unsupported bitdepth"
102 static inline void FUNC(idctRowCondDC)(int16_t *row, int extra_shift)
104 int a0, a1, a2, a3, b0, b1, b2, b3;
107 #define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
108 if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {
110 if (DC_SHIFT - extra_shift > 0) {
111 temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
113 temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
117 ((uint64_t *)row)[0] = temp;
118 ((uint64_t *)row)[1] = temp;
122 if (!(((uint32_t*)row)[1] |
123 ((uint32_t*)row)[2] |
124 ((uint32_t*)row)[3] |
127 if (DC_SHIFT - extra_shift > 0) {
128 temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
130 temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
133 ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
134 ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
139 a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
149 b0 = MUL(W1, row[1]);
151 b1 = MUL(W3, row[1]);
152 MAC(b1, -W7, row[3]);
153 b2 = MUL(W5, row[1]);
154 MAC(b2, -W1, row[3]);
155 b3 = MUL(W7, row[1]);
156 MAC(b3, -W5, row[3]);
158 if (AV_RN64A(row + 4)) {
159 a0 += W4*row[4] + W6*row[6];
160 a1 += - W4*row[4] - W2*row[6];
161 a2 += - W4*row[4] + W2*row[6];
162 a3 += W4*row[4] - W6*row[6];
167 MAC(b1, -W1, row[5]);
168 MAC(b1, -W5, row[7]);
174 MAC(b3, -W1, row[7]);
177 row[0] = (a0 + b0) >> (ROW_SHIFT + extra_shift);
178 row[7] = (a0 - b0) >> (ROW_SHIFT + extra_shift);
179 row[1] = (a1 + b1) >> (ROW_SHIFT + extra_shift);
180 row[6] = (a1 - b1) >> (ROW_SHIFT + extra_shift);
181 row[2] = (a2 + b2) >> (ROW_SHIFT + extra_shift);
182 row[5] = (a2 - b2) >> (ROW_SHIFT + extra_shift);
183 row[3] = (a3 + b3) >> (ROW_SHIFT + extra_shift);
184 row[4] = (a3 - b3) >> (ROW_SHIFT + extra_shift);
187 #define IDCT_COLS do { \
188 a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
195 a2 += -W6*col[8*2]; \
196 a3 += -W2*col[8*2]; \
198 b0 = MUL(W1, col[8*1]); \
199 b1 = MUL(W3, col[8*1]); \
200 b2 = MUL(W5, col[8*1]); \
201 b3 = MUL(W7, col[8*1]); \
203 MAC(b0, W3, col[8*3]); \
204 MAC(b1, -W7, col[8*3]); \
205 MAC(b2, -W1, col[8*3]); \
206 MAC(b3, -W5, col[8*3]); \
210 a1 += -W4*col[8*4]; \
211 a2 += -W4*col[8*4]; \
216 MAC(b0, W5, col[8*5]); \
217 MAC(b1, -W1, col[8*5]); \
218 MAC(b2, W7, col[8*5]); \
219 MAC(b3, W3, col[8*5]); \
224 a1 += -W2*col[8*6]; \
226 a3 += -W6*col[8*6]; \
230 MAC(b0, W7, col[8*7]); \
231 MAC(b1, -W5, col[8*7]); \
232 MAC(b2, W3, col[8*7]); \
233 MAC(b3, -W1, col[8*7]); \
237 static inline void FUNC(idctSparseColPut)(pixel *dest, int line_size,
240 int a0, a1, a2, a3, b0, b1, b2, b3;
244 dest[0] = av_clip_pixel((a0 + b0) >> COL_SHIFT);
246 dest[0] = av_clip_pixel((a1 + b1) >> COL_SHIFT);
248 dest[0] = av_clip_pixel((a2 + b2) >> COL_SHIFT);
250 dest[0] = av_clip_pixel((a3 + b3) >> COL_SHIFT);
252 dest[0] = av_clip_pixel((a3 - b3) >> COL_SHIFT);
254 dest[0] = av_clip_pixel((a2 - b2) >> COL_SHIFT);
256 dest[0] = av_clip_pixel((a1 - b1) >> COL_SHIFT);
258 dest[0] = av_clip_pixel((a0 - b0) >> COL_SHIFT);
261 static inline void FUNC(idctSparseColAdd)(pixel *dest, int line_size,
264 int a0, a1, a2, a3, b0, b1, b2, b3;
268 dest[0] = av_clip_pixel(dest[0] + ((a0 + b0) >> COL_SHIFT));
270 dest[0] = av_clip_pixel(dest[0] + ((a1 + b1) >> COL_SHIFT));
272 dest[0] = av_clip_pixel(dest[0] + ((a2 + b2) >> COL_SHIFT));
274 dest[0] = av_clip_pixel(dest[0] + ((a3 + b3) >> COL_SHIFT));
276 dest[0] = av_clip_pixel(dest[0] + ((a3 - b3) >> COL_SHIFT));
278 dest[0] = av_clip_pixel(dest[0] + ((a2 - b2) >> COL_SHIFT));
280 dest[0] = av_clip_pixel(dest[0] + ((a1 - b1) >> COL_SHIFT));
282 dest[0] = av_clip_pixel(dest[0] + ((a0 - b0) >> COL_SHIFT));
285 static inline void FUNC(idctSparseCol)(int16_t *col)
287 int a0, a1, a2, a3, b0, b1, b2, b3;
291 col[0 ] = ((a0 + b0) >> COL_SHIFT);
292 col[8 ] = ((a1 + b1) >> COL_SHIFT);
293 col[16] = ((a2 + b2) >> COL_SHIFT);
294 col[24] = ((a3 + b3) >> COL_SHIFT);
295 col[32] = ((a3 - b3) >> COL_SHIFT);
296 col[40] = ((a2 - b2) >> COL_SHIFT);
297 col[48] = ((a1 - b1) >> COL_SHIFT);
298 col[56] = ((a0 - b0) >> COL_SHIFT);
301 void FUNC(ff_simple_idct_put)(uint8_t *dest_, int line_size, int16_t *block)
303 pixel *dest = (pixel *)dest_;
306 line_size /= sizeof(pixel);
308 for (i = 0; i < 8; i++)
309 FUNC(idctRowCondDC)(block + i*8, 0);
311 for (i = 0; i < 8; i++)
312 FUNC(idctSparseColPut)(dest + i, line_size, block + i);
315 void FUNC(ff_simple_idct_add)(uint8_t *dest_, int line_size, int16_t *block)
317 pixel *dest = (pixel *)dest_;
320 line_size /= sizeof(pixel);
322 for (i = 0; i < 8; i++)
323 FUNC(idctRowCondDC)(block + i*8, 0);
325 for (i = 0; i < 8; i++)
326 FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
329 void FUNC(ff_simple_idct)(int16_t *block)
333 for (i = 0; i < 8; i++)
334 FUNC(idctRowCondDC)(block + i*8, 0);
336 for (i = 0; i < 8; i++)
337 FUNC(idctSparseCol)(block + i);