Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.6 KiB

  1. /* Ppmd7.h -- PPMdH compression codec
  2. 2010-03-12 : 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. Bool Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAlloc *alloc);
  50. void Ppmd7_Free(CPpmd7 *p, ISzAlloc *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[Ppmd7Context_OneState(p->MinContext)->Freq - 1][p->PrevSuccess + \
  70. p->NS2BSIndx[Ppmd7_GetContext(p, p->MinContext->Suffix)->NumStats - 1] + \
  71. (p->HiBitsFlag = p->HB2Flag[p->FoundState->Symbol]) + \
  72. 2 * p->HB2Flag[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
  77. {
  78. UInt32 (*GetThreshold)(void *p, UInt32 total);
  79. void (*Decode)(void *p, UInt32 start, UInt32 size);
  80. UInt32 (*DecodeBit)(void *p, UInt32 size0);
  81. } IPpmd7_RangeDec;
  82. typedef struct
  83. {
  84. IPpmd7_RangeDec p;
  85. UInt32 Range;
  86. UInt32 Code;
  87. IByteIn *Stream;
  88. } CPpmd7z_RangeDec;
  89. void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec *p);
  90. Bool Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec *p);
  91. #define Ppmd7z_RangeDec_IsFinishedOK(p) ((p)->Code == 0)
  92. int Ppmd7_DecodeSymbol(CPpmd7 *p, IPpmd7_RangeDec *rc);
  93. /* ---------- Encode ---------- */
  94. typedef struct
  95. {
  96. UInt64 Low;
  97. UInt32 Range;
  98. Byte Cache;
  99. UInt64 CacheSize;
  100. IByteOut *Stream;
  101. } CPpmd7z_RangeEnc;
  102. void Ppmd7z_RangeEnc_Init(CPpmd7z_RangeEnc *p);
  103. void Ppmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc *p);
  104. void Ppmd7_EncodeSymbol(CPpmd7 *p, CPpmd7z_RangeEnc *rc, int symbol);
  105. EXTERN_C_END
  106. #endif