EncGOP.h 12.2 KB
Newer Older
1
2
3
4
5
/* The copyright in this software is being made available under the BSD
 * License, included below. This software may be subject to other third party
 * and contributor rights, including patent rights, and no such rights are
 * granted under this license.
 *
6
 * Copyright (c) 2010-2019, ITU/ISO/IEC
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 * 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     EncGOP.h
    \brief    GOP encoder class (header)
*/

#ifndef __ENCGOP__
#define __ENCGOP__

#include <list>

#include <stdlib.h>

#include "CommonLib/Picture.h"
#include "CommonLib/LoopFilter.h"
#include "CommonLib/NAL.h"
#include "EncSampleAdaptiveOffset.h"
#include "EncAdaptiveLoopFilter.h"
Taoran Lu's avatar
Taoran Lu committed
50
#include "EncReshape.h"
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "EncSlice.h"
#include "VLCWriter.h"
#include "CABACWriter.h"
#include "SEIwrite.h"
#include "SEIEncoder.h"
#if EXTENSION_360_VIDEO
#include "AppEncHelper360/TExt360EncGop.h"
#endif

#include "Analyze.h"
#include "RateCtrl.h"
#include <vector>

//! \ingroup EncoderLib
//! \{

class EncLib;

// ====================================================================================================================
// Class definition
// ====================================================================================================================
class AUWriterIf
{
public:
  virtual void outputAU( const AccessUnit& ) = 0;
};


class EncGOP
{
  class DUData
  {
  public:
    DUData()
    :accumBitsDU(0)
    ,accumNalsDU(0) {};

    int accumBitsDU;
    int accumNalsDU;
  };

private:

  Analyze                 m_gcAnalyzeAll;
  Analyze                 m_gcAnalyzeI;
  Analyze                 m_gcAnalyzeP;
  Analyze                 m_gcAnalyzeB;
#if WCG_WPSNR
  Analyze                 m_gcAnalyzeWPSNR;
#endif
  Analyze                 m_gcAnalyzeAll_in;
#if EXTENSION_360_VIDEO
  TExt360EncGop           m_ext360;
public:
  TExt360EncGop &getExt360Data() { return m_ext360; }
private:
#endif

  //  Data
  bool                    m_bLongtermTestPictureHasBeenCoded;
  bool                    m_bLongtermTestPictureHasBeenCoded2;
  uint32_t                    m_numLongTermRefPicSPS;
  uint32_t                    m_ltRefPicPocLsbSps[MAX_NUM_LONG_TERM_REF_PICS];
  bool                    m_ltRefPicUsedByCurrPicFlag[MAX_NUM_LONG_TERM_REF_PICS];
  int                     m_iLastIDR;
  int                     m_iGopSize;
  int                     m_iNumPicCoded;
  bool                    m_bFirst;
  int                     m_iLastRecoveryPicPOC;
  int                     m_lastRasPoc;

  //  Access channel
  EncLib*                 m_pcEncLib;
  EncCfg*                 m_pcCfg;
  EncSlice*               m_pcSliceEncoder;
  PicList*                m_pcListPic;

  HLSWriter*              m_HLSWriter;
  LoopFilter*             m_pcLoopFilter;

  SEIWriter               m_seiWriter;

133
134
135
136
137
138
139
  Picture *               m_picBg;
  Picture *               m_picOrig;
  int                     m_bgPOC;
  bool                    m_isEncodedLTRef;
  bool                    m_isPrepareLTRef;
  bool                    m_isUseLTRef;
  int                     m_lastLTRefPoc;
140
141
142
  //--Adaptive Loop filter
  EncSampleAdaptiveOffset*  m_pcSAO;
  EncAdaptiveLoopFilter*    m_pcALF;
Taoran Lu's avatar
Taoran Lu committed
143
  EncReshape*               m_pcReshaper;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  RateCtrl*                 m_pcRateCtrl;
  // indicate sequence first
  bool                    m_bSeqFirst;

  // clean decoding refresh
  bool                    m_bRefreshPending;
  int                     m_pocCRA;
  NalUnitType             m_associatedIRAPType;
  int                     m_associatedIRAPPOC;

  std::vector<int>        m_vRVM_RP;
  uint32_t                    m_lastBPSEI;
  uint32_t                    m_totalCoded;
  bool                    m_bufferingPeriodSEIPresentInAU;
  SEIEncoder              m_seiEncoder;
#if W0038_DB_OPT
  PelStorage*             m_pcDeblockingTempPicYuv;
  int                     m_DBParam[MAX_ENCODER_DEBLOCKING_QUALITY_LAYERS][4];   //[layer_id][0: available; 1: bDBDisabled; 2: Beta Offset Div2; 3: Tc Offset Div2;]
#endif

