xref: /utopia/UTPA2-700.0.x/modules/ojpd_vdec_v1/api/jpeg/cmodel/src/idct.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 //
79 // 2D IDCT
80 // Derived from an older version of the IJG's JPEG software.
81 // Downloadable from: www.ijg.org
82 // This module is going to be replaced with a faster (and
83 // uncopyrighted) version.
84 // I am unable to find the original file from which this code was derived.
85 // I have included the copyright notice included with latest IJG version of this
86 // module.
87 //
88 
89 /*
90  * jidctint.c
91  *
92  * Copyright (C) 1991-1998, Thomas G. Lane.
93  * This file is part of the Independent JPEG Group's software.
94  * For conditions of distribution and use, see the accompanying README file.
95  *
96  * This file contains a slow-but-accurate integer implementation of the
97  * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
98  * must also perform dequantization of the input coefficients.
99  *
100  * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
101  * on each row (or vice versa, but it's more convenient to emit a row at
102  * a time).  Direct algorithms are also available, but they are much more
103  * complex and seem not to be any faster when reduced to code.
104  *
105  * This implementation is based on an algorithm described in
106  *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
107  *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
108  *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
109  * The primary algorithm described there uses 11 multiplies and 29 adds.
110  * We use their alternate method with 12 multiplies and 32 adds.
111  * The advantage of this method is that no data path contains more than one
112  * multiplication; this allows a very simple and accurate implementation in
113  * scaled fixed-point arithmetic, with a minimal number of shifts.
114  */
115 
116 /*----------------------------------------------------------------------------*/
117 #include "jpegmain.h"
118 #include "apiJPEG.h"
119 /*----------------------------------------------------------------------------*/
120 #define CONST_BITS  13
121 #define PASS1_BITS  2
122 #define SCALEDONE ((S32) 1)
123 #define CONST_SCALE (SCALEDONE << CONST_BITS)
124 #define FIX(x)  ((S32) ((x) * CONST_SCALE + 0.5))
125 /*----------------------------------------------------------------------------*/
126 #define FIX_0_298631336  ((S32)  2446)        /* FIX(0.298631336) */
127 #define FIX_0_390180644  ((S32)  3196)        /* FIX(0.390180644) */
128 #define FIX_0_541196100  ((S32)  4433)        /* FIX(0.541196100) */
129 #define FIX_0_765366865  ((S32)  6270)        /* FIX(0.765366865) */
130 #define FIX_0_899976223  ((S32)  7373)        /* FIX(0.899976223) */
131 #define FIX_1_175875602  ((S32)  9633)        /* FIX(1.175875602) */
132 #define FIX_1_501321110  ((S32)  12299)       /* FIX(1.501321110) */
133 #define FIX_1_847759065  ((S32)  15137)       /* FIX(1.847759065) */
134 #define FIX_1_961570560  ((S32)  16069)       /* FIX(1.961570560) */
135 #define FIX_2_053119869  ((S32)  16819)       /* FIX(2.053119869) */
136 #define FIX_2_562915447  ((S32)  20995)       /* FIX(2.562915447) */
137 #define FIX_3_072711026  ((S32)  25172)       /* FIX(3.072711026) */
138 /*----------------------------------------------------------------------------*/
139 #define DESCALE(x,n)  (((x) + (SCALEDONE << ((n)-1))) >> (n))
140 /*----------------------------------------------------------------------------*/
141 #define MULTIPLY(var,cnst)  ((var) * (cnst))
142 #define clamp(i) if (i & 0xFF00) i = (((~i) >> 15) & 0xFF);
143 
144 /*----------------------------------------------------------------------------*/
idct(JPEG_BLOCK_TYPE * data,U8 * Pdst_ptr)145 void idct( JPEG_BLOCK_TYPE *data, U8 *Pdst_ptr )
146 {
147     S32 tmp0, tmp1, tmp2, tmp3;
148     S32 tmp10, tmp11, tmp12, tmp13;
149     S32 z1, z2, z3, z4, z5;
150     register JPEG_BLOCK_TYPE *dataptr;
151     int rowctr;
152 
153     //kevinhuang, use an internal array in idct to avoid memcpy to save time
154     JPEG_BLOCK_TYPE workspace[64];
155     JPEG_BLOCK_TYPE *wsptr;
156 //printf("idct::idct\n");
157     dataptr = data;
158     wsptr = workspace;
159     for ( rowctr = 8 - 1; rowctr >= 0; rowctr-- )
160     {
161         if ( ( dataptr[1] | dataptr[2] | dataptr[3] | dataptr[4] | dataptr[5] | dataptr[6] | dataptr[7] ) == 0 )
162         {
163             S16 dcval = ( S16 )( dataptr[0] << PASS1_BITS );
164 
165             wsptr[0] = dcval;
166             wsptr[1] = dcval;
167             wsptr[2] = dcval;
168             wsptr[3] = dcval;
169             wsptr[4] = dcval;
170             wsptr[5] = dcval;
171             wsptr[6] = dcval;
172             wsptr[7] = dcval;
173 
174             dataptr += 8;       /* advance pointer to next row */
175             wsptr += 8;
176             continue;
177         }
178 
179         z2 = ( S32 )dataptr[2];
180         z3 = ( S32 )dataptr[6];
181 
182         z1 = MULTIPLY( z2 + z3, FIX_0_541196100 );
183         tmp2 = z1 + MULTIPLY( z3, -FIX_1_847759065 );
184         tmp3 = z1 + MULTIPLY( z2, FIX_0_765366865 );
185 
186         tmp0 = ( ( S32 )dataptr[0] + ( S32 )dataptr[4] ) << CONST_BITS;
187         tmp1 = ( ( S32 )dataptr[0] - ( S32 )dataptr[4] ) << CONST_BITS;
188 
189         tmp10 = tmp0 + tmp3;
190         tmp13 = tmp0 - tmp3;
191         tmp11 = tmp1 + tmp2;
192         tmp12 = tmp1 - tmp2;
193 
194         tmp0 = ( S32 )dataptr[7];
195         tmp1 = ( S32 )dataptr[5];
196         tmp2 = ( S32 )dataptr[3];
197         tmp3 = ( S32 )dataptr[1];
198 
199         z1 = tmp0 + tmp3;
200         z2 = tmp1 + tmp2;
201         z3 = tmp0 + tmp2;
202         z4 = tmp1 + tmp3;
203         z5 = MULTIPLY( z3 + z4, FIX_1_175875602 );
204 
205         tmp0 = MULTIPLY( tmp0, FIX_0_298631336 );
206         tmp1 = MULTIPLY( tmp1, FIX_2_053119869 );
207         tmp2 = MULTIPLY( tmp2, FIX_3_072711026 );
208         tmp3 = MULTIPLY( tmp3, FIX_1_501321110 );
209         z1 = MULTIPLY( z1, -FIX_0_899976223 );
210         z2 = MULTIPLY( z2, -FIX_2_562915447 );
211         z3 = MULTIPLY( z3, -FIX_1_961570560 );
212         z4 = MULTIPLY( z4, -FIX_0_390180644 );
213 
214         z3 += z5;
215         z4 += z5;
216 
217         tmp0 += z1 + z3;
218         tmp1 += z2 + z4;
219         tmp2 += z2 + z3;
220         tmp3 += z1 + z4;
221 
222         wsptr[0] = ( S16 )DESCALE( tmp10 + tmp3, CONST_BITS - PASS1_BITS );
223         wsptr[7] = ( S16 )DESCALE( tmp10 - tmp3, CONST_BITS - PASS1_BITS );
224         wsptr[1] = ( S16 )DESCALE( tmp11 + tmp2, CONST_BITS - PASS1_BITS );
225         wsptr[6] = ( S16 )DESCALE( tmp11 - tmp2, CONST_BITS - PASS1_BITS );
226         wsptr[2] = ( S16 )DESCALE( tmp12 + tmp1, CONST_BITS - PASS1_BITS );
227         wsptr[5] = ( S16 )DESCALE( tmp12 - tmp1, CONST_BITS - PASS1_BITS );
228         wsptr[3] = ( S16 )DESCALE( tmp13 + tmp0, CONST_BITS - PASS1_BITS );
229         wsptr[4] = ( S16 )DESCALE( tmp13 - tmp0, CONST_BITS - PASS1_BITS );
230 
231         dataptr += 8;
232         wsptr += 8;
233     }
234 
235     dataptr = workspace;
236     for ( rowctr = 8 - 1; rowctr >= 0; rowctr-- )
237     {
238         S16 i;
239 
240         if ( ( dataptr[8 * 1] | dataptr[8 * 2] | dataptr[8 * 3] | dataptr[8 * 4] | dataptr[8 * 5] | dataptr[8 * 6] | dataptr[8 * 7] ) == 0 )
241         {
242             S16 dcval = ( S16 )DESCALE( ( S32 )dataptr[0], PASS1_BITS + 3 ) + 128;
243             /*
244             if ((dcval) < 0)
245               dcval = 0;
246             else if (dcval > 255)
247               dcval = 255;
248             */
249             clamp( dcval );
250 
251             Pdst_ptr[8 * 0] = ( U8 )dcval;
252             Pdst_ptr[8 * 1] = ( U8 )dcval;
253             Pdst_ptr[8 * 2] = ( U8 )dcval;
254             Pdst_ptr[8 * 3] = ( U8 )dcval;
255             Pdst_ptr[8 * 4] = ( U8 )dcval;
256             Pdst_ptr[8 * 5] = ( U8 )dcval;
257             Pdst_ptr[8 * 6] = ( U8 )dcval;
258             Pdst_ptr[8 * 7] = ( U8 )dcval;
259 
260             dataptr++;
261             Pdst_ptr++;
262             continue;
263         }
264 
265         z2 = ( S32 )dataptr[8 * 2];
266         z3 = ( S32 )dataptr[8 * 6];
267 
268         z1 = MULTIPLY( z2 + z3, FIX_0_541196100 );
269         tmp2 = z1 + MULTIPLY( z3, -FIX_1_847759065 );
270         tmp3 = z1 + MULTIPLY( z2, FIX_0_765366865 );
271 
272         tmp0 = ( ( S32 )dataptr[8 * 0] + ( S32 )dataptr[8 * 4] ) << CONST_BITS;
273         tmp1 = ( ( S32 )dataptr[8 * 0] - ( S32 )dataptr[8 * 4] ) << CONST_BITS;
274 
275         tmp10 = tmp0 + tmp3;
276         tmp13 = tmp0 - tmp3;
277         tmp11 = tmp1 + tmp2;
278         tmp12 = tmp1 - tmp2;
279 
280         tmp0 = ( S32 )dataptr[8 * 7];
281         tmp1 = ( S32 )dataptr[8 * 5];
282         tmp2 = ( S32 )dataptr[8 * 3];
283         tmp3 = ( S32 )dataptr[8 * 1];
284 
285         z1 = tmp0 + tmp3;
286         z2 = tmp1 + tmp2;
287         z3 = tmp0 + tmp2;
288         z4 = tmp1 + tmp3;
289         z5 = MULTIPLY( z3 + z4, FIX_1_175875602 );
290 
291         tmp0 = MULTIPLY( tmp0, FIX_0_298631336 );
292         tmp1 = MULTIPLY( tmp1, FIX_2_053119869 );
293         tmp2 = MULTIPLY( tmp2, FIX_3_072711026 );
294         tmp3 = MULTIPLY( tmp3, FIX_1_501321110 );
295         z1 = MULTIPLY( z1, -FIX_0_899976223 );
296         z2 = MULTIPLY( z2, -FIX_2_562915447 );
297         z3 = MULTIPLY( z3, -FIX_1_961570560 );
298         z4 = MULTIPLY( z4, -FIX_0_390180644 );
299 
300         z3 += z5;
301         z4 += z5;
302 
303         tmp0 += z1 + z3;
304         tmp1 += z2 + z4;
305         tmp2 += z2 + z3;
306         tmp3 += z1 + z4;
307 
308         i = ( S16 )DESCALE( tmp10 + tmp3, CONST_BITS + PASS1_BITS + 3 ) + 128;
309         clamp( i )
310         Pdst_ptr[8 * 0] = ( U8 )i;
311 
312         i = ( S16 )DESCALE( tmp10 - tmp3, CONST_BITS + PASS1_BITS + 3 ) + 128;
313         clamp( i )
314         Pdst_ptr[8 * 7] = ( U8 )i;
315 
316         i = ( S16 )DESCALE( tmp11 + tmp2, CONST_BITS + PASS1_BITS + 3 ) + 128;
317         clamp( i )
318         Pdst_ptr[8 * 1] = ( U8 )i;
319 
320         i = ( S16 )DESCALE( tmp11 - tmp2, CONST_BITS + PASS1_BITS + 3 ) + 128;
321         clamp( i )
322         Pdst_ptr[8 * 6] = ( U8 )i;
323 
324         i = ( S16 )DESCALE( tmp12 + tmp1, CONST_BITS + PASS1_BITS + 3 ) + 128;
325         clamp( i )
326         Pdst_ptr[8 * 2] = ( U8 )i;
327 
328         i = ( S16 )DESCALE( tmp12 - tmp1, CONST_BITS + PASS1_BITS + 3 ) + 128;
329         clamp( i )
330         Pdst_ptr[8 * 5] = ( U8 )i;
331 
332         i = ( S16 )DESCALE( tmp13 + tmp0, CONST_BITS + PASS1_BITS + 3 ) + 128;
333         clamp( i )
334         Pdst_ptr[8 * 3] = ( U8 )i;
335 
336         i = ( S16 )DESCALE( tmp13 - tmp0, CONST_BITS + PASS1_BITS + 3 ) + 128;
337         clamp( i )
338         Pdst_ptr[8 * 4] = ( U8 )i;
339 
340         dataptr++;
341         Pdst_ptr++;
342     }
343 }
344 /*----------------------------------------------------------------------------*/
345 
346