1*dfc6aa5cSAndroid Build Coastguard Worker /* 2*dfc6aa5cSAndroid Build Coastguard Worker * jdct.h 3*dfc6aa5cSAndroid Build Coastguard Worker * 4*dfc6aa5cSAndroid Build Coastguard Worker * This file was part of the Independent JPEG Group's software: 5*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 1994-1996, Thomas G. Lane. 6*dfc6aa5cSAndroid Build Coastguard Worker * libjpeg-turbo Modifications: 7*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 2015, D. R. Commander. 8*dfc6aa5cSAndroid Build Coastguard Worker * For conditions of distribution and use, see the accompanying README.ijg 9*dfc6aa5cSAndroid Build Coastguard Worker * file. 10*dfc6aa5cSAndroid Build Coastguard Worker * 11*dfc6aa5cSAndroid Build Coastguard Worker * This include file contains common declarations for the forward and 12*dfc6aa5cSAndroid Build Coastguard Worker * inverse DCT modules. These declarations are private to the DCT managers 13*dfc6aa5cSAndroid Build Coastguard Worker * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms. 14*dfc6aa5cSAndroid Build Coastguard Worker * The individual DCT algorithms are kept in separate files to ease 15*dfc6aa5cSAndroid Build Coastguard Worker * machine-dependent tuning (e.g., assembly coding). 16*dfc6aa5cSAndroid Build Coastguard Worker */ 17*dfc6aa5cSAndroid Build Coastguard Worker 18*dfc6aa5cSAndroid Build Coastguard Worker 19*dfc6aa5cSAndroid Build Coastguard Worker /* 20*dfc6aa5cSAndroid Build Coastguard Worker * A forward DCT routine is given a pointer to a work area of type DCTELEM[]; 21*dfc6aa5cSAndroid Build Coastguard Worker * the DCT is to be performed in-place in that buffer. Type DCTELEM is int 22*dfc6aa5cSAndroid Build Coastguard Worker * for 8-bit samples, JLONG for 12-bit samples. (NOTE: Floating-point DCT 23*dfc6aa5cSAndroid Build Coastguard Worker * implementations use an array of type FAST_FLOAT, instead.) 24*dfc6aa5cSAndroid Build Coastguard Worker * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE). 25*dfc6aa5cSAndroid Build Coastguard Worker * The DCT outputs are returned scaled up by a factor of 8; they therefore 26*dfc6aa5cSAndroid Build Coastguard Worker * have a range of +-8K for 8-bit data, +-128K for 12-bit data. This 27*dfc6aa5cSAndroid Build Coastguard Worker * convention improves accuracy in integer implementations and saves some 28*dfc6aa5cSAndroid Build Coastguard Worker * work in floating-point ones. 29*dfc6aa5cSAndroid Build Coastguard Worker * Quantization of the output coefficients is done by jcdctmgr.c. This 30*dfc6aa5cSAndroid Build Coastguard Worker * step requires an unsigned type and also one with twice the bits. 31*dfc6aa5cSAndroid Build Coastguard Worker */ 32*dfc6aa5cSAndroid Build Coastguard Worker 33*dfc6aa5cSAndroid Build Coastguard Worker #if BITS_IN_JSAMPLE == 8 34*dfc6aa5cSAndroid Build Coastguard Worker #ifndef WITH_SIMD 35*dfc6aa5cSAndroid Build Coastguard Worker typedef int DCTELEM; /* 16 or 32 bits is fine */ 36*dfc6aa5cSAndroid Build Coastguard Worker typedef unsigned int UDCTELEM; 37*dfc6aa5cSAndroid Build Coastguard Worker typedef unsigned long long UDCTELEM2; 38*dfc6aa5cSAndroid Build Coastguard Worker #else 39*dfc6aa5cSAndroid Build Coastguard Worker typedef short DCTELEM; /* prefer 16 bit with SIMD for parellelism */ 40*dfc6aa5cSAndroid Build Coastguard Worker typedef unsigned short UDCTELEM; 41*dfc6aa5cSAndroid Build Coastguard Worker typedef unsigned int UDCTELEM2; 42*dfc6aa5cSAndroid Build Coastguard Worker #endif 43*dfc6aa5cSAndroid Build Coastguard Worker #else 44*dfc6aa5cSAndroid Build Coastguard Worker typedef JLONG DCTELEM; /* must have 32 bits */ 45*dfc6aa5cSAndroid Build Coastguard Worker typedef unsigned long long UDCTELEM2; 46*dfc6aa5cSAndroid Build Coastguard Worker #endif 47*dfc6aa5cSAndroid Build Coastguard Worker 48*dfc6aa5cSAndroid Build Coastguard Worker 49*dfc6aa5cSAndroid Build Coastguard Worker /* 50*dfc6aa5cSAndroid Build Coastguard Worker * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer 51*dfc6aa5cSAndroid Build Coastguard Worker * to an output sample array. The routine must dequantize the input data as 52*dfc6aa5cSAndroid Build Coastguard Worker * well as perform the IDCT; for dequantization, it uses the multiplier table 53*dfc6aa5cSAndroid Build Coastguard Worker * pointed to by compptr->dct_table. The output data is to be placed into the 54*dfc6aa5cSAndroid Build Coastguard Worker * sample array starting at a specified column. (Any row offset needed will 55*dfc6aa5cSAndroid Build Coastguard Worker * be applied to the array pointer before it is passed to the IDCT code.) 56*dfc6aa5cSAndroid Build Coastguard Worker * Note that the number of samples emitted by the IDCT routine is 57*dfc6aa5cSAndroid Build Coastguard Worker * DCT_scaled_size * DCT_scaled_size. 58*dfc6aa5cSAndroid Build Coastguard Worker */ 59*dfc6aa5cSAndroid Build Coastguard Worker 60*dfc6aa5cSAndroid Build Coastguard Worker /* typedef inverse_DCT_method_ptr is declared in jpegint.h */ 61*dfc6aa5cSAndroid Build Coastguard Worker 62*dfc6aa5cSAndroid Build Coastguard Worker /* 63*dfc6aa5cSAndroid Build Coastguard Worker * Each IDCT routine has its own ideas about the best dct_table element type. 64*dfc6aa5cSAndroid Build Coastguard Worker */ 65*dfc6aa5cSAndroid Build Coastguard Worker 66*dfc6aa5cSAndroid Build Coastguard Worker typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */ 67*dfc6aa5cSAndroid Build Coastguard Worker #if BITS_IN_JSAMPLE == 8 68*dfc6aa5cSAndroid Build Coastguard Worker typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */ 69*dfc6aa5cSAndroid Build Coastguard Worker #define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */ 70*dfc6aa5cSAndroid Build Coastguard Worker #else 71*dfc6aa5cSAndroid Build Coastguard Worker typedef JLONG IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */ 72*dfc6aa5cSAndroid Build Coastguard Worker #define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */ 73*dfc6aa5cSAndroid Build Coastguard Worker #endif 74*dfc6aa5cSAndroid Build Coastguard Worker typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */ 75*dfc6aa5cSAndroid Build Coastguard Worker 76*dfc6aa5cSAndroid Build Coastguard Worker 77*dfc6aa5cSAndroid Build Coastguard Worker /* 78*dfc6aa5cSAndroid Build Coastguard Worker * Each IDCT routine is responsible for range-limiting its results and 79*dfc6aa5cSAndroid Build Coastguard Worker * converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could 80*dfc6aa5cSAndroid Build Coastguard Worker * be quite far out of range if the input data is corrupt, so a bulletproof 81*dfc6aa5cSAndroid Build Coastguard Worker * range-limiting step is required. We use a mask-and-table-lookup method 82*dfc6aa5cSAndroid Build Coastguard Worker * to do the combined operations quickly. See the comments with 83*dfc6aa5cSAndroid Build Coastguard Worker * prepare_range_limit_table (in jdmaster.c) for more info. 84*dfc6aa5cSAndroid Build Coastguard Worker */ 85*dfc6aa5cSAndroid Build Coastguard Worker 86*dfc6aa5cSAndroid Build Coastguard Worker #define IDCT_range_limit(cinfo) ((cinfo)->sample_range_limit + CENTERJSAMPLE) 87*dfc6aa5cSAndroid Build Coastguard Worker 88*dfc6aa5cSAndroid Build Coastguard Worker #define RANGE_MASK (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */ 89*dfc6aa5cSAndroid Build Coastguard Worker 90*dfc6aa5cSAndroid Build Coastguard Worker 91*dfc6aa5cSAndroid Build Coastguard Worker /* Extern declarations for the forward and inverse DCT routines. */ 92*dfc6aa5cSAndroid Build Coastguard Worker 93*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_fdct_islow(DCTELEM *data); 94*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_fdct_ifast(DCTELEM *data); 95*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_fdct_float(FAST_FLOAT *data); 96*dfc6aa5cSAndroid Build Coastguard Worker 97*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_islow(j_decompress_ptr cinfo, 98*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 99*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 100*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_ifast(j_decompress_ptr cinfo, 101*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 102*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 103*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_float(j_decompress_ptr cinfo, 104*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 105*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 106*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_7x7(j_decompress_ptr cinfo, 107*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 108*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 109*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_6x6(j_decompress_ptr cinfo, 110*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 111*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 112*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_5x5(j_decompress_ptr cinfo, 113*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 114*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 115*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_4x4(j_decompress_ptr cinfo, 116*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 117*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 118*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_3x3(j_decompress_ptr cinfo, 119*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 120*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 121*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_2x2(j_decompress_ptr cinfo, 122*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 123*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 124*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_1x1(j_decompress_ptr cinfo, 125*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 126*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 127*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_9x9(j_decompress_ptr cinfo, 128*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 129*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 130*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_10x10(j_decompress_ptr cinfo, 131*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 132*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 133*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_11x11(j_decompress_ptr cinfo, 134*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 135*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 136*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_12x12(j_decompress_ptr cinfo, 137*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 138*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 139*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_13x13(j_decompress_ptr cinfo, 140*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 141*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 142*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_14x14(j_decompress_ptr cinfo, 143*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 144*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 145*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_15x15(j_decompress_ptr cinfo, 146*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 147*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 148*dfc6aa5cSAndroid Build Coastguard Worker EXTERN(void) jpeg_idct_16x16(j_decompress_ptr cinfo, 149*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr, JCOEFPTR coef_block, 150*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, JDIMENSION output_col); 151*dfc6aa5cSAndroid Build Coastguard Worker 152*dfc6aa5cSAndroid Build Coastguard Worker 153*dfc6aa5cSAndroid Build Coastguard Worker /* 154*dfc6aa5cSAndroid Build Coastguard Worker * Macros for handling fixed-point arithmetic; these are used by many 155*dfc6aa5cSAndroid Build Coastguard Worker * but not all of the DCT/IDCT modules. 156*dfc6aa5cSAndroid Build Coastguard Worker * 157*dfc6aa5cSAndroid Build Coastguard Worker * All values are expected to be of type JLONG. 158*dfc6aa5cSAndroid Build Coastguard Worker * Fractional constants are scaled left by CONST_BITS bits. 159*dfc6aa5cSAndroid Build Coastguard Worker * CONST_BITS is defined within each module using these macros, 160*dfc6aa5cSAndroid Build Coastguard Worker * and may differ from one module to the next. 161*dfc6aa5cSAndroid Build Coastguard Worker */ 162*dfc6aa5cSAndroid Build Coastguard Worker 163*dfc6aa5cSAndroid Build Coastguard Worker #define ONE ((JLONG)1) 164*dfc6aa5cSAndroid Build Coastguard Worker #define CONST_SCALE (ONE << CONST_BITS) 165*dfc6aa5cSAndroid Build Coastguard Worker 166*dfc6aa5cSAndroid Build Coastguard Worker /* Convert a positive real constant to an integer scaled by CONST_SCALE. 167*dfc6aa5cSAndroid Build Coastguard Worker * Caution: some C compilers fail to reduce "FIX(constant)" at compile time, 168*dfc6aa5cSAndroid Build Coastguard Worker * thus causing a lot of useless floating-point operations at run time. 169*dfc6aa5cSAndroid Build Coastguard Worker */ 170*dfc6aa5cSAndroid Build Coastguard Worker 171*dfc6aa5cSAndroid Build Coastguard Worker #define FIX(x) ((JLONG)((x) * CONST_SCALE + 0.5)) 172*dfc6aa5cSAndroid Build Coastguard Worker 173*dfc6aa5cSAndroid Build Coastguard Worker /* Descale and correctly round a JLONG value that's scaled by N bits. 174*dfc6aa5cSAndroid Build Coastguard Worker * We assume RIGHT_SHIFT rounds towards minus infinity, so adding 175*dfc6aa5cSAndroid Build Coastguard Worker * the fudge factor is correct for either sign of X. 176*dfc6aa5cSAndroid Build Coastguard Worker */ 177*dfc6aa5cSAndroid Build Coastguard Worker 178*dfc6aa5cSAndroid Build Coastguard Worker #define DESCALE(x, n) RIGHT_SHIFT((x) + (ONE << ((n) - 1)), n) 179*dfc6aa5cSAndroid Build Coastguard Worker 180*dfc6aa5cSAndroid Build Coastguard Worker /* Multiply a JLONG variable by a JLONG constant to yield a JLONG result. 181*dfc6aa5cSAndroid Build Coastguard Worker * This macro is used only when the two inputs will actually be no more than 182*dfc6aa5cSAndroid Build Coastguard Worker * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a 183*dfc6aa5cSAndroid Build Coastguard Worker * full 32x32 multiply. This provides a useful speedup on many machines. 184*dfc6aa5cSAndroid Build Coastguard Worker * Unfortunately there is no way to specify a 16x16->32 multiply portably 185*dfc6aa5cSAndroid Build Coastguard Worker * in C, but some C compilers will do the right thing if you provide the 186*dfc6aa5cSAndroid Build Coastguard Worker * correct combination of casts. 187*dfc6aa5cSAndroid Build Coastguard Worker */ 188*dfc6aa5cSAndroid Build Coastguard Worker 189*dfc6aa5cSAndroid Build Coastguard Worker #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ 190*dfc6aa5cSAndroid Build Coastguard Worker #define MULTIPLY16C16(var, const) (((INT16)(var)) * ((INT16)(const))) 191*dfc6aa5cSAndroid Build Coastguard Worker #endif 192*dfc6aa5cSAndroid Build Coastguard Worker #ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */ 193*dfc6aa5cSAndroid Build Coastguard Worker #define MULTIPLY16C16(var, const) (((INT16)(var)) * ((JLONG)(const))) 194*dfc6aa5cSAndroid Build Coastguard Worker #endif 195*dfc6aa5cSAndroid Build Coastguard Worker 196*dfc6aa5cSAndroid Build Coastguard Worker #ifndef MULTIPLY16C16 /* default definition */ 197*dfc6aa5cSAndroid Build Coastguard Worker #define MULTIPLY16C16(var, const) ((var) * (const)) 198*dfc6aa5cSAndroid Build Coastguard Worker #endif 199*dfc6aa5cSAndroid Build Coastguard Worker 200*dfc6aa5cSAndroid Build Coastguard Worker /* Same except both inputs are variables. */ 201*dfc6aa5cSAndroid Build Coastguard Worker 202*dfc6aa5cSAndroid Build Coastguard Worker #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ 203*dfc6aa5cSAndroid Build Coastguard Worker #define MULTIPLY16V16(var1, var2) (((INT16)(var1)) * ((INT16)(var2))) 204*dfc6aa5cSAndroid Build Coastguard Worker #endif 205*dfc6aa5cSAndroid Build Coastguard Worker 206*dfc6aa5cSAndroid Build Coastguard Worker #ifndef MULTIPLY16V16 /* default definition */ 207*dfc6aa5cSAndroid Build Coastguard Worker #define MULTIPLY16V16(var1, var2) ((var1) * (var2)) 208*dfc6aa5cSAndroid Build Coastguard Worker #endif 209