  // members needed for adaptive max BT size
  uint32_t                    m_uiBlkSize[10];
  uint32_t                    m_uiNumBlk[10];
  uint32_t                    m_uiPrevISlicePOC;
  bool                    m_bInitAMaxBT;

  AUWriterIf*             m_AUWriterIf;

public:
  EncGOP();
  virtual ~EncGOP();

  void  create      ();
  void  destroy     ();

  void  init        ( EncLib* pcEncLib );
  void  compressGOP ( int iPOCLast, int iNumPicRcvd, PicList& rcListPic, std::list<PelUnitBuf*>& rcListPicYuvRec,
181
182
183
                      bool isField, bool isTff, const InputColourSpaceConversion snr_conversion, const bool printFrameMSE
                    , bool isEncodeLtRef
  );
184
185
186
187
188
189
  void  xAttachSliceDataToNalUnit (OutputNALUnit& rNalu, OutputBitstream* pcBitstreamRedirect);


  int   getGOPSize()          { return  m_iGopSize;  }

  PicList*   getListPic()      { return m_pcListPic; }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  void      setPicBg(Picture* tmpPicBg) { m_picBg = tmpPicBg; }
  Picture*  getPicBg() const { return m_picBg; }
  void      setPicOrig(Picture* tmpPicBg) { m_picOrig = tmpPicBg; }
  Picture*  getPicOrig() { return m_picOrig; }
  void      setNewestBgPOC(int poc) { m_bgPOC = poc; }
  int       getNewestBgPOC() const { return m_bgPOC; }
  void      setEncodedLTRef(bool isEncodedLTRef) { m_isEncodedLTRef = isEncodedLTRef; }
  bool      getEncodedLTRef() { return m_isEncodedLTRef; }
  void      setUseLTRef(bool isUseLTRef) { m_isUseLTRef = isUseLTRef; }
  bool      getUseLTRef() { return m_isUseLTRef; }
  void      setPrepareLTRef(bool isPrepareLTRef) { m_isPrepareLTRef = isPrepareLTRef; }
  bool      getPrepareLTRef() { return m_isPrepareLTRef; }
  void      setLastLTRefPoc(int iLastLTRefPoc) { m_lastLTRefPoc = iLastLTRefPoc; }
  int       getLastLTRefPoc() const { return m_lastLTRefPoc; }
204

205
  void  printOutSummary      ( uint32_t uiNumAllPicCoded, bool isField, const bool printMSEBasedSNR, const bool printSequenceMSE, const bool printHexPsnr, const BitDepths &bitDepths );
206
207
208
209
210
#if W0038_DB_OPT
  uint64_t  preLoopFilterPicAndCalcDist( Picture* pcPic );
#endif
  EncSlice*  getSliceEncoder()   { return m_pcSliceEncoder; }
  NalUnitType getNalUnitType( int pocCurr, int lastIdr, bool isField );
211
212
  void arrangeCompositeReference(Slice* pcSlice, PicList& rcListPic, int pocCurr);
  void updateCompositeReference(Slice* pcSlice, PicList& rcListPic, int pocCurr);
213
214
215
216
217
218
219
220
221
222
223
224
225

#if EXTENSION_360_VIDEO
  Analyze& getAnalyzeAllData() { return m_gcAnalyzeAll; }
  Analyze& getAnalyzeIData() { return m_gcAnalyzeI; }
  Analyze& getAnalyzePData() { return m_gcAnalyzeP; }
  Analyze& getAnalyzeBData() { return m_gcAnalyzeB; }
#endif

protected:
  RateCtrl* getRateCtrl()       { return m_pcRateCtrl;  }

protected:

226
227
228
  void  xInitGOP          ( int iPOCLast, int iNumPicRcvd, bool isField
    , bool isEncodeLtRef
  );
229
230
231
  void  xGetBuffer        ( PicList& rcListPic, std::list<PelUnitBuf*>& rcListPicYuvRecOut,
                            int iNumPicRcvd, int iTimeOffset, Picture*& rpcPic, int pocCurr, bool isField );

232
233
234
235
236
237
  void  xCalculateAddPSNRs(const bool isField, const bool isFieldTopFieldFirst, const int iGOPid, Picture* pcPic, const AccessUnit&accessUnit, PicList &rcListPic, int64_t dEncTime, const InputColourSpaceConversion snr_conversion, const bool printFrameMSE, double* PSNR_Y
    , bool isEncodeLtRef
  );
  void  xCalculateAddPSNR(Picture* pcPic, PelUnitBuf cPicD, const AccessUnit&, double dEncTime, const InputColourSpaceConversion snr_conversion, const bool printFrameMSE, double* PSNR_Y
    , bool isEncodeLtRef
  );
238
239
  void  xCalculateInterlacedAddPSNR( Picture* pcPicOrgFirstField, Picture* pcPicOrgSecondField,
                                     PelUnitBuf cPicRecFirstField, PelUnitBuf cPicRecSecondField,
240
241
242
                                     const InputColourSpaceConversion snr_conversion, const bool printFrameMSE, double* PSNR_Y
                                    , bool isEncodeLtRef
  );
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

