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.

177 lines
5.0KB

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