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. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 8 | * 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. |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 13 | * The individual DCT algorithms are kept in separate files to ease |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 14 | * 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 Ossman | dedc42e | 2009-03-09 13:23:04 +0000 | [diff] [blame] | 28 | * 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. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #if BITS_IN_JSAMPLE == 8 |
Pierre Ossman | 5eb84ff | 2009-03-09 13:25:30 +0000 | [diff] [blame] | 33 | #ifndef WITH_SIMD |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 34 | typedef int DCTELEM; /* 16 or 32 bits is fine */ |
Pierre Ossman | dedc42e | 2009-03-09 13:23:04 +0000 | [diff] [blame] | 35 | typedef unsigned int UDCTELEM; |
| 36 | typedef unsigned long long UDCTELEM2; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 37 | #else |
Pierre Ossman | 5eb84ff | 2009-03-09 13:25:30 +0000 | [diff] [blame] | 38 | typedef short DCTELEM; /* prefer 16 bit with SIMD for parellelism */ |
| 39 | typedef unsigned short UDCTELEM; |
| 40 | typedef unsigned int UDCTELEM2; |
| 41 | #endif |
| 42 | #else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 43 | typedef INT32 DCTELEM; /* must have 32 bits */ |
Pierre Ossman | dedc42e | 2009-03-09 13:23:04 +0000 | [diff] [blame] | 44 | typedef unsigned long long UDCTELEM2; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 47 | |
| 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 | |
| 65 | typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */ |
| 66 | #if BITS_IN_JSAMPLE == 8 |
| 67 | typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 68 | #define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 69 | #else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 70 | typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */ |
| 71 | #define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 72 | #endif |
| 73 | typedef 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. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 90 | /* Extern declarations for the forward and inverse DCT routines. */ |
| 91 | |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 92 | EXTERN(void) jpeg_fdct_islow (DCTELEM * data); |
| 93 | EXTERN(void) jpeg_fdct_ifast (DCTELEM * data); |
| 94 | EXTERN(void) jpeg_fdct_float (FAST_FLOAT * data); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 95 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 96 | EXTERN(void) jpeg_idct_islow |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 97 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 98 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 99 | EXTERN(void) jpeg_idct_ifast |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 100 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 101 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 102 | EXTERN(void) jpeg_idct_float |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 103 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 104 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 105 | EXTERN(void) jpeg_idct_7x7 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 106 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 107 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 108 | EXTERN(void) jpeg_idct_6x6 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 109 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 110 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 111 | EXTERN(void) jpeg_idct_5x5 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 112 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 113 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 114 | EXTERN(void) jpeg_idct_4x4 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 115 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 116 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 117 | EXTERN(void) jpeg_idct_3x3 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 118 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 119 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 120 | EXTERN(void) jpeg_idct_2x2 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 121 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 122 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 123 | EXTERN(void) jpeg_idct_1x1 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 124 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 125 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 126 | EXTERN(void) jpeg_idct_9x9 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 127 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 128 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 129 | EXTERN(void) jpeg_idct_10x10 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 130 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 131 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 132 | EXTERN(void) jpeg_idct_11x11 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 133 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 134 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 135 | EXTERN(void) jpeg_idct_12x12 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 136 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 137 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 138 | EXTERN(void) jpeg_idct_13x13 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 139 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 140 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 141 | EXTERN(void) jpeg_idct_14x14 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 142 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 143 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 144 | EXTERN(void) jpeg_idct_15x15 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 145 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 146 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 147 | EXTERN(void) jpeg_idct_16x16 |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 148 | (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 149 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 150 | |
| 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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 162 | #define ONE ((INT32) 1) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 163 | #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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 170 | #define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5)) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 171 | |
| 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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 188 | #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 189 | #define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const))) |
| 190 | #endif |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 191 | #ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 192 | #define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const))) |
| 193 | #endif |
| 194 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 195 | #ifndef MULTIPLY16C16 /* default definition */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 196 | #define MULTIPLY16C16(var,const) ((var) * (const)) |
| 197 | #endif |
| 198 | |
| 199 | /* Same except both inputs are variables. */ |
| 200 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 201 | #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 202 | #define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2))) |
| 203 | #endif |
| 204 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 205 | #ifndef MULTIPLY16V16 /* default definition */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 206 | #define MULTIPLY16V16(var1,var2) ((var1) * (var2)) |
| 207 | #endif |