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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (view) (download)

1 : Isibaar 3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
2 :    
3 :     /*
4 :     * Disclaimer of Warranty
5 :     *
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 :     *
26 :     */
27 :    
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 :     // function pointer
112 :     fdctFuncPtr fdct;
113 :    
114 :     /*
115 :     * Perform an integer forward DCT on one block of samples.
116 :     */
117 :    
118 :     void fdct_int32(short * const block)
119 :     {
120 :     int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
121 :     int tmp10, tmp11, tmp12, tmp13;
122 :     int z1, z2, z3, z4, z5;
123 :     short *blkptr;
124 :     int *dataptr;
125 :     int data[64];
126 :     int i;
127 :    
128 :     /* Pass 1: process rows. */
129 :     /* Note results are scaled up by sqrt(8) compared to a true DCT; */
130 :     /* furthermore, we scale the results by 2**PASS1_BITS. */
131 :    
132 :     dataptr = data;
133 :     blkptr = block;
134 :     for (i = 0; i < 8; i++)
135 :     {
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] = DESCALE(z1 + tmp13 * FIX_0_765366865, CONST_BITS - PASS1_BITS);
159 :     dataptr[6] = DESCALE(z1 + tmp12 * (-FIX_1_847759065), CONST_BITS - PASS1_BITS);
160 :    
161 :     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
162 :     * cK represents cos(K*pi/16).
163 :     * i0..i3 in the paper are tmp4..tmp7 here.
164 :     */
165 :    
166 :     z1 = tmp4 + tmp7;
167 :     z2 = tmp5 + tmp6;
168 :     z3 = tmp4 + tmp6;
169 :     z4 = tmp5 + tmp7;
170 :     z5 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */
171 :    
172 :     tmp4 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */
173 :     tmp5 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */
174 :     tmp6 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */
175 :     tmp7 *= FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */
176 :     z1 *= -FIX_0_899976223; /* sqrt(2) * (c7-c3) */
177 :     z2 *= -FIX_2_562915447; /* sqrt(2) * (-c1-c3) */
178 :     z3 *= -FIX_1_961570560; /* sqrt(2) * (-c3-c5) */
179 :     z4 *= -FIX_0_390180644; /* sqrt(2) * (c5-c3) */
180 :    
181 :     z3 += z5;
182 :     z4 += z5;
183 :    
184 :     dataptr[7] = DESCALE(tmp4 + z1 + z3, CONST_BITS - PASS1_BITS);
185 :     dataptr[5] = DESCALE(tmp5 + z2 + z4, CONST_BITS - PASS1_BITS);
186 :     dataptr[3] = DESCALE(tmp6 + z2 + z3, CONST_BITS - PASS1_BITS);
187 :     dataptr[1] = DESCALE(tmp7 + z1 + z4, CONST_BITS - PASS1_BITS);
188 :    
189 :     dataptr += 8; /* advance pointer to next row */
190 :     blkptr += 8;
191 :     }
192 :    
193 :     /* Pass 2: process columns.
194 :     * We remove the PASS1_BITS scaling, but leave the results scaled up
195 :     * by an overall factor of 8.
196 :     */
197 :    
198 :     dataptr = data;
199 :     for (i = 0; i < 8; i++)
200 :     {
201 :     tmp0 = dataptr[0] + dataptr[56];
202 :     tmp7 = dataptr[0] - dataptr[56];
203 :     tmp1 = dataptr[8] + dataptr[48];
204 :     tmp6 = dataptr[8] - dataptr[48];
205 :     tmp2 = dataptr[16] + dataptr[40];
206 :     tmp5 = dataptr[16] - dataptr[40];
207 :     tmp3 = dataptr[24] + dataptr[32];
208 :     tmp4 = dataptr[24] - dataptr[32];
209 :    
210 :     /* Even part per LL&M figure 1 --- note that published figure is faulty;
211 :     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
212 :     */
213 :    
214 :     tmp10 = tmp0 + tmp3;
215 :     tmp13 = tmp0 - tmp3;
216 :     tmp11 = tmp1 + tmp2;
217 :     tmp12 = tmp1 - tmp2;
218 :    
219 :     dataptr[0] = DESCALE(tmp10 + tmp11, PASS1_BITS);
220 :     dataptr[32] = DESCALE(tmp10 - tmp11, PASS1_BITS);
221 :    
222 :     z1 = (tmp12 + tmp13) * FIX_0_541196100;
223 :     dataptr[16] = DESCALE(z1 + tmp13 * FIX_0_765366865, CONST_BITS + PASS1_BITS);
224 :     dataptr[48] = DESCALE(z1 + tmp12 * (-FIX_1_847759065), CONST_BITS + PASS1_BITS);
225 :    
226 :     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
227 :     * cK represents cos(K*pi/16).
228 :     * i0..i3 in the paper are tmp4..tmp7 here.
229 :     */
230 :    
231 :     z1 = tmp4 + tmp7;
232 :     z2 = tmp5 + tmp6;
233 :     z3 = tmp4 + tmp6;
234 :     z4 = tmp5 + tmp7;
235 :     z5 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */
236 :    
237 :     tmp4 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */
238 :     tmp5 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */
239 :     tmp6 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */
240 :     tmp7 *= FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */
241 :     z1 *= -FIX_0_899976223; /* sqrt(2) * (c7-c3) */
242 :     z2 *= -FIX_2_562915447; /* sqrt(2) * (-c1-c3) */
243 :     z3 *= -FIX_1_961570560; /* sqrt(2) * (-c3-c5) */
244 :     z4 *= -FIX_0_390180644; /* sqrt(2) * (c5-c3) */
245 :    
246 :     z3 += z5;
247 :     z4 += z5;
248 :    
249 :     dataptr[56] = DESCALE(tmp4 + z1 + z3, CONST_BITS + PASS1_BITS);
250 :     dataptr[40] = DESCALE(tmp5 + z2 + z4, CONST_BITS + PASS1_BITS);
251 :     dataptr[24] = DESCALE(tmp6 + z2 + z3, CONST_BITS + PASS1_BITS);
252 :     dataptr[8] = DESCALE(tmp7 + z1 + z4, CONST_BITS + PASS1_BITS);
253 :    
254 :     dataptr++; /* advance pointer to next column */
255 :     }
256 :     /* descale */
257 :     for (i = 0; i < 64; i++)
258 :     block[i] = (short int) DESCALE(data[i], 3);
259 :     }
260 :    

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