EncSlice.cpp 81.4 KB
Newer Older
1 2 3 4 5
/* 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.
 *
6
 * Copyright (c) 2010-2018, ITU/ISO/IEC
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
 * 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     EncSlice.cpp
    \brief    slice encoder class
*/

#include "EncSlice.h"

#include "EncLib.h"
#include "CommonLib/UnitTools.h"
#include "CommonLib/Picture.h"
jamesxxiu's avatar
jamesxxiu committed
43 44 45
#if K0149_BLOCK_STATISTICS
#include "CommonLib/dtrace_blockstatistics.h"
#endif
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#if ENABLE_WPP_PARALLELISM
#include <mutex>
extern recursive_mutex g_cache_mutex;
#endif

#include <math.h>

//! \ingroup EncoderLib
//! \{

// ====================================================================================================================
// Constructor / destructor / create / destroy
// ====================================================================================================================

EncSlice::EncSlice()
 : m_encCABACTableIdx(I_SLICE)
jamesxxiu's avatar
jamesxxiu committed
63 64 65
#if ENABLE_QPA
 , m_adaptedLumaQP(-1)
#endif
66 67 68 69 70 71 72 73
{
}

EncSlice::~EncSlice()
{
  destroy();
}

Frank Bossen's avatar
Frank Bossen committed
74
void EncSlice::create( int iWidth, int iHeight, ChromaFormat chromaFormat, uint32_t iMaxCUWidth, uint32_t iMaxCUHeight, uint8_t uhTotalDepth )
75 76 77
{
}

Frank Bossen's avatar
Frank Bossen committed
78
void EncSlice::destroy()
79 80 81 82 83 84 85
{
  // free lambda and QP arrays
  m_vdRdPicLambda.clear();
  m_vdRdPicQp.clear();
  m_viRdPicQp.clear();
}

Frank Bossen's avatar
Frank Bossen committed
86
void EncSlice::init( EncLib* pcEncLib, const SPS& sps )
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
  m_pcCfg             = pcEncLib;
  m_pcLib             = pcEncLib;
  m_pcListPic         = pcEncLib->getListPic();

  m_pcGOPEncoder      = pcEncLib->getGOPEncoder();
  m_pcCuEncoder       = pcEncLib->getCuEncoder();
  m_pcInterSearch     = pcEncLib->getInterSearch();
#if JEM_TOOLS
  m_CABACDataStore    = pcEncLib->getCABACDataStore();
#endif
  m_CABACWriter       = pcEncLib->getCABACEncoder()->getCABACWriter   (&sps);
  m_CABACEstimator    = pcEncLib->getCABACEncoder()->getCABACEstimator(&sps);
  m_pcTrQuant         = pcEncLib->getTrQuant();
  m_pcRdCost          = pcEncLib->getRdCost();

  // create lambda and QP arrays
  m_vdRdPicLambda.resize(m_pcCfg->getDeltaQpRD() * 2 + 1 );
  m_vdRdPicQp.resize(    m_pcCfg->getDeltaQpRD() * 2 + 1 );
  m_viRdPicQp.resize(    m_pcCfg->getDeltaQpRD() * 2 + 1 );
  m_pcRateCtrl        = pcEncLib->getRateCtrl();
}

Frank Bossen's avatar
Frank Bossen committed
110
void
Frank Bossen's avatar
Frank Bossen committed
111
EncSlice::setUpLambda( Slice* slice, const double dLambda, int iQP)
112 113 114 115 116 117
{
  // store lambda
  m_pcRdCost ->setLambda( dLambda, slice->getSPS()->getBitDepths() );

  // for RDO
  // in RdCost there is only one lambda because the luma and chroma bits are not separated, instead we weight the distortion of chroma.
Frank Bossen's avatar
Frank Bossen committed
118
  double dLambdas[MAX_NUM_COMPONENT] = { dLambda };
Frank Bossen's avatar
Frank Bossen committed
119
  for( uint32_t compIdx = 1; compIdx < MAX_NUM_COMPONENT; compIdx++ )
120 121
  {
    const ComponentID compID = ComponentID( compIdx );
Frank Bossen's avatar
Frank Bossen committed
122 123
    int chromaQPOffset       = slice->getPPS()->getQpOffset( compID ) + slice->getSliceChromaQpDelta( compID );
    int qpc                  = ( iQP + chromaQPOffset < 0 ) ? iQP : getScaledChromaQP( iQP + chromaQPOffset, m_pcCfg->getChromaFormatIdc() );
Frank Bossen's avatar
Frank Bossen committed
124
    double tmpWeight         = pow( 2.0, ( iQP - qpc ) / 3.0 );  // takes into account of the chroma qp mapping and chroma qp Offset
125 126 127 128 129 130
#if JVET_K0072
    if( m_pcCfg->getDepQuantEnabledFlag() )
    {
      tmpWeight *= ( m_pcCfg->getGOPSize() >= 8 ? pow( 2.0, 0.1/3.0 ) : pow( 2.0, 0.2/3.0 ) );  // increase chroma weight for dependent quantization (in order to reduce bit rate shift from chroma to luma)
    }
#endif
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    m_pcRdCost->setDistortionWeight( compID, tmpWeight );
#if ENABLE_WPP_PARALLELISM
    for( int jId = 1; jId < ( m_pcLib->getNumWppThreads() + m_pcLib->getNumWppExtraLines() ); jId++ )
    {
      m_pcLib->getRdCost( slice->getPic()->scheduler.getWppDataId( jId ) )->setDistortionWeight( compID, tmpWeight );
    }
#endif
    dLambdas[compIdx] = dLambda / tmpWeight;
  }

#if RDOQ_CHROMA_LAMBDA
  // for RDOQ
  m_pcTrQuant->setLambdas( dLambdas );
#else
  m_pcTrQuant->setLambda( dLambda );
#endif

  // for SAO
  slice->setLambdas( dLambdas );
}

jamesxxiu's avatar
jamesxxiu committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#if ENABLE_QPA

static inline int apprI3Log2 (const double d) // rounded 3*log2(d)
{
  return d < 1.5e-13 ? -128 : int (floor (3.0 * log (d) / log (2.0) + 0.5));
}

static void filterAndCalculateAverageEnergies (const Pel* pSrc, const int  iSrcStride,
                                               double &hpEner,  const int  iHeight,    const int iWidth,
                                               const uint32_t uBitDepth /* luma bit-depth (4-16) */)
{
  uint64_t saAct = 0;

  // skip first row as there may be a black border frame
  pSrc += iSrcStride;
  // center rows
  for (int y = 1; y < iHeight - 1; y++)
  {
    // skip column as there may be a black border frame

    for (int x = 1; x < iWidth - 1; x++) // and columns
    {
      const int f = 12 * (int)pSrc[x  ] - 2 * ((int)pSrc[x-1] + (int)pSrc[x+1] + (int)pSrc[x  -iSrcStride] + (int)pSrc[x  +iSrcStride])
                       - (int)pSrc[x-1-iSrcStride] - (int)pSrc[x+1-iSrcStride] - (int)pSrc[x-1+iSrcStride] - (int)pSrc[x+1+iSrcStride];
      saAct += abs (f);
    }
    // skip column as there may be a black border frame
    pSrc += iSrcStride;
  }
  // skip last row as there may be a black border frame

  hpEner = double(saAct) / double((iWidth - 2) * (iHeight - 2));

  // lower limit, compensate for highpass amplification
  if (hpEner < double(1 << (uBitDepth - 4))) hpEner = double(1 << (uBitDepth - 4));
}

#ifndef GLOBAL_AVERAGING
  #define GLOBAL_AVERAGING 1 // "global" averaging of a_k across a set instead of one picture
#endif

#if GLOBAL_AVERAGING
static double getAveragePictureEnergy (const CPelBuf picOrig, const uint32_t uBitDepth)
{
  double hpEnerPic = 5.65625 * double(1 << (uBitDepth >> 1));   // square-root of a_pic value

  if (picOrig.width > 2048 && picOrig.height > 1280) // for UHD/4K
  {
    hpEnerPic *= (4.0 / 5.65625);
  }
  else if (picOrig.width <= 1024 || picOrig.height <= 640) // 480p
  {
    hpEnerPic *= (8.0 / 5.65625);
  }

  return hpEnerPic;
}
#endif

