emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
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];    int dct_offset   = s->linesize*8; //default for progressive frames        for(i=0; i<6; i++) skip_dct[i]=0;        if(s->adaptive_quant){        s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;        if(s->out_format==FMT_H263){            if     (s->dquant> 2) s->dquant= 2;            else if(s->dquant<-2) s->dquant=-2;        }                    if(s->codec_id==CODEC_ID_MPEG4){                    if(!s->mb_intra){                assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);                if(s->mv_dir&MV_DIRECT)                    s->dquant=0;            }        }        s->qscale+= s->dquant;        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];    }    if (s->mb_intra) {        UINT8 *ptr;        int wrap_y;        int emu=0;        wrap_y = s->linesize;        ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){            emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);            ptr= s->edge_emu_buffer;            emu=1;        }                if(s->flags&CODEC_FLAG_INTERLACED_DCT){            int progressive_score, interlaced_score;                        progressive_score= pix_vcmp16x8(ptr, wrap_y  ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );            interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y  , wrap_y*2);                        if(progressive_score > interlaced_score + 100){                s->interlaced_dct=1;                            dct_offset= wrap_y;                wrap_y<<=1;            }else                s->interlaced_dct=0;        }        	s->dsp.get_pixels(s->block[0], ptr                 , wrap_y);        s->dsp.get_pixels(s->block[1], ptr              + 8, wrap_y);        s->dsp.get_pixels(s->block[2], ptr + dct_offset    , wrap_y);        s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);        if(s->flags&CODEC_FLAG_GRAY){            skip_dct[4]= 1;            skip_dct[5]= 1;        }else{            int wrap_c = s->uvlinesize;            ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;            if(emu){                emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);                ptr= s->edge_emu_buffer;            }	    s->dsp.get_pixels(s->block[4], ptr, wrap_c);            ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;            if(emu){                emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);                ptr= s->edge_emu_buffer;            }            s->dsp.get_pixels(s->block[5], ptr, wrap_c);        }    }else{        op_pixels_func (*op_pix)[4];        qpel_mc_func (*op_qpix)[16];        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.data[0] + (mb_y * 16 * s->linesize    ) + mb_x * 16;        dest_cb = s->current_picture.data[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;        dest_cr = s->current_picture.data[2] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;        wrap_y = s->linesize;        wrap_c = s->uvlinesize;        ptr_y  = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;        ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;        ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;        if ((!s->no_rounding) || s->pict_type==B_TYPE){	    op_pix = s->dsp.put_pixels_tab;            op_qpix= s->dsp.put_qpel_pixels_tab;        }else{            op_pix = s->dsp.put_no_rnd_pixels_tab;            op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;        }        if (s->mv_dir & MV_DIR_FORWARD) {            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);            op_pix = s->dsp.avg_pixels_tab;            op_qpix= s->dsp.avg_qpel_pixels_tab;        }        if (s->mv_dir & MV_DIR_BACKWARD) {            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, 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;        }                if(s->flags&CODEC_FLAG_INTERLACED_DCT){            int progressive_score, interlaced_score;                        progressive_score= pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y  )                              + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y  );            interlaced_score = pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y*2)                             + pix_diff_vcmp16x8(ptr_y + wrap_y  , dest_y + wrap_y  , wrap_y*2);                        if(progressive_score > interlaced_score + 600){                s->interlaced_dct=1;                            dct_offset= wrap_y;                wrap_y<<=1;            }else                s->interlaced_dct=0;        }        	s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);        s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 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;            }            s->dsp.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;            }            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);        }        /* pre quantization */                 if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){            //FIXME optimize	    if(s->dsp.pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;            if(s->dsp.pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;            if(s->dsp.pix_abs8x8(ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y) < 20*s->qscale) skip_dct[2]= 1;            if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;            if(s->dsp.pix_abs8x8(ptr_cb              , dest_cb              , wrap_c) < 20*s->qscale) skip_dct[4]= 1;            if(s->dsp.pix_abs8x8(ptr_cr              , dest_cr              , wrap_c) < 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;    }#ifdef CONFIG_ENCODERS    /* 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;    }#endif}