static int tgv_decode_inter(TgvContext *s, AVFrame *frame,

                            const uint8_t *buf, const uint8_t *buf_end)

{

    int num_mvs;

    int num_blocks_raw;

    int num_blocks_packed;

    int vector_bits;

    int i,j,x,y;

    GetBitContext gb;

    int mvbits;

    const uint8_t *blocks_raw;



    if(buf_end - buf < 12)

        return AVERROR_INVALIDDATA;



    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 (vector_bits > MIN_CACHE_BITS || !vector_bits) {

        av_log(s->avctx, AV_LOG_ERROR,

               "Invalid value for motion vector bits: %d\n", vector_bits);

        return AVERROR_INVALIDDATA;

    }



    /* allocate codebook buffers as necessary */

    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);

        s->num_blocks_packed = num_blocks_packed;

    }



    /* read motion vectors */

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



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

        return AVERROR_INVALIDDATA;



    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 AVERROR_INVALIDDATA;



    /* 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 uint8_t *src;

            int src_stride;



            if (vector < num_mvs) {

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

                int my = y * 4 + s->mv_codebook[vector][1];



                if (mx < 0 || mx + 4 > s->avctx->width ||

                    my < 0 || my + 4 > s->avctx->height) {

                    av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);

                    continue;

                }



                src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];

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

            } 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++)

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

                        src[j * src_stride + i];

    }



    return 0;

}

static int tgv_decode_inter(TgvContext *s, AVFrame *frame,

                            const uint8_t *buf, const uint8_t *buf_end)

{

    int num_mvs;

    int num_blocks_raw;

    int num_blocks_packed;

    int vector_bits;

    int i,j,x,y;

    GetBitContext gb;

    int mvbits;

    const uint8_t *blocks_raw;



    if(buf_end - buf < 12)

        return AVERROR_INVALIDDATA;



    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 (vector_bits > MIN_CACHE_BITS || !vector_bits) {

        av_log(s->avctx, AV_LOG_ERROR,

               "Invalid value for motion vector bits: %d\n", vector_bits);

        return AVERROR_INVALIDDATA;

    }



    

    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);

        s->num_blocks_packed = num_blocks_packed;

    }



    

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



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

        return AVERROR_INVALIDDATA;



    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 AVERROR_INVALIDDATA;



    

    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 uint8_t *src;

            int src_stride;



            if (vector < num_mvs) {

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

                int my = y * 4 + s->mv_codebook[vector][1];



                if (mx < 0 || mx + 4 > s->avctx->width ||

                    my < 0 || my + 4 > s->avctx->height) {

                    av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);

                    continue;

                }



                src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];

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

            } 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++)

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

                        src[j * src_stride + i];

    }



    return 0;

}

static int FUNC_0(TgvContext *VAR_0, AVFrame *VAR_1,

                            const uint8_t *VAR_2, const uint8_t *VAR_3)

{

    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 uint8_t *VAR_13;



    if(VAR_3 - VAR_2 < 12)

        return AVERROR_INVALIDDATA;



    VAR_4           = AV_RL16(&VAR_2[0]);

    VAR_5    = AV_RL16(&VAR_2[2]);

    VAR_6 = AV_RL16(&VAR_2[4]);

    VAR_7       = AV_RL16(&VAR_2[6]);

    VAR_2 += 12;



    if (VAR_7 > MIN_CACHE_BITS || !VAR_7) {

        av_log(VAR_0->avctx, AV_LOG_ERROR,

               "Invalid value for motion vector bits: %d\n", VAR_7);

        return AVERROR_INVALIDDATA;

    }



    

    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);

        VAR_0->VAR_6 = VAR_6;

    }



    

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



    if (VAR_3 - VAR_2 < (VAR_12>>3) + 16*VAR_5 + 8*VAR_6)

        return AVERROR_INVALIDDATA;



    init_get_bits(&gb, VAR_2, 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_2 += VAR_12 >> 3;



    

    VAR_13 = VAR_2;

    VAR_2       += VAR_5 * 16;



    

    init_get_bits(&gb, VAR_2, (VAR_3 - VAR_2) << 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 AVERROR_INVALIDDATA;



    

    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 uint8_t *src;

            int src_stride;



            if (vector < VAR_4) {

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

                int my = VAR_11 * 4 + VAR_0->mv_codebook[vector][1];



                if (mx < 0 || mx + 4 > VAR_0->avctx->width ||

                    my < 0 || my + 4 > VAR_0->avctx->height) {

                    av_log(VAR_0->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);

                    continue;

                }



                src = VAR_0->last_frame.data[0] + mx + my * VAR_0->last_frame.linesize[0];

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

            } 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_1->data[0][(VAR_11 * 4 + VAR_9) * VAR_1->linesize[0] + (VAR_10 * 4 + VAR_8)] =

                        src[VAR_9 * src_stride + VAR_8];

    }



    return 0;

}

static int FUNC_0(TgvContext *VAR_0, AVFrame *VAR_1,
const uint8_t *VAR_2, const uint8_t *VAR_3)
{ 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 uint8_t *VAR_13; if(VAR_3 - VAR_2 < 12)
return AVERROR_INVALIDDATA; VAR_4           = AV_RL16(&VAR_2[0]); VAR_5    = AV_RL16(&VAR_2[2]); VAR_6 = AV_RL16(&VAR_2[4]); VAR_7       = AV_RL16(&VAR_2[6]); VAR_2 += 12; if (VAR_7 > MIN_CACHE_BITS || !VAR_7) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Invalid value for motion vector bits: %d\n", VAR_7); return AVERROR_INVALIDDATA; } 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); VAR_0->VAR_6 = VAR_6; } VAR_12 = (VAR_4 * 2 * 10 + 31) & ~31; if (VAR_3 - VAR_2 < (VAR_12>>3) + 16*VAR_5 + 8*VAR_6)
return AVERROR_INVALIDDATA; init_get_bits(&gb, VAR_2, 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_2 += VAR_12 >> 3; VAR_13 = VAR_2; VAR_2       += VAR_5 * 16; init_get_bits(&gb, VAR_2, (VAR_3 - VAR_2) << 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 AVERROR_INVALIDDATA; 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 uint8_t *src; int src_stride; if (vector < VAR_4) { int mx = VAR_10 * 4 + VAR_0->mv_codebook[vector][0]; int my = VAR_11 * 4 + VAR_0->mv_codebook[vector][1]; if (mx < 0 || mx + 4 > VAR_0->avctx->width ||
my < 0 || my + 4 > VAR_0->avctx->height) { av_log(VAR_0->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my); continue; } src = VAR_0->last_frame.data[0] + mx + my * VAR_0->last_frame.linesize[0]; src_stride = VAR_0->last_frame.linesize[0]; } 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_1->data[0][(VAR_11 * 4 + VAR_9) * VAR_1->linesize[0] + (VAR_10 * 4 + VAR_8)] =
src[VAR_9 * src_stride + VAR_8]; } return 0; }