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