static int xan_wc3_decode_frame(XanContext *s) {



    int width = s->avctx->width;

    int height = s->avctx->height;

    int total_pixels = width * height;

    unsigned char opcode;

    unsigned char flag = 0;

    int size = 0;

    int motion_x, motion_y;

    int x, y;



    unsigned char *opcode_buffer = s->buffer1;

    unsigned char *opcode_buffer_end = s->buffer1 + s->buffer1_size;

    int opcode_buffer_size = s->buffer1_size;

    const unsigned char *imagedata_buffer = s->buffer2;



    /* pointers to segments inside the compressed chunk */

    const unsigned char *huffman_segment;

    const unsigned char *size_segment;

    const unsigned char *vector_segment;

    const unsigned char *imagedata_segment;

    int huffman_offset, size_offset, vector_offset, imagedata_offset, imagedata_size;



    if (s->size < 8)

        return AVERROR_INVALIDDATA;



    huffman_offset    = AV_RL16(&s->buf[0]);

    size_offset       = AV_RL16(&s->buf[2]);

    vector_offset     = AV_RL16(&s->buf[4]);

    imagedata_offset  = AV_RL16(&s->buf[6]);



    if (huffman_offset   >= s->size ||

        size_offset      >= s->size ||

        vector_offset    >= s->size ||

        imagedata_offset >= s->size)

        return AVERROR_INVALIDDATA;



    huffman_segment   = s->buf + huffman_offset;

    size_segment      = s->buf + size_offset;

    vector_segment    = s->buf + vector_offset;

    imagedata_segment = s->buf + imagedata_offset;



    if (xan_huffman_decode(opcode_buffer, opcode_buffer_size,

                           huffman_segment, s->size - huffman_offset) < 0)

        return AVERROR_INVALIDDATA;



    if (imagedata_segment[0] == 2) {

        xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size);

        imagedata_size = s->buffer2_size;

    } else {

        imagedata_size = s->size - imagedata_offset - 1;

        imagedata_buffer = &imagedata_segment[1];

    }



    /* use the decoded data segments to build the frame */

    x = y = 0;

    while (total_pixels && opcode_buffer < opcode_buffer_end) {



        opcode = *opcode_buffer++;

        size = 0;



        switch (opcode) {



        case 0:

            flag ^= 1;

            continue;



        case 1:

        case 2:

        case 3:

        case 4:

        case 5:

        case 6:

        case 7:

        case 8:

            size = opcode;

            break;



        case 12:

        case 13:

        case 14:

        case 15:

        case 16:

        case 17:

        case 18:

            size += (opcode - 10);

            break;



        case 9:

        case 19:

            size = *size_segment++;

            break;



        case 10:

        case 20:

            size = AV_RB16(&size_segment[0]);

            size_segment += 2;

            break;



        case 11:

        case 21:

            size = AV_RB24(size_segment);

            size_segment += 3;

            break;

        }

        if (size > total_pixels)

            break;



        if (opcode < 12) {

            flag ^= 1;

            if (flag) {

                /* run of (size) pixels is unchanged from last frame */

                xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);

            } else {

                /* output a run of pixels from imagedata_buffer */

                if (imagedata_size < size)

                    break;

                xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);

                imagedata_buffer += size;

                imagedata_size -= size;

            }

        } else {

            /* run-based motion compensation from last frame */

            motion_x = sign_extend(*vector_segment >> 4,  4);

            motion_y = sign_extend(*vector_segment & 0xF, 4);

            vector_segment++;



            /* copy a run of pixels from the previous frame */

            xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);



            flag = 0;

        }



        /* coordinate accounting */

        total_pixels -= size;

        y += (x + size) / width;

        x  = (x + size) % width;

    }

    return 0;

}

