Path: blob/master/src/java.desktop/share/native/libjavajpeg/jidctfst.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jidctfst.c6*7* Copyright (C) 1994-1998, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains a fast, not so accurate integer implementation of the12* inverse DCT (Discrete Cosine Transform). In the IJG code, this routine13* must also perform dequantization of the input coefficients.14*15* A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT16* on each row (or vice versa, but it's more convenient to emit a row at17* a time). Direct algorithms are also available, but they are much more18* complex and seem not to be any faster when reduced to code.19*20* This implementation is based on Arai, Agui, and Nakajima's algorithm for21* scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in22* Japanese, but the algorithm is described in the Pennebaker & Mitchell23* JPEG textbook (see REFERENCES section in file README). The following code24* is based directly on figure 4-8 in P&M.25* While an 8-point DCT cannot be done in less than 11 multiplies, it is26* possible to arrange the computation so that many of the multiplies are27* simple scalings of the final outputs. These multiplies can then be28* folded into the multiplications or divisions by the JPEG quantization29* table entries. The AA&N method leaves only 5 multiplies and 29 adds30* to be done in the DCT itself.31* The primary disadvantage of this method is that with fixed-point math,32* accuracy is lost due to imprecise representation of the scaled33* quantization values. The smaller the quantization table entry, the less34* precise the scaled value, so this implementation does worse with high-35* quality-setting files than with low-quality ones.36*/3738#define JPEG_INTERNALS39#include "jinclude.h"40#include "jpeglib.h"41#include "jdct.h" /* Private declarations for DCT subsystem */4243#ifdef DCT_IFAST_SUPPORTED444546/*47* This module is specialized to the case DCTSIZE = 8.48*/4950#if DCTSIZE != 851Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */52#endif535455/* Scaling decisions are generally the same as in the LL&M algorithm;56* see jidctint.c for more details. However, we choose to descale57* (right shift) multiplication products as soon as they are formed,58* rather than carrying additional fractional bits into subsequent additions.59* This compromises accuracy slightly, but it lets us save a few shifts.60* More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)61* everywhere except in the multiplications proper; this saves a good deal62* of work on 16-bit-int machines.63*64* The dequantized coefficients are not integers because the AA&N scaling65* factors have been incorporated. We represent them scaled up by PASS1_BITS,66* so that the first and second IDCT rounds have the same input scaling.67* For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to68* avoid a descaling shift; this compromises accuracy rather drastically69* for small quantization table entries, but it saves a lot of shifts.70* For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,71* so we use a much larger scaling factor to preserve accuracy.72*73* A final compromise is to represent the multiplicative constants to only74* 8 fractional bits, rather than 13. This saves some shifting work on some75* machines, and may also reduce the cost of multiplication (since there76* are fewer one-bits in the constants).77*/7879#if BITS_IN_JSAMPLE == 880#define CONST_BITS 881#define PASS1_BITS 282#else83#define CONST_BITS 884#define PASS1_BITS 1 /* lose a little precision to avoid overflow */85#endif8687/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus88* causing a lot of useless floating-point operations at run time.89* To get around this we use the following pre-calculated constants.90* If you change CONST_BITS you may want to add appropriate values.91* (With a reasonable C compiler, you can just rely on the FIX() macro...)92*/9394#if CONST_BITS == 895#define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */96#define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */97#define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */98#define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */99#else100#define FIX_1_082392200 FIX(1.082392200)101#define FIX_1_414213562 FIX(1.414213562)102#define FIX_1_847759065 FIX(1.847759065)103#define FIX_2_613125930 FIX(2.613125930)104#endif105106107/* We can gain a little more speed, with a further compromise in accuracy,108* by omitting the addition in a descaling shift. This yields an incorrectly109* rounded result half the time...110*/111112#ifndef USE_ACCURATE_ROUNDING113#undef DESCALE114#define DESCALE(x,n) RIGHT_SHIFT(x, n)115#endif116117118/* Multiply a DCTELEM variable by an INT32 constant, and immediately119* descale to yield a DCTELEM result.120*/121122#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))123124125/* Dequantize a coefficient by multiplying it by the multiplier-table126* entry; produce a DCTELEM result. For 8-bit data a 16x16->16127* multiplication will do. For 12-bit data, the multiplier table is128* declared INT32, so a 32-bit multiply will be used.129*/130131#if BITS_IN_JSAMPLE == 8132#define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval))133#else134#define DEQUANTIZE(coef,quantval) \135DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)136#endif137138139/* Like DESCALE, but applies to a DCTELEM and produces an int.140* We assume that int right shift is unsigned if INT32 right shift is.141*/142143#ifdef RIGHT_SHIFT_IS_UNSIGNED144#define ISHIFT_TEMPS DCTELEM ishift_temp;145#if BITS_IN_JSAMPLE == 8146#define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */147#else148#define DCTELEMBITS 32 /* DCTELEM must be 32 bits */149#endif150#define IRIGHT_SHIFT(x,shft) \151((ishift_temp = (x)) < 0 ? \152(ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \153(ishift_temp >> (shft)))154#else155#define ISHIFT_TEMPS156#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))157#endif158159#ifdef USE_ACCURATE_ROUNDING160#define IDESCALE(x,n) ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))161#else162#define IDESCALE(x,n) ((int) IRIGHT_SHIFT(x, n))163#endif164165166/*167* Perform dequantization and inverse DCT on one block of coefficients.168*/169170GLOBAL(void)171jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,172JCOEFPTR coef_block,173JSAMPARRAY output_buf, JDIMENSION output_col)174{175DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;176DCTELEM tmp10, tmp11, tmp12, tmp13;177DCTELEM z5, z10, z11, z12, z13;178JCOEFPTR inptr;179IFAST_MULT_TYPE * quantptr;180int * wsptr;181JSAMPROW outptr;182JSAMPLE *range_limit = IDCT_range_limit(cinfo);183int ctr;184int workspace[DCTSIZE2]; /* buffers data between passes */185SHIFT_TEMPS /* for DESCALE */186ISHIFT_TEMPS /* for IDESCALE */187188/* Pass 1: process columns from input, store into work array. */189190inptr = coef_block;191quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;192wsptr = workspace;193for (ctr = DCTSIZE; ctr > 0; ctr--) {194/* Due to quantization, we will usually find that many of the input195* coefficients are zero, especially the AC terms. We can exploit this196* by short-circuiting the IDCT calculation for any column in which all197* the AC terms are zero. In that case each output is equal to the198* DC coefficient (with scale factor as needed).199* With typical images and quantization tables, half or more of the200* column DCT calculations can be simplified this way.201*/202203if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&204inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&205inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&206inptr[DCTSIZE*7] == 0) {207/* AC terms all zero */208int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);209210wsptr[DCTSIZE*0] = dcval;211wsptr[DCTSIZE*1] = dcval;212wsptr[DCTSIZE*2] = dcval;213wsptr[DCTSIZE*3] = dcval;214wsptr[DCTSIZE*4] = dcval;215wsptr[DCTSIZE*5] = dcval;216wsptr[DCTSIZE*6] = dcval;217wsptr[DCTSIZE*7] = dcval;218219inptr++; /* advance pointers to next column */220quantptr++;221wsptr++;222continue;223}224225/* Even part */226227tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);228tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);229tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);230tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);231232tmp10 = tmp0 + tmp2; /* phase 3 */233tmp11 = tmp0 - tmp2;234235tmp13 = tmp1 + tmp3; /* phases 5-3 */236tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */237238tmp0 = tmp10 + tmp13; /* phase 2 */239tmp3 = tmp10 - tmp13;240tmp1 = tmp11 + tmp12;241tmp2 = tmp11 - tmp12;242243/* Odd part */244245tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);246tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);247tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);248tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);249250z13 = tmp6 + tmp5; /* phase 6 */251z10 = tmp6 - tmp5;252z11 = tmp4 + tmp7;253z12 = tmp4 - tmp7;254255tmp7 = z11 + z13; /* phase 5 */256tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */257258z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */259tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */260tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */261262tmp6 = tmp12 - tmp7; /* phase 2 */263tmp5 = tmp11 - tmp6;264tmp4 = tmp10 + tmp5;265266wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);267wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);268wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);269wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);270wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);271wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);272wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);273wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);274275inptr++; /* advance pointers to next column */276quantptr++;277wsptr++;278}279280/* Pass 2: process rows from work array, store into output array. */281/* Note that we must descale the results by a factor of 8 == 2**3, */282/* and also undo the PASS1_BITS scaling. */283284wsptr = workspace;285for (ctr = 0; ctr < DCTSIZE; ctr++) {286outptr = output_buf[ctr] + output_col;287/* Rows of zeroes can be exploited in the same way as we did with columns.288* However, the column calculation has created many nonzero AC terms, so289* the simplification applies less often (typically 5% to 10% of the time).290* On machines with very fast multiplication, it's possible that the291* test takes more time than it's worth. In that case this section292* may be commented out.293*/294295#ifndef NO_ZERO_ROW_TEST296if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&297wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {298/* AC terms all zero */299JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)300& RANGE_MASK];301302outptr[0] = dcval;303outptr[1] = dcval;304outptr[2] = dcval;305outptr[3] = dcval;306outptr[4] = dcval;307outptr[5] = dcval;308outptr[6] = dcval;309outptr[7] = dcval;310311wsptr += DCTSIZE; /* advance pointer to next row */312continue;313}314#endif315316/* Even part */317318tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);319tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);320321tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);322tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)323- tmp13;324325tmp0 = tmp10 + tmp13;326tmp3 = tmp10 - tmp13;327tmp1 = tmp11 + tmp12;328tmp2 = tmp11 - tmp12;329330/* Odd part */331332z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];333z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];334z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];335z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];336337tmp7 = z11 + z13; /* phase 5 */338tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */339340z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */341tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */342tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */343344tmp6 = tmp12 - tmp7; /* phase 2 */345tmp5 = tmp11 - tmp6;346tmp4 = tmp10 + tmp5;347348/* Final output stage: scale down by a factor of 8 and range-limit */349350outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)351& RANGE_MASK];352outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)353& RANGE_MASK];354outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)355& RANGE_MASK];356outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)357& RANGE_MASK];358outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)359& RANGE_MASK];360outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)361& RANGE_MASK];362outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)363& RANGE_MASK];364outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)365& RANGE_MASK];366367wsptr += DCTSIZE; /* advance pointer to next row */368}369}370371#endif /* DCT_IFAST_SUPPORTED */372373374