xref: /utopia/UTPA2-700.0.x/modules/ojpd_vdec_v1/api/jpeg/cmodel/src/jpeg_cmodel.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 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    jpeg_cmodel.c
98 /// @brief  JPEG CMODEL
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 //-------------------------------------------------------------------------------------------------
103 //  Include Files
104 //-------------------------------------------------------------------------------------------------
105 #include "jpegmain.h"
106 #include "jpeg_cmodel.h"
107 #include "apiJPEG.h"
108 #include <setjmp.h>
109 
110 //-------------------------------------------------------------------------------------------------
111 //  Local Compiler Options
112 //-------------------------------------------------------------------------------------------------
113 
114 
115 //-------------------------------------------------------------------------------------------------
116 //  Local Defines
117 //-------------------------------------------------------------------------------------------------
118 #define SCALEBITS 16
119 #define ONE_HALF ((long) 1 << (SCALEBITS-1))
120 #define FIX(x) ((long) ((x) * (1L<<SCALEBITS) + 0.5))
121 
122 #define HUFF_EXTEND(x,s)    ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
123 
124 //-------------------------------------------------------------------------------------------------
125 //  Local Structures
126 //-------------------------------------------------------------------------------------------------
127 
128 
129 //-------------------------------------------------------------------------------------------------
130 //  Global Variables
131 //-------------------------------------------------------------------------------------------------
132 // Coefficients are stored in this sequence in the data stream.
133 const MS_U8 _u8ZAG[64] =
134 {
135     0, 1, 8, 16, 9, 2, 3, 10,
136     17, 24, 32, 25, 18, 11, 4, 5,
137     12, 19, 26, 33, 40, 48, 41, 34,
138     27, 20, 13, 6, 7, 14, 21, 28,
139     35, 42, 49, 56, 57, 50, 43, 36,
140     29, 22, 15, 23, 30, 37, 44, 51,
141     58, 59, 52, 45, 38, 31, 39, 46,
142     53, 60, 61, 54, 47, 55, 62, 63
143 };
144 // This table is only used in JPD
145 const MS_U8 _u8Jpeg_zigzag_order[64] =
146 {
147     0, 1, 5, 6, 14, 15, 27, 28,
148     2, 4, 7, 13, 16, 26, 29, 42,
149     3, 8, 12, 17, 25, 30, 41, 43,
150     9, 11, 18, 24, 31, 40, 44, 53,
151     10, 19, 23, 32, 39, 45, 52, 54,
152     20, 22, 33, 38, 46, 51, 55, 60,
153     21, 34, 37, 47, 50, 56, 59, 61,
154     35, 36, 48, 49, 57, 58, 62, 63
155 };
156 
157 extern const MS_S32 extend_test[16];
158 extern const MS_S32 extend_offset[16];
159 
160 extern MS_U16 _u16Image_x_size;
161 extern JPEG_HuffInfo _Huff_info[JPEG_MAXHUFFTABLES];
162 
163 extern JPEG_QuantTbl _QuantTables[JPEG_MAXQUANTTABLES];
164 
165 extern MS_U8 _u8Comps_in_frame;
166 extern MS_U8 _u8Comp_quant[JPEG_MAXCOMPONENTS];
167 extern MS_U8 _u8Comp_ident[JPEG_MAXCOMPONENTS];
168 extern MS_U8 _u8LumaCi;
169 extern MS_U8 _u8ChromaCi;
170 extern MS_U8 _u8Comp_dc_tab[JPEG_MAXCOMPONENTS];
171 extern MS_U8 _u8Comp_ac_tab[JPEG_MAXCOMPONENTS];
172 extern MS_U8 _u8Blocks_per_mcu;
173 extern MS_U32 _u32Max_blocks_per_row;
174 extern MS_U16 _u16Mcus_per_row;
175 
176 extern MS_U8 _u8Mcu_org[JPEG_MAXBLOCKSPERMCU];
177 extern MS_U8 gu8Max_mcu_y_size;         /* MCU's max. Y size in pixels */
178 extern MS_U16 gu16Max_mcus_per_row;
179 
180 extern MS_U16 _u16Total_lines_left;               /* total # lines left in image */
181 extern JPEG_HuffTbl _Huff_tbls[JPEG_MAXHUFFTABLES];
182 extern MS_U32 _u32Last_dc_val[JPEG_MAXCOMPONENTS];
183 extern MS_U16 gu16Mcu_lines_left;       /* total # lines left in this MCU */
184 
185 extern MS_U8 gu8Scan_type;
186 
187 extern MS_U32 _u32In_buf_left;
188 
189 extern MS_S16 _s16Bits_left;
190 
191 extern MS_U16 _u16Restart_interval;
192 
193 extern MS_U16 _u16Restarts_left;
194 
195 extern JPEG_ErrCode _Error_code;
196 extern MS_BOOL _bReady_flag;
197 
198 extern jmp_buf _jmp_state;
199 
200 extern MS_U32 _u32Total_bytes_read;
201 
202 
203 //-------------------------------------------------------------------------------------------------
204 //  Local Variables
205 //-------------------------------------------------------------------------------------------------
206 
207 //-------------------------------------------------------------------------------------------------
208 // Local Function Prototypes
209 //-------------------------------------------------------------------------------------------------
210 extern void JPEG_load_next_row( void );
211 extern void JPEG_process_restart( void );
212 extern MS_U32 JPEG_get_bits_2( MS_U8 numbits );
213 extern MS_S32 JPEG_huff_decode(JPEG_HuffTbl *Ph);
214 extern void JPEG_terminate( MS_U16 status );
215 extern MS_U32 JPEG_process_markers( void );
216 extern void idct( JPEG_BLOCK_TYPE *data, MS_U8 *Pdst_ptr );
217 extern void msAPI_JPEG_H2V2Convert( void );
218 
219 //-------------------------------------------------------------------------------------------------
220 //  Debug Functions
221 //-------------------------------------------------------------------------------------------------
JPEG_CMODEL_DumpTables(void)222 void JPEG_CMODEL_DumpTables(void)
223 {
224     // Symbol tables
225     {
226         MS_U16 i, tbl_num_luma, tbl_num_chroma;
227         MS_U8 ci, luma_ci = 0, chroma_ci = 0;
228 
229         for(ci = 0; ci<_u8Comps_in_frame; ci++)
230         {
231             if(_u8LumaCi==_u8Comp_ident[ci])
232             {
233                 luma_ci = ci;
234                 break;
235             }
236         }
237 
238         for(ci = 0; ci<_u8Comps_in_frame; ci++)
239         {
240             if(_u8ChromaCi==_u8Comp_ident[ci])
241             {
242                 chroma_ci = ci;
243                 break;
244             }
245         }
246 
247         tbl_num_luma = _u8Comp_ac_tab[luma_ci];
248         tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
249 
250         JPEG_CMODEL_DEBUG("Symidx\n");
251         JPEG_CMODEL_DEBUG("%d %d\n", tbl_num_luma, tbl_num_chroma);
252         JPEG_CMODEL_DEBUG("Write symidx : AC\n");
253         JPEG_CMODEL_DEBUG("====\n");
254         JPEG_CMODEL_DEBUG("   // Symidx Addr\n");
255         JPEG_CMODEL_DEBUG("wriu 0x101780 0x00\n");
256         JPEG_CMODEL_DEBUG("wriu 0x101781 0x02\n");
257 #if 0
258         for ( i = 0; i < 256; i++ )
259         {
260             JPEG_DEBUG("%04x\n", ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ));
261         }
262 #else
263         for ( i = 0; i < 256; i++ )
264         {
265             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%x\n", (_Huff_info[tbl_num_luma].u8Huff_val[i]));
266             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%x\n", (_Huff_info[tbl_num_chroma].u8Huff_val[i]));
267         }
268 #endif
269 
270         tbl_num_luma = _u8Comp_dc_tab[luma_ci];
271         tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
272 
273         JPEG_CMODEL_DEBUG("%d %d\n", tbl_num_luma, tbl_num_chroma);
274         JPEG_CMODEL_DEBUG("Write symidx : DC\n");
275 #if 0
276         for ( i = 0; i < 16; i++ )
277         {
278             JPEG_CMODEL_DEBUG("%04x\n", ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ));
279         }
280 #else
281         for ( i = 0; i < 16; i++ )
282         {
283             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%x\n", (_Huff_info[tbl_num_luma].u8Huff_val[i]));
284             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%x\n", (_Huff_info[tbl_num_chroma].u8Huff_val[i]));
285         }
286 #endif
287     }
288 
289     // Q tables
290     {
291         MS_U8 i, j;
292         MS_U8 com_num = 0;
293         MS_U8 comp[JPEG_MAXCOMPONENTS];
294 
295         // Calculate how many valid quantization tables
296         memset(comp, 0, JPEG_MAXCOMPONENTS);
297         for(i = 0; i<_u8Comps_in_frame; i++)
298         {
299             comp[_u8Comp_quant[i]] = 1;
300         }
301 
302         for(i = 0; i<JPEG_MAXCOMPONENTS; i++)
303         {
304             if(comp[i]==1)
305                 com_num++;
306         }
307 
308         JPEG_CMODEL_DEBUG("Q Table\n");
309         JPEG_CMODEL_DEBUG("Component: %d\n", com_num);
310 
311         JPEG_CMODEL_DEBUG("   // IQtbl\n");
312         JPEG_CMODEL_DEBUG("====\n");
313 
314         JPEG_CMODEL_DEBUG("wriu 0x101780 0x00\n");
315         JPEG_CMODEL_DEBUG("wriu 0x101781 0x04\n");
316 
317         for ( i = 0; i < com_num; i++ )
318         {
319             JPEG_CMODEL_DEBUG("====\n");
320             JPEG_CMODEL_DEBUG("\n");
321 #if 0
322             for(j = 0; j<64; j++)
323             {
324                 JPEG_CMODEL_DEBUG("%04x\n", _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
325             }
326 #else
327             for(j = 0; j<64; j++)
328             {
329                 JPEG_CMODEL_DEBUG("wriu 0x101782 0x%x\n", (_QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]] & 0x00FF));
330                 JPEG_CMODEL_DEBUG("wriu 0x101783 0x%x\n", ((_QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]] >> 8) & 0x00FF));
331             }
332 #endif
333         }
334     }
335 
336     // SC tables
337     {
338         MS_U32 reg_value;
339         MS_U16 i, ci, valid, tbl_num_luma, tbl_num_chroma;
340         MS_U8 luma_ci = 0, chroma_ci = 0;
341 
342         JPEG_CMODEL_DEBUG("group table\n");
343         JPEG_CMODEL_DEBUG("   // Grpinf Addr\n");
344         JPEG_CMODEL_DEBUG("====\n");
345 
346         JPEG_CMODEL_DEBUG("wriu 0x101780 0x00\n");
347         JPEG_CMODEL_DEBUG("wriu 0x101781 0x00\n");
348 
349         for(ci = 0; ci<_u8Comps_in_frame; ci++)
350         {
351             if(_u8LumaCi==_u8Comp_ident[ci])
352             {
353                 luma_ci = ci;
354                 break;
355             }
356         }
357 
358         for(ci = 0; ci<_u8Comps_in_frame; ci++)
359         {
360             if(_u8ChromaCi==_u8Comp_ident[ci])
361             {
362                 chroma_ci = ci;
363                 break;
364             }
365         }
366 
367         tbl_num_luma = _u8Comp_dc_tab[luma_ci];
368         tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
369 
370         for ( i = 1; i <= 16; i++ )
371         {
372             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
373                 valid = 0;
374             else
375                 valid = 1;
376 
377             if ( valid )
378             {
379                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] << 4 );
380             }
381             else
382             {
383                 reg_value = 0;
384             }
385 #if 0
386             JPEG_CMODEL_DEBUG("%08lx\n", reg_value);
387 #else
388             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%lx\n", (reg_value & 0x000000FF));
389             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%lx\n", ((reg_value >> 8) & 0x000000FF));
390             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%lx\n", ((reg_value >> 16) & 0x000000FF));
391             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%lx\n", ((reg_value >> 24) & 0x000000FF));
392 #endif
393         }
394 
395         for ( i = 1; i <= 16; i++ )
396         {
397             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
398                 valid = 0;
399             else
400                 valid = 1;
401 
402             if ( valid )
403             {
404                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] << 4 );
405             }
406             else
407             {
408                 reg_value = 0;
409             }
410 #if 0
411             JPEG_CMODEL_DEBUG("%08lx\n", reg_value);
412 #else
413             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%lx\n", (reg_value & 0x000000FF));
414             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%lx\n", ((reg_value >> 8) & 0x000000FF));
415             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%lx\n", ((reg_value >> 16) & 0x000000FF));
416             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%lx\n", ((reg_value >> 24) & 0x000000FF));
417 #endif
418         }
419 
420         tbl_num_luma = _u8Comp_ac_tab[luma_ci];
421         tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
422 
423         for ( i = 1; i <= 16; i++ )
424         {
425             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
426                 valid = 0;
427             else
428                 valid = 1;
429 
430             if ( valid )
431             {
432                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] );
433             }
434             else
435             {
436                 reg_value = 0;
437             }
438 #if 0
439             JPEG_CMODEL_DEBUG("%08lx\n", reg_value);
440 #else
441             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%lx\n", (reg_value & 0x000000FF));
442             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%lx\n", ((reg_value >> 8) & 0x000000FF));
443             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%lx\n", ((reg_value >> 16) & 0x000000FF));
444             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%lx\n", ((reg_value >> 24) & 0x000000FF));
445 #endif
446         }
447 
448         for ( i = 1; i <= 16; i++ )
449         {
450             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
451                 valid = 0;
452             else
453                 valid = 1;
454 
455             if ( valid )
456             {
457                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] );
458             }
459             else
460             {
461                 reg_value = 0;
462             }
463 #if 0
464             JPEG_CMODEL_DEBUG("%08lx\n", reg_value);
465 #else
466             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%lx\n", (reg_value & 0x000000FF));
467             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%lx\n", ((reg_value >> 8) & 0x000000FF));
468             JPEG_CMODEL_DEBUG("wriu 0x101782 0x%lx\n", ((reg_value >> 16) & 0x000000FF));
469             JPEG_CMODEL_DEBUG("wriu 0x101783 0x%lx\n", ((reg_value >> 24) & 0x000000FF));
470 #endif
471         }
472     }
473 }
474 
475 
476 //-------------------------------------------------------------------------------------------------
477 //  Local Functions
478 //-------------------------------------------------------------------------------------------------
479 //------------------------------------------------------------------------------
480 // Performs a 2D IDCT over the entire row's coefficient buffer.
JPEG_transform_row(void)481 static void JPEG_transform_row( void )
482 {
483     {
484         JPEG_BLOCK_TYPE *Psrc_ptr = gps16Block_seg[0];
485         MS_U8 *Pdst_ptr = gpu8Sample_buf;
486         MS_U32 i;
487 
488         for ( i = _u32Max_blocks_per_row; i > 0; i-- )
489         {
490             // Copy the block to a temp. buffer to prevent the IDCT
491             // from modifying the entire block.
492             //kevinhuang, use an internal array in idct to avoid memcpy to save time
493             //memcpy(temp_block, Psrc_ptr, 64 * sizeof(JPEG_BLOCK_TYPE));
494             //idct(temp_block, Pdst_ptr);
495             idct( Psrc_ptr, Pdst_ptr );
496             Psrc_ptr += 64;
497             Pdst_ptr += 64;
498         }
499     }
500 }
501 
502 //------------------------------------------------------------------------------
503 // Decodes and dequantizes the next row of coefficients.
JPEG_decode_next_row(void)504 static void JPEG_decode_next_row( void )
505 {
506     MS_U16 row_block = 0;
507     MS_U16 mcu_row, mcu_block, k;
508 
509     // Clearing the entire row block buffer can take a lot of time!
510     // Instead of clearing the entire buffer each row, keep track
511     // of the number of nonzero entries written to each block and do
512     // selective clears.
513     //memset(gps16Block_seg[0], 0, _u16Mcus_per_row * _u8Blocks_per_mcu * 64 * sizeof(JPEG_BLOCK_TYPE));
514 
515     for ( mcu_row = 0; mcu_row < _u16Mcus_per_row; mcu_row++ )
516     {
517         if ( ( _u16Restart_interval ) && ( _u16Restarts_left == 0 ) )
518         {
519             JPEG_process_restart();
520         }
521 
522         for ( mcu_block = 0; mcu_block < _u8Blocks_per_mcu; mcu_block++ )
523         {
524             MS_U8 component_id = _u8Mcu_org[mcu_block];
525             MS_U8 prev_num_set;
526             JPEG_HuffTbl *Ph;
527 
528             JPEG_BLOCK_TYPE *p = gps16Block_seg[row_block];
529             JPEG_QUANT_TYPE *q = _QuantTables[_u8Comp_quant[component_id]].s16Value;
530             MS_U32 r, s;
531 
532             if ( ( s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_dc_tab[component_id]] ) ) != 0 )
533             {
534                 r = JPEG_get_bits_2( s );
535                 s = HUFF_EXTEND( r, s );
536             }
537 
538             _u32Last_dc_val[component_id] = ( s += _u32Last_dc_val[component_id] );
539 
540             p[0] = s * q[0];
541 
542             prev_num_set = gu8Block_max_zag_set[row_block];
543 
544             Ph = &_Huff_tbls[_u8Comp_ac_tab[component_id]];
545 
546             for ( k = 1; k < 64; k++ )
547             {
548                 s = JPEG_huff_decode( Ph );
549 
550                 r = s >> 4;
551                 s &= 15;
552 
553                 if ( s )
554                 {
555                     if ( r )
556                     {
557                         if ( ( k + r ) > 63 )
558                         {
559                             JPEG_terminate( E_JPEG_DECODE_ERROR );
560                         }
561 
562                         if ( k < prev_num_set )
563                         {
564                             MS_U32 n = min( r, prev_num_set - k );
565                             MS_U16 kt = k;
566                             while ( n-- )
567                             {
568                                 p[_u8ZAG[kt++]] = 0;
569                             }
570                         }
571 
572                         k += r;
573                     }
574 
575                     r = JPEG_get_bits_2( s );
576                     s = HUFF_EXTEND( r, s );
577 
578                     //assert(k < 64);
579 
580                     p[_u8ZAG[k]] = s * q[k];
581                 }
582                 else
583                 {
584                     if ( r == 15 )
585                     {
586                         if ( ( k + 15 ) > 63 )
587                         {
588                             JPEG_terminate( E_JPEG_DECODE_ERROR );
589                         }
590 
591                         if ( k < prev_num_set )
592                         {
593                             MS_U16 n = min( 16, prev_num_set - k );        //bugfix Dec. 19, 2001 - was 15!
594                             MS_U16 kt = k;
595                             while ( n-- )
596                             {
597                                 p[_u8ZAG[kt++]] = 0;
598                             }
599                         }
600 
601                         k += 15;
602                     }
603                     else
604                     {
605                         //while (k < 64)
606                         //  p[ZAG[k++]] = 0;
607 
608                         break;
609                     }
610                 }
611             }
612 
613             if ( k < prev_num_set )
614             {
615                 MS_U16 kt = k;
616                 while ( kt < prev_num_set )
617                 {
618                     p[_u8ZAG[kt++]] = 0;
619                 }
620             }
621 
622             gu8Block_max_zag_set[row_block] = k;
623 
624             //block_num[row_block++] = k;
625             row_block++;
626         }
627 
628         _u16Restarts_left--;
629     }
630 }
631 
632 
633 //------------------------------------------------------------------------------
634 // Find end of image (EOI) marker, so we can return to the user the
635 // exact size of the input stream.
JPEG_find_eoi(void)636 static void JPEG_find_eoi( void )
637 {
638     if ( !MApi_JPEG_IsProgressive() )
639     {
640         // Prime the bit buffer
641         _s16Bits_left = 0;
642 
643         // The next marker _should_ be EOI
644         JPEG_process_markers();
645     }
646 
647     _u32Total_bytes_read -= _u32In_buf_left;
648 }
649 
650 //------------------------------------------------------------------------------
651 // YCbCr H1V1 (1x1:1:1, 3 blocks per MCU) to 24-bit RGB
JPEG_CMODEL_H1V1Convert(void)652 static void JPEG_CMODEL_H1V1Convert( void )
653 {
654     MS_U8 row = gu8Max_mcu_y_size - gu16Mcu_lines_left;
655     MS_U8 *d = pgu8Scan_line_0;
656     MS_U8 *s = gpu8Sample_buf + row * 8;
657     MS_U16 i, j;
658 
659     for ( i = gu16Max_mcus_per_row; i > 0; i-- )
660     {
661         for ( j = 0; j < 8; j++ )
662         {
663             MS_U8 y = s[j];
664             MS_U8 cb = s[64 + j];
665             MS_U8 cr = s[128 + j];
666 
667             d[0] = JPEG_CMODEL_clamp( y + gs32Crr[cr] );
668             d[1] = JPEG_CMODEL_clamp( y + ( ( gs32Crg[cr] + gs32Cbg[cb] ) >> 16 ) );
669             d[2] = JPEG_CMODEL_clamp( y + gs32Cbb[cb] );
670             d += 4;
671         }
672 
673         s += 64 * 3;
674     }
675 }
676 
677 //------------------------------------------------------------------------------
678 // YCbCr H4V1 (4x1:1:1, 4 blocks per MCU) to 24-bit RGB
679 // Wrong... but it can be used to see the decoding effect... =_=""
JPEG_CMODEL_H4V1Convert(void)680 static void JPEG_CMODEL_H4V1Convert( void )
681 {
682     MS_U8 row = gu8Max_mcu_y_size - gu16Mcu_lines_left;
683     MS_U8 *d0 = pgu8Scan_line_0;
684     MS_U8 *y = gpu8Sample_buf + row * 8;
685     MS_U8 *c = gpu8Sample_buf + 4 * 64 + row * 8;
686     MS_U16 i, l, j;
687 
688     for ( i = gu16Max_mcus_per_row; i > 0; i-- )
689     {
690         for ( l = 0; l < 4; l++ )
691         {
692             for ( j = 0; j < 4; j++ )
693             {
694                 MS_U8 cb = c[0];
695                 MS_U8 cr = c[64];
696 
697                 MS_S32 rc = gs32Crr[cr];
698                 MS_S32 gc = ( ( gs32Crg[cr] + gs32Cbg[cb] ) >> 16 );
699                 MS_S32 bc = gs32Cbb[cb];
700 
701                 MS_U16 yy = y[j << 1];
702                 d0[0] = JPEG_CMODEL_clamp( yy + rc );
703                 d0[1] = JPEG_CMODEL_clamp( yy + gc );
704                 d0[2] = JPEG_CMODEL_clamp( yy + bc );
705 
706                 yy = y[( j << 1 ) + 1];
707                 d0[4] = JPEG_CMODEL_clamp( yy + rc );
708                 d0[5] = JPEG_CMODEL_clamp( yy + gc );
709                 d0[6] = JPEG_CMODEL_clamp( yy + bc );
710 
711                 d0 += 8;
712 
713                 if(j%2==0)
714                     c++;
715             }
716             y += 64;
717         }
718 
719         y += 64 * 4 - 64 * 2;
720         c += 64 * 6 - 8;
721     }
722 }
723 
724 //------------------------------------------------------------------------------
725 // YCbCr H2V1 (2x1:1:1, 4 blocks per MCU) to 24-bit RGB
JPEG_CMODEL_H2V1Convert(void)726 static void JPEG_CMODEL_H2V1Convert( void )
727 {
728     MS_U8 row = gu8Max_mcu_y_size - gu16Mcu_lines_left;
729     MS_U8 *d0 = pgu8Scan_line_0;
730     MS_U8 *y = gpu8Sample_buf + row * 8;
731     MS_U8 *c = gpu8Sample_buf + 2 * 64 + row * 8;
732     MS_U16 i, l, j;
733 
734     for ( i = gu16Max_mcus_per_row; i > 0; i-- )
735     {
736         for ( l = 0; l < 2; l++ )
737         {
738             for ( j = 0; j < 4; j++ )
739             {
740                 MS_U8 cb = c[0];
741                 MS_U8 cr = c[64];
742 
743                 MS_S32 rc = gs32Crr[cr];
744                 MS_S32 gc = ( ( gs32Crg[cr] + gs32Cbg[cb] ) >> 16 );
745                 MS_S32 bc = gs32Cbb[cb];
746 
747                 MS_U16 yy = y[j << 1];
748                 d0[0] = JPEG_CMODEL_clamp( yy + rc );
749                 d0[1] = JPEG_CMODEL_clamp( yy + gc );
750                 d0[2] = JPEG_CMODEL_clamp( yy + bc );
751 
752                 yy = y[( j << 1 ) + 1];
753                 d0[4] = JPEG_CMODEL_clamp( yy + rc );
754                 d0[5] = JPEG_CMODEL_clamp( yy + gc );
755                 d0[6] = JPEG_CMODEL_clamp( yy + bc );
756 
757                 d0 += 8;
758 
759                 c++;
760             }
761             y += 64;
762         }
763 
764         y += 64 * 4 - 64 * 2;
765         c += 64 * 4 - 8;
766     }
767 }
768 
769 //------------------------------------------------------------------------------
770 // YCbCr H2V1 (1x2:1:1, 4 blocks per MCU) to 24-bit RGB
JPEG_CMODEL_H1V2Convert(void)771 static void JPEG_CMODEL_H1V2Convert( void )
772 {
773     MS_U8 row = gu8Max_mcu_y_size - gu16Mcu_lines_left;
774     MS_U8 *d0 = pgu8Scan_line_0;
775     MS_U8 *d1 = pgu8scan_line_1;
776     MS_U8 *y;
777     MS_U8 *c;
778     MS_U16 i, j;
779 
780     if ( row < 8 )
781     {
782         y = gpu8Sample_buf + row * 8;
783     }
784     else
785     {
786         y = gpu8Sample_buf + 64 * 1 + ( row & 7 ) * 8;
787     }
788 
789     c = gpu8Sample_buf + 64 * 2 + ( row >> 1 ) * 8;
790 
791     for ( i = gu16Max_mcus_per_row; i > 0; i-- )
792     {
793         for ( j = 0; j < 8; j++ )
794         {
795             MS_U8 cb = c[0 + j];
796             MS_U8 cr = c[64 + j];
797 
798             MS_S32 rc = gs32Crr[cr];
799             MS_S32 gc = ( ( gs32Crg[cr] + gs32Cbg[cb] ) >> 16 );
800             MS_S32 bc = gs32Cbb[cb];
801 
802             MS_U16 yy = y[j];
803             d0[0] = JPEG_CMODEL_clamp( yy + rc );
804             d0[1] = JPEG_CMODEL_clamp( yy + gc );
805             d0[2] = JPEG_CMODEL_clamp( yy + bc );
806 
807             yy = y[8 + j];
808             d1[0] = JPEG_CMODEL_clamp( yy + rc );
809             d1[1] = JPEG_CMODEL_clamp( yy + gc );
810             d1[2] = JPEG_CMODEL_clamp( yy + bc );
811 
812             d0 += 4;
813             d1 += 4;
814         }
815 
816         y += 64 * 4;
817         c += 64 * 4;
818     }
819 }
820 
821 //------------------------------------------------------------------------------
JPEG_CMODEL_GrayConvert(void)822 static void JPEG_CMODEL_GrayConvert( void )
823 {
824     MS_U8 row = gu8Max_mcu_y_size - gu16Mcu_lines_left;
825     MS_U8 *d = pgu8Scan_line_0;
826     MS_U8 *s = gpu8Sample_buf + row * 8;
827     MS_U16 i;
828 
829     for ( i = gu16Max_mcus_per_row; i > 0; i-- )
830     {
831         *( MS_U32 * )d = *( MS_U32 * )s;
832         *( MS_U32 * )( &d[4] ) = *( MS_U32 * )( &s[4] );
833 
834         s += 64;
835         d += 8;
836     }
837 }
838 
839 
840 //-------------------------------------------------------------------------------------------------
841 //  Global Functions
842 //-------------------------------------------------------------------------------------------------
843 //------------------------------------------------------------------------------
844 // Create a few tables that allow us to quickly convert YCbCr to RGB.
JPEG_CMODEL_create_look_ups(void)845 void JPEG_CMODEL_create_look_ups( void )
846 {
847     MS_S16 i, k;
848     //kevinhuang, modify
849     /*
850     for (i = 0; i <= 255; i++)
851     {
852       //k = (i * 2) - 255;
853         k = (i * 2) - 256; // Dec. 28 2001- change so table[128] == 0
854       gs32Crr[i] = ( FIX(1.40200/2)  * k + ONE_HALF) >> SCALEBITS;
855       gs32Cbb[i] = ( FIX(1.77200/2)  * k + ONE_HALF) >> SCALEBITS;
856       gs32Crg[i] = (-FIX(0.71414/2)) * k;
857       gs32Cbg[i] = (-FIX(0.34414/2)) * k + ONE_HALF;
858     }
859     */
860     for ( i = 0; i <= 255; i++ )
861     {
862         k = i - 128;
863 
864         gs32Crr[i] = ( FIX( 1.40200 ) * k + ONE_HALF ) >> SCALEBITS;
865         gs32Cbb[i] = ( FIX( 1.77200 ) * k + ONE_HALF ) >> SCALEBITS;
866 
867         gs32Crg[i] = ( -FIX( 0.71414 ) ) * k ; //+ ONE_HALF) >> SCALEBITS;???
868         gs32Cbg[i] = ( -FIX( 0.34414 ) ) * k + ONE_HALF; //>> SCALEBITS;???
869     }
870 }
871 
872 //------------------------------------------------------------------------------
873 // Returns the next scan line.
874 // Returns E_JPEG_OKAY if all scan lines have been returned.
875 // Returns E_JPEG_DONE if a scan line has been returned.
876 // Returns E_JPEG_FAILED if an error occured.
JPEG_CMODEL_decode(void ** Pscan_line_ofs,MS_U32 * Pscan_line_len)877 MS_S16 JPEG_CMODEL_decode( void **Pscan_line_ofs, MS_U32 *Pscan_line_len )
878 {
879     if ( ( _Error_code ) || ( !_bReady_flag ) )
880     {
881         return ( E_JPEG_FAILED );
882     }
883 
884     if ( _u16Total_lines_left == 0 )
885     {
886         return ( E_JPEG_OKAY );
887     }
888 
889     if ( gu16Mcu_lines_left == 0 ) //decode every gu8Max_mcu_y_size (8 or 16) lines
890     {
891         if ( setjmp( _jmp_state ) )
892         {
893             return ( E_JPEG_DECODE_ERROR );
894         }
895 
896         if ( MApi_JPEG_IsProgressive() )
897         {
898             JPEG_load_next_row();
899         }
900         else
901         {
902             JPEG_decode_next_row(); //Dequantization
903         }
904 
905         // Find the EOI marker if that was the last row.
906         if ( _u16Total_lines_left <= gu8Max_mcu_y_size )
907         {
908             JPEG_find_eoi();
909         }
910 
911         JPEG_transform_row(); //IDCT
912 
913         gu16Mcu_lines_left = gu8Max_mcu_y_size;
914     }
915 
916     //YUV->RGB
917     switch ( gu8Scan_type )
918     {
919         case E_JPEG_YH2V2:
920             {
921                 if ( ( gu16Mcu_lines_left & 1 ) == 0 )
922                 {
923                     msAPI_JPEG_H2V2Convert();
924                     *Pscan_line_ofs = pgu8Scan_line_0;
925                 }
926                 else
927                 {
928                     *Pscan_line_ofs = pgu8scan_line_1;
929                 }
930 
931                 break;
932             }
933         case E_JPEG_YH4V1:
934             {
935                 JPEG_CMODEL_H4V1Convert();
936                 *Pscan_line_ofs = pgu8Scan_line_0;
937                 break;
938             }
939         case E_JPEG_YH2V1:
940             {
941                 JPEG_CMODEL_H2V1Convert();
942                 *Pscan_line_ofs = pgu8Scan_line_0;
943                 break;
944             }
945         case E_JPEG_YH1V2:
946             {
947                 if ( ( gu16Mcu_lines_left & 1 ) == 0 )
948                 {
949                     JPEG_CMODEL_H1V2Convert();
950                     *Pscan_line_ofs = pgu8Scan_line_0;
951                 }
952                 else
953                 {
954                     *Pscan_line_ofs = pgu8scan_line_1;
955                 }
956 
957                 break;
958             }
959         case E_JPEG_YH1V1:
960             {
961                 JPEG_CMODEL_H1V1Convert();
962                 *Pscan_line_ofs = pgu8Scan_line_0;
963                 break;
964             }
965         case E_JPEG_GRAYSCALE:
966             {
967                 JPEG_CMODEL_GrayConvert();
968                 *Pscan_line_ofs = pgu8Scan_line_0;
969 
970                 break;
971             }
972     }
973 
974     *Pscan_line_len = gu16Real_dest_bytes_per_scan_line;
975 
976     gu16Mcu_lines_left--;
977     _u16Total_lines_left--;
978 
979     return ( E_JPEG_DONE );
980 }
981 
982 //------------------------------------------------------------------------------
JPEG_CMODEL_get_bytes_per_pixel(void)983 MS_U8 JPEG_CMODEL_get_bytes_per_pixel( void )
984 {
985     return ( gu8Dest_bytes_per_pixel );
986 }
987 
988 //------------------------------------------------------------------------------
JPEG_CMODEL_get_num_components(void)989 MS_U8 JPEG_CMODEL_get_num_components( void ) //cmodel
990 {
991     return ( _u8Comps_in_frame );
992 }
993 //------------------------------------------------------------------------------
JPEG_CMODEL_get_total_bytes_read(void)994 MS_U32 JPEG_CMODEL_get_total_bytes_read( void ) //cmodel
995 {
996     return ( _u32Total_bytes_read );
997 }
998 
999 //------------------------------------------------------------------------------
1000 // Clamps a value between 0-255.
JPEG_CMODEL_clamp(MS_S32 i)1001 MS_U8 JPEG_CMODEL_clamp( MS_S32 i ) //???
1002 {
1003     if ( i & 0xFFFFFF00 )
1004     {
1005         i = ( ( ( ~i ) >> 31 ) & 0xFF );
1006     }
1007 
1008     return ( i );
1009 }
1010 
1011 #if 0 //useless funciton
1012 //------------------------------------------------------------------------------
1013 MS_U32 JPEG_CMODEL_get_bytes_per_scan_line( void )
1014 {
1015     return ( _u16Image_x_size * JPEG_CMODEL_get_bytes_per_pixel() );
1016 }
1017 #endif
1018 
1019 
1020