int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0)

{

    HeapElem *h  = av_malloc_array(sizeof(*h), stats_size);

    int *up      = av_malloc_array(sizeof(*up) * 2, stats_size);

    uint8_t *len = av_malloc_array(sizeof(*len) * 2, stats_size);

    uint16_t *map= av_malloc_array(sizeof(*map), stats_size);

    int offset, i, next;

    int size = 0;

    int ret = 0;



    if (!h || !up || !len) {

        ret = AVERROR(ENOMEM);

        goto end;

    }



    for (i = 0; i<stats_size; i++) {

        dst[i] = 255;

        if (stats[i] || !skip0)

            map[size++] = i;

    }



    for (offset = 1; ; offset <<= 1) {

        for (i=0; i < size; i++) {

            h[i].name = i;

            h[i].val = (stats[map[i]] << 14) + offset;

        }

        for (i = size / 2 - 1; i >= 0; i--)

            heap_sift(h, i, size);



        for (next = size; next < size * 2 - 1; next++) {

            // merge the two smallest entries, and put it back in the heap

            uint64_t min1v = h[0].val;

            up[h[0].name] = next;

            h[0].val = INT64_MAX;

            heap_sift(h, 0, size);

            up[h[0].name] = next;

            h[0].name = next;

            h[0].val += min1v;

            heap_sift(h, 0, size);

        }



        len[2 * size - 2] = 0;

        for (i = 2 * size - 3; i >= size; i--)

            len[i] = len[up[i]] + 1;

        for (i = 0; i < size; i++) {

            dst[map[i]] = len[up[i]] + 1;

            if (dst[map[i]] >= 32) break;

        }

        if (i==size) break;

    }

end:

    av_free(h);

    av_free(up);

    av_free(len);

    av_free(map);

    return ret;

}

int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0)

{

    HeapElem *h  = av_malloc_array(sizeof(*h), stats_size);

    int *up      = av_malloc_array(sizeof(*up) * 2, stats_size);

    uint8_t *len = av_malloc_array(sizeof(*len) * 2, stats_size);

    uint16_t *map= av_malloc_array(sizeof(*map), stats_size);

    int offset, i, next;

    int size = 0;

    int ret = 0;



    if (!h || !up || !len) {

        ret = AVERROR(ENOMEM);

        goto end;

    }



    for (i = 0; i<stats_size; i++) {

        dst[i] = 255;

        if (stats[i] || !skip0)

            map[size++] = i;

    }



    for (offset = 1; ; offset <<= 1) {

        for (i=0; i < size; i++) {

            h[i].name = i;

            h[i].val = (stats[map[i]] << 14) + offset;

        }

        for (i = size / 2 - 1; i >= 0; i--)

            heap_sift(h, i, size);



        for (next = size; next < size * 2 - 1; next++) {

            

            uint64_t min1v = h[0].val;

            up[h[0].name] = next;

            h[0].val = INT64_MAX;

            heap_sift(h, 0, size);

            up[h[0].name] = next;

            h[0].name = next;

            h[0].val += min1v;

            heap_sift(h, 0, size);

        }



        len[2 * size - 2] = 0;

        for (i = 2 * size - 3; i >= size; i--)

            len[i] = len[up[i]] + 1;

        for (i = 0; i < size; i++) {

            dst[map[i]] = len[up[i]] + 1;

            if (dst[map[i]] >= 32) break;

        }

        if (i==size) break;

    }

end:

    av_free(h);

    av_free(up);

    av_free(len);

    av_free(map);

    return ret;

}

int FUNC_0(uint8_t *VAR_0, const uint64_t *VAR_1, int VAR_2, int VAR_3)

