static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){

    unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0];

    int num_mvs;

    int num_blocks_raw;

    int num_blocks_packed;

    int vector_bits;

    int i,j,x,y;

    GetBitContext gb;

    int mvbits;

    const unsigned char *blocks_raw;



    if(buf+12>buf_end)

        return -1;



    num_mvs           = AV_RL16(&buf[0]);

    num_blocks_raw    = AV_RL16(&buf[2]);

    num_blocks_packed = AV_RL16(&buf[4]);

    vector_bits       = AV_RL16(&buf[6]);

    buf += 12;



    /* allocate codebook buffers as neccessary */

    if (num_mvs > s->num_mvs) {

        s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));

        s->num_mvs = num_mvs;

    }



    if (num_blocks_packed > s->num_blocks_packed) {

        s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));

        s->num_blocks_packed = num_blocks_packed;

    }



    /* read motion vectors */

    mvbits = (num_mvs*2*10+31) & ~31;



    if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end)

        return -1;



    init_get_bits(&gb, buf, mvbits);

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

        s->mv_codebook[i][0] = get_sbits(&gb, 10);

        s->mv_codebook[i][1] = get_sbits(&gb, 10);

    }

    buf += mvbits>>3;



    /* note ptr to uncompressed blocks */

    blocks_raw = buf;

    buf += num_blocks_raw*16;



    /* read compressed blocks */

    init_get_bits(&gb, buf, (buf_end-buf)<<3);

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

        int tmp[4];

        for(j=0; j<4; j++)

            tmp[j] = get_bits(&gb, 8);

        for(j=0; j<16; j++)

            s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];

    }



    if (get_bits_left(&gb) < vector_bits *

        (s->avctx->height/4) * (s->avctx->width/4))

        return -1;



    /* read vectors and build frame */

    for(y=0; y<s->avctx->height/4; y++)

    for(x=0; x<s->avctx->width/4; x++) {

        unsigned int vector = get_bits(&gb, vector_bits);

        const unsigned char *src;

        int src_stride;



        if (vector < num_mvs) {

            src = s->last_frame.data[0] +

                  (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +

                   x*4 + s->mv_codebook[vector][0];

            src_stride = s->last_frame.linesize[0];

            if (src+3*src_stride+3>=frame0_end)

                continue;

        }else{

            int offset = vector - num_mvs;

            if (offset<num_blocks_raw)

                src = blocks_raw + 16*offset;

            else if (offset-num_blocks_raw<num_blocks_packed)

                src = s->block_codebook[offset-num_blocks_raw];

            else

                continue;

            src_stride = 4;

        }



        for(j=0; j<4; j++)

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

            s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i)  ] =

               src[j*src_stride + i];

    }



    return 0;

}

static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){

    unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0];

    int num_mvs;

    int num_blocks_raw;

    int num_blocks_packed;

    int vector_bits;

    int i,j,x,y;

    GetBitContext gb;

    int mvbits;

    const unsigned char *blocks_raw;



    if(buf+12>buf_end)

        return -1;



    num_mvs           = AV_RL16(&buf[0]);

    num_blocks_raw    = AV_RL16(&buf[2]);

    num_blocks_packed = AV_RL16(&buf[4]);

    vector_bits       = AV_RL16(&buf[6]);

    buf += 12;



    

    if (num_mvs > s->num_mvs) {

        s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));

        s->num_mvs = num_mvs;

    }



    if (num_blocks_packed > s->num_blocks_packed) {

        s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));

        s->num_blocks_packed = num_blocks_packed;

    }



    

    mvbits = (num_mvs*2*10+31) & ~31;



    if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end)

        return -1;



    init_get_bits(&gb, buf, mvbits);

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

        s->mv_codebook[i][0] = get_sbits(&gb, 10);

        s->mv_codebook[i][1] = get_sbits(&gb, 10);

    }

    buf += mvbits>>3;



    

    blocks_raw = buf;

    buf += num_blocks_raw*16;



    

    init_get_bits(&gb, buf, (buf_end-buf)<<3);

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

        int tmp[4];

        for(j=0; j<4; j++)

            tmp[j] = get_bits(&gb, 8);

        for(j=0; j<16; j++)

            s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];

    }



    if (get_bits_left(&gb) < vector_bits *

        (s->avctx->height/4) * (s->avctx->width/4))

        return -1;



    

    for(y=0; y<s->avctx->height/4; y++)

    for(x=0; x<s->avctx->width/4; x++) {

        unsigned int vector = get_bits(&gb, vector_bits);

        const unsigned char *src;

        int src_stride;



        if (vector < num_mvs) {

            src = s->last_frame.data[0] +

                  (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +

                   x*4 + s->mv_codebook[vector][0];

            src_stride = s->last_frame.linesize[0];

            if (src+3*src_stride+3>=frame0_end)

                continue;

        }else{

            int offset = vector - num_mvs;

            if (offset<num_blocks_raw)

                src = blocks_raw + 16*offset;

            else if (offset-num_blocks_raw<num_blocks_packed)

                src = s->block_codebook[offset-num_blocks_raw];

            else

                continue;

            src_stride = 4;

        }



        for(j=0; j<4; j++)

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

            s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i)  ] =

               src[j*src_stride + i];

    }



    return 0;

}

    unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0];             if (src+3*src_stride+3>=frame0_end)
