huffman_encode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Copyright 2011 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. // Author: Jyrki Alakuijala (jyrki@google.com)
  11. //
  12. // Entropy encoding (Huffman) for webp lossless.
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "./huffman_encode.h"
  17. #include "../utils/utils.h"
  18. #include "../webp/format_constants.h"
  19. // -----------------------------------------------------------------------------
  20. // Util function to optimize the symbol map for RLE coding
  21. // Heuristics for selecting the stride ranges to collapse.
  22. static int ValuesShouldBeCollapsedToStrideAverage(int a, int b) {
  23. return abs(a - b) < 4;
  24. }
  25. // Change the population counts in a way that the consequent
  26. // Huffman tree compression, especially its RLE-part, give smaller output.
  27. static void OptimizeHuffmanForRle(int length, uint8_t* const good_for_rle,
  28. uint32_t* const counts) {
  29. // 1) Let's make the Huffman code more compatible with rle encoding.
  30. int i;
  31. for (; length >= 0; --length) {
  32. if (length == 0) {
  33. return; // All zeros.
  34. }
  35. if (counts[length - 1] != 0) {
  36. // Now counts[0..length - 1] does not have trailing zeros.
  37. break;
  38. }
  39. }
  40. // 2) Let's mark all population counts that already can be encoded
  41. // with an rle code.
  42. {
  43. // Let's not spoil any of the existing good rle codes.
  44. // Mark any seq of 0's that is longer as 5 as a good_for_rle.
  45. // Mark any seq of non-0's that is longer as 7 as a good_for_rle.
  46. uint32_t symbol = counts[0];
  47. int stride = 0;
  48. for (i = 0; i < length + 1; ++i) {
  49. if (i == length || counts[i] != symbol) {
  50. if ((symbol == 0 && stride >= 5) ||
  51. (symbol != 0 && stride >= 7)) {
  52. int k;
  53. for (k = 0; k < stride; ++k) {
  54. good_for_rle[i - k - 1] = 1;
  55. }
  56. }
  57. stride = 1;
  58. if (i != length) {
  59. symbol = counts[i];
  60. }
  61. } else {
  62. ++stride;
  63. }
  64. }
  65. }
  66. // 3) Let's replace those population counts that lead to more rle codes.
  67. {
  68. uint32_t stride = 0;
  69. uint32_t limit = counts[0];
  70. uint32_t sum = 0;
  71. for (i = 0; i < length + 1; ++i) {
  72. if (i == length || good_for_rle[i] ||
  73. (i != 0 && good_for_rle[i - 1]) ||
  74. !ValuesShouldBeCollapsedToStrideAverage(counts[i], limit)) {
  75. if (stride >= 4 || (stride >= 3 && sum == 0)) {
  76. uint32_t k;
  77. // The stride must end, collapse what we have, if we have enough (4).
  78. uint32_t count = (sum + stride / 2) / stride;
  79. if (count < 1) {
  80. count = 1;
  81. }
  82. if (sum == 0) {
  83. // Don't make an all zeros stride to be upgraded to ones.
  84. count = 0;
  85. }
  86. for (k = 0; k < stride; ++k) {
  87. // We don't want to change value at counts[i],
  88. // that is already belonging to the next stride. Thus - 1.
  89. counts[i - k - 1] = count;
  90. }
  91. }
  92. stride = 0;
  93. sum = 0;
  94. if (i < length - 3) {
  95. // All interesting strides have a count of at least 4,
  96. // at least when non-zeros.
  97. limit = (counts[i] + counts[i + 1] +
  98. counts[i + 2] + counts[i + 3] + 2) / 4;
  99. } else if (i < length) {
  100. limit = counts[i];
  101. } else {
  102. limit = 0;
  103. }
  104. }
  105. ++stride;
  106. if (i != length) {
  107. sum += counts[i];
  108. if (stride >= 4) {
  109. limit = (sum + stride / 2) / stride;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. // A comparer function for two Huffman trees: sorts first by 'total count'
  116. // (more comes first), and then by 'value' (more comes first).
  117. static int CompareHuffmanTrees(const void* ptr1, const void* ptr2) {
  118. const HuffmanTree* const t1 = (const HuffmanTree*)ptr1;
  119. const HuffmanTree* const t2 = (const HuffmanTree*)ptr2;
  120. if (t1->total_count_ > t2->total_count_) {
  121. return -1;
  122. } else if (t1->total_count_ < t2->total_count_) {
  123. return 1;
  124. } else {
  125. assert(t1->value_ != t2->value_);
  126. return (t1->value_ < t2->value_) ? -1 : 1;
  127. }
  128. }
  129. static void SetBitDepths(const HuffmanTree* const tree,
  130. const HuffmanTree* const pool,
  131. uint8_t* const bit_depths, int level) {
  132. if (tree->pool_index_left_ >= 0) {
  133. SetBitDepths(&pool[tree->pool_index_left_], pool, bit_depths, level + 1);
  134. SetBitDepths(&pool[tree->pool_index_right_], pool, bit_depths, level + 1);
  135. } else {
  136. bit_depths[tree->value_] = level;
  137. }
  138. }
  139. // Create an optimal Huffman tree.
  140. //
  141. // (data,length): population counts.
  142. // tree_limit: maximum bit depth (inclusive) of the codes.
  143. // bit_depths[]: how many bits are used for the symbol.
  144. //
  145. // Returns 0 when an error has occurred.
  146. //
  147. // The catch here is that the tree cannot be arbitrarily deep
  148. //
  149. // count_limit is the value that is to be faked as the minimum value
  150. // and this minimum value is raised until the tree matches the
  151. // maximum length requirement.
  152. //
  153. // This algorithm is not of excellent performance for very long data blocks,
  154. // especially when population counts are longer than 2**tree_limit, but
  155. // we are not planning to use this with extremely long blocks.
  156. //
  157. // See http://en.wikipedia.org/wiki/Huffman_coding
  158. static void GenerateOptimalTree(const uint32_t* const histogram,
  159. int histogram_size,
  160. HuffmanTree* tree, int tree_depth_limit,
  161. uint8_t* const bit_depths) {
  162. uint32_t count_min;
  163. HuffmanTree* tree_pool;
  164. int tree_size_orig = 0;
  165. int i;
  166. for (i = 0; i < histogram_size; ++i) {
  167. if (histogram[i] != 0) {
  168. ++tree_size_orig;
  169. }
  170. }
  171. if (tree_size_orig == 0) { // pretty optimal already!
  172. return;
  173. }
  174. tree_pool = tree + tree_size_orig;
  175. // For block sizes with less than 64k symbols we never need to do a
  176. // second iteration of this loop.
  177. // If we actually start running inside this loop a lot, we would perhaps
  178. // be better off with the Katajainen algorithm.
  179. assert(tree_size_orig <= (1 << (tree_depth_limit - 1)));
  180. for (count_min = 1; ; count_min *= 2) {
  181. int tree_size = tree_size_orig;
  182. // We need to pack the Huffman tree in tree_depth_limit bits.
  183. // So, we try by faking histogram entries to be at least 'count_min'.
  184. int idx = 0;
  185. int j;
  186. for (j = 0; j < histogram_size; ++j) {
  187. if (histogram[j] != 0) {
  188. const uint32_t count =
  189. (histogram[j] < count_min) ? count_min : histogram[j];
  190. tree[idx].total_count_ = count;
  191. tree[idx].value_ = j;
  192. tree[idx].pool_index_left_ = -1;
  193. tree[idx].pool_index_right_ = -1;
  194. ++idx;
  195. }
  196. }
  197. // Build the Huffman tree.
  198. qsort(tree, tree_size, sizeof(*tree), CompareHuffmanTrees);
  199. if (tree_size > 1) { // Normal case.
  200. int tree_pool_size = 0;
  201. while (tree_size > 1) { // Finish when we have only one root.
  202. uint32_t count;
  203. tree_pool[tree_pool_size++] = tree[tree_size - 1];
  204. tree_pool[tree_pool_size++] = tree[tree_size - 2];
  205. count = tree_pool[tree_pool_size - 1].total_count_ +
  206. tree_pool[tree_pool_size - 2].total_count_;
  207. tree_size -= 2;
  208. {
  209. // Search for the insertion point.
  210. int k;
  211. for (k = 0; k < tree_size; ++k) {
  212. if (tree[k].total_count_ <= count) {
  213. break;
  214. }
  215. }
  216. memmove(tree + (k + 1), tree + k, (tree_size - k) * sizeof(*tree));
  217. tree[k].total_count_ = count;
  218. tree[k].value_ = -1;
  219. tree[k].pool_index_left_ = tree_pool_size - 1;
  220. tree[k].pool_index_right_ = tree_pool_size - 2;
  221. tree_size = tree_size + 1;
  222. }
  223. }
  224. SetBitDepths(&tree[0], tree_pool, bit_depths, 0);
  225. } else if (tree_size == 1) { // Trivial case: only one element.
  226. bit_depths[tree[0].value_] = 1;
  227. }
  228. {
  229. // Test if this Huffman tree satisfies our 'tree_depth_limit' criteria.
  230. int max_depth = bit_depths[0];
  231. for (j = 1; j < histogram_size; ++j) {
  232. if (max_depth < bit_depths[j]) {
  233. max_depth = bit_depths[j];
  234. }
  235. }
  236. if (max_depth <= tree_depth_limit) {
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. // -----------------------------------------------------------------------------
  243. // Coding of the Huffman tree values
  244. static HuffmanTreeToken* CodeRepeatedValues(int repetitions,
  245. HuffmanTreeToken* tokens,
  246. int value, int prev_value) {
  247. assert(value <= MAX_ALLOWED_CODE_LENGTH);
  248. if (value != prev_value) {
  249. tokens->code = value;
  250. tokens->extra_bits = 0;
  251. ++tokens;
  252. --repetitions;
  253. }
  254. while (repetitions >= 1) {
  255. if (repetitions < 3) {
  256. int i;
  257. for (i = 0; i < repetitions; ++i) {
  258. tokens->code = value;
  259. tokens->extra_bits = 0;
  260. ++tokens;
  261. }
  262. break;
  263. } else if (repetitions < 7) {
  264. tokens->code = 16;
  265. tokens->extra_bits = repetitions - 3;
  266. ++tokens;
  267. break;
  268. } else {
  269. tokens->code = 16;
  270. tokens->extra_bits = 3;
  271. ++tokens;
  272. repetitions -= 6;
  273. }
  274. }
  275. return tokens;
  276. }
  277. static HuffmanTreeToken* CodeRepeatedZeros(int repetitions,
  278. HuffmanTreeToken* tokens) {
  279. while (repetitions >= 1) {
  280. if (repetitions < 3) {
  281. int i;
  282. for (i = 0; i < repetitions; ++i) {
  283. tokens->code = 0; // 0-value
  284. tokens->extra_bits = 0;
  285. ++tokens;
  286. }
  287. break;
  288. } else if (repetitions < 11) {
  289. tokens->code = 17;
  290. tokens->extra_bits = repetitions - 3;
  291. ++tokens;
  292. break;
  293. } else if (repetitions < 139) {
  294. tokens->code = 18;
  295. tokens->extra_bits = repetitions - 11;
  296. ++tokens;
  297. break;
  298. } else {
  299. tokens->code = 18;
  300. tokens->extra_bits = 0x7f; // 138 repeated 0s
  301. ++tokens;
  302. repetitions -= 138;
  303. }
  304. }
  305. return tokens;
  306. }
  307. int VP8LCreateCompressedHuffmanTree(const HuffmanTreeCode* const tree,
  308. HuffmanTreeToken* tokens, int max_tokens) {
  309. HuffmanTreeToken* const starting_token = tokens;
  310. HuffmanTreeToken* const ending_token = tokens + max_tokens;
  311. const int depth_size = tree->num_symbols;
  312. int prev_value = 8; // 8 is the initial value for rle.
  313. int i = 0;
  314. assert(tokens != NULL);
  315. while (i < depth_size) {
  316. const int value = tree->code_lengths[i];
  317. int k = i + 1;
  318. int runs;
  319. while (k < depth_size && tree->code_lengths[k] == value) ++k;
  320. runs = k - i;
  321. if (value == 0) {
  322. tokens = CodeRepeatedZeros(runs, tokens);
  323. } else {
  324. tokens = CodeRepeatedValues(runs, tokens, value, prev_value);
  325. prev_value = value;
  326. }
  327. i += runs;
  328. assert(tokens <= ending_token);
  329. }
  330. (void)ending_token; // suppress 'unused variable' warning
  331. return (int)(tokens - starting_token);
  332. }
  333. // -----------------------------------------------------------------------------
  334. // Pre-reversed 4-bit values.
  335. static const uint8_t kReversedBits[16] = {
  336. 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
  337. 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
  338. };
  339. static uint32_t ReverseBits(int num_bits, uint32_t bits) {
  340. uint32_t retval = 0;
  341. int i = 0;
  342. while (i < num_bits) {
  343. i += 4;
  344. retval |= kReversedBits[bits & 0xf] << (MAX_ALLOWED_CODE_LENGTH + 1 - i);
  345. bits >>= 4;
  346. }
  347. retval >>= (MAX_ALLOWED_CODE_LENGTH + 1 - num_bits);
  348. return retval;
  349. }
  350. // Get the actual bit values for a tree of bit depths.
  351. static void ConvertBitDepthsToSymbols(HuffmanTreeCode* const tree) {
  352. // 0 bit-depth means that the symbol does not exist.
  353. int i;
  354. int len;
  355. uint32_t next_code[MAX_ALLOWED_CODE_LENGTH + 1];
  356. int depth_count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
  357. assert(tree != NULL);
  358. len = tree->num_symbols;
  359. for (i = 0; i < len; ++i) {
  360. const int code_length = tree->code_lengths[i];
  361. assert(code_length <= MAX_ALLOWED_CODE_LENGTH);
  362. ++depth_count[code_length];
  363. }
  364. depth_count[0] = 0; // ignore unused symbol
  365. next_code[0] = 0;
  366. {
  367. uint32_t code = 0;
  368. for (i = 1; i <= MAX_ALLOWED_CODE_LENGTH; ++i) {
  369. code = (code + depth_count[i - 1]) << 1;
  370. next_code[i] = code;
  371. }
  372. }
  373. for (i = 0; i < len; ++i) {
  374. const int code_length = tree->code_lengths[i];
  375. tree->codes[i] = ReverseBits(code_length, next_code[code_length]++);
  376. }
  377. }
  378. // -----------------------------------------------------------------------------
  379. // Main entry point
  380. void VP8LCreateHuffmanTree(uint32_t* const histogram, int tree_depth_limit,
  381. uint8_t* const buf_rle,
  382. HuffmanTree* const huff_tree,
  383. HuffmanTreeCode* const huff_code) {
  384. const int num_symbols = huff_code->num_symbols;
  385. memset(buf_rle, 0, num_symbols * sizeof(*buf_rle));
  386. OptimizeHuffmanForRle(num_symbols, buf_rle, histogram);
  387. GenerateOptimalTree(histogram, num_symbols, huff_tree, tree_depth_limit,
  388. huff_code->code_lengths);
  389. // Create the actual bit codes for the bit lengths.
  390. ConvertBitDepthsToSymbols(huff_code);
  391. }