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