⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 motion_est_template.c

📁 ffmpeg源码分析
💻 C
📖 第 1 页 / 共 3 页
字号:
        int d;        if(minima[i].checked) continue;        if(   x >= xmax || x <= xmin           || y >= ymax || y <= ymin)           continue;        SAB_CHECK_MV(x-1, y)        SAB_CHECK_MV(x+1, y)        SAB_CHECK_MV(x  , y-1)        SAB_CHECK_MV(x  , y+1)        minima[i].checked= 1;    }    best[0]= minima[0].x;    best[1]= minima[0].y;    dmin= minima[0].height;    if(   best[0] < xmax && best[0] > xmin       && best[1] < ymax && best[1] > ymin){        int d;        //ensure that the refernece samples for hpel refinement are in the map        CHECK_MV(best[0]-1, best[1])        CHECK_MV(best[0]+1, best[1])        CHECK_MV(best[0], best[1]-1)        CHECK_MV(best[0], best[1]+1)    }    return dmin;}static int var_diamond_search(MpegEncContext * s, int *best, int dmin,                                       int src_index, int ref_index, int const penalty_factor,                                       int size, int h, int flags){    MotionEstContext * const c= &s->me;    me_cmp_func cmpf, chroma_cmpf;    int dia_size;    LOAD_COMMON    LOAD_COMMON2    int map_generation= c->map_generation;    cmpf= s->dsp.me_cmp[size];    chroma_cmpf= s->dsp.me_cmp[size+1];    for(dia_size=1; dia_size<=c->dia_size; dia_size++){        int dir, start, end;        const int x= best[0];        const int y= best[1];        start= FFMAX(0, y + dia_size - ymax);        end  = FFMIN(dia_size, xmax - x + 1);        for(dir= start; dir<end; dir++){            int d;//check(x + dir,y + dia_size - dir,0, a0)            CHECK_MV(x + dir           , y + dia_size - dir);        }        start= FFMAX(0, x + dia_size - xmax);        end  = FFMIN(dia_size, y - ymin + 1);        for(dir= start; dir<end; dir++){            int d;//check(x + dia_size - dir, y - dir,0, a1)            CHECK_MV(x + dia_size - dir, y - dir           );        }        start= FFMAX(0, -y + dia_size + ymin );        end  = FFMIN(dia_size, x - xmin + 1);        for(dir= start; dir<end; dir++){            int d;//check(x - dir,y - dia_size + dir,0, a2)            CHECK_MV(x - dir           , y - dia_size + dir);        }        start= FFMAX(0, -x + dia_size + xmin );        end  = FFMIN(dia_size, ymax - y + 1);        for(dir= start; dir<end; dir++){            int d;//check(x - dia_size + dir, y + dir,0, a3)            CHECK_MV(x - dia_size + dir, y + dir           );        }        if(x!=best[0] || y!=best[1])            dia_size=0;#if 0{int dx, dy, i;static int stats[8*8];dx= ABS(x-best[0]);dy= ABS(y-best[1]);stats[dy*8 + dx] ++;if(256*256*256*64 % (stats[0]+1)==0){    for(i=0; i<64; i++){        if((i&7)==0) printf("\n");        printf("%6d ", stats[i]);    }    printf("\n");}}#endif    }    return dmin;}static always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,                                       int src_index, int ref_index, int const penalty_factor,                                       int size, int h, int flags){    MotionEstContext * const c= &s->me;    if(c->dia_size==-1)        return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);    else if(c->dia_size<-1)        return   sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);    else if(c->dia_size<2)        return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);    else        return   var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);}static always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,                             int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],                             int ref_mv_scale, int flags, int size, int h){    MotionEstContext * const c= &s->me;    int best[2]={0, 0};    int d, dmin;    int map_generation;    int penalty_factor;    const int ref_mv_stride= s->mb_stride; //pass as arg  FIXME    const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME    me_cmp_func cmpf, chroma_cmpf;    LOAD_COMMON    LOAD_COMMON2    if(c->pre_pass){        penalty_factor= c->pre_penalty_factor;        cmpf= s->dsp.me_pre_cmp[size];        chroma_cmpf= s->dsp.me_pre_cmp[size+1];    }else{        penalty_factor= c->penalty_factor;        cmpf= s->dsp.me_cmp[size];        chroma_cmpf= s->dsp.me_cmp[size+1];    }    map_generation= update_map_generation(c);    assert(cmpf);    dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);    map[0]= map_generation;    score_map[0]= dmin;    /* first line */    if (s->first_slice_line) {        CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)        CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,                        (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)    }else{        if(dmin<h*h && ( P_LEFT[0]    |P_LEFT[1]                        |P_TOP[0]     |P_TOP[1]                        |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){            *mx_ptr= 0;            *my_ptr= 0;            c->skip=1;            return dmin;        }        CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)        if(dmin>h*h*2){            CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,                            (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)            CHECK_MV(P_LEFT[0]    >>shift, P_LEFT[1]    >>shift)            CHECK_MV(P_TOP[0]     >>shift, P_TOP[1]     >>shift)            CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)        }    }    if(dmin>h*h*4){        if(c->pre_pass){            CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,                            (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)            if(!s->first_slice_line)                CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,                                (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)        }else{            CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,                            (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)            if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line                CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,                                (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)        }    }    if(c->avctx->last_predictor_count){        const int count= c->avctx->last_predictor_count;        const int xstart= FFMAX(0, s->mb_x - count);        const int ystart= FFMAX(0, s->mb_y - count);        const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);        const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);        int mb_y;        for(mb_y=ystart; mb_y<yend; mb_y++){            int mb_x;            for(mb_x=xstart; mb_x<xend; mb_x++){                const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;                int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;                int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;                if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;                CHECK_MV(mx,my)            }        }    }//check(best[0],best[1],0, b0)    dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);//check(best[0],best[1],0, b1)    *mx_ptr= best[0];    *my_ptr= best[1];//    printf("%d %d %d \n", best[0], best[1], dmin);    return dmin;}//this function is dedicated to the braindamaged gccinline int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,                             int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],                             int ref_mv_scale, int size, int h){    MotionEstContext * const c= &s->me;//FIXME convert other functions in the same way if faster    if(c->flags==0 && h==16 && size==0){        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16);//    case FLAG_QPEL://        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL);    }else{        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags, size, h);    }}static int epzs_motion_search4(MpegEncContext * s,                             int *mx_ptr, int *my_ptr, int P[10][2],                             int src_index, int ref_index, int16_t (*last_mv)[2],                             int ref_mv_scale){    MotionEstContext * const c= &s->me;    int best[2]={0, 0};    int d, dmin;    int map_generation;    const int penalty_factor= c->penalty_factor;    const int size=1;    const int h=8;    const int ref_mv_stride= s->mb_stride;    const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;    me_cmp_func cmpf, chroma_cmpf;    LOAD_COMMON    int flags= c->flags;    LOAD_COMMON2    cmpf= s->dsp.me_cmp[size];    chroma_cmpf= s->dsp.me_cmp[size+1];    map_generation= update_map_generation(c);    dmin = 1000000;//printf("%d %d %d %d //",xmin, ymin, xmax, ymax);    /* first line */    if (s->first_slice_line) {        CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)        CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,                        (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)        CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)    }else{        CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)        //FIXME try some early stop        if(dmin>64*2){            CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)            CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)            CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)            CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)            CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,                            (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)        }    }    if(dmin>64*4){        CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,                        (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)        if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line            CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,                            (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)    }    dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);    *mx_ptr= best[0];    *my_ptr= best[1];//    printf("%d %d %d \n", best[0], best[1], dmin);    return dmin;}//try to merge with above FIXME (needs PSNR test)static int epzs_motion_search2(MpegEncContext * s,                             int *mx_ptr, int *my_ptr, int P[10][2],                             int src_index, int ref_index, int16_t (*last_mv)[2],                             int ref_mv_scale){    MotionEstContext * const c= &s->me;    int best[2]={0, 0};    int d, dmin;    int map_generation;    const int penalty_factor= c->penalty_factor;    const int size=0; //FIXME pass as arg    const int h=8;    const int ref_mv_stride= s->mb_stride;    const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;    me_cmp_func cmpf, chroma_cmpf;    LOAD_COMMON    int flags= c->flags;    LOAD_COMMON2    cmpf= s->dsp.me_cmp[size];    chroma_cmpf= s->dsp.me_cmp[size+1];    map_generation= update_map_generation(c);    dmin = 1000000;//printf("%d %d %d %d //",xmin, ymin, xmax, ymax);    /* first line */    if (s->first_slice_line) {        CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)        CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,                        (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)        CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)    }else{        CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)        //FIXME try some early stop        if(dmin>64*2){            CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)            CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)            CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)            CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)            CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,                            (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)        }    }    if(dmin>64*4){        CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,                        (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)        if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line            CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,                            (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)    }    dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);    *mx_ptr= best[0];    *my_ptr= best[1];//    printf("%d %d %d \n", best[0], best[1], dmin);    return dmin;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -