source: 3DVCSoftware/branches/HTM-16.0-dev1/source/Lib/TLibEncoder/TEncGOP.cpp @ 1417

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

Merged 15.1-dev1@1381.

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