pix = c->src[0][0]; sum = s->dsp.pix_sum(pix, s->linesize); varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;  pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;
void ff_estimate_p_frame_motion(MpegEncContext * s,                                int mb_x, int mb_y){    MotionEstContext * const c= &s->me;    uint8_t *pix, *ppix;    int sum, varc, vard, mx, my, dmin, xx, yy;    int P[10][2];    const int shift= 1+s->quarter_sample;    int mb_type=0;    Picture * const pic= &s->current_picture;        init_ref(s, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);    assert(s->quarter_sample==0 || s->quarter_sample==1);    assert(s->linesize == s->me.stride);    assert(s->uvlinesize == s->me.uvstride);    s->me.penalty_factor    = get_penalty_factor(s, s->avctx->me_cmp);    s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);    s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);    s->me.current_mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;    get_limits(s, 16*mb_x, 16*mb_y);    s->me.skip=0;    if(s->avctx->me_threshold){        vard= (check_input_motion(s, mb_x, mb_y, 1)+128)>>8;                if(vard<s->avctx->me_threshold){            pix = c->src[0][0];            sum = s->dsp.pix_sum(pix, s->linesize);            varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;                    pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;            pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;            pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;            s->mb_var_sum_temp    += varc;            s->mc_mb_var_sum_temp += vard;            if (vard <= 64 || vard < varc) { //FIXME                s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);            }else{                s->scene_change_score+= s->qscale;            }            return;        }    }    switch(s->me_method) {    case ME_ZERO:    default:	no_motion_search(s, &mx, &my);        mx-= mb_x*16;        my-= mb_y*16;        dmin = 0;        break;#if 0    case ME_FULL:	dmin = full_motion_search(s, &mx, &my, range, ref_picture);        mx-= mb_x*16;        my-= mb_y*16;        break;    case ME_LOG:	dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);        mx-= mb_x*16;        my-= mb_y*16;        break;    case ME_PHODS:	dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);        mx-= mb_x*16;        my-= mb_y*16;        break;#endif    case ME_X1:    case ME_EPZS:       {            const int mot_stride = s->b8_stride;            const int mot_xy = s->block_index[0];            P_LEFT[0]       = s->current_picture.motion_val[0][mot_xy - 1][0];            P_LEFT[1]       = s->current_picture.motion_val[0][mot_xy - 1][1];            if(P_LEFT[0]       > (s->me.xmax<<shift)) P_LEFT[0]       = (s->me.xmax<<shift);            if(!s->first_slice_line) {                P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];                P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];                P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];                P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];                if(P_TOP[1]      > (s->me.ymax<<shift)) P_TOP[1]     = (s->me.ymax<<shift);                if(P_TOPRIGHT[0] < (s->me.xmin<<shift)) P_TOPRIGHT[0]= (s->me.xmin<<shift);                if(P_TOPRIGHT[1] > (s->me.ymax<<shift)) P_TOPRIGHT[1]= (s->me.ymax<<shift);                        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);                if(s->out_format == FMT_H263){                    c->pred_x = P_MEDIAN[0];                    c->pred_y = P_MEDIAN[1];                }else { /* mpeg1 at least */                    c->pred_x= P_LEFT[0];                    c->pred_y= P_LEFT[1];                }            }else{                c->pred_x= P_LEFT[0];                c->pred_y= P_LEFT[1];            }        }        dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift);               break;    }    /* intra / predictive decision */    xx = mb_x * 16;    yy = mb_y * 16;    pix = c->src[0][0];    /* At this point (mx,my) are full-pell and the relative displacement */    ppix = c->ref[0][0] + (my * s->linesize) + mx;        sum = s->dsp.pix_sum(pix, s->linesize);        varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;    vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16)+128)>>8;//printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);    pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;    pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;//    pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;     s->mb_var_sum_temp    += varc;    s->mc_mb_var_sum_temp += vard;//printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);    #if 0    printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",	   varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);#endif    if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){        if (vard <= 64 || vard < varc)            s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);        else            s->scene_change_score+= s->qscale;        if (vard*2 + 200 > varc)            mb_type|= CANDIDATE_MB_TYPE_INTRA;        if (varc*2 + 200 > vard){            mb_type|= CANDIDATE_MB_TYPE_INTER;            s->me.sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);            if(s->flags&CODEC_FLAG_MV0)                if(mx || my)                    mb_type |= CANDIDATE_MB_TYPE_SKIPED; //FIXME check difference        }else{            mx <<=shift;            my <<=shift;        }        if((s->flags&CODEC_FLAG_4MV)           && !s->me.skip && varc>50 && vard>10){            if(h263_mv4_search(s, mx, my, shift) < INT_MAX)                mb_type|=CANDIDATE_MB_TYPE_INTER4V;            set_p_mv_tables(s, mx, my, 0);        }else            set_p_mv_tables(s, mx, my, 1);        if((s->flags&CODEC_FLAG_INTERLACED_ME)           && !s->me.skip){ //FIXME varc/d checks            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my) < INT_MAX)                mb_type |= CANDIDATE_MB_TYPE_INTER_I;        }    }else{        int intra_score, i;        mb_type= CANDIDATE_MB_TYPE_INTER;        dmin= s->me.sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)            dmin= get_mb_score(s, mx, my, 0, 0);        if((s->flags&CODEC_FLAG_4MV)           && !s->me.skip && varc>50 && vard>10){            int dmin4= h263_mv4_search(s, mx, my, shift);            if(dmin4 < dmin){                mb_type= CANDIDATE_MB_TYPE_INTER4V;                dmin=dmin4;            }        }        if((s->flags&CODEC_FLAG_INTERLACED_ME)           && !s->me.skip){ //FIXME varc/d checks            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my);            if(dmin_i < dmin){                mb_type = CANDIDATE_MB_TYPE_INTER_I;                dmin= dmin_i;            }        }                //        pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;         set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);        /* get intra luma score */        if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){            intra_score= (varc<<8) - 500; //FIXME dont scale it down so we dont have to fix it        }else{            int mean= (sum+128)>>8;            mean*= 0x01010101;                        for(i=0; i<16; i++){                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;            }            intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, pix, s->linesize, 16);        }#if 0 //FIXME        /* get chroma score */        if(s->avctx->mb_cmp&FF_CMP_CHROMA){            for(i=1; i<3; i++){                uint8_t *dest_c;                int mean;                                if(s->out_format == FMT_H263){                    mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;)                }else{                    mean= (s->last_dc[i] + 4)>>3;                }                dest_c = s->new_picture.data[i] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;                                mean*= 0x01010101;                for(i=0; i<8; i++){                    *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;                    *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;                }                                intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);            }                        }#endif        intra_score += s->me.mb_penalty_factor*16;                if(intra_score < dmin){            mb_type= CANDIDATE_MB_TYPE_INTRA;            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup        }else            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;                if (vard <= 64 || vard < varc) { //FIXME            s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);        }else{            s->scene_change_score+= s->qscale;        }    }    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;}