- const HuffEntry *aa = a, *bb = b;
- return (aa->len - bb->len) * 256 + bb->sym - aa->sym;
-}
-
-static int huff_cmp_len10(const void *a, const void *b)
-{
- const HuffEntry *aa = a, *bb = b;
- return (aa->len - bb->len) * 1024 + bb->sym - aa->sym;
-}
-
-static int huff_cmp_len12(const void *a, const void *b)
-{
- const HuffEntry *aa = a, *bb = b;
- return (aa->len - bb->len) * 4096 + bb->sym - aa->sym;
-}
-
-static int huff_build10(VLC *vlc, uint8_t *len)
-{
- HuffEntry he[1024];
- uint32_t code;
- int i;
-
- for (i = 0; i < 1024; i++) {
- he[i].sym = i;
- he[i].len = len[i];
- if (len[i] == 0 || len[i] > 32)
- return AVERROR_INVALIDDATA;
- }
- AV_QSORT(he, 1024, HuffEntry, huff_cmp_len10);
-
- code = 1;
- for (i = 1023; i >= 0; i--) {
- he[i].code = code >> (32 - he[i].len);
- code += 0x80000000u >> (he[i].len - 1);
- }
-
- ff_free_vlc(vlc);
- return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024,
- &he[0].len, sizeof(he[0]), sizeof(he[0].len),
- &he[0].code, sizeof(he[0]), sizeof(he[0].code),
- &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0);
-}
-
-static int huff_build12(VLC *vlc, uint8_t *len)
-{
- HuffEntry he[4096];
- uint32_t code;
- int i;
-
- for (i = 0; i < 4096; i++) {
- he[i].sym = i;
- he[i].len = len[i];
- if (len[i] == 0 || len[i] > 32)
- return AVERROR_INVALIDDATA;
+ unsigned nb_codes = 0, max = 0;
+
+ for (int i = 32; i > 0; i--) {
+ uint16_t curr = codes_count[i]; // # of leafs of length i
+ codes_count[i] = nb_codes / 2; // # of non-leaf nodes on level i
+ nb_codes = codes_count[i] + curr; // # of nodes on level i
+ if (curr && !max)
+ max = i;