1 /*
2  * jdct.h
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1994-1996, Thomas G. Lane.
6  * It was modified by The libjpeg-turbo Project to include only code relevant
7  * to libjpeg-turbo.
8  * For conditions of distribution and use, see the accompanying README file.
9  *
10  * This include file contains common declarations for the forward and
11  * inverse DCT modules.  These declarations are private to the DCT managers
12  * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms.
13  * The individual DCT algorithms are kept in separate files to ease
14  * machine-dependent tuning (e.g., assembly coding).
15  */
16 
17 
18 /*
19  * A forward DCT routine is given a pointer to a work area of type DCTELEM[];
20  * the DCT is to be performed in-place in that buffer.  Type DCTELEM is int
21  * for 8-bit samples, INT32 for 12-bit samples.  (NOTE: Floating-point DCT
22  * implementations use an array of type FAST_FLOAT, instead.)
23  * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
24  * The DCT outputs are returned scaled up by a factor of 8; they therefore
25  * have a range of +-8K for 8-bit data, +-128K for 12-bit data.  This
26  * convention improves accuracy in integer implementations and saves some
27  * work in floating-point ones.
28  * Quantization of the output coefficients is done by jcdctmgr.c. This
29  * step requires an unsigned type and also one with twice the bits.
30  */
31 
32 #if BITS_IN_JSAMPLE == 8
33 #ifndef WITH_SIMD
34 typedef int DCTELEM;            /* 16 or 32 bits is fine */
35 typedef unsigned int UDCTELEM;
36 typedef unsigned long long UDCTELEM2;
37 #else
38 typedef short DCTELEM;  /* prefer 16 bit with SIMD for parellelism */
39 typedef unsigned short UDCTELEM;
40 typedef unsigned int UDCTELEM2;
41 #endif
42 #else
43 typedef INT32 DCTELEM;          /* must have 32 bits */
44 typedef unsigned long long UDCTELEM2;
45 #endif
46 
47 
48 /*
49  * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer
50  * to an output sample array.  The routine must dequantize the input data as
51  * well as perform the IDCT; for dequantization, it uses the multiplier table
52  * pointed to by compptr->dct_table.  The output data is to be placed into the
53  * sample array starting at a specified column.  (Any row offset needed will
54  * be applied to the array pointer before it is passed to the IDCT code.)
55  * Note that the number of samples emitted by the IDCT routine is
56  * DCT_scaled_size * DCT_scaled_size.
57  */
58 
59 /* typedef inverse_DCT_method_ptr is declared in jpegint.h */
60 
61 /*
62  * Each IDCT routine has its own ideas about the best dct_table element type.
63  */
64 
65 typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
66 #if BITS_IN_JSAMPLE == 8
67 typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
68 #define IFAST_SCALE_BITS  2     /* fractional bits in scale factors */
69 #else
70 typedef INT32 IFAST_MULT_TYPE;  /* need 32 bits for scaled quantizers */
71 #define IFAST_SCALE_BITS  13    /* fractional bits in scale factors */
72 #endif
73 typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
74 
75 
76 /*
77  * Each IDCT routine is responsible for range-limiting its results and
78  * converting them to unsigned form (0..MAXJSAMPLE).  The raw outputs could
79  * be quite far out of range if the input data is corrupt, so a bulletproof
80  * range-limiting step is required.  We use a mask-and-table-lookup method
81  * to do the combined operations quickly.  See the comments with
82  * prepare_range_limit_table (in jdmaster.c) for more info.
83  */
84 
85 #define IDCT_range_limit(cinfo)  ((cinfo)->sample_range_limit + CENTERJSAMPLE)
86 
87 #define RANGE_MASK  (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */
88 
89 
90 /* Extern declarations for the forward and inverse DCT routines. */
91 
92 EXTERN(void) jpeg_fdct_islow (DCTELEM * data);
93 EXTERN(void) jpeg_fdct_ifast (DCTELEM * data);
94 EXTERN(void) jpeg_fdct_float (FAST_FLOAT * data);
95 
96 EXTERN(void) jpeg_idct_islow
97         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
98          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
99 EXTERN(void) jpeg_idct_ifast
100         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
101          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
102 EXTERN(void) jpeg_idct_float
103         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
104          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
105 EXTERN(void) jpeg_idct_7x7
106         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
107          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
108 EXTERN(void) jpeg_idct_6x6
109         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
110          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
111 EXTERN(void) jpeg_idct_5x5
112         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
113          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
114 EXTERN(void) jpeg_idct_4x4
115         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
116          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
117 EXTERN(void) jpeg_idct_3x3
118         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
119          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
120 EXTERN(void) jpeg_idct_2x2
121         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
122          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
123 EXTERN(void) jpeg_idct_1x1
124         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
125          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
126 EXTERN(void) jpeg_idct_9x9
127         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
128          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
129 EXTERN(void) jpeg_idct_10x10
130         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
131          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
132 EXTERN(void) jpeg_idct_11x11
133         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
134          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
135 EXTERN(void) jpeg_idct_12x12
136         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
137          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
138 EXTERN(void) jpeg_idct_13x13
139         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
140          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
141 EXTERN(void) jpeg_idct_14x14
142         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
143          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
144 EXTERN(void) jpeg_idct_15x15
145         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
146          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
147 EXTERN(void) jpeg_idct_16x16
148         (j_decompress_ptr cinfo, jpeg_component_info * compptr,
149          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
150 
151 
152 /*
153  * Macros for handling fixed-point arithmetic; these are used by many
154  * but not all of the DCT/IDCT modules.
155  *
156  * All values are expected to be of type INT32.
157  * Fractional constants are scaled left by CONST_BITS bits.
158  * CONST_BITS is defined within each module using these macros,
159  * and may differ from one module to the next.
160  */
161 
162 #define ONE     ((INT32) 1)
163 #define CONST_SCALE (ONE << CONST_BITS)
164 
165 /* Convert a positive real constant to an integer scaled by CONST_SCALE.
166  * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
167  * thus causing a lot of useless floating-point operations at run time.
168  */
169 
170 #define FIX(x)  ((INT32) ((x) * CONST_SCALE + 0.5))
171 
172 /* Descale and correctly round an INT32 value that's scaled by N bits.
173  * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
174  * the fudge factor is correct for either sign of X.
175  */
176 
177 #define DESCALE(x,n)  RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
178 
179 /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
180  * This macro is used only when the two inputs will actually be no more than
181  * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a
182  * full 32x32 multiply.  This provides a useful speedup on many machines.
183  * Unfortunately there is no way to specify a 16x16->32 multiply portably
184  * in C, but some C compilers will do the right thing if you provide the
185  * correct combination of casts.
186  */
187 
188 #ifdef SHORTxSHORT_32           /* may work if 'int' is 32 bits */
189 #define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT16) (const)))
190 #endif
191 #ifdef SHORTxLCONST_32          /* known to work with Microsoft C 6.0 */
192 #define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT32) (const)))
193 #endif
194 
195 #ifndef MULTIPLY16C16           /* default definition */
196 #define MULTIPLY16C16(var,const)  ((var) * (const))
197 #endif
198 
199 /* Same except both inputs are variables. */
200 
201 #ifdef SHORTxSHORT_32           /* may work if 'int' is 32 bits */
202 #define MULTIPLY16V16(var1,var2)  (((INT16) (var1)) * ((INT16) (var2)))
203 #endif
204 
205 #ifndef MULTIPLY16V16           /* default definition */
206 #define MULTIPLY16V16(var1,var2)  ((var1) * (var2))
207 #endif
208