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

                             AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size       = avpkt->size;

    MpegEncContext *s = avctx->priv_data;

    AVFrame *pict = data;

    int i, ret;

    int slice_count;

    const uint8_t *slices_hdr = NULL;



    av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);



    /* no supplementary picture */

    if (buf_size == 0) {

        return 0;

    }



    if (!avctx->slice_count) {

        slice_count = (*buf++) + 1;

        buf_size--;



        if (!slice_count || buf_size <= 8 * slice_count) {

            av_log(avctx, AV_LOG_ERROR, "Invalid slice count: %d.\n",

                   slice_count);

            return AVERROR_INVALIDDATA;

        }



        slices_hdr = buf + 4;

        buf       += 8 * slice_count;

        buf_size  -= 8 * slice_count;

    } else

        slice_count = avctx->slice_count;



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

        unsigned offset = get_slice_offset(avctx, slices_hdr, i);

        int size, size2;



        if (offset >= buf_size)

            return AVERROR_INVALIDDATA;



        if (i + 1 == slice_count)

            size = buf_size - offset;

        else

            size = get_slice_offset(avctx, slices_hdr, i + 1) - offset;



        if (i + 2 >= slice_count)

            size2 = buf_size - offset;

        else

            size2 = get_slice_offset(avctx, slices_hdr, i + 2) - offset;



        if (size <= 0 || size2 <= 0 ||

            offset + FFMAX(size, size2) > buf_size)

            return AVERROR_INVALIDDATA;



        if ((ret = rv10_decode_packet(avctx, buf + offset, size, size2)) < 0)

            return ret;



        if (ret > 8 * size)

            i++;

    }



    if (s->current_picture_ptr != NULL && s->mb_y >= s->mb_height) {

        ff_er_frame_end(&s->er);

        ff_mpv_frame_end(s);



        if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {

            if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)

                return ret;

            ff_print_debug_info(s, s->current_picture_ptr);

        } else if (s->last_picture_ptr != NULL) {

            if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)

                return ret;

            ff_print_debug_info(s, s->last_picture_ptr);

        }



        if (s->last_picture_ptr || s->low_delay) {

            *got_frame = 1;

        }



        // so we can detect if frame_end was not called (find some nicer solution...)

        s->current_picture_ptr = NULL;

    }



    return avpkt->size;

}

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

                             AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size       = avpkt->size;

    MpegEncContext *s = avctx->priv_data;

    AVFrame *pict = data;

    int i, ret;

    int slice_count;

    const uint8_t *slices_hdr = NULL;



    av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);



    

    if (buf_size == 0) {

        return 0;

    }



    if (!avctx->slice_count) {

        slice_count = (*buf++) + 1;

        buf_size--;



        if (!slice_count || buf_size <= 8 * slice_count) {

            av_log(avctx, AV_LOG_ERROR, "Invalid slice count: %d.\n",

                   slice_count);

            return AVERROR_INVALIDDATA;

        }



        slices_hdr = buf + 4;

        buf       += 8 * slice_count;

        buf_size  -= 8 * slice_count;

    } else

        slice_count = avctx->slice_count;



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

        unsigned offset = get_slice_offset(avctx, slices_hdr, i);

        int size, size2;



        if (offset >= buf_size)

            return AVERROR_INVALIDDATA;



        if (i + 1 == slice_count)

            size = buf_size - offset;

        else

            size = get_slice_offset(avctx, slices_hdr, i + 1) - offset;



        if (i + 2 >= slice_count)

            size2 = buf_size - offset;

        else

            size2 = get_slice_offset(avctx, slices_hdr, i + 2) - offset;



        if (size <= 0 || size2 <= 0 ||

            offset + FFMAX(size, size2) > buf_size)

            return AVERROR_INVALIDDATA;



        if ((ret = rv10_decode_packet(avctx, buf + offset, size, size2)) < 0)

            return ret;



        if (ret > 8 * size)

            i++;

    }



    if (s->current_picture_ptr != NULL && s->mb_y >= s->mb_height) {

        ff_er_frame_end(&s->er);

        ff_mpv_frame_end(s);



        if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {

            if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)

                return ret;

            ff_print_debug_info(s, s->current_picture_ptr);

        } else if (s->last_picture_ptr != NULL) {

            if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)

                return ret;

            ff_print_debug_info(s, s->last_picture_ptr);

        }



        if (s->last_picture_ptr || s->low_delay) {

            *got_frame = 1;

        }



        

        s->current_picture_ptr = NULL;

    }



    return avpkt->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->VAR_11;

    MpegEncContext *s = VAR_0->priv_data;

    AVFrame *pict = VAR_1;

    int VAR_6, VAR_7;

    int VAR_8;

    const uint8_t *VAR_9 = NULL;



    av_dlog(VAR_0, "*****frame %d VAR_11=%d\n", VAR_0->frame_number, VAR_5);



    

    if (VAR_5 == 0) {

        return 0;

    }



    if (!VAR_0->VAR_8) {

        VAR_8 = (*VAR_4++) + 1;

        VAR_5--;



        if (!VAR_8 || VAR_5 <= 8 * VAR_8) {

            av_log(VAR_0, AV_LOG_ERROR, "Invalid slice count: %d.\n",

                   VAR_8);

            return AVERROR_INVALIDDATA;

        }



        VAR_9 = VAR_4 + 4;

        VAR_4       += 8 * VAR_8;

        VAR_5  -= 8 * VAR_8;

    } else

        VAR_8 = VAR_0->VAR_8;



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

        unsigned VAR_10 = get_slice_offset(VAR_0, VAR_9, VAR_6);

        int VAR_11, VAR_12;



        if (VAR_10 >= VAR_5)

            return AVERROR_INVALIDDATA;



        if (VAR_6 + 1 == VAR_8)

            VAR_11 = VAR_5 - VAR_10;

        else

            VAR_11 = get_slice_offset(VAR_0, VAR_9, VAR_6 + 1) - VAR_10;



        if (VAR_6 + 2 >= VAR_8)

            VAR_12 = VAR_5 - VAR_10;

        else

            VAR_12 = get_slice_offset(VAR_0, VAR_9, VAR_6 + 2) - VAR_10;



        if (VAR_11 <= 0 || VAR_12 <= 0 ||

            VAR_10 + FFMAX(VAR_11, VAR_12) > VAR_5)

            return AVERROR_INVALIDDATA;



        if ((VAR_7 = rv10_decode_packet(VAR_0, VAR_4 + VAR_10, VAR_11, VAR_12)) < 0)

            return VAR_7;



        if (VAR_7 > 8 * VAR_11)

            VAR_6++;

    }



    if (s->current_picture_ptr != NULL && s->mb_y >= s->mb_height) {

        ff_er_frame_end(&s->er);

        ff_mpv_frame_end(s);



        if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {

            if ((VAR_7 = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)

                return VAR_7;

            ff_print_debug_info(s, s->current_picture_ptr);

        } else if (s->last_picture_ptr != NULL) {

            if ((VAR_7 = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)

                return VAR_7;

            ff_print_debug_info(s, s->last_picture_ptr);

        }



        if (s->last_picture_ptr || s->low_delay) {

            *VAR_2 = 1;

        }



        

        s->current_picture_ptr = NULL;

    }



    return VAR_3->VAR_11;

}

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->VAR_11; MpegEncContext *s = VAR_0->priv_data; AVFrame *pict = VAR_1; int VAR_6, VAR_7; int VAR_8; const uint8_t *VAR_9 = NULL; av_dlog(VAR_0, "*****frame %d VAR_11=%d\n", VAR_0->frame_number, VAR_5); if (VAR_5 == 0) { return 0; } if (!VAR_0->VAR_8) { VAR_8 = (*VAR_4++) + 1; VAR_5--; if (!VAR_8 || VAR_5 <= 8 * VAR_8) { av_log(VAR_0, AV_LOG_ERROR, "Invalid slice count: %d.\n",
VAR_8); return AVERROR_INVALIDDATA; } VAR_9 = VAR_4 + 4; VAR_4       += 8 * VAR_8; VAR_5  -= 8 * VAR_8; } else VAR_8 = VAR_0->VAR_8; for (VAR_6 = 0; VAR_6 < VAR_8; VAR_6++) { unsigned VAR_10 = get_slice_offset(VAR_0, VAR_9, VAR_6); int VAR_11, VAR_12; if (VAR_10 >= VAR_5)
return AVERROR_INVALIDDATA; if (VAR_6 + 1 == VAR_8)
VAR_11 = VAR_5 - VAR_10; else
VAR_11 = get_slice_offset(VAR_0, VAR_9, VAR_6 + 1) - VAR_10; if (VAR_6 + 2 >= VAR_8)
VAR_12 = VAR_5 - VAR_10; else
VAR_12 = get_slice_offset(VAR_0, VAR_9, VAR_6 + 2) - VAR_10; if (VAR_11 <= 0 || VAR_12 <= 0 ||
VAR_10 + FFMAX(VAR_11, VAR_12) > VAR_5)
return AVERROR_INVALIDDATA; if ((VAR_7 = rv10_decode_packet(VAR_0, VAR_4 + VAR_10, VAR_11, VAR_12)) < 0)
return VAR_7; if (VAR_7 > 8 * VAR_11)
VAR_6++; } if (s->current_picture_ptr != NULL && s->mb_y >= s->mb_height) { ff_er_frame_end(&s->er); ff_mpv_frame_end(s); if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) { if ((VAR_7 = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
return VAR_7; ff_print_debug_info(s, s->current_picture_ptr); } else if (s->last_picture_ptr != NULL) { if ((VAR_7 = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
return VAR_7; ff_print_debug_info(s, s->last_picture_ptr); } if (s->last_picture_ptr || s->low_delay) { *VAR_2 = 1; } s->current_picture_ptr = NULL; } return VAR_3->VAR_11; }