Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PPMDEncode.h « PPMD « Compress « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6a720cacd542665d7c1ebe9353a66da602473c72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// PPMDEncode.h
// This code is based on Dmitry Shkarin's PPMdH code

#ifndef __COMPRESS_PPMD_ENCODE_H
#define __COMPRESS_PPMD_ENCODE_H

#include "PPMDContext.h"

namespace NCompress {
namespace NPPMD {

struct CEncodeInfo: public CInfo
{

  void EncodeBinSymbol(int symbol, NRangeCoder::CEncoder *rangeEncoder)
  {
    PPM_CONTEXT::STATE& rs = MinContext->oneState();                   
    UInt16 &bs = GetBinSumm(rs, GetContextNoCheck(MinContext->Suffix)->NumStats);
    if (rs.Symbol == symbol) 
    {
      FoundState = &rs;
      rs.Freq = (Byte)(rs.Freq + (rs.Freq < 128 ? 1: 0));
      rangeEncoder->EncodeBit(bs, TOT_BITS, 0);
      bs = (UInt16)(bs + INTERVAL - GET_MEAN(bs, PERIOD_BITS, 2));
      PrevSuccess = 1;
      RunLength++;
    } 
    else 
    {
      rangeEncoder->EncodeBit(bs, TOT_BITS, 1);
      bs = (UInt16)(bs - GET_MEAN(bs, PERIOD_BITS, 2));
      InitEsc = ExpEscape[bs >> 10];
      NumMasked = 1;                        
      CharMask[rs.Symbol] = EscCount;
      PrevSuccess = 0;                      
      FoundState = NULL;
    }
  }

  void EncodeSymbol1(int symbol, NRangeCoder::CEncoder *rangeEncoder)
  {
    PPM_CONTEXT::STATE* p = GetStateNoCheck(MinContext->Stats);
    if (p->Symbol == symbol) 
    {
      PrevSuccess = (2 * (p->Freq) > MinContext->SummFreq);
      RunLength += PrevSuccess;
      rangeEncoder->Encode(0, p->Freq, MinContext->SummFreq);
      (FoundState = p)->Freq += 4;          
      MinContext->SummFreq += 4;
      if (p->Freq > MAX_FREQ)             
        rescale();
      return;
    }
    PrevSuccess = 0;
    int LoCnt = p->Freq, i = MinContext->NumStats - 1;
    while ((++p)->Symbol != symbol) 
    {
      LoCnt += p->Freq;
      if (--i == 0) 
      {
        HiBitsFlag = HB2Flag[FoundState->Symbol];
        CharMask[p->Symbol] = EscCount;
        i=(NumMasked = MinContext->NumStats)-1;       
        FoundState = NULL;
        do { CharMask[(--p)->Symbol] = EscCount; } while ( --i );
        rangeEncoder->Encode(LoCnt, MinContext->SummFreq - LoCnt, MinContext->SummFreq);
        return;
      }
    }
    rangeEncoder->Encode(LoCnt, p->Freq, MinContext->SummFreq);
    update1(p);
  }

  void EncodeSymbol2(int symbol, NRangeCoder::CEncoder *rangeEncoder)
  {
    int hiCnt, i = MinContext->NumStats - NumMasked;
    UInt32 scale;
    SEE2_CONTEXT* psee2c = makeEscFreq2(i, scale);
    PPM_CONTEXT::STATE* p = GetStateNoCheck(MinContext->Stats) - 1;                       
    hiCnt = 0;
    do 
    {
      do { p++; } while (CharMask[p->Symbol] == EscCount);
      hiCnt += p->Freq;
      if (p->Symbol == symbol)            
        goto SYMBOL_FOUND;
      CharMask[p->Symbol] = EscCount;
    } 
    while ( --i );
    
    rangeEncoder->Encode(hiCnt, scale, hiCnt + scale);
    scale += hiCnt;
    
    psee2c->Summ = (UInt16)(psee2c->Summ + scale);
    NumMasked = MinContext->NumStats;
    return;
SYMBOL_FOUND:
    
    UInt32 highCount = hiCnt;
    UInt32 lowCount = highCount - p->Freq;
    if ( --i ) 
    {
      PPM_CONTEXT::STATE* p1 = p;
      do 
      {
        do { p1++; } while (CharMask[p1->Symbol] == EscCount);
        hiCnt += p1->Freq;
      } 
      while ( --i );
    }
    // SubRange.scale += hiCnt;
    scale += hiCnt;
    rangeEncoder->Encode(lowCount, highCount - lowCount, scale);
    psee2c->update();                       
    update2(p);
  }

  void EncodeSymbol(int c, NRangeCoder::CEncoder *rangeEncoder)
  {
    if (MinContext->NumStats != 1) 
      EncodeSymbol1(c, rangeEncoder);   
    else 
      EncodeBinSymbol(c, rangeEncoder); 
    while ( !FoundState ) 
    {
      do 
      {
        OrderFall++;                
        MinContext = GetContext(MinContext->Suffix);
        if (MinContext == 0) 
          return; //  S_OK;
      } 
      while (MinContext->NumStats == NumMasked);
      EncodeSymbol2(c, rangeEncoder);   
    }
    NextContext();
  }

};
}}

#endif