1 //*@@@+++@@@@******************************************************************
2 //
3 // Copyright � Microsoft Corp.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 //
9 // � Redistributions of source code must retain the above copyright notice,
10 //   this list of conditions and the following disclaimer.
11 // � Redistributions in binary form must reproduce the above copyright notice,
12 //   this list of conditions and the following disclaimer in the documentation
13 //   and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 // POSSIBILITY OF SUCH DAMAGE.
26 //
27 //*@@@---@@@@******************************************************************
28 #pragma once
29 
30 #include <stddef.h>
31 #if defined(__MINGW32__)
32 #include <stdint.h>
33 #endif
34 
35 #include "windowsmediaphoto.h"
36 #include "common.h"
37 // #include "xplatform_image.h"
38 
39 // added for Xcode PK universal binary
40 #ifdef __ppc__
41 #define _BIG__ENDIAN_
42 #endif
43 
44 //================================================================
45 #ifdef ENABLE_OPTIMIZATIONS
46 #if defined(WIN32) && !defined(_WIN64)
47 #define WMP_OPT_SSE2
48 
49 #define WMP_OPT_CC_ENC
50 //#define WMP_OPT_TRFM_ENC
51 //#define WMP_OPT_QT
52 
53 #define WMP_OPT_CC_DEC
54 #define WMP_OPT_TRFM_DEC
55 
56 #define X86OPT_INLINE
57 
58 #endif
59 #endif // ENABLE_OPTIMIZATIONS
60 
61 //================================================================
62 //#ifdef WIN32
63 #if defined(WIN32) && !defined(UNDER_CE)   // WIN32 seems to be defined always in VS2005 for ARM platform
64 #define PLATFORM_X86
65 #include "../x86/x86.h"
66 #endif
67 
68 #ifndef UNREFERENCED_PARAMETER
69 #define UNREFERENCED_PARAMETER(P) { (P) = (P); }
70 #endif // UNREFERENCED_PARAMETER
71 
72 #ifdef UNDER_CE
73 #define PLATFORM_WCE
74 #include "arm.h"
75 #endif
76 
77 #ifdef __ANSI__
78 #define PLATFORM_ANSI
79 #include "ansi.h"
80 #endif
81 
82 //================================================================
83 
84 #ifdef PLATFORM_ANSI
85 typedef unsigned long long U64;
86 #else // PLATFORM_ANSI
87 typedef unsigned __int64 U64;
88 #endif // PLATFORM_ANSI
89 
90 //================================================================
91 #define MARKERCOUNT (PACKETLENGTH * 2)
92 
93 // The following macros depend on UINTPTR_T and INTPTR_T being properly defined
94 // so that they are equal to pointer width. Confirm and fail if our assumptions are wrong.
95 CT_ASSERT(sizeof(UINTPTR_T) == sizeof(void*), strcodec1);
96 CT_ASSERT(sizeof(INTPTR_T) == sizeof(void*), strcodec2);
97 
98 // wrap around pointer, s=pow(2,n), p wraps aligned to s
99 #define WRAPPTR(p, s) ((void*)((UINTPTR_T)(p) & ~(UINTPTR_T)(s)))
100 
101 // mask certain bit inside a pointer, simulate wrap around
102 #define MASKPTR(p, m) ((void*)((UINTPTR_T)(p) & (INTPTR_T)(m)))
103 
104 // test for more than 1 packet data
105 #define PACKET1(ps, pc, s) (((INTPTR_T)(ps) ^ (INTPTR_T)(pc)) & ((UINTPTR_T)(s)))
106 
107 // alternate pointer p between 2 values aligned to s, s=pow(2,n)
108 //#define ALTPTR(p, s) ((void*)((uintptr_t)(p) ^ (s)))
109 
110 // align point, s=pow(2,n), p aligns to s
111 #define ALIGNUP(p, s) ((void*)(((UINTPTR_T)(p) + ((UINTPTR_T)(s) - 1)) & ~((UINTPTR_T)(s) - 1)))
112 #define ALIGNDOWN(p, s) ((void*)((UINTPTR_T)(p) & ~((UINTPTR_T)(s) - 1)))
113 
114 //================================================================
115 // timer support
116 //================================================================
117 
118 #define TraceResult(a)
119 
120 //================================================================
121 typedef enum tagPacketType
122 {
123     PK_NULL = 0,
124     PK_DC = 1, PK_AD, PK_AC, PK_CP,
125     PK_MAX,
126 } PACKETTYPE;
127 
128 typedef struct tagIOContext
129 {
130     U8 P0[PACKETLENGTH];        // packet circular buffer 0
131     U8 P1[PACKETLENGTH];        // packet circular buffer 1
132 
133     union
134     {
135         U8 P2[PACKETLENGTH];
136         struct
137         {
138             U32 uiShadow;   // shadow of P0[0]-P0[3]
139 
140             U32 uiAccumulator;  // 32bit acc as bit field cache
141             U32 cBitsUsed;  // # of bits used of acc, [0,16)
142 
143             U8* pbPacket;   // packet pointer
144             U8* pbCurrent;  // current pointer
145 
146             struct WMPStream* pWS;    // pointer to WMPStream
147             long offPacket; // byte offset into stream
148 
149             //ULARGE_INTEGER u64Acc;
150 
151     //========================================
152     // index packet, used for packet retrieval
153     //========================================
154             U32 cIndex; // current index for index packet
155             long offIndex;  // byte offset into stream for index packet
156         }State;
157     }P2Info;
158     U8 P3[PACKETLENGTH];    // index packet buffer
159 } IOContext;
160 
161 typedef struct tagMemReadState
162 {
163     U8* pbBuf;
164     size_t cbBuf;
165     size_t cbCur;
166 } MemReadState;
167 
168 typedef struct tagBitIOInfo
169 {
170     U32 uiShadow;   // shadow of first 4B of circular buffer
171 
172     U32 uiAccumulator;  // 32bit acc as bit field cache
173     U32 cBitsUsed;  // # of bits used of acc, [0,16)
174 #ifdef ARMOPT_BITIO
175     U32 cBitsUnused;  // # of bits remain unused in acc, [0,32]
176 #endif
177 
178     I32 iMask;  // mask used simulate pointer wrap around
179 
180     U8* pbStart;    // start pointer
181 #ifndef ARMOPT_BITIO
182     U8* pbCurrent;  // current pointer
183 #else
184     U32* pbCurrent;  // current pointer
185 #endif
186 
187     struct WMPStream* pWS;  // pointer to WMPStream
188     size_t offRef;  // reference offset on IStream,
189                             // for read, it moves along the stream
190                             // for write, it stays at the attach point
191 } BitIOInfo;
192 
193 //================================================================
194 typedef struct tagCWMIQuantizer {
195     U8 iIndex;
196     I32 iQP;
197     I32 iOffset;
198     I32 iMan;
199     I32 iExp;
200 #if defined(WMP_OPT_QT)
201     float f1_QP;
202     double d1_QP;
203 #endif
204 } CWMIQuantizer;
205 
206 /* temporary bridge between old APIs and streaming APIs */
207 typedef struct tagCWMIMBInfo {
208     I32 iBlockDC[MAX_CHANNELS][16];
209     I32 iOrientation;
210     Int iCBP[MAX_CHANNELS];
211     Int iDiffCBP[MAX_CHANNELS];
212     U8 iQIndexLP; // 0 - 15
213     U8 iQIndexHP; // 0 - 15
214 } CWMIMBInfo;
215 
216 struct CWMImageStrCodec;
217 
218 typedef Int (*ImageDataProc)(struct CWMImageStrCodec*);
219 
220 /** scan model **/
221 typedef struct CAdaptiveScan {
222     U32		uTotal;
223     U32		uScan;
224 } CAdaptiveScan;
225 
226 /** Adaptive context model **/
227 typedef struct CCodingContext {
228     BitIOInfo * m_pIODC;
229     BitIOInfo * m_pIOLP;
230     BitIOInfo * m_pIOAC;
231     BitIOInfo * m_pIOFL;
232 
233     /** adaptive huffman structs **/
234     CAdaptiveHuffman *m_pAdaptHuffCBPCY;
235     CAdaptiveHuffman *m_pAdaptHuffCBPCY1;
236     CAdaptiveHuffman *m_pAHexpt[NUMVLCTABLES];
237 
238     /** 4x4 zigzag patterns */
239     CAdaptiveScan m_aScanLowpass[16];
240     CAdaptiveScan m_aScanHoriz[16];
241     CAdaptiveScan m_aScanVert[16];
242 
243     /** Adaptive bit reduction model **/
244     CAdaptiveModel m_aModelAC;
245     CAdaptiveModel m_aModelLP;
246     CAdaptiveModel m_aModelDC;
247 
248     /** Adaptive lowpass CBP model **/
249     Int     m_iCBPCountZero;
250     Int     m_iCBPCountMax;
251 
252     /** Adaptive AC CBP model **/
253     CCBPModel m_aCBPModel;
254 
255     /** Trim flex bits - externally set **/
256     Int     m_iTrimFlexBits;
257 
258     Bool m_bInROI;  // inside ROI (for region decode and compressed domain cropping)?
259 } CCodingContext;
260 
261 // Following stuff used to be in strPredQuant.h
262 /* circulant buffer for 2 MB rows: current row and previous row */
263 typedef struct tagCWMIPredInfo {
264     Int iQPIndex;     // QP Index
265     Int iCBP;      // coded block pattern
266     PixelI iDC;    // DC of MB
267     PixelI iAD[6];
268     PixelI * piAD; // AC of DC block: [2] 420UV [4] 422UV [6] elsewhere
269 }CWMIPredInfo;
270 
271 // the following is used on decode side while reading image info
272 typedef struct CWMImageStrCodecParameters {
273     size_t cVersion;
274     size_t cSubVersion;
275     COLORFORMAT cfColorFormat; // color format
276     Bool bRBSwapped; // blue and red shall be swapped in BGR555,565,101010
277     Bool bAlphaChannel; // alpha channel present
278     Bool bScaledArith; // lossless mode
279     Bool bIndexTable; // index table present
280     Bool bTrimFlexbitsFlag; // trimmed flexbits indicated in packet header
281     Bool bUseHardTileBoundaries; //default is soft tile boundaries
282     size_t cNumChannels;
283     size_t cExtraPixelsTop;
284     size_t cExtraPixelsLeft;
285     size_t cExtraPixelsBottom;
286     size_t cExtraPixelsRight;
287     Bool bTranscode;  // transcoding flag
288     U32 uQPMode;       // 0/1: no dquant/with dquant, first bit for DC, second bit for LP, third bit for HP
289     U8 uiQPIndexDC[MAX_CHANNELS];
290     U8 uiQPIndexLP[MAX_CHANNELS];
291     U8 uiQPIndexHP[MAX_CHANNELS];
292 }CCoreParameters;
293 
294 typedef struct CWMITile
295 {
296     CWMIQuantizer * pQuantizerDC[MAX_CHANNELS];
297     CWMIQuantizer * pQuantizerLP[MAX_CHANNELS];
298     CWMIQuantizer * pQuantizerHP[MAX_CHANNELS];
299     U8 cNumQPLP;
300     U8 cNumQPHP;
301     U8 cBitsLP;
302     U8 cBitsHP;
303 
304     Bool bUseDC;
305     Bool bUseLP;
306     U8 cChModeDC;
307     U8 cChModeLP[16];
308     U8 cChModeHP[16];
309 } CWMITile;
310 
311 #ifdef ARMOPT_COLORCONVERSION_C
312 #include "ARM_InvColorConversion.h"
313 #endif
314 
315 struct tagPostProcInfo{
316     Int iMBDC;                  // DC of MB
317     U8 ucMBTexture;             // MB texture   : 0(flat) 1(horizontal) 2(vertical) 3(bumpy)
318     Int iBlockDC[4][4];         // DC of block
319     U8 ucBlockTexture[4][4];    // block texture: 0(flat) 1(horizontal) 2(vertical) 3(bumpy)
320 };
321 
322 typedef struct CWMImageStrCodec {
323 #ifdef ARMOPT_COLORCONVERSION_C
324     CWMImageStrInvCCParam InvCCParam;
325 #endif
326 
327     size_t cbStruct;
328 
329     CWMImageInfo WMII;
330     CWMIStrCodecParam WMISCP;
331     CWMImageBufferInfo WMIBI;
332     CWMIMBInfo MBInfo;
333 
334     /** core parameters **/
335     CCoreParameters m_param;
336 
337     struct CWMDecoderParameters *m_Dparam;  // this is specified thru pointer because the same set of parameters may be used by multiple image planes
338 
339     U8 cSB;
340 
341     Bool m_bUVResolutionChange;
342 
343     Bool bTileExtraction;
344 
345     BitIOInfo * pIOHeader;
346 
347     Bool bUseHardTileBoundaries; //default is soft tile boundaries
348 
349     PixelI * pInterU;
350     PixelI * pInterV;
351 
352     //============== tile related info begins here ===========
353     // index table
354     size_t *pIndexTable;
355 
356     // current tile position
357     size_t cTileRow;
358     size_t cTileColumn;
359 
360     // tile boundary
361     Bool m_bCtxLeft;
362     Bool m_bCtxTop;
363 
364     Bool m_bResetRGITotals;
365     Bool m_bResetContext;
366 
367     CWMITile * pTile;
368 
369     // BitIOs
370     BitIOInfo ** m_ppBitIO;
371     size_t cNumBitIO;
372     size_t cHeaderSize;
373 
374     // coding contexts
375     struct CCodingContext *m_pCodingContext;
376     size_t cNumCodingContext;
377 
378     //============== tile related info ends here  ===========
379 
380     size_t cNumOfQPIndex;        // number of QP indexes
381     U8 cBitsDQUANT;              // number of bits to encode DQUANT
382 
383     size_t cRow;        // row for current macro block
384     size_t cColumn;     // column for current macro block
385 
386     size_t cmbWidth;    // macro block/image width
387     size_t cmbHeight;   // macro block/image height
388 
389     size_t cbChannel;   // byte/channel
390 
391     size_t mbX, mbY;
392     size_t tileX, tileY;
393     Bool bVertTileBoundary, bHoriTileBoundary;
394     Bool bOneMBLeftVertTB, bOneMBRightVertTB; //Macroblock to the left and to the right of tile boundaries
395 
396     PixelI iPredBefore[2][2];
397     PixelI iPredAfter[2][2];
398 
399     //================================
400     // input data into
401     // macro block 3 of 2x2 working widow
402     //================================
403     ImageDataProc Load;
404     //ImageDataProc Load2;
405     ImageDataProc Transform;
406     ImageDataProc TransformCenter;
407 
408     //================================
409     ImageDataProc Quantize;
410     //ImageDataProc QuantizeLuma;
411     //ImageDataProc QuantizeChroma;
412 
413     //================================
414     // process and store data from
415     // macro block 0 of 2x2 working window
416     //================================
417     ImageDataProc ProcessTopLeft;
418     ImageDataProc ProcessTop;
419     ImageDataProc ProcessTopRight;
420     ImageDataProc ProcessLeft;
421     ImageDataProc ProcessCenter;
422     ImageDataProc ProcessRight;
423     ImageDataProc ProcessBottomLeft;
424     ImageDataProc ProcessBottom;
425     ImageDataProc ProcessBottomRight;
426 
427 
428     //================================
429     // 2 MB working window for encoder
430     //================================
431     PixelI *pPlane[MAX_CHANNELS];
432 
433     //================================
434     // 2 rows of MB buffer
435     //================================
436     PixelI *a0MBbuffer[MAX_CHANNELS];  // pointer to start of previous MB row
437     PixelI *a1MBbuffer[MAX_CHANNELS];  // pointer to start of current MB row
438     PixelI *p0MBbuffer[MAX_CHANNELS];  // working pointer to start of previous row MB
439     PixelI *p1MBbuffer[MAX_CHANNELS];  // working pointer to start of current row MB
440 
441     //================================
442     // downsampling buffer for UV
443     //================================
444     PixelI * pResU;
445     PixelI * pResV;
446 
447     //================================
448     // circular buffer for 2 MB rows: current row and previous row
449     //================================
450     CWMIPredInfo *PredInfo[MAX_CHANNELS];
451     CWMIPredInfo *PredInfoPrevRow[MAX_CHANNELS];
452     CWMIPredInfo *pPredInfoMemory;
453 
454     struct WMPStream ** ppWStream;
455 
456 #ifdef WIN32
457     TCHAR **ppTempFile;
458 #else
459     char **ppTempFile;
460 #endif
461 
462     // interleaved alpha support - linked structure for Alpha channel
463     struct CWMImageStrCodec *m_pNextSC;
464     Bool   m_bSecondary;
465 
466     //================================
467     // Perf Timers
468     //================================
469 #ifndef DISABLE_PERF_MEASUREMENT
470     Bool            m_fMeasurePerf;
471     struct PERFTIMERSTATE *m_ptEndToEndPerf;   // Measures from Init to Term, including I/O
472     struct PERFTIMERSTATE *m_ptEncDecPerf;     // Measures time spent in ImageStrEncEncode/ImageStrDecDecode, excluding I/O
473 #endif // DISABLE_PERF_MEASUREMENT
474 
475     // postproc information for 2 MB rows: 0(previous row) 1(current row)
476     struct tagPostProcInfo * pPostProcInfo[MAX_CHANNELS][2];
477 } CWMImageStrCodec;
478 
479 
480 //================================================================
481 ERR WMPAlloc(void** ppv, size_t cb);
482 ERR WMPFree(void** ppv);
483 
484 //================================================================
485 Void initMRPtr(CWMImageStrCodec*);
486 Void advanceMRPtr(CWMImageStrCodec*);
487 Void swapMRPtr(CWMImageStrCodec*);
488 
489 Int IDPEmpty(CWMImageStrCodec*);
490 
491 //================================================================
492 extern const int dctIndex[3][16];
493 extern const int blkOffset[16];
494 extern const int blkOffsetUV[4];
495 extern const int blkOffsetUV_422[8];
496 
497 extern const U8 idxCC[16][16];
498 extern const U8 idxCC_420[8][8];
499 
500 extern const Char gGDISignature[];
501 
502 //================================================================
503 Int allocatePredInfo(CWMImageStrCodec*);
504 Void freePredInfo(CWMImageStrCodec*);
505 Void advanceOneMBRow(CWMImageStrCodec*);
506 
507 //================================================================
508 // bit I/O
509 //================================================================
510 Int allocateBitIOInfo(CWMImageStrCodec*);
511 Int setBitIOPointers(CWMImageStrCodec* pSC);
512 
513 #ifndef ARMOPT_BITIO
514 U32 peekBit16(BitIOInfo* pIO, U32 cBits);
515 U32 flushBit16(BitIOInfo* pIO, U32 cBits);
516 U32 getBit16(BitIOInfo* pIO, U32 cBits);
517 U32 getBool16(BitIOInfo* pIO);
518 I32 getBit16s(BitIOInfo* pIO, U32 cBits);
519 U32 getBit32(BitIOInfo* pIO, U32 cBits);
520 U32 flushToByte(BitIOInfo* pIO);
521 #endif  // ARMOPT_BITIO
522 
523 Void putBit16z(BitIOInfo* pIO, U32 uiBits, U32 cBits);
524 Void putBit16(BitIOInfo* pIO, U32 uiBits, U32 cBits);
525 Void putBit32(BitIOInfo* pIO, U32 uiBits, U32 cBits);
526 Void fillToByte(BitIOInfo* pIO);
527 
528 U32 getSizeRead(BitIOInfo* pIO);
529 U32 getSizeWrite(BitIOInfo* pIO);
530 
531 U32 getPosRead(BitIOInfo* pIO);
532 
533 // safe function, solely for the convenience of test code
534 #ifndef ARMOPT_BITIO
535 U32 getBit16_S(CWMImageStrCodec* pSC, BitIOInfo* pIO, U32 cBits);
536 #endif  // ARMOPT_BITIO
537 
538 //================================================================
539 // packet I/O
540 //================================================================
541 ERR attachISRead(BitIOInfo* pIO, struct WMPStream* pWS, CWMImageStrCodec* pSC);
542 ERR readIS(CWMImageStrCodec* pSC, BitIOInfo* pIO);
543 ERR detachISRead(CWMImageStrCodec* pSC, BitIOInfo* pIO);
544 
545 ERR attachISWrite(BitIOInfo* pIO, struct WMPStream* pWS);
546 ERR writeIS(CWMImageStrCodec* pSC, BitIOInfo* pIO);
547 ERR detachISWrite(CWMImageStrCodec* pSC, BitIOInfo* pIO);
548 
549 
550 //================================================================
551 // post processing for decoder
552 //================================================================
553 Int initPostProc(struct tagPostProcInfo * strPostProcInfo[MAX_CHANNELS][2], size_t mbWidth, size_t iNumChannels);
554 Void termPostProc(struct tagPostProcInfo * strPostProcInfo[MAX_CHANNELS][2], size_t iNumChannels);
555 Void slideOneMBRow(struct tagPostProcInfo * strPostProcInfo[MAX_CHANNELS][2], size_t iNumChannels, size_t mbWidth, Bool top, Bool bottom);
556 Void updatePostProcInfo(struct tagPostProcInfo * strPostProcInfo[MAX_CHANNELS][2], PixelI * p, size_t mbX, size_t cc);
557 Void postProcMB(struct tagPostProcInfo * strPostProcInfo[MAX_CHANNELS][2], PixelI * p0, PixelI * p1, size_t mbX, size_t cc, Int threshold);
558 Void postProcBlock(struct tagPostProcInfo * strPostProcInfo[MAX_CHANNELS][2], PixelI * p0, PixelI * p1, size_t mbX, size_t cc, Int threshold);
559 
560 //================================================================
561 // Simple BitIO access functions
562 //================================================================
563 typedef struct tagSimpleBitIO
564 {
565     struct WMPStream* pWS;
566     U32 cbRead;
567     U8 bAccumulator;
568     U32 cBitLeft;
569 } SimpleBitIO;
570 
571 ERR attach_SB(SimpleBitIO* pSB, struct WMPStream* pWS);
572 U32 getBit32_SB(SimpleBitIO* pSB, U32 cBits);
573 Void flushToByte_SB(SimpleBitIO* pSB);
574 U32 getByteRead_SB(SimpleBitIO* pSB);
575 ERR detach_SB(SimpleBitIO* pSB);
576 
577 //----------------------------------------------------------------
578 EXTERN_C Bool EOSWS_File(struct WMPStream* pWS);
579 
580 EXTERN_C ERR ReadWS_File(struct WMPStream* pWS, void* pv, size_t cb);
581 EXTERN_C ERR WriteWS_File(struct WMPStream* pWS, const void* pv, size_t cb);
582 //EXTERN_C ERR GetLineWS_File(struct WMPStream* pWS, void* pv, size_t cb);
583 
584 EXTERN_C ERR SetPosWS_File(struct WMPStream* pWS, size_t offPos);
585 EXTERN_C ERR GetPosWS_File(struct WMPStream* pWS, size_t* poffPos);
586 
587 //----------------------------------------------------------------
588 EXTERN_C Bool EOSWS_Memory(struct WMPStream* pWS);
589 
590 EXTERN_C ERR ReadWS_Memory(struct WMPStream* pWS, void* pv, size_t cb);
591 EXTERN_C ERR WriteWS_Memory(struct WMPStream* pWS, const void* pv, size_t cb);
592 //EXTERN_C ERR GetLineWS_Memory(struct WMPStream* pWS, void* pv, size_t cb);
593 
594 EXTERN_C ERR SetPosWS_Memory(struct WMPStream* pWS, size_t offPos);
595 EXTERN_C ERR GetPosWS_Memory(struct WMPStream* pWS, size_t* poffPos);
596 
597 //EXTERN_C ERR GetPtrWS_Memory(struct WMPStream* pWS, size_t align, U8** ppb);
598 //----------------------------------------------------------------
599 EXTERN_C Bool EOSWS_List(struct WMPStream* pWS);
600 
601 EXTERN_C ERR ReadWS_List(struct WMPStream* pWS, void* pv, size_t cb);
602 EXTERN_C ERR WriteWS_List(struct WMPStream* pWS, const void* pv, size_t cb);
603 
604 EXTERN_C ERR SetPosWS_List(struct WMPStream* pWS, size_t offPos);
605 EXTERN_C ERR GetPosWS_List(struct WMPStream* pWS, size_t* poffPos);
606 
607 EXTERN_C ERR CreateWS_List(struct WMPStream** ppWS);
608 EXTERN_C ERR CloseWS_List(struct WMPStream** ppWS);
609 
610 /********************************************************************/
611 // Stuff related to scale/spatial ordering
612 typedef struct PacketInfo
613 {
614     BAND    m_iBand;
615     size_t  m_iSize;
616     size_t  m_iOffset;
617     struct PacketInfo *m_pNext;
618 } PacketInfo;
619 /********************************************************************/
620 
621 /********************************************************************/
622 const static Int blkIdxByRow[4][4] = {{0, 1, 4, 5}, {2, 3, 6, 7}, {8, 9, 12, 13}, {10, 11, 14, 15}};
623 const static Int blkIdxByColumn[4][4] = {{0, 2, 8, 10}, {1, 3, 9, 11},{4, 6, 12, 14},{5, 7, 13, 15}};
624 
625 Int getACPredMode(CWMIMBInfo *, COLORFORMAT);
626 Int getDCACPredMode(CWMImageStrCodec *, size_t);
627 Void updatePredInfo(CWMImageStrCodec* pSC, CWMIMBInfo *, size_t, COLORFORMAT);
628 
629 Int AllocateCodingContextDec(struct CWMImageStrCodec *pSC, Int iNumContexts);
630 Void ResetCodingContext(CCodingContext *pContext);
631 Void getTilePos(CWMImageStrCodec* pSC, size_t mbX, size_t mbY);
632 Void InitZigzagScan(CCodingContext * pSC);
633 Int checkImageBuffer(CWMImageStrCodec *, size_t, size_t);
634 
635 //U32 log2(U32);
636 
637 //DQUANT stuff
638 EXTERN_C Void remapQP(CWMIQuantizer *, I32, Bool);
639 Int allocateTileInfo(CWMImageStrCodec *);
640 Void freeTileInfo(CWMImageStrCodec *);
641 Int allocateQuantizer(CWMIQuantizer * pQuantizer[MAX_CHANNELS], size_t, size_t);
642 Void freeQuantizer(CWMIQuantizer * pQuantizer[MAX_CHANNELS]);
643 Void setUniformQuantizer(CWMImageStrCodec *, size_t);
644 Void useDCQuantizer(CWMImageStrCodec *, size_t);
645 Void useLPQuantizer(CWMImageStrCodec *, size_t, size_t);
646 Void formatQuantizer(CWMIQuantizer * pQuantizer[MAX_CHANNELS], U8, size_t, size_t, Bool, Bool);
647 U8 dquantBits(U8);
648 
649 #ifdef ARMOPT_BITIO
650 #define peekBit16   peekBits
651 #define flushBit16  flushBits
652 #define getBit16    getBits
653 #define getBit32    getBits
654 #define getBit16s   getBitsS
655 #define getBool16(pIO) getBits(pIO, 1)
656 
657 U32 peekBits(BitIOInfo* pIO, U32 cBits);
658 void flushBits(BitIOInfo* pIO, U32 cBits);
659 U32 getBits(BitIOInfo* pIO, U32 cBits);
660 U32 getBitsS(BitIOInfo* pIO, U32 cBits);
661 void flushToByte(BitIOInfo* pIO);
662 #endif // ARMOPT_BITIO
663 
664 /*************************************************************************
665     Bitio defines
666 *************************************************************************/
667 #define PEEKBIT16(pIO, cBits) \
668     assert(0 <= (I32)cBits && cBits <= 16);\
669     return (pIO->uiAccumulator >> (32 - cBits/* - pIO->cBitsUsed*/));
670 
671 #define FLUSHBIT16(pIO, cBits) \
672     assert(0 <= (I32)cBits && cBits <= 16);\
673     assert((pIO->iMask & 1) == 0);\
674     pIO->cBitsUsed += cBits;\
675     pIO->pbCurrent = MASKPTR(pIO->pbCurrent + ((pIO->cBitsUsed >> 3)/* & 2*/), pIO->iMask);\
676     pIO->cBitsUsed &= 16 - 1;\
677     pIO->uiAccumulator = LOAD16(pIO->pbCurrent) << pIO->cBitsUsed;\
678     return 0;
679 //    pIO->uiAccumulator = LOAD16(pIO->pbCurrent) & ((U32)(-1) >> pIO->cBitsUsed);\
680 
681 void OutputPerfTimerReport(CWMImageStrCodec *pState);
682