huffman.c 3.4 KB

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