static int FUNC_0(TgvContext * VAR_0, const uint8_t *VAR_1, const uint8_t *VAR_2){

    unsigned char *VAR_3 = VAR_0->last_frame.data[0] + VAR_0->avctx->width*VAR_0->last_frame.linesize[0];

    int VAR_4;

    int VAR_5;

    int VAR_6;

    int VAR_7;

    int VAR_8,VAR_9,VAR_10,VAR_11;

    GetBitContext gb;

    int VAR_12;

    const unsigned char *VAR_13;



    if(VAR_1+12>VAR_2)

        return -1;



    VAR_4           = AV_RL16(&VAR_1[0]);

    VAR_5    = AV_RL16(&VAR_1[2]);

    VAR_6 = AV_RL16(&VAR_1[4]);

    VAR_7       = AV_RL16(&VAR_1[6]);

    VAR_1 += 12;



    

    if (VAR_4 > VAR_0->VAR_4) {

        VAR_0->mv_codebook = av_realloc(VAR_0->mv_codebook, VAR_4*2*sizeof(int));

        VAR_0->VAR_4 = VAR_4;

    }



    if (VAR_6 > VAR_0->VAR_6) {

        VAR_0->block_codebook = av_realloc(VAR_0->block_codebook, VAR_6*16*sizeof(unsigned char));

        VAR_0->VAR_6 = VAR_6;

    }



    

    VAR_12 = (VAR_4*2*10+31) & ~31;



    if (VAR_1+(VAR_12>>3)+16*VAR_5+8*VAR_6>VAR_2)

        return -1;



    init_get_bits(&gb, VAR_1, VAR_12);

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

        VAR_0->mv_codebook[VAR_8][0] = get_sbits(&gb, 10);

        VAR_0->mv_codebook[VAR_8][1] = get_sbits(&gb, 10);

    }

    VAR_1 += VAR_12>>3;



    

    VAR_13 = VAR_1;

    VAR_1 += VAR_5*16;



    

    init_get_bits(&gb, VAR_1, (VAR_2-VAR_1)<<3);

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

        int VAR_14[4];

        for(VAR_9=0; VAR_9<4; VAR_9++)

            VAR_14[VAR_9] = get_bits(&gb, 8);

        for(VAR_9=0; VAR_9<16; VAR_9++)

            VAR_0->block_codebook[VAR_8][15-VAR_9] = VAR_14[get_bits(&gb, 2)];

    }



    if (get_bits_left(&gb) < VAR_7 *

        (VAR_0->avctx->height/4) * (VAR_0->avctx->width/4))

        return -1;



    

    for(VAR_11=0; VAR_11<VAR_0->avctx->height/4; VAR_11++)

    for(VAR_10=0; VAR_10<VAR_0->avctx->width/4; VAR_10++) {

        unsigned int vector = get_bits(&gb, VAR_7);

        const unsigned char *src;

        int src_stride;



        if (vector < VAR_4) {

            src = VAR_0->last_frame.data[0] +

                  (VAR_11*4 + VAR_0->mv_codebook[vector][1])*VAR_0->last_frame.linesize[0] +

                   VAR_10*4 + VAR_0->mv_codebook[vector][0];

            src_stride = VAR_0->last_frame.linesize[0];

            if (src+3*src_stride+3>=VAR_3)

                continue;

        }else{

            int offset = vector - VAR_4;

            if (offset<VAR_5)

                src = VAR_13 + 16*offset;

            else if (offset-VAR_5<VAR_6)

                src = VAR_0->block_codebook[offset-VAR_5];

            else

                continue;

            src_stride = 4;

        }



        for(VAR_9=0; VAR_9<4; VAR_9++)

        for(VAR_8=0; VAR_8<4; VAR_8++)

            VAR_0->frame.data[0][ (VAR_11*4+VAR_9)*VAR_0->frame.linesize[0] + (VAR_10*4+VAR_8)  ] =

               src[VAR_9*src_stride + VAR_8];

    }



    return 0;

}

