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.

106 lines
3.1KB

  1. /**
  2. * @file 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)
  28. {
  29. int s;
  30. s = nodes[node].sym;
  31. if(s != HNODE || !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. pfx |= 1;
  41. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos);
  42. }
  43. }
  44. static int build_huff_tree(VLC *vlc, Node *nodes, int head)
  45. {
  46. uint32_t bits[256];
  47. int16_t lens[256];
  48. uint8_t xlat[256];
  49. int pos = 0;
  50. get_tree_codes(bits, lens, xlat, nodes, head, 0, 0, &pos);
  51. return init_vlc_sparse(vlc, 9, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
  52. }
  53. /**
  54. * nodes size must be 2*nb_codes
  55. * first nb_codes nodes.count must be set
  56. */
  57. int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes,
  58. Node *nodes, huff_cmp_t cmp, int hnode_first)
  59. {
  60. int i, j;
  61. int cur_node;
  62. int64_t sum = 0;
  63. for(i = 0; i < nb_codes; i++){
  64. nodes[i].sym = i;
  65. nodes[i].n0 = -2;
  66. sum += nodes[i].count;
  67. }
  68. if(sum >> 31) {
  69. av_log(avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
  70. return -1;
  71. }
  72. qsort(nodes, nb_codes, sizeof(Node), cmp);
  73. cur_node = nb_codes;
  74. nodes[nb_codes*2-1].count = 0;
  75. for(i = 0; i < nb_codes*2-1; i += 2){
  76. nodes[cur_node].sym = HNODE;
  77. nodes[cur_node].count = nodes[i].count + nodes[i+1].count;
  78. nodes[cur_node].n0 = i;
  79. for(j = cur_node; j > 0; j--){
  80. if(nodes[j].count > nodes[j-1].count ||
  81. (nodes[j].count == nodes[j-1].count &&
  82. (!hnode_first || nodes[j].n0==j-1 || nodes[j].n0==j-2 ||
  83. (nodes[j].sym!=HNODE && nodes[j-1].sym!=HNODE))))
  84. break;
  85. FFSWAP(Node, nodes[j], nodes[j-1]);
  86. }
  87. cur_node++;
  88. }
  89. if(build_huff_tree(vlc, nodes, nb_codes*2-2) < 0){
  90. av_log(avctx, AV_LOG_ERROR, "Error building tree\n");
  91. return -1;
  92. }
  93. return 0;
  94. }