xref: /utopia/UTPA2-700.0.x/modules/ojpd_vdec_v1/api/jpeg/apiJPEG.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    apiJPEG.c
98 /// @brief  JPEG API
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 //-------------------------------------------------------------------------------------------------
104 //  Include Files
105 //-------------------------------------------------------------------------------------------------
106 #ifndef CMODEL
107 #include "MsCommon.h"
108 #include "MsVersion.h"
109 #include "MsOS.h"
110 #include "asmCPU.h"
111 #include "jpeg_def.h"
112 #include "drvJPD.h"
113 //#include "Utl.h"
114 ////#include "drvUART.h"
115 #else
116 #include "jpeg_cmodel_def.h"
117 #include "jpeg_cmodel.h"
118 #endif
119 #ifndef MSOS_TYPE_LINUX_KERNEL
120 #include <setjmp.h>
121 #endif
122 #include "jpeg_memory.h"
123 #include "apiJPEG.h"
124 
125 #ifdef MSOS_TYPE_LINUX_KERNEL
126 #include <linux/string.h>
127 #else
128 #include <string.h>
129 #endif
130 
131 #include "utopia.h"
132 #include "apiJPEG_priv.h"
133 #include "apiJPEG_v2.h"
134 void* pInstantJPD = NULL;
135 
136 //-------------------------------------------------------------------------------------------------
137 //  Local Compiler Options
138 //-------------------------------------------------------------------------------------------------
139 // PS. CMODEL always supports progressive mode decode
140 #define SUPPORT_PROGRESSIVE_MODE    1
141 
142 #define ENABLE_JPEG_NO_SIZE_LOWER_BOUND 0
143 
144 #define SUPPORT_PROGRESSIVE_SCLAEDOWN_MODE    1 //CL82399
145 
146 #define SW_OPTIMIZE         0
147 #define SW_JPD_RGB_CMYK     1
148 #ifdef CMODEL
149 // This funcion is not ready in CMODEL
150 #undef SW_JPD_RGB_CMYK
151 #define SW_JPD_RGB_CMYK     0
152 #endif
153 
154 /* Need to check whether OS support jump API or not */
155 #ifdef CMODEL
156 #define JPD_LONGJUMP_SUPPORT
157 #endif
158 #if 0
159 #if !defined(MSOS_TYPE_ECOS)
160 #define USE_LIBC
161 #endif
162 #ifndef USE_LIBC
163 #define jmp_buf             MS_U32
164 #define setjmp(jmp_state)   FALSE
165 #define longjmp(jmp_state, status)
166 #endif
167 #endif
168 
169 #define PRINT_JPD_DECODE_TIME   0
170 #define LOG_DATA_TO_USB         0
171 //-------------------------------------------------------------------------------------------------
172 //  Local Defines
173 //-------------------------------------------------------------------------------------------------
174 // Max. allocated blocks
175 #ifdef CMODEL
176 #define JPEG_MAXBLOCKS    100
177 #else
178 #define JPEG_MAXBLOCKS    50
179 #endif
180 
181 #define DEFAULT_DECODE_TIMEOUT  100
182 
183 //fractional bits in scale factors
184 #define IFAST_SCALE_BITS    2
185 
186 #define JPEG_TIFF_SOI_OFFSET        0x0201
187 #define JPEG_TIFF_JPEG_IMG_BYTES    0x0202
188 
189 #define JPEG_TIFF_BIG_ENDIAN        0x4D4D
190 #define JPEG_TIFF_LITTLE_ENDIAN     0x4949
191 
192 //EXIF Tag
193 #define JPEG_EXIF_TAG_MANUFACTURER              0x010F
194 #define JPEG_EXIF_TAG_MODEL                     0x0110
195 #define JPEG_EXIF_TAG_ORIENTATION               0x0112
196 #define JPEG_EXIF_TAG_DATETIME_MOD              0x0132
197 #define JPEG_EXIF_TAG_IFD_POINTER               0x8769
198 #define JPEG_EXIF_TAG_EXPOSURE_PROGRAM          0x8822
199 #define JPEG_EXIF_TAG_ISO_SPEED_RATING          0x8827
200 #define JPEG_EXIF_TAG_DATETIME_ORI              0x9003
201 #define JPEG_EXIF_TAG_EXPOSURE_TIME             0x829A
202 #define JPEG_EXIF_TAG_F_NUMBER                  0x829D
203 #define JPEG_EXIF_TAG_SHUTTER_SPEED             0x9201
204 #define JPEG_EXIF_TAG_APERTURE                  0x9202
205 #define JPEG_EXIF_TAG_EXPOSURE_BIAS             0x9204
206 #define JPEG_EXIF_TAG_FLASH                     0x9209
207 #define JPEG_EXIF_TAG_FOCAL_LENGTH              0x920A
208 #define JPEG_EXIF_TAG_IMAGE_WIDTH               0xA002
209 #define JPEG_EXIF_TAG_IMAGE_HEIGHT              0xA003
210 
211 #define JPEG_MANUFACTURER_SIZE                  32
212 #define JPEG_MODEL_SIZE                         128
213 
214 #define MIN_READBUFFER_SIZE 128
215 
216 #define MRC_BUFFER_ADDR         _u32ReadBufferAddr
217 #define MRC_BUFFER_SIZE         _u32ReadBufferSize
218 #define MWC_BUFFER_ADDR         _u32WriteBufferAddr
219 #define MWC_BUFFER_SIZE         _u32WriteBufferSize
220 #define INTERNAL_BUFFER_ADDR    _u32InternalBufferAddr
221 #define INTERNAL_BUFFER_SIZE    _u32InternalBufferSize
222 
223 #define HUFF_EXTEND_TBL(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
224 
225 #define HUFF_EXTEND_P(x,s)  HUFF_EXTEND_TBL(x,s)
226 
227 /* EXIF parsing section */
228 #define EndianChangeL(_x)   \
229     ((((_x) & 0xff) << 24) | (((_x) & 0xff00) << 8) | (((_x) & 0xff0000) >> 8) | (((_x) & 0xff000000) >> 24))
230 
231 #define EndianChangeS(_x)   \
232     ((((_x) & 0xff) << 8) | (((_x) & 0xff00) >> 8))
233 
234 #define JPEG_TAG_EXIF   EndianChangeL(0x45786966)
235 
236 #define JPEG_ABS(x)     (((x)>=0)?(x):(-(x)))
237 
238 
239 static APP0_Unit App0_Unit_Data;
240 
241 // No need to do ZAG order in JPD mode
242 #if SW_JPD_RGB_CMYK
243 JPEG_STATIC MS_U16 _u16PaddingMcuNumber;
244 
245 MS_U32 u32_Decode_Line;
246 MS_U8 *u8Out_buf;
247 //#define DCTSIZE 8
248 //#define DCTELEM int
249 #define PASS1_BITS  2
250 #define FIX_3_0727  ((MS_S32)25172)  /* FIX(3.072711026) */
251 #define FIX_2_5629  ((MS_S32)20995)  /* FIX(2.562915447) */
252 #define FIX_2_0531  ((MS_S32)16819)  /* FIX(2.053119869) */
253 #define FIX_1_9615  ((MS_S32)16069)  /* FIX(1.961570560) */
254 #define FIX_1_8477  ((MS_S32)15137)  /* FIX(1.847759065) */
255 #define FIX_1_5013  ((MS_S32)12299)  /* FIX(1.501321110) */
256 #define FIX_1_1758  ((MS_S32)9633)   /* FIX(1.175875602) */
257 #define FIX_0_8999  ((MS_S32)7373)   /* FIX(0.899976223) */
258 #define FIX_0_7653  ((MS_S32)6270)   /* FIX(0.765366865) */
259 #define FIX_0_5411  ((MS_S32)4433)   /* FIX(0.541196100) */
260 #define FIX_0_3901  ((MS_S32)3196)   /* FIX(0.390180644) */
261 #define FIX_0_2986  ((MS_S32)2446)   /* FIX(0.298631336) */
262 #define SCALE_DONE ((MS_S32) 1)
263 #define DESCALE(a,m)  (((a) + (SCALE_DONE << ((m)-1))) >> (m))
264 #define clamp(i) if (i & 0xFF00) i = (((~i) >> 15) & 0xFF);
265 #define SCALEBITS 16
266 #define ONE_HALF ((MS_S32) 1 << (SCALEBITS-1))
267 #define FIX(x) ((MS_S32) ((x) * (1L<<SCALEBITS) + 0.5))
268 
269 
270 #define HUFF_EXTEND(x,s)    ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
271 
272 #ifdef CMODEL
273 // Same as drvJPD.h
274 // JPD Downscale Ratio
275 // Bellows are 1, 1/2, 1/4 and 1/8 in order
276 typedef enum
277 {
278     E_JPD_DOWNSCALE_ORG     = 0x00
279   , E_JPD_DOWNSCALE_HALF    = 0x01
280   , E_JPD_DOWNSCALE_FOURTH  = 0x02
281   , E_JPD_DOWNSCALE_EIGHTH  = 0x03
282 } JPD_DownScale;
283 #endif
284 #endif
285 
286 #if LOG_DATA_TO_USB
287 FILE *logBinfp;
288 
289 #define JPEG_DEBUG_API_MSG(format, args...)  do{if(logBinfp) fprintf(logBinfp, format, ##args);}while(0)
290 #define JPEG_DEBUG_API_ERR(format, args...)  do{if(logBinfp)  fprintf(logBinfp, format, ##args);}while(0)
291 #else
292 #define JPEG_DEBUG_API_MSG(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API) printf(format, ##args);}while(0)
293 #define JPEG_DEBUG_API_ERR(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_ERR) printf(format, ##args);}while(0)
294 #endif
295 #define printf_red(args...)           do{ printf("\033[1;31m"); printf(args); printf("\033[m"); }while(0)
296 #define UTOPIA_20_DBG(args...)        //do{ printf("\033[1;31m"); printf(args); printf("\033[m"); }while(0)
297 
298 #define JPEG_OVER_BUFFER_RET(addr, buf_addr, buf_size) \
299 do \
300 { \
301     if(((MS_U32)(addr) < (MS_U32)(buf_addr)) \
302     || ((MS_U32)(addr) >= ((MS_U32)(buf_addr) + (MS_U32)(buf_size)))) \
303     { \
304         JPEG_DEBUG_API_ERR("%s [%d] invalid address 0x%lx\n", __FUNCTION__, __LINE__, (MS_U32)(addr)); \
305         return FALSE; \
306     } \
307 } \
308 while(0)
309 
310 //-------------------------------------------------------------------------------------------------
311 //  Local Structures
312 //-------------------------------------------------------------------------------------------------
313 //JPEG header marker id
314 typedef enum
315 {
316     E_JPEG_SOF0     = 0xC0
317   , E_JPEG_SOF1     = 0xC1
318   , E_JPEG_SOF2     = 0xC2
319   , E_JPEG_SOF3     = 0xC3
320   , E_JPEG_SOF5     = 0xC5
321   , E_JPEG_SOF6     = 0xC6
322   , E_JPEG_SOF7     = 0xC7
323   , E_JPEG_JPG      = 0xC8
324   , E_JPEG_SOF9     = 0xC9
325   , E_JPEG_SOF10    = 0xCA
326   , E_JPEG_SOF11    = 0xCB
327   , E_JPEG_SOF13    = 0xCD
328   , E_JPEG_SOF14    = 0xCE
329   , E_JPEG_SOF15    = 0xCF
330   , E_JPEG_DHT      = 0xC4
331   , E_JPEG_DAC      = 0xCC
332   , E_JPEG_RST0     = 0xD0
333   , E_JPEG_RST1     = 0xD1
334   , E_JPEG_RST2     = 0xD2
335   , E_JPEG_RST3     = 0xD3
336   , E_JPEG_RST4     = 0xD4
337   , E_JPEG_RST5     = 0xD5
338   , E_JPEG_RST6     = 0xD6
339   , E_JPEG_RST7     = 0xD7
340   , E_JPEG_SOI      = 0xD8
341   , E_JPEG_EOI      = 0xD9
342   , E_JPEG_SOS      = 0xDA
343   , E_JPEG_DQT      = 0xDB
344   , E_JPEG_DNL      = 0xDC
345   , E_JPEG_DRI      = 0xDD
346   , E_JPEG_DHP      = 0xDE
347   , E_JPEG_EXP      = 0xDF
348   , E_JPEG_APP0     = 0xE0
349   , E_JPEG_APP1     = 0xE1
350   , E_JPEG_APP2     = 0xE2
351   , E_JPEG_APP3     = 0xE3
352   , E_JPEG_APP4     = 0xE4
353   , E_JPEG_APP5     = 0xE5
354   , E_JPEG_APP6     = 0xE6
355   , E_JPEG_APP7     = 0xE7
356   , E_JPEG_APP8     = 0xE8
357   , E_JPEG_APP9     = 0xE9
358   , E_JPEG_APP10     = 0xEA
359   , E_JPEG_APP11     = 0xEB
360   , E_JPEG_APP12     = 0xEC
361   , E_JPEG_APP13     = 0xED
362   , E_JPEG_APP14     = 0xEE
363   , E_JPEG_APP15    = 0xEF
364   , E_JPEG_JPG0     = 0xF0
365   , E_JPEG_JPG1     = 0xF1
366   , E_JPEG_JPG2     = 0xF2
367   , E_JPEG_JPG3     = 0xF3
368   , E_JPEG_JPG4     = 0xF4
369   , E_JPEG_JPG5     = 0xF5
370   , E_JPEG_JPG6     = 0xF6
371   , E_JPEG_JPG7     = 0xF7
372   , E_JPEG_JPG8     = 0xF8
373   , E_JPEG_JPG9     = 0xF9
374   , E_JPEG_JPG10     = 0xFA
375   , E_JPEG_JPG11     = 0xFB
376   , E_JPEG_JPG12     = 0xFC
377   , E_JPEG_JPG13    = 0xFD
378   , E_JPEG_COM      = 0xFE
379   , E_JPEG_TEM      = 0x01
380   , E_JPEG_ERROR    = 0x100
381 } JPEG_HdrMarker;
382 
383 //------------------------------------------------------------------------------
384 typedef enum
385 {
386     E_RLE_DC = 0,
387     E_RLE_AC,
388     E_RLE_ZRL,
389     E_RLE_EOB,
390 } JPEG_RLESymbol;
391 //-----------------------------------------------------------------------------
392 /// @brief \b Struct \b Name: JPEG_CoeffBuf
393 /// @brief \b Struct \b Description: The info of coefficient for JPEG decode
394 //-----------------------------------------------------------------------------
395 typedef struct
396 {
397     MS_U8 *pu8Data;         ///<data of coefficient of DC, AC
398     MS_U16 u16Block_num_x;  ///<the number of block for width
399     MS_U16 u16Block_num_y;  ///<the number of block for height
400     MS_U16 u16Block_size;   ///<block size
401     MS_U8 u8Block_len_x;    ///<The width of block
402     MS_U8 u8Block_len_y;    ///<The height of block
403 } JPEG_CoeffBuf, *PJPEG_CoeffBuf;
404 //-----------------------------------------------------------------------------
405 /// @brief \b Struct \b Name: JPEG_SVLD
406 /// @brief \b Struct \b Description: The info of SVLD for JPEG decode
407 //-----------------------------------------------------------------------------
408 typedef struct
409 {
410     union
411     {
412         struct
413         {
414             MS_U32 amp          :11;    ///<The amplitude of value of VLI
415             MS_U32 sign         :1;     ///<The sign of value of VLI
416             MS_U32 run          :4;     ///<run value
417             MS_U32 sym_type     :2;     ///<symbol type
418             MS_U32 blk_type     :2;     ///<YUV type
419             MS_U32 EOP          :1;     ///<End of picture
420             MS_U32 trash        :11;    ///<reserved
421         };
422 
423         struct
424         {
425             MS_U8 byte0;    ///<byte0 of SVLD
426             MS_U8 byte1;    ///<byte1 of SVLD
427             MS_U8 byte2;    ///<byte2 of SVLD
428             MS_U8 byte3;    ///<byte3 of SVLD
429         };
430     };
431 } JPEG_SVLD;
432 //-----------------------------------------------------------------------------
433 /// @brief \b Struct \b Name: JPEG_HdrChk
434 /// @brief \b Struct \b Description: The info of header checking for JPEG decode
435 //-----------------------------------------------------------------------------
436 typedef union
437 {
438     struct
439     {
440         MS_U8 DQT:1;///<has Quant Table?
441         MS_U8 DHT:1;///<has Huffman Table?
442 //      MS_U8 SOF:1;
443 //      MS_U8 SOS:1;
444     };
445     MS_U8 result;   ///<wildcard for header check
446 } JPEG_HdrChk;
447 //------------------------------------------------------------------------------
448 typedef MS_BOOL ( *Pdecode_block_func )( MS_U8, MS_U16, MS_U16 );
449 
450 
451 //-------------------------------------------------------------------------------------------------
452 //  Global Variables
453 //-------------------------------------------------------------------------------------------------
454 
455 
456 //-------------------------------------------------------------------------------------------------
457 //  Local Variables
458 //-------------------------------------------------------------------------------------------------
459 #ifdef CMODEL
460 JPEG_STATIC MS_U16 JPEG_MAX_HEIGHT = 0xFFFF;
461 JPEG_STATIC MS_U16 JPEG_MAX_WIDTH = 0xFFFF;
462 #else
463 JPEG_STATIC MS_U16 JPEG_MAX_HEIGHT = 1200;
464 JPEG_STATIC MS_U16 JPEG_MAX_WIDTH = 1600;
465 #endif
466 
467 // max progressive resolution setting
468 JPEG_STATIC MS_U16 JPEG_PRO_MAX_HEIGHT  = 768;
469 JPEG_STATIC MS_U16 JPEG_PRO_MAX_WIDTH   = 1024;
470 
471 JPEG_STATIC MS_U16 MAX_JPEG_WIDTH_HD    = 0;
472 JPEG_STATIC MS_U16 MAX_JPEG_HEIGHT_HD   = 0;
473 
474 #if SUPPORT_MPO_FORMAT
475 JPEG_STATIC MS_U16 JPEG_MPO_MAX_HEIGHT = 1200;
476 JPEG_STATIC MS_U16 JPEG_MPO_MAX_WIDTH = 1600;
477 JPEG_STATIC MS_U16 JPEG_MPO_PRO_MAX_HEIGHT  = 768;
478 JPEG_STATIC MS_U16 JPEG_MPO_PRO_MAX_WIDTH   = 1024;
479 #endif
480 //------------------------------------------------------------------------------
481 #if 0
482 // Default Table for JPEG
483 const MS_U16 g16GRPINFO_TBL[128]=
484 {
485 0x0000,
486 0x0000,
487 0x0000,
488 0x0100,
489 0x0010,
490 0x0140,
491 0x0060,
492 0x01e0,
493 0x0070,
494 0x01f0,
495 0x0080,
496 0x01f8,
497 0x0090,
498 0x01fc,
499 0x00a0,
500 0x01fe,
501 0x00b0,
502 0x01ff,
503 0x0000,
504 0x0000,
505 0x0000,
506 0x0000,
507 0x0000,
508 0x0000,
509 0x0000,
510 0x0000,
511 0x0000,
512 0x0000,
513 0x0000,
514 0x0000,
515 0x0000,
516 0x0000,
517 0x0000,
518 0x0000,
519 0x0000,
520 0x0100,
521 0x0030,
522 0x01c0,
523 0x0040,
524 0x01e0,
525 0x0050,
526 0x01f0,
527 0x0060,
528 0x01f8,
529 0x0070,
530 0x01fc,
531 0x0080,
532 0x01fe,
533 0x0090,
534 0x01ff,
535 0x80a0,
536 0x01ff,
537 0xc0b0,
538 0x01ff,
539 0x0000,
540 0x0000,
541 0x0000,
542 0x0000,
543 0x0000,
544 0x0000,
545 0x0000,
546 0x0000,
547 0x0000,
548 0x0000,
549 0x0000,
550 0x0000,
551 0x0000,
552 0x0100,
553 0x0002,
554 0x0180,
555 0x0003,
556 0x01a0,
557 0x0006,
558 0x01d0,
559 0x0009,
560 0x01e8,
561 0x000b,
562 0x01f0,
563 0x000f,
564 0x01f8,
565 0x0012,
566 0x01fb,
567 0x8017,
568 0x01fd,
569 0xc01c,
570 0x01fe,
571 0x4020,
572 0x01ff,
573 0x0000,
574 0x0000,
575 0x0000,
576 0x0000,
577 0x8024,
578 0x01ff,
579 0x8225,
580 0x01ff,
581 0x0000,
582 0x0000,
583 0x0000,
584 0x0100,
585 0x0002,
586 0x0180,
587 0x0003,
588 0x01a0,
589 0x0005,
590 0x01c0,
591 0x0009,
592 0x01e0,
593 0x000d,
594 0x01f0,
595 0x0010,
596 0x01f6,
597 0x0014,
598 0x01fa,
599 0x801b,
600 0x01fd,
601 0xc020,
602 0x01fe,
603 0x4024,
604 0x01ff,
605 0x0000,
606 0x0000,
607 0x8028,
608 0x01ff,
609 0x8429,
610 0x01ff,
611 0x882b,
612 0x01ff
613 };
614 #endif
615 #if 0
616 const MS_U16 g16SYMIDX_TBL[]=
617 {
618 0x0001,
619 0x0102,
620 0x0203,
621 0x0300,
622 0x1104,
623 0x0411,
624 0x0505,
625 0x2112,
626 0x3121,
627 0x0631,
628 0x1241,
629 0x4106,
630 0x5113,
631 0x0751,
632 0x6161,
633 0x7107,
634 0x1322,
635 0x2271,
636 0x3214,
637 0x8132,
638 0x0881,
639 0x1491,
640 0x42a1,
641 0x9108,
642 0xa123,
643 0xb142,
644 0xc1b1,
645 0x09c1,
646 0x2315,
647 0x3352,
648 0x52d1,
649 0xf0f0,
650 0x1524,
651 0x6233,
652 0x7262,
653 0xd172,
654 0x0a82,
655 0x1609,
656 0x240a,
657 0x3416,
658 0xe117,
659 0x2518,
660 0xf119,
661 0x171a,
662 0x1825,
663 0x1926,
664 0x1a27,
665 0x2628,
666 0x2729,
667 0x282a,
668 0x2934,
669 0x2a35,
670 0x3536,
671 0x3637,
672 0x3738,
673 0x3839,
674 0x393a,
675 0x3a43,
676 0x4344,
677 0x4445,
678 0x4546,
679 0x4647,
680 0x4748,
681 0x4849,
682 0x494a,
683 0x4a53,
684 0x5354,
685 0x5455,
686 0x5556,
687 0x5657,
688 0x5758,
689 0x5859,
690 0x595a,
691 0x5a63,
692 0x6364,
693 0x6465,
694 0x6566,
695 0x6667,
696 0x6768,
697 0x6869,
698 0x696a,
699 0x6a73,
700 0x7374,
701 0x7475,
702 0x7576,
703 0x7677,
704 0x7778,
705 0x7879,
706 0x797a,
707 0x7a83,
708 0x8284,
709 0x8385,
710 0x8486,
711 0x8587,
712 0x8688,
713 0x8789,
714 0x888a,
715 0x8992,
716 0x8a93,
717 0x9294,
718 0x9395,
719 0x9496,
720 0x9597,
721 0x9698,
722 0x9799,
723 0x989a,
724 0x99a2,
725 0x9aa3,
726 0xa2a4,
727 0xa3a5,
728 0xa4a6,
729 0xa5a7,
730 0xa6a8,
731 0xa7a9,
732 0xa8aa,
733 0xa9b2,
734 0xaab3,
735 0xb2b4,
736 0xb3b5,
737 0xb4b6,
738 0xb5b7,
739 0xb6b8,
740 0xb7b9,
741 0xb8ba,
742 0xb9c2,
743 0xbac3,
744 0xc2c4,
745 0xc3c5,
746 0xc4c6,
747 0xc5c7,
748 0xc6c8,
749 0xc7c9,
750 0xc8ca,
751 0xc9d2,
752 0xcad3,
753 0xd2d4,
754 0xd3d5,
755 0xd4d6,
756 0xd5d7,
757 0xd6d8,
758 0xd7d9,
759 0xd8da,
760 0xd9e1,
761 0xdae2,
762 0xe2e3,
763 0xe3e4,
764 0xe4e5,
765 0xe5e6,
766 0xe6e7,
767 0xe7e8,
768 0xe8e9,
769 0xe9ea,
770 0xeaf1,
771 0xf2f2,
772 0xf3f3,
773 0xf4f4,
774 0xf5f5,
775 0xf6f6,
776 0xf7f7,
777 0xf8f8,
778 0xf9f9,
779 0xfafa,
780 0x0000,
781 0x0000,
782 0x0000,
783 0x0000,
784 0x0000,
785 0x0000,
786 0x0000,
787 0x0000,
788 0x0000,
789 0x0000,
790 0x0000,
791 0x0000,
792 0x0000,
793 0x0000,
794 0x0000,
795 0x0000,
796 0x0000,
797 0x0000,
798 0x0000,
799 0x0000,
800 0x0000,
801 0x0000,
802 0x0000,
803 0x0000,
804 0x0000,
805 0x0000,
806 0x0000,
807 0x0000,
808 0x0000,
809 0x0000,
810 0x0000,
811 0x0000,
812 0x0000,
813 0x0000,
814 0x0000,
815 0x0000,
816 0x0000,
817 0x0000,
818 0x0000,
819 0x0000,
820 0x0000,
821 0x0000,
822 0x0000,
823 0x0000,
824 0x0000,
825 0x0000,
826 0x0000,
827 0x0000,
828 0x0000,
829 0x0000,
830 0x0000,
831 0x0000,
832 0x0000,
833 0x0000,
834 0x0000,
835 0x0000,
836 0x0000,
837 0x0000,
838 0x0000,
839 0x0000,
840 0x0000,
841 0x0000,
842 0x0000,
843 0x0000,
844 0x0000,
845 0x0000,
846 0x0000,
847 0x0000,
848 0x0000,
849 0x0000,
850 0x0000,
851 0x0000,
852 0x0000,
853 0x0000,
854 0x0000,
855 0x0000,
856 0x0000,
857 0x0000,
858 0x0000,
859 0x0000,
860 0x0000,
861 0x0000,
862 0x0000,
863 0x0000,
864 0x0000,
865 0x0000,
866 0x0000,
867 0x0000,
868 0x0000,
869 0x0000,
870 0x0000,
871 0x0000,
872 0x0000,
873 0x0000,
874 0x0000,
875 0x0101,
876 0x0202,
877 0x0303,
878 0x0404,
879 0x0505,
880 0x0606,
881 0x0707,
882 0x0808,
883 0x0909,
884 0x0a0a,
885 0x0b0b,
886 0x0000,
887 0x0000,
888 0x0000,
889 0x0000
890 };
891 #endif
892 #if 0
893 const MS_U16 g16IQ_TBL[128]=
894 {
895 0x0010,
896 0x000b,
897 0x000a,
898 0x0010,
899 0x0018,
900 0x0028,
901 0x0033,
902 0x003d,
903 0x000c,
904 0x000c,
905 0x000e,
906 0x0013,
907 0x001a,
908 0x003a,
909 0x003c,
910 0x0037,
911 0x000e,
912 0x000d,
913 0x0010,
914 0x0018,
915 0x0028,
916 0x0039,
917 0x0045,
918 0x0038,
919 0x000e,
920 0x0011,
921 0x0016,
922 0x001d,
923 0x0033,
924 0x0057,
925 0x0050,
926 0x003e,
927 0x0012,
928 0x0016,
929 0x0025,
930 0x0038,
931 0x0044,
932 0x006d,
933 0x0067,
934 0x004d,
935 0x0018,
936 0x0023,
937 0x0037,
938 0x0040,
939 0x0051,
940 0x0068,
941 0x0071,
942 0x005c,
943 0x0031,
944 0x0040,
945 0x004e,
946 0x0057,
947 0x0067,
948 0x0079,
949 0x0078,
950 0x0065,
951 0x0048,
952 0x005c,
953 0x005f,
954 0x0062,
955 0x0070,
956 0x0064,
957 0x0067,
958 0x0063,
959 0x0011,
960 0x0012,
961 0x0018,
962 0x002f,
963 0x0063,
964 0x0063,
965 0x0063,
966 0x0063,
967 0x0012,
968 0x0015,
969 0x001a,
970 0x0042,
971 0x0063,
972 0x0063,
973 0x0063,
974 0x0063,
975 0x0018,
976 0x001a,
977 0x0038,
978 0x0063,
979 0x0063,
980 0x0063,
981 0x0063,
982 0x0063,
983 0x002f,
984 0x0042,
985 0x0063,
986 0x0063,
987 0x0063,
988 0x0063,
989 0x0063,
990 0x0063,
991 0x0063,
992 0x0063,
993 0x0063,
994 0x0063,
995 0x0063,
996 0x0063,
997 0x0063,
998 0x0063,
999 0x0063,
1000 0x0063,
1001 0x0063,
1002 0x0063,
1003 0x0063,
1004 0x0063,
1005 0x0063,
1006 0x0063,
1007 0x0063,
1008 0x0063,
1009 0x0063,
1010 0x0063,
1011 0x0063,
1012 0x0063,
1013 0x0063,
1014 0x0063,
1015 0x0063,
1016 0x0063,
1017 0x0063,
1018 0x0063,
1019 0x0063,
1020 0x0063,
1021 0x0063,
1022 0x0063
1023 };
1024 #endif
1025 
1026 /* entry n is (-1 << n) + 1 */
1027 static const MS_S32 extend_offset[16] =
1028 {
1029     0, (( -1)<<1)+1, ((-1)<<2)+1, ((-1)<<3) + 1, ((-1)<<4)+1, ((-1)<<5)+1,
1030     ((-1)<<6)+1, ((-1)<<7)+1, ((-1)<<8)+1, ((-1)<<9)+1, ((-1) <<10)+1,
1031     ((-1)<<11)+1, ((-1)<<12)+1, ((-1)<<13)+1, ((-1)<<14)+1, ((-1)<<15)+1
1032 };
1033 
1034 /* entry n is 2**(n-1) */
1035 static const MS_S32 extend_test[16] =
1036 {
1037     0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
1038 };
1039 
1040 static const MS_S32 extend_mask[] =
1041 {
1042     0, (1<<0), (1<<1), (1<<2), (1<<3), (1<<4), (1<<5), (1<<6), (1<<7), (1<<8),
1043     (1<<9), (1<<10), (1<<11), (1<<12), (1<<13), (1<<14), (1<<15), (1<<16),
1044 };
1045 
1046 
1047 JPEG_STATIC MS_U32 _u32ReadBufferAddr;
1048 JPEG_STATIC MS_U32 _u32ReadBufferSize;
1049 JPEG_STATIC MS_U32 _u32WriteBufferAddr;
1050 JPEG_STATIC MS_U32 _u32WriteBufferSize;
1051 JPEG_STATIC MS_U32 _u32InternalBufferAddr;
1052 JPEG_STATIC MS_U32 _u32InternalBufferSize;
1053 
1054 JPEG_STATIC MS_U32 _u32RLEOffset;   // offset to record the current RLE access address
1055 
1056 JPEG_STATIC JPEG_HdrChk _HeadCheck;
1057 
1058 JPEG_STATIC JPEG_BLOCK_TYPE _s16dc_pred[3];
1059 
1060 // The width/height may be the thumbnail or original image size, it based on decoding mode
1061 JPEG_STATIC MS_U16 _u16Image_x_size;
1062 JPEG_STATIC MS_U16 _u16Image_y_size;
1063 
1064 // The original size of this JPEG file after alignment
1065 JPEG_STATIC MS_U16 _u16OriginalImage_x_size;
1066 JPEG_STATIC MS_U16 _u16OriginalImage_y_size;
1067 
1068 // The original size before alignment
1069 JPEG_STATIC MS_U16 _u16NonAlignmentImage_x_size;
1070 JPEG_STATIC MS_U16 _u16NonAlignmentImage_y_size;
1071 
1072 // The width/height/pitch of image for displaying.
1073 JPEG_STATIC MS_U16 _u16AlignedImageWidth;
1074 JPEG_STATIC MS_U16 _u16AlignedImagePitch;
1075 JPEG_STATIC MS_U16 _u16AlignedImageHeight;
1076 JPEG_STATIC MS_U16 _u16AlignedImagePitch_H;
1077 
1078 // Scale Down Factor
1079 JPEG_STATIC MS_U8 _u8ScaleDownFactor;
1080 
1081 ////JPEG_STATIC PJPEG_FILE_FileSystem_t _pStream;
1082 
1083 JPEG_STATIC MS_U8 _u8DecodeType = E_JPEG_TYPE_MAIN;
1084 JPEG_STATIC MS_BOOL _bProgressive_flag;
1085 
1086 #ifndef CMODEL
1087 JPEG_STATIC MS_U8 _u8DownScaleRatio;
1088 #endif
1089 
1090 JPEG_STATIC MS_BOOL _bFirstRLE;
1091 
1092 /******* Thumbnail related *******/
1093 JPEG_STATIC MS_BOOL _bThumbnailFound;
1094 JPEG_STATIC MS_BOOL _bThumbnailAccessMode;
1095 JPEG_STATIC MS_U32 _u32ThumbnailOffset;
1096 JPEG_STATIC MS_U16 _u16ThumbnailSize;
1097 JPEG_STATIC MS_BOOL _bTiffBigEndian;
1098 
1099 JPEG_STATIC MS_U32 _u32ThumbnailBufferOffset;   // keep track of thumb buffer access address
1100 JPEG_STATIC MS_U32 _u16ThumbnailBufferSize;   // keep track of thumb buffer size
1101 /*****************************/
1102 
1103 JPEG_STATIC JPEG_HuffInfo _Huff_info[JPEG_MAXHUFFTABLES];
1104 
1105 JPEG_STATIC JPEG_QuantTbl _QuantTables[JPEG_MAXQUANTTABLES]; /* pointer to quantization tables */
1106 
1107 JPEG_STATIC MS_U8 _u8Comps_in_frame;                             /* # of components in frame */
1108 JPEG_STATIC MS_U8 _u8Comp_h_samp[JPEG_MAXCOMPONENTS];     /* component's horizontal sampling factor */
1109 JPEG_STATIC MS_U8 _u8Comp_v_samp[JPEG_MAXCOMPONENTS];     /* component's vertical sampling factor */
1110 JPEG_STATIC MS_U8 _u8Comp_quant[JPEG_MAXCOMPONENTS];      /* component's quantization table selector */
1111 JPEG_STATIC MS_U8 _u8Comp_ident[JPEG_MAXCOMPONENTS];      /* component's ID */
1112 
1113 // The Luma and Chroma (YU) component ID, default is 1 & 2
1114 JPEG_STATIC MS_U8 _u8LumaCi = 1;
1115 JPEG_STATIC MS_U8 _u8ChromaCi = 2;
1116 JPEG_STATIC MS_U8 _u8Chroma2Ci = 3;
1117 
1118 JPEG_STATIC MS_U16 _u16Comp_h_blocks[JPEG_MAXCOMPONENTS];
1119 JPEG_STATIC MS_U16 _u16Comp_v_blocks[JPEG_MAXCOMPONENTS];
1120 
1121 JPEG_STATIC MS_U8 _u8Comps_in_scan;                  /* # of components in scan */
1122 JPEG_STATIC MS_U8 _u8Comp_list[JPEG_MAXCOMPSINSCAN];      /* components in this scan */
1123 JPEG_STATIC MS_U8 _u8Comp_dc_tab[JPEG_MAXCOMPONENTS];     /* component's DC Huffman coding table selector */
1124 JPEG_STATIC MS_U8 _u8Comp_ac_tab[JPEG_MAXCOMPONENTS];     /* component's AC Huffman coding table selector */
1125 
1126 JPEG_STATIC MS_U8 _u8Spectral_start;                 /* spectral selection start */
1127 JPEG_STATIC MS_U8 _u8Spectral_end;                   /* spectral selection end   */
1128 JPEG_STATIC MS_U8 _u8Successive_low;                 /* successive approximation low */
1129 JPEG_STATIC MS_U8 _u8Successive_high;                /* successive approximation high */
1130 
1131 JPEG_STATIC MS_U8 _u8Blocks_per_mcu;
1132 JPEG_STATIC MS_U32 _u32Max_blocks_per_row;
1133 JPEG_STATIC MS_U16 _u16Mcus_per_row;
1134 JPEG_STATIC MS_U16 _u16Mcus_per_col;
1135 
1136 JPEG_STATIC MS_U8 _u8Mcu_org[JPEG_MAXBLOCKSPERMCU];
1137 
1138 JPEG_STATIC MS_U8 gu8Max_mcu_x_size;          /* MCU's max. X size in pixels */
1139 JPEG_STATIC MS_U8 gu8Max_mcu_y_size;                 /* MCU's max. Y size in pixels */
1140 JPEG_STATIC MS_U16 gu16Max_mcus_per_row;
1141 
1142 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
1143 JPEG_STATIC MS_U16 _u16Total_lines_left;               /* total # lines left in image */
1144 JPEG_STATIC MS_U32 _u32Block_y_mcu[JPEG_MAXCOMPONENTS];
1145 JPEG_STATIC JPEG_HuffTbl _Huff_tbls[JPEG_MAXHUFFTABLES];
1146 JPEG_STATIC PJPEG_CoeffBuf _DC_Coeffs[JPEG_MAXCOMPONENTS];
1147 JPEG_STATIC PJPEG_CoeffBuf _AC_Coeffs[JPEG_MAXCOMPONENTS];
1148 JPEG_STATIC MS_U32 _u32Last_dc_val[JPEG_MAXCOMPONENTS];
1149 JPEG_STATIC MS_U32 _u32EOB_run;
1150 #ifdef CMODEL
1151 JPEG_STATIC MS_U16 gu16Mcu_lines_left;                 /* total # lines left in this MCU */
1152 #endif
1153 #endif
1154 
1155 JPEG_STATIC MS_U16 _u16Max_blocks_per_mcu;
1156 JPEG_STATIC MS_U16 _u16Max_mcus_per_col;
1157 
1158 JPEG_STATIC MS_U8 gu8Scan_type;                 /* Grey, Yh1v1, Yh1v2, Yh2v1, Yh2v2,
1159                                        CMYK111, CMYK4114 */
1160 JPEG_STATIC MS_U8 *_pu8In_buf_ofs;
1161 JPEG_STATIC MS_U32 _u32In_buf_left;
1162 JPEG_STATIC MS_U8 _u8Tem_flag;
1163 JPEG_STATIC MS_BOOL _bEOF_flag;
1164 
1165 JPEG_STATIC MS_U8 *_pu8In_buf;
1166 
1167 JPEG_STATIC MS_S16 _s16Bits_left;
1168 JPEG_STATIC MS_U32 _u32Bit_buf;
1169 
1170 JPEG_STATIC MS_U16 _u16Restart_interval;
1171 JPEG_STATIC MS_U16 _u16Restarts_left;
1172 JPEG_STATIC MS_U16 _u16Next_restart_num;
1173 
1174 JPEG_STATIC void *_pBlocks[JPEG_MAXBLOCKS];         /* list of all dynamically allocated blocks */
1175 
1176 JPEG_STATIC JPEG_ErrCode _Error_code;
1177 
1178 #ifdef CMODEL
1179 JPEG_STATIC MS_BOOL _bReady_flag;
1180 #endif
1181 
1182 //#ifdef USE_LIBC
1183 #ifdef JPD_LONGJUMP_SUPPORT
1184 JPEG_STATIC jmp_buf _jmp_state;
1185 #endif
1186 //#endif
1187 
1188 ////JPEG_STATIC MS_S32 _Total_Decoded_Size = 0;
1189 #if 0 // not implement for new MDDI - harold
1190 JPEG_STATIC MS_U32  NumPics;
1191 #endif
1192 JPEG_STATIC MS_U32 _u32Total_bytes_read;
1193 
1194 //JPEG_STATIC MS_U8 *_pu32ExifHeaderAddr;
1195 
1196 JPEG_STATIC JPEG_FillHdrFunc _pFillHdrFunc = NULL;
1197 
1198 #ifdef CMODEL
1199 extern const MS_U8 _u8ZAG[64];
1200 extern const MS_U8 _u8Jpeg_zigzag_order[64];
1201 #elif SW_JPD_RGB_CMYK
1202 static const MS_U8 _u8ZAG[64+1] =
1203 {
1204     0, 1, 8, 16, 9, 2, 3, 10,
1205     17, 24, 32, 25, 18, 11, 4, 5,
1206     12, 19, 26, 33, 40, 48, 41, 34,
1207     27, 20, 13, 6, 7, 14, 21, 28,
1208     35, 42, 49, 56, 57, 50, 43, 36,
1209     29, 22, 15, 23, 30, 37, 44, 51,
1210     58, 59, 52, 45, 38, 31, 39, 46,
1211     53, 60, 61, 54, 47, 55, 62, 63
1212     ,0
1213 };
1214 #endif
1215 
1216 JPEG_STATIC MSIF_Version _api_jpeg_version = {
1217     .DDI = { JPEG_API_VERSION },
1218 };
1219 
1220 JPEG_STATIC MS_U8 _u8JPEG_ApiDbgLevel = E_JPEG_DEBUG_NONE;
1221 JPEG_STATIC MS_BOOL bMHEG5 = FALSE;
1222 
1223 JPEG_STATIC MS_U16 _JPD_PreVIdx = 0; //For H/W bug, some cases can not exit after decode done
1224 JPEG_STATIC MS_BOOL _JPD_IsDecoding = FALSE; //For H/W bug, some cases can not exit after decode done
1225 JPEG_STATIC MS_U32 _JPD_ReCheckTime = 0; //For H/W bug, some cases can not exit after decode done
1226 
1227 JPEG_STATIC MS_BOOL _Progressive_ROI_flag = FALSE; //CL82399
1228 JPEG_STATIC MS_U16 ROI_width; //CL82399
1229 
1230 JPEG_STATIC JPEG_BuffLoadType u8PreLHFlag = E_JPEG_BUFFER_NONE;
1231 
1232 JPEG_STATIC MS_U32 u32MRCheckCount = 0;
1233 
1234 JPEG_STATIC MS_BOOL _bIsInit = FALSE;
1235 
1236 ///JPEG_STATIC MS_U32 jpeg_input = 0;
1237 ///#define JPEG_GO     do{scanf("%d", &jpeg_input);}while(0)
1238 
1239 #if SW_JPD_RGB_CMYK
1240 JPEG_BLOCK_TYPE *_ps16Block_seg[JPEG_MAXBLOCKSPERROW];
1241 MS_U8 _u8Block_max_zag_set[JPEG_MAXBLOCKSPERROW];
1242 MS_U8 *gpu8Sample_buf;
1243 MS_S32 gs32Crr[256];
1244 MS_S32 gs32Cbb[256];
1245 MS_S32 gs32Crg[256];
1246 MS_S32 gs32Cbg[256];
1247 
1248 MS_U8 *pgu8Scan_line_0;
1249 MS_U8 *pgu8scan_line_1;
1250 
1251 //JPEG_STATIC MS_U16 _u16Real_dest_bytes_per_scan_line;
1252 JPEG_STATIC MS_U16 _u16Dest_bytes_per_scan_line;        /* rounded up */
1253 JPEG_STATIC MS_U8 _u8Dest_bytes_per_pixel;            /* currently, 4 (RGB) or 1 (Y) */
1254 
1255 #if ( ! SUPPORT_PROGRESSIVE_MODE) && ( ! defined(CMODEL))
1256 JPEG_STATIC MS_U16 _u16Total_lines_left;               /* total # lines left in image */
1257 JPEG_STATIC MS_U32 _u32Block_y_mcu[JPEG_MAXCOMPONENTS];
1258 //JPEG_STATIC MS_U16 gu16Mcu_lines_left;                 /* total # lines left in this MCU */
1259 #endif
1260 #endif
1261 
1262 /*===========================================================================*/
1263 JPEG_STATIC MS_BOOL __bIsMjpeg = FALSE;
1264 JPEG_STATIC MS_U32 __u32RealMjpegBase = 0x00000000;
1265 JPEG_STATIC MS_U32 __u32TrickyMjpegBase = 0x00000000;
1266 /*===========================================================================*/
1267 
1268 //The info of JPEG EXIF
1269 JPEG_STATIC JPEG_EXIF_DateTime _stEXIF_DateTime;
1270 JPEG_STATIC JPEG_EXIF_Orientation _eEXIF_Orientation;
1271 
1272 #if SUPPORT_EXIF_EXTRA_INFO
1273 JPEG_STATIC MS_U8 _u8EXIF_Manufacturer[JPEG_MANUFACTURER_SIZE];
1274 JPEG_STATIC MS_U8 _u8EXIF_Model[JPEG_MODEL_SIZE];
1275 JPEG_STATIC MS_U16 _u16EXIF_Exposureprogram;
1276 JPEG_STATIC MS_U16 _u16EXIF_Flash;
1277 JPEG_STATIC MS_U32 _u32EXIF_ISOSpeedRatings;
1278 JPEG_STATIC JPEG_RATIONAL _stEXIF_ExposureTime;
1279 JPEG_STATIC JPEG_RATIONAL _stEXIF_FNumber;
1280 JPEG_STATIC JPEG_RATIONAL _stEXIF_ShutterSpeedValue;
1281 JPEG_STATIC JPEG_RATIONAL _stEXIF_ApertureValue;
1282 JPEG_STATIC JPEG_RATIONAL _stEXIF_ExposureBiasValue;
1283 JPEG_STATIC JPEG_RATIONAL _stEXIF_FocalLength;
1284 JPEG_STATIC MS_U32 _u32EXIF_ImageWidth;
1285 JPEG_STATIC MS_U32 _u32EXIF_ImageHeight;
1286 #endif
1287 
1288 //record data offset
1289 JPEG_STATIC MS_U32 u32DataOffset = 0;
1290 JPEG_STATIC MS_U32 u32SOFOffset = 0; //for SEC request, they need to know the offset of SOF marker
1291 
1292 JPEG_STATIC MS_BOOL bIs3HuffTbl = FALSE;
1293 
1294 #if SW_JPD_RGB_CMYK
1295 JPEG_STATIC MS_BOOL bEnableCMYK = TRUE;
1296 JPEG_STATIC MS_BOOL bEnableRGB = TRUE;
1297 #endif
1298 
1299 
1300 #if SUPPORT_MPO_FORMAT
1301 //MPO buffer
1302 static MS_U8 *_pu8In_buf_MPO_ofs;
1303 static MS_U32 _u32In_buf_MPO_left = 0xFFFFFFFFUL;
1304 
1305 static MS_BOOL mpo_load_data = TRUE;  //check whether load data when start decode or fill buffer.
1306 static MS_BOOL bIsMPOFormat = FALSE;
1307 static MS_U32 u32MPFOffset = 0;
1308 #endif
1309 
1310 #if PRINT_JPD_DECODE_TIME
1311 static MS_U32 u32MeasureDecodeTime;
1312 static MS_U32 u32MeasureDecodeTimeSW;
1313 static MS_U32 u32MeasureDecodeTimeHW;
1314 #endif
1315 
1316 static unsigned long  u32CRC;
1317 
verJPD_CRC32_Init(void)1318 static void verJPD_CRC32_Init(void)
1319 {
1320     u32CRC = 0xFFFFFFFF;
1321 }
1322 
1323 
verJPD_CRC32_GetResult(void)1324 static MS_U32 verJPD_CRC32_GetResult(void)
1325 {
1326     return (~u32CRC);
1327 }
1328 
1329 
verJPD_Init_CRC_Table(MS_U32 * u32CRCtemptable)1330 static void verJPD_Init_CRC_Table(MS_U32 *u32CRCtemptable)
1331 {
1332 
1333     int i,j,tmp, result;
1334 
1335     for(i=0;i<=0xFF;i++)
1336     {
1337         tmp=i;
1338         result=0;
1339 
1340         for(j=1;j<9;j++)
1341         {
1342              if(tmp & 1)
1343              {
1344                         result |= (1 << (8 - j));
1345              }
1346              tmp >>= 1;
1347         }
1348 
1349         u32CRCtemptable[i]=result<<24;
1350         for(j=0; j<8; j++)
1351         {
1352             u32CRCtemptable[i] = (u32CRCtemptable[i] << 1) ^ ((u32CRCtemptable[i] & (1 << 31)) ? 0x04C11DB7 : 0);
1353         }
1354 
1355         tmp=u32CRCtemptable[i];
1356         result=0;
1357 
1358         for(j=1;j<33;j++)
1359         {
1360              if(tmp & 1)
1361              {
1362                 result |= (1 << (32 - j));
1363              }
1364              tmp >>= 1;
1365         }
1366         u32CRCtemptable[i]=result;
1367         }
1368 }
1369 
verJPD_CRC32_Update(const MS_U8 * pu8Data,MS_U32 u32Size)1370 static void verJPD_CRC32_Update(const MS_U8 *pu8Data, MS_U32 u32Size)
1371 {
1372     MS_U32 u32CRCTable[256];
1373     MS_U32  u32LoopCounter;
1374 
1375     if (pu8Data == NULL || u32Size == 0) return;
1376 
1377     verJPD_Init_CRC_Table(u32CRCTable);
1378 
1379     #if 0
1380         int i;
1381         for(i = 0;i<256;i++)
1382         {
1383             printf("0x%08x,\n", u32CRCTable[i]);
1384         }
1385     #endif
1386 
1387     for (u32LoopCounter=0; u32LoopCounter<u32Size; u32LoopCounter++)
1388     {
1389         u32CRC = (u32CRC >> 8) ^ u32CRCTable[ pu8Data[u32LoopCounter] ^ (unsigned char)(u32CRC & 0xFF) ];
1390     }
1391 }
1392 
1393 //-------------------------------------------------------------------------------------------------
1394 // Local Function Prototypes
1395 //-------------------------------------------------------------------------------------------------
1396 #if 0 // not implement for new MDDI - harold
1397 JPEG_STATIC void MSAPI_MJPEG_Get_Pics(void);
1398 #endif // not implement for new MDDI - harold
1399 
1400 JPEG_STATIC void JPEG_terminate(JPEG_ErrCode status);
1401 
1402 JPEG_STATIC MS_BOOL JPEG_decode_init(void);
1403 
1404 #ifndef CMODEL
1405 JPEG_STATIC MS_BOOL JPEG_StartDecode(void);
1406 #endif
1407 
1408 JPEG_STATIC void JPEG_GetAlignedResolution(MS_U16 *width, MS_U16 *height);
1409 
1410 #if SW_JPD_RGB_CMYK
1411 JPEG_STATIC MS_BOOL msAPI_JPEG_transform_row( void );
1412 JPEG_STATIC JPEG_Result msAPI_JPEG_Baseline_Decode(void);
1413 #endif //SW_JPD_RGB_CMYK
1414 
1415 JPEG_STATIC MS_U32 JPEG_GetECS(void);
1416 
1417 //-------------------------------------------------------------------------------------------------
1418 //  Debug Functions
1419 //-------------------------------------------------------------------------------------------------
1420 
1421 //-------------------------------------------------------------------------------------------------
1422 //  Local Functions
1423 //-------------------------------------------------------------------------------------------------
1424 
JPEG_OVER_BUFFER(MS_U32 addr,MS_U32 buf_addr,MS_U32 buf_size)1425 JPEG_STATIC MS_BOOL JPEG_OVER_BUFFER(MS_U32 addr, MS_U32 buf_addr, MS_U32 buf_size)
1426 {
1427     if((addr < buf_addr)
1428     || (addr >= (buf_addr+ buf_size)))
1429     {
1430         JPEG_DEBUG_API_ERR("%s [%d] invalid address 0x%lx\n", __FUNCTION__, __LINE__, addr);
1431         return TRUE;
1432     }
1433     return FALSE;
1434 }
1435 
1436 //------------------------------------------------------------------------------
1437 // Refill the input buffer.
1438 // This method will sit in a loop until (A) the buffer is full or (B)
1439 // the stream's read() method reports an end of file condition.
JPEG_fill_read_buffer(void)1440 JPEG_STATIC MS_BOOL JPEG_fill_read_buffer(void)
1441 {
1442     JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer:\n");
1443     _u32In_buf_left = 0;
1444     _pu8In_buf_ofs = _pu8In_buf;
1445 
1446     if(_bEOF_flag
1447     && (FALSE == _bThumbnailAccessMode))
1448     {
1449         JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer : EOF!!!\n");
1450         return TRUE;
1451     }
1452 
1453     if(_bThumbnailAccessMode)
1454     {
1455         JPEG_DEBUG_API_MSG("Fill Thumbnail, buffer left = %ld, buffer offset = %ld\n", _u16ThumbnailBufferSize, _u32ThumbnailBufferOffset);
1456         if(_u16ThumbnailBufferSize>=MRC_BUFFER_SIZE)
1457         {
1458             JPEG_memcpy((void *)(_pu8In_buf + _u32In_buf_left), (void *)(INTERNAL_BUFFER_ADDR + _u32ThumbnailBufferOffset), MRC_BUFFER_SIZE);
1459             _u32In_buf_left = MRC_BUFFER_SIZE;
1460             _u32ThumbnailBufferOffset += MRC_BUFFER_SIZE;
1461             _u16ThumbnailBufferSize -= MRC_BUFFER_SIZE;
1462         }
1463         else
1464         {
1465             JPEG_memcpy((void *)(_pu8In_buf + _u32In_buf_left), (void *)(INTERNAL_BUFFER_ADDR + _u32ThumbnailBufferOffset), _u16ThumbnailBufferSize);
1466             _bEOF_flag = TRUE;
1467             _u32In_buf_left = _u16ThumbnailBufferSize;
1468             _u32ThumbnailBufferOffset += _u16ThumbnailBufferSize;
1469             _u16ThumbnailBufferSize = 0;
1470         }
1471     }
1472     else
1473     {
1474         if(_pFillHdrFunc)
1475         {
1476             MS_S32 byte_read = _pFillHdrFunc((MS_PHYADDR)MS_VA2PA((MS_U32)_pu8In_buf), MRC_BUFFER_SIZE);
1477             if(byte_read < 0)
1478             {
1479                 JPEG_terminate(E_JPEG_STREAM_READ);
1480                 return FALSE;
1481             }
1482             _u32In_buf_left = byte_read;
1483         }
1484         else
1485         {
1486             JPEG_DEBUG_API_MSG("_pFillHdrFunc is not registered!!\n");
1487             if (bMHEG5)
1488             {
1489                 _u32In_buf_left = MRC_BUFFER_SIZE;
1490             }
1491             else
1492             {
1493                 JPEG_terminate(E_JPEG_NOT_ENOUGH_HEADER_INFO);
1494                 return FALSE;
1495             }
1496         }
1497     }
1498 #if 0
1499     else
1500     {
1501         do
1502         {
1503             MS_S32 bytes_read = _pStream->read( _pu8In_buf + _u32In_buf_left, MRC_BUFFER_SIZE - _u32In_buf_left, &_bEOF_flag, _pStream);//, 0 );
1504             if ( bytes_read == -1 )
1505             {
1506                 JPEG_terminate( E_JPEG_STREAM_READ );
1507             }
1508             _u32In_buf_left += bytes_read;
1509 ////            _Total_Decoded_Size += bytes_read;
1510         } while ( ( _u32In_buf_left < MRC_BUFFER_SIZE ) && ( !_bEOF_flag ) );
1511     }
1512 
1513     _u32Total_bytes_read += _u32In_buf_left;
1514 #endif
1515 
1516     u32DataOffset += MRC_BUFFER_SIZE;
1517     return TRUE;
1518 }
JPEG_force_fill_read_buffer(MS_U32 u32Size)1519 JPEG_STATIC MS_BOOL JPEG_force_fill_read_buffer(MS_U32 u32Size)
1520 {
1521     JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer:\n");
1522     _u32In_buf_left = u32Size;
1523     _pu8In_buf_ofs = _pu8In_buf;
1524 
1525     if(_bEOF_flag)
1526     {
1527         JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer : EOF!!!\n");
1528         return TRUE;
1529     }
1530 
1531     if(_pFillHdrFunc)
1532     {
1533         MS_S32 byte_read = 0;
1534         if((MRC_BUFFER_SIZE-u32Size) > 0)
1535         {
1536             JPEG_DEBUG_API_MSG("[VA] _pu8In_buf=0x%lx\n", (MS_U32)_pu8In_buf);
1537             JPEG_DEBUG_API_MSG("[PA] _pu8In_buf=0x%lx\n", MS_VA2PA((MS_U32)_pu8In_buf));
1538             JPEG_DEBUG_API_MSG("[PA] callback addr=0x%lx\n", (MS_VA2PA((MS_U32)_pu8In_buf)+u32Size));
1539             byte_read  = _pFillHdrFunc((MS_PHYADDR)(MS_VA2PA((MS_U32)_pu8In_buf)+u32Size), MRC_BUFFER_SIZE-u32Size);
1540             JPEG_DEBUG_API_MSG("@@@fill byte_read=0x%lx\n", byte_read);
1541         }
1542 
1543         if(byte_read < 0)
1544         {
1545             JPEG_terminate(E_JPEG_STREAM_READ);
1546             return FALSE;
1547         }
1548         _u32In_buf_left += byte_read;
1549     }
1550     else
1551     {
1552         JPEG_DEBUG_API_MSG("_pFillHdrFunc is not registered!!\n");
1553         return FALSE;
1554     }
1555 
1556     return TRUE;
1557 }
1558 //------------------------------------------------------------------------------
1559 // Logical rotate left operation.
JPEG_rol(MS_U32 i,MS_U8 j)1560 JPEG_STATIC MS_U32 JPEG_rol(MS_U32 i, MS_U8 j)
1561 {
1562     return ( ( i << j ) | ( i >> ( 32 - j ) ) );
1563 }
1564 //------------------------------------------------------------------------------
1565 // Retrieve one character from the input stream.
JPEG_get_char(void)1566 JPEG_STATIC MS_U8 JPEG_get_char(void)
1567 {
1568     MS_U8 c;
1569 
1570     // Any bytes remaining in buffer?
1571     if ( !_u32In_buf_left )
1572     {
1573         // Try to get more bytes.
1574         if(!JPEG_fill_read_buffer())
1575         {
1576             JPEG_DEBUG_API_MSG("JPEG_get_char : JPEG_fill_read_buffer failed!!!\n");
1577             _u32In_buf_left = 0;
1578         }
1579         // Still nothing to get?
1580         if ( !_u32In_buf_left )
1581         {
1582             // Padd the end of the stream with 0xFF 0xD9 (EOI marker)
1583             // FIXME: Is there a better padding pattern to use?
1584             MS_U8 t = _u8Tem_flag;
1585             _u8Tem_flag ^= 1;
1586             if ( t )
1587             {
1588                 return ( 0xD9 );
1589             }
1590             else
1591             {
1592                 return ( 0xFF );
1593             }
1594         }
1595     }
1596 
1597     c = *_pu8In_buf_ofs++;
1598     _u32In_buf_left--;
1599 
1600     return ( c );
1601 }
1602 //------------------------------------------------------------------------------
1603 // Same as previus method, except can indicate if the character is
1604 // a "padd" character or not.
JPEG_get_charP(MS_BOOL * Ppadding_flag)1605 JPEG_STATIC MS_U8 JPEG_get_charP(MS_BOOL *Ppadding_flag)
1606 {
1607     MS_U8 c;
1608 
1609     if ( !_u32In_buf_left )
1610     {
1611         if(!JPEG_fill_read_buffer())
1612         {
1613             JPEG_DEBUG_API_MSG("JPEG_get_charP : JPEG_fill_read_buffer failed!!!\n");
1614             _u32In_buf_left = 0;
1615         }
1616 
1617         if ( !_u32In_buf_left )
1618         {
1619             *Ppadding_flag = TRUE;
1620 #if 1
1621             _u8Tem_flag ^= 1;
1622             // This function is called during when progressive huffman decoding
1623             // Should not padding 0xFFD9 at this state
1624             return 0;
1625 #else
1626             MS_U8 t;
1627             t = _u8Tem_flag;
1628             _u8Tem_flag ^= 1;
1629             if ( t )
1630             {
1631                 return ( 0xD9 );
1632             }
1633             else
1634             {
1635                 return ( 0xFF );
1636             }
1637 #endif
1638         }
1639     }
1640 
1641     *Ppadding_flag = FALSE;
1642 
1643     c = *_pu8In_buf_ofs++;
1644     _u32In_buf_left--;
1645 
1646     return ( c );
1647 }
1648 //------------------------------------------------------------------------------
1649 // Inserts a previously retrieved character back into the input buffer.
JPEG_stuff_char(MS_U8 q)1650 JPEG_STATIC void JPEG_stuff_char(MS_U8 q)
1651 {
1652     *( --_pu8In_buf_ofs ) = q;
1653     _u32In_buf_left++;
1654 }
1655 //------------------------------------------------------------------------------
1656 // Retrieves one character from the input stream, but does
1657 // not read past markers. Will continue to return 0xFF when a
1658 // marker is encountered.
1659 // FIXME: Bad name?
JPEG_get_octet(void)1660 JPEG_STATIC MS_U8 JPEG_get_octet(void)
1661 {
1662     MS_BOOL padding_flag;
1663     MS_U8 c = JPEG_get_charP( &padding_flag );
1664 
1665     if ( c == 0xFF )
1666     {
1667         if ( padding_flag )
1668         {
1669             return ( 0xFF );
1670         }
1671 
1672         c = JPEG_get_charP( &padding_flag );
1673         if ( padding_flag )
1674         {
1675             JPEG_stuff_char( 0xFF );
1676             return ( 0xFF );
1677         }
1678 
1679         if ( c == 0x00 )
1680         {
1681             return ( 0xFF );
1682         }
1683         else
1684         {
1685             JPEG_stuff_char( c );
1686             JPEG_stuff_char( 0xFF );
1687             return ( 0xFF );
1688         }
1689     }
1690 
1691     return ( c );
1692 }
1693 //------------------------------------------------------------------------------
1694 // Retrieves a variable number of bits from the input stream.
1695 // Markers will not be read into the input bit buffer. Instead,
1696 // an infinite number of all 1's will be returned when a marker
1697 // is encountered.
1698 // FIXME: Is it better to return all 0's instead, like the older implementation?
JPEG_get_bits_2(MS_U8 numbits)1699 JPEG_STATIC MS_U32 JPEG_get_bits_2(MS_U8 numbits)
1700 {
1701     MS_U32 i, c1, c2;
1702 
1703     i = ( _u32Bit_buf >> ( 16 - numbits ) ) & ( ( 1 << numbits ) - 1 );
1704 
1705     _s16Bits_left -= numbits;
1706     if ( _s16Bits_left <= 0 )
1707     {
1708         _u32Bit_buf = JPEG_rol( _u32Bit_buf, numbits += _s16Bits_left );
1709 
1710         c1 = JPEG_get_octet();
1711         c2 = JPEG_get_octet();
1712 
1713         _u32Bit_buf = ( _u32Bit_buf & 0xFFFF ) | ( ( ( MS_U32 )c1 ) << 24 ) | ( ( ( MS_U32 )c2 ) << 16 );
1714 
1715         _u32Bit_buf = JPEG_rol( _u32Bit_buf, -_s16Bits_left );
1716 
1717         _s16Bits_left += 16;
1718     }
1719     else
1720     {
1721         _u32Bit_buf = JPEG_rol( _u32Bit_buf, numbits );
1722     }
1723 
1724     return i;
1725 }
1726 //------------------------------------------------------------------------------
1727 // Decodes a Huffman encoded symbol.
JPEG_huff_decode(JPEG_HuffTbl * Ph)1728 JPEG_STATIC MS_S32 JPEG_huff_decode(JPEG_HuffTbl *Ph)
1729 {
1730     MS_S32 symbol;
1731 
1732     // Check first 8-bits: do we have a complete symbol?
1733     symbol = Ph->s16Look_up[( _u32Bit_buf >> 8 ) & 0xFF];
1734     if ( symbol < 0 )
1735     {
1736         // Decode more bits, use a tree traversal to find symbol.
1737         JPEG_get_bits_2( 8 );
1738 
1739         do
1740         {
1741             symbol = Ph->s16Tree[~symbol + ( 1 - JPEG_get_bits_2( 1 ) )];
1742         }
1743         while ( symbol < 0 );
1744     }
1745     else
1746     {
1747         JPEG_get_bits_2( Ph->u8Code_size[symbol] );
1748     }
1749 
1750     return symbol;
1751 }
1752 //------------------------------------------------------------------------------
1753 // Unconditionally frees all allocated blocks.
JPEG_free_all_blocks(void)1754 JPEG_STATIC void JPEG_free_all_blocks(void)
1755 {
1756     MS_U8 i;
1757 #if 0
1758     if ( _pStream )
1759     {
1760         _pStream = NULL;
1761     }
1762 #endif
1763     for ( i = 0; i < JPEG_MAXBLOCKS; i++ )
1764     {
1765         if(_pBlocks[i]!=NULL)
1766             JPEG_MEMORY_free( _pBlocks[i] );
1767 
1768         _pBlocks[i] = NULL;
1769     }
1770 }
1771 //------------------------------------------------------------------------------
1772 // This method handles all errors.
1773 // It could easily be changed to use C++ exceptions.
JPEG_terminate(JPEG_ErrCode status)1774 JPEG_STATIC void JPEG_terminate(JPEG_ErrCode status)
1775 {
1776     _Error_code = status;
1777 
1778     JPEG_free_all_blocks();
1779 
1780     JPEG_DEBUG_API_ERR("%s:ERROR= %d\n", __FUNCTION__, _Error_code);
1781 #ifdef JPD_LONGJUMP_SUPPORT
1782     longjmp( _jmp_state, status );
1783 #endif
1784 }
1785 //------------------------------------------------------------------------------
1786 // Allocate a block of memory-- store block's address in list for
1787 // later deallocation by JPEG_free_all_blocks().
JPEG_alloc(MS_U32 n)1788 JPEG_STATIC void * JPEG_alloc(MS_U32 n)
1789 {
1790     MS_U8 i;
1791     void *q;
1792 
1793     // Find a free slot. The number of allocated slots will
1794     // always be very low, so a linear search is good enough.
1795     for ( i = 0; i < JPEG_MAXBLOCKS; i++ )
1796     {
1797         if ( _pBlocks[i] == NULL )
1798         {
1799             break;
1800         }
1801     }
1802 
1803     if ( i >= JPEG_MAXBLOCKS )
1804     {
1805         JPEG_terminate( E_JPEG_TOO_MANY_BLOCKS );
1806         return NULL;
1807     }
1808 
1809     //FixMe: eCos does not support aligned allocate ???
1810     q = JPEG_MEMORY_malloc( n + 8 );
1811     //q = MsOS_AllocateAlignedMemory(n+8, 8, gs32CachedPoolID);
1812     //q = MsOS_AllocateMemory(n+8, gs32CachedPoolID);
1813 
1814     if ( q == NULL )
1815     {
1816         JPEG_terminate( E_JPEG_NOTENOUGHMEM );
1817         return NULL;
1818     }
1819 
1820     JPEG_memset((void *)q, 0, n + 8 );
1821 
1822     _pBlocks[i] = q;
1823 
1824     JPEG_DEBUG_API_MSG("JPEG %ld bytes allocated\n", n);
1825 
1826     return ( ( void* )q );
1827 }
1828 //------------------------------------------------------------------------------
JPEG_Tiff_EndianChangeL(MS_U32 u32Val)1829 JPEG_STATIC MS_U32 JPEG_Tiff_EndianChangeL(MS_U32 u32Val)
1830 {
1831     if (_bTiffBigEndian)
1832         return EndianChangeL(u32Val);
1833     else
1834         return u32Val;
1835 }
1836 //------------------------------------------------------------------------------
JPEG_Tiff_EndianChangeS(MS_U16 u16Val)1837 JPEG_STATIC MS_U16 JPEG_Tiff_EndianChangeS(MS_U16 u16Val)
1838 {
1839     if (_bTiffBigEndian)
1840         return EndianChangeS(u16Val);
1841     else
1842         return u16Val;
1843 }
1844 //------------------------------------------------------------------------------
JPEG_GetU16(MS_U8 * data)1845 JPEG_STATIC MS_U16 JPEG_GetU16(MS_U8 *data)
1846 {
1847     MS_S8 i;
1848     MS_U16 val = 0;
1849 
1850     for(i = 1; i>=0; i--)
1851         val = (val << 8) + (MS_U8) *(data + i);
1852 
1853 
1854     return val;
1855 }
1856 //------------------------------------------------------------------------------
JPEG_GetU32(MS_U8 * data)1857 JPEG_STATIC MS_U32 JPEG_GetU32(MS_U8 *data)
1858 {
1859     MS_S8 i;
1860     MS_U32 val = 0;
1861 
1862     for(i = 3; i>=0; i--)
1863         val = (val << 8) + (MS_U8) *(data + i);
1864 
1865     return val;
1866 }
1867 //------------------------------------------------------------------------------
1868 //***************************************************
1869 //Parse EXIF header
1870 //***************************************************
JPEG_DecodeExifInfo(MS_U8 * data,MS_U32 data_length)1871 JPEG_STATIC MS_BOOL JPEG_DecodeExifInfo(MS_U8 *data, MS_U32 data_length)
1872 {
1873     MS_U8 *pJpegBuff = data;
1874 //    MS_U32 u32tmp, u32Len;
1875     MS_U32 u32tmp;
1876     MS_U16 u16Marker, u16Len, u16NumOfEntry, i;
1877     MS_U8  *pTiffHdrPtr, *pNextIfd;
1878     MS_U32 u32DataValue;
1879     MS_U8 *pExifHdr = NULL, *pExifHdrOffet = NULL;
1880     MS_BOOL bIFD_pt_found = FALSE;
1881 
1882     u32tmp = JPEG_GetU32(pJpegBuff);
1883     if (JPEG_TAG_EXIF != u32tmp)
1884     {
1885         return FALSE;
1886     }
1887 
1888     // Exif starts here
1889     pJpegBuff += 6;
1890     pExifHdr = pJpegBuff;
1891     u16Marker = JPEG_GetU16(pJpegBuff);
1892     if (JPEG_TIFF_BIG_ENDIAN == u16Marker)
1893         _bTiffBigEndian = TRUE;
1894     else if (JPEG_TIFF_LITTLE_ENDIAN == u16Marker)
1895         _bTiffBigEndian = FALSE;
1896     else
1897         return FALSE;
1898 
1899     _u32ThumbnailOffset += 6;
1900     pTiffHdrPtr = pJpegBuff;
1901 
1902     pJpegBuff += 2;
1903     u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
1904     if (u16Marker != 0x002A)
1905         return FALSE;
1906 
1907     pJpegBuff += 2;
1908     u16Len = (MS_U16)JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
1909     pJpegBuff += (u16Len - 4); //ENDIAN(2byte) + 0x002A(2byte)
1910 
1911     // 0th IFD start here
1912     u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
1913     pJpegBuff += 2;
1914 
1915     // Boundary check, prevent from buffer over-run
1916     if((((MS_U32) pJpegBuff) - INTERNAL_BUFFER_ADDR + u16NumOfEntry*12)>=data_length)
1917     {
1918         return FALSE;
1919     }
1920 
1921 //    pJpegBuff += 12*u16NumOfEntry;
1922 
1923     JPEG_DEBUG_API_MSG("IFD0 : u16NumOfEntry = %d\n", u16NumOfEntry);
1924 
1925     //Retrieve orientation
1926     for(i = 0; i < u16NumOfEntry; i++)
1927     {
1928         u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
1929         pJpegBuff += 8;
1930 
1931         if((JPEG_EXIF_TAG_ORIENTATION == u16Marker)
1932         && (E_JPEG_EXIF_ORIENT_NOT_FOUND == _eEXIF_Orientation))
1933         {
1934             //The datatype of orientation is short
1935             u32DataValue = (MS_U32)JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
1936             if((E_JPEG_EXIF_ORIENT_ROTATE_0 > u32DataValue)
1937             || (E_JPEG_EXIF_ORIENT_ROTATE_270 < u32DataValue))
1938             {
1939                 _eEXIF_Orientation = E_JPEG_EXIF_ORIENT_RESERVED;
1940             }
1941             else
1942             {
1943                 _eEXIF_Orientation = (JPEG_EXIF_Orientation)u32DataValue;
1944             }
1945             JPEG_DEBUG_API_MSG("JPEG EXIF Orientation = %d\n", _eEXIF_Orientation);
1946         }
1947         else if(JPEG_EXIF_TAG_IFD_POINTER == u16Marker)
1948         {
1949             bIFD_pt_found = TRUE;
1950             //datavalue is an offset from exif marker for datetime
1951             u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
1952             pExifHdrOffet = pExifHdr + u32DataValue;
1953             JPEG_OVER_BUFFER_RET(pExifHdrOffet, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
1954             JPEG_DEBUG_API_MSG("Exif IFD pointer = 0x%lx\n", (MS_U32)pExifHdrOffet);
1955         }
1956 #if SUPPORT_EXIF_EXTRA_INFO
1957         else if(JPEG_EXIF_TAG_MANUFACTURER == u16Marker)
1958         {
1959             MS_U8 *pIFDPtrOffset;
1960             pIFDPtrOffset = pJpegBuff;
1961             u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pIFDPtrOffset));
1962             JPEG_DEBUG_API_MSG("MANUFACTURER Offset = 0x%lx\n", u32DataValue);
1963             pIFDPtrOffset = pExifHdr + u32DataValue;
1964             if(JPEG_OVER_BUFFER((MS_U32)pIFDPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE))
1965             {
1966                 pJpegBuff += 4;
1967                 continue;
1968             }
1969             //JPEG_DEBUG_API_MSG("MANUFACTURER addr = 0x%lx\n", (MS_U32)pIFDPtrOffset);
1970             MS_U8 u8i=0;
1971             while(*(pIFDPtrOffset+u8i) && u8i<JPEG_MANUFACTURER_SIZE)
1972             {
1973                 _u8EXIF_Manufacturer[u8i] = *(pIFDPtrOffset+u8i);
1974                 u8i++;
1975             }
1976         }
1977         else if(JPEG_EXIF_TAG_MODEL== u16Marker)
1978         {
1979             MS_U8 *pIFDPtrOffset;
1980             pIFDPtrOffset = pJpegBuff;
1981             u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pIFDPtrOffset));
1982             JPEG_DEBUG_API_MSG("MODEL Offset = 0x%lx\n", u32DataValue);
1983             pIFDPtrOffset = pExifHdr + u32DataValue;
1984             if(JPEG_OVER_BUFFER((MS_U32)pIFDPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE))
1985             {
1986                 pJpegBuff += 4;
1987                 continue;
1988             }
1989             //JPEG_DEBUG_API_MSG("MODEL addr = 0x%lx\n", (MS_U32)pIFDPtrOffset);
1990             MS_U8 u8i=0;
1991             while(*(pIFDPtrOffset+u8i) && u8i<JPEG_MODEL_SIZE)
1992             {
1993                 _u8EXIF_Model[u8i] = *(pIFDPtrOffset+u8i);
1994                 u8i++;
1995             }
1996         }
1997 #endif
1998 #if 0
1999         else if((JPEG_EXIF_TAG_DATETIME_MOD == u16Marker)
2000         && (FALSE == _stEXIF_DateTime.bHasDataTime))
2001         {
2002             _stEXIF_DateTime.bHasDataTime = TRUE;
2003             //datavalue is an offset from exif marker for datetime
2004             u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2005             pExifOffset = pExifHdr + u32DataValue;
2006             printf("pExifOffset1 = 0x%lx\n", (MS_U32)pExifOffset);
2007             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Year);
2008             pExifOffset++;
2009             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Month);
2010             pExifOffset++;
2011             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Day);
2012             pExifOffset++;
2013             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Hour);
2014             pExifOffset++;
2015             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Minute);
2016             pExifOffset++;
2017             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Second);
2018             JPEG_DEBUG_API_MSG("JPEG EXIF Orientation = %d\n", _eEXIF_Orientation);
2019             JPEG_DEBUG_API_MSG("=======JPEG EXIF DateTime======= Exist = %d, Original = %d \n", _stEXIF_DateTime.bHasDataTime, bOriDateTimeFound);
2020             JPEG_DEBUG_API_MSG("Year = %ld, Month = %ld, Day = %ld\n", _stEXIF_DateTime.u32Year, _stEXIF_DateTime.u32Month, _stEXIF_DateTime.u32Day);
2021             JPEG_DEBUG_API_MSG("Hour = %ld, Minute = %ld, Second = %ld\n", _stEXIF_DateTime.u32Hour, _stEXIF_DateTime.u32Minute, _stEXIF_DateTime.u32Second);
2022         }
2023 #endif
2024         pJpegBuff += 4;
2025     }
2026 
2027     if(TRUE == bIFD_pt_found)
2028     {
2029         u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifHdrOffet));
2030         pExifHdrOffet += 2;
2031         JPEG_DEBUG_API_MSG("EXIF IFD : u16NumOfEntry = %d\n", u16NumOfEntry);
2032 
2033         for(i = 0; i < u16NumOfEntry; i++)
2034         {
2035             u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifHdrOffet));
2036 #if (SUPPORT_EXIF_EXTRA_INFO==false)
2037             if(JPEG_EXIF_TAG_DATETIME_ORI == u16Marker)
2038             {
2039                 pExifHdrOffet += 8;
2040                 _stEXIF_DateTime.bHasDataTime = TRUE;
2041                 //datavalue is an offset from exif marker for datetime
2042                 u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifHdrOffet));;
2043                 pExifHdrOffet = pExifHdr + u32DataValue;
2044                 JPEG_OVER_BUFFER_RET(pExifHdrOffet, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2045                 JPEG_DEBUG_API_MSG("pExifOriDateTime Offset = 0x%lx\n", (MS_U32)pExifHdrOffet);
2046                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Year);
2047                 pExifHdrOffet++;
2048                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Month);
2049                 pExifHdrOffet++;
2050                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Day);
2051                 pExifHdrOffet++;
2052                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Hour);
2053                 pExifHdrOffet++;
2054                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Minute);
2055                 pExifHdrOffet++;
2056                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Second);
2057                 JPEG_DEBUG_API_MSG("=======JPEG EXIF DateTime=======\n");
2058                 JPEG_DEBUG_API_MSG("Year = %ld, Month = %ld, Day = %ld\n", _stEXIF_DateTime.u32Year, _stEXIF_DateTime.u32Month, _stEXIF_DateTime.u32Day);
2059                 JPEG_DEBUG_API_MSG("Hour = %ld, Minute = %ld, Second = %ld\n", _stEXIF_DateTime.u32Hour, _stEXIF_DateTime.u32Minute, _stEXIF_DateTime.u32Second);
2060                 break;
2061             }
2062 #else
2063             MS_U8 *pExifPtrOffset;
2064             MS_U16 u16Type;
2065             MS_U32 u32Count;
2066             switch(u16Marker)
2067             {
2068                 case JPEG_EXIF_TAG_DATETIME_ORI:
2069                     pExifPtrOffset = pExifHdrOffet + 8;
2070                     _stEXIF_DateTime.bHasDataTime = TRUE;
2071                     //datavalue is an offset from exif marker for datetime
2072                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2073                     pExifPtrOffset = pExifHdr + u32DataValue;
2074                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2075                     JPEG_DEBUG_API_MSG("pExifOriDateTime Offset = 0x%lx\n", (MS_U32)pExifPtrOffset);
2076                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Year);
2077                     pExifPtrOffset++;
2078                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Month);
2079                     pExifPtrOffset++;
2080                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Day);
2081                     pExifPtrOffset++;
2082                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Hour);
2083                     pExifPtrOffset++;
2084                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Minute);
2085                     pExifPtrOffset++;
2086                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Second);
2087                     JPEG_DEBUG_API_MSG("=======JPEG EXIF DateTime=======\n");
2088                     JPEG_DEBUG_API_MSG("Year = %ld, Month = %ld, Day = %ld\n", _stEXIF_DateTime.u32Year, _stEXIF_DateTime.u32Month, _stEXIF_DateTime.u32Day);
2089                     JPEG_DEBUG_API_MSG("Hour = %ld, Minute = %ld, Second = %ld\n", _stEXIF_DateTime.u32Hour, _stEXIF_DateTime.u32Minute, _stEXIF_DateTime.u32Second);
2090                     break;
2091                 case JPEG_EXIF_TAG_ISO_SPEED_RATING:
2092                     pExifPtrOffset = pExifHdrOffet + 2;
2093                     u16Type = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2094                     pExifPtrOffset = pExifHdrOffet + 4;
2095                     u32Count = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2096                     pExifPtrOffset = pExifHdrOffet + 8;
2097                     if(u16Type==3)
2098                     {
2099                         if(u32Count==2)
2100                         {
2101                             _u32EXIF_ISOSpeedRatings=JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2102                         }
2103                         else if(u32Count==1)
2104                         {
2105                             _u32EXIF_ISOSpeedRatings=JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2106                         }
2107                     }
2108                     else if(u16Type==4)
2109                     {
2110                         if(u32Count==1)
2111                         {
2112                             _u32EXIF_ISOSpeedRatings=JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2113                         }
2114                     }
2115                     JPEG_DEBUG_API_MSG("ISO Speed Rating=%ld\n", _u32EXIF_ISOSpeedRatings);
2116                     break;
2117                 case JPEG_EXIF_TAG_EXPOSURE_TIME:
2118                     pExifPtrOffset = pExifHdrOffet + 8;
2119                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2120                     JPEG_DEBUG_API_MSG("ExposureTime Offset = 0x%lx\n", u32DataValue);
2121                     pExifPtrOffset = pExifHdr + u32DataValue;
2122                     //JPEG_DEBUG_API_MSG("ExposureTime addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2123                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2124                     _stEXIF_ExposureTime.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2125                     pExifPtrOffset+=4;
2126                     _stEXIF_ExposureTime.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2127                     JPEG_DEBUG_API_MSG("ExposureTime numerator=%ld, denominator=%ld\n", _stEXIF_ExposureTime.numerator, _stEXIF_ExposureTime.denominator);
2128                     break;
2129                 case JPEG_EXIF_TAG_F_NUMBER:
2130                     pExifPtrOffset = pExifHdrOffet + 8;
2131                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2132                     JPEG_DEBUG_API_MSG("FNumber Offset = 0x%lx\n", u32DataValue);
2133                     pExifPtrOffset = pExifHdr + u32DataValue;
2134                     //JPEG_DEBUG_API_MSG("FNumber addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2135                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2136                     _stEXIF_FNumber.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2137                     pExifPtrOffset+=4;
2138                     _stEXIF_FNumber.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2139                     JPEG_DEBUG_API_MSG("FNumber numerator=%ld, denominator=%ld\n", _stEXIF_FNumber.numerator, _stEXIF_FNumber.denominator);
2140                     break;
2141 
2142                 case JPEG_EXIF_TAG_SHUTTER_SPEED:
2143                     pExifPtrOffset = pExifHdrOffet + 8;
2144                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2145                     JPEG_DEBUG_API_MSG("ShutterSpeedValue Offset = 0x%lx\n", u32DataValue);
2146                     pExifPtrOffset = pExifHdr + u32DataValue;
2147                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2148                     //JPEG_DEBUG_API_MSG("ShutterSpeedValue addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2149                     _stEXIF_ShutterSpeedValue.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2150                     pExifPtrOffset+=4;
2151                     _stEXIF_ShutterSpeedValue.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2152                     JPEG_DEBUG_API_MSG("ShutterSpeedValue numerator=%ld, denominator=%ld\n", _stEXIF_ShutterSpeedValue.numerator, _stEXIF_ShutterSpeedValue.denominator);
2153                     break;
2154                 case JPEG_EXIF_TAG_APERTURE:
2155                     pExifPtrOffset = pExifHdrOffet + 8;
2156                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2157                     JPEG_DEBUG_API_MSG("ApertureValue Offset = 0x%lx\n", u32DataValue);
2158                     pExifPtrOffset = pExifHdr + u32DataValue;
2159                     //JPEG_DEBUG_API_MSG("ApertureValue addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2160                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2161                     _stEXIF_ApertureValue.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2162                     pExifPtrOffset+=4;
2163                     _stEXIF_ApertureValue.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2164                     JPEG_DEBUG_API_MSG("ApertureValue numerator=%ld, denominator=%ld\n", _stEXIF_ApertureValue.s_numerator, _stEXIF_ApertureValue.s_denominator);
2165                     break;
2166                 case JPEG_EXIF_TAG_EXPOSURE_BIAS:
2167                     pExifPtrOffset = pExifHdrOffet + 8;
2168                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2169                     JPEG_DEBUG_API_MSG("ExposureBiasValue Offset = 0x%lx\n", u32DataValue);
2170                     pExifPtrOffset = pExifHdr + u32DataValue;
2171                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2172                     //JPEG_DEBUG_API_MSG("ExposureBiasValue addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2173                     _stEXIF_ExposureBiasValue.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2174                     pExifPtrOffset+=4;
2175                     _stEXIF_ExposureBiasValue.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2176                     JPEG_DEBUG_API_MSG("ExposureBiasValue numerator=%ld, denominator=%ld\n", _stEXIF_ExposureBiasValue.numerator, _stEXIF_ExposureBiasValue.denominator);
2177                     break;
2178                 case JPEG_EXIF_TAG_FLASH:
2179                     pExifPtrOffset = pExifHdrOffet + 8;
2180                     _u16EXIF_Flash = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2181                     JPEG_DEBUG_API_MSG("EXIF_Flash=0x%x\n", _u16EXIF_Flash);
2182                     break;
2183                 case JPEG_EXIF_TAG_FOCAL_LENGTH:
2184                     pExifPtrOffset = pExifHdrOffet + 8;
2185                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2186                     JPEG_DEBUG_API_MSG("FocalLength Offset = 0x%lx\n", u32DataValue);
2187                     pExifPtrOffset = pExifHdr + u32DataValue;
2188                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2189                     //JPEG_DEBUG_API_MSG("FocalLength addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2190                     _stEXIF_FocalLength.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2191                     pExifPtrOffset+=4;
2192                     _stEXIF_FocalLength.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2193                     JPEG_DEBUG_API_MSG("FocalLength numerator=%ld, denominator=%ld\n", _stEXIF_FocalLength.s_numerator, _stEXIF_FocalLength.s_denominator);
2194                     break;
2195                 case JPEG_EXIF_TAG_IMAGE_WIDTH:
2196                     pExifPtrOffset = pExifHdrOffet + 2;
2197                     u16Type = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2198                     pExifPtrOffset = pExifHdrOffet + 8;
2199                     if(u16Type==3)
2200                     {
2201                         _u32EXIF_ImageWidth = (MS_U32)JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2202                     }
2203                     else
2204                     {
2205                         _u32EXIF_ImageWidth = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2206                     }
2207                     JPEG_DEBUG_API_MSG("EXIF_ImageWidth=%ld\n", _u32EXIF_ImageWidth);
2208                     break;
2209                 case JPEG_EXIF_TAG_IMAGE_HEIGHT:
2210                     pExifPtrOffset = pExifHdrOffet + 2;
2211                     u16Type = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2212                     pExifPtrOffset = pExifHdrOffet + 8;
2213                     if(u16Type==3)
2214                     {
2215                         _u32EXIF_ImageHeight = (MS_U32)JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2216                     }
2217                     else
2218                     {
2219                         _u32EXIF_ImageHeight = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2220                     }
2221                     JPEG_DEBUG_API_MSG("EXIF_ImageHeight=%ld\n", _u32EXIF_ImageHeight);
2222                     break;
2223                 case JPEG_EXIF_TAG_EXPOSURE_PROGRAM:
2224                     pExifPtrOffset = pExifHdrOffet + 8;
2225                     _u16EXIF_Exposureprogram = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2226                     JPEG_DEBUG_API_MSG("EXIF_Exposureprogram=0x%x\n", _u16EXIF_Exposureprogram);
2227                     break;
2228                 default:
2229                     break;
2230             }
2231 #endif
2232             pExifHdrOffet += 12;
2233         }
2234     }
2235 
2236     if(E_JPEG_TYPE_THUMBNAIL != _u8DecodeType)
2237     {
2238         JPEG_DEBUG_API_MSG("decode type is not thumbnail...\n");
2239         return FALSE;
2240     }
2241 
2242     // 1th IFD
2243     u32tmp = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2244     if (u32tmp == 0)
2245         return FALSE;
2246     else
2247     {
2248         if(u32tmp >= data_length)
2249         {
2250             JPEG_DEBUG_API_ERR("u32tmp = %ld is invalid > %ld\n", u32tmp,data_length);
2251             return FALSE;
2252         }
2253         pNextIfd = &pTiffHdrPtr[u32tmp];
2254         JPEG_DEBUG_API_MSG("1st IFD pNextIfd = 0x%lx, u32tmp = %ld\n", (MS_U32)pNextIfd, u32tmp);
2255     }
2256 
2257     u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pNextIfd));
2258     pNextIfd += 2;
2259 
2260     // Boundary check, prevent from buffer over-run
2261     if((((MS_U32) pNextIfd) - INTERNAL_BUFFER_ADDR + u16NumOfEntry*12)>=data_length)
2262     {
2263         return FALSE;
2264     }
2265 
2266     for (i = 0; i < u16NumOfEntry; i++)
2267     {
2268         u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pNextIfd));
2269 //        u32Len = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pNextIfd + 4));
2270         u32tmp = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pNextIfd + 8));
2271 
2272         switch (u16Marker)
2273         {
2274             case JPEG_TIFF_JPEG_IMG_BYTES:
2275                 _u16ThumbnailSize = u32tmp;
2276                 break;
2277             case JPEG_TIFF_SOI_OFFSET:
2278                 JPEG_DEBUG_API_MSG("Thumbnail marker found in 0x%lx!!\n", (MS_U32)pNextIfd);
2279                 _bThumbnailFound = TRUE;
2280                 _u32ThumbnailOffset += u32tmp;
2281                 break;
2282             default:
2283                 break;
2284         }
2285 
2286         pNextIfd += 12;
2287     }
2288 
2289     // Boundary check, prevent from buffer over-run
2290     if(_bThumbnailFound)
2291     {
2292         if((_u32ThumbnailOffset + _u16ThumbnailSize) > data_length)
2293             _bThumbnailFound = FALSE;
2294 
2295         // means it only contains SOI header..
2296         if(_u16ThumbnailSize<=2)
2297             _bThumbnailFound = FALSE;
2298     }
2299 
2300     return _bThumbnailFound;
2301 }
2302 
2303 // Used to skip unrecognized markers.
JPEG_skip_bytes(MS_U32 count)2304 JPEG_STATIC void JPEG_skip_bytes(MS_U32 count)
2305 {
2306     while(count!=0)
2307     {
2308         // Any bytes remaining in buffer?
2309         if ( !_u32In_buf_left )
2310         {
2311             // Try to get more bytes.
2312             if(!JPEG_fill_read_buffer())
2313             {
2314                 JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer failed!!!\n");
2315                 _u32In_buf_left = 0;
2316             }
2317             // Still nothing to get?
2318             if ( !_u32In_buf_left )
2319             {
2320                 // should not happen
2321                 break;
2322             }
2323         }
2324 
2325         if(count<_u32In_buf_left)
2326         {
2327             _u32In_buf_left -= count;
2328             _pu8In_buf_ofs += count;
2329             count = 0;
2330         }
2331         else
2332         {
2333             count -= _u32In_buf_left;
2334             _u32In_buf_left = 0;
2335         }
2336     }
2337 }
2338 //------------------------------------------------------------------------------
2339 #if SUPPORT_MPO_FORMAT
2340 #define JPEG_TAG_MPO                EndianChangeL(0x4D504600)
2341 #define JPEG_MPO_TAG_MPF_VERSION        0xB000
2342 #define JPEG_MPO_TAG_NUM_OF_IMAGES      0xB001
2343 #define JPEG_MPO_TAG_MP_ENTRY           0xB002
2344 
2345 static JPEG_MPO_INDEX_INFO _stMPOIndex;
2346 static JPEG_MPO_ATTRIBUTE_INFO _stMPOAttri[JPEG_MPO_MAX_SUPPORT_IMAGE];
2347 //For start of offset of MPO(only for first MPO picture)
2348 static MS_U32 _u32App2MarkerOffset = 0;
2349 
2350 typedef enum
2351 {
2352     E_JPEG_MPO_ATTR_IMAGE_NO            = 0xB101
2353   , E_JPEG_MPO_ATTR_PAN_ORIENTATION     = 0xB201
2354   , E_JPEG_MPO_ATTR_PAN_OVERLAP_H       = 0xB202
2355   , E_JPEG_MPO_ATTR_PAN_OVERLAP_V       = 0xB203
2356   , E_JPEG_MPO_ATTR_BASE_VIEWPOINT_NO   = 0xB204
2357   , E_JPEG_MPO_ATTR_CONVERGENCE_ANGLE   = 0xB205
2358   , E_JPEG_MPO_ATTR_BASELINE_LENGTH     = 0xB206
2359   , E_JPEG_MPO_ATTR_DIVERGENCE_ANGLE    = 0xB207
2360   , E_JPEG_MPO_ATTR_AXIS_DISTANCE_X     = 0xB208
2361   , E_JPEG_MPO_ATTR_AXIS_DISTANCE_Y     = 0xB209
2362   , E_JPEG_MPO_ATTR_AXIS_DISTANCE_Z     = 0xB20A
2363   , E_JPEG_MPO_ATTR_YAW_ANGLE           = 0xB20B
2364   , E_JPEG_MPO_ATTR_PITCH_ANGLE         = 0xB20C
2365   , E_JPEG_MPO_ATTR_ROLL_ANGLE          = 0xB20D
2366 } JPEG_MPO_ATTR_TAG;
2367 
2368 #define MAX_MPO_ATTR_TAG_NUM        14          // from E_JPEG_MPO_ATTR_IMAGE_NO to E_JPEG_MPO_ATTR_ROLL_ANGLE
2369 
2370 //***************************************************
2371 //Initialize EXIF MPO database
2372 //***************************************************
JPEG_InitMPO(void)2373 static void JPEG_InitMPO(void)
2374 {
2375     MS_U32 i = 0;
2376 
2377     //init MPO Index structure
2378     _stMPOIndex.num_of_image = 1;
2379     for(i = 0; i < JPEG_MPO_MAX_SUPPORT_IMAGE; i++)
2380     {
2381         _stMPOIndex.mp_entry[i].attribute = 0;
2382         _stMPOIndex.mp_entry[i].size = 0;
2383         _stMPOIndex.mp_entry[i].offset = 0;
2384         _stMPOIndex.mp_entry[i].image1_no = 0;
2385         _stMPOIndex.mp_entry[i].image2_no = 0;
2386     }
2387 
2388     // init MPO Attribute structure
2389     for(i = 0; i < JPEG_MPO_MAX_SUPPORT_IMAGE; i++)
2390     {
2391         _stMPOAttri[i].MPIndividualNum = 0;
2392         _stMPOAttri[i].PanOrientation= 0;
2393         _stMPOAttri[i].PanOverlap_H.numerator = 0;
2394         _stMPOAttri[i].PanOverlap_H.denominator = 0;
2395         _stMPOAttri[i].PanOverlap_V.numerator = 0;
2396         _stMPOAttri[i].PanOverlap_V.denominator = 0;
2397         _stMPOAttri[i].BaseViewpointNum = 0;
2398         _stMPOAttri[i].ConvergenceAngle.s_numerator = 0;
2399         _stMPOAttri[i].ConvergenceAngle.s_denominator = 0;
2400         _stMPOAttri[i].BaselineLength.numerator = 0;
2401         _stMPOAttri[i].BaselineLength.denominator = 0;
2402         _stMPOAttri[i].VerticalDivergence.s_numerator = 0;
2403         _stMPOAttri[i].VerticalDivergence.s_denominator = 0;
2404         _stMPOAttri[i].AxisDistance_X.s_numerator = 0;
2405         _stMPOAttri[i].AxisDistance_X.s_denominator = 0;
2406         _stMPOAttri[i].AxisDistance_Y.s_numerator = 0;
2407         _stMPOAttri[i].AxisDistance_Y.s_denominator = 0;
2408         _stMPOAttri[i].AxisDistance_Z.s_numerator = 0;
2409         _stMPOAttri[i].AxisDistance_Z.s_denominator = 0;
2410         _stMPOAttri[i].YawAngle.s_numerator = 0;
2411         _stMPOAttri[i].YawAngle.s_denominator = 0;
2412         _stMPOAttri[i].PitchAngle.s_numerator = 0;
2413         _stMPOAttri[i].PitchAngle.s_denominator = 0;
2414         _stMPOAttri[i].RollAngle.s_numerator = 0;
2415         _stMPOAttri[i].RollAngle.s_denominator = 0;
2416         _stMPOAttri[i].used = FALSE;
2417     }
2418 }
2419 //***************************************************
2420 //Parse EXIF MPO Attribute header
2421 //***************************************************
JPEG_DecodeMPAttrIFD(MS_U8 * data,MS_U8 * start_of_offset,MS_U32 count)2422 static MS_BOOL JPEG_DecodeMPAttrIFD(MS_U8 *data, MS_U8 *start_of_offset, MS_U32 count)
2423 {
2424     MS_U8 *pMPOAttrPtr = data;
2425     MS_U8 *pMPO_offset;
2426     MS_U16 u16Marker, i = 0, image_idx = 0;
2427 
2428     JPEG_DEBUG_API_MSG("JPEG_DecodeMPAttrIFD, count = %ld\n", count);
2429 
2430     if(count >MAX_MPO_ATTR_TAG_NUM)
2431     {
2432         JPEG_DEBUG_API_ERR("Warning!!!unreasonable attribute IFD number !!!\n");
2433         return TRUE;
2434     }
2435 
2436     for(i = 0; i < JPEG_MPO_MAX_SUPPORT_IMAGE; i++)
2437     {
2438         if(FALSE == _stMPOAttri[i].used)
2439         {
2440             image_idx = i;
2441             break;
2442         }
2443     }
2444 
2445     if(JPEG_MPO_MAX_SUPPORT_IMAGE <= i)
2446     {
2447         JPEG_DEBUG_API_MSG("Not support too much images in MPO!!\n");
2448         return FALSE;
2449     }
2450 
2451     _stMPOAttri[image_idx].used = TRUE;
2452 
2453     for(i = 0; i < count; i++)
2454     {
2455         u16Marker = (JPEG_MPO_ATTR_TAG)JPEG_Tiff_EndianChangeS(JPEG_GetU16(pMPOAttrPtr));
2456         pMPOAttrPtr += 8;
2457         switch(u16Marker)
2458         {
2459             case E_JPEG_MPO_ATTR_IMAGE_NO:
2460                 _stMPOAttri[image_idx].MPIndividualNum = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2461                 break;
2462             case E_JPEG_MPO_ATTR_PAN_ORIENTATION:
2463                 _stMPOAttri[image_idx].PanOrientation = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2464                 break;
2465             case E_JPEG_MPO_ATTR_PAN_OVERLAP_H:
2466                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2467                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2468                 _stMPOAttri[image_idx].PanOverlap_H.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2469                 pMPO_offset += 4;
2470                 _stMPOAttri[image_idx].PanOverlap_H.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2471                 break;
2472             case E_JPEG_MPO_ATTR_PAN_OVERLAP_V:
2473                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2474                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2475                 _stMPOAttri[image_idx].PanOverlap_V.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2476                 pMPO_offset += 4;
2477                 _stMPOAttri[image_idx].PanOverlap_V.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2478                 break;
2479             case E_JPEG_MPO_ATTR_BASE_VIEWPOINT_NO:
2480                 _stMPOAttri[image_idx].BaseViewpointNum = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2481                 break;
2482             case E_JPEG_MPO_ATTR_CONVERGENCE_ANGLE:
2483                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2484                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2485                 _stMPOAttri[image_idx].ConvergenceAngle.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2486                 pMPO_offset += 4;
2487                 _stMPOAttri[image_idx].ConvergenceAngle.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2488                 break;
2489             case E_JPEG_MPO_ATTR_BASELINE_LENGTH:
2490                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2491                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2492                 _stMPOAttri[image_idx].BaselineLength.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2493                 pMPO_offset += 4;
2494                 _stMPOAttri[image_idx].BaselineLength.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2495                 break;
2496             case E_JPEG_MPO_ATTR_DIVERGENCE_ANGLE:
2497                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2498                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2499                 _stMPOAttri[image_idx].VerticalDivergence.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2500                 pMPO_offset += 4;
2501                 _stMPOAttri[image_idx].VerticalDivergence.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2502                 break;
2503             case E_JPEG_MPO_ATTR_AXIS_DISTANCE_X:
2504                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2505                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2506                 _stMPOAttri[image_idx].AxisDistance_X.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2507                 pMPO_offset += 4;
2508                 _stMPOAttri[image_idx].AxisDistance_X.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2509                 break;
2510             case E_JPEG_MPO_ATTR_AXIS_DISTANCE_Y:
2511                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2512                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2513                 _stMPOAttri[image_idx].AxisDistance_Y.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2514                 pMPO_offset += 4;
2515                 _stMPOAttri[image_idx].AxisDistance_Y.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2516                 break;
2517             case E_JPEG_MPO_ATTR_AXIS_DISTANCE_Z:
2518                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2519                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2520                 _stMPOAttri[image_idx].AxisDistance_Z.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2521                 pMPO_offset += 4;
2522                 _stMPOAttri[image_idx].AxisDistance_Z.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2523                 break;
2524             case E_JPEG_MPO_ATTR_YAW_ANGLE:
2525                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2526                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2527                 _stMPOAttri[image_idx].YawAngle.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2528                 pMPO_offset += 4;
2529                 _stMPOAttri[image_idx].YawAngle.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2530                 break;
2531             case E_JPEG_MPO_ATTR_PITCH_ANGLE:
2532                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2533                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2534                 _stMPOAttri[image_idx].PitchAngle.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2535                 pMPO_offset += 4;
2536                 _stMPOAttri[image_idx].PitchAngle.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2537                 break;
2538             case E_JPEG_MPO_ATTR_ROLL_ANGLE:
2539                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2540                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2541                 _stMPOAttri[image_idx].RollAngle.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2542                 pMPO_offset += 4;
2543                 _stMPOAttri[image_idx].RollAngle.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2544                 break;
2545             default:
2546                 JPEG_DEBUG_API_ERR("Unknown MPO attribute marker =0x%x!!!!!!\n", u16Marker);
2547                 break;
2548         }
2549         pMPOAttrPtr += 4;
2550     }
2551     return TRUE;
2552 }
2553 //***************************************************
2554 //Parse EXIF MPO header
2555 //***************************************************
JPEG_DecodeMPOInfo(MS_U8 * data)2556 static MS_BOOL JPEG_DecodeMPOInfo(MS_U8 *data)
2557 {
2558     MS_U8 *pJpegBuff = data;
2559     MS_U8 *pMPOHdrPtr;
2560     MS_U32 u32tmp, u32Len;
2561     MS_U16 u16Marker, u16NumOfEntry, i;
2562 
2563     u32tmp = JPEG_GetU32(pJpegBuff);
2564     if (u32tmp != JPEG_TAG_MPO)
2565     {
2566         return FALSE;
2567     }
2568 
2569     // mpo header start from here
2570     pJpegBuff += 4;
2571     u16Marker = JPEG_GetU16(pJpegBuff);
2572     if (u16Marker == JPEG_TIFF_BIG_ENDIAN)
2573         _bTiffBigEndian = TRUE;
2574     else if (u16Marker == JPEG_TIFF_LITTLE_ENDIAN)
2575         _bTiffBigEndian = FALSE;
2576     else
2577         return FALSE;
2578 
2579 //    _u32ThumbnailOffset += 4;
2580     pMPOHdrPtr = pJpegBuff;
2581 
2582     pJpegBuff += 2;
2583     u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2584     if (u16Marker != 0x002A)
2585         return FALSE;
2586 
2587     //offset to 1st MPO IFD
2588     pJpegBuff += 2;
2589     u32Len = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2590 
2591     //1st MPO IFD start from here(either MP Index IFD or MP Attribute IFD)
2592     //Count Info
2593     pJpegBuff = pMPOHdrPtr + u32Len;
2594     JPEG_OVER_BUFFER_RET(pJpegBuff, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2595 
2596     u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2597 
2598     JPEG_DEBUG_API_MSG("num of entry = %d\n", u16NumOfEntry);
2599 
2600     //MPFVersion
2601     pJpegBuff += 2;
2602     u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2603     if(u16Marker == JPEG_MPO_TAG_MPF_VERSION)
2604     {
2605         pJpegBuff += 12;
2606         u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2607         if(u16Marker == JPEG_MPO_TAG_NUM_OF_IMAGES)
2608         {
2609             //It is a MP Index IFD
2610             //Check number of images
2611             pJpegBuff += 8;
2612             u32tmp = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2613 
2614             if(JPEG_MPO_MAX_SUPPORT_IMAGE < u32tmp)
2615             {
2616                 JPEG_DEBUG_API_MSG("Not support %ld images in MPO!!\n", u32tmp);
2617                 _stMPOIndex.num_of_image = 0xFFFFFFFF;
2618                 return FALSE;
2619             }
2620 
2621             //Init MPO database
2622             JPEG_InitMPO();
2623 
2624             //start of offset for every individual picture
2625             _stMPOIndex.start_of_offset = _u32App2MarkerOffset + 8 - MRC_BUFFER_ADDR;
2626 
2627             //the numbers of image
2628             _stMPOIndex.num_of_image = u32tmp;
2629 
2630             printf_red("detected _stMPOIndex.num_of_image=0x%lx\n", _stMPOIndex.num_of_image);
2631             //MP Entry
2632             pJpegBuff += 4;
2633             u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2634             if(u16Marker != JPEG_MPO_TAG_MP_ENTRY)
2635             {
2636                 JPEG_DEBUG_API_MSG("No MP Entry in MP index IFD!!\n");
2637                 return FALSE;
2638             }
2639 
2640             //offset to next IFD(MP Attribute IFD)
2641             pJpegBuff = pMPOHdrPtr + 10 + u16NumOfEntry*12;
2642             u32Len = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2643             JPEG_DEBUG_API_MSG("pJpegBuff = %lx, u32Len = %ld\n", (MS_U32)pJpegBuff, u32Len);
2644 
2645             //MP Entry Internal Fields
2646             pJpegBuff += 4;
2647             for(i = 0; i < _stMPOIndex.num_of_image; i++)
2648             {
2649                 //Image Attribute
2650                 _stMPOIndex.mp_entry[i].attribute = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2651                 //Image size
2652                 pJpegBuff += 4;
2653                 _stMPOIndex.mp_entry[i].size = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2654                 //Image size
2655                 pJpegBuff += 4;
2656                 _stMPOIndex.mp_entry[i].offset = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2657                 //Image size
2658                 pJpegBuff += 4;
2659                 _stMPOIndex.mp_entry[i].image1_no = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2660                 //Image size
2661                 pJpegBuff += 2;
2662                 _stMPOIndex.mp_entry[i].image2_no = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2663                 pJpegBuff += 2;
2664                 JPEG_DEBUG_API_MSG("%d th : attribute = %lx\n", i, _stMPOIndex.mp_entry[i].attribute);
2665                 JPEG_DEBUG_API_MSG("%d th : size = %lx\n", i, _stMPOIndex.mp_entry[i].size);
2666                 JPEG_DEBUG_API_MSG("%d th : offset = %lx\n", i, _stMPOIndex.mp_entry[i].offset);
2667                 JPEG_DEBUG_API_MSG("%d th : image1_no = %x\n", i, _stMPOIndex.mp_entry[i].image1_no);
2668                 JPEG_DEBUG_API_MSG("%d th : image2_no = %x\n", i, _stMPOIndex.mp_entry[i].image2_no);
2669             }
2670 
2671             //get MPO Attribute count
2672             pJpegBuff = pMPOHdrPtr + u32Len;
2673             JPEG_OVER_BUFFER_RET(pJpegBuff, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2674 
2675             u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2676 
2677             pJpegBuff += 2;
2678         }
2679     }
2680 
2681     //Decode MP Attr header
2682     return JPEG_DecodeMPAttrIFD(pJpegBuff, pMPOHdrPtr, u16NumOfEntry);
2683 }
2684 
2685 
2686 #endif
2687 
2688 // Parse APP0 to get X&Y density.
JPEG_read_app0_marker(void)2689 static MS_BOOL JPEG_read_app0_marker( void )
2690 {
2691 
2692     MS_U32 left;
2693     MS_U8 u8Xthumbnail, u8Ythumbnail;
2694     JPEG_DEBUG_API_MSG("APP0\n");
2695 
2696     //Get APP0 length
2697     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2698 
2699     left-= 2;
2700 
2701     if(left < 8)
2702     {
2703         JPEG_terminate( E_JPEG_BAD_APP0_MARKER );
2704         return FALSE;
2705     }
2706 
2707     //Skip Block ID "JFIF"
2708     JPEG_skip_bytes(5);
2709 
2710     left-= 5;
2711 
2712     //Skip Version Number
2713     JPEG_skip_bytes(2);
2714 
2715     left-= 2;
2716 
2717     //Get units
2718     App0_Unit_Data.u8Unit = JPEG_get_char();
2719     App0_Unit_Data.u16Xdensity= (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2720     App0_Unit_Data.u16Ydensity= (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2721 
2722     left-= 5;
2723 
2724     //Get thumbnail x,y
2725     u8Xthumbnail = JPEG_get_char();
2726     u8Ythumbnail = JPEG_get_char();
2727 
2728     left-= 2;
2729 
2730     //Skip Version Number
2731     JPEG_skip_bytes(3*u8Xthumbnail*u8Ythumbnail);
2732 
2733     left-= 3*u8Xthumbnail*u8Ythumbnail;
2734 
2735     JPEG_DEBUG_API_MSG("APP0 : unit : %d\n",(MS_U16)App0_Unit_Data.u8Unit);
2736     JPEG_DEBUG_API_MSG("APP0 : Xdensity : %d\n",App0_Unit_Data.u16Xdensity);
2737     JPEG_DEBUG_API_MSG("APP0 : Ydensity : %d\n",App0_Unit_Data.u16Ydensity);
2738 
2739     if(left > 0)
2740         JPEG_skip_bytes(left);
2741 
2742     return TRUE;
2743 }
2744 
_msAPI_JPEG_get_APP0_info(MS_U8 * unit,MS_U16 * x,MS_U16 * y)2745 void _msAPI_JPEG_get_APP0_info( MS_U8 *unit, MS_U16 *x, MS_U16 *y )
2746 {
2747     *unit = App0_Unit_Data.u8Unit;
2748     *x = App0_Unit_Data.u16Xdensity;
2749     *y = App0_Unit_Data.u16Ydensity;
2750 }
2751 //------------------------------------------------------------------------------
2752 // Read exif info
JPEG_read_app1_marker(void)2753 JPEG_STATIC MS_BOOL JPEG_read_app1_marker(void)
2754 {
2755     MS_U16 length;
2756     MS_U8 *exif_buffer = (MS_U8 *) INTERNAL_BUFFER_ADDR;
2757     MS_U16 i = 0;
2758 
2759     JPEG_DEBUG_API_MSG("APP1\n");
2760 
2761     length = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2762 
2763     if ( length < 2 )
2764     {
2765         JPEG_terminate( E_JPEG_BAD_APP1_MARKER );
2766         return FALSE;
2767     }
2768 
2769     length -= 2;
2770 
2771 #if SUPPORT_MPO_FORMAT
2772     u32MPFOffset = length;
2773     JPEG_DEBUG_API_MSG("u32MPFOffset=0x%lx\n", u32MPFOffset);
2774 #endif
2775     while((length - i)!=0)
2776     {
2777         exif_buffer[i] = (MS_U8) JPEG_get_char();
2778         i++;
2779     }
2780 
2781     if(JPEG_DecodeExifInfo(exif_buffer, length) == TRUE)
2782     {
2783         JPEG_DEBUG_API_MSG("FOUND THUMBNAIL!\n");
2784         _u32ThumbnailBufferOffset = _u32ThumbnailOffset;
2785         _u16ThumbnailBufferSize = _u16ThumbnailSize;
2786     }
2787     else
2788     {
2789         JPEG_DEBUG_API_MSG("NO THUMBNAIL!\n");
2790     }
2791     return TRUE;
2792 }
2793 
2794 #if SUPPORT_MPO_FORMAT
2795 
2796 // Read exif app2 info
JPEG_read_app2_marker(void)2797 static MS_BOOL JPEG_read_app2_marker( void )
2798 {
2799     MS_U16 length;
2800     MS_U8 *exif_buffer = (MS_U8 *) (INTERNAL_BUFFER_ADDR + u32MPFOffset);
2801     MS_U16 i = 0;
2802 
2803     JPEG_DEBUG_API_MSG("APP2\n");
2804 
2805     //For start of offset of MPO(only for first MPO picture)
2806     JPEG_DEBUG_API_MSG("_pu8In_buf_ofs = %lx\n", (MS_U32)_pu8In_buf_ofs);
2807     _u32App2MarkerOffset = (MS_U32)_pu8In_buf_ofs - 2;
2808 
2809     length = (MS_U16)((JPEG_get_char()<<8)+JPEG_get_char());
2810 
2811     if ( length < 2 )
2812     {
2813         JPEG_terminate( E_JPEG_BAD_APP2_MARKER );
2814         return FALSE;
2815     }
2816 
2817     length -= 2;
2818 
2819     if(length > INTERNAL_BUFFER_SIZE - u32MPFOffset)
2820     {
2821         JPEG_terminate( E_JPEG_NOTENOUGHMEM );
2822         return FALSE;
2823     }
2824 
2825     while((length - i)!=0)
2826     {
2827         exif_buffer[i] = (MS_U8) JPEG_get_char();
2828         i++;
2829     }
2830 
2831     if(JPEG_DecodeMPOInfo(exif_buffer)==TRUE)
2832     {
2833         JPEG_DEBUG_API_MSG("FOUND MPO!\n");
2834         bIsMPOFormat = TRUE;
2835         u32MPFOffset = 0;           // Reset to zero after FOUND MPO
2836 
2837         //// TODO: MPO format not support thumbnail now....
2838         //_u8DecodeType = E_JPEG_TYPE_MAIN;
2839         //printf_red("NOTE: MPO not support thumbnail now!!!\n");
2840     }
2841     else
2842     {
2843         JPEG_DEBUG_API_MSG("NO MPO!\n");
2844         // Not clear num_of_image for the image with 2 app2 marker
2845         // _stMPOIndex.num_of_image = 0;
2846     }
2847     return TRUE;
2848 }
2849 
2850 
2851 #endif
2852 
2853 // Read app14 info
JPEG_read_app14_marker(void)2854 static MS_BOOL JPEG_read_app14_marker( void )
2855 {
2856 
2857     MS_U32 left;
2858     MS_U8 u8Transform;
2859 
2860     JPEG_DEBUG_API_MSG("APP14\n");
2861 
2862     //Get APP14 length
2863     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2864 
2865     left-= 2;
2866 
2867     if(left < 8)
2868     {
2869         JPEG_terminate( E_JPEG_BAD_APP0_MARKER );
2870         return FALSE;
2871     }
2872 
2873     //Skip Block ID "Adobe"
2874     JPEG_skip_bytes(5);
2875 
2876     left-= 5;
2877 
2878     //Skip Version Number
2879     JPEG_skip_bytes(2);
2880 
2881     left-= 2;
2882 
2883     //Skip F0 Number
2884     JPEG_skip_bytes(2);
2885 
2886     left-= 2;
2887 
2888     //Skip F1 Number
2889     JPEG_skip_bytes(2);
2890 
2891     left-= 2;
2892 
2893     u8Transform=  JPEG_get_char();
2894 
2895     left-= 1;
2896 
2897     if(u8Transform!=1)
2898     {
2899         // 0: RGB or CMYK, depends on SOF
2900         // 1: YCbCr
2901         // 2: YCCK
2902         JPEG_DEBUG_API_MSG("Unsupported Adobe Format with Transform=%d\n", u8Transform);
2903         JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
2904         return FALSE;
2905     }
2906 
2907     if(left > 0)
2908         JPEG_skip_bytes(left);
2909 
2910     return TRUE;
2911 }
2912 
2913 /* END OF EXIF PARSING SECTION */
2914 //------------------------------------------------------------------------------
2915 // Read a Huffman code table.
JPEG_read_dht_marker(void)2916 JPEG_STATIC MS_BOOL JPEG_read_dht_marker(void)
2917 {
2918     MS_U16 i, index, count;
2919     MS_U32 left;
2920     MS_U8 u8Huff_num[17];
2921     MS_U8 u8Huff_val[256];
2922 
2923     JPEG_DEBUG_API_MSG("DHT\n");
2924 
2925     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2926 
2927     if ( left < 2 )
2928     {
2929         JPEG_terminate( E_JPEG_BAD_DHT_MARKER );
2930         return FALSE;
2931     }
2932 
2933     left -= 2;
2934 
2935     while ( left )
2936     {
2937         // set it to zero, initialize
2938         JPEG_memset((void *)u8Huff_num, 0, 17);
2939         JPEG_memset((void *)u8Huff_val, 0, 256);
2940 
2941         index = JPEG_get_char();
2942 
2943         u8Huff_num[0] = 0;
2944 
2945         count = 0;
2946 
2947         for ( i = 1; i <= 16; i++ )
2948         {
2949             u8Huff_num[i] = JPEG_get_char();
2950             count += u8Huff_num[i];
2951         }
2952 
2953         if ( count > 255 )
2954         {
2955             JPEG_terminate( E_JPEG_BAD_DHT_COUNTS );
2956             return FALSE;
2957         }
2958 
2959         for ( i = 0; i < count; i++ )
2960         {
2961             u8Huff_val[i] = JPEG_get_char();
2962         }
2963 
2964         i = 1 + 16 + count;
2965 
2966         if ( left < ( MS_U32 )i )
2967         {
2968             JPEG_terminate( E_JPEG_BAD_DHT_MARKER );
2969             return FALSE;
2970         }
2971 
2972         left -= i;
2973 
2974         index = ( index & 0x0F ) + ( ( index & 0x10 ) >> 4 ) * ( JPEG_MAXHUFFTABLES >> 1 ); //???
2975 
2976         if ( index >= JPEG_MAXHUFFTABLES )
2977         {
2978             JPEG_terminate( E_JPEG_BAD_DHT_INDEX );
2979             return FALSE;
2980         }
2981 
2982         if(_Huff_info[index].bValid==FALSE)
2983             _Huff_info[index].bValid = TRUE;
2984 
2985         JPEG_memcpy( (void *)_Huff_info[index].u8Huff_num, (void *)u8Huff_num, 17 );
2986         JPEG_memcpy( (void *)_Huff_info[index].u8Huff_val, (void *)u8Huff_val, 256 );
2987 
2988         // Compute the inverse order of HuffNum. this step is only needed in JPD mode (baseline)
2989         for(i = 1; i<=16; i++)
2990         {
2991             if(u8Huff_num[17 - i]!=0)
2992             {
2993                 count = count - u8Huff_num[17 - i];
2994                 u8Huff_num[17 - i] = count;
2995             }
2996             else
2997                 u8Huff_num[17 - i] = 0xFF;
2998         }
2999         JPEG_memcpy( (void *)_Huff_info[index].u8Symbol, (void *)u8Huff_num, 17 );
3000     }
3001     return TRUE;
3002 }
3003 //------------------------------------------------------------------------------
3004 // Read a quantization table.
JPEG_read_dqt_marker(void)3005 JPEG_STATIC MS_BOOL JPEG_read_dqt_marker(void)
3006 {
3007     MS_U16 n, i, prec;
3008     MS_U32 left;
3009     MS_U32 temp;
3010 
3011     JPEG_DEBUG_API_MSG("DQT\n");
3012 
3013     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3014 
3015     if ( left < 2 )
3016     {
3017         JPEG_terminate( E_JPEG_BAD_DQT_MARKER );
3018         return FALSE;
3019     }
3020 
3021     left -= 2;
3022 
3023     while ( left )
3024     {
3025         n = JPEG_get_char();
3026         prec = n >> 4;
3027         n &= 0x0F;
3028 
3029         if ( n >= JPEG_MAXQUANTTABLES )
3030         {
3031             JPEG_terminate( E_JPEG_BAD_DQT_TABLE );
3032             return FALSE;
3033         }
3034 
3035         if(_QuantTables[n].bValid == FALSE)
3036             _QuantTables[n].bValid = TRUE;
3037 
3038         // read quantization entries, in zag order
3039         for ( i = 0; i < 64; i++ )
3040         {
3041             temp = JPEG_get_char();
3042 
3043             if ( prec )
3044             {
3045                 temp = ( temp << 8 ) + JPEG_get_char();
3046             }
3047 
3048             _QuantTables[n].s16Value[i] = temp;
3049         }
3050 
3051         i = 64 + 1;
3052 
3053         if ( prec )
3054         {
3055             i += 64;
3056         }
3057 
3058         if ( left < ( MS_U32 )i )
3059         {
3060             JPEG_terminate( E_JPEG_BAD_DQT_LENGTH );
3061             return FALSE;
3062         }
3063 
3064         left -= i;
3065     }
3066     return TRUE;
3067 }
3068 //------------------------------------------------------------------------------
3069 // Read the start of frame (SOF) marker.
JPEG_read_sof_marker(void)3070 JPEG_STATIC MS_BOOL JPEG_read_sof_marker(void)
3071 {
3072     MS_U8 i;
3073     MS_U32 left;
3074     MS_U16 JpegMaxWidth, JpegMaxHeight;
3075     MS_U8 c1;
3076 
3077     JPEG_DEBUG_API_MSG("SOF\n");
3078 
3079     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3080 
3081     if ( JPEG_get_char() != 8 )   /* precision: sorry, only 8-bit precision is supported right now */
3082     {
3083         JPEG_terminate( E_JPEG_BAD_PRECISION );
3084         return FALSE;
3085     }
3086 
3087     _u16Image_y_size = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3088     _u16Image_x_size = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3089 
3090     // save the original image size
3091     _u16OriginalImage_x_size = _u16Image_x_size;
3092     _u16OriginalImage_y_size = _u16Image_y_size;
3093 
3094     if((_bProgressive_flag)
3095     && (E_JPEG_TYPE_MAIN == _u8DecodeType))
3096     {
3097        //Define MAX picture size for progressive, 8 is the max factor of scale down
3098         JpegMaxWidth  = JPEG_PRO_MAX_WIDTH * 8;
3099         JpegMaxHeight = JPEG_PRO_MAX_HEIGHT * 8;
3100 
3101         //Calculate Internal Buffer size
3102         //check needed buffer for progressive jpeg decoding, for worst case:
3103         MS_U32 buff_size = _u16Image_x_size*_u16Image_y_size*3*2
3104                          + _u16Image_x_size*_u16Image_y_size*3*2/64
3105                          + JPEG_DEFAULT_EXIF_SIZE
3106                          + 8*1024;
3107 //        if( (MS_U32)(_u16Image_x_size*_u16Image_y_size*3*2) > (MS_U32)(INTERNAL_BUFFER_SIZE - JPEG_DEFAULT_EXIF_SIZE) )
3108         if( buff_size > INTERNAL_BUFFER_SIZE )
3109         {
3110             JPEG_DEBUG_API_ERR("Progressive image size too big... do not handle it\n");
3111             JPEG_terminate( E_JPEG_NOTENOUGHMEM );
3112             return FALSE;
3113         }
3114     }
3115     else
3116     {
3117         //Define MAX picture size for Baseline, 8 is the max factor of scale down
3118         JpegMaxWidth  = JPEG_MAX_WIDTH*8;
3119         JpegMaxHeight = JPEG_MAX_HEIGHT*8;
3120     }
3121 
3122     if ((_u16Image_y_size < 1) || (_u16Image_y_size > JpegMaxHeight))
3123     {
3124         JPEG_terminate(E_JPEG_BAD_HEIGHT);
3125         return FALSE;
3126     }
3127 
3128     if ((_u16Image_x_size < 1) || (_u16Image_x_size > JpegMaxWidth))
3129     {
3130         JPEG_terminate(E_JPEG_BAD_WIDTH);
3131         return FALSE;
3132     }
3133 
3134     _u8Comps_in_frame = JPEG_get_char();
3135 
3136     if ( _u8Comps_in_frame > JPEG_MAXCOMPONENTS )
3137     {
3138         JPEG_terminate( E_JPEG_TOO_MANY_COMPONENTS );
3139         return FALSE;
3140     }
3141 
3142     if ( left != ( MS_U32 )( _u8Comps_in_frame * 3 + 8 ) )
3143     {
3144         JPEG_terminate( E_JPEG_BAD_SOF_LENGTH );
3145         return FALSE;
3146     }
3147 
3148     for ( i = 0; i < _u8Comps_in_frame; i++ )
3149     {
3150         _u8Comp_ident[i] = JPEG_get_char();
3151         if(_u8Comp_ident[i]==0)     // The component ID is start from 0 (0 1 2). The normal case is start from 1 (1 2 3) for YUV
3152         {
3153             _u8LumaCi = 0;
3154             _u8ChromaCi = 1;
3155             _u8Chroma2Ci = 2;
3156         }
3157 
3158         c1 = JPEG_get_char();
3159 
3160         _u8Comp_h_samp[i] = (c1 & 0xf0)>>4;
3161         _u8Comp_v_samp[i] = (c1 & 0x0f);
3162         _u8Comp_quant[i] = JPEG_get_char();
3163 
3164         // patch from LG driver for the file with the following problem:
3165         // only has one component, but its sampling factor is 1x2
3166         // Per the JPEG spec A.2.2 (see the attached file, "regardless of the values of H1 and V1"),
3167         // please always set H=1 & V=1 to hw, when mono image.
3168         if(_u8Comps_in_frame==1)    //allen.chang 2011/6/16 patch
3169         {
3170             _u8Comp_h_samp[0] =1;
3171             _u8Comp_v_samp[0] =1;
3172         }
3173 
3174     }
3175     return TRUE;
3176 }
3177 //------------------------------------------------------------------------------
3178 // Used to skip unrecognized markers.
JPEG_skip_variable_marker(void)3179 JPEG_STATIC MS_BOOL JPEG_skip_variable_marker(void)
3180 {
3181     MS_U32 left;
3182 
3183     JPEG_DEBUG_API_MSG("SKIP markers\n");
3184 
3185     left = (MS_U32)((JPEG_get_char()<<8)|JPEG_get_char());
3186 
3187     if ( left < 2 )
3188     {
3189         JPEG_terminate( E_JPEG_BAD_VARIABLE_MARKER );
3190         return FALSE;
3191     }
3192 
3193     left -= 2;
3194 
3195     JPEG_skip_bytes(left);
3196     return TRUE;
3197 }
3198 //------------------------------------------------------------------------------
3199 // Read a define restart interval (DRI) marker.
JPEG_read_dri_marker(void)3200 JPEG_STATIC MS_BOOL JPEG_read_dri_marker(void)
3201 {
3202     JPEG_DEBUG_API_MSG("DRI\n");
3203 
3204     if ( (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char()) != 4 )
3205     {
3206         JPEG_terminate( E_JPEG_BAD_DRI_LENGTH );
3207         return FALSE;
3208     }
3209 
3210     _u16Restart_interval = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3211     return TRUE;
3212 }
3213 //------------------------------------------------------------------------------
3214 // Read a start of scan (SOS) marker.
JPEG_read_sos_marker(void)3215 JPEG_STATIC MS_BOOL JPEG_read_sos_marker(void)
3216 {
3217     MS_U32 left;
3218     MS_U16 i, ci, n, c, cc;
3219     MS_U8 c1;
3220 
3221     JPEG_DEBUG_API_MSG("SOS\n");
3222 
3223     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3224 
3225     n = JPEG_get_char();
3226 
3227     _u8Comps_in_scan = n;
3228 
3229     left -= 3;
3230 
3231     if ( ( left != ( MS_U32 )( n * 2 + 3 ) ) || ( n < 1 ) || ( n > JPEG_MAXCOMPSINSCAN ) )
3232     {
3233         JPEG_terminate( E_JPEG_BAD_SOS_LENGTH );
3234         return FALSE;
3235     }
3236 
3237     for ( i = 0; i < n; i++ )
3238     {
3239         cc = JPEG_get_char();
3240         c = JPEG_get_char();
3241         left -= 2;
3242 
3243         for ( ci = 0; ci < _u8Comps_in_frame; ci++ )
3244         {
3245             if ( cc == _u8Comp_ident[ci] )
3246             {
3247                 break;
3248             }
3249         }
3250 
3251         if ( ci >= _u8Comps_in_frame )
3252         {
3253             JPEG_terminate( E_JPEG_BAD_SOS_COMP_ID );
3254             return FALSE;
3255         }
3256 
3257         _u8Comp_list[i] = ci;
3258         _u8Comp_dc_tab[ci] = ( c >> 4 ) & 15;
3259         _u8Comp_ac_tab[ci] = ( c & 15 ) + ( JPEG_MAXHUFFTABLES >> 1 );
3260     }
3261 
3262 #ifndef CMODEL
3263     //HW limitation, for baseline JPEG, U.V need to refer to the same DC and AC huffman table.
3264     if(!_bProgressive_flag && (_u8Comps_in_frame == 3)) //Y.U.V.
3265     {
3266         if((_u8Comp_dc_tab[1] != _u8Comp_dc_tab[2])
3267         || (_u8Comp_ac_tab[1] != _u8Comp_ac_tab[2]))
3268         {
3269 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
3270             JPEG_DEBUG_API_MSG("U, V use different Huffman table~~\n");
3271             bIs3HuffTbl = TRUE;
3272 #else
3273             JPEG_terminate( E_JPEG_UNSUPPORTED_HUFF_DECODE );
3274             return FALSE;
3275 #endif
3276         }
3277     }
3278 #endif
3279 
3280     _u8Spectral_start = JPEG_get_char();
3281     _u8Spectral_end = JPEG_get_char();
3282     c1 = JPEG_get_char();
3283     _u8Successive_high = (c1 & 0xf0)>>4;
3284     _u8Successive_low = (c1 & 0x0f);
3285 
3286     if ( !_bProgressive_flag )
3287     {
3288         _u8Spectral_start = 0;
3289         _u8Spectral_end = 63;
3290     }
3291 
3292     left -= 3;
3293 
3294     JPEG_skip_bytes(left);    /* read past whatever is left */
3295     return TRUE;
3296 }
3297 //------------------------------------------------------------------------------
3298 // Finds the next marker.
JPEG_next_marker(void)3299 JPEG_STATIC MS_U32 JPEG_next_marker(void) //ok
3300 {
3301     MS_U32 c, bytes;
3302 
3303     bytes = 0;
3304 
3305     do
3306     {
3307         do
3308         {
3309             bytes++;
3310 
3311             c = JPEG_get_char();
3312             //JPEG_DEBUG_API_MSG("c = %X\n",c);
3313         }
3314         while ( c != 0xFF );
3315 
3316         do
3317         {
3318             c = JPEG_get_char();
3319             //JPEG_DEBUG_API_MSG("c = %X\n",c);
3320         }
3321         while ( c == 0xFF );
3322     }
3323     while ( c == 0 );
3324 
3325     // If bytes > 0 here, there where extra bytes before the marker (not good).
3326 
3327     return c;
3328 }
3329 //------------------------------------------------------------------------------
3330 // Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is
3331 // encountered.
JPEG_process_markers(void)3332 JPEG_STATIC MS_U32 JPEG_process_markers(void)
3333 {
3334     MS_U32 c;
3335 
3336     JPEG_DEBUG_API_MSG("JPEG_process_markers:\n");
3337     for ( ; ; )
3338     {
3339         c = JPEG_next_marker();
3340 
3341         switch ( c )
3342         {
3343             case E_JPEG_APP1:
3344                 // Prevent from there's thumbnail in thumbnail... & multiple APP1
3345                 // Although it's impossible.. =_=
3346 //                if((E_JPEG_TYPE_THUMBNAIL == _u8DecodeType)
3347 //                && (FALSE == _bThumbnailFound))
3348                 // We need to get EXIF info for decoding main picture or thumbnail.
3349                 if((__bIsMjpeg == FALSE)
3350                 && (FALSE == _bThumbnailFound))
3351                 {
3352                     if( !JPEG_read_app1_marker() )
3353                         return FALSE;
3354                 }
3355                 else
3356                 {
3357                     if( !JPEG_skip_variable_marker() )
3358                         return FALSE;
3359                 }
3360                 break;
3361 #if SUPPORT_MPO_FORMAT
3362             case E_JPEG_APP2:
3363                 if( !JPEG_read_app2_marker() )
3364                     return FALSE;
3365                 break;
3366 
3367 #endif
3368             case E_JPEG_SOF0:
3369             case E_JPEG_SOF1:
3370             case E_JPEG_SOF2:
3371             case E_JPEG_SOF3:
3372             case E_JPEG_SOF5:
3373             case E_JPEG_SOF6:
3374             case E_JPEG_SOF7:
3375                 //      case E_JPEG_JPG:
3376             case E_JPEG_SOF9:
3377             case E_JPEG_SOF10:
3378             case E_JPEG_SOF11:
3379             case E_JPEG_SOF13:
3380             case E_JPEG_SOF14:
3381             case E_JPEG_SOF15:
3382             case E_JPEG_SOI:
3383             case E_JPEG_EOI:
3384             case E_JPEG_SOS:
3385                 {
3386                     return c;
3387                 }
3388             case E_JPEG_DHT:
3389                 {
3390                     if( !JPEG_read_dht_marker() )
3391                         return FALSE;
3392                     _HeadCheck.DHT = TRUE;
3393                     break;
3394                 }
3395                 // Sorry, no arithmitic support at this time. Dumb patents!
3396             case E_JPEG_DAC:
3397                 {
3398                     JPEG_terminate( E_JPEG_NO_ARITHMETIC_SUPPORT );
3399                     return FALSE;
3400                     break;
3401                 }
3402             case E_JPEG_DQT:
3403                 {
3404                     if( !JPEG_read_dqt_marker() )
3405                         return FALSE;
3406                     _HeadCheck.DQT = TRUE;
3407                     break;
3408                 }
3409             case E_JPEG_DRI:
3410                 {
3411                     if( !JPEG_read_dri_marker() )
3412                         return FALSE;
3413                     break;
3414                 }
3415                 //case E_JPEG_APP0:  /* no need to read the JFIF marker */
3416 
3417             case E_JPEG_JPG:
3418             case E_JPEG_RST0:
3419                 /* no parameters */
3420             case E_JPEG_RST1:
3421             case E_JPEG_RST2:
3422             case E_JPEG_RST3:
3423             case E_JPEG_RST4:
3424             case E_JPEG_RST5:
3425             case E_JPEG_RST6:
3426             case E_JPEG_RST7:
3427             case E_JPEG_TEM:
3428                 {
3429                     JPEG_terminate( E_JPEG_UNEXPECTED_MARKER );
3430                     return FALSE;
3431                     break;
3432                 }
3433             case E_JPEG_APP0:
3434                 if (!bMHEG5)
3435                 {
3436                     if(!JPEG_skip_variable_marker())
3437                     {
3438                         return FALSE;
3439                     }
3440                     break;
3441                 }
3442                 if(!JPEG_read_app0_marker())
3443                 {
3444                     return FALSE;
3445                 }
3446                 break;
3447             case E_JPEG_APP14:
3448                 if(__bIsMjpeg == FALSE)
3449                 {
3450                     if( !JPEG_read_app14_marker() )
3451                         return FALSE;
3452                 }
3453                 else
3454                 {
3455                     if( !JPEG_skip_variable_marker() )
3456                         return FALSE;
3457                 }
3458                 break;
3459             case E_JPEG_DNL:
3460             case E_JPEG_DHP:
3461             case E_JPEG_EXP:
3462 #if (SUPPORT_MPO_FORMAT == 0)
3463             case E_JPEG_APP2:
3464 #endif
3465             case E_JPEG_APP3:
3466             case E_JPEG_APP4:
3467             case E_JPEG_APP5:
3468             case E_JPEG_APP6:
3469             case E_JPEG_APP7:
3470             case E_JPEG_APP8:
3471             case E_JPEG_APP9:
3472             case E_JPEG_APP10:
3473             case E_JPEG_APP11:
3474             case E_JPEG_APP12:
3475             case E_JPEG_APP13:
3476             case E_JPEG_APP15:
3477             case E_JPEG_JPG0:
3478             case E_JPEG_JPG1:
3479             case E_JPEG_JPG2:
3480             case E_JPEG_JPG3:
3481             case E_JPEG_JPG4:
3482             case E_JPEG_JPG5:
3483             case E_JPEG_JPG6:
3484             case E_JPEG_JPG7:
3485             case E_JPEG_JPG8:
3486             case E_JPEG_JPG9:
3487             case E_JPEG_JPG10:
3488             case E_JPEG_JPG11:
3489             case E_JPEG_JPG12:
3490             case E_JPEG_JPG13:
3491             case E_JPEG_COM:
3492                 /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */
3493                 {
3494                     if(!JPEG_skip_variable_marker())
3495                     {
3496                         return FALSE;
3497                     }
3498                     break;
3499                 }
3500             default:
3501                 break;
3502         }
3503     }
3504     return TRUE;
3505 }
3506 //------------------------------------------------------------------------------
3507 // Finds the start of image (SOI) marker.
3508 // This code is rather defensive: it only checks the first 512 bytes to avoid
3509 // false positives.
JPEG_locate_soi_marker(void)3510 JPEG_STATIC MS_BOOL JPEG_locate_soi_marker(void)
3511 {
3512     MS_U32 lastchar, thischar;
3513     MS_U32 bytesleft;
3514 
3515     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
3516 
3517     lastchar = JPEG_get_char();
3518 
3519     //JPEG_DEBUG_API_MSG("0x%lx\n", lastchar);
3520     thischar = JPEG_get_char();
3521 
3522     //JPEG_DEBUG_API_MSG("0x%lx\n", thischar);
3523     /* ok if it's a normal JPEG file without a special header */
3524 
3525     if ((0xFF == lastchar)
3526     && (E_JPEG_SOI == thischar))
3527     {
3528         //JPEG_DEBUG_API_MSG("SOI\n");
3529         return TRUE;
3530     }
3531 
3532     // Set this value to 0x1000 for 4k alignment MPO case when parse 2nd/3rd/... JPEG file
3533     bytesleft = 0x1000; //Fix this number from 512 -> 640 for some cases
3534 
3535     for ( ; ; )
3536     {
3537         if ( --bytesleft == 0 )
3538         {
3539             JPEG_terminate( E_JPEG_NOT_JPEG );
3540             return FALSE;
3541         }
3542 
3543         lastchar = thischar;
3544 
3545         thischar = JPEG_get_char();
3546 
3547         //JPEG_DEBUG_API_MSG("%x ", (MS_U8)thischar);
3548         //if(bytesleft%8==0)
3549         //    JPEG_DEBUG_API_MSG("\n");
3550 
3551         if ((0xFF == lastchar)
3552         && (E_JPEG_SOI == thischar))
3553         {
3554             //JPEG_DEBUG_API_MSG("SOI\n");
3555             break;
3556         }
3557     }
3558 
3559     /* Check the next character after marker: if it's not 0xFF, it can't
3560        be the start of the next marker, so it probably isn't a JPEG */
3561 /*  need to check for more detail, currently _u32Bit_buf is not updated during JPEG_get_char()
3562     thischar = ( _u32Bit_buf >> 8 ) & 0xFF;
3563 
3564     if ( thischar != 0xFF )
3565     {
3566         JPEG_terminate( E_JPEG_NOT_JPEG );
3567         return FALSE;
3568     }
3569 */
3570     return TRUE;
3571 }
3572 //------------------------------------------------------------------------------
3573 // Find a start of frame (SOF) marker.
JPEG_locate_sof_marker(void)3574 JPEG_STATIC MS_BOOL JPEG_locate_sof_marker(void)
3575 {
3576     MS_U32 c;
3577     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
3578 
3579     if(!JPEG_locate_soi_marker())
3580         return FALSE;
3581 
3582     c = JPEG_process_markers();
3583 
3584     if( c == FALSE )
3585         return FALSE;
3586 
3587     switch ( c )
3588     {
3589         case E_JPEG_SOF2:
3590             {
3591                 JPEG_DEBUG_API_MSG("Progressive\n");
3592                 _bProgressive_flag = TRUE;
3593                 u32SOFOffset = u32DataOffset + JPEG_GetECS() - 2;
3594                 if(!JPEG_read_sof_marker())
3595                     return FALSE;
3596                 break;
3597             }
3598         case E_JPEG_SOF0:
3599             /* baseline DCT */
3600         case E_JPEG_SOF1:
3601             /* extended sequential DCT */
3602             {
3603                 JPEG_DEBUG_API_MSG("Baseline\n");
3604                 u32SOFOffset = u32DataOffset + JPEG_GetECS() - 2;
3605                 if(!JPEG_read_sof_marker())
3606                     return FALSE;
3607                 break;
3608             }
3609         case E_JPEG_SOF9:
3610             /* Arithmitic coding */
3611             {
3612                 JPEG_terminate( E_JPEG_NO_ARITHMETIC_SUPPORT );
3613                 return FALSE;
3614                 break;
3615             }
3616 
3617         default:
3618             {
3619                 JPEG_DEBUG_API_MSG("parsed unsupported marker = 0x%04lX\n", c);
3620                 JPEG_terminate( E_JPEG_UNSUPPORTED_MARKER );
3621                 return FALSE;
3622                 break;
3623             }
3624     }
3625     return TRUE;
3626 }
3627 //------------------------------------------------------------------------------
3628 // Find a start of scan (SOS) marker.
JPEG_locate_sos_marker(void)3629 JPEG_STATIC MS_BOOL JPEG_locate_sos_marker(void)
3630 {
3631     MS_U32 c;
3632 
3633     c = JPEG_process_markers();
3634 
3635     if ( c == E_JPEG_EOI )
3636     {
3637         return FALSE;
3638     }
3639     else if ( c != E_JPEG_SOS )
3640     {
3641         JPEG_terminate( E_JPEG_UNEXPECTED_MARKER );
3642         return FALSE;
3643     }
3644 
3645     if(!JPEG_read_sos_marker())
3646         return FALSE;
3647 
3648     JPEG_DEBUG_API_MSG(" _pu8In_buf_MPO_ofs=0x%lx, _u32In_buf_MPO_left=0x%lx===========================\n", (MS_U32)_pu8In_buf_ofs, (MS_U32)_u32In_buf_left);
3649     JPEG_DEBUG_API_MSG("%02x %02x %02x %02x %02x %02x %02x %02x \n",
3650     *_pu8In_buf_ofs, *(_pu8In_buf_ofs+1), *(_pu8In_buf_ofs+2), *(_pu8In_buf_ofs+3),
3651     *(_pu8In_buf_ofs+4), *(_pu8In_buf_ofs+5), *(_pu8In_buf_ofs+6), *(_pu8In_buf_ofs+7)
3652     );
3653 
3654     return TRUE;
3655 }
3656 //------------------------------------------------------------------------------
3657 // Reset thumbnail parameters
JPEG_init_thumbnail(void)3658 JPEG_STATIC void JPEG_init_thumbnail(void)
3659 {
3660     _bThumbnailFound = FALSE;
3661     _u32ThumbnailOffset = 0;
3662     _u16ThumbnailSize = 0;
3663     _bTiffBigEndian = FALSE;
3664 
3665     _u32ThumbnailBufferOffset = 0;
3666     _u16ThumbnailSize = 0;
3667     _bThumbnailAccessMode = FALSE;
3668 
3669     _stEXIF_DateTime.bHasDataTime = FALSE;
3670     _stEXIF_DateTime.u32Year = 0;
3671     _stEXIF_DateTime.u32Month = 0;
3672     _stEXIF_DateTime.u32Day = 0;
3673     _stEXIF_DateTime.u32Hour = 0;
3674     _stEXIF_DateTime.u32Minute = 0;
3675     _stEXIF_DateTime.u32Second = 0;
3676     _eEXIF_Orientation = E_JPEG_EXIF_ORIENT_NOT_FOUND;
3677 #if SUPPORT_EXIF_EXTRA_INFO
3678     JPEG_memset((void *)_u8EXIF_Manufacturer, 0, JPEG_MANUFACTURER_SIZE);
3679     JPEG_memset((void *)_u8EXIF_Model, 0, JPEG_MODEL_SIZE);
3680     _u16EXIF_Flash = 0;
3681     _u16EXIF_Exposureprogram = 0;
3682     _u32EXIF_ISOSpeedRatings = 0;
3683     _stEXIF_ShutterSpeedValue.numerator = 0;
3684     _stEXIF_ShutterSpeedValue.denominator= 0;
3685     _stEXIF_ApertureValue.s_numerator= 0;
3686     _stEXIF_ApertureValue.s_denominator= 0;
3687     _stEXIF_ExposureBiasValue.numerator= 0;
3688     _stEXIF_ExposureBiasValue.denominator= 0;
3689     _stEXIF_FocalLength.s_numerator= 0;
3690     _stEXIF_FocalLength.s_denominator= 0;
3691     _u32EXIF_ImageWidth = 0;
3692     _u32EXIF_ImageHeight = 0;
3693     _stEXIF_ExposureTime.numerator = 0;
3694     _stEXIF_ExposureTime.denominator =0;
3695     _stEXIF_FNumber.numerator = 0;
3696     _stEXIF_FNumber.denominator =0;
3697 #endif
3698 }
3699 //------------------------------------------------------------------------------
3700 // Reset everything to default/uninitialized state.
JPEG_init(void)3701 JPEG_STATIC MS_BOOL JPEG_init(void)
3702 {
3703     MS_U16 i;
3704     JPEG_DEBUG_API_MSG("%s!!\n", __FUNCTION__);
3705     _u8LumaCi = 1;
3706     _u8ChromaCi = 2;
3707     _u8Chroma2Ci = 3;
3708     bIs3HuffTbl = FALSE;
3709 
3710     _Error_code = E_JPEG_NO_ERROR;
3711 
3712 #ifdef CMODEL
3713     _bReady_flag = FALSE;
3714 #endif
3715 
3716     _u16Image_x_size = _u16Image_y_size = 0;
3717     _u16OriginalImage_x_size = _u16OriginalImage_y_size = 0;
3718     _u16AlignedImageWidth = _u16AlignedImagePitch = _u16AlignedImageHeight = 0;
3719     _u16AlignedImagePitch_H = 0;
3720 
3721 ////    _pStream = _Pstream;
3722 
3723     _bProgressive_flag = FALSE;
3724 #ifndef CMODEL
3725     _u8DownScaleRatio = E_JPD_DOWNSCALE_ORG;
3726 #endif
3727     _u32RLEOffset = 0;
3728     _bFirstRLE = TRUE;
3729 
3730     _s16dc_pred[0] = _s16dc_pred[1] = _s16dc_pred[2] = 0;
3731 
3732     _JPD_IsDecoding = FALSE; //For H/W bug, some cases can not exit after decode done.
3733     _JPD_ReCheckTime = 0; //For H/W bug, some cases can not exit after decode done, record repeat time.
3734     _JPD_PreVIdx = 0;     //For H/W bug, some cases can not exit after decode done, record previous Vidx.
3735 
3736     _Progressive_ROI_flag = FALSE; //CL82399
3737     ROI_width = 0; //CL82399
3738 
3739     u8PreLHFlag = E_JPEG_BUFFER_NONE;
3740 
3741     u32MRCheckCount = 0;
3742 
3743     for(i = 0; i<JPEG_MAXHUFFTABLES; i++)
3744     {
3745         _Huff_info[i].bValid = FALSE;
3746         JPEG_memset((void *)_Huff_info[i].u8Huff_num, 0, 17);
3747         JPEG_memset((void *)_Huff_info[i].u8Huff_val, 0, 256);
3748         JPEG_memset((void *)_Huff_info[i].u8Symbol, 0, 17);
3749         JPEG_memset((void *)_Huff_info[i].u16Code, 0, 17);
3750     }
3751 
3752     for(i = 0; i<JPEG_MAXQUANTTABLES; i++)
3753     {
3754         _QuantTables[i].bValid = FALSE;
3755         JPEG_memset(_QuantTables[i].s16Value, 0, 64);
3756     }
3757 
3758     gu8Scan_type = E_JPEG_GRAYSCALE;
3759 
3760     _u8Comps_in_frame = 0;
3761 
3762     JPEG_memset((void *)_u8Comp_h_samp, 0, sizeof( _u8Comp_h_samp ) );
3763     JPEG_memset((void *)_u8Comp_v_samp, 0, sizeof( _u8Comp_v_samp ) );
3764     JPEG_memset((void *)_u8Comp_quant, 0, sizeof( _u8Comp_quant ) );
3765     JPEG_memset((void *)_u8Comp_ident, 0, sizeof( _u8Comp_ident ) );
3766     JPEG_memset((void *)_u16Comp_h_blocks, 0, sizeof( _u16Comp_h_blocks ) );
3767     JPEG_memset((void *)_u16Comp_v_blocks, 0, sizeof( _u16Comp_v_blocks ) );
3768 
3769     _u8Comps_in_scan = 0;
3770     JPEG_memset((void *)_u8Comp_list, 0, sizeof( _u8Comp_list ) );
3771     JPEG_memset((void *)_u8Comp_dc_tab, 0, sizeof( _u8Comp_dc_tab ) );
3772     JPEG_memset((void *)_u8Comp_ac_tab, 0, sizeof( _u8Comp_ac_tab ) );
3773 
3774     _u8Spectral_start = 0;
3775     _u8Spectral_end = 0;
3776     _u8Successive_low = 0;
3777     _u8Successive_high = 0;
3778 
3779     gu8Max_mcu_x_size = 0;
3780     gu8Max_mcu_y_size = 0;
3781 
3782     _u8Blocks_per_mcu = 0;
3783     _u32Max_blocks_per_row = 0;
3784     _u16Mcus_per_row = 0;
3785     _u16Mcus_per_col = 0;
3786 
3787     JPEG_memset((void *)_u8Mcu_org, 0, sizeof( _u8Mcu_org ) );
3788 
3789 #ifdef CMODEL
3790     gu16Real_dest_bytes_per_scan_line = 0;
3791     gu16Dest_bytes_per_scan_line = 0;
3792     gu8Dest_bytes_per_pixel = 0;
3793 #endif
3794 
3795     JPEG_memset((void *)_pBlocks, 0, sizeof( _pBlocks ) );
3796 
3797 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
3798     _u16Total_lines_left = 0;
3799 #ifdef CMODEL
3800     gu16Mcu_lines_left = 0;
3801 #endif
3802     JPEG_memset((void *)_u32Block_y_mcu, 0, sizeof( _u32Block_y_mcu ) );
3803     JPEG_memset((void *)_Huff_tbls, 0, sizeof( _Huff_tbls ) );
3804     JPEG_memset((void *)_DC_Coeffs, 0, sizeof( _DC_Coeffs ) );
3805     JPEG_memset((void *)_AC_Coeffs, 0, sizeof( _AC_Coeffs ) );
3806     JPEG_memset((void *)_u32Last_dc_val, 0, sizeof( _u32Last_dc_val ) );
3807 
3808     _u32EOB_run = 0;
3809 #endif
3810 
3811     _pu8In_buf_ofs = _pu8In_buf;
3812 ////    _u32In_buf_left = 0;
3813 ////    _bEOF_flag = FALSE;
3814     _u8Tem_flag = 0;
3815 
3816     //sharon JPEG_memset((void *)_pu8In_buf, 0, sizeof(MS_U8)*(MRC_BUFFER_SIZE + 128) );
3817 
3818     _u16Restart_interval = 0;
3819     _u16Restarts_left = 0;
3820     _u16Next_restart_num = 0;
3821 
3822     gu16Max_mcus_per_row = 0;
3823     _u16Max_blocks_per_mcu = 0;
3824     _u16Max_mcus_per_col = 0;
3825 
3826 #ifdef CMODEL
3827     JPEG_memset((void *)gps16Block_seg, 0, sizeof( gps16Block_seg ) );
3828     gpu8Sample_buf = NULL;
3829 #endif
3830 
3831 #if SW_JPD_RGB_CMYK
3832     JPEG_memset( _ps16Block_seg, 0, sizeof( _ps16Block_seg ) );
3833     gpu8Sample_buf = NULL;
3834 #endif
3835 
3836     // Tell the stream we're going to use it.
3837     //_pStream->attach();
3838 
3839     // Ready the input buffer.
3840     if(_bThumbnailAccessMode)
3841     {
3842         // It means that thumbnail is found and re-call JPEG_init()
3843         // to re-fill thumbnail data to internal buffer.
3844         if(!JPEG_fill_read_buffer())
3845         {
3846             JPEG_DEBUG_API_MSG("JPEG_init : JPEG_fill_read_buffer failed!!!\n");
3847             return FALSE;
3848         }
3849     }
3850     else
3851     {
3852 ////        _u32In_buf_left = MRC_BUFFER_SIZE;  //sharon
3853 ////        _Total_Decoded_Size = MRC_BUFFER_SIZE;  //sharon
3854 ////        _u32Total_bytes_read = MRC_BUFFER_SIZE;  //sharon
3855         _u32Total_bytes_read = _u32In_buf_left;
3856     }
3857 
3858     // Prime the bit buffer.
3859     _s16Bits_left = 0;
3860     _u32Bit_buf = 0;
3861 
3862 //    _pu32ExifHeaderAddr = 0;
3863 
3864 #ifdef CMODEL
3865     for ( i = 0; i < JPEG_MAXBLOCKSPERROW; i++ )
3866     {
3867         gu8Block_max_zag_set[i] = 64;
3868     }
3869 #endif
3870 
3871 #if SW_JPD_RGB_CMYK
3872     for ( i = 0; i < JPEG_MAXBLOCKSPERROW; i++ )
3873     {
3874         _u8Block_max_zag_set[i] = 64;
3875     }
3876 #endif
3877     return TRUE;
3878 }
3879 //------------------------------------------------------------------------------
3880 // The coeff_buf series of methods originally stored the coefficients
3881 // into a "virtual" file which was located in EMS, XMS, or a disk file. A cache
3882 // was used to make this process more efficient. Now, we can store the entire
3883 // thing in RAM.
JPEG_coeff_buf_open(MS_U16 block_num_x,MS_U16 block_num_y,MS_U8 block_len_x,MS_U8 block_len_y)3884 JPEG_STATIC PJPEG_CoeffBuf JPEG_coeff_buf_open(MS_U16 block_num_x, MS_U16 block_num_y, MS_U8 block_len_x, MS_U8 block_len_y)
3885 {
3886     PJPEG_CoeffBuf cb = ( PJPEG_CoeffBuf )JPEG_alloc( sizeof( JPEG_CoeffBuf ) );
3887 
3888     if(cb == NULL)
3889         return NULL;
3890 
3891     cb->u16Block_num_x = block_num_x;
3892     cb->u16Block_num_y = block_num_y;
3893 
3894     cb->u8Block_len_x = block_len_x;
3895     cb->u8Block_len_y = block_len_y;
3896 
3897     cb->u16Block_size = ( block_len_x * block_len_y ) * sizeof( JPEG_BLOCK_TYPE );
3898 
3899     cb->pu8Data = ( MS_U8 * )JPEG_alloc( cb->u16Block_size * block_num_x * block_num_y );
3900 
3901     if(cb->pu8Data == NULL)
3902         return NULL;
3903 
3904     return cb;
3905 }
3906 //------------------------------------------------------------------------------
JPEG_coeff_buf_getp(PJPEG_CoeffBuf cb,MS_U16 block_x,MS_U16 block_y)3907 JPEG_STATIC JPEG_BLOCK_TYPE * JPEG_coeff_buf_getp( PJPEG_CoeffBuf cb, MS_U16 block_x, MS_U16 block_y )
3908 {
3909     if ( block_x >= cb->u16Block_num_x )
3910     {
3911         JPEG_terminate( E_JPEG_ASSERTION_ERROR );
3912         return NULL;
3913     }
3914 
3915     if ( block_y >= cb->u16Block_num_y )
3916     {
3917         JPEG_terminate( E_JPEG_ASSERTION_ERROR );
3918         return NULL;
3919     }
3920 
3921     return ( JPEG_BLOCK_TYPE * )((MS_U32)( cb->pu8Data + block_x * cb->u16Block_size
3922                                             + block_y * (cb->u16Block_size * cb->u16Block_num_x)));
3923 }
3924 //------------------------------------------------------------------------------
3925 // Creates the tables needed for efficient Huffman decoding.
JPEG_make_huff_table(MS_U8 index)3926 JPEG_STATIC MS_BOOL JPEG_make_huff_table(MS_U8 index)
3927 {
3928     MS_U16 p, i, l, si;
3929     MS_U8 huffsize[257];
3930     MS_U16 huffcode[257];
3931     MS_U16 _code;
3932     MS_U16 subtree;
3933     MS_U16 code_size;
3934     MS_U16 lastp;
3935 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
3936     MS_S16 nextfreeentry;
3937 #endif
3938     MS_S16 currententry;
3939 
3940 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
3941     JPEG_HuffTbl *hs = &_Huff_tbls[index];
3942 #endif
3943 
3944     JPEG_memset((void *)huffsize, 0, sizeof(huffsize));
3945     JPEG_memset((void *)huffcode, 0, sizeof(huffcode));
3946 
3947     p = 0;
3948 
3949     for ( l = 1; l <= 16; l++ )
3950     {
3951         for ( i = 1; i <= _Huff_info[index].u8Huff_num[l]; i++ )
3952         {
3953             huffsize[p++] = l;
3954 
3955             //kevinhuang, add protection
3956             if ( p >= 257 )
3957             {
3958                 JPEG_terminate( E_JPEG_UNDEFINED_HUFF_TABLE );
3959                 return FALSE;
3960             }
3961         }
3962     }
3963 
3964     huffsize[p] = 0;
3965 
3966     lastp = p;
3967 
3968     _code = 0;
3969     si = huffsize[0];
3970     p = 0;
3971 
3972     while ( huffsize[p] )
3973     {
3974         while ( huffsize[p] == si )
3975         {
3976             huffcode[p++] = _code;
3977             _code++;
3978 
3979             //kevinhuang, add protection
3980             if ( p >= 257 )
3981             {
3982                 JPEG_terminate( E_JPEG_UNDEFINED_HUFF_TABLE );
3983                 return FALSE;
3984             }
3985         }
3986 
3987         _code <<= 1;
3988         si++;
3989     }
3990 
3991     // Calculate the min code
3992     for(i = 1; i<=16; i++)
3993         _Huff_info[index].u16Code[i] = huffcode[_Huff_info[index].u8Symbol[i]] << (15 - (i - 1));
3994 
3995 
3996 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
3997     // In JPD mode, SW doesn't need huff table when baseline decoding
3998 #if SW_JPD_RGB_CMYK
3999     if((E_JPEG_CMYK != gu8Scan_type)
4000     && (E_JPEG_RGB != gu8Scan_type))
4001 #endif
4002     {
4003 #ifndef CMODEL
4004         if(_bProgressive_flag==FALSE)
4005             return TRUE;
4006 #endif
4007     }
4008 
4009     JPEG_DEBUG_API_MSG("Make HUFF TABLE\n");
4010 
4011     JPEG_memset((void *)(hs->s16Look_up), 0, sizeof( hs->s16Look_up ) );
4012     JPEG_memset((void *)(hs->s16Tree), 0, sizeof( hs->s16Tree ) );
4013     JPEG_memset((void *)(hs->u8Code_size), 0, sizeof( hs->u8Code_size ) );
4014 
4015     nextfreeentry = -1;
4016 
4017     p = 0;
4018 
4019     while ( p < lastp )
4020     {
4021         i = _Huff_info[index].u8Huff_val[p];
4022         _code = huffcode[p];
4023         code_size = huffsize[p];
4024 
4025         hs->u8Code_size[i] = code_size;
4026 
4027         if ( code_size <= 8 )
4028         {
4029             _code <<= ( 8 - code_size );
4030 
4031             for ( l = 1 << ( 8 - code_size ); l > 0; l-- )
4032             {
4033                 hs->s16Look_up[_code] = i;
4034                 _code++;
4035             }
4036         }
4037         else
4038         {
4039             subtree = ( _code >> ( code_size - 8 ) ) & 0xFF;
4040 
4041             currententry = hs->s16Look_up[subtree];
4042 
4043             if ( currententry == 0 )
4044             {
4045                 hs->s16Look_up[subtree] = currententry = nextfreeentry;
4046 
4047                 nextfreeentry -= 2;
4048             }
4049 
4050             _code <<= ( 16 - ( code_size - 8 ) );
4051 
4052             for ( l = code_size; l > 9; l-- )
4053             {
4054                 if ( ( _code & 0x8000 ) == 0 )
4055                 {
4056                     currententry--;
4057                 }
4058 
4059                 if ( hs->s16Tree[-currententry - 1] == 0 )
4060                 {
4061                     hs->s16Tree[-currententry - 1] = nextfreeentry;
4062 
4063                     currententry = nextfreeentry;
4064 
4065                     nextfreeentry -= 2;
4066                 }
4067                 else
4068                 {
4069                     currententry = hs->s16Tree[-currententry - 1];
4070                 }
4071 
4072                 _code <<= 1;
4073             }
4074 
4075             if ( ( _code & 0x8000 ) == 0 )
4076             {
4077                 currententry--;
4078             }
4079 
4080             hs->s16Tree[-currententry - 1] = i;
4081         }
4082 
4083         p++;
4084     }
4085 #endif  //SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
4086     return TRUE;
4087 }
4088 //------------------------------------------------------------------------------
4089 // Verifies the quantization tables needed for this scan are available.
JPEG_check_quant_tables(void)4090 JPEG_STATIC MS_BOOL JPEG_check_quant_tables( void ) //ok
4091 {
4092     MS_U8 i;
4093 
4094     for ( i = 0; i < _u8Comps_in_scan; i++ )
4095     {
4096         if ( _QuantTables[_u8Comp_quant[_u8Comp_list[i]]].bValid==FALSE )
4097         {
4098             JPEG_terminate( E_JPEG_UNDEFINED_QUANT_TABLE );
4099             return FALSE;
4100         }
4101     }
4102     return TRUE;
4103 }
4104 //------------------------------------------------------------------------------
4105 // Verifies that all the Huffman tables needed for this scan are available.
JPEG_check_huff_tables(void)4106 JPEG_STATIC MS_BOOL JPEG_check_huff_tables( void )
4107 {
4108     MS_U8 i;
4109 
4110     for ( i = 0; i < _u8Comps_in_scan; i++ )
4111     {
4112         if ( ( _u8Spectral_start == 0 ) && ( _Huff_info[_u8Comp_dc_tab[_u8Comp_list[i]]].bValid== FALSE ) )
4113         {
4114             JPEG_terminate( E_JPEG_UNDEFINED_HUFF_TABLE );
4115             return FALSE;
4116         }
4117 
4118         if ( ( _u8Spectral_end > 0 ) && ( _Huff_info[_u8Comp_ac_tab[_u8Comp_list[i]]].bValid== FALSE ) )
4119         {
4120             JPEG_terminate( E_JPEG_UNDEFINED_HUFF_TABLE );
4121             return FALSE;
4122         }
4123     }
4124 
4125     for ( i = 0; i < JPEG_MAXHUFFTABLES; i++ )
4126     {
4127         if ( _Huff_info[i].bValid )
4128         {
4129             if(!JPEG_make_huff_table(i))
4130                 return FALSE;
4131         }
4132     }
4133     return TRUE;
4134 }
4135 //------------------------------------------------------------------------------
4136 // Determines the component order inside each MCU.
4137 // Also calcs how many MCU's are on each row, etc.
JPEG_calc_mcu_block_order(void)4138 JPEG_STATIC void JPEG_calc_mcu_block_order( void ) //ok
4139 {
4140     MS_U8 component_num, component_id;
4141     MS_U8 max_h_samp = 0, max_v_samp = 0;
4142 
4143     for ( component_id = 0; component_id < _u8Comps_in_frame; component_id++ )
4144     {
4145         if ( _u8Comp_h_samp[component_id] > max_h_samp )
4146         {
4147             max_h_samp = _u8Comp_h_samp[component_id];
4148         }
4149 
4150         if ( _u8Comp_v_samp[component_id] > max_v_samp )
4151         {
4152             max_v_samp = _u8Comp_v_samp[component_id];
4153         }
4154     }
4155 
4156     if((max_h_samp == 0) || (max_v_samp == 0))
4157     {
4158         JPEG_terminate( E_JPEG_NOT_ENOUGH_HEADER_INFO );
4159         return;
4160     }
4161 
4162     for ( component_id = 0; component_id < _u8Comps_in_frame; component_id++ )
4163     {
4164         _u16Comp_h_blocks[component_id] = ( ( ( ( _u16Image_x_size * _u8Comp_h_samp[component_id] ) + ( max_h_samp - 1 ) ) / max_h_samp ) + 7 ) / 8;
4165         _u16Comp_v_blocks[component_id] = ( ( ( ( _u16Image_y_size * _u8Comp_v_samp[component_id] ) + ( max_v_samp - 1 ) ) / max_v_samp ) + 7 ) / 8;
4166     }
4167 
4168     if ( _u8Comps_in_scan == 1 )
4169     {
4170         _u16Mcus_per_row = _u16Comp_h_blocks[_u8Comp_list[0]];
4171         _u16Mcus_per_col = _u16Comp_v_blocks[_u8Comp_list[0]];
4172     }
4173     else
4174     {
4175         _u16Mcus_per_row = ( ( ( _u16Image_x_size + 7 ) / 8 ) + ( max_h_samp - 1 ) ) / max_h_samp;
4176         _u16Mcus_per_col = ( ( ( _u16Image_y_size + 7 ) / 8 ) + ( max_v_samp - 1 ) ) / max_v_samp;
4177     }
4178 
4179     if ( _u8Comps_in_scan == 1 )
4180     {
4181         _u8Mcu_org[0] = _u8Comp_list[0];
4182 
4183         _u8Blocks_per_mcu = 1;
4184     }
4185     else
4186     {
4187         _u8Blocks_per_mcu = 0;
4188 
4189         for ( component_num = 0; component_num < _u8Comps_in_scan; component_num++ )
4190         {
4191             MS_U8 num_blocks;
4192 
4193             component_id = _u8Comp_list[component_num];
4194 
4195             num_blocks = _u8Comp_h_samp[component_id] * _u8Comp_v_samp[component_id];
4196 
4197             while ( num_blocks-- )
4198             {
4199                 _u8Mcu_org[_u8Blocks_per_mcu++] = component_id;
4200             }
4201         }
4202     }
4203 }
4204 //------------------------------------------------------------------------------
4205 /* Get current access byte address in MRC buffer relative to MRC start address */
JPEG_GetECS(void)4206 JPEG_STATIC MS_U32 JPEG_GetECS(void)
4207 {
4208     MS_U32 data_end_addr = (MS_U32)_pu8In_buf_ofs;
4209     MS_U32 data_start_addr =(MS_U32) _pu8In_buf;
4210 
4211     return (data_end_addr - data_start_addr);
4212 }
4213 //------------------------------------------------------------------------------
4214 //*************************************************
4215 //write symbol table
4216 //*************************************************
4217 #ifndef CMODEL
4218 #if 0
4219 JPEG_STATIC void JPEG_write_symidx(void)
4220 {
4221     MS_U16 i, tbl_num_luma, tbl_num_chroma;
4222     MS_U8 ci, luma_ci = 0, chroma_ci = 0;
4223 
4224     MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_SYMIDX_BASE);
4225     if (_HeadCheck.DHT)
4226     {
4227         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4228         {
4229             if(_u8LumaCi==_u8Comp_ident[ci])
4230             {
4231                 luma_ci = ci;
4232                 break;
4233             }
4234         }
4235 
4236         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4237         {
4238             if(_u8ChromaCi==_u8Comp_ident[ci])
4239             {
4240                 chroma_ci = ci;
4241                 break;
4242             }
4243         }
4244 
4245         tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4246         tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4247 
4248         for ( i = 0; i < 256; i++ )
4249         {
4250             MDrv_Write2Byte( BK_JPD_TID_DAT, ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ) );
4251         }
4252 
4253         tbl_num_luma = _u8Comp_dc_tab[luma_ci];
4254         tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
4255 
4256         for ( i = 0; i < 16; i++ )
4257         {
4258             MDrv_Write2Byte( BK_JPD_TID_DAT, ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ) );
4259         }
4260     }
4261     else
4262     {
4263         for(i=0;i<272;i++)
4264             MDrv_Write2Byte( BK_JPD_TID_DAT, g16SYMIDX_TBL[i]);
4265     }
4266 }
4267 #endif
4268 //------------------------------------------------------------------------------
JPEG_WriteSymidx(void)4269 JPEG_STATIC void JPEG_WriteSymidx(void)
4270 {
4271     //MS_U16 i, tbl_num_luma, tbl_num_chroma;
4272     MS_U16 ac_tbl_num_luma = 0, ac_tbl_num_chroma = 0;
4273     MS_U16 dc_tbl_num_luma = 0, dc_tbl_num_chroma = 0;
4274     MS_U8 ci, luma_ci = 0, chroma_ci = 0;
4275 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4276     MS_U16 ac_tbl_num_chroma2 = 0;
4277     MS_U16 dc_tbl_num_chroma2 = 0;
4278     MS_U8 chroma2_ci = 0;
4279 #endif
4280     JPD_Symidx structSymidx;
4281 
4282     JPEG_memset((void *)(&structSymidx), 0, sizeof(structSymidx));
4283 
4284     // Moved to MDrv_JPD_WriteSymidx()
4285     //MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_SYMIDX_BASE);
4286 
4287     if (_HeadCheck.DHT)
4288     {
4289         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4290         {
4291             if(_u8LumaCi==_u8Comp_ident[ci])
4292             {
4293                 luma_ci = ci;
4294                 break;
4295             }
4296         }
4297 
4298         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4299         {
4300             if(_u8ChromaCi==_u8Comp_ident[ci])
4301             {
4302                 chroma_ci = ci;
4303                 break;
4304             }
4305         }
4306 
4307 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4308         if(TRUE == bIs3HuffTbl)
4309         {
4310             for(ci = 0; ci<_u8Comps_in_frame; ci++)
4311             {
4312                 if(_u8Chroma2Ci==_u8Comp_ident[ci])
4313                 {
4314                     chroma2_ci = ci;
4315                     break;
4316                 }
4317             }
4318         }
4319 #endif
4320 
4321         //tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4322         ac_tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4323         //tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4324         ac_tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4325 
4326 // Moved to MDrv_JPD_WriteSymidx()
4327 #if 0
4328         for ( i = 0; i < 256; i++ )
4329         {
4330             MDrv_Write2Byte( BK_JPD_TID_DAT, ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ) );
4331         }
4332 #endif
4333 
4334         dc_tbl_num_luma = _u8Comp_dc_tab[luma_ci];
4335         dc_tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
4336 #if 0
4337         for ( i = 0; i < 16; i++ )
4338         {
4339             MDrv_Write2Byte( BK_JPD_TID_DAT, ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ) );
4340         }
4341 #endif
4342 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4343         if(TRUE == bIs3HuffTbl)
4344         {
4345             ac_tbl_num_chroma2 = _u8Comp_ac_tab[chroma2_ci];
4346             dc_tbl_num_chroma2 = _u8Comp_dc_tab[chroma2_ci];
4347         }
4348 #endif
4349     }
4350     else
4351     {
4352 // Moved to MDrv_JPD_WriteSymidx()
4353 #if 0
4354         for(i=0;i<272;i++)
4355             MDrv_Write2Byte( BK_JPD_TID_DAT, g16SYMIDX_TBL[i]);
4356 #endif
4357     }
4358 
4359     structSymidx.DHT = _HeadCheck.DHT;
4360     structSymidx.bUVHuffman = bIs3HuffTbl;
4361     structSymidx.u8DcLumaHuffVal = _Huff_info[dc_tbl_num_luma].u8Huff_val;
4362     structSymidx.u8DcChromaHuffVal = _Huff_info[dc_tbl_num_chroma].u8Huff_val;
4363     structSymidx.u8AcLumaHuffVal = _Huff_info[ac_tbl_num_luma].u8Huff_val;
4364     structSymidx.u8AcChromaHuffVal = _Huff_info[ac_tbl_num_chroma].u8Huff_val;
4365 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4366     if(TRUE == bIs3HuffTbl)
4367     {
4368         structSymidx.u8DcChroma2HuffVal = _Huff_info[dc_tbl_num_chroma2].u8Huff_val;
4369         structSymidx.u8AcChroma2HuffVal = _Huff_info[ac_tbl_num_chroma2].u8Huff_val;
4370     }
4371 #endif
4372     MDrv_JPD_WriteSymidx(structSymidx);
4373 }
4374 //------------------------------------------------------------------------------
4375 //***************************************************
4376 //write quantization table
4377 //***************************************************
4378 #if 0
4379 JPEG_STATIC void JPEG_write_Qtbl(void)
4380 {
4381     MS_U8 i, j;
4382     MS_U8 com_num = 0;
4383     MS_U8 comp[JPEG_MAXCOMPONENTS];
4384 
4385     MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_QTBL_BASE);
4386 
4387     if (_HeadCheck.DQT)
4388     {
4389         // Calculate how many valid quantization tables
4390         JPEG_memset((void *)comp, 0, JPEG_MAXCOMPONENTS);
4391         for(i = 0; i<_u8Comps_in_frame; i++)
4392         {
4393             comp[_u8Comp_quant[i]] = 1;
4394         }
4395 
4396         for(i = 0; i<JPEG_MAXCOMPONENTS; i++)
4397         {
4398             if(comp[i]==1)
4399                 com_num++;
4400         }
4401 
4402         for ( i = 0; i < com_num; i++ )
4403         {
4404             for(j = 0; j<64; j++)
4405             {
4406                 MDrv_Write2Byte(BK_JPD_TID_DAT, _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
4407             }
4408         }
4409 
4410         // if all compoents refer to the same Qtable, need to write Qtable twice
4411         if(com_num==1)
4412         {
4413             for ( i = 0; i < com_num; i++ )
4414             {
4415                 for(j = 0; j<64; j++)
4416                 {
4417                     MDrv_Write2Byte(BK_JPD_TID_DAT, _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
4418                 }
4419             }
4420         }
4421     }
4422     else
4423     {
4424         for (i=0; i<128; i++)
4425             MDrv_Write2Byte(BK_JPD_TID_DAT, g16IQ_TBL[i]);
4426     }
4427 }
4428 #endif
JPEG_WriteIQTbl(void)4429 JPEG_STATIC void JPEG_WriteIQTbl(void)
4430 {
4431     //MS_U8 i, j;
4432     MS_U8 i;
4433     MS_U8 com_num = 0;
4434     MS_U8 comp[JPEG_MAXCOMPONENTS];
4435     JPD_IQTbl structIqtbl;
4436 
4437     JPEG_memset((void *)&structIqtbl, 0, sizeof(structIqtbl));
4438 
4439     // Moved to MDrv_JPD_WriteIQTbl()
4440     //MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_QTBL_BASE);
4441 
4442     if (_HeadCheck.DQT)
4443     {
4444         // Calculate how many valid quantization tables
4445         JPEG_memset((void *)comp, 0, JPEG_MAXCOMPONENTS);
4446         for(i = 0; i<_u8Comps_in_frame; i++)
4447         {
4448             comp[_u8Comp_quant[i]] = 1;
4449         }
4450 
4451         for(i = 0; i<JPEG_MAXCOMPONENTS; i++)
4452         {
4453             if(comp[i]==1)
4454                 com_num++;
4455         }
4456 
4457         // Moved to MDrv_JPD_WriteIQTbl
4458 #if 0
4459         for ( i = 0; i < com_num; i++ )
4460         {
4461             for(j = 0; j<64; j++)
4462             {
4463                 MDrv_Write2Byte(BK_JPD_TID_DAT, _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
4464             }
4465         }
4466 
4467         // if all compoents refer to the same Qtable, need to write Qtable twice
4468         if(com_num==1)
4469         {
4470             for ( i = 0; i < com_num; i++ )
4471             {
4472                 for(j = 0; j<64; j++)
4473                 {
4474                     MDrv_Write2Byte(BK_JPD_TID_DAT, _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
4475                 }
4476             }
4477         }
4478 #endif
4479     }
4480     else
4481     {
4482         // Moved to MDrv_JPD_WriteIQTbl()
4483 #if 0
4484         for (i=0; i<128; i++)
4485             MDrv_Write2Byte(BK_JPD_TID_DAT, g16IQ_TBL[i]);
4486 #endif
4487     }
4488 
4489     structIqtbl.DQT = _HeadCheck.DQT;
4490     structIqtbl.u8CompNum = com_num;
4491     structIqtbl.u8CompQuant = _u8Comp_quant;
4492     structIqtbl.QuantTables = (JPD_QuanTbl *)_QuantTables;
4493     MDrv_JPD_WriteIQTbl(structIqtbl);
4494 }
4495 //------------------------------------------------------------------------------
4496 //*************************************************
4497 //write group information
4498 //*************************************************
4499 #if 0
4500 JPEG_STATIC void JPEG_write_Scwgif(void) //type : luma=>0  chroma=>1
4501 {
4502     MS_U32 reg_value;
4503     MS_U16 i, ci, valid, tbl_num_luma, tbl_num_chroma;
4504     MS_U8 luma_ci = 0, chroma_ci = 0;
4505 
4506     MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_SCWGIF_BASE);
4507     if (_HeadCheck.DHT)
4508     {
4509         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4510         {
4511             if(_u8LumaCi==_u8Comp_ident[ci])
4512             {
4513                 luma_ci = ci;
4514                 break;
4515             }
4516         }
4517 
4518         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4519         {
4520             if(_u8ChromaCi==_u8Comp_ident[ci])
4521             {
4522                 chroma_ci = ci;
4523                 break;
4524             }
4525         }
4526 
4527         tbl_num_luma = _u8Comp_dc_tab[luma_ci];
4528         tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
4529 
4530         for ( i = 1; i <= 16; i++ )
4531         {
4532             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
4533                 valid = 0;
4534             else
4535                 valid = 1;
4536 
4537             if ( valid )
4538             {
4539                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] << 4 );
4540             }
4541             else
4542             {
4543                 reg_value = 0;
4544             }
4545 
4546             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4547             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4548         }
4549 
4550         for ( i = 1; i <= 16; i++ )
4551         {
4552             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
4553                 valid = 0;
4554             else
4555                 valid = 1;
4556 
4557             if ( valid )
4558             {
4559                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] << 4 );
4560             }
4561             else
4562             {
4563                 reg_value = 0;
4564             }
4565 
4566             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4567             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4568         }
4569 
4570         tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4571         tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4572 
4573         for ( i = 1; i <= 16; i++ )
4574         {
4575             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
4576                 valid = 0;
4577             else
4578                 valid = 1;
4579 
4580             if ( valid )
4581             {
4582                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] );
4583             }
4584             else
4585             {
4586                 reg_value = 0;
4587             }
4588 
4589             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4590             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4591         }
4592 
4593         for ( i = 1; i <= 16; i++ )
4594         {
4595             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
4596                 valid = 0;
4597             else
4598                 valid = 1;
4599 
4600             if ( valid )
4601             {
4602                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] );
4603             }
4604             else
4605             {
4606                 reg_value = 0;
4607             }
4608 
4609             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4610             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4611         }
4612     }
4613     else
4614     {
4615         for( i = 0; i < 128; i++ )
4616             MDrv_Write2Byte(BK_JPD_TID_DAT, g16GRPINFO_TBL[i]);
4617     }
4618 }
4619 #endif
JPEG_WriteGrpinf(void)4620 JPEG_STATIC void JPEG_WriteGrpinf(void) //type : luma=>0  chroma=>1
4621 {
4622     //MS_U32 reg_value;
4623     //MS_U16 i, ci, valid, tbl_num_luma, tbl_num_chroma;
4624     MS_U16 ci, dc_tbl_num_luma = 0, dc_tbl_num_chroma = 0;
4625     MS_U16 ac_tbl_num_luma = 0, ac_tbl_num_chroma = 0;
4626     MS_U8 luma_ci = 0, chroma_ci = 0;
4627 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4628     MS_U16 dc_tbl_num_chroma2 = 0;
4629     MS_U16 ac_tbl_num_chroma2 = 0;
4630     MS_U8 chroma2_ci = 0;
4631 #endif
4632     JPD_Grpinf structGrpinf;
4633 
4634     JPEG_memset((void *)&structGrpinf, 0, sizeof(structGrpinf));
4635 
4636 // Moved to MDrv_JPD_WriteGrpinf()
4637 #if 0
4638     //MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_SCWGIF_BASE);
4639 #endif
4640     if (_HeadCheck.DHT)
4641     {
4642         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4643         {
4644             if(_u8LumaCi==_u8Comp_ident[ci])
4645             {
4646                 luma_ci = ci;
4647                 break;
4648             }
4649         }
4650 
4651         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4652         {
4653             if(_u8ChromaCi==_u8Comp_ident[ci])
4654             {
4655                 chroma_ci = ci;
4656                 break;
4657             }
4658         }
4659 
4660 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4661         if(TRUE == bIs3HuffTbl)
4662         {
4663             for(ci = 0; ci<_u8Comps_in_frame; ci++)
4664             {
4665                 if(_u8Chroma2Ci==_u8Comp_ident[ci])
4666                 {
4667                     chroma2_ci = ci;
4668                     break;
4669                 }
4670             }
4671         }
4672 #endif
4673 
4674         dc_tbl_num_luma = _u8Comp_dc_tab[luma_ci];
4675         dc_tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
4676 
4677 // Moved to MDrv_JPD_WriteGrpinf()
4678 #if 0
4679         for ( i = 1; i <= 16; i++ )
4680         {
4681             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
4682                 valid = 0;
4683             else
4684                 valid = 1;
4685 
4686             if ( valid )
4687             {
4688                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] << 4 );
4689             }
4690             else
4691             {
4692                 reg_value = 0;
4693             }
4694 
4695             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4696             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4697         }
4698 
4699         for ( i = 1; i <= 16; i++ )
4700         {
4701             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
4702                 valid = 0;
4703             else
4704                 valid = 1;
4705 
4706             if ( valid )
4707             {
4708                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] << 4 );
4709             }
4710             else
4711             {
4712                 reg_value = 0;
4713             }
4714 
4715             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4716             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4717         }
4718 #endif
4719 
4720         ac_tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4721         ac_tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4722 // Moved to MDrv_JPD_WriteGrpinf()
4723 #if 0
4724         for ( i = 1; i <= 16; i++ )
4725         {
4726             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
4727                 valid = 0;
4728             else
4729                 valid = 1;
4730 
4731             if ( valid )
4732             {
4733                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] );
4734             }
4735             else
4736             {
4737                 reg_value = 0;
4738             }
4739 
4740             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4741             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4742         }
4743 
4744         for ( i = 1; i <= 16; i++ )
4745         {
4746             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
4747                 valid = 0;
4748             else
4749                 valid = 1;
4750 
4751             if ( valid )
4752             {
4753                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] );
4754             }
4755             else
4756             {
4757                 reg_value = 0;
4758             }
4759 
4760             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4761             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4762         }
4763 #endif
4764 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4765         if(TRUE == bIs3HuffTbl)
4766         {
4767             dc_tbl_num_chroma2 = _u8Comp_dc_tab[chroma2_ci];
4768             ac_tbl_num_chroma2 = _u8Comp_ac_tab[chroma2_ci];
4769         }
4770 #endif
4771     }
4772     else
4773     {
4774 #if 0
4775         for( i = 0; i < 128; i++ )
4776             MDrv_Write2Byte(BK_JPD_TID_DAT, g16GRPINFO_TBL[i]);
4777 #endif
4778     }
4779 
4780     structGrpinf.DHT = _HeadCheck.DHT;
4781     structGrpinf.bUVHuffman = bIs3HuffTbl;
4782     structGrpinf.u8DcLumaSymbol = _Huff_info[dc_tbl_num_luma].u8Symbol;
4783     structGrpinf.u16DcLumaCode = _Huff_info[dc_tbl_num_luma].u16Code;
4784     structGrpinf.u8DcChromaSymbol = _Huff_info[dc_tbl_num_chroma].u8Symbol;
4785     structGrpinf.u16DcChromaCode = _Huff_info[dc_tbl_num_chroma].u16Code;
4786     structGrpinf.u8AcLumaSymbol = _Huff_info[ac_tbl_num_luma].u8Symbol;
4787     structGrpinf.u16AcLumaCode = _Huff_info[ac_tbl_num_luma].u16Code;
4788     structGrpinf.u8AcChromaSymbol = _Huff_info[ac_tbl_num_chroma].u8Symbol;
4789     structGrpinf.u16AcChromaCode = _Huff_info[ac_tbl_num_chroma].u16Code;
4790 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4791     if(TRUE == bIs3HuffTbl)
4792     {
4793         structGrpinf.u8DcChroma2Symbol = _Huff_info[dc_tbl_num_chroma2].u8Symbol;
4794         structGrpinf.u16DcChroma2Code = _Huff_info[dc_tbl_num_chroma2].u16Code;
4795         structGrpinf.u8AcChroma2Symbol = _Huff_info[ac_tbl_num_chroma2].u8Symbol;
4796         structGrpinf.u16AcChroma2Code = _Huff_info[ac_tbl_num_chroma2].u16Code;
4797     }
4798 #endif
4799     MDrv_JPD_WriteGrpinf(structGrpinf);
4800 }
4801 #endif /* #ifndef CMODEL */
4802 //------------------------------------------------------------------------------
4803 // Write RLE result
JPEG_write_RLE(JPEG_SVLD * pVld,MS_BOOL bDecodeNow)4804 JPEG_STATIC MS_BOOL JPEG_write_RLE(JPEG_SVLD *pVld, MS_BOOL bDecodeNow)
4805 {
4806 #ifdef CMODEL
4807     JPEG_DEBUG_API_MSG("%02x%02x%02x%02x\n",(MS_U8)pVld->byte3, (MS_U8)pVld->byte2, (MS_U8)pVld->byte1, (MS_U8)pVld->byte0);
4808 #else
4809     MS_U8 *mrc_buffer = (MS_U8 *) MRC_BUFFER_ADDR;
4810     MS_U16 status;
4811     MS_U32 start_time;
4812     MS_U16 cur_vIdx;
4813 
4814     JPEG_memcpy((void *)(mrc_buffer + _u32RLEOffset), (void *)pVld, 4);
4815     _u32RLEOffset += 4;
4816 
4817     // Check if buffer full
4818     if((MRC_BUFFER_SIZE == _u32RLEOffset)
4819     || (TRUE == bDecodeNow))
4820     {
4821         JPEG_DEBUG_API_MSG("Do RLE, LENG 0x%lx, bDecodeNow = %d\n", _u32RLEOffset, bDecodeNow);
4822         JPEG_DEBUG_API_MSG("CPU Sync and Flush Memory~~~~\n");
4823         MAsm_CPU_Sync();
4824         MsOS_FlushMemory();
4825 
4826         if(_bFirstRLE == TRUE)
4827         {
4828             // Trigger JPD decoding
4829             if(!JPEG_StartDecode())
4830                 return FALSE;
4831             _bFirstRLE = FALSE;
4832         }
4833         else
4834         {
4835             // clear MRC low/high portion read complete event
4836             //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBH_DONE | E_JPD_EVENT_MRBL_DONE);
4837             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE | E_JPD_EVENT_MRBL_DONE);
4838             // mark low/high buffer valid
4839             //MDrv_Write2Byte( BK_JPD_MCONFIG, (MDrv_Read2Byte(BK_JPD_MCONFIG) & ~0x0003) | (JPD_H_VLD |JPD_L_VLD));
4840             MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | (JPD_H_VLD |JPD_L_VLD));
4841         }
4842 
4843 #if MSOS_GET_SYSTEM_TIME
4844         start_time = MsOS_GetSystemTime();
4845 #else
4846         start_time = 0;
4847 #endif
4848         //cur_vIdx = MDrv_Read2Byte(BK_JPD_CUR_VIDX);
4849         cur_vIdx = MDrv_JPD_GetCurVidx();
4850 
4851         JPEG_DEBUG_API_MSG("cur_vIdx = 0x%04X\n",cur_vIdx);
4852 
4853         if( bDecodeNow )
4854         {
4855             return TRUE;  //wait done in main loop
4856         }
4857 
4858         while(1)
4859         {
4860             //status = MDrv_JPD_ReadJPDStatus();
4861             status = MDrv_JPD_GetEventFlag();
4862 
4863             if(status & E_JPD_EVENT_DEC_DONE)
4864             {
4865                 JPEG_DEBUG_API_MSG("P deocde done\n");
4866                 break;
4867             }
4868 
4869             if((status & E_JPD_EVENT_ECS_ERROR) || (status & E_JPD_EVENT_IS_ERROR) || (status & E_JPD_EVENT_RST_ERROR)
4870 #if (JPD_SUPPORT_AUTO_PROTECT==true)
4871                 || (status & E_JPD_EVENT_MWB_FULL)
4872 #endif
4873                 )
4874             {
4875                 // temp patch for protect JPD from writing to illegal memory
4876                 JPEG_DEBUG_API_MSG("CurVidx = %d, CurRow = %d, CurCol = %d ",
4877                         MDrv_JPD_GetCurVidx(),
4878                         MDrv_JPD_GetCurRow(),
4879                         MDrv_JPD_GetCurCol());
4880                 JPEG_DEBUG_API_MSG("CurMRCAddr = 0x%lx\n", MDrv_JPD_GetCurMRCAddr());
4881                 //MDrv_JPD_SW_Pause_Reset();
4882                 MDrv_JPD_Rst();
4883 
4884                 JPEG_terminate( E_JPEG_JPD_DECODE_ERROR );
4885                 return FALSE;
4886             }
4887 
4888             if((status & E_JPD_EVENT_MRBH_DONE) && (status & E_JPD_EVENT_MRBL_DONE))
4889             {
4890                 JPEG_DEBUG_API_MSG("Partial SVLD decode done\n");
4891                 break;
4892             }
4893 
4894             // Check the V index. If it is not changed withing 500ms, it means that the JPD has some problem.
4895             // We need to break the infinite loop
4896             //if(cur_vIdx!=MDrv_Read2Byte(BK_JPD_CUR_VIDX))
4897             if(cur_vIdx != MDrv_JPD_GetCurVidx())
4898             {
4899 #if MSOS_GET_SYSTEM_TIME
4900                 start_time = MsOS_GetSystemTime();
4901 #else
4902                 start_time = 0;
4903 #endif
4904                 //cur_vIdx = MDrv_Read2Byte(BK_JPD_CUR_VIDX);
4905                 cur_vIdx = MDrv_JPD_GetCurVidx();
4906             }
4907             else
4908             {
4909 #if MSOS_GET_SYSTEM_TIME
4910                 if((MsOS_GetSystemTime() - start_time) >= DEFAULT_DECODE_TIMEOUT)
4911 #else
4912                 if(start_time++ >= DEFAULT_DECODE_TIMEOUT * 100)
4913 #endif
4914                 {
4915                     JPEG_DEBUG_API_ERR("ERROR: SVLD deocde time out, VIdx %d\n", cur_vIdx);
4916                     return FALSE;
4917                 }
4918             }
4919         }
4920 
4921         _u32RLEOffset = 0;
4922     }
4923 #endif
4924     return TRUE;
4925 }
4926 //------------------------------------------------------------------------------
4927 // Do run length encode of coefficient buffer
4928 //JPEG_STATIC void JPEG_do_RLE(JPEG_BLOCK_TYPE *p, MS_BOOL eop, MS_U8 comp_id)
JPEG_do_RLE(JPEG_BLOCK_TYPE * p,MS_BOOL eop,MS_U8 comp_id,MS_BOOL BlockInRange)4929 JPEG_STATIC MS_BOOL JPEG_do_RLE(JPEG_BLOCK_TYPE *p, MS_BOOL eop, MS_U8 comp_id, MS_BOOL BlockInRange)
4930 {
4931     JPEG_SVLD my_vld;
4932     MS_U8 counter;
4933     MS_S16 value;
4934     MS_U16 run;
4935     MS_U8 cur_blk;
4936     JPEG_BLOCK_TYPE predictor;
4937 
4938     if(comp_id==0)
4939         cur_blk = 1;     // Y
4940     else if(comp_id==1)
4941         cur_blk = 3;    // U
4942     else
4943         cur_blk = 2;    // V
4944 
4945     predictor = _s16dc_pred[cur_blk - 1];
4946 
4947     run = 0;
4948     my_vld.byte0 = my_vld.byte1 = my_vld.byte2 = my_vld.byte3 = 0;
4949     my_vld.blk_type = cur_blk;
4950 
4951     //sent DC info
4952 ////    my_vld.run = 8;
4953     if( BlockInRange )//Current block is within display range.
4954         my_vld.run = 8;
4955     else
4956         my_vld.run = 0;
4957 
4958     value = (p[0] - predictor);
4959     my_vld.sign = (value<0)?1:0;
4960     my_vld.amp = JPEG_ABS(value);
4961     my_vld.sym_type = E_RLE_DC;
4962     if(!JPEG_write_RLE(&my_vld, FALSE))
4963         return FALSE;
4964 
4965     if( BlockInRange == FALSE )//Current block is not within display range.
4966         return TRUE;
4967 
4968     my_vld.byte0 = my_vld.byte1 =my_vld.byte2 = my_vld.byte3= 0;
4969     my_vld.blk_type = cur_blk;
4970 
4971     for(counter = 1;counter<64; counter++)
4972     {
4973         if(p[counter]==0)
4974         {
4975             run++;
4976         }
4977         else
4978         {
4979             while(run>15)
4980             {
4981                 my_vld.sign = 0;
4982                 my_vld.amp = 0;
4983                 my_vld.sym_type = E_RLE_ZRL;
4984                 my_vld.run = 15;
4985                 if(!JPEG_write_RLE(&my_vld, FALSE))
4986                     return FALSE;
4987                 my_vld.byte0 = my_vld.byte1 = my_vld.byte2 = my_vld.byte3 = 0;
4988                 my_vld.blk_type = cur_blk;
4989                 run -= 16;
4990             }
4991 
4992             my_vld.sign = (p[counter]<0)?1:0;
4993             my_vld.amp = JPEG_ABS(p[counter]);
4994             my_vld.sym_type = E_RLE_AC;
4995             my_vld.run = run;
4996 
4997             // Check if the last byte is non-zero. If it's non-zero & EOP, add the EOP flag
4998             if(counter==63&&eop&&p[63]!=0)
4999             {
5000                 my_vld.EOP = 1;
5001                 if(!JPEG_write_RLE(&my_vld, TRUE))
5002                     return FALSE;
5003 
5004                 _s16dc_pred[cur_blk - 1] = p[0];//update predictor
5005                 return TRUE;
5006             }
5007             else
5008             {
5009                 if(!JPEG_write_RLE(&my_vld, FALSE))
5010                     return FALSE;
5011             }
5012 
5013             my_vld.byte0 = my_vld.byte1 = my_vld.byte2 = my_vld.byte3 = 0;
5014             my_vld.blk_type = cur_blk;
5015             run = 0;
5016         }
5017     }
5018 
5019     counter = 63;
5020 
5021     if(p[counter]==0)
5022     {
5023         my_vld.amp = JPEG_ABS(p[counter]);
5024         my_vld.sign = p[counter]<0?1:0;
5025         my_vld.sym_type = E_RLE_EOB;
5026         my_vld.run = 0;
5027         if(eop)
5028         {
5029             my_vld.EOP = 1;
5030             if(!JPEG_write_RLE(&my_vld, TRUE))
5031                 return FALSE;
5032         }
5033         else
5034         {
5035             if(!JPEG_write_RLE(&my_vld, FALSE))
5036                 return FALSE;
5037         }
5038     }
5039 
5040     _s16dc_pred[cur_blk - 1] = p[0];//update predictor
5041     return TRUE;
5042 }
5043 //------------------------------------------------------------------------------
5044 // Starts a new scan.
JPEG_init_scan(void)5045 JPEG_STATIC MS_BOOL JPEG_init_scan(void)
5046 {
5047     if (!JPEG_locate_sos_marker())
5048         return FALSE;
5049 
5050     JPEG_calc_mcu_block_order();
5051 
5052     if (_HeadCheck.DHT)
5053     {
5054         if(!JPEG_check_huff_tables())
5055             return FALSE;
5056     }
5057 
5058     if (_HeadCheck.DQT)
5059     {
5060         if(!JPEG_check_quant_tables())
5061             return FALSE;
5062     }
5063 
5064 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
5065     JPEG_memset((void *)_u32Last_dc_val, 0, _u8Comps_in_frame * sizeof( MS_U32 ) );
5066 
5067     _u32EOB_run = 0;
5068 #endif
5069 
5070     if ( _u16Restart_interval )
5071     {
5072         _u16Restarts_left = _u16Restart_interval;
5073         _u16Next_restart_num = 0;
5074     }
5075 
5076 ////    _Total_Decoded_Size = (MS_S32)JPEG_GetECS();
5077 
5078 #ifdef CMODEL
5079     {
5080         // pre-fill bit buffer for later decoding
5081         _s16Bits_left = 16;
5082         JPEG_get_bits_2( 16 );
5083         JPEG_get_bits_2( 16 );
5084     }
5085 #else
5086     {
5087 #if SW_JPD_RGB_CMYK
5088         if(_bProgressive_flag
5089         || (E_JPEG_CMYK == gu8Scan_type)
5090         || (E_JPEG_RGB == gu8Scan_type))
5091 #else
5092         if(_bProgressive_flag)
5093 #endif
5094         {
5095             // pre-fill bit buffer for later decoding
5096             _s16Bits_left = 16;
5097             JPEG_get_bits_2( 16 );
5098             JPEG_get_bits_2( 16 );
5099         }
5100     }
5101 #endif
5102 
5103     JPEG_DEBUG_API_MSG("JPEG_init_scan:ECS 0x%08lx\n", JPEG_GetECS());
5104 
5105     return TRUE;
5106 }
5107 
5108 #if SW_JPD_RGB_CMYK
5109 //------------------------------------------------------------------------------
5110 // Create a few tables that allow us to quickly convert YCbCr to RGB.
msAPI_JPEG_create_look_ups(void)5111 JPEG_STATIC void msAPI_JPEG_create_look_ups( void )
5112 {
5113     MS_S16 i, k;
5114     //kevinhuang, modify
5115     /*
5116     for (i = 0; i <= 255; i++)
5117     {
5118       //k = (i * 2) - 255;
5119         k = (i * 2) - 256; // Dec. 28 2001- change so table[128] == 0
5120       gs32Crr[i] = ( FIX(1.40200/2)  * k + ONE_HALF) >> SCALEBITS;
5121       gs32Cbb[i] = ( FIX(1.77200/2)  * k + ONE_HALF) >> SCALEBITS;
5122       gs32Crg[i] = (-FIX(0.71414/2)) * k;
5123       gs32Cbg[i] = (-FIX(0.34414/2)) * k + ONE_HALF;
5124     }
5125     */
5126     for ( i = 0; i <= 255; i++ )
5127     {
5128         k = i - 128;
5129 
5130         gs32Crr[i] = ( FIX( 1.40200 ) * k + ONE_HALF ) >> SCALEBITS;
5131         gs32Cbb[i] = ( FIX( 1.77200 ) * k + ONE_HALF ) >> SCALEBITS;
5132 
5133         gs32Crg[i] = ( -FIX( 0.71414 ) ) * k ; //+ ONE_HALF) >> SCALEBITS;???
5134         gs32Cbg[i] = ( -FIX( 0.34414 ) ) * k + ONE_HALF; //>> SCALEBITS;???
5135 
5136     }
5137 }
5138 
5139 #endif //SW_JPD_RGB_CMYK
5140 //------------------------------------------------------------------------------
5141 // Starts a frame. Determines if the number of components or sampling factors
5142 // are supported.
JPEG_init_frame(void)5143 JPEG_STATIC MS_BOOL JPEG_init_frame(void) //ok
5144 {
5145 #if SW_JPD_RGB_CMYK
5146     MS_U32 i;
5147     MS_U8 *q;
5148 #endif
5149 
5150     JPEG_DEBUG_API_MSG("JPEG_init_frame:\n");
5151     if ( _u8Comps_in_frame == 1 )
5152     {
5153         gu8Scan_type = E_JPEG_GRAYSCALE;
5154 
5155         _u16Max_blocks_per_mcu = 1;
5156 
5157         gu8Max_mcu_x_size = 8;
5158         gu8Max_mcu_y_size = 8;
5159     }
5160     else if ( _u8Comps_in_frame == 3 )
5161     {
5162         if ( ( ( _u8Comp_h_samp[1] != 1 ) || ( _u8Comp_v_samp[1] != 1 ) ) ||   //support only U_H1V1 & V_H1V1
5163             ( ( _u8Comp_h_samp[2] != 1 ) || ( _u8Comp_v_samp[2] != 1 ) ) )
5164         {
5165             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
5166             return FALSE;
5167         }
5168 
5169         if ( ( _u8Comp_h_samp[0] == 1 ) && ( _u8Comp_v_samp[0] == 1 ) )
5170         {
5171             //set RGB based jpeg flag
5172             if(_u8Comp_ident[0] == 82 || _u8Comp_ident[0] == 71 || _u8Comp_ident[0] == 66)
5173             {
5174 #if SW_JPD_RGB_CMYK
5175                 if( FALSE == bEnableRGB )
5176                 {
5177                     JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5178                     return FALSE;
5179                 }
5180                 JPEG_DEBUG_API_MSG("Get JPEG_RGB\n");
5181                 gu8Scan_type = E_JPEG_RGB; //RGB
5182 #else
5183                 JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5184                 return FALSE;
5185 #endif
5186             }
5187             else
5188             {
5189                 JPEG_DEBUG_API_MSG("Get JPEG_YH1V1\n");
5190                 gu8Scan_type = E_JPEG_YH1V1; //4:4:4
5191             }
5192 
5193             _u16Max_blocks_per_mcu = 3;
5194 
5195             gu8Max_mcu_x_size = 8;
5196             gu8Max_mcu_y_size = 8;
5197         }
5198         else if ( ( _u8Comp_h_samp[0] == 2 ) && ( _u8Comp_v_samp[0] == 1 ) )
5199         {
5200             gu8Scan_type = E_JPEG_YH2V1; //4:2:2
5201 
5202             _u16Max_blocks_per_mcu = 4;
5203 
5204             gu8Max_mcu_x_size = 16;
5205             gu8Max_mcu_y_size = 8;
5206         }
5207         else if ( ( _u8Comp_h_samp[0] == 1 ) && ( _u8Comp_v_samp[0] == 2 ) )
5208         {
5209             gu8Scan_type = E_JPEG_YH1V2;
5210 
5211             _u16Max_blocks_per_mcu = 4;
5212 
5213             gu8Max_mcu_x_size = 8;
5214             gu8Max_mcu_y_size = 16;
5215         }
5216         else if ( ( _u8Comp_h_samp[0] == 2 ) && ( _u8Comp_v_samp[0] == 2 ) )
5217         {
5218             gu8Scan_type = E_JPEG_YH2V2; //4:2:0
5219 
5220             _u16Max_blocks_per_mcu = 6;
5221 
5222             gu8Max_mcu_x_size = 16;
5223             gu8Max_mcu_y_size = 16;
5224         }
5225         else if ( ( _u8Comp_h_samp[0] == 4 ) && ( _u8Comp_v_samp[0] == 1 ) )
5226         {
5227             // 4:1:1
5228             gu8Scan_type = E_JPEG_YH4V1;
5229 
5230             _u16Max_blocks_per_mcu = 6;
5231 
5232             gu8Max_mcu_x_size = 32;
5233             gu8Max_mcu_y_size = 8;
5234             //#if CMODEL
5235             //JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
5236             //#endif
5237         }
5238         else
5239         {
5240             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
5241             return FALSE;
5242         }
5243     }
5244 #if SW_JPD_RGB_CMYK
5245     else if(_u8Comps_in_frame == 4) //handle YCCK & CMYK case, must distinguish YCCK and CMYK later
5246     {
5247         if( FALSE == bEnableCMYK )
5248         {
5249             JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5250             return FALSE;
5251         }
5252 
5253         if ( ( _u8Comp_h_samp[0] == 1 ) && ( _u8Comp_v_samp[0] == 1 ) )
5254         {
5255             JPEG_DEBUG_API_MSG("Get JPEG_CMYK\n");
5256             gu8Scan_type = E_JPEG_CMYK;
5257 
5258             _u16Max_blocks_per_mcu = 4;
5259 
5260             gu8Max_mcu_x_size = 8;
5261             gu8Max_mcu_y_size = 8;
5262         }
5263         else
5264         {
5265             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
5266             return FALSE;
5267         }
5268     }
5269 #endif
5270     else
5271     {
5272         JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5273         return FALSE;
5274     }
5275 
5276     JPEG_DEBUG_API_MSG("JPEG_init_frame:gu8Scan_type = %d\n", gu8Scan_type);
5277 
5278     gu16Max_mcus_per_row = ( _u16Image_x_size + ( gu8Max_mcu_x_size - 1 ) ) / gu8Max_mcu_x_size;
5279     _u16Max_mcus_per_col = ( _u16Image_y_size + ( gu8Max_mcu_y_size - 1 ) ) / gu8Max_mcu_y_size;
5280 
5281 #ifdef CMODEL
5282     /* these values are for the *destination* pixels: after conversion */
5283 
5284     if ( E_JPEG_GRAYSCALE == gu8Scan_type )
5285     {
5286         gu8Dest_bytes_per_pixel = 1;
5287     }
5288     else
5289         //kevinhuang, ToDo 4 -> 3 later
5290     {
5291         gu8Dest_bytes_per_pixel = 4;
5292     }
5293 
5294     gu16Dest_bytes_per_scan_line = ( ( _u16Image_x_size + 15 ) & 0xFFF0 ) * gu8Dest_bytes_per_pixel;
5295     gu16Real_dest_bytes_per_scan_line = ( _u16Image_x_size * gu8Dest_bytes_per_pixel );
5296 
5297     // Initialize two scan line buffers.
5298     // FIXME: Only the V2 sampling factors need two buffers.
5299     #define DC_ALIGNMENT    16
5300     //pgu8Scan_line_0         = (MS_U8 *)JPEG_alloc(gu16Dest_bytes_per_scan_line + 8);
5301     pgu8Scan_line_0 = ( MS_U8 * )JPEG_alloc( gu16Dest_bytes_per_scan_line + DC_ALIGNMENT * 4 );
5302     if(pgu8Scan_line_0 == NULL)
5303         return FALSE;
5304     JPEG_memset((void *)pgu8Scan_line_0, 0x7F, gu16Dest_bytes_per_scan_line ); //kevinhuang, write dummy bytes for DC pitch alignment to display right border in certain color thru VE
5305 
5306     //pgu8scan_line_1         = (MS_U8 *)JPEG_alloc(gu16Dest_bytes_per_scan_line + 8);
5307     pgu8scan_line_1 = ( MS_U8 * )JPEG_alloc( gu16Dest_bytes_per_scan_line + DC_ALIGNMENT * 4 );
5308     if(pgu8scan_line_1 == NULL)
5309         return FALSE;
5310     JPEG_memset((void *)pgu8scan_line_1, 0x7F, gu16Dest_bytes_per_scan_line ); //kevinhuang, write dummy bytes for DC pitch alignment to display right border in certain color thru VE
5311 #endif
5312 #if SW_JPD_RGB_CMYK
5313     if((E_JPEG_CMYK == gu8Scan_type)
5314     || (E_JPEG_RGB == gu8Scan_type))
5315     {
5316         /* these values are for the *destination* pixels: after conversion */
5317 
5318         //Reset image x size by new Max_mcus
5319         _u16Image_x_size = gu16Max_mcus_per_row*gu8Max_mcu_x_size;
5320 
5321         _u16NonAlignmentImage_x_size = _u16Image_x_size;
5322 
5323         if ( E_JPEG_GRAYSCALE == gu8Scan_type )
5324         {
5325             _u8Dest_bytes_per_pixel = 1;
5326         }
5327         else
5328             //kevinhuang, ToDo 4 -> 3 later
5329         {
5330             _u8Dest_bytes_per_pixel = 4;
5331         }
5332 
5333         _u16Dest_bytes_per_scan_line = ( ( _u16Image_x_size + 15 ) & 0xFFF0 ) * _u8Dest_bytes_per_pixel;
5334 //        _u16Real_dest_bytes_per_scan_line = ( _u16Image_x_size * _u8Dest_bytes_per_pixel );
5335 
5336         // Initialize two scan line buffers.
5337         // FIXME: Only the V2 sampling factors need two buffers.
5338         #define DC_ALIGNMENT    16
5339         //pgu8Scan_line_0         = (U8 *)alloc(_u16Dest_bytes_per_scan_line + 8);
5340         pgu8Scan_line_0 = ( MS_U8 * )JPEG_alloc( _u16Dest_bytes_per_scan_line + DC_ALIGNMENT * 4 );
5341         if(pgu8Scan_line_0 == NULL)
5342             return FALSE;
5343         JPEG_memset( pgu8Scan_line_0, 0x7F, _u16Dest_bytes_per_scan_line ); //kevinhuang, write dummy bytes for DC pitch alignment to display right border in certain color thru VE
5344 
5345         //pgu8scan_line_1         = (U8 *)alloc(_u16Dest_bytes_per_scan_line + 8);
5346         pgu8scan_line_1 = ( MS_U8 * )JPEG_alloc( _u16Dest_bytes_per_scan_line + DC_ALIGNMENT * 4 );
5347         if(pgu8scan_line_1 == NULL)
5348             return FALSE;
5349         JPEG_memset( pgu8scan_line_1, 0x7F, _u16Dest_bytes_per_scan_line ); //kevinhuang, write dummy bytes for DC pitch alignment to display right border in certain color thru VE
5350     }
5351 #endif
5352 
5353     _u32Max_blocks_per_row = gu16Max_mcus_per_row * _u16Max_blocks_per_mcu;
5354 
5355     // Should never happen
5356     if ( _u32Max_blocks_per_row > JPEG_MAXBLOCKSPERROW )
5357     {
5358         JPEG_terminate( E_JPEG_ASSERTION_ERROR );
5359         return FALSE;
5360     }
5361 
5362 #ifdef CMODEL
5363     {
5364         MS_U32 i;
5365         MS_U8 *q;
5366 
5367         // Allocate the coefficient buffer, enough for one row's worth of MCU's
5368         q = ( MS_U8 * )JPEG_alloc( _u32Max_blocks_per_row * 64 * sizeof( JPEG_BLOCK_TYPE ) + 8 );
5369         if(q == NULL)
5370             return FALSE;
5371 
5372         // Align to 8-byte boundry, for MMX code
5373         q = ( MS_U8 * )( ( ( MS_U32 )q + 7 ) & ~7 );
5374         if(q == NULL)
5375             return FALSE;
5376 
5377         // The gps16Block_seg[] array's name dates back to the
5378         // 16-bit assembler implementation. "seg" stood for "segment".
5379         for ( i = 0; i < _u32Max_blocks_per_row; i++ )
5380         {
5381             gps16Block_seg[i] = ( JPEG_BLOCK_TYPE * )( q + i * 64 * sizeof( JPEG_BLOCK_TYPE ) );
5382         }
5383 
5384         for ( i = 0; i < _u32Max_blocks_per_row; i++ )
5385         {
5386             gu8Block_max_zag_set[i] = 64;
5387         }
5388 
5389         gpu8Sample_buf = ( MS_U8 * )( ( ( MS_U32 )JPEG_alloc( _u32Max_blocks_per_row * 64 + 8 ) + 7 ) & ~7 );
5390         if(gpu8Sample_buf == NULL)
5391             return FALSE;
5392 
5393         JPEG_CMODEL_create_look_ups();
5394     }
5395 #endif
5396 #if SW_JPD_RGB_CMYK
5397     if((E_JPEG_CMYK == gu8Scan_type)
5398     || (E_JPEG_RGB == gu8Scan_type))
5399     {
5400         // Allocate the coefficient buffer, enough for one row's worth of MCU's
5401         q = ( MS_U8 * )JPEG_alloc( _u32Max_blocks_per_row * 64 * sizeof( JPEG_BLOCK_TYPE ) + 8 );
5402         if(q == NULL)
5403             return FALSE;
5404 
5405         // Align to 8-byte boundry, for MMX code
5406         q = ( MS_U8 * )( ( ( MS_U32 )q + 7 ) & ~7 );
5407 
5408         // The _ps16Block_seg[] array's name dates back to the
5409         // 16-bit assembler implementation. "seg" stood for "segment".
5410         for ( i = 0; i < _u32Max_blocks_per_row; i++ )
5411         {
5412             _ps16Block_seg[i] = ( JPEG_BLOCK_TYPE * )( (MS_U32)q + i * 64 * sizeof( JPEG_BLOCK_TYPE ) );
5413         }
5414 
5415         for ( i = 0; i < _u32Max_blocks_per_row; i++ )
5416         {
5417             _u8Block_max_zag_set[i] = 64;
5418         }
5419 
5420         gpu8Sample_buf = ( MS_U8 * )( ( ( MS_U32 )JPEG_alloc( _u32Max_blocks_per_row * 64 + 8 ) + 7 ) & ~7 );
5421         if(gpu8Sample_buf == NULL)
5422             return FALSE;
5423 
5424         _u16Total_lines_left = _u16Image_y_size;
5425 
5426 #ifdef CMODEL
5427         gu16Mcu_lines_left = 0;
5428 #endif
5429 
5430         msAPI_JPEG_create_look_ups();
5431     }
5432 #endif
5433 
5434 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
5435     _u16Total_lines_left = _u16Max_mcus_per_col * gu8Max_mcu_y_size;
5436 #ifdef CMODEL
5437     gu16Mcu_lines_left = 0;
5438 #endif
5439 #endif
5440     return TRUE;
5441 }
5442 //------------------------------------------------------------------------------
5443 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
5444 //------------------------------------------------------------------------------
5445 // Restart interval processing.
JPEG_process_restart(void)5446 JPEG_STATIC MS_BOOL JPEG_process_restart(void)
5447 {
5448     MS_U16 i, c = 0;
5449 
5450     // Let's scan a little bit to find the marker, but not _too_ far.
5451     // 1536 is a "fudge factor" that determines how much to scan.
5452     for ( i = 1536; i > 0; i-- )
5453     {
5454         if ( JPEG_get_char() == 0xFF )
5455         {
5456             break;
5457         }
5458     }
5459 
5460     if ( i == 0 )
5461     {
5462         JPEG_terminate( E_JPEG_BAD_RESTART_MARKER );
5463         return FALSE;
5464     }
5465 
5466     for ( ; i > 0; i-- )
5467     {
5468         c = JPEG_get_char();
5469         if ( c != 0xFF )
5470         {
5471             break;
5472         }
5473     }
5474 
5475     if ( i == 0 )
5476     {
5477         JPEG_terminate( E_JPEG_BAD_RESTART_MARKER );
5478         return FALSE;
5479     }
5480 
5481     // Is it the expected marker? If not, something bad happened.
5482     if ( c != ( _u16Next_restart_num + E_JPEG_RST0 ) )
5483     {
5484         JPEG_terminate( E_JPEG_BAD_RESTART_MARKER );
5485         return FALSE;
5486     }
5487 
5488     // Reset each component's DC prediction values.
5489     JPEG_memset((void *)&_u32Last_dc_val, 0, _u8Comps_in_frame * sizeof( MS_U32 ) );
5490 
5491     _u32EOB_run = 0;
5492 
5493     _u16Restarts_left = _u16Restart_interval;
5494 
5495     _u16Next_restart_num = ( _u16Next_restart_num + 1 ) & 7;
5496 
5497     // Get the bit buffer going again...
5498     {
5499         _s16Bits_left = 16;
5500         JPEG_get_bits_2( 16 );
5501         JPEG_get_bits_2( 16 );
5502     }
5503     return TRUE;
5504 }
5505 //------------------------------------------------------------------------------
5506 // The following methods decode the various types of blocks encountered
5507 // in progressively encoded images.
JPEG_decode_block_dc_first(MS_U8 component_id,MS_U16 block_x,MS_U16 block_y)5508 JPEG_STATIC MS_BOOL JPEG_decode_block_dc_first(//JPEG_DECODER *Pd,
5509   MS_U8 component_id, MS_U16 block_x, MS_U16 block_y)
5510 {
5511     MS_S32 s, r;
5512     JPEG_BLOCK_TYPE *p = JPEG_coeff_buf_getp( _DC_Coeffs[component_id], block_x, block_y );
5513 
5514     if(p == NULL)
5515     {
5516         JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5517         return FALSE;
5518     }
5519 
5520     s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_dc_tab[component_id]] );
5521     if ( s != 0 )
5522     {
5523         r = JPEG_get_bits_2( s );
5524         s = HUFF_EXTEND_P( r, s );
5525     }
5526 
5527     // In JPD mode, the DC coefficient is the difference of nearest DC
5528     _u32Last_dc_val[component_id] = ( s += _u32Last_dc_val[component_id] );
5529 
5530     p[0] = s << _u8Successive_low;
5531     return TRUE;
5532 }
5533 //------------------------------------------------------------------------------
JPEG_decode_block_dc_refine(MS_U8 component_id,MS_U16 block_x,MS_U16 block_y)5534 JPEG_STATIC MS_BOOL JPEG_decode_block_dc_refine(//JPEG_DECODER *Pd,
5535   MS_U8 component_id, MS_U16 block_x, MS_U16 block_y)
5536 {
5537     if ( JPEG_get_bits_2( 1 ) )
5538     {
5539         JPEG_BLOCK_TYPE *p = JPEG_coeff_buf_getp( _DC_Coeffs[component_id], block_x, block_y );
5540 
5541         if(p == NULL)
5542         {
5543             JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5544             return FALSE;
5545         }
5546 
5547         p[0] |= ( 1 << _u8Successive_low );
5548     }
5549     return TRUE;
5550 }
5551 //------------------------------------------------------------------------------
JPEG_decode_block_ac_first(MS_U8 component_id,MS_U16 block_x,MS_U16 block_y)5552 JPEG_STATIC MS_BOOL JPEG_decode_block_ac_first(//JPEG_DECODER *Pd,
5553   MS_U8 component_id, MS_U16 block_x, MS_U16 block_y)
5554 {
5555     JPEG_BLOCK_TYPE *p;
5556     MS_S32 k, s, r;
5557 
5558     if ( _u32EOB_run )
5559     {
5560         _u32EOB_run--;
5561         return TRUE;
5562     }
5563 
5564     p = JPEG_coeff_buf_getp( _AC_Coeffs[component_id], block_x, block_y );
5565 
5566     if(p == NULL)
5567     {
5568         JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5569         return FALSE;
5570     }
5571 
5572     for ( k = _u8Spectral_start; k <= _u8Spectral_end; k++ )
5573     {
5574         s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_ac_tab[component_id]] );
5575 
5576         r = s >> 4;
5577         s &= 15;
5578 
5579         if ( s )
5580         {
5581             k += r;
5582             if ( k > 63 )
5583             {
5584                 JPEG_terminate( E_JPEG_DECODE_ERROR );
5585                 return FALSE;
5586             }
5587 
5588             r = JPEG_get_bits_2( s );
5589             s = HUFF_EXTEND_P( r, s );
5590 
5591             // No need to do ZAG order in JPD mode
5592 #ifdef CMODEL
5593             //p[_u8ZAG[k]] = s << _u8Successive_low;
5594             p[k] = s << _u8Successive_low;
5595 #else
5596 #if SW_JPD_RGB_CMYK
5597             if((E_JPEG_CMYK == gu8Scan_type)
5598             || (E_JPEG_RGB == gu8Scan_type))
5599             {
5600                 p[_u8ZAG[k]] = s << _u8Successive_low;
5601             }
5602             else
5603 #endif
5604             {
5605                 p[k] = s << _u8Successive_low;
5606             }
5607 #endif
5608         }
5609         else
5610         {
5611             if ( r == 15 )
5612             {
5613                 k += 15;
5614                 if ( k > 63 )
5615                 {
5616                     JPEG_terminate( E_JPEG_DECODE_ERROR );
5617                     return FALSE;
5618                 }
5619             }
5620             else
5621             {
5622                 _u32EOB_run = 1 << r;
5623 
5624                 if ( r )
5625                 {
5626                     _u32EOB_run += JPEG_get_bits_2( r );
5627                 }
5628 
5629                 _u32EOB_run--;
5630 
5631                 break;
5632             }
5633         }
5634     }
5635     return TRUE;
5636 }
5637 //------------------------------------------------------------------------------
JPEG_decode_block_ac_refine(MS_U8 component_id,MS_U16 block_x,MS_U16 block_y)5638 JPEG_STATIC MS_BOOL JPEG_decode_block_ac_refine(//JPEG_DECODER *Pd,
5639   MS_U8 component_id, MS_U16 block_x, MS_U16 block_y)
5640 {
5641     MS_S32 s, k, r;
5642     MS_S32 p1 = 1 << _u8Successive_low;
5643     MS_S32 m1 = ( -1 ) << _u8Successive_low;
5644     JPEG_BLOCK_TYPE *p = JPEG_coeff_buf_getp( _AC_Coeffs[component_id], block_x, block_y );
5645 
5646     if(p == NULL)
5647     {
5648         JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5649         return FALSE;
5650     }
5651 
5652     k = _u8Spectral_start;
5653 
5654     if ( _u32EOB_run == 0 )
5655     {
5656         for ( ; (k <= _u8Spectral_end) && ( k < 64 ); k++ )
5657         {
5658             s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_ac_tab[component_id]] );
5659 
5660             r = s >> 4;
5661             s &= 15;
5662 
5663             if ( s )
5664             {
5665                 if ( s != 1 )
5666                 {
5667                     JPEG_terminate( E_JPEG_DECODE_ERROR );
5668                     return FALSE;
5669                 }
5670 
5671                 if ( JPEG_get_bits_2( 1 ) )
5672                 {
5673                     s = p1;
5674                 }
5675                 else
5676                 {
5677                     s = m1;
5678                 }
5679             }
5680             else
5681             {
5682                 if ( r != 15 )
5683                 {
5684                     _u32EOB_run = 1 << r;
5685 
5686                     if ( r )
5687                     {
5688                         _u32EOB_run += JPEG_get_bits_2( r );
5689                     }
5690 
5691                     break;
5692                 }
5693             }
5694 
5695             do
5696             {
5697                 // No need to do ZAG order in JPD mode
5698 #ifdef CMODEL
5699                 //JPEG_BLOCK_TYPE *this_coef = p + _u8ZAG[k];
5700                 JPEG_BLOCK_TYPE *this_coef = p + k;
5701 #else
5702                 JPEG_BLOCK_TYPE *this_coef;
5703 #if SW_JPD_RGB_CMYK
5704                 if((E_JPEG_CMYK == gu8Scan_type)
5705                 || (E_JPEG_RGB == gu8Scan_type))
5706                 {
5707                     this_coef = p + _u8ZAG[k];
5708                 }
5709                 else
5710 #endif
5711                 {
5712                     this_coef = p + k;
5713                 }
5714 #endif
5715 
5716                 if ( *this_coef != 0 )
5717                 {
5718                     if ( JPEG_get_bits_2( 1 ) )
5719                     {
5720                         if ( ( *this_coef & p1 ) == 0 )
5721                         {
5722                             if ( *this_coef >= 0 )
5723                             {
5724                                 *this_coef += p1;
5725                             }
5726                             else
5727                             {
5728                                 *this_coef += m1;
5729                             }
5730                         }
5731                     }
5732                 }
5733                 else
5734                 {
5735                     if ( --r < 0 )
5736                     {
5737                         break;
5738                     }
5739                 }
5740 
5741                 k++;
5742             }
5743             while ( (k <= _u8Spectral_end) && ( k < 64 ) );
5744 
5745             if ( ( s ) && ( k < 64 ) )
5746             {
5747                 // No need to do ZAG order in JPD mode
5748 #ifdef CMODEL
5749                 //p[_u8ZAG[k]] = s;
5750                 p[k] = s;
5751 #else
5752 #if SW_JPD_RGB_CMYK
5753                 if((E_JPEG_CMYK == gu8Scan_type)
5754                 || (E_JPEG_RGB == gu8Scan_type))
5755                 {
5756                     p[_u8ZAG[k]] = s;
5757                 }
5758                 else
5759 #endif
5760                 {
5761                     p[k] = s;
5762                 }
5763 #endif
5764             }
5765         }
5766     }
5767 
5768     if ( _u32EOB_run > 0 )
5769     {
5770         for ( ; (k <= _u8Spectral_end) && ( k < 64 ); k++ )
5771         {
5772             // No need to do ZAG order in JPD mode
5773 #ifdef CMODEL
5774             //JPEG_BLOCK_TYPE *this_coef = p + _u8ZAG[k];
5775             JPEG_BLOCK_TYPE *this_coef = p + k;
5776 #else
5777             JPEG_BLOCK_TYPE *this_coef;
5778 #if SW_JPD_RGB_CMYK
5779             if((E_JPEG_CMYK == gu8Scan_type)
5780             || (E_JPEG_RGB == gu8Scan_type))
5781             {
5782                 this_coef = p + _u8ZAG[k];
5783             }
5784             else
5785 #endif
5786             {
5787                 this_coef = p + k;
5788             }
5789 #endif
5790 
5791             if ( *this_coef != 0 )
5792             {
5793                 if ( JPEG_get_bits_2( 1 ) )
5794                 {
5795                     if ( ( *this_coef & p1 ) == 0 )
5796                     {
5797                         if ( *this_coef >= 0 )
5798                         {
5799                             *this_coef += p1;
5800                         }
5801                         else
5802                         {
5803                             *this_coef += m1;
5804                         }
5805                     }
5806                 }
5807             }
5808         }
5809 
5810         _u32EOB_run--;
5811     }
5812     return TRUE;
5813 }
5814 //------------------------------------------------------------------------------
5815 // Decode a scan in a progressively encoded image.
JPEG_decode_scan(Pdecode_block_func decode_block_func)5816 JPEG_STATIC MS_BOOL JPEG_decode_scan(Pdecode_block_func decode_block_func)
5817 {
5818     MS_U16 mcu_row, mcu_col, mcu_block;
5819     MS_U32 block_x_mcu[JPEG_MAXCOMPONENTS], block_y_mcu[JPEG_MAXCOMPONENTS];
5820 
5821     JPEG_memset((void *)block_y_mcu, 0, sizeof( block_y_mcu ) );
5822 
5823     for ( mcu_col = 0; mcu_col < _u16Mcus_per_col; mcu_col++ )
5824     {
5825         int component_num, component_id;
5826 
5827         JPEG_memset((void *)block_x_mcu, 0, sizeof( block_x_mcu ) );
5828 
5829         for ( mcu_row = 0; mcu_row < _u16Mcus_per_row; mcu_row++ )
5830         {
5831             MS_U8 block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
5832 
5833             if ( ( _u16Restart_interval ) && ( _u16Restarts_left == 0 ) )
5834             {
5835                 if(!JPEG_process_restart())
5836                     return FALSE;
5837             }
5838 
5839             for ( mcu_block = 0; mcu_block < _u8Blocks_per_mcu; mcu_block++ )
5840             {
5841                 component_id = _u8Mcu_org[mcu_block];
5842 
5843                 if(!decode_block_func( component_id, block_x_mcu[component_id] + block_x_mcu_ofs, block_y_mcu[component_id] + block_y_mcu_ofs ))
5844                 {
5845                     JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5846                     return FALSE;
5847                 }
5848 
5849                 if ( _u8Comps_in_scan == 1 )
5850                 {
5851                     block_x_mcu[component_id]++;
5852                 }
5853                 else
5854                 {
5855                     if ( ++block_x_mcu_ofs == _u8Comp_h_samp[component_id] )
5856                     {
5857                         block_x_mcu_ofs = 0;
5858 
5859                         if ( ++block_y_mcu_ofs == _u8Comp_v_samp[component_id] )
5860                         {
5861                             block_y_mcu_ofs = 0;
5862 
5863                             block_x_mcu[component_id] += _u8Comp_h_samp[component_id];
5864                         }
5865                     }
5866                 }
5867             }
5868 
5869             _u16Restarts_left--;
5870         }
5871 
5872         if ( _u8Comps_in_scan == 1 )
5873         {
5874             block_y_mcu[_u8Comp_list[0]]++;
5875         }
5876         else
5877         {
5878             for ( component_num = 0; component_num < _u8Comps_in_scan; component_num++ )
5879             {
5880                 component_id = _u8Comp_list[component_num];
5881 
5882                 block_y_mcu[component_id] += _u8Comp_v_samp[component_id];
5883             }
5884         }
5885     }
5886     return TRUE;
5887 }
5888 //------------------------------------------------------------------------------
5889 // Decode a progressively encoded image.
JPEG_init_progressive(void)5890 JPEG_STATIC MS_BOOL JPEG_init_progressive(void)
5891 {
5892     MS_U8 i;
5893 
5894     if ( _u8Comps_in_frame == 4 )
5895     {
5896         JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5897         return FALSE;
5898     }
5899 
5900     // Allocate the coefficient buffers.
5901     for ( i = 0; i < _u8Comps_in_frame; i++ )
5902     {
5903         _DC_Coeffs[i] = JPEG_coeff_buf_open( ((gu16Max_mcus_per_row+0x1)& ~0x1) * _u8Comp_h_samp[i], ((_u16Max_mcus_per_col+0x1)& ~0x1) * _u8Comp_v_samp[i], 1, 1 );
5904         if(_DC_Coeffs[i] == NULL)
5905         {
5906             return FALSE;
5907         }
5908 
5909         _AC_Coeffs[i] = JPEG_coeff_buf_open( ((gu16Max_mcus_per_row+0x1)& ~0x1) * _u8Comp_h_samp[i], ((_u16Max_mcus_per_col+0x1)& ~0x1) * _u8Comp_v_samp[i], 8, 8 );
5910         if(_AC_Coeffs[i] == NULL)
5911         {
5912             return FALSE;
5913         }
5914     }
5915 
5916     for ( ; ; )
5917     {
5918         MS_BOOL dc_only_scan, refinement_scan;
5919         Pdecode_block_func decode_block_func;
5920 
5921         if (!JPEG_init_scan())
5922         {
5923             break;
5924         }
5925 
5926         dc_only_scan = ( _u8Spectral_start == 0 );
5927         refinement_scan = ( _u8Successive_high != 0 );
5928 
5929         if ( ( _u8Spectral_start > _u8Spectral_end ) || ( _u8Spectral_end > 63 ) )
5930         {
5931             JPEG_terminate( E_JPEG_BAD_SOS_SPECTRAL );
5932             return FALSE;
5933         }
5934 
5935         if ( dc_only_scan )
5936         {
5937             if ( _u8Spectral_end )
5938             {
5939                 JPEG_terminate( E_JPEG_BAD_SOS_SPECTRAL );
5940                 return FALSE;
5941             }
5942         }
5943         else if ( _u8Comps_in_scan != 1 )  /* AC scans can only contain one component */
5944         {
5945             JPEG_terminate( E_JPEG_BAD_SOS_SPECTRAL );
5946             return FALSE;
5947         }
5948 
5949         if ( ( refinement_scan ) && ( _u8Successive_low != _u8Successive_high - 1 ) )
5950         {
5951             JPEG_terminate( E_JPEG_BAD_SOS_SUCCESSIVE );
5952             return FALSE;
5953         }
5954 
5955         if ( dc_only_scan )
5956         {
5957             if ( refinement_scan )
5958             {
5959                 decode_block_func = JPEG_decode_block_dc_refine;
5960             }
5961             else
5962             {
5963                 decode_block_func = JPEG_decode_block_dc_first;
5964             }
5965         }
5966         else
5967         {
5968             if ( refinement_scan )
5969             {
5970                 decode_block_func = JPEG_decode_block_ac_refine;
5971             }
5972             else
5973             {
5974                 decode_block_func = JPEG_decode_block_ac_first;
5975             }
5976         }
5977 
5978         if(!JPEG_decode_scan( decode_block_func ))
5979             return FALSE;
5980 
5981         _s16Bits_left = 0;
5982     }
5983 
5984     _u8Comps_in_scan = _u8Comps_in_frame;
5985 
5986     for ( i = 0; i < _u8Comps_in_frame; i++ )
5987     {
5988         _u8Comp_list[i] = i;
5989     }
5990 
5991     JPEG_calc_mcu_block_order();
5992     return TRUE;
5993 }
5994 #endif
5995 //------------------------------------------------------------------------------
JPEG_init_sequential(void)5996 JPEG_STATIC MS_BOOL JPEG_init_sequential(void)
5997 {
5998     if ( !JPEG_init_scan() )
5999     {
6000         JPEG_DEBUG_API_ERR("JPEG_init_scan - E_JPEG_UNEXPECTED_MARKER\n");
6001         JPEG_terminate( E_JPEG_UNEXPECTED_MARKER );
6002         return FALSE;
6003     }
6004     return TRUE;
6005 }
6006 //------------------------------------------------------------------------------
6007 #if 0
6008 JPEG_STATIC void JPEG_decode_start(void)
6009 {
6010     JPEG_init_frame();
6011 
6012     if ( _bProgressive_flag )
6013     {
6014         JPEG_init_progressive();
6015     }
6016     else
6017     {
6018         JPEG_init_sequential();
6019     }
6020 }
6021 #endif
6022 //------------------------------------------------------------------------------
6023 // Find the start of the JPEG file and reads enough data to determine
6024 // its size, number of components, etc.
JPEG_decode_init(void)6025 JPEG_STATIC MS_BOOL JPEG_decode_init(void)
6026 {
6027     MS_U16 pic_width, pic_height;
6028 
6029     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
6030 
6031     _HeadCheck.result = 0;
6032 
6033     if(!JPEG_init())
6034         return FALSE;
6035 
6036 #if SUPPORT_MPO_FORMAT
6037     _pu8In_buf_ofs = _pu8In_buf_MPO_ofs;
6038     _u32In_buf_left = _u32In_buf_MPO_left;
6039     JPEG_DEBUG_API_MSG("JPEG_decode_init(), set _pu8In_buf_ofs=0x%lx, _u32In_buf_left=0x%lx========\n", (MS_U32)_pu8In_buf_ofs, (MS_U32)_u32In_buf_left);
6040 #endif
6041 
6042     if(!JPEG_locate_sof_marker())
6043         return FALSE;
6044 
6045     if (bMHEG5)
6046     {
6047     if(_Error_code == E_JPEG_UNSUPPORTED_MARKER)
6048         {
6049             return TRUE;
6050         }
6051     }
6052 
6053     if(_u8DecodeType == E_JPEG_TYPE_THUMBNAIL)
6054     {
6055         if(_bThumbnailFound)
6056         {
6057             _bThumbnailAccessMode = TRUE;
6058             ////MApi_JPEG_Finalize(); //replace with JPEG_free_all_blocks()
6059             JPEG_free_all_blocks();
6060 
6061             if(!JPEG_init())
6062                 return FALSE;
6063 
6064             // save the original image size, because msAPI_JPEG_init will reset all variables to 0
6065             if(!JPEG_locate_sof_marker())
6066                 return FALSE;
6067 
6068             if(_bProgressive_flag)
6069             {
6070                 JPEG_DEBUG_API_ERR("Progressive image in thumbnail... do not handle it\n");
6071                 JPEG_terminate( E_JPEG_BAD_APP1_MARKER );
6072                 return FALSE;
6073             }
6074         }
6075         else
6076         {
6077             JPEG_terminate( E_JPEG_NO_THUMBNAIL );
6078             return FALSE;
6079         }
6080     }
6081 
6082     // calculate aligned resolution
6083     JPEG_GetAlignedResolution(&pic_width, &pic_height);
6084     JPEG_DEBUG_API_MSG("pic_width = %d, pic_height = %d\n",pic_width, pic_height);
6085 
6086     _u16AlignedImagePitch = pic_width;
6087     _u16AlignedImagePitch_H = pic_height;
6088     _u16AlignedImageWidth = _u16Image_x_size;
6089     _u16AlignedImageHeight = _u16Image_y_size;
6090 
6091 #if SUPPORT_MPO_FORMAT
6092     if(bIsMPOFormat)
6093     {
6094         JPEG_PRO_MAX_WIDTH = JPEG_MPO_PRO_MAX_WIDTH;
6095         JPEG_PRO_MAX_HEIGHT = JPEG_MPO_PRO_MAX_HEIGHT;
6096         JPEG_MAX_WIDTH  = JPEG_MPO_MAX_WIDTH;
6097         JPEG_MAX_HEIGHT   = JPEG_MPO_MAX_HEIGHT;
6098     }
6099 #endif
6100 
6101     if(_bProgressive_flag)
6102     {
6103         MAX_JPEG_WIDTH_HD = JPEG_PRO_MAX_WIDTH;
6104         MAX_JPEG_HEIGHT_HD = JPEG_PRO_MAX_HEIGHT;
6105     }
6106     else
6107     {
6108         MAX_JPEG_WIDTH_HD = JPEG_MAX_WIDTH;
6109         MAX_JPEG_HEIGHT_HD = JPEG_MAX_HEIGHT;
6110     }
6111 
6112 #ifndef CMODEL
6113     // calculate down scale factor
6114     if ((_u16Image_x_size > MAX_JPEG_WIDTH_HD * 4) || (_u16Image_y_size > MAX_JPEG_HEIGHT_HD * 4))
6115     {
6116         JPEG_DEBUG_API_MSG("down scale 1/8!!\n");
6117         _u8DownScaleRatio = E_JPD_DOWNSCALE_EIGHTH;
6118         _u8ScaleDownFactor = 8;
6119     }
6120     else if ((_u16Image_x_size > MAX_JPEG_WIDTH_HD * 2) || (_u16Image_y_size > MAX_JPEG_HEIGHT_HD * 2))
6121     {
6122         JPEG_DEBUG_API_MSG("down scale 1/4!!\n");
6123         _u8DownScaleRatio = E_JPD_DOWNSCALE_FOURTH;
6124         _u8ScaleDownFactor = 4;
6125     }
6126     else if ((_u16Image_x_size > MAX_JPEG_WIDTH_HD) || (_u16Image_y_size > MAX_JPEG_HEIGHT_HD))
6127     {
6128         JPEG_DEBUG_API_MSG("down scale 1/2!!\n");
6129         _u8DownScaleRatio = E_JPD_DOWNSCALE_HALF;
6130         _u8ScaleDownFactor = 2;
6131     }
6132     else
6133     {
6134         JPEG_DEBUG_API_MSG("down scale 1/1!!\n");
6135         _u8DownScaleRatio = E_JPD_DOWNSCALE_ORG;
6136         _u8ScaleDownFactor = 1;
6137     }
6138 
6139 #if (ENABLE_TEST_11_JPEGScaleDownFunctionTest_2==true) \
6140     || (ENABLE_TEST_11_JPEGScaleDownFunctionTest_4==true) \
6141     || (ENABLE_TEST_11_JPEGScaleDownFunctionTest_8==true)
6142     //downscale test(only for unit test)
6143 
6144     #if (ENABLE_TEST_11_JPEGScaleDownFunctionTest_2==true)
6145     _u8DownScaleRatio = E_JPD_DOWNSCALE_HALF;
6146     #elif (ENABLE_TEST_11_JPEGScaleDownFunctionTest_4==true)
6147     _u8DownScaleRatio = E_JPD_DOWNSCALE_FOURTH;
6148     #elif (ENABLE_TEST_11_JPEGScaleDownFunctionTest_8==true)
6149     _u8DownScaleRatio = E_JPD_DOWNSCALE_EIGHTH;
6150     #else
6151     _u8DownScaleRatio = E_JPD_DOWNSCALE_ORG;
6152     #endif
6153 
6154     if(_u8DownScaleRatio == E_JPD_DOWNSCALE_HALF)
6155     {
6156         _u8ScaleDownFactor = 2;
6157     }
6158     else if(_u8DownScaleRatio == E_JPD_DOWNSCALE_FOURTH)
6159     {
6160         _u8ScaleDownFactor = 4;
6161     }
6162     else if(_u8DownScaleRatio == E_JPD_DOWNSCALE_EIGHTH)
6163     {
6164         _u8ScaleDownFactor = 8;
6165     }
6166 #endif
6167 
6168     if (E_JPD_DOWNSCALE_ORG != _u8DownScaleRatio)
6169     {
6170         _u16AlignedImagePitch = pic_width / _u8ScaleDownFactor;
6171         _u16AlignedImagePitch_H =
6172             ((pic_height + ((MS_U32)_u8ScaleDownFactor - 1)) & (~((MS_U32)_u8ScaleDownFactor - 1)))/ _u8ScaleDownFactor;
6173         _u16AlignedImageWidth = _u16Image_x_size / _u8ScaleDownFactor;
6174         _u16AlignedImageHeight =
6175                 ((_u16AlignedImageHeight + ((MS_U32)_u8ScaleDownFactor - 1)) & (~((MS_U32)_u8ScaleDownFactor - 1))) / _u8ScaleDownFactor;
6176     }
6177 
6178     switch (_u8DownScaleRatio)
6179     {
6180         case E_JPD_DOWNSCALE_HALF:
6181             ROI_width = (pic_width / 16) * 16;
6182             break;
6183 
6184         case E_JPD_DOWNSCALE_FOURTH:
6185             ROI_width = (pic_width / 32) * 32;
6186             break;
6187 
6188         case E_JPD_DOWNSCALE_EIGHTH:
6189             ROI_width = (pic_width / 64) * 64;
6190             break;
6191 
6192         default:
6193             ROI_width = pic_width;
6194             break;
6195     }
6196     JPEG_DEBUG_API_MSG("Original ROI_width = %d \n", ROI_width);
6197 
6198     if (ROI_width != pic_width)
6199     {
6200         _u16AlignedImagePitch = _u16AlignedImageWidth = ROI_width / _u8ScaleDownFactor;
6201 
6202         if(_bProgressive_flag)
6203             _Progressive_ROI_flag = TRUE;
6204     }
6205 #endif //#ifndef CMODEL
6206     return TRUE;
6207 }
6208 #if 0 // not implement for new MDDI - harold
6209 //------------------------------------------------------------------------------
6210 JPEG_STATIC void msAPI_MJPEG_decode_init(PJPEG_FILE_FileSystem_t Pstream)
6211 {
6212     JPEG_init( Pstream );
6213     MSAPI_MJPEG_Get_Pics();
6214     JPEG_locate_sof_marker();
6215 
6216     if(_u8DecodeType == E_JPEG_TYPE_THUMBNAIL)
6217     {
6218         if(_bThumbnailFound)
6219         {
6220             _bThumbnailAccessMode = TRUE;
6221             ////MApi_JPEG_Finalize(); //replace with JPEG_free_all_blocks()
6222             JPEG_free_all_blocks();
6223             JPEG_init( Pstream );
6224 
6225             JPEG_locate_sof_marker();
6226 
6227             if(_bProgressive_flag)
6228             {
6229                 JPEG_DEBUG_API_MSG("Progressive image in thumbnail... do not handle it\n");
6230                 JPEG_terminate( E_JPEG_BAD_APP1_MARKER );
6231             }
6232         }
6233         else
6234         {
6235             JPEG_terminate( E_JPEG_NO_THUMBNAIL );
6236         }
6237     }
6238 }
6239 //------------------------------------------------------------------------------
6240 // Parse header of MJPEG to get total num of pics in the file
6241 JPEG_STATIC void MSAPI_MJPEG_Get_Pics(void)
6242 {
6243     //Num of pics located at Byte 49~52, hence we need to offset 48 bytes
6244     MS_U8 ByteOffset, ByteSkip = 48;
6245 
6246     //Total num of pics
6247     NumPics = 0;
6248 
6249     for(ByteOffset = 0; ByteOffset < ByteSkip; ByteOffset++)
6250     {
6251         JPEG_get_char();
6252     }
6253 
6254     //Handle Little-Ending
6255     NumPics += (JPEG_get_char());
6256     NumPics += (JPEG_get_char() << 8);
6257     NumPics += (JPEG_get_char() << 16);
6258     NumPics += (JPEG_get_char() << 24);
6259 
6260     JPEG_DEBUG_API_MSG("NumPics = %ld\n",NumPics);
6261 }
6262 #endif // not implement for new MDDI - harold
6263 
6264 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
6265 //------------------------------------------------------------------------------
6266 // Loads and dequantizes the next row of (already decoded) coefficients.
6267 // Progressive images only.
JPEG_load_next_row(void)6268 JPEG_STATIC MS_BOOL JPEG_load_next_row(void)
6269 {
6270 #ifndef CMODEL
6271     JPEG_BLOCK_TYPE p[64];
6272     MS_BOOL EOF_Flag = FALSE; //CL82399
6273 #else
6274     MS_S16 i;
6275     JPEG_BLOCK_TYPE *p;
6276     MS_U16 row_block = 0;
6277     JPEG_QUANT_TYPE *q;
6278 #endif
6279 
6280     MS_U16 mcu_row, mcu_block;
6281     MS_U8 component_num, component_id;
6282     MS_U16 block_x_mcu[JPEG_MAXCOMPONENTS];
6283 
6284     JPEG_memset((void *)block_x_mcu, 0, JPEG_MAXCOMPONENTS * sizeof( MS_U16 ) );
6285 
6286     for ( mcu_row = 0; mcu_row < _u16Mcus_per_row; mcu_row++ )
6287     {
6288         MS_U16 block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
6289 
6290         for ( mcu_block = 0; mcu_block < _u8Blocks_per_mcu; mcu_block++ )
6291         {
6292             JPEG_BLOCK_TYPE *pAC;
6293             JPEG_BLOCK_TYPE *pDC;
6294 
6295             component_id = _u8Mcu_org[mcu_block];
6296 
6297 #ifdef CMODEL
6298             p = gps16Block_seg[row_block];
6299             q = _QuantTables[_u8Comp_quant[component_id]].s16Value;
6300 #endif
6301 
6302             pAC = JPEG_coeff_buf_getp( _AC_Coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, _u32Block_y_mcu[component_id] + block_y_mcu_ofs );
6303             if(pAC == NULL)
6304             {
6305                 JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
6306                 return FALSE;
6307             }
6308 
6309             pDC = JPEG_coeff_buf_getp( _DC_Coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, _u32Block_y_mcu[component_id] + block_y_mcu_ofs );
6310             if(pDC == NULL)
6311             {
6312                 JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
6313                 return FALSE;
6314             }
6315             p[0] = pDC[0];
6316             JPEG_memcpy((void *)&p[1], (void *)&pAC[1], 63 * sizeof( JPEG_BLOCK_TYPE ) );
6317 
6318 #ifdef CMODEL //cmodel do not support scale down feature.
6319             if(mcu_block==(_u8Blocks_per_mcu -1)&&mcu_row==(_u16Mcus_per_row - 1)&&(_u16Total_lines_left - gu8Max_mcu_y_size)==0)
6320             {
6321                 if(!JPEG_do_RLE(p, TRUE, component_id, TRUE))   // means it is end of picture
6322                     return FALSE;
6323             }
6324             else
6325             {
6326                 if(!JPEG_do_RLE(p, FALSE, component_id, TRUE))
6327                     return FALSE;
6328             }
6329 #else //CL82399
6330             if( _Progressive_ROI_flag == FALSE )
6331             {
6332                 if((mcu_block == (_u8Blocks_per_mcu - 1))
6333                 && (mcu_row == (_u16Mcus_per_row - 1))
6334                 && (_u16Total_lines_left == gu8Max_mcu_y_size))
6335                 {
6336                     JPEG_DEBUG_API_MSG("EOF!!!!!ROI enable!!!\n");
6337                     if(!JPEG_do_RLE(p, TRUE, component_id, TRUE))   // means it is end of picture
6338                         return FALSE;
6339                 }
6340                 else
6341                 {
6342                     if(!JPEG_do_RLE(p, FALSE, component_id, TRUE))
6343                         return FALSE;
6344                 }
6345             }
6346             else
6347             {
6348                 if( _u16Total_lines_left == gu8Max_mcu_y_size )//Last Line
6349                 {
6350                     //JPEG_DEBUG_API_MSG("_u16Total_lines_left ==%d,%d,%d \n", gu8Max_mcu_y_size, mcu_block, mcu_row);
6351 
6352                     if((mcu_block == (_u8Blocks_per_mcu - 1))
6353                     && ((mcu_row + 2) * gu8Max_mcu_x_size >  ROI_width))//Last line last block within rang
6354                     {
6355                         if( EOF_Flag == FALSE )
6356                         {
6357                             EOF_Flag = TRUE;
6358                             JPEG_DEBUG_API_MSG("EOF!!!!!No ROI!!!\n");
6359                             if(!JPEG_do_RLE(p, TRUE, component_id, TRUE))   // means it is end of picture
6360                                 return FALSE;
6361                         }
6362                     }
6363                     else
6364                     {
6365                          if(!JPEG_do_RLE(p, FALSE, component_id, TRUE))
6366                             return FALSE;
6367                     }
6368                 }
6369                 else
6370                 {
6371                     if((mcu_row + 1) * gu8Max_mcu_x_size >  ROI_width)//ever line out rang block
6372                     {
6373                         //JPEG_do_RLE(p, FALSE, component_id, FALSE);
6374                     }
6375                     else
6376                     {
6377                         if(!JPEG_do_RLE(p, FALSE, component_id, TRUE))
6378                             return FALSE;
6379                     }
6380                 }
6381             }
6382 #endif
6383 
6384 
6385 #ifdef CMODEL
6386             {
6387                 for ( i = 63; i > 0; i-- )
6388                 {
6389                     if ( p[_u8ZAG[i]] )
6390                     {
6391                         break;
6392                     }
6393                 }
6394 
6395                 //block_num[row_block++] = i + 1;
6396 
6397                 for ( ; i >= 0; i-- )
6398                 {
6399                     if ( p[_u8ZAG[i]] )
6400                     {
6401                         p[_u8ZAG[i]] *= q[i];
6402                     }
6403                 }
6404             }
6405 
6406             row_block++;
6407 #endif
6408 
6409             if ( _u8Comps_in_scan == 1 )
6410             {
6411                 block_x_mcu[component_id]++;
6412             }
6413             else
6414             {
6415                 if ( ++block_x_mcu_ofs == _u8Comp_h_samp[component_id] )
6416                 {
6417                     block_x_mcu_ofs = 0;
6418 
6419                     if ( ++block_y_mcu_ofs == _u8Comp_v_samp[component_id] )
6420                     {
6421                         block_y_mcu_ofs = 0;
6422 
6423                         block_x_mcu[component_id] += _u8Comp_h_samp[component_id];
6424                     }
6425                 }
6426             }
6427         }
6428     }
6429 
6430     if ( _u8Comps_in_scan == 1 )
6431     {
6432         _u32Block_y_mcu[_u8Comp_list[0]]++;
6433     }
6434     else
6435     {
6436         for ( component_num = 0; component_num < _u8Comps_in_scan; component_num++ )
6437         {
6438             component_id = _u8Comp_list[component_num];
6439 
6440             _u32Block_y_mcu[component_id] += _u8Comp_v_samp[component_id];
6441         }
6442     }
6443     return TRUE;
6444 }
6445 #endif
6446 
6447 #ifndef CMODEL
6448 //------------------------------------------------------------------------------
6449 /******************************************************************************/
6450 ///Start JPEG decoding
6451 /******************************************************************************/
JPEG_StartDecode(void)6452 JPEG_STATIC MS_BOOL JPEG_StartDecode(void)
6453 {
6454     MS_U16 pic_width, pic_height;
6455     MS_U8 Y_VSF = _u8Comp_v_samp[0];
6456     MS_U8 Y_HSF = _u8Comp_h_samp[0];
6457     MS_BOOL bUV_en;
6458     MS_U32 reg_value;
6459     MS_U8 i;
6460     MS_U8 com_num = 0;
6461     MS_U8 comp[JPEG_MAXCOMPONENTS];
6462     JPD_BufCfg structBuf;
6463 
6464     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
6465 #ifdef JPD_LONGJUMP_SUPPORT
6466     if ( setjmp( _jmp_state ) )
6467     {
6468         return FALSE;
6469     }
6470 #endif
6471 
6472 #if ENABLE_JPEG_NO_SIZE_LOWER_BOUND
6473     MDrv_JPD_SetPicDimension(0xFF, 0xFF);
6474 #endif
6475 
6476     // reset JPD hardware
6477     //MDrv_JPD_Reset();
6478     if(_bProgressive_flag)
6479     {
6480         MDrv_JPD_Rst_SVLD();
6481     }
6482     else
6483     {
6484         MDrv_JPD_Rst();
6485     }
6486 
6487 
6488     // Calculate how many valid quantization tables for components
6489     JPEG_memset((void *)comp, 0, JPEG_MAXCOMPONENTS);
6490     for(i = 0; i<_u8Comps_in_frame; i++)
6491     {
6492         comp[_u8Comp_quant[i]] = 1;
6493     }
6494 
6495     for(i = 0; i<JPEG_MAXCOMPONENTS; i++)
6496     {
6497         if(comp[i]==1)
6498             com_num++;
6499     }
6500 
6501     if(_u8Comps_in_frame>1)
6502         bUV_en = TRUE;
6503     else
6504         bUV_en = FALSE;
6505 
6506     //Get Aligned width & height
6507     JPEG_GetAlignedResolution(&pic_width, &pic_height);
6508 
6509 #if 1
6510     structBuf.bProgressive = _bProgressive_flag;
6511     structBuf.bThumbnailAccessMode = _bThumbnailAccessMode;
6512     structBuf.u32ThumbnailBufAddr = MS_VA2PA((MS_U32)INTERNAL_BUFFER_ADDR);
6513     structBuf.u32ThumbnailBufSize = JPEG_DEFAULT_EXIF_SIZE;
6514     structBuf.u32ThumbnailBufOffset = _u32ThumbnailOffset + JPEG_GetECS();
6515     structBuf.u32MRCBufAddr = MS_VA2PA((MS_U32)MRC_BUFFER_ADDR);
6516     structBuf.u32MRCBufSize = MRC_BUFFER_SIZE;
6517     structBuf.u32MRCBufOffset = JPEG_GetECS();
6518     structBuf.u32MWCBufAddr = MS_VA2PA((MS_U32)MWC_BUFFER_ADDR);
6519     //HW limitation:if we don't enable write-protect mode, set this value to zero.
6520 
6521 #if (ENABLE_TEST_09_JPEGWriteProtectTest==false)
6522     structBuf.u16MWCBufLineNum = 0;
6523 //    structBuf.u16MWCBufLineNum = ((pic_height/_u8ScaleDownFactor + 8) < 0x07FF)?(pic_height/_u8ScaleDownFactor + 8):0x7FF;
6524 #else
6525     structBuf.u16MWCBufLineNum = 32;
6526 #endif
6527 
6528     JPEG_DEBUG_API_MSG("bProgressive = %d, bThumbnailAccessMode = %d\n"
6529     , structBuf.bProgressive, structBuf.bThumbnailAccessMode);
6530     JPEG_DEBUG_API_MSG("u32ThumbnailBufAddr = 0x%lX, u32ThumbnailBufSize = 0x%lX, u32ThumbnailBufOffset = 0x%lX\n"
6531     , structBuf.u32ThumbnailBufAddr, structBuf.u32ThumbnailBufSize, structBuf.u32ThumbnailBufOffset);
6532     JPEG_DEBUG_API_MSG("u32MRCBufAddr = 0x%lX, u32MRCBufSize = 0x%lX, u32MRCBufOffset = 0x%lX\n"
6533     , structBuf.u32MRCBufAddr, structBuf.u32MRCBufSize, structBuf.u32MRCBufOffset);
6534     JPEG_DEBUG_API_MSG("u32MWCBufAddr = 0x%lX, u16MWCBufLineNum = %d\n"
6535     , structBuf.u32MWCBufAddr, structBuf.u16MWCBufLineNum);
6536 
6537     u32DataOffset += JPEG_GetECS();
6538 
6539     MDrv_JPD_InitBuf(structBuf);
6540 
6541 //    JPEG_DEBUG_API_MSG("MWCLineNum = %d, pic_height = %d, DwnScaleRatio = %d\n"
6542 //            , structBuf.u16MWCBufLineNum, pic_height, _u8ScaleDownFactor);
6543 #else
6544     if(_bThumbnailAccessMode)
6545     {
6546         // Set MRC buffer for JPD
6547         MDrv_JPD_SetReadBuffer(INTERNAL_BUFFER_ADDR, JPEG_DEFAULT_EXIF_SIZE);
6548         // Set MRC start access byte address
6549         MDrv_JPD_SetMRCStartAddr(INTERNAL_BUFFER_ADDR + _u32ThumbnailOffset + JPEG_GetECS());
6550     }
6551     else
6552     {
6553         if(_bProgressive_flag)
6554         {
6555             // Set MRC buffer for JPD
6556             MDrv_JPD_SetReadBuffer(MRC_BUFFER_ADDR, MRC_BUFFER_SIZE);
6557             // Set MRC start access byte address
6558             MDrv_JPD_SetMRCStartAddr(MRC_BUFFER_ADDR);
6559         }
6560         else
6561         {
6562             // Set MRC buffer for JPD
6563             MDrv_JPD_SetReadBuffer(MRC_BUFFER_ADDR, MRC_BUFFER_SIZE);
6564             // Set MRC start access byte address
6565             MDrv_JPD_SetMRCStartAddr(MRC_BUFFER_ADDR + JPEG_GetECS());
6566         }
6567     }
6568 
6569     // Set MWC buffer for JPD
6570     MDrv_JPD_SetOutputFrameBuffer(MWC_BUFFER_ADDR);
6571 #endif
6572 
6573     // Set picture width and height
6574 #if (ENABLE_JPEG_NO_SIZE_LOWER_BOUND == 0)
6575     MDrv_JPD_SetPicDimension(pic_width, pic_height);
6576 #endif
6577 
6578     _u16NonAlignmentImage_x_size = pic_width;
6579     _u16NonAlignmentImage_y_size = _u16Image_y_size;
6580 
6581     _u16Image_x_size = pic_width/_u8ScaleDownFactor;
6582     _u16Image_y_size = pic_height/_u8ScaleDownFactor;
6583     JPEG_DEBUG_API_MSG("ScaleDownFactor = %d\n", _u8ScaleDownFactor);
6584 
6585     // In JPD, software VLD mode, we don't need to write huff & symbol tables
6586     if(_bProgressive_flag==FALSE)
6587     {
6588         //JPEG_write_Scwgif();
6589         JPEG_WriteGrpinf();
6590         //JPEG_write_symidx();
6591         JPEG_WriteSymidx();
6592     }
6593 
6594     //JPEG_write_Qtbl();
6595     JPEG_WriteIQTbl();
6596 
6597     Y_VSF -= 1;
6598     if ( Y_HSF == 4 )
6599     {
6600         Y_HSF = 3;
6601     }
6602 
6603     if(_u16Restart_interval)
6604     {
6605         JPEG_DEBUG_API_MSG("RST found! Enable JPD_RST_EN! Restart_interval = %d\n", _u16Restart_interval);
6606         //MDrv_Write2Byte(BK_JPD_RSTINTV, _u16Restart_interval - 1);
6607         MDrv_JPD_SetRSTIntv(_u16Restart_interval - 1);
6608         reg_value = ( JPD_TBL_RDY | JPD_RST_EN | ((MS_U32) _u8DownScaleRatio) << 4 | ((MS_U32) bUV_en) << 3 | ( Y_VSF << 2 ) | Y_HSF );
6609     }
6610     else
6611     {
6612         reg_value = ( JPD_TBL_RDY | ((MS_U32) _u8DownScaleRatio) << 4 | ((MS_U32) bUV_en) << 3 | ( Y_VSF << 2 ) | Y_HSF );
6613     }
6614 
6615     // There're Q tables for U & V, respectively.
6616     if(com_num>2)
6617     {
6618         JPEG_DEBUG_API_MSG("More than two Q tables! Enable JPD_SUVQ! comnum = %d\n", com_num);
6619         reg_value = reg_value | JPD_SUVQ;
6620     }
6621 
6622     /* Check the read pointer. If it is in HIGH buffer, we need
6623        to preload data to LOW buffer and then start decode.
6624        Progressive mode & thumbnail don't need to check it. */
6625     if((FALSE == _bProgressive_flag)
6626     && (FALSE == _bThumbnailAccessMode))
6627     {
6628         MS_U32 start_offset = JPEG_GetECS();
6629         JPEG_DEBUG_API_MSG("start offset = 0x%lx\n", start_offset);
6630         if ( start_offset >= (MRC_BUFFER_SIZE/2) )
6631         {
6632             JPEG_DEBUG_API_MSG("ReadPtr is in HIGH, Load LOW!!\n");
6633             if(_pFillHdrFunc)
6634             {
6635                 MS_S32 byte_read = _pFillHdrFunc((MS_PHYADDR)MS_VA2PA((MS_U32)_pu8In_buf), (MRC_BUFFER_SIZE/2));
6636                 if(byte_read < 0)
6637                 {
6638                     JPEG_terminate(E_JPEG_STREAM_READ);
6639                     return FALSE;
6640                 }
6641                 u8PreLHFlag = E_JPEG_BUFFER_LOW;
6642                 u32MRCheckCount = 0;
6643             }
6644             else
6645             {
6646                 JPEG_DEBUG_API_MSG("_pFillHdrFunc is not registered!!, still need to start decode.\n");
6647             }
6648         }
6649     }
6650 
6651     if(_bProgressive_flag)
6652         reg_value = reg_value | JPD_SVLD;
6653 
6654     // Check if it needs to do ROI
6655     if(E_JPD_DOWNSCALE_ORG != _u8DownScaleRatio)
6656     {
6657         if(ROI_width != pic_width)
6658         {
6659             JPEG_DEBUG_API_MSG("ROI!! ROI_width = %d, ROI_height = %d\n", ROI_width, pic_height);
6660             MDrv_JPD_SetROI(0, 0, (ROI_width>>3), (pic_height>>3));
6661             reg_value = reg_value | JPD_ROI_EN;
6662         }
6663         _u16Image_x_size = ROI_width/_u8ScaleDownFactor;
6664     }
6665 
6666     //Enable ECO support
6667     {
6668         MS_U16 reg_val = 0;
6669         reg_val = JPD_RST_STUFF_BYTE_HANDLE;
6670 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
6671         if(TRUE == bIs3HuffTbl)
6672         {
6673             reg_val = reg_val | JPD_3_HUFFMAN_TABLE_SUPPORT;
6674         }
6675 #endif
6676         MDrv_JPD_SetSpare(reg_val);
6677     }
6678 
6679     // mark low/high buffer valid
6680     //MDrv_Write2Byte( BK_JPD_MCONFIG, JPD_H_VLD |JPD_L_VLD);
6681     MDrv_JPD_Set_M_Config(JPD_H_VLD |JPD_L_VLD);
6682 
6683 #if (ENABLE_TEST_18_miu_sel_128M==true) \
6684     ||(ENABLE_TEST_18_miu_sel_64M==true) \
6685     ||(ENABLE_TEST_18_miu_sel_32M==true)
6686 // This is used for verification code in TEST_18_miu_sel
6687 //        MDrv_JPD_Set_M_Config(MDrv_JPD_Get_M_Config());             // 0 1
6688     #if (ENABLE_TEST_18_miu_sel_128M==true)
6689         MDrv_JPD_Set_M_Config(MDrv_JPD_Get_M_Config() | JPD_BIT(9));          // 0 1 0 1
6690     #elif (ENABLE_TEST_18_miu_sel_64M==true)
6691         MDrv_JPD_Set_M_Config(MDrv_JPD_Get_M_Config() | JPD_BIT(10));   // 0 1 0 1 0 1 0 1
6692     #else
6693         MDrv_JPD_Set_M_Config(MDrv_JPD_Get_M_Config() | JPD_BIT(9) | JPD_BIT(10));    // 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
6694     #endif
6695 #endif
6696 
6697     // enable JPD decoding
6698 #if ENABLE_JPEG_NO_SIZE_LOWER_BOUND
6699     MDrv_JPD_SetPicDimension(pic_width, pic_height);
6700 #endif
6701 
6702     //MDrv_Write2Byte( BK_JPD_SCONFIG, reg_value | JPD_DEC_EN | MDrv_JPD_GetSWResetMask());
6703     //MDrv_JPD_Set_S_Config(reg_value | JPD_DEC_EN | MDrv_JPD_GetSWResetMask());
6704     //MDrv_JPD_Set_S_Config(reg_value | JPD_DEC_EN | JPD_SWRST);
6705 #if 1
6706     // decide if we need to set the JPD_PSAVE_EN register in hal layer
6707     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST | JPD_PSAVE_EN);
6708 #else
6709 #if (ENABLE_TEST_16_JPEGEnablePsaveModeTest==false)
6710 #if defined(CHIP_A3) || defined(CHIP_E3) || defined(CHIP_A5) || defined(CHIP_A5P) ||  defined(CHIP_A6) || defined(CHIP_A7)
6711     // For safety, enable the JPD_PSAVE_EN as default since A5 (HW already fixed this bug since A1 or earlier chip)
6712     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST | JPD_PSAVE_EN);
6713 #else
6714     //due to power saving mode will make system hang in some case(the image of RST error),we do not enable this function currently.
6715     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST);
6716 #endif
6717 #else
6718 #if defined(CHIP_T3) || defined(CHIP_T4) || defined(CHIP_T7) || defined(CHIP_JANUS) || defined(CHIP_U4) || defined(CHIP_T8) || defined(CHIP_J2) || defined(CHIP_A1) || defined(CHIP_A2) || defined(CHIP_A5) || defined(CHIP_A5P) || defined(CHIP_A6) || defined(CHIP_A7) || defined(CHIP_A3) || defined(CHIP_AMETHYST) || defined(CHIP_E3)
6719     //Enable power save mode on normal JPEG decoding.
6720     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST | JPD_PSAVE_EN);
6721 #else
6722     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST);
6723 #endif
6724 #endif
6725 #endif
6726 
6727 // protect test(only for unit test)
6728 #if (ENABLE_TEST_09_JPEGWriteProtectTest==false)
6729     //MDrv_JPD_SetWriteProtect(TRUE);
6730 #else
6731     #if(JPD_SUPPORT_AUTO_PROTECT==false)
6732     MDrv_JPD_SetWriteProtect(TRUE);
6733     #endif
6734 #endif
6735 
6736 #if (JPD_SUPPORT_AUTO_PROTECT==true)
6737     MDrv_JPD_SetAutoProtect(TRUE);
6738     #if (ENABLE_TEST_22_AutoProtectFailTest == true)
6739         MDrv_JPD_SetWPENEndAddr(MWC_BUFFER_ADDR+0x200-1);
6740         JPEG_DEBUG_API_MSG("Set Auto protect address =0x%lx!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", MWC_BUFFER_ADDR+0x200);
6741     #else
6742         MDrv_JPD_SetWPENEndAddr(MWC_BUFFER_ADDR+MWC_BUFFER_SIZE-1);
6743     #endif
6744 #endif
6745 
6746     MDrv_JPD_Set_S_Config(MDrv_JPD_Get_S_Config() | JPD_DEC_EN);
6747     JPEG_DEBUG_API_MSG("After Setting SCONFIG, JPD START!!\n");
6748     //JPEG_GO;
6749     return TRUE;
6750 }
6751 #endif
6752 
6753 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
6754 //------------------------------------------------------------------------------
6755 /******************************************************************************/
6756 ///Start Progressive JPEG decode for JPD
6757 /******************************************************************************/
JPEG_Progressive_Decode(void)6758 JPEG_STATIC JPEG_Result JPEG_Progressive_Decode(void)
6759 {
6760     if(_bProgressive_flag==FALSE)
6761         return (E_JPEG_FAILED);
6762 
6763     if ( _u16Total_lines_left == 0 )
6764     {
6765         return ( E_JPEG_OKAY );
6766     }
6767 
6768 #ifdef JPD_LONGJUMP_SUPPORT
6769     if ( setjmp( _jmp_state ) )
6770     {
6771         return ( E_JPEG_FAILED );
6772     }
6773 #endif
6774 
6775     //JPEG_DEBUG_API_MSG("_u16Mcus_per_row is %d!\n",_u16Mcus_per_row);
6776     //JPEG_DEBUG_API_MSG("_u16Mcus_per_col is %d!\n",_u16Mcus_per_col);
6777     //JPEG_DEBUG_API_MSG("_u8Blocks_per_mcu is %d!\n",_u8Blocks_per_mcu);
6778     //JPEG_DEBUG_API_MSG("gu8Max_mcu_x_size is %d!\n",gu8Max_mcu_x_size);
6779     //JPEG_DEBUG_API_MSG("gu8Max_mcu_y_size is %d!\n",gu8Max_mcu_y_size);
6780 
6781     if( _u16Total_lines_left > 0 )
6782     {
6783         JPEG_DEBUG_API_MSG("%s:_u16Total_lines_left = %d\n", __FUNCTION__ , _u16Total_lines_left);
6784         if(!JPEG_load_next_row())
6785             return E_JPEG_FAILED;
6786 
6787 #if SW_JPD_RGB_CMYK
6788         if((E_JPEG_CMYK == gu8Scan_type)
6789         || (E_JPEG_RGB == gu8Scan_type))
6790         {
6791             if(!msAPI_JPEG_transform_row()) //IDCT
6792             {
6793                 JPEG_DEBUG_API_MSG("CMYK and RGB decode failed!!\n");
6794                 return ( E_JPEG_FAILED );
6795             }
6796         }
6797 #endif
6798 
6799         _u16Total_lines_left -= gu8Max_mcu_y_size;
6800     }
6801 
6802     return (E_JPEG_DONE);
6803 }
6804 #endif
6805 
6806 //------------------------------------------------------------------------------
JPEG_GetAlignedResolution(MS_U16 * width,MS_U16 * height)6807 JPEG_STATIC void JPEG_GetAlignedResolution(MS_U16 *width, MS_U16 *height)
6808 {
6809     MS_U8 mcu_width, mcu_height;
6810     MS_U8 Y_VSF = _u8Comp_v_samp[0];
6811     MS_U8 Y_HSF = _u8Comp_h_samp[0];
6812 
6813     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
6814 
6815     *width = _u16OriginalImage_x_size;
6816     *height = _u16OriginalImage_y_size;
6817 
6818     mcu_width = _u16OriginalImage_x_size % (Y_HSF * 8);
6819     if (mcu_width)
6820     {
6821         *width += (Y_HSF * 8 - mcu_width);
6822     }
6823 
6824     mcu_height = _u16OriginalImage_y_size % (Y_VSF * 8);
6825     if (mcu_height)
6826     {
6827         *height += (Y_VSF * 8 - mcu_height);
6828     }
6829 
6830     JPEG_DEBUG_API_MSG("_u8Comp_v_samp = %d, _u8Comp_h_samp = %d\n", _u8Comp_v_samp[0], _u8Comp_h_samp[0]);
6831     JPEG_DEBUG_API_MSG("_u16OriginalImage_x_size = %d, _u16OriginalImage_y_size = %d\n", _u16OriginalImage_x_size, _u16OriginalImage_y_size);
6832     JPEG_DEBUG_API_MSG("AlignWidth = %d, AlignHeight = %d\n", *width, *height);
6833 }
6834 
6835 //-------------------------------------------------------------------------------------------------
6836 //  Global Functions
6837 //-------------------------------------------------------------------------------------------------
6838 #if 0
6839 //------------------------------------------------------------------------------
6840 /******************************************************************************/
6841 ///This will set MRC buffer address & size, MWC buffer address, and internal buffer
6842 ///address & size.
6843 ///@param *pInitParam \b The pointer of information for JPEG Buffer initialization
6844 /// structure {
6845 ///         U32 u32MRCBufAddr, \b IN MRC buffer address
6846 ///         U32 u32MRCBufSize, \b IN MRC buffer size
6847 ///         U32 u32MWCBufAddr, \b IN MWC buffer address
6848 ///                                 JPEG decompress data (YUV422)
6849 ///         U32 u32InternalBufAddr, \b IN internal buffer address
6850 ///                                 (exif:64K + memory pool)
6851 ///         U32 u32InternalBufSize, \b IN internal buffer size
6852 ///         BOOL bInitMem, \b IN initialize memory pool or not
6853 ///         };
6854 ///@return true or false
6855 /******************************************************************************/
6856 MS_BOOL MApi_JPEG_SetInitParameter(JPEG_InitParam *pInitParam)
6857 {
6858     // the buffer size must be multiple of 4 bytes
6859     if((!pInitParam)
6860     || (pInitParam->u32MRCBufSize < MIN_READBUFFER_SIZE))
6861     {
6862         JPEG_terminate(E_JPEG_READBUFFER_TOOSMALL);
6863         return false;
6864     }
6865     // the buffer size must be multiple of 8 bytes
6866     pInitParam->u32MRCBufSize = pInitParam->u32MRCBufSize & ~0x7;
6867 
6868     MRC_BUFFER_ADDR = pInitParam->u32MRCBufAddr | AEON_NON_CACHE_MASK;
6869     MWC_BUFFER_ADDR = pInitParam->u32MWCBufAddr | AEON_NON_CACHE_MASK;
6870     INTERNAL_BUFFER_ADDR = pInitParam->u32InternalBufAddr | AEON_NON_CACHE_MASK;
6871     MRC_BUFFER_SIZE = pInitParam->u32MRCBufSize;
6872     INTERNAL_BUFFER_SIZE = pInitParam->u32InternalBufSize;
6873 
6874     _pu8In_buf = (MS_U8 *) pInitParam->u32MRCBufAddr;
6875 
6876     if (pInitParam->bInitMem)
6877     {
6878         JPEG_MEMORY_init_mempool(((MS_U8 *)(pInitParam->u32InternalBufAddr + JPEG_DEFAULT_EXIF_SIZE))
6879                                 , (pInitParam->u32InternalBufSize - JPEG_DEFAULT_EXIF_SIZE));
6880     }
6881 
6882     return TRUE;
6883 }
6884 //------------------------------------------------------------------------------
6885 // Call get_error_code() after constructing to determine if the stream
6886 // was valid or not. You may call the get_width(), get_height(), etc.
6887 // methods after the constructor is called.
6888 // You may then either destruct the object, or begin decoding the image
6889 // by calling begin(), then decode().
6890 void MApi_JPEG_Constructor( PJPEG_FILE_FileSystem_t Pstream, MS_U8 decode_type)
6891 {
6892     if ( setjmp( _jmp_state ) )
6893     {
6894         return;
6895     }
6896 
6897     _u8DecodeType = decode_type;
6898     JPEG_init_thumbnail();
6899     JPEG_decode_init( Pstream);
6900 
6901 #ifdef CMODEL
6902     _bReady_flag = TRUE;
6903 #endif
6904 }
6905 #endif
6906 
6907 #if SW_JPD_RGB_CMYK
MApi_JPEG_Is_RGB_CMYK(void)6908 MS_BOOL MApi_JPEG_Is_RGB_CMYK(void)
6909 {
6910     if (_u8Comps_in_frame == 3)
6911     {
6912         //support only U_H1V1 & V_H1V1
6913         if ((_u8Comp_h_samp[1] != 1 || _u8Comp_v_samp[1] != 1)
6914             || (_u8Comp_h_samp[2] != 1 || _u8Comp_v_samp[2] != 1))
6915         {
6916             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
6917             return FALSE;
6918         }
6919 
6920         if (_u8Comp_h_samp[0] == 1 && _u8Comp_v_samp[0] == 1)
6921         {
6922             if (_u8Comp_ident[0] == 82 || _u8Comp_ident[0] == 71 || _u8Comp_ident[0] == 66)
6923                 return TRUE; // RGB
6924         }
6925     }
6926     else
6927     if (_u8Comps_in_frame == 4) //handle YCCK & CMYK case, must distinguish YCCK and CMYK later
6928     {
6929         if (_u8Comp_h_samp[0] == 1 && _u8Comp_v_samp[0] == 1)
6930             return TRUE; // CMYK
6931     }
6932 
6933     return FALSE;
6934 }
6935 #endif
6936 //------------------------------------------------------------------------------
6937 /********************************************************************/
6938 ///This function will\n
6939 ///1. set MRC buffer address & size, MWC buffer address, and internal buffer address & size.\n
6940 ///2. power on JPD\n
6941 ///3. reset thumbnail parameters\n
6942 ///4. set the stream and its size, the number of components, etc.\n
6943 ///Call MApi_JPEG_GetErrorCode() after it to determine if the stream was valid or not.\n
6944 ///@param -pInitParam \b IN : \n
6945 /// structure {\n
6946 ///     MS_PHYADDR \b u32MRCBufAddr : MRC buffer address\n
6947 ///     MS_U32 \b u32MRCBufSize : MRC buffer size\n
6948 ///     MS_PHYADDR \b u32MWCBufAddr : MWC buffer address(JPEG decompress data (YUV422))\n
6949 ///     MS_U32 \b u32MWCBufSize : MWC buffer size\n
6950 ///     MS_PHYADDR \b u32InternalBufAddr : Internal buffer address(exif:64K + memory pool)\n
6951 ///     MS_U32 \b u32InternalBufSize : Internal buffer size\n
6952 ///     MS_U32 \b u32DecByteRead : how many byte read for JPEG decoder initialization\n
6953 ///     MS_BOOL \b bEOF : has read to the end of file\n
6954 ///     MS_U8 \b u8DecoderType : 3 types: JPEG main, thumbnail, or MJPEG\n
6955 ///     MS_BOOL \b bInitMem : initialize memory pool or not\n
6956 ///     JPEG_FillHdrFunc \b pFillHdrFunc : the function for fill header information\n
6957 /// };
6958 ///@return \b JPEG_Result : JPEG init status
6959 ///     - E_JPEG_OKAY : Success
6960 ///     - E_JPEG_FAILED : Failed
6961 /********************************************************************/
_MApi_JPEG_Init(JPEG_InitParam * pInitParam)6962 JPEG_Result _MApi_JPEG_Init(JPEG_InitParam *pInitParam)
6963 {
6964 #if 0
6965     MApi_JPEG_SetDbgLevel(E_JPEG_DEBUG_ALL);
6966 #endif
6967 
6968 #if LOG_DATA_TO_USB
6969     static MS_U8 fileName2[]={'/','u','s','b','/','s','d','a','1','/','l','o', 'g', '0', '0', '0', '.', 'b', 'i', 'n',0};
6970     fileName2[15]++;
6971     if(fileName2[15] > '9')
6972     {
6973         fileName2[14]++;
6974         fileName2[15] = '0';
6975     }
6976     if(fileName2[14] > '9')
6977     {
6978         fileName2[13]++;
6979         fileName2[14] = '0';
6980     }
6981 
6982     if((logBinfp = fopen((char *)fileName2, "wb"))==NULL) {
6983         printf("Can't open /usb/sda1/log.bin\n");
6984     }
6985     else
6986     {
6987         printf("open /usb/sda1/log.bin success\n");
6988     }
6989 #endif
6990 
6991     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
6992 
6993     // jpeg constructor
6994 #ifdef JPD_LONGJUMP_SUPPORT
6995     if ( setjmp( _jmp_state ) )
6996     {
6997         return  E_JPEG_FAILED;
6998     }
6999 #endif
7000 
7001     if(_bIsInit == TRUE)
7002     {
7003 #if SUPPORT_MPO_FORMAT
7004         if(MApi_JPEG_IsMPOFormat()==FALSE)
7005         {
7006             JPEG_terminate(E_JPEG_RE_INIT);
7007             return E_JPEG_FAILED;
7008         }
7009 #else
7010         JPEG_terminate(E_JPEG_RE_INIT);
7011         return E_JPEG_FAILED;
7012 #endif
7013     }
7014     else
7015     {
7016         _bIsInit = TRUE;
7017     }
7018 
7019     if(!pInitParam)
7020     {
7021         JPEG_terminate(E_JPEG_DECODE_ERROR);
7022         return E_JPEG_FAILED;
7023     }
7024 
7025 //    if(pInitParam->u32MRCBufSize < MIN_READBUFFER_SIZE)
7026     if(pInitParam->u32MRCBufSize < JPEG_DEFAULT_EXIF_SIZE)
7027     {
7028         JPEG_terminate(E_JPEG_READBUFFER_TOOSMALL);
7029         return E_JPEG_FAILED;
7030     }
7031 
7032     // the buffer size must be multiple of 8 bytes
7033     pInitParam->u32MRCBufSize = pInitParam->u32MRCBufSize & ~0x7;
7034 
7035     MRC_BUFFER_ADDR = MS_PA2KSEG1((MS_U32)pInitParam->u32MRCBufAddr);// | AEON_NON_CACHE_MASK;
7036     MWC_BUFFER_ADDR = MS_PA2KSEG1((MS_U32)pInitParam->u32MWCBufAddr);// | AEON_NON_CACHE_MASK;
7037     INTERNAL_BUFFER_ADDR = MS_PA2KSEG1((MS_U32)pInitParam->u32InternalBufAddr);// | AEON_NON_CACHE_MASK;
7038     if (__bIsMjpeg)
7039     {
7040         JPEG_DEBUG_API_MSG ("MJPEG Read buffer address CONVERT : 0x%lx - 0x%lx + 0x%lx ",
7041                 MS_VA2PA(MRC_BUFFER_ADDR), MS_VA2PA(__u32RealMjpegBase), MS_VA2PA(__u32TrickyMjpegBase));
7042         MRC_BUFFER_ADDR -= __u32RealMjpegBase;
7043         MRC_BUFFER_ADDR += __u32TrickyMjpegBase;
7044         JPEG_DEBUG_API_MSG ("= 0x%lx\n", MS_VA2PA(MRC_BUFFER_ADDR));
7045     }
7046     _pu8In_buf = (MS_U8 *)MRC_BUFFER_ADDR;//pInitParam->u32MRCBufAddr;
7047 
7048     MRC_BUFFER_SIZE = pInitParam->u32MRCBufSize;
7049     MWC_BUFFER_SIZE = pInitParam->u32MWCBufSize;
7050     INTERNAL_BUFFER_SIZE = pInitParam->u32InternalBufSize;
7051 
7052 
7053     JPEG_DEBUG_API_MSG("MRC_ADDR = 0x%lX, MRC_BUFFER_SIZE = 0x%lX\n", _u32ReadBufferAddr, _u32ReadBufferSize);
7054     JPEG_DEBUG_API_MSG("MWC_ADDR = 0x%lX, MWC_BUFFER_SIZE = 0x%lX\n", _u32WriteBufferAddr, _u32WriteBufferSize);
7055     JPEG_DEBUG_API_MSG("INTERNAL_ADDR = 0x%lX, INTERNAL_BUFFER_SIZE = 0x%lX\n", _u32InternalBufferAddr, _u32InternalBufferSize);
7056     JPEG_DEBUG_API_MSG ("MRC_PA= 0x%lx\n", MS_VA2PA(MRC_BUFFER_ADDR));
7057     JPEG_DEBUG_API_MSG ("MWC_PA= 0x%lx\n", MS_VA2PA(MWC_BUFFER_ADDR));
7058     JPEG_DEBUG_API_MSG ("INTER_PA= 0x%lx\n", MS_VA2PA(INTERNAL_BUFFER_ADDR));
7059 
7060     _pFillHdrFunc = pInitParam->pFillHdrFunc;
7061 
7062 #if SW_JPD_RGB_CMYK
7063     u8Out_buf = (MS_U8 *) MWC_BUFFER_ADDR;
7064     u32_Decode_Line = 0;
7065 #endif
7066 
7067     //JPEG_GO;
7068 
7069     // powner on JPD
7070 #ifndef CMODEL
7071     MDrv_JPD_PowerOn();
7072 #endif
7073     JPEG_DEBUG_API_MSG("MDrv_JPD_PowerOn OK!!\n");
7074 
7075     _u8DecodeType = pInitParam->u8DecodeType;
7076 
7077     _u32In_buf_left = pInitParam->u32DecByteRead;
7078     _bEOF_flag = pInitParam->bEOF;
7079 
7080     JPEG_DEBUG_API_MSG("@1,  _pu8In_buf_ofs=0x%lx, _u32In_buf_left=0x%lx, _bEOF_flag=%d========\n",
7081     (MS_U32)_pu8In_buf_ofs, (MS_U32)_u32In_buf_left, _bEOF_flag);
7082     // Try to get more bytes.
7083     if(!JPEG_force_fill_read_buffer(_u32In_buf_left))
7084     {
7085         JPEG_DEBUG_API_MSG("JPEG_force_fill_read_buffer failed!!!\n");
7086         _u32In_buf_left = pInitParam->u32DecByteRead;
7087     }
7088     JPEG_DEBUG_API_MSG("@2,  _pu8In_buf_ofs=0x%lx, _u32In_buf_left=0x%lx, _bEOF_flag=%d========\n",
7089     (MS_U32)_pu8In_buf_ofs, (MS_U32)_u32In_buf_left, _bEOF_flag);
7090 
7091 
7092 #if SUPPORT_MPO_FORMAT
7093     if(_u32In_buf_MPO_left ==0xFFFFFFFFUL)
7094     {
7095         _pu8In_buf_MPO_ofs = (MS_U8*)_pu8In_buf;
7096         _u32In_buf_MPO_left = _u32In_buf_left;
7097         mpo_load_data = TRUE;
7098         bIsMPOFormat = FALSE;
7099         u32MPFOffset = 0;
7100         _u32App2MarkerOffset = 0;
7101 
7102         //Init MPO database
7103         JPEG_InitMPO();
7104     }
7105 #endif
7106 
7107     u32DataOffset = 0;
7108     u32SOFOffset = 0;
7109 
7110     JPEG_init_thumbnail();
7111 
7112     if (_u8DecodeType == E_JPEG_TYPE_MJPEG)
7113     {
7114         __bIsMjpeg = TRUE;
7115         __u32RealMjpegBase = MRC_BUFFER_ADDR;
7116         __u32TrickyMjpegBase = INTERNAL_BUFFER_ADDR;
7117 
7118         return E_JPEG_OKAY;
7119     }
7120 
7121     if(!JPEG_decode_init())
7122         return E_JPEG_FAILED;
7123 
7124     if (bMHEG5)
7125     {
7126         if(_Error_code == E_JPEG_UNSUPPORTED_MARKER)
7127             return E_JPEG_FAILED;
7128     }
7129 
7130 #ifndef CMODEL
7131     //init mempool for progressive decoding.
7132 #if SW_JPD_RGB_CMYK
7133     if (pInitParam->bInitMem && (_bProgressive_flag || MApi_JPEG_Is_RGB_CMYK()))
7134 #else
7135     if (pInitParam->bInitMem && _bProgressive_flag)
7136 #endif
7137 #endif
7138     {
7139         if(!JPEG_MEMORY_init_mempool((void *)(INTERNAL_BUFFER_ADDR + JPEG_DEFAULT_EXIF_SIZE)
7140                                 , (INTERNAL_BUFFER_SIZE - JPEG_DEFAULT_EXIF_SIZE)))
7141         {
7142             JPEG_DEBUG_API_MSG("JPEG_MEMORY_init_mempool FAIL!!\n");
7143             return E_JPEG_FAILED;
7144         }
7145         JPEG_DEBUG_API_MSG("JPEG_MEMORY_init_mempool OK!!\n");
7146     }
7147 
7148     //JPEG_GO;
7149 
7150 #ifdef CMODEL
7151     _bReady_flag = TRUE;
7152 #endif
7153 
7154     return E_JPEG_OKAY;
7155 }
7156 //------------------------------------------------------------------------------
7157 /********************************************************************/
7158 ///Decode JPEG header
7159 ///@return \b JPEG_Result : JPEG header decode status
7160 ///     - E_JPEG_OKAY : Success
7161 ///     - E_JPEG_FAILED : Failed
7162 /********************************************************************/
_MApi_JPEG_DecodeHdr(void)7163 JPEG_Result _MApi_JPEG_DecodeHdr(void)
7164 {
7165 #if SW_JPD_RGB_CMYK
7166     MS_U8 mcu_width, mcu_height;
7167     MS_U16 pic_width = _u16Image_x_size;
7168     MS_U16 pic_height = _u16Image_y_size;
7169     MS_U8 Y_VSF = _u8Comp_v_samp[0];
7170     MS_U8 Y_HSF = _u8Comp_h_samp[0];
7171 #endif
7172 
7173 #if PRINT_JPD_DECODE_TIME
7174     u32MeasureDecodeTime = MsOS_GetSystemTime();
7175     u32MeasureDecodeTimeSW = 0;
7176     u32MeasureDecodeTimeHW = 0;
7177 #endif
7178 
7179     if(_bIsInit == FALSE)
7180     {
7181         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7182         return E_JPEG_FAILED;
7183     }
7184 
7185     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7186 
7187 #ifdef JPD_LONGJUMP_SUPPORT
7188     if ( setjmp( _jmp_state ) )
7189     {
7190         return ( E_JPEG_FAILED );
7191     }
7192 #endif
7193 
7194     if(!JPEG_init_frame())
7195     {
7196         JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
7197         return E_JPEG_FAILED;
7198     }
7199 
7200     if ( _bProgressive_flag )
7201     {
7202 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
7203         if(!JPEG_init_progressive())
7204             return E_JPEG_FAILED;
7205 #endif
7206     }
7207     else
7208     {
7209         if(!JPEG_init_sequential())
7210             return E_JPEG_FAILED;
7211     }
7212 
7213 #if SW_JPD_RGB_CMYK
7214     if((E_JPEG_CMYK == gu8Scan_type)
7215     || (E_JPEG_RGB == gu8Scan_type))
7216     {
7217         if ( ( mcu_width = pic_width % ( Y_HSF * 8 ) ) )
7218         {
7219             pic_width += ( Y_HSF * 8 - mcu_width );
7220         }
7221 
7222         if ( ( mcu_height = pic_height% ( Y_VSF * 8 ) ) )
7223         {
7224             pic_height += ( Y_VSF * 8 - mcu_height );
7225         }
7226 
7227         JPEG_DEBUG_API_MSG("Y_HSF = %d and Y_VSF = %d\n",Y_HSF, Y_VSF);
7228 
7229         JPEG_DEBUG_API_MSG("mcu_width = %d and mcu_height = %d\n",mcu_width, mcu_height);
7230 
7231         _u16NonAlignmentImage_x_size = pic_width;
7232         _u16NonAlignmentImage_y_size = _u16Image_y_size;
7233         _u16Image_x_size = pic_width;
7234         _u16Image_y_size = pic_height;
7235 
7236         JPEG_DEBUG_API_MSG("Width = %d and Height = %d\n",JPEG_MAX_WIDTH, JPEG_MAX_HEIGHT);
7237 
7238         //Set the Scale down variable
7239         if(_u16Image_x_size > JPEG_MAX_WIDTH*4 || _u16Image_y_size>JPEG_MAX_HEIGHT*4)
7240         {
7241             _u8ScaleDownFactor = E_JPD_DOWNSCALE_EIGHTH;
7242             _u16Image_x_size= (_u16Image_x_size/ 64)*64;
7243             _u16Image_x_size = _u16Image_x_size/ 8;
7244             _u16Image_y_size = _u16Image_y_size/ 8;
7245             JPEG_DEBUG_API_MSG("down scale 1/8!!\n");
7246         }
7247         else if(_u16Image_x_size > JPEG_MAX_WIDTH*2 || _u16Image_y_size>JPEG_MAX_HEIGHT*2)
7248         {
7249             _u8ScaleDownFactor = E_JPD_DOWNSCALE_FOURTH;
7250             _u16Image_x_size= (_u16Image_x_size/ 32)*32;
7251             _u16Image_x_size = _u16Image_x_size/ 4;
7252             _u16Image_y_size = _u16Image_y_size/ 4;
7253              JPEG_DEBUG_API_MSG("down scale 1/4!!\n");
7254         }
7255         else if (_u16Image_x_size > JPEG_MAX_WIDTH || _u16Image_y_size>JPEG_MAX_HEIGHT)
7256         {
7257             _u8ScaleDownFactor = E_JPD_DOWNSCALE_HALF;
7258             _u16Image_x_size= (_u16Image_x_size/ 16)*16;
7259             _u16Image_x_size = _u16Image_x_size/ 2;
7260             _u16Image_y_size = _u16Image_y_size/ 2;
7261              JPEG_DEBUG_API_MSG("down scale 1/2!!\n");
7262         }
7263         else
7264         {
7265              JPEG_DEBUG_API_MSG("down scale 1/1!!\n");
7266             _u8ScaleDownFactor = E_JPD_DOWNSCALE_ORG;
7267 
7268         }
7269 
7270         _u16PaddingMcuNumber = (_u16Image_x_size << _u8ScaleDownFactor)/gu8Max_mcu_x_size;
7271         _u32Max_blocks_per_row = _u16PaddingMcuNumber* _u16Max_blocks_per_mcu;
7272 
7273         JPEG_DEBUG_API_MSG("down scale width : %d\n",_u16Image_x_size);
7274         JPEG_DEBUG_API_MSG("down scale height : %d\n",_u16Image_y_size);
7275         JPEG_DEBUG_API_MSG("down scale MCU : %d\n",_u16PaddingMcuNumber);
7276         JPEG_DEBUG_API_MSG("down scale MCU : %ld\n",_u32Max_blocks_per_row);
7277     }
7278 #endif
7279 
7280     if (__bIsMjpeg)
7281     {
7282         JPEG_DEBUG_API_MSG ("MJPEG Read buffer address RECOVER : 0x%lx - 0x%lx + 0x%lx ",
7283                 MS_VA2PA(MRC_BUFFER_ADDR), MS_VA2PA(__u32TrickyMjpegBase), MS_VA2PA(__u32RealMjpegBase));
7284         MRC_BUFFER_ADDR -= __u32TrickyMjpegBase;
7285         MRC_BUFFER_ADDR += __u32RealMjpegBase;
7286         JPEG_DEBUG_API_MSG ("= 0x%lx\n", MS_VA2PA(MRC_BUFFER_ADDR));
7287     }
7288 
7289     return ( E_JPEG_OKAY );
7290 }
7291 //------------------------------------------------------------------------------
7292 /********************************************************************/
7293 ///Decode JPEG data (baseline & progressive)
7294 ///@return \b JPEG_Result : JPEG decode status
7295 ///     - E_JPEG_DONE : For progressive decoding one line done.
7296 ///     - E_JPEG_OKAY : Success
7297 ///     - E_JPEG_FAILED : Failed
7298 /********************************************************************/
_MApi_JPEG_Decode(void)7299 JPEG_Result _MApi_JPEG_Decode(void)
7300 {
7301     JPEG_Result retVal = E_JPEG_FAILED;
7302 
7303     if(_bIsInit == FALSE)
7304     {
7305         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7306         return E_JPEG_FAILED;
7307     }
7308 
7309     JPEG_DEBUG_API_MSG("%s: _bProgressive_flag = %d\n", __FUNCTION__ , _bProgressive_flag);
7310 
7311     if(_bProgressive_flag)
7312     {
7313 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
7314         retVal = JPEG_Progressive_Decode();
7315 #endif
7316     }
7317     else
7318     {
7319 #if SW_JPD_RGB_CMYK
7320         if((E_JPEG_CMYK == gu8Scan_type)
7321         || (E_JPEG_RGB == gu8Scan_type))
7322         {
7323             retVal = msAPI_JPEG_Baseline_Decode();
7324             return retVal;
7325         }
7326 #endif
7327 
7328 #ifndef CMODEL
7329         JPEG_StartDecode();
7330 #if PRINT_JPD_DECODE_TIME
7331         u32MeasureDecodeTimeSW = MsOS_GetSystemTime()-u32MeasureDecodeTime;
7332         u32MeasureDecodeTime = MsOS_GetSystemTime();
7333 #endif
7334         if(_Error_code == E_JPEG_NO_ERROR)
7335             retVal = E_JPEG_OKAY;
7336         else
7337             retVal = E_JPEG_FAILED;
7338 #endif
7339     }
7340     return retVal;
7341 }
7342 
7343 #if LOG_DATA_TO_USB
_PrintOutputMem(MS_U32 u32addr,MS_U32 u32Size)7344 static void _PrintOutputMem(MS_U32 u32addr, MS_U32 u32Size)
7345 {
7346     u32Size = ALIGN_16(u32Size);
7347     static MS_U8 fileName2[]={'/','u','s','b','/','s','d','a','1','/','0','0', '0', '0', 'x','0','0', '0', '0', '_','0', '0', '0', '.', 'y', 'u', 'y', 'v',0};
7348     fileName2[10]=MApi_JPEG_GetWidth()/1000+'0';
7349     fileName2[11]=(MApi_JPEG_GetWidth()/100)%10+'0';
7350     fileName2[12]=(MApi_JPEG_GetWidth()/10)%10+'0';
7351     fileName2[13]=MApi_JPEG_GetWidth()%10+'0';
7352     fileName2[15]=MApi_JPEG_GetHeight()/1000+'0';
7353     fileName2[16]=(MApi_JPEG_GetHeight()/100)%10+'0';
7354     fileName2[17]=(MApi_JPEG_GetHeight()/10)%10+'0';
7355     fileName2[18]=MApi_JPEG_GetHeight()%10+'0';
7356 
7357     fileName2[22]++;
7358     if(fileName2[22] > '9')
7359     {
7360         fileName2[21]++;
7361         fileName2[22] = '0';
7362     }
7363     if(fileName2[21] > '9')
7364     {
7365         fileName2[20]++;
7366         fileName2[21] = '0';
7367     }
7368 
7369     FILE *outBinfp;
7370     if((outBinfp = fopen((char *)fileName2, "wb"))==NULL) {
7371         printf("Can't open output yuyv file\n");
7372     }
7373     else
7374     {
7375         printf("open output yuyv file success\n");
7376     }
7377 
7378     fwrite((void *)u32addr, 1, u32Size, outBinfp);
7379     fclose(outBinfp);
7380 
7381 }
7382 #endif
7383 
7384 //------------------------------------------------------------------------------
7385 /********************************************************************/
7386 /// Free all block of JPD. Complete destroy the decoder object.
7387 /// It could be called at any time.
7388 ///@return None
7389 /********************************************************************/
_MApi_JPEG_Exit(void)7390 void _MApi_JPEG_Exit(void)
7391 {
7392     if(_bIsInit == FALSE)
7393     {
7394         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7395         return;
7396     }
7397     else
7398     {
7399         _bIsInit = FALSE;
7400     }
7401     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7402 #ifndef CMODEL
7403     JPEG_DEBUG_API_MSG("MApi_JPEG_GetCurReadStatus : \nCurVidx = %d, CurRow = %d, CurCol = %d ",
7404         MDrv_JPD_GetCurVidx(),
7405         MDrv_JPD_GetCurRow(),
7406         MDrv_JPD_GetCurCol());
7407     JPEG_DEBUG_API_MSG("CurMRCAddr = 0x%lx\n", MDrv_JPD_GetCurMRCAddr());
7408 
7409     MDrv_JPD_PowerOff();
7410 #endif
7411     JPEG_free_all_blocks();
7412 
7413 #if SW_JPD_RGB_CMYK
7414     bEnableCMYK = TRUE;
7415     bEnableRGB = TRUE;
7416 #endif
7417 
7418 #if SUPPORT_MPO_FORMAT
7419     _u32In_buf_MPO_left = 0xFFFFFFFFUL;         // reset _u32In_buf_MPO_left to U32_MAX
7420     bIsMPOFormat = FALSE;
7421 #endif
7422 
7423 #if LOG_DATA_TO_USB
7424 fclose(logBinfp);
7425 logBinfp = NULL;
7426 #endif
7427 
7428     JPEG_DEBUG_API_MSG("%s: done!!\n", __FUNCTION__);
7429     return;
7430 }
7431 //------------------------------------------------------------------------------
7432 /********************************************************************/
7433 ///Get JPEG decoder error code
7434 ///@return JPEG decoder error code
7435 /********************************************************************/
_MApi_JPEG_GetErrorCode(void)7436 JPEG_ErrCode _MApi_JPEG_GetErrorCode(void)
7437 {
7438     if(_bIsInit == FALSE)
7439     {
7440         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7441         return E_JPEG_NOT_INIT;
7442     }
7443     JPEG_DEBUG_API_MSG("%s: ErrCode = %d\n", __FUNCTION__ , _Error_code);
7444     return _Error_code;
7445 }
7446 //------------------------------------------------------------------------------
7447 #if 0 //move to APP
7448 /********************************************************************/
7449 ///Preload buffer from JPEG content
7450 ///The calling order must be HIGH -> LOW -> HIGH -> LOW....
7451 ///or LOW -> HIGH -> LOW -> HIGH.
7452 ///Since the file offset is maintained out side, we assume it won't be out of order.
7453 ///@param u8Type \b IN indicate which parts of buffer wants to load
7454 ///@return true or false
7455 /********************************************************************/
7456 MS_BOOL MApi_JPEG_PreLoadBuffer(MS_U8 u8Type)
7457 {
7458     MS_S32 bytes_read;
7459     MS_U8 *pBuffer_addr, Position;
7460     MS_U32 buf_left = 0;
7461 
7462     JPEG_DEBUG_API_MSG("MApi_JPEG_PreLoadBuffer: type = %d\n", u8Type);
7463 #if 1
7464     if(MAPi_JPEG_ProcessEOF(u8Type))
7465         return TRUE;
7466 #else
7467     if ( _bEOF_flag )
7468     {
7469         JPEG_DEBUG_API_MSG("end of file!!\n");
7470         MDrv_UART_PutChar('t'); // DO NOT MARK THIS, THIS FUNCTION MUST BE CALLED. OR THE JPD DECODE WILL FAIL for LARGE FILES
7471         if(u8Type==E_JPEG_BUFFER_HIGH)
7472         {
7473             // clear MRC high portion read complete event
7474             //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBH_DONE);
7475             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE);
7476         }
7477         else
7478         {
7479             // clear MRC low portion read complete event
7480             //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBL_DONE);
7481             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBL_DONE);
7482         }
7483         return TRUE;
7484     }
7485 #endif
7486 
7487     if ( setjmp( _jmp_state ) )
7488     {
7489         JPEG_DEBUG_API_MSG("ERROR\n");
7490         return ( FALSE );
7491     }
7492 
7493     if(u8Type==E_JPEG_BUFFER_HIGH)
7494     {
7495         pBuffer_addr = _pu8In_buf + (MRC_BUFFER_SIZE/2);
7496         Position = 1;
7497     }
7498     else
7499     {
7500         pBuffer_addr = _pu8In_buf;
7501         Position = 0;
7502     }
7503 
7504 #if 1
7505 {
7506     MS_BOOL EOF_flag = FALSE;
7507     do
7508     {
7509         bytes_read = JPEG_FILE_read( pBuffer_addr + buf_left, MRC_BUFFER_SIZE/2 - buf_left, &EOF_flag, _pStream, Position );
7510 
7511         if ( bytes_read == -1 )
7512         {
7513             JPEG_terminate( E_JPEG_STREAM_READ );
7514         }
7515 
7516         buf_left += bytes_read;
7517 //        _Total_Decoded_Size += bytes_read;
7518     } while(( buf_left < MRC_BUFFER_SIZE/2 ) && ( !EOF_flag ));
7519     MAPi_JPEG_FileReadInfo(buf_left, EOF_flag);
7520 }
7521 #else
7522     do
7523     {
7524         bytes_read = JPEG_FILE_read( pBuffer_addr + buf_left, MRC_BUFFER_SIZE/2 - buf_left, &_bEOF_flag, _pStream, Position );
7525 
7526         if ( bytes_read == -1 )
7527         {
7528             JPEG_terminate( E_JPEG_STREAM_READ );
7529         }
7530 
7531         buf_left += bytes_read;
7532 ////        _Total_Decoded_Size += bytes_read;
7533     } while(( buf_left < MRC_BUFFER_SIZE/2 ) && ( !_bEOF_flag ));
7534 #endif
7535 
7536 
7537 #if 1
7538     MApi_JPEG_SetMRBufferValid(u8Type);
7539 #else
7540     if(u8Type==E_JPEG_BUFFER_HIGH)
7541     {
7542         // clear MRC high portion read complete event
7543         //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBH_DONE);
7544         MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE);
7545         // mark high portion valid
7546         //MDrv_Write2Byte( BK_JPD_MCONFIG, (MDrv_Read2Byte(BK_JPD_MCONFIG) & ~0x0003) | JPD_H_VLD);
7547         MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | JPD_H_VLD);
7548     }
7549     else
7550     {
7551         // clear MRC low portion read complete event
7552         //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBL_DONE);
7553         MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBL_DONE);
7554         // mark low portion valid
7555         //MDrv_Write2Byte( BK_JPD_MCONFIG, (MDrv_Read2Byte(BK_JPD_MCONFIG) & ~0x0003) | JPD_L_VLD);
7556         MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | JPD_L_VLD);
7557     }
7558 #endif
7559     return TRUE;
7560 }
7561 #else // for APP reference
7562 #if 0
7563 MS_BOOL MApi_JPEG_PreLoadBuffer(MS_U8 u8MRBuffType, MS_U8* u8In_buf, MS_U32 MaxBufSize)
7564 {
7565     MS_S32 bytes_read;
7566     MS_U8 *pBuffer_addr, Position;
7567     MS_U32 buf_left = 0;
7568     MS_BOOL EOF_flag = FALSE;
7569 
7570     if(MAPi_JPEG_ProcessEOF(u8MRBuffType))
7571         return TRUE;
7572 
7573     if(MaxBufSize > (MRC_BUFFER_SIZE/2))
7574     {
7575         return FALSE;
7576     }
7577 
7578     if(u8MRBuffType==E_JPEG_BUFFER_HIGH)
7579     {
7580         pBuffer_addr = u8In_buf + (MRC_BUFFER_SIZE/2);
7581         Position = 1;
7582     }
7583     else
7584     {
7585         pBuffer_addr = u8In_buf;
7586         Position = 0;
7587     }
7588 
7589     do
7590     {
7591 //        bytes_read = JPEG_FILE_read( pBuffer_addr + buf_left, MRC_BUFFER_SIZE/2 - buf_left, &EOF_flag, _pStream, Position );
7592         bytes_read = JPEG_FILE_read( pBuffer_addr + buf_left, MaxBufSize - buf_left, &EOF_flag, _pStream, Position );
7593 
7594         if ( bytes_read == -1 )
7595         {
7596             MApi_JPEG_SetErrCode( E_JPEG_STREAM_READ );
7597             return FALSE;
7598         }
7599 
7600         buf_left += bytes_read;
7601 //        _Total_Decoded_Size += bytes_read;
7602     } while(( buf_left < MaxBufSize ) && ( !EOF_flag ));
7603     MAPi_JPEG_FileReadInfo(buf_left, EOF_flag);
7604     MApi_JPEG_SetMRBufferValid(u8MRBuffType);
7605     return TRUE;
7606 }
7607 #endif
7608 #endif
7609 //------------------------------------------------------------------------------
7610 /********************************************************************/
7611 ///Get JPD EVENT FLAG
7612 ///@return \b JPEG_Event : JPEG event flag
7613 /********************************************************************/
_MApi_JPEG_GetJPDEventFlag(void)7614 JPEG_Event _MApi_JPEG_GetJPDEventFlag(void)
7615 {
7616     if(_bIsInit == FALSE)
7617     {
7618         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7619         return E_JPEG_EVENT_DEC_ERROR_MASK;
7620     }
7621 
7622 #if SW_JPD_RGB_CMYK
7623     // It's pure software decode
7624     if((E_JPEG_CMYK == gu8Scan_type)
7625     || (E_JPEG_RGB == gu8Scan_type))
7626     {
7627         return E_JPEG_EVENT_DEC_DONE;
7628     }
7629 #endif
7630 
7631 #ifndef CMODEL
7632     {
7633         JPEG_Event reg_val = (JPEG_Event)MDrv_JPD_GetEventFlag();
7634         if(reg_val!=0)
7635         {
7636         JPEG_DEBUG_API_MSG("%s: reg_val = 0x%04X\n", __FUNCTION__ , reg_val);
7637         }
7638         if(E_JPEG_EVENT_DEC_DONE & reg_val)
7639         {
7640 #if PRINT_JPD_DECODE_TIME
7641             u32MeasureDecodeTimeHW = MsOS_GetSystemTime()-u32MeasureDecodeTime;
7642             printf_red("SW decode header:[%ld]ms, HW decode: [%ld]ms\n", u32MeasureDecodeTimeSW, u32MeasureDecodeTimeHW);
7643 #endif
7644             if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API)
7645             {
7646                 verJPD_CRC32_Init();
7647                 verJPD_CRC32_Update((MS_U8 *)MWC_BUFFER_ADDR,
7648                     (MS_U32)MApi_JPEG_GetWidth()*MApi_JPEG_GetHeight()*2);
7649                 printf_red("[%dx%d], u32CRCResult=0x%lx\n",MApi_JPEG_GetWidth(), MApi_JPEG_GetHeight(), verJPD_CRC32_GetResult());
7650             }
7651 
7652 #if LOG_DATA_TO_USB
7653             if(MApi_JPEG_GetWidth() <=JPEG_MAX_WIDTH
7654                 && MApi_JPEG_GetHeight() <=JPEG_MAX_HEIGHT)
7655             {
7656                 MS_U32 u32Size;
7657                 u32Size = MApi_JPEG_GetWidth()*MApi_JPEG_GetHeight()*2;
7658                 printf("[%dx%d], MWC_BUFFER_ADDR=0x%lx, size=0x%lx", MApi_JPEG_GetWidth(),
7659                     MApi_JPEG_GetHeight(), MS_VA2PA(MWC_BUFFER_ADDR),u32Size);
7660                 _PrintOutputMem(MWC_BUFFER_ADDR, u32Size);
7661             }
7662 #endif
7663         }
7664         return reg_val;
7665     }
7666 #else
7667     return E_JPEG_EVENT_DEC_DONE;
7668 #endif
7669 }
7670 //------------------------------------------------------------------------------
7671 /********************************************************************/
7672 ///Set JPD EVENT FLAG
7673 ///@param -eEvtVal \b IN : JPEG_Event
7674 ///@return None
7675 /********************************************************************/
_MApi_JPEG_SetJPDEventFlag(JPEG_Event eEvtVal)7676 void _MApi_JPEG_SetJPDEventFlag(JPEG_Event eEvtVal)
7677 {
7678     if(_bIsInit == FALSE)
7679     {
7680         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7681         return;
7682     }
7683 
7684 #if SW_JPD_RGB_CMYK
7685     // It's pure software decode
7686     if((E_JPEG_CMYK == gu8Scan_type)
7687     || (E_JPEG_RGB == gu8Scan_type))
7688     {
7689         return;
7690     }
7691 #endif
7692 
7693 #ifndef CMODEL
7694     MDrv_JPD_SetEventFlag((MS_U16)eEvtVal);
7695 #endif
7696     return;
7697 }
7698 //------------------------------------------------------------------------------
7699 /********************************************************************/
7700 ///Reset JPD -- Reset must be called before trigger JPD\n
7701 ///This function always issue pause command and wait for both MRC & MWC\n
7702 ///becoming inactive, then reset JPD.\n
7703 ///\b NOTE: The pause function can be only used by Pluto/T1/T2... not include ERIS.
7704 ///@return None
7705 /********************************************************************/
_MApi_JPEG_Rst(void)7706 void _MApi_JPEG_Rst(void)
7707 {
7708     if(_bIsInit == FALSE)
7709     {
7710         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7711         return;
7712     }
7713     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7714 #ifndef CMODEL
7715     JPEG_DEBUG_API_MSG("MApi_JPEG_GetCurReadStatus : \nCurVidx = %d, CurRow = %d, CurCol = %d ",
7716         MDrv_JPD_GetCurVidx(),
7717         MDrv_JPD_GetCurRow(),
7718         MDrv_JPD_GetCurCol());
7719     JPEG_DEBUG_API_MSG("CurMRCAddr = 0x%lx\n", MDrv_JPD_GetCurMRCAddr());
7720 
7721     MDrv_JPD_Rst();
7722 #endif
7723     JPEG_DEBUG_API_MSG("%s:done!!\n", __FUNCTION__);
7724     return;
7725 }
7726 //------------------------------------------------------------------------------
7727 /********************************************************************/
7728 ///Power On JPEG decoder
7729 ///@return None
7730 /********************************************************************/
_MApi_JPEG_PowerOn(void)7731 void _MApi_JPEG_PowerOn(void)
7732 {
7733     if(_bIsInit == FALSE)
7734     {
7735         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7736         return;
7737     }
7738     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7739 #ifndef CMODEL
7740     MDrv_JPD_PowerOn();
7741 #endif
7742     return;
7743 }
7744 //------------------------------------------------------------------------------
7745 /********************************************************************/
7746 ///Power Off JPEG decoder
7747 ///@return None
7748 /********************************************************************/
_MApi_JPEG_PowerOff(void)7749 void _MApi_JPEG_PowerOff(void)
7750 {
7751     if(_bIsInit == FALSE)
7752     {
7753         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7754         return;
7755     }
7756     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7757 #ifndef CMODEL
7758     MDrv_JPD_PowerOff();
7759 #endif
7760     return;
7761 }
7762 //------------------------------------------------------------------------------
7763 /********************************************************************/
7764 ///Get current vertical line index written to memory
7765 ///@return Current vertical line index written to memory
7766 /********************************************************************/
_MApi_JPEG_GetCurVidx(void)7767 MS_U16 _MApi_JPEG_GetCurVidx(void) //new
7768 {
7769     if(_bIsInit == FALSE)
7770     {
7771         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7772         return 0;
7773     }
7774     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7775 #ifndef CMODEL
7776     return MDrv_JPD_GetCurVidx();
7777 #else
7778     return 0;
7779 #endif
7780 }
7781 //------------------------------------------------------------------------------
7782 /********************************************************************/
7783 ///Check current JPEG image is progressive or not
7784 ///@return TRUE / FALSE
7785 /// - TRUE : progressive
7786 /// - FALSE : baseline
7787 /********************************************************************/
_MApi_JPEG_IsProgressive(void)7788 MS_BOOL _MApi_JPEG_IsProgressive(void)
7789 {
7790     if(_bIsInit == FALSE)
7791     {
7792         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7793         return FALSE;
7794     }
7795     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _bProgressive_flag);
7796     return (_bProgressive_flag);
7797 }
7798 //------------------------------------------------------------------------------
7799 /********************************************************************/
7800 ///Check current JPEG image has thumbnail or not
7801 ///@return TRUE / FALSE
7802 /// - TRUE : thumbnail found
7803 /// - FALSE : no thumbnail
7804 /********************************************************************/
_MApi_JPEG_ThumbnailFound(void)7805 MS_BOOL _MApi_JPEG_ThumbnailFound(void)
7806 {
7807     if(_bIsInit == FALSE)
7808     {
7809         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7810         return FALSE;
7811     }
7812     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _bThumbnailFound);
7813     return (_bThumbnailFound);
7814 }
7815 //------------------------------------------------------------------------------
7816 /********************************************************************/
7817 ///The width may be the thumbnail or original image size, it based on decoding mode
7818 ///@return width
7819 /********************************************************************/
_MApi_JPEG_GetWidth(void)7820 MS_U16 _MApi_JPEG_GetWidth(void)
7821 {
7822     if(_bIsInit == FALSE)
7823     {
7824         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7825         return 0;
7826     }
7827     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16Image_x_size);
7828     return (_u16Image_x_size);
7829 }
7830 //------------------------------------------------------------------------------
7831 /********************************************************************/
7832 ///The height may be the thumbnail or original image size, it based on decoding mode
7833 ///@return height
7834 /********************************************************************/
_MApi_JPEG_GetHeight(void)7835 MS_U16 _MApi_JPEG_GetHeight(void)
7836 {
7837     if(_bIsInit == FALSE)
7838     {
7839         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7840         return 0;
7841     }
7842     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16Image_y_size);
7843     return (_u16Image_y_size);
7844 }
7845 //------------------------------------------------------------------------------
7846 /********************************************************************/
7847 ///Get the original width of this JPEG file after alignment
7848 ///@return original width
7849 /********************************************************************/
_MApi_JPEG_GetOriginalWidth(void)7850 MS_U16 _MApi_JPEG_GetOriginalWidth(void)
7851 {
7852     if(_bIsInit == FALSE)
7853     {
7854         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7855         return 0;
7856     }
7857     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16OriginalImage_x_size);
7858     return (_u16OriginalImage_x_size);
7859 }
7860 //------------------------------------------------------------------------------
7861 /********************************************************************/
7862 ///Get the original height of this JPEG file after alignment
7863 ///@return original height
7864 /********************************************************************/
_MApi_JPEG_GetOriginalHeight(void)7865 MS_U16 _MApi_JPEG_GetOriginalHeight(void)
7866 {
7867     if(_bIsInit == FALSE)
7868     {
7869         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7870         return 0;
7871     }
7872     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16OriginalImage_y_size);
7873     return (_u16OriginalImage_y_size);
7874 }
7875 //------------------------------------------------------------------------------
7876 /********************************************************************/
7877 ///Get the original width before alignment
7878 ///@return non-alignment width
7879 /********************************************************************/
_MApi_JPEG_GetNonAlignmentWidth(void)7880 MS_U16 _MApi_JPEG_GetNonAlignmentWidth(void)
7881 {
7882     if(_bIsInit == FALSE)
7883     {
7884         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7885         return 0;
7886     }
7887     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16NonAlignmentImage_x_size);
7888     return (_u16NonAlignmentImage_x_size);
7889 }
7890 //------------------------------------------------------------------------------
7891 /********************************************************************/
7892 ///Get the original height before alignment
7893 ///@return non-alignment height
7894 /********************************************************************/
_MApi_JPEG_GetNonAlignmentHeight(void)7895 MS_U16 _MApi_JPEG_GetNonAlignmentHeight(void)
7896 {
7897     if(_bIsInit == FALSE)
7898     {
7899         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7900         return 0;
7901     }
7902     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16NonAlignmentImage_y_size);
7903     return (_u16NonAlignmentImage_y_size);
7904 }
7905 //------------------------------------------------------------------------------
7906 /********************************************************************/
7907 ///Get the displayed pitch of JPEG image
7908 ///@return displayed pitch
7909 /********************************************************************/
_MApi_JPEG_GetAlignedPitch(void)7910 MS_U16 _MApi_JPEG_GetAlignedPitch(void)
7911 {
7912     if(_bIsInit == FALSE)
7913     {
7914         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7915         return 0;
7916     }
7917     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16AlignedImagePitch);
7918     return (_u16AlignedImagePitch);
7919 }
7920 
_MApi_JPEG_GetAlignedPitch_H(void)7921 MS_U16 _MApi_JPEG_GetAlignedPitch_H(void)
7922 {
7923     if(_bIsInit == FALSE)
7924     {
7925         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7926         return 0;
7927     }
7928     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16AlignedImagePitch_H);
7929     return (_u16AlignedImagePitch_H);
7930 }
7931 
7932 
7933 //------------------------------------------------------------------------------
7934 /********************************************************************/
7935 ///Get the displayed width of JPEG image
7936 ///@return displayed width
7937 /********************************************************************/
_MApi_JPEG_GetAlignedWidth(void)7938 MS_U16 _MApi_JPEG_GetAlignedWidth(void)
7939 {
7940     if(_bIsInit == FALSE)
7941     {
7942         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7943         return 0;
7944     }
7945     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16AlignedImageWidth);
7946     return (_u16AlignedImageWidth);
7947 }
7948 //------------------------------------------------------------------------------
7949 /********************************************************************/
7950 ///Get the displayed height of JPEG image
7951 ///@return displayed height
7952 /********************************************************************/
_MApi_JPEG_GetAlignedHeight(void)7953 MS_U16 _MApi_JPEG_GetAlignedHeight(void)
7954 {
7955     if(_bIsInit == FALSE)
7956     {
7957         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7958         return 0;
7959     }
7960     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16AlignedImageHeight);
7961     return (_u16AlignedImageHeight);
7962 }
7963 //------------------------------------------------------------------------------
7964 /********************************************************************/
7965 ///Get scale down factor\n
7966 ///Depending on the real picture width & height, it will automatically set scale down\n
7967 ///factor to meet maximum JPEG width & height allowed.
7968 ///@return scale down factor
7969 /********************************************************************/
_MApi_JPEG_GetScaleDownFactor(void)7970 MS_U8 _MApi_JPEG_GetScaleDownFactor(void)
7971 {
7972     if(_bIsInit == FALSE)
7973     {
7974         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7975         return 0;
7976     }
7977 
7978 #if SW_JPD_RGB_CMYK
7979     if((E_JPEG_CMYK == gu8Scan_type)
7980     || (E_JPEG_RGB == gu8Scan_type))
7981     {
7982         JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u8ScaleDownFactor*2);
7983         return (_u8ScaleDownFactor*2);
7984     }
7985 #endif
7986     {
7987         JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u8ScaleDownFactor);
7988         return (_u8ScaleDownFactor);
7989     }
7990 }
7991 //------------------------------------------------------------------------------
7992 /********************************************************************/
7993 /// Set Max decoding resolution
7994 ///@param -u16Width \b IN : indicate max decoding width
7995 ///@param -u16Height \b IN : indicate max decoding height
7996 ///@return None
7997 /********************************************************************/
_MApi_JPEG_SetMaxDecodeResolution(MS_U16 u16Width,MS_U16 u16Height)7998 void _MApi_JPEG_SetMaxDecodeResolution(MS_U16 u16Width, MS_U16 u16Height)
7999 {
8000     JPEG_DEBUG_API_MSG("%s: Width = %d, Height = %d\n", __FUNCTION__, u16Width, u16Height);
8001     JPEG_MAX_WIDTH = u16Width;
8002     JPEG_MAX_HEIGHT = u16Height;
8003     return;
8004 }
8005 //------------------------------------------------------------------------------
8006 /********************************************************************/
8007 /// Set Max decoding resolution of Progressive JPEG
8008 ///@param -u16ProWidth \b IN : indicate max decoding width
8009 ///@param -u16ProHeight \b IN : indicate max decoding height
8010 ///@return None
8011 /********************************************************************/
_MApi_JPEG_SetProMaxDecodeResolution(MS_U16 u16ProWidth,MS_U16 u16ProHeight)8012 void _MApi_JPEG_SetProMaxDecodeResolution(MS_U16 u16ProWidth, MS_U16 u16ProHeight)
8013 {
8014     JPEG_DEBUG_API_MSG("%s: ProWidth = %d, ProHeight = %d\n", __FUNCTION__, u16ProWidth, u16ProHeight);
8015     JPEG_PRO_MAX_WIDTH = u16ProWidth;
8016     JPEG_PRO_MAX_HEIGHT = u16ProHeight;
8017     return;
8018 }
8019 //------------------------------------------------------------------------------
8020 /********************************************************************/
8021 /// Valid the JPEG read buffer flag
8022 ///@param -u8MRBuffType \b IN : the type of JPEG read buffer
8023 ///@return None
8024 /********************************************************************/
_MApi_JPEG_SetMRBufferValid(JPEG_BuffLoadType u8MRBuffType)8025 void _MApi_JPEG_SetMRBufferValid(JPEG_BuffLoadType u8MRBuffType)
8026 {
8027     if(_bIsInit == FALSE)
8028     {
8029         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8030         return;
8031     }
8032     JPEG_DEBUG_API_MSG("%s: MRBuffType = %d\n", __FUNCTION__ , u8MRBuffType);
8033 #ifndef CMODEL
8034     switch(u8MRBuffType)
8035     {
8036         case E_JPEG_BUFFER_HIGH:
8037             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE);
8038             MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | JPD_H_VLD);
8039             break;
8040         case E_JPEG_BUFFER_LOW:
8041             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBL_DONE);
8042             MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | JPD_L_VLD);
8043             break;
8044         default:
8045             break;
8046     }
8047 #endif
8048     return;
8049 }
8050 //------------------------------------------------------------------------------
8051 /********************************************************************/
8052 /// Set JPEG EOF flag and the total byte-read of JPEG file
8053 ///@param -u32DataRead \b IN : the amount of byte of file reading
8054 ///@param -bEOFflag \b IN : the EOF flag of file reading
8055 ///@return None
8056 /********************************************************************/
_MApi_JPEG_UpdateReadInfo(MS_U32 u32DataRead,MS_BOOL bEOFflag)8057 void _MApi_JPEG_UpdateReadInfo(MS_U32 u32DataRead, MS_BOOL bEOFflag)
8058 {
8059     if(_bIsInit == FALSE)
8060     {
8061         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8062         return;
8063     }
8064     JPEG_DEBUG_API_MSG("%s: DataRead = %ld, EOFflag = %d\n", __FUNCTION__ , u32DataRead, bEOFflag);
8065 ////    _Total_Decoded_Size += data_read;
8066     _u32Total_bytes_read += u32DataRead;
8067     _bEOF_flag = bEOFflag;
8068 
8069 #ifndef CMODEL
8070     // Set buffer valid to avoid last data not decoded done
8071     if(bEOFflag)
8072     {
8073         MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | (JPD_H_VLD |JPD_L_VLD));
8074     }
8075 #endif
8076     return;
8077 }
8078 //------------------------------------------------------------------------------
8079 /********************************************************************/
8080 /// Process JPEG read buffer flag when EOF.
8081 ///@param -u8MRBuffType \b IN : The type of JPEG read buffer
8082 ///@return None
8083 /********************************************************************/
_MApi_JPEG_ProcessEOF(JPEG_BuffLoadType u8MRBuffType)8084 void _MApi_JPEG_ProcessEOF(JPEG_BuffLoadType u8MRBuffType)
8085 {
8086     if(_bIsInit == FALSE)
8087     {
8088         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8089         return;
8090     }
8091     JPEG_DEBUG_API_MSG("%s: end of file!! MRBuffType = %d\n", __FUNCTION__ , u8MRBuffType);
8092 ////    MDrv_UART_PutChar('t'); // DO NOT MARK THIS, THIS FUNCTION MUST BE CALLED. OR THE JPD DECODE WILL FAIL for LARGE FILES
8093 #ifndef CMODEL
8094     if(u8MRBuffType == E_JPEG_BUFFER_HIGH)
8095     {
8096         // clear MRC high portion read complete event
8097         //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBH_DONE);
8098         MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE);
8099     }
8100     else
8101     {
8102         // clear MRC low portion read complete event
8103         //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBL_DONE);
8104         MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBL_DONE);
8105     }
8106 
8107     // Set buffer valid to avoid last data not decoded done
8108     MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | (JPD_H_VLD |JPD_L_VLD));
8109 #endif
8110     return;
8111 }
8112 //------------------------------------------------------------------------------
8113 /********************************************************************/
8114 /// Set JPEG Error Code if something error on upper layer.
8115 ///@param -ErrCode \b IN : The error type of JPEG decoder
8116 ///@return None
8117 /********************************************************************/
_MApi_JPEG_SetErrCode(JPEG_ErrCode ErrCode)8118 void _MApi_JPEG_SetErrCode(JPEG_ErrCode ErrCode)
8119 {
8120     JPEG_DEBUG_API_ERR("%s: ErrCode = %d\n", __FUNCTION__ , ErrCode);
8121     _Error_code = ErrCode;
8122 ////    JPEG_free_all_blocks();
8123     return;
8124 }
8125 //------------------------------------------------------------------------------
8126 /********************************************************************/
8127 /// Set JPEG debug level.
8128 ///@param -u8DbgLevel \b IN : The JPEG debug level, refer to JPEG_DbgLevel.
8129 ///@return None
8130 /********************************************************************/
_MApi_JPEG_SetDbgLevel(MS_U8 u8DbgLevel)8131 void _MApi_JPEG_SetDbgLevel(MS_U8 u8DbgLevel)
8132 {
8133     JPEG_DEBUG_API_MSG("%s: DbgLevel = 0x%04X\n", __FUNCTION__, u8DbgLevel);
8134     _u8JPEG_ApiDbgLevel = u8DbgLevel;
8135 #ifndef CMODEL
8136     MDrv_JPD_SetDbgLevel(u8DbgLevel);
8137 #endif
8138     return;
8139 }
8140 //------------------------------------------------------------------------------
8141 /********************************************************************/
8142 /// Get JPEG debug level.
8143 ///@return JPEG debug level, refer to JPEG_DbgLevel.
8144 /********************************************************************/
_MApi_JPEG_GetDbgLevel(void)8145 MS_U8 _MApi_JPEG_GetDbgLevel(void)
8146 {
8147     JPEG_DEBUG_API_MSG("%s: DbgLevel = 0x%04X\n", __FUNCTION__, _u8JPEG_ApiDbgLevel);
8148     return _u8JPEG_ApiDbgLevel;
8149 }
8150 //------------------------------------------------------------------------------
8151 /********************************************************************/
8152 /// Set MHEG5 Flag.
8153 ///@param -bEnable \b IN :
8154 ///@return None
8155 /********************************************************************/
_MApi_JPEG_SetMHEG5(MS_BOOL bEnable)8156 void _MApi_JPEG_SetMHEG5(MS_BOOL bEnable)
8157 {
8158     bMHEG5 = bEnable;
8159 }
8160 //------------------------------------------------------------------------------
8161 /********************************************************************/
8162 /// Get JPEG information
8163 ///@param -pJPEG_Info \b OUT : JPEG information\n
8164 /// structure {\n
8165 ///    MS_U8* \b pu8DrvVer : JPEG DRV version\n
8166 ///    MS_U8* \b pu8HalVer : JPEG HAL version\n
8167 ///    MS_U8* \b pu8FwVer : JPEG FW version\n
8168 ///    JPEG_DrvCap \b stDrvCap : JPEG driver capability\n
8169 ///           };
8170 ///@return None
8171 /********************************************************************/
_MApi_JPEG_GetInfo(JPEG_Info * pJPEG_Info)8172 void _MApi_JPEG_GetInfo(JPEG_Info *pJPEG_Info)
8173 {
8174 #ifndef CMODEL
8175     JPD_Info DrvInfo;
8176     const MSIF_Version *DrvVer;
8177     MDrv_JPD_GetInfo(&DrvInfo);
8178     MDrv_JPD_GetLibVer(&DrvVer);
8179     pJPEG_Info->pu8DrvVer = DrvVer;
8180     pJPEG_Info->pu8HalVer = DrvInfo.pu8HalVer;
8181     pJPEG_Info->pu8FwVer = DrvInfo.pu8FwVer;
8182     pJPEG_Info->stDrvCap.bBaseline = DrvInfo.stCap.bBaseline;
8183     pJPEG_Info->stDrvCap.bProgressive = DrvInfo.stCap.bProgressive;
8184     pJPEG_Info->stDrvCap.bMJPEG = DrvInfo.stCap.bMJPEG;
8185     pJPEG_Info->stDrvCap.u16MaxWidth = JPEG_MAX_WIDTH;
8186     pJPEG_Info->stDrvCap.u16MaxHeight = JPEG_MAX_HEIGHT;
8187     pJPEG_Info->stDrvCap.u16MaxProWidth = JPEG_PRO_MAX_WIDTH;
8188     pJPEG_Info->stDrvCap.u16MaxProHeight = JPEG_PRO_MAX_HEIGHT;
8189 
8190     JPEG_DEBUG_API_MSG("JPD %04x, %04x, %04x, %s\n",
8191         DrvVer->DDI.customer, DrvVer->DDI.model, DrvVer->DDI.chip, &(DrvVer->DDI.cpu));
8192     JPEG_DEBUG_API_MSG("pu8HalVer = %s, pu8FwVer = %s\n", pJPEG_Info->pu8HalVer, pJPEG_Info->pu8FwVer);
8193     JPEG_DEBUG_API_MSG("%s: DrvCap_BL = %d, DrvCap_PRO = %d, DrvCap_MJPEG = %d\n", __FUNCTION__,
8194         pJPEG_Info->stDrvCap.bBaseline, pJPEG_Info->stDrvCap.bProgressive, pJPEG_Info->stDrvCap.bMJPEG);
8195     JPEG_DEBUG_API_MSG("%s: MaxWidth = %d, MaxHeight = %d\n", __FUNCTION__,
8196         pJPEG_Info->stDrvCap.u16MaxWidth, pJPEG_Info->stDrvCap.u16MaxHeight);
8197     JPEG_DEBUG_API_MSG("%s: MaxProWidth = %d, MaxProHeight = %d\n", __FUNCTION__,
8198         pJPEG_Info->stDrvCap.u16MaxProWidth, pJPEG_Info->stDrvCap.u16MaxProHeight);
8199 #endif
8200     return;
8201 }
8202 //------------------------------------------------------------------------------
8203 /********************************************************************/
8204 /// Get JPEG status.
8205 ///@param -pJPEG_Status \b OUT : JPEG status\n
8206 /// structure {\n
8207 ///    MS_U32 \b u32CurMRCAddr : JPD current decode address\n
8208 ///    MS_U16 \b u16CurVidx : JPD current decode vertical index\n
8209 ///    MS_U16 \b u16CurRow : JPD current decode row\n
8210 ///    MS_U16 \b u16CurCol : JPD current decode column\n
8211 ///    MS_BOOL \b bDrvBusy : JPEG DRV busy status\n
8212 ///    MS_BOOL \b bIsrEnable : JPD ISR status\n
8213 ///           };
8214 ///@return None
8215 /********************************************************************/
_MApi_JPEG_GetStatus(JPEG_Status * pJPEG_Status)8216 void _MApi_JPEG_GetStatus(JPEG_Status *pJPEG_Status)
8217 {
8218 #ifndef CMODEL
8219     JPD_Status *pDrvStatus = MDrv_JPD_GetStatus();
8220     pJPEG_Status->u32CurMRCAddr = pDrvStatus->u32CurMRCAddr;
8221     pJPEG_Status->u16CurVidx = pDrvStatus->u16CurVidx;
8222     pJPEG_Status->u16CurRow = pDrvStatus->u16CurRow;
8223     pJPEG_Status->u16CurCol = pDrvStatus->u16CurCol;
8224     pJPEG_Status->bDrvBusy = pDrvStatus->bIsBusy;
8225     pJPEG_Status->bIsrEnable = pDrvStatus->bIsrEnable;
8226     JPEG_DEBUG_API_MSG("%s: Drv_busy = %d, Isr_status = %d\n", __FUNCTION__ , pJPEG_Status->bDrvBusy, pJPEG_Status->bIsrEnable);
8227     JPEG_DEBUG_API_MSG("%s: CurVidx = %d, CurRow = %d, CurCol = %d\n", __FUNCTION__ ,
8228                         pJPEG_Status->u16CurVidx, pJPEG_Status->u16CurRow, pJPEG_Status->u16CurCol);
8229     JPEG_DEBUG_API_MSG("%s: CurMRCAddr = 0x%lx\n", __FUNCTION__ , pJPEG_Status->u32CurMRCAddr);
8230 #endif
8231     return;
8232 }
8233 //------------------------------------------------------------------------------
8234 /********************************************************************/
8235 /// Get JPEG Version.
8236 ///@param -ppVersion \b OUT : JPEG Version
8237 ///@return \b JPEG_Result
8238 ///     - E_JPEG_OKAY : Success
8239 ///     - E_JPEG_FAILED : Failed
8240 /********************************************************************/
_MApi_JPEG_GetLibVer(const MSIF_Version ** ppVersion)8241 JPEG_Result _MApi_JPEG_GetLibVer(const MSIF_Version **ppVersion)
8242 {
8243     if (!ppVersion)
8244     {
8245         return E_JPEG_FAILED;
8246     }
8247     *ppVersion = &_api_jpeg_version;
8248     JPEG_DEBUG_API_MSG("%s: JPEG %04x, %04x, %04x, %s\n", __FUNCTION__ ,
8249         _api_jpeg_version.DDI.customer, _api_jpeg_version.DDI.model, _api_jpeg_version.DDI.chip, &(_api_jpeg_version.DDI.cpu));
8250     return E_JPEG_OKAY;
8251 }
8252 //------------------------------------------------------------------------------
8253 /********************************************************************/
8254 /// For H/W bug, some cases can not exit after decode done, Check
8255 /// Vidx to exit.
8256 ///@return \b JPEG_Result
8257 /// - E_JPEG_OKAY : Vidx checking is successful.
8258 /// - E_JPEG_FAILED : Vidx checking is failed.
8259 /********************************************************************/
_MApi_JPEG_HdlVidxChk(void)8260 JPEG_Result _MApi_JPEG_HdlVidxChk(void)
8261 {
8262 #ifndef CMODEL
8263     MS_U16 cur_vidx = 0;
8264     if(_bIsInit == FALSE)
8265     {
8266         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8267         _JPD_IsDecoding = FALSE;
8268         return E_JPEG_FAILED;
8269     }
8270 
8271     if(FALSE == _JPD_IsDecoding)
8272     {
8273         _JPD_IsDecoding = TRUE;
8274 #if MSOS_GET_SYSTEM_TIME
8275         _JPD_ReCheckTime = MsOS_GetSystemTime();
8276 #else
8277         _JPD_ReCheckTime = 0;
8278 #endif
8279     }
8280 
8281     //For H/W bug, some cases can not exit after decode done. Check Vidx to exit.
8282     cur_vidx = MDrv_JPD_GetCurVidx();
8283     if(_JPD_PreVIdx != cur_vidx)
8284     {
8285         _JPD_PreVIdx = cur_vidx;
8286 #if MSOS_GET_SYSTEM_TIME
8287         _JPD_ReCheckTime = MsOS_GetSystemTime();
8288 #else
8289         _JPD_ReCheckTime = 0;
8290 #endif
8291     }
8292     else
8293     {
8294 #if MSOS_GET_SYSTEM_TIME
8295         if((MsOS_GetSystemTime() - _JPD_ReCheckTime) >= 1000)
8296 #else
8297         if(_JPD_ReCheckTime++ >= 10000)
8298 #endif
8299         {
8300             _JPD_IsDecoding = FALSE;
8301             JPEG_DEBUG_API_ERR("Decode timeout...!!!!\n");
8302             return E_JPEG_FAILED;
8303         }
8304     }
8305 #endif
8306     return E_JPEG_OKAY;
8307 }
8308 //------------------------------------------------------------------------------
8309 /********************************************************************/
8310 /// Get Preload buffer information.
8311 ///@param -pBuffLoadType \b OUT : JPEG preload buffer type
8312 ///@return \b JPEG_Result
8313 /// - E_JPEG_OKAY : success.
8314 /// - E_JPEG_FAILED : fail.
8315 /// - E_JPEG_RETRY : we need to re-get preload buffer type
8316 /********************************************************************/
_MApi_JPEG_GetBuffLoadType(JPEG_BuffLoadType * pBuffLoadType)8317 JPEG_Result _MApi_JPEG_GetBuffLoadType(JPEG_BuffLoadType *pBuffLoadType)
8318 {
8319 #ifndef CMODEL
8320     MS_U16 reg_val = MDrv_JPD_GetEventFlag();
8321 
8322     if(_bIsInit == FALSE)
8323     {
8324         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8325         return E_JPEG_FAILED;
8326     }
8327 
8328     if((E_JPD_EVENT_MRBL_DONE & reg_val)
8329     && (E_JPEG_BUFFER_LOW != u8PreLHFlag))
8330     {
8331         JPEG_DEBUG_API_MSG("%s : MRBuffer Load LOW!!!!\n", __FUNCTION__);
8332         u8PreLHFlag = E_JPEG_BUFFER_LOW;
8333         u32MRCheckCount = 0;
8334         *pBuffLoadType = u8PreLHFlag;
8335         return E_JPEG_OKAY;
8336     }
8337     else if((E_JPD_EVENT_MRBH_DONE & reg_val)
8338     && (E_JPEG_BUFFER_HIGH != u8PreLHFlag))
8339     {
8340         JPEG_DEBUG_API_MSG("%s : MRBuffer Load HIGH!!!!\n", __FUNCTION__);
8341         u8PreLHFlag = E_JPEG_BUFFER_HIGH;
8342         u32MRCheckCount = 0;
8343         *pBuffLoadType = u8PreLHFlag;
8344         return E_JPEG_OKAY;
8345     }
8346     else
8347     {
8348         JPEG_DEBUG_API_MSG("%s : MRBuffer Load NONE!!!!\n", __FUNCTION__);
8349         *pBuffLoadType = E_JPEG_BUFFER_NONE;
8350         u32MRCheckCount++;
8351         if(u32MRCheckCount >= 1000)
8352         {
8353             JPEG_DEBUG_API_ERR("Decoding time out!!\n");
8354             u32MRCheckCount = 0;
8355             return E_JPEG_FAILED;
8356         }
8357     }
8358     return E_JPEG_RETRY;
8359 #else
8360     return E_JPEG_OKAY;
8361 #endif
8362 }
8363 //------------------------------------------------------------------------------
8364 /********************************************************************/
8365 /// Enable JPD ISR and register callback function.
8366 ///@param -IsrCb \b IN : JPD ISR callback function
8367 ///@return \b JPEG_Result
8368 /// - E_JPEG_OKAY : success.
8369 /// - E_JPEG_FAILED : fail.
8370 /********************************************************************/
_MApi_JPEG_EnableISR(JPEG_IsrFuncCb IsrCb)8371 JPEG_Result _MApi_JPEG_EnableISR(JPEG_IsrFuncCb IsrCb)
8372 {
8373 #ifndef CMODEL
8374     if(TRUE == MDrv_JPD_EnableISR((JPD_IsrFuncCb)IsrCb))
8375     {
8376         JPEG_DEBUG_API_MSG("%s : Success!!!!\n", __FUNCTION__);
8377         return E_JPEG_OKAY;
8378     }
8379     else
8380     {
8381         JPEG_DEBUG_API_MSG("%s : Fail!!!!\n", __FUNCTION__);
8382         return E_JPEG_FAILED;
8383     }
8384 #else
8385     return E_JPEG_OKAY;
8386 #endif
8387 }
8388 //------------------------------------------------------------------------------
8389 /********************************************************************/
8390 /// Disable JPD ISR and unregister callback function.
8391 ///@return \b JPEG_Result
8392 /// - E_JPEG_OKAY : success.
8393 /// - E_JPEG_FAILED : fail.
8394 /********************************************************************/
_MApi_JPEG_DisableISR(void)8395 JPEG_Result _MApi_JPEG_DisableISR(void)
8396 {
8397 #ifndef CMODEL
8398     if(TRUE == MDrv_JPD_DisableISR())
8399     {
8400         JPEG_DEBUG_API_MSG("%s : Success!!!!\n", __FUNCTION__);
8401         return E_JPEG_OKAY;
8402     }
8403     else
8404     {
8405         JPEG_DEBUG_API_MSG("%s : Fail!!!!\n", __FUNCTION__);
8406         return E_JPEG_FAILED;
8407     }
8408 #else
8409     return E_JPEG_OKAY;
8410 #endif
8411 }
8412 //------------------------------------------------------------------------------
8413 /********************************************************************/
8414 /// JPEG wait done function, call it to check JPEG decoding status.
8415 ///@return \b JPEG_DecodeStatus
8416 /// - E_JPEG_DEC_DONE : JPEG decode done.
8417 /// - E_JPEG_DEC_FAILED : JPEG decode failed.
8418 /// - E_JPEG_DEC_MRBL_DONE : MRB low done, need to feed data.
8419 /// - E_JPEG_DEC_MRBH_DONE : MRB high done, need to feed data.
8420 /// - E_JPEG_DEC_DECODING : JPEG decoding
8421 /********************************************************************/
_MApi_JPEG_WaitDone(void)8422 JPEG_DecodeStatus _MApi_JPEG_WaitDone(void)
8423 {
8424 #ifndef CMODEL
8425     MS_U16 reg_val = E_JPD_EVENT_DEC_DONE;
8426     MS_U16 cur_vidx = 0;
8427 #endif
8428 
8429     if(_bIsInit == FALSE)
8430     {
8431         JPEG_DEBUG_API_ERR("%s: JPD have not init!!\n", __FUNCTION__);
8432         _JPD_IsDecoding = FALSE;
8433         return E_JPEG_DEC_FAILED;
8434     }
8435 
8436 #ifndef CMODEL
8437     if(FALSE == _JPD_IsDecoding)
8438     {
8439         _JPD_IsDecoding = TRUE;
8440 #if MSOS_GET_SYSTEM_TIME
8441         _JPD_ReCheckTime = MsOS_GetSystemTime();
8442 #else
8443         _JPD_ReCheckTime = 0;
8444 #endif
8445     }
8446 
8447     //For H/W bug, some cases can not exit after decode done. Check Vidx to exit.
8448     cur_vidx = MDrv_JPD_GetCurVidx();
8449     if(_JPD_PreVIdx != cur_vidx)
8450     {
8451         _JPD_PreVIdx = cur_vidx;
8452 #if MSOS_GET_SYSTEM_TIME
8453         _JPD_ReCheckTime = MsOS_GetSystemTime();
8454 #else
8455         _JPD_ReCheckTime = 0;
8456 #endif
8457     }
8458     else
8459     {
8460 #if MSOS_GET_SYSTEM_TIME
8461         if((MsOS_GetSystemTime() - _JPD_ReCheckTime) >= 1000)
8462 #else
8463         if(_JPD_ReCheckTime++ >= 10000)
8464 #endif
8465         {
8466             JPEG_DEBUG_API_ERR("%s:Decode timeout.....!!!!\n", __FUNCTION__);
8467             _JPD_IsDecoding = FALSE;
8468             return E_JPEG_DEC_FAILED;
8469         }
8470         else
8471         {
8472             return E_JPEG_DEC_DECODING;
8473         }
8474     }
8475 
8476     reg_val = MDrv_JPD_GetEventFlag();
8477 
8478     if(E_JPD_EVENT_DEC_DONE & reg_val)
8479     {
8480         JPEG_DEBUG_API_MSG("%s:Decode Done!!\n", __FUNCTION__);
8481 #if PRINT_JPD_DECODE_TIME
8482         u32MeasureDecodeTimeHW = MsOS_GetSystemTime()-u32MeasureDecodeTime;
8483         printf_red("SW decode header:[%ld]ms, HW decode: [%ld]ms\n", u32MeasureDecodeTimeSW, u32MeasureDecodeTimeHW);
8484 #endif
8485         if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API)
8486         {
8487             verJPD_CRC32_Init();
8488             verJPD_CRC32_Update((MS_U8 *)MWC_BUFFER_ADDR,
8489                 (MS_U32)MApi_JPEG_GetWidth()*MApi_JPEG_GetHeight()*2);
8490             printf_red("[%dx%d], u32CRCResult=0x%lx\n",MApi_JPEG_GetWidth(), MApi_JPEG_GetHeight(), verJPD_CRC32_GetResult());
8491         }
8492 
8493 #if LOG_DATA_TO_USB
8494             if(MApi_JPEG_GetWidth() <=JPEG_MAX_WIDTH
8495                 && MApi_JPEG_GetHeight() <=JPEG_MAX_HEIGHT)
8496             {
8497                 MS_U32 u32Size;
8498                 u32Size = MApi_JPEG_GetWidth()*MApi_JPEG_GetHeight()*2;
8499                 printf("[%dx%d], MWC_BUFFER_ADDR=0x%lx, size=0x%lx", MApi_JPEG_GetWidth(),
8500                     MApi_JPEG_GetHeight(), MS_VA2PA(MWC_BUFFER_ADDR),u32Size);
8501                 _PrintOutputMem(MWC_BUFFER_ADDR, u32Size);
8502             }
8503 #endif
8504         _JPD_IsDecoding = FALSE;
8505         return E_JPEG_DEC_DONE;
8506     }
8507     else if(E_JPD_EVENT_RST_ERROR & reg_val)
8508     {
8509         JPEG_DEBUG_API_ERR("%s:RST Error!!\n", __FUNCTION__);
8510         _JPD_IsDecoding = FALSE;
8511         return E_JPEG_DEC_RST_ERROR;
8512     }
8513     else if((E_JPD_EVENT_ECS_ERROR & reg_val)
8514         || (E_JPD_EVENT_IS_ERROR & reg_val))
8515     {
8516         JPEG_DEBUG_API_ERR("%s:Bitstream Error, reg_val = 0x%02x!!\n", __FUNCTION__ , reg_val);
8517         _JPD_IsDecoding = FALSE;
8518         return E_JPEG_DEC_BITSTREAM_ERROR;
8519     }
8520 #if (JPD_SUPPORT_AUTO_PROTECT==true)
8521     else if(E_JPD_EVENT_MWB_FULL & reg_val)
8522     {
8523         JPEG_DEBUG_API_MSG("[%s]MWB is full!!\n", __FUNCTION__);
8524         _JPD_IsDecoding = FALSE;
8525         return E_JPEG_DEC_FAILED;
8526     }
8527 #endif
8528     else if((E_JPD_EVENT_MRBL_DONE & reg_val)
8529     && (E_JPEG_BUFFER_LOW != u8PreLHFlag))
8530     {
8531         u8PreLHFlag = E_JPEG_BUFFER_LOW;
8532         JPEG_DEBUG_API_MSG("[%s]MRBL done!!\n", __FUNCTION__);
8533         return E_JPEG_DEC_MRBL_DONE;
8534     }
8535     else if((E_JPD_EVENT_MRBH_DONE & reg_val)
8536     && (E_JPEG_BUFFER_HIGH != u8PreLHFlag))
8537     {
8538         u8PreLHFlag = E_JPEG_BUFFER_HIGH;
8539         JPEG_DEBUG_API_MSG("[%s]MRBH done!!\n", __FUNCTION__);
8540         return E_JPEG_DEC_MRBH_DONE;
8541     }
8542 #endif
8543     return E_JPEG_DEC_DECODING;
8544 }
8545 //------------------------------------------------------------------------------
8546 /********************************************************************/
8547 /// Get datetime info of JPEG EXIF
8548 ///@return \b JPEG_Result
8549 /// - E_JPEG_OKAY : success.
8550 /// - E_JPEG_FAILED : fail.
8551 /********************************************************************/
_MApi_JPEG_GetEXIFDateTime(JPEG_EXIF_DateTime * DateTime)8552 JPEG_Result _MApi_JPEG_GetEXIFDateTime(JPEG_EXIF_DateTime *DateTime)
8553 {
8554     if(_bIsInit == FALSE)
8555     {
8556         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8557         return E_JPEG_FAILED;
8558     }
8559 
8560     DateTime->bHasDataTime = _stEXIF_DateTime.bHasDataTime;
8561     DateTime->u32Year = _stEXIF_DateTime.u32Year;
8562     DateTime->u32Month = _stEXIF_DateTime.u32Month;
8563     DateTime->u32Day = _stEXIF_DateTime.u32Day;
8564     DateTime->u32Hour = _stEXIF_DateTime.u32Hour;
8565     DateTime->u32Minute = _stEXIF_DateTime.u32Minute;
8566     DateTime->u32Second = _stEXIF_DateTime.u32Second;
8567 
8568     JPEG_DEBUG_API_MSG("=======JPEG EXIF DateTime======= Exist = %d \n", _stEXIF_DateTime.bHasDataTime);
8569     JPEG_DEBUG_API_MSG("Year = %ld, Month = %ld, Day = %ld\n", _stEXIF_DateTime.u32Year, _stEXIF_DateTime.u32Month, _stEXIF_DateTime.u32Day);
8570     JPEG_DEBUG_API_MSG("Hour = %ld, Minute = %ld, Second = %ld\n", _stEXIF_DateTime.u32Hour, _stEXIF_DateTime.u32Minute, _stEXIF_DateTime.u32Second);
8571 
8572     return E_JPEG_OKAY;
8573 }
8574 //------------------------------------------------------------------------------
8575 /********************************************************************/
8576 /// Get orientation info of JPEG EXIF
8577 ///@return \b JPEG_Result
8578 /// - E_JPEG_OKAY : success.
8579 /// - E_JPEG_FAILED : fail.
8580 /********************************************************************/
_MApi_JPEG_GetEXIFOrientation(JPEG_EXIF_Orientation * eOrientation)8581 JPEG_Result _MApi_JPEG_GetEXIFOrientation(JPEG_EXIF_Orientation *eOrientation)
8582 {
8583     if(_bIsInit == FALSE)
8584     {
8585         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8586         return E_JPEG_FAILED;
8587     }
8588     *eOrientation = _eEXIF_Orientation;
8589     JPEG_DEBUG_API_MSG("JPEG EXIF Orientation = %d\n", _eEXIF_Orientation);
8590     return E_JPEG_OKAY;
8591 }
8592 
8593 #if SUPPORT_EXIF_EXTRA_INFO
_MApi_JPEG_GetEXIFManufacturer(MS_U8 * pu8Manufacturer,MS_U8 u8size)8594 JPEG_Result _MApi_JPEG_GetEXIFManufacturer(MS_U8 *pu8Manufacturer, MS_U8 u8size)
8595 {
8596     if(_bIsInit == FALSE)
8597     {
8598         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8599         return E_JPEG_FAILED;
8600     }
8601     MS_U8 u8i=0;
8602     JPEG_memset((void*)(pu8Manufacturer), 0, u8size);
8603     JPEG_DEBUG_API_MSG("JPEG EXIF Manufacturer =");
8604     while(_u8EXIF_Manufacturer[u8i] && u8i<JPEG_MANUFACTURER_SIZE)
8605     {
8606         *(pu8Manufacturer+u8i) =_u8EXIF_Manufacturer[u8i];
8607         JPEG_DEBUG_API_MSG("%c", *(pu8Manufacturer+u8i));
8608         u8i++;
8609         if(u8i>=u8size)
8610         {
8611             JPEG_DEBUG_API_ERR("%s: NOT enough space!!\n", __FUNCTION__);
8612             return E_JPEG_FAILED;
8613         }
8614     }
8615     JPEG_DEBUG_API_MSG("\n");
8616 //    JPEG_DEBUG_API_MSG("JPEG EXIF Manufacturer = %02x %02x %02x %02x %02x %02x %02x %02x\n",
8617 //        *pu8Manufacturer, *(pu8Manufacturer+1), *(pu8Manufacturer+2), *(pu8Manufacturer+3),
8618 //        *(pu8Manufacturer+4),*(pu8Manufacturer+5),*(pu8Manufacturer+6),*(pu8Manufacturer+7));
8619     return E_JPEG_OKAY;
8620 }
8621 
_MApi_JPEG_GetEXIFModel(MS_U8 * pu8Model,MS_U8 u8size)8622 JPEG_Result _MApi_JPEG_GetEXIFModel(MS_U8 *pu8Model, MS_U8 u8size)
8623 {
8624     if(_bIsInit == FALSE)
8625     {
8626         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8627         return E_JPEG_FAILED;
8628     }
8629     MS_U8 u8i=0;
8630     JPEG_memset((void*)(pu8Model), 0, u8size);
8631     JPEG_DEBUG_API_MSG("JPEG EXIF Model =");
8632     while(_u8EXIF_Model[u8i] && u8i<JPEG_MODEL_SIZE)
8633     {
8634         *(pu8Model+u8i) =_u8EXIF_Model[u8i];
8635         JPEG_DEBUG_API_MSG("%c", *(pu8Model+u8i));
8636         u8i++;
8637         if(u8i>=u8size)
8638         {
8639             JPEG_DEBUG_API_ERR("%s: NOT enough space!!\n", __FUNCTION__);
8640             return E_JPEG_FAILED;
8641         }
8642     }
8643     JPEG_DEBUG_API_MSG("\n");
8644 
8645 //    JPEG_DEBUG_API_MSG("JPEG EXIF Model = %02x %02x %02x %02x %02x %02x %02x %02x\n",
8646 //        *pu8Model, *(pu8Model+1), *(pu8Model+2), *(pu8Model+3),
8647 //        *(pu8Model+4),*(pu8Model+5),*(pu8Model+6),*(pu8Model+7));
8648     return E_JPEG_OKAY;
8649 }
8650 
_MApi_JPEG_GetEXIFFlash(MS_U16 * pu16Flash)8651 JPEG_Result _MApi_JPEG_GetEXIFFlash(MS_U16 *pu16Flash)
8652 {
8653     if(_bIsInit == FALSE)
8654     {
8655         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8656         return E_JPEG_FAILED;
8657     }
8658     *pu16Flash = _u16EXIF_Flash;
8659     JPEG_DEBUG_API_MSG("JPEG EXIF Flash = %d \n", *pu16Flash);
8660     return E_JPEG_OKAY;
8661 }
8662 
_MApi_JPEG_GetEXIFISOSpeedRatings(MS_U32 * pu32ISOSpeedRatings)8663 JPEG_Result _MApi_JPEG_GetEXIFISOSpeedRatings(MS_U32 *pu32ISOSpeedRatings)
8664 {
8665     if(_bIsInit == FALSE)
8666     {
8667         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8668         return E_JPEG_FAILED;
8669     }
8670     *pu32ISOSpeedRatings = _u32EXIF_ISOSpeedRatings;
8671     JPEG_DEBUG_API_MSG("JPEG EXIF ISO = %ld \n", *pu32ISOSpeedRatings);
8672     return E_JPEG_OKAY;
8673 }
8674 
_MApi_JPEG_GetEXIFShutterSpeedValue(JPEG_RATIONAL * pShutterSpeedValue)8675 JPEG_Result _MApi_JPEG_GetEXIFShutterSpeedValue(JPEG_RATIONAL *pShutterSpeedValue)
8676 {
8677     if(_bIsInit == FALSE)
8678     {
8679         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8680         return E_JPEG_FAILED;
8681     }
8682     pShutterSpeedValue->numerator = _stEXIF_ShutterSpeedValue.numerator;
8683     pShutterSpeedValue->denominator= _stEXIF_ShutterSpeedValue.denominator;
8684     JPEG_DEBUG_API_MSG("ShutterSpeedValue numerator=%ld, denominator=%ld\n", pShutterSpeedValue->numerator, pShutterSpeedValue->denominator);
8685     return E_JPEG_OKAY;
8686 }
8687 
_MApi_JPEG_GetEXIFApertureValue(JPEG_RATIONAL * pApertureValue)8688 JPEG_Result _MApi_JPEG_GetEXIFApertureValue(JPEG_RATIONAL *pApertureValue)
8689 {
8690     if(_bIsInit == FALSE)
8691     {
8692         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8693         return E_JPEG_FAILED;
8694     }
8695     pApertureValue->s_numerator = _stEXIF_ApertureValue.s_numerator;
8696     pApertureValue->s_denominator= _stEXIF_ApertureValue.s_denominator;
8697     JPEG_DEBUG_API_MSG("ApertureValue numerator=%ld, denominator=%ld\n", pApertureValue->s_numerator, pApertureValue->s_denominator);
8698     return E_JPEG_OKAY;
8699 }
8700 
8701 
_MApi_JPEG_GetEXIFExposureBiasValue(JPEG_RATIONAL * pExposureBiasValue)8702 JPEG_Result _MApi_JPEG_GetEXIFExposureBiasValue(JPEG_RATIONAL *pExposureBiasValue)
8703 {
8704     if(_bIsInit == FALSE)
8705     {
8706         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8707         return E_JPEG_FAILED;
8708     }
8709     pExposureBiasValue->numerator = _stEXIF_ExposureBiasValue.numerator;
8710     pExposureBiasValue->denominator= _stEXIF_ExposureBiasValue.denominator;
8711     JPEG_DEBUG_API_MSG("ExposureBiasValue numerator=%ld, denominator=%ld\n", pExposureBiasValue->numerator, pExposureBiasValue->denominator);
8712     return E_JPEG_OKAY;
8713 }
8714 
_MApi_JPEG_GetEXIFFocalLength(JPEG_RATIONAL * pFocalLength)8715 JPEG_Result _MApi_JPEG_GetEXIFFocalLength(JPEG_RATIONAL *pFocalLength)
8716 {
8717     if(_bIsInit == FALSE)
8718     {
8719         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8720         return E_JPEG_FAILED;
8721     }
8722     pFocalLength->s_numerator = _stEXIF_FocalLength.s_numerator;
8723     pFocalLength->s_denominator= _stEXIF_FocalLength.s_denominator;
8724     JPEG_DEBUG_API_MSG("FocalLength numerator=%ld, denominator=%ld\n", pFocalLength->s_numerator, pFocalLength->s_denominator);
8725     return E_JPEG_OKAY;
8726 }
_MApi_JPEG_GetEXIFImageWidth(MS_U32 * pu32ImageWidth)8727 JPEG_Result _MApi_JPEG_GetEXIFImageWidth(MS_U32 *pu32ImageWidth)
8728 {
8729     if(_bIsInit == FALSE)
8730     {
8731         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8732         return E_JPEG_FAILED;
8733     }
8734     *pu32ImageWidth = _u32EXIF_ImageWidth;
8735     JPEG_DEBUG_API_MSG("JPEG EXIF Image Width = %ld \n", *pu32ImageWidth);
8736     return E_JPEG_OKAY;
8737 }
8738 
_MApi_JPEG_GetEXIFImageHeight(MS_U32 * pu32ImageHeight)8739 JPEG_Result _MApi_JPEG_GetEXIFImageHeight(MS_U32 *pu32ImageHeight)
8740 {
8741     if(_bIsInit == FALSE)
8742     {
8743         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8744         return E_JPEG_FAILED;
8745     }
8746     *pu32ImageHeight = _u32EXIF_ImageHeight;
8747     JPEG_DEBUG_API_MSG("JPEG EXIF Image Height = %ld \n", *pu32ImageHeight);
8748     return E_JPEG_OKAY;
8749 }
_MApi_JPEG_GetEXIFExposureTime(JPEG_RATIONAL * pExposureTime)8750 JPEG_Result _MApi_JPEG_GetEXIFExposureTime(JPEG_RATIONAL *pExposureTime)
8751 {
8752     if(_bIsInit == FALSE)
8753     {
8754         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8755         return E_JPEG_FAILED;
8756     }
8757     pExposureTime->s_numerator = _stEXIF_ExposureTime.s_numerator;
8758     pExposureTime->s_denominator= _stEXIF_ExposureTime.s_denominator;
8759     JPEG_DEBUG_API_MSG("ExposureTime numerator=%ld, denominator=%ld\n", pExposureTime->s_numerator, pExposureTime->s_denominator);
8760     return E_JPEG_OKAY;
8761 }
8762 
8763 
_MApi_JPEG_GetEXIFFNumber(JPEG_RATIONAL * pFNumber)8764 JPEG_Result _MApi_JPEG_GetEXIFFNumber(JPEG_RATIONAL *pFNumber)
8765 {
8766     if(_bIsInit == FALSE)
8767     {
8768         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8769         return E_JPEG_FAILED;
8770     }
8771     pFNumber->s_numerator = _stEXIF_FNumber.s_numerator;
8772     pFNumber->s_denominator= _stEXIF_FNumber.s_denominator;
8773     JPEG_DEBUG_API_MSG("FNumber numerator=%ld, denominator=%ld\n", pFNumber->s_numerator, pFNumber->s_denominator);
8774     return E_JPEG_OKAY;
8775 }
8776 #endif
8777 
8778 
_MApi_JPEG_GetControl(EN_JPEG_GET_CTRL_ID eGetID,MS_U32 * param,MS_U32 u32size)8779 JPEG_Result _MApi_JPEG_GetControl(EN_JPEG_GET_CTRL_ID eGetID, MS_U32 *param, MS_U32 u32size)
8780 {
8781     JPEG_Result eResult = E_JPEG_OKAY;
8782 
8783     if(_bIsInit == FALSE)
8784     {
8785         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8786         return E_JPEG_FAILED;
8787     }
8788 
8789     switch(eGetID)
8790     {
8791         case E_JPEG_GET_EXIF_MANUFACTURER:
8792             eResult = MApi_JPEG_GetEXIFManufacturer((MS_U8 *)param, u32size*4);
8793             JPEG_DEBUG_API_MSG("@JPEG EXIF Manufacturer = %c%c%c%c%c%c%c%c%c%c%c%c\n",
8794                 *((MS_U8 *)param), *((MS_U8 *)param+1), *((MS_U8 *)param+2), *((MS_U8 *)param+3),
8795                 *((MS_U8 *)param+4),*((MS_U8 *)param+5),*((MS_U8 *)param+6),*((MS_U8 *)param+7),
8796                 *((MS_U8 *)param+8),*((MS_U8 *)param+9),*((MS_U8 *)param+10),*((MS_U8 *)param+11)
8797                 );
8798             break;
8799         case E_JPEG_GET_EXIF_MODEL:
8800             eResult = MApi_JPEG_GetEXIFModel((MS_U8 *)param, u32size*4);
8801             JPEG_DEBUG_API_MSG("@JPEG EXIF Model = %c%c%c%c%c%c%c%c%c%c%c%c\n",
8802                 *((MS_U8 *)param), *((MS_U8 *)param+1), *((MS_U8 *)param+2), *((MS_U8 *)param+3),
8803                 *((MS_U8 *)param+4),*((MS_U8 *)param+5),*((MS_U8 *)param+6),*((MS_U8 *)param+7),
8804                 *((MS_U8 *)param+8),*((MS_U8 *)param+9),*((MS_U8 *)param+10),*((MS_U8 *)param+11)
8805                 );
8806             break;
8807         case E_JPEG_GET_EXIF_FLASH:
8808             *param = (MS_U32)_u16EXIF_Flash;
8809             JPEG_DEBUG_API_MSG("@JPEG EXIF Flash = %ld \n", *param);
8810             break;
8811         case E_JPEG_GET_EXIF_ISO_SPEED_RATINGS:
8812             *param = _u32EXIF_ISOSpeedRatings;
8813             JPEG_DEBUG_API_MSG("@JPEG EXIF ISO = %ld \n", *param);
8814             break;
8815         case E_JPEG_GET_EXIF_SHUTTER_SPEED_VALUE:
8816             if(u32size<2)
8817             {
8818                 eResult = E_JPEG_FAILED;
8819             }
8820             *param = _stEXIF_ShutterSpeedValue.numerator;
8821             *(param+1) = _stEXIF_ShutterSpeedValue.denominator;
8822             JPEG_DEBUG_API_MSG("@ShutterSpeedValue numerator=%ld, denominator=%ld\n", *param, *(param+1));
8823             break;
8824         case E_JPEG_GET_EXIF_APERTURE_VALUE:
8825             if(u32size<2)
8826             {
8827                 eResult = E_JPEG_FAILED;
8828             }
8829             *param = (MS_U32)_stEXIF_ApertureValue.s_numerator;
8830             *(param+1) = (MS_U32)_stEXIF_ApertureValue.s_denominator;
8831             JPEG_DEBUG_API_MSG("@ApertureValue numerator=%ld, denominator=%ld\n", *param, *(param+1));
8832             break;
8833         case E_JPEG_GET_EXIF_EXPOSURE_BIAS_VALUE:
8834             if(u32size<2)
8835             {
8836                 eResult = E_JPEG_FAILED;
8837             }
8838             *param = _stEXIF_ExposureBiasValue.numerator;
8839             *(param+1) = _stEXIF_ExposureBiasValue.denominator;
8840             JPEG_DEBUG_API_MSG("@ExposureBiasValue numerator=%ld, denominator=%ld\n", *param, *(param+1));
8841             break;
8842         case E_JPEG_GET_EXIF_FOCAL_LENGTH:
8843             if(u32size<2)
8844             {
8845                 eResult = E_JPEG_FAILED;
8846             }
8847             *param = (MS_U32)_stEXIF_FocalLength.s_numerator;
8848             *(param+1) = (MS_U32)_stEXIF_FocalLength.s_denominator;
8849             JPEG_DEBUG_API_MSG("@FocalLength numerator=%ld, denominator=%ld\n", *param, *(param+1));
8850             break;
8851         case E_JPEG_GET_EXIF_IMAGE_WIDTH:
8852             *param = _u32EXIF_ImageWidth;
8853             JPEG_DEBUG_API_MSG("@JPEG EXIF Image Width = %ld \n", *param);
8854             break;
8855         case E_JPEG_GET_EXIF_IMAGE_HEIGHT:
8856             *param = _u32EXIF_ImageHeight;
8857             JPEG_DEBUG_API_MSG("@JPEG EXIF Image Height = %ld \n", *param);
8858             break;
8859         case E_JPEG_GET_EXIF_EXPOSURE_TIME:
8860             if(u32size<2)
8861             {
8862                 eResult = E_JPEG_FAILED;
8863             }
8864             *param = (MS_U32)_stEXIF_ExposureTime.s_numerator;
8865             *(param+1) = (MS_U32)_stEXIF_ExposureTime.s_denominator;
8866             JPEG_DEBUG_API_MSG("@ExposureTime numerator=%ld, denominator=%ld\n", *param, *(param+1));
8867             break;
8868         case E_JPEG_GET_EXIF_F_NUMBER:
8869             if(u32size<2)
8870             {
8871                 eResult = E_JPEG_FAILED;
8872             }
8873             *param = (MS_U32)_stEXIF_FNumber.s_numerator;
8874             *(param+1) = (MS_U32)_stEXIF_FNumber.s_denominator;
8875             JPEG_DEBUG_API_MSG("@FNumber numerator=%ld, denominator=%ld\n", *param, *(param+1));
8876             break;
8877         case E_JPEG_GET_EXIF_EXPOSURE_PROGRAM:
8878             *param = (MS_U32)_u16EXIF_Exposureprogram;
8879             JPEG_DEBUG_API_MSG("@JPEG EXIF Exposure Program = %ld \n", *param);
8880             break;
8881         default:
8882             eResult = E_JPEG_FAILED;
8883             break;
8884     }
8885     return eResult;
8886 }
8887 
8888 ////////////////////////////////////////////////////////////////////////////////////////
8889 ///   current unused function, maybe removed in new MDDI
8890 ////////////////////////////////////////////////////////////////////////////////////////
8891 #if 0 // not implement for new MDDI
8892 //------------------------------------------------------------------------------
8893 void msAPI_MJPEG_Parse_SOF(void)
8894 {
8895     JPEG_locate_sof_marker();
8896 }
8897 //------------------------------------------------------------------------------
8898 void msAPI_MJPEG_constructor( PJPEG_FILE_FileSystem_t Pstream, MS_U8 decode_type)
8899 {
8900     _u8DecodeType = decode_type;
8901     JPEG_init_thumbnail();
8902     msAPI_MJPEG_decode_init( Pstream);
8903 }
8904 //------------------------------------------------------------------------------
8905 //Set Total pics in the file
8906 void msAPI_MJPEG_SetTotal_Pics(MS_U32 *DecodeTime)
8907 {
8908     *DecodeTime = NumPics;
8909 }
8910 //------------------------------------------------------------------------------
8911 MS_S16 msAPI_JPEG_Check_End_Flag( void )
8912 {
8913     return ( _bEOF_flag );
8914 }
8915 
8916 #ifndef CMODEL
8917 //------------------------------------------------------------------------------
8918 /******************************************************************************/
8919 ///Set downscale ratio
8920 ///@param ratio \b OUT scale ratio
8921 /******************************************************************************/
8922 void msAPI_JPEG_SetDownScaleRatio(JPEG_DownScaleRatio ratio)
8923 {
8924     switch(ratio)
8925     {
8926     case E_RATIO_HALF:
8927         _u8DownScaleRatio = E_JPD_DOWNSCALE_HALF;
8928         break;
8929     case E_RATIO_FOURTH:
8930         _u8DownScaleRatio = E_JPD_DOWNSCALE_FOURTH;
8931         break;
8932     case E_RATIO_EIGHTH:
8933         _u8DownScaleRatio = E_JPD_DOWNSCALE_EIGHTH;
8934         break;
8935     default:
8936         _u8DownScaleRatio = E_JPD_DOWNSCALE_ORG;
8937         break;
8938     }
8939 }
8940 //------------------------------------------------------------------------------
8941 void msAPI_JPEG_GetOutputDimension(MS_U16 *width, MS_U16 *height)
8942 {
8943     MS_U8 mcu_width, mcu_height;
8944     MS_U16 pic_width = _u16Image_x_size;
8945     MS_U16 pic_height = _u16Image_y_size;
8946     MS_U8 Y_VSF = _u8Comp_v_samp[0];
8947     MS_U8 Y_HSF = _u8Comp_h_samp[0];
8948     MS_U16 ROI_width;
8949 
8950     if ( ( mcu_width = pic_width % ( Y_HSF * 8 ) ) )
8951     {
8952         pic_width += ( Y_HSF * 8 - mcu_width );
8953     }
8954 
8955     if ( ( mcu_height = pic_height% ( Y_VSF * 8 ) ) )
8956     {
8957         pic_height += ( Y_VSF * 8 - mcu_height );
8958     }
8959 
8960     ROI_width = pic_width;
8961 
8962     if(_u8DownScaleRatio==E_JPD_DOWNSCALE_FOURTH||_u8DownScaleRatio==E_JPD_DOWNSCALE_EIGHTH)
8963     {
8964         MS_U16 ROI_width;
8965 
8966         if(_u8DownScaleRatio==E_JPD_DOWNSCALE_FOURTH)
8967         {
8968             // width must be multiple of 32
8969             ROI_width = (pic_width/32)*32;
8970         }
8971         else if(_u8DownScaleRatio==E_JPD_DOWNSCALE_EIGHTH)
8972         {
8973             // width must be multiple of 64
8974             ROI_width = (pic_width/64)*64;
8975         }
8976     }
8977 
8978     switch(_u8DownScaleRatio)
8979     {
8980         case E_JPD_DOWNSCALE_HALF:
8981             *width = ROI_width/2;
8982             break;
8983 
8984         case E_JPD_DOWNSCALE_FOURTH:
8985             *width = ROI_width/4;
8986             break;
8987 
8988         case E_JPD_DOWNSCALE_EIGHTH:
8989             *width = ROI_width/8;
8990             break;
8991 
8992         default:
8993             *width = ROI_width;
8994             break;
8995     }
8996 
8997     *height = pic_height;
8998 }
8999 #endif
9000 //------------------------------------------------------------------------------
9001 MS_BOOL msAPI_MJPEG_SetInitParameter(MS_U32 u32RBufAddr, MS_U32 u32RBufSize, MS_U32 u32WBufAddr, MS_U32 u32IBufAddr, MS_U32 u32IBufSize)
9002 {
9003     // the buffer size must be multiple of 4 bytes
9004     //if((u32RBufSize%4)!=0)
9005      //   return FALSE;
9006     MS_U32 WBuffer = _u16Image_x_size*_u16Image_y_size*2;
9007 #ifdef CMODEL
9008     _bReady_flag = FALSE;
9009 #endif
9010     MRC_BUFFER_ADDR = _u32ReadBufferAddr + u32RBufAddr;
9011     JPEG_DEBUG_API_MSG("MRC_BUFFER_ADDR = %08lx\n",MRC_BUFFER_ADDR);
9012     //MRC_BUFFER_SIZE = _pu8In_buf + u32RBufSize;
9013    // JPEG_DEBUG_API_MSG("_u32WriteBufferAddr = %08X\n",_u32WriteBufferAddr);
9014    // JPEG_DEBUG_API_MSG("WBuffer = %08X\n",WBuffer);
9015     MWC_BUFFER_ADDR = _u32WriteBufferAddr + WBuffer;
9016     JPEG_DEBUG_API_MSG("MWC_BUFFER_ADDR = %08lx\n",MWC_BUFFER_ADDR);
9017     //JPEG_DEBUG_API_MSG("MWC_BUFFER_ADDR = %08X\n",MWC_BUFFER_ADDR);
9018     //INTERNAL_BUFFER_ADDR = u32IBufAddr;
9019     //INTERNAL_BUFFER_SIZE = u32IBufSize;
9020     //_u32In_buf_left = 0;
9021     _pu8In_buf = (MS_U8 *) (_u32ReadBufferAddr);
9022     _u32Total_bytes_read -= u32RBufAddr;
9023     //JPEG_MEMORY_init_mempool(((MS_U8 *) (u32IBufAddr + JPEG_DEFAULT_EXIF_SIZE)), u32IBufSize - JPEG_DEFAULT_EXIF_SIZE);
9024     //JPEG_locate_sof_marker();
9025 
9026     return TRUE;
9027 }
9028 //------------------------------------------------------------------------------
9029 MS_BOOL msAPI_MJPEG_ReSetMWC(MS_U32 u32RBufAddr, MS_U32 u32RBufSize, MS_U32 u32WBufAddr, MS_U32 u32IBufAddr, MS_U32 u32IBufSize)
9030 {
9031 
9032     _u32WriteBufferAddr = u32WBufAddr;
9033 
9034     JPEG_DEBUG_API_MSG("MWC_BUFFER_ADDR = %08lx\n",MWC_BUFFER_ADDR);
9035 
9036     return TRUE;
9037 }
9038 //------------------------------------------------------------------------------
9039 void msAPI_JPEG_SetErrorCode(MS_U16 u16Code)
9040 {
9041     _Error_code = u16Code;
9042 }
9043 #if 0
9044 #ifdef CMODEL
9045 //------------------------------------------------------------------------------
9046 // If you wish to decompress the image, call this method after constructing
9047 // the object. If E_JPEG_OKAY is returned you may then call decode() to
9048 // fetch the scan lines.
9049 JPEG_Result msAPI_JPEG_begin( void )
9050 {
9051     if ( _bReady_flag )
9052     {
9053         JPEG_DEBUG_API_MSG("Flag!!!!\n");
9054         return ( E_JPEG_OKAY );
9055     }
9056 
9057     if ( _Error_code )
9058     {
9059         JPEG_DEBUG_API_MSG("Error!!!!\n");
9060         return ( E_JPEG_FAILED );
9061     }
9062     JPEG_locate_sof_marker();
9063 
9064     //JPEG_decode_start();
9065 
9066     _bReady_flag = TRUE;
9067 
9068     return ( E_JPEG_OKAY );
9069 }
9070 #endif
9071 #endif
9072 #endif // not implement for new MDDI
9073 
9074 
9075 #if SW_JPD_RGB_CMYK
9076 // Decodes and dequantizes the next row of coefficients.
msAPI_JPEG_decode_next_row(void)9077 JPEG_STATIC MS_BOOL msAPI_JPEG_decode_next_row( void )
9078 {
9079     MS_U16 row_block = 0;
9080     MS_U16 mcu_row, mcu_block, k;
9081 
9082     // Clearing the entire row block buffer can take a lot of time!
9083     // Instead of clearing the entire buffer each row, keep track
9084     // of the number of nonzero entries written to each block and do
9085     // selective clears.
9086     //memset(_ps16Block_seg[0], 0, _u16Mcus_per_row * _u8Blocks_per_mcu * 64 * sizeof(BLOCK_TYPE));
9087 
9088     for ( mcu_row = 0; mcu_row < _u16Mcus_per_row; mcu_row++ )
9089     {
9090         if ( ( _u16Restart_interval ) && ( _u16Restarts_left == 0 ) )
9091         {
9092             if(!JPEG_process_restart())
9093                 return FALSE;
9094         }
9095 
9096         for ( mcu_block = 0; mcu_block < _u8Blocks_per_mcu; mcu_block++ )
9097         {
9098             MS_U8 component_id = _u8Mcu_org[mcu_block];
9099             MS_U8 prev_num_set;
9100             JPEG_HuffTbl *Ph;
9101 
9102             JPEG_BLOCK_TYPE *p = _ps16Block_seg[row_block];
9103             JPEG_QUANT_TYPE *q = _QuantTables[_u8Comp_quant[component_id]].s16Value;
9104             MS_S32 r, s;
9105 
9106             if ( ( s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_dc_tab[component_id]] ) ) != 0 )
9107             {
9108                 r = JPEG_get_bits_2( s );
9109                 s = HUFF_EXTEND( r, s );
9110             }
9111 
9112             //printf("r : %d s : %d\n",s,r);
9113 
9114             _u32Last_dc_val[component_id] = ( s += _u32Last_dc_val[component_id] );
9115 
9116             p[0] = s * q[0];
9117 
9118             prev_num_set = _u8Block_max_zag_set[row_block];
9119 
9120             Ph = &_Huff_tbls[_u8Comp_ac_tab[component_id]];
9121 
9122             for ( k = 1; k < 64; k++ )
9123             {
9124                 s = JPEG_huff_decode( Ph );
9125 
9126                 //printf("Decode s :%d\n",s);
9127 
9128                 r = s >> 4;
9129                 s &= 15;
9130 
9131                 if ( s )
9132                 {
9133                     if ( r )
9134                     {
9135                         if ( ( k + r ) > 63 )
9136                         {
9137                             JPEG_terminate( E_JPEG_DECODE_ERROR );
9138                             return FALSE;
9139                         }
9140 
9141                         if ( k < prev_num_set )
9142                         {
9143                             MS_U32 n = MIN( r, prev_num_set - k );
9144                             MS_U16 kt = k;
9145                             while ( n-- )
9146                             {
9147                                 p[_u8ZAG[kt++]] = 0;
9148                             }
9149                         }
9150 
9151                         k += r;
9152                     }
9153 
9154                     r = JPEG_get_bits_2( s );
9155                     s = HUFF_EXTEND( r, s );
9156 
9157                     //assert(k < 64);
9158 
9159                     p[_u8ZAG[k]] = s * q[k];
9160                 }
9161                 else
9162                 {
9163                     if ( r == 15 )
9164                     {
9165                         if ( ( k + 15 ) > 63 )
9166                         {
9167                             JPEG_terminate( E_JPEG_DECODE_ERROR );
9168                             return FALSE;
9169                         }
9170 
9171                         if ( k < prev_num_set )
9172                         {
9173                             MS_U16 n = MIN( 16, prev_num_set - k );        //bugfix Dec. 19, 2001 - was 15!
9174                             MS_U16 kt = k;
9175                             while ( n-- )
9176                             {
9177                                 p[_u8ZAG[kt++]] = 0;
9178                             }
9179                         }
9180 
9181                         k += 15;
9182                     }
9183                     else
9184                     {
9185                         //while (k < 64)
9186                         //  p[ZAG[k++]] = 0;
9187 
9188                         break;
9189                     }
9190                 }
9191             }
9192 
9193             if ( k < prev_num_set )
9194             {
9195                 MS_U16 kt = k;
9196                 while ( kt < prev_num_set && kt < 64)           // kt < 64 is for coverity check
9197                 {
9198                     p[_u8ZAG[kt++]] = 0;
9199                 }
9200             }
9201 
9202             _u8Block_max_zag_set[row_block] = k;
9203 
9204             //block_num[row_block++] = k;
9205             row_block++;
9206         }
9207 
9208         _u16Restarts_left--;
9209     }
9210     return TRUE;
9211 }
9212 
9213 /*----------------------------------------------------------------------------*/
msAPI_JPEG_idct(JPEG_BLOCK_TYPE * data,MS_U8 * Pdst_ptr)9214 JPEG_STATIC void msAPI_JPEG_idct( JPEG_BLOCK_TYPE *data, MS_U8 *Pdst_ptr )
9215 {
9216     MS_S32 t0, t1, t2, t3;
9217     MS_S32 t10, t11, t12, t13;
9218     MS_S32 a1, a2, a3, a4, a5;
9219     MS_S32 rowctl;
9220     register JPEG_BLOCK_TYPE *pdata;
9221 
9222     //kevinhuang, use an internal array in idct to avoid memcpy to save time
9223     JPEG_BLOCK_TYPE workspace[64];
9224     JPEG_BLOCK_TYPE *wsptr;
9225 
9226     pdata = data;
9227     wsptr = workspace;
9228     for ( rowctl = 8 - 1; rowctl >= 0; rowctl-- )
9229     {
9230         if ( ( pdata[1] | pdata[2] | pdata[3] | pdata[4] | pdata[5] | pdata[6] | pdata[7] ) == 0 )
9231         {
9232             MS_S16 dc_val = ( MS_S16 )( pdata[0] << 2 );
9233 
9234             wsptr[0] = dc_val;
9235             wsptr[1] = dc_val;
9236             wsptr[2] = dc_val;
9237             wsptr[3] = dc_val;
9238             wsptr[4] = dc_val;
9239             wsptr[5] = dc_val;
9240             wsptr[6] = dc_val;
9241             wsptr[7] = dc_val;
9242 
9243             pdata += 8;       /* advance pointer to next row */
9244             wsptr += 8;
9245             continue;
9246         }
9247 
9248         a2 = ( MS_S32 )pdata[2];
9249         a3 = ( MS_S32 )pdata[6];
9250 
9251         a1 = (FIX_0_5411) * (a2 + a3);
9252         t2 = (-FIX_1_8477) * a3 + a1;
9253         t3 = (FIX_0_7653) * a2 + a1;
9254 
9255         t0 = ( ( MS_S32 )pdata[0] + ( MS_S32 )pdata[4] ) << 13;
9256         t1 = ( ( MS_S32 )pdata[0] - ( MS_S32 )pdata[4] ) << 13;
9257 
9258         t10 = t0 + t3;
9259         t13 = t0 - t3;
9260         t11 = t1 + t2;
9261         t12 = t1 - t2;
9262 
9263         t0 = ( MS_S32 )pdata[7];
9264         t1 = ( MS_S32 )pdata[5];
9265         t2 = ( MS_S32 )pdata[3];
9266         t3 = ( MS_S32 )pdata[1];
9267 
9268         a1 = t0 + t3;
9269         a2 = t1 + t2;
9270         a3 = t0 + t2;
9271         a4 = t1 + t3;
9272         a5 = (FIX_1_1758) * (a3 + a4);
9273 
9274         a1 = (-FIX_0_8999) * a1;
9275         a2 = (-FIX_2_5629) * a2;
9276         a3 = (-FIX_1_9615) * a3;
9277         a4 = (-FIX_0_3901) * a4;
9278         t0 = (FIX_0_2986) * t0;
9279         t1 = (FIX_2_0531) * t1;
9280         t2 = (FIX_3_0727) * t2;
9281         t3 = (FIX_1_5013) * t3;
9282 
9283         a3 += a5;
9284         a4 += a5;
9285 
9286         t0 += a1 + a3;
9287         t1 += a2 + a4;
9288         t2 += a2 + a3;
9289         t3 += a1 + a4;
9290 
9291         wsptr[0] = ( MS_S16 )DESCALE( t10 + t3, 13 - 2 );
9292         wsptr[1] = ( MS_S16 )DESCALE( t11 + t2, 13 - 2 );
9293         wsptr[2] = ( MS_S16 )DESCALE( t12 + t1, 13 - 2 );
9294         wsptr[3] = ( MS_S16 )DESCALE( t13 + t0, 13 - 2 );
9295         wsptr[4] = ( MS_S16 )DESCALE( t13 - t0, 13 - 2 );
9296         wsptr[5] = ( MS_S16 )DESCALE( t12 - t1, 13 - 2 );
9297         wsptr[6] = ( MS_S16 )DESCALE( t11 - t2, 13 - 2 );
9298         wsptr[7] = ( MS_S16 )DESCALE( t10 - t3, 13 - 2 );
9299 
9300         pdata += 8;
9301         wsptr += 8;
9302     }
9303 
9304     pdata = workspace;
9305     for ( rowctl = 8 - 1; rowctl >= 0; rowctl-- )
9306     {
9307         MS_S16 i;
9308 
9309         if ( ( pdata[8 * 1] | pdata[8 * 2] | pdata[8 * 3] | pdata[8 * 4] | pdata[8 * 5] | pdata[8 * 6] | pdata[8 * 7] ) == 0 )
9310         {
9311             MS_S16 dc_val = ( MS_S16 )DESCALE( ( MS_S32 )pdata[0], 2 + 3 ) + 128;
9312 
9313             clamp( dc_val );
9314 
9315             Pdst_ptr[8 * 0] = ( MS_U8 )dc_val;
9316 
9317 #if SW_OPTIMIZE
9318             if(_u8ScaleDownFactor == 3)
9319             {
9320                 pdata++;
9321                 Pdst_ptr++;
9322                 continue;
9323             }
9324 #endif
9325 
9326             Pdst_ptr[8 * 4] = ( MS_U8 )dc_val;
9327 
9328 #if SW_OPTIMIZE
9329             if(_u8ScaleDownFactor == 2)
9330             {
9331                 pdata++;
9332                 Pdst_ptr++;
9333                 continue;
9334             }
9335 #endif
9336 
9337             Pdst_ptr[8 * 2] = ( MS_U8 )dc_val;
9338             Pdst_ptr[8 * 6] = ( MS_U8 )dc_val;
9339 
9340 #if SW_OPTIMIZE
9341             if(_u8ScaleDownFactor == 1)
9342             {
9343                 pdata++;
9344                 Pdst_ptr++;
9345                 continue;
9346             }
9347 #endif
9348 
9349             Pdst_ptr[8 * 1] = ( MS_U8 )dc_val;
9350             Pdst_ptr[8 * 5] = ( MS_U8 )dc_val;
9351             Pdst_ptr[8 * 3] = ( MS_U8 )dc_val;
9352             Pdst_ptr[8 * 7] = ( MS_U8 )dc_val;
9353 
9354             pdata++;
9355             Pdst_ptr++;
9356             continue;
9357         }
9358 
9359         a2 = ( MS_S32 )pdata[8 * 2];
9360         a3 = ( MS_S32 )pdata[8 * 6];
9361 
9362         a1 = (FIX_0_5411) * (a2 + a3);
9363         t2 = (-FIX_1_8477) * a3 + a1;
9364         t3 = (FIX_0_7653) * a2 + a1;
9365 
9366         t0 = ( ( MS_S32 )pdata[8 * 0] + ( MS_S32 )pdata[8 * 4] ) << 13;
9367         t1 = ( ( MS_S32 )pdata[8 * 0] - ( MS_S32 )pdata[8 * 4] ) << 13;
9368 
9369         t10 = t0 + t3;
9370         t13 = t0 - t3;
9371         t11 = t1 + t2;
9372         t12 = t1 - t2;
9373 
9374         t0 = ( MS_S32 )pdata[8 * 7];
9375         t1 = ( MS_S32 )pdata[8 * 5];
9376         t2 = ( MS_S32 )pdata[8 * 3];
9377         t3 = ( MS_S32 )pdata[8 * 1];
9378 
9379         a1 = t0 + t3;
9380         a2 = t1 + t2;
9381         a3 = t0 + t2;
9382         a4 = t1 + t3;
9383         a5 = (FIX_1_1758) * (a3 + a4);
9384 
9385         a1 = (-FIX_0_8999) * a1;
9386         a2 = (-FIX_2_5629) * a2;
9387         a3 = (-FIX_1_9615) * a3;
9388         a4 = (-FIX_0_3901) * a4;
9389         t0 = (FIX_0_2986) * t0;
9390         t1 = (FIX_2_0531) * t1;
9391         t2 = (FIX_3_0727) * t2;
9392         t3 = (FIX_1_5013) * t3;
9393 
9394         a3 += a5;
9395         a4 += a5;
9396 
9397         t0 += a1 + a3;
9398         t1 += a2 + a4;
9399         t2 += a2 + a3;
9400         t3 += a1 + a4;
9401 
9402         i = ( MS_S16 )DESCALE( t10 + t3, 13 + 2 + 3 ) + 128;
9403         clamp( i )
9404         Pdst_ptr[8 * 0] = ( MS_U8 )i;
9405 
9406 #if SW_OPTIMIZE
9407         if(_u8ScaleDownFactor== 3)
9408         {
9409             pdata++;
9410             Pdst_ptr++;
9411             continue;
9412         }
9413 #endif
9414 
9415         i = ( MS_S16 )DESCALE( t13 - t0, 13 + 2 + 3 ) + 128;
9416         clamp( i )
9417         Pdst_ptr[8 * 4] = ( MS_U8 )i;
9418 
9419 #if SW_OPTIMIZE
9420         if(_u8ScaleDownFactor== 2)
9421         {
9422             pdata++;
9423             Pdst_ptr++;
9424             continue;
9425         }
9426 #endif
9427 
9428         i = ( MS_S16 )DESCALE( t12 + t1, 13 + 2 + 3 ) + 128;
9429         clamp( i )
9430         Pdst_ptr[8 * 2] = ( MS_U8 )i;
9431 
9432         i = ( MS_S16 )DESCALE( t11 - t2, 13 + 2 + 3 ) + 128;
9433         clamp( i )
9434         Pdst_ptr[8 * 6] = ( MS_U8 )i;
9435 
9436 #if SW_OPTIMIZE
9437         if(_u8ScaleDownFactor== 1)
9438         {
9439             pdata++;
9440             Pdst_ptr++;
9441             continue;
9442         }
9443 #endif
9444 
9445         i = ( MS_S16 )DESCALE( t11 + t2, 13 + 2 + 3 ) + 128;
9446         clamp( i )
9447         Pdst_ptr[8 * 1] = ( MS_U8 )i;
9448 
9449 
9450         i = ( MS_S16 )DESCALE( t13 + t0, 13 + 2 + 3 ) + 128;
9451         clamp( i )
9452         Pdst_ptr[8 * 3] = ( MS_U8 )i;
9453 
9454 
9455         i = ( MS_S16 )DESCALE( t12 - t1, 13 + 2 + 3 ) + 128;
9456         clamp( i )
9457         Pdst_ptr[8 * 5] = ( MS_U8 )i;
9458 
9459 
9460         i = ( MS_S16 )DESCALE( t10 - t3, 13 + 2 + 3 ) + 128;
9461         clamp( i )
9462         Pdst_ptr[8 * 7] = ( MS_U8 )i;
9463 
9464         pdata++;
9465         Pdst_ptr++;
9466     }
9467 }
9468 
msAPI_JPEG_Idct_CMYKConvert(void)9469 JPEG_STATIC MS_BOOL msAPI_JPEG_Idct_CMYKConvert(void)
9470 {
9471     JPEG_BLOCK_TYPE *Psrc_ptr = _ps16Block_seg[0];
9472     MS_U8 *Pdst_ptr = gpu8Sample_buf;
9473     MS_U8 *u8Start_Each_Block, *u8Start_Each_Row;
9474     MS_U32 u32_y_Blcok_Pos = 0, Max_Pixel_Per_Row;
9475     MS_S32 i;
9476     MS_U8  u8Row_in_block, k;
9477     MS_U16 u16Pixel_Per_Block_x = 8, u16Pixel_Per_Block_y = 8;
9478     MS_U16 u16Pixel_Per_Pack_row = 16, u16TotalBlock_Per_Row, u16ScaleDownStep = 1 << _u8ScaleDownFactor;
9479 
9480     u16TotalBlock_Per_Row = (_u32Max_blocks_per_row)/2;
9481 
9482     u16Pixel_Per_Pack_row = u16Pixel_Per_Pack_row >> _u8ScaleDownFactor;
9483     u16Pixel_Per_Block_x = u16Pixel_Per_Block_x/* >> _u8ScaleDownFactor*/;
9484     u16Pixel_Per_Block_y = u16Pixel_Per_Block_y >> _u8ScaleDownFactor;
9485 
9486 
9487     Max_Pixel_Per_Row = (u16TotalBlock_Per_Row*u16Pixel_Per_Block_x >> _u8ScaleDownFactor);
9488 
9489     //Get start position of each row
9490     u8Start_Each_Row = (u8Out_buf + (u32_Decode_Line*(Max_Pixel_Per_Row*u16Pixel_Per_Block_y)));
9491 
9492     for ( i = (MS_S32)_u32Max_blocks_per_row; i > 0;)
9493     {
9494         //===================GET Y1 BLOCKs=======================
9495         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9496 
9497         //Get start position of block
9498         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9499 
9500         //Write value to destination buffer
9501         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9502         {
9503             for(k=0;k<u16Pixel_Per_Block_y;k++)
9504             {
9505                 *(u8Start_Each_Block + (k*2) ) = 255 - Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9506             }
9507             u8Start_Each_Block += Max_Pixel_Per_Row;
9508         }
9509 
9510         //Skip U. V block for 422 format
9511         Psrc_ptr += 256;
9512         Pdst_ptr += 256;
9513         i-=4;
9514 
9515         if(i == 0)
9516         {
9517             //Already decoded all blocks
9518             continue;
9519         }
9520         else if(i < 0)
9521         {
9522             printf("GET BLOCK NUMBER ERROR!\n");
9523             JPEG_terminate( E_JPEG_DECODE_ERROR );
9524             return FALSE;
9525         }
9526 
9527         //Move y block position in a row
9528         u32_y_Blcok_Pos++;
9529         //===================GET Y2 BLOCKs=======================
9530         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9531 
9532         //Get start position of block
9533         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9534 
9535         //Write value to destination buffer
9536         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9537         {
9538             for(k=0;k<u16Pixel_Per_Block_y;k++)
9539             {
9540                 *(u8Start_Each_Block + (k*2) ) = 255 - Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9541             }
9542             u8Start_Each_Block += Max_Pixel_Per_Row;
9543         }
9544 
9545         //Move idct data position
9546         Psrc_ptr += 64;
9547         Pdst_ptr += 64;
9548         i--;
9549 
9550         //Move y block position in a row, Reset position for U block
9551         u32_y_Blcok_Pos--;
9552         //===================GET U BLOCKs=======================
9553         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9554 
9555         //Get start position of block
9556         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9557 
9558         //Write value to destination buffer
9559         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9560         {
9561             for(k=0;k<u16Pixel_Per_Block_y;k++)
9562             {
9563                 *(u8Start_Each_Block + ((k*4) + 1) ) = 255 - Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9564             }
9565             u8Start_Each_Block += Max_Pixel_Per_Row;
9566         }
9567 
9568         //Move idct data position
9569         Psrc_ptr += 64;
9570         Pdst_ptr += 64;
9571         i--;
9572 
9573         //===================GET V BLOCKs=======================
9574         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9575 
9576         //Get start position of block
9577         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9578 
9579         //Write value to destination buffer
9580         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9581         {
9582             for(k=0;k<u16Pixel_Per_Block_y;k++)
9583             {
9584                 *(u8Start_Each_Block + ((k*4) + 3) ) =  255 - Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9585             }
9586             u8Start_Each_Block += Max_Pixel_Per_Row;
9587         }
9588 
9589         //Move idct data position
9590         Psrc_ptr += 128;
9591         Pdst_ptr += 128;
9592         i-=2;
9593 
9594         //Move y block position in a row, Reset position for U block
9595         u32_y_Blcok_Pos+=2;
9596 
9597     }
9598 
9599     u32_Decode_Line++;
9600     return TRUE;
9601 }
9602 
msAPI_JPEG_Idct_RGBConvert(void)9603 JPEG_STATIC MS_BOOL msAPI_JPEG_Idct_RGBConvert(void)
9604 {
9605     JPEG_BLOCK_TYPE *Psrc_ptr = _ps16Block_seg[0];
9606     MS_U8 *Pdst_ptr = gpu8Sample_buf;
9607     MS_U8 *u8Start_Each_Block, *u8Start_Each_Row;
9608     MS_U32 u32_y_Blcok_Pos = 0, Max_Pixel_Per_Row;
9609     MS_S32 i;
9610     MS_U8  u8Row_in_block, k;
9611     MS_U16 u16Pixel_Per_Block_x = 8, u16Pixel_Per_Block_y = 8;
9612     MS_U16 u16Pixel_Per_Pack_row = 16, u16TotalBlock_Per_Row, u16ScaleDownStep = 1 << _u8ScaleDownFactor;
9613 
9614     u16TotalBlock_Per_Row = (_u32Max_blocks_per_row*2)/3;
9615 
9616     u16Pixel_Per_Pack_row = u16Pixel_Per_Pack_row >> _u8ScaleDownFactor;
9617     u16Pixel_Per_Block_x = u16Pixel_Per_Block_x/* >> _u8ScaleDownFactor*/;
9618     u16Pixel_Per_Block_y = u16Pixel_Per_Block_y >> _u8ScaleDownFactor;
9619 
9620 
9621     Max_Pixel_Per_Row = (u16TotalBlock_Per_Row*u16Pixel_Per_Block_x >> _u8ScaleDownFactor);
9622 
9623     //Get start position of each row
9624     u8Start_Each_Row = (u8Out_buf + (u32_Decode_Line*(Max_Pixel_Per_Row*u16Pixel_Per_Block_y)));
9625 
9626     for ( i = (MS_S32)_u32Max_blocks_per_row; i > 0;)
9627     {
9628 
9629         //===================GET R BLOCKs========================
9630         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9631 
9632         //Get start position of block
9633         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9634 
9635         //Write value to destination buffer
9636         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9637         {
9638             for(k=0;k<u16Pixel_Per_Block_y;k++)
9639             {
9640                 *(u8Start_Each_Block + (k*2) ) = Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9641             }
9642             u8Start_Each_Block += Max_Pixel_Per_Row;
9643         }
9644 
9645         //Skip U. V block for 422 format
9646         Psrc_ptr += 64;
9647         Pdst_ptr += 64;
9648         i--;
9649 
9650         //===================GET G BLOCKs=======================
9651         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9652 
9653         //Get start position of block
9654         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9655         //Write value to destination buffer
9656         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9657         {
9658             for(k=0;k<u16Pixel_Per_Block_y;k++)
9659             {
9660                 *(u8Start_Each_Block + ((k*4) + 1) ) = Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9661             }
9662             u8Start_Each_Block += Max_Pixel_Per_Row;
9663         }
9664 
9665         //Move idct data position
9666         Psrc_ptr += 64;
9667         Pdst_ptr += 64;
9668         i--;
9669 
9670         //===================GET B BLOCKs=======================
9671         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9672 
9673         //Get start position of block
9674         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9675         //Write value to destination buffer
9676         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9677         {
9678             for(k=0;k<u16Pixel_Per_Block_y;k++)
9679             {
9680                 *(u8Start_Each_Block + ((k*4) + 3) ) =  Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9681             }
9682             u8Start_Each_Block += Max_Pixel_Per_Row;
9683         }
9684 
9685         //Move idct data position
9686         Psrc_ptr += 64;
9687         Pdst_ptr += 64;
9688         i--;
9689 
9690         if(i == 0)
9691         {
9692             //Already decoded all blocks
9693             continue;
9694         }
9695         else if(i < 0)
9696         {
9697             printf("GET BLOCK NUMBER ERROR!\n");
9698             JPEG_terminate( E_JPEG_DECODE_ERROR );
9699             return FALSE;
9700         }
9701 
9702         //Move y block position in a row
9703         u32_y_Blcok_Pos++;
9704         //===================GET R BLOCKs=======================
9705         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9706 
9707         //Get start position of block
9708         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9709         //Write value to destination buffer
9710         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9711         {
9712             for(k=0;k<u16Pixel_Per_Block_y;k++)
9713             {
9714                 *(u8Start_Each_Block + (k*2) ) = Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9715             }
9716             u8Start_Each_Block += Max_Pixel_Per_Row;
9717         }
9718 
9719         //Move idct data position
9720         Psrc_ptr += 192;
9721         Pdst_ptr += 192;
9722         i-=3;
9723 
9724         //Move y block position in a row, Reset position for U block
9725         u32_y_Blcok_Pos++;
9726 
9727     }
9728 
9729     u32_Decode_Line++;
9730     return TRUE;
9731 }
9732 
9733 //------------------------------------------------------------------------------
9734 // Performs a 2D IDCT over the entire row's coefficient buffer.
msAPI_JPEG_transform_row(void)9735 JPEG_STATIC MS_BOOL msAPI_JPEG_transform_row( void )
9736 {
9737     JPEG_DEBUG_API_MSG("Max block : %ld\n", _u32Max_blocks_per_row);
9738 
9739     switch ( gu8Scan_type )
9740     {
9741         case E_JPEG_CMYK:
9742             if(!msAPI_JPEG_Idct_CMYKConvert())
9743                 return FALSE;
9744             break;
9745         case E_JPEG_RGB:
9746             if(!msAPI_JPEG_Idct_RGBConvert())
9747                 return FALSE;
9748             break;
9749         default :
9750             JPEG_DEBUG_API_ERR("System don`t support this format!\n");
9751             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
9752             return FALSE;
9753             break;
9754     }
9755     return TRUE;
9756 
9757 }
9758 
msAPI_JPEG_Baseline_Decode(void)9759 JPEG_STATIC JPEG_Result msAPI_JPEG_Baseline_Decode(void)
9760 {
9761 
9762     if(_bProgressive_flag==TRUE)
9763         return (E_JPEG_FAILED);
9764 
9765     if ( _u16Total_lines_left == 0 )
9766     {
9767         return ( E_JPEG_OKAY );
9768     }
9769 
9770 #ifdef JPD_LONGJUMP_SUPPORT
9771 if ( setjmp( _jmp_state ) )
9772     {
9773         return ( E_JPEG_FAILED );
9774     }
9775 #endif
9776 
9777     if( _u16Total_lines_left > 0 )
9778     {
9779 
9780         if(!msAPI_JPEG_decode_next_row())
9781             return E_JPEG_FAILED;
9782 
9783         JPEG_DEBUG_API_MSG("Max line num : %d\n",(_u16Total_lines_left));
9784 
9785         if(!msAPI_JPEG_transform_row()) //IDCT
9786             return E_JPEG_FAILED;
9787 
9788         _u16Total_lines_left -= gu8Max_mcu_y_size;
9789     }
9790 
9791     return (E_JPEG_DONE);
9792 }
9793 #endif //SW_JPD_RGB_CMYK
9794 
9795 
9796 
MApi_JPEG_DisableAddressConvert(void)9797 void MApi_JPEG_DisableAddressConvert(void)
9798 {
9799     JPEG_DEBUG_API_MSG("%s\n", __FUNCTION__);
9800     __bIsMjpeg = FALSE;
9801 }
9802 
9803 //------------------------------------------------------------------------------
9804 /********************************************************************/
9805 ///Get the free memory for temporary usage.
9806 ///@param size \b IN indicate the size of wanted free memory
9807 ///@return the physical address of free memory
9808 /********************************************************************/
_MApi_JPEG_GetFreeMemory(MS_U32 size)9809 MS_PHYADDR _MApi_JPEG_GetFreeMemory(MS_U32 size)
9810 {
9811     if(_bIsInit == FALSE)
9812     {
9813         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
9814         return 0;
9815     }
9816 
9817     if(_bProgressive_flag == TRUE)
9818     {
9819         return 0;
9820     }
9821 
9822     if(size > (INTERNAL_BUFFER_SIZE - JPEG_DEFAULT_EXIF_SIZE))
9823     {
9824         return 0;
9825     }
9826 
9827     INTERNAL_BUFFER_SIZE = INTERNAL_BUFFER_SIZE - size;
9828 
9829     return (MS_VA2PA((MS_U32)INTERNAL_BUFFER_ADDR) + INTERNAL_BUFFER_SIZE);
9830 }
9831 
9832 //------------------------------------------------------------------------------
9833 /********************************************************************/
9834 ///Get the data offset of JPEG file.
9835 ///@return the data offset of JPEG file
9836 /********************************************************************/
_MApi_JPEG_GetDataOffset(void)9837 MS_U32 _MApi_JPEG_GetDataOffset(void)
9838 {
9839     if(_bIsInit == FALSE)
9840     {
9841         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
9842         return 0;
9843     }
9844 
9845     if(_bThumbnailAccessMode)
9846     {
9847         JPEG_DEBUG_API_MSG("%s: Thumbnail mode!!\n", __FUNCTION__);
9848         return 0;
9849     }
9850 
9851     JPEG_DEBUG_API_MSG("%s : u32DataOffset =  0x%lx\n", __FUNCTION__ , u32DataOffset);
9852     return u32DataOffset;
9853 }
9854 
9855 //------------------------------------------------------------------------------
9856 /********************************************************************/
9857 ///Get the sof marker offset of JPEG file.
9858 ///@return the sof marker offset of JPEG file
9859 /********************************************************************/
_MApi_JPEG_GetSOFOffset(void)9860 MS_U32 _MApi_JPEG_GetSOFOffset(void)
9861 {
9862     if(_bIsInit == FALSE)
9863     {
9864         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
9865         return 0;
9866     }
9867 
9868     if(_bThumbnailAccessMode)
9869     {
9870         JPEG_DEBUG_API_MSG("%s: Thumbnail mode!!\n", __FUNCTION__);
9871         return 0;
9872     }
9873 
9874     JPEG_DEBUG_API_MSG("%s : u32SOFOffset =  0x%lx\n", __FUNCTION__ , u32SOFOffset);
9875     return u32SOFOffset;
9876 }
9877 
9878 //------------------------------------------------------------------------------
9879 /********************************************************************/
9880 ///Enable/Disable JPD CMYK support.
9881 ///@param bEnable \b IN enable/disable supporting JPD CMYK
9882 ///@return E_JPEG_OKAY/E_JPEG_FAILED
9883 ///Note: It need to be called before init. The default is JPD CMYK support.
9884 /********************************************************************/
_MApi_JPEG_SupportCMYK(MS_BOOL bEnable)9885 JPEG_Result _MApi_JPEG_SupportCMYK(MS_BOOL bEnable)
9886 {
9887 #if SW_JPD_RGB_CMYK
9888     bEnableCMYK = bEnable;
9889     return E_JPEG_OKAY;
9890 #else
9891     JPEG_DEBUG_API_MSG("Not Support CMYK formant!!!\n");
9892     UNUSED(bEnable);
9893     return E_JPEG_FAILED;
9894 #endif
9895 }
9896 
9897 //------------------------------------------------------------------------------
9898 /********************************************************************/
9899 ///Enable/Disable JPD RGB support.
9900 ///@param bEnable \b IN enable/disable supporting JPD RGB
9901 ///@return E_JPEG_OKAY/E_JPEG_FAILED
9902 ///Note: It need to be called before init. The default is JPD RGB support.
9903 /********************************************************************/
_MApi_JPEG_SupportRGB(MS_BOOL bEnable)9904 JPEG_Result _MApi_JPEG_SupportRGB(MS_BOOL bEnable)
9905 {
9906 #if SW_JPD_RGB_CMYK
9907     bEnableRGB = bEnable;
9908     return E_JPEG_OKAY;
9909 #else
9910     JPEG_DEBUG_API_MSG("Not Support RGB formant!!!\n");
9911     UNUSED(bEnable);
9912     return E_JPEG_FAILED;
9913 #endif
9914 }
9915 
9916 
9917 #if SUPPORT_MPO_FORMAT
_MApi_JPEG_IsMPOFormat(void)9918 MS_BOOL _MApi_JPEG_IsMPOFormat( void )
9919 {
9920     JPEG_DEBUG_API_MSG("MApi_JPEG_IsMPOFormat=%d\n", bIsMPOFormat);
9921     return bIsMPOFormat;
9922 }
9923 
_MApi_JPEG_GetMPOIndex(JPEG_MPO_INDEX_INFO ** ppMPOIndex)9924 MS_BOOL _MApi_JPEG_GetMPOIndex(JPEG_MPO_INDEX_INFO **ppMPOIndex)
9925 {
9926     if(_stMPOIndex.num_of_image == 0xFFFFFFFF)
9927     {
9928         JPEG_DEBUG_API_MSG("No MPO Index\n");
9929         return FALSE;
9930     }
9931 
9932     *ppMPOIndex = &_stMPOIndex;
9933     return TRUE;
9934 }
9935 
_MApi_JPEG_GetMPOAttr(MS_U32 image_no,JPEG_MPO_ATTRIBUTE_INFO ** ppMPOAttr)9936 MS_BOOL _MApi_JPEG_GetMPOAttr(MS_U32 image_no, JPEG_MPO_ATTRIBUTE_INFO **ppMPOAttr)
9937 {
9938     if((JPEG_MPO_MAX_SUPPORT_IMAGE < image_no)
9939     || (FALSE == _stMPOAttri[image_no - 1].used))
9940     {
9941         JPEG_DEBUG_API_MSG("No MPO Attribute with image no. %ld\n", image_no);
9942         return FALSE;
9943     }
9944 
9945     *ppMPOAttr = &_stMPOAttri[image_no - 1];
9946     return TRUE;
9947 }
9948 
_MApi_JPEG_DumpMPO(void)9949 void _MApi_JPEG_DumpMPO(void)
9950 {
9951     MS_U32 i = 0;
9952 
9953     if(_stMPOIndex.num_of_image == 0xFFFFFFFF)
9954     {
9955         JPEG_DEBUG_API_MSG("No MPO Index\n");
9956         return;
9957     }
9958 
9959     JPEG_DEBUG_API_MSG("start_of_offset = %ld, num_of_image = %ld\n", _stMPOIndex.start_of_offset, _stMPOIndex.num_of_image);
9960     for(i = 0; i<JPEG_MPO_MAX_SUPPORT_IMAGE; i++)
9961     {
9962         JPEG_DEBUG_API_MSG("mp_entry[%ld]:attribute = 0x%lx\n", i, _stMPOIndex.mp_entry[i].attribute);
9963         JPEG_DEBUG_API_MSG("mp_entry[%ld]:size = 0x%lx\n", i, _stMPOIndex.mp_entry[i].size);
9964         JPEG_DEBUG_API_MSG("mp_entry[%ld]:offset = 0x%lx\n", i, _stMPOIndex.mp_entry[i].offset);
9965         JPEG_DEBUG_API_MSG("mp_entry[%ld]:image1_no = 0x%x\n", i, _stMPOIndex.mp_entry[i].image1_no);
9966         JPEG_DEBUG_API_MSG("mp_entry[%ld]:image2_no = 0x%x\n", i, _stMPOIndex.mp_entry[i].image2_no);
9967 
9968         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:MPIndividualNum = 0x%lx\n", i, _stMPOAttri[i].MPIndividualNum);
9969         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOrientation = 0x%lx\n", i, _stMPOAttri[i].PanOrientation);
9970         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOverlap_H.numerator = 0x%lx\n", i, _stMPOAttri[i].PanOverlap_H.numerator);
9971         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOverlap_H.denominator = 0x%lx\n", i, _stMPOAttri[i].PanOverlap_H.denominator);
9972         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOverlap_V.numerator = 0x%lx\n", i, _stMPOAttri[i].PanOverlap_V.numerator);
9973         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOverlap_V.denominator = 0x%lx\n", i, _stMPOAttri[i].PanOverlap_V.denominator);
9974         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:BaseViewpointNum = 0x%lx\n", i, _stMPOAttri[i].BaseViewpointNum);
9975         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:ConvergenceAngle.s_numerator = 0x%lx\n", i, _stMPOAttri[i].ConvergenceAngle.s_numerator);
9976         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:ConvergenceAngle.s_denominator = 0x%lx\n", i, _stMPOAttri[i].ConvergenceAngle.s_denominator);
9977         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:BaselineLength.numerator = 0x%lx\n", i, _stMPOAttri[i].BaselineLength.numerator);
9978         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:BaselineLength.denominator = 0x%lx\n", i, _stMPOAttri[i].BaselineLength.denominator);
9979         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:VerticalDivergence.s_numerator = 0x%lx\n", i, _stMPOAttri[i].VerticalDivergence.s_numerator);
9980         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:VerticalDivergence.s_denominator = 0x%lx\n", i, _stMPOAttri[i].VerticalDivergence.s_denominator);
9981         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_X.s_numerator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_X.s_numerator);
9982         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_X.s_denominator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_X.s_denominator);
9983         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_Y.s_numerator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_Y.s_numerator);
9984         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_Y.s_denominator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_Y.s_denominator);
9985         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_Z.s_numerator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_Z.s_numerator);
9986         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_Z.s_denominator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_Z.s_denominator);
9987         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:YawAngle.s_numerator = 0x%lx\n", i, _stMPOAttri[i].YawAngle.s_numerator);
9988         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:YawAngle.s_denominator = 0x%lx\n", i, _stMPOAttri[i].YawAngle.s_denominator);
9989         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PitchAngle.s_numerator = 0x%lx\n", i, _stMPOAttri[i].PitchAngle.s_numerator);
9990         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PitchAngle.s_denominator = 0x%lx\n", i, _stMPOAttri[i].PitchAngle.s_denominator);
9991         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:RollAngle.s_numerator = 0x%lx\n", i, _stMPOAttri[i].RollAngle.s_numerator);
9992         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:RollAngle.s_denominator = 0x%lx\n", i, _stMPOAttri[i].RollAngle.s_denominator);
9993         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:used = 0x%d\n", i, _stMPOAttri[i].used);
9994     }
9995     return;
9996 }
9997 
_MApi_JPEG_SetMPOBuffer(MS_U32 read_offset,MS_U32 output_start)9998 MS_BOOL _MApi_JPEG_SetMPOBuffer(MS_U32 read_offset, MS_U32 output_start)
9999 {
10000     read_offset = MS_PA2KSEG1((MS_U32)read_offset);// | AEON_NON_CACHE_MASK;
10001     output_start = MS_PA2KSEG1((MS_U32)output_start);// | AEON_NON_CACHE_MASK;
10002 
10003     if((read_offset < MRC_BUFFER_ADDR)
10004     || (read_offset > (MRC_BUFFER_ADDR + MRC_BUFFER_SIZE)))
10005     {
10006         JPEG_DEBUG_API_MSG("JPEG_SetMPOBuffer, read_offset invalid\n");
10007         return FALSE;
10008     }
10009 
10010     if(read_offset > (MRC_BUFFER_ADDR + MRC_BUFFER_SIZE/2))
10011     {
10012         mpo_load_data = FALSE;
10013     }
10014 
10015     if(output_start < MWC_BUFFER_ADDR)
10016     {
10017         JPEG_DEBUG_API_MSG("JPEG_SetMPOBuffer, output_start invalid\n");
10018         return FALSE;
10019     }
10020 
10021     _pu8In_buf_MPO_ofs = (MS_U8*)read_offset;
10022     _u32In_buf_MPO_left = (MRC_BUFFER_ADDR + MRC_BUFFER_SIZE) - read_offset;
10023     JPEG_DEBUG_API_MSG("After MApi_JPEG_SetMPOBuffer(), _pu8In_buf_MPO_ofs=0x%lx, _u32In_buf_MPO_left=0x%lx=============\n", (MS_U32)_pu8In_buf_MPO_ofs, (MS_U32)_u32In_buf_MPO_left);
10024     MWC_BUFFER_ADDR = output_start;
10025     return TRUE;
10026 }
10027 
10028 /********************************************************************/
10029 /// Set Max decoding resolution for MPO
10030 ///@param -u16Width \b IN : indicate max decoding width
10031 ///@param -u16Height \b IN : indicate max decoding height
10032 ///@return None
10033 /********************************************************************/
_MApi_JPEG_SetMPOMaxDecodeResolution(MS_U16 u16Width,MS_U16 u16Height)10034 void _MApi_JPEG_SetMPOMaxDecodeResolution(MS_U16 u16Width, MS_U16 u16Height)
10035 {
10036     JPEG_DEBUG_API_MSG("%s: Width = %d, Height = %d\n", __FUNCTION__, u16Width, u16Height);
10037     JPEG_MPO_MAX_WIDTH = u16Width;
10038     JPEG_MPO_MAX_HEIGHT = u16Height;
10039     return;
10040 }
10041 //------------------------------------------------------------------------------
10042 /********************************************************************/
10043 /// Set Max decoding resolution of Progressive MPO
10044 ///@param -u16ProWidth \b IN : indicate max decoding width
10045 ///@param -u16ProHeight \b IN : indicate max decoding height
10046 ///@return None
10047 /********************************************************************/
_MApi_JPEG_SetMPOProMaxDecodeResolution(MS_U16 u16ProWidth,MS_U16 u16ProHeight)10048 void _MApi_JPEG_SetMPOProMaxDecodeResolution(MS_U16 u16ProWidth, MS_U16 u16ProHeight)
10049 {
10050     JPEG_DEBUG_API_MSG("%s: ProWidth = %d, ProHeight = %d\n", __FUNCTION__, u16ProWidth, u16ProHeight);
10051     JPEG_MPO_PRO_MAX_WIDTH = u16ProWidth;
10052     JPEG_MPO_PRO_MAX_HEIGHT = u16ProHeight;
10053     return;
10054 }
10055 
10056 #endif
10057 
msAPI_JPEG_get_APP0_info(MS_U8 * unit,MS_U16 * x,MS_U16 * y)10058 void msAPI_JPEG_get_APP0_info( MS_U8 *unit,MS_U16 *x, MS_U16 *y )
10059 {
10060 #if (JPEG_UTOPIA20)
10061     if(pInstantJPD==NULL)
10062     {
10063         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10064         return;
10065     }
10066 
10067     JPEG_GETAPP0INFO_PARAM App0Info;
10068     memset(&App0Info , 0 ,sizeof(JPEG_GETAPP0INFO_PARAM));
10069     App0Info.unit= unit;
10070     App0Info.x= x;
10071     App0Info.y= y;
10072     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_get_APP0_info\n");
10073     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_get_APP0_info, (void*)(&App0Info)) != UTOPIA_STATUS_SUCCESS)
10074     {
10075         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10076     }
10077 #else
10078     _msAPI_JPEG_get_APP0_info(unit, x, y );
10079 #endif
10080 }
10081 
MApi_JPEG_Init(JPEG_InitParam * pInitParam)10082 JPEG_Result MApi_JPEG_Init(JPEG_InitParam *pInitParam)
10083 {
10084 #if (JPEG_UTOPIA20)
10085     if (pInstantJPD == NULL)
10086     {
10087         if(UtopiaOpen(MODULE_JPEG, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10088         {
10089             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10090             return E_JPEG_FAILED;
10091         }
10092     }
10093 
10094     if(pInstantJPD==NULL)
10095     {
10096         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10097         UtopiaClose(pInstantJPD);
10098         return E_JPEG_FAILED;
10099     }
10100 
10101     JPEG_INIT_PARAM InitPara;
10102     memset(&InitPara , 0 ,sizeof(JPEG_INIT_PARAM));
10103     InitPara.pInitParam = pInitParam;
10104     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Init\n");
10105     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Init, (void*)(&InitPara)) != UTOPIA_STATUS_SUCCESS)
10106     {
10107         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10108         InitPara.retVal= E_JPEG_FAILED;
10109         UtopiaClose(pInstantJPD);
10110         pInstantJPD = NULL;
10111     }
10112     return InitPara.retVal;
10113 #else
10114     return _MApi_JPEG_Init(pInitParam);
10115 #endif
10116 }
10117 
MApi_JPEG_DecodeHdr(void)10118 JPEG_Result MApi_JPEG_DecodeHdr(void)
10119 {
10120 #if (JPEG_UTOPIA20)
10121     if(pInstantJPD==NULL)
10122     {
10123         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10124         return E_JPEG_FAILED;
10125     }
10126 
10127     JPEG_RESULT_PARAM ResPara;
10128     memset(&ResPara , 0 ,sizeof(JPEG_RESULT_PARAM));
10129     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_DecodeHdr\n");
10130     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_DecodeHdr, (void*)(&ResPara)) != UTOPIA_STATUS_SUCCESS)
10131     {
10132         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10133         return E_JPEG_FAILED;
10134     }
10135     return ResPara.retVal;
10136 #else
10137     return _MApi_JPEG_DecodeHdr();
10138 #endif
10139 }
10140 
MApi_JPEG_Decode(void)10141 JPEG_Result MApi_JPEG_Decode(void)
10142 {
10143 #if (JPEG_UTOPIA20)
10144     if(pInstantJPD==NULL)
10145     {
10146         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10147         return E_JPEG_FAILED;
10148     }
10149 
10150     JPEG_RESULT_PARAM ResPara;
10151     memset(&ResPara , 0 ,sizeof(JPEG_RESULT_PARAM));
10152     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Decode\n");
10153     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Decode, (void*)(&ResPara)) != UTOPIA_STATUS_SUCCESS)
10154     {
10155         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10156         return E_JPEG_FAILED;
10157     }
10158     return ResPara.retVal;
10159 #else
10160     return _MApi_JPEG_Decode();
10161 #endif
10162 }
10163 
MApi_JPEG_Exit(void)10164 void MApi_JPEG_Exit(void)
10165 {
10166 #if (JPEG_UTOPIA20)
10167     if(pInstantJPD==NULL)
10168     {
10169         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10170         return;
10171     }
10172 
10173     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Exit\n");
10174     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Exit, NULL) != UTOPIA_STATUS_SUCCESS)
10175     {
10176         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10177     }
10178     else
10179     {
10180         if(UtopiaClose(pInstantJPD) != UTOPIA_STATUS_SUCCESS)
10181         {
10182             JPEG_DEBUG_API_ERR("Utopia Close JPD failed\n");
10183             return;
10184         }
10185         pInstantJPD = NULL;
10186     }
10187 
10188 #else
10189     _MApi_JPEG_Exit();
10190 #endif
10191 }
10192 
MApi_JPEG_GetErrorCode(void)10193 JPEG_ErrCode MApi_JPEG_GetErrorCode(void)
10194 {
10195 #if (JPEG_UTOPIA20)
10196     if(pInstantJPD==NULL)
10197     {
10198         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10199         return E_JPEG_NOT_INIT;
10200     }
10201 
10202     JPEG_GETERRORCODE_PARAM GetErrorCode;
10203     memset(&GetErrorCode , 0 ,sizeof(JPEG_GETERRORCODE_PARAM));
10204     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetErrorCode\n");
10205     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetErrorCode, (void*)(&GetErrorCode)) != UTOPIA_STATUS_SUCCESS)
10206     {
10207         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10208         return E_JPEG_NOT_INIT;
10209     }
10210     return GetErrorCode.ErrCode;
10211 #else
10212     return _MApi_JPEG_GetErrorCode();
10213 #endif
10214 }
10215 
MApi_JPEG_GetJPDEventFlag(void)10216 JPEG_Event MApi_JPEG_GetJPDEventFlag(void)
10217 {
10218 #if (JPEG_UTOPIA20)
10219     if(pInstantJPD==NULL)
10220     {
10221         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10222         return 0;
10223     }
10224 
10225     JPEG_JPEGEVENT_PARAM JpegEvent;
10226     memset(&JpegEvent , 0 ,sizeof(JPEG_JPEGEVENT_PARAM));
10227     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetJPDEventFlag\n");
10228     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetJPDEventFlag, (void*)(&JpegEvent)) != UTOPIA_STATUS_SUCCESS)
10229     {
10230         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10231         return 0;
10232     }
10233     return JpegEvent.eEvtVal;
10234 #else
10235     return _MApi_JPEG_GetJPDEventFlag();
10236 #endif
10237 }
10238 
MApi_JPEG_SetJPDEventFlag(JPEG_Event eEvtVal)10239 void MApi_JPEG_SetJPDEventFlag(JPEG_Event eEvtVal)
10240 {
10241 #if (JPEG_UTOPIA20)
10242     if(pInstantJPD==NULL)
10243     {
10244         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10245         return;
10246     }
10247 
10248     JPEG_JPEGEVENT_PARAM JpegEvent;
10249     memset(&JpegEvent , 0 ,sizeof(JPEG_JPEGEVENT_PARAM));
10250     JpegEvent.eEvtVal = eEvtVal;
10251     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetJPDEventFlag\n");
10252     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetJPDEventFlag, (void*)(&JpegEvent)) != UTOPIA_STATUS_SUCCESS)
10253     {
10254         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10255     }
10256 #else
10257     _MApi_JPEG_SetJPDEventFlag(eEvtVal);
10258 #endif
10259 }
10260 
MApi_JPEG_Rst(void)10261 void MApi_JPEG_Rst(void)
10262 {
10263 #if (JPEG_UTOPIA20)
10264     if(pInstantJPD==NULL)
10265     {
10266         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10267         return;
10268     }
10269 
10270     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Rst\n");
10271     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Rst, NULL) != UTOPIA_STATUS_SUCCESS)
10272     {
10273         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10274     }
10275 #else
10276     _MApi_JPEG_Rst();
10277 #endif
10278 }
10279 
MApi_JPEG_PowerOn(void)10280 void MApi_JPEG_PowerOn(void)
10281 {
10282 #if (JPEG_UTOPIA20)
10283     if(pInstantJPD==NULL)
10284     {
10285         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10286         return;
10287     }
10288 
10289     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_PowerOn\n");
10290     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_PowerOn, NULL) != UTOPIA_STATUS_SUCCESS)
10291     {
10292         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10293     }
10294 #else
10295     _MApi_JPEG_PowerOn();
10296 #endif
10297 }
10298 
MApi_JPEG_PowerOff(void)10299 void MApi_JPEG_PowerOff(void)
10300 {
10301 #if (JPEG_UTOPIA20)
10302     if(pInstantJPD==NULL)
10303     {
10304         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10305         return;
10306     }
10307 
10308     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_PowerOff\n");
10309     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_PowerOff, NULL) != UTOPIA_STATUS_SUCCESS)
10310     {
10311         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10312     }
10313 #else
10314     _MApi_JPEG_PowerOff();
10315 #endif
10316 }
10317 
MApi_JPEG_GetCurVidx(void)10318 MS_U16 MApi_JPEG_GetCurVidx(void)
10319 {
10320 #if (JPEG_UTOPIA20)
10321     if(pInstantJPD==NULL)
10322     {
10323         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10324         return 0;
10325     }
10326 
10327     JPEG_U16_PARAM U16PARA;
10328     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10329     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetCurVidx\n");
10330     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetCurVidx, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10331     {
10332         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10333         return 0;
10334     }
10335     return U16PARA.u16Value;
10336 #else
10337     return _MApi_JPEG_GetCurVidx();
10338 #endif
10339 }
10340 
MApi_JPEG_IsProgressive(void)10341 MS_BOOL MApi_JPEG_IsProgressive(void)
10342 {
10343 #if (JPEG_UTOPIA20)
10344     if(pInstantJPD==NULL)
10345     {
10346         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10347         return 0;
10348     }
10349 
10350     JPEG_BOOL_PARAM bPARA;
10351     memset(&bPARA , 0 ,sizeof(JPEG_BOOL_PARAM));
10352     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_IsProgressive\n");
10353     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_IsProgressive, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
10354     {
10355         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10356         return 0;
10357     }
10358     return bPARA.bValue;
10359 #else
10360     return _MApi_JPEG_IsProgressive();
10361 #endif
10362 }
10363 
MApi_JPEG_ThumbnailFound(void)10364 MS_BOOL MApi_JPEG_ThumbnailFound(void)
10365 {
10366 #if (JPEG_UTOPIA20)
10367     if(pInstantJPD==NULL)
10368     {
10369         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10370         return 0;
10371     }
10372 
10373     JPEG_BOOL_PARAM bPARA;
10374     memset(&bPARA , 0 ,sizeof(JPEG_BOOL_PARAM));
10375     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_ThumbnailFound\n");
10376     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_ThumbnailFound, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
10377     {
10378         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10379         return 0;
10380     }
10381     return bPARA.bValue;
10382 #else
10383     return _MApi_JPEG_ThumbnailFound();
10384 #endif
10385 }
10386 
MApi_JPEG_GetWidth(void)10387 MS_U16 MApi_JPEG_GetWidth(void)
10388 {
10389 #if (JPEG_UTOPIA20)
10390     if(pInstantJPD==NULL)
10391     {
10392         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10393         return 0;
10394     }
10395 
10396     JPEG_U16_PARAM U16PARA;
10397     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10398     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetWidth\n");
10399     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetWidth, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10400     {
10401         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10402         return 0;
10403     }
10404     return U16PARA.u16Value;
10405 #else
10406     return _MApi_JPEG_GetWidth();
10407 #endif
10408 }
10409 
MApi_JPEG_GetHeight(void)10410 MS_U16 MApi_JPEG_GetHeight(void)
10411 {
10412 #if (JPEG_UTOPIA20)
10413     if(pInstantJPD==NULL)
10414     {
10415         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10416         return 0;
10417     }
10418 
10419     JPEG_U16_PARAM U16PARA;
10420     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10421     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetHeight\n");
10422     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetHeight, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10423     {
10424         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10425         return 0;
10426     }
10427     return U16PARA.u16Value;
10428 #else
10429     return _MApi_JPEG_GetHeight();
10430 #endif
10431 }
10432 
MApi_JPEG_GetOriginalWidth(void)10433 MS_U16 MApi_JPEG_GetOriginalWidth(void)
10434 {
10435 #if (JPEG_UTOPIA20)
10436     if(pInstantJPD==NULL)
10437     {
10438         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10439         return 0;
10440     }
10441 
10442     JPEG_U16_PARAM U16PARA;
10443     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10444     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetOriginalWidth\n");
10445     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetOriginalWidth, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10446     {
10447         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10448         return 0;
10449     }
10450     return U16PARA.u16Value;
10451 #else
10452     return _MApi_JPEG_GetOriginalWidth();
10453 #endif
10454 }
10455 
MApi_JPEG_GetOriginalHeight(void)10456 MS_U16 MApi_JPEG_GetOriginalHeight(void)
10457 {
10458 #if (JPEG_UTOPIA20)
10459     if(pInstantJPD==NULL)
10460     {
10461         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10462         return 0;
10463     }
10464 
10465     JPEG_U16_PARAM U16PARA;
10466     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10467     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetOriginalHeight\n");
10468     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetOriginalHeight, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10469     {
10470         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10471         return 0;
10472     }
10473     return U16PARA.u16Value;
10474 #else
10475     return _MApi_JPEG_GetOriginalHeight();
10476 #endif
10477 }
10478 
MApi_JPEG_GetNonAlignmentWidth(void)10479 MS_U16 MApi_JPEG_GetNonAlignmentWidth(void)
10480 {
10481 #if (JPEG_UTOPIA20)
10482     if(pInstantJPD==NULL)
10483     {
10484         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10485         return 0;
10486     }
10487 
10488     JPEG_U16_PARAM U16PARA;
10489     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10490     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetNonAlignmentWidth\n");
10491     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetNonAlignmentWidth, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10492     {
10493         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10494         return 0;
10495     }
10496     return U16PARA.u16Value;
10497 #else
10498     return _MApi_JPEG_GetNonAlignmentWidth();
10499 #endif
10500 }
10501 
MApi_JPEG_GetNonAlignmentHeight(void)10502 MS_U16 MApi_JPEG_GetNonAlignmentHeight(void)
10503 {
10504 #if (JPEG_UTOPIA20)
10505     if(pInstantJPD==NULL)
10506     {
10507         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10508         return 0;
10509     }
10510 
10511     JPEG_U16_PARAM U16PARA;
10512     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10513     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetNonAlignmentHeight\n");
10514     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetNonAlignmentHeight, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10515     {
10516         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10517         return 0;
10518     }
10519     return U16PARA.u16Value;
10520 #else
10521     return _MApi_JPEG_GetWidth();
10522 #endif
10523 }
10524 
MApi_JPEG_GetAlignedPitch(void)10525 MS_U16 MApi_JPEG_GetAlignedPitch(void)
10526 {
10527 #if (JPEG_UTOPIA20)
10528     if(pInstantJPD==NULL)
10529     {
10530         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10531         return 0;
10532     }
10533 
10534     JPEG_U16_PARAM U16PARA;
10535     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10536     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetAlignedPitch\n");
10537     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetAlignedPitch, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10538     {
10539         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10540         return 0;
10541     }
10542     return U16PARA.u16Value;
10543 #else
10544     return _MApi_JPEG_GetAlignedPitch();
10545 #endif
10546 }
10547 
MApi_JPEG_GetAlignedPitch_H(void)10548 MS_U16 MApi_JPEG_GetAlignedPitch_H(void)
10549 {
10550 #if (JPEG_UTOPIA20)
10551     if(pInstantJPD==NULL)
10552     {
10553         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10554         return 0;
10555     }
10556 
10557     JPEG_U16_PARAM U16PARA;
10558     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10559     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetAlignedPitch_H\n");
10560     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetAlignedPitch_H, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10561     {
10562         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10563         return 0;
10564     }
10565     return U16PARA.u16Value;
10566 #else
10567     return _MApi_JPEG_GetAlignedPitch_H();
10568 #endif
10569 }
10570 
MApi_JPEG_GetAlignedWidth(void)10571 MS_U16 MApi_JPEG_GetAlignedWidth(void)
10572 {
10573 #if (JPEG_UTOPIA20)
10574     if(pInstantJPD==NULL)
10575     {
10576         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10577         return 0;
10578     }
10579 
10580     JPEG_U16_PARAM U16PARA;
10581     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10582     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetAlignedWidth\n");
10583     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetAlignedWidth, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10584     {
10585         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10586         return 0;
10587     }
10588     return U16PARA.u16Value;
10589 #else
10590     return _MApi_JPEG_GetAlignedWidth();
10591 #endif
10592 }
10593 
MApi_JPEG_GetAlignedHeight(void)10594 MS_U16 MApi_JPEG_GetAlignedHeight(void)
10595 {
10596 #if (JPEG_UTOPIA20)
10597     if(pInstantJPD==NULL)
10598     {
10599         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10600         return 0;
10601     }
10602 
10603     JPEG_U16_PARAM U16PARA;
10604     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10605     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetAlignedHeight\n");
10606     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetAlignedHeight, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10607     {
10608         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10609         return 0;
10610     }
10611     return U16PARA.u16Value;
10612 #else
10613     return _MApi_JPEG_GetAlignedHeight();
10614 #endif
10615 }
10616 
MApi_JPEG_GetScaleDownFactor(void)10617 MS_U8 MApi_JPEG_GetScaleDownFactor(void)
10618 {
10619 #if (JPEG_UTOPIA20)
10620     if(pInstantJPD==NULL)
10621     {
10622         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10623         return 0;
10624     }
10625 
10626     JPEG_U8_PARAM U8PARA;
10627     memset(&U8PARA , 0 ,sizeof(JPEG_U8_PARAM));
10628     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetScaleDownFactor\n");
10629     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetScaleDownFactor, (void*)(&U8PARA)) != UTOPIA_STATUS_SUCCESS)
10630     {
10631         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10632         return 0;
10633     }
10634     return U8PARA.u8Value;
10635 #else
10636     return _MApi_JPEG_GetScaleDownFactor();
10637 #endif
10638 }
10639 
MApi_JPEG_SetMaxDecodeResolution(MS_U16 u16Width,MS_U16 u16Height)10640 void MApi_JPEG_SetMaxDecodeResolution(MS_U16 u16Width, MS_U16 u16Height)
10641 {
10642 #if (JPEG_UTOPIA20)
10643     if (pInstantJPD == NULL)
10644     {
10645         if(UtopiaOpen(MODULE_JPEG, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10646         {
10647             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10648             return;
10649         }
10650     }
10651     if(pInstantJPD==NULL)
10652     {
10653         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10654         UtopiaClose(pInstantJPD);
10655         return;
10656     }
10657 
10658     JPEG_RESOLUTION_PARAM Resol;
10659     memset(&Resol , 0 ,sizeof(JPEG_RESOLUTION_PARAM));
10660     Resol.u16Width= u16Width;
10661     Resol.u16Height= u16Height;
10662     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMaxDecodeResolution\n");
10663     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMaxDecodeResolution, (void*)(&Resol)) != UTOPIA_STATUS_SUCCESS)
10664     {
10665         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10666         UtopiaClose(pInstantJPD);
10667         pInstantJPD = NULL;
10668     }
10669 #else
10670     _MApi_JPEG_SetMaxDecodeResolution(u16Width, u16Height);
10671 #endif
10672 }
10673 
MApi_JPEG_SetProMaxDecodeResolution(MS_U16 u16ProWidth,MS_U16 u16ProHeight)10674 void MApi_JPEG_SetProMaxDecodeResolution(MS_U16 u16ProWidth, MS_U16 u16ProHeight)
10675 {
10676 #if (JPEG_UTOPIA20)
10677     if (pInstantJPD == NULL)
10678     {
10679         if(UtopiaOpen(MODULE_JPEG, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10680         {
10681             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10682             return;
10683         }
10684     }
10685     if(pInstantJPD==NULL)
10686     {
10687         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10688         UtopiaClose(pInstantJPD);
10689         return;
10690     }
10691 
10692     JPEG_RESOLUTION_PARAM Resol;
10693     memset(&Resol , 0 ,sizeof(JPEG_RESOLUTION_PARAM));
10694     Resol.u16Width= u16ProWidth;
10695     Resol.u16Height= u16ProHeight;
10696     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetProMaxDecodeResolution\n");
10697     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetProMaxDecodeResolution, (void*)(&Resol)) != UTOPIA_STATUS_SUCCESS)
10698     {
10699         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10700         UtopiaClose(pInstantJPD);
10701         pInstantJPD = NULL;
10702     }
10703 #else
10704     _MApi_JPEG_SetProMaxDecodeResolution(u16ProWidth, u16ProHeight);
10705 #endif
10706 }
10707 
MApi_JPEG_SetMRBufferValid(JPEG_BuffLoadType u8MRBuffType)10708 void MApi_JPEG_SetMRBufferValid(JPEG_BuffLoadType u8MRBuffType)
10709 {
10710 #if (JPEG_UTOPIA20)
10711     if(pInstantJPD==NULL)
10712     {
10713         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10714         return;
10715     }
10716 
10717     JPEG_SETMBUFFERVALID_PARAM MBuffValid;
10718     memset(&MBuffValid , 0 ,sizeof(JPEG_SETMBUFFERVALID_PARAM));
10719     MBuffValid.u8MRBuffType = u8MRBuffType;
10720     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMRBufferValid\n");
10721     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMRBufferValid, (void*)(&MBuffValid)) != UTOPIA_STATUS_SUCCESS)
10722     {
10723         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10724     }
10725 #else
10726     _MApi_JPEG_SetMRBufferValid(u8MRBuffType);
10727 #endif
10728 }
10729 
MApi_JPEG_UpdateReadInfo(MS_U32 u32DataRead,MS_BOOL bEOFflag)10730 void MApi_JPEG_UpdateReadInfo(MS_U32 u32DataRead, MS_BOOL bEOFflag)
10731 {
10732 #if (JPEG_UTOPIA20)
10733     if(pInstantJPD==NULL)
10734     {
10735         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10736         return;
10737     }
10738 
10739     JPEG_UPDATEREADINFO_PARAM ReadInfo;
10740     memset(&ReadInfo , 0 ,sizeof(JPEG_UPDATEREADINFO_PARAM));
10741     ReadInfo.u32DataRead = u32DataRead;
10742     ReadInfo.bEOFflag = bEOFflag;
10743     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_UpdateReadInfo\n");
10744     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_UpdateReadInfo, (void*)(&ReadInfo)) != UTOPIA_STATUS_SUCCESS)
10745     {
10746         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10747     }
10748 #else
10749     _MApi_JPEG_UpdateReadInfo(u32DataRead, bEOFflag);
10750 #endif
10751 }
10752 
MApi_JPEG_ProcessEOF(JPEG_BuffLoadType u8MRBuffType)10753 void MApi_JPEG_ProcessEOF(JPEG_BuffLoadType u8MRBuffType)
10754 {
10755 #if (JPEG_UTOPIA20)
10756     if(pInstantJPD==NULL)
10757     {
10758         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10759         return;
10760     }
10761 
10762     JPEG_PROCESSEOF_PARAM EofPara;
10763     memset(&EofPara , 0 ,sizeof(JPEG_PROCESSEOF_PARAM));
10764     EofPara.u8MRBuffType = u8MRBuffType;
10765     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_ProcessEOF\n");
10766     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_ProcessEOF, (void*)(&EofPara)) != UTOPIA_STATUS_SUCCESS)
10767     {
10768         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10769     }
10770 #else
10771     _MApi_JPEG_ProcessEOF(u8MRBuffType);
10772 #endif
10773 }
10774 
MApi_JPEG_SetErrCode(JPEG_ErrCode ErrStatus)10775 void MApi_JPEG_SetErrCode(JPEG_ErrCode ErrStatus)
10776 {
10777 #if (JPEG_UTOPIA20)
10778     if(pInstantJPD==NULL)
10779     {
10780         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10781         return;
10782     }
10783 
10784     JPEG_SETERRORCODE_PARAM ErrCode;
10785     memset(&ErrCode , 0 ,sizeof(JPEG_SETERRORCODE_PARAM));
10786     ErrCode.ErrStatus = ErrStatus;
10787     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetErrCode\n");
10788     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetErrCode, (void*)(&ErrCode)) != UTOPIA_STATUS_SUCCESS)
10789     {
10790         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10791     }
10792 #else
10793     _MApi_JPEG_SetErrCode(ErrStatus);
10794 #endif
10795 }
10796 
MApi_JPEG_SetDbgLevel(MS_U8 u8DbgLevel)10797 void MApi_JPEG_SetDbgLevel(MS_U8 u8DbgLevel)
10798 {
10799 #if (JPEG_UTOPIA20)
10800     if (pInstantJPD == NULL)
10801     {
10802         if(UtopiaOpen(MODULE_JPEG, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10803         {
10804             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10805             return;
10806         }
10807     }
10808     if(pInstantJPD==NULL)
10809     {
10810         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10811         UtopiaClose(pInstantJPD);
10812         return;
10813     }
10814 
10815     JPEG_U8_PARAM U8PARA;
10816     memset(&U8PARA , 0 ,sizeof(JPEG_U8_PARAM));
10817     U8PARA.u8Value = u8DbgLevel;
10818     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetDbgLevel\n");
10819     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetDbgLevel, (void*)(&U8PARA)) != UTOPIA_STATUS_SUCCESS)
10820     {
10821         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10822         UtopiaClose(pInstantJPD);
10823         pInstantJPD = NULL;
10824     }
10825 #else
10826     _MApi_JPEG_SetDbgLevel(u8DbgLevel);
10827 #endif
10828 }
10829 
MApi_JPEG_GetDbgLevel(void)10830 MS_U8 MApi_JPEG_GetDbgLevel(void)
10831 {
10832 #if (JPEG_UTOPIA20)
10833     if(pInstantJPD==NULL)
10834     {
10835         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10836         return 0;
10837     }
10838 
10839     JPEG_U8_PARAM U8PARA;
10840     memset(&U8PARA , 0 ,sizeof(JPEG_U8_PARAM));
10841     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetDbgLevel\n");
10842     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetDbgLevel, (void*)(&U8PARA)) != UTOPIA_STATUS_SUCCESS)
10843     {
10844         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10845     }
10846     return U8PARA.u8Value;
10847 #else
10848     return _MApi_JPEG_GetDbgLevel();
10849 #endif
10850 }
10851 
MApi_JPEG_GetInfo(JPEG_Info * pJPEG_Info)10852 void MApi_JPEG_GetInfo(JPEG_Info *pJPEG_Info)
10853 {
10854 #if (JPEG_UTOPIA20)
10855     if(pInstantJPD==NULL)
10856     {
10857         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10858         return;
10859     }
10860 
10861     JPEG_GETINFO_PARAM eGETINFO;
10862     memset(&eGETINFO , 0 ,sizeof(JPEG_GETINFO_PARAM));
10863     eGETINFO.pJPEG_Info = pJPEG_Info;
10864     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetInfo\n");
10865     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetInfo, (void*)(&eGETINFO)) != UTOPIA_STATUS_SUCCESS)
10866     {
10867         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10868     }
10869 #else
10870     _MApi_JPEG_GetInfo(pJPEG_Info);
10871 #endif
10872 }
10873 
MApi_JPEG_GetStatus(JPEG_Status * pJPEG_Status)10874 void MApi_JPEG_GetStatus(JPEG_Status *pJPEG_Status)
10875 {
10876 #if (JPEG_UTOPIA20)
10877     if(pInstantJPD==NULL)
10878     {
10879         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10880         return;
10881     }
10882 
10883     JPEG_GETSTATUS_PARAM eGETSTATUS;
10884     memset(&eGETSTATUS , 0 ,sizeof(JPEG_GETSTATUS_PARAM));
10885     eGETSTATUS.pJPEG_Status = pJPEG_Status;
10886     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetStatus\n");
10887     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetStatus, (void*)(&eGETSTATUS)) != UTOPIA_STATUS_SUCCESS)
10888     {
10889         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10890     }
10891 #else
10892     _MApi_JPEG_GetStatus(pJPEG_Status);
10893 #endif
10894 }
10895 
MApi_JPEG_GetLibVer(const MSIF_Version ** ppVersion)10896 JPEG_Result MApi_JPEG_GetLibVer(const MSIF_Version **ppVersion)
10897 {
10898 #if (JPEG_UTOPIA20)
10899     if(pInstantJPD==NULL)
10900     {
10901         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10902         return E_JPEG_FAILED;
10903     }
10904 
10905     JPEG_GETLIBVER_PARAM eLIBVER;
10906     memset(&eLIBVER , 0 ,sizeof(JPEG_GETLIBVER_PARAM));
10907     eLIBVER.ppVersion = ppVersion;
10908     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetLibVer\n");
10909     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetLibVer, (void*)(&eLIBVER)) != UTOPIA_STATUS_SUCCESS)
10910     {
10911         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10912     }
10913     return eLIBVER.retVal;
10914 #else
10915     return _MApi_JPEG_GetLibVer(ppVersion);
10916 #endif
10917 }
10918 
MApi_JPEG_HdlVidxChk(void)10919 JPEG_Result MApi_JPEG_HdlVidxChk(void)
10920 {
10921 #if (JPEG_UTOPIA20)
10922     if(pInstantJPD==NULL)
10923     {
10924         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10925         return E_JPEG_FAILED;
10926     }
10927 
10928     JPEG_RESULT_PARAM ResPara;
10929     memset(&ResPara , 0 ,sizeof(JPEG_RESULT_PARAM));
10930     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_HdlVidxChk\n");
10931     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_HdlVidxChk, (void*)(&ResPara)) != UTOPIA_STATUS_SUCCESS)
10932     {
10933         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10934     }
10935     return ResPara.retVal;
10936 #else
10937     return _MApi_JPEG_HdlVidxChk();
10938 #endif
10939 }
10940 
MApi_JPEG_GetBuffLoadType(JPEG_BuffLoadType * pBuffLoadType)10941 JPEG_Result MApi_JPEG_GetBuffLoadType(JPEG_BuffLoadType *pBuffLoadType)
10942 {
10943 #if (JPEG_UTOPIA20)
10944     if(pInstantJPD==NULL)
10945     {
10946         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10947         return E_JPEG_FAILED;
10948     }
10949 
10950     JPEG_GETBUFFLOADTYPE_PARAM eBufferLoad;
10951     memset(&eBufferLoad , 0 ,sizeof(JPEG_GETBUFFLOADTYPE_PARAM));
10952     eBufferLoad.pBuffLoadType = pBuffLoadType;
10953     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetBuffLoadType\n");
10954     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetBuffLoadType, (void*)(&eBufferLoad)) != UTOPIA_STATUS_SUCCESS)
10955     {
10956         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10957     }
10958     return eBufferLoad.retVal;
10959 #else
10960     return _MApi_JPEG_GetBuffLoadType(pBuffLoadType);
10961 #endif
10962 }
10963 
MApi_JPEG_EnableISR(JPEG_IsrFuncCb IsrCb)10964 JPEG_Result MApi_JPEG_EnableISR(JPEG_IsrFuncCb IsrCb)
10965 {
10966 #if (JPEG_UTOPIA20)
10967     if(pInstantJPD==NULL)
10968     {
10969         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10970         return E_JPEG_FAILED;
10971     }
10972 
10973     JPEG_ENABLEISR_PARAM eISR;
10974     memset(&eISR , 0 ,sizeof(JPEG_ENABLEISR_PARAM));
10975     eISR.IsrCb = IsrCb;
10976     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_EnableISR\n");
10977     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_EnableISR, (void*)(&eISR)) != UTOPIA_STATUS_SUCCESS)
10978     {
10979         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10980     }
10981     return eISR.retVal;
10982 #else
10983     return _MApi_JPEG_EnableISR(IsrCb);
10984 #endif
10985 }
10986 
MApi_JPEG_DisableISR(void)10987 JPEG_Result MApi_JPEG_DisableISR(void)
10988 {
10989 #if (JPEG_UTOPIA20)
10990     if(pInstantJPD==NULL)
10991     {
10992         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10993         return E_JPEG_FAILED;
10994     }
10995 
10996     JPEG_RESULT_PARAM ResPara;
10997     memset(&ResPara , 0 ,sizeof(JPEG_RESULT_PARAM));
10998     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_DisableISR\n");
10999     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_DisableISR, (void*)(&ResPara)) != UTOPIA_STATUS_SUCCESS)
11000     {
11001         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11002     }
11003     return ResPara.retVal;
11004 #else
11005     return _MApi_JPEG_DisableISR();
11006 #endif
11007 }
11008 
MApi_JPEG_WaitDone(void)11009 JPEG_DecodeStatus MApi_JPEG_WaitDone(void)
11010 {
11011 #if (JPEG_UTOPIA20)
11012     if(pInstantJPD==NULL)
11013     {
11014         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11015         return E_JPEG_DEC_DECODING;
11016     }
11017 
11018     JPEG_WAITDONE_PARAM eWaitDone;
11019     memset(&eWaitDone , 0 ,sizeof(JPEG_WAITDONE_PARAM));
11020     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_WaitDone\n");
11021     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_WaitDone, (void*)(&eWaitDone)) != UTOPIA_STATUS_SUCCESS)
11022     {
11023         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11024     }
11025     return eWaitDone.retVal;
11026 #else
11027     return _MApi_JPEG_WaitDone();
11028 #endif
11029 }
11030 
MApi_JPEG_GetEXIFDateTime(JPEG_EXIF_DateTime * DateTime)11031 JPEG_Result MApi_JPEG_GetEXIFDateTime(JPEG_EXIF_DateTime *DateTime)
11032 {
11033 #if (JPEG_UTOPIA20)
11034     if(pInstantJPD==NULL)
11035     {
11036         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11037         return E_JPEG_FAILED;
11038     }
11039 
11040     JPEG_GETEXIFDATETIME_PARAM eEXIFDate;
11041     memset(&eEXIFDate , 0 ,sizeof(JPEG_GETEXIFDATETIME_PARAM));
11042     eEXIFDate.DateTime = DateTime;
11043     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFDateTime\n");
11044     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFDateTime, (void*)(&eEXIFDate)) != UTOPIA_STATUS_SUCCESS)
11045     {
11046         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11047     }
11048     return eEXIFDate.retVal;
11049 #else
11050     return _MApi_JPEG_GetEXIFDateTime(DateTime);
11051 #endif
11052 }
11053 
MApi_JPEG_GetEXIFOrientation(JPEG_EXIF_Orientation * eOrientation)11054 JPEG_Result MApi_JPEG_GetEXIFOrientation(JPEG_EXIF_Orientation *eOrientation)
11055 {
11056 #if (JPEG_UTOPIA20)
11057     if(pInstantJPD==NULL)
11058     {
11059         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11060         return E_JPEG_FAILED;
11061     }
11062 
11063     JPEG_GETEXIFORIENTATION_PARAM eEXIFOri;
11064     memset(&eEXIFOri , 0 ,sizeof(JPEG_GETEXIFORIENTATION_PARAM));
11065     eEXIFOri.eOrientation = eOrientation;
11066     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFOrientation\n");
11067     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFOrientation, (void*)(&eEXIFOri)) != UTOPIA_STATUS_SUCCESS)
11068     {
11069         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11070     }
11071     return eEXIFOri.retVal;
11072 #else
11073     return _MApi_JPEG_GetEXIFOrientation(eOrientation);
11074 #endif
11075 }
11076 
11077 #if SUPPORT_EXIF_EXTRA_INFO
MApi_JPEG_GetEXIFManufacturer(MS_U8 * pu8Manufacturer,MS_U8 u8size)11078 JPEG_Result MApi_JPEG_GetEXIFManufacturer(MS_U8 *pu8Manufacturer, MS_U8 u8size)
11079 {
11080 #if (JPEG_UTOPIA20)
11081     if(pInstantJPD==NULL)
11082     {
11083         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11084         return E_JPEG_FAILED;
11085     }
11086 
11087     JPEG_GETEXIFSTRING_PARAM eEXIFStr;
11088     memset(&eEXIFStr , 0 ,sizeof(JPEG_GETEXIFSTRING_PARAM));
11089     eEXIFStr.pu8String = pu8Manufacturer;
11090     eEXIFStr.u8size = u8size;
11091     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFManufacturer\n");
11092     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFManufacturer, (void*)(&eEXIFStr)) != UTOPIA_STATUS_SUCCESS)
11093     {
11094         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11095     }
11096     return eEXIFStr.retVal;
11097 #else
11098     return _MApi_JPEG_GetEXIFManufacturer(pu8Manufacturer, u8size);
11099 #endif
11100 }
11101 
MApi_JPEG_GetEXIFModel(MS_U8 * pu8Model,MS_U8 u8size)11102 JPEG_Result MApi_JPEG_GetEXIFModel(MS_U8 *pu8Model, MS_U8 u8size)
11103 {
11104 #if (JPEG_UTOPIA20)
11105     if(pInstantJPD==NULL)
11106     {
11107         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11108         return E_JPEG_FAILED;
11109     }
11110 
11111     JPEG_GETEXIFSTRING_PARAM eEXIFStr;
11112     memset(&eEXIFStr , 0 ,sizeof(JPEG_GETEXIFSTRING_PARAM));
11113     eEXIFStr.pu8String = pu8Model;
11114     eEXIFStr.u8size = u8size;
11115     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFModel\n");
11116     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFModel, (void*)(&eEXIFStr)) != UTOPIA_STATUS_SUCCESS)
11117     {
11118         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11119     }
11120     return eEXIFStr.retVal;
11121 #else
11122     return _MApi_JPEG_GetEXIFModel(pu8Model, u8size);
11123 #endif
11124 }
11125 
MApi_JPEG_GetEXIFFlash(MS_U16 * pu16Flash)11126 JPEG_Result MApi_JPEG_GetEXIFFlash(MS_U16 *pu16Flash)
11127 {
11128 #if (JPEG_UTOPIA20)
11129     if(pInstantJPD==NULL)
11130     {
11131         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11132         return E_JPEG_FAILED;
11133     }
11134 
11135     JPEG_GETEXIFU16_PARAM U16PARA;
11136     memset(&U16PARA , 0 ,sizeof(JPEG_GETEXIFU16_PARAM));
11137     U16PARA.pu16Value = pu16Flash;
11138     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFFlash\n");
11139     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFFlash, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
11140     {
11141         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11142     }
11143     return U16PARA.retVal;
11144 #else
11145     return _MApi_JPEG_GetEXIFFlash(pu16Flash);
11146 #endif
11147 }
11148 
MApi_JPEG_GetEXIFISOSpeedRatings(MS_U32 * pu32ISOSpeedRatings)11149 JPEG_Result MApi_JPEG_GetEXIFISOSpeedRatings(MS_U32 *pu32ISOSpeedRatings)
11150 {
11151 #if (JPEG_UTOPIA20)
11152     if(pInstantJPD==NULL)
11153     {
11154         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11155         return E_JPEG_FAILED;
11156     }
11157 
11158     JPEG_GETEXIFU32_PARAM U32PARA;
11159     memset(&U32PARA , 0 ,sizeof(JPEG_GETEXIFU32_PARAM));
11160     U32PARA.pu32Value = pu32ISOSpeedRatings;
11161     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFISOSpeedRatings\n");
11162     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFISOSpeedRatings, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11163     {
11164         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11165     }
11166     return U32PARA.retVal;
11167 #else
11168     return _MApi_JPEG_GetEXIFISOSpeedRatings(pu32ISOSpeedRatings);
11169 #endif
11170 }
11171 
MApi_JPEG_GetEXIFShutterSpeedValue(JPEG_RATIONAL * pShutterSpeedValue)11172 JPEG_Result MApi_JPEG_GetEXIFShutterSpeedValue(JPEG_RATIONAL *pShutterSpeedValue)
11173 {
11174 #if (JPEG_UTOPIA20)
11175     if(pInstantJPD==NULL)
11176     {
11177         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11178         return E_JPEG_FAILED;
11179     }
11180 
11181     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11182     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11183     RATIONALPARA.pRational = pShutterSpeedValue;
11184     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFShutterSpeedValue\n");
11185     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFShutterSpeedValue, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11186     {
11187         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11188     }
11189     return RATIONALPARA.retVal;
11190 #else
11191     return _MApi_JPEG_GetEXIFShutterSpeedValue(pShutterSpeedValue);
11192 #endif
11193 }
11194 
MApi_JPEG_GetEXIFApertureValue(JPEG_RATIONAL * pApertureValue)11195 JPEG_Result MApi_JPEG_GetEXIFApertureValue(JPEG_RATIONAL *pApertureValue)
11196 {
11197 #if (JPEG_UTOPIA20)
11198     if(pInstantJPD==NULL)
11199     {
11200         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11201         return E_JPEG_FAILED;
11202     }
11203 
11204     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11205     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11206     RATIONALPARA.pRational = pApertureValue;
11207     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFApertureValue\n");
11208     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFApertureValue, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11209     {
11210         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11211     }
11212     return RATIONALPARA.retVal;
11213 #else
11214     return _MApi_JPEG_GetEXIFApertureValue(pApertureValue);
11215 #endif
11216 }
11217 
MApi_JPEG_GetEXIFExposureBiasValue(JPEG_RATIONAL * pExposureBiasValue)11218 JPEG_Result MApi_JPEG_GetEXIFExposureBiasValue(JPEG_RATIONAL *pExposureBiasValue)
11219 {
11220 #if (JPEG_UTOPIA20)
11221     if(pInstantJPD==NULL)
11222     {
11223         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11224         return E_JPEG_FAILED;
11225     }
11226 
11227     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11228     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11229     RATIONALPARA.pRational = pExposureBiasValue;
11230     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFExposureBiasValue\n");
11231     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFExposureBiasValue, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11232     {
11233         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11234     }
11235     return RATIONALPARA.retVal;
11236 #else
11237     return _MApi_JPEG_GetEXIFExposureBiasValue(pExposureBiasValue);
11238 #endif
11239 }
11240 
MApi_JPEG_GetEXIFFocalLength(JPEG_RATIONAL * pFocalLength)11241 JPEG_Result MApi_JPEG_GetEXIFFocalLength(JPEG_RATIONAL *pFocalLength)
11242 {
11243 #if (JPEG_UTOPIA20)
11244     if(pInstantJPD==NULL)
11245     {
11246         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11247         return E_JPEG_FAILED;
11248     }
11249 
11250     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11251     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11252     RATIONALPARA.pRational = pFocalLength;
11253     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFFocalLength\n");
11254     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFFocalLength, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11255     {
11256         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11257     }
11258     return RATIONALPARA.retVal;
11259 #else
11260     return _MApi_JPEG_GetEXIFFocalLength(pFocalLength);
11261 #endif
11262 }
11263 
MApi_JPEG_GetEXIFImageWidth(MS_U32 * pu32ImageWidth)11264 JPEG_Result MApi_JPEG_GetEXIFImageWidth(MS_U32 *pu32ImageWidth)
11265 {
11266 #if (JPEG_UTOPIA20)
11267     if(pInstantJPD==NULL)
11268     {
11269         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11270         return E_JPEG_FAILED;
11271     }
11272 
11273     JPEG_GETEXIFU32_PARAM U32PARA;
11274     memset(&U32PARA , 0 ,sizeof(JPEG_GETEXIFU32_PARAM));
11275     U32PARA.pu32Value = pu32ImageWidth;
11276     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFImageWidth\n");
11277     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFImageWidth, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11278     {
11279         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11280     }
11281     return U32PARA.retVal;
11282 #else
11283     return _MApi_JPEG_GetEXIFImageWidth(pu32ImageWidth);
11284 #endif
11285 }
11286 
MApi_JPEG_GetEXIFImageHeight(MS_U32 * pu32ImageHeight)11287 JPEG_Result MApi_JPEG_GetEXIFImageHeight(MS_U32 *pu32ImageHeight)
11288 {
11289 #if (JPEG_UTOPIA20)
11290     if(pInstantJPD==NULL)
11291     {
11292         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11293         return E_JPEG_FAILED;
11294     }
11295 
11296     JPEG_GETEXIFU32_PARAM U32PARA;
11297     memset(&U32PARA , 0 ,sizeof(JPEG_GETEXIFU32_PARAM));
11298     U32PARA.pu32Value = pu32ImageHeight;
11299     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFImageHeight\n");
11300     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFImageHeight, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11301     {
11302         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11303     }
11304     return U32PARA.retVal;
11305 #else
11306     return _MApi_JPEG_GetEXIFImageHeight(pu32ImageHeight);
11307 #endif
11308 }
11309 
MApi_JPEG_GetEXIFExposureTime(JPEG_RATIONAL * pExposureTime)11310 JPEG_Result MApi_JPEG_GetEXIFExposureTime(JPEG_RATIONAL *pExposureTime)
11311 {
11312 #if (JPEG_UTOPIA20)
11313     if(pInstantJPD==NULL)
11314     {
11315         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11316         return E_JPEG_FAILED;
11317     }
11318 
11319     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11320     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11321     RATIONALPARA.pRational = pExposureTime;
11322     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFExposureTime\n");
11323     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFExposureTime, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11324     {
11325         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11326     }
11327     return RATIONALPARA.retVal;
11328 #else
11329     return _MApi_JPEG_GetEXIFExposureTime(pExposureTime);
11330 #endif
11331 }
11332 
MApi_JPEG_GetEXIFFNumber(JPEG_RATIONAL * pFNumber)11333 JPEG_Result MApi_JPEG_GetEXIFFNumber(JPEG_RATIONAL *pFNumber)
11334 {
11335 #if (JPEG_UTOPIA20)
11336     if(pInstantJPD==NULL)
11337     {
11338         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11339         return E_JPEG_FAILED;
11340     }
11341 
11342     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11343     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11344     RATIONALPARA.pRational = pFNumber;
11345     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFFNumber\n");
11346     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFFNumber, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11347     {
11348         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11349     }
11350     return RATIONALPARA.retVal;
11351 #else
11352     return _MApi_JPEG_GetEXIFFNumber(pFNumber);
11353 #endif
11354 }
11355 
11356 #endif
11357 
MApi_JPEG_GetFreeMemory(MS_U32 size)11358 MS_PHYADDR MApi_JPEG_GetFreeMemory(MS_U32 size)
11359 {
11360 #if (JPEG_UTOPIA20)
11361     if(pInstantJPD==NULL)
11362     {
11363         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11364         return 0;
11365     }
11366 
11367     JPEG_GETFREEMEMORY_PARAM MEMORYPARA;
11368     memset(&MEMORYPARA , 0 ,sizeof(JPEG_GETFREEMEMORY_PARAM));
11369     MEMORYPARA.size = size;
11370     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetFreeMemory\n");
11371     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetFreeMemory, (void*)(&MEMORYPARA)) != UTOPIA_STATUS_SUCCESS)
11372     {
11373         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11374     }
11375     return MEMORYPARA.addr;
11376 #else
11377     return _MApi_JPEG_GetFreeMemory(size);
11378 #endif
11379 }
11380 
11381 
MApi_JPEG_GetDataOffset(void)11382 MS_U32 MApi_JPEG_GetDataOffset(void)
11383 {
11384 #if (JPEG_UTOPIA20)
11385     if(pInstantJPD==NULL)
11386     {
11387         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11388         return 0;
11389     }
11390 
11391     JPEG_U32_PARAM U32PARA;
11392     memset(&U32PARA , 0 ,sizeof(JPEG_U32_PARAM));
11393     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetDataOffset\n");
11394     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetDataOffset, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11395     {
11396         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11397         return 0;
11398     }
11399     return U32PARA.u32Value;
11400 #else
11401     return _MApi_JPEG_GetDataOffset();
11402 #endif
11403 }
11404 
MApi_JPEG_GetSOFOffset(void)11405 MS_U32 MApi_JPEG_GetSOFOffset(void)
11406 {
11407 #if (JPEG_UTOPIA20)
11408     if(pInstantJPD==NULL)
11409     {
11410         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11411         return 0;
11412     }
11413 
11414     JPEG_U32_PARAM U32PARA;
11415     memset(&U32PARA , 0 ,sizeof(JPEG_U32_PARAM));
11416     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetSOFOffset\n");
11417     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetSOFOffset, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11418     {
11419         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11420         return 0;
11421     }
11422     return U32PARA.u32Value;
11423 #else
11424     return _MApi_JPEG_GetSOFOffset();
11425 #endif
11426 }
11427 
MApi_JPEG_SupportCMYK(MS_BOOL bEnable)11428 JPEG_Result MApi_JPEG_SupportCMYK(MS_BOOL bEnable)
11429 {
11430 #if (JPEG_UTOPIA20)
11431     if (pInstantJPD == NULL)
11432     {
11433         if(UtopiaOpen(MODULE_JPEG, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
11434         {
11435             JPEG_DEBUG_API_ERR("Open JPD fail\n");
11436             return E_JPEG_FAILED;
11437         }
11438     }
11439     if(pInstantJPD==NULL)
11440     {
11441         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11442         UtopiaClose(pInstantJPD);
11443         return E_JPEG_FAILED;
11444     }
11445 
11446     JPEG_SUPPORTCMYK_PARAM CMYKRGB;
11447     memset(&CMYKRGB , 0 ,sizeof(JPEG_SUPPORTCMYK_PARAM));
11448     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SupportCMYK\n");
11449     CMYKRGB.bEnable = bEnable;
11450     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SupportCMYK, (void*)(&CMYKRGB)) != UTOPIA_STATUS_SUCCESS)
11451     {
11452         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11453         UtopiaClose(pInstantJPD);
11454         pInstantJPD = NULL;
11455         return E_JPEG_FAILED;
11456     }
11457     return CMYKRGB.retVal;
11458 #else
11459     return _MApi_JPEG_SupportCMYK(bEnable);
11460 #endif
11461 }
11462 
MApi_JPEG_SupportRGB(MS_BOOL bEnable)11463 JPEG_Result MApi_JPEG_SupportRGB(MS_BOOL bEnable)
11464 {
11465 #if (JPEG_UTOPIA20)
11466     if (pInstantJPD == NULL)
11467     {
11468         if(UtopiaOpen(MODULE_JPEG, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
11469         {
11470             JPEG_DEBUG_API_ERR("Open JPD fail\n");
11471             return E_JPEG_FAILED;
11472         }
11473     }
11474     if(pInstantJPD==NULL)
11475     {
11476         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11477         UtopiaClose(pInstantJPD);
11478         return E_JPEG_FAILED;
11479     }
11480 
11481     JPEG_SUPPORTCMYK_PARAM CMYKRGB;
11482     memset(&CMYKRGB , 0 ,sizeof(JPEG_SUPPORTCMYK_PARAM));
11483     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SupportRGB\n");
11484     CMYKRGB.bEnable = bEnable;
11485     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SupportRGB, (void*)(&CMYKRGB)) != UTOPIA_STATUS_SUCCESS)
11486     {
11487         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11488         UtopiaClose(pInstantJPD);
11489         pInstantJPD = NULL;
11490         return E_JPEG_FAILED;
11491     }
11492     return CMYKRGB.retVal;
11493 #else
11494     return _MApi_JPEG_SupportRGB(bEnable);
11495 #endif
11496 }
11497 
MApi_JPEG_SetMHEG5(MS_BOOL bEnable)11498 void MApi_JPEG_SetMHEG5(MS_BOOL bEnable)
11499 {
11500 #if (JPEG_UTOPIA20)
11501     if(pInstantJPD==NULL)
11502     {
11503         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11504         return;
11505     }
11506 
11507     JPEG_BOOL_PARAM bPARA;
11508     memset(&bPARA , 0 ,sizeof(JPEG_BOOL_PARAM));
11509     bPARA.bValue= bEnable;
11510     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMHEG5\n");
11511     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMHEG5, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
11512     {
11513         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11514         return;
11515     }
11516     return;
11517 #else
11518     _MApi_JPEG_SetMHEG5(bEnable);
11519 #endif
11520 }
11521 
11522 
11523 #if SUPPORT_MPO_FORMAT
11524 //For MPO
MApi_JPEG_IsMPOFormat(void)11525 MS_BOOL MApi_JPEG_IsMPOFormat(void)
11526 {
11527 #if (JPEG_UTOPIA20)
11528     if(pInstantJPD==NULL)
11529     {
11530         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11531         return 0;
11532     }
11533 
11534     JPEG_BOOL_PARAM bPARA;
11535     memset(&bPARA , 0 ,sizeof(JPEG_BOOL_PARAM));
11536     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_IsMPOFormat\n");
11537     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_IsMPOFormat, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
11538     {
11539         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11540         return 0;
11541     }
11542     return bPARA.bValue;
11543 #else
11544     return _MApi_JPEG_IsMPOFormat();
11545 #endif
11546 }
11547 
11548 
MApi_JPEG_GetMPOIndex(JPEG_MPO_INDEX_INFO ** ppMPOIndex)11549 MS_BOOL MApi_JPEG_GetMPOIndex(JPEG_MPO_INDEX_INFO **ppMPOIndex)
11550 {
11551 #if (JPEG_UTOPIA20)
11552     if(pInstantJPD==NULL)
11553     {
11554         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11555         return 0;
11556     }
11557 
11558     JPEG_GETMPOINDEX_PARAM eMPOIndex;
11559     memset(&eMPOIndex , 0 ,sizeof(JPEG_GETMPOINDEX_PARAM));
11560     eMPOIndex.ppMPOIndex = ppMPOIndex;
11561     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetMPOIndex\n");
11562     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetMPOIndex, (void*)(&eMPOIndex)) != UTOPIA_STATUS_SUCCESS)
11563     {
11564         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11565         return 0;
11566     }
11567     return eMPOIndex.bretVal;
11568 #else
11569     return _MApi_JPEG_GetMPOIndex(ppMPOIndex);
11570 #endif
11571 }
11572 
MApi_JPEG_GetMPOAttr(MS_U32 image_no,JPEG_MPO_ATTRIBUTE_INFO ** ppMPOAttr)11573 MS_BOOL MApi_JPEG_GetMPOAttr(MS_U32 image_no, JPEG_MPO_ATTRIBUTE_INFO **ppMPOAttr)
11574 {
11575 #if (JPEG_UTOPIA20)
11576     if(pInstantJPD==NULL)
11577     {
11578         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11579         return 0;
11580     }
11581 
11582     JPEG_GETMPOATTR_PARAM eMPOAttr;
11583     memset(&eMPOAttr , 0 ,sizeof(JPEG_GETMPOATTR_PARAM));
11584     eMPOAttr.image_no = image_no;
11585     eMPOAttr.ppMPOAttr = ppMPOAttr;
11586     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetMPOAttr\n");
11587     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetMPOAttr, (void*)(&eMPOAttr)) != UTOPIA_STATUS_SUCCESS)
11588     {
11589         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11590         return 0;
11591     }
11592     return eMPOAttr.bretVal;
11593 #else
11594     return _MApi_JPEG_GetMPOAttr(image_no, ppMPOAttr);
11595 #endif
11596 }
11597 
MApi_JPEG_DumpMPO(void)11598 void MApi_JPEG_DumpMPO(void)
11599 {
11600 #if (JPEG_UTOPIA20)
11601     if(pInstantJPD==NULL)
11602     {
11603         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11604         return;
11605     }
11606 
11607     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_DumpMPO\n");
11608     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_DumpMPO, NULL) != UTOPIA_STATUS_SUCCESS)
11609     {
11610         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11611     }
11612 #else
11613     _MApi_JPEG_DumpMPO();
11614 #endif
11615 }
11616 
MApi_JPEG_SetMPOBuffer(MS_U32 read_offset,MS_U32 output_start)11617 MS_BOOL MApi_JPEG_SetMPOBuffer(MS_U32 read_offset, MS_U32 output_start)
11618 {
11619 #if (JPEG_UTOPIA20)
11620     if(pInstantJPD==NULL)
11621     {
11622         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11623         return 0;
11624     }
11625 
11626     JPEG_SETMPOBUFFER_PARAM eMPOBuff;
11627     memset(&eMPOBuff , 0 ,sizeof(JPEG_SETMPOBUFFER_PARAM));
11628     eMPOBuff.read_offset = read_offset;
11629     eMPOBuff.output_start = output_start;
11630     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMPOBuffer\n");
11631     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMPOBuffer, (void*)(&eMPOBuff)) != UTOPIA_STATUS_SUCCESS)
11632     {
11633         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11634         return 0;
11635     }
11636     return eMPOBuff.bretVal;
11637 #else
11638     return _MApi_JPEG_SetMPOBuffer(read_offset, output_start);
11639 #endif
11640 }
11641 
MApi_JPEG_SetMPOMaxDecodeResolution(MS_U16 u16Width,MS_U16 u16Height)11642 void MApi_JPEG_SetMPOMaxDecodeResolution(MS_U16 u16Width, MS_U16 u16Height)
11643 {
11644 #if (JPEG_UTOPIA20)
11645     if (pInstantJPD == NULL)
11646     {
11647         if(UtopiaOpen(MODULE_JPEG, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
11648         {
11649             JPEG_DEBUG_API_ERR("Open JPD fail\n");
11650             return;
11651         }
11652     }
11653     if(pInstantJPD==NULL)
11654     {
11655         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11656         UtopiaClose(pInstantJPD);
11657         return;
11658     }
11659 
11660     JPEG_RESOLUTION_PARAM Resol;
11661     memset(&Resol , 0 ,sizeof(JPEG_RESOLUTION_PARAM));
11662     Resol.u16Width= u16Width;
11663     Resol.u16Height= u16Height;
11664     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMPOMaxDecodeResolution\n");
11665     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMPOMaxDecodeResolution, (void*)(&Resol)) != UTOPIA_STATUS_SUCCESS)
11666     {
11667         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11668         UtopiaClose(pInstantJPD);
11669         pInstantJPD = NULL;
11670     }
11671 #else
11672     _MApi_JPEG_SetMPOMaxDecodeResolution(u16Width, u16Height);
11673 #endif
11674 }
11675 
MApi_JPEG_SetMPOProMaxDecodeResolution(MS_U16 u16ProWidth,MS_U16 u16ProHeight)11676 void MApi_JPEG_SetMPOProMaxDecodeResolution(MS_U16 u16ProWidth, MS_U16 u16ProHeight)
11677 {
11678 #if (JPEG_UTOPIA20)
11679     if (pInstantJPD == NULL)
11680     {
11681         if(UtopiaOpen(MODULE_JPEG, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
11682         {
11683             JPEG_DEBUG_API_ERR("Open JPD fail\n");
11684             return;
11685         }
11686     }
11687     if(pInstantJPD==NULL)
11688     {
11689         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11690         UtopiaClose(pInstantJPD);
11691         return;
11692     }
11693 
11694     JPEG_RESOLUTION_PARAM Resol;
11695     memset(&Resol , 0 ,sizeof(JPEG_RESOLUTION_PARAM));
11696     Resol.u16Width= u16ProWidth;
11697     Resol.u16Height= u16ProHeight;
11698     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMPOProMaxDecodeResolution\n");
11699     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMPOProMaxDecodeResolution, (void*)(&Resol)) != UTOPIA_STATUS_SUCCESS)
11700     {
11701         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11702         UtopiaClose(pInstantJPD);
11703         pInstantJPD = NULL;
11704     }
11705 #else
11706     _MApi_JPEG_SetMPOProMaxDecodeResolution(u16ProWidth, u16ProHeight);
11707 #endif
11708 }
11709 
11710 #endif
11711 
MApi_JPEG_GetControl(EN_JPEG_GET_CTRL_ID eGetID,MS_U32 * param,MS_U32 u32size)11712 JPEG_Result MApi_JPEG_GetControl(EN_JPEG_GET_CTRL_ID eGetID, MS_U32 *param, MS_U32 u32size)
11713 {
11714 #if (JPEG_UTOPIA20)
11715     if(pInstantJPD==NULL)
11716     {
11717         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11718         return E_JPEG_FAILED;
11719     }
11720 
11721     JPEG_GET_CTRL_PARAM eGetCTRL;
11722     memset(&eGetCTRL , 0 ,sizeof(JPEG_GET_CTRL_PARAM));
11723     eGetCTRL.eGetID = eGetID;
11724     eGetCTRL.param = param;
11725     eGetCTRL.u32size = u32size;
11726 
11727     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetControl\n");
11728     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetControl, (void*)(&eGetCTRL)) != UTOPIA_STATUS_SUCCESS)
11729     {
11730         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11731         return E_JPEG_FAILED;
11732     }
11733     return eGetCTRL.retVal;
11734 #else
11735     return _MApi_JPEG_GetControl(eGetID, param, u32size);
11736 #endif
11737 }
11738 
MApi_JPEG_EnableOJPD(MS_BOOL bOnOff)11739 void MApi_JPEG_EnableOJPD(MS_BOOL bOnOff)
11740 {
11741     // wrapper only
11742     UNUSED(bOnOff);
11743     return;
11744 }
MApi_JPEG_Init_UsingOJPD(JPEG_InitParam * pInitParam)11745 JPEG_Result MApi_JPEG_Init_UsingOJPD(JPEG_InitParam *pInitParam)
11746 {
11747     // wrapper only
11748     UNUSED(pInitParam);
11749     return E_JPEG_FAILED;
11750 }
MApi_JPEG_SetNJPDInstance(MS_U8 JPDNum)11751 void MApi_JPEG_SetNJPDInstance(MS_U8 JPDNum)
11752 {
11753     // wrapper only
11754     UNUSED(JPDNum);
11755     return;
11756 }
MApi_JPEG_SetVerificationMode(NJPEG_VerificationMode VerificationMode)11757 void MApi_JPEG_SetVerificationMode(NJPEG_VerificationMode VerificationMode)
11758 {
11759     // wrapper only
11760     UNUSED(VerificationMode);
11761     return;
11762 }
MApi_JPEG_GetVerificationMode(void)11763 NJPEG_VerificationMode MApi_JPEG_GetVerificationMode(void)
11764 {
11765     // wrapper only
11766     return E_NJPEG00_NONE;
11767 }
MApi_NJPD_Debug(void)11768 void MApi_NJPD_Debug(void)
11769 {
11770     // wrapper only
11771     return;
11772 }
11773 
11774 
11775