43 while (root * 2 + 1 < size) {
44 int child = root * 2 + 1;
45 if (child < size - 1 && h[child].val > h[child+1].val)
47 if (h[root].val > h[child].val) {
63 for (offset = 1; ; offset <<= 1) {
64 for (i=0; i <
size; i++) {
66 h[i].
val = (stats[i] << 8) + offset;
68 for (i = size / 2 - 1; i >= 0; i--)
71 for (next = size; next < size * 2 - 1; next++) {
73 uint64_t min1v = h[0].
val;
83 len[2 * size - 2] = 0;
84 for (i = 2 * size - 3; i >=
size; i--)
85 len[i] = len[up[i]] + 1;
86 for (i = 0; i <
size; i++) {
87 dst[i] = len[up[i]] + 1;
88 if (dst[i] >= 32)
break;
95 Node *nodes,
int node,
96 uint32_t pfx,
int pl,
int *pos,
int no_zero_count)
101 if (s !=
HNODE || (no_zero_count && !nodes[node].count)) {
112 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
126 &pos, no_zero_count);
127 return ff_init_vlc_sparse(vlc, 9, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
142 for (i = 0; i < nb_codes; i++) {
145 sum += nodes[i].
count;
150 "Too high symbol frequencies. " 151 "Tree construction is not possible\n");
154 qsort(nodes, nb_codes,
sizeof(
Node), cmp);
156 nodes[nb_codes*2-1].
count = 0;
157 for (i = 0; i < nb_codes * 2 - 1; i += 2) {
160 nodes[cur_node].
n0 = i;
161 for (j = cur_node; j > 0; j--) {
162 if (nodes[j].count > nodes[j - 1].count ||
163 (nodes[j].count == nodes[j - 1].count &&
165 nodes[j].n0 == j - 1 || nodes[j].n0 == j - 2 ||
166 (nodes[j].sym!=
HNODE && nodes[j-1].sym!=
HNODE))))
#define FF_HUFFMAN_FLAG_HNODE_FIRST
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)
void ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats)
int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, Node *nodes, HuffCmp cmp, int flags)
nodes size must be 2*nb_codes first nb_codes nodes.count must be set
int ff_init_vlc_sparse(VLC *vlc, 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)
bitstream reader API header.
static void heap_sift(HeapElem *h, int root, int size)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_HUFFMAN_FLAG_ZERO_COUNT
void av_log(void *avcl, int level, const char *fmt,...)
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...
static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags)
Libavcodec external API header.
main external API structure.
huffman tree builder and VLC generator
#define FFSWAP(type, a, b)
int(* HuffCmp)(const void *va, const void *vb)