xref: /utopia/UTPA2-700.0.x/modules/ojpd_vdec_v2/api/jpeg_ex/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     0
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 MJPEG_SW_PARSING_IN_MIU0      false // true
170 #define PRINT_JPD_DECODE_TIME   0
171 #define LOG_DATA_TO_USB         0
172 
173 
174 #if PRINT_JPD_DECODE_TIME
175 #include "../drv/wdt/drvWDT.h"
176 #endif
177 
178 //-------------------------------------------------------------------------------------------------
179 //  Local Defines
180 //-------------------------------------------------------------------------------------------------
181 // Max. allocated blocks
182 #ifdef CMODEL
183 #define JPEG_MAXBLOCKS    100
184 #else
185 #define JPEG_MAXBLOCKS    50
186 #endif
187 
188 #define DEFAULT_DECODE_TIMEOUT  100
189 
190 //fractional bits in scale factors
191 #define IFAST_SCALE_BITS    2
192 
193 #define JPEG_TIFF_SOI_OFFSET        0x0201
194 #define JPEG_TIFF_JPEG_IMG_BYTES    0x0202
195 
196 #define JPEG_TIFF_BIG_ENDIAN        0x4D4D
197 #define JPEG_TIFF_LITTLE_ENDIAN     0x4949
198 
199 //EXIF Tag
200 #define JPEG_EXIF_TAG_MANUFACTURER              0x010F
201 #define JPEG_EXIF_TAG_MODEL                     0x0110
202 #define JPEG_EXIF_TAG_ORIENTATION               0x0112
203 #define JPEG_EXIF_TAG_DATETIME_MOD              0x0132
204 #define JPEG_EXIF_TAG_EXPOSURE_TIME             0x829A
205 #define JPEG_EXIF_TAG_F_NUMBER                  0x829D
206 #define JPEG_EXIF_TAG_IFD_POINTER               0x8769
207 #define JPEG_EXIF_TAG_EXPOSURE_PROGRAM          0x8822
208 #define JPEG_EXIF_TAG_ISO_SPEED_RATING          0x8827
209 #define JPEG_EXIF_TAG_DATETIME_ORI              0x9003
210 #define JPEG_EXIF_TAG_SHUTTER_SPEED             0x9201
211 #define JPEG_EXIF_TAG_APERTURE                  0x9202
212 #define JPEG_EXIF_TAG_EXPOSURE_BIAS             0x9204
213 #define JPEG_EXIF_TAG_FLASH                     0x9209
214 #define JPEG_EXIF_TAG_FOCAL_LENGTH              0x920A
215 #define JPEG_EXIF_TAG_IMAGE_WIDTH               0xA002
216 #define JPEG_EXIF_TAG_IMAGE_HEIGHT              0xA003
217 
218 #define JPEG_MANUFACTURER_SIZE                  32
219 #define JPEG_MODEL_SIZE                         128
220 
221 #define MIN_READBUFFER_SIZE 128
222 
223 #define MRC_BUFFER_ADDR         _u32ReadBufferAddr
224 #define MRC_BUFFER_SIZE         _u32ReadBufferSize
225 #define MWC_BUFFER_ADDR         _u32WriteBufferAddr
226 #define MWC_BUFFER_SIZE         _u32WriteBufferSize
227 #define INTERNAL_BUFFER_ADDR    _u32InternalBufferAddr
228 #define INTERNAL_BUFFER_SIZE    _u32InternalBufferSize
229 
230 #define HUFF_EXTEND_TBL(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
231 
232 #define HUFF_EXTEND_P(x,s)  HUFF_EXTEND_TBL(x,s)
233 
234 /* EXIF parsing section */
235 #define EndianChangeL(_x)   \
236     ((((_x) & 0xff) << 24) | (((_x) & 0xff00) << 8) | (((_x) & 0xff0000) >> 8) | (((_x) & 0xff000000) >> 24))
237 
238 #define EndianChangeS(_x)   \
239     ((((_x) & 0xff) << 8) | (((_x) & 0xff00) >> 8))
240 
241 #define JPEG_TAG_EXIF   EndianChangeL(0x45786966)
242 
243 #define JPEG_ABS(x)     (((x)>=0)?(x):(-(x)))
244 
245 
246 static APP0_Unit App0_Unit_Data;
247 
248 // No need to do ZAG order in JPD mode
249 #if SW_JPD_RGB_CMYK
250 JPEG_STATIC MS_U16 _u16PaddingMcuNumber;
251 
252 MS_U32 u32_Decode_Line;
253 MS_U8 *u8Out_buf;
254 //#define DCTSIZE 8
255 //#define DCTELEM int
256 #define PASS1_BITS  2
257 #define FIX_3_0727  ((MS_S32)25172)  /* FIX(3.072711026) */
258 #define FIX_2_5629  ((MS_S32)20995)  /* FIX(2.562915447) */
259 #define FIX_2_0531  ((MS_S32)16819)  /* FIX(2.053119869) */
260 #define FIX_1_9615  ((MS_S32)16069)  /* FIX(1.961570560) */
261 #define FIX_1_8477  ((MS_S32)15137)  /* FIX(1.847759065) */
262 #define FIX_1_5013  ((MS_S32)12299)  /* FIX(1.501321110) */
263 #define FIX_1_1758  ((MS_S32)9633)   /* FIX(1.175875602) */
264 #define FIX_0_8999  ((MS_S32)7373)   /* FIX(0.899976223) */
265 #define FIX_0_7653  ((MS_S32)6270)   /* FIX(0.765366865) */
266 #define FIX_0_5411  ((MS_S32)4433)   /* FIX(0.541196100) */
267 #define FIX_0_3901  ((MS_S32)3196)   /* FIX(0.390180644) */
268 #define FIX_0_2986  ((MS_S32)2446)   /* FIX(0.298631336) */
269 #define SCALE_DONE ((MS_S32) 1)
270 #define DESCALE(a,m)  (((a) + (SCALE_DONE << ((m)-1))) >> (m))
271 #define clamp(i) if (i & 0xFF00) i = (((~i) >> 15) & 0xFF);
272 #define SCALEBITS 16
273 #define ONE_HALF ((MS_S32) 1 << (SCALEBITS-1))
274 #define FIX(x) ((MS_S32) ((x) * (1L<<SCALEBITS) + 0.5))
275 
276 
277 #define HUFF_EXTEND(x,s)    ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
278 
279 #ifdef CMODEL
280 // Same as drvJPD.h
281 // JPD Downscale Ratio
282 // Bellows are 1, 1/2, 1/4 and 1/8 in order
283 typedef enum
284 {
285     E_JPD_DOWNSCALE_ORG     = 0x00
286   , E_JPD_DOWNSCALE_HALF    = 0x01
287   , E_JPD_DOWNSCALE_FOURTH  = 0x02
288   , E_JPD_DOWNSCALE_EIGHTH  = 0x03
289 } JPD_DownScale;
290 #endif
291 #endif
292 
293 #if LOG_DATA_TO_USB
294 FILE *logBinfp;
295 
296 #define JPEG_DEBUG_API_MSG(format, args...)  do{if(logBinfp) fprintf(logBinfp, format, ##args);}while(0)
297 #define JPEG_DEBUG_API_ERR(format, args...)  do{if(logBinfp)  fprintf(logBinfp, format, ##args);}while(0)
298 #else
299 
300 #ifndef ANDROID
301 #define JPEG_DEBUG_API_MSG(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API) printf(format, ##args);}while(0)
302 #define JPEG_DEBUG_API_ERR(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_ERR) printf(format, ##args);}while(0)
303 #define UTOPIA_20_DBG(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API) printf(format, ##args);}while(0)
304 #else
305 #define LOG_TAG "API_JPEG"
306 #include <cutils/log.h>
307 #ifndef LOGD
308 #define JPEG_DEBUG_API_MSG(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API) ALOGI(format, ##args);}while(0)
309 #define JPEG_DEBUG_API_ERR(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_ERR) ALOGE(format, ##args);}while(0)
310 #define UTOPIA_20_DBG(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API) ALOGE(format, ##args);}while(0)
311 #else
312 #define JPEG_DEBUG_API_MSG(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API) LOGI(format, ##args);}while(0)
313 #define JPEG_DEBUG_API_ERR(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_ERR) LOGE(format, ##args);}while(0)
314 #define UTOPIA_20_DBG(format, args...)  do{if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API) LOGE(format, ##args);}while(0)
315 #endif
316 
317 #endif
318 
319 #endif
320 #define printf_red(args...)           do{ printf("\033[1;31m"); printf(args); printf("\033[m"); }while(0)
321 //#define UTOPIA_20_DBG(args...)        //do{ printf("\033[1;31m"); printf(args); printf("\033[m"); }while(0)
322 
323 #define JPEG_OVER_BUFFER_RET(addr, buf_addr, buf_size) \
324 do \
325 { \
326     if(((MS_U32)(addr) < (MS_U32)(buf_addr)) \
327     || ((MS_U32)(addr) >= ((MS_U32)(buf_addr) + (MS_U32)(buf_size)))) \
328     { \
329         JPEG_DEBUG_API_ERR("%s [%d] invalid address 0x%lx\n", __FUNCTION__, __LINE__, (MS_U32)(addr)); \
330         return FALSE; \
331     } \
332 } \
333 while(0)
334 
335 //-------------------------------------------------------------------------------------------------
336 //  Local Structures
337 //-------------------------------------------------------------------------------------------------
338 //JPEG header marker id
339 typedef enum
340 {
341     E_JPEG_SOF0     = 0xC0
342   , E_JPEG_SOF1     = 0xC1
343   , E_JPEG_SOF2     = 0xC2
344   , E_JPEG_SOF3     = 0xC3
345   , E_JPEG_SOF5     = 0xC5
346   , E_JPEG_SOF6     = 0xC6
347   , E_JPEG_SOF7     = 0xC7
348   , E_JPEG_JPG      = 0xC8
349   , E_JPEG_SOF9     = 0xC9
350   , E_JPEG_SOF10    = 0xCA
351   , E_JPEG_SOF11    = 0xCB
352   , E_JPEG_SOF13    = 0xCD
353   , E_JPEG_SOF14    = 0xCE
354   , E_JPEG_SOF15    = 0xCF
355   , E_JPEG_DHT      = 0xC4
356   , E_JPEG_DAC      = 0xCC
357   , E_JPEG_RST0     = 0xD0
358   , E_JPEG_RST1     = 0xD1
359   , E_JPEG_RST2     = 0xD2
360   , E_JPEG_RST3     = 0xD3
361   , E_JPEG_RST4     = 0xD4
362   , E_JPEG_RST5     = 0xD5
363   , E_JPEG_RST6     = 0xD6
364   , E_JPEG_RST7     = 0xD7
365   , E_JPEG_SOI      = 0xD8
366   , E_JPEG_EOI      = 0xD9
367   , E_JPEG_SOS      = 0xDA
368   , E_JPEG_DQT      = 0xDB
369   , E_JPEG_DNL      = 0xDC
370   , E_JPEG_DRI      = 0xDD
371   , E_JPEG_DHP      = 0xDE
372   , E_JPEG_EXP      = 0xDF
373   , E_JPEG_APP0     = 0xE0
374   , E_JPEG_APP1     = 0xE1
375   , E_JPEG_APP2     = 0xE2
376   , E_JPEG_APP3     = 0xE3
377   , E_JPEG_APP4     = 0xE4
378   , E_JPEG_APP5     = 0xE5
379   , E_JPEG_APP6     = 0xE6
380   , E_JPEG_APP7     = 0xE7
381   , E_JPEG_APP8     = 0xE8
382   , E_JPEG_APP9     = 0xE9
383   , E_JPEG_APP10     = 0xEA
384   , E_JPEG_APP11     = 0xEB
385   , E_JPEG_APP12     = 0xEC
386   , E_JPEG_APP13     = 0xED
387   , E_JPEG_APP14     = 0xEE
388   , E_JPEG_APP15    = 0xEF
389   , E_JPEG_JPG0     = 0xF0
390   , E_JPEG_JPG1     = 0xF1
391   , E_JPEG_JPG2     = 0xF2
392   , E_JPEG_JPG3     = 0xF3
393   , E_JPEG_JPG4     = 0xF4
394   , E_JPEG_JPG5     = 0xF5
395   , E_JPEG_JPG6     = 0xF6
396   , E_JPEG_JPG7     = 0xF7
397   , E_JPEG_JPG8     = 0xF8
398   , E_JPEG_JPG9     = 0xF9
399   , E_JPEG_JPG10     = 0xFA
400   , E_JPEG_JPG11     = 0xFB
401   , E_JPEG_JPG12     = 0xFC
402   , E_JPEG_JPG13    = 0xFD
403   , E_JPEG_COM      = 0xFE
404   , E_JPEG_TEM      = 0x01
405   , E_JPEG_ERROR    = 0x100
406 } JPEG_HdrMarker;
407 
408 //------------------------------------------------------------------------------
409 typedef enum
410 {
411     E_RLE_DC = 0,
412     E_RLE_AC,
413     E_RLE_ZRL,
414     E_RLE_EOB,
415 } JPEG_RLESymbol;
416 //-----------------------------------------------------------------------------
417 /// @brief \b Struct \b Name: JPEG_CoeffBuf
418 /// @brief \b Struct \b Description: The info of coefficient for JPEG decode
419 //-----------------------------------------------------------------------------
420 typedef struct
421 {
422     MS_U8 *pu8Data;         ///<data of coefficient of DC, AC
423     MS_U16 u16Block_num_x;  ///<the number of block for width
424     MS_U16 u16Block_num_y;  ///<the number of block for height
425     MS_U16 u16Block_size;   ///<block size
426     MS_U8 u8Block_len_x;    ///<The width of block
427     MS_U8 u8Block_len_y;    ///<The height of block
428 } JPEG_CoeffBuf, *PJPEG_CoeffBuf;
429 //-----------------------------------------------------------------------------
430 /// @brief \b Struct \b Name: JPEG_SVLD
431 /// @brief \b Struct \b Description: The info of SVLD for JPEG decode
432 //-----------------------------------------------------------------------------
433 typedef struct
434 {
435     union
436     {
437         struct
438         {
439             MS_U32 amp          :11;    ///<The amplitude of value of VLI
440             MS_U32 sign         :1;     ///<The sign of value of VLI
441             MS_U32 run          :4;     ///<run value
442             MS_U32 sym_type     :2;     ///<symbol type
443             MS_U32 blk_type     :2;     ///<YUV type
444             MS_U32 EOP          :1;     ///<End of picture
445             MS_U32 trash        :11;    ///<reserved
446         };
447 
448         struct
449         {
450             MS_U8 byte0;    ///<byte0 of SVLD
451             MS_U8 byte1;    ///<byte1 of SVLD
452             MS_U8 byte2;    ///<byte2 of SVLD
453             MS_U8 byte3;    ///<byte3 of SVLD
454         };
455     };
456 } JPEG_SVLD;
457 //-----------------------------------------------------------------------------
458 /// @brief \b Struct \b Name: JPEG_HdrChk
459 /// @brief \b Struct \b Description: The info of header checking for JPEG decode
460 //-----------------------------------------------------------------------------
461 typedef union
462 {
463     struct
464     {
465         MS_U8 DQT:1;///<has Quant Table?
466         MS_U8 DHT:1;///<has Huffman Table?
467 //      MS_U8 SOF:1;
468 //      MS_U8 SOS:1;
469     };
470     MS_U8 result;   ///<wildcard for header check
471 } JPEG_HdrChk;
472 //------------------------------------------------------------------------------
473 typedef MS_BOOL ( *Pdecode_block_func )( MS_U8, MS_U16, MS_U16 );
474 
475 
476 //-------------------------------------------------------------------------------------------------
477 //  Global Variables
478 //-------------------------------------------------------------------------------------------------
479 
480 
481 //-------------------------------------------------------------------------------------------------
482 //  Local Variables
483 //-------------------------------------------------------------------------------------------------
484 #ifdef CMODEL
485 JPEG_STATIC MS_U16 JPEG_MAX_HEIGHT = 0xFFFF;
486 JPEG_STATIC MS_U16 JPEG_MAX_WIDTH = 0xFFFF;
487 #else
488 JPEG_STATIC MS_U16 JPEG_MAX_HEIGHT = 1200;
489 JPEG_STATIC MS_U16 JPEG_MAX_WIDTH = 1600;
490 #endif
491 
492 // max progressive resolution setting
493 JPEG_STATIC MS_U16 JPEG_PRO_MAX_HEIGHT  = 768;
494 JPEG_STATIC MS_U16 JPEG_PRO_MAX_WIDTH   = 1024;
495 
496 JPEG_STATIC MS_U16 MAX_JPEG_WIDTH_HD    = 0;
497 JPEG_STATIC MS_U16 MAX_JPEG_HEIGHT_HD   = 0;
498 
499 #if SUPPORT_MPO_FORMAT
500 JPEG_STATIC MS_U16 JPEG_MPO_MAX_HEIGHT = 1200;
501 JPEG_STATIC MS_U16 JPEG_MPO_MAX_WIDTH = 1600;
502 JPEG_STATIC MS_U16 JPEG_MPO_PRO_MAX_HEIGHT  = 768;
503 JPEG_STATIC MS_U16 JPEG_MPO_PRO_MAX_WIDTH   = 1024;
504 #endif
505 //------------------------------------------------------------------------------
506 #if 0
507 // Default Table for JPEG
508 const MS_U16 g16GRPINFO_TBL[128]=
509 {
510 0x0000,
511 0x0000,
512 0x0000,
513 0x0100,
514 0x0010,
515 0x0140,
516 0x0060,
517 0x01e0,
518 0x0070,
519 0x01f0,
520 0x0080,
521 0x01f8,
522 0x0090,
523 0x01fc,
524 0x00a0,
525 0x01fe,
526 0x00b0,
527 0x01ff,
528 0x0000,
529 0x0000,
530 0x0000,
531 0x0000,
532 0x0000,
533 0x0000,
534 0x0000,
535 0x0000,
536 0x0000,
537 0x0000,
538 0x0000,
539 0x0000,
540 0x0000,
541 0x0000,
542 0x0000,
543 0x0000,
544 0x0000,
545 0x0100,
546 0x0030,
547 0x01c0,
548 0x0040,
549 0x01e0,
550 0x0050,
551 0x01f0,
552 0x0060,
553 0x01f8,
554 0x0070,
555 0x01fc,
556 0x0080,
557 0x01fe,
558 0x0090,
559 0x01ff,
560 0x80a0,
561 0x01ff,
562 0xc0b0,
563 0x01ff,
564 0x0000,
565 0x0000,
566 0x0000,
567 0x0000,
568 0x0000,
569 0x0000,
570 0x0000,
571 0x0000,
572 0x0000,
573 0x0000,
574 0x0000,
575 0x0000,
576 0x0000,
577 0x0100,
578 0x0002,
579 0x0180,
580 0x0003,
581 0x01a0,
582 0x0006,
583 0x01d0,
584 0x0009,
585 0x01e8,
586 0x000b,
587 0x01f0,
588 0x000f,
589 0x01f8,
590 0x0012,
591 0x01fb,
592 0x8017,
593 0x01fd,
594 0xc01c,
595 0x01fe,
596 0x4020,
597 0x01ff,
598 0x0000,
599 0x0000,
600 0x0000,
601 0x0000,
602 0x8024,
603 0x01ff,
604 0x8225,
605 0x01ff,
606 0x0000,
607 0x0000,
608 0x0000,
609 0x0100,
610 0x0002,
611 0x0180,
612 0x0003,
613 0x01a0,
614 0x0005,
615 0x01c0,
616 0x0009,
617 0x01e0,
618 0x000d,
619 0x01f0,
620 0x0010,
621 0x01f6,
622 0x0014,
623 0x01fa,
624 0x801b,
625 0x01fd,
626 0xc020,
627 0x01fe,
628 0x4024,
629 0x01ff,
630 0x0000,
631 0x0000,
632 0x8028,
633 0x01ff,
634 0x8429,
635 0x01ff,
636 0x882b,
637 0x01ff
638 };
639 #endif
640 #if 0
641 const MS_U16 g16SYMIDX_TBL[]=
642 {
643 0x0001,
644 0x0102,
645 0x0203,
646 0x0300,
647 0x1104,
648 0x0411,
649 0x0505,
650 0x2112,
651 0x3121,
652 0x0631,
653 0x1241,
654 0x4106,
655 0x5113,
656 0x0751,
657 0x6161,
658 0x7107,
659 0x1322,
660 0x2271,
661 0x3214,
662 0x8132,
663 0x0881,
664 0x1491,
665 0x42a1,
666 0x9108,
667 0xa123,
668 0xb142,
669 0xc1b1,
670 0x09c1,
671 0x2315,
672 0x3352,
673 0x52d1,
674 0xf0f0,
675 0x1524,
676 0x6233,
677 0x7262,
678 0xd172,
679 0x0a82,
680 0x1609,
681 0x240a,
682 0x3416,
683 0xe117,
684 0x2518,
685 0xf119,
686 0x171a,
687 0x1825,
688 0x1926,
689 0x1a27,
690 0x2628,
691 0x2729,
692 0x282a,
693 0x2934,
694 0x2a35,
695 0x3536,
696 0x3637,
697 0x3738,
698 0x3839,
699 0x393a,
700 0x3a43,
701 0x4344,
702 0x4445,
703 0x4546,
704 0x4647,
705 0x4748,
706 0x4849,
707 0x494a,
708 0x4a53,
709 0x5354,
710 0x5455,
711 0x5556,
712 0x5657,
713 0x5758,
714 0x5859,
715 0x595a,
716 0x5a63,
717 0x6364,
718 0x6465,
719 0x6566,
720 0x6667,
721 0x6768,
722 0x6869,
723 0x696a,
724 0x6a73,
725 0x7374,
726 0x7475,
727 0x7576,
728 0x7677,
729 0x7778,
730 0x7879,
731 0x797a,
732 0x7a83,
733 0x8284,
734 0x8385,
735 0x8486,
736 0x8587,
737 0x8688,
738 0x8789,
739 0x888a,
740 0x8992,
741 0x8a93,
742 0x9294,
743 0x9395,
744 0x9496,
745 0x9597,
746 0x9698,
747 0x9799,
748 0x989a,
749 0x99a2,
750 0x9aa3,
751 0xa2a4,
752 0xa3a5,
753 0xa4a6,
754 0xa5a7,
755 0xa6a8,
756 0xa7a9,
757 0xa8aa,
758 0xa9b2,
759 0xaab3,
760 0xb2b4,
761 0xb3b5,
762 0xb4b6,
763 0xb5b7,
764 0xb6b8,
765 0xb7b9,
766 0xb8ba,
767 0xb9c2,
768 0xbac3,
769 0xc2c4,
770 0xc3c5,
771 0xc4c6,
772 0xc5c7,
773 0xc6c8,
774 0xc7c9,
775 0xc8ca,
776 0xc9d2,
777 0xcad3,
778 0xd2d4,
779 0xd3d5,
780 0xd4d6,
781 0xd5d7,
782 0xd6d8,
783 0xd7d9,
784 0xd8da,
785 0xd9e1,
786 0xdae2,
787 0xe2e3,
788 0xe3e4,
789 0xe4e5,
790 0xe5e6,
791 0xe6e7,
792 0xe7e8,
793 0xe8e9,
794 0xe9ea,
795 0xeaf1,
796 0xf2f2,
797 0xf3f3,
798 0xf4f4,
799 0xf5f5,
800 0xf6f6,
801 0xf7f7,
802 0xf8f8,
803 0xf9f9,
804 0xfafa,
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 0x0000,
876 0x0000,
877 0x0000,
878 0x0000,
879 0x0000,
880 0x0000,
881 0x0000,
882 0x0000,
883 0x0000,
884 0x0000,
885 0x0000,
886 0x0000,
887 0x0000,
888 0x0000,
889 0x0000,
890 0x0000,
891 0x0000,
892 0x0000,
893 0x0000,
894 0x0000,
895 0x0000,
896 0x0000,
897 0x0000,
898 0x0000,
899 0x0000,
900 0x0101,
901 0x0202,
902 0x0303,
903 0x0404,
904 0x0505,
905 0x0606,
906 0x0707,
907 0x0808,
908 0x0909,
909 0x0a0a,
910 0x0b0b,
911 0x0000,
912 0x0000,
913 0x0000,
914 0x0000
915 };
916 #endif
917 #if 0
918 const MS_U16 g16IQ_TBL[128]=
919 {
920 0x0010,
921 0x000b,
922 0x000a,
923 0x0010,
924 0x0018,
925 0x0028,
926 0x0033,
927 0x003d,
928 0x000c,
929 0x000c,
930 0x000e,
931 0x0013,
932 0x001a,
933 0x003a,
934 0x003c,
935 0x0037,
936 0x000e,
937 0x000d,
938 0x0010,
939 0x0018,
940 0x0028,
941 0x0039,
942 0x0045,
943 0x0038,
944 0x000e,
945 0x0011,
946 0x0016,
947 0x001d,
948 0x0033,
949 0x0057,
950 0x0050,
951 0x003e,
952 0x0012,
953 0x0016,
954 0x0025,
955 0x0038,
956 0x0044,
957 0x006d,
958 0x0067,
959 0x004d,
960 0x0018,
961 0x0023,
962 0x0037,
963 0x0040,
964 0x0051,
965 0x0068,
966 0x0071,
967 0x005c,
968 0x0031,
969 0x0040,
970 0x004e,
971 0x0057,
972 0x0067,
973 0x0079,
974 0x0078,
975 0x0065,
976 0x0048,
977 0x005c,
978 0x005f,
979 0x0062,
980 0x0070,
981 0x0064,
982 0x0067,
983 0x0063,
984 0x0011,
985 0x0012,
986 0x0018,
987 0x002f,
988 0x0063,
989 0x0063,
990 0x0063,
991 0x0063,
992 0x0012,
993 0x0015,
994 0x001a,
995 0x0042,
996 0x0063,
997 0x0063,
998 0x0063,
999 0x0063,
1000 0x0018,
1001 0x001a,
1002 0x0038,
1003 0x0063,
1004 0x0063,
1005 0x0063,
1006 0x0063,
1007 0x0063,
1008 0x002f,
1009 0x0042,
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 0x0063,
1024 0x0063,
1025 0x0063,
1026 0x0063,
1027 0x0063,
1028 0x0063,
1029 0x0063,
1030 0x0063,
1031 0x0063,
1032 0x0063,
1033 0x0063,
1034 0x0063,
1035 0x0063,
1036 0x0063,
1037 0x0063,
1038 0x0063,
1039 0x0063,
1040 0x0063,
1041 0x0063,
1042 0x0063,
1043 0x0063,
1044 0x0063,
1045 0x0063,
1046 0x0063,
1047 0x0063
1048 };
1049 #endif
1050 
1051 /* entry n is (-1 << n) + 1 */
1052 static const MS_S32 extend_offset[16] =
1053 {
1054     0, (( -1)<<1)+1, ((-1)<<2)+1, ((-1)<<3) + 1, ((-1)<<4)+1, ((-1)<<5)+1,
1055     ((-1)<<6)+1, ((-1)<<7)+1, ((-1)<<8)+1, ((-1)<<9)+1, ((-1) <<10)+1,
1056     ((-1)<<11)+1, ((-1)<<12)+1, ((-1)<<13)+1, ((-1)<<14)+1, ((-1)<<15)+1
1057 };
1058 
1059 /* entry n is 2**(n-1) */
1060 static const MS_S32 extend_test[16] =
1061 {
1062     0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
1063 };
1064 
1065 static const MS_S32 extend_mask[] =
1066 {
1067     0, (1<<0), (1<<1), (1<<2), (1<<3), (1<<4), (1<<5), (1<<6), (1<<7), (1<<8),
1068     (1<<9), (1<<10), (1<<11), (1<<12), (1<<13), (1<<14), (1<<15), (1<<16),
1069 };
1070 
1071 
1072 JPEG_STATIC MS_U32 _u32ReadBufferAddr;
1073 JPEG_STATIC MS_U32 _u32ReadBufferSize;
1074 JPEG_STATIC MS_U32 _u32WriteBufferAddr;
1075 JPEG_STATIC MS_U32 _u32WriteBufferSize;
1076 JPEG_STATIC MS_U32 _u32InternalBufferAddr;
1077 JPEG_STATIC MS_U32 _u32InternalBufferSize;
1078 
1079 JPEG_STATIC MS_U32 _u32RLEOffset;   // offset to record the current RLE access address
1080 
1081 JPEG_STATIC JPEG_HdrChk _HeadCheck;
1082 
1083 JPEG_STATIC JPEG_BLOCK_TYPE _s16dc_pred[3];
1084 
1085 // The width/height may be the thumbnail or original image size, it based on decoding mode
1086 JPEG_STATIC MS_U16 _u16Image_x_size;
1087 JPEG_STATIC MS_U16 _u16Image_y_size;
1088 
1089 // The original size of this JPEG file after alignment
1090 JPEG_STATIC MS_U16 _u16OriginalImage_x_size;
1091 JPEG_STATIC MS_U16 _u16OriginalImage_y_size;
1092 
1093 // The original size before alignment
1094 JPEG_STATIC MS_U16 _u16NonAlignmentImage_x_size;
1095 JPEG_STATIC MS_U16 _u16NonAlignmentImage_y_size;
1096 
1097 // The width/height/pitch of image for displaying.
1098 JPEG_STATIC MS_U16 _u16AlignedImageWidth;
1099 JPEG_STATIC MS_U16 _u16AlignedImagePitch;
1100 JPEG_STATIC MS_U16 _u16AlignedImageHeight;
1101 JPEG_STATIC MS_U16 _u16AlignedImagePitch_H;
1102 
1103 // Scale Down Factor
1104 JPEG_STATIC MS_U8 _u8ScaleDownFactor;
1105 
1106 ////JPEG_STATIC PJPEG_FILE_FileSystem_t _pStream;
1107 
1108 JPEG_STATIC MS_U8 _u8DecodeType = E_JPEG_TYPE_MAIN;
1109 JPEG_STATIC MS_BOOL _bProgressive_flag;
1110 
1111 #ifndef CMODEL
1112 JPEG_STATIC MS_U8 _u8DownScaleRatio;
1113 #endif
1114 
1115 JPEG_STATIC MS_BOOL _bFirstRLE;
1116 
1117 /******* Thumbnail related *******/
1118 JPEG_STATIC MS_BOOL _bThumbnailFound;
1119 JPEG_STATIC MS_BOOL _bThumbnailAccessMode;
1120 JPEG_STATIC MS_U32 _u32ThumbnailOffset;
1121 JPEG_STATIC MS_U16 _u16ThumbnailSize;
1122 JPEG_STATIC MS_BOOL _bTiffBigEndian;
1123 
1124 JPEG_STATIC MS_U32 _u32ThumbnailBufferOffset;   // keep track of thumb buffer access address
1125 JPEG_STATIC MS_U32 _u16ThumbnailBufferSize;   // keep track of thumb buffer size
1126 /*****************************/
1127 
1128 JPEG_STATIC JPEG_HuffInfo _Huff_info[JPEG_MAXHUFFTABLES];
1129 
1130 JPEG_STATIC JPEG_QuantTbl _QuantTables[JPEG_MAXQUANTTABLES]; /* pointer to quantization tables */
1131 
1132 JPEG_STATIC MS_U8 _u8Comps_in_frame;                             /* # of components in frame */
1133 JPEG_STATIC MS_U8 _u8Comp_h_samp[JPEG_MAXCOMPONENTS];     /* component's horizontal sampling factor */
1134 JPEG_STATIC MS_U8 _u8Comp_v_samp[JPEG_MAXCOMPONENTS];     /* component's vertical sampling factor */
1135 JPEG_STATIC MS_U8 _u8Comp_quant[JPEG_MAXCOMPONENTS];      /* component's quantization table selector */
1136 JPEG_STATIC MS_U8 _u8Comp_ident[JPEG_MAXCOMPONENTS];      /* component's ID */
1137 
1138 // The Luma and Chroma (YU) component ID, default is 1 & 2
1139 JPEG_STATIC MS_U8 _u8LumaCi = 1;
1140 JPEG_STATIC MS_U8 _u8ChromaCi = 2;
1141 JPEG_STATIC MS_U8 _u8Chroma2Ci = 3;
1142 
1143 JPEG_STATIC MS_U16 _u16Comp_h_blocks[JPEG_MAXCOMPONENTS];
1144 JPEG_STATIC MS_U16 _u16Comp_v_blocks[JPEG_MAXCOMPONENTS];
1145 
1146 JPEG_STATIC MS_U8 _u8Comps_in_scan;                  /* # of components in scan */
1147 JPEG_STATIC MS_U8 _u8Comp_list[JPEG_MAXCOMPSINSCAN];      /* components in this scan */
1148 JPEG_STATIC MS_U8 _u8Comp_dc_tab[JPEG_MAXCOMPONENTS];     /* component's DC Huffman coding table selector */
1149 JPEG_STATIC MS_U8 _u8Comp_ac_tab[JPEG_MAXCOMPONENTS];     /* component's AC Huffman coding table selector */
1150 
1151 JPEG_STATIC MS_U8 _u8Spectral_start;                 /* spectral selection start */
1152 JPEG_STATIC MS_U8 _u8Spectral_end;                   /* spectral selection end   */
1153 JPEG_STATIC MS_U8 _u8Successive_low;                 /* successive approximation low */
1154 JPEG_STATIC MS_U8 _u8Successive_high;                /* successive approximation high */
1155 
1156 JPEG_STATIC MS_U8 _u8Blocks_per_mcu;
1157 JPEG_STATIC MS_U32 _u32Max_blocks_per_row;
1158 JPEG_STATIC MS_U16 _u16Mcus_per_row;
1159 JPEG_STATIC MS_U16 _u16Mcus_per_col;
1160 
1161 JPEG_STATIC MS_U8 _u8Mcu_org[JPEG_MAXBLOCKSPERMCU];
1162 
1163 JPEG_STATIC MS_U8 gu8Max_mcu_x_size;          /* MCU's max. X size in pixels */
1164 JPEG_STATIC MS_U8 gu8Max_mcu_y_size;                 /* MCU's max. Y size in pixels */
1165 JPEG_STATIC MS_U16 gu16Max_mcus_per_row;
1166 
1167 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
1168 JPEG_STATIC MS_U16 _u16Total_lines_left;               /* total # lines left in image */
1169 JPEG_STATIC MS_U32 _u32Block_y_mcu[JPEG_MAXCOMPONENTS];
1170 JPEG_STATIC JPEG_HuffTbl _Huff_tbls[JPEG_MAXHUFFTABLES];
1171 JPEG_STATIC PJPEG_CoeffBuf _DC_Coeffs[JPEG_MAXCOMPONENTS];
1172 JPEG_STATIC PJPEG_CoeffBuf _AC_Coeffs[JPEG_MAXCOMPONENTS];
1173 JPEG_STATIC MS_U32 _u32Last_dc_val[JPEG_MAXCOMPONENTS];
1174 JPEG_STATIC MS_U32 _u32EOB_run;
1175 #ifdef CMODEL
1176 JPEG_STATIC MS_U16 gu16Mcu_lines_left;                 /* total # lines left in this MCU */
1177 #endif
1178 #endif
1179 
1180 JPEG_STATIC MS_U16 _u16Max_blocks_per_mcu;
1181 JPEG_STATIC MS_U16 _u16Max_mcus_per_col;
1182 
1183 JPEG_STATIC MS_U8 gu8Scan_type;                 /* Grey, Yh1v1, Yh1v2, Yh2v1, Yh2v2,
1184                                        CMYK111, CMYK4114 */
1185 JPEG_STATIC MS_U8 *_pu8In_buf_ofs;
1186 JPEG_STATIC MS_U32 _u32In_buf_left;
1187 JPEG_STATIC MS_U8 _u8Tem_flag;
1188 JPEG_STATIC MS_BOOL _bEOF_flag;
1189 
1190 JPEG_STATIC MS_U8 *_pu8In_buf;
1191 
1192 JPEG_STATIC MS_S16 _s16Bits_left;
1193 JPEG_STATIC MS_U32 _u32Bit_buf;
1194 
1195 JPEG_STATIC MS_U16 _u16Restart_interval;
1196 JPEG_STATIC MS_U16 _u16Restarts_left;
1197 JPEG_STATIC MS_U16 _u16Next_restart_num;
1198 
1199 JPEG_STATIC void *_pBlocks[JPEG_MAXBLOCKS];         /* list of all dynamically allocated blocks */
1200 
1201 JPEG_STATIC JPEG_ErrCode _Error_code;
1202 
1203 #ifdef CMODEL
1204 JPEG_STATIC MS_BOOL _bReady_flag;
1205 #endif
1206 
1207 //#ifdef USE_LIBC
1208 #ifdef JPD_LONGJUMP_SUPPORT
1209 JPEG_STATIC jmp_buf _jmp_state;
1210 #endif
1211 //#endif
1212 
1213 ////JPEG_STATIC MS_S32 _Total_Decoded_Size = 0;
1214 #if 0 // not implement for new MDDI - harold
1215 JPEG_STATIC MS_U32  NumPics;
1216 #endif
1217 JPEG_STATIC MS_U32 _u32Total_bytes_read;
1218 
1219 //JPEG_STATIC MS_U8 *_pu32ExifHeaderAddr;
1220 
1221 JPEG_STATIC JPEG_FillHdrFunc _pFillHdrFunc = NULL;
1222 
1223 #ifdef CMODEL
1224 extern const MS_U8 _u8ZAG[64];
1225 extern const MS_U8 _u8Jpeg_zigzag_order[64];
1226 #elif SW_JPD_RGB_CMYK
1227 static const MS_U8 _u8ZAG[64+1] =
1228 {
1229     0, 1, 8, 16, 9, 2, 3, 10,
1230     17, 24, 32, 25, 18, 11, 4, 5,
1231     12, 19, 26, 33, 40, 48, 41, 34,
1232     27, 20, 13, 6, 7, 14, 21, 28,
1233     35, 42, 49, 56, 57, 50, 43, 36,
1234     29, 22, 15, 23, 30, 37, 44, 51,
1235     58, 59, 52, 45, 38, 31, 39, 46,
1236     53, 60, 61, 54, 47, 55, 62, 63
1237     ,0
1238 };
1239 #endif
1240 
1241 JPEG_STATIC MSIF_Version _api_jpeg_version =
1242 {
1243     .DDI = { JPEG_API_VERSION },
1244 };
1245 
1246 JPEG_STATIC MS_U8 _u8JPEG_ApiDbgLevel = E_JPEG_DEBUG_NONE;
1247 JPEG_STATIC MS_BOOL bMHEG5 = FALSE;
1248 
1249 JPEG_STATIC MS_U16 _JPD_PreVIdx = 0; //For H/W bug, some cases can not exit after decode done
1250 JPEG_STATIC MS_BOOL _JPD_IsDecoding = FALSE; //For H/W bug, some cases can not exit after decode done
1251 JPEG_STATIC MS_U32 _JPD_ReCheckTime = 0; //For H/W bug, some cases can not exit after decode done
1252 
1253 JPEG_STATIC MS_BOOL _Progressive_ROI_flag = FALSE; //CL82399
1254 JPEG_STATIC MS_U16 ROI_width; //CL82399
1255 
1256 JPEG_STATIC JPEG_BuffLoadType u8PreLHFlag = E_JPEG_BUFFER_NONE;
1257 
1258 JPEG_STATIC MS_U32 u32MRCheckCount = 0;
1259 
1260 JPEG_STATIC MS_BOOL _bIsInit = FALSE;
1261 
1262 ///JPEG_STATIC MS_U32 jpeg_input = 0;
1263 ///#define JPEG_GO     do{scanf("%d", &jpeg_input);}while(0)
1264 
1265 #if SW_JPD_RGB_CMYK
1266 JPEG_BLOCK_TYPE *_ps16Block_seg[JPEG_MAXBLOCKSPERROW];
1267 MS_U8 _u8Block_max_zag_set[JPEG_MAXBLOCKSPERROW];
1268 MS_U8 *gpu8Sample_buf;
1269 MS_S32 gs32Crr[256];
1270 MS_S32 gs32Cbb[256];
1271 MS_S32 gs32Crg[256];
1272 MS_S32 gs32Cbg[256];
1273 
1274 MS_U8 *pgu8Scan_line_0;
1275 MS_U8 *pgu8scan_line_1;
1276 
1277 //JPEG_STATIC MS_U16 _u16Real_dest_bytes_per_scan_line;
1278 JPEG_STATIC MS_U16 _u16Dest_bytes_per_scan_line;        /* rounded up */
1279 JPEG_STATIC MS_U8 _u8Dest_bytes_per_pixel;            /* currently, 4 (RGB) or 1 (Y) */
1280 
1281 #if ( ! SUPPORT_PROGRESSIVE_MODE) && ( ! defined(CMODEL))
1282 JPEG_STATIC MS_U16 _u16Total_lines_left;               /* total # lines left in image */
1283 JPEG_STATIC MS_U32 _u32Block_y_mcu[JPEG_MAXCOMPONENTS];
1284 //JPEG_STATIC MS_U16 gu16Mcu_lines_left;                 /* total # lines left in this MCU */
1285 #endif
1286 #endif
1287 
1288 /*===========================================================================*/
1289 JPEG_STATIC MS_BOOL __bIsMjpeg = FALSE;
1290 JPEG_STATIC MS_U32 __u32RealMjpegBase = 0x00000000;
1291 JPEG_STATIC MS_U32 __u32TrickyMjpegBase = 0x00000000;
1292 /*===========================================================================*/
1293 
1294 //The info of JPEG EXIF
1295 JPEG_STATIC JPEG_EXIF_DateTime _stEXIF_DateTime;
1296 JPEG_STATIC JPEG_EXIF_Orientation _eEXIF_Orientation;
1297 
1298 #if SUPPORT_EXIF_EXTRA_INFO
1299 JPEG_STATIC MS_U8 _u8EXIF_Manufacturer[JPEG_MANUFACTURER_SIZE];
1300 JPEG_STATIC MS_U8 _u8EXIF_Model[JPEG_MODEL_SIZE];
1301 JPEG_STATIC MS_U16 _u16EXIF_Exposureprogram;
1302 JPEG_STATIC MS_U16 _u16EXIF_Flash;
1303 JPEG_STATIC MS_U32 _u32EXIF_ISOSpeedRatings;
1304 JPEG_STATIC JPEG_RATIONAL _stEXIF_ExposureTime;
1305 JPEG_STATIC JPEG_RATIONAL _stEXIF_FNumber;
1306 JPEG_STATIC JPEG_RATIONAL _stEXIF_ShutterSpeedValue;
1307 JPEG_STATIC JPEG_RATIONAL _stEXIF_ApertureValue;
1308 JPEG_STATIC JPEG_RATIONAL _stEXIF_ExposureBiasValue;
1309 JPEG_STATIC JPEG_RATIONAL _stEXIF_FocalLength;
1310 JPEG_STATIC MS_U32 _u32EXIF_ImageWidth;
1311 JPEG_STATIC MS_U32 _u32EXIF_ImageHeight;
1312 #endif
1313 
1314 //record data offset
1315 JPEG_STATIC MS_U32 u32DataOffset = 0;
1316 JPEG_STATIC MS_U32 u32SOFOffset = 0; //for SEC request, they need to know the offset of SOF marker
1317 
1318 JPEG_STATIC MS_BOOL bIs3HuffTbl = FALSE;
1319 
1320 #if SW_JPD_RGB_CMYK
1321 JPEG_STATIC MS_BOOL bEnableCMYK = TRUE;
1322 JPEG_STATIC MS_BOOL bEnableRGB = TRUE;
1323 #endif
1324 
1325 
1326 #if SUPPORT_MPO_FORMAT
1327 //MPO buffer
1328 static MS_U8 *_pu8In_buf_MPO_ofs;
1329 static MS_U32 _u32In_buf_MPO_left = 0xFFFFFFFFUL;
1330 
1331 static MS_BOOL mpo_load_data = TRUE;  //check whether load data when start decode or fill buffer.
1332 static MS_BOOL bIsMPOFormat = FALSE;
1333 static MS_U32 u32MPFOffset = 0;
1334 #endif
1335 
1336 #if PRINT_JPD_DECODE_TIME
1337 static MS_U32 u32MeasureDecodeTime;
1338 static MS_U32 u32MeasureDecodeTimeSW;
1339 static MS_U32 u32MeasureDecodeTimeHW;
1340 #endif
1341 
1342 static unsigned long  u32CRC;
1343 
verJPD_CRC32_Init(void)1344 static void verJPD_CRC32_Init(void)
1345 {
1346     u32CRC = 0xFFFFFFFF;
1347 }
1348 
1349 
verJPD_CRC32_GetResult(void)1350 static MS_U32 verJPD_CRC32_GetResult(void)
1351 {
1352     return (~u32CRC);
1353 }
1354 
1355 
verJPD_Init_CRC_Table(MS_U32 * u32CRCtemptable)1356 static void verJPD_Init_CRC_Table(MS_U32 *u32CRCtemptable)
1357 {
1358 
1359     int i,j,tmp, result;
1360 
1361     for(i=0;i<=0xFF;i++)
1362     {
1363         tmp=i;
1364         result=0;
1365 
1366         for(j=1;j<9;j++)
1367         {
1368              if(tmp & 1)
1369              {
1370                         result |= (1 << (8 - j));
1371              }
1372              tmp >>= 1;
1373         }
1374 
1375         u32CRCtemptable[i]=result<<24;
1376         for(j=0; j<8; j++)
1377         {
1378             u32CRCtemptable[i] = (u32CRCtemptable[i] << 1) ^ ((u32CRCtemptable[i] & (1 << 31)) ? 0x04C11DB7 : 0);
1379         }
1380 
1381         tmp=u32CRCtemptable[i];
1382         result=0;
1383 
1384         for(j=1;j<33;j++)
1385         {
1386              if(tmp & 1)
1387              {
1388                 result |= (1 << (32 - j));
1389              }
1390              tmp >>= 1;
1391         }
1392         u32CRCtemptable[i]=result;
1393         }
1394 }
1395 
verJPD_CRC32_Update(const MS_U8 * pu8Data,MS_U32 u32Size)1396 static void verJPD_CRC32_Update(const MS_U8 *pu8Data, MS_U32 u32Size)
1397 {
1398     MS_U32 u32CRCTable[256];
1399     MS_U32  u32LoopCounter;
1400 
1401     if (pu8Data == NULL || u32Size == 0) return;
1402 
1403     verJPD_Init_CRC_Table(u32CRCTable);
1404 
1405     #if 0
1406         int i;
1407         for(i = 0;i<256;i++)
1408         {
1409             printf("0x%08x,\n", u32CRCTable[i]);
1410         }
1411     #endif
1412 
1413     for (u32LoopCounter=0; u32LoopCounter<u32Size; u32LoopCounter++)
1414     {
1415         u32CRC = (u32CRC >> 8) ^ u32CRCTable[ pu8Data[u32LoopCounter] ^ (unsigned char)(u32CRC & 0xFF) ];
1416     }
1417 }
1418 
1419 //-------------------------------------------------------------------------------------------------
1420 // Local Function Prototypes
1421 //-------------------------------------------------------------------------------------------------
1422 #if 0 // not implement for new MDDI - harold
1423 JPEG_STATIC void MSAPI_MJPEG_Get_Pics(void);
1424 #endif // not implement for new MDDI - harold
1425 
1426 JPEG_STATIC void JPEG_terminate(JPEG_ErrCode status);
1427 
1428 JPEG_STATIC MS_BOOL JPEG_decode_init(void);
1429 
1430 #ifndef CMODEL
1431 JPEG_STATIC MS_BOOL JPEG_StartDecode(void);
1432 #endif
1433 
1434 JPEG_STATIC void JPEG_GetAlignedResolution(MS_U16 *width, MS_U16 *height);
1435 
1436 #if SW_JPD_RGB_CMYK
1437 JPEG_STATIC MS_BOOL msAPI_JPEG_transform_row( void );
1438 JPEG_STATIC JPEG_Result msAPI_JPEG_Baseline_Decode(void);
1439 #endif //SW_JPD_RGB_CMYK
1440 
1441 JPEG_STATIC MS_U32 JPEG_GetECS(void);
1442 
1443 //-------------------------------------------------------------------------------------------------
1444 //  Debug Functions
1445 //-------------------------------------------------------------------------------------------------
1446 
1447 //-------------------------------------------------------------------------------------------------
1448 //  Local Functions
1449 //-------------------------------------------------------------------------------------------------
1450 
JPEG_OVER_BUFFER(MS_U32 addr,MS_U32 buf_addr,MS_U32 buf_size)1451 JPEG_STATIC MS_BOOL JPEG_OVER_BUFFER(MS_U32 addr, MS_U32 buf_addr, MS_U32 buf_size)
1452 {
1453     if((addr < buf_addr)
1454     || (addr >= (buf_addr+ buf_size)))
1455     {
1456         JPEG_DEBUG_API_ERR("%s [%d] invalid address 0x%lx\n", __FUNCTION__, __LINE__, addr);
1457         return TRUE;
1458     }
1459     return FALSE;
1460 }
1461 
1462 //------------------------------------------------------------------------------
1463 // Refill the input buffer.
1464 // This method will sit in a loop until (A) the buffer is full or (B)
1465 // the stream's read() method reports an end of file condition.
JPEG_fill_read_buffer(void)1466 JPEG_STATIC MS_BOOL JPEG_fill_read_buffer(void)
1467 {
1468     JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer:\n");
1469     _u32In_buf_left = 0;
1470     _pu8In_buf_ofs = _pu8In_buf;
1471 
1472     if(_bEOF_flag
1473     && (FALSE == _bThumbnailAccessMode))
1474     {
1475         JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer : EOF!!!\n");
1476         return TRUE;
1477     }
1478 
1479     if(_bThumbnailAccessMode)
1480     {
1481         JPEG_DEBUG_API_MSG("Fill Thumbnail, buffer left = %ld, buffer offset = %ld\n", _u16ThumbnailBufferSize, _u32ThumbnailBufferOffset);
1482         if(_u16ThumbnailBufferSize>=MRC_BUFFER_SIZE)
1483         {
1484             JPEG_memcpy((void *)(_pu8In_buf + _u32In_buf_left), (void *)(INTERNAL_BUFFER_ADDR + _u32ThumbnailBufferOffset), MRC_BUFFER_SIZE);
1485             _u32In_buf_left = MRC_BUFFER_SIZE;
1486             _u32ThumbnailBufferOffset += MRC_BUFFER_SIZE;
1487             _u16ThumbnailBufferSize -= MRC_BUFFER_SIZE;
1488         }
1489         else
1490         {
1491             JPEG_memcpy((void *)(_pu8In_buf + _u32In_buf_left), (void *)(INTERNAL_BUFFER_ADDR + _u32ThumbnailBufferOffset), _u16ThumbnailBufferSize);
1492             _bEOF_flag = TRUE;
1493             _u32In_buf_left = _u16ThumbnailBufferSize;
1494             _u32ThumbnailBufferOffset += _u16ThumbnailBufferSize;
1495             _u16ThumbnailBufferSize = 0;
1496         }
1497     }
1498     else
1499     {
1500         if(_pFillHdrFunc)
1501         {
1502             MS_S32 byte_read = _pFillHdrFunc((MS_PHYADDR)MS_VA2PA((MS_U32)_pu8In_buf), MRC_BUFFER_SIZE);
1503             if(byte_read < 0)
1504             {
1505                 JPEG_terminate(E_JPEG_STREAM_READ);
1506                 return FALSE;
1507             }
1508             _u32In_buf_left = byte_read;
1509         }
1510         else
1511         {
1512             JPEG_DEBUG_API_MSG("_pFillHdrFunc is not registered!!\n");
1513             if (bMHEG5)
1514             {
1515                 _u32In_buf_left = MRC_BUFFER_SIZE;
1516             }
1517             else
1518             {
1519                 JPEG_terminate(E_JPEG_NOT_ENOUGH_HEADER_INFO);
1520                 return FALSE;
1521             }
1522         }
1523     }
1524 #if 0
1525     else
1526     {
1527         do
1528         {
1529             MS_S32 bytes_read = _pStream->read( _pu8In_buf + _u32In_buf_left, MRC_BUFFER_SIZE - _u32In_buf_left, &_bEOF_flag, _pStream);//, 0 );
1530             if ( bytes_read == -1 )
1531             {
1532                 JPEG_terminate( E_JPEG_STREAM_READ );
1533             }
1534             _u32In_buf_left += bytes_read;
1535 ////            _Total_Decoded_Size += bytes_read;
1536         } while ( ( _u32In_buf_left < MRC_BUFFER_SIZE ) && ( !_bEOF_flag ) );
1537     }
1538 
1539     _u32Total_bytes_read += _u32In_buf_left;
1540 #endif
1541 
1542     u32DataOffset += MRC_BUFFER_SIZE;
1543     return TRUE;
1544 }
JPEG_force_fill_read_buffer(MS_U32 u32Size)1545 JPEG_STATIC MS_BOOL JPEG_force_fill_read_buffer(MS_U32 u32Size)
1546 {
1547     JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer:\n");
1548     _u32In_buf_left = u32Size;
1549     _pu8In_buf_ofs = _pu8In_buf;
1550 
1551     if(_bEOF_flag)
1552     {
1553         JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer : EOF!!!\n");
1554         return TRUE;
1555     }
1556 
1557     if(_pFillHdrFunc)
1558     {
1559         MS_S32 byte_read = 0;
1560         if((MRC_BUFFER_SIZE-u32Size) > 0)
1561         {
1562             JPEG_DEBUG_API_MSG("[VA] _pu8In_buf=0x%lx\n", (MS_U32)_pu8In_buf);
1563             JPEG_DEBUG_API_MSG("[PA] _pu8In_buf=0x%lx\n", MS_VA2PA((MS_U32)_pu8In_buf));
1564             JPEG_DEBUG_API_MSG("[PA] callback addr=0x%lx\n", (MS_VA2PA((MS_U32)_pu8In_buf)+u32Size));
1565             byte_read = _pFillHdrFunc((MS_PHYADDR)(MS_VA2PA((MS_U32)_pu8In_buf)+u32Size), MRC_BUFFER_SIZE-u32Size);
1566             JPEG_DEBUG_API_MSG("@@@fill byte_read=0x%lx\n", byte_read);
1567         }
1568 
1569         if(byte_read < 0)
1570         {
1571             JPEG_terminate(E_JPEG_STREAM_READ);
1572             return FALSE;
1573         }
1574         _u32In_buf_left += byte_read;
1575     }
1576     else
1577     {
1578         JPEG_DEBUG_API_MSG("_pFillHdrFunc is not registered!!\n");
1579         return FALSE;
1580     }
1581 
1582     return TRUE;
1583 }
1584 //------------------------------------------------------------------------------
1585 // Logical rotate left operation.
JPEG_rol(MS_U32 i,MS_U8 j)1586 JPEG_STATIC MS_U32 JPEG_rol(MS_U32 i, MS_U8 j)
1587 {
1588     return ( ( i << j ) | ( i >> ( 32 - j ) ) );
1589 }
1590 //------------------------------------------------------------------------------
1591 // Retrieve one character from the input stream.
JPEG_get_char(void)1592 JPEG_STATIC MS_U8 JPEG_get_char(void)
1593 {
1594     MS_U8 c;
1595 
1596     // Any bytes remaining in buffer?
1597     if ( !_u32In_buf_left )
1598     {
1599         // Try to get more bytes.
1600         if(!JPEG_fill_read_buffer())
1601         {
1602             JPEG_DEBUG_API_MSG("JPEG_get_char : JPEG_fill_read_buffer failed!!!\n");
1603             _u32In_buf_left = 0;
1604         }
1605         // Still nothing to get?
1606         if ( !_u32In_buf_left )
1607         {
1608             // Padd the end of the stream with 0xFF 0xD9 (EOI marker)
1609             // FIXME: Is there a better padding pattern to use?
1610             MS_U8 t = _u8Tem_flag;
1611             _u8Tem_flag ^= 1;
1612             if ( t )
1613             {
1614                 return ( 0xD9 );
1615             }
1616             else
1617             {
1618                 return ( 0xFF );
1619             }
1620         }
1621     }
1622 
1623     c = *_pu8In_buf_ofs++;
1624     _u32In_buf_left--;
1625 
1626     return ( c );
1627 }
1628 //------------------------------------------------------------------------------
1629 // Same as previus method, except can indicate if the character is
1630 // a "padd" character or not.
JPEG_get_charP(MS_BOOL * Ppadding_flag)1631 JPEG_STATIC MS_U8 JPEG_get_charP(MS_BOOL *Ppadding_flag)
1632 {
1633     MS_U8 c;
1634 
1635     if ( !_u32In_buf_left )
1636     {
1637         if(!JPEG_fill_read_buffer())
1638         {
1639             JPEG_DEBUG_API_MSG("JPEG_get_charP : JPEG_fill_read_buffer failed!!!\n");
1640             _u32In_buf_left = 0;
1641         }
1642 
1643         if ( !_u32In_buf_left )
1644         {
1645             *Ppadding_flag = TRUE;
1646 #if 1
1647             _u8Tem_flag ^= 1;
1648             // This function is called during when progressive huffman decoding
1649             // Should not padding 0xFFD9 at this state
1650             return 0;
1651 #else
1652             MS_U8 t;
1653             t = _u8Tem_flag;
1654             _u8Tem_flag ^= 1;
1655             if ( t )
1656             {
1657                 return ( 0xD9 );
1658             }
1659             else
1660             {
1661                 return ( 0xFF );
1662             }
1663 #endif
1664         }
1665     }
1666 
1667     *Ppadding_flag = FALSE;
1668 
1669     c = *_pu8In_buf_ofs++;
1670     _u32In_buf_left--;
1671 
1672     return ( c );
1673 }
1674 //------------------------------------------------------------------------------
1675 // Inserts a previously retrieved character back into the input buffer.
JPEG_stuff_char(MS_U8 q)1676 JPEG_STATIC void JPEG_stuff_char(MS_U8 q)
1677 {
1678     *( --_pu8In_buf_ofs ) = q;
1679     _u32In_buf_left++;
1680 }
1681 //------------------------------------------------------------------------------
1682 // Retrieves one character from the input stream, but does
1683 // not read past markers. Will continue to return 0xFF when a
1684 // marker is encountered.
1685 // FIXME: Bad name?
JPEG_get_octet(void)1686 JPEG_STATIC MS_U8 JPEG_get_octet(void)
1687 {
1688     MS_BOOL padding_flag;
1689     MS_U8 c = JPEG_get_charP( &padding_flag );
1690 
1691     if ( c == 0xFF )
1692     {
1693         if ( padding_flag )
1694         {
1695             return ( 0xFF );
1696         }
1697 
1698         c = JPEG_get_charP( &padding_flag );
1699         if ( padding_flag )
1700         {
1701             JPEG_stuff_char( 0xFF );
1702             return ( 0xFF );
1703         }
1704 
1705         if ( c == 0x00 )
1706         {
1707             return ( 0xFF );
1708         }
1709         else
1710         {
1711             JPEG_stuff_char( c );
1712             JPEG_stuff_char( 0xFF );
1713             return ( 0xFF );
1714         }
1715     }
1716 
1717     return ( c );
1718 }
1719 //------------------------------------------------------------------------------
1720 // Retrieves a variable number of bits from the input stream.
1721 // Markers will not be read into the input bit buffer. Instead,
1722 // an infinite number of all 1's will be returned when a marker
1723 // is encountered.
1724 // FIXME: Is it better to return all 0's instead, like the older implementation?
JPEG_get_bits_2(MS_U8 numbits)1725 JPEG_STATIC MS_U32 JPEG_get_bits_2(MS_U8 numbits)
1726 {
1727     MS_U32 i, c1, c2;
1728 
1729     i = ( _u32Bit_buf >> ( 16 - numbits ) ) & ( ( 1 << numbits ) - 1 );
1730 
1731     _s16Bits_left -= numbits;
1732     if ( _s16Bits_left <= 0 )
1733     {
1734         _u32Bit_buf = JPEG_rol( _u32Bit_buf, numbits += _s16Bits_left );
1735 
1736         c1 = JPEG_get_octet();
1737         c2 = JPEG_get_octet();
1738 
1739         _u32Bit_buf = ( _u32Bit_buf & 0xFFFF ) | ( ( ( MS_U32 )c1 ) << 24 ) | ( ( ( MS_U32 )c2 ) << 16 );
1740 
1741         _u32Bit_buf = JPEG_rol( _u32Bit_buf, -_s16Bits_left );
1742 
1743         _s16Bits_left += 16;
1744     }
1745     else
1746     {
1747         _u32Bit_buf = JPEG_rol( _u32Bit_buf, numbits );
1748     }
1749 
1750     return i;
1751 }
1752 //------------------------------------------------------------------------------
1753 // Decodes a Huffman encoded symbol.
JPEG_huff_decode(JPEG_HuffTbl * Ph)1754 JPEG_STATIC MS_S32 JPEG_huff_decode(JPEG_HuffTbl *Ph)
1755 {
1756     MS_S32 symbol;
1757 
1758     // Check first 8-bits: do we have a complete symbol?
1759     symbol = Ph->s16Look_up[( _u32Bit_buf >> 8 ) & 0xFF];
1760     if ( symbol < 0 )
1761     {
1762         // Decode more bits, use a tree traversal to find symbol.
1763         JPEG_get_bits_2( 8 );
1764 
1765         do
1766         {
1767             symbol = Ph->s16Tree[~symbol + ( 1 - JPEG_get_bits_2( 1 ) )];
1768         }
1769         while ( symbol < 0 );
1770     }
1771     else
1772     {
1773         JPEG_get_bits_2( Ph->u8Code_size[symbol] );
1774     }
1775 
1776     return symbol;
1777 }
1778 //------------------------------------------------------------------------------
1779 // Unconditionally frees all allocated blocks.
JPEG_free_all_blocks(void)1780 JPEG_STATIC void JPEG_free_all_blocks(void)
1781 {
1782     MS_U8 i;
1783 #if 0
1784     if ( _pStream )
1785     {
1786         _pStream = NULL;
1787     }
1788 #endif
1789     for ( i = 0; i < JPEG_MAXBLOCKS; i++ )
1790     {
1791         if(_pBlocks[i]!=NULL)
1792             JPEG_MEMORY_free( _pBlocks[i] );
1793 
1794         _pBlocks[i] = NULL;
1795     }
1796 }
1797 //------------------------------------------------------------------------------
1798 // This method handles all errors.
1799 // It could easily be changed to use C++ exceptions.
JPEG_terminate(JPEG_ErrCode status)1800 JPEG_STATIC void JPEG_terminate(JPEG_ErrCode status)
1801 {
1802     _Error_code = status;
1803 
1804     JPEG_free_all_blocks();
1805 
1806     JPEG_DEBUG_API_ERR("%s:ERROR= %d\n", __FUNCTION__, _Error_code);
1807 #ifdef JPD_LONGJUMP_SUPPORT
1808     longjmp( _jmp_state, status );
1809 #endif
1810 }
1811 //------------------------------------------------------------------------------
1812 // Allocate a block of memory-- store block's address in list for
1813 // later deallocation by JPEG_free_all_blocks().
JPEG_alloc(MS_U32 n)1814 JPEG_STATIC void * JPEG_alloc(MS_U32 n)
1815 {
1816     MS_U8 i;
1817     void *q;
1818 
1819     // Find a free slot. The number of allocated slots will
1820     // always be very low, so a linear search is good enough.
1821     for ( i = 0; i < JPEG_MAXBLOCKS; i++ )
1822     {
1823         if ( _pBlocks[i] == NULL )
1824         {
1825             break;
1826         }
1827     }
1828 
1829     if ( i >= JPEG_MAXBLOCKS )
1830     {
1831         JPEG_terminate( E_JPEG_TOO_MANY_BLOCKS );
1832         return NULL;
1833     }
1834 
1835     //FixMe: eCos does not support aligned allocate ???
1836     q = JPEG_MEMORY_malloc( n + 8 );
1837     //q = MsOS_AllocateAlignedMemory(n+8, 8, gs32CachedPoolID);
1838     //q = MsOS_AllocateMemory(n+8, gs32CachedPoolID);
1839 
1840     if ( q == NULL )
1841     {
1842         JPEG_terminate( E_JPEG_NOTENOUGHMEM );
1843         return NULL;
1844     }
1845 
1846     JPEG_memset((void *)q, 0, n + 8 );
1847 
1848     _pBlocks[i] = q;
1849 
1850     JPEG_DEBUG_API_MSG("JPEG %ld bytes allocated\n", n);
1851 
1852     return ( ( void* )q );
1853 }
1854 //------------------------------------------------------------------------------
JPEG_Tiff_EndianChangeL(MS_U32 u32Val)1855 JPEG_STATIC MS_U32 JPEG_Tiff_EndianChangeL(MS_U32 u32Val)
1856 {
1857     if (_bTiffBigEndian)
1858         return EndianChangeL(u32Val);
1859     else
1860         return u32Val;
1861 }
1862 //------------------------------------------------------------------------------
JPEG_Tiff_EndianChangeS(MS_U16 u16Val)1863 JPEG_STATIC MS_U16 JPEG_Tiff_EndianChangeS(MS_U16 u16Val)
1864 {
1865     if (_bTiffBigEndian)
1866         return EndianChangeS(u16Val);
1867     else
1868         return u16Val;
1869 }
1870 //------------------------------------------------------------------------------
JPEG_GetU16(MS_U8 * data)1871 JPEG_STATIC MS_U16 JPEG_GetU16(MS_U8 *data)
1872 {
1873     MS_S8 i;
1874     MS_U16 val = 0;
1875 
1876     for(i = 1; i>=0; i--)
1877         val = (val << 8) + (MS_U8) *(data + i);
1878 
1879 
1880     return val;
1881 }
1882 //------------------------------------------------------------------------------
JPEG_GetU32(MS_U8 * data)1883 JPEG_STATIC MS_U32 JPEG_GetU32(MS_U8 *data)
1884 {
1885     MS_S8 i;
1886     MS_U32 val = 0;
1887 
1888     for(i = 3; i>=0; i--)
1889         val = (val << 8) + (MS_U8) *(data + i);
1890 
1891     return val;
1892 }
1893 //------------------------------------------------------------------------------
1894 //***************************************************
1895 //Parse EXIF header
1896 //***************************************************
JPEG_DecodeExifInfo(MS_U8 * data,MS_U32 data_length)1897 JPEG_STATIC MS_BOOL JPEG_DecodeExifInfo(MS_U8 *data, MS_U32 data_length)
1898 {
1899     MS_U8 *pJpegBuff = data;
1900 //    MS_U32 u32tmp, u32Len;
1901     MS_U32 u32tmp;
1902     MS_U16 u16Marker, u16Len, u16NumOfEntry, i;
1903     MS_U8  *pTiffHdrPtr, *pNextIfd;
1904     MS_U32 u32DataValue;
1905     MS_U8 *pExifHdr = NULL, *pExifHdrOffet = NULL;
1906     MS_BOOL bIFD_pt_found = FALSE;
1907 
1908     u32tmp = JPEG_GetU32(pJpegBuff);
1909     if (JPEG_TAG_EXIF != u32tmp)
1910     {
1911         return FALSE;
1912     }
1913 
1914     // Exif starts here
1915     pJpegBuff += 6;
1916     pExifHdr = pJpegBuff;
1917     u16Marker = JPEG_GetU16(pJpegBuff);
1918     if (JPEG_TIFF_BIG_ENDIAN == u16Marker)
1919         _bTiffBigEndian = TRUE;
1920     else if (JPEG_TIFF_LITTLE_ENDIAN == u16Marker)
1921         _bTiffBigEndian = FALSE;
1922     else
1923         return FALSE;
1924 
1925     _u32ThumbnailOffset += 6;
1926     pTiffHdrPtr = pJpegBuff;
1927 
1928     pJpegBuff += 2;
1929     u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
1930     if (u16Marker != 0x002A)
1931         return FALSE;
1932 
1933     pJpegBuff += 2;
1934     u16Len = (MS_U16)JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
1935     pJpegBuff += (u16Len - 4); //ENDIAN(2byte) + 0x002A(2byte)
1936 
1937     // 0th IFD start here
1938     u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
1939     pJpegBuff += 2;
1940 
1941     // Boundary check, prevent from buffer over-run
1942     if((((MS_U32) pJpegBuff) - INTERNAL_BUFFER_ADDR + u16NumOfEntry*12)>=data_length)
1943     {
1944         return FALSE;
1945     }
1946 
1947 //    pJpegBuff += 12*u16NumOfEntry;
1948 
1949     JPEG_DEBUG_API_MSG("IFD0 : u16NumOfEntry = %d\n", u16NumOfEntry);
1950 
1951     //Retrieve orientation
1952     for(i = 0; i < u16NumOfEntry; i++)
1953     {
1954         u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
1955         pJpegBuff += 8;
1956 
1957         if((JPEG_EXIF_TAG_ORIENTATION == u16Marker)
1958         && (E_JPEG_EXIF_ORIENT_NOT_FOUND == _eEXIF_Orientation))
1959         {
1960             //The datatype of orientation is short
1961             u32DataValue = (MS_U32)JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
1962             if((E_JPEG_EXIF_ORIENT_ROTATE_0 > u32DataValue)
1963             || (E_JPEG_EXIF_ORIENT_ROTATE_270 < u32DataValue))
1964             {
1965                 _eEXIF_Orientation = E_JPEG_EXIF_ORIENT_RESERVED;
1966             }
1967             else
1968             {
1969                 _eEXIF_Orientation = (JPEG_EXIF_Orientation)u32DataValue;
1970             }
1971             JPEG_DEBUG_API_MSG("JPEG EXIF Orientation = %d\n", _eEXIF_Orientation);
1972         }
1973         else if(JPEG_EXIF_TAG_IFD_POINTER == u16Marker)
1974         {
1975             bIFD_pt_found = TRUE;
1976             //datavalue is an offset from exif marker for datetime
1977             u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
1978             pExifHdrOffet = pExifHdr + u32DataValue;
1979             JPEG_OVER_BUFFER_RET(pExifHdrOffet, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
1980             JPEG_DEBUG_API_MSG("Exif IFD pointer = 0x%lx\n", (MS_U32)pExifHdrOffet);
1981         }
1982 #if SUPPORT_EXIF_EXTRA_INFO
1983         else if(JPEG_EXIF_TAG_MANUFACTURER == u16Marker)
1984         {
1985             MS_U8 *pIFDPtrOffset;
1986             pIFDPtrOffset = pJpegBuff;
1987             u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pIFDPtrOffset));
1988             JPEG_DEBUG_API_MSG("MANUFACTURER Offset = 0x%lx\n", u32DataValue);
1989             pIFDPtrOffset = pExifHdr + u32DataValue;
1990             if(JPEG_OVER_BUFFER((MS_U32)pIFDPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE))
1991             {
1992                 pJpegBuff += 4;
1993                 continue;
1994             }
1995             //JPEG_DEBUG_API_MSG("MANUFACTURER addr = 0x%lx\n", (MS_U32)pIFDPtrOffset);
1996             MS_U8 u8i=0;
1997             while(*(pIFDPtrOffset+u8i) && u8i<JPEG_MANUFACTURER_SIZE)
1998             {
1999                 _u8EXIF_Manufacturer[u8i] = *(pIFDPtrOffset+u8i);
2000                 u8i++;
2001             }
2002         }
2003         else if(JPEG_EXIF_TAG_MODEL== u16Marker)
2004         {
2005             MS_U8 *pIFDPtrOffset;
2006             pIFDPtrOffset = pJpegBuff;
2007             u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pIFDPtrOffset));
2008             JPEG_DEBUG_API_MSG("MODEL Offset = 0x%lx\n", u32DataValue);
2009             pIFDPtrOffset = pExifHdr + u32DataValue;
2010             if(JPEG_OVER_BUFFER((MS_U32)pIFDPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE))
2011             {
2012                 pJpegBuff += 4;
2013                 continue;
2014             }
2015             //JPEG_DEBUG_API_MSG("MODEL addr = 0x%lx\n", (MS_U32)pIFDPtrOffset);
2016             MS_U8 u8i=0;
2017             while(*(pIFDPtrOffset+u8i) && u8i<JPEG_MODEL_SIZE)
2018             {
2019                 _u8EXIF_Model[u8i] = *(pIFDPtrOffset+u8i);
2020                 u8i++;
2021             }
2022         }
2023 #endif
2024 #if 0
2025         else if((JPEG_EXIF_TAG_DATETIME_MOD == u16Marker)
2026         && (FALSE == _stEXIF_DateTime.bHasDataTime))
2027         {
2028             _stEXIF_DateTime.bHasDataTime = TRUE;
2029             //datavalue is an offset from exif marker for datetime
2030             u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2031             pExifOffset = pExifHdr + u32DataValue;
2032             printf("pExifOffset1 = 0x%lx\n", (MS_U32)pExifOffset);
2033             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Year);
2034             pExifOffset++;
2035             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Month);
2036             pExifOffset++;
2037             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Day);
2038             pExifOffset++;
2039             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Hour);
2040             pExifOffset++;
2041             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Minute);
2042             pExifOffset++;
2043             JPEG_AtoU32(pExifOffset, _stEXIF_DateTime.u32Second);
2044             JPEG_DEBUG_API_MSG("JPEG EXIF Orientation = %d\n", _eEXIF_Orientation);
2045             JPEG_DEBUG_API_MSG("=======JPEG EXIF DateTime======= Exist = %d, Original = %d \n", _stEXIF_DateTime.bHasDataTime, bOriDateTimeFound);
2046             JPEG_DEBUG_API_MSG("Year = %ld, Month = %ld, Day = %ld\n", _stEXIF_DateTime.u32Year, _stEXIF_DateTime.u32Month, _stEXIF_DateTime.u32Day);
2047             JPEG_DEBUG_API_MSG("Hour = %ld, Minute = %ld, Second = %ld\n", _stEXIF_DateTime.u32Hour, _stEXIF_DateTime.u32Minute, _stEXIF_DateTime.u32Second);
2048         }
2049 #endif
2050         pJpegBuff += 4;
2051     }
2052 
2053     if(TRUE == bIFD_pt_found)
2054     {
2055         u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifHdrOffet));
2056         pExifHdrOffet += 2;
2057         JPEG_DEBUG_API_MSG("EXIF IFD : u16NumOfEntry = %d\n", u16NumOfEntry);
2058 
2059         for(i = 0; i < u16NumOfEntry; i++)
2060         {
2061             u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifHdrOffet));
2062 #if (SUPPORT_EXIF_EXTRA_INFO==false)
2063             if(JPEG_EXIF_TAG_DATETIME_ORI == u16Marker)
2064             {
2065                 pExifHdrOffet += 8;
2066                 _stEXIF_DateTime.bHasDataTime = TRUE;
2067                 //datavalue is an offset from exif marker for datetime
2068                 u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifHdrOffet));;
2069                 pExifHdrOffet = pExifHdr + u32DataValue;
2070                 JPEG_OVER_BUFFER_RET(pExifHdrOffet, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2071                 JPEG_DEBUG_API_MSG("pExifOriDateTime Offset = 0x%lx\n", (MS_U32)pExifHdrOffet);
2072                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Year);
2073                 pExifHdrOffet++;
2074                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Month);
2075                 pExifHdrOffet++;
2076                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Day);
2077                 pExifHdrOffet++;
2078                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Hour);
2079                 pExifHdrOffet++;
2080                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Minute);
2081                 pExifHdrOffet++;
2082                 JPEG_AtoU32(pExifHdrOffet, _stEXIF_DateTime.u32Second);
2083                 JPEG_DEBUG_API_MSG("=======JPEG EXIF DateTime=======\n");
2084                 JPEG_DEBUG_API_MSG("Year = %ld, Month = %ld, Day = %ld\n", _stEXIF_DateTime.u32Year, _stEXIF_DateTime.u32Month, _stEXIF_DateTime.u32Day);
2085                 JPEG_DEBUG_API_MSG("Hour = %ld, Minute = %ld, Second = %ld\n", _stEXIF_DateTime.u32Hour, _stEXIF_DateTime.u32Minute, _stEXIF_DateTime.u32Second);
2086                 break;
2087             }
2088 #else
2089             MS_U8 *pExifPtrOffset;
2090             MS_U16 u16Type;
2091             MS_U32 u32Count;
2092             switch(u16Marker)
2093             {
2094                 case JPEG_EXIF_TAG_DATETIME_ORI:
2095                     pExifPtrOffset = pExifHdrOffet + 8;
2096                     _stEXIF_DateTime.bHasDataTime = TRUE;
2097                     //datavalue is an offset from exif marker for datetime
2098                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2099                     pExifPtrOffset = pExifHdr + u32DataValue;
2100                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2101                     JPEG_DEBUG_API_MSG("pExifOriDateTime Offset = 0x%lx\n", (MS_U32)pExifPtrOffset);
2102                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Year);
2103                     pExifPtrOffset++;
2104                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Month);
2105                     pExifPtrOffset++;
2106                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Day);
2107                     pExifPtrOffset++;
2108                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Hour);
2109                     pExifPtrOffset++;
2110                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Minute);
2111                     pExifPtrOffset++;
2112                     JPEG_AtoU32(pExifPtrOffset, _stEXIF_DateTime.u32Second);
2113                     JPEG_DEBUG_API_MSG("=======JPEG EXIF DateTime=======\n");
2114                     JPEG_DEBUG_API_MSG("Year = %ld, Month = %ld, Day = %ld\n", _stEXIF_DateTime.u32Year, _stEXIF_DateTime.u32Month, _stEXIF_DateTime.u32Day);
2115                     JPEG_DEBUG_API_MSG("Hour = %ld, Minute = %ld, Second = %ld\n", _stEXIF_DateTime.u32Hour, _stEXIF_DateTime.u32Minute, _stEXIF_DateTime.u32Second);
2116                     break;
2117                 case JPEG_EXIF_TAG_ISO_SPEED_RATING:
2118                     pExifPtrOffset = pExifHdrOffet + 2;
2119                     u16Type = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2120                     pExifPtrOffset = pExifHdrOffet + 4;
2121                     u32Count = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2122                     pExifPtrOffset = pExifHdrOffet + 8;
2123                     if(u16Type==3)
2124                     {
2125                         if(u32Count==2)
2126                         {
2127                             _u32EXIF_ISOSpeedRatings=JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2128                         }
2129                         else if(u32Count==1)
2130                         {
2131                             _u32EXIF_ISOSpeedRatings=JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2132                         }
2133                     }
2134                     else if(u16Type==4)
2135                     {
2136                         if(u32Count==1)
2137                         {
2138                             _u32EXIF_ISOSpeedRatings=JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2139                         }
2140                     }
2141                     JPEG_DEBUG_API_MSG("ISO Speed Rating=%ld\n", _u32EXIF_ISOSpeedRatings);
2142                     break;
2143                 case JPEG_EXIF_TAG_EXPOSURE_TIME:
2144                     pExifPtrOffset = pExifHdrOffet + 8;
2145                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2146                     JPEG_DEBUG_API_MSG("ExposureTime Offset = 0x%lx\n", u32DataValue);
2147                     pExifPtrOffset = pExifHdr + u32DataValue;
2148                     //JPEG_DEBUG_API_MSG("ExposureTime addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2149                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2150                     _stEXIF_ExposureTime.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2151                     pExifPtrOffset+=4;
2152                     _stEXIF_ExposureTime.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2153                     JPEG_DEBUG_API_MSG("ExposureTime numerator=%ld, denominator=%ld\n", _stEXIF_ExposureTime.numerator, _stEXIF_ExposureTime.denominator);
2154                     break;
2155                 case JPEG_EXIF_TAG_F_NUMBER:
2156                     pExifPtrOffset = pExifHdrOffet + 8;
2157                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2158                     JPEG_DEBUG_API_MSG("FNumber Offset = 0x%lx\n", u32DataValue);
2159                     pExifPtrOffset = pExifHdr + u32DataValue;
2160                     //JPEG_DEBUG_API_MSG("FNumber addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2161                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2162                     _stEXIF_FNumber.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2163                     pExifPtrOffset+=4;
2164                     _stEXIF_FNumber.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2165                     JPEG_DEBUG_API_MSG("FNumber numerator=%ld, denominator=%ld\n", _stEXIF_FNumber.numerator, _stEXIF_FNumber.denominator);
2166                     break;
2167 
2168                 case JPEG_EXIF_TAG_SHUTTER_SPEED:
2169                     pExifPtrOffset = pExifHdrOffet + 8;
2170                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2171                     JPEG_DEBUG_API_MSG("ShutterSpeedValue Offset = 0x%lx\n", u32DataValue);
2172                     pExifPtrOffset = pExifHdr + u32DataValue;
2173                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2174                     //JPEG_DEBUG_API_MSG("ShutterSpeedValue addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2175                     _stEXIF_ShutterSpeedValue.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2176                     pExifPtrOffset+=4;
2177                     _stEXIF_ShutterSpeedValue.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2178                     JPEG_DEBUG_API_MSG("ShutterSpeedValue numerator=%ld, denominator=%ld\n", _stEXIF_ShutterSpeedValue.numerator, _stEXIF_ShutterSpeedValue.denominator);
2179                     break;
2180                 case JPEG_EXIF_TAG_APERTURE:
2181                     pExifPtrOffset = pExifHdrOffet + 8;
2182                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2183                     JPEG_DEBUG_API_MSG("ApertureValue Offset = 0x%lx\n", u32DataValue);
2184                     pExifPtrOffset = pExifHdr + u32DataValue;
2185                     //JPEG_DEBUG_API_MSG("ApertureValue addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2186                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2187                     _stEXIF_ApertureValue.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2188                     pExifPtrOffset+=4;
2189                     _stEXIF_ApertureValue.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2190                     JPEG_DEBUG_API_MSG("ApertureValue numerator=%ld, denominator=%ld\n", _stEXIF_ApertureValue.s_numerator, _stEXIF_ApertureValue.s_denominator);
2191                     break;
2192                 case JPEG_EXIF_TAG_EXPOSURE_BIAS:
2193                     pExifPtrOffset = pExifHdrOffet + 8;
2194                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2195                     JPEG_DEBUG_API_MSG("ExposureBiasValue Offset = 0x%lx\n", u32DataValue);
2196                     pExifPtrOffset = pExifHdr + u32DataValue;
2197                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2198                     //JPEG_DEBUG_API_MSG("ExposureBiasValue addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2199                     _stEXIF_ExposureBiasValue.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2200                     pExifPtrOffset+=4;
2201                     _stEXIF_ExposureBiasValue.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2202                     JPEG_DEBUG_API_MSG("ExposureBiasValue numerator=%ld, denominator=%ld\n", _stEXIF_ExposureBiasValue.numerator, _stEXIF_ExposureBiasValue.denominator);
2203                     break;
2204                 case JPEG_EXIF_TAG_FLASH:
2205                     pExifPtrOffset = pExifHdrOffet + 8;
2206                     _u16EXIF_Flash = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2207                     JPEG_DEBUG_API_MSG("EXIF_Flash=0x%x\n", _u16EXIF_Flash);
2208                     break;
2209                 case JPEG_EXIF_TAG_FOCAL_LENGTH:
2210                     pExifPtrOffset = pExifHdrOffet + 8;
2211                     u32DataValue = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2212                     JPEG_DEBUG_API_MSG("FocalLength Offset = 0x%lx\n", u32DataValue);
2213                     pExifPtrOffset = pExifHdr + u32DataValue;
2214                     JPEG_OVER_BUFFER_RET(pExifPtrOffset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2215                     //JPEG_DEBUG_API_MSG("FocalLength addr = 0x%lx\n", (MS_U32)pExifPtrOffset);
2216                     _stEXIF_FocalLength.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2217                     pExifPtrOffset+=4;
2218                     _stEXIF_FocalLength.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2219                     JPEG_DEBUG_API_MSG("FocalLength numerator=%ld, denominator=%ld\n", _stEXIF_FocalLength.s_numerator, _stEXIF_FocalLength.s_denominator);
2220                     break;
2221                 case JPEG_EXIF_TAG_IMAGE_WIDTH:
2222                     pExifPtrOffset = pExifHdrOffet + 2;
2223                     u16Type = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2224                     pExifPtrOffset = pExifHdrOffet + 8;
2225                     if(u16Type==3)
2226                     {
2227                         _u32EXIF_ImageWidth = (MS_U32)JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2228                     }
2229                     else
2230                     {
2231                         _u32EXIF_ImageWidth = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2232                     }
2233                     JPEG_DEBUG_API_MSG("EXIF_ImageWidth=%ld\n", _u32EXIF_ImageWidth);
2234                     break;
2235                 case JPEG_EXIF_TAG_IMAGE_HEIGHT:
2236                     pExifPtrOffset = pExifHdrOffet + 2;
2237                     u16Type = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2238                     pExifPtrOffset = pExifHdrOffet + 8;
2239                     if(u16Type==3)
2240                     {
2241                         _u32EXIF_ImageHeight = (MS_U32)JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2242                     }
2243                     else
2244                     {
2245                         _u32EXIF_ImageHeight = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pExifPtrOffset));
2246                     }
2247                     JPEG_DEBUG_API_MSG("EXIF_ImageHeight=%ld\n", _u32EXIF_ImageHeight);
2248                     break;
2249                 case JPEG_EXIF_TAG_EXPOSURE_PROGRAM:
2250                     pExifPtrOffset = pExifHdrOffet + 8;
2251                     _u16EXIF_Exposureprogram = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pExifPtrOffset));
2252                     JPEG_DEBUG_API_MSG("EXIF_Exposureprogram=0x%x\n", _u16EXIF_Exposureprogram);
2253                     break;
2254                 default:
2255                     break;
2256             }
2257 #endif
2258             pExifHdrOffet += 12;
2259         }
2260     }
2261 
2262     if(E_JPEG_TYPE_THUMBNAIL != _u8DecodeType)
2263     {
2264         JPEG_DEBUG_API_MSG("decode type is not thumbnail...\n");
2265         return FALSE;
2266     }
2267 
2268     // 1th IFD
2269     u32tmp = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2270     if (u32tmp == 0)
2271         return FALSE;
2272     else
2273     {
2274         if(u32tmp >= data_length)
2275         {
2276             JPEG_DEBUG_API_ERR("u32tmp = %ld is invalid > %ld\n", u32tmp,data_length);
2277             return FALSE;
2278         }
2279         pNextIfd = &pTiffHdrPtr[u32tmp];
2280         JPEG_DEBUG_API_MSG("1st IFD pNextIfd = 0x%lx, u32tmp = %ld\n", (MS_U32)pNextIfd, u32tmp);
2281     }
2282 
2283     u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pNextIfd));
2284     pNextIfd += 2;
2285 
2286     // Boundary check, prevent from buffer over-run
2287     if((((MS_U32) pNextIfd) - INTERNAL_BUFFER_ADDR + u16NumOfEntry*12)>=data_length)
2288     {
2289         return FALSE;
2290     }
2291 
2292     for (i = 0; i < u16NumOfEntry; i++)
2293     {
2294         u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pNextIfd));
2295 //        u32Len = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pNextIfd + 4));
2296         u32tmp = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pNextIfd + 8));
2297 
2298         switch (u16Marker)
2299         {
2300             case JPEG_TIFF_JPEG_IMG_BYTES:
2301                 _u16ThumbnailSize = u32tmp;
2302                 break;
2303             case JPEG_TIFF_SOI_OFFSET:
2304                 JPEG_DEBUG_API_MSG("Thumbnail marker found in 0x%lx!!\n", (MS_U32)pNextIfd);
2305                 _bThumbnailFound = TRUE;
2306                 _u32ThumbnailOffset += u32tmp;
2307                 break;
2308             default:
2309                 break;
2310         }
2311 
2312         pNextIfd += 12;
2313     }
2314 
2315     // Boundary check, prevent from buffer over-run
2316     if(_bThumbnailFound)
2317     {
2318         if((_u32ThumbnailOffset + _u16ThumbnailSize) > data_length)
2319             _bThumbnailFound = FALSE;
2320 
2321         // means it only contains SOI header..
2322         if(_u16ThumbnailSize<=2)
2323             _bThumbnailFound = FALSE;
2324     }
2325 
2326     return _bThumbnailFound;
2327 }
2328 
2329 // Used to skip unrecognized markers.
JPEG_skip_bytes(MS_U32 count)2330 JPEG_STATIC void JPEG_skip_bytes(MS_U32 count)
2331 {
2332     while(count!=0)
2333     {
2334         // Any bytes remaining in buffer?
2335         if ( !_u32In_buf_left )
2336         {
2337             // Try to get more bytes.
2338             if(!JPEG_fill_read_buffer())
2339             {
2340                 JPEG_DEBUG_API_MSG("JPEG_fill_read_buffer failed!!!\n");
2341                 _u32In_buf_left = 0;
2342             }
2343             // Still nothing to get?
2344             if ( !_u32In_buf_left )
2345             {
2346                 // should not happen
2347                 break;
2348             }
2349         }
2350 
2351         if(count<_u32In_buf_left)
2352         {
2353             _u32In_buf_left -= count;
2354             _pu8In_buf_ofs += count;
2355             count = 0;
2356         }
2357         else
2358         {
2359             count -= _u32In_buf_left;
2360             _u32In_buf_left = 0;
2361         }
2362     }
2363 }
2364 //------------------------------------------------------------------------------
2365 #if SUPPORT_MPO_FORMAT
2366 #define JPEG_TAG_MPO                EndianChangeL(0x4D504600)
2367 #define JPEG_MPO_TAG_MPF_VERSION        0xB000
2368 #define JPEG_MPO_TAG_NUM_OF_IMAGES      0xB001
2369 #define JPEG_MPO_TAG_MP_ENTRY           0xB002
2370 
2371 static JPEG_MPO_INDEX_INFO _stMPOIndex;
2372 static JPEG_MPO_ATTRIBUTE_INFO _stMPOAttri[JPEG_MPO_MAX_SUPPORT_IMAGE];
2373 //For start of offset of MPO(only for first MPO picture)
2374 static MS_U32 _u32App2MarkerOffset = 0;
2375 
2376 typedef enum
2377 {
2378     E_JPEG_MPO_ATTR_IMAGE_NO            = 0xB101
2379   , E_JPEG_MPO_ATTR_PAN_ORIENTATION     = 0xB201
2380   , E_JPEG_MPO_ATTR_PAN_OVERLAP_H       = 0xB202
2381   , E_JPEG_MPO_ATTR_PAN_OVERLAP_V       = 0xB203
2382   , E_JPEG_MPO_ATTR_BASE_VIEWPOINT_NO   = 0xB204
2383   , E_JPEG_MPO_ATTR_CONVERGENCE_ANGLE   = 0xB205
2384   , E_JPEG_MPO_ATTR_BASELINE_LENGTH     = 0xB206
2385   , E_JPEG_MPO_ATTR_DIVERGENCE_ANGLE    = 0xB207
2386   , E_JPEG_MPO_ATTR_AXIS_DISTANCE_X     = 0xB208
2387   , E_JPEG_MPO_ATTR_AXIS_DISTANCE_Y     = 0xB209
2388   , E_JPEG_MPO_ATTR_AXIS_DISTANCE_Z     = 0xB20A
2389   , E_JPEG_MPO_ATTR_YAW_ANGLE           = 0xB20B
2390   , E_JPEG_MPO_ATTR_PITCH_ANGLE         = 0xB20C
2391   , E_JPEG_MPO_ATTR_ROLL_ANGLE          = 0xB20D
2392 } JPEG_MPO_ATTR_TAG;
2393 
2394 #define MAX_MPO_ATTR_TAG_NUM        14          // from E_JPEG_MPO_ATTR_IMAGE_NO to E_JPEG_MPO_ATTR_ROLL_ANGLE
2395 
2396 //***************************************************
2397 //Initialize EXIF MPO database
2398 //***************************************************
JPEG_InitMPO(void)2399 static void JPEG_InitMPO(void)
2400 {
2401     MS_U32 i = 0;
2402 
2403     //init MPO Index structure
2404     _stMPOIndex.num_of_image = 1;
2405     for(i = 0; i < JPEG_MPO_MAX_SUPPORT_IMAGE; i++)
2406     {
2407         _stMPOIndex.mp_entry[i].attribute = 0;
2408         _stMPOIndex.mp_entry[i].size = 0;
2409         _stMPOIndex.mp_entry[i].offset = 0;
2410         _stMPOIndex.mp_entry[i].image1_no = 0;
2411         _stMPOIndex.mp_entry[i].image2_no = 0;
2412     }
2413 
2414     // init MPO Attribute structure
2415     for(i = 0; i < JPEG_MPO_MAX_SUPPORT_IMAGE; i++)
2416     {
2417         _stMPOAttri[i].MPIndividualNum = 0;
2418         _stMPOAttri[i].PanOrientation= 0;
2419         _stMPOAttri[i].PanOverlap_H.numerator = 0;
2420         _stMPOAttri[i].PanOverlap_H.denominator = 0;
2421         _stMPOAttri[i].PanOverlap_V.numerator = 0;
2422         _stMPOAttri[i].PanOverlap_V.denominator = 0;
2423         _stMPOAttri[i].BaseViewpointNum = 0;
2424         _stMPOAttri[i].ConvergenceAngle.s_numerator = 0;
2425         _stMPOAttri[i].ConvergenceAngle.s_denominator = 0;
2426         _stMPOAttri[i].BaselineLength.numerator = 0;
2427         _stMPOAttri[i].BaselineLength.denominator = 0;
2428         _stMPOAttri[i].VerticalDivergence.s_numerator = 0;
2429         _stMPOAttri[i].VerticalDivergence.s_denominator = 0;
2430         _stMPOAttri[i].AxisDistance_X.s_numerator = 0;
2431         _stMPOAttri[i].AxisDistance_X.s_denominator = 0;
2432         _stMPOAttri[i].AxisDistance_Y.s_numerator = 0;
2433         _stMPOAttri[i].AxisDistance_Y.s_denominator = 0;
2434         _stMPOAttri[i].AxisDistance_Z.s_numerator = 0;
2435         _stMPOAttri[i].AxisDistance_Z.s_denominator = 0;
2436         _stMPOAttri[i].YawAngle.s_numerator = 0;
2437         _stMPOAttri[i].YawAngle.s_denominator = 0;
2438         _stMPOAttri[i].PitchAngle.s_numerator = 0;
2439         _stMPOAttri[i].PitchAngle.s_denominator = 0;
2440         _stMPOAttri[i].RollAngle.s_numerator = 0;
2441         _stMPOAttri[i].RollAngle.s_denominator = 0;
2442         _stMPOAttri[i].used = FALSE;
2443     }
2444 }
2445 //***************************************************
2446 //Parse EXIF MPO Attribute header
2447 //***************************************************
JPEG_DecodeMPAttrIFD(MS_U8 * data,MS_U8 * start_of_offset,MS_U32 count)2448 static MS_BOOL JPEG_DecodeMPAttrIFD(MS_U8 *data, MS_U8 *start_of_offset, MS_U32 count)
2449 {
2450     MS_U8 *pMPOAttrPtr = data;
2451     MS_U8 *pMPO_offset;
2452     MS_U16 u16Marker, i = 0, image_idx = 0;
2453 
2454     JPEG_DEBUG_API_MSG("JPEG_DecodeMPAttrIFD, count = %ld\n", count);
2455 
2456     if(count >MAX_MPO_ATTR_TAG_NUM)
2457     {
2458         JPEG_DEBUG_API_ERR("Warning!!!unreasonable attribute IFD number !!!\n");
2459         return TRUE;
2460     }
2461 
2462     for(i = 0; i < JPEG_MPO_MAX_SUPPORT_IMAGE; i++)
2463     {
2464         if(FALSE == _stMPOAttri[i].used)
2465         {
2466             image_idx = i;
2467             break;
2468         }
2469     }
2470 
2471     if(JPEG_MPO_MAX_SUPPORT_IMAGE <= i)
2472     {
2473         JPEG_DEBUG_API_MSG("Not support too much images in MPO!!\n");
2474         return FALSE;
2475     }
2476 
2477     _stMPOAttri[image_idx].used = TRUE;
2478 
2479     for(i = 0; i < count; i++)
2480     {
2481         u16Marker = (JPEG_MPO_ATTR_TAG)JPEG_Tiff_EndianChangeS(JPEG_GetU16(pMPOAttrPtr));
2482         pMPOAttrPtr += 8;
2483         switch(u16Marker)
2484         {
2485             case E_JPEG_MPO_ATTR_IMAGE_NO:
2486                 _stMPOAttri[image_idx].MPIndividualNum = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2487                 break;
2488             case E_JPEG_MPO_ATTR_PAN_ORIENTATION:
2489                 _stMPOAttri[image_idx].PanOrientation = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2490                 break;
2491             case E_JPEG_MPO_ATTR_PAN_OVERLAP_H:
2492                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2493                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2494                 _stMPOAttri[image_idx].PanOverlap_H.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2495                 pMPO_offset += 4;
2496                 _stMPOAttri[image_idx].PanOverlap_H.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2497                 break;
2498             case E_JPEG_MPO_ATTR_PAN_OVERLAP_V:
2499                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2500                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2501                 _stMPOAttri[image_idx].PanOverlap_V.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2502                 pMPO_offset += 4;
2503                 _stMPOAttri[image_idx].PanOverlap_V.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2504                 break;
2505             case E_JPEG_MPO_ATTR_BASE_VIEWPOINT_NO:
2506                 _stMPOAttri[image_idx].BaseViewpointNum = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2507                 break;
2508             case E_JPEG_MPO_ATTR_CONVERGENCE_ANGLE:
2509                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2510                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2511                 _stMPOAttri[image_idx].ConvergenceAngle.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2512                 pMPO_offset += 4;
2513                 _stMPOAttri[image_idx].ConvergenceAngle.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2514                 break;
2515             case E_JPEG_MPO_ATTR_BASELINE_LENGTH:
2516                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2517                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2518                 _stMPOAttri[image_idx].BaselineLength.numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2519                 pMPO_offset += 4;
2520                 _stMPOAttri[image_idx].BaselineLength.denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2521                 break;
2522             case E_JPEG_MPO_ATTR_DIVERGENCE_ANGLE:
2523                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2524                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2525                 _stMPOAttri[image_idx].VerticalDivergence.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2526                 pMPO_offset += 4;
2527                 _stMPOAttri[image_idx].VerticalDivergence.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2528                 break;
2529             case E_JPEG_MPO_ATTR_AXIS_DISTANCE_X:
2530                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2531                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2532                 _stMPOAttri[image_idx].AxisDistance_X.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2533                 pMPO_offset += 4;
2534                 _stMPOAttri[image_idx].AxisDistance_X.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2535                 break;
2536             case E_JPEG_MPO_ATTR_AXIS_DISTANCE_Y:
2537                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2538                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2539                 _stMPOAttri[image_idx].AxisDistance_Y.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2540                 pMPO_offset += 4;
2541                 _stMPOAttri[image_idx].AxisDistance_Y.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2542                 break;
2543             case E_JPEG_MPO_ATTR_AXIS_DISTANCE_Z:
2544                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2545                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2546                 _stMPOAttri[image_idx].AxisDistance_Z.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2547                 pMPO_offset += 4;
2548                 _stMPOAttri[image_idx].AxisDistance_Z.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2549                 break;
2550             case E_JPEG_MPO_ATTR_YAW_ANGLE:
2551                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2552                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2553                 _stMPOAttri[image_idx].YawAngle.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2554                 pMPO_offset += 4;
2555                 _stMPOAttri[image_idx].YawAngle.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2556                 break;
2557             case E_JPEG_MPO_ATTR_PITCH_ANGLE:
2558                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2559                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2560                 _stMPOAttri[image_idx].PitchAngle.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2561                 pMPO_offset += 4;
2562                 _stMPOAttri[image_idx].PitchAngle.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2563                 break;
2564             case E_JPEG_MPO_ATTR_ROLL_ANGLE:
2565                 pMPO_offset = start_of_offset + JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPOAttrPtr));
2566                 JPEG_OVER_BUFFER_RET(pMPO_offset, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2567                 _stMPOAttri[image_idx].RollAngle.s_numerator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2568                 pMPO_offset += 4;
2569                 _stMPOAttri[image_idx].RollAngle.s_denominator = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pMPO_offset));
2570                 break;
2571             default:
2572                 JPEG_DEBUG_API_ERR("Unknown MPO attribute marker =0x%x!!!!!!\n", u16Marker);
2573                 break;
2574         }
2575         pMPOAttrPtr += 4;
2576     }
2577     return TRUE;
2578 }
2579 //***************************************************
2580 //Parse EXIF MPO header
2581 //***************************************************
JPEG_DecodeMPOInfo(MS_U8 * data)2582 static MS_BOOL JPEG_DecodeMPOInfo(MS_U8 *data)
2583 {
2584     MS_U8 *pJpegBuff = data;
2585     MS_U8 *pMPOHdrPtr;
2586     MS_U32 u32tmp, u32Len;
2587     MS_U16 u16Marker, u16NumOfEntry, i;
2588 
2589     u32tmp = JPEG_GetU32(pJpegBuff);
2590     if (u32tmp != JPEG_TAG_MPO)
2591     {
2592         return FALSE;
2593     }
2594 
2595     // mpo header start from here
2596     pJpegBuff += 4;
2597     u16Marker = JPEG_GetU16(pJpegBuff);
2598     if (u16Marker == JPEG_TIFF_BIG_ENDIAN)
2599         _bTiffBigEndian = TRUE;
2600     else if (u16Marker == JPEG_TIFF_LITTLE_ENDIAN)
2601         _bTiffBigEndian = FALSE;
2602     else
2603         return FALSE;
2604 
2605 //    _u32ThumbnailOffset += 4;
2606     pMPOHdrPtr = pJpegBuff;
2607 
2608     pJpegBuff += 2;
2609     u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2610     if (u16Marker != 0x002A)
2611         return FALSE;
2612 
2613     //offset to 1st MPO IFD
2614     pJpegBuff += 2;
2615     u32Len = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2616 
2617     //1st MPO IFD start from here(either MP Index IFD or MP Attribute IFD)
2618     //Count Info
2619     pJpegBuff = pMPOHdrPtr + u32Len;
2620     JPEG_OVER_BUFFER_RET(pJpegBuff, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2621 
2622     u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2623 
2624     JPEG_DEBUG_API_MSG("num of entry = %d\n", u16NumOfEntry);
2625 
2626     //MPFVersion
2627     pJpegBuff += 2;
2628     u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2629     if(u16Marker == JPEG_MPO_TAG_MPF_VERSION)
2630     {
2631         pJpegBuff += 12;
2632         u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2633         if(u16Marker == JPEG_MPO_TAG_NUM_OF_IMAGES)
2634         {
2635             //It is a MP Index IFD
2636             //Check number of images
2637             pJpegBuff += 8;
2638             u32tmp = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2639 
2640             if(JPEG_MPO_MAX_SUPPORT_IMAGE < u32tmp)
2641             {
2642                 JPEG_DEBUG_API_MSG("Not support %ld images in MPO!!\n", u32tmp);
2643                 _stMPOIndex.num_of_image = 0xFFFFFFFF;
2644                 return FALSE;
2645             }
2646 
2647             //Init MPO database
2648             JPEG_InitMPO();
2649 
2650             //start of offset for every individual picture
2651             _stMPOIndex.start_of_offset = _u32App2MarkerOffset + 8 - MRC_BUFFER_ADDR;
2652 
2653             //the numbers of image
2654             _stMPOIndex.num_of_image = u32tmp;
2655 
2656             printf_red("detected _stMPOIndex.num_of_image=0x%lx\n", _stMPOIndex.num_of_image);
2657             //MP Entry
2658             pJpegBuff += 4;
2659             u16Marker = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2660             if(u16Marker != JPEG_MPO_TAG_MP_ENTRY)
2661             {
2662                 JPEG_DEBUG_API_MSG("No MP Entry in MP index IFD!!\n");
2663                 return FALSE;
2664             }
2665 
2666             //offset to next IFD(MP Attribute IFD)
2667             pJpegBuff = pMPOHdrPtr + 10 + u16NumOfEntry*12;
2668             u32Len = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2669             JPEG_DEBUG_API_MSG("pJpegBuff = %lx, u32Len = %ld\n", (MS_U32)pJpegBuff, u32Len);
2670 
2671             //MP Entry Internal Fields
2672             pJpegBuff += 4;
2673             for(i = 0; i < _stMPOIndex.num_of_image; i++)
2674             {
2675                 //Image Attribute
2676                 _stMPOIndex.mp_entry[i].attribute = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2677                 //Image size
2678                 pJpegBuff += 4;
2679                 _stMPOIndex.mp_entry[i].size = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2680                 //Image size
2681                 pJpegBuff += 4;
2682                 _stMPOIndex.mp_entry[i].offset = JPEG_Tiff_EndianChangeL(JPEG_GetU32(pJpegBuff));
2683                 //Image size
2684                 pJpegBuff += 4;
2685                 _stMPOIndex.mp_entry[i].image1_no = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2686                 //Image size
2687                 pJpegBuff += 2;
2688                 _stMPOIndex.mp_entry[i].image2_no = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2689                 pJpegBuff += 2;
2690                 JPEG_DEBUG_API_MSG("%d th : attribute = %lx\n", i, _stMPOIndex.mp_entry[i].attribute);
2691                 JPEG_DEBUG_API_MSG("%d th : size = %lx\n", i, _stMPOIndex.mp_entry[i].size);
2692                 JPEG_DEBUG_API_MSG("%d th : offset = %lx\n", i, _stMPOIndex.mp_entry[i].offset);
2693                 JPEG_DEBUG_API_MSG("%d th : image1_no = %x\n", i, _stMPOIndex.mp_entry[i].image1_no);
2694                 JPEG_DEBUG_API_MSG("%d th : image2_no = %x\n", i, _stMPOIndex.mp_entry[i].image2_no);
2695             }
2696 
2697             //get MPO Attribute count
2698             pJpegBuff = pMPOHdrPtr + u32Len;
2699             JPEG_OVER_BUFFER_RET(pJpegBuff, INTERNAL_BUFFER_ADDR, INTERNAL_BUFFER_SIZE);
2700 
2701             u16NumOfEntry = JPEG_Tiff_EndianChangeS(JPEG_GetU16(pJpegBuff));
2702 
2703             pJpegBuff += 2;
2704         }
2705     }
2706 
2707     //Decode MP Attr header
2708     return JPEG_DecodeMPAttrIFD(pJpegBuff, pMPOHdrPtr, u16NumOfEntry);
2709 }
2710 
2711 
2712 #endif
2713 
2714 // Parse APP0 to get X&Y density.
JPEG_read_app0_marker(void)2715 static MS_BOOL JPEG_read_app0_marker( void )
2716 {
2717 
2718     MS_U32 left;
2719     MS_U8 u8Xthumbnail, u8Ythumbnail;
2720     JPEG_DEBUG_API_MSG("APP0\n");
2721 
2722     //Get APP0 length
2723     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2724 
2725     left-= 2;
2726 
2727     if(left < 8)
2728     {
2729         JPEG_terminate( E_JPEG_BAD_APP0_MARKER );
2730         return FALSE;
2731     }
2732 
2733     //Skip Block ID "JFIF"
2734     JPEG_skip_bytes(5);
2735 
2736     left-= 5;
2737 
2738     //Skip Version Number
2739     JPEG_skip_bytes(2);
2740 
2741     left-= 2;
2742 
2743     //Get units
2744     App0_Unit_Data.u8Unit = JPEG_get_char();
2745     App0_Unit_Data.u16Xdensity= (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2746     App0_Unit_Data.u16Ydensity= (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2747 
2748     left-= 5;
2749 
2750     //Get thumbnail x,y
2751     u8Xthumbnail = JPEG_get_char();
2752     u8Ythumbnail = JPEG_get_char();
2753 
2754     left-= 2;
2755 
2756     //Skip Version Number
2757     JPEG_skip_bytes(3*u8Xthumbnail*u8Ythumbnail);
2758 
2759     left-= 3*u8Xthumbnail*u8Ythumbnail;
2760 
2761     JPEG_DEBUG_API_MSG("APP0 : unit : %d\n",(MS_U16)App0_Unit_Data.u8Unit);
2762     JPEG_DEBUG_API_MSG("APP0 : Xdensity : %d\n",App0_Unit_Data.u16Xdensity);
2763     JPEG_DEBUG_API_MSG("APP0 : Ydensity : %d\n",App0_Unit_Data.u16Ydensity);
2764 
2765     if(left > 0)
2766         JPEG_skip_bytes(left);
2767 
2768     return TRUE;
2769 }
2770 
_msAPI_JPEG_get_APP0_info(MS_U8 * unit,MS_U16 * x,MS_U16 * y)2771 void _msAPI_JPEG_get_APP0_info( MS_U8 *unit, MS_U16 *x, MS_U16 *y )
2772 {
2773     *unit = App0_Unit_Data.u8Unit;
2774     *x = App0_Unit_Data.u16Xdensity;
2775     *y = App0_Unit_Data.u16Ydensity;
2776 }
2777 //------------------------------------------------------------------------------
2778 // Read exif info
JPEG_read_app1_marker(void)2779 JPEG_STATIC MS_BOOL JPEG_read_app1_marker(void)
2780 {
2781     MS_U16 length;
2782     MS_U8 *exif_buffer = (MS_U8 *) INTERNAL_BUFFER_ADDR;
2783     MS_U16 i = 0;
2784 
2785     JPEG_DEBUG_API_MSG("APP1\n");
2786 
2787     length = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2788 
2789     if ( length < 2 )
2790     {
2791         JPEG_terminate( E_JPEG_BAD_APP1_MARKER );
2792         return FALSE;
2793     }
2794 
2795     length -= 2;
2796 
2797 #if SUPPORT_MPO_FORMAT
2798     u32MPFOffset = length;
2799     JPEG_DEBUG_API_MSG("u32MPFOffset=0x%lx\n", u32MPFOffset);
2800 #endif
2801     while((length - i)!=0)
2802     {
2803         exif_buffer[i] = (MS_U8) JPEG_get_char();
2804         i++;
2805     }
2806 
2807     if(JPEG_DecodeExifInfo(exif_buffer, length) == TRUE)
2808     {
2809         JPEG_DEBUG_API_MSG("FOUND THUMBNAIL!\n");
2810         _u32ThumbnailBufferOffset = _u32ThumbnailOffset;
2811         _u16ThumbnailBufferSize = _u16ThumbnailSize;
2812     }
2813     else
2814     {
2815         JPEG_DEBUG_API_MSG("NO THUMBNAIL!\n");
2816     }
2817     return TRUE;
2818 }
2819 
2820 #if SUPPORT_MPO_FORMAT
2821 
2822 // Read exif app2 info
JPEG_read_app2_marker(void)2823 static MS_BOOL JPEG_read_app2_marker( void )
2824 {
2825     MS_U16 length;
2826     MS_U8 *exif_buffer = (MS_U8 *) (INTERNAL_BUFFER_ADDR + u32MPFOffset);
2827     MS_U16 i = 0;
2828 
2829     JPEG_DEBUG_API_MSG("APP2\n");
2830 
2831     //For start of offset of MPO(only for first MPO picture)
2832     JPEG_DEBUG_API_MSG("_pu8In_buf_ofs = %lx\n", (MS_U32)_pu8In_buf_ofs);
2833     _u32App2MarkerOffset = (MS_U32)_pu8In_buf_ofs - 2;
2834 
2835     length = (MS_U16)((JPEG_get_char()<<8)+JPEG_get_char());
2836 
2837     if ( length < 2 )
2838     {
2839         JPEG_terminate( E_JPEG_BAD_APP2_MARKER );
2840         return FALSE;
2841     }
2842 
2843     length -= 2;
2844 
2845     if(length > INTERNAL_BUFFER_SIZE - u32MPFOffset)
2846     {
2847         JPEG_terminate( E_JPEG_NOTENOUGHMEM );
2848         return FALSE;
2849     }
2850 
2851     while((length - i)!=0)
2852     {
2853         exif_buffer[i] = (MS_U8) JPEG_get_char();
2854         i++;
2855     }
2856 
2857     if(JPEG_DecodeMPOInfo(exif_buffer)==TRUE)
2858     {
2859         JPEG_DEBUG_API_MSG("FOUND MPO!\n");
2860         bIsMPOFormat = TRUE;
2861         u32MPFOffset = 0;           // Reset to zero after FOUND MPO
2862 
2863         //// TODO: MPO format not support thumbnail now....
2864         //_u8DecodeType = E_JPEG_TYPE_MAIN;
2865         //printf_red("NOTE: MPO not support thumbnail now!!!\n");
2866     }
2867     else
2868     {
2869         JPEG_DEBUG_API_MSG("NO MPO!\n");
2870         // Not clear num_of_image for the image with 2 app2 marker
2871         //_stMPOIndex.num_of_image = 0;
2872     }
2873     return TRUE;
2874 }
2875 
2876 
2877 #endif
2878 
2879 /* END OF EXIF PARSING SECTION */
2880 //------------------------------------------------------------------------------
2881 // Read a Huffman code table.
JPEG_read_dht_marker(void)2882 JPEG_STATIC MS_BOOL JPEG_read_dht_marker(void)
2883 {
2884     MS_U16 i, indextmp, count;
2885     MS_U32 left;
2886     MS_U8 u8Huff_num[17];
2887     MS_U8 u8Huff_val[256];
2888 
2889     JPEG_DEBUG_API_MSG("DHT\n");
2890 
2891     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2892 
2893     if ( left < 2 )
2894     {
2895         JPEG_terminate( E_JPEG_BAD_DHT_MARKER );
2896         return FALSE;
2897     }
2898 
2899     left -= 2;
2900 
2901     while ( left )
2902     {
2903         // set it to zero, initialize
2904         JPEG_memset((void *)u8Huff_num, 0, 17);
2905         JPEG_memset((void *)u8Huff_val, 0, 256);
2906 
2907         indextmp = JPEG_get_char();
2908 
2909         u8Huff_num[0] = 0;
2910 
2911         count = 0;
2912 
2913         for ( i = 1; i <= 16; i++ )
2914         {
2915             u8Huff_num[i] = JPEG_get_char();
2916             count += u8Huff_num[i];
2917         }
2918 
2919         if ( count > 255 )
2920         {
2921             JPEG_terminate( E_JPEG_BAD_DHT_COUNTS );
2922             return FALSE;
2923         }
2924 
2925         for ( i = 0; i < count; i++ )
2926         {
2927             u8Huff_val[i] = JPEG_get_char();
2928         }
2929 
2930         i = 1 + 16 + count;
2931 
2932         if ( left < ( MS_U32 )i )
2933         {
2934             JPEG_terminate( E_JPEG_BAD_DHT_MARKER );
2935             return FALSE;
2936         }
2937 
2938         left -= i;
2939 
2940         indextmp = ( indextmp & 0x0F ) + ( ( indextmp & 0x10 ) >> 4 ) * ( JPEG_MAXHUFFTABLES >> 1 ); //???
2941 
2942         if ( indextmp >= JPEG_MAXHUFFTABLES )
2943         {
2944             JPEG_terminate( E_JPEG_BAD_DHT_INDEX );
2945             return FALSE;
2946         }
2947 
2948         if(_Huff_info[indextmp].bValid==FALSE)
2949             _Huff_info[indextmp].bValid = TRUE;
2950 
2951         JPEG_memcpy( (void *)_Huff_info[indextmp].u8Huff_num, (void *)u8Huff_num, 17 );
2952         JPEG_memcpy( (void *)_Huff_info[indextmp].u8Huff_val, (void *)u8Huff_val, 256 );
2953 
2954         // Compute the inverse order of HuffNum. this step is only needed in JPD mode (baseline)
2955         for(i = 1; i<=16; i++)
2956         {
2957             if(u8Huff_num[17 - i]!=0)
2958             {
2959                 count = count - u8Huff_num[17 - i];
2960                 u8Huff_num[17 - i] = count;
2961             }
2962             else
2963                 u8Huff_num[17 - i] = 0xFF;
2964         }
2965         JPEG_memcpy( (void *)_Huff_info[indextmp].u8Symbol, (void *)u8Huff_num, 17 );
2966     }
2967     return TRUE;
2968 }
2969 //------------------------------------------------------------------------------
2970 // Read a quantization table.
JPEG_read_dqt_marker(void)2971 JPEG_STATIC MS_BOOL JPEG_read_dqt_marker(void)
2972 {
2973     MS_U16 n, i, prec;
2974     MS_U32 left;
2975     MS_U32 temp;
2976 
2977     JPEG_DEBUG_API_MSG("DQT\n");
2978 
2979     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
2980 
2981     if ( left < 2 )
2982     {
2983         JPEG_terminate( E_JPEG_BAD_DQT_MARKER );
2984         return FALSE;
2985     }
2986 
2987     left -= 2;
2988 
2989     while ( left )
2990     {
2991         n = JPEG_get_char();
2992         prec = n >> 4;
2993         n &= 0x0F;
2994 
2995         if ( n >= JPEG_MAXQUANTTABLES )
2996         {
2997             JPEG_terminate( E_JPEG_BAD_DQT_TABLE );
2998             return FALSE;
2999         }
3000 
3001         if(_QuantTables[n].bValid == FALSE)
3002             _QuantTables[n].bValid = TRUE;
3003 
3004         // read quantization entries, in zag order
3005         for ( i = 0; i < 64; i++ )
3006         {
3007             temp = JPEG_get_char();
3008 
3009             if ( prec )
3010             {
3011                 temp = ( temp << 8 ) + JPEG_get_char();
3012             }
3013 
3014             _QuantTables[n].s16Value[i] = temp;
3015         }
3016 
3017         i = 64 + 1;
3018 
3019         if ( prec )
3020         {
3021             i += 64;
3022         }
3023 
3024         if ( left < ( MS_U32 )i )
3025         {
3026             JPEG_terminate( E_JPEG_BAD_DQT_LENGTH );
3027             return FALSE;
3028         }
3029 
3030         left -= i;
3031     }
3032     return TRUE;
3033 }
3034 //------------------------------------------------------------------------------
3035 // Read the start of frame (SOF) marker.
JPEG_read_sof_marker(void)3036 JPEG_STATIC MS_BOOL JPEG_read_sof_marker(void)
3037 {
3038     MS_U8 i;
3039     MS_U32 left;
3040     MS_U16 JpegMaxWidth, JpegMaxHeight;
3041     MS_U8 c1;
3042 
3043     JPEG_DEBUG_API_MSG("SOF\n");
3044 
3045     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3046 
3047     if ( JPEG_get_char() != 8 )   /* precision: sorry, only 8-bit precision is supported right now */
3048     {
3049         JPEG_terminate( E_JPEG_BAD_PRECISION );
3050         return FALSE;
3051     }
3052 
3053     _u16Image_y_size = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3054     _u16Image_x_size = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3055 
3056     // save the original image size
3057     _u16OriginalImage_x_size = _u16Image_x_size;
3058     _u16OriginalImage_y_size = _u16Image_y_size;
3059 
3060     if((_bProgressive_flag)
3061     && (E_JPEG_TYPE_MAIN == _u8DecodeType))
3062     {
3063        //Define MAX picture size for progressive, 8 is the max factor of scale down
3064         JpegMaxWidth  = JPEG_PRO_MAX_WIDTH * 8;
3065         JpegMaxHeight = JPEG_PRO_MAX_HEIGHT * 8;
3066 
3067         //Calculate Internal Buffer size
3068         //check needed buffer for progressive jpeg decoding, for worst case:
3069         MS_U32 buff_size = _u16Image_x_size*_u16Image_y_size*3*2
3070                          + _u16Image_x_size*_u16Image_y_size*3*2/64
3071                          + JPEG_DEFAULT_EXIF_SIZE
3072                          + 8*1024;
3073 //        if( (MS_U32)(_u16Image_x_size*_u16Image_y_size*3*2) > (MS_U32)(INTERNAL_BUFFER_SIZE - JPEG_DEFAULT_EXIF_SIZE) )
3074         if( buff_size > INTERNAL_BUFFER_SIZE )
3075         {
3076             JPEG_DEBUG_API_ERR("Progressive image size too big... do not handle it\n");
3077             JPEG_terminate( E_JPEG_NOTENOUGHMEM );
3078             return FALSE;
3079         }
3080     }
3081     else
3082     {
3083         //Define MAX picture size for Baseline, 8 is the max factor of scale down
3084         JpegMaxWidth  = JPEG_MAX_WIDTH*8;
3085         JpegMaxHeight = JPEG_MAX_HEIGHT*8;
3086     }
3087 
3088     if ((_u16Image_y_size < 1) || (_u16Image_y_size > JpegMaxHeight))
3089     {
3090         JPEG_terminate(E_JPEG_BAD_HEIGHT);
3091         return FALSE;
3092     }
3093 
3094     if ((_u16Image_x_size < 1) || (_u16Image_x_size > JpegMaxWidth))
3095     {
3096         JPEG_terminate(E_JPEG_BAD_WIDTH);
3097         return FALSE;
3098     }
3099 
3100     _u8Comps_in_frame = JPEG_get_char();
3101 
3102     if ( _u8Comps_in_frame > JPEG_MAXCOMPONENTS )
3103     {
3104         JPEG_terminate( E_JPEG_TOO_MANY_COMPONENTS );
3105         return FALSE;
3106     }
3107 
3108     if ( left != ( MS_U32 )( _u8Comps_in_frame * 3 + 8 ) )
3109     {
3110         JPEG_terminate( E_JPEG_BAD_SOF_LENGTH );
3111         return FALSE;
3112     }
3113 
3114     for ( i = 0; i < _u8Comps_in_frame; i++ )
3115     {
3116         _u8Comp_ident[i] = JPEG_get_char();
3117         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
3118         {
3119             _u8LumaCi = 0;
3120             _u8ChromaCi = 1;
3121             _u8Chroma2Ci = 2;
3122         }
3123 
3124         c1 = JPEG_get_char();
3125 
3126         _u8Comp_h_samp[i] = (c1 & 0xf0)>>4;
3127         _u8Comp_v_samp[i] = (c1 & 0x0f);
3128         _u8Comp_quant[i] = JPEG_get_char();
3129 
3130         // patch from LG driver for the file with the following problem:
3131         // only has one component, but its sampling factor is 1x2
3132         // Per the JPEG spec A.2.2 (see the attached file, "regardless of the values of H1 and V1"),
3133         // please always set H=1 & V=1 to hw, when mono image.
3134         if(_u8Comps_in_frame==1)    //allen.chang 2011/6/16 patch
3135         {
3136             _u8Comp_h_samp[0] =1;
3137             _u8Comp_v_samp[0] =1;
3138         }
3139 
3140     }
3141     return TRUE;
3142 }
3143 //------------------------------------------------------------------------------
3144 // Used to skip unrecognized markers.
JPEG_skip_variable_marker(void)3145 JPEG_STATIC MS_BOOL JPEG_skip_variable_marker(void)
3146 {
3147     MS_U32 left;
3148 
3149     JPEG_DEBUG_API_MSG("SKIP markers\n");
3150 
3151     left = (MS_U32)((JPEG_get_char()<<8)|JPEG_get_char());
3152 
3153     if ( left < 2 )
3154     {
3155         JPEG_terminate( E_JPEG_BAD_VARIABLE_MARKER );
3156         return FALSE;
3157     }
3158 
3159     left -= 2;
3160 
3161     JPEG_skip_bytes(left);
3162     return TRUE;
3163 }
3164 //------------------------------------------------------------------------------
3165 // Read a define restart interval (DRI) marker.
JPEG_read_dri_marker(void)3166 JPEG_STATIC MS_BOOL JPEG_read_dri_marker(void)
3167 {
3168     JPEG_DEBUG_API_MSG("DRI\n");
3169 
3170     if ( (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char()) != 4 )
3171     {
3172         JPEG_terminate( E_JPEG_BAD_DRI_LENGTH );
3173         return FALSE;
3174     }
3175 
3176     _u16Restart_interval = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3177     return TRUE;
3178 }
3179 //------------------------------------------------------------------------------
3180 // Read a start of scan (SOS) marker.
JPEG_read_sos_marker(void)3181 JPEG_STATIC MS_BOOL JPEG_read_sos_marker(void)
3182 {
3183     MS_U32 left;
3184     MS_U16 i, ci, n, c, cc;
3185     MS_U8 c1;
3186 
3187     JPEG_DEBUG_API_MSG("SOS\n");
3188 
3189     left = (MS_U16) ((JPEG_get_char()<<8) + JPEG_get_char());
3190 
3191     n = JPEG_get_char();
3192 
3193     _u8Comps_in_scan = n;
3194 
3195     left -= 3;
3196 
3197     if ( ( left != ( MS_U32 )( n * 2 + 3 ) ) || ( n < 1 ) || ( n > JPEG_MAXCOMPSINSCAN ) )
3198     {
3199         JPEG_terminate( E_JPEG_BAD_SOS_LENGTH );
3200         return FALSE;
3201     }
3202 
3203     for ( i = 0; i < n; i++ )
3204     {
3205         cc = JPEG_get_char();
3206         c = JPEG_get_char();
3207         left -= 2;
3208 
3209         for ( ci = 0; ci < _u8Comps_in_frame; ci++ )
3210         {
3211             if ( cc == _u8Comp_ident[ci] )
3212             {
3213                 break;
3214             }
3215         }
3216 
3217         if ( ci >= _u8Comps_in_frame )
3218         {
3219             JPEG_terminate( E_JPEG_BAD_SOS_COMP_ID );
3220             return FALSE;
3221         }
3222 
3223         _u8Comp_list[i] = ci;
3224         _u8Comp_dc_tab[ci] = ( c >> 4 ) & 15;
3225         _u8Comp_ac_tab[ci] = ( c & 15 ) + ( JPEG_MAXHUFFTABLES >> 1 );
3226     }
3227 
3228 #ifndef CMODEL
3229     //HW limitation, for baseline JPEG, U.V need to refer to the same DC and AC huffman table.
3230     if(!_bProgressive_flag && (_u8Comps_in_frame == 3)) //Y.U.V.
3231     {
3232         if((_u8Comp_dc_tab[1] != _u8Comp_dc_tab[2])
3233         || (_u8Comp_ac_tab[1] != _u8Comp_ac_tab[2]))
3234         {
3235 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
3236             JPEG_DEBUG_API_MSG("U, V use different Huffman table~~\n");
3237             bIs3HuffTbl = TRUE;
3238 #else
3239             JPEG_terminate( E_JPEG_UNSUPPORTED_HUFF_DECODE );
3240             return FALSE;
3241 #endif
3242         }
3243     }
3244 #endif
3245 
3246     _u8Spectral_start = JPEG_get_char();
3247     _u8Spectral_end = JPEG_get_char();
3248     c1 = JPEG_get_char();
3249     _u8Successive_high = (c1 & 0xf0)>>4;
3250     _u8Successive_low = (c1 & 0x0f);
3251 
3252     if ( !_bProgressive_flag )
3253     {
3254         _u8Spectral_start = 0;
3255         _u8Spectral_end = 63;
3256     }
3257 
3258     left -= 3;
3259 
3260     JPEG_skip_bytes(left);    /* read past whatever is left */
3261     return TRUE;
3262 }
3263 //------------------------------------------------------------------------------
3264 // Finds the next marker.
JPEG_next_marker(void)3265 JPEG_STATIC MS_U32 JPEG_next_marker(void) //ok
3266 {
3267     MS_U32 c, bytes;
3268 
3269     bytes = 0;
3270 
3271     do
3272     {
3273         do
3274         {
3275             bytes++;
3276 
3277             c = JPEG_get_char();
3278             //JPEG_DEBUG_API_MSG("c = %X\n",c);
3279         }
3280         while ( c != 0xFF );
3281 
3282         do
3283         {
3284             c = JPEG_get_char();
3285             //JPEG_DEBUG_API_MSG("c = %X\n",c);
3286         }
3287         while ( c == 0xFF );
3288     }
3289     while ( c == 0 );
3290 
3291     // If bytes > 0 here, there where extra bytes before the marker (not good).
3292 
3293     return c;
3294 }
3295 //------------------------------------------------------------------------------
3296 // Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is
3297 // encountered.
JPEG_process_markers(void)3298 JPEG_STATIC MS_U32 JPEG_process_markers(void)
3299 {
3300     MS_U32 c;
3301 
3302     JPEG_DEBUG_API_MSG("JPEG_process_markers:\n");
3303     for ( ; ; )
3304     {
3305         c = JPEG_next_marker();
3306 
3307         switch ( c )
3308         {
3309             case E_JPEG_APP1:
3310                 // Prevent from there's thumbnail in thumbnail... & multiple APP1
3311                 // Although it's impossible.. =_=
3312 //                if((E_JPEG_TYPE_THUMBNAIL == _u8DecodeType)
3313 //                && (FALSE == _bThumbnailFound))
3314                 // We need to get EXIF info for decoding main picture or thumbnail.
3315                 if((__bIsMjpeg == FALSE)
3316                 && (FALSE == _bThumbnailFound))
3317                 {
3318                     if( !JPEG_read_app1_marker() )
3319                         return FALSE;
3320                 }
3321                 else
3322                 {
3323                     if( !JPEG_skip_variable_marker() )
3324                         return FALSE;
3325                 }
3326                 break;
3327 #if SUPPORT_MPO_FORMAT
3328             case E_JPEG_APP2:
3329                 if( !JPEG_read_app2_marker() )
3330                     return FALSE;
3331                 break;
3332 
3333 #endif
3334             case E_JPEG_SOF0:
3335             case E_JPEG_SOF1:
3336             case E_JPEG_SOF2:
3337             case E_JPEG_SOF3:
3338             case E_JPEG_SOF5:
3339             case E_JPEG_SOF6:
3340             case E_JPEG_SOF7:
3341                 //      case E_JPEG_JPG:
3342             case E_JPEG_SOF9:
3343             case E_JPEG_SOF10:
3344             case E_JPEG_SOF11:
3345             case E_JPEG_SOF13:
3346             case E_JPEG_SOF14:
3347             case E_JPEG_SOF15:
3348             case E_JPEG_SOI:
3349             case E_JPEG_EOI:
3350             case E_JPEG_SOS:
3351                 {
3352                     return c;
3353                 }
3354             case E_JPEG_DHT:
3355                 {
3356                     if( !JPEG_read_dht_marker() )
3357                         return FALSE;
3358                     _HeadCheck.DHT = TRUE;
3359                     break;
3360                 }
3361                 // Sorry, no arithmitic support at this time. Dumb patents!
3362             case E_JPEG_DAC:
3363                 {
3364                     JPEG_terminate( E_JPEG_NO_ARITHMETIC_SUPPORT );
3365                     return FALSE;
3366                     break;
3367                 }
3368             case E_JPEG_DQT:
3369                 {
3370                     if( !JPEG_read_dqt_marker() )
3371                         return FALSE;
3372                     _HeadCheck.DQT = TRUE;
3373                     break;
3374                 }
3375             case E_JPEG_DRI:
3376                 {
3377                     if( !JPEG_read_dri_marker() )
3378                         return FALSE;
3379                     break;
3380                 }
3381                 //case E_JPEG_APP0:  /* no need to read the JFIF marker */
3382 
3383             case E_JPEG_JPG:
3384             case E_JPEG_RST0:
3385                 /* no parameters */
3386             case E_JPEG_RST1:
3387             case E_JPEG_RST2:
3388             case E_JPEG_RST3:
3389             case E_JPEG_RST4:
3390             case E_JPEG_RST5:
3391             case E_JPEG_RST6:
3392             case E_JPEG_RST7:
3393             case E_JPEG_TEM:
3394                 {
3395                     JPEG_terminate( E_JPEG_UNEXPECTED_MARKER );
3396                     return FALSE;
3397                     break;
3398                 }
3399             case E_JPEG_APP0:
3400                 if (!bMHEG5)
3401                 {
3402                     if(!JPEG_skip_variable_marker())
3403                     {
3404                         return FALSE;
3405                     }
3406                     break;
3407                 }
3408                 if(!JPEG_read_app0_marker())
3409                 {
3410                     return FALSE;
3411                 }
3412                 break;
3413             case E_JPEG_DNL:
3414             case E_JPEG_DHP:
3415             case E_JPEG_EXP:
3416 #if (SUPPORT_MPO_FORMAT == 0)
3417             case E_JPEG_APP2:
3418 #endif
3419             case E_JPEG_APP3:
3420             case E_JPEG_APP4:
3421             case E_JPEG_APP5:
3422             case E_JPEG_APP6:
3423             case E_JPEG_APP7:
3424             case E_JPEG_APP8:
3425             case E_JPEG_APP9:
3426             case E_JPEG_APP10:
3427             case E_JPEG_APP11:
3428             case E_JPEG_APP12:
3429             case E_JPEG_APP13:
3430             case E_JPEG_APP14:
3431             case E_JPEG_APP15:
3432             case E_JPEG_JPG0:
3433             case E_JPEG_JPG1:
3434             case E_JPEG_JPG2:
3435             case E_JPEG_JPG3:
3436             case E_JPEG_JPG4:
3437             case E_JPEG_JPG5:
3438             case E_JPEG_JPG6:
3439             case E_JPEG_JPG7:
3440             case E_JPEG_JPG8:
3441             case E_JPEG_JPG9:
3442             case E_JPEG_JPG10:
3443             case E_JPEG_JPG11:
3444             case E_JPEG_JPG12:
3445             case E_JPEG_JPG13:
3446             case E_JPEG_COM:
3447                 /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */
3448                 {
3449                     if(!JPEG_skip_variable_marker())
3450                     {
3451                         return FALSE;
3452                     }
3453                     break;
3454                 }
3455             default:
3456                 break;
3457         }
3458     }
3459     return TRUE;
3460 }
3461 //------------------------------------------------------------------------------
3462 // Finds the start of image (SOI) marker.
3463 // This code is rather defensive: it only checks the first 512 bytes to avoid
3464 // false positives.
JPEG_locate_soi_marker(void)3465 JPEG_STATIC MS_BOOL JPEG_locate_soi_marker(void)
3466 {
3467     MS_U32 lastchar, thischar;
3468     MS_U32 bytesleft;
3469 
3470     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
3471 
3472     lastchar = JPEG_get_char();
3473 
3474     //JPEG_DEBUG_API_MSG("0x%lx\n", lastchar);
3475     thischar = JPEG_get_char();
3476 
3477     //JPEG_DEBUG_API_MSG("0x%lx\n", thischar);
3478     /* ok if it's a normal JPEG file without a special header */
3479 
3480     if ((0xFF == lastchar)
3481     && (E_JPEG_SOI == thischar))
3482     {
3483         //JPEG_DEBUG_API_MSG("SOI\n");
3484         return TRUE;
3485     }
3486 
3487     // Set this value to 0x1000 for 4k alignment MPO case when parse 2nd/3rd/... JPEG file
3488     bytesleft = 0x1000; //Fix this number from 512 -> 640 for some cases
3489 
3490     for ( ; ; )
3491     {
3492         if ( --bytesleft == 0 )
3493         {
3494             JPEG_terminate( E_JPEG_NOT_JPEG );
3495             return FALSE;
3496         }
3497 
3498         lastchar = thischar;
3499 
3500         thischar = JPEG_get_char();
3501 
3502         //JPEG_DEBUG_API_MSG("%x ", (MS_U8)thischar);
3503         //if(bytesleft%8==0)
3504         //    JPEG_DEBUG_API_MSG("\n");
3505 
3506         if ((0xFF == lastchar)
3507         && (E_JPEG_SOI == thischar))
3508         {
3509             //JPEG_DEBUG_API_MSG("SOI\n");
3510             break;
3511         }
3512     }
3513 
3514     /* Check the next character after marker: if it's not 0xFF, it can't
3515        be the start of the next marker, so it probably isn't a JPEG */
3516 /*  need to check for more detail, currently _u32Bit_buf is not updated during JPEG_get_char()
3517     thischar = ( _u32Bit_buf >> 8 ) & 0xFF;
3518 
3519     if ( thischar != 0xFF )
3520     {
3521         JPEG_terminate( E_JPEG_NOT_JPEG );
3522         return FALSE;
3523     }
3524 */
3525     return TRUE;
3526 }
3527 //------------------------------------------------------------------------------
3528 // Find a start of frame (SOF) marker.
JPEG_locate_sof_marker(void)3529 JPEG_STATIC MS_BOOL JPEG_locate_sof_marker(void)
3530 {
3531     MS_U32 c;
3532     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
3533 
3534     if(!JPEG_locate_soi_marker())
3535         return FALSE;
3536 
3537     c = JPEG_process_markers();
3538 
3539     if( c == FALSE )
3540         return FALSE;
3541 
3542     switch ( c )
3543     {
3544         case E_JPEG_SOF2:
3545             {
3546                 JPEG_DEBUG_API_MSG("Progressive\n");
3547                 _bProgressive_flag = TRUE;
3548                 u32SOFOffset = u32DataOffset + JPEG_GetECS() - 2;
3549                 if(!JPEG_read_sof_marker())
3550                     return FALSE;
3551                 break;
3552             }
3553         case E_JPEG_SOF0:
3554             /* baseline DCT */
3555         case E_JPEG_SOF1:
3556             /* extended sequential DCT */
3557             {
3558                 JPEG_DEBUG_API_MSG("Baseline\n");
3559                 u32SOFOffset = u32DataOffset + JPEG_GetECS() - 2;
3560                 if(!JPEG_read_sof_marker())
3561                     return FALSE;
3562                 break;
3563             }
3564         case E_JPEG_SOF9:
3565             /* Arithmitic coding */
3566             {
3567                 JPEG_terminate( E_JPEG_NO_ARITHMETIC_SUPPORT );
3568                 return FALSE;
3569                 break;
3570             }
3571 
3572         default:
3573             {
3574                 JPEG_DEBUG_API_MSG("parsed unsupported marker = 0x%04lX\n", c);
3575                 JPEG_terminate( E_JPEG_UNSUPPORTED_MARKER );
3576                 return FALSE;
3577                 break;
3578             }
3579     }
3580     return TRUE;
3581 }
3582 //------------------------------------------------------------------------------
3583 // Find a start of scan (SOS) marker.
JPEG_locate_sos_marker(void)3584 JPEG_STATIC MS_BOOL JPEG_locate_sos_marker(void)
3585 {
3586     MS_U32 c;
3587 
3588     c = JPEG_process_markers();
3589 
3590     if ( c == E_JPEG_EOI )
3591     {
3592         return FALSE;
3593     }
3594     else if ( c != E_JPEG_SOS )
3595     {
3596         JPEG_terminate( E_JPEG_UNEXPECTED_MARKER );
3597         return FALSE;
3598     }
3599 
3600     if(!JPEG_read_sos_marker())
3601         return FALSE;
3602 
3603     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);
3604     JPEG_DEBUG_API_MSG("%02x %02x %02x %02x %02x %02x %02x %02x \n",
3605     *_pu8In_buf_ofs, *(_pu8In_buf_ofs+1), *(_pu8In_buf_ofs+2), *(_pu8In_buf_ofs+3),
3606     *(_pu8In_buf_ofs+4), *(_pu8In_buf_ofs+5), *(_pu8In_buf_ofs+6), *(_pu8In_buf_ofs+7)
3607     );
3608 
3609     return TRUE;
3610 }
3611 //------------------------------------------------------------------------------
3612 // Reset thumbnail parameters
JPEG_init_thumbnail(void)3613 JPEG_STATIC void JPEG_init_thumbnail(void)
3614 {
3615     _bThumbnailFound = FALSE;
3616     _u32ThumbnailOffset = 0;
3617     _u16ThumbnailSize = 0;
3618     _bTiffBigEndian = FALSE;
3619 
3620     _u32ThumbnailBufferOffset = 0;
3621     _u16ThumbnailSize = 0;
3622     _bThumbnailAccessMode = FALSE;
3623 
3624     _stEXIF_DateTime.bHasDataTime = FALSE;
3625     _stEXIF_DateTime.u32Year = 0;
3626     _stEXIF_DateTime.u32Month = 0;
3627     _stEXIF_DateTime.u32Day = 0;
3628     _stEXIF_DateTime.u32Hour = 0;
3629     _stEXIF_DateTime.u32Minute = 0;
3630     _stEXIF_DateTime.u32Second = 0;
3631     _eEXIF_Orientation = E_JPEG_EXIF_ORIENT_NOT_FOUND;
3632 #if SUPPORT_EXIF_EXTRA_INFO
3633     JPEG_memset((void *)_u8EXIF_Manufacturer, 0, JPEG_MANUFACTURER_SIZE);
3634     JPEG_memset((void *)_u8EXIF_Model, 0, JPEG_MODEL_SIZE);
3635     _u16EXIF_Flash = 0;
3636     _u16EXIF_Exposureprogram = 0;
3637     _u32EXIF_ISOSpeedRatings = 0;
3638     _stEXIF_ShutterSpeedValue.numerator = 0;
3639     _stEXIF_ShutterSpeedValue.denominator= 0;
3640     _stEXIF_ApertureValue.s_numerator= 0;
3641     _stEXIF_ApertureValue.s_denominator= 0;
3642     _stEXIF_ExposureBiasValue.numerator= 0;
3643     _stEXIF_ExposureBiasValue.denominator= 0;
3644     _stEXIF_FocalLength.s_numerator= 0;
3645     _stEXIF_FocalLength.s_denominator= 0;
3646     _u32EXIF_ImageWidth = 0;
3647     _u32EXIF_ImageHeight = 0;
3648     _stEXIF_ExposureTime.numerator = 0;
3649     _stEXIF_ExposureTime.denominator =0;
3650     _stEXIF_FNumber.numerator = 0;
3651     _stEXIF_FNumber.denominator =0;
3652 #endif
3653 }
3654 //------------------------------------------------------------------------------
3655 // Reset everything to default/uninitialized state.
JPEG_init(void)3656 JPEG_STATIC MS_BOOL JPEG_init(void)
3657 {
3658     MS_U16 i;
3659     JPEG_DEBUG_API_MSG("%s!!\n", __FUNCTION__);
3660     _u8LumaCi = 1;
3661     _u8ChromaCi = 2;
3662     _u8Chroma2Ci = 3;
3663     bIs3HuffTbl = FALSE;
3664 
3665     _Error_code = E_JPEG_NO_ERROR;
3666 
3667 #ifdef CMODEL
3668     _bReady_flag = FALSE;
3669 #endif
3670 
3671     _u16Image_x_size = _u16Image_y_size = 0;
3672     _u16OriginalImage_x_size = _u16OriginalImage_y_size = 0;
3673     _u16AlignedImageWidth = _u16AlignedImagePitch = _u16AlignedImageHeight = 0;
3674     _u16AlignedImagePitch_H = 0;
3675 
3676 ////    _pStream = _Pstream;
3677 
3678     _bProgressive_flag = FALSE;
3679 #ifndef CMODEL
3680     _u8DownScaleRatio = E_JPD_DOWNSCALE_ORG;
3681 #endif
3682     _u32RLEOffset = 0;
3683     _bFirstRLE = TRUE;
3684 
3685     _s16dc_pred[0] = _s16dc_pred[1] = _s16dc_pred[2] = 0;
3686 
3687     _JPD_IsDecoding = FALSE; //For H/W bug, some cases can not exit after decode done.
3688     _JPD_ReCheckTime = 0; //For H/W bug, some cases can not exit after decode done, record repeat time.
3689     _JPD_PreVIdx = 0;     //For H/W bug, some cases can not exit after decode done, record previous Vidx.
3690 
3691     _Progressive_ROI_flag = FALSE; //CL82399
3692     ROI_width = 0; //CL82399
3693 
3694     u8PreLHFlag = E_JPEG_BUFFER_NONE;
3695 
3696     u32MRCheckCount = 0;
3697 
3698     for(i = 0; i<JPEG_MAXHUFFTABLES; i++)
3699     {
3700         _Huff_info[i].bValid = FALSE;
3701         JPEG_memset((void *)_Huff_info[i].u8Huff_num, 0, 17);
3702         JPEG_memset((void *)_Huff_info[i].u8Huff_val, 0, 256);
3703         JPEG_memset((void *)_Huff_info[i].u8Symbol, 0, 17);
3704         JPEG_memset((void *)_Huff_info[i].u16Code, 0, 17);
3705     }
3706 
3707     for(i = 0; i<JPEG_MAXQUANTTABLES; i++)
3708     {
3709         _QuantTables[i].bValid = FALSE;
3710         JPEG_memset(_QuantTables[i].s16Value, 0, 64);
3711     }
3712 
3713     gu8Scan_type = E_JPEG_GRAYSCALE;
3714 
3715     _u8Comps_in_frame = 0;
3716 
3717     JPEG_memset((void *)_u8Comp_h_samp, 0, sizeof( _u8Comp_h_samp ) );
3718     JPEG_memset((void *)_u8Comp_v_samp, 0, sizeof( _u8Comp_v_samp ) );
3719     JPEG_memset((void *)_u8Comp_quant, 0, sizeof( _u8Comp_quant ) );
3720     JPEG_memset((void *)_u8Comp_ident, 0, sizeof( _u8Comp_ident ) );
3721     JPEG_memset((void *)_u16Comp_h_blocks, 0, sizeof( _u16Comp_h_blocks ) );
3722     JPEG_memset((void *)_u16Comp_v_blocks, 0, sizeof( _u16Comp_v_blocks ) );
3723 
3724     _u8Comps_in_scan = 0;
3725     JPEG_memset((void *)_u8Comp_list, 0, sizeof( _u8Comp_list ) );
3726     JPEG_memset((void *)_u8Comp_dc_tab, 0, sizeof( _u8Comp_dc_tab ) );
3727     JPEG_memset((void *)_u8Comp_ac_tab, 0, sizeof( _u8Comp_ac_tab ) );
3728 
3729     _u8Spectral_start = 0;
3730     _u8Spectral_end = 0;
3731     _u8Successive_low = 0;
3732     _u8Successive_high = 0;
3733 
3734     gu8Max_mcu_x_size = 0;
3735     gu8Max_mcu_y_size = 0;
3736 
3737     _u8Blocks_per_mcu = 0;
3738     _u32Max_blocks_per_row = 0;
3739     _u16Mcus_per_row = 0;
3740     _u16Mcus_per_col = 0;
3741 
3742     JPEG_memset((void *)_u8Mcu_org, 0, sizeof( _u8Mcu_org ) );
3743 
3744 #ifdef CMODEL
3745     gu16Real_dest_bytes_per_scan_line = 0;
3746     gu16Dest_bytes_per_scan_line = 0;
3747     gu8Dest_bytes_per_pixel = 0;
3748 #endif
3749 
3750     JPEG_memset((void *)_pBlocks, 0, sizeof( _pBlocks ) );
3751 
3752 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
3753     _u16Total_lines_left = 0;
3754 #ifdef CMODEL
3755     gu16Mcu_lines_left = 0;
3756 #endif
3757     JPEG_memset((void *)_u32Block_y_mcu, 0, sizeof( _u32Block_y_mcu ) );
3758     JPEG_memset((void *)_Huff_tbls, 0, sizeof( _Huff_tbls ) );
3759     JPEG_memset((void *)_DC_Coeffs, 0, sizeof( _DC_Coeffs ) );
3760     JPEG_memset((void *)_AC_Coeffs, 0, sizeof( _AC_Coeffs ) );
3761     JPEG_memset((void *)_u32Last_dc_val, 0, sizeof( _u32Last_dc_val ) );
3762 
3763     _u32EOB_run = 0;
3764 #endif
3765 
3766     _pu8In_buf_ofs = _pu8In_buf;
3767 ////    _u32In_buf_left = 0;
3768 ////    _bEOF_flag = FALSE;
3769     _u8Tem_flag = 0;
3770 
3771     //sharon JPEG_memset((void *)_pu8In_buf, 0, sizeof(MS_U8)*(MRC_BUFFER_SIZE + 128) );
3772 
3773     _u16Restart_interval = 0;
3774     _u16Restarts_left = 0;
3775     _u16Next_restart_num = 0;
3776 
3777     gu16Max_mcus_per_row = 0;
3778     _u16Max_blocks_per_mcu = 0;
3779     _u16Max_mcus_per_col = 0;
3780 
3781 #ifdef CMODEL
3782     JPEG_memset((void *)gps16Block_seg, 0, sizeof( gps16Block_seg ) );
3783     gpu8Sample_buf = NULL;
3784 #endif
3785 
3786 #if SW_JPD_RGB_CMYK
3787     JPEG_memset( _ps16Block_seg, 0, sizeof( _ps16Block_seg ) );
3788     gpu8Sample_buf = NULL;
3789 #endif
3790 
3791     // Tell the stream we're going to use it.
3792     //_pStream->attach();
3793 
3794     // Ready the input buffer.
3795     if(_bThumbnailAccessMode)
3796     {
3797         // It means that thumbnail is found and re-call JPEG_init()
3798         // to re-fill thumbnail data to internal buffer.
3799         if(!JPEG_fill_read_buffer())
3800         {
3801             JPEG_DEBUG_API_MSG("JPEG_init : JPEG_fill_read_buffer failed!!!\n");
3802             return FALSE;
3803         }
3804     }
3805     else
3806     {
3807 ////        _u32In_buf_left = MRC_BUFFER_SIZE;  //sharon
3808 ////        _Total_Decoded_Size = MRC_BUFFER_SIZE;  //sharon
3809 ////        _u32Total_bytes_read = MRC_BUFFER_SIZE;  //sharon
3810         _u32Total_bytes_read = _u32In_buf_left;
3811     }
3812 
3813     // Prime the bit buffer.
3814     _s16Bits_left = 0;
3815     _u32Bit_buf = 0;
3816 
3817 //    _pu32ExifHeaderAddr = 0;
3818 
3819 #ifdef CMODEL
3820     for ( i = 0; i < JPEG_MAXBLOCKSPERROW; i++ )
3821     {
3822         gu8Block_max_zag_set[i] = 64;
3823     }
3824 #endif
3825 
3826 #if SW_JPD_RGB_CMYK
3827     for ( i = 0; i < JPEG_MAXBLOCKSPERROW; i++ )
3828     {
3829         _u8Block_max_zag_set[i] = 64;
3830     }
3831 #endif
3832     return TRUE;
3833 }
3834 //------------------------------------------------------------------------------
3835 // The coeff_buf series of methods originally stored the coefficients
3836 // into a "virtual" file which was located in EMS, XMS, or a disk file. A cache
3837 // was used to make this process more efficient. Now, we can store the entire
3838 // 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)3839 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)
3840 {
3841     PJPEG_CoeffBuf cb = ( PJPEG_CoeffBuf )JPEG_alloc( sizeof( JPEG_CoeffBuf ) );
3842 
3843     if(cb == NULL)
3844         return NULL;
3845 
3846     cb->u16Block_num_x = block_num_x;
3847     cb->u16Block_num_y = block_num_y;
3848 
3849     cb->u8Block_len_x = block_len_x;
3850     cb->u8Block_len_y = block_len_y;
3851 
3852     cb->u16Block_size = ( block_len_x * block_len_y ) * sizeof( JPEG_BLOCK_TYPE );
3853 
3854     cb->pu8Data = ( MS_U8 * )JPEG_alloc( cb->u16Block_size * block_num_x * block_num_y );
3855 
3856     if(cb->pu8Data == NULL)
3857         return NULL;
3858 
3859     return cb;
3860 }
3861 //------------------------------------------------------------------------------
JPEG_coeff_buf_getp(PJPEG_CoeffBuf cb,MS_U16 block_x,MS_U16 block_y)3862 JPEG_STATIC JPEG_BLOCK_TYPE * JPEG_coeff_buf_getp( PJPEG_CoeffBuf cb, MS_U16 block_x, MS_U16 block_y )
3863 {
3864     if ( block_x >= cb->u16Block_num_x )
3865     {
3866         JPEG_terminate( E_JPEG_ASSERTION_ERROR );
3867         return NULL;
3868     }
3869 
3870     if ( block_y >= cb->u16Block_num_y )
3871     {
3872         JPEG_terminate( E_JPEG_ASSERTION_ERROR );
3873         return NULL;
3874     }
3875 
3876     return ( JPEG_BLOCK_TYPE * )((MS_U32)( cb->pu8Data + block_x * cb->u16Block_size
3877                                             + block_y * (cb->u16Block_size * cb->u16Block_num_x)));
3878 }
3879 //------------------------------------------------------------------------------
3880 // Creates the tables needed for efficient Huffman decoding.
JPEG_make_huff_table(MS_U8 indextmp)3881 JPEG_STATIC MS_BOOL JPEG_make_huff_table(MS_U8 indextmp)
3882 {
3883     MS_U16 p, i, l, si;
3884     MS_U8 huffsize[257];
3885     MS_U16 huffcode[257];
3886     MS_U16 _code;
3887     MS_U16 subtree;
3888     MS_U16 code_size;
3889     MS_U16 lastp;
3890 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
3891     MS_S16 nextfreeentry;
3892 #endif
3893     MS_S16 currententry;
3894 
3895 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
3896     JPEG_HuffTbl *hs = &_Huff_tbls[indextmp];
3897 #endif
3898 
3899     JPEG_memset((void *)huffsize, 0, sizeof(huffsize));
3900     JPEG_memset((void *)huffcode, 0, sizeof(huffcode));
3901 
3902     p = 0;
3903 
3904     for ( l = 1; l <= 16; l++ )
3905     {
3906         for ( i = 1; i <= _Huff_info[indextmp].u8Huff_num[l]; i++ )
3907         {
3908             huffsize[p++] = l;
3909 
3910             //kevinhuang, add protection
3911             if ( p >= 257 )
3912             {
3913                 JPEG_terminate( E_JPEG_UNDEFINED_HUFF_TABLE );
3914                 return FALSE;
3915             }
3916         }
3917     }
3918 
3919     huffsize[p] = 0;
3920 
3921     lastp = p;
3922 
3923     _code = 0;
3924     si = huffsize[0];
3925     p = 0;
3926 
3927     while ( huffsize[p] )
3928     {
3929         while ( huffsize[p] == si )
3930         {
3931             huffcode[p++] = _code;
3932             _code++;
3933 
3934             //kevinhuang, add protection
3935             if ( p >= 257 )
3936             {
3937                 JPEG_terminate( E_JPEG_UNDEFINED_HUFF_TABLE );
3938                 return FALSE;
3939             }
3940         }
3941 
3942         _code <<= 1;
3943         si++;
3944     }
3945 
3946     // Calculate the min code
3947     for(i = 1; i<=16; i++)
3948         _Huff_info[indextmp].u16Code[i] = huffcode[_Huff_info[indextmp].u8Symbol[i]] << (15 - (i - 1));
3949 
3950 
3951 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
3952     // In JPD mode, SW doesn't need huff table when baseline decoding
3953 #if SW_JPD_RGB_CMYK
3954     if((E_JPEG_CMYK != gu8Scan_type)
3955     && (E_JPEG_RGB != gu8Scan_type))
3956 #endif
3957     {
3958 #ifndef CMODEL
3959         if(_bProgressive_flag==FALSE)
3960             return TRUE;
3961 #endif
3962     }
3963 
3964     JPEG_DEBUG_API_MSG("Make HUFF TABLE\n");
3965 
3966     JPEG_memset((void *)(hs->s16Look_up), 0, sizeof( hs->s16Look_up ) );
3967     JPEG_memset((void *)(hs->s16Tree), 0, sizeof( hs->s16Tree ) );
3968     JPEG_memset((void *)(hs->u8Code_size), 0, sizeof( hs->u8Code_size ) );
3969 
3970     nextfreeentry = -1;
3971 
3972     p = 0;
3973 
3974     while ( p < lastp )
3975     {
3976         i = _Huff_info[indextmp].u8Huff_val[p];
3977         _code = huffcode[p];
3978         code_size = huffsize[p];
3979 
3980         hs->u8Code_size[i] = code_size;
3981 
3982         if ( code_size <= 8 )
3983         {
3984             _code <<= ( 8 - code_size );
3985 
3986             for ( l = 1 << ( 8 - code_size ); l > 0; l-- )
3987             {
3988                 hs->s16Look_up[_code] = i;
3989                 _code++;
3990             }
3991         }
3992         else
3993         {
3994             subtree = ( _code >> ( code_size - 8 ) ) & 0xFF;
3995 
3996             currententry = hs->s16Look_up[subtree];
3997 
3998             if ( currententry == 0 )
3999             {
4000                 hs->s16Look_up[subtree] = currententry = nextfreeentry;
4001 
4002                 nextfreeentry -= 2;
4003             }
4004 
4005             _code <<= ( 16 - ( code_size - 8 ) );
4006 
4007             for ( l = code_size; l > 9; l-- )
4008             {
4009                 if ( ( _code & 0x8000 ) == 0 )
4010                 {
4011                     currententry--;
4012                 }
4013 
4014                 if ( hs->s16Tree[-currententry - 1] == 0 )
4015                 {
4016                     hs->s16Tree[-currententry - 1] = nextfreeentry;
4017 
4018                     currententry = nextfreeentry;
4019 
4020                     nextfreeentry -= 2;
4021                 }
4022                 else
4023                 {
4024                     currententry = hs->s16Tree[-currententry - 1];
4025                 }
4026 
4027                 _code <<= 1;
4028             }
4029 
4030             if ( ( _code & 0x8000 ) == 0 )
4031             {
4032                 currententry--;
4033             }
4034 
4035             hs->s16Tree[-currententry - 1] = i;
4036         }
4037 
4038         p++;
4039     }
4040 #endif  //SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
4041     return TRUE;
4042 }
4043 //------------------------------------------------------------------------------
4044 // Verifies the quantization tables needed for this scan are available.
JPEG_check_quant_tables(void)4045 JPEG_STATIC MS_BOOL JPEG_check_quant_tables( void ) //ok
4046 {
4047     MS_U8 i;
4048 
4049     for ( i = 0; i < _u8Comps_in_scan; i++ )
4050     {
4051         if ( _QuantTables[_u8Comp_quant[_u8Comp_list[i]]].bValid==FALSE )
4052         {
4053             JPEG_terminate( E_JPEG_UNDEFINED_QUANT_TABLE );
4054             return FALSE;
4055         }
4056     }
4057     return TRUE;
4058 }
4059 //------------------------------------------------------------------------------
4060 // Verifies that all the Huffman tables needed for this scan are available.
JPEG_check_huff_tables(void)4061 JPEG_STATIC MS_BOOL JPEG_check_huff_tables( void )
4062 {
4063     MS_U8 i;
4064 
4065     for ( i = 0; i < _u8Comps_in_scan; i++ )
4066     {
4067         if ( ( _u8Spectral_start == 0 ) && ( _Huff_info[_u8Comp_dc_tab[_u8Comp_list[i]]].bValid== FALSE ) )
4068         {
4069             JPEG_terminate( E_JPEG_UNDEFINED_HUFF_TABLE );
4070             return FALSE;
4071         }
4072 
4073         if ( ( _u8Spectral_end > 0 ) && ( _Huff_info[_u8Comp_ac_tab[_u8Comp_list[i]]].bValid== FALSE ) )
4074         {
4075             JPEG_terminate( E_JPEG_UNDEFINED_HUFF_TABLE );
4076             return FALSE;
4077         }
4078     }
4079 
4080     for ( i = 0; i < JPEG_MAXHUFFTABLES; i++ )
4081     {
4082         if ( _Huff_info[i].bValid )
4083         {
4084             if(!JPEG_make_huff_table(i))
4085                 return FALSE;
4086         }
4087     }
4088     return TRUE;
4089 }
4090 //------------------------------------------------------------------------------
4091 // Determines the component order inside each MCU.
4092 // Also calcs how many MCU's are on each row, etc.
JPEG_calc_mcu_block_order(void)4093 JPEG_STATIC void JPEG_calc_mcu_block_order( void ) //ok
4094 {
4095     MS_U8 component_num, component_id;
4096     MS_U8 max_h_samp = 0, max_v_samp = 0;
4097 
4098     for ( component_id = 0; component_id < _u8Comps_in_frame; component_id++ )
4099     {
4100         if ( _u8Comp_h_samp[component_id] > max_h_samp )
4101         {
4102             max_h_samp = _u8Comp_h_samp[component_id];
4103         }
4104 
4105         if ( _u8Comp_v_samp[component_id] > max_v_samp )
4106         {
4107             max_v_samp = _u8Comp_v_samp[component_id];
4108         }
4109     }
4110 
4111     if((max_h_samp == 0) || (max_v_samp == 0))
4112     {
4113         JPEG_terminate( E_JPEG_NOT_ENOUGH_HEADER_INFO );
4114         return;
4115     }
4116 
4117     for ( component_id = 0; component_id < _u8Comps_in_frame; component_id++ )
4118     {
4119         _u16Comp_h_blocks[component_id] = ( ( ( ( _u16Image_x_size * _u8Comp_h_samp[component_id] ) + ( max_h_samp - 1 ) ) / max_h_samp ) + 7 ) / 8;
4120         _u16Comp_v_blocks[component_id] = ( ( ( ( _u16Image_y_size * _u8Comp_v_samp[component_id] ) + ( max_v_samp - 1 ) ) / max_v_samp ) + 7 ) / 8;
4121     }
4122 
4123     if ( _u8Comps_in_scan == 1 )
4124     {
4125         _u16Mcus_per_row = _u16Comp_h_blocks[_u8Comp_list[0]];
4126         _u16Mcus_per_col = _u16Comp_v_blocks[_u8Comp_list[0]];
4127     }
4128     else
4129     {
4130         _u16Mcus_per_row = ( ( ( _u16Image_x_size + 7 ) / 8 ) + ( max_h_samp - 1 ) ) / max_h_samp;
4131         _u16Mcus_per_col = ( ( ( _u16Image_y_size + 7 ) / 8 ) + ( max_v_samp - 1 ) ) / max_v_samp;
4132     }
4133 
4134     if ( _u8Comps_in_scan == 1 )
4135     {
4136         _u8Mcu_org[0] = _u8Comp_list[0];
4137 
4138         _u8Blocks_per_mcu = 1;
4139     }
4140     else
4141     {
4142         _u8Blocks_per_mcu = 0;
4143 
4144         for ( component_num = 0; component_num < _u8Comps_in_scan; component_num++ )
4145         {
4146             MS_U8 num_blocks;
4147 
4148             component_id = _u8Comp_list[component_num];
4149 
4150             num_blocks = _u8Comp_h_samp[component_id] * _u8Comp_v_samp[component_id];
4151 
4152             while ( num_blocks-- )
4153             {
4154                 _u8Mcu_org[_u8Blocks_per_mcu++] = component_id;
4155             }
4156         }
4157     }
4158 }
4159 //------------------------------------------------------------------------------
4160 /* Get current access byte address in MRC buffer relative to MRC start address */
JPEG_GetECS(void)4161 JPEG_STATIC MS_U32 JPEG_GetECS(void)
4162 {
4163     MS_U32 data_end_addr = (MS_U32)_pu8In_buf_ofs;
4164     MS_U32 data_start_addr =(MS_U32) _pu8In_buf;
4165 
4166     return (data_end_addr - data_start_addr);
4167 }
4168 //------------------------------------------------------------------------------
4169 //*************************************************
4170 //write symbol table
4171 //*************************************************
4172 #ifndef CMODEL
4173 #if 0
4174 JPEG_STATIC void JPEG_write_symidx(void)
4175 {
4176     MS_U16 i, tbl_num_luma, tbl_num_chroma;
4177     MS_U8 ci, luma_ci = 0, chroma_ci = 0;
4178 
4179     MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_SYMIDX_BASE);
4180     if (_HeadCheck.DHT)
4181     {
4182         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4183         {
4184             if(_u8LumaCi==_u8Comp_ident[ci])
4185             {
4186                 luma_ci = ci;
4187                 break;
4188             }
4189         }
4190 
4191         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4192         {
4193             if(_u8ChromaCi==_u8Comp_ident[ci])
4194             {
4195                 chroma_ci = ci;
4196                 break;
4197             }
4198         }
4199 
4200         tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4201         tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4202 
4203         for ( i = 0; i < 256; i++ )
4204         {
4205             MDrv_Write2Byte( BK_JPD_TID_DAT, ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ) );
4206         }
4207 
4208         tbl_num_luma = _u8Comp_dc_tab[luma_ci];
4209         tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
4210 
4211         for ( i = 0; i < 16; i++ )
4212         {
4213             MDrv_Write2Byte( BK_JPD_TID_DAT, ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ) );
4214         }
4215     }
4216     else
4217     {
4218         for(i=0;i<272;i++)
4219             MDrv_Write2Byte( BK_JPD_TID_DAT, g16SYMIDX_TBL[i]);
4220     }
4221 }
4222 #endif
4223 //------------------------------------------------------------------------------
JPEG_WriteSymidx(void)4224 JPEG_STATIC void JPEG_WriteSymidx(void)
4225 {
4226     //MS_U16 i, tbl_num_luma, tbl_num_chroma;
4227     MS_U16 ac_tbl_num_luma = 0, ac_tbl_num_chroma = 0;
4228     MS_U16 dc_tbl_num_luma = 0, dc_tbl_num_chroma = 0;
4229     MS_U8 ci, luma_ci = 0, chroma_ci = 0;
4230 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4231     MS_U16 ac_tbl_num_chroma2 = 0;
4232     MS_U16 dc_tbl_num_chroma2 = 0;
4233     MS_U8 chroma2_ci = 0;
4234 #endif
4235     JPD_Symidx structSymidx;
4236 
4237     JPEG_memset((void *)(&structSymidx), 0, sizeof(structSymidx));
4238 
4239     // Moved to MDrv_JPD_WriteSymidx()
4240     //MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_SYMIDX_BASE);
4241 
4242     if (_HeadCheck.DHT)
4243     {
4244         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4245         {
4246             if(_u8LumaCi==_u8Comp_ident[ci])
4247             {
4248                 luma_ci = ci;
4249                 break;
4250             }
4251         }
4252 
4253         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4254         {
4255             if(_u8ChromaCi==_u8Comp_ident[ci])
4256             {
4257                 chroma_ci = ci;
4258                 break;
4259             }
4260         }
4261 
4262 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4263         if(TRUE == bIs3HuffTbl)
4264         {
4265             for(ci = 0; ci<_u8Comps_in_frame; ci++)
4266             {
4267                 if(_u8Chroma2Ci==_u8Comp_ident[ci])
4268                 {
4269                     chroma2_ci = ci;
4270                     break;
4271                 }
4272             }
4273         }
4274 #endif
4275 
4276         //tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4277         ac_tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4278         //tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4279         ac_tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4280 
4281 // Moved to MDrv_JPD_WriteSymidx()
4282 #if 0
4283         for ( i = 0; i < 256; i++ )
4284         {
4285             MDrv_Write2Byte( BK_JPD_TID_DAT, ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ) );
4286         }
4287 #endif
4288 
4289         dc_tbl_num_luma = _u8Comp_dc_tab[luma_ci];
4290         dc_tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
4291 #if 0
4292         for ( i = 0; i < 16; i++ )
4293         {
4294             MDrv_Write2Byte( BK_JPD_TID_DAT, ( _Huff_info[tbl_num_chroma].u8Huff_val[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Huff_val[i] ) );
4295         }
4296 #endif
4297 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4298         if(TRUE == bIs3HuffTbl)
4299         {
4300             ac_tbl_num_chroma2 = _u8Comp_ac_tab[chroma2_ci];
4301             dc_tbl_num_chroma2 = _u8Comp_dc_tab[chroma2_ci];
4302         }
4303 #endif
4304     }
4305     else
4306     {
4307 // Moved to MDrv_JPD_WriteSymidx()
4308 #if 0
4309         for(i=0;i<272;i++)
4310             MDrv_Write2Byte( BK_JPD_TID_DAT, g16SYMIDX_TBL[i]);
4311 #endif
4312     }
4313 
4314     structSymidx.DHT = _HeadCheck.DHT;
4315     structSymidx.bUVHuffman = bIs3HuffTbl;
4316     structSymidx.u8DcLumaHuffVal = _Huff_info[dc_tbl_num_luma].u8Huff_val;
4317     structSymidx.u8DcChromaHuffVal = _Huff_info[dc_tbl_num_chroma].u8Huff_val;
4318     structSymidx.u8AcLumaHuffVal = _Huff_info[ac_tbl_num_luma].u8Huff_val;
4319     structSymidx.u8AcChromaHuffVal = _Huff_info[ac_tbl_num_chroma].u8Huff_val;
4320 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4321     if(TRUE == bIs3HuffTbl)
4322     {
4323         structSymidx.u8DcChroma2HuffVal = _Huff_info[dc_tbl_num_chroma2].u8Huff_val;
4324         structSymidx.u8AcChroma2HuffVal = _Huff_info[ac_tbl_num_chroma2].u8Huff_val;
4325     }
4326 #endif
4327     MDrv_JPD_WriteSymidx(structSymidx);
4328 }
4329 //------------------------------------------------------------------------------
4330 //***************************************************
4331 //write quantization table
4332 //***************************************************
4333 #if 0
4334 JPEG_STATIC void JPEG_write_Qtbl(void)
4335 {
4336     MS_U8 i, j;
4337     MS_U8 com_num = 0;
4338     MS_U8 comp[JPEG_MAXCOMPONENTS];
4339 
4340     MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_QTBL_BASE);
4341 
4342     if (_HeadCheck.DQT)
4343     {
4344         // Calculate how many valid quantization tables
4345         JPEG_memset((void *)comp, 0, JPEG_MAXCOMPONENTS);
4346         for(i = 0; i<_u8Comps_in_frame; i++)
4347         {
4348             comp[_u8Comp_quant[i]] = 1;
4349         }
4350 
4351         for(i = 0; i<JPEG_MAXCOMPONENTS; i++)
4352         {
4353             if(comp[i]==1)
4354                 com_num++;
4355         }
4356 
4357         for ( i = 0; i < com_num; i++ )
4358         {
4359             for(j = 0; j<64; j++)
4360             {
4361                 MDrv_Write2Byte(BK_JPD_TID_DAT, _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
4362             }
4363         }
4364 
4365         // if all compoents refer to the same Qtable, need to write Qtable twice
4366         if(com_num==1)
4367         {
4368             for ( i = 0; i < com_num; i++ )
4369             {
4370                 for(j = 0; j<64; j++)
4371                 {
4372                     MDrv_Write2Byte(BK_JPD_TID_DAT, _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
4373                 }
4374             }
4375         }
4376     }
4377     else
4378     {
4379         for (i=0; i<128; i++)
4380             MDrv_Write2Byte(BK_JPD_TID_DAT, g16IQ_TBL[i]);
4381     }
4382 }
4383 #endif
JPEG_WriteIQTbl(void)4384 JPEG_STATIC void JPEG_WriteIQTbl(void)
4385 {
4386     //MS_U8 i, j;
4387     MS_U8 i;
4388     MS_U8 com_num = 0;
4389     MS_U8 comp[JPEG_MAXCOMPONENTS];
4390     JPD_IQTbl structIqtbl;
4391 
4392     JPEG_memset((void *)&structIqtbl, 0, sizeof(structIqtbl));
4393 
4394     // Moved to MDrv_JPD_WriteIQTbl()
4395     //MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_QTBL_BASE);
4396 
4397     if (_HeadCheck.DQT)
4398     {
4399         // Calculate how many valid quantization tables
4400         JPEG_memset((void *)comp, 0, JPEG_MAXCOMPONENTS);
4401         for(i = 0; i<_u8Comps_in_frame; i++)
4402         {
4403             comp[_u8Comp_quant[i]] = 1;
4404         }
4405 
4406         for(i = 0; i<JPEG_MAXCOMPONENTS; i++)
4407         {
4408             if(comp[i]==1)
4409                 com_num++;
4410         }
4411 
4412         // Moved to MDrv_JPD_WriteIQTbl
4413 #if 0
4414         for ( i = 0; i < com_num; i++ )
4415         {
4416             for(j = 0; j<64; j++)
4417             {
4418                 MDrv_Write2Byte(BK_JPD_TID_DAT, _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
4419             }
4420         }
4421 
4422         // if all compoents refer to the same Qtable, need to write Qtable twice
4423         if(com_num==1)
4424         {
4425             for ( i = 0; i < com_num; i++ )
4426             {
4427                 for(j = 0; j<64; j++)
4428                 {
4429                     MDrv_Write2Byte(BK_JPD_TID_DAT, _QuantTables[_u8Comp_quant[i]].s16Value[_u8Jpeg_zigzag_order[j]]);
4430                 }
4431             }
4432         }
4433 #endif
4434     }
4435     else
4436     {
4437         // Moved to MDrv_JPD_WriteIQTbl()
4438 #if 0
4439         for (i=0; i<128; i++)
4440             MDrv_Write2Byte(BK_JPD_TID_DAT, g16IQ_TBL[i]);
4441 #endif
4442     }
4443 
4444     structIqtbl.DQT = _HeadCheck.DQT;
4445     structIqtbl.u8CompNum = com_num;
4446     structIqtbl.u8CompQuant = _u8Comp_quant;
4447     structIqtbl.QuantTables = (JPD_QuanTbl *)_QuantTables;
4448     MDrv_JPD_WriteIQTbl(structIqtbl);
4449 }
4450 //------------------------------------------------------------------------------
4451 //*************************************************
4452 //write group information
4453 //*************************************************
4454 #if 0
4455 JPEG_STATIC void JPEG_write_Scwgif(void) //type : luma=>0  chroma=>1
4456 {
4457     MS_U32 reg_value;
4458     MS_U16 i, ci, valid, tbl_num_luma, tbl_num_chroma;
4459     MS_U8 luma_ci = 0, chroma_ci = 0;
4460 
4461     MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_SCWGIF_BASE);
4462     if (_HeadCheck.DHT)
4463     {
4464         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4465         {
4466             if(_u8LumaCi==_u8Comp_ident[ci])
4467             {
4468                 luma_ci = ci;
4469                 break;
4470             }
4471         }
4472 
4473         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4474         {
4475             if(_u8ChromaCi==_u8Comp_ident[ci])
4476             {
4477                 chroma_ci = ci;
4478                 break;
4479             }
4480         }
4481 
4482         tbl_num_luma = _u8Comp_dc_tab[luma_ci];
4483         tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
4484 
4485         for ( i = 1; i <= 16; i++ )
4486         {
4487             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
4488                 valid = 0;
4489             else
4490                 valid = 1;
4491 
4492             if ( valid )
4493             {
4494                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] << 4 );
4495             }
4496             else
4497             {
4498                 reg_value = 0;
4499             }
4500 
4501             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4502             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4503         }
4504 
4505         for ( i = 1; i <= 16; i++ )
4506         {
4507             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
4508                 valid = 0;
4509             else
4510                 valid = 1;
4511 
4512             if ( valid )
4513             {
4514                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] << 4 );
4515             }
4516             else
4517             {
4518                 reg_value = 0;
4519             }
4520 
4521             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4522             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4523         }
4524 
4525         tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4526         tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
4527 
4528         for ( i = 1; i <= 16; i++ )
4529         {
4530             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
4531                 valid = 0;
4532             else
4533                 valid = 1;
4534 
4535             if ( valid )
4536             {
4537                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] );
4538             }
4539             else
4540             {
4541                 reg_value = 0;
4542             }
4543 
4544             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4545             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4546         }
4547 
4548         for ( i = 1; i <= 16; i++ )
4549         {
4550             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
4551                 valid = 0;
4552             else
4553                 valid = 1;
4554 
4555             if ( valid )
4556             {
4557                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] );
4558             }
4559             else
4560             {
4561                 reg_value = 0;
4562             }
4563 
4564             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4565             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4566         }
4567     }
4568     else
4569     {
4570         for( i = 0; i < 128; i++ )
4571             MDrv_Write2Byte(BK_JPD_TID_DAT, g16GRPINFO_TBL[i]);
4572     }
4573 }
4574 #endif
JPEG_WriteGrpinf(void)4575 JPEG_STATIC void JPEG_WriteGrpinf(void) //type : luma=>0  chroma=>1
4576 {
4577     //MS_U32 reg_value;
4578     //MS_U16 i, ci, valid, tbl_num_luma, tbl_num_chroma;
4579     MS_U16 ci, dc_tbl_num_luma = 0, dc_tbl_num_chroma = 0;
4580     MS_U16 ac_tbl_num_luma = 0, ac_tbl_num_chroma = 0;
4581     MS_U8 luma_ci = 0, chroma_ci = 0;
4582 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4583     MS_U16 dc_tbl_num_chroma2 = 0;
4584     MS_U16 ac_tbl_num_chroma2 = 0;
4585     MS_U8 chroma2_ci = 0;
4586 #endif
4587     JPD_Grpinf structGrpinf;
4588 
4589     JPEG_memset((void *)&structGrpinf, 0, sizeof(structGrpinf));
4590 
4591 // Moved to MDrv_JPD_WriteGrpinf()
4592 #if 0
4593     //MDrv_Write2Byte(BK_JPD_TID_ADR, JPD_MEM_SCWGIF_BASE);
4594 #endif
4595     if (_HeadCheck.DHT)
4596     {
4597         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4598         {
4599             if(_u8LumaCi==_u8Comp_ident[ci])
4600             {
4601                 luma_ci = ci;
4602                 break;
4603             }
4604         }
4605 
4606         for(ci = 0; ci<_u8Comps_in_frame; ci++)
4607         {
4608             if(_u8ChromaCi==_u8Comp_ident[ci])
4609             {
4610                 chroma_ci = ci;
4611                 break;
4612             }
4613         }
4614 
4615 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4616         if(TRUE == bIs3HuffTbl)
4617         {
4618             for(ci = 0; ci<_u8Comps_in_frame; ci++)
4619             {
4620                 if(_u8Chroma2Ci==_u8Comp_ident[ci])
4621                 {
4622                     chroma2_ci = ci;
4623                     break;
4624                 }
4625             }
4626         }
4627 #endif
4628 
4629         dc_tbl_num_luma = _u8Comp_dc_tab[luma_ci];
4630         dc_tbl_num_chroma = _u8Comp_dc_tab[chroma_ci];
4631 
4632 // Moved to MDrv_JPD_WriteGrpinf()
4633 #if 0
4634         for ( i = 1; i <= 16; i++ )
4635         {
4636             if(_Huff_info[tbl_num_luma].u8Symbol[i] == 0xFF)
4637                 valid = 0;
4638             else
4639                 valid = 1;
4640 
4641             if ( valid )
4642             {
4643                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_luma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_luma].u8Symbol[i] << 4 );
4644             }
4645             else
4646             {
4647                 reg_value = 0;
4648             }
4649 
4650             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4651             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4652         }
4653 
4654         for ( i = 1; i <= 16; i++ )
4655         {
4656             if(_Huff_info[tbl_num_chroma].u8Symbol[i] == 0xFF)
4657                 valid = 0;
4658             else
4659                 valid = 1;
4660 
4661             if ( valid )
4662             {
4663                 reg_value = ( valid << 24 ) | ( _Huff_info[tbl_num_chroma].u16Code[i] << 8 ) | ( _Huff_info[tbl_num_chroma].u8Symbol[i] << 4 );
4664             }
4665             else
4666             {
4667                 reg_value = 0;
4668             }
4669 
4670             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value & 0xffff);
4671             MDrv_Write2Byte(BK_JPD_TID_DAT, reg_value >> 16);
4672         }
4673 #endif
4674 
4675         ac_tbl_num_luma = _u8Comp_ac_tab[luma_ci];
4676         ac_tbl_num_chroma = _u8Comp_ac_tab[chroma_ci];
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] );
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] );
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 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4720         if(TRUE == bIs3HuffTbl)
4721         {
4722             dc_tbl_num_chroma2 = _u8Comp_dc_tab[chroma2_ci];
4723             ac_tbl_num_chroma2 = _u8Comp_ac_tab[chroma2_ci];
4724         }
4725 #endif
4726     }
4727     else
4728     {
4729 #if 0
4730         for( i = 0; i < 128; i++ )
4731             MDrv_Write2Byte(BK_JPD_TID_DAT, g16GRPINFO_TBL[i]);
4732 #endif
4733     }
4734 
4735     structGrpinf.DHT = _HeadCheck.DHT;
4736     structGrpinf.bUVHuffman = bIs3HuffTbl;
4737     structGrpinf.u8DcLumaSymbol = _Huff_info[dc_tbl_num_luma].u8Symbol;
4738     structGrpinf.u16DcLumaCode = _Huff_info[dc_tbl_num_luma].u16Code;
4739     structGrpinf.u8DcChromaSymbol = _Huff_info[dc_tbl_num_chroma].u8Symbol;
4740     structGrpinf.u16DcChromaCode = _Huff_info[dc_tbl_num_chroma].u16Code;
4741     structGrpinf.u8AcLumaSymbol = _Huff_info[ac_tbl_num_luma].u8Symbol;
4742     structGrpinf.u16AcLumaCode = _Huff_info[ac_tbl_num_luma].u16Code;
4743     structGrpinf.u8AcChromaSymbol = _Huff_info[ac_tbl_num_chroma].u8Symbol;
4744     structGrpinf.u16AcChromaCode = _Huff_info[ac_tbl_num_chroma].u16Code;
4745 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
4746     if(TRUE == bIs3HuffTbl)
4747     {
4748         structGrpinf.u8DcChroma2Symbol = _Huff_info[dc_tbl_num_chroma2].u8Symbol;
4749         structGrpinf.u16DcChroma2Code = _Huff_info[dc_tbl_num_chroma2].u16Code;
4750         structGrpinf.u8AcChroma2Symbol = _Huff_info[ac_tbl_num_chroma2].u8Symbol;
4751         structGrpinf.u16AcChroma2Code = _Huff_info[ac_tbl_num_chroma2].u16Code;
4752     }
4753 #endif
4754     MDrv_JPD_WriteGrpinf(structGrpinf);
4755 }
4756 #endif /* #ifndef CMODEL */
4757 //------------------------------------------------------------------------------
4758 // Write RLE result
JPEG_write_RLE(JPEG_SVLD * pVld,MS_BOOL bDecodeNow)4759 JPEG_STATIC MS_BOOL JPEG_write_RLE(JPEG_SVLD *pVld, MS_BOOL bDecodeNow)
4760 {
4761 #ifdef CMODEL
4762     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);
4763 #else
4764     MS_U8 *mrc_buffer = (MS_U8 *) MRC_BUFFER_ADDR;
4765     MS_U16 status;
4766     MS_U32 start_time;
4767     MS_U16 cur_vIdx;
4768 
4769     JPEG_memcpy((void *)(mrc_buffer + _u32RLEOffset), (void *)pVld, 4);
4770     _u32RLEOffset += 4;
4771 
4772     // Check if buffer full
4773     if((MRC_BUFFER_SIZE == _u32RLEOffset)
4774     || (TRUE == bDecodeNow))
4775     {
4776         JPEG_DEBUG_API_MSG("Do RLE, LENG 0x%lx, bDecodeNow = %d\n", _u32RLEOffset, bDecodeNow);
4777         JPEG_DEBUG_API_MSG("CPU Sync and Flush Memory~~~~\n");
4778         MAsm_CPU_Sync();
4779         MsOS_FlushMemory();
4780 
4781         if(_bFirstRLE == TRUE)
4782         {
4783             // Trigger JPD decoding
4784             if(!JPEG_StartDecode())
4785                 return FALSE;
4786             _bFirstRLE = FALSE;
4787         }
4788         else
4789         {
4790             // clear MRC low/high portion read complete event
4791             //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBH_DONE | E_JPD_EVENT_MRBL_DONE);
4792             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE | E_JPD_EVENT_MRBL_DONE);
4793             // mark low/high buffer valid
4794             //MDrv_Write2Byte( BK_JPD_MCONFIG, (MDrv_Read2Byte(BK_JPD_MCONFIG) & ~0x0003) | (JPD_H_VLD |JPD_L_VLD));
4795             MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | (JPD_H_VLD |JPD_L_VLD));
4796         }
4797 
4798 #if MSOS_GET_SYSTEM_TIME
4799         start_time = MsOS_GetSystemTime();
4800 #else
4801         start_time = 0;
4802 #endif
4803         //cur_vIdx = MDrv_Read2Byte(BK_JPD_CUR_VIDX);
4804         cur_vIdx = MDrv_JPD_GetCurVidx();
4805 
4806         JPEG_DEBUG_API_MSG("cur_vIdx = 0x%04X\n",cur_vIdx);
4807 
4808         if( bDecodeNow )
4809         {
4810             return TRUE;  //wait done in main loop
4811         }
4812 
4813         while(1)
4814         {
4815             //status = MDrv_JPD_ReadJPDStatus();
4816             status = MDrv_JPD_GetEventFlag();
4817 
4818             if(status & E_JPD_EVENT_DEC_DONE)
4819             {
4820                 JPEG_DEBUG_API_MSG("P deocde done\n");
4821                 break;
4822             }
4823 
4824             if((status & E_JPD_EVENT_ECS_ERROR) || (status & E_JPD_EVENT_IS_ERROR) || (status & E_JPD_EVENT_RST_ERROR)
4825 #if (JPD_SUPPORT_AUTO_PROTECT==true)
4826                 || (status & E_JPD_EVENT_MWB_FULL)
4827 #endif
4828                 )
4829             {
4830                 // temp patch for protect JPD from writing to illegal memory
4831                 JPEG_DEBUG_API_MSG("CurVidx = %d, CurRow = %d, CurCol = %d ",
4832                         MDrv_JPD_GetCurVidx(),
4833                         MDrv_JPD_GetCurRow(),
4834                         MDrv_JPD_GetCurCol());
4835                 JPEG_DEBUG_API_MSG("CurMRCAddr = 0x%lx\n", MDrv_JPD_GetCurMRCAddr());
4836                 //MDrv_JPD_SW_Pause_Reset();
4837                 MDrv_JPD_Rst();
4838 
4839                 JPEG_terminate( E_JPEG_JPD_DECODE_ERROR );
4840                 return FALSE;
4841             }
4842 
4843             if((status & E_JPD_EVENT_MRBH_DONE) && (status & E_JPD_EVENT_MRBL_DONE))
4844             {
4845                 JPEG_DEBUG_API_MSG("Partial SVLD decode done\n");
4846                 break;
4847             }
4848 
4849             // Check the V index. If it is not changed withing 500ms, it means that the JPD has some problem.
4850             // We need to break the infinite loop
4851             //if(cur_vIdx!=MDrv_Read2Byte(BK_JPD_CUR_VIDX))
4852             if(cur_vIdx != MDrv_JPD_GetCurVidx())
4853             {
4854 #if MSOS_GET_SYSTEM_TIME
4855                 start_time = MsOS_GetSystemTime();
4856 #else
4857                 start_time = 0;
4858 #endif
4859                 //cur_vIdx = MDrv_Read2Byte(BK_JPD_CUR_VIDX);
4860                 cur_vIdx = MDrv_JPD_GetCurVidx();
4861             }
4862             else
4863             {
4864 #if MSOS_GET_SYSTEM_TIME
4865                 if((MsOS_GetSystemTime() - start_time) >= DEFAULT_DECODE_TIMEOUT)
4866 #else
4867                 if(start_time++ >= DEFAULT_DECODE_TIMEOUT * 100)
4868 #endif
4869                 {
4870                     JPEG_DEBUG_API_ERR("ERROR: SVLD deocde time out, VIdx %d\n", cur_vIdx);
4871                     return FALSE;
4872                 }
4873             }
4874         }
4875 
4876         _u32RLEOffset = 0;
4877     }
4878 #endif
4879     return TRUE;
4880 }
4881 //------------------------------------------------------------------------------
4882 // Do run length encode of coefficient buffer
4883 //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)4884 JPEG_STATIC MS_BOOL JPEG_do_RLE(JPEG_BLOCK_TYPE *p, MS_BOOL eop, MS_U8 comp_id, MS_BOOL BlockInRange)
4885 {
4886     JPEG_SVLD my_vld;
4887     MS_U8 counter;
4888     MS_S16 value;
4889     MS_U16 run;
4890     MS_U8 cur_blk;
4891     JPEG_BLOCK_TYPE predictor;
4892 
4893     if(comp_id==0)
4894         cur_blk = 1;     // Y
4895     else if(comp_id==1)
4896         cur_blk = 3;    // U
4897     else
4898         cur_blk = 2;    // V
4899 
4900     predictor = _s16dc_pred[cur_blk - 1];
4901 
4902     run = 0;
4903     my_vld.byte0 = my_vld.byte1 = my_vld.byte2 = my_vld.byte3 = 0;
4904     my_vld.blk_type = cur_blk;
4905 
4906     //sent DC info
4907 ////    my_vld.run = 8;
4908     if( BlockInRange )//Current block is within display range.
4909         my_vld.run = 8;
4910     else
4911         my_vld.run = 0;
4912 
4913     value = (p[0] - predictor);
4914     my_vld.sign = (value<0)?1:0;
4915     my_vld.amp = JPEG_ABS(value);
4916     my_vld.sym_type = E_RLE_DC;
4917     if(!JPEG_write_RLE(&my_vld, FALSE))
4918         return FALSE;
4919 
4920     if( BlockInRange == FALSE )//Current block is not within display range.
4921         return TRUE;
4922 
4923     my_vld.byte0 = my_vld.byte1 =my_vld.byte2 = my_vld.byte3= 0;
4924     my_vld.blk_type = cur_blk;
4925 
4926     for(counter = 1;counter<64; counter++)
4927     {
4928         if(p[counter]==0)
4929         {
4930             run++;
4931         }
4932         else
4933         {
4934             while(run>15)
4935             {
4936                 my_vld.sign = 0;
4937                 my_vld.amp = 0;
4938                 my_vld.sym_type = E_RLE_ZRL;
4939                 my_vld.run = 15;
4940                 if(!JPEG_write_RLE(&my_vld, FALSE))
4941                     return FALSE;
4942                 my_vld.byte0 = my_vld.byte1 = my_vld.byte2 = my_vld.byte3 = 0;
4943                 my_vld.blk_type = cur_blk;
4944                 run -= 16;
4945             }
4946 
4947             my_vld.sign = (p[counter]<0)?1:0;
4948             my_vld.amp = JPEG_ABS(p[counter]);
4949             my_vld.sym_type = E_RLE_AC;
4950             my_vld.run = run;
4951 
4952             // Check if the last byte is non-zero. If it's non-zero & EOP, add the EOP flag
4953             if(counter==63&&eop&&p[63]!=0)
4954             {
4955                 my_vld.EOP = 1;
4956                 if(!JPEG_write_RLE(&my_vld, TRUE))
4957                     return FALSE;
4958 
4959                 _s16dc_pred[cur_blk - 1] = p[0];//update predictor
4960                 return TRUE;
4961             }
4962             else
4963             {
4964                 if(!JPEG_write_RLE(&my_vld, FALSE))
4965                     return FALSE;
4966             }
4967 
4968             my_vld.byte0 = my_vld.byte1 = my_vld.byte2 = my_vld.byte3 = 0;
4969             my_vld.blk_type = cur_blk;
4970             run = 0;
4971         }
4972     }
4973 
4974     counter = 63;
4975 
4976     if(p[counter]==0)
4977     {
4978         my_vld.amp = JPEG_ABS(p[counter]);
4979         my_vld.sign = p[counter]<0?1:0;
4980         my_vld.sym_type = E_RLE_EOB;
4981         my_vld.run = 0;
4982         if(eop)
4983         {
4984             my_vld.EOP = 1;
4985             if(!JPEG_write_RLE(&my_vld, TRUE))
4986                 return FALSE;
4987         }
4988         else
4989         {
4990             if(!JPEG_write_RLE(&my_vld, FALSE))
4991                 return FALSE;
4992         }
4993     }
4994 
4995     _s16dc_pred[cur_blk - 1] = p[0];//update predictor
4996     return TRUE;
4997 }
4998 //------------------------------------------------------------------------------
4999 // Starts a new scan.
JPEG_init_scan(void)5000 JPEG_STATIC MS_BOOL JPEG_init_scan(void)
5001 {
5002     if (!JPEG_locate_sos_marker())
5003         return FALSE;
5004 
5005     JPEG_calc_mcu_block_order();
5006 
5007     if (_HeadCheck.DHT)
5008     {
5009         if(!JPEG_check_huff_tables())
5010             return FALSE;
5011     }
5012 
5013     if (_HeadCheck.DQT)
5014     {
5015         if(!JPEG_check_quant_tables())
5016             return FALSE;
5017     }
5018 
5019 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
5020     JPEG_memset((void *)_u32Last_dc_val, 0, _u8Comps_in_frame * sizeof( MS_U32 ) );
5021 
5022     _u32EOB_run = 0;
5023 #endif
5024 
5025     if ( _u16Restart_interval )
5026     {
5027         _u16Restarts_left = _u16Restart_interval;
5028         _u16Next_restart_num = 0;
5029     }
5030 
5031 ////    _Total_Decoded_Size = (MS_S32)JPEG_GetECS();
5032 
5033 #ifdef CMODEL
5034     {
5035         // pre-fill bit buffer for later decoding
5036         _s16Bits_left = 16;
5037         JPEG_get_bits_2( 16 );
5038         JPEG_get_bits_2( 16 );
5039     }
5040 #else
5041     {
5042 #if SW_JPD_RGB_CMYK
5043         if(_bProgressive_flag
5044         || (E_JPEG_CMYK == gu8Scan_type)
5045         || (E_JPEG_RGB == gu8Scan_type))
5046 #else
5047         if(_bProgressive_flag)
5048 #endif
5049         {
5050             // pre-fill bit buffer for later decoding
5051             _s16Bits_left = 16;
5052             JPEG_get_bits_2( 16 );
5053             JPEG_get_bits_2( 16 );
5054         }
5055     }
5056 #endif
5057 
5058     JPEG_DEBUG_API_MSG("JPEG_init_scan:ECS 0x%08lx\n", JPEG_GetECS());
5059 
5060     return TRUE;
5061 }
5062 
5063 #if SW_JPD_RGB_CMYK
5064 //------------------------------------------------------------------------------
5065 // Create a few tables that allow us to quickly convert YCbCr to RGB.
msAPI_JPEG_create_look_ups(void)5066 JPEG_STATIC void msAPI_JPEG_create_look_ups( void )
5067 {
5068     MS_S16 i, k;
5069     //kevinhuang, modify
5070     /*
5071     for (i = 0; i <= 255; i++)
5072     {
5073       //k = (i * 2) - 255;
5074         k = (i * 2) - 256; // Dec. 28 2001- change so table[128] == 0
5075       gs32Crr[i] = ( FIX(1.40200/2)  * k + ONE_HALF) >> SCALEBITS;
5076       gs32Cbb[i] = ( FIX(1.77200/2)  * k + ONE_HALF) >> SCALEBITS;
5077       gs32Crg[i] = (-FIX(0.71414/2)) * k;
5078       gs32Cbg[i] = (-FIX(0.34414/2)) * k + ONE_HALF;
5079     }
5080     */
5081     for ( i = 0; i <= 255; i++ )
5082     {
5083         k = i - 128;
5084 
5085         gs32Crr[i] = ( FIX( 1.40200 ) * k + ONE_HALF ) >> SCALEBITS;
5086         gs32Cbb[i] = ( FIX( 1.77200 ) * k + ONE_HALF ) >> SCALEBITS;
5087 
5088         gs32Crg[i] = ( -FIX( 0.71414 ) ) * k ; //+ ONE_HALF) >> SCALEBITS;???
5089         gs32Cbg[i] = ( -FIX( 0.34414 ) ) * k + ONE_HALF; //>> SCALEBITS;???
5090 
5091     }
5092 }
5093 
5094 #endif //SW_JPD_RGB_CMYK
5095 //------------------------------------------------------------------------------
5096 // Starts a frame. Determines if the number of components or sampling factors
5097 // are supported.
JPEG_init_frame(void)5098 JPEG_STATIC MS_BOOL JPEG_init_frame(void) //ok
5099 {
5100 #if SW_JPD_RGB_CMYK
5101     MS_U32 i;
5102     MS_U8 *q;
5103 #endif
5104 
5105     JPEG_DEBUG_API_MSG("JPEG_init_frame:\n");
5106     if ( _u8Comps_in_frame == 1 )
5107     {
5108         gu8Scan_type = E_JPEG_GRAYSCALE;
5109 
5110         _u16Max_blocks_per_mcu = 1;
5111 
5112         gu8Max_mcu_x_size = 8;
5113         gu8Max_mcu_y_size = 8;
5114     }
5115     else if ( _u8Comps_in_frame == 3 )
5116     {
5117         if ( ( ( _u8Comp_h_samp[1] != 1 ) || ( _u8Comp_v_samp[1] != 1 ) ) ||   //support only U_H1V1 & V_H1V1
5118             ( ( _u8Comp_h_samp[2] != 1 ) || ( _u8Comp_v_samp[2] != 1 ) ) )
5119         {
5120             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
5121             return FALSE;
5122         }
5123 
5124         if ( ( _u8Comp_h_samp[0] == 1 ) && ( _u8Comp_v_samp[0] == 1 ) )
5125         {
5126             //set RGB based jpeg flag
5127             if(_u8Comp_ident[0] == 82 || _u8Comp_ident[0] == 71 || _u8Comp_ident[0] == 66)
5128             {
5129 #if SW_JPD_RGB_CMYK
5130                 if( FALSE == bEnableRGB )
5131                 {
5132                     JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5133                     return FALSE;
5134                 }
5135                 JPEG_DEBUG_API_MSG("Get JPEG_RGB\n");
5136                 gu8Scan_type = E_JPEG_RGB; //RGB
5137 #else
5138                 JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5139                 return FALSE;
5140 #endif
5141             }
5142             else
5143             {
5144                 JPEG_DEBUG_API_MSG("Get JPEG_YH1V1\n");
5145                 gu8Scan_type = E_JPEG_YH1V1; //4:4:4
5146             }
5147 
5148             _u16Max_blocks_per_mcu = 3;
5149 
5150             gu8Max_mcu_x_size = 8;
5151             gu8Max_mcu_y_size = 8;
5152         }
5153         else if ( ( _u8Comp_h_samp[0] == 2 ) && ( _u8Comp_v_samp[0] == 1 ) )
5154         {
5155             gu8Scan_type = E_JPEG_YH2V1; //4:2:2
5156 
5157             _u16Max_blocks_per_mcu = 4;
5158 
5159             gu8Max_mcu_x_size = 16;
5160             gu8Max_mcu_y_size = 8;
5161         }
5162         else if ( ( _u8Comp_h_samp[0] == 1 ) && ( _u8Comp_v_samp[0] == 2 ) )
5163         {
5164             gu8Scan_type = E_JPEG_YH1V2;
5165 
5166             _u16Max_blocks_per_mcu = 4;
5167 
5168             gu8Max_mcu_x_size = 8;
5169             gu8Max_mcu_y_size = 16;
5170         }
5171         else if ( ( _u8Comp_h_samp[0] == 2 ) && ( _u8Comp_v_samp[0] == 2 ) )
5172         {
5173             gu8Scan_type = E_JPEG_YH2V2; //4:2:0
5174 
5175             _u16Max_blocks_per_mcu = 6;
5176 
5177             gu8Max_mcu_x_size = 16;
5178             gu8Max_mcu_y_size = 16;
5179         }
5180         else if ( ( _u8Comp_h_samp[0] == 4 ) && ( _u8Comp_v_samp[0] == 1 ) )
5181         {
5182             // 4:1:1
5183             gu8Scan_type = E_JPEG_YH4V1;
5184 
5185             _u16Max_blocks_per_mcu = 6;
5186 
5187             gu8Max_mcu_x_size = 32;
5188             gu8Max_mcu_y_size = 8;
5189             //#if CMODEL
5190             //JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
5191             //#endif
5192         }
5193         else
5194         {
5195             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
5196             return FALSE;
5197         }
5198     }
5199 #if SW_JPD_RGB_CMYK
5200     else if(_u8Comps_in_frame == 4) //handle YCCK & CMYK case, must distinguish YCCK and CMYK later
5201     {
5202         if( FALSE == bEnableCMYK )
5203         {
5204             JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5205             return FALSE;
5206         }
5207 
5208         if ( ( _u8Comp_h_samp[0] == 1 ) && ( _u8Comp_v_samp[0] == 1 ) )
5209         {
5210             JPEG_DEBUG_API_MSG("Get JPEG_CMYK\n");
5211             gu8Scan_type = E_JPEG_CMYK;
5212 
5213             _u16Max_blocks_per_mcu = 4;
5214 
5215             gu8Max_mcu_x_size = 8;
5216             gu8Max_mcu_y_size = 8;
5217         }
5218         else
5219         {
5220             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
5221             return FALSE;
5222         }
5223     }
5224 #endif
5225     else
5226     {
5227         JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5228         return FALSE;
5229     }
5230 
5231     JPEG_DEBUG_API_MSG("JPEG_init_frame:gu8Scan_type = %d\n", gu8Scan_type);
5232 
5233     gu16Max_mcus_per_row = ( _u16Image_x_size + ( gu8Max_mcu_x_size - 1 ) ) / gu8Max_mcu_x_size;
5234     _u16Max_mcus_per_col = ( _u16Image_y_size + ( gu8Max_mcu_y_size - 1 ) ) / gu8Max_mcu_y_size;
5235 
5236 #ifdef CMODEL
5237     /* these values are for the *destination* pixels: after conversion */
5238 
5239     if ( E_JPEG_GRAYSCALE == gu8Scan_type )
5240     {
5241         gu8Dest_bytes_per_pixel = 1;
5242     }
5243     else
5244         //kevinhuang, ToDo 4 -> 3 later
5245     {
5246         gu8Dest_bytes_per_pixel = 4;
5247     }
5248 
5249     gu16Dest_bytes_per_scan_line = ( ( _u16Image_x_size + 15 ) & 0xFFF0 ) * gu8Dest_bytes_per_pixel;
5250     gu16Real_dest_bytes_per_scan_line = ( _u16Image_x_size * gu8Dest_bytes_per_pixel );
5251 
5252     // Initialize two scan line buffers.
5253     // FIXME: Only the V2 sampling factors need two buffers.
5254     #define DC_ALIGNMENT    16
5255     //pgu8Scan_line_0         = (MS_U8 *)JPEG_alloc(gu16Dest_bytes_per_scan_line + 8);
5256     pgu8Scan_line_0 = ( MS_U8 * )JPEG_alloc( gu16Dest_bytes_per_scan_line + DC_ALIGNMENT * 4 );
5257     if(pgu8Scan_line_0 == NULL)
5258         return FALSE;
5259     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
5260 
5261     //pgu8scan_line_1         = (MS_U8 *)JPEG_alloc(gu16Dest_bytes_per_scan_line + 8);
5262     pgu8scan_line_1 = ( MS_U8 * )JPEG_alloc( gu16Dest_bytes_per_scan_line + DC_ALIGNMENT * 4 );
5263     if(pgu8scan_line_1 == NULL)
5264         return FALSE;
5265     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
5266 #endif
5267 #if SW_JPD_RGB_CMYK
5268     if((E_JPEG_CMYK == gu8Scan_type)
5269     || (E_JPEG_RGB == gu8Scan_type))
5270     {
5271         /* these values are for the *destination* pixels: after conversion */
5272 
5273         //Reset image x size by new Max_mcus
5274         _u16Image_x_size = gu16Max_mcus_per_row*gu8Max_mcu_x_size;
5275 
5276         _u16NonAlignmentImage_x_size = _u16Image_x_size;
5277 
5278         if ( E_JPEG_GRAYSCALE == gu8Scan_type )
5279         {
5280             _u8Dest_bytes_per_pixel = 1;
5281         }
5282         else
5283             //kevinhuang, ToDo 4 -> 3 later
5284         {
5285             _u8Dest_bytes_per_pixel = 4;
5286         }
5287 
5288         _u16Dest_bytes_per_scan_line = ( ( _u16Image_x_size + 15 ) & 0xFFF0 ) * _u8Dest_bytes_per_pixel;
5289 //        _u16Real_dest_bytes_per_scan_line = ( _u16Image_x_size * _u8Dest_bytes_per_pixel );
5290 
5291         // Initialize two scan line buffers.
5292         // FIXME: Only the V2 sampling factors need two buffers.
5293         #define DC_ALIGNMENT    16
5294         //pgu8Scan_line_0         = (U8 *)alloc(_u16Dest_bytes_per_scan_line + 8);
5295         pgu8Scan_line_0 = ( MS_U8 * )JPEG_alloc( _u16Dest_bytes_per_scan_line + DC_ALIGNMENT * 4 );
5296         if(pgu8Scan_line_0 == NULL)
5297             return FALSE;
5298         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
5299 
5300         //pgu8scan_line_1         = (U8 *)alloc(_u16Dest_bytes_per_scan_line + 8);
5301         pgu8scan_line_1 = ( MS_U8 * )JPEG_alloc( _u16Dest_bytes_per_scan_line + DC_ALIGNMENT * 4 );
5302         if(pgu8scan_line_1 == NULL)
5303             return FALSE;
5304         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
5305     }
5306 #endif
5307 
5308     _u32Max_blocks_per_row = gu16Max_mcus_per_row * _u16Max_blocks_per_mcu;
5309 
5310     // Should never happen
5311     if ( _u32Max_blocks_per_row > JPEG_MAXBLOCKSPERROW )
5312     {
5313         JPEG_terminate( E_JPEG_ASSERTION_ERROR );
5314         return FALSE;
5315     }
5316 
5317 #ifdef CMODEL
5318     {
5319         MS_U32 i;
5320         MS_U8 *q;
5321 
5322         // Allocate the coefficient buffer, enough for one row's worth of MCU's
5323         q = ( MS_U8 * )JPEG_alloc( _u32Max_blocks_per_row * 64 * sizeof( JPEG_BLOCK_TYPE ) + 8 );
5324         if(q == NULL)
5325             return FALSE;
5326 
5327         // Align to 8-byte boundry, for MMX code
5328         q = ( MS_U8 * )( ( ( MS_U32 )q + 7 ) & ~7 );
5329         if(q == NULL)
5330             return FALSE;
5331 
5332         // The gps16Block_seg[] array's name dates back to the
5333         // 16-bit assembler implementation. "seg" stood for "segment".
5334         for ( i = 0; i < _u32Max_blocks_per_row; i++ )
5335         {
5336             gps16Block_seg[i] = ( JPEG_BLOCK_TYPE * )( q + i * 64 * sizeof( JPEG_BLOCK_TYPE ) );
5337         }
5338 
5339         for ( i = 0; i < _u32Max_blocks_per_row; i++ )
5340         {
5341             gu8Block_max_zag_set[i] = 64;
5342         }
5343 
5344         gpu8Sample_buf = ( MS_U8 * )( ( ( MS_U32 )JPEG_alloc( _u32Max_blocks_per_row * 64 + 8 ) + 7 ) & ~7 );
5345         if(gpu8Sample_buf == NULL)
5346             return FALSE;
5347 
5348         JPEG_CMODEL_create_look_ups();
5349     }
5350 #endif
5351 #if SW_JPD_RGB_CMYK
5352     if((E_JPEG_CMYK == gu8Scan_type)
5353     || (E_JPEG_RGB == gu8Scan_type))
5354     {
5355         // Allocate the coefficient buffer, enough for one row's worth of MCU's
5356         q = ( MS_U8 * )JPEG_alloc( _u32Max_blocks_per_row * 64 * sizeof( JPEG_BLOCK_TYPE ) + 8 );
5357         if(q == NULL)
5358             return FALSE;
5359 
5360         // Align to 8-byte boundry, for MMX code
5361         q = ( MS_U8 * )( ( ( MS_U32 )q + 7 ) & ~7 );
5362 
5363         // The _ps16Block_seg[] array's name dates back to the
5364         // 16-bit assembler implementation. "seg" stood for "segment".
5365         for ( i = 0; i < _u32Max_blocks_per_row; i++ )
5366         {
5367             _ps16Block_seg[i] = ( JPEG_BLOCK_TYPE * )( (MS_U32)q + i * 64 * sizeof( JPEG_BLOCK_TYPE ) );
5368         }
5369 
5370         for ( i = 0; i < _u32Max_blocks_per_row; i++ )
5371         {
5372             _u8Block_max_zag_set[i] = 64;
5373         }
5374 
5375         gpu8Sample_buf = ( MS_U8 * )( ( ( MS_U32 )JPEG_alloc( _u32Max_blocks_per_row * 64 + 8 ) + 7 ) & ~7 );
5376         if(gpu8Sample_buf == NULL)
5377             return FALSE;
5378 
5379         _u16Total_lines_left = _u16Image_y_size;
5380 
5381 #ifdef CMODEL
5382         gu16Mcu_lines_left = 0;
5383 #endif
5384 
5385         msAPI_JPEG_create_look_ups();
5386     }
5387 #endif
5388 
5389 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
5390     _u16Total_lines_left = _u16Max_mcus_per_col * gu8Max_mcu_y_size;
5391 #ifdef CMODEL
5392     gu16Mcu_lines_left = 0;
5393 #endif
5394 #endif
5395     return TRUE;
5396 }
5397 //------------------------------------------------------------------------------
5398 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
5399 //------------------------------------------------------------------------------
5400 // Restart interval processing.
JPEG_process_restart(void)5401 JPEG_STATIC MS_BOOL JPEG_process_restart(void)
5402 {
5403     MS_U16 i, c = 0;
5404 
5405     // Let's scan a little bit to find the marker, but not _too_ far.
5406     // 1536 is a "fudge factor" that determines how much to scan.
5407     for ( i = 1536; i > 0; i-- )
5408     {
5409         if ( JPEG_get_char() == 0xFF )
5410         {
5411             break;
5412         }
5413     }
5414 
5415     if ( i == 0 )
5416     {
5417         JPEG_terminate( E_JPEG_BAD_RESTART_MARKER );
5418         return FALSE;
5419     }
5420 
5421     for ( ; i > 0; i-- )
5422     {
5423         c = JPEG_get_char();
5424         if ( c != 0xFF )
5425         {
5426             break;
5427         }
5428     }
5429 
5430     if ( i == 0 )
5431     {
5432         JPEG_terminate( E_JPEG_BAD_RESTART_MARKER );
5433         return FALSE;
5434     }
5435 
5436     // Is it the expected marker? If not, something bad happened.
5437     if ( c != ( _u16Next_restart_num + E_JPEG_RST0 ) )
5438     {
5439         JPEG_terminate( E_JPEG_BAD_RESTART_MARKER );
5440         return FALSE;
5441     }
5442 
5443     // Reset each component's DC prediction values.
5444     JPEG_memset((void *)&_u32Last_dc_val, 0, _u8Comps_in_frame * sizeof( MS_U32 ) );
5445 
5446     _u32EOB_run = 0;
5447 
5448     _u16Restarts_left = _u16Restart_interval;
5449 
5450     _u16Next_restart_num = ( _u16Next_restart_num + 1 ) & 7;
5451 
5452     // Get the bit buffer going again...
5453     {
5454         _s16Bits_left = 16;
5455         JPEG_get_bits_2( 16 );
5456         JPEG_get_bits_2( 16 );
5457     }
5458     return TRUE;
5459 }
5460 //------------------------------------------------------------------------------
5461 // The following methods decode the various types of blocks encountered
5462 // in progressively encoded images.
JPEG_decode_block_dc_first(MS_U8 component_id,MS_U16 block_x,MS_U16 block_y)5463 JPEG_STATIC MS_BOOL JPEG_decode_block_dc_first(//JPEG_DECODER *Pd,
5464   MS_U8 component_id, MS_U16 block_x, MS_U16 block_y)
5465 {
5466     MS_S32 s, r;
5467     JPEG_BLOCK_TYPE *p = JPEG_coeff_buf_getp( _DC_Coeffs[component_id], block_x, block_y );
5468 
5469     if(p == NULL)
5470     {
5471         JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5472         return FALSE;
5473     }
5474 
5475     s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_dc_tab[component_id]] );
5476     if ( s != 0 )
5477     {
5478         r = JPEG_get_bits_2( s );
5479         s = HUFF_EXTEND_P( r, s );
5480     }
5481 
5482     // In JPD mode, the DC coefficient is the difference of nearest DC
5483     _u32Last_dc_val[component_id] = ( s += _u32Last_dc_val[component_id] );
5484 
5485     p[0] = s << _u8Successive_low;
5486     return TRUE;
5487 }
5488 //------------------------------------------------------------------------------
JPEG_decode_block_dc_refine(MS_U8 component_id,MS_U16 block_x,MS_U16 block_y)5489 JPEG_STATIC MS_BOOL JPEG_decode_block_dc_refine(//JPEG_DECODER *Pd,
5490   MS_U8 component_id, MS_U16 block_x, MS_U16 block_y)
5491 {
5492     if ( JPEG_get_bits_2( 1 ) )
5493     {
5494         JPEG_BLOCK_TYPE *p = JPEG_coeff_buf_getp( _DC_Coeffs[component_id], block_x, block_y );
5495 
5496         if(p == NULL)
5497         {
5498             JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5499             return FALSE;
5500         }
5501 
5502         p[0] |= ( 1 << _u8Successive_low );
5503     }
5504     return TRUE;
5505 }
5506 //------------------------------------------------------------------------------
JPEG_decode_block_ac_first(MS_U8 component_id,MS_U16 block_x,MS_U16 block_y)5507 JPEG_STATIC MS_BOOL JPEG_decode_block_ac_first(//JPEG_DECODER *Pd,
5508   MS_U8 component_id, MS_U16 block_x, MS_U16 block_y)
5509 {
5510     JPEG_BLOCK_TYPE *p;
5511     MS_S32 k, s, r;
5512 
5513     if ( _u32EOB_run )
5514     {
5515         _u32EOB_run--;
5516         return TRUE;
5517     }
5518 
5519     p = JPEG_coeff_buf_getp( _AC_Coeffs[component_id], block_x, block_y );
5520 
5521     if(p == NULL)
5522     {
5523         JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5524         return FALSE;
5525     }
5526 
5527     for ( k = _u8Spectral_start; k <= _u8Spectral_end; k++ )
5528     {
5529         s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_ac_tab[component_id]] );
5530 
5531         r = s >> 4;
5532         s &= 15;
5533 
5534         if ( s )
5535         {
5536             k += r;
5537             if ( k > 63 )
5538             {
5539                 JPEG_terminate( E_JPEG_DECODE_ERROR );
5540                 return FALSE;
5541             }
5542 
5543             r = JPEG_get_bits_2( s );
5544             s = HUFF_EXTEND_P( r, s );
5545 
5546             // No need to do ZAG order in JPD mode
5547 #ifdef CMODEL
5548             //p[_u8ZAG[k]] = s << _u8Successive_low;
5549             p[k] = s << _u8Successive_low;
5550 #else
5551 #if SW_JPD_RGB_CMYK
5552             if((E_JPEG_CMYK == gu8Scan_type)
5553             || (E_JPEG_RGB == gu8Scan_type))
5554             {
5555                 p[_u8ZAG[k]] = s << _u8Successive_low;
5556             }
5557             else
5558 #endif
5559             {
5560                 p[k] = s << _u8Successive_low;
5561             }
5562 #endif
5563         }
5564         else
5565         {
5566             if ( r == 15 )
5567             {
5568                 k += 15;
5569                 if ( k > 63 )
5570                 {
5571                     JPEG_terminate( E_JPEG_DECODE_ERROR );
5572                     return FALSE;
5573                 }
5574             }
5575             else
5576             {
5577                 _u32EOB_run = 1 << r;
5578 
5579                 if ( r )
5580                 {
5581                     _u32EOB_run += JPEG_get_bits_2( r );
5582                 }
5583 
5584                 _u32EOB_run--;
5585 
5586                 break;
5587             }
5588         }
5589     }
5590     return TRUE;
5591 }
5592 //------------------------------------------------------------------------------
JPEG_decode_block_ac_refine(MS_U8 component_id,MS_U16 block_x,MS_U16 block_y)5593 JPEG_STATIC MS_BOOL JPEG_decode_block_ac_refine(//JPEG_DECODER *Pd,
5594   MS_U8 component_id, MS_U16 block_x, MS_U16 block_y)
5595 {
5596     MS_S32 s, k, r;
5597     MS_S32 p1 = 1 << _u8Successive_low;
5598     MS_S32 m1 = ( -1 ) << _u8Successive_low;
5599     JPEG_BLOCK_TYPE *p = JPEG_coeff_buf_getp( _AC_Coeffs[component_id], block_x, block_y );
5600 
5601     if(p == NULL)
5602     {
5603         JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5604         return FALSE;
5605     }
5606 
5607     k = _u8Spectral_start;
5608 
5609     if ( _u32EOB_run == 0 )
5610     {
5611         for ( ; (k <= _u8Spectral_end) && ( k < 64 ); k++ )
5612         {
5613             s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_ac_tab[component_id]] );
5614 
5615             r = s >> 4;
5616             s &= 15;
5617 
5618             if ( s )
5619             {
5620                 if ( s != 1 )
5621                 {
5622                     JPEG_terminate( E_JPEG_DECODE_ERROR );
5623                     return FALSE;
5624                 }
5625 
5626                 if ( JPEG_get_bits_2( 1 ) )
5627                 {
5628                     s = p1;
5629                 }
5630                 else
5631                 {
5632                     s = m1;
5633                 }
5634             }
5635             else
5636             {
5637                 if ( r != 15 )
5638                 {
5639                     _u32EOB_run = 1 << r;
5640 
5641                     if ( r )
5642                     {
5643                         _u32EOB_run += JPEG_get_bits_2( r );
5644                     }
5645 
5646                     break;
5647                 }
5648             }
5649 
5650             do
5651             {
5652                 // No need to do ZAG order in JPD mode
5653 #ifdef CMODEL
5654                 //JPEG_BLOCK_TYPE *this_coef = p + _u8ZAG[k];
5655                 JPEG_BLOCK_TYPE *this_coef = p + k;
5656 #else
5657                 JPEG_BLOCK_TYPE *this_coef;
5658 #if SW_JPD_RGB_CMYK
5659                 if((E_JPEG_CMYK == gu8Scan_type)
5660                 || (E_JPEG_RGB == gu8Scan_type))
5661                 {
5662                     this_coef = p + _u8ZAG[k];
5663                 }
5664                 else
5665 #endif
5666                 {
5667                     this_coef = p + k;
5668                 }
5669 #endif
5670 
5671                 if ( *this_coef != 0 )
5672                 {
5673                     if ( JPEG_get_bits_2( 1 ) )
5674                     {
5675                         if ( ( *this_coef & p1 ) == 0 )
5676                         {
5677                             if ( *this_coef >= 0 )
5678                             {
5679                                 *this_coef += p1;
5680                             }
5681                             else
5682                             {
5683                                 *this_coef += m1;
5684                             }
5685                         }
5686                     }
5687                 }
5688                 else
5689                 {
5690                     if ( --r < 0 )
5691                     {
5692                         break;
5693                     }
5694                 }
5695 
5696                 k++;
5697             }
5698             while ( (k <= _u8Spectral_end) && ( k < 64 ) );
5699 
5700             if ( ( s ) && ( k < 64 ) )
5701             {
5702                 // No need to do ZAG order in JPD mode
5703 #ifdef CMODEL
5704                 //p[_u8ZAG[k]] = s;
5705                 p[k] = s;
5706 #else
5707 #if SW_JPD_RGB_CMYK
5708                 if((E_JPEG_CMYK == gu8Scan_type)
5709                 || (E_JPEG_RGB == gu8Scan_type))
5710                 {
5711                     p[_u8ZAG[k]] = s;
5712                 }
5713                 else
5714 #endif
5715                 {
5716                     p[k] = s;
5717                 }
5718 #endif
5719             }
5720         }
5721     }
5722 
5723     if ( _u32EOB_run > 0 )
5724     {
5725         for ( ; (k <= _u8Spectral_end) && ( k < 64 ); k++ )
5726         {
5727             // No need to do ZAG order in JPD mode
5728 #ifdef CMODEL
5729             //JPEG_BLOCK_TYPE *this_coef = p + _u8ZAG[k];
5730             JPEG_BLOCK_TYPE *this_coef = p + k;
5731 #else
5732             JPEG_BLOCK_TYPE *this_coef;
5733 #if SW_JPD_RGB_CMYK
5734             if((E_JPEG_CMYK == gu8Scan_type)
5735             || (E_JPEG_RGB == gu8Scan_type))
5736             {
5737                 this_coef = p + _u8ZAG[k];
5738             }
5739             else
5740 #endif
5741             {
5742                 this_coef = p + k;
5743             }
5744 #endif
5745 
5746             if ( *this_coef != 0 )
5747             {
5748                 if ( JPEG_get_bits_2( 1 ) )
5749                 {
5750                     if ( ( *this_coef & p1 ) == 0 )
5751                     {
5752                         if ( *this_coef >= 0 )
5753                         {
5754                             *this_coef += p1;
5755                         }
5756                         else
5757                         {
5758                             *this_coef += m1;
5759                         }
5760                     }
5761                 }
5762             }
5763         }
5764 
5765         _u32EOB_run--;
5766     }
5767     return TRUE;
5768 }
5769 //------------------------------------------------------------------------------
5770 // Decode a scan in a progressively encoded image.
JPEG_decode_scan(Pdecode_block_func decode_block_func)5771 JPEG_STATIC MS_BOOL JPEG_decode_scan(Pdecode_block_func decode_block_func)
5772 {
5773     MS_U16 mcu_row, mcu_col, mcu_block;
5774     MS_U32 block_x_mcu[JPEG_MAXCOMPONENTS], block_y_mcu[JPEG_MAXCOMPONENTS];
5775 
5776     JPEG_memset((void *)block_y_mcu, 0, sizeof( block_y_mcu ) );
5777 
5778     for ( mcu_col = 0; mcu_col < _u16Mcus_per_col; mcu_col++ )
5779     {
5780         int component_num, component_id;
5781 
5782         JPEG_memset((void *)block_x_mcu, 0, sizeof( block_x_mcu ) );
5783 
5784         for ( mcu_row = 0; mcu_row < _u16Mcus_per_row; mcu_row++ )
5785         {
5786             MS_U8 block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
5787 
5788             if ( ( _u16Restart_interval ) && ( _u16Restarts_left == 0 ) )
5789             {
5790                 if(!JPEG_process_restart())
5791                     return FALSE;
5792             }
5793 
5794             for ( mcu_block = 0; mcu_block < _u8Blocks_per_mcu; mcu_block++ )
5795             {
5796                 component_id = _u8Mcu_org[mcu_block];
5797 
5798                 if(!decode_block_func( component_id, block_x_mcu[component_id] + block_x_mcu_ofs, block_y_mcu[component_id] + block_y_mcu_ofs ))
5799                 {
5800                     JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
5801                     return FALSE;
5802                 }
5803 
5804                 if ( _u8Comps_in_scan == 1 )
5805                 {
5806                     block_x_mcu[component_id]++;
5807                 }
5808                 else
5809                 {
5810                     if ( ++block_x_mcu_ofs == _u8Comp_h_samp[component_id] )
5811                     {
5812                         block_x_mcu_ofs = 0;
5813 
5814                         if ( ++block_y_mcu_ofs == _u8Comp_v_samp[component_id] )
5815                         {
5816                             block_y_mcu_ofs = 0;
5817 
5818                             block_x_mcu[component_id] += _u8Comp_h_samp[component_id];
5819                         }
5820                     }
5821                 }
5822             }
5823 
5824             _u16Restarts_left--;
5825         }
5826 
5827         if ( _u8Comps_in_scan == 1 )
5828         {
5829             block_y_mcu[_u8Comp_list[0]]++;
5830         }
5831         else
5832         {
5833             for ( component_num = 0; component_num < _u8Comps_in_scan; component_num++ )
5834             {
5835                 component_id = _u8Comp_list[component_num];
5836 
5837                 block_y_mcu[component_id] += _u8Comp_v_samp[component_id];
5838             }
5839         }
5840     }
5841     return TRUE;
5842 }
5843 //------------------------------------------------------------------------------
5844 // Decode a progressively encoded image.
JPEG_init_progressive(void)5845 JPEG_STATIC MS_BOOL JPEG_init_progressive(void)
5846 {
5847     MS_U8 i;
5848 
5849     if ( _u8Comps_in_frame == 4 )
5850     {
5851         JPEG_terminate( E_JPEG_UNSUPPORTED_COLORSPACE );
5852         return FALSE;
5853     }
5854 
5855     // Allocate the coefficient buffers.
5856     for ( i = 0; i < _u8Comps_in_frame; i++ )
5857     {
5858         _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 );
5859         if(_DC_Coeffs[i] == NULL)
5860         {
5861             return FALSE;
5862         }
5863 
5864         _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 );
5865         if(_AC_Coeffs[i] == NULL)
5866         {
5867             return FALSE;
5868         }
5869     }
5870 
5871     for ( ; ; )
5872     {
5873         MS_BOOL dc_only_scan, refinement_scan;
5874         Pdecode_block_func decode_block_func;
5875 
5876         if (!JPEG_init_scan())
5877         {
5878             break;
5879         }
5880 
5881         dc_only_scan = ( _u8Spectral_start == 0 );
5882         refinement_scan = ( _u8Successive_high != 0 );
5883 
5884         if ( ( _u8Spectral_start > _u8Spectral_end ) || ( _u8Spectral_end > 63 ) )
5885         {
5886             JPEG_terminate( E_JPEG_BAD_SOS_SPECTRAL );
5887             return FALSE;
5888         }
5889 
5890         if ( dc_only_scan )
5891         {
5892             if ( _u8Spectral_end )
5893             {
5894                 JPEG_terminate( E_JPEG_BAD_SOS_SPECTRAL );
5895                 return FALSE;
5896             }
5897         }
5898         else if ( _u8Comps_in_scan != 1 )  /* AC scans can only contain one component */
5899         {
5900             JPEG_terminate( E_JPEG_BAD_SOS_SPECTRAL );
5901             return FALSE;
5902         }
5903 
5904         if ( ( refinement_scan ) && ( _u8Successive_low != _u8Successive_high - 1 ) )
5905         {
5906             JPEG_terminate( E_JPEG_BAD_SOS_SUCCESSIVE );
5907             return FALSE;
5908         }
5909 
5910         if ( dc_only_scan )
5911         {
5912             if ( refinement_scan )
5913             {
5914                 decode_block_func = JPEG_decode_block_dc_refine;
5915             }
5916             else
5917             {
5918                 decode_block_func = JPEG_decode_block_dc_first;
5919             }
5920         }
5921         else
5922         {
5923             if ( refinement_scan )
5924             {
5925                 decode_block_func = JPEG_decode_block_ac_refine;
5926             }
5927             else
5928             {
5929                 decode_block_func = JPEG_decode_block_ac_first;
5930             }
5931         }
5932 
5933         if(!JPEG_decode_scan( decode_block_func ))
5934             return FALSE;
5935 
5936         _s16Bits_left = 0;
5937     }
5938 
5939     _u8Comps_in_scan = _u8Comps_in_frame;
5940 
5941     for ( i = 0; i < _u8Comps_in_frame; i++ )
5942     {
5943         _u8Comp_list[i] = i;
5944     }
5945 
5946     JPEG_calc_mcu_block_order();
5947     return TRUE;
5948 }
5949 #endif
5950 //------------------------------------------------------------------------------
JPEG_init_sequential(void)5951 JPEG_STATIC MS_BOOL JPEG_init_sequential(void)
5952 {
5953     if ( !JPEG_init_scan() )
5954     {
5955         JPEG_DEBUG_API_ERR("JPEG_init_scan - E_JPEG_UNEXPECTED_MARKER\n");
5956         JPEG_terminate( E_JPEG_UNEXPECTED_MARKER );
5957         return FALSE;
5958     }
5959     return TRUE;
5960 }
5961 //------------------------------------------------------------------------------
5962 #if 0
5963 JPEG_STATIC void JPEG_decode_start(void)
5964 {
5965     JPEG_init_frame();
5966 
5967     if ( _bProgressive_flag )
5968     {
5969         JPEG_init_progressive();
5970     }
5971     else
5972     {
5973         JPEG_init_sequential();
5974     }
5975 }
5976 #endif
5977 //------------------------------------------------------------------------------
5978 // Find the start of the JPEG file and reads enough data to determine
5979 // its size, number of components, etc.
JPEG_decode_init(void)5980 JPEG_STATIC MS_BOOL JPEG_decode_init(void)
5981 {
5982     MS_U16 pic_width, pic_height;
5983 
5984     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
5985 
5986     _HeadCheck.result = 0;
5987 
5988     if(!JPEG_init())
5989         return FALSE;
5990 
5991 #if SUPPORT_MPO_FORMAT
5992     _pu8In_buf_ofs = _pu8In_buf_MPO_ofs;
5993     _u32In_buf_left = _u32In_buf_MPO_left;
5994     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);
5995 #endif
5996 
5997     if(!JPEG_locate_sof_marker())
5998         return FALSE;
5999 
6000     if (bMHEG5)
6001     {
6002     if(_Error_code == E_JPEG_UNSUPPORTED_MARKER)
6003         {
6004             return TRUE;
6005         }
6006     }
6007 
6008     if(_u8DecodeType == E_JPEG_TYPE_THUMBNAIL)
6009     {
6010         if(_bThumbnailFound)
6011         {
6012             _bThumbnailAccessMode = TRUE;
6013             ////MApi_JPEG_Finalize(); //replace with JPEG_free_all_blocks()
6014             JPEG_free_all_blocks();
6015 
6016             if(!JPEG_init())
6017                 return FALSE;
6018 
6019             // save the original image size, because msAPI_JPEG_init will reset all variables to 0
6020             if(!JPEG_locate_sof_marker())
6021                 return FALSE;
6022 
6023             if(_bProgressive_flag)
6024             {
6025                 JPEG_DEBUG_API_ERR("Progressive image in thumbnail... do not handle it\n");
6026                 JPEG_terminate( E_JPEG_BAD_APP1_MARKER );
6027                 return FALSE;
6028             }
6029         }
6030         else
6031         {
6032             JPEG_terminate( E_JPEG_NO_THUMBNAIL );
6033             return FALSE;
6034         }
6035     }
6036 
6037     // calculate aligned resolution
6038     JPEG_GetAlignedResolution(&pic_width, &pic_height);
6039     JPEG_DEBUG_API_MSG("pic_width = %d, pic_height = %d\n",pic_width, pic_height);
6040 
6041     _u16AlignedImagePitch = pic_width;
6042     _u16AlignedImagePitch_H = pic_height;
6043     _u16AlignedImageWidth = _u16Image_x_size;
6044     _u16AlignedImageHeight = _u16Image_y_size;
6045 
6046 #if SUPPORT_MPO_FORMAT
6047     if(bIsMPOFormat)
6048     {
6049         JPEG_PRO_MAX_WIDTH = JPEG_MPO_PRO_MAX_WIDTH;
6050         JPEG_PRO_MAX_HEIGHT = JPEG_MPO_PRO_MAX_HEIGHT;
6051         JPEG_MAX_WIDTH  = JPEG_MPO_MAX_WIDTH;
6052         JPEG_MAX_HEIGHT   = JPEG_MPO_MAX_HEIGHT;
6053     }
6054 #endif
6055 
6056     if(_bProgressive_flag)
6057     {
6058         MAX_JPEG_WIDTH_HD = JPEG_PRO_MAX_WIDTH;
6059         MAX_JPEG_HEIGHT_HD = JPEG_PRO_MAX_HEIGHT;
6060     }
6061     else
6062     {
6063         MAX_JPEG_WIDTH_HD = JPEG_MAX_WIDTH;
6064         MAX_JPEG_HEIGHT_HD = JPEG_MAX_HEIGHT;
6065     }
6066 
6067 #ifndef CMODEL
6068     // calculate down scale factor
6069     if ((_u16Image_x_size > MAX_JPEG_WIDTH_HD * 4) || (_u16Image_y_size > MAX_JPEG_HEIGHT_HD * 4))
6070     {
6071         JPEG_DEBUG_API_MSG("down scale 1/8!!\n");
6072         _u8DownScaleRatio = E_JPD_DOWNSCALE_EIGHTH;
6073         _u8ScaleDownFactor = 8;
6074     }
6075     else if ((_u16Image_x_size > MAX_JPEG_WIDTH_HD * 2) || (_u16Image_y_size > MAX_JPEG_HEIGHT_HD * 2))
6076     {
6077         JPEG_DEBUG_API_MSG("down scale 1/4!!\n");
6078         _u8DownScaleRatio = E_JPD_DOWNSCALE_FOURTH;
6079         _u8ScaleDownFactor = 4;
6080     }
6081     else if ((_u16Image_x_size > MAX_JPEG_WIDTH_HD) || (_u16Image_y_size > MAX_JPEG_HEIGHT_HD))
6082     {
6083         JPEG_DEBUG_API_MSG("down scale 1/2!!\n");
6084         _u8DownScaleRatio = E_JPD_DOWNSCALE_HALF;
6085         _u8ScaleDownFactor = 2;
6086     }
6087     else
6088     {
6089         JPEG_DEBUG_API_MSG("down scale 1/1!!\n");
6090         _u8DownScaleRatio = E_JPD_DOWNSCALE_ORG;
6091         _u8ScaleDownFactor = 1;
6092     }
6093 
6094 #if (ENABLE_TEST_11_JPEGScaleDownFunctionTest_2==true) \
6095     || (ENABLE_TEST_11_JPEGScaleDownFunctionTest_4==true) \
6096     || (ENABLE_TEST_11_JPEGScaleDownFunctionTest_8==true)
6097     //downscale test(only for unit test)
6098 
6099     #if (ENABLE_TEST_11_JPEGScaleDownFunctionTest_2==true)
6100     _u8DownScaleRatio = E_JPD_DOWNSCALE_HALF;
6101     #elif (ENABLE_TEST_11_JPEGScaleDownFunctionTest_4==true)
6102     _u8DownScaleRatio = E_JPD_DOWNSCALE_FOURTH;
6103     #elif (ENABLE_TEST_11_JPEGScaleDownFunctionTest_8==true)
6104     _u8DownScaleRatio = E_JPD_DOWNSCALE_EIGHTH;
6105     #else
6106     _u8DownScaleRatio = E_JPD_DOWNSCALE_ORG;
6107     #endif
6108 
6109     if(_u8DownScaleRatio == E_JPD_DOWNSCALE_HALF)
6110     {
6111         _u8ScaleDownFactor = 2;
6112     }
6113     else if(_u8DownScaleRatio == E_JPD_DOWNSCALE_FOURTH)
6114     {
6115         _u8ScaleDownFactor = 4;
6116     }
6117     else if(_u8DownScaleRatio == E_JPD_DOWNSCALE_EIGHTH)
6118     {
6119         _u8ScaleDownFactor = 8;
6120     }
6121 #endif
6122 
6123     if (E_JPD_DOWNSCALE_ORG != _u8DownScaleRatio)
6124     {
6125         _u16AlignedImagePitch = pic_width / _u8ScaleDownFactor;
6126         _u16AlignedImagePitch_H =
6127             ((pic_height + ((MS_U32)_u8ScaleDownFactor - 1)) & (~((MS_U32)_u8ScaleDownFactor - 1)))/ _u8ScaleDownFactor;
6128         _u16AlignedImageWidth = _u16Image_x_size / _u8ScaleDownFactor;
6129         _u16AlignedImageHeight =
6130                 ((_u16AlignedImageHeight + ((MS_U32)_u8ScaleDownFactor - 1)) & (~((MS_U32)_u8ScaleDownFactor - 1))) / _u8ScaleDownFactor;
6131     }
6132 
6133     switch (_u8DownScaleRatio)
6134     {
6135         case E_JPD_DOWNSCALE_HALF:
6136             ROI_width = (pic_width / 16) * 16;
6137             break;
6138 
6139         case E_JPD_DOWNSCALE_FOURTH:
6140             ROI_width = (pic_width / 32) * 32;
6141             break;
6142 
6143         case E_JPD_DOWNSCALE_EIGHTH:
6144             ROI_width = (pic_width / 64) * 64;
6145             break;
6146 
6147         default:
6148             ROI_width = pic_width;
6149             break;
6150     }
6151     JPEG_DEBUG_API_MSG("Original ROI_width = %d \n", ROI_width);
6152 
6153     if (ROI_width != pic_width)
6154     {
6155         _u16AlignedImagePitch = _u16AlignedImageWidth = ROI_width / _u8ScaleDownFactor;
6156 
6157         if(_bProgressive_flag)
6158             _Progressive_ROI_flag = TRUE;
6159     }
6160 #endif //#ifndef CMODEL
6161     return TRUE;
6162 }
6163 #if 0 // not implement for new MDDI - harold
6164 //------------------------------------------------------------------------------
6165 JPEG_STATIC void msAPI_MJPEG_decode_init(PJPEG_FILE_FileSystem_t Pstream)
6166 {
6167     JPEG_init( Pstream );
6168     MSAPI_MJPEG_Get_Pics();
6169     JPEG_locate_sof_marker();
6170 
6171     if(_u8DecodeType == E_JPEG_TYPE_THUMBNAIL)
6172     {
6173         if(_bThumbnailFound)
6174         {
6175             _bThumbnailAccessMode = TRUE;
6176             ////MApi_JPEG_Finalize(); //replace with JPEG_free_all_blocks()
6177             JPEG_free_all_blocks();
6178             JPEG_init( Pstream );
6179 
6180             JPEG_locate_sof_marker();
6181 
6182             if(_bProgressive_flag)
6183             {
6184                 JPEG_DEBUG_API_MSG("Progressive image in thumbnail... do not handle it\n");
6185                 JPEG_terminate( E_JPEG_BAD_APP1_MARKER );
6186             }
6187         }
6188         else
6189         {
6190             JPEG_terminate( E_JPEG_NO_THUMBNAIL );
6191         }
6192     }
6193 }
6194 //------------------------------------------------------------------------------
6195 // Parse header of MJPEG to get total num of pics in the file
6196 JPEG_STATIC void MSAPI_MJPEG_Get_Pics(void)
6197 {
6198     //Num of pics located at Byte 49~52, hence we need to offset 48 bytes
6199     MS_U8 ByteOffset, ByteSkip = 48;
6200 
6201     //Total num of pics
6202     NumPics = 0;
6203 
6204     for(ByteOffset = 0; ByteOffset < ByteSkip; ByteOffset++)
6205     {
6206         JPEG_get_char();
6207     }
6208 
6209     //Handle Little-Ending
6210     NumPics += (JPEG_get_char());
6211     NumPics += (JPEG_get_char() << 8);
6212     NumPics += (JPEG_get_char() << 16);
6213     NumPics += (JPEG_get_char() << 24);
6214 
6215     JPEG_DEBUG_API_MSG("NumPics = %ld\n",NumPics);
6216 }
6217 #endif // not implement for new MDDI - harold
6218 
6219 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
6220 //------------------------------------------------------------------------------
6221 // Loads and dequantizes the next row of (already decoded) coefficients.
6222 // Progressive images only.
JPEG_load_next_row(void)6223 JPEG_STATIC MS_BOOL JPEG_load_next_row(void)
6224 {
6225 #ifndef CMODEL
6226     JPEG_BLOCK_TYPE p[64];
6227     MS_BOOL EOF_Flag = FALSE; //CL82399
6228 #else
6229     MS_S16 i;
6230     JPEG_BLOCK_TYPE *p;
6231     MS_U16 row_block = 0;
6232     JPEG_QUANT_TYPE *q;
6233 #endif
6234 
6235     MS_U16 mcu_row, mcu_block;
6236     MS_U8 component_num, component_id;
6237     MS_U16 block_x_mcu[JPEG_MAXCOMPONENTS];
6238 
6239     JPEG_memset((void *)block_x_mcu, 0, JPEG_MAXCOMPONENTS * sizeof( MS_U16 ) );
6240 
6241     for ( mcu_row = 0; mcu_row < _u16Mcus_per_row; mcu_row++ )
6242     {
6243         MS_U16 block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
6244 
6245         for ( mcu_block = 0; mcu_block < _u8Blocks_per_mcu; mcu_block++ )
6246         {
6247             JPEG_BLOCK_TYPE *pAC;
6248             JPEG_BLOCK_TYPE *pDC;
6249 
6250             component_id = _u8Mcu_org[mcu_block];
6251 
6252 #ifdef CMODEL
6253             p = gps16Block_seg[row_block];
6254             q = _QuantTables[_u8Comp_quant[component_id]].s16Value;
6255 #endif
6256 
6257             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 );
6258             if(pAC == NULL)
6259             {
6260                 JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
6261                 return FALSE;
6262             }
6263 
6264             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 );
6265             if(pDC == NULL)
6266             {
6267                 JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
6268                 return FALSE;
6269             }
6270             p[0] = pDC[0];
6271             JPEG_memcpy((void *)&p[1], (void *)&pAC[1], 63 * sizeof( JPEG_BLOCK_TYPE ) );
6272 
6273 #ifdef CMODEL //cmodel do not support scale down feature.
6274             if(mcu_block==(_u8Blocks_per_mcu -1)&&mcu_row==(_u16Mcus_per_row - 1)&&(_u16Total_lines_left - gu8Max_mcu_y_size)==0)
6275             {
6276                 if(!JPEG_do_RLE(p, TRUE, component_id, TRUE))   // means it is end of picture
6277                     return FALSE;
6278             }
6279             else
6280             {
6281                 if(!JPEG_do_RLE(p, FALSE, component_id, TRUE))
6282                     return FALSE;
6283             }
6284 #else //CL82399
6285             if( _Progressive_ROI_flag == FALSE )
6286             {
6287                 if((mcu_block == (_u8Blocks_per_mcu - 1))
6288                 && (mcu_row == (_u16Mcus_per_row - 1))
6289                 && (_u16Total_lines_left == gu8Max_mcu_y_size))
6290                 {
6291                     JPEG_DEBUG_API_MSG("EOF!!!!!ROI enable!!!\n");
6292                     if(!JPEG_do_RLE(p, TRUE, component_id, TRUE))   // means it is end of picture
6293                         return FALSE;
6294                 }
6295                 else
6296                 {
6297                     if(!JPEG_do_RLE(p, FALSE, component_id, TRUE))
6298                         return FALSE;
6299                 }
6300             }
6301             else
6302             {
6303                 if( _u16Total_lines_left == gu8Max_mcu_y_size )//Last Line
6304                 {
6305                     //JPEG_DEBUG_API_MSG("_u16Total_lines_left ==%d,%d,%d \n", gu8Max_mcu_y_size, mcu_block, mcu_row);
6306 
6307                     if((mcu_block == (_u8Blocks_per_mcu - 1))
6308                     && ((mcu_row + 2) * gu8Max_mcu_x_size >  ROI_width))//Last line last block within rang
6309                     {
6310                         if( EOF_Flag == FALSE )
6311                         {
6312                             EOF_Flag = TRUE;
6313                             JPEG_DEBUG_API_MSG("EOF!!!!!No ROI!!!\n");
6314                             if(!JPEG_do_RLE(p, TRUE, component_id, TRUE))   // means it is end of picture
6315                                 return FALSE;
6316                         }
6317                     }
6318                     else
6319                     {
6320                          if(!JPEG_do_RLE(p, FALSE, component_id, TRUE))
6321                             return FALSE;
6322                     }
6323                 }
6324                 else
6325                 {
6326                     if((mcu_row + 1) * gu8Max_mcu_x_size >  ROI_width)//ever line out rang block
6327                     {
6328                         //JPEG_do_RLE(p, FALSE, component_id, FALSE);
6329                     }
6330                     else
6331                     {
6332                         if(!JPEG_do_RLE(p, FALSE, component_id, TRUE))
6333                             return FALSE;
6334                     }
6335                 }
6336             }
6337 #endif
6338 
6339 
6340 #ifdef CMODEL
6341             {
6342                 for ( i = 63; i > 0; i-- )
6343                 {
6344                     if ( p[_u8ZAG[i]] )
6345                     {
6346                         break;
6347                     }
6348                 }
6349 
6350                 //block_num[row_block++] = i + 1;
6351 
6352                 for ( ; i >= 0; i-- )
6353                 {
6354                     if ( p[_u8ZAG[i]] )
6355                     {
6356                         p[_u8ZAG[i]] *= q[i];
6357                     }
6358                 }
6359             }
6360 
6361             row_block++;
6362 #endif
6363 
6364             if ( _u8Comps_in_scan == 1 )
6365             {
6366                 block_x_mcu[component_id]++;
6367             }
6368             else
6369             {
6370                 if ( ++block_x_mcu_ofs == _u8Comp_h_samp[component_id] )
6371                 {
6372                     block_x_mcu_ofs = 0;
6373 
6374                     if ( ++block_y_mcu_ofs == _u8Comp_v_samp[component_id] )
6375                     {
6376                         block_y_mcu_ofs = 0;
6377 
6378                         block_x_mcu[component_id] += _u8Comp_h_samp[component_id];
6379                     }
6380                 }
6381             }
6382         }
6383     }
6384 
6385     if ( _u8Comps_in_scan == 1 )
6386     {
6387         _u32Block_y_mcu[_u8Comp_list[0]]++;
6388     }
6389     else
6390     {
6391         for ( component_num = 0; component_num < _u8Comps_in_scan; component_num++ )
6392         {
6393             component_id = _u8Comp_list[component_num];
6394 
6395             _u32Block_y_mcu[component_id] += _u8Comp_v_samp[component_id];
6396         }
6397     }
6398     return TRUE;
6399 }
6400 #endif
6401 
6402 #ifndef CMODEL
6403 //------------------------------------------------------------------------------
6404 /******************************************************************************/
6405 ///Start JPEG decoding
6406 /******************************************************************************/
JPEG_StartDecode(void)6407 JPEG_STATIC MS_BOOL JPEG_StartDecode(void)
6408 {
6409     MS_U16 pic_width, pic_height;
6410     MS_U8 Y_VSF = _u8Comp_v_samp[0];
6411     MS_U8 Y_HSF = _u8Comp_h_samp[0];
6412     MS_BOOL bUV_en;
6413     MS_U32 reg_value;
6414     MS_U8 i;
6415     MS_U8 com_num = 0;
6416     MS_U8 comp[JPEG_MAXCOMPONENTS];
6417     JPD_BufCfg structBuf;
6418 
6419     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
6420 #ifdef JPD_LONGJUMP_SUPPORT
6421     if ( setjmp( _jmp_state ) )
6422     {
6423         return FALSE;
6424     }
6425 #endif
6426 
6427 #if ENABLE_JPEG_NO_SIZE_LOWER_BOUND
6428     MDrv_JPD_SetPicDimension(0xFF, 0xFF);
6429 #endif
6430 
6431     // reset JPD hardware
6432     //MDrv_JPD_Reset();
6433     if(_bProgressive_flag)
6434     {
6435         MDrv_JPD_Rst_SVLD();
6436     }
6437     else
6438     {
6439         MDrv_JPD_Rst();
6440     }
6441 
6442 
6443     // Calculate how many valid quantization tables for components
6444     JPEG_memset((void *)comp, 0, JPEG_MAXCOMPONENTS);
6445     for(i = 0; i<_u8Comps_in_frame; i++)
6446     {
6447         comp[_u8Comp_quant[i]] = 1;
6448     }
6449 
6450     for(i = 0; i<JPEG_MAXCOMPONENTS; i++)
6451     {
6452         if(comp[i]==1)
6453             com_num++;
6454     }
6455 
6456     if(_u8Comps_in_frame>1)
6457         bUV_en = TRUE;
6458     else
6459         bUV_en = FALSE;
6460 
6461     //Get Aligned width & height
6462     JPEG_GetAlignedResolution(&pic_width, &pic_height);
6463 
6464 #if 1
6465     structBuf.bProgressive = _bProgressive_flag;
6466     structBuf.bThumbnailAccessMode = _bThumbnailAccessMode;
6467     structBuf.u32ThumbnailBufAddr = MS_VA2PA((MS_U32)INTERNAL_BUFFER_ADDR);
6468     structBuf.u32ThumbnailBufSize = JPEG_DEFAULT_EXIF_SIZE;
6469     structBuf.u32ThumbnailBufOffset = _u32ThumbnailOffset + JPEG_GetECS();
6470     structBuf.u32MRCBufAddr = MS_VA2PA((MS_U32)MRC_BUFFER_ADDR);
6471     structBuf.u32MRCBufSize = MRC_BUFFER_SIZE;
6472     structBuf.u32MRCBufOffset = JPEG_GetECS();
6473     structBuf.u32MWCBufAddr = MS_VA2PA((MS_U32)MWC_BUFFER_ADDR);
6474     //HW limitation:if we don't enable write-protect mode, set this value to zero.
6475 
6476 #if (ENABLE_TEST_09_JPEGWriteProtectTest==false)
6477     structBuf.u16MWCBufLineNum = 0;
6478 //    structBuf.u16MWCBufLineNum = ((pic_height/_u8ScaleDownFactor + 8) < 0x07FF)?(pic_height/_u8ScaleDownFactor + 8):0x7FF;
6479 #else
6480     structBuf.u16MWCBufLineNum = 32;
6481 #endif
6482 
6483     JPEG_DEBUG_API_MSG("bProgressive = %d, bThumbnailAccessMode = %d\n"
6484     , structBuf.bProgressive, structBuf.bThumbnailAccessMode);
6485     JPEG_DEBUG_API_MSG("u32ThumbnailBufAddr = 0x%lX, u32ThumbnailBufSize = 0x%lX, u32ThumbnailBufOffset = 0x%lX\n"
6486     , structBuf.u32ThumbnailBufAddr, structBuf.u32ThumbnailBufSize, structBuf.u32ThumbnailBufOffset);
6487     JPEG_DEBUG_API_MSG("u32MRCBufAddr = 0x%lX, u32MRCBufSize = 0x%lX, u32MRCBufOffset = 0x%lX\n"
6488     , structBuf.u32MRCBufAddr, structBuf.u32MRCBufSize, structBuf.u32MRCBufOffset);
6489     JPEG_DEBUG_API_MSG("u32MWCBufAddr = 0x%lX, u16MWCBufLineNum = %d\n"
6490     , structBuf.u32MWCBufAddr, structBuf.u16MWCBufLineNum);
6491 
6492     u32DataOffset += JPEG_GetECS();
6493 
6494     MDrv_JPD_InitBuf(structBuf);
6495 
6496 //    JPEG_DEBUG_API_MSG("MWCLineNum = %d, pic_height = %d, DwnScaleRatio = %d\n"
6497 //            , structBuf.u16MWCBufLineNum, pic_height, _u8ScaleDownFactor);
6498 #else
6499     if(_bThumbnailAccessMode)
6500     {
6501         // Set MRC buffer for JPD
6502         MDrv_JPD_SetReadBuffer(INTERNAL_BUFFER_ADDR, JPEG_DEFAULT_EXIF_SIZE);
6503         // Set MRC start access byte address
6504         MDrv_JPD_SetMRCStartAddr(INTERNAL_BUFFER_ADDR + _u32ThumbnailOffset + JPEG_GetECS());
6505     }
6506     else
6507     {
6508         if(_bProgressive_flag)
6509         {
6510             // Set MRC buffer for JPD
6511             MDrv_JPD_SetReadBuffer(MRC_BUFFER_ADDR, MRC_BUFFER_SIZE);
6512             // Set MRC start access byte address
6513             MDrv_JPD_SetMRCStartAddr(MRC_BUFFER_ADDR);
6514         }
6515         else
6516         {
6517             // Set MRC buffer for JPD
6518             MDrv_JPD_SetReadBuffer(MRC_BUFFER_ADDR, MRC_BUFFER_SIZE);
6519             // Set MRC start access byte address
6520             MDrv_JPD_SetMRCStartAddr(MRC_BUFFER_ADDR + JPEG_GetECS());
6521         }
6522     }
6523 
6524     // Set MWC buffer for JPD
6525     MDrv_JPD_SetOutputFrameBuffer(MWC_BUFFER_ADDR);
6526 #endif
6527 
6528     // Set picture width and height
6529 #if (ENABLE_JPEG_NO_SIZE_LOWER_BOUND == 0)
6530     MDrv_JPD_SetPicDimension(pic_width, pic_height);
6531 #endif
6532 
6533     _u16NonAlignmentImage_x_size = pic_width;
6534     _u16NonAlignmentImage_y_size = _u16Image_y_size;
6535 
6536     _u16Image_x_size = pic_width/_u8ScaleDownFactor;
6537     _u16Image_y_size = pic_height/_u8ScaleDownFactor;
6538     JPEG_DEBUG_API_MSG("ScaleDownFactor = %d\n", _u8ScaleDownFactor);
6539 
6540     // In JPD, software VLD mode, we don't need to write huff & symbol tables
6541     if(_bProgressive_flag==FALSE)
6542     {
6543         //JPEG_write_Scwgif();
6544         JPEG_WriteGrpinf();
6545         //JPEG_write_symidx();
6546         JPEG_WriteSymidx();
6547     }
6548 
6549     //JPEG_write_Qtbl();
6550     JPEG_WriteIQTbl();
6551 
6552     Y_VSF -= 1;
6553     if ( Y_HSF == 4 )
6554     {
6555         Y_HSF = 3;
6556     }
6557 
6558     if(_u16Restart_interval)
6559     {
6560         JPEG_DEBUG_API_MSG("RST found! Enable JPD_RST_EN! Restart_interval = %d\n", _u16Restart_interval);
6561         //MDrv_Write2Byte(BK_JPD_RSTINTV, _u16Restart_interval - 1);
6562         MDrv_JPD_SetRSTIntv(_u16Restart_interval - 1);
6563         reg_value = ( JPD_TBL_RDY | JPD_RST_EN | ((MS_U32) _u8DownScaleRatio) << 4 | ((MS_U32) bUV_en) << 3 | ( Y_VSF << 2 ) | Y_HSF );
6564     }
6565     else
6566     {
6567         reg_value = ( JPD_TBL_RDY | ((MS_U32) _u8DownScaleRatio) << 4 | ((MS_U32) bUV_en) << 3 | ( Y_VSF << 2 ) | Y_HSF );
6568     }
6569 
6570     // There're Q tables for U & V, respectively.
6571     if(com_num>2)
6572     {
6573         JPEG_DEBUG_API_MSG("More than two Q tables! Enable JPD_SUVQ! comnum = %d\n", com_num);
6574         reg_value = reg_value | JPD_SUVQ;
6575     }
6576 
6577     /* Check the read pointer. If it is in HIGH buffer, we need
6578        to preload data to LOW buffer and then start decode.
6579        Progressive mode & thumbnail don't need to check it. */
6580     if((FALSE == _bProgressive_flag)
6581     && (FALSE == _bThumbnailAccessMode))
6582     {
6583         MS_U32 start_offset = JPEG_GetECS();
6584         JPEG_DEBUG_API_MSG("start offset = 0x%lx\n", start_offset);
6585         if ( start_offset >= (MRC_BUFFER_SIZE/2) )
6586         {
6587             JPEG_DEBUG_API_MSG("ReadPtr is in HIGH, Load LOW!!\n");
6588             if(_pFillHdrFunc)
6589             {
6590                 MS_S32 byte_read = _pFillHdrFunc((MS_PHYADDR)MS_VA2PA((MS_U32)_pu8In_buf), (MRC_BUFFER_SIZE/2));
6591                 if(byte_read < 0)
6592                 {
6593                     JPEG_terminate(E_JPEG_STREAM_READ);
6594                     return FALSE;
6595                 }
6596                 u8PreLHFlag = E_JPEG_BUFFER_LOW;
6597                 u32MRCheckCount = 0;
6598             }
6599             else
6600             {
6601                 JPEG_DEBUG_API_MSG("_pFillHdrFunc is not registered!!, still need to start decode.\n");
6602             }
6603         }
6604     }
6605 
6606     if(_bProgressive_flag)
6607         reg_value = reg_value | JPD_SVLD;
6608 
6609     // Check if it needs to do ROI
6610     if(E_JPD_DOWNSCALE_ORG != _u8DownScaleRatio)
6611     {
6612         if(ROI_width != pic_width)
6613         {
6614             JPEG_DEBUG_API_MSG("ROI!! ROI_width = %d, ROI_height = %d\n", ROI_width, pic_height);
6615             MDrv_JPD_SetROI(0, 0, (ROI_width>>3), (pic_height>>3));
6616             reg_value = reg_value | JPD_ROI_EN;
6617         }
6618         _u16Image_x_size = ROI_width/_u8ScaleDownFactor;
6619     }
6620 
6621     //Enable ECO support
6622     {
6623         MS_U16 reg_val = 0;
6624         reg_val = JPD_RST_STUFF_BYTE_HANDLE;
6625 #if (JPD_SUPPORT_3_HUFFMAN_TABLE==true)
6626         if(TRUE == bIs3HuffTbl)
6627         {
6628             reg_val = reg_val | JPD_3_HUFFMAN_TABLE_SUPPORT;
6629         }
6630 #endif
6631         MDrv_JPD_SetSpare(reg_val);
6632     }
6633 
6634     // mark low/high buffer valid
6635     //MDrv_Write2Byte( BK_JPD_MCONFIG, JPD_H_VLD |JPD_L_VLD);
6636     MDrv_JPD_Set_M_Config(JPD_H_VLD |JPD_L_VLD);
6637 
6638 #if (ENABLE_TEST_18_miu_sel_128M==true) \
6639     ||(ENABLE_TEST_18_miu_sel_64M==true) \
6640     ||(ENABLE_TEST_18_miu_sel_32M==true)
6641 // This is used for verification code in TEST_18_miu_sel
6642 //        MDrv_JPD_Set_M_Config(MDrv_JPD_Get_M_Config());             // 0 1
6643     #if (ENABLE_TEST_18_miu_sel_128M==true)
6644         MDrv_JPD_Set_M_Config(MDrv_JPD_Get_M_Config() | JPD_BIT(9));          // 0 1 0 1
6645     #elif (ENABLE_TEST_18_miu_sel_64M==true)
6646         MDrv_JPD_Set_M_Config(MDrv_JPD_Get_M_Config() | JPD_BIT(10));   // 0 1 0 1 0 1 0 1
6647     #else
6648         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
6649     #endif
6650 #endif
6651 
6652     // enable JPD decoding
6653 #if ENABLE_JPEG_NO_SIZE_LOWER_BOUND
6654     MDrv_JPD_SetPicDimension(pic_width, pic_height);
6655 #endif
6656 
6657     //MDrv_Write2Byte( BK_JPD_SCONFIG, reg_value | JPD_DEC_EN | MDrv_JPD_GetSWResetMask());
6658     //MDrv_JPD_Set_S_Config(reg_value | JPD_DEC_EN | MDrv_JPD_GetSWResetMask());
6659     //MDrv_JPD_Set_S_Config(reg_value | JPD_DEC_EN | JPD_SWRST);
6660 #if 1
6661     // decide if we need to set the JPD_PSAVE_EN register in hal layer
6662     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST | JPD_PSAVE_EN);
6663 #else
6664 #if (ENABLE_TEST_16_JPEGEnablePsaveModeTest==false)
6665 #if defined(CHIP_A3) || defined(CHIP_E3) || defined(CHIP_A5) || defined(CHIP_A5P) ||  defined(CHIP_A6) || defined(CHIP_A7)
6666     // For safety, enable the JPD_PSAVE_EN as default since A5 (HW already fixed this bug since A1 or earlier chip)
6667     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST | JPD_PSAVE_EN);
6668 #else
6669     //due to power saving mode will make system hang in some case(the image of RST error),we do not enable this function currently.
6670     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST);
6671 #endif
6672 #else
6673 #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)
6674     //Enable power save mode on normal JPEG decoding.
6675     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST | JPD_PSAVE_EN);
6676 #else
6677     MDrv_JPD_Set_S_Config(reg_value | JPD_SWRST);
6678 #endif
6679 #endif
6680 #endif
6681 
6682 // protect test(only for unit test)
6683 #if (ENABLE_TEST_09_JPEGWriteProtectTest==false)
6684     //MDrv_JPD_SetWriteProtect(TRUE);
6685 #else
6686     #if(JPD_SUPPORT_AUTO_PROTECT==false)
6687     MDrv_JPD_SetWriteProtect(TRUE);
6688     #endif
6689 #endif
6690 
6691 #if (JPD_SUPPORT_AUTO_PROTECT==true)
6692     MDrv_JPD_SetAutoProtect(TRUE);
6693     #if (ENABLE_TEST_22_AutoProtectFailTest == true)
6694         MDrv_JPD_SetWPENEndAddr(MWC_BUFFER_ADDR+0x200-1);
6695         JPEG_DEBUG_API_MSG("Set Auto protect address =0x%lx!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", MWC_BUFFER_ADDR+0x200);
6696     #else
6697         MDrv_JPD_SetWPENEndAddr(MWC_BUFFER_ADDR+MWC_BUFFER_SIZE-1);
6698     #endif
6699 #endif
6700 
6701     MDrv_JPD_Set_S_Config(MDrv_JPD_Get_S_Config() | JPD_DEC_EN);
6702     JPEG_DEBUG_API_MSG("After Setting SCONFIG, JPD START!!\n");
6703     //JPEG_GO;
6704     return TRUE;
6705 }
6706 #endif
6707 
6708 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
6709 //------------------------------------------------------------------------------
6710 /******************************************************************************/
6711 ///Start Progressive JPEG decode for JPD
6712 /******************************************************************************/
JPEG_Progressive_Decode(void)6713 JPEG_STATIC JPEG_Result JPEG_Progressive_Decode(void)
6714 {
6715     if(_bProgressive_flag==FALSE)
6716         return (E_JPEG_FAILED);
6717 
6718     if ( _u16Total_lines_left == 0 )
6719     {
6720         return ( E_JPEG_OKAY );
6721     }
6722 
6723 #ifdef JPD_LONGJUMP_SUPPORT
6724     if ( setjmp( _jmp_state ) )
6725     {
6726         return ( E_JPEG_FAILED );
6727     }
6728 #endif
6729 
6730     //JPEG_DEBUG_API_MSG("_u16Mcus_per_row is %d!\n",_u16Mcus_per_row);
6731     //JPEG_DEBUG_API_MSG("_u16Mcus_per_col is %d!\n",_u16Mcus_per_col);
6732     //JPEG_DEBUG_API_MSG("_u8Blocks_per_mcu is %d!\n",_u8Blocks_per_mcu);
6733     //JPEG_DEBUG_API_MSG("gu8Max_mcu_x_size is %d!\n",gu8Max_mcu_x_size);
6734     //JPEG_DEBUG_API_MSG("gu8Max_mcu_y_size is %d!\n",gu8Max_mcu_y_size);
6735 
6736     if( _u16Total_lines_left > 0 )
6737     {
6738         JPEG_DEBUG_API_MSG("%s:_u16Total_lines_left = %d\n", __FUNCTION__ , _u16Total_lines_left);
6739         if(!JPEG_load_next_row())
6740             return E_JPEG_FAILED;
6741 
6742 #if SW_JPD_RGB_CMYK
6743         if((E_JPEG_CMYK == gu8Scan_type)
6744         || (E_JPEG_RGB == gu8Scan_type))
6745         {
6746             if(!msAPI_JPEG_transform_row()) //IDCT
6747             {
6748                 JPEG_DEBUG_API_MSG("CMYK and RGB decode failed!!\n");
6749                 return ( E_JPEG_FAILED );
6750             }
6751         }
6752 #endif
6753 
6754         _u16Total_lines_left -= gu8Max_mcu_y_size;
6755     }
6756 
6757     return (E_JPEG_DONE);
6758 }
6759 #endif
6760 
6761 //------------------------------------------------------------------------------
JPEG_GetAlignedResolution(MS_U16 * width,MS_U16 * height)6762 JPEG_STATIC void JPEG_GetAlignedResolution(MS_U16 *width, MS_U16 *height)
6763 {
6764     MS_U8 mcu_width, mcu_height;
6765     MS_U8 Y_VSF = _u8Comp_v_samp[0];
6766     MS_U8 Y_HSF = _u8Comp_h_samp[0];
6767 
6768     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
6769 
6770     *width = _u16OriginalImage_x_size;
6771     *height = _u16OriginalImage_y_size;
6772 
6773     mcu_width = _u16OriginalImage_x_size % (Y_HSF * 8);
6774     if (mcu_width)
6775     {
6776         *width += (Y_HSF * 8 - mcu_width);
6777     }
6778 
6779     mcu_height = _u16OriginalImage_y_size % (Y_VSF * 8);
6780     if (mcu_height)
6781     {
6782         *height += (Y_VSF * 8 - mcu_height);
6783     }
6784 
6785     JPEG_DEBUG_API_MSG("_u8Comp_v_samp = %d, _u8Comp_h_samp = %d\n", _u8Comp_v_samp[0], _u8Comp_h_samp[0]);
6786     JPEG_DEBUG_API_MSG("_u16OriginalImage_x_size = %d, _u16OriginalImage_y_size = %d\n", _u16OriginalImage_x_size, _u16OriginalImage_y_size);
6787     JPEG_DEBUG_API_MSG("AlignWidth = %d, AlignHeight = %d\n", *width, *height);
6788 }
6789 
6790 //-------------------------------------------------------------------------------------------------
6791 //  Global Functions
6792 //-------------------------------------------------------------------------------------------------
6793 #if 0
6794 //------------------------------------------------------------------------------
6795 /******************************************************************************/
6796 ///This will set MRC buffer address & size, MWC buffer address, and internal buffer
6797 ///address & size.
6798 ///@param *pInitParam \b The pointer of information for JPEG Buffer initialization
6799 /// structure {
6800 ///         U32 u32MRCBufAddr, \b IN MRC buffer address
6801 ///         U32 u32MRCBufSize, \b IN MRC buffer size
6802 ///         U32 u32MWCBufAddr, \b IN MWC buffer address
6803 ///                                 JPEG decompress data (YUV422)
6804 ///         U32 u32InternalBufAddr, \b IN internal buffer address
6805 ///                                 (exif:64K + memory pool)
6806 ///         U32 u32InternalBufSize, \b IN internal buffer size
6807 ///         BOOL bInitMem, \b IN initialize memory pool or not
6808 ///         };
6809 ///@return true or false
6810 /******************************************************************************/
6811 MS_BOOL MApi_JPEG_SetInitParameter(JPEG_InitParam *pInitParam)
6812 {
6813     // the buffer size must be multiple of 4 bytes
6814     if((!pInitParam)
6815     || (pInitParam->u32MRCBufSize < MIN_READBUFFER_SIZE))
6816     {
6817         JPEG_terminate(E_JPEG_READBUFFER_TOOSMALL);
6818         return false;
6819     }
6820     // the buffer size must be multiple of 8 bytes
6821     pInitParam->u32MRCBufSize = pInitParam->u32MRCBufSize & ~0x7;
6822 
6823     MRC_BUFFER_ADDR = pInitParam->u32MRCBufAddr | AEON_NON_CACHE_MASK;
6824     MWC_BUFFER_ADDR = pInitParam->u32MWCBufAddr | AEON_NON_CACHE_MASK;
6825     INTERNAL_BUFFER_ADDR = pInitParam->u32InternalBufAddr | AEON_NON_CACHE_MASK;
6826     MRC_BUFFER_SIZE = pInitParam->u32MRCBufSize;
6827     INTERNAL_BUFFER_SIZE = pInitParam->u32InternalBufSize;
6828 
6829     _pu8In_buf = (MS_U8 *) pInitParam->u32MRCBufAddr;
6830 
6831     if (pInitParam->bInitMem)
6832     {
6833         JPEG_MEMORY_init_mempool(((MS_U8 *)(pInitParam->u32InternalBufAddr + JPEG_DEFAULT_EXIF_SIZE))
6834                                 , (pInitParam->u32InternalBufSize - JPEG_DEFAULT_EXIF_SIZE));
6835     }
6836 
6837     return TRUE;
6838 }
6839 //------------------------------------------------------------------------------
6840 // Call get_error_code() after constructing to determine if the stream
6841 // was valid or not. You may call the get_width(), get_height(), etc.
6842 // methods after the constructor is called.
6843 // You may then either destruct the object, or begin decoding the image
6844 // by calling begin(), then decode().
6845 void MApi_JPEG_Constructor( PJPEG_FILE_FileSystem_t Pstream, MS_U8 decode_type)
6846 {
6847     if ( setjmp( _jmp_state ) )
6848     {
6849         return;
6850     }
6851 
6852     _u8DecodeType = decode_type;
6853     JPEG_init_thumbnail();
6854     JPEG_decode_init( Pstream);
6855 
6856 #ifdef CMODEL
6857     _bReady_flag = TRUE;
6858 #endif
6859 }
6860 #endif
6861 
6862 #if SW_JPD_RGB_CMYK
MApi_JPEG_Is_RGB_CMYK(void)6863 JPEG_STATIC MS_BOOL MApi_JPEG_Is_RGB_CMYK(void)
6864 {
6865     if (_u8Comps_in_frame == 3)
6866     {
6867         //support only U_H1V1 & V_H1V1
6868         if ((_u8Comp_h_samp[1] != 1 || _u8Comp_v_samp[1] != 1)
6869             || (_u8Comp_h_samp[2] != 1 || _u8Comp_v_samp[2] != 1))
6870         {
6871             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
6872             return FALSE;
6873         }
6874 
6875         if (_u8Comp_h_samp[0] == 1 && _u8Comp_v_samp[0] == 1)
6876         {
6877             if (_u8Comp_ident[0] == 82 || _u8Comp_ident[0] == 71 || _u8Comp_ident[0] == 66)
6878                 return TRUE; // RGB
6879         }
6880     }
6881     else
6882     if (_u8Comps_in_frame == 4) //handle YCCK & CMYK case, must distinguish YCCK and CMYK later
6883     {
6884         if (_u8Comp_h_samp[0] == 1 && _u8Comp_v_samp[0] == 1)
6885             return TRUE; // CMYK
6886     }
6887 
6888     return FALSE;
6889 }
6890 #endif
6891 
6892 static MS_BOOL bUseOJPD =FALSE;
6893 static MS_BOOL bOJPDOn = FALSE;
6894 
_MApi_JPEG_EnableOJPD(MS_BOOL bOnOff)6895 void _MApi_JPEG_EnableOJPD(MS_BOOL bOnOff)
6896 {
6897     bOJPDOn = bOnOff;
6898 }
6899 
_MApi_JPEG_Init_UsingOJPD(JPEG_InitParam * pInitParam)6900 JPEG_Result _MApi_JPEG_Init_UsingOJPD(JPEG_InitParam *pInitParam)
6901 {
6902     bUseOJPD = TRUE;
6903     return MApi_JPEG_Init(pInitParam);
6904 }
6905 
6906 //------------------------------------------------------------------------------
6907 /********************************************************************/
6908 ///This function will\n
6909 ///1. set MRC buffer address & size, MWC buffer address, and internal buffer address & size.\n
6910 ///2. power on JPD\n
6911 ///3. reset thumbnail parameters\n
6912 ///4. set the stream and its size, the number of components, etc.\n
6913 ///Call MApi_JPEG_GetErrorCode() after it to determine if the stream was valid or not.\n
6914 ///@param -pInitParam \b IN : \n
6915 /// structure {\n
6916 ///     MS_PHYADDR \b u32MRCBufAddr : MRC buffer address\n
6917 ///     MS_U32 \b u32MRCBufSize : MRC buffer size\n
6918 ///     MS_PHYADDR \b u32MWCBufAddr : MWC buffer address(JPEG decompress data (YUV422))\n
6919 ///     MS_U32 \b u32MWCBufSize : MWC buffer size\n
6920 ///     MS_PHYADDR \b u32InternalBufAddr : Internal buffer address(exif:64K + memory pool)\n
6921 ///     MS_U32 \b u32InternalBufSize : Internal buffer size\n
6922 ///     MS_U32 \b u32DecByteRead : how many byte read for JPEG decoder initialization\n
6923 ///     MS_BOOL \b bEOF : has read to the end of file\n
6924 ///     MS_U8 \b u8DecoderType : 3 types: JPEG main, thumbnail, or MJPEG\n
6925 ///     MS_BOOL \b bInitMem : initialize memory pool or not\n
6926 ///     JPEG_FillHdrFunc \b pFillHdrFunc : the function for fill header information\n
6927 /// };
6928 ///@return \b JPEG_Result : JPEG init status
6929 ///     - E_JPEG_OKAY : Success
6930 ///     - E_JPEG_FAILED : Failed
6931 /********************************************************************/
_MApi_JPEG_Init(JPEG_InitParam * pInitParam)6932 JPEG_Result _MApi_JPEG_Init(JPEG_InitParam *pInitParam)
6933 {
6934 #if 0
6935     MApi_JPEG_SetDbgLevel(E_JPEG_DEBUG_ALL);
6936 #endif
6937 
6938 #if PRINT_JPD_DECODE_TIME
6939     //printf("init WDT and timer1....\n");
6940     MDrv_WDT_Init((WDT_DbgLv)E_WDT_DBGLV_ERR_ONLY);
6941     MDrv_TIMER_Count(E_TIMER_1, 1);
6942 #endif
6943 
6944 #if LOG_DATA_TO_USB
6945     static MS_U8 fileName2[]={'/','u','s','b','/','s','d','a','1','/','l','o', 'g', '0', '0', '0', '.', 'b', 'i', 'n',0};
6946     fileName2[15]++;
6947     if(fileName2[15] > '9')
6948     {
6949         fileName2[14]++;
6950         fileName2[15] = '0';
6951     }
6952     if(fileName2[14] > '9')
6953     {
6954         fileName2[13]++;
6955         fileName2[14] = '0';
6956     }
6957 
6958     if((logBinfp = fopen((char *)fileName2, "wb"))==NULL)
6959     {
6960         printf("Can't open /usb/sda1/log.bin\n");
6961     }
6962     else
6963     {
6964         printf("open /usb/sda1/log.bin success\n");
6965     }
6966 #endif
6967 
6968     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
6969 
6970     // jpeg constructor
6971 #ifdef JPD_LONGJUMP_SUPPORT
6972     if ( setjmp( _jmp_state ) )
6973     {
6974         return  E_JPEG_FAILED;
6975     }
6976 #endif
6977 
6978     if(_bIsInit == TRUE)
6979     {
6980 #if SUPPORT_MPO_FORMAT
6981         if(MApi_JPEG_IsMPOFormat()==FALSE)
6982         {
6983             JPEG_terminate(E_JPEG_RE_INIT);
6984             return E_JPEG_FAILED;
6985         }
6986 #else
6987         JPEG_terminate(E_JPEG_RE_INIT);
6988         return E_JPEG_FAILED;
6989 #endif
6990     }
6991     else
6992     {
6993         _bIsInit = TRUE;
6994     }
6995 
6996     if(!pInitParam)
6997     {
6998         JPEG_terminate(E_JPEG_DECODE_ERROR);
6999         return E_JPEG_FAILED;
7000     }
7001 
7002 //    if(pInitParam->u32MRCBufSize < MIN_READBUFFER_SIZE)
7003     if(pInitParam->u32MRCBufSize < JPEG_DEFAULT_EXIF_SIZE)
7004     {
7005         JPEG_terminate(E_JPEG_READBUFFER_TOOSMALL);
7006         return E_JPEG_FAILED;
7007     }
7008 
7009     // the buffer size must be multiple of 8 bytes
7010     pInitParam->u32MRCBufSize = pInitParam->u32MRCBufSize & ~0x7;
7011 
7012     MRC_BUFFER_ADDR = MS_PA2KSEG1((MS_U32)pInitParam->u32MRCBufAddr);// | AEON_NON_CACHE_MASK;
7013     MWC_BUFFER_ADDR = MS_PA2KSEG1((MS_U32)pInitParam->u32MWCBufAddr);// | AEON_NON_CACHE_MASK;
7014     INTERNAL_BUFFER_ADDR = MS_PA2KSEG1((MS_U32)pInitParam->u32InternalBufAddr);// | AEON_NON_CACHE_MASK;
7015     if (__bIsMjpeg)
7016     {
7017         JPEG_DEBUG_API_MSG ("MJPEG Read buffer address CONVERT : 0x%lx - 0x%lx + 0x%lx ",
7018                 MS_VA2PA(MRC_BUFFER_ADDR), MS_VA2PA(__u32RealMjpegBase), MS_VA2PA(__u32TrickyMjpegBase));
7019 #if MJPEG_SW_PARSING_IN_MIU0
7020         MRC_BUFFER_ADDR -= __u32RealMjpegBase;
7021         MRC_BUFFER_ADDR += __u32TrickyMjpegBase;
7022 #endif
7023         JPEG_DEBUG_API_MSG ("= 0x%lx\n", MS_VA2PA(MRC_BUFFER_ADDR));
7024     }
7025     _pu8In_buf = (MS_U8 *)MRC_BUFFER_ADDR;//pInitParam->u32MRCBufAddr;
7026 
7027     MRC_BUFFER_SIZE = pInitParam->u32MRCBufSize;
7028     MWC_BUFFER_SIZE = pInitParam->u32MWCBufSize;
7029     INTERNAL_BUFFER_SIZE = pInitParam->u32InternalBufSize;
7030 
7031 
7032     JPEG_DEBUG_API_MSG("MRC_ADDR = 0x%lX, MRC_BUFFER_SIZE = 0x%lX\n", _u32ReadBufferAddr, _u32ReadBufferSize);
7033     JPEG_DEBUG_API_MSG("MWC_ADDR = 0x%lX, MWC_BUFFER_SIZE = 0x%lX\n", _u32WriteBufferAddr, _u32WriteBufferSize);
7034     JPEG_DEBUG_API_MSG("INTERNAL_ADDR = 0x%lX, INTERNAL_BUFFER_SIZE = 0x%lX\n", _u32InternalBufferAddr, _u32InternalBufferSize);
7035     JPEG_DEBUG_API_MSG ("MRC_PA= 0x%lx\n", MS_VA2PA(MRC_BUFFER_ADDR));
7036     JPEG_DEBUG_API_MSG ("MWC_PA= 0x%lx\n", MS_VA2PA(MWC_BUFFER_ADDR));
7037     JPEG_DEBUG_API_MSG ("INTER_PA= 0x%lx\n", MS_VA2PA(INTERNAL_BUFFER_ADDR));
7038 
7039     _pFillHdrFunc = pInitParam->pFillHdrFunc;
7040 
7041 #if SW_JPD_RGB_CMYK
7042     u8Out_buf = (MS_U8 *) MWC_BUFFER_ADDR;
7043     u32_Decode_Line = 0;
7044 #endif
7045 
7046     //JPEG_GO;
7047 
7048     // powner on JPD
7049 #ifndef CMODEL
7050     MDrv_JPD_PowerOn();
7051 #endif
7052     JPEG_DEBUG_API_MSG("MDrv_JPD_PowerOn OK!!\n");
7053 
7054     _u8DecodeType = pInitParam->u8DecodeType;
7055 
7056     _u32In_buf_left = pInitParam->u32DecByteRead;
7057     _bEOF_flag = pInitParam->bEOF;
7058 
7059     JPEG_DEBUG_API_MSG("@1,  _pu8In_buf_ofs=0x%lx, _u32In_buf_left=0x%lx, _bEOF_flag=%d========\n",
7060     (MS_U32)_pu8In_buf_ofs, (MS_U32)_u32In_buf_left, _bEOF_flag);
7061     // Try to get more bytes.
7062     if(!JPEG_force_fill_read_buffer(_u32In_buf_left))
7063     {
7064         JPEG_DEBUG_API_MSG("JPEG_force_fill_read_buffer failed!!!\n");
7065         _u32In_buf_left = pInitParam->u32DecByteRead;
7066     }
7067     JPEG_DEBUG_API_MSG("@2,  _pu8In_buf_ofs=0x%lx, _u32In_buf_left=0x%lx, _bEOF_flag=%d========\n",
7068     (MS_U32)_pu8In_buf_ofs, (MS_U32)_u32In_buf_left, _bEOF_flag);
7069 
7070 
7071 #if SUPPORT_MPO_FORMAT
7072     if(_u32In_buf_MPO_left ==0xFFFFFFFFUL)
7073     {
7074         _pu8In_buf_MPO_ofs = (MS_U8*)_pu8In_buf;
7075         _u32In_buf_MPO_left = _u32In_buf_left;
7076         mpo_load_data = TRUE;
7077         bIsMPOFormat = FALSE;
7078         u32MPFOffset = 0;
7079         _u32App2MarkerOffset = 0;
7080 
7081         //Init MPO database
7082         JPEG_InitMPO();
7083     }
7084 #endif
7085 
7086     u32DataOffset = 0;
7087     u32SOFOffset = 0;
7088 
7089     JPEG_init_thumbnail();
7090 
7091     if (_u8DecodeType == E_JPEG_TYPE_MJPEG)
7092     {
7093         __bIsMjpeg = TRUE;
7094 #if MJPEG_SW_PARSING_IN_MIU0
7095         __u32RealMjpegBase = MRC_BUFFER_ADDR;
7096         __u32TrickyMjpegBase = INTERNAL_BUFFER_ADDR;
7097 #endif
7098 
7099         return E_JPEG_OKAY;
7100     }
7101 
7102     if(!JPEG_decode_init())
7103         return E_JPEG_FAILED;
7104 
7105     if (bMHEG5)
7106     {
7107         if(_Error_code == E_JPEG_UNSUPPORTED_MARKER)
7108             return E_JPEG_FAILED;
7109     }
7110 
7111 #ifndef CMODEL
7112     //init mempool for progressive decoding.
7113 #if SW_JPD_RGB_CMYK
7114     if (pInitParam->bInitMem && (_bProgressive_flag || MApi_JPEG_Is_RGB_CMYK()))
7115 #else
7116     if (pInitParam->bInitMem && _bProgressive_flag)
7117 #endif
7118 #endif
7119     {
7120         if(!JPEG_MEMORY_init_mempool((void *)(INTERNAL_BUFFER_ADDR + JPEG_DEFAULT_EXIF_SIZE)
7121                                 , (INTERNAL_BUFFER_SIZE - JPEG_DEFAULT_EXIF_SIZE)))
7122         {
7123             JPEG_DEBUG_API_MSG("JPEG_MEMORY_init_mempool FAIL!!\n");
7124             return E_JPEG_FAILED;
7125         }
7126         JPEG_DEBUG_API_MSG("JPEG_MEMORY_init_mempool OK!!\n");
7127     }
7128 
7129     //JPEG_GO;
7130 
7131 #ifdef CMODEL
7132     _bReady_flag = TRUE;
7133 #endif
7134 
7135     return E_JPEG_OKAY;
7136 }
7137 //------------------------------------------------------------------------------
7138 /********************************************************************/
7139 ///Decode JPEG header
7140 ///@return \b JPEG_Result : JPEG header decode status
7141 ///     - E_JPEG_OKAY : Success
7142 ///     - E_JPEG_FAILED : Failed
7143 /********************************************************************/
_MApi_JPEG_DecodeHdr(void)7144 JPEG_Result _MApi_JPEG_DecodeHdr(void)
7145 {
7146 #if SW_JPD_RGB_CMYK
7147     MS_U8 mcu_width, mcu_height;
7148     MS_U16 pic_width = _u16Image_x_size;
7149     MS_U16 pic_height = _u16Image_y_size;
7150     MS_U8 Y_VSF = _u8Comp_v_samp[0];
7151     MS_U8 Y_HSF = _u8Comp_h_samp[0];
7152 #endif
7153 
7154 #if PRINT_JPD_DECODE_TIME
7155     u32MeasureDecodeTime =MDrv_TIMER_GetUs(E_TIMER_1);
7156     u32MeasureDecodeTimeSW = 0;
7157     u32MeasureDecodeTimeHW = 0;
7158 #endif
7159 
7160     if(_bIsInit == FALSE)
7161     {
7162         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7163         return E_JPEG_FAILED;
7164     }
7165 
7166     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7167 
7168 #ifdef JPD_LONGJUMP_SUPPORT
7169     if ( setjmp( _jmp_state ) )
7170     {
7171         return ( E_JPEG_FAILED );
7172     }
7173 #endif
7174 
7175     if(!JPEG_init_frame())
7176     {
7177         JPEG_DEBUG_API_ERR("%s [%d]\n", __FUNCTION__, __LINE__);
7178         return E_JPEG_FAILED;
7179     }
7180 
7181     if ( _bProgressive_flag )
7182     {
7183 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
7184         if(!JPEG_init_progressive())
7185             return E_JPEG_FAILED;
7186 #endif
7187     }
7188     else
7189     {
7190         if(!JPEG_init_sequential())
7191             return E_JPEG_FAILED;
7192     }
7193 
7194 #if SW_JPD_RGB_CMYK
7195     if((E_JPEG_CMYK == gu8Scan_type)
7196     || (E_JPEG_RGB == gu8Scan_type))
7197     {
7198         if ( ( mcu_width = pic_width % ( Y_HSF * 8 ) ) )
7199         {
7200             pic_width += ( Y_HSF * 8 - mcu_width );
7201         }
7202 
7203         if ( ( mcu_height = pic_height% ( Y_VSF * 8 ) ) )
7204         {
7205             pic_height += ( Y_VSF * 8 - mcu_height );
7206         }
7207 
7208         JPEG_DEBUG_API_MSG("Y_HSF = %d and Y_VSF = %d\n",Y_HSF, Y_VSF);
7209 
7210         JPEG_DEBUG_API_MSG("mcu_width = %d and mcu_height = %d\n",mcu_width, mcu_height);
7211 
7212         _u16NonAlignmentImage_x_size = pic_width;
7213         _u16NonAlignmentImage_y_size = _u16Image_y_size;
7214         _u16Image_x_size = pic_width;
7215         _u16Image_y_size = pic_height;
7216 
7217         JPEG_DEBUG_API_MSG("Width = %d and Height = %d\n",JPEG_MAX_WIDTH, JPEG_MAX_HEIGHT);
7218 
7219         //Set the Scale down variable
7220         if(_u16Image_x_size > JPEG_MAX_WIDTH*4 || _u16Image_y_size>JPEG_MAX_HEIGHT*4)
7221         {
7222             _u8ScaleDownFactor = E_JPD_DOWNSCALE_EIGHTH;
7223             _u16Image_x_size= (_u16Image_x_size/ 64)*64;
7224             _u16Image_x_size = _u16Image_x_size/ 8;
7225             _u16Image_y_size = _u16Image_y_size/ 8;
7226             JPEG_DEBUG_API_MSG("down scale 1/8!!\n");
7227         }
7228         else if(_u16Image_x_size > JPEG_MAX_WIDTH*2 || _u16Image_y_size>JPEG_MAX_HEIGHT*2)
7229         {
7230             _u8ScaleDownFactor = E_JPD_DOWNSCALE_FOURTH;
7231             _u16Image_x_size= (_u16Image_x_size/ 32)*32;
7232             _u16Image_x_size = _u16Image_x_size/ 4;
7233             _u16Image_y_size = _u16Image_y_size/ 4;
7234              JPEG_DEBUG_API_MSG("down scale 1/4!!\n");
7235         }
7236         else if (_u16Image_x_size > JPEG_MAX_WIDTH || _u16Image_y_size>JPEG_MAX_HEIGHT)
7237         {
7238             _u8ScaleDownFactor = E_JPD_DOWNSCALE_HALF;
7239             _u16Image_x_size= (_u16Image_x_size/ 16)*16;
7240             _u16Image_x_size = _u16Image_x_size/ 2;
7241             _u16Image_y_size = _u16Image_y_size/ 2;
7242              JPEG_DEBUG_API_MSG("down scale 1/2!!\n");
7243         }
7244         else
7245         {
7246              JPEG_DEBUG_API_MSG("down scale 1/1!!\n");
7247             _u8ScaleDownFactor = E_JPD_DOWNSCALE_ORG;
7248 
7249         }
7250 
7251         _u16PaddingMcuNumber = (_u16Image_x_size << _u8ScaleDownFactor)/gu8Max_mcu_x_size;
7252         _u32Max_blocks_per_row = _u16PaddingMcuNumber* _u16Max_blocks_per_mcu;
7253 
7254         JPEG_DEBUG_API_MSG("down scale width : %d\n",_u16Image_x_size);
7255         JPEG_DEBUG_API_MSG("down scale height : %d\n",_u16Image_y_size);
7256         JPEG_DEBUG_API_MSG("down scale MCU : %d\n",_u16PaddingMcuNumber);
7257         JPEG_DEBUG_API_MSG("down scale MCU : %ld\n",_u32Max_blocks_per_row);
7258     }
7259 #endif
7260 
7261     if (__bIsMjpeg)
7262     {
7263         JPEG_DEBUG_API_MSG ("MJPEG Read buffer address RECOVER : 0x%lx - 0x%lx + 0x%lx ",
7264                 MS_VA2PA(MRC_BUFFER_ADDR), MS_VA2PA(__u32TrickyMjpegBase), MS_VA2PA(__u32RealMjpegBase));
7265 #if MJPEG_SW_PARSING_IN_MIU0
7266         MRC_BUFFER_ADDR -= __u32TrickyMjpegBase;
7267         MRC_BUFFER_ADDR += __u32RealMjpegBase;
7268 #endif
7269         JPEG_DEBUG_API_MSG ("= 0x%lx\n", MS_VA2PA(MRC_BUFFER_ADDR));
7270     }
7271 
7272     return ( E_JPEG_OKAY );
7273 }
7274 //------------------------------------------------------------------------------
7275 /********************************************************************/
7276 ///Decode JPEG data (baseline & progressive)
7277 ///@return \b JPEG_Result : JPEG decode status
7278 ///     - E_JPEG_DONE : For progressive decoding one line done.
7279 ///     - E_JPEG_OKAY : Success
7280 ///     - E_JPEG_FAILED : Failed
7281 /********************************************************************/
_MApi_JPEG_Decode(void)7282 JPEG_Result _MApi_JPEG_Decode(void)
7283 {
7284     JPEG_Result retVal = E_JPEG_FAILED;
7285 
7286     if(_bIsInit == FALSE)
7287     {
7288         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7289         return E_JPEG_FAILED;
7290     }
7291 
7292     JPEG_DEBUG_API_MSG("%s: _bProgressive_flag = %d\n", __FUNCTION__ , _bProgressive_flag);
7293 
7294     if(_bProgressive_flag)
7295     {
7296 #if SUPPORT_PROGRESSIVE_MODE || defined(CMODEL)
7297         retVal = JPEG_Progressive_Decode();
7298 #endif
7299     }
7300     else
7301     {
7302 #if SW_JPD_RGB_CMYK
7303         if((E_JPEG_CMYK == gu8Scan_type)
7304         || (E_JPEG_RGB == gu8Scan_type))
7305         {
7306             retVal = msAPI_JPEG_Baseline_Decode();
7307             return retVal;
7308         }
7309 #endif
7310 
7311 #ifndef CMODEL
7312         JPEG_StartDecode();
7313 #if PRINT_JPD_DECODE_TIME
7314         u32MeasureDecodeTimeSW = MDrv_TIMER_GetUs(E_TIMER_1)-u32MeasureDecodeTime;
7315         u32MeasureDecodeTime = MDrv_TIMER_GetUs(E_TIMER_1);
7316 #endif
7317         if(_Error_code == E_JPEG_NO_ERROR)
7318             retVal = E_JPEG_OKAY;
7319         else
7320             retVal = E_JPEG_FAILED;
7321 #endif
7322     }
7323     return retVal;
7324 }
7325 
7326 #if LOG_DATA_TO_USB
_PrintOutputMem(MS_U32 u32addr,MS_U32 u32Size)7327 static void _PrintOutputMem(MS_U32 u32addr, MS_U32 u32Size)
7328 {
7329     u32Size = ALIGN_16(u32Size);
7330     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};
7331     fileName2[10]=MApi_JPEG_GetWidth()/1000+'0';
7332     fileName2[11]=(MApi_JPEG_GetWidth()/100)%10+'0';
7333     fileName2[12]=(MApi_JPEG_GetWidth()/10)%10+'0';
7334     fileName2[13]=MApi_JPEG_GetWidth()%10+'0';
7335     fileName2[15]=MApi_JPEG_GetHeight()/1000+'0';
7336     fileName2[16]=(MApi_JPEG_GetHeight()/100)%10+'0';
7337     fileName2[17]=(MApi_JPEG_GetHeight()/10)%10+'0';
7338     fileName2[18]=MApi_JPEG_GetHeight()%10+'0';
7339 
7340     fileName2[22]++;
7341     if(fileName2[22] > '9')
7342     {
7343         fileName2[21]++;
7344         fileName2[22] = '0';
7345     }
7346     if(fileName2[21] > '9')
7347     {
7348         fileName2[20]++;
7349         fileName2[21] = '0';
7350     }
7351 
7352     FILE *outBinfp;
7353     if((outBinfp = fopen((char *)fileName2, "wb"))==NULL)
7354     {
7355         printf("Can't open output yuyv file\n");
7356     }
7357     else
7358     {
7359         printf("open output yuyv file success\n");
7360     }
7361 
7362     fwrite((void *)u32addr, 1, u32Size, outBinfp);
7363     fclose(outBinfp);
7364 
7365 }
7366 #endif
7367 
7368 //------------------------------------------------------------------------------
7369 /********************************************************************/
7370 /// Free all block of JPD. Complete destroy the decoder object.
7371 /// It could be called at any time.
7372 ///@return None
7373 /********************************************************************/
_MApi_JPEG_Exit(void)7374 void _MApi_JPEG_Exit(void)
7375 {
7376     if(_bIsInit == FALSE)
7377     {
7378         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7379         return;
7380     }
7381     else
7382     {
7383         _bIsInit = FALSE;
7384     }
7385     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7386 #ifndef CMODEL
7387     JPEG_DEBUG_API_MSG("MApi_JPEG_GetCurReadStatus : \nCurVidx = %d, CurRow = %d, CurCol = %d ",
7388         MDrv_JPD_GetCurVidx(),
7389         MDrv_JPD_GetCurRow(),
7390         MDrv_JPD_GetCurCol());
7391     JPEG_DEBUG_API_MSG("CurMRCAddr = 0x%lx\n", MDrv_JPD_GetCurMRCAddr());
7392 
7393     MDrv_JPD_PowerOff();
7394 #endif
7395     JPEG_free_all_blocks();
7396 
7397 #if SW_JPD_RGB_CMYK
7398     bEnableCMYK = TRUE;
7399     bEnableRGB = TRUE;
7400 #endif
7401 
7402 #if SUPPORT_MPO_FORMAT
7403     _u32In_buf_MPO_left = 0xFFFFFFFFUL;         // reset _u32In_buf_MPO_left to U32_MAX
7404     bIsMPOFormat = FALSE;
7405 #endif
7406 
7407 #if LOG_DATA_TO_USB
7408 fclose(logBinfp);
7409 logBinfp = NULL;
7410 #endif
7411 
7412     JPEG_DEBUG_API_MSG("%s: done!!\n", __FUNCTION__);
7413     return;
7414 }
7415 //------------------------------------------------------------------------------
7416 /********************************************************************/
7417 ///Get JPEG decoder error code
7418 ///@return JPEG decoder error code
7419 /********************************************************************/
_MApi_JPEG_GetErrorCode(void)7420 JPEG_ErrCode _MApi_JPEG_GetErrorCode(void)
7421 {
7422     if(_bIsInit == FALSE)
7423     {
7424         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7425         return E_JPEG_NOT_INIT;
7426     }
7427     JPEG_DEBUG_API_MSG("%s: ErrCode = %d\n", __FUNCTION__ , _Error_code);
7428     return _Error_code;
7429 }
7430 //------------------------------------------------------------------------------
7431 #if 0 //move to APP
7432 /********************************************************************/
7433 ///Preload buffer from JPEG content
7434 ///The calling order must be HIGH -> LOW -> HIGH -> LOW....
7435 ///or LOW -> HIGH -> LOW -> HIGH.
7436 ///Since the file offset is maintained out side, we assume it won't be out of order.
7437 ///@param u8Type \b IN indicate which parts of buffer wants to load
7438 ///@return true or false
7439 /********************************************************************/
7440 MS_BOOL MApi_JPEG_PreLoadBuffer(MS_U8 u8Type)
7441 {
7442     MS_S32 bytes_read;
7443     MS_U8 *pBuffer_addr, Position;
7444     MS_U32 buf_left = 0;
7445 
7446     JPEG_DEBUG_API_MSG("MApi_JPEG_PreLoadBuffer: type = %d\n", u8Type);
7447 #if 1
7448     if(MAPi_JPEG_ProcessEOF(u8Type))
7449         return TRUE;
7450 #else
7451     if ( _bEOF_flag )
7452     {
7453         JPEG_DEBUG_API_MSG("end of file!!\n");
7454         MDrv_UART_PutChar('t'); // DO NOT MARK THIS, THIS FUNCTION MUST BE CALLED. OR THE JPD DECODE WILL FAIL for LARGE FILES
7455         if(u8Type==E_JPEG_BUFFER_HIGH)
7456         {
7457             // clear MRC high portion read complete event
7458             //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBH_DONE);
7459             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE);
7460         }
7461         else
7462         {
7463             // clear MRC low portion read complete event
7464             //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBL_DONE);
7465             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBL_DONE);
7466         }
7467         return TRUE;
7468     }
7469 #endif
7470 
7471     if ( setjmp( _jmp_state ) )
7472     {
7473         JPEG_DEBUG_API_MSG("ERROR\n");
7474         return ( FALSE );
7475     }
7476 
7477     if(u8Type==E_JPEG_BUFFER_HIGH)
7478     {
7479         pBuffer_addr = _pu8In_buf + (MRC_BUFFER_SIZE/2);
7480         Position = 1;
7481     }
7482     else
7483     {
7484         pBuffer_addr = _pu8In_buf;
7485         Position = 0;
7486     }
7487 
7488 #if 1
7489 {
7490     MS_BOOL EOF_flag = FALSE;
7491     do
7492     {
7493         bytes_read = JPEG_FILE_read( pBuffer_addr + buf_left, MRC_BUFFER_SIZE/2 - buf_left, &EOF_flag, _pStream, Position );
7494 
7495         if ( bytes_read == -1 )
7496         {
7497             JPEG_terminate( E_JPEG_STREAM_READ );
7498         }
7499 
7500         buf_left += bytes_read;
7501 //        _Total_Decoded_Size += bytes_read;
7502     } while(( buf_left < MRC_BUFFER_SIZE/2 ) && ( !EOF_flag ));
7503     MAPi_JPEG_FileReadInfo(buf_left, EOF_flag);
7504 }
7505 #else
7506     do
7507     {
7508         bytes_read = JPEG_FILE_read( pBuffer_addr + buf_left, MRC_BUFFER_SIZE/2 - buf_left, &_bEOF_flag, _pStream, Position );
7509 
7510         if ( bytes_read == -1 )
7511         {
7512             JPEG_terminate( E_JPEG_STREAM_READ );
7513         }
7514 
7515         buf_left += bytes_read;
7516 ////        _Total_Decoded_Size += bytes_read;
7517     } while(( buf_left < MRC_BUFFER_SIZE/2 ) && ( !_bEOF_flag ));
7518 #endif
7519 
7520 
7521 #if 1
7522     MApi_JPEG_SetMRBufferValid(u8Type);
7523 #else
7524     if(u8Type==E_JPEG_BUFFER_HIGH)
7525     {
7526         // clear MRC high portion read complete event
7527         //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBH_DONE);
7528         MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE);
7529         // mark high portion valid
7530         //MDrv_Write2Byte( BK_JPD_MCONFIG, (MDrv_Read2Byte(BK_JPD_MCONFIG) & ~0x0003) | JPD_H_VLD);
7531         MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | JPD_H_VLD);
7532     }
7533     else
7534     {
7535         // clear MRC low portion read complete event
7536         //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBL_DONE);
7537         MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBL_DONE);
7538         // mark low portion valid
7539         //MDrv_Write2Byte( BK_JPD_MCONFIG, (MDrv_Read2Byte(BK_JPD_MCONFIG) & ~0x0003) | JPD_L_VLD);
7540         MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | JPD_L_VLD);
7541     }
7542 #endif
7543     return TRUE;
7544 }
7545 #else // for APP reference
7546 #if 0
7547 MS_BOOL MApi_JPEG_PreLoadBuffer(MS_U8 u8MRBuffType, MS_U8* u8In_buf, MS_U32 MaxBufSize)
7548 {
7549     MS_S32 bytes_read;
7550     MS_U8 *pBuffer_addr, Position;
7551     MS_U32 buf_left = 0;
7552     MS_BOOL EOF_flag = FALSE;
7553 
7554     if(MAPi_JPEG_ProcessEOF(u8MRBuffType))
7555         return TRUE;
7556 
7557     if(MaxBufSize > (MRC_BUFFER_SIZE/2))
7558     {
7559         return FALSE;
7560     }
7561 
7562     if(u8MRBuffType==E_JPEG_BUFFER_HIGH)
7563     {
7564         pBuffer_addr = u8In_buf + (MRC_BUFFER_SIZE/2);
7565         Position = 1;
7566     }
7567     else
7568     {
7569         pBuffer_addr = u8In_buf;
7570         Position = 0;
7571     }
7572 
7573     do
7574     {
7575 //        bytes_read = JPEG_FILE_read( pBuffer_addr + buf_left, MRC_BUFFER_SIZE/2 - buf_left, &EOF_flag, _pStream, Position );
7576         bytes_read = JPEG_FILE_read( pBuffer_addr + buf_left, MaxBufSize - buf_left, &EOF_flag, _pStream, Position );
7577 
7578         if ( bytes_read == -1 )
7579         {
7580             MApi_JPEG_SetErrCode( E_JPEG_STREAM_READ );
7581             return FALSE;
7582         }
7583 
7584         buf_left += bytes_read;
7585 //        _Total_Decoded_Size += bytes_read;
7586     } while(( buf_left < MaxBufSize ) && ( !EOF_flag ));
7587     MAPi_JPEG_FileReadInfo(buf_left, EOF_flag);
7588     MApi_JPEG_SetMRBufferValid(u8MRBuffType);
7589     return TRUE;
7590 }
7591 #endif
7592 #endif
7593 //------------------------------------------------------------------------------
7594 /********************************************************************/
7595 ///Get JPD EVENT FLAG
7596 ///@return \b JPEG_Event : JPEG event flag
7597 /********************************************************************/
_MApi_JPEG_GetJPDEventFlag(void)7598 JPEG_Event _MApi_JPEG_GetJPDEventFlag(void)
7599 {
7600     if(_bIsInit == FALSE)
7601     {
7602         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7603         return E_JPEG_EVENT_DEC_ERROR_MASK;
7604     }
7605 
7606 #if SW_JPD_RGB_CMYK
7607     // It's pure software decode
7608     if((E_JPEG_CMYK == gu8Scan_type)
7609     || (E_JPEG_RGB == gu8Scan_type))
7610     {
7611         return E_JPEG_EVENT_DEC_DONE;
7612     }
7613 #endif
7614 
7615 #ifndef CMODEL
7616     {
7617         JPEG_Event reg_val = (JPEG_Event)MDrv_JPD_GetEventFlag();
7618         if(reg_val!=0)
7619         {
7620             JPEG_DEBUG_API_MSG("%s: reg_val = 0x%04X\n", __FUNCTION__ , reg_val);
7621         }
7622         if(E_JPEG_EVENT_DEC_DONE & reg_val)
7623         {
7624 #if PRINT_JPD_DECODE_TIME
7625             u32MeasureDecodeTimeHW = MDrv_TIMER_GetUs(E_TIMER_1)-u32MeasureDecodeTime;
7626             printf_red("SW decode header:[%ld]us, HW decode: [%ld]us\n", u32MeasureDecodeTimeSW, u32MeasureDecodeTimeHW);
7627 #endif
7628             if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API)
7629             {
7630                 verJPD_CRC32_Init();
7631                 verJPD_CRC32_Update((MS_U8 *)MWC_BUFFER_ADDR,
7632                     (MS_U32)MApi_JPEG_GetWidth()*MApi_JPEG_GetHeight()*2);
7633                 printf_red("[%dx%d], u32CRCResult=0x%lx\n",MApi_JPEG_GetWidth(), MApi_JPEG_GetHeight(), verJPD_CRC32_GetResult());
7634             }
7635 
7636 #if LOG_DATA_TO_USB
7637             if(MApi_JPEG_GetWidth() <=JPEG_MAX_WIDTH
7638                 && MApi_JPEG_GetHeight() <=JPEG_MAX_HEIGHT)
7639             {
7640                 MS_U32 u32Size;
7641                 u32Size = MApi_JPEG_GetWidth()*MApi_JPEG_GetHeight()*2;
7642                 printf("[%dx%d], MWC_BUFFER_ADDR=0x%lx, size=0x%lx", MApi_JPEG_GetWidth(),
7643                     MApi_JPEG_GetHeight(), MS_VA2PA(MWC_BUFFER_ADDR),u32Size);
7644                 _PrintOutputMem(MWC_BUFFER_ADDR, u32Size);
7645             }
7646 #endif
7647         }
7648         return reg_val;
7649     }
7650 #else
7651     return E_JPEG_EVENT_DEC_DONE;
7652 #endif
7653 }
7654 //------------------------------------------------------------------------------
7655 /********************************************************************/
7656 ///Set JPD EVENT FLAG
7657 ///@param -eEvtVal \b IN : JPEG_Event
7658 ///@return None
7659 /********************************************************************/
_MApi_JPEG_SetJPDEventFlag(JPEG_Event eEvtVal)7660 void _MApi_JPEG_SetJPDEventFlag(JPEG_Event eEvtVal)
7661 {
7662     if(_bIsInit == FALSE)
7663     {
7664         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7665         return;
7666     }
7667 
7668 #if SW_JPD_RGB_CMYK
7669     // It's pure software decode
7670     if((E_JPEG_CMYK == gu8Scan_type)
7671     || (E_JPEG_RGB == gu8Scan_type))
7672     {
7673         return;
7674     }
7675 #endif
7676 
7677 #ifndef CMODEL
7678     MDrv_JPD_SetEventFlag((MS_U16)eEvtVal);
7679 #endif
7680     return;
7681 }
7682 //------------------------------------------------------------------------------
7683 /********************************************************************/
7684 ///Reset JPD -- Reset must be called before trigger JPD\n
7685 ///This function always issue pause command and wait for both MRC & MWC\n
7686 ///becoming inactive, then reset JPD.\n
7687 ///\b NOTE: The pause function can be only used by Pluto/T1/T2... not include ERIS.
7688 ///@return None
7689 /********************************************************************/
_MApi_JPEG_Rst(void)7690 void _MApi_JPEG_Rst(void)
7691 {
7692     if(_bIsInit == FALSE)
7693     {
7694         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7695         return;
7696     }
7697     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7698 #ifndef CMODEL
7699     JPEG_DEBUG_API_MSG("MApi_JPEG_GetCurReadStatus : \nCurVidx = %d, CurRow = %d, CurCol = %d ",
7700         MDrv_JPD_GetCurVidx(),
7701         MDrv_JPD_GetCurRow(),
7702         MDrv_JPD_GetCurCol());
7703     JPEG_DEBUG_API_MSG("CurMRCAddr = 0x%lx\n", MDrv_JPD_GetCurMRCAddr());
7704 
7705     MDrv_JPD_Rst();
7706 #endif
7707     JPEG_DEBUG_API_MSG("%s:done!!\n", __FUNCTION__);
7708     return;
7709 }
7710 //------------------------------------------------------------------------------
7711 /********************************************************************/
7712 ///Power On JPEG decoder
7713 ///@return None
7714 /********************************************************************/
_MApi_JPEG_PowerOn(void)7715 void _MApi_JPEG_PowerOn(void)
7716 {
7717     if(_bIsInit == FALSE)
7718     {
7719         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7720         return;
7721     }
7722     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7723 #ifndef CMODEL
7724     MDrv_JPD_PowerOn();
7725 #endif
7726     return;
7727 }
7728 //------------------------------------------------------------------------------
7729 /********************************************************************/
7730 ///Power Off JPEG decoder
7731 ///@return None
7732 /********************************************************************/
_MApi_JPEG_PowerOff(void)7733 void _MApi_JPEG_PowerOff(void)
7734 {
7735     if(_bIsInit == FALSE)
7736     {
7737         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7738         return;
7739     }
7740     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7741 #ifndef CMODEL
7742     MDrv_JPD_PowerOff();
7743 #endif
7744     return;
7745 }
7746 //------------------------------------------------------------------------------
7747 /********************************************************************/
7748 ///Get current vertical line index written to memory
7749 ///@return Current vertical line index written to memory
7750 /********************************************************************/
_MApi_JPEG_GetCurVidx(void)7751 MS_U16 _MApi_JPEG_GetCurVidx(void) //new
7752 {
7753     if(_bIsInit == FALSE)
7754     {
7755         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7756         return 0;
7757     }
7758     JPEG_DEBUG_API_MSG("%s:\n", __FUNCTION__);
7759 #ifndef CMODEL
7760     return MDrv_JPD_GetCurVidx();
7761 #else
7762     return 0;
7763 #endif
7764 }
7765 //------------------------------------------------------------------------------
7766 /********************************************************************/
7767 ///Check current JPEG image is progressive or not
7768 ///@return TRUE / FALSE
7769 /// - TRUE : progressive
7770 /// - FALSE : baseline
7771 /********************************************************************/
_MApi_JPEG_IsProgressive(void)7772 MS_BOOL _MApi_JPEG_IsProgressive(void)
7773 {
7774     if(_bIsInit == FALSE)
7775     {
7776         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7777         return FALSE;
7778     }
7779     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _bProgressive_flag);
7780     return (_bProgressive_flag);
7781 }
7782 //------------------------------------------------------------------------------
7783 /********************************************************************/
7784 ///Check current JPEG image has thumbnail or not
7785 ///@return TRUE / FALSE
7786 /// - TRUE : thumbnail found
7787 /// - FALSE : no thumbnail
7788 /********************************************************************/
_MApi_JPEG_ThumbnailFound(void)7789 MS_BOOL _MApi_JPEG_ThumbnailFound(void)
7790 {
7791     if(_bIsInit == FALSE)
7792     {
7793         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7794         return FALSE;
7795     }
7796     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _bThumbnailFound);
7797     return (_bThumbnailFound);
7798 }
7799 //------------------------------------------------------------------------------
7800 /********************************************************************/
7801 ///The width may be the thumbnail or original image size, it based on decoding mode
7802 ///@return width
7803 /********************************************************************/
_MApi_JPEG_GetWidth(void)7804 MS_U16 _MApi_JPEG_GetWidth(void)
7805 {
7806     if(_bIsInit == FALSE)
7807     {
7808         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7809         return 0;
7810     }
7811     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16Image_x_size);
7812     return (_u16Image_x_size);
7813 }
7814 //------------------------------------------------------------------------------
7815 /********************************************************************/
7816 ///The height may be the thumbnail or original image size, it based on decoding mode
7817 ///@return height
7818 /********************************************************************/
_MApi_JPEG_GetHeight(void)7819 MS_U16 _MApi_JPEG_GetHeight(void)
7820 {
7821     if(_bIsInit == FALSE)
7822     {
7823         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7824         return 0;
7825     }
7826     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16Image_y_size);
7827     return (_u16Image_y_size);
7828 }
7829 //------------------------------------------------------------------------------
7830 /********************************************************************/
7831 ///Get the original width of this JPEG file after alignment
7832 ///@return original width
7833 /********************************************************************/
_MApi_JPEG_GetOriginalWidth(void)7834 MS_U16 _MApi_JPEG_GetOriginalWidth(void)
7835 {
7836     if(_bIsInit == FALSE)
7837     {
7838         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7839         return 0;
7840     }
7841     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16OriginalImage_x_size);
7842     return (_u16OriginalImage_x_size);
7843 }
7844 //------------------------------------------------------------------------------
7845 /********************************************************************/
7846 ///Get the original height of this JPEG file after alignment
7847 ///@return original height
7848 /********************************************************************/
_MApi_JPEG_GetOriginalHeight(void)7849 MS_U16 _MApi_JPEG_GetOriginalHeight(void)
7850 {
7851     if(_bIsInit == FALSE)
7852     {
7853         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7854         return 0;
7855     }
7856     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16OriginalImage_y_size);
7857     return (_u16OriginalImage_y_size);
7858 }
7859 //------------------------------------------------------------------------------
7860 /********************************************************************/
7861 ///Get the original width before alignment
7862 ///@return non-alignment width
7863 /********************************************************************/
_MApi_JPEG_GetNonAlignmentWidth(void)7864 MS_U16 _MApi_JPEG_GetNonAlignmentWidth(void)
7865 {
7866     if(_bIsInit == FALSE)
7867     {
7868         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7869         return 0;
7870     }
7871     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16NonAlignmentImage_x_size);
7872     return (_u16NonAlignmentImage_x_size);
7873 }
7874 //------------------------------------------------------------------------------
7875 /********************************************************************/
7876 ///Get the original height before alignment
7877 ///@return non-alignment height
7878 /********************************************************************/
_MApi_JPEG_GetNonAlignmentHeight(void)7879 MS_U16 _MApi_JPEG_GetNonAlignmentHeight(void)
7880 {
7881     if(_bIsInit == FALSE)
7882     {
7883         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7884         return 0;
7885     }
7886     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16NonAlignmentImage_y_size);
7887     return (_u16NonAlignmentImage_y_size);
7888 }
7889 //------------------------------------------------------------------------------
7890 /********************************************************************/
7891 ///Get the displayed pitch of JPEG image
7892 ///@return displayed pitch
7893 /********************************************************************/
_MApi_JPEG_GetAlignedPitch(void)7894 MS_U16 _MApi_JPEG_GetAlignedPitch(void)
7895 {
7896     if(_bIsInit == FALSE)
7897     {
7898         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7899         return 0;
7900     }
7901     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16AlignedImagePitch);
7902     return (_u16AlignedImagePitch);
7903 }
7904 
_MApi_JPEG_GetAlignedPitch_H(void)7905 MS_U16 _MApi_JPEG_GetAlignedPitch_H(void)
7906 {
7907     if(_bIsInit == FALSE)
7908     {
7909         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7910         return 0;
7911     }
7912     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16AlignedImagePitch_H);
7913     return (_u16AlignedImagePitch_H);
7914 }
7915 
7916 
7917 //------------------------------------------------------------------------------
7918 /********************************************************************/
7919 ///Get the displayed width of JPEG image
7920 ///@return displayed width
7921 /********************************************************************/
_MApi_JPEG_GetAlignedWidth(void)7922 MS_U16 _MApi_JPEG_GetAlignedWidth(void)
7923 {
7924     if(_bIsInit == FALSE)
7925     {
7926         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7927         return 0;
7928     }
7929     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16AlignedImageWidth);
7930     return (_u16AlignedImageWidth);
7931 }
7932 //------------------------------------------------------------------------------
7933 /********************************************************************/
7934 ///Get the displayed height of JPEG image
7935 ///@return displayed height
7936 /********************************************************************/
_MApi_JPEG_GetAlignedHeight(void)7937 MS_U16 _MApi_JPEG_GetAlignedHeight(void)
7938 {
7939     if(_bIsInit == FALSE)
7940     {
7941         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7942         return 0;
7943     }
7944     JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u16AlignedImageHeight);
7945     return (_u16AlignedImageHeight);
7946 }
7947 //------------------------------------------------------------------------------
7948 /********************************************************************/
7949 ///Get scale down factor\n
7950 ///Depending on the real picture width & height, it will automatically set scale down\n
7951 ///factor to meet maximum JPEG width & height allowed.
7952 ///@return scale down factor
7953 /********************************************************************/
_MApi_JPEG_GetScaleDownFactor(void)7954 MS_U8 _MApi_JPEG_GetScaleDownFactor(void)
7955 {
7956     if(_bIsInit == FALSE)
7957     {
7958         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
7959         return 0;
7960     }
7961 
7962 #if SW_JPD_RGB_CMYK
7963     if((E_JPEG_CMYK == gu8Scan_type)
7964     || (E_JPEG_RGB == gu8Scan_type))
7965     {
7966         JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u8ScaleDownFactor*2);
7967         return (_u8ScaleDownFactor*2);
7968     }
7969 #endif
7970     {
7971         JPEG_DEBUG_API_MSG("%s: %d\n", __FUNCTION__ , _u8ScaleDownFactor);
7972         return (_u8ScaleDownFactor);
7973     }
7974 }
7975 //------------------------------------------------------------------------------
7976 /********************************************************************/
7977 /// Set Max decoding resolution
7978 ///@param -u16Width \b IN : indicate max decoding width
7979 ///@param -u16Height \b IN : indicate max decoding height
7980 ///@return None
7981 /********************************************************************/
_MApi_JPEG_SetMaxDecodeResolution(MS_U16 u16Width,MS_U16 u16Height)7982 void _MApi_JPEG_SetMaxDecodeResolution(MS_U16 u16Width, MS_U16 u16Height)
7983 {
7984     JPEG_DEBUG_API_MSG("%s: Width = %d, Height = %d\n", __FUNCTION__, u16Width, u16Height);
7985     JPEG_MAX_WIDTH = u16Width;
7986     JPEG_MAX_HEIGHT = u16Height;
7987     return;
7988 }
7989 //------------------------------------------------------------------------------
7990 /********************************************************************/
7991 /// Set Max decoding resolution of Progressive JPEG
7992 ///@param -u16ProWidth \b IN : indicate max decoding width
7993 ///@param -u16ProHeight \b IN : indicate max decoding height
7994 ///@return None
7995 /********************************************************************/
_MApi_JPEG_SetProMaxDecodeResolution(MS_U16 u16ProWidth,MS_U16 u16ProHeight)7996 void _MApi_JPEG_SetProMaxDecodeResolution(MS_U16 u16ProWidth, MS_U16 u16ProHeight)
7997 {
7998     JPEG_DEBUG_API_MSG("%s: ProWidth = %d, ProHeight = %d\n", __FUNCTION__, u16ProWidth, u16ProHeight);
7999     JPEG_PRO_MAX_WIDTH = u16ProWidth;
8000     JPEG_PRO_MAX_HEIGHT = u16ProHeight;
8001     return;
8002 }
8003 //------------------------------------------------------------------------------
8004 /********************************************************************/
8005 /// Valid the JPEG read buffer flag
8006 ///@param -u8MRBuffType \b IN : the type of JPEG read buffer
8007 ///@return None
8008 /********************************************************************/
_MApi_JPEG_SetMRBufferValid(JPEG_BuffLoadType u8MRBuffType)8009 void _MApi_JPEG_SetMRBufferValid(JPEG_BuffLoadType u8MRBuffType)
8010 {
8011     if(_bIsInit == FALSE)
8012     {
8013         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8014         return;
8015     }
8016     JPEG_DEBUG_API_MSG("%s: MRBuffType = %d\n", __FUNCTION__ , u8MRBuffType);
8017 #ifndef CMODEL
8018     switch(u8MRBuffType)
8019     {
8020         case E_JPEG_BUFFER_HIGH:
8021             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE);
8022             MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | JPD_H_VLD);
8023             break;
8024         case E_JPEG_BUFFER_LOW:
8025             MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBL_DONE);
8026             MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | JPD_L_VLD);
8027             break;
8028         default:
8029             break;
8030     }
8031 #endif
8032     return;
8033 }
8034 //------------------------------------------------------------------------------
8035 /********************************************************************/
8036 /// Set JPEG EOF flag and the total byte-read of JPEG file
8037 ///@param -u32DataRead \b IN : the amount of byte of file reading
8038 ///@param -bEOFflag \b IN : the EOF flag of file reading
8039 ///@return None
8040 /********************************************************************/
_MApi_JPEG_UpdateReadInfo(MS_U32 u32DataRead,MS_BOOL bEOFflag)8041 void _MApi_JPEG_UpdateReadInfo(MS_U32 u32DataRead, MS_BOOL bEOFflag)
8042 {
8043     if(_bIsInit == FALSE)
8044     {
8045         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8046         return;
8047     }
8048     JPEG_DEBUG_API_MSG("%s: DataRead = %ld, EOFflag = %d\n", __FUNCTION__ , u32DataRead, bEOFflag);
8049 ////    _Total_Decoded_Size += data_read;
8050     _u32Total_bytes_read += u32DataRead;
8051     _bEOF_flag = bEOFflag;
8052 
8053 #ifndef CMODEL
8054     // Set buffer valid to avoid last data not decoded done
8055     if(bEOFflag)
8056     {
8057         MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | (JPD_H_VLD |JPD_L_VLD));
8058     }
8059 #endif
8060 
8061     return;
8062 }
8063 //------------------------------------------------------------------------------
8064 /********************************************************************/
8065 /// Process JPEG read buffer flag when EOF.
8066 ///@param -u8MRBuffType \b IN : The type of JPEG read buffer
8067 ///@return None
8068 /********************************************************************/
_MApi_JPEG_ProcessEOF(JPEG_BuffLoadType u8MRBuffType)8069 void _MApi_JPEG_ProcessEOF(JPEG_BuffLoadType u8MRBuffType)
8070 {
8071     if(_bIsInit == FALSE)
8072     {
8073         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8074         return;
8075     }
8076     JPEG_DEBUG_API_MSG("%s: end of file!! MRBuffType = %d\n", __FUNCTION__ , u8MRBuffType);
8077 ////    MDrv_UART_PutChar('t'); // DO NOT MARK THIS, THIS FUNCTION MUST BE CALLED. OR THE JPD DECODE WILL FAIL for LARGE FILES
8078 #ifndef CMODEL
8079     if(u8MRBuffType == E_JPEG_BUFFER_HIGH)
8080     {
8081         // clear MRC high portion read complete event
8082         //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBH_DONE);
8083         MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBH_DONE);
8084     }
8085     else
8086     {
8087         // clear MRC low portion read complete event
8088         //MDrv_JPD_ClearJPDStatus(E_JPD_EVENT_MRBL_DONE);
8089         MDrv_JPD_SetEventFlag(E_JPD_EVENT_MRBL_DONE);
8090     }
8091 
8092     // Set buffer valid to avoid last data not decoded done
8093     MDrv_JPD_Set_M_Config((MDrv_JPD_Get_M_Config() & ~0x0003) | (JPD_H_VLD |JPD_L_VLD));
8094 #endif
8095     return;
8096 }
8097 //------------------------------------------------------------------------------
8098 /********************************************************************/
8099 /// Set JPEG Error Code if something error on upper layer.
8100 ///@param -ErrCode \b IN : The error type of JPEG decoder
8101 ///@return None
8102 /********************************************************************/
_MApi_JPEG_SetErrCode(JPEG_ErrCode ErrCode)8103 void _MApi_JPEG_SetErrCode(JPEG_ErrCode ErrCode)
8104 {
8105     JPEG_DEBUG_API_ERR("%s: ErrCode = %d\n", __FUNCTION__ , ErrCode);
8106     _Error_code = ErrCode;
8107 ////    JPEG_free_all_blocks();
8108     return;
8109 }
8110 //------------------------------------------------------------------------------
8111 /********************************************************************/
8112 /// Set JPEG debug level.
8113 ///@param -u8DbgLevel \b IN : The JPEG debug level, refer to JPEG_DbgLevel.
8114 ///@return None
8115 /********************************************************************/
_MApi_JPEG_SetDbgLevel(MS_U8 u8DbgLevel)8116 void _MApi_JPEG_SetDbgLevel(MS_U8 u8DbgLevel)
8117 {
8118     JPEG_DEBUG_API_MSG("%s: DbgLevel = 0x%04X\n", __FUNCTION__, u8DbgLevel);
8119     _u8JPEG_ApiDbgLevel = u8DbgLevel;
8120 #ifndef CMODEL
8121     MDrv_JPD_SetDbgLevel(u8DbgLevel);
8122 #endif
8123     return;
8124 }
8125 //------------------------------------------------------------------------------
8126 /********************************************************************/
8127 /// Get JPEG debug level.
8128 ///@return JPEG debug level, refer to JPEG_DbgLevel.
8129 /********************************************************************/
_MApi_JPEG_GetDbgLevel(void)8130 MS_U8 _MApi_JPEG_GetDbgLevel(void)
8131 {
8132     JPEG_DEBUG_API_MSG("%s: DbgLevel = 0x%04X\n", __FUNCTION__, _u8JPEG_ApiDbgLevel);
8133     return _u8JPEG_ApiDbgLevel;
8134 }
8135 //------------------------------------------------------------------------------
8136 /********************************************************************/
8137 /// Set MHEG5 Flag.
8138 ///@param -bEnable \b IN :
8139 ///@return None
8140 /********************************************************************/
_MApi_JPEG_SetMHEG5(MS_BOOL bEnable)8141 void _MApi_JPEG_SetMHEG5(MS_BOOL bEnable)
8142 {
8143     bMHEG5 = bEnable;
8144 }
8145 //------------------------------------------------------------------------------
8146 /********************************************************************/
8147 /// Get JPEG information
8148 ///@param -pJPEG_Info \b OUT : JPEG information\n
8149 /// structure {\n
8150 ///    MS_U8* \b pu8DrvVer : JPEG DRV version\n
8151 ///    MS_U8* \b pu8HalVer : JPEG HAL version\n
8152 ///    MS_U8* \b pu8FwVer : JPEG FW version\n
8153 ///    JPEG_DrvCap \b stDrvCap : JPEG driver capability\n
8154 ///           };
8155 ///@return None
8156 /********************************************************************/
_MApi_JPEG_GetInfo(JPEG_Info * pJPEG_Info)8157 void _MApi_JPEG_GetInfo(JPEG_Info *pJPEG_Info)
8158 {
8159 #ifndef CMODEL
8160     JPD_Info DrvInfo;
8161     const MSIF_Version *DrvVer;
8162     MDrv_JPD_GetInfo(&DrvInfo);
8163     MDrv_JPD_GetLibVer(&DrvVer);
8164     pJPEG_Info->pu8DrvVer = DrvVer;
8165     pJPEG_Info->pu8HalVer = DrvInfo.pu8HalVer;
8166     pJPEG_Info->pu8FwVer = DrvInfo.pu8FwVer;
8167     pJPEG_Info->stDrvCap.bBaseline = DrvInfo.stCap.bBaseline;
8168     pJPEG_Info->stDrvCap.bProgressive = DrvInfo.stCap.bProgressive;
8169     pJPEG_Info->stDrvCap.bMJPEG = DrvInfo.stCap.bMJPEG;
8170     pJPEG_Info->stDrvCap.u16MaxWidth = JPEG_MAX_WIDTH;
8171     pJPEG_Info->stDrvCap.u16MaxHeight = JPEG_MAX_HEIGHT;
8172     pJPEG_Info->stDrvCap.u16MaxProWidth = JPEG_PRO_MAX_WIDTH;
8173     pJPEG_Info->stDrvCap.u16MaxProHeight = JPEG_PRO_MAX_HEIGHT;
8174 
8175     JPEG_DEBUG_API_MSG("JPD %04x, %04x, %04x, %s\n",
8176         DrvVer->DDI.customer, DrvVer->DDI.model, DrvVer->DDI.chip, &(DrvVer->DDI.cpu));
8177     JPEG_DEBUG_API_MSG("pu8HalVer = %s, pu8FwVer = %s\n", pJPEG_Info->pu8HalVer, pJPEG_Info->pu8FwVer);
8178     JPEG_DEBUG_API_MSG("%s: DrvCap_BL = %d, DrvCap_PRO = %d, DrvCap_MJPEG = %d\n", __FUNCTION__,
8179         pJPEG_Info->stDrvCap.bBaseline, pJPEG_Info->stDrvCap.bProgressive, pJPEG_Info->stDrvCap.bMJPEG);
8180     JPEG_DEBUG_API_MSG("%s: MaxWidth = %d, MaxHeight = %d\n", __FUNCTION__,
8181         pJPEG_Info->stDrvCap.u16MaxWidth, pJPEG_Info->stDrvCap.u16MaxHeight);
8182     JPEG_DEBUG_API_MSG("%s: MaxProWidth = %d, MaxProHeight = %d\n", __FUNCTION__,
8183         pJPEG_Info->stDrvCap.u16MaxProWidth, pJPEG_Info->stDrvCap.u16MaxProHeight);
8184 #endif
8185     return;
8186 }
8187 //------------------------------------------------------------------------------
8188 /********************************************************************/
8189 /// Get JPEG status.
8190 ///@param -pJPEG_Status \b OUT : JPEG status\n
8191 /// structure {\n
8192 ///    MS_U32 \b u32CurMRCAddr : JPD current decode address\n
8193 ///    MS_U16 \b u16CurVidx : JPD current decode vertical index\n
8194 ///    MS_U16 \b u16CurRow : JPD current decode row\n
8195 ///    MS_U16 \b u16CurCol : JPD current decode column\n
8196 ///    MS_BOOL \b bDrvBusy : JPEG DRV busy status\n
8197 ///    MS_BOOL \b bIsrEnable : JPD ISR status\n
8198 ///           };
8199 ///@return None
8200 /********************************************************************/
_MApi_JPEG_GetStatus(JPEG_Status * pJPEG_Status)8201 void _MApi_JPEG_GetStatus(JPEG_Status *pJPEG_Status)
8202 {
8203 #ifndef CMODEL
8204     JPD_Status *pDrvStatus = MDrv_JPD_GetStatus();
8205     pJPEG_Status->u32CurMRCAddr = pDrvStatus->u32CurMRCAddr;
8206     pJPEG_Status->u16CurVidx = pDrvStatus->u16CurVidx;
8207     pJPEG_Status->u16CurRow = pDrvStatus->u16CurRow;
8208     pJPEG_Status->u16CurCol = pDrvStatus->u16CurCol;
8209     pJPEG_Status->bDrvBusy = pDrvStatus->bIsBusy;
8210     pJPEG_Status->bIsrEnable = pDrvStatus->bIsrEnable;
8211     JPEG_DEBUG_API_MSG("%s: Drv_busy = %d, Isr_status = %d\n", __FUNCTION__ , pJPEG_Status->bDrvBusy, pJPEG_Status->bIsrEnable);
8212     JPEG_DEBUG_API_MSG("%s: CurVidx = %d, CurRow = %d, CurCol = %d\n", __FUNCTION__ ,
8213                         pJPEG_Status->u16CurVidx, pJPEG_Status->u16CurRow, pJPEG_Status->u16CurCol);
8214     JPEG_DEBUG_API_MSG("%s: CurMRCAddr = 0x%lx\n", __FUNCTION__ , pJPEG_Status->u32CurMRCAddr);
8215 #endif
8216     return;
8217 }
8218 //------------------------------------------------------------------------------
8219 /********************************************************************/
8220 /// Get JPEG Version.
8221 ///@param -ppVersion \b OUT : JPEG Version
8222 ///@return \b JPEG_Result
8223 ///     - E_JPEG_OKAY : Success
8224 ///     - E_JPEG_FAILED : Failed
8225 /********************************************************************/
_MApi_JPEG_GetLibVer(const MSIF_Version ** ppVersion)8226 JPEG_Result _MApi_JPEG_GetLibVer(const MSIF_Version **ppVersion)
8227 {
8228     if (!ppVersion)
8229     {
8230         return E_JPEG_FAILED;
8231     }
8232     *ppVersion = &_api_jpeg_version;
8233     JPEG_DEBUG_API_MSG("%s: JPEG %04x, %04x, %04x, %s\n", __FUNCTION__ ,
8234         _api_jpeg_version.DDI.customer, _api_jpeg_version.DDI.model, _api_jpeg_version.DDI.chip, &(_api_jpeg_version.DDI.cpu));
8235     return E_JPEG_OKAY;
8236 }
8237 //------------------------------------------------------------------------------
8238 /********************************************************************/
8239 /// For H/W bug, some cases can not exit after decode done, Check
8240 /// Vidx to exit.
8241 ///@return \b JPEG_Result
8242 /// - E_JPEG_OKAY : Vidx checking is successful.
8243 /// - E_JPEG_FAILED : Vidx checking is failed.
8244 /********************************************************************/
_MApi_JPEG_HdlVidxChk(void)8245 JPEG_Result _MApi_JPEG_HdlVidxChk(void)
8246 {
8247 #ifndef CMODEL
8248     MS_U16 cur_vidx = 0;
8249     if(_bIsInit == FALSE)
8250     {
8251         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8252         _JPD_IsDecoding = FALSE;
8253         return E_JPEG_FAILED;
8254     }
8255 
8256     if(FALSE == _JPD_IsDecoding)
8257     {
8258         _JPD_IsDecoding = TRUE;
8259 #if MSOS_GET_SYSTEM_TIME
8260         _JPD_ReCheckTime = MsOS_GetSystemTime();
8261 #else
8262         _JPD_ReCheckTime = 0;
8263 #endif
8264     }
8265 
8266     //For H/W bug, some cases can not exit after decode done. Check Vidx to exit.
8267     cur_vidx = MDrv_JPD_GetCurVidx();
8268     if(_JPD_PreVIdx != cur_vidx)
8269     {
8270         _JPD_PreVIdx = cur_vidx;
8271 #if MSOS_GET_SYSTEM_TIME
8272         _JPD_ReCheckTime = MsOS_GetSystemTime();
8273 #else
8274         _JPD_ReCheckTime = 0;
8275 #endif
8276     }
8277     else
8278     {
8279 #if MSOS_GET_SYSTEM_TIME
8280         if((MsOS_GetSystemTime() - _JPD_ReCheckTime) >= 1000)
8281 #else
8282         if(_JPD_ReCheckTime++ >= 10000)
8283 #endif
8284         {
8285             _JPD_IsDecoding = FALSE;
8286             JPEG_DEBUG_API_ERR("Decode timeout...!!!!\n");
8287             return E_JPEG_FAILED;
8288         }
8289     }
8290 #endif
8291     return E_JPEG_OKAY;
8292 }
8293 //------------------------------------------------------------------------------
8294 /********************************************************************/
8295 /// Get Preload buffer information.
8296 ///@param -pBuffLoadType \b OUT : JPEG preload buffer type
8297 ///@return \b JPEG_Result
8298 /// - E_JPEG_OKAY : success.
8299 /// - E_JPEG_FAILED : fail.
8300 /// - E_JPEG_RETRY : we need to re-get preload buffer type
8301 /********************************************************************/
_MApi_JPEG_GetBuffLoadType(JPEG_BuffLoadType * pBuffLoadType)8302 JPEG_Result _MApi_JPEG_GetBuffLoadType(JPEG_BuffLoadType *pBuffLoadType)
8303 {
8304 #ifndef CMODEL
8305     MS_U16 reg_val = MDrv_JPD_GetEventFlag();
8306 
8307     if(_bIsInit == FALSE)
8308     {
8309         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8310         return E_JPEG_FAILED;
8311     }
8312 
8313     if((E_JPD_EVENT_MRBL_DONE & reg_val)
8314     && (E_JPEG_BUFFER_LOW != u8PreLHFlag))
8315     {
8316         JPEG_DEBUG_API_MSG("%s : MRBuffer Load LOW!!!!\n", __FUNCTION__);
8317         u8PreLHFlag = E_JPEG_BUFFER_LOW;
8318         u32MRCheckCount = 0;
8319         *pBuffLoadType = u8PreLHFlag;
8320         return E_JPEG_OKAY;
8321     }
8322     else if((E_JPD_EVENT_MRBH_DONE & reg_val)
8323     && (E_JPEG_BUFFER_HIGH != u8PreLHFlag))
8324     {
8325         JPEG_DEBUG_API_MSG("%s : MRBuffer Load HIGH!!!!\n", __FUNCTION__);
8326         u8PreLHFlag = E_JPEG_BUFFER_HIGH;
8327         u32MRCheckCount = 0;
8328         *pBuffLoadType = u8PreLHFlag;
8329         return E_JPEG_OKAY;
8330     }
8331     else
8332     {
8333         JPEG_DEBUG_API_MSG("%s : MRBuffer Load NONE!!!!\n", __FUNCTION__);
8334         *pBuffLoadType = E_JPEG_BUFFER_NONE;
8335         u32MRCheckCount++;
8336         if(u32MRCheckCount >= 1000)
8337         {
8338             JPEG_DEBUG_API_ERR("Decoding time out!!\n");
8339             u32MRCheckCount = 0;
8340             return E_JPEG_FAILED;
8341         }
8342     }
8343     return E_JPEG_RETRY;
8344 #else
8345     return E_JPEG_OKAY;
8346 #endif
8347 }
8348 //------------------------------------------------------------------------------
8349 /********************************************************************/
8350 /// Enable JPD ISR and register callback function.
8351 ///@param -IsrCb \b IN : JPD ISR callback function
8352 ///@return \b JPEG_Result
8353 /// - E_JPEG_OKAY : success.
8354 /// - E_JPEG_FAILED : fail.
8355 /********************************************************************/
_MApi_JPEG_EnableISR(JPEG_IsrFuncCb IsrCb)8356 JPEG_Result _MApi_JPEG_EnableISR(JPEG_IsrFuncCb IsrCb)
8357 {
8358 #ifndef CMODEL
8359     if(TRUE == MDrv_JPD_EnableISR((JPD_IsrFuncCb)IsrCb))
8360     {
8361         JPEG_DEBUG_API_MSG("%s : Success!!!!\n", __FUNCTION__);
8362         return E_JPEG_OKAY;
8363     }
8364     else
8365     {
8366         JPEG_DEBUG_API_MSG("%s : Fail!!!!\n", __FUNCTION__);
8367         return E_JPEG_FAILED;
8368     }
8369 #else
8370     return E_JPEG_OKAY;
8371 #endif
8372 }
8373 //------------------------------------------------------------------------------
8374 /********************************************************************/
8375 /// Disable JPD ISR and unregister callback function.
8376 ///@return \b JPEG_Result
8377 /// - E_JPEG_OKAY : success.
8378 /// - E_JPEG_FAILED : fail.
8379 /********************************************************************/
_MApi_JPEG_DisableISR(void)8380 JPEG_Result _MApi_JPEG_DisableISR(void)
8381 {
8382 #ifndef CMODEL
8383     if(TRUE == MDrv_JPD_DisableISR())
8384     {
8385         JPEG_DEBUG_API_MSG("%s : Success!!!!\n", __FUNCTION__);
8386         return E_JPEG_OKAY;
8387     }
8388     else
8389     {
8390         JPEG_DEBUG_API_MSG("%s : Fail!!!!\n", __FUNCTION__);
8391         return E_JPEG_FAILED;
8392     }
8393 #else
8394     return E_JPEG_OKAY;
8395 #endif
8396 }
8397 //------------------------------------------------------------------------------
8398 /********************************************************************/
8399 /// JPEG wait done function, call it to check JPEG decoding status.
8400 ///@return \b JPEG_DecodeStatus
8401 /// - E_JPEG_DEC_DONE : JPEG decode done.
8402 /// - E_JPEG_DEC_FAILED : JPEG decode failed.
8403 /// - E_JPEG_DEC_MRBL_DONE : MRB low done, need to feed data.
8404 /// - E_JPEG_DEC_MRBH_DONE : MRB high done, need to feed data.
8405 /// - E_JPEG_DEC_DECODING : JPEG decoding
8406 /********************************************************************/
_MApi_JPEG_WaitDone(void)8407 JPEG_DecodeStatus _MApi_JPEG_WaitDone(void)
8408 {
8409 #ifndef CMODEL
8410     MS_U16 reg_val = E_JPD_EVENT_DEC_DONE;
8411     MS_U16 cur_vidx = 0;
8412 #endif
8413 
8414     if(_bIsInit == FALSE)
8415     {
8416         JPEG_DEBUG_API_ERR("%s: JPD have not init!!\n", __FUNCTION__);
8417         _JPD_IsDecoding = FALSE;
8418         return E_JPEG_DEC_FAILED;
8419     }
8420 
8421 #ifndef CMODEL
8422     if(FALSE == _JPD_IsDecoding)
8423     {
8424         _JPD_IsDecoding = TRUE;
8425 #if MSOS_GET_SYSTEM_TIME
8426         _JPD_ReCheckTime = MsOS_GetSystemTime();
8427 #else
8428         _JPD_ReCheckTime = 0;
8429 #endif
8430     }
8431 
8432     //For H/W bug, some cases can not exit after decode done. Check Vidx to exit.
8433     cur_vidx = MDrv_JPD_GetCurVidx();
8434     if(_JPD_PreVIdx != cur_vidx)
8435     {
8436         _JPD_PreVIdx = cur_vidx;
8437 #if MSOS_GET_SYSTEM_TIME
8438         _JPD_ReCheckTime = MsOS_GetSystemTime();
8439 #else
8440         _JPD_ReCheckTime = 0;
8441 #endif
8442     }
8443     else
8444     {
8445 #if MSOS_GET_SYSTEM_TIME
8446         if((MsOS_GetSystemTime() - _JPD_ReCheckTime) >= 1000)
8447 #else
8448         if(_JPD_ReCheckTime++ >= 10000)
8449 #endif
8450         {
8451             JPEG_DEBUG_API_ERR("%s:Decode timeout.....!!!!\n", __FUNCTION__);
8452             _JPD_IsDecoding = FALSE;
8453             return E_JPEG_DEC_FAILED;
8454         }
8455         else
8456         {
8457             return E_JPEG_DEC_DECODING;
8458         }
8459     }
8460 
8461     reg_val = MDrv_JPD_GetEventFlag();
8462 
8463     if(E_JPD_EVENT_DEC_DONE & reg_val)
8464     {
8465         JPEG_DEBUG_API_MSG("%s:Decode Done!!\n", __FUNCTION__);
8466 #if PRINT_JPD_DECODE_TIME
8467         u32MeasureDecodeTimeHW = MDrv_TIMER_GetUs(E_TIMER_1)-u32MeasureDecodeTime;
8468         printf_red("SW decode header:[%ld]us, HW decode: [%ld]us\n", u32MeasureDecodeTimeSW, u32MeasureDecodeTimeHW);
8469 #endif
8470         if(_u8JPEG_ApiDbgLevel & E_JPEG_DEBUG_API)
8471         {
8472             verJPD_CRC32_Init();
8473             verJPD_CRC32_Update((MS_U8 *)MWC_BUFFER_ADDR,
8474                 (MS_U32)MApi_JPEG_GetWidth()*MApi_JPEG_GetHeight()*2);
8475             printf_red("[%dx%d], u32CRCResult=0x%lx\n",MApi_JPEG_GetWidth(), MApi_JPEG_GetHeight(), verJPD_CRC32_GetResult());
8476         }
8477 
8478 #if LOG_DATA_TO_USB
8479             if(MApi_JPEG_GetWidth() <=JPEG_MAX_WIDTH
8480                 && MApi_JPEG_GetHeight() <=JPEG_MAX_HEIGHT)
8481             {
8482                 MS_U32 u32Size;
8483                 u32Size = MApi_JPEG_GetWidth()*MApi_JPEG_GetHeight()*2;
8484                 printf("[%dx%d], MWC_BUFFER_ADDR=0x%lx, size=0x%lx", MApi_JPEG_GetWidth(),
8485                     MApi_JPEG_GetHeight(), MS_VA2PA(MWC_BUFFER_ADDR),u32Size);
8486                 _PrintOutputMem(MWC_BUFFER_ADDR, u32Size);
8487             }
8488 #endif
8489         _JPD_IsDecoding = FALSE;
8490         return E_JPEG_DEC_DONE;
8491     }
8492     else if(E_JPD_EVENT_RST_ERROR & reg_val)
8493     {
8494         JPEG_DEBUG_API_ERR("%s:RST Error!!\n", __FUNCTION__);
8495         _JPD_IsDecoding = FALSE;
8496         return E_JPEG_DEC_RST_ERROR;
8497     }
8498     else if((E_JPD_EVENT_ECS_ERROR & reg_val)
8499         || (E_JPD_EVENT_IS_ERROR & reg_val))
8500     {
8501         JPEG_DEBUG_API_ERR("%s:Bitstream Error, reg_val = 0x%02x!!\n", __FUNCTION__ , reg_val);
8502         _JPD_IsDecoding = FALSE;
8503         return E_JPEG_DEC_BITSTREAM_ERROR;
8504     }
8505 #if (JPD_SUPPORT_AUTO_PROTECT==true)
8506     else if(E_JPD_EVENT_MWB_FULL & reg_val)
8507     {
8508         JPEG_DEBUG_API_MSG("[%s]MWB is full!!\n", __FUNCTION__);
8509         _JPD_IsDecoding = FALSE;
8510         return E_JPEG_DEC_FAILED;
8511     }
8512 #endif
8513     else if((E_JPD_EVENT_MRBL_DONE & reg_val)
8514     && (E_JPEG_BUFFER_LOW != u8PreLHFlag))
8515     {
8516         u8PreLHFlag = E_JPEG_BUFFER_LOW;
8517         JPEG_DEBUG_API_MSG("[%s]MRBL done!!\n", __FUNCTION__);
8518         return E_JPEG_DEC_MRBL_DONE;
8519     }
8520     else if((E_JPD_EVENT_MRBH_DONE & reg_val)
8521     && (E_JPEG_BUFFER_HIGH != u8PreLHFlag))
8522     {
8523         u8PreLHFlag = E_JPEG_BUFFER_HIGH;
8524         JPEG_DEBUG_API_MSG("[%s]MRBH done!!\n", __FUNCTION__);
8525         return E_JPEG_DEC_MRBH_DONE;
8526     }
8527 #endif
8528     return E_JPEG_DEC_DECODING;
8529 }
8530 //------------------------------------------------------------------------------
8531 /********************************************************************/
8532 /// Get datetime info of JPEG EXIF
8533 ///@return \b JPEG_Result
8534 /// - E_JPEG_OKAY : success.
8535 /// - E_JPEG_FAILED : fail.
8536 /********************************************************************/
_MApi_JPEG_GetEXIFDateTime(JPEG_EXIF_DateTime * DateTime)8537 JPEG_Result _MApi_JPEG_GetEXIFDateTime(JPEG_EXIF_DateTime *DateTime)
8538 {
8539     if(_bIsInit == FALSE)
8540     {
8541         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8542         return E_JPEG_FAILED;
8543     }
8544 
8545     DateTime->bHasDataTime = _stEXIF_DateTime.bHasDataTime;
8546     DateTime->u32Year = _stEXIF_DateTime.u32Year;
8547     DateTime->u32Month = _stEXIF_DateTime.u32Month;
8548     DateTime->u32Day = _stEXIF_DateTime.u32Day;
8549     DateTime->u32Hour = _stEXIF_DateTime.u32Hour;
8550     DateTime->u32Minute = _stEXIF_DateTime.u32Minute;
8551     DateTime->u32Second = _stEXIF_DateTime.u32Second;
8552 
8553     JPEG_DEBUG_API_MSG("=======JPEG EXIF DateTime======= Exist = %d \n", _stEXIF_DateTime.bHasDataTime);
8554     JPEG_DEBUG_API_MSG("Year = %ld, Month = %ld, Day = %ld\n", _stEXIF_DateTime.u32Year, _stEXIF_DateTime.u32Month, _stEXIF_DateTime.u32Day);
8555     JPEG_DEBUG_API_MSG("Hour = %ld, Minute = %ld, Second = %ld\n", _stEXIF_DateTime.u32Hour, _stEXIF_DateTime.u32Minute, _stEXIF_DateTime.u32Second);
8556 
8557     return E_JPEG_OKAY;
8558 }
8559 //------------------------------------------------------------------------------
8560 /********************************************************************/
8561 /// Get orientation info of JPEG EXIF
8562 ///@return \b JPEG_Result
8563 /// - E_JPEG_OKAY : success.
8564 /// - E_JPEG_FAILED : fail.
8565 /********************************************************************/
_MApi_JPEG_GetEXIFOrientation(JPEG_EXIF_Orientation * eOrientation)8566 JPEG_Result _MApi_JPEG_GetEXIFOrientation(JPEG_EXIF_Orientation *eOrientation)
8567 {
8568     if(_bIsInit == FALSE)
8569     {
8570         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8571         return E_JPEG_FAILED;
8572     }
8573     *eOrientation = _eEXIF_Orientation;
8574     JPEG_DEBUG_API_MSG("JPEG EXIF Orientation = %d\n", _eEXIF_Orientation);
8575     return E_JPEG_OKAY;
8576 }
8577 
8578 #if SUPPORT_EXIF_EXTRA_INFO
_MApi_JPEG_GetEXIFManufacturer(MS_U8 * pu8Manufacturer,MS_U8 u8size)8579 JPEG_Result _MApi_JPEG_GetEXIFManufacturer(MS_U8 *pu8Manufacturer, MS_U8 u8size)
8580 {
8581     if(_bIsInit == FALSE)
8582     {
8583         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8584         return E_JPEG_FAILED;
8585     }
8586     MS_U8 u8i=0;
8587     JPEG_memset((void*)(pu8Manufacturer), 0, u8size);
8588     JPEG_DEBUG_API_MSG("JPEG EXIF Manufacturer =");
8589     while(_u8EXIF_Manufacturer[u8i] && u8i<JPEG_MANUFACTURER_SIZE)
8590     {
8591         *(pu8Manufacturer+u8i) =_u8EXIF_Manufacturer[u8i];
8592         JPEG_DEBUG_API_MSG("%c", *(pu8Manufacturer+u8i));
8593         u8i++;
8594         if(u8i>=u8size)
8595         {
8596             JPEG_DEBUG_API_ERR("%s: NOT enough space!!\n", __FUNCTION__);
8597             return E_JPEG_FAILED;
8598         }
8599     }
8600     JPEG_DEBUG_API_MSG("\n");
8601 //    JPEG_DEBUG_API_MSG("JPEG EXIF Manufacturer = %02x %02x %02x %02x %02x %02x %02x %02x\n",
8602 //        *pu8Manufacturer, *(pu8Manufacturer+1), *(pu8Manufacturer+2), *(pu8Manufacturer+3),
8603 //        *(pu8Manufacturer+4),*(pu8Manufacturer+5),*(pu8Manufacturer+6),*(pu8Manufacturer+7));
8604     return E_JPEG_OKAY;
8605 }
8606 
_MApi_JPEG_GetEXIFModel(MS_U8 * pu8Model,MS_U8 u8size)8607 JPEG_Result _MApi_JPEG_GetEXIFModel(MS_U8 *pu8Model, MS_U8 u8size)
8608 {
8609     if(_bIsInit == FALSE)
8610     {
8611         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8612         return E_JPEG_FAILED;
8613     }
8614     MS_U8 u8i=0;
8615     JPEG_memset((void*)(pu8Model), 0, u8size);
8616     JPEG_DEBUG_API_MSG("JPEG EXIF Model =");
8617     while(_u8EXIF_Model[u8i] && u8i<JPEG_MODEL_SIZE)
8618     {
8619         *(pu8Model+u8i) =_u8EXIF_Model[u8i];
8620         JPEG_DEBUG_API_MSG("%c", *(pu8Model+u8i));
8621         u8i++;
8622         if(u8i>=u8size)
8623         {
8624             JPEG_DEBUG_API_ERR("%s: NOT enough space!!\n", __FUNCTION__);
8625             return E_JPEG_FAILED;
8626         }
8627     }
8628     JPEG_DEBUG_API_MSG("\n");
8629 
8630 //    JPEG_DEBUG_API_MSG("JPEG EXIF Model = %02x %02x %02x %02x %02x %02x %02x %02x\n",
8631 //        *pu8Model, *(pu8Model+1), *(pu8Model+2), *(pu8Model+3),
8632 //        *(pu8Model+4),*(pu8Model+5),*(pu8Model+6),*(pu8Model+7));
8633     return E_JPEG_OKAY;
8634 }
8635 
_MApi_JPEG_GetEXIFFlash(MS_U16 * pu16Flash)8636 JPEG_Result _MApi_JPEG_GetEXIFFlash(MS_U16 *pu16Flash)
8637 {
8638     if(_bIsInit == FALSE)
8639     {
8640         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8641         return E_JPEG_FAILED;
8642     }
8643     *pu16Flash = _u16EXIF_Flash;
8644     JPEG_DEBUG_API_MSG("JPEG EXIF Flash = %d \n", *pu16Flash);
8645     return E_JPEG_OKAY;
8646 }
8647 
_MApi_JPEG_GetEXIFISOSpeedRatings(MS_U32 * pu32ISOSpeedRatings)8648 JPEG_Result _MApi_JPEG_GetEXIFISOSpeedRatings(MS_U32 *pu32ISOSpeedRatings)
8649 {
8650     if(_bIsInit == FALSE)
8651     {
8652         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8653         return E_JPEG_FAILED;
8654     }
8655     *pu32ISOSpeedRatings = _u32EXIF_ISOSpeedRatings;
8656     JPEG_DEBUG_API_MSG("JPEG EXIF ISO = %ld \n", *pu32ISOSpeedRatings);
8657     return E_JPEG_OKAY;
8658 }
8659 
_MApi_JPEG_GetEXIFShutterSpeedValue(JPEG_RATIONAL * pShutterSpeedValue)8660 JPEG_Result _MApi_JPEG_GetEXIFShutterSpeedValue(JPEG_RATIONAL *pShutterSpeedValue)
8661 {
8662     if(_bIsInit == FALSE)
8663     {
8664         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8665         return E_JPEG_FAILED;
8666     }
8667     pShutterSpeedValue->numerator = _stEXIF_ShutterSpeedValue.numerator;
8668     pShutterSpeedValue->denominator= _stEXIF_ShutterSpeedValue.denominator;
8669     JPEG_DEBUG_API_MSG("ShutterSpeedValue numerator=%ld, denominator=%ld\n", pShutterSpeedValue->numerator, pShutterSpeedValue->denominator);
8670     return E_JPEG_OKAY;
8671 }
8672 
_MApi_JPEG_GetEXIFApertureValue(JPEG_RATIONAL * pApertureValue)8673 JPEG_Result _MApi_JPEG_GetEXIFApertureValue(JPEG_RATIONAL *pApertureValue)
8674 {
8675     if(_bIsInit == FALSE)
8676     {
8677         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8678         return E_JPEG_FAILED;
8679     }
8680     pApertureValue->s_numerator = _stEXIF_ApertureValue.s_numerator;
8681     pApertureValue->s_denominator= _stEXIF_ApertureValue.s_denominator;
8682     JPEG_DEBUG_API_MSG("ApertureValue numerator=%ld, denominator=%ld\n", pApertureValue->s_numerator, pApertureValue->s_denominator);
8683     return E_JPEG_OKAY;
8684 }
8685 
8686 
_MApi_JPEG_GetEXIFExposureBiasValue(JPEG_RATIONAL * pExposureBiasValue)8687 JPEG_Result _MApi_JPEG_GetEXIFExposureBiasValue(JPEG_RATIONAL *pExposureBiasValue)
8688 {
8689     if(_bIsInit == FALSE)
8690     {
8691         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8692         return E_JPEG_FAILED;
8693     }
8694     pExposureBiasValue->numerator = _stEXIF_ExposureBiasValue.numerator;
8695     pExposureBiasValue->denominator= _stEXIF_ExposureBiasValue.denominator;
8696     JPEG_DEBUG_API_MSG("ExposureBiasValue numerator=%ld, denominator=%ld\n", pExposureBiasValue->numerator, pExposureBiasValue->denominator);
8697     return E_JPEG_OKAY;
8698 }
8699 
_MApi_JPEG_GetEXIFFocalLength(JPEG_RATIONAL * pFocalLength)8700 JPEG_Result _MApi_JPEG_GetEXIFFocalLength(JPEG_RATIONAL *pFocalLength)
8701 {
8702     if(_bIsInit == FALSE)
8703     {
8704         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8705         return E_JPEG_FAILED;
8706     }
8707     pFocalLength->s_numerator = _stEXIF_FocalLength.s_numerator;
8708     pFocalLength->s_denominator= _stEXIF_FocalLength.s_denominator;
8709     JPEG_DEBUG_API_MSG("FocalLength numerator=%ld, denominator=%ld\n", pFocalLength->s_numerator, pFocalLength->s_denominator);
8710     return E_JPEG_OKAY;
8711 }
8712 
_MApi_JPEG_GetEXIFImageWidth(MS_U32 * pu32ImageWidth)8713 JPEG_Result _MApi_JPEG_GetEXIFImageWidth(MS_U32 *pu32ImageWidth)
8714 {
8715     if(_bIsInit == FALSE)
8716     {
8717         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8718         return E_JPEG_FAILED;
8719     }
8720     *pu32ImageWidth = _u32EXIF_ImageWidth;
8721     JPEG_DEBUG_API_MSG("JPEG EXIF Image Width = %ld \n", *pu32ImageWidth);
8722     return E_JPEG_OKAY;
8723 }
8724 
_MApi_JPEG_GetEXIFImageHeight(MS_U32 * pu32ImageHeight)8725 JPEG_Result _MApi_JPEG_GetEXIFImageHeight(MS_U32 *pu32ImageHeight)
8726 {
8727     if(_bIsInit == FALSE)
8728     {
8729         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8730         return E_JPEG_FAILED;
8731     }
8732     *pu32ImageHeight = _u32EXIF_ImageHeight;
8733     JPEG_DEBUG_API_MSG("JPEG EXIF Image Height = %ld \n", *pu32ImageHeight);
8734     return E_JPEG_OKAY;
8735 }
_MApi_JPEG_GetEXIFExposureTime(JPEG_RATIONAL * pExposureTime)8736 JPEG_Result _MApi_JPEG_GetEXIFExposureTime(JPEG_RATIONAL *pExposureTime)
8737 {
8738     if(_bIsInit == FALSE)
8739     {
8740         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8741         return E_JPEG_FAILED;
8742     }
8743     pExposureTime->s_numerator = _stEXIF_ExposureTime.s_numerator;
8744     pExposureTime->s_denominator= _stEXIF_ExposureTime.s_denominator;
8745     JPEG_DEBUG_API_MSG("ExposureTime numerator=%ld, denominator=%ld\n", pExposureTime->s_numerator, pExposureTime->s_denominator);
8746     return E_JPEG_OKAY;
8747 }
8748 
8749 
_MApi_JPEG_GetEXIFFNumber(JPEG_RATIONAL * pFNumber)8750 JPEG_Result _MApi_JPEG_GetEXIFFNumber(JPEG_RATIONAL *pFNumber)
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     pFNumber->s_numerator = _stEXIF_FNumber.s_numerator;
8758     pFNumber->s_denominator= _stEXIF_FNumber.s_denominator;
8759     JPEG_DEBUG_API_MSG("FNumber numerator=%ld, denominator=%ld\n", pFNumber->s_numerator, pFNumber->s_denominator);
8760     return E_JPEG_OKAY;
8761 }
8762 #endif
8763 
8764 
_MApi_JPEG_GetControl(EN_JPEG_GET_CTRL_ID eGetID,MS_U32 * param,MS_U32 u32size)8765 JPEG_Result _MApi_JPEG_GetControl(EN_JPEG_GET_CTRL_ID eGetID, MS_U32 *param, MS_U32 u32size)
8766 {
8767     JPEG_Result eResult = E_JPEG_OKAY;
8768 
8769     if(_bIsInit == FALSE)
8770     {
8771         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
8772         return E_JPEG_FAILED;
8773     }
8774 
8775     switch(eGetID)
8776     {
8777         case E_JPEG_GET_EXIF_MANUFACTURER:
8778             eResult = MApi_JPEG_GetEXIFManufacturer((MS_U8 *)param, u32size*4);
8779             JPEG_DEBUG_API_MSG("@JPEG EXIF Manufacturer = %c%c%c%c%c%c%c%c%c%c%c%c\n",
8780                 *((MS_U8 *)param), *((MS_U8 *)param+1), *((MS_U8 *)param+2), *((MS_U8 *)param+3),
8781                 *((MS_U8 *)param+4),*((MS_U8 *)param+5),*((MS_U8 *)param+6),*((MS_U8 *)param+7),
8782                 *((MS_U8 *)param+8),*((MS_U8 *)param+9),*((MS_U8 *)param+10),*((MS_U8 *)param+11)
8783                 );
8784             break;
8785         case E_JPEG_GET_EXIF_MODEL:
8786             eResult = MApi_JPEG_GetEXIFModel((MS_U8 *)param, u32size*4);
8787             JPEG_DEBUG_API_MSG("@JPEG EXIF Model = %c%c%c%c%c%c%c%c%c%c%c%c\n",
8788                 *((MS_U8 *)param), *((MS_U8 *)param+1), *((MS_U8 *)param+2), *((MS_U8 *)param+3),
8789                 *((MS_U8 *)param+4),*((MS_U8 *)param+5),*((MS_U8 *)param+6),*((MS_U8 *)param+7),
8790                 *((MS_U8 *)param+8),*((MS_U8 *)param+9),*((MS_U8 *)param+10),*((MS_U8 *)param+11)
8791                 );
8792             break;
8793         case E_JPEG_GET_EXIF_FLASH:
8794             *param = (MS_U32)_u16EXIF_Flash;
8795             JPEG_DEBUG_API_MSG("@JPEG EXIF Flash = %ld \n", *param);
8796             break;
8797         case E_JPEG_GET_EXIF_ISO_SPEED_RATINGS:
8798             *param = _u32EXIF_ISOSpeedRatings;
8799             JPEG_DEBUG_API_MSG("@JPEG EXIF ISO = %ld \n", *param);
8800             break;
8801         case E_JPEG_GET_EXIF_SHUTTER_SPEED_VALUE:
8802             if(u32size<2)
8803             {
8804                 eResult = E_JPEG_FAILED;
8805             }
8806             *param = _stEXIF_ShutterSpeedValue.numerator;
8807             *(param+1) = _stEXIF_ShutterSpeedValue.denominator;
8808             JPEG_DEBUG_API_MSG("@ShutterSpeedValue numerator=%ld, denominator=%ld\n", *param, *(param+1));
8809             break;
8810         case E_JPEG_GET_EXIF_APERTURE_VALUE:
8811             if(u32size<2)
8812             {
8813                 eResult = E_JPEG_FAILED;
8814             }
8815             *param = (MS_U32)_stEXIF_ApertureValue.s_numerator;
8816             *(param+1) = (MS_U32)_stEXIF_ApertureValue.s_denominator;
8817             JPEG_DEBUG_API_MSG("@ApertureValue numerator=%ld, denominator=%ld\n", *param, *(param+1));
8818             break;
8819         case E_JPEG_GET_EXIF_EXPOSURE_BIAS_VALUE:
8820             if(u32size<2)
8821             {
8822                 eResult = E_JPEG_FAILED;
8823             }
8824             *param = _stEXIF_ExposureBiasValue.numerator;
8825             *(param+1) = _stEXIF_ExposureBiasValue.denominator;
8826             JPEG_DEBUG_API_MSG("@ExposureBiasValue numerator=%ld, denominator=%ld\n", *param, *(param+1));
8827             break;
8828         case E_JPEG_GET_EXIF_FOCAL_LENGTH:
8829             if(u32size<2)
8830             {
8831                 eResult = E_JPEG_FAILED;
8832             }
8833             *param = (MS_U32)_stEXIF_FocalLength.s_numerator;
8834             *(param+1) = (MS_U32)_stEXIF_FocalLength.s_denominator;
8835             JPEG_DEBUG_API_MSG("@FocalLength numerator=%ld, denominator=%ld\n", *param, *(param+1));
8836             break;
8837         case E_JPEG_GET_EXIF_IMAGE_WIDTH:
8838             *param = _u32EXIF_ImageWidth;
8839             JPEG_DEBUG_API_MSG("@JPEG EXIF Image Width = %ld \n", *param);
8840             break;
8841         case E_JPEG_GET_EXIF_IMAGE_HEIGHT:
8842             *param = _u32EXIF_ImageHeight;
8843             JPEG_DEBUG_API_MSG("@JPEG EXIF Image Height = %ld \n", *param);
8844             break;
8845         case E_JPEG_GET_EXIF_EXPOSURE_TIME:
8846             if(u32size<2)
8847             {
8848                 eResult = E_JPEG_FAILED;
8849             }
8850             *param = (MS_U32)_stEXIF_ExposureTime.s_numerator;
8851             *(param+1) = (MS_U32)_stEXIF_ExposureTime.s_denominator;
8852             JPEG_DEBUG_API_MSG("@ExposureTime numerator=%ld, denominator=%ld\n", *param, *(param+1));
8853             break;
8854         case E_JPEG_GET_EXIF_F_NUMBER:
8855             if(u32size<2)
8856             {
8857                 eResult = E_JPEG_FAILED;
8858             }
8859             *param = (MS_U32)_stEXIF_FNumber.s_numerator;
8860             *(param+1) = (MS_U32)_stEXIF_FNumber.s_denominator;
8861             JPEG_DEBUG_API_MSG("@FNumber numerator=%ld, denominator=%ld\n", *param, *(param+1));
8862             break;
8863         case E_JPEG_GET_EXIF_EXPOSURE_PROGRAM:
8864             *param = (MS_U32)_u16EXIF_Exposureprogram;
8865             JPEG_DEBUG_API_MSG("@JPEG EXIF Exposure Program = %ld \n", *param);
8866             break;
8867         default:
8868             eResult = E_JPEG_FAILED;
8869             break;
8870     }
8871     return eResult;
8872 }
8873 
8874 
8875 ////////////////////////////////////////////////////////////////////////////////////////
8876 ///   current unused function, maybe removed in new MDDI
8877 ////////////////////////////////////////////////////////////////////////////////////////
8878 #if 0 // not implement for new MDDI
8879 //------------------------------------------------------------------------------
8880 void msAPI_MJPEG_Parse_SOF(void)
8881 {
8882     JPEG_locate_sof_marker();
8883 }
8884 //------------------------------------------------------------------------------
8885 void msAPI_MJPEG_constructor( PJPEG_FILE_FileSystem_t Pstream, MS_U8 decode_type)
8886 {
8887     _u8DecodeType = decode_type;
8888     JPEG_init_thumbnail();
8889     msAPI_MJPEG_decode_init( Pstream);
8890 }
8891 //------------------------------------------------------------------------------
8892 //Set Total pics in the file
8893 void msAPI_MJPEG_SetTotal_Pics(MS_U32 *DecodeTime)
8894 {
8895     *DecodeTime = NumPics;
8896 }
8897 //------------------------------------------------------------------------------
8898 MS_S16 msAPI_JPEG_Check_End_Flag( void )
8899 {
8900     return ( _bEOF_flag );
8901 }
8902 
8903 #ifndef CMODEL
8904 //------------------------------------------------------------------------------
8905 /******************************************************************************/
8906 ///Set downscale ratio
8907 ///@param ratio \b OUT scale ratio
8908 /******************************************************************************/
8909 void msAPI_JPEG_SetDownScaleRatio(JPEG_DownScaleRatio ratio)
8910 {
8911     switch(ratio)
8912     {
8913     case E_RATIO_HALF:
8914         _u8DownScaleRatio = E_JPD_DOWNSCALE_HALF;
8915         break;
8916     case E_RATIO_FOURTH:
8917         _u8DownScaleRatio = E_JPD_DOWNSCALE_FOURTH;
8918         break;
8919     case E_RATIO_EIGHTH:
8920         _u8DownScaleRatio = E_JPD_DOWNSCALE_EIGHTH;
8921         break;
8922     default:
8923         _u8DownScaleRatio = E_JPD_DOWNSCALE_ORG;
8924         break;
8925     }
8926 }
8927 //------------------------------------------------------------------------------
8928 void msAPI_JPEG_GetOutputDimension(MS_U16 *width, MS_U16 *height)
8929 {
8930     MS_U8 mcu_width, mcu_height;
8931     MS_U16 pic_width = _u16Image_x_size;
8932     MS_U16 pic_height = _u16Image_y_size;
8933     MS_U8 Y_VSF = _u8Comp_v_samp[0];
8934     MS_U8 Y_HSF = _u8Comp_h_samp[0];
8935     MS_U16 ROI_width;
8936 
8937     if ( ( mcu_width = pic_width % ( Y_HSF * 8 ) ) )
8938     {
8939         pic_width += ( Y_HSF * 8 - mcu_width );
8940     }
8941 
8942     if ( ( mcu_height = pic_height% ( Y_VSF * 8 ) ) )
8943     {
8944         pic_height += ( Y_VSF * 8 - mcu_height );
8945     }
8946 
8947     ROI_width = pic_width;
8948 
8949     if(_u8DownScaleRatio==E_JPD_DOWNSCALE_FOURTH||_u8DownScaleRatio==E_JPD_DOWNSCALE_EIGHTH)
8950     {
8951         MS_U16 ROI_width;
8952 
8953         if(_u8DownScaleRatio==E_JPD_DOWNSCALE_FOURTH)
8954         {
8955             // width must be multiple of 32
8956             ROI_width = (pic_width/32)*32;
8957         }
8958         else if(_u8DownScaleRatio==E_JPD_DOWNSCALE_EIGHTH)
8959         {
8960             // width must be multiple of 64
8961             ROI_width = (pic_width/64)*64;
8962         }
8963     }
8964 
8965     switch(_u8DownScaleRatio)
8966     {
8967         case E_JPD_DOWNSCALE_HALF:
8968             *width = ROI_width/2;
8969             break;
8970 
8971         case E_JPD_DOWNSCALE_FOURTH:
8972             *width = ROI_width/4;
8973             break;
8974 
8975         case E_JPD_DOWNSCALE_EIGHTH:
8976             *width = ROI_width/8;
8977             break;
8978 
8979         default:
8980             *width = ROI_width;
8981             break;
8982     }
8983 
8984     *height = pic_height;
8985 }
8986 #endif
8987 //------------------------------------------------------------------------------
8988 MS_BOOL msAPI_MJPEG_SetInitParameter(MS_U32 u32RBufAddr, MS_U32 u32RBufSize, MS_U32 u32WBufAddr, MS_U32 u32IBufAddr, MS_U32 u32IBufSize)
8989 {
8990     // the buffer size must be multiple of 4 bytes
8991     //if((u32RBufSize%4)!=0)
8992      //   return FALSE;
8993     MS_U32 WBuffer = _u16Image_x_size*_u16Image_y_size*2;
8994 #ifdef CMODEL
8995     _bReady_flag = FALSE;
8996 #endif
8997     MRC_BUFFER_ADDR = _u32ReadBufferAddr + u32RBufAddr;
8998     JPEG_DEBUG_API_MSG("MRC_BUFFER_ADDR = %08lx\n",MRC_BUFFER_ADDR);
8999     //MRC_BUFFER_SIZE = _pu8In_buf + u32RBufSize;
9000    // JPEG_DEBUG_API_MSG("_u32WriteBufferAddr = %08X\n",_u32WriteBufferAddr);
9001    // JPEG_DEBUG_API_MSG("WBuffer = %08X\n",WBuffer);
9002     MWC_BUFFER_ADDR = _u32WriteBufferAddr + WBuffer;
9003     JPEG_DEBUG_API_MSG("MWC_BUFFER_ADDR = %08lx\n",MWC_BUFFER_ADDR);
9004     //JPEG_DEBUG_API_MSG("MWC_BUFFER_ADDR = %08X\n",MWC_BUFFER_ADDR);
9005     //INTERNAL_BUFFER_ADDR = u32IBufAddr;
9006     //INTERNAL_BUFFER_SIZE = u32IBufSize;
9007     //_u32In_buf_left = 0;
9008     _pu8In_buf = (MS_U8 *) (_u32ReadBufferAddr);
9009     _u32Total_bytes_read -= u32RBufAddr;
9010     //JPEG_MEMORY_init_mempool(((MS_U8 *) (u32IBufAddr + JPEG_DEFAULT_EXIF_SIZE)), u32IBufSize - JPEG_DEFAULT_EXIF_SIZE);
9011     //JPEG_locate_sof_marker();
9012 
9013     return TRUE;
9014 }
9015 //------------------------------------------------------------------------------
9016 MS_BOOL msAPI_MJPEG_ReSetMWC(MS_U32 u32RBufAddr, MS_U32 u32RBufSize, MS_U32 u32WBufAddr, MS_U32 u32IBufAddr, MS_U32 u32IBufSize)
9017 {
9018 
9019     _u32WriteBufferAddr = u32WBufAddr;
9020 
9021     JPEG_DEBUG_API_MSG("MWC_BUFFER_ADDR = %08lx\n",MWC_BUFFER_ADDR);
9022 
9023     return TRUE;
9024 }
9025 //------------------------------------------------------------------------------
9026 void msAPI_JPEG_SetErrorCode(MS_U16 u16Code)
9027 {
9028     _Error_code = u16Code;
9029 }
9030 #if 0
9031 #ifdef CMODEL
9032 //------------------------------------------------------------------------------
9033 // If you wish to decompress the image, call this method after constructing
9034 // the object. If E_JPEG_OKAY is returned you may then call decode() to
9035 // fetch the scan lines.
9036 JPEG_Result msAPI_JPEG_begin( void )
9037 {
9038     if ( _bReady_flag )
9039     {
9040         JPEG_DEBUG_API_MSG("Flag!!!!\n");
9041         return ( E_JPEG_OKAY );
9042     }
9043 
9044     if ( _Error_code )
9045     {
9046         JPEG_DEBUG_API_MSG("Error!!!!\n");
9047         return ( E_JPEG_FAILED );
9048     }
9049     JPEG_locate_sof_marker();
9050 
9051     //JPEG_decode_start();
9052 
9053     _bReady_flag = TRUE;
9054 
9055     return ( E_JPEG_OKAY );
9056 }
9057 #endif
9058 #endif
9059 #endif // not implement for new MDDI
9060 
9061 
9062 #if SW_JPD_RGB_CMYK
9063 // Decodes and dequantizes the next row of coefficients.
msAPI_JPEG_decode_next_row(void)9064 JPEG_STATIC MS_BOOL msAPI_JPEG_decode_next_row( void )
9065 {
9066     MS_U16 row_block = 0;
9067     MS_U16 mcu_row, mcu_block, k;
9068 
9069     // Clearing the entire row block buffer can take a lot of time!
9070     // Instead of clearing the entire buffer each row, keep track
9071     // of the number of nonzero entries written to each block and do
9072     // selective clears.
9073     //memset(_ps16Block_seg[0], 0, _u16Mcus_per_row * _u8Blocks_per_mcu * 64 * sizeof(BLOCK_TYPE));
9074 
9075     for ( mcu_row = 0; mcu_row < _u16Mcus_per_row; mcu_row++ )
9076     {
9077         if ( ( _u16Restart_interval ) && ( _u16Restarts_left == 0 ) )
9078         {
9079             if(!JPEG_process_restart())
9080                 return FALSE;
9081         }
9082 
9083         for ( mcu_block = 0; mcu_block < _u8Blocks_per_mcu; mcu_block++ )
9084         {
9085             MS_U8 component_id = _u8Mcu_org[mcu_block];
9086             MS_U8 prev_num_set;
9087             JPEG_HuffTbl *Ph;
9088 
9089             JPEG_BLOCK_TYPE *p = _ps16Block_seg[row_block];
9090             JPEG_QUANT_TYPE *q = _QuantTables[_u8Comp_quant[component_id]].s16Value;
9091             MS_S32 r, s;
9092 
9093             if ( ( s = JPEG_huff_decode( &_Huff_tbls[_u8Comp_dc_tab[component_id]] ) ) != 0 )
9094             {
9095                 r = JPEG_get_bits_2( s );
9096                 s = HUFF_EXTEND( r, s );
9097             }
9098 
9099             //printf("r : %d s : %d\n",s,r);
9100 
9101             _u32Last_dc_val[component_id] = ( s += _u32Last_dc_val[component_id] );
9102 
9103             p[0] = s * q[0];
9104 
9105             prev_num_set = _u8Block_max_zag_set[row_block];
9106 
9107             Ph = &_Huff_tbls[_u8Comp_ac_tab[component_id]];
9108 
9109             for ( k = 1; k < 64; k++ )
9110             {
9111                 s = JPEG_huff_decode( Ph );
9112 
9113                 //printf("Decode s :%d\n",s);
9114 
9115                 r = s >> 4;
9116                 s &= 15;
9117 
9118                 if ( s )
9119                 {
9120                     if ( r )
9121                     {
9122                         if ( ( k + r ) > 63 )
9123                         {
9124                             JPEG_terminate( E_JPEG_DECODE_ERROR );
9125                             return FALSE;
9126                         }
9127 
9128                         if ( k < prev_num_set )
9129                         {
9130                             MS_U32 n = MIN( r, prev_num_set - k );
9131                             MS_U16 kt = k;
9132                             while ( n-- )
9133                             {
9134                                 p[_u8ZAG[kt++]] = 0;
9135                             }
9136                         }
9137 
9138                         k += r;
9139                     }
9140 
9141                     r = JPEG_get_bits_2( s );
9142                     s = HUFF_EXTEND( r, s );
9143 
9144                     //assert(k < 64);
9145 
9146                     p[_u8ZAG[k]] = s * q[k];
9147                 }
9148                 else
9149                 {
9150                     if ( r == 15 )
9151                     {
9152                         if ( ( k + 15 ) > 63 )
9153                         {
9154                             JPEG_terminate( E_JPEG_DECODE_ERROR );
9155                             return FALSE;
9156                         }
9157 
9158                         if ( k < prev_num_set )
9159                         {
9160                             MS_U16 n = MIN( 16, prev_num_set - k );        //bugfix Dec. 19, 2001 - was 15!
9161                             MS_U16 kt = k;
9162                             while ( n-- )
9163                             {
9164                                 p[_u8ZAG[kt++]] = 0;
9165                             }
9166                         }
9167 
9168                         k += 15;
9169                     }
9170                     else
9171                     {
9172                         //while (k < 64)
9173                         //  p[ZAG[k++]] = 0;
9174 
9175                         break;
9176                     }
9177                 }
9178             }
9179 
9180             if ( k < prev_num_set )
9181             {
9182                 MS_U16 kt = k;
9183                 while ( kt < prev_num_set && kt < 64)           // kt < 64 is for coverity check
9184                 {
9185                     p[_u8ZAG[kt++]] = 0;
9186                 }
9187             }
9188 
9189             _u8Block_max_zag_set[row_block] = k;
9190 
9191             //block_num[row_block++] = k;
9192             row_block++;
9193         }
9194 
9195         _u16Restarts_left--;
9196     }
9197     return TRUE;
9198 }
9199 
9200 /*----------------------------------------------------------------------------*/
msAPI_JPEG_idct(JPEG_BLOCK_TYPE * data,MS_U8 * Pdst_ptr)9201 JPEG_STATIC void msAPI_JPEG_idct( JPEG_BLOCK_TYPE *data, MS_U8 *Pdst_ptr )
9202 {
9203     MS_S32 t0, t1, t2, t3;
9204     MS_S32 t10, t11, t12, t13;
9205     MS_S32 a1, a2, a3, a4, a5;
9206     MS_S32 rowctl;
9207     register JPEG_BLOCK_TYPE *pdata;
9208 
9209     //kevinhuang, use an internal array in idct to avoid memcpy to save time
9210     JPEG_BLOCK_TYPE workspace[64];
9211     JPEG_BLOCK_TYPE *wsptr;
9212 
9213     pdata = data;
9214     wsptr = workspace;
9215     for ( rowctl = 8 - 1; rowctl >= 0; rowctl-- )
9216     {
9217         if ( ( pdata[1] | pdata[2] | pdata[3] | pdata[4] | pdata[5] | pdata[6] | pdata[7] ) == 0 )
9218         {
9219             MS_S16 dc_val = ( MS_S16 )( pdata[0] << 2 );
9220 
9221             wsptr[0] = dc_val;
9222             wsptr[1] = dc_val;
9223             wsptr[2] = dc_val;
9224             wsptr[3] = dc_val;
9225             wsptr[4] = dc_val;
9226             wsptr[5] = dc_val;
9227             wsptr[6] = dc_val;
9228             wsptr[7] = dc_val;
9229 
9230             pdata += 8;       /* advance pointer to next row */
9231             wsptr += 8;
9232             continue;
9233         }
9234 
9235         a2 = ( MS_S32 )pdata[2];
9236         a3 = ( MS_S32 )pdata[6];
9237 
9238         a1 = (FIX_0_5411) * (a2 + a3);
9239         t2 = (-FIX_1_8477) * a3 + a1;
9240         t3 = (FIX_0_7653) * a2 + a1;
9241 
9242         t0 = ( ( MS_S32 )pdata[0] + ( MS_S32 )pdata[4] ) << 13;
9243         t1 = ( ( MS_S32 )pdata[0] - ( MS_S32 )pdata[4] ) << 13;
9244 
9245         t10 = t0 + t3;
9246         t13 = t0 - t3;
9247         t11 = t1 + t2;
9248         t12 = t1 - t2;
9249 
9250         t0 = ( MS_S32 )pdata[7];
9251         t1 = ( MS_S32 )pdata[5];
9252         t2 = ( MS_S32 )pdata[3];
9253         t3 = ( MS_S32 )pdata[1];
9254 
9255         a1 = t0 + t3;
9256         a2 = t1 + t2;
9257         a3 = t0 + t2;
9258         a4 = t1 + t3;
9259         a5 = (FIX_1_1758) * (a3 + a4);
9260 
9261         a1 = (-FIX_0_8999) * a1;
9262         a2 = (-FIX_2_5629) * a2;
9263         a3 = (-FIX_1_9615) * a3;
9264         a4 = (-FIX_0_3901) * a4;
9265         t0 = (FIX_0_2986) * t0;
9266         t1 = (FIX_2_0531) * t1;
9267         t2 = (FIX_3_0727) * t2;
9268         t3 = (FIX_1_5013) * t3;
9269 
9270         a3 += a5;
9271         a4 += a5;
9272 
9273         t0 += a1 + a3;
9274         t1 += a2 + a4;
9275         t2 += a2 + a3;
9276         t3 += a1 + a4;
9277 
9278         wsptr[0] = ( MS_S16 )DESCALE( t10 + t3, 13 - 2 );
9279         wsptr[1] = ( MS_S16 )DESCALE( t11 + t2, 13 - 2 );
9280         wsptr[2] = ( MS_S16 )DESCALE( t12 + t1, 13 - 2 );
9281         wsptr[3] = ( MS_S16 )DESCALE( t13 + t0, 13 - 2 );
9282         wsptr[4] = ( MS_S16 )DESCALE( t13 - t0, 13 - 2 );
9283         wsptr[5] = ( MS_S16 )DESCALE( t12 - t1, 13 - 2 );
9284         wsptr[6] = ( MS_S16 )DESCALE( t11 - t2, 13 - 2 );
9285         wsptr[7] = ( MS_S16 )DESCALE( t10 - t3, 13 - 2 );
9286 
9287         pdata += 8;
9288         wsptr += 8;
9289     }
9290 
9291     pdata = workspace;
9292     for ( rowctl = 8 - 1; rowctl >= 0; rowctl-- )
9293     {
9294         MS_S16 i;
9295 
9296         if ( ( pdata[8 * 1] | pdata[8 * 2] | pdata[8 * 3] | pdata[8 * 4] | pdata[8 * 5] | pdata[8 * 6] | pdata[8 * 7] ) == 0 )
9297         {
9298             MS_S16 dc_val = ( MS_S16 )DESCALE( ( MS_S32 )pdata[0], 2 + 3 ) + 128;
9299 
9300             clamp( dc_val );
9301 
9302             Pdst_ptr[8 * 0] = ( MS_U8 )dc_val;
9303 
9304 #if SW_OPTIMIZE
9305             if(_u8ScaleDownFactor == 3)
9306             {
9307                 pdata++;
9308                 Pdst_ptr++;
9309                 continue;
9310             }
9311 #endif
9312 
9313             Pdst_ptr[8 * 4] = ( MS_U8 )dc_val;
9314 
9315 #if SW_OPTIMIZE
9316             if(_u8ScaleDownFactor == 2)
9317             {
9318                 pdata++;
9319                 Pdst_ptr++;
9320                 continue;
9321             }
9322 #endif
9323 
9324             Pdst_ptr[8 * 2] = ( MS_U8 )dc_val;
9325             Pdst_ptr[8 * 6] = ( MS_U8 )dc_val;
9326 
9327 #if SW_OPTIMIZE
9328             if(_u8ScaleDownFactor == 1)
9329             {
9330                 pdata++;
9331                 Pdst_ptr++;
9332                 continue;
9333             }
9334 #endif
9335 
9336             Pdst_ptr[8 * 1] = ( MS_U8 )dc_val;
9337             Pdst_ptr[8 * 5] = ( MS_U8 )dc_val;
9338             Pdst_ptr[8 * 3] = ( MS_U8 )dc_val;
9339             Pdst_ptr[8 * 7] = ( MS_U8 )dc_val;
9340 
9341             pdata++;
9342             Pdst_ptr++;
9343             continue;
9344         }
9345 
9346         a2 = ( MS_S32 )pdata[8 * 2];
9347         a3 = ( MS_S32 )pdata[8 * 6];
9348 
9349         a1 = (FIX_0_5411) * (a2 + a3);
9350         t2 = (-FIX_1_8477) * a3 + a1;
9351         t3 = (FIX_0_7653) * a2 + a1;
9352 
9353         t0 = ( ( MS_S32 )pdata[8 * 0] + ( MS_S32 )pdata[8 * 4] ) << 13;
9354         t1 = ( ( MS_S32 )pdata[8 * 0] - ( MS_S32 )pdata[8 * 4] ) << 13;
9355 
9356         t10 = t0 + t3;
9357         t13 = t0 - t3;
9358         t11 = t1 + t2;
9359         t12 = t1 - t2;
9360 
9361         t0 = ( MS_S32 )pdata[8 * 7];
9362         t1 = ( MS_S32 )pdata[8 * 5];
9363         t2 = ( MS_S32 )pdata[8 * 3];
9364         t3 = ( MS_S32 )pdata[8 * 1];
9365 
9366         a1 = t0 + t3;
9367         a2 = t1 + t2;
9368         a3 = t0 + t2;
9369         a4 = t1 + t3;
9370         a5 = (FIX_1_1758) * (a3 + a4);
9371 
9372         a1 = (-FIX_0_8999) * a1;
9373         a2 = (-FIX_2_5629) * a2;
9374         a3 = (-FIX_1_9615) * a3;
9375         a4 = (-FIX_0_3901) * a4;
9376         t0 = (FIX_0_2986) * t0;
9377         t1 = (FIX_2_0531) * t1;
9378         t2 = (FIX_3_0727) * t2;
9379         t3 = (FIX_1_5013) * t3;
9380 
9381         a3 += a5;
9382         a4 += a5;
9383 
9384         t0 += a1 + a3;
9385         t1 += a2 + a4;
9386         t2 += a2 + a3;
9387         t3 += a1 + a4;
9388 
9389         i = ( MS_S16 )DESCALE( t10 + t3, 13 + 2 + 3 ) + 128;
9390         clamp( i )
9391         Pdst_ptr[8 * 0] = ( MS_U8 )i;
9392 
9393 #if SW_OPTIMIZE
9394         if(_u8ScaleDownFactor== 3)
9395         {
9396             pdata++;
9397             Pdst_ptr++;
9398             continue;
9399         }
9400 #endif
9401 
9402         i = ( MS_S16 )DESCALE( t13 - t0, 13 + 2 + 3 ) + 128;
9403         clamp( i )
9404         Pdst_ptr[8 * 4] = ( MS_U8 )i;
9405 
9406 #if SW_OPTIMIZE
9407         if(_u8ScaleDownFactor== 2)
9408         {
9409             pdata++;
9410             Pdst_ptr++;
9411             continue;
9412         }
9413 #endif
9414 
9415         i = ( MS_S16 )DESCALE( t12 + t1, 13 + 2 + 3 ) + 128;
9416         clamp( i )
9417         Pdst_ptr[8 * 2] = ( MS_U8 )i;
9418 
9419         i = ( MS_S16 )DESCALE( t11 - t2, 13 + 2 + 3 ) + 128;
9420         clamp( i )
9421         Pdst_ptr[8 * 6] = ( MS_U8 )i;
9422 
9423 #if SW_OPTIMIZE
9424         if(_u8ScaleDownFactor== 1)
9425         {
9426             pdata++;
9427             Pdst_ptr++;
9428             continue;
9429         }
9430 #endif
9431 
9432         i = ( MS_S16 )DESCALE( t11 + t2, 13 + 2 + 3 ) + 128;
9433         clamp( i )
9434         Pdst_ptr[8 * 1] = ( MS_U8 )i;
9435 
9436 
9437         i = ( MS_S16 )DESCALE( t13 + t0, 13 + 2 + 3 ) + 128;
9438         clamp( i )
9439         Pdst_ptr[8 * 3] = ( MS_U8 )i;
9440 
9441 
9442         i = ( MS_S16 )DESCALE( t12 - t1, 13 + 2 + 3 ) + 128;
9443         clamp( i )
9444         Pdst_ptr[8 * 5] = ( MS_U8 )i;
9445 
9446 
9447         i = ( MS_S16 )DESCALE( t10 - t3, 13 + 2 + 3 ) + 128;
9448         clamp( i )
9449         Pdst_ptr[8 * 7] = ( MS_U8 )i;
9450 
9451         pdata++;
9452         Pdst_ptr++;
9453     }
9454 }
9455 
msAPI_JPEG_Idct_CMYKConvert(void)9456 JPEG_STATIC MS_BOOL msAPI_JPEG_Idct_CMYKConvert(void)
9457 {
9458     JPEG_BLOCK_TYPE *Psrc_ptr = _ps16Block_seg[0];
9459     MS_U8 *Pdst_ptr = gpu8Sample_buf;
9460     MS_U8 *u8Start_Each_Block, *u8Start_Each_Row;
9461     MS_U32 u32_y_Blcok_Pos = 0, Max_Pixel_Per_Row;
9462     MS_S32 i;
9463     MS_U8  u8Row_in_block, k;
9464     MS_U16 u16Pixel_Per_Block_x = 8, u16Pixel_Per_Block_y = 8;
9465     MS_U16 u16Pixel_Per_Pack_row = 16, u16TotalBlock_Per_Row, u16ScaleDownStep = 1 << _u8ScaleDownFactor;
9466 
9467     u16TotalBlock_Per_Row = (_u32Max_blocks_per_row)/2;
9468 
9469     u16Pixel_Per_Pack_row = u16Pixel_Per_Pack_row >> _u8ScaleDownFactor;
9470     u16Pixel_Per_Block_x = u16Pixel_Per_Block_x/* >> _u8ScaleDownFactor*/;
9471     u16Pixel_Per_Block_y = u16Pixel_Per_Block_y >> _u8ScaleDownFactor;
9472 
9473 
9474     Max_Pixel_Per_Row = (u16TotalBlock_Per_Row*u16Pixel_Per_Block_x >> _u8ScaleDownFactor);
9475 
9476     //Get start position of each row
9477     u8Start_Each_Row = (u8Out_buf + (u32_Decode_Line*(Max_Pixel_Per_Row*u16Pixel_Per_Block_y)));
9478 
9479     for ( i = (MS_S32)_u32Max_blocks_per_row; i > 0;)
9480     {
9481         //===================GET Y1 BLOCKs=======================
9482         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9483 
9484         //Get start position of block
9485         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9486 
9487         //Write value to destination buffer
9488         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9489         {
9490             for(k=0;k<u16Pixel_Per_Block_y;k++)
9491             {
9492                 *(u8Start_Each_Block + (k*2) ) = 255 - Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9493             }
9494             u8Start_Each_Block += Max_Pixel_Per_Row;
9495         }
9496 
9497         //Skip U. V block for 422 format
9498         Psrc_ptr += 256;
9499         Pdst_ptr += 256;
9500         i-=4;
9501 
9502         if(i == 0)
9503         {
9504             //Already decoded all blocks
9505             continue;
9506         }
9507         else if(i < 0)
9508         {
9509             printf("GET BLOCK NUMBER ERROR!\n");
9510             JPEG_terminate( E_JPEG_DECODE_ERROR );
9511             return FALSE;
9512         }
9513 
9514         //Move y block position in a row
9515         u32_y_Blcok_Pos++;
9516         //===================GET Y2 BLOCKs=======================
9517         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9518 
9519         //Get start position of block
9520         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9521 
9522         //Write value to destination buffer
9523         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9524         {
9525             for(k=0;k<u16Pixel_Per_Block_y;k++)
9526             {
9527                 *(u8Start_Each_Block + (k*2) ) = 255 - Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9528             }
9529             u8Start_Each_Block += Max_Pixel_Per_Row;
9530         }
9531 
9532         //Move idct data position
9533         Psrc_ptr += 64;
9534         Pdst_ptr += 64;
9535         i--;
9536 
9537         //Move y block position in a row, Reset position for U block
9538         u32_y_Blcok_Pos--;
9539         //===================GET U BLOCKs=======================
9540         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9541 
9542         //Get start position of block
9543         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9544 
9545         //Write value to destination buffer
9546         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9547         {
9548             for(k=0;k<u16Pixel_Per_Block_y;k++)
9549             {
9550                 *(u8Start_Each_Block + ((k*4) + 1) ) = 255 - Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9551             }
9552             u8Start_Each_Block += Max_Pixel_Per_Row;
9553         }
9554 
9555         //Move idct data position
9556         Psrc_ptr += 64;
9557         Pdst_ptr += 64;
9558         i--;
9559 
9560         //===================GET V BLOCKs=======================
9561         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9562 
9563         //Get start position of block
9564         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9565 
9566         //Write value to destination buffer
9567         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9568         {
9569             for(k=0;k<u16Pixel_Per_Block_y;k++)
9570             {
9571                 *(u8Start_Each_Block + ((k*4) + 3) ) =  255 - Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9572             }
9573             u8Start_Each_Block += Max_Pixel_Per_Row;
9574         }
9575 
9576         //Move idct data position
9577         Psrc_ptr += 128;
9578         Pdst_ptr += 128;
9579         i-=2;
9580 
9581         //Move y block position in a row, Reset position for U block
9582         u32_y_Blcok_Pos+=2;
9583 
9584     }
9585 
9586     u32_Decode_Line++;
9587     return TRUE;
9588 }
9589 
msAPI_JPEG_Idct_RGBConvert(void)9590 JPEG_STATIC MS_BOOL msAPI_JPEG_Idct_RGBConvert(void)
9591 {
9592     JPEG_BLOCK_TYPE *Psrc_ptr = _ps16Block_seg[0];
9593     MS_U8 *Pdst_ptr = gpu8Sample_buf;
9594     MS_U8 *u8Start_Each_Block, *u8Start_Each_Row;
9595     MS_U32 u32_y_Blcok_Pos = 0, Max_Pixel_Per_Row;
9596     MS_S32 i;
9597     MS_U8  u8Row_in_block, k;
9598     MS_U16 u16Pixel_Per_Block_x = 8, u16Pixel_Per_Block_y = 8;
9599     MS_U16 u16Pixel_Per_Pack_row = 16, u16TotalBlock_Per_Row, u16ScaleDownStep = 1 << _u8ScaleDownFactor;
9600 
9601     u16TotalBlock_Per_Row = (_u32Max_blocks_per_row*2)/3;
9602 
9603     u16Pixel_Per_Pack_row = u16Pixel_Per_Pack_row >> _u8ScaleDownFactor;
9604     u16Pixel_Per_Block_x = u16Pixel_Per_Block_x/* >> _u8ScaleDownFactor*/;
9605     u16Pixel_Per_Block_y = u16Pixel_Per_Block_y >> _u8ScaleDownFactor;
9606 
9607 
9608     Max_Pixel_Per_Row = (u16TotalBlock_Per_Row*u16Pixel_Per_Block_x >> _u8ScaleDownFactor);
9609 
9610     //Get start position of each row
9611     u8Start_Each_Row = (u8Out_buf + (u32_Decode_Line*(Max_Pixel_Per_Row*u16Pixel_Per_Block_y)));
9612 
9613     for ( i = (MS_S32)_u32Max_blocks_per_row; i > 0;)
9614     {
9615 
9616         //===================GET R BLOCKs========================
9617         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9618 
9619         //Get start position of block
9620         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9621 
9622         //Write value to destination buffer
9623         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9624         {
9625             for(k=0;k<u16Pixel_Per_Block_y;k++)
9626             {
9627                 *(u8Start_Each_Block + (k*2) ) = Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9628             }
9629             u8Start_Each_Block += Max_Pixel_Per_Row;
9630         }
9631 
9632         //Skip U. V block for 422 format
9633         Psrc_ptr += 64;
9634         Pdst_ptr += 64;
9635         i--;
9636 
9637         //===================GET G BLOCKs=======================
9638         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9639 
9640         //Get start position of block
9641         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9642         //Write value to destination buffer
9643         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9644         {
9645             for(k=0;k<u16Pixel_Per_Block_y;k++)
9646             {
9647                 *(u8Start_Each_Block + ((k*4) + 1) ) = Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9648             }
9649             u8Start_Each_Block += Max_Pixel_Per_Row;
9650         }
9651 
9652         //Move idct data position
9653         Psrc_ptr += 64;
9654         Pdst_ptr += 64;
9655         i--;
9656 
9657         //===================GET B BLOCKs=======================
9658         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9659 
9660         //Get start position of block
9661         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9662         //Write value to destination buffer
9663         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9664         {
9665             for(k=0;k<u16Pixel_Per_Block_y;k++)
9666             {
9667                 *(u8Start_Each_Block + ((k*4) + 3) ) =  Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9668             }
9669             u8Start_Each_Block += Max_Pixel_Per_Row;
9670         }
9671 
9672         //Move idct data position
9673         Psrc_ptr += 64;
9674         Pdst_ptr += 64;
9675         i--;
9676 
9677         if(i == 0)
9678         {
9679             //Already decoded all blocks
9680             continue;
9681         }
9682         else if(i < 0)
9683         {
9684             printf("GET BLOCK NUMBER ERROR!\n");
9685             JPEG_terminate( E_JPEG_DECODE_ERROR );
9686             return FALSE;
9687         }
9688 
9689         //Move y block position in a row
9690         u32_y_Blcok_Pos++;
9691         //===================GET R BLOCKs=======================
9692         msAPI_JPEG_idct( Psrc_ptr, Pdst_ptr );
9693 
9694         //Get start position of block
9695         u8Start_Each_Block = u8Start_Each_Row + (u32_y_Blcok_Pos*u16Pixel_Per_Pack_row);
9696         //Write value to destination buffer
9697         for(u8Row_in_block=0;u8Row_in_block<u16Pixel_Per_Block_x;u8Row_in_block+=u16ScaleDownStep)
9698         {
9699             for(k=0;k<u16Pixel_Per_Block_y;k++)
9700             {
9701                 *(u8Start_Each_Block + (k*2) ) = Pdst_ptr[(u8Row_in_block*8)+(k*u16ScaleDownStep)];
9702             }
9703             u8Start_Each_Block += Max_Pixel_Per_Row;
9704         }
9705 
9706         //Move idct data position
9707         Psrc_ptr += 192;
9708         Pdst_ptr += 192;
9709         i-=3;
9710 
9711         //Move y block position in a row, Reset position for U block
9712         u32_y_Blcok_Pos++;
9713 
9714     }
9715 
9716     u32_Decode_Line++;
9717     return TRUE;
9718 }
9719 
9720 //------------------------------------------------------------------------------
9721 // Performs a 2D IDCT over the entire row's coefficient buffer.
msAPI_JPEG_transform_row(void)9722 JPEG_STATIC MS_BOOL msAPI_JPEG_transform_row( void )
9723 {
9724     JPEG_DEBUG_API_MSG("Max block : %ld\n", _u32Max_blocks_per_row);
9725 
9726     switch ( gu8Scan_type )
9727     {
9728         case E_JPEG_CMYK:
9729             if(!msAPI_JPEG_Idct_CMYKConvert())
9730                 return FALSE;
9731             break;
9732         case E_JPEG_RGB:
9733             if(!msAPI_JPEG_Idct_RGBConvert())
9734                 return FALSE;
9735             break;
9736         default :
9737             JPEG_DEBUG_API_ERR("System don`t support this format!\n");
9738             JPEG_terminate( E_JPEG_UNSUPPORTED_SAMP_FACTORS );
9739             return FALSE;
9740             break;
9741     }
9742     return TRUE;
9743 
9744 }
9745 
msAPI_JPEG_Baseline_Decode(void)9746 JPEG_STATIC JPEG_Result msAPI_JPEG_Baseline_Decode(void)
9747 {
9748 
9749     if(_bProgressive_flag==TRUE)
9750         return (E_JPEG_FAILED);
9751 
9752     if ( _u16Total_lines_left == 0 )
9753     {
9754         return ( E_JPEG_OKAY );
9755     }
9756 
9757 #ifdef JPD_LONGJUMP_SUPPORT
9758 if ( setjmp( _jmp_state ) )
9759     {
9760         return ( E_JPEG_FAILED );
9761     }
9762 #endif
9763 
9764     if( _u16Total_lines_left > 0 )
9765     {
9766 
9767         if(!msAPI_JPEG_decode_next_row())
9768             return E_JPEG_FAILED;
9769 
9770         JPEG_DEBUG_API_MSG("Max line num : %d\n",(_u16Total_lines_left));
9771 
9772         if(!msAPI_JPEG_transform_row()) //IDCT
9773             return E_JPEG_FAILED;
9774 
9775         _u16Total_lines_left -= gu8Max_mcu_y_size;
9776     }
9777 
9778     return (E_JPEG_DONE);
9779 }
9780 #endif //SW_JPD_RGB_CMYK
9781 
9782 
9783 
MApi_JPEG_DisableAddressConvert(void)9784 void MApi_JPEG_DisableAddressConvert(void)
9785 {
9786     JPEG_DEBUG_API_MSG("%s\n", __FUNCTION__);
9787     __bIsMjpeg = FALSE;
9788 }
9789 
9790 //------------------------------------------------------------------------------
9791 /********************************************************************/
9792 ///Get the free memory for temporary usage.
9793 ///@param size \b IN indicate the size of wanted free memory
9794 ///@return the physical address of free memory
9795 /********************************************************************/
_MApi_JPEG_GetFreeMemory(MS_U32 size)9796 MS_PHYADDR _MApi_JPEG_GetFreeMemory(MS_U32 size)
9797 {
9798     if(_bIsInit == FALSE)
9799     {
9800         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
9801         return 0;
9802     }
9803 
9804     if(_bProgressive_flag == TRUE)
9805     {
9806         return 0;
9807     }
9808 
9809     if(size > (INTERNAL_BUFFER_SIZE - JPEG_DEFAULT_EXIF_SIZE))
9810     {
9811         return 0;
9812     }
9813 
9814     INTERNAL_BUFFER_SIZE = INTERNAL_BUFFER_SIZE - size;
9815 
9816     return (MS_VA2PA((MS_U32)INTERNAL_BUFFER_ADDR) + INTERNAL_BUFFER_SIZE);
9817 }
9818 
9819 //------------------------------------------------------------------------------
9820 /********************************************************************/
9821 ///Get the data offset of JPEG file.
9822 ///@return the data offset of JPEG file
9823 /********************************************************************/
_MApi_JPEG_GetDataOffset(void)9824 MS_U32 _MApi_JPEG_GetDataOffset(void)
9825 {
9826     if(_bIsInit == FALSE)
9827     {
9828         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
9829         return 0;
9830     }
9831 
9832     if(_bThumbnailAccessMode)
9833     {
9834         JPEG_DEBUG_API_MSG("%s: Thumbnail mode!!\n", __FUNCTION__);
9835         return 0;
9836     }
9837 
9838     JPEG_DEBUG_API_MSG("%s : u32DataOffset =  0x%lx\n", __FUNCTION__ , u32DataOffset);
9839     return u32DataOffset;
9840 }
9841 
9842 //------------------------------------------------------------------------------
9843 /********************************************************************/
9844 ///Get the sof marker offset of JPEG file.
9845 ///@return the sof marker offset of JPEG file
9846 /********************************************************************/
_MApi_JPEG_GetSOFOffset(void)9847 MS_U32 _MApi_JPEG_GetSOFOffset(void)
9848 {
9849     if(_bIsInit == FALSE)
9850     {
9851         JPEG_DEBUG_API_MSG("%s: JPD have not init!!\n", __FUNCTION__);
9852         return 0;
9853     }
9854 
9855     if(_bThumbnailAccessMode)
9856     {
9857         JPEG_DEBUG_API_MSG("%s: Thumbnail mode!!\n", __FUNCTION__);
9858         return 0;
9859     }
9860 
9861     JPEG_DEBUG_API_MSG("%s : u32SOFOffset =  0x%lx\n", __FUNCTION__ , u32SOFOffset);
9862     return u32SOFOffset;
9863 }
9864 
9865 //------------------------------------------------------------------------------
9866 /********************************************************************/
9867 ///Enable/Disable JPD CMYK support.
9868 ///@param bEnable \b IN enable/disable supporting JPD CMYK
9869 ///@return E_JPEG_OKAY/E_JPEG_FAILED
9870 ///Note: It need to be called before init. The default is JPD CMYK support.
9871 /********************************************************************/
_MApi_JPEG_SupportCMYK(MS_BOOL bEnable)9872 JPEG_Result _MApi_JPEG_SupportCMYK(MS_BOOL bEnable)
9873 {
9874 #if SW_JPD_RGB_CMYK
9875     bEnableCMYK = bEnable;
9876     return E_JPEG_OKAY;
9877 #else
9878     JPEG_DEBUG_API_MSG("Not Support CMYK formant!!!\n");
9879     UNUSED(bEnable);
9880     return E_JPEG_FAILED;
9881 #endif
9882 }
9883 
9884 //------------------------------------------------------------------------------
9885 /********************************************************************/
9886 ///Enable/Disable JPD RGB support.
9887 ///@param bEnable \b IN enable/disable supporting JPD RGB
9888 ///@return E_JPEG_OKAY/E_JPEG_FAILED
9889 ///Note: It need to be called before init. The default is JPD RGB support.
9890 /********************************************************************/
_MApi_JPEG_SupportRGB(MS_BOOL bEnable)9891 JPEG_Result _MApi_JPEG_SupportRGB(MS_BOOL bEnable)
9892 {
9893 #if SW_JPD_RGB_CMYK
9894     bEnableRGB = bEnable;
9895     return E_JPEG_OKAY;
9896 #else
9897     JPEG_DEBUG_API_MSG("Not Support RGB formant!!!\n");
9898     UNUSED(bEnable);
9899     return E_JPEG_FAILED;
9900 #endif
9901 }
9902 
9903 
9904 #if SUPPORT_MPO_FORMAT
_MApi_JPEG_IsMPOFormat(void)9905 MS_BOOL _MApi_JPEG_IsMPOFormat( void )
9906 {
9907     JPEG_DEBUG_API_MSG("MApi_JPEG_IsMPOFormat=%d\n", bIsMPOFormat);
9908     return bIsMPOFormat;
9909 }
9910 
_MApi_JPEG_GetMPOIndex(JPEG_MPO_INDEX_INFO ** ppMPOIndex)9911 MS_BOOL _MApi_JPEG_GetMPOIndex(JPEG_MPO_INDEX_INFO **ppMPOIndex)
9912 {
9913     if(_stMPOIndex.num_of_image == 0xFFFFFFFF)
9914     {
9915         JPEG_DEBUG_API_MSG("No MPO Index\n");
9916         return FALSE;
9917     }
9918 
9919     *ppMPOIndex = &_stMPOIndex;
9920     return TRUE;
9921 }
9922 
_MApi_JPEG_GetMPOAttr(MS_U32 image_no,JPEG_MPO_ATTRIBUTE_INFO ** ppMPOAttr)9923 MS_BOOL _MApi_JPEG_GetMPOAttr(MS_U32 image_no, JPEG_MPO_ATTRIBUTE_INFO **ppMPOAttr)
9924 {
9925     if((JPEG_MPO_MAX_SUPPORT_IMAGE < image_no)
9926     || (FALSE == _stMPOAttri[image_no - 1].used))
9927     {
9928         JPEG_DEBUG_API_MSG("No MPO Attribute with image no. %ld\n", image_no);
9929         return FALSE;
9930     }
9931 
9932     *ppMPOAttr = &_stMPOAttri[image_no - 1];
9933     return TRUE;
9934 }
9935 
_MApi_JPEG_DumpMPO(void)9936 void _MApi_JPEG_DumpMPO(void)
9937 {
9938     MS_U32 i = 0;
9939 
9940     if(_stMPOIndex.num_of_image == 0xFFFFFFFF)
9941     {
9942         JPEG_DEBUG_API_MSG("No MPO Index\n");
9943         return;
9944     }
9945 
9946     JPEG_DEBUG_API_MSG("start_of_offset = %ld, num_of_image = %ld\n", _stMPOIndex.start_of_offset, _stMPOIndex.num_of_image);
9947     for(i = 0; i<JPEG_MPO_MAX_SUPPORT_IMAGE; i++)
9948     {
9949         JPEG_DEBUG_API_MSG("mp_entry[%ld]:attribute = 0x%lx\n", i, _stMPOIndex.mp_entry[i].attribute);
9950         JPEG_DEBUG_API_MSG("mp_entry[%ld]:size = 0x%lx\n", i, _stMPOIndex.mp_entry[i].size);
9951         JPEG_DEBUG_API_MSG("mp_entry[%ld]:offset = 0x%lx\n", i, _stMPOIndex.mp_entry[i].offset);
9952         JPEG_DEBUG_API_MSG("mp_entry[%ld]:image1_no = 0x%x\n", i, _stMPOIndex.mp_entry[i].image1_no);
9953         JPEG_DEBUG_API_MSG("mp_entry[%ld]:image2_no = 0x%x\n", i, _stMPOIndex.mp_entry[i].image2_no);
9954 
9955         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:MPIndividualNum = 0x%lx\n", i, _stMPOAttri[i].MPIndividualNum);
9956         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOrientation = 0x%lx\n", i, _stMPOAttri[i].PanOrientation);
9957         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOverlap_H.numerator = 0x%lx\n", i, _stMPOAttri[i].PanOverlap_H.numerator);
9958         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOverlap_H.denominator = 0x%lx\n", i, _stMPOAttri[i].PanOverlap_H.denominator);
9959         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOverlap_V.numerator = 0x%lx\n", i, _stMPOAttri[i].PanOverlap_V.numerator);
9960         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PanOverlap_V.denominator = 0x%lx\n", i, _stMPOAttri[i].PanOverlap_V.denominator);
9961         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:BaseViewpointNum = 0x%lx\n", i, _stMPOAttri[i].BaseViewpointNum);
9962         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:ConvergenceAngle.s_numerator = 0x%lx\n", i, _stMPOAttri[i].ConvergenceAngle.s_numerator);
9963         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:ConvergenceAngle.s_denominator = 0x%lx\n", i, _stMPOAttri[i].ConvergenceAngle.s_denominator);
9964         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:BaselineLength.numerator = 0x%lx\n", i, _stMPOAttri[i].BaselineLength.numerator);
9965         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:BaselineLength.denominator = 0x%lx\n", i, _stMPOAttri[i].BaselineLength.denominator);
9966         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:VerticalDivergence.s_numerator = 0x%lx\n", i, _stMPOAttri[i].VerticalDivergence.s_numerator);
9967         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:VerticalDivergence.s_denominator = 0x%lx\n", i, _stMPOAttri[i].VerticalDivergence.s_denominator);
9968         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_X.s_numerator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_X.s_numerator);
9969         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_X.s_denominator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_X.s_denominator);
9970         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_Y.s_numerator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_Y.s_numerator);
9971         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_Y.s_denominator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_Y.s_denominator);
9972         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_Z.s_numerator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_Z.s_numerator);
9973         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:AxisDistance_Z.s_denominator = 0x%lx\n", i, _stMPOAttri[i].AxisDistance_Z.s_denominator);
9974         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:YawAngle.s_numerator = 0x%lx\n", i, _stMPOAttri[i].YawAngle.s_numerator);
9975         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:YawAngle.s_denominator = 0x%lx\n", i, _stMPOAttri[i].YawAngle.s_denominator);
9976         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PitchAngle.s_numerator = 0x%lx\n", i, _stMPOAttri[i].PitchAngle.s_numerator);
9977         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:PitchAngle.s_denominator = 0x%lx\n", i, _stMPOAttri[i].PitchAngle.s_denominator);
9978         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:RollAngle.s_numerator = 0x%lx\n", i, _stMPOAttri[i].RollAngle.s_numerator);
9979         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:RollAngle.s_denominator = 0x%lx\n", i, _stMPOAttri[i].RollAngle.s_denominator);
9980         JPEG_DEBUG_API_MSG("_stMPOAttri[%ld]:used = 0x%d\n", i, _stMPOAttri[i].used);
9981     }
9982     return;
9983 }
9984 
_MApi_JPEG_SetMPOBuffer(MS_U32 read_offset,MS_U32 output_start)9985 MS_BOOL _MApi_JPEG_SetMPOBuffer(MS_U32 read_offset, MS_U32 output_start)
9986 {
9987     read_offset = MS_PA2KSEG1((MS_U32)read_offset);// | AEON_NON_CACHE_MASK;
9988     output_start = MS_PA2KSEG1((MS_U32)output_start);// | AEON_NON_CACHE_MASK;
9989 
9990     if((read_offset < MRC_BUFFER_ADDR)
9991     || (read_offset > (MRC_BUFFER_ADDR + MRC_BUFFER_SIZE)))
9992     {
9993         JPEG_DEBUG_API_MSG("JPEG_SetMPOBuffer, read_offset invalid\n");
9994         return FALSE;
9995     }
9996 
9997     if(read_offset > (MRC_BUFFER_ADDR + MRC_BUFFER_SIZE/2))
9998     {
9999         mpo_load_data = FALSE;
10000     }
10001 
10002     if(output_start < MWC_BUFFER_ADDR)
10003     {
10004         JPEG_DEBUG_API_MSG("JPEG_SetMPOBuffer, output_start invalid\n");
10005         return FALSE;
10006     }
10007 
10008     _pu8In_buf_MPO_ofs = (MS_U8*)read_offset;
10009     _u32In_buf_MPO_left = (MRC_BUFFER_ADDR + MRC_BUFFER_SIZE) - read_offset;
10010     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);
10011     MWC_BUFFER_ADDR = output_start;
10012     return TRUE;
10013 }
10014 
10015 /********************************************************************/
10016 /// Set Max decoding resolution for MPO
10017 ///@param -u16Width \b IN : indicate max decoding width
10018 ///@param -u16Height \b IN : indicate max decoding height
10019 ///@return None
10020 /********************************************************************/
_MApi_JPEG_SetMPOMaxDecodeResolution(MS_U16 u16Width,MS_U16 u16Height)10021 void _MApi_JPEG_SetMPOMaxDecodeResolution(MS_U16 u16Width, MS_U16 u16Height)
10022 {
10023     JPEG_DEBUG_API_MSG("%s: Width = %d, Height = %d\n", __FUNCTION__, u16Width, u16Height);
10024     JPEG_MPO_MAX_WIDTH = u16Width;
10025     JPEG_MPO_MAX_HEIGHT = u16Height;
10026     return;
10027 }
10028 //------------------------------------------------------------------------------
10029 /********************************************************************/
10030 /// Set Max decoding resolution of Progressive MPO
10031 ///@param -u16ProWidth \b IN : indicate max decoding width
10032 ///@param -u16ProHeight \b IN : indicate max decoding height
10033 ///@return None
10034 /********************************************************************/
_MApi_JPEG_SetMPOProMaxDecodeResolution(MS_U16 u16ProWidth,MS_U16 u16ProHeight)10035 void _MApi_JPEG_SetMPOProMaxDecodeResolution(MS_U16 u16ProWidth, MS_U16 u16ProHeight)
10036 {
10037     JPEG_DEBUG_API_MSG("%s: ProWidth = %d, ProHeight = %d\n", __FUNCTION__, u16ProWidth, u16ProHeight);
10038     JPEG_MPO_PRO_MAX_WIDTH = u16ProWidth;
10039     JPEG_MPO_PRO_MAX_HEIGHT = u16ProHeight;
10040     return;
10041 }
10042 
10043 #endif
10044 
MApi_JPEG_EnableOJPD(MS_BOOL bOnOff)10045 void MApi_JPEG_EnableOJPD(MS_BOOL bOnOff)
10046 {
10047 #if (JPEG_UTOPIA20)
10048     if (pInstantJPD == NULL && bOnOff==TRUE)
10049     {
10050         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
10051         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10052         {
10053             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10054             return;
10055         }
10056     }
10057 
10058     if(pInstantJPD==NULL)
10059     {
10060         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10061         UtopiaClose(pInstantJPD);
10062         return;
10063     }
10064 
10065     JPEG_BOOL_PARAM bPARA;
10066     bPARA.bValue = bOnOff;
10067     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_EnableOJPD\n");
10068     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_EnableOJPD, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
10069     {
10070         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10071         UtopiaClose(pInstantJPD);
10072         pInstantJPD = NULL;
10073     }
10074     else if(bOnOff==FALSE)
10075     {
10076         UTOPIA_20_DBG("%s(): UtopiaClose()\n", __FUNCTION__);
10077         if(UtopiaClose(pInstantJPD) != UTOPIA_STATUS_SUCCESS)
10078         {
10079             JPEG_DEBUG_API_ERR("Utopia Close JPD failed\n");
10080             return;
10081         }
10082         pInstantJPD = NULL;
10083     }
10084 #else
10085     _MApi_JPEG_EnableOJPD(bOnOff);
10086 #endif
10087 }
MApi_JPEG_Init_UsingOJPD(JPEG_InitParam * pInitParam)10088 JPEG_Result MApi_JPEG_Init_UsingOJPD(JPEG_InitParam *pInitParam)
10089 {
10090 #if (JPEG_UTOPIA20)
10091     if (pInstantJPD == NULL)
10092     {
10093         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
10094         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10095         {
10096             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10097             return E_JPEG_FAILED;
10098         }
10099     }
10100 
10101     if(pInstantJPD==NULL)
10102     {
10103         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10104         UtopiaClose(pInstantJPD);
10105         return E_JPEG_FAILED;
10106     }
10107 
10108     JPEG_INIT_PARAM InitPara;
10109     memset(&InitPara , 0 ,sizeof(JPEG_INIT_PARAM));
10110     InitPara.pInitParam = pInitParam;
10111     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Init_UsingOJPD\n");
10112     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Init_UsingOJPD, (void*)(&InitPara)) != UTOPIA_STATUS_SUCCESS)
10113     {
10114         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10115         UtopiaClose(pInstantJPD);
10116         pInstantJPD = NULL;
10117         return E_JPEG_FAILED;
10118     }
10119     return InitPara.retVal;
10120 #else
10121     return _MApi_JPEG_Init_UsingOJPD(pInitParam);
10122 #endif
10123 }
10124 
msAPI_JPEG_get_APP0_info(MS_U8 * unit,MS_U16 * x,MS_U16 * y)10125 void msAPI_JPEG_get_APP0_info( MS_U8 *unit,MS_U16 *x, MS_U16 *y )
10126 {
10127 #if (JPEG_UTOPIA20)
10128     if(pInstantJPD==NULL)
10129     {
10130         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10131         return;
10132     }
10133 
10134     JPEG_GETAPP0INFO_PARAM App0Info;
10135     memset(&App0Info , 0 ,sizeof(JPEG_GETAPP0INFO_PARAM));
10136     App0Info.unit= unit;
10137     App0Info.x= x;
10138     App0Info.y= y;
10139     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_get_APP0_info\n");
10140     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_get_APP0_info, (void*)(&App0Info)) != UTOPIA_STATUS_SUCCESS)
10141     {
10142         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10143     }
10144 #else
10145     _msAPI_JPEG_get_APP0_info(unit, x, y );
10146 #endif
10147 }
10148 
MApi_JPEG_Init(JPEG_InitParam * pInitParam)10149 JPEG_Result MApi_JPEG_Init(JPEG_InitParam *pInitParam)
10150 {
10151 #if (JPEG_UTOPIA20)
10152     if (pInstantJPD == NULL)
10153     {
10154         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
10155         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10156         {
10157             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10158             return E_JPEG_FAILED;
10159         }
10160     }
10161 
10162     if(pInstantJPD==NULL)
10163     {
10164         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10165         UtopiaClose(pInstantJPD);
10166         return E_JPEG_FAILED;
10167     }
10168 
10169     JPEG_INIT_PARAM InitPara;
10170     memset(&InitPara , 0 ,sizeof(JPEG_INIT_PARAM));
10171     InitPara.pInitParam = pInitParam;
10172     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Init\n");
10173     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Init, (void*)(&InitPara)) != UTOPIA_STATUS_SUCCESS)
10174     {
10175         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10176         InitPara.retVal= E_JPEG_FAILED;
10177         UtopiaClose(pInstantJPD);
10178         pInstantJPD = NULL;
10179     }
10180     return InitPara.retVal;
10181 #else
10182     return _MApi_JPEG_Init(pInitParam);
10183 #endif
10184 }
10185 
MApi_JPEG_DecodeHdr(void)10186 JPEG_Result MApi_JPEG_DecodeHdr(void)
10187 {
10188 #if (JPEG_UTOPIA20)
10189     if(pInstantJPD==NULL)
10190     {
10191         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10192         return E_JPEG_FAILED;
10193     }
10194 
10195     JPEG_RESULT_PARAM ResPara;
10196     memset(&ResPara , 0 ,sizeof(JPEG_RESULT_PARAM));
10197     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_DecodeHdr\n");
10198     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_DecodeHdr, (void*)(&ResPara)) != UTOPIA_STATUS_SUCCESS)
10199     {
10200         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10201         return E_JPEG_FAILED;
10202     }
10203     return ResPara.retVal;
10204 #else
10205     return _MApi_JPEG_DecodeHdr();
10206 #endif
10207 }
10208 
MApi_JPEG_Decode(void)10209 JPEG_Result MApi_JPEG_Decode(void)
10210 {
10211 #if (JPEG_UTOPIA20)
10212     if(pInstantJPD==NULL)
10213     {
10214         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10215         return E_JPEG_FAILED;
10216     }
10217 
10218     JPEG_RESULT_PARAM ResPara;
10219     memset(&ResPara , 0 ,sizeof(JPEG_RESULT_PARAM));
10220     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Decode\n");
10221     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Decode, (void*)(&ResPara)) != UTOPIA_STATUS_SUCCESS)
10222     {
10223         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10224         return E_JPEG_FAILED;
10225     }
10226     return ResPara.retVal;
10227 #else
10228     return _MApi_JPEG_Decode();
10229 #endif
10230 }
10231 
MApi_JPEG_Exit(void)10232 void MApi_JPEG_Exit(void)
10233 {
10234 #if (JPEG_UTOPIA20)
10235     if(pInstantJPD==NULL)
10236     {
10237         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10238         return;
10239     }
10240 
10241     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Exit\n");
10242     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Exit, NULL) != UTOPIA_STATUS_SUCCESS)
10243     {
10244         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10245     }
10246     else
10247     {
10248         if(bOJPDOn==FALSE)
10249         {
10250             UTOPIA_20_DBG("%s(): UtopiaClose()\n", __FUNCTION__);
10251             if(UtopiaClose(pInstantJPD) != UTOPIA_STATUS_SUCCESS)
10252             {
10253                 JPEG_DEBUG_API_ERR("Utopia Close JPD failed\n");
10254                 return;
10255             }
10256             pInstantJPD = NULL;
10257         }
10258     }
10259 
10260 #else
10261     _MApi_JPEG_Exit();
10262 #endif
10263 }
10264 
MApi_JPEG_GetErrorCode(void)10265 JPEG_ErrCode MApi_JPEG_GetErrorCode(void)
10266 {
10267 #if (JPEG_UTOPIA20)
10268     if(pInstantJPD==NULL)
10269     {
10270         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10271         return E_JPEG_NOT_INIT;
10272     }
10273 
10274     JPEG_GETERRORCODE_PARAM GetErrorCode;
10275     memset(&GetErrorCode , 0 ,sizeof(JPEG_GETERRORCODE_PARAM));
10276     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetErrorCode\n");
10277     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetErrorCode, (void*)(&GetErrorCode)) != UTOPIA_STATUS_SUCCESS)
10278     {
10279         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10280         return E_JPEG_NOT_INIT;
10281     }
10282     return GetErrorCode.ErrCode;
10283 #else
10284     return _MApi_JPEG_GetErrorCode();
10285 #endif
10286 }
10287 
MApi_JPEG_GetJPDEventFlag(void)10288 JPEG_Event MApi_JPEG_GetJPDEventFlag(void)
10289 {
10290 #if (JPEG_UTOPIA20)
10291     if(pInstantJPD==NULL)
10292     {
10293         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10294         return 0;
10295     }
10296 
10297     JPEG_JPEGEVENT_PARAM JpegEvent;
10298     memset(&JpegEvent , 0 ,sizeof(JPEG_JPEGEVENT_PARAM));
10299     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetJPDEventFlag\n");
10300     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetJPDEventFlag, (void*)(&JpegEvent)) != UTOPIA_STATUS_SUCCESS)
10301     {
10302         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10303         return 0;
10304     }
10305     return JpegEvent.eEvtVal;
10306 #else
10307     return _MApi_JPEG_GetJPDEventFlag();
10308 #endif
10309 }
10310 
MApi_JPEG_SetJPDEventFlag(JPEG_Event eEvtVal)10311 void MApi_JPEG_SetJPDEventFlag(JPEG_Event eEvtVal)
10312 {
10313 #if (JPEG_UTOPIA20)
10314     if(pInstantJPD==NULL)
10315     {
10316         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10317         return;
10318     }
10319 
10320     JPEG_JPEGEVENT_PARAM JpegEvent;
10321     memset(&JpegEvent , 0 ,sizeof(JPEG_JPEGEVENT_PARAM));
10322     JpegEvent.eEvtVal = eEvtVal;
10323     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetJPDEventFlag\n");
10324     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetJPDEventFlag, (void*)(&JpegEvent)) != UTOPIA_STATUS_SUCCESS)
10325     {
10326         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10327     }
10328 #else
10329     _MApi_JPEG_SetJPDEventFlag(eEvtVal);
10330 #endif
10331 }
10332 
MApi_JPEG_Rst(void)10333 void MApi_JPEG_Rst(void)
10334 {
10335 #if (JPEG_UTOPIA20)
10336     if(pInstantJPD==NULL)
10337     {
10338         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10339         return;
10340     }
10341 
10342     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_Rst\n");
10343     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_Rst, NULL) != UTOPIA_STATUS_SUCCESS)
10344     {
10345         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10346     }
10347 #else
10348     _MApi_JPEG_Rst();
10349 #endif
10350 }
10351 
MApi_JPEG_PowerOn(void)10352 void MApi_JPEG_PowerOn(void)
10353 {
10354 #if (JPEG_UTOPIA20)
10355     if(pInstantJPD==NULL)
10356     {
10357         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10358         return;
10359     }
10360 
10361     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_PowerOn\n");
10362     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_PowerOn, NULL) != UTOPIA_STATUS_SUCCESS)
10363     {
10364         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10365     }
10366 #else
10367     _MApi_JPEG_PowerOn();
10368 #endif
10369 }
10370 
MApi_JPEG_PowerOff(void)10371 void MApi_JPEG_PowerOff(void)
10372 {
10373 #if (JPEG_UTOPIA20)
10374     if(pInstantJPD==NULL)
10375     {
10376         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10377         return;
10378     }
10379 
10380     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_PowerOff\n");
10381     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_PowerOff, NULL) != UTOPIA_STATUS_SUCCESS)
10382     {
10383         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10384     }
10385 #else
10386     _MApi_JPEG_PowerOff();
10387 #endif
10388 }
10389 
MApi_JPEG_GetCurVidx(void)10390 MS_U16 MApi_JPEG_GetCurVidx(void)
10391 {
10392 #if (JPEG_UTOPIA20)
10393     if(pInstantJPD==NULL)
10394     {
10395         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10396         return 0;
10397     }
10398 
10399     JPEG_U16_PARAM U16PARA;
10400     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10401     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetCurVidx\n");
10402     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetCurVidx, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10403     {
10404         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10405         return 0;
10406     }
10407     return U16PARA.u16Value;
10408 #else
10409     return _MApi_JPEG_GetCurVidx();
10410 #endif
10411 }
10412 
MApi_JPEG_IsProgressive(void)10413 MS_BOOL MApi_JPEG_IsProgressive(void)
10414 {
10415 #if (JPEG_UTOPIA20)
10416     if(pInstantJPD==NULL)
10417     {
10418         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10419         return 0;
10420     }
10421 
10422     JPEG_BOOL_PARAM bPARA;
10423     memset(&bPARA , 0 ,sizeof(JPEG_BOOL_PARAM));
10424     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_IsProgressive\n");
10425     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_IsProgressive, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
10426     {
10427         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10428         return 0;
10429     }
10430     return bPARA.bValue;
10431 #else
10432     return _MApi_JPEG_IsProgressive();
10433 #endif
10434 }
10435 
MApi_JPEG_ThumbnailFound(void)10436 MS_BOOL MApi_JPEG_ThumbnailFound(void)
10437 {
10438 #if (JPEG_UTOPIA20)
10439     if(pInstantJPD==NULL)
10440     {
10441         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10442         return 0;
10443     }
10444 
10445     JPEG_BOOL_PARAM bPARA;
10446     memset(&bPARA , 0 ,sizeof(JPEG_BOOL_PARAM));
10447     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_ThumbnailFound\n");
10448     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_ThumbnailFound, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
10449     {
10450         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10451         return 0;
10452     }
10453     return bPARA.bValue;
10454 #else
10455     return _MApi_JPEG_ThumbnailFound();
10456 #endif
10457 }
10458 
MApi_JPEG_GetWidth(void)10459 MS_U16 MApi_JPEG_GetWidth(void)
10460 {
10461 #if (JPEG_UTOPIA20)
10462     if(pInstantJPD==NULL)
10463     {
10464         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10465         return 0;
10466     }
10467 
10468     JPEG_U16_PARAM U16PARA;
10469     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10470     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetWidth\n");
10471     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetWidth, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10472     {
10473         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10474         return 0;
10475     }
10476     return U16PARA.u16Value;
10477 #else
10478     return _MApi_JPEG_GetWidth();
10479 #endif
10480 }
10481 
MApi_JPEG_GetHeight(void)10482 MS_U16 MApi_JPEG_GetHeight(void)
10483 {
10484 #if (JPEG_UTOPIA20)
10485     if(pInstantJPD==NULL)
10486     {
10487         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10488         return 0;
10489     }
10490 
10491     JPEG_U16_PARAM U16PARA;
10492     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10493     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetHeight\n");
10494     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetHeight, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10495     {
10496         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10497         return 0;
10498     }
10499     return U16PARA.u16Value;
10500 #else
10501     return _MApi_JPEG_GetHeight();
10502 #endif
10503 }
10504 
MApi_JPEG_GetOriginalWidth(void)10505 MS_U16 MApi_JPEG_GetOriginalWidth(void)
10506 {
10507 #if (JPEG_UTOPIA20)
10508     if(pInstantJPD==NULL)
10509     {
10510         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10511         return 0;
10512     }
10513 
10514     JPEG_U16_PARAM U16PARA;
10515     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10516     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetOriginalWidth\n");
10517     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetOriginalWidth, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10518     {
10519         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10520         return 0;
10521     }
10522     return U16PARA.u16Value;
10523 #else
10524     return _MApi_JPEG_GetOriginalWidth();
10525 #endif
10526 }
10527 
MApi_JPEG_GetOriginalHeight(void)10528 MS_U16 MApi_JPEG_GetOriginalHeight(void)
10529 {
10530 #if (JPEG_UTOPIA20)
10531     if(pInstantJPD==NULL)
10532     {
10533         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10534         return 0;
10535     }
10536 
10537     JPEG_U16_PARAM U16PARA;
10538     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10539     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetOriginalHeight\n");
10540     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetOriginalHeight, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10541     {
10542         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10543         return 0;
10544     }
10545     return U16PARA.u16Value;
10546 #else
10547     return _MApi_JPEG_GetOriginalHeight();
10548 #endif
10549 }
10550 
MApi_JPEG_GetNonAlignmentWidth(void)10551 MS_U16 MApi_JPEG_GetNonAlignmentWidth(void)
10552 {
10553 #if (JPEG_UTOPIA20)
10554     if(pInstantJPD==NULL)
10555     {
10556         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10557         return 0;
10558     }
10559 
10560     JPEG_U16_PARAM U16PARA;
10561     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10562     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetNonAlignmentWidth\n");
10563     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetNonAlignmentWidth, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10564     {
10565         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10566         return 0;
10567     }
10568     return U16PARA.u16Value;
10569 #else
10570     return _MApi_JPEG_GetNonAlignmentWidth();
10571 #endif
10572 }
10573 
MApi_JPEG_GetNonAlignmentHeight(void)10574 MS_U16 MApi_JPEG_GetNonAlignmentHeight(void)
10575 {
10576 #if (JPEG_UTOPIA20)
10577     if(pInstantJPD==NULL)
10578     {
10579         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10580         return 0;
10581     }
10582 
10583     JPEG_U16_PARAM U16PARA;
10584     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10585     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetNonAlignmentHeight\n");
10586     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetNonAlignmentHeight, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10587     {
10588         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10589         return 0;
10590     }
10591     return U16PARA.u16Value;
10592 #else
10593     return _MApi_JPEG_GetWidth();
10594 #endif
10595 }
10596 
MApi_JPEG_GetAlignedPitch(void)10597 MS_U16 MApi_JPEG_GetAlignedPitch(void)
10598 {
10599 #if (JPEG_UTOPIA20)
10600     if(pInstantJPD==NULL)
10601     {
10602         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10603         return 0;
10604     }
10605 
10606     JPEG_U16_PARAM U16PARA;
10607     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10608     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetAlignedPitch\n");
10609     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetAlignedPitch, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10610     {
10611         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10612         return 0;
10613     }
10614     return U16PARA.u16Value;
10615 #else
10616     return _MApi_JPEG_GetAlignedPitch();
10617 #endif
10618 }
10619 
MApi_JPEG_GetAlignedPitch_H(void)10620 MS_U16 MApi_JPEG_GetAlignedPitch_H(void)
10621 {
10622 #if (JPEG_UTOPIA20)
10623     if(pInstantJPD==NULL)
10624     {
10625         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10626         return 0;
10627     }
10628 
10629     JPEG_U16_PARAM U16PARA;
10630     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10631     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetAlignedPitch_H\n");
10632     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetAlignedPitch_H, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10633     {
10634         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10635         return 0;
10636     }
10637     return U16PARA.u16Value;
10638 #else
10639     return _MApi_JPEG_GetAlignedPitch_H();
10640 #endif
10641 }
10642 
MApi_JPEG_GetAlignedWidth(void)10643 MS_U16 MApi_JPEG_GetAlignedWidth(void)
10644 {
10645 #if (JPEG_UTOPIA20)
10646     if(pInstantJPD==NULL)
10647     {
10648         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10649         return 0;
10650     }
10651 
10652     JPEG_U16_PARAM U16PARA;
10653     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10654     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetAlignedWidth\n");
10655     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetAlignedWidth, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10656     {
10657         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10658         return 0;
10659     }
10660     return U16PARA.u16Value;
10661 #else
10662     return _MApi_JPEG_GetAlignedWidth();
10663 #endif
10664 }
10665 
MApi_JPEG_GetAlignedHeight(void)10666 MS_U16 MApi_JPEG_GetAlignedHeight(void)
10667 {
10668 #if (JPEG_UTOPIA20)
10669     if(pInstantJPD==NULL)
10670     {
10671         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10672         return 0;
10673     }
10674 
10675     JPEG_U16_PARAM U16PARA;
10676     memset(&U16PARA , 0 ,sizeof(JPEG_U16_PARAM));
10677     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetAlignedHeight\n");
10678     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetAlignedHeight, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
10679     {
10680         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10681         return 0;
10682     }
10683     return U16PARA.u16Value;
10684 #else
10685     return _MApi_JPEG_GetAlignedHeight();
10686 #endif
10687 }
10688 
MApi_JPEG_GetScaleDownFactor(void)10689 MS_U8 MApi_JPEG_GetScaleDownFactor(void)
10690 {
10691 #if (JPEG_UTOPIA20)
10692     if(pInstantJPD==NULL)
10693     {
10694         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10695         return 0;
10696     }
10697 
10698     JPEG_U8_PARAM U8PARA;
10699     memset(&U8PARA , 0 ,sizeof(JPEG_U8_PARAM));
10700     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetScaleDownFactor\n");
10701     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetScaleDownFactor, (void*)(&U8PARA)) != UTOPIA_STATUS_SUCCESS)
10702     {
10703         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10704         return 0;
10705     }
10706     return U8PARA.u8Value;
10707 #else
10708     return _MApi_JPEG_GetScaleDownFactor();
10709 #endif
10710 }
10711 
MApi_JPEG_SetMaxDecodeResolution(MS_U16 u16Width,MS_U16 u16Height)10712 void MApi_JPEG_SetMaxDecodeResolution(MS_U16 u16Width, MS_U16 u16Height)
10713 {
10714 #if (JPEG_UTOPIA20)
10715     if (pInstantJPD == NULL)
10716     {
10717         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
10718         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10719         {
10720             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10721             return;
10722         }
10723     }
10724 
10725     if(pInstantJPD==NULL)
10726     {
10727         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10728         UtopiaClose(pInstantJPD);
10729         return;
10730     }
10731 
10732     JPEG_RESOLUTION_PARAM Resol;
10733     memset(&Resol , 0 ,sizeof(JPEG_RESOLUTION_PARAM));
10734     Resol.u16Width= u16Width;
10735     Resol.u16Height= u16Height;
10736     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMaxDecodeResolution\n");
10737     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMaxDecodeResolution, (void*)(&Resol)) != UTOPIA_STATUS_SUCCESS)
10738     {
10739         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10740         UtopiaClose(pInstantJPD);
10741         pInstantJPD = NULL;
10742     }
10743 #else
10744     _MApi_JPEG_SetMaxDecodeResolution(u16Width, u16Height);
10745 #endif
10746 }
10747 
MApi_JPEG_SetProMaxDecodeResolution(MS_U16 u16ProWidth,MS_U16 u16ProHeight)10748 void MApi_JPEG_SetProMaxDecodeResolution(MS_U16 u16ProWidth, MS_U16 u16ProHeight)
10749 {
10750 #if (JPEG_UTOPIA20)
10751     if (pInstantJPD == NULL)
10752     {
10753         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
10754         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10755         {
10756             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10757             return;
10758         }
10759     }
10760 
10761     if(pInstantJPD==NULL)
10762     {
10763         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10764         UtopiaClose(pInstantJPD);
10765         return;
10766     }
10767 
10768     JPEG_RESOLUTION_PARAM Resol;
10769     memset(&Resol , 0 ,sizeof(JPEG_RESOLUTION_PARAM));
10770     Resol.u16Width= u16ProWidth;
10771     Resol.u16Height= u16ProHeight;
10772     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetProMaxDecodeResolution\n");
10773     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetProMaxDecodeResolution, (void*)(&Resol)) != UTOPIA_STATUS_SUCCESS)
10774     {
10775         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10776         UtopiaClose(pInstantJPD);
10777         pInstantJPD = NULL;
10778     }
10779 #else
10780     _MApi_JPEG_SetProMaxDecodeResolution(u16ProWidth, u16ProHeight);
10781 #endif
10782 }
10783 
MApi_JPEG_SetMRBufferValid(JPEG_BuffLoadType u8MRBuffType)10784 void MApi_JPEG_SetMRBufferValid(JPEG_BuffLoadType u8MRBuffType)
10785 {
10786 #if (JPEG_UTOPIA20)
10787     if(pInstantJPD==NULL)
10788     {
10789         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10790         return;
10791     }
10792 
10793     JPEG_SETMBUFFERVALID_PARAM MBuffValid;
10794     memset(&MBuffValid , 0 ,sizeof(JPEG_SETMBUFFERVALID_PARAM));
10795     MBuffValid.u8MRBuffType = u8MRBuffType;
10796     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMRBufferValid\n");
10797     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMRBufferValid, (void*)(&MBuffValid)) != UTOPIA_STATUS_SUCCESS)
10798     {
10799         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10800     }
10801 #else
10802     _MApi_JPEG_SetMRBufferValid(u8MRBuffType);
10803 #endif
10804 }
10805 
MApi_JPEG_UpdateReadInfo(MS_U32 u32DataRead,MS_BOOL bEOFflag)10806 void MApi_JPEG_UpdateReadInfo(MS_U32 u32DataRead, MS_BOOL bEOFflag)
10807 {
10808 #if (JPEG_UTOPIA20)
10809     if(pInstantJPD==NULL)
10810     {
10811         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10812         return;
10813     }
10814 
10815     JPEG_UPDATEREADINFO_PARAM ReadInfo;
10816     memset(&ReadInfo , 0 ,sizeof(JPEG_UPDATEREADINFO_PARAM));
10817     ReadInfo.u32DataRead = u32DataRead;
10818     ReadInfo.bEOFflag = bEOFflag;
10819     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_UpdateReadInfo\n");
10820     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_UpdateReadInfo, (void*)(&ReadInfo)) != UTOPIA_STATUS_SUCCESS)
10821     {
10822         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10823     }
10824 #else
10825     _MApi_JPEG_UpdateReadInfo(u32DataRead, bEOFflag);
10826 #endif
10827 }
10828 
MApi_JPEG_ProcessEOF(JPEG_BuffLoadType u8MRBuffType)10829 void MApi_JPEG_ProcessEOF(JPEG_BuffLoadType u8MRBuffType)
10830 {
10831 #if (JPEG_UTOPIA20)
10832     if(pInstantJPD==NULL)
10833     {
10834         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10835         return;
10836     }
10837 
10838     JPEG_PROCESSEOF_PARAM EofPara;
10839     memset(&EofPara , 0 ,sizeof(JPEG_PROCESSEOF_PARAM));
10840     EofPara.u8MRBuffType = u8MRBuffType;
10841     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_ProcessEOF\n");
10842     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_ProcessEOF, (void*)(&EofPara)) != UTOPIA_STATUS_SUCCESS)
10843     {
10844         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10845     }
10846 #else
10847     _MApi_JPEG_ProcessEOF(u8MRBuffType);
10848 #endif
10849 }
10850 
MApi_JPEG_SetErrCode(JPEG_ErrCode ErrStatus)10851 void MApi_JPEG_SetErrCode(JPEG_ErrCode ErrStatus)
10852 {
10853 #if (JPEG_UTOPIA20)
10854     if(pInstantJPD==NULL)
10855     {
10856         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10857         return;
10858     }
10859 
10860     JPEG_SETERRORCODE_PARAM ErrCode;
10861     memset(&ErrCode , 0 ,sizeof(JPEG_SETERRORCODE_PARAM));
10862     ErrCode.ErrStatus = ErrStatus;
10863     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetErrCode\n");
10864     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetErrCode, (void*)(&ErrCode)) != UTOPIA_STATUS_SUCCESS)
10865     {
10866         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10867     }
10868 #else
10869     _MApi_JPEG_SetErrCode(ErrStatus);
10870 #endif
10871 }
10872 
MApi_JPEG_SetDbgLevel(MS_U8 u8DbgLevel)10873 void MApi_JPEG_SetDbgLevel(MS_U8 u8DbgLevel)
10874 {
10875 #if (JPEG_UTOPIA20)
10876     _u8JPEG_ApiDbgLevel = u8DbgLevel;
10877     JPEG_DEBUG_API_MSG("%s: DbgLevel = 0x%04X\n", __FUNCTION__, u8DbgLevel);
10878     JPEG_SetDbgLevel_V2(u8DbgLevel);
10879     if (pInstantJPD == NULL)
10880     {
10881         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
10882         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
10883         {
10884             JPEG_DEBUG_API_ERR("Open JPD fail\n");
10885             return;
10886         }
10887     }
10888     if(pInstantJPD==NULL)
10889     {
10890         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10891         UtopiaClose(pInstantJPD);
10892         return;
10893     }
10894 
10895     JPEG_U8_PARAM U8PARA;
10896     memset(&U8PARA , 0 ,sizeof(JPEG_U8_PARAM));
10897     U8PARA.u8Value = u8DbgLevel;
10898     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetDbgLevel\n");
10899     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetDbgLevel, (void*)(&U8PARA)) != UTOPIA_STATUS_SUCCESS)
10900     {
10901         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10902         UtopiaClose(pInstantJPD);
10903         pInstantJPD = NULL;
10904     }
10905 #else
10906     _MApi_JPEG_SetDbgLevel(u8DbgLevel);
10907 #endif
10908 }
10909 
MApi_JPEG_GetDbgLevel(void)10910 MS_U8 MApi_JPEG_GetDbgLevel(void)
10911 {
10912 #if (JPEG_UTOPIA20)
10913     if(pInstantJPD==NULL)
10914     {
10915         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10916         return 0;
10917     }
10918 
10919     JPEG_U8_PARAM U8PARA;
10920     memset(&U8PARA , 0 ,sizeof(JPEG_U8_PARAM));
10921     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetDbgLevel\n");
10922     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetDbgLevel, (void*)(&U8PARA)) != UTOPIA_STATUS_SUCCESS)
10923     {
10924         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10925     }
10926     return U8PARA.u8Value;
10927 #else
10928     return _MApi_JPEG_GetDbgLevel();
10929 #endif
10930 }
10931 
MApi_JPEG_GetInfo(JPEG_Info * pJPEG_Info)10932 void MApi_JPEG_GetInfo(JPEG_Info *pJPEG_Info)
10933 {
10934 #if (JPEG_UTOPIA20)
10935     if(pInstantJPD==NULL)
10936     {
10937         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10938         return;
10939     }
10940 
10941     JPEG_GETINFO_PARAM eGETINFO;
10942     memset(&eGETINFO , 0 ,sizeof(JPEG_GETINFO_PARAM));
10943     eGETINFO.pJPEG_Info = pJPEG_Info;
10944     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetInfo\n");
10945     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetInfo, (void*)(&eGETINFO)) != UTOPIA_STATUS_SUCCESS)
10946     {
10947         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10948     }
10949 #else
10950     _MApi_JPEG_GetInfo(pJPEG_Info);
10951 #endif
10952 }
10953 
MApi_JPEG_GetStatus(JPEG_Status * pJPEG_Status)10954 void MApi_JPEG_GetStatus(JPEG_Status *pJPEG_Status)
10955 {
10956 #if (JPEG_UTOPIA20)
10957     if(pInstantJPD==NULL)
10958     {
10959         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10960         return;
10961     }
10962 
10963     JPEG_GETSTATUS_PARAM eGETSTATUS;
10964     memset(&eGETSTATUS , 0 ,sizeof(JPEG_GETSTATUS_PARAM));
10965     eGETSTATUS.pJPEG_Status = pJPEG_Status;
10966     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetStatus\n");
10967     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetStatus, (void*)(&eGETSTATUS)) != UTOPIA_STATUS_SUCCESS)
10968     {
10969         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10970     }
10971 #else
10972     _MApi_JPEG_GetStatus(pJPEG_Status);
10973 #endif
10974 }
10975 
MApi_JPEG_GetLibVer(const MSIF_Version ** ppVersion)10976 JPEG_Result MApi_JPEG_GetLibVer(const MSIF_Version **ppVersion)
10977 {
10978 #if (JPEG_UTOPIA20)
10979     if(pInstantJPD==NULL)
10980     {
10981         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10982         return E_JPEG_FAILED;
10983     }
10984 
10985     JPEG_GETLIBVER_PARAM eLIBVER;
10986     memset(&eLIBVER , 0 ,sizeof(JPEG_GETLIBVER_PARAM));
10987     eLIBVER.ppVersion = ppVersion;
10988     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetLibVer\n");
10989     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetLibVer, (void*)(&eLIBVER)) != UTOPIA_STATUS_SUCCESS)
10990     {
10991         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
10992     }
10993     return eLIBVER.retVal;
10994 #else
10995     return _MApi_JPEG_GetLibVer(ppVersion);
10996 #endif
10997 }
10998 
MApi_JPEG_HdlVidxChk(void)10999 JPEG_Result MApi_JPEG_HdlVidxChk(void)
11000 {
11001 #if (JPEG_UTOPIA20)
11002     if(pInstantJPD==NULL)
11003     {
11004         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11005         return E_JPEG_FAILED;
11006     }
11007 
11008     JPEG_RESULT_PARAM ResPara;
11009     memset(&ResPara , 0 ,sizeof(JPEG_RESULT_PARAM));
11010     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_HdlVidxChk\n");
11011     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_HdlVidxChk, (void*)(&ResPara)) != UTOPIA_STATUS_SUCCESS)
11012     {
11013         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11014     }
11015     return ResPara.retVal;
11016 #else
11017     return _MApi_JPEG_HdlVidxChk();
11018 #endif
11019 }
11020 
MApi_JPEG_GetBuffLoadType(JPEG_BuffLoadType * pBuffLoadType)11021 JPEG_Result MApi_JPEG_GetBuffLoadType(JPEG_BuffLoadType *pBuffLoadType)
11022 {
11023 #if (JPEG_UTOPIA20)
11024     if(pInstantJPD==NULL)
11025     {
11026         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11027         return E_JPEG_FAILED;
11028     }
11029 
11030     JPEG_GETBUFFLOADTYPE_PARAM eBufferLoad;
11031     memset(&eBufferLoad , 0 ,sizeof(JPEG_GETBUFFLOADTYPE_PARAM));
11032     eBufferLoad.pBuffLoadType = pBuffLoadType;
11033     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetBuffLoadType\n");
11034     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetBuffLoadType, (void*)(&eBufferLoad)) != UTOPIA_STATUS_SUCCESS)
11035     {
11036         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11037     }
11038     return eBufferLoad.retVal;
11039 #else
11040     return _MApi_JPEG_GetBuffLoadType(pBuffLoadType);
11041 #endif
11042 }
11043 
MApi_JPEG_EnableISR(JPEG_IsrFuncCb IsrCb)11044 JPEG_Result MApi_JPEG_EnableISR(JPEG_IsrFuncCb IsrCb)
11045 {
11046 #if (JPEG_UTOPIA20)
11047     if(pInstantJPD==NULL)
11048     {
11049         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11050         return E_JPEG_FAILED;
11051     }
11052 
11053     JPEG_ENABLEISR_PARAM eISR;
11054     memset(&eISR , 0 ,sizeof(JPEG_ENABLEISR_PARAM));
11055     eISR.IsrCb = IsrCb;
11056     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_EnableISR\n");
11057     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_EnableISR, (void*)(&eISR)) != UTOPIA_STATUS_SUCCESS)
11058     {
11059         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11060     }
11061     return eISR.retVal;
11062 #else
11063     return _MApi_JPEG_EnableISR(IsrCb);
11064 #endif
11065 }
11066 
MApi_JPEG_DisableISR(void)11067 JPEG_Result MApi_JPEG_DisableISR(void)
11068 {
11069 #if (JPEG_UTOPIA20)
11070     if(pInstantJPD==NULL)
11071     {
11072         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11073         return E_JPEG_FAILED;
11074     }
11075 
11076     JPEG_RESULT_PARAM ResPara;
11077     memset(&ResPara , 0 ,sizeof(JPEG_RESULT_PARAM));
11078     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_DisableISR\n");
11079     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_DisableISR, (void*)(&ResPara)) != UTOPIA_STATUS_SUCCESS)
11080     {
11081         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11082     }
11083     return ResPara.retVal;
11084 #else
11085     return _MApi_JPEG_DisableISR();
11086 #endif
11087 }
11088 
MApi_JPEG_WaitDone(void)11089 JPEG_DecodeStatus MApi_JPEG_WaitDone(void)
11090 {
11091 #if (JPEG_UTOPIA20)
11092     if(pInstantJPD==NULL)
11093     {
11094         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11095         return E_JPEG_DEC_DECODING;
11096     }
11097 
11098     JPEG_WAITDONE_PARAM eWaitDone;
11099     memset(&eWaitDone , 0 ,sizeof(JPEG_WAITDONE_PARAM));
11100     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_WaitDone\n");
11101     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_WaitDone, (void*)(&eWaitDone)) != UTOPIA_STATUS_SUCCESS)
11102     {
11103         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11104     }
11105     return eWaitDone.retVal;
11106 #else
11107     return _MApi_JPEG_WaitDone();
11108 #endif
11109 }
11110 
MApi_JPEG_GetEXIFDateTime(JPEG_EXIF_DateTime * DateTime)11111 JPEG_Result MApi_JPEG_GetEXIFDateTime(JPEG_EXIF_DateTime *DateTime)
11112 {
11113 #if (JPEG_UTOPIA20)
11114     if(pInstantJPD==NULL)
11115     {
11116         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11117         return E_JPEG_FAILED;
11118     }
11119 
11120     JPEG_GETEXIFDATETIME_PARAM eEXIFDate;
11121     memset(&eEXIFDate , 0 ,sizeof(JPEG_GETEXIFDATETIME_PARAM));
11122     eEXIFDate.DateTime = DateTime;
11123     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFDateTime\n");
11124     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFDateTime, (void*)(&eEXIFDate)) != UTOPIA_STATUS_SUCCESS)
11125     {
11126         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11127     }
11128     return eEXIFDate.retVal;
11129 #else
11130     return _MApi_JPEG_GetEXIFDateTime(DateTime);
11131 #endif
11132 }
11133 
MApi_JPEG_GetEXIFOrientation(JPEG_EXIF_Orientation * eOrientation)11134 JPEG_Result MApi_JPEG_GetEXIFOrientation(JPEG_EXIF_Orientation *eOrientation)
11135 {
11136 #if (JPEG_UTOPIA20)
11137     if(pInstantJPD==NULL)
11138     {
11139         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11140         return E_JPEG_FAILED;
11141     }
11142 
11143     JPEG_GETEXIFORIENTATION_PARAM eEXIFOri;
11144     memset(&eEXIFOri , 0 ,sizeof(JPEG_GETEXIFORIENTATION_PARAM));
11145     eEXIFOri.eOrientation = eOrientation;
11146     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFOrientation\n");
11147     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFOrientation, (void*)(&eEXIFOri)) != UTOPIA_STATUS_SUCCESS)
11148     {
11149         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11150     }
11151     return eEXIFOri.retVal;
11152 #else
11153     return _MApi_JPEG_GetEXIFOrientation(eOrientation);
11154 #endif
11155 }
11156 
11157 #if SUPPORT_EXIF_EXTRA_INFO
MApi_JPEG_GetEXIFManufacturer(MS_U8 * pu8Manufacturer,MS_U8 u8size)11158 JPEG_Result MApi_JPEG_GetEXIFManufacturer(MS_U8 *pu8Manufacturer, MS_U8 u8size)
11159 {
11160 #if (JPEG_UTOPIA20)
11161     if(pInstantJPD==NULL)
11162     {
11163         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11164         return E_JPEG_FAILED;
11165     }
11166 
11167     JPEG_GETEXIFSTRING_PARAM eEXIFStr;
11168     memset(&eEXIFStr , 0 ,sizeof(JPEG_GETEXIFSTRING_PARAM));
11169     eEXIFStr.pu8String = pu8Manufacturer;
11170     eEXIFStr.u8size = u8size;
11171     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFManufacturer\n");
11172     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFManufacturer, (void*)(&eEXIFStr)) != UTOPIA_STATUS_SUCCESS)
11173     {
11174         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11175     }
11176     return eEXIFStr.retVal;
11177 #else
11178     return _MApi_JPEG_GetEXIFManufacturer(pu8Manufacturer, u8size);
11179 #endif
11180 }
11181 
MApi_JPEG_GetEXIFModel(MS_U8 * pu8Model,MS_U8 u8size)11182 JPEG_Result MApi_JPEG_GetEXIFModel(MS_U8 *pu8Model, MS_U8 u8size)
11183 {
11184 #if (JPEG_UTOPIA20)
11185     if(pInstantJPD==NULL)
11186     {
11187         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11188         return E_JPEG_FAILED;
11189     }
11190 
11191     JPEG_GETEXIFSTRING_PARAM eEXIFStr;
11192     memset(&eEXIFStr , 0 ,sizeof(JPEG_GETEXIFSTRING_PARAM));
11193     eEXIFStr.pu8String = pu8Model;
11194     eEXIFStr.u8size = u8size;
11195     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFModel\n");
11196     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFModel, (void*)(&eEXIFStr)) != UTOPIA_STATUS_SUCCESS)
11197     {
11198         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11199     }
11200     return eEXIFStr.retVal;
11201 #else
11202     return _MApi_JPEG_GetEXIFModel(pu8Model, u8size);
11203 #endif
11204 }
11205 
MApi_JPEG_GetEXIFFlash(MS_U16 * pu16Flash)11206 JPEG_Result MApi_JPEG_GetEXIFFlash(MS_U16 *pu16Flash)
11207 {
11208 #if (JPEG_UTOPIA20)
11209     if(pInstantJPD==NULL)
11210     {
11211         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11212         return E_JPEG_FAILED;
11213     }
11214 
11215     JPEG_GETEXIFU16_PARAM U16PARA;
11216     memset(&U16PARA , 0 ,sizeof(JPEG_GETEXIFU16_PARAM));
11217     U16PARA.pu16Value = pu16Flash;
11218     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFFlash\n");
11219     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFFlash, (void*)(&U16PARA)) != UTOPIA_STATUS_SUCCESS)
11220     {
11221         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11222     }
11223     return U16PARA.retVal;
11224 #else
11225     return _MApi_JPEG_GetEXIFFlash(pu16Flash);
11226 #endif
11227 }
11228 
MApi_JPEG_GetEXIFISOSpeedRatings(MS_U32 * pu32ISOSpeedRatings)11229 JPEG_Result MApi_JPEG_GetEXIFISOSpeedRatings(MS_U32 *pu32ISOSpeedRatings)
11230 {
11231 #if (JPEG_UTOPIA20)
11232     if(pInstantJPD==NULL)
11233     {
11234         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11235         return E_JPEG_FAILED;
11236     }
11237 
11238     JPEG_GETEXIFU32_PARAM U32PARA;
11239     memset(&U32PARA , 0 ,sizeof(JPEG_GETEXIFU32_PARAM));
11240     U32PARA.pu32Value = pu32ISOSpeedRatings;
11241     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFISOSpeedRatings\n");
11242     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFISOSpeedRatings, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11243     {
11244         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11245     }
11246     return U32PARA.retVal;
11247 #else
11248     return _MApi_JPEG_GetEXIFISOSpeedRatings(pu32ISOSpeedRatings);
11249 #endif
11250 }
11251 
MApi_JPEG_GetEXIFShutterSpeedValue(JPEG_RATIONAL * pShutterSpeedValue)11252 JPEG_Result MApi_JPEG_GetEXIFShutterSpeedValue(JPEG_RATIONAL *pShutterSpeedValue)
11253 {
11254 #if (JPEG_UTOPIA20)
11255     if(pInstantJPD==NULL)
11256     {
11257         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11258         return E_JPEG_FAILED;
11259     }
11260 
11261     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11262     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11263     RATIONALPARA.pRational = pShutterSpeedValue;
11264     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFShutterSpeedValue\n");
11265     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFShutterSpeedValue, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11266     {
11267         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11268     }
11269     return RATIONALPARA.retVal;
11270 #else
11271     return _MApi_JPEG_GetEXIFShutterSpeedValue(pShutterSpeedValue);
11272 #endif
11273 }
11274 
MApi_JPEG_GetEXIFApertureValue(JPEG_RATIONAL * pApertureValue)11275 JPEG_Result MApi_JPEG_GetEXIFApertureValue(JPEG_RATIONAL *pApertureValue)
11276 {
11277 #if (JPEG_UTOPIA20)
11278     if(pInstantJPD==NULL)
11279     {
11280         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11281         return E_JPEG_FAILED;
11282     }
11283 
11284     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11285     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11286     RATIONALPARA.pRational = pApertureValue;
11287     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFApertureValue\n");
11288     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFApertureValue, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11289     {
11290         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11291     }
11292     return RATIONALPARA.retVal;
11293 #else
11294     return _MApi_JPEG_GetEXIFApertureValue(pApertureValue);
11295 #endif
11296 }
11297 
MApi_JPEG_GetEXIFExposureBiasValue(JPEG_RATIONAL * pExposureBiasValue)11298 JPEG_Result MApi_JPEG_GetEXIFExposureBiasValue(JPEG_RATIONAL *pExposureBiasValue)
11299 {
11300 #if (JPEG_UTOPIA20)
11301     if(pInstantJPD==NULL)
11302     {
11303         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11304         return E_JPEG_FAILED;
11305     }
11306 
11307     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11308     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11309     RATIONALPARA.pRational = pExposureBiasValue;
11310     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFExposureBiasValue\n");
11311     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFExposureBiasValue, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11312     {
11313         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11314     }
11315     return RATIONALPARA.retVal;
11316 #else
11317     return _MApi_JPEG_GetEXIFExposureBiasValue(pExposureBiasValue);
11318 #endif
11319 }
11320 
MApi_JPEG_GetEXIFFocalLength(JPEG_RATIONAL * pFocalLength)11321 JPEG_Result MApi_JPEG_GetEXIFFocalLength(JPEG_RATIONAL *pFocalLength)
11322 {
11323 #if (JPEG_UTOPIA20)
11324     if(pInstantJPD==NULL)
11325     {
11326         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11327         return E_JPEG_FAILED;
11328     }
11329 
11330     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11331     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11332     RATIONALPARA.pRational = pFocalLength;
11333     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFFocalLength\n");
11334     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFFocalLength, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11335     {
11336         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11337     }
11338     return RATIONALPARA.retVal;
11339 #else
11340     return _MApi_JPEG_GetEXIFFocalLength(pFocalLength);
11341 #endif
11342 }
11343 
MApi_JPEG_GetEXIFImageWidth(MS_U32 * pu32ImageWidth)11344 JPEG_Result MApi_JPEG_GetEXIFImageWidth(MS_U32 *pu32ImageWidth)
11345 {
11346 #if (JPEG_UTOPIA20)
11347     if(pInstantJPD==NULL)
11348     {
11349         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11350         return E_JPEG_FAILED;
11351     }
11352 
11353     JPEG_GETEXIFU32_PARAM U32PARA;
11354     memset(&U32PARA , 0 ,sizeof(JPEG_GETEXIFU32_PARAM));
11355     U32PARA.pu32Value = pu32ImageWidth;
11356     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFImageWidth\n");
11357     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFImageWidth, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11358     {
11359         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11360     }
11361     return U32PARA.retVal;
11362 #else
11363     return _MApi_JPEG_GetEXIFImageWidth(pu32ImageWidth);
11364 #endif
11365 }
11366 
MApi_JPEG_GetEXIFImageHeight(MS_U32 * pu32ImageHeight)11367 JPEG_Result MApi_JPEG_GetEXIFImageHeight(MS_U32 *pu32ImageHeight)
11368 {
11369 #if (JPEG_UTOPIA20)
11370     if(pInstantJPD==NULL)
11371     {
11372         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11373         return E_JPEG_FAILED;
11374     }
11375 
11376     JPEG_GETEXIFU32_PARAM U32PARA;
11377     memset(&U32PARA , 0 ,sizeof(JPEG_GETEXIFU32_PARAM));
11378     U32PARA.pu32Value = pu32ImageHeight;
11379     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFImageHeight\n");
11380     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFImageHeight, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11381     {
11382         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11383     }
11384     return U32PARA.retVal;
11385 #else
11386     return _MApi_JPEG_GetEXIFImageHeight(pu32ImageHeight);
11387 #endif
11388 }
11389 
MApi_JPEG_GetEXIFExposureTime(JPEG_RATIONAL * pExposureTime)11390 JPEG_Result MApi_JPEG_GetEXIFExposureTime(JPEG_RATIONAL *pExposureTime)
11391 {
11392 #if (JPEG_UTOPIA20)
11393     if(pInstantJPD==NULL)
11394     {
11395         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11396         return E_JPEG_FAILED;
11397     }
11398 
11399     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11400     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11401     RATIONALPARA.pRational = pExposureTime;
11402     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFExposureTime\n");
11403     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFExposureTime, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11404     {
11405         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11406     }
11407     return RATIONALPARA.retVal;
11408 #else
11409     return _MApi_JPEG_GetEXIFExposureTime(pExposureTime);
11410 #endif
11411 }
11412 
MApi_JPEG_GetEXIFFNumber(JPEG_RATIONAL * pFNumber)11413 JPEG_Result MApi_JPEG_GetEXIFFNumber(JPEG_RATIONAL *pFNumber)
11414 {
11415 #if (JPEG_UTOPIA20)
11416     if(pInstantJPD==NULL)
11417     {
11418         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11419         return E_JPEG_FAILED;
11420     }
11421 
11422     JPEG_GETEXIFRATIONAL_PARAM RATIONALPARA;
11423     memset(&RATIONALPARA , 0 ,sizeof(JPEG_GETEXIFRATIONAL_PARAM));
11424     RATIONALPARA.pRational = pFNumber;
11425     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetEXIFFNumber\n");
11426     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetEXIFFNumber, (void*)(&RATIONALPARA)) != UTOPIA_STATUS_SUCCESS)
11427     {
11428         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11429     }
11430     return RATIONALPARA.retVal;
11431 #else
11432     return _MApi_JPEG_GetEXIFFNumber(pFNumber);
11433 #endif
11434 }
11435 
11436 #endif
11437 
MApi_JPEG_GetFreeMemory(MS_U32 size)11438 MS_PHYADDR MApi_JPEG_GetFreeMemory(MS_U32 size)
11439 {
11440 #if (JPEG_UTOPIA20)
11441     if(pInstantJPD==NULL)
11442     {
11443         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11444         return 0;
11445     }
11446 
11447     JPEG_GETFREEMEMORY_PARAM MEMORYPARA;
11448     memset(&MEMORYPARA , 0 ,sizeof(JPEG_GETFREEMEMORY_PARAM));
11449     MEMORYPARA.size = size;
11450     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetFreeMemory\n");
11451     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetFreeMemory, (void*)(&MEMORYPARA)) != UTOPIA_STATUS_SUCCESS)
11452     {
11453         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11454     }
11455     return MEMORYPARA.addr;
11456 #else
11457     return _MApi_JPEG_GetFreeMemory(size);
11458 #endif
11459 }
11460 
11461 
MApi_JPEG_GetDataOffset(void)11462 MS_U32 MApi_JPEG_GetDataOffset(void)
11463 {
11464 #if (JPEG_UTOPIA20)
11465     if(pInstantJPD==NULL)
11466     {
11467         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11468         return 0;
11469     }
11470 
11471     JPEG_U32_PARAM U32PARA;
11472     memset(&U32PARA , 0 ,sizeof(JPEG_U32_PARAM));
11473     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetDataOffset\n");
11474     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetDataOffset, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11475     {
11476         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11477         return 0;
11478     }
11479     return U32PARA.u32Value;
11480 #else
11481     return _MApi_JPEG_GetDataOffset();
11482 #endif
11483 }
11484 
MApi_JPEG_GetSOFOffset(void)11485 MS_U32 MApi_JPEG_GetSOFOffset(void)
11486 {
11487 #if (JPEG_UTOPIA20)
11488     if(pInstantJPD==NULL)
11489     {
11490         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11491         return 0;
11492     }
11493 
11494     JPEG_U32_PARAM U32PARA;
11495     memset(&U32PARA , 0 ,sizeof(JPEG_U32_PARAM));
11496     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetSOFOffset\n");
11497     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetSOFOffset, (void*)(&U32PARA)) != UTOPIA_STATUS_SUCCESS)
11498     {
11499         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11500         return 0;
11501     }
11502     return U32PARA.u32Value;
11503 #else
11504     return _MApi_JPEG_GetSOFOffset();
11505 #endif
11506 }
11507 
MApi_JPEG_SupportCMYK(MS_BOOL bEnable)11508 JPEG_Result MApi_JPEG_SupportCMYK(MS_BOOL bEnable)
11509 {
11510 #if (JPEG_UTOPIA20)
11511     if (pInstantJPD == NULL)
11512     {
11513         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
11514         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
11515         {
11516             JPEG_DEBUG_API_ERR("Open JPD fail\n");
11517             return E_JPEG_FAILED;
11518         }
11519     }
11520 
11521     if(pInstantJPD==NULL)
11522     {
11523         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11524         UtopiaClose(pInstantJPD);
11525         return E_JPEG_FAILED;
11526     }
11527 
11528     JPEG_SUPPORTCMYK_PARAM CMYKRGB;
11529     memset(&CMYKRGB , 0 ,sizeof(JPEG_SUPPORTCMYK_PARAM));
11530     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SupportCMYK\n");
11531     CMYKRGB.bEnable = bEnable;
11532     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SupportCMYK, (void*)(&CMYKRGB)) != UTOPIA_STATUS_SUCCESS)
11533     {
11534         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11535         UtopiaClose(pInstantJPD);
11536         pInstantJPD = NULL;
11537         return E_JPEG_FAILED;
11538     }
11539     return CMYKRGB.retVal;
11540 #else
11541     return _MApi_JPEG_SupportCMYK(bEnable);
11542 #endif
11543 }
11544 
MApi_JPEG_SupportRGB(MS_BOOL bEnable)11545 JPEG_Result MApi_JPEG_SupportRGB(MS_BOOL bEnable)
11546 {
11547 #if (JPEG_UTOPIA20)
11548     if (pInstantJPD == NULL)
11549     {
11550         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
11551         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
11552         {
11553             JPEG_DEBUG_API_ERR("Open JPD fail\n");
11554             return E_JPEG_FAILED;
11555         }
11556     }
11557 
11558     if(pInstantJPD==NULL)
11559     {
11560         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11561         UtopiaClose(pInstantJPD);
11562         return E_JPEG_FAILED;
11563     }
11564 
11565     JPEG_SUPPORTCMYK_PARAM CMYKRGB;
11566     memset(&CMYKRGB , 0 ,sizeof(JPEG_SUPPORTCMYK_PARAM));
11567     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SupportRGB\n");
11568     CMYKRGB.bEnable = bEnable;
11569     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SupportRGB, (void*)(&CMYKRGB)) != UTOPIA_STATUS_SUCCESS)
11570     {
11571         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11572         UtopiaClose(pInstantJPD);
11573         pInstantJPD = NULL;
11574         return E_JPEG_FAILED;
11575     }
11576     return CMYKRGB.retVal;
11577 #else
11578     return _MApi_JPEG_SupportRGB(bEnable);
11579 #endif
11580 }
11581 
MApi_JPEG_SetMHEG5(MS_BOOL bEnable)11582 void MApi_JPEG_SetMHEG5(MS_BOOL bEnable)
11583 {
11584 #if (JPEG_UTOPIA20)
11585     if(pInstantJPD==NULL)
11586     {
11587         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11588         return;
11589     }
11590 
11591     JPEG_BOOL_PARAM bPARA;
11592     memset(&bPARA , 0 ,sizeof(JPEG_BOOL_PARAM));
11593     bPARA.bValue= bEnable;
11594     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMHEG5\n");
11595     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMHEG5, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
11596     {
11597         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11598         return;
11599     }
11600     return;
11601 #else
11602     _MApi_JPEG_SetMHEG5(bEnable);
11603 #endif
11604 }
11605 
11606 
11607 #if SUPPORT_MPO_FORMAT
11608 //For MPO
MApi_JPEG_IsMPOFormat(void)11609 MS_BOOL MApi_JPEG_IsMPOFormat(void)
11610 {
11611 #if (JPEG_UTOPIA20)
11612     if(pInstantJPD==NULL)
11613     {
11614         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11615         return 0;
11616     }
11617 
11618     JPEG_BOOL_PARAM bPARA;
11619     memset(&bPARA , 0 ,sizeof(JPEG_BOOL_PARAM));
11620     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_IsMPOFormat\n");
11621     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_IsMPOFormat, (void*)(&bPARA)) != UTOPIA_STATUS_SUCCESS)
11622     {
11623         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11624         return 0;
11625     }
11626     return bPARA.bValue;
11627 #else
11628     return _MApi_JPEG_IsMPOFormat();
11629 #endif
11630 }
11631 
11632 
MApi_JPEG_GetMPOIndex(JPEG_MPO_INDEX_INFO ** ppMPOIndex)11633 MS_BOOL MApi_JPEG_GetMPOIndex(JPEG_MPO_INDEX_INFO **ppMPOIndex)
11634 {
11635 #if (JPEG_UTOPIA20)
11636     if(pInstantJPD==NULL)
11637     {
11638         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11639         return 0;
11640     }
11641 
11642     JPEG_GETMPOINDEX_PARAM eMPOIndex;
11643     memset(&eMPOIndex , 0 ,sizeof(JPEG_GETMPOINDEX_PARAM));
11644     eMPOIndex.ppMPOIndex = ppMPOIndex;
11645     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetMPOIndex\n");
11646     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetMPOIndex, (void*)(&eMPOIndex)) != UTOPIA_STATUS_SUCCESS)
11647     {
11648         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11649         return 0;
11650     }
11651     return eMPOIndex.bretVal;
11652 #else
11653     return _MApi_JPEG_GetMPOIndex(ppMPOIndex);
11654 #endif
11655 }
11656 
MApi_JPEG_GetMPOAttr(MS_U32 image_no,JPEG_MPO_ATTRIBUTE_INFO ** ppMPOAttr)11657 MS_BOOL MApi_JPEG_GetMPOAttr(MS_U32 image_no, JPEG_MPO_ATTRIBUTE_INFO **ppMPOAttr)
11658 {
11659 #if (JPEG_UTOPIA20)
11660     if(pInstantJPD==NULL)
11661     {
11662         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11663         return 0;
11664     }
11665 
11666     JPEG_GETMPOATTR_PARAM eMPOAttr;
11667     memset(&eMPOAttr , 0 ,sizeof(JPEG_GETMPOATTR_PARAM));
11668     eMPOAttr.image_no = image_no;
11669     eMPOAttr.ppMPOAttr = ppMPOAttr;
11670     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetMPOAttr\n");
11671     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetMPOAttr, (void*)(&eMPOAttr)) != UTOPIA_STATUS_SUCCESS)
11672     {
11673         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11674         return 0;
11675     }
11676     return eMPOAttr.bretVal;
11677 #else
11678     return _MApi_JPEG_GetMPOAttr(image_no, ppMPOAttr);
11679 #endif
11680 }
11681 
MApi_JPEG_DumpMPO(void)11682 void MApi_JPEG_DumpMPO(void)
11683 {
11684 #if (JPEG_UTOPIA20)
11685     if(pInstantJPD==NULL)
11686     {
11687         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11688         return;
11689     }
11690 
11691     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_DumpMPO\n");
11692     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_DumpMPO, NULL) != UTOPIA_STATUS_SUCCESS)
11693     {
11694         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11695     }
11696 #else
11697     _MApi_JPEG_DumpMPO();
11698 #endif
11699 }
11700 
MApi_JPEG_SetMPOBuffer(MS_U32 read_offset,MS_U32 output_start)11701 MS_BOOL MApi_JPEG_SetMPOBuffer(MS_U32 read_offset, MS_U32 output_start)
11702 {
11703 #if (JPEG_UTOPIA20)
11704     if(pInstantJPD==NULL)
11705     {
11706         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11707         return 0;
11708     }
11709 
11710     JPEG_SETMPOBUFFER_PARAM eMPOBuff;
11711     memset(&eMPOBuff , 0 ,sizeof(JPEG_SETMPOBUFFER_PARAM));
11712     eMPOBuff.read_offset = read_offset;
11713     eMPOBuff.output_start = output_start;
11714     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMPOBuffer\n");
11715     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMPOBuffer, (void*)(&eMPOBuff)) != UTOPIA_STATUS_SUCCESS)
11716     {
11717         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11718         return 0;
11719     }
11720     return eMPOBuff.bretVal;
11721 #else
11722     return _MApi_JPEG_SetMPOBuffer(read_offset, output_start);
11723 #endif
11724 }
11725 
MApi_JPEG_SetMPOMaxDecodeResolution(MS_U16 u16Width,MS_U16 u16Height)11726 void MApi_JPEG_SetMPOMaxDecodeResolution(MS_U16 u16Width, MS_U16 u16Height)
11727 {
11728 #if (JPEG_UTOPIA20)
11729     if (pInstantJPD == NULL)
11730     {
11731         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
11732         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
11733         {
11734             JPEG_DEBUG_API_ERR("Open JPD fail\n");
11735             return;
11736         }
11737     }
11738 
11739     if(pInstantJPD==NULL)
11740     {
11741         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11742         UtopiaClose(pInstantJPD);
11743         return;
11744     }
11745 
11746     JPEG_RESOLUTION_PARAM Resol;
11747     memset(&Resol , 0 ,sizeof(JPEG_RESOLUTION_PARAM));
11748     Resol.u16Width= u16Width;
11749     Resol.u16Height= u16Height;
11750     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMPOMaxDecodeResolution\n");
11751     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMPOMaxDecodeResolution, (void*)(&Resol)) != UTOPIA_STATUS_SUCCESS)
11752     {
11753         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11754         UtopiaClose(pInstantJPD);
11755         pInstantJPD = NULL;
11756     }
11757 #else
11758     _MApi_JPEG_SetMPOMaxDecodeResolution(u16Width, u16Height);
11759 #endif
11760 }
11761 
MApi_JPEG_SetMPOProMaxDecodeResolution(MS_U16 u16ProWidth,MS_U16 u16ProHeight)11762 void MApi_JPEG_SetMPOProMaxDecodeResolution(MS_U16 u16ProWidth, MS_U16 u16ProHeight)
11763 {
11764 #if (JPEG_UTOPIA20)
11765     if (pInstantJPD == NULL)
11766     {
11767         UTOPIA_20_DBG("%s(): UtopiaOpen()\n", __FUNCTION__);
11768         if(UtopiaOpen(MODULE_JPEG_EX, &pInstantJPD, 0, NULL) !=  UTOPIA_STATUS_SUCCESS)
11769         {
11770             JPEG_DEBUG_API_ERR("Open JPD fail\n");
11771             return;
11772         }
11773     }
11774 
11775     if(pInstantJPD==NULL)
11776     {
11777         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11778         UtopiaClose(pInstantJPD);
11779         return;
11780     }
11781 
11782     JPEG_RESOLUTION_PARAM Resol;
11783     memset(&Resol , 0 ,sizeof(JPEG_RESOLUTION_PARAM));
11784     Resol.u16Width= u16ProWidth;
11785     Resol.u16Height= u16ProHeight;
11786     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_SetMPOProMaxDecodeResolution\n");
11787     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_SetMPOProMaxDecodeResolution, (void*)(&Resol)) != UTOPIA_STATUS_SUCCESS)
11788     {
11789         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11790         UtopiaClose(pInstantJPD);
11791         pInstantJPD = NULL;
11792     }
11793 #else
11794     _MApi_JPEG_SetMPOProMaxDecodeResolution(u16ProWidth, u16ProHeight);
11795 #endif
11796 }
11797 
11798 #endif
11799 
MApi_JPEG_GetControl(EN_JPEG_GET_CTRL_ID eGetID,MS_U32 * param,MS_U32 u32size)11800 JPEG_Result MApi_JPEG_GetControl(EN_JPEG_GET_CTRL_ID eGetID, MS_U32 *param, MS_U32 u32size)
11801 {
11802 #if (JPEG_UTOPIA20)
11803     if(pInstantJPD==NULL)
11804     {
11805         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11806         return E_JPEG_FAILED;
11807     }
11808 
11809     JPEG_GET_CTRL_PARAM eGetCTRL;
11810     memset(&eGetCTRL , 0 ,sizeof(JPEG_GET_CTRL_PARAM));
11811     eGetCTRL.eGetID = eGetID;
11812     eGetCTRL.param = param;
11813     eGetCTRL.u32size = u32size;
11814 
11815     UTOPIA_20_DBG("UtopiaIoctl(): MApi_CMD_JPEG_GetControl\n");
11816     if(UtopiaIoctl(pInstantJPD,MApi_CMD_JPEG_GetControl, (void*)(&eGetCTRL)) != UTOPIA_STATUS_SUCCESS)
11817     {
11818         JPEG_DEBUG_API_ERR("[Fail][%s,%d]\n",__FUNCTION__,__LINE__);
11819         return E_JPEG_FAILED;
11820     }
11821     return eGetCTRL.retVal;
11822 #else
11823     return _MApi_JPEG_GetControl(eGetID, param, u32size);
11824 #endif
11825 }
MApi_JPEG_SetNJPDInstance(MS_U8 JPDNum)11826 void MApi_JPEG_SetNJPDInstance(MS_U8 JPDNum)
11827 {
11828     // wrapper only for ojpd
11829     UNUSED(JPDNum);
11830 }
11831 
MApi_JPEG_IsNJPD(void)11832 MS_BOOL MApi_JPEG_IsNJPD(void)
11833 {
11834     return FALSE;
11835 }
11836 
MApi_JPEG_SetVerificationMode(NJPEG_VerificationMode VerificationMode)11837 void MApi_JPEG_SetVerificationMode(NJPEG_VerificationMode VerificationMode)
11838 {
11839     // wrapper only
11840     UNUSED(VerificationMode);
11841     return;
11842 }
MApi_JPEG_GetVerificationMode(void)11843 NJPEG_VerificationMode MApi_JPEG_GetVerificationMode(void)
11844 {
11845     // wrapper only
11846     return E_NJPEG00_NONE;
11847 }
MApi_NJPD_Debug(void)11848 void MApi_NJPD_Debug(void)
11849 {
11850     // wrapper only
11851     return;
11852 }
11853 
11854 
11855