static int mss4_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,

                             AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    MSS4Context *c = avctx->priv_data;

    GetBitContext gb;

    GetByteContext bc;

    uint8_t *dst[3];

    int width, height, quality, frame_type;

    int x, y, i, mb_width, mb_height, blk_type;

    int ret;



    if (buf_size < HEADER_SIZE) {

        av_log(avctx, AV_LOG_ERROR,

               "Frame should have at least %d bytes, got %d instead\n",

               HEADER_SIZE, buf_size);

        return AVERROR_INVALIDDATA;

    }



    bytestream2_init(&bc, buf, buf_size);

    width      = bytestream2_get_be16(&bc);

    height     = bytestream2_get_be16(&bc);

    bytestream2_skip(&bc, 2);

    quality    = bytestream2_get_byte(&bc);

    frame_type = bytestream2_get_byte(&bc);



    if (width > avctx->width ||

        height != avctx->height) {

        av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\n",

               width, height);

        return AVERROR_INVALIDDATA;

    }

    if (quality < 1 || quality > 100) {

        av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);

        return AVERROR_INVALIDDATA;

    }

    if ((frame_type & ~3) || frame_type == 3) {

        av_log(avctx, AV_LOG_ERROR, "Invalid frame type %d\n", frame_type);

        return AVERROR_INVALIDDATA;

    }



    if (frame_type != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) {

        av_log(avctx, AV_LOG_ERROR,

               "Empty frame found but it is not a skip frame.\n");

        return AVERROR_INVALIDDATA;

    }



    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)

        return ret;

    c->pic->key_frame = (frame_type == INTRA_FRAME);

    c->pic->pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I

                                                   : AV_PICTURE_TYPE_P;

    if (frame_type == SKIP_FRAME) {

        *got_frame      = 1;

        if ((ret = av_frame_ref(data, c->pic)) < 0)

            return ret;



        return buf_size;

    }



    if (c->quality != quality) {

        c->quality = quality;

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

            ff_mss34_gen_quant_mat(c->quant_mat[i], quality, !i);

    }



    init_get_bits8(&gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE));



    mb_width  = FFALIGN(width,  16) >> 4;

    mb_height = FFALIGN(height, 16) >> 4;

    dst[0] = c->pic->data[0];

    dst[1] = c->pic->data[1];

    dst[2] = c->pic->data[2];



    memset(c->prev_vec, 0, sizeof(c->prev_vec));

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

        memset(c->dc_cache, 0, sizeof(c->dc_cache));

        for (x = 0; x < mb_width; x++) {

            blk_type = decode012(&gb);

            switch (blk_type) {

            case DCT_BLOCK:

                if (mss4_decode_dct_block(c, &gb, dst, x, y) < 0) {

                    av_log(avctx, AV_LOG_ERROR,

                           "Error decoding DCT block %d,%d\n",

                           x, y);

                    return AVERROR_INVALIDDATA;

                }

                break;

            case IMAGE_BLOCK:

                if (mss4_decode_image_block(c, &gb, dst, x, y) < 0) {

                    av_log(avctx, AV_LOG_ERROR,

                           "Error decoding VQ block %d,%d\n",

                           x, y);

                    return AVERROR_INVALIDDATA;

                }

                break;

            case SKIP_BLOCK:

                if (frame_type == INTRA_FRAME) {

                    av_log(avctx, AV_LOG_ERROR, "Skip block in intra frame\n");

                    return AVERROR_INVALIDDATA;

                }

                break;

            }

            if (blk_type != DCT_BLOCK)

                mss4_update_dc_cache(c, x);

        }

        dst[0] += c->pic->linesize[0] * 16;

        dst[1] += c->pic->linesize[1] * 16;

        dst[2] += c->pic->linesize[2] * 16;

    }



    if ((ret = av_frame_ref(data, c->pic)) < 0)

        return ret;



    *got_frame      = 1;



    return buf_size;

}

