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