Skip to content
Snippets Groups Projects
EncGOP.cpp 212 KiB
Newer Older
  • Learn to ignore specific revisions
  •           , (m_pcCfg->getUsePerceptQPA() && !m_pcCfg->getUseRateCtrl() && pcSlice->getPPS()->getUseDQP() ? m_pcEncLib->getRdCost(PARL_PARAM0(0))->getChromaWeight() : 0.0)
    
    #endif
              , pcPic, uiNumSliceSegments
    
            //assign ALF slice header
            for (int s = 0; s < uiNumSliceSegments; s++)
            {
    
               //For the first slice, even if it is lossless, slice level ALF is not disabled and ALF-APS is signaled so that the later lossy slices can use APS of the first slice. 
               //However, if the first slice is lossless, the ALF process is disabled for all of the CTUs ( m_ctuEnableFlag == 0) of that slice which is implemented in the function void EncAdaptiveLoopFilter::ALFProcess.         
          
              if (pcPic->slices[s]->isLossless() && s && m_pcCfg->getCostMode() == COST_LOSSLESS_CODING)
              {
                pcPic->slices[s]->setTileGroupAlfEnabledFlag(COMPONENT_Y, false);
                pcPic->slices[s]->setTileGroupAlfEnabledFlag(COMPONENT_Cb, false);
                pcPic->slices[s]->setTileGroupAlfEnabledFlag(COMPONENT_Cr, false);
              }
              else
              {
                pcPic->slices[s]->setTileGroupAlfEnabledFlag(COMPONENT_Y, cs.slice->getTileGroupAlfEnabledFlag(COMPONENT_Y));
                pcPic->slices[s]->setTileGroupAlfEnabledFlag(COMPONENT_Cb, cs.slice->getTileGroupAlfEnabledFlag(COMPONENT_Cb));
                pcPic->slices[s]->setTileGroupAlfEnabledFlag(COMPONENT_Cr, cs.slice->getTileGroupAlfEnabledFlag(COMPONENT_Cr));
    
              }
    
              if (pcPic->slices[s]->getTileGroupAlfEnabledFlag(COMPONENT_Y))
              {
                pcPic->slices[s]->setTileGroupNumAps(cs.slice->getTileGroupNumAps());
    
                pcPic->slices[s]->setAlfAPSs(cs.slice->getTileGroupApsIdLuma());
    
              pcPic->slices[s]->setAlfAPSs(cs.slice->getAlfAPSs());
              pcPic->slices[s]->setTileGroupApsIdChroma(cs.slice->getTileGroupApsIdChroma());
    
              pcPic->slices[s]->setTileGroupCcAlfCbApsId(cs.slice->getTileGroupCcAlfCbApsId());
              pcPic->slices[s]->setTileGroupCcAlfCrApsId(cs.slice->getTileGroupCcAlfCrApsId());
    
              pcPic->slices[s]->m_ccAlfFilterParam      = m_pcALF->getCcAlfFilterParam();
              pcPic->slices[s]->m_ccAlfFilterControl[0] = m_pcALF->getCcAlfControlIdc(COMPONENT_Cb);
              pcPic->slices[s]->m_ccAlfFilterControl[1] = m_pcALF->getCcAlfControlIdc(COMPONENT_Cr);
    
          DTRACE_UPDATE( g_trace_ctx, ( std::make_pair( "final", 1 ) ) );
    
          if (m_pcCfg->getUseCompositeRef() && getPrepareLTRef())
    
          {
            updateCompositeReference(pcSlice, rcListPic, pocCurr);
          }
    
        }
        else // skip enc picture
        {
          pcSlice->setSliceQpBase( pcSlice->getSliceQp() );
    
    
    #if ENABLE_QPA
          if (m_pcCfg->getUsePerceptQPA() && !m_pcCfg->getUseRateCtrl() && pcSlice->getPPS()->getUseDQP())
          {
            const double picLambda = pcSlice->getLambdas()[0];
    
            for (uint32_t ctuRsAddr = 0; ctuRsAddr < numberOfCtusInFrame; ctuRsAddr++)
            {
              pcPic->m_uEnerHpCtu[ctuRsAddr] = picLambda;  // initialize to slice lambda (just for safety)
            }
          }
    #endif
    
          if( pcSlice->getSPS()->getSAOEnabledFlag() )
    
          {
            m_pcSAO->disabledRate( *pcPic->cs, pcPic->getSAO(1), m_pcCfg->getSaoEncodingRate(), m_pcCfg->getSaoEncodingRateChroma());
          }
    
          if (pcSlice->getSPS()->getALFEnabledFlag() && (pcSlice->getTileGroupAlfEnabledFlag(COMPONENT_Y) || pcSlice->getTileGroupCcAlfCbEnabledFlag() || pcSlice->getTileGroupCcAlfCrEnabledFlag()))
          {
            // IRAP AU: reset APS map
            {
              int layerIdx = pcSlice->getVPS() == nullptr ? 0 : pcSlice->getVPS()->getGeneralLayerIdx( pcSlice->getPic()->layerId );
              if( !layerIdx && ( pcSlice->getPendingRasInit() || pcSlice->isIDRorBLA() ) )
              {
                // We have to reset all APS on IRAP, but in not encoding case we have to keep the parsed APS of current slice
                // Get active ALF APSs from picture/slice header
                const std::vector<int> sliceApsIdsLuma = pcSlice->getTileGroupApsIdLuma();
    
                m_pcALF->setApsIdStart( ALF_CTB_MAX_NUM_APS );
    
                ParameterSetMap<APS>* apsMap = m_pcEncLib->getApsMap();
                apsMap->clear();
    
                for( int apsId = 0; apsId < ALF_CTB_MAX_NUM_APS; apsId++ )
                {
                  int psId = ( apsId << NUM_APS_TYPE_LEN ) + ALF_APS;
                  APS* aps = apsMap->getPS( psId );
                  if( aps )
                  {
                    // Check if this APS is currently the active one (used in current slice)
                    bool activeAps = false;
                    bool activeApsCcAlf = false;
                    // Luma
                    for( int i = 0; i < sliceApsIdsLuma.size(); i++ )
                    {
                      if( aps->getAPSId() == sliceApsIdsLuma[i] )
                      {
                        activeAps = true;
                        break;
                      }
                    }
                    // Chroma
                    activeAps |= aps->getAPSId() == pcSlice->getTileGroupApsIdChroma();
                    // CC-ALF
                    activeApsCcAlf |= pcSlice->getTileGroupCcAlfCbEnabledFlag() && aps->getAPSId() == pcSlice->getTileGroupCcAlfCbApsId();
                    activeApsCcAlf |= pcSlice->getTileGroupCcAlfCrEnabledFlag() && aps->getAPSId() == pcSlice->getTileGroupCcAlfCrApsId();
                    if( !activeAps && !activeApsCcAlf )
                    {
                      apsMap->clearChangedFlag( psId );
                    }
                    if( !activeAps  )
                    {
                      aps->getAlfAPSParam().reset();
                    }
                    if( !activeApsCcAlf )
                    {
                      aps->getCcAlfAPSParam().reset();
                    }
                  }
                }
              }
            }
    
            // Assign tne correct APS to slice and emulate the setting of ALF start APS ID
            int changedApsId = -1;
            for( int apsId = ALF_CTB_MAX_NUM_APS - 1; apsId >= 0; apsId-- )
            {
              ParameterSetMap<APS>* apsMap = m_pcEncLib->getApsMap();
              int psId = ( apsId << NUM_APS_TYPE_LEN ) + ALF_APS;
              APS* aps = apsMap->getPS( psId );
              if( aps )
              {
                // In slice, replace the old APS (from decoder map) with the APS from encoder map due to later checks while bitstream writing
                if( pcSlice->getAlfAPSs() && pcSlice->getAlfAPSs()[apsId] )
                {
                  pcSlice->getAlfAPSs()[apsId] = aps;
                }
                if( apsMap->getChangedFlag( psId ) )
                  changedApsId = apsId;
              }
            }
            if( changedApsId >= 0 )
              m_pcALF->setApsIdStart( changedApsId );
          }
    
        pcSlice->freeScaledRefPicList( scaledRefPic );
    
    
        if( m_pcCfg->getUseAMaxBT() )
        {
          for( const CodingUnit *cu : pcPic->cs->cus )
          {
    
            {
              m_uiBlkSize[pcSlice->getDepth()] += cu->Y().area();
              m_uiNumBlk [pcSlice->getDepth()]++;
            }
          }
        }
    
        if( encPic || decPic )
        {
          pcSlice = pcPic->slices[0];
    
          /////////////////////////////////////////////////////////////////////////////////////////////////// File writing
    
          // write various parameter sets
    
          bool writePS = m_bSeqFirst || (m_pcCfg->getReWriteParamSets() && (pcSlice->isIRAP()));
          if (writePS)
          {
            m_pcEncLib->setParamSetChanged(pcSlice->getSPS()->getSPSId(), pcSlice->getPPS()->getPPSId());
          }
    
          int layerIdx = m_pcEncLib->getVPS() == nullptr ? 0 : m_pcEncLib->getVPS()->getGeneralLayerIdx( m_pcEncLib->getLayerId() );
    
          // it is assumed that layerIdx equal to 0 is always present
          m_audIrapOrGdrAuFlag = pcSlice->getPicHeader()->getGdrPicFlag() || (pcSlice->isIRAP() && !pcSlice->getPPS()->getMixedNaluTypesInPicFlag());
          if ((( m_pcEncLib->getVPS()->getMaxLayers() > 1 && m_audIrapOrGdrAuFlag) || m_pcCfg->getAccessUnitDelimiter()) && !layerIdx )
          {
            xWriteAccessUnitDelimiter(accessUnit, pcSlice);
          }
    
    
          // it is assumed that layerIdx equal to 0 is always present
    
    BDChoi's avatar
    BDChoi committed
          actualTotalBits += xWriteParameterSets(accessUnit, pcSlice, writePS, layerIdx);
    
          {
            // create prefix SEI messages at the beginning of the sequence
            CHECK(!(leadingSeiMessages.empty()), "Unspecified error");
            xCreateIRAPLeadingSEIMessages(leadingSeiMessages, pcSlice->getSPS(), pcSlice->getPPS());
    
            m_bSeqFirst = false;
          }
    
    
          //send LMCS APS when LMCSModel is updated. It can be updated even current slice does not enable reshaper.
          //For example, in RA, update is on intra slice, but intra slice may not use reshaper
    
          if (pcSlice->getSPS()->getUseLmcs())
    
          {
            //only 1 LMCS data for 1 picture
    
    Brian Heng's avatar
    Brian Heng committed
            int apsId = picHeader->getLmcsAPSId();
    
            ParameterSetMap<APS> *apsMap = m_pcEncLib->getApsMap();
            APS* aps = apsMap->getPS((apsId << NUM_APS_TYPE_LEN) + LMCS_APS);
            bool writeAPS = aps && apsMap->getChangedFlag((apsId << NUM_APS_TYPE_LEN) + LMCS_APS);
            if (writeAPS)
            {
    
    #if JVET_R0433
              aps->chromaPresentFlag = pcSlice->getSPS()->getChromaFormatIdc() != CHROMA_400;
    #endif
    
              actualTotalBits += xWriteAPS( accessUnit, aps, m_pcEncLib->getLayerId(), true );
    
              apsMap->clearChangedFlag((apsId << NUM_APS_TYPE_LEN) + LMCS_APS);
    
    Brian Heng's avatar
    Brian Heng committed
              CHECK(aps != picHeader->getLmcsAPS(), "Wrong LMCS APS pointer in compressGOP");
    
          // only 1 SCALING LIST data for 1 picture
    
          if( pcSlice->getSPS()->getScalingListFlag() && ( m_pcCfg->getUseScalingListId() == SCALING_LIST_FILE_READ ) )
          {
    
    Brian Heng's avatar
    Brian Heng committed
            int apsId = picHeader->getScalingListAPSId();
    
            ParameterSetMap<APS> *apsMap = m_pcEncLib->getApsMap();
            APS* aps = apsMap->getPS( ( apsId << NUM_APS_TYPE_LEN ) + SCALING_LIST_APS );
            bool writeAPS = aps && apsMap->getChangedFlag( ( apsId << NUM_APS_TYPE_LEN ) + SCALING_LIST_APS );
            if( writeAPS )
            {
    
    #if JVET_R0433
              aps->chromaPresentFlag = pcSlice->getSPS()->getChromaFormatIdc() != CHROMA_400;
    #endif
    
              actualTotalBits += xWriteAPS( accessUnit, aps, m_pcEncLib->getLayerId(), true );
    
              apsMap->clearChangedFlag( ( apsId << NUM_APS_TYPE_LEN ) + SCALING_LIST_APS );
    
    Brian Heng's avatar
    Brian Heng committed
              CHECK( aps != picHeader->getScalingListAPS(), "Wrong SCALING LIST APS pointer in compressGOP" );
    
          if (pcSlice->getSPS()->getALFEnabledFlag() && (pcSlice->getTileGroupAlfEnabledFlag(COMPONENT_Y) || pcSlice->getTileGroupCcAlfCbEnabledFlag() || pcSlice->getTileGroupCcAlfCrEnabledFlag()))
    
            for (int apsId = 0; apsId < ALF_CTB_MAX_NUM_APS; apsId++)
    
            {
              ParameterSetMap<APS> *apsMap = m_pcEncLib->getApsMap();
    
              APS* aps = apsMap->getPS((apsId << NUM_APS_TYPE_LEN) + ALF_APS);
              bool writeAPS = aps && apsMap->getChangedFlag((apsId << NUM_APS_TYPE_LEN) + ALF_APS);
              if (!aps && pcSlice->getAlfAPSs() && pcSlice->getAlfAPSs()[apsId])
              {
                writeAPS = true;
                aps = pcSlice->getAlfAPSs()[apsId]; // use asp from slice header
    
                *apsMap->allocatePS((apsId << NUM_APS_TYPE_LEN) + ALF_APS) = *aps; //allocate and cpy
    
                m_pcALF->setApsIdStart( apsId );
              }
    
              else if (pcSlice->getTileGroupCcAlfCbEnabledFlag() && !aps && apsId == pcSlice->getTileGroupCcAlfCbApsId())
              {
                writeAPS = true;
                aps = apsMap->getPS((pcSlice->getTileGroupCcAlfCbApsId() << NUM_APS_TYPE_LEN) + ALF_APS);
              }
              else if (pcSlice->getTileGroupCcAlfCrEnabledFlag() && !aps && apsId == pcSlice->getTileGroupCcAlfCrApsId())
              {
                writeAPS = true;
                aps = apsMap->getPS((pcSlice->getTileGroupCcAlfCrApsId() << NUM_APS_TYPE_LEN) + ALF_APS);
              }
    
              if (writeAPS )
    
    #if JVET_R0433
                aps->chromaPresentFlag = pcSlice->getSPS()->getChromaFormatIdc() != CHROMA_400;
    #endif
    
                actualTotalBits += xWriteAPS( accessUnit, aps, m_pcEncLib->getLayerId(), true );
    
                apsMap->clearChangedFlag((apsId << NUM_APS_TYPE_LEN) + ALF_APS);
    
                CHECK(aps != pcSlice->getAlfAPSs()[apsId] && apsId != pcSlice->getTileGroupCcAlfCbApsId() && apsId != pcSlice->getTileGroupCcAlfCrApsId(), "Wrong APS pointer in compressGOP");
    
    
          // reset presence of BP SEI indication
          m_bufferingPeriodSEIPresentInAU = false;
          // create prefix SEI associated with a picture
          xCreatePerPictureSEIMessages(iGOPid, leadingSeiMessages, nestedSeiMessages, pcSlice);
    
          // pcSlice is currently slice 0.
          std::size_t binCountsInNalUnits   = 0; // For implementation of cabac_zero_word stuffing (section 7.4.3.10)
          std::size_t numBytesInVclNalUnits = 0; // For implementation of cabac_zero_word stuffing (section 7.4.3.10)
    
    #if JVET_Q0406_CABAC_ZERO
          std::size_t sumZeroWords          = 0; // sum of cabac_zero_word inserted per sub-picture
          std::vector<EncBitstreamParams> subPicStats (pcPic->cs->pps->getNumSubPics());
    #endif
    
          for(uint32_t sliceSegmentIdxCount = 0; sliceSegmentIdxCount < pcPic->cs->pps->getNumSlicesInPic(); sliceSegmentIdxCount++ )
    
          {
            pcSlice = pcPic->slices[sliceSegmentIdxCount];
            if(sliceSegmentIdxCount > 0 && pcSlice->getSliceType()!= I_SLICE)
            {
              pcSlice->checkColRefIdx(sliceSegmentIdxCount, pcPic);
            }
            m_pcSliceEncoder->setSliceSegmentIdx(sliceSegmentIdxCount);
    
    
    Hendry's avatar
    Hendry committed
            pcSlice->setRPL0(pcPic->slices[0]->getRPL0());
            pcSlice->setRPL1(pcPic->slices[0]->getRPL1());
            pcSlice->setRPL0idx(pcPic->slices[0]->getRPL0idx());
            pcSlice->setRPL1idx(pcPic->slices[0]->getRPL1idx());
    
            picHeader->setNoOutputBeforeRecoveryFlag( false );
    
    Brian Heng's avatar
    Brian Heng committed
            if (pcSlice->isIRAP())
            {
              if (pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_IDR_W_RADL && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_IDR_N_LP)
              {
    
                picHeader->setNoOutputBeforeRecoveryFlag( true );
    
    Brian Heng's avatar
    Brian Heng committed
              }
              //the inference for NoOutputPriorPicsFlag
              // KJS: This cannot happen at the encoder
    
              if( !m_bFirst && ( pcSlice->isIRAP() || pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_GDR ) && picHeader->getNoOutputBeforeRecoveryFlag() )
    
    Brian Heng's avatar
    Brian Heng committed
              {
                if (pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA || pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_GDR)
                {
    
    bdchoi's avatar
    bdchoi committed
    #if JVET_S0193_NO_OUTPUT_PRIOR_PIC
                  pcSlice->setNoOutputOfPriorPicsFlag(true);
    #else
    
    Brian Heng's avatar
    Brian Heng committed
                  picHeader->setNoOutputOfPriorPicsFlag(true);
    
    bdchoi's avatar
    bdchoi committed
    #endif
    
    Brian Heng's avatar
    Brian Heng committed
                }
              }
            }
    
            // code picture header before first slice
    
            if(sliceSegmentIdxCount == 0)
    
    Brian Heng's avatar
    Brian Heng committed
            {
              // code RPL in picture header or slice headers
              if( !m_pcCfg->getSliceLevelRpl() && (!pcSlice->getIdrPicFlag() || pcSlice->getSPS()->getIDRRefParamListPresent()) )
              {
                picHeader->setRPL0idx(pcSlice->getRPL0idx());
                picHeader->setRPL1idx(pcSlice->getRPL1idx());
                picHeader->setRPL0(pcSlice->getRPL0());
                picHeader->setRPL1(pcSlice->getRPL1());
                *picHeader->getLocalRPL0() = *pcSlice->getLocalRPL0();
                *picHeader->getLocalRPL1() = *pcSlice->getLocalRPL1();
              }
    
    Brian Heng's avatar
    Brian Heng committed
              // code DBLK in picture header or slice headers
              if( !m_pcCfg->getSliceLevelDblk() )
              {
                picHeader->setDeblockingFilterOverrideFlag   ( pcSlice->getDeblockingFilterOverrideFlag()   );
    
                picHeader->setDeblockingFilterDisable        ( pcSlice->getDeblockingFilterDisable()        );
                picHeader->setDeblockingFilterBetaOffsetDiv2 ( pcSlice->getDeblockingFilterBetaOffsetDiv2() );
    
    Brian Heng's avatar
    Brian Heng committed
                picHeader->setDeblockingFilterTcOffsetDiv2   ( pcSlice->getDeblockingFilterTcOffsetDiv2()   );
    
                picHeader->setDeblockingFilterCbBetaOffsetDiv2( pcSlice->getDeblockingFilterCbBetaOffsetDiv2() );
                picHeader->setDeblockingFilterCbTcOffsetDiv2  ( pcSlice->getDeblockingFilterCbTcOffsetDiv2() );
                picHeader->setDeblockingFilterCrBetaOffsetDiv2( pcSlice->getDeblockingFilterCrBetaOffsetDiv2() );
                picHeader->setDeblockingFilterCrTcOffsetDiv2  ( pcSlice->getDeblockingFilterCrTcOffsetDiv2() );
    
    Hendry's avatar
    Hendry committed
    
              if (!m_pcCfg->getSliceLevelDeltaQp())
              {
                picHeader->setQpDelta(pcSlice->getSliceQp() - (pcSlice->getPPS()->getPicInitQPMinus26() + 26));
              }
    
    
    Brian Heng's avatar
    Brian Heng committed
              // code SAO parameters in picture header or slice headers
              if( !m_pcCfg->getSliceLevelSao() )
              {
                picHeader->setSaoEnabledFlag(CHANNEL_TYPE_LUMA,   pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA  ));
                picHeader->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA));
              }
    
    Brian Heng's avatar
    Brian Heng committed
              // code ALF parameters in picture header or slice headers
              if( !m_pcCfg->getSliceLevelAlf() )
              {
                picHeader->setAlfEnabledFlag(COMPONENT_Y,  pcSlice->getTileGroupAlfEnabledFlag(COMPONENT_Y ) );
                picHeader->setAlfEnabledFlag(COMPONENT_Cb, pcSlice->getTileGroupAlfEnabledFlag(COMPONENT_Cb) );
    
                picHeader->setAlfEnabledFlag(COMPONENT_Cr, pcSlice->getTileGroupAlfEnabledFlag(COMPONENT_Cr) );
    
    Brian Heng's avatar
    Brian Heng committed
                picHeader->setNumAlfAps(pcSlice->getTileGroupNumAps());
                picHeader->setAlfAPSs(pcSlice->getTileGroupApsIdLuma());
                picHeader->setAlfApsIdChroma(pcSlice->getTileGroupApsIdChroma());
    
                picHeader->setCcAlfEnabledFlag(COMPONENT_Cb, pcSlice->getTileGroupCcAlfCbEnabledFlag());
                picHeader->setCcAlfEnabledFlag(COMPONENT_Cr, pcSlice->getTileGroupCcAlfCrEnabledFlag());
                picHeader->setCcAlfCbApsId(pcSlice->getTileGroupCcAlfCbApsId());
                picHeader->setCcAlfCrApsId(pcSlice->getTileGroupCcAlfCrApsId());
    
    Hendry's avatar
    Hendry committed
    
              // code WP parameters in picture header or slice headers
              if (!m_pcCfg->getSliceLevelWp())
              {
                picHeader->setWpScaling(pcSlice->getWpScalingAll());
                picHeader->setNumL0Weights(pcSlice->getNumRefIdx(REF_PIC_LIST_0));
                picHeader->setNumL0Weights(pcSlice->getNumRefIdx(REF_PIC_LIST_1));
              }
    
    Brian Heng's avatar
    Brian Heng committed
    
              pcPic->cs->picHeader->setPic(pcPic);
              pcPic->cs->picHeader->setValid();
    
              if (pcPic->cs->pps->getNumSlicesInPic() > 1 || !m_pcCfg->getEnablePictureHeaderInSliceHeader())
    
                pcSlice->setPictureHeaderInSliceHeader(false);
    
                actualTotalBits += xWritePicHeader(accessUnit, pcPic->cs->picHeader);
              }
    
    bdchoi's avatar
    bdchoi committed
              if (pcSlice->getSPS()->getProfileTierLevel()->getConstraintInfo()->getPicHeaderInSliceHeaderConstraintFlag())
              {
                CHECK(pcSlice->getPictureHeaderInSliceHeader() == false, "PH shall be present in SH, when pic_header_in_slice_header_constraint_flag is equal to 1");
              }
    
    Brian Heng's avatar
    Brian Heng committed
            }
            pcSlice->setPicHeader( pcPic->cs->picHeader );
    
            pcSlice->setNalUnitLayerId( m_pcEncLib->getLayerId() );
    
            for ( uint32_t ui = 0 ; ui < numSubstreams; ui++ )
            {
              substreamsOut[ui].clear();
            }
    
            /* start slice NALunit */
    
    Vadim Seregin's avatar
    Vadim Seregin committed
            OutputNALUnit nalu( pcSlice->getNalUnitType(), m_pcEncLib->getLayerId(), pcSlice->getTLayer() );
    
            m_HLSWriter->setBitstream( &nalu.m_Bitstream );
    
    
            tmpBitsBeforeWriting = m_HLSWriter->getNumberOfWrittenBits();
            m_HLSWriter->codeSliceHeader( pcSlice );
            actualHeadBits += ( m_HLSWriter->getNumberOfWrittenBits() - tmpBitsBeforeWriting );
    
            pcSlice->setFinalized(true);
    
    
    Hendry's avatar
    Hendry committed
            pcSlice->resetNumberOfSubstream( );
            pcSlice->setNumSubstream( pcSlice->getSPS(), pcSlice->getPPS() );
    
    #if JVET_Q0406_CABAC_ZERO
            const int subpicIdx = pcPic->cs->pps->getSubPicIdxFromSubPicId(pcSlice->getSliceSubPicId());
    #endif
    
            {
              uint32_t numBinsCoded = 0;
              m_pcSliceEncoder->encodeSlice(pcPic, &(substreamsOut[0]), numBinsCoded);
              binCountsInNalUnits+=numBinsCoded;
    
    #if JVET_Q0406_CABAC_ZERO
              subPicStats[subpicIdx].numBinsWritten += numBinsCoded;
    #endif
    
            }
            {
              // Construct the final bitstream by concatenating substreams.
              // The final bitstream is either nalu.m_Bitstream or pcBitstreamRedirect;
              // Complete the slice header info.
              m_HLSWriter->setBitstream( &nalu.m_Bitstream );
              m_HLSWriter->codeTilesWPPEntryPoint( pcSlice );
    
              // Append substreams...
              OutputBitstream *pcOut = pcBitstreamRedirect;
    
    Hendry's avatar
    Hendry committed
              const int numSubstreamsToCode = pcSlice->getNumberOfSubstream() + 1;
    
    
              for ( uint32_t ui = 0 ; ui < numSubstreamsToCode; ui++ )
              {
    
                pcOut->addSubstream(&(substreamsOut[ui]));
    
              }
            }
    
            // If current NALU is the first NALU of slice (containing slice header) and more NALUs exist (due to multiple dependent slices) then buffer it.
            // If current NALU is the last NALU of slice and a NALU was buffered, then (a) Write current NALU (b) Update an write buffered NALU at approproate location in NALU list.
            bool bNALUAlignedWrittenToList    = false; // used to ensure current NALU is not written more than once to the NALU list.
            xAttachSliceDataToNalUnit(nalu, pcBitstreamRedirect);
            accessUnit.push_back(new NALUnitEBSP(nalu));
            actualTotalBits += uint32_t(accessUnit.back()->m_nalUnitData.str().size()) * 8;
            numBytesInVclNalUnits += (std::size_t)(accessUnit.back()->m_nalUnitData.str().size());
    
    #if JVET_Q0406_CABAC_ZERO
            subPicStats[subpicIdx].numBytesInVclNalUnits += (std::size_t)(accessUnit.back()->m_nalUnitData.str().size());
    #endif
    
            bNALUAlignedWrittenToList = true;
    
            if (!bNALUAlignedWrittenToList)
            {
              nalu.m_Bitstream.writeAlignZero();
              accessUnit.push_back(new NALUnitEBSP(nalu));
            }
    
    
            if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) &&
    
            ((pcSlice->getSPS()->getGeneralHrdParameters()->getGeneralNalHrdParametersPresentFlag())
              || (pcSlice->getSPS()->getGeneralHrdParameters()->getGeneralVclHrdParametersPresentFlag())) &&
              (pcSlice->getSPS()->getGeneralHrdParameters()->getGeneralDecodingUnitHrdParamsPresentFlag()))
    
              uint32_t numRBSPBytes = 0;
              for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
              {
                numRBSPBytes += uint32_t((*it)->m_nalUnitData.str().size());
                numNalus ++;
              }
              duData.push_back(DUData());
              duData.back().accumBitsDU = ( numRBSPBytes << 3 );
              duData.back().accumNalsDU = numNalus;
            }
    
    #if JVET_Q0406_CABAC_ZERO
            if (pcSlice->isLastSliceInSubpic())
            {
              // Check picture level encoding constraints/requirements
              ProfileLevelTierFeatures profileLevelTierFeatures;
              profileLevelTierFeatures.extractPTLInformation(*(pcSlice->getSPS()));
              sumZeroWords += cabac_zero_word_padding(pcSlice, pcPic, subPicStats[subpicIdx].numBinsWritten, subPicStats[subpicIdx].numBytesInVclNalUnits, 0,
                                                      accessUnit.back()->m_nalUnitData, m_pcCfg->getCabacZeroWordPaddingEnabled(), profileLevelTierFeatures);
            }
    #endif
    
          {
            // Check picture level encoding constraints/requirements
            ProfileLevelTierFeatures profileLevelTierFeatures;
            profileLevelTierFeatures.extractPTLInformation(*(pcSlice->getSPS()));
            validateMinCrRequirements(profileLevelTierFeatures, numBytesInVclNalUnits, pcPic, m_pcCfg);
            // cabac_zero_words processing
    
    #if JVET_Q0406_CABAC_ZERO
            cabac_zero_word_padding(pcSlice, pcPic, binCountsInNalUnits, numBytesInVclNalUnits, sumZeroWords, accessUnit.back()->m_nalUnitData, m_pcCfg->getCabacZeroWordPaddingEnabled(), profileLevelTierFeatures);
    #else
    
            cabac_zero_word_padding(pcSlice, pcPic, binCountsInNalUnits, numBytesInVclNalUnits, accessUnit.back()->m_nalUnitData, m_pcCfg->getCabacZeroWordPaddingEnabled(), profileLevelTierFeatures);
    
    
          //-- For time output for each slice
          auto elapsed = std::chrono::steady_clock::now() - beforeTime;
          auto encTime = std::chrono::duration_cast<std::chrono::seconds>( elapsed ).count();
    
          std::string digestStr;
          if (m_pcCfg->getDecodedPictureHashSEIType()!=HASHTYPE_NONE)
          {
            SEIDecodedPictureHash *decodedPictureHashSei = new SEIDecodedPictureHash();
            PelUnitBuf recoBuf = pcPic->cs->getRecoBuf();
            m_seiEncoder.initDecodedPictureHashSEI(decodedPictureHashSei, recoBuf, digestStr, pcSlice->getSPS()->getBitDepths());
            trailingSeiMessages.push_back(decodedPictureHashSei);
          }
    
          m_pcCfg->setEncodedFlag(iGOPid, true);
    
          double PSNR_Y;
    
    Vadim Seregin's avatar
    Vadim Seregin committed
          xCalculateAddPSNRs(isField, isTff, iGOPid, pcPic, accessUnit, rcListPic, encTime, snr_conversion, printFrameMSE, &PSNR_Y, isEncodeLtRef );
    
          xWriteTrailingSEIMessages(trailingSeiMessages, accessUnit, pcSlice->getTLayer());
    
    
          printHash(m_pcCfg->getDecodedPictureHashSEIType(), digestStr);
    
          if ( m_pcCfg->getUseRateCtrl() )
          {
            double avgQP     = m_pcRateCtrl->getRCPic()->calAverageQP();
            double avgLambda = m_pcRateCtrl->getRCPic()->calAverageLambda();
            if ( avgLambda < 0.0 )
            {
              avgLambda = lambda;
            }
    
    
            m_pcRateCtrl->getRCPic()->updateAfterPicture( actualHeadBits, actualTotalBits, avgQP, avgLambda, pcSlice->isIRAP());
    
            m_pcRateCtrl->getRCPic()->addToPictureLsit( m_pcRateCtrl->getPicList() );
    
            m_pcRateCtrl->getRCSeq()->updateAfterPic( actualTotalBits );
    
            if ( !pcSlice->isIRAP() )
    
            {
              m_pcRateCtrl->getRCGOP()->updateAfterPicture( actualTotalBits );
            }
            else    // for intra picture, the estimated bits are used to update the current status in the GOP
            {
              m_pcRateCtrl->getRCGOP()->updateAfterPicture( estimatedBits );
            }
      #if U0132_TARGET_BITS_SATURATION
            if (m_pcRateCtrl->getCpbSaturationEnabled())
            {
              m_pcRateCtrl->updateCpbState(actualTotalBits);
              msg( NOTICE, " [CPB %6d bits]", m_pcRateCtrl->getCpbState() );
            }
      #endif
          }
    
          xCreateFrameFieldInfoSEI( leadingSeiMessages, pcSlice, isField );
    
          xCreatePictureTimingSEI( m_pcCfg->getEfficientFieldIRAPEnabled() ? effFieldIRAPMap.GetIRAPGOPid() : 0, leadingSeiMessages, nestedSeiMessages, duInfoSeiMessages, pcSlice, isField, duData );
    
    
          if (m_pcCfg->getScalableNestingSEIEnabled())
    
            const SPS* sps = pcSlice->getSPS();
            const PPS* pps = pcSlice->getPPS();
    
            std::vector<uint16_t> subpicIDs;
            if (sps->getSubPicInfoPresentFlag())
            {
              if(sps->getSubPicIdMappingExplicitlySignalledFlag())
              {
                if(sps->getSubPicIdMappingInSpsFlag())
                {
                  subpicIDs = sps->getSubPicIds();
                }
                else
                {
                  subpicIDs = pps->getSubPicIds();
                }
              }
              else
              {
                const int numSubPics = sps->getNumSubPics();
                subpicIDs.resize(numSubPics);
                for (int i = 0 ; i < numSubPics; i++)
                {
                  subpicIDs[i] = (uint16_t) i;
                }
              }
            }
            xCreateScalableNestingSEI(leadingSeiMessages, nestedSeiMessages, subpicIDs);
    
          xWriteLeadingSEIMessages( leadingSeiMessages, duInfoSeiMessages, accessUnit, pcSlice->getTLayer(), pcSlice->getSPS(), duData );
    
          xWriteDuSEIMessages( duInfoSeiMessages, accessUnit, pcSlice->getTLayer(), duData );
    
    
          m_AUWriterIf->outputAU( accessUnit );
    
          msg( NOTICE, "\n" );
          fflush( stdout );
        }
    
    
        DTRACE_UPDATE( g_trace_ctx, ( std::make_pair( "final", 0 ) ) );
    
        pcPic->reconstructed = true;
        m_bFirst = false;
        m_iNumPicCoded++;
    
        if (!(m_pcCfg->getUseCompositeRef() && isEncodeLtRef))
    
        {
          for( int i = pcSlice->getTLayer() ; i < pcSlice->getSPS()->getMaxTLayers() ; i ++ )
          {
            m_totalCoded[i]++;
          }
        }
    
        /* logging: insert a newline at end of picture period */
    
        if (m_pcCfg->getEfficientFieldIRAPEnabled())
        {
          iGOPid=effFieldIRAPMap.restoreGOPid(iGOPid);
        }
    
        pcPic->destroyTempBuffers();
        pcPic->cs->destroyCoeffs();
        pcPic->cs->releaseIntermediateData();
      } // iGOPid-loop
    
      delete pcBitstreamRedirect;
    
    
      CHECK( m_iNumPicCoded > 1, "Unspecified error" );
    
    void EncGOP::printOutSummary( uint32_t uiNumAllPicCoded, bool isField, const bool printMSEBasedSNR, const bool printSequenceMSE, const bool printHexPsnr, const bool printRprPSNR, const BitDepths &bitDepths )
    
    {
    #if ENABLE_QPA
      const bool    useWPSNR = m_pcEncLib->getUseWPSNR();
    #endif
    #if WCG_WPSNR
    
      const bool    useLumaWPSNR = m_pcEncLib->getLumaLevelToDeltaQPMapping().isEnabled() || (m_pcCfg->getLmcs() && m_pcCfg->getReshapeSignalType() == RESHAPE_SIGNAL_PQ);
    
    #endif
    
      if( m_pcCfg->getDecodeBitstream(0).empty() && m_pcCfg->getDecodeBitstream(1).empty() && !m_pcCfg->useFastForwardToPOC() )
      {
        CHECK( !( uiNumAllPicCoded == m_gcAnalyzeAll.getNumPic() ), "Unspecified error" );
      }
    
      //--CFG_KDY
      const int rateMultiplier=(isField?2:1);
      m_gcAnalyzeAll.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier / (double)m_pcCfg->getTemporalSubsampleRatio());
      m_gcAnalyzeI.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier / (double)m_pcCfg->getTemporalSubsampleRatio());
      m_gcAnalyzeP.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier / (double)m_pcCfg->getTemporalSubsampleRatio());
      m_gcAnalyzeB.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier / (double)m_pcCfg->getTemporalSubsampleRatio());
    #if WCG_WPSNR
      if (useLumaWPSNR)
      {
        m_gcAnalyzeWPSNR.setFrmRate(m_pcCfg->getFrameRate()*rateMultiplier / (double)m_pcCfg->getTemporalSubsampleRatio());
      }
    #endif
    
      const ChromaFormat chFmt = m_pcCfg->getChromaFormatIdc();
    
      //-- all
      msg( INFO, "\n" );
      msg( DETAILS,"\nSUMMARY --------------------------------------------------------\n" );
    
    #if JVET_O0756_CALCULATE_HDRMETRICS
      const bool calculateHdrMetrics = m_pcEncLib->getCalcluateHdrMetrics();
    #endif
    
      m_gcAnalyzeAll.printOut( 'a', chFmt, printMSEBasedSNR, printSequenceMSE, printHexPsnr, printRprPSNR, bitDepths, useWPSNR
    
    #if JVET_O0756_CALCULATE_HDRMETRICS
                              , calculateHdrMetrics
    #endif
                              );
    
      m_gcAnalyzeAll.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE, printHexPsnr, bitDepths
    #if JVET_O0756_CALCULATE_HDRMETRICS
                              , calculateHdrMetrics
    #endif
                              );
    
      msg( DETAILS, "\n\nI Slices--------------------------------------------------------\n" );
      m_gcAnalyzeI.printOut( 'i', chFmt, printMSEBasedSNR, printSequenceMSE, printHexPsnr, printRprPSNR, bitDepths );
    
      msg( DETAILS, "\n\nP Slices--------------------------------------------------------\n" );
      m_gcAnalyzeP.printOut( 'p', chFmt, printMSEBasedSNR, printSequenceMSE, printHexPsnr, printRprPSNR, bitDepths );
    
      msg( DETAILS, "\n\nB Slices--------------------------------------------------------\n" );
      m_gcAnalyzeB.printOut( 'b', chFmt, printMSEBasedSNR, printSequenceMSE, printHexPsnr, printRprPSNR, bitDepths );
    
    #if WCG_WPSNR
      if (useLumaWPSNR)
      {
        msg(DETAILS, "\nWPSNR SUMMARY --------------------------------------------------------\n");
    
        m_gcAnalyzeWPSNR.printOut( 'w', chFmt, printMSEBasedSNR, printSequenceMSE, printHexPsnr, printRprPSNR, bitDepths, useLumaWPSNR );
    
        m_gcAnalyzeAll.printSummary(chFmt, printSequenceMSE, printHexPsnr, bitDepths, m_pcCfg->getSummaryOutFilename());
    
        m_gcAnalyzeI.printSummary(chFmt, printSequenceMSE, printHexPsnr, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"I.txt");
        m_gcAnalyzeP.printSummary(chFmt, printSequenceMSE, printHexPsnr, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"P.txt");
        m_gcAnalyzeB.printSummary(chFmt, printSequenceMSE, printHexPsnr, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"B.txt");
    
      }
    
    #if WCG_WPSNR
      if (!m_pcCfg->getSummaryOutFilename().empty() && useLumaWPSNR)
      {
    
        m_gcAnalyzeWPSNR.printSummary(chFmt, printSequenceMSE, printHexPsnr, bitDepths, m_pcCfg->getSummaryOutFilename());
    
      }
    #endif
      if(isField)
      {
        //-- interlaced summary
        m_gcAnalyzeAll_in.setFrmRate( m_pcCfg->getFrameRate() / (double)m_pcCfg->getTemporalSubsampleRatio());
        m_gcAnalyzeAll_in.setBits(m_gcAnalyzeAll.getBits());
        // prior to the above statement, the interlace analyser does not contain the correct total number of bits.
    
    
    Haiwei Sun's avatar
    Haiwei Sun committed
        msg( INFO,"\n\nSUMMARY INTERLACED ---------------------------------------------\n" );
    
        m_gcAnalyzeAll_in.printOut( 'a', chFmt, printMSEBasedSNR, printSequenceMSE, printHexPsnr, printRprPSNR, bitDepths, useWPSNR );
    
        m_gcAnalyzeAll_in.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE, printHexPsnr, bitDepths);
    
    #endif
        if (!m_pcCfg->getSummaryOutFilename().empty())
        {
    
          m_gcAnalyzeAll_in.printSummary(chFmt, printSequenceMSE, printHexPsnr, bitDepths, m_pcCfg->getSummaryOutFilename());
    
            m_gcAnalyzeWPSNR.printSummary(chFmt, printSequenceMSE, printHexPsnr, bitDepths, m_pcCfg->getSummaryOutFilename());
    
          }
    #endif
        }
      }
    
      msg( DETAILS,"\nRVM: %.3lf\n", xCalculateRVM() );
    }
    
    #if W0038_DB_OPT
    uint64_t EncGOP::preLoopFilterPicAndCalcDist( Picture* pcPic )
    {
      CodingStructure& cs = *pcPic->cs;
      m_pcLoopFilter->loopFilterPic( cs );
    
      const CPelUnitBuf picOrg = pcPic->getRecoBuf();
      const CPelUnitBuf picRec = cs.getRecoBuf();
    
      uint64_t uiDist = 0;
      for( uint32_t comp = 0; comp < (uint32_t)picRec.bufs.size(); comp++)
      {
        const ComponentID compID = ComponentID(comp);
        const uint32_t rshift = 2 * DISTORTION_PRECISION_ADJUSTMENT(cs.sps->getBitDepth(toChannelType(compID)));
    #if ENABLE_QPA
        CHECK( rshift >= 8, "shifts greater than 7 are not supported." );
    #endif
        uiDist += xFindDistortionPlane( picOrg.get(compID), picRec.get(compID), rshift );
      }
      return uiDist;
    }
    #endif
    
    // ====================================================================================================================
    // Protected member functions
    // ====================================================================================================================
    
    void EncGOP::xInitGOP( int iPOCLast, int iNumPicRcvd, bool isField
      , bool isEncodeLtRef
    )
    
    {
      CHECK(!( iNumPicRcvd > 0 ), "Unspecified error");
      //  Exception for the first frames
    
      if ((isField && (iPOCLast == 0 || iPOCLast == 1)) || (!isField && (iPOCLast == 0)) || isEncodeLtRef)
    
      {
        m_iGopSize    = 1;
      }
      else
      {
        m_iGopSize    = m_pcCfg->getGOPSize();
      }
      CHECK(!(m_iGopSize > 0), "Unspecified error");
    
      return;
    }
    
    
    void EncGOP::xGetBuffer( PicList&                  rcListPic,
                             std::list<PelUnitBuf*>&   rcListPicYuvRecOut,
                             int                       iNumPicRcvd,
                             int                       iTimeOffset,
                             Picture*&                 rpcPic,
                             int                       pocCurr,
                             bool                      isField )
    {
      int i;
      //  Rec. output
      std::list<PelUnitBuf*>::iterator     iterPicYuvRec = rcListPicYuvRecOut.end();
    
      if (isField && pocCurr > 1 && m_iGopSize!=1)
      {
        iTimeOffset--;
      }
    
    
      int multipleFactor = m_pcCfg->getUseCompositeRef() ? 2 : 1;
      for (i = 0; i < (iNumPicRcvd * multipleFactor - iTimeOffset + 1); i += multipleFactor)
    
      {
        iterPicYuvRec--;
      }
    
      //  Current pic.
      PicList::iterator        iterPic       = rcListPic.begin();
      while (iterPic != rcListPic.end())
      {
        rpcPic = *(iterPic);
    
    Vadim Seregin's avatar
    Vadim Seregin committed
        if( rpcPic->getPOC() == pocCurr && rpcPic->layerId == m_pcEncLib->getLayerId() )
    
        {
          break;
        }
        iterPic++;
      }
    
      CHECK(!(rpcPic != NULL), "Unspecified error");
      CHECK(!(rpcPic->getPOC() == pocCurr), "Unspecified error");
    
      (**iterPicYuvRec) = rpcPic->getRecoBuf();
      return;
    }
    
    #if ENABLE_QPA
    
    #ifndef BETA
    
      #define BETA 0.5 // value between 0.0 and 1; use 0.0 to obtain traditional PSNR
    
    static inline double calcWeightedSquaredError(const CPelBuf& org,        const CPelBuf& rec,
                                                  double &sumAct,            const uint32_t bitDepth,
                                                  const uint32_t imageWidth, const uint32_t imageHeight,
                                                  const uint32_t offsetX,    const uint32_t offsetY,
                                                  int blockWidth,            int blockHeight)
    
    {
      const int    O = org.stride;
      const int    R = rec.stride;
      const Pel   *o = org.bufAt(offsetX, offsetY);
      const Pel   *r = rec.bufAt(offsetX, offsetY);
      const int yAct = offsetY > 0 ? 0 : 1;
      const int xAct = offsetX > 0 ? 0 : 1;
    
      if (offsetY + (uint32_t)blockHeight > imageHeight) blockHeight = imageHeight - offsetY;
      if (offsetX + (uint32_t)blockWidth  > imageWidth ) blockWidth  = imageWidth  - offsetX;
    
      const int hAct = offsetY + (uint32_t)blockHeight < imageHeight ? blockHeight : blockHeight - 1;
      const int wAct = offsetX + (uint32_t)blockWidth  < imageWidth  ? blockWidth  : blockWidth  - 1;
    
      uint64_t ssErr = 0; // sum of squared diffs
      uint64_t saAct = 0; // sum of abs. activity
    
      double msAct;
      int x, y;
    
      // calculate image differences and activity
      for (y = 0; y < blockHeight; y++)  // error
      {
        for (x = 0; x < blockWidth; x++)
        {
    
          const     int64_t iDiff = (int64_t)o[y*O + x] - (int64_t)r[y*R + x];
    
          ssErr += uint64_t(iDiff * iDiff);
        }
      }
      if (wAct <= xAct || hAct <= yAct) return (double)ssErr;
    
      for (y = yAct; y < hAct; y++)   // activity
      {
        for (x = xAct; x < wAct; x++)
        {
    
          const int f = 12 * (int)o[y*O + x] - 2 * ((int)o[y*O + x-1] + (int)o[y*O + x+1] + (int)o[(y-1)*O + x] + (int)o[(y+1)*O + x])
                           - (int)o[(y-1)*O + x-1] - (int)o[(y-1)*O + x+1] - (int)o[(y+1)*O + x-1] - (int)o[(y+1)*O + x+1];
          saAct += abs(f);
    
        }
      }
    
      // calculate weight (mean squared activity)
      msAct = (double)saAct / (double(wAct - xAct) * double(hAct - yAct));
    
    
      // lower limit, accounts for high-pass gain
      if (msAct < double(1 << (bitDepth - 4))) msAct = double(1 << (bitDepth - 4));
    
    
      msAct *= msAct; // because ssErr is squared
    
      sumAct += msAct; // includes high-pass gain
    
      // calculate activity weighted error square
      return (double)ssErr * pow(msAct, -1.0 * BETA);
    }
    #endif // ENABLE_QPA
    
    uint64_t EncGOP::xFindDistortionPlane(const CPelBuf& pic0, const CPelBuf& pic1, const uint32_t rshift
    #if ENABLE_QPA
    
                                        , const uint32_t chromaShiftHor /*= 0*/, const uint32_t chromaShiftVer /*= 0*/
    
    {
      uint64_t uiTotalDiff;
      const  Pel*  pSrc0 = pic0.bufAt(0, 0);
      const  Pel*  pSrc1 = pic1.bufAt(0, 0);
    
      CHECK(pic0.width  != pic1.width , "Unspecified error");
      CHECK(pic0.height != pic1.height, "Unspecified error");
    
      if( rshift > 0 )
      {
    #if ENABLE_QPA
        const   uint32_t  BD = rshift;      // image bit-depth
        if (BD >= 8)
        {
          const uint32_t   W = pic0.width;  // image width
          const uint32_t   H = pic0.height; // image height
    
          const double     R = double(W * H) / (1920.0 * 1080.0);
    
          const uint32_t   B = Clip3<uint32_t>(0, 128 >> chromaShiftVer, 4 * uint32_t(16.0 * sqrt(R) + 0.5)); // WPSNR block size in integer multiple of 4 (for SIMD, = 64 at full-HD)
    
    
          uint32_t x, y;
    
          if (B < 4) // image is too small to use WPSNR, resort to traditional PSNR
          {
            uiTotalDiff = 0;
            for (y = 0; y < H; y++)
            {
              for (x = 0; x < W; x++)
              {
    
                const           int64_t iDiff = (int64_t)pSrc0[x] - (int64_t)pSrc1[x];
    
                uiTotalDiff += uint64_t(iDiff * iDiff);
              }
              pSrc0 += pic0.stride;
              pSrc1 += pic1.stride;
            }
            return uiTotalDiff;
          }
    
          double wmse = 0.0, sumAct = 0.0; // compute activity normalized SNR value
    
              wmse += calcWeightedSquaredError(pic1,   pic0,
                                               sumAct, BD,
                                               W,      H,
                                               x,      y,
                                               B,      B);
    
          sumAct = 16.0 * sqrt ((3840.0 * 2160.0) / double((W << chromaShiftHor) * (H << chromaShiftVer))) * double(1 << BD);
    
          return (wmse <= 0.0) ? 0 : uint64_t(wmse * pow(sumAct, BETA) + 0.5);
        }
    #endif // ENABLE_QPA
        uiTotalDiff = 0;
        for (int y = 0; y < pic0.height; y++)
        {
          for (int x = 0; x < pic0.width; x++)
          {
            Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
            uiTotalDiff += uint64_t((iTemp * iTemp) >> rshift);
          }
          pSrc0 += pic0.stride;
          pSrc1 += pic1.stride;
        }
      }
      else
      {
        uiTotalDiff = 0;
        for (int y = 0; y < pic0.height; y++)
        {
          for (int x = 0; x < pic0.width; x++)
          {
            Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
            uiTotalDiff += uint64_t(iTemp * iTemp);
          }
          pSrc0 += pic0.stride;
          pSrc1 += pic1.stride;
        }
      }
    
      return uiTotalDiff;
    }
    #if WCG_WPSNR
    
    double EncGOP::xFindDistortionPlaneWPSNR(const CPelBuf& pic0, const CPelBuf& pic1, const uint32_t rshift, const CPelBuf& picLuma0,
    
      const bool    useLumaWPSNR = m_pcEncLib->getLumaLevelToDeltaQPMapping().isEnabled() || (m_pcCfg->getLmcs() && m_pcCfg->getReshapeSignalType() == RESHAPE_SIGNAL_PQ);
    
      if (!useLumaWPSNR)
      {
        return 0;
      }
    
      double uiTotalDiffWPSNR;
      const  Pel*  pSrc0 = pic0.bufAt(0, 0);
      const  Pel*  pSrc1 = pic1.bufAt(0, 0);
      const  Pel*  pSrcLuma = picLuma0.bufAt(0, 0);