other-licenses/7zstub/src/7zip/Compress/RangeCoder/RangeCoderBit.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/RangeCoderBit.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +// Compress/RangeCoder/RangeCoderBit.h
     1.5 +
     1.6 +#ifndef __COMPRESS_RANGECODER_BIT_H
     1.7 +#define __COMPRESS_RANGECODER_BIT_H
     1.8 +
     1.9 +#include "RangeCoder.h"
    1.10 +
    1.11 +namespace NCompress {
    1.12 +namespace NRangeCoder {
    1.13 +
    1.14 +const int kNumBitModelTotalBits  = 11;
    1.15 +const UInt32 kBitModelTotal = (1 << kNumBitModelTotalBits);
    1.16 +
    1.17 +const int kNumMoveReducingBits = 2;
    1.18 +
    1.19 +const int kNumBitPriceShiftBits = 6;
    1.20 +const UInt32 kBitPrice = 1 << kNumBitPriceShiftBits;
    1.21 +
    1.22 +class CPriceTables
    1.23 +{
    1.24 +public:
    1.25 +  static UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
    1.26 +  static void Init();
    1.27 +  CPriceTables();
    1.28 +};
    1.29 +
    1.30 +template <int numMoveBits>
    1.31 +class CBitModel
    1.32 +{
    1.33 +public:
    1.34 +  UInt32 Prob;
    1.35 +  void UpdateModel(UInt32 symbol)
    1.36 +  {
    1.37 +    /*
    1.38 +    Prob -= (Prob + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits;
    1.39 +    Prob += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits);
    1.40 +    */
    1.41 +    if (symbol == 0)
    1.42 +      Prob += (kBitModelTotal - Prob) >> numMoveBits;
    1.43 +    else
    1.44 +      Prob -= (Prob) >> numMoveBits;
    1.45 +  }
    1.46 +public:
    1.47 +  void Init() { Prob = kBitModelTotal / 2; }
    1.48 +};
    1.49 +
    1.50 +template <int numMoveBits>
    1.51 +class CBitEncoder: public CBitModel<numMoveBits>
    1.52 +{
    1.53 +public:
    1.54 +  void Encode(CEncoder *encoder, UInt32 symbol)
    1.55 +  {
    1.56 +    /*
    1.57 +    encoder->EncodeBit(this->Prob, kNumBitModelTotalBits, symbol);
    1.58 +    this->UpdateModel(symbol);
    1.59 +    */
    1.60 +    UInt32 newBound = (encoder->Range >> kNumBitModelTotalBits) * this->Prob;
    1.61 +    if (symbol == 0)
    1.62 +    {
    1.63 +      encoder->Range = newBound;
    1.64 +      this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
    1.65 +    }
    1.66 +    else
    1.67 +    {
    1.68 +      encoder->Low += newBound;
    1.69 +      encoder->Range -= newBound;
    1.70 +      this->Prob -= (this->Prob) >> numMoveBits;
    1.71 +    }
    1.72 +    if (encoder->Range < kTopValue)
    1.73 +    {
    1.74 +      encoder->Range <<= 8;
    1.75 +      encoder->ShiftLow();
    1.76 +    }
    1.77 +  }
    1.78 +  UInt32 GetPrice(UInt32 symbol) const
    1.79 +  {
    1.80 +    return CPriceTables::ProbPrices[
    1.81 +      (((this->Prob - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
    1.82 +  }
    1.83 +  UInt32 GetPrice0() const { return CPriceTables::ProbPrices[this->Prob >> kNumMoveReducingBits]; }
    1.84 +  UInt32 GetPrice1() const { return CPriceTables::ProbPrices[(kBitModelTotal - this->Prob) >> kNumMoveReducingBits]; }
    1.85 +};
    1.86 +
    1.87 +
    1.88 +template <int numMoveBits>
    1.89 +class CBitDecoder: public CBitModel<numMoveBits>
    1.90 +{
    1.91 +public:
    1.92 +  UInt32 Decode(CDecoder *decoder)
    1.93 +  {
    1.94 +    UInt32 newBound = (decoder->Range >> kNumBitModelTotalBits) * this->Prob;
    1.95 +    if (decoder->Code < newBound)
    1.96 +    {
    1.97 +      decoder->Range = newBound;
    1.98 +      this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
    1.99 +      if (decoder->Range < kTopValue)
   1.100 +      {
   1.101 +        decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
   1.102 +        decoder->Range <<= 8;
   1.103 +      }
   1.104 +      return 0;
   1.105 +    }
   1.106 +    else
   1.107 +    {
   1.108 +      decoder->Range -= newBound;
   1.109 +      decoder->Code -= newBound;
   1.110 +      this->Prob -= (this->Prob) >> numMoveBits;
   1.111 +      if (decoder->Range < kTopValue)
   1.112 +      {
   1.113 +        decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
   1.114 +        decoder->Range <<= 8;
   1.115 +      }
   1.116 +      return 1;
   1.117 +    }
   1.118 +  }
   1.119 +};
   1.120 +
   1.121 +}}
   1.122 +
   1.123 +#endif

mercurial