static int gif_read_image(GifState *s, AVFrame *frame)

{

    int left, top, width, height, bits_per_pixel, code_size, flags;

    int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;

    uint8_t *ptr, *spal, *palette, *ptr1;



    left = bytestream_get_le16(&s->bytestream);

    top = bytestream_get_le16(&s->bytestream);

    width = bytestream_get_le16(&s->bytestream);

    height = bytestream_get_le16(&s->bytestream);

    flags = bytestream_get_byte(&s->bytestream);

    is_interleaved = flags & 0x40;

    has_local_palette = flags & 0x80;

    bits_per_pixel = (flags & 0x07) + 1;



    av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);



    if (has_local_palette) {

        bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));

        palette = s->local_palette;

    } else {

        palette = s->global_palette;

        bits_per_pixel = s->bits_per_pixel;

    }



    /* verify that all the image is inside the screen dimensions */

    if (left + width > s->screen_width ||

        top + height > s->screen_height)

        return AVERROR(EINVAL);



    /* build the palette */

    n = (1 << bits_per_pixel);

    spal = palette;

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

        s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);

        spal += 3;

    }

    for(; i < 256; i++)

        s->image_palette[i] = (0xffu << 24);

    /* handle transparency */

    if (s->transparent_color_index >= 0)

        s->image_palette[s->transparent_color_index] = 0;



    /* now get the image data */

    code_size = bytestream_get_byte(&s->bytestream);

    ff_lzw_decode_init(s->lzw, code_size, s->bytestream,

                       s->bytestream_end - s->bytestream, FF_LZW_GIF);



    /* read all the image */

    linesize = frame->linesize[0];

    ptr1 = frame->data[0] + top * linesize + left;

    ptr = ptr1;

    pass = 0;

    y1 = 0;

    for (y = 0; y < height; y++) {

        ff_lzw_decode(s->lzw, ptr, width);

        if (is_interleaved) {

            switch(pass) {

            default:

            case 0:

            case 1:

                y1 += 8;

                ptr += linesize * 8;

                if (y1 >= height) {

                    y1 = pass ? 2 : 4;

                    ptr = ptr1 + linesize * y1;

                    pass++;

                }

                break;

            case 2:

                y1 += 4;

                ptr += linesize * 4;

                if (y1 >= height) {

                    y1 = 1;

                    ptr = ptr1 + linesize;

                    pass++;

                }

                break;

            case 3:

                y1 += 2;

                ptr += linesize * 2;

                break;

            }

        } else {

            ptr += linesize;

        }

    }

    /* read the garbage data until end marker is found */

    ff_lzw_decode_tail(s->lzw);

    s->bytestream = ff_lzw_cur_ptr(s->lzw);

    return 0;

}

static int gif_read_image(GifState *s, AVFrame *frame)

{

    int left, top, width, height, bits_per_pixel, code_size, flags;

    int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;

    uint8_t *ptr, *spal, *palette, *ptr1;



    left = bytestream_get_le16(&s->bytestream);

    top = bytestream_get_le16(&s->bytestream);

    width = bytestream_get_le16(&s->bytestream);

    height = bytestream_get_le16(&s->bytestream);

    flags = bytestream_get_byte(&s->bytestream);

    is_interleaved = flags & 0x40;

    has_local_palette = flags & 0x80;

    bits_per_pixel = (flags & 0x07) + 1;



    av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);



    if (has_local_palette) {

        bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));

        palette = s->local_palette;

    } else {

        palette = s->global_palette;

        bits_per_pixel = s->bits_per_pixel;

    }



    

    if (left + width > s->screen_width ||

        top + height > s->screen_height)

        return AVERROR(EINVAL);



    

    n = (1 << bits_per_pixel);

    spal = palette;

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

        s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);

        spal += 3;

    }

    for(; i < 256; i++)

        s->image_palette[i] = (0xffu << 24);

    

    if (s->transparent_color_index >= 0)

        s->image_palette[s->transparent_color_index] = 0;



    

    code_size = bytestream_get_byte(&s->bytestream);

    ff_lzw_decode_init(s->lzw, code_size, s->bytestream,

                       s->bytestream_end - s->bytestream, FF_LZW_GIF);



    

    linesize = frame->linesize[0];

    ptr1 = frame->data[0] + top * linesize + left;

    ptr = ptr1;

    pass = 0;

    y1 = 0;

    for (y = 0; y < height; y++) {

        ff_lzw_decode(s->lzw, ptr, width);

        if (is_interleaved) {

            switch(pass) {

            default:

            case 0:

            case 1:

                y1 += 8;

                ptr += linesize * 8;

                if (y1 >= height) {

                    y1 = pass ? 2 : 4;

                    ptr = ptr1 + linesize * y1;

                    pass++;

                }

                break;

            case 2:

                y1 += 4;

                ptr += linesize * 4;

                if (y1 >= height) {

                    y1 = 1;

                    ptr = ptr1 + linesize;

                    pass++;

                }

                break;

            case 3:

                y1 += 2;

                ptr += linesize * 2;

                break;

            }

        } else {

            ptr += linesize;

        }

    }

    

    ff_lzw_decode_tail(s->lzw);

    s->bytestream = ff_lzw_cur_ptr(s->lzw);

    return 0;

}

