- const HuffEntry *aa = a, *bb = b;
- return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
-}
-
-static int huff_cmp_sym(const void *a, const void *b)
-{
- const HuffEntry *aa = a, *bb = b;
- return bb->sym - aa->sym;
-}
-
-static void calculate_codes(HuffEntry *he)
-{
- uint32_t code;
- int i;
-
- AV_QSORT(he, 256, HuffEntry, magy_huff_cmp_len);
-
- code = 1;
- for (i = 255; i >= 0; i--) {
- he[i].code = code >> (32 - he[i].len);
- code += 0x80000000u >> (he[i].len - 1);
+ for (unsigned i = 32, nb_codes = 0; 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