static int xan_wc3_decode_frame(XanContext *s) {



    int width = s->avctx->width;

    int height = s->avctx->height;

    int total_pixels = width * height;

    unsigned char opcode;

    unsigned char flag = 0;

    int size = 0;

    int motion_x, motion_y;

    int x, y;



    unsigned char *opcode_buffer = s->buffer1;

    unsigned char *opcode_buffer_end = s->buffer1 + s->buffer1_size;

    int opcode_buffer_size = s->buffer1_size;

    const unsigned char *imagedata_buffer = s->buffer2;



    

    const unsigned char *huffman_segment;

    const unsigned char *size_segment;

    const unsigned char *vector_segment;

    const unsigned char *imagedata_segment;

    int huffman_offset, size_offset, vector_offset, imagedata_offset, imagedata_size;



    if (s->size < 8)

        return AVERROR_INVALIDDATA;



    huffman_offset    = AV_RL16(&s->buf[0]);

    size_offset       = AV_RL16(&s->buf[2]);

    vector_offset     = AV_RL16(&s->buf[4]);

    imagedata_offset  = AV_RL16(&s->buf[6]);



    if (huffman_offset   >= s->size ||

        size_offset      >= s->size ||

        vector_offset    >= s->size ||

        imagedata_offset >= s->size)

        return AVERROR_INVALIDDATA;



    huffman_segment   = s->buf + huffman_offset;

    size_segment      = s->buf + size_offset;

    vector_segment    = s->buf + vector_offset;

    imagedata_segment = s->buf + imagedata_offset;



    if (xan_huffman_decode(opcode_buffer, opcode_buffer_size,

                           huffman_segment, s->size - huffman_offset) < 0)

        return AVERROR_INVALIDDATA;



    if (imagedata_segment[0] == 2) {

        xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size);

        imagedata_size = s->buffer2_size;

    } else {

        imagedata_size = s->size - imagedata_offset - 1;

        imagedata_buffer = &imagedata_segment[1];

    }



    

    x = y = 0;

    while (total_pixels && opcode_buffer < opcode_buffer_end) {



        opcode = *opcode_buffer++;

        size = 0;



        switch (opcode) {



        case 0:

            flag ^= 1;

            continue;



        case 1:

        case 2:

        case 3:

        case 4:

        case 5:

        case 6:

        case 7:

        case 8:

            size = opcode;

            break;



        case 12:

        case 13:

        case 14:

        case 15:

        case 16:

        case 17:

        case 18:

            size += (opcode - 10);

            break;



        case 9:

        case 19:

            size = *size_segment++;

            break;



        case 10:

        case 20:

            size = AV_RB16(&size_segment[0]);

            size_segment += 2;

            break;



        case 11:

        case 21:

            size = AV_RB24(size_segment);

            size_segment += 3;

            break;

        }

        if (size > total_pixels)

            break;



        if (opcode < 12) {

            flag ^= 1;

            if (flag) {

                

                xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);

            } else {

                

                if (imagedata_size < size)

                    break;

                xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);

                imagedata_buffer += size;

                imagedata_size -= size;

            }

        } else {

            

            motion_x = sign_extend(*vector_segment >> 4,  4);

            motion_y = sign_extend(*vector_segment & 0xF, 4);

            vector_segment++;



            

            xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);



            flag = 0;

        }



        

        total_pixels -= size;

        y += (x + size) / width;

        x  = (x + size) % width;

    }

    return 0;

}

        xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size);
