source: 3DVCSoftware/branches/HTM-13.1-dev1-Samsung2/source/Lib/TLibEncoder/TEncTop.cpp @ 1159

Last change on this file since 1159 was 1133, checked in by tech, 10 years ago

Merged 13.0-dev0@1132

  • Cleanup
  • TICKET083_IVPFLAG_FIX
  • Property svn:eol-style set to native
File size: 49.4 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 further 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 H_3D
899  // Check if this condition is still correct
900  if( getVPS()->getNumRefListLayers( getLayerId() ) > 0 )
901#else
902  if( getVPS()->getNumDirectRefLayers( getLayerId() ) > 0 )
903#endif
904  {
905    m_cPPS.setListsModificationPresentFlag( true );
906  }
907  m_cPPS.setPPSId( getLayerIdInVps() );
908  m_cPPS.setSPSId( getLayerIdInVps() );
909  m_cPPS.setPpsMultilayerExtensionFlag    ( true ); 
910#if H_3D
911  m_cPPS.setPps3dExtensionFlag            ( true ); 
912#endif
913#endif
914
915#if H_3D
916  m_cPPS.setDLT( getDLT() );
917#endif
918  m_cPPS.setConstrainedIntraPred( m_bUseConstrainedIntraPred );
919  Bool bUseDQP = (getMaxCuDQPDepth() > 0)? true : false;
920
921      if((getMaxDeltaQP() != 0 )|| getUseAdaptiveQP())
922      {
923        bUseDQP = true;
924      }
925
926  if(bUseDQP)
927  {
928    m_cPPS.setUseDQP(true);
929    m_cPPS.setMaxCuDQPDepth( m_iMaxCuDQPDepth );
930    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
931  }
932  else
933  {
934    m_cPPS.setUseDQP(false);
935    m_cPPS.setMaxCuDQPDepth( 0 );
936    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
937  }
938
939  if ( m_RCEnableRateControl )
940  {
941    m_cPPS.setUseDQP(true);
942    m_cPPS.setMaxCuDQPDepth( 0 );
943    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
944  }
945
946  m_cPPS.setChromaCbQpOffset( m_chromaCbQpOffset );
947  m_cPPS.setChromaCrQpOffset( m_chromaCrQpOffset );
948
949  m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams);
950  m_cPPS.setEntropyCodingSyncEnabledFlag( m_iWaveFrontSynchro > 0 );
951  m_cPPS.setTilesEnabledFlag( (m_iNumColumnsMinus1 > 0 || m_iNumRowsMinus1 > 0) );
952  m_cPPS.setUseWP( m_useWeightedPred );
953  m_cPPS.setWPBiPred( m_useWeightedBiPred );
954  m_cPPS.setOutputFlagPresentFlag( false );
955#if H_MV
956  m_cPPS.setNumExtraSliceHeaderBits( 3 ); 
957#endif
958  m_cPPS.setSignHideFlag(getSignHideFlag());
959  if ( getDeblockingFilterMetric() )
960  {
961    m_cPPS.setDeblockingFilterControlPresentFlag (true);
962    m_cPPS.setDeblockingFilterOverrideEnabledFlag(true);
963    m_cPPS.setPicDisableDeblockingFilterFlag(false);
964    m_cPPS.setDeblockingFilterBetaOffsetDiv2(0);
965    m_cPPS.setDeblockingFilterTcOffsetDiv2(0);
966  } 
967  else
968  {
969    m_cPPS.setDeblockingFilterControlPresentFlag (m_DeblockingFilterControlPresent );
970  }
971  m_cPPS.setLog2ParallelMergeLevelMinus2   (m_log2ParallelMergeLevelMinus2 );
972  m_cPPS.setCabacInitPresentFlag(CABAC_INIT_PRESENT_FLAG);
973  m_cPPS.setLoopFilterAcrossSlicesEnabledFlag( m_bLFCrossSliceBoundaryFlag );
974  Int histogram[MAX_NUM_REF + 1];
975  for( Int i = 0; i <= MAX_NUM_REF; i++ )
976  {
977    histogram[i]=0;
978  }
979  for( Int i = 0; i < getGOPSize(); i++ )
980  {
981    assert(getGOPEntry(i).m_numRefPicsActive >= 0 && getGOPEntry(i).m_numRefPicsActive <= MAX_NUM_REF);
982    histogram[getGOPEntry(i).m_numRefPicsActive]++;
983  }
984  Int maxHist=-1;
985  Int bestPos=0;
986  for( Int i = 0; i <= MAX_NUM_REF; i++ )
987  {
988    if(histogram[i]>maxHist)
989    {
990      maxHist=histogram[i];
991      bestPos=i;
992    }
993  }
994  assert(bestPos <= 15);
995  m_cPPS.setNumRefIdxL0DefaultActive(bestPos);
996  m_cPPS.setNumRefIdxL1DefaultActive(bestPos);
997  m_cPPS.setTransquantBypassEnableFlag(getTransquantBypassEnableFlag());
998  m_cPPS.setUseTransformSkip( m_useTransformSkip );
999  if (m_sliceSegmentMode)
1000  {
1001    m_cPPS.setDependentSliceSegmentsEnabledFlag( true );
1002  }
1003  if( m_cPPS.getDependentSliceSegmentsEnabledFlag() )
1004  {
1005    Int NumCtx = m_cPPS.getEntropyCodingSyncEnabledFlag()?2:1;
1006    m_cSliceEncoder.initCtxMem( NumCtx );
1007    for ( UInt st = 0; st < NumCtx; st++ )
1008    {
1009      TEncSbac* ctx = NULL;
1010      ctx = new TEncSbac;
1011      ctx->init( &m_cBinCoderCABAC );
1012      m_cSliceEncoder.setCtxMem( ctx, st );
1013    }
1014  }
1015}
1016
1017//Function for initializing m_RPSList, a list of TComReferencePictureSet, based on the GOPEntry objects read from the config file.
1018Void TEncTop::xInitRPS(Bool isFieldCoding)
1019{
1020  TComReferencePictureSet*      rps;
1021 
1022  m_cSPS.createRPSList(getGOPSize()+m_extraRPSs+1);
1023  TComRPSList* rpsList = m_cSPS.getRPSList();
1024
1025  for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++) 
1026  {
1027    GOPEntry ge = getGOPEntry(i);
1028    rps = rpsList->getReferencePictureSet(i);
1029    rps->setNumberOfPictures(ge.m_numRefPics);
1030    rps->setNumRefIdc(ge.m_numRefIdc);
1031    Int numNeg = 0;
1032    Int numPos = 0;
1033    for( Int j = 0; j < ge.m_numRefPics; j++)
1034    {
1035      rps->setDeltaPOC(j,ge.m_referencePics[j]);
1036      rps->setUsed(j,ge.m_usedByCurrPic[j]);
1037      if(ge.m_referencePics[j]>0)
1038      {
1039        numPos++;
1040      }
1041      else
1042      {
1043        numNeg++;
1044      }
1045    }
1046    rps->setNumberOfNegativePictures(numNeg);
1047    rps->setNumberOfPositivePictures(numPos);
1048
1049    // handle inter RPS intialization from the config file.
1050#if AUTO_INTER_RPS
1051    rps->setInterRPSPrediction(ge.m_interRPSPrediction > 0);  // not very clean, converting anything > 0 to true.
1052    rps->setDeltaRIdxMinus1(0);                               // index to the Reference RPS is always the previous one.
1053    TComReferencePictureSet*     RPSRef = rpsList->getReferencePictureSet(i-1);  // get the reference RPS
1054
1055    if (ge.m_interRPSPrediction == 2)  // Automatic generation of the inter RPS idc based on the RIdx provided.
1056    {
1057      Int deltaRPS = getGOPEntry(i-1).m_POC - ge.m_POC;  // the ref POC - current POC
1058      Int numRefDeltaPOC = RPSRef->getNumberOfPictures();
1059
1060      rps->setDeltaRPS(deltaRPS);           // set delta RPS
1061      rps->setNumRefIdc(numRefDeltaPOC+1);  // set the numRefIdc to the number of pictures in the reference RPS + 1.
1062      Int count=0;
1063      for (Int j = 0; j <= numRefDeltaPOC; j++ ) // cycle through pics in reference RPS.
1064      {
1065        Int RefDeltaPOC = (j<numRefDeltaPOC)? RPSRef->getDeltaPOC(j): 0;  // if it is the last decoded picture, set RefDeltaPOC = 0
1066        rps->setRefIdc(j, 0);
1067        for (Int k = 0; k < rps->getNumberOfPictures(); k++ )  // cycle through pics in current RPS.
1068        {
1069          if (rps->getDeltaPOC(k) == ( RefDeltaPOC + deltaRPS))  // if the current RPS has a same picture as the reference RPS.
1070          {
1071              rps->setRefIdc(j, (rps->getUsed(k)?1:2));
1072              count++;
1073              break;
1074          }
1075        }
1076      }
1077      if (count != rps->getNumberOfPictures())
1078      {
1079        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");
1080        rps->setInterRPSPrediction(0);
1081      }
1082    }
1083    else if (ge.m_interRPSPrediction == 1)  // inter RPS idc based on the RefIdc values provided in config file.
1084    {
1085      rps->setDeltaRPS(ge.m_deltaRPS);
1086      rps->setNumRefIdc(ge.m_numRefIdc);
1087      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1088      {
1089        rps->setRefIdc(j, ge.m_refIdc[j]);
1090      }
1091#if WRITE_BACK
1092      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1093      // computed from the RefIdc.  A warning is printed if they are not identical.
1094      numNeg = 0;
1095      numPos = 0;
1096      TComReferencePictureSet      RPSTemp;  // temporary variable
1097
1098      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1099      {
1100        if (ge.m_refIdc[j])
1101        {
1102          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1103          RPSTemp.setDeltaPOC((numNeg+numPos),deltaPOC);
1104          RPSTemp.setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1105          if (deltaPOC<0)
1106          {
1107            numNeg++;
1108          }
1109          else
1110          {
1111            numPos++;
1112          }
1113        }
1114      }
1115      if (numNeg != rps->getNumberOfNegativePictures())
1116      {
1117        printf("Warning: number of negative pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1118        rps->setNumberOfNegativePictures(numNeg);
1119        rps->setNumberOfPictures(numNeg+numPos);
1120      }
1121      if (numPos != rps->getNumberOfPositivePictures())
1122      {
1123        printf("Warning: number of positive pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1124        rps->setNumberOfPositivePictures(numPos);
1125        rps->setNumberOfPictures(numNeg+numPos);
1126      }
1127      RPSTemp.setNumberOfPictures(numNeg+numPos);
1128      RPSTemp.setNumberOfNegativePictures(numNeg);
1129      RPSTemp.sortDeltaPOC();     // sort the created delta POC before comparing
1130      // check if Delta POC and Used are the same
1131      // print warning if they are not.
1132      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1133      {
1134        if (RPSTemp.getDeltaPOC(j) != rps->getDeltaPOC(j))
1135        {
1136          printf("Warning: delta POC is different between intra RPS and inter RPS specified in the config file.\n");
1137          rps->setDeltaPOC(j,RPSTemp.getDeltaPOC(j));
1138        }
1139        if (RPSTemp.getUsed(j) != rps->getUsed(j))
1140        {
1141          printf("Warning: Used by Current in RPS is different between intra and inter RPS specified in the config file.\n");
1142          rps->setUsed(j,RPSTemp.getUsed(j));
1143        }
1144      }
1145#endif
1146    }
1147#else
1148    rps->setInterRPSPrediction(ge.m_interRPSPrediction);
1149    if (ge.m_interRPSPrediction)
1150    {
1151      rps->setDeltaRIdxMinus1(0);
1152      rps->setDeltaRPS(ge.m_deltaRPS);
1153      rps->setNumRefIdc(ge.m_numRefIdc);
1154      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1155      {
1156        rps->setRefIdc(j, ge.m_refIdc[j]);
1157      }
1158#if WRITE_BACK
1159      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1160      // computed from the RefIdc.  This is not necessary if both are identical. Currently there is no check to see if they are identical.
1161      numNeg = 0;
1162      numPos = 0;
1163      TComReferencePictureSet*     RPSRef = m_RPSList.getReferencePictureSet(i-1);
1164
1165      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1166      {
1167        if (ge.m_refIdc[j])
1168        {
1169          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1170          rps->setDeltaPOC((numNeg+numPos),deltaPOC);
1171          rps->setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1172          if (deltaPOC<0)
1173          {
1174            numNeg++;
1175          }
1176          else
1177          {
1178            numPos++;
1179          }
1180        }
1181      }
1182      rps->setNumberOfNegativePictures(numNeg);
1183      rps->setNumberOfPositivePictures(numPos);
1184      rps->sortDeltaPOC();
1185#endif
1186    }
1187#endif //INTER_RPS_AUTO
1188  }
1189  //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.
1190  //The position = GOPSize + extraRPSs which is (a priori) unused is reserved for this field in the RPS.
1191  if (isFieldCoding) 
1192  {
1193    rps = rpsList->getReferencePictureSet(getGOPSize()+m_extraRPSs);
1194    rps->setNumberOfPictures(1);
1195    rps->setNumberOfNegativePictures(1);
1196    rps->setNumberOfPositivePictures(0);
1197    rps->setNumberOfLongtermPictures(0);
1198    rps->setDeltaPOC(0,-1);
1199    rps->setPOC(0,0);
1200    rps->setUsed(0,true);
1201    rps->setInterRPSPrediction(false);
1202    rps->setDeltaRIdxMinus1(0);
1203    rps->setDeltaRPS(0);
1204    rps->setNumRefIdc(0);
1205}
1206}
1207
1208   // This is a function that
1209   // determines what Reference Picture Set to use
1210   // for a specific slice (with POC = POCCurr)
1211Void TEncTop::selectReferencePictureSet(TComSlice* slice, Int POCCurr, Int GOPid )
1212{
1213#if H_MV
1214  if( slice->getRapPicFlag() == true && getLayerId() > 0 && POCCurr == 0 )
1215  {
1216    TComReferencePictureSet* rps = slice->getLocalRPS();
1217    rps->setNumberOfNegativePictures(0);
1218    rps->setNumberOfPositivePictures(0);
1219    rps->setNumberOfLongtermPictures(0);
1220    rps->setNumberOfPictures(0);
1221    slice->setRPS(rps);
1222  }
1223  else
1224  {
1225#endif
1226  slice->setRPSidx(GOPid);
1227
1228  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1229  {   
1230    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1231    {
1232      Int POCIndex = POCCurr%m_uiIntraPeriod;
1233      if(POCIndex == 0)
1234      {
1235        POCIndex = m_uiIntraPeriod;
1236      }
1237      if(POCIndex == m_GOPList[extraNum].m_POC)
1238      {
1239        slice->setRPSidx(extraNum);
1240      }
1241    }
1242    else
1243    {
1244      if(POCCurr==m_GOPList[extraNum].m_POC)
1245      {
1246        slice->setRPSidx(extraNum);
1247      }
1248    }
1249  }
1250  if(POCCurr == 1 && slice->getPic()->isField())
1251  {
1252    slice->setRPSidx(m_iGOPSize+m_extraRPSs);
1253  }
1254
1255  slice->setRPS(getSPS()->getRPSList()->getReferencePictureSet(slice->getRPSidx()));
1256  slice->getRPS()->setNumberOfPictures(slice->getRPS()->getNumberOfNegativePictures()+slice->getRPS()->getNumberOfPositivePictures());
1257#if H_MV
1258  }
1259#endif
1260
1261}
1262
1263Int TEncTop::getReferencePictureSetIdxForSOP(TComSlice* slice, Int POCCurr, Int GOPid )
1264{
1265  int rpsIdx = GOPid;
1266
1267  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1268  {   
1269    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1270    {
1271      Int POCIndex = POCCurr%m_uiIntraPeriod;
1272      if(POCIndex == 0)
1273      {
1274        POCIndex = m_uiIntraPeriod;
1275      }
1276      if(POCIndex == m_GOPList[extraNum].m_POC)
1277      {
1278        rpsIdx = extraNum;
1279      }
1280    }
1281    else
1282    {
1283      if(POCCurr==m_GOPList[extraNum].m_POC)
1284      {
1285        rpsIdx = extraNum;
1286      }
1287    }
1288  }
1289
1290  return rpsIdx;
1291}
1292
1293Void  TEncTop::xInitPPSforTiles()
1294{
1295  m_cPPS.setTileUniformSpacingFlag( m_tileUniformSpacingFlag );
1296  m_cPPS.setNumTileColumnsMinus1( m_iNumColumnsMinus1 );
1297  m_cPPS.setNumTileRowsMinus1( m_iNumRowsMinus1 );
1298  if( !m_tileUniformSpacingFlag )
1299  {
1300    m_cPPS.setTileColumnWidth( m_tileColumnWidth );
1301    m_cPPS.setTileRowHeight( m_tileRowHeight );
1302  }
1303  m_cPPS.setLoopFilterAcrossTilesEnabledFlag( m_loopFilterAcrossTilesEnabledFlag );
1304
1305  // # substreams is "per tile" when tiles are independent.
1306  if (m_iWaveFrontSynchro )
1307  {
1308    m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams * (m_iNumColumnsMinus1+1));
1309  }
1310}
1311
1312Void  TEncCfg::xCheckGSParameters()
1313{
1314  Int   iWidthInCU = ( m_iSourceWidth%g_uiMaxCUWidth ) ? m_iSourceWidth/g_uiMaxCUWidth + 1 : m_iSourceWidth/g_uiMaxCUWidth;
1315  Int   iHeightInCU = ( m_iSourceHeight%g_uiMaxCUHeight ) ? m_iSourceHeight/g_uiMaxCUHeight + 1 : m_iSourceHeight/g_uiMaxCUHeight;
1316  UInt  uiCummulativeColumnWidth = 0;
1317  UInt  uiCummulativeRowHeight = 0;
1318
1319  //check the column relative parameters
1320  if( m_iNumColumnsMinus1 >= (1<<(LOG2_MAX_NUM_COLUMNS_MINUS1+1)) )
1321  {
1322    printf( "The number of columns is larger than the maximum allowed number of columns.\n" );
1323    exit( EXIT_FAILURE );
1324  }
1325
1326  if( m_iNumColumnsMinus1 >= iWidthInCU )
1327  {
1328    printf( "The current picture can not have so many columns.\n" );
1329    exit( EXIT_FAILURE );
1330  }
1331
1332  if( m_iNumColumnsMinus1 && !m_tileUniformSpacingFlag )
1333  {
1334    for(Int i=0; i<m_iNumColumnsMinus1; i++)
1335    {
1336      uiCummulativeColumnWidth += m_tileColumnWidth[i];
1337    }
1338
1339    if( uiCummulativeColumnWidth >= iWidthInCU )
1340    {
1341      printf( "The width of the column is too large.\n" );
1342      exit( EXIT_FAILURE );
1343    }
1344  }
1345
1346  //check the row relative parameters
1347  if( m_iNumRowsMinus1 >= (1<<(LOG2_MAX_NUM_ROWS_MINUS1+1)) )
1348  {
1349    printf( "The number of rows is larger than the maximum allowed number of rows.\n" );
1350    exit( EXIT_FAILURE );
1351  }
1352
1353  if( m_iNumRowsMinus1 >= iHeightInCU )
1354  {
1355    printf( "The current picture can not have so many rows.\n" );
1356    exit( EXIT_FAILURE );
1357  }
1358
1359  if( m_iNumRowsMinus1 && !m_tileUniformSpacingFlag )
1360  {
1361    for(Int i=0; i<m_iNumRowsMinus1; i++)
1362      uiCummulativeRowHeight += m_tileRowHeight[i];
1363
1364    if( uiCummulativeRowHeight >= iHeightInCU )
1365    {
1366      printf( "The height of the row is too large.\n" );
1367      exit( EXIT_FAILURE );
1368    }
1369  }
1370}
1371#if H_MV
1372Void TEncTop::printSummary( Int numAllPicCoded, Bool isField )
1373{
1374  assert ( !isField ); // Multiview and field coding need to be further unified
1375  assert (numAllPicCoded == m_cAnalyzeAll.getNumPic());
1376
1377  //--CFG_KDY
1378  m_cAnalyzeAll.setFrmRate( getFrameRate() );
1379  m_cAnalyzeI.setFrmRate( getFrameRate() );
1380  m_cAnalyzeP.setFrmRate( getFrameRate() );
1381  m_cAnalyzeB.setFrmRate( getFrameRate() );
1382
1383  //-- all
1384  printf( "\n\nSUMMARY ------------------------------------------- LayerId %2d\n", m_layerId );
1385
1386  m_cAnalyzeAll.printOut('a');
1387
1388  printf( "\n\nI Slices--------------------------------------------------------\n" );
1389  m_cAnalyzeI.printOut('i');
1390
1391  printf( "\n\nP Slices--------------------------------------------------------\n" );
1392  m_cAnalyzeP.printOut('p');
1393
1394  printf( "\n\nB Slices--------------------------------------------------------\n" );
1395  m_cAnalyzeB.printOut('b');
1396
1397#if _SUMMARY_OUT_
1398  m_cAnalyzeAll.printSummaryOut();
1399#endif
1400#if _SUMMARY_PIC_
1401  m_cAnalyzeI.printSummary('I');
1402  m_cAnalyzeP.printSummary('P');
1403  m_cAnalyzeB.printSummary('B');
1404#endif
1405}
1406
1407Int TEncTop::getFrameId(Int iGOPid) 
1408{
1409  if(m_iPOCLast == 0)
1410  {
1411    return(0 );
1412  }
1413  else
1414  {
1415    return m_iPOCLast -m_iNumPicRcvd+ getGOPEntry(iGOPid).m_POC ;
1416  }
1417}
1418
1419TComPic* TEncTop::getPic( Int poc )
1420{
1421  TComList<TComPic*>* listPic = getListPic();
1422  TComPic* pcPic = NULL;
1423  for(TComList<TComPic*>::iterator it=listPic->begin(); it!=listPic->end(); it++)
1424  {
1425    if( (*it)->getPOC() == poc )
1426    {
1427      pcPic = *it ;
1428      break ;
1429    }
1430  }
1431  return pcPic;
1432}
1433#endif
1434
1435#if H_3D_VSO
1436Void TEncTop::setupRenModel( Int iPoc, Int iEncViewIdx, Int iEncContent, Int iHorOffset )
1437{
1438  TRenModel* rendererModel = m_cRdCost.getRenModel(); 
1439  rendererModel->setupPart( iHorOffset, std::min( (Int) g_uiMaxCUHeight, (Int) ( m_iSourceHeight - iHorOffset ) )) ; 
1440 
1441  Int iEncViewSIdx = m_cameraParameters->getBaseId2SortedId()[ iEncViewIdx ];
1442
1443  // setup base views
1444  Int iNumOfBV = m_renderModelParameters->getNumOfBaseViewsForView( iEncViewSIdx, iEncContent );
1445
1446  for (Int iCurView = 0; iCurView < iNumOfBV; iCurView++ )
1447  {
1448    Int iBaseViewSIdx;
1449    Int iVideoDistMode;
1450    Int iDepthDistMode;
1451
1452    m_renderModelParameters->getBaseViewData( iEncViewSIdx, iEncContent, iCurView, iBaseViewSIdx, iVideoDistMode, iDepthDistMode );
1453
1454    AOT( iVideoDistMode < 0 || iVideoDistMode > 2 );
1455
1456    Int iBaseViewIdx = m_cameraParameters->getBaseSortedId2Id()[ iBaseViewSIdx ];
1457
1458    TComPicYuv* pcPicYuvVideoRec  = m_ivPicLists->getPicYuv( iBaseViewIdx, false, iPoc, true  );
1459    TComPicYuv* pcPicYuvDepthRec  = m_ivPicLists->getPicYuv( iBaseViewIdx, true , iPoc, true  );
1460    TComPicYuv* pcPicYuvVideoOrg  = m_ivPicLists->getPicYuv( iBaseViewIdx, false, iPoc, false );
1461    TComPicYuv* pcPicYuvDepthOrg  = m_ivPicLists->getPicYuv( iBaseViewIdx, true , iPoc, false );   
1462
1463    TComPicYuv* pcPicYuvVideoRef  = ( iVideoDistMode == 2 ) ? pcPicYuvVideoOrg  : NULL;
1464    TComPicYuv* pcPicYuvDepthRef  = ( iDepthDistMode == 2 ) ? pcPicYuvDepthOrg  : NULL;
1465
1466    TComPicYuv* pcPicYuvVideoTest = ( iVideoDistMode == 0 ) ? pcPicYuvVideoOrg  : pcPicYuvVideoRec;
1467    TComPicYuv* pcPicYuvDepthTest = ( iDepthDistMode == 0 ) ? pcPicYuvDepthOrg  : pcPicYuvDepthRec;
1468
1469    AOT( (iVideoDistMode == 2) != (pcPicYuvVideoRef != NULL) );
1470    AOT( (iDepthDistMode == 2) != (pcPicYuvDepthRef != NULL) );
1471    AOT( pcPicYuvDepthTest == NULL );
1472    AOT( pcPicYuvVideoTest == NULL );
1473
1474    rendererModel->setBaseView( iBaseViewSIdx, pcPicYuvVideoTest, pcPicYuvDepthTest, pcPicYuvVideoRef, pcPicYuvDepthRef );
1475  }
1476
1477  rendererModel->setErrorMode( iEncViewSIdx, iEncContent, 0 );
1478  // setup virtual views
1479  Int iNumOfSV  = m_renderModelParameters->getNumOfModelsForView( iEncViewSIdx, iEncContent );
1480  for (Int iCurView = 0; iCurView < iNumOfSV; iCurView++ )
1481  {
1482    Int iOrgRefBaseViewSIdx;
1483    Int iLeftBaseViewSIdx;
1484    Int iRightBaseViewSIdx;
1485    Int iSynthViewRelNum;
1486    Int iModelNum;
1487    Int iBlendMode;
1488    m_renderModelParameters->getSingleModelData(iEncViewSIdx, iEncContent, iCurView, iModelNum, iBlendMode,iLeftBaseViewSIdx, iRightBaseViewSIdx, iOrgRefBaseViewSIdx, iSynthViewRelNum );
1489
1490    Int iLeftBaseViewIdx    = -1;
1491    Int iRightBaseViewIdx   = -1;
1492
1493    TComPicYuv* pcPicYuvOrgRef  = NULL;
1494    Int**      ppiShiftLUTLeft  = NULL;
1495    Int**      ppiShiftLUTRight = NULL;
1496    Int**      ppiBaseShiftLUTLeft  = NULL;
1497    Int**      ppiBaseShiftLUTRight = NULL;
1498
1499
1500    Int        iDistToLeft      = -1;
1501
1502    Int iSynthViewIdx = m_cameraParameters->synthRelNum2Idx( iSynthViewRelNum );
1503
1504    if ( iLeftBaseViewSIdx != -1 )
1505    {
1506      iLeftBaseViewIdx   = m_cameraParameters->getBaseSortedId2Id()   [ iLeftBaseViewSIdx ];
1507      ppiShiftLUTLeft    = m_cameraParameters->getSynthViewShiftLUTI()[ iLeftBaseViewIdx  ][ iSynthViewIdx  ];
1508    }
1509
1510    if ( iRightBaseViewSIdx != -1 )
1511    {
1512      iRightBaseViewIdx  = m_cameraParameters->getBaseSortedId2Id()   [iRightBaseViewSIdx ];
1513      ppiShiftLUTRight   = m_cameraParameters->getSynthViewShiftLUTI()[ iRightBaseViewIdx ][ iSynthViewIdx ];
1514    }
1515
1516    if ( iRightBaseViewSIdx != -1 && iLeftBaseViewSIdx != -1 )
1517    {
1518      iDistToLeft          = m_cameraParameters->getRelDistLeft(  iSynthViewIdx , iLeftBaseViewIdx, iRightBaseViewIdx);
1519      ppiBaseShiftLUTLeft  = m_cameraParameters->getBaseViewShiftLUTI() [ iLeftBaseViewIdx  ][ iRightBaseViewIdx ];
1520      ppiBaseShiftLUTRight = m_cameraParameters->getBaseViewShiftLUTI() [ iRightBaseViewIdx ][ iLeftBaseViewIdx  ];
1521
1522    }
1523
1524    if ( iOrgRefBaseViewSIdx != -1 )
1525    {
1526      pcPicYuvOrgRef = m_ivPicLists->getPicYuv(  m_cameraParameters->getBaseSortedId2Id()[ iOrgRefBaseViewSIdx ] , false, iPoc, false );
1527      AOF ( pcPicYuvOrgRef );
1528    }
1529
1530    rendererModel->setSingleModel( iModelNum, ppiShiftLUTLeft, ppiBaseShiftLUTLeft, ppiShiftLUTRight, ppiBaseShiftLUTRight, iDistToLeft, pcPicYuvOrgRef );
1531  }
1532}
1533#endif
1534//! \}
Note: See TracBrowser for help on using the repository browser.