model.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef _RAR_PPMMODEL_
  2. #define _RAR_PPMMODEL_
  3. #include "coder.hpp"
  4. #include "suballoc.hpp"
  5. const int MAX_O=64; /* maximum allowed model order */
  6. const int INT_BITS=7, PERIOD_BITS=7, TOT_BITS=INT_BITS+PERIOD_BITS,
  7. INTERVAL=1 << INT_BITS, BIN_SCALE=1 << TOT_BITS, MAX_FREQ=124;
  8. #ifndef STRICT_ALIGNMENT_REQUIRED
  9. #pragma pack(1)
  10. #endif
  11. struct SEE2_CONTEXT
  12. { // SEE-contexts for PPM-contexts with masked symbols
  13. ushort Summ;
  14. byte Shift, Count;
  15. void init(int InitVal)
  16. {
  17. Summ=InitVal << (Shift=PERIOD_BITS-4);
  18. Count=4;
  19. }
  20. uint getMean()
  21. {
  22. uint RetVal=SHORT16(Summ) >> Shift;
  23. Summ -= RetVal;
  24. return RetVal+(RetVal == 0);
  25. }
  26. void update()
  27. {
  28. if (Shift < PERIOD_BITS && --Count == 0)
  29. {
  30. Summ += Summ;
  31. Count=3 << Shift++;
  32. }
  33. }
  34. };
  35. class ModelPPM;
  36. struct PPM_CONTEXT;
  37. struct STATE
  38. {
  39. byte Symbol;
  40. byte Freq;
  41. PPM_CONTEXT* Successor;
  42. };
  43. struct FreqData
  44. {
  45. ushort SummFreq;
  46. STATE _PACK_ATTR * Stats;
  47. };
  48. struct PPM_CONTEXT
  49. {
  50. ushort NumStats;
  51. union
  52. {
  53. FreqData U;
  54. STATE OneState;
  55. };
  56. PPM_CONTEXT* Suffix;
  57. inline void encodeBinSymbol(ModelPPM *Model,int symbol); // MaxOrder:
  58. inline void encodeSymbol1(ModelPPM *Model,int symbol); // ABCD context
  59. inline void encodeSymbol2(ModelPPM *Model,int symbol); // BCD suffix
  60. inline void decodeBinSymbol(ModelPPM *Model); // BCDE successor
  61. inline bool decodeSymbol1(ModelPPM *Model); // other orders:
  62. inline bool decodeSymbol2(ModelPPM *Model); // BCD context
  63. inline void update1(ModelPPM *Model,STATE* p); // CD suffix
  64. inline void update2(ModelPPM *Model,STATE* p); // BCDE successor
  65. void rescale(ModelPPM *Model);
  66. inline PPM_CONTEXT* createChild(ModelPPM *Model,STATE* pStats,STATE& FirstState);
  67. inline SEE2_CONTEXT* makeEscFreq2(ModelPPM *Model,int Diff);
  68. };
  69. #ifndef STRICT_ALIGNMENT_REQUIRED
  70. #ifdef _AIX
  71. #pragma pack(pop)
  72. #else
  73. #pragma pack()
  74. #endif
  75. #endif
  76. const uint UNIT_SIZE=Max(sizeof(PPM_CONTEXT),sizeof(RAR_MEM_BLK));
  77. const uint FIXED_UNIT_SIZE=12;
  78. /*
  79. inline PPM_CONTEXT::PPM_CONTEXT(STATE* pStats,PPM_CONTEXT* ShorterContext):
  80. NumStats(1), Suffix(ShorterContext) { pStats->Successor=this; }
  81. inline PPM_CONTEXT::PPM_CONTEXT(): NumStats(0) {}
  82. */
  83. template <class T>
  84. inline void _PPMD_SWAP(T& t1,T& t2) { T tmp=t1; t1=t2; t2=tmp; }
  85. class ModelPPM
  86. {
  87. private:
  88. friend struct PPM_CONTEXT;
  89. SEE2_CONTEXT SEE2Cont[25][16], DummySEE2Cont;
  90. struct PPM_CONTEXT *MinContext, *MedContext, *MaxContext;
  91. STATE* FoundState; // found next state transition
  92. int NumMasked, InitEsc, OrderFall, MaxOrder, RunLength, InitRL;
  93. byte CharMask[256], NS2Indx[256], NS2BSIndx[256], HB2Flag[256];
  94. byte EscCount, PrevSuccess, HiBitsFlag;
  95. ushort BinSumm[128][64]; // binary SEE-contexts
  96. RangeCoder Coder;
  97. SubAllocator SubAlloc;
  98. void RestartModelRare();
  99. void StartModelRare(int MaxOrder);
  100. inline PPM_CONTEXT* CreateSuccessors(bool Skip,STATE* p1);
  101. inline void UpdateModel();
  102. inline void ClearMask();
  103. public:
  104. ModelPPM();
  105. void CleanUp(); // reset PPM variables after data error
  106. bool DecodeInit(Unpack *UnpackRead,int &EscChar);
  107. int DecodeChar();
  108. };
  109. #endif