if ((!s->no_rounding) || s->pict_type==B_TYPE) op_pix = avg_pixels_tab; else op_pix = avg_no_rnd_pixels_tab;
op_pix = avg_pixels_tab; op_qpix= avg_qpel_pixels_tab;
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y){    const int mb_x= s->mb_x;    const int mb_y= s->mb_y;    int i;    int skip_dct[6];#if 0        if (s->interlaced_dct) {            dct_linesize = s->linesize * 2;            dct_offset = s->linesize;        } else {            dct_linesize = s->linesize;            dct_offset = s->linesize * 8;        }#endif    for(i=0; i<6; i++) skip_dct[i]=0;    if (s->mb_intra) {        UINT8 *ptr;        int wrap;        int emu=0;        wrap = s->linesize;        ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){            emulated_edge_mc(s, ptr, wrap, 16, 16, mb_x*16, mb_y*16, s->width, s->height);            ptr= s->edge_emu_buffer;            emu=1;        }        get_pixels(s->block[0], ptr               , wrap);        get_pixels(s->block[1], ptr            + 8, wrap);        get_pixels(s->block[2], ptr + 8 * wrap    , wrap);        get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);        if(s->flags&CODEC_FLAG_GRAY){            skip_dct[4]= 1;            skip_dct[5]= 1;        }else{            wrap >>=1;            ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;            if(emu){                emulated_edge_mc(s, ptr, wrap, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);                ptr= s->edge_emu_buffer;            }            get_pixels(s->block[4], ptr, wrap);            ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;            if(emu){                emulated_edge_mc(s, ptr, wrap, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);                ptr= s->edge_emu_buffer;            }            get_pixels(s->block[5], ptr, wrap);        }    }else{        op_pixels_func *op_pix;        qpel_mc_func *op_qpix;        UINT8 *dest_y, *dest_cb, *dest_cr;        UINT8 *ptr_y, *ptr_cb, *ptr_cr;        int wrap_y, wrap_c;        int emu=0;        dest_y  = s->current_picture[0] + (mb_y * 16 * s->linesize       ) + mb_x * 16;        dest_cb = s->current_picture[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;        dest_cr = s->current_picture[2] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;        wrap_y = s->linesize;        wrap_c = wrap_y>>1;        ptr_y  = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;        ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;        ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;        if ((!s->no_rounding) || s->pict_type==B_TYPE){            op_pix = put_pixels_tab;            op_qpix= qpel_mc_rnd_tab;        }else{            op_pix = put_no_rnd_pixels_tab;            op_qpix= qpel_mc_no_rnd_tab;        }        if (s->mv_dir & MV_DIR_FORWARD) {            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);           if ((!s->no_rounding) || s->pict_type==B_TYPE)                op_pix = avg_pixels_tab;            else                op_pix = avg_no_rnd_pixels_tab;        }        if (s->mv_dir & MV_DIR_BACKWARD) {            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);        }        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){            emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);            ptr_y= s->edge_emu_buffer;            emu=1;        }        diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);        diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);        diff_pixels(s->block[2], ptr_y + 8 * wrap_y    , dest_y + 8 * wrap_y    , wrap_y);        diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y);                if(s->flags&CODEC_FLAG_GRAY){            skip_dct[4]= 1;            skip_dct[5]= 1;        }else{            if(emu){                emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);                ptr_cb= s->edge_emu_buffer;            }            diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);            if(emu){                emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);                ptr_cr= s->edge_emu_buffer;            }            diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);        }        /* pre quantization */                 if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){            if(pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;            if(pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;            if(pix_abs8x8(ptr_y + 8*wrap_y    , dest_y + 8*wrap_y    , wrap_y) < 20*s->qscale) skip_dct[2]= 1;            if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;            if(pix_abs8x8(ptr_cb              , dest_cb              , wrap_y) < 20*s->qscale) skip_dct[4]= 1;            if(pix_abs8x8(ptr_cr              , dest_cr              , wrap_y) < 20*s->qscale) skip_dct[5]= 1;#if 0{ static int stat[7]; int num=0; for(i=0; i<6; i++)  if(skip_dct[i]) num++; stat[num]++;  if(s->mb_x==0 && s->mb_y==0){  for(i=0; i<7; i++){   printf("%6d %1d\n", stat[i], i);  } }}#endif        }    }            #if 0            {                float adap_parm;                                adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /                            ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);                            printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",                         (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',                         s->qscale, adap_parm, s->qscale*adap_parm,                        s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);            }#endif    /* DCT & quantize */    if(s->out_format==FMT_MJPEG){        for(i=0;i<6;i++) {            int overflow;            s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);        }    }else{        for(i=0;i<6;i++) {            if(!skip_dct[i]){                int overflow;                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);            // FIXME we could decide to change to quantizer instead of clipping            // JS: I don't think that would be a good idea it could lower quality instead            //     of improve it. Just INTRADC clipping deserves changes in quantizer                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);            }else                s->block_last_index[i]= -1;        }        if(s->luma_elim_threshold && !s->mb_intra)            for(i=0; i<4; i++)                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);        if(s->chroma_elim_threshold && !s->mb_intra)            for(i=4; i<6; i++)                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);    }    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){        s->block_last_index[4]=        s->block_last_index[5]= 0;        s->block[4][0]=        s->block[5][0]= 128;    }    /* huffman encode */    switch(s->out_format) {    case FMT_MPEG1:        mpeg1_encode_mb(s, s->block, motion_x, motion_y);        break;    case FMT_H263:        if (s->h263_msmpeg4)            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);        else if(s->h263_pred)            mpeg4_encode_mb(s, s->block, motion_x, motion_y);        else            h263_encode_mb(s, s->block, motion_x, motion_y);        break;    case FMT_MJPEG:        mjpeg_encode_mb(s, s->block);        break;    }}