📄 me.c
字号:
/***************************************************************************** * me.c: h264 encoder library (Motion Estimation) ***************************************************************************** * Copyright (C) 2003 Laurent Aimar * $Id: me.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * Loren Merritt <lorenm@u.washington.edu> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************/#include <stdio.h>#include <string.h>#include "common/common.h"#include "me.h"/* presets selected from good points on the speed-vs-quality curve of several test videos * subpel_iters[i_subpel_refine] = { refine_hpel, refine_qpel, me_hpel, me_qpel } * where me_* are the number of EPZS iterations run on all candidate block types, * and refine_* are run only on the winner. */static const int subpel_iterations[][4] = {{1,0,0,0}, {1,1,0,0}, {0,1,1,0}, {0,2,1,0}, {0,2,1,1}, {0,2,1,2}, {0,0,2,2}, {0,0,2,2}};static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_iters, int *p_halfpel_thresh, int b_refine_qpel );#define BITS_MVD( mx, my )\ (p_cost_mvx[(mx)<<2] + p_cost_mvy[(my)<<2])#define COST_MV( mx, my )\{\ int cost = h->pixf.sad[i_pixel]( m->p_fenc[0], FENC_STRIDE,\ &p_fref[(my)*m->i_stride[0]+(mx)], m->i_stride[0] )\ + BITS_MVD(mx,my);\ COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my );\}#define COST_MV_PRED( mx, my ) \{ \ int stride = 16; \ uint8_t *src = h->mc.get_ref( m->p_fref, m->i_stride[0], pix, &stride, mx, my, bw, bh ); \ int cost = h->pixf.sad[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \ + p_cost_mvx[ mx ] + p_cost_mvy[ my ]; \ COPY3_IF_LT( bpred_cost, cost, bpred_mx, mx, bpred_my, my ); \}#define COST_MV_X3_DIR( m0x, m0y, m1x, m1y, m2x, m2y, costs )\{\ uint8_t *pix_base = p_fref + bmx + bmy*m->i_stride[0];\ h->pixf.sad_x3[i_pixel]( m->p_fenc[0],\ pix_base + (m0x) + (m0y)*m->i_stride[0],\ pix_base + (m1x) + (m1y)*m->i_stride[0],\ pix_base + (m2x) + (m2y)*m->i_stride[0],\ m->i_stride[0], costs );\ (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\ (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\ (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\}#define COST_MV_X4( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y )\{\ uint8_t *pix_base = p_fref + omx + omy*m->i_stride[0];\ h->pixf.sad_x4[i_pixel]( m->p_fenc[0],\ pix_base + (m0x) + (m0y)*m->i_stride[0],\ pix_base + (m1x) + (m1y)*m->i_stride[0],\ pix_base + (m2x) + (m2y)*m->i_stride[0],\ pix_base + (m3x) + (m3y)*m->i_stride[0],\ m->i_stride[0], costs );\ costs[0] += BITS_MVD( omx+(m0x), omy+(m0y) );\ costs[1] += BITS_MVD( omx+(m1x), omy+(m1y) );\ costs[2] += BITS_MVD( omx+(m2x), omy+(m2y) );\ costs[3] += BITS_MVD( omx+(m3x), omy+(m3y) );\ COPY3_IF_LT( bcost, costs[0], bmx, omx+(m0x), bmy, omy+(m0y) );\ COPY3_IF_LT( bcost, costs[1], bmx, omx+(m1x), bmy, omy+(m1y) );\ COPY3_IF_LT( bcost, costs[2], bmx, omx+(m2x), bmy, omy+(m2y) );\ COPY3_IF_LT( bcost, costs[3], bmx, omx+(m3x), bmy, omy+(m3y) );\}#define COST_MV_X4_ABS( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y )\{\ h->pixf.sad_x4[i_pixel]( m->p_fenc[0],\ p_fref + (m0x) + (m0y)*m->i_stride[0],\ p_fref + (m1x) + (m1y)*m->i_stride[0],\ p_fref + (m2x) + (m2y)*m->i_stride[0],\ p_fref + (m3x) + (m3y)*m->i_stride[0],\ m->i_stride[0], costs );\ costs[0] += BITS_MVD( m0x, m0y );\ costs[1] += BITS_MVD( m1x, m1y );\ costs[2] += BITS_MVD( m2x, m2y );\ costs[3] += BITS_MVD( m3x, m3y );\ COPY3_IF_LT( bcost, costs[0], bmx, m0x, bmy, m0y );\ COPY3_IF_LT( bcost, costs[1], bmx, m1x, bmy, m1y );\ COPY3_IF_LT( bcost, costs[2], bmx, m2x, bmy, m2y );\ COPY3_IF_LT( bcost, costs[3], bmx, m3x, bmy, m3y );\}/* 1 *//* 101 *//* 1 */#define DIA1_ITER( mx, my )\{\ omx = mx; omy = my;\ COST_MV_X4( 0,-1, 0,1, -1,0, 1,0 );\}#define CROSS( start, x_max, y_max )\{\ i = start;\ if( x_max <= X264_MIN(mv_x_max-omx, omx-mv_x_min) )\ for( ; i < x_max-2; i+=4 )\ COST_MV_X4( i,0, -i,0, i+2,0, -i-2,0 );\ for( ; i < x_max; i+=2 )\ {\ if( omx+i <= mv_x_max )\ COST_MV( omx+i, omy );\ if( omx-i >= mv_x_min )\ COST_MV( omx-i, omy );\ }\ i = start;\ if( y_max <= X264_MIN(mv_y_max-omy, omy-mv_y_min) )\ for( ; i < y_max-2; i+=4 )\ COST_MV_X4( 0,i, 0,-i, 0,i+2, 0,-i-2 );\ for( ; i < y_max; i+=2 )\ {\ if( omy+i <= mv_y_max )\ COST_MV( omx, omy+i );\ if( omy-i >= mv_y_min )\ COST_MV( omx, omy-i );\ }\}void x264_me_search_ref( x264_t *h, x264_me_t *m, int (*mvc)[2], int i_mvc, int *p_halfpel_thresh ){ const int bw = x264_pixel_size[m->i_pixel].w; const int bh = x264_pixel_size[m->i_pixel].h; const int i_pixel = m->i_pixel; int i_me_range = h->param.analyse.i_me_range; int bmx, bmy, bcost; int bpred_mx = 0, bpred_my = 0, bpred_cost = COST_MAX; int omx, omy, pmx, pmy; uint8_t *p_fref = m->p_fref[0]; DECLARE_ALIGNED( uint8_t, pix[16*16], 16 ); int i, j; int dir; int costs[6]; int mv_x_min = h->mb.mv_min_fpel[0]; int mv_y_min = h->mb.mv_min_fpel[1]; int mv_x_max = h->mb.mv_max_fpel[0]; int mv_y_max = h->mb.mv_max_fpel[1]; const int16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0]; const int16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1]; if( h->mb.i_me_method == X264_ME_UMH ) { /* clamp mvp to inside frame+padding, so that we don't have to check it each iteration */ p_cost_mvx = m->p_cost_mv - x264_clip3( m->mvp[0], h->mb.mv_min_spel[0], h->mb.mv_max_spel[0] ); p_cost_mvy = m->p_cost_mv - x264_clip3( m->mvp[1], h->mb.mv_min_spel[1], h->mb.mv_max_spel[1] ); } bmx = x264_clip3( m->mvp[0], mv_x_min*4, mv_x_max*4 ); bmy = x264_clip3( m->mvp[1], mv_y_min*4, mv_y_max*4 ); pmx = ( bmx + 2 ) >> 2; pmy = ( bmy + 2 ) >> 2; bcost = COST_MAX; /* try extra predictors if provided */ if( h->mb.i_subpel_refine >= 3 ) { COST_MV_PRED( bmx, bmy ); //计算搜索的起点,即中心点的cost值 for( i = 0; i < i_mvc; i++ ) { const int mx = x264_clip3( mvc[i][0], mv_x_min*4, mv_x_max*4 ); const int my = x264_clip3( mvc[i][1], mv_y_min*4, mv_y_max*4 ); if( mx != bpred_mx || my != bpred_my ) COST_MV_PRED( mx, my ); } bmx = ( bpred_mx + 2 ) >> 2; bmy = ( bpred_my + 2 ) >> 2; COST_MV( bmx, bmy ); } else { /* check the MVP */ COST_MV( pmx, pmy ); /* I don't know why this helps */ bcost -= BITS_MVD(bmx,bmy); for( i = 0; i < i_mvc; i++ ) { const int mx = x264_clip3( ( mvc[i][0] + 2 ) >> 2, mv_x_min, mv_x_max ); const int my = x264_clip3( ( mvc[i][1] + 2 ) >> 2, mv_y_min, mv_y_max ); if( mx != bmx || my != bmy ) COST_MV( mx, my ); } } COST_MV( 0, 0 ); mv_x_max += 8; mv_y_max += 8; mv_x_min -= 8; mv_y_min -= 8; switch( h->mb.i_me_method ) //开始具体的运动搜索算法 { case X264_ME_DIA: //小菱形搜索算法 /* diamond search, radius 1 */ for( i = 0; i < i_me_range; i++ ) { DIA1_ITER( bmx, bmy ); //调用COST_MV_X4计算小菱形的4个顶点的cost,并与中心点比较出最小的cost点 if( bmx == omx && bmy == omy ) break; } break; case X264_ME_HEX: //六边形搜索me_hex2: /* hexagon search, radius 2 */#if 0 for( i = 0; i < i_me_range/2; i++ ) { omx = bmx; omy = bmy; COST_MV( omx-2, omy ); COST_MV( omx-1, omy+2 ); COST_MV( omx+1, omy+2 ); COST_MV( omx+2, omy ); COST_MV( omx+1, omy-2 ); COST_MV( omx-1, omy-2 ); if( bmx == omx && bmy == omy ) break; }#else /* equivalent to the above, but eliminates duplicate candidates */ dir = -2; /* hexagon */ COST_MV_X3_DIR( -2,0, -1, 2, 1, 2, costs ); COST_MV_X3_DIR( 2,0, 1,-2, -1,-2, costs+3 ); //以上两句话是求六边形的六个点的costs值 COPY2_IF_LT( bcost, costs[0], dir, 0 ); COPY2_IF_LT( bcost, costs[1], dir, 1 ); COPY2_IF_LT( bcost, costs[2], dir, 2 ); COPY2_IF_LT( bcost, costs[3], dir, 3 ); COPY2_IF_LT( bcost, costs[4], dir, 4 ); COPY2_IF_LT( bcost, costs[5], dir, 5 ); //这六句比较出六边形六个点和中心点的具有最小cost的点的位置 if( dir != -2 ) { static const int hex2[8][2] = {{-1,-2}, {-2,0}, {-1,2}, {1,2}, {2,0}, {1,-2}, {-1,-2}, {-2,0}}; bmx += hex2[dir+1][0]; bmy += hex2[dir+1][1]; //移动六边形中心到最小cost的位置 /* half hexagon, not overlapping the previous iteration */ for( i = 1; i < i_me_range/2; i++ ) { static const int mod6[8] = {5,0,1,2,3,4,5,0}; const int odir = mod6[dir+1]; COST_MV_X3_DIR( hex2[odir+0][0], hex2[odir+0][1], hex2[odir+1][0], hex2[odir+1][1], hex2[odir+2][0], hex2[odir+2][1], costs ); dir = -2; COPY2_IF_LT( bcost, costs[0], dir, odir-1 ); COPY2_IF_LT( bcost, costs[1], dir, odir ); COPY2_IF_LT( bcost, costs[2], dir, odir+1 ); //比较出最小cost的位置 if( dir == -2 ) break; bmx += hex2[dir+1][0]; //移动六边形中心到最小cost的位置 bmy += hex2[dir+1][1]; } }#endif /* square refine */ omx = bmx; omy = bmy; COST_MV_X4( 0,-1, 0,1, -1,0, 1,0 ); COST_MV_X4( -1,-1, -1,1, 1,-1, 1,1 ); //小模板正方形搜索,计算正方形边上的8个点的cost,并和中心点的cost比较,最后得出最小的cost点,保存在(bmx,bny)里面 break; case X264_ME_UMH: //UMHexagonS搜索 { /* Uneven-cross Multi-Hexagon-grid Search * as in JM, except with different early termination */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -