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