Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jddctmgr.c |
| 3 | * |
DRC | a73e870 | 2012-12-31 02:52:30 +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 | a73e870 | 2012-12-31 02:52:30 +0000 | [diff] [blame] | 6 | * Modifications: |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 7 | * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 8 | * Copyright (C) 2010, D. R. Commander. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 9 | * For conditions of distribution and use, see the accompanying README file. |
| 10 | * |
| 11 | * This file contains the inverse-DCT management logic. |
| 12 | * This code selects a particular IDCT implementation to be used, |
| 13 | * and it performs related housekeeping chores. No code in this file |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 14 | * is executed per IDCT step, only during output pass setup. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 15 | * |
| 16 | * Note that the IDCT routines are responsible for performing coefficient |
| 17 | * dequantization as well as the IDCT proper. This module sets up the |
| 18 | * dequantization multiplier table needed by the IDCT routine. |
| 19 | */ |
| 20 | |
| 21 | #define JPEG_INTERNALS |
| 22 | #include "jinclude.h" |
| 23 | #include "jpeglib.h" |
| 24 | #include "jdct.h" /* Private declarations for DCT subsystem */ |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 25 | #include "jsimddct.h" |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 26 | #include "jpegcomp.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 27 | |
| 28 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 29 | /* |
| 30 | * The decompressor input side (jdinput.c) saves away the appropriate |
| 31 | * quantization table for each component at the start of the first scan |
| 32 | * involving that component. (This is necessary in order to correctly |
| 33 | * decode files that reuse Q-table slots.) |
| 34 | * When we are ready to make an output pass, the saved Q-table is converted |
| 35 | * to a multiplier table that will actually be used by the IDCT routine. |
| 36 | * The multiplier table contents are IDCT-method-dependent. To support |
| 37 | * application changes in IDCT method between scans, we can remake the |
| 38 | * multiplier tables if necessary. |
| 39 | * In buffered-image mode, the first output pass may occur before any data |
| 40 | * has been seen for some components, and thus before their Q-tables have |
| 41 | * been saved away. To handle this case, multiplier tables are preset |
| 42 | * to zeroes; the result of the IDCT will be a neutral gray level. |
| 43 | */ |
| 44 | |
| 45 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 46 | /* Private subobject for this module */ |
| 47 | |
| 48 | typedef struct { |
| 49 | struct jpeg_inverse_dct pub; /* public fields */ |
| 50 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 51 | /* This array contains the IDCT method code that each multiplier table |
| 52 | * is currently set up for, or -1 if it's not yet set up. |
| 53 | * The actual multiplier tables are pointed to by dct_table in the |
| 54 | * per-component comp_info structures. |
| 55 | */ |
| 56 | int cur_method[MAX_COMPONENTS]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 57 | } my_idct_controller; |
| 58 | |
| 59 | typedef my_idct_controller * my_idct_ptr; |
| 60 | |
| 61 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 62 | /* Allocated multiplier tables: big enough for any supported variant */ |
| 63 | |
| 64 | typedef union { |
| 65 | ISLOW_MULT_TYPE islow_array[DCTSIZE2]; |
| 66 | #ifdef DCT_IFAST_SUPPORTED |
| 67 | IFAST_MULT_TYPE ifast_array[DCTSIZE2]; |
| 68 | #endif |
| 69 | #ifdef DCT_FLOAT_SUPPORTED |
| 70 | FLOAT_MULT_TYPE float_array[DCTSIZE2]; |
| 71 | #endif |
| 72 | } multiplier_table; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | /* The current scaled-IDCT routines require ISLOW-style multiplier tables, |
| 76 | * so be sure to compile that code if either ISLOW or SCALING is requested. |
| 77 | */ |
| 78 | #ifdef DCT_ISLOW_SUPPORTED |
| 79 | #define PROVIDE_ISLOW_TABLES |
| 80 | #else |
| 81 | #ifdef IDCT_SCALING_SUPPORTED |
| 82 | #define PROVIDE_ISLOW_TABLES |
| 83 | #endif |
| 84 | #endif |
| 85 | |
| 86 | |
| 87 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 88 | * Prepare for an output pass. |
| 89 | * Here we select the proper IDCT routine for each component and build |
| 90 | * a matching multiplier table. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 91 | */ |
| 92 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 93 | METHODDEF(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 94 | start_pass (j_decompress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 95 | { |
| 96 | my_idct_ptr idct = (my_idct_ptr) cinfo->idct; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 97 | int ci, i; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 98 | jpeg_component_info *compptr; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 99 | int method = 0; |
| 100 | inverse_DCT_method_ptr method_ptr = NULL; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 101 | JQUANT_TBL * qtbl; |
| 102 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 103 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 104 | ci++, compptr++) { |
| 105 | /* Select the proper IDCT routine for this component's scaling */ |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 106 | switch (compptr->_DCT_scaled_size) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 107 | #ifdef IDCT_SCALING_SUPPORTED |
| 108 | case 1: |
| 109 | method_ptr = jpeg_idct_1x1; |
| 110 | method = JDCT_ISLOW; /* jidctred uses islow-style table */ |
| 111 | break; |
| 112 | case 2: |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 113 | if (jsimd_can_idct_2x2()) |
| 114 | method_ptr = jsimd_idct_2x2; |
| 115 | else |
| 116 | method_ptr = jpeg_idct_2x2; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 117 | method = JDCT_ISLOW; /* jidctred uses islow-style table */ |
| 118 | break; |
| 119 | case 4: |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 120 | if (jsimd_can_idct_4x4()) |
| 121 | method_ptr = jsimd_idct_4x4; |
| 122 | else |
| 123 | method_ptr = jpeg_idct_4x4; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 124 | method = JDCT_ISLOW; /* jidctred uses islow-style table */ |
| 125 | break; |
| 126 | #endif |
| 127 | case DCTSIZE: |
| 128 | switch (cinfo->dct_method) { |
| 129 | #ifdef DCT_ISLOW_SUPPORTED |
| 130 | case JDCT_ISLOW: |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 131 | if (jsimd_can_idct_islow()) |
| 132 | method_ptr = jsimd_idct_islow; |
| 133 | else |
| 134 | method_ptr = jpeg_idct_islow; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 135 | method = JDCT_ISLOW; |
| 136 | break; |
| 137 | #endif |
| 138 | #ifdef DCT_IFAST_SUPPORTED |
| 139 | case JDCT_IFAST: |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 140 | if (jsimd_can_idct_ifast()) |
| 141 | method_ptr = jsimd_idct_ifast; |
| 142 | else |
| 143 | method_ptr = jpeg_idct_ifast; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 144 | method = JDCT_IFAST; |
| 145 | break; |
| 146 | #endif |
| 147 | #ifdef DCT_FLOAT_SUPPORTED |
| 148 | case JDCT_FLOAT: |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 149 | if (jsimd_can_idct_float()) |
| 150 | method_ptr = jsimd_idct_float; |
| 151 | else |
| 152 | method_ptr = jpeg_idct_float; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 153 | method = JDCT_FLOAT; |
| 154 | break; |
| 155 | #endif |
| 156 | default: |
| 157 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 158 | break; |
| 159 | } |
| 160 | break; |
| 161 | default: |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 162 | ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | idct->pub.inverse_DCT[ci] = method_ptr; |
| 166 | /* Create multiplier table from quant table. |
| 167 | * However, we can skip this if the component is uninteresting |
| 168 | * or if we already built the table. Also, if no quant table |
| 169 | * has yet been saved for the component, we leave the |
| 170 | * multiplier table all-zero; we'll be reading zeroes from the |
| 171 | * coefficient controller's buffer anyway. |
| 172 | */ |
| 173 | if (! compptr->component_needed || idct->cur_method[ci] == method) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 174 | continue; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 175 | qtbl = compptr->quant_table; |
| 176 | if (qtbl == NULL) /* happens if no data yet for component */ |
| 177 | continue; |
| 178 | idct->cur_method[ci] = method; |
| 179 | switch (method) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 180 | #ifdef PROVIDE_ISLOW_TABLES |
| 181 | case JDCT_ISLOW: |
| 182 | { |
| 183 | /* For LL&M IDCT method, multipliers are equal to raw quantization |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 184 | * coefficients, but are stored as ints to ensure access efficiency. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 185 | */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 186 | ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 187 | for (i = 0; i < DCTSIZE2; i++) { |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 188 | ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | break; |
| 192 | #endif |
| 193 | #ifdef DCT_IFAST_SUPPORTED |
| 194 | case JDCT_IFAST: |
| 195 | { |
| 196 | /* For AA&N IDCT method, multipliers are equal to quantization |
| 197 | * coefficients scaled by scalefactor[row]*scalefactor[col], where |
| 198 | * scalefactor[0] = 1 |
| 199 | * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 |
| 200 | * For integer operation, the multiplier table is to be scaled by |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 201 | * IFAST_SCALE_BITS. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 202 | */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 203 | IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 204 | #define CONST_BITS 14 |
| 205 | static const INT16 aanscales[DCTSIZE2] = { |
| 206 | /* precomputed values scaled up by 14 bits */ |
| 207 | 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, |
| 208 | 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, |
| 209 | 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, |
| 210 | 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, |
| 211 | 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, |
| 212 | 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, |
| 213 | 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, |
| 214 | 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 |
| 215 | }; |
| 216 | SHIFT_TEMPS |
| 217 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 218 | for (i = 0; i < DCTSIZE2; i++) { |
| 219 | ifmtbl[i] = (IFAST_MULT_TYPE) |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 220 | DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 221 | (INT32) aanscales[i]), |
| 222 | CONST_BITS-IFAST_SCALE_BITS); |
| 223 | } |
| 224 | } |
| 225 | break; |
| 226 | #endif |
| 227 | #ifdef DCT_FLOAT_SUPPORTED |
| 228 | case JDCT_FLOAT: |
| 229 | { |
| 230 | /* For float AA&N IDCT method, multipliers are equal to quantization |
| 231 | * coefficients scaled by scalefactor[row]*scalefactor[col], where |
| 232 | * scalefactor[0] = 1 |
| 233 | * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 234 | */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 235 | FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 236 | int row, col; |
| 237 | static const double aanscalefactor[DCTSIZE] = { |
| 238 | 1.0, 1.387039845, 1.306562965, 1.175875602, |
| 239 | 1.0, 0.785694958, 0.541196100, 0.275899379 |
| 240 | }; |
| 241 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 242 | i = 0; |
| 243 | for (row = 0; row < DCTSIZE; row++) { |
| 244 | for (col = 0; col < DCTSIZE; col++) { |
| 245 | fmtbl[i] = (FLOAT_MULT_TYPE) |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 246 | ((double) qtbl->quantval[i] * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 247 | aanscalefactor[row] * aanscalefactor[col]); |
| 248 | i++; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | break; |
| 253 | #endif |
| 254 | default: |
| 255 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 263 | * Initialize IDCT manager. |
| 264 | */ |
| 265 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 266 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 267 | jinit_inverse_dct (j_decompress_ptr cinfo) |
| 268 | { |
| 269 | my_idct_ptr idct; |
| 270 | int ci; |
| 271 | jpeg_component_info *compptr; |
| 272 | |
| 273 | idct = (my_idct_ptr) |
| 274 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 275 | SIZEOF(my_idct_controller)); |
| 276 | cinfo->idct = (struct jpeg_inverse_dct *) idct; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 277 | idct->pub.start_pass = start_pass; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 278 | |
| 279 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 280 | ci++, compptr++) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 281 | /* Allocate and pre-zero a multiplier table for each component */ |
| 282 | compptr->dct_table = |
| 283 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 284 | SIZEOF(multiplier_table)); |
| 285 | MEMZERO(compptr->dct_table, SIZEOF(multiplier_table)); |
| 286 | /* Mark multiplier table not yet set up for any method */ |
| 287 | idct->cur_method[ci] = -1; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 288 | } |
| 289 | } |