static int applyQPAdaptationChroma (Picture* const pcPic, Slice* const pcSlice, EncCfg* const pcEncCfg, const int sliceQP)
{
  double hpEner[MAX_NUM_COMPONENT] = {0.0, 0.0, 0.0};
  int    optSliceChromaQpOffset[2] = {0, 0};
  int    savedLumaQP               = -1;

  for (uint32_t comp = 0; comp < getNumberValidComponents (pcPic->chromaFormat); comp++)
  {
    const ComponentID compID = (ComponentID)comp;
    const CPelBuf    picOrig = pcPic->getOrigBuf (pcPic->block (compID));

    filterAndCalculateAverageEnergies (picOrig.buf, picOrig.stride, hpEner[comp], picOrig.height, picOrig.width,
                                       pcSlice->getSPS()->getBitDepth (toChannelType (compID)) - (isChroma (compID) ? 1 : 0));
    if (isChroma (compID))
    {
      const int  adaptChromaQPOffset = 2.0 * hpEner[comp] <= hpEner[0] ? 0 : apprI3Log2 (2.0 * hpEner[comp] / hpEner[0]);
   #if GLOBAL_AVERAGING
      int       averageAdaptedLumaQP = Clip3 (0, MAX_QP, sliceQP + apprI3Log2 (hpEner[0] / getAveragePictureEnergy (pcPic->getOrigBuf().Y(), pcSlice->getSPS()->getBitDepth (CH_L))));
   #else
      int       averageAdaptedLumaQP = Clip3 (0, MAX_QP, sliceQP); // mean slice QP
   #endif
   #if SHARP_LUMA_DELTA_QP

      // change mean picture QP index based on picture's average luma value (Sharp)
      if (pcEncCfg->getLumaLevelToDeltaQPMapping().mode == LUMALVL_TO_DQP_NUM_MODES)
      {
        const CPelBuf picLuma = pcPic->getOrigBuf().Y();
        uint64_t uAvgLuma = 0;

        for (SizeType y = 0; y < picLuma.height; y++)
        {
          for (SizeType x = 0; x < picLuma.width; x++)
          {
            uAvgLuma += (uint64_t)picLuma.at (x, y);
          }
        }
        uAvgLuma = (uAvgLuma + (picLuma.area() >> 1)) / picLuma.area();
248

jamesxxiu's avatar
jamesxxiu committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        averageAdaptedLumaQP = Clip3 (0, MAX_QP, averageAdaptedLumaQP + 1 - int((3 * uAvgLuma * uAvgLuma) >> uint64_t (2 * pcSlice->getSPS()->getBitDepth (CH_L) - 1)));
      }
   #endif
      const int lumaChromaMappingDQP = averageAdaptedLumaQP - getScaledChromaQP (averageAdaptedLumaQP, pcEncCfg->getChromaFormatIdc());

      optSliceChromaQpOffset[comp-1] = std::min (3 + lumaChromaMappingDQP, adaptChromaQPOffset + lumaChromaMappingDQP);

      if (savedLumaQP < 0) savedLumaQP = averageAdaptedLumaQP; // save it for later
    }
  }

  pcEncCfg->setSliceChromaOffsetQpIntraOrPeriodic (pcEncCfg->getSliceChromaOffsetQpPeriodicity(), optSliceChromaQpOffset);

  return savedLumaQP;
}

#endif // ENABLE_QPA
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

/**
 - non-referenced frame marking
 - QP computation based on temporal structure
 - lambda computation based on QP
 - set temporal layer ID and the parameter sets
 .
 \param pcPic         picture class
 \param pocLast       POC of last picture
 \param pocCurr       current POC
 \param iNumPicRcvd   number of received pictures
 \param iGOPid        POC offset for hierarchical structure
 \param rpcSlice      slice header class
 \param isField       true for field coding
 */
