Ppmd7.h 3.9 KB

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