static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,

                      uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,

                      int keyframe, int kf_slipt, int slice, int w, int h)

{

    uint8_t bits[270] = { 0 };

    uint32_t codes[270];

    VLC vlc;



    int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;

    int remaining_codes, surplus_codes, i;



    const int alphabet_size = 270 - keyframe;



    int last_symbol = 0, repeat = 0, prev_avail = 0;



    if (!keyframe) {

        int x, y, clipw, cliph;



        x     = get_bits(gb, 12);

        y     = get_bits(gb, 12);

        clipw = get_bits(gb, 12) + 1;

        cliph = get_bits(gb, 12) + 1;



        if (x + clipw > w || y + cliph > h)

            return AVERROR_INVALIDDATA;

        pal_dst += pal_stride * y + x;

        rgb_dst += rgb_stride * y + x * 3;

        w        = clipw;

        h        = cliph;

        if (y)

            prev_avail = 1;

    } else {

        if (slice > 0) {

            pal_dst   += pal_stride * kf_slipt;

            rgb_dst   += rgb_stride * kf_slipt;

            prev_avail = 1;

            h         -= kf_slipt;

        } else

            h = kf_slipt;

    }



    /* read explicit codes */

    do {

        while (current_codes--) {

            int symbol = get_bits(gb, 8);

            if (symbol >= 204 - keyframe)

                symbol += 14 - keyframe;

            else if (symbol > 189)

                symbol = get_bits1(gb) + (symbol << 1) - 190;

            if (bits[symbol])

                return AVERROR_INVALIDDATA;

            bits[symbol]  = current_length;

            codes[symbol] = next_code++;

            read_codes++;

        }

        current_length++;

        next_code     <<= 1;

        remaining_codes = (1 << current_length) - next_code;

        current_codes   = get_bits(gb, av_ceil_log2(remaining_codes + 1));

        if (current_length > 22 || current_codes > remaining_codes)

            return AVERROR_INVALIDDATA;

    } while (current_codes != remaining_codes);



    remaining_codes = alphabet_size - read_codes;



    /* determine the minimum length to fit the rest of the alphabet */

    while ((surplus_codes = (2 << current_length) -

                            (next_code << 1) - remaining_codes) < 0) {

        current_length++;

        next_code <<= 1;

    }



    /* add the rest of the symbols lexicographically */

    for (i = 0; i < alphabet_size; i++)

        if (!bits[i]) {

            if (surplus_codes-- == 0) {

                current_length++;

                next_code <<= 1;

            }

            bits[i]  = current_length;

            codes[i] = next_code++;

        }



    if (next_code != 1 << current_length)

        return AVERROR_INVALIDDATA;



    if (i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0))

        return i;



    /* frame decode */

    do {

        uint8_t *pp = pal_dst;

        uint8_t *rp = rgb_dst;

        do {

            if (repeat-- < 1) {

                int b = get_vlc2(gb, vlc.table, 9, 3);

                if (b < 256)

                    last_symbol = b;

                else if (b < 268) {

                    b -= 256;

                    if (b == 11)

                        b = get_bits(gb, 4) + 10;



                    if (!b)

                        repeat = 0;

                    else

                        repeat = get_bits(gb, b);



                    repeat += (1 << b) - 1;



                    if (last_symbol == -2) {

                        int skip = FFMIN(repeat, pal_dst + w - pp);

                        repeat -= skip;

                        pp     += skip;

                        rp     += skip * 3;

                    }

                } else

                    last_symbol = 267 - b;

            }

            if (last_symbol >= 0) {

                *pp = last_symbol;

                AV_WB24(rp, pal[last_symbol]);

            } else if (last_symbol == -1 && prev_avail) {

                *pp = *(pp - pal_stride);

                memcpy(rp, rp - rgb_stride, 3);

            }

            rp += 3;

        } while (++pp < pal_dst + w);

        pal_dst   += pal_stride;

        rgb_dst   += rgb_stride;

        prev_avail = 1;

    } while (--h);



    ff_free_vlc(&vlc);

    return 0;

}

static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,

                      uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,

                      int keyframe, int kf_slipt, int slice, int w, int h)