jamesxxiu's avatar
jamesxxiu committed
281 282 283 284 285
void EncSlice::initEncSlice(Picture* pcPic, const int pocLast, const int pocCurr, const int iGOPid, Slice*& rpcSlice, const bool isField
#if JVET_K0157
  , bool isEncodeLtRef
#endif
)
286
{
Frank Bossen's avatar
Frank Bossen committed
287 288
  double dQP;
  double dLambda;
289 290 291 292 293

  rpcSlice = pcPic->slices[0];
  rpcSlice->setSliceBits(0);
  rpcSlice->setPic( pcPic );
  rpcSlice->initSlice();
jamesxxiu's avatar
jamesxxiu committed
294 295 296 297 298 299 300 301 302 303 304
#if JVET_K0157
  int multipleFactor = pcPic->cs->sps->getSpsNext().getUseCompositeRef() ? 2 : 1;
  if (pcPic->cs->sps->getSpsNext().getUseCompositeRef() && isEncodeLtRef)
  {
    rpcSlice->setPicOutputFlag(false);
  }
  else
  {
    rpcSlice->setPicOutputFlag(true);
  }
#else
305
  rpcSlice->setPicOutputFlag( true );
jamesxxiu's avatar
jamesxxiu committed
306
#endif
307 308 309 310
  rpcSlice->setPOC( pocCurr );
#if JEM_TOOLS
  rpcSlice->setUseLIC( false );
#endif
311 312 313 314 315 316
#if JVET_K0072
  rpcSlice->setDepQuantEnabledFlag( m_pcCfg->getDepQuantEnabledFlag() );
#if HEVC_USE_SIGN_HIDING
  rpcSlice->setSignDataHidingEnabledFlag( m_pcCfg->getSignDataHidingEnabledFlag() );
#endif
#endif
317 318 319 320 321 322 323

#if SHARP_LUMA_DELTA_QP
  pcPic->fieldPic = isField;
  m_gopID = iGOPid;
#endif

  // depth computation based on GOP size
Frank Bossen's avatar
Frank Bossen committed
324
  int depth;
325
  {
Frank Bossen's avatar
Frank Bossen committed
326
    int poc = rpcSlice->getPOC();
327 328 329 330 331 332
    if(isField)
    {
      poc = (poc/2) % (m_pcCfg->getGOPSize()/2);
    }
    else
    {
jamesxxiu's avatar
jamesxxiu committed
333 334 335
#if JVET_K0157
      poc = poc % (m_pcCfg->getGOPSize() * multipleFactor);
#else
336
      poc = poc % m_pcCfg->getGOPSize();
jamesxxiu's avatar
jamesxxiu committed
337
#endif
338 339 340 341 342 343 344 345
    }

    if ( poc == 0 )
    {
      depth = 0;
    }
    else
    {
jamesxxiu's avatar
jamesxxiu committed
346 347 348
#if JVET_K0157
      int step = m_pcCfg->getGOPSize() * multipleFactor;
#else
Frank Bossen's avatar
Frank Bossen committed
349
      int step = m_pcCfg->getGOPSize();
jamesxxiu's avatar
jamesxxiu committed
350
#endif
351
      depth    = 0;
Frank Bossen's avatar
Frank Bossen committed
352
      for( int i=step>>1; i>=1; i>>=1 )
353
      {
jamesxxiu's avatar
jamesxxiu committed
354 355 356
#if JVET_K0157
        for (int j = i; j<(m_pcCfg->getGOPSize() * multipleFactor); j += step)
#else
Frank Bossen's avatar
Frank Bossen committed
357
        for ( int j=i; j<m_pcCfg->getGOPSize(); j+=step )
jamesxxiu's avatar
jamesxxiu committed
358
#endif
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
        {
          if ( j == poc )
          {
            i=0;
            break;
          }
        }
        step >>= 1;
        depth++;
      }
    }

    if(m_pcCfg->getHarmonizeGopFirstFieldCoupleEnabled() && poc != 0)
    {
      if (isField && ((rpcSlice->getPOC() % 2) == 1))
      {
        depth++;
      }
    }
  }

  // slice type
  SliceType eSliceType;

  eSliceType=B_SLICE;
  if(!(isField && pocLast == 1) || !m_pcCfg->getEfficientFieldIRAPEnabled())
  {
    if(m_pcCfg->getDecodingRefreshType() == 3)
    {
jamesxxiu's avatar
jamesxxiu committed
388 389 390
#if JVET_K0157
      eSliceType = (pocLast == 0 || pocCurr % (m_pcCfg->getIntraPeriod() * multipleFactor) == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
#else
391
      eSliceType = (pocLast == 0 || pocCurr % m_pcCfg->getIntraPeriod() == 0             || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
jamesxxiu's avatar
jamesxxiu committed
392
#endif
393 394 395
    }
    else
    {
jamesxxiu's avatar
jamesxxiu committed
396 397 398
#if JVET_K0157
      eSliceType = (pocLast == 0 || (pocCurr - (isField ? 1 : 0)) % (m_pcCfg->getIntraPeriod() * multipleFactor) == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
#else
399
      eSliceType = (pocLast == 0 || (pocCurr - (isField ? 1 : 0)) % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
jamesxxiu's avatar
jamesxxiu committed
400
#endif
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    }
  }

  rpcSlice->setSliceType    ( eSliceType );

  // ------------------------------------------------------------------------------------------------------------------
  // Non-referenced frame marking
  // ------------------------------------------------------------------------------------------------------------------

  if(pocLast == 0)
  {
    rpcSlice->setTemporalLayerNonReferenceFlag(false);
  }
  else
  {
    rpcSlice->setTemporalLayerNonReferenceFlag(!m_pcCfg->getGOPEntry(iGOPid).m_refPic);
  }
  pcPic->referenced = true;

  // ------------------------------------------------------------------------------------------------------------------
  // QP setting
  // ------------------------------------------------------------------------------------------------------------------

#if X0038_LAMBDA_FROM_QP_CAPABILITY
  dQP = m_pcCfg->getQPForPicture(iGOPid, rpcSlice);
#else
  dQP = m_pcCfg->getBaseQP();
  if(eSliceType!=I_SLICE)
  {
#if SHARP_LUMA_DELTA_QP
    if (!(( m_pcCfg->getMaxDeltaQP() == 0) && (!m_pcCfg->getLumaLevelToDeltaQPMapping().isEnabled()) && (dQP == -rpcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA) ) && (rpcSlice->getPPS()->getTransquantBypassEnabledFlag())))
#else
    if (!(( m_pcCfg->getMaxDeltaQP() == 0 ) && (dQP == -rpcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA) ) && (rpcSlice->getPPS()->getTransquantBypassEnabledFlag())))
#endif
    {
      dQP += m_pcCfg->getGOPEntry(iGOPid).m_QPOffset;
    }
  }

  // modify QP
Frank Bossen's avatar
Frank Bossen committed
441
  const int* pdQPs = m_pcCfg->getdQPs();
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  if ( pdQPs )
  {
    dQP += pdQPs[ rpcSlice->getPOC() ];
  }

  if (m_pcCfg->getCostMode()==COST_LOSSLESS_CODING)
  {
    dQP=LOSSLESS_AND_MIXED_LOSSLESS_RD_COST_TEST_QP;
    m_pcCfg->setDeltaQpRD(0);
  }
#endif

  // ------------------------------------------------------------------------------------------------------------------
  // Lambda computation
  // ------------------------------------------------------------------------------------------------------------------

#if X0038_LAMBDA_FROM_QP_CAPABILITY
Frank Bossen's avatar
Frank Bossen committed
459
  const int temporalId=m_pcCfg->getGOPEntry(iGOPid).m_temporalId;
460
#if !SHARP_LUMA_DELTA_QP
Frank Bossen's avatar
Frank Bossen committed
461
  const std::vector<double> &intraLambdaModifiers=m_pcCfg->getIntraLambdaModifier();
462 463
#endif
#endif
Frank Bossen's avatar
Frank Bossen committed
464
  int iQP;
Frank Bossen's avatar
Frank Bossen committed
465
  double dOrigQP = dQP;
466 467

  // pre-compute lambda and QP values for all possible QP candidates
Frank Bossen's avatar
Frank Bossen committed
468
  for ( int iDQpIdx = 0; iDQpIdx < 2 * m_pcCfg->getDeltaQpRD() + 1; iDQpIdx++ )
469 470 471 472 473 474 475
  {
    // compute QP value
    dQP = dOrigQP + ((iDQpIdx+1)>>1)*(iDQpIdx%2 ? -1 : 1);
#if SHARP_LUMA_DELTA_QP
    dLambda = calculateLambda(rpcSlice, iGOPid, depth, dQP, dQP, iQP );
#else
    // compute lambda value
Frank Bossen's avatar
Frank Bossen committed
476 477
    int    NumberBFrames = ( m_pcCfg->getGOPSize() - 1 );
    int    SHIFT_QP = 12;
478

Frank Bossen's avatar
Frank Bossen committed
479
#if DISTORTION_LAMBDA_BUGFIX
Frank Bossen's avatar
Frank Bossen committed
480
    int    bitdepth_luma_qp_scale =
Frank Bossen's avatar
Frank Bossen committed
481 482 483 484
      6
      * (rpcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8
         - DISTORTION_PRECISION_ADJUSTMENT(rpcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA)));
#else
485
#if FULL_NBIT
Frank Bossen's avatar
Frank Bossen committed
486
    int    bitdepth_luma_qp_scale = 6 * (rpcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8);
487
#else
Frank Bossen's avatar
Frank Bossen committed
488
    int    bitdepth_luma_qp_scale = 0;
Frank Bossen's avatar
Frank Bossen committed
489
#endif
490
#endif
Frank Bossen's avatar
Frank Bossen committed
491
    double qp_temp = (double) dQP + bitdepth_luma_qp_scale - SHIFT_QP;
492
#if FULL_NBIT
Frank Bossen's avatar
Frank Bossen committed
493
    double qp_temp_orig = (double) dQP - SHIFT_QP;
494 495
#endif
    // Case #1: I or P-slices (key-frame)
Frank Bossen's avatar
Frank Bossen committed
496
    double dQPFactor = m_pcCfg->getGOPEntry(iGOPid).m_QPFactor;
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    if ( eSliceType==I_SLICE )
    {
      if (m_pcCfg->getIntraQpFactor()>=0.0 && m_pcCfg->getGOPEntry(iGOPid).m_sliceType != I_SLICE)
      {
        dQPFactor=m_pcCfg->getIntraQpFactor();
      }
      else
      {
#if X0038_LAMBDA_FROM_QP_CAPABILITY
        if(m_pcCfg->getLambdaFromQPEnable())
        {
          dQPFactor=0.57;
        }
        else
        {
#endif
Frank Bossen's avatar
Frank Bossen committed
513
        double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(double)(isField ? NumberBFrames/2 : NumberBFrames) );
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

        dQPFactor=0.57*dLambda_scale;
#if X0038_LAMBDA_FROM_QP_CAPABILITY
        }
#endif
      }
    }
#if X0038_LAMBDA_FROM_QP_CAPABILITY
    else if( m_pcCfg->getLambdaFromQPEnable() )
    {
      dQPFactor=0.57;
    }
#endif

    dLambda = dQPFactor*pow( 2.0, qp_temp/3.0 );

#if X0038_LAMBDA_FROM_QP_CAPABILITY
    if(!m_pcCfg->getLambdaFromQPEnable() && depth>0)
#else
    if ( depth>0 )
#endif
    {
#if FULL_NBIT
        dLambda *= Clip3( 2.00, 4.00, (qp_temp_orig / 6.0) ); // (j == B_SLICE && p_cur_frm->layer != 0 )
#else
        dLambda *= Clip3( 2.00, 4.00, (qp_temp / 6.0) ); // (j == B_SLICE && p_cur_frm->layer != 0 )
#endif
    }

    // if hadamard is used in ME process
    if ( !m_pcCfg->getUseHADME() && rpcSlice->getSliceType( ) != I_SLICE )
    {
      dLambda *= 0.95;
    }

#if X0038_LAMBDA_FROM_QP_CAPABILITY
Frank Bossen's avatar
Frank Bossen committed
550
    double lambdaModifier;
551 552 553 554 555 556 557 558 559 560 561
    if( rpcSlice->getSliceType( ) != I_SLICE || intraLambdaModifiers.empty())
    {
      lambdaModifier = m_pcCfg->getLambdaModifier( temporalId );
    }
    else
    {
      lambdaModifier = intraLambdaModifiers[ (temporalId < intraLambdaModifiers.size()) ? temporalId : (intraLambdaModifiers.size()-1) ];
    }
    dLambda *= lambdaModifier;
#endif

jamesxxiu's avatar
jamesxxiu committed
562
    iQP = Clip3( -rpcSlice->getSPS()->getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, (int) floor( dQP + 0.5 ) );
563 564 565 566 567 568 569 570 571 572 573 574 575
#endif

    m_vdRdPicLambda[iDQpIdx] = dLambda;
    m_vdRdPicQp    [iDQpIdx] = dQP;
    m_viRdPicQp    [iDQpIdx] = iQP;
  }

  // obtain dQP = 0 case
  dLambda = m_vdRdPicLambda[0];
  dQP     = m_vdRdPicQp    [0];
  iQP     = m_viRdPicQp    [0];

#if !X0038_LAMBDA_FROM_QP_CAPABILITY
Frank Bossen's avatar
Frank Bossen committed
576
  const int temporalId=m_pcCfg->getGOPEntry(iGOPid).m_temporalId;
Frank Bossen's avatar
Frank Bossen committed
577
  const std::vector<double> &intraLambdaModifiers=m_pcCfg->getIntraLambdaModifier();
578 579 580
#endif

#if W0038_CQP_ADJ
jamesxxiu's avatar
jamesxxiu committed
581 582 583 584 585 586 587 588 589
 #if ENABLE_QPA
  m_adaptedLumaQP = -1;

  if ((m_pcCfg->getUsePerceptQPA() || m_pcCfg->getSliceChromaOffsetQpPeriodicity() > 0) && !m_pcCfg->getUseRateCtrl() && rpcSlice->getPPS()->getSliceChromaQpFlag() &&
      (rpcSlice->isIntra() || (m_pcCfg->getSliceChromaOffsetQpPeriodicity() > 0 && (rpcSlice->getPOC() % m_pcCfg->getSliceChromaOffsetQpPeriodicity()) == 0)))
  {
    m_adaptedLumaQP = applyQPAdaptationChroma (pcPic, rpcSlice, m_pcCfg, iQP);
  }
 #endif
590 591
  if(rpcSlice->getPPS()->getSliceChromaQpFlag())
  {
jamesxxiu's avatar
jamesxxiu committed
592 593 594
    const bool bUseIntraOrPeriodicOffset = rpcSlice->isIntra() || (m_pcCfg->getSliceChromaOffsetQpPeriodicity() > 0 && (rpcSlice->getPOC() % m_pcCfg->getSliceChromaOffsetQpPeriodicity()) == 0);
    int cbQP = bUseIntraOrPeriodicOffset ? m_pcCfg->getSliceChromaOffsetQpIntraOrPeriodic(false) : m_pcCfg->getGOPEntry(iGOPid).m_CbQPoffset;
    int crQP = bUseIntraOrPeriodicOffset ? m_pcCfg->getSliceChromaOffsetQpIntraOrPeriodic(true)  : m_pcCfg->getGOPEntry(iGOPid).m_CrQPoffset;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

    cbQP = Clip3( -12, 12, cbQP + rpcSlice->getPPS()->getQpOffset(COMPONENT_Cb) ) - rpcSlice->getPPS()->getQpOffset(COMPONENT_Cb);
    crQP = Clip3( -12, 12, crQP + rpcSlice->getPPS()->getQpOffset(COMPONENT_Cr) ) - rpcSlice->getPPS()->getQpOffset(COMPONENT_Cr);
    rpcSlice->setSliceChromaQpDelta(COMPONENT_Cb, Clip3( -12, 12, cbQP));
    CHECK(!(rpcSlice->getSliceChromaQpDelta(COMPONENT_Cb)+rpcSlice->getPPS()->getQpOffset(COMPONENT_Cb)<=12 && rpcSlice->getSliceChromaQpDelta(COMPONENT_Cb)+rpcSlice->getPPS()->getQpOffset(COMPONENT_Cb)>=-12), "Unspecified error");
    rpcSlice->setSliceChromaQpDelta(COMPONENT_Cr, Clip3( -12, 12, crQP));
    CHECK(!(rpcSlice->getSliceChromaQpDelta(COMPONENT_Cr)+rpcSlice->getPPS()->getQpOffset(COMPONENT_Cr)<=12 && rpcSlice->getSliceChromaQpDelta(COMPONENT_Cr)+rpcSlice->getPPS()->getQpOffset(COMPONENT_Cr)>=-12), "Unspecified error");
  }
  else
  {
    rpcSlice->setSliceChromaQpDelta( COMPONENT_Cb, 0 );
    rpcSlice->setSliceChromaQpDelta( COMPONENT_Cr, 0 );
  }
#endif

#if !X0038_LAMBDA_FROM_QP_CAPABILITY
Frank Bossen's avatar
Frank Bossen committed
611
  double lambdaModifier;
612 613 614 615 616 617 618 619 620 621 622 623 624
  if( rpcSlice->getSliceType( ) != I_SLICE || intraLambdaModifiers.empty())
  {
    lambdaModifier = m_pcCfg->getLambdaModifier( temporalId );
  }
  else
  {
    lambdaModifier = intraLambdaModifiers[ (temporalId < intraLambdaModifiers.size()) ? temporalId : (intraLambdaModifiers.size()-1) ];
  }

  dLambda *= lambdaModifier;
#endif

  setUpLambda(rpcSlice, dLambda, iQP);
jamesxxiu's avatar
jamesxxiu committed
625

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
#if WCG_EXT
  // cost = Distortion + Lambda*R,
  // when QP is adjusted by luma, distortion is changed, so we have to adjust lambda to match the distortion, then the cost function becomes
  // costA = Distortion + AdjustedLambda * R          -- currently, costA is still used when calculating intermediate cost of using SAD, HAD, resisual etc.
  // an alternative way is to weight the distortion to before the luma QP adjustment, then the cost function becomes
  // costB = weightedDistortion + Lambda * R          -- currently, costB is used to calculat final cost, and when DF_FUNC is DF_DEFAULT
  m_pcRdCost->saveUnadjustedLambda();
#endif

  if (m_pcCfg->getFastMEForGenBLowDelayEnabled())
  {
    // restore original slice type

    if(!(isField && pocLast == 1) || !m_pcCfg->getEfficientFieldIRAPEnabled())
    {
      if(m_pcCfg->getDecodingRefreshType() == 3)
      {
jamesxxiu's avatar
jamesxxiu committed
643 644 645
#if JVET_K0157
        eSliceType = (pocLast == 0 || (pocCurr) % (m_pcCfg->getIntraPeriod() * multipleFactor) == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
#else
646
        eSliceType = (pocLast == 0 || (pocCurr)                     % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
jamesxxiu's avatar
jamesxxiu committed
647
#endif
648 649 650
      }
      else
      {
jamesxxiu's avatar
jamesxxiu committed
651 652 653
#if JVET_K0157
        eSliceType = (pocLast == 0 || (pocCurr - (isField ? 1 : 0)) % (m_pcCfg->getIntraPeriod() * multipleFactor) == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
#else
654
        eSliceType = (pocLast == 0 || (pocCurr - (isField ? 1 : 0)) % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
jamesxxiu's avatar
jamesxxiu committed
655
#endif
656 657 658 659 660 661 662 663 664
      }
    }

    rpcSlice->setSliceType        ( eSliceType );
  }

  if (m_pcCfg->getUseRecalculateQPAccordingToLambda())
  {
    dQP = xGetQPValueAccordingToLambda( dLambda );
jamesxxiu's avatar
jamesxxiu committed
665
    iQP = Clip3( -rpcSlice->getSPS()->getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, (int) floor( dQP + 0.5 ) );
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
  }

  rpcSlice->setSliceQp           ( iQP );
  rpcSlice->setSliceQpDelta      ( 0 );
#if !W0038_CQP_ADJ
  rpcSlice->setSliceChromaQpDelta( COMPONENT_Cb, 0 );
  rpcSlice->setSliceChromaQpDelta( COMPONENT_Cr, 0 );
#endif
  rpcSlice->setUseChromaQpAdj( rpcSlice->getPPS()->getPpsRangeExtension().getChromaQpOffsetListEnabledFlag() );
  rpcSlice->setNumRefIdx(REF_PIC_LIST_0,m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive);
  rpcSlice->setNumRefIdx(REF_PIC_LIST_1,m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive);

  if ( m_pcCfg->getDeblockingFilterMetric() )
  {
    rpcSlice->setDeblockingFilterOverrideFlag(true);
    rpcSlice->setDeblockingFilterDisable(false);
    rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
    rpcSlice->setDeblockingFilterTcOffsetDiv2( 0 );
  }
  else if (rpcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
  {
    rpcSlice->setDeblockingFilterOverrideFlag( rpcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag() );
    rpcSlice->setDeblockingFilterDisable( rpcSlice->getPPS()->getPPSDeblockingFilterDisabledFlag() );
    if ( !rpcSlice->getDeblockingFilterDisable())
    {
      if ( rpcSlice->getDeblockingFilterOverrideFlag() && eSliceType!=I_SLICE)
      {
        rpcSlice->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getGOPEntry(iGOPid).m_betaOffsetDiv2 + m_pcCfg->getLoopFilterBetaOffset()  );
        rpcSlice->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getGOPEntry(iGOPid).m_tcOffsetDiv2 + m_pcCfg->getLoopFilterTcOffset() );
      }
      else
      {
        rpcSlice->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getLoopFilterBetaOffset() );
        rpcSlice->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getLoopFilterTcOffset() );
      }
    }
  }
  else
  {
    rpcSlice->setDeblockingFilterOverrideFlag( false );
    rpcSlice->setDeblockingFilterDisable( false );
    rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
    rpcSlice->setDeblockingFilterTcOffsetDiv2( 0 );
  }

  rpcSlice->setDepth            ( depth );

  pcPic->layer =  temporalId;
  if(eSliceType==I_SLICE)
  {
    pcPic->layer = 0;
  }
  rpcSlice->setTLayer( pcPic->layer );

  rpcSlice->setSliceMode            ( m_pcCfg->getSliceMode()            );
  rpcSlice->setSliceArgument        ( m_pcCfg->getSliceArgument()        );
#if HEVC_DEPENDENT_SLICES
  rpcSlice->setSliceSegmentMode     ( m_pcCfg->getSliceSegmentMode()     );
  rpcSlice->setSliceSegmentArgument ( m_pcCfg->getSliceSegmentArgument() );
#endif
  rpcSlice->setMaxNumMergeCand      ( m_pcCfg->getMaxNumMergeCand()      );
  rpcSlice->setMaxBTSize            ( rpcSlice->isIntra() ? MAX_BT_SIZE : MAX_BT_SIZE_INTER );
}


#if SHARP_LUMA_DELTA_QP
Frank Bossen's avatar
Frank Bossen committed
732
double EncSlice::calculateLambda( const Slice*     slice,
Frank Bossen's avatar
Frank Bossen committed
733 734
                                  const int        GOPid, // entry in the GOP table
                                  const int        depth, // slice GOP hierarchical depth.
Frank Bossen's avatar
Frank Bossen committed
735 736
                                  const double     refQP, // initial slice-level QP
                                  const double     dQP,   // initial double-precision QP
Frank Bossen's avatar
Frank Bossen committed
737
                                        int       &iQP )  // returned integer QP.
738 739
{
  enum   SliceType eSliceType    = slice->getSliceType();
Frank Bossen's avatar
Frank Bossen committed
740
  const  bool      isField       = slice->getPic()->fieldPic;
Frank Bossen's avatar
Frank Bossen committed
741 742
  const  int       NumberBFrames = ( m_pcCfg->getGOPSize() - 1 );
  const  int       SHIFT_QP      = 12;
743
#if X0038_LAMBDA_FROM_QP_CAPABILITY
Frank Bossen's avatar
Frank Bossen committed
744
  const int temporalId=m_pcCfg->getGOPEntry(GOPid).m_temporalId;
Frank Bossen's avatar
Frank Bossen committed
745
  const std::vector<double> &intraLambdaModifiers=m_pcCfg->getIntraLambdaModifier();
746 747
#endif

Frank Bossen's avatar
Frank Bossen committed
748
#if DISTORTION_LAMBDA_BUGFIX
Frank Bossen's avatar
Frank Bossen committed
749
  int bitdepth_luma_qp_scale = 6
Frank Bossen's avatar
Frank Bossen committed
750 751 752
                               * (slice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8
                                  - DISTORTION_PRECISION_ADJUSTMENT(slice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA)));
#else
753
#if FULL_NBIT
Frank Bossen's avatar
Frank Bossen committed
754
  int    bitdepth_luma_qp_scale = 6 * (slice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8);
755
#else
Frank Bossen's avatar
Frank Bossen committed
756
  int    bitdepth_luma_qp_scale = 0;
Frank Bossen's avatar
Frank Bossen committed
757
#endif
758
#endif
Frank Bossen's avatar
Frank Bossen committed
759
  double qp_temp = dQP + bitdepth_luma_qp_scale - SHIFT_QP;
760
  // Case #1: I or P-slices (key-frame)
Frank Bossen's avatar
Frank Bossen committed
761
  double dQPFactor = m_pcCfg->getGOPEntry(GOPid).m_QPFactor;
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  if ( eSliceType==I_SLICE )
  {
    if (m_pcCfg->getIntraQpFactor()>=0.0 && m_pcCfg->getGOPEntry(GOPid).m_sliceType != I_SLICE)
    {
      dQPFactor=m_pcCfg->getIntraQpFactor();
    }
    else
    {
#if X0038_LAMBDA_FROM_QP_CAPABILITY
      if(m_pcCfg->getLambdaFromQPEnable())
      {
        dQPFactor=0.57;
      }
      else
      {
#endif
Frank Bossen's avatar
Frank Bossen committed
778
        double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(double)(isField ? NumberBFrames/2 : NumberBFrames) );
779 780 781 782 783 784 785 786 787 788 789 790 791
        dQPFactor=0.57*dLambda_scale;
#if X0038_LAMBDA_FROM_QP_CAPABILITY
      }
#endif
    }
  }
