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

Last change on this file since 1417 was 1157, checked in by sony, 10 years ago

Added macro SONY_MV_V_CONST_C0078
for disparity vector constraint control under MV-HEVC setting

ohji.nakagami@…

  • Property svn:eol-style set to native
File size: 49.6 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6* Copyright (c) 2010-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#if SONY_MV_V_CONST_C0078
763    if ( m_bUseDisparitySearchRangeRestriction )
764    {
765      m_cSPS.setInterViewMvVertConstraintFlag ( true ) ;
766    }
767#endif
768  }
769  m_cSPS.setSpsExtensionPresentFlag       ( true ); 
770  m_cSPS.setSpsMultilayerExtensionFlag    ( true ); 
771#if H_3D
772  m_cSPS.setSps3dExtensionFlag            ( true ); 
773#endif
774#endif
775  m_cSPS.setPicWidthInLumaSamples         ( m_iSourceWidth      );
776  m_cSPS.setPicHeightInLumaSamples        ( m_iSourceHeight     );
777  m_cSPS.setConformanceWindow             ( m_conformanceWindow );
778  m_cSPS.setMaxCUWidth    ( g_uiMaxCUWidth      );
779  m_cSPS.setMaxCUHeight   ( g_uiMaxCUHeight     );
780  m_cSPS.setMaxCUDepth    ( g_uiMaxCUDepth      );
781
782  Int minCUSize = m_cSPS.getMaxCUWidth() >> ( m_cSPS.getMaxCUDepth()-g_uiAddCUDepth );
783  Int log2MinCUSize = 0;
784  while(minCUSize > 1)
785  {
786    minCUSize >>= 1;
787    log2MinCUSize++;
788  }
789
790  m_cSPS.setLog2MinCodingBlockSize(log2MinCUSize);
791  m_cSPS.setLog2DiffMaxMinCodingBlockSize(m_cSPS.getMaxCUDepth()-g_uiAddCUDepth);
792 
793  m_cSPS.setPCMLog2MinSize (m_uiPCMLog2MinSize);
794  m_cSPS.setUsePCM        ( m_usePCM           );
795  m_cSPS.setPCMLog2MaxSize( m_pcmLog2MaxSize  );
796
797  m_cSPS.setQuadtreeTULog2MaxSize( m_uiQuadtreeTULog2MaxSize );
798  m_cSPS.setQuadtreeTULog2MinSize( m_uiQuadtreeTULog2MinSize );
799  m_cSPS.setQuadtreeTUMaxDepthInter( m_uiQuadtreeTUMaxDepthInter    );
800  m_cSPS.setQuadtreeTUMaxDepthIntra( m_uiQuadtreeTUMaxDepthIntra    );
801 
802  m_cSPS.setTMVPFlagsPresent(false);
803
804  m_cSPS.setMaxTrSize   ( 1 << m_uiQuadtreeTULog2MaxSize );
805 
806  Int i;
807 
808  for (i = 0; i < g_uiMaxCUDepth-g_uiAddCUDepth; i++ )
809  {
810    m_cSPS.setAMPAcc( i, m_useAMP );
811    //m_cSPS.setAMPAcc( i, 1 );
812  }
813
814  m_cSPS.setUseAMP ( m_useAMP );
815
816  for (i = g_uiMaxCUDepth-g_uiAddCUDepth; i < g_uiMaxCUDepth; i++ )
817  {
818    m_cSPS.setAMPAcc(i, 0);
819  }
820
821  m_cSPS.setBitDepthY( g_bitDepthY );
822  m_cSPS.setBitDepthC( g_bitDepthC );
823
824  m_cSPS.setQpBDOffsetY ( 6*(g_bitDepthY - 8) );
825  m_cSPS.setQpBDOffsetC ( 6*(g_bitDepthC - 8) );
826
827  m_cSPS.setUseSAO( m_bUseSAO );
828
829  m_cSPS.setMaxTLayers( m_maxTempLayer );
830  m_cSPS.setTemporalIdNestingFlag( ( m_maxTempLayer == 1 ) ? true : false );
831  for ( i = 0; i < min(m_cSPS.getMaxTLayers(),(UInt) MAX_TLAYER); i++ )
832  {
833    m_cSPS.setMaxDecPicBuffering(m_maxDecPicBuffering[i], i);
834    m_cSPS.setNumReorderPics(m_numReorderPics[i], i);
835  }
836#if H_MV
837  for ( Int ols = 0; ols < m_cVPS->getNumOutputLayerSets(); ols++)
838  {
839    // Check MaxDecPicBuffering
840    const std::vector<Int>& targetDecLayerIdList = m_cVPS->getTargetDecLayerIdList( m_cVPS->olsIdxToLsIdx( ols )); 
841    for( Int is = 0; is < targetDecLayerIdList.size(); is++  )
842    {
843      if ( m_cVPS->getNecessaryLayerFlag( ols, is ) )
844      {     
845        m_cSPS.inferSpsMaxDecPicBufferingMinus1( m_cVPS, ols, targetDecLayerIdList[is], true );       
846      }
847    }
848  }
849#endif
850  m_cSPS.setPCMBitDepthLuma (g_uiPCMBitDepthLuma);
851  m_cSPS.setPCMBitDepthChroma (g_uiPCMBitDepthChroma);
852  m_cSPS.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag );
853
854  m_cSPS.setScalingListFlag ( (m_useScalingListId == 0) ? 0 : 1 );
855
856  m_cSPS.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
857
858  m_cSPS.setVuiParametersPresentFlag(getVuiParametersPresentFlag());
859  if (m_cSPS.getVuiParametersPresentFlag())
860  {
861    TComVUI* pcVUI = m_cSPS.getVuiParameters();
862    pcVUI->setAspectRatioInfoPresentFlag(getAspectRatioInfoPresentFlag());
863    pcVUI->setAspectRatioIdc(getAspectRatioIdc());
864    pcVUI->setSarWidth(getSarWidth());
865    pcVUI->setSarHeight(getSarHeight());
866    pcVUI->setOverscanInfoPresentFlag(getOverscanInfoPresentFlag());
867    pcVUI->setOverscanAppropriateFlag(getOverscanAppropriateFlag());
868#if H_MV
869    pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag() && getLayerId() == 0 );
870#else
871   pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag());
872#endif
873    pcVUI->setVideoFormat(getVideoFormat());
874    pcVUI->setVideoFullRangeFlag(getVideoFullRangeFlag());
875    pcVUI->setColourDescriptionPresentFlag(getColourDescriptionPresentFlag());
876    pcVUI->setColourPrimaries(getColourPrimaries());
877    pcVUI->setTransferCharacteristics(getTransferCharacteristics());
878    pcVUI->setMatrixCoefficients(getMatrixCoefficients());
879    pcVUI->setChromaLocInfoPresentFlag(getChromaLocInfoPresentFlag());
880    pcVUI->setChromaSampleLocTypeTopField(getChromaSampleLocTypeTopField());
881    pcVUI->setChromaSampleLocTypeBottomField(getChromaSampleLocTypeBottomField());
882    pcVUI->setNeutralChromaIndicationFlag(getNeutralChromaIndicationFlag());
883    pcVUI->setDefaultDisplayWindow(getDefaultDisplayWindow());
884    pcVUI->setFrameFieldInfoPresentFlag(getFrameFieldInfoPresentFlag());
885    pcVUI->setFieldSeqFlag(false);
886    pcVUI->setHrdParametersPresentFlag(false);
887    pcVUI->getTimingInfo()->setPocProportionalToTimingFlag(getPocProportionalToTimingFlag());
888    pcVUI->getTimingInfo()->setNumTicksPocDiffOneMinus1   (getNumTicksPocDiffOneMinus1()   );
889    pcVUI->setBitstreamRestrictionFlag(getBitstreamRestrictionFlag());
890    pcVUI->setTilesFixedStructureFlag(getTilesFixedStructureFlag());
891    pcVUI->setMotionVectorsOverPicBoundariesFlag(getMotionVectorsOverPicBoundariesFlag());
892    pcVUI->setMinSpatialSegmentationIdc(getMinSpatialSegmentationIdc());
893    pcVUI->setMaxBytesPerPicDenom(getMaxBytesPerPicDenom());
894    pcVUI->setMaxBitsPerMinCuDenom(getMaxBitsPerMinCuDenom());
895    pcVUI->setLog2MaxMvLengthHorizontal(getLog2MaxMvLengthHorizontal());
896    pcVUI->setLog2MaxMvLengthVertical(getLog2MaxMvLengthVertical());
897  }
898}
899
900Void TEncTop::xInitPPS()
901{
902#if H_MV
903  m_cPPS.setLayerId( getLayerId() );
904#if H_3D
905  // Check if this condition is still correct
906  if( getVPS()->getNumRefListLayers( getLayerId() ) > 0 )
907#else
908  if( getVPS()->getNumDirectRefLayers( getLayerId() ) > 0 )
909#endif
910  {
911    m_cPPS.setListsModificationPresentFlag( true );
912  }
913  m_cPPS.setPPSId( getLayerIdInVps() );
914  m_cPPS.setSPSId( getLayerIdInVps() );
915  m_cPPS.setPpsMultilayerExtensionFlag    ( true ); 
916#if H_3D
917  m_cPPS.setPps3dExtensionFlag            ( true ); 
918#endif
919#endif
920
921#if H_3D
922  m_cPPS.setDLT( getDLT() );
923#endif
924  m_cPPS.setConstrainedIntraPred( m_bUseConstrainedIntraPred );
925  Bool bUseDQP = (getMaxCuDQPDepth() > 0)? true : false;
926
927      if((getMaxDeltaQP() != 0 )|| getUseAdaptiveQP())
928      {
929        bUseDQP = true;
930      }
931
932  if(bUseDQP)
933  {
934    m_cPPS.setUseDQP(true);
935    m_cPPS.setMaxCuDQPDepth( m_iMaxCuDQPDepth );
936    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
937  }
938  else
939  {
940    m_cPPS.setUseDQP(false);
941    m_cPPS.setMaxCuDQPDepth( 0 );
942    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
943  }
944
945  if ( m_RCEnableRateControl )
946  {
947    m_cPPS.setUseDQP(true);
948    m_cPPS.setMaxCuDQPDepth( 0 );
949    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
950  }
951
952  m_cPPS.setChromaCbQpOffset( m_chromaCbQpOffset );
953  m_cPPS.setChromaCrQpOffset( m_chromaCrQpOffset );
954
955  m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams);
956  m_cPPS.setEntropyCodingSyncEnabledFlag( m_iWaveFrontSynchro > 0 );
957  m_cPPS.setTilesEnabledFlag( (m_iNumColumnsMinus1 > 0 || m_iNumRowsMinus1 > 0) );
958  m_cPPS.setUseWP( m_useWeightedPred );
959  m_cPPS.setWPBiPred( m_useWeightedBiPred );
960  m_cPPS.setOutputFlagPresentFlag( false );
961#if H_MV
962  m_cPPS.setNumExtraSliceHeaderBits( 3 ); 
963#endif
964  m_cPPS.setSignHideFlag(getSignHideFlag());
965  if ( getDeblockingFilterMetric() )
966  {
967    m_cPPS.setDeblockingFilterControlPresentFlag (true);
968    m_cPPS.setDeblockingFilterOverrideEnabledFlag(true);
969    m_cPPS.setPicDisableDeblockingFilterFlag(false);
970    m_cPPS.setDeblockingFilterBetaOffsetDiv2(0);
971    m_cPPS.setDeblockingFilterTcOffsetDiv2(0);
972  } 
973  else
974  {
975    m_cPPS.setDeblockingFilterControlPresentFlag (m_DeblockingFilterControlPresent );
976  }
977  m_cPPS.setLog2ParallelMergeLevelMinus2   (m_log2ParallelMergeLevelMinus2 );
978  m_cPPS.setCabacInitPresentFlag(CABAC_INIT_PRESENT_FLAG);
979  m_cPPS.setLoopFilterAcrossSlicesEnabledFlag( m_bLFCrossSliceBoundaryFlag );
980  Int histogram[MAX_NUM_REF + 1];
981  for( Int i = 0; i <= MAX_NUM_REF; i++ )
982  {
983    histogram[i]=0;
984  }
985  for( Int i = 0; i < getGOPSize(); i++ )
986  {
987    assert(getGOPEntry(i).m_numRefPicsActive >= 0 && getGOPEntry(i).m_numRefPicsActive <= MAX_NUM_REF);
988    histogram[getGOPEntry(i).m_numRefPicsActive]++;
989  }
990  Int maxHist=-1;
991  Int bestPos=0;
992  for( Int i = 0; i <= MAX_NUM_REF; i++ )
993  {
994    if(histogram[i]>maxHist)
995    {
996      maxHist=histogram[i];
997      bestPos=i;
998    }
999  }
1000  assert(bestPos <= 15);
1001  m_cPPS.setNumRefIdxL0DefaultActive(bestPos);
1002  m_cPPS.setNumRefIdxL1DefaultActive(bestPos);
1003  m_cPPS.setTransquantBypassEnableFlag(getTransquantBypassEnableFlag());
1004  m_cPPS.setUseTransformSkip( m_useTransformSkip );
1005  if (m_sliceSegmentMode)
1006  {
1007    m_cPPS.setDependentSliceSegmentsEnabledFlag( true );
1008  }
1009  if( m_cPPS.getDependentSliceSegmentsEnabledFlag() )
1010  {
1011    Int NumCtx = m_cPPS.getEntropyCodingSyncEnabledFlag()?2:1;
1012    m_cSliceEncoder.initCtxMem( NumCtx );
1013    for ( UInt st = 0; st < NumCtx; st++ )
1014    {
1015      TEncSbac* ctx = NULL;
1016      ctx = new TEncSbac;
1017      ctx->init( &m_cBinCoderCABAC );
1018      m_cSliceEncoder.setCtxMem( ctx, st );
1019    }
1020  }
1021}
1022
1023//Function for initializing m_RPSList, a list of TComReferencePictureSet, based on the GOPEntry objects read from the config file.
1024Void TEncTop::xInitRPS(Bool isFieldCoding)
1025{
1026  TComReferencePictureSet*      rps;
1027 
1028  m_cSPS.createRPSList(getGOPSize()+m_extraRPSs+1);
1029  TComRPSList* rpsList = m_cSPS.getRPSList();
1030
1031  for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++) 
1032  {
1033    GOPEntry ge = getGOPEntry(i);
1034    rps = rpsList->getReferencePictureSet(i);
1035    rps->setNumberOfPictures(ge.m_numRefPics);
1036    rps->setNumRefIdc(ge.m_numRefIdc);
1037    Int numNeg = 0;
1038    Int numPos = 0;
1039    for( Int j = 0; j < ge.m_numRefPics; j++)
1040    {
1041      rps->setDeltaPOC(j,ge.m_referencePics[j]);
1042      rps->setUsed(j,ge.m_usedByCurrPic[j]);
1043      if(ge.m_referencePics[j]>0)
1044      {
1045        numPos++;
1046      }
1047      else
1048      {
1049        numNeg++;
1050      }
1051    }
1052    rps->setNumberOfNegativePictures(numNeg);
1053    rps->setNumberOfPositivePictures(numPos);
1054
1055    // handle inter RPS intialization from the config file.
1056#if AUTO_INTER_RPS
1057    rps->setInterRPSPrediction(ge.m_interRPSPrediction > 0);  // not very clean, converting anything > 0 to true.
1058    rps->setDeltaRIdxMinus1(0);                               // index to the Reference RPS is always the previous one.
1059    TComReferencePictureSet*     RPSRef = rpsList->getReferencePictureSet(i-1);  // get the reference RPS
1060
1061    if (ge.m_interRPSPrediction == 2)  // Automatic generation of the inter RPS idc based on the RIdx provided.
1062    {
1063      Int deltaRPS = getGOPEntry(i-1).m_POC - ge.m_POC;  // the ref POC - current POC
1064      Int numRefDeltaPOC = RPSRef->getNumberOfPictures();
1065
1066      rps->setDeltaRPS(deltaRPS);           // set delta RPS
1067      rps->setNumRefIdc(numRefDeltaPOC+1);  // set the numRefIdc to the number of pictures in the reference RPS + 1.
1068      Int count=0;
1069      for (Int j = 0; j <= numRefDeltaPOC; j++ ) // cycle through pics in reference RPS.
1070      {
1071        Int RefDeltaPOC = (j<numRefDeltaPOC)? RPSRef->getDeltaPOC(j): 0;  // if it is the last decoded picture, set RefDeltaPOC = 0
1072        rps->setRefIdc(j, 0);
1073        for (Int k = 0; k < rps->getNumberOfPictures(); k++ )  // cycle through pics in current RPS.
1074        {
1075          if (rps->getDeltaPOC(k) == ( RefDeltaPOC + deltaRPS))  // if the current RPS has a same picture as the reference RPS.
1076          {
1077              rps->setRefIdc(j, (rps->getUsed(k)?1:2));
1078              count++;
1079              break;
1080          }
1081        }
1082      }
1083      if (count != rps->getNumberOfPictures())
1084      {
1085        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");
1086        rps->setInterRPSPrediction(0);
1087      }
1088    }
1089    else if (ge.m_interRPSPrediction == 1)  // inter RPS idc based on the RefIdc values provided in config file.
1090    {
1091      rps->setDeltaRPS(ge.m_deltaRPS);
1092      rps->setNumRefIdc(ge.m_numRefIdc);
1093      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1094      {
1095        rps->setRefIdc(j, ge.m_refIdc[j]);
1096      }
1097#if WRITE_BACK
1098      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1099      // computed from the RefIdc.  A warning is printed if they are not identical.
1100      numNeg = 0;
1101      numPos = 0;
1102      TComReferencePictureSet      RPSTemp;  // temporary variable
1103
1104      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1105      {
1106        if (ge.m_refIdc[j])
1107        {
1108          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1109          RPSTemp.setDeltaPOC((numNeg+numPos),deltaPOC);
1110          RPSTemp.setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1111          if (deltaPOC<0)
1112          {
1113            numNeg++;
1114          }
1115          else
1116          {
1117            numPos++;
1118          }
1119        }
1120      }
1121      if (numNeg != rps->getNumberOfNegativePictures())
1122      {
1123        printf("Warning: number of negative pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1124        rps->setNumberOfNegativePictures(numNeg);
1125        rps->setNumberOfPictures(numNeg+numPos);
1126      }
1127      if (numPos != rps->getNumberOfPositivePictures())
1128      {
1129        printf("Warning: number of positive pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1130        rps->setNumberOfPositivePictures(numPos);
1131        rps->setNumberOfPictures(numNeg+numPos);
1132      }
1133      RPSTemp.setNumberOfPictures(numNeg+numPos);
1134      RPSTemp.setNumberOfNegativePictures(numNeg);
1135      RPSTemp.sortDeltaPOC();     // sort the created delta POC before comparing
1136      // check if Delta POC and Used are the same
1137      // print warning if they are not.
1138      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1139      {
1140        if (RPSTemp.getDeltaPOC(j) != rps->getDeltaPOC(j))
1141        {
1142          printf("Warning: delta POC is different between intra RPS and inter RPS specified in the config file.\n");
1143          rps->setDeltaPOC(j,RPSTemp.getDeltaPOC(j));
1144        }
1145        if (RPSTemp.getUsed(j) != rps->getUsed(j))
1146        {
1147          printf("Warning: Used by Current in RPS is different between intra and inter RPS specified in the config file.\n");
1148          rps->setUsed(j,RPSTemp.getUsed(j));
1149        }
1150      }
1151#endif
1152    }
1153#else
1154    rps->setInterRPSPrediction(ge.m_interRPSPrediction);
1155    if (ge.m_interRPSPrediction)
1156    {
1157      rps->setDeltaRIdxMinus1(0);
1158      rps->setDeltaRPS(ge.m_deltaRPS);
1159      rps->setNumRefIdc(ge.m_numRefIdc);
1160      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1161      {
1162        rps->setRefIdc(j, ge.m_refIdc[j]);
1163      }
1164#if WRITE_BACK
1165      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1166      // computed from the RefIdc.  This is not necessary if both are identical. Currently there is no check to see if they are identical.
1167      numNeg = 0;
1168      numPos = 0;
1169      TComReferencePictureSet*     RPSRef = m_RPSList.getReferencePictureSet(i-1);
1170
1171      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1172      {
1173        if (ge.m_refIdc[j])
1174        {
1175          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1176          rps->setDeltaPOC((numNeg+numPos),deltaPOC);
1177          rps->setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1178          if (deltaPOC<0)
1179          {
1180            numNeg++;
1181          }
1182          else
1183          {
1184            numPos++;
1185          }
1186        }
1187      }
1188      rps->setNumberOfNegativePictures(numNeg);
1189      rps->setNumberOfPositivePictures(numPos);
1190      rps->sortDeltaPOC();
1191#endif
1192    }
1193#endif //INTER_RPS_AUTO
1194  }
1195  //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.
1196  //The position = GOPSize + extraRPSs which is (a priori) unused is reserved for this field in the RPS.
1197  if (isFieldCoding) 
1198  {
1199    rps = rpsList->getReferencePictureSet(getGOPSize()+m_extraRPSs);
1200    rps->setNumberOfPictures(1);
1201    rps->setNumberOfNegativePictures(1);
1202    rps->setNumberOfPositivePictures(0);
1203    rps->setNumberOfLongtermPictures(0);
1204    rps->setDeltaPOC(0,-1);
1205    rps->setPOC(0,0);
1206    rps->setUsed(0,true);
1207    rps->setInterRPSPrediction(false);
1208    rps->setDeltaRIdxMinus1(0);
1209    rps->setDeltaRPS(0);
1210    rps->setNumRefIdc(0);
1211}
1212}
1213
1214   // This is a function that
1215   // determines what Reference Picture Set to use
1216   // for a specific slice (with POC = POCCurr)
1217Void TEncTop::selectReferencePictureSet(TComSlice* slice, Int POCCurr, Int GOPid )
1218{
1219#if H_MV
1220  if( slice->getRapPicFlag() == true && getLayerId() > 0 && POCCurr == 0 )
1221  {
1222    TComReferencePictureSet* rps = slice->getLocalRPS();
1223    rps->setNumberOfNegativePictures(0);
1224    rps->setNumberOfPositivePictures(0);
1225    rps->setNumberOfLongtermPictures(0);
1226    rps->setNumberOfPictures(0);
1227    slice->setRPS(rps);
1228  }
1229  else
1230  {
1231#endif
1232  slice->setRPSidx(GOPid);
1233
1234  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1235  {   
1236    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1237    {
1238      Int POCIndex = POCCurr%m_uiIntraPeriod;
1239      if(POCIndex == 0)
1240      {
1241        POCIndex = m_uiIntraPeriod;
1242      }
1243      if(POCIndex == m_GOPList[extraNum].m_POC)
1244      {
1245        slice->setRPSidx(extraNum);
1246      }
1247    }
1248    else
1249    {
1250      if(POCCurr==m_GOPList[extraNum].m_POC)
1251      {
1252        slice->setRPSidx(extraNum);
1253      }
1254    }
1255  }
1256  if(POCCurr == 1 && slice->getPic()->isField())
1257  {
1258    slice->setRPSidx(m_iGOPSize+m_extraRPSs);
1259  }
1260
1261  slice->setRPS(getSPS()->getRPSList()->getReferencePictureSet(slice->getRPSidx()));
1262  slice->getRPS()->setNumberOfPictures(slice->getRPS()->getNumberOfNegativePictures()+slice->getRPS()->getNumberOfPositivePictures());
1263#if H_MV
1264  }
1265#endif
1266
1267}
1268
1269Int TEncTop::getReferencePictureSetIdxForSOP(TComSlice* slice, Int POCCurr, Int GOPid )
1270{
1271  int rpsIdx = GOPid;
1272
1273  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1274  {   
1275    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1276    {
1277      Int POCIndex = POCCurr%m_uiIntraPeriod;
1278      if(POCIndex == 0)
1279      {
1280        POCIndex = m_uiIntraPeriod;
1281      }
1282      if(POCIndex == m_GOPList[extraNum].m_POC)
1283      {
1284        rpsIdx = extraNum;
1285      }
1286    }
1287    else
1288    {
1289      if(POCCurr==m_GOPList[extraNum].m_POC)
1290      {
1291        rpsIdx = extraNum;
1292      }
1293    }
1294  }
1295
1296  return rpsIdx;
1297}
1298
1299Void  TEncTop::xInitPPSforTiles()
1300{
1301  m_cPPS.setTileUniformSpacingFlag( m_tileUniformSpacingFlag );
1302  m_cPPS.setNumTileColumnsMinus1( m_iNumColumnsMinus1 );
1303  m_cPPS.setNumTileRowsMinus1( m_iNumRowsMinus1 );
1304  if( !m_tileUniformSpacingFlag )
1305  {
1306    m_cPPS.setTileColumnWidth( m_tileColumnWidth );
1307    m_cPPS.setTileRowHeight( m_tileRowHeight );
1308  }
1309  m_cPPS.setLoopFilterAcrossTilesEnabledFlag( m_loopFilterAcrossTilesEnabledFlag );
1310
1311  // # substreams is "per tile" when tiles are independent.
1312  if (m_iWaveFrontSynchro )
1313  {
1314    m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams * (m_iNumColumnsMinus1+1));
1315  }
1316}
1317
1318Void  TEncCfg::xCheckGSParameters()
1319{
1320  Int   iWidthInCU = ( m_iSourceWidth%g_uiMaxCUWidth ) ? m_iSourceWidth/g_uiMaxCUWidth + 1 : m_iSourceWidth/g_uiMaxCUWidth;
1321  Int   iHeightInCU = ( m_iSourceHeight%g_uiMaxCUHeight ) ? m_iSourceHeight/g_uiMaxCUHeight + 1 : m_iSourceHeight/g_uiMaxCUHeight;
1322  UInt  uiCummulativeColumnWidth = 0;
1323  UInt  uiCummulativeRowHeight = 0;
1324
1325  //check the column relative parameters
1326  if( m_iNumColumnsMinus1 >= (1<<(LOG2_MAX_NUM_COLUMNS_MINUS1+1)) )
1327  {
1328    printf( "The number of columns is larger than the maximum allowed number of columns.\n" );
1329    exit( EXIT_FAILURE );
1330  }
1331
1332  if( m_iNumColumnsMinus1 >= iWidthInCU )
1333  {
1334    printf( "The current picture can not have so many columns.\n" );
1335    exit( EXIT_FAILURE );
1336  }
1337
1338  if( m_iNumColumnsMinus1 && !m_tileUniformSpacingFlag )
1339  {
1340    for(Int i=0; i<m_iNumColumnsMinus1; i++)
1341    {
1342      uiCummulativeColumnWidth += m_tileColumnWidth[i];
1343    }
1344
1345    if( uiCummulativeColumnWidth >= iWidthInCU )
1346    {
1347      printf( "The width of the column is too large.\n" );
1348      exit( EXIT_FAILURE );
1349    }
1350  }
1351
1352  //check the row relative parameters
1353  if( m_iNumRowsMinus1 >= (1<<(LOG2_MAX_NUM_ROWS_MINUS1+1)) )
1354  {
1355    printf( "The number of rows is larger than the maximum allowed number of rows.\n" );
1356    exit( EXIT_FAILURE );
1357  }
1358
1359  if( m_iNumRowsMinus1 >= iHeightInCU )
1360  {
1361    printf( "The current picture can not have so many rows.\n" );
1362    exit( EXIT_FAILURE );
1363  }
1364
1365  if( m_iNumRowsMinus1 && !m_tileUniformSpacingFlag )
1366  {
1367    for(Int i=0; i<m_iNumRowsMinus1; i++)
1368      uiCummulativeRowHeight += m_tileRowHeight[i];
1369
1370    if( uiCummulativeRowHeight >= iHeightInCU )
1371    {
1372      printf( "The height of the row is too large.\n" );
1373      exit( EXIT_FAILURE );
1374    }
1375  }
1376}
1377#if H_MV
1378Void TEncTop::printSummary( Int numAllPicCoded, Bool isField )
1379{
1380  assert ( !isField ); // Multiview and field coding need to be further unified
1381  assert (numAllPicCoded == m_cAnalyzeAll.getNumPic());
1382
1383  //--CFG_KDY
1384  m_cAnalyzeAll.setFrmRate( getFrameRate() );
1385  m_cAnalyzeI.setFrmRate( getFrameRate() );
1386  m_cAnalyzeP.setFrmRate( getFrameRate() );
1387  m_cAnalyzeB.setFrmRate( getFrameRate() );
1388
1389  //-- all
1390  printf( "\n\nSUMMARY ------------------------------------------- LayerId %2d\n", m_layerId );
1391
1392  m_cAnalyzeAll.printOut('a');
1393
1394  printf( "\n\nI Slices--------------------------------------------------------\n" );
1395  m_cAnalyzeI.printOut('i');
1396
1397  printf( "\n\nP Slices--------------------------------------------------------\n" );
1398  m_cAnalyzeP.printOut('p');
1399
1400  printf( "\n\nB Slices--------------------------------------------------------\n" );
1401  m_cAnalyzeB.printOut('b');
1402
1403#if _SUMMARY_OUT_
1404  m_cAnalyzeAll.printSummaryOut();
1405#endif
1406#if _SUMMARY_PIC_
1407  m_cAnalyzeI.printSummary('I');
1408  m_cAnalyzeP.printSummary('P');
1409  m_cAnalyzeB.printSummary('B');
1410#endif
1411}
1412
1413Int TEncTop::getFrameId(Int iGOPid) 
1414{
1415  if(m_iPOCLast == 0)
1416  {
1417    return(0 );
1418  }
1419  else
1420  {
1421    return m_iPOCLast -m_iNumPicRcvd+ getGOPEntry(iGOPid).m_POC ;
1422  }
1423}
1424
1425TComPic* TEncTop::getPic( Int poc )
1426{
1427  TComList<TComPic*>* listPic = getListPic();
1428  TComPic* pcPic = NULL;
1429  for(TComList<TComPic*>::iterator it=listPic->begin(); it!=listPic->end(); it++)
1430  {
1431    if( (*it)->getPOC() == poc )
1432    {
1433      pcPic = *it ;
1434      break ;
1435    }
1436  }
1437  return pcPic;
1438}
1439#endif
1440
1441#if H_3D_VSO
1442Void TEncTop::setupRenModel( Int iPoc, Int iEncViewIdx, Int iEncContent, Int iHorOffset )
1443{
1444  TRenModel* rendererModel = m_cRdCost.getRenModel(); 
1445  rendererModel->setupPart( iHorOffset, std::min( (Int) g_uiMaxCUHeight, (Int) ( m_iSourceHeight - iHorOffset ) )) ; 
1446 
1447  Int iEncViewSIdx = m_cameraParameters->getBaseId2SortedId()[ iEncViewIdx ];
1448
1449  // setup base views
1450  Int iNumOfBV = m_renderModelParameters->getNumOfBaseViewsForView( iEncViewSIdx, iEncContent );
1451
1452  for (Int iCurView = 0; iCurView < iNumOfBV; iCurView++ )
1453  {
1454    Int iBaseViewSIdx;
1455    Int iVideoDistMode;
1456    Int iDepthDistMode;
1457
1458    m_renderModelParameters->getBaseViewData( iEncViewSIdx, iEncContent, iCurView, iBaseViewSIdx, iVideoDistMode, iDepthDistMode );
1459
1460    AOT( iVideoDistMode < 0 || iVideoDistMode > 2 );
1461
1462    Int iBaseViewIdx = m_cameraParameters->getBaseSortedId2Id()[ iBaseViewSIdx ];
1463
1464    TComPicYuv* pcPicYuvVideoRec  = m_ivPicLists->getPicYuv( iBaseViewIdx, false, iPoc, true  );
1465    TComPicYuv* pcPicYuvDepthRec  = m_ivPicLists->getPicYuv( iBaseViewIdx, true , iPoc, true  );
1466    TComPicYuv* pcPicYuvVideoOrg  = m_ivPicLists->getPicYuv( iBaseViewIdx, false, iPoc, false );
1467    TComPicYuv* pcPicYuvDepthOrg  = m_ivPicLists->getPicYuv( iBaseViewIdx, true , iPoc, false );   
1468
1469    TComPicYuv* pcPicYuvVideoRef  = ( iVideoDistMode == 2 ) ? pcPicYuvVideoOrg  : NULL;
1470    TComPicYuv* pcPicYuvDepthRef  = ( iDepthDistMode == 2 ) ? pcPicYuvDepthOrg  : NULL;
1471
1472    TComPicYuv* pcPicYuvVideoTest = ( iVideoDistMode == 0 ) ? pcPicYuvVideoOrg  : pcPicYuvVideoRec;
1473    TComPicYuv* pcPicYuvDepthTest = ( iDepthDistMode == 0 ) ? pcPicYuvDepthOrg  : pcPicYuvDepthRec;
1474
1475    AOT( (iVideoDistMode == 2) != (pcPicYuvVideoRef != NULL) );
1476    AOT( (iDepthDistMode == 2) != (pcPicYuvDepthRef != NULL) );
1477    AOT( pcPicYuvDepthTest == NULL );
1478    AOT( pcPicYuvVideoTest == NULL );
1479
1480    rendererModel->setBaseView( iBaseViewSIdx, pcPicYuvVideoTest, pcPicYuvDepthTest, pcPicYuvVideoRef, pcPicYuvDepthRef );
1481  }
1482
1483  rendererModel->setErrorMode( iEncViewSIdx, iEncContent, 0 );
1484  // setup virtual views
1485  Int iNumOfSV  = m_renderModelParameters->getNumOfModelsForView( iEncViewSIdx, iEncContent );
1486  for (Int iCurView = 0; iCurView < iNumOfSV; iCurView++ )
1487  {
1488    Int iOrgRefBaseViewSIdx;
1489    Int iLeftBaseViewSIdx;
1490    Int iRightBaseViewSIdx;
1491    Int iSynthViewRelNum;
1492    Int iModelNum;
1493    Int iBlendMode;
1494    m_renderModelParameters->getSingleModelData(iEncViewSIdx, iEncContent, iCurView, iModelNum, iBlendMode,iLeftBaseViewSIdx, iRightBaseViewSIdx, iOrgRefBaseViewSIdx, iSynthViewRelNum );
1495
1496    Int iLeftBaseViewIdx    = -1;
1497    Int iRightBaseViewIdx   = -1;
1498
1499    TComPicYuv* pcPicYuvOrgRef  = NULL;
1500    Int**      ppiShiftLUTLeft  = NULL;
1501    Int**      ppiShiftLUTRight = NULL;
1502    Int**      ppiBaseShiftLUTLeft  = NULL;
1503    Int**      ppiBaseShiftLUTRight = NULL;
1504
1505
1506    Int        iDistToLeft      = -1;
1507
1508    Int iSynthViewIdx = m_cameraParameters->synthRelNum2Idx( iSynthViewRelNum );
1509
1510    if ( iLeftBaseViewSIdx != -1 )
1511    {
1512      iLeftBaseViewIdx   = m_cameraParameters->getBaseSortedId2Id()   [ iLeftBaseViewSIdx ];
1513      ppiShiftLUTLeft    = m_cameraParameters->getSynthViewShiftLUTI()[ iLeftBaseViewIdx  ][ iSynthViewIdx  ];
1514    }
1515
1516    if ( iRightBaseViewSIdx != -1 )
1517    {
1518      iRightBaseViewIdx  = m_cameraParameters->getBaseSortedId2Id()   [iRightBaseViewSIdx ];
1519      ppiShiftLUTRight   = m_cameraParameters->getSynthViewShiftLUTI()[ iRightBaseViewIdx ][ iSynthViewIdx ];
1520    }
1521
1522    if ( iRightBaseViewSIdx != -1 && iLeftBaseViewSIdx != -1 )
1523    {
1524      iDistToLeft          = m_cameraParameters->getRelDistLeft(  iSynthViewIdx , iLeftBaseViewIdx, iRightBaseViewIdx);
1525      ppiBaseShiftLUTLeft  = m_cameraParameters->getBaseViewShiftLUTI() [ iLeftBaseViewIdx  ][ iRightBaseViewIdx ];
1526      ppiBaseShiftLUTRight = m_cameraParameters->getBaseViewShiftLUTI() [ iRightBaseViewIdx ][ iLeftBaseViewIdx  ];
1527
1528    }
1529
1530    if ( iOrgRefBaseViewSIdx != -1 )
1531    {
1532      pcPicYuvOrgRef = m_ivPicLists->getPicYuv(  m_cameraParameters->getBaseSortedId2Id()[ iOrgRefBaseViewSIdx ] , false, iPoc, false );
1533      AOF ( pcPicYuvOrgRef );
1534    }
1535
1536    rendererModel->setSingleModel( iModelNum, ppiShiftLUTLeft, ppiBaseShiftLUTLeft, ppiShiftLUTRight, ppiBaseShiftLUTRight, iDistToLeft, pcPicYuvOrgRef );
1537  }
1538}
1539#endif
1540//! \}
Note: See TracBrowser for help on using the repository browser.