michael@0: /* michael@0: * jddctmgr.c michael@0: * michael@0: * This file was part of the Independent JPEG Group's software: michael@0: * Copyright (C) 1994-1996, Thomas G. Lane. michael@0: * Modified 2002-2010 by Guido Vollbeding. michael@0: * libjpeg-turbo Modifications: michael@0: * Copyright 2009 Pierre Ossman for Cendio AB michael@0: * Copyright (C) 2010, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains the inverse-DCT management logic. michael@0: * This code selects a particular IDCT implementation to be used, michael@0: * and it performs related housekeeping chores. No code in this file michael@0: * is executed per IDCT step, only during output pass setup. michael@0: * michael@0: * Note that the IDCT routines are responsible for performing coefficient michael@0: * dequantization as well as the IDCT proper. This module sets up the michael@0: * dequantization multiplier table needed by the IDCT routine. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jdct.h" /* Private declarations for DCT subsystem */ michael@0: #include "jsimddct.h" michael@0: #include "jpegcomp.h" michael@0: michael@0: michael@0: /* michael@0: * The decompressor input side (jdinput.c) saves away the appropriate michael@0: * quantization table for each component at the start of the first scan michael@0: * involving that component. (This is necessary in order to correctly michael@0: * decode files that reuse Q-table slots.) michael@0: * When we are ready to make an output pass, the saved Q-table is converted michael@0: * to a multiplier table that will actually be used by the IDCT routine. michael@0: * The multiplier table contents are IDCT-method-dependent. To support michael@0: * application changes in IDCT method between scans, we can remake the michael@0: * multiplier tables if necessary. michael@0: * In buffered-image mode, the first output pass may occur before any data michael@0: * has been seen for some components, and thus before their Q-tables have michael@0: * been saved away. To handle this case, multiplier tables are preset michael@0: * to zeroes; the result of the IDCT will be a neutral gray level. michael@0: */ michael@0: michael@0: michael@0: /* Private subobject for this module */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_inverse_dct pub; /* public fields */ michael@0: michael@0: /* This array contains the IDCT method code that each multiplier table michael@0: * is currently set up for, or -1 if it's not yet set up. michael@0: * The actual multiplier tables are pointed to by dct_table in the michael@0: * per-component comp_info structures. michael@0: */ michael@0: int cur_method[MAX_COMPONENTS]; michael@0: } my_idct_controller; michael@0: michael@0: typedef my_idct_controller * my_idct_ptr; michael@0: michael@0: michael@0: /* Allocated multiplier tables: big enough for any supported variant */ michael@0: michael@0: typedef union { michael@0: ISLOW_MULT_TYPE islow_array[DCTSIZE2]; michael@0: #ifdef DCT_IFAST_SUPPORTED michael@0: IFAST_MULT_TYPE ifast_array[DCTSIZE2]; michael@0: #endif michael@0: #ifdef DCT_FLOAT_SUPPORTED michael@0: FLOAT_MULT_TYPE float_array[DCTSIZE2]; michael@0: #endif michael@0: } multiplier_table; michael@0: michael@0: michael@0: /* The current scaled-IDCT routines require ISLOW-style multiplier tables, michael@0: * so be sure to compile that code if either ISLOW or SCALING is requested. michael@0: */ michael@0: #ifdef DCT_ISLOW_SUPPORTED michael@0: #define PROVIDE_ISLOW_TABLES michael@0: #else michael@0: #ifdef IDCT_SCALING_SUPPORTED michael@0: #define PROVIDE_ISLOW_TABLES michael@0: #endif michael@0: #endif michael@0: michael@0: michael@0: /* michael@0: * Prepare for an output pass. michael@0: * Here we select the proper IDCT routine for each component and build michael@0: * a matching multiplier table. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_pass (j_decompress_ptr cinfo) michael@0: { michael@0: my_idct_ptr idct = (my_idct_ptr) cinfo->idct; michael@0: int ci, i; michael@0: jpeg_component_info *compptr; michael@0: int method = 0; michael@0: inverse_DCT_method_ptr method_ptr = NULL; michael@0: JQUANT_TBL * qtbl; michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* Select the proper IDCT routine for this component's scaling */ michael@0: switch (compptr->_DCT_scaled_size) { michael@0: #ifdef IDCT_SCALING_SUPPORTED michael@0: case 1: michael@0: method_ptr = jpeg_idct_1x1; michael@0: method = JDCT_ISLOW; /* jidctred uses islow-style table */ michael@0: break; michael@0: case 2: michael@0: if (jsimd_can_idct_2x2()) michael@0: method_ptr = jsimd_idct_2x2; michael@0: else michael@0: method_ptr = jpeg_idct_2x2; michael@0: method = JDCT_ISLOW; /* jidctred uses islow-style table */ michael@0: break; michael@0: case 3: michael@0: method_ptr = jpeg_idct_3x3; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 4: michael@0: if (jsimd_can_idct_4x4()) michael@0: method_ptr = jsimd_idct_4x4; michael@0: else michael@0: method_ptr = jpeg_idct_4x4; michael@0: method = JDCT_ISLOW; /* jidctred uses islow-style table */ michael@0: break; michael@0: case 5: michael@0: method_ptr = jpeg_idct_5x5; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 6: michael@0: method_ptr = jpeg_idct_6x6; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 7: michael@0: method_ptr = jpeg_idct_7x7; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: #endif michael@0: case DCTSIZE: michael@0: switch (cinfo->dct_method) { michael@0: #ifdef DCT_ISLOW_SUPPORTED michael@0: case JDCT_ISLOW: michael@0: if (jsimd_can_idct_islow()) michael@0: method_ptr = jsimd_idct_islow; michael@0: else michael@0: method_ptr = jpeg_idct_islow; michael@0: method = JDCT_ISLOW; michael@0: break; michael@0: #endif michael@0: #ifdef DCT_IFAST_SUPPORTED michael@0: case JDCT_IFAST: michael@0: if (jsimd_can_idct_ifast()) michael@0: method_ptr = jsimd_idct_ifast; michael@0: else michael@0: method_ptr = jpeg_idct_ifast; michael@0: method = JDCT_IFAST; michael@0: break; michael@0: #endif michael@0: #ifdef DCT_FLOAT_SUPPORTED michael@0: case JDCT_FLOAT: michael@0: if (jsimd_can_idct_float()) michael@0: method_ptr = jsimd_idct_float; michael@0: else michael@0: method_ptr = jpeg_idct_float; michael@0: method = JDCT_FLOAT; michael@0: break; michael@0: #endif michael@0: default: michael@0: ERREXIT(cinfo, JERR_NOT_COMPILED); michael@0: break; michael@0: } michael@0: break; michael@0: case 9: michael@0: method_ptr = jpeg_idct_9x9; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 10: michael@0: method_ptr = jpeg_idct_10x10; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 11: michael@0: method_ptr = jpeg_idct_11x11; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 12: michael@0: method_ptr = jpeg_idct_12x12; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 13: michael@0: method_ptr = jpeg_idct_13x13; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 14: michael@0: method_ptr = jpeg_idct_14x14; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 15: michael@0: method_ptr = jpeg_idct_15x15; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: case 16: michael@0: method_ptr = jpeg_idct_16x16; michael@0: method = JDCT_ISLOW; /* jidctint uses islow-style table */ michael@0: break; michael@0: default: michael@0: ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size); michael@0: break; michael@0: } michael@0: idct->pub.inverse_DCT[ci] = method_ptr; michael@0: /* Create multiplier table from quant table. michael@0: * However, we can skip this if the component is uninteresting michael@0: * or if we already built the table. Also, if no quant table michael@0: * has yet been saved for the component, we leave the michael@0: * multiplier table all-zero; we'll be reading zeroes from the michael@0: * coefficient controller's buffer anyway. michael@0: */ michael@0: if (! compptr->component_needed || idct->cur_method[ci] == method) michael@0: continue; michael@0: qtbl = compptr->quant_table; michael@0: if (qtbl == NULL) /* happens if no data yet for component */ michael@0: continue; michael@0: idct->cur_method[ci] = method; michael@0: switch (method) { michael@0: #ifdef PROVIDE_ISLOW_TABLES michael@0: case JDCT_ISLOW: michael@0: { michael@0: /* For LL&M IDCT method, multipliers are equal to raw quantization michael@0: * coefficients, but are stored as ints to ensure access efficiency. michael@0: */ michael@0: ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table; michael@0: for (i = 0; i < DCTSIZE2; i++) { michael@0: ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i]; michael@0: } michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef DCT_IFAST_SUPPORTED michael@0: case JDCT_IFAST: michael@0: { michael@0: /* For AA&N IDCT method, multipliers are equal to quantization michael@0: * coefficients scaled by scalefactor[row]*scalefactor[col], where michael@0: * scalefactor[0] = 1 michael@0: * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 michael@0: * For integer operation, the multiplier table is to be scaled by michael@0: * IFAST_SCALE_BITS. michael@0: */ michael@0: IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table; michael@0: #define CONST_BITS 14 michael@0: static const INT16 aanscales[DCTSIZE2] = { michael@0: /* precomputed values scaled up by 14 bits */ michael@0: 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, michael@0: 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, michael@0: 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, michael@0: 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, michael@0: 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, michael@0: 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, michael@0: 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, michael@0: 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 michael@0: }; michael@0: SHIFT_TEMPS michael@0: michael@0: for (i = 0; i < DCTSIZE2; i++) { michael@0: ifmtbl[i] = (IFAST_MULT_TYPE) michael@0: DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], michael@0: (INT32) aanscales[i]), michael@0: CONST_BITS-IFAST_SCALE_BITS); michael@0: } michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef DCT_FLOAT_SUPPORTED michael@0: case JDCT_FLOAT: michael@0: { michael@0: /* For float AA&N IDCT method, multipliers are equal to quantization michael@0: * coefficients scaled by scalefactor[row]*scalefactor[col], where michael@0: * scalefactor[0] = 1 michael@0: * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 michael@0: */ michael@0: FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table; michael@0: int row, col; michael@0: static const double aanscalefactor[DCTSIZE] = { michael@0: 1.0, 1.387039845, 1.306562965, 1.175875602, michael@0: 1.0, 0.785694958, 0.541196100, 0.275899379 michael@0: }; michael@0: michael@0: i = 0; michael@0: for (row = 0; row < DCTSIZE; row++) { michael@0: for (col = 0; col < DCTSIZE; col++) { michael@0: fmtbl[i] = (FLOAT_MULT_TYPE) michael@0: ((double) qtbl->quantval[i] * michael@0: aanscalefactor[row] * aanscalefactor[col]); michael@0: i++; michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: ERREXIT(cinfo, JERR_NOT_COMPILED); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize IDCT manager. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_inverse_dct (j_decompress_ptr cinfo) michael@0: { michael@0: my_idct_ptr idct; michael@0: int ci; michael@0: jpeg_component_info *compptr; michael@0: michael@0: idct = (my_idct_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(my_idct_controller)); michael@0: cinfo->idct = (struct jpeg_inverse_dct *) idct; michael@0: idct->pub.start_pass = start_pass; michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* Allocate and pre-zero a multiplier table for each component */ michael@0: compptr->dct_table = michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(multiplier_table)); michael@0: MEMZERO(compptr->dct_table, SIZEOF(multiplier_table)); michael@0: /* Mark multiplier table not yet set up for any method */ michael@0: idct->cur_method[ci] = -1; michael@0: } michael@0: }