1 /* Ppmd7.h -- PPMdH compression codec
2 2018-07-04 : Igor Pavlov : Public domain
3 This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
5 /* This code supports virtual RangeDecoder and includes the implementation
6 of RangeCoder from 7z, instead of RangeCoder from original PPMd var.H.
7 If you need the compatibility with original PPMd var.H, you can use external RangeDecoder */
16 #define PPMD7_MIN_ORDER 2
17 #define PPMD7_MAX_ORDER 64
19 #define PPMD7_MIN_MEM_SIZE (1 << 11)
20 #define PPMD7_MAX_MEM_SIZE (0xFFFFFFFF - 12 * 3)
22 struct CPpmd7_Context_
;
26 struct CPpmd7_Context_
*
32 typedef struct CPpmd7_Context_
36 CPpmd_State_Ref Stats
;
37 CPpmd7_Context_Ref Suffix
;
40 #define Ppmd7Context_OneState(p) ((CPpmd_State *)&(p)->SummFreq)
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 */
51 Byte
*Base
, *LoUnit
, *HiUnit
, *Text
, *UnitsStart
;
54 Byte Indx2Units
[PPMD_NUM_INDEXES
];
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];
62 void Ppmd7_Construct(CPpmd7
*p
);
63 BoolInt
Ppmd7_Alloc(CPpmd7
*p
, UInt32 size
, ISzAllocPtr alloc
);
64 void Ppmd7_Free(CPpmd7
*p
, ISzAllocPtr alloc
);
65 void Ppmd7_Init(CPpmd7
*p
, unsigned maxOrder
);
66 #define Ppmd7_WasAllocated(p) ((p)->Base != NULL)
69 /* ---------- Internal Functions ---------- */
71 extern const Byte PPMD7_kExpEscape
[16];
74 #define Ppmd7_GetPtr(p, ptr) (ptr)
75 #define Ppmd7_GetContext(p, ptr) (ptr)
76 #define Ppmd7_GetStats(p, ctx) ((ctx)->Stats)
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)))
83 void Ppmd7_Update1(CPpmd7
*p
);
84 void Ppmd7_Update1_0(CPpmd7
*p
);
85 void Ppmd7_Update2(CPpmd7
*p
);
86 void Ppmd7_UpdateBin(CPpmd7
*p
);
88 #define Ppmd7_GetBinSumm(p) \
89 &p->BinSumm[(size_t)(unsigned)Ppmd7Context_OneState(p->MinContext)->Freq - 1][p->PrevSuccess + \
90 p->NS2BSIndx[(size_t)Ppmd7_GetContext(p, p->MinContext->Suffix)->NumStats - 1] + \
91 (p->HiBitsFlag = p->HB2Flag[p->FoundState->Symbol]) + \
92 2 * p->HB2Flag[(unsigned)Ppmd7Context_OneState(p->MinContext)->Symbol] + \
93 ((p->RunLength >> 26) & 0x20)]
95 CPpmd_See
*Ppmd7_MakeEscFreq(CPpmd7
*p
, unsigned numMasked
, UInt32
*scale
);
98 /* ---------- Decode ---------- */
100 typedef struct IPpmd7_RangeDec IPpmd7_RangeDec
;
102 struct IPpmd7_RangeDec
104 UInt32 (*GetThreshold
)(const IPpmd7_RangeDec
*p
, UInt32 total
);
105 void (*Decode
)(const IPpmd7_RangeDec
*p
, UInt32 start
, UInt32 size
);
106 UInt32 (*DecodeBit
)(const IPpmd7_RangeDec
*p
, UInt32 size0
);
117 void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec
*p
);
118 BoolInt
Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec
*p
);
119 #define Ppmd7z_RangeDec_IsFinishedOK(p) ((p)->Code == 0)
121 int Ppmd7_DecodeSymbol(CPpmd7
*p
, const IPpmd7_RangeDec
*rc
);
124 /* ---------- Encode ---------- */
135 void Ppmd7z_RangeEnc_Init(CPpmd7z_RangeEnc
*p
);
136 void Ppmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc
*p
);
138 void Ppmd7_EncodeSymbol(CPpmd7
*p
, CPpmd7z_RangeEnc
*rc
, int symbol
);