1/* Ppmd7.h -- PPMdH compression codec
22010-03-12 : Igor Pavlov : Public domain
3This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
4
5/* This code supports virtual RangeDecoder and includes the implementation
6of RangeCoder from 7z, instead of RangeCoder from original PPMd var.H.
7If you need the compatibility with original PPMd var.H, you can use external RangeDecoder */
8
9#ifndef __PPMD7_H
10#define __PPMD7_H
11
12#include "Ppmd.h"
13
14EXTERN_C_BEGIN
15
16#define PPMD7_MIN_ORDER 2
17#define PPMD7_MAX_ORDER 64
18
19#define PPMD7_MIN_MEM_SIZE (1 << 11)
20#define PPMD7_MAX_MEM_SIZE (0xFFFFFFFF - 12 * 3)
21
22struct CPpmd7_Context_;
23
24typedef
25 #ifdef PPMD_32BIT
26 struct CPpmd7_Context_ *
27 #else
28 UInt32
29 #endif
30 CPpmd7_Context_Ref;
31
32typedef struct CPpmd7_Context_
33{
34 UInt16 NumStats;
35 UInt16 SummFreq;
36 CPpmd_State_Ref Stats;
37 CPpmd7_Context_Ref Suffix;
38} CPpmd7_Context;
39
40#define Ppmd7Context_OneState(p) ((CPpmd_State *)&(p)->SummFreq)
41
42typedef struct
43{
44 CPpmd7_Context *MinContext, *MaxContext;
45 CPpmd_State *FoundState;
46 unsigned OrderFall, InitEsc, PrevSuccess, MaxOrder, HiBitsFlag;
47 Int32 RunLength, InitRL; /* must be 32-bit at least */
48
49 UInt32 Size;
50 UInt32 GlueCount;
51 Byte *Base, *LoUnit, *HiUnit, *Text, *UnitsStart;
52 UInt32 AlignOffset;
53
54 Byte Indx2Units[PPMD_NUM_INDEXES];
55 Byte Units2Indx[128];
56 CPpmd_Void_Ref FreeList[PPMD_NUM_INDEXES];
57 Byte NS2Indx[256], NS2BSIndx[256], HB2Flag[256];
58 CPpmd_See DummySee, See[25][16];
59 UInt16 BinSumm[128][64];
60} CPpmd7;
61
62void Ppmd7_Construct(CPpmd7 *p);
63Bool Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAlloc *alloc);
64void Ppmd7_Free(CPpmd7 *p, ISzAlloc *alloc);
65void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder);
66#define Ppmd7_WasAllocated(p) ((p)->Base != NULL)
67
68
69/* ---------- Internal Functions ---------- */
70
71extern const Byte PPMD7_kExpEscape[16];
72
73#ifdef PPMD_32BIT
74 #define Ppmd7_GetPtr(p, ptr) (ptr)
75 #define Ppmd7_GetContext(p, ptr) (ptr)
76 #define Ppmd7_GetStats(p, ctx) ((ctx)->Stats)
77#else
78 #define Ppmd7_GetPtr(p, offs) ((void *)((p)->Base + (offs)))
79 #define Ppmd7_GetContext(p, offs) ((CPpmd7_Context *)Ppmd7_GetPtr((p), (offs)))
80 #define Ppmd7_GetStats(p, ctx) ((CPpmd_State *)Ppmd7_GetPtr((p), ((ctx)->Stats)))
81#endif
82
83void Ppmd7_Update1(CPpmd7 *p);
84void Ppmd7_Update1_0(CPpmd7 *p);
85void Ppmd7_Update2(CPpmd7 *p);
86void Ppmd7_UpdateBin(CPpmd7 *p);
87
88#define Ppmd7_GetBinSumm(p) \
89 &p->BinSumm[Ppmd7Context_OneState(p->MinContext)->Freq - 1][p->PrevSuccess + \
90 p->NS2BSIndx[Ppmd7_GetContext(p, p->MinContext->Suffix)->NumStats - 1] + \
91 (p->HiBitsFlag = p->HB2Flag[p->FoundState->Symbol]) + \
92 2 * p->HB2Flag[Ppmd7Context_OneState(p->MinContext)->Symbol] + \
93 ((p->RunLength >> 26) & 0x20)]
94
95CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *scale);
96
97
98/* ---------- Decode ---------- */
99
100typedef struct
101{
102 UInt32 (*GetThreshold)(void *p, UInt32 total);
103 void (*Decode)(void *p, UInt32 start, UInt32 size);
104 UInt32 (*DecodeBit)(void *p, UInt32 size0);
105} IPpmd7_RangeDec;
106
107typedef struct
108{
109 IPpmd7_RangeDec p;
110 UInt32 Range;
111 UInt32 Code;
112 IByteIn *Stream;
113} CPpmd7z_RangeDec;
114
115void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec *p);
116Bool Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec *p);
117#define Ppmd7z_RangeDec_IsFinishedOK(p) ((p)->Code == 0)
118
119int Ppmd7_DecodeSymbol(CPpmd7 *p, IPpmd7_RangeDec *rc);
120
121
122/* ---------- Encode ---------- */
123
124typedef struct
125{
126 UInt64 Low;
127 UInt32 Range;
128 Byte Cache;
129 UInt64 CacheSize;
130 IByteOut *Stream;
131} CPpmd7z_RangeEnc;
132
133void Ppmd7z_RangeEnc_Init(CPpmd7z_RangeEnc *p);
134void Ppmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc *p);
135
136void Ppmd7_EncodeSymbol(CPpmd7 *p, CPpmd7z_RangeEnc *rc, int symbol);
137
138EXTERN_C_END
139
140#endif
141