转载于:http://blog.csdn.net/vblittleboy/article/details/8139486
一、 x264运动估计中宏块的位移cost计算:
/* lambda = pow(2,qp/6-2) */
const byte x264_lambda_tab[52] =
{
1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
1, 1, 1, 1, /* 8-11 */
1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
6, 7, 8, 9,10,11,13,14, /* 28-35 */
16,18,20,23,25,29,32,36, /* 36-43 */
40,45,51,57,64,72,81,91 /* 44-51 */
};
/*根据量化参数得到i_lambda的值*/
int i_qpm = 10; /*取值0 - 51; 每个宏块的qpm都是变化的,在函数x264_ratecontrol_mb中改变qpm值*/
int i_lambda = x264_lambda_tab[i_qpm];
/*计算i_lambda时所有位移的cost值*/
short *cost_mv[92];
malloc(cost_mv[i_lambda], (4*4*2048 + 1) * sizeof(short) );
cost_mv[i_lambda] += 2*4*2048;
for( i = 0; i <= 2*4*2048; i++ )
{
cost_mv[i_lambda][-i] = cost_mv[i_lambda][i]
= (short)( i_lambda * (log2f((float)( i+1 ))*2 + 0.718f + !!i) + .5f );
}
/*得到实际位移cost*/
int i_mvx = -1;
int i_mvy = 2;
int i_cost = cost_mv[i_mvx] + cost_mv[i_mvy];
二、 x264运动估计中宏块的参考帧cost计算:
/*************************************************************
static const byte x264_ue_size_tab[256] =
{
1, 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
};
static inline int bs_size_te( int x, int val )
{
if( x == 1 )
return 1;
else
return x264_ue_size_tab[val+1];
}
static inline int x264_clip3( int v, int i_min, int i_max )
{
return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
}
***************************************************************/
int i_lambda; /*i_lambda的获取同上*/
/* 92: i_lambda值; 3: 当前编码帧的参考帧数量(大于3时设为3); 33: 参考帧序号 */
static short x264_cost_ref[92][3][33];
for( int i = 0; i < 3; i++ )
{
for( int j = 0; j < 33; j++ )
{
x264_cost_ref[i_lambda][i][j] = i ? i_lambda * bs_size_te( i, j ) : 0;
}
}
int i_num_ref_idx_l0_active = h->i_ref0 <= 0 ? 1 : h->i_ref0; /* 当前编码帧的ref0参考帧数量 */
int i_num_ref_idx_l1_active = h->i_ref1 <= 0 ? 1 : h->i_ref1; /* 当前编码帧的ref1参考帧数量 */
short *p_cost_ref[2]; /* ref0和ref1 */
p_cost_ref[0] = x264_cost_ref[i_lambda][x264_clip3(i_num_ref_idx_l0_active-1,0,2)];
p_cost_ref[1] = x264_cost_ref[i_lambda][x264_clip3(i_num_ref_idx_l1_active-1,0,2)];
/*得到实际参考帧i_ref的cost*/
int i_cost_ref0 = p_cost_ref[0][i_ref];
int i_cost_ref1 = p_cost_ref[1][i_ref];
总结:(1)一个参考帧时, i_cost_ref始终为0;
(2)两个参考帧时, i_cost_ref为i_lambda;
(3)大于等于3个参考帧时, i_cost_ref要根据参考帧的序号来计算。