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.

180 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"libavutil/common.h"
  27. #include "avcodec.h"
  28. #include "huffman.h"
  29. #include "vlc.h"
  30. /* symbol for Huffman tree node */
  31. #define HNODE -1
  32. typedef struct HeapElem {
  33. uint64_t val;
  34. int name;
  35. } HeapElem;
  36. static void heap_sift(HeapElem *h, int root, int size)
  37. {
  38. while (root * 2 + 1 < size) {
  39. int child = root * 2 + 1;
  40. if (child < size - 1 && h[child].val > h[child+1].val)
  41. child++;
  42. if (h[root].val > h[child].val) {
  43. FFSWAP(HeapElem, h[root], h[child]);
  44. root = child;
  45. } else
  46. break;
  47. }
  48. }
  49. void ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats)
  50. {
  51. HeapElem h[256];
  52. int up[2*256];
  53. int len[2*256];
  54. int offset, i, next;
  55. int size = 256;
  56. for (offset = 1; ; offset <<= 1) {
  57. for (i=0; i < size; i++) {
  58. h[i].name = i;
  59. h[i].val = (stats[i] << 8) + offset;
  60. }
  61. for (i = size / 2 - 1; i >= 0; i--)
  62. heap_sift(h, i, size);
  63. for (next = size; next < size * 2 - 1; next++) {
  64. // merge the two smallest entries, and put it back in the heap
  65. uint64_t min1v = h[0].val;
  66. up[h[0].name] = next;
  67. h[0].val = INT64_MAX;
  68. heap_sift(h, 0, size);
  69. up[h[0].name] = next;
  70. h[0].name = next;
  71. h[0].val += min1v;
  72. heap_sift(h, 0, size);
  73. }
  74. len[2 * size - 2] = 0;
  75. for (i = 2 * size - 3; i >= size; i--)
  76. len[i] = len[up[i]] + 1;
  77. for (i = 0; i < size; i++) {
  78. dst[i] = len[up[i]] + 1;
  79. if (dst[i] >= 32) break;
  80. }
  81. if (i==size) break;
  82. }
  83. }
  84. static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
  85. Node *nodes, int node,
  86. uint32_t pfx, int pl, int *pos, int no_zero_count)
  87. {
  88. int s;
  89. s = nodes[node].sym;
  90. if (s != HNODE || (no_zero_count && !nodes[node].count)) {
  91. bits[*pos] = pfx;
  92. lens[*pos] = pl;
  93. xlat[*pos] = s;
  94. (*pos)++;
  95. } else {
  96. pfx <<= 1;
  97. pl++;
  98. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl,
  99. pos, no_zero_count);
  100. pfx |= 1;
  101. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
  102. pos, no_zero_count);
  103. }
  104. }
  105. static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags, int nb_bits)
  106. {
  107. int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
  108. uint32_t bits[256];
  109. int16_t lens[256];
  110. uint8_t xlat[256];
  111. int pos = 0;
  112. get_tree_codes(bits, lens, xlat, nodes, head, 0, 0,
  113. &pos, no_zero_count);
  114. return ff_init_vlc_sparse(vlc, nb_bits, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
  115. }
  116. /**
  117. * nodes size must be 2*nb_codes
  118. * first nb_codes nodes.count must be set
  119. */
  120. int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, int nb_bits,
  121. Node *nodes, HuffCmp cmp, int flags)
  122. {
  123. int i, j;
  124. int cur_node;
  125. int64_t sum = 0;
  126. for (i = 0; i < nb_codes; i++) {
  127. nodes[i].sym = i;
  128. nodes[i].n0 = -2;
  129. sum += nodes[i].count;
  130. }
  131. if (sum >> 31) {
  132. av_log(avctx, AV_LOG_ERROR,
  133. "Too high symbol frequencies. "
  134. "Tree construction is not possible\n");
  135. return -1;
  136. }
  137. qsort(nodes, nb_codes, sizeof(Node), cmp);
  138. cur_node = nb_codes;
  139. nodes[nb_codes*2-1].count = 0;
  140. for (i = 0; i < nb_codes * 2 - 1; i += 2) {
  141. nodes[cur_node].sym = HNODE;
  142. nodes[cur_node].count = nodes[i].count + nodes[i + 1].count;
  143. nodes[cur_node].n0 = i;
  144. for (j = cur_node; j > 0; j--) {
  145. if (nodes[j].count > nodes[j - 1].count ||
  146. (nodes[j].count == nodes[j - 1].count &&
  147. (!(flags & FF_HUFFMAN_FLAG_HNODE_FIRST) ||
  148. nodes[j].n0 == j - 1 || nodes[j].n0 == j - 2 ||
  149. (nodes[j].sym!=HNODE && nodes[j-1].sym!=HNODE))))
  150. break;
  151. FFSWAP(Node, nodes[j], nodes[j - 1]);
  152. }
  153. cur_node++;
  154. }
  155. if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags, nb_bits) < 0) {
  156. av_log(avctx, AV_LOG_ERROR, "Error building tree\n");
  157. return -1;
  158. }
  159. return 0;
  160. }