imagingmodules/jp2kcodec/Src/JP2KEntropyDecoder.cpp
changeset 0 469c91dae73b
equal deleted inserted replaced
-1:000000000000 0:469c91dae73b
       
     1 /*
       
     2 * Copyright (c) 2003, 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Entropy decoder functionality..
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "JP2KCodeBlock.h"
       
    21 #include "JP2KTileInfo.h"
       
    22 #include "JP2KImageInfo.h"
       
    23 #include "JP2KEntropyDecoder.h"
       
    24 
       
    25 // EXTERNAL DATA STRUCTURES
       
    26 
       
    27 // EXTERNAL FUNCTION PROTOTYPES  
       
    28 
       
    29 // CONSTANTS
       
    30 
       
    31 // Mq decoder state information, all of the 47 states contain
       
    32 // the probability estimate of LPS in hexadecimal, the next
       
    33 // mps state index, the next lps state index and the flag
       
    34 // indicating a mps switch.
       
    35 const TUint32 KMqQeStates[KNumberOriginalMQEntries] =  {0x5601,0x3401,0x1801,0x0ac1,0x0521,0x0221,0x5601,
       
    36                                                         0x5401,0x4801,0x3801,0x3001,0x2401,0x1c01,0x1601,
       
    37                                                         0x5601,0x5401,0x5101,0x4801,0x3801,0x3401,0x3001,
       
    38                                                         0x2801,0x2401,0x2201,0x1c01,0x1801,0x1601,0x1401,
       
    39                                                         0x1201,0x1101,0x0ac1,0x09c1,0x08a1,0x0521,0x0441,
       
    40                                                         0x02a1,0x0221,0x0141,0x0111,0x0085,0x0049,0x0025,
       
    41                                                         0x0015,0x0009,0x0005,0x0001,0x5601};
       
    42 
       
    43 const TInt32 KMqMpsStates[KNumberOriginalMQEntries] =  {1,2,3,4,5,38,7,8,9,10,11,12,13,29,15,16,17,18,19,
       
    44                                                         20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,
       
    45                                                         36,37,38,39,40,41,42,43,44,45,45,46};
       
    46 
       
    47 const TInt32 KMqLpsStates[KNumberOriginalMQEntries] =  {1,6,9,12,29,33,6,14,14,14,17,18,20,21,14,14,15,16,
       
    48                                                         17,18,19,19,20,21,22,23,24,25,26,27,28,29,30,31,
       
    49                                                         32,33,34,35,36,37,38,39,40,41,42,43,46};
       
    50 
       
    51 const TInt32 KMqSwitchFlagStates[KNumberOriginalMQEntries] =  {1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,
       
    52                                                                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       
    53                                                                0,0,0,0,0,0,0};
       
    54 
       
    55 // MQCoder initial states
       
    56 const TInt32 KMqInitStates[KNumberContexts] = {46,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
       
    57 
       
    58 // MACROS
       
    59 
       
    60 // LOCAL CONSTANTS AND MACROS
       
    61 
       
    62 // MODULE DATA STRUCTURES
       
    63 
       
    64 // LOCAL FUNCTION PROTOTYPES
       
    65 
       
    66 // FORWARD DECLARATIONS
       
    67 
       
    68 // ============================ MEMBER FUNCTIONS ===============================
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // J2KEntropyStream::ReadByteFromStream
       
    72 // Read a byte from the input stream
       
    73 // ( other items were commented in a header ).
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 TUint8 J2KEntropyStream::ReadByteFromStream()
       
    77     {
       
    78     if ( iPosition < iNumBytes )
       
    79         {
       
    80         return ( TUint8 )iBuffer[iPosition++];
       
    81         }
       
    82     else
       
    83         {
       
    84         // 0xFF to represent EOF
       
    85         return ( 0xFF );   
       
    86         }
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // J2KEntropyStream::ReadBitFromStream
       
    91 // Read a bit from the input stream
       
    92 // ( other items were commented in a header ).
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 TUint8 J2KEntropyStream::ReadBitFromStream() 
       
    96     {
       
    97     if ( iTinyBufferPos == 0 )
       
    98         {
       
    99         // Input the byte buffer
       
   100         iTinyBuffer = ReadByteFromStream();
       
   101 
       
   102         if ( iDelayedFF )
       
   103             {
       
   104             iTinyBufferPos = 7;
       
   105             }
       
   106         else
       
   107             {
       
   108             iTinyBufferPos = 8;
       
   109             }
       
   110 
       
   111         if ( iTinyBuffer != 0xFF ) // No bit-stuffing needed
       
   112             {
       
   113             iDelayedFF = 0;
       
   114             }
       
   115         else  // We need to do bit stuffing on next byte
       
   116             {
       
   117             iDelayedFF = 1;
       
   118             }
       
   119         }
       
   120 
       
   121     return (TUint8)( ( iTinyBuffer >> ( --iTinyBufferPos ) ) & 0x01 );
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // J2KEntropyStream::CheckPrediction
       
   126 // Check a prediction termination for raw coding
       
   127 // ( other items were commented in a header ).
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 TEntropyErrorState J2KEntropyStream::CheckPrediction()
       
   131     {
       
   132     // Error resilient sequence in last byte
       
   133     TInt32 errorResSeq = 0; 
       
   134  
       
   135     // If tiny buffer is empty and equal to 0xFF, 
       
   136     // get the next byte for error resilience marker
       
   137     if ( iTinyBufferPos == 0 && iTinyBuffer == 0xFF ) 
       
   138         {
       
   139         iTinyBuffer = ReadByteFromStream();
       
   140         iTinyBufferPos = 7;
       
   141         }
       
   142   
       
   143     // Search for the error resilience marker
       
   144     if ( iTinyBufferPos > 0 ) 
       
   145         {
       
   146         // Get the error resilience sequence
       
   147         errorResSeq = iTinyBuffer & ( ( 1 << iTinyBufferPos ) - 1 );
       
   148 
       
   149         // Compare the read error resilience marker to the constant 01010101 byte
       
   150         if ( errorResSeq != ( KErrorResilienceTermination >> ( 8 - iTinyBufferPos ) ) ) 
       
   151             {
       
   152             return EEntropyCodingError;
       
   153             }
       
   154         }
       
   155   
       
   156     // If we are not at the end of this stream, 
       
   157     // next byte should be smaller than 0x80.
       
   158     if ( iPosition < iNumBytes )
       
   159         {
       
   160         if ( iTinyBuffer == 0xFF && iTinyBufferPos == 1 ) 
       
   161             {
       
   162             if ( ReadByteFromStream() >= 0x80 ) 
       
   163                 {
       
   164                 return EEntropyCodingError;
       
   165                 }
       
   166             }
       
   167         else 
       
   168             {
       
   169             if ( ReadByteFromStream() != 0xFF ) 
       
   170                 {
       
   171                 return EEntropyCodingError;
       
   172                 }
       
   173             }
       
   174         }
       
   175 
       
   176     // No error detected
       
   177     return ENoError;
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // J2KEntropyStream::ResetInputStream
       
   182 // Reset the input stream
       
   183 // ( other items were commented in a header ).
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void J2KEntropyStream::ResetInputStream()
       
   187     {
       
   188     // Initialize the stream parameters. Note that stream->position is not reset here,
       
   189     // it is reset only when the stream is created for the first time.
       
   190     iTinyBuffer = 0;
       
   191     iTinyBufferPos = 0;
       
   192     iDelayedFF = 0;
       
   193     }
       
   194 
       
   195 // ============================ MEMBER FUNCTIONS ===============================
       
   196 
       
   197 // Destructor
       
   198 J2KMQCoder::~J2KMQCoder()
       
   199     {
       
   200     iOriginalStates.ResetAndDestroy();
       
   201     iContextList.ResetAndDestroy();
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // J2KMQCoder::MqByteIn
       
   206 // Read a byte from the input stream
       
   207 // ( other items were commented in a header ).
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void J2KMQCoder::MqByteIn()
       
   211     {
       
   212     if ( !iMarker )
       
   213         {
       
   214         if ( iB == 0xff )
       
   215             {
       
   216             iB = iInputStream.ReadByteFromStream();
       
   217 
       
   218             if ( iB > 0x8f )
       
   219                 {
       
   220                 iMarker = 1;
       
   221                 iCT = 8;
       
   222                 }
       
   223             else
       
   224                 {
       
   225                 iC += 0xFE00 - ( iB << 9 );
       
   226                 iCT = 7;
       
   227                 }
       
   228             }
       
   229         else
       
   230             {
       
   231             iB = iInputStream.ReadByteFromStream();
       
   232             iC += 0xFF00 - ( iB << 8 );
       
   233             iCT = 8;
       
   234             }   
       
   235         }
       
   236     else
       
   237         {
       
   238         iCT = 8;
       
   239         }
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // J2KMQCoder::MqDecodeSymbol
       
   244 // Decode the symbol
       
   245 // ( other items were commented in a header ).
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 TUint8 J2KMQCoder::MqDecodeSymbol( TInt32 aContextIndex )
       
   249     {
       
   250     // Set the current context to point to the right place in the context list
       
   251     iCurrentContext = iContextList[aContextIndex];
       
   252     iCurrentState = iCurrentContext->iState;
       
   253 
       
   254     iA -= iCurrentState->iQe;
       
   255 
       
   256     // Decision returned ( i.e. decoded symbol )
       
   257     TInt32 decision = 0;  
       
   258     if ( ( iC >> 16 ) < iA )  // Chigh >= A
       
   259         {
       
   260         MpsExchange( decision );
       
   261         }
       
   262     else   // lps exchange
       
   263         {
       
   264         LpsExchange( decision );
       
   265         }
       
   266 
       
   267     // Return decision 
       
   268     return (TUint8)decision;   
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // J2KMQCoder::MqInitDecoder
       
   273 // Initialize the MQCoder
       
   274 // ( other items were commented in a header ).
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 void J2KMQCoder::MqInitDecoder()
       
   278     {
       
   279     iMarker = 0;
       
   280     iB = iInputStream.ReadByteFromStream();
       
   281 
       
   282     // Software conventions decoder initialization 
       
   283     iC = ( iB ^ 0xFF ) << 16;
       
   284     MqByteIn();
       
   285   
       
   286     iC <<= 7;
       
   287     iCT -= 7;
       
   288     iA = 0x8000;
       
   289     }
       
   290 
       
   291 // -----------------------------------------------------------------------------
       
   292 // J2KMQCoder::MqCheckPrediction
       
   293 // Check the prediction termination
       
   294 // ( other items were commented in a header ).
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 TEntropyErrorState J2KMQCoder::MqCheckPrediction()
       
   298     {
       
   299     // If no marker was found, B should be == 0xFF and counter zero, 
       
   300     // otherwise there is an error
       
   301     if ( !iMarker && ( iCT != 0 || iB != 0xFF ) ) 
       
   302         {
       
   303         return EEntropyCodingError;
       
   304         }
       
   305   
       
   306     // If counter CT is 1, no need to check any further
       
   307     if ( iCT == 1 )
       
   308         {
       
   309         return ENoError;
       
   310         }
       
   311   
       
   312     // If counter CT is zero, then next byte must be larger than 0x8F, if the terminating
       
   313     // marker has not been reached yet
       
   314     if ( iCT == 0 )  
       
   315         {
       
   316         if ( !iMarker ) 
       
   317             {
       
   318             // Get next byte and check that it is larger than 0x8F
       
   319             iB = iInputStream.ReadByteFromStream();
       
   320             if ( iB <= 0x8F )
       
   321                 {
       
   322                 return EEntropyCodingError;
       
   323                 }
       
   324             }
       
   325         // Set the counter back to 8
       
   326         iCT = 8;
       
   327         }
       
   328 
       
   329     // Number of bits that where added in the termination process
       
   330     // Compute the number of bits, k for error resilience information
       
   331     TUint32 k = (TUint32)( iCT - 1 );
       
   332   
       
   333     // Check for a coded LPS.
       
   334     TUint32 q = 0x8000 >> k;
       
   335   
       
   336     // Check that we can decode an LPS interval of probability 'q'
       
   337     iA -= q;
       
   338     if ( ( iC >> 16 ) < iA )   // software convention
       
   339         {
       
   340         // MPS interval was decoded, thus error occured
       
   341         return EEntropyCodingError;
       
   342         }
       
   343  
       
   344     // Check for coded LPS interval.
       
   345     iA = q;
       
   346 
       
   347     do  // Renormalization
       
   348         {
       
   349         if ( iCT == 0 )
       
   350             {
       
   351             MqByteIn();
       
   352             }
       
   353 
       
   354         iA <<= 1;
       
   355         iC <<= 1;
       
   356         iCT--;
       
   357 
       
   358         } while ( iA < 0x8000 );
       
   359   
       
   360     // No error was found
       
   361     return ENoError;
       
   362     }
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // J2KMQCoder::ResetMqContexts
       
   366 // Reset the MQCoder context list to the original state
       
   367 // ( other items were commented in a header ).
       
   368 // -----------------------------------------------------------------------------
       
   369 //
       
   370 void J2KMQCoder::ResetMqContexts()
       
   371     {
       
   372     // Reset all context states to initial states and set the mps
       
   373     // of every context to 0.
       
   374     for ( TInt32 i = KNumberContexts - 1; i >= 0; i-- )
       
   375         {
       
   376         iContextList[i]->iState = iOriginalStates[KMqInitStates[i]];
       
   377         iContextList[i]->iMPS = 0;
       
   378         }
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // J2KMQCoder::ResetMQDecoder
       
   383 // Initialze MQCoder and reset the context
       
   384 // ( other items were commented in a header ).
       
   385 // -----------------------------------------------------------------------------
       
   386 //
       
   387 void J2KMQCoder::ResetMQDecoder( TInt32 aSegmentLength )
       
   388     {
       
   389     // Reset all contexts ( states and mps )
       
   390     ResetMqContexts();              
       
   391     iInputStream.ResetInputStream();
       
   392     iInputStream.iPosition = 0;
       
   393     iInputStream.iNumBytes = aSegmentLength;
       
   394     MqInitDecoder();
       
   395     }
       
   396 
       
   397 // -----------------------------------------------------------------------------
       
   398 // J2KMQCoder::InitializeOrigMqTable
       
   399 // Initialize MQCoder original states table
       
   400 // ( other items were commented in a header ).
       
   401 // -----------------------------------------------------------------------------
       
   402 //
       
   403 void J2KMQCoder::InitializeOrigMqTable()
       
   404     {
       
   405     for ( TUint8 i = 0; i < KNumberOriginalMQEntries; i++ )
       
   406         {
       
   407         iOriginalStates[i]->iQe = KMqQeStates[i];
       
   408         iOriginalStates[i]->iNextMPS = iOriginalStates[KMqMpsStates[i]];
       
   409         iOriginalStates[i]->iNextLPS = iOriginalStates[KMqLpsStates[i]];
       
   410         iOriginalStates[i]->iSwitchFlag = KMqSwitchFlagStates[i];
       
   411         }
       
   412     }
       
   413 
       
   414 // -----------------------------------------------------------------------------
       
   415 // J2KMQCoder::ReNormalize
       
   416 // Renormalize 
       
   417 // ( other items were commented in a header ).
       
   418 // -----------------------------------------------------------------------------
       
   419 //
       
   420 void J2KMQCoder::ReNormalize()
       
   421     {
       
   422     do
       
   423         {
       
   424         if ( iCT == 0 )
       
   425             {
       
   426             MqByteIn();
       
   427             }
       
   428 
       
   429         iA <<= 1;
       
   430         iC <<= 1;
       
   431         --iCT;
       
   432         } while ( !( iA & 0x8000 ) );
       
   433     }
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // J2KMQCoder::MpsExchange
       
   437 // MPS exchange
       
   438 // ( other items were commented in a header ).
       
   439 // -----------------------------------------------------------------------------
       
   440 //
       
   441 void J2KMQCoder::MpsExchange( TInt32& aD )
       
   442     {
       
   443     aD = iCurrentContext->iMPS;
       
   444     if ( !( iA & 0x8000 ) )
       
   445         {
       
   446         if ( iA < iCurrentState->iQe )
       
   447             {
       
   448             aD ^= 1;
       
   449             if ( iCurrentState->iSwitchFlag )
       
   450                 {
       
   451                 iCurrentContext->iMPS ^= 1;
       
   452                 }
       
   453 
       
   454             iCurrentContext->iState = iCurrentState->iNextLPS;
       
   455             }
       
   456         else
       
   457             {
       
   458             iCurrentContext->iState = iCurrentState->iNextMPS;
       
   459             }
       
   460 
       
   461         ReNormalize();
       
   462         }
       
   463     }
       
   464 
       
   465 // -----------------------------------------------------------------------------
       
   466 // J2KMQCoder::LpsExchange
       
   467 // LPS exchange
       
   468 // ( other items were commented in a header ).
       
   469 // -----------------------------------------------------------------------------
       
   470 //
       
   471 void J2KMQCoder::LpsExchange( TInt32& aD )
       
   472     {
       
   473     iC -= iA << 16;
       
   474     aD = iCurrentContext->iMPS;
       
   475     if ( iA < iCurrentState->iQe )
       
   476         {
       
   477         iCurrentContext->iState = iCurrentState->iNextMPS;
       
   478         }
       
   479     else
       
   480         {
       
   481         aD ^= 1;
       
   482         if ( iCurrentState->iSwitchFlag )
       
   483             {
       
   484             iCurrentContext->iMPS ^= 1;
       
   485             }
       
   486 
       
   487         iCurrentContext->iState = iCurrentState->iNextLPS;
       
   488         }
       
   489     iA = iCurrentState->iQe;
       
   490     ReNormalize(  );
       
   491     }
       
   492 
       
   493 // ============================ MEMBER FUNCTIONS ===============================
       
   494 
       
   495 // -----------------------------------------------------------------------------
       
   496 // CJ2kEntropyDecoder::NewL
       
   497 // Two-phased constructor.
       
   498 // -----------------------------------------------------------------------------
       
   499 //
       
   500 CJ2kEntropyDecoder* CJ2kEntropyDecoder::NewL( CJ2kImageInfo& aImageInfo )
       
   501     {
       
   502     CJ2kEntropyDecoder *self = new ( ELeave ) CJ2kEntropyDecoder;
       
   503 
       
   504     CleanupStack::PushL( self );
       
   505     self->ConstructL( aImageInfo );
       
   506     CleanupStack::Pop();
       
   507 
       
   508     return self;
       
   509     }
       
   510 
       
   511 // Destructor
       
   512 CJ2kEntropyDecoder::~CJ2kEntropyDecoder()
       
   513     {  
       
   514     delete iMRLutBuf;
       
   515     iMRLutBuf = 0;
       
   516 
       
   517     delete iSCLutBuf;
       
   518     iSCLutBuf = 0;
       
   519   
       
   520     delete iZcLutLL;
       
   521     iZcLutLL = 0;
       
   522 
       
   523     delete iZcLutHL;
       
   524     iZcLutHL = 0;
       
   525 
       
   526     delete iZcLutHH;
       
   527     iZcLutHH = 0;
       
   528     
       
   529     TJ2kUtils::Free2DArray( iData );
       
   530     User::Free( iStates );
       
   531     }
       
   532 
       
   533 // -----------------------------------------------------------------------------
       
   534 // CJ2kEntropyDecoder::SetNewSizeL
       
   535 // Set the size of internal buffer and other control data
       
   536 // (other items were commented in a header).
       
   537 // -----------------------------------------------------------------------------
       
   538 //
       
   539 void CJ2kEntropyDecoder::SetNewSizeL( const TSize& aSize )
       
   540     {
       
   541     if ( aSize.iWidth <= iCurrentSize.iWidth &&
       
   542          aSize.iHeight <= iCurrentSize.iHeight )
       
   543         {
       
   544         return;
       
   545         }
       
   546     else
       
   547         {
       
   548         iCurrentSize = aSize;
       
   549 
       
   550         TJ2kUtils::Free2DArray( iData );
       
   551         iData = 0;
       
   552         iData = TJ2kUtils::Alloc2DArrayL( iCurrentSize.iHeight, iCurrentSize.iWidth );
       
   553 
       
   554         iMaxBlockWidth = (TUint8)( iCurrentSize.iWidth );
       
   555 
       
   556         // We can compute some parameters for entropy decoding here not to compute them for every pass!!!
       
   557         iBlockDataWidth        = iMaxBlockWidth;
       
   558         iStateWidth            = iMaxBlockWidth + 2;
       
   559         iStateSize             = ( iCurrentSize.iHeight + 2 ) * ( iCurrentSize.iWidth + 2 );
       
   560         iDataSamplesPerStripe  = iBlockDataWidth * KStripeHeight;
       
   561         iStateSamplesPerStripe = iStateWidth * KStripeHeight;
       
   562 
       
   563         TInt16* ptrTemp = STATIC_CAST( TInt16*, User::ReAlloc( iStates, iStateSize * sizeof( TInt16 ) ) );
       
   564         if ( !ptrTemp )
       
   565             {
       
   566             User::Leave( KErrNoMemory );
       
   567             }
       
   568         iStates = ptrTemp;
       
   569         }
       
   570     }
       
   571 
       
   572 // -----------------------------------------------------------------------------
       
   573 // CJ2kEntropyDecoder::SetCurrentZCLUT
       
   574 // Set the current pointer to point to the right LUT depending on  the current subband
       
   575 // (other items were commented in a header).
       
   576 // -----------------------------------------------------------------------------
       
   577 //
       
   578 void CJ2kEntropyDecoder::SetCurrentZCLUT( TUint8 aBandIndex )
       
   579     {
       
   580     if ( aBandIndex == 1 )
       
   581         {
       
   582         // For HL band
       
   583         iCurrentZcLutPtr = (TUint8*)iZcLutHL->Des().Ptr();
       
   584         }
       
   585     else if ( aBandIndex == 3 )
       
   586         {
       
   587         // For HH band
       
   588         iCurrentZcLutPtr = (TUint8*)iZcLutHH->Des().Ptr();
       
   589         }
       
   590     else 
       
   591         {
       
   592         // For LL and LH band
       
   593         iCurrentZcLutPtr = (TUint8*)iZcLutLL->Des().Ptr();
       
   594         }
       
   595     }
       
   596 
       
   597 // -----------------------------------------------------------------------------
       
   598 // CJ2kEntropyDecoder::DecodeCodeblock
       
   599 // Decode the coded codeblock
       
   600 // (other items were commented in a header).
       
   601 // -----------------------------------------------------------------------------
       
   602 //
       
   603 void CJ2kEntropyDecoder::DecodeCodeblock( CJ2kCodeBlock& aCodeblock, TUint8 aCblkStyle, 
       
   604                                           TUint8 aMagBits )
       
   605     {
       
   606     TInt32 startBitplane = 0;     // The bitplane where coding starts
       
   607     TInt32 endBitplane = 0;       // The last coded bitplane
       
   608     TInt32 terminationLength = 0;
       
   609     TUint8 tempPassIndex = 0;
       
   610     TUint8 passIndex = 0;
       
   611 
       
   612     iMQDecoder.iInputStream.iBuffer = CONST_CAST( TUint8*, aCodeblock.Data() );
       
   613     TSize codeBlockSize = aCodeblock.CodeBlockCanvas().Size();
       
   614 
       
   615     iVerticalCausalContextUsed     = ( ( aCblkStyle & EVerticalStripe ) != 0 );
       
   616     iResetContexts                 = ( ( aCblkStyle & EResetContext ) != 0 );
       
   617     iPredictableTerminationUsed    = ( ( aCblkStyle & EPredictableTermination ) != 0 );
       
   618     TUint8 terminateEachPass       = ( ( aCblkStyle & ETermination ) != 0 );
       
   619     TUint8 segmentationSymbolsUsed = ( ( aCblkStyle & ESegmentationSymbols ) != 0 );
       
   620     TUint8 arithmeticBypass        = ( ( aCblkStyle & EArithmeticBypass ) != 0 );
       
   621     aCodeblock.ResetPassIndex();
       
   622   
       
   623   
       
   624     // Initialize the stream and the mq coder for this code block:
       
   625     // also set mq coder's stream to point to aCodeblock's stream 
       
   626     if ( !arithmeticBypass && !terminateEachPass )  // No termination
       
   627         {
       
   628         iMQDecoder.ResetMQDecoder( aCodeblock.DataLength() );
       
   629         }
       
   630     else
       
   631         {
       
   632         // Compute the length of the first terminated segment. If we are 
       
   633         // terminating on each pass, terminated lengths are equal to 
       
   634         // codeblock lengths. On the other hand if we are using arithmetic
       
   635         // bypass ( without terminating each pass ), the first termination length
       
   636         // is the codeblock lengths summed up to the point where we have last AC
       
   637         // pass before first raw coding pass.
       
   638 
       
   639         if ( terminateEachPass )
       
   640             {
       
   641             terminationLength = aCodeblock.CblkLength( (TUint16)passIndex++ );
       
   642             }
       
   643         else  // AC bypass without termination on each pass
       
   644             {
       
   645             // Loop adding codeblock lengths until enough passes have been consumed 
       
   646             while ( ( tempPassIndex < KFirstBypassTermIndex ) && ( passIndex <= aCodeblock.LastPass() ) )
       
   647                 {
       
   648                 tempPassIndex = (TUint8)( tempPassIndex + aCodeblock.PassesPerSegment( passIndex ) );
       
   649                 terminationLength = (TUint32)( terminationLength + aCodeblock.CblkLength( passIndex++ ) );
       
   650                 }
       
   651             }
       
   652 
       
   653         // Now initialize the iMQDecoder with the right terminated length
       
   654         iMQDecoder.ResetMQDecoder( terminationLength );
       
   655         }
       
   656   
       
   657     // Initialize data array 
       
   658     for ( TInt i = codeBlockSize.iHeight - 1; i >= 0; i-- )
       
   659         {
       
   660         Mem::FillZ( iData[i], codeBlockSize.iWidth * sizeof( TPrecInt ) );
       
   661         }
       
   662   
       
   663     // Initialize iStates array
       
   664     Mem::FillZ( iStates, iStateSize * sizeof( TInt16 ) );
       
   665   
       
   666     // Compute different bitplanes; empty bitplanes tells how many bitplanes are 
       
   667     // empty in this codeblock compared to the whole subband, iStartBitplane gives
       
   668     // the index of the first bitplane to be coded for this block and iEndBitplane
       
   669     // is the index of the last bitplane coded for this codeblock.
       
   670   
       
   671     // Compute coded block's magnitude bits 
       
   672     aMagBits = (TUint8)( aMagBits - aCodeblock.EmptyBitplanes() );
       
   673     startBitplane = ( KImplementationPrecision - 2 ) - aCodeblock.EmptyBitplanes();
       
   674     endBitplane = startBitplane - aMagBits;
       
   675  
       
   676     // Compute the number of stripes for this codeblock 
       
   677     iNumStripes = (TUint16)( ( codeBlockSize.iHeight + KStripeHeight - 1 ) / KStripeHeight );
       
   678     iBlockWidth = codeBlockSize.iWidth;
       
   679 
       
   680     // Compute the last stripe's height once, 
       
   681     // so that we don't have to compute it for all passes!
       
   682     iLastStripeHeight = codeBlockSize.iHeight - ( iNumStripes - 1 ) * KStripeHeight;
       
   683 
       
   684     iCurrentBitplane = (TUint8)( startBitplane );
       
   685 
       
   686     // First do only the normalization pass 
       
   687     iTerminateThisPass = ( terminateEachPass || ( iCurrentBitplane == endBitplane ) );
       
   688 
       
   689     CleanupPass( segmentationSymbolsUsed );
       
   690 
       
   691     aCodeblock.IncrementPassIndex();
       
   692 
       
   693     iCurrentBitplane--;
       
   694   
       
   695     // Then repeat the three passes: significance, refinement and normalization 
       
   696     // for the remaining bitplanes.
       
   697     while ( aCodeblock.PassIndex() <= aCodeblock.LastPass() )
       
   698         {
       
   699         // Significance pass, terminate only if terminating after each pass
       
   700         iTerminateThisPass = terminateEachPass;
       
   701 
       
   702         if ( ( arithmeticBypass == 0 ) || ( aCodeblock.PassIndex() < KFirstLazyPassIndex ) )
       
   703             {
       
   704             if ( terminateEachPass )  // if last pass was terminated, initialize MQ-coder
       
   705                 {
       
   706                 // Update the number of available TUint8s for this terminated sequence
       
   707                 iMQDecoder.iInputStream.iNumBytes += aCodeblock.CblkLength( passIndex++ );
       
   708                 iMQDecoder.MqInitDecoder();
       
   709                 }
       
   710 
       
   711             SignificancePass();
       
   712             }
       
   713         else
       
   714             {
       
   715             // if we are here, then the previous pass was terminated
       
   716             if ( terminateEachPass )
       
   717                 {
       
   718                 // Update the number of available bytes for this terminated sequence 
       
   719                 iMQDecoder.iInputStream.iNumBytes += aCodeblock.CblkLength( passIndex++ );
       
   720                 }
       
   721             else  // AC bypass without termination on each pass 
       
   722                 {
       
   723                 // Here we have two cases:
       
   724                 // 1. If this codeblock length is for this pass only, then for the terminated length 
       
   725                 //    we have to add the next pass codeblock length also. 
       
   726                 // 2. This codeblock length is for both this pass ( significance ) and the next pass 
       
   727                 //    ( refinement ). Terminated length is equal to the CblkLength[iPassIndex].
       
   728                 if ( aCodeblock.PassesPerSegment( passIndex ) == 2 )
       
   729                     {
       
   730                     iMQDecoder.iInputStream.iNumBytes += aCodeblock.CblkLength( passIndex++ );
       
   731                     }
       
   732                 else
       
   733                     {
       
   734                     iMQDecoder.iInputStream.iNumBytes += aCodeblock.CblkLength( passIndex ) + 
       
   735                                                          aCodeblock.CblkLength( (TUint16)( passIndex + 1 ) );
       
   736                     passIndex += 2;
       
   737                     }
       
   738                 }
       
   739             iMQDecoder.iInputStream.ResetInputStream();
       
   740             LazySignificancePass();
       
   741             }
       
   742     
       
   743         aCodeblock.IncrementPassIndex();
       
   744         if ( aCodeblock.PassIndex() > aCodeblock.LastPass() )
       
   745             {
       
   746             // We have fully decoded this codeblock
       
   747             return;
       
   748             }
       
   749     
       
   750         // Magnitude refinement pass, terminate if raw coding or termination after each pass
       
   751         iTerminateThisPass = ( terminateEachPass || ( arithmeticBypass && ( aCodeblock.PassIndex() > 
       
   752                                                                             KFirstLazyPassIndex ) ) );
       
   753 
       
   754         if ( arithmeticBypass == 0 || ( aCodeblock.PassIndex() < KFirstLazyPassIndex ) )
       
   755             {
       
   756             if ( terminateEachPass ) // if previous pass was terminated, initialize MQ-coder 
       
   757                 {
       
   758                 // Update the number of available bytes for this terminated sequence
       
   759                 iMQDecoder.iInputStream.iNumBytes += aCodeblock.CblkLength( passIndex++ );
       
   760                 iMQDecoder.MqInitDecoder();
       
   761                 }
       
   762 
       
   763             RefinementPass();
       
   764             }
       
   765         else
       
   766             {
       
   767             if ( terminateEachPass )  // if previous pass was terminated
       
   768                 {
       
   769                 // Update the number of available bytes for this terminated sequence
       
   770                 iMQDecoder.iInputStream.iNumBytes += aCodeblock.CblkLength( passIndex++ );
       
   771                 iMQDecoder.iInputStream.ResetInputStream();
       
   772                 }
       
   773 
       
   774             LazyRefinementPass();
       
   775             }
       
   776     
       
   777         aCodeblock.IncrementPassIndex();
       
   778         if ( aCodeblock.PassIndex() > aCodeblock.LastPass() )
       
   779             {
       
   780             // We have fully decoded this codeblock
       
   781             return;
       
   782             }
       
   783     
       
   784         // Cleanup pass, terminate if coding raw, termination after each pass, last bitplane
       
   785         // before starting raw coding or last bitplane in this codeblock.
       
   786         iTerminateThisPass = ( terminateEachPass || ( iCurrentBitplane == endBitplane ) || 
       
   787                              ( arithmeticBypass && ( aCodeblock.PassIndex() >= KFirstLazyPassIndex ) ) );
       
   788     
       
   789         // If last ( AC ) pass was terminated, initialize MQ-coder
       
   790         if ( terminateEachPass || ( arithmeticBypass && ( aCodeblock.PassIndex() > KFirstLazyPassIndex ) ) )
       
   791             {
       
   792             // Update the number of available bytes for this terminated sequence
       
   793             iMQDecoder.iInputStream.iNumBytes += aCodeblock.CblkLength( passIndex++ );
       
   794             iMQDecoder.MqInitDecoder();
       
   795             }
       
   796 
       
   797         CleanupPass( segmentationSymbolsUsed );
       
   798     
       
   799         aCodeblock.IncrementPassIndex();
       
   800         if ( aCodeblock.PassIndex() > aCodeblock.LastPass() )
       
   801             {
       
   802             // We have fully decoded this codeblock
       
   803             return;
       
   804             }
       
   805 
       
   806         // move onto next bitplane
       
   807         if ( iCurrentBitplane == 0 )  // This might happen with 16-bit and ROI
       
   808             {
       
   809             // We have fully decoded this codeblock
       
   810             return;
       
   811             }
       
   812 
       
   813         iCurrentBitplane--;
       
   814         }
       
   815     }
       
   816 
       
   817 // -----------------------------------------------------------------------------
       
   818 // CJ2kEntropyDecoder::ConstructL
       
   819 // Symbian 2nd phase constructor can leave.
       
   820 // -----------------------------------------------------------------------------
       
   821 //
       
   822 void CJ2kEntropyDecoder::ConstructL( CJ2kImageInfo& aImageInfo )
       
   823     {
       
   824     iCurrentSize = aImageInfo.MaxBlockSize();
       
   825 
       
   826     for ( TUint16 i = 0; i < KNumberContexts; i++ )
       
   827         {
       
   828         J2KEntropyContexts *context = new ( ELeave ) J2KEntropyContexts;
       
   829         CleanupStack::PushL( context );
       
   830         User::LeaveIfError( iMQDecoder.iContextList.Append( context ) );
       
   831         CleanupStack::Pop(1);
       
   832         }
       
   833 
       
   834     for ( TUint16 ii = 0; ii < KNumberOriginalMQEntries; ii++ )
       
   835         {
       
   836         J2KEntropyStates *state = new ( ELeave ) J2KEntropyStates;
       
   837         CleanupStack::PushL( state );
       
   838         User::LeaveIfError( iMQDecoder.iOriginalStates.Append( state ) );
       
   839         CleanupStack::Pop(1);
       
   840         }
       
   841 
       
   842     // Allocate memory for the SC Look up table
       
   843     iSCLutBuf = HBufC8::NewL( KLutSize );
       
   844     iSCLut = CONST_CAST( TUint8*, iSCLutBuf->Des().Ptr() );
       
   845 
       
   846     // Allocate memory for the MR Look up table
       
   847     iMRLutBuf = HBufC8::NewL( KLutSize );
       
   848     iMRLut = CONST_CAST( TUint8*, iMRLutBuf->Des().Ptr() );
       
   849 
       
   850     // Allocate memory for the Zero coding Look up table
       
   851     iZcLutLL = HBufC8::NewL( ( 1 << KZcLutBits ) );
       
   852     iZcLutHL = HBufC8::NewL( ( 1 << KZcLutBits ) );
       
   853     iZcLutHH = HBufC8::NewL( ( 1 << KZcLutBits ) );
       
   854 
       
   855     iData = TJ2kUtils::Alloc2DArrayL( iCurrentSize.iHeight, iCurrentSize.iWidth );
       
   856 
       
   857     iMaxBlockWidth = (TUint16)( iCurrentSize.iWidth );
       
   858 
       
   859     // We can compute some parameters for entropy decoding here not to compute them for every pass!!!
       
   860     iBlockDataWidth        = iMaxBlockWidth;
       
   861     iStateWidth            = iMaxBlockWidth + 2;
       
   862     iStateSize             = ( iCurrentSize.iHeight + 2 ) * ( iCurrentSize.iWidth + 2 );
       
   863     iDataSamplesPerStripe  = iBlockDataWidth * KStripeHeight;
       
   864     iStateSamplesPerStripe = iStateWidth * KStripeHeight;
       
   865 
       
   866     iStates = STATIC_CAST( TInt16*, User::AllocL( iStateSize * sizeof( TInt16 ) ) );
       
   867 
       
   868     // Initialization of the MQ tables needed since
       
   869     // the original table could not be used as it was constant!
       
   870     iMQDecoder.InitializeOrigMqTable();
       
   871 
       
   872     iMaxBitDepth = 0;
       
   873     for ( TUint16 iii = 0; iii < aImageInfo.NumOfComponents(); ++iii )
       
   874         {
       
   875         iMaxBitDepth = Max( iMaxBitDepth, aImageInfo.DepthOfComponent( iii ) );
       
   876         }
       
   877 
       
   878     // Initialize ZC lookup table
       
   879     InitializeZCLut();
       
   880 
       
   881     // Initialize SC/MR lookup table
       
   882     InitializeScMrLut();
       
   883     }
       
   884 
       
   885 // -----------------------------------------------------------------------------
       
   886 // CJ2kEntropyDecoder::InitializeZCLut
       
   887 // Initialize ZC lookup table
       
   888 // (other items were commented in a header).
       
   889 // -----------------------------------------------------------------------------
       
   890 //
       
   891 void CJ2kEntropyDecoder::InitializeZCLut()
       
   892     {
       
   893     // Create pointers for all the ZC LUT's
       
   894     TUint8* iZcLutLLPtr = CONST_CAST( TUint8*, iZcLutLL->Des().Ptr() );
       
   895     TUint8* iZcLutHLPtr = CONST_CAST( TUint8*, iZcLutHL->Des().Ptr() );
       
   896     TUint8* iZcLutHHPtr = CONST_CAST( TUint8*, iZcLutHH->Des().Ptr() );
       
   897 
       
   898     TInt32 ctxt = 0; 
       
   899     TUint16 sh = 0;     // horizontal sum
       
   900     TUint16 sv = 0;     // vertical sum
       
   901     TUint16 sd = 0;     // diagonal sum
       
   902 
       
   903     for ( TUint16 i = 0; i <= KZcMask; i++ )
       
   904         {
       
   905         sh = (TUint16)( ( ( i >> KPositionRight ) & 1 ) + ( ( i >> KPositionLeft ) & 1 ) );
       
   906         sv = (TUint16)( ( ( i >> KPositionDown ) & 1 ) + ( ( i >> KPositionUp ) & 1 ) );
       
   907         sd = (TUint16)( ( ( i >> KPositionUpperLeft ) & 1 ) + ( ( i >> KPositionUpperRight ) & 1 ) + 
       
   908                       ( ( i >> KPositionLowerLeft ) & 1 ) + ( ( i >> KPositionLowerRight ) & 1 ) );
       
   909 
       
   910         if ( sh == 2 )
       
   911             {
       
   912             ctxt = 10;
       
   913             }
       
   914         else if ( sh == 1 )
       
   915             {
       
   916             if ( sv )
       
   917                 {
       
   918                 ctxt = 9;
       
   919                 }
       
   920             else if ( sd )
       
   921                 {
       
   922                 ctxt = 8;
       
   923                 }
       
   924             else
       
   925                 {
       
   926                 ctxt = 7;
       
   927                 }
       
   928             }
       
   929         else
       
   930             {
       
   931             if ( sv )
       
   932                 {
       
   933                 ctxt = 4 + sv;
       
   934                 }
       
   935             else
       
   936                 {
       
   937                 ctxt = 2 + ( ( sd > 2 ) ? 2 : sd );
       
   938                 }
       
   939             }
       
   940 
       
   941         iZcLutLLPtr[i] = (TUint8) ctxt;
       
   942 
       
   943 
       
   944         if ( sv == 2 )
       
   945             {
       
   946             ctxt = 10;
       
   947             }
       
   948         else if ( sv == 1 )
       
   949             {
       
   950             if ( sh )
       
   951                 {
       
   952                 ctxt = 9;
       
   953                 }
       
   954             else if ( sd )
       
   955                 {
       
   956                 ctxt = 8;
       
   957                 }
       
   958             else
       
   959                 {
       
   960                 ctxt = 7;
       
   961                 }
       
   962             }
       
   963         else
       
   964             {
       
   965             if ( sh )
       
   966                 {
       
   967                 ctxt = 4 + sh;
       
   968                 }
       
   969             else
       
   970                 {
       
   971                 ctxt = 2 + ( ( sd > 2 ) ? 2 : sd );
       
   972                 }
       
   973             }
       
   974 
       
   975         iZcLutHLPtr[i] = (TUint8) ctxt;
       
   976     
       
   977         if ( sd >= 3 )
       
   978             {
       
   979             ctxt = 10;
       
   980             }
       
   981         else if ( sd == 2 )
       
   982             {
       
   983             if ( ( sv + sh ) >= 1 )
       
   984                 {
       
   985                 ctxt = 9;
       
   986                 }
       
   987             else
       
   988                 {
       
   989                 ctxt = 8;
       
   990                 }
       
   991             }
       
   992         else if ( sd == 1 )
       
   993             {
       
   994             ctxt = 5 + ( ( ( sv + sh ) > 2 ) ? 2 : ( sv + sh ) );
       
   995             }
       
   996         else
       
   997             {
       
   998             ctxt = 2 + ( ( ( sv + sh ) > 2 ) ? 2 : ( sv + sh ) );
       
   999             }
       
  1000     
       
  1001         iZcLutHHPtr[i] = (TUint8) ctxt;
       
  1002         } 
       
  1003     }
       
  1004 
       
  1005 // -----------------------------------------------------------------------------
       
  1006 // CJ2kEntropyDecoder::InitializeScMrLut
       
  1007 // Initialize SC/MR lookup table
       
  1008 // (other items were commented in a header).
       
  1009 // -----------------------------------------------------------------------------
       
  1010 //
       
  1011 void CJ2kEntropyDecoder::InitializeScMrLut()
       
  1012     {
       
  1013     TInt32 ctxt = 0; 
       
  1014     TInt32 vpos = 0;    // vert +
       
  1015     TInt32 vneg = 0;    // vert -
       
  1016     TInt32 hpos = 0;    // hor +
       
  1017     TInt32 hneg = 0;    // hor -
       
  1018     TInt32 hc = 0;      // horizontal contribution
       
  1019     TInt32 vc = 0;      // vertical contribution
       
  1020     TInt16 predict = 0; // the xor bit for differentiating states. ( positioned at the 6th bit )
       
  1021 
       
  1022     for ( TUint8 i = 0; i < 16; i++ )
       
  1023         {
       
  1024         vpos = i & 1;
       
  1025         vneg = ( i >> 1 ) & 1;
       
  1026         hpos = ( i >> 2 ) & 1;
       
  1027         hneg = ( i >> 3 ) & 1;
       
  1028         hc = hpos - hneg;
       
  1029         vc = vpos - vneg;
       
  1030         predict = 0;
       
  1031         if ( hc < 0 )
       
  1032             {
       
  1033             predict = 1;
       
  1034             hc = -hc;
       
  1035             vc = -vc;
       
  1036             }
       
  1037         if ( hc == 0 )
       
  1038             {
       
  1039             if ( vc < 0 )
       
  1040                 {
       
  1041                 predict = 1;
       
  1042                 vc = -vc;
       
  1043                 }
       
  1044             ctxt = 11 + vc;
       
  1045             }
       
  1046         else
       
  1047             {
       
  1048             ctxt = 14 + vc;
       
  1049             }
       
  1050 
       
  1051         iSCLut[i] = (TUint8) ( ctxt | ( predict << KPredictionBit ) );
       
  1052         }
       
  1053     iMRLut[0] = 16;
       
  1054     iMRLut[1] = 17;
       
  1055     iMRLut[2] = 18;
       
  1056     }
       
  1057 
       
  1058 // -----------------------------------------------------------------------------
       
  1059 // CJ2kEntropyDecoder::GetCausal
       
  1060 // Get the casual
       
  1061 // (other items were commented in a header).
       
  1062 // -----------------------------------------------------------------------------
       
  1063 //
       
  1064 void CJ2kEntropyDecoder::GetCausal( TPrecInt*& aDataCol, TInt16*& aStateCol )
       
  1065     {
       
  1066     TInt32 vpos = 0;
       
  1067     TInt32 vneg = 0;
       
  1068     TUint16 state = 0;
       
  1069     TInt16 causalityMask = 0;
       
  1070 
       
  1071     aDataCol--;
       
  1072     for ( TInt32 col = iStateWidth; col > 0; col-- )
       
  1073         {
       
  1074         state = *aStateCol;
       
  1075         vpos = 0;
       
  1076         vneg = 0;
       
  1077         if ( ( ( state >> KPositionDown ) & 1 ) == 1 )
       
  1078             {
       
  1079             if ( ( ( state >> KPositionUp ) & 1 ) == 1 )
       
  1080                 {
       
  1081                 if ( col < iStateWidth )
       
  1082                     {
       
  1083                     if ( *aDataCol >= 0 )
       
  1084                         {
       
  1085                         vneg = 1;
       
  1086                         }
       
  1087                     else
       
  1088                         {
       
  1089                         vpos = 1;
       
  1090                         }
       
  1091                     }
       
  1092                 }
       
  1093             else
       
  1094                 { 
       
  1095                 vpos = 1;
       
  1096                 vneg = 1;
       
  1097                 }
       
  1098             }
       
  1099         causalityMask = (TInt16)( ~( ( KStateDown| KStateLowerLeft | KStateLowerRight ) | 
       
  1100                                      ( vneg << KPositionVerNeg ) | ( vpos << KPositionVerPos ) ) );
       
  1101 
       
  1102         ( *aStateCol ) &= causalityMask;
       
  1103         aDataCol++;
       
  1104         aStateCol++;
       
  1105         }
       
  1106     }
       
  1107 
       
  1108 // -----------------------------------------------------------------------------
       
  1109 // CJ2kEntropyDecoder::DecodeSignificance
       
  1110 // Decode the significance bit
       
  1111 // (other items were commented in a header).
       
  1112 // -----------------------------------------------------------------------------
       
  1113 //
       
  1114 void CJ2kEntropyDecoder::DecodeSignificance( TPrecInt*& aDataValue, TInt16*& aStateValue, TPrecInt aMask )
       
  1115     {
       
  1116     TUint32 state = *aStateValue;
       
  1117     TInt32 context = state & KZcMask;
       
  1118     if ( ( state & KStateSignificant ) == 0 && context > 0 )
       
  1119         { 
       
  1120         if ( iMQDecoder.MqDecodeSymbol( iCurrentZcLutPtr[context] ) != 0 )
       
  1121             {
       
  1122             TUint32 ctxt = iSCLut[( state >> KScShift ) & KScLutMask];
       
  1123             TInt32 symbol = iMQDecoder.MqDecodeSymbol( ( ctxt & KScLutMask ) ) ^ ( ( ctxt >> KPredictionBit ) & 1 );
       
  1124             *aDataValue = ( symbol << KSignShift ) | aMask;
       
  1125             *aStateValue |= KStateSignificant | KStateVisited;
       
  1126             UpdateSignificance( aStateValue, symbol );
       
  1127             }
       
  1128         else
       
  1129             {
       
  1130             ( *aStateValue ) |= KStateVisited;
       
  1131             }
       
  1132         }
       
  1133     }
       
  1134 
       
  1135 // -----------------------------------------------------------------------------
       
  1136 // CJ2kEntropyDecoder::DecodeRawSignificance
       
  1137 // Decode the lazy significance bit
       
  1138 // (other items were commented in a header).
       
  1139 // -----------------------------------------------------------------------------
       
  1140 //
       
  1141 void CJ2kEntropyDecoder::DecodeRawSignificance( TPrecInt*& aDataValue, TInt16*& aStateValue, TPrecInt aMask )
       
  1142     {
       
  1143     TInt32 state = *aStateValue;
       
  1144     if ( ( state & KStateSignificant ) == 0 && ( state & KZcMask ) > 0 )
       
  1145         { 
       
  1146         if ( iMQDecoder.iInputStream.ReadBitFromStream() )
       
  1147             {
       
  1148             TInt32 symbol = iMQDecoder.iInputStream.ReadBitFromStream();
       
  1149             *aDataValue = ( symbol << KSignShift ) | aMask;
       
  1150             *aStateValue |= KStateSignificant | KStateVisited;
       
  1151             UpdateSignificance( aStateValue, symbol );
       
  1152             }
       
  1153         else
       
  1154             {
       
  1155             ( *aStateValue ) |= KStateVisited;
       
  1156             }
       
  1157         }
       
  1158     }
       
  1159 
       
  1160 // -----------------------------------------------------------------------------
       
  1161 // CJ2kEntropyDecoder::DecodeRefinement
       
  1162 // Decode the refinement bit
       
  1163 // (other items were commented in a header).
       
  1164 // -----------------------------------------------------------------------------
       
  1165 //
       
  1166 void CJ2kEntropyDecoder::DecodeRefinement( TPrecInt*& aDataValue, TInt16*& aStateValue, 
       
  1167                                            TPrecInt aMask, TInt32 aResetMask )
       
  1168     {
       
  1169     TInt32 state = *aStateValue;
       
  1170     if ( ( state & ( KStateSignificant | KStateVisited ) ) == KStateSignificant )
       
  1171         {
       
  1172         TInt16 mrContext = (TInt16)( state & KStatePreviousMR );
       
  1173         TInt32 ctxt = 0;
       
  1174         if ( mrContext )
       
  1175             {
       
  1176             ctxt = 2;
       
  1177             }
       
  1178         else
       
  1179             {
       
  1180             if ( ( state & KZcMask ) == 0 )
       
  1181                 {
       
  1182                 ctxt = 0;
       
  1183                 }
       
  1184             else
       
  1185                 {
       
  1186                 ctxt = 1;
       
  1187                 }
       
  1188             } 
       
  1189         TInt32 symbol = iMQDecoder.MqDecodeSymbol( iMRLut[ctxt] );
       
  1190         ( *aDataValue ) &= aResetMask;
       
  1191         ( *aDataValue ) |= ( symbol << iCurrentBitplane ) | aMask;
       
  1192         ( *aStateValue ) |= KStatePreviousMR;
       
  1193         }
       
  1194     }
       
  1195 
       
  1196 // -----------------------------------------------------------------------------
       
  1197 // CJ2kEntropyDecoder::DecodeRawRefinement
       
  1198 // Decode the lazy refinement bit
       
  1199 // (other items were commented in a header).
       
  1200 // -----------------------------------------------------------------------------
       
  1201 //
       
  1202 void CJ2kEntropyDecoder::DecodeRawRefinement( TPrecInt*& aDataValue, TInt16*& aStateValue, 
       
  1203                                               TPrecInt aMask, TInt32 aResetMask )
       
  1204     {
       
  1205     if ( ( ( *aStateValue ) & ( KStateSignificant | KStateVisited ) ) == KStateSignificant )
       
  1206         {
       
  1207         TInt32 symbol = iMQDecoder.iInputStream.ReadBitFromStream();
       
  1208         ( *aDataValue ) &= aResetMask;
       
  1209         ( *aDataValue ) |= ( symbol << iCurrentBitplane ) | aMask;
       
  1210         ( *aStateValue ) |= KStatePreviousMR;
       
  1211         }
       
  1212     }
       
  1213 
       
  1214 // -----------------------------------------------------------------------------
       
  1215 // CJ2kEntropyDecoder::DecodeNormalization
       
  1216 // Perform the normalization
       
  1217 // (other items were commented in a header).
       
  1218 // -----------------------------------------------------------------------------
       
  1219 //
       
  1220 void CJ2kEntropyDecoder::DecodeNormalization( TPrecInt*& aDataValue, TInt16*& aStateValue, 
       
  1221                                               TPrecInt aMask, TInt32& aZCValue )
       
  1222     {
       
  1223     TUint32 state = *aStateValue;
       
  1224     if ( state < (TUint32)KStateVisited )
       
  1225         {
       
  1226         TInt32 symbol = 0;
       
  1227         TUint32 ctxt = 0;
       
  1228         if ( aZCValue == 1 )
       
  1229             {
       
  1230             ( *aStateValue ) &= ~KStateVisited;
       
  1231             if ( iMQDecoder.MqDecodeSymbol( iCurrentZcLutPtr[state & KZcMask] ) != 0 )
       
  1232                 {
       
  1233                 ctxt = iSCLut[( state >> KScShift ) & KScLutMask];
       
  1234                 symbol = iMQDecoder.MqDecodeSymbol( ( ctxt & KScLutMask ) ) ^( ( ctxt >> KPredictionBit ) & 1 );
       
  1235                 ( *aDataValue ) = ( symbol << KSignShift ) | aMask;
       
  1236                 ( *aStateValue ) |= KStateSignificant;
       
  1237                 UpdateSignificance( aStateValue, symbol );
       
  1238                 aZCValue = 1;
       
  1239                 }
       
  1240             }
       
  1241         else
       
  1242             {
       
  1243             ctxt = iSCLut[( state >> KScShift ) & KScLutMask];
       
  1244             symbol = iMQDecoder.MqDecodeSymbol( ( ctxt & KScLutMask ) ) ^ ( ( ctxt >> KPredictionBit ) & 1 );
       
  1245             ( *aDataValue ) = ( symbol << KSignShift ) | aMask;
       
  1246             ( *aStateValue ) |= KStateSignificant; \
       
  1247             UpdateSignificance( aStateValue, symbol );
       
  1248             aZCValue = 1; 
       
  1249             }
       
  1250         } 
       
  1251     else
       
  1252         {
       
  1253         ( *aStateValue ) &= ( ~KStateVisited );
       
  1254         }
       
  1255     }
       
  1256 
       
  1257 // -----------------------------------------------------------------------------
       
  1258 // CJ2kEntropyDecoder::UpdateSignificance
       
  1259 // Update the significance
       
  1260 // (other items were commented in a header).
       
  1261 // -----------------------------------------------------------------------------
       
  1262 //
       
  1263 void CJ2kEntropyDecoder::UpdateSignificance( TInt16*& aStateValue, TInt32 aSymbol )
       
  1264     {
       
  1265     TInt16* u = ( aStateValue ) - ( iStateWidth );
       
  1266     TInt16* d = ( aStateValue ) + ( iStateWidth );
       
  1267     u[-1] |= KStateLowerRight;
       
  1268     u[1] |= KStateLowerLeft;
       
  1269     d[-1] |= KStateUpperRight;
       
  1270     d[1] |= KStateUpperLeft;
       
  1271 
       
  1272     if ( aSymbol != 0 )
       
  1273         {
       
  1274         aStateValue[-1] |= KStateRight | KStateHorNegative;
       
  1275         aStateValue[1] |= KStateLeft | KStateHorNegative;
       
  1276         *d |= KStateUp | KStateVerNegative;
       
  1277         *u |= KStateDown | KStateVerNegative;
       
  1278         }
       
  1279     else
       
  1280         {
       
  1281         aStateValue[-1] |= KStateRight | KStateHorPositive;
       
  1282         aStateValue[1] |= KStateLeft | KStateHorPositive;
       
  1283         *d |= KStateUp | KStateVerPositive;
       
  1284         *u |= KStateDown | KStateVerPositive;
       
  1285         }
       
  1286     }
       
  1287 
       
  1288 // -----------------------------------------------------------------------------
       
  1289 // CJ2kEntropyDecoder::SignificancePass
       
  1290 // Perform the significance pass
       
  1291 // (other items were commented in a header).
       
  1292 // -----------------------------------------------------------------------------
       
  1293 //
       
  1294 void CJ2kEntropyDecoder::SignificancePass()
       
  1295     {
       
  1296     // Mask for the current bitplane is actually also an approximation of the 
       
  1297     // lower bitplanes, i.e. mask might be 00001100 for the fourth bitplane instead
       
  1298     // of 00001000, thus the data will be increased by the current bitplane and
       
  1299     // half of the sum of the lower bitplanes.
       
  1300     TPrecInt mask = ( 1 << iCurrentBitplane ) | ( 1 << ( iCurrentBitplane - 1 ) );
       
  1301   
       
  1302     TPrecInt* dataRow = &( iData[0][0] );
       
  1303     TInt16*   stateRow = CONST_CAST( TInt16*, GetFirstStateRow(  ) );
       
  1304 
       
  1305     TPrecInt* dataCol = 0;
       
  1306     TPrecInt* dataValue = 0; 
       
  1307     TInt16*   stateCol = 0;
       
  1308     TInt16*   stateValue = 0;
       
  1309     
       
  1310     // Column index in the stripe
       
  1311     TInt32 col = 0; 
       
  1312     TInt32 row = 0;
       
  1313 
       
  1314     // Code stripe by stripe
       
  1315     for ( TInt32 s = iNumStripes - 1; s > 0; s-- )
       
  1316         {
       
  1317         // If using stripe causal context formation, reset necessary bits
       
  1318         // in state of first line in next stripe ( not on last
       
  1319         // stripe ).
       
  1320         if ( iVerticalCausalContextUsed )
       
  1321             {
       
  1322             dataCol = dataRow + 2 * iBlockDataWidth;
       
  1323             stateCol = stateRow + iStateSamplesPerStripe - iStateWidth - 1;
       
  1324             GetCausal( dataCol, stateCol );
       
  1325             }
       
  1326   
       
  1327         dataCol = dataRow;
       
  1328         stateCol = stateRow;
       
  1329     
       
  1330         // Scan column by column in each stripe
       
  1331         for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1332             {
       
  1333             stateValue = stateCol;
       
  1334             dataValue = dataCol;
       
  1335 
       
  1336             for ( row = KStripeHeight; row > 0; row-- )
       
  1337                 {
       
  1338                 DecodeSignificance( dataValue, stateValue, mask );
       
  1339                 dataValue += iBlockDataWidth;
       
  1340                 stateValue += iStateWidth;
       
  1341                 }
       
  1342             }
       
  1343 
       
  1344         dataRow += iDataSamplesPerStripe;
       
  1345         stateRow += iStateSamplesPerStripe;
       
  1346         }
       
  1347 
       
  1348     // Deal with the last stripe separately
       
  1349     dataCol = dataRow;
       
  1350     stateCol = stateRow;
       
  1351     
       
  1352     // Scan column by column in each stripe
       
  1353     for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1354         {
       
  1355         stateValue = stateCol;
       
  1356         dataValue = dataCol;
       
  1357 
       
  1358         for ( row = iLastStripeHeight; row > 0; row-- )
       
  1359             {
       
  1360             DecodeSignificance( dataValue, stateValue, mask );
       
  1361             if ( row > 1 )
       
  1362                 {
       
  1363                 dataValue += iBlockDataWidth;
       
  1364                 }
       
  1365             stateValue += iStateWidth;
       
  1366             }
       
  1367         }
       
  1368 
       
  1369     // If there is predictable termination, check for errors
       
  1370     if ( iTerminateThisPass && iPredictableTerminationUsed )
       
  1371         {
       
  1372         iErrorState = iMQDecoder.MqCheckPrediction();
       
  1373         }
       
  1374   
       
  1375     // Reset the MQ context states if we need to
       
  1376     if ( iResetContexts )
       
  1377         {
       
  1378         iMQDecoder.ResetMqContexts();
       
  1379         }
       
  1380     }
       
  1381 
       
  1382 // -----------------------------------------------------------------------------
       
  1383 // CJ2kEntropyDecoder::LazySignificancePass
       
  1384 // Perform the lazy significance pass
       
  1385 // (other items were commented in a header).
       
  1386 // -----------------------------------------------------------------------------
       
  1387 //
       
  1388 void CJ2kEntropyDecoder::LazySignificancePass()
       
  1389     {
       
  1390     // Mask for the current bitplane 
       
  1391     TPrecInt mask = ( 1 << iCurrentBitplane ) | ( 1 << ( iCurrentBitplane - 1 ) );
       
  1392   
       
  1393     TPrecInt* dataRow = &( iData[0][0] );
       
  1394     TInt16*   stateRow = CONST_CAST( TInt16*, GetFirstStateRow() );
       
  1395     TPrecInt* dataCol = 0;
       
  1396     TPrecInt* dataValue = 0; 
       
  1397     TInt16* stateCol = 0;
       
  1398     TInt16* stateValue = 0;
       
  1399 
       
  1400     // Column index in the stripe
       
  1401     TInt32 col = 0; 
       
  1402     TInt32 row = 0;
       
  1403 
       
  1404     for ( TInt32 s = iNumStripes - 1; s > 0; s-- )
       
  1405         {
       
  1406         // If using stripe causal context formation, reset necessary bits
       
  1407         // in state of first line in next stripe ( not on last
       
  1408         // stripe ).
       
  1409         if ( iVerticalCausalContextUsed )
       
  1410             {
       
  1411             dataCol = dataRow + 2 * iBlockDataWidth;
       
  1412             stateCol = stateRow + iStateSamplesPerStripe - iStateWidth - 1;
       
  1413             GetCausal( dataCol, stateCol );
       
  1414             }
       
  1415 
       
  1416         // Scan column by column in each stripe
       
  1417         dataCol = dataRow;
       
  1418         stateCol = stateRow;
       
  1419     
       
  1420         // Scan column by column in each stripe 
       
  1421         for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1422             {
       
  1423             stateValue = stateCol;
       
  1424             dataValue = dataCol;
       
  1425 
       
  1426             for ( row = KStripeHeight; row > 0; row-- )
       
  1427                 {
       
  1428                 DecodeRawSignificance( dataValue, stateValue, mask );
       
  1429                 dataValue += iBlockDataWidth;
       
  1430                 stateValue += iStateWidth;
       
  1431                 }
       
  1432             }
       
  1433         dataRow += iDataSamplesPerStripe;
       
  1434         stateRow += iStateSamplesPerStripe;
       
  1435         }
       
  1436 
       
  1437     // Deal with the last stripe separately
       
  1438     // Scan column by column in each stripe
       
  1439     dataCol = dataRow;
       
  1440     stateCol = stateRow;
       
  1441   
       
  1442     // Line index
       
  1443     TInt32 l = 0;
       
  1444     
       
  1445     // Scan column by column in each stripe 
       
  1446     for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1447         {
       
  1448         stateValue = stateCol;
       
  1449         dataValue = dataCol;
       
  1450 
       
  1451         l = iLastStripeHeight;
       
  1452         DecodeRawSignificance( dataValue, stateValue, mask );
       
  1453         if ( --l <= 0 )
       
  1454             {
       
  1455             continue;   //lint !e960    Continue is OK, no need to go end of for.
       
  1456             }
       
  1457     
       
  1458         dataValue += iBlockDataWidth;
       
  1459         stateValue += iStateWidth;
       
  1460         DecodeRawSignificance( dataValue, stateValue, mask );
       
  1461         if ( --l <= 0 )
       
  1462             {
       
  1463             continue;   //lint !e960    Continue is OK, no need to go end of for.
       
  1464             }
       
  1465     
       
  1466         dataValue += iBlockDataWidth;
       
  1467         stateValue += iStateWidth;
       
  1468         DecodeRawSignificance( dataValue, stateValue, mask );
       
  1469         if ( --l <= 0 )
       
  1470             {
       
  1471             continue;   //lint !e960    Continue is OK, no need to go end of for.
       
  1472             }
       
  1473     
       
  1474         dataValue += iBlockDataWidth;
       
  1475         stateValue += iStateWidth;
       
  1476         DecodeRawSignificance( dataValue, stateValue, mask );
       
  1477 
       
  1478         }
       
  1479   
       
  1480     // If there is predictable termination, check for errors 
       
  1481     if ( iTerminateThisPass && iPredictableTerminationUsed )
       
  1482         {
       
  1483         iErrorState = iMQDecoder.iInputStream.CheckPrediction();
       
  1484         }
       
  1485     }
       
  1486 
       
  1487 // -----------------------------------------------------------------------------
       
  1488 // CJ2kEntropyDecoder::RefinementPass
       
  1489 // Perform the refinement pass
       
  1490 // (other items were commented in a header).
       
  1491 // -----------------------------------------------------------------------------
       
  1492 //
       
  1493 void CJ2kEntropyDecoder::RefinementPass()
       
  1494     {
       
  1495     // To reset the effects of the approximation done in significance and
       
  1496     // normalization passes we have the resetmask.
       
  1497     TInt32 resetMask = ( -1 ) << ( iCurrentBitplane + 1 );
       
  1498     TUint32 mask = ( ( TUint32 )( 1 << iCurrentBitplane ) ) >> 1;
       
  1499   
       
  1500     TPrecInt* dataRow = &( iData[0][0] );
       
  1501     TInt16*   stateRow = CONST_CAST( TInt16*, GetFirstStateRow() );
       
  1502 
       
  1503     TPrecInt* dataCol = 0;
       
  1504     TPrecInt* dataValue = 0; 
       
  1505     TInt16* stateCol = 0;
       
  1506     TInt16* stateValue = 0;
       
  1507 
       
  1508     // Column index in the stripe
       
  1509     TInt32 col = 0; 
       
  1510     TInt32 row = 0;
       
  1511   
       
  1512     // Code stripe by stripe 
       
  1513     for ( TInt32 s = iNumStripes - 1; s > 0; s-- )
       
  1514         {
       
  1515         // If using stripe causal context formation, reset necessary bits
       
  1516         // in state of first line in next stripe ( not on last
       
  1517         // stripe ).
       
  1518         if ( iVerticalCausalContextUsed )
       
  1519             {
       
  1520             dataCol = dataRow + 2 * iBlockDataWidth;
       
  1521             stateCol = stateRow + iStateSamplesPerStripe - iStateWidth - 1;
       
  1522             GetCausal( dataCol, stateCol );
       
  1523             }
       
  1524     
       
  1525         // Scan column by column in each stripe
       
  1526         dataCol = dataRow;
       
  1527         stateCol = stateRow;
       
  1528     
       
  1529         // Scan column by column in each stripe
       
  1530         for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1531             {
       
  1532             dataValue  = dataCol;
       
  1533             stateValue = stateCol;
       
  1534 
       
  1535             for ( row = KStripeHeight; row > 0; row-- )
       
  1536                 {
       
  1537                 DecodeRefinement( dataValue, stateValue, mask, resetMask );
       
  1538                 dataValue += iBlockDataWidth;
       
  1539                 stateValue += iStateWidth;
       
  1540                 }
       
  1541             }
       
  1542         dataRow += iDataSamplesPerStripe;
       
  1543         stateRow += iStateSamplesPerStripe;
       
  1544         }
       
  1545 
       
  1546     // Deal with the last stripe separately
       
  1547     // Scan column by column in each stripe
       
  1548     dataCol = dataRow;
       
  1549     stateCol = stateRow;
       
  1550   
       
  1551     // Scan column by column in the last stripe
       
  1552     for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1553         {
       
  1554         dataValue  = dataCol;
       
  1555         stateValue = stateCol;
       
  1556 
       
  1557         for ( row = iLastStripeHeight; row > 0; row-- )
       
  1558             {
       
  1559             DecodeRefinement( dataValue, stateValue, mask, resetMask );
       
  1560             if ( row > 1 )
       
  1561                 {
       
  1562                 dataValue += iBlockDataWidth;
       
  1563                 }
       
  1564             stateValue += iStateWidth;
       
  1565             }
       
  1566         }
       
  1567   
       
  1568     // If there is predictable termination, check for errors 
       
  1569     if ( iTerminateThisPass && iPredictableTerminationUsed )
       
  1570         {
       
  1571         iErrorState = iMQDecoder.MqCheckPrediction();
       
  1572         }
       
  1573   
       
  1574     // Reset the MQ context states if we need to
       
  1575     if ( iResetContexts )
       
  1576         {
       
  1577         iMQDecoder.ResetMqContexts();
       
  1578         }
       
  1579     }
       
  1580 
       
  1581 // -----------------------------------------------------------------------------
       
  1582 // CJ2kEntropyDecoder::LazyRefinementPass
       
  1583 // Perform the lazy refinement pass
       
  1584 // (other items were commented in a header).
       
  1585 // -----------------------------------------------------------------------------
       
  1586 //
       
  1587 void CJ2kEntropyDecoder::LazyRefinementPass()
       
  1588     {  
       
  1589     TInt32 resetMask = ( -1 ) << ( iCurrentBitplane + 1 );
       
  1590     TUint32 mask = ( ( TUint32 )( 1 << iCurrentBitplane ) ) >> 1;
       
  1591     TPrecInt* dataRow = &( iData[0][0] );
       
  1592     TInt16*   stateRow = CONST_CAST( TInt16*, GetFirstStateRow() );
       
  1593     TPrecInt* dataCol = 0;
       
  1594     TPrecInt* dataValue = 0; 
       
  1595     TInt16* stateCol = 0;
       
  1596     TInt16* stateValue = 0;
       
  1597 
       
  1598     // Column index in the stripe
       
  1599     TInt32 col = 0; 
       
  1600     TInt32 row = 0;
       
  1601 
       
  1602     // Code stripe by stripe
       
  1603     for ( TInt32 s = iNumStripes - 1; s > 0; s-- )
       
  1604         {
       
  1605         // If using stripe causal context formation, reset necessary bits
       
  1606         // in state of first line in next stripe ( not on last
       
  1607         // stripe ).
       
  1608         if ( iVerticalCausalContextUsed )
       
  1609             {
       
  1610             dataCol = dataRow + 2 * iBlockDataWidth;
       
  1611             stateCol = stateRow + iStateSamplesPerStripe - iStateWidth - 1;
       
  1612             GetCausal( dataCol, stateCol );
       
  1613             }
       
  1614     
       
  1615         // Scan column by column in each stripe
       
  1616         dataCol = dataRow;
       
  1617         stateCol = stateRow;
       
  1618     
       
  1619         // Scan column by column in each stripe
       
  1620         for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1621             {
       
  1622             dataValue  = dataCol;
       
  1623             stateValue = stateCol;
       
  1624             for ( row = KStripeHeight; row > 0; row-- )
       
  1625                 {
       
  1626                 DecodeRawRefinement( dataValue, stateValue, mask, resetMask );
       
  1627                 dataValue += iBlockDataWidth;
       
  1628                 stateValue += iStateWidth;
       
  1629                 }
       
  1630             }
       
  1631         dataRow += iDataSamplesPerStripe;
       
  1632         stateRow += iStateSamplesPerStripe;
       
  1633         }
       
  1634 
       
  1635     // Deal with the last stripe separately
       
  1636     // Scan column by column in each stripe
       
  1637     dataCol = dataRow;
       
  1638     stateCol = stateRow;
       
  1639   
       
  1640     // Scan column by column in each stripe
       
  1641     for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1642         {
       
  1643         dataValue  = dataCol;
       
  1644         stateValue = stateCol;
       
  1645 
       
  1646         for ( row = iLastStripeHeight; row > 0; row-- )
       
  1647             {
       
  1648             DecodeRawRefinement( dataValue, stateValue, mask, resetMask );
       
  1649             dataValue += iBlockDataWidth;
       
  1650             stateValue += iStateWidth;
       
  1651             }
       
  1652         stateValue = stateCol;
       
  1653         dataValue = dataCol;
       
  1654         }
       
  1655   
       
  1656     // If there is predictable termination, check for errors
       
  1657     if ( iTerminateThisPass && iPredictableTerminationUsed )
       
  1658         {
       
  1659         iErrorState = iMQDecoder.iInputStream.CheckPrediction();
       
  1660         }
       
  1661 
       
  1662     // Because this is always terminated, reset stream
       
  1663     iMQDecoder.iInputStream.ResetInputStream();
       
  1664     }
       
  1665 
       
  1666 // -----------------------------------------------------------------------------
       
  1667 // CJ2kEntropyDecoder::CleanupPass
       
  1668 // Perform the cleanup pass
       
  1669 // (other items were commented in a header).
       
  1670 // -----------------------------------------------------------------------------
       
  1671 //
       
  1672 void CJ2kEntropyDecoder::CleanupPass( TUint8 aSegSymbols )
       
  1673     {
       
  1674     TInt32 zcCoding = 0;
       
  1675 
       
  1676     // Symbol to be coded 
       
  1677     TInt32 symbol = 0;   
       
  1678   
       
  1679     TPrecInt mask = ( 1 << iCurrentBitplane ) | ( 1 << ( iCurrentBitplane - 1 ) );
       
  1680   
       
  1681     TPrecInt* dataRow = &( iData[0][0] );
       
  1682     TInt16* stateRow = CONST_CAST( TInt16*, GetFirstStateRow() );
       
  1683     TPrecInt* dataCol = 0;
       
  1684     TPrecInt* dataValue = 0; 
       
  1685     TInt16* stateCol = 0;
       
  1686     TInt16* stateValue = 0;
       
  1687 
       
  1688     // Column index in the stripe
       
  1689     TInt32 col = 0; 
       
  1690     TInt32 row = 0;
       
  1691 
       
  1692     // Code stripe by stripe
       
  1693     for ( TInt32 s = iNumStripes - 1; s > 0; s-- )
       
  1694         {
       
  1695         // If using stripe causal context formation, reset necessary bits
       
  1696         // in state of first line in next stripe 
       
  1697         if ( iVerticalCausalContextUsed )
       
  1698             {
       
  1699             dataCol = dataRow + 2 * iBlockDataWidth;
       
  1700             stateCol = stateRow + iStateSamplesPerStripe - iStateWidth - 1;
       
  1701             GetCausal( dataCol, stateCol );
       
  1702             }
       
  1703     
       
  1704         // Scan column by column in each stripe
       
  1705         dataCol = dataRow;
       
  1706         stateCol = stateRow;
       
  1707     
       
  1708         for ( col = iBlockWidth; col > 0; col--, dataCol++, stateCol++ )
       
  1709             {
       
  1710             stateValue = stateCol;
       
  1711             dataValue = dataCol;
       
  1712 
       
  1713             // If sample is insignificant and not yet visited in the
       
  1714             // current bitplane, then apply the normalization pass
       
  1715             if ( ( *stateValue == 0 ) &&
       
  1716                 ( stateValue += iStateWidth, ( *stateValue ) == 0 ) &&    //lint !e960    Comma is OK.
       
  1717                 ( stateValue += iStateWidth, ( *stateValue ) == 0 ) &&    //lint !e960    Comma is OK.
       
  1718                 ( stateValue += iStateWidth, ( *stateValue ) == 0 ) )      //lint !e960    Comma is OK.
       
  1719                 {
       
  1720                 // Use RLC
       
  1721                 if ( iMQDecoder.MqDecodeSymbol( KRlcContext ) != 0 ) //some sample( s ) not zero
       
  1722                     {
       
  1723                     // Decode the symbol most significant bit first 
       
  1724                     symbol  = (TUint8)( iMQDecoder.MqDecodeSymbol( KUniformContext ) << 1 );    // MSB 
       
  1725                     symbol |= (TUint8)( iMQDecoder.MqDecodeSymbol( KUniformContext ) & 0x01 );  // LSB
       
  1726           
       
  1727                     // Update data and state pointers
       
  1728                     stateValue = stateCol + symbol * iStateWidth;
       
  1729                     dataValue  = dataCol + symbol * iBlockDataWidth;
       
  1730 
       
  1731                     // Set zcCoding flag to zero 
       
  1732                     zcCoding = 0;
       
  1733                     row = KStripeHeight - symbol;
       
  1734                     }
       
  1735                 else // All samples zero 
       
  1736                     {
       
  1737                     // Move to next column
       
  1738                     continue;   //lint !e960    Continue is OK, no need to go end of for.
       
  1739                     }
       
  1740                 }
       
  1741             else  // No RLC used 
       
  1742                 {
       
  1743                 stateValue = stateCol;
       
  1744 
       
  1745                 // Set zcCoding flag to one
       
  1746                 zcCoding = 1;
       
  1747                 row = KStripeHeight;
       
  1748                 }
       
  1749 
       
  1750             for ( ; row > 0; row-- )
       
  1751                 {
       
  1752                 DecodeNormalization( dataValue, stateValue, mask, zcCoding );
       
  1753                 dataValue += iBlockDataWidth;
       
  1754                 stateValue += iStateWidth;
       
  1755                 }
       
  1756             }
       
  1757         dataRow += iDataSamplesPerStripe;
       
  1758         stateRow += iStateSamplesPerStripe;
       
  1759         }
       
  1760   
       
  1761     // Deal with last stripe separately
       
  1762     // Scan column by column in each stripe
       
  1763     dataCol = dataRow;
       
  1764     stateCol = stateRow;
       
  1765   
       
  1766     // Scan column by column in each stripe
       
  1767     for ( col = iBlockWidth; col > 0; col--, stateCol++, dataCol++ )
       
  1768         {
       
  1769         stateValue = stateCol;
       
  1770         dataValue = dataCol;
       
  1771 
       
  1772         // If sample is insignificant and not yet visited in the
       
  1773         // current bitplane, then apply the normalization pass
       
  1774         if( ( iLastStripeHeight == KStripeHeight ) && ( *stateValue ==0 ) &&
       
  1775            ( stateValue += iStateWidth, ( *stateValue ) == 0 ) &&    //lint !e960    Comma is OK.
       
  1776            ( stateValue += iStateWidth, ( *stateValue ) == 0 ) &&    //lint !e960    Comma is OK.
       
  1777            ( stateValue += iStateWidth, ( *stateValue ) == 0 ) )      //lint !e960    Comma is OK.
       
  1778             {
       
  1779             // Use RLC:
       
  1780             if ( iMQDecoder.MqDecodeSymbol( KRlcContext ) != 0 )  //some sample( s ) not zero
       
  1781                 {
       
  1782                 // Decode the symbol most significant bit first
       
  1783                 symbol  = ( TUint8 )( iMQDecoder.MqDecodeSymbol( KUniformContext ) << 1 );   // MSB 
       
  1784                 symbol |= ( TUint8 )( iMQDecoder.MqDecodeSymbol( KUniformContext ) & 0x01 ); // LSB 
       
  1785         
       
  1786                 // Update data and state pointers 
       
  1787                 stateValue = stateCol + symbol * iStateWidth;
       
  1788                 dataValue  = dataCol + symbol * iBlockDataWidth;
       
  1789 
       
  1790                 // Set zcCoding flag to zero
       
  1791                 zcCoding = 0;
       
  1792                 row = KStripeHeight - symbol;
       
  1793                 }
       
  1794             else   // All samples zero
       
  1795                 {
       
  1796                 // Move to next column
       
  1797                 continue;      //lint !e960    Continue is OK, no need to go end of for.
       
  1798                 }
       
  1799             }
       
  1800         else  // No RLC used 
       
  1801             {
       
  1802             row = iLastStripeHeight;
       
  1803             stateValue = stateCol;
       
  1804 
       
  1805             // Set zcCoding flag to one 
       
  1806             zcCoding = 1;
       
  1807             }
       
  1808 
       
  1809         for ( ; row > 0; row-- )
       
  1810             {
       
  1811             DecodeNormalization( dataValue, stateValue, mask, zcCoding );
       
  1812             if ( row > 1 )
       
  1813                 {
       
  1814                 dataValue += iBlockDataWidth;
       
  1815                 }
       
  1816             stateValue += iStateWidth;
       
  1817             }
       
  1818         }
       
  1819   
       
  1820     // Decode a segment marker if we need to
       
  1821     if ( aSegSymbols )
       
  1822         {
       
  1823         symbol  = (TUint8)( iMQDecoder.MqDecodeSymbol( KUniformContext ) << 3 );
       
  1824         symbol |= (TUint8)( iMQDecoder.MqDecodeSymbol( KUniformContext ) << 2 );
       
  1825         symbol |= (TUint8)( iMQDecoder.MqDecodeSymbol( KUniformContext ) << 1 );
       
  1826         symbol |= (TUint8)( iMQDecoder.MqDecodeSymbol( KUniformContext ) );
       
  1827 
       
  1828         if ( symbol != KSegmentationMarker )
       
  1829             {
       
  1830             iErrorState = EEntropyCodingError;
       
  1831             }
       
  1832         } 
       
  1833   
       
  1834     // If there is predictable termination, check for errors 
       
  1835     if ( iTerminateThisPass && iPredictableTerminationUsed )
       
  1836         {
       
  1837         iErrorState = iMQDecoder.MqCheckPrediction();
       
  1838         }
       
  1839   
       
  1840     // Reset the MQ context states if we need to
       
  1841     if ( iResetContexts )
       
  1842         {
       
  1843         iMQDecoder.ResetMqContexts();
       
  1844         }
       
  1845     }
       
  1846 
       
  1847 // -----------------------------------------------------------------------------
       
  1848 // CJ2kEntropyDecoder::GetFirstStateRow
       
  1849 // Get the first state row
       
  1850 // (other items were commented in a header).
       
  1851 // -----------------------------------------------------------------------------
       
  1852 //
       
  1853 const TInt16* CJ2kEntropyDecoder::GetFirstStateRow() const
       
  1854     {
       
  1855     return iStates + ( iStateWidth + 1 );
       
  1856     }
       
  1857