"Fossies" - the Fresh Open Source Software Archive

Member "grzip-0.3.0/MTF_Ari.c" (4 Jan 2007, 18786 Bytes) of package /linux/privat/old/grzip-0.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MTF_Ari.c" see the Fossies "Dox" file reference documentation.

    1 /*-------------------------------------------------*/
    2 /* GRZipII/libGRZip compressor           MTF_Ari.c */
    3 /* RLE + MTF + Entropy Coder Functions             */
    4 /*-------------------------------------------------*/
    5 
    6 /*--
    7   This file is a part of GRZipII and/or libGRZip, a program
    8   and library for lossless, block-sorting data compression.
    9 
   10   Copyright (C) 2002-2003 Grebnov Ilya. All rights reserved.
   11 
   12   This library is free software; you can redistribute it and/or
   13   modify it under the terms of the GNU Lesser General Public
   14   License as published by the Free Software Foundation; either
   15   version 2.1 of the License, or (at your option) any later version.
   16 
   17   This library is distributed in the hope that it will be useful,
   18   but WITHOUT ANY WARRANTY; without even the implied warranty of
   19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   20   Lesser General Public License for more details.
   21 
   22   Grebnov Ilya, Ivanovo, Russian Federation.
   23   grib@crazy.ru
   24 
   25   This program is based on (at least) the work of:
   26   Juergen Abel, Jon L. Bentley, Edgar Binder,
   27   Charles Bloom, Mike Burrows, Andrey Cadach,
   28   Damien Debin, Sebastian Deorowicz, Peter Fenwick,
   29   Michael Schindler, Robert Sedgewick, Julian Seward,
   30   David Wheeler, Vadim Yoockin.
   31 
   32   For more information on these sources, see the manual.
   33 --*/
   34 
   35 #include <stdlib.h>
   36 #include "libGRZip.h"
   37 #include "WFC_MTF.h"
   38 
   39 #define ARI_MaxByte            256
   40 
   41 #define ARI_RangeTOP           (1<<24)
   42 
   43 #define ARI_OutTgtByte(B)      (*Output++=B)
   44 
   45 #define Model_NumBits          11
   46 #define Model_MaxFreq          (1<<Model_NumBits)
   47 
   48 #define M_Log2RLE_Shift_0      6
   49 #define M_Log2RLE_Shift_1      3
   50 #define M_Log2RLE_Shift_2      6
   51 
   52 #define M_L1_Shift_0           4
   53 #define M_L1_Shift_1           6
   54 
   55 #define M_L2_Shift             7
   56 
   57 #define ModelL0_0MaxFreq       58
   58 #define ModelL0_1MaxFreq       62
   59 #define ModelL0_2MaxFreq       204
   60 
   61 #define ARI_ShiftLow()                              \
   62 {                                                   \
   63   if ((Low^0xFF000000)>0xFFFFFF)                    \
   64   {                                                 \
   65     ARI_OutTgtByte((uint8)(Cache+(Low>>32)));       \
   66     sint32 c = (sint32)(0xFF+(Low>>32));            \
   67     while (FFNum) ARI_OutTgtByte(c), FFNum--;       \
   68     Cache = (uint32)(Low)>>24;                      \
   69   } else FFNum++;                                   \
   70   Low = (uint32)(Low)<<8;                           \
   71 }
   72 
   73 #define UpdateVCtx_1(P,NumShiftBits)                \
   74 {                                                   \
   75   VCtx[P]-=(VCtx[P]>>NumShiftBits);                 \
   76 }
   77 
   78 #define UpdateVCtx_0(P,NumShiftBits)                \
   79 {                                                   \
   80   VCtx[P]+=((Model_MaxFreq-VCtx[P])>>NumShiftBits); \
   81 }
   82 
   83 #define UpdateUCtx_1(P,NumShiftBits)                \
   84 {                                                   \
   85   UCtx[P]-=(UCtx[P]>>NumShiftBits);                 \
   86 }
   87 
   88 #define UpdateUCtx_0(P,NumShiftBits)                \
   89 {                                                   \
   90   UCtx[P]+=((Model_MaxFreq-UCtx[P])>>NumShiftBits); \
   91 }
   92 
   93 #define Update_Model_L0()                           \
   94 {                                                   \
   95   if (Model_L0_0[4]>ModelL0_0MaxFreq)               \
   96   {                                                 \
   97     sint32 Sum=(Model_L0_0[0]=(Model_L0_0[0]+1)>>1);\
   98     Sum+=(Model_L0_0[1]=(Model_L0_0[1]+1)>>1);      \
   99     Sum+=(Model_L0_0[2]=(Model_L0_0[2]+1)>>1);      \
  100     Sum+=(Model_L0_0[3]=(Model_L0_0[3]+1)>>1);      \
  101     Model_L0_0[4]=Sum;                              \
  102   }                                                 \
  103   if (VCtx[4]>ModelL0_1MaxFreq)                     \
  104   {                                                 \
  105     sint32 Sum=(VCtx[0]>>=1);                       \
  106     Sum+=(VCtx[1]>>=1);Sum+=(VCtx[2]>>=1);          \
  107     Sum+=(VCtx[3]>>=1);VCtx[4]=Sum;                 \
  108   }                                                 \
  109   if (UCtx[4]>ModelL0_2MaxFreq)                     \
  110   {                                                 \
  111     sint32 Sum=(UCtx[0]>>=1);                       \
  112     Sum+=(UCtx[1]>>=1);Sum+=(UCtx[2]>>=1);          \
  113     Sum+=(UCtx[3]>>=1);UCtx[4]=Sum;                 \
  114   }                                                 \
  115 }                                                   \
  116 
  117 #define Init_Models()                               \
  118   uint32  MTF_List[ARI_MaxByte];                    \
  119                                                     \
  120   uint32  Model_L0_0[5];                            \
  121   uint32  Model_L0_1[ARI_MaxByte][5];               \
  122   uint32  Model_L0_2[4*ARI_MaxByte][5];             \
  123                                                     \
  124   uint32  Model_L1_0[8];                            \
  125   uint32  Model_L1_1[8][8];                         \
  126                                                     \
  127   uint32  Model_L2_0[8][128];                       \
  128                                                     \
  129   uint32  Model_Log2RLE_0[64][GRZ_Log2MaxBlockSize+1];\
  130   uint32  Model_Log2RLE_2[GRZ_Log2MaxBlockSize+1][GRZ_Log2MaxBlockSize+1];\
  131                                                     \
  132   uint32* Model_Log2RLE_1;                          \
  133                                                     \
  134   Model_Log2RLE_1=(uint32*)malloc(ARI_MaxByte*(GRZ_Log2MaxBlockSize+1)*sizeof(uint32));\
  135   if (Model_Log2RLE_1==NULL) return (GRZ_NOT_ENOUGH_MEMORY);\
  136                                                     \
  137   sint32 i,j,CtxRLE=0,CtxL0=0,CtxL1=0,CtxL2;        \
  138                                                     \
  139   for (i=0;i<ARI_MaxByte;i++) MTF_List[i]=i;        \
  140                                                     \
  141   Model_L0_0[0]=Model_L0_0[1]=1;                    \
  142   Model_L0_0[2]=Model_L0_0[3]=1;                    \
  143   Model_L0_0[4]=4;                                  \
  144                                                     \
  145   Model_L1_0[0]=Model_L1_0[1]=Model_MaxFreq>>1;     \
  146   Model_L1_0[2]=Model_L1_0[3]=Model_MaxFreq>>1;     \
  147   Model_L1_0[4]=Model_L1_0[5]=Model_MaxFreq>>1;     \
  148   Model_L1_0[6]=Model_L1_0[7]=Model_MaxFreq>>1;     \
  149                                                     \
  150   memset(Model_L0_1,0,5*ARI_MaxByte*sizeof(uint32));\
  151   memset(Model_L0_2,0,4*5*ARI_MaxByte*sizeof(uint32));\
  152                                                     \
  153   for (i=0;i<8;i++)                                 \
  154     for (j=0;j<128;j++)                             \
  155       Model_L2_0[i][j]=Model_MaxFreq>>1;            \
  156                                                     \
  157   for (i=0;i<7;i++)                                 \
  158   {                                                 \
  159     Model_L1_1[i][0]=Model_L1_1[i][1]=Model_MaxFreq>>1;\
  160     Model_L1_1[i][2]=Model_L1_1[i][3]=Model_MaxFreq>>1;\
  161     Model_L1_1[i][4]=Model_L1_1[i][5]=Model_MaxFreq>>1;\
  162     Model_L1_1[i][6]=Model_L1_1[i][7]=Model_MaxFreq>>1;\
  163   }                                                 \
  164                                                     \
  165   for (j=0;j<64;j++)                                \
  166     for (i=0;i<=GRZ_Log2MaxBlockSize;i++)           \
  167       Model_Log2RLE_0[j][i]=Model_MaxFreq>>1;       \
  168                                                     \
  169   for (j=0;j<=GRZ_Log2MaxBlockSize;j++)             \
  170     for (i=0;i<=GRZ_Log2MaxBlockSize;i++)           \
  171       Model_Log2RLE_2[j][i]=Model_MaxFreq>>1;       \
  172                                                     \
  173   uint32* VCtx;                                     \
  174   uint32* UCtx;                                     \
  175                                                     \
  176   for (UCtx=Model_Log2RLE_1,i=0;i<ARI_MaxByte;i++)  \
  177     for (j=0;j<=GRZ_Log2MaxBlockSize;j++)           \
  178       *UCtx++=Model_MaxFreq>>1;                     \
  179 
  180 
  181 sint32 GRZip_MTF_Ari_Encode(uint8* Input,sint32 Size,uint8* Output)
  182 {
  183   uint8 * InputEnd=Input+Size;
  184   uint8 * OutputEnd=Output+Size-24;
  185 
  186   sint64 Low=0;
  187   uint32 FFNum=0;
  188   uint32 Cache=0;
  189   uint32 Range=(uint32)(-1);
  190 
  191 #define ARI_Encode(TFreq,TCumFreq,TTotFreq)                \
  192 {                                                          \
  193   Low+=((sint32)TCumFreq)*(Range/=((sint32)TTotFreq));     \
  194   Range*=(sint32)TFreq;                                    \
  195   while(Range<ARI_RangeTOP) {ARI_ShiftLow();Range<<=8;}    \
  196 }                                                          \
  197 
  198 #define ARI_Encode_0(FFreq0)                               \
  199 {                                                          \
  200   ARI_Encode(((sint32)FFreq0),0,Model_MaxFreq)             \
  201 }                                                          \
  202 
  203 #define ARI_Encode_1(FFreq0)                               \
  204 {                                                          \
  205   sint32 FTFreq0=FFreq0;                                   \
  206   ARI_Encode(Model_MaxFreq-FTFreq0,FTFreq0,Model_MaxFreq)  \
  207 }                                                          \
  208 
  209   Init_Models();
  210 
  211   uint32 Mask=0,Log2RunSize,RunSize,WFCMTF_Rank,PredChar,Char=0;
  212 
  213   while (Input<InputEnd)
  214   {
  215 
  216     if (Output>=OutputEnd)
  217     {
  218       free(Model_Log2RLE_1);
  219       return (GRZ_NOT_COMPRESSIBLE);
  220     }
  221 
  222     PredChar=Char; Char=*Input++;
  223 
  224     RunSize=1;
  225     while ((Input<InputEnd)&&(*Input==Char)) RunSize++,Input++;
  226 
  227     WFCMTF_Rank=0; while (MTF_List[WFCMTF_Rank]!=Char) WFCMTF_Rank++;
  228 
  229     if (WFCMTF_Rank)
  230     {
  231       sint32 Tmp=WFCMTF_Rank;
  232       do MTF_List[Tmp]=MTF_List[Tmp-1]; while (--Tmp);
  233       MTF_List[0]=Char;
  234     }
  235 
  236     WFCMTF_Rank=(WFCMTF_Rank-1)&0xFF;
  237 
  238     VCtx=&Model_L0_1[PredChar][0]; UCtx=&Model_L0_2[4*CtxL0+(CtxRLE&3)][0];
  239 
  240     if (WFCMTF_Rank<3)
  241      {
  242        uint32 Tmp,Cum=0;
  243        for (Tmp=0;Tmp<WFCMTF_Rank;Tmp++) Cum+=UCtx[Tmp]+VCtx[Tmp]+Model_L0_0[Tmp];
  244        ARI_Encode(Model_L0_0[WFCMTF_Rank]+UCtx[WFCMTF_Rank]+VCtx[WFCMTF_Rank],Cum,
  245                   Model_L0_0[4]+UCtx[4]+VCtx[4]);
  246 
  247        Model_L0_0[WFCMTF_Rank]+=2;UCtx[WFCMTF_Rank]+=2;VCtx[WFCMTF_Rank]+=2;
  248        Model_L0_0[4]+=2;UCtx[4]+=2;VCtx[4]+=2;
  249        Update_Model_L0();
  250      }
  251     else
  252      {
  253        uint32 Cum=Model_L0_0[4]+UCtx[4]+VCtx[4]-Model_L0_0[3]-UCtx[3]-VCtx[3];
  254        ARI_Encode(Model_L0_0[3]+UCtx[3]+VCtx[3],Cum,
  255                   Model_L0_0[4]+UCtx[4]+VCtx[4]);
  256 
  257        Model_L0_0[3]+=2;UCtx[3]+=2;VCtx[3]+=2;
  258        Model_L0_0[4]+=2;UCtx[4]+=2;VCtx[4]+=2;
  259        Update_Model_L0();
  260 
  261        sint32 Mask,GrNum,GrPos;
  262 
  263        GrNum=WFCMTF_Rank2GrNum[WFCMTF_Rank];
  264        GrPos=WFCMTF_Rank2GrPos[WFCMTF_Rank]; Mask=WFCMTF_Rank2Mask[WFCMTF_Rank];
  265 
  266        VCtx=Model_L1_0; UCtx=&Model_L1_1[CtxL1][0]; CtxL1=GrNum;
  267 
  268        for (i=0;i<GrNum;i++)
  269        {
  270          ARI_Encode_1((VCtx[0]+UCtx[0])>>1);
  271          UpdateVCtx_1(0,M_L1_Shift_0); UpdateUCtx_1(0,M_L1_Shift_1);
  272          VCtx++;UCtx++;
  273        }
  274 
  275        if (GrNum!=6)
  276        {
  277          ARI_Encode_0((VCtx[0]+UCtx[0])>>1);
  278          UpdateVCtx_0(0,M_L1_Shift_0); UpdateUCtx_0(0,M_L1_Shift_1);
  279        }
  280 
  281        VCtx=&Model_L2_0[GrNum][0];
  282 
  283        for (CtxL2=1,i=0;i<=GrNum;i++,Mask>>=1)
  284          if (GrPos&Mask)
  285           {
  286             ARI_Encode_1(VCtx[CtxL2]);
  287             UpdateVCtx_1(CtxL2,M_L2_Shift);
  288             CtxL2=(CtxL2<<1)|1;
  289           }
  290          else
  291           {
  292             ARI_Encode_0(VCtx[CtxL2]);
  293             UpdateVCtx_0(CtxL2,M_L2_Shift);
  294             CtxL2<<=1;
  295           }
  296      }
  297 
  298     if (WFCMTF_Rank>3) WFCMTF_Rank=3;
  299     CtxL0=((CtxL0<<2)|WFCMTF_Rank)&0xFF;
  300 
  301     sint32 Tmp=RunSize;
  302 
  303     if (Tmp==1) Log2RunSize=0;
  304     else
  305      {
  306        Log2RunSize=1;Mask=1;
  307        while (1)
  308        {
  309          if ((Tmp>>=1)==1) break;
  310          Log2RunSize++;Mask<<=1;
  311        }
  312      }
  313 
  314     VCtx=&Model_Log2RLE_0[CtxRLE+16*WFCMTF_Rank][0];
  315     UCtx=Model_Log2RLE_1+Char*(GRZ_Log2MaxBlockSize+1);
  316 
  317     for (i=0;i<Log2RunSize;i++)
  318     {
  319       ARI_Encode_1((VCtx[0]+UCtx[0])>>1);
  320       UpdateVCtx_1(0,M_Log2RLE_Shift_0); UpdateUCtx_1(0,M_Log2RLE_Shift_1);
  321       VCtx++;UCtx++;
  322     }
  323 
  324     ARI_Encode_0((VCtx[0]+UCtx[0])>>1);
  325     UpdateVCtx_0(0,M_Log2RLE_Shift_0); UpdateUCtx_0(0,M_Log2RLE_Shift_1);
  326 
  327     if (Log2RunSize<2)
  328       CtxRLE=(CtxRLE<<1)&0xF;
  329     else
  330       CtxRLE=((CtxRLE<<1)|1)&0xF;
  331 
  332     VCtx=&Model_Log2RLE_2[Log2RunSize][0];
  333 
  334     for (i=0;i<Log2RunSize;i++,Mask>>=1,VCtx++)
  335       if (RunSize&Mask)
  336       {
  337         ARI_Encode_1(VCtx[0]);
  338         UpdateVCtx_1(0,M_Log2RLE_Shift_2);
  339       }
  340       else
  341       {
  342         ARI_Encode_0(VCtx[0]);
  343         UpdateVCtx_0(0,M_Log2RLE_Shift_2);
  344       }
  345   }
  346 
  347   VCtx=&Model_L0_1[Char][0]; UCtx=&Model_L0_2[4*CtxL0+(CtxRLE&3)][0];
  348   uint32 Cum=Model_L0_0[4]+UCtx[4]+VCtx[4]-Model_L0_0[3]-UCtx[3]-VCtx[3];
  349   ARI_Encode(Model_L0_0[3]+UCtx[3]+VCtx[3],Cum,
  350              Model_L0_0[4]+UCtx[4]+VCtx[4]);
  351 
  352   Model_L0_0[3]+=2;UCtx[3]+=2;VCtx[3]+=2;
  353   Model_L0_0[4]+=2;UCtx[4]+=2;VCtx[4]+=2;
  354   Update_Model_L0();
  355 
  356   VCtx=Model_L1_0; UCtx=&Model_L1_1[CtxL1][0];
  357 
  358   for (i=0;i<6;i++)
  359   {
  360     ARI_Encode_1((VCtx[0]+UCtx[0])>>1);
  361     UpdateVCtx_1(0,M_L1_Shift_0); UpdateUCtx_1(0,M_L1_Shift_1);
  362     VCtx++;UCtx++;
  363   }
  364 
  365   VCtx=&Model_L2_0[6][0];
  366 
  367   for (Mask=64,CtxL2=1,i=0;i<=6;i++,Mask>>=1)
  368   {
  369     ARI_Encode_1(VCtx[CtxL2]);
  370     UpdateVCtx_1(CtxL2,M_L2_Shift);
  371     CtxL2=(CtxL2<<1)|1;
  372   }
  373 
  374   free(Model_Log2RLE_1);
  375   Low+=(Range>>=1); ARI_ShiftLow(); ARI_ShiftLow();
  376   ARI_ShiftLow(); ARI_ShiftLow(); ARI_ShiftLow();
  377   return (Output+Size-OutputEnd-24);
  378 }
  379 
  380 #undef ARI_OutTgtByte
  381 #undef ARI_ShiftLow
  382 #undef ARI_Encode
  383 #undef ARI_Encode_0
  384 #undef ARI_Encode_1
  385 
  386 #define ARI_InTgtByte()      (*Input++)
  387 #define ARI_GetFreq(TotFreq) (ARICode/(Range/=(TotFreq)))
  388 
  389 sint32 GRZip_MTF_Ari_Decode(uint8* Input,uint8* Output)
  390 {
  391   uint8* SOutput=Output;
  392   uint32 ARICode=0;
  393   uint32 Range=(uint32)(-1);
  394 
  395 #define ARI_Decode(TFreq,TCumFreq,TTotFreq)                \
  396 {                                                          \
  397   ARICode-=((uint32)TCumFreq)*Range;                       \
  398   Range*=(uint32)TFreq;                                    \
  399   while(Range<ARI_RangeTOP)                                \
  400     {ARICode=(ARICode<<8)|ARI_InTgtByte();Range<<=8;}      \
  401 }                                                          \
  402 
  403 #define ARI_Decode_0(FFreq0)                               \
  404 {                                                          \
  405   ARI_Decode(((uint32)FFreq0),0,Model_MaxFreq);            \
  406 }                                                          \
  407 
  408 #define ARI_Decode_1(FFreq0)                               \
  409 {                                                          \
  410   uint32 FAFreq=FFreq0;                                    \
  411   ARI_Decode(Model_MaxFreq-FAFreq,FAFreq,Model_MaxFreq);   \
  412 }                                                          \
  413 
  414   ARICode|=ARI_InTgtByte();ARICode<<=8;
  415   ARICode|=ARI_InTgtByte();ARICode<<=8;
  416   ARICode|=ARI_InTgtByte();ARICode<<=8;
  417   ARICode|=ARI_InTgtByte();ARICode<<=8;
  418   ARICode|=ARI_InTgtByte();
  419 
  420   Init_Models();
  421 
  422   uint32 Log2RunSize,RunSize,WFCMTF_Rank,PredChar,Char=0;
  423 
  424   while (1)
  425   {
  426     PredChar=Char;
  427 
  428     VCtx=&Model_L0_1[PredChar][0]; UCtx=&Model_L0_2[4*CtxL0+(CtxRLE&3)][0];
  429     uint32 Cum=0,Frq=ARI_GetFreq(Model_L0_0[4]+UCtx[4]+VCtx[4]);
  430 
  431     WFCMTF_Rank=0;while (Frq>=Cum) {Cum+=(Model_L0_0[WFCMTF_Rank]+UCtx[WFCMTF_Rank]+VCtx[WFCMTF_Rank]);WFCMTF_Rank++;}
  432     WFCMTF_Rank--; Cum-=(Model_L0_0[WFCMTF_Rank]+UCtx[WFCMTF_Rank]+VCtx[WFCMTF_Rank]);
  433 
  434     ARI_Decode(Model_L0_0[WFCMTF_Rank]+UCtx[WFCMTF_Rank]+VCtx[WFCMTF_Rank],Cum,
  435                Model_L0_0[4]+UCtx[4]+VCtx[4]);
  436 
  437     Model_L0_0[WFCMTF_Rank]+=2;UCtx[WFCMTF_Rank]+=2;VCtx[WFCMTF_Rank]+=2;
  438     Model_L0_0[4]+=2;UCtx[4]+=2;VCtx[4]+=2;
  439     Update_Model_L0();
  440 
  441     if (WFCMTF_Rank==3)
  442     {
  443 
  444       VCtx=Model_L1_0; UCtx=&Model_L1_1[CtxL1][0];
  445 
  446       sint32 GrNum,GrPos=0;
  447 
  448       GrNum=0;
  449 
  450       while (GrNum!=6)
  451       {
  452         if (ARI_GetFreq(Model_MaxFreq)<((VCtx[0]+UCtx[0])>>1))
  453          {
  454            ARI_Decode_0((VCtx[0]+UCtx[0])>>1);
  455            UpdateVCtx_0(0,M_L1_Shift_0); UpdateUCtx_0(0,M_L1_Shift_1);
  456            break;
  457          }
  458         ARI_Decode_1((VCtx[0]+UCtx[0])>>1);
  459         UpdateVCtx_1(0,M_L1_Shift_0); UpdateUCtx_1(0,M_L1_Shift_1);
  460         VCtx++;UCtx++;
  461         GrNum++;
  462       }
  463 
  464       CtxL1=GrNum;
  465 
  466       VCtx=&Model_L2_0[GrNum][0];
  467 
  468       for (CtxL2=1,i=0;i<=GrNum;i++)
  469         if (ARI_GetFreq(Model_MaxFreq)<VCtx[CtxL2])
  470          {
  471            ARI_Decode_0(VCtx[CtxL2]);
  472            UpdateVCtx_0(CtxL2,M_L2_Shift);
  473            CtxL2<<=1;GrPos<<=1;
  474          }
  475         else
  476          {
  477            ARI_Decode_1(VCtx[CtxL2]);
  478            UpdateVCtx_1(CtxL2,M_L2_Shift);
  479            CtxL2=(CtxL2<<1)|1;GrPos=(GrPos<<1)|1;
  480          }
  481       if ((GrNum==6)&&(GrPos==127)) break;
  482 
  483       WFCMTF_Rank=WFCMTF_GrNum2GrBegin[GrNum]+GrPos;
  484 
  485     }
  486 
  487     WFCMTF_Rank=(WFCMTF_Rank+1)&0xFF;
  488 
  489     Char=MTF_List[WFCMTF_Rank];
  490     if (WFCMTF_Rank)
  491     {
  492       sint32 Tmp=WFCMTF_Rank;
  493       do MTF_List[Tmp]=MTF_List[Tmp-1]; while (--Tmp);
  494       MTF_List[0]=Char;
  495     }
  496 
  497     WFCMTF_Rank=(WFCMTF_Rank-1)&0xFF;
  498 
  499     if (WFCMTF_Rank>3) WFCMTF_Rank=3;
  500     CtxL0=((CtxL0<<2)|WFCMTF_Rank)&0xFF;
  501 
  502     VCtx=&Model_Log2RLE_0[CtxRLE+16*WFCMTF_Rank][0];
  503     UCtx=Model_Log2RLE_1+Char*(GRZ_Log2MaxBlockSize+1);
  504 
  505     Log2RunSize=0;
  506 
  507     while (1)
  508     {
  509       if (ARI_GetFreq(Model_MaxFreq)<((VCtx[0]+UCtx[0])>>1))
  510        {
  511          ARI_Decode_0((VCtx[0]+UCtx[0])>>1);
  512          UpdateVCtx_0(0,M_Log2RLE_Shift_0); UpdateUCtx_0(0,M_Log2RLE_Shift_1);
  513          break;
  514        }
  515       ARI_Decode_1((VCtx[0]+UCtx[0])>>1);
  516       UpdateVCtx_1(0,M_Log2RLE_Shift_0); UpdateUCtx_1(0,M_Log2RLE_Shift_1);
  517       VCtx++;UCtx++;
  518       Log2RunSize++;
  519     }
  520 
  521     if (Log2RunSize<2)
  522       CtxRLE=(CtxRLE<<1)&0xF;
  523     else
  524       CtxRLE=((CtxRLE<<1)|1)&0xF;
  525 
  526     VCtx=&Model_Log2RLE_2[Log2RunSize][0];
  527 
  528     RunSize=0;
  529 
  530     for (i=0;i<Log2RunSize;i++,VCtx++)
  531       if (ARI_GetFreq(Model_MaxFreq)<VCtx[0])
  532        {
  533          ARI_Decode_0(VCtx[0]);
  534          UpdateVCtx_0(0,M_Log2RLE_Shift_2);
  535          RunSize<<=1;
  536        }
  537       else
  538        {
  539          ARI_Decode_1(VCtx[0]);
  540          UpdateVCtx_1(0,M_Log2RLE_Shift_2);
  541          RunSize=(RunSize<<1)|1;
  542        }
  543     RunSize+=WFCMTF_Log2RLESize[Log2RunSize];
  544 
  545     while (RunSize--) *Output++=Char;
  546 
  547   }
  548   free(Model_Log2RLE_1);
  549   return (Output-SOutput);
  550 }
  551 
  552 #undef ARI_InTgtByte
  553 #undef ARI_GetFreq
  554 #undef ARI_Decode
  555 #undef ARI_Decode_0
  556 #undef ARI_Decode_1
  557 
  558 #undef ARI_MaxByte
  559 #undef ARI_RangeTOP
  560 #undef Model_NumBits
  561 #undef Model_MaxFreq
  562 #undef M_Log2RLE_Shift_0
  563 #undef M_Log2RLE_Shift_1
  564 #undef M_Log2RLE_Shift_2
  565 #undef M_L1_Shift_0
  566 #undef M_L1_Shift_1
  567 #undef M_L2_Shift
  568 #undef ModelL0_0MaxFreq
  569 #undef ModelL0_1MaxFreq
  570 #undef ModelL0_2MaxFreq
  571 #undef UpdateVCtx_0
  572 #undef UpdateVCtx_1
  573 #undef UpdateUCtx_0
  574 #undef UpdateUCtx_1
  575 #undef Update_Model_L0
  576 #undef Init_Models
  577 
  578 /*-------------------------------------------------*/
  579 /* End                                   MTF_Ari.c */
  580 /*-------------------------------------------------*/