xref: /utopia/UTPA2-700.0.x/modules/ojpd_vdec_v1/api/jpeg/cmodel/src/jidctfst.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1*53ee8cc1Swenshuai.xi //<MStar Software>
2*53ee8cc1Swenshuai.xi //******************************************************************************
3*53ee8cc1Swenshuai.xi // MStar Software
4*53ee8cc1Swenshuai.xi // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5*53ee8cc1Swenshuai.xi // All software, firmware and related documentation herein ("MStar Software") are
6*53ee8cc1Swenshuai.xi // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7*53ee8cc1Swenshuai.xi // law, including, but not limited to, copyright law and international treaties.
8*53ee8cc1Swenshuai.xi // Any use, modification, reproduction, retransmission, or republication of all
9*53ee8cc1Swenshuai.xi // or part of MStar Software is expressly prohibited, unless prior written
10*53ee8cc1Swenshuai.xi // permission has been granted by MStar.
11*53ee8cc1Swenshuai.xi //
12*53ee8cc1Swenshuai.xi // By accessing, browsing and/or using MStar Software, you acknowledge that you
13*53ee8cc1Swenshuai.xi // have read, understood, and agree, to be bound by below terms ("Terms") and to
14*53ee8cc1Swenshuai.xi // comply with all applicable laws and regulations:
15*53ee8cc1Swenshuai.xi //
16*53ee8cc1Swenshuai.xi // 1. MStar shall retain any and all right, ownership and interest to MStar
17*53ee8cc1Swenshuai.xi //    Software and any modification/derivatives thereof.
18*53ee8cc1Swenshuai.xi //    No right, ownership, or interest to MStar Software and any
19*53ee8cc1Swenshuai.xi //    modification/derivatives thereof is transferred to you under Terms.
20*53ee8cc1Swenshuai.xi //
21*53ee8cc1Swenshuai.xi // 2. You understand that MStar Software might include, incorporate or be
22*53ee8cc1Swenshuai.xi //    supplied together with third party`s software and the use of MStar
23*53ee8cc1Swenshuai.xi //    Software may require additional licenses from third parties.
24*53ee8cc1Swenshuai.xi //    Therefore, you hereby agree it is your sole responsibility to separately
25*53ee8cc1Swenshuai.xi //    obtain any and all third party right and license necessary for your use of
26*53ee8cc1Swenshuai.xi //    such third party`s software.
27*53ee8cc1Swenshuai.xi //
28*53ee8cc1Swenshuai.xi // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29*53ee8cc1Swenshuai.xi //    MStar`s confidential information and you agree to keep MStar`s
30*53ee8cc1Swenshuai.xi //    confidential information in strictest confidence and not disclose to any
31*53ee8cc1Swenshuai.xi //    third party.
32*53ee8cc1Swenshuai.xi //
33*53ee8cc1Swenshuai.xi // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34*53ee8cc1Swenshuai.xi //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35*53ee8cc1Swenshuai.xi //    without limitation, any warranties of merchantability, non-infringement of
36*53ee8cc1Swenshuai.xi //    intellectual property rights, fitness for a particular purpose, error free
37*53ee8cc1Swenshuai.xi //    and in conformity with any international standard.  You agree to waive any
38*53ee8cc1Swenshuai.xi //    claim against MStar for any loss, damage, cost or expense that you may
39*53ee8cc1Swenshuai.xi //    incur related to your use of MStar Software.
40*53ee8cc1Swenshuai.xi //    In no event shall MStar be liable for any direct, indirect, incidental or
41*53ee8cc1Swenshuai.xi //    consequential damages, including without limitation, lost of profit or
42*53ee8cc1Swenshuai.xi //    revenues, lost or damage of data, and unauthorized system use.
43*53ee8cc1Swenshuai.xi //    You agree that this Section 4 shall still apply without being affected
44*53ee8cc1Swenshuai.xi //    even if MStar Software has been modified by MStar in accordance with your
45*53ee8cc1Swenshuai.xi //    request or instruction for your use, except otherwise agreed by both
46*53ee8cc1Swenshuai.xi //    parties in writing.
47*53ee8cc1Swenshuai.xi //
48*53ee8cc1Swenshuai.xi // 5. If requested, MStar may from time to time provide technical supports or
49*53ee8cc1Swenshuai.xi //    services in relation with MStar Software to you for your use of
50*53ee8cc1Swenshuai.xi //    MStar Software in conjunction with your or your customer`s product
51*53ee8cc1Swenshuai.xi //    ("Services").
52*53ee8cc1Swenshuai.xi //    You understand and agree that, except otherwise agreed by both parties in
53*53ee8cc1Swenshuai.xi //    writing, Services are provided on an "AS IS" basis and the warranty
54*53ee8cc1Swenshuai.xi //    disclaimer set forth in Section 4 above shall apply.
55*53ee8cc1Swenshuai.xi //
56*53ee8cc1Swenshuai.xi // 6. Nothing contained herein shall be construed as by implication, estoppels
57*53ee8cc1Swenshuai.xi //    or otherwise:
58*53ee8cc1Swenshuai.xi //    (a) conferring any license or right to use MStar name, trademark, service
59*53ee8cc1Swenshuai.xi //        mark, symbol or any other identification;
60*53ee8cc1Swenshuai.xi //    (b) obligating MStar or any of its affiliates to furnish any person,
61*53ee8cc1Swenshuai.xi //        including without limitation, you and your customers, any assistance
62*53ee8cc1Swenshuai.xi //        of any kind whatsoever, or any information; or
63*53ee8cc1Swenshuai.xi //    (c) conferring any license or right under any intellectual property right.
64*53ee8cc1Swenshuai.xi //
65*53ee8cc1Swenshuai.xi // 7. These terms shall be governed by and construed in accordance with the laws
66*53ee8cc1Swenshuai.xi //    of Taiwan, R.O.C., excluding its conflict of law rules.
67*53ee8cc1Swenshuai.xi //    Any and all dispute arising out hereof or related hereto shall be finally
68*53ee8cc1Swenshuai.xi //    settled by arbitration referred to the Chinese Arbitration Association,
69*53ee8cc1Swenshuai.xi //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70*53ee8cc1Swenshuai.xi //    Rules of the Association by three (3) arbitrators appointed in accordance
71*53ee8cc1Swenshuai.xi //    with the said Rules.
72*53ee8cc1Swenshuai.xi //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73*53ee8cc1Swenshuai.xi //    be English.
74*53ee8cc1Swenshuai.xi //    The arbitration award shall be final and binding to both parties.
75*53ee8cc1Swenshuai.xi //
76*53ee8cc1Swenshuai.xi //******************************************************************************
77*53ee8cc1Swenshuai.xi //<MStar Software>
78*53ee8cc1Swenshuai.xi /*
79*53ee8cc1Swenshuai.xi  * jidctfst.c
80*53ee8cc1Swenshuai.xi  *
81*53ee8cc1Swenshuai.xi  * Copyright (C) 1994-1998, Thomas G. Lane.
82*53ee8cc1Swenshuai.xi  * This file is part of the Independent JPEG Group's software.
83*53ee8cc1Swenshuai.xi  * For conditions of distribution and use, see the accompanying README file.
84*53ee8cc1Swenshuai.xi  *
85*53ee8cc1Swenshuai.xi  * This file contains a fast, not so accurate integer implementation of the
86*53ee8cc1Swenshuai.xi  * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
87*53ee8cc1Swenshuai.xi  * must also perform dequantization of the input coefficients.
88*53ee8cc1Swenshuai.xi  *
89*53ee8cc1Swenshuai.xi  * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
90*53ee8cc1Swenshuai.xi  * on each row (or vice versa, but it's more convenient to emit a row at
91*53ee8cc1Swenshuai.xi  * a time).  Direct algorithms are also available, but they are much more
92*53ee8cc1Swenshuai.xi  * complex and seem not to be any faster when reduced to code.
93*53ee8cc1Swenshuai.xi  *
94*53ee8cc1Swenshuai.xi  * This implementation is based on Arai, Agui, and Nakajima's algorithm for
95*53ee8cc1Swenshuai.xi  * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
96*53ee8cc1Swenshuai.xi  * Japanese, but the algorithm is described in the Pennebaker & Mitchell
97*53ee8cc1Swenshuai.xi  * JPEG textbook (see REFERENCES section in file README).  The following code
98*53ee8cc1Swenshuai.xi  * is based directly on figure 4-8 in P&M.
99*53ee8cc1Swenshuai.xi  * While an 8-point DCT cannot be done in less than 11 multiplies, it is
100*53ee8cc1Swenshuai.xi  * possible to arrange the computation so that many of the multiplies are
101*53ee8cc1Swenshuai.xi  * simple scalings of the final outputs.  These multiplies can then be
102*53ee8cc1Swenshuai.xi  * folded into the multiplications or divisions by the JPEG quantization
103*53ee8cc1Swenshuai.xi  * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
104*53ee8cc1Swenshuai.xi  * to be done in the DCT itself.
105*53ee8cc1Swenshuai.xi  * The primary disadvantage of this method is that with fixed-point math,
106*53ee8cc1Swenshuai.xi  * accuracy is lost due to imprecise representation of the scaled
107*53ee8cc1Swenshuai.xi  * quantization values.  The smaller the quantization table entry, the less
108*53ee8cc1Swenshuai.xi  * precise the scaled value, so this implementation does worse with high-
109*53ee8cc1Swenshuai.xi  * quality-setting files than with low-quality ones.
110*53ee8cc1Swenshuai.xi  */
111*53ee8cc1Swenshuai.xi 
112*53ee8cc1Swenshuai.xi ///#define JPEG_INTERNALS
113*53ee8cc1Swenshuai.xi ///#include "jinclude.h"
114*53ee8cc1Swenshuai.xi ///#include "jpeglib.h"
115*53ee8cc1Swenshuai.xi ///#include "jdct.h"        /* Private declarations for DCT subsystem */
116*53ee8cc1Swenshuai.xi #include "jpegmain.h"
117*53ee8cc1Swenshuai.xi #include "apiJPEG.h"
118*53ee8cc1Swenshuai.xi 
119*53ee8cc1Swenshuai.xi #define DCTSIZE 8
120*53ee8cc1Swenshuai.xi #define BITS_IN_JSAMPLE 8
121*53ee8cc1Swenshuai.xi #define DCT_IFAST_SUPPORTED
122*53ee8cc1Swenshuai.xi 
123*53ee8cc1Swenshuai.xi #ifdef DCT_IFAST_SUPPORTED
124*53ee8cc1Swenshuai.xi 
125*53ee8cc1Swenshuai.xi /*
126*53ee8cc1Swenshuai.xi  * This module is specialized to the case DCTSIZE = 8.
127*53ee8cc1Swenshuai.xi  */
128*53ee8cc1Swenshuai.xi 
129*53ee8cc1Swenshuai.xi #if DCTSIZE != 8
130*53ee8cc1Swenshuai.xi Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
131*53ee8cc1Swenshuai.xi #endif
132*53ee8cc1Swenshuai.xi 
133*53ee8cc1Swenshuai.xi 
134*53ee8cc1Swenshuai.xi /* Scaling decisions are generally the same as in the LL&M algorithm;
135*53ee8cc1Swenshuai.xi  * see jidctint.c for more details.  However, we choose to descale
136*53ee8cc1Swenshuai.xi  * (right shift) multiplication products as soon as they are formed,
137*53ee8cc1Swenshuai.xi  * rather than carrying additional fractional bits into subsequent additions.
138*53ee8cc1Swenshuai.xi  * This compromises accuracy slightly, but it lets us save a few shifts.
139*53ee8cc1Swenshuai.xi  * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
140*53ee8cc1Swenshuai.xi  * everywhere except in the multiplications proper; this saves a good deal
141*53ee8cc1Swenshuai.xi  * of work on 16-bit-int machines.
142*53ee8cc1Swenshuai.xi  *
143*53ee8cc1Swenshuai.xi  * The dequantized coefficients are not integers because the AA&N scaling
144*53ee8cc1Swenshuai.xi  * factors have been incorporated.  We represent them scaled up by PASS1_BITS,
145*53ee8cc1Swenshuai.xi  * so that the first and second IDCT rounds have the same input scaling.
146*53ee8cc1Swenshuai.xi  * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
147*53ee8cc1Swenshuai.xi  * avoid a descaling shift; this compromises accuracy rather drastically
148*53ee8cc1Swenshuai.xi  * for small quantization table entries, but it saves a lot of shifts.
149*53ee8cc1Swenshuai.xi  * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
150*53ee8cc1Swenshuai.xi  * so we use a much larger scaling factor to preserve accuracy.
151*53ee8cc1Swenshuai.xi  *
152*53ee8cc1Swenshuai.xi  * A final compromise is to represent the multiplicative constants to only
153*53ee8cc1Swenshuai.xi  * 8 fractional bits, rather than 13.  This saves some shifting work on some
154*53ee8cc1Swenshuai.xi  * machines, and may also reduce the cost of multiplication (since there
155*53ee8cc1Swenshuai.xi  * are fewer one-bits in the constants).
156*53ee8cc1Swenshuai.xi  */
157*53ee8cc1Swenshuai.xi 
158*53ee8cc1Swenshuai.xi #if BITS_IN_JSAMPLE == 8
159*53ee8cc1Swenshuai.xi #define CONST_BITS  8
160*53ee8cc1Swenshuai.xi #define PASS1_BITS  2
161*53ee8cc1Swenshuai.xi #else
162*53ee8cc1Swenshuai.xi #define CONST_BITS  8
163*53ee8cc1Swenshuai.xi #define PASS1_BITS  1       /* lose a little precision to avoid overflow */
164*53ee8cc1Swenshuai.xi #endif
165*53ee8cc1Swenshuai.xi 
166*53ee8cc1Swenshuai.xi /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
167*53ee8cc1Swenshuai.xi  * causing a lot of useless floating-point operations at run time.
168*53ee8cc1Swenshuai.xi  * To get around this we use the following pre-calculated constants.
169*53ee8cc1Swenshuai.xi  * If you change CONST_BITS you may want to add appropriate values.
170*53ee8cc1Swenshuai.xi  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
171*53ee8cc1Swenshuai.xi  */
172*53ee8cc1Swenshuai.xi 
173*53ee8cc1Swenshuai.xi #if CONST_BITS == 8
174*53ee8cc1Swenshuai.xi #define FIX_1_082392200  ((INT32)  277)     /* FIX(1.082392200) */
175*53ee8cc1Swenshuai.xi #define FIX_1_414213562  ((INT32)  362)     /* FIX(1.414213562) */
176*53ee8cc1Swenshuai.xi #define FIX_1_847759065  ((INT32)  473)     /* FIX(1.847759065) */
177*53ee8cc1Swenshuai.xi #define FIX_2_613125930  ((INT32)  669)     /* FIX(2.613125930) */
178*53ee8cc1Swenshuai.xi #else
179*53ee8cc1Swenshuai.xi #define FIX_1_082392200  FIX(1.082392200)
180*53ee8cc1Swenshuai.xi #define FIX_1_414213562  FIX(1.414213562)
181*53ee8cc1Swenshuai.xi #define FIX_1_847759065  FIX(1.847759065)
182*53ee8cc1Swenshuai.xi #define FIX_2_613125930  FIX(2.613125930)
183*53ee8cc1Swenshuai.xi #endif
184*53ee8cc1Swenshuai.xi 
185*53ee8cc1Swenshuai.xi 
186*53ee8cc1Swenshuai.xi /* We can gain a little more speed, with a further compromise in accuracy,
187*53ee8cc1Swenshuai.xi  * by omitting the addition in a descaling shift.  This yields an incorrectly
188*53ee8cc1Swenshuai.xi  * rounded result half the time...
189*53ee8cc1Swenshuai.xi  */
190*53ee8cc1Swenshuai.xi 
191*53ee8cc1Swenshuai.xi #ifndef USE_ACCURATE_ROUNDING
192*53ee8cc1Swenshuai.xi #undef DESCALE
193*53ee8cc1Swenshuai.xi //#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
194*53ee8cc1Swenshuai.xi #define DESCALE(x,n)  ((x) >> (n)) //=> speed a little with a compromise in accuracy
195*53ee8cc1Swenshuai.xi //#define DESCALE(x,n)  (((x) + ( ((int32)1) << ((n)-1))) >> (n))  //
196*53ee8cc1Swenshuai.xi #endif
197*53ee8cc1Swenshuai.xi 
198*53ee8cc1Swenshuai.xi 
199*53ee8cc1Swenshuai.xi /* Multiply a DCTELEM variable by an INT32 constant, and immediately
200*53ee8cc1Swenshuai.xi  * descale to yield a DCTELEM result.
201*53ee8cc1Swenshuai.xi  */
202*53ee8cc1Swenshuai.xi 
203*53ee8cc1Swenshuai.xi typedef int DCTELEM;        /* 16 or 32 bits is fine */
204*53ee8cc1Swenshuai.xi #define MULTIPLY(var,cnst)  ((DCTELEM) DESCALE((var) * (cnst), CONST_BITS))
205*53ee8cc1Swenshuai.xi 
206*53ee8cc1Swenshuai.xi 
207*53ee8cc1Swenshuai.xi /* Dequantize a coefficient by multiplying it by the multiplier-table
208*53ee8cc1Swenshuai.xi  * entry; produce a DCTELEM result.  For 8-bit data a 16x16->16
209*53ee8cc1Swenshuai.xi  * multiplication will do.  For 12-bit data, the multiplier table is
210*53ee8cc1Swenshuai.xi  * declared INT32, so a 32-bit multiply will be used.
211*53ee8cc1Swenshuai.xi  */
212*53ee8cc1Swenshuai.xi 
213*53ee8cc1Swenshuai.xi #if BITS_IN_JSAMPLE == 8
214*53ee8cc1Swenshuai.xi //#define DEQUANTIZE(coef,quantval)  (((IFAST_MULT_TYPE) (coef)) * (quantval))
215*53ee8cc1Swenshuai.xi #define DEQUANTIZE(coef,quantval)  (coef)
216*53ee8cc1Swenshuai.xi #else
217*53ee8cc1Swenshuai.xi #define DEQUANTIZE(coef,quantval)  \
218*53ee8cc1Swenshuai.xi     DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
219*53ee8cc1Swenshuai.xi #endif
220*53ee8cc1Swenshuai.xi 
221*53ee8cc1Swenshuai.xi 
222*53ee8cc1Swenshuai.xi /* Like DESCALE, but applies to a DCTELEM and produces an int.
223*53ee8cc1Swenshuai.xi  * We assume that int right shift is unsigned if INT32 right shift is.
224*53ee8cc1Swenshuai.xi  */
225*53ee8cc1Swenshuai.xi 
226*53ee8cc1Swenshuai.xi #ifdef RIGHT_SHIFT_IS_UNSIGNED
227*53ee8cc1Swenshuai.xi #define ISHIFT_TEMPS    DCTELEM ishift_temp;
228*53ee8cc1Swenshuai.xi #if BITS_IN_JSAMPLE == 8
229*53ee8cc1Swenshuai.xi #define DCTELEMBITS  16     /* DCTELEM may be 16 or 32 bits */
230*53ee8cc1Swenshuai.xi #else
231*53ee8cc1Swenshuai.xi #define DCTELEMBITS  32     /* DCTELEM must be 32 bits */
232*53ee8cc1Swenshuai.xi #endif
233*53ee8cc1Swenshuai.xi #define IRIGHT_SHIFT(x,shft)  \
234*53ee8cc1Swenshuai.xi     ((ishift_temp = (x)) < 0 ? \
235*53ee8cc1Swenshuai.xi      (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
236*53ee8cc1Swenshuai.xi      (ishift_temp >> (shft)))
237*53ee8cc1Swenshuai.xi #else
238*53ee8cc1Swenshuai.xi #define ISHIFT_TEMPS
239*53ee8cc1Swenshuai.xi #define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
240*53ee8cc1Swenshuai.xi #endif
241*53ee8cc1Swenshuai.xi 
242*53ee8cc1Swenshuai.xi #if 1///def USE_ACCURATE_ROUNDING
243*53ee8cc1Swenshuai.xi #define IDESCALE(x,n)  ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
244*53ee8cc1Swenshuai.xi //#define IDESCALE(x,n)  ( ( (int)(x) + (1 << ((n)-1)) ) >> (n) )
245*53ee8cc1Swenshuai.xi #else
246*53ee8cc1Swenshuai.xi #define IDESCALE(x,n)  ((int) IRIGHT_SHIFT(x, n))
247*53ee8cc1Swenshuai.xi #endif
248*53ee8cc1Swenshuai.xi 
249*53ee8cc1Swenshuai.xi #define IFAST_MULT_TYPE int
250*53ee8cc1Swenshuai.xi 
251*53ee8cc1Swenshuai.xi #define clamp(i) if (i & 0xFF00) i = (((~i) >> 15) & 0xFF);
252*53ee8cc1Swenshuai.xi 
253*53ee8cc1Swenshuai.xi /*
254*53ee8cc1Swenshuai.xi  * Perform dequantization and inverse DCT on one block of coefficients.
255*53ee8cc1Swenshuai.xi  */
256*53ee8cc1Swenshuai.xi 
257*53ee8cc1Swenshuai.xi ///GLOBAL(void)
258*53ee8cc1Swenshuai.xi ///jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
259*53ee8cc1Swenshuai.xi ///      JCOEFPTR coef_block,
260*53ee8cc1Swenshuai.xi ///      JSAMPARRAY output_buf, JDIMENSION output_col)
jpeg_idct_ifast(JPEG_BLOCK_TYPE * data,U8 * Pdst_ptr)261*53ee8cc1Swenshuai.xi void jpeg_idct_ifast( JPEG_BLOCK_TYPE *data, U8 *Pdst_ptr )
262*53ee8cc1Swenshuai.xi {
263*53ee8cc1Swenshuai.xi     #define INT32   S32
264*53ee8cc1Swenshuai.xi     #define DCTSIZE2 64
265*53ee8cc1Swenshuai.xi     #define DCTSIZE 8
266*53ee8cc1Swenshuai.xi 
267*53ee8cc1Swenshuai.xi     DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
268*53ee8cc1Swenshuai.xi     DCTELEM tmp10, tmp11, tmp12, tmp13;
269*53ee8cc1Swenshuai.xi     DCTELEM z5, z10, z11, z12, z13;
270*53ee8cc1Swenshuai.xi     ///JCOEFPTR inptr;
271*53ee8cc1Swenshuai.xi     register JPEG_BLOCK_TYPE *inptr;
272*53ee8cc1Swenshuai.xi     IFAST_MULT_TYPE *quantptr;
273*53ee8cc1Swenshuai.xi     ///JSAMPROW outptr;
274*53ee8cc1Swenshuai.xi     U8 *outptr = Pdst_ptr;
275*53ee8cc1Swenshuai.xi     ///JSAMPLE *range_limit = IDCT_range_limit(cinfo);
276*53ee8cc1Swenshuai.xi     int ctr;
277*53ee8cc1Swenshuai.xi     ///int workspace[DCTSIZE2]; /* buffers data between passes */
278*53ee8cc1Swenshuai.xi     JPEG_BLOCK_TYPE workspace[DCTSIZE2];
279*53ee8cc1Swenshuai.xi     JPEG_BLOCK_TYPE *wsptr;
280*53ee8cc1Swenshuai.xi     ///SHIFT_TEMPS          /* for DESCALE */
281*53ee8cc1Swenshuai.xi     ///ISHIFT_TEMPS         /* for IDESCALE */
282*53ee8cc1Swenshuai.xi     S16 i;
283*53ee8cc1Swenshuai.xi //printf("jidctfst::jpeg_idct_ifast\n");
284*53ee8cc1Swenshuai.xi     /* Pass 1: process columns from input, store into work array. */
285*53ee8cc1Swenshuai.xi 
286*53ee8cc1Swenshuai.xi     inptr = data;
287*53ee8cc1Swenshuai.xi     ///quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
288*53ee8cc1Swenshuai.xi     wsptr = workspace;
289*53ee8cc1Swenshuai.xi     for ( ctr = DCTSIZE; ctr > 0; ctr-- )
290*53ee8cc1Swenshuai.xi     {
291*53ee8cc1Swenshuai.xi         /* Due to quantization, we will usually find that many of the input
292*53ee8cc1Swenshuai.xi          * coefficients are zero, especially the AC terms.  We can exploit this
293*53ee8cc1Swenshuai.xi          * by short-circuiting the IDCT calculation for any column in which all
294*53ee8cc1Swenshuai.xi          * the AC terms are zero.  In that case each output is equal to the
295*53ee8cc1Swenshuai.xi          * DC coefficient (with scale factor as needed).
296*53ee8cc1Swenshuai.xi          * With typical images and quantization tables, half or more of the
297*53ee8cc1Swenshuai.xi          * column DCT calculations can be simplified this way.
298*53ee8cc1Swenshuai.xi          */
299*53ee8cc1Swenshuai.xi 
300*53ee8cc1Swenshuai.xi         if ( inptr[DCTSIZE * 1] == 0 && inptr[DCTSIZE * 2] == 0 && inptr[DCTSIZE * 3] == 0 && inptr[DCTSIZE * 4] == 0 && inptr[DCTSIZE * 5] == 0 && inptr[DCTSIZE * 6] == 0 && inptr[DCTSIZE * 7] == 0 )
301*53ee8cc1Swenshuai.xi         {
302*53ee8cc1Swenshuai.xi             /* AC terms all zero */
303*53ee8cc1Swenshuai.xi             int dcval = ( int )DEQUANTIZE( inptr[DCTSIZE*0], quantptr[DCTSIZE*0] );
304*53ee8cc1Swenshuai.xi 
305*53ee8cc1Swenshuai.xi             wsptr[DCTSIZE * 0] = dcval;
306*53ee8cc1Swenshuai.xi             wsptr[DCTSIZE * 1] = dcval;
307*53ee8cc1Swenshuai.xi             wsptr[DCTSIZE * 2] = dcval;
308*53ee8cc1Swenshuai.xi             wsptr[DCTSIZE * 3] = dcval;
309*53ee8cc1Swenshuai.xi             wsptr[DCTSIZE * 4] = dcval;
310*53ee8cc1Swenshuai.xi             wsptr[DCTSIZE * 5] = dcval;
311*53ee8cc1Swenshuai.xi             wsptr[DCTSIZE * 6] = dcval;
312*53ee8cc1Swenshuai.xi             wsptr[DCTSIZE * 7] = dcval;
313*53ee8cc1Swenshuai.xi 
314*53ee8cc1Swenshuai.xi             inptr++;            /* advance pointers to next column */
315*53ee8cc1Swenshuai.xi             quantptr++;
316*53ee8cc1Swenshuai.xi             wsptr++;
317*53ee8cc1Swenshuai.xi             continue;
318*53ee8cc1Swenshuai.xi         }
319*53ee8cc1Swenshuai.xi 
320*53ee8cc1Swenshuai.xi         /* Even part */
321*53ee8cc1Swenshuai.xi 
322*53ee8cc1Swenshuai.xi         tmp0 = DEQUANTIZE( inptr[DCTSIZE * 0], quantptr[DCTSIZE * 0] );
323*53ee8cc1Swenshuai.xi         tmp1 = DEQUANTIZE( inptr[DCTSIZE * 2], quantptr[DCTSIZE * 2] );
324*53ee8cc1Swenshuai.xi         tmp2 = DEQUANTIZE( inptr[DCTSIZE * 4], quantptr[DCTSIZE * 4] );
325*53ee8cc1Swenshuai.xi         tmp3 = DEQUANTIZE( inptr[DCTSIZE * 6], quantptr[DCTSIZE * 6] );
326*53ee8cc1Swenshuai.xi 
327*53ee8cc1Swenshuai.xi         tmp10 = tmp0 + tmp2;    /* phase 3 */
328*53ee8cc1Swenshuai.xi         tmp11 = tmp0 - tmp2;
329*53ee8cc1Swenshuai.xi 
330*53ee8cc1Swenshuai.xi         tmp13 = tmp1 + tmp3;    /* phases 5-3 */
331*53ee8cc1Swenshuai.xi         tmp12 = MULTIPLY( tmp1 - tmp3, FIX_1_414213562 ) - tmp13; /* 2*c4 */
332*53ee8cc1Swenshuai.xi 
333*53ee8cc1Swenshuai.xi         tmp0 = tmp10 + tmp13;   /* phase 2 */
334*53ee8cc1Swenshuai.xi         tmp3 = tmp10 - tmp13;
335*53ee8cc1Swenshuai.xi         tmp1 = tmp11 + tmp12;
336*53ee8cc1Swenshuai.xi         tmp2 = tmp11 - tmp12;
337*53ee8cc1Swenshuai.xi 
338*53ee8cc1Swenshuai.xi         /* Odd part */
339*53ee8cc1Swenshuai.xi 
340*53ee8cc1Swenshuai.xi         tmp4 = DEQUANTIZE( inptr[DCTSIZE * 1], quantptr[DCTSIZE * 1] );
341*53ee8cc1Swenshuai.xi         tmp5 = DEQUANTIZE( inptr[DCTSIZE * 3], quantptr[DCTSIZE * 3] );
342*53ee8cc1Swenshuai.xi         tmp6 = DEQUANTIZE( inptr[DCTSIZE * 5], quantptr[DCTSIZE * 5] );
343*53ee8cc1Swenshuai.xi         tmp7 = DEQUANTIZE( inptr[DCTSIZE * 7], quantptr[DCTSIZE * 7] );
344*53ee8cc1Swenshuai.xi 
345*53ee8cc1Swenshuai.xi         z13 = tmp6 + tmp5;      /* phase 6 */
346*53ee8cc1Swenshuai.xi         z10 = tmp6 - tmp5;
347*53ee8cc1Swenshuai.xi         z11 = tmp4 + tmp7;
348*53ee8cc1Swenshuai.xi         z12 = tmp4 - tmp7;
349*53ee8cc1Swenshuai.xi 
350*53ee8cc1Swenshuai.xi         tmp7 = z11 + z13;       /* phase 5 */
351*53ee8cc1Swenshuai.xi         tmp11 = MULTIPLY( z11 - z13, FIX_1_414213562 ); /* 2*c4 */
352*53ee8cc1Swenshuai.xi 
353*53ee8cc1Swenshuai.xi         z5 = MULTIPLY( z10 + z12, FIX_1_847759065 ); /* 2*c2 */
354*53ee8cc1Swenshuai.xi         tmp10 = MULTIPLY( z12, FIX_1_082392200 ) - z5; /* 2*(c2-c6) */
355*53ee8cc1Swenshuai.xi         tmp12 = MULTIPLY( z10, -FIX_2_613125930 ) + z5; /* -2*(c2+c6) */
356*53ee8cc1Swenshuai.xi 
357*53ee8cc1Swenshuai.xi         tmp6 = tmp12 - tmp7;    /* phase 2 */
358*53ee8cc1Swenshuai.xi         tmp5 = tmp11 - tmp6;
359*53ee8cc1Swenshuai.xi         tmp4 = tmp10 + tmp5;
360*53ee8cc1Swenshuai.xi 
361*53ee8cc1Swenshuai.xi         wsptr[DCTSIZE * 0] = ( int )( tmp0 + tmp7 );
362*53ee8cc1Swenshuai.xi         wsptr[DCTSIZE * 7] = ( int )( tmp0 - tmp7 );
363*53ee8cc1Swenshuai.xi         wsptr[DCTSIZE * 1] = ( int )( tmp1 + tmp6 );
364*53ee8cc1Swenshuai.xi         wsptr[DCTSIZE * 6] = ( int )( tmp1 - tmp6 );
365*53ee8cc1Swenshuai.xi         wsptr[DCTSIZE * 2] = ( int )( tmp2 + tmp5 );
366*53ee8cc1Swenshuai.xi         wsptr[DCTSIZE * 5] = ( int )( tmp2 - tmp5 );
367*53ee8cc1Swenshuai.xi         wsptr[DCTSIZE * 4] = ( int )( tmp3 + tmp4 );
368*53ee8cc1Swenshuai.xi         wsptr[DCTSIZE * 3] = ( int )( tmp3 - tmp4 );
369*53ee8cc1Swenshuai.xi 
370*53ee8cc1Swenshuai.xi         inptr++;            /* advance pointers to next column */
371*53ee8cc1Swenshuai.xi         quantptr++;
372*53ee8cc1Swenshuai.xi         wsptr++;
373*53ee8cc1Swenshuai.xi     }
374*53ee8cc1Swenshuai.xi 
375*53ee8cc1Swenshuai.xi     /* Pass 2: process rows from work array, store into output array. */
376*53ee8cc1Swenshuai.xi     /* Note that we must descale the results by a factor of 8 == 2**3, */
377*53ee8cc1Swenshuai.xi     /* and also undo the PASS1_BITS scaling. */
378*53ee8cc1Swenshuai.xi 
379*53ee8cc1Swenshuai.xi     wsptr = workspace;
380*53ee8cc1Swenshuai.xi     for ( ctr = 0; ctr < DCTSIZE; ctr++ )
381*53ee8cc1Swenshuai.xi     {
382*53ee8cc1Swenshuai.xi         ///outptr = output_buf[ctr] + output_col;
383*53ee8cc1Swenshuai.xi         /* Rows of zeroes can be exploited in the same way as we did with columns.
384*53ee8cc1Swenshuai.xi          * However, the column calculation has created many nonzero AC terms, so
385*53ee8cc1Swenshuai.xi          * the simplification applies less often (typically 5% to 10% of the time).
386*53ee8cc1Swenshuai.xi          * On machines with very fast multiplication, it's possible that the
387*53ee8cc1Swenshuai.xi          * test takes more time than it's worth.  In that case this section
388*53ee8cc1Swenshuai.xi          * may be commented out.
389*53ee8cc1Swenshuai.xi          */
390*53ee8cc1Swenshuai.xi 
391*53ee8cc1Swenshuai.xi         #if 1///ndef NO_ZERO_ROW_TEST
392*53ee8cc1Swenshuai.xi         if ( wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0 )
393*53ee8cc1Swenshuai.xi         {
394*53ee8cc1Swenshuai.xi             /* AC terms all zero */
395*53ee8cc1Swenshuai.xi             //JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3) & RANGE_MASK];
396*53ee8cc1Swenshuai.xi             int dcval = IDESCALE( wsptr[0], PASS1_BITS + 3 ) + 128;
397*53ee8cc1Swenshuai.xi             clamp( dcval );
398*53ee8cc1Swenshuai.xi 
399*53ee8cc1Swenshuai.xi             outptr[0] = dcval;
400*53ee8cc1Swenshuai.xi             outptr[1] = dcval;
401*53ee8cc1Swenshuai.xi             outptr[2] = dcval;
402*53ee8cc1Swenshuai.xi             outptr[3] = dcval;
403*53ee8cc1Swenshuai.xi             outptr[4] = dcval;
404*53ee8cc1Swenshuai.xi             outptr[5] = dcval;
405*53ee8cc1Swenshuai.xi             outptr[6] = dcval;
406*53ee8cc1Swenshuai.xi             outptr[7] = dcval;
407*53ee8cc1Swenshuai.xi 
408*53ee8cc1Swenshuai.xi             wsptr += DCTSIZE;       /* advance pointer to next row */
409*53ee8cc1Swenshuai.xi             outptr += DCTSIZE;
410*53ee8cc1Swenshuai.xi             continue;
411*53ee8cc1Swenshuai.xi         }
412*53ee8cc1Swenshuai.xi         #endif
413*53ee8cc1Swenshuai.xi 
414*53ee8cc1Swenshuai.xi         /* Even part */
415*53ee8cc1Swenshuai.xi 
416*53ee8cc1Swenshuai.xi         tmp10 = ( ( DCTELEM )wsptr[0] + ( DCTELEM )wsptr[4] );
417*53ee8cc1Swenshuai.xi         tmp11 = ( ( DCTELEM )wsptr[0] - ( DCTELEM )wsptr[4] );
418*53ee8cc1Swenshuai.xi 
419*53ee8cc1Swenshuai.xi         tmp13 = ( ( DCTELEM )wsptr[2] + ( DCTELEM )wsptr[6] );
420*53ee8cc1Swenshuai.xi         tmp12 = MULTIPLY( ( DCTELEM )wsptr[2] - ( DCTELEM )wsptr[6], FIX_1_414213562 ) - tmp13;
421*53ee8cc1Swenshuai.xi 
422*53ee8cc1Swenshuai.xi         tmp0 = tmp10 + tmp13;
423*53ee8cc1Swenshuai.xi         tmp3 = tmp10 - tmp13;
424*53ee8cc1Swenshuai.xi         tmp1 = tmp11 + tmp12;
425*53ee8cc1Swenshuai.xi         tmp2 = tmp11 - tmp12;
426*53ee8cc1Swenshuai.xi 
427*53ee8cc1Swenshuai.xi         /* Odd part */
428*53ee8cc1Swenshuai.xi 
429*53ee8cc1Swenshuai.xi         z13 = ( DCTELEM )wsptr[5] + ( DCTELEM )wsptr[3];
430*53ee8cc1Swenshuai.xi         z10 = ( DCTELEM )wsptr[5] - ( DCTELEM )wsptr[3];
431*53ee8cc1Swenshuai.xi         z11 = ( DCTELEM )wsptr[1] + ( DCTELEM )wsptr[7];
432*53ee8cc1Swenshuai.xi         z12 = ( DCTELEM )wsptr[1] - ( DCTELEM )wsptr[7];
433*53ee8cc1Swenshuai.xi 
434*53ee8cc1Swenshuai.xi         tmp7 = z11 + z13;       /* phase 5 */
435*53ee8cc1Swenshuai.xi         tmp11 = MULTIPLY( z11 - z13, FIX_1_414213562 ); /* 2*c4 */
436*53ee8cc1Swenshuai.xi 
437*53ee8cc1Swenshuai.xi         z5 = MULTIPLY( z10 + z12, FIX_1_847759065 ); /* 2*c2 */
438*53ee8cc1Swenshuai.xi         tmp10 = MULTIPLY( z12, FIX_1_082392200 ) - z5; /* 2*(c2-c6) */
439*53ee8cc1Swenshuai.xi         tmp12 = MULTIPLY( z10, -FIX_2_613125930 ) + z5; /* -2*(c2+c6) */
440*53ee8cc1Swenshuai.xi 
441*53ee8cc1Swenshuai.xi         tmp6 = tmp12 - tmp7;    /* phase 2 */
442*53ee8cc1Swenshuai.xi         tmp5 = tmp11 - tmp6;
443*53ee8cc1Swenshuai.xi         tmp4 = tmp10 + tmp5;
444*53ee8cc1Swenshuai.xi 
445*53ee8cc1Swenshuai.xi         /* Final output stage: scale down by a factor of 8 and range-limit */
446*53ee8cc1Swenshuai.xi         /*
447*53ee8cc1Swenshuai.xi         outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
448*53ee8cc1Swenshuai.xi                 & RANGE_MASK];
449*53ee8cc1Swenshuai.xi         outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
450*53ee8cc1Swenshuai.xi                 & RANGE_MASK];
451*53ee8cc1Swenshuai.xi         outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
452*53ee8cc1Swenshuai.xi                 & RANGE_MASK];
453*53ee8cc1Swenshuai.xi         outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
454*53ee8cc1Swenshuai.xi                 & RANGE_MASK];
455*53ee8cc1Swenshuai.xi         outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
456*53ee8cc1Swenshuai.xi                 & RANGE_MASK];
457*53ee8cc1Swenshuai.xi         outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
458*53ee8cc1Swenshuai.xi                 & RANGE_MASK];
459*53ee8cc1Swenshuai.xi         outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
460*53ee8cc1Swenshuai.xi                 & RANGE_MASK];
461*53ee8cc1Swenshuai.xi         outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
462*53ee8cc1Swenshuai.xi                 & RANGE_MASK];
463*53ee8cc1Swenshuai.xi         */
464*53ee8cc1Swenshuai.xi         i = IDESCALE( tmp0 + tmp7, PASS1_BITS + 3 ) + 128;
465*53ee8cc1Swenshuai.xi         clamp( i );
466*53ee8cc1Swenshuai.xi         outptr[0] = ( U8 )i;
467*53ee8cc1Swenshuai.xi         i = IDESCALE( tmp0 - tmp7, PASS1_BITS + 3 ) + 128;
468*53ee8cc1Swenshuai.xi         clamp( i );
469*53ee8cc1Swenshuai.xi         outptr[7] = ( U8 )i;
470*53ee8cc1Swenshuai.xi         i = IDESCALE( tmp1 + tmp6, PASS1_BITS + 3 ) + 128;
471*53ee8cc1Swenshuai.xi         clamp( i );
472*53ee8cc1Swenshuai.xi         outptr[1] = ( U8 )i;
473*53ee8cc1Swenshuai.xi         i = IDESCALE( tmp1 - tmp6, PASS1_BITS + 3 ) + 128;
474*53ee8cc1Swenshuai.xi         clamp( i );
475*53ee8cc1Swenshuai.xi         outptr[6] = ( U8 )i;
476*53ee8cc1Swenshuai.xi         i = IDESCALE( tmp2 + tmp5, PASS1_BITS + 3 ) + 128;
477*53ee8cc1Swenshuai.xi         clamp( i );
478*53ee8cc1Swenshuai.xi         outptr[2] = ( U8 )i;
479*53ee8cc1Swenshuai.xi         i = IDESCALE( tmp2 - tmp5, PASS1_BITS + 3 ) + 128;
480*53ee8cc1Swenshuai.xi         clamp( i );
481*53ee8cc1Swenshuai.xi         outptr[5] = ( U8 )i;
482*53ee8cc1Swenshuai.xi         i = IDESCALE( tmp3 + tmp4, PASS1_BITS + 3 ) + 128;
483*53ee8cc1Swenshuai.xi         clamp( i );
484*53ee8cc1Swenshuai.xi         outptr[4] = ( U8 )i;
485*53ee8cc1Swenshuai.xi         i = IDESCALE( tmp3 - tmp4, PASS1_BITS + 3 ) + 128;
486*53ee8cc1Swenshuai.xi         clamp( i );
487*53ee8cc1Swenshuai.xi         outptr[3] = ( U8 )i;
488*53ee8cc1Swenshuai.xi 
489*53ee8cc1Swenshuai.xi         wsptr += DCTSIZE;       /* advance pointer to next row */
490*53ee8cc1Swenshuai.xi         outptr += DCTSIZE;
491*53ee8cc1Swenshuai.xi     }
492*53ee8cc1Swenshuai.xi }
493*53ee8cc1Swenshuai.xi 
494*53ee8cc1Swenshuai.xi #endif /* DCT_IFAST_SUPPORTED */
495