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
28 /* Based upon some commented-out C code from mpeg2dec (idct_mmx.c
29 * written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>). */
31 #include "simple_idct.h"
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
68 #define W1 22725 // 90901
69 #define W2 21407 // 85627
70 #define W3 19265 // 77062
71 #define W4 16384 // 65535
72 #define W5 12873 // 51491
73 #define W6 8867 // 35468
74 #define W7 4520 // 18081
80 # elif IN_IDCT_DEPTH == 32
104 #define MUL(a, b) ((int)((SUINT)(a) * (b)))
105 #define MAC(a, b, c) ((a) += (SUINT)(b) * (c))
109 #error "Unsupported bitdepth"
114 static inline void FUNC(idctRowCondDC_extrashift
)(int16_t *row
, int extra_shift
)
116 static inline void FUNC6(idctRowCondDC
)(idctin
*row
, int extra_shift
)
119 SUINT a0
, a1
, a2
, a3
, b0
, b1
, b2
, b3
;
121 // TODO: Add DC-only support for int32_t input
122 #if IN_IDCT_DEPTH == 16
124 #define ROW0_MASK (0xffffULL << 48 * HAVE_BIGENDIAN)
125 if (((AV_RN64A(row
) & ~ROW0_MASK
) | AV_RN64A(row
+4)) == 0) {
127 if (DC_SHIFT
- extra_shift
>= 0) {
128 temp
= (row
[0] * (1 << (DC_SHIFT
- extra_shift
))) & 0xffff;
130 temp
= ((row
[0] + (1<<(extra_shift
- DC_SHIFT
-1))) >> (extra_shift
- DC_SHIFT
)) & 0xffff;
132 temp
+= temp
* (1 << 16);
133 temp
+= temp
* ((uint64_t) 1 << 32);
135 AV_WN64A(row
+ 4, temp
);
139 if (!(AV_RN32A(row
+2) |
144 if (DC_SHIFT
- extra_shift
>= 0) {
145 temp
= (row
[0] * (1 << (DC_SHIFT
- extra_shift
))) & 0xffff;
147 temp
= ((row
[0] + (1<<(extra_shift
- DC_SHIFT
-1))) >> (extra_shift
- DC_SHIFT
)) & 0xffff;
149 temp
+= temp
* (1 << 16);
151 AV_WN32A(row
+2, temp
);
152 AV_WN32A(row
+4, temp
);
153 AV_WN32A(row
+6, temp
);
159 a0
= ((SUINT
)W4
* row
[0]) + (1 << (ROW_SHIFT
+ extra_shift
- 1));
164 a0
+= (SUINT
)W2
* row
[2];
165 a1
+= (SUINT
)W6
* row
[2];
166 a2
-= (SUINT
)W6
* row
[2];
167 a3
-= (SUINT
)W2
* row
[2];
169 b0
= MUL(W1
, row
[1]);
171 b1
= MUL(W3
, row
[1]);
172 MAC(b1
, -W7
, row
[3]);
173 b2
= MUL(W5
, row
[1]);
174 MAC(b2
, -W1
, row
[3]);
175 b3
= MUL(W7
, row
[1]);
176 MAC(b3
, -W5
, row
[3]);
178 #if IN_IDCT_DEPTH == 32
179 if (AV_RN64A(row
+ 4) | AV_RN64A(row
+ 6)) {
181 if (AV_RN64A(row
+ 4)) {
183 a0
+= (SUINT
) W4
*row
[4] + (SUINT
)W6
*row
[6];
184 a1
+= (SUINT
)- W4
*row
[4] - (SUINT
)W2
*row
[6];
185 a2
+= (SUINT
)- W4
*row
[4] + (SUINT
)W2
*row
[6];
186 a3
+= (SUINT
) W4
*row
[4] - (SUINT
)W6
*row
[6];
191 MAC(b1
, -W1
, row
[5]);
192 MAC(b1
, -W5
, row
[7]);
198 MAC(b3
, -W1
, row
[7]);
201 row
[0] = (int)(a0
+ b0
) >> (ROW_SHIFT
+ extra_shift
);
202 row
[7] = (int)(a0
- b0
) >> (ROW_SHIFT
+ extra_shift
);
203 row
[1] = (int)(a1
+ b1
) >> (ROW_SHIFT
+ extra_shift
);
204 row
[6] = (int)(a1
- b1
) >> (ROW_SHIFT
+ extra_shift
);
205 row
[2] = (int)(a2
+ b2
) >> (ROW_SHIFT
+ extra_shift
);
206 row
[5] = (int)(a2
- b2
) >> (ROW_SHIFT
+ extra_shift
);
207 row
[3] = (int)(a3
+ b3
) >> (ROW_SHIFT
+ extra_shift
);
208 row
[4] = (int)(a3
- b3
) >> (ROW_SHIFT
+ extra_shift
);
211 #define IDCT_COLS do { \
212 a0 = (SUINT)W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
217 a0 += (SUINT) W2*col[8*2]; \
218 a1 += (SUINT) W6*col[8*2]; \
219 a2 += (SUINT)-W6*col[8*2]; \
220 a3 += (SUINT)-W2*col[8*2]; \
222 b0 = MUL(W1, col[8*1]); \
223 b1 = MUL(W3, col[8*1]); \
224 b2 = MUL(W5, col[8*1]); \
225 b3 = MUL(W7, col[8*1]); \
227 MAC(b0, W3, col[8*3]); \
228 MAC(b1, -W7, col[8*3]); \
229 MAC(b2, -W1, col[8*3]); \
230 MAC(b3, -W5, col[8*3]); \
233 a0 += (SUINT) W4*col[8*4]; \
234 a1 += (SUINT)-W4*col[8*4]; \
235 a2 += (SUINT)-W4*col[8*4]; \
236 a3 += (SUINT) W4*col[8*4]; \
240 MAC(b0, W5, col[8*5]); \
241 MAC(b1, -W1, col[8*5]); \
242 MAC(b2, W7, col[8*5]); \
243 MAC(b3, W3, col[8*5]); \
247 a0 += (SUINT) W6*col[8*6]; \
248 a1 += (SUINT)-W2*col[8*6]; \
249 a2 += (SUINT) W2*col[8*6]; \
250 a3 += (SUINT)-W6*col[8*6]; \
254 MAC(b0, W7, col[8*7]); \
255 MAC(b1, -W5, col[8*7]); \
256 MAC(b2, W3, col[8*7]); \
257 MAC(b3, -W1, col[8*7]); \
262 static inline void FUNC(idctSparseCol_extrashift
)(int16_t *col
)
264 static inline void FUNC6(idctSparseColPut
)(pixel
*dest
, ptrdiff_t line_size
,
267 SUINT a0
, a1
, a2
, a3
, b0
, b1
, b2
, b3
;
271 dest
[0] = av_clip_pixel((int)(a0
+ b0
) >> COL_SHIFT
);
273 dest
[0] = av_clip_pixel((int)(a1
+ b1
) >> COL_SHIFT
);
275 dest
[0] = av_clip_pixel((int)(a2
+ b2
) >> COL_SHIFT
);
277 dest
[0] = av_clip_pixel((int)(a3
+ b3
) >> COL_SHIFT
);
279 dest
[0] = av_clip_pixel((int)(a3
- b3
) >> COL_SHIFT
);
281 dest
[0] = av_clip_pixel((int)(a2
- b2
) >> COL_SHIFT
);
283 dest
[0] = av_clip_pixel((int)(a1
- b1
) >> COL_SHIFT
);
285 dest
[0] = av_clip_pixel((int)(a0
- b0
) >> COL_SHIFT
);
288 static inline void FUNC6(idctSparseColAdd
)(pixel
*dest
, ptrdiff_t line_size
,
291 unsigned a0
, a1
, a2
, a3
, b0
, b1
, b2
, b3
;
295 dest
[0] = av_clip_pixel(dest
[0] + ((int)(a0
+ b0
) >> COL_SHIFT
));
297 dest
[0] = av_clip_pixel(dest
[0] + ((int)(a1
+ b1
) >> COL_SHIFT
));
299 dest
[0] = av_clip_pixel(dest
[0] + ((int)(a2
+ b2
) >> COL_SHIFT
));
301 dest
[0] = av_clip_pixel(dest
[0] + ((int)(a3
+ b3
) >> COL_SHIFT
));
303 dest
[0] = av_clip_pixel(dest
[0] + ((int)(a3
- b3
) >> COL_SHIFT
));
305 dest
[0] = av_clip_pixel(dest
[0] + ((int)(a2
- b2
) >> COL_SHIFT
));
307 dest
[0] = av_clip_pixel(dest
[0] + ((int)(a1
- b1
) >> COL_SHIFT
));
309 dest
[0] = av_clip_pixel(dest
[0] + ((int)(a0
- b0
) >> COL_SHIFT
));
312 static inline void FUNC6(idctSparseCol
)(idctin
*col
)
315 unsigned a0
, a1
, a2
, a3
, b0
, b1
, b2
, b3
;
319 col
[0 ] = ((int)(a0
+ b0
) >> COL_SHIFT
);
320 col
[8 ] = ((int)(a1
+ b1
) >> COL_SHIFT
);
321 col
[16] = ((int)(a2
+ b2
) >> COL_SHIFT
);
322 col
[24] = ((int)(a3
+ b3
) >> COL_SHIFT
);
323 col
[32] = ((int)(a3
- b3
) >> COL_SHIFT
);
324 col
[40] = ((int)(a2
- b2
) >> COL_SHIFT
);
325 col
[48] = ((int)(a1
- b1
) >> COL_SHIFT
);
326 col
[56] = ((int)(a0
- b0
) >> COL_SHIFT
);
330 void FUNC6(ff_simple_idct_put
)(uint8_t *dest_
, ptrdiff_t line_size
, int16_t *block_
)
332 idctin
*block
= (idctin
*)block_
;
333 pixel
*dest
= (pixel
*)dest_
;
336 line_size
/= sizeof(pixel
);
338 for (i
= 0; i
< 8; i
++)
339 FUNC6(idctRowCondDC
)(block
+ i
*8, 0);
341 for (i
= 0; i
< 8; i
++)
342 FUNC6(idctSparseColPut
)(dest
+ i
, line_size
, block
+ i
);
345 #if IN_IDCT_DEPTH == 16
346 void FUNC6(ff_simple_idct_add
)(uint8_t *dest_
, ptrdiff_t line_size
, int16_t *block
)
348 pixel
*dest
= (pixel
*)dest_
;
351 line_size
/= sizeof(pixel
);
353 for (i
= 0; i
< 8; i
++)
354 FUNC6(idctRowCondDC
)(block
+ i
*8, 0);
356 for (i
= 0; i
< 8; i
++)
357 FUNC6(idctSparseColAdd
)(dest
+ i
, line_size
, block
+ i
);
360 void FUNC6(ff_simple_idct
)(int16_t *block
)
364 for (i
= 0; i
< 8; i
++)
365 FUNC6(idctRowCondDC
)(block
+ i
*8, 0);
367 for (i
= 0; i
< 8; i
++)
368 FUNC6(idctSparseCol
)(block
+ i
);