Skip to content
Snippets Groups Projects
EncGOP.cpp 126 KiB
Newer Older
  • Learn to ignore specific revisions
  • 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
            }
            else
            {
      #endif
              applyDeblockingFilterMetric(pcPic, uiNumSliceSegments);
      #if W0038_DB_OPT
            }
      #endif
          }
    
          m_pcLoopFilter->loopFilterPic( cs );
    
          DTRACE_UPDATE( g_trace_ctx, ( std::make_pair( "final", 1 ) ) );
    
          if( pcSlice->getSPS()->getUseSAO() )
          {
            bool sliceEnabled[MAX_NUM_COMPONENT];
            m_pcSAO->initCABACEstimator( m_pcEncLib->getCABACEncoder(), m_pcEncLib->getCtxCache(), pcSlice );
    #if K0238_SAO_GREEDY_MERGE_ENCODING
            m_pcSAO->SAOProcess(cs, sliceEnabled, pcSlice->getLambdas(), m_pcCfg->getTestSAODisableAtPictureLevel(), m_pcCfg->getSaoEncodingRate(), m_pcCfg->getSaoEncodingRateChroma(), m_pcCfg->getSaoCtuBoundary(), m_pcCfg->getSaoGreedyMergeEnc());
    #else
            m_pcSAO->SAOProcess(cs, sliceEnabled, pcSlice->getLambdas(), m_pcCfg->getTestSAODisableAtPictureLevel(), m_pcCfg->getSaoEncodingRate(), m_pcCfg->getSaoEncodingRateChroma(), m_pcCfg->getSaoCtuBoundary());
    #endif
            //assign SAO slice header
            for(int s=0; s< uiNumSliceSegments; s++)
            {
              pcPic->slices[s]->setSaoEnabledFlag(CHANNEL_TYPE_LUMA, sliceEnabled[COMPONENT_Y]);
              CHECK(!(sliceEnabled[COMPONENT_Cb] == sliceEnabled[COMPONENT_Cr]), "Unspecified error");
              pcPic->slices[s]->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, sliceEnabled[COMPONENT_Cb]);
            }
          }
    
    #if JVET_K0371_ALF
          if( pcSlice->getSPS()->getUseALF() )
          {
            AlfSliceParam alfSliceParam;
            m_pcALF->initCABACEstimator( m_pcEncLib->getCABACEncoder(), m_pcEncLib->getCtxCache(), pcSlice );
            m_pcALF->ALFProcess( cs, pcSlice->getLambdas(), alfSliceParam );
            //assign ALF slice header
            for( int s = 0; s< uiNumSliceSegments; s++ )
            {
              pcPic->slices[s]->setAlfSliceParam( alfSliceParam );
            }
          }
    #endif
    
        }
        else // skip enc picture
        {
          pcSlice->setSliceQpBase( pcSlice->getSliceQp() );
    
          if( pcSlice->getSPS()->getUseSAO() )
          {
            m_pcSAO->disabledRate( *pcPic->cs, pcPic->getSAO(1), m_pcCfg->getSaoEncodingRate(), m_pcCfg->getSaoEncodingRateChroma());
          }
        }
    
        if( m_pcCfg->getUseAMaxBT() )
        {
          for( const CodingUnit *cu : pcPic->cs->cus )
          {
            if( !pcSlice->isIntra() )
            {
              m_uiBlkSize[pcSlice->getDepth()] += cu->Y().area();
              m_uiNumBlk [pcSlice->getDepth()]++;
            }
          }
        }
    
        if( encPic || decPic )
        {
          pcSlice = pcPic->slices[0];
    
          /////////////////////////////////////////////////////////////////////////////////////////////////// File writing
    
          // write various parameter sets
          actualTotalBits += xWriteParameterSets( accessUnit, pcSlice, m_bSeqFirst );
    
          if ( m_bSeqFirst )
          {
            // create prefix SEI messages at the beginning of the sequence
            CHECK(!(leadingSeiMessages.empty()), "Unspecified error");
            xCreateIRAPLeadingSEIMessages(leadingSeiMessages, pcSlice->getSPS(), pcSlice->getPPS());
    
            m_bSeqFirst = false;
          }
          if (m_pcCfg->getAccessUnitDelimiter())
          {
            xWriteAccessUnitDelimiter(accessUnit, pcSlice);
          }
    
          // 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 HEVC_DEPENDENT_SLICES
          for( uint32_t sliceSegmentStartCtuTsAddr = 0, sliceSegmentIdxCount=0; sliceSegmentStartCtuTsAddr < numberOfCtusInFrame; sliceSegmentIdxCount++, sliceSegmentStartCtuTsAddr=pcSlice->getSliceSegmentCurEndCtuTsAddr() )
    #else
          for(uint32_t sliceSegmentStartCtuTsAddr = 0, sliceSegmentIdxCount = 0; sliceSegmentStartCtuTsAddr < numberOfCtusInFrame; sliceSegmentIdxCount++, sliceSegmentStartCtuTsAddr = pcSlice->getSliceCurEndCtuTsAddr())
    #endif
          {
            pcSlice = pcPic->slices[sliceSegmentIdxCount];
            if(sliceSegmentIdxCount > 0 && pcSlice->getSliceType()!= I_SLICE)
            {
              pcSlice->checkColRefIdx(sliceSegmentIdxCount, pcPic);
            }
            m_pcSliceEncoder->setSliceSegmentIdx(sliceSegmentIdxCount);
    
            pcSlice->setRPS   (pcPic->slices[0]->getRPS());
            pcSlice->setRPSidx(pcPic->slices[0]->getRPSidx());
    
            for ( uint32_t ui = 0 ; ui < numSubstreams; ui++ )
            {
              substreamsOut[ui].clear();
            }
    
            /* start slice NALunit */
            OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->getTLayer() );
            m_HLSWriter->setBitstream( &nalu.m_Bitstream );
    
            pcSlice->setNoRaslOutputFlag(false);
            if (pcSlice->isIRAP())
            {
              if (pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_IDR_N_LP)
              {
                pcSlice->setNoRaslOutputFlag(true);
              }
              //the inference for NoOutputPriorPicsFlag
              // KJS: This cannot happen at the encoder
              if (!m_bFirst && pcSlice->isIRAP() && pcSlice->getNoRaslOutputFlag())
              {
                if (pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA)
                {
                  pcSlice->setNoOutputPriorPicsFlag(true);
                }
              }
            }
    
            tmpBitsBeforeWriting = m_HLSWriter->getNumberOfWrittenBits();
            m_HLSWriter->codeSliceHeader( pcSlice );
            actualHeadBits += ( m_HLSWriter->getNumberOfWrittenBits() - tmpBitsBeforeWriting );
    
            pcSlice->setFinalized(true);
    
            pcSlice->clearSubstreamSizes(  );
            {
              uint32_t numBinsCoded = 0;
              m_pcSliceEncoder->encodeSlice(pcPic, &(substreamsOut[0]), numBinsCoded);
              binCountsInNalUnits+=numBinsCoded;
            }
            {
              // 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 );
    #if HEVC_TILES_WPP
              m_HLSWriter->codeTilesWPPEntryPoint( pcSlice );
    #endif
    
              // Append substreams...
              OutputBitstream *pcOut = pcBitstreamRedirect;
    #if HEVC_TILES_WPP
    #if HEVC_DEPENDENT_SLICES
    
              const int numZeroSubstreamsAtStartOfSlice = pcPic->tileMap->getSubstreamForCtuAddr(pcSlice->getSliceSegmentCurStartCtuTsAddr(), false, pcSlice);
    #else
              const int numZeroSubstreamsAtStartOfSlice  = pcPic->tileMap->getSubstreamForCtuAddr(pcSlice->getSliceCurStartCtuTsAddr(), false, pcSlice);
    #endif
              const int numSubstreamsToCode  = pcSlice->getNumberOfSubstreamSizes()+1;
    #else
              const int numZeroSubstreamsAtStartOfSlice  = 0;
              const int numSubstreamsToCode  = pcSlice->getNumberOfSubstreamSizes()+1;
    #endif
              for ( uint32_t ui = 0 ; ui < numSubstreamsToCode; ui++ )
              {
                pcOut->addSubstream(&(substreamsOut[ui+numZeroSubstreamsAtStartOfSlice]));
              }
            }
    
            // 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());
            bNALUAlignedWrittenToList = true;
    
            if (!bNALUAlignedWrittenToList)
            {
              nalu.m_Bitstream.writeAlignZero();
              accessUnit.push_back(new NALUnitEBSP(nalu));
            }
    
            if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) &&
                ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) &&
                ( ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNalHrdParametersPresentFlag() )
               || ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getVclHrdParametersPresentFlag() ) ) &&
                ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getSubPicCpbParamsPresentFlag() ) )
            {
                uint32_t numNalus = 0;
              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;
            }
          } // end iteration over slices
    
    
          // cabac_zero_words processing
          cabac_zero_word_padding(pcSlice, pcPic, binCountsInNalUnits, numBytesInVclNalUnits, accessUnit.back()->m_nalUnitData, m_pcCfg->getCabacZeroWordPaddingEnabled());
    
          //-- 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;
          xCalculateAddPSNRs( isField, isTff, iGOPid, pcPic, accessUnit, rcListPic, encTime, snr_conversion, printFrameMSE, &PSNR_Y );
    
          // Only produce the Green Metadata SEI message with the last picture.
          if( m_pcCfg->getSEIGreenMetadataInfoSEIEnable() && pcSlice->getPOC() == ( m_pcCfg->getFramesToBeEncoded() - 1 )  )
          {
            SEIGreenMetadataInfo *seiGreenMetadataInfo = new SEIGreenMetadataInfo;
            m_seiEncoder.initSEIGreenMetadataInfo(seiGreenMetadataInfo, (uint32_t)(PSNR_Y * 100 + 0.5));
            trailingSeiMessages.push_back(seiGreenMetadataInfo);
          }
    
          xWriteTrailingSEIMessages(trailingSeiMessages, accessUnit, pcSlice->getTLayer(), pcSlice->getSPS());
    
          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->getSliceType());
            m_pcRateCtrl->getRCPic()->addToPictureLsit( m_pcRateCtrl->getPicList() );
    
            m_pcRateCtrl->getRCSeq()->updateAfterPic( actualTotalBits );
            if ( pcSlice->getSliceType() != I_SLICE )
            {
              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
          }
    
          xCreatePictureTimingSEI( m_pcCfg->getEfficientFieldIRAPEnabled() ? effFieldIRAPMap.GetIRAPGOPid() : 0, leadingSeiMessages, nestedSeiMessages, duInfoSeiMessages, pcSlice, isField, duData );
          if( m_pcCfg->getScalableNestingSEIEnabled() )
          {
            xCreateScalableNestingSEI( leadingSeiMessages, nestedSeiMessages );
          }
          xWriteLeadingSEIMessages( leadingSeiMessages, duInfoSeiMessages, accessUnit, pcSlice->getTLayer(), pcSlice->getSPS(), duData );
          xWriteDuSEIMessages( duInfoSeiMessages, accessUnit, pcSlice->getTLayer(), pcSlice->getSPS(), 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++;
        m_totalCoded ++;
        /* 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 == iNumPicRcvd) ), "Unspecified error");
    
    }
    
    void EncGOP::printOutSummary(uint32_t uiNumAllPicCoded, bool isField, const bool printMSEBasedSNR, const bool printSequenceMSE, const BitDepths &bitDepths)
    {
    #if ENABLE_QPA
      const bool    useWPSNR = m_pcEncLib->getUseWPSNR();
    #endif
    #if WCG_WPSNR
      const bool    useLumaWPSNR = m_pcEncLib->getLumaLevelToDeltaQPMapping().isEnabled();
    #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 ENABLE_QPA
      m_gcAnalyzeAll.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths, useWPSNR);
    #else
      m_gcAnalyzeAll.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
    #endif
      msg( DETAILS,"\n\nI Slices--------------------------------------------------------\n" );
      m_gcAnalyzeI.printOut('i', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
    
      msg( DETAILS,"\n\nP Slices--------------------------------------------------------\n" );
      m_gcAnalyzeP.printOut('p', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
    
      msg( DETAILS,"\n\nB Slices--------------------------------------------------------\n" );
      m_gcAnalyzeB.printOut('b', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
      
    #if WCG_WPSNR
      if (useLumaWPSNR)
      {
        msg(DETAILS, "\nWPSNR SUMMARY --------------------------------------------------------\n");
        m_gcAnalyzeWPSNR.printOut('w', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths, useLumaWPSNR);
      }
    #endif
      if (!m_pcCfg->getSummaryOutFilename().empty())
      {
        m_gcAnalyzeAll.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryOutFilename());
      }
    
      if (!m_pcCfg->getSummaryPicFilenameBase().empty())
      {
        m_gcAnalyzeI.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"I.txt");
        m_gcAnalyzeP.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"P.txt");
        m_gcAnalyzeB.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"B.txt");
      }
    
    #if WCG_WPSNR
      if (!m_pcCfg->getSummaryOutFilename().empty() && useLumaWPSNR)
      {
        m_gcAnalyzeWPSNR.printSummary(chFmt, printSequenceMSE, 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.
    
        msg( DETAILS,"\n\nSUMMARY INTERLACED ---------------------------------------------\n" );
    #if ENABLE_QPA
        m_gcAnalyzeAll_in.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths, useWPSNR);
    #else
        m_gcAnalyzeAll_in.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
    #endif
        if (!m_pcCfg->getSummaryOutFilename().empty())
        {
          m_gcAnalyzeAll_in.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryOutFilename());
    #if WCG_WPSNR
          if (useLumaWPSNR)
          {
            m_gcAnalyzeWPSNR.printSummary(chFmt, printSequenceMSE, 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);
    #if DISTORTION_LAMBDA_BUGFIX
        const uint32_t rshift = 2 * DISTORTION_PRECISION_ADJUSTMENT(cs.sps->getBitDepth(toChannelType(compID)));
    #else
        const uint32_t rshift = 2 * DISTORTION_PRECISION_ADJUSTMENT(cs.sps->getBitDepth(toChannelType(compID)) - 8);
    #endif
    #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 )
    {
      CHECK(!( iNumPicRcvd > 0 ), "Unspecified error");
      //  Exception for the first frames
      if ( ( isField && (iPOCLast == 0 || iPOCLast == 1) ) || (!isField  && (iPOCLast == 0))  )
      {
        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--;
      }
    
      for ( i = 0; i < (iNumPicRcvd - iTimeOffset + 1); i++ )
      {
        iterPicYuvRec--;
      }
    
      //  Current pic.
      PicList::iterator        iterPic       = rcListPic.begin();
      while (iterPic != rcListPic.end())
      {
        rpcPic = *(iterPic);
        if (rpcPic->getPOC() == pocCurr)
        {
          break;
        }
        iterPic++;
      }
    
      CHECK(!(rpcPic != NULL), "Unspecified error");
      CHECK(!(rpcPic->getPOC() == pocCurr), "Unspecified error");
    
      (**iterPicYuvRec) = rpcPic->getRecoBuf();
      return;
    }
    
    #if ENABLE_QPA
    
    #ifndef BETA
     #define BETA (2.0 / 3.0)  // value between 0 and 1; use 0.0 for traditional PSNR
    #endif
    #define GLOBAL_AVERAGING 1 // "global" averaging of a_k across a set instead of one picture
    #if FRAME_WEIGHTING
    static const uint32_t DQP[16] = { 4, 12, 11, 12,  9, 12, 11, 12,  6, 12, 11, 12,  9, 12, 11, 12 };
    #endif
    
    static inline double calcWeightedSquaredError(const CPelBuf& org,    const CPelBuf& rec,     double &sumAct,
                                                  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++)
        {
          register  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++)
        {
          saAct += uint64_t(abs(4 * (int64_t)o[y*O + x] - (int64_t)o[y*O + x-1] - (int64_t)o[y*O + x+1] - (int64_t)o[(y-1)*O + x] - (int64_t)o[(y+1)*O + x]));
        }
      }
    
      // calculate weight (mean squared activity)
      msAct = (double)saAct / (double(wAct - xAct) * double(hAct - yAct));
      if (msAct < 8.0) msAct = 8.0;
      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 chromaShift /*= 0*/
    #endif
                                       )
    {
      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 >> chromaShift, 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++)
              {
                register 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
    #if !GLOBAL_AVERAGING
          double numAct = 0.0;
    #endif
          for (y = 0; y < H; y += B)
          {
            for (x = 0; x < W; x += B)
            {
              wmse += calcWeightedSquaredError(pic1, pic0, sumAct, W, H, x, y, B, B);
    #if !GLOBAL_AVERAGING
              numAct += 1.0;
    #endif
            }
          }
    
          // integer weighted distortion
    #if GLOBAL_AVERAGING
          sumAct = 1.5 * double(1 << BD);
          if ((W << chromaShift) > 2048 && (H << chromaShift) > 1280)   // UHD luma
          {
            sumAct /= 1.5;
          }
          return (wmse <= 0.0) ? 0 : uint64_t(wmse * pow(sumAct, BETA) + 0.5);
    #else
          return (wmse <= 0.0 || numAct <= 0.0) ? 0 : uint64_t(wmse * pow(sumAct / numAct, BETA) + 0.5);
    #endif
        }
    #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, 
      ComponentID compID, const ChromaFormat chfmt    )
    {
      const bool    useLumaWPSNR = m_pcEncLib->getLumaLevelToDeltaQPMapping().isEnabled();
      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);
      CHECK(pic0.width  != pic1.width , "Unspecified error");
      CHECK(pic0.height != pic1.height, "Unspecified error");
    
      if( rshift > 0 )
      {
        uiTotalDiffWPSNR = 0;
        for (int y = 0; y < pic0.height; y++)
        {
          for (int x = 0; x < pic0.width; x++)
          {
            Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
            double dW = m_pcEncLib->getRdCost()->getWPSNRLumaLevelWeight(pSrcLuma[(x << getComponentScaleX(compID, chfmt))]);
            uiTotalDiffWPSNR += ((dW * (double)iTemp * (double)iTemp)) * (double)(1 >> rshift);
          }
          pSrc0 += pic0.stride;
          pSrc1 += pic1.stride;
          pSrcLuma += picLuma0.stride << getComponentScaleY(compID, chfmt);
        }
      }
      else
      {
        uiTotalDiffWPSNR = 0;
        for (int y = 0; y < pic0.height; y++)
        {
          for (int x = 0; x < pic0.width; x++)
          {
            Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
            double dW = m_pcEncLib->getRdCost()->getWPSNRLumaLevelWeight(pSrcLuma[x << getComponentScaleX(compID, chfmt)]);
            uiTotalDiffWPSNR += dW * (double)iTemp * (double)iTemp;
          }
          pSrc0 += pic0.stride;
          pSrc1 += pic1.stride;
          pSrcLuma += picLuma0.stride << getComponentScaleY(compID, chfmt);
        }
      }
    
      return uiTotalDiffWPSNR;
    }
    #endif
    
    void EncGOP::xCalculateAddPSNRs( const bool isField, const bool isFieldTopFieldFirst, const int iGOPid, Picture* pcPic, const AccessUnit&accessUnit, PicList &rcListPic, const int64_t dEncTime, const InputColourSpaceConversion snr_conversion, const bool printFrameMSE, double* PSNR_Y )
    {
      xCalculateAddPSNR( pcPic, pcPic->getRecoBuf(), accessUnit, (double) dEncTime, snr_conversion, printFrameMSE, PSNR_Y );
    
      //In case of field coding, compute the interlaced PSNR for both fields
      if(isField)
      {
        bool bothFieldsAreEncoded = false;
        int correspondingFieldPOC = pcPic->getPOC();
        int currentPicGOPPoc = m_pcCfg->getGOPEntry(iGOPid).m_POC;
        if(pcPic->getPOC() == 0)
        {
          // particular case for POC 0 and 1.
          // If they are not encoded first and separately from other pictures, we need to change this
          // POC 0 is always encoded first then POC 1 is encoded
          bothFieldsAreEncoded = false;
        }
        else if(pcPic->getPOC() == 1)
        {
          // if we are at POC 1, POC 0 has been encoded for sure
          correspondingFieldPOC = 0;
          bothFieldsAreEncoded = true;
        }
        else
        {
          if(pcPic->getPOC()%2 == 1)
          {
            correspondingFieldPOC -= 1; // all odd POC are associated with the preceding even POC (e.g poc 1 is associated to poc 0)
            currentPicGOPPoc      -= 1;
          }
          else
          {
            correspondingFieldPOC += 1; // all even POC are associated with the following odd POC (e.g poc 0 is associated to poc 1)
            currentPicGOPPoc      += 1;
          }
          for(int i = 0; i < m_iGopSize; i ++)
          {
            if(m_pcCfg->getGOPEntry(i).m_POC == currentPicGOPPoc)
            {
              bothFieldsAreEncoded = m_pcCfg->getGOPEntry(i).m_isEncoded;
              break;
            }
          }
        }
    
        if(bothFieldsAreEncoded)
        {
          //get complementary top field
          PicList::iterator   iterPic = rcListPic.begin();
          while ((*iterPic)->getPOC() != correspondingFieldPOC)
          {
            iterPic ++;
          }
          Picture* correspondingFieldPic = *(iterPic);
    
          if( (pcPic->topField && isFieldTopFieldFirst) || (!pcPic->topField && !isFieldTopFieldFirst))
          {
            xCalculateInterlacedAddPSNR(pcPic, correspondingFieldPic, pcPic->getRecoBuf(), correspondingFieldPic->getRecoBuf(), snr_conversion, printFrameMSE, PSNR_Y );
          }
          else
          {
            xCalculateInterlacedAddPSNR(correspondingFieldPic, pcPic, correspondingFieldPic->getRecoBuf(), pcPic->getRecoBuf(), snr_conversion, printFrameMSE, PSNR_Y );
          }
        }
      }
    }
    
    void EncGOP::xCalculateAddPSNR( Picture* pcPic, PelUnitBuf cPicD, const AccessUnit& accessUnit, double dEncTime, const InputColourSpaceConversion conversion, const bool printFrameMSE, double* PSNR_Y )
    {
      const SPS&         sps = *pcPic->cs->sps;
      const CPelUnitBuf& pic = cPicD;
      CHECK(!(conversion == IPCOLOURSPACE_UNCHANGED), "Unspecified error");
    //  const CPelUnitBuf& org = (conversion != IPCOLOURSPACE_UNCHANGED) ? pcPic->getPicYuvTrueOrg()->getBuf() : pcPic->getPicYuvOrg()->getBuf();
      const CPelUnitBuf& org = pcPic->getOrigBuf();
    #if ENABLE_QPA
      const bool    useWPSNR = m_pcEncLib->getUseWPSNR();
    #endif
      double  dPSNR[MAX_NUM_COMPONENT];
    #if WCG_WPSNR
      const bool    useLumaWPSNR = m_pcEncLib->getLumaLevelToDeltaQPMapping().isEnabled();
      double  dPSNRWeighted[MAX_NUM_COMPONENT];
      double  MSEyuvframeWeighted[MAX_NUM_COMPONENT];
    #endif
      for(int i=0; i<MAX_NUM_COMPONENT; i++)
      {
        dPSNR[i]=0.0;
    #if WCG_WPSNR
        dPSNRWeighted[i]=0.0;
        MSEyuvframeWeighted[i] = 0.0;
    #endif
      }
    
      PelStorage interm;
    
      if (conversion != IPCOLOURSPACE_UNCHANGED)
      {
        interm.create(pic.chromaFormat, Area(Position(), pic.Y()));
        VideoIOYuv::ColourSpaceConvert(pic, interm, conversion, false);
      }
    
      const CPelUnitBuf& picC = (conversion == IPCOLOURSPACE_UNCHANGED) ? pic : interm;
    
      //===== calculate PSNR =====
      double MSEyuvframe[MAX_NUM_COMPONENT] = {0, 0, 0};
      const ChromaFormat formatD = pic.chromaFormat;
      const ChromaFormat format  = sps.getChromaFormatIdc();
    
      const bool bPicIsField     = pcPic->fieldPic;
      const Slice*  pcSlice      = pcPic->slices[0];
    #if ENABLE_QPA && FRAME_WEIGHTING
      const uint32_t    currDQP      = (pcSlice->getPOC() % m_pcEncLib->getIntraPeriod()) == 0 ? 0 : DQP[pcSlice->getPOC() % m_pcEncLib->getGOPSize()];
      const double  frameWeight  = pow(2.0, (double)currDQP / -3.0);
    
      if (useWPSNR) m_gcAnalyzeAll.addWeight(frameWeight);
    #endif
      for (int comp = 0; comp < ::getNumberValidComponents(formatD); comp++)
      {
        const ComponentID compID = ComponentID(comp);
        const CPelBuf&    p = picC.get(compID);
        const CPelBuf&    o = org.get(compID);
    
        CHECK(!( p.width  == o.width), "Unspecified error");
        CHECK(!( p.height == o.height), "Unspecified error");
    
        const uint32_t   width  = p.width  - (m_pcEncLib->getPad(0) >> ::getComponentScaleX(compID, format));
        const uint32_t   height = p.height - (m_pcEncLib->getPad(1) >> (!!bPicIsField+::getComponentScaleY(compID,format)));
    
        // create new buffers with correct dimensions
        const CPelBuf recPB(p.bufAt(0, 0), p.stride, width, height);
        const CPelBuf orgPB(o.bufAt(0, 0), o.stride, width, height);
        const uint32_t    bitDepth = sps.getBitDepth(toChannelType(compID));
    #if ENABLE_QPA
        const uint64_t uiSSDtemp = xFindDistortionPlane(recPB, orgPB, useWPSNR ? bitDepth : 0, ::getComponentScaleX(compID, format));
        const uint32_t maxval = /*useWPSNR ? (1 << bitDepth) - 1 :*/ 255 << (bitDepth - 8); // fix with WPSNR: 1023 (4095) instead of 1020 (4080) for bit-depth 10 (12)
    #else
        const uint64_t uiSSDtemp = xFindDistortionPlane(recPB, orgPB, 0);
    #if WCG_WPSNR
      const double uiSSDtempWeighted = xFindDistortionPlaneWPSNR(recPB, orgPB, 0, org.get(COMPONENT_Y), compID, format);
    #endif
        const uint32_t maxval = 255 << (bitDepth - 8);
    #endif
        const uint32_t size   = width * height;
        const double fRefValue = (double)maxval * maxval * size;
        dPSNR[comp]       = uiSSDtemp ? 10.0 * log10(fRefValue / (double)uiSSDtemp) : 999.99;
        MSEyuvframe[comp] = (double)uiSSDtemp / size;
    #if WCG_WPSNR
        if (useLumaWPSNR)
        {
          dPSNRWeighted[comp] = uiSSDtempWeighted ? 10.0 * log10(fRefValue / (double)uiSSDtempWeighted) : 999.99;
          MSEyuvframeWeighted[comp] = (double)uiSSDtempWeighted / size;
        }
    #endif
    
    #if ENABLE_QPA && FRAME_WEIGHTING
        if (useWPSNR) m_gcAnalyzeAll.addWeightedSSD(frameWeight * (double)uiSSDtemp / fRefValue, compID);
    #endif
      }
    
    #if EXTENSION_360_VIDEO
      m_ext360.calculatePSNRs(pcPic);
    #endif
    
      /* calculate the size of the access unit, excluding:
       *  - any AnnexB contributions (start_code_prefix, zero_byte, etc.,)
       *  - SEI NAL units
       */
      uint32_t numRBSPBytes = 0;
      for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
      {
        uint32_t numRBSPBytes_nal = uint32_t((*it)->m_nalUnitData.str().size());
        if (m_pcCfg->getSummaryVerboseness() > 0)
        {
          msg( NOTICE, "*** %6s numBytesInNALunit: %u\n", nalUnitTypeToString((*it)->m_nalUnitType), numRBSPBytes_nal);
        }
        if( ( *it )->m_nalUnitType != NAL_UNIT_PREFIX_SEI && ( *it )->m_nalUnitType != NAL_UNIT_SUFFIX_SEI )
        {
          numRBSPBytes += numRBSPBytes_nal;
    #if HEVC_VPS
          if( it == accessUnit.begin() || ( *it )->m_nalUnitType == NAL_UNIT_VPS || ( *it )->m_nalUnitType == NAL_UNIT_SPS || ( *it )->m_nalUnitType == NAL_UNIT_PPS )
    #else
          if (it == accessUnit.begin() || (*it)->m_nalUnitType == NAL_UNIT_SPS || (*it)->m_nalUnitType == NAL_UNIT_PPS)
    #endif
          {
            numRBSPBytes += 4;
          }
          else
          {
            numRBSPBytes += 3;
          }
        }
      }
    
      uint32_t uibits = numRBSPBytes * 8;
      m_vRVM_RP.push_back( uibits );
    
      //===== add PSNR =====
      m_gcAnalyzeAll.addResult (dPSNR, (double)uibits, MSEyuvframe);
    #if EXTENSION_360_VIDEO
      m_ext360.addResult(m_gcAnalyzeAll);
    #endif
      if (pcSlice->isIntra())
      {
        m_gcAnalyzeI.addResult (dPSNR, (double)uibits, MSEyuvframe);
        *PSNR_Y = dPSNR[COMPONENT_Y];
    #if EXTENSION_360_VIDEO
        m_ext360.addResult(m_gcAnalyzeI);
    #endif
      }
      if (pcSlice->isInterP())
      {
        m_gcAnalyzeP.addResult (dPSNR, (double)uibits, MSEyuvframe);
        *PSNR_Y = dPSNR[COMPONENT_Y];
    #if EXTENSION_360_VIDEO
        m_ext360.addResult(m_gcAnalyzeP);
    #endif
      }
      if (pcSlice->isInterB())
      {
        m_gcAnalyzeB.addResult (dPSNR, (double)uibits, MSEyuvframe);
        *PSNR_Y = dPSNR[COMPONENT_Y];
    #if EXTENSION_360_VIDEO
        m_ext360.addResult(m_gcAnalyzeB);
    #endif
      }
    #if WCG_WPSNR
      if (useLumaWPSNR)
      {
        m_gcAnalyzeWPSNR.addResult(dPSNRWeighted, (double)uibits, MSEyuvframeWeighted);
      }
    #endif
    
      char c = (pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B');
      if (! pcPic->referenced)
      {
        c += 32;
      }
    
      if( g_verbosity >= NOTICE )
      {
        msg( NOTICE, "POC %4d TId: %1d ( %c-SLICE, QP %d ) %10d bits",
             pcSlice->getPOC() - pcSlice->getLastIDR(),
             pcSlice->getTLayer(),
             c,
             pcSlice->getSliceQp(),
             uibits );
    
        msg( NOTICE, " [Y %6.4lf dB    U %6.4lf dB    V %6.4lf dB]", dPSNR[COMPONENT_Y], dPSNR[COMPONENT_Cb], dPSNR[COMPONENT_Cr] );
    
    
    #if EXTENSION_360_VIDEO
        m_ext360.printPerPOCInfo(NOTICE);
    #endif
    
    
        if (m_pcEncLib->getPrintHexPsnr())
        {
          uint64_t xPsnr[MAX_NUM_COMPONENT];
          for (int i = 0; i < MAX_NUM_COMPONENT; i++)
          {
            copy(reinterpret_cast<uint8_t *>(&dPSNR[i]),
                 reinterpret_cast<uint8_t *>(&dPSNR[i]) + sizeof(dPSNR[i]),
                 reinterpret_cast<uint8_t *>(&xPsnr[i]));
          }
          msg(NOTICE, " [xY %16" PRIx64 " xU %16" PRIx64 " xV %16" PRIx64 "]", xPsnr[COMPONENT_Y], xPsnr[COMPONENT_Cb], xPsnr[COMPONENT_Cr]);
    
    #if EXTENSION_360_VIDEO
    
          m_ext360.printPerPOCInfo(NOTICE, true);
    
    
        if( printFrameMSE )
        {
          msg( NOTICE, " [Y MSE %6.4lf  U MSE %6.4lf  V MSE %6.4lf]", MSEyuvframe[COMPONENT_Y], MSEyuvframe[COMPONENT_Cb], MSEyuvframe[COMPONENT_Cr] );
        }
    #if WCG_WPSNR
        if (useLumaWPSNR)
        {