  uint64_t xFindDistortionPlane(const CPelBuf& pic0, const CPelBuf& pic1, const uint32_t rshift
#if ENABLE_QPA
                            , const uint32_t chromaShift = 0
#endif
                             );
#if WCG_WPSNR
  double xFindDistortionPlaneWPSNR(const CPelBuf& pic0, const CPelBuf& pic1, const uint32_t rshift, const CPelBuf& picLuma0, ComponentID compID, const ChromaFormat chfmt );
#endif
  double xCalculateRVM();

  void xUpdateRasInit(Slice* slice);

  void xWriteAccessUnitDelimiter (AccessUnit &accessUnit, Slice *slice);

  void xCreateIRAPLeadingSEIMessages (SEIMessages& seiMessages, const SPS *sps, const PPS *pps);
  void xCreatePerPictureSEIMessages (int picInGOP, SEIMessages& seiMessages, SEIMessages& nestedSeiMessages, Slice *slice);
  void xCreatePictureTimingSEI  (int IRAPGOPid, SEIMessages& seiMessages, SEIMessages& nestedSeiMessages, SEIMessages& duInfoSeiMessages, Slice *slice, bool isField, std::deque<DUData> &duData);
  void xUpdateDuData(AccessUnit &testAU, std::deque<DUData> &duData);
  void xUpdateTimingSEI(SEIPictureTiming *pictureTimingSEI, std::deque<DUData> &duData, const SPS *sps);
  void xUpdateDuInfoSEI(SEIMessages &duInfoSeiMessages, SEIPictureTiming *pictureTimingSEI);

  void xCreateScalableNestingSEI (SEIMessages& seiMessages, SEIMessages& nestedSeiMessages);
  void xWriteSEI (NalUnitType naluType, SEIMessages& seiMessages, AccessUnit &accessUnit, AccessUnit::iterator &auPos, int temporalId, const SPS *sps);
  void xWriteSEISeparately (NalUnitType naluType, SEIMessages& seiMessages, AccessUnit &accessUnit, AccessUnit::iterator &auPos, int temporalId, const SPS *sps);
  void xClearSEIs(SEIMessages& seiMessages, bool deleteMessages);
  void xWriteLeadingSEIOrdered (SEIMessages& seiMessages, SEIMessages& duInfoSeiMessages, AccessUnit &accessUnit, int temporalId, const SPS *sps, bool testWrite);
  void xWriteLeadingSEIMessages  (SEIMessages& seiMessages, SEIMessages& duInfoSeiMessages, AccessUnit &accessUnit, int temporalId, const SPS *sps, std::deque<DUData> &duData);
  void xWriteTrailingSEIMessages (SEIMessages& seiMessages, AccessUnit &accessUnit, int temporalId, const SPS *sps);
  void xWriteDuSEIMessages       (SEIMessages& duInfoSeiMessages, AccessUnit &accessUnit, int temporalId, const SPS *sps, std::deque<DUData> &duData);

  int xWriteVPS (AccessUnit &accessUnit, const VPS *vps);
275
  int xWriteDPS (AccessUnit &accessUnit, const DPS *dps);
276
  int xWriteSPS (AccessUnit &accessUnit, const SPS *sps);
277
278
279
#if JVET_O1136_TS_BDPCM_SIGNALLING
  int xWritePPS (AccessUnit &accessUnit, const PPS *pps, const SPS *sps);
#else
280
  int xWritePPS (AccessUnit &accessUnit, const PPS *pps);
281
#endif
Hendry's avatar
Hendry committed
282
  int xWriteAPS(AccessUnit &accessUnit, APS *aps);
283
284
285
286
287
288
289
290
291
292
293
294
  int xWriteParameterSets (AccessUnit &accessUnit, Slice *slice, const bool bSeqFirst);

  void applyDeblockingFilterMetric( Picture* pcPic, uint32_t uiNumSlices );
#if W0038_DB_OPT
  void applyDeblockingFilterParameterSelection( Picture* pcPic, const uint32_t numSlices, const int gopID );
#endif
};// END CLASS DEFINITION EncGOP

//! \}

#endif // __ENCGOP__