static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
{
    MpegEncContext *s = &r->s;
    GetBitContext *gb = &s->gb;
    int mb_pos;
    int res;
    init_get_bits(&r->s.gb, buf, buf_size*8);
    res = r->parse_slice_header(r, gb, &r->si);
    if(res < 0){
        av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
        return -1;
    if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
        if(s->width != r->si.width || s->height != r->si.height){
            av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", r->si.width,r->si.height);
            MPV_common_end(s);
            s->width  = r->si.width;
            s->height = r->si.height;
            avcodec_set_dimensions(s->avctx, s->width, s->height);
            if(MPV_common_init(s) < 0)
                return -1;
            r->intra_types_stride = s->mb_width*4 + 4;
            r->intra_types_hist = av_realloc(r->intra_types_hist, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
            r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
            r->mb_type = av_realloc(r->mb_type, r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
            r->cbp_luma   = av_realloc(r->cbp_luma,   r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
            r->cbp_chroma = av_realloc(r->cbp_chroma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
            r->deblock_coefs = av_realloc(r->deblock_coefs, r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
        s->pict_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
        if(MPV_frame_start(s, s->avctx) < 0)
            return -1;
        ff_er_frame_start(s);
        if (!r->tmp_b_block_base || s->width != r->si.width || s->height != r->si.height) {
            int i;
            av_free(r->tmp_b_block_base); //realloc() doesn't guarantee alignment
            r->tmp_b_block_base = av_malloc(s->linesize * 48);
            for (i = 0; i < 2; i++)
                r->tmp_b_block_y[i] = r->tmp_b_block_base + i * 16 * s->linesize;
            for (i = 0; i < 4; i++)
                r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
                                       + (i >> 1) * 8 * s->uvlinesize + (i & 1) * 16;
        r->cur_pts = r->si.pts;
        if(s->pict_type != AV_PICTURE_TYPE_B){
            r->last_pts = r->next_pts;
            r->next_pts = r->cur_pts;
        }else{
            int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
            int dist0   = GET_PTS_DIFF(r->cur_pts,  r->last_pts);
            int dist1   = GET_PTS_DIFF(r->next_pts, r->cur_pts);
            if(!refdist){
                r->weight1 = r->weight2 = 8192;
            }else{
                r->weight1 = (dist0 << 14) / refdist;
                r->weight2 = (dist1 << 14) / refdist;
        s->mb_x = s->mb_y = 0;
    r->si.end = end;
    s->qscale = r->si.quant;
    r->bits = buf_size*8;
    s->mb_num_left = r->si.end - r->si.start;
    r->s.mb_skip_run = 0;
    mb_pos = s->mb_x + s->mb_y * s->mb_width;
    if(r->si.start != mb_pos){
        av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
        s->mb_x = r->si.start % s->mb_width;
        s->mb_y = r->si.start / s->mb_width;
    memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
    s->first_slice_line = 1;
    s->resync_mb_x = s->mb_x;
    s->resync_mb_y = s->mb_y;
    ff_init_block_index(s);
    while(!check_slice_end(r, s)) {
        ff_update_block_index(s);
        s->dsp.clear_blocks(s->block[0]);
        if(rv34_decode_macroblock(r, r->intra_types + s->mb_x * 4 + 4) < 0){
            ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
            return -1;
        if (++s->mb_x == s->mb_width) {
            s->mb_x = 0;
            s->mb_y++;
            ff_init_block_index(s);
            memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
            memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
            if(r->loop_filter && s->mb_y >= 2)
                r->loop_filter(r, s->mb_y - 2);
        if(s->mb_x == s->resync_mb_x)
            s->first_slice_line=0;
        s->mb_num_left--;
    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
    return s->mb_y == s->mb_height;
static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
{
    MpegEncContext *s = &r->s;
    GetBitContext *gb = &s->gb;
    int mb_pos;
    int res;
    init_get_bits(&r->s.gb, buf, buf_size*8);
    res = r->parse_slice_header(r, gb, &r->si);
    if(res < 0){
        av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
        return -1;
    if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
        if(s->width != r->si.width || s->height != r->si.height){
            av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", r->si.width,r->si.height);
            MPV_common_end(s);
            s->width  = r->si.width;
            s->height = r->si.height;
            avcodec_set_dimensions(s->avctx, s->width, s->height);
            if(MPV_common_init(s) < 0)
                return -1;
            r->intra_types_stride = s->mb_width*4 + 4;
            r->intra_types_hist = av_realloc(r->intra_types_hist, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
            r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
            r->mb_type = av_realloc(r->mb_type, r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
            r->cbp_luma   = av_realloc(r->cbp_luma,   r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
            r->cbp_chroma = av_realloc(r->cbp_chroma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
            r->deblock_coefs = av_realloc(r->deblock_coefs, r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
        s->pict_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
        if(MPV_frame_start(s, s->avctx) < 0)
            return -1;
        ff_er_frame_start(s);
        if (!r->tmp_b_block_base || s->width != r->si.width || s->height != r->si.height) {
            int i;
            av_free(r->tmp_b_block_base); 
            r->tmp_b_block_base = av_malloc(s->linesize * 48);
            for (i = 0; i < 2; i++)
                r->tmp_b_block_y[i] = r->tmp_b_block_base + i * 16 * s->linesize;
            for (i = 0; i < 4; i++)
                r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
                                       + (i >> 1) * 8 * s->uvlinesize + (i & 1) * 16;
        r->cur_pts = r->si.pts;
        if(s->pict_type != AV_PICTURE_TYPE_B){
            r->last_pts = r->next_pts;
            r->next_pts = r->cur_pts;
        }else{
            int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
            int dist0   = GET_PTS_DIFF(r->cur_pts,  r->last_pts);
            int dist1   = GET_PTS_DIFF(r->next_pts, r->cur_pts);
            if(!refdist){
                r->weight1 = r->weight2 = 8192;
            }else{
                r->weight1 = (dist0 << 14) / refdist;
                r->weight2 = (dist1 << 14) / refdist;
        s->mb_x = s->mb_y = 0;
    r->si.end = end;
    s->qscale = r->si.quant;
    r->bits = buf_size*8;
    s->mb_num_left = r->si.end - r->si.start;
    r->s.mb_skip_run = 0;
    mb_pos = s->mb_x + s->mb_y * s->mb_width;
    if(r->si.start != mb_pos){
        av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
        s->mb_x = r->si.start % s->mb_width;
        s->mb_y = r->si.start / s->mb_width;
    memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
    s->first_slice_line = 1;
    s->resync_mb_x = s->mb_x;
    s->resync_mb_y = s->mb_y;
    ff_init_block_index(s);
    while(!check_slice_end(r, s)) {
        ff_update_block_index(s);
        s->dsp.clear_blocks(s->block[0]);
        if(rv34_decode_macroblock(r, r->intra_types + s->mb_x * 4 + 4) < 0){
            ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
            return -1;
        if (++s->mb_x == s->mb_width) {
            s->mb_x = 0;
            s->mb_y++;
            ff_init_block_index(s);
            memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
            memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
            if(r->loop_filter && s->mb_y >= 2)
                r->loop_filter(r, s->mb_y - 2);
        if(s->mb_x == s->resync_mb_x)
            s->first_slice_line=0;
        s->mb_num_left--;
    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
    return s->mb_y == s->mb_height;
static int FUNC_0(RV34DecContext *VAR_0, int VAR_1, const uint8_t* VAR_2, int VAR_3)
{
    MpegEncContext *s = &VAR_0->s;
    GetBitContext *gb = &s->gb;
    int VAR_4;
    int VAR_5;
    init_get_bits(&VAR_0->s.gb, VAR_2, VAR_3*8);
    VAR_5 = VAR_0->parse_slice_header(VAR_0, gb, &VAR_0->si);
    if(VAR_5 < 0){
        av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
        return -1;
    if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
        if(s->width != VAR_0->si.width || s->height != VAR_0->si.height){
            av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", VAR_0->si.width,VAR_0->si.height);
            MPV_common_end(s);
            s->width  = VAR_0->si.width;
            s->height = VAR_0->si.height;
            avcodec_set_dimensions(s->avctx, s->width, s->height);
            if(MPV_common_init(s) < 0)
                return -1;
            VAR_0->intra_types_stride = s->mb_width*4 + 4;
            VAR_0->intra_types_hist = av_realloc(VAR_0->intra_types_hist, VAR_0->intra_types_stride * 4 * 2 * sizeof(*VAR_0->intra_types_hist));
            VAR_0->intra_types = VAR_0->intra_types_hist + VAR_0->intra_types_stride * 4;
            VAR_0->mb_type = av_realloc(VAR_0->mb_type, VAR_0->s.mb_stride * VAR_0->s.mb_height * sizeof(*VAR_0->mb_type));
            VAR_0->cbp_luma   = av_realloc(VAR_0->cbp_luma,   VAR_0->s.mb_stride * VAR_0->s.mb_height * sizeof(*VAR_0->cbp_luma));
            VAR_0->cbp_chroma = av_realloc(VAR_0->cbp_chroma, VAR_0->s.mb_stride * VAR_0->s.mb_height * sizeof(*VAR_0->cbp_chroma));
            VAR_0->deblock_coefs = av_realloc(VAR_0->deblock_coefs, VAR_0->s.mb_stride * VAR_0->s.mb_height * sizeof(*VAR_0->deblock_coefs));
        s->pict_type = VAR_0->si.type ? VAR_0->si.type : AV_PICTURE_TYPE_I;
        if(MPV_frame_start(s, s->avctx) < 0)
            return -1;
        ff_er_frame_start(s);
        if (!VAR_0->tmp_b_block_base || s->width != VAR_0->si.width || s->height != VAR_0->si.height) {
            int VAR_6;
            av_free(VAR_0->tmp_b_block_base); 
            VAR_0->tmp_b_block_base = av_malloc(s->linesize * 48);
            for (VAR_6 = 0; VAR_6 < 2; VAR_6++)
                VAR_0->tmp_b_block_y[VAR_6] = VAR_0->tmp_b_block_base + VAR_6 * 16 * s->linesize;
            for (VAR_6 = 0; VAR_6 < 4; VAR_6++)
                VAR_0->tmp_b_block_uv[VAR_6] = VAR_0->tmp_b_block_base + 32 * s->linesize
                                       + (VAR_6 >> 1) * 8 * s->uvlinesize + (VAR_6 & 1) * 16;
        VAR_0->cur_pts = VAR_0->si.pts;
        if(s->pict_type != AV_PICTURE_TYPE_B){
            VAR_0->last_pts = VAR_0->next_pts;
            VAR_0->next_pts = VAR_0->cur_pts;
        }else{
            int VAR_7 = GET_PTS_DIFF(VAR_0->next_pts, VAR_0->last_pts);
            int VAR_8   = GET_PTS_DIFF(VAR_0->cur_pts,  VAR_0->last_pts);
            int VAR_9   = GET_PTS_DIFF(VAR_0->next_pts, VAR_0->cur_pts);
            if(!VAR_7){
                VAR_0->weight1 = VAR_0->weight2 = 8192;
            }else{
                VAR_0->weight1 = (VAR_8 << 14) / VAR_7;
                VAR_0->weight2 = (VAR_9 << 14) / VAR_7;
        s->mb_x = s->mb_y = 0;
    VAR_0->si.VAR_1 = VAR_1;
    s->qscale = VAR_0->si.quant;
    VAR_0->bits = VAR_3*8;
    s->mb_num_left = VAR_0->si.VAR_1 - VAR_0->si.start;
    VAR_0->s.mb_skip_run = 0;
    VAR_4 = s->mb_x + s->mb_y * s->mb_width;
    if(VAR_0->si.start != VAR_4){
        av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", VAR_0->si.start, VAR_4);
        s->mb_x = VAR_0->si.start % s->mb_width;
        s->mb_y = VAR_0->si.start / s->mb_width;
    memset(VAR_0->intra_types_hist, -1, VAR_0->intra_types_stride * 4 * 2 * sizeof(*VAR_0->intra_types_hist));
    s->first_slice_line = 1;
    s->resync_mb_x = s->mb_x;
    s->resync_mb_y = s->mb_y;
    ff_init_block_index(s);
    while(!check_slice_end(VAR_0, s)) {
        ff_update_block_index(s);
        s->dsp.clear_blocks(s->block[0]);
        if(rv34_decode_macroblock(VAR_0, VAR_0->intra_types + s->mb_x * 4 + 4) < 0){
            ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
            return -1;
        if (++s->mb_x == s->mb_width) {
            s->mb_x = 0;
            s->mb_y++;
            ff_init_block_index(s);
            memmove(VAR_0->intra_types_hist, VAR_0->intra_types, VAR_0->intra_types_stride * 4 * sizeof(*VAR_0->intra_types_hist));
            memset(VAR_0->intra_types, -1, VAR_0->intra_types_stride * 4 * sizeof(*VAR_0->intra_types_hist));
            if(VAR_0->loop_filter && s->mb_y >= 2)
                VAR_0->loop_filter(VAR_0, s->mb_y - 2);
        if(s->mb_x == s->resync_mb_x)
            s->first_slice_line=0;
        s->mb_num_left--;
    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
    return s->mb_y == s->mb_height;
static int FUNC_0(RV34DecContext *VAR_0, int VAR_1, const uint8_t* VAR_2, int VAR_3)
{ MpegEncContext *s = &VAR_0->s; GetBitContext *gb = &s->gb; int VAR_4; int VAR_5; init_get_bits(&VAR_0->s.gb, VAR_2, VAR_3*8); VAR_5 = VAR_0->parse_slice_header(VAR_0, gb, &VAR_0->si); if(VAR_5 < 0){ av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n"); return -1; if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) { if(s->width != VAR_0->si.width || s->height != VAR_0->si.height){ av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", VAR_0->si.width,VAR_0->si.height); MPV_common_end(s); s->width  = VAR_0->si.width; s->height = VAR_0->si.height; avcodec_set_dimensions(s->avctx, s->width, s->height); if(MPV_common_init(s) < 0)
return -1; VAR_0->intra_types_stride = s->mb_width*4 + 4; VAR_0->intra_types_hist = av_realloc(VAR_0->intra_types_hist, VAR_0->intra_types_stride * 4 * 2 * sizeof(*VAR_0->intra_types_hist)); VAR_0->intra_types = VAR_0->intra_types_hist + VAR_0->intra_types_stride * 4; VAR_0->mb_type = av_realloc(VAR_0->mb_type, VAR_0->s.mb_stride * VAR_0->s.mb_height * sizeof(*VAR_0->mb_type)); VAR_0->cbp_luma   = av_realloc(VAR_0->cbp_luma,   VAR_0->s.mb_stride * VAR_0->s.mb_height * sizeof(*VAR_0->cbp_luma)); VAR_0->cbp_chroma = av_realloc(VAR_0->cbp_chroma, VAR_0->s.mb_stride * VAR_0->s.mb_height * sizeof(*VAR_0->cbp_chroma)); VAR_0->deblock_coefs = av_realloc(VAR_0->deblock_coefs, VAR_0->s.mb_stride * VAR_0->s.mb_height * sizeof(*VAR_0->deblock_coefs)); s->pict_type = VAR_0->si.type ? VAR_0->si.type : AV_PICTURE_TYPE_I; if(MPV_frame_start(s, s->avctx) < 0)
return -1; ff_er_frame_start(s); if (!VAR_0->tmp_b_block_base || s->width != VAR_0->si.width || s->height != VAR_0->si.height) { int VAR_6; av_free(VAR_0->tmp_b_block_base); VAR_0->tmp_b_block_base = av_malloc(s->linesize * 48); for (VAR_6 = 0; VAR_6 < 2; VAR_6++) VAR_0->tmp_b_block_y[VAR_6] = VAR_0->tmp_b_block_base + VAR_6 * 16 * s->linesize; for (VAR_6 = 0; VAR_6 < 4; VAR_6++) VAR_0->tmp_b_block_uv[VAR_6] = VAR_0->tmp_b_block_base + 32 * s->linesize
+ (VAR_6 >> 1) * 8 * s->uvlinesize + (VAR_6 & 1) * 16; VAR_0->cur_pts = VAR_0->si.pts; if(s->pict_type != AV_PICTURE_TYPE_B){ VAR_0->last_pts = VAR_0->next_pts; VAR_0->next_pts = VAR_0->cur_pts; }else{ int VAR_7 = GET_PTS_DIFF(VAR_0->next_pts, VAR_0->last_pts); int VAR_8   = GET_PTS_DIFF(VAR_0->cur_pts,  VAR_0->last_pts); int VAR_9   = GET_PTS_DIFF(VAR_0->next_pts, VAR_0->cur_pts); if(!VAR_7){ VAR_0->weight1 = VAR_0->weight2 = 8192; }else{ VAR_0->weight1 = (VAR_8 << 14) / VAR_7; VAR_0->weight2 = (VAR_9 << 14) / VAR_7; s->mb_x = s->mb_y = 0; VAR_0->si.VAR_1 = VAR_1; s->qscale = VAR_0->si.quant; VAR_0->bits = VAR_3*8; s->mb_num_left = VAR_0->si.VAR_1 - VAR_0->si.start; VAR_0->s.mb_skip_run = 0; VAR_4 = s->mb_x + s->mb_y * s->mb_width; if(VAR_0->si.start != VAR_4){ av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", VAR_0->si.start, VAR_4); s->mb_x = VAR_0->si.start % s->mb_width; s->mb_y = VAR_0->si.start / s->mb_width; memset(VAR_0->intra_types_hist, -1, VAR_0->intra_types_stride * 4 * 2 * sizeof(*VAR_0->intra_types_hist)); s->first_slice_line = 1; s->resync_mb_x = s->mb_x; s->resync_mb_y = s->mb_y; ff_init_block_index(s); while(!check_slice_end(VAR_0, s)) { ff_update_block_index(s); s->dsp.clear_blocks(s->block[0]); if(rv34_decode_macroblock(VAR_0, VAR_0->intra_types + s->mb_x * 4 + 4) < 0){ ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR); return -1; if (++s->mb_x == s->mb_width) { s->mb_x = 0; s->mb_y++; ff_init_block_index(s); memmove(VAR_0->intra_types_hist, VAR_0->intra_types, VAR_0->intra_types_stride * 4 * sizeof(*VAR_0->intra_types_hist)); memset(VAR_0->intra_types, -1, VAR_0->intra_types_stride * 4 * sizeof(*VAR_0->intra_types_hist)); if(VAR_0->loop_filter && s->mb_y >= 2)
VAR_0->loop_filter(VAR_0, s->mb_y - 2); if(s->mb_x == s->resync_mb_x)
s->first_slice_line=0; s->mb_num_left--; ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END); return s->mb_y == s->mb_height;