int y_size, c_size, i, size;    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); c_size = (s->mb_width + 2) * (s->mb_height + 2); size = y_size + 2 * c_size; CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16)); s->dc_val[1] = s->dc_val[0] + y_size; s->dc_val[2] = s->dc_val[1] + c_size; for(i=0;i<size;i++) s->dc_val[0][i] = 1024;
int MPV_common_init(MpegEncContext *s){    int c_size, i;    UINT8 *pict;    DCT_common_init(s);        s->flags= s->avctx->flags;    s->mb_width = (s->width + 15) / 16;    s->mb_height = (s->height + 15) / 16;        /* set default edge pos, will be overriden in decode_header if needed */    s->h_edge_pos= s->mb_width*16;    s->v_edge_pos= s->mb_height*16;        /* convert fourcc to upper case */    s->avctx->fourcc=   toupper( s->avctx->fourcc     &0xFF)                               + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )                     + (toupper((s->avctx->fourcc>>16)&0xFF)<<16)                      + (toupper((s->avctx->fourcc>>24)&0xFF)<<24);    s->mb_num = s->mb_width * s->mb_height;    if(!(s->flags&CODEC_FLAG_DR1)){      s->linesize   = s->mb_width * 16 + 2 * EDGE_WIDTH;      s->uvlinesize = s->mb_width * 8  +     EDGE_WIDTH;      for(i=0;i<3;i++) {        int w, h, shift, pict_start;        w = s->linesize;        h = s->mb_height * 16 + 2 * EDGE_WIDTH;        shift = (i == 0) ? 0 : 1;        c_size = (s->linesize>>shift) * (h >> shift);        pict_start = (s->linesize>>shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);        CHECKED_ALLOCZ(pict, c_size)        s->last_picture_base[i] = pict;        s->last_picture[i] = pict + pict_start;        if(i>0) memset(s->last_picture_base[i], 128, c_size);            CHECKED_ALLOCZ(pict, c_size)        s->next_picture_base[i] = pict;        s->next_picture[i] = pict + pict_start;        if(i>0) memset(s->next_picture_base[i], 128, c_size);                if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {        /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but            do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */            CHECKED_ALLOCZ(pict, c_size)            s->aux_picture_base[i] = pict;            s->aux_picture[i] = pict + pict_start;            if(i>0) memset(s->aux_picture_base[i], 128, c_size);        }      }      s->ip_buffer_count= 2;    }        CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance        if (s->encoding) {        int j;        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);                CHECKED_ALLOCZ(s->mb_var   , s->mb_num * sizeof(INT16))        CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))        CHECKED_ALLOCZ(s->mb_mean  , s->mb_num * sizeof(INT8))        /* Allocate MV tables */        CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(INT16))        CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(INT16))        CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(INT16))        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))        CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))        CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))        CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))        CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(INT16))        CHECKED_ALLOCZ(s->me_scratchpad,  s->linesize*16*3*sizeof(uint8_t))                CHECKED_ALLOCZ(s->me_map      , ME_MAP_SIZE*sizeof(uint32_t))        CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))        if(s->max_b_frames){            for(j=0; j<REORDER_BUFFER_SIZE; j++){                int i;                for(i=0;i<3;i++) {                    int w, h, shift;                    w = s->linesize;                    h = s->mb_height * 16;                    shift = (i == 0) ? 0 : 1;                    c_size = (w >> shift) * (h >> shift);                    CHECKED_ALLOCZ(pict, c_size);                    s->picture_buffer[j][i] = pict;                }            }        }        if(s->codec_id==CODEC_ID_MPEG4){            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);        }                if(s->msmpeg4_version){            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));        }        CHECKED_ALLOCZ(s->avctx->stats_out, 256);    }            CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(UINT8))        if (s->out_format == FMT_H263 || s->encoding) {        int size;        /* Allocate MB type table */        CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(UINT8))        /* MV prediction */        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));    }    if(s->codec_id==CODEC_ID_MPEG4){        /* interlaced direct mode decoding tables */        CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16))        CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8))    }    /* 4mv b frame decoding table */    //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)    CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))    if (s->h263_pred || s->h263_plus) {        int y_size, c_size, i, size;                /* dc values */        y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);        c_size = (s->mb_width + 2) * (s->mb_height + 2);        size = y_size + 2 * c_size;        CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16));        s->dc_val[1] = s->dc_val[0] + y_size;        s->dc_val[2] = s->dc_val[1] + c_size;        for(i=0;i<size;i++)            s->dc_val[0][i] = 1024;        /* ac values */        CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16);        s->ac_val[1] = s->ac_val[0] + y_size;        s->ac_val[2] = s->ac_val[1] + c_size;                /* cbp values */        CHECKED_ALLOCZ(s->coded_block, y_size);                /* divx501 bitstream reorder buffer */        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);                /* cbp, ac_pred, pred_dir */        CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(UINT8))        CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))    }    CHECKED_ALLOCZ(s->qscale_table  , s->mb_num * sizeof(UINT8))        /* which mb is a intra block */    CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);    memset(s->mbintra_table, 1, s->mb_num);        /* default structure is frame */    s->picture_structure = PICT_FRAME;        /* init macroblock skip table */    CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);    //Note the +1 is for a quicker mpeg4 slice_end detection        s->block= s->blocks[0];    s->context_initialized = 1;    return 0; fail:    MPV_common_end(s);    return -1;}