huffman.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Utilities for building and looking up Huffman trees.
  11. //
  12. // Author: Urvang Joshi (urvang@google.com)
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "./huffman.h"
  17. #include "../utils/utils.h"
  18. #include "../webp/format_constants.h"
  19. // Uncomment the following to use look-up table for ReverseBits()
  20. // (might be faster on some platform)
  21. // #define USE_LUT_REVERSE_BITS
  22. // Huffman data read via DecodeImageStream is represented in two (red and green)
  23. // bytes.
  24. #define MAX_HTREE_GROUPS 0x10000
  25. #define NON_EXISTENT_SYMBOL (-1)
  26. static void TreeNodeInit(HuffmanTreeNode* const node) {
  27. node->children_ = -1; // means: 'unassigned so far'
  28. }
  29. static int NodeIsEmpty(const HuffmanTreeNode* const node) {
  30. return (node->children_ < 0);
  31. }
  32. static int IsFull(const HuffmanTree* const tree) {
  33. return (tree->num_nodes_ == tree->max_nodes_);
  34. }
  35. static void AssignChildren(HuffmanTree* const tree,
  36. HuffmanTreeNode* const node) {
  37. HuffmanTreeNode* const children = tree->root_ + tree->num_nodes_;
  38. node->children_ = (int)(children - node);
  39. assert(children - node == (int)(children - node));
  40. tree->num_nodes_ += 2;
  41. TreeNodeInit(children + 0);
  42. TreeNodeInit(children + 1);
  43. }
  44. // A Huffman tree is a full binary tree; and in a full binary tree with L
  45. // leaves, the total number of nodes N = 2 * L - 1.
  46. static int HuffmanTreeMaxNodes(int num_leaves) {
  47. return (2 * num_leaves - 1);
  48. }
  49. static int HuffmanTreeAllocate(HuffmanTree* const tree, int num_nodes) {
  50. assert(tree != NULL);
  51. tree->root_ =
  52. (HuffmanTreeNode*)WebPSafeMalloc(num_nodes, sizeof(*tree->root_));
  53. return (tree->root_ != NULL);
  54. }
  55. static int TreeInit(HuffmanTree* const tree, int num_leaves) {
  56. assert(tree != NULL);
  57. if (num_leaves == 0) return 0;
  58. tree->max_nodes_ = HuffmanTreeMaxNodes(num_leaves);
  59. assert(tree->max_nodes_ < (1 << 16)); // limit for the lut_jump_ table
  60. if (!HuffmanTreeAllocate(tree, tree->max_nodes_)) return 0;
  61. TreeNodeInit(tree->root_); // Initialize root.
  62. tree->num_nodes_ = 1;
  63. memset(tree->lut_bits_, 255, sizeof(tree->lut_bits_));
  64. memset(tree->lut_jump_, 0, sizeof(tree->lut_jump_));
  65. return 1;
  66. }
  67. void VP8LHuffmanTreeFree(HuffmanTree* const tree) {
  68. if (tree != NULL) {
  69. WebPSafeFree(tree->root_);
  70. tree->root_ = NULL;
  71. tree->max_nodes_ = 0;
  72. tree->num_nodes_ = 0;
  73. }
  74. }
  75. HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups) {
  76. HTreeGroup* const htree_groups =
  77. (HTreeGroup*)WebPSafeCalloc(num_htree_groups, sizeof(*htree_groups));
  78. assert(num_htree_groups <= MAX_HTREE_GROUPS);
  79. if (htree_groups == NULL) {
  80. return NULL;
  81. }
  82. return htree_groups;
  83. }
  84. void VP8LHtreeGroupsFree(HTreeGroup* htree_groups, int num_htree_groups) {
  85. if (htree_groups != NULL) {
  86. int i, j;
  87. for (i = 0; i < num_htree_groups; ++i) {
  88. HuffmanTree* const htrees = htree_groups[i].htrees_;
  89. for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
  90. VP8LHuffmanTreeFree(&htrees[j]);
  91. }
  92. }
  93. WebPSafeFree(htree_groups);
  94. }
  95. }
  96. int VP8LHuffmanCodeLengthsToCodes(
  97. const int* const code_lengths, int code_lengths_size,
  98. int* const huff_codes) {
  99. int symbol;
  100. int code_len;
  101. int code_length_hist[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
  102. int curr_code;
  103. int next_codes[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
  104. int max_code_length = 0;
  105. assert(code_lengths != NULL);
  106. assert(code_lengths_size > 0);
  107. assert(huff_codes != NULL);
  108. // Calculate max code length.
  109. for (symbol = 0; symbol < code_lengths_size; ++symbol) {
  110. if (code_lengths[symbol] > max_code_length) {
  111. max_code_length = code_lengths[symbol];
  112. }
  113. }
  114. if (max_code_length > MAX_ALLOWED_CODE_LENGTH) return 0;
  115. // Calculate code length histogram.
  116. for (symbol = 0; symbol < code_lengths_size; ++symbol) {
  117. ++code_length_hist[code_lengths[symbol]];
  118. }
  119. code_length_hist[0] = 0;
  120. // Calculate the initial values of 'next_codes' for each code length.
  121. // next_codes[code_len] denotes the code to be assigned to the next symbol
  122. // of code length 'code_len'.
  123. curr_code = 0;
  124. next_codes[0] = -1; // Unused, as code length = 0 implies code doesn't exist.
  125. for (code_len = 1; code_len <= max_code_length; ++code_len) {
  126. curr_code = (curr_code + code_length_hist[code_len - 1]) << 1;
  127. next_codes[code_len] = curr_code;
  128. }
  129. // Get symbols.
  130. for (symbol = 0; symbol < code_lengths_size; ++symbol) {
  131. if (code_lengths[symbol] > 0) {
  132. huff_codes[symbol] = next_codes[code_lengths[symbol]]++;
  133. } else {
  134. huff_codes[symbol] = NON_EXISTENT_SYMBOL;
  135. }
  136. }
  137. return 1;
  138. }
  139. #ifndef USE_LUT_REVERSE_BITS
  140. static int ReverseBitsShort(int bits, int num_bits) {
  141. int retval = 0;
  142. int i;
  143. assert(num_bits <= 8); // Not a hard requirement, just for coherency.
  144. for (i = 0; i < num_bits; ++i) {
  145. retval <<= 1;
  146. retval |= bits & 1;
  147. bits >>= 1;
  148. }
  149. return retval;
  150. }
  151. #else
  152. static const uint8_t kReversedBits[16] = { // Pre-reversed 4-bit values.
  153. 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
  154. 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
  155. };
  156. static int ReverseBitsShort(int bits, int num_bits) {
  157. const uint8_t v = (kReversedBits[bits & 0xf] << 4) | kReversedBits[bits >> 4];
  158. assert(num_bits <= 8);
  159. return v >> (8 - num_bits);
  160. }
  161. #endif
  162. static int TreeAddSymbol(HuffmanTree* const tree,
  163. int symbol, int code, int code_length) {
  164. int step = HUFF_LUT_BITS;
  165. int base_code;
  166. HuffmanTreeNode* node = tree->root_;
  167. const HuffmanTreeNode* const max_node = tree->root_ + tree->max_nodes_;
  168. assert(symbol == (int16_t)symbol);
  169. if (code_length <= HUFF_LUT_BITS) {
  170. int i;
  171. base_code = ReverseBitsShort(code, code_length);
  172. for (i = 0; i < (1 << (HUFF_LUT_BITS - code_length)); ++i) {
  173. const int idx = base_code | (i << code_length);
  174. tree->lut_symbol_[idx] = (int16_t)symbol;
  175. tree->lut_bits_[idx] = code_length;
  176. }
  177. } else {
  178. base_code = ReverseBitsShort((code >> (code_length - HUFF_LUT_BITS)),
  179. HUFF_LUT_BITS);
  180. }
  181. while (code_length-- > 0) {
  182. if (node >= max_node) {
  183. return 0;
  184. }
  185. if (NodeIsEmpty(node)) {
  186. if (IsFull(tree)) return 0; // error: too many symbols.
  187. AssignChildren(tree, node);
  188. } else if (!HuffmanTreeNodeIsNotLeaf(node)) {
  189. return 0; // leaf is already occupied.
  190. }
  191. node += node->children_ + ((code >> code_length) & 1);
  192. if (--step == 0) {
  193. tree->lut_jump_[base_code] = (int16_t)(node - tree->root_);
  194. }
  195. }
  196. if (NodeIsEmpty(node)) {
  197. node->children_ = 0; // turn newly created node into a leaf.
  198. } else if (HuffmanTreeNodeIsNotLeaf(node)) {
  199. return 0; // trying to assign a symbol to already used code.
  200. }
  201. node->symbol_ = symbol; // Add symbol in this node.
  202. return 1;
  203. }
  204. int VP8LHuffmanTreeBuildImplicit(HuffmanTree* const tree,
  205. const int* const code_lengths,
  206. int* const codes,
  207. int code_lengths_size) {
  208. int symbol;
  209. int num_symbols = 0;
  210. int root_symbol = 0;
  211. assert(tree != NULL);
  212. assert(code_lengths != NULL);
  213. // Find out number of symbols and the root symbol.
  214. for (symbol = 0; symbol < code_lengths_size; ++symbol) {
  215. if (code_lengths[symbol] > 0) {
  216. // Note: code length = 0 indicates non-existent symbol.
  217. ++num_symbols;
  218. root_symbol = symbol;
  219. }
  220. }
  221. // Initialize the tree. Will fail for num_symbols = 0
  222. if (!TreeInit(tree, num_symbols)) return 0;
  223. // Build tree.
  224. if (num_symbols == 1) { // Trivial case.
  225. const int max_symbol = code_lengths_size;
  226. if (root_symbol < 0 || root_symbol >= max_symbol) {
  227. VP8LHuffmanTreeFree(tree);
  228. return 0;
  229. }
  230. return TreeAddSymbol(tree, root_symbol, 0, 0);
  231. } else { // Normal case.
  232. int ok = 0;
  233. memset(codes, 0, code_lengths_size * sizeof(*codes));
  234. if (!VP8LHuffmanCodeLengthsToCodes(code_lengths, code_lengths_size,
  235. codes)) {
  236. goto End;
  237. }
  238. // Add symbols one-by-one.
  239. for (symbol = 0; symbol < code_lengths_size; ++symbol) {
  240. if (code_lengths[symbol] > 0) {
  241. if (!TreeAddSymbol(tree, symbol, codes[symbol],
  242. code_lengths[symbol])) {
  243. goto End;
  244. }
  245. }
  246. }
  247. ok = 1;
  248. End:
  249. ok = ok && IsFull(tree);
  250. if (!ok) VP8LHuffmanTreeFree(tree);
  251. return ok;
  252. }
  253. }
  254. int VP8LHuffmanTreeBuildExplicit(HuffmanTree* const tree,
  255. const int* const code_lengths,
  256. const int* const codes,
  257. const int* const symbols, int max_symbol,
  258. int num_symbols) {
  259. int ok = 0;
  260. int i;
  261. assert(tree != NULL);
  262. assert(code_lengths != NULL);
  263. assert(codes != NULL);
  264. assert(symbols != NULL);
  265. // Initialize the tree. Will fail if num_symbols = 0.
  266. if (!TreeInit(tree, num_symbols)) return 0;
  267. // Add symbols one-by-one.
  268. for (i = 0; i < num_symbols; ++i) {
  269. if (codes[i] != NON_EXISTENT_SYMBOL) {
  270. if (symbols[i] < 0 || symbols[i] >= max_symbol) {
  271. goto End;
  272. }
  273. if (!TreeAddSymbol(tree, symbols[i], codes[i], code_lengths[i])) {
  274. goto End;
  275. }
  276. }
  277. }
  278. ok = 1;
  279. End:
  280. ok = ok && IsFull(tree);
  281. if (!ok) VP8LHuffmanTreeFree(tree);
  282. return ok;
  283. }