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.

201 lines
5.7KB

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