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.

172 lines
4.8KB

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