all repos — mgba @ b2d406a411b31acce5bbf0246af32a80c22ca834

mGBA Game Boy Advance Emulator

src/third-party/lzma/Ppmd7.h (view raw)

  1/* Ppmd7.h -- PPMdH compression codec
  22018-07-04 : 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);
 63BoolInt Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAllocPtr alloc);
 64void Ppmd7_Free(CPpmd7 *p, ISzAllocPtr 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[(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)]
 94
 95CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *scale);
 96
 97
 98/* ---------- Decode ---------- */
 99
100typedef struct IPpmd7_RangeDec IPpmd7_RangeDec;
101
102struct IPpmd7_RangeDec
103{
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);
107};
108
109typedef struct
110{
111  IPpmd7_RangeDec vt;
112  UInt32 Range;
113  UInt32 Code;
114  IByteIn *Stream;
115} CPpmd7z_RangeDec;
116
117void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec *p);
118BoolInt Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec *p);
119#define Ppmd7z_RangeDec_IsFinishedOK(p) ((p)->Code == 0)
120
121int Ppmd7_DecodeSymbol(CPpmd7 *p, const IPpmd7_RangeDec *rc);
122
123
124/* ---------- Encode ---------- */
125
126typedef struct
127{
128  UInt64 Low;
129  UInt32 Range;
130  Byte Cache;
131  UInt64 CacheSize;
132  IByteOut *Stream;
133} CPpmd7z_RangeEnc;
134
135void Ppmd7z_RangeEnc_Init(CPpmd7z_RangeEnc *p);
136void Ppmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc *p);
137
138void Ppmd7_EncodeSymbol(CPpmd7 *p, CPpmd7z_RangeEnc *rc, int symbol);
139
140EXTERN_C_END
141 
142#endif