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.

118 lines
3.5KB

  1. /*
  2. * Copyright (c) 2006 Konstantin Shishkov
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * huffman tree builder and VLC generator
  23. */
  24. #include "avcodec.h"
  25. #include "get_bits.h"
  26. #include "huffman.h"
  27. /* symbol for Huffman tree node */
  28. #define HNODE -1
  29. static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
  30. Node *nodes, int node,
  31. uint32_t pfx, int pl, int *pos, int no_zero_count)
  32. {
  33. int s;
  34. s = nodes[node].sym;
  35. if (s != HNODE || (no_zero_count && !nodes[node].count)) {
  36. bits[*pos] = pfx;
  37. lens[*pos] = pl;
  38. xlat[*pos] = s;
  39. (*pos)++;
  40. } else {
  41. pfx <<= 1;
  42. pl++;
  43. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl,
  44. pos, no_zero_count);
  45. pfx |= 1;
  46. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
  47. pos, no_zero_count);
  48. }
  49. }
  50. static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags)
  51. {
  52. int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
  53. uint32_t bits[256];
  54. int16_t lens[256];
  55. uint8_t xlat[256];
  56. int pos = 0;
  57. get_tree_codes(bits, lens, xlat, nodes, head, 0, 0,
  58. &pos, no_zero_count);
  59. return ff_init_vlc_sparse(vlc, 9, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
  60. }
  61. /**
  62. * nodes size must be 2*nb_codes
  63. * first nb_codes nodes.count must be set
  64. */
  65. int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes,
  66. Node *nodes, HuffCmp cmp, int flags)
  67. {
  68. int i, j;
  69. int cur_node;
  70. int64_t sum = 0;
  71. for (i = 0; i < nb_codes; i++) {
  72. nodes[i].sym = i;
  73. nodes[i].n0 = -2;
  74. sum += nodes[i].count;
  75. }
  76. if (sum >> 31) {
  77. av_log(avctx, AV_LOG_ERROR,
  78. "Too high symbol frequencies. "
  79. "Tree construction is not possible\n");
  80. return -1;
  81. }
  82. qsort(nodes, nb_codes, sizeof(Node), cmp);
  83. cur_node = nb_codes;
  84. nodes[nb_codes*2-1].count = 0;
  85. for (i = 0; i < nb_codes * 2 - 1; i += 2) {
  86. nodes[cur_node].sym = HNODE;
  87. nodes[cur_node].count = nodes[i].count + nodes[i + 1].count;
  88. nodes[cur_node].n0 = i;
  89. for (j = cur_node; j > 0; j--) {
  90. if (nodes[j].count > nodes[j - 1].count ||
  91. (nodes[j].count == nodes[j - 1].count &&
  92. (!(flags & FF_HUFFMAN_FLAG_HNODE_FIRST) ||
  93. nodes[j].n0 == j - 1 || nodes[j].n0 == j - 2 ||
  94. (nodes[j].sym!=HNODE && nodes[j-1].sym!=HNODE))))
  95. break;
  96. FFSWAP(Node, nodes[j], nodes[j - 1]);
  97. }
  98. cur_node++;
  99. }
  100. if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags) < 0) {
  101. av_log(avctx, AV_LOG_ERROR, "Error building tree\n");
  102. return -1;
  103. }
  104. return 0;
  105. }