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.

198 lines
5.6KB

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