diff src/win32/7zip/7z/CPP/7zip/Compress/RangeCoderBit.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/RangeCoderBit.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,113 @@
     1.4 +// Compress/RangeCoderBit.h
     1.5 +
     1.6 +#ifndef __COMPRESS_RANGE_CODER_BIT_H
     1.7 +#define __COMPRESS_RANGE_CODER_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 = 4;
    1.18 +
    1.19 +const int kNumBitPriceShiftBits = 4;
    1.20 +const UInt32 kBitPrice = 1 << kNumBitPriceShiftBits;
    1.21 +
    1.22 +extern UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
    1.23 +
    1.24 +template <int numMoveBits>
    1.25 +class CBitModel
    1.26 +{
    1.27 +public:
    1.28 +  UInt32 Prob;
    1.29 +  void UpdateModel(UInt32 symbol)
    1.30 +  {
    1.31 +    /*
    1.32 +    Prob -= (Prob + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits;
    1.33 +    Prob += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits);
    1.34 +    */
    1.35 +    if (symbol == 0)
    1.36 +      Prob += (kBitModelTotal - Prob) >> numMoveBits;
    1.37 +    else
    1.38 +      Prob -= (Prob) >> numMoveBits;
    1.39 +  }
    1.40 +public:
    1.41 +  void Init() { Prob = kBitModelTotal / 2; }
    1.42 +};
    1.43 +
    1.44 +template <int numMoveBits>
    1.45 +class CBitEncoder: public CBitModel<numMoveBits>
    1.46 +{
    1.47 +public:
    1.48 +  void Encode(CEncoder *encoder, UInt32 symbol)
    1.49 +  {
    1.50 +    /*
    1.51 +    encoder->EncodeBit(this->Prob, kNumBitModelTotalBits, symbol);
    1.52 +    this->UpdateModel(symbol);
    1.53 +    */
    1.54 +    UInt32 newBound = (encoder->Range >> kNumBitModelTotalBits) * this->Prob;
    1.55 +    if (symbol == 0)
    1.56 +    {
    1.57 +      encoder->Range = newBound;
    1.58 +      this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
    1.59 +    }
    1.60 +    else
    1.61 +    {
    1.62 +      encoder->Low += newBound;
    1.63 +      encoder->Range -= newBound;
    1.64 +      this->Prob -= (this->Prob) >> numMoveBits;
    1.65 +    }
    1.66 +    if (encoder->Range < kTopValue)
    1.67 +    {
    1.68 +      encoder->Range <<= 8;
    1.69 +      encoder->ShiftLow();
    1.70 +    }
    1.71 +  }
    1.72 +  UInt32 GetPrice(UInt32 symbol) const
    1.73 +  {
    1.74 +    return ProbPrices[(this->Prob ^ ((-(int)symbol)) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
    1.75 +  }
    1.76 +  UInt32 GetPrice0() const { return ProbPrices[this->Prob >> kNumMoveReducingBits]; }
    1.77 +  UInt32 GetPrice1() const { return ProbPrices[(this->Prob ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]; }
    1.78 +};
    1.79 +
    1.80 +
    1.81 +template <int numMoveBits>
    1.82 +class CBitDecoder: public CBitModel<numMoveBits>
    1.83 +{
    1.84 +public:
    1.85 +  UInt32 Decode(CDecoder *decoder)
    1.86 +  {
    1.87 +    UInt32 newBound = (decoder->Range >> kNumBitModelTotalBits) * this->Prob;
    1.88 +    if (decoder->Code < newBound)
    1.89 +    {
    1.90 +      decoder->Range = newBound;
    1.91 +      this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
    1.92 +      if (decoder->Range < kTopValue)
    1.93 +      {
    1.94 +        decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
    1.95 +        decoder->Range <<= 8;
    1.96 +      }
    1.97 +      return 0;
    1.98 +    }
    1.99 +    else
   1.100 +    {
   1.101 +      decoder->Range -= newBound;
   1.102 +      decoder->Code -= newBound;
   1.103 +      this->Prob -= (this->Prob) >> numMoveBits;
   1.104 +      if (decoder->Range < kTopValue)
   1.105 +      {
   1.106 +        decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
   1.107 +        decoder->Range <<= 8;
   1.108 +      }
   1.109 +      return 1;
   1.110 +    }
   1.111 +  }
   1.112 +};
   1.113 +
   1.114 +}}
   1.115 +
   1.116 +#endif