static int tscc2_decode_frame(AVCodecContext *avctx, void *data,

                              int *got_frame, AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    TSCC2Context *c = avctx->priv_data;

    GetByteContext gb;

    uint32_t frame_type, size;

    int i, val, len, pos = 0;

    int num_mb = c->mb_width * c->mb_height;

    int ret;



    bytestream2_init(&gb, buf, buf_size);

    frame_type = bytestream2_get_byte(&gb);

    if (frame_type > 1) {

        av_log(avctx, AV_LOG_ERROR, "Incorrect frame type %"PRIu32"\n",

               frame_type);

        return AVERROR_INVALIDDATA;

    }



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

        return ret;

    }



    if (frame_type == 0) {

        *got_frame      = 1;

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

            return ret;



        return buf_size;

    }



    if (bytestream2_get_bytes_left(&gb) < 4) {

        av_log(avctx, AV_LOG_ERROR, "Frame is too short\n");

        return AVERROR_INVALIDDATA;

    }



    c->quant[0] = bytestream2_get_byte(&gb);

    c->quant[1] = bytestream2_get_byte(&gb);

    if (c->quant[0] < 2 || c->quant[0] > NUM_VLC_SETS + 1 ||

        c->quant[1] < 2 || c->quant[1] > NUM_VLC_SETS + 1) {

        av_log(avctx, AV_LOG_ERROR, "Invalid quantisers %d / %d\n",

               c->quant[0], c->quant[1]);

        return AVERROR_INVALIDDATA;

    }



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

        c->q[0][i] = tscc2_quants[c->quant[0] - 2][i];

        c->q[1][i] = tscc2_quants[c->quant[1] - 2][i];

    }



    bytestream2_skip(&gb, 1);



    size = bytestream2_get_le32(&gb);

    if (size > bytestream2_get_bytes_left(&gb)) {

        av_log(avctx, AV_LOG_ERROR, "Slice properties chunk is too large\n");

        return AVERROR_INVALIDDATA;

    }



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

        val   = bytestream2_get_byte(&gb);

        len   = val & 0x3F;

        val >>= 6;

        if (pos + len > num_mb) {

            av_log(avctx, AV_LOG_ERROR, "Too many slice properties\n");

            return AVERROR_INVALIDDATA;

        }

        memset(c->slice_quants + pos, val, len);

        pos += len;

    }

    if (pos < num_mb) {

        av_log(avctx, AV_LOG_ERROR, "Too few slice properties (%d / %d)\n",

               pos, num_mb);

        return AVERROR_INVALIDDATA;

    }



    for (i = 0; i < c->mb_height; i++) {

        size = bytestream2_peek_byte(&gb);

        if (size & 1) {

            size = bytestream2_get_byte(&gb) - 1;

        } else {

            size = bytestream2_get_le32(&gb) >> 1;

        }

        if (!size) {

            int skip_row = 1, j, off = i * c->mb_width;

            for (j = 0; j < c->mb_width; j++) {

                if (c->slice_quants[off + j] == 1 ||

                    c->slice_quants[off + j] == 2) {

                    skip_row = 0;

                    break;

                }

            }

            if (!skip_row) {

                av_log(avctx, AV_LOG_ERROR, "Non-skip row with zero size\n");

                return AVERROR_INVALIDDATA;

            }

        }

        if (bytestream2_get_bytes_left(&gb) < size) {

            av_log(avctx, AV_LOG_ERROR, "Invalid slice size (%"PRIu32"/%u)\n",

                   size, bytestream2_get_bytes_left(&gb));

            return AVERROR_INVALIDDATA;

        }

        ret = tscc2_decode_slice(c, i, buf + bytestream2_tell(&gb), size);

        if (ret) {

            av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d\n", i);

            return ret;

        }

        bytestream2_skip(&gb, size);

    }



    *got_frame      = 1;

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

        return ret;



    /* always report that the buffer was completely consumed */

    return buf_size;

}

