[svn] / branches / dev-api-4 / xvidcore / src / dct / fdct.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/dct/fdct.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 461 - (view) (download)
Original Path: trunk/xvidcore/src/dct/fdct.c

1 : chl 461 /*****************************************************************************
2 : Isibaar 3 *
3 : chl 461 * XVID MPEG-4 VIDEO CODEC
4 :     * - fast disrete cosine transformation - integer C version
5 : Isibaar 3 *
6 : chl 461 * These routines are from Independent JPEG Group's free JPEG software
7 :     * Copyright (C) 1991-1998, Thomas G. Lane (see the file README.IJG)
8 : Isibaar 3 *
9 : chl 461 * This program is an implementation of a part of one or more MPEG-4
10 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
11 :     * to use this software module in hardware or software products are
12 :     * advised that its use may infringe existing patents or copyrights, and
13 :     * any such use would be at such party's own risk. The original
14 :     * developer of this software module and his/her company, and subsequent
15 :     * editors and their companies, will have no liability for use of this
16 :     * software or modifications or derivatives thereof.
17 : Isibaar 3 *
18 : chl 461 * This program is free software; you can redistribute it and/or modify
19 :     * it under the terms of the GNU General Public License as published by
20 :     * the Free Software Foundation; either version 2 of the License, or
21 :     * (at your option) any later version.
22 : Isibaar 3 *
23 : chl 461 * This program is distributed in the hope that it will be useful,
24 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 :     * GNU General Public License for more details.
27 :     *
28 :     * You should have received a copy of the GNU General Public License
29 :     * along with this program; if not, write to the Free Software
30 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 :     *
32 :     *************************************************************************/
33 : Isibaar 3
34 :     /* This routine is a slow-but-accurate integer implementation of the
35 :     * forward DCT (Discrete Cosine Transform). Taken from the IJG software
36 :     *
37 :     * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
38 :     * on each column. Direct algorithms are also available, but they are
39 :     * much more complex and seem not to be any faster when reduced to code.
40 :     *
41 :     * This implementation is based on an algorithm described in
42 :     * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
43 :     * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
44 :     * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
45 :     * The primary algorithm described there uses 11 multiplies and 29 adds.
46 :     * We use their alternate method with 12 multiplies and 32 adds.
47 :     * The advantage of this method is that no data path contains more than one
48 :     * multiplication; this allows a very simple and accurate implementation in
49 :     * scaled fixed-point arithmetic, with a minimal number of shifts.
50 :     *
51 :     * The poop on this scaling stuff is as follows:
52 :     *
53 :     * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
54 :     * larger than the true DCT outputs. The final outputs are therefore
55 :     * a factor of N larger than desired; since N=8 this can be cured by
56 :     * a simple right shift at the end of the algorithm. The advantage of
57 :     * this arrangement is that we save two multiplications per 1-D DCT,
58 :     * because the y0 and y4 outputs need not be divided by sqrt(N).
59 :     * In the IJG code, this factor of 8 is removed by the quantization step
60 :     * (in jcdctmgr.c), here it is removed.
61 :     *
62 :     * We have to do addition and subtraction of the integer inputs, which
63 :     * is no problem, and multiplication by fractional constants, which is
64 :     * a problem to do in integer arithmetic. We multiply all the constants
65 :     * by CONST_SCALE and convert them to integer constants (thus retaining
66 :     * CONST_BITS bits of precision in the constants). After doing a
67 :     * multiplication we have to divide the product by CONST_SCALE, with proper
68 :     * rounding, to produce the correct output. This division can be done
69 :     * cheaply as a right shift of CONST_BITS bits. We postpone shifting
70 :     * as long as possible so that partial sums can be added together with
71 :     * full fractional precision.
72 :     *
73 :     * The outputs of the first pass are scaled up by PASS1_BITS bits so that
74 :     * they are represented to better-than-integral precision. These outputs
75 :     * require 8 + PASS1_BITS + 3 bits; this fits in a 16-bit word
76 :     * with the recommended scaling. (For 12-bit sample data, the intermediate
77 :     * array is INT32 anyway.)
78 :     *
79 :     * To avoid overflow of the 32-bit intermediate results in pass 2, we must
80 :     * have 8 + CONST_BITS + PASS1_BITS <= 26. Error analysis
81 :     * shows that the values given below are the most effective.
82 :     *
83 :     * We can gain a little more speed, with a further compromise in accuracy,
84 :     * by omitting the addition in a descaling shift. This yields an incorrectly
85 :     * rounded result half the time...
86 :     */
87 :    
88 :     #include "fdct.h"
89 :    
90 :     #define USE_ACCURATE_ROUNDING
91 :    
92 :     #define RIGHT_SHIFT(x, shft) ((x) >> (shft))
93 :    
94 :     #ifdef USE_ACCURATE_ROUNDING
95 :     #define ONE ((int) 1)
96 :     #define DESCALE(x, n) RIGHT_SHIFT((x) + (ONE << ((n) - 1)), n)
97 :     #else
98 :     #define DESCALE(x, n) RIGHT_SHIFT(x, n)
99 :     #endif
100 :    
101 :     #define CONST_BITS 13
102 :     #define PASS1_BITS 2
103 :    
104 :     #define FIX_0_298631336 ((int) 2446) /* FIX(0.298631336) */
105 :     #define FIX_0_390180644 ((int) 3196) /* FIX(0.390180644) */
106 :     #define FIX_0_541196100 ((int) 4433) /* FIX(0.541196100) */
107 :     #define FIX_0_765366865 ((int) 6270) /* FIX(0.765366865) */
108 :     #define FIX_0_899976223 ((int) 7373) /* FIX(0.899976223) */
109 :     #define FIX_1_175875602 ((int) 9633) /* FIX(1.175875602) */
110 :     #define FIX_1_501321110 ((int) 12299) /* FIX(1.501321110) */
111 :     #define FIX_1_847759065 ((int) 15137) /* FIX(1.847759065) */
112 :     #define FIX_1_961570560 ((int) 16069) /* FIX(1.961570560) */
113 :     #define FIX_2_053119869 ((int) 16819) /* FIX(2.053119869) */
114 :     #define FIX_2_562915447 ((int) 20995) /* FIX(2.562915447) */
115 :     #define FIX_3_072711026 ((int) 25172) /* FIX(3.072711026) */
116 :    
117 :     // function pointer
118 :     fdctFuncPtr fdct;
119 :    
120 :     /*
121 :     * Perform an integer forward DCT on one block of samples.
122 :     */
123 :    
124 : edgomez 195 void
125 :     fdct_int32(short *const block)
126 : Isibaar 3 {
127 : edgomez 195 int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
128 :     int tmp10, tmp11, tmp12, tmp13;
129 :     int z1, z2, z3, z4, z5;
130 :     short *blkptr;
131 :     int *dataptr;
132 :     int data[64];
133 :     int i;
134 : Isibaar 3
135 : edgomez 195 /* Pass 1: process rows. */
136 :     /* Note results are scaled up by sqrt(8) compared to a true DCT; */
137 :     /* furthermore, we scale the results by 2**PASS1_BITS. */
138 : Isibaar 3
139 : edgomez 195 dataptr = data;
140 :     blkptr = block;
141 :     for (i = 0; i < 8; i++) {
142 :     tmp0 = blkptr[0] + blkptr[7];
143 :     tmp7 = blkptr[0] - blkptr[7];
144 :     tmp1 = blkptr[1] + blkptr[6];
145 :     tmp6 = blkptr[1] - blkptr[6];
146 :     tmp2 = blkptr[2] + blkptr[5];
147 :     tmp5 = blkptr[2] - blkptr[5];
148 :     tmp3 = blkptr[3] + blkptr[4];
149 :     tmp4 = blkptr[3] - blkptr[4];
150 : Isibaar 3
151 : edgomez 195 /* Even part per LL&M figure 1 --- note that published figure is faulty;
152 :     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
153 :     */
154 : Isibaar 3
155 : edgomez 195 tmp10 = tmp0 + tmp3;
156 :     tmp13 = tmp0 - tmp3;
157 :     tmp11 = tmp1 + tmp2;
158 :     tmp12 = tmp1 - tmp2;
159 : Isibaar 3
160 : edgomez 195 dataptr[0] = (tmp10 + tmp11) << PASS1_BITS;
161 :     dataptr[4] = (tmp10 - tmp11) << PASS1_BITS;
162 : Isibaar 3
163 : edgomez 195 z1 = (tmp12 + tmp13) * FIX_0_541196100;
164 :     dataptr[2] =
165 :     DESCALE(z1 + tmp13 * FIX_0_765366865, CONST_BITS - PASS1_BITS);
166 :     dataptr[6] =
167 :     DESCALE(z1 + tmp12 * (-FIX_1_847759065), CONST_BITS - PASS1_BITS);
168 : Isibaar 3
169 : edgomez 195 /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
170 :     * cK represents cos(K*pi/16).
171 :     * i0..i3 in the paper are tmp4..tmp7 here.
172 :     */
173 : Isibaar 3
174 : edgomez 195 z1 = tmp4 + tmp7;
175 :     z2 = tmp5 + tmp6;
176 :     z3 = tmp4 + tmp6;
177 :     z4 = tmp5 + tmp7;
178 :     z5 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */
179 : Isibaar 3
180 : edgomez 195 tmp4 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */
181 :     tmp5 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */
182 :     tmp6 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */
183 :     tmp7 *= FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */
184 :     z1 *= -FIX_0_899976223; /* sqrt(2) * (c7-c3) */
185 :     z2 *= -FIX_2_562915447; /* sqrt(2) * (-c1-c3) */
186 :     z3 *= -FIX_1_961570560; /* sqrt(2) * (-c3-c5) */
187 :     z4 *= -FIX_0_390180644; /* sqrt(2) * (c5-c3) */
188 : Isibaar 3
189 : edgomez 195 z3 += z5;
190 :     z4 += z5;
191 : Isibaar 3
192 : edgomez 195 dataptr[7] = DESCALE(tmp4 + z1 + z3, CONST_BITS - PASS1_BITS);
193 :     dataptr[5] = DESCALE(tmp5 + z2 + z4, CONST_BITS - PASS1_BITS);
194 :     dataptr[3] = DESCALE(tmp6 + z2 + z3, CONST_BITS - PASS1_BITS);
195 :     dataptr[1] = DESCALE(tmp7 + z1 + z4, CONST_BITS - PASS1_BITS);
196 : Isibaar 3
197 : edgomez 195 dataptr += 8; /* advance pointer to next row */
198 :     blkptr += 8;
199 :     }
200 : Isibaar 3
201 : edgomez 195 /* Pass 2: process columns.
202 :     * We remove the PASS1_BITS scaling, but leave the results scaled up
203 :     * by an overall factor of 8.
204 :     */
205 : Isibaar 3
206 : edgomez 195 dataptr = data;
207 :     for (i = 0; i < 8; i++) {
208 :     tmp0 = dataptr[0] + dataptr[56];
209 :     tmp7 = dataptr[0] - dataptr[56];
210 :     tmp1 = dataptr[8] + dataptr[48];
211 :     tmp6 = dataptr[8] - dataptr[48];
212 :     tmp2 = dataptr[16] + dataptr[40];
213 :     tmp5 = dataptr[16] - dataptr[40];
214 :     tmp3 = dataptr[24] + dataptr[32];
215 :     tmp4 = dataptr[24] - dataptr[32];
216 : Isibaar 3
217 : edgomez 195 /* Even part per LL&M figure 1 --- note that published figure is faulty;
218 :     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
219 :     */
220 : Isibaar 3
221 : edgomez 195 tmp10 = tmp0 + tmp3;
222 :     tmp13 = tmp0 - tmp3;
223 :     tmp11 = tmp1 + tmp2;
224 :     tmp12 = tmp1 - tmp2;
225 : Isibaar 3
226 : edgomez 195 dataptr[0] = DESCALE(tmp10 + tmp11, PASS1_BITS);
227 :     dataptr[32] = DESCALE(tmp10 - tmp11, PASS1_BITS);
228 : Isibaar 3
229 : edgomez 195 z1 = (tmp12 + tmp13) * FIX_0_541196100;
230 :     dataptr[16] =
231 :     DESCALE(z1 + tmp13 * FIX_0_765366865, CONST_BITS + PASS1_BITS);
232 :     dataptr[48] =
233 :     DESCALE(z1 + tmp12 * (-FIX_1_847759065), CONST_BITS + PASS1_BITS);
234 : Isibaar 3
235 : edgomez 195 /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
236 :     * cK represents cos(K*pi/16).
237 :     * i0..i3 in the paper are tmp4..tmp7 here.
238 :     */
239 : Isibaar 3
240 : edgomez 195 z1 = tmp4 + tmp7;
241 :     z2 = tmp5 + tmp6;
242 :     z3 = tmp4 + tmp6;
243 :     z4 = tmp5 + tmp7;
244 :     z5 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */
245 : Isibaar 3
246 : edgomez 195 tmp4 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */
247 :     tmp5 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */
248 :     tmp6 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */
249 :     tmp7 *= FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */
250 :     z1 *= -FIX_0_899976223; /* sqrt(2) * (c7-c3) */
251 :     z2 *= -FIX_2_562915447; /* sqrt(2) * (-c1-c3) */
252 :     z3 *= -FIX_1_961570560; /* sqrt(2) * (-c3-c5) */
253 :     z4 *= -FIX_0_390180644; /* sqrt(2) * (c5-c3) */
254 : Isibaar 3
255 : edgomez 195 z3 += z5;
256 :     z4 += z5;
257 : Isibaar 3
258 : edgomez 195 dataptr[56] = DESCALE(tmp4 + z1 + z3, CONST_BITS + PASS1_BITS);
259 :     dataptr[40] = DESCALE(tmp5 + z2 + z4, CONST_BITS + PASS1_BITS);
260 :     dataptr[24] = DESCALE(tmp6 + z2 + z3, CONST_BITS + PASS1_BITS);
261 :     dataptr[8] = DESCALE(tmp7 + z1 + z4, CONST_BITS + PASS1_BITS);
262 : Isibaar 3
263 : edgomez 195 dataptr++; /* advance pointer to next column */
264 :     }
265 :     /* descale */
266 :     for (i = 0; i < 64; i++)
267 :     block[i] = (short int) DESCALE(data[i], 3);
268 : Isibaar 3 }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4