0N/A * reserved comment block 0N/A * DO NOT REMOVE OR ALTER! 0N/A * Copyright (C) 1991-1998, Thomas G. Lane. 0N/A * This file is part of the Independent JPEG Group's software. 0N/A * For conditions of distribution and use, see the accompanying README file. 0N/A * This file contains a slow-but-accurate integer implementation of the 0N/A * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine 0N/A * must also perform dequantization of the input coefficients. 0N/A * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT 0N/A * on each row (or vice versa, but it's more convenient to emit a row at 0N/A * a time). Direct algorithms are also available, but they are much more 0N/A * complex and seem not to be any faster when reduced to code. 0N/A * This implementation is based on an algorithm described in 0N/A * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT 0N/A * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, 0N/A * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. 0N/A * The primary algorithm described there uses 11 multiplies and 29 adds. 0N/A * We use their alternate method with 12 multiplies and 32 adds. 0N/A * The advantage of this method is that no data path contains more than one 0N/A * multiplication; this allows a very simple and accurate implementation in 0N/A * scaled fixed-point arithmetic, with a minimal number of shifts. 0N/A#
include "jdct.h" /* Private declarations for DCT subsystem */ 0N/A * This module is specialized to the case DCTSIZE = 8. 0N/A * The poop on this scaling stuff is as follows: 0N/A * Each 1-D IDCT step produces outputs which are a factor of sqrt(N) 0N/A * larger than the true IDCT outputs. The final outputs are therefore 0N/A * a factor of N larger than desired; since N=8 this can be cured by 0N/A * a simple right shift at the end of the algorithm. The advantage of 0N/A * this arrangement is that we save two multiplications per 1-D IDCT, 0N/A * because the y0 and y4 inputs need not be divided by sqrt(N). 0N/A * We have to do addition and subtraction of the integer inputs, which 0N/A * is no problem, and multiplication by fractional constants, which is 0N/A * a problem to do in integer arithmetic. We multiply all the constants 0N/A * by CONST_SCALE and convert them to integer constants (thus retaining 0N/A * CONST_BITS bits of precision in the constants). After doing a 0N/A * multiplication we have to divide the product by CONST_SCALE, with proper 0N/A * rounding, to produce the correct output. This division can be done 0N/A * cheaply as a right shift of CONST_BITS bits. We postpone shifting 0N/A * as long as possible so that partial sums can be added together with 0N/A * full fractional precision. 0N/A * The outputs of the first pass are scaled up by PASS1_BITS bits so that 0N/A * they are represented to better-than-integral precision. These outputs 0N/A * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word 0N/A * with the recommended scaling. (To scale up 12-bit sample data further, an 0N/A * intermediate INT32 array would be needed.) 0N/A * To avoid overflow of the 32-bit intermediate results in pass 2, we must 0N/A * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis 0N/A * shows that the values given below are the most effective. 0N/A#
define PASS1_BITS 1 /* lose a little precision to avoid overflow */ 0N/A/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus 0N/A * causing a lot of useless floating-point operations at run time. 0N/A * To get around this we use the following pre-calculated constants. 0N/A * If you change CONST_BITS you may want to add appropriate values. 0N/A * (With a reasonable C compiler, you can just rely on the FIX() macro...) 0N/A/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. 0N/A * For 8-bit samples with the recommended scaling, all the variable 0N/A * and constant values involved are no more than 16 bits wide, so a 0N/A * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. 0N/A * For 12-bit samples, a full 32-bit multiplication will be needed. 0N/A/* Dequantize a coefficient by multiplying it by the multiplier-table 0N/A * entry; produce an int result. In this module, both inputs and result 0N/A * are 16 bits or less, so either int or short multiply will work. 0N/A * Perform dequantization and inverse DCT on one block of coefficients. 0N/A /* Pass 1: process columns from input, store into work array. */ 0N/A /* Note results are scaled up by sqrt(8) compared to a true IDCT; */ 0N/A /* furthermore, we scale the results by 2**PASS1_BITS. */ /* Due to quantization, we will usually find that many of the input * coefficients are zero, especially the AC terms. We can exploit this * by short-circuiting the IDCT calculation for any column in which all * the AC terms are zero. In that case each output is equal to the * DC coefficient (with scale factor as needed). * With typical images and quantization tables, half or more of the * column DCT calculations can be simplified this way. inptr++;
/* advance pointers to next column */ /* Even part: reverse the even part of the forward DCT. */ /* The rotator is sqrt(2)*c(-6). */ /* Odd part per figure 8; the matrix is unitary and hence its * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. inptr++;
/* advance pointers to next column */ /* Pass 2: process rows from work array, store into output array. */ /* Note that we must descale the results by a factor of 8 == 2**3, */ /* and also undo the PASS1_BITS scaling. */ /* Rows of zeroes can be exploited in the same way as we did with columns. * However, the column calculation has created many nonzero AC terms, so * the simplification applies less often (typically 5% to 10% of the time). * On machines with very fast multiplication, it's possible that the * test takes more time than it's worth. In that case this section /* Even part: reverse the even part of the forward DCT. */ /* The rotator is sqrt(2)*c(-6). */ /* Odd part per figure 8; the matrix is unitary and hence its * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. #
endif /* DCT_ISLOW_SUPPORTED */