2009-11-10 16:48:42 +01:00
|
|
|
#ifndef _RAR_PPMMODEL_
|
|
|
|
#define _RAR_PPMMODEL_
|
|
|
|
|
|
|
|
#include "coder.hpp"
|
|
|
|
#include "suballoc.hpp"
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
#ifdef ALLOW_MISALIGNED
|
2009-11-10 16:48:42 +01:00
|
|
|
#pragma pack(1)
|
|
|
|
#endif
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
struct RARPPM_DEF
|
|
|
|
{
|
|
|
|
static const int INT_BITS=7, PERIOD_BITS=7, TOT_BITS=INT_BITS+PERIOD_BITS,
|
|
|
|
INTERVAL=1 << INT_BITS, BIN_SCALE=1 << TOT_BITS, MAX_FREQ=124;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RARPPM_SEE2_CONTEXT : RARPPM_DEF
|
2009-11-10 16:48:42 +01:00
|
|
|
{ // SEE-contexts for PPM-contexts with masked symbols
|
|
|
|
ushort Summ;
|
|
|
|
byte Shift, Count;
|
|
|
|
void init(int InitVal)
|
|
|
|
{
|
|
|
|
Summ=InitVal << (Shift=PERIOD_BITS-4);
|
|
|
|
Count=4;
|
|
|
|
}
|
|
|
|
uint getMean()
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
uint RetVal=GET_SHORT16(Summ) >> Shift;
|
2009-11-10 16:48:42 +01:00
|
|
|
Summ -= RetVal;
|
|
|
|
return RetVal+(RetVal == 0);
|
|
|
|
}
|
|
|
|
void update()
|
|
|
|
{
|
|
|
|
if (Shift < PERIOD_BITS && --Count == 0)
|
|
|
|
{
|
|
|
|
Summ += Summ;
|
|
|
|
Count=3 << Shift++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ModelPPM;
|
2020-07-15 00:40:26 +02:00
|
|
|
struct RARPPM_CONTEXT;
|
2009-11-10 16:48:42 +01:00
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
struct RARPPM_STATE
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
|
|
|
byte Symbol;
|
|
|
|
byte Freq;
|
2020-07-15 00:40:26 +02:00
|
|
|
RARPPM_CONTEXT* Successor;
|
2009-11-10 16:48:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
struct RARPPM_CONTEXT : RARPPM_DEF
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
|
|
|
ushort NumStats;
|
2020-07-15 00:40:26 +02:00
|
|
|
|
|
|
|
struct FreqData
|
|
|
|
{
|
|
|
|
ushort SummFreq;
|
|
|
|
RARPPM_STATE RARPPM_PACK_ATTR * Stats;
|
|
|
|
};
|
|
|
|
|
2009-11-10 16:48:42 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
FreqData U;
|
2020-07-15 00:40:26 +02:00
|
|
|
RARPPM_STATE OneState;
|
2009-11-10 16:48:42 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
RARPPM_CONTEXT* Suffix;
|
2009-11-10 16:48:42 +01:00
|
|
|
inline void encodeBinSymbol(ModelPPM *Model,int symbol); // MaxOrder:
|
|
|
|
inline void encodeSymbol1(ModelPPM *Model,int symbol); // ABCD context
|
|
|
|
inline void encodeSymbol2(ModelPPM *Model,int symbol); // BCD suffix
|
|
|
|
inline void decodeBinSymbol(ModelPPM *Model); // BCDE successor
|
|
|
|
inline bool decodeSymbol1(ModelPPM *Model); // other orders:
|
|
|
|
inline bool decodeSymbol2(ModelPPM *Model); // BCD context
|
2020-07-15 00:40:26 +02:00
|
|
|
inline void update1(ModelPPM *Model,RARPPM_STATE* p); // CD suffix
|
|
|
|
inline void update2(ModelPPM *Model,RARPPM_STATE* p); // BCDE successor
|
2009-11-10 16:48:42 +01:00
|
|
|
void rescale(ModelPPM *Model);
|
2020-07-15 00:40:26 +02:00
|
|
|
inline RARPPM_CONTEXT* createChild(ModelPPM *Model,RARPPM_STATE* pStats,RARPPM_STATE& FirstState);
|
|
|
|
inline RARPPM_SEE2_CONTEXT* makeEscFreq2(ModelPPM *Model,int Diff);
|
2009-11-10 16:48:42 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
#ifdef ALLOW_MISALIGNED
|
2009-11-10 16:48:42 +01:00
|
|
|
#ifdef _AIX
|
|
|
|
#pragma pack(pop)
|
|
|
|
#else
|
|
|
|
#pragma pack()
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
class ModelPPM : RARPPM_DEF
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
|
|
|
private:
|
2020-07-15 00:40:26 +02:00
|
|
|
friend struct RARPPM_CONTEXT;
|
2009-11-10 16:48:42 +01:00
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
RARPPM_SEE2_CONTEXT SEE2Cont[25][16], DummySEE2Cont;
|
2009-11-10 16:48:42 +01:00
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
struct RARPPM_CONTEXT *MinContext, *MedContext, *MaxContext;
|
|
|
|
RARPPM_STATE* FoundState; // found next state transition
|
2009-11-10 16:48:42 +01:00
|
|
|
int NumMasked, InitEsc, OrderFall, MaxOrder, RunLength, InitRL;
|
|
|
|
byte CharMask[256], NS2Indx[256], NS2BSIndx[256], HB2Flag[256];
|
|
|
|
byte EscCount, PrevSuccess, HiBitsFlag;
|
|
|
|
ushort BinSumm[128][64]; // binary SEE-contexts
|
|
|
|
|
|
|
|
RangeCoder Coder;
|
|
|
|
SubAllocator SubAlloc;
|
|
|
|
|
|
|
|
void RestartModelRare();
|
|
|
|
void StartModelRare(int MaxOrder);
|
2020-07-15 00:40:26 +02:00
|
|
|
inline RARPPM_CONTEXT* CreateSuccessors(bool Skip,RARPPM_STATE* p1);
|
2009-11-10 16:48:42 +01:00
|
|
|
|
|
|
|
inline void UpdateModel();
|
|
|
|
inline void ClearMask();
|
|
|
|
public:
|
|
|
|
ModelPPM();
|
|
|
|
void CleanUp(); // reset PPM variables after data error
|
|
|
|
bool DecodeInit(Unpack *UnpackRead,int &EscChar);
|
|
|
|
int DecodeChar();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|