注:问号以及未注释部分 会在x265-1.8版本内更新
/*****************************************************************************
* Copyright (C) 2013 x265 project
*
* Authors: Steve Borho <steve@borho.org>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
*
* This program is also available under a commercial proprietary license.
* For more information, contact us at license @ x265.com.
*****************************************************************************/
#include "common.h"
#include "primitives.h"
#include "bitcost.h"
using namespace x265;
/** 函数功能 :建立当前qp下的MVD占用的cost:λ*bits = 2^(qp/6-2) * s_bitsizes[i]
* \参数 qp :当前ME下的qp大小
* \返回 :null * */
void BitCost::setQP(unsigned int qp)
{
if (!s_costs[qp]) //如果当前还没有申请空间并初始化,在下面将其申请空间并初始化 s_costs,所有的me共有,每个qp只初始化一次
{
ScopedLock s(s_costCalcLock); //加锁防止多线程读取脏数据
// Now that we have acquired the lock, check again if another thread calculated
// this row while we were blocked
if (!s_costs[qp])
{
x265_emms(); // just to be safe 清除MMX寄存器中的内容,即初始化(以避免和浮点数操作发生冲突)。
CalculateLogs(); //建立MVD估计占用bits表格,例如:当前MV.x = 3 ,则其占用bits 为 s_bitsizes[3] 2*(log2(3+1))+e-1
s_costs[qp] = new uint16_t[4 * BC_MAX_MV + 1] + 2 * BC_MAX_MV; //申请空间为4*32768+1 并指向当前地址2*32768
double lambda = x265_lambda_tab[qp]; //2^(qp/6-2)
// estimate same cost for negative and positive MVD
for (int i = 0; i <= 2 * BC_MAX_MV; i++)
s_costs[qp][i] = s_costs[qp][-i] = (uint16_t)X265_MIN(s_bitsizes[i] * lambda + 0.5f, (1 << 16) - 1);//其值为 s_bitsizes[i] * lambda 四舍五入取整
}
}
m_cost = s_costs[qp]; //将m_cost指针指向当前qp所对应的MVD cost表(MVD为0的位置),在计算MVD的cost就是通过MVD寻址m_cost表
//在setMVP()中,将m_cost_mvx/m_cost_mvy赋值为MVP的cost表地址,在ME中根据MVD来寻址m_cost_mvx/m_cost_mvy,即可以得到mvx/mvy的cost
}
/***
* Class static data and methods
*/
uint16_t *BitCost::s_costs[BC_MAX_QP]; //所有的me共有,每个qp只初始化一次
float *BitCost::s_bitsizes;//申请空间大小为 2*32768+1 MVD估计占用bits表格,例如:当前MV.x = 3 ,则其占用bits 为 s_bitsizes[3] 2*(log2(3+1))+e-1
/*
s_bitsizes[0] = e - 2 s_bitsizes[i]= 2*(log2(i+1))+e-1 log2 表示以2为底
s_bitsizes[1] = 2log2(2) + e -1
**/
Lock BitCost::s_costCalcLock;//多线程锁
/** 函数功能 :建立MVD估计占用bits表格,例如:当前MV.x = 3 ,则其占用bits 为 s_bitsizes[3] 2*(log2(3+1))+e-1
* \返回 :null * */
void BitCost::CalculateLogs()
{
if (!s_bitsizes) //没有申请空间,在此申请,所有ME共有,所以一般只进入一次
{
s_bitsizes = new float[2 * BC_MAX_MV + 1]; //空间大小:32786*2+1
s_bitsizes[0] = 0.718f;
float log2_2 = 2.0f / log(2.0f); // 2 x 1/log(2) double log (double); 以e为底的对数 e = 2.718281828459
for (int i = 1; i <= 2 * BC_MAX_MV; i++)
s_bitsizes[i] = log((float)(i + 1)) * log2_2 + 1.718f;
/*
s_bitsizes[0] = e - 2, s_bitsizes[i]= 2*(log2(i+1))+e-1, log2表示以2为底
s_bitsizes[1] = 2log2(2) + e -1
**/
}
}
/** 函数功能 : 释放空间内存
* \返回 : null */
void BitCost::destroy()
{
for (int i = 0; i < BC_MAX_QP; i++)
{
if (s_costs[i])
{
delete [] (s_costs[i] - 2 * BC_MAX_MV);
s_costs[i] = 0;
}
}
delete [] s_bitsizes;
s_bitsizes = 0;
}