Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * jddctmgr.c |
| 3 | * |
| 4 | * Copyright (C) 1994-1996, Thomas G. Lane. |
| 5 | * Copyright (c) 2010, Code Aurora Forum. All rights reserved. |
| 6 | * This file is part of the Independent JPEG Group's software. |
| 7 | * For conditions of distribution and use, see the accompanying README file. |
| 8 | * |
| 9 | * This file contains the inverse-DCT management logic. |
| 10 | * This code selects a particular IDCT implementation to be used, |
| 11 | * and it performs related housekeeping chores. No code in this file |
| 12 | * is executed per IDCT step, only during output pass setup. |
| 13 | * |
| 14 | * Note that the IDCT routines are responsible for performing coefficient |
| 15 | * dequantization as well as the IDCT proper. This module sets up the |
| 16 | * dequantization multiplier table needed by the IDCT routine. |
| 17 | */ |
| 18 | |
| 19 | #define JPEG_INTERNALS |
| 20 | #include "jinclude.h" |
| 21 | #include "jpeglib.h" |
| 22 | #include "jdct.h" /* Private declarations for DCT subsystem */ |
| 23 | |
| 24 | #ifdef ANDROID_ARMV6_IDCT |
| 25 | #undef ANDROID_ARMV6_IDCT |
| 26 | #ifdef __arm__ |
| 27 | #include <machine/cpu-features.h> |
| 28 | #if __ARM_ARCH__ >= 6 |
| 29 | #define ANDROID_ARMV6_IDCT |
| 30 | #else |
| 31 | #warning "ANDROID_ARMV6_IDCT is disabled" |
| 32 | #endif |
| 33 | #endif |
| 34 | #endif |
| 35 | |
| 36 | #ifdef ANDROID_ARMV6_IDCT |
| 37 | |
| 38 | /* Intentionally declare the prototype with arguments of primitive types instead |
| 39 | * of type-defined ones. This will at least generate some warnings if jmorecfg.h |
| 40 | * is changed and becomes incompatible with the assembly code. |
| 41 | */ |
| 42 | extern void armv6_idct(short *coefs, int *quans, unsigned char **rows, int col); |
| 43 | |
| 44 | void jpeg_idct_armv6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
| 45 | JCOEFPTR coef_block, |
| 46 | JSAMPARRAY output_buf, JDIMENSION output_col) |
| 47 | { |
| 48 | IFAST_MULT_TYPE *dct_table = (IFAST_MULT_TYPE *)compptr->dct_table; |
| 49 | armv6_idct(coef_block, dct_table, output_buf, output_col); |
| 50 | } |
| 51 | |
| 52 | #endif |
| 53 | |
| 54 | /* |
| 55 | * The decompressor input side (jdinput.c) saves away the appropriate |
| 56 | * quantization table for each component at the start of the first scan |
| 57 | * involving that component. (This is necessary in order to correctly |
| 58 | * decode files that reuse Q-table slots.) |
| 59 | * When we are ready to make an output pass, the saved Q-table is converted |
| 60 | * to a multiplier table that will actually be used by the IDCT routine. |
| 61 | * The multiplier table contents are IDCT-method-dependent. To support |
| 62 | * application changes in IDCT method between scans, we can remake the |
| 63 | * multiplier tables if necessary. |
| 64 | * In buffered-image mode, the first output pass may occur before any data |
| 65 | * has been seen for some components, and thus before their Q-tables have |
| 66 | * been saved away. To handle this case, multiplier tables are preset |
| 67 | * to zeroes; the result of the IDCT will be a neutral gray level. |
| 68 | */ |
| 69 | |
| 70 | |
| 71 | /* Private subobject for this module */ |
| 72 | |
| 73 | typedef struct { |
| 74 | struct jpeg_inverse_dct pub; /* public fields */ |
| 75 | |
| 76 | /* This array contains the IDCT method code that each multiplier table |
| 77 | * is currently set up for, or -1 if it's not yet set up. |
| 78 | * The actual multiplier tables are pointed to by dct_table in the |
| 79 | * per-component comp_info structures. |
| 80 | */ |
| 81 | int cur_method[MAX_COMPONENTS]; |
| 82 | } my_idct_controller; |
| 83 | |
| 84 | typedef my_idct_controller * my_idct_ptr; |
| 85 | |
| 86 | |
| 87 | /* Allocated multiplier tables: big enough for any supported variant */ |
| 88 | |
| 89 | typedef union { |
| 90 | ISLOW_MULT_TYPE islow_array[DCTSIZE2]; |
| 91 | #ifdef DCT_IFAST_SUPPORTED |
| 92 | IFAST_MULT_TYPE ifast_array[DCTSIZE2]; |
| 93 | #endif |
| 94 | #ifdef DCT_FLOAT_SUPPORTED |
| 95 | FLOAT_MULT_TYPE float_array[DCTSIZE2]; |
| 96 | #endif |
| 97 | } multiplier_table; |
| 98 | |
| 99 | |
| 100 | /* The current scaled-IDCT routines require ISLOW-style multiplier tables, |
| 101 | * so be sure to compile that code if either ISLOW or SCALING is requested. |
| 102 | */ |
| 103 | #ifdef DCT_ISLOW_SUPPORTED |
| 104 | #define PROVIDE_ISLOW_TABLES |
| 105 | #else |
| 106 | #ifdef IDCT_SCALING_SUPPORTED |
| 107 | #define PROVIDE_ISLOW_TABLES |
| 108 | #endif |
| 109 | #endif |
| 110 | |
| 111 | |
| 112 | /* |
| 113 | * Prepare for an output pass. |
| 114 | * Here we select the proper IDCT routine for each component and build |
| 115 | * a matching multiplier table. |
| 116 | */ |
| 117 | |
| 118 | METHODDEF(void) |
| 119 | start_pass (j_decompress_ptr cinfo) |
| 120 | { |
| 121 | my_idct_ptr idct = (my_idct_ptr) cinfo->idct; |
| 122 | int ci, i; |
| 123 | jpeg_component_info *compptr; |
| 124 | int method = 0; |
| 125 | inverse_DCT_method_ptr method_ptr = NULL; |
| 126 | JQUANT_TBL * qtbl; |
| 127 | |
| 128 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 129 | ci++, compptr++) { |
| 130 | /* Select the proper IDCT routine for this component's scaling */ |
| 131 | switch (compptr->DCT_scaled_size) { |
| 132 | #ifdef IDCT_SCALING_SUPPORTED |
| 133 | case 1: |
| 134 | method_ptr = jpeg_idct_1x1; |
| 135 | method = JDCT_ISLOW; /* jidctred uses islow-style table */ |
| 136 | break; |
| 137 | case 2: |
| 138 | method_ptr = jpeg_idct_2x2; |
| 139 | method = JDCT_ISLOW; /* jidctred uses islow-style table */ |
| 140 | break; |
| 141 | case 4: |
| 142 | method_ptr = jpeg_idct_4x4; |
| 143 | method = JDCT_ISLOW; /* jidctred uses islow-style table */ |
| 144 | break; |
| 145 | #endif |
| 146 | case DCTSIZE: |
| 147 | switch (cinfo->dct_method) { |
| 148 | #ifdef ANDROID_ARMV6_IDCT |
| 149 | case JDCT_ISLOW: |
| 150 | case JDCT_IFAST: |
| 151 | method_ptr = jpeg_idct_armv6; |
| 152 | method = JDCT_IFAST; |
| 153 | break; |
| 154 | #else /* ANDROID_ARMV6_IDCT */ |
| 155 | #ifdef DCT_ISLOW_SUPPORTED |
| 156 | case JDCT_ISLOW: |
| 157 | method_ptr = jpeg_idct_islow; |
| 158 | method = JDCT_ISLOW; |
| 159 | break; |
| 160 | #endif |
| 161 | #ifdef DCT_IFAST_SUPPORTED |
| 162 | case JDCT_IFAST: |
| 163 | #ifdef ANDROID_JPEG_USE_VENUM |
| 164 | /* Use VeNum implementation of jpeg_idct_islow even if fast DCT option is selected */ |
| 165 | method_ptr = jpeg_idct_islow; |
| 166 | method = JDCT_ISLOW; |
| 167 | #else |
| 168 | method_ptr = jpeg_idct_ifast; |
| 169 | method = JDCT_IFAST; |
| 170 | #endif /* ANDROID_JPEG_USE_VENUM */ |
| 171 | break; |
| 172 | #endif |
| 173 | #endif /* ANDROID_ARMV6_IDCT */ |
| 174 | #ifdef DCT_FLOAT_SUPPORTED |
| 175 | case JDCT_FLOAT: |
| 176 | method_ptr = jpeg_idct_float; |
| 177 | method = JDCT_FLOAT; |
| 178 | break; |
| 179 | #endif |
| 180 | default: |
| 181 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 182 | break; |
| 183 | } |
| 184 | break; |
| 185 | default: |
| 186 | ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size); |
| 187 | break; |
| 188 | } |
| 189 | idct->pub.inverse_DCT[ci] = method_ptr; |
| 190 | /* Create multiplier table from quant table. |
| 191 | * However, we can skip this if the component is uninteresting |
| 192 | * or if we already built the table. Also, if no quant table |
| 193 | * has yet been saved for the component, we leave the |
| 194 | * multiplier table all-zero; we'll be reading zeroes from the |
| 195 | * coefficient controller's buffer anyway. |
| 196 | */ |
| 197 | if (! compptr->component_needed || idct->cur_method[ci] == method) |
| 198 | continue; |
| 199 | qtbl = compptr->quant_table; |
| 200 | if (qtbl == NULL) /* happens if no data yet for component */ |
| 201 | continue; |
| 202 | idct->cur_method[ci] = method; |
| 203 | switch (method) { |
| 204 | #ifdef PROVIDE_ISLOW_TABLES |
| 205 | case JDCT_ISLOW: |
| 206 | { |
| 207 | /* For LL&M IDCT method, multipliers are equal to raw quantization |
| 208 | * coefficients, but are stored as ints to ensure access efficiency. |
| 209 | */ |
| 210 | ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table; |
| 211 | for (i = 0; i < DCTSIZE2; i++) { |
| 212 | ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i]; |
| 213 | } |
| 214 | } |
| 215 | break; |
| 216 | #endif |
| 217 | #ifdef DCT_IFAST_SUPPORTED |
| 218 | case JDCT_IFAST: |
| 219 | { |
| 220 | /* For AA&N IDCT method, multipliers are equal to quantization |
| 221 | * coefficients scaled by scalefactor[row]*scalefactor[col], where |
| 222 | * scalefactor[0] = 1 |
| 223 | * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 |
| 224 | * For integer operation, the multiplier table is to be scaled by |
| 225 | * IFAST_SCALE_BITS. |
| 226 | */ |
| 227 | IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table; |
| 228 | #ifdef ANDROID_ARMV6_IDCT |
| 229 | /* Precomputed values scaled up by 15 bits. */ |
| 230 | static const unsigned short scales[DCTSIZE2] = { |
| 231 | 32768, 45451, 42813, 38531, 32768, 25746, 17734, 9041, |
| 232 | 45451, 63042, 59384, 53444, 45451, 35710, 24598, 12540, |
| 233 | 42813, 59384, 55938, 50343, 42813, 33638, 23170, 11812, |
| 234 | 38531, 53444, 50343, 45308, 38531, 30274, 20853, 10631, |
| 235 | 32768, 45451, 42813, 38531, 32768, 25746, 17734, 9041, |
| 236 | 25746, 35710, 33638, 30274, 25746, 20228, 13933, 7103, |
| 237 | 17734, 24598, 23170, 20853, 17734, 13933, 9598, 4893, |
| 238 | 9041, 12540, 11812, 10631, 9041, 7103, 4893, 2494, |
| 239 | }; |
| 240 | /* Inverse map of [7, 5, 1, 3, 0, 2, 4, 6]. */ |
| 241 | static const char orders[DCTSIZE] = {4, 2, 5, 3, 6, 1, 7, 0}; |
| 242 | /* Reorder the columns after transposing. */ |
| 243 | for (i = 0; i < DCTSIZE2; ++i) { |
| 244 | int j = ((i & 7) << 3) + orders[i >> 3]; |
| 245 | ifmtbl[j] = (qtbl->quantval[i] * scales[i] + 2) >> 2; |
| 246 | } |
| 247 | #else /* ANDROID_ARMV6_IDCT */ |
| 248 | |
| 249 | #define CONST_BITS 14 |
| 250 | static const INT16 aanscales[DCTSIZE2] = { |
| 251 | /* precomputed values scaled up by 14 bits */ |
| 252 | 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, |
| 253 | 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, |
| 254 | 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, |
| 255 | 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, |
| 256 | 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, |
| 257 | 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, |
| 258 | 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, |
| 259 | 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 |
| 260 | }; |
| 261 | SHIFT_TEMPS |
| 262 | |
| 263 | for (i = 0; i < DCTSIZE2; i++) { |
| 264 | ifmtbl[i] = (IFAST_MULT_TYPE) |
| 265 | DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], |
| 266 | (INT32) aanscales[i]), |
| 267 | CONST_BITS-IFAST_SCALE_BITS); |
| 268 | } |
| 269 | #endif /* ANDROID_ARMV6_IDCT */ |
| 270 | } |
| 271 | break; |
| 272 | #endif |
| 273 | #ifdef DCT_FLOAT_SUPPORTED |
| 274 | case JDCT_FLOAT: |
| 275 | { |
| 276 | /* For float AA&N IDCT method, multipliers are equal to quantization |
| 277 | * coefficients scaled by scalefactor[row]*scalefactor[col], where |
| 278 | * scalefactor[0] = 1 |
| 279 | * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 |
| 280 | */ |
| 281 | FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table; |
| 282 | int row, col; |
| 283 | static const double aanscalefactor[DCTSIZE] = { |
| 284 | 1.0, 1.387039845, 1.306562965, 1.175875602, |
| 285 | 1.0, 0.785694958, 0.541196100, 0.275899379 |
| 286 | }; |
| 287 | |
| 288 | i = 0; |
| 289 | for (row = 0; row < DCTSIZE; row++) { |
| 290 | for (col = 0; col < DCTSIZE; col++) { |
| 291 | fmtbl[i] = (FLOAT_MULT_TYPE) |
| 292 | ((double) qtbl->quantval[i] * |
| 293 | aanscalefactor[row] * aanscalefactor[col]); |
| 294 | i++; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | break; |
| 299 | #endif |
| 300 | default: |
| 301 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /* |
| 309 | * Initialize IDCT manager. |
| 310 | */ |
| 311 | |
| 312 | GLOBAL(void) |
| 313 | jinit_inverse_dct (j_decompress_ptr cinfo) |
| 314 | { |
| 315 | my_idct_ptr idct; |
| 316 | int ci; |
| 317 | jpeg_component_info *compptr; |
| 318 | |
| 319 | idct = (my_idct_ptr) |
| 320 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 321 | SIZEOF(my_idct_controller)); |
| 322 | cinfo->idct = (struct jpeg_inverse_dct *) idct; |
| 323 | idct->pub.start_pass = start_pass; |
| 324 | |
| 325 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 326 | ci++, compptr++) { |
| 327 | /* Allocate and pre-zero a multiplier table for each component */ |
| 328 | compptr->dct_table = |
| 329 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 330 | SIZEOF(multiplier_table)); |
| 331 | MEMZERO(compptr->dct_table, SIZEOF(multiplier_table)); |
| 332 | /* Mark multiplier table not yet set up for any method */ |
| 333 | idct->cur_method[ci] = -1; |
| 334 | } |
| 335 | } |