source: 3DVCSoftware/branches/HTM-15.2-dev/source/Lib/TLibEncoder/TEncGOP.cpp @ 1370

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

Align macros

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