static int FUNC_0(GifState *VAR_0, AVFrame *VAR_1)

{

    int VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8;

    int VAR_9, VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15, VAR_16;

    uint8_t *ptr, *spal, *palette, *ptr1;



    VAR_2 = bytestream_get_le16(&VAR_0->bytestream);

    VAR_3 = bytestream_get_le16(&VAR_0->bytestream);

    VAR_4 = bytestream_get_le16(&VAR_0->bytestream);

    VAR_5 = bytestream_get_le16(&VAR_0->bytestream);

    VAR_8 = bytestream_get_byte(&VAR_0->bytestream);

    VAR_9 = VAR_8 & 0x40;

    VAR_10 = VAR_8 & 0x80;

    VAR_6 = (VAR_8 & 0x07) + 1;



    av_dlog(VAR_0->avctx, "gif: image x=%d VAR_11=%d w=%d h=%d\VAR_15", VAR_2, VAR_3, VAR_4, VAR_5);



    if (VAR_10) {

        bytestream_get_buffer(&VAR_0->bytestream, VAR_0->local_palette, 3 * (1 << VAR_6));

        palette = VAR_0->local_palette;

    } else {

        palette = VAR_0->global_palette;

        VAR_6 = VAR_0->VAR_6;

    }



    

    if (VAR_2 + VAR_4 > VAR_0->screen_width ||

        VAR_3 + VAR_5 > VAR_0->screen_height)

        return AVERROR(EINVAL);



    

    VAR_15 = (1 << VAR_6);

    spal = palette;

    for(VAR_16 = 0; VAR_16 < VAR_15; VAR_16++) {

        VAR_0->image_palette[VAR_16] = (0xffu << 24) | AV_RB24(spal);

        spal += 3;

    }

    for(; VAR_16 < 256; VAR_16++)

        VAR_0->image_palette[VAR_16] = (0xffu << 24);

    

    if (VAR_0->transparent_color_index >= 0)

        VAR_0->image_palette[VAR_0->transparent_color_index] = 0;



    

    VAR_7 = bytestream_get_byte(&VAR_0->bytestream);

    ff_lzw_decode_init(VAR_0->lzw, VAR_7, VAR_0->bytestream,

                       VAR_0->bytestream_end - VAR_0->bytestream, FF_LZW_GIF);



    

    VAR_14 = VAR_1->VAR_14[0];

    ptr1 = VAR_1->data[0] + VAR_3 * VAR_14 + VAR_2;

    ptr = ptr1;

    VAR_12 = 0;

    VAR_13 = 0;

    for (VAR_11 = 0; VAR_11 < VAR_5; VAR_11++) {

        ff_lzw_decode(VAR_0->lzw, ptr, VAR_4);

        if (VAR_9) {

            switch(VAR_12) {

            default:

            case 0:

            case 1:

                VAR_13 += 8;

                ptr += VAR_14 * 8;

                if (VAR_13 >= VAR_5) {

                    VAR_13 = VAR_12 ? 2 : 4;

                    ptr = ptr1 + VAR_14 * VAR_13;

                    VAR_12++;

                }

                break;

            case 2:

                VAR_13 += 4;

                ptr += VAR_14 * 4;

                if (VAR_13 >= VAR_5) {

                    VAR_13 = 1;

                    ptr = ptr1 + VAR_14;

                    VAR_12++;

                }

                break;

            case 3:

                VAR_13 += 2;

                ptr += VAR_14 * 2;

                break;

            }

        } else {

            ptr += VAR_14;

        }

    }

    

    ff_lzw_decode_tail(VAR_0->lzw);

    VAR_0->bytestream = ff_lzw_cur_ptr(VAR_0->lzw);

    return 0;

}