static int mss4_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,

                             AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    MSS4Context *c = avctx->priv_data;

    GetBitContext gb;

    GetByteContext bc;

    uint8_t *dst[3];

    int width, height, quality, frame_type;

    int x, y, i, mb_width, mb_height, blk_type;

    int ret;



    if (buf_size < HEADER_SIZE) {

        av_log(avctx, AV_LOG_ERROR,

               "Frame should have at least %d bytes, got %d instead\n",

               HEADER_SIZE, buf_size);

        return AVERROR_INVALIDDATA;

    }



    bytestream2_init(&bc, buf, buf_size);

    width      = bytestream2_get_be16(&bc);

    height     = bytestream2_get_be16(&bc);

    bytestream2_skip(&bc, 2);

    quality    = bytestream2_get_byte(&bc);

    frame_type = bytestream2_get_byte(&bc);



    if (width > avctx->width ||

        height != avctx->height) {

        av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\n",

               width, height);

        return AVERROR_INVALIDDATA;

    }

    if (quality < 1 || quality > 100) {

        av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);

        return AVERROR_INVALIDDATA;

    }

    if ((frame_type & ~3) || frame_type == 3) {

        av_log(avctx, AV_LOG_ERROR, "Invalid frame type %d\n", frame_type);

        return AVERROR_INVALIDDATA;

    }



    if (frame_type != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) {

        av_log(avctx, AV_LOG_ERROR,

               "Empty frame found but it is not a skip frame.\n");

        return AVERROR_INVALIDDATA;

    }



    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)

        return ret;

    c->pic->key_frame = (frame_type == INTRA_FRAME);

    c->pic->pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I

                                                   : AV_PICTURE_TYPE_P;

    if (frame_type == SKIP_FRAME) {

        *got_frame      = 1;

        if ((ret = av_frame_ref(data, c->pic)) < 0)

            return ret;



        return buf_size;

    }



    if (c->quality != quality) {

        c->quality = quality;

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

            ff_mss34_gen_quant_mat(c->quant_mat[i], quality, !i);

    }



    init_get_bits8(&gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE));



    mb_width  = FFALIGN(width,  16) >> 4;

    mb_height = FFALIGN(height, 16) >> 4;

    dst[0] = c->pic->data[0];

    dst[1] = c->pic->data[1];

    dst[2] = c->pic->data[2];



    memset(c->prev_vec, 0, sizeof(c->prev_vec));

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

        memset(c->dc_cache, 0, sizeof(c->dc_cache));

        for (x = 0; x < mb_width; x++) {

            blk_type = decode012(&gb);

            switch (blk_type) {

            case DCT_BLOCK:

                if (mss4_decode_dct_block(c, &gb, dst, x, y) < 0) {

                    av_log(avctx, AV_LOG_ERROR,

                           "Error decoding DCT block %d,%d\n",

                           x, y);

                    return AVERROR_INVALIDDATA;

                }

                break;

            case IMAGE_BLOCK:

                if (mss4_decode_image_block(c, &gb, dst, x, y) < 0) {

                    av_log(avctx, AV_LOG_ERROR,

                           "Error decoding VQ block %d,%d\n",

                           x, y);

                    return AVERROR_INVALIDDATA;

                }

                break;

            case SKIP_BLOCK:

                if (frame_type == INTRA_FRAME) {

                    av_log(avctx, AV_LOG_ERROR, "Skip block in intra frame\n");

                    return AVERROR_INVALIDDATA;

                }

                break;

            }

            if (blk_type != DCT_BLOCK)

                mss4_update_dc_cache(c, x);

        }

        dst[0] += c->pic->linesize[0] * 16;

        dst[1] += c->pic->linesize[1] * 16;

        dst[2] += c->pic->linesize[2] * 16;

    }



    if ((ret = av_frame_ref(data, c->pic)) < 0)

        return ret;



    *got_frame      = 1;



    return buf_size;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2,

                             AVPacket *VAR_3)

