1
0

model.hpp 3.0 KB

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