static int FUNC_0(XanContext *VAR_0) {



    int VAR_1 = VAR_0->avctx->VAR_1;

    int VAR_2 = VAR_0->avctx->VAR_2;

    int VAR_3 = VAR_1 * VAR_2;

    unsigned char VAR_4;

    unsigned char VAR_5 = 0;

    int VAR_6 = 0;

    int VAR_7, VAR_8;

    int VAR_9, VAR_10;



    unsigned char *VAR_11 = VAR_0->buffer1;

    unsigned char *VAR_12 = VAR_0->buffer1 + VAR_0->buffer1_size;

    int VAR_13 = VAR_0->buffer1_size;

    const unsigned char *VAR_14 = VAR_0->buffer2;



    

    const unsigned char *VAR_15;

    const unsigned char *VAR_16;

    const unsigned char *VAR_17;

    const unsigned char *VAR_18;

    int VAR_19, VAR_20, VAR_21, VAR_22, VAR_23;



    if (VAR_0->VAR_6 < 8)

        return AVERROR_INVALIDDATA;



    VAR_19    = AV_RL16(&VAR_0->buf[0]);

    VAR_20       = AV_RL16(&VAR_0->buf[2]);

    VAR_21     = AV_RL16(&VAR_0->buf[4]);

    VAR_22  = AV_RL16(&VAR_0->buf[6]);



    if (VAR_19   >= VAR_0->VAR_6 ||

        VAR_20      >= VAR_0->VAR_6 ||

        VAR_21    >= VAR_0->VAR_6 ||

        VAR_22 >= VAR_0->VAR_6)

        return AVERROR_INVALIDDATA;



    VAR_15   = VAR_0->buf + VAR_19;

    VAR_16      = VAR_0->buf + VAR_20;

    VAR_17    = VAR_0->buf + VAR_21;

    VAR_18 = VAR_0->buf + VAR_22;



    if (xan_huffman_decode(VAR_11, VAR_13,

                           VAR_15, VAR_0->VAR_6 - VAR_19) < 0)

        return AVERROR_INVALIDDATA;



    if (VAR_18[0] == 2) {

        xan_unpack(VAR_0->buffer2, &VAR_18[1], VAR_0->buffer2_size);

        VAR_23 = VAR_0->buffer2_size;

    } else {

        VAR_23 = VAR_0->VAR_6 - VAR_22 - 1;

        VAR_14 = &VAR_18[1];

    }



    

    VAR_9 = VAR_10 = 0;

    while (VAR_3 && VAR_11 < VAR_12) {



        VAR_4 = *VAR_11++;

        VAR_6 = 0;



        switch (VAR_4) {



        case 0:

            VAR_5 ^= 1;

            continue;



        case 1:

        case 2:

        case 3:

        case 4:

        case 5:

        case 6:

        case 7:

        case 8:

            VAR_6 = VAR_4;

            break;



        case 12:

        case 13:

        case 14:

        case 15:

        case 16:

        case 17:

        case 18:

            VAR_6 += (VAR_4 - 10);

            break;



        case 9:

        case 19:

            VAR_6 = *VAR_16++;

            break;



        case 10:

        case 20:

            VAR_6 = AV_RB16(&VAR_16[0]);

            VAR_16 += 2;

            break;



        case 11:

        case 21:

            VAR_6 = AV_RB24(VAR_16);

            VAR_16 += 3;

            break;

        }

        if (VAR_6 > VAR_3)

            break;



        if (VAR_4 < 12) {

            VAR_5 ^= 1;

            if (VAR_5) {

                

                xan_wc3_copy_pixel_run(VAR_0, VAR_9, VAR_10, VAR_6, 0, 0);

            } else {

                

                if (VAR_23 < VAR_6)

                    break;

                xan_wc3_output_pixel_run(VAR_0, VAR_14, VAR_9, VAR_10, VAR_6);

                VAR_14 += VAR_6;

                VAR_23 -= VAR_6;

            }

        } else {

            

            VAR_7 = sign_extend(*VAR_17 >> 4,  4);

            VAR_8 = sign_extend(*VAR_17 & 0xF, 4);

            VAR_17++;



            

            xan_wc3_copy_pixel_run(VAR_0, VAR_9, VAR_10, VAR_6, VAR_7, VAR_8);



            VAR_5 = 0;

        }



        

        VAR_3 -= VAR_6;

        VAR_10 += (VAR_9 + VAR_6) / VAR_1;

        VAR_9  = (VAR_9 + VAR_6) % VAR_1;

    }

    return 0;

}