{

    uint8_t bits[270] = { 0 };

    uint32_t codes[270];

    VLC vlc;



    int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;

    int remaining_codes, surplus_codes, i;



    const int alphabet_size = 270 - keyframe;



    int last_symbol = 0, repeat = 0, prev_avail = 0;



    if (!keyframe) {

        int x, y, clipw, cliph;



        x     = get_bits(gb, 12);

        y     = get_bits(gb, 12);

        clipw = get_bits(gb, 12) + 1;

        cliph = get_bits(gb, 12) + 1;



        if (x + clipw > w || y + cliph > h)

            return AVERROR_INVALIDDATA;

        pal_dst += pal_stride * y + x;

        rgb_dst += rgb_stride * y + x * 3;

        w        = clipw;

        h        = cliph;

        if (y)

            prev_avail = 1;

    } else {

        if (slice > 0) {

            pal_dst   += pal_stride * kf_slipt;

            rgb_dst   += rgb_stride * kf_slipt;

            prev_avail = 1;

            h         -= kf_slipt;

        } else

            h = kf_slipt;

    }



    

    do {

        while (current_codes--) {

            int symbol = get_bits(gb, 8);

            if (symbol >= 204 - keyframe)

                symbol += 14 - keyframe;

            else if (symbol > 189)

                symbol = get_bits1(gb) + (symbol << 1) - 190;

            if (bits[symbol])

                return AVERROR_INVALIDDATA;

            bits[symbol]  = current_length;

            codes[symbol] = next_code++;

            read_codes++;

        }

        current_length++;

        next_code     <<= 1;

        remaining_codes = (1 << current_length) - next_code;

        current_codes   = get_bits(gb, av_ceil_log2(remaining_codes + 1));

        if (current_length > 22 || current_codes > remaining_codes)

            return AVERROR_INVALIDDATA;

    } while (current_codes != remaining_codes);



    remaining_codes = alphabet_size - read_codes;



    

    while ((surplus_codes = (2 << current_length) -

                            (next_code << 1) - remaining_codes) < 0) {

        current_length++;

        next_code <<= 1;

    }



    

    for (i = 0; i < alphabet_size; i++)

        if (!bits[i]) {

            if (surplus_codes-- == 0) {

                current_length++;

                next_code <<= 1;

            }

            bits[i]  = current_length;

            codes[i] = next_code++;

        }



    if (next_code != 1 << current_length)

        return AVERROR_INVALIDDATA;



    if (i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0))

        return i;



    

    do {

        uint8_t *pp = pal_dst;

        uint8_t *rp = rgb_dst;

        do {

            if (repeat-- < 1) {

                int b = get_vlc2(gb, vlc.table, 9, 3);

                if (b < 256)

                    last_symbol = b;

                else if (b < 268) {

                    b -= 256;

                    if (b == 11)

                        b = get_bits(gb, 4) + 10;



                    if (!b)

                        repeat = 0;

                    else

                        repeat = get_bits(gb, b);



                    repeat += (1 << b) - 1;



                    if (last_symbol == -2) {

                        int skip = FFMIN(repeat, pal_dst + w - pp);

                        repeat -= skip;

                        pp     += skip;

                        rp     += skip * 3;

                    }

                } else

                    last_symbol = 267 - b;

            }

            if (last_symbol >= 0) {

                *pp = last_symbol;

                AV_WB24(rp, pal[last_symbol]);

            } else if (last_symbol == -1 && prev_avail) {

                *pp = *(pp - pal_stride);

                memcpy(rp, rp - rgb_stride, 3);

            }

            rp += 3;

        } while (++pp < pal_dst + w);

        pal_dst   += pal_stride;

        rgb_dst   += rgb_stride;

        prev_avail = 1;

    } while (--h);



    ff_free_vlc(&vlc);

    return 0;

}

static int FUNC_0(GetBitContext *VAR_0, uint8_t *VAR_1, int VAR_2,

                      uint8_t *VAR_3, int VAR_4, uint32_t *VAR_5,

                      int VAR_6, int VAR_7, int VAR_8, int VAR_9, int VAR_10)