static int FUNC_0(TgvContext * VAR_0, const uint8_t *VAR_1, const uint8_t *VAR_2){ unsigned char *VAR_3 = VAR_0->last_frame.data[0] + VAR_0->avctx->width*VAR_0->last_frame.linesize[0]; int VAR_4; int VAR_5; int VAR_6; int VAR_7; int VAR_8,VAR_9,VAR_10,VAR_11; GetBitContext gb; int VAR_12; const unsigned char *VAR_13; if(VAR_1+12>VAR_2)
return -1; VAR_4           = AV_RL16(&VAR_1[0]); VAR_5    = AV_RL16(&VAR_1[2]); VAR_6 = AV_RL16(&VAR_1[4]); VAR_7       = AV_RL16(&VAR_1[6]); VAR_1 += 12; if (VAR_4 > VAR_0->VAR_4) { VAR_0->mv_codebook = av_realloc(VAR_0->mv_codebook, VAR_4*2*sizeof(int)); VAR_0->VAR_4 = VAR_4; } if (VAR_6 > VAR_0->VAR_6) { VAR_0->block_codebook = av_realloc(VAR_0->block_codebook, VAR_6*16*sizeof(unsigned char)); VAR_0->VAR_6 = VAR_6; } VAR_12 = (VAR_4*2*10+31) & ~31; if (VAR_1+(VAR_12>>3)+16*VAR_5+8*VAR_6>VAR_2)
return -1; init_get_bits(&gb, VAR_1, VAR_12); for (VAR_8=0; VAR_8<VAR_4; VAR_8++) { VAR_0->mv_codebook[VAR_8][0] = get_sbits(&gb, 10); VAR_0->mv_codebook[VAR_8][1] = get_sbits(&gb, 10); } VAR_1 += VAR_12>>3; VAR_13 = VAR_1; VAR_1 += VAR_5*16; init_get_bits(&gb, VAR_1, (VAR_2-VAR_1)<<3); for (VAR_8=0; VAR_8<VAR_6; VAR_8++) { int VAR_14[4]; for(VAR_9=0; VAR_9<4; VAR_9++) VAR_14[VAR_9] = get_bits(&gb, 8); for(VAR_9=0; VAR_9<16; VAR_9++) VAR_0->block_codebook[VAR_8][15-VAR_9] = VAR_14[get_bits(&gb, 2)]; } if (get_bits_left(&gb) < VAR_7 *
(VAR_0->avctx->height/4) * (VAR_0->avctx->width/4))
return -1; for(VAR_11=0; VAR_11<VAR_0->avctx->height/4; VAR_11++) for(VAR_10=0; VAR_10<VAR_0->avctx->width/4; VAR_10++) { unsigned int vector = get_bits(&gb, VAR_7); const unsigned char *src; int src_stride; if (vector < VAR_4) { src = VAR_0->last_frame.data[0] +
(VAR_11*4 + VAR_0->mv_codebook[vector][1])*VAR_0->last_frame.linesize[0] +
VAR_10*4 + VAR_0->mv_codebook[vector][0]; src_stride = VAR_0->last_frame.linesize[0]; if (src+3*src_stride+3>=VAR_3)
continue; }else{ int offset = vector - VAR_4; if (offset<VAR_5)
src = VAR_13 + 16*offset; else if (offset-VAR_5<VAR_6)
src = VAR_0->block_codebook[offset-VAR_5]; else
continue; src_stride = 4; } for(VAR_9=0; VAR_9<4; VAR_9++) for(VAR_8=0; VAR_8<4; VAR_8++) VAR_0->frame.data[0][ (VAR_11*4+VAR_9)*VAR_0->frame.linesize[0] + (VAR_10*4+VAR_8)  ] =
src[VAR_9*src_stride + VAR_8]; } return 0; }