source: 3DVCSoftware/branches/HTM-12.2-dev2-HHI/source/Lib/TLibEncoder/TEncTop.cpp @ 1103

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

HHI_DEPENDENCY_SIGNALLING_I1_J0107: Integrated IdRefListLayers.

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