blob: 6f8b1591aace672147757d6eb3ef338a4316facd [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.
DRC5033f3e2014-05-18 18:33:44 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * 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.
DRCe5eaf372014-05-09 18:00:32 +000013 * The individual DCT algorithms are kept in separate files to ease
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000014 * 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.
Pierre Ossmandedc42e2009-03-09 13:23:04 +000028 * Quantization of the output coefficients is done by jcdctmgr.c. This
29 * step requires an unsigned type and also one with twice the bits.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030 */
31
32#if BITS_IN_JSAMPLE == 8
Pierre Ossman5eb84ff2009-03-09 13:25:30 +000033#ifndef WITH_SIMD
DRCe5eaf372014-05-09 18:00:32 +000034typedef int DCTELEM; /* 16 or 32 bits is fine */
Pierre Ossmandedc42e2009-03-09 13:23:04 +000035typedef unsigned int UDCTELEM;
36typedef unsigned long long UDCTELEM2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037#else
Pierre Ossman5eb84ff2009-03-09 13:25:30 +000038typedef short DCTELEM; /* prefer 16 bit with SIMD for parellelism */
39typedef unsigned short UDCTELEM;
40typedef unsigned int UDCTELEM2;
41#endif
42#else
DRCe5eaf372014-05-09 18:00:32 +000043typedef INT32 DCTELEM; /* must have 32 bits */
Pierre Ossmandedc42e2009-03-09 13:23:04 +000044typedef unsigned long long UDCTELEM2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000045#endif
46
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047
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
65typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
66#if BITS_IN_JSAMPLE == 8
67typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
DRCe5eaf372014-05-09 18:00:32 +000068#define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069#else
DRCe5eaf372014-05-09 18:00:32 +000070typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */
71#define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072#endif
73typedef 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
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090/* Extern declarations for the forward and inverse DCT routines. */
91
DRCbc56b752014-05-16 10:43:44 +000092EXTERN(void) jpeg_fdct_islow (DCTELEM * data);
93EXTERN(void) jpeg_fdct_ifast (DCTELEM * data);
94EXTERN(void) jpeg_fdct_float (FAST_FLOAT * data);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095
Thomas G. Lane489583f1996-02-07 00:00:00 +000096EXTERN(void) jpeg_idct_islow
DRCbc56b752014-05-16 10:43:44 +000097 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
98 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +000099EXTERN(void) jpeg_idct_ifast
DRCbc56b752014-05-16 10:43:44 +0000100 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
101 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000102EXTERN(void) jpeg_idct_float
DRCbc56b752014-05-16 10:43:44 +0000103 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
104 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000105EXTERN(void) jpeg_idct_7x7
DRCbc56b752014-05-16 10:43:44 +0000106 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
107 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000108EXTERN(void) jpeg_idct_6x6
DRCbc56b752014-05-16 10:43:44 +0000109 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
110 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000111EXTERN(void) jpeg_idct_5x5
DRCbc56b752014-05-16 10:43:44 +0000112 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
113 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000114EXTERN(void) jpeg_idct_4x4
DRCbc56b752014-05-16 10:43:44 +0000115 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
116 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000117EXTERN(void) jpeg_idct_3x3
DRCbc56b752014-05-16 10:43:44 +0000118 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
119 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000120EXTERN(void) jpeg_idct_2x2
DRCbc56b752014-05-16 10:43:44 +0000121 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
122 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000123EXTERN(void) jpeg_idct_1x1
DRCbc56b752014-05-16 10:43:44 +0000124 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
125 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000126EXTERN(void) jpeg_idct_9x9
DRCbc56b752014-05-16 10:43:44 +0000127 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
128 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000129EXTERN(void) jpeg_idct_10x10
DRCbc56b752014-05-16 10:43:44 +0000130 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
131 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000132EXTERN(void) jpeg_idct_11x11
DRCbc56b752014-05-16 10:43:44 +0000133 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
134 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000135EXTERN(void) jpeg_idct_12x12
DRCbc56b752014-05-16 10:43:44 +0000136 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
137 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000138EXTERN(void) jpeg_idct_13x13
DRCbc56b752014-05-16 10:43:44 +0000139 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
140 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000141EXTERN(void) jpeg_idct_14x14
DRCbc56b752014-05-16 10:43:44 +0000142 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
143 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000144EXTERN(void) jpeg_idct_15x15
DRCbc56b752014-05-16 10:43:44 +0000145 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
146 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000147EXTERN(void) jpeg_idct_16x16
DRCbc56b752014-05-16 10:43:44 +0000148 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
149 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000150
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
DRCe5eaf372014-05-09 18:00:32 +0000162#define ONE ((INT32) 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000163#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
DRCe5eaf372014-05-09 18:00:32 +0000170#define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000171
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
DRCe5eaf372014-05-09 18:00:32 +0000188#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const)))
190#endif
DRCe5eaf372014-05-09 18:00:32 +0000191#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const)))
193#endif
194
DRCe5eaf372014-05-09 18:00:32 +0000195#ifndef MULTIPLY16C16 /* default definition */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196#define MULTIPLY16C16(var,const) ((var) * (const))
197#endif
198
199/* Same except both inputs are variables. */
200
DRCe5eaf372014-05-09 18:00:32 +0000201#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202#define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2)))
203#endif
204
DRCe5eaf372014-05-09 18:00:32 +0000205#ifndef MULTIPLY16V16 /* default definition */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000206#define MULTIPLY16V16(var1,var2) ((var1) * (var2))
207#endif