{

    uint8_t bits[270] = { 0 };

    uint32_t codes[270];

    VLC vlc;



    int VAR_11 = 0, VAR_12 = 0, VAR_13 = 0, VAR_14 = 0;

    int VAR_15, VAR_16, VAR_17;



    const int VAR_18 = 270 - VAR_6;



    int VAR_19 = 0, VAR_20 = 0, VAR_21 = 0;



    if (!VAR_6) {

        int VAR_22, VAR_23, VAR_24, VAR_25;



        VAR_22     = get_bits(VAR_0, 12);

        VAR_23     = get_bits(VAR_0, 12);

        VAR_24 = get_bits(VAR_0, 12) + 1;

        VAR_25 = get_bits(VAR_0, 12) + 1;



        if (VAR_22 + VAR_24 > VAR_9 || VAR_23 + VAR_25 > VAR_10)

            return AVERROR_INVALIDDATA;

        VAR_1 += VAR_2 * VAR_23 + VAR_22;

        VAR_3 += VAR_4 * VAR_23 + VAR_22 * 3;

        VAR_9        = VAR_24;

        VAR_10        = VAR_25;

        if (VAR_23)

            VAR_21 = 1;

    } else {

        if (VAR_8 > 0) {

            VAR_1   += VAR_2 * VAR_7;

            VAR_3   += VAR_4 * VAR_7;

            VAR_21 = 1;

            VAR_10         -= VAR_7;

        } else

            VAR_10 = VAR_7;

    }



    

    do {

        while (VAR_14--) {

            int VAR_26 = get_bits(VAR_0, 8);

            if (VAR_26 >= 204 - VAR_6)

                VAR_26 += 14 - VAR_6;

            else if (VAR_26 > 189)

                VAR_26 = get_bits1(VAR_0) + (VAR_26 << 1) - 190;

            if (bits[VAR_26])

                return AVERROR_INVALIDDATA;

            bits[VAR_26]  = VAR_11;

            codes[VAR_26] = VAR_13++;

            VAR_12++;

        }

        VAR_11++;

        VAR_13     <<= 1;

        VAR_15 = (1 << VAR_11) - VAR_13;

        VAR_14   = get_bits(VAR_0, av_ceil_log2(VAR_15 + 1));

        if (VAR_11 > 22 || VAR_14 > VAR_15)

            return AVERROR_INVALIDDATA;

    } while (VAR_14 != VAR_15);



    VAR_15 = VAR_18 - VAR_12;



    

    while ((VAR_16 = (2 << VAR_11) -

                            (VAR_13 << 1) - VAR_15) < 0) {

        VAR_11++;

        VAR_13 <<= 1;

    }



    

    for (VAR_17 = 0; VAR_17 < VAR_18; VAR_17++)

        if (!bits[VAR_17]) {

            if (VAR_16-- == 0) {

                VAR_11++;

                VAR_13 <<= 1;

            }

            bits[VAR_17]  = VAR_11;

            codes[VAR_17] = VAR_13++;

        }



    if (VAR_13 != 1 << VAR_11)

        return AVERROR_INVALIDDATA;



    if (VAR_17 = init_vlc(&vlc, 9, VAR_18, bits, 1, 1, codes, 4, 4, 0))

        return VAR_17;



    

    do {

        uint8_t *pp = VAR_1;

        uint8_t *rp = VAR_3;

        do {

            if (VAR_20-- < 1) {

                int VAR_27 = get_vlc2(VAR_0, vlc.table, 9, 3);

                if (VAR_27 < 256)

                    VAR_19 = VAR_27;

                else if (VAR_27 < 268) {

                    VAR_27 -= 256;

                    if (VAR_27 == 11)

                        VAR_27 = get_bits(VAR_0, 4) + 10;



                    if (!VAR_27)

                        VAR_20 = 0;

                    else

                        VAR_20 = get_bits(VAR_0, VAR_27);



                    VAR_20 += (1 << VAR_27) - 1;



                    if (VAR_19 == -2) {

                        int VAR_28 = FFMIN(VAR_20, VAR_1 + VAR_9 - pp);

                        VAR_20 -= VAR_28;

                        pp     += VAR_28;

                        rp     += VAR_28 * 3;

                    }

                } else

                    VAR_19 = 267 - VAR_27;

            }

            if (VAR_19 >= 0) {

                *pp = VAR_19;

                AV_WB24(rp, VAR_5[VAR_19]);

            } else if (VAR_19 == -1 && VAR_21) {

                *pp = *(pp - VAR_2);

                memcpy(rp, rp - VAR_4, 3);

            }

            rp += 3;

        } while (++pp < VAR_1 + VAR_9);

        VAR_1   += VAR_2;

        VAR_3   += VAR_4;

        VAR_21 = 1;

    } while (--VAR_10);



    ff_free_vlc(&vlc);

    return 0;

}