static int tscc2_decode_frame(AVCodecContext *avctx, void *data,

                              int *got_frame, AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    TSCC2Context *c = avctx->priv_data;

    GetByteContext gb;

    uint32_t frame_type, size;

    int i, val, len, pos = 0;

    int num_mb = c->mb_width * c->mb_height;

    int ret;



    bytestream2_init(&gb, buf, buf_size);

    frame_type = bytestream2_get_byte(&gb);

    if (frame_type > 1) {

        av_log(avctx, AV_LOG_ERROR, "Incorrect frame type %"PRIu32"\n",

               frame_type);

        return AVERROR_INVALIDDATA;

    }



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

        return ret;

    }



    if (frame_type == 0) {

        *got_frame      = 1;

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

            return ret;



        return buf_size;

    }



    if (bytestream2_get_bytes_left(&gb) < 4) {

        av_log(avctx, AV_LOG_ERROR, "Frame is too short\n");

        return AVERROR_INVALIDDATA;

    }



    c->quant[0] = bytestream2_get_byte(&gb);

    c->quant[1] = bytestream2_get_byte(&gb);

    if (c->quant[0] < 2 || c->quant[0] > NUM_VLC_SETS + 1 ||

        c->quant[1] < 2 || c->quant[1] > NUM_VLC_SETS + 1) {

        av_log(avctx, AV_LOG_ERROR, "Invalid quantisers %d / %d\n",

               c->quant[0], c->quant[1]);

        return AVERROR_INVALIDDATA;

    }



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

        c->q[0][i] = tscc2_quants[c->quant[0] - 2][i];

        c->q[1][i] = tscc2_quants[c->quant[1] - 2][i];

    }



    bytestream2_skip(&gb, 1);



    size = bytestream2_get_le32(&gb);

    if (size > bytestream2_get_bytes_left(&gb)) {

        av_log(avctx, AV_LOG_ERROR, "Slice properties chunk is too large\n");

        return AVERROR_INVALIDDATA;

    }



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

        val   = bytestream2_get_byte(&gb);

        len   = val & 0x3F;

        val >>= 6;

        if (pos + len > num_mb) {

            av_log(avctx, AV_LOG_ERROR, "Too many slice properties\n");

            return AVERROR_INVALIDDATA;

        }

        memset(c->slice_quants + pos, val, len);

        pos += len;

    }

    if (pos < num_mb) {

        av_log(avctx, AV_LOG_ERROR, "Too few slice properties (%d / %d)\n",

               pos, num_mb);

        return AVERROR_INVALIDDATA;

    }



    for (i = 0; i < c->mb_height; i++) {

        size = bytestream2_peek_byte(&gb);

        if (size & 1) {

            size = bytestream2_get_byte(&gb) - 1;

        } else {

            size = bytestream2_get_le32(&gb) >> 1;

        }

        if (!size) {

            int skip_row = 1, j, off = i * c->mb_width;

            for (j = 0; j < c->mb_width; j++) {

                if (c->slice_quants[off + j] == 1 ||

                    c->slice_quants[off + j] == 2) {

                    skip_row = 0;

                    break;

                }

            }

            if (!skip_row) {

                av_log(avctx, AV_LOG_ERROR, "Non-skip row with zero size\n");

                return AVERROR_INVALIDDATA;

            }

        }

        if (bytestream2_get_bytes_left(&gb) < size) {

            av_log(avctx, AV_LOG_ERROR, "Invalid slice size (%"PRIu32"/%u)\n",

                   size, bytestream2_get_bytes_left(&gb));

            return AVERROR_INVALIDDATA;

        }

        ret = tscc2_decode_slice(c, i, buf + bytestream2_tell(&gb), size);

        if (ret) {

            av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d\n", i);

            return ret;

        }

        bytestream2_skip(&gb, size);

    }



    *got_frame      = 1;

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

        return ret;



    

    return buf_size;

}

    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) {         return ret;         *got_frame      = 1;         if ((ret = av_frame_ref(data, c->pic)) < 0)             return ret;
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;

    TSCC2Context *c = VAR_0->priv_data;

    GetByteContext gb;

    uint32_t frame_type, size;

    int VAR_6, VAR_7, VAR_8, VAR_9 = 0;

    int VAR_10 = c->mb_width * c->mb_height;

    int VAR_11;



    bytestream2_init(&gb, VAR_4, VAR_5);

    frame_type = bytestream2_get_byte(&gb);

    if (frame_type > 1) {

        av_log(VAR_0, AV_LOG_ERROR, "Incorrect frame type %"PRIu32"\n",

               frame_type);

        return AVERROR_INVALIDDATA;

    }



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

        return VAR_11;

    }



    if (frame_type == 0) {

        *VAR_2      = 1;

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

            return VAR_11;



        return VAR_5;

    }



    if (bytestream2_get_bytes_left(&gb) < 4) {

        av_log(VAR_0, AV_LOG_ERROR, "Frame is too short\n");

        return AVERROR_INVALIDDATA;

    }



    c->quant[0] = bytestream2_get_byte(&gb);

    c->quant[1] = bytestream2_get_byte(&gb);

    if (c->quant[0] < 2 || c->quant[0] > NUM_VLC_SETS + 1 ||

        c->quant[1] < 2 || c->quant[1] > NUM_VLC_SETS + 1) {

        av_log(VAR_0, AV_LOG_ERROR, "Invalid quantisers %d / %d\n",

               c->quant[0], c->quant[1]);

        return AVERROR_INVALIDDATA;

    }



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

        c->q[0][VAR_6] = tscc2_quants[c->quant[0] - 2][VAR_6];

        c->q[1][VAR_6] = tscc2_quants[c->quant[1] - 2][VAR_6];

    }



    bytestream2_skip(&gb, 1);



    size = bytestream2_get_le32(&gb);

    if (size > bytestream2_get_bytes_left(&gb)) {

        av_log(VAR_0, AV_LOG_ERROR, "Slice properties chunk is too large\n");

        return AVERROR_INVALIDDATA;

    }



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

        VAR_7   = bytestream2_get_byte(&gb);

        VAR_8   = VAR_7 & 0x3F;

        VAR_7 >>= 6;

        if (VAR_9 + VAR_8 > VAR_10) {

            av_log(VAR_0, AV_LOG_ERROR, "Too many slice properties\n");

            return AVERROR_INVALIDDATA;

        }

        memset(c->slice_quants + VAR_9, VAR_7, VAR_8);

        VAR_9 += VAR_8;

    }

    if (VAR_9 < VAR_10) {

        av_log(VAR_0, AV_LOG_ERROR, "Too few slice properties (%d / %d)\n",

               VAR_9, VAR_10);

        return AVERROR_INVALIDDATA;

    }



    for (VAR_6 = 0; VAR_6 < c->mb_height; VAR_6++) {

        size = bytestream2_peek_byte(&gb);

        if (size & 1) {

            size = bytestream2_get_byte(&gb) - 1;

        } else {

            size = bytestream2_get_le32(&gb) >> 1;

        }

        if (!size) {

            int skip_row = 1, j, off = VAR_6 * c->mb_width;

            for (j = 0; j < c->mb_width; j++) {

                if (c->slice_quants[off + j] == 1 ||

                    c->slice_quants[off + j] == 2) {

                    skip_row = 0;

                    break;

                }

            }

            if (!skip_row) {

                av_log(VAR_0, AV_LOG_ERROR, "Non-skip row with zero size\n");

                return AVERROR_INVALIDDATA;

            }

        }

        if (bytestream2_get_bytes_left(&gb) < size) {

            av_log(VAR_0, AV_LOG_ERROR, "Invalid slice size (%"PRIu32"/%u)\n",

                   size, bytestream2_get_bytes_left(&gb));

            return AVERROR_INVALIDDATA;

        }

        VAR_11 = tscc2_decode_slice(c, VAR_6, VAR_4 + bytestream2_tell(&gb), size);

        if (VAR_11) {

            av_log(VAR_0, AV_LOG_ERROR, "Error decoding slice %d\n", VAR_6);

            return VAR_11;

        }

        bytestream2_skip(&gb, size);

    }



    *VAR_2      = 1;

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

        return VAR_11;



    

    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; TSCC2Context *c = VAR_0->priv_data; GetByteContext gb; uint32_t frame_type, size; int VAR_6, VAR_7, VAR_8, VAR_9 = 0; int VAR_10 = c->mb_width * c->mb_height; int VAR_11; bytestream2_init(&gb, VAR_4, VAR_5); frame_type = bytestream2_get_byte(&gb); if (frame_type > 1) { av_log(VAR_0, AV_LOG_ERROR, "Incorrect frame type %"PRIu32"\n",
frame_type); return AVERROR_INVALIDDATA; } if ((VAR_11 = ff_reget_buffer(VAR_0, c->pic)) < 0) { return VAR_11; } if (frame_type == 0) { *VAR_2      = 1; if ((VAR_11 = av_frame_ref(VAR_1, c->pic)) < 0)
return VAR_11; return VAR_5; } if (bytestream2_get_bytes_left(&gb) < 4) { av_log(VAR_0, AV_LOG_ERROR, "Frame is too short\n"); return AVERROR_INVALIDDATA; } c->quant[0] = bytestream2_get_byte(&gb); c->quant[1] = bytestream2_get_byte(&gb); if (c->quant[0] < 2 || c->quant[0] > NUM_VLC_SETS + 1 ||
c->quant[1] < 2 || c->quant[1] > NUM_VLC_SETS + 1) { av_log(VAR_0, AV_LOG_ERROR, "Invalid quantisers %d / %d\n",
c->quant[0], c->quant[1]); return AVERROR_INVALIDDATA; } for (VAR_6 = 0; VAR_6 < 3; VAR_6++) { c->q[0][VAR_6] = tscc2_quants[c->quant[0] - 2][VAR_6]; c->q[1][VAR_6] = tscc2_quants[c->quant[1] - 2][VAR_6]; } bytestream2_skip(&gb, 1); size = bytestream2_get_le32(&gb); if (size > bytestream2_get_bytes_left(&gb)) { av_log(VAR_0, AV_LOG_ERROR, "Slice properties chunk is too large\n"); return AVERROR_INVALIDDATA; } for (VAR_6 = 0; VAR_6 < size; VAR_6++) { VAR_7   = bytestream2_get_byte(&gb); VAR_8   = VAR_7 & 0x3F; VAR_7 >>= 6; if (VAR_9 + VAR_8 > VAR_10) { av_log(VAR_0, AV_LOG_ERROR, "Too many slice properties\n"); return AVERROR_INVALIDDATA; } memset(c->slice_quants + VAR_9, VAR_7, VAR_8); VAR_9 += VAR_8; } if (VAR_9 < VAR_10) { av_log(VAR_0, AV_LOG_ERROR, "Too few slice properties (%d / %d)\n",
VAR_9, VAR_10); return AVERROR_INVALIDDATA; } for (VAR_6 = 0; VAR_6 < c->mb_height; VAR_6++) { size = bytestream2_peek_byte(&gb); if (size & 1) { size = bytestream2_get_byte(&gb) - 1; } else { size = bytestream2_get_le32(&gb) >> 1; } if (!size) { int skip_row = 1, j, off = VAR_6 * c->mb_width; for (j = 0; j < c->mb_width; j++) { if (c->slice_quants[off + j] == 1 ||
c->slice_quants[off + j] == 2) { skip_row = 0; break; } } if (!skip_row) { av_log(VAR_0, AV_LOG_ERROR, "Non-skip row with zero size\n"); return AVERROR_INVALIDDATA; } } if (bytestream2_get_bytes_left(&gb) < size) { av_log(VAR_0, AV_LOG_ERROR, "Invalid slice size (%"PRIu32"/%u)\n",
size, bytestream2_get_bytes_left(&gb)); return AVERROR_INVALIDDATA; } VAR_11 = tscc2_decode_slice(c, VAR_6, VAR_4 + bytestream2_tell(&gb), size); if (VAR_11) { av_log(VAR_0, AV_LOG_ERROR, "Error decoding slice %d\n", VAR_6); return VAR_11; } bytestream2_skip(&gb, size); } *VAR_2      = 1; if ((VAR_11 = av_frame_ref(VAR_1, c->pic)) < 0)
return VAR_11; return VAR_5; }