other-licenses/7zstub/src/7zip/Compress/RangeCoder/RangeCoder.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/7zip/Compress/RangeCoder/RangeCoder.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,205 @@
     1.4 +// Compress/RangeCoder/RangeCoder.h
     1.5 +
     1.6 +#ifndef __COMPRESS_RANGECODER_H
     1.7 +#define __COMPRESS_RANGECODER_H
     1.8 +
     1.9 +#include "../../Common/InBuffer.h"
    1.10 +#include "../../Common/OutBuffer.h"
    1.11 +
    1.12 +namespace NCompress {
    1.13 +namespace NRangeCoder {
    1.14 +
    1.15 +const int kNumTopBits = 24;
    1.16 +const UInt32 kTopValue = (1 << kNumTopBits);
    1.17 +
    1.18 +class CEncoder
    1.19 +{
    1.20 +  UInt32 _cacheSize;
    1.21 +  Byte _cache;
    1.22 +public:
    1.23 +  UInt64 Low;
    1.24 +  UInt32 Range;
    1.25 +  COutBuffer Stream;
    1.26 +  bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); }
    1.27 +
    1.28 +  void SetStream(ISequentialOutStream *stream) { Stream.SetStream(stream); }
    1.29 +  void Init()
    1.30 +  {
    1.31 +    Stream.Init();
    1.32 +    Low = 0;
    1.33 +    Range = 0xFFFFFFFF;
    1.34 +    _cacheSize = 1;
    1.35 +    _cache = 0;
    1.36 +  }
    1.37 +
    1.38 +  void FlushData()
    1.39 +  {
    1.40 +    // Low += 1; 
    1.41 +    for(int i = 0; i < 5; i++)
    1.42 +      ShiftLow();
    1.43 +  }
    1.44 +
    1.45 +  HRESULT FlushStream() { return Stream.Flush();  }
    1.46 +
    1.47 +  void ReleaseStream() { Stream.ReleaseStream(); }
    1.48 +
    1.49 +  void Encode(UInt32 start, UInt32 size, UInt32 total)
    1.50 +  {
    1.51 +    Low += start * (Range /= total);
    1.52 +    Range *= size;
    1.53 +    while (Range < kTopValue)
    1.54 +    {
    1.55 +      Range <<= 8;
    1.56 +      ShiftLow();
    1.57 +    }
    1.58 +  }
    1.59 +
    1.60 +  void ShiftLow()
    1.61 +  {
    1.62 +    if ((UInt32)Low < (UInt32)0xFF000000 || (int)(Low >> 32) != 0) 
    1.63 +    {
    1.64 +      Byte temp = _cache;
    1.65 +      do
    1.66 +      {
    1.67 +        Stream.WriteByte((Byte)(temp + (Byte)(Low >> 32)));
    1.68 +        temp = 0xFF;
    1.69 +      }
    1.70 +      while(--_cacheSize != 0);
    1.71 +      _cache = (Byte)((UInt32)Low >> 24);                      
    1.72 +    } 
    1.73 +    _cacheSize++;                               
    1.74 +    Low = (UInt32)Low << 8;                           
    1.75 +  }
    1.76 +  
    1.77 +  void EncodeDirectBits(UInt32 value, int numTotalBits)
    1.78 +  {
    1.79 +    for (int i = numTotalBits - 1; i >= 0; i--)
    1.80 +    {
    1.81 +      Range >>= 1;
    1.82 +      if (((value >> i) & 1) == 1)
    1.83 +        Low += Range;
    1.84 +      if (Range < kTopValue)
    1.85 +      {
    1.86 +        Range <<= 8;
    1.87 +        ShiftLow();
    1.88 +      }
    1.89 +    }
    1.90 +  }
    1.91 +
    1.92 +  void EncodeBit(UInt32 size0, UInt32 numTotalBits, UInt32 symbol)
    1.93 +  {
    1.94 +    UInt32 newBound = (Range >> numTotalBits) * size0;
    1.95 +    if (symbol == 0)
    1.96 +      Range = newBound;
    1.97 +    else
    1.98 +    {
    1.99 +      Low += newBound;
   1.100 +      Range -= newBound;
   1.101 +    }
   1.102 +    while (Range < kTopValue)
   1.103 +    {
   1.104 +      Range <<= 8;
   1.105 +      ShiftLow();
   1.106 +    }
   1.107 +  }
   1.108 +
   1.109 +  UInt64 GetProcessedSize() {  return Stream.GetProcessedSize() + _cacheSize + 4; }
   1.110 +};
   1.111 +
   1.112 +class CDecoder
   1.113 +{
   1.114 +public:
   1.115 +  CInBuffer Stream;
   1.116 +  UInt32 Range;
   1.117 +  UInt32 Code;
   1.118 +  bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); }
   1.119 +
   1.120 +  void Normalize()
   1.121 +  {
   1.122 +    while (Range < kTopValue)
   1.123 +    {
   1.124 +      Code = (Code << 8) | Stream.ReadByte();
   1.125 +      Range <<= 8;
   1.126 +    }
   1.127 +  }
   1.128 +  
   1.129 +  void SetStream(ISequentialInStream *stream) { Stream.SetStream(stream); }
   1.130 +  void Init()
   1.131 +  {
   1.132 +    Stream.Init();
   1.133 +    Code = 0;
   1.134 +    Range = 0xFFFFFFFF;
   1.135 +    for(int i = 0; i < 5; i++)
   1.136 +      Code = (Code << 8) | Stream.ReadByte();
   1.137 +  }
   1.138 +
   1.139 +  void ReleaseStream() { Stream.ReleaseStream(); }
   1.140 +
   1.141 +  UInt32 GetThreshold(UInt32 total)
   1.142 +  {
   1.143 +    return (Code) / ( Range /= total);
   1.144 +  }
   1.145 +
   1.146 +  void Decode(UInt32 start, UInt32 size)
   1.147 +  {
   1.148 +    Code -= start * Range;
   1.149 +    Range *= size;
   1.150 +    Normalize();
   1.151 +  }
   1.152 +
   1.153 +  UInt32 DecodeDirectBits(int numTotalBits)
   1.154 +  {
   1.155 +    UInt32 range = Range;
   1.156 +    UInt32 code = Code;        
   1.157 +    UInt32 result = 0;
   1.158 +    for (int i = numTotalBits; i != 0; i--)
   1.159 +    {
   1.160 +      range >>= 1;
   1.161 +      /*
   1.162 +      result <<= 1;
   1.163 +      if (code >= range)
   1.164 +      {
   1.165 +        code -= range;
   1.166 +        result |= 1;
   1.167 +      }
   1.168 +      */
   1.169 +      UInt32 t = (code - range) >> 31;
   1.170 +      code -= range & (t - 1);
   1.171 +      result = (result << 1) | (1 - t);
   1.172 +
   1.173 +      if (range < kTopValue)
   1.174 +      {
   1.175 +        code = (code << 8) | Stream.ReadByte();
   1.176 +        range <<= 8; 
   1.177 +      }
   1.178 +    }
   1.179 +    Range = range;
   1.180 +    Code = code;
   1.181 +    return result;
   1.182 +  }
   1.183 +
   1.184 +  UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits)
   1.185 +  {
   1.186 +    UInt32 newBound = (Range >> numTotalBits) * size0;
   1.187 +    UInt32 symbol;
   1.188 +    if (Code < newBound)
   1.189 +    {
   1.190 +      symbol = 0;
   1.191 +      Range = newBound;
   1.192 +    }
   1.193 +    else
   1.194 +    {
   1.195 +      symbol = 1;
   1.196 +      Code -= newBound;
   1.197 +      Range -= newBound;
   1.198 +    }
   1.199 +    Normalize();
   1.200 +    return symbol;
   1.201 +  }
   1.202 +
   1.203 +  UInt64 GetProcessedSize() {return Stream.GetProcessedSize(); }
   1.204 +};
   1.205 +
   1.206 +}}
   1.207 +
   1.208 +#endif

mercurial