Skip to content
Snippets Groups Projects
EncLib.h 15.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* The copyright in this software is being made available under the BSD
     * License, included below. This software may be subject to other third party
     * and contributor rights, including patent rights, and no such rights are
     * granted under this license.
     *
    
     * Copyright (c) 2010-2023, ITU/ISO/IEC
    
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions are met:
     *
     *  * Redistributions of source code must retain the above copyright notice,
     *    this list of conditions and the following disclaimer.
     *  * Redistributions in binary form must reproduce the above copyright notice,
     *    this list of conditions and the following disclaimer in the documentation
     *    and/or other materials provided with the distribution.
     *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
     *    be used to endorse or promote products derived from this software without
     *    specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
     * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     * THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /** \file     EncLib.h
        \brief    encoder class (header)
    */
    
    #ifndef __ENCTOP__
    #define __ENCTOP__
    
    // Include files
    #include "CommonLib/TrQuant.h"
    #include "CommonLib/LoopFilter.h"
    #include "CommonLib/NAL.h"
    
    #if JVET_V0094_BILATERAL_FILTER || JVET_X0071_CHROMA_BILATERAL_FILTER
    
    Vadim Seregin's avatar
    Vadim Seregin committed
    #include "CommonLib/BilateralFilter.h"
    #endif
    
    
    #include "Utilities/VideoIOYuv.h"
    
    #include "EncCfg.h"
    #include "EncGOP.h"
    #include "EncSlice.h"
    
    #include "VLCWriter.h"
    #include "CABACWriter.h"
    #include "InterSearch.h"
    #include "IntraSearch.h"
    #include "EncSampleAdaptiveOffset.h"
    
    Taoran Lu's avatar
    Taoran Lu committed
    #include "EncReshape.h"
    
    #include "EncAdaptiveLoopFilter.h"
    #include "RateCtrl.h"
    
    
    class EncLibCommon;
    
    //! \ingroup EncoderLib
    //! \{
    
    // ====================================================================================================================
    // Class definition
    // ====================================================================================================================
    
    /// encoder class
    class EncLib : public EncCfg
    {
    private:
      // picture
      int                       m_iPOCLast;                           ///< time index (POC)
      int                       m_iNumPicRcvd;                        ///< number of received pictures
    
      uint32_t                  m_uiNumAllPicCoded;                   ///< number of coded pictures
      PicList&                  m_cListPic;                           ///< dynamic list of pictures
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      int                       m_layerId;
    
    #if ENABLE_SPLIT_PARALLELISM
    
      InterSearch              *m_cInterSearch;                       ///< encoder search class
      IntraSearch              *m_cIntraSearch;                       ///< encoder search class
    #else
      InterSearch               m_cInterSearch;                       ///< encoder search class
      IntraSearch               m_cIntraSearch;                       ///< encoder search class
    #endif
      // coding tool
    
    #if ENABLE_SPLIT_PARALLELISM
    
    #if JVET_V0094_BILATERAL_FILTER || JVET_X0071_CHROMA_BILATERAL_FILTER
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      BilateralFilter          *m_bilateralFilter;
    #endif
    
      TrQuant                  *m_cTrQuant;                           ///< transform & quantization class
    #else
    
    #if JVET_V0094_BILATERAL_FILTER || JVET_X0071_CHROMA_BILATERAL_FILTER
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      BilateralFilter           m_bilateralFilter;
    #endif
    
      TrQuant                   m_cTrQuant;                           ///< transform & quantization class
    #endif
      LoopFilter                m_cLoopFilter;                        ///< deblocking filter class
      EncSampleAdaptiveOffset   m_cEncSAO;                            ///< sample adaptive offset class
      EncAdaptiveLoopFilter     m_cEncALF;
      HLSWriter                 m_HLSWriter;                          ///< CAVLC encoder
    
    #if ENABLE_SPLIT_PARALLELISM
    
      CABACEncoder             *m_CABACEncoder;
    #else
      CABACEncoder              m_CABACEncoder;
    #endif
    
    
    #if ENABLE_SPLIT_PARALLELISM
    
      EncReshape               *m_cReshaper;                        ///< reshaper class
    #else
    
    Taoran Lu's avatar
    Taoran Lu committed
      EncReshape                m_cReshaper;                        ///< reshaper class
    #endif
    
    
    #if JVET_AA0096_MC_BOUNDARY_PADDING
      InterPrediction m_cFrameMcPadPredSearch;
    #endif
    
    
      // processing unit
      EncGOP                    m_cGOPEncoder;                        ///< GOP encoder
      EncSlice                  m_cSliceEncoder;                      ///< slice encoder
    
    #if ENABLE_SPLIT_PARALLELISM
    
      EncCu                    *m_cCuEncoder;                         ///< CU encoder
    #else
      EncCu                     m_cCuEncoder;                         ///< CU encoder
    #endif
      // SPS
    
      ParameterSetMap<SPS>&     m_spsMap;                             ///< SPS. This is the base value. This is copied to PicSym
      ParameterSetMap<PPS>&     m_ppsMap;                             ///< PPS. This is the base value. This is copied to PicSym
      ParameterSetMap<APS>&     m_apsMap;                             ///< APS. This is the base value. This is copied to PicSym
    
    Brian Heng's avatar
    Brian Heng committed
      PicHeader                 m_picHeader;                          ///< picture header
    
    #if ENABLE_SPLIT_PARALLELISM
    
      RdCost                   *m_cRdCost;                            ///< RD cost computation class
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      CtxCache                 *m_ctxCache;                           ///< buffer for temporarily stored context models
    
    #else
      RdCost                    m_cRdCost;                            ///< RD cost computation class
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      CtxCache                  m_ctxCache;                           ///< buffer for temporarily stored context models
    
    #endif
      // quality control
      RateCtrl                  m_cRateCtrl;                          ///< Rate control class
    
      AUWriterIf*               m_AUWriterIf;
    
    
    #if ENABLE_SPLIT_PARALLELISM
    
      int                       m_numCuEncStacks;
    #endif
    
    #if JVET_J0090_MEMORY_BANDWITH_MEASURE
      CacheModel                m_cacheModel;
    #endif
    
    
    Luhang Xu's avatar
    Luhang Xu committed
    #if JVET_AK0065_TALF
      APS*                      m_apss2[ALF_CTB_MAX_NUM_APS];
    #endif
    
      APS*                      m_apss[ALF_CTB_MAX_NUM_APS];
    
    #if JVET_O0756_CALCULATE_HDRMETRICS
      std::chrono::duration<long long, ratio<1, 1000000000>> m_metricTime;
    #endif
    
      SPS*                      getSPS( int spsId ) { return m_spsMap.getPS( spsId ); };
      APS**                     getApss() { return m_apss; }
    
    Luhang Xu's avatar
    Luhang Xu committed
    #if JVET_AK0065_TALF
      APS**                     getApss2() { return m_apss2; }
    #endif
    
      Ctx                       m_entropyCodingSyncContextState;      ///< leave in addition to vector for compatibility
    
    #if JVET_AC0096 || JVET_AG0116
    
      int                       m_gopRprPpsId;
    #endif
    
    protected:
      void  xGetNewPicBuffer  ( std::list<PelUnitBuf*>& rcListPicYuvRecOut, Picture*& rpcPic, int ppsId ); ///< get picture buffer which will be processed. If ppsId<0, then the ppsMap will be queried for the first match.
    
      void  xInitDCI(DCI& dci, const SPS& sps); ///< initialize Decoding Capability Information (DCI) from encoder options
    
      void  xInitVPS( const SPS& sps ); ///< initialize VPS from encoder options
      void  xInitSPS( SPS& sps );       ///< initialize SPS from encoder options
    
      void  xInitPPS          (PPS &pps, const SPS &sps); ///< initialize PPS from encoder options
    
    Brian Heng's avatar
    Brian Heng committed
      void  xInitPicHeader    (PicHeader &picHeader, const SPS &sps, const PPS &pps); ///< initialize Picture Header from encoder options
    
    Hendry's avatar
    Hendry committed
      void  xInitAPS          (APS &aps);                 ///< initialize APS from encoder options
    
      void  xInitScalingLists ( SPS &sps, APS &aps );     ///< initialize scaling lists
    
      void  xInitHrdParameters(SPS &sps);                 ///< initialize HRDParameters parameters
    
    Hendry's avatar
    Hendry committed
      void  xInitRPL(SPS &sps, bool isFieldCoding);           ///< initialize SPS from encoder options
    
      EncLib( EncLibCommon* encLibCommon );
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      void      create          ( const int layerId );
    
      void      destroy         ();
      void      init            ( bool isFieldCoding, AUWriterIf* auWriterIf );
      void      deletePicBuffer ();
    
      // -------------------------------------------------------------------------------------------------------------------
      // member access functions
      // -------------------------------------------------------------------------------------------------------------------
    
      AUWriterIf*             getAUWriterIf         ()              { return   m_AUWriterIf;           }
      PicList*                getListPic            ()              { return  &m_cListPic;             }
    
    #if ENABLE_SPLIT_PARALLELISM
    
      InterSearch*            getInterSearch        ( int jId = 0 ) { return  &m_cInterSearch[jId];    }
      IntraSearch*            getIntraSearch        ( int jId = 0 ) { return  &m_cIntraSearch[jId];    }
    
    
    #if JVET_V0094_BILATERAL_FILTER || JVET_X0071_CHROMA_BILATERAL_FILTER
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      BilateralFilter*        getBilateralFilter    ( int jId = 0 ) { return  &m_bilateralFilter[jId]; }
    #endif
    
      TrQuant*                getTrQuant            ( int jId = 0 ) { return  &m_cTrQuant[jId];        }
    #else
      InterSearch*            getInterSearch        ()              { return  &m_cInterSearch;         }
      IntraSearch*            getIntraSearch        ()              { return  &m_cIntraSearch;         }
    
    
    #if JVET_V0094_BILATERAL_FILTER || JVET_X0071_CHROMA_BILATERAL_FILTER
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      BilateralFilter*        getBilateralFilter    ()              { return  &m_bilateralFilter;      }
    #endif
    
      TrQuant*                getTrQuant            ()              { return  &m_cTrQuant;             }
    #endif
      LoopFilter*             getLoopFilter         ()              { return  &m_cLoopFilter;          }
      EncSampleAdaptiveOffset* getSAO               ()              { return  &m_cEncSAO;              }
      EncAdaptiveLoopFilter*  getALF                ()              { return  &m_cEncALF;              }
      EncGOP*                 getGOPEncoder         ()              { return  &m_cGOPEncoder;          }
      EncSlice*               getSliceEncoder       ()              { return  &m_cSliceEncoder;        }
    
    #if ENABLE_SPLIT_PARALLELISM
    
      EncCu*                  getCuEncoder          ( int jId = 0 ) { return  &m_cCuEncoder[jId];      }
    #else
      EncCu*                  getCuEncoder          ()              { return  &m_cCuEncoder;           }
    #endif
      HLSWriter*              getHLSWriter          ()              { return  &m_HLSWriter;            }
    
    #if ENABLE_SPLIT_PARALLELISM
    
      CABACEncoder*           getCABACEncoder       ( int jId = 0 ) { return  &m_CABACEncoder[jId];    }
    
      RdCost*                 getRdCost             ( int jId = 0 ) { return  &m_cRdCost[jId];         }
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      CtxCache*               getCtxCache           ( int jId = 0 ) { return  &m_ctxCache[jId];        }
    
    #else
      CABACEncoder*           getCABACEncoder       ()              { return  &m_CABACEncoder;         }
    
    #if JVET_AA0096_MC_BOUNDARY_PADDING
      InterPrediction *         getFrameMcPadPredSearch() { return &m_cFrameMcPadPredSearch; }
    #endif
    
      RdCost*                 getRdCost             ()              { return  &m_cRdCost;              }
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      CtxCache*               getCtxCache           ()              { return  &m_ctxCache;             }
    
    #endif
      RateCtrl*               getRateCtrl           ()              { return  &m_cRateCtrl;            }
    
    
    Hendry's avatar
    Hendry committed
      void                    getActiveRefPicListNumForPOC(const SPS *sps, int POCCurr, int GOPid, uint32_t *activeL0, uint32_t *activeL1);
      void                    selectReferencePictureList(Slice* slice, int POCCurr, int GOPid, int ltPoc);
    
      void                   setParamSetChanged(int spsId, int ppsId);
    
    Hendry's avatar
    Hendry committed
      bool                   APSNeedsWriting(int apsId);
    
      bool                   PPSNeedsWriting(int ppsId);
      bool                   SPSNeedsWriting(int spsId);
      const PPS* getPPS( int Id ) { return m_ppsMap.getPS( Id); }
    
    Hendry's avatar
    Hendry committed
      const APS*             getAPS(int Id) { return m_apsMap.getPS(Id); }
    
    #if ENABLE_SPLIT_PARALLELISM
    
      void                   setNumCuEncStacks( int n )             { m_numCuEncStacks = n; }
      int                    getNumCuEncStacks()              const { return m_numCuEncStacks; }
    #endif
    
    
    #if ENABLE_SPLIT_PARALLELISM
    
      EncReshape*            getReshaper( int jId = 0 )             { return  &m_cReshaper[jId]; }
    #else
    
    Taoran Lu's avatar
    Taoran Lu committed
      EncReshape*            getReshaper()                          { return  &m_cReshaper; }
    #endif
    
    
      ParameterSetMap<APS>*  getApsMap() { return &m_apsMap; }
    
      EncTemporalFilter&     getTemporalFilter()                    { return m_temporalFilter; }
    
    
      bool                   getPltEnc()                      const { return   m_doPlt; }
    
      void                   checkPltStats( Picture* pic );
    
    #if JVET_O0756_CALCULATE_HDRMETRICS
      std::chrono::duration<long long, ratio<1, 1000000000>> getMetricTime()    const { return m_metricTime; };
    
      // -------------------------------------------------------------------------------------------------------------------
      // encoder function
      // -------------------------------------------------------------------------------------------------------------------
    
      /// encode several number of pictures until end-of-sequence
    
      bool encodePrep( bool bEos,
                   PelStorage* pcPicYuvOrg,
    
    Vadim Seregin's avatar
    Vadim Seregin committed
                   const InputColourSpaceConversion snrCSC, // used for SNR calculations. Picture in original colour space.
    
                   std::list<PelUnitBuf*>& rcListPicYuvRecOut,
    
                   int& iNumEncoded
    #if JVET_AG0116
        , PelStorage** ppcPicYuvRPR
    #endif
      );
    
    
      bool encode( const InputColourSpaceConversion snrCSC, // used for SNR calculations. Picture in original colour space.
                   std::list<PelUnitBuf*>& rcListPicYuvRecOut,
                   int& iNumEncoded );
    
      bool encodePrep( bool bEos,
                   PelStorage* pcPicYuvOrg,
    
    Vadim Seregin's avatar
    Vadim Seregin committed
                   const InputColourSpaceConversion snrCSC, // used for SNR calculations. Picture in original colour space.
    
                   std::list<PelUnitBuf*>& rcListPicYuvRecOut,
                   int& iNumEncoded, bool isTff );
    
      bool encode( const InputColourSpaceConversion snrCSC, // used for SNR calculations. Picture in original colour space.
                   std::list<PelUnitBuf*>& rcListPicYuvRecOut,
                   int& iNumEncoded, bool isTff );
    
      void printSummary(bool isField) {
        m_cGOPEncoder.printOutSummary(m_uiNumAllPicCoded, isField, m_printMSEBasedSequencePSNR, m_printSequenceMSE,
    
    #if MSSIM_UNIFORM_METRICS_LOG
                                      m_printMSSSIM,
    #endif
    
                                      m_printHexPsnr, m_resChangeInClvsEnabled, m_spsMap.getFirstPS()->getBitDepths()
    #if JVET_W0134_UNIFORM_METRICS_LOG
                                                                                  ,
                                      m_layerId
    #endif
        ); }
    
    Vadim Seregin's avatar
    Vadim Seregin committed
      int getLayerId() const { return m_layerId; }
    
      VPS* getVPS()          { return m_vps;     }
    
    Vadim Seregin's avatar
    Vadim Seregin committed
    
    #if DUMP_BEFORE_INLOOP
      std::string m_reconFileName;
    #endif
    
    
    #if JVET_AA0093_DIVERSITY_CRITERION_FOR_ARMC
      void setQPOffsetList(const int QPOffset[MAX_GOP]);
    #endif