Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdct.h |
| 3 | * |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1994-1996, Thomas G. Lane. |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 6 | * It was modified by The libjpeg-turbo Project to include only code relevant |
| 7 | * to libjpeg-turbo. |
DRC | 7e3acc0 | 2015-10-10 10:25:46 -0500 | [diff] [blame] | 8 | * For conditions of distribution and use, see the accompanying README.ijg |
| 9 | * file. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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. |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 14 | * The individual DCT algorithms are kept in separate files to ease |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 15 | * 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 |
| 22 | * for 8-bit samples, INT32 for 12-bit samples. (NOTE: Floating-point DCT |
| 23 | * 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 Ossman | dedc42e | 2009-03-09 13:23:04 +0000 | [diff] [blame] | 29 | * 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. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #if BITS_IN_JSAMPLE == 8 |
Pierre Ossman | 5eb84ff | 2009-03-09 13:25:30 +0000 | [diff] [blame] | 34 | #ifndef WITH_SIMD |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 35 | typedef int DCTELEM; /* 16 or 32 bits is fine */ |
Pierre Ossman | dedc42e | 2009-03-09 13:23:04 +0000 | [diff] [blame] | 36 | typedef unsigned int UDCTELEM; |
| 37 | typedef unsigned long long UDCTELEM2; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 38 | #else |
Pierre Ossman | 5eb84ff | 2009-03-09 13:25:30 +0000 | [diff] [blame] | 39 | typedef short DCTELEM; /* prefer 16 bit with SIMD for parellelism */ |
| 40 | typedef unsigned short UDCTELEM; |
| 41 | typedef unsigned int UDCTELEM2; |
| 42 | #endif |
| 43 | #else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 44 | typedef INT32 DCTELEM; /* must have 32 bits */ |
Pierre Ossman | dedc42e | 2009-03-09 13:23:04 +0000 | [diff] [blame] | 45 | typedef unsigned long long UDCTELEM2; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 48 | |
| 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 | |
| 66 | typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */ |
| 67 | #if BITS_IN_JSAMPLE == 8 |
| 68 | typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 69 | #define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 70 | #else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 71 | typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */ |
| 72 | #define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 73 | #endif |
| 74 | typedef 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. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 91 | /* Extern declarations for the forward and inverse DCT routines. */ |
| 92 | |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 93 | EXTERN(void) jpeg_fdct_islow (DCTELEM * data); |
| 94 | EXTERN(void) jpeg_fdct_ifast (DCTELEM * data); |
| 95 | EXTERN(void) jpeg_fdct_float (FAST_FLOAT * data); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 96 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 97 | EXTERN(void) jpeg_idct_islow |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 98 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 99 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 100 | EXTERN(void) jpeg_idct_ifast |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 101 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 102 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 103 | EXTERN(void) jpeg_idct_float |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 104 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 105 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 106 | EXTERN(void) jpeg_idct_7x7 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 107 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 108 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 109 | EXTERN(void) jpeg_idct_6x6 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 110 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 111 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 112 | EXTERN(void) jpeg_idct_5x5 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 113 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 114 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 115 | EXTERN(void) jpeg_idct_4x4 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 116 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 117 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 118 | EXTERN(void) jpeg_idct_3x3 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 119 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 120 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 121 | EXTERN(void) jpeg_idct_2x2 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 122 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 123 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 124 | EXTERN(void) jpeg_idct_1x1 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 125 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 126 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 127 | EXTERN(void) jpeg_idct_9x9 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 128 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 129 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 130 | EXTERN(void) jpeg_idct_10x10 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 131 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 132 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 133 | EXTERN(void) jpeg_idct_11x11 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 134 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 135 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 136 | EXTERN(void) jpeg_idct_12x12 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 137 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 138 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 139 | EXTERN(void) jpeg_idct_13x13 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 140 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 141 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 142 | EXTERN(void) jpeg_idct_14x14 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 143 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 144 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 145 | EXTERN(void) jpeg_idct_15x15 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 146 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 147 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 148 | EXTERN(void) jpeg_idct_16x16 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 149 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 150 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | /* |
| 154 | * Macros for handling fixed-point arithmetic; these are used by many |
| 155 | * but not all of the DCT/IDCT modules. |
| 156 | * |
| 157 | * All values are expected to be of type INT32. |
| 158 | * 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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 163 | #define ONE ((INT32) 1) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 164 | #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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 171 | #define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5)) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 172 | |
| 173 | /* Descale and correctly round an INT32 value that's scaled by N bits. |
| 174 | * 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 | |
| 180 | /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. |
| 181 | * 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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 189 | #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 190 | #define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const))) |
| 191 | #endif |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 192 | #ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 193 | #define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const))) |
| 194 | #endif |
| 195 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 196 | #ifndef MULTIPLY16C16 /* default definition */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 197 | #define MULTIPLY16C16(var,const) ((var) * (const)) |
| 198 | #endif |
| 199 | |
| 200 | /* Same except both inputs are variables. */ |
| 201 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 202 | #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 203 | #define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2))) |
| 204 | #endif |
| 205 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 206 | #ifndef MULTIPLY16V16 /* default definition */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 207 | #define MULTIPLY16V16(var1,var2) ((var1) * (var2)) |
| 208 | #endif |