HEVC Test Model (HM)  HM-16.3
TEncGOP.cpp
Go to the documentation of this file.
1 /* The copyright in this software is being made available under the BSD
2  * License, included below. This software may be subject to other third party
3  * and contributor rights, including patent rights, and no such rights are
4  * granted under this license.
5  *
6  * Copyright (c) 2010-2015, ITU/ISO/IEC
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18  * be used to endorse or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
38 #include <list>
39 #include <algorithm>
40 #include <functional>
41 
42 #include "TEncTop.h"
43 #include "TEncGOP.h"
44 #include "TEncAnalyze.h"
45 #include "libmd5/MD5.h"
46 #include "TLibCommon/SEI.h"
47 #include "TLibCommon/NAL.h"
48 #include "NALwrite.h"
49 #include <time.h>
50 #include <math.h>
51 
52 using namespace std;
53 
54 #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
55 Bool g_bFinalEncode = false;
56 #endif
57 
60 
61 // ====================================================================================================================
62 // Constructor / destructor / initialization / destroy
63 // ====================================================================================================================
64 Int getLSB(Int poc, Int maxLSB)
65 {
66  if (poc >= 0)
67  {
68  return poc % maxLSB;
69  }
70  else
71  {
72  return (maxLSB - ((-poc) % maxLSB)) % maxLSB;
73  }
74 }
75 
77 {
78  m_iLastIDR = 0;
79  m_iGopSize = 0;
80  m_iNumPicCoded = 0; //Niko
81  m_bFirst = true;
82 #if ALLOW_RECOVERY_POINT_AS_RAP
83  m_iLastRecoveryPicPOC = 0;
84 #endif
85 
86  m_pcCfg = NULL;
87  m_pcSliceEncoder = NULL;
88  m_pcListPic = NULL;
89 
90  m_pcEntropyCoder = NULL;
91  m_pcCavlcCoder = NULL;
92  m_pcSbacCoder = NULL;
93  m_pcBinCABAC = NULL;
94 
95  m_bSeqFirst = true;
96 
97  m_bRefreshPending = 0;
98  m_pocCRA = 0;
99  m_numLongTermRefPicSPS = 0;
100  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
101  ::memset(m_ltRefPicUsedByCurrPicFlag, 0, sizeof(m_ltRefPicUsedByCurrPicFlag));
102  m_cpbRemovalDelay = 0;
103  m_lastBPSEI = 0;
104  xResetNonNestedSEIPresentFlags();
105  xResetNestedSEIPresentFlags();
106  m_associatedIRAPType = NAL_UNIT_CODED_SLICE_IDR_N_LP;
107  m_associatedIRAPPOC = 0;
108  return;
109 }
110 
112 {
113 }
114 
118 {
119  m_bLongtermTestPictureHasBeenCoded = 0;
120  m_bLongtermTestPictureHasBeenCoded2 = 0;
121 }
122 
124 {
125 }
126 
127 Void TEncGOP::init ( TEncTop* pcTEncTop )
128 {
129  m_pcEncTop = pcTEncTop;
130  m_pcCfg = pcTEncTop;
131  m_pcSliceEncoder = pcTEncTop->getSliceEncoder();
132  m_pcListPic = pcTEncTop->getListPic();
133 
134  m_pcEntropyCoder = pcTEncTop->getEntropyCoder();
135  m_pcCavlcCoder = pcTEncTop->getCavlcCoder();
136  m_pcSbacCoder = pcTEncTop->getSbacCoder();
137  m_pcBinCABAC = pcTEncTop->getBinCABAC();
138  m_pcLoopFilter = pcTEncTop->getLoopFilter();
139 
140  m_pcSAO = pcTEncTop->getSAO();
141  m_pcRateCtrl = pcTEncTop->getRateCtrl();
142  m_lastBPSEI = 0;
143  m_totalCoded = 0;
144 
145 }
146 
148 {
149  SEIActiveParameterSets *seiActiveParameterSets = new SEIActiveParameterSets();
150  seiActiveParameterSets->activeVPSId = m_pcCfg->getVPS()->getVPSId();
151  seiActiveParameterSets->m_selfContainedCvsFlag = false;
152  seiActiveParameterSets->m_noParameterSetUpdateFlag = false;
153  seiActiveParameterSets->numSpsIdsMinus1 = 0;
154  seiActiveParameterSets->activeSeqParameterSetId.resize(seiActiveParameterSets->numSpsIdsMinus1 + 1);
155  seiActiveParameterSets->activeSeqParameterSetId[0] = sps->getSPSId();
156  return seiActiveParameterSets;
157 }
158 
160 {
161  SEIFramePacking *seiFramePacking = new SEIFramePacking();
162  seiFramePacking->m_arrangementId = m_pcCfg->getFramePackingArrangementSEIId();
163  seiFramePacking->m_arrangementCancelFlag = 0;
164  seiFramePacking->m_arrangementType = m_pcCfg->getFramePackingArrangementSEIType();
165  assert((seiFramePacking->m_arrangementType > 2) && (seiFramePacking->m_arrangementType < 6) );
166  seiFramePacking->m_quincunxSamplingFlag = m_pcCfg->getFramePackingArrangementSEIQuincunx();
167  seiFramePacking->m_contentInterpretationType = m_pcCfg->getFramePackingArrangementSEIInterpretation();
168  seiFramePacking->m_spatialFlippingFlag = 0;
169  seiFramePacking->m_frame0FlippedFlag = 0;
170  seiFramePacking->m_fieldViewsFlag = (seiFramePacking->m_arrangementType == 2);
171  seiFramePacking->m_currentFrameIsFrame0Flag = ((seiFramePacking->m_arrangementType == 5) && (m_iNumPicCoded&1));
172  seiFramePacking->m_frame0SelfContainedFlag = 0;
173  seiFramePacking->m_frame1SelfContainedFlag = 0;
174  seiFramePacking->m_frame0GridPositionX = 0;
175  seiFramePacking->m_frame0GridPositionY = 0;
176  seiFramePacking->m_frame1GridPositionX = 0;
177  seiFramePacking->m_frame1GridPositionY = 0;
178  seiFramePacking->m_arrangementReservedByte = 0;
179  seiFramePacking->m_arrangementPersistenceFlag = true;
180  seiFramePacking->m_upsampledAspectRatio = 0;
181  return seiFramePacking;
182 }
183 
185 {
186  SEISegmentedRectFramePacking *seiSegmentedRectFramePacking = new SEISegmentedRectFramePacking();
187  seiSegmentedRectFramePacking->m_arrangementCancelFlag = m_pcCfg->getSegmentedRectFramePackingArrangementSEICancel();
188  seiSegmentedRectFramePacking->m_contentInterpretationType = m_pcCfg->getSegmentedRectFramePackingArrangementSEIType();
189  seiSegmentedRectFramePacking->m_arrangementPersistenceFlag = m_pcCfg->getSegmentedRectFramePackingArrangementSEIPersistence();
190  return seiSegmentedRectFramePacking;
191 }
192 
194 {
195  SEIDisplayOrientation *seiDisplayOrientation = new SEIDisplayOrientation();
196  seiDisplayOrientation->cancelFlag = false;
197  seiDisplayOrientation->horFlip = false;
198  seiDisplayOrientation->verFlip = false;
199  seiDisplayOrientation->anticlockwiseRotation = m_pcCfg->getDisplayOrientationSEIAngle();
200  return seiDisplayOrientation;
201 }
203 {
204  SEIToneMappingInfo *seiToneMappingInfo = new SEIToneMappingInfo();
205  seiToneMappingInfo->m_toneMapId = m_pcCfg->getTMISEIToneMapId();
206  seiToneMappingInfo->m_toneMapCancelFlag = m_pcCfg->getTMISEIToneMapCancelFlag();
207  seiToneMappingInfo->m_toneMapPersistenceFlag = m_pcCfg->getTMISEIToneMapPersistenceFlag();
208 
209  seiToneMappingInfo->m_codedDataBitDepth = m_pcCfg->getTMISEICodedDataBitDepth();
210  assert(seiToneMappingInfo->m_codedDataBitDepth >= 8 && seiToneMappingInfo->m_codedDataBitDepth <= 14);
211  seiToneMappingInfo->m_targetBitDepth = m_pcCfg->getTMISEITargetBitDepth();
212  assert(seiToneMappingInfo->m_targetBitDepth >= 1 && seiToneMappingInfo->m_targetBitDepth <= 17);
213  seiToneMappingInfo->m_modelId = m_pcCfg->getTMISEIModelID();
214  assert(seiToneMappingInfo->m_modelId >=0 &&seiToneMappingInfo->m_modelId<=4);
215 
216  switch( seiToneMappingInfo->m_modelId)
217  {
218  case 0:
219  {
220  seiToneMappingInfo->m_minValue = m_pcCfg->getTMISEIMinValue();
221  seiToneMappingInfo->m_maxValue = m_pcCfg->getTMISEIMaxValue();
222  break;
223  }
224  case 1:
225  {
226  seiToneMappingInfo->m_sigmoidMidpoint = m_pcCfg->getTMISEISigmoidMidpoint();
227  seiToneMappingInfo->m_sigmoidWidth = m_pcCfg->getTMISEISigmoidWidth();
228  break;
229  }
230  case 2:
231  {
232  UInt num = 1u<<(seiToneMappingInfo->m_targetBitDepth);
233  seiToneMappingInfo->m_startOfCodedInterval.resize(num);
234  Int* ptmp = m_pcCfg->getTMISEIStartOfCodedInterva();
235  if(ptmp)
236  {
237  for(Int i=0; i<num;i++)
238  {
239  seiToneMappingInfo->m_startOfCodedInterval[i] = ptmp[i];
240  }
241  }
242  break;
243  }
244  case 3:
245  {
246  seiToneMappingInfo->m_numPivots = m_pcCfg->getTMISEINumPivots();
247  seiToneMappingInfo->m_codedPivotValue.resize(seiToneMappingInfo->m_numPivots);
248  seiToneMappingInfo->m_targetPivotValue.resize(seiToneMappingInfo->m_numPivots);
249  Int* ptmpcoded = m_pcCfg->getTMISEICodedPivotValue();
250  Int* ptmptarget = m_pcCfg->getTMISEITargetPivotValue();
251  if(ptmpcoded&&ptmptarget)
252  {
253  for(Int i=0; i<(seiToneMappingInfo->m_numPivots);i++)
254  {
255  seiToneMappingInfo->m_codedPivotValue[i]=ptmpcoded[i];
256  seiToneMappingInfo->m_targetPivotValue[i]=ptmptarget[i];
257  }
258  }
259  break;
260  }
261  case 4:
262  {
263  seiToneMappingInfo->m_cameraIsoSpeedIdc = m_pcCfg->getTMISEICameraIsoSpeedIdc();
264  seiToneMappingInfo->m_cameraIsoSpeedValue = m_pcCfg->getTMISEICameraIsoSpeedValue();
265  assert( seiToneMappingInfo->m_cameraIsoSpeedValue !=0 );
266  seiToneMappingInfo->m_exposureIndexIdc = m_pcCfg->getTMISEIExposurIndexIdc();
267  seiToneMappingInfo->m_exposureIndexValue = m_pcCfg->getTMISEIExposurIndexValue();
268  assert( seiToneMappingInfo->m_exposureIndexValue !=0 );
269  seiToneMappingInfo->m_exposureCompensationValueSignFlag = m_pcCfg->getTMISEIExposureCompensationValueSignFlag();
270  seiToneMappingInfo->m_exposureCompensationValueNumerator = m_pcCfg->getTMISEIExposureCompensationValueNumerator();
271  seiToneMappingInfo->m_exposureCompensationValueDenomIdc = m_pcCfg->getTMISEIExposureCompensationValueDenomIdc();
272  seiToneMappingInfo->m_refScreenLuminanceWhite = m_pcCfg->getTMISEIRefScreenLuminanceWhite();
273  seiToneMappingInfo->m_extendedRangeWhiteLevel = m_pcCfg->getTMISEIExtendedRangeWhiteLevel();
274  assert( seiToneMappingInfo->m_extendedRangeWhiteLevel >= 100 );
275  seiToneMappingInfo->m_nominalBlackLevelLumaCodeValue = m_pcCfg->getTMISEINominalBlackLevelLumaCodeValue();
276  seiToneMappingInfo->m_nominalWhiteLevelLumaCodeValue = m_pcCfg->getTMISEINominalWhiteLevelLumaCodeValue();
277  assert( seiToneMappingInfo->m_nominalWhiteLevelLumaCodeValue > seiToneMappingInfo->m_nominalBlackLevelLumaCodeValue );
278  seiToneMappingInfo->m_extendedWhiteLevelLumaCodeValue = m_pcCfg->getTMISEIExtendedWhiteLevelLumaCodeValue();
279  assert( seiToneMappingInfo->m_extendedWhiteLevelLumaCodeValue >= seiToneMappingInfo->m_nominalWhiteLevelLumaCodeValue );
280  break;
281  }
282  default:
283  {
284  assert(!"Undefined SEIToneMapModelId");
285  break;
286  }
287  }
288  return seiToneMappingInfo;
289 }
290 
292 {
294  if(pps->getTilesEnabledFlag())
295  {
297  sei->m_each_tile_one_tile_set_flag = false;
298  sei->m_limited_tile_set_display_flag = false;
299  sei->setNumberOfTileSets((pps->getNumTileColumnsMinus1() + 1) * (pps->getNumTileRowsMinus1() + 1));
300 
301  for(Int i=0; i < sei->getNumberOfTileSets(); i++)
302  {
303  sei->tileSetData(i).m_mcts_id = i; //depends the application;
305 
306  for(Int j=0; j<sei->tileSetData(i).getNumberOfTileRects(); j++)
307  {
308  sei->tileSetData(i).topLeftTileIndex(j) = i+j;
309  sei->tileSetData(i).bottomRightTileIndex(j) = i+j;
310  }
311 
314  }
315  }
316  else
317  {
318  assert(!"Tile is not enabled");
319  }
320  return sei;
321 }
322 
324 {
325  SEIKneeFunctionInfo *seiKneeFunctionInfo = new SEIKneeFunctionInfo();
326  seiKneeFunctionInfo->m_kneeId = m_pcCfg->getKneeSEIId();
327  seiKneeFunctionInfo->m_kneeCancelFlag = m_pcCfg->getKneeSEICancelFlag();
328  if ( !seiKneeFunctionInfo->m_kneeCancelFlag )
329  {
330  seiKneeFunctionInfo->m_kneePersistenceFlag = m_pcCfg->getKneeSEIPersistenceFlag();
331  seiKneeFunctionInfo->m_kneeInputDrange = m_pcCfg->getKneeSEIInputDrange();
332  seiKneeFunctionInfo->m_kneeInputDispLuminance = m_pcCfg->getKneeSEIInputDispLuminance();
333  seiKneeFunctionInfo->m_kneeOutputDrange = m_pcCfg->getKneeSEIOutputDrange();
334  seiKneeFunctionInfo->m_kneeOutputDispLuminance = m_pcCfg->getKneeSEIOutputDispLuminance();
335 
336  seiKneeFunctionInfo->m_kneeNumKneePointsMinus1 = m_pcCfg->getKneeSEINumKneePointsMinus1();
337  Int* piInputKneePoint = m_pcCfg->getKneeSEIInputKneePoint();
338  Int* piOutputKneePoint = m_pcCfg->getKneeSEIOutputKneePoint();
339  if(piInputKneePoint&&piOutputKneePoint)
340  {
341  seiKneeFunctionInfo->m_kneeInputKneePoint.resize(seiKneeFunctionInfo->m_kneeNumKneePointsMinus1+1);
342  seiKneeFunctionInfo->m_kneeOutputKneePoint.resize(seiKneeFunctionInfo->m_kneeNumKneePointsMinus1+1);
343  for(Int i=0; i<=seiKneeFunctionInfo->m_kneeNumKneePointsMinus1; i++)
344  {
345  seiKneeFunctionInfo->m_kneeInputKneePoint[i] = piInputKneePoint[i];
346  seiKneeFunctionInfo->m_kneeOutputKneePoint[i] = piOutputKneePoint[i];
347  }
348  }
349  }
350  return seiKneeFunctionInfo;
351 }
352 
353 SEIChromaSamplingFilterHint* TEncGOP::xCreateSEIChromaSamplingFilterHint(Bool bChromaLocInfoPresent, Int iHorFilterIndex, Int iVerFilterIndex)
354 {
355  SEIChromaSamplingFilterHint *seiChromaSamplingFilterHint = new SEIChromaSamplingFilterHint();
356  seiChromaSamplingFilterHint->m_verChromaFilterIdc = iVerFilterIndex;
357  seiChromaSamplingFilterHint->m_horChromaFilterIdc = iHorFilterIndex;
358  seiChromaSamplingFilterHint->m_verFilteringProcessFlag = 1;
359  seiChromaSamplingFilterHint->m_targetFormatIdc = 3;
360  seiChromaSamplingFilterHint->m_perfectReconstructionFlag = false;
361  if(seiChromaSamplingFilterHint->m_verChromaFilterIdc == 1)
362  {
363  seiChromaSamplingFilterHint->m_numVerticalFilters = 1;
364  seiChromaSamplingFilterHint->m_verTapLengthMinus1 = (Int*)malloc(seiChromaSamplingFilterHint->m_numVerticalFilters * sizeof(Int));
365  seiChromaSamplingFilterHint->m_verFilterCoeff = (Int**)malloc(seiChromaSamplingFilterHint->m_numVerticalFilters * sizeof(Int*));
366  for(Int i = 0; i < seiChromaSamplingFilterHint->m_numVerticalFilters; i ++)
367  {
368  seiChromaSamplingFilterHint->m_verTapLengthMinus1[i] = 0;
369  seiChromaSamplingFilterHint->m_verFilterCoeff[i] = (Int*)malloc(seiChromaSamplingFilterHint->m_verTapLengthMinus1[i] * sizeof(Int));
370  for(Int j = 0; j < seiChromaSamplingFilterHint->m_verTapLengthMinus1[i]; j ++)
371  {
372  seiChromaSamplingFilterHint->m_verFilterCoeff[i][j] = 0;
373  }
374  }
375  }
376  else
377  {
378  seiChromaSamplingFilterHint->m_numVerticalFilters = 0;
379  seiChromaSamplingFilterHint->m_verTapLengthMinus1 = NULL;
380  seiChromaSamplingFilterHint->m_verFilterCoeff = NULL;
381  }
382  if(seiChromaSamplingFilterHint->m_horChromaFilterIdc == 1)
383  {
384  seiChromaSamplingFilterHint->m_numHorizontalFilters = 1;
385  seiChromaSamplingFilterHint->m_horTapLengthMinus1 = (Int*)malloc(seiChromaSamplingFilterHint->m_numHorizontalFilters * sizeof(Int));
386  seiChromaSamplingFilterHint->m_horFilterCoeff = (Int**)malloc(seiChromaSamplingFilterHint->m_numHorizontalFilters * sizeof(Int*));
387  for(Int i = 0; i < seiChromaSamplingFilterHint->m_numHorizontalFilters; i ++)
388  {
389  seiChromaSamplingFilterHint->m_horTapLengthMinus1[i] = 0;
390  seiChromaSamplingFilterHint->m_horFilterCoeff[i] = (Int*)malloc(seiChromaSamplingFilterHint->m_horTapLengthMinus1[i] * sizeof(Int));
391  for(Int j = 0; j < seiChromaSamplingFilterHint->m_horTapLengthMinus1[i]; j ++)
392  {
393  seiChromaSamplingFilterHint->m_horFilterCoeff[i][j] = 0;
394  }
395  }
396  }
397  else
398  {
399  seiChromaSamplingFilterHint->m_numHorizontalFilters = 0;
400  seiChromaSamplingFilterHint->m_horTapLengthMinus1 = NULL;
401  seiChromaSamplingFilterHint->m_horFilterCoeff = NULL;
402  }
403  return seiChromaSamplingFilterHint;
404 }
405 
406 Void TEncGOP::xCreateLeadingSEIMessages (/*SEIMessages seiMessages,*/ AccessUnit &accessUnit, const TComSPS *sps, const TComPPS *pps)
407 {
409 
410  if(m_pcCfg->getActiveParameterSetsSEIEnabled())
411  {
412  SEIActiveParameterSets *sei = xCreateSEIActiveParameterSets (sps);
413 
414  //nalu = NALUnit(NAL_UNIT_PREFIX_SEI);
415  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
416  m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps);
418  accessUnit.push_back(new NALUnitEBSP(nalu));
419  delete sei;
420  m_activeParameterSetSEIPresentInAU = true;
421  }
422 
423  if(m_pcCfg->getFramePackingArrangementSEIEnabled())
424  {
425  SEIFramePacking *sei = xCreateSEIFramePacking ();
426 
428  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
429  m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps);
431  accessUnit.push_back(new NALUnitEBSP(nalu));
432  delete sei;
433  }
434  if(m_pcCfg->getSegmentedRectFramePackingArrangementSEIEnabled())
435  {
436  SEISegmentedRectFramePacking *sei = xCreateSEISegmentedRectFramePacking ();
437 
439  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
440  m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps);
442  accessUnit.push_back(new NALUnitEBSP(nalu));
443  delete sei;
444  }
445  if (m_pcCfg->getDisplayOrientationSEIAngle())
446  {
447  SEIDisplayOrientation *sei = xCreateSEIDisplayOrientation();
448 
450  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
451  m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps);
453  accessUnit.push_back(new NALUnitEBSP(nalu));
454  delete sei;
455  }
456 
457  if(m_pcCfg->getToneMappingInfoSEIEnabled())
458  {
459  SEIToneMappingInfo *sei = xCreateSEIToneMappingInfo ();
460 
462  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
463  m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps);
465  accessUnit.push_back(new NALUnitEBSP(nalu));
466  delete sei;
467  }
468 
469  if(m_pcCfg->getTMCTSSEIEnabled())
470  {
471  SEITempMotionConstrainedTileSets *sei_tmcts = xCreateSEITempMotionConstrainedTileSets (pps);
472 
474  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
475  m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei_tmcts, sps);
477  accessUnit.push_back(new NALUnitEBSP(nalu));
478  delete sei_tmcts;
479  }
480 
481  if(m_pcCfg->getTimeCodeSEIEnabled())
482  {
483  SEITimeCode sei_time_code;
484  // Set data as per command line options
485  sei_time_code.numClockTs = m_pcCfg->getNumberOfTimesets();
486  for(Int i = 0; i < sei_time_code.numClockTs; i++)
487  {
488  sei_time_code.timeSetArray[i] = m_pcCfg->getTimeSet(i);
489  }
490 
492  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
493  m_seiWriter.writeSEImessage(nalu.m_Bitstream, sei_time_code, sps);
495  accessUnit.push_back(new NALUnitEBSP(nalu));
496  }
497 
498  if(m_pcCfg->getKneeSEIEnabled())
499  {
500  SEIKneeFunctionInfo *sei = xCreateSEIKneeFunctionInfo();
501 
503  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
504  m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps);
506  accessUnit.push_back(new NALUnitEBSP(nalu));
507  delete sei;
508  }
509 
510  if(m_pcCfg->getMasteringDisplaySEI().colourVolumeSEIEnabled)
511  {
512  const TComSEIMasteringDisplay &seiCfg=m_pcCfg->getMasteringDisplaySEI();
514  mdcv.values = seiCfg;
515 
517  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
518  m_seiWriter.writeSEImessage(nalu.m_Bitstream, mdcv, sps);
520  accessUnit.push_back(new NALUnitEBSP(nalu));
521 
522  }
523 }
524 
525 // ====================================================================================================================
526 // Public member functions
527 // ====================================================================================================================
528 Void TEncGOP::compressGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic,
529  TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP,
530  Bool isField, Bool isTff, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE )
531 {
532  // TODO: Split this function up.
533 
534  TComPic* pcPic = NULL;
535  TComPicYuv* pcPicYuvRecOut;
536  TComSlice* pcSlice;
537  TComOutputBitstream *pcBitstreamRedirect;
538  pcBitstreamRedirect = new TComOutputBitstream;
539  AccessUnit::iterator itLocationToPushSliceHeaderNALU; // used to store location where NALU containing slice header is to be inserted
540 
541  xInitGOP( iPOCLast, iNumPicRcvd, rcListPic, rcListPicYuvRecOut, isField );
542 
543  m_iNumPicCoded = 0;
544  SEIPictureTiming pictureTimingSEI;
545  Bool writeSOP = m_pcCfg->getSOPDescriptionSEIEnabled();
546 
547  // Initialize Scalable Nesting SEI with single layer values
548  SEIScalableNesting scalableNestingSEI;
549  scalableNestingSEI.m_bitStreamSubsetFlag = 1; // If the nested SEI messages are picture buffereing SEI mesages, picure timing SEI messages or sub-picture timing SEI messages, bitstream_subset_flag shall be equal to 1
550  scalableNestingSEI.m_nestingOpFlag = 0;
551  scalableNestingSEI.m_nestingNumOpsMinus1 = 0; //nesting_num_ops_minus1
552  scalableNestingSEI.m_allLayersFlag = 0;
553  scalableNestingSEI.m_nestingNoOpMaxTemporalIdPlus1 = 6 + 1; //nesting_no_op_max_temporal_id_plus1
554  scalableNestingSEI.m_nestingNumLayersMinus1 = 1 - 1; //nesting_num_layers_minus1
555  scalableNestingSEI.m_nestingLayerId[0] = 0;
556  scalableNestingSEI.m_callerOwnsSEIs = true;
557 
558  Int picSptDpbOutputDuDelay = 0;
559  UInt *accumBitsDU = NULL;
560  UInt *accumNalsDU = NULL;
561  SEIDecodingUnitInfo decodingUnitInfoSEI;
562 
563 #if EFFICIENT_FIELD_IRAP
564  Int IRAPGOPid = -1;
565  Bool IRAPtoReorder = false;
566  Bool swapIRAPForward = false;
567  if(isField)
568  {
569  Int pocCurr;
570  for ( Int iGOPid=0; iGOPid < m_iGopSize; iGOPid++ )
571  {
572  // determine actual POC
573  if(iPOCLast == 0) //case first frame or first top field
574  {
575  pocCurr=0;
576  }
577  else if(iPOCLast == 1 && isField) //case first bottom field, just like the first frame, the poc computation is not right anymore, we set the right value
578  {
579  pocCurr = 1;
580  }
581  else
582  {
583  pocCurr = iPOCLast - iNumPicRcvd + m_pcCfg->getGOPEntry(iGOPid).m_POC - isField;
584  }
585 
586  // check if POC corresponds to IRAP
587  NalUnitType tmpUnitType = getNalUnitType(pocCurr, m_iLastIDR, isField);
588  if(tmpUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && tmpUnitType <= NAL_UNIT_CODED_SLICE_CRA) // if picture is an IRAP
589  {
590  if(pocCurr%2 == 0 && iGOPid < m_iGopSize-1 && m_pcCfg->getGOPEntry(iGOPid).m_POC == m_pcCfg->getGOPEntry(iGOPid+1).m_POC-1)
591  { // if top field and following picture in enc order is associated bottom field
592  IRAPGOPid = iGOPid;
593  IRAPtoReorder = true;
594  swapIRAPForward = true;
595  break;
596  }
597  if(pocCurr%2 != 0 && iGOPid > 0 && m_pcCfg->getGOPEntry(iGOPid).m_POC == m_pcCfg->getGOPEntry(iGOPid-1).m_POC+1)
598  {
599  // if picture is an IRAP remember to process it first
600  IRAPGOPid = iGOPid;
601  IRAPtoReorder = true;
602  swapIRAPForward = false;
603  break;
604  }
605  }
606  }
607  }
608 #endif
609  // reset flag indicating whether pictures have been encoded
610  for ( Int iGOPid=0; iGOPid < m_iGopSize; iGOPid++ )
611  {
612  m_pcCfg->setEncodedFlag(iGOPid, false);
613  }
614 
615  for ( Int iGOPid=0; iGOPid < m_iGopSize; iGOPid++ )
616  {
617 #if EFFICIENT_FIELD_IRAP
618  if(IRAPtoReorder)
619  {
620  if(swapIRAPForward)
621  {
622  if(iGOPid == IRAPGOPid)
623  {
624  iGOPid = IRAPGOPid +1;
625  }
626  else if(iGOPid == IRAPGOPid +1)
627  {
628  iGOPid = IRAPGOPid;
629  }
630  }
631  else
632  {
633  if(iGOPid == IRAPGOPid -1)
634  {
635  iGOPid = IRAPGOPid;
636  }
637  else if(iGOPid == IRAPGOPid)
638  {
639  iGOPid = IRAPGOPid -1;
640  }
641  }
642  }
643 #endif
644 
645  UInt uiColDir = 1;
646  //-- For time output for each slice
647  clock_t iBeforeTime = clock();
648 
649  //select uiColDir
650  Int iCloseLeft=1, iCloseRight=-1;
651  for(Int i = 0; i<m_pcCfg->getGOPEntry(iGOPid).m_numRefPics; i++)
652  {
653  Int iRef = m_pcCfg->getGOPEntry(iGOPid).m_referencePics[i];
654  if(iRef>0&&(iRef<iCloseRight||iCloseRight==-1))
655  {
656  iCloseRight=iRef;
657  }
658  else if(iRef<0&&(iRef>iCloseLeft||iCloseLeft==1))
659  {
660  iCloseLeft=iRef;
661  }
662  }
663  if(iCloseRight>-1)
664  {
665  iCloseRight=iCloseRight+m_pcCfg->getGOPEntry(iGOPid).m_POC-1;
666  }
667  if(iCloseLeft<1)
668  {
669  iCloseLeft=iCloseLeft+m_pcCfg->getGOPEntry(iGOPid).m_POC-1;
670  while(iCloseLeft<0)
671  {
672  iCloseLeft+=m_iGopSize;
673  }
674  }
675  Int iLeftQP=0, iRightQP=0;
676  for(Int i=0; i<m_iGopSize; i++)
677  {
678  if(m_pcCfg->getGOPEntry(i).m_POC==(iCloseLeft%m_iGopSize)+1)
679  {
680  iLeftQP= m_pcCfg->getGOPEntry(i).m_QPOffset;
681  }
682  if (m_pcCfg->getGOPEntry(i).m_POC==(iCloseRight%m_iGopSize)+1)
683  {
684  iRightQP=m_pcCfg->getGOPEntry(i).m_QPOffset;
685  }
686  }
687  if(iCloseRight>-1&&iRightQP<iLeftQP)
688  {
689  uiColDir=0;
690  }
691 
693  Int iTimeOffset;
694  Int pocCurr;
695 
696  if(iPOCLast == 0) //case first frame or first top field
697  {
698  pocCurr=0;
699  iTimeOffset = 1;
700  }
701  else if(iPOCLast == 1 && isField) //case first bottom field, just like the first frame, the poc computation is not right anymore, we set the right value
702  {
703  pocCurr = 1;
704  iTimeOffset = 1;
705  }
706  else
707  {
708  pocCurr = iPOCLast - iNumPicRcvd + m_pcCfg->getGOPEntry(iGOPid).m_POC - ((isField && m_iGopSize>1) ? 1:0);
709  iTimeOffset = m_pcCfg->getGOPEntry(iGOPid).m_POC;
710  }
711 
712  if(pocCurr>=m_pcCfg->getFramesToBeEncoded())
713  {
714 #if EFFICIENT_FIELD_IRAP
715  if(IRAPtoReorder)
716  {
717  if(swapIRAPForward)
718  {
719  if(iGOPid == IRAPGOPid)
720  {
721  iGOPid = IRAPGOPid +1;
722  IRAPtoReorder = false;
723  }
724  else if(iGOPid == IRAPGOPid +1)
725  {
726  iGOPid --;
727  }
728  }
729  else
730  {
731  if(iGOPid == IRAPGOPid)
732  {
733  iGOPid = IRAPGOPid -1;
734  }
735  else if(iGOPid == IRAPGOPid -1)
736  {
737  iGOPid = IRAPGOPid;
738  IRAPtoReorder = false;
739  }
740  }
741  }
742 #endif
743  continue;
744  }
745 
746  if( getNalUnitType(pocCurr, m_iLastIDR, isField) == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType(pocCurr, m_iLastIDR, isField) == NAL_UNIT_CODED_SLICE_IDR_N_LP )
747  {
748  m_iLastIDR = pocCurr;
749  }
750  // start a new access unit: create an entry in the list of output access units
751  accessUnitsInGOP.push_back(AccessUnit());
752  AccessUnit& accessUnit = accessUnitsInGOP.back();
753  xGetBuffer( rcListPic, rcListPicYuvRecOut, iNumPicRcvd, iTimeOffset, pcPic, pcPicYuvRecOut, pocCurr, isField );
754 
755  // Slice data initialization
756  pcPic->clearSliceBuffer();
757  pcPic->allocateNewSlice();
758  m_pcSliceEncoder->setSliceIdx(0);
759  pcPic->setCurrSliceIdx(0);
760 
761  m_pcSliceEncoder->initEncSlice ( pcPic, iPOCLast, pocCurr, iNumPicRcvd, iGOPid, pcSlice, &(pcPic->getPicSym()->getSPS()), &(pcPic->getPicSym()->getPPS()), isField );
762 
763  //Set Frame/Field coding
764  pcSlice->getPic()->setField(isField);
765 
766  pcSlice->setLastIDR(m_iLastIDR);
767  pcSlice->setSliceIdx(0);
768  //set default slice level flag to the same as SPS level flag
770 
771  if(pcSlice->getSliceType()==B_SLICE&&m_pcCfg->getGOPEntry(iGOPid).m_sliceType=='P')
772  {
773  pcSlice->setSliceType(P_SLICE);
774  }
775  if(pcSlice->getSliceType()==B_SLICE&&m_pcCfg->getGOPEntry(iGOPid).m_sliceType=='I')
776  {
777  pcSlice->setSliceType(I_SLICE);
778  }
779 
780  // Set the nal unit type
781  pcSlice->setNalUnitType(getNalUnitType(pocCurr, m_iLastIDR, isField));
782  if(pcSlice->getTemporalLayerNonReferenceFlag())
783  {
784  if (pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_R &&
785  !(m_iGopSize == 1 && pcSlice->getSliceType() == I_SLICE))
786  // Add this condition to avoid POC issues with encoder_intra_main.cfg configuration (see #1127 in bug tracker)
787  {
789  }
791  {
793  }
795  {
797  }
798  }
799 
800 #if EFFICIENT_FIELD_IRAP
806  || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // IRAP picture
807  {
808  m_associatedIRAPType = pcSlice->getNalUnitType();
809  m_associatedIRAPPOC = pocCurr;
810  }
811  pcSlice->setAssociatedIRAPType(m_associatedIRAPType);
812  pcSlice->setAssociatedIRAPPOC(m_associatedIRAPPOC);
813 #endif
814  // Do decoding refresh marking if any
815  pcSlice->decodingRefreshMarking(m_pocCRA, m_bRefreshPending, rcListPic);
816  m_pcEncTop->selectReferencePictureSet(pcSlice, pocCurr, iGOPid);
817  pcSlice->getRPS()->setNumberOfLongtermPictures(0);
818 #if !EFFICIENT_FIELD_IRAP
824  || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // IRAP picture
825  {
826  m_associatedIRAPType = pcSlice->getNalUnitType();
827  m_associatedIRAPPOC = pocCurr;
828  }
829  pcSlice->setAssociatedIRAPType(m_associatedIRAPType);
830  pcSlice->setAssociatedIRAPPOC(m_associatedIRAPPOC);
831 #endif
832 
833 #if ALLOW_RECOVERY_POINT_AS_RAP
834  if ((pcSlice->checkThatAllRefPicsAreAvailable(rcListPic, pcSlice->getRPS(), false, m_iLastRecoveryPicPOC, m_pcCfg->getDecodingRefreshType() == 3) != 0) || (pcSlice->isIRAP())
836  || (isField && pcSlice->getAssociatedIRAPType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getAssociatedIRAPType() <= NAL_UNIT_CODED_SLICE_CRA && pcSlice->getAssociatedIRAPPOC() == pcSlice->getPOC()+1)
837 #endif
838  )
839  {
840  pcSlice->createExplicitReferencePictureSetFromReference(rcListPic, pcSlice->getRPS(), pcSlice->isIRAP(), m_iLastRecoveryPicPOC, m_pcCfg->getDecodingRefreshType() == 3);
841  }
842 #else
843  if ((pcSlice->checkThatAllRefPicsAreAvailable(rcListPic, pcSlice->getRPS(), false) != 0) || (pcSlice->isIRAP()))
844  {
845  pcSlice->createExplicitReferencePictureSetFromReference(rcListPic, pcSlice->getRPS(), pcSlice->isIRAP());
846  }
847 #endif
848 
849  pcSlice->applyReferencePictureSet(rcListPic, pcSlice->getRPS());
850 
851  if(pcSlice->getTLayer() > 0
852  && !( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N // Check if not a leading picture
856  )
857  {
858  if(pcSlice->isTemporalLayerSwitchingPoint(rcListPic) || pcSlice->getSPS()->getTemporalIdNestingFlag())
859  {
860  if(pcSlice->getTemporalLayerNonReferenceFlag())
861  {
863  }
864  else
865  {
867  }
868  }
869  else if(pcSlice->isStepwiseTemporalLayerSwitchingPointCandidate(rcListPic))
870  {
871  Bool isSTSA=true;
872  for(Int ii=iGOPid+1;(ii<m_pcCfg->getGOPSize() && isSTSA==true);ii++)
873  {
874  Int lTid= m_pcCfg->getGOPEntry(ii).m_temporalId;
875  if(lTid==pcSlice->getTLayer())
876  {
877  const TComReferencePictureSet* nRPS = pcSlice->getSPS()->getRPSList()->getReferencePictureSet(ii);
878  for(Int jj=0;jj<nRPS->getNumberOfPictures();jj++)
879  {
880  if(nRPS->getUsed(jj))
881  {
882  Int tPoc=m_pcCfg->getGOPEntry(ii).m_POC+nRPS->getDeltaPOC(jj);
883  Int kk=0;
884  for(kk=0;kk<m_pcCfg->getGOPSize();kk++)
885  {
886  if(m_pcCfg->getGOPEntry(kk).m_POC==tPoc)
887  {
888  break;
889  }
890  }
891  Int tTid=m_pcCfg->getGOPEntry(kk).m_temporalId;
892  if(tTid >= pcSlice->getTLayer())
893  {
894  isSTSA=false;
895  break;
896  }
897  }
898  }
899  }
900  }
901  if(isSTSA==true)
902  {
903  if(pcSlice->getTemporalLayerNonReferenceFlag())
904  {
906  }
907  else
908  {
910  }
911  }
912  }
913  }
914  arrangeLongtermPicturesInRPS(pcSlice, rcListPic);
915  TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
916  refPicListModification->setRefPicListModificationFlagL0(0);
917  refPicListModification->setRefPicListModificationFlagL1(0);
918  pcSlice->setNumRefIdx(REF_PIC_LIST_0,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures()));
919  pcSlice->setNumRefIdx(REF_PIC_LIST_1,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures()));
920 
921 #if ADAPTIVE_QP_SELECTION
922  pcSlice->setTrQuant( m_pcEncTop->getTrQuant() );
923 #endif
924 
925  // Set reference list
926  pcSlice->setRefPicList ( rcListPic );
927 
928  // Slice info. refinement
929  if ( (pcSlice->getSliceType() == B_SLICE) && (pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0) )
930  {
931  pcSlice->setSliceType ( P_SLICE );
932  }
933  pcSlice->setEncCABACTableIdx(m_pcSliceEncoder->getEncCABACTableIdx());
934 
935  if (pcSlice->getSliceType() == B_SLICE)
936  {
937  pcSlice->setColFromL0Flag(1-uiColDir);
938  Bool bLowDelay = true;
939  Int iCurrPOC = pcSlice->getPOC();
940  Int iRefIdx = 0;
941 
942  for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
943  {
944  if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
945  {
946  bLowDelay = false;
947  }
948  }
949  for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
950  {
951  if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
952  {
953  bLowDelay = false;
954  }
955  }
956 
957  pcSlice->setCheckLDC(bLowDelay);
958  }
959  else
960  {
961  pcSlice->setCheckLDC(true);
962  }
963 
964  uiColDir = 1-uiColDir;
965 
966  //-------------------------------------------------------------
967  pcSlice->setRefPOCList();
968 
969  pcSlice->setList1IdxToList0Idx();
970 
971  if (m_pcEncTop->getTMVPModeId() == 2)
972  {
973  if (iGOPid == 0) // first picture in SOP (i.e. forward B)
974  {
975  pcSlice->setEnableTMVPFlag(0);
976  }
977  else
978  {
979  // Note: pcSlice->getColFromL0Flag() is assumed to be always 0 and getcolRefIdx() is always 0.
980  pcSlice->setEnableTMVPFlag(1);
981  }
982  }
983  else if (m_pcEncTop->getTMVPModeId() == 1)
984  {
985  pcSlice->setEnableTMVPFlag(1);
986  }
987  else
988  {
989  pcSlice->setEnableTMVPFlag(0);
990  }
992  // Slice compression
993  if (m_pcCfg->getUseASR())
994  {
995  m_pcSliceEncoder->setSearchRange(pcSlice);
996  }
997 
998  Bool bGPBcheck=false;
999  if ( pcSlice->getSliceType() == B_SLICE)
1000  {
1001  if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
1002  {
1003  bGPBcheck=true;
1004  Int i;
1005  for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
1006  {
1007  if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) )
1008  {
1009  bGPBcheck=false;
1010  break;
1011  }
1012  }
1013  }
1014  }
1015  if(bGPBcheck)
1016  {
1017  pcSlice->setMvdL1ZeroFlag(true);
1018  }
1019  else
1020  {
1021  pcSlice->setMvdL1ZeroFlag(false);
1022  }
1023  pcPic->getSlice(pcSlice->getSliceIdx())->setMvdL1ZeroFlag(pcSlice->getMvdL1ZeroFlag());
1024 
1025 
1026  Double lambda = 0.0;
1027  Int actualHeadBits = 0;
1028  Int actualTotalBits = 0;
1029  Int estimatedBits = 0;
1030  Int tmpBitsBeforeWriting = 0;
1031  if ( m_pcCfg->getUseRateCtrl() )
1032  {
1033  Int frameLevel = m_pcRateCtrl->getRCSeq()->getGOPID2Level( iGOPid );
1034  if ( pcPic->getSlice(0)->getSliceType() == I_SLICE )
1035  {
1036  frameLevel = 0;
1037  }
1038  m_pcRateCtrl->initRCPic( frameLevel );
1039  estimatedBits = m_pcRateCtrl->getRCPic()->getTargetBits();
1040 
1041  Int sliceQP = m_pcCfg->getInitialQP();
1042  if ( ( pcSlice->getPOC() == 0 && m_pcCfg->getInitialQP() > 0 ) || ( frameLevel == 0 && m_pcCfg->getForceIntraQP() ) ) // QP is specified
1043  {
1044  Int NumberBFrames = ( m_pcCfg->getGOPSize() - 1 );
1045  Double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(Double)NumberBFrames );
1046  Double dQPFactor = 0.57*dLambda_scale;
1047  Int SHIFT_QP = 12;
1048  Int bitdepth_luma_qp_scale = 0;
1049  Double qp_temp = (Double) sliceQP + bitdepth_luma_qp_scale - SHIFT_QP;
1050  lambda = dQPFactor*pow( 2.0, qp_temp/3.0 );
1051  }
1052  else if ( frameLevel == 0 ) // intra case, but use the model
1053  {
1054  m_pcSliceEncoder->calCostSliceI(pcPic);
1055 
1056  if ( m_pcCfg->getIntraPeriod() != 1 ) // do not refine allocated bits for all intra case
1057  {
1058  Int bits = m_pcRateCtrl->getRCSeq()->getLeftAverageBits();
1059  bits = m_pcRateCtrl->getRCPic()->getRefineBitsForIntra( bits );
1060  if ( bits < 200 )
1061  {
1062  bits = 200;
1063  }
1064  m_pcRateCtrl->getRCPic()->setTargetBits( bits );
1065  }
1066 
1067  list<TEncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList();
1068  m_pcRateCtrl->getRCPic()->getLCUInitTargetBits();
1069  lambda = m_pcRateCtrl->getRCPic()->estimatePicLambda( listPreviousPicture, pcSlice->getSliceType());
1070  sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture );
1071  }
1072  else // normal case
1073  {
1074  list<TEncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList();
1075  lambda = m_pcRateCtrl->getRCPic()->estimatePicLambda( listPreviousPicture, pcSlice->getSliceType());
1076  sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture );
1077  }
1078 
1079  sliceQP = Clip3( -pcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, sliceQP );
1080  m_pcRateCtrl->getRCPic()->setPicEstQP( sliceQP );
1081 
1082  m_pcSliceEncoder->resetQP( pcPic, sliceQP, lambda );
1083  }
1084 
1085  UInt uiNumSliceSegments = 1;
1086 
1087  // Allocate some coders, now the number of tiles are known.
1088  const Int numSubstreamsColumns = (pcSlice->getPPS()->getNumTileColumnsMinus1() + 1);
1089  const Int numSubstreamRows = pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag() ? pcPic->getFrameHeightInCtus() : (pcSlice->getPPS()->getNumTileRowsMinus1() + 1);
1090  const Int numSubstreams = numSubstreamRows * numSubstreamsColumns;
1091  std::vector<TComOutputBitstream> substreamsOut(numSubstreams);
1092 
1093  // now compress (trial encode) the various slice segments (slices, and dependent slices)
1094  {
1095  const UInt numberOfCtusInFrame=pcPic->getPicSym()->getNumberOfCtusInFrame();
1096  pcSlice->setSliceCurStartCtuTsAddr( 0 );
1097  pcSlice->setSliceSegmentCurStartCtuTsAddr( 0 );
1098 
1099  for(UInt nextCtuTsAddr = 0; nextCtuTsAddr < numberOfCtusInFrame; )
1100  {
1101  m_pcSliceEncoder->precompressSlice( pcPic );
1102  m_pcSliceEncoder->compressSlice ( pcPic );
1103 
1104  const UInt curSliceSegmentEnd = pcSlice->getSliceSegmentCurEndCtuTsAddr();
1105  if (curSliceSegmentEnd < numberOfCtusInFrame)
1106  {
1107  const Bool bNextSegmentIsDependentSlice=curSliceSegmentEnd<pcSlice->getSliceCurEndCtuTsAddr();
1108  const UInt sliceBits=pcSlice->getSliceBits();
1109  pcPic->allocateNewSlice();
1110  // prepare for next slice
1111  pcPic->setCurrSliceIdx ( uiNumSliceSegments );
1112  m_pcSliceEncoder->setSliceIdx ( uiNumSliceSegments );
1113  pcSlice = pcPic->getSlice ( uiNumSliceSegments );
1114  assert(pcSlice->getPPS()!=0);
1115  pcSlice->copySliceInfo ( pcPic->getSlice(uiNumSliceSegments-1) );
1116  pcSlice->setSliceIdx ( uiNumSliceSegments );
1117  if (bNextSegmentIsDependentSlice)
1118  {
1119  pcSlice->setSliceBits(sliceBits);
1120  }
1121  else
1122  {
1123  pcSlice->setSliceCurStartCtuTsAddr ( curSliceSegmentEnd );
1124  pcSlice->setSliceBits(0);
1125  }
1126  pcSlice->setDependentSliceSegmentFlag(bNextSegmentIsDependentSlice);
1127  pcSlice->setSliceSegmentCurStartCtuTsAddr ( curSliceSegmentEnd );
1128  // TODO: optimise cabac_init during compress slice to improve multi-slice operation
1129  // pcSlice->setEncCABACTableIdx(m_pcSliceEncoder->getEncCABACTableIdx());
1130  uiNumSliceSegments ++;
1131  }
1132  nextCtuTsAddr = curSliceSegmentEnd;
1133  }
1134  }
1135 
1136  pcSlice = pcPic->getSlice(0);
1137 
1138  // SAO parameter estimation using non-deblocked pixels for CTU bottom and right boundary areas
1139  if( pcSlice->getSPS()->getUseSAO() && m_pcCfg->getSaoCtuBoundary() )
1140  {
1141  m_pcSAO->getPreDBFStatistics(pcPic);
1142  }
1143 
1144  //-- Loop filter
1145  Bool bLFCrossTileBoundary = pcSlice->getPPS()->getLoopFilterAcrossTilesEnabledFlag();
1146  m_pcLoopFilter->setCfg(bLFCrossTileBoundary);
1147  if ( m_pcCfg->getDeblockingFilterMetric() )
1148  {
1149  applyDeblockingFilterMetric(pcPic, uiNumSliceSegments);
1150  }
1151  m_pcLoopFilter->loopFilterPic( pcPic );
1152 
1154  // Set entropy coder
1155  m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice );
1156 
1157  // write various header sets.
1158  if ( m_bSeqFirst )
1159  {
1161  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1162  m_pcEntropyCoder->encodeVPS(m_pcEncTop->getVPS());
1164  accessUnit.push_back(new NALUnitEBSP(nalu));
1165  actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8;
1166 
1167  nalu = NALUnit(NAL_UNIT_SPS);
1168  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1169  m_pcEntropyCoder->encodeSPS(pcSlice->getSPS());
1170  writeRBSPTrailingBits(nalu.m_Bitstream);
1171  accessUnit.push_back(new NALUnitEBSP(nalu));
1172  actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8;
1173 
1174  nalu = NALUnit(NAL_UNIT_PPS);
1175  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1176  m_pcEntropyCoder->encodePPS(pcSlice->getPPS());
1177  writeRBSPTrailingBits(nalu.m_Bitstream);
1178  accessUnit.push_back(new NALUnitEBSP(nalu));
1179  actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8;
1180 
1181  xCreateLeadingSEIMessages(accessUnit, pcSlice->getSPS(), pcSlice->getPPS());
1182 
1183  m_bSeqFirst = false;
1184  }
1185 
1186  if (writeSOP) // write SOP description SEI (if enabled) at the beginning of GOP
1187  {
1188  Int SOPcurrPOC = pocCurr;
1189 
1191  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1192  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1193 
1194  SEISOPDescription SOPDescriptionSEI;
1195  SOPDescriptionSEI.m_sopSeqParameterSetId = pcSlice->getSPS()->getSPSId();
1196 
1197  UInt i = 0;
1198  UInt prevEntryId = iGOPid;
1199  for (Int j = iGOPid; j < m_iGopSize; j++)
1200  {
1201  Int deltaPOC = m_pcCfg->getGOPEntry(j).m_POC - m_pcCfg->getGOPEntry(prevEntryId).m_POC;
1202  if ((SOPcurrPOC + deltaPOC) < m_pcCfg->getFramesToBeEncoded())
1203  {
1204  SOPcurrPOC += deltaPOC;
1205  SOPDescriptionSEI.m_sopDescVclNaluType[i] = getNalUnitType(SOPcurrPOC, m_iLastIDR, isField);
1206  SOPDescriptionSEI.m_sopDescTemporalId[i] = m_pcCfg->getGOPEntry(j).m_temporalId;
1207  SOPDescriptionSEI.m_sopDescStRpsIdx[i] = m_pcEncTop->getReferencePictureSetIdxForSOP(pcSlice, SOPcurrPOC, j);
1208  SOPDescriptionSEI.m_sopDescPocDelta[i] = deltaPOC;
1209 
1210  prevEntryId = j;
1211  i++;
1212  }
1213  }
1214 
1215  SOPDescriptionSEI.m_numPicsInSopMinus1 = i - 1;
1216 
1217  m_seiWriter.writeSEImessage( nalu.m_Bitstream, SOPDescriptionSEI, pcSlice->getSPS());
1219  accessUnit.push_back(new NALUnitEBSP(nalu));
1220 
1221  writeSOP = false;
1222  }
1223 
1224  if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) &&
1225  ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) &&
1228  {
1230  {
1231  UInt numDU = pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNumDU();
1232  pictureTimingSEI.m_numDecodingUnitsMinus1 = ( numDU - 1 );
1233  pictureTimingSEI.m_duCommonCpbRemovalDelayFlag = false;
1234 
1235  if( pictureTimingSEI.m_numNalusInDuMinus1 == NULL )
1236  {
1237  pictureTimingSEI.m_numNalusInDuMinus1 = new UInt[ numDU ];
1238  }
1239  if( pictureTimingSEI.m_duCpbRemovalDelayMinus1 == NULL )
1240  {
1241  pictureTimingSEI.m_duCpbRemovalDelayMinus1 = new UInt[ numDU ];
1242  }
1243  if( accumBitsDU == NULL )
1244  {
1245  accumBitsDU = new UInt[ numDU ];
1246  }
1247  if( accumNalsDU == NULL )
1248  {
1249  accumNalsDU = new UInt[ numDU ];
1250  }
1251  }
1252  pictureTimingSEI.m_auCpbRemovalDelay = std::min<Int>(std::max<Int>(1, m_totalCoded - m_lastBPSEI), static_cast<Int>(pow(2, static_cast<Double>(pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getCpbRemovalDelayLengthMinus1()+1)))); // Syntax element signalled as minus, hence the .
1253  pictureTimingSEI.m_picDpbOutputDelay = pcSlice->getSPS()->getNumReorderPics(pcSlice->getSPS()->getMaxTLayers()-1) + pcSlice->getPOC() - m_totalCoded;
1254 #if EFFICIENT_FIELD_IRAP
1255  if(IRAPGOPid > 0 && IRAPGOPid < m_iGopSize)
1256  {
1257  // if pictures have been swapped there is likely one more picture delay on their tid. Very rough approximation
1258  pictureTimingSEI.m_picDpbOutputDelay ++;
1259  }
1260 #endif
1261  Int factor = pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getTickDivisorMinus2() + 2;
1262  pictureTimingSEI.m_picDpbOutputDuDelay = factor * pictureTimingSEI.m_picDpbOutputDelay;
1263  if( m_pcCfg->getDecodingUnitInfoSEIEnabled() )
1264  {
1265  picSptDpbOutputDuDelay = factor * pictureTimingSEI.m_picDpbOutputDelay;
1266  }
1267  }
1268 
1269  if( ( m_pcCfg->getBufferingPeriodSEIEnabled() ) && ( pcSlice->getSliceType() == I_SLICE ) &&
1270  ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) &&
1273  {
1275  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1276  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1277 
1278  SEIBufferingPeriod sei_buffering_period;
1279 
1280  UInt uiInitialCpbRemovalDelay = (90000/2); // 0.5 sec
1281  sei_buffering_period.m_initialCpbRemovalDelay [0][0] = uiInitialCpbRemovalDelay;
1282  sei_buffering_period.m_initialCpbRemovalDelayOffset[0][0] = uiInitialCpbRemovalDelay;
1283  sei_buffering_period.m_initialCpbRemovalDelay [0][1] = uiInitialCpbRemovalDelay;
1284  sei_buffering_period.m_initialCpbRemovalDelayOffset[0][1] = uiInitialCpbRemovalDelay;
1285 
1287 
1288  UInt uiTmp = (UInt)( dTmp * 90000.0 );
1289  uiInitialCpbRemovalDelay -= uiTmp;
1290  uiInitialCpbRemovalDelay -= uiTmp / ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getTickDivisorMinus2() + 2 );
1291  sei_buffering_period.m_initialAltCpbRemovalDelay [0][0] = uiInitialCpbRemovalDelay;
1292  sei_buffering_period.m_initialAltCpbRemovalDelayOffset[0][0] = uiInitialCpbRemovalDelay;
1293  sei_buffering_period.m_initialAltCpbRemovalDelay [0][1] = uiInitialCpbRemovalDelay;
1294  sei_buffering_period.m_initialAltCpbRemovalDelayOffset[0][1] = uiInitialCpbRemovalDelay;
1295 
1296  sei_buffering_period.m_rapCpbParamsPresentFlag = 0;
1297  //for the concatenation, it can be set to one during splicing.
1298  sei_buffering_period.m_concatenationFlag = 0;
1299  //since the temporal layer HRD is not ready, we assumed it is fixed
1300  sei_buffering_period.m_auCpbRemovalDelayDelta = 1;
1301 
1302  sei_buffering_period.m_cpbDelayOffset = 0;
1303  sei_buffering_period.m_dpbDelayOffset = 0;
1304 
1305  m_seiWriter.writeSEImessage( nalu.m_Bitstream, sei_buffering_period, pcSlice->getSPS());
1307 
1308  {
1309  UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit);
1310  UInt offsetPosition = m_activeParameterSetSEIPresentInAU; // Insert BP SEI after APS SEI
1311  AccessUnit::iterator it = accessUnit.begin();
1312  for(Int j = 0; j < seiPositionInAu + offsetPosition; j++)
1313  {
1314  it++;
1315  }
1316  accessUnit.insert(it, new NALUnitEBSP(nalu));
1317  m_bufferingPeriodSEIPresentInAU = true;
1318  }
1319 
1320  if (m_pcCfg->getScalableNestingSEIEnabled())
1321  {
1323  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1324  m_pcEntropyCoder->setBitstream(&naluTmp.m_Bitstream);
1325  scalableNestingSEI.m_nestedSEIs.clear();
1326  scalableNestingSEI.m_nestedSEIs.push_back(&sei_buffering_period);
1327  m_seiWriter.writeSEImessage( naluTmp.m_Bitstream, scalableNestingSEI, pcSlice->getSPS());
1329  UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit);
1330  UInt offsetPosition = m_activeParameterSetSEIPresentInAU + m_bufferingPeriodSEIPresentInAU + m_pictureTimingSEIPresentInAU; // Insert BP SEI after non-nested APS, BP and PT SEIs
1331  AccessUnit::iterator it = accessUnit.begin();
1332  for(Int j = 0; j < seiPositionInAu + offsetPosition; j++)
1333  {
1334  it++;
1335  }
1336  accessUnit.insert(it, new NALUnitEBSP(naluTmp));
1337  m_nestedBufferingPeriodSEIPresentInAU = true;
1338  }
1339 
1340  m_lastBPSEI = m_totalCoded;
1341  m_cpbRemovalDelay = 0;
1342  }
1343  m_cpbRemovalDelay ++;
1344 
1345  if(pcSlice->getSPS()->getVuiParametersPresentFlag() && m_pcCfg->getChromaSamplingFilterHintEnabled() && ( pcSlice->getSliceType() == I_SLICE ))
1346  {
1347  SEIChromaSamplingFilterHint *seiChromaSamplingFilterHint = xCreateSEIChromaSamplingFilterHint(m_pcCfg->getChromaLocInfoPresentFlag(), m_pcCfg->getChromaSamplingHorFilterIdc(), m_pcCfg->getChromaSamplingVerFilterIdc());
1348 
1350  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1351  m_pcEntropyCoder->setBitstream(&naluTmp.m_Bitstream);
1352  m_seiWriter.writeSEImessage(naluTmp.m_Bitstream, *seiChromaSamplingFilterHint, pcSlice->getSPS());
1353  writeRBSPTrailingBits(naluTmp.m_Bitstream);
1354  accessUnit.push_back(new NALUnitEBSP(naluTmp));
1355  delete seiChromaSamplingFilterHint;
1356  }
1357 
1358  if( ( m_pcEncTop->getRecoveryPointSEIEnabled() ) && ( pcSlice->getSliceType() == I_SLICE ) )
1359  {
1360  if( m_pcEncTop->getGradualDecodingRefreshInfoEnabled() && !pcSlice->getRapPicFlag() )
1361  {
1362  // Gradual decoding refresh SEI
1364  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1365  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1366 
1367  SEIGradualDecodingRefreshInfo seiGradualDecodingRefreshInfo;
1368  seiGradualDecodingRefreshInfo.m_gdrForegroundFlag = true; // Indicating all "foreground"
1369 
1370  m_seiWriter.writeSEImessage( nalu.m_Bitstream, seiGradualDecodingRefreshInfo, pcSlice->getSPS() );
1372  accessUnit.push_back(new NALUnitEBSP(nalu));
1373  }
1374  // Recovery point SEI
1376  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1377  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1378 
1379  SEIRecoveryPoint sei_recovery_point;
1380  sei_recovery_point.m_recoveryPocCnt = 0;
1381  sei_recovery_point.m_exactMatchingFlag = ( pcSlice->getPOC() == 0 ) ? (true) : (false);
1382  sei_recovery_point.m_brokenLinkFlag = false;
1383 #if ALLOW_RECOVERY_POINT_AS_RAP
1384  if(m_pcCfg->getDecodingRefreshType() == 3)
1385  {
1386  m_iLastRecoveryPicPOC = pocCurr;
1387  }
1388 #endif
1389 
1390  m_seiWriter.writeSEImessage( nalu.m_Bitstream, sei_recovery_point, pcSlice->getSPS() );
1392  accessUnit.push_back(new NALUnitEBSP(nalu));
1393  }
1394 
1395  if( m_pcEncTop->getNoDisplaySEITLayer() )
1396  {
1397  if( pcSlice->getTLayer() >= m_pcEncTop->getNoDisplaySEITLayer() )
1398  {
1399  // No display SEI
1401  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1402  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1403 
1404  SEINoDisplay seiNoDisplay;
1405  seiNoDisplay.m_noDisplay = true;
1406 
1407  m_seiWriter.writeSEImessage( nalu.m_Bitstream, seiNoDisplay, pcSlice->getSPS() );
1409  accessUnit.push_back(new NALUnitEBSP(nalu));
1410  }
1411  }
1412 
1413  /* use the main bitstream buffer for storing the marshalled picture */
1414  m_pcEntropyCoder->setBitstream(NULL);
1415 
1416  pcSlice = pcPic->getSlice(0);
1417 
1418  if (pcSlice->getSPS()->getUseSAO())
1419  {
1420  Bool sliceEnabled[MAX_NUM_COMPONENT];
1421  TComBitCounter tempBitCounter;
1422  tempBitCounter.resetBits();
1423  m_pcEncTop->getRDGoOnSbacCoder()->setBitstream(&tempBitCounter);
1424  m_pcSAO->initRDOCabacCoder(m_pcEncTop->getRDGoOnSbacCoder(), pcSlice);
1425  m_pcSAO->SAOProcess(pcPic, sliceEnabled, pcPic->getSlice(0)->getLambdas()
1426 #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK
1427  , m_pcCfg->getSaoCtuBoundary()
1428 #endif
1429  );
1430  m_pcSAO->PCMLFDisableProcess(pcPic);
1431  m_pcEncTop->getRDGoOnSbacCoder()->setBitstream(NULL);
1432 
1433  //assign SAO slice header
1434  for(Int s=0; s< uiNumSliceSegments; s++)
1435  {
1436  pcPic->getSlice(s)->setSaoEnabledFlag(CHANNEL_TYPE_LUMA, sliceEnabled[COMPONENT_Y]);
1437  assert(sliceEnabled[COMPONENT_Cb] == sliceEnabled[COMPONENT_Cr]);
1438  pcPic->getSlice(s)->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, sliceEnabled[COMPONENT_Cb]);
1439  }
1440  }
1441 
1442  // pcSlice is currently slice 0.
1443  std::size_t binCountsInNalUnits = 0; // For implementation of cabac_zero_word stuffing (section 7.4.3.10)
1444  std::size_t numBytesInVclNalUnits = 0; // For implementation of cabac_zero_word stuffing (section 7.4.3.10)
1445 
1446  for( UInt sliceSegmentStartCtuTsAddr = 0, sliceIdxCount=0; sliceSegmentStartCtuTsAddr < pcPic->getPicSym()->getNumberOfCtusInFrame(); sliceIdxCount++, sliceSegmentStartCtuTsAddr=pcSlice->getSliceSegmentCurEndCtuTsAddr() )
1447  {
1448  pcSlice = pcPic->getSlice(sliceIdxCount);
1449  if(sliceIdxCount > 0 && pcSlice->getSliceType()!= I_SLICE)
1450  {
1451  pcSlice->checkColRefIdx(sliceIdxCount, pcPic);
1452  }
1453  pcPic->setCurrSliceIdx(sliceIdxCount);
1454  m_pcSliceEncoder->setSliceIdx(sliceIdxCount);
1455 
1456  pcSlice->setRPS(pcPic->getSlice(0)->getRPS());
1457  pcSlice->setRPSidx(pcPic->getSlice(0)->getRPSidx());
1458 
1459  for ( UInt ui = 0 ; ui < numSubstreams; ui++ )
1460  {
1461  substreamsOut[ui].clear();
1462  }
1463 
1464  m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice );
1465  m_pcEntropyCoder->resetEntropy ();
1466  /* start slice NALunit */
1467  OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->getTLayer() );
1468  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1469 
1470  pcSlice->setNoRaslOutputFlag(false);
1471  if (pcSlice->isIRAP())
1472  {
1474  {
1475  pcSlice->setNoRaslOutputFlag(true);
1476  }
1477  //the inference for NoOutputPriorPicsFlag
1478  // KJS: This cannot happen at the encoder
1479  if (!m_bFirst && pcSlice->isIRAP() && pcSlice->getNoRaslOutputFlag())
1480  {
1481  if (pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA)
1482  {
1483  pcSlice->setNoOutputPriorPicsFlag(true);
1484  }
1485  }
1486  }
1487 
1488  pcSlice->setEncCABACTableIdx(m_pcSliceEncoder->getEncCABACTableIdx());
1489 
1490  tmpBitsBeforeWriting = m_pcEntropyCoder->getNumberOfWrittenBits();
1491  m_pcEntropyCoder->encodeSliceHeader(pcSlice);
1492  actualHeadBits += ( m_pcEntropyCoder->getNumberOfWrittenBits() - tmpBitsBeforeWriting );
1493 
1494  pcSlice->setFinalized(true);
1495 
1496 #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
1497  g_bFinalEncode = true;
1498 #endif
1499 
1500  pcSlice->clearSubstreamSizes( );
1501  {
1502  UInt numBinsCoded = 0;
1503  m_pcSliceEncoder->encodeSlice(pcPic, &(substreamsOut[0]), numBinsCoded);
1504  binCountsInNalUnits+=numBinsCoded;
1505  }
1506 
1507 #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
1508  g_bFinalEncode = false;
1509 #endif
1510 
1511  {
1512  // Construct the final bitstream by concatenating substreams.
1513  // The final bitstream is either nalu.m_Bitstream or pcBitstreamRedirect;
1514  // Complete the slice header info.
1515  m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice );
1516  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1517  m_pcEntropyCoder->encodeTilesWPPEntryPoint( pcSlice );
1518 
1519  // Append substreams...
1520  TComOutputBitstream *pcOut = pcBitstreamRedirect;
1521  const Int numZeroSubstreamsAtStartOfSlice = pcPic->getSubstreamForCtuAddr(pcSlice->getSliceSegmentCurStartCtuTsAddr(), false, pcSlice);
1522  const Int numSubstreamsToCode = pcSlice->getNumberOfSubstreamSizes()+1;
1523  for ( UInt ui = 0 ; ui < numSubstreamsToCode; ui++ )
1524  {
1525  pcOut->addSubstream(&(substreamsOut[ui+numZeroSubstreamsAtStartOfSlice]));
1526  }
1527  }
1528 
1529  // If current NALU is the first NALU of slice (containing slice header) and more NALUs exist (due to multiple dependent slices) then buffer it.
1530  // 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.
1531  Bool bNALUAlignedWrittenToList = false; // used to ensure current NALU is not written more than once to the NALU list.
1532  xAttachSliceDataToNalUnit(nalu, pcBitstreamRedirect);
1533  accessUnit.push_back(new NALUnitEBSP(nalu));
1534  actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8;
1535  numBytesInVclNalUnits += (std::size_t)(accessUnit.back()->m_nalUnitData.str().size());
1536  bNALUAlignedWrittenToList = true;
1537 
1538  if (!bNALUAlignedWrittenToList)
1539  {
1540  nalu.m_Bitstream.writeAlignZero();
1541  accessUnit.push_back(new NALUnitEBSP(nalu));
1542  }
1543 
1544  if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) &&
1545  ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) &&
1549  {
1550  UInt numNalus = 0;
1551  UInt numRBSPBytes = 0;
1552  for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
1553  {
1554  UInt numRBSPBytes_nal = UInt((*it)->m_nalUnitData.str().size());
1555  if ((*it)->m_nalUnitType != NAL_UNIT_PREFIX_SEI && (*it)->m_nalUnitType != NAL_UNIT_SUFFIX_SEI)
1556  {
1557  numRBSPBytes += numRBSPBytes_nal;
1558  numNalus ++;
1559  }
1560  }
1561  accumBitsDU[ pcSlice->getSliceIdx() ] = ( numRBSPBytes << 3 );
1562  accumNalsDU[ pcSlice->getSliceIdx() ] = numNalus; // SEI not counted for bit count; hence shouldn't be counted for # of NALUs - only for consistency
1563  }
1564  } // end iteration over slices
1565 
1566  // cabac_zero_words processing
1567  {
1568  const Int log2subWidthCxsubHeightC = (pcPic->getComponentScaleX(COMPONENT_Cb)+pcPic->getComponentScaleY(COMPONENT_Cb));
1569  const Int minCuWidth = pcPic->getMinCUWidth();
1570  const Int minCuHeight = pcPic->getMinCUHeight();
1571  const Int paddedWidth = ((pcSlice->getSPS()->getPicWidthInLumaSamples() + minCuWidth - 1) / minCuWidth) * minCuWidth;
1572  const Int paddedHeight= ((pcSlice->getSPS()->getPicHeightInLumaSamples() + minCuHeight - 1) / minCuHeight) * minCuHeight;
1573  const Int rawBits = paddedWidth * paddedHeight *
1574  (g_bitDepth[CHANNEL_TYPE_LUMA] + 2*(g_bitDepth[CHANNEL_TYPE_CHROMA]>>log2subWidthCxsubHeightC));
1575  const std::size_t threshold = (32/3)*numBytesInVclNalUnits + (rawBits/32);
1576  if (binCountsInNalUnits >= threshold)
1577  {
1578  // need to add additional cabac zero words (each one accounts for 3 bytes (=00 00 03)) to increase numBytesInVclNalUnits
1579  const std::size_t targetNumBytesInVclNalUnits = ((binCountsInNalUnits - (rawBits/32))*3+31)/32;
1580 
1581  if (targetNumBytesInVclNalUnits>numBytesInVclNalUnits) // It should be!
1582  {
1583  const std::size_t numberOfAdditionalBytesNeeded=targetNumBytesInVclNalUnits - numBytesInVclNalUnits;
1584  const std::size_t numberOfAdditionalCabacZeroWords=(numberOfAdditionalBytesNeeded+2)/3;
1585  const std::size_t numberOfAdditionalCabacZeroBytes=numberOfAdditionalCabacZeroWords*3;
1586  if (m_pcCfg->getCabacZeroWordPaddingEnabled())
1587  {
1588  std::vector<Char> zeroBytesPadding(numberOfAdditionalCabacZeroBytes, Char(0));
1589  for(std::size_t i=0; i<numberOfAdditionalCabacZeroWords; i++)
1590  {
1591  zeroBytesPadding[i*3+2]=3; // 00 00 03
1592  }
1593  accessUnit.back()->m_nalUnitData.write(&(zeroBytesPadding[0]), numberOfAdditionalCabacZeroBytes);
1594  printf("Adding %d bytes of padding\n", UInt(numberOfAdditionalCabacZeroWords*3));
1595  }
1596  else
1597  {
1598  printf("Standard would normally require adding %d bytes of padding\n", UInt(numberOfAdditionalCabacZeroWords*3));
1599  }
1600  }
1601  }
1602  }
1603 
1604  pcPic->compressMotion();
1605 
1606  //-- For time output for each slice
1607  Double dEncTime = (Double)(clock()-iBeforeTime) / CLOCKS_PER_SEC;
1608 
1609  std::string digestStr;
1610  if (m_pcCfg->getDecodedPictureHashSEIEnabled())
1611  {
1612  /* calculate MD5sum for entire reconstructed picture */
1613  SEIDecodedPictureHash sei_recon_picture_digest;
1614  if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 1)
1615  {
1616  sei_recon_picture_digest.method = SEIDecodedPictureHash::MD5;
1617  UInt numChar=calcMD5(*pcPic->getPicYuvRec(), sei_recon_picture_digest.m_digest);
1618  digestStr = digestToString(sei_recon_picture_digest.m_digest, numChar);
1619  }
1620  else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 2)
1621  {
1622  sei_recon_picture_digest.method = SEIDecodedPictureHash::CRC;
1623  UInt numChar=calcCRC(*pcPic->getPicYuvRec(), sei_recon_picture_digest.m_digest);
1624  digestStr = digestToString(sei_recon_picture_digest.m_digest, numChar);
1625  }
1626  else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 3)
1627  {
1628  sei_recon_picture_digest.method = SEIDecodedPictureHash::CHECKSUM;
1629  UInt numChar=calcChecksum(*pcPic->getPicYuvRec(), sei_recon_picture_digest.m_digest);
1630  digestStr = digestToString(sei_recon_picture_digest.m_digest, numChar);
1631  }
1632  OutputNALUnit nalu(NAL_UNIT_SUFFIX_SEI, pcSlice->getTLayer());
1633 
1634  /* write the SEI messages */
1635  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1636  m_seiWriter.writeSEImessage(nalu.m_Bitstream, sei_recon_picture_digest, pcSlice->getSPS());
1637  writeRBSPTrailingBits(nalu.m_Bitstream);
1638 
1639  accessUnit.insert(accessUnit.end(), new NALUnitEBSP(nalu));
1640  }
1641  if (m_pcCfg->getTemporalLevel0IndexSEIEnabled())
1642  {
1643  SEITemporalLevel0Index sei_temporal_level0_index;
1644  if (pcSlice->getRapPicFlag())
1645  {
1646  m_tl0Idx = 0;
1647  m_rapIdx = (m_rapIdx + 1) & 0xFF;
1648  }
1649  else
1650  {
1651  m_tl0Idx = (m_tl0Idx + (pcSlice->getTLayer() ? 0 : 1)) & 0xFF;
1652  }
1653  sei_temporal_level0_index.tl0Idx = m_tl0Idx;
1654  sei_temporal_level0_index.rapIdx = m_rapIdx;
1655 
1657 
1658  /* write the SEI messages */
1659  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1660  m_seiWriter.writeSEImessage(nalu.m_Bitstream, sei_temporal_level0_index, pcSlice->getSPS());
1662 
1663  /* insert the SEI message NALUnit before any Slice NALUnits */
1664  AccessUnit::iterator it = find_if(accessUnit.begin(), accessUnit.end(), mem_fun(&NALUnit::isSlice));
1665  accessUnit.insert(it, new NALUnitEBSP(nalu));
1666  }
1667 
1668  m_pcCfg->setEncodedFlag(iGOPid, true);
1669  xCalculateAddPSNR( pcPic, pcPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
1670 
1671  //In case of field coding, compute the interlaced PSNR for both fields
1672  if(isField)
1673  {
1674  Bool bothFieldsAreEncoded = false;
1675  Int correspondingFieldPOC = pcPic->getPOC();
1676  Int currentPicGOPPoc = m_pcCfg->getGOPEntry(iGOPid).m_POC;
1677  if(pcPic->getPOC() == 0)
1678  {
1679  // particular case for POC 0 and 1.
1680  // If they are not encoded first and separately from other pictures, we need to change this
1681  // POC 0 is always encoded first then POC 1 is encoded
1682  bothFieldsAreEncoded = false;
1683  }
1684  else if(pcPic->getPOC() == 1)
1685  {
1686  // if we are at POC 1, POC 0 has been encoded for sure
1687  correspondingFieldPOC = 0;
1688  bothFieldsAreEncoded = true;
1689  }
1690  else
1691  {
1692  if(pcPic->getPOC()%2 == 1)
1693  {
1694  correspondingFieldPOC -= 1; // all odd POC are associated with the preceding even POC (e.g poc 1 is associated to poc 0)
1695  currentPicGOPPoc -= 1;
1696  }
1697  else
1698  {
1699  correspondingFieldPOC += 1; // all even POC are associated with the following odd POC (e.g poc 0 is associated to poc 1)
1700  currentPicGOPPoc += 1;
1701  }
1702  for(Int i = 0; i < m_iGopSize; i ++)
1703  {
1704  if(m_pcCfg->getGOPEntry(i).m_POC == currentPicGOPPoc)
1705  {
1706  bothFieldsAreEncoded = m_pcCfg->getGOPEntry(i).m_isEncoded;
1707  break;
1708  }
1709  }
1710  }
1711 
1712  if(bothFieldsAreEncoded)
1713  {
1714  //get complementary top field
1715  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1716  while ((*iterPic)->getPOC() != correspondingFieldPOC)
1717  {
1718  iterPic ++;
1719  }
1720  TComPic* correspondingFieldPic = *(iterPic);
1721 
1722  if( (pcPic->isTopField() && isTff) || (!pcPic->isTopField() && !isTff))
1723  {
1724  xCalculateInterlacedAddPSNR(pcPic, correspondingFieldPic, pcPic->getPicYuvRec(), correspondingFieldPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
1725  }
1726  else
1727  {
1728  xCalculateInterlacedAddPSNR(correspondingFieldPic, pcPic, correspondingFieldPic->getPicYuvRec(), pcPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
1729  }
1730  }
1731  }
1732 
1733  if (!digestStr.empty())
1734  {
1735  if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 1)
1736  {
1737  printf(" [MD5:%s]", digestStr.c_str());
1738  }
1739  else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 2)
1740  {
1741  printf(" [CRC:%s]", digestStr.c_str());
1742  }
1743  else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 3)
1744  {
1745  printf(" [Checksum:%s]", digestStr.c_str());
1746  }
1747  }
1748 
1749  if ( m_pcCfg->getUseRateCtrl() )
1750  {
1751  Double avgQP = m_pcRateCtrl->getRCPic()->calAverageQP();
1752  Double avgLambda = m_pcRateCtrl->getRCPic()->calAverageLambda();
1753  if ( avgLambda < 0.0 )
1754  {
1755  avgLambda = lambda;
1756  }
1757 
1758  m_pcRateCtrl->getRCPic()->updateAfterPicture( actualHeadBits, actualTotalBits, avgQP, avgLambda, pcSlice->getSliceType());
1759  m_pcRateCtrl->getRCPic()->addToPictureLsit( m_pcRateCtrl->getPicList() );
1760 
1761  m_pcRateCtrl->getRCSeq()->updateAfterPic( actualTotalBits );
1762  if ( pcSlice->getSliceType() != I_SLICE )
1763  {
1764  m_pcRateCtrl->getRCGOP()->updateAfterPicture( actualTotalBits );
1765  }
1766  else // for intra picture, the estimated bits are used to update the current status in the GOP
1767  {
1768  m_pcRateCtrl->getRCGOP()->updateAfterPicture( estimatedBits );
1769  }
1770  }
1771 
1772  if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) &&
1773  ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) &&
1776  {
1777  const TComVUI *vui = pcSlice->getSPS()->getVuiParameters();
1778  const TComHRD *hrd = vui->getHrdParameters();
1779 
1780  if( hrd->getSubPicCpbParamsPresentFlag() )
1781  {
1782  Int i;
1783  UInt64 ui64Tmp;
1784  UInt uiPrev = 0;
1785  UInt numDU = ( pictureTimingSEI.m_numDecodingUnitsMinus1 + 1 );
1786  UInt *pCRD = &pictureTimingSEI.m_duCpbRemovalDelayMinus1[0];
1787  UInt maxDiff = ( hrd->getTickDivisorMinus2() + 2 ) - 1;
1788 
1789  for( i = 0; i < numDU; i ++ )
1790  {
1791  pictureTimingSEI.m_numNalusInDuMinus1[ i ] = ( i == 0 ) ? ( accumNalsDU[ i ] - 1 ) : ( accumNalsDU[ i ] - accumNalsDU[ i - 1] - 1 );
1792  }
1793 
1794  if( numDU == 1 )
1795  {
1796  pCRD[ 0 ] = 0; /* don't care */
1797  }
1798  else
1799  {
1800  pCRD[ numDU - 1 ] = 0;/* by definition */
1801  UInt tmp = 0;
1802  UInt accum = 0;
1803 
1804  for( i = ( numDU - 2 ); i >= 0; i -- )
1805  {
1806  ui64Tmp = ( ( ( accumBitsDU[ numDU - 1 ] - accumBitsDU[ i ] ) * ( vui->getTimingInfo()->getTimeScale() / vui->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) );
1807  if( (UInt)ui64Tmp > maxDiff )
1808  {
1809  tmp ++;
1810  }
1811  }
1812  uiPrev = 0;
1813 
1814  UInt flag = 0;
1815  for( i = ( numDU - 2 ); i >= 0; i -- )
1816  {
1817  flag = 0;
1818  ui64Tmp = ( ( ( accumBitsDU[ numDU - 1 ] - accumBitsDU[ i ] ) * ( vui->getTimingInfo()->getTimeScale() / vui->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) );
1819 
1820  if( (UInt)ui64Tmp > maxDiff )
1821  {
1822  if(uiPrev >= maxDiff - tmp)
1823  {
1824  ui64Tmp = uiPrev + 1;
1825  flag = 1;
1826  }
1827  else
1828  {
1829  ui64Tmp = maxDiff - tmp + 1;
1830  }
1831  }
1832  pCRD[ i ] = (UInt)ui64Tmp - uiPrev - 1;
1833  if( (Int)pCRD[ i ] < 0 )
1834  {
1835  pCRD[ i ] = 0;
1836  }
1837  else if (tmp > 0 && flag == 1)
1838  {
1839  tmp --;
1840  }
1841  accum += pCRD[ i ] + 1;
1842  uiPrev = accum;
1843  }
1844  }
1845  }
1846 
1847  if( m_pcCfg->getPictureTimingSEIEnabled() )
1848  {
1849  {
1850  OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI, pcSlice->getTLayer());
1851  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1852  pictureTimingSEI.m_picStruct = (isField && pcSlice->getPic()->isTopField())? 1 : isField? 2 : 0;
1853  m_seiWriter.writeSEImessage(nalu.m_Bitstream, pictureTimingSEI, pcSlice->getSPS());
1854  writeRBSPTrailingBits(nalu.m_Bitstream);
1855  UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit);
1856  UInt offsetPosition = m_activeParameterSetSEIPresentInAU
1857  + m_bufferingPeriodSEIPresentInAU; // Insert PT SEI after APS and BP SEI
1858  AccessUnit::iterator it = accessUnit.begin();
1859  for(Int j = 0; j < seiPositionInAu + offsetPosition; j++)
1860  {
1861  it++;
1862  }
1863  accessUnit.insert(it, new NALUnitEBSP(nalu));
1864  m_pictureTimingSEIPresentInAU = true;
1865  }
1866 
1867  if ( m_pcCfg->getScalableNestingSEIEnabled() ) // put picture timing SEI into scalable nesting SEI
1868  {
1869  OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI, pcSlice->getTLayer());
1870  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1871  scalableNestingSEI.m_nestedSEIs.clear();
1872  scalableNestingSEI.m_nestedSEIs.push_back(&pictureTimingSEI);
1873  m_seiWriter.writeSEImessage(nalu.m_Bitstream, scalableNestingSEI, pcSlice->getSPS());
1874  writeRBSPTrailingBits(nalu.m_Bitstream);
1875  UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit);
1876  UInt offsetPosition = m_activeParameterSetSEIPresentInAU
1877  + m_bufferingPeriodSEIPresentInAU + m_pictureTimingSEIPresentInAU + m_nestedBufferingPeriodSEIPresentInAU; // Insert PT SEI after APS and BP SEI
1878  AccessUnit::iterator it = accessUnit.begin();
1879  for(Int j = 0; j < seiPositionInAu + offsetPosition; j++)
1880  {
1881  it++;
1882  }
1883  accessUnit.insert(it, new NALUnitEBSP(nalu));
1884  m_nestedPictureTimingSEIPresentInAU = true;
1885  }
1886  }
1887 
1888  if( m_pcCfg->getDecodingUnitInfoSEIEnabled() && hrd->getSubPicCpbParamsPresentFlag() )
1889  {
1890  m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1891  for( Int i = 0; i < ( pictureTimingSEI.m_numDecodingUnitsMinus1 + 1 ); i ++ )
1892  {
1893  OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI, pcSlice->getTLayer());
1894 
1895  SEIDecodingUnitInfo tempSEI;
1896  tempSEI.m_decodingUnitIdx = i;
1897  tempSEI.m_duSptCpbRemovalDelay = pictureTimingSEI.m_duCpbRemovalDelayMinus1[i] + 1;
1898  tempSEI.m_dpbOutputDuDelayPresentFlag = false;
1899  tempSEI.m_picSptDpbOutputDuDelay = picSptDpbOutputDuDelay;
1900 
1901  // Insert the first one in the right location, before the first slice
1902  if(i == 0)
1903  {
1904  // Insert before the first slice.
1905  m_seiWriter.writeSEImessage(nalu.m_Bitstream, tempSEI, pcSlice->getSPS());
1906  writeRBSPTrailingBits(nalu.m_Bitstream);
1907 
1908  UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit);
1909  UInt offsetPosition = m_activeParameterSetSEIPresentInAU
1910  + m_bufferingPeriodSEIPresentInAU
1911  + m_pictureTimingSEIPresentInAU; // Insert DU info SEI after APS, BP and PT SEI
1912  AccessUnit::iterator it = accessUnit.begin();
1913  for(Int j = 0; j < seiPositionInAu + offsetPosition; j++)
1914  {
1915  it++;
1916  }
1917  accessUnit.insert(it, new NALUnitEBSP(nalu));
1918  }
1919  else
1920  {
1921  // For the second decoding unit onwards we know how many NALUs are present
1922  AccessUnit::iterator it = accessUnit.begin();
1923  for (Int ctr = 0; it != accessUnit.end(); it++)
1924  {
1925  if(ctr == accumNalsDU[ i - 1 ])
1926  {
1927  // Insert before the first slice.
1928  m_seiWriter.writeSEImessage(nalu.m_Bitstream, tempSEI, pcSlice->getSPS());
1929  writeRBSPTrailingBits(nalu.m_Bitstream);
1930 
1931  accessUnit.insert(it, new NALUnitEBSP(nalu));
1932  break;
1933  }
1934  if ((*it)->m_nalUnitType != NAL_UNIT_PREFIX_SEI && (*it)->m_nalUnitType != NAL_UNIT_SUFFIX_SEI)
1935  {
1936  ctr++;
1937  }
1938  }
1939  }
1940  }
1941  }
1942  }
1943 
1944  xResetNonNestedSEIPresentFlags();
1945  xResetNestedSEIPresentFlags();
1946 
1947  pcPic->getPicYuvRec()->copyToPic(pcPicYuvRecOut);
1948 
1949  pcPic->setReconMark ( true );
1950  m_bFirst = false;
1951  m_iNumPicCoded++;
1952  m_totalCoded ++;
1953  /* logging: insert a newline at end of picture period */
1954  printf("\n");
1955  fflush(stdout);
1956 
1957 #if EFFICIENT_FIELD_IRAP
1958  if(IRAPtoReorder)
1959  {
1960  if(swapIRAPForward)
1961  {
1962  if(iGOPid == IRAPGOPid)
1963  {
1964  iGOPid = IRAPGOPid +1;
1965  IRAPtoReorder = false;
1966  }
1967  else if(iGOPid == IRAPGOPid +1)
1968  {
1969  iGOPid --;
1970  }
1971  }
1972  else
1973  {
1974  if(iGOPid == IRAPGOPid)
1975  {
1976  iGOPid = IRAPGOPid -1;
1977  }
1978  else if(iGOPid == IRAPGOPid -1)
1979  {
1980  iGOPid = IRAPGOPid;
1981  IRAPtoReorder = false;
1982  }
1983  }
1984  }
1985 #endif
1986  } // iGOPid-loop
1987 
1988  delete pcBitstreamRedirect;
1989 
1990  if( accumBitsDU != NULL)
1991  {
1992  delete accumBitsDU;
1993  }
1994  if( accumNalsDU != NULL)
1995  {
1996  delete accumNalsDU;
1997  }
1998 
1999  assert ( (m_iNumPicCoded == iNumPicRcvd) );
2000 }
2001 
2002 Void TEncGOP::printOutSummary(UInt uiNumAllPicCoded, Bool isField, const Bool printMSEBasedSNR, const Bool printSequenceMSE)
2003 {
2004  assert (uiNumAllPicCoded == m_gcAnalyzeAll.getNumPic());
2005 
2006 
2007  //--CFG_KDY
2008  const Int rateMultiplier=(isField?2:1);
2009  m_gcAnalyzeAll.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier );
2010  m_gcAnalyzeI.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier );
2011  m_gcAnalyzeP.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier );
2012  m_gcAnalyzeB.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier );
2013  const ChromaFormat chFmt = m_pcCfg->getChromaFormatIdc();
2014 
2015  //-- all
2016  printf( "\n\nSUMMARY --------------------------------------------------------\n" );
2017  m_gcAnalyzeAll.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE);
2018 
2019  printf( "\n\nI Slices--------------------------------------------------------\n" );
2020  m_gcAnalyzeI.printOut('i', chFmt, printMSEBasedSNR, printSequenceMSE);
2021 
2022  printf( "\n\nP Slices--------------------------------------------------------\n" );
2023  m_gcAnalyzeP.printOut('p', chFmt, printMSEBasedSNR, printSequenceMSE);
2024 
2025  printf( "\n\nB Slices--------------------------------------------------------\n" );
2026  m_gcAnalyzeB.printOut('b', chFmt, printMSEBasedSNR, printSequenceMSE);
2027 
2028 #if _SUMMARY_OUT_
2029  m_gcAnalyzeAll.printSummary(chFmt, printSequenceMSE);
2030 #endif
2031 #if _SUMMARY_PIC_
2032  m_gcAnalyzeI.printSummary(chFmt, printSequenceMSE,'I');
2033  m_gcAnalyzeP.printSummary(chFmt, printSequenceMSE,'P');
2034  m_gcAnalyzeB.printSummary(chFmt, printSequenceMSE,'B');
2035 #endif
2036 
2037  if(isField)
2038  {
2039  //-- interlaced summary
2040  m_gcAnalyzeAll_in.setFrmRate( m_pcCfg->getFrameRate());
2042  // prior to the above statement, the interlace analyser does not contain the correct total number of bits.
2043 
2044  printf( "\n\nSUMMARY INTERLACED ---------------------------------------------\n" );
2045  m_gcAnalyzeAll_in.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE);
2046 
2047 #if _SUMMARY_OUT_
2048  m_gcAnalyzeAll_in.printSummary(chFmt, printSequenceMSE);
2049 #endif
2050  }
2051 
2052  printf("\nRVM: %.3lf\n" , xCalculateRVM());
2053 }
2054 
2056 {
2057  Bool bCalcDist = false;
2058  m_pcLoopFilter->setCfg(m_pcCfg->getLFCrossTileBoundaryFlag());
2059  m_pcLoopFilter->loopFilterPic( pcPic );
2060 
2061  if (!bCalcDist)
2062  {
2063  ruiDist = xFindDistortionFrame(pcPic->getPicYuvOrg(), pcPic->getPicYuvRec());
2064  }
2065 }
2066 
2067 // ====================================================================================================================
2068 // Protected member functions
2069 // ====================================================================================================================
2070 
2071 
2072 Void TEncGOP::xInitGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut, Bool isField )
2073 {
2074  assert( iNumPicRcvd > 0 );
2075  // Exception for the first frames
2076  if ( ( isField && (iPOCLast == 0 || iPOCLast == 1) ) || (!isField && (iPOCLast == 0)) )
2077  {
2078  m_iGopSize = 1;
2079  }
2080  else
2081  {
2082  m_iGopSize = m_pcCfg->getGOPSize();
2083  }
2084  assert (m_iGopSize > 0);
2085 
2086  return;
2087 }
2088 
2089 
2091  TComList<TComPicYuv*>& rcListPicYuvRecOut,
2092  Int iNumPicRcvd,
2093  Int iTimeOffset,
2094  TComPic*& rpcPic,
2095  TComPicYuv*& rpcPicYuvRecOut,
2096  Int pocCurr,
2097  Bool isField)
2098 {
2099  Int i;
2100  // Rec. output
2101  TComList<TComPicYuv*>::iterator iterPicYuvRec = rcListPicYuvRecOut.end();
2102 
2103  if (isField && pocCurr > 1 && m_iGopSize!=1)
2104  {
2105  iTimeOffset--;
2106  }
2107 
2108  for ( i = 0; i < (iNumPicRcvd - iTimeOffset + 1); i++ )
2109  {
2110  iterPicYuvRec--;
2111  }
2112 
2113  rpcPicYuvRecOut = *(iterPicYuvRec);
2114 
2115  // Current pic.
2116  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
2117  while (iterPic != rcListPic.end())
2118  {
2119  rpcPic = *(iterPic);
2120  rpcPic->setCurrSliceIdx(0);
2121  if (rpcPic->getPOC() == pocCurr)
2122  {
2123  break;
2124  }
2125  iterPic++;
2126  }
2127 
2128  assert (rpcPic != NULL);
2129  assert (rpcPic->getPOC() == pocCurr);
2130 
2131  return;
2132 }
2133 
2135 {
2136  UInt64 uiTotalDiff = 0;
2137 
2138  for(Int chan=0; chan<pcPic0 ->getNumberValidComponents(); chan++)
2139  {
2140  const ComponentID ch=ComponentID(chan);
2141  Pel* pSrc0 = pcPic0 ->getAddr(ch);
2142  Pel* pSrc1 = pcPic1 ->getAddr(ch);
2144 
2145  const Int iStride = pcPic0->getStride(ch);
2146  const Int iWidth = pcPic0->getWidth(ch);
2147  const Int iHeight = pcPic0->getHeight(ch);
2148 
2149  for(Int y = 0; y < iHeight; y++ )
2150  {
2151  for(Int x = 0; x < iWidth; x++ )
2152  {
2153  Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
2154  uiTotalDiff += UInt64((iTemp*iTemp) >> uiShift);
2155  }
2156  pSrc0 += iStride;
2157  pSrc1 += iStride;
2158  }
2159  }
2160 
2161  return uiTotalDiff;
2162 }
2163 
2164 #if VERBOSE_RATE
2165 static const Char* nalUnitTypeToString(NalUnitType type)
2166 {
2167  switch (type)
2168  {
2169  case NAL_UNIT_CODED_SLICE_TRAIL_R: return "TRAIL_R";
2170  case NAL_UNIT_CODED_SLICE_TRAIL_N: return "TRAIL_N";
2171  case NAL_UNIT_CODED_SLICE_TSA_R: return "TSA_R";
2172  case NAL_UNIT_CODED_SLICE_TSA_N: return "TSA_N";
2173  case NAL_UNIT_CODED_SLICE_STSA_R: return "STSA_R";
2174  case NAL_UNIT_CODED_SLICE_STSA_N: return "STSA_N";
2175  case NAL_UNIT_CODED_SLICE_BLA_W_LP: return "BLA_W_LP";
2176  case NAL_UNIT_CODED_SLICE_BLA_W_RADL: return "BLA_W_RADL";
2177  case NAL_UNIT_CODED_SLICE_BLA_N_LP: return "BLA_N_LP";
2178  case NAL_UNIT_CODED_SLICE_IDR_W_RADL: return "IDR_W_RADL";
2179  case NAL_UNIT_CODED_SLICE_IDR_N_LP: return "IDR_N_LP";
2180  case NAL_UNIT_CODED_SLICE_CRA: return "CRA";
2181  case NAL_UNIT_CODED_SLICE_RADL_R: return "RADL_R";
2182  case NAL_UNIT_CODED_SLICE_RADL_N: return "RADL_N";
2183  case NAL_UNIT_CODED_SLICE_RASL_R: return "RASL_R";
2184  case NAL_UNIT_CODED_SLICE_RASL_N: return "RASL_N";
2185  case NAL_UNIT_VPS: return "VPS";
2186  case NAL_UNIT_SPS: return "SPS";
2187  case NAL_UNIT_PPS: return "PPS";
2188  case NAL_UNIT_ACCESS_UNIT_DELIMITER: return "AUD";
2189  case NAL_UNIT_EOS: return "EOS";
2190  case NAL_UNIT_EOB: return "EOB";
2191  case NAL_UNIT_FILLER_DATA: return "FILLER";
2192  case NAL_UNIT_PREFIX_SEI: return "SEI";
2193  case NAL_UNIT_SUFFIX_SEI: return "SEI";
2194  default: return "UNK";
2195  }
2196 }
2197 #endif
2198 
2199 Void TEncGOP::xCalculateAddPSNR( TComPic* pcPic, TComPicYuv* pcPicD, const AccessUnit& accessUnit, Double dEncTime, const InputColourSpaceConversion conversion, const Bool printFrameMSE )
2200 {
2201  Double dPSNR[MAX_NUM_COMPONENT];
2202 
2203  for(Int i=0; i<MAX_NUM_COMPONENT; i++)
2204  {
2205  dPSNR[i]=0.0;
2206  }
2207 
2208  TComPicYuv cscd;
2209  if (conversion!=IPCOLOURSPACE_UNCHANGED)
2210  {
2212  TVideoIOYuv::ColourSpaceConvert(*pcPicD, cscd, conversion, g_bitDepth, false);
2213  }
2214  TComPicYuv &picd=(conversion==IPCOLOURSPACE_UNCHANGED)?*pcPicD : cscd;
2215 
2216  //===== calculate PSNR =====
2217  Double MSEyuvframe[MAX_NUM_COMPONENT] = {0, 0, 0};
2218 
2219  for(Int chan=0; chan<pcPicD->getNumberValidComponents(); chan++)
2220  {
2221  const ComponentID ch=ComponentID(chan);
2222  const Pel* pOrg = (conversion!=IPCOLOURSPACE_UNCHANGED) ? pcPic ->getPicYuvTrueOrg()->getAddr(ch) : pcPic ->getPicYuvOrg()->getAddr(ch);
2223  Pel* pRec = picd.getAddr(ch);
2224  const Int iStride = pcPicD->getStride(ch);
2225 
2226  const Int iWidth = pcPicD->getWidth (ch) - (m_pcEncTop->getPad(0) >> pcPic->getComponentScaleX(ch));
2227  const Int iHeight = pcPicD->getHeight(ch) - ((m_pcEncTop->getPad(1) >> (pcPic->isField()?1:0)) >> pcPic->getComponentScaleY(ch));
2228 
2229  Int iSize = iWidth*iHeight;
2230 
2231  UInt64 uiSSDtemp=0;
2232  for(Int y = 0; y < iHeight; y++ )
2233  {
2234  for(Int x = 0; x < iWidth; x++ )
2235  {
2236  Intermediate_Int iDiff = (Intermediate_Int)( pOrg[x] - pRec[x] );
2237  uiSSDtemp += iDiff * iDiff;
2238  }
2239  pOrg += iStride;
2240  pRec += iStride;
2241  }
2242  const Int maxval = 255 << (g_bitDepth[toChannelType(ch)] - 8);
2243  const Double fRefValue = (Double) maxval * maxval * iSize;
2244  dPSNR[ch] = ( uiSSDtemp ? 10.0 * log10( fRefValue / (Double)uiSSDtemp ) : 999.99 );
2245  MSEyuvframe[ch] = (Double)uiSSDtemp/(iSize);
2246  }
2247 
2248 
2249  /* calculate the size of the access unit, excluding:
2250  * - any AnnexB contributions (start_code_prefix, zero_byte, etc.,)
2251  * - SEI NAL units
2252  */
2253  UInt numRBSPBytes = 0;
2254  for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
2255  {
2256  UInt numRBSPBytes_nal = UInt((*it)->m_nalUnitData.str().size());
2257 #if VERBOSE_RATE
2258  printf("*** %6s numBytesInNALunit: %u\n", nalUnitTypeToString((*it)->m_nalUnitType), numRBSPBytes_nal);
2259 #endif
2260  if ((*it)->m_nalUnitType != NAL_UNIT_PREFIX_SEI && (*it)->m_nalUnitType != NAL_UNIT_SUFFIX_SEI)
2261  {
2262  numRBSPBytes += numRBSPBytes_nal;
2263  }
2264  }
2265 
2266  UInt uibits = numRBSPBytes * 8;
2267  m_vRVM_RP.push_back( uibits );
2268 
2269  //===== add PSNR =====
2270  m_gcAnalyzeAll.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2271  TComSlice* pcSlice = pcPic->getSlice(0);
2272  if (pcSlice->isIntra())
2273  {
2274  m_gcAnalyzeI.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2275  }
2276  if (pcSlice->isInterP())
2277  {
2278  m_gcAnalyzeP.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2279  }
2280  if (pcSlice->isInterB())
2281  {
2282  m_gcAnalyzeB.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2283  }
2284 
2285  Char c = (pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B');
2286  if (!pcSlice->isReferenced())
2287  {
2288  c += 32;
2289  }
2290 
2291 #if ADAPTIVE_QP_SELECTION
2292  printf("POC %4d TId: %1d ( %c-SLICE, nQP %d QP %d ) %10d bits",
2293  pcSlice->getPOC(),
2294  pcSlice->getTLayer(),
2295  c,
2296  pcSlice->getSliceQpBase(),
2297  pcSlice->getSliceQp(),
2298  uibits );
2299 #else
2300  printf("POC %4d TId: %1d ( %c-SLICE, QP %d ) %10d bits",
2301  pcSlice->getPOC()-pcSlice->getLastIDR(),
2302  pcSlice->getTLayer(),
2303  c,
2304  pcSlice->getSliceQp(),
2305  uibits );
2306 #endif
2307 
2308  printf(" [Y %6.4lf dB U %6.4lf dB V %6.4lf dB]", dPSNR[COMPONENT_Y], dPSNR[COMPONENT_Cb], dPSNR[COMPONENT_Cr] );
2309  if (printFrameMSE)
2310  {
2311  printf(" [Y MSE %6.4lf U MSE %6.4lf V MSE %6.4lf]", MSEyuvframe[COMPONENT_Y], MSEyuvframe[COMPONENT_Cb], MSEyuvframe[COMPONENT_Cr] );
2312  }
2313  printf(" [ET %5.0f ]", dEncTime );
2314 
2315  for (Int iRefList = 0; iRefList < 2; iRefList++)
2316  {
2317  printf(" [L%d ", iRefList);
2318  for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(RefPicList(iRefList)); iRefIndex++)
2319  {
2320  printf ("%d ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex)-pcSlice->getLastIDR());
2321  }
2322  printf("]");
2323  }
2324 
2325  cscd.destroy();
2326 }
2327 
2328 Void TEncGOP::xCalculateInterlacedAddPSNR( TComPic* pcPicOrgFirstField, TComPic* pcPicOrgSecondField,
2329  TComPicYuv* pcPicRecFirstField, TComPicYuv* pcPicRecSecondField,
2330  const AccessUnit& accessUnit, Double dEncTime, const InputColourSpaceConversion conversion, const Bool printFrameMSE )
2331 {
2332  Double dPSNR[MAX_NUM_COMPONENT];
2333  TComPic *apcPicOrgFields[2]={pcPicOrgFirstField, pcPicOrgSecondField};
2334  TComPicYuv *apcPicRecFields[2]={pcPicRecFirstField, pcPicRecSecondField};
2335 
2336  for(Int i=0; i<MAX_NUM_COMPONENT; i++)
2337  {
2338  dPSNR[i]=0.0;
2339  }
2340 
2341  TComPicYuv cscd[2 /* first/second field */];
2342  if (conversion!=IPCOLOURSPACE_UNCHANGED)
2343  {
2344  for(UInt fieldNum=0; fieldNum<2; fieldNum++)
2345  {
2346  TComPicYuv &reconField=*(apcPicRecFields[fieldNum]);
2347  cscd[fieldNum].create(reconField.getWidth(COMPONENT_Y), reconField.getHeight(COMPONENT_Y), reconField.getChromaFormat(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth);
2348  TVideoIOYuv::ColourSpaceConvert(reconField, cscd[fieldNum], conversion, g_bitDepth, false);
2349  apcPicRecFields[fieldNum]=cscd+fieldNum;
2350  }
2351  }
2352 
2353  //===== calculate PSNR =====
2354  Double MSEyuvframe[MAX_NUM_COMPONENT] = {0, 0, 0};
2355 
2356  assert(apcPicRecFields[0]->getChromaFormat()==apcPicRecFields[1]->getChromaFormat());
2357  const UInt numValidComponents=apcPicRecFields[0]->getNumberValidComponents();
2358 
2359  for(Int chan=0; chan<numValidComponents; chan++)
2360  {
2361  const ComponentID ch=ComponentID(chan);
2362  assert(apcPicRecFields[0]->getWidth(ch)==apcPicRecFields[1]->getWidth(ch));
2363  assert(apcPicRecFields[0]->getHeight(ch)==apcPicRecFields[1]->getHeight(ch));
2364 
2365  UInt64 uiSSDtemp=0;
2366  const Int iWidth = apcPicRecFields[0]->getWidth (ch) - (m_pcEncTop->getPad(0) >> apcPicRecFields[0]->getComponentScaleX(ch));
2367  const Int iHeight = apcPicRecFields[0]->getHeight(ch) - ((m_pcEncTop->getPad(1) >> 1) >> apcPicRecFields[0]->getComponentScaleY(ch));
2368 
2369  Int iSize = iWidth*iHeight;
2370 
2371  for(UInt fieldNum=0; fieldNum<2; fieldNum++)
2372  {
2373  TComPic *pcPic=apcPicOrgFields[fieldNum];
2374  TComPicYuv *pcPicD=apcPicRecFields[fieldNum];
2375 
2376  const Pel* pOrg = (conversion!=IPCOLOURSPACE_UNCHANGED) ? pcPic ->getPicYuvTrueOrg()->getAddr(ch) : pcPic ->getPicYuvOrg()->getAddr(ch);
2377  Pel* pRec = pcPicD->getAddr(ch);
2378  const Int iStride = pcPicD->getStride(ch);
2379 
2380 
2381  for(Int y = 0; y < iHeight; y++ )
2382  {
2383  for(Int x = 0; x < iWidth; x++ )
2384  {
2385  Intermediate_Int iDiff = (Intermediate_Int)( pOrg[x] - pRec[x] );
2386  uiSSDtemp += iDiff * iDiff;
2387  }
2388  pOrg += iStride;
2389  pRec += iStride;
2390  }
2391  }
2392  const Int maxval = 255 << (g_bitDepth[toChannelType(ch)] - 8);
2393  const Double fRefValue = (Double) maxval * maxval * iSize*2;
2394  dPSNR[ch] = ( uiSSDtemp ? 10.0 * log10( fRefValue / (Double)uiSSDtemp ) : 999.99 );
2395  MSEyuvframe[ch] = (Double)uiSSDtemp/(iSize*2);
2396  }
2397 
2398  UInt uibits = 0; // the number of bits for the pair is not calculated here - instead the overall total is used elsewhere.
2399 
2400  //===== add PSNR =====
2401  m_gcAnalyzeAll_in.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2402 
2403  printf("\n Interlaced frame %d: [Y %6.4lf dB U %6.4lf dB V %6.4lf dB]", pcPicOrgSecondField->getPOC()/2 , dPSNR[COMPONENT_Y], dPSNR[COMPONENT_Cb], dPSNR[COMPONENT_Cr] );
2404  if (printFrameMSE)
2405  {
2406  printf(" [Y MSE %6.4lf U MSE %6.4lf V MSE %6.4lf]", MSEyuvframe[COMPONENT_Y], MSEyuvframe[COMPONENT_Cb], MSEyuvframe[COMPONENT_Cr] );
2407  }
2408 
2409  for(UInt fieldNum=0; fieldNum<2; fieldNum++)
2410  {
2411  cscd[fieldNum].destroy();
2412  }
2413 }
2414 
2422 NalUnitType TEncGOP::getNalUnitType(Int pocCurr, Int lastIDR, Bool isField)
2423 {
2424  if (pocCurr == 0)
2425  {
2427  }
2428 
2429 #if EFFICIENT_FIELD_IRAP
2430  if(isField && pocCurr == 1)
2431  {
2432  // to avoid the picture becoming an IRAP
2434  }
2435 #endif
2436 
2437 #if ALLOW_RECOVERY_POINT_AS_RAP
2438  if(m_pcCfg->getDecodingRefreshType() != 3 && (pocCurr - isField) % m_pcCfg->getIntraPeriod() == 0)
2439 #else
2440  if ((pocCurr - isField) % m_pcCfg->getIntraPeriod() == 0)
2441 #endif
2442  {
2443  if (m_pcCfg->getDecodingRefreshType() == 1)
2444  {
2445  return NAL_UNIT_CODED_SLICE_CRA;
2446  }
2447  else if (m_pcCfg->getDecodingRefreshType() == 2)
2448  {
2450  }
2451  }
2452  if(m_pocCRA>0)
2453  {
2454  if(pocCurr<m_pocCRA)
2455  {
2456  // All leading pictures are being marked as TFD pictures here since current encoder uses all
2457  // reference pictures while encoding leading pictures. An encoder can ensure that a leading
2458  // picture can be still decodable when random accessing to a CRA/CRANT/BLA/BLANT picture by
2459  // controlling the reference pictures used for encoding that leading picture. Such a leading
2460  // picture need not be marked as a TFD picture.
2462  }
2463  }
2464  if (lastIDR>0)
2465  {
2466  if (pocCurr < lastIDR)
2467  {
2469  }
2470  }
2472 }
2473 
2475 {
2476  Double dRVM = 0;
2477 
2478  if( m_pcCfg->getGOPSize() == 1 && m_pcCfg->getIntraPeriod() != 1 && m_pcCfg->getFramesToBeEncoded() > RVM_VCEGAM10_M * 2 )
2479  {
2480  // calculate RVM only for lowdelay configurations
2481  std::vector<Double> vRL , vB;
2482  size_t N = m_vRVM_RP.size();
2483  vRL.resize( N );
2484  vB.resize( N );
2485 
2486  Int i;
2487  Double dRavg = 0 , dBavg = 0;
2488  vB[RVM_VCEGAM10_M] = 0;
2489  for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
2490  {
2491  vRL[i] = 0;
2492  for( Int j = i - RVM_VCEGAM10_M ; j <= i + RVM_VCEGAM10_M - 1 ; j++ )
2493  {
2494  vRL[i] += m_vRVM_RP[j];
2495  }
2496  vRL[i] /= ( 2 * RVM_VCEGAM10_M );
2497  vB[i] = vB[i-1] + m_vRVM_RP[i] - vRL[i];
2498  dRavg += m_vRVM_RP[i];
2499  dBavg += vB[i];
2500  }
2501 
2502  dRavg /= ( N - 2 * RVM_VCEGAM10_M );
2503  dBavg /= ( N - 2 * RVM_VCEGAM10_M );
2504 
2505  Double dSigamB = 0;
2506  for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
2507  {
2508  Double tmp = vB[i] - dBavg;
2509  dSigamB += tmp * tmp;
2510  }
2511  dSigamB = sqrt( dSigamB / ( N - 2 * RVM_VCEGAM10_M ) );
2512 
2513  Double f = sqrt( 12.0 * ( RVM_VCEGAM10_M - 1 ) / ( RVM_VCEGAM10_M + 1 ) );
2514 
2515  dRVM = dSigamB / dRavg * f;
2516  }
2517 
2518  return( dRVM );
2519 }
2520 
2527 {
2528  // Byte-align
2529  rNalu.m_Bitstream.writeByteAlignment(); // Slice header byte-alignment
2530 
2531  // Perform bitstream concatenation
2532  if (codedSliceData->getNumberOfWrittenBits() > 0)
2533  {
2534  rNalu.m_Bitstream.addSubstream(codedSliceData);
2535  }
2536 
2537  m_pcEntropyCoder->setBitstream(&rNalu.m_Bitstream);
2538 
2539  codedSliceData->clear();
2540 }
2541 
2542 // Function will arrange the long-term pictures in the decreasing order of poc_lsb_lt,
2543 // and among the pictures with the same lsb, it arranges them in increasing delta_poc_msb_cycle_lt value
2545 {
2546  TComReferencePictureSet *rps = pcSlice->getRPS();
2547  if(!rps->getNumberOfLongtermPictures())
2548  {
2549  return;
2550  }
2551 
2552  // Arrange long-term reference pictures in the correct order of LSB and MSB,
2553  // and assign values for pocLSBLT and MSB present flag
2554  Int longtermPicsPoc[MAX_NUM_REF_PICS], longtermPicsLSB[MAX_NUM_REF_PICS], indices[MAX_NUM_REF_PICS];
2555  Int longtermPicsMSB[MAX_NUM_REF_PICS];
2556  Bool mSBPresentFlag[MAX_NUM_REF_PICS];
2557  ::memset(longtermPicsPoc, 0, sizeof(longtermPicsPoc)); // Store POC values of LTRP
2558  ::memset(longtermPicsLSB, 0, sizeof(longtermPicsLSB)); // Store POC LSB values of LTRP
2559  ::memset(longtermPicsMSB, 0, sizeof(longtermPicsMSB)); // Store POC LSB values of LTRP
2560  ::memset(indices , 0, sizeof(indices)); // Indices to aid in tracking sorted LTRPs
2561  ::memset(mSBPresentFlag , 0, sizeof(mSBPresentFlag)); // Indicate if MSB needs to be present
2562 
2563  // Get the long-term reference pictures
2565  Int i, ctr = 0;
2566  Int maxPicOrderCntLSB = 1 << pcSlice->getSPS()->getBitsForPOC();
2567  for(i = rps->getNumberOfPictures() - 1; i >= offset; i--, ctr++)
2568  {
2569  longtermPicsPoc[ctr] = rps->getPOC(i); // LTRP POC
2570  longtermPicsLSB[ctr] = getLSB(longtermPicsPoc[ctr], maxPicOrderCntLSB); // LTRP POC LSB
2571  indices[ctr] = i;
2572  longtermPicsMSB[ctr] = longtermPicsPoc[ctr] - longtermPicsLSB[ctr];
2573  }
2574  Int numLongPics = rps->getNumberOfLongtermPictures();
2575  assert(ctr == numLongPics);
2576 
2577  // Arrange pictures in decreasing order of MSB;
2578  for(i = 0; i < numLongPics; i++)
2579  {
2580  for(Int j = 0; j < numLongPics - 1; j++)
2581  {
2582  if(longtermPicsMSB[j] < longtermPicsMSB[j+1])
2583  {
2584  std::swap(longtermPicsPoc[j], longtermPicsPoc[j+1]);
2585  std::swap(longtermPicsLSB[j], longtermPicsLSB[j+1]);
2586  std::swap(longtermPicsMSB[j], longtermPicsMSB[j+1]);
2587  std::swap(indices[j] , indices[j+1] );
2588  }
2589  }
2590  }
2591 
2592  for(i = 0; i < numLongPics; i++)
2593  {
2594  // Check if MSB present flag should be enabled.
2595  // Check if the buffer contains any pictures that have the same LSB.
2596  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
2597  TComPic* pcPic;
2598  while ( iterPic != rcListPic.end() )
2599  {
2600  pcPic = *iterPic;
2601  if( (getLSB(pcPic->getPOC(), maxPicOrderCntLSB) == longtermPicsLSB[i]) && // Same LSB
2602  (pcPic->getSlice(0)->isReferenced()) && // Reference picture
2603  (pcPic->getPOC() != longtermPicsPoc[i]) ) // Not the LTRP itself
2604  {
2605  mSBPresentFlag[i] = true;
2606  break;
2607  }
2608  iterPic++;
2609  }
2610  }
2611 
2612  // tempArray for usedByCurr flag
2613  Bool tempArray[MAX_NUM_REF_PICS]; ::memset(tempArray, 0, sizeof(tempArray));
2614  for(i = 0; i < numLongPics; i++)
2615  {
2616  tempArray[i] = rps->getUsed(indices[i]);
2617  }
2618  // Now write the final values;
2619  ctr = 0;
2620  Int currMSB = 0, currLSB = 0;
2621  // currPicPoc = currMSB + currLSB
2622  currLSB = getLSB(pcSlice->getPOC(), maxPicOrderCntLSB);
2623  currMSB = pcSlice->getPOC() - currLSB;
2624 
2625  for(i = rps->getNumberOfPictures() - 1; i >= offset; i--, ctr++)
2626  {
2627  rps->setPOC (i, longtermPicsPoc[ctr]);
2628  rps->setDeltaPOC (i, - pcSlice->getPOC() + longtermPicsPoc[ctr]);
2629  rps->setUsed (i, tempArray[ctr]);
2630  rps->setPocLSBLT (i, longtermPicsLSB[ctr]);
2631  rps->setDeltaPocMSBCycleLT (i, (currMSB - (longtermPicsPoc[ctr] - longtermPicsLSB[ctr])) / maxPicOrderCntLSB);
2632  rps->setDeltaPocMSBPresentFlag(i, mSBPresentFlag[ctr]);
2633 
2634  assert(rps->getDeltaPocMSBCycleLT(i) >= 0); // Non-negative value
2635  }
2636  for(i = rps->getNumberOfPictures() - 1, ctr = 1; i >= offset; i--, ctr++)
2637  {
2638  for(Int j = rps->getNumberOfPictures() - 1 - ctr; j >= offset; j--)
2639  {
2640  // Here at the encoder we know that we have set the full POC value for the LTRPs, hence we
2641  // don't have to check the MSB present flag values for this constraint.
2642  assert( rps->getPOC(i) != rps->getPOC(j) ); // If assert fails, LTRP entry repeated in RPS!!!
2643  }
2644  }
2645 }
2646 
2652 {
2653  // Find the location of the first SEI message
2654  Int seiStartPos = 0;
2655  for(AccessUnit::iterator it = accessUnit.begin(); it != accessUnit.end(); it++, seiStartPos++)
2656  {
2657  if ((*it)->isSei() || (*it)->isVcl())
2658  {
2659  break;
2660  }
2661  }
2662  // assert(it != accessUnit.end()); // Triggers with some legit configurations
2663  return seiStartPos;
2664 }
2665 
2667 {
2668  TComPicYuv* pcPicYuvRec = pcPic->getPicYuvRec();
2669  Pel* Rec = pcPicYuvRec->getAddr(COMPONENT_Y);
2670  Pel* tempRec = Rec;
2671  Int stride = pcPicYuvRec->getStride(COMPONENT_Y);
2672  UInt log2maxTB = pcPic->getSlice(0)->getSPS()->getQuadtreeTULog2MaxSize();
2673  UInt maxTBsize = (1<<log2maxTB);
2674  const UInt minBlockArtSize = 8;
2675  const UInt picWidth = pcPicYuvRec->getWidth(COMPONENT_Y);
2676  const UInt picHeight = pcPicYuvRec->getHeight(COMPONENT_Y);
2677  const UInt noCol = (picWidth>>log2maxTB);
2678  const UInt noRows = (picHeight>>log2maxTB);
2679  assert(noCol > 1);
2680  assert(noRows > 1);
2681  UInt64 *colSAD = (UInt64*)malloc(noCol*sizeof(UInt64));
2682  UInt64 *rowSAD = (UInt64*)malloc(noRows*sizeof(UInt64));
2683  UInt colIdx = 0;
2684  UInt rowIdx = 0;
2685  Pel p0, p1, p2, q0, q1, q2;
2686 
2687  Int qp = pcPic->getSlice(0)->getSliceQp();
2688  Int bitdepthScale = 1 << (g_bitDepth[CHANNEL_TYPE_LUMA]-8);
2689  Int beta = TComLoopFilter::getBeta( qp ) * bitdepthScale;
2690  const Int thr2 = (beta>>2);
2691  const Int thr1 = 2*bitdepthScale;
2692  UInt a = 0;
2693 
2694  memset(colSAD, 0, noCol*sizeof(UInt64));
2695  memset(rowSAD, 0, noRows*sizeof(UInt64));
2696 
2697  if (maxTBsize > minBlockArtSize)
2698  {
2699  // Analyze vertical artifact edges
2700  for(Int c = maxTBsize; c < picWidth; c += maxTBsize)
2701  {
2702  for(Int r = 0; r < picHeight; r++)
2703  {
2704  p2 = Rec[c-3];
2705  p1 = Rec[c-2];
2706  p0 = Rec[c-1];
2707  q0 = Rec[c];
2708  q1 = Rec[c+1];
2709  q2 = Rec[c+2];
2710  a = ((abs(p2-(p1<<1)+p0)+abs(q0-(q1<<1)+q2))<<1);
2711  if ( thr1 < a && a < thr2)
2712  {
2713  colSAD[colIdx] += abs(p0 - q0);
2714  }
2715  Rec += stride;
2716  }
2717  colIdx++;
2718  Rec = tempRec;
2719  }
2720 
2721  // Analyze horizontal artifact edges
2722  for(Int r = maxTBsize; r < picHeight; r += maxTBsize)
2723  {
2724  for(Int c = 0; c < picWidth; c++)
2725  {
2726  p2 = Rec[c + (r-3)*stride];
2727  p1 = Rec[c + (r-2)*stride];
2728  p0 = Rec[c + (r-1)*stride];
2729  q0 = Rec[c + r*stride];
2730  q1 = Rec[c + (r+1)*stride];
2731  q2 = Rec[c + (r+2)*stride];
2732  a = ((abs(p2-(p1<<1)+p0)+abs(q0-(q1<<1)+q2))<<1);
2733  if (thr1 < a && a < thr2)
2734  {
2735  rowSAD[rowIdx] += abs(p0 - q0);
2736  }
2737  }
2738  rowIdx++;
2739  }
2740  }
2741 
2742  UInt64 colSADsum = 0;
2743  UInt64 rowSADsum = 0;
2744  for(Int c = 0; c < noCol-1; c++)
2745  {
2746  colSADsum += colSAD[c];
2747  }
2748  for(Int r = 0; r < noRows-1; r++)
2749  {
2750  rowSADsum += rowSAD[r];
2751  }
2752 
2753  colSADsum <<= 10;
2754  rowSADsum <<= 10;
2755  colSADsum /= (noCol-1);
2756  colSADsum /= picHeight;
2757  rowSADsum /= (noRows-1);
2758  rowSADsum /= picWidth;
2759 
2760  UInt64 avgSAD = ((colSADsum + rowSADsum)>>1);
2761  avgSAD >>= (g_bitDepth[CHANNEL_TYPE_LUMA]-8);
2762 
2763  if ( avgSAD > 2048 )
2764  {
2765  avgSAD >>= 9;
2766  Int offset = Clip3(2,6,(Int)avgSAD);
2767  for (Int i=0; i<uiNumSlices; i++)
2768  {
2769  pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(true);
2770  pcPic->getSlice(i)->setDeblockingFilterDisable(false);
2771  pcPic->getSlice(i)->setDeblockingFilterBetaOffsetDiv2( offset );
2772  pcPic->getSlice(i)->setDeblockingFilterTcOffsetDiv2( offset );
2773  }
2774  }
2775  else
2776  {
2777  for (Int i=0; i<uiNumSlices; i++)
2778  {
2779  pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(false);
2783  }
2784  }
2785 
2786  free(colSAD);
2787  free(rowSAD);
2788 }
2789 
Void setRefPicList(TComList< TComPic * > &rcListPic, Bool checkNumPocTotalCurr=false)
Definition: TComSlice.cpp:322
SEIMessages m_nestedSEIs
Definition: SEI.h:517
Bool m_callerOwnsSEIs
Definition: SEI.h:516
Int getNumberOfPositivePictures() const
Definition: TComSlice.h:109
Void xCalculateAddPSNR(TComPic *pcPic, TComPicYuv *pcPicD, const AccessUnit &, Double dEncTime, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE)
Definition: TEncGOP.cpp:2199
Void setCheckLDC(Bool b)
Definition: TComSlice.h:1401
Bool getNalHrdParametersPresentFlag() const
Definition: TComSlice.h:321
Int getNumTileRowsMinus1() const
Definition: TComSlice.h:1139
static Void ColourSpaceConvert(const TComPicYuv &src, TComPicYuv &dest, const InputColourSpaceConversion conversion, const Int bitDepths[MAX_NUM_CHANNEL_TYPE], Bool bIsForwards)
Int getLastIDR() const
Definition: TComSlice.h:1332
Void setList1IdxToList0Idx()
Definition: TComSlice.cpp:305
UInt g_uiMaxCUDepth
Definition: TComRom.cpp:253
Void setNoOutputPriorPicsFlag(Bool val)
Definition: TComSlice.h:1453
Bool m_currentFrameIsFrame0Flag
Definition: SEI.h:272
Void setUsed(Int bufferNum, Bool used)
Definition: TComSlice.cpp:1800
Void clearSubstreamSizes()
Definition: TComSlice.h:1509
Bool getVclHrdParametersPresentFlag() const
Definition: TComSlice.h:324
Bool m_selfContainedCvsFlag
Definition: SEI.h:151
Void setDeblockingFilterTcOffsetDiv2(Int i)
Definition: TComSlice.h:1389
Bool m_exposureCompensationValueSignFlag
Definition: SEI.h:401
TComSEIMasteringDisplay values
Definition: SEI.h:476
Void create(const Int iPicWidth, const Int iPicHeight, const ChromaFormat chromaFormatIDC, const UInt uiMaxCUWidth, const UInt uiMaxCUHeight, const UInt uiMaxCUDepth)
Definition: TComPicYuv.cpp:81
Int m_frame1GridPositionX
Definition: SEI.h:277
Int getDeltaPocMSBCycleLT(Int i) const
Definition: TComSlice.h:90
picture YUV buffer class
Definition: TComPicYuv.h:55
Bool isTopField()
Definition: TComPic.h:153
SliceType getSliceType() const
Definition: TComSlice.h:1337
Void setDependentSliceSegmentFlag(Bool val)
Definition: TComSlice.h:1341
Void xAttachSliceDataToNalUnit(OutputNALUnit &rNalu, TComOutputBitstream *pcBitstreamRedirect)
Definition: TEncGOP.cpp:2526
Void setFrmRate(Double dFrameRate)
Definition: TEncAnalyze.h:90
Void setColFromL0Flag(Bool colFromL0)
Definition: TComSlice.h:1399
NalUnitType getNalUnitType(Int pocCurr, Int lastIdr, Bool isField)
Definition: TEncGOP.cpp:2422
Bool g_bFinalEncode
Int m_contentInterpretationType
Definition: SEI.h:268
encoder analyzer class (header)
Bool getTemporalLayerNonReferenceFlag()
Definition: TComSlice.h:1516
Int getSPSId() const
Definition: TComSlice.h:812
TEncRateCtrl * getRateCtrl()
Definition: TEncTop.h:154
Int getNumTileColumnsMinus1() const
Definition: TComSlice.h:1135
Bool isSlice()
Definition: NAL.h:67
GOP encoder class (header)
Int m_exposureIndexIdc
Definition: SEI.h:399
Bool m_frame1SelfContainedFlag
Definition: SEI.h:274
picture class (symbol + YUV buffers)
Definition: TComPic.h:56
Int m_exposureCompensationValueNumerator
Definition: SEI.h:402
TEncAnalyze m_gcAnalyzeP
Definition: TEncAnalyze.cpp:49
void Void
Definition: TypeDef.h:285
Int getRPSidx() const
Definition: TComSlice.h:1328
std::vector< Int > m_targetPivotValue
Definition: SEI.h:396
UInt calcChecksum(const TComPicYuv &pic, TComDigest &digest)
Int getUsed(Int bufferNum) const
Definition: TComSlice.cpp:1815
Bool m_fieldViewsFlag
Definition: SEI.h:271
const TComPPS & getPPS() const
Definition: TComPicSym.h:136
UInt m_dpbDelayOffset
Definition: SEI.h:178
Bool m_exactMatchingFlag
Definition: SEI.h:252
Int getStride(const ComponentID id) const
Definition: TComPicYuv.h:113
Bool m_arrangementPersistenceFlag
Definition: SEI.h:280
Void decodingRefreshMarking(Int &pocCRA, Bool &bRefreshPending, TComList< TComPic * > &rcListPic)
Definition: TComSlice.cpp:599
Void preLoopFilterPicAll(TComPic *pcPic, UInt64 &ruiDist)
Definition: TEncGOP.cpp:2055
UInt getTimeScale() const
Definition: TComSlice.h:413
Void setDeblockingFilterBetaOffsetDiv2(Int i)
Definition: TComSlice.h:1388
SEIChromaSamplingFilterHint * xCreateSEIChromaSamplingFilterHint(Bool bChromaLocInfoPresent, Int iHorFilterIndex, Int iVerFilterIdc)
Definition: TEncGOP.cpp:353
NalUnitType getAssociatedIRAPType() const
Definition: TComSlice.h:1336
#define NULL
Definition: CommonDef.h:100
Bool m_kneePersistenceFlag
Definition: SEI.h:420
Int & topLeftTileIndex(const Int tileRectIndex)
Definition: SEI.h:561
Void setEnableTMVPFlag(Bool b)
Definition: TComSlice.h:1521
Void xCalculateInterlacedAddPSNR(TComPic *pcPicOrgFirstField, TComPic *pcPicOrgSecondField, TComPicYuv *pcPicRecFirstField, TComPicYuv *pcPicRecSecondField, const AccessUnit &accessUnit, Double dEncTime, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE)
Definition: TEncGOP.cpp:2328
Int getHeight(const ComponentID id) const
Definition: TComPicYuv.h:109
UInt getBitsForPOC() const
Definition: TComSlice.h:857
TComSlice * getSlice(Int i)
Definition: TComPic.h:103
UInt m_picDpbOutputDelay
Definition: SEI.h:216
std::vector< Int > m_kneeOutputKneePoint
Definition: SEI.h:427
static UInt getComponentScaleY(const ComponentID id, const ChromaFormat fmt)
SEIDisplayOrientation * xCreateSEIDisplayOrientation()
Definition: TEncGOP.cpp:193
Int m_decodingUnitIdx
Definition: SEI.h:237
Bool m_frame0FlippedFlag
Definition: SEI.h:270
std::vector< Int > m_kneeInputKneePoint
Definition: SEI.h:426
char Char
Definition: TypeDef.h:291
unsigned int UInt
Definition: TypeDef.h:297
Void setDeltaPOC(Int bufferNum, Int deltaPOC)
Definition: TComSlice.cpp:1805
Void setSaoEnabledFlag(ChannelType chType, Bool s)
Definition: TComSlice.h:1321
Bool m_rapCpbParamsPresentFlag
Definition: SEI.h:176
Int m_frame0GridPositionX
Definition: SEI.h:275
UInt m_cpbDelayOffset
Definition: SEI.h:177
Bool getUseSAO() const
Definition: TComSlice.h:893
Bool getMvdL1ZeroFlag() const
Definition: TComSlice.h:1364
Void setRefPOCList()
Definition: TComSlice.cpp:293
Bool isStepwiseTemporalLayerSwitchingPointCandidate(TComList< TComPic * > &rcListPic)
Definition: TComSlice.cpp:826
Bool getPicDisableDeblockingFilterFlag() const
get offset for deblocking filter disabled
Definition: TComSlice.h:1153
Bool m_frame0SelfContainedFlag
Definition: SEI.h:273
Bool isReferenced() const
Definition: TComSlice.h:1368
Short Pel
pixel type
Definition: TypeDef.h:692
Void setTrQuant(TComTrQuant *pcTrQuant)
Definition: TComSlice.h:1312
Int m_numPivots
Definition: SEI.h:394
UInt getNumPic() const
Definition: TEncAnalyze.h:88
Void setNalUnitType(NalUnitType e)
Definition: TComSlice.h:1371
Int m_minValue
Definition: SEI.h:389
const TComRPSList * getRPSList() const
Definition: TComSlice.h:871
UChar m_nestingLayerId[64]
Definition: SEI.h:514
STL namespace.
UInt numClockTs
Definition: SEI.h:527
Void checkColRefIdx(UInt curSliceIdx, TComPic *pic)
Definition: TComSlice.cpp:513
TileSetData & tileSetData(const Int index)
Definition: SEI.h:584
TEncAnalyze m_gcAnalyzeI
Definition: TEncAnalyze.cpp:48
Bool getNoRaslOutputFlag() const
Definition: TComSlice.h:1457
TEncAnalyze m_gcAnalyzeAll_in
Definition: TEncAnalyze.cpp:52
TEncGOP()
Definition: TEncGOP.cpp:76
Bool m_verFilteringProcessFlag
Definition: SEI.h:458
TComRefPicListModification * getRefPicListModification()
Definition: TComSlice.h:1330
static Int getBeta(Int qp)
Void setSliceCurStartCtuTsAddr(UInt ctuTsAddr)
Definition: TComSlice.h:1466
SEIFramePacking * xCreateSEIFramePacking()
Definition: TEncGOP.cpp:159
Reference Picture Lists class.
Definition: TComSlice.h:951
Void setAssociatedIRAPPOC(Int iAssociatedIRAPPOC)
Definition: TComSlice.h:1333
TEncEntropy * getEntropyCoder()
Definition: TEncTop.h:146
Void setField(Bool b)
Definition: TComPic.h:154
Void setSliceIdx(UInt i)
Definition: TComSlice.h:1470
#define MAX_QP
Definition: CommonDef.h:127
Int m_nominalBlackLevelLumaCodeValue
Definition: SEI.h:406
Int getNumRefIdx(RefPicList e) const
Definition: TComSlice.h:1353
std::vector< Int > activeSeqParameterSetId
Definition: SEI.h:154
Void setLastIDR(Int iIDRPOC)
Definition: TComSlice.h:1331
class for counting bits
Bool getRapPicFlag() const
Definition: TComSlice.cpp:182
UInt getMinCUWidth() const
Definition: TComPic.h:122
Void setRefPicListModificationFlagL1(Bool flag)
Definition: TComSlice.h:969
Void setEncCABACTableIdx(SliceType idx)
Definition: TComSlice.h:1524
SEIToneMappingInfo * xCreateSEIToneMappingInfo()
Definition: TEncGOP.cpp:202
TComVUI * getVuiParameters()
Definition: TComSlice.h:939
Void applyReferencePictureSet(TComList< TComPic * > &rcListPic, const TComReferencePictureSet *RPSList)
Definition: TComSlice.cpp:1015
Void setSliceBits(UInt uiVal)
Definition: TComSlice.h:1481
Int m_sopDescPocDelta[1024]
Definition: SEI.h:373
Bool isInterB() const
Definition: TComSlice.h:1405
Bool getTilesEnabledFlag() const
Definition: TComSlice.h:1131
Void setLFCrossSliceBoundaryFlag(Bool val)
Definition: TComSlice.h:1518
Void addResult(Double psnr[MAX_NUM_COMPONENT], Double bits, const Double MSEyuvframe[MAX_NUM_COMPONENT])
Definition: TEncAnalyze.h:73
Void setRPSidx(Int iBDidx)
Definition: TComSlice.h:1327
TComPicYuv * getPicYuvRec()
Definition: TComPic.h:109
UInt m_picStruct
Definition: SEI.h:211
Void setRefPicListModificationFlagL0(Bool flag)
Definition: TComSlice.h:967
Int m_kneeOutputDrange
Definition: SEI.h:423
#define RVM_VCEGAM10_M
Definition: TypeDef.h:178
UInt m_initialCpbRemovalDelay[32][2]
Definition: SEI.h:179
Void setPOC(Int bufferNum, Int deltaPOC)
Definition: TComSlice.cpp:1835
Void xGetBuffer(TComList< TComPic * > &rcListPic, TComList< TComPicYuv * > &rcListPicYuvRecOut, Int iNumPicRcvd, Int iTimeOffset, TComPic *&rpcPic, TComPicYuv *&rpcPicYuvRecOut, Int pocCurr, Bool isField)
Definition: TEncGOP.cpp:2090
Bool getLoopFilterAcrossSlicesEnabledFlag() const
Definition: TComSlice.h:1169
Int m_frame1GridPositionY
Definition: SEI.h:278
Int m_kneeInputDispLuminance
Definition: SEI.h:422
UInt m_sopDescTemporalId[1024]
Definition: SEI.h:371
TEncAnalyze m_gcAnalyzeAll
Definition: TEncAnalyze.cpp:47
Int getWidth(const ComponentID id) const
Definition: TComPicYuv.h:108
Void destroy()
Definition: TEncGOP.cpp:123
Void setSliceType(SliceType e)
Definition: TComSlice.h:1378
UInt calcCRC(const TComPicYuv &pic, TComDigest &digest)
Void setNoRaslOutputFlag(Bool val)
Definition: TComSlice.h:1456
TEncSbac * getSbacCoder()
Definition: TEncTop.h:148
TComList< TComPic * > * getListPic()
Definition: TEncTop.h:137
Int Intermediate_Int
used as intermediate value in calculations
Definition: TypeDef.h:696
Void printOut(Char cDelim, const ChromaFormat chFmt, const Bool printMSEBasedSNR, const Bool printSequenceMSE)
Definition: TEncAnalyze.h:139
Void create()
Definition: TEncGOP.cpp:117
UInt calcMD5(const TComPicYuv &pic, TComDigest &digest)
std::vector< Int > m_startOfCodedInterval
Definition: SEI.h:393
TComLoopFilter * getLoopFilter()
Definition: TEncTop.h:141
SEIActiveParameterSets * xCreateSEIActiveParameterSets(const TComSPS *sps)
Definition: TEncGOP.cpp:147
Bool getTemporalIdNestingFlag() const
Definition: TComSlice.h:913
Int getQpBDOffset(ChannelType type) const
Definition: TComSlice.h:885
UInt m_numPicsInSopMinus1
Definition: SEI.h:368
Int m_maxValue
Definition: SEI.h:390
UInt getComponentScaleX(const ComponentID id) const
Definition: TComPicYuv.h:138
Void copySliceInfo(TComSlice *pcSliceSrc)
Definition: TComSlice.cpp:680
#define MAX_NUM_REF_PICS
max. number of pictures used for reference
Definition: CommonDef.h:112
bool Bool
Definition: TypeDef.h:286
UInt getSubstreamForCtuAddr(const UInt ctuAddr, const Bool bAddressInRaster, TComSlice *pcSlice)
Definition: TComPic.cpp:125
Bool getSubPicCpbParamsPresentFlag() const
Definition: TComSlice.h:327
UInt g_uiMaxCUHeight
Definition: TComRom.cpp:252
TComReferencePictureSet * getReferencePictureSet(Int referencePictureSetNum)
Definition: TComSlice.h:143
Bool m_allLayersFlag
Definition: SEI.h:511
Int m_sigmoidWidth
Definition: SEI.h:392
Void printOutSummary(UInt uiNumAllPicCoded, Bool isField, const Bool printMSEBasedSNR, const Bool printSequenceMSE)
Definition: TEncGOP.cpp:2002
UInt g_uiMaxCUWidth
Definition: TComRom.cpp:251
Int getAssociatedIRAPPOC() const
Definition: TComSlice.h:1334
#define EFFICIENT_FIELD_IRAP
Definition: TypeDef.h:86
UInt getMinCUHeight() const
Definition: TComPic.h:123
TEncAnalyze m_gcAnalyzeB
Definition: TEncAnalyze.cpp:50
PPS class.
Definition: TComSlice.h:977
TComDigest m_digest
Definition: SEI.h:134
Void copyToPic(TComPicYuv *pcPicYuvDst) const
Definition: TComPicYuv.cpp:176
Int m_nominalWhiteLevelLumaCodeValue
Definition: SEI.h:407
Void setDeltaPocMSBCycleLT(Int i, Int x)
Definition: TComSlice.h:91
Void setRPS(TComReferencePictureSet *pcRPS)
Definition: TComSlice.h:1323
Int m_cameraIsoSpeedIdc
Definition: SEI.h:397
UInt getPicHeightInLumaSamples() const
Definition: TComSlice.h:824
Void clearSliceBuffer()
Definition: TComPic.h:141
Void setAssociatedIRAPType(NalUnitType associatedIRAPType)
Definition: TComSlice.h:1335
Int & bottomRightTileIndex(const Int tileRectIndex)
Definition: SEI.h:562
UInt getSliceBits() const
Definition: TComSlice.h:1482
Void setDeltaPocMSBPresentFlag(Int i, Bool x)
Definition: TComSlice.h:93
UInt getNumberOfSubstreamSizes()
Definition: TComSlice.h:1510
Int getDeltaPOC(Int bufferNum) const
Definition: TComSlice.cpp:1820
Int m_toneMapId
Definition: SEI.h:383
UInt * m_duCpbRemovalDelayMinus1
Definition: SEI.h:222
static ChannelType toChannelType(const ComponentID id)
T Clip3(const T minVal, const T maxVal, const T a)
general min/max clip
Definition: CommonDef.h:137
UInt m_numDecodingUnitsMinus1
Definition: SEI.h:218
Bool m_quincunxSamplingFlag
Definition: SEI.h:267
UInt m_nestingNumOpsMinus1
Definition: SEI.h:507
TComPicYuv * getPicYuvOrg()
Definition: TComPic.h:108
reference list 0
Definition: TypeDef.h:417
Void setDeblockingFilterOverrideFlag(Bool b)
Definition: TComSlice.h:1387
Void createExplicitReferencePictureSetFromReference(TComList< TComPic * > &rcListPic, const TComReferencePictureSet *pReferencePictureSet, Bool isRAP, Int pocRandomAccess=0, Bool bUseRecoveryPoint=false)
Definition: TComSlice.cpp:1320
Int getComponentScaleX(const ComponentID id) const
Definition: TComPic.h:126
RefPicList
reference list index
Definition: TypeDef.h:415
UInt getNumberOfWrittenBits() const
Int xGetFirstSeiLocation(AccessUnit &accessUnit)
Definition: TEncGOP.cpp:2651
Int getSliceQpBase() const
Definition: TComSlice.h:1343
UInt m_auCpbRemovalDelayDelta
Definition: SEI.h:184
TComPicSym * getPicSym()
Definition: TComPic.h:102
ChromaFormat
chroma formats (according to semantics of chroma_format_idc)
Definition: TypeDef.h:352
TEncCavlc * getCavlcCoder()
Definition: TEncTop.h:147
UInt getSliceSegmentCurStartCtuTsAddr() const
Definition: TComSlice.h:1478
Bool m_arrangementCancelFlag
Definition: SEI.h:265
Int m_codedDataBitDepth
Definition: SEI.h:386
TEncSlice * getSliceEncoder()
Definition: TEncTop.h:144
const TComPPS * getPPS() const
Definition: TComSlice.h:1309
UInt m_nestingNoOpMaxTemporalIdPlus1
Definition: SEI.h:512
Int getDeblockingFilterBetaOffsetDiv2() const
get beta offset for deblocking filter
Definition: TComSlice.h:1155
UInt getPicWidthInLumaSamples() const
Definition: TComSlice.h:822
UInt64 xFindDistortionFrame(TComPicYuv *pcPic0, TComPicYuv *pcPic1)
Definition: TEncGOP.cpp:2134
Int m_extendedWhiteLevelLumaCodeValue
Definition: SEI.h:408
Void setNumberOfTileRects(const Int number)
Definition: SEI.h:549
UInt getNumberValidComponents() const
Definition: TComPicYuv.h:111
Int getNumberOfTileSets() const
Definition: SEI.h:582
virtual ~TEncGOP()
Definition: TEncGOP.cpp:111
Bool isIntra() const
Definition: TComSlice.h:1404
UInt anticlockwiseRotation
Definition: SEI.h:313
reference list 1
Definition: TypeDef.h:418
Bool m_spatialFlippingFlag
Definition: SEI.h:269
Double getBits() const
Definition: TEncAnalyze.h:86
Void arrangeLongtermPicturesInRPS(TComSlice *, TComList< TComPic * > &)
Definition: TEncGOP.cpp:2544
Void init(TEncTop *pcTEncTop)
Definition: TEncGOP.cpp:127
Void setNumberOfTileSets(const Int number)
Definition: SEI.h:581
Int getPOC() const
Definition: TComSlice.h:1338
const TComSPS & getSPS() const
Definition: TComPicSym.h:135
encoder class (header)
Bool getEntropyCodingSyncEnabledFlag() const
Definition: TComSlice.h:1127
Int getPOC() const
Definition: TComPic.h:104
TimingInfo * getTimingInfo()
Definition: TComSlice.h:717
Bool isInterP() const
Definition: TComSlice.h:1406
InputColourSpaceConversion
Definition: TypeDef.h:376
UInt getSliceIdx() const
Definition: TComSlice.h:1471
unsigned long long UInt64
Definition: TypeDef.h:318
UInt m_sopDescVclNaluType[1024]
Definition: SEI.h:370
UInt getCpbRemovalDelayLengthMinus1() const
Definition: TComSlice.h:353
Int g_bitDepth[MAX_NUM_CHANNEL_TYPE]
Definition: TComRom.cpp:548
Bool m_noDisplay
Definition: SEI.h:356
Void setPocLSBLT(Int i, Int x)
Definition: TComSlice.h:89
Int m_recoveryPocCnt
Definition: SEI.h:251
Void compressMotion()
Definition: TComPic.cpp:108
TComPic * getPic()
Definition: TComSlice.h:1354
UInt m_initialAltCpbRemovalDelayOffset[32][2]
Definition: SEI.h:182
UInt m_initialCpbRemovalDelayOffset[32][2]
Definition: SEI.h:180
Void xCreateLeadingSEIMessages(AccessUnit &accessUnit, const TComSPS *sps, const TComPPS *pps)
Definition: TEncGOP.cpp:406
Int getRefPOC(RefPicList e, Int iRefIdx)
Definition: TComSlice.h:1356
Int m_exposureIndexValue
Definition: SEI.h:400
Bool m_nestingOpFlag
Definition: SEI.h:505
Bool isTemporalLayerSwitchingPoint(TComList< TComPic * > &rcListPic)
Definition: TComSlice.cpp:805
Int getLSB(Int poc, Int maxLSB)
Definition: TEncGOP.cpp:64
Int getNumberOfNegativePictures() const
Definition: TComSlice.h:107
Void setReconMark(Bool b)
Definition: TComPic.h:131
Pel * getAddr(const ComponentID ch)
Definition: TComPicYuv.h:127
Int m_arrangementType
Definition: SEI.h:266
Bool m_duCommonCpbRemovalDelayFlag
Definition: SEI.h:219
Int m_exposureCompensationValueDenomIdc
Definition: SEI.h:403
Int m_arrangementId
Definition: SEI.h:264
UInt * m_numNalusInDuMinus1
Definition: SEI.h:221
std::vector< Int > m_codedPivotValue
Definition: SEI.h:395
#define DISTORTION_PRECISION_ADJUSTMENT(x)
Definition: TypeDef.h:269
Int m_kneeOutputDispLuminance
Definition: SEI.h:424
Void printSummary(const ChromaFormat chFmt, const Bool printSequenceMSE, Char ch='T')
Definition: TEncAnalyze.h:328
UInt getSliceSegmentCurEndCtuTsAddr() const
Definition: TComSlice.h:1480
Int m_sigmoidMidpoint
Definition: SEI.h:391
Int getPOC(Int bufferNum) const
Definition: TComSlice.cpp:1830
UInt m_auCpbRemovalDelay
Definition: SEI.h:215
enum SEIDecodedPictureHash::Method method
Int m_extendedRangeWhiteLevel
Definition: SEI.h:405
Bool m_perfectReconstructionFlag
Definition: SEI.h:460
UInt getNumberOfCtusInFrame() const
Definition: TComPicSym.h:132
UInt m_nestingNumLayersMinus1
Definition: SEI.h:513
SEIKneeFunctionInfo * xCreateSEIKneeFunctionInfo()
Definition: TEncGOP.cpp:323
Int getNumberOfPictures() const
Definition: TComSlice.cpp:1825
int Int
Definition: TypeDef.h:296
Void setSliceSegmentCurStartCtuTsAddr(UInt ctuTsAddr)
Definition: TComSlice.h:1477
Bool m_mc_all_tiles_exact_sample_value_match_flag
Definition: SEI.h:572
UInt getQuadtreeTULog2MaxSize() const
Definition: TComSlice.h:861
UInt getFrameHeightInCtus() const
Definition: TComPic.h:121
UInt getMaxTLayers() const
Definition: TComSlice.h:910
encoder class
Definition: TEncTop.h:68
Int getComponentScaleY(const ComponentID id) const
Definition: TComPic.h:127
ComponentID
Definition: TypeDef.h:368
const Double * getLambdas() const
Definition: TComSlice.h:1409
Int getSliceQp() const
Definition: TComSlice.h:1339
Void compressGOP(Int iPOCLast, Int iNumPicRcvd, TComList< TComPic * > &rcListPic, TComList< TComPicYuv * > &rcListPicYuvRec, std::list< AccessUnit > &accessUnitsInGOP, Bool isField, Bool isTff, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE)
Definition: TEncGOP.cpp:528
Int m_refScreenLuminanceWhite
Definition: SEI.h:404
Bool m_concatenationFlag
Definition: SEI.h:183
SEITempMotionConstrainedTileSets * xCreateSEITempMotionConstrainedTileSets(const TComPPS *pps)
Definition: TEncGOP.cpp:291
Bool m_brokenLinkFlag
Definition: SEI.h:253
Double xCalculateRVM()
Definition: TEncGOP.cpp:2474
Bool m_toneMapPersistenceFlag
Definition: SEI.h:385
NalUnitType
Definition: CommonDef.h:218
Int m_kneeInputDrange
Definition: SEI.h:421
Bool getLoopFilterAcrossTilesEnabledFlag() const
Definition: TComSlice.h:1124
Int m_kneeNumKneePointsMinus1
Definition: SEI.h:425
Int getNumberOfLongtermPictures() const
Definition: TComSlice.h:111
Int m_frame0GridPositionY
Definition: SEI.h:276
UInt m_picDpbOutputDuDelay
Definition: SEI.h:217
Void setDeblockingFilterDisable(Bool b)
Definition: TComSlice.h:1386
double Double
Definition: TypeDef.h:298
Bool m_toneMapCancelFlag
Definition: SEI.h:384
Int m_cameraIsoSpeedValue
Definition: SEI.h:398
UInt m_initialAltCpbRemovalDelay[32][2]
Definition: SEI.h:181
Void applyDeblockingFilterMetric(TComPic *pcPic, UInt uiNumSlices)
Definition: TEncGOP.cpp:2666
Bool m_noParameterSetUpdateFlag
Definition: SEI.h:152
Reference Picture Set class.
Definition: TComSlice.h:65
NalUnitType getNalUnitType() const
Definition: TComSlice.h:1372
Void setFinalized(Bool uiVal)
Definition: TComSlice.h:1485
slice header class
Definition: TComSlice.h:1198
Bool m_upsampledAspectRatio
Definition: SEI.h:281
Bool isIRAP() const
Definition: TComSlice.h:1375
UInt getTLayer() const
Definition: TComSlice.h:1434
Bool m_kneeCancelFlag
Definition: SEI.h:419
Void setNumRefIdx(RefPicList e, Int i)
Definition: TComSlice.h:1393
TEncBinCABAC * getBinCABAC()
Definition: TEncTop.h:149
Int m_arrangementReservedByte
Definition: SEI.h:279
Void addSubstream(TComOutputBitstream *pcSubstream)
Bool isField()
Definition: TComPic.h:155
Int getDeblockingFilterTcOffsetDiv2() const
get tc offset for deblocking filter
Definition: TComSlice.h:1157
Void setMvdL1ZeroFlag(Bool b)
Definition: TComSlice.h:1402
Void destroy()
Definition: TComPicYuv.cpp:146
TComPic * getRefPic(RefPicList e, Int iRefIdx)
Definition: TComSlice.h:1355
Int m_targetBitDepth
Definition: SEI.h:387
UInt getNumUnitsInTick() const
Definition: TComSlice.h:410
Bool m_bitStreamSubsetFlag
Definition: SEI.h:504
Void setBits(Double numBits)
Definition: TEncAnalyze.h:87
UInt m_sopSeqParameterSetId
Definition: SEI.h:367
Int checkThatAllRefPicsAreAvailable(TComList< TComPic * > &rcListPic, const TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess=0, Bool bUseRecoveryPoint=false)
Definition: TComSlice.cpp:1094
Definition: NAL.h:47
TComOutputBitstream m_Bitstream
Definition: NALwrite.h:74
TEncSampleAdaptiveOffset * getSAO()
Definition: TEncTop.h:142
TComHRD * getHrdParameters()
Definition: TComSlice.h:714
Bool getVuiParametersPresentFlag() const
Definition: TComSlice.h:937
Void setNumberOfLongtermPictures(Int number)
Definition: TComSlice.h:110
Void xInitGOP(Int iPOCLast, Int iNumPicRcvd, TComList< TComPic * > &rcListPic, TComList< TComPicYuv * > &rcListPicYuvRecOut, Bool isField)
Definition: TEncGOP.cpp:2072
UInt getSliceCurEndCtuTsAddr() const
Definition: TComSlice.h:1469
UInt getNumDU() const
Definition: TComSlice.h:386
TComSEITimeSet timeSetArray[3]
Definition: SEI.h:528
std::string digestToString(const TComDigest &digest, Int numChar)
Int getNumReorderPics(UInt tlayer) const
Definition: TComSlice.h:869
Void setCurrSliceIdx(UInt i)
Definition: TComPic.h:138
Void writeRBSPTrailingBits(TComOutputBitstream &bs)
Definition: NALwrite.cpp:128
const TComSPS * getSPS() const
Definition: TComSlice.h:1306
SEISegmentedRectFramePacking * xCreateSEISegmentedRectFramePacking()
Definition: TEncGOP.cpp:184
SPS class.
Definition: TComSlice.h:722
TComReferencePictureSet * getRPS()
Definition: TComSlice.h:1324
ChromaFormat getChromaFormat() const
Definition: TComPicYuv.h:110
UInt getTickDivisorMinus2() const
Definition: TComSlice.h:330
Bool m_arrangementPersistenceFlag
Definition: SEI.h:294
UInt m_sopDescStRpsIdx[1024]
Definition: SEI.h:372
Void allocateNewSlice()
Definition: TComPic.h:140