static int FUNC_0(XanContext *VAR_0) { int VAR_1 = VAR_0->avctx->VAR_1; int VAR_2 = VAR_0->avctx->VAR_2; int VAR_3 = VAR_1 * VAR_2; unsigned char VAR_4; unsigned char VAR_5 = 0; int VAR_6 = 0; int VAR_7, VAR_8; int VAR_9, VAR_10; unsigned char *VAR_11 = VAR_0->buffer1; unsigned char *VAR_12 = VAR_0->buffer1 + VAR_0->buffer1_size; int VAR_13 = VAR_0->buffer1_size; const unsigned char *VAR_14 = VAR_0->buffer2; const unsigned char *VAR_15; const unsigned char *VAR_16; const unsigned char *VAR_17; const unsigned char *VAR_18; int VAR_19, VAR_20, VAR_21, VAR_22, VAR_23; if (VAR_0->VAR_6 < 8)
return AVERROR_INVALIDDATA; VAR_19    = AV_RL16(&VAR_0->buf[0]); VAR_20       = AV_RL16(&VAR_0->buf[2]); VAR_21     = AV_RL16(&VAR_0->buf[4]); VAR_22  = AV_RL16(&VAR_0->buf[6]); if (VAR_19   >= VAR_0->VAR_6 ||
VAR_20      >= VAR_0->VAR_6 ||
VAR_21    >= VAR_0->VAR_6 ||
VAR_22 >= VAR_0->VAR_6)
return AVERROR_INVALIDDATA; VAR_15   = VAR_0->buf + VAR_19; VAR_16      = VAR_0->buf + VAR_20; VAR_17    = VAR_0->buf + VAR_21; VAR_18 = VAR_0->buf + VAR_22; if (xan_huffman_decode(VAR_11, VAR_13,
VAR_15, VAR_0->VAR_6 - VAR_19) < 0)
return AVERROR_INVALIDDATA; if (VAR_18[0] == 2) { xan_unpack(VAR_0->buffer2, &VAR_18[1], VAR_0->buffer2_size); VAR_23 = VAR_0->buffer2_size; } else { VAR_23 = VAR_0->VAR_6 - VAR_22 - 1; VAR_14 = &VAR_18[1]; } VAR_9 = VAR_10 = 0; while (VAR_3 && VAR_11 < VAR_12) { VAR_4 = *VAR_11++; VAR_6 = 0; switch (VAR_4) { case 0:
VAR_5 ^= 1; continue; case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
VAR_6 = VAR_4; break; case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
VAR_6 += (VAR_4 - 10); break; case 9:
case 19:
VAR_6 = *VAR_16++; break; case 10:
case 20:
VAR_6 = AV_RB16(&VAR_16[0]); VAR_16 += 2; break; case 11:
case 21:
VAR_6 = AV_RB24(VAR_16); VAR_16 += 3; break; } if (VAR_6 > VAR_3)
break; if (VAR_4 < 12) { VAR_5 ^= 1; if (VAR_5) { xan_wc3_copy_pixel_run(VAR_0, VAR_9, VAR_10, VAR_6, 0, 0); } else { if (VAR_23 < VAR_6)
break; xan_wc3_output_pixel_run(VAR_0, VAR_14, VAR_9, VAR_10, VAR_6); VAR_14 += VAR_6; VAR_23 -= VAR_6; } } else { VAR_7 = sign_extend(*VAR_17 >> 4,  4); VAR_8 = sign_extend(*VAR_17 & 0xF, 4); VAR_17++; xan_wc3_copy_pixel_run(VAR_0, VAR_9, VAR_10, VAR_6, VAR_7, VAR_8); VAR_5 = 0; } VAR_3 -= VAR_6; VAR_10 += (VAR_9 + VAR_6) / VAR_1; VAR_9  = (VAR_9 + VAR_6) % VAR_1; } return 0; }