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
34
37
38 /* symbol for Huffman tree node */
40
45
47 {
48 while (root * 2 + 1 <
size) {
49 int child = root * 2 + 1;
51 child++;
54 root = child;
55 } else
56 break;
57 }
58 }
59
61 {
69
70 if (!
h || !up || !
len || !
map) {
72 goto end;
73 }
74
75 for (
i = 0;
i<stats_size;
i++) {
79 }
80
85 }
86 for (
i =
size / 2 - 1;
i >= 0;
i--)
88
89 for (next =
size; next <
size * 2 - 1; next++) {
90 // merge the two smallest entries, and put it back in the heap
91 uint64_t min1v =
h[0].val;
99 }
100
106 if (dst[
map[
i]] >= 32)
break;
107 }
109 }
110 end:
116 }
117
119 Node *nodes,
int node,
120 uint32_t pfx,
int pl,
int *
pos,
int no_zero_count)
121 {
123
125 if (
s !=
HNODE || (no_zero_count && !nodes[node].count)) {
129 (*pos)++;
130 } else {
131 pfx <<= 1;
132 pl++;
135 pfx |= 1;
138 }
139 }
140
142 {
145 int16_t lens[256];
146 uint8_t xlat[256];
148
150 &
pos, no_zero_count);
151 return ff_init_vlc_sparse(vlc, nb_bits,
pos, lens, 2, 2,
bits, 4, 4, xlat, 1, 1, 0);
152 }
153
154
155 /**
156 * nodes size must be 2*nb_codes
157 * first nb_codes nodes.count must be set
158 */
161 {
163 int cur_node;
164 int64_t sum = 0;
165
166 for (
i = 0;
i < nb_codes;
i++) {
170 }
171
172 if (sum >> 31) {
174 "Too high symbol frequencies. "
175 "Tree construction is not possible\n");
176 return -1;
177 }
179 cur_node = nb_codes;
180 nodes[nb_codes*2-1].
count = 0;
181 for (
i = 0;
i < nb_codes * 2 - 1;
i += 2) {
183 // find correct place to insert new node, and
184 // make space for the new node while at it
185 for(j = cur_node; j >
i + 2; j--){
186 if(cur_count > nodes[j-1].count ||
187 (cur_count == nodes[j-1].count &&
189 break;
190 nodes[j] = nodes[j - 1];
191 }
193 nodes[j].
count = cur_count;
195 cur_node++;
196 }
199 return -1;
200 }
201 return 0;
202 }