source: 3DVCSoftware/branches/HTM-16.0-MV-draft-5/source/Lib/TLibEncoder/TEncGOP.cpp

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

Removed 3D.

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