#if X0038_LAMBDA_FROM_QP_CAPABILITY
  else if( m_pcCfg->getLambdaFromQPEnable() )
  {
    dQPFactor=0.57;
  }
#endif

Frank Bossen's avatar
Frank Bossen committed
792
  double dLambda = dQPFactor*pow( 2.0, qp_temp/3.0 );
793 794 795 796 797 798 799

#if X0038_LAMBDA_FROM_QP_CAPABILITY
  if( !(m_pcCfg->getLambdaFromQPEnable()) && depth>0 )
#else
  if ( depth>0 )
#endif
  {
Frank Bossen's avatar
Frank Bossen committed
800
#if DISTORTION_LAMBDA_BUGFIX
Frank Bossen's avatar
Frank Bossen committed
801
    double qp_temp_ref = refQP + bitdepth_luma_qp_scale - SHIFT_QP;
Frank Bossen's avatar
Frank Bossen committed
802 803
    dLambda *= Clip3(2.00, 4.00, (qp_temp_ref / 6.0));   // (j == B_SLICE && p_cur_frm->layer != 0 )
#else
804
#if FULL_NBIT
Frank Bossen's avatar
Frank Bossen committed
805
      double qp_temp_ref_orig = refQP - SHIFT_QP;
806 807
      dLambda *= Clip3( 2.00, 4.00, (qp_temp_ref_orig / 6.0) ); // (j == B_SLICE && p_cur_frm->layer != 0 )
#else
Frank Bossen's avatar
Frank Bossen committed
808
      double qp_temp_ref = refQP + bitdepth_luma_qp_scale - SHIFT_QP;
809
      dLambda *= Clip3( 2.00, 4.00, (qp_temp_ref / 6.0) ); // (j == B_SLICE && p_cur_frm->layer != 0 )
Frank Bossen's avatar
Frank Bossen committed
810
#endif
811 812 813 814 815 816 817 818 819 820
#endif
  }

  // if hadamard is used in ME process
  if ( !m_pcCfg->getUseHADME() && slice->getSliceType( ) != I_SLICE )
  {
    dLambda *= 0.95;
  }

