avs_decode_frame(AVCodecContext * avctx,

                 void *data, int *got_frame, AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    const uint8_t *buf_end = avpkt->data + avpkt->size;

    int buf_size = avpkt->size;

    AvsContext *const avs = avctx->priv_data;

    AVFrame *picture = data;

    AVFrame *const p =  &avs->picture;

    const uint8_t *table, *vect;

    uint8_t *out;

    int i, j, x, y, stride, vect_w = 3, vect_h = 3;

    AvsVideoSubType sub_type;

    AvsBlockType type;

    GetBitContext change_map;



    if (avctx->reget_buffer(avctx, p)) {

        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");

        return -1;

    }

    p->reference = 3;

    p->pict_type = AV_PICTURE_TYPE_P;

    p->key_frame = 0;



    out = avs->picture.data[0];

    stride = avs->picture.linesize[0];



    if (buf_end - buf < 4)

        return AVERROR_INVALIDDATA;

    sub_type = buf[0];

    type = buf[1];

    buf += 4;



    if (type == AVS_PALETTE) {

        int first, last;

        uint32_t *pal = (uint32_t *) avs->picture.data[1];



        first = AV_RL16(buf);

        last = first + AV_RL16(buf + 2);

        if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))

            return AVERROR_INVALIDDATA;

        buf += 4;

        for (i=first; i<last; i++, buf+=3) {

            pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);

            pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;

        }



        sub_type = buf[0];

        type = buf[1];

        buf += 4;

    }



    if (type != AVS_VIDEO)

        return -1;



    switch (sub_type) {

    case AVS_I_FRAME:

        p->pict_type = AV_PICTURE_TYPE_I;

        p->key_frame = 1;

    case AVS_P_FRAME_3X3:

        vect_w = 3;

        vect_h = 3;

        break;



    case AVS_P_FRAME_2X2:

        vect_w = 2;

        vect_h = 2;

        break;



    case AVS_P_FRAME_2X3:

        vect_w = 2;

        vect_h = 3;

        break;



    default:

      return -1;

    }



    if (buf_end - buf < 256 * vect_w * vect_h)

        return AVERROR_INVALIDDATA;

    table = buf + (256 * vect_w * vect_h);

    if (sub_type != AVS_I_FRAME) {

        int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);

        if (buf_end - table < map_size)

            return AVERROR_INVALIDDATA;

        init_get_bits(&change_map, table, map_size * 8);

        table += map_size;

    }



    for (y=0; y<198; y+=vect_h) {

        for (x=0; x<318; x+=vect_w) {

            if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {

                if (buf_end - table < 1)

                    return AVERROR_INVALIDDATA;

                vect = &buf[*table++ * (vect_w * vect_h)];

                for (j=0; j<vect_w; j++) {

                    out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];

                    out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];

                    if (vect_h == 3)

                        out[(y + 2) * stride + x + j] =

                            vect[(2 * vect_w) + j];

                }

            }

        }

        if (sub_type != AVS_I_FRAME)

            align_get_bits(&change_map);

    }



    *picture   = avs->picture;

    *got_frame = 1;



    return buf_size;

}

avs_decode_frame(AVCodecContext * avctx,

                 void *data, int *got_frame, AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    const uint8_t *buf_end = avpkt->data + avpkt->size;

    int buf_size = avpkt->size;

    AvsContext *const avs = avctx->priv_data;

    AVFrame *picture = data;

    AVFrame *const p =  &avs->picture;

    const uint8_t *table, *vect;

    uint8_t *out;

    int i, j, x, y, stride, vect_w = 3, vect_h = 3;

    AvsVideoSubType sub_type;

    AvsBlockType type;

    GetBitContext change_map;



    if (avctx->reget_buffer(avctx, p)) {

        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");

        return -1;

    }

    p->reference = 3;

    p->pict_type = AV_PICTURE_TYPE_P;

    p->key_frame = 0;



    out = avs->picture.data[0];

    stride = avs->picture.linesize[0];



    if (buf_end - buf < 4)

        return AVERROR_INVALIDDATA;

    sub_type = buf[0];

    type = buf[1];

    buf += 4;



    if (type == AVS_PALETTE) {

        int first, last;

        uint32_t *pal = (uint32_t *) avs->picture.data[1];



        first = AV_RL16(buf);

        last = first + AV_RL16(buf + 2);

        if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))

            return AVERROR_INVALIDDATA;

        buf += 4;

        for (i=first; i<last; i++, buf+=3) {

            pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);

            pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;

        }



        sub_type = buf[0];

        type = buf[1];

        buf += 4;

    }



    if (type != AVS_VIDEO)

        return -1;



    switch (sub_type) {

    case AVS_I_FRAME:

        p->pict_type = AV_PICTURE_TYPE_I;

        p->key_frame = 1;

    case AVS_P_FRAME_3X3:

        vect_w = 3;

        vect_h = 3;

        break;



    case AVS_P_FRAME_2X2:

        vect_w = 2;

        vect_h = 2;

        break;



    case AVS_P_FRAME_2X3:

        vect_w = 2;

        vect_h = 3;

        break;



    default:

      return -1;

    }



    if (buf_end - buf < 256 * vect_w * vect_h)

        return AVERROR_INVALIDDATA;

    table = buf + (256 * vect_w * vect_h);

    if (sub_type != AVS_I_FRAME) {

        int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);

        if (buf_end - table < map_size)

            return AVERROR_INVALIDDATA;

        init_get_bits(&change_map, table, map_size * 8);

        table += map_size;

    }



    for (y=0; y<198; y+=vect_h) {

        for (x=0; x<318; x+=vect_w) {

            if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {

                if (buf_end - table < 1)

                    return AVERROR_INVALIDDATA;

                vect = &buf[*table++ * (vect_w * vect_h)];

                for (j=0; j<vect_w; j++) {

                    out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];

                    out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];

                    if (vect_h == 3)

                        out[(y + 2) * stride + x + j] =

                            vect[(2 * vect_w) + j];

                }

            }

        }

        if (sub_type != AVS_I_FRAME)

            align_get_bits(&change_map);

    }



    *picture   = avs->picture;

    *got_frame = 1;



    return buf_size;

}

    GetBitContext change_map;
