Ppmd.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Ppmd.h -- PPMD codec common code
  2. 2017-04-03 : Igor Pavlov : Public domain
  3. This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
  4. #ifndef __PPMD_H
  5. #define __PPMD_H
  6. #include "CpuArch.h"
  7. EXTERN_C_BEGIN
  8. #ifdef MY_CPU_32BIT
  9. #define PPMD_32BIT
  10. #endif
  11. #define PPMD_INT_BITS 7
  12. #define PPMD_PERIOD_BITS 7
  13. #define PPMD_BIN_SCALE (1 << (PPMD_INT_BITS + PPMD_PERIOD_BITS))
  14. #define PPMD_GET_MEAN_SPEC(summ, shift, round) (((summ) + (1 << ((shift) - (round)))) >> (shift))
  15. #define PPMD_GET_MEAN(summ) PPMD_GET_MEAN_SPEC((summ), PPMD_PERIOD_BITS, 2)
  16. #define PPMD_UPDATE_PROB_0(prob) ((prob) + (1 << PPMD_INT_BITS) - PPMD_GET_MEAN(prob))
  17. #define PPMD_UPDATE_PROB_1(prob) ((prob) - PPMD_GET_MEAN(prob))
  18. #define PPMD_N1 4
  19. #define PPMD_N2 4
  20. #define PPMD_N3 4
  21. #define PPMD_N4 ((128 + 3 - 1 * PPMD_N1 - 2 * PPMD_N2 - 3 * PPMD_N3) / 4)
  22. #define PPMD_NUM_INDEXES (PPMD_N1 + PPMD_N2 + PPMD_N3 + PPMD_N4)
  23. #pragma pack(push, 1)
  24. /* Most compilers works OK here even without #pragma pack(push, 1), but some GCC compilers need it. */
  25. /* SEE-contexts for PPM-contexts with masked symbols */
  26. typedef struct
  27. {
  28. UInt16 Summ; /* Freq */
  29. Byte Shift; /* Speed of Freq change; low Shift is for fast change */
  30. Byte Count; /* Count to next change of Shift */
  31. } CPpmd_See;
  32. #define Ppmd_See_Update(p) if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \
  33. { (p)->Summ <<= 1; (p)->Count = (Byte)(3 << (p)->Shift++); }
  34. typedef struct
  35. {
  36. Byte Symbol;
  37. Byte Freq;
  38. UInt16 SuccessorLow;
  39. UInt16 SuccessorHigh;
  40. } CPpmd_State;
  41. #pragma pack(pop)
  42. typedef
  43. #ifdef PPMD_32BIT
  44. CPpmd_State *
  45. #else
  46. UInt32
  47. #endif
  48. CPpmd_State_Ref;
  49. typedef
  50. #ifdef PPMD_32BIT
  51. void *
  52. #else
  53. UInt32
  54. #endif
  55. CPpmd_Void_Ref;
  56. typedef
  57. #ifdef PPMD_32BIT
  58. Byte *
  59. #else
  60. UInt32
  61. #endif
  62. CPpmd_Byte_Ref;
  63. #define PPMD_SetAllBitsIn256Bytes(p) \
  64. { size_t z; for (z = 0; z < 256 / sizeof(p[0]); z += 8) { \
  65. p[z+7] = p[z+6] = p[z+5] = p[z+4] = p[z+3] = p[z+2] = p[z+1] = p[z+0] = ~(size_t)0; }}
  66. EXTERN_C_END
  67. #endif