static int FUNC_0(GetBitContext *VAR_0, uint8_t *VAR_1, int VAR_2,
uint8_t *VAR_3, int VAR_4, uint32_t *VAR_5,
int VAR_6, int VAR_7, int VAR_8, int VAR_9, int VAR_10)
{ uint8_t bits[270] = { 0 }; uint32_t codes[270]; VLC vlc; int VAR_11 = 0, VAR_12 = 0, VAR_13 = 0, VAR_14 = 0; int VAR_15, VAR_16, VAR_17; const int VAR_18 = 270 - VAR_6; int VAR_19 = 0, VAR_20 = 0, VAR_21 = 0; if (!VAR_6) { int VAR_22, VAR_23, VAR_24, VAR_25; VAR_22     = get_bits(VAR_0, 12); VAR_23     = get_bits(VAR_0, 12); VAR_24 = get_bits(VAR_0, 12) + 1; VAR_25 = get_bits(VAR_0, 12) + 1; if (VAR_22 + VAR_24 > VAR_9 || VAR_23 + VAR_25 > VAR_10)
return AVERROR_INVALIDDATA; VAR_1 += VAR_2 * VAR_23 + VAR_22; VAR_3 += VAR_4 * VAR_23 + VAR_22 * 3; VAR_9        = VAR_24; VAR_10        = VAR_25; if (VAR_23)
VAR_21 = 1; } else { if (VAR_8 > 0) { VAR_1   += VAR_2 * VAR_7; VAR_3   += VAR_4 * VAR_7; VAR_21 = 1; VAR_10         -= VAR_7; } else VAR_10 = VAR_7; } do { while (VAR_14--) { int VAR_26 = get_bits(VAR_0, 8); if (VAR_26 >= 204 - VAR_6)
VAR_26 += 14 - VAR_6; else if (VAR_26 > 189)
VAR_26 = get_bits1(VAR_0) + (VAR_26 << 1) - 190; if (bits[VAR_26])
return AVERROR_INVALIDDATA; bits[VAR_26]  = VAR_11; codes[VAR_26] = VAR_13++; VAR_12++; } VAR_11++; VAR_13     <<= 1; VAR_15 = (1 << VAR_11) - VAR_13; VAR_14   = get_bits(VAR_0, av_ceil_log2(VAR_15 + 1)); if (VAR_11 > 22 || VAR_14 > VAR_15)
return AVERROR_INVALIDDATA; } while (VAR_14 != VAR_15); VAR_15 = VAR_18 - VAR_12; while ((VAR_16 = (2 << VAR_11) -
(VAR_13 << 1) - VAR_15) < 0) { VAR_11++; VAR_13 <<= 1; } for (VAR_17 = 0; VAR_17 < VAR_18; VAR_17++) if (!bits[VAR_17]) { if (VAR_16-- == 0) { VAR_11++; VAR_13 <<= 1; } bits[VAR_17]  = VAR_11; codes[VAR_17] = VAR_13++; } if (VAR_13 != 1 << VAR_11)
return AVERROR_INVALIDDATA; if (VAR_17 = init_vlc(&vlc, 9, VAR_18, bits, 1, 1, codes, 4, 4, 0))
return VAR_17; do { uint8_t *pp = VAR_1; uint8_t *rp = VAR_3; do { if (VAR_20-- < 1) { int VAR_27 = get_vlc2(VAR_0, vlc.table, 9, 3); if (VAR_27 < 256)
VAR_19 = VAR_27; else if (VAR_27 < 268) { VAR_27 -= 256; if (VAR_27 == 11)
VAR_27 = get_bits(VAR_0, 4) + 10; if (!VAR_27)
VAR_20 = 0; else
VAR_20 = get_bits(VAR_0, VAR_27); VAR_20 += (1 << VAR_27) - 1; if (VAR_19 == -2) { int VAR_28 = FFMIN(VAR_20, VAR_1 + VAR_9 - pp); VAR_20 -= VAR_28; pp     += VAR_28; rp     += VAR_28 * 3; } } else VAR_19 = 267 - VAR_27; } if (VAR_19 >= 0) { *pp = VAR_19; AV_WB24(rp, VAR_5[VAR_19]); } else if (VAR_19 == -1 && VAR_21) { *pp = *(pp - VAR_2); memcpy(rp, rp - VAR_4, 3); } rp += 3; } while (++pp < VAR_1 + VAR_9); VAR_1   += VAR_2; VAR_3   += VAR_4; VAR_21 = 1; } while (--VAR_10); ff_free_vlc(&vlc); return 0; }