source: 3DVCSoftware/branches/HTM-9.2-dev0/source/Lib/TLibEncoder/TEncTop.cpp @ 767

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

Cleanup part 4

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