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, mx = 0, my = 0, dmin = 0;
    int varc;
    int vard;
    int P[10][2];
    const int shift= 1+s->quarter_sample;
    int mb_type=0;
    Picture * const pic= &s->current_picture;
    init_ref(c, s->new_picture.f->data, s->last_picture.f->data, NULL, 16*mb_x, 16*mb_y, 0);
    assert(s->quarter_sample==0 || s->quarter_sample==1);
    assert(s->linesize == c->stride);
    assert(s->uvlinesize == c->uvstride);
    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
    get_limits(s, 16*mb_x, 16*mb_y);
    c->skip=0;
    pix = c->src[0][0];
    sum  = s->mpvencdsp.pix_sum(pix, s->linesize);
    varc = s->mpvencdsp.pix_norm1(pix, s->linesize) -
           (((unsigned) sum * sum) >> 8) + 500;
    pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
    pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
    c->mb_var_sum_temp += (varc+128)>>8;
    if (s->motion_est != FF_ME_ZERO) {
        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] > (c->xmax << shift))
            P_LEFT[0] =  c->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] > (c->ymax << shift))
                P_TOP[1] =  c->ymax << shift;
            if (P_TOPRIGHT[0] < (c->xmin << shift))
                P_TOPRIGHT[0] =  c->xmin << shift;
            if (P_TOPRIGHT[1] > (c->ymax << shift))
                P_TOPRIGHT[1] =  c->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 {
                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 = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
    }
    ppix = c->ref[0][0] + (my * s->linesize) + mx;
    vard = s->mecc.sse[0](NULL, pix, ppix, s->linesize, 16);
    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
    c->mc_mb_var_sum_temp += (vard+128)>>8;
    if (c->avctx->mb_decision > FF_MB_DECISION_SIMPLE) {
        int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
        int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
        c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
        if (vard*2 + 200*256 > varc)
            mb_type|= CANDIDATE_MB_TYPE_INTRA;
        if (varc*2 + 200*256 > vard || s->qscale > 24){
            mb_type|= CANDIDATE_MB_TYPE_INTER;
            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
            if (s->mpv_flags & FF_MPV_FLAG_MV0)
                if(mx || my)
                    mb_type |= CANDIDATE_MB_TYPE_SKIPPED;
        }else{
            mx <<=shift;
            my <<=shift;
        }
        if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
           && !c->skip && varc>50<<8 && vard>10<<8){
            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->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
           && !c->skip){
            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
                mb_type |= CANDIDATE_MB_TYPE_INTER_I;
        }
    }else{
        int intra_score, i;
        mb_type= CANDIDATE_MB_TYPE_INTER;
        dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
        if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
            dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
        if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
           && !c->skip && varc>50<<8 && vard>10<<8){
            int dmin4= h263_mv4_search(s, mx, my, shift);
            if(dmin4 < dmin){
                mb_type= CANDIDATE_MB_TYPE_INTER4V;
                dmin=dmin4;
            }
        }
        if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
           && !c->skip){
            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
            if(dmin_i < dmin){
                mb_type = CANDIDATE_MB_TYPE_INTER_I;
                dmin= dmin_i;
            }
        }
        set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
        if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
            intra_score= varc - 500;
        }else{
            unsigned mean = (sum+128)>>8;
            mean*= 0x01010101;
            for(i=0; i<16; i++){
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
                *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
            }
            intra_score= s->mecc.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
        }
        intra_score += c->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;
        }else
            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = 0;
        {
            int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
            int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
            c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
        }
    }
    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
}
['void ff_estimate_p_frame_motion(MpegEncContext * s,\n                                int mb_x, int mb_y)\n{\n    MotionEstContext * const c= &s->me;\n    uint8_t *pix, *ppix;\n    int sum, mx = 0, my = 0, dmin = 0;\n    int varc;\n    int vard;\n    int P[10][2];\n    const int shift= 1+s->quarter_sample;\n    int mb_type=0;\n    Picture * const pic= &s->current_picture;\n    init_ref(c, s->new_picture.f->data, s->last_picture.f->data, NULL, 16*mb_x, 16*mb_y, 0);\n    assert(s->quarter_sample==0 || s->quarter_sample==1);\n    assert(s->linesize == c->stride);\n    assert(s->uvlinesize == c->uvstride);\n    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);\n    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);\n    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);\n    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n    get_limits(s, 16*mb_x, 16*mb_y);\n    c->skip=0;\n    pix = c->src[0][0];\n    sum  = s->mpvencdsp.pix_sum(pix, s->linesize);\n    varc = s->mpvencdsp.pix_norm1(pix, s->linesize) -\n           (((unsigned) sum * sum) >> 8) + 500;\n    pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;\n    pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;\n    c->mb_var_sum_temp += (varc+128)>>8;\n    if (s->motion_est != FF_ME_ZERO) {\n        const int mot_stride = s->b8_stride;\n        const int mot_xy = s->block_index[0];\n        P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];\n        P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];\n        if (P_LEFT[0] > (c->xmax << shift))\n            P_LEFT[0] =  c->xmax << shift;\n        if (!s->first_slice_line) {\n            P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];\n            P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];\n            P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];\n            P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];\n            if (P_TOP[1] > (c->ymax << shift))\n                P_TOP[1] =  c->ymax << shift;\n            if (P_TOPRIGHT[0] < (c->xmin << shift))\n                P_TOPRIGHT[0] =  c->xmin << shift;\n            if (P_TOPRIGHT[1] > (c->ymax << shift))\n                P_TOPRIGHT[1] =  c->ymax << shift;\n            P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);\n            P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);\n            if (s->out_format == FMT_H263) {\n                c->pred_x = P_MEDIAN[0];\n                c->pred_y = P_MEDIAN[1];\n            } else {\n                c->pred_x = P_LEFT[0];\n                c->pred_y = P_LEFT[1];\n            }\n        } else {\n            c->pred_x = P_LEFT[0];\n            c->pred_y = P_LEFT[1];\n        }\n        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);\n    }\n    ppix = c->ref[0][0] + (my * s->linesize) + mx;\n    vard = s->mecc.sse[0](NULL, pix, ppix, s->linesize, 16);\n    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;\n    c->mc_mb_var_sum_temp += (vard+128)>>8;\n    if (c->avctx->mb_decision > FF_MB_DECISION_SIMPLE) {\n        int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);\n        int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;\n        c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);\n        if (vard*2 + 200*256 > varc)\n            mb_type|= CANDIDATE_MB_TYPE_INTRA;\n        if (varc*2 + 200*256 > vard || s->qscale > 24){\n            mb_type|= CANDIDATE_MB_TYPE_INTER;\n            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);\n            if (s->mpv_flags & FF_MPV_FLAG_MV0)\n                if(mx || my)\n                    mb_type |= CANDIDATE_MB_TYPE_SKIPPED;\n        }else{\n            mx <<=shift;\n            my <<=shift;\n        }\n        if ((s->avctx->flags & AV_CODEC_FLAG_4MV)\n           && !c->skip && varc>50<<8 && vard>10<<8){\n            if(h263_mv4_search(s, mx, my, shift) < INT_MAX)\n                mb_type|=CANDIDATE_MB_TYPE_INTER4V;\n            set_p_mv_tables(s, mx, my, 0);\n        }else\n            set_p_mv_tables(s, mx, my, 1);\n        if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)\n           && !c->skip){\n            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)\n                mb_type |= CANDIDATE_MB_TYPE_INTER_I;\n        }\n    }else{\n        int intra_score, i;\n        mb_type= CANDIDATE_MB_TYPE_INTER;\n        dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);\n        if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)\n            dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);\n        if ((s->avctx->flags & AV_CODEC_FLAG_4MV)\n           && !c->skip && varc>50<<8 && vard>10<<8){\n            int dmin4= h263_mv4_search(s, mx, my, shift);\n            if(dmin4 < dmin){\n                mb_type= CANDIDATE_MB_TYPE_INTER4V;\n                dmin=dmin4;\n            }\n        }\n        if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)\n           && !c->skip){\n            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);\n            if(dmin_i < dmin){\n                mb_type = CANDIDATE_MB_TYPE_INTER_I;\n                dmin= dmin_i;\n            }\n        }\n        set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);\n        if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){\n            intra_score= varc - 500;\n        }else{\n            unsigned mean = (sum+128)>>8;\n            mean*= 0x01010101;\n            for(i=0; i<16; i++){\n                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;\n                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;\n                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;\n                *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;\n            }\n            intra_score= s->mecc.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);\n        }\n        intra_score += c->mb_penalty_factor*16;\n        if(intra_score < dmin){\n            mb_type= CANDIDATE_MB_TYPE_INTRA;\n            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA;\n        }else\n            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = 0;\n        {\n            int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);\n            int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;\n            c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);\n        }\n    }\n    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;\n}', 'static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){\n    const int offset[3]= {\n          y*c->  stride + x,\n        ((y*c->uvstride + x)>>1),\n        ((y*c->uvstride + x)>>1),\n    };\n    int i;\n    for(i=0; i<3; i++){\n        c->src[0][i]= src [i] + offset[i];\n        c->ref[0][i]= ref [i] + offset[i];\n    }\n    if(ref_index){\n        for(i=0; i<3; i++){\n            c->ref[ref_index][i]= ref2[i] + offset[i];\n        }\n    }\n}']