{

    const uint8_t *VAR_4 = VAR_3->VAR_1;

    int VAR_5 = VAR_3->size;

    MSS4Context *c = VAR_0->priv_data;

    GetBitContext gb;

    GetByteContext bc;

    uint8_t *dst[3];

    int VAR_6, VAR_7, VAR_8, VAR_9;

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

    int VAR_16;



    if (VAR_5 < HEADER_SIZE) {

        av_log(VAR_0, AV_LOG_ERROR,

               "Frame should have at least %d bytes, got %d instead\n",

               HEADER_SIZE, VAR_5);

        return AVERROR_INVALIDDATA;

    }



    bytestream2_init(&bc, VAR_4, VAR_5);

    VAR_6      = bytestream2_get_be16(&bc);

    VAR_7     = bytestream2_get_be16(&bc);

    bytestream2_skip(&bc, 2);

    VAR_8    = bytestream2_get_byte(&bc);

    VAR_9 = bytestream2_get_byte(&bc);



    if (VAR_6 > VAR_0->VAR_6 ||

        VAR_7 != VAR_0->VAR_7) {

        av_log(VAR_0, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\n",

               VAR_6, VAR_7);

        return AVERROR_INVALIDDATA;

    }

    if (VAR_8 < 1 || VAR_8 > 100) {

        av_log(VAR_0, AV_LOG_ERROR, "Invalid VAR_8 setting %d\n", VAR_8);

        return AVERROR_INVALIDDATA;

    }

    if ((VAR_9 & ~3) || VAR_9 == 3) {

        av_log(VAR_0, AV_LOG_ERROR, "Invalid frame type %d\n", VAR_9);

        return AVERROR_INVALIDDATA;

    }



    if (VAR_9 != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) {

        av_log(VAR_0, AV_LOG_ERROR,

               "Empty frame found but it is not a skip frame.\n");

        return AVERROR_INVALIDDATA;

    }



    if ((VAR_16 = ff_reget_buffer(VAR_0, c->pic)) < 0)

        return VAR_16;

    c->pic->key_frame = (VAR_9 == INTRA_FRAME);

    c->pic->pict_type = (VAR_9 == INTRA_FRAME) ? AV_PICTURE_TYPE_I

                                                   : AV_PICTURE_TYPE_P;

    if (VAR_9 == SKIP_FRAME) {

        *VAR_2      = 1;

        if ((VAR_16 = av_frame_ref(VAR_1, c->pic)) < 0)

            return VAR_16;



        return VAR_5;

    }



    if (c->VAR_8 != VAR_8) {

        c->VAR_8 = VAR_8;

        for (VAR_12 = 0; VAR_12 < 2; VAR_12++)

            ff_mss34_gen_quant_mat(c->quant_mat[VAR_12], VAR_8, !VAR_12);

    }



    init_get_bits8(&gb, VAR_4 + HEADER_SIZE, (VAR_5 - HEADER_SIZE));



    VAR_13  = FFALIGN(VAR_6,  16) >> 4;

    VAR_14 = FFALIGN(VAR_7, 16) >> 4;

    dst[0] = c->pic->VAR_1[0];

    dst[1] = c->pic->VAR_1[1];

    dst[2] = c->pic->VAR_1[2];



    memset(c->prev_vec, 0, sizeof(c->prev_vec));

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

        memset(c->dc_cache, 0, sizeof(c->dc_cache));

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

            VAR_15 = decode012(&gb);

            switch (VAR_15) {

            case DCT_BLOCK:

                if (mss4_decode_dct_block(c, &gb, dst, VAR_10, VAR_11) < 0) {

                    av_log(VAR_0, AV_LOG_ERROR,

                           "Error decoding DCT block %d,%d\n",

                           VAR_10, VAR_11);

                    return AVERROR_INVALIDDATA;

                }

                break;

            case IMAGE_BLOCK:

                if (mss4_decode_image_block(c, &gb, dst, VAR_10, VAR_11) < 0) {

                    av_log(VAR_0, AV_LOG_ERROR,

                           "Error decoding VQ block %d,%d\n",

                           VAR_10, VAR_11);

                    return AVERROR_INVALIDDATA;

                }

                break;

            case SKIP_BLOCK:

                if (VAR_9 == INTRA_FRAME) {

                    av_log(VAR_0, AV_LOG_ERROR, "Skip block in intra frame\n");

                    return AVERROR_INVALIDDATA;

                }

                break;

            }

            if (VAR_15 != DCT_BLOCK)

                mss4_update_dc_cache(c, VAR_10);

        }

        dst[0] += c->pic->linesize[0] * 16;

        dst[1] += c->pic->linesize[1] * 16;

        dst[2] += c->pic->linesize[2] * 16;

    }



    if ((VAR_16 = av_frame_ref(VAR_1, c->pic)) < 0)

        return VAR_16;



    *VAR_2      = 1;



    return VAR_5;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2,
