Home | History | Annotate | Download | only in libjpeg-turbo
      1 /*
      2  * jdct.h
      3  *
      4  * This file was part of the Independent JPEG Group's software:
      5  * Copyright (C) 1994-1996, Thomas G. Lane.
      6  * libjpeg-turbo Modifications:
      7  * Copyright (C) 2015, D. R. Commander.
      8  * For conditions of distribution and use, see the accompanying README.ijg
      9  * file.
     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.
     14  * The individual DCT algorithms are kept in separate files to ease
     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, JLONG 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.
     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.
     31  */
     32 
     33 #if BITS_IN_JSAMPLE == 8
     34 #ifndef WITH_SIMD
     35 typedef int DCTELEM;            /* 16 or 32 bits is fine */
     36 typedef unsigned int UDCTELEM;
     37 typedef unsigned long long UDCTELEM2;
     38 #else
     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
     44 typedef JLONG DCTELEM;          /* must have 32 bits */
     45 typedef unsigned long long UDCTELEM2;
     46 #endif
     47 
     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 */
     69 #define IFAST_SCALE_BITS  2     /* fractional bits in scale factors */
     70 #else
     71 typedef JLONG IFAST_MULT_TYPE;  /* need 32 bits for scaled quantizers */
     72 #define IFAST_SCALE_BITS  13    /* fractional bits in scale factors */
     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 
     91 /* Extern declarations for the forward and inverse DCT routines. */
     92 
     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);
     96 
     97 EXTERN(void) jpeg_idct_islow
     98         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
     99          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    100 EXTERN(void) jpeg_idct_ifast
    101         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    102          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    103 EXTERN(void) jpeg_idct_float
    104         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    105          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    106 EXTERN(void) jpeg_idct_7x7
    107         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    108          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    109 EXTERN(void) jpeg_idct_6x6
    110         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    111          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    112 EXTERN(void) jpeg_idct_5x5
    113         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    114          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    115 EXTERN(void) jpeg_idct_4x4
    116         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    117          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    118 EXTERN(void) jpeg_idct_3x3
    119         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    120          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    121 EXTERN(void) jpeg_idct_2x2
    122         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    123          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    124 EXTERN(void) jpeg_idct_1x1
    125         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    126          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    127 EXTERN(void) jpeg_idct_9x9
    128         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    129          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    130 EXTERN(void) jpeg_idct_10x10
    131         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    132          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    133 EXTERN(void) jpeg_idct_11x11
    134         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    135          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    136 EXTERN(void) jpeg_idct_12x12
    137         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    138          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    139 EXTERN(void) jpeg_idct_13x13
    140         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    141          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    142 EXTERN(void) jpeg_idct_14x14
    143         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    144          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    145 EXTERN(void) jpeg_idct_15x15
    146         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    147          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    148 EXTERN(void) jpeg_idct_16x16
    149         (j_decompress_ptr cinfo, jpeg_component_info *compptr,
    150          JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col);
    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 JLONG.
    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 
    163 #define ONE     ((JLONG) 1)
    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 
    171 #define FIX(x)  ((JLONG) ((x) * CONST_SCALE + 0.5))
    172 
    173 /* Descale and correctly round a JLONG 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 a JLONG variable by a JLONG constant to yield a JLONG 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 
    189 #ifdef SHORTxSHORT_32           /* may work if 'int' is 32 bits */
    190 #define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT16) (const)))
    191 #endif
    192 #ifdef SHORTxLCONST_32          /* known to work with Microsoft C 6.0 */
    193 #define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((JLONG) (const)))
    194 #endif
    195 
    196 #ifndef MULTIPLY16C16           /* default definition */
    197 #define MULTIPLY16C16(var,const)  ((var) * (const))
    198 #endif
    199 
    200 /* Same except both inputs are variables. */
    201 
    202 #ifdef SHORTxSHORT_32           /* may work if 'int' is 32 bits */
    203 #define MULTIPLY16V16(var1,var2)  (((INT16) (var1)) * ((INT16) (var2)))
    204 #endif
    205 
    206 #ifndef MULTIPLY16V16           /* default definition */
    207 #define MULTIPLY16V16(var1,var2)  ((var1) * (var2))
    208 #endif
    209