41 while (root * 2 + 1 < size) {
42 int child = root * 2 + 1;
43 if (child < size - 1 && h[child].
val > h[child+1].
val)
45 if (h[root].
val > h[child].
val) {
61 for (offset = 1; ; offset <<= 1) {
62 for (i=0; i <
size; i++) {
64 h[i].
val = (stats[i] << 8) + offset;
66 for (i = size / 2 - 1; i >= 0; i--)
69 for (next = size; next < size * 2 - 1; next++) {
71 uint64_t min1v = h[0].
val;
81 len[2 * size - 2] = 0;
82 for (i = 2 * size - 3; i >=
size; i--)
83 len[i] = len[up[i]] + 1;
84 for (i = 0; i <
size; i++) {
85 dst[i] = len[up[i]] + 1;
86 if (dst[i] >= 32)
break;
93 Node *nodes,
int node,
94 uint32_t pfx,
int pl,
int *pos,
int no_zero_count)
99 if (s !=
HNODE || (no_zero_count && !nodes[node].
count)) {
110 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
124 &pos, no_zero_count);
125 return ff_init_vlc_sparse(vlc, nb_bits, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
140 for (i = 0; i < nb_codes; i++) {
143 sum += nodes[i].
count;
148 "Too high symbol frequencies. "
149 "Tree construction is not possible\n");
152 qsort(nodes, nb_codes,
sizeof(
Node), cmp);
154 nodes[nb_codes*2-1].
count = 0;
155 for (i = 0; i < nb_codes * 2 - 1; i += 2) {
156 uint32_t cur_count = nodes[i].
count + nodes[i+1].
count;
159 for(j = cur_node; j > i + 2; j--){
160 if(cur_count > nodes[j-1].
count ||
161 (cur_count == nodes[j-1].
count &&
164 nodes[j] = nodes[j - 1];
167 nodes[j].
count = cur_count;
171 if (
build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags, nb_bits) < 0) {
const char const char void * val
#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)
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
void ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats)
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
int(* HuffCmp)(const void *va, const void *vb)
static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags, int nb_bits)
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)
AVS_FilterInfo AVS_Value child
static const uint8_t offset[511][2]
bitstream reader API header.
#define FFSWAP(type, a, b)
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
Libavcodec external API header.
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...
main external API structure.
huffman tree builder and VLC generator