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
4.9KB

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