You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.1KB

  1. /*
  2. * Copyright (c) 2006 Konstantin Shishkov
  3. * Copyright (c) 2007 Loren Merritt
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * huffman tree builder and VLC generator
  24. */
  25. #include <stdint.h>
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "huffman.h"
  29. /* symbol for Huffman tree node */
  30. #define HNODE -1
  31. typedef struct HeapElem {
  32. uint64_t val;
  33. int name;
  34. } HeapElem;
  35. static void heap_sift(HeapElem *h, int root, int size)
  36. {
  37. while (root * 2 + 1 < size) {
  38. int child = root * 2 + 1;
  39. if (child < size - 1 && h[child].val > h[child+1].val)
  40. child++;
  41. if (h[root].val > h[child].val) {
  42. FFSWAP(HeapElem, h[root], h[child]);
  43. root = child;
  44. } else
  45. break;
  46. }
  47. }
  48. void ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats)
  49. {
  50. HeapElem h[256];
  51. int up[2*256];
  52. int len[2*256];
  53. int offset, i, next;
  54. int size = 256;
  55. for (offset = 1; ; offset <<= 1) {
  56. for (i=0; i < size; i++) {
  57. h[i].name = i;
  58. h[i].val = (stats[i] << 8) + offset;
  59. }
  60. for (i = size / 2 - 1; i >= 0; i--)
  61. heap_sift(h, i, size);
  62. for (next = size; next < size * 2 - 1; next++) {
  63. // merge the two smallest entries, and put it back in the heap
  64. uint64_t min1v = h[0].val;
  65. up[h[0].name] = next;
  66. h[0].val = INT64_MAX;
  67. heap_sift(h, 0, size);
  68. up[h[0].name] = next;
  69. h[0].name = next;
  70. h[0].val += min1v;
  71. heap_sift(h, 0, size);
  72. }
  73. len[2 * size - 2] = 0;
  74. for (i = 2 * size - 3; i >= size; i--)
  75. len[i] = len[up[i]] + 1;
  76. for (i = 0; i < size; i++) {
  77. dst[i] = len[up[i]] + 1;
  78. if (dst[i] >= 32) break;
  79. }
  80. if (i==size) break;
  81. }
  82. }
  83. static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
  84. Node *nodes, int node,
  85. uint32_t pfx, int pl, int *pos, int no_zero_count)
  86. {
  87. int s;
  88. s = nodes[node].sym;
  89. if (s != HNODE || (no_zero_count && !nodes[node].count)) {
  90. bits[*pos] = pfx;
  91. lens[*pos] = pl;
  92. xlat[*pos] = s;
  93. (*pos)++;
  94. } else {
  95. pfx <<= 1;
  96. pl++;
  97. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl,
  98. pos, no_zero_count);
  99. pfx |= 1;
  100. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
  101. pos, no_zero_count);
  102. }
  103. }
  104. static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags, int nb_bits)
  105. {
  106. int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
  107. uint32_t bits[256];
  108. int16_t lens[256];
  109. uint8_t xlat[256];
  110. int pos = 0;
  111. get_tree_codes(bits, lens, xlat, nodes, head, 0, 0,
  112. &pos, no_zero_count);
  113. return ff_init_vlc_sparse(vlc, nb_bits, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
  114. }
  115. /**
  116. * nodes size must be 2*nb_codes
  117. * first nb_codes nodes.count must be set
  118. */
  119. int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, int nb_bits,
  120. Node *nodes, HuffCmp cmp, int flags)
  121. {
  122. int i, j;
  123. int cur_node;
  124. int64_t sum = 0;
  125. for (i = 0; i < nb_codes; i++) {
  126. nodes[i].sym = i;
  127. nodes[i].n0 = -2;
  128. sum += nodes[i].count;
  129. }
  130. if (sum >> 31) {
  131. av_log(avctx, AV_LOG_ERROR,
  132. "Too high symbol frequencies. "
  133. "Tree construction is not possible\n");
  134. return -1;
  135. }
  136. qsort(nodes, nb_codes, sizeof(Node), cmp);
  137. cur_node = nb_codes;
  138. nodes[nb_codes*2-1].count = 0;
  139. for (i = 0; i < nb_codes * 2 - 1; i += 2) {
  140. nodes[cur_node].sym = HNODE;
  141. nodes[cur_node].count = nodes[i].count + nodes[i + 1].count;
  142. nodes[cur_node].n0 = i;
  143. for (j = cur_node; j > 0; j--) {
  144. if (nodes[j].count > nodes[j - 1].count ||
  145. (nodes[j].count == nodes[j - 1].count &&
  146. (!(flags & FF_HUFFMAN_FLAG_HNODE_FIRST) ||
  147. nodes[j].n0 == j - 1 || nodes[j].n0 == j - 2 ||
  148. (nodes[j].sym!=HNODE && nodes[j-1].sym!=HNODE))))
  149. break;
  150. FFSWAP(Node, nodes[j], nodes[j - 1]);
  151. }
  152. cur_node++;
  153. }
  154. if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags, nb_bits) < 0) {
  155. av_log(avctx, AV_LOG_ERROR, "Error building tree\n");
  156. return -1;
  157. }
  158. return 0;
  159. }