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
28 #include "libavutil/intreadwrite.h"
31 #include "simple_idct.h"
34 #include "simple_idct_template.c"
38 #include "simple_idct_template.c"
44 #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
45 #define C1 C_FIX(0.6532814824)
46 #define C2 C_FIX(0.2705980501)
48 /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
49 and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
50 #define C_SHIFT (4+1+12)
52 static inline void idct4col_put(uint8_t *dest, int line_size, const int16_t *col)
54 int c0, c1, c2, c3, a0, a1, a2, a3;
60 c0 = ((a0 + a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
61 c2 = ((a0 - a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
62 c1 = a1 * C1 + a3 * C2;
63 c3 = a1 * C2 - a3 * C1;
64 dest[0] = av_clip_uint8((c0 + c1) >> C_SHIFT);
66 dest[0] = av_clip_uint8((c2 + c3) >> C_SHIFT);
68 dest[0] = av_clip_uint8((c2 - c3) >> C_SHIFT);
70 dest[0] = av_clip_uint8((c0 - c1) >> C_SHIFT);
79 ptr[8 + k] = a0 - a1;\
82 /* only used by DV codec. The input must be interlaced. 128 is added
83 to the pixels before clamping to avoid systematic error
84 (1024*sqrt(2)) offset would be needed otherwise. */
85 /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
86 compensate the extra butterfly stage - I don't have the full DV
88 void ff_simple_idct248_put(uint8_t *dest, int line_size, int16_t *block)
107 /* IDCT8 on each line */
109 idctRowCondDC_8(block + i*8, 0);
112 /* IDCT4 and store */
114 idct4col_put(dest + i, 2 * line_size, block + i);
115 idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i);
119 /* 8x4 & 4x8 WMV2 IDCT */
126 #define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
127 #define C1 C_FIX(0.6532814824)
128 #define C2 C_FIX(0.2705980501)
129 #define C3 C_FIX(0.5)
130 #define C_SHIFT (4+1+12)
131 static inline void idct4col_add(uint8_t *dest, int line_size, const int16_t *col)
133 int c0, c1, c2, c3, a0, a1, a2, a3;
139 c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
140 c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
141 c1 = a1 * C1 + a3 * C2;
142 c3 = a1 * C2 - a3 * C1;
143 dest[0] = av_clip_uint8(dest[0] + ((c0 + c1) >> C_SHIFT));
145 dest[0] = av_clip_uint8(dest[0] + ((c2 + c3) >> C_SHIFT));
147 dest[0] = av_clip_uint8(dest[0] + ((c2 - c3) >> C_SHIFT));
149 dest[0] = av_clip_uint8(dest[0] + ((c0 - c1) >> C_SHIFT));
153 #define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
154 #define R1 R_FIX(0.6532814824)
155 #define R2 R_FIX(0.2705980501)
156 #define R3 R_FIX(0.5)
158 static inline void idct4row(int16_t *row)
160 int c0, c1, c2, c3, a0, a1, a2, a3;
166 c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
167 c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
168 c1 = a1 * R1 + a3 * R2;
169 c3 = a1 * R2 - a3 * R1;
170 row[0]= (c0 + c1) >> R_SHIFT;
171 row[1]= (c2 + c3) >> R_SHIFT;
172 row[2]= (c2 - c3) >> R_SHIFT;
173 row[3]= (c0 - c1) >> R_SHIFT;
176 void ff_simple_idct84_add(uint8_t *dest, int line_size, int16_t *block)
180 /* IDCT8 on each line */
182 idctRowCondDC_8(block + i*8, 0);
185 /* IDCT4 and store */
187 idct4col_add(dest + i, line_size, block + i);
191 void ff_simple_idct48_add(uint8_t *dest, int line_size, int16_t *block)
195 /* IDCT4 on each line */
197 idct4row(block + i*8);
200 /* IDCT8 and store */
202 idctSparseColAdd_8(dest + i, line_size, block + i);
206 void ff_simple_idct44_add(uint8_t *dest, int line_size, int16_t *block)
210 /* IDCT4 on each line */
212 idct4row(block + i*8);
215 /* IDCT4 and store */
217 idct4col_add(dest + i, line_size, block + i);
221 void ff_prores_idct(int16_t *block, const int16_t *qmat)
225 for (i = 0; i < 64; i++)
228 for (i = 0; i < 8; i++)
229 idctRowCondDC_10(block + i*8, 2);
231 for (i = 0; i < 8; i++)
232 idctSparseCol_10(block + i);