Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* The copyright in this software is being made available under the BSD
* License, included below. This software may be subject to other third party
* and contributor rights, including patent rights, and no such rights are
* granted under this license.
*
* Copyright (c) 2010-2019, ITU/ISO/IEC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/** \file Reshape.cpp
\brief common reshaper class
*/
#include "Reshape.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#if JVET_O1109_UNFIY_CRS
#include <UnitTools.h>
#endif
//! \ingroup CommonLib
//! \{
// ====================================================================================================================
// Constructor / destructor / create / destroy
// ====================================================================================================================
Reshape::Reshape()
{
#if JVET_O1109_UNFIY_CRS
m_chromaScale = (1 << CSCALE_FP_PREC);
#endif
m_lumaBD = bitDepth;
m_reshapeLUTSize = 1 << m_lumaBD;
m_initCW = m_reshapeLUTSize / PIC_CODE_CW_BINS;
if (m_fwdLUT.empty())
m_fwdLUT.resize(m_reshapeLUTSize, 0);
if (m_invLUT.empty())
m_invLUT.resize(m_reshapeLUTSize, 0);
if (m_binCW.empty())
m_binCW.resize(PIC_CODE_CW_BINS, 0);
if (m_reshapePivot.empty())
m_reshapePivot.resize(PIC_CODE_CW_BINS + 1, 0);
if (m_chromaAdjHelpLUT.empty())
m_chromaAdjHelpLUT.resize(PIC_CODE_CW_BINS, 1<<CSCALE_FP_PREC);
}
void Reshape::destroy()
{
}
/**
-Perform inverse of a one dimension LUT
\param InputLUT describing the input LUT
\retval OutputLUT describing the inversed LUT of InputLUT
\param lut_size size of LUT in number of samples
*/
void Reshape::reverseLUT(std::vector<Pel>& inputLUT, std::vector<Pel>& outputLUT, uint16_t lutSize)
outputLUT[m_reshapePivot[m_sliceReshapeInfo.reshaperModelMinBinIdx]] = m_sliceReshapeInfo.reshaperModelMinBinIdx*m_initCW;
for (i = m_sliceReshapeInfo.reshaperModelMinBinIdx; i <= m_sliceReshapeInfo.reshaperModelMaxBinIdx; i++)
int16_t X1 = m_reshapePivot[i];
int16_t X2 = m_reshapePivot[i + 1];
outputLUT[X2] = (i + 1)*m_initCW;
int16_t Y1 = outputLUT[X1];
int16_t Y2 = outputLUT[X2];
if (X2 !=X1)
{
int32_t scale = (int32_t)(Y2 - Y1) * (1 << FP_PREC) / (int32_t)(X2 - X1);
for (j = X1 + 1; j < X2; j++)
{
outputLUT[j] = (Pel)((scale*(int32_t)(j - X1) + (1 << (FP_PREC - 1))) >> FP_PREC) + Y1;
for (i = 0; i < m_reshapePivot[m_sliceReshapeInfo.reshaperModelMinBinIdx]; i++)
outputLUT[i] = outputLUT[m_reshapePivot[m_sliceReshapeInfo.reshaperModelMinBinIdx]];
for (i = m_reshapePivot[m_sliceReshapeInfo.reshaperModelMaxBinIdx + 1]; i < m_reshapeLUTSize; i++)
outputLUT[i] = outputLUT[m_reshapePivot[m_sliceReshapeInfo.reshaperModelMaxBinIdx + 1]];
for (i = 0; i < lutSize; i++)
{
outputLUT[i] = Clip3((Pel)0, (Pel)((1<<m_lumaBD)-1), outputLUT[i]);
}
}
/** compute chroma residuce scale for TU
* \param average luma pred of TU
* \return chroma residue scale
*/
int Reshape::calculateChromaAdj(Pel avgLuma)
{
int lumaIdx = Clip3<int>(0, (1<<m_lumaBD) - 1, avgLuma);
int iAdj = m_chromaAdjHelpLUT[getPWLIdxInv(lumaIdx)];
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#if JVET_O1109_UNFIY_CRS
/** compute chroma residuce scale for TU
* \param average luma pred of TU
* \return chroma residue scale
*/
int Reshape::calculateChromaAdjVpduNei(TransformUnit &tu, const CompArea &areaY)
{
CodingStructure &cs = *tu.cs;
int xPos = areaY.lumaPos().x;
int yPos = areaY.lumaPos().y;
int ctuSize = cs.sps->getCTUSize();
if (ctuSize == 128)
{
xPos = xPos / 64 * 64;
yPos = yPos / 64 * 64;
}
else
{
xPos = xPos / ctuSize * ctuSize;
yPos = yPos / ctuSize * ctuSize;
}
if (isVPDUprocessed(xPos, yPos) && !cs.pcv->isEncoder)
{
return getChromaScale();
}
else
{
setVPDULoc(xPos, yPos);
Position topLeft(xPos, yPos);
CodingUnit *topLeftLuma;
if (CS::isDualITree(cs) && cs.slice->getSliceType() == I_SLICE)
topLeftLuma = tu.cs->picture->cs->getCU(topLeft, CHANNEL_TYPE_LUMA);
else
topLeftLuma = cs.getCU(topLeft, CHANNEL_TYPE_LUMA);
xPos = topLeftLuma->lumaPos().x;
yPos = topLeftLuma->lumaPos().y;
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
CompArea lumaArea = CompArea(COMPONENT_Y, tu.chromaFormat, topLeftLuma->lumaPos(), topLeftLuma->lumaSize(), true);
PelBuf piRecoY = cs.picture->getRecoBuf(lumaArea);
int strideY = piRecoY.stride;
int chromaScale = (1 << CSCALE_FP_PREC);
int lumaValue = -1;
Pel* recSrc0 = piRecoY.bufAt(0, 0);
const uint32_t picH = tu.cs->picture->lheight();
const uint32_t picW = tu.cs->picture->lwidth();
const Pel valueDC = 1 << (tu.cs->sps->getBitDepth(CHANNEL_TYPE_LUMA) - 1);
int32_t recLuma = 0;
int pelnum = 0;
if (xPos > 0)
{
for (int i = 0; i < NEIG_NUM; i++)
{
int k = (yPos + i) >= picH ? (picH - yPos - 1) : i;
recLuma += recSrc0[-1 + k * strideY];
pelnum++;
}
}
if (yPos > 0)
{
for (int i = 0; i < NEIG_NUM; i++)
{
int k = (xPos + i) >= picW ? (picW - xPos - 1) : i;
recLuma += recSrc0[-strideY + k];
pelnum++;
}
}
if (pelnum == NEIG_NUM)
{
lumaValue = ClipPel((recLuma + (1 << (NEIG_NUM_LOG - 1))) >> NEIG_NUM_LOG, tu.cs->slice->clpRng(COMPONENT_Y));
}
else if (pelnum == (NEIG_NUM << 1))
{
lumaValue = ClipPel((recLuma + (1 << NEIG_NUM_LOG)) >> (NEIG_NUM_LOG + 1), tu.cs->slice->clpRng(COMPONENT_Y));
}
else
{
CHECK(pelnum != 0, "");
lumaValue = ClipPel(valueDC, tu.cs->slice->clpRng(COMPONENT_Y));
}
chromaScale = calculateChromaAdj(lumaValue);
setChromaScale(chromaScale);
return(chromaScale);
}
}
#endif
/** find inx of PWL for inverse mapping
* \param average luma pred of TU
* \return idx of PWL for inverse mapping
*/
int Reshape::getPWLIdxInv(int lumaVal)
{
int idxS = 0;
if (lumaVal < m_reshapePivot[m_sliceReshapeInfo.reshaperModelMinBinIdx + 1])
return m_sliceReshapeInfo.reshaperModelMinBinIdx;
else if (lumaVal >= m_reshapePivot[m_sliceReshapeInfo.reshaperModelMaxBinIdx])
return m_sliceReshapeInfo.reshaperModelMaxBinIdx;
for (idxS = m_sliceReshapeInfo.reshaperModelMinBinIdx; (idxS < m_sliceReshapeInfo.reshaperModelMaxBinIdx); idxS++)
if (lumaVal < m_reshapePivot[idxS + 1]) break;
}
return idxS;
}
}
/**
-copy Slice reshaper info structure
\param tInfo describing the target Slice reshaper info structure
\param sInfo describing the source Slice reshaper info structure
*/
void Reshape::copySliceReshaperInfo(SliceReshapeInfo& tInfo, SliceReshapeInfo& sInfo)
tInfo.sliceReshaperModelPresentFlag = sInfo.sliceReshaperModelPresentFlag;
if (sInfo.sliceReshaperModelPresentFlag)
tInfo.reshaperModelMaxBinIdx = sInfo.reshaperModelMaxBinIdx;
tInfo.reshaperModelMinBinIdx = sInfo.reshaperModelMinBinIdx;
memcpy(tInfo.reshaperModelBinCWDelta, sInfo.reshaperModelBinCWDelta, sizeof(int)*(PIC_CODE_CW_BINS));
tInfo.maxNbitsNeededDeltaCW = sInfo.maxNbitsNeededDeltaCW;
}
tInfo.sliceReshaperEnableFlag = sInfo.sliceReshaperEnableFlag;
if (sInfo.sliceReshaperEnableFlag)
tInfo.enableChromaAdj = sInfo.enableChromaAdj;
}
/** Construct reshaper from syntax
* \param void
* \return void
*/
void Reshape::constructReshaper()
{
int pwlFwdLUTsize = PIC_CODE_CW_BINS;
int pwlFwdBinLen = m_reshapeLUTSize / PIC_CODE_CW_BINS;
for (int i = m_sliceReshapeInfo.reshaperModelMaxBinIdx + 1; i < PIC_CODE_CW_BINS; i++)
for (int i = m_sliceReshapeInfo.reshaperModelMinBinIdx; i <= m_sliceReshapeInfo.reshaperModelMaxBinIdx; i++)
m_binCW[i] = (uint16_t)(m_sliceReshapeInfo.reshaperModelBinCWDelta[i] + (int)m_initCW);
for (int i = 0; i < pwlFwdLUTsize; i++)
{
m_reshapePivot[i + 1] = m_reshapePivot[i] + m_binCW[i];
int16_t Y1 = m_reshapePivot[i];
int16_t Y2 = m_reshapePivot[i + 1];
m_fwdLUT[i*pwlFwdBinLen] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)Y1);
int log2PwlFwdBinLen = floorLog2(pwlFwdBinLen);
int32_t scale = ((int32_t)(Y2 - Y1) * (1 << FP_PREC) + (1 << (log2PwlFwdBinLen - 1))) >> (log2PwlFwdBinLen);
for (int j = 1; j < pwlFwdBinLen; j++)
{
int tempVal = Y1 + (((int32_t)scale * (int32_t)j + (1 << (FP_PREC - 1))) >> FP_PREC);
m_fwdLUT[i*pwlFwdBinLen + j] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)tempVal);
reverseLUT(m_fwdLUT, m_invLUT, m_reshapeLUTSize);
updateChromaScaleLUT();
}
/** generate chroma residue scaling LUT
* \param void
* \return void
*/
m_chromaAdjHelpLUT[i] = 1 << CSCALE_FP_PREC;
else
m_chromaAdjHelpLUT[i] = m_initCW * (1 << CSCALE_FP_PREC) / m_binCW[i];