~ubuntu-branches/ubuntu/precise/p7zip/precise-updates

« back to all changes in this revision

Viewing changes to CPP/7zip/Compress/RangeCoderBit.h

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2009-02-14 20:12:27 UTC
  • mfrom: (1.1.11 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090214201227-go63qxm9ozfdma60
Tags: 4.65~dfsg.1-1
* New upstream release.
* Remove wx2.8 Build-Depends added by mistakes (7zG is not yet
  intended to be built).
* Use dh_clean without -k.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Compress/RangeCoderBit.h
 
2
 
 
3
#ifndef __COMPRESS_RANGE_CODER_BIT_H
 
4
#define __COMPRESS_RANGE_CODER_BIT_H
 
5
 
 
6
#include "RangeCoder.h"
 
7
 
 
8
namespace NCompress {
 
9
namespace NRangeCoder {
 
10
 
 
11
const int kNumBitModelTotalBits  = 11;
 
12
const UInt32 kBitModelTotal = (1 << kNumBitModelTotalBits);
 
13
 
 
14
const int kNumMoveReducingBits = 4;
 
15
 
 
16
const int kNumBitPriceShiftBits = 4;
 
17
const UInt32 kBitPrice = 1 << kNumBitPriceShiftBits;
 
18
 
 
19
extern UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
 
20
 
 
21
template <int numMoveBits>
 
22
class CBitModel
 
23
{
 
24
public:
 
25
  UInt32 Prob;
 
26
  void UpdateModel(UInt32 symbol)
 
27
  {
 
28
    /*
 
29
    Prob -= (Prob + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits;
 
30
    Prob += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits);
 
31
    */
 
32
    if (symbol == 0)
 
33
      Prob += (kBitModelTotal - Prob) >> numMoveBits;
 
34
    else
 
35
      Prob -= (Prob) >> numMoveBits;
 
36
  }
 
37
public:
 
38
  void Init() { Prob = kBitModelTotal / 2; }
 
39
};
 
40
 
 
41
template <int numMoveBits>
 
42
class CBitEncoder: public CBitModel<numMoveBits>
 
43
{
 
44
public:
 
45
  void Encode(CEncoder *encoder, UInt32 symbol)
 
46
  {
 
47
    /*
 
48
    encoder->EncodeBit(this->Prob, kNumBitModelTotalBits, symbol);
 
49
    this->UpdateModel(symbol);
 
50
    */
 
51
    UInt32 newBound = (encoder->Range >> kNumBitModelTotalBits) * this->Prob;
 
52
    if (symbol == 0)
 
53
    {
 
54
      encoder->Range = newBound;
 
55
      this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
 
56
    }
 
57
    else
 
58
    {
 
59
      encoder->Low += newBound;
 
60
      encoder->Range -= newBound;
 
61
      this->Prob -= (this->Prob) >> numMoveBits;
 
62
    }
 
63
    if (encoder->Range < kTopValue)
 
64
    {
 
65
      encoder->Range <<= 8;
 
66
      encoder->ShiftLow();
 
67
    }
 
68
  }
 
69
  UInt32 GetPrice(UInt32 symbol) const
 
70
  {
 
71
    return ProbPrices[(this->Prob ^ ((-(int)symbol)) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
 
72
  }
 
73
  UInt32 GetPrice0() const { return ProbPrices[this->Prob >> kNumMoveReducingBits]; }
 
74
  UInt32 GetPrice1() const { return ProbPrices[(this->Prob ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]; }
 
75
};
 
76
 
 
77
 
 
78
template <int numMoveBits>
 
79
class CBitDecoder: public CBitModel<numMoveBits>
 
80
{
 
81
public:
 
82
  UInt32 Decode(CDecoder *decoder)
 
83
  {
 
84
    UInt32 newBound = (decoder->Range >> kNumBitModelTotalBits) * this->Prob;
 
85
    if (decoder->Code < newBound)
 
86
    {
 
87
      decoder->Range = newBound;
 
88
      this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
 
89
      if (decoder->Range < kTopValue)
 
90
      {
 
91
        decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
 
92
        decoder->Range <<= 8;
 
93
      }
 
94
      return 0;
 
95
    }
 
96
    else
 
97
    {
 
98
      decoder->Range -= newBound;
 
99
      decoder->Code -= newBound;
 
100
      this->Prob -= (this->Prob) >> numMoveBits;
 
101
      if (decoder->Range < kTopValue)
 
102
      {
 
103
        decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
 
104
        decoder->Range <<= 8;
 
105
      }
 
106
      return 1;
 
107
    }
 
108
  }
 
109
};
 
110
 
 
111
}}
 
112
 
 
113
#endif