AVPacket *VAR_3)
{ const uint8_t *VAR_4 = VAR_3->VAR_1; int VAR_5 = VAR_3->size; MSS4Context *c = VAR_0->priv_data; GetBitContext gb; GetByteContext bc; uint8_t *dst[3]; int VAR_6, VAR_7, VAR_8, VAR_9; int VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15; int VAR_16; if (VAR_5 < HEADER_SIZE) { av_log(VAR_0, AV_LOG_ERROR,
"Frame should have at least %d bytes, got %d instead\n",
HEADER_SIZE, VAR_5); return AVERROR_INVALIDDATA; } bytestream2_init(&bc, VAR_4, VAR_5); VAR_6      = bytestream2_get_be16(&bc); VAR_7     = bytestream2_get_be16(&bc); bytestream2_skip(&bc, 2); VAR_8    = bytestream2_get_byte(&bc); VAR_9 = bytestream2_get_byte(&bc); if (VAR_6 > VAR_0->VAR_6 ||
VAR_7 != VAR_0->VAR_7) { av_log(VAR_0, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\n",
VAR_6, VAR_7); return AVERROR_INVALIDDATA; } if (VAR_8 < 1 || VAR_8 > 100) { av_log(VAR_0, AV_LOG_ERROR, "Invalid VAR_8 setting %d\n", VAR_8); return AVERROR_INVALIDDATA; } if ((VAR_9 & ~3) || VAR_9 == 3) { av_log(VAR_0, AV_LOG_ERROR, "Invalid frame type %d\n", VAR_9); return AVERROR_INVALIDDATA; } if (VAR_9 != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) { av_log(VAR_0, AV_LOG_ERROR,
"Empty frame found but it is not a skip frame.\n"); return AVERROR_INVALIDDATA; } if ((VAR_16 = ff_reget_buffer(VAR_0, c->pic)) < 0)
return VAR_16; c->pic->key_frame = (VAR_9 == INTRA_FRAME); c->pic->pict_type = (VAR_9 == INTRA_FRAME) ? AV_PICTURE_TYPE_I
: AV_PICTURE_TYPE_P; if (VAR_9 == SKIP_FRAME) { *VAR_2      = 1; if ((VAR_16 = av_frame_ref(VAR_1, c->pic)) < 0)
return VAR_16; return VAR_5; } if (c->VAR_8 != VAR_8) { c->VAR_8 = VAR_8; for (VAR_12 = 0; VAR_12 < 2; VAR_12++) ff_mss34_gen_quant_mat(c->quant_mat[VAR_12], VAR_8, !VAR_12); } init_get_bits8(&gb, VAR_4 + HEADER_SIZE, (VAR_5 - HEADER_SIZE)); VAR_13  = FFALIGN(VAR_6,  16) >> 4; VAR_14 = FFALIGN(VAR_7, 16) >> 4; dst[0] = c->pic->VAR_1[0]; dst[1] = c->pic->VAR_1[1]; dst[2] = c->pic->VAR_1[2]; memset(c->prev_vec, 0, sizeof(c->prev_vec)); for (VAR_11 = 0; VAR_11 < VAR_14; VAR_11++) { memset(c->dc_cache, 0, sizeof(c->dc_cache)); for (VAR_10 = 0; VAR_10 < VAR_13; VAR_10++) { VAR_15 = decode012(&gb); switch (VAR_15) { case DCT_BLOCK:
if (mss4_decode_dct_block(c, &gb, dst, VAR_10, VAR_11) < 0) { av_log(VAR_0, AV_LOG_ERROR,
"Error decoding DCT block %d,%d\n",
VAR_10, VAR_11); return AVERROR_INVALIDDATA; } break; case IMAGE_BLOCK:
if (mss4_decode_image_block(c, &gb, dst, VAR_10, VAR_11) < 0) { av_log(VAR_0, AV_LOG_ERROR,
"Error decoding VQ block %d,%d\n",
VAR_10, VAR_11); return AVERROR_INVALIDDATA; } break; case SKIP_BLOCK:
if (VAR_9 == INTRA_FRAME) { av_log(VAR_0, AV_LOG_ERROR, "Skip block in intra frame\n"); return AVERROR_INVALIDDATA; } break; } if (VAR_15 != DCT_BLOCK)
mss4_update_dc_cache(c, VAR_10); } dst[0] += c->pic->linesize[0] * 16; dst[1] += c->pic->linesize[1] * 16; dst[2] += c->pic->linesize[2] * 16; } if ((VAR_16 = av_frame_ref(VAR_1, c->pic)) < 0)
return VAR_16; *VAR_2      = 1; return VAR_5; }