[cvs] / xvidcore / src / dct / fdct.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/dct/fdct.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (view) (download)

1 : edgomez 1.6 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
2 :    
3 :     /*
4 :     * Disclaimer of Warranty
5 : Isibaar 1.1 *
6 : edgomez 1.6 * These software programs are available to the user without any license fee or
7 :     * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
8 :     * any and all warranties, whether express, implied, or statuary, including any
9 :     * implied warranties or merchantability or of fitness for a particular
10 :     * purpose. In no event shall the copyright-holder be liable for any
11 :     * incidental, punitive, or consequential damages of any kind whatsoever
12 :     * arising from the use of these programs.
13 :     *
14 :     * This disclaimer of warranty extends to the user of these programs and user's
15 :     * customers, employees, agents, transferees, successors, and assigns.
16 :     *
17 :     * The MPEG Software Simulation Group does not represent or warrant that the
18 :     * programs furnished hereunder are free of infringement of any third-party
19 :     * patents.
20 :     *
21 :     * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
22 :     * are subject to royalty fees to patent holders. Many of these patents are
23 :     * general enough such that they are unavoidable regardless of implementation
24 :     * design.
25 : chl 1.3 *
26 : edgomez 1.6 */
27 : Isibaar 1.1
28 :     /* This routine is a slow-but-accurate integer implementation of the
29 :     * forward DCT (Discrete Cosine Transform). Taken from the IJG software
30 :     *
31 :     * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
32 :     * on each column. Direct algorithms are also available, but they are
33 :     * much more complex and seem not to be any faster when reduced to code.
34 :     *
35 :     * This implementation is based on an algorithm described in
36 :     * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
37 :     * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
38 :     * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
39 :     * The primary algorithm described there uses 11 multiplies and 29 adds.
40 :     * We use their alternate method with 12 multiplies and 32 adds.
41 :     * The advantage of this method is that no data path contains more than one
42 :     * multiplication; this allows a very simple and accurate implementation in
43 :     * scaled fixed-point arithmetic, with a minimal number of shifts.
44 :     *
45 :     * The poop on this scaling stuff is as follows:
46 :     *
47 :     * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
48 :     * larger than the true DCT outputs. The final outputs are therefore
49 :     * a factor of N larger than desired; since N=8 this can be cured by
50 :     * a simple right shift at the end of the algorithm. The advantage of
51 :     * this arrangement is that we save two multiplications per 1-D DCT,
52 :     * because the y0 and y4 outputs need not be divided by sqrt(N).
53 :     * In the IJG code, this factor of 8 is removed by the quantization step
54 :     * (in jcdctmgr.c), here it is removed.
55 :     *
56 :     * We have to do addition and subtraction of the integer inputs, which
57 :     * is no problem, and multiplication by fractional constants, which is
58 :     * a problem to do in integer arithmetic. We multiply all the constants
59 :     * by CONST_SCALE and convert them to integer constants (thus retaining
60 :     * CONST_BITS bits of precision in the constants). After doing a
61 :     * multiplication we have to divide the product by CONST_SCALE, with proper
62 :     * rounding, to produce the correct output. This division can be done
63 :     * cheaply as a right shift of CONST_BITS bits. We postpone shifting
64 :     * as long as possible so that partial sums can be added together with
65 :     * full fractional precision.
66 :     *
67 :     * The outputs of the first pass are scaled up by PASS1_BITS bits so that
68 :     * they are represented to better-than-integral precision. These outputs
69 :     * require 8 + PASS1_BITS + 3 bits; this fits in a 16-bit word
70 :     * with the recommended scaling. (For 12-bit sample data, the intermediate
71 :     * array is INT32 anyway.)
72 :     *
73 :     * To avoid overflow of the 32-bit intermediate results in pass 2, we must
74 :     * have 8 + CONST_BITS + PASS1_BITS <= 26. Error analysis
75 :     * shows that the values given below are the most effective.
76 :     *
77 :     * We can gain a little more speed, with a further compromise in accuracy,
78 :     * by omitting the addition in a descaling shift. This yields an incorrectly
79 :     * rounded result half the time...
80 :     */
81 :    
82 :     #include "fdct.h"
83 :    
84 :     #define USE_ACCURATE_ROUNDING
85 :    
86 :     #define RIGHT_SHIFT(x, shft) ((x) >> (shft))
87 :    
88 :     #ifdef USE_ACCURATE_ROUNDING
89 :     #define ONE ((int) 1)
90 :     #define DESCALE(x, n) RIGHT_SHIFT((x) + (ONE << ((n) - 1)), n)
91 :     #else
92 :     #define DESCALE(x, n) RIGHT_SHIFT(x, n)
93 :     #endif
94 :    
95 :     #define CONST_BITS 13
96 :     #define PASS1_BITS 2
97 :    
98 :     #define FIX_0_298631336 ((int) 2446) /* FIX(0.298631336) */
99 :     #define FIX_0_390180644 ((int) 3196) /* FIX(0.390180644) */
100 :     #define FIX_0_541196100 ((int) 4433) /* FIX(0.541196100) */
101 :     #define FIX_0_765366865 ((int) 6270) /* FIX(0.765366865) */
102 :     #define FIX_0_899976223 ((int) 7373) /* FIX(0.899976223) */
103 :     #define FIX_1_175875602 ((int) 9633) /* FIX(1.175875602) */
104 :     #define FIX_1_501321110 ((int) 12299) /* FIX(1.501321110) */
105 :     #define FIX_1_847759065 ((int) 15137) /* FIX(1.847759065) */
106 :     #define FIX_1_961570560 ((int) 16069) /* FIX(1.961570560) */
107 :     #define FIX_2_053119869 ((int) 16819) /* FIX(2.053119869) */
108 :     #define FIX_2_562915447 ((int) 20995) /* FIX(2.562915447) */
109 :     #define FIX_3_072711026 ((int) 25172) /* FIX(3.072711026) */
110 :    
111 : edgomez 1.6 // function pointer
112 : Isibaar 1.1 fdctFuncPtr fdct;
113 :    
114 :     /*
115 :     * Perform an integer forward DCT on one block of samples.
116 :     */
117 :    
118 : edgomez 1.2 void
119 :     fdct_int32(short *const block)
120 : Isibaar 1.1 {
121 : edgomez 1.2 int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
122 :     int tmp10, tmp11, tmp12, tmp13;
123 :     int z1, z2, z3, z4, z5;
124 :     short *blkptr;
125 :     int *dataptr;
126 :     int data[64];
127 :     int i;
128 :    
129 :     /* Pass 1: process rows. */
130 :     /* Note results are scaled up by sqrt(8) compared to a true DCT; */
131 :     /* furthermore, we scale the results by 2**PASS1_BITS. */
132 :    
133 :     dataptr = data;
134 :     blkptr = block;
135 :     for (i = 0; i < 8; i++) {
136 :     tmp0 = blkptr[0] + blkptr[7];
137 :     tmp7 = blkptr[0] - blkptr[7];
138 :     tmp1 = blkptr[1] + blkptr[6];
139 :     tmp6 = blkptr[1] - blkptr[6];
140 :     tmp2 = blkptr[2] + blkptr[5];
141 :     tmp5 = blkptr[2] - blkptr[5];
142 :     tmp3 = blkptr[3] + blkptr[4];
143 :     tmp4 = blkptr[3] - blkptr[4];
144 :    
145 :     /* Even part per LL&M figure 1 --- note that published figure is faulty;
146 :     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
147 :     */
148 :    
149 :     tmp10 = tmp0 + tmp3;
150 :     tmp13 = tmp0 - tmp3;
151 :     tmp11 = tmp1 + tmp2;
152 :     tmp12 = tmp1 - tmp2;
153 :    
154 :     dataptr[0] = (tmp10 + tmp11) << PASS1_BITS;
155 :     dataptr[4] = (tmp10 - tmp11) << PASS1_BITS;
156 :    
157 :     z1 = (tmp12 + tmp13) * FIX_0_541196100;
158 :     dataptr[2] =
159 :     DESCALE(z1 + tmp13 * FIX_0_765366865, CONST_BITS - PASS1_BITS);
160 :     dataptr[6] =
161 :     DESCALE(z1 + tmp12 * (-FIX_1_847759065), CONST_BITS - PASS1_BITS);
162 :    
163 :     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
164 :     * cK represents cos(K*pi/16).
165 :     * i0..i3 in the paper are tmp4..tmp7 here.
166 :     */
167 :    
168 :     z1 = tmp4 + tmp7;
169 :     z2 = tmp5 + tmp6;
170 :     z3 = tmp4 + tmp6;
171 :     z4 = tmp5 + tmp7;
172 :     z5 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */
173 :    
174 :     tmp4 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */
175 :     tmp5 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */
176 :     tmp6 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */
177 :     tmp7 *= FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */
178 :     z1 *= -FIX_0_899976223; /* sqrt(2) * (c7-c3) */
179 :     z2 *= -FIX_2_562915447; /* sqrt(2) * (-c1-c3) */
180 :     z3 *= -FIX_1_961570560; /* sqrt(2) * (-c3-c5) */
181 :     z4 *= -FIX_0_390180644; /* sqrt(2) * (c5-c3) */
182 :    
183 :     z3 += z5;
184 :     z4 += z5;
185 :    
186 :     dataptr[7] = DESCALE(tmp4 + z1 + z3, CONST_BITS - PASS1_BITS);
187 :     dataptr[5] = DESCALE(tmp5 + z2 + z4, CONST_BITS - PASS1_BITS);
188 :     dataptr[3] = DESCALE(tmp6 + z2 + z3, CONST_BITS - PASS1_BITS);
189 :     dataptr[1] = DESCALE(tmp7 + z1 + z4, CONST_BITS - PASS1_BITS);
190 :    
191 :     dataptr += 8; /* advance pointer to next row */
192 :     blkptr += 8;
193 :     }
194 :    
195 :     /* Pass 2: process columns.
196 :     * We remove the PASS1_BITS scaling, but leave the results scaled up
197 :     * by an overall factor of 8.
198 :     */
199 :    
200 :     dataptr = data;
201 :     for (i = 0; i < 8; i++) {
202 :     tmp0 = dataptr[0] + dataptr[56];
203 :     tmp7 = dataptr[0] - dataptr[56];
204 :     tmp1 = dataptr[8] + dataptr[48];
205 :     tmp6 = dataptr[8] - dataptr[48];
206 :     tmp2 = dataptr[16] + dataptr[40];
207 :     tmp5 = dataptr[16] - dataptr[40];
208 :     tmp3 = dataptr[24] + dataptr[32];
209 :     tmp4 = dataptr[24] - dataptr[32];
210 :    
211 :     /* Even part per LL&M figure 1 --- note that published figure is faulty;
212 :     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
213 :     */
214 :    
215 :     tmp10 = tmp0 + tmp3;
216 :     tmp13 = tmp0 - tmp3;
217 :     tmp11 = tmp1 + tmp2;
218 :     tmp12 = tmp1 - tmp2;
219 :    
220 :     dataptr[0] = DESCALE(tmp10 + tmp11, PASS1_BITS);
221 :     dataptr[32] = DESCALE(tmp10 - tmp11, PASS1_BITS);
222 :    
223 :     z1 = (tmp12 + tmp13) * FIX_0_541196100;
224 :     dataptr[16] =
225 :     DESCALE(z1 + tmp13 * FIX_0_765366865, CONST_BITS + PASS1_BITS);
226 :     dataptr[48] =
227 :     DESCALE(z1 + tmp12 * (-FIX_1_847759065), CONST_BITS + PASS1_BITS);
228 :    
229 :     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
230 :     * cK represents cos(K*pi/16).
231 :     * i0..i3 in the paper are tmp4..tmp7 here.
232 :     */
233 :    
234 :     z1 = tmp4 + tmp7;
235 :     z2 = tmp5 + tmp6;
236 :     z3 = tmp4 + tmp6;
237 :     z4 = tmp5 + tmp7;
238 :     z5 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */
239 :    
240 :     tmp4 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */
241 :     tmp5 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */
242 :     tmp6 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */
243 :     tmp7 *= FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */
244 :     z1 *= -FIX_0_899976223; /* sqrt(2) * (c7-c3) */
245 :     z2 *= -FIX_2_562915447; /* sqrt(2) * (-c1-c3) */
246 :     z3 *= -FIX_1_961570560; /* sqrt(2) * (-c3-c5) */
247 :     z4 *= -FIX_0_390180644; /* sqrt(2) * (c5-c3) */
248 :    
249 :     z3 += z5;
250 :     z4 += z5;
251 :    
252 :     dataptr[56] = DESCALE(tmp4 + z1 + z3, CONST_BITS + PASS1_BITS);
253 :     dataptr[40] = DESCALE(tmp5 + z2 + z4, CONST_BITS + PASS1_BITS);
254 :     dataptr[24] = DESCALE(tmp6 + z2 + z3, CONST_BITS + PASS1_BITS);
255 :     dataptr[8] = DESCALE(tmp7 + z1 + z4, CONST_BITS + PASS1_BITS);
256 :    
257 :     dataptr++; /* advance pointer to next column */
258 :     }
259 :     /* descale */
260 :     for (i = 0; i < 64; i++)
261 :     block[i] = (short int) DESCALE(data[i], 3);
262 : Isibaar 1.1 }

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