source: 3DVCSoftware/branches/HTM-12.1-dev0/source/Lib/TLibEncoder/TEncTop.cpp @ 1075

Last change on this file since 1075 was 1074, checked in by tech, 10 years ago

Removed 3D-HEVC related macros.

  • Property svn:eol-style set to native
File size: 50.0 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-2014, 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     TEncTop.cpp
35    \brief    encoder class
36*/
37
38#include "TLibCommon/CommonDef.h"
39#include "TEncTop.h"
40#include "TEncPic.h"
41#if FAST_BIT_EST
42#include "TLibCommon/ContextModel.h"
43#endif
44#if H_MV
45#include "../../App/TAppEncoder/TAppEncTop.h"
46#endif
47
48//! \ingroup TLibEncoder
49//! \{
50
51// ====================================================================================================================
52// Constructor / destructor / create / destroy
53// ====================================================================================================================
54
55TEncTop::TEncTop()
56{
57  m_iPOCLast          = -1;
58  m_iNumPicRcvd       =  0;
59  m_uiNumAllPicCoded  =  0;
60  m_pppcRDSbacCoder   =  NULL;
61  m_pppcBinCoderCABAC =  NULL;
62  m_cRDGoOnSbacCoder.init( &m_cRDGoOnBinCoderCABAC );
63#if ENC_DEC_TRACE
64  g_hTrace = fopen( "TraceEnc.txt", "wb" );
65  g_bJustDoIt = g_bEncDecTraceDisable;
66  g_nSymbolCounter = 0;
67#endif
68
69  m_iMaxRefPicNum     = 0;
70
71#if FAST_BIT_EST
72  ContextModel::buildNextStateTable();
73#endif
74#if H_MV_HLS10_GEN_FIX
75#if H_MV
76  m_iNumSubstreams         = 0; 
77#endif
78#endif
79
80  m_pcSbacCoders           = NULL;
81  m_pcBinCoderCABACs       = NULL;
82  m_ppppcRDSbacCoders      = NULL;
83  m_ppppcBinCodersCABAC    = NULL;
84  m_pcRDGoOnSbacCoders     = NULL;
85  m_pcRDGoOnBinCodersCABAC = NULL;
86  m_pcBitCounters          = NULL;
87  m_pcRdCosts              = NULL;
88#if H_MV
89  m_ivPicLists = NULL;
90#endif
91#if H_3D_IC
92  m_aICEnableCandidate = NULL;
93  m_aICEnableNum = NULL;
94#endif
95}
96
97TEncTop::~TEncTop()
98{
99#if ENC_DEC_TRACE
100  fclose( g_hTrace );
101#endif
102}
103
104Void TEncTop::create ()
105{
106#if !H_MV
107  // initialize global variables
108  initROM();
109#endif
110 
111  // create processing unit classes
112  m_cGOPEncoder.        create();
113  m_cSliceEncoder.      create( getSourceWidth(), getSourceHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
114  m_cCuEncoder.         create( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
115  if (m_bUseSAO)
116  {
117    m_cEncSAO.create( getSourceWidth(), getSourceHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
118#if SAO_ENCODE_ALLOW_USE_PREDEBLOCK
119    m_cEncSAO.createEncData(getSaoLcuBoundary());
120#else
121    m_cEncSAO.createEncData();
122#endif
123  }
124#if ADAPTIVE_QP_SELECTION
125  if (m_bUseAdaptQpSelect)
126  {
127    m_cTrQuant.initSliceQpDelta();
128  }
129#endif
130  m_cLoopFilter.        create( g_uiMaxCUDepth );
131
132  if ( m_RCEnableRateControl )
133  {
134#if KWU_RC_MADPRED_E0227
135    m_cRateCtrl.init( m_framesToBeEncoded, m_RCTargetBitrate, m_iFrameRate, m_iGOPSize, m_iSourceWidth, m_iSourceHeight,
136      g_uiMaxCUWidth, g_uiMaxCUHeight, m_RCKeepHierarchicalBit, m_RCUseLCUSeparateModel, m_GOPList, getLayerId() );
137#else
138    m_cRateCtrl.init( m_framesToBeEncoded, m_RCTargetBitrate, m_iFrameRate, m_iGOPSize, m_iSourceWidth, m_iSourceHeight,
139                      g_uiMaxCUWidth, g_uiMaxCUHeight, m_RCKeepHierarchicalBit, m_RCUseLCUSeparateModel, m_GOPList );
140#endif
141  }
142    m_pppcRDSbacCoder = new TEncSbac** [g_uiMaxCUDepth+1];
143#if FAST_BIT_EST
144    m_pppcBinCoderCABAC = new TEncBinCABACCounter** [g_uiMaxCUDepth+1];
145#else
146    m_pppcBinCoderCABAC = new TEncBinCABAC** [g_uiMaxCUDepth+1];
147#endif
148   
149    for ( Int iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
150    {
151      m_pppcRDSbacCoder[iDepth] = new TEncSbac* [CI_NUM];
152#if FAST_BIT_EST
153      m_pppcBinCoderCABAC[iDepth] = new TEncBinCABACCounter* [CI_NUM];
154#else
155      m_pppcBinCoderCABAC[iDepth] = new TEncBinCABAC* [CI_NUM];
156#endif
157     
158      for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
159      {
160        m_pppcRDSbacCoder[iDepth][iCIIdx] = new TEncSbac;
161#if FAST_BIT_EST
162        m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABACCounter;
163#else
164        m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABAC;
165#endif
166        m_pppcRDSbacCoder   [iDepth][iCIIdx]->init( m_pppcBinCoderCABAC [iDepth][iCIIdx] );
167      }
168    }
169  }
170
171/**
172 - Allocate coders required for wavefront for the nominated number of substreams.
173 .
174 \param iNumSubstreams Determines how much information to allocate.
175 */
176Void TEncTop::createWPPCoders(Int iNumSubstreams)
177{
178  if (m_pcSbacCoders != NULL)
179  {
180    return; // already generated.
181  }
182
183  m_iNumSubstreams         = iNumSubstreams;
184  m_pcSbacCoders           = new TEncSbac       [iNumSubstreams];
185  m_pcBinCoderCABACs       = new TEncBinCABAC   [iNumSubstreams];
186  m_pcRDGoOnSbacCoders     = new TEncSbac       [iNumSubstreams];
187  m_pcRDGoOnBinCodersCABAC = new TEncBinCABAC   [iNumSubstreams];
188  m_pcBitCounters          = new TComBitCounter [iNumSubstreams];
189  m_pcRdCosts              = new TComRdCost     [iNumSubstreams];
190
191  for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ )
192  {
193    m_pcRDGoOnSbacCoders[ui].init( &m_pcRDGoOnBinCodersCABAC[ui] );
194    m_pcSbacCoders[ui].init( &m_pcBinCoderCABACs[ui] );
195  }
196
197    m_ppppcRDSbacCoders      = new TEncSbac***    [iNumSubstreams];
198    m_ppppcBinCodersCABAC    = new TEncBinCABAC***[iNumSubstreams];
199    for ( UInt ui = 0 ; ui < iNumSubstreams ; ui++ )
200    {
201      m_ppppcRDSbacCoders[ui]  = new TEncSbac** [g_uiMaxCUDepth+1];
202      m_ppppcBinCodersCABAC[ui]= new TEncBinCABAC** [g_uiMaxCUDepth+1];
203     
204      for ( Int iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
205      {
206        m_ppppcRDSbacCoders[ui][iDepth]  = new TEncSbac*     [CI_NUM];
207        m_ppppcBinCodersCABAC[ui][iDepth]= new TEncBinCABAC* [CI_NUM];
208
209        for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
210        {
211          m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx] = new TEncSbac;
212          m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx] = new TEncBinCABAC;
213          m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx]->init( m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx] );
214        }
215      }
216    }
217  }
218
219Void TEncTop::destroy ()
220{
221  // destroy processing unit classes
222  m_cGOPEncoder.        destroy();
223  m_cSliceEncoder.      destroy();
224  m_cCuEncoder.         destroy();
225  if (m_cSPS.getUseSAO())
226  {
227    m_cEncSAO.destroyEncData();
228    m_cEncSAO.destroy();
229  }
230  m_cLoopFilter.        destroy();
231  m_cRateCtrl.          destroy();
232
233    Int iDepth;
234    for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
235    {
236      for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
237      {
238#if H_MV_HLS10_GEN_FIX
239#if H_MV
240        xDelete( false, m_pppcRDSbacCoder, iDepth, iCIIdx);
241        xDelete( false, m_pppcBinCoderCABAC, iDepth, iCIIdx);
242#else
243        delete m_pppcRDSbacCoder[iDepth][iCIIdx];
244        delete m_pppcBinCoderCABAC[iDepth][iCIIdx];
245#endif
246#endif
247      }
248    }
249   
250    for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
251    {
252#if H_MV_HLS10_GEN_FIX
253#if H_MV
254      xDelete( true, m_pppcRDSbacCoder  , iDepth);
255      xDelete( true, m_pppcBinCoderCABAC, iDepth);
256#else
257      delete [] m_pppcRDSbacCoder[iDepth];
258      delete [] m_pppcBinCoderCABAC[iDepth];
259#endif
260#endif
261    }
262
263#if H_MV_HLS10_GEN_FIX
264#if H_MV
265     xDelete( true, m_pppcRDSbacCoder  );
266     xDelete( true, m_pppcBinCoderCABAC);
267#else
268    delete [] m_pppcRDSbacCoder;
269    delete [] m_pppcBinCoderCABAC;
270#endif
271#endif
272    for ( UInt ui = 0; ui < m_iNumSubstreams; ui++ )
273    {
274      for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
275      {
276        for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
277        {
278#if H_MV_HLS10_GEN_FIX
279#if H_MV
280          xDelete(false, m_ppppcRDSbacCoders  ,ui, iDepth, iCIIdx);
281          xDelete(false, m_ppppcBinCodersCABAC,ui, iDepth, iCIIdx);
282#else
283          delete m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx];
284          delete m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx];
285#endif
286#endif
287        }
288      }
289
290      for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
291      {
292#if H_MV_HLS10_GEN_FIX
293#if H_MV
294        xDelete(true, m_ppppcRDSbacCoders  ,ui, iDepth);
295        xDelete(true, m_ppppcBinCodersCABAC,ui, iDepth);       
296#else
297        delete [] m_ppppcRDSbacCoders  [ui][iDepth];
298        delete [] m_ppppcBinCodersCABAC[ui][iDepth];
299#endif
300#endif
301      }
302
303
304#if H_MV_HLS10_GEN_FIX
305#if H_MV
306      xDelete(true, m_ppppcRDSbacCoders,   ui);
307      xDelete(true, m_ppppcBinCodersCABAC, ui);     
308#else
309      delete[] m_ppppcRDSbacCoders  [ui];
310      delete[] m_ppppcBinCodersCABAC[ui];
311#endif
312#endif
313    }
314#if H_MV_HLS10_GEN_FIX
315#if H_MV
316    xDelete(true, m_ppppcRDSbacCoders    ) ;
317    xDelete(true, m_ppppcBinCodersCABAC);
318    xDelete(true, m_pcSbacCoders);
319    xDelete(true, m_pcBinCoderCABACs);
320    xDelete(true, m_pcRDGoOnSbacCoders); 
321    xDelete(true, m_pcRDGoOnBinCodersCABAC);
322    xDelete(true, m_pcBitCounters);
323    xDelete(true, m_pcRdCosts);
324#else
325    delete[] m_ppppcRDSbacCoders;
326    delete[] m_ppppcBinCodersCABAC;
327  delete[] m_pcSbacCoders;
328  delete[] m_pcBinCoderCABACs;
329  delete[] m_pcRDGoOnSbacCoders; 
330  delete[] m_pcRDGoOnBinCodersCABAC;
331  delete[] m_pcBitCounters;
332  delete[] m_pcRdCosts;
333#endif
334#endif
335
336#if !H_MV
337    // destroy ROM
338  destroyROM();
339#endif
340
341  return;
342}
343
344#if KWU_RC_MADPRED_E0227
345Void TEncTop::init(TAppEncTop* pcTAppEncTop, Bool isFieldCoding)
346#else
347Void TEncTop::init(Bool isFieldCoding)
348#endif
349{
350  // initialize SPS
351#if H_3D
352  // Assuming that all PPS indirectly refer to the same VPS via different SPS
353  m_cSPS.setVPS(m_cVPS);
354#endif
355  xInitSPS();
356 
357  /* set the VPS profile information */
358#if H_MV
359  // This seems to be incorrect, but irrelevant for the MV-HEVC
360  *(m_cVPS->getPTL()) = *m_cSPS.getPTL();
361  m_cVPS->getTimingInfo()->setTimingInfoPresentFlag       ( false );
362#else
363  *m_cVPS.getPTL() = *m_cSPS.getPTL();
364  m_cVPS.getTimingInfo()->setTimingInfoPresentFlag       ( false );
365#endif
366  // initialize PPS
367  m_cPPS.setSPS(&m_cSPS);
368  xInitPPS();
369  xInitRPS(isFieldCoding);
370
371  xInitPPSforTiles();
372#if H_3D_IC
373  m_aICEnableCandidate = new Int[ 10 ];
374  m_aICEnableNum = new Int[ 10 ];
375
376  for(int i=0;i<10;i++)
377  {
378    m_aICEnableCandidate[i]=0;
379    m_aICEnableNum[i]=0;
380  }
381#endif
382  // initialize processing unit classes
383  m_cGOPEncoder.  init( this );
384  m_cSliceEncoder.init( this );
385  m_cCuEncoder.   init( this );
386 
387#if KWU_RC_MADPRED_E0227
388  m_pcTAppEncTop = pcTAppEncTop;
389#endif
390  // initialize transform & quantization class
391  m_pcCavlcCoder = getCavlcCoder();
392 
393  m_cTrQuant.init( 1 << m_uiQuadtreeTULog2MaxSize,
394                  m_useRDOQ, 
395                  m_useRDOQTS,
396                  true 
397                  ,m_useTransformSkipFast
398#if ADAPTIVE_QP_SELECTION                 
399                  , m_bUseAdaptQpSelect
400#endif
401                  );
402 
403  // initialize encoder search class
404  m_cSearch.init( this, &m_cTrQuant, m_iSearchRange, m_bipredSearchRange, m_iFastSearch, 0, &m_cEntropyCoder, &m_cRdCost, getRDSbacCoder(), getRDGoOnSbacCoder() );
405
406  m_iMaxRefPicNum = 0;
407}
408
409// ====================================================================================================================
410// Public member functions
411// ====================================================================================================================
412
413#if H_MV
414Void TEncTop::initNewPic( TComPicYuv* pcPicYuvOrg )
415{
416  TComPic* pcPicCurr = NULL;
417
418  // get original YUV
419  xGetNewPicBuffer( pcPicCurr );
420  pcPicYuvOrg->copyToPic( pcPicCurr->getPicYuvOrg() );
421
422  // compute image characteristics
423  if ( getUseAdaptiveQP() )
424  {
425    m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcPicCurr ) );
426  }
427#if H_MV
428  pcPicCurr->setLayerId( getLayerId()); 
429#endif
430#if H_3D
431  pcPicCurr->setScaleOffset( m_aaiCodedScale, m_aaiCodedOffset );
432#endif
433}
434#endif
435Void TEncTop::deletePicBuffer()
436{
437  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
438  Int iSize = Int( m_cListPic.size() );
439 
440  for ( Int i = 0; i < iSize; i++ )
441  {
442    TComPic* pcPic = *(iterPic++);
443   
444    pcPic->destroy();
445    delete pcPic;
446    pcPic = NULL;
447  }
448}
449
450/**
451 - Application has picture buffer list with size of GOP + 1
452 - Picture buffer list acts like as ring buffer
453 - End of the list has the latest picture
454 .
455 \param   flush               cause encoder to encode a partial GOP
456 \param   pcPicYuvOrg         original YUV picture
457 \retval  rcListPicYuvRecOut  list of reconstruction YUV pictures
458 \retval  rcListBitstreamOut  list of output bitstreams
459 \retval  iNumEncoded         number of encoded pictures
460 */
461#if H_MV
462Void TEncTop::encode(Bool flush, TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded , Int gopId )
463{
464#else
465Void TEncTop::encode(Bool flush, TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded )
466{
467#endif
468#if H_3D
469  TComPic* picLastCoded = getPic( getGOPEncoder()->getPocLastCoded() );
470  if( picLastCoded )
471  {
472    picLastCoded->compressMotion(1); 
473  }
474#endif
475#if H_MV
476  if( gopId == 0)
477  {
478    m_cGOPEncoder.initGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut); 
479#else
480  if (pcPicYuvOrg) {
481    // get original YUV
482    TComPic* pcPicCurr = NULL;
483    xGetNewPicBuffer( pcPicCurr );
484    pcPicYuvOrg->copyToPic( pcPicCurr->getPicYuvOrg() );
485
486    // compute image characteristics
487    if ( getUseAdaptiveQP() )
488    {
489      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcPicCurr ) );
490    }
491  }
492 
493  if (!m_iNumPicRcvd || (!flush && m_iPOCLast != 0 && m_iNumPicRcvd != m_iGOPSize && m_iGOPSize))
494  {
495    iNumEncoded = 0;
496    return;
497  }
498#endif
499 
500  if ( m_RCEnableRateControl )
501  {
502    m_cRateCtrl.initRCGOP( m_iNumPicRcvd );
503  }
504#if H_MV
505  }
506  m_cGOPEncoder.compressPicInGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, gopId, false, false );
507
508  if( gopId + 1 == m_cGOPEncoder.getGOPSize() )
509  {
510#else
511  // compress GOP
512  m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, false, false);
513#endif
514
515  if ( m_RCEnableRateControl )
516  {
517    m_cRateCtrl.destroyRCGOP();
518  }
519 
520  iNumEncoded         = m_iNumPicRcvd;
521  m_iNumPicRcvd       = 0;
522  m_uiNumAllPicCoded += iNumEncoded;
523#if H_MV
524}
525#endif
526}
527/**------------------------------------------------
528 Separate interlaced frame into two fields
529 -------------------------------------------------**/
530void separateFields(Pel* org, Pel* dstField, UInt stride, UInt width, UInt height, bool isTop)
531{
532  if (!isTop)
533  {
534    org += stride;
535  }
536  for (Int y = 0; y < height>>1; y++)
537  {
538    for (Int x = 0; x < width; x++)
539    {
540      dstField[x] = org[x];
541    }
542   
543    dstField += stride;
544    org += stride*2;
545  }
546 
547}
548
549#if H_MV
550Void TEncTop::encode(Bool flush, TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded, bool isTff, Int gopId )
551{
552  assert( 0 ); // Field coding and multiview need to be furhter harmonized.
553}
554#else
555Void TEncTop::encode(Bool flush, TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded, bool isTff)
556{
557  /* -- TOP FIELD -- */
558 
559  if (pcPicYuvOrg)
560  {
561   
562    /* -- Top field initialization -- */
563   
564    TComPic *pcTopField;
565    xGetNewPicBuffer( pcTopField );
566    pcTopField->setReconMark (false);
567   
568    pcTopField->getSlice(0)->setPOC( m_iPOCLast );
569    pcTopField->getPicYuvRec()->setBorderExtension(false);
570    pcTopField->setTopField(isTff);
571   
572    int nHeight = pcPicYuvOrg->getHeight();
573    int nWidth = pcPicYuvOrg->getWidth();
574    int nStride = pcPicYuvOrg->getStride();
575    int nPadLuma = pcPicYuvOrg->getLumaMargin();
576    int nPadChroma = pcPicYuvOrg->getChromaMargin();
577   
578    // Get pointers
579    Pel * PicBufY = pcPicYuvOrg->getBufY();
580    Pel * PicBufU = pcPicYuvOrg->getBufU();
581    Pel * PicBufV = pcPicYuvOrg->getBufV();
582   
583    Pel * pcTopFieldY =  pcTopField->getPicYuvOrg()->getLumaAddr();
584    Pel * pcTopFieldU =  pcTopField->getPicYuvOrg()->getCbAddr();
585    Pel * pcTopFieldV =  pcTopField->getPicYuvOrg()->getCrAddr();
586   
587    /* -- Defield -- */
588   
589    bool isTop = isTff;
590   
591    separateFields(PicBufY + nPadLuma + nStride*nPadLuma, pcTopFieldY, nStride, nWidth, nHeight, isTop);
592    separateFields(PicBufU + nPadChroma + (nStride >> 1)*nPadChroma, pcTopFieldU, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
593    separateFields(PicBufV + nPadChroma + (nStride >> 1)*nPadChroma, pcTopFieldV, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
594   
595    // compute image characteristics
596    if ( getUseAdaptiveQP() )
597    {
598      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcTopField ) );
599    }   
600  }
601 
602  if (m_iPOCLast == 0) // compress field 0
603  {
604    m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, true, isTff);
605  }
606 
607  /* -- BOTTOM FIELD -- */
608 
609  if (pcPicYuvOrg)
610  {
611   
612    /* -- Bottom field initialization -- */
613   
614    TComPic* pcBottomField;
615    xGetNewPicBuffer( pcBottomField );
616    pcBottomField->setReconMark (false);
617   
618    TComPicYuv* rpcPicYuvRec;
619    if ( rcListPicYuvRecOut.size() == (UInt)m_iGOPSize )
620    {
621      rpcPicYuvRec = rcListPicYuvRecOut.popFront();
622    }
623    else
624    {
625      rpcPicYuvRec = new TComPicYuv;
626      rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
627    }
628    rcListPicYuvRecOut.pushBack( rpcPicYuvRec );
629   
630    pcBottomField->getSlice(0)->setPOC( m_iPOCLast);
631    pcBottomField->getPicYuvRec()->setBorderExtension(false);
632    pcBottomField->setTopField(!isTff);
633   
634    int nHeight = pcPicYuvOrg->getHeight();
635    int nWidth = pcPicYuvOrg->getWidth();
636    int nStride = pcPicYuvOrg->getStride();
637    int nPadLuma = pcPicYuvOrg->getLumaMargin();
638    int nPadChroma = pcPicYuvOrg->getChromaMargin();
639   
640    // Get pointers
641    Pel * PicBufY = pcPicYuvOrg->getBufY();
642    Pel * PicBufU = pcPicYuvOrg->getBufU();
643    Pel * PicBufV = pcPicYuvOrg->getBufV();
644   
645    Pel * pcBottomFieldY =  pcBottomField->getPicYuvOrg()->getLumaAddr();
646    Pel * pcBottomFieldU =  pcBottomField->getPicYuvOrg()->getCbAddr();
647    Pel * pcBottomFieldV =  pcBottomField->getPicYuvOrg()->getCrAddr();
648   
649    /* -- Defield -- */
650   
651    bool isTop = !isTff;
652   
653    separateFields(PicBufY + nPadLuma + nStride*nPadLuma, pcBottomFieldY, nStride, nWidth, nHeight, isTop);
654    separateFields(PicBufU + nPadChroma + (nStride >> 1)*nPadChroma, pcBottomFieldU, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
655    separateFields(PicBufV + nPadChroma + (nStride >> 1)*nPadChroma, pcBottomFieldV, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
656   
657    // Compute image characteristics
658    if ( getUseAdaptiveQP() )
659    {
660      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcBottomField ) );
661    }   
662  }
663 
664  if ( ( !(m_iNumPicRcvd) || (!flush && m_iPOCLast != 1 && m_iNumPicRcvd != m_iGOPSize && m_iGOPSize)) )
665  {
666    iNumEncoded = 0;
667    return;
668  }
669 
670  // compress GOP
671  m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, true, isTff);
672 
673  iNumEncoded = m_iNumPicRcvd;
674  m_iNumPicRcvd = 0;
675  m_uiNumAllPicCoded += iNumEncoded;
676}
677#endif
678// ====================================================================================================================
679// Protected member functions
680// ====================================================================================================================
681
682/**
683 - Application has picture buffer list with size of GOP + 1
684 - Picture buffer list acts like as ring buffer
685 - End of the list has the latest picture
686 .
687 \retval rpcPic obtained picture buffer
688 */
689Void TEncTop::xGetNewPicBuffer ( TComPic*& rpcPic )
690{
691  TComSlice::sortPicList(m_cListPic);
692 
693  if (m_cListPic.size() >= (UInt)(m_iGOPSize + getMaxDecPicBuffering(MAX_TLAYER-1) + 2) )
694  {
695    TComList<TComPic*>::iterator iterPic  = m_cListPic.begin();
696    Int iSize = Int( m_cListPic.size() );
697    for ( Int i = 0; i < iSize; i++ )
698    {
699      rpcPic = *(iterPic++);
700      if(rpcPic->getSlice(0)->isReferenced() == false)
701      {
702        break;
703      }
704    }
705  }
706  else
707  {
708    if ( getUseAdaptiveQP() )
709    {
710      TEncPic* pcEPic = new TEncPic;
711      pcEPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, m_cPPS.getMaxCuDQPDepth()+1 ,
712                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics);
713      rpcPic = pcEPic;
714    }
715    else
716    {
717      rpcPic = new TComPic;
718
719      rpcPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
720                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics);
721    }
722    m_cListPic.pushBack( rpcPic );
723  }
724  rpcPic->setReconMark (false);
725 
726  m_iPOCLast++;
727  m_iNumPicRcvd++;
728 
729  rpcPic->getSlice(0)->setPOC( m_iPOCLast );
730  // mark it should be extended
731  rpcPic->getPicYuvRec()->setBorderExtension(false);
732
733#if H_MV
734  rpcPic->getPicYuvOrg()->setBorderExtension(false);
735#endif
736}
737
738Void TEncTop::xInitSPS()
739{
740#if H_MV
741  m_cSPS.setSPSId( getLayerIdInVps() );
742  m_cSPS.setLayerId( getLayerId() );
743 // Code below needs to be moved to VPS
744#endif
745  ProfileTierLevel& profileTierLevel = *m_cSPS.getPTL()->getGeneralPTL();
746  profileTierLevel.setLevelIdc(m_level);
747  profileTierLevel.setTierFlag(m_levelTier);
748  profileTierLevel.setProfileIdc(m_profile);
749  profileTierLevel.setProfileCompatibilityFlag(m_profile, 1);
750  profileTierLevel.setProgressiveSourceFlag(m_progressiveSourceFlag);
751  profileTierLevel.setInterlacedSourceFlag(m_interlacedSourceFlag);
752  profileTierLevel.setNonPackedConstraintFlag(m_nonPackedConstraintFlag);
753  profileTierLevel.setFrameOnlyConstraintFlag(m_frameOnlyConstraintFlag);
754 
755  if (m_profile == Profile::MAIN10 && g_bitDepthY == 8 && g_bitDepthC == 8)
756  {
757    /* The above constraint is equal to Profile::MAIN */
758    profileTierLevel.setProfileCompatibilityFlag(Profile::MAIN, 1);
759  }
760  if (m_profile == Profile::MAIN)
761  {
762    /* A Profile::MAIN10 decoder can always decode Profile::MAIN */
763    profileTierLevel.setProfileCompatibilityFlag(Profile::MAIN10, 1);
764  }
765  /* XXX: should Main be marked as compatible with still picture? */
766  /* XXX: may be a good idea to refactor the above into a function
767   * that chooses the actual compatibility based upon options */
768
769#if H_MV 
770  m_cSPS.setUpdateRepFormatFlag           ( false );   
771#if H_MV_HLS10_MULTILAYERSPS 
772  Bool multiLayerExtensionFlag  = ( getLayerId() > 0 ) && ( m_cVPS->getNumRefLayers( getLayerId() ) > 0 ); 
773 
774  m_cSPS.setSpsExtOrMaxSubLayersMinus1( multiLayerExtensionFlag ? 7 : m_maxTempLayer - 1 );
775  if ( multiLayerExtensionFlag )
776  {
777    m_cSPS.setSpsInferScalingListFlag   ( true ); 
778    m_cSPS.setSpsScalingListRefLayerId( m_cVPS->getIdRefLayer( getLayerId(), 0 ) ); 
779  }
780#else
781  m_cSPS.setSpsInferScalingListFlag       ( m_layerId > 0 && m_cVPS->getInDirectDependencyFlag( getLayerIdInVps(), 0 ) ); 
782  m_cSPS.setSpsScalingListRefLayerId      ( 0              ); 
783#endif
784  m_cSPS.setSpsExtensionPresentFlag       ( true ); 
785  m_cSPS.setSpsMultilayerExtensionFlag    ( true ); 
786#if H_3D
787  m_cSPS.setSps3dExtensionFlag            ( true ); 
788#endif
789#endif
790  m_cSPS.setPicWidthInLumaSamples         ( m_iSourceWidth      );
791  m_cSPS.setPicHeightInLumaSamples        ( m_iSourceHeight     );
792  m_cSPS.setConformanceWindow             ( m_conformanceWindow );
793  m_cSPS.setMaxCUWidth    ( g_uiMaxCUWidth      );
794  m_cSPS.setMaxCUHeight   ( g_uiMaxCUHeight     );
795  m_cSPS.setMaxCUDepth    ( g_uiMaxCUDepth      );
796
797  Int minCUSize = m_cSPS.getMaxCUWidth() >> ( m_cSPS.getMaxCUDepth()-g_uiAddCUDepth );
798  Int log2MinCUSize = 0;
799  while(minCUSize > 1)
800  {
801    minCUSize >>= 1;
802    log2MinCUSize++;
803  }
804
805  m_cSPS.setLog2MinCodingBlockSize(log2MinCUSize);
806  m_cSPS.setLog2DiffMaxMinCodingBlockSize(m_cSPS.getMaxCUDepth()-g_uiAddCUDepth);
807 
808  m_cSPS.setPCMLog2MinSize (m_uiPCMLog2MinSize);
809  m_cSPS.setUsePCM        ( m_usePCM           );
810  m_cSPS.setPCMLog2MaxSize( m_pcmLog2MaxSize  );
811
812  m_cSPS.setQuadtreeTULog2MaxSize( m_uiQuadtreeTULog2MaxSize );
813  m_cSPS.setQuadtreeTULog2MinSize( m_uiQuadtreeTULog2MinSize );
814  m_cSPS.setQuadtreeTUMaxDepthInter( m_uiQuadtreeTUMaxDepthInter    );
815  m_cSPS.setQuadtreeTUMaxDepthIntra( m_uiQuadtreeTUMaxDepthIntra    );
816 
817  m_cSPS.setTMVPFlagsPresent(false);
818
819  m_cSPS.setMaxTrSize   ( 1 << m_uiQuadtreeTULog2MaxSize );
820 
821  Int i;
822 
823  for (i = 0; i < g_uiMaxCUDepth-g_uiAddCUDepth; i++ )
824  {
825    m_cSPS.setAMPAcc( i, m_useAMP );
826    //m_cSPS.setAMPAcc( i, 1 );
827  }
828
829  m_cSPS.setUseAMP ( m_useAMP );
830
831  for (i = g_uiMaxCUDepth-g_uiAddCUDepth; i < g_uiMaxCUDepth; i++ )
832  {
833    m_cSPS.setAMPAcc(i, 0);
834  }
835
836  m_cSPS.setBitDepthY( g_bitDepthY );
837  m_cSPS.setBitDepthC( g_bitDepthC );
838
839  m_cSPS.setQpBDOffsetY ( 6*(g_bitDepthY - 8) );
840  m_cSPS.setQpBDOffsetC ( 6*(g_bitDepthC - 8) );
841
842  m_cSPS.setUseSAO( m_bUseSAO );
843
844  m_cSPS.setMaxTLayers( m_maxTempLayer );
845  m_cSPS.setTemporalIdNestingFlag( ( m_maxTempLayer == 1 ) ? true : false );
846  for ( i = 0; i < min(m_cSPS.getMaxTLayers(),(UInt) MAX_TLAYER); i++ )
847  {
848    m_cSPS.setMaxDecPicBuffering(m_maxDecPicBuffering[i], i);
849    m_cSPS.setNumReorderPics(m_numReorderPics[i], i);
850  }
851#if H_MV
852  for ( Int ols = 0; ols < m_cVPS->getNumOutputLayerSets(); ols++)
853  {
854    // Check MaxDecPicBuffering
855    const std::vector<Int>& targetDecLayerIdList = m_cVPS->getTargetDecLayerIdList( m_cVPS->olsIdxToLsIdx( ols )); 
856    for( Int is = 0; is < targetDecLayerIdList.size(); is++  )
857    {
858#if H_MV_HLS10_ADD_LAYERSETS
859      if ( m_cVPS->getNecessaryLayerFlag( ols, is ) )
860      {     
861        m_cSPS.inferSpsMaxDecPicBufferingMinus1( m_cVPS, ols, targetDecLayerIdList[is], true );       
862      }
863#else
864      m_cSPS.inferSpsMaxDecPicBufferingMinus1( m_cVPS, ols, targetDecLayerIdList[is], true );       
865#endif
866    }
867  }
868#if !H_MV_HLS10_ADD_LAYERSETS
869  m_cVPS->inferDbpSizeLayerSetZero( &m_cSPS, true ); 
870#endif
871#endif
872  m_cSPS.setPCMBitDepthLuma (g_uiPCMBitDepthLuma);
873  m_cSPS.setPCMBitDepthChroma (g_uiPCMBitDepthChroma);
874  m_cSPS.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag );
875
876  m_cSPS.setScalingListFlag ( (m_useScalingListId == 0) ? 0 : 1 );
877
878  m_cSPS.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
879
880  m_cSPS.setVuiParametersPresentFlag(getVuiParametersPresentFlag());
881  if (m_cSPS.getVuiParametersPresentFlag())
882  {
883    TComVUI* pcVUI = m_cSPS.getVuiParameters();
884    pcVUI->setAspectRatioInfoPresentFlag(getAspectRatioIdc() != -1);
885    pcVUI->setAspectRatioIdc(getAspectRatioIdc());
886    pcVUI->setSarWidth(getSarWidth());
887    pcVUI->setSarHeight(getSarHeight());
888    pcVUI->setOverscanInfoPresentFlag(getOverscanInfoPresentFlag());
889    pcVUI->setOverscanAppropriateFlag(getOverscanAppropriateFlag());
890#if H_MV
891    pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag() && getLayerId() == 0 );
892#else
893   pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag());
894#endif
895    pcVUI->setVideoFormat(getVideoFormat());
896    pcVUI->setVideoFullRangeFlag(getVideoFullRangeFlag());
897    pcVUI->setColourDescriptionPresentFlag(getColourDescriptionPresentFlag());
898    pcVUI->setColourPrimaries(getColourPrimaries());
899    pcVUI->setTransferCharacteristics(getTransferCharacteristics());
900    pcVUI->setMatrixCoefficients(getMatrixCoefficients());
901    pcVUI->setChromaLocInfoPresentFlag(getChromaLocInfoPresentFlag());
902    pcVUI->setChromaSampleLocTypeTopField(getChromaSampleLocTypeTopField());
903    pcVUI->setChromaSampleLocTypeBottomField(getChromaSampleLocTypeBottomField());
904    pcVUI->setNeutralChromaIndicationFlag(getNeutralChromaIndicationFlag());
905    pcVUI->setDefaultDisplayWindow(getDefaultDisplayWindow());
906    pcVUI->setFrameFieldInfoPresentFlag(getFrameFieldInfoPresentFlag());
907    pcVUI->setFieldSeqFlag(false);
908    pcVUI->setHrdParametersPresentFlag(false);
909    pcVUI->getTimingInfo()->setPocProportionalToTimingFlag(getPocProportionalToTimingFlag());
910    pcVUI->getTimingInfo()->setNumTicksPocDiffOneMinus1   (getNumTicksPocDiffOneMinus1()   );
911    pcVUI->setBitstreamRestrictionFlag(getBitstreamRestrictionFlag());
912    pcVUI->setTilesFixedStructureFlag(getTilesFixedStructureFlag());
913    pcVUI->setMotionVectorsOverPicBoundariesFlag(getMotionVectorsOverPicBoundariesFlag());
914    pcVUI->setMinSpatialSegmentationIdc(getMinSpatialSegmentationIdc());
915    pcVUI->setMaxBytesPerPicDenom(getMaxBytesPerPicDenom());
916    pcVUI->setMaxBitsPerMinCuDenom(getMaxBitsPerMinCuDenom());
917    pcVUI->setLog2MaxMvLengthHorizontal(getLog2MaxMvLengthHorizontal());
918    pcVUI->setLog2MaxMvLengthVertical(getLog2MaxMvLengthVertical());
919  }
920}
921
922Void TEncTop::xInitPPS()
923{
924#if H_MV
925  m_cPPS.setLayerId( getLayerId() );
926  if( getVPS()->getNumDirectRefLayers( getLayerId() ) > 0 )
927  {
928    m_cPPS.setListsModificationPresentFlag( true );
929  }
930  m_cPPS.setPPSId( getLayerIdInVps() );
931  m_cPPS.setSPSId( getLayerIdInVps() );
932  m_cPPS.setPpsMultilayerExtensionFlag    ( true ); 
933#if H_3D
934  m_cPPS.setPps3dExtensionFlag            ( true ); 
935#endif
936#endif
937
938#if H_3D
939  m_cPPS.setDLT( getDLT() );
940#endif
941  m_cPPS.setConstrainedIntraPred( m_bUseConstrainedIntraPred );
942  Bool bUseDQP = (getMaxCuDQPDepth() > 0)? true : false;
943
944      if((getMaxDeltaQP() != 0 )|| getUseAdaptiveQP())
945      {
946        bUseDQP = true;
947      }
948
949  if(bUseDQP)
950  {
951    m_cPPS.setUseDQP(true);
952    m_cPPS.setMaxCuDQPDepth( m_iMaxCuDQPDepth );
953    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
954  }
955  else
956  {
957    m_cPPS.setUseDQP(false);
958    m_cPPS.setMaxCuDQPDepth( 0 );
959    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
960  }
961
962  if ( m_RCEnableRateControl )
963  {
964    m_cPPS.setUseDQP(true);
965    m_cPPS.setMaxCuDQPDepth( 0 );
966    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
967  }
968
969  m_cPPS.setChromaCbQpOffset( m_chromaCbQpOffset );
970  m_cPPS.setChromaCrQpOffset( m_chromaCrQpOffset );
971
972  m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams);
973  m_cPPS.setEntropyCodingSyncEnabledFlag( m_iWaveFrontSynchro > 0 );
974  m_cPPS.setTilesEnabledFlag( (m_iNumColumnsMinus1 > 0 || m_iNumRowsMinus1 > 0) );
975  m_cPPS.setUseWP( m_useWeightedPred );
976  m_cPPS.setWPBiPred( m_useWeightedBiPred );
977  m_cPPS.setOutputFlagPresentFlag( false );
978#if H_MV
979  m_cPPS.setNumExtraSliceHeaderBits( 3 ); 
980#endif
981  m_cPPS.setSignHideFlag(getSignHideFlag());
982  if ( getDeblockingFilterMetric() )
983  {
984    m_cPPS.setDeblockingFilterControlPresentFlag (true);
985    m_cPPS.setDeblockingFilterOverrideEnabledFlag(true);
986    m_cPPS.setPicDisableDeblockingFilterFlag(false);
987    m_cPPS.setDeblockingFilterBetaOffsetDiv2(0);
988    m_cPPS.setDeblockingFilterTcOffsetDiv2(0);
989  } 
990  else
991  {
992    m_cPPS.setDeblockingFilterControlPresentFlag (m_DeblockingFilterControlPresent );
993  }
994  m_cPPS.setLog2ParallelMergeLevelMinus2   (m_log2ParallelMergeLevelMinus2 );
995  m_cPPS.setCabacInitPresentFlag(CABAC_INIT_PRESENT_FLAG);
996  m_cPPS.setLoopFilterAcrossSlicesEnabledFlag( m_bLFCrossSliceBoundaryFlag );
997  Int histogram[MAX_NUM_REF + 1];
998  for( Int i = 0; i <= MAX_NUM_REF; i++ )
999  {
1000    histogram[i]=0;
1001  }
1002  for( Int i = 0; i < getGOPSize(); i++ )
1003  {
1004    assert(getGOPEntry(i).m_numRefPicsActive >= 0 && getGOPEntry(i).m_numRefPicsActive <= MAX_NUM_REF);
1005    histogram[getGOPEntry(i).m_numRefPicsActive]++;
1006  }
1007  Int maxHist=-1;
1008  Int bestPos=0;
1009  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1010  {
1011    if(histogram[i]>maxHist)
1012    {
1013      maxHist=histogram[i];
1014      bestPos=i;
1015    }
1016  }
1017  assert(bestPos <= 15);
1018  m_cPPS.setNumRefIdxL0DefaultActive(bestPos);
1019  m_cPPS.setNumRefIdxL1DefaultActive(bestPos);
1020  m_cPPS.setTransquantBypassEnableFlag(getTransquantBypassEnableFlag());
1021  m_cPPS.setUseTransformSkip( m_useTransformSkip );
1022  if (m_sliceSegmentMode)
1023  {
1024    m_cPPS.setDependentSliceSegmentsEnabledFlag( true );
1025  }
1026  if( m_cPPS.getDependentSliceSegmentsEnabledFlag() )
1027  {
1028    Int NumCtx = m_cPPS.getEntropyCodingSyncEnabledFlag()?2:1;
1029    m_cSliceEncoder.initCtxMem( NumCtx );
1030    for ( UInt st = 0; st < NumCtx; st++ )
1031    {
1032      TEncSbac* ctx = NULL;
1033      ctx = new TEncSbac;
1034      ctx->init( &m_cBinCoderCABAC );
1035      m_cSliceEncoder.setCtxMem( ctx, st );
1036    }
1037  }
1038}
1039
1040//Function for initializing m_RPSList, a list of TComReferencePictureSet, based on the GOPEntry objects read from the config file.
1041Void TEncTop::xInitRPS(Bool isFieldCoding)
1042{
1043  TComReferencePictureSet*      rps;
1044 
1045  m_cSPS.createRPSList(getGOPSize()+m_extraRPSs+1);
1046  TComRPSList* rpsList = m_cSPS.getRPSList();
1047
1048  for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++) 
1049  {
1050    GOPEntry ge = getGOPEntry(i);
1051    rps = rpsList->getReferencePictureSet(i);
1052    rps->setNumberOfPictures(ge.m_numRefPics);
1053    rps->setNumRefIdc(ge.m_numRefIdc);
1054    Int numNeg = 0;
1055    Int numPos = 0;
1056    for( Int j = 0; j < ge.m_numRefPics; j++)
1057    {
1058      rps->setDeltaPOC(j,ge.m_referencePics[j]);
1059      rps->setUsed(j,ge.m_usedByCurrPic[j]);
1060      if(ge.m_referencePics[j]>0)
1061      {
1062        numPos++;
1063      }
1064      else
1065      {
1066        numNeg++;
1067      }
1068    }
1069    rps->setNumberOfNegativePictures(numNeg);
1070    rps->setNumberOfPositivePictures(numPos);
1071
1072    // handle inter RPS intialization from the config file.
1073#if AUTO_INTER_RPS
1074    rps->setInterRPSPrediction(ge.m_interRPSPrediction > 0);  // not very clean, converting anything > 0 to true.
1075    rps->setDeltaRIdxMinus1(0);                               // index to the Reference RPS is always the previous one.
1076    TComReferencePictureSet*     RPSRef = rpsList->getReferencePictureSet(i-1);  // get the reference RPS
1077
1078    if (ge.m_interRPSPrediction == 2)  // Automatic generation of the inter RPS idc based on the RIdx provided.
1079    {
1080      Int deltaRPS = getGOPEntry(i-1).m_POC - ge.m_POC;  // the ref POC - current POC
1081      Int numRefDeltaPOC = RPSRef->getNumberOfPictures();
1082
1083      rps->setDeltaRPS(deltaRPS);           // set delta RPS
1084      rps->setNumRefIdc(numRefDeltaPOC+1);  // set the numRefIdc to the number of pictures in the reference RPS + 1.
1085      Int count=0;
1086      for (Int j = 0; j <= numRefDeltaPOC; j++ ) // cycle through pics in reference RPS.
1087      {
1088        Int RefDeltaPOC = (j<numRefDeltaPOC)? RPSRef->getDeltaPOC(j): 0;  // if it is the last decoded picture, set RefDeltaPOC = 0
1089        rps->setRefIdc(j, 0);
1090        for (Int k = 0; k < rps->getNumberOfPictures(); k++ )  // cycle through pics in current RPS.
1091        {
1092          if (rps->getDeltaPOC(k) == ( RefDeltaPOC + deltaRPS))  // if the current RPS has a same picture as the reference RPS.
1093          {
1094              rps->setRefIdc(j, (rps->getUsed(k)?1:2));
1095              count++;
1096              break;
1097          }
1098        }
1099      }
1100      if (count != rps->getNumberOfPictures())
1101      {
1102        printf("Warning: Unable fully predict all delta POCs using the reference RPS index given in the config file.  Setting Inter RPS to false for this RPS.\n");
1103        rps->setInterRPSPrediction(0);
1104      }
1105    }
1106    else if (ge.m_interRPSPrediction == 1)  // inter RPS idc based on the RefIdc values provided in config file.
1107    {
1108      rps->setDeltaRPS(ge.m_deltaRPS);
1109      rps->setNumRefIdc(ge.m_numRefIdc);
1110      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1111      {
1112        rps->setRefIdc(j, ge.m_refIdc[j]);
1113      }
1114#if WRITE_BACK
1115      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1116      // computed from the RefIdc.  A warning is printed if they are not identical.
1117      numNeg = 0;
1118      numPos = 0;
1119      TComReferencePictureSet      RPSTemp;  // temporary variable
1120
1121      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1122      {
1123        if (ge.m_refIdc[j])
1124        {
1125          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1126          RPSTemp.setDeltaPOC((numNeg+numPos),deltaPOC);
1127          RPSTemp.setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1128          if (deltaPOC<0)
1129          {
1130            numNeg++;
1131          }
1132          else
1133          {
1134            numPos++;
1135          }
1136        }
1137      }
1138      if (numNeg != rps->getNumberOfNegativePictures())
1139      {
1140        printf("Warning: number of negative pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1141        rps->setNumberOfNegativePictures(numNeg);
1142        rps->setNumberOfPictures(numNeg+numPos);
1143      }
1144      if (numPos != rps->getNumberOfPositivePictures())
1145      {
1146        printf("Warning: number of positive pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1147        rps->setNumberOfPositivePictures(numPos);
1148        rps->setNumberOfPictures(numNeg+numPos);
1149      }
1150      RPSTemp.setNumberOfPictures(numNeg+numPos);
1151      RPSTemp.setNumberOfNegativePictures(numNeg);
1152      RPSTemp.sortDeltaPOC();     // sort the created delta POC before comparing
1153      // check if Delta POC and Used are the same
1154      // print warning if they are not.
1155      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1156      {
1157        if (RPSTemp.getDeltaPOC(j) != rps->getDeltaPOC(j))
1158        {
1159          printf("Warning: delta POC is different between intra RPS and inter RPS specified in the config file.\n");
1160          rps->setDeltaPOC(j,RPSTemp.getDeltaPOC(j));
1161        }
1162        if (RPSTemp.getUsed(j) != rps->getUsed(j))
1163        {
1164          printf("Warning: Used by Current in RPS is different between intra and inter RPS specified in the config file.\n");
1165          rps->setUsed(j,RPSTemp.getUsed(j));
1166        }
1167      }
1168#endif
1169    }
1170#else
1171    rps->setInterRPSPrediction(ge.m_interRPSPrediction);
1172    if (ge.m_interRPSPrediction)
1173    {
1174      rps->setDeltaRIdxMinus1(0);
1175      rps->setDeltaRPS(ge.m_deltaRPS);
1176      rps->setNumRefIdc(ge.m_numRefIdc);
1177      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1178      {
1179        rps->setRefIdc(j, ge.m_refIdc[j]);
1180      }
1181#if WRITE_BACK
1182      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1183      // computed from the RefIdc.  This is not necessary if both are identical. Currently there is no check to see if they are identical.
1184      numNeg = 0;
1185      numPos = 0;
1186      TComReferencePictureSet*     RPSRef = m_RPSList.getReferencePictureSet(i-1);
1187
1188      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1189      {
1190        if (ge.m_refIdc[j])
1191        {
1192          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1193          rps->setDeltaPOC((numNeg+numPos),deltaPOC);
1194          rps->setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1195          if (deltaPOC<0)
1196          {
1197            numNeg++;
1198          }
1199          else
1200          {
1201            numPos++;
1202          }
1203        }
1204      }
1205      rps->setNumberOfNegativePictures(numNeg);
1206      rps->setNumberOfPositivePictures(numPos);
1207      rps->sortDeltaPOC();
1208#endif
1209    }
1210#endif //INTER_RPS_AUTO
1211  }
1212  //In case of field coding, we need to set special parameters for the first bottom field of the sequence, since it is not specified in the cfg file.
1213  //The position = GOPSize + extraRPSs which is (a priori) unused is reserved for this field in the RPS.
1214  if (isFieldCoding) 
1215  {
1216    rps = rpsList->getReferencePictureSet(getGOPSize()+m_extraRPSs);
1217    rps->setNumberOfPictures(1);
1218    rps->setNumberOfNegativePictures(1);
1219    rps->setNumberOfPositivePictures(0);
1220    rps->setNumberOfLongtermPictures(0);
1221    rps->setDeltaPOC(0,-1);
1222    rps->setPOC(0,0);
1223    rps->setUsed(0,true);
1224    rps->setInterRPSPrediction(false);
1225    rps->setDeltaRIdxMinus1(0);
1226    rps->setDeltaRPS(0);
1227    rps->setNumRefIdc(0);
1228}
1229}
1230
1231   // This is a function that
1232   // determines what Reference Picture Set to use
1233   // for a specific slice (with POC = POCCurr)
1234Void TEncTop::selectReferencePictureSet(TComSlice* slice, Int POCCurr, Int GOPid )
1235{
1236#if H_MV
1237  if( slice->getRapPicFlag() == true && getLayerId() > 0 && POCCurr == 0 )
1238  {
1239    TComReferencePictureSet* rps = slice->getLocalRPS();
1240    rps->setNumberOfNegativePictures(0);
1241    rps->setNumberOfPositivePictures(0);
1242    rps->setNumberOfLongtermPictures(0);
1243    rps->setNumberOfPictures(0);
1244    slice->setRPS(rps);
1245  }
1246  else
1247  {
1248#endif
1249  slice->setRPSidx(GOPid);
1250
1251  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1252  {   
1253    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1254    {
1255      Int POCIndex = POCCurr%m_uiIntraPeriod;
1256      if(POCIndex == 0)
1257      {
1258        POCIndex = m_uiIntraPeriod;
1259      }
1260      if(POCIndex == m_GOPList[extraNum].m_POC)
1261      {
1262        slice->setRPSidx(extraNum);
1263      }
1264    }
1265    else
1266    {
1267      if(POCCurr==m_GOPList[extraNum].m_POC)
1268      {
1269        slice->setRPSidx(extraNum);
1270      }
1271    }
1272  }
1273  if(POCCurr == 1 && slice->getPic()->isField())
1274  {
1275    slice->setRPSidx(m_iGOPSize+m_extraRPSs);
1276  }
1277
1278  slice->setRPS(getSPS()->getRPSList()->getReferencePictureSet(slice->getRPSidx()));
1279  slice->getRPS()->setNumberOfPictures(slice->getRPS()->getNumberOfNegativePictures()+slice->getRPS()->getNumberOfPositivePictures());
1280#if H_MV
1281  }
1282#endif
1283
1284}
1285
1286Int TEncTop::getReferencePictureSetIdxForSOP(TComSlice* slice, Int POCCurr, Int GOPid )
1287{
1288  int rpsIdx = GOPid;
1289
1290  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1291  {   
1292    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1293    {
1294      Int POCIndex = POCCurr%m_uiIntraPeriod;
1295      if(POCIndex == 0)
1296      {
1297        POCIndex = m_uiIntraPeriod;
1298      }
1299      if(POCIndex == m_GOPList[extraNum].m_POC)
1300      {
1301        rpsIdx = extraNum;
1302      }
1303    }
1304    else
1305    {
1306      if(POCCurr==m_GOPList[extraNum].m_POC)
1307      {
1308        rpsIdx = extraNum;
1309      }
1310    }
1311  }
1312
1313  return rpsIdx;
1314}
1315
1316Void  TEncTop::xInitPPSforTiles()
1317{
1318  m_cPPS.setUniformSpacingFlag( m_iUniformSpacingIdr );
1319  m_cPPS.setNumColumnsMinus1( m_iNumColumnsMinus1 );
1320  m_cPPS.setNumRowsMinus1( m_iNumRowsMinus1 );
1321  if( m_iUniformSpacingIdr == 0 )
1322  {
1323    m_cPPS.setColumnWidth( m_puiColumnWidth );
1324    m_cPPS.setRowHeight( m_puiRowHeight );
1325  }
1326  m_cPPS.setLoopFilterAcrossTilesEnabledFlag( m_loopFilterAcrossTilesEnabledFlag );
1327
1328  // # substreams is "per tile" when tiles are independent.
1329  if (m_iWaveFrontSynchro
1330    )
1331  {
1332    m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams * (m_iNumColumnsMinus1+1));
1333  }
1334}
1335
1336Void  TEncCfg::xCheckGSParameters()
1337{
1338  Int   iWidthInCU = ( m_iSourceWidth%g_uiMaxCUWidth ) ? m_iSourceWidth/g_uiMaxCUWidth + 1 : m_iSourceWidth/g_uiMaxCUWidth;
1339  Int   iHeightInCU = ( m_iSourceHeight%g_uiMaxCUHeight ) ? m_iSourceHeight/g_uiMaxCUHeight + 1 : m_iSourceHeight/g_uiMaxCUHeight;
1340  UInt  uiCummulativeColumnWidth = 0;
1341  UInt  uiCummulativeRowHeight = 0;
1342
1343  //check the column relative parameters
1344  if( m_iNumColumnsMinus1 >= (1<<(LOG2_MAX_NUM_COLUMNS_MINUS1+1)) )
1345  {
1346    printf( "The number of columns is larger than the maximum allowed number of columns.\n" );
1347    exit( EXIT_FAILURE );
1348  }
1349
1350  if( m_iNumColumnsMinus1 >= iWidthInCU )
1351  {
1352    printf( "The current picture can not have so many columns.\n" );
1353    exit( EXIT_FAILURE );
1354  }
1355
1356  if( m_iNumColumnsMinus1 && m_iUniformSpacingIdr==0 )
1357  {
1358    for(Int i=0; i<m_iNumColumnsMinus1; i++)
1359    {
1360      uiCummulativeColumnWidth += m_puiColumnWidth[i];
1361    }
1362
1363    if( uiCummulativeColumnWidth >= iWidthInCU )
1364    {
1365      printf( "The width of the column is too large.\n" );
1366      exit( EXIT_FAILURE );
1367    }
1368  }
1369
1370  //check the row relative parameters
1371  if( m_iNumRowsMinus1 >= (1<<(LOG2_MAX_NUM_ROWS_MINUS1+1)) )
1372  {
1373    printf( "The number of rows is larger than the maximum allowed number of rows.\n" );
1374    exit( EXIT_FAILURE );
1375  }
1376
1377  if( m_iNumRowsMinus1 >= iHeightInCU )
1378  {
1379    printf( "The current picture can not have so many rows.\n" );
1380    exit( EXIT_FAILURE );
1381  }
1382
1383  if( m_iNumRowsMinus1 && m_iUniformSpacingIdr==0 )
1384  {
1385    for(Int i=0; i<m_iNumRowsMinus1; i++)
1386      uiCummulativeRowHeight += m_puiRowHeight[i];
1387
1388    if( uiCummulativeRowHeight >= iHeightInCU )
1389    {
1390      printf( "The height of the row is too large.\n" );
1391      exit( EXIT_FAILURE );
1392    }
1393  }
1394}
1395#if H_MV
1396Void TEncTop::printSummary( Int numAllPicCoded, Bool isField )
1397{
1398  assert ( !isField ); // Multiview and field coding need to be further unified
1399  assert (numAllPicCoded == m_cAnalyzeAll.getNumPic());
1400
1401  //--CFG_KDY
1402  m_cAnalyzeAll.setFrmRate( getFrameRate() );
1403  m_cAnalyzeI.setFrmRate( getFrameRate() );
1404  m_cAnalyzeP.setFrmRate( getFrameRate() );
1405  m_cAnalyzeB.setFrmRate( getFrameRate() );
1406
1407  //-- all
1408  printf( "\n\nSUMMARY ------------------------------------------- LayerId %2d\n", m_layerId );
1409
1410  m_cAnalyzeAll.printOut('a');
1411
1412  printf( "\n\nI Slices--------------------------------------------------------\n" );
1413  m_cAnalyzeI.printOut('i');
1414
1415  printf( "\n\nP Slices--------------------------------------------------------\n" );
1416  m_cAnalyzeP.printOut('p');
1417
1418  printf( "\n\nB Slices--------------------------------------------------------\n" );
1419  m_cAnalyzeB.printOut('b');
1420
1421#if _SUMMARY_OUT_
1422  m_cAnalyzeAll.printSummaryOut();
1423#endif
1424#if _SUMMARY_PIC_
1425  m_cAnalyzeI.printSummary('I');
1426  m_cAnalyzeP.printSummary('P');
1427  m_cAnalyzeB.printSummary('B');
1428#endif
1429}
1430
1431Int TEncTop::getFrameId(Int iGOPid) 
1432{
1433  if(m_iPOCLast == 0)
1434  {
1435    return(0 );
1436  }
1437  else
1438  {
1439    return m_iPOCLast -m_iNumPicRcvd+ getGOPEntry(iGOPid).m_POC ;
1440  }
1441}
1442
1443TComPic* TEncTop::getPic( Int poc )
1444{
1445  TComList<TComPic*>* listPic = getListPic();
1446  TComPic* pcPic = NULL;
1447  for(TComList<TComPic*>::iterator it=listPic->begin(); it!=listPic->end(); it++)
1448  {
1449    if( (*it)->getPOC() == poc )
1450    {
1451      pcPic = *it ;
1452      break ;
1453    }
1454  }
1455  return pcPic;
1456}
1457#endif
1458
1459#if H_3D_VSO
1460Void TEncTop::setupRenModel( Int iPoc, Int iEncViewIdx, Int iEncContent, Int iHorOffset )
1461{
1462  TRenModel* rendererModel = m_cRdCost.getRenModel(); 
1463  rendererModel->setupPart( iHorOffset, std::min( (Int) g_uiMaxCUHeight, (Int) ( m_iSourceHeight - iHorOffset ) )) ; 
1464 
1465  Int iEncViewSIdx = m_cameraParameters->getBaseId2SortedId()[ iEncViewIdx ];
1466
1467  // setup base views
1468  Int iNumOfBV = m_renderModelParameters->getNumOfBaseViewsForView( iEncViewSIdx, iEncContent );
1469
1470  for (Int iCurView = 0; iCurView < iNumOfBV; iCurView++ )
1471  {
1472    Int iBaseViewSIdx;
1473    Int iVideoDistMode;
1474    Int iDepthDistMode;
1475
1476    m_renderModelParameters->getBaseViewData( iEncViewSIdx, iEncContent, iCurView, iBaseViewSIdx, iVideoDistMode, iDepthDistMode );
1477
1478    AOT( iVideoDistMode < 0 || iVideoDistMode > 2 );
1479
1480    Int iBaseViewIdx = m_cameraParameters->getBaseSortedId2Id()[ iBaseViewSIdx ];
1481
1482    TComPicYuv* pcPicYuvVideoRec  = m_ivPicLists->getPicYuv( iBaseViewIdx, false, iPoc, true  );
1483    TComPicYuv* pcPicYuvDepthRec  = m_ivPicLists->getPicYuv( iBaseViewIdx, true , iPoc, true  );
1484    TComPicYuv* pcPicYuvVideoOrg  = m_ivPicLists->getPicYuv( iBaseViewIdx, false, iPoc, false );
1485    TComPicYuv* pcPicYuvDepthOrg  = m_ivPicLists->getPicYuv( iBaseViewIdx, true , iPoc, false );   
1486
1487    TComPicYuv* pcPicYuvVideoRef  = ( iVideoDistMode == 2 ) ? pcPicYuvVideoOrg  : NULL;
1488    TComPicYuv* pcPicYuvDepthRef  = ( iDepthDistMode == 2 ) ? pcPicYuvDepthOrg  : NULL;
1489
1490    TComPicYuv* pcPicYuvVideoTest = ( iVideoDistMode == 0 ) ? pcPicYuvVideoOrg  : pcPicYuvVideoRec;
1491    TComPicYuv* pcPicYuvDepthTest = ( iDepthDistMode == 0 ) ? pcPicYuvDepthOrg  : pcPicYuvDepthRec;
1492
1493    AOT( (iVideoDistMode == 2) != (pcPicYuvVideoRef != NULL) );
1494    AOT( (iDepthDistMode == 2) != (pcPicYuvDepthRef != NULL) );
1495    AOT( pcPicYuvDepthTest == NULL );
1496    AOT( pcPicYuvVideoTest == NULL );
1497
1498    rendererModel->setBaseView( iBaseViewSIdx, pcPicYuvVideoTest, pcPicYuvDepthTest, pcPicYuvVideoRef, pcPicYuvDepthRef );
1499  }
1500
1501  rendererModel->setErrorMode( iEncViewSIdx, iEncContent, 0 );
1502  // setup virtual views
1503  Int iNumOfSV  = m_renderModelParameters->getNumOfModelsForView( iEncViewSIdx, iEncContent );
1504  for (Int iCurView = 0; iCurView < iNumOfSV; iCurView++ )
1505  {
1506    Int iOrgRefBaseViewSIdx;
1507    Int iLeftBaseViewSIdx;
1508    Int iRightBaseViewSIdx;
1509    Int iSynthViewRelNum;
1510    Int iModelNum;
1511    Int iBlendMode;
1512    m_renderModelParameters->getSingleModelData(iEncViewSIdx, iEncContent, iCurView, iModelNum, iBlendMode,iLeftBaseViewSIdx, iRightBaseViewSIdx, iOrgRefBaseViewSIdx, iSynthViewRelNum );
1513
1514    Int iLeftBaseViewIdx    = -1;
1515    Int iRightBaseViewIdx   = -1;
1516
1517    TComPicYuv* pcPicYuvOrgRef  = NULL;
1518    Int**      ppiShiftLUTLeft  = NULL;
1519    Int**      ppiShiftLUTRight = NULL;
1520    Int**      ppiBaseShiftLUTLeft  = NULL;
1521    Int**      ppiBaseShiftLUTRight = NULL;
1522
1523
1524    Int        iDistToLeft      = -1;
1525
1526    Int iSynthViewIdx = m_cameraParameters->synthRelNum2Idx( iSynthViewRelNum );
1527
1528    if ( iLeftBaseViewSIdx != -1 )
1529    {
1530      iLeftBaseViewIdx   = m_cameraParameters->getBaseSortedId2Id()   [ iLeftBaseViewSIdx ];
1531      ppiShiftLUTLeft    = m_cameraParameters->getSynthViewShiftLUTI()[ iLeftBaseViewIdx  ][ iSynthViewIdx  ];
1532    }
1533
1534    if ( iRightBaseViewSIdx != -1 )
1535    {
1536      iRightBaseViewIdx  = m_cameraParameters->getBaseSortedId2Id()   [iRightBaseViewSIdx ];
1537      ppiShiftLUTRight   = m_cameraParameters->getSynthViewShiftLUTI()[ iRightBaseViewIdx ][ iSynthViewIdx ];
1538    }
1539
1540    if ( iRightBaseViewSIdx != -1 && iLeftBaseViewSIdx != -1 )
1541    {
1542      iDistToLeft          = m_cameraParameters->getRelDistLeft(  iSynthViewIdx , iLeftBaseViewIdx, iRightBaseViewIdx);
1543      ppiBaseShiftLUTLeft  = m_cameraParameters->getBaseViewShiftLUTI() [ iLeftBaseViewIdx  ][ iRightBaseViewIdx ];
1544      ppiBaseShiftLUTRight = m_cameraParameters->getBaseViewShiftLUTI() [ iRightBaseViewIdx ][ iLeftBaseViewIdx  ];
1545
1546    }
1547
1548    if ( iOrgRefBaseViewSIdx != -1 )
1549    {
1550      pcPicYuvOrgRef = m_ivPicLists->getPicYuv(  m_cameraParameters->getBaseSortedId2Id()[ iOrgRefBaseViewSIdx ] , false, iPoc, false );
1551      AOF ( pcPicYuvOrgRef );
1552    }
1553
1554    rendererModel->setSingleModel( iModelNum, ppiShiftLUTLeft, ppiBaseShiftLUTLeft, ppiShiftLUTRight, ppiBaseShiftLUTRight, iDistToLeft, pcPicYuvOrgRef );
1555  }
1556}
1557#endif
1558//! \}
Note: See TracBrowser for help on using the repository browser.