/*1* jidctfst.c2*3* Copyright (C) 1994-1998, Thomas G. Lane.4* Modified 2015-2017 by Guido Vollbeding.5* This file is part of the Independent JPEG Group's software.6* For conditions of distribution and use, see the accompanying README file.7*8* This file contains a fast, not so accurate integer implementation of the9* inverse DCT (Discrete Cosine Transform). In the IJG code, this routine10* must also perform dequantization of the input coefficients.11*12* A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT13* on each row (or vice versa, but it's more convenient to emit a row at14* a time). Direct algorithms are also available, but they are much more15* complex and seem not to be any faster when reduced to code.16*17* This implementation is based on Arai, Agui, and Nakajima's algorithm for18* scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in19* Japanese, but the algorithm is described in the Pennebaker & Mitchell20* JPEG textbook (see REFERENCES section in file README). The following code21* is based directly on figure 4-8 in P&M.22* While an 8-point DCT cannot be done in less than 11 multiplies, it is23* possible to arrange the computation so that many of the multiplies are24* simple scalings of the final outputs. These multiplies can then be25* folded into the multiplications or divisions by the JPEG quantization26* table entries. The AA&N method leaves only 5 multiplies and 29 adds27* to be done in the DCT itself.28* The primary disadvantage of this method is that with fixed-point math,29* accuracy is lost due to imprecise representation of the scaled30* quantization values. The smaller the quantization table entry, the less31* precise the scaled value, so this implementation does worse with high-32* quality-setting files than with low-quality ones.33*/3435#define JPEG_INTERNALS36#include "jinclude.h"37#include "jpeglib.h"38#include "jdct.h" /* Private declarations for DCT subsystem */3940#ifdef DCT_IFAST_SUPPORTED414243/*44* This module is specialized to the case DCTSIZE = 8.45*/4647#if DCTSIZE != 848Sorry, this code only copes with 8x8 DCT blocks. /* deliberate syntax err */49#endif505152/* Scaling decisions are generally the same as in the LL&M algorithm;53* see jidctint.c for more details. However, we choose to descale54* (right shift) multiplication products as soon as they are formed,55* rather than carrying additional fractional bits into subsequent additions.56* This compromises accuracy slightly, but it lets us save a few shifts.57* More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)58* everywhere except in the multiplications proper; this saves a good deal59* of work on 16-bit-int machines.60*61* The dequantized coefficients are not integers because the AA&N scaling62* factors have been incorporated. We represent them scaled up by PASS1_BITS,63* so that the first and second IDCT rounds have the same input scaling.64* For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to65* avoid a descaling shift; this compromises accuracy rather drastically66* for small quantization table entries, but it saves a lot of shifts.67* For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,68* so we use a much larger scaling factor to preserve accuracy.69*70* A final compromise is to represent the multiplicative constants to only71* 8 fractional bits, rather than 13. This saves some shifting work on some72* machines, and may also reduce the cost of multiplication (since there73* are fewer one-bits in the constants).74*/7576#if BITS_IN_JSAMPLE == 877#define CONST_BITS 878#define PASS1_BITS 279#else80#define CONST_BITS 881#define PASS1_BITS 1 /* lose a little precision to avoid overflow */82#endif8384/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus85* causing a lot of useless floating-point operations at run time.86* To get around this we use the following pre-calculated constants.87* If you change CONST_BITS you may want to add appropriate values.88* (With a reasonable C compiler, you can just rely on the FIX() macro...)89*/9091#if CONST_BITS == 892#define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */93#define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */94#define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */95#define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */96#else97#define FIX_1_082392200 FIX(1.082392200)98#define FIX_1_414213562 FIX(1.414213562)99#define FIX_1_847759065 FIX(1.847759065)100#define FIX_2_613125930 FIX(2.613125930)101#endif102103104/* We can gain a little more speed, with a further compromise in accuracy,105* by omitting the addition in a descaling shift. This yields an incorrectly106* rounded result half the time...107*/108109#ifndef USE_ACCURATE_ROUNDING110#undef DESCALE111#define DESCALE(x,n) RIGHT_SHIFT(x, n)112#endif113114115/* Multiply a DCTELEM variable by an INT32 constant, and immediately116* descale to yield a DCTELEM result.117*/118119#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))120121122/* Dequantize a coefficient by multiplying it by the multiplier-table123* entry; produce a DCTELEM result. For 8-bit data a 16x16->16124* multiplication will do. For 12-bit data, the multiplier table is125* declared INT32, so a 32-bit multiply will be used.126*/127128#if BITS_IN_JSAMPLE == 8129#define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval))130#else131#define DEQUANTIZE(coef,quantval) \132DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)133#endif134135136/*137* Perform dequantization and inverse DCT on one block of coefficients.138*139* cK represents cos(K*pi/16).140*/141142GLOBAL(void)143jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,144JCOEFPTR coef_block,145JSAMPARRAY output_buf, JDIMENSION output_col)146{147DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;148DCTELEM tmp10, tmp11, tmp12, tmp13;149DCTELEM z5, z10, z11, z12, z13;150JCOEFPTR inptr;151IFAST_MULT_TYPE * quantptr;152int * wsptr;153JSAMPROW outptr;154JSAMPLE *range_limit = IDCT_range_limit(cinfo);155int ctr;156int workspace[DCTSIZE2]; /* buffers data between passes */157SHIFT_TEMPS /* for DESCALE */158ISHIFT_TEMPS /* for IRIGHT_SHIFT */159160/* Pass 1: process columns from input, store into work array. */161162inptr = coef_block;163quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;164wsptr = workspace;165for (ctr = DCTSIZE; ctr > 0; ctr--) {166/* Due to quantization, we will usually find that many of the input167* coefficients are zero, especially the AC terms. We can exploit this168* by short-circuiting the IDCT calculation for any column in which all169* the AC terms are zero. In that case each output is equal to the170* DC coefficient (with scale factor as needed).171* With typical images and quantization tables, half or more of the172* column DCT calculations can be simplified this way.173*/174175if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&176inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&177inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&178inptr[DCTSIZE*7] == 0) {179/* AC terms all zero */180int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);181182wsptr[DCTSIZE*0] = dcval;183wsptr[DCTSIZE*1] = dcval;184wsptr[DCTSIZE*2] = dcval;185wsptr[DCTSIZE*3] = dcval;186wsptr[DCTSIZE*4] = dcval;187wsptr[DCTSIZE*5] = dcval;188wsptr[DCTSIZE*6] = dcval;189wsptr[DCTSIZE*7] = dcval;190191inptr++; /* advance pointers to next column */192quantptr++;193wsptr++;194continue;195}196197/* Even part */198199tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);200tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);201tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);202tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);203204tmp10 = tmp0 + tmp2; /* phase 3 */205tmp11 = tmp0 - tmp2;206207tmp13 = tmp1 + tmp3; /* phases 5-3 */208tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */209210tmp0 = tmp10 + tmp13; /* phase 2 */211tmp3 = tmp10 - tmp13;212tmp1 = tmp11 + tmp12;213tmp2 = tmp11 - tmp12;214215/* Odd part */216217tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);218tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);219tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);220tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);221222z13 = tmp6 + tmp5; /* phase 6 */223z10 = tmp6 - tmp5;224z11 = tmp4 + tmp7;225z12 = tmp4 - tmp7;226227tmp7 = z11 + z13; /* phase 5 */228tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */229230z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */231tmp10 = z5 - MULTIPLY(z12, FIX_1_082392200); /* 2*(c2-c6) */232tmp12 = z5 - MULTIPLY(z10, FIX_2_613125930); /* 2*(c2+c6) */233234tmp6 = tmp12 - tmp7; /* phase 2 */235tmp5 = tmp11 - tmp6;236tmp4 = tmp10 - tmp5;237238wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);239wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);240wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);241wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);242wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);243wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);244wsptr[DCTSIZE*3] = (int) (tmp3 + tmp4);245wsptr[DCTSIZE*4] = (int) (tmp3 - tmp4);246247inptr++; /* advance pointers to next column */248quantptr++;249wsptr++;250}251252/* Pass 2: process rows from work array, store into output array.253* Note that we must descale the results by a factor of 8 == 2**3,254* and also undo the PASS1_BITS scaling.255*/256257wsptr = workspace;258for (ctr = 0; ctr < DCTSIZE; ctr++) {259outptr = output_buf[ctr] + output_col;260261/* Add range center and fudge factor for final descale and range-limit. */262z5 = (DCTELEM) wsptr[0] +263((((DCTELEM) RANGE_CENTER) << (PASS1_BITS+3)) +264(1 << (PASS1_BITS+2)));265266/* Rows of zeroes can be exploited in the same way as we did with columns.267* However, the column calculation has created many nonzero AC terms, so268* the simplification applies less often (typically 5% to 10% of the time).269* On machines with very fast multiplication, it's possible that the270* test takes more time than it's worth. In that case this section271* may be commented out.272*/273274#ifndef NO_ZERO_ROW_TEST275if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&276wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {277/* AC terms all zero */278JSAMPLE dcval = range_limit[(int) IRIGHT_SHIFT(z5, PASS1_BITS+3)279& RANGE_MASK];280281outptr[0] = dcval;282outptr[1] = dcval;283outptr[2] = dcval;284outptr[3] = dcval;285outptr[4] = dcval;286outptr[5] = dcval;287outptr[6] = dcval;288outptr[7] = dcval;289290wsptr += DCTSIZE; /* advance pointer to next row */291continue;292}293#endif294295/* Even part */296297tmp10 = z5 + (DCTELEM) wsptr[4];298tmp11 = z5 - (DCTELEM) wsptr[4];299300tmp13 = (DCTELEM) wsptr[2] + (DCTELEM) wsptr[6];301tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6],302FIX_1_414213562) - tmp13; /* 2*c4 */303304tmp0 = tmp10 + tmp13;305tmp3 = tmp10 - tmp13;306tmp1 = tmp11 + tmp12;307tmp2 = tmp11 - tmp12;308309/* Odd part */310311z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];312z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];313z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];314z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];315316tmp7 = z11 + z13; /* phase 5 */317tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */318319z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */320tmp10 = z5 - MULTIPLY(z12, FIX_1_082392200); /* 2*(c2-c6) */321tmp12 = z5 - MULTIPLY(z10, FIX_2_613125930); /* 2*(c2+c6) */322323tmp6 = tmp12 - tmp7; /* phase 2 */324tmp5 = tmp11 - tmp6;325tmp4 = tmp10 - tmp5;326327/* Final output stage: scale down by a factor of 8 and range-limit */328329outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp7, PASS1_BITS+3)330& RANGE_MASK];331outptr[7] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp7, PASS1_BITS+3)332& RANGE_MASK];333outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp1 + tmp6, PASS1_BITS+3)334& RANGE_MASK];335outptr[6] = range_limit[(int) IRIGHT_SHIFT(tmp1 - tmp6, PASS1_BITS+3)336& RANGE_MASK];337outptr[2] = range_limit[(int) IRIGHT_SHIFT(tmp2 + tmp5, PASS1_BITS+3)338& RANGE_MASK];339outptr[5] = range_limit[(int) IRIGHT_SHIFT(tmp2 - tmp5, PASS1_BITS+3)340& RANGE_MASK];341outptr[3] = range_limit[(int) IRIGHT_SHIFT(tmp3 + tmp4, PASS1_BITS+3)342& RANGE_MASK];343outptr[4] = range_limit[(int) IRIGHT_SHIFT(tmp3 - tmp4, PASS1_BITS+3)344& RANGE_MASK];345346wsptr += DCTSIZE; /* advance pointer to next row */347}348}349350#endif /* DCT_IFAST_SUPPORTED */351352353