FUNC_0(AVCodecContext * VAR_0,

                 void *VAR_1, int *VAR_2, AVPacket *VAR_3)

{

    const uint8_t *VAR_4 = VAR_3->VAR_1;

    const uint8_t *VAR_5 = VAR_3->VAR_1 + VAR_3->size;

    int VAR_6 = VAR_3->size;

    AvsContext *const avs = VAR_0->priv_data;

    AVFrame *picture = VAR_1;

    AVFrame *const p =  &avs->picture;

    const uint8_t *VAR_7, *vect;

    uint8_t *out;

    int VAR_8, VAR_9, VAR_10, VAR_11, VAR_12, VAR_13 = 3, VAR_14 = 3;

    AvsVideoSubType sub_type;

    AvsBlockType type;

    GetBitContext change_map;



    if (VAR_0->reget_buffer(VAR_0, p)) {

        av_log(VAR_0, AV_LOG_ERROR, "reget_buffer() failed\n");

        return -1;

    }

    p->reference = 3;

    p->pict_type = AV_PICTURE_TYPE_P;

    p->key_frame = 0;



    out = avs->picture.VAR_1[0];

    VAR_12 = avs->picture.linesize[0];



    if (VAR_5 - VAR_4 < 4)

        return AVERROR_INVALIDDATA;

    sub_type = VAR_4[0];

    type = VAR_4[1];

    VAR_4 += 4;



    if (type == AVS_PALETTE) {

        int VAR_15, VAR_16;

        uint32_t *pal = (uint32_t *) avs->picture.VAR_1[1];



        VAR_15 = AV_RL16(VAR_4);

        VAR_16 = VAR_15 + AV_RL16(VAR_4 + 2);

        if (VAR_15 >= 256 || VAR_16 > 256 || VAR_5 - VAR_4 < 4 + 4 + 3 * (VAR_16 - VAR_15))

            return AVERROR_INVALIDDATA;

        VAR_4 += 4;

        for (VAR_8=VAR_15; VAR_8<VAR_16; VAR_8++, VAR_4+=3) {

            pal[VAR_8] = (VAR_4[0] << 18) | (VAR_4[1] << 10) | (VAR_4[2] << 2);

            pal[VAR_8] |= 0xFFU << 24 | (pal[VAR_8] >> 6) & 0x30303;

        }



        sub_type = VAR_4[0];

        type = VAR_4[1];

        VAR_4 += 4;

    }



    if (type != AVS_VIDEO)

        return -1;



    switch (sub_type) {

    case AVS_I_FRAME:

        p->pict_type = AV_PICTURE_TYPE_I;

        p->key_frame = 1;

    case AVS_P_FRAME_3X3:

        VAR_13 = 3;

        VAR_14 = 3;

        break;



    case AVS_P_FRAME_2X2:

        VAR_13 = 2;

        VAR_14 = 2;

        break;



    case AVS_P_FRAME_2X3:

        VAR_13 = 2;

        VAR_14 = 3;

        break;



    default:

      return -1;

    }



    if (VAR_5 - VAR_4 < 256 * VAR_13 * VAR_14)

        return AVERROR_INVALIDDATA;

    VAR_7 = VAR_4 + (256 * VAR_13 * VAR_14);

    if (sub_type != AVS_I_FRAME) {

        int VAR_17 = ((318 / VAR_13 + 7) / 8) * (198 / VAR_14);

        if (VAR_5 - VAR_7 < VAR_17)

            return AVERROR_INVALIDDATA;

        init_get_bits(&change_map, VAR_7, VAR_17 * 8);

        VAR_7 += VAR_17;

    }



    for (VAR_11=0; VAR_11<198; VAR_11+=VAR_14) {

        for (VAR_10=0; VAR_10<318; VAR_10+=VAR_13) {

            if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {

                if (VAR_5 - VAR_7 < 1)

                    return AVERROR_INVALIDDATA;

                vect = &VAR_4[*VAR_7++ * (VAR_13 * VAR_14)];

                for (VAR_9=0; VAR_9<VAR_13; VAR_9++) {

                    out[(VAR_11 + 0) * VAR_12 + VAR_10 + VAR_9] = vect[(0 * VAR_13) + VAR_9];

                    out[(VAR_11 + 1) * VAR_12 + VAR_10 + VAR_9] = vect[(1 * VAR_13) + VAR_9];

                    if (VAR_14 == 3)

                        out[(VAR_11 + 2) * VAR_12 + VAR_10 + VAR_9] =

                            vect[(2 * VAR_13) + VAR_9];

                }

            }

        }

        if (sub_type != AVS_I_FRAME)

            align_get_bits(&change_map);

    }



    *picture   = avs->picture;

    *VAR_2 = 1;



    return VAR_6;

}