#if X0038_LAMBDA_FROM_QP_CAPABILITY
Frank Bossen's avatar
Frank Bossen committed
821
  double lambdaModifier;
822 823 824 825 826 827 828 829 830 831 832
  if( eSliceType != I_SLICE || intraLambdaModifiers.empty())
  {
    lambdaModifier = m_pcCfg->getLambdaModifier( temporalId );
  }
  else
  {
    lambdaModifier = intraLambdaModifiers[ (temporalId < intraLambdaModifiers.size()) ? temporalId : (intraLambdaModifiers.size()-1) ];
  }
  dLambda *= lambdaModifier;
#endif

jamesxxiu's avatar
jamesxxiu committed
833
  iQP = Clip3( -slice->getSPS()->getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, (int) floor( dQP + 0.5 ) );
834

835 836 837 838 839 840 841
#if JVET_K0072
  if( m_pcCfg->getDepQuantEnabledFlag() )
  {
    dLambda *= pow( 2.0, 0.25/3.0 ); // slight lambda adjustment for dependent quantization (due to different slope of quantizer)
  }
#endif

842 843 844 845 846
  // NOTE: the lambda modifiers that are sometimes applied later might be best always applied in here.
  return dLambda;
}
#endif

Frank Bossen's avatar
Frank Bossen committed
847
void EncSlice::resetQP( Picture* pic, int sliceQP, double lambda )
848 849 850 851 852 853 854 855 856
{
  Slice* slice = pic->slices[0];

  // store lambda
  slice->setSliceQp( sliceQP );
  setUpLambda(slice, lambda, sliceQP);
}

#if ENABLE_QPA
jamesxxiu's avatar
jamesxxiu committed
857
static bool applyQPAdaptation (Picture* const pcPic,     Slice* const pcSlice,        const PreCalcValues& pcv,
Frank Bossen's avatar
Frank Bossen committed
858
                               const uint32_t startAddr, const uint32_t boundingAddr, const bool useSharpLumaDQP,
jamesxxiu's avatar
jamesxxiu committed
859
                               const double hpEnerAvg,   const double hpEnerMax,      const bool useFrameWiseQPA, const int previouslyAdaptedLumaQP = -1)
860 861 862 863 864 865 866
{
  const int  iBitDepth   = pcSlice->getSPS()->getBitDepth (CHANNEL_TYPE_LUMA);
  const int  iQPIndex    = pcSlice->getSliceQp(); // initial QP index for current slice, used in following loops
#if HEVC_TILES_WPP
  const TileMap& tileMap = *pcPic->tileMap;
#endif
  bool   sliceQPModified = false;
jamesxxiu's avatar
jamesxxiu committed
867 868 869 870 871
#if GLOBAL_AVERAGING
  const double hpEnerPic = 1.0 / getAveragePictureEnergy (pcPic->getOrigBuf().Y(), iBitDepth); // inverse, speed
#else
  const double hpEnerPic = 1.0 / hpEnerAvg; // speedup: multiply instead of divide in loop below; 1.0 for tuning
#endif
872

jamesxxiu's avatar
jamesxxiu committed
873
  if (useFrameWiseQPA || (iQPIndex >= MAX_QP))
874
  {
jamesxxiu's avatar
jamesxxiu committed
875
    int iQPFixed;
876

jamesxxiu's avatar
jamesxxiu committed
877 878 879 880 881 882 883 884
    if (useFrameWiseQPA)
    {
      iQPFixed = (previouslyAdaptedLumaQP < 0) ? Clip3 (0, MAX_QP, iQPIndex + apprI3Log2 (hpEnerAvg * hpEnerPic)) : previouslyAdaptedLumaQP; // average-activity slice QP
    }
    else
    {
      iQPFixed = Clip3 (0, MAX_QP, iQPIndex + ((apprI3Log2 (hpEnerAvg * hpEnerPic) + apprI3Log2 (hpEnerMax * hpEnerPic) + 1) >> 1)); // adapted slice QP = (mean(QP) + max(QP)) / 2
    }
885 886 887
#if SHARP_LUMA_DELTA_QP

    // change new fixed QP based on average CTU luma value (Sharp)
jamesxxiu's avatar
jamesxxiu committed
888
    if (useSharpLumaDQP && (iQPIndex < MAX_QP) && (previouslyAdaptedLumaQP < 0))
889
    {
Frank Bossen's avatar
Frank Bossen committed
890
      uint64_t uAvgLuma = 0;
891

Frank Bossen's avatar
Frank Bossen committed
892
      for (uint32_t ctuTsAddr = startAddr; ctuTsAddr < boundingAddr; ctuTsAddr++)
893 894
      {
#if HEVC_TILES_WPP
Frank Bossen's avatar
Frank Bossen committed
895
        const uint32_t ctuRsAddr = tileMap.getCtuTsToRsAddrMap (ctuTsAddr);
896
#else
Frank Bossen's avatar
Frank Bossen committed
897
        const uint32_t ctuRsAddr = ctuTsAddr;
898 899
#endif

Frank Bossen's avatar
Frank Bossen committed
900
        uAvgLuma += (uint64_t)pcPic->m_iOffsetCtu[ctuRsAddr];
901 902 903
      }
      uAvgLuma = (uAvgLuma + ((boundingAddr - startAddr) >> 1)) / (boundingAddr - startAddr);

Frank Bossen's avatar
Frank Bossen committed
904
      iQPFixed = Clip3 (0, MAX_QP, iQPFixed + 1 - int((3 * uAvgLuma * uAvgLuma) >> uint64_t(2 * iBitDepth - 1)));
905 906 907
    }
#endif

jamesxxiu's avatar
jamesxxiu committed
908
    if (iQPIndex >= MAX_QP) iQPFixed = MAX_QP;
909
    else
jamesxxiu's avatar
jamesxxiu committed
910
    if (iQPFixed != iQPIndex)
911 912 913 914 915 916 917 918 919 920 921 922 923
    {
      const double* oldLambdas = pcSlice->getLambdas();
      const double  corrFactor = pow (2.0, double(iQPFixed - iQPIndex) / 3.0);
      const double  newLambdas[MAX_NUM_COMPONENT] = {oldLambdas[0] * corrFactor, oldLambdas[1] * corrFactor, oldLambdas[2] * corrFactor};

      CHECK (iQPIndex != pcSlice->getSliceQpBase(), "Invalid slice QP!");
      pcSlice->setLambdas (newLambdas);
      pcSlice->setSliceQp (iQPFixed); // update the slice/base QPs
      pcSlice->setSliceQpBase (iQPFixed);

      sliceQPModified = true;
    }

Frank Bossen's avatar
Frank Bossen committed
924
    for (uint32_t ctuTsAddr = startAddr; ctuTsAddr < boundingAddr; ctuTsAddr++)
925 926
    {
#if HEVC_TILES_WPP
Frank Bossen's avatar
Frank Bossen committed
927
      const uint32_t ctuRsAddr = tileMap.getCtuTsToRsAddrMap (ctuTsAddr);
928
#else
Frank Bossen's avatar
Frank Bossen committed
929
      const uint32_t ctuRsAddr = ctuTsAddr;
930 931 932 933 934 935 936
#endif

      pcPic->m_iOffsetCtu[ctuRsAddr] = (Pel)iQPFixed; // fixed QPs
    }
  }
  else
  {
Frank Bossen's avatar
Frank Bossen committed
937
    for (uint32_t ctuTsAddr = startAddr; ctuTsAddr < boundingAddr; ctuTsAddr++)
938 939
    {
#if HEVC_TILES_WPP
Frank Bossen's avatar
Frank Bossen committed
940
      const uint32_t ctuRsAddr = tileMap.getCtuTsToRsAddrMap (ctuTsAddr);
941
#else
Frank Bossen's avatar
Frank Bossen committed
942
      const uint32_t ctuRsAddr = ctuTsAddr;
943 944
#endif

jamesxxiu's avatar
jamesxxiu committed
945
      int iQPAdapt = Clip3 (0, MAX_QP, iQPIndex + apprI3Log2 (pcPic->m_uEnerHpCtu[ctuRsAddr] * hpEnerPic));
946 947

#if SHARP_LUMA_DELTA_QP
jamesxxiu's avatar
jamesxxiu committed
948
      if (pcv.widthInCtus > 1) // try to enforce CTU SNR greater than zero dB
949
#else
jamesxxiu's avatar
jamesxxiu committed
950
      if (!pcSlice->isIntra()) // try to enforce CTU SNR greater than zero dB
951 952 953 954 955 956 957 958
#endif
      {
        const Pel      dcOffset   = pcPic->m_iOffsetCtu[ctuRsAddr];
#if SHARP_LUMA_DELTA_QP

        // change adaptive QP based on mean CTU luma value (Sharp)
        if (useSharpLumaDQP)
        {
Frank Bossen's avatar
Frank Bossen committed
959
          const uint64_t uAvgLuma   = (uint64_t)dcOffset;
960

jamesxxiu's avatar
jamesxxiu committed
961
          iQPAdapt = std::max (0, iQPAdapt + 1 - int((3 * uAvgLuma * uAvgLuma) >> uint64_t(2 * iBitDepth - 1)));
962 963 964
        }

#endif
jamesxxiu's avatar
jamesxxiu committed
965
        const uint32_t uRefScale  = g_invQuantScales[iQPAdapt % 6] << ((iQPAdapt / 6) + iBitDepth - 4);
966 967 968 969 970
        const CompArea subArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area ((ctuRsAddr % pcv.widthInCtus) * pcv.maxCUWidth, (ctuRsAddr / pcv.widthInCtus) * pcv.maxCUHeight, pcv.maxCUWidth, pcv.maxCUHeight)), pcPic->Y());
        const Pel*     pSrc       = pcPic->getOrigBuf (subArea).buf;
        const SizeType iSrcStride = pcPic->getOrigBuf (subArea).stride;
        const SizeType iSrcHeight = pcPic->getOrigBuf (subArea).height;
        const SizeType iSrcWidth  = pcPic->getOrigBuf (subArea).width;
Frank Bossen's avatar
Frank Bossen committed
971
        uint32_t uAbsDCless = 0;
972 973 974 975 976 977

        // compute sum of absolute DC-less (high-pass) luma values
        for (SizeType h = 0; h < iSrcHeight; h++)
        {
          for (SizeType w = 0; w < iSrcWidth; w++)
          {
Frank Bossen's avatar
Frank Bossen committed
978
            uAbsDCless += (uint32_t)abs (pSrc[w] - dcOffset);
979 980 981 982 983 984
          }
          pSrc += iSrcStride;
        }

        if (iSrcHeight >= 64 || iSrcWidth >= 64)  // normalization
        {
Frank Bossen's avatar
Frank Bossen committed
985
          const uint64_t blockSize = uint64_t(iSrcWidth * iSrcHeight);
986

Frank Bossen's avatar
Frank Bossen committed
987
          uAbsDCless = uint32_t((uint64_t(uAbsDCless) * 64*64 + (blockSize >> 1)) / blockSize);
988 989 990 991 992 993 994
        }

        if (uAbsDCless < 64*64) uAbsDCless = 64*64;  // limit to 1

        // reduce QP index if CTU would be fully quantized to zero
        if (uAbsDCless < uRefScale)
        {
jamesxxiu's avatar
jamesxxiu committed
995 996
          const int limit  = std::min (0, ((iQPIndex + 4) >> 3) - 6);
          const int redVal = std::max (limit, apprI3Log2 ((double)uAbsDCless / (double)uRefScale));
997

jamesxxiu's avatar
jamesxxiu committed
998
          iQPAdapt = std::max (0, iQPAdapt + redVal);
999 1000 1001 1002 1003 1004 1005 1006 1007
        }
#if SHARP_LUMA_DELTA_QP

        if (iQPAdapt > MAX_QP) iQPAdapt = MAX_QP;
#endif
      }

      pcPic->m_iOffsetCtu[ctuRsAddr] = (Pel)iQPAdapt; // adapted QPs

jamesxxiu's avatar
jamesxxiu committed
1008
      if (pcv.widthInCtus > 1) // try to reduce local bitrate peaks via minimum smoothing of the adapted QPs
1009 1010 1011 1012 1013 1014 1015 1016
      {
        iQPAdapt = ctuRsAddr % pcv.widthInCtus; // horizontal offset
        if (iQPAdapt == 0)
        {
          iQPAdapt = (ctuRsAddr > 1) ? pcPic->m_iOffsetCtu[ctuRsAddr - 2] : 0;
        }
        else // iQPAdapt >= 1
        {
jamesxxiu's avatar
jamesxxiu committed
1017
          iQPAdapt = (iQPAdapt > 1) ? std::min (pcPic->m_iOffsetCtu[ctuRsAddr - 2], pcPic->m_iOffsetCtu[ctuRsAddr]) : pcPic->m_iOffsetCtu[ctuRsAddr];
1018 1019 1020
        }
        if (ctuRsAddr > pcv.widthInCtus)
        {
jamesxxiu's avatar
jamesxxiu committed
1021
          iQPAdapt = std::min (iQPAdapt, (int)pcPic->m_iOffsetCtu[ctuRsAddr - 1 - pcv.widthInCtus]);
1022 1023 1024 1025 1026 1027 1028
        }
        if ((ctuRsAddr > 0) && (pcPic->m_iOffsetCtu[ctuRsAddr - 1] < (Pel)iQPAdapt))
        {
          pcPic->m_iOffsetCtu[ctuRsAddr - 1] = (Pel)iQPAdapt;
        }
        if ((ctuTsAddr == boundingAddr - 1) && (ctuRsAddr > pcv.widthInCtus)) // last CTU in the given slice
        {
jamesxxiu's avatar
jamesxxiu committed
1029
          iQPAdapt = std::min (pcPic->m_iOffsetCtu[ctuRsAddr - 1], pcPic->m_iOffsetCtu[ctuRsAddr - pcv.widthInCtus]);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
          if (pcPic->m_iOffsetCtu[ctuRsAddr] < (Pel)iQPAdapt)
          {
            pcPic->m_iOffsetCtu[ctuRsAddr] = (Pel)iQPAdapt;
          }
        }
      }
    } // end iteration over all CTUs in current slice
  }

  return sliceQPModified;
}
#endif // ENABLE_QPA

