Path: blob/master/thirdparty/libjpeg-turbo/src/jddctmgr.c
9904 views
/*1* jddctmgr.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1996, Thomas G. Lane.5* Modified 2002-2010 by Guido Vollbeding.6* libjpeg-turbo Modifications:7* Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB8* Copyright (C) 2010, 2015, 2022, D. R. Commander.9* Copyright (C) 2013, MIPS Technologies, Inc., California.10* For conditions of distribution and use, see the accompanying README.ijg11* file.12*13* This file contains the inverse-DCT management logic.14* This code selects a particular IDCT implementation to be used,15* and it performs related housekeeping chores. No code in this file16* is executed per IDCT step, only during output pass setup.17*18* Note that the IDCT routines are responsible for performing coefficient19* dequantization as well as the IDCT proper. This module sets up the20* dequantization multiplier table needed by the IDCT routine.21*/2223#define JPEG_INTERNALS24#include "jinclude.h"25#include "jpeglib.h"26#include "jdct.h" /* Private declarations for DCT subsystem */27#include "jsimddct.h"28#include "jpegapicomp.h"293031/*32* The decompressor input side (jdinput.c) saves away the appropriate33* quantization table for each component at the start of the first scan34* involving that component. (This is necessary in order to correctly35* decode files that reuse Q-table slots.)36* When we are ready to make an output pass, the saved Q-table is converted37* to a multiplier table that will actually be used by the IDCT routine.38* The multiplier table contents are IDCT-method-dependent. To support39* application changes in IDCT method between scans, we can remake the40* multiplier tables if necessary.41* In buffered-image mode, the first output pass may occur before any data42* has been seen for some components, and thus before their Q-tables have43* been saved away. To handle this case, multiplier tables are preset44* to zeroes; the result of the IDCT will be a neutral gray level.45*/464748/* Private subobject for this module */4950typedef struct {51struct jpeg_inverse_dct pub; /* public fields */5253/* This array contains the IDCT method code that each multiplier table54* is currently set up for, or -1 if it's not yet set up.55* The actual multiplier tables are pointed to by dct_table in the56* per-component comp_info structures.57*/58int cur_method[MAX_COMPONENTS];59} my_idct_controller;6061typedef my_idct_controller *my_idct_ptr;626364/* Allocated multiplier tables: big enough for any supported variant */6566typedef union {67ISLOW_MULT_TYPE islow_array[DCTSIZE2];68#ifdef DCT_IFAST_SUPPORTED69IFAST_MULT_TYPE ifast_array[DCTSIZE2];70#endif71#ifdef DCT_FLOAT_SUPPORTED72FLOAT_MULT_TYPE float_array[DCTSIZE2];73#endif74} multiplier_table;757677/* The current scaled-IDCT routines require ISLOW-style multiplier tables,78* so be sure to compile that code if either ISLOW or SCALING is requested.79*/80#ifdef DCT_ISLOW_SUPPORTED81#define PROVIDE_ISLOW_TABLES82#else83#ifdef IDCT_SCALING_SUPPORTED84#define PROVIDE_ISLOW_TABLES85#endif86#endif878889/*90* Prepare for an output pass.91* Here we select the proper IDCT routine for each component and build92* a matching multiplier table.93*/9495METHODDEF(void)96start_pass(j_decompress_ptr cinfo)97{98my_idct_ptr idct = (my_idct_ptr)cinfo->idct;99int ci, i;100jpeg_component_info *compptr;101int method = 0;102_inverse_DCT_method_ptr method_ptr = NULL;103JQUANT_TBL *qtbl;104105for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;106ci++, compptr++) {107/* Select the proper IDCT routine for this component's scaling */108switch (compptr->_DCT_scaled_size) {109#ifdef IDCT_SCALING_SUPPORTED110case 1:111method_ptr = _jpeg_idct_1x1;112method = JDCT_ISLOW; /* jidctred uses islow-style table */113break;114case 2:115#ifdef WITH_SIMD116if (jsimd_can_idct_2x2())117method_ptr = jsimd_idct_2x2;118else119#endif120method_ptr = _jpeg_idct_2x2;121method = JDCT_ISLOW; /* jidctred uses islow-style table */122break;123case 3:124method_ptr = _jpeg_idct_3x3;125method = JDCT_ISLOW; /* jidctint uses islow-style table */126break;127case 4:128#ifdef WITH_SIMD129if (jsimd_can_idct_4x4())130method_ptr = jsimd_idct_4x4;131else132#endif133method_ptr = _jpeg_idct_4x4;134method = JDCT_ISLOW; /* jidctred uses islow-style table */135break;136case 5:137method_ptr = _jpeg_idct_5x5;138method = JDCT_ISLOW; /* jidctint uses islow-style table */139break;140case 6:141#if defined(WITH_SIMD) && defined(__mips__)142if (jsimd_can_idct_6x6())143method_ptr = jsimd_idct_6x6;144else145#endif146method_ptr = _jpeg_idct_6x6;147method = JDCT_ISLOW; /* jidctint uses islow-style table */148break;149case 7:150method_ptr = _jpeg_idct_7x7;151method = JDCT_ISLOW; /* jidctint uses islow-style table */152break;153#endif154case DCTSIZE:155switch (cinfo->dct_method) {156#ifdef DCT_ISLOW_SUPPORTED157case JDCT_ISLOW:158#ifdef WITH_SIMD159if (jsimd_can_idct_islow())160method_ptr = jsimd_idct_islow;161else162#endif163method_ptr = _jpeg_idct_islow;164method = JDCT_ISLOW;165break;166#endif167#ifdef DCT_IFAST_SUPPORTED168case JDCT_IFAST:169#ifdef WITH_SIMD170if (jsimd_can_idct_ifast())171method_ptr = jsimd_idct_ifast;172else173#endif174method_ptr = _jpeg_idct_ifast;175method = JDCT_IFAST;176break;177#endif178#ifdef DCT_FLOAT_SUPPORTED179case JDCT_FLOAT:180#ifdef WITH_SIMD181if (jsimd_can_idct_float())182method_ptr = jsimd_idct_float;183else184#endif185method_ptr = _jpeg_idct_float;186method = JDCT_FLOAT;187break;188#endif189default:190ERREXIT(cinfo, JERR_NOT_COMPILED);191break;192}193break;194#ifdef IDCT_SCALING_SUPPORTED195case 9:196method_ptr = _jpeg_idct_9x9;197method = JDCT_ISLOW; /* jidctint uses islow-style table */198break;199case 10:200method_ptr = _jpeg_idct_10x10;201method = JDCT_ISLOW; /* jidctint uses islow-style table */202break;203case 11:204method_ptr = _jpeg_idct_11x11;205method = JDCT_ISLOW; /* jidctint uses islow-style table */206break;207case 12:208#if defined(WITH_SIMD) && defined(__mips__)209if (jsimd_can_idct_12x12())210method_ptr = jsimd_idct_12x12;211else212#endif213method_ptr = _jpeg_idct_12x12;214method = JDCT_ISLOW; /* jidctint uses islow-style table */215break;216case 13:217method_ptr = _jpeg_idct_13x13;218method = JDCT_ISLOW; /* jidctint uses islow-style table */219break;220case 14:221method_ptr = _jpeg_idct_14x14;222method = JDCT_ISLOW; /* jidctint uses islow-style table */223break;224case 15:225method_ptr = _jpeg_idct_15x15;226method = JDCT_ISLOW; /* jidctint uses islow-style table */227break;228case 16:229method_ptr = _jpeg_idct_16x16;230method = JDCT_ISLOW; /* jidctint uses islow-style table */231break;232#endif233default:234ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);235break;236}237idct->pub._inverse_DCT[ci] = method_ptr;238/* Create multiplier table from quant table.239* However, we can skip this if the component is uninteresting240* or if we already built the table. Also, if no quant table241* has yet been saved for the component, we leave the242* multiplier table all-zero; we'll be reading zeroes from the243* coefficient controller's buffer anyway.244*/245if (!compptr->component_needed || idct->cur_method[ci] == method)246continue;247qtbl = compptr->quant_table;248if (qtbl == NULL) /* happens if no data yet for component */249continue;250idct->cur_method[ci] = method;251switch (method) {252#ifdef PROVIDE_ISLOW_TABLES253case JDCT_ISLOW:254{255/* For LL&M IDCT method, multipliers are equal to raw quantization256* coefficients, but are stored as ints to ensure access efficiency.257*/258ISLOW_MULT_TYPE *ismtbl = (ISLOW_MULT_TYPE *)compptr->dct_table;259for (i = 0; i < DCTSIZE2; i++) {260ismtbl[i] = (ISLOW_MULT_TYPE)qtbl->quantval[i];261}262}263break;264#endif265#ifdef DCT_IFAST_SUPPORTED266case JDCT_IFAST:267{268/* For AA&N IDCT method, multipliers are equal to quantization269* coefficients scaled by scalefactor[row]*scalefactor[col], where270* scalefactor[0] = 1271* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7272* For integer operation, the multiplier table is to be scaled by273* IFAST_SCALE_BITS.274*/275IFAST_MULT_TYPE *ifmtbl = (IFAST_MULT_TYPE *)compptr->dct_table;276#define CONST_BITS 14277static const INT16 aanscales[DCTSIZE2] = {278/* precomputed values scaled up by 14 bits */27916384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,28022725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,28121407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,28219266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,28316384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,28412873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,2858867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,2864520, 6270, 5906, 5315, 4520, 3552, 2446, 1247287};288SHIFT_TEMPS289290for (i = 0; i < DCTSIZE2; i++) {291ifmtbl[i] = (IFAST_MULT_TYPE)292DESCALE(MULTIPLY16V16((JLONG)qtbl->quantval[i],293(JLONG)aanscales[i]),294CONST_BITS - IFAST_SCALE_BITS);295}296}297break;298#endif299#ifdef DCT_FLOAT_SUPPORTED300case JDCT_FLOAT:301{302/* For float AA&N IDCT method, multipliers are equal to quantization303* coefficients scaled by scalefactor[row]*scalefactor[col], where304* scalefactor[0] = 1305* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7306*/307FLOAT_MULT_TYPE *fmtbl = (FLOAT_MULT_TYPE *)compptr->dct_table;308int row, col;309static const double aanscalefactor[DCTSIZE] = {3101.0, 1.387039845, 1.306562965, 1.175875602,3111.0, 0.785694958, 0.541196100, 0.275899379312};313314i = 0;315for (row = 0; row < DCTSIZE; row++) {316for (col = 0; col < DCTSIZE; col++) {317fmtbl[i] = (FLOAT_MULT_TYPE)318((double)qtbl->quantval[i] *319aanscalefactor[row] * aanscalefactor[col]);320i++;321}322}323}324break;325#endif326default:327ERREXIT(cinfo, JERR_NOT_COMPILED);328break;329}330}331}332333334/*335* Initialize IDCT manager.336*/337338GLOBAL(void)339_jinit_inverse_dct(j_decompress_ptr cinfo)340{341my_idct_ptr idct;342int ci;343jpeg_component_info *compptr;344345if (cinfo->data_precision != BITS_IN_JSAMPLE)346ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);347348idct = (my_idct_ptr)349(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,350sizeof(my_idct_controller));351cinfo->idct = (struct jpeg_inverse_dct *)idct;352idct->pub.start_pass = start_pass;353354for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;355ci++, compptr++) {356/* Allocate and pre-zero a multiplier table for each component */357compptr->dct_table =358(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,359sizeof(multiplier_table));360memset(compptr->dct_table, 0, sizeof(multiplier_table));361/* Mark multiplier table not yet set up for any method */362idct->cur_method[ci] = -1;363}364}365366367