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.

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