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.

114 lines
3.4KB

  1. /*
  2. * Copyright (c) 2006 Konstantin Shishkov
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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, Node *nodes, int node, uint32_t pfx, int pl, int *pos, int no_zero_count)
  30. {
  31. int s;
  32. s = nodes[node].sym;
  33. if(s != HNODE || (no_zero_count && !nodes[node].count)){
  34. bits[*pos] = pfx;
  35. lens[*pos] = pl;
  36. xlat[*pos] = s;
  37. (*pos)++;
  38. }else{
  39. pfx <<= 1;
  40. pl++;
  41. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl, pos,
  42. no_zero_count);
  43. pfx |= 1;
  44. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos,
  45. no_zero_count);
  46. }
  47. }
  48. static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags)
  49. {
  50. int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
  51. uint32_t bits[256];
  52. int16_t lens[256];
  53. uint8_t xlat[256];
  54. int pos = 0;
  55. get_tree_codes(bits, lens, xlat, nodes, head, 0, 0, &pos, no_zero_count);
  56. return ff_init_vlc_sparse(vlc, 9, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
  57. }
  58. /**
  59. * nodes size must be 2*nb_codes
  60. * first nb_codes nodes.count must be set
  61. */
  62. int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes,
  63. Node *nodes, HuffCmp cmp, int flags)
  64. {
  65. int i, j;
  66. int cur_node;
  67. int64_t sum = 0;
  68. for(i = 0; i < nb_codes; i++){
  69. nodes[i].sym = i;
  70. nodes[i].n0 = -2;
  71. sum += nodes[i].count;
  72. }
  73. if(sum >> 31) {
  74. av_log(avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
  75. return -1;
  76. }
  77. qsort(nodes, nb_codes, sizeof(Node), cmp);
  78. cur_node = nb_codes;
  79. nodes[nb_codes*2-1].count = 0;
  80. for(i = 0; i < nb_codes*2-1; i += 2){
  81. uint32_t cur_count = nodes[i].count + nodes[i+1].count;
  82. // find correct place to insert new node, and
  83. // make space for the new node while at it
  84. for(j = cur_node; j > i + 2; j--){
  85. if(cur_count > nodes[j-1].count ||
  86. (cur_count == nodes[j-1].count &&
  87. !(flags & FF_HUFFMAN_FLAG_HNODE_FIRST)))
  88. break;
  89. nodes[j] = nodes[j - 1];
  90. }
  91. nodes[j].sym = HNODE;
  92. nodes[j].count = cur_count;
  93. nodes[j].n0 = i;
  94. cur_node++;
  95. }
  96. if(build_huff_tree(vlc, nodes, nb_codes*2-2, flags) < 0){
  97. av_log(avctx, AV_LOG_ERROR, "Error building tree\n");
  98. return -1;
  99. }
  100. return 0;
  101. }