注:问号以及未注释部分 会在x265-1.8版本内更新
/*****************************************************************************
* Copyright (C) 2013 x265 project
*
* Authors: Steve Borho <steve@borho.org>
* Deepthi Devaki <deepthidevaki@multicorewareinc.com>
*
* 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 "slice.h"
#include "picyuv.h"
#include "reference.h"
using namespace x265;
MotionReference::MotionReference()
{
weightBuffer[0] = NULL;
weightBuffer[1] = NULL;
weightBuffer[2] = NULL;
}
MotionReference::~MotionReference()
{
X265_FREE(weightBuffer[0]);
X265_FREE(weightBuffer[1]);
X265_FREE(weightBuffer[2]);
}
/** 函数功能 : 获取参考帧信息,申请加权帧内存
/* 调用范围 : 只在FrameEncoder::compressFrame()函数中被调用
* \参数 recPic : 当前参考帧
* \参数 wp : 加权参数
* \参数 p : 配置参数
* 返回值 : 成功返回0 失败返回-1**/
int MotionReference::init(PicYuv* recPic, WeightParam *wp, const x265_param& p)
{
reconPic = recPic; //设置重构帧
numWeightedRows = 0;//初始化为0
lumaStride = recPic->m_stride;//获取步长
chromaStride = recPic->m_strideC;//获取色度步长
numInterpPlanes = p.subpelRefine > 2 ? 3 : 1; //???/* is chroma satd possible? */
/* directly reference the extended integer pel planes */
fpelPlane[0] = recPic->m_picOrg[0];//获取Y
fpelPlane[1] = recPic->m_picOrg[1];//获取U
fpelPlane[2] = recPic->m_picOrg[2];//获取V
isWeighted = false;//初始化
if (wp) //如果有加权信息
{
uint32_t numCUinHeight = (reconPic->m_picHeight + g_maxCUSize - 1) / g_maxCUSize; //一帧CTU行数
int marginX = reconPic->m_lumaMarginX;//宽度左右各扩边值
int marginY = reconPic->m_lumaMarginY;//高度上下各扩边值
intptr_t stride = reconPic->m_stride;//亮度步长
int cuHeight = g_maxCUSize;//CTU大小
for (int c = 0; c < numInterpPlanes; c++)
{
if (c == 1)//如果是色度 准备色度参量 c=2 可以复用
{
marginX = reconPic->m_chromaMarginX;//宽度左右各扩边值
marginY = reconPic->m_chromaMarginY;;//高度上下各扩边值
stride = reconPic->m_strideC;//色度步长
cuHeight >>= reconPic->m_vChromaShift;//色度CTU大小
}
if (wp[c].bPresentFlag)//如果当前需要加权
{
if (!weightBuffer[c])//如果未申请内存,申请内存
{
size_t padheight = (numCUinHeight * cuHeight) + marginY * 2;
weightBuffer[c] = X265_MALLOC(pixel, stride * padheight);
if (!weightBuffer[c])
return -1;
}
/* use our buffer which will have weighted pixels written to it */
fpelPlane[c] = weightBuffer[c] + marginY * stride + marginX;//到存储实际像素位置
X265_CHECK(recPic->m_picOrg[c] - recPic->m_picBuf[c] == marginY * stride + marginX, "PicYuv pad calculation mismatch\n");
w[c].weight = wp[c].inputWeight; //获取相应权重等信息
w[c].offset = wp[c].inputOffset * (1 << (X265_DEPTH - 8));
w[c].shift = wp[c].log2WeightDenom;
w[c].round = w[c].shift ? 1 << (w[c].shift - 1) : 0;
}
}
isWeighted = true;
}
return 0;
}
void MotionReference::applyWeight(int finishedRows, int maxNumRows)
{
finishedRows = X265_MIN(finishedRows, maxNumRows);
if (numWeightedRows >= finishedRows)
return;
int marginX = reconPic->m_lumaMarginX;
int marginY = reconPic->m_lumaMarginY;
intptr_t stride = reconPic->m_stride;
int width = reconPic->m_picWidth;
int height = (finishedRows - numWeightedRows) * g_maxCUSize;
if (finishedRows == maxNumRows && (reconPic->m_picHeight % g_maxCUSize))
{
/* the last row may be partial height */
height -= g_maxCUSize;
height += reconPic->m_picHeight % g_maxCUSize;
}
int cuHeight = g_maxCUSize;
for (int c = 0; c < numInterpPlanes; c++)
{
if (c == 1)
{
marginX = reconPic->m_chromaMarginX;
marginY = reconPic->m_chromaMarginY;
stride = reconPic->m_strideC;
width >>= reconPic->m_hChromaShift;
height >>= reconPic->m_vChromaShift;
cuHeight >>= reconPic->m_vChromaShift;
}
/* Do not generate weighted predictions if using original picture */
if (fpelPlane[c] == reconPic->m_picOrg[c])
continue;
const pixel* src = reconPic->m_picOrg[c] + numWeightedRows * cuHeight * stride;
pixel* dst = fpelPlane[c] + numWeightedRows * cuHeight * stride;
// Computing weighted CU rows
int correction = IF_INTERNAL_PREC - X265_DEPTH; // intermediate interpolation depth
int padwidth = (width + 15) & ~15; // weightp assembly needs even 16 byte widths
primitives.weight_pp(src, dst, stride, padwidth, height, w[c].weight, w[c].round << correction, w[c].shift + correction, w[c].offset);
// Extending Left & Right
primitives.extendRowBorder(dst, stride, width, height, marginX);
// Extending Above
if (numWeightedRows == 0)
{
pixel *pixY = fpelPlane[c] - marginX;
for (int y = 0; y < marginY; y++)
memcpy(pixY - (y + 1) * stride, pixY, stride * sizeof(pixel));
}
// Extending Bottom
if (finishedRows == maxNumRows)
{
int picHeight = reconPic->m_picHeight;
if (c) picHeight >>= reconPic->m_vChromaShift;
pixel *pixY = fpelPlane[c] - marginX + (picHeight - 1) * stride;
for (int y = 0; y < marginY; y++)
memcpy(pixY + (y + 1) * stride, pixY, stride * sizeof(pixel));
}
}
numWeightedRows = finishedRows;
}