blob: 66d1718b770bd7668e4c8a7b275475695f6b2e1c [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jdct.h
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
Alex Naidis6eb7d372016-10-16 23:10:08 +02006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2015, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 *
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.
DRCe5eaf372014-05-09 18:00:32 +000014 * The individual DCT algorithms are kept in separate files to ease
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000015 * machine-dependent tuning (e.g., assembly coding).
16 */
17
18
19/*
20 * A forward DCT routine is given a pointer to a work area of type DCTELEM[];
21 * the DCT is to be performed in-place in that buffer. Type DCTELEM is int
Alex Naidis6eb7d372016-10-16 23:10:08 +020022 * for 8-bit samples, JLONG for 12-bit samples. (NOTE: Floating-point DCT
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023 * implementations use an array of type FAST_FLOAT, instead.)
24 * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
25 * The DCT outputs are returned scaled up by a factor of 8; they therefore
26 * have a range of +-8K for 8-bit data, +-128K for 12-bit data. This
27 * convention improves accuracy in integer implementations and saves some
28 * work in floating-point ones.
Pierre Ossmandedc42e2009-03-09 13:23:04 +000029 * Quantization of the output coefficients is done by jcdctmgr.c. This
30 * step requires an unsigned type and also one with twice the bits.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000031 */
32
33#if BITS_IN_JSAMPLE == 8
Pierre Ossman5eb84ff2009-03-09 13:25:30 +000034#ifndef WITH_SIMD
DRCe5eaf372014-05-09 18:00:32 +000035typedef int DCTELEM; /* 16 or 32 bits is fine */
Pierre Ossmandedc42e2009-03-09 13:23:04 +000036typedef unsigned int UDCTELEM;
37typedef unsigned long long UDCTELEM2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000038#else
Leon Scroggins III3993b372018-07-16 10:43:45 -040039typedef short DCTELEM; /* prefer 16 bit with SIMD for parellelism */
Pierre Ossman5eb84ff2009-03-09 13:25:30 +000040typedef unsigned short UDCTELEM;
41typedef unsigned int UDCTELEM2;
42#endif
43#else
Alex Naidis6eb7d372016-10-16 23:10:08 +020044typedef JLONG DCTELEM; /* must have 32 bits */
Pierre Ossmandedc42e2009-03-09 13:23:04 +000045typedef unsigned long long UDCTELEM2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000046#endif
47
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000048
49/*
50 * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer
51 * to an output sample array. The routine must dequantize the input data as
52 * well as perform the IDCT; for dequantization, it uses the multiplier table
53 * pointed to by compptr->dct_table. The output data is to be placed into the
54 * sample array starting at a specified column. (Any row offset needed will
55 * be applied to the array pointer before it is passed to the IDCT code.)
56 * Note that the number of samples emitted by the IDCT routine is
57 * DCT_scaled_size * DCT_scaled_size.
58 */
59
60/* typedef inverse_DCT_method_ptr is declared in jpegint.h */
61
62/*
63 * Each IDCT routine has its own ideas about the best dct_table element type.
64 */
65
Leon Scroggins III3993b372018-07-16 10:43:45 -040066typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067#if BITS_IN_JSAMPLE == 8
Leon Scroggins III3993b372018-07-16 10:43:45 -040068typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
69#define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000070#else
Leon Scroggins III3993b372018-07-16 10:43:45 -040071typedef JLONG IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */
72#define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073#endif
Leon Scroggins III3993b372018-07-16 10:43:45 -040074typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075
76
77/*
78 * Each IDCT routine is responsible for range-limiting its results and
79 * converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
80 * be quite far out of range if the input data is corrupt, so a bulletproof
81 * range-limiting step is required. We use a mask-and-table-lookup method
82 * to do the combined operations quickly. See the comments with
83 * prepare_range_limit_table (in jdmaster.c) for more info.
84 */
85
86#define IDCT_range_limit(cinfo) ((cinfo)->sample_range_limit + CENTERJSAMPLE)
87
88#define RANGE_MASK (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */
89
90
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091/* Extern declarations for the forward and inverse DCT routines. */
92
Leon Scroggins III3993b372018-07-16 10:43:45 -040093EXTERN(void) jpeg_fdct_islow(DCTELEM *data);
94EXTERN(void) jpeg_fdct_ifast(DCTELEM *data);
95EXTERN(void) jpeg_fdct_float(FAST_FLOAT *data);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096
Leon Scroggins III3993b372018-07-16 10:43:45 -040097EXTERN(void) jpeg_idct_islow(j_decompress_ptr cinfo,
98 jpeg_component_info *compptr, JCOEFPTR coef_block,
99 JSAMPARRAY output_buf, JDIMENSION output_col);
100EXTERN(void) jpeg_idct_ifast(j_decompress_ptr cinfo,
101 jpeg_component_info *compptr, JCOEFPTR coef_block,
102 JSAMPARRAY output_buf, JDIMENSION output_col);
103EXTERN(void) jpeg_idct_float(j_decompress_ptr cinfo,
104 jpeg_component_info *compptr, JCOEFPTR coef_block,
105 JSAMPARRAY output_buf, JDIMENSION output_col);
106EXTERN(void) jpeg_idct_7x7(j_decompress_ptr cinfo,
107 jpeg_component_info *compptr, JCOEFPTR coef_block,
108 JSAMPARRAY output_buf, JDIMENSION output_col);
109EXTERN(void) jpeg_idct_6x6(j_decompress_ptr cinfo,
110 jpeg_component_info *compptr, JCOEFPTR coef_block,
111 JSAMPARRAY output_buf, JDIMENSION output_col);
112EXTERN(void) jpeg_idct_5x5(j_decompress_ptr cinfo,
113 jpeg_component_info *compptr, JCOEFPTR coef_block,
114 JSAMPARRAY output_buf, JDIMENSION output_col);
115EXTERN(void) jpeg_idct_4x4(j_decompress_ptr cinfo,
116 jpeg_component_info *compptr, JCOEFPTR coef_block,
117 JSAMPARRAY output_buf, JDIMENSION output_col);
118EXTERN(void) jpeg_idct_3x3(j_decompress_ptr cinfo,
119 jpeg_component_info *compptr, JCOEFPTR coef_block,
120 JSAMPARRAY output_buf, JDIMENSION output_col);
121EXTERN(void) jpeg_idct_2x2(j_decompress_ptr cinfo,
122 jpeg_component_info *compptr, JCOEFPTR coef_block,
123 JSAMPARRAY output_buf, JDIMENSION output_col);
124EXTERN(void) jpeg_idct_1x1(j_decompress_ptr cinfo,
125 jpeg_component_info *compptr, JCOEFPTR coef_block,
126 JSAMPARRAY output_buf, JDIMENSION output_col);
127EXTERN(void) jpeg_idct_9x9(j_decompress_ptr cinfo,
128 jpeg_component_info *compptr, JCOEFPTR coef_block,
129 JSAMPARRAY output_buf, JDIMENSION output_col);
130EXTERN(void) jpeg_idct_10x10(j_decompress_ptr cinfo,
131 jpeg_component_info *compptr, JCOEFPTR coef_block,
132 JSAMPARRAY output_buf, JDIMENSION output_col);
133EXTERN(void) jpeg_idct_11x11(j_decompress_ptr cinfo,
134 jpeg_component_info *compptr, JCOEFPTR coef_block,
135 JSAMPARRAY output_buf, JDIMENSION output_col);
136EXTERN(void) jpeg_idct_12x12(j_decompress_ptr cinfo,
137 jpeg_component_info *compptr, JCOEFPTR coef_block,
138 JSAMPARRAY output_buf, JDIMENSION output_col);
139EXTERN(void) jpeg_idct_13x13(j_decompress_ptr cinfo,
140 jpeg_component_info *compptr, JCOEFPTR coef_block,
141 JSAMPARRAY output_buf, JDIMENSION output_col);
142EXTERN(void) jpeg_idct_14x14(j_decompress_ptr cinfo,
143 jpeg_component_info *compptr, JCOEFPTR coef_block,
144 JSAMPARRAY output_buf, JDIMENSION output_col);
145EXTERN(void) jpeg_idct_15x15(j_decompress_ptr cinfo,
146 jpeg_component_info *compptr, JCOEFPTR coef_block,
147 JSAMPARRAY output_buf, JDIMENSION output_col);
148EXTERN(void) jpeg_idct_16x16(j_decompress_ptr cinfo,
149 jpeg_component_info *compptr, JCOEFPTR coef_block,
150 JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000151
152
153/*
154 * Macros for handling fixed-point arithmetic; these are used by many
155 * but not all of the DCT/IDCT modules.
156 *
Alex Naidis6eb7d372016-10-16 23:10:08 +0200157 * All values are expected to be of type JLONG.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158 * Fractional constants are scaled left by CONST_BITS bits.
159 * CONST_BITS is defined within each module using these macros,
160 * and may differ from one module to the next.
161 */
162
Leon Scroggins III3993b372018-07-16 10:43:45 -0400163#define ONE ((JLONG)1)
164#define CONST_SCALE (ONE << CONST_BITS)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000165
166/* Convert a positive real constant to an integer scaled by CONST_SCALE.
167 * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
168 * thus causing a lot of useless floating-point operations at run time.
169 */
170
Leon Scroggins III3993b372018-07-16 10:43:45 -0400171#define FIX(x) ((JLONG)((x) * CONST_SCALE + 0.5))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172
Alex Naidis6eb7d372016-10-16 23:10:08 +0200173/* Descale and correctly round a JLONG value that's scaled by N bits.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174 * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
175 * the fudge factor is correct for either sign of X.
176 */
177
Leon Scroggins III3993b372018-07-16 10:43:45 -0400178#define DESCALE(x, n) RIGHT_SHIFT((x) + (ONE << ((n) - 1)), n)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179
Alex Naidis6eb7d372016-10-16 23:10:08 +0200180/* Multiply a JLONG variable by a JLONG constant to yield a JLONG result.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181 * This macro is used only when the two inputs will actually be no more than
182 * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a
183 * full 32x32 multiply. This provides a useful speedup on many machines.
184 * Unfortunately there is no way to specify a 16x16->32 multiply portably
185 * in C, but some C compilers will do the right thing if you provide the
186 * correct combination of casts.
187 */
188
DRCe5eaf372014-05-09 18:00:32 +0000189#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400190#define MULTIPLY16C16(var, const) (((INT16)(var)) * ((INT16)(const)))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191#endif
DRCe5eaf372014-05-09 18:00:32 +0000192#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400193#define MULTIPLY16C16(var, const) (((INT16)(var)) * ((JLONG)(const)))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194#endif
195
DRCe5eaf372014-05-09 18:00:32 +0000196#ifndef MULTIPLY16C16 /* default definition */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400197#define MULTIPLY16C16(var, const) ((var) * (const))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000198#endif
199
200/* Same except both inputs are variables. */
201
DRCe5eaf372014-05-09 18:00:32 +0000202#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400203#define MULTIPLY16V16(var1, var2) (((INT16)(var1)) * ((INT16)(var2)))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204#endif
205
DRCe5eaf372014-05-09 18:00:32 +0000206#ifndef MULTIPLY16V16 /* default definition */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400207#define MULTIPLY16V16(var1, var2) ((var1) * (var2))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208#endif