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
41
42
43
44
45
46
47
48
49
50
/* 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 EncReshape.cpp
\brief encoder reshaper class
*/
#include "EncReshape.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#if JVET_M0427_INLOOP_RESHAPER
//! \ingroup EncLib
//! \{
// ====================================================================================================================
// Constructor / destructor / create / destroy
// ====================================================================================================================
EncReshape::EncReshape()
{
m_CTUFlag = false;
m_srcReshaped = false;
m_recReshaped = false;
m_reshape = true;
m_exceedSTD = false;
m_tcase = 0;
m_rateAdpMode = 0;
m_chromaAdj = 0;
}
EncReshape::~EncReshape()
{
}
void EncReshape::createEnc(int picWidth, int picHeight, uint32_t maxCUWidth, uint32_t maxCUHeight, int bitDepth)
m_lumaBD = bitDepth;
m_reshapeLUTSize = 1 << m_lumaBD;
m_initCWAnalyze = m_reshapeLUTSize / PIC_ANALYZE_CW_BINS;
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_ANALYZE_CW_BINS);
if (m_binImportance.empty())
m_binImportance.resize(PIC_ANALYZE_CW_BINS);
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);
m_sliceReshapeInfo.setUseSliceReshaper(true);
m_sliceReshapeInfo.setSliceReshapeChromaAdj(true);
m_sliceReshapeInfo.setSliceReshapeModelPresentFlag(true);
m_sliceReshapeInfo.reshaperModelMinBinIdx = 0;
m_sliceReshapeInfo.reshaperModelMaxBinIdx = PIC_CODE_CW_BINS - 1;
memset(m_sliceReshapeInfo.reshaperModelBinCWDelta, 0, (PIC_CODE_CW_BINS) * sizeof(int));
m_picWidth = picWidth;
m_picHeight = picHeight;
m_maxCUWidth = maxCUWidth;
m_maxCUHeight = maxCUHeight;
m_widthInCtus = (m_picWidth + m_maxCUWidth - 1) / m_maxCUWidth;
m_heightInCtus = (m_picHeight + m_maxCUHeight - 1) / m_maxCUHeight;
m_numCtuInFrame = m_widthInCtus * m_heightInCtus;
}
void EncReshape::destroy()
{
}
/**
-Perform HDR set up
\param pcPic describe pointer of current coding picture
\param sliceType describe the slice type
*/
void EncReshape::preAnalyzerHDR(Picture *pcPic, const SliceType sliceType, const ReshapeCW& reshapeCW, bool isDualT, bool isCPR)
{
m_sliceReshapeInfo.sliceReshaperEnableFlag = true;
if (reshapeCW.rspIntraPeriod == 1)
if (pcPic->getPOC() == 0) { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = true; }
else { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false; }
if (sliceType == I_SLICE || (sliceType==P_SLICE && isCPR) ) { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = true; }
else { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false; }
if ((sliceType == I_SLICE || (sliceType == P_SLICE && isCPR)) && isDualT) { m_sliceReshapeInfo.enableChromaAdj = 0; }
else { m_sliceReshapeInfo.enableChromaAdj = 1; }
m_sliceReshapeInfo.sliceReshaperEnableFlag = false;
m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false;
}
}
/**
-Perform picture analysis for SDR
\param pcPic describe pointer of current coding picture
\param sliceType describe the slice type
\param reshapeCW describe some input info
*/
void EncReshape::preAnalyzerSDR(Picture *pcPic, const SliceType sliceType, const ReshapeCW& reshapeCW, bool isDualT, bool isCPR)
{
m_sliceReshapeInfo.sliceReshaperModelPresentFlag = true;
m_sliceReshapeInfo.sliceReshaperEnableFlag = true;
int modIP = pcPic->getPOC() - pcPic->getPOC() / reshapeCW.rspFpsToIp * reshapeCW.rspFpsToIp;
if (sliceType == I_SLICE || (reshapeCW.rspIntraPeriod == -1 && modIP == 0) || (sliceType== P_SLICE && isCPR))
int stdMin = 16 <<(m_lumaBD-8);
int stdMax = 235 << (m_lumaBD - 8);
int binLen = m_reshapeLUTSize / PIC_ANALYZE_CW_BINS;
m_reshapeCW = reshapeCW;
for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
{
int startBinIdx = stdMin / binLen;
int endBinIdx = stdMax / binLen;
m_sliceReshapeInfo.reshaperModelMinBinIdx = startBinIdx;
m_sliceReshapeInfo.reshaperModelMaxBinIdx = endBinIdx;
PelBuf picY = pcPic->getOrigBuf(COMPONENT_Y);
const int width = picY.width;
const int height = picY.height;
const int stride = picY.stride;
double blockBinVarSum[PIC_ANALYZE_CW_BINS] = { 0.0 };
uint32_t bockBinCnt[PIC_ANALYZE_CW_BINS] = { 0 };
const int PIC_ANALYZE_WIN_SIZE = 5;
const uint32_t uiWinSize = PIC_ANALYZE_WIN_SIZE;
const uint32_t uiWinLens = (uiWinSize - 1) >> 1;
int64_t tempSq = 0;
int64_t leftSum = 0, leftSumSq = 0;
int64_t *leftColSum = new int64_t[width];
int64_t *leftColSumSq = new int64_t[width];
memset(leftColSum, 0, width * sizeof(int64_t));
memset(leftColSumSq, 0, width * sizeof(int64_t));
int64_t *topRowSum = new int64_t[height];
int64_t *topRowSumSq = new int64_t[height];
memset(topRowSum, 0, height * sizeof(int64_t));
memset(topRowSumSq, 0, height * sizeof(int64_t));
int64_t *topColSum = new int64_t[width];
int64_t *topColSumSq = new int64_t[width];
memset(topColSum, 0, width * sizeof(int64_t));
memset(topColSumSq, 0, width * sizeof(int64_t));
for (uint32_t y = 0; y < height; y++)
const Pel pxlY = picY.buf[x];
int64_t sum = 0;
int64_t sumSq = 0;
uint32_t numPixInPart = 0;
uint32_t y1 = std::max((int)(y - uiWinLens), 0);
uint32_t x1 = std::max((int)(x - uiWinLens), 0);
uint32_t bx = 0, by = 0;
const Pel *pWinY = &picY.buf[0];
if (x == 0 && y == 0) // for the 1st Pixel, calc all points
{
for (by = y1; by <= y2; by++)
{
for (bx = x1; bx <= x2; bx++)
{
tempSq = pWinY[bx] * pWinY[bx];
leftSum += pWinY[bx];
leftSumSq += tempSq;
leftColSum[bx] += pWinY[bx];
leftColSumSq[bx] += tempSq;
topColSum[bx] += pWinY[bx];
topColSumSq[bx] += tempSq;
topRowSum[by] += pWinY[bx];
topRowSumSq[by] += tempSq;
}
}
topSum = leftSum;
topSumSq = leftSumSq;
}
else if (x == 0 && y > 0) // for the 1st column, calc the bottom stripe
{
topRowSum[y + uiWinLens] = 0;
topRowSumSq[y + uiWinLens] = 0;
for (bx = x1; bx <= x2; bx++)
{
topRowSum[y + uiWinLens] += pWinY[bx];
topRowSumSq[y + uiWinLens] += pWinY[bx] * pWinY[bx];
}
topSum += topRowSum[y + uiWinLens];
topSumSq += topRowSumSq[y + uiWinLens];
}
if (y > uiWinLens)
{
topSum -= topRowSum[y - 1 - uiWinLens];
topSumSq -= topRowSumSq[y - 1 - uiWinLens];
}
memset(leftColSum, 0, width * sizeof(int64_t));
memset(leftColSumSq, 0, width * sizeof(int64_t));
for (by = y1; by <= y2; by++)
{
for (bx = x1; bx <= x2; bx++)
{
leftColSum[bx] += pWinY[bx];
leftColSumSq[bx] += pWinY[bx] * pWinY[bx];
}
}
leftSum = topSum;
leftSumSq = topSumSq;
if (y == 0) // for the 1st row, calc the right stripe
{
leftColSum[x + uiWinLens] = 0;
leftColSumSq[x + uiWinLens] = 0;
for (by = y1; by <= y2; by++)
{
leftColSum[x + uiWinLens] += pWinY[x + uiWinLens];
leftColSumSq[x + uiWinLens] += pWinY[x + uiWinLens] * pWinY[x + uiWinLens];
}
}
else // for the main area, calc the B-R point
{
leftColSum[x + uiWinLens] = topColSum[x + uiWinLens];
leftColSumSq[x + uiWinLens] = topColSumSq[x + uiWinLens];
leftColSum[x + uiWinLens] += pWinY[x + uiWinLens];
leftColSumSq[x + uiWinLens] += pWinY[x + uiWinLens] * pWinY[x + uiWinLens];
}
if (y > uiWinLens)
{
pWinY = &picY.buf[0];
leftColSum[x + uiWinLens] -= pWinY[x + uiWinLens];
leftColSumSq[x + uiWinLens] -= pWinY[x + uiWinLens] * pWinY[x + uiWinLens];
}
}
topColSum[x + uiWinLens] = leftColSum[x + uiWinLens];
topColSumSq[x + uiWinLens] = leftColSumSq[x + uiWinLens];
leftSum += leftColSum[x + uiWinLens];
leftSumSq += leftColSumSq[x + uiWinLens];
}
if (x > uiWinLens)
{
leftSum -= leftColSum[x - 1 - uiWinLens];
leftSumSq -= leftColSumSq[x - 1 - uiWinLens];
}
double average = double(sum) / numPixInPart;
double variance = double(sumSq) / numPixInPart - average * average;
average = average / (double)(1<<(m_lumaBD - 10));
variance = variance / (double)(1 << (2*m_lumaBD - 20));
average = average * (double)(1 << (10 - m_lumaBD));
variance = variance * (double)(1 << (20-2*m_lumaBD));
uint32_t uiBinNum = (uint32_t)(pxlY/PIC_ANALYZE_CW_BINS);
}
delete[] topColSum;
delete[] topColSumSq;
delete[] topRowSum;
delete[] topRowSumSq;
delete[] leftColSum;
delete[] leftColSumSq;
for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
{
if (bockBinCnt[b] > 0)
blockBinVarSum[b] = blockBinVarSum[b] / bockBinCnt[b];
m_sliceReshapeInfo.enableChromaAdj = 1;
bool intraAdp = false;
bool interAdp = true;
double reshapeTH1 = 0.0;
double reshapeTH2 = 5.0;
deriveReshapeParametersSDRfromStats(bockBinCnt, blockBinVarSum, &reshapeTH1, &reshapeTH2, &intraAdp, &interAdp);
if (m_rateAdpMode == 2 && reshapeCW.rspBaseQP <= 22)
m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false;
m_reshape = false;
for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
{
m_sliceReshapeInfo.reshaperModelMinBinIdx = startBinIdx;
m_sliceReshapeInfo.reshaperModelMaxBinIdx = endBinIdx;
{
for (int i = 0; i < PIC_ANALYZE_CW_BINS; i++)
{
if (i >= startBinIdx && i <= endBinIdx)
m_binCW[i] = m_initCWAnalyze + 1;
deriveReshapeParameters(blockBinVarSum, startBinIdx, endBinIdx, m_reshapeCW, Alpha, Beta);
for (int i = 0; i < PIC_ANALYZE_CW_BINS; i++)
{
if (i >= startBinIdx && i <= endBinIdx)
for (int b = startBinIdx; b <= endBinIdx; b++)
if (blockBinVarSum[b] < reshapeTH1)
m_binImportance[b] = 2;
else if (blockBinVarSum[b] > reshapeTH2)
m_binImportance[b] = 3;
}
for (int i = 0; i < PIC_ANALYZE_CW_BINS; i++)
{
else if (m_binImportance[i] == 2)
m_binCW[i] = m_reshapeCW.binCW[0];
else if (m_binImportance[i] == 3)
m_binCW[i] = m_reshapeCW.binCW[1];
else
THROW("SDR Reshape Bin Importance not supported");
}
}
if (m_reshapeCW.rspPicSize <= 1497600 && reshapeCW.rspIntraPeriod == -1 && modIP == 0 && sliceType != I_SLICE)
if ((sliceType == I_SLICE || (sliceType == P_SLICE && isCPR)) && isDualT)
{
m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false;
m_sliceReshapeInfo.enableChromaAdj = m_chromaAdj;
bool enableRsp = m_tcase == 5 ? false : (m_tcase < 5 ? (cTid < m_tcase + 1 ? false : true) : (cTid <= 10 - m_tcase ? true : false));
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
}
}
}
// Bubble Sort to descending order with index
void EncReshape::bubbleSortDsd(double* array, int * idx, int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++)
{
swapped = false;
for (j = 0; j < n - i - 1; j++)
{
if (array[j] < array[j + 1])
{
swap(&array[j], &array[j + 1]);
swap(&idx[j], &idx[j + 1]);
swapped = true;
}
}
if (swapped == false)
break;
}
}
void EncReshape::deriveReshapeParametersSDRfromStats(uint32_t * blockBinCnt, double *blockBinVarSum, double* reshapeTH1, double* reshapeTH2, bool *intraAdp, bool *interAdp)
int binIdxSortDsd[PIC_ANALYZE_CW_BINS] = { 0 };
double binVarSortDsd[PIC_ANALYZE_CW_BINS] = { 0.0 };
double binHist[PIC_ANALYZE_CW_BINS] = { 0.0 };
double binVarSortDsdCDF[PIC_ANALYZE_CW_BINS] = { 0.0 };
double maxBinVar = 0.0, meanBinVar = 0.0, minBinVar = 5.0;
int nonZeroBinCt = 0;
int firstBinVarLessThanVal1 = 0;
int firstBinVarLessThanVal2 = 0;
int firstBinVarLessThanVal3 = 0;
int firstBinVarLessThanVal4 = 0;
for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
{
binHist[b] = (double) blockBinCnt[b] / (double)(m_reshapeCW.rspPicSize);
if (binHist[b] > 0.001)
meanBinVar += blockBinVarSum[b];
if (blockBinVarSum[b] > maxBinVar) { maxBinVar = blockBinVarSum[b]; }
if (blockBinVarSum[b] < minBinVar) { minBinVar = blockBinVarSum[b]; }
if ((binHist[0] + binHist[1] + binHist[PIC_ANALYZE_CW_BINS - 2] + binHist[PIC_ANALYZE_CW_BINS - 1]) > 0.01) { m_exceedSTD = true; }
if ((binHist[PIC_ANALYZE_CW_BINS - 2] + binHist[PIC_ANALYZE_CW_BINS - 1]) > 0.01) { *interAdp = false; return; }
else { *interAdp = true; }
meanBinVar = meanBinVar / (double)nonZeroBinCt;
bubbleSortDsd(binVarSortDsd, binIdxSortDsd, PIC_ANALYZE_CW_BINS);
binVarSortDsdCDF[0] = binHist[binIdxSortDsd[0]];
for (int b = 1; b < PIC_ANALYZE_CW_BINS; b++)
{
binVarSortDsdCDF[b] = binVarSortDsdCDF[b - 1] + binHist[binIdxSortDsd[b]];
}
for (int b = 0; b < PIC_ANALYZE_CW_BINS - 1; b++)
{
if (binVarSortDsd[b] > 3.5) { firstBinVarLessThanVal1 = b + 1; }
if (binVarSortDsd[b] > 3.0) { firstBinVarLessThanVal2 = b + 1; }
if (binVarSortDsd[b] > 2.5) { firstBinVarLessThanVal3 = b + 1; }
if (binVarSortDsd[b] > 2.0) { firstBinVarLessThanVal4 = b + 1; }
m_reshapeCW.binCW[0] = 36;
*reshapeTH1 = 2.4;
*reshapeTH2 = 4.5;
m_rateAdpMode = 2;
if (meanBinVar >= 2.52)
{
else if (binVarSortDsdCDF[firstBinVarLessThanVal2] < 0.1 && binVarSortDsdCDF[firstBinVarLessThanVal1] > 0.02)
m_reshapeCW.binCW[0] = 34;
*reshapeTH1 = 3.4;
*reshapeTH2 = 4.0;
m_reshapeCW.binCW[0] = 40;
*reshapeTH1 = 2.2;
*reshapeTH2 = 4.5;
m_rateAdpMode = 0;
}
}
else
{
if (maxBinVar > 3.3)
{
m_reshapeCW.binCW[0] = 36;
*reshapeTH1 = 2.5;
*reshapeTH2 = 4.5;
double diff1 = binVarSortDsdCDF[firstBinVarLessThanVal4] - binVarSortDsdCDF[firstBinVarLessThanVal3];
double diff2 = binVarSortDsdCDF[firstBinVarLessThanVal2] - binVarSortDsdCDF[firstBinVarLessThanVal1];
if (diff1 > 0.4 || binVarSortDsdCDF[firstBinVarLessThanVal1] > 0.1)
else if (diff2 <= 0.1 && binVarSortDsdCDF[firstBinVarLessThanVal4] > 0.99 && binVarSortDsdCDF[firstBinVarLessThanVal3] > 0.642 && binVarSortDsdCDF[firstBinVarLessThanVal2] > 0.03)
m_rateAdpMode = 1;
}
else
{
m_rateAdpMode = 2;
}
}
}
else
{
m_reshapeCW.binCW[0] = 36;
*reshapeTH1 = 2.6;
*reshapeTH2 = 4.5;
if (binVarSortDsdCDF[firstBinVarLessThanVal2] > 0.5 && maxBinVar < 4.7)
m_rateAdpMode = 2;
if (maxBinVar > 2.4)
{
*reshapeTH1 = 2.5;
*reshapeTH2 = 4.0;
m_reshapeCW.binCW[0] = 36;
m_sliceReshapeInfo.enableChromaAdj = 0;
m_useAdpCW = true;
m_reshapeCW.binCW[0] = 36;
m_reshapeCW.binCW[1] = 30;
}
}
}
else
{
if (maxBinVar > 2.8)
{
*reshapeTH1 = 2.2;
*reshapeTH2 = 4.0;
m_reshapeCW.binCW[0] = 36;
m_sliceReshapeInfo.enableChromaAdj = 0;
m_useAdpCW = true;
m_reshapeCW.binCW[0] = 38;
m_reshapeCW.binCW[1] = 28;
}
}
}
else
{
if (maxBinVar > 2.24)
{
m_useAdpCW = true;
m_reshapeCW.binCW[0] = 34;
m_reshapeCW.binCW[1] = 30;
{
int firstVarCDFLargerThanVal = 1;
for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
{
{
firstVarCDFLargerThanVal = b;
break;
}
}
if (meanBinVar < 2.52 || binVarSortDsdCDF[firstBinVarLessThanVal2] > 0.5)
*reshapeTH1 = 2.2;
*reshapeTH2 = (binVarSortDsd[firstVarCDFLargerThanVal] + binVarSortDsd[firstVarCDFLargerThanVal - 1]) / 2.0;
else if (binVarSortDsdCDF[firstBinVarLessThanVal2] < 0.1 && binVarSortDsdCDF[firstBinVarLessThanVal1] > 0.02)
}
}
else
{
if (maxBinVar > 3.3)
{
m_reshapeCW.binCW[0] = 36;
*reshapeTH1 = 2.5;
*reshapeTH2 = 4.5;
if (binVarSortDsdCDF[firstBinVarLessThanVal2] < 0.33 && m_reshapeCW.rspFps>40)
m_reshapeCW.binCW[0] = 36;
*reshapeTH1 = 3.0;
*reshapeTH2 = 4.0;
m_rateAdpMode = 2;
if (maxBinVar < 2.4)
{
m_tcase = 3;
}
else if (maxBinVar > 3.0)
{
if (minBinVar > 1)
{
m_reshapeCW.binCW[0] = 36;
*reshapeTH1 = 2.8;
*reshapeTH2 = 3.5;
m_sliceReshapeInfo.enableChromaAdj = 0;
m_chromaWeight = 1.05;
m_rateAdpMode = 0;
}
else
{
m_reshapeCW.binCW[0] = 36;
*reshapeTH1 = 2.2;
*reshapeTH2 = 3.5;
m_sliceReshapeInfo.enableChromaAdj = 0;
m_chromaWeight = 0.95;
}
}
else
{
m_rateAdpMode = 1;
if (meanBinVar < 2.52)
{
m_rateAdpMode = 0;
m_tcase = 9;
}
else
{
else if (binVarSortDsdCDF[firstBinVarLessThanVal2] < 0.1 && binVarSortDsdCDF[firstBinVarLessThanVal1] > 0.02)
*reshapeTH1 = 2.4;
*reshapeTH2 = 4.5;
m_reshapeCW.binCW[0] = 40;
m_rateAdpMode = 0;
}
}
else
{
if (maxBinVar > 3.3)
{
*reshapeTH1 = 3.0;
*reshapeTH2 = 4.0;
m_reshapeCW.binCW[1] = 30;
m_reshapeCW.binCW[1] = 30;
*reshapeTH1 = 2.5;
*reshapeTH2 = 4.5;
*intraAdp = true;
m_rateAdpMode = 1;
if (minBinVar > 2.6)
{
m_rateAdpMode = 0;
m_tcase = 9;
}
else {
double diff1 = binVarSortDsdCDF[firstBinVarLessThanVal4] - binVarSortDsdCDF[firstBinVarLessThanVal3];
double diff2 = binVarSortDsdCDF[firstBinVarLessThanVal2] - binVarSortDsdCDF[firstBinVarLessThanVal1];
if (diff1 > 0.4 || binVarSortDsdCDF[firstBinVarLessThanVal1] > 0.1)
*reshapeTH1 = 2.9;
if (binVarSortDsdCDF[firstBinVarLessThanVal4] > 0.99 && binVarSortDsdCDF[firstBinVarLessThanVal3] > 0.642 && binVarSortDsdCDF[firstBinVarLessThanVal2] > 0.03)
{
m_rateAdpMode = 0;
m_tcase = 9;
}
}
}
}
}
else
{
m_reshapeCW.binCW[0] = 36;
m_reshapeCW.binCW[1] = 30;
*reshapeTH1 = 2.6;
*reshapeTH2 = 4.5;
*intraAdp = true;
if (binVarSortDsdCDF[firstBinVarLessThanVal2] > 0.5 && maxBinVar < 4.7)
}
}
}
}
void EncReshape::deriveReshapeParameters(double *array, int start, int end, ReshapeCW respCW, double &alpha, double &beta)
{
double minVar = 10.0, maxVar = 0.0;
for (int b = start; b <= end; b++)
{
if (array[b] < minVar) minVar = array[b];
if (array[b] > maxVar) maxVar = array[b];
}
double maxCW = (double)respCW.binCW[0];
double minCW = (double)respCW.binCW[1];
alpha = (minCW - maxCW) / (maxVar - minVar);
beta = (maxCW*maxVar - minCW*minVar) / (maxVar - minVar);
}
/**
-Init reshaping LUT from dQP model
*/
void EncReshape::initLUTfromdQPModel()
{
initModelParam();
int pwlFwdLUTsize = PIC_CODE_CW_BINS;
int pwlFwdBinLen = m_reshapeLUTSize / PIC_CODE_CW_BINS;
int p1 = m_dQPModel.scaleFracPrec;
int p2 = m_dQPModel.offsetFracPrec;
int scaleFP = (1 - 2 * m_dQPModel.scaleSign) * m_dQPModel.scaleAbs;
int offsetFP = (1 - 2 * m_dQPModel.offsetSign) * m_dQPModel.offsetAbs;
int maxQP = (1 - 2 * m_dQPModel.maxQPSign) * m_dQPModel.maxQPAbs;
int minQP = (1 - 2 * m_dQPModel.minQPSign) * m_dQPModel.minQPAbs;
int maxFP = maxQP * (1 << totalShift);
int minFP = minQP * (1 << totalShift);
int dQPDiv6FP;
int32_t * slopeLUT = new int32_t[m_reshapeLUTSize]();
int32_t * fwdLUTHighPrec = new int32_t[m_reshapeLUTSize]();
for (int i = 0; i < m_reshapeLUTSize; i++)
int inputY = m_lumaBD < 10 ? i << (10 - m_lumaBD) : m_lumaBD > 10 ? i >> (m_lumaBD - 10) : i;
temp = int64_t((scaleFP*inputY) * (1 << p2)) + int64_t(offsetFP * (1 << p1));
temp = temp > maxFP ? maxFP : temp < minFP ? minFP : temp;
signval = temp >= 0 ? 1 : -1;
absval = signval * temp;
dQPDiv6FP = signval * (((absval + 3) / 6 + (1 << (totalShift - 17))) >> (totalShift - 16));
for (int i = 0; i < (16 << (m_lumaBD - 8)); i++) { slopeLUT[i] = 0; }
for (int i = (235 << (m_lumaBD - 8)); i < m_reshapeLUTSize; i++) { slopeLUT[i] = 0; }
for (int i = 0; i < m_reshapeLUTSize - 1; i++)
fwdLUTHighPrec[i + 1] = fwdLUTHighPrec[i] + slopeLUT[i];
if (slopeLUT != nullptr) { delete[] slopeLUT; slopeLUT = nullptr; }
for (int i = 0; i < m_reshapeLUTSize; i++)
m_fwdLUT[i] = (short)(((fwdLUTHighPrec[i] >> 8) * (m_reshapeLUTSize - 1) + Roffset) / max_Y);
if (fwdLUTHighPrec != nullptr) { delete[] fwdLUTHighPrec; fwdLUTHighPrec = nullptr; }
m_sliceReshapeInfo.reshaperModelMinBinIdx = 1;
m_sliceReshapeInfo.reshaperModelMaxBinIdx = 14;
for (int i = 0; i < pwlFwdLUTsize; i++)
{
int16_t X1 = i * pwlFwdBinLen;
m_reshapePivot[pwlFwdLUTsize] = ((1 << m_lumaBD) - 1);
for (int i = 0; i < pwlFwdLUTsize; i++)
{
m_binCW[i] = m_reshapePivot[i + 1] - m_reshapePivot[i];
int maxAbsDeltaCW = 0, absDeltaCW = 0, deltaCW = 0;
for (int i = m_sliceReshapeInfo.reshaperModelMinBinIdx; i <= m_sliceReshapeInfo.reshaperModelMaxBinIdx; i++)
deltaCW = (int)m_binCW[i] - (int)m_initCW;
m_sliceReshapeInfo.reshaperModelBinCWDelta[i] = deltaCW;
absDeltaCW = (deltaCW < 0) ? (-deltaCW) : deltaCW;
if (absDeltaCW > maxAbsDeltaCW) { maxAbsDeltaCW = absDeltaCW; }
}
m_sliceReshapeInfo.maxNbitsNeededDeltaCW = g_aucLog2[maxAbsDeltaCW << 1];
for (int i = 0; i < pwlFwdLUTsize; 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 = g_aucLog2[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();
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
}
/**
-Perform fixe point exp2 calculation
\param val input value
\retval output value = exp2(val)
*/
int EncReshape::calcEXP2(int val)
{
int32_t i, f, r, s;
r = 0x00000e20;
i = ((int32_t)(val)+0x8000) & ~0xffff;
f = (int32_t)(val)-i;
s = ((15 << 16) - i) >> 16;
r = (r * f + 0x3e1cc333) >> 17;
r = (r * f + 0x58bd46a6) >> 16;
r = r * f + 0x7ffde4a3;
return (uint32_t)r >> s;
}
void EncReshape::constructReshaperSDR()
{
int bdShift = m_lumaBD - 10;
int usedCW = 0;
int totCW = bdShift != 0 ? (bdShift > 0 ? m_reshapeLUTSize / (1<<bdShift) : m_reshapeLUTSize * (1 << (-bdShift))) : m_reshapeLUTSize;
int histBins = PIC_ANALYZE_CW_BINS;
int histLenth = totCW/histBins;
int log2HistLenth = g_aucLog2[histLenth];
int16_t *tempFwdLUT = new int16_t[m_reshapeLUTSize + 1]();
cwScaleBins1 = m_reshapeCW.binCW[0];
cwScaleBins2 = m_reshapeCW.binCW[1];
for (i = 0; i < histBins; i++)
usedCW += m_binCW[i];
if (m_binCW[i] == histLenth + 1) cnt0++;
else if (m_binCW[i] == cwScaleBins1) cnt1++;
else if (m_binCW[i] == cwScaleBins2) cnt2++;
int resCW = usedCW - maxAllowedCW;
int cwReduce1 = (cwScaleBins1 - histLenth - 1) * cnt1;
int cwReduce2 = (histLenth + 1 - cwScaleBins2) * cnt0;
if (m_binCW[i] == histLenth + 1)
m_binCW[i] = cwScaleBins2;
if (m_binCW[i] == cwScaleBins1)
m_binCW[i] = histLenth + 1;
if (bdShift != 0)
{
for (int i = 0; i < PIC_ANALYZE_CW_BINS; i++)
{
m_binCW[i] = bdShift > 0 ? m_binCW[i] * (1 << bdShift) : m_binCW[i] / (1 << (-bdShift));
}
}
for (int i = 0; i < PIC_CODE_CW_BINS; i++)
{
m_binCW[i] = m_binCW[2 * i] + m_binCW[2 * i + 1];
m_sliceReshapeInfo.reshaperModelMinBinIdx = 0;
m_sliceReshapeInfo.reshaperModelMaxBinIdx = PIC_CODE_CW_BINS - 1;
for (int i = 0; i < PIC_CODE_CW_BINS; i++)
{
break;
}
}
for (int i = PIC_CODE_CW_BINS - 1; i >= 0; i--)
{
int maxAbsDeltaCW = 0, absDeltaCW = 0, deltaCW = 0;
for (int i = m_sliceReshapeInfo.reshaperModelMinBinIdx; i <= m_sliceReshapeInfo.reshaperModelMaxBinIdx; i++)
deltaCW = (int)m_binCW[i] - (int)m_initCW;
m_sliceReshapeInfo.reshaperModelBinCWDelta[i] = deltaCW;
absDeltaCW = (deltaCW < 0) ? (-deltaCW) : deltaCW;
if (absDeltaCW > maxAbsDeltaCW) { maxAbsDeltaCW = absDeltaCW; }
}
m_sliceReshapeInfo.maxNbitsNeededDeltaCW = g_aucLog2[maxAbsDeltaCW << 1];
for (i = 0; i < PIC_CODE_CW_BINS; i++) { sum_bins += m_binCW[i]; }
CHECK(sum_bins > m_reshapeLUTSize, "SDR CW assignment is wrong!!");
memset(tempFwdLUT, 0, (m_reshapeLUTSize + 1) * sizeof(int16_t));
tempFwdLUT[0] = 0;
tempFwdLUT[(i + 1)*histLenth] = tempFwdLUT[i*histLenth] + m_binCW[i];
int16_t Y1 = tempFwdLUT[i*histLenth];
int16_t Y2 = tempFwdLUT[(i + 1)*histLenth];
int32_t scale = ((int32_t)(Y2 - Y1) * (1 << FP_PREC) + (1 << (log2HistLenth - 1))) >> (log2HistLenth);
m_fwdLUT[i*histLenth] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)Y1);
for (j = 1; j < histLenth; j++)
tempFwdLUT[i*histLenth + j] = Y1 + (((int32_t)scale * (int32_t)j + (1 << (FP_PREC - 1))) >> FP_PREC);
m_fwdLUT[i*histLenth + j] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)tempFwdLUT[i*histLenth + j]);
}
}
for (i = 0; i < PIC_CODE_CW_BINS; i++)
{
int start = i*histLenth;
int end = (i + 1)*histLenth - 1;
m_cwLumaWeight[i] = m_fwdLUT[end] - m_fwdLUT[start];
if (tempFwdLUT != nullptr) { delete[] tempFwdLUT; tempFwdLUT = nullptr; }
reverseLUT(m_fwdLUT, m_invLUT, m_reshapeLUTSize);
updateChromaScaleLUT();