1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* Ppmd.h -- PPMD codec common code
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync2010-03-12 : Igor Pavlov : Public domain
3baa3858d3f5d128a5c8466b700098109edcad5f2repo syncThis code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef __PPMD_H
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define __PPMD_H
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "Types.h"
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "CpuArch.h"
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
11baa3858d3f5d128a5c8466b700098109edcad5f2repo syncEXTERN_C_BEGIN
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef MY_CPU_32BIT
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #define PPMD_32BIT
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_INT_BITS 7
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_PERIOD_BITS 7
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_BIN_SCALE (1 << (PPMD_INT_BITS + PPMD_PERIOD_BITS))
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_GET_MEAN_SPEC(summ, shift, round) (((summ) + (1 << ((shift) - (round)))) >> (shift))
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_GET_MEAN(summ) PPMD_GET_MEAN_SPEC((summ), PPMD_PERIOD_BITS, 2)
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_UPDATE_PROB_0(prob) ((prob) + (1 << PPMD_INT_BITS) - PPMD_GET_MEAN(prob))
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_UPDATE_PROB_1(prob) ((prob) - PPMD_GET_MEAN(prob))
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_N1 4
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_N2 4
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_N3 4
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_N4 ((128 + 3 - 1 * PPMD_N1 - 2 * PPMD_N2 - 3 * PPMD_N3) / 4)
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_NUM_INDEXES (PPMD_N1 + PPMD_N2 + PPMD_N3 + PPMD_N4)
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* SEE-contexts for PPM-contexts with masked symbols */
33baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt16 Summ; /* Freq */
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte Shift;  /* Speed of Freq change; low Shift is for fast change */
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte Count;  /* Count to next change of Shift */
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} CPpmd_See;
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define Ppmd_See_Update(p)  if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    { (p)->Summ <<= 1; (p)->Count = (Byte)(3 << (p)->Shift++); }
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
43baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte Symbol;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte Freq;
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt16 SuccessorLow;
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt16 SuccessorHigh;
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} CPpmd_State;
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
51baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef PPMD_32BIT
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    CPpmd_State *
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CPpmd_State_Ref;
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
59baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef PPMD_32BIT
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    void *
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CPpmd_Void_Ref;
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
67baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef PPMD_32BIT
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Byte *
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CPpmd_Byte_Ref;
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PPMD_SetAllBitsIn256Bytes(p) \
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { unsigned i; for (i = 0; i < 256 / sizeof(p[0]); i += 8) { \
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p[i+7] = p[i+6] = p[i+5] = p[i+4] = p[i+3] = p[i+2] = p[i+1] = p[i+0] = ~(size_t)0; }}
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
79baa3858d3f5d128a5c8466b700098109edcad5f2repo syncEXTERN_C_END
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
82