// ====================================================================================================================
// Public member functions
// ====================================================================================================================

//! set adaptive search range based on poc difference
Frank Bossen's avatar
Frank Bossen committed
1048
void EncSlice::setSearchRange( Slice* pcSlice )
1049
{
Frank Bossen's avatar
Frank Bossen committed
1050 1051 1052 1053 1054 1055 1056 1057
  int iCurrPOC = pcSlice->getPOC();
  int iRefPOC;
  int iGOPSize = m_pcCfg->getGOPSize();
  int iOffset = (iGOPSize >> 1);
  int iMaxSR = m_pcCfg->getSearchRange();
  int iNumPredDir = pcSlice->isInterP() ? 1 : 2;

  for (int iDir = 0; iDir < iNumPredDir; iDir++)
1058 1059
  {
    RefPicList  e = ( iDir ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
Frank Bossen's avatar
Frank Bossen committed
1060
    for (int iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(e); iRefIdx++)
1061 1062
    {
      iRefPOC = pcSlice->getRefPic(e, iRefIdx)->getPOC();
Frank Bossen's avatar
Frank Bossen committed
1063
      int newSearchRange = Clip3(m_pcCfg->getMinSearchWindow(), iMaxSR, (iMaxSR*ADAPT_SR_SCALE*abs(iCurrPOC - iRefPOC)+iOffset)/iGOPSize);
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
      m_pcInterSearch->setAdaptiveSearchRange(iDir, iRefIdx, newSearchRange);
#if ENABLE_WPP_PARALLELISM
      for( int jId = 1; jId < m_pcLib->getNumCuEncStacks(); jId++ )
      {
        m_pcLib->getInterSearch( jId )->setAdaptiveSearchRange( iDir, iRefIdx, newSearchRange );
      }
#endif
    }
  }
}

/**
 Multi-loop slice encoding for different slice QP

 \param pcPic    picture class
 */
Frank Bossen's avatar
Frank Bossen committed
1080
void EncSlice::precompressSlice( Picture* pcPic )
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
{
  // if deltaQP RD is not used, simply return
  if ( m_pcCfg->getDeltaQpRD() == 0 )
  {
    return;
  }

  if ( m_pcCfg->getUseRateCtrl() )
  {
    THROW("\nMultiple QP optimization is not allowed when rate control is enabled." );
  }

  Slice* pcSlice        = pcPic->slices[getSliceSegmentIdx()];

#if HEVC_DEPENDENT_SLICES
  if (pcSlice->getDependentSliceSegmentFlag())
  {
    // if this is a dependent slice segment, then it was optimised
    // when analysing the entire slice.
    return;
  }
#endif

  if (pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES)
  {
    // TODO: investigate use of average cost per CTU so that this Slice Mode can be used.
    THROW( "Unable to optimise Slice-level QP if Slice Mode is set to FIXED_NUMBER_OF_BYTES\n" );
  }

Frank Bossen's avatar
Frank Bossen committed
1110
  double     dPicRdCostBest = MAX_DOUBLE;
Frank Bossen's avatar
Frank Bossen committed
1111
  uint32_t       uiQpIdxBest = 0;
1112

Frank Bossen's avatar
Frank Bossen committed
1113
  double dFrameLambda;
Frank Bossen's avatar
Frank Bossen committed
1114
#if DISTORTION_LAMBDA_BUGFIX
Frank Bossen's avatar
Frank Bossen committed
1115
  int SHIFT_QP = 12
Frank Bossen's avatar
Frank Bossen committed
1116 1117 1118 1119
                 + 6
                     * (pcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8
                        - DISTORTION_PRECISION_ADJUSTMENT(pcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA)));
#else
1120
#if FULL_NBIT
Frank Bossen's avatar
Frank Bossen committed
1121
  int    SHIFT_QP = 12 + 6 * (pcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8);
1122
#else
Frank Bossen's avatar
Frank Bossen committed
1123
  int    SHIFT_QP = 12;
1124
#endif
Frank Bossen's avatar
Frank Bossen committed
1125
#endif
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

  // set frame lambda
  if (m_pcCfg->getGOPSize() > 1)
  {
    dFrameLambda = 0.68 * pow (2, (m_viRdPicQp[0]  - SHIFT_QP) / 3.0) * (pcSlice->isInterB()? 2 : 1);
  }
  else
  {
    dFrameLambda = 0.68 * pow (2, (m_viRdPicQp[0] - SHIFT_QP) / 3.0);
  }

  // for each QP candidate
Frank Bossen's avatar
Frank Bossen committed
1138
  for ( uint32_t uiQpIdx = 0; uiQpIdx < 2 * m_pcCfg->getDeltaQpRD() + 1; uiQpIdx++ )
1139 1140 1141 1142 1143 1144 1145
  {
    pcSlice       ->setSliceQp             ( m_viRdPicQp    [uiQpIdx] );
    setUpLambda(pcSlice, m_vdRdPicLambda[uiQpIdx], m_viRdPicQp    [uiQpIdx]);

    // try compress
    compressSlice   ( pcPic, true, m_pcCfg->getFastDeltaQp());

Frank Bossen's avatar
Frank Bossen committed
1146
    uint64_t uiPicDist        = m_uiPicDist; // Distortion, as calculated by compressSlice.
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
    // NOTE: This distortion is the chroma-weighted SSE distortion for the slice.
    //       Previously a standard SSE distortion was calculated (for the entire frame).
    //       Which is correct?
#if W0038_DB_OPT
    // TODO: Update loop filter, SAO and distortion calculation to work on one slice only.
    // uiPicDist = m_pcGOPEncoder->preLoopFilterPicAndCalcDist( pcPic );
#endif
    // compute RD cost and choose the best
    double dPicRdCost = double( uiPicDist ) + dFrameLambda * double( m_uiPicTotalBits );

    if ( dPicRdCost < dPicRdCostBest )
    {
      uiQpIdxBest    = uiQpIdx;
      dPicRdCostBest = dPicRdCost;
    }
  }

  // set best values
  pcSlice       ->setSliceQp             ( m_viRdPicQp    [uiQpIdxBest] );
  setUpLambda(pcSlice, m_vdRdPicLambda[uiQpIdxBest], m_viRdPicQp    [uiQpIdxBest]);
}

