stats.h
上传用户:ykyjsl
上传日期:2022-01-30
资源大小:145k
文件大小:4k
源码类别:

压缩解压

开发平台:

C/C++

  1. /******************************************************************************
  2. File: stats.h
  3. Authors:  John Carpenelli   (johnfc@ecr.mu.oz.au)
  4.   Wayne Salamonsen  (wbs@mundil.cs.mu.oz.au)
  5. Purpose: Data compression using a word-based model and revised 
  6. arithmetic coding method.
  7. Based on:  A. Moffat, R. Neal, I.H. Witten, "Arithmetic Coding Revisited",
  8. Proc. IEEE Data Compression Conference, Snowbird, Utah, 
  9. March 1995.
  10. Copyright 1995 John Carpinelli and Wayne Salamonsen, All Rights Reserved.
  11. These programs are supplied free of charge for research purposes only,
  12. and may not sold or incorporated into any commercial product.  There is
  13. ABSOLUTELY NO WARRANTY of any sort, nor any undertaking that they are
  14. fit for ANY PURPOSE WHATSOEVER.  Use them at your own risk.  If you do
  15. happen to find a bug, or have modifications to suggest, please report
  16. the same to Alistair Moffat, alistair@cs.mu.oz.au.  The copyright
  17. notice above and this statement of conditions must remain an integral
  18. part of each and every copy made of these files.
  19. ******************************************************************************/
  20. #ifndef STATS_H
  21. #define STATS_H
  22. /* 
  23.    Note:  arith.h needs to have been previously included, as it has the
  24.    freq_value definition.  Theoretically it should have been defined here,
  25.    but it is easier to read and modify when the frequency and code
  26.    value definitions are placed together, as they are interrelated.
  27.  */
  28. /* 
  29.  * macros to add and remove the end '1' bit of a binary number 
  30.  * using two's complement arithmetic
  31.  */
  32. #define BACK(i) ((i) & ((i) - 1))
  33. #define FORW(i) ((i) + ((i) & - (i)))
  34. #define NOT_KNOWN (-1) /* attempt to code unknown symbol */
  35. #define TOO_MANY_SYMBOLS (-1) /* could not install symbol */
  36. #define NO_MEMORY (-2) /* install exceeded memory */
  37. #define STATIC 0 /* context cannot grow- no escape */
  38. #define DYNAMIC 1 /* context may grow- escape needed */
  39. /* memory used per symbol is a determined by noting that the Fenwick
  40.  * structure used to store the symbols in is doubled as it is
  41.  * realloc'ed.  The old structures (assuming not reusable) roughly
  42.  * double the space used.  This doubling also means that almost half the
  43.  * space (the new half) may never be used.  Together, this means, the
  44.  * maximum cost of a "dynamic symbol" to memory requirements may be up
  45.  * to 4 times that of a "static symbol" (the amount of memory it
  46.  * actually takes to store the symbol).
  47.  * Here we define MEM_PER_SYMBOL as this worst case, so that we can always
  48.  * stay within any user specified memory limit.
  49.  */
  50. #define MEM_PER_SYMBOL (4 * sizeof(freq_value))
  51. #define MIN_INCR 1 /* minimum increment value */
  52. /* context structure used to store frequencies */
  53. typedef struct {
  54.     int initial_size; /* original length of context */
  55.     int max_length, length; /* length of tree and current length */
  56.     freq_value nSingletons; /* no. symbols with frequency=1 */
  57.     int type; /* context may be STATIC or DYNAMIC */
  58.     int nSymbols; /* count of installed symbols */
  59.     freq_value total; /* total of all frequencies */
  60.     freq_value *tree; /* Fenwick's binary index tree */
  61.     freq_value incr; /* current increment */
  62.     int  most_freq_symbol;
  63.     freq_value most_freq_count;
  64.     freq_value most_freq_pos;
  65. } context;
  66. /* context structure for binary contexts */
  67. typedef struct {
  68.     freq_value c0; /* number of zeroes */
  69.     freq_value c1; /* number of ones */
  70.     freq_value incr; /* current increment used */
  71. } binary_context;
  72. extern char *stats_desc;
  73. /* function prototypes */
  74. context *create_context(int length, int type);
  75. int install_symbol(context *pTree, int symbol);
  76. int encode(context *pContext, int symbol);
  77. int decode(context *pContext);
  78. void purge_context(context *pContext);
  79. binary_context *create_binary_context(void);
  80. int binary_encode(binary_context *pContext, int bit);
  81. int binary_decode(binary_context *pContext);
  82. #endif