source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncTop.cpp @ 1203

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

Merged 14.0-dev0@1187.

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