tree_decode.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright (c) 2011, 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // Common tree decoding code.
  17. //
  18. // A recurring feature used by the different LHA algorithms is to
  19. // encode a set of codes, which have varying bit lengths. This is
  20. // implemented using a binary tree, stored inside an array of
  21. // elements.
  22. //
  23. // This file is implemented as a "template" file to be #include-d by
  24. // other files. The typedef for TreeElement must be defined before
  25. // include.
  26. // Upper bit is set in a node value to indicate a leaf.
  27. #define TREE_NODE_LEAF (TreeElement) (1 << (sizeof(TreeElement) * 8 - 1))
  28. // Structure used to hold data needed to build the tree.
  29. typedef struct {
  30. // The tree data and its size (must not be exceeded)
  31. TreeElement *tree;
  32. unsigned int tree_len;
  33. // Counter used to allocate entries from the tree.
  34. // Every time a new node is allocated, this increase by 2.
  35. unsigned int tree_allocated;
  36. // The next tree entry.
  37. // As entries are allocated sequentially, the range from
  38. // next_entry..tree_allocated-1 constitutes the indices into
  39. // the tree that are available to be filled in. By the
  40. // end of the tree build, next_entry should = tree_allocated.
  41. unsigned int next_entry;
  42. } TreeBuildData;
  43. // Initialize all elements of the given tree to a good initial state.
  44. static void init_tree(TreeElement *tree, size_t tree_len)
  45. {
  46. unsigned int i;
  47. for (i = 0; i < tree_len; ++i) {
  48. tree[i] = TREE_NODE_LEAF;
  49. }
  50. }
  51. // Set tree to always decode to a single code.
  52. static void set_tree_single(TreeElement *tree, TreeElement code)
  53. {
  54. tree[0] = (TreeElement) code | TREE_NODE_LEAF;
  55. }
  56. // "Expand" the list of queue entries. This generates a new child
  57. // node at each of the entries currently in the queue, adding the
  58. // children of those nodes into the queue to replace them.
  59. // The effect of this is to add an extra level to the tree, and
  60. // to increase the tree depth of the indices in the queue.
  61. static void expand_queue(TreeBuildData *build)
  62. {
  63. unsigned int end_offset;
  64. unsigned int new_nodes;
  65. // Sanity check that there is enough space in the tree for
  66. // all the new nodes.
  67. new_nodes = (build->tree_allocated - build->next_entry) * 2;
  68. if (build->tree_allocated + new_nodes > build->tree_len) {
  69. return;
  70. }
  71. // Go through all entries currently in the allocated range, and
  72. // allocate a subnode for each.
  73. end_offset = build->tree_allocated;
  74. while (build->next_entry < end_offset) {
  75. build->tree[build->next_entry] = build->tree_allocated;
  76. build->tree_allocated += 2;
  77. ++build->next_entry;
  78. }
  79. }
  80. // Read the next entry from the queue of entries waiting to be used.
  81. static unsigned int read_next_entry(TreeBuildData *build)
  82. {
  83. unsigned int result;
  84. // Sanity check.
  85. if (build->next_entry >= build->tree_allocated) {
  86. return 0;
  87. }
  88. result = build->next_entry;
  89. ++build->next_entry;
  90. return result;
  91. }
  92. // Add all codes to the tree that have the specified length.
  93. // Returns non-zero if there are any entries in code_lengths[] still
  94. // waiting to be added to the tree.
  95. static int add_codes_with_length(TreeBuildData *build,
  96. uint8_t *code_lengths,
  97. unsigned int num_code_lengths,
  98. unsigned int code_len)
  99. {
  100. unsigned int i;
  101. unsigned int node;
  102. int codes_remaining;
  103. codes_remaining = 0;
  104. for (i = 0; i < num_code_lengths; ++i) {
  105. // Does this code belong at this depth in the tree?
  106. if (code_lengths[i] == code_len) {
  107. node = read_next_entry(build);
  108. build->tree[node] = (TreeElement) i | TREE_NODE_LEAF;
  109. }
  110. // More work to be done after this pass?
  111. else if (code_lengths[i] > code_len) {
  112. codes_remaining = 1;
  113. }
  114. }
  115. return codes_remaining;
  116. }
  117. // Build a tree, given the specified array of codes indicating the
  118. // required depth within the tree at which each code should be
  119. // located.
  120. static void build_tree(TreeElement *tree, size_t tree_len,
  121. uint8_t *code_lengths, unsigned int num_code_lengths)
  122. {
  123. TreeBuildData build;
  124. unsigned int code_len;
  125. build.tree = tree;
  126. build.tree_len = tree_len;
  127. // Start with a single entry in the queue - the root node
  128. // pointer.
  129. build.next_entry = 0;
  130. // We always have the root ...
  131. build.tree_allocated = 1;
  132. // Iterate over each possible code length.
  133. // Note: code_len == 0 is deliberately skipped over, as 0
  134. // indicates "not used".
  135. code_len = 0;
  136. do {
  137. // Advance to the next code length by allocating extra
  138. // nodes to the tree - the slots waiting in the queue
  139. // will now be one level deeper in the tree (and the
  140. // codes 1 bit longer).
  141. expand_queue(&build);
  142. ++code_len;
  143. // Add all codes that have this length.
  144. } while (add_codes_with_length(&build, code_lengths,
  145. num_code_lengths, code_len));
  146. }
  147. /*
  148. static void display_tree(TreeElement *tree, unsigned int node, int offset)
  149. {
  150. unsigned int i;
  151. if (node & TREE_NODE_LEAF) {
  152. for (i = 0; i < offset; ++i) putchar(' ');
  153. printf("leaf %i\n", node & ~TREE_NODE_LEAF);
  154. } else {
  155. for (i = 0; i < offset; ++i) putchar(' ');
  156. printf("0 ->\n");
  157. display_tree(tree, tree[node], offset + 4);
  158. for (i = 0; i < offset; ++i) putchar(' ');
  159. printf("1 ->\n");
  160. display_tree(tree, tree[node + 1], offset + 4);
  161. }
  162. }
  163. */
  164. // Read bits from the input stream, traversing the specified tree
  165. // from the root node until we reach a leaf. The leaf value is
  166. // returned.
  167. static int read_from_tree(BitStreamReader *reader, TreeElement *tree)
  168. {
  169. TreeElement code;
  170. int bit;
  171. // Start from root.
  172. code = tree[0];
  173. while ((code & TREE_NODE_LEAF) == 0) {
  174. bit = read_bit(reader);
  175. if (bit < 0) {
  176. return -1;
  177. }
  178. code = tree[code + (unsigned int) bit];
  179. }
  180. // Mask off leaf bit to get the plain code.
  181. return (int) (code & ~TREE_NODE_LEAF);
  182. }