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"
31 #include "simple_idct.h"
34 #include "simple_idct_template.c"
38 #include "simple_idct_template.c"
41 #include "simple_idct_template.c"
47 #include "simple_idct_template.c"
53 #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
54 #define C1 C_FIX(0.6532814824)
55 #define C2 C_FIX(0.2705980501)
57 /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
58 and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
59 #define C_SHIFT (4+1+12)
61 static inline void idct4col_put(uint8_t *dest, ptrdiff_t line_size, const int16_t *col)
63 int c0, c1, c2, c3, a0, a1, a2, a3;
69 c0 = ((a0 + a2) * (1 << CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
70 c2 = ((a0 - a2) * (1 << CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
71 c1 = a1 * C1 + a3 * C2;
72 c3 = a1 * C2 - a3 * C1;
73 dest[0] = av_clip_uint8((c0 + c1) >> C_SHIFT);
75 dest[0] = av_clip_uint8((c2 + c3) >> C_SHIFT);
77 dest[0] = av_clip_uint8((c2 - c3) >> C_SHIFT);
79 dest[0] = av_clip_uint8((c0 - c1) >> C_SHIFT);
88 ptr[8 + k] = a0 - a1;\
91 /* only used by DV codec. The input must be interlaced. 128 is added
92 to the pixels before clamping to avoid systematic error
93 (1024*sqrt(2)) offset would be needed otherwise. */
94 /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
95 compensate the extra butterfly stage - I don't have the full DV
97 void ff_simple_idct248_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
116 /* IDCT8 on each line */
118 idctRowCondDC_8(block + i*8, 0);
121 /* IDCT4 and store */
123 idct4col_put(dest + i, 2 * line_size, block + i);
124 idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i);
128 /* 8x4 & 4x8 WMV2 IDCT */
135 #define C_FIX(x) ((int)((x) * M_SQRT2 * (1 << CN_SHIFT) + 0.5))
136 #define C1 C_FIX(0.6532814824)
137 #define C2 C_FIX(0.2705980501)
138 #define C3 C_FIX(0.5)
139 #define C_SHIFT (4+1+12)
140 static inline void idct4col_add(uint8_t *dest, ptrdiff_t line_size, const int16_t *col)
142 int c0, c1, c2, c3, a0, a1, a2, a3;
148 c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
149 c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
150 c1 = a1 * C1 + a3 * C2;
151 c3 = a1 * C2 - a3 * C1;
152 dest[0] = av_clip_uint8(dest[0] + ((c0 + c1) >> C_SHIFT));
154 dest[0] = av_clip_uint8(dest[0] + ((c2 + c3) >> C_SHIFT));
156 dest[0] = av_clip_uint8(dest[0] + ((c2 - c3) >> C_SHIFT));
158 dest[0] = av_clip_uint8(dest[0] + ((c0 - c1) >> C_SHIFT));
162 #define R_FIX(x) ((int)((x) * M_SQRT2 * (1 << RN_SHIFT) + 0.5))
163 #define R1 R_FIX(0.6532814824)
164 #define R2 R_FIX(0.2705980501)
165 #define R3 R_FIX(0.5)
167 static inline void idct4row(int16_t *row)
169 int c0, c1, c2, c3, a0, a1, a2, a3;
175 c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
176 c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
177 c1 = a1 * R1 + a3 * R2;
178 c3 = a1 * R2 - a3 * R1;
179 row[0]= (c0 + c1) >> R_SHIFT;
180 row[1]= (c2 + c3) >> R_SHIFT;
181 row[2]= (c2 - c3) >> R_SHIFT;
182 row[3]= (c0 - c1) >> R_SHIFT;
185 void ff_simple_idct84_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
189 /* IDCT8 on each line */
191 idctRowCondDC_8(block + i*8, 0);
194 /* IDCT4 and store */
196 idct4col_add(dest + i, line_size, block + i);
200 void ff_simple_idct48_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
204 /* IDCT4 on each line */
206 idct4row(block + i*8);
209 /* IDCT8 and store */
211 idctSparseColAdd_8(dest + i, line_size, block + i);
215 void ff_simple_idct44_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
219 /* IDCT4 on each line */
221 idct4row(block + i*8);
224 /* IDCT4 and store */
226 idct4col_add(dest + i, line_size, block + i);
230 void ff_prores_idct(int16_t *block, const int16_t *qmat)
234 for (i = 0; i < 64; i++)
237 for (i = 0; i < 8; i++)
238 idctRowCondDC_extrashift_10(block + i*8, 2);
240 for (i = 0; i < 8; i++) {
242 idctSparseCol_extrashift_10(block + i);