source: 3DVCSoftware/branches/HTM-15.1-MV-draft-4/source/Lib/TLibEncoder/TEncGOP.cpp @ 1389

Last change on this file since 1389 was 1325, checked in by tech, 10 years ago

Removed 3D-HEVC.

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