4 { This file contains a slow-but-accurate integer implementation of the
5 forward DCT (Discrete Cosine Transform).
7 A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
8 on each column. Direct algorithms are also available, but they are
9 much more complex and seem not to be any faster when reduced to code.
11 This implementation is based on an algorithm described in
12 C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
13 Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
14 Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
15 The primary algorithm described there uses 11 multiplies and 29 adds.
16 We use their alternate method with 12 multiplies and 32 adds.
17 The advantage of this method is that no data path contains more than one
18 multiplication; this allows a very simple and accurate implementation in
19 scaled fixed-point arithmetic, with a minimal number of shifts. }
21 { Original : jfdctint.c ; Copyright (C) 1991-1996, Thomas G. Lane. }
23 interface
25 {$I imjconfig.inc}
27 uses
28 imjmorecfg,
29 imjinclude,
30 imjutils,
31 imjpeglib,
35 { Perform the forward DCT on one block of samples. }
37 {GLOBAL}
40 implementation
42 { This module is specialized to the case DCTSIZE = 8. }
44 {$ifndef DCTSIZE_IS_8}
46 {$endif}
49 { The poop on this scaling stuff is as follows:
51 Each 1-D DCT step produces outputs which are a factor of sqrt(N)
52 larger than the true DCT outputs. The final outputs are therefore
53 a factor of N larger than desired; since N=8 this can be cured by
54 a simple right shift at the end of the algorithm. The advantage of
55 this arrangement is that we save two multiplications per 1-D DCT,
56 because the y0 and y4 outputs need not be divided by sqrt(N).
57 In the IJG code, this factor of 8 is removed by the quantization step
58 (in jcdctmgr.c), NOT in this module.
60 We have to do addition and subtraction of the integer inputs, which
61 is no problem, and multiplication by fractional constants, which is
62 a problem to do in integer arithmetic. We multiply all the constants
63 by CONST_SCALE and convert them to integer constants (thus retaining
64 CONST_BITS bits of precision in the constants). After doing a
65 multiplication we have to divide the product by CONST_SCALE, with proper
66 rounding, to produce the correct output. This division can be done
67 cheaply as a right shift of CONST_BITS bits. We postpone shifting
68 as long as possible so that partial sums can be added together with
69 full fractional precision.
71 The outputs of the first pass are scaled up by PASS1_BITS bits so that
72 they are represented to better-than-integral precision. These outputs
73 require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
74 with the recommended scaling. (For 12-bit sample data, the intermediate
75 array is INT32 anyway.)
77 To avoid overflow of the 32-bit intermediate results in pass 2, we must
78 have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
79 shows that the values given below are the most effective. }
81 {$ifdef BITS_IN_JSAMPLE_IS_8}
82 const
85 {$else}
86 const
89 {$endif}
91 const
94 const
109 { Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
110 For 8-bit samples with the recommended scaling, all the variable
111 and constant values involved are no more than 16 bits wide, so a
112 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
113 For 12-bit samples, a full 32-bit multiplication will be needed. }
115 {$ifdef BITS_IN_JSAMPLE_IS_8}
117 {MULTIPLY16C16(var,const)}
119 begin
123 {$else}
125 begin
128 {$endif}
130 { Descale and correctly round an INT32 value that's scaled by N bits.
131 We assume RIGHT_SHIFT rounds towards minus infinity, so adding
132 the fudge factor is correct for either sign of X. }
135 var
137 begin
138 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
142 else
144 {$else}
146 {$endif}
150 { Perform the forward DCT on one block of samples. }
152 {GLOBAL}
154 type
157 var
163 {SHIFT_TEMPS}
164 begin
166 { Pass 1: process rows. }
167 { Note results are scaled up by sqrt(8) compared to a true DCT; }
168 { furthermore, we scale the results by 2**PASS1_BITS. }
172 begin
182 { Even part per LL&M figure 1 --- note that published figure is faulty;
183 rotator "sqrt(2)*c1" should be "sqrt(2)*c6". }
199 { Odd part per figure 8 --- note paper omits factor of sqrt(2).
200 cK represents cos(K*pi/16).
201 i0..i3 in the paper are tmp4..tmp7 here. }
229 { Pass 2: process columns.
230 We remove the PASS1_BITS scaling, but leave the results scaled up
231 by an overall factor of 8. }
235 begin
245 { Even part per LL&M figure 1 --- note that published figure is faulty;
246 rotator "sqrt(2)*c1" should be "sqrt(2)*c6". }
262 { Odd part per figure 8 --- note paper omits factor of sqrt(2).
263 cK represents cos(K*pi/16).
264 i0..i3 in the paper are tmp4..tmp7 here. }