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

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