3 { This file contains a fast, not so accurate integer implementation of the
4 forward DCT (Discrete Cosine Transform).
6 A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
7 on each column. Direct algorithms are also available, but they are
8 much more complex and seem not to be any faster when reduced to code.
10 This implementation is based on Arai, Agui, and Nakajima's algorithm for
11 scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
12 Japanese, but the algorithm is described in the Pennebaker & Mitchell
13 JPEG textbook (see REFERENCES section in file README). The following code
14 is based directly on figure 4-8 in P&M.
15 While an 8-point DCT cannot be done in less than 11 multiplies, it is
16 possible to arrange the computation so that many of the multiplies are
17 simple scalings of the final outputs. These multiplies can then be
18 folded into the multiplications or divisions by the JPEG quantization
19 table entries. The AA&N method leaves only 5 multiplies and 29 adds
20 to be done in the DCT itself.
21 The primary disadvantage of this method is that with fixed-point math,
22 accuracy is lost due to imprecise representation of the scaled
23 quantization values. The smaller the quantization table entry, the less
24 precise the scaled value, so this implementation does worse with high-
25 quality-setting files than with low-quality ones. }
27 { Original: jfdctfst.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
30 interface
32 {$I imjconfig.inc}
34 uses
35 imjmorecfg,
36 imjinclude,
37 imjpeglib,
41 { Perform the forward DCT on one block of samples. }
43 {GLOBAL}
46 implementation
48 { This module is specialized to the case DCTSIZE = 8. }
50 {$ifndef DCTSIZE_IS_8}
52 {$endif}
55 { Scaling decisions are generally the same as in the LL&M algorithm;
56 see jfdctint.c for more details. However, we choose to descale
57 (right shift) multiplication products as soon as they are formed,
58 rather than carrying additional fractional bits into subsequent additions.
59 This compromises accuracy slightly, but it lets us save a few shifts.
60 More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
61 everywhere except in the multiplications proper; this saves a good deal
62 of work on 16-bit-int machines.
64 Again to save a few shifts, the intermediate results between pass 1 and
65 pass 2 are not upscaled, but are represented only to integral precision.
67 A final compromise is to represent the multiplicative constants to only
68 8 fractional bits, rather than 13. This saves some shifting work on some
69 machines, and may also reduce the cost of multiplication (since there
70 are fewer one-bits in the constants). }
72 const
74 const
78 const
84 { Descale and correctly round an INT32 value that's scaled by N bits.
85 We assume RIGHT_SHIFT rounds towards minus infinity, so adding
86 the fudge factor is correct for either sign of X. }
89 var
91 begin
92 { We can gain a little more speed, with a further compromise in accuracy,
93 by omitting the addition in a descaling shift. This yields an incorrectly
94 rounded result half the time... }
95 {$ifndef USE_ACCURATE_ROUNDING}
97 {$else}
99 {$endif}
101 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
104 else
105 {$endif}
109 { Multiply a DCTELEM variable by an INT32 constant, and immediately
110 descale to yield a DCTELEM result. }
114 begin
119 { Perform the forward DCT on one block of samples. }
121 {GLOBAL}
123 type
126 var
132 {SHIFT_TEMPS}
133 begin
134 { Pass 1: process rows. }
138 begin
148 { Even part }
162 { Odd part }
168 { The rotator is modified from fig 4-8 to avoid extra negations. }
185 { Pass 2: process columns. }
189 begin
199 { Even part }
213 { Odd part }
219 { The rotator is modified from fig 4-8 to avoid extra negations. }