{

    HeapElem *h  = av_malloc_array(sizeof(*h), VAR_2);

    int *VAR_4      = av_malloc_array(sizeof(*VAR_4) * 2, VAR_2);

    uint8_t *len = av_malloc_array(sizeof(*len) * 2, VAR_2);

    uint16_t *map= av_malloc_array(sizeof(*map), VAR_2);

    int VAR_5, VAR_6, VAR_7;

    int VAR_8 = 0;

    int VAR_9 = 0;



    if (!h || !VAR_4 || !len) {

        VAR_9 = AVERROR(ENOMEM);

        goto end;

    }



    for (VAR_6 = 0; VAR_6<VAR_2; VAR_6++) {

        VAR_0[VAR_6] = 255;

        if (VAR_1[VAR_6] || !VAR_3)

            map[VAR_8++] = VAR_6;

    }



    for (VAR_5 = 1; ; VAR_5 <<= 1) {

        for (VAR_6=0; VAR_6 < VAR_8; VAR_6++) {

            h[VAR_6].name = VAR_6;

            h[VAR_6].val = (VAR_1[map[VAR_6]] << 14) + VAR_5;

        }

        for (VAR_6 = VAR_8 / 2 - 1; VAR_6 >= 0; VAR_6--)

            heap_sift(h, VAR_6, VAR_8);



        for (VAR_7 = VAR_8; VAR_7 < VAR_8 * 2 - 1; VAR_7++) {

            

            uint64_t min1v = h[0].val;

            VAR_4[h[0].name] = VAR_7;

            h[0].val = INT64_MAX;

            heap_sift(h, 0, VAR_8);

            VAR_4[h[0].name] = VAR_7;

            h[0].name = VAR_7;

            h[0].val += min1v;

            heap_sift(h, 0, VAR_8);

        }



        len[2 * VAR_8 - 2] = 0;

        for (VAR_6 = 2 * VAR_8 - 3; VAR_6 >= VAR_8; VAR_6--)

            len[VAR_6] = len[VAR_4[VAR_6]] + 1;

        for (VAR_6 = 0; VAR_6 < VAR_8; VAR_6++) {

            VAR_0[map[VAR_6]] = len[VAR_4[VAR_6]] + 1;

            if (VAR_0[map[VAR_6]] >= 32) break;

        }

        if (VAR_6==VAR_8) break;

    }

end:

    av_free(h);

    av_free(VAR_4);

    av_free(len);

    av_free(map);

    return VAR_9;

}

int FUNC_0(uint8_t *VAR_0, const uint64_t *VAR_1, int VAR_2, int VAR_3)
{ HeapElem *h  = av_malloc_array(sizeof(*h), VAR_2); int *VAR_4      = av_malloc_array(sizeof(*VAR_4) * 2, VAR_2); uint8_t *len = av_malloc_array(sizeof(*len) * 2, VAR_2); uint16_t *map= av_malloc_array(sizeof(*map), VAR_2); int VAR_5, VAR_6, VAR_7; int VAR_8 = 0; int VAR_9 = 0; if (!h || !VAR_4 || !len) { VAR_9 = AVERROR(ENOMEM); goto end; } for (VAR_6 = 0; VAR_6<VAR_2; VAR_6++) { VAR_0[VAR_6] = 255; if (VAR_1[VAR_6] || !VAR_3)
map[VAR_8++] = VAR_6; } for (VAR_5 = 1; ; VAR_5 <<= 1) { for (VAR_6=0; VAR_6 < VAR_8; VAR_6++) { h[VAR_6].name = VAR_6; h[VAR_6].val = (VAR_1[map[VAR_6]] << 14) + VAR_5; } for (VAR_6 = VAR_8 / 2 - 1; VAR_6 >= 0; VAR_6--) heap_sift(h, VAR_6, VAR_8); for (VAR_7 = VAR_8; VAR_7 < VAR_8 * 2 - 1; VAR_7++) { uint64_t min1v = h[0].val; VAR_4[h[0].name] = VAR_7; h[0].val = INT64_MAX; heap_sift(h, 0, VAR_8); VAR_4[h[0].name] = VAR_7; h[0].name = VAR_7; h[0].val += min1v; heap_sift(h, 0, VAR_8); } len[2 * VAR_8 - 2] = 0; for (VAR_6 = 2 * VAR_8 - 3; VAR_6 >= VAR_8; VAR_6--) len[VAR_6] = len[VAR_4[VAR_6]] + 1; for (VAR_6 = 0; VAR_6 < VAR_8; VAR_6++) { VAR_0[map[VAR_6]] = len[VAR_4[VAR_6]] + 1; if (VAR_0[map[VAR_6]] >= 32) break; } if (VAR_6==VAR_8) break; } end:
av_free(h); av_free(VAR_4); av_free(len); av_free(map); return VAR_9; }