source: 3DVCSoftware/branches/HTM-15.1-dev0/source/Lib/TLibEncoder/TEncGOP.cpp @ 1328

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

Integrated general SEI changes and following SEIs:

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