FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
simple_idct.c
Go to the documentation of this file.
1 /*
2  * Simple IDCT
3  *
4  * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 /**
24  * @file
25  * simpleidct in C.
26  */
27 
28 #include "libavutil/intreadwrite.h"
29 #include "avcodec.h"
30 #include "mathops.h"
31 #include "simple_idct.h"
32 
33 #define BIT_DEPTH 8
34 #include "simple_idct_template.c"
35 #undef BIT_DEPTH
36 
37 #define BIT_DEPTH 10
38 #include "simple_idct_template.c"
39 #undef BIT_DEPTH
40 
41 #define BIT_DEPTH 12
42 #include "simple_idct_template.c"
43 #undef BIT_DEPTH
44 
45 /* 2x4x8 idct */
46 
47 #define CN_SHIFT 12
48 #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
49 #define C1 C_FIX(0.6532814824)
50 #define C2 C_FIX(0.2705980501)
51 
52 /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
53  and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
54 #define C_SHIFT (4+1+12)
55 
56 static inline void idct4col_put(uint8_t *dest, int line_size, const int16_t *col)
57 {
58  int c0, c1, c2, c3, a0, a1, a2, a3;
59 
60  a0 = col[8*0];
61  a1 = col[8*2];
62  a2 = col[8*4];
63  a3 = col[8*6];
64  c0 = ((a0 + a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
65  c2 = ((a0 - a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
66  c1 = a1 * C1 + a3 * C2;
67  c3 = a1 * C2 - a3 * C1;
68  dest[0] = av_clip_uint8((c0 + c1) >> C_SHIFT);
69  dest += line_size;
70  dest[0] = av_clip_uint8((c2 + c3) >> C_SHIFT);
71  dest += line_size;
72  dest[0] = av_clip_uint8((c2 - c3) >> C_SHIFT);
73  dest += line_size;
74  dest[0] = av_clip_uint8((c0 - c1) >> C_SHIFT);
75 }
76 
77 #define BF(k) \
78 {\
79  int a0, a1;\
80  a0 = ptr[k];\
81  a1 = ptr[8 + k];\
82  ptr[k] = a0 + a1;\
83  ptr[8 + k] = a0 - a1;\
84 }
85 
86 /* only used by DV codec. The input must be interlaced. 128 is added
87  to the pixels before clamping to avoid systematic error
88  (1024*sqrt(2)) offset would be needed otherwise. */
89 /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
90  compensate the extra butterfly stage - I don't have the full DV
91  specification */
92 void ff_simple_idct248_put(uint8_t *dest, int line_size, int16_t *block)
93 {
94  int i;
95  int16_t *ptr;
96 
97  /* butterfly */
98  ptr = block;
99  for(i=0;i<4;i++) {
100  BF(0);
101  BF(1);
102  BF(2);
103  BF(3);
104  BF(4);
105  BF(5);
106  BF(6);
107  BF(7);
108  ptr += 2 * 8;
109  }
110 
111  /* IDCT8 on each line */
112  for(i=0; i<8; i++) {
113  idctRowCondDC_8(block + i*8, 0);
114  }
115 
116  /* IDCT4 and store */
117  for(i=0;i<8;i++) {
118  idct4col_put(dest + i, 2 * line_size, block + i);
119  idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i);
120  }
121 }
122 
123 /* 8x4 & 4x8 WMV2 IDCT */
124 #undef CN_SHIFT
125 #undef C_SHIFT
126 #undef C_FIX
127 #undef C1
128 #undef C2
129 #define CN_SHIFT 12
130 #define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
131 #define C1 C_FIX(0.6532814824)
132 #define C2 C_FIX(0.2705980501)
133 #define C3 C_FIX(0.5)
134 #define C_SHIFT (4+1+12)
135 static inline void idct4col_add(uint8_t *dest, int line_size, const int16_t *col)
136 {
137  int c0, c1, c2, c3, a0, a1, a2, a3;
138 
139  a0 = col[8*0];
140  a1 = col[8*1];
141  a2 = col[8*2];
142  a3 = col[8*3];
143  c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
144  c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
145  c1 = a1 * C1 + a3 * C2;
146  c3 = a1 * C2 - a3 * C1;
147  dest[0] = av_clip_uint8(dest[0] + ((c0 + c1) >> C_SHIFT));
148  dest += line_size;
149  dest[0] = av_clip_uint8(dest[0] + ((c2 + c3) >> C_SHIFT));
150  dest += line_size;
151  dest[0] = av_clip_uint8(dest[0] + ((c2 - c3) >> C_SHIFT));
152  dest += line_size;
153  dest[0] = av_clip_uint8(dest[0] + ((c0 - c1) >> C_SHIFT));
154 }
155 
156 #define RN_SHIFT 15
157 #define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
158 #define R1 R_FIX(0.6532814824)
159 #define R2 R_FIX(0.2705980501)
160 #define R3 R_FIX(0.5)
161 #define R_SHIFT 11
162 static inline void idct4row(int16_t *row)
163 {
164  int c0, c1, c2, c3, a0, a1, a2, a3;
165 
166  a0 = row[0];
167  a1 = row[1];
168  a2 = row[2];
169  a3 = row[3];
170  c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
171  c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
172  c1 = a1 * R1 + a3 * R2;
173  c3 = a1 * R2 - a3 * R1;
174  row[0]= (c0 + c1) >> R_SHIFT;
175  row[1]= (c2 + c3) >> R_SHIFT;
176  row[2]= (c2 - c3) >> R_SHIFT;
177  row[3]= (c0 - c1) >> R_SHIFT;
178 }
179 
180 void ff_simple_idct84_add(uint8_t *dest, int line_size, int16_t *block)
181 {
182  int i;
183 
184  /* IDCT8 on each line */
185  for(i=0; i<4; i++) {
186  idctRowCondDC_8(block + i*8, 0);
187  }
188 
189  /* IDCT4 and store */
190  for(i=0;i<8;i++) {
191  idct4col_add(dest + i, line_size, block + i);
192  }
193 }
194 
195 void ff_simple_idct48_add(uint8_t *dest, int line_size, int16_t *block)
196 {
197  int i;
198 
199  /* IDCT4 on each line */
200  for(i=0; i<8; i++) {
201  idct4row(block + i*8);
202  }
203 
204  /* IDCT8 and store */
205  for(i=0; i<4; i++){
206  idctSparseColAdd_8(dest + i, line_size, block + i);
207  }
208 }
209 
210 void ff_simple_idct44_add(uint8_t *dest, int line_size, int16_t *block)
211 {
212  int i;
213 
214  /* IDCT4 on each line */
215  for(i=0; i<4; i++) {
216  idct4row(block + i*8);
217  }
218 
219  /* IDCT4 and store */
220  for(i=0; i<4; i++){
221  idct4col_add(dest + i, line_size, block + i);
222  }
223 }
224 
225 void ff_prores_idct(int16_t *block, const int16_t *qmat)
226 {
227  int i;
228 
229  for (i = 0; i < 64; i++)
230  block[i] *= qmat[i];
231 
232  for (i = 0; i < 8; i++)
233  idctRowCondDC_10(block + i*8, 2);
234 
235  for (i = 0; i < 8; i++) {
236  block[i] += 8192;
237  idctSparseCol_10(block + i);
238  }
239 }