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 #include "libavutil/intreadwrite.h"
30 #include "simple_idct.h"
32 #define IN_IDCT_DEPTH 16
35 #include "simple_idct_template.c"
39 #include "simple_idct_template.c"
42 #include "simple_idct_template.c"
48 #include "simple_idct_template.c"
52 #define IN_IDCT_DEPTH 32
54 #include "simple_idct_template.c"
61 #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
62 #define C1 C_FIX(0.6532814824)
63 #define C2 C_FIX(0.2705980501)
65 /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
66 and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
67 #define C_SHIFT (4+1+12)
69 static inline void idct4col_put(uint8_t *dest
, ptrdiff_t line_size
, const int16_t *col
)
71 int c0
, c1
, c2
, c3
, a0
, a1
, a2
, a3
;
77 c0
= ((a0
+ a2
) * (1 << CN_SHIFT
- 1)) + (1 << (C_SHIFT
- 1));
78 c2
= ((a0
- a2
) * (1 << CN_SHIFT
- 1)) + (1 << (C_SHIFT
- 1));
79 c1
= a1
* C1
+ a3
* C2
;
80 c3
= a1
* C2
- a3
* C1
;
81 dest
[0] = av_clip_uint8((c0
+ c1
) >> C_SHIFT
);
83 dest
[0] = av_clip_uint8((c2
+ c3
) >> C_SHIFT
);
85 dest
[0] = av_clip_uint8((c2
- c3
) >> C_SHIFT
);
87 dest
[0] = av_clip_uint8((c0
- c1
) >> C_SHIFT
);
96 ptr[8 + k] = a0 - a1;\
99 /* only used by DV codec. The input must be interlaced. 128 is added
100 to the pixels before clamping to avoid systematic error
101 (1024*sqrt(2)) offset would be needed otherwise. */
102 /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
103 compensate the extra butterfly stage - I don't have the full DV
105 void ff_simple_idct248_put(uint8_t *dest
, ptrdiff_t line_size
, int16_t *block
)
124 /* IDCT8 on each line */
126 idctRowCondDC_int16_8bit(block
+ i
*8, 0);
129 /* IDCT4 and store */
131 idct4col_put(dest
+ i
, 2 * line_size
, block
+ i
);
132 idct4col_put(dest
+ line_size
+ i
, 2 * line_size
, block
+ 8 + i
);
136 /* 8x4 & 4x8 WMV2 IDCT */
143 #define C_FIX(x) ((int)((x) * M_SQRT2 * (1 << CN_SHIFT) + 0.5))
144 #define C1 C_FIX(0.6532814824)
145 #define C2 C_FIX(0.2705980501)
146 #define C3 C_FIX(0.5)
147 #define C_SHIFT (4+1+12)
148 static inline void idct4col_add(uint8_t *dest
, ptrdiff_t line_size
, const int16_t *col
)
150 int c0
, c1
, c2
, c3
, a0
, a1
, a2
, a3
;
156 c0
= (a0
+ a2
)*C3
+ (1 << (C_SHIFT
- 1));
157 c2
= (a0
- a2
)*C3
+ (1 << (C_SHIFT
- 1));
158 c1
= a1
* C1
+ a3
* C2
;
159 c3
= a1
* C2
- a3
* C1
;
160 dest
[0] = av_clip_uint8(dest
[0] + ((c0
+ c1
) >> C_SHIFT
));
162 dest
[0] = av_clip_uint8(dest
[0] + ((c2
+ c3
) >> C_SHIFT
));
164 dest
[0] = av_clip_uint8(dest
[0] + ((c2
- c3
) >> C_SHIFT
));
166 dest
[0] = av_clip_uint8(dest
[0] + ((c0
- c1
) >> C_SHIFT
));
170 #define R_FIX(x) ((int)((x) * M_SQRT2 * (1 << RN_SHIFT) + 0.5))
171 #define R1 R_FIX(0.6532814824)
172 #define R2 R_FIX(0.2705980501)
173 #define R3 R_FIX(0.5)
175 static inline void idct4row(int16_t *row
)
177 unsigned c0
, c1
, c2
, c3
;
184 c0
= (a0
+ a2
)*R3
+ (1 << (R_SHIFT
- 1));
185 c2
= (a0
- a2
)*R3
+ (1 << (R_SHIFT
- 1));
186 c1
= a1
* R1
+ a3
* R2
;
187 c3
= a1
* R2
- a3
* R1
;
188 row
[0]= (c0
+ c1
) >> R_SHIFT
;
189 row
[1]= (c2
+ c3
) >> R_SHIFT
;
190 row
[2]= (c2
- c3
) >> R_SHIFT
;
191 row
[3]= (c0
- c1
) >> R_SHIFT
;
194 void ff_simple_idct84_add(uint8_t *dest
, ptrdiff_t line_size
, int16_t *block
)
198 /* IDCT8 on each line */
200 idctRowCondDC_int16_8bit(block
+ i
*8, 0);
203 /* IDCT4 and store */
205 idct4col_add(dest
+ i
, line_size
, block
+ i
);
209 void ff_simple_idct48_add(uint8_t *dest
, ptrdiff_t line_size
, int16_t *block
)
213 /* IDCT4 on each line */
215 idct4row(block
+ i
*8);
218 /* IDCT8 and store */
220 idctSparseColAdd_int16_8bit(dest
+ i
, line_size
, block
+ i
);
224 void ff_simple_idct44_add(uint8_t *dest
, ptrdiff_t line_size
, int16_t *block
)
228 /* IDCT4 on each line */
230 idct4row(block
+ i
*8);
233 /* IDCT4 and store */
235 idct4col_add(dest
+ i
, line_size
, block
+ i
);
239 void ff_prores_idct_10(int16_t *block
, const int16_t *qmat
)
243 for (i
= 0; i
< 64; i
++)
246 for (i
= 0; i
< 8; i
++)
247 idctRowCondDC_extrashift_10(block
+ i
*8, 2);
249 for (i
= 0; i
< 8; i
++) {
251 idctSparseCol_extrashift_10(block
+ i
);
255 void ff_prores_idct_12(int16_t *block
, const int16_t *qmat
)
259 for (i
= 0; i
< 64; i
++)
262 for (i
= 0; i
< 8; i
++)
263 idctRowCondDC_int16_12bit(block
+ i
*8, 0);
265 for (i
= 0; i
< 8; i
++) {
267 idctSparseCol_int16_12bit(block
+ i
);