FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
huffman.c
Go to the documentation of this file.
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 /**
23  * @file
24  * huffman tree builder and VLC generator
25  */
26 
27 #include <stdint.h>
28 
29 #include "libavutil/qsort.h"
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "huffman.h"
33 
34 /* symbol for Huffman tree node */
35 #define HNODE -1
36 
37 typedef struct HeapElem {
38  uint64_t val;
39  int name;
40 } HeapElem;
41 
42 static void heap_sift(HeapElem *h, int root, int size)
43 {
44  while (root * 2 + 1 < size) {
45  int child = root * 2 + 1;
46  if (child < size - 1 && h[child].val > h[child+1].val)
47  child++;
48  if (h[root].val > h[child].val) {
49  FFSWAP(HeapElem, h[root], h[child]);
50  root = child;
51  } else
52  break;
53  }
54 }
55 
56 int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0)
57 {
58  HeapElem *h = av_malloc_array(sizeof(*h), stats_size);
59  int *up = av_malloc_array(sizeof(*up) * 2, stats_size);
60  uint8_t *len = av_malloc_array(sizeof(*len) * 2, stats_size);
61  uint16_t *map= av_malloc_array(sizeof(*map), stats_size);
62  int offset, i, next;
63  int size = 0;
64  int ret = 0;
65 
66  if (!h || !up || !len || !map) {
67  ret = AVERROR(ENOMEM);
68  goto end;
69  }
70 
71  for (i = 0; i<stats_size; i++) {
72  dst[i] = 255;
73  if (stats[i] || !skip0)
74  map[size++] = i;
75  }
76 
77  for (offset = 1; ; offset <<= 1) {
78  for (i=0; i < size; i++) {
79  h[i].name = i;
80  h[i].val = (stats[map[i]] << 14) + offset;
81  }
82  for (i = size / 2 - 1; i >= 0; i--)
83  heap_sift(h, i, size);
84 
85  for (next = size; next < size * 2 - 1; next++) {
86  // merge the two smallest entries, and put it back in the heap
87  uint64_t min1v = h[0].val;
88  up[h[0].name] = next;
89  h[0].val = INT64_MAX;
90  heap_sift(h, 0, size);
91  up[h[0].name] = next;
92  h[0].name = next;
93  h[0].val += min1v;
94  heap_sift(h, 0, size);
95  }
96 
97  len[2 * size - 2] = 0;
98  for (i = 2 * size - 3; i >= size; i--)
99  len[i] = len[up[i]] + 1;
100  for (i = 0; i < size; i++) {
101  dst[map[i]] = len[up[i]] + 1;
102  if (dst[map[i]] >= 32) break;
103  }
104  if (i==size) break;
105  }
106 end:
107  av_free(h);
108  av_free(up);
109  av_free(len);
110  av_free(map);
111  return ret;
112 }
113 
114 static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
115  Node *nodes, int node,
116  uint32_t pfx, int pl, int *pos, int no_zero_count)
117 {
118  int s;
119 
120  s = nodes[node].sym;
121  if (s != HNODE || (no_zero_count && !nodes[node].count)) {
122  bits[*pos] = pfx;
123  lens[*pos] = pl;
124  xlat[*pos] = s;
125  (*pos)++;
126  } else {
127  pfx <<= 1;
128  pl++;
129  get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl,
130  pos, no_zero_count);
131  pfx |= 1;
132  get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
133  pos, no_zero_count);
134  }
135 }
136 
137 static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags, int nb_bits)
138 {
139  int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
140  uint32_t bits[256];
141  int16_t lens[256];
142  uint8_t xlat[256];
143  int pos = 0;
144 
145  get_tree_codes(bits, lens, xlat, nodes, head, 0, 0,
146  &pos, no_zero_count);
147  return ff_init_vlc_sparse(vlc, nb_bits, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
148 }
149 
150 
151 /**
152  * nodes size must be 2*nb_codes
153  * first nb_codes nodes.count must be set
154  */
155 int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, int nb_bits,
156  Node *nodes, HuffCmp cmp, int flags)
157 {
158  int i, j;
159  int cur_node;
160  int64_t sum = 0;
161 
162  for (i = 0; i < nb_codes; i++) {
163  nodes[i].sym = i;
164  nodes[i].n0 = -2;
165  sum += nodes[i].count;
166  }
167 
168  if (sum >> 31) {
169  av_log(avctx, AV_LOG_ERROR,
170  "Too high symbol frequencies. "
171  "Tree construction is not possible\n");
172  return -1;
173  }
174  AV_QSORT(nodes, nb_codes, Node, cmp);
175  cur_node = nb_codes;
176  nodes[nb_codes*2-1].count = 0;
177  for (i = 0; i < nb_codes * 2 - 1; i += 2) {
178  uint32_t cur_count = nodes[i].count + nodes[i+1].count;
179  // find correct place to insert new node, and
180  // make space for the new node while at it
181  for(j = cur_node; j > i + 2; j--){
182  if(cur_count > nodes[j-1].count ||
183  (cur_count == nodes[j-1].count &&
184  !(flags & FF_HUFFMAN_FLAG_HNODE_FIRST)))
185  break;
186  nodes[j] = nodes[j - 1];
187  }
188  nodes[j].sym = HNODE;
189  nodes[j].count = cur_count;
190  nodes[j].n0 = i;
191  cur_node++;
192  }
193  if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags, nb_bits) < 0) {
194  av_log(avctx, AV_LOG_ERROR, "Error building tree\n");
195  return -1;
196  }
197  return 0;
198 }
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
#define FF_HUFFMAN_FLAG_HNODE_FIRST
Definition: huffman.h:38
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)
Definition: huffman.c:114
int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, int nb_bits, Node *nodes, HuffCmp cmp, int flags)
nodes size must be 2*nb_codes first nb_codes nodes.count must be set
Definition: huffman.c:155
int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:275
int(* HuffCmp)(const void *va, const void *vb)
Definition: huffman.h:42
Definition: huffman.h:32
static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags, int nb_bits)
Definition: huffman.c:137
uint8_t bits
Definition: crc.c:295
uint8_t
AVS_FilterInfo AVS_Value child
Definition: avisynth_c.h:594
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
static void heap_sift(HeapElem *h, int root, int size)
Definition: huffman.c:42
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define HNODE
Definition: huffman.c:35
#define FF_HUFFMAN_FLAG_ZERO_COUNT
Definition: huffman.h:39
#define AVERROR(e)
Definition: error.h:43
uint64_t val
Definition: huffman.c:38
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
Definition: get_bits.h:63
int name
Definition: huffman.c:39
static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags)
compares a block (either a full macroblock or a partition thereof) against a proposed motion-compensa...
Definition: motion_est.c:260
int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0)
Definition: huffman.c:56
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1532
int16_t n0
Definition: huffman.h:34
huffman tree builder and VLC generator
int16_t sym
Definition: huffman.h:33
static int flags
Definition: cpu.c:47
#define av_free(p)
int len
uint32_t count
Definition: huffman.h:35
#define av_malloc_array(a, b)
#define FFSWAP(type, a, b)
Definition: common.h:99
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
Definition: qsort.h:33