Skip to content
Snippets Groups Projects
EncReshape.cpp 40.1 KiB
Newer Older
Taoran Lu's avatar
Taoran Lu committed
/* 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()
{
Taoran Lu's avatar
Taoran Lu committed
  m_CTUFlag      = false;
  m_srcReshaped  = false;
  m_recReshaped  = false;
  m_reshape      = true;
  m_exceedSTD    = false;
Taoran Lu's avatar
Taoran Lu committed
  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;

Taoran Lu's avatar
Taoran Lu committed
  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);
Taoran Lu's avatar
Taoran Lu committed
  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);
Taoran Lu's avatar
Taoran Lu committed

  m_sliceReshapeInfo.setUseSliceReshaper(true);
  m_sliceReshapeInfo.setSliceReshapeChromaAdj(true);
  m_sliceReshapeInfo.setSliceReshapeModelPresentFlag(true);
Taoran Lu's avatar
Taoran Lu committed
  m_sliceReshapeInfo.reshaperModelMinBinIdx = 0;
  m_sliceReshapeInfo.reshaperModelMaxBinIdx = PIC_CODE_CW_BINS - 1;
  memset(m_sliceReshapeInfo.reshaperModelBinCWDelta, 0, (PIC_CODE_CW_BINS) * sizeof(int));
Taoran Lu's avatar
Taoran Lu committed

  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
*/
Taoran Lu's avatar
Taoran Lu committed
void EncReshape::preAnalyzerHDR(Picture *pcPic, const SliceType sliceType, const ReshapeCW& reshapeCW, bool isDualT)
#else
void EncReshape::preAnalyzerHDR(Picture *pcPic, const SliceType sliceType, const ReshapeCW& reshapeCW, bool isDualT, bool isIBC)
#endif
Taoran Lu's avatar
Taoran Lu committed
  if (m_lumaBD >= 10)
Taoran Lu's avatar
Taoran Lu committed
    m_sliceReshapeInfo.sliceReshaperEnableFlag = true;
    if (reshapeCW.rspIntraPeriod == 1)
Taoran Lu's avatar
Taoran Lu committed
      if (pcPic->getPOC() == 0)          { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = true;  }
      else                               { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false; }
Taoran Lu's avatar
Taoran Lu committed
      if (sliceType == I_SLICE )                                              { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = true;  }
#else
      if (sliceType == I_SLICE || (sliceType == P_SLICE && isIBC))            { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = true;  }
#endif
Taoran Lu's avatar
Taoran Lu committed
      else                                                                    { m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false; }
Taoran Lu's avatar
Taoran Lu committed
    if (sliceType == I_SLICE  && isDualT)                                     { m_sliceReshapeInfo.enableChromaAdj = 0;                   }
#else
    if ((sliceType == I_SLICE || (sliceType == P_SLICE && isIBC)) && isDualT) { m_sliceReshapeInfo.enableChromaAdj = 0;                   }
#endif
Taoran Lu's avatar
Taoran Lu committed
    else                                                                      { m_sliceReshapeInfo.enableChromaAdj = 1;                   }
Taoran Lu's avatar
Taoran Lu committed
    m_sliceReshapeInfo.sliceReshaperEnableFlag = false;
    m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false;
Taoran Lu's avatar
Taoran Lu committed
  }
}

/**
-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
*/
Taoran Lu's avatar
Taoran Lu committed
void EncReshape::preAnalyzerSDR(Picture *pcPic, const SliceType sliceType, const ReshapeCW& reshapeCW, bool isDualT)
#else
void EncReshape::preAnalyzerSDR(Picture *pcPic, const SliceType sliceType, const ReshapeCW& reshapeCW, bool isDualT, bool isIBC)
#endif
Taoran Lu's avatar
Taoran Lu committed
  m_sliceReshapeInfo.sliceReshaperModelPresentFlag = true;
  m_sliceReshapeInfo.sliceReshaperEnableFlag = true;
Taoran Lu's avatar
Taoran Lu committed
  int modIP = pcPic->getPOC() - pcPic->getPOC() / reshapeCW.rspFpsToIp * reshapeCW.rspFpsToIp;
Taoran Lu's avatar
Taoran Lu committed
  if (sliceType == I_SLICE || (reshapeCW.rspIntraPeriod == -1 && modIP == 0))
#else
  if (sliceType == I_SLICE || (reshapeCW.rspIntraPeriod == -1 && modIP == 0) || (sliceType == P_SLICE && isIBC))
#endif
Taoran Lu's avatar
Taoran Lu committed
    if (m_sliceReshapeInfo.sliceReshaperModelPresentFlag == true)
Taoran Lu's avatar
Taoran Lu committed
      int stdMin = 16 <<(m_lumaBD-8);
      int stdMax = 235 << (m_lumaBD - 8);
      int  binLen = m_reshapeLUTSize / PIC_ANALYZE_CW_BINS;
Taoran Lu's avatar
Taoran Lu committed

      m_reshapeCW = reshapeCW;
Taoran Lu's avatar
Taoran Lu committed
      m_initCWAnalyze = binLen;
Taoran Lu's avatar
Taoran Lu committed

      for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
      {
Taoran Lu's avatar
Taoran Lu committed
        m_binImportance[b] = 0;
        m_binCW[b] = binLen;
Taoran Lu's avatar
Taoran Lu committed
      int startBinIdx = stdMin / binLen;
      int endBinIdx = stdMax / binLen;
Taoran Lu's avatar
Taoran Lu committed
      m_sliceReshapeInfo.reshaperModelMinBinIdx = startBinIdx;
      m_sliceReshapeInfo.reshaperModelMaxBinIdx = endBinIdx;
Taoran Lu's avatar
Taoran Lu committed

      PelBuf picY = pcPic->getOrigBuf(COMPONENT_Y);
Taoran Lu's avatar
Taoran Lu committed
      const int width = picY.width;
      const int height = picY.height;
      const int stride = picY.stride;
Taoran Lu's avatar
Taoran Lu committed
      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 winSize = PIC_ANALYZE_WIN_SIZE;
      const uint32_t winLens = (winSize - 1) >> 1;
Loading
Loading full blame...