1/*
2 * IDCT implementation using the MIPS DSP ASE (little endian version)
3 *
4 * jidctfst.c
5 *
6 * Copyright (C) 1994-1998, Thomas G. Lane.
7 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains a fast, not so accurate integer implementation of the
11 * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
12 * must also perform dequantization of the input coefficients.
13 *
14 * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
15 * on each row (or vice versa, but it's more convenient to emit a row at
16 * a time).  Direct algorithms are also available, but they are much more
17 * complex and seem not to be any faster when reduced to code.
18 *
19 * This implementation is based on Arai, Agui, and Nakajima's algorithm for
20 * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
21 * Japanese, but the algorithm is described in the Pennebaker & Mitchell
22 * JPEG textbook (see REFERENCES section in file README).  The following code
23 * is based directly on figure 4-8 in P&M.
24 * While an 8-point DCT cannot be done in less than 11 multiplies, it is
25 * possible to arrange the computation so that many of the multiplies are
26 * simple scalings of the final outputs.  These multiplies can then be
27 * folded into the multiplications or divisions by the JPEG quantization
28 * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
29 * to be done in the DCT itself.
30 * The primary disadvantage of this method is that with fixed-point math,
31 * accuracy is lost due to imprecise representation of the scaled
32 * quantization values.  The smaller the quantization table entry, the less
33 * precise the scaled value, so this implementation does worse with high-
34 * quality-setting files than with low-quality ones.
35 */
36
37#define JPEG_INTERNALS
38#include "jinclude.h"
39#include "jpeglib.h"
40#include "jdct.h"               /* Private declarations for DCT subsystem */
41
42#ifdef DCT_IFAST_SUPPORTED
43
44
45/*
46 * This module is specialized to the case DCTSIZE = 8.
47 */
48
49#if DCTSIZE != 8
50  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
51#endif
52
53
54/* Scaling decisions are generally the same as in the LL&M algorithm;
55 * see jidctint.c for more details.  However, we choose to descale
56 * (right shift) multiplication products as soon as they are formed,
57 * rather than carrying additional fractional bits into subsequent additions.
58 * This compromises accuracy slightly, but it lets us save a few shifts.
59 * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
60 * everywhere except in the multiplications proper; this saves a good deal
61 * of work on 16-bit-int machines.
62 *
63 * The dequantized coefficients are not integers because the AA&N scaling
64 * factors have been incorporated.  We represent them scaled up by PASS1_BITS,
65 * so that the first and second IDCT rounds have the same input scaling.
66 * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
67 * avoid a descaling shift; this compromises accuracy rather drastically
68 * for small quantization table entries, but it saves a lot of shifts.
69 * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
70 * so we use a much larger scaling factor to preserve accuracy.
71 *
72 * A final compromise is to represent the multiplicative constants to only
73 * 8 fractional bits, rather than 13.  This saves some shifting work on some
74 * machines, and may also reduce the cost of multiplication (since there
75 * are fewer one-bits in the constants).
76 */
77
78#if BITS_IN_JSAMPLE == 8
79#define CONST_BITS  8
80#define PASS1_BITS  2
81#else
82#define CONST_BITS  8
83#define PASS1_BITS  1           /* lose a little precision to avoid overflow */
84#endif
85
86/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
87 * causing a lot of useless floating-point operations at run time.
88 * To get around this we use the following pre-calculated constants.
89 * If you change CONST_BITS you may want to add appropriate values.
90 * (With a reasonable C compiler, you can just rely on the FIX() macro...)
91 */
92
93#if CONST_BITS == 8
94#define FIX_1_082392200  ((INT32)  277)         /* FIX(1.082392200) */
95#define FIX_1_414213562  ((INT32)  362)         /* FIX(1.414213562) */
96#define FIX_1_847759065  ((INT32)  473)         /* FIX(1.847759065) */
97#define FIX_2_613125930  ((INT32)  669)         /* FIX(2.613125930) */
98#else
99#define FIX_1_082392200  FIX(1.082392200)
100#define FIX_1_414213562  FIX(1.414213562)
101#define FIX_1_847759065  FIX(1.847759065)
102#define FIX_2_613125930  FIX(2.613125930)
103#endif
104
105
106/* We can gain a little more speed, with a further compromise in accuracy,
107 * by omitting the addition in a descaling shift.  This yields an incorrectly
108 * rounded result half the time...
109 */
110
111#ifndef USE_ACCURATE_ROUNDING
112#undef DESCALE
113#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
114#endif
115
116
117/* Multiply a DCTELEM variable by an INT32 constant, and immediately
118 * descale to yield a DCTELEM result.
119 */
120
121#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
122
123
124/* Dequantize a coefficient by multiplying it by the multiplier-table
125 * entry; produce a DCTELEM result.  For 8-bit data a 16x16->16
126 * multiplication will do.  For 12-bit data, the multiplier table is
127 * declared INT32, so a 32-bit multiply will be used.
128 */
129
130#if BITS_IN_JSAMPLE == 8
131#define DEQUANTIZE(coef,quantval)  (((IFAST_MULT_TYPE) (coef)) * (quantval))
132#else
133#define DEQUANTIZE(coef,quantval)  \
134        DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
135#endif
136
137
138/* Like DESCALE, but applies to a DCTELEM and produces an int.
139 * We assume that int right shift is unsigned if INT32 right shift is.
140 */
141
142#ifdef RIGHT_SHIFT_IS_UNSIGNED
143#define ISHIFT_TEMPS    DCTELEM ishift_temp;
144#if BITS_IN_JSAMPLE == 8
145#define DCTELEMBITS  16         /* DCTELEM may be 16 or 32 bits */
146#else
147#define DCTELEMBITS  32         /* DCTELEM must be 32 bits */
148#endif
149#define IRIGHT_SHIFT(x,shft)  \
150    ((ishift_temp = (x)) < 0 ? \
151     (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
152     (ishift_temp >> (shft)))
153#else
154#define ISHIFT_TEMPS
155#define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
156#endif
157
158#ifdef USE_ACCURATE_ROUNDING
159#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
160#else
161#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT(x, n))
162#endif
163
164
165// this table of constants has been moved from mips_idct_le/_be.s to
166// avoid having to make the assembler code position independent
167static const int mips_idct_coefs[4] = {
168  0x45464546,           // FIX( 1.082392200 / 2) =  17734 = 0x4546
169  0x5A825A82,           // FIX( 1.414213562 / 2) =  23170 = 0x5A82
170  0x76427642,           // FIX( 1.847759065 / 2) =  30274 = 0x7642
171  0xAC61AC61            // FIX(-2.613125930 / 4) = -21407 = 0xAC61
172};
173
174void mips_idct_columns(JCOEF * inptr, IFAST_MULT_TYPE * quantptr,
175                       DCTELEM * wsptr, const int * mips_idct_coefs);
176void mips_idct_rows(DCTELEM * wsptr, JSAMPARRAY output_buf,
177                    JDIMENSION output_col, const int * mips_idct_coefs);
178
179
180/*
181 * Perform dequantization and inverse DCT on one block of coefficients.
182 */
183
184GLOBAL(void)
185jpeg_idct_mips (j_decompress_ptr cinfo, jpeg_component_info * compptr,
186                 JCOEFPTR coef_block,
187                 JSAMPARRAY output_buf, JDIMENSION output_col)
188{
189  JCOEFPTR inptr;
190  IFAST_MULT_TYPE * quantptr;
191  DCTELEM workspace[DCTSIZE2];  /* buffers data between passes */
192
193  /* Pass 1: process columns from input, store into work array. */
194
195  inptr = coef_block;
196  quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
197
198  mips_idct_columns(inptr, quantptr, workspace, mips_idct_coefs);
199
200  /* Pass 2: process rows from work array, store into output array. */
201  /* Note that we must descale the results by a factor of 8 == 2**3, */
202  /* and also undo the PASS1_BITS scaling. */
203
204  mips_idct_rows(workspace, output_buf, output_col, mips_idct_coefs);
205
206}
207
208#endif /* DCT_IFAST_SUPPORTED */
209