Frank Bossen's avatar
Frank Bossen committed
1169
void EncSlice::calCostSliceI(Picture* pcPic) // TODO: this only analyses the first slice segment. What about the others?
1170
{
Frank Bossen's avatar
Frank Bossen committed
1171
  double         iSumHadSlice      = 0;
1172 1173 1174 1175 1176 1177
  Slice * const  pcSlice           = pcPic->slices[getSliceSegmentIdx()];
#if HEVC_TILES_WPP
  const TileMap &tileMap           = *pcPic->tileMap;
#endif
  const PreCalcValues& pcv         = *pcPic->cs->pcv;
  const SPS     &sps               = *(pcSlice->getSPS());
Frank Bossen's avatar
Frank Bossen committed
1178 1179
  const int      shift             = sps.getBitDepth(CHANNEL_TYPE_LUMA)-8;
  const int      offset            = (shift>0)?(1<<(shift-1)):0;
1180 1181 1182 1183 1184

#if HEVC_DEPENDENT_SLICES
  pcSlice->setSliceSegmentBits(0);
#endif

Frank Bossen's avatar
Frank Bossen committed
1185
  uint32_t startCtuTsAddr, boundingCtuTsAddr;
1186 1187 1188
  xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic );

#if HEVC_TILES_WPP
Frank Bossen's avatar
Frank Bossen committed
1189
  for( uint32_t ctuTsAddr = startCtuTsAddr, ctuRsAddr = tileMap.getCtuTsToRsAddrMap( startCtuTsAddr);
1190 1191 1192
       ctuTsAddr < boundingCtuTsAddr;
       ctuRsAddr = tileMap.getCtuTsToRsAddrMap(++ctuTsAddr) )
#else
Frank Bossen's avatar
Frank Bossen committed
1193
  for( uint32_t ctuTsAddr = startCtuTsAddr, ctuRsAddr = startCtuTsAddr;
1194 1195 1196 1197 1198 1199
       ctuTsAddr < boundingCtuTsAddr;
       ctuRsAddr = ++ctuTsAddr )
#endif
  {
    Position pos( (ctuRsAddr % pcv.widthInCtus) * pcv.maxCUWidth, (ctuRsAddr / pcv.widthInCtus) * pcv.maxCUHeight);

Frank Bossen's avatar
Frank Bossen committed
1200 1201
    const int height  = std::min( pcv.maxCUHeight, pcv.lumaHeight - pos.y );
    const int width   = std::min( pcv.maxCUWidth,  pcv.lumaWidth  - pos.x );
1202
    const CompArea blk( COMPONENT_Y, pcv.chrFormat, pos, Size( width, height));
Frank Bossen's avatar
Frank Bossen committed
1203
    int iSumHad = m_pcCuEncoder->updateCtuDataISlice( pcPic->getOrigBuf( blk ) );
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213

    (m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr)).m_costIntra=(iSumHad+offset)>>shift;
    iSumHadSlice += (m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr)).m_costIntra;

  }
  m_pcRateCtrl->getRCPic()->setTotalIntraCost(iSumHadSlice);
}

/** \param pcPic   picture class
 */
Frank Bossen's avatar
Frank Bossen committed
1214
void EncSlice::compressSlice( Picture* pcPic, const bool bCompressEntireSlice, const bool bFastDeltaQP )
1215 1216 1217 1218 1219 1220 1221 1222
{
  // if bCompressEntireSlice is true, then the entire slice (not slice segment) is compressed,
  //   effectively disabling the slice-segment-mode.

  Slice* const pcSlice    = pcPic->slices[getSliceSegmentIdx()];
#if HEVC_TILES_WPP
  const TileMap&  tileMap = *pcPic->tileMap;
#endif
Frank Bossen's avatar
Frank Bossen committed
1223 1224
  uint32_t  startCtuTsAddr;
  uint32_t  boundingCtuTsAddr;
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

#if HEVC_DEPENDENT_SLICES
  pcSlice->setSliceSegmentBits(0);
#endif
  xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic );
  if (bCompressEntireSlice)
  {
    boundingCtuTsAddr = pcSlice->getSliceCurEndCtuTsAddr();
#if HEVC_DEPENDENT_SLICES
    pcSlice->setSliceSegmentCurEndCtuTsAddr(boundingCtuTsAddr);
#endif
  }

  // initialize cost values - these are used by precompressSlice (they should be parameters).
  m_uiPicTotalBits  = 0;
  m_uiPicDist       = 0;

  pcSlice->setSliceQpBase( pcSlice->getSliceQp() );

#if JEM_TOOLS
#if JEM_TOOLS
  m_CABACDataStore->updateBufferState(pcSlice, true);
#else
  m_CABACDataStore->updateBufferState(pcSlice);
#endif
#endif
#if JEM_TOOLS
  m_CABACDataStore->setSliceWinUpdateMode(pcSlice);
#endif
#if JEM_TOOLS
  m_CABACEstimator->initCtxModels( *pcSlice, m_CABACDataStore );
#else
  m_CABACEstimator->initCtxModels( *pcSlice );
#endif

#if ENABLE_SPLIT_PARALLELISM || ENABLE_WPP_PARALLELISM
  for( int jId = 1; jId < m_pcLib->getNumCuEncStacks(); jId++ )
  {
    CABACWriter* cw = m_pcLib->getCABACEncoder( jId )->getCABACEstimator( pcSlice->getSPS() );
#if JEM_TOOLS
    cw->initCtxModels( *pcSlice, m_CABACDataStore );
#else
    cw->initCtxModels( *pcSlice );
#endif
  }

#endif
  m_pcCuEncoder->getModeCtrl()->setFastDeltaQp(bFastDeltaQP);