static int FUNC_0(GifState *VAR_0, AVFrame *VAR_1)
{ int VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8; int VAR_9, VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15, VAR_16; uint8_t *ptr, *spal, *palette, *ptr1; VAR_2 = bytestream_get_le16(&VAR_0->bytestream); VAR_3 = bytestream_get_le16(&VAR_0->bytestream); VAR_4 = bytestream_get_le16(&VAR_0->bytestream); VAR_5 = bytestream_get_le16(&VAR_0->bytestream); VAR_8 = bytestream_get_byte(&VAR_0->bytestream); VAR_9 = VAR_8 & 0x40; VAR_10 = VAR_8 & 0x80; VAR_6 = (VAR_8 & 0x07) + 1; av_dlog(VAR_0->avctx, "gif: image x=%d VAR_11=%d w=%d h=%d\VAR_15", VAR_2, VAR_3, VAR_4, VAR_5); if (VAR_10) { bytestream_get_buffer(&VAR_0->bytestream, VAR_0->local_palette, 3 * (1 << VAR_6)); palette = VAR_0->local_palette; } else { palette = VAR_0->global_palette; VAR_6 = VAR_0->VAR_6; } if (VAR_2 + VAR_4 > VAR_0->screen_width ||
VAR_3 + VAR_5 > VAR_0->screen_height)
return AVERROR(EINVAL); VAR_15 = (1 << VAR_6); spal = palette; for(VAR_16 = 0; VAR_16 < VAR_15; VAR_16++) { VAR_0->image_palette[VAR_16] = (0xffu << 24) | AV_RB24(spal); spal += 3; } for(; VAR_16 < 256; VAR_16++) VAR_0->image_palette[VAR_16] = (0xffu << 24); if (VAR_0->transparent_color_index >= 0)
VAR_0->image_palette[VAR_0->transparent_color_index] = 0; VAR_7 = bytestream_get_byte(&VAR_0->bytestream); ff_lzw_decode_init(VAR_0->lzw, VAR_7, VAR_0->bytestream,
VAR_0->bytestream_end - VAR_0->bytestream, FF_LZW_GIF); VAR_14 = VAR_1->VAR_14[0]; ptr1 = VAR_1->data[0] + VAR_3 * VAR_14 + VAR_2; ptr = ptr1; VAR_12 = 0; VAR_13 = 0; for (VAR_11 = 0; VAR_11 < VAR_5; VAR_11++) { ff_lzw_decode(VAR_0->lzw, ptr, VAR_4); if (VAR_9) { switch(VAR_12) { default:
case 0:
case 1:
VAR_13 += 8; ptr += VAR_14 * 8; if (VAR_13 >= VAR_5) { VAR_13 = VAR_12 ? 2 : 4; ptr = ptr1 + VAR_14 * VAR_13; VAR_12++; } break; case 2:
VAR_13 += 4; ptr += VAR_14 * 4; if (VAR_13 >= VAR_5) { VAR_13 = 1; ptr = ptr1 + VAR_14; VAR_12++; } break; case 3:
VAR_13 += 2; ptr += VAR_14 * 2; break; } } else { ptr += VAR_14; } } ff_lzw_decode_tail(VAR_0->lzw); VAR_0->bytestream = ff_lzw_cur_ptr(VAR_0->lzw); return 0; }