blob: faf8e1cf0315192edc114797cc6819a657781ec2 [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.
DRC1e32fe32015-10-14 17:32:39 -05006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2015, D. R. Commander.
DRC7e3acc02015-10-10 10:25:46 -05008 * 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
DRC1e32fe32015-10-14 17:32:39 -050022 * 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
Pierre Ossman5eb84ff2009-03-09 13:25:30 +000039typedef short DCTELEM; /* prefer 16 bit with SIMD for parellelism */
40typedef unsigned short UDCTELEM;
41typedef unsigned int UDCTELEM2;
42#endif
43#else
DRC1e32fe32015-10-14 17:32:39 -050044typedef 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
66typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
67#if BITS_IN_JSAMPLE == 8
68typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
DRCe5eaf372014-05-09 18:00:32 +000069#define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000070#else
DRC1e32fe32015-10-14 17:32:39 -050071typedef JLONG IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */
DRCe5eaf372014-05-09 18:00:32 +000072#define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073#endif
74typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
75
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
DRCbd498032016-02-19 08:53:33 -060093EXTERN(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
Thomas G. Lane489583f1996-02-07 00:00:00 +000097EXTERN(void) jpeg_idct_islow
DRCbd498032016-02-19 08:53:33 -060098 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +000099 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000100EXTERN(void) jpeg_idct_ifast
DRCbd498032016-02-19 08:53:33 -0600101 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000102 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000103EXTERN(void) jpeg_idct_float
DRCbd498032016-02-19 08:53:33 -0600104 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000105 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000106EXTERN(void) jpeg_idct_7x7
DRCbd498032016-02-19 08:53:33 -0600107 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000108 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000109EXTERN(void) jpeg_idct_6x6
DRCbd498032016-02-19 08:53:33 -0600110 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000111 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000112EXTERN(void) jpeg_idct_5x5
DRCbd498032016-02-19 08:53:33 -0600113 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000114 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000115EXTERN(void) jpeg_idct_4x4
DRCbd498032016-02-19 08:53:33 -0600116 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000117 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000118EXTERN(void) jpeg_idct_3x3
DRCbd498032016-02-19 08:53:33 -0600119 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000120 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000121EXTERN(void) jpeg_idct_2x2
DRCbd498032016-02-19 08:53:33 -0600122 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000123 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Thomas G. Lane489583f1996-02-07 00:00:00 +0000124EXTERN(void) jpeg_idct_1x1
DRCbd498032016-02-19 08:53:33 -0600125 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000126 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000127EXTERN(void) jpeg_idct_9x9
DRCbd498032016-02-19 08:53:33 -0600128 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000129 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000130EXTERN(void) jpeg_idct_10x10
DRCbd498032016-02-19 08:53:33 -0600131 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000132 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000133EXTERN(void) jpeg_idct_11x11
DRCbd498032016-02-19 08:53:33 -0600134 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000135 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000136EXTERN(void) jpeg_idct_12x12
DRCbd498032016-02-19 08:53:33 -0600137 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000138 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000139EXTERN(void) jpeg_idct_13x13
DRCbd498032016-02-19 08:53:33 -0600140 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000141 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000142EXTERN(void) jpeg_idct_14x14
DRCbd498032016-02-19 08:53:33 -0600143 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000144 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000145EXTERN(void) jpeg_idct_15x15
DRCbd498032016-02-19 08:53:33 -0600146 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000147 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000148EXTERN(void) jpeg_idct_16x16
DRCbd498032016-02-19 08:53:33 -0600149 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
DRCbc56b752014-05-16 10:43:44 +0000150 JCOEFPTR coef_block, 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 *
DRC1e32fe32015-10-14 17:32:39 -0500157 * 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
DRC1e32fe32015-10-14 17:32:39 -0500163#define ONE ((JLONG) 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164#define CONST_SCALE (ONE << CONST_BITS)
165
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
DRC1e32fe32015-10-14 17:32:39 -0500171#define FIX(x) ((JLONG) ((x) * CONST_SCALE + 0.5))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172
DRC1e32fe32015-10-14 17:32:39 -0500173/* 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
178#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
179
DRC1e32fe32015-10-14 17:32:39 -0500180/* 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 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const)))
191#endif
DRCe5eaf372014-05-09 18:00:32 +0000192#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
DRC1e32fe32015-10-14 17:32:39 -0500193#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 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197#define MULTIPLY16C16(var,const) ((var) * (const))
198#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 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203#define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2)))
204#endif
205
DRCe5eaf372014-05-09 18:00:32 +0000206#ifndef MULTIPLY16V16 /* default definition */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000207#define MULTIPLY16V16(var1,var2) ((var1) * (var2))
208#endif