FUNC_0(AVCodecContext * VAR_0,
void *VAR_1, int *VAR_2, AVPacket *VAR_3)
{ const uint8_t *VAR_4 = VAR_3->VAR_1; const uint8_t *VAR_5 = VAR_3->VAR_1 + VAR_3->size; int VAR_6 = VAR_3->size; AvsContext *const avs = VAR_0->priv_data; AVFrame *picture = VAR_1; AVFrame *const p =  &avs->picture; const uint8_t *VAR_7, *vect; uint8_t *out; int VAR_8, VAR_9, VAR_10, VAR_11, VAR_12, VAR_13 = 3, VAR_14 = 3; AvsVideoSubType sub_type; AvsBlockType type; GetBitContext change_map; if (VAR_0->reget_buffer(VAR_0, p)) { av_log(VAR_0, AV_LOG_ERROR, "reget_buffer() failed\n"); return -1; } p->reference = 3; p->pict_type = AV_PICTURE_TYPE_P; p->key_frame = 0; out = avs->picture.VAR_1[0]; VAR_12 = avs->picture.linesize[0]; if (VAR_5 - VAR_4 < 4)
return AVERROR_INVALIDDATA; sub_type = VAR_4[0]; type = VAR_4[1]; VAR_4 += 4; if (type == AVS_PALETTE) { int VAR_15, VAR_16; uint32_t *pal = (uint32_t *) avs->picture.VAR_1[1]; VAR_15 = AV_RL16(VAR_4); VAR_16 = VAR_15 + AV_RL16(VAR_4 + 2); if (VAR_15 >= 256 || VAR_16 > 256 || VAR_5 - VAR_4 < 4 + 4 + 3 * (VAR_16 - VAR_15))
return AVERROR_INVALIDDATA; VAR_4 += 4; for (VAR_8=VAR_15; VAR_8<VAR_16; VAR_8++, VAR_4+=3) { pal[VAR_8] = (VAR_4[0] << 18) | (VAR_4[1] << 10) | (VAR_4[2] << 2); pal[VAR_8] |= 0xFFU << 24 | (pal[VAR_8] >> 6) & 0x30303; } sub_type = VAR_4[0]; type = VAR_4[1]; VAR_4 += 4; } if (type != AVS_VIDEO)
return -1; switch (sub_type) { case AVS_I_FRAME:
p->pict_type = AV_PICTURE_TYPE_I; p->key_frame = 1; case AVS_P_FRAME_3X3:
VAR_13 = 3; VAR_14 = 3; break; case AVS_P_FRAME_2X2:
VAR_13 = 2; VAR_14 = 2; break; case AVS_P_FRAME_2X3:
VAR_13 = 2; VAR_14 = 3; break; default:
return -1; } if (VAR_5 - VAR_4 < 256 * VAR_13 * VAR_14)
return AVERROR_INVALIDDATA; VAR_7 = VAR_4 + (256 * VAR_13 * VAR_14); if (sub_type != AVS_I_FRAME) { int VAR_17 = ((318 / VAR_13 + 7) / 8) * (198 / VAR_14); if (VAR_5 - VAR_7 < VAR_17)
return AVERROR_INVALIDDATA; init_get_bits(&change_map, VAR_7, VAR_17 * 8); VAR_7 += VAR_17; } for (VAR_11=0; VAR_11<198; VAR_11+=VAR_14) { for (VAR_10=0; VAR_10<318; VAR_10+=VAR_13) { if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) { if (VAR_5 - VAR_7 < 1)
return AVERROR_INVALIDDATA; vect = &VAR_4[*VAR_7++ * (VAR_13 * VAR_14)]; for (VAR_9=0; VAR_9<VAR_13; VAR_9++) { out[(VAR_11 + 0) * VAR_12 + VAR_10 + VAR_9] = vect[(0 * VAR_13) + VAR_9]; out[(VAR_11 + 1) * VAR_12 + VAR_10 + VAR_9] = vect[(1 * VAR_13) + VAR_9]; if (VAR_14 == 3)
out[(VAR_11 + 2) * VAR_12 + VAR_10 + VAR_9] =
vect[(2 * VAR_13) + VAR_9]; } } } if (sub_type != AVS_I_FRAME)
align_get_bits(&change_map); } *picture   = avs->picture; *VAR_2 = 1; return VAR_6; }