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