Skip to content
Snippets Groups Projects
DepQuant.cpp 81.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* 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.
     */
    
    #include "DepQuant.h"
    #include "TrQuant.h"
    #include "CodingStructure.h"
    #include "UnitTools.h"
    
    #include <bitset>
    
    
    
    
    
    
    namespace DQIntern
    {
      /*================================================================================*/
      /*=====                                                                      =====*/
      /*=====   R A T E   E S T I M A T O R                                        =====*/
      /*=====                                                                      =====*/
      /*================================================================================*/
    
      struct NbInfoSbb
      {
        uint8_t   num;
        uint8_t   inPos[5];
      };
      struct NbInfoOut
      {
        uint16_t  maxDist;
        uint16_t  num;
        uint16_t  outPos[5];
      };
      struct CoeffFracBits
      {
    
      };
    
    
      enum ScanPosType { SCAN_ISCSBB = 0, SCAN_SOCSBB = 1, SCAN_EOCSBB = 2 };
    
      struct ScanInfo
      {
        ScanInfo() {}
        int           sbbSize;
        int           numSbb;
        int           scanIdx;
        int           rasterPos;
        int           sbbPos;
        int           insidePos;
        bool          eosbb;
        ScanPosType   spt;
        unsigned      sigCtxOffsetNext;
        unsigned      gtxCtxOffsetNext;
        int           nextInsidePos;
        NbInfoSbb     nextNbInfoSbb;
        int           nextSbbRight;
        int           nextSbbBelow;
    
        int           posX;
        int           posY;
    
      };
    
      class Rom;
      struct TUParameters
      {
        TUParameters ( const Rom& rom, const unsigned width, const unsigned height, const ChannelType chType );
        ~TUParameters()
        {
          delete [] m_scanInfo;
        }
    
        ChannelType       m_chType;
        unsigned          m_width;
        unsigned          m_height;
        unsigned          m_numCoeff;
        unsigned          m_numSbb;
        unsigned          m_log2SbbWidth;
        unsigned          m_log2SbbHeight;
        unsigned          m_log2SbbSize;
        unsigned          m_sbbSize;
        unsigned          m_sbbMask;
        unsigned          m_widthInSbb;
        unsigned          m_heightInSbb;
        CoeffScanType     m_scanType;
    
    Frank Bossen's avatar
    Frank Bossen committed
        const ScanElement *m_scanSbbId2SbbPos;
        const ScanElement *m_scanId2BlkPos;
    
        const NbInfoSbb*  m_scanId2NbInfoSbb;
        const NbInfoOut*  m_scanId2NbInfoOut;
        ScanInfo*         m_scanInfo;
      private:
        void xSetScanInfo( ScanInfo& scanInfo, int scanIdx );
      };
    
      class Rom
      {
      public:
        Rom() : m_scansInitialized(false) {}
        ~Rom() { xUninitScanArrays(); }
        void                init        ()                       { xInitScanArrays(); }
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
        const NbInfoSbb*    getNbInfoSbb( int hd, int vd ) const { return m_scanId2NbInfoSbbArray[hd][vd]; }
        const NbInfoOut*    getNbInfoOut( int hd, int vd ) const { return m_scanId2NbInfoOutArray[hd][vd]; }
    #else
    
        const NbInfoSbb*    getNbInfoSbb( int hd, int vd, int ch ) const { return m_scanId2NbInfoSbbArray[hd][vd][ch]; }
        const NbInfoOut*    getNbInfoOut( int hd, int vd, int ch ) const { return m_scanId2NbInfoOutArray[hd][vd][ch]; }
    
        const TUParameters* getTUPars   ( const CompArea& area, const ComponentID compID ) const
        {
          return m_tuParameters[g_aucLog2[area.width]][g_aucLog2[area.height]][toChannelType(compID)];
        }
      private:
        void  xInitScanArrays   ();
        void  xUninitScanArrays ();
      private:
        bool          m_scansInitialized;
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
        NbInfoSbb*    m_scanId2NbInfoSbbArray[ MAX_CU_DEPTH+1 ][ MAX_CU_DEPTH+1 ];
        NbInfoOut*    m_scanId2NbInfoOutArray[ MAX_CU_DEPTH+1 ][ MAX_CU_DEPTH+1 ];
    #else
    
        NbInfoSbb*    m_scanId2NbInfoSbbArray[ MAX_CU_DEPTH+1 ][ MAX_CU_DEPTH+1 ][ MAX_NUM_CHANNEL_TYPE ];
        NbInfoOut*    m_scanId2NbInfoOutArray[ MAX_CU_DEPTH+1 ][ MAX_CU_DEPTH+1 ][ MAX_NUM_CHANNEL_TYPE ];
    
        TUParameters* m_tuParameters         [ MAX_CU_DEPTH+1 ][ MAX_CU_DEPTH+1 ][ MAX_NUM_CHANNEL_TYPE ];
      };
    
      void Rom::xInitScanArrays()
      {
        if( m_scansInitialized )
        {
          return;
        }
        ::memset( m_scanId2NbInfoSbbArray, 0, sizeof(m_scanId2NbInfoSbbArray) );
        ::memset( m_scanId2NbInfoOutArray, 0, sizeof(m_scanId2NbInfoOutArray) );
        ::memset( m_tuParameters,          0, sizeof(m_tuParameters) );
    
        uint32_t raster2id[ MAX_CU_SIZE * MAX_CU_SIZE ];
    
        ::memset(raster2id, 0, sizeof(raster2id));
    
    #if !JVET_N0103_CGSIZE_HARMONIZATION
    
        for( int ch = 0; ch < MAX_NUM_CHANNEL_TYPE; ch++ )
        {
    
        for( int hd = 0; hd <= MAX_CU_DEPTH; hd++ )
        {
          for( int vd = 0; vd <= MAX_CU_DEPTH; vd++ )
          {
            if( (hd == 0 && vd <= 1) || (hd <= 1 && vd == 0) )
            {
              continue;
            }
    
            const uint32_t      blockWidth    = (1 << hd);
            const uint32_t      blockHeight   = (1 << vd);
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
            const uint32_t      log2CGWidth   = g_log2SbbSize[hd][vd][0];
            const uint32_t      log2CGHeight  = g_log2SbbSize[hd][vd][1];
    #else
    
            const uint32_t      log2CGWidth   = g_log2SbbSize[ch][hd][vd][0];
            const uint32_t      log2CGHeight  = g_log2SbbSize[ch][hd][vd][1];
    
            const uint32_t      groupWidth    = 1 << log2CGWidth;
            const uint32_t      groupHeight   = 1 << log2CGHeight;
            const uint32_t      groupSize     = groupWidth * groupHeight;
            const CoeffScanType scanType      = SCAN_DIAG;
            const SizeType      blkWidthIdx   = gp_sizeIdxInfo->idxFrom( blockWidth  );
            const SizeType      blkHeightIdx  = gp_sizeIdxInfo->idxFrom( blockHeight );
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
            const ScanElement * scanId2RP     = g_scanOrder[SCAN_GROUPED_4x4][scanType][blkWidthIdx][blkHeightIdx];
            NbInfoSbb*&         sId2NbSbb     = m_scanId2NbInfoSbbArray[hd][vd];
            NbInfoOut*&         sId2NbOut     = m_scanId2NbInfoOutArray[hd][vd];
    #else
    
    Frank Bossen's avatar
    Frank Bossen committed
            const ScanElement * scanId2RP     = g_scanOrder[ch][SCAN_GROUPED_4x4][scanType][blkWidthIdx][blkHeightIdx];
    
            NbInfoSbb*&         sId2NbSbb     = m_scanId2NbInfoSbbArray[hd][vd][ch];
            NbInfoOut*&         sId2NbOut     = m_scanId2NbInfoOutArray[hd][vd][ch];
    
            // consider only non-zero-out region
            const uint32_t      blkWidthNZOut = std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, blockWidth  );
            const uint32_t      blkHeightNZOut= std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, blockHeight );
            const uint32_t      totalValues   = blkWidthNZOut * blkHeightNZOut;
    
    
            sId2NbSbb = new NbInfoSbb[ totalValues ];
            sId2NbOut = new NbInfoOut[ totalValues ];
    
            for( uint32_t scanId = 0; scanId < totalValues; scanId++ )
            {
    
    Frank Bossen's avatar
    Frank Bossen committed
              raster2id[scanId2RP[scanId].idx] = scanId;
    
            }
    
            for( unsigned scanId = 0; scanId < totalValues; scanId++ )
            {
    
    Frank Bossen's avatar
    Frank Bossen committed
              const int posX = scanId2RP[scanId].x;
              const int posY = scanId2RP[scanId].y;
              const int rpos = scanId2RP[scanId].idx;
    
              {
                //===== inside subband neighbours =====
                NbInfoSbb&     nbSbb  = sId2NbSbb[ scanId ];
                const int      begSbb = scanId - ( scanId & (groupSize-1) ); // first pos in current subblock
                int            cpos[5];
    
                cpos[0] = ( posX + 1 < blkWidthNZOut                              ? ( raster2id[rpos+1           ] < groupSize + begSbb ? raster2id[rpos+1           ] - begSbb : 0 ) : 0 );
                cpos[1] = ( posX + 2 < blkWidthNZOut                              ? ( raster2id[rpos+2           ] < groupSize + begSbb ? raster2id[rpos+2           ] - begSbb : 0 ) : 0 );
                cpos[2] = ( posX + 1 < blkWidthNZOut && posY + 1 < blkHeightNZOut ? ( raster2id[rpos+1+blockWidth] < groupSize + begSbb ? raster2id[rpos+1+blockWidth] - begSbb : 0 ) : 0 );
                cpos[3] = ( posY + 1 < blkHeightNZOut                             ? ( raster2id[rpos+  blockWidth] < groupSize + begSbb ? raster2id[rpos+  blockWidth] - begSbb : 0 ) : 0 );
                cpos[4] = ( posY + 2 < blkHeightNZOut                             ? ( raster2id[rpos+2*blockWidth] < groupSize + begSbb ? raster2id[rpos+2*blockWidth] - begSbb : 0 ) : 0 );
    
                for( nbSbb.num = 0; true; )
                {
                  int nk = -1;
                  for( int k = 0; k < 5; k++ )
                  {
                    if( cpos[k] != 0 && ( nk < 0 || cpos[k] < cpos[nk] ) )
                    {
                      nk = k;
                    }
                  }
                  if( nk < 0 )
                  {
                    break;
                  }
                  nbSbb.inPos[ nbSbb.num++ ] = uint8_t( cpos[nk] );
                  cpos[nk] = 0;
                }
                for( int k = nbSbb.num; k < 5; k++ )
                {
                  nbSbb.inPos[k] = 0;
                }
              }
              {
                //===== outside subband neighbours =====
                NbInfoOut&     nbOut  = sId2NbOut[ scanId ];
                const int      begSbb = scanId - ( scanId & (groupSize-1) ); // first pos in current subblock
                int            cpos[5];
    
    
                cpos[0] = ( posX + 1 < blkWidthNZOut                              ? ( raster2id[rpos+1           ] >= groupSize + begSbb ? raster2id[rpos+1           ] : 0 ) : 0 );
                cpos[1] = ( posX + 2 < blkWidthNZOut                              ? ( raster2id[rpos+2           ] >= groupSize + begSbb ? raster2id[rpos+2           ] : 0 ) : 0 );
                cpos[2] = ( posX + 1 < blkWidthNZOut && posY + 1 < blkHeightNZOut ? ( raster2id[rpos+1+blockWidth] >= groupSize + begSbb ? raster2id[rpos+1+blockWidth] : 0 ) : 0 );
                cpos[3] = ( posY + 1 < blkHeightNZOut                             ? ( raster2id[rpos+  blockWidth] >= groupSize + begSbb ? raster2id[rpos+  blockWidth] : 0 ) : 0 );
                cpos[4] = ( posY + 2 < blkHeightNZOut                             ? ( raster2id[rpos+2*blockWidth] >= groupSize + begSbb ? raster2id[rpos+2*blockWidth] : 0 ) : 0 );
    
    
                for( nbOut.num = 0; true; )
                {
                  int nk = -1;
                  for( int k = 0; k < 5; k++ )
                  {
                    if( cpos[k] != 0 && ( nk < 0 || cpos[k] < cpos[nk] ) )
                    {
                      nk = k;
                    }
                  }
                  if( nk < 0 )
                  {
                    break;
                  }
                  nbOut.outPos[ nbOut.num++ ] = uint16_t( cpos[nk] );
                  cpos[nk] = 0;
                }
                for( int k = nbOut.num; k < 5; k++ )
                {
                  nbOut.outPos[k] = 0;
                }
                nbOut.maxDist = ( scanId == 0 ? 0 : sId2NbOut[scanId-1].maxDist );
                for( int k = 0; k < nbOut.num; k++ )
                {
                  if( nbOut.outPos[k] > nbOut.maxDist )
                  {
                    nbOut.maxDist = nbOut.outPos[k];
                  }
                }
              }
            }
    
            // make it relative
            for( unsigned scanId = 0; scanId < totalValues; scanId++ )
            {
              NbInfoOut& nbOut  = sId2NbOut[scanId];
              const int  begSbb = scanId - ( scanId & (groupSize-1) ); // first pos in current subblock
              for( int k = 0; k < nbOut.num; k++ )
              {
    
                CHECK(begSbb > nbOut.outPos[k], "Position must be past sub block begin");
    
                nbOut.outPos[k] -= begSbb;
              }
              nbOut.maxDist -= scanId;
            }
    
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
            for( int chId = 0; chId < MAX_NUM_CHANNEL_TYPE; chId++ )
            {
              m_tuParameters[hd][vd][chId] = new TUParameters( *this, blockWidth, blockHeight, ChannelType(chId) );
            }
    #else
    
            m_tuParameters[hd][vd][ch] = new TUParameters( *this, blockWidth, blockHeight, ChannelType(ch) );
    
    #if !JVET_N0103_CGSIZE_HARMONIZATION
    
        m_scansInitialized = true;
      }
    
      void Rom::xUninitScanArrays()
      {
        if( !m_scansInitialized )
        {
          return;
        }
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
        for( int hd = 0; hd <= MAX_CU_DEPTH; hd++ )
        {
          for( int vd = 0; vd <= MAX_CU_DEPTH; vd++ )
          {
            NbInfoSbb*& sId2NbSbb = m_scanId2NbInfoSbbArray[hd][vd];
            NbInfoOut*& sId2NbOut = m_scanId2NbInfoOutArray[hd][vd];
            if( sId2NbSbb )
            {
              delete [] sId2NbSbb;
            }
            if( sId2NbOut )
            {
              delete [] sId2NbOut;
            }
            for( int chId = 0; chId < MAX_NUM_CHANNEL_TYPE; chId++ )
            {
              TUParameters*& tuPars = m_tuParameters[hd][vd][chId];
              if( tuPars )
              {
                delete tuPars;
              }
            }
          }
        }
    #else
    
        for( int hd = 0; hd <= MAX_CU_DEPTH; hd++ )
        {
          for( int vd = 0; vd <= MAX_CU_DEPTH; vd++ )
          {
            for( int ch = 0; ch < 2; ch++ )
            {
              NbInfoSbb*&     sId2NbSbb = m_scanId2NbInfoSbbArray[hd][vd][ch];
              NbInfoOut*&     sId2NbOut = m_scanId2NbInfoOutArray[hd][vd][ch];
              TUParameters*&  tuPars    = m_tuParameters         [hd][vd][ch];
              if( sId2NbSbb )
              {
                delete [] sId2NbSbb;
              }
              if( sId2NbOut )
              {
                delete [] sId2NbOut;
              }
              if( tuPars )
              {
                delete tuPars;
              }
            }
          }
        }
    
        m_scansInitialized = false;
      }
    
    
      static Rom g_Rom;
    
    
      TUParameters::TUParameters( const Rom& rom, const unsigned width, const unsigned height, const ChannelType chType )
      {
        m_chType              = chType;
        m_width               = width;
        m_height              = height;
    
        const uint32_t nonzeroWidth  = std::min<uint32_t>(JVET_C0024_ZERO_OUT_TH, m_width);
        const uint32_t nonzeroHeight = std::min<uint32_t>(JVET_C0024_ZERO_OUT_TH, m_height);
        m_numCoeff                   = nonzeroWidth * nonzeroHeight;
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
        m_log2SbbWidth        = g_log2SbbSize[ g_aucLog2[m_width] ][ g_aucLog2[m_height] ][0];
        m_log2SbbHeight       = g_log2SbbSize[ g_aucLog2[m_width] ][ g_aucLog2[m_height] ][1];
    #else
    
        m_log2SbbWidth        = g_log2SbbSize[m_chType][ g_aucLog2[m_width] ][ g_aucLog2[m_height] ][0];
        m_log2SbbHeight       = g_log2SbbSize[m_chType][ g_aucLog2[m_width] ][ g_aucLog2[m_height] ][1];
    
        m_log2SbbSize         = m_log2SbbWidth + m_log2SbbHeight;
        m_sbbSize             = ( 1 << m_log2SbbSize );
        m_sbbMask             = m_sbbSize - 1;
    
        m_widthInSbb  = nonzeroWidth >> m_log2SbbWidth;
        m_heightInSbb = nonzeroHeight >> m_log2SbbHeight;
    
        m_numSbb              = m_widthInSbb * m_heightInSbb;
    #if HEVC_USE_MDCS
    #error "MDCS is not supported" // use different function...
        //  m_scanType            = CoeffScanType( TU::getCoefScanIdx( tu, m_compID ) );
    #else
        m_scanType            = SCAN_DIAG;
    #endif
        SizeType        hsbb  = gp_sizeIdxInfo->idxFrom( m_widthInSbb  );
        SizeType        vsbb  = gp_sizeIdxInfo->idxFrom( m_heightInSbb );
        SizeType        hsId  = gp_sizeIdxInfo->idxFrom( m_width  );
        SizeType        vsId  = gp_sizeIdxInfo->idxFrom( m_height );
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
        m_scanSbbId2SbbPos    = g_scanOrder     [ SCAN_UNGROUPED   ][ m_scanType ][ hsbb ][ vsbb ];
        m_scanId2BlkPos       = g_scanOrder     [ SCAN_GROUPED_4x4 ][ m_scanType ][ hsId ][ vsId ];
        int log2W             = g_aucLog2[ m_width  ];
        int log2H             = g_aucLog2[ m_height ];
        m_scanId2NbInfoSbb    = rom.getNbInfoSbb( log2W, log2H );
        m_scanId2NbInfoOut    = rom.getNbInfoOut( log2W, log2H );
    #else
    
        m_scanSbbId2SbbPos    = g_scanOrder     [ chType ][ SCAN_UNGROUPED   ][ m_scanType ][ hsbb ][ vsbb ];
        m_scanId2BlkPos       = g_scanOrder     [ chType ][ SCAN_GROUPED_4x4 ][ m_scanType ][ hsId ][ vsId ];
        int log2W             = g_aucLog2[ m_width  ];
        int log2H             = g_aucLog2[ m_height ];
        m_scanId2NbInfoSbb    = rom.getNbInfoSbb( log2W, log2H, chType );
        m_scanId2NbInfoOut    = rom.getNbInfoOut( log2W, log2H, chType );
    
        m_scanInfo            = new ScanInfo[ m_numCoeff ];
        for( int scanIdx = 0; scanIdx < m_numCoeff; scanIdx++ )
        {
          xSetScanInfo( m_scanInfo[scanIdx], scanIdx );
        }
      }
    
    
      void TUParameters::xSetScanInfo( ScanInfo& scanInfo, int scanIdx )
      {
        scanInfo.sbbSize    = m_sbbSize;
        scanInfo.numSbb     = m_numSbb;
        scanInfo.scanIdx    = scanIdx;
    
    Frank Bossen's avatar
    Frank Bossen committed
        scanInfo.rasterPos  = m_scanId2BlkPos[scanIdx].idx;
        scanInfo.sbbPos     = m_scanSbbId2SbbPos[scanIdx >> m_log2SbbSize].idx;
    
        scanInfo.insidePos  = scanIdx & m_sbbMask;
        scanInfo.eosbb      = ( scanInfo.insidePos == 0 );
        scanInfo.spt        = SCAN_ISCSBB;
        if(  scanInfo.insidePos == m_sbbMask && scanIdx > scanInfo.sbbSize && scanIdx < m_numCoeff - 1 )
          scanInfo.spt      = SCAN_SOCSBB;
        else if( scanInfo.eosbb && scanIdx > 0 && scanIdx < m_numCoeff - m_sbbSize )
          scanInfo.spt      = SCAN_EOCSBB;
    
    Frank Bossen's avatar
    Frank Bossen committed
        scanInfo.posX = m_scanId2BlkPos[scanIdx].x;
        scanInfo.posY = m_scanId2BlkPos[scanIdx].y;
    
        if( scanIdx )
        {
          const int nextScanIdx = scanIdx - 1;
    
    Frank Bossen's avatar
    Frank Bossen committed
          const int diag        = m_scanId2BlkPos[nextScanIdx].x + m_scanId2BlkPos[nextScanIdx].y;
    
          if( m_chType == CHANNEL_TYPE_LUMA )
          {
            scanInfo.sigCtxOffsetNext = ( diag < 2 ? 12 : diag < 5 ?  6 : 0 );
            scanInfo.gtxCtxOffsetNext = ( diag < 1 ? 16 : diag < 3 ? 11 : diag < 10 ? 6 : 1 );
          }
          else
          {
            scanInfo.sigCtxOffsetNext = ( diag < 2 ? 6 : 0 );
            scanInfo.gtxCtxOffsetNext = ( diag < 1 ? 6 : 1 );
          }
          scanInfo.nextInsidePos      = nextScanIdx & m_sbbMask;
          scanInfo.nextNbInfoSbb      = m_scanId2NbInfoSbb[ nextScanIdx ];
          if( scanInfo.eosbb )
          {
    
    Frank Bossen's avatar
    Frank Bossen committed
            const int nextSbbPos  = m_scanSbbId2SbbPos[nextScanIdx >> m_log2SbbSize].idx;
    
            const int nextSbbPosY = nextSbbPos               / m_widthInSbb;
            const int nextSbbPosX = nextSbbPos - nextSbbPosY * m_widthInSbb;
            scanInfo.nextSbbRight = ( nextSbbPosX < m_widthInSbb  - 1 ? nextSbbPos + 1            : 0 );
            scanInfo.nextSbbBelow = ( nextSbbPosY < m_heightInSbb - 1 ? nextSbbPos + m_widthInSbb : 0 );
          }
        }
      }
    
    
    
      class RateEstimator
      {
      public:
        RateEstimator () {}
        ~RateEstimator() {}
        void initCtx  ( const TUParameters& tuPars, const TransformUnit& tu, const ComponentID compID, const FracBitsAccess& fracBitsAccess );
    
        inline const BinFracBits *sigSbbFracBits() const { return m_sigSbbFracBits; }
        inline const BinFracBits *sigFlagBits(unsigned stateId) const
        {
          return m_sigFracBits[std::max(((int) stateId) - 1, 0)];
        }
        inline const CoeffFracBits *gtxFracBits(unsigned stateId) const { return m_gtxFracBits; }
        inline int32_t              lastOffset(unsigned scanIdx) const
        {
    
    Frank Bossen's avatar
    Frank Bossen committed
          return m_lastBitsX[m_scanId2Pos[scanIdx].x] + m_lastBitsY[m_scanId2Pos[scanIdx].y];
    
        }
    
      private:
        void  xSetLastCoeffOffset ( const FracBitsAccess& fracBitsAccess, const TUParameters& tuPars, const TransformUnit& tu, const ComponentID compID );
        void  xSetSigSbbFracBits  ( const FracBitsAccess& fracBitsAccess, ChannelType chType );
        void  xSetSigFlagBits     ( const FracBitsAccess& fracBitsAccess, ChannelType chType );
        void  xSetGtxFlagBits     ( const FracBitsAccess& fracBitsAccess, ChannelType chType );
    
      private:
        static const unsigned sm_numCtxSetsSig    = 3;
        static const unsigned sm_numCtxSetsGtx    = 2;
        static const unsigned sm_maxNumSigSbbCtx  = 2;
        static const unsigned sm_maxNumSigCtx     = 18;
        static const unsigned sm_maxNumGtxCtx     = 21;
    
    Frank Bossen's avatar
    Frank Bossen committed
        const ScanElement * m_scanId2Pos;
    
        int32_t             m_lastBitsX      [ MAX_TB_SIZEY ];
        int32_t             m_lastBitsY      [ MAX_TB_SIZEY ];
    
        BinFracBits         m_sigSbbFracBits [ sm_maxNumSigSbbCtx ];
        BinFracBits         m_sigFracBits    [ sm_numCtxSetsSig   ][ sm_maxNumSigCtx ];
        CoeffFracBits       m_gtxFracBits                          [ sm_maxNumGtxCtx ];
    
      void RateEstimator::initCtx( const TUParameters& tuPars, const TransformUnit& tu, const ComponentID compID, const FracBitsAccess& fracBitsAccess )
      {
    
    Frank Bossen's avatar
    Frank Bossen committed
        m_scanId2Pos = tuPars.m_scanId2BlkPos;
    
        xSetSigSbbFracBits  ( fracBitsAccess, tuPars.m_chType );
        xSetSigFlagBits     ( fracBitsAccess, tuPars.m_chType );
        xSetGtxFlagBits     ( fracBitsAccess, tuPars.m_chType );
        xSetLastCoeffOffset ( fracBitsAccess, tuPars, tu, compID );
      }
    
      void RateEstimator::xSetLastCoeffOffset( const FracBitsAccess& fracBitsAccess, const TUParameters& tuPars, const TransformUnit& tu, const ComponentID compID )
      {
        const ChannelType chType = ( compID == COMPONENT_Y ? CHANNEL_TYPE_LUMA : CHANNEL_TYPE_CHROMA );
        int32_t cbfDeltaBits = 0;
        if( compID == COMPONENT_Y && !CU::isIntra(*tu.cu) && !tu.depth )
        {
          const BinFracBits bits  = fracBitsAccess.getFracBitsArray( Ctx::QtRootCbf() );
          cbfDeltaBits            = int32_t( bits.intBits[1] ) - int32_t( bits.intBits[0] );
        }
        else
        {
    
          BinFracBits bits;
          bool prevLumaCbf           = false;
          bool lastCbfIsInferred     = false;
          bool useIntraSubPartitions = tu.cu->ispMode && isLuma(chType);
          if( useIntraSubPartitions )
          {
            bool rootCbfSoFar = false;
            bool isLastSubPartition = CU::isISPLast(*tu.cu, tu.Y(), compID);
            uint32_t nTus = tu.cu->ispMode == HOR_INTRA_SUBPARTITIONS ? tu.cu->lheight() >> g_aucLog2[tu.lheight()] : tu.cu->lwidth() >> g_aucLog2[tu.lwidth()];
            if( isLastSubPartition )
            {
              TransformUnit* tuPointer = tu.cu->firstTU;
              for( int tuIdx = 0; tuIdx < nTus - 1; tuIdx++ )
              {
                rootCbfSoFar |= TU::getCbfAtDepth(*tuPointer, COMPONENT_Y, tu.depth);
                tuPointer     = tuPointer->next;
              }
              if( !rootCbfSoFar )
              {
                lastCbfIsInferred = true;
              }
            }
            if( !lastCbfIsInferred )
            {
              prevLumaCbf = TU::getPrevTuCbfAtDepth(tu, compID, tu.depth);
            }
            bits = fracBitsAccess.getFracBitsArray(Ctx::QtCbf[compID](DeriveCtx::CtxQtCbf(compID, tu.depth, prevLumaCbf, true)));
          }
          else
          {
            bits = fracBitsAccess.getFracBitsArray(Ctx::QtCbf[compID](DeriveCtx::CtxQtCbf(compID, tu.depth, tu.cbf[COMPONENT_Cb])));
          }
          cbfDeltaBits = lastCbfIsInferred ? 0 : int32_t(bits.intBits[1]) - int32_t(bits.intBits[0]);
    
        }
    
        static const unsigned prefixCtx[] = { 0, 0, 0, 3, 6, 10, 15, 21 };
        uint32_t              ctxBits  [ LAST_SIGNIFICANT_GROUPS ];
        for( unsigned xy = 0; xy < 2; xy++ )
        {
          int32_t             bitOffset   = ( xy ? cbfDeltaBits : 0 );
          int32_t*            lastBits    = ( xy ? m_lastBitsY : m_lastBitsX );
          const unsigned      size        = ( xy ? tuPars.m_height : tuPars.m_width );
          const unsigned      log2Size    = g_aucNextLog2[ size ];
    #if HEVC_USE_MDCS
          const bool          useYCtx     = ( m_scanType == SCAN_VER ? ( xy == 0 ) : ( xy != 0 ) );
    #else
          const bool          useYCtx     = ( xy != 0 );
    #endif
          const CtxSet&       ctxSetLast  = ( useYCtx ? Ctx::LastY : Ctx::LastX )[ chType ];
    
    Karsten Suehring's avatar
    Karsten Suehring committed
          const unsigned      lastShift   = ( compID == COMPONENT_Y ? (log2Size+1)>>2 : Clip3<unsigned>(0,2,size>>3) );
          const unsigned      lastOffset  = ( compID == COMPONENT_Y ? ( prefixCtx[log2Size] ) : 0 );
    
          unsigned            maxCtxId    = g_uiGroupIdx[std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, size) - 1];
    
          for( unsigned ctxId = 0; ctxId < maxCtxId; ctxId++ )
          {
            const BinFracBits bits  = fracBitsAccess.getFracBitsArray( ctxSetLast( lastOffset + ( ctxId >> lastShift ) ) );
            ctxBits[ ctxId ]        = sumFBits + bits.intBits[0] + ( ctxId>3 ? ((ctxId-2)>>1)<<SCALE_BITS : 0 ) + bitOffset;
            sumFBits               +=            bits.intBits[1];
          }
          ctxBits  [ maxCtxId ]     = sumFBits + ( maxCtxId>3 ? ((maxCtxId-2)>>1)<<SCALE_BITS : 0 ) + bitOffset;
    
          for (unsigned pos = 0; pos < std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, size); pos++)
    
          {
            lastBits[ pos ]         = ctxBits[ g_uiGroupIdx[ pos ] ];
          }
        }
      }
    
      void RateEstimator::xSetSigSbbFracBits( const FracBitsAccess& fracBitsAccess, ChannelType chType )
      {
        const CtxSet& ctxSet = Ctx::SigCoeffGroup[ chType ];
        for( unsigned ctxId = 0; ctxId < sm_maxNumSigSbbCtx; ctxId++ )
        {
          m_sigSbbFracBits[ ctxId ] = fracBitsAccess.getFracBitsArray( ctxSet( ctxId ) );
        }
      }
    
      void RateEstimator::xSetSigFlagBits( const FracBitsAccess& fracBitsAccess, ChannelType chType )
      {
        for( unsigned ctxSetId = 0; ctxSetId < sm_numCtxSetsSig; ctxSetId++ )
        {
          BinFracBits*    bits    = m_sigFracBits [ ctxSetId ];
          const CtxSet&   ctxSet  = Ctx::SigFlag  [ chType + 2*ctxSetId ];
          const unsigned  numCtx  = ( chType == CHANNEL_TYPE_LUMA ? 18 : 12 );
          for( unsigned ctxId = 0; ctxId < numCtx; ctxId++ )
          {
            bits[ ctxId ] = fracBitsAccess.getFracBitsArray( ctxSet( ctxId ) );
          }
        }
      }
    
      void RateEstimator::xSetGtxFlagBits( const FracBitsAccess& fracBitsAccess, ChannelType chType )
      {
        const CtxSet&   ctxSetPar   = Ctx::ParFlag [     chType ];
        const CtxSet&   ctxSetGt1   = Ctx::GtxFlag [ 2 + chType ];
        const CtxSet&   ctxSetGt2   = Ctx::GtxFlag [     chType ];
        const unsigned  numCtx      = ( chType == CHANNEL_TYPE_LUMA ? 21 : 11 );
        for( unsigned ctxId = 0; ctxId < numCtx; ctxId++ )
        {
          BinFracBits     fbPar = fracBitsAccess.getFracBitsArray( ctxSetPar( ctxId ) );
          BinFracBits     fbGt1 = fracBitsAccess.getFracBitsArray( ctxSetGt1( ctxId ) );
          BinFracBits     fbGt2 = fracBitsAccess.getFracBitsArray( ctxSetGt2( ctxId ) );
          CoeffFracBits&  cb    = m_gtxFracBits[ ctxId ];
          int32_t         par0  = (1<<SCALE_BITS) + int32_t(fbPar.intBits[0]);
          int32_t         par1  = (1<<SCALE_BITS) + int32_t(fbPar.intBits[1]);
          cb.bits[0] = 0;
          cb.bits[1] = fbGt1.intBits[0] + (1 << SCALE_BITS);
          cb.bits[2] = fbGt1.intBits[1] + par0 + fbGt2.intBits[0];
          cb.bits[3] = fbGt1.intBits[1] + par1 + fbGt2.intBits[0];
          cb.bits[4] = fbGt1.intBits[1] + par0 + fbGt2.intBits[1];
          cb.bits[5] = fbGt1.intBits[1] + par1 + fbGt2.intBits[1];
        }
      }
    
    
    
    
    
    
      /*================================================================================*/
      /*=====                                                                      =====*/
      /*=====   D A T A   S T R U C T U R E S                                      =====*/
      /*=====                                                                      =====*/
      /*================================================================================*/
    
    
      struct PQData
      {
        TCoeff  absLevel;
        int64_t deltaDist;
      };
    
    
      struct Decision
      {
        int64_t rdCost;
        TCoeff  absLevel;
        int     prevId;
      };
    
    
    
    
      /*================================================================================*/
      /*=====                                                                      =====*/
      /*=====   P R E - Q U A N T I Z E R                                          =====*/
      /*=====                                                                      =====*/
      /*================================================================================*/
    
      class Quantizer
      {
      public:
        Quantizer() {}
    
    #if JVET_N0847_SCALING_LISTS
        void  dequantBlock         ( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, CoeffBuf& recCoeff, bool enableScalingLists, int* piDequantCoef ) const;
        void  initQuantBlock2Pos   ( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, const double lambda, int gValue );
        inline void   preQuantCoeff( const TCoeff absCoeff, PQData *pqData, int QuanCoeff ) const;
    #else
    
        void  dequantBlock         ( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, CoeffBuf& recCoeff) const;
    
        void  initQuantBlock( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, const double lambda  );
    
    #if !JVET_N0847_SCALING_LISTS    
    
        inline void   preQuantCoeff(const TCoeff absCoeff, PQData *pqData) const;
    
        inline TCoeff getLastThreshold() const { return m_thresLast; }
        inline TCoeff getSSbbThreshold() const { return m_thresSSbb; }
    
    
    #if JVET_N0847_SCALING_LISTS
        inline int64_t getQScale()       const { return m_QScale; }
    #endif
    
      private:
        // quantization
        int               m_QShift;
        int64_t           m_QAdd;
        int64_t           m_QScale;
        TCoeff            m_maxQIdx;
        TCoeff            m_thresLast;
        TCoeff            m_thresSSbb;
        // distortion normalization
        int               m_DistShift;
        int64_t           m_DistAdd;
        int64_t           m_DistStepAdd;
        int64_t           m_DistOrgFact;
      };
    
      inline int ceil_log2(uint64_t x)
      {
        static const uint64_t t[6] = { 0xFFFFFFFF00000000ull, 0x00000000FFFF0000ull, 0x000000000000FF00ull, 0x00000000000000F0ull, 0x000000000000000Cull, 0x0000000000000002ull };
        int y = (((x & (x - 1)) == 0) ? 0 : 1);
        int j = 32;
        for( int i = 0; i < 6; i++)
        {
          int k = (((x & t[i]) == 0) ? 0 : j);
          y += k;
          x >>= k;
          j >>= 1;
        }
        return y;
      }
    
    #if JVET_N0847_SCALING_LISTS
      void Quantizer::initQuantBlock2Pos(const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, const double lambda, int gValue)
      {
    #if HEVC_USE_SCALING_LISTS && !JVET_N0847_SCALING_LISTS
        CHECK(tu.cs->sps->getScalingListFlag(), "Scaling lists not supported");
    #endif
        CHECKD(lambda <= 0.0, "Lambda must be greater than 0");
    
        const int         qpDQ = cQP.Qp + 1;
        const int         qpPer = qpDQ / 6;
        const int         qpRem = qpDQ - 6 * qpPer;
        const SPS&        sps = *tu.cs->sps;
        const CompArea&   area = tu.blocks[compID];
        const ChannelType chType = toChannelType(compID);
        const int         channelBitDepth = sps.getBitDepth(chType);
        const int         maxLog2TrDynamicRange = sps.getMaxLog2TrDynamicRange(chType);
        const int         nomTransformShift = getTransformShift(channelBitDepth, area.size(), maxLog2TrDynamicRange);
        const bool        clipTransformShift = (tu.mtsIdx == MTS_SKIP && sps.getSpsRangeExtension().getExtendedPrecisionProcessingFlag());
    #if JVET_N0246_MODIFIED_QUANTSCALES
        const bool        needsSqrt2ScaleAdjustment = TU::needsSqrt2Scale(tu, compID);
        const int         transformShift = (clipTransformShift ? std::max<int>(0, nomTransformShift) : nomTransformShift) + (needsSqrt2ScaleAdjustment ? -1 : 0);
    #else
    
        const int         transformShift = (clipTransformShift ? std::max<int>(0, nomTransformShift) : nomTransformShift);
    
    #endif
    	  // quant parameters
        m_QShift = QUANT_SHIFT - 1 + qpPer + transformShift;
        m_QAdd = -((3 << m_QShift) >> 1);
    #if JVET_N0246_MODIFIED_QUANTSCALES
        Intermediate_Int  invShift = IQUANT_SHIFT + 1 - qpPer - transformShift;
        m_QScale = g_quantScales[needsSqrt2ScaleAdjustment ? 1 : 0][qpRem];
    #else // JVET_N0246_MODIFIED_QUANTSCALES
    #if HM_QTBT_AS_IN_JEM_QUANT
        Intermediate_Int  invShift = IQUANT_SHIFT + 1 - qpPer - transformShift + (TU::needsBlockSizeTrafoScale(tu, compID) ? ADJ_DEQUANT_SHIFT : 0);
        m_QScale = (TU::needsSqrt2Scale(tu, compID) ? (g_quantScales[qpRem] * 181) >> 7 : g_quantScales[qpRem]);
    #else
        Intermediate_Int  invShift = IQUANT_SHIFT + 1 - qpPer - transformShift;
        m_QScale = g_quantScales[qpRem];
    #endif
    #endif // JVET_N0246_MODIFIED_QUANTSCALES
        const unsigned    qIdxBD = std::min<unsigned>(maxLog2TrDynamicRange + 1, 8 * sizeof(Intermediate_Int) + invShift - IQUANT_SHIFT - 1);
        m_maxQIdx = (1 << (qIdxBD - 1)) - 4;
    #if JVET_N0847_SCALING_LISTS 
        m_thresLast = TCoeff((int64_t(3) << m_QShift));
        m_thresSSbb = TCoeff((int64_t(3) << m_QShift));										
    #else
        m_thresLast = TCoeff((int64_t(3) << m_QShift) / (4 * m_QScale));
        m_thresSSbb = TCoeff((int64_t(3) << m_QShift) / (4 * m_QScale));
    #endif
    
        // distortion calculation parameters
    
        const int64_t qScale = gValue;  
    #if HM_QTBT_AS_IN_JEM_QUANT
    
        const int nomDShift = SCALE_BITS - 2 * (nomTransformShift + DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth)) + m_QShift;
    
    #else
        const int nomDShift = SCALE_BITS - 2 * (nomTransformShift + DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth))
    
                              + m_QShift + (TU::needsQP3Offset(tu, compID) ? 1 : 0);
    
    #endif
        const double  qScale2 = double(qScale * qScale);
        const double  nomDistFactor = (nomDShift < 0 ? 1.0 / (double(int64_t(1) << (-nomDShift))*qScale2*lambda) : double(int64_t(1) << nomDShift) / (qScale2*lambda));
        const int64_t pow2dfShift = (int64_t)(nomDistFactor * qScale2) + 1;
    
        const int     dfShift = ceil_log2(pow2dfShift);
    
        m_DistShift = 62 + m_QShift - 2 * maxLog2TrDynamicRange - dfShift;
        m_DistAdd = (int64_t(1) << m_DistShift) >> 1;
        m_DistStepAdd = (int64_t)(nomDistFactor * double(int64_t(1) << (m_DistShift + m_QShift)) + .5);
        m_DistOrgFact = (int64_t)(nomDistFactor * double(int64_t(1) << (m_DistShift + 1)) + .5);
      }
    #endif
    
      void Quantizer::initQuantBlock( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, const double lambda )
      {
    
    #if HEVC_USE_SCALING_LISTS && !JVET_N0847_SCALING_LISTS
    
        CHECK ( tu.cs->sps->getScalingListFlag(), "Scaling lists not supported" );
    #endif
        CHECKD( lambda <= 0.0, "Lambda must be greater than 0" );
    
        const int         qpDQ                  = cQP.Qp + 1;
        const int         qpPer                 = qpDQ / 6;
        const int         qpRem                 = qpDQ - 6 * qpPer;
        const SPS&        sps                   = *tu.cs->sps;
        const CompArea&   area                  = tu.blocks[ compID ];
        const ChannelType chType                = toChannelType( compID );
        const int         channelBitDepth       = sps.getBitDepth( chType );
        const int         maxLog2TrDynamicRange = sps.getMaxLog2TrDynamicRange( chType );
        const int         nomTransformShift     = getTransformShift( channelBitDepth, area.size(), maxLog2TrDynamicRange );
    
        const bool        clipTransformShift    = ( tu.mtsIdx==MTS_SKIP && sps.getSpsRangeExtension().getExtendedPrecisionProcessingFlag() );
    
    #if JVET_N0246_MODIFIED_QUANTSCALES
        const bool    needsSqrt2ScaleAdjustment = TU::needsSqrt2Scale(tu, compID);
        const int         transformShift        = ( clipTransformShift ? std::max<int>( 0, nomTransformShift ) : nomTransformShift ) + (needsSqrt2ScaleAdjustment?-1:0);
    #else
    
        const int         transformShift        = ( clipTransformShift ? std::max<int>( 0, nomTransformShift ) : nomTransformShift );
    
        // quant parameters
        m_QShift                    = QUANT_SHIFT  - 1 + qpPer + transformShift;
        m_QAdd                      = -( ( 3 << m_QShift ) >> 1 );
    
    #if JVET_N0246_MODIFIED_QUANTSCALES
        Intermediate_Int  invShift  = IQUANT_SHIFT + 1 - qpPer - transformShift;
        m_QScale                    = g_quantScales[needsSqrt2ScaleAdjustment?1:0][ qpRem ];
    #else // JVET_N0246_MODIFIED_QUANTSCALES
    
        Intermediate_Int  invShift  = IQUANT_SHIFT + 1 - qpPer - transformShift + ( TU::needsBlockSizeTrafoScale( tu, compID ) ? ADJ_DEQUANT_SHIFT : 0 );
        m_QScale                    = ( TU::needsSqrt2Scale( tu, compID ) ? ( g_quantScales[ qpRem ] * 181 ) >> 7 : g_quantScales[ qpRem ] );
    
    #else
        Intermediate_Int  invShift  = IQUANT_SHIFT + 1 - qpPer - transformShift;
        m_QScale                    = g_quantScales   [ qpRem ];
    #endif
    
    #endif // JVET_N0246_MODIFIED_QUANTSCALES
    
        const unsigned    qIdxBD    = std::min<unsigned>( maxLog2TrDynamicRange + 1, 8*sizeof(Intermediate_Int) + invShift - IQUANT_SHIFT - 1 );
        m_maxQIdx                   = ( 1 << (qIdxBD-1) ) - 4;
        m_thresLast                 = TCoeff( ( int64_t(3) << m_QShift ) / ( 4 * m_QScale ) );
        m_thresSSbb                 = TCoeff( ( int64_t(3) << m_QShift ) / ( 4 * m_QScale ) );
    
        // distortion calculation parameters
    
    #if JVET_N0246_MODIFIED_QUANTSCALES
        const int64_t qScale        = m_QScale;
        const int nomDShift =
          SCALE_BITS - 2 * (nomTransformShift + DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth)) + m_QShift + (needsSqrt2ScaleAdjustment ? 1 : 0);
    #else // JVET_N0246_MODIFIED_QUANTSCALES
    
        const int64_t qScale        = g_quantScales[ qpRem ];
    #if HM_QTBT_AS_IN_JEM_QUANT
    
        const int nomDShift = SCALE_BITS - 2 * (nomTransformShift + DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth)) + m_QShift;
    
    #else
        const int nomDShift = SCALE_BITS - 2 * (nomTransformShift + DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth))
                              + m_QShift + (TU::needsQP3Offset(tu, compID) ? 1 : 0);
    #endif
    
    #endif // JVET_N0246_MODIFIED_QUANTSCALES
    
        const double  qScale2       = double( qScale * qScale );
        const double  nomDistFactor = ( nomDShift < 0 ? 1.0/(double(int64_t(1)<<(-nomDShift))*qScale2*lambda) : double(int64_t(1)<<nomDShift)/(qScale2*lambda) );
        const int64_t pow2dfShift   = (int64_t)( nomDistFactor * qScale2 ) + 1;
        const int     dfShift       = ceil_log2( pow2dfShift );
        m_DistShift                 = 62 + m_QShift - 2*maxLog2TrDynamicRange - dfShift;
        m_DistAdd                   = (int64_t(1) << m_DistShift) >> 1;
        m_DistStepAdd               = (int64_t)( nomDistFactor * double(int64_t(1)<<(m_DistShift+m_QShift)) + .5 );
        m_DistOrgFact               = (int64_t)( nomDistFactor * double(int64_t(1)<<(m_DistShift+1       )) + .5 );
      }
    
    
    #if JVET_N0847_SCALING_LISTS
      void Quantizer::dequantBlock( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, CoeffBuf& recCoeff, bool enableScalingLists, int* piDequantCoef) const
    #else
    
      void Quantizer::dequantBlock( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, CoeffBuf& recCoeff ) const
    
    #if HEVC_USE_SCALING_LISTS && !JVET_N0847_SCALING_LISTS
    
        CHECK ( tu.cs->sps->getScalingListFlag(), "Scaling lists not supported" );
    #endif
    
        //----- set basic parameters -----
        const CompArea&     area      = tu.blocks[ compID ];
        const int           numCoeff  = area.area();
        const SizeType      hsId      = gp_sizeIdxInfo->idxFrom( area.width  );
        const SizeType      vsId      = gp_sizeIdxInfo->idxFrom( area.height );
    #if HEVC_USE_MDCS
        const CoeffScanType scanType  = CoeffScanType( TU::getCoefScanIdx( tu, compID ) );
    #else
        const CoeffScanType scanType  = SCAN_DIAG;
    #endif
    
    #if JVET_N0103_CGSIZE_HARMONIZATION
        const ScanElement *scan       = g_scanOrder[SCAN_GROUPED_4x4][scanType][hsId][vsId];
    #else
    
    Frank Bossen's avatar
    Frank Bossen committed
        const ScanElement *scan = g_scanOrder[toChannelType(compID)][SCAN_GROUPED_4x4][scanType][hsId][vsId];
    
        const TCoeff*       qCoeff    = tu.getCoeffs( compID ).buf;
              TCoeff*       tCoeff    = recCoeff.buf;
    
        //----- reset coefficients and get last scan index -----
        ::memset( tCoeff, 0, numCoeff * sizeof(TCoeff) );
        int lastScanIdx = -1;
        for( int scanIdx = numCoeff - 1; scanIdx >= 0; scanIdx-- )
        {
    
    Frank Bossen's avatar
    Frank Bossen committed
          if (qCoeff[scan[scanIdx].idx])
    
          {
            lastScanIdx = scanIdx;
            break;
          }
        }
        if( lastScanIdx < 0 )
        {
          return;
        }
    
        //----- set dequant parameters -----
        const int         qpDQ                  = cQP.Qp + 1;
        const int         qpPer                 = qpDQ / 6;
        const int         qpRem                 = qpDQ - 6 * qpPer;
        const SPS&        sps                   = *tu.cs->sps;
        const ChannelType chType                = toChannelType( compID );
        const int         channelBitDepth       = sps.getBitDepth( chType );
        const int         maxLog2TrDynamicRange = sps.getMaxLog2TrDynamicRange( chType );
        const TCoeff      minTCoeff             = -( 1 << maxLog2TrDynamicRange );
        const TCoeff      maxTCoeff             =  ( 1 << maxLog2TrDynamicRange ) - 1;
        const int         nomTransformShift     = getTransformShift( channelBitDepth, area.size(), maxLog2TrDynamicRange );
    
        const bool        clipTransformShift    = ( tu.mtsIdx==MTS_SKIP && sps.getSpsRangeExtension().getExtendedPrecisionProcessingFlag() );
    
    #if JVET_N0246_MODIFIED_QUANTSCALES
        const bool    needsSqrt2ScaleAdjustment = TU::needsSqrt2Scale(tu, compID);
        const int         transformShift        = ( clipTransformShift ? std::max<int>( 0, nomTransformShift ) : nomTransformShift ) + (needsSqrt2ScaleAdjustment?-1:0);
    
    #if JVET_N0847_SCALING_LISTS 
        Intermediate_Int  shift                 = IQUANT_SHIFT + 1 - qpPer - transformShift + (enableScalingLists ? LOG2_SCALING_LIST_NEUTRAL_VALUE : 0);
    #else
    
        Intermediate_Int  shift                 = IQUANT_SHIFT + 1 - qpPer - transformShift;
    
        Intermediate_Int  invQScale             = g_invQuantScales[needsSqrt2ScaleAdjustment?1:0][ qpRem ];
    
    #else // JVET_N0246_MODIFIED_QUANTSCALES
    
        const int         transformShift        = ( clipTransformShift ? std::max<int>( 0, nomTransformShift ) : nomTransformShift );
    #if HM_QTBT_AS_IN_JEM_QUANT
    
        Intermediate_Int  shift                 = IQUANT_SHIFT + 1 - qpPer - transformShift + ( TU::needsBlockSizeTrafoScale( tu, compID ) ? ADJ_DEQUANT_SHIFT : 0 );
        Intermediate_Int  invQScale             = g_invQuantScales[ qpRem ] * ( TU::needsSqrt2Scale( tu, compID ) ? 181 : 1 );
    
    #else
        Intermediate_Int  shift                 = IQUANT_SHIFT + 1 - qpPer - transformShift;
        Intermediate_Int  invQScale             = g_invQuantScales[ qpRem ];
    #endif
    
    #endif // JVET_N0246_MODIFIED_QUANTSCALES
    
    #if JVET_N0847_SCALING_LISTS
        Intermediate_Int  add = (shift < 0) ? 0 : ((1 << shift) >> 1);
    #else
    
        if( shift < 0 )
        {
          invQScale <<= -shift;
          shift       = 0;
        }
        Intermediate_Int  add       = ( 1 << shift ) >> 1;
    
    
        //----- dequant coefficients -----
        for( int state = 0, scanIdx = lastScanIdx; scanIdx >= 0; scanIdx-- )
        {
    
    Frank Bossen's avatar
    Frank Bossen committed
          const unsigned  rasterPos = scan[scanIdx].idx;
    
          const TCoeff&   level     = qCoeff[ rasterPos ];
          if( level )
          {
    
    #if JVET_N0847_SCALING_LISTS
            if (enableScalingLists)
              invQScale = piDequantCoef[rasterPos];//scalingfactor*levelScale
            if (shift < 0)
            {
              invQScale <<= -shift;
              shift = 0;
              //add = (1 << shift) >> 1;
            }
    #endif
    
            Intermediate_Int  qIdx      = ( level << 1 ) + ( level > 0 ? -(state>>1) : (state>>1) );
            Intermediate_Int  nomTCoeff = ( qIdx * invQScale + add ) >> shift;
            tCoeff[ rasterPos ]         = (TCoeff)Clip3<Intermediate_Int>( minTCoeff, maxTCoeff, nomTCoeff );
          }
          state = ( 32040 >> ((state<<2)+((level&1)<<1)) ) & 3;   // the 16-bit value "32040" represent the state transition table
        }
      }
    
    
    #if JVET_N0847_SCALING_LISTS
      inline void Quantizer::preQuantCoeff(const TCoeff absCoeff, PQData *pqData, int QuanCoeff) const
    #else
    
      inline void Quantizer::preQuantCoeff(const TCoeff absCoeff, PQData *pqData) const