jamesxxiu's avatar
jamesxxiu committed
1274 1275 1276
#if JVET_K0346
  if (pcSlice->getSPS()->getSpsNext().getUseSubPuMvp())
  {
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1277 1278 1279 1280 1281
    if (!pcSlice->isIntra()
#if JVET_K0076_CPR
        && !pcSlice->getCprIsOnlyRefPic()
#endif
       )
jamesxxiu's avatar
jamesxxiu committed
1282
    {
1283
      if (pcSlice->getPOC() > m_pcCuEncoder->getPrevPOC() && m_pcCuEncoder->getClearSubMergeStatic())
jamesxxiu's avatar
jamesxxiu committed
1284
      {
1285 1286
        m_pcCuEncoder->clearSubMergeStatics();
        m_pcCuEncoder->setClearSubMergeStatic(false);
jamesxxiu's avatar
jamesxxiu committed
1287 1288
      }

1289 1290 1291
      unsigned int layer = pcSlice->getDepth();
      unsigned int subMergeBlkSize = m_pcCuEncoder->getSubMergeBlkSize(layer);
      unsigned int subMergeBlkNum = m_pcCuEncoder->getSubMergeBlkNum(layer);
jamesxxiu's avatar
jamesxxiu committed
1292

1293
      if (subMergeBlkNum > 0)
jamesxxiu's avatar
jamesxxiu committed
1294
      {
1295 1296 1297
        unsigned int subMergeBlkSizeTh = pcSlice->getCheckLDC() ? 75 : 27;
        unsigned int aveBlkSize = subMergeBlkSize / subMergeBlkNum;
        if (aveBlkSize < (subMergeBlkSizeTh*subMergeBlkSizeTh))
jamesxxiu's avatar
jamesxxiu committed
1298
        {
1299
          pcSlice->setSubPuMvpSubblkLog2Size(2);
jamesxxiu's avatar
jamesxxiu committed
1300 1301 1302
        }
        else
        {
1303
          pcSlice->setSubPuMvpSubblkLog2Size(3);
jamesxxiu's avatar
jamesxxiu committed
1304
        }
1305
        m_pcCuEncoder->clearOneTLayerSubMergeStatics(layer);
jamesxxiu's avatar
jamesxxiu committed
1306 1307 1308
      }
      else
      {
1309 1310
        pcSlice->setSubPuMvpSubblkLog2Size(pcSlice->getSPS()->getSpsNext().getSubPuMvpLog2Size());
        CHECK(subMergeBlkSize != 0, "subMerge blksize should be 0");
jamesxxiu's avatar
jamesxxiu committed
1311 1312
      }

1313
      if (pcSlice->getSubPuMvpSubblkLog2Size() == pcSlice->getSPS()->getSpsNext().getSubPuMvpLog2Size())
jamesxxiu's avatar
jamesxxiu committed
1314
      {
1315
        pcSlice->setSubPuMvpSliceSubblkSizeEnable(false);
jamesxxiu's avatar
jamesxxiu committed
1316 1317 1318
      }
      else
      {
1319
        pcSlice->setSubPuMvpSliceSubblkSizeEnable(true);
jamesxxiu's avatar
jamesxxiu committed
1320 1321 1322 1323 1324
      }
    }
    else
    {
      m_pcCuEncoder->setPrevPOC(pcSlice->getPOC());
jamesxxiu's avatar
jamesxxiu committed
1325 1326
      if (m_pcCfg->getGOPSize() != m_pcCfg->getIntraPeriod())
      {
1327
        m_pcCuEncoder->setClearSubMergeStatic(true);
jamesxxiu's avatar
jamesxxiu committed
1328 1329 1330 1331 1332 1333
      }
      else
      {
        m_pcCuEncoder->clearSubMergeStatics();
        m_pcCuEncoder->setClearSubMergeStatic(false);
      }
jamesxxiu's avatar
jamesxxiu committed
1334 1335 1336 1337
    }
  }
#endif

1338 1339 1340 1341 1342 1343 1344 1345 1346
  //------------------------------------------------------------------------------
  //  Weighted Prediction parameters estimation.
  //------------------------------------------------------------------------------
  // calculate AC/DC values for current picture
  if( pcSlice->getPPS()->getUseWP() || pcSlice->getPPS()->getWPBiPred() )
  {
    xCalcACDCParamSlice(pcSlice);
  }

Frank Bossen's avatar
Frank Bossen committed
1347
  const bool bWp_explicit = (pcSlice->getSliceType()==P_SLICE && pcSlice->getPPS()->getUseWP()) || (pcSlice->getSliceType()==B_SLICE && pcSlice->getPPS()->getWPBiPred());
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384

  if ( bWp_explicit )
  {
    //------------------------------------------------------------------------------
    //  Weighted Prediction implemented at Slice level. SliceMode=2 is not supported yet.
    //------------------------------------------------------------------------------
#if HEVC_DEPENDENT_SLICES
    if ( pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES || pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES )
#else
    if(pcSlice->getSliceMode() == FIXED_NUMBER_OF_BYTES)
#endif
    {
      EXIT("Weighted Prediction is not yet supported with slice mode determined by max number of bins.");
    }

    xEstimateWPParamSlice( pcSlice, m_pcCfg->getWeightedPredictionMethod() );
    pcSlice->initWpScaling(pcSlice->getSPS());

    // check WP on/off
    xCheckWPEnable( pcSlice );
  }

#if JEM_TOOLS
#if HEVC_DEPENDENT_SLICES
  if( pcSlice->getSliceSegmentIdx() == 0 )
  {
#endif
    pcSlice->setUseLICOnPicLevel( m_pcCfg->getFastPicLevelLIC() );
#if HEVC_DEPENDENT_SLICES
  }
#endif
#endif

#if HEVC_DEPENDENT_SLICES
#if HEVC_TILES_WPP
  // Adjust initial state if this is the start of a dependent slice.
  {
Frank Bossen's avatar
Frank Bossen committed
1385 1386
    const uint32_t      ctuRsAddr               = tileMap.getCtuTsToRsAddrMap( startCtuTsAddr);
    const uint32_t      currentTileIdx          = tileMap.getTileIdxMap(ctuRsAddr);
1387
    const Tile&     currentTile             = tileMap.tiles[currentTileIdx];
Frank Bossen's avatar
Frank Bossen committed
1388
    const uint32_t      firstCtuRsAddrOfTile    = currentTile.getFirstCtuRsAddr();
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
    if( pcSlice->getDependentSliceSegmentFlag() && ctuRsAddr != firstCtuRsAddrOfTile )
    {
      // This will only occur if dependent slice-segments (m_entropyCodingSyncContextState=true) are being used.
      if( currentTile.getTileWidthInCtus() >= 2 || !m_pcCfg->getEntropyCodingSyncEnabledFlag() )
      {
        m_CABACEstimator->getCtx() = m_lastSliceSegmentEndContextState;
        m_CABACEstimator->start();
      }
    }
  }
#else
  // KJS: not sure if this works (but both dep slices and tiles shall be removed in VTM, so this code should not be used)
  if( pcSlice->getDependentSliceSegmentFlag() && ctuRsAddr != startCtuTsAddr )
  {
    if( pcPic->cs->pcv->widthInCtus >= 2 || !m_pcCfg->getEntropyCodingSyncEnabledFlag() )
    {
      m_CABACEstimator->getCtx() = m_lastSliceSegmentEndContextState;
      m_CABACEstimator->start();
    }
#endif
#endif

#if HEVC_DEPENDENT_SLICES
  if( !pcSlice->getDependentSliceSegmentFlag() )
  {
#endif
    pcPic->m_prevQP[0] = pcPic->m_prevQP[1] = pcSlice->getSliceQp();
#if HEVC_DEPENDENT_SLICES
  }
#endif

Frank Bossen's avatar
Frank Bossen committed
1420
  CHECK( pcPic->m_prevQP[0] == std::numeric_limits<int>::max(), "Invalid previous QP" );
1421 1422 1423 1424

  CodingStructure&  cs          = *pcPic->cs;
#if ENABLE_QPA || ENABLE_WPP_PARALLELISM
  const PreCalcValues& pcv      = *cs.pcv;
Frank Bossen's avatar
Frank Bossen committed
1425
  const uint32_t        widthInCtus = pcv.widthInCtus;
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
#endif

  cs.slice = pcSlice;

  if (startCtuTsAddr == 0)
  {
    cs.initStructData (pcSlice->getSliceQp(), pcSlice->getPPS()->getTransquantBypassEnabledFlag());
  }

#if ENABLE_QPA
  double hpEnerMax     = 1.0;
  double hpEnerPic     = 0.0;
Frank Bossen's avatar
Frank Bossen committed
1438
  int    iSrcOffset;
1439

jamesxxiu's avatar
jamesxxiu committed
1440
  if (m_pcCfg->getUsePerceptQPA() && !m_pcCfg->getUseRateCtrl())
1441
  {
Frank Bossen's avatar
Frank Bossen committed
1442
    for (uint32_t ctuTsAddr = startCtuTsAddr; ctuTsAddr < boundingCtuTsAddr; ctuTsAddr++)
1443
    {
1444
 #if HEVC_TILES_WPP
jamesxxiu's avatar
jamesxxiu committed
1445
      const uint32_t ctuRsAddr  = tileMap.getCtuTsToRsAddrMap (ctuTsAddr);
1446
 #else
jamesxxiu's avatar
jamesxxiu committed
1447
      const uint32_t ctuRsAddr  = ctuTsAddr;
1448
 #endif
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
      const Position pos ((ctuRsAddr % widthInCtus) * pcv.maxCUWidth, (ctuRsAddr / widthInCtus) * pcv.maxCUHeight);
      const CompArea subArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (pos.x, pos.y, pcv.maxCUWidth, pcv.maxCUHeight)), pcPic->Y());
      const CompArea fltArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (pos.x > 0 ? pos.x - 1 : 0, pos.y > 0 ? pos.y - 1 : 0, pcv.maxCUWidth + (pos.x > 0 ? 2 : 1), pcv.maxCUHeight + (pos.y > 0 ? 2 : 1))), pcPic->Y());
      const SizeType iSrcStride = pcPic->getOrigBuf (subArea).stride;
      const Pel*     pSrc       = pcPic->getOrigBuf (subArea).buf;
      const SizeType iSrcHeight = pcPic->getOrigBuf (subArea).height;
      const SizeType iSrcWidth  = pcPic->getOrigBuf (subArea).width;
      const SizeType iFltHeight = pcPic->getOrigBuf (fltArea).height;
      const SizeType iFltWidth  = pcPic->getOrigBuf (fltArea).width;
      double hpEner = 0.0;

      DTRACE_UPDATE (g_trace_ctx, std::make_pair ("ctu", ctuRsAddr));

      // compute DC offset to be subtracted from luma values
      iSrcOffset = 0;
      for (SizeType h = 0; h < iSrcHeight; h++)
      {
        for (SizeType w = 0; w < iSrcWidth; w++)
        {
          iSrcOffset += pSrc[w];
        }
        pSrc += iSrcStride;
      }
      CHECK (iSrcOffset < 0, "DC offset cannot be negative!");

Frank Bossen's avatar
Frank Bossen committed
1474
      int x = iSrcHeight * iSrcWidth;
1475 1476 1477
      iSrcOffset = (iSrcOffset + (x >> 1)) / x; // slow division

      filterAndCalculateAverageEnergies (pcPic->getOrigBuf (fltArea).buf, iSrcStride,
jamesxxiu's avatar
jamesxxiu committed
1478