source: 3DVCSoftware/branches/HTM-16.1-dev/source/Lib/TLibEncoder/TEncGOP.cpp @ 1402

Last change on this file since 1402 was 1402, checked in by tech, 9 years ago

Initial merge of HM-16.9.

  • Property svn:eol-style set to native
File size: 116.5 KB
Line 
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-2016, 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
34/** \file     TEncGOP.cpp
35    \brief    GOP encoder class
36*/
37
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#include <deque>
53using namespace std;
54
55//! \ingroup TLibEncoder
56//! \{
57
58// ====================================================================================================================
59// Constructor / destructor / initialization / destroy
60// ====================================================================================================================
61Int getLSB(Int poc, Int maxLSB)
62{
63  if (poc >= 0)
64  {
65    return poc % maxLSB;
66  }
67  else
68  {
69    return (maxLSB - ((-poc) % maxLSB)) % maxLSB;
70  }
71}
72
73TEncGOP::TEncGOP()
74{
75  m_iLastIDR            = 0;
76  m_iGopSize            = 0;
77  m_iNumPicCoded        = 0; //Niko
78  m_bFirst              = true;
79  m_iLastRecoveryPicPOC = 0;
80
81  m_pcCfg               = NULL;
82  m_pcSliceEncoder      = NULL;
83  m_pcListPic           = NULL;
84
85  m_pcEntropyCoder      = NULL;
86  m_pcCavlcCoder        = NULL;
87  m_pcSbacCoder         = NULL;
88  m_pcBinCABAC          = NULL;
89
90  m_bSeqFirst           = true;
91
92  m_bRefreshPending     = 0;
93  m_pocCRA            = 0;
94  m_numLongTermRefPicSPS = 0;
95  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
96  ::memset(m_ltRefPicUsedByCurrPicFlag, 0, sizeof(m_ltRefPicUsedByCurrPicFlag));
97  m_lastBPSEI         = 0;
98  m_bufferingPeriodSEIPresentInAU = false;
99#if NH_MV
100  m_layerId      = 0;
101  m_viewId       = 0;
102  m_pocLastCoded = -1;
103#if NH_3D
104  m_viewIndex  =   0;
105  m_isDepth = false;
106#endif
107#endif
108  m_associatedIRAPType = NAL_UNIT_CODED_SLICE_IDR_N_LP;
109  m_associatedIRAPPOC  = 0;
110#if W0038_DB_OPT
111  m_pcDeblockingTempPicYuv = NULL;
112#endif
113  return;
114}
115
116TEncGOP::~TEncGOP()
117{
118}
119
120/** Create list to contain pointers to CTU start addresses of slice.
121 */
122Void  TEncGOP::create()
123{
124  m_bLongtermTestPictureHasBeenCoded = 0;
125  m_bLongtermTestPictureHasBeenCoded2 = 0;
126}
127
128Void  TEncGOP::destroy()
129{
130#if W0038_DB_OPT
131  if (m_pcDeblockingTempPicYuv)
132  {
133    m_pcDeblockingTempPicYuv->destroy();
134    delete m_pcDeblockingTempPicYuv;
135    m_pcDeblockingTempPicYuv = NULL;
136  }
137#endif
138}
139
140Void TEncGOP::init ( TEncTop* pcTEncTop )
141{
142  m_pcEncTop     = pcTEncTop;
143  m_pcCfg                = pcTEncTop;
144  m_seiEncoder.init(m_pcCfg, pcTEncTop, this);
145  m_pcSliceEncoder       = pcTEncTop->getSliceEncoder();
146  m_pcListPic            = pcTEncTop->getListPic();
147
148  m_pcEntropyCoder       = pcTEncTop->getEntropyCoder();
149  m_pcCavlcCoder         = pcTEncTop->getCavlcCoder();
150  m_pcSbacCoder          = pcTEncTop->getSbacCoder();
151  m_pcBinCABAC           = pcTEncTop->getBinCABAC();
152  m_pcLoopFilter         = pcTEncTop->getLoopFilter();
153
154  m_pcSAO                = pcTEncTop->getSAO();
155  m_pcRateCtrl           = pcTEncTop->getRateCtrl();
156  m_lastBPSEI          = 0;
157  m_totalCoded         = 0;
158
159#if NH_MV
160  m_ivPicLists           = pcTEncTop->getIvPicLists();
161  m_layerId              = pcTEncTop->getLayerId();
162  m_viewId               = pcTEncTop->getViewId();
163#if NH_3D
164  m_viewIndex            = pcTEncTop->getViewIndex();
165  m_isDepth              = pcTEncTop->getIsDepth();
166#endif
167#endif
168#if NH_3D_IC
169  m_aICEnableCandidate   = pcTEncTop->getICEnableCandidate();
170  m_aICEnableNum         = pcTEncTop->getICEnableNum();
171#endif
172#if KWU_FIX_URQ
173  m_pcRateCtrl           = pcTEncTop->getRateCtrl();
174#endif
175}
176
177Int TEncGOP::xWriteVPS (AccessUnit &accessUnit, const TComVPS *vps)
178{
179  OutputNALUnit nalu(NAL_UNIT_VPS);
180  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
181  m_pcEntropyCoder->encodeVPS(vps);
182  accessUnit.push_back(new NALUnitEBSP(nalu));
183  return (Int)(accessUnit.back()->m_nalUnitData.str().size()) * 8;
184}
185
186Int TEncGOP::xWriteSPS (AccessUnit &accessUnit, const TComSPS *sps)
187{
188#if NH_MV
189  OutputNALUnit nalu(NAL_UNIT_SPS, 0, getLayerId() );
190#else
191  OutputNALUnit nalu(NAL_UNIT_SPS);
192#endif
193  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
194  m_pcEntropyCoder->encodeSPS(sps);
195  accessUnit.push_back(new NALUnitEBSP(nalu));
196  return (Int)(accessUnit.back()->m_nalUnitData.str().size()) * 8;
197
198}
199
200Int TEncGOP::xWritePPS (AccessUnit &accessUnit, const TComPPS *pps)
201{
202#if NH_MV
203  OutputNALUnit nalu(NAL_UNIT_PPS, 0, getLayerId() );
204#else
205  OutputNALUnit nalu(NAL_UNIT_PPS);
206#endif
207  m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
208  m_pcEntropyCoder->encodePPS(pps);
209  accessUnit.push_back(new NALUnitEBSP(nalu));
210  return (Int)(accessUnit.back()->m_nalUnitData.str().size()) * 8;
211}
212
213
214Int TEncGOP::xWriteParameterSets (AccessUnit &accessUnit, TComSlice *slice)
215{
216  Int actualTotalBits = 0;
217
218#if NH_MV
219  if ( getLayerId() == 0 )
220  {
221    actualTotalBits += xWriteVPS(accessUnit, m_pcEncTop->getVPS());
222  }
223#else
224  actualTotalBits += xWriteVPS(accessUnit, m_pcEncTop->getVPS());
225#endif
226  actualTotalBits += xWriteSPS(accessUnit, slice->getSPS());
227  actualTotalBits += xWritePPS(accessUnit, slice->getPPS());
228
229  return actualTotalBits;
230}
231
232Void TEncGOP::xWriteAccessUnitDelimiter (AccessUnit &accessUnit, TComSlice *slice)
233{
234  AUDWriter audWriter;
235  OutputNALUnit nalu(NAL_UNIT_ACCESS_UNIT_DELIMITER);
236
237  Int picType = slice->isIntra() ? 0 : (slice->isInterP() ? 1 : 2);
238
239  audWriter.codeAUD(nalu.m_Bitstream, picType);
240  accessUnit.push_front(new NALUnitEBSP(nalu));
241}
242
243// write SEI list into one NAL unit and add it to the Access unit at auPos
244Void TEncGOP::xWriteSEI (NalUnitType naluType, SEIMessages& seiMessages, AccessUnit &accessUnit, AccessUnit::iterator &auPos, Int temporalId, const TComSPS *sps)
245{
246  // don't do anything, if we get an empty list
247  if (seiMessages.empty())
248  {
249    return;
250  }
251#if NH_MV
252  OutputNALUnit nalu(naluType, temporalId, getLayerId() );
253#else
254  OutputNALUnit nalu(naluType, temporalId);
255#endif
256  m_seiWriter.writeSEImessages(nalu.m_Bitstream, seiMessages, sps, false);
257  auPos = accessUnit.insert(auPos, new NALUnitEBSP(nalu));
258  auPos++;
259}
260
261Void TEncGOP::xWriteSEISeparately (NalUnitType naluType, SEIMessages& seiMessages, AccessUnit &accessUnit, AccessUnit::iterator &auPos, Int temporalId, const TComSPS *sps)
262{
263  // don't do anything, if we get an empty list
264  if (seiMessages.empty())
265  {
266    return;
267  }
268  for (SEIMessages::const_iterator sei = seiMessages.begin(); sei!=seiMessages.end(); sei++ )
269  {
270    SEIMessages tmpMessages;
271    tmpMessages.push_back(*sei);
272#if NH_MV
273    OutputNALUnit nalu(naluType, temporalId, getLayerId() );
274#else
275    OutputNALUnit nalu(naluType, temporalId);
276#endif
277    m_seiWriter.writeSEImessages(nalu.m_Bitstream, tmpMessages, sps, false);
278    auPos = accessUnit.insert(auPos, new NALUnitEBSP(nalu));
279    auPos++;
280  }
281}
282
283Void TEncGOP::xClearSEIs(SEIMessages& seiMessages, Bool deleteMessages)
284{
285  if (deleteMessages)
286  {
287    deleteSEIs(seiMessages);
288  }
289  else
290  {
291    seiMessages.clear();
292  }
293}
294
295// write SEI messages as separate NAL units ordered
296Void TEncGOP::xWriteLeadingSEIOrdered (SEIMessages& seiMessages, SEIMessages& duInfoSeiMessages, AccessUnit &accessUnit, Int temporalId, const TComSPS *sps, Bool testWrite)
297{
298  AccessUnit::iterator itNalu = accessUnit.begin();
299
300  while ( (itNalu!=accessUnit.end())&&
301    ( (*itNalu)->m_nalUnitType==NAL_UNIT_ACCESS_UNIT_DELIMITER
302    || (*itNalu)->m_nalUnitType==NAL_UNIT_VPS
303    || (*itNalu)->m_nalUnitType==NAL_UNIT_SPS
304    || (*itNalu)->m_nalUnitType==NAL_UNIT_PPS
305    ))
306  {
307    itNalu++;
308  }
309
310  SEIMessages localMessages = seiMessages;
311  SEIMessages currentMessages;
312
313#if ENC_DEC_TRACE
314  g_HLSTraceEnable = !testWrite;
315#endif
316  // The case that a specific SEI is not present is handled in xWriteSEI (empty list)
317
318  // Active parameter sets SEI must always be the first SEI
319  currentMessages = extractSeisByType(localMessages, SEI::ACTIVE_PARAMETER_SETS);
320  assert (currentMessages.size() <= 1);
321  xWriteSEI(NAL_UNIT_PREFIX_SEI, currentMessages, accessUnit, itNalu, temporalId, sps);
322  xClearSEIs(currentMessages, !testWrite);
323
324  // Buffering period SEI must always be following active parameter sets
325  currentMessages = extractSeisByType(localMessages, SEI::BUFFERING_PERIOD);
326  assert (currentMessages.size() <= 1);
327  xWriteSEI(NAL_UNIT_PREFIX_SEI, currentMessages, accessUnit, itNalu, temporalId, sps);
328  xClearSEIs(currentMessages, !testWrite);
329
330  // Picture timing SEI must always be following buffering period
331  currentMessages = extractSeisByType(localMessages, SEI::PICTURE_TIMING);
332  assert (currentMessages.size() <= 1);
333  xWriteSEI(NAL_UNIT_PREFIX_SEI, currentMessages, accessUnit, itNalu, temporalId, sps);
334  xClearSEIs(currentMessages, !testWrite);
335
336  // Decoding unit info SEI must always be following picture timing
337  if (!duInfoSeiMessages.empty())
338  {
339    currentMessages.push_back(duInfoSeiMessages.front());
340    if (!testWrite)
341    {
342      duInfoSeiMessages.pop_front();
343    }
344    xWriteSEI(NAL_UNIT_PREFIX_SEI, currentMessages, accessUnit, itNalu, temporalId, sps);
345    xClearSEIs(currentMessages, !testWrite);
346  }
347
348  // Scalable nesting SEI must always be the following DU info
349  currentMessages = extractSeisByType(localMessages, SEI::SCALABLE_NESTING);
350  xWriteSEISeparately(NAL_UNIT_PREFIX_SEI, currentMessages, accessUnit, itNalu, temporalId, sps);
351  xClearSEIs(currentMessages, !testWrite);
352
353#if NH_MV
354  // Layers not present SEI message
355  currentMessages = extractSeisByType(localMessages, SEI::LAYERS_NOT_PRESENT);
356  xWriteSEISeparately(NAL_UNIT_PREFIX_SEI, currentMessages, accessUnit, itNalu, temporalId, sps);
357  xClearSEIs(currentMessages, !testWrite);
358#endif
359
360  // And finally everything else one by one
361  xWriteSEISeparately(NAL_UNIT_PREFIX_SEI, localMessages, accessUnit, itNalu, temporalId, sps);
362  xClearSEIs(localMessages, !testWrite);
363
364  if (!testWrite)
365  {
366    seiMessages.clear();
367  }
368}
369
370
371Void TEncGOP::xWriteLeadingSEIMessages (SEIMessages& seiMessages, SEIMessages& duInfoSeiMessages, AccessUnit &accessUnit, Int temporalId, const TComSPS *sps, std::deque<DUData> &duData)
372{
373  AccessUnit testAU;
374  SEIMessages picTimingSEIs = getSeisByType(seiMessages, SEI::PICTURE_TIMING);
375  assert (picTimingSEIs.size() < 2);
376  SEIPictureTiming * picTiming = picTimingSEIs.empty() ? NULL : (SEIPictureTiming*) picTimingSEIs.front();
377
378  // test writing
379  xWriteLeadingSEIOrdered(seiMessages, duInfoSeiMessages, testAU, temporalId, sps, true);
380  // update Timing and DU info SEI
381  xUpdateDuData(testAU, duData);
382  xUpdateTimingSEI(picTiming, duData, sps);
383  xUpdateDuInfoSEI(duInfoSeiMessages, picTiming);
384  // actual writing
385  xWriteLeadingSEIOrdered(seiMessages, duInfoSeiMessages, accessUnit, temporalId, sps, false);
386
387  // testAU will automatically be cleaned up when losing scope
388}
389
390Void TEncGOP::xWriteTrailingSEIMessages (SEIMessages& seiMessages, AccessUnit &accessUnit, Int temporalId, const TComSPS *sps)
391{
392  // Note: using accessUnit.end() works only as long as this function is called after slice coding and before EOS/EOB NAL units
393  AccessUnit::iterator pos = accessUnit.end();
394  xWriteSEISeparately(NAL_UNIT_SUFFIX_SEI, seiMessages, accessUnit, pos, temporalId, sps);
395  deleteSEIs(seiMessages);
396}
397
398Void TEncGOP::xWriteDuSEIMessages (SEIMessages& duInfoSeiMessages, AccessUnit &accessUnit, Int temporalId, const TComSPS *sps, std::deque<DUData> &duData)
399{
400  const TComHRD *hrd = sps->getVuiParameters()->getHrdParameters();
401
402  if( m_pcCfg->getDecodingUnitInfoSEIEnabled() && hrd->getSubPicCpbParamsPresentFlag() )
403  {
404    Int naluIdx = 0;
405    AccessUnit::iterator nalu = accessUnit.begin();
406
407    // skip over first DU, we have a DU info SEI there already
408    while (naluIdx < duData[0].accumNalsDU && nalu!=accessUnit.end())
409    {
410      naluIdx++;
411      nalu++;
412    }
413
414    SEIMessages::iterator duSEI = duInfoSeiMessages.begin();
415    // loop over remaining DUs
416    for (Int duIdx = 1; duIdx < duData.size(); duIdx++)
417    {
418      if (duSEI == duInfoSeiMessages.end())
419      {
420        // if the number of generated SEIs matches the number of DUs, this should not happen
421        assert (false);
422        return;
423      }
424      // write the next SEI
425      SEIMessages tmpSEI;
426      tmpSEI.push_back(*duSEI);
427      xWriteSEI(NAL_UNIT_PREFIX_SEI, tmpSEI, accessUnit, nalu, temporalId, sps);
428      // nalu points to the position after the SEI, so we have to increase the index as well
429      naluIdx++;
430      while ((naluIdx < duData[duIdx].accumNalsDU) && nalu!=accessUnit.end())
431      {
432        naluIdx++;
433        nalu++;
434      }
435      duSEI++;
436    }
437  }
438  deleteSEIs(duInfoSeiMessages);
439}
440
441Void TEncGOP::xCreateIRAPLeadingSEIMessages (SEIMessages& seiMessages, const TComSPS *sps, const TComPPS *pps)
442{
443  OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI);
444
445  if(m_pcCfg->getActiveParameterSetsSEIEnabled())
446  {
447    SEIActiveParameterSets *sei = new SEIActiveParameterSets;
448    m_seiEncoder.initSEIActiveParameterSets (sei, m_pcCfg->getVPS(), sps);
449    seiMessages.push_back(sei);
450  }
451
452  if(m_pcCfg->getFramePackingArrangementSEIEnabled())
453  {
454    SEIFramePacking *sei = new SEIFramePacking;
455    m_seiEncoder.initSEIFramePacking (sei, m_iNumPicCoded);
456    seiMessages.push_back(sei);
457  }
458
459  if(m_pcCfg->getSegmentedRectFramePackingArrangementSEIEnabled())
460  {
461    SEISegmentedRectFramePacking *sei = new SEISegmentedRectFramePacking;
462    m_seiEncoder.initSEISegmentedRectFramePacking(sei);
463    seiMessages.push_back(sei);
464  }
465
466  if (m_pcCfg->getDisplayOrientationSEIAngle())
467  {
468    SEIDisplayOrientation *sei = new SEIDisplayOrientation;
469    m_seiEncoder.initSEIDisplayOrientation(sei);
470    seiMessages.push_back(sei);
471  }
472
473  if(m_pcCfg->getToneMappingInfoSEIEnabled())
474  {
475    SEIToneMappingInfo *sei = new SEIToneMappingInfo;
476    m_seiEncoder.initSEIToneMappingInfo (sei);
477    seiMessages.push_back(sei);
478  }
479
480  if(m_pcCfg->getTMCTSSEIEnabled())
481  {
482    SEITempMotionConstrainedTileSets *sei = new SEITempMotionConstrainedTileSets;
483    m_seiEncoder.initSEITempMotionConstrainedTileSets(sei, pps);
484    seiMessages.push_back(sei);
485  }
486
487  if(m_pcCfg->getTimeCodeSEIEnabled())
488  {
489    SEITimeCode *seiTimeCode = new SEITimeCode;
490    m_seiEncoder.initSEITimeCode(seiTimeCode);
491    seiMessages.push_back(seiTimeCode);
492  }
493
494  if(m_pcCfg->getKneeSEIEnabled())
495  {
496    SEIKneeFunctionInfo *sei = new SEIKneeFunctionInfo;
497    m_seiEncoder.initSEIKneeFunctionInfo(sei);
498    seiMessages.push_back(sei);
499  }
500
501  if(m_pcCfg->getMasteringDisplaySEI().colourVolumeSEIEnabled)
502  {
503    const TComSEIMasteringDisplay &seiCfg=m_pcCfg->getMasteringDisplaySEI();
504    SEIMasteringDisplayColourVolume *sei = new SEIMasteringDisplayColourVolume;
505    sei->values = seiCfg;
506    seiMessages.push_back(sei);
507  }
508
509  if(m_pcCfg->getChromaResamplingFilterHintEnabled())
510  {
511    SEIChromaResamplingFilterHint *seiChromaResamplingFilterHint = new SEIChromaResamplingFilterHint;
512    m_seiEncoder.initSEIChromaResamplingFilterHint(seiChromaResamplingFilterHint, m_pcCfg->getChromaResamplingHorFilterIdc(), m_pcCfg->getChromaResamplingVerFilterIdc());
513    seiMessages.push_back(seiChromaResamplingFilterHint);
514  }
515#if U0033_ALTERNATIVE_TRANSFER_CHARACTERISTICS_SEI
516  if(m_pcCfg->getSEIAlternativeTransferCharacteristicsSEIEnable())
517  {
518    SEIAlternativeTransferCharacteristics *seiAlternativeTransferCharacteristics = new SEIAlternativeTransferCharacteristics;
519    m_seiEncoder.initSEIAlternativeTransferCharacteristics(seiAlternativeTransferCharacteristics);
520    seiMessages.push_back(seiAlternativeTransferCharacteristics);
521  }
522#endif
523}
524
525Void TEncGOP::xCreatePerPictureSEIMessages (Int picInGOP, SEIMessages& seiMessages, SEIMessages& nestedSeiMessages, TComSlice *slice)
526{
527  if( ( m_pcCfg->getBufferingPeriodSEIEnabled() ) && ( slice->getSliceType() == I_SLICE ) &&
528    ( slice->getSPS()->getVuiParametersPresentFlag() ) &&
529    ( ( slice->getSPS()->getVuiParameters()->getHrdParameters()->getNalHrdParametersPresentFlag() )
530    || ( slice->getSPS()->getVuiParameters()->getHrdParameters()->getVclHrdParametersPresentFlag() ) ) )
531  {
532    SEIBufferingPeriod *bufferingPeriodSEI = new SEIBufferingPeriod();
533    m_seiEncoder.initSEIBufferingPeriod(bufferingPeriodSEI, slice);
534    seiMessages.push_back(bufferingPeriodSEI);
535    m_bufferingPeriodSEIPresentInAU = true;
536
537    if (m_pcCfg->getScalableNestingSEIEnabled())
538    {
539      SEIBufferingPeriod *bufferingPeriodSEIcopy = new SEIBufferingPeriod();
540      bufferingPeriodSEI->copyTo(*bufferingPeriodSEIcopy);
541      nestedSeiMessages.push_back(bufferingPeriodSEIcopy);
542    }
543  }
544
545  if (picInGOP ==0 && m_pcCfg->getSOPDescriptionSEIEnabled() ) // write SOP description SEI (if enabled) at the beginning of GOP
546  {
547    SEISOPDescription* sopDescriptionSEI = new SEISOPDescription();
548    m_seiEncoder.initSEISOPDescription(sopDescriptionSEI, slice, picInGOP, m_iLastIDR, m_iGopSize);
549    seiMessages.push_back(sopDescriptionSEI);
550  }
551
552  if( ( m_pcEncTop->getRecoveryPointSEIEnabled() ) && ( slice->getSliceType() == I_SLICE ) )
553  {
554    if( m_pcEncTop->getGradualDecodingRefreshInfoEnabled() && !slice->getRapPicFlag() )
555    {
556      // Gradual decoding refresh SEI
557      SEIGradualDecodingRefreshInfo *gradualDecodingRefreshInfoSEI = new SEIGradualDecodingRefreshInfo();
558      gradualDecodingRefreshInfoSEI->m_gdrForegroundFlag = true; // Indicating all "foreground"
559      seiMessages.push_back(gradualDecodingRefreshInfoSEI);
560    }
561    // Recovery point SEI
562    SEIRecoveryPoint *recoveryPointSEI = new SEIRecoveryPoint();
563    m_seiEncoder.initSEIRecoveryPoint(recoveryPointSEI, slice);
564    seiMessages.push_back(recoveryPointSEI);
565  }
566  if (m_pcCfg->getTemporalLevel0IndexSEIEnabled())
567  {
568    SEITemporalLevel0Index *temporalLevel0IndexSEI = new SEITemporalLevel0Index();
569    m_seiEncoder.initTemporalLevel0IndexSEI(temporalLevel0IndexSEI, slice);
570    seiMessages.push_back(temporalLevel0IndexSEI);
571  }
572
573  if( m_pcEncTop->getNoDisplaySEITLayer() && ( slice->getTLayer() >= m_pcEncTop->getNoDisplaySEITLayer() ) )
574  {
575    SEINoDisplay *seiNoDisplay = new SEINoDisplay;
576    seiNoDisplay->m_noDisplay = true;
577    seiMessages.push_back(seiNoDisplay);
578  }
579
580  // insert one Colour Remapping Info SEI for the picture (if the file exists)
581  if (!m_pcCfg->getColourRemapInfoSEIFileRoot().empty())
582  {
583    SEIColourRemappingInfo *seiColourRemappingInfo = new SEIColourRemappingInfo();
584    const Bool success = m_seiEncoder.initSEIColourRemappingInfo(seiColourRemappingInfo, slice->getPOC() );
585
586    if(success)
587    {
588      seiMessages.push_back(seiColourRemappingInfo);
589    }
590    else
591    {
592      delete seiColourRemappingInfo;
593    }
594  }
595}
596
597Void TEncGOP::xCreateScalableNestingSEI (SEIMessages& seiMessages, SEIMessages& nestedSeiMessages)
598{
599  SEIMessages tmpMessages;
600  while (!nestedSeiMessages.empty())
601  {
602    SEI* sei=nestedSeiMessages.front();
603    nestedSeiMessages.pop_front();
604    tmpMessages.push_back(sei);
605    SEIScalableNesting *nestingSEI = new SEIScalableNesting();
606    m_seiEncoder.initSEIScalableNesting(nestingSEI, tmpMessages);
607    seiMessages.push_back(nestingSEI);
608    tmpMessages.clear();
609  }
610}
611
612Void TEncGOP::xCreatePictureTimingSEI  (Int IRAPGOPid, SEIMessages& seiMessages, SEIMessages& nestedSeiMessages, SEIMessages& duInfoSeiMessages, TComSlice *slice, Bool isField, std::deque<DUData> &duData)
613{
614
615  const TComVUI *vui = slice->getSPS()->getVuiParameters();
616  const TComHRD *hrd = vui->getHrdParameters();
617
618  // update decoding unit parameters
619  if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) &&
620    ( slice->getSPS()->getVuiParametersPresentFlag() ) &&
621    (  hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() ) )
622  {
623    Int picSptDpbOutputDuDelay = 0;
624    SEIPictureTiming *pictureTimingSEI = new SEIPictureTiming();
625
626    // DU parameters
627    if( hrd->getSubPicCpbParamsPresentFlag() )
628    {
629      UInt numDU = (UInt) duData.size();
630      pictureTimingSEI->m_numDecodingUnitsMinus1     = ( numDU - 1 );
631      pictureTimingSEI->m_duCommonCpbRemovalDelayFlag = false;
632      pictureTimingSEI->m_numNalusInDuMinus1.resize( numDU );
633      pictureTimingSEI->m_duCpbRemovalDelayMinus1.resize( numDU );
634    }
635    pictureTimingSEI->m_auCpbRemovalDelay = std::min<Int>(std::max<Int>(1, m_totalCoded - m_lastBPSEI), static_cast<Int>(pow(2, static_cast<Double>(hrd->getCpbRemovalDelayLengthMinus1()+1)))); // Syntax element signalled as minus, hence the .
636    pictureTimingSEI->m_picDpbOutputDelay = slice->getSPS()->getNumReorderPics(slice->getSPS()->getMaxTLayers()-1) + slice->getPOC() - m_totalCoded;
637    if(m_pcCfg->getEfficientFieldIRAPEnabled() && IRAPGOPid > 0 && IRAPGOPid < m_iGopSize)
638    {
639      // if pictures have been swapped there is likely one more picture delay on their tid. Very rough approximation
640      pictureTimingSEI->m_picDpbOutputDelay ++;
641    }
642    Int factor = hrd->getTickDivisorMinus2() + 2;
643    pictureTimingSEI->m_picDpbOutputDuDelay = factor * pictureTimingSEI->m_picDpbOutputDelay;
644    if( m_pcCfg->getDecodingUnitInfoSEIEnabled() )
645    {
646      picSptDpbOutputDuDelay = factor * pictureTimingSEI->m_picDpbOutputDelay;
647    }
648    if (m_bufferingPeriodSEIPresentInAU)
649    {
650      m_lastBPSEI = m_totalCoded;
651    }
652
653    if( hrd->getSubPicCpbParamsPresentFlag() )
654    {
655      Int i;
656      UInt64 ui64Tmp;
657      UInt uiPrev = 0;
658      UInt numDU = ( pictureTimingSEI->m_numDecodingUnitsMinus1 + 1 );
659      std::vector<UInt> &rDuCpbRemovalDelayMinus1 = pictureTimingSEI->m_duCpbRemovalDelayMinus1;
660      UInt maxDiff = ( hrd->getTickDivisorMinus2() + 2 ) - 1;
661
662      for( i = 0; i < numDU; i ++ )
663      {
664        pictureTimingSEI->m_numNalusInDuMinus1[ i ]       = ( i == 0 ) ? ( duData[i].accumNalsDU - 1 ) : ( duData[i].accumNalsDU- duData[i-1].accumNalsDU - 1 );
665      }
666
667      if( numDU == 1 )
668      {
669        rDuCpbRemovalDelayMinus1[ 0 ] = 0; /* don't care */
670      }
671      else
672      {
673        rDuCpbRemovalDelayMinus1[ numDU - 1 ] = 0;/* by definition */
674        UInt tmp = 0;
675        UInt accum = 0;
676
677        for( i = ( numDU - 2 ); i >= 0; i -- )
678        {
679          ui64Tmp = ( ( ( duData[numDU - 1].accumBitsDU  - duData[i].accumBitsDU ) * ( vui->getTimingInfo()->getTimeScale() / vui->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) );
680          if( (UInt)ui64Tmp > maxDiff )
681          {
682            tmp ++;
683          }
684        }
685        uiPrev = 0;
686
687        UInt flag = 0;
688        for( i = ( numDU - 2 ); i >= 0; i -- )
689        {
690          flag = 0;
691          ui64Tmp = ( ( ( duData[numDU - 1].accumBitsDU  - duData[i].accumBitsDU ) * ( vui->getTimingInfo()->getTimeScale() / vui->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) );
692
693          if( (UInt)ui64Tmp > maxDiff )
694          {
695            if(uiPrev >= maxDiff - tmp)
696            {
697              ui64Tmp = uiPrev + 1;
698              flag = 1;
699            }
700            else                            ui64Tmp = maxDiff - tmp + 1;
701          }
702          rDuCpbRemovalDelayMinus1[ i ] = (UInt)ui64Tmp - uiPrev - 1;
703          if( (Int)rDuCpbRemovalDelayMinus1[ i ] < 0 )
704          {
705            rDuCpbRemovalDelayMinus1[ i ] = 0;
706          }
707          else if (tmp > 0 && flag == 1)
708          {
709            tmp --;
710          }
711          accum += rDuCpbRemovalDelayMinus1[ i ] + 1;
712          uiPrev = accum;
713        }
714      }
715    }
716
717    if( m_pcCfg->getPictureTimingSEIEnabled() )
718    {
719      pictureTimingSEI->m_picStruct = (isField && slice->getPic()->isTopField())? 1 : isField? 2 : 0;
720      seiMessages.push_back(pictureTimingSEI);
721
722      if ( m_pcCfg->getScalableNestingSEIEnabled() ) // put picture timing SEI into scalable nesting SEI
723      {
724          SEIPictureTiming *pictureTimingSEIcopy = new SEIPictureTiming();
725          pictureTimingSEI->copyTo(*pictureTimingSEIcopy);
726          nestedSeiMessages.push_back(pictureTimingSEIcopy);
727        }
728    }
729
730    if( m_pcCfg->getDecodingUnitInfoSEIEnabled() && hrd->getSubPicCpbParamsPresentFlag() )
731    {
732      for( Int i = 0; i < ( pictureTimingSEI->m_numDecodingUnitsMinus1 + 1 ); i ++ )
733      {
734        SEIDecodingUnitInfo *duInfoSEI = new SEIDecodingUnitInfo();
735        duInfoSEI->m_decodingUnitIdx = i;
736        duInfoSEI->m_duSptCpbRemovalDelay = pictureTimingSEI->m_duCpbRemovalDelayMinus1[i] + 1;
737        duInfoSEI->m_dpbOutputDuDelayPresentFlag = false;
738        duInfoSEI->m_picSptDpbOutputDuDelay = picSptDpbOutputDuDelay;
739
740        duInfoSeiMessages.push_back(duInfoSEI);
741      }
742    }
743
744    if( !m_pcCfg->getPictureTimingSEIEnabled() && pictureTimingSEI )
745    {
746      delete pictureTimingSEI;
747    }
748  }
749}
750
751Void TEncGOP::xUpdateDuData(AccessUnit &testAU, std::deque<DUData> &duData)
752{
753  if (duData.empty())
754  {
755    return;
756  }
757  // fix first
758  UInt numNalUnits = (UInt)testAU.size();
759  UInt numRBSPBytes = 0;
760  for (AccessUnit::const_iterator it = testAU.begin(); it != testAU.end(); it++)
761  {
762    numRBSPBytes += UInt((*it)->m_nalUnitData.str().size());
763  }
764  duData[0].accumBitsDU += ( numRBSPBytes << 3 );
765  duData[0].accumNalsDU += numNalUnits;
766
767  // adapt cumulative sums for all following DUs
768  // and add one DU info SEI, if enabled
769  for (Int i=1; i<duData.size(); i++)
770  {
771    if (m_pcCfg->getDecodingUnitInfoSEIEnabled())
772    {
773      numNalUnits  += 1;
774      numRBSPBytes += ( 5 << 3 );
775    }
776    duData[i].accumBitsDU += numRBSPBytes; // probably around 5 bytes
777    duData[i].accumNalsDU += numNalUnits;
778  }
779
780  // The last DU may have a trailing SEI
781  if (m_pcCfg->getDecodedPictureHashSEIType()!=HASHTYPE_NONE)
782  {
783    duData.back().accumBitsDU += ( 20 << 3 ); // probably around 20 bytes - should be further adjusted, e.g. by type
784    duData.back().accumNalsDU += 1;
785  }
786
787}
788Void TEncGOP::xUpdateTimingSEI(SEIPictureTiming *pictureTimingSEI, std::deque<DUData> &duData, const TComSPS *sps)
789{
790  if (!pictureTimingSEI)
791  {
792    return;
793  }
794  const TComVUI *vui = sps->getVuiParameters();
795  const TComHRD *hrd = vui->getHrdParameters();
796  if( hrd->getSubPicCpbParamsPresentFlag() )
797  {
798    Int i;
799    UInt64 ui64Tmp;
800    UInt uiPrev = 0;
801    UInt numDU = ( pictureTimingSEI->m_numDecodingUnitsMinus1 + 1 );
802    std::vector<UInt> &rDuCpbRemovalDelayMinus1 = pictureTimingSEI->m_duCpbRemovalDelayMinus1;
803    UInt maxDiff = ( hrd->getTickDivisorMinus2() + 2 ) - 1;
804
805    for( i = 0; i < numDU; i ++ )
806    {
807      pictureTimingSEI->m_numNalusInDuMinus1[ i ]       = ( i == 0 ) ? ( duData[i].accumNalsDU - 1 ) : ( duData[i].accumNalsDU- duData[i-1].accumNalsDU - 1 );
808    }
809
810    if( numDU == 1 )
811    {
812      rDuCpbRemovalDelayMinus1[ 0 ] = 0; /* don't care */
813    }
814    else
815    {
816      rDuCpbRemovalDelayMinus1[ numDU - 1 ] = 0;/* by definition */
817      UInt tmp = 0;
818      UInt accum = 0;
819
820      for( i = ( numDU - 2 ); i >= 0; i -- )
821      {
822        ui64Tmp = ( ( ( duData[numDU - 1].accumBitsDU  - duData[i].accumBitsDU ) * ( vui->getTimingInfo()->getTimeScale() / vui->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) );
823        if( (UInt)ui64Tmp > maxDiff )
824        {
825          tmp ++;
826        }
827      }
828      uiPrev = 0;
829
830      UInt flag = 0;
831      for( i = ( numDU - 2 ); i >= 0; i -- )
832      {
833        flag = 0;
834        ui64Tmp = ( ( ( duData[numDU - 1].accumBitsDU  - duData[i].accumBitsDU ) * ( vui->getTimingInfo()->getTimeScale() / vui->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) );
835
836        if( (UInt)ui64Tmp > maxDiff )
837        {
838          if(uiPrev >= maxDiff - tmp)
839          {
840            ui64Tmp = uiPrev + 1;
841            flag = 1;
842          }
843          else                            ui64Tmp = maxDiff - tmp + 1;
844        }
845        rDuCpbRemovalDelayMinus1[ i ] = (UInt)ui64Tmp - uiPrev - 1;
846        if( (Int)rDuCpbRemovalDelayMinus1[ i ] < 0 )
847        {
848          rDuCpbRemovalDelayMinus1[ i ] = 0;
849        }
850        else if (tmp > 0 && flag == 1)
851        {
852          tmp --;
853        }
854        accum += rDuCpbRemovalDelayMinus1[ i ] + 1;
855        uiPrev = accum;
856      }
857    }
858  }
859}
860Void TEncGOP::xUpdateDuInfoSEI(SEIMessages &duInfoSeiMessages, SEIPictureTiming *pictureTimingSEI)
861{
862  if (duInfoSeiMessages.empty() || (pictureTimingSEI == NULL))
863  {
864    return;
865  }
866
867  Int i=0;
868
869  for (SEIMessages::iterator du = duInfoSeiMessages.begin(); du!= duInfoSeiMessages.end(); du++)
870  {
871    SEIDecodingUnitInfo *duInfoSEI = (SEIDecodingUnitInfo*) (*du);
872    duInfoSEI->m_decodingUnitIdx = i;
873    duInfoSEI->m_duSptCpbRemovalDelay = pictureTimingSEI->m_duCpbRemovalDelayMinus1[i] + 1;
874    duInfoSEI->m_dpbOutputDuDelayPresentFlag = false;
875    i++;
876  }
877}
878
879static Void
880cabac_zero_word_padding(TComSlice *const pcSlice, TComPic *const pcPic, const std::size_t binCountsInNalUnits, const std::size_t numBytesInVclNalUnits, std::ostringstream &nalUnitData, const Bool cabacZeroWordPaddingEnabled)
881{
882  const TComSPS &sps=*(pcSlice->getSPS());
883  const Int log2subWidthCxsubHeightC = (pcPic->getComponentScaleX(COMPONENT_Cb)+pcPic->getComponentScaleY(COMPONENT_Cb));
884  const Int minCuWidth  = pcPic->getMinCUWidth();
885  const Int minCuHeight = pcPic->getMinCUHeight();
886  const Int paddedWidth = ((sps.getPicWidthInLumaSamples()  + minCuWidth  - 1) / minCuWidth) * minCuWidth;
887  const Int paddedHeight= ((sps.getPicHeightInLumaSamples() + minCuHeight - 1) / minCuHeight) * minCuHeight;
888  const Int rawBits = paddedWidth * paddedHeight *
889                         (sps.getBitDepth(CHANNEL_TYPE_LUMA) + 2*(sps.getBitDepth(CHANNEL_TYPE_CHROMA)>>log2subWidthCxsubHeightC));
890  const std::size_t threshold = (32/3)*numBytesInVclNalUnits + (rawBits/32);
891  if (binCountsInNalUnits >= threshold)
892  {
893    // need to add additional cabac zero words (each one accounts for 3 bytes (=00 00 03)) to increase numBytesInVclNalUnits
894    const std::size_t targetNumBytesInVclNalUnits = ((binCountsInNalUnits - (rawBits/32))*3+31)/32;
895
896    if (targetNumBytesInVclNalUnits>numBytesInVclNalUnits) // It should be!
897    {
898      const std::size_t numberOfAdditionalBytesNeeded=targetNumBytesInVclNalUnits - numBytesInVclNalUnits;
899      const std::size_t numberOfAdditionalCabacZeroWords=(numberOfAdditionalBytesNeeded+2)/3;
900      const std::size_t numberOfAdditionalCabacZeroBytes=numberOfAdditionalCabacZeroWords*3;
901      if (cabacZeroWordPaddingEnabled)
902      {
903        std::vector<UChar> zeroBytesPadding(numberOfAdditionalCabacZeroBytes, UChar(0));
904        for(std::size_t i=0; i<numberOfAdditionalCabacZeroWords; i++)
905        {
906          zeroBytesPadding[i*3+2]=3;  // 00 00 03
907        }
908        nalUnitData.write(reinterpret_cast<const TChar*>(&(zeroBytesPadding[0])), numberOfAdditionalCabacZeroBytes);
909        printf("Adding %d bytes of padding\n", UInt(numberOfAdditionalCabacZeroWords*3));
910      }
911      else
912      {
913        printf("Standard would normally require adding %d bytes of padding\n", UInt(numberOfAdditionalCabacZeroWords*3));
914      }
915    }
916  }
917}
918
919class EfficientFieldIRAPMapping
920{
921  private:
922    Int  IRAPGOPid;
923    Bool IRAPtoReorder;
924    Bool swapIRAPForward;
925
926  public:
927    EfficientFieldIRAPMapping() :
928      IRAPGOPid(-1),
929      IRAPtoReorder(false),
930      swapIRAPForward(false)
931    { }
932
933    Void initialize(const Bool isField, const Int gopSize, const Int POCLast, const Int numPicRcvd, const Int lastIDR, TEncGOP *pEncGop, TEncCfg *pCfg);
934
935    Int adjustGOPid(const Int gopID);
936    Int restoreGOPid(const Int gopID);
937    Int GetIRAPGOPid() const { return IRAPGOPid; }
938};
939
940Void EfficientFieldIRAPMapping::initialize(const Bool isField, const Int gopSize, const Int POCLast, const Int numPicRcvd, const Int lastIDR, TEncGOP *pEncGop, TEncCfg *pCfg )
941{
942  if(isField)
943  {
944    Int pocCurr;
945    for ( Int iGOPid=0; iGOPid < gopSize; iGOPid++ )
946    {
947      // determine actual POC
948      if(POCLast == 0) //case first frame or first top field
949      {
950        pocCurr=0;
951      }
952      else if(POCLast == 1 && isField) //case first bottom field, just like the first frame, the poc computation is not right anymore, we set the right value
953      {
954        pocCurr = 1;
955      }
956      else
957      {
958        pocCurr = POCLast - numPicRcvd + pCfg->getGOPEntry(iGOPid).m_POC - isField;
959      }
960
961      // check if POC corresponds to IRAP
962      NalUnitType tmpUnitType = pEncGop->getNalUnitType(pocCurr, lastIDR, isField);
963      if(tmpUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && tmpUnitType <= NAL_UNIT_CODED_SLICE_CRA) // if picture is an IRAP
964      {
965        if(pocCurr%2 == 0 && iGOPid < gopSize-1 && pCfg->getGOPEntry(iGOPid).m_POC == pCfg->getGOPEntry(iGOPid+1).m_POC-1)
966        { // if top field and following picture in enc order is associated bottom field
967          IRAPGOPid = iGOPid;
968          IRAPtoReorder = true;
969          swapIRAPForward = true;
970          break;
971        }
972        if(pocCurr%2 != 0 && iGOPid > 0 && pCfg->getGOPEntry(iGOPid).m_POC == pCfg->getGOPEntry(iGOPid-1).m_POC+1)
973        {
974          // if picture is an IRAP remember to process it first
975          IRAPGOPid = iGOPid;
976          IRAPtoReorder = true;
977          swapIRAPForward = false;
978          break;
979        }
980      }
981    }
982  }
983}
984
985Int EfficientFieldIRAPMapping::adjustGOPid(const Int GOPid)
986{
987  if(IRAPtoReorder)
988  {
989    if(swapIRAPForward)
990    {
991      if(GOPid == IRAPGOPid)
992      {
993        return IRAPGOPid +1;
994      }
995      else if(GOPid == IRAPGOPid +1)
996      {
997        return IRAPGOPid;
998      }
999    }
1000    else
1001    {
1002      if(GOPid == IRAPGOPid -1)
1003      {
1004        return IRAPGOPid;
1005      }
1006      else if(GOPid == IRAPGOPid)
1007      {
1008        return IRAPGOPid -1;
1009      }
1010    }
1011  }
1012  return GOPid;
1013}
1014
1015Int EfficientFieldIRAPMapping::restoreGOPid(const Int GOPid)
1016{
1017  if(IRAPtoReorder)
1018  {
1019    if(swapIRAPForward)
1020    {
1021      if(GOPid == IRAPGOPid)
1022      {
1023        IRAPtoReorder = false;
1024        return IRAPGOPid +1;
1025      }
1026      else if(GOPid == IRAPGOPid +1)
1027      {
1028        return GOPid -1;
1029      }
1030    }
1031    else
1032    {
1033      if(GOPid == IRAPGOPid)
1034      {
1035        return IRAPGOPid -1;
1036      }
1037      else if(GOPid == IRAPGOPid -1)
1038      {
1039        IRAPtoReorder = false;
1040        return IRAPGOPid;
1041      }
1042    }
1043  }
1044  return GOPid;
1045}
1046
1047
1048static UInt calculateCollocatedFromL1Flag(TEncCfg *pCfg, const Int GOPid, const Int gopSize)
1049{
1050  Int iCloseLeft=1, iCloseRight=-1;
1051  for(Int i = 0; i<pCfg->getGOPEntry(GOPid).m_numRefPics; i++)
1052  {
1053    Int iRef = pCfg->getGOPEntry(GOPid).m_referencePics[i];
1054    if(iRef>0&&(iRef<iCloseRight||iCloseRight==-1))
1055    {
1056      iCloseRight=iRef;
1057    }
1058    else if(iRef<0&&(iRef>iCloseLeft||iCloseLeft==1))
1059    {
1060      iCloseLeft=iRef;
1061    }
1062  }
1063  if(iCloseRight>-1)
1064  {
1065    iCloseRight=iCloseRight+pCfg->getGOPEntry(GOPid).m_POC-1;
1066  }
1067  if(iCloseLeft<1)
1068  {
1069    iCloseLeft=iCloseLeft+pCfg->getGOPEntry(GOPid).m_POC-1;
1070    while(iCloseLeft<0)
1071    {
1072      iCloseLeft+=gopSize;
1073    }
1074  }
1075  Int iLeftQP=0, iRightQP=0;
1076  for(Int i=0; i<gopSize; i++)
1077  {
1078    if(pCfg->getGOPEntry(i).m_POC==(iCloseLeft%gopSize)+1)
1079    {
1080      iLeftQP= pCfg->getGOPEntry(i).m_QPOffset;
1081    }
1082    if (pCfg->getGOPEntry(i).m_POC==(iCloseRight%gopSize)+1)
1083    {
1084      iRightQP=pCfg->getGOPEntry(i).m_QPOffset;
1085    }
1086  }
1087  if(iCloseRight>-1&&iRightQP<iLeftQP)
1088  {
1089    return 0;
1090  }
1091  else
1092  {
1093    return 1;
1094  }
1095}
1096
1097
1098static Void
1099printHash(const HashType hashType, const std::string &digestStr)
1100{
1101  const TChar *decodedPictureHashModeName;
1102  switch (hashType)
1103  {
1104    case HASHTYPE_MD5:
1105      decodedPictureHashModeName = "MD5";
1106      break;
1107    case HASHTYPE_CRC:
1108      decodedPictureHashModeName = "CRC";
1109      break;
1110    case HASHTYPE_CHECKSUM:
1111      decodedPictureHashModeName = "Checksum";
1112      break;
1113    default:
1114      decodedPictureHashModeName = NULL;
1115      break;
1116  }
1117  if (decodedPictureHashModeName != NULL)
1118  {
1119    if (digestStr.empty())
1120    {
1121      printf(" [%s:%s]", decodedPictureHashModeName, "?");
1122    }
1123    else
1124    {
1125      printf(" [%s:%s]", decodedPictureHashModeName, digestStr.c_str());
1126    }
1127  }
1128}
1129
1130// ====================================================================================================================
1131// Public member functions
1132// ====================================================================================================================
1133#if NH_MV
1134Void TEncGOP::initGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP)
1135{
1136  xInitGOP( iPOCLast, iNumPicRcvd, false );
1137  m_iNumPicCoded = 0;
1138}
1139#endif
1140#if NH_MV
1141Void TEncGOP::compressPicInGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic,
1142                                TComList<TComPicYuv*>& rcListPicYuvRecOut,  std::list<AccessUnit>& accessUnitsInGOP,
1143                                Bool isField, Bool isTff, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE, Int iGOPid )
1144#else
1145Void TEncGOP::compressGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic,
1146                           TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP,
1147                           Bool isField, Bool isTff, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE )
1148#endif
1149{
1150  // TODO: Split this function up.
1151
1152  TComPic*        pcPic = NULL;
1153  TComPicYuv*     pcPicYuvRecOut;
1154  TComSlice*      pcSlice;
1155  TComOutputBitstream  *pcBitstreamRedirect;
1156  pcBitstreamRedirect = new TComOutputBitstream;
1157  AccessUnit::iterator  itLocationToPushSliceHeaderNALU; // used to store location where NALU containing slice header is to be inserted
1158#if !NH_MV
1159  xInitGOP( iPOCLast, iNumPicRcvd, isField );
1160#endif
1161
1162  m_iNumPicCoded = 0;
1163  SEIMessages leadingSeiMessages;
1164  SEIMessages nestedSeiMessages;
1165  SEIMessages duInfoSeiMessages;
1166  SEIMessages trailingSeiMessages;
1167  std::deque<DUData> duData;
1168  SEIDecodingUnitInfo decodingUnitInfoSEI;
1169
1170  EfficientFieldIRAPMapping effFieldIRAPMap;
1171  if (m_pcCfg->getEfficientFieldIRAPEnabled())
1172  {
1173   effFieldIRAPMap.initialize(isField, m_iGopSize, iPOCLast, iNumPicRcvd, m_iLastIDR, this, m_pcCfg);
1174  }
1175
1176  // reset flag indicating whether pictures have been encoded
1177#if !NH_MV
1178  for ( Int iGOPid=0; iGOPid < m_iGopSize; iGOPid++ )
1179#endif
1180  {
1181    m_pcCfg->setEncodedFlag(iGOPid, false);
1182  }
1183#if !NH_MV
1184  for ( Int iGOPid=0; iGOPid < m_iGopSize; iGOPid++ )
1185#endif
1186  {
1187    if (m_pcCfg->getEfficientFieldIRAPEnabled())
1188    {
1189      iGOPid=effFieldIRAPMap.adjustGOPid(iGOPid);
1190    }
1191
1192    //-- For time output for each slice
1193    clock_t iBeforeTime = clock();
1194
1195    UInt uiColDir = calculateCollocatedFromL1Flag(m_pcCfg, iGOPid, m_iGopSize);
1196
1197    /////////////////////////////////////////////////////////////////////////////////////////////////// Initial to start encoding
1198    Int iTimeOffset;
1199    Int pocCurr;
1200    if(iPOCLast == 0) //case first frame or first top field
1201    {
1202      pocCurr=0;
1203      iTimeOffset = 1;
1204    }
1205    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
1206    {
1207      pocCurr = 1;
1208      iTimeOffset = 1;
1209    }
1210    else
1211    {
1212      pocCurr = iPOCLast - iNumPicRcvd + m_pcCfg->getGOPEntry(iGOPid).m_POC - ((isField && m_iGopSize>1) ? 1:0);
1213      iTimeOffset = m_pcCfg->getGOPEntry(iGOPid).m_POC;
1214    }
1215
1216    if(pocCurr>=m_pcCfg->getFramesToBeEncoded())
1217    {
1218      if (m_pcCfg->getEfficientFieldIRAPEnabled())
1219      {
1220        iGOPid=effFieldIRAPMap.restoreGOPid(iGOPid);
1221      }
1222#if NH_MV
1223      delete pcBitstreamRedirect;
1224      return;
1225#else
1226      continue;
1227#endif
1228    }
1229
1230    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 )
1231    {
1232      m_iLastIDR = pocCurr;
1233    }
1234    // start a new access unit: create an entry in the list of output access units
1235    accessUnitsInGOP.push_back(AccessUnit());
1236    AccessUnit& accessUnit = accessUnitsInGOP.back();
1237    xGetBuffer( rcListPic, rcListPicYuvRecOut, iNumPicRcvd, iTimeOffset, pcPic, pcPicYuvRecOut, pocCurr, isField );
1238
1239    //  Slice data initialization
1240    pcPic->clearSliceBuffer();
1241    pcPic->allocateNewSlice();
1242    m_pcSliceEncoder->setSliceIdx(0);
1243    pcPic->setCurrSliceIdx(0);
1244#if NH_MV
1245    m_pcSliceEncoder->initEncSlice ( pcPic, iPOCLast, pocCurr, iGOPid, pcSlice, m_pcEncTop->getVPS(), getLayerId(), isField  );
1246#else
1247    m_pcSliceEncoder->initEncSlice ( pcPic, iPOCLast, pocCurr, iGOPid, pcSlice, isField );
1248#endif
1249
1250    //Set Frame/Field coding
1251    pcSlice->getPic()->setField(isField);
1252
1253    pcSlice->setLastIDR(m_iLastIDR);
1254    pcSlice->setSliceIdx(0);
1255#if NH_MV
1256    pcSlice->setRefPicSetInterLayer ( &m_refPicSetInterLayer0, &m_refPicSetInterLayer1 );
1257    pcPic  ->setLayerId     ( getLayerId()   );
1258    pcPic  ->setViewId      ( getViewId()    );
1259#if !NH_3D
1260    pcSlice->setLayerId     ( getLayerId() );
1261    pcSlice->setViewId      ( getViewId()  );
1262    pcSlice->setVPS         ( m_pcEncTop->getVPS() );
1263#else
1264    pcPic  ->setViewIndex   ( getViewIndex() );
1265    pcPic  ->setIsDepth( getIsDepth() );
1266    pcSlice->setCamparaSlice( pcPic->getCodedScale(), pcPic->getCodedOffset() );
1267#endif
1268#endif
1269    //set default slice level flag to the same as SPS level flag
1270    pcSlice->setLFCrossSliceBoundaryFlag(  pcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag()  );
1271#if NH_MV
1272    // Set the nal unit type
1273    pcSlice->setNalUnitType(getNalUnitType(pocCurr, m_iLastIDR, isField));
1274    if( pcSlice->getSliceType() == B_SLICE )
1275    {
1276      if( m_pcCfg->getGOPEntry( ( pcSlice->getRapPicFlag() && getLayerId() > 0 ) ? MAX_GOP : iGOPid ).m_sliceType == 'P' )
1277      {
1278        pcSlice->setSliceType( P_SLICE );
1279      }
1280    }
1281
1282// To be checked!
1283    if( pcSlice->getSliceType() == B_SLICE )
1284    {
1285      if( m_pcCfg->getGOPEntry( ( pcSlice->getRapPicFlag() && getLayerId() > 0 ) ? MAX_GOP : iGOPid ).m_sliceType == 'I' )
1286      {
1287        pcSlice->setSliceType( I_SLICE );
1288      }
1289    }
1290#else
1291
1292    if(pcSlice->getSliceType()==B_SLICE&&m_pcCfg->getGOPEntry(iGOPid).m_sliceType=='P')
1293    {
1294      pcSlice->setSliceType(P_SLICE);
1295    }
1296    if(pcSlice->getSliceType()==B_SLICE&&m_pcCfg->getGOPEntry(iGOPid).m_sliceType=='I')
1297    {
1298      pcSlice->setSliceType(I_SLICE);
1299    }
1300
1301    // Set the nal unit type
1302    pcSlice->setNalUnitType(getNalUnitType(pocCurr, m_iLastIDR, isField));
1303#endif
1304    if(pcSlice->getTemporalLayerNonReferenceFlag())
1305    {
1306      if (pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_R &&
1307          !(m_iGopSize == 1 && pcSlice->getSliceType() == I_SLICE))
1308        // Add this condition to avoid POC issues with encoder_intra_main.cfg configuration (see #1127 in bug tracker)
1309      {
1310        pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TRAIL_N);
1311      }
1312      if(pcSlice->getNalUnitType()==NAL_UNIT_CODED_SLICE_RADL_R)
1313      {
1314        pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_RADL_N);
1315      }
1316      if(pcSlice->getNalUnitType()==NAL_UNIT_CODED_SLICE_RASL_R)
1317      {
1318        pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_RASL_N);
1319      }
1320    }
1321    if (m_pcCfg->getEfficientFieldIRAPEnabled())
1322    {
1323    if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1324      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1325      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
1326      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
1327      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
1328      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA )  // IRAP picture
1329    {
1330      m_associatedIRAPType = pcSlice->getNalUnitType();
1331      m_associatedIRAPPOC = pocCurr;
1332    }
1333    pcSlice->setAssociatedIRAPType(m_associatedIRAPType);
1334    pcSlice->setAssociatedIRAPPOC(m_associatedIRAPPOC);
1335    }
1336    // Do decoding refresh marking if any
1337    pcSlice->decodingRefreshMarking(m_pocCRA, m_bRefreshPending, rcListPic, m_pcCfg->getEfficientFieldIRAPEnabled());
1338    m_pcEncTop->selectReferencePictureSet(pcSlice, pocCurr, iGOPid);
1339    if (!m_pcCfg->getEfficientFieldIRAPEnabled())
1340    {
1341    if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1342      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1343      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
1344      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
1345      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
1346      || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA )  // IRAP picture
1347    {
1348      m_associatedIRAPType = pcSlice->getNalUnitType();
1349      m_associatedIRAPPOC = pocCurr;
1350    }
1351    pcSlice->setAssociatedIRAPType(m_associatedIRAPType);
1352    pcSlice->setAssociatedIRAPPOC(m_associatedIRAPPOC);
1353    }
1354    if ((pcSlice->checkThatAllRefPicsAreAvailable(rcListPic, pcSlice->getRPS(), false, m_iLastRecoveryPicPOC, m_pcCfg->getDecodingRefreshType() == 3) != 0) || (pcSlice->isIRAP())
1355      || (m_pcCfg->getEfficientFieldIRAPEnabled() && isField && pcSlice->getAssociatedIRAPType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getAssociatedIRAPType() <= NAL_UNIT_CODED_SLICE_CRA && pcSlice->getAssociatedIRAPPOC() == pcSlice->getPOC()+1)
1356      )
1357    {
1358      pcSlice->createExplicitReferencePictureSetFromReference(rcListPic, pcSlice->getRPS(), pcSlice->isIRAP(), m_iLastRecoveryPicPOC, m_pcCfg->getDecodingRefreshType() == 3, m_pcCfg->getEfficientFieldIRAPEnabled());
1359    }
1360
1361    pcSlice->applyReferencePictureSet(rcListPic, pcSlice->getRPS());
1362
1363    if(pcSlice->getTLayer() > 0
1364      &&  !( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N     // Check if not a leading picture
1365          || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_R
1366          || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N
1367          || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R )
1368        )
1369    {
1370      if(pcSlice->isTemporalLayerSwitchingPoint(rcListPic) || pcSlice->getSPS()->getTemporalIdNestingFlag())
1371      {
1372        if(pcSlice->getTemporalLayerNonReferenceFlag())
1373        {
1374          pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TSA_N);
1375        }
1376        else
1377        {
1378          pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TSA_R);
1379        }
1380      }
1381      else if(pcSlice->isStepwiseTemporalLayerSwitchingPointCandidate(rcListPic))
1382      {
1383        Bool isSTSA=true;
1384        for(Int ii=iGOPid+1;(ii<m_pcCfg->getGOPSize() && isSTSA==true);ii++)
1385        {
1386          Int lTid= m_pcCfg->getGOPEntry(ii).m_temporalId;
1387          if(lTid==pcSlice->getTLayer())
1388          {
1389            const TComReferencePictureSet* nRPS = pcSlice->getSPS()->getRPSList()->getReferencePictureSet(ii);
1390            for(Int jj=0;jj<nRPS->getNumberOfPictures();jj++)
1391            {
1392              if(nRPS->getUsed(jj))
1393              {
1394                Int tPoc=m_pcCfg->getGOPEntry(ii).m_POC+nRPS->getDeltaPOC(jj);
1395                Int kk=0;
1396                for(kk=0;kk<m_pcCfg->getGOPSize();kk++)
1397                {
1398                  if(m_pcCfg->getGOPEntry(kk).m_POC==tPoc)
1399                  {
1400                    break;
1401                  }
1402                }
1403                Int tTid=m_pcCfg->getGOPEntry(kk).m_temporalId;
1404                if(tTid >= pcSlice->getTLayer())
1405                {
1406                  isSTSA=false;
1407                  break;
1408                }
1409              }
1410            }
1411          }
1412        }
1413        if(isSTSA==true)
1414        {
1415          if(pcSlice->getTemporalLayerNonReferenceFlag())
1416          {
1417            pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_N);
1418          }
1419          else
1420          {
1421            pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_R);
1422          }
1423        }
1424      }
1425    }
1426    arrangeLongtermPicturesInRPS(pcSlice, rcListPic);
1427    TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
1428    refPicListModification->setRefPicListModificationFlagL0(0);
1429    refPicListModification->setRefPicListModificationFlagL1(0);
1430#if NH_MV
1431    if ( pcSlice->getPPS()->getNumExtraSliceHeaderBits() > 0 )
1432    {
1433      // Some more sophisticated algorithm to determine discardable_flag might be added here.
1434      pcSlice->setDiscardableFlag           ( false );
1435    }
1436
1437    const TComVPS*           vps = pcSlice->getVPS();
1438#if NH_3D
1439    Int numDirectRefLayers = vps    ->getNumRefListLayers( getLayerId() );
1440#else
1441    Int numDirectRefLayers = vps    ->getNumDirectRefLayers( getLayerId() );
1442#endif
1443#if NH_3D_QTL
1444    pcSlice->setIvPicLists( m_ivPicLists );
1445#endif
1446#if NH_3D
1447
1448
1449    Int gopNum = (pcSlice->getRapPicFlag() && getLayerId() > 0) ? MAX_GOP : iGOPid;
1450    GOPEntry gopEntry      = m_pcCfg->getGOPEntry( gopNum );
1451#else
1452    GOPEntry gopEntry      = m_pcCfg->getGOPEntry( (pcSlice->getRapPicFlag() && getLayerId() > 0) ? MAX_GOP : iGOPid );
1453#endif
1454
1455
1456
1457    Bool interLayerPredLayerIdcPresentFlag = false;
1458    if ( getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && numDirectRefLayers > 0 )
1459    {
1460      pcSlice->setInterLayerPredEnabledFlag ( gopEntry.m_numActiveRefLayerPics > 0 );
1461      if ( pcSlice->getInterLayerPredEnabledFlag() && numDirectRefLayers > 1 )
1462      {
1463        if ( !vps->getMaxOneActiveRefLayerFlag() )
1464        {
1465          pcSlice->setNumInterLayerRefPicsMinus1( gopEntry.m_numActiveRefLayerPics - 1 );
1466        }
1467#if NH_3D
1468        if ( gopEntry.m_numActiveRefLayerPics != vps->getNumRefListLayers( getLayerId() ) )
1469#else
1470        if ( gopEntry.m_numActiveRefLayerPics != vps->getNumDirectRefLayers( getLayerId() ) )
1471#endif
1472        {
1473          interLayerPredLayerIdcPresentFlag = true;
1474          for (Int i = 0; i < gopEntry.m_numActiveRefLayerPics; i++ )
1475          {
1476            pcSlice->setInterLayerPredLayerIdc( i, gopEntry.m_interLayerPredLayerIdc[ i ] );
1477          }
1478        }
1479      }
1480    }
1481    if ( !interLayerPredLayerIdcPresentFlag )
1482    {
1483      for( Int i = 0; i < pcSlice->getNumActiveRefLayerPics(); i++ )
1484      {
1485        pcSlice->setInterLayerPredLayerIdc(i, pcSlice->getRefLayerPicIdc( i ) );
1486      }
1487    }
1488
1489
1490    assert( pcSlice->getNumActiveRefLayerPics() == gopEntry.m_numActiveRefLayerPics );
1491
1492#if NH_3D
1493    if ( m_pcEncTop->decProcAnnexI() )
1494    {
1495      pcSlice->deriveInCmpPredAndCpAvailFlag( );
1496      if ( pcSlice->getInCmpPredAvailFlag() )
1497      {
1498        pcSlice->setInCompPredFlag( gopEntry.m_interCompPredFlag );
1499      }
1500      else
1501      {
1502        if (gopEntry.m_interCompPredFlag )
1503        {
1504          if ( gopNum == MAX_GOP)
1505          {
1506            printf( "\nError: FrameI_l%d cannot enable inter-component prediction on slice level. All reference layers need to be available and at least one tool using inter-component prediction must be enabled in the SPS. \n", pcSlice->getVPS()->getLayerIdInVps( getLayerId() ) );
1507          }
1508          else
1509          {
1510            printf( "\nError: Frame%d_l%d cannot enable inter-component prediction on slice level. All reference layers need to be available and at least one tool using inter-component prediction must be enabled in the SPS. \n", gopNum, pcSlice->getVPS()->getLayerIdInVps( getLayerId() ) );
1511          }
1512
1513          exit(EXIT_FAILURE);
1514        }
1515      }
1516      pcSlice->init3dToolParameters();
1517      pcSlice->checkInCompPredRefLayers();
1518    }
1519#if NH_3D_IV_MERGE
1520    // This needs to be done after initialization of 3D tool parameters.
1521    pcSlice->setMaxNumMergeCand      ( m_pcCfg->getMaxNumMergeCand()   + ( ( pcSlice->getMpiFlag( ) || pcSlice->getIvMvPredFlag( ) || pcSlice->getViewSynthesisPredFlag( )   ) ? 1 : 0 ));
1522#endif
1523#endif
1524
1525    pcSlice->createInterLayerReferencePictureSet( m_ivPicLists, m_refPicSetInterLayer0, m_refPicSetInterLayer1 );
1526    pcSlice->setNumRefIdx(REF_PIC_LIST_0,min(gopEntry.m_numRefPicsActive,( pcSlice->getRPS()->getNumberOfPictures() + (Int) m_refPicSetInterLayer0.size() + (Int) m_refPicSetInterLayer1.size()) ) );
1527    pcSlice->setNumRefIdx(REF_PIC_LIST_1,min(gopEntry.m_numRefPicsActive,( pcSlice->getRPS()->getNumberOfPictures() + (Int) m_refPicSetInterLayer0.size() + (Int) m_refPicSetInterLayer1.size()) ) );
1528
1529    std::vector< TComPic* >    tempRefPicLists[2];
1530    std::vector< Bool     >    usedAsLongTerm [2];
1531    Int       numPocTotalCurr;
1532
1533    pcSlice->getTempRefPicLists( rcListPic, m_refPicSetInterLayer0, m_refPicSetInterLayer1, tempRefPicLists, usedAsLongTerm, numPocTotalCurr, true );
1534
1535
1536    xSetRefPicListModificationsMv( tempRefPicLists, pcSlice, iGOPid );
1537#else
1538    pcSlice->setNumRefIdx(REF_PIC_LIST_0,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures()));
1539    pcSlice->setNumRefIdx(REF_PIC_LIST_1,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures()));
1540#endif
1541    //  Set reference list
1542#if NH_MV
1543    pcSlice->setRefPicList( tempRefPicLists, usedAsLongTerm, numPocTotalCurr );
1544#else
1545    pcSlice->setRefPicList ( rcListPic );
1546#endif
1547#if NH_3D_NBDV
1548    pcSlice->setDefaultRefView();
1549#endif
1550#if NH_3D_ARP
1551    //GT: This seems to be broken when layerId in vps is not equal to layerId in nuh
1552    pcSlice->setARPStepNum(m_ivPicLists);
1553#endif
1554#if NH_3D_IC
1555    pcSlice->setICEnableCandidate( m_aICEnableCandidate );
1556    pcSlice->setICEnableNum( m_aICEnableNum );
1557#endif
1558
1559    //  Slice info. refinement
1560#if NH_MV
1561    if ( pcSlice->getSliceType() == B_SLICE )
1562    {
1563      if( m_pcCfg->getGOPEntry( ( pcSlice->getRapPicFlag() == true && getLayerId() > 0 ) ? MAX_GOP : iGOPid ).m_sliceType == 'P' )
1564      {
1565        pcSlice->setSliceType( P_SLICE );
1566      }
1567    }
1568#else
1569    if ( (pcSlice->getSliceType() == B_SLICE) && (pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0) )
1570    {
1571      pcSlice->setSliceType ( P_SLICE );
1572    }
1573#endif
1574    pcSlice->setEncCABACTableIdx(m_pcSliceEncoder->getEncCABACTableIdx());
1575
1576    if (pcSlice->getSliceType() == B_SLICE)
1577    {
1578      pcSlice->setColFromL0Flag(1-uiColDir);
1579      Bool bLowDelay = true;
1580      Int  iCurrPOC  = pcSlice->getPOC();
1581      Int iRefIdx = 0;
1582
1583      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
1584      {
1585        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
1586        {
1587          bLowDelay = false;
1588        }
1589      }
1590      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
1591      {
1592        if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
1593        {
1594          bLowDelay = false;
1595        }
1596      }
1597
1598      pcSlice->setCheckLDC(bLowDelay);
1599    }
1600    else
1601    {
1602      pcSlice->setCheckLDC(true);
1603    }
1604
1605    uiColDir = 1-uiColDir;
1606
1607    //-------------------------------------------------------------
1608    pcSlice->setRefPOCList();
1609
1610    pcSlice->setList1IdxToList0Idx();
1611#if NH_3D_TMVP
1612    if(pcSlice->getLayerId())
1613      pcSlice->generateAlterRefforTMVP();
1614#endif
1615
1616    if (m_pcEncTop->getTMVPModeId() == 2)
1617    {
1618      if (iGOPid == 0) // first picture in SOP (i.e. forward B)
1619      {
1620        pcSlice->setEnableTMVPFlag(0);
1621      }
1622      else
1623      {
1624        // Note: pcSlice->getColFromL0Flag() is assumed to be always 0 and getcolRefIdx() is always 0.
1625        pcSlice->setEnableTMVPFlag(1);
1626      }
1627    }
1628    else if (m_pcEncTop->getTMVPModeId() == 1)
1629    {
1630      pcSlice->setEnableTMVPFlag(1);
1631    }
1632    else
1633    {
1634      pcSlice->setEnableTMVPFlag(0);
1635    }
1636#if NH_MV
1637    if( pcSlice->getIdrPicFlag() )
1638    {
1639      pcSlice->setEnableTMVPFlag(0);
1640    }
1641#endif
1642
1643#if NH_3D_VSO
1644  // Should be moved to TEncTop !!!
1645  Bool bUseVSO = m_pcEncTop->getUseVSO();
1646
1647  TComRdCost* pcRdCost = m_pcEncTop->getRdCost();
1648
1649  pcRdCost->setUseVSO( bUseVSO );
1650
1651  // SAIT_VSO_EST_A0033
1652  pcRdCost->setUseEstimatedVSD( m_pcEncTop->getUseEstimatedVSD() );
1653
1654  if ( bUseVSO )
1655  {
1656    Int iVSOMode = m_pcEncTop->getVSOMode();
1657    pcRdCost->setVSOMode( iVSOMode  );
1658    pcRdCost->setAllowNegDist( m_pcEncTop->getAllowNegDist() );
1659
1660    // SAIT_VSO_EST_A0033
1661#if H_3D_FCO
1662    Bool flagRec;
1663    flagRec =  ((m_pcEncTop->getIvPicLists()->getPicYuv( pcSlice->getViewIndex(), false, pcSlice->getPOC(), true) == NULL) ? false: true);
1664    pcRdCost->setVideoRecPicYuv( m_pcEncTop->getIvPicLists()->getPicYuv( pcSlice->getViewIndex(), false, pcSlice->getPOC(), flagRec ) );
1665    pcRdCost->setDepthPicYuv   ( m_pcEncTop->getIvPicLists()->getPicYuv( pcSlice->getViewIndex(), true, pcSlice->getPOC(), false ) );
1666#else   
1667    Int curAuxId     = pcSlice->getVPS()->getAuxId( getLayerId() ); 
1668    Int curDepthFlag = pcSlice->getIsDepth(); 
1669    assert( curAuxId == 2 || curDepthFlag  ); 
1670    pcRdCost->setVideoRecPicYuv( m_pcEncTop->getIvPicLists()->getPicYuv( pcSlice->getViewIndex(), false       , 0       , pcSlice->getPOC(), true ) );
1671    pcRdCost->setDepthPicYuv   ( m_pcEncTop->getIvPicLists()->getPicYuv( pcSlice->getViewIndex(), curDepthFlag, curAuxId, pcSlice->getPOC(), false ) );
1672#endif
1673    // LGE_WVSO_A0119
1674    Bool bUseWVSO  = m_pcEncTop->getUseWVSO();
1675    pcRdCost->setUseWVSO( bUseWVSO );
1676
1677  }
1678#endif
1679    // set adaptive search range for non-intra-slices
1680    if (m_pcCfg->getUseASR() && pcSlice->getSliceType()!=I_SLICE)
1681    {
1682      m_pcSliceEncoder->setSearchRange(pcSlice);
1683    }
1684
1685    Bool bGPBcheck=false;
1686    if ( pcSlice->getSliceType() == B_SLICE)
1687    {
1688      if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
1689      {
1690        bGPBcheck=true;
1691        Int i;
1692        for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
1693        {
1694          if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) )
1695          {
1696            bGPBcheck=false;
1697            break;
1698          }
1699        }
1700      }
1701    }
1702    if(bGPBcheck)
1703    {
1704      pcSlice->setMvdL1ZeroFlag(true);
1705    }
1706    else
1707    {
1708      pcSlice->setMvdL1ZeroFlag(false);
1709    }
1710    pcPic->getSlice(pcSlice->getSliceIdx())->setMvdL1ZeroFlag(pcSlice->getMvdL1ZeroFlag());
1711
1712
1713    Double lambda            = 0.0;
1714    Int actualHeadBits       = 0;
1715    Int actualTotalBits      = 0;
1716    Int estimatedBits        = 0;
1717    Int tmpBitsBeforeWriting = 0;
1718    if ( m_pcCfg->getUseRateCtrl() ) // TODO: does this work with multiple slices and slice-segments?
1719    {
1720      Int frameLevel = m_pcRateCtrl->getRCSeq()->getGOPID2Level( iGOPid );
1721      if ( pcPic->getSlice(0)->getSliceType() == I_SLICE )
1722      {
1723        frameLevel = 0;
1724      }
1725      m_pcRateCtrl->initRCPic( frameLevel );
1726      estimatedBits = m_pcRateCtrl->getRCPic()->getTargetBits();
1727
1728#if KWU_RC_MADPRED_E0227
1729      if(m_pcCfg->getLayerId() != 0)
1730      {
1731        m_pcRateCtrl->getRCPic()->setIVPic( m_pcEncTop->getEncTop()->getTEncTop(0)->getRateCtrl()->getRCPic() );
1732      }
1733#endif
1734
1735#if U0132_TARGET_BITS_SATURATION
1736      if (m_pcRateCtrl->getCpbSaturationEnabled() && frameLevel != 0)
1737      {
1738        Int estimatedCpbFullness = m_pcRateCtrl->getCpbState() + m_pcRateCtrl->getBufferingRate();
1739
1740        // prevent overflow
1741        if (estimatedCpbFullness - estimatedBits > (Int)(m_pcRateCtrl->getCpbSize()*0.9f))
1742        {
1743          estimatedBits = estimatedCpbFullness - (Int)(m_pcRateCtrl->getCpbSize()*0.9f);
1744        }
1745
1746        estimatedCpbFullness -= m_pcRateCtrl->getBufferingRate();
1747        // prevent underflow
1748#if V0078_ADAPTIVE_LOWER_BOUND
1749        if (estimatedCpbFullness - estimatedBits < m_pcRateCtrl->getRCPic()->getLowerBound())
1750        {
1751          estimatedBits = max(200, estimatedCpbFullness - m_pcRateCtrl->getRCPic()->getLowerBound());
1752        }
1753#else
1754        if (estimatedCpbFullness - estimatedBits < (Int)(m_pcRateCtrl->getCpbSize()*0.1f))
1755        {
1756          estimatedBits = max(200, estimatedCpbFullness - (Int)(m_pcRateCtrl->getCpbSize()*0.1f));
1757        }
1758#endif
1759
1760        m_pcRateCtrl->getRCPic()->setTargetBits(estimatedBits);
1761      }
1762#endif
1763
1764      Int sliceQP = m_pcCfg->getInitialQP();
1765      if ( ( pcSlice->getPOC() == 0 && m_pcCfg->getInitialQP() > 0 ) || ( frameLevel == 0 && m_pcCfg->getForceIntraQP() ) ) // QP is specified
1766      {
1767        Int    NumberBFrames = ( m_pcCfg->getGOPSize() - 1 );
1768        Double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(Double)NumberBFrames );
1769        Double dQPFactor     = 0.57*dLambda_scale;
1770        Int    SHIFT_QP      = 12;
1771        Int    bitdepth_luma_qp_scale = 0;
1772        Double qp_temp = (Double) sliceQP + bitdepth_luma_qp_scale - SHIFT_QP;
1773        lambda = dQPFactor*pow( 2.0, qp_temp/3.0 );
1774      }
1775      else if ( frameLevel == 0 )   // intra case, but use the model
1776      {
1777        m_pcSliceEncoder->calCostSliceI(pcPic); // TODO: This only analyses the first slice segment - what about the others?
1778
1779        if ( m_pcCfg->getIntraPeriod() != 1 )   // do not refine allocated bits for all intra case
1780        {
1781          Int bits = m_pcRateCtrl->getRCSeq()->getLeftAverageBits();
1782          bits = m_pcRateCtrl->getRCPic()->getRefineBitsForIntra( bits );
1783
1784#if U0132_TARGET_BITS_SATURATION
1785          if (m_pcRateCtrl->getCpbSaturationEnabled() )
1786          {
1787            Int estimatedCpbFullness = m_pcRateCtrl->getCpbState() + m_pcRateCtrl->getBufferingRate();
1788
1789            // prevent overflow
1790            if (estimatedCpbFullness - bits > (Int)(m_pcRateCtrl->getCpbSize()*0.9f))
1791            {
1792              bits = estimatedCpbFullness - (Int)(m_pcRateCtrl->getCpbSize()*0.9f);
1793            }
1794
1795            estimatedCpbFullness -= m_pcRateCtrl->getBufferingRate();
1796            // prevent underflow
1797#if V0078_ADAPTIVE_LOWER_BOUND
1798            if (estimatedCpbFullness - bits < m_pcRateCtrl->getRCPic()->getLowerBound())
1799            {
1800              bits = estimatedCpbFullness - m_pcRateCtrl->getRCPic()->getLowerBound();
1801            }
1802#else
1803            if (estimatedCpbFullness - bits < (Int)(m_pcRateCtrl->getCpbSize()*0.1f))
1804            {
1805              bits = estimatedCpbFullness - (Int)(m_pcRateCtrl->getCpbSize()*0.1f);
1806            }
1807#endif
1808          }
1809#endif
1810
1811          if ( bits < 200 )
1812          {
1813            bits = 200;
1814          }
1815          m_pcRateCtrl->getRCPic()->setTargetBits( bits );
1816        }
1817
1818        list<TEncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList();
1819        m_pcRateCtrl->getRCPic()->getLCUInitTargetBits();
1820        lambda  = m_pcRateCtrl->getRCPic()->estimatePicLambda( listPreviousPicture, pcSlice->getSliceType());
1821        sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture );
1822      }
1823      else    // normal case
1824      {
1825#if KWU_RC_MADPRED_E0227
1826        if(m_pcRateCtrl->getLayerID() != 0)
1827        {
1828          list<TEncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList();
1829          lambda  = m_pcRateCtrl->getRCPic()->estimatePicLambdaIV( listPreviousPicture, pcSlice->getPOC() );
1830          sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture );
1831        }
1832        else
1833        {
1834#endif
1835        list<TEncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList();
1836        lambda  = m_pcRateCtrl->getRCPic()->estimatePicLambda( listPreviousPicture, pcSlice->getSliceType());
1837        sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture );
1838#if KWU_RC_MADPRED_E0227
1839        }
1840#endif
1841      }
1842
1843      sliceQP = Clip3( -pcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, sliceQP );
1844      m_pcRateCtrl->getRCPic()->setPicEstQP( sliceQP );
1845
1846      m_pcSliceEncoder->resetQP( pcPic, sliceQP, lambda );
1847    }
1848
1849    UInt uiNumSliceSegments = 1;
1850
1851#if NH_3D_NBDV
1852      if(pcSlice->getViewIndex() && !pcSlice->getIsDepth()) //Notes from QC: this condition shall be changed once the configuration is completed, e.g. in pcSlice->getSPS()->getMultiviewMvPredMode() || ARP in prev. HTM. Remove this comment once it is done.
1853      {
1854        Int iColPoc = pcSlice->getRefPOC(RefPicList(1 - pcSlice->getColFromL0Flag()), pcSlice->getColRefIdx());
1855        pcPic->setNumDdvCandPics(pcPic->getDisCandRefPictures(iColPoc));
1856      }
1857#endif
1858#if NH_3D
1859      pcSlice->setDepthToDisparityLUTs();
1860
1861#endif
1862
1863#if NH_3D_NBDV
1864      if(pcSlice->getViewIndex() && !pcSlice->getIsDepth() && !pcSlice->isIntra()) //Notes from QC: this condition shall be changed once the configuration is completed, e.g. in pcSlice->getSPS()->getMultiviewMvPredMode() || ARP in prev. HTM. Remove this comment once it is done.
1865      {
1866        pcPic->checkTemporalIVRef();
1867      }
1868
1869      if(pcSlice->getIsDepth())
1870      {
1871        pcPic->checkTextureRef();
1872      }
1873#endif
1874    // Allocate some coders, now the number of tiles are known.
1875    const Int numSubstreamsColumns = (pcSlice->getPPS()->getNumTileColumnsMinus1() + 1);
1876    const Int numSubstreamRows     = pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag() ? pcPic->getFrameHeightInCtus() : (pcSlice->getPPS()->getNumTileRowsMinus1() + 1);
1877    const Int numSubstreams        = numSubstreamRows * numSubstreamsColumns;
1878    std::vector<TComOutputBitstream> substreamsOut(numSubstreams);
1879
1880    // now compress (trial encode) the various slice segments (slices, and dependent slices)
1881    {
1882      const UInt numberOfCtusInFrame=pcPic->getPicSym()->getNumberOfCtusInFrame();
1883      pcSlice->setSliceCurStartCtuTsAddr( 0 );
1884      pcSlice->setSliceSegmentCurStartCtuTsAddr( 0 );
1885
1886      for(UInt nextCtuTsAddr = 0; nextCtuTsAddr < numberOfCtusInFrame; )
1887      {
1888        m_pcSliceEncoder->precompressSlice( pcPic );
1889        m_pcSliceEncoder->compressSlice   ( pcPic, false, false );
1890
1891        const UInt curSliceSegmentEnd = pcSlice->getSliceSegmentCurEndCtuTsAddr();
1892        if (curSliceSegmentEnd < numberOfCtusInFrame)
1893        {
1894          const Bool bNextSegmentIsDependentSlice=curSliceSegmentEnd<pcSlice->getSliceCurEndCtuTsAddr();
1895          const UInt sliceBits=pcSlice->getSliceBits();
1896          pcPic->allocateNewSlice();
1897          // prepare for next slice
1898          pcPic->setCurrSliceIdx                    ( uiNumSliceSegments );
1899          m_pcSliceEncoder->setSliceIdx             ( uiNumSliceSegments   );
1900          pcSlice = pcPic->getSlice                 ( uiNumSliceSegments   );
1901          assert(pcSlice->getPPS()!=0);
1902          pcSlice->copySliceInfo                    ( pcPic->getSlice(uiNumSliceSegments-1)  );
1903          pcSlice->setSliceIdx                      ( uiNumSliceSegments   );
1904          if (bNextSegmentIsDependentSlice)
1905          {
1906            pcSlice->setSliceBits(sliceBits);
1907          }
1908          else
1909          {
1910            pcSlice->setSliceCurStartCtuTsAddr      ( curSliceSegmentEnd );
1911            pcSlice->setSliceBits(0);
1912          }
1913          pcSlice->setDependentSliceSegmentFlag(bNextSegmentIsDependentSlice);
1914          pcSlice->setSliceSegmentCurStartCtuTsAddr ( curSliceSegmentEnd );
1915          // TODO: optimise cabac_init during compress slice to improve multi-slice operation
1916          // pcSlice->setEncCABACTableIdx(m_pcSliceEncoder->getEncCABACTableIdx());
1917          uiNumSliceSegments ++;
1918        }
1919        nextCtuTsAddr = curSliceSegmentEnd;
1920      }
1921    }
1922
1923    duData.clear();
1924    pcSlice = pcPic->getSlice(0);
1925
1926    // SAO parameter estimation using non-deblocked pixels for CTU bottom and right boundary areas
1927    if( pcSlice->getSPS()->getUseSAO() && m_pcCfg->getSaoCtuBoundary() )
1928    {
1929      m_pcSAO->getPreDBFStatistics(pcPic);
1930    }
1931
1932    //-- Loop filter
1933    Bool bLFCrossTileBoundary = pcSlice->getPPS()->getLoopFilterAcrossTilesEnabledFlag();
1934    m_pcLoopFilter->setCfg(bLFCrossTileBoundary);
1935    if ( m_pcCfg->getDeblockingFilterMetric() )
1936    {
1937#if W0038_DB_OPT
1938      if ( m_pcCfg->getDeblockingFilterMetric()==2 )
1939      {
1940        applyDeblockingFilterParameterSelection(pcPic, uiNumSliceSegments, iGOPid);
1941      }
1942      else
1943      {
1944#endif
1945      applyDeblockingFilterMetric(pcPic, uiNumSliceSegments);
1946#if W0038_DB_OPT
1947      }
1948#endif
1949    }
1950    m_pcLoopFilter->loopFilterPic( pcPic );
1951
1952    /////////////////////////////////////////////////////////////////////////////////////////////////// File writing
1953    // Set entropy coder
1954    m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder );
1955    if ( m_bSeqFirst )
1956    {
1957      // write various parameter sets
1958      actualTotalBits += xWriteParameterSets(accessUnit, pcSlice);
1959#if H_3D_PPS_FIX_DEPTH
1960      if(!pcSlice->getIsDepth() || !pcSlice->getViewIndex() )
1961      {
1962#endif
1963#if H_3D_PPS_FIX_DEPTH
1964      }
1965#endif
1966
1967
1968      // create prefix SEI messages at the beginning of the sequence
1969      assert(leadingSeiMessages.empty());
1970      xCreateIRAPLeadingSEIMessages(leadingSeiMessages, pcSlice->getSPS(), pcSlice->getPPS());
1971
1972      m_bSeqFirst = false;
1973    }
1974    if (m_pcCfg->getAccessUnitDelimiter())
1975    {
1976      xWriteAccessUnitDelimiter(accessUnit, pcSlice);
1977    }
1978
1979    // reset presence of BP SEI indication
1980    m_bufferingPeriodSEIPresentInAU = false;
1981    // create prefix SEI associated with a picture
1982    xCreatePerPictureSEIMessages(iGOPid, leadingSeiMessages, nestedSeiMessages, pcSlice);
1983
1984#if NH_MV
1985    m_seiEncoder.createAnnexFGISeiMessages( leadingSeiMessages, pcSlice );
1986#endif
1987
1988    /* use the main bitstream buffer for storing the marshalled picture */
1989    m_pcEntropyCoder->setBitstream(NULL);
1990
1991    pcSlice = pcPic->getSlice(0);
1992
1993    if (pcSlice->getSPS()->getUseSAO())
1994    {
1995      Bool sliceEnabled[MAX_NUM_COMPONENT];
1996      TComBitCounter tempBitCounter;
1997      tempBitCounter.resetBits();
1998      m_pcEncTop->getRDGoOnSbacCoder()->setBitstream(&tempBitCounter);
1999      m_pcSAO->initRDOCabacCoder(m_pcEncTop->getRDGoOnSbacCoder(), pcSlice);
2000#if OPTIONAL_RESET_SAO_ENCODING_AFTER_IRAP
2001      m_pcSAO->SAOProcess(pcPic, sliceEnabled, pcPic->getSlice(0)->getLambdas(),
2002                          m_pcCfg->getTestSAODisableAtPictureLevel(),
2003                          m_pcCfg->getSaoEncodingRate(),
2004                          m_pcCfg->getSaoEncodingRateChroma(),
2005                          m_pcCfg->getSaoCtuBoundary(),
2006                          m_pcCfg->getSaoResetEncoderStateAfterIRAP());
2007#else
2008      m_pcSAO->SAOProcess(pcPic, sliceEnabled, pcPic->getSlice(0)->getLambdas(), m_pcCfg->getTestSAODisableAtPictureLevel(), m_pcCfg->getSaoEncodingRate(), m_pcCfg->getSaoEncodingRateChroma(), m_pcCfg->getSaoCtuBoundary());
2009#endif
2010      m_pcSAO->PCMLFDisableProcess(pcPic);
2011      m_pcEncTop->getRDGoOnSbacCoder()->setBitstream(NULL);
2012
2013      //assign SAO slice header
2014      for(Int s=0; s< uiNumSliceSegments; s++)
2015      {
2016        pcPic->getSlice(s)->setSaoEnabledFlag(CHANNEL_TYPE_LUMA, sliceEnabled[COMPONENT_Y]);
2017        assert(sliceEnabled[COMPONENT_Cb] == sliceEnabled[COMPONENT_Cr]);
2018        pcPic->getSlice(s)->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, sliceEnabled[COMPONENT_Cb]);
2019      }
2020    }
2021
2022    // pcSlice is currently slice 0.
2023    std::size_t binCountsInNalUnits   = 0; // For implementation of cabac_zero_word stuffing (section 7.4.3.10)
2024    std::size_t numBytesInVclNalUnits = 0; // For implementation of cabac_zero_word stuffing (section 7.4.3.10)
2025
2026    for( UInt sliceSegmentStartCtuTsAddr = 0, sliceIdxCount=0; sliceSegmentStartCtuTsAddr < pcPic->getPicSym()->getNumberOfCtusInFrame(); sliceIdxCount++, sliceSegmentStartCtuTsAddr=pcSlice->getSliceSegmentCurEndCtuTsAddr() )
2027    {
2028      pcSlice = pcPic->getSlice(sliceIdxCount);
2029      if(sliceIdxCount > 0 && pcSlice->getSliceType()!= I_SLICE)
2030      {
2031        pcSlice->checkColRefIdx(sliceIdxCount, pcPic);
2032      }
2033      pcPic->setCurrSliceIdx(sliceIdxCount);
2034      m_pcSliceEncoder->setSliceIdx(sliceIdxCount);
2035
2036      pcSlice->setRPS(pcPic->getSlice(0)->getRPS());
2037      pcSlice->setRPSidx(pcPic->getSlice(0)->getRPSidx());
2038
2039      for ( UInt ui = 0 ; ui < numSubstreams; ui++ )
2040      {
2041        substreamsOut[ui].clear();
2042      }
2043
2044      m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder );
2045      m_pcEntropyCoder->resetEntropy      ( pcSlice );
2046      /* start slice NALunit */
2047#if NH_MV
2048      OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->getTLayer(), getLayerId() );
2049#else
2050      OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->getTLayer() );
2051#endif
2052      m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
2053
2054      pcSlice->setNoRaslOutputFlag(false);
2055      if (pcSlice->isIRAP())
2056      {
2057        if (pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_IDR_N_LP)
2058        {
2059          pcSlice->setNoRaslOutputFlag(true);
2060        }
2061        //the inference for NoOutputPriorPicsFlag
2062        // KJS: This cannot happen at the encoder
2063        if (!m_bFirst && pcSlice->isIRAP() && pcSlice->getNoRaslOutputFlag())
2064        {
2065          if (pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA)
2066          {
2067            pcSlice->setNoOutputPriorPicsFlag(true);
2068          }
2069        }
2070      }
2071
2072      pcSlice->setEncCABACTableIdx(m_pcSliceEncoder->getEncCABACTableIdx());
2073
2074      tmpBitsBeforeWriting = m_pcEntropyCoder->getNumberOfWrittenBits();
2075      m_pcEntropyCoder->encodeSliceHeader(pcSlice);
2076      actualHeadBits += ( m_pcEntropyCoder->getNumberOfWrittenBits() - tmpBitsBeforeWriting );
2077
2078      pcSlice->setFinalized(true);
2079
2080      pcSlice->clearSubstreamSizes(  );
2081      {
2082        UInt numBinsCoded = 0;
2083        m_pcSliceEncoder->encodeSlice(pcPic, &(substreamsOut[0]), numBinsCoded);
2084        binCountsInNalUnits+=numBinsCoded;
2085      }
2086
2087      {
2088        // Construct the final bitstream by concatenating substreams.
2089        // The final bitstream is either nalu.m_Bitstream or pcBitstreamRedirect;
2090        // Complete the slice header info.
2091        m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder );
2092        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
2093        m_pcEntropyCoder->encodeTilesWPPEntryPoint( pcSlice );
2094
2095        // Append substreams...
2096        TComOutputBitstream *pcOut = pcBitstreamRedirect;
2097        const Int numZeroSubstreamsAtStartOfSlice  = pcPic->getSubstreamForCtuAddr(pcSlice->getSliceSegmentCurStartCtuTsAddr(), false, pcSlice);
2098        const Int numSubstreamsToCode  = pcSlice->getNumberOfSubstreamSizes()+1;
2099        for ( UInt ui = 0 ; ui < numSubstreamsToCode; ui++ )
2100        {
2101          pcOut->addSubstream(&(substreamsOut[ui+numZeroSubstreamsAtStartOfSlice]));
2102        }
2103      }
2104
2105      // If current NALU is the first NALU of slice (containing slice header) and more NALUs exist (due to multiple dependent slices) then buffer it.
2106      // 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.
2107      Bool bNALUAlignedWrittenToList    = false; // used to ensure current NALU is not written more than once to the NALU list.
2108      xAttachSliceDataToNalUnit(nalu, pcBitstreamRedirect);
2109      accessUnit.push_back(new NALUnitEBSP(nalu));
2110      actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8;
2111      numBytesInVclNalUnits += (std::size_t)(accessUnit.back()->m_nalUnitData.str().size());
2112      bNALUAlignedWrittenToList = true;
2113
2114      if (!bNALUAlignedWrittenToList)
2115      {
2116        nalu.m_Bitstream.writeAlignZero();
2117        accessUnit.push_back(new NALUnitEBSP(nalu));
2118      }
2119
2120      if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) &&
2121          ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) &&
2122          ( ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNalHrdParametersPresentFlag() )
2123         || ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getVclHrdParametersPresentFlag() ) ) &&
2124          ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getSubPicCpbParamsPresentFlag() ) )
2125      {
2126          UInt numNalus = 0;
2127        UInt numRBSPBytes = 0;
2128        for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
2129        {
2130          numRBSPBytes += UInt((*it)->m_nalUnitData.str().size());
2131          numNalus ++;
2132        }
2133        duData.push_back(DUData());
2134        duData.back().accumBitsDU = ( numRBSPBytes << 3 );
2135        duData.back().accumNalsDU = numNalus;
2136      }
2137    } // end iteration over slices
2138
2139    // cabac_zero_words processing
2140    cabac_zero_word_padding(pcSlice, pcPic, binCountsInNalUnits, numBytesInVclNalUnits, accessUnit.back()->m_nalUnitData, m_pcCfg->getCabacZeroWordPaddingEnabled());
2141#if NH_3D
2142      pcPic->compressMotion(2);
2143#else
2144    pcPic->compressMotion();
2145#endif
2146#if NH_MV
2147      m_pocLastCoded = pcPic->getPOC();
2148#endif
2149
2150    //-- For time output for each slice
2151    Double dEncTime = (Double)(clock()-iBeforeTime) / CLOCKS_PER_SEC;
2152
2153    std::string digestStr;
2154    if (m_pcCfg->getDecodedPictureHashSEIType()!=HASHTYPE_NONE)
2155    {
2156      SEIDecodedPictureHash *decodedPictureHashSei = new SEIDecodedPictureHash();
2157      m_seiEncoder.initDecodedPictureHashSEI(decodedPictureHashSei, pcPic, digestStr, pcSlice->getSPS()->getBitDepths());
2158      trailingSeiMessages.push_back(decodedPictureHashSei);
2159    }
2160    xWriteTrailingSEIMessages(trailingSeiMessages, accessUnit, pcSlice->getTLayer(), pcSlice->getSPS());
2161
2162    m_pcCfg->setEncodedFlag(iGOPid, true);
2163
2164    xCalculateAddPSNRs( isField, isTff, iGOPid, pcPic, accessUnit, rcListPic, dEncTime, snr_conversion, printFrameMSE );
2165
2166    printHash(m_pcCfg->getDecodedPictureHashSEIType(), digestStr);
2167
2168    if ( m_pcCfg->getUseRateCtrl() )
2169    {
2170      Double avgQP     = m_pcRateCtrl->getRCPic()->calAverageQP();
2171      Double avgLambda = m_pcRateCtrl->getRCPic()->calAverageLambda();
2172      if ( avgLambda < 0.0 )
2173      {
2174        avgLambda = lambda;
2175      }
2176
2177      m_pcRateCtrl->getRCPic()->updateAfterPicture( actualHeadBits, actualTotalBits, avgQP, avgLambda, pcSlice->getSliceType());
2178      m_pcRateCtrl->getRCPic()->addToPictureLsit( m_pcRateCtrl->getPicList() );
2179
2180      m_pcRateCtrl->getRCSeq()->updateAfterPic( actualTotalBits );
2181      if ( pcSlice->getSliceType() != I_SLICE )
2182      {
2183        m_pcRateCtrl->getRCGOP()->updateAfterPicture( actualTotalBits );
2184      }
2185      else    // for intra picture, the estimated bits are used to update the current status in the GOP
2186      {
2187        m_pcRateCtrl->getRCGOP()->updateAfterPicture( estimatedBits );
2188      }
2189#if U0132_TARGET_BITS_SATURATION
2190      if (m_pcRateCtrl->getCpbSaturationEnabled())
2191      {
2192        m_pcRateCtrl->updateCpbState(actualTotalBits);
2193        printf(" [CPB %6d bits]", m_pcRateCtrl->getCpbState());
2194      }
2195#endif
2196    }
2197
2198    xCreatePictureTimingSEI(m_pcCfg->getEfficientFieldIRAPEnabled()?effFieldIRAPMap.GetIRAPGOPid():0, leadingSeiMessages, nestedSeiMessages, duInfoSeiMessages, pcSlice, isField, duData);
2199    if (m_pcCfg->getScalableNestingSEIEnabled())
2200    {
2201      xCreateScalableNestingSEI (leadingSeiMessages, nestedSeiMessages);
2202    }
2203    xWriteLeadingSEIMessages(leadingSeiMessages, duInfoSeiMessages, accessUnit, pcSlice->getTLayer(), pcSlice->getSPS(), duData);
2204    xWriteDuSEIMessages(duInfoSeiMessages, accessUnit, pcSlice->getTLayer(), pcSlice->getSPS(), duData);
2205
2206    pcPic->getPicYuvRec()->copyToPic(pcPicYuvRecOut);
2207
2208    pcPic->setReconMark   ( true );
2209#if NH_MV
2210      TComSlice::markIvRefPicsAsShortTerm( m_refPicSetInterLayer0, m_refPicSetInterLayer1 );
2211      std::vector<Int> temp;
2212      TComSlice::markCurrPic( pcPic );
2213#endif
2214    m_bFirst = false;
2215    m_iNumPicCoded++;
2216    m_totalCoded ++;
2217    /* logging: insert a newline at end of picture period */
2218    printf("\n");
2219    fflush(stdout);
2220
2221    if (m_pcCfg->getEfficientFieldIRAPEnabled())
2222    {
2223    iGOPid=effFieldIRAPMap.restoreGOPid(iGOPid);
2224    }
2225  } // iGOPid-loop
2226
2227  delete pcBitstreamRedirect;
2228
2229#if !NH_MV
2230  assert ( (m_iNumPicCoded == iNumPicRcvd) );
2231#endif
2232}
2233
2234Void TEncGOP::printOutSummary(UInt uiNumAllPicCoded, Bool isField, const Bool printMSEBasedSNR, const Bool printSequenceMSE, const BitDepths &bitDepths)
2235{
2236  assert (uiNumAllPicCoded == m_gcAnalyzeAll.getNumPic());
2237
2238
2239  //--CFG_KDY
2240  const Int rateMultiplier=(isField?2:1);
2241  m_gcAnalyzeAll.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier / (Double)m_pcCfg->getTemporalSubsampleRatio());
2242  m_gcAnalyzeI.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier / (Double)m_pcCfg->getTemporalSubsampleRatio());
2243  m_gcAnalyzeP.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier / (Double)m_pcCfg->getTemporalSubsampleRatio());
2244  m_gcAnalyzeB.setFrmRate( m_pcCfg->getFrameRate()*rateMultiplier / (Double)m_pcCfg->getTemporalSubsampleRatio());
2245  const ChromaFormat chFmt = m_pcCfg->getChromaFormatIdc();
2246
2247  //-- all
2248#if NH_MV
2249  printf( "\n\nSUMMARY -------------------------------------------- LayerId %2d\n", getLayerId() );
2250#else
2251  printf( "\n\nSUMMARY --------------------------------------------------------\n" );
2252#endif
2253  m_gcAnalyzeAll.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
2254
2255  printf( "\n\nI Slices--------------------------------------------------------\n" );
2256  m_gcAnalyzeI.printOut('i', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
2257
2258  printf( "\n\nP Slices--------------------------------------------------------\n" );
2259  m_gcAnalyzeP.printOut('p', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
2260
2261  printf( "\n\nB Slices--------------------------------------------------------\n" );
2262  m_gcAnalyzeB.printOut('b', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
2263
2264  if (!m_pcCfg->getSummaryOutFilename().empty())
2265  {
2266    m_gcAnalyzeAll.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryOutFilename());
2267  }
2268
2269  if (!m_pcCfg->getSummaryPicFilenameBase().empty())
2270  {
2271    m_gcAnalyzeI.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"I.txt");
2272    m_gcAnalyzeP.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"P.txt");
2273    m_gcAnalyzeB.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryPicFilenameBase()+"B.txt");
2274  }
2275
2276  if(isField)
2277  {
2278    //-- interlaced summary
2279#if TEMPORAL_SUBSAMPLE
2280    m_gcAnalyzeAll_in.setFrmRate( m_pcCfg->getFrameRate() / (Double)m_pcCfg->getTemporalSubsampleRatio());
2281#else
2282    m_gcAnalyzeAll_in.setFrmRate( m_pcCfg->getFrameRate());
2283#endif
2284    m_gcAnalyzeAll_in.setBits(m_gcAnalyzeAll.getBits());
2285    // prior to the above statement, the interlace analyser does not contain the correct total number of bits.
2286
2287    printf( "\n\nSUMMARY INTERLACED ---------------------------------------------\n" );
2288    m_gcAnalyzeAll_in.printOut('a', chFmt, printMSEBasedSNR, printSequenceMSE, bitDepths);
2289
2290    if (!m_pcCfg->getSummaryOutFilename().empty())
2291    {
2292      m_gcAnalyzeAll_in.printSummary(chFmt, printSequenceMSE, bitDepths, m_pcCfg->getSummaryOutFilename());
2293    }
2294  }
2295
2296  printf("\nRVM: %.3lf\n" , xCalculateRVM());
2297}
2298
2299#if NH_3D_VSO
2300Void TEncGOP::preLoopFilterPicAll( TComPic* pcPic, Dist64& ruiDist )
2301#else
2302Void TEncGOP::preLoopFilterPicAll( TComPic* pcPic, UInt64& ruiDist )
2303#endif
2304{
2305  Bool bCalcDist = false;
2306  m_pcLoopFilter->setCfg(m_pcCfg->getLFCrossTileBoundaryFlag());
2307  m_pcLoopFilter->loopFilterPic( pcPic );
2308
2309  if (!bCalcDist)
2310  {
2311    ruiDist = xFindDistortionFrame(pcPic->getPicYuvOrg(), pcPic->getPicYuvRec(), pcPic->getPicSym()->getSPS().getBitDepths());
2312  }
2313}
2314
2315// ====================================================================================================================
2316// Protected member functions
2317// ====================================================================================================================
2318
2319
2320Void TEncGOP::xInitGOP( Int iPOCLast, Int iNumPicRcvd, Bool isField )
2321{
2322  assert( iNumPicRcvd > 0 );
2323  //  Exception for the first frames
2324  if ( ( isField && (iPOCLast == 0 || iPOCLast == 1) ) || (!isField  && (iPOCLast == 0))  )
2325  {
2326    m_iGopSize    = 1;
2327  }
2328  else
2329  {
2330    m_iGopSize    = m_pcCfg->getGOPSize();
2331  }
2332  assert (m_iGopSize > 0);
2333
2334  return;
2335}
2336
2337
2338Void TEncGOP::xGetBuffer( TComList<TComPic*>&      rcListPic,
2339                         TComList<TComPicYuv*>&    rcListPicYuvRecOut,
2340                         Int                       iNumPicRcvd,
2341                         Int                       iTimeOffset,
2342                         TComPic*&                 rpcPic,
2343                         TComPicYuv*&              rpcPicYuvRecOut,
2344                         Int                       pocCurr,
2345                         Bool                      isField)
2346{
2347  Int i;
2348  //  Rec. output
2349  TComList<TComPicYuv*>::iterator     iterPicYuvRec = rcListPicYuvRecOut.end();
2350
2351  if (isField && pocCurr > 1 && m_iGopSize!=1)
2352  {
2353    iTimeOffset--;
2354  }
2355
2356  for ( i = 0; i < (iNumPicRcvd - iTimeOffset + 1); i++ )
2357  {
2358    iterPicYuvRec--;
2359  }
2360
2361  rpcPicYuvRecOut = *(iterPicYuvRec);
2362
2363  //  Current pic.
2364  TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
2365  while (iterPic != rcListPic.end())
2366  {
2367    rpcPic = *(iterPic);
2368    rpcPic->setCurrSliceIdx(0);
2369    if (rpcPic->getPOC() == pocCurr)
2370    {
2371      break;
2372    }
2373    iterPic++;
2374  }
2375
2376#if !NH_MV
2377  assert (rpcPic != NULL);
2378#endif
2379  assert (rpcPic->getPOC() == pocCurr);
2380
2381  return;
2382}
2383
2384#if NH_3D_VSO
2385Dist64 TEncGOP::xFindDistortionFrame (TComPicYuv* pcPic0, TComPicYuv* pcPic1, const BitDepths &bitDepths)
2386#else
2387UInt64 TEncGOP::xFindDistortionFrame (TComPicYuv* pcPic0, TComPicYuv* pcPic1, const BitDepths &bitDepths)
2388#endif
2389{
2390#if NH_3D_VSO
2391  Dist64  uiTotalDiff = 0;
2392#else
2393  UInt64  uiTotalDiff = 0;
2394#endif
2395
2396  for(Int chan=0; chan<pcPic0 ->getNumberValidComponents(); chan++)
2397  {
2398    const ComponentID ch=ComponentID(chan);
2399    Pel*  pSrc0   = pcPic0 ->getAddr(ch);
2400    Pel*  pSrc1   = pcPic1 ->getAddr(ch);
2401    UInt  uiShift     = 2 * DISTORTION_PRECISION_ADJUSTMENT(bitDepths.recon[toChannelType(ch)]-8);
2402
2403    const Int   iStride = pcPic0->getStride(ch);
2404    const Int   iWidth  = pcPic0->getWidth(ch);
2405    const Int   iHeight = pcPic0->getHeight(ch);
2406
2407    for(Int y = 0; y < iHeight; y++ )
2408    {
2409      for(Int x = 0; x < iWidth; x++ )
2410      {
2411        Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
2412        uiTotalDiff += UInt64((iTemp*iTemp) >> uiShift);
2413      }
2414      pSrc0 += iStride;
2415      pSrc1 += iStride;
2416    }
2417  }
2418
2419  return uiTotalDiff;
2420}
2421
2422Void TEncGOP::xCalculateAddPSNRs( const Bool isField, const Bool isFieldTopFieldFirst, const Int iGOPid, TComPic* pcPic, const AccessUnit&accessUnit, TComList<TComPic*> &rcListPic, const Double dEncTime, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE )
2423{
2424  xCalculateAddPSNR( pcPic, pcPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
2425
2426  //In case of field coding, compute the interlaced PSNR for both fields
2427  if(isField)
2428  {
2429    Bool bothFieldsAreEncoded = false;
2430    Int correspondingFieldPOC = pcPic->getPOC();
2431    Int currentPicGOPPoc = m_pcCfg->getGOPEntry(iGOPid).m_POC;
2432    if(pcPic->getPOC() == 0)
2433    {
2434      // particular case for POC 0 and 1.
2435      // If they are not encoded first and separately from other pictures, we need to change this
2436      // POC 0 is always encoded first then POC 1 is encoded
2437      bothFieldsAreEncoded = false;
2438    }
2439    else if(pcPic->getPOC() == 1)
2440    {
2441      // if we are at POC 1, POC 0 has been encoded for sure
2442      correspondingFieldPOC = 0;
2443      bothFieldsAreEncoded = true;
2444    }
2445    else
2446    {
2447      if(pcPic->getPOC()%2 == 1)
2448      {
2449        correspondingFieldPOC -= 1; // all odd POC are associated with the preceding even POC (e.g poc 1 is associated to poc 0)
2450        currentPicGOPPoc      -= 1;
2451      }
2452      else
2453      {
2454        correspondingFieldPOC += 1; // all even POC are associated with the following odd POC (e.g poc 0 is associated to poc 1)
2455        currentPicGOPPoc      += 1;
2456      }
2457      for(Int i = 0; i < m_iGopSize; i ++)
2458      {
2459        if(m_pcCfg->getGOPEntry(i).m_POC == currentPicGOPPoc)
2460        {
2461          bothFieldsAreEncoded = m_pcCfg->getGOPEntry(i).m_isEncoded;
2462          break;
2463        }
2464      }
2465    }
2466
2467    if(bothFieldsAreEncoded)
2468    {
2469      //get complementary top field
2470      TComList<TComPic*>::iterator   iterPic = rcListPic.begin();
2471      while ((*iterPic)->getPOC() != correspondingFieldPOC)
2472      {
2473        iterPic ++;
2474      }
2475      TComPic* correspondingFieldPic = *(iterPic);
2476
2477      if( (pcPic->isTopField() && isFieldTopFieldFirst) || (!pcPic->isTopField() && !isFieldTopFieldFirst))
2478      {
2479        xCalculateInterlacedAddPSNR(pcPic, correspondingFieldPic, pcPic->getPicYuvRec(), correspondingFieldPic->getPicYuvRec(), snr_conversion, printFrameMSE );
2480      }
2481      else
2482      {
2483        xCalculateInterlacedAddPSNR(correspondingFieldPic, pcPic, correspondingFieldPic->getPicYuvRec(), pcPic->getPicYuvRec(), snr_conversion, printFrameMSE );
2484      }
2485    }
2486  }
2487}
2488
2489Void TEncGOP::xCalculateAddPSNR( TComPic* pcPic, TComPicYuv* pcPicD, const AccessUnit& accessUnit, Double dEncTime, const InputColourSpaceConversion conversion, const Bool printFrameMSE )
2490{
2491  Double  dPSNR[MAX_NUM_COMPONENT];
2492
2493  for(Int i=0; i<MAX_NUM_COMPONENT; i++)
2494  {
2495    dPSNR[i]=0.0;
2496  }
2497
2498  TComPicYuv cscd;
2499  if (conversion!=IPCOLOURSPACE_UNCHANGED)
2500  {
2501    cscd.createWithoutCUInfo(pcPicD->getWidth(COMPONENT_Y), pcPicD->getHeight(COMPONENT_Y), pcPicD->getChromaFormat() );
2502    TVideoIOYuv::ColourSpaceConvert(*pcPicD, cscd, conversion, false);
2503  }
2504  TComPicYuv &picd=(conversion==IPCOLOURSPACE_UNCHANGED)?*pcPicD : cscd;
2505
2506  //===== calculate PSNR =====
2507  Double MSEyuvframe[MAX_NUM_COMPONENT] = {0, 0, 0};
2508
2509  for(Int chan=0; chan<pcPicD->getNumberValidComponents(); chan++)
2510  {
2511    const ComponentID ch=ComponentID(chan);
2512    const TComPicYuv *pOrgPicYuv =(conversion!=IPCOLOURSPACE_UNCHANGED) ? pcPic ->getPicYuvTrueOrg() : pcPic ->getPicYuvOrg();
2513    const Pel*  pOrg       = pOrgPicYuv->getAddr(ch);
2514    const Int   iOrgStride = pOrgPicYuv->getStride(ch);
2515    Pel*  pRec    = picd.getAddr(ch);
2516    const Int   iRecStride = picd.getStride(ch);
2517
2518    const Int   iWidth  = pcPicD->getWidth (ch) - (m_pcEncTop->getPad(0) >> pcPic->getComponentScaleX(ch));
2519    const Int   iHeight = pcPicD->getHeight(ch) - ((m_pcEncTop->getPad(1) >> (pcPic->isField()?1:0)) >> pcPic->getComponentScaleY(ch));
2520
2521    Int   iSize   = iWidth*iHeight;
2522
2523    UInt64 uiSSDtemp=0;
2524    for(Int y = 0; y < iHeight; y++ )
2525    {
2526      for(Int x = 0; x < iWidth; x++ )
2527      {
2528        Intermediate_Int iDiff = (Intermediate_Int)( pOrg[x] - pRec[x] );
2529        uiSSDtemp   += iDiff * iDiff;
2530      }
2531      pOrg += iOrgStride;
2532      pRec += iRecStride;
2533    }
2534#if NH_3D_VSO
2535#if H_3D_VSO_SYNTH_DIST_OUT
2536  if ( m_pcRdCost->getUseRenModel() )
2537  {
2538    unsigned int maxval = 255 * (1<<(g_uiBitDepth + g_uiBitIncrement -8));
2539    Double fRefValueY = (double) maxval * maxval * iSize;
2540    Double fRefValueC = fRefValueY / 4.0;
2541    TRenModel*  pcRenModel = m_pcEncTop->getEncTop()->getRenModel();
2542    Int64 iDistVSOY, iDistVSOU, iDistVSOV;
2543    pcRenModel->getTotalSSE( iDistVSOY, iDistVSOU, iDistVSOV );
2544    dYPSNR = ( iDistVSOY ? 10.0 * log10( fRefValueY / (Double) iDistVSOY ) : 99.99 );
2545    dUPSNR = ( iDistVSOU ? 10.0 * log10( fRefValueC / (Double) iDistVSOU ) : 99.99 );
2546    dVPSNR = ( iDistVSOV ? 10.0 * log10( fRefValueC / (Double) iDistVSOV ) : 99.99 );
2547  }
2548  else
2549  {
2550#endif
2551#endif
2552    const Int maxval = 255 << (pcPic->getPicSym()->getSPS().getBitDepth(toChannelType(ch)) - 8);
2553    const Double fRefValue = (Double) maxval * maxval * iSize;
2554    dPSNR[ch]         = ( uiSSDtemp ? 10.0 * log10( fRefValue / (Double)uiSSDtemp ) : 999.99 );
2555    MSEyuvframe[ch]   = (Double)uiSSDtemp/(iSize);
2556  }
2557
2558#if NH_3D_VSO
2559#if H_3D_VSO_SYNTH_DIST_OUT
2560}
2561#endif
2562#endif
2563  /* calculate the size of the access unit, excluding:
2564   *  - any AnnexB contributions (start_code_prefix, zero_byte, etc.,)
2565   *  - SEI NAL units
2566   */
2567  UInt numRBSPBytes = 0;
2568  for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
2569  {
2570    UInt numRBSPBytes_nal = UInt((*it)->m_nalUnitData.str().size());
2571    if (m_pcCfg->getSummaryVerboseness() > 0)
2572    {
2573    printf("*** %6s numBytesInNALunit: %u\n", nalUnitTypeToString((*it)->m_nalUnitType), numRBSPBytes_nal);
2574    }
2575    if ((*it)->m_nalUnitType != NAL_UNIT_PREFIX_SEI && (*it)->m_nalUnitType != NAL_UNIT_SUFFIX_SEI)
2576    {
2577      numRBSPBytes += numRBSPBytes_nal;
2578    }
2579  }
2580
2581  UInt uibits = numRBSPBytes * 8;
2582  m_vRVM_RP.push_back( uibits );
2583
2584  //===== add PSNR =====
2585  m_gcAnalyzeAll.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2586
2587  TComSlice*  pcSlice = pcPic->getSlice(0);
2588  if (pcSlice->isIntra())
2589  {
2590    m_gcAnalyzeI.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2591  }
2592  if (pcSlice->isInterP())
2593  {
2594    m_gcAnalyzeP.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2595  }
2596  if (pcSlice->isInterB())
2597  {
2598    m_gcAnalyzeB.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2599  }
2600
2601  TChar c = (pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B');
2602  if (!pcSlice->isReferenced())
2603  {
2604    c += 32;
2605  }
2606
2607#if ADAPTIVE_QP_SELECTION
2608#if NH_MV
2609  printf("Layer %3d   POC %4d TId: %1d ( %c-SLICE, nQP %d QP %d ) %10d  bits",
2610    pcSlice->getLayerId(),
2611    pcSlice->getPOC(),
2612    pcSlice->getTLayer(),
2613    c,
2614    pcSlice->getSliceQpBase(),
2615    pcSlice->getSliceQp(),
2616    uibits );
2617#else
2618  printf("POC %4d TId: %1d ( %c-SLICE, nQP %d QP %d ) %10d bits",
2619         pcSlice->getPOC(),
2620         pcSlice->getTLayer(),
2621         c,
2622         pcSlice->getSliceQpBase(),
2623         pcSlice->getSliceQp(),
2624         uibits );
2625#endif
2626#else
2627#if NH_MV
2628  printf("Layer %3d   POC %4d TId: %1d ( %c-SLICE, QP %d ) %10d bits",
2629    pcSlice->getLayerId(),
2630    pcSlice->getPOC()-pcSlice->getLastIDR(),
2631    pcSlice->getTLayer(),
2632    c,
2633    pcSlice->getSliceQp(),
2634    uibits );
2635#else
2636  printf("POC %4d TId: %1d ( %c-SLICE, QP %d ) %10d bits",
2637         pcSlice->getPOC()-pcSlice->getLastIDR(),
2638         pcSlice->getTLayer(),
2639         c,
2640         pcSlice->getSliceQp(),
2641         uibits );
2642#endif
2643#endif
2644#if NH_MV
2645  printf(" [Y %8.4lf dB    U %8.4lf dB    V %8.4lf dB]", dPSNR[COMPONENT_Y], dPSNR[COMPONENT_Cb], dPSNR[COMPONENT_Cr] );
2646#else
2647  printf(" [Y %6.4lf dB    U %6.4lf dB    V %6.4lf dB]", dPSNR[COMPONENT_Y], dPSNR[COMPONENT_Cb], dPSNR[COMPONENT_Cr] );
2648#endif
2649  if (printFrameMSE)
2650  {
2651    printf(" [Y MSE %6.4lf  U MSE %6.4lf  V MSE %6.4lf]", MSEyuvframe[COMPONENT_Y], MSEyuvframe[COMPONENT_Cb], MSEyuvframe[COMPONENT_Cr] );
2652  }
2653  printf(" [ET %5.0f ]", dEncTime );
2654
2655  // printf(" [WP %d]", pcSlice->getUseWeightedPrediction());
2656
2657  for (Int iRefList = 0; iRefList < 2; iRefList++)
2658  {
2659    printf(" [L%d ", iRefList);
2660    for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(RefPicList(iRefList)); iRefIndex++)
2661    {
2662#if NH_MV
2663      if( pcSlice->getLayerId() != pcSlice->getRefLayerId( RefPicList(iRefList), iRefIndex ) )
2664      {
2665        printf( "V%d ", pcSlice->getRefLayerId( RefPicList(iRefList), iRefIndex ) );
2666      }
2667      else
2668      {
2669#endif
2670      printf ("%d ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex)-pcSlice->getLastIDR());
2671#if NH_MV
2672      }
2673#endif
2674    }
2675    printf("]");
2676  }
2677
2678  cscd.destroy();
2679}
2680
2681Void TEncGOP::xCalculateInterlacedAddPSNR( TComPic* pcPicOrgFirstField, TComPic* pcPicOrgSecondField,
2682                                           TComPicYuv* pcPicRecFirstField, TComPicYuv* pcPicRecSecondField,
2683                                           const InputColourSpaceConversion conversion, const Bool printFrameMSE )
2684{
2685
2686#if  NH_MV
2687  assert( 0 ); // Field coding and MV need to be aligned.
2688#else
2689
2690  const TComSPS &sps=pcPicOrgFirstField->getPicSym()->getSPS();
2691  Double  dPSNR[MAX_NUM_COMPONENT];
2692  TComPic    *apcPicOrgFields[2]={pcPicOrgFirstField, pcPicOrgSecondField};
2693  TComPicYuv *apcPicRecFields[2]={pcPicRecFirstField, pcPicRecSecondField};
2694
2695  for(Int i=0; i<MAX_NUM_COMPONENT; i++)
2696  {
2697    dPSNR[i]=0.0;
2698  }
2699
2700  TComPicYuv cscd[2 /* first/second field */];
2701  if (conversion!=IPCOLOURSPACE_UNCHANGED)
2702  {
2703    for(UInt fieldNum=0; fieldNum<2; fieldNum++)
2704    {
2705      TComPicYuv &reconField=*(apcPicRecFields[fieldNum]);
2706      cscd[fieldNum].createWithoutCUInfo(reconField.getWidth(COMPONENT_Y), reconField.getHeight(COMPONENT_Y), reconField.getChromaFormat() );
2707      TVideoIOYuv::ColourSpaceConvert(reconField, cscd[fieldNum], conversion, false);
2708      apcPicRecFields[fieldNum]=cscd+fieldNum;
2709    }
2710  }
2711
2712  //===== calculate PSNR =====
2713  Double MSEyuvframe[MAX_NUM_COMPONENT] = {0, 0, 0};
2714
2715  assert(apcPicRecFields[0]->getChromaFormat()==apcPicRecFields[1]->getChromaFormat());
2716  const UInt numValidComponents=apcPicRecFields[0]->getNumberValidComponents();
2717
2718  for(Int chan=0; chan<numValidComponents; chan++)
2719  {
2720    const ComponentID ch=ComponentID(chan);
2721    assert(apcPicRecFields[0]->getWidth(ch)==apcPicRecFields[1]->getWidth(ch));
2722    assert(apcPicRecFields[0]->getHeight(ch)==apcPicRecFields[1]->getHeight(ch));
2723
2724    UInt64 uiSSDtemp=0;
2725    const Int   iWidth  = apcPicRecFields[0]->getWidth (ch) - (m_pcEncTop->getPad(0) >> apcPicRecFields[0]->getComponentScaleX(ch));
2726    const Int   iHeight = apcPicRecFields[0]->getHeight(ch) - ((m_pcEncTop->getPad(1) >> 1) >> apcPicRecFields[0]->getComponentScaleY(ch));
2727
2728    Int   iSize   = iWidth*iHeight;
2729
2730    for(UInt fieldNum=0; fieldNum<2; fieldNum++)
2731    {
2732      TComPic *pcPic=apcPicOrgFields[fieldNum];
2733      TComPicYuv *pcPicD=apcPicRecFields[fieldNum];
2734
2735      const Pel*  pOrg    = (conversion!=IPCOLOURSPACE_UNCHANGED) ? pcPic ->getPicYuvTrueOrg()->getAddr(ch) : pcPic ->getPicYuvOrg()->getAddr(ch);
2736      Pel*  pRec    = pcPicD->getAddr(ch);
2737      const Int   iStride = pcPicD->getStride(ch);
2738
2739
2740      for(Int y = 0; y < iHeight; y++ )
2741      {
2742        for(Int x = 0; x < iWidth; x++ )
2743        {
2744          Intermediate_Int iDiff = (Intermediate_Int)( pOrg[x] - pRec[x] );
2745          uiSSDtemp   += iDiff * iDiff;
2746        }
2747        pOrg += iStride;
2748        pRec += iStride;
2749      }
2750    }
2751    const Int maxval = 255 << (sps.getBitDepth(toChannelType(ch)) - 8);
2752    const Double fRefValue = (Double) maxval * maxval * iSize*2;
2753    dPSNR[ch]         = ( uiSSDtemp ? 10.0 * log10( fRefValue / (Double)uiSSDtemp ) : 999.99 );
2754    MSEyuvframe[ch]   = (Double)uiSSDtemp/(iSize*2);
2755  }
2756
2757  UInt uibits = 0; // the number of bits for the pair is not calculated here - instead the overall total is used elsewhere.
2758
2759  //===== add PSNR =====
2760  m_gcAnalyzeAll_in.addResult (dPSNR, (Double)uibits, MSEyuvframe);
2761
2762  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] );
2763  if (printFrameMSE)
2764  {
2765    printf(" [Y MSE %6.4lf  U MSE %6.4lf  V MSE %6.4lf]", MSEyuvframe[COMPONENT_Y], MSEyuvframe[COMPONENT_Cb], MSEyuvframe[COMPONENT_Cr] );
2766  }
2767
2768  for(UInt fieldNum=0; fieldNum<2; fieldNum++)
2769  {
2770    cscd[fieldNum].destroy();
2771  }
2772#endif
2773}
2774
2775/** Function for deciding the nal_unit_type.
2776 * \param pocCurr POC of the current picture
2777 * \param lastIDR  POC of the last IDR picture
2778 * \param isField  true to indicate field coding
2779 * \returns the NAL unit type of the picture
2780 * This function checks the configuration and returns the appropriate nal_unit_type for the picture.
2781 */
2782NalUnitType TEncGOP::getNalUnitType(Int pocCurr, Int lastIDR, Bool isField)
2783{
2784  if (pocCurr == 0)
2785  {
2786    return NAL_UNIT_CODED_SLICE_IDR_W_RADL;
2787  }
2788
2789  if(m_pcCfg->getEfficientFieldIRAPEnabled() && isField && pocCurr == 1)
2790  {
2791    // to avoid the picture becoming an IRAP
2792    return NAL_UNIT_CODED_SLICE_TRAIL_R;
2793  }
2794
2795  if(m_pcCfg->getDecodingRefreshType() != 3 && (pocCurr - isField) % m_pcCfg->getIntraPeriod() == 0)
2796  {
2797    if (m_pcCfg->getDecodingRefreshType() == 1)
2798    {
2799      return NAL_UNIT_CODED_SLICE_CRA;
2800    }
2801    else if (m_pcCfg->getDecodingRefreshType() == 2)
2802    {
2803      return NAL_UNIT_CODED_SLICE_IDR_W_RADL;
2804    }
2805  }
2806  if(m_pocCRA>0)
2807  {
2808    if(pocCurr<m_pocCRA)
2809    {
2810      // All leading pictures are being marked as TFD pictures here since current encoder uses all
2811      // reference pictures while encoding leading pictures. An encoder can ensure that a leading
2812      // picture can be still decodable when random accessing to a CRA/CRANT/BLA/BLANT picture by
2813      // controlling the reference pictures used for encoding that leading picture. Such a leading
2814      // picture need not be marked as a TFD picture.
2815      return NAL_UNIT_CODED_SLICE_RASL_R;
2816    }
2817  }
2818  if (lastIDR>0)
2819  {
2820    if (pocCurr < lastIDR)
2821    {
2822      return NAL_UNIT_CODED_SLICE_RADL_R;
2823    }
2824  }
2825  return NAL_UNIT_CODED_SLICE_TRAIL_R;
2826}
2827
2828
2829Double TEncGOP::xCalculateRVM()
2830{
2831  Double dRVM = 0;
2832
2833  if( m_pcCfg->getGOPSize() == 1 && m_pcCfg->getIntraPeriod() != 1 && m_pcCfg->getFramesToBeEncoded() > RVM_VCEGAM10_M * 2 )
2834  {
2835    // calculate RVM only for lowdelay configurations
2836    std::vector<Double> vRL , vB;
2837    size_t N = m_vRVM_RP.size();
2838    vRL.resize( N );
2839    vB.resize( N );
2840
2841    Int i;
2842    Double dRavg = 0 , dBavg = 0;
2843    vB[RVM_VCEGAM10_M] = 0;
2844    for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
2845    {
2846      vRL[i] = 0;
2847      for( Int j = i - RVM_VCEGAM10_M ; j <= i + RVM_VCEGAM10_M - 1 ; j++ )
2848      {
2849        vRL[i] += m_vRVM_RP[j];
2850      }
2851      vRL[i] /= ( 2 * RVM_VCEGAM10_M );
2852      vB[i] = vB[i-1] + m_vRVM_RP[i] - vRL[i];
2853      dRavg += m_vRVM_RP[i];
2854      dBavg += vB[i];
2855    }
2856
2857    dRavg /= ( N - 2 * RVM_VCEGAM10_M );
2858    dBavg /= ( N - 2 * RVM_VCEGAM10_M );
2859
2860    Double dSigamB = 0;
2861    for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
2862    {
2863      Double tmp = vB[i] - dBavg;
2864      dSigamB += tmp * tmp;
2865    }
2866    dSigamB = sqrt( dSigamB / ( N - 2 * RVM_VCEGAM10_M ) );
2867
2868    Double f = sqrt( 12.0 * ( RVM_VCEGAM10_M - 1 ) / ( RVM_VCEGAM10_M + 1 ) );
2869
2870    dRVM = dSigamB / dRavg * f;
2871  }
2872
2873  return( dRVM );
2874}
2875/** Attaches the input bitstream to the stream in the output NAL unit
2876    Updates rNalu to contain concatenated bitstream. rpcBitstreamRedirect is cleared at the end of this function call.
2877 *  \param codedSliceData contains the coded slice data (bitstream) to be concatenated to rNalu
2878 *  \param rNalu          target NAL unit
2879 */
2880Void TEncGOP::xAttachSliceDataToNalUnit (OutputNALUnit& rNalu, TComOutputBitstream* codedSliceData)
2881{
2882  // Byte-align
2883  rNalu.m_Bitstream.writeByteAlignment();   // Slice header byte-alignment
2884
2885  // Perform bitstream concatenation
2886  if (codedSliceData->getNumberOfWrittenBits() > 0)
2887  {
2888    rNalu.m_Bitstream.addSubstream(codedSliceData);
2889  }
2890
2891  m_pcEntropyCoder->setBitstream(&rNalu.m_Bitstream);
2892
2893  codedSliceData->clear();
2894}
2895
2896// Function will arrange the long-term pictures in the decreasing order of poc_lsb_lt,
2897// and among the pictures with the same lsb, it arranges them in increasing delta_poc_msb_cycle_lt value
2898Void TEncGOP::arrangeLongtermPicturesInRPS(TComSlice *pcSlice, TComList<TComPic*>& rcListPic)
2899{
2900  if(pcSlice->getRPS()->getNumberOfLongtermPictures() == 0)
2901  {
2902    return;
2903  }
2904  // we can only modify the local RPS!
2905  assert (pcSlice->getRPSidx()==-1);
2906  TComReferencePictureSet *rps = pcSlice->getLocalRPS();
2907
2908  // Arrange long-term reference pictures in the correct order of LSB and MSB,
2909  // and assign values for pocLSBLT and MSB present flag
2910  Int longtermPicsPoc[MAX_NUM_REF_PICS], longtermPicsLSB[MAX_NUM_REF_PICS], indices[MAX_NUM_REF_PICS];
2911  Int longtermPicsMSB[MAX_NUM_REF_PICS];
2912  Bool mSBPresentFlag[MAX_NUM_REF_PICS];
2913  ::memset(longtermPicsPoc, 0, sizeof(longtermPicsPoc));    // Store POC values of LTRP
2914  ::memset(longtermPicsLSB, 0, sizeof(longtermPicsLSB));    // Store POC LSB values of LTRP
2915  ::memset(longtermPicsMSB, 0, sizeof(longtermPicsMSB));    // Store POC LSB values of LTRP
2916  ::memset(indices        , 0, sizeof(indices));            // Indices to aid in tracking sorted LTRPs
2917  ::memset(mSBPresentFlag , 0, sizeof(mSBPresentFlag));     // Indicate if MSB needs to be present
2918
2919  // Get the long-term reference pictures
2920  Int offset = rps->getNumberOfNegativePictures() + rps->getNumberOfPositivePictures();
2921  Int i, ctr = 0;
2922  Int maxPicOrderCntLSB = 1 << pcSlice->getSPS()->getBitsForPOC();
2923  for(i = rps->getNumberOfPictures() - 1; i >= offset; i--, ctr++)
2924  {
2925    longtermPicsPoc[ctr] = rps->getPOC(i);                                  // LTRP POC
2926    longtermPicsLSB[ctr] = getLSB(longtermPicsPoc[ctr], maxPicOrderCntLSB); // LTRP POC LSB
2927    indices[ctr]      = i;
2928    longtermPicsMSB[ctr] = longtermPicsPoc[ctr] - longtermPicsLSB[ctr];
2929  }
2930  Int numLongPics = rps->getNumberOfLongtermPictures();
2931  assert(ctr == numLongPics);
2932
2933  // Arrange pictures in decreasing order of MSB;
2934  for(i = 0; i < numLongPics; i++)
2935  {
2936    for(Int j = 0; j < numLongPics - 1; j++)
2937    {
2938      if(longtermPicsMSB[j] < longtermPicsMSB[j+1])
2939      {
2940        std::swap(longtermPicsPoc[j], longtermPicsPoc[j+1]);
2941        std::swap(longtermPicsLSB[j], longtermPicsLSB[j+1]);
2942        std::swap(longtermPicsMSB[j], longtermPicsMSB[j+1]);
2943        std::swap(indices[j]        , indices[j+1]        );
2944      }
2945    }
2946  }
2947
2948  for(i = 0; i < numLongPics; i++)
2949  {
2950    // Check if MSB present flag should be enabled.
2951    // Check if the buffer contains any pictures that have the same LSB.
2952    TComList<TComPic*>::iterator  iterPic = rcListPic.begin();
2953    TComPic*                      pcPic;
2954    while ( iterPic != rcListPic.end() )
2955    {
2956      pcPic = *iterPic;
2957      if( (getLSB(pcPic->getPOC(), maxPicOrderCntLSB) == longtermPicsLSB[i])   &&     // Same LSB
2958                                      (pcPic->getSlice(0)->isReferenced())     &&    // Reference picture
2959                                        (pcPic->getPOC() != longtermPicsPoc[i])    )  // Not the LTRP itself
2960      {
2961        mSBPresentFlag[i] = true;
2962        break;
2963      }
2964      iterPic++;
2965    }
2966  }
2967
2968  // tempArray for usedByCurr flag
2969  Bool tempArray[MAX_NUM_REF_PICS]; ::memset(tempArray, 0, sizeof(tempArray));
2970  for(i = 0; i < numLongPics; i++)
2971  {
2972    tempArray[i] = rps->getUsed(indices[i]);
2973  }
2974  // Now write the final values;
2975  ctr = 0;
2976  Int currMSB = 0, currLSB = 0;
2977  // currPicPoc = currMSB + currLSB
2978  currLSB = getLSB(pcSlice->getPOC(), maxPicOrderCntLSB);
2979  currMSB = pcSlice->getPOC() - currLSB;
2980
2981  for(i = rps->getNumberOfPictures() - 1; i >= offset; i--, ctr++)
2982  {
2983    rps->setPOC                   (i, longtermPicsPoc[ctr]);
2984    rps->setDeltaPOC              (i, - pcSlice->getPOC() + longtermPicsPoc[ctr]);
2985    rps->setUsed                  (i, tempArray[ctr]);
2986    rps->setPocLSBLT              (i, longtermPicsLSB[ctr]);
2987    rps->setDeltaPocMSBCycleLT    (i, (currMSB - (longtermPicsPoc[ctr] - longtermPicsLSB[ctr])) / maxPicOrderCntLSB);
2988    rps->setDeltaPocMSBPresentFlag(i, mSBPresentFlag[ctr]);
2989
2990    assert(rps->getDeltaPocMSBCycleLT(i) >= 0);   // Non-negative value
2991  }
2992  for(i = rps->getNumberOfPictures() - 1, ctr = 1; i >= offset; i--, ctr++)
2993  {
2994    for(Int j = rps->getNumberOfPictures() - 1 - ctr; j >= offset; j--)
2995    {
2996      // Here at the encoder we know that we have set the full POC value for the LTRPs, hence we
2997      // don't have to check the MSB present flag values for this constraint.
2998      assert( rps->getPOC(i) != rps->getPOC(j) ); // If assert fails, LTRP entry repeated in RPS!!!
2999    }
3000  }
3001}
3002
3003Void TEncGOP::applyDeblockingFilterMetric( TComPic* pcPic, UInt uiNumSlices )
3004{
3005  TComPicYuv* pcPicYuvRec = pcPic->getPicYuvRec();
3006  Pel* Rec    = pcPicYuvRec->getAddr(COMPONENT_Y);
3007  Pel* tempRec = Rec;
3008  Int  stride = pcPicYuvRec->getStride(COMPONENT_Y);
3009  UInt log2maxTB = pcPic->getSlice(0)->getSPS()->getQuadtreeTULog2MaxSize();
3010  UInt maxTBsize = (1<<log2maxTB);
3011  const UInt minBlockArtSize = 8;
3012  const UInt picWidth = pcPicYuvRec->getWidth(COMPONENT_Y);
3013  const UInt picHeight = pcPicYuvRec->getHeight(COMPONENT_Y);
3014  const UInt noCol = (picWidth>>log2maxTB);
3015  const UInt noRows = (picHeight>>log2maxTB);
3016  assert(noCol > 1);
3017  assert(noRows > 1);
3018  std::vector<UInt64> colSAD(noCol,  UInt64(0));
3019  std::vector<UInt64> rowSAD(noRows, UInt64(0));
3020  UInt colIdx = 0;
3021  UInt rowIdx = 0;
3022  Pel p0, p1, p2, q0, q1, q2;
3023
3024  Int qp = pcPic->getSlice(0)->getSliceQp();
3025  const Int bitDepthLuma=pcPic->getSlice(0)->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA);
3026  Int bitdepthScale = 1 << (bitDepthLuma-8);
3027  Int beta = TComLoopFilter::getBeta( qp ) * bitdepthScale;
3028  const Int thr2 = (beta>>2);
3029  const Int thr1 = 2*bitdepthScale;
3030  UInt a = 0;
3031
3032  if (maxTBsize > minBlockArtSize)
3033  {
3034    // Analyze vertical artifact edges
3035    for(Int c = maxTBsize; c < picWidth; c += maxTBsize)
3036    {
3037      for(Int r = 0; r < picHeight; r++)
3038      {
3039        p2 = Rec[c-3];
3040        p1 = Rec[c-2];
3041        p0 = Rec[c-1];
3042        q0 = Rec[c];
3043        q1 = Rec[c+1];
3044        q2 = Rec[c+2];
3045        a = ((abs(p2-(p1<<1)+p0)+abs(q0-(q1<<1)+q2))<<1);
3046        if ( thr1 < a && a < thr2)
3047        {
3048          colSAD[colIdx] += abs(p0 - q0);
3049        }
3050        Rec += stride;
3051      }
3052      colIdx++;
3053      Rec = tempRec;
3054    }
3055
3056    // Analyze horizontal artifact edges
3057    for(Int r = maxTBsize; r < picHeight; r += maxTBsize)
3058    {
3059      for(Int c = 0; c < picWidth; c++)
3060      {
3061        p2 = Rec[c + (r-3)*stride];
3062        p1 = Rec[c + (r-2)*stride];
3063        p0 = Rec[c + (r-1)*stride];
3064        q0 = Rec[c + r*stride];
3065        q1 = Rec[c + (r+1)*stride];
3066        q2 = Rec[c + (r+2)*stride];
3067        a = ((abs(p2-(p1<<1)+p0)+abs(q0-(q1<<1)+q2))<<1);
3068        if (thr1 < a && a < thr2)
3069        {
3070          rowSAD[rowIdx] += abs(p0 - q0);
3071        }
3072      }
3073      rowIdx++;
3074    }
3075  }
3076
3077  UInt64 colSADsum = 0;
3078  UInt64 rowSADsum = 0;
3079  for(Int c = 0; c < noCol-1; c++)
3080  {
3081    colSADsum += colSAD[c];
3082  }
3083  for(Int r = 0; r < noRows-1; r++)
3084  {
3085    rowSADsum += rowSAD[r];
3086  }
3087
3088  colSADsum <<= 10;
3089  rowSADsum <<= 10;
3090  colSADsum /= (noCol-1);
3091  colSADsum /= picHeight;
3092  rowSADsum /= (noRows-1);
3093  rowSADsum /= picWidth;
3094
3095  UInt64 avgSAD = ((colSADsum + rowSADsum)>>1);
3096  avgSAD >>= (bitDepthLuma-8);
3097
3098  if ( avgSAD > 2048 )
3099  {
3100    avgSAD >>= 9;
3101    Int offset = Clip3(2,6,(Int)avgSAD);
3102    for (Int i=0; i<uiNumSlices; i++)
3103    {
3104      pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(true);
3105      pcPic->getSlice(i)->setDeblockingFilterDisable(false);
3106      pcPic->getSlice(i)->setDeblockingFilterBetaOffsetDiv2( offset );
3107      pcPic->getSlice(i)->setDeblockingFilterTcOffsetDiv2( offset );
3108    }
3109  }
3110  else
3111  {
3112    for (Int i=0; i<uiNumSlices; i++)
3113    {
3114      pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(false);
3115      pcPic->getSlice(i)->setDeblockingFilterDisable(        pcPic->getSlice(i)->getPPS()->getPicDisableDeblockingFilterFlag() );
3116      pcPic->getSlice(i)->setDeblockingFilterBetaOffsetDiv2( pcPic->getSlice(i)->getPPS()->getDeblockingFilterBetaOffsetDiv2() );
3117      pcPic->getSlice(i)->setDeblockingFilterTcOffsetDiv2(   pcPic->getSlice(i)->getPPS()->getDeblockingFilterTcOffsetDiv2()   );
3118    }
3119  }
3120}
3121
3122#if W0038_DB_OPT
3123Void TEncGOP::applyDeblockingFilterParameterSelection( TComPic* pcPic, const UInt numSlices, const Int gopID )
3124{
3125  enum DBFltParam
3126  {
3127    DBFLT_PARAM_AVAILABLE = 0,
3128    DBFLT_DISABLE_FLAG,
3129    DBFLT_BETA_OFFSETD2,
3130    DBFLT_TC_OFFSETD2,
3131    //NUM_DBFLT_PARAMS
3132  };
3133  const Int MAX_BETA_OFFSET = 3;
3134  const Int MIN_BETA_OFFSET = -3;
3135  const Int MAX_TC_OFFSET = 3;
3136  const Int MIN_TC_OFFSET = -3;
3137
3138  TComPicYuv* pcPicYuvRec = pcPic->getPicYuvRec();
3139  TComPicYuv* pcPicYuvOrg = pcPic ->getPicYuvOrg();
3140
3141  const Int currQualityLayer = (pcPic->getSlice(0)->getSliceType() != I_SLICE) ? m_pcCfg->getGOPEntry(gopID).m_temporalId+1 : 0;
3142  assert(currQualityLayer <MAX_ENCODER_DEBLOCKING_QUALITY_LAYERS);
3143
3144  if(!m_pcDeblockingTempPicYuv)
3145  {
3146    m_pcDeblockingTempPicYuv         = new TComPicYuv;
3147    m_pcDeblockingTempPicYuv->create( m_pcEncTop->getSourceWidth(), m_pcEncTop->getSourceHeight(), m_pcEncTop->getChromaFormatIdc(),  pcPic->getSlice(0)->getSPS()->getMaxCUWidth(), pcPic->getSlice(0)->getSPS()->getMaxCUHeight(), pcPic->getSlice(0)->getSPS()->getMaxTotalCUDepth(),true );
3148    memset(m_DBParam, 0, sizeof(m_DBParam));
3149  }
3150
3151  //preserve current reconstruction
3152  pcPicYuvRec->copyToPic(m_pcDeblockingTempPicYuv);
3153
3154  const Bool bNoFiltering      = m_DBParam[currQualityLayer][DBFLT_PARAM_AVAILABLE] && m_DBParam[currQualityLayer][DBFLT_DISABLE_FLAG]==false /*&& pcPic->getTLayer()==0*/;
3155  const Int  maxBetaOffsetDiv2 = bNoFiltering? Clip3(MIN_BETA_OFFSET, MAX_BETA_OFFSET, m_DBParam[currQualityLayer][DBFLT_BETA_OFFSETD2]+1) : MAX_BETA_OFFSET;
3156  const Int  minBetaOffsetDiv2 = bNoFiltering? Clip3(MIN_BETA_OFFSET, MAX_BETA_OFFSET, m_DBParam[currQualityLayer][DBFLT_BETA_OFFSETD2]-1) : MIN_BETA_OFFSET;
3157  const Int  maxTcOffsetDiv2   = bNoFiltering? Clip3(MIN_TC_OFFSET, MAX_TC_OFFSET, m_DBParam[currQualityLayer][DBFLT_TC_OFFSETD2]+2)       : MAX_TC_OFFSET;
3158  const Int  minTcOffsetDiv2   = bNoFiltering? Clip3(MIN_TC_OFFSET, MAX_TC_OFFSET, m_DBParam[currQualityLayer][DBFLT_TC_OFFSETD2]-2)       : MIN_TC_OFFSET;
3159
3160  UInt64 distBetaPrevious      = std::numeric_limits<UInt64>::max();
3161  UInt64 distMin               = std::numeric_limits<UInt64>::max();
3162  Bool   bDBFilterDisabledBest = true;
3163  Int    betaOffsetDiv2Best    = 0;
3164  Int    tcOffsetDiv2Best      = 0;
3165
3166  for(Int betaOffsetDiv2=maxBetaOffsetDiv2; betaOffsetDiv2>=minBetaOffsetDiv2; betaOffsetDiv2--)
3167  {
3168    UInt64 distTcMin = std::numeric_limits<UInt64>::max();
3169    for(Int tcOffsetDiv2=maxTcOffsetDiv2; tcOffsetDiv2 >= minTcOffsetDiv2; tcOffsetDiv2--)
3170    {
3171      for (Int i=0; i<numSlices; i++)
3172      {
3173        pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(true);
3174        pcPic->getSlice(i)->setDeblockingFilterDisable(false);
3175        pcPic->getSlice(i)->setDeblockingFilterBetaOffsetDiv2( betaOffsetDiv2 );
3176        pcPic->getSlice(i)->setDeblockingFilterTcOffsetDiv2( tcOffsetDiv2 );
3177      }
3178      m_pcDeblockingTempPicYuv->copyToPic(pcPicYuvRec); // restore reconstruction
3179      m_pcLoopFilter->loopFilterPic( pcPic );
3180      const UInt64 dist = xFindDistortionFrame(pcPicYuvOrg, pcPicYuvRec, pcPic->getPicSym()->getSPS().getBitDepths());
3181      if(dist < distMin)
3182      {
3183        distMin = dist;
3184        bDBFilterDisabledBest = false;
3185        betaOffsetDiv2Best  = betaOffsetDiv2;
3186        tcOffsetDiv2Best = tcOffsetDiv2;
3187      }
3188      if(dist < distTcMin)
3189      {
3190        distTcMin = dist;
3191      }
3192      else if(tcOffsetDiv2 <-2)
3193      {
3194        break;
3195      }
3196    }
3197    if(betaOffsetDiv2<-1 && distTcMin >= distBetaPrevious)
3198    {
3199      break;
3200    }
3201    distBetaPrevious = distTcMin;
3202  }
3203
3204  //update:
3205  m_DBParam[currQualityLayer][DBFLT_PARAM_AVAILABLE] = 1;
3206  m_DBParam[currQualityLayer][DBFLT_DISABLE_FLAG]    = bDBFilterDisabledBest;
3207  m_DBParam[currQualityLayer][DBFLT_BETA_OFFSETD2]   = betaOffsetDiv2Best;
3208  m_DBParam[currQualityLayer][DBFLT_TC_OFFSETD2]     = tcOffsetDiv2Best;
3209
3210  m_pcDeblockingTempPicYuv->copyToPic(pcPicYuvRec); //restore reconstruction
3211
3212  if(bDBFilterDisabledBest)
3213  {
3214    for (Int i=0; i<numSlices; i++)
3215    {
3216      pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(true);
3217      pcPic->getSlice(i)->setDeblockingFilterDisable(true);
3218    }
3219  }
3220  else if(betaOffsetDiv2Best ==pcPic->getSlice(0)->getPPS()->getDeblockingFilterBetaOffsetDiv2() &&  tcOffsetDiv2Best==pcPic->getSlice(0)->getPPS()->getDeblockingFilterTcOffsetDiv2())
3221  {
3222    for (Int i=0; i<numSlices; i++)
3223    {
3224      pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(false);
3225      pcPic->getSlice(i)->setDeblockingFilterDisable(        pcPic->getSlice(i)->getPPS()->getPicDisableDeblockingFilterFlag() );
3226      pcPic->getSlice(i)->setDeblockingFilterBetaOffsetDiv2( pcPic->getSlice(i)->getPPS()->getDeblockingFilterBetaOffsetDiv2() );
3227      pcPic->getSlice(i)->setDeblockingFilterTcOffsetDiv2(   pcPic->getSlice(i)->getPPS()->getDeblockingFilterTcOffsetDiv2()   );
3228    }
3229  }
3230  else
3231  {
3232    for (Int i=0; i<numSlices; i++)
3233    {
3234      pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(true);
3235      pcPic->getSlice(i)->setDeblockingFilterDisable( false );
3236      pcPic->getSlice(i)->setDeblockingFilterBetaOffsetDiv2(betaOffsetDiv2Best);
3237      pcPic->getSlice(i)->setDeblockingFilterTcOffsetDiv2(tcOffsetDiv2Best);
3238    }
3239  }
3240}
3241#endif
3242
3243#if NH_MV
3244Void TEncGOP::xSetRefPicListModificationsMv( std::vector<TComPic*> tempPicLists[2], TComSlice* pcSlice, UInt iGOPid )
3245{
3246
3247  if( pcSlice->getSliceType() == I_SLICE || !(pcSlice->getPPS()->getListsModificationPresentFlag()) || pcSlice->getNumActiveRefLayerPics() == 0 )
3248  {
3249    return;
3250  }
3251
3252  GOPEntry ge = m_pcCfg->getGOPEntry( (pcSlice->getRapPicFlag() && ( pcSlice->getLayerId( ) > 0) ) ? MAX_GOP : iGOPid );
3253  assert( ge.m_numActiveRefLayerPics == pcSlice->getNumActiveRefLayerPics() );
3254
3255  Int numPicsInTempList     = pcSlice->getNumRpsCurrTempList();
3256
3257  // GT: check if SliceType should be checked here.
3258  for (Int li = 0; li < 2; li ++) // Loop over lists L0 and L1
3259  {
3260    Int numPicsInFinalRefList = pcSlice->getNumRefIdx( ( li == 0 ) ? REF_PIC_LIST_0 : REF_PIC_LIST_1 );
3261
3262    Int finalIdxToTempIdxMap[16];
3263    for( Int k = 0; k < 16; k++ )
3264    {
3265      finalIdxToTempIdxMap[ k ] = -1;
3266    }
3267
3268    Bool isModified = false;
3269    if ( numPicsInTempList > 1 )
3270    {
3271      for( Int k = 0; k < pcSlice->getNumActiveRefLayerPics(); k++ )
3272      {
3273        // get position in temp. list
3274        Int refPicLayerId = pcSlice->getRefPicLayerId(k);
3275        Int idxInTempList = 0;
3276        for (; idxInTempList < numPicsInTempList; idxInTempList++)
3277        {
3278          if ( (tempPicLists[li][idxInTempList])->getLayerId() == refPicLayerId )
3279          {
3280            break;
3281          }
3282        }
3283
3284        Int idxInFinalList = ge.m_interViewRefPosL[ li ][ k ];
3285
3286        // Add negative from behind
3287        idxInFinalList = ( idxInFinalList < 0 )? ( numPicsInTempList + idxInFinalList ) : idxInFinalList;
3288
3289        Bool curIsModified = ( idxInFinalList != idxInTempList ) && ( ( idxInTempList < numPicsInFinalRefList ) || ( idxInFinalList < numPicsInFinalRefList ) ) ;
3290        if ( curIsModified )
3291        {
3292          isModified = true;
3293          assert( finalIdxToTempIdxMap[ idxInFinalList ] == -1 ); // Assert when two inter layer reference pictures are sorted to the same position
3294        }
3295        finalIdxToTempIdxMap[ idxInFinalList ] = idxInTempList;
3296      }
3297    }
3298
3299    TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
3300    refPicListModification->setRefPicListModificationFlagL( li, isModified );
3301
3302    if( isModified )
3303    {
3304      Int refIdx = 0;
3305
3306      for( Int i = 0; i < numPicsInFinalRefList; i++ )
3307      {
3308        if( finalIdxToTempIdxMap[i] >= 0 )
3309        {
3310          refPicListModification->setRefPicSetIdxL( li, i, finalIdxToTempIdxMap[i] );
3311        }
3312        else
3313        {
3314          ///* Fill gaps with temporal references *///
3315          // Forward inter layer reference pictures
3316          while( ( refIdx < numPicsInTempList ) && ( tempPicLists[li][refIdx]->getLayerId() != getLayerId())  )
3317          {
3318            refIdx++;
3319          }
3320          refPicListModification->setRefPicSetIdxL( li, i, refIdx );
3321          refIdx++;
3322        }
3323      }
3324    }
3325  }
3326}
3327#endif
3328//! \}
Note: See TracBrowser for help on using the repository browser.