source: SHVCSoftware/trunk/source/App/TAppEncoder/TAppEncTop.cpp @ 131

Last change on this file since 131 was 125, checked in by seregin, 12 years ago

copy from HM-10.0-dev-SHM

File size: 49.6 KB
RevLine 
[125]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     TAppEncTop.cpp
35\brief    Encoder application class
36*/
37
38#include <list>
39#include <fstream>
40#include <stdlib.h>
41#include <stdio.h>
42#include <fcntl.h>
43#include <assert.h>
44
45#include "TAppEncTop.h"
46#include "TLibEncoder/AnnexBwrite.h"
47
48using namespace std;
49
50//! \ingroup TAppEncoder
51//! \{
52
53// ====================================================================================================================
54// Constructor / destructor / initialization / destroy
55// ====================================================================================================================
56
57TAppEncTop::TAppEncTop()
58{
59  m_iFrameRcvd = 0;
60  m_totalBytes = 0;
61  m_essentialBytes = 0;
[2]62#if SVC_EXTENSION
63  for(UInt layer=0; layer < MAX_LAYERS; layer++)
64  {
65    m_apcTEncTop[layer] = &m_acTEncTop[layer];
66  }
[125]67#endif
68}
69
70TAppEncTop::~TAppEncTop()
71{
72}
73
74#if SVC_EXTENSION
75Void TAppEncTop::xInitLibCfg()
76{
77  TComVPS* vps = m_acTEncTop[0].getVPS();
78
79  vps->setMaxTLayers                       ( m_maxTempLayer );
80  if (m_maxTempLayer == 1)
81  {
82    vps->setTemporalNestingFlag(true);
83  }
84#if !VPS_RENAME
85  vps.setMaxLayers                        ( 1 );
86#endif
87  for(Int i = 0; i < MAX_TLAYER; i++)
88  {
89    vps->setNumReorderPics                 ( m_numReorderPics[i], i );
90    vps->setMaxDecPicBuffering             ( m_maxDecPicBuffering[i], i );
91  }
92
93  for(UInt layer=0; layer<m_numLayers; layer++)
94  {
95    //m_acTEncTop[layer].setVPS(&vps);
96    m_acTEncTop[layer].setFrameRate                    ( m_acLayerCfg[layer].getFrameRate() );
97    m_acTEncTop[layer].setFrameSkip                    ( m_FrameSkip );
98    m_acTEncTop[layer].setSourceWidth                  ( m_acLayerCfg[layer].getSourceWidth() );
99    m_acTEncTop[layer].setSourceHeight                 ( m_acLayerCfg[layer].getSourceHeight() );
100    m_acTEncTop[layer].setConformanceMode              ( m_acLayerCfg[layer].getConformanceMode() );
101    m_acTEncTop[layer].setConformanceWindow            ( m_acLayerCfg[layer].m_confLeft, m_acLayerCfg[layer].m_confRight, m_acLayerCfg[layer].m_confTop, m_acLayerCfg[layer].m_confBottom );
102    m_acTEncTop[layer].setFramesToBeEncoded            ( m_framesToBeEncoded );
103
104    m_acTEncTop[layer].setProfile(m_profile);
105    m_acTEncTop[layer].setLevel(m_levelTier, m_level);
106#if L0046_CONSTRAINT_FLAGS
107    m_acTEncTop[layer].setProgressiveSourceFlag(m_progressiveSourceFlag);
108    m_acTEncTop[layer].setInterlacedSourceFlag(m_interlacedSourceFlag);
109    m_acTEncTop[layer].setNonPackedConstraintFlag(m_nonPackedConstraintFlag);
110    m_acTEncTop[layer].setFrameOnlyConstraintFlag(m_frameOnlyConstraintFlag);
111#endif
112
113#if REF_IDX_MFM
114    m_acTEncTop[layer].setMFMEnabledFlag(layer == 0 ? false : true);
115#endif
116    // set layer ID
117    m_acTEncTop[layer].setLayerId ( layer ); 
118    m_acTEncTop[layer].setNumLayer ( m_numLayers );
119    m_acTEncTop[layer].setLayerEnc(m_apcTEncTop);
120
121    //====== Coding Structure ========
122    m_acTEncTop[layer].setIntraPeriod                  ( m_acLayerCfg[layer].m_iIntraPeriod );
123    m_acTEncTop[layer].setDecodingRefreshType          ( m_iDecodingRefreshType );
124    m_acTEncTop[layer].setGOPSize                      ( m_iGOPSize );
125    m_acTEncTop[layer].setGopList                      ( m_GOPList );
126    m_acTEncTop[layer].setExtraRPSs                    ( m_extraRPSs );
127    for(Int i = 0; i < MAX_TLAYER; i++)
128    {
129      m_acTEncTop[layer].setNumReorderPics             ( m_numReorderPics[i], i );
130      m_acTEncTop[layer].setMaxDecPicBuffering         ( m_maxDecPicBuffering[i], i );
131    }
132    for( UInt uiLoop = 0; uiLoop < MAX_TLAYER; ++uiLoop )
133    {
134      m_acTEncTop[layer].setLambdaModifier( uiLoop, m_adLambdaModifier[ uiLoop ] );
135    }
136    m_acTEncTop[layer].setQP                           ( m_acLayerCfg[layer].getIntQP() );
137
138    m_acTEncTop[layer].setPad                          ( m_acLayerCfg[layer].getPad() );
139
140    m_acTEncTop[layer].setMaxTempLayer                 ( m_maxTempLayer );
141    m_acTEncTop[layer].setUseAMP( m_enableAMP );
142#if VPS_EXTN_DIRECT_REF_LAYERS
143    if(layer)
144    {
145      if(m_acLayerCfg[layer].getNumDirectRefLayers() == -1)
146      {
147        // Not included in the configuration file; assume that each layer depends on previous layer
148        m_acTEncTop[layer].setNumDirectRefLayers       (1);      // One ref. layer
149        m_acTEncTop[layer].setRefLayerId               (0, layer - 1);   // Previous layer
150      }
151      else
152      {
153        m_acTEncTop[layer].setNumDirectRefLayers       ( m_acLayerCfg[layer].getNumDirectRefLayers() );     
154        for(Int i = 0; i < m_acTEncTop[layer].getNumDirectRefLayers(); i++)
155        {
156          m_acTEncTop[layer].setRefLayerId             ( i, m_acLayerCfg[layer].getRefLayerId(i));
157        }       
158      }
159    }
160#endif
161    //===== Slice ========
162
163    //====== Loop/Deblock Filter ========
164    m_acTEncTop[layer].setLoopFilterDisable            ( m_bLoopFilterDisable       );
165    m_acTEncTop[layer].setLoopFilterOffsetInPPS        ( m_loopFilterOffsetInPPS );
166    m_acTEncTop[layer].setLoopFilterBetaOffset         ( m_loopFilterBetaOffsetDiv2  );
167    m_acTEncTop[layer].setLoopFilterTcOffset           ( m_loopFilterTcOffsetDiv2    );
168    m_acTEncTop[layer].setDeblockingFilterControlPresent( m_DeblockingFilterControlPresent);
169
170    //====== Motion search ========
171    m_acTEncTop[layer].setFastSearch                   ( m_iFastSearch  );
172    m_acTEncTop[layer].setSearchRange                  ( m_iSearchRange );
173    m_acTEncTop[layer].setBipredSearchRange            ( m_bipredSearchRange );
174
175    //====== Quality control ========
176    m_acTEncTop[layer].setMaxDeltaQP                   ( m_iMaxDeltaQP  );
177    m_acTEncTop[layer].setMaxCuDQPDepth                ( m_iMaxCuDQPDepth  );
178
179    m_acTEncTop[layer].setChromaCbQpOffset             ( m_cbQpOffset     );
180    m_acTEncTop[layer].setChromaCrQpOffset             ( m_crQpOffset  );
181
182#if ADAPTIVE_QP_SELECTION
183    m_acTEncTop[layer].setUseAdaptQpSelect             ( m_bUseAdaptQpSelect   );
184#endif
185
186    Int lowestQP;
187    lowestQP =  - 6*(g_bitDepthY - 8); // XXX: check
188
189    if ((m_iMaxDeltaQP == 0 ) && (m_acLayerCfg[layer].getIntQP() == lowestQP) && (m_useLossless == true))
190    {
191      m_bUseAdaptiveQP = false;
192    }
193    m_acTEncTop[layer].setUseAdaptiveQP                ( m_bUseAdaptiveQP  );
194    m_acTEncTop[layer].setQPAdaptationRange            ( m_iQPAdaptationRange );
195
196    //====== Tool list ========
197    m_acTEncTop[layer].setUseSBACRD                    ( m_bUseSBACRD   );
198    m_acTEncTop[layer].setDeltaQpRD                    ( m_uiDeltaQpRD  );
199    m_acTEncTop[layer].setUseASR                       ( m_bUseASR      );
200    m_acTEncTop[layer].setUseHADME                     ( m_bUseHADME    );
201    m_acTEncTop[layer].setUseLossless                  ( m_useLossless );
202    m_acTEncTop[layer].setUseLComb                     ( m_bUseLComb    );
203    m_acTEncTop[layer].setdQPs                         ( m_acLayerCfg[layer].getdQPs() );
204    m_acTEncTop[layer].setUseRDOQ                      ( m_useRDOQ     );
205    m_acTEncTop[layer].setUseRDOQTS                    ( m_useRDOQTS   );
206#if L0232_RD_PENALTY
207    m_acTEncTop[layer].setRDpenalty                    ( m_rdPenalty );
208#endif
209    m_acTEncTop[layer].setQuadtreeTULog2MaxSize        ( m_uiQuadtreeTULog2MaxSize );
210    m_acTEncTop[layer].setQuadtreeTULog2MinSize        ( m_uiQuadtreeTULog2MinSize );
211    m_acTEncTop[layer].setQuadtreeTUMaxDepthInter      ( m_uiQuadtreeTUMaxDepthInter );
212    m_acTEncTop[layer].setQuadtreeTUMaxDepthIntra      ( m_uiQuadtreeTUMaxDepthIntra );
213    m_acTEncTop[layer].setUseFastEnc                   ( m_bUseFastEnc  );
214    m_acTEncTop[layer].setUseEarlyCU                   ( m_bUseEarlyCU  ); 
215    m_acTEncTop[layer].setUseFastDecisionForMerge      ( m_useFastDecisionForMerge  );
216    m_acTEncTop[layer].setUseCbfFastMode               ( m_bUseCbfFastMode  );
217    m_acTEncTop[layer].setUseEarlySkipDetection        ( m_useEarlySkipDetection );
218
219    m_acTEncTop[layer].setUseTransformSkip             ( m_useTransformSkip      );
220    m_acTEncTop[layer].setUseTransformSkipFast         ( m_useTransformSkipFast  );
221    m_acTEncTop[layer].setUseConstrainedIntraPred      ( m_bUseConstrainedIntraPred );
222    m_acTEncTop[layer].setPCMLog2MinSize               ( m_uiPCMLog2MinSize);
223    m_acTEncTop[layer].setUsePCM                       ( m_usePCM );
224    m_acTEncTop[layer].setPCMLog2MaxSize               ( m_pcmLog2MaxSize);
225    m_acTEncTop[layer].setMaxNumMergeCand              ( m_maxNumMergeCand );
226
227
228    //====== Weighted Prediction ========
229    m_acTEncTop[layer].setUseWP                   ( m_useWeightedPred      );
230    m_acTEncTop[layer].setWPBiPred                ( m_useWeightedBiPred   );
231    //====== Parallel Merge Estimation ========
232    m_acTEncTop[layer].setLog2ParallelMergeLevelMinus2 ( m_log2ParallelMergeLevel - 2 );
233
234    //====== Slice ========
235    m_acTEncTop[layer].setSliceMode               ( m_sliceMode                );
236    m_acTEncTop[layer].setSliceArgument           ( m_sliceArgument            );
237
238    //====== Dependent Slice ========
239    m_acTEncTop[layer].setSliceSegmentMode        ( m_sliceSegmentMode         );
240    m_acTEncTop[layer].setSliceSegmentArgument    ( m_sliceSegmentArgument     );
241    Int iNumPartInCU = 1<<(m_uiMaxCUDepth<<1);
242    if(m_sliceSegmentMode==FIXED_NUMBER_OF_LCU)
243    {
244      m_acTEncTop[layer].setSliceSegmentArgument ( m_sliceSegmentArgument * iNumPartInCU );
245    }
246    if(m_sliceMode==FIXED_NUMBER_OF_LCU)
247    {
248      m_acTEncTop[layer].setSliceArgument ( m_sliceArgument * iNumPartInCU );
249    }
250    if(m_sliceMode==FIXED_NUMBER_OF_TILES)
251    {
252      m_acTEncTop[layer].setSliceArgument ( m_sliceArgument );
253    }
254
255    if(m_sliceMode == 0 )
256    {
257      m_bLFCrossSliceBoundaryFlag = true;
258    }
259    m_acTEncTop[layer].setLFCrossSliceBoundaryFlag( m_bLFCrossSliceBoundaryFlag );
260    m_acTEncTop[layer].setUseSAO ( m_bUseSAO );
261    m_acTEncTop[layer].setMaxNumOffsetsPerPic (m_maxNumOffsetsPerPic);
262
263    m_acTEncTop[layer].setSaoLcuBoundary (m_saoLcuBoundary);
264    m_acTEncTop[layer].setSaoLcuBasedOptimization (m_saoLcuBasedOptimization);
265    m_acTEncTop[layer].setPCMInputBitDepthFlag  ( m_bPCMInputBitDepthFlag); 
266    m_acTEncTop[layer].setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag); 
267
268    m_acTEncTop[layer].setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
269    m_acTEncTop[layer].setRecoveryPointSEIEnabled( m_recoveryPointSEIEnabled );
270    m_acTEncTop[layer].setBufferingPeriodSEIEnabled( m_bufferingPeriodSEIEnabled );
271    m_acTEncTop[layer].setPictureTimingSEIEnabled( m_pictureTimingSEIEnabled );
272    m_acTEncTop[layer].setFramePackingArrangementSEIEnabled( m_framePackingSEIEnabled );
273    m_acTEncTop[layer].setFramePackingArrangementSEIType( m_framePackingSEIType );
274    m_acTEncTop[layer].setFramePackingArrangementSEIId( m_framePackingSEIId );
275    m_acTEncTop[layer].setFramePackingArrangementSEIQuincunx( m_framePackingSEIQuincunx );
276    m_acTEncTop[layer].setFramePackingArrangementSEIInterpretation( m_framePackingSEIInterpretation );
277    m_acTEncTop[layer].setDisplayOrientationSEIAngle( m_displayOrientationSEIAngle );
278    m_acTEncTop[layer].setTemporalLevel0IndexSEIEnabled( m_temporalLevel0IndexSEIEnabled );
279    m_acTEncTop[layer].setGradualDecodingRefreshInfoEnabled( m_gradualDecodingRefreshInfoEnabled );
280    m_acTEncTop[layer].setDecodingUnitInfoSEIEnabled( m_decodingUnitInfoSEIEnabled );
281    m_acTEncTop[layer].setUniformSpacingIdr          ( m_iUniformSpacingIdr );
282    m_acTEncTop[layer].setNumColumnsMinus1           ( m_iNumColumnsMinus1 );
283    m_acTEncTop[layer].setNumRowsMinus1              ( m_iNumRowsMinus1 );
284    if(m_iUniformSpacingIdr==0)
285    {
286      m_acTEncTop[layer].setColumnWidth              ( m_pColumnWidth );
287      m_acTEncTop[layer].setRowHeight                ( m_pRowHeight );
288    }
289    m_acTEncTop[layer].xCheckGSParameters();
290    Int uiTilesCount          = (m_iNumRowsMinus1+1) * (m_iNumColumnsMinus1+1);
291    if(uiTilesCount == 1)
292    {
293      m_bLFCrossTileBoundaryFlag = true; 
294    }
295    m_acTEncTop[layer].setLFCrossTileBoundaryFlag( m_bLFCrossTileBoundaryFlag );
296    m_acTEncTop[layer].setWaveFrontSynchro           ( m_iWaveFrontSynchro );
297    m_acTEncTop[layer].setWaveFrontSubstreams        ( m_acLayerCfg[layer].m_iWaveFrontSubstreams );
298    m_acTEncTop[layer].setTMVPModeId ( m_TMVPModeId );
299    m_acTEncTop[layer].setUseScalingListId           ( m_useScalingListId  );
300    m_acTEncTop[layer].setScalingListFile            ( m_scalingListFile   );
301    m_acTEncTop[layer].setSignHideFlag(m_signHideFlag);
302#if RATE_CONTROL_LAMBDA_DOMAIN
303    m_acTEncTop[layer].setUseRateCtrl         ( m_RCEnableRateControl );
304    m_acTEncTop[layer].setTargetBitrate       ( m_RCTargetBitrate );
305    m_acTEncTop[layer].setKeepHierBit         ( m_RCKeepHierarchicalBit );
306    m_acTEncTop[layer].setLCULevelRC          ( m_RCLCULevelRC );
307    m_acTEncTop[layer].setUseLCUSeparateModel ( m_RCUseLCUSeparateModel );
308    m_acTEncTop[layer].setInitialQP           ( m_RCInitialQP );
309    m_acTEncTop[layer].setForceIntraQP        ( m_RCForceIntraQP );
310#else
311    m_acTEncTop[layer].setUseRateCtrl     ( m_enableRateCtrl);
312    m_acTEncTop[layer].setTargetBitrate   ( m_targetBitrate);
313    m_acTEncTop[layer].setNumLCUInUnit    ( m_numLCUInUnit);
314#endif
315    m_acTEncTop[layer].setTransquantBypassEnableFlag(m_TransquantBypassEnableFlag);
316    m_acTEncTop[layer].setCUTransquantBypassFlagValue(m_CUTransquantBypassFlagValue);
317    m_acTEncTop[layer].setUseRecalculateQPAccordingToLambda( m_recalculateQPAccordingToLambda );
318    m_acTEncTop[layer].setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
319    m_acTEncTop[layer].setActiveParameterSetsSEIEnabled ( m_activeParameterSetsSEIEnabled ); 
320    m_acTEncTop[layer].setVuiParametersPresentFlag( m_vuiParametersPresentFlag );
321    m_acTEncTop[layer].setAspectRatioIdc( m_aspectRatioIdc );
322    m_acTEncTop[layer].setSarWidth( m_sarWidth );
323    m_acTEncTop[layer].setSarHeight( m_sarHeight );
324    m_acTEncTop[layer].setOverscanInfoPresentFlag( m_overscanInfoPresentFlag );
325    m_acTEncTop[layer].setOverscanAppropriateFlag( m_overscanAppropriateFlag );
326    m_acTEncTop[layer].setVideoSignalTypePresentFlag( m_videoSignalTypePresentFlag );
327    m_acTEncTop[layer].setVideoFormat( m_videoFormat );
328    m_acTEncTop[layer].setVideoFullRangeFlag( m_videoFullRangeFlag );
329    m_acTEncTop[layer].setColourDescriptionPresentFlag( m_colourDescriptionPresentFlag );
330    m_acTEncTop[layer].setColourPrimaries( m_colourPrimaries );
331    m_acTEncTop[layer].setTransferCharacteristics( m_transferCharacteristics );
332    m_acTEncTop[layer].setMatrixCoefficients( m_matrixCoefficients );
333    m_acTEncTop[layer].setChromaLocInfoPresentFlag( m_chromaLocInfoPresentFlag );
334    m_acTEncTop[layer].setChromaSampleLocTypeTopField( m_chromaSampleLocTypeTopField );
335    m_acTEncTop[layer].setChromaSampleLocTypeBottomField( m_chromaSampleLocTypeBottomField );
336    m_acTEncTop[layer].setNeutralChromaIndicationFlag( m_neutralChromaIndicationFlag );
337    m_acTEncTop[layer].setDefaultDisplayWindow( m_defDispWinLeftOffset, m_defDispWinRightOffset, m_defDispWinTopOffset, m_defDispWinBottomOffset );
338    m_acTEncTop[layer].setFrameFieldInfoPresentFlag( m_frameFieldInfoPresentFlag );
339    m_acTEncTop[layer].setPocProportionalToTimingFlag( m_pocProportionalToTimingFlag );
340    m_acTEncTop[layer].setNumTicksPocDiffOneMinus1   ( m_numTicksPocDiffOneMinus1    );
341    m_acTEncTop[layer].setBitstreamRestrictionFlag( m_bitstreamRestrictionFlag );
342    m_acTEncTop[layer].setTilesFixedStructureFlag( m_tilesFixedStructureFlag );
343    m_acTEncTop[layer].setMotionVectorsOverPicBoundariesFlag( m_motionVectorsOverPicBoundariesFlag );
344    m_acTEncTop[layer].setMinSpatialSegmentationIdc( m_minSpatialSegmentationIdc );
345    m_acTEncTop[layer].setMaxBytesPerPicDenom( m_maxBytesPerPicDenom );
346    m_acTEncTop[layer].setMaxBitsPerMinCuDenom( m_maxBitsPerMinCuDenom );
347    m_acTEncTop[layer].setLog2MaxMvLengthHorizontal( m_log2MaxMvLengthHorizontal );
348    m_acTEncTop[layer].setLog2MaxMvLengthVertical( m_log2MaxMvLengthVertical );
349#if SIGNAL_BITRATE_PICRATE_IN_VPS
350    TComBitRatePicRateInfo *bitRatePicRateInfo = m_cTEncTop[layer].getVPS()->getBitratePicrateInfo();
351    // The number of bit rate/pic rate have to equal to number of sub-layers.
352    if(m_bitRatePicRateMaxTLayers)
353    {
354      assert(m_bitRatePicRateMaxTLayers == m_cTEncTop[layer].getVPS()->getMaxTLayers());
355    }
356    for(Int i = 0; i < m_bitRatePicRateMaxTLayers; i++)
357    {
358      bitRatePicRateInfo->setBitRateInfoPresentFlag( i, m_bitRateInfoPresentFlag[i] );
359      if( bitRatePicRateInfo->getBitRateInfoPresentFlag(i) )
360      {
361        bitRatePicRateInfo->setAvgBitRate(i, m_avgBitRate[i]);
362        bitRatePicRateInfo->setMaxBitRate(i, m_maxBitRate[i]);
363      }
364    }
365    for(Int i = 0; i < m_bitRatePicRateMaxTLayers; i++)
366    {
367      bitRatePicRateInfo->setPicRateInfoPresentFlag( i, m_picRateInfoPresentFlag[i] );
368      if( bitRatePicRateInfo->getPicRateInfoPresentFlag(i) )
369      {
370        bitRatePicRateInfo->setAvgPicRate     (i, m_avgPicRate[i]);
371        bitRatePicRateInfo->setConstantPicRateIdc(i, m_constantPicRateIdc[i]);
372      }
373    }
374#endif
375#if REF_IDX_FRAMEWORK
376    m_acTEncTop[layer].setElRapSliceTypeB(layer == 0? 0 : m_elRapSliceBEnabled);
377#endif
378  }
379}
380#else
381Void TAppEncTop::xInitLibCfg()
382{
383  TComVPS vps;
384
385  vps.setMaxTLayers                       ( m_maxTempLayer );
386  if (m_maxTempLayer == 1)
387  {
388    vps.setTemporalNestingFlag(true);
389  }
390  vps.setMaxLayers                        ( 1 );
391  for(Int i = 0; i < MAX_TLAYER; i++)
392  {
393    vps.setNumReorderPics                 ( m_numReorderPics[i], i );
394    vps.setMaxDecPicBuffering             ( m_maxDecPicBuffering[i], i );
395  }
396  m_cTEncTop.setVPS(&vps);
397
398  m_cTEncTop.setProfile(m_profile);
399  m_cTEncTop.setLevel(m_levelTier, m_level);
400#if L0046_CONSTRAINT_FLAGS
401  m_cTEncTop.setProgressiveSourceFlag(m_progressiveSourceFlag);
402  m_cTEncTop.setInterlacedSourceFlag(m_interlacedSourceFlag);
403  m_cTEncTop.setNonPackedConstraintFlag(m_nonPackedConstraintFlag);
404  m_cTEncTop.setFrameOnlyConstraintFlag(m_frameOnlyConstraintFlag);
405#endif
406
407  m_cTEncTop.setFrameRate                    ( m_iFrameRate );
408  m_cTEncTop.setFrameSkip                    ( m_FrameSkip );
409  m_cTEncTop.setSourceWidth                  ( m_iSourceWidth );
410  m_cTEncTop.setSourceHeight                 ( m_iSourceHeight );
411  m_cTEncTop.setConformanceWindow            ( m_confLeft, m_confRight, m_confTop, m_confBottom );
412  m_cTEncTop.setFramesToBeEncoded            ( m_framesToBeEncoded );
413
414  //====== Coding Structure ========
415  m_cTEncTop.setIntraPeriod                  ( m_iIntraPeriod );
416  m_cTEncTop.setDecodingRefreshType          ( m_iDecodingRefreshType );
417  m_cTEncTop.setGOPSize                      ( m_iGOPSize );
418  m_cTEncTop.setGopList                      ( m_GOPList );
419  m_cTEncTop.setExtraRPSs                    ( m_extraRPSs );
420  for(Int i = 0; i < MAX_TLAYER; i++)
421  {
422    m_cTEncTop.setNumReorderPics             ( m_numReorderPics[i], i );
423    m_cTEncTop.setMaxDecPicBuffering         ( m_maxDecPicBuffering[i], i );
424  }
425  for( UInt uiLoop = 0; uiLoop < MAX_TLAYER; ++uiLoop )
426  {
427    m_cTEncTop.setLambdaModifier( uiLoop, m_adLambdaModifier[ uiLoop ] );
428  }
429  m_cTEncTop.setQP                           ( m_iQP );
430
431  m_cTEncTop.setPad                          ( m_aiPad );
432
433  m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayer );
434  m_cTEncTop.setUseAMP( m_enableAMP );
435
436  //===== Slice ========
437
438  //====== Loop/Deblock Filter ========
439  m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable       );
440  m_cTEncTop.setLoopFilterOffsetInPPS        ( m_loopFilterOffsetInPPS );
441  m_cTEncTop.setLoopFilterBetaOffset         ( m_loopFilterBetaOffsetDiv2  );
442  m_cTEncTop.setLoopFilterTcOffset           ( m_loopFilterTcOffsetDiv2    );
443  m_cTEncTop.setDeblockingFilterControlPresent( m_DeblockingFilterControlPresent);
444
445  //====== Motion search ========
446  m_cTEncTop.setFastSearch                   ( m_iFastSearch  );
447  m_cTEncTop.setSearchRange                  ( m_iSearchRange );
448  m_cTEncTop.setBipredSearchRange            ( m_bipredSearchRange );
449
450  //====== Quality control ========
451  m_cTEncTop.setMaxDeltaQP                   ( m_iMaxDeltaQP  );
452  m_cTEncTop.setMaxCuDQPDepth                ( m_iMaxCuDQPDepth  );
453
454  m_cTEncTop.setChromaCbQpOffset               ( m_cbQpOffset     );
455  m_cTEncTop.setChromaCrQpOffset            ( m_crQpOffset  );
456
457#if ADAPTIVE_QP_SELECTION
458  m_cTEncTop.setUseAdaptQpSelect             ( m_bUseAdaptQpSelect   );
459#endif
460
461  Int lowestQP;
462  lowestQP =  - 6*(g_bitDepthY - 8); // XXX: check
463
464  if ((m_iMaxDeltaQP == 0 ) && (m_iQP == lowestQP) && (m_useLossless == true))
465  {
466    m_bUseAdaptiveQP = false;
467  }
468  m_cTEncTop.setUseAdaptiveQP                ( m_bUseAdaptiveQP  );
469  m_cTEncTop.setQPAdaptationRange            ( m_iQPAdaptationRange );
470
471  //====== Tool list ========
472  m_cTEncTop.setUseSBACRD                    ( m_bUseSBACRD   );
473  m_cTEncTop.setDeltaQpRD                    ( m_uiDeltaQpRD  );
474  m_cTEncTop.setUseASR                       ( m_bUseASR      );
475  m_cTEncTop.setUseHADME                     ( m_bUseHADME    );
476  m_cTEncTop.setUseLossless                  ( m_useLossless );
477  m_cTEncTop.setUseLComb                     ( m_bUseLComb    );
478  m_cTEncTop.setdQPs                         ( m_aidQP        );
479  m_cTEncTop.setUseRDOQ                      ( m_useRDOQ     );
480  m_cTEncTop.setUseRDOQTS                    ( m_useRDOQTS   );
481#if L0232_RD_PENALTY
482  m_cTEncTop.setRDpenalty                 ( m_rdPenalty );
483#endif
484  m_cTEncTop.setQuadtreeTULog2MaxSize        ( m_uiQuadtreeTULog2MaxSize );
485  m_cTEncTop.setQuadtreeTULog2MinSize        ( m_uiQuadtreeTULog2MinSize );
486  m_cTEncTop.setQuadtreeTUMaxDepthInter      ( m_uiQuadtreeTUMaxDepthInter );
487  m_cTEncTop.setQuadtreeTUMaxDepthIntra      ( m_uiQuadtreeTUMaxDepthIntra );
488  m_cTEncTop.setUseFastEnc                   ( m_bUseFastEnc  );
489  m_cTEncTop.setUseEarlyCU                   ( m_bUseEarlyCU  ); 
490  m_cTEncTop.setUseFastDecisionForMerge      ( m_useFastDecisionForMerge  );
491  m_cTEncTop.setUseCbfFastMode            ( m_bUseCbfFastMode  );
492  m_cTEncTop.setUseEarlySkipDetection            ( m_useEarlySkipDetection );
493
494  m_cTEncTop.setUseTransformSkip             ( m_useTransformSkip      );
495  m_cTEncTop.setUseTransformSkipFast         ( m_useTransformSkipFast  );
496  m_cTEncTop.setUseConstrainedIntraPred      ( m_bUseConstrainedIntraPred );
497  m_cTEncTop.setPCMLog2MinSize          ( m_uiPCMLog2MinSize);
498  m_cTEncTop.setUsePCM                       ( m_usePCM );
499  m_cTEncTop.setPCMLog2MaxSize               ( m_pcmLog2MaxSize);
500  m_cTEncTop.setMaxNumMergeCand              ( m_maxNumMergeCand );
501
502
503  //====== Weighted Prediction ========
504  m_cTEncTop.setUseWP                   ( m_useWeightedPred      );
505  m_cTEncTop.setWPBiPred                ( m_useWeightedBiPred   );
506  //====== Parallel Merge Estimation ========
507  m_cTEncTop.setLog2ParallelMergeLevelMinus2 ( m_log2ParallelMergeLevel - 2 );
508
509  //====== Slice ========
510  m_cTEncTop.setSliceMode               ( m_sliceMode                );
511  m_cTEncTop.setSliceArgument           ( m_sliceArgument            );
512
513  //====== Dependent Slice ========
514  m_cTEncTop.setSliceSegmentMode        ( m_sliceSegmentMode         );
515  m_cTEncTop.setSliceSegmentArgument    ( m_sliceSegmentArgument     );
516  Int iNumPartInCU = 1<<(m_uiMaxCUDepth<<1);
517  if(m_sliceSegmentMode==FIXED_NUMBER_OF_LCU)
518  {
519    m_cTEncTop.setSliceSegmentArgument ( m_sliceSegmentArgument * iNumPartInCU );
520  }
521  if(m_sliceMode==FIXED_NUMBER_OF_LCU)
522  {
523    m_cTEncTop.setSliceArgument ( m_sliceArgument * iNumPartInCU );
524  }
525  if(m_sliceMode==FIXED_NUMBER_OF_TILES)
526  {
527    m_cTEncTop.setSliceArgument ( m_sliceArgument );
528  }
529
530  if(m_sliceMode == 0 )
531  {
532    m_bLFCrossSliceBoundaryFlag = true;
533  }
534  m_cTEncTop.setLFCrossSliceBoundaryFlag( m_bLFCrossSliceBoundaryFlag );
535  m_cTEncTop.setUseSAO ( m_bUseSAO );
536  m_cTEncTop.setMaxNumOffsetsPerPic (m_maxNumOffsetsPerPic);
537
538  m_cTEncTop.setSaoLcuBoundary (m_saoLcuBoundary);
539  m_cTEncTop.setSaoLcuBasedOptimization (m_saoLcuBasedOptimization);
540  m_cTEncTop.setPCMInputBitDepthFlag  ( m_bPCMInputBitDepthFlag); 
541  m_cTEncTop.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag); 
542
543  m_cTEncTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
544  m_cTEncTop.setRecoveryPointSEIEnabled( m_recoveryPointSEIEnabled );
545  m_cTEncTop.setBufferingPeriodSEIEnabled( m_bufferingPeriodSEIEnabled );
546  m_cTEncTop.setPictureTimingSEIEnabled( m_pictureTimingSEIEnabled );
547  m_cTEncTop.setFramePackingArrangementSEIEnabled( m_framePackingSEIEnabled );
548  m_cTEncTop.setFramePackingArrangementSEIType( m_framePackingSEIType );
549  m_cTEncTop.setFramePackingArrangementSEIId( m_framePackingSEIId );
550  m_cTEncTop.setFramePackingArrangementSEIQuincunx( m_framePackingSEIQuincunx );
551  m_cTEncTop.setFramePackingArrangementSEIInterpretation( m_framePackingSEIInterpretation );
552  m_cTEncTop.setDisplayOrientationSEIAngle( m_displayOrientationSEIAngle );
553  m_cTEncTop.setTemporalLevel0IndexSEIEnabled( m_temporalLevel0IndexSEIEnabled );
554  m_cTEncTop.setGradualDecodingRefreshInfoEnabled( m_gradualDecodingRefreshInfoEnabled );
555  m_cTEncTop.setDecodingUnitInfoSEIEnabled( m_decodingUnitInfoSEIEnabled );
556  m_cTEncTop.setUniformSpacingIdr          ( m_iUniformSpacingIdr );
557  m_cTEncTop.setNumColumnsMinus1           ( m_iNumColumnsMinus1 );
558  m_cTEncTop.setNumRowsMinus1              ( m_iNumRowsMinus1 );
559  if(m_iUniformSpacingIdr==0)
560  {
561    m_cTEncTop.setColumnWidth              ( m_pColumnWidth );
562    m_cTEncTop.setRowHeight                ( m_pRowHeight );
563  }
564  m_cTEncTop.xCheckGSParameters();
565  Int uiTilesCount          = (m_iNumRowsMinus1+1) * (m_iNumColumnsMinus1+1);
566  if(uiTilesCount == 1)
567  {
568    m_bLFCrossTileBoundaryFlag = true; 
569  }
570  m_cTEncTop.setLFCrossTileBoundaryFlag( m_bLFCrossTileBoundaryFlag );
571  m_cTEncTop.setWaveFrontSynchro           ( m_iWaveFrontSynchro );
572  m_cTEncTop.setWaveFrontSubstreams        ( m_iWaveFrontSubstreams );
573  m_cTEncTop.setTMVPModeId ( m_TMVPModeId );
574  m_cTEncTop.setUseScalingListId           ( m_useScalingListId  );
575  m_cTEncTop.setScalingListFile            ( m_scalingListFile   );
576  m_cTEncTop.setSignHideFlag(m_signHideFlag);
577#if RATE_CONTROL_LAMBDA_DOMAIN
578  m_cTEncTop.setUseRateCtrl         ( m_RCEnableRateControl );
579  m_cTEncTop.setTargetBitrate       ( m_RCTargetBitrate );
580  m_cTEncTop.setKeepHierBit         ( m_RCKeepHierarchicalBit );
581  m_cTEncTop.setLCULevelRC          ( m_RCLCULevelRC );
582  m_cTEncTop.setUseLCUSeparateModel ( m_RCUseLCUSeparateModel );
583  m_cTEncTop.setInitialQP           ( m_RCInitialQP );
584  m_cTEncTop.setForceIntraQP        ( m_RCForceIntraQP );
585#else
586  m_cTEncTop.setUseRateCtrl     ( m_enableRateCtrl);
587  m_cTEncTop.setTargetBitrate   ( m_targetBitrate);
588  m_cTEncTop.setNumLCUInUnit    ( m_numLCUInUnit);
589#endif
590  m_cTEncTop.setTransquantBypassEnableFlag(m_TransquantBypassEnableFlag);
591  m_cTEncTop.setCUTransquantBypassFlagValue(m_CUTransquantBypassFlagValue);
592  m_cTEncTop.setUseRecalculateQPAccordingToLambda( m_recalculateQPAccordingToLambda );
593  m_cTEncTop.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
594  m_cTEncTop.setActiveParameterSetsSEIEnabled ( m_activeParameterSetsSEIEnabled ); 
595  m_cTEncTop.setVuiParametersPresentFlag( m_vuiParametersPresentFlag );
596  m_cTEncTop.setAspectRatioIdc( m_aspectRatioIdc );
597  m_cTEncTop.setSarWidth( m_sarWidth );
598  m_cTEncTop.setSarHeight( m_sarHeight );
599  m_cTEncTop.setOverscanInfoPresentFlag( m_overscanInfoPresentFlag );
600  m_cTEncTop.setOverscanAppropriateFlag( m_overscanAppropriateFlag );
601  m_cTEncTop.setVideoSignalTypePresentFlag( m_videoSignalTypePresentFlag );
602  m_cTEncTop.setVideoFormat( m_videoFormat );
603  m_cTEncTop.setVideoFullRangeFlag( m_videoFullRangeFlag );
604  m_cTEncTop.setColourDescriptionPresentFlag( m_colourDescriptionPresentFlag );
605  m_cTEncTop.setColourPrimaries( m_colourPrimaries );
606  m_cTEncTop.setTransferCharacteristics( m_transferCharacteristics );
607  m_cTEncTop.setMatrixCoefficients( m_matrixCoefficients );
608  m_cTEncTop.setChromaLocInfoPresentFlag( m_chromaLocInfoPresentFlag );
609  m_cTEncTop.setChromaSampleLocTypeTopField( m_chromaSampleLocTypeTopField );
610  m_cTEncTop.setChromaSampleLocTypeBottomField( m_chromaSampleLocTypeBottomField );
611  m_cTEncTop.setNeutralChromaIndicationFlag( m_neutralChromaIndicationFlag );
612  m_cTEncTop.setDefaultDisplayWindow( m_defDispWinLeftOffset, m_defDispWinRightOffset, m_defDispWinTopOffset, m_defDispWinBottomOffset );
613  m_cTEncTop.setFrameFieldInfoPresentFlag( m_frameFieldInfoPresentFlag );
614  m_cTEncTop.setPocProportionalToTimingFlag( m_pocProportionalToTimingFlag );
615  m_cTEncTop.setNumTicksPocDiffOneMinus1   ( m_numTicksPocDiffOneMinus1    );
616  m_cTEncTop.setBitstreamRestrictionFlag( m_bitstreamRestrictionFlag );
617  m_cTEncTop.setTilesFixedStructureFlag( m_tilesFixedStructureFlag );
618  m_cTEncTop.setMotionVectorsOverPicBoundariesFlag( m_motionVectorsOverPicBoundariesFlag );
619  m_cTEncTop.setMinSpatialSegmentationIdc( m_minSpatialSegmentationIdc );
620  m_cTEncTop.setMaxBytesPerPicDenom( m_maxBytesPerPicDenom );
621  m_cTEncTop.setMaxBitsPerMinCuDenom( m_maxBitsPerMinCuDenom );
622  m_cTEncTop.setLog2MaxMvLengthHorizontal( m_log2MaxMvLengthHorizontal );
623  m_cTEncTop.setLog2MaxMvLengthVertical( m_log2MaxMvLengthVertical );
624#if SIGNAL_BITRATE_PICRATE_IN_VPS
625  TComBitRatePicRateInfo *bitRatePicRateInfo = m_cTEncTop.getVPS()->getBitratePicrateInfo();
626  // The number of bit rate/pic rate have to equal to number of sub-layers.
627  if(m_bitRatePicRateMaxTLayers)
628  {
629    assert(m_bitRatePicRateMaxTLayers == m_cTEncTop.getVPS()->getMaxTLayers());
630  }
631  for(Int i = 0; i < m_bitRatePicRateMaxTLayers; i++)
632  {
633    bitRatePicRateInfo->setBitRateInfoPresentFlag( i, m_bitRateInfoPresentFlag[i] );
634    if( bitRatePicRateInfo->getBitRateInfoPresentFlag(i) )
635    {
636      bitRatePicRateInfo->setAvgBitRate(i, m_avgBitRate[i]);
637      bitRatePicRateInfo->setMaxBitRate(i, m_maxBitRate[i]);
638    }
639  }
640  for(Int i = 0; i < m_bitRatePicRateMaxTLayers; i++)
641  {
642    bitRatePicRateInfo->setPicRateInfoPresentFlag( i, m_picRateInfoPresentFlag[i] );
643    if( bitRatePicRateInfo->getPicRateInfoPresentFlag(i) )
644    {
645      bitRatePicRateInfo->setAvgPicRate     (i, m_avgPicRate[i]);
646      bitRatePicRateInfo->setConstantPicRateIdc(i, m_constantPicRateIdc[i]);
647    }
648  }
649#endif
650}
651#endif
652
653Void TAppEncTop::xCreateLib()
654{
655  // Video I/O
656#if SVC_EXTENSION
657  // initialize global variables
658  initROM();
659
660  for(UInt layer=0; layer<m_numLayers; layer++)
661  {
662    m_acTVideoIOYuvInputFile[layer].open( (Char *)m_acLayerCfg[layer].getInputFile().c_str(),  false, m_inputBitDepthY, m_inputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // read  mode
663    m_acTVideoIOYuvInputFile[layer].skipFrames(m_FrameSkip, m_acLayerCfg[layer].getSourceWidth() - m_acLayerCfg[layer].getPad()[0], m_acLayerCfg[layer].getSourceHeight() - m_acLayerCfg[layer].getPad()[1]);
664
665    if (!m_acLayerCfg[layer].getReconFile().empty())
666    {
667      m_acTVideoIOYuvReconFile[layer].open((Char *)m_acLayerCfg[layer].getReconFile().c_str(), true, m_outputBitDepthY, m_outputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // write mode
668    }
669
670    m_acTEncTop[layer].create();
671  }
672#else
673  m_cTVideoIOYuvInputFile.open( m_pchInputFile,     false, m_inputBitDepthY, m_inputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // read  mode
674  m_cTVideoIOYuvInputFile.skipFrames(m_FrameSkip, m_iSourceWidth - m_aiPad[0], m_iSourceHeight - m_aiPad[1]);
675
676  if (m_pchReconFile)
677    m_cTVideoIOYuvReconFile.open(m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, m_internalBitDepthY, m_internalBitDepthC);  // write mode
678
679  // Neo Decoder
680  m_cTEncTop.create();
681#endif
682}
683
684Void TAppEncTop::xDestroyLib()
685{
686  // Video I/O
687#if SVC_EXTENSION
688  // destroy ROM
689  destroyROM();
690
691  for(UInt layer=0; layer<m_numLayers; layer++)
692  {
693    m_acTVideoIOYuvInputFile[layer].close();
694    m_acTVideoIOYuvReconFile[layer].close();
695
696    m_acTEncTop[layer].destroy();
697  }
698#else
699  m_cTVideoIOYuvInputFile.close();
700  m_cTVideoIOYuvReconFile.close();
701
702  // Neo Decoder
703  m_cTEncTop.destroy();
704#endif
705}
706
707Void TAppEncTop::xInitLib()
708{
709#if SVC_EXTENSION
710  for(UInt layer=0; layer<m_numLayers; layer++)
711  {
712    m_acTEncTop[layer].init();
713  }
714#if VPS_RENAME
715  m_acTEncTop[0].getVPS()->setMaxLayers( m_numLayers );
716#endif
717#if VPS_EXTN_OP_LAYER_SETS
718  TComVPS* vps = m_acTEncTop[0].getVPS();
719  vps->setMaxLayerId(m_numLayers - 1);    // Set max-layer ID
720
721  vps->setNumLayerSets(m_numLayers);
722  for(Int setId = 1; setId < vps->getNumLayerSets(); setId++)
723  {
724    for(Int layerId = 0; layerId <= vps->getMaxLayerId(); layerId++)
725    {
726      vps->setLayerIdIncludedFlag(true, setId, layerId);
727    }
728  }
729#if VPS_EXTN_MASK_AND_DIM_INFO
730  UInt i = 0, dimIdLen = 0;
731  vps->setAvcBaseLayerFlag(false);
732  vps->setSplittingFlag(false);
733  for(i = 0; i < MAX_VPS_NUM_SCALABILITY_TYPES; i++)
734  {
735    vps->setScalabilityMask(i, false);
736  }
737  if(m_numLayers > 1) 
738  {
739    vps->setScalabilityMask(1, true); // Only turn on spatial/SNR scalability
740    vps->setNumScalabilityTypes(1);
741  }
742  else
743  {
744    vps->setNumScalabilityTypes(0);
745  }
746  while((1 << dimIdLen) < m_numLayers)
747  {
748    dimIdLen++;
749  }
750  vps->setDimensionIdLen(0, dimIdLen);
751  vps->setNuhLayerIdPresentFlag(false);
752  vps->setLayerIdInNuh(0, 0);
753  vps->setLayerIdInVps(0, 0);
754  for(i = 1; i < vps->getMaxLayers(); i++)
755  {
756    vps->setLayerIdInNuh(i, i);
757    vps->setLayerIdInVps(vps->getLayerIdInNuh(i), i);
758    vps->setDimensionId(i, 0, i);
759  }
760#endif
761#if VPS_EXTN_PROFILE_INFO
762  vps->getPTLForExtnPtr()->resize(vps->getNumLayerSets());
763  for(Int setId = 1; setId < vps->getNumLayerSets(); setId++)
764  {
765    vps->setProfilePresentFlag(setId, true);
766    // Note - may need to be changed for other layer structures.
767    *(vps->getPTLForExtn(setId)) = *(m_acTEncTop[setId].getSPS()->getPTL());
768  }
769#endif
770  // Target output layer
771  vps->setNumOutputLayerSets(1);
772  Int lsIdx = 1;
773  vps->setOutputLayerSetIdx(0, lsIdx); // Because only one layer set
774  // Include the highest layer as output layer
775  for(UInt layer=0; layer <= vps->getMaxLayerId() ; layer++)
[2]776  {
[125]777    if(vps->getLayerIdIncludedFlag(lsIdx, layer))
[2]778    {
[125]779      vps->setOutputLayerFlag(lsIdx, layer, layer == (vps->getMaxLayerId()));
[2]780    }
[125]781  }
782#endif
783#if VPS_EXTN_DIRECT_REF_LAYERS
784  // Direct reference layers
785  for(UInt layerCtr = 1;layerCtr <= vps->getMaxLayers() - 1; layerCtr++)
786  {
787    vps->setNumDirectRefLayers( layerCtr, m_acTEncTop[layerCtr].getNumDirectRefLayers() );
788    for(i = 0; i < vps->getNumDirectRefLayers(layerCtr); i++)
789    {
790      vps->setRefLayerId( layerCtr, i, m_acTEncTop[layerCtr].getRefLayerId(i) );
791    }
792    // Set direct dependency flag
793    // Initialize flag to 0
794    for(Int refLayerCtr = 0; refLayerCtr < layerCtr; refLayerCtr++)
795    {
796      vps->setDirectDependencyFlag( layerCtr, refLayerCtr, false );
797    }
798    for(i = 0; i < vps->getNumDirectRefLayers(layerCtr); i++)
799    {
800      vps->setDirectDependencyFlag( layerCtr, vps->getLayerIdInVps(m_acTEncTop[layerCtr].getRefLayerId(i)), true);
801    }
802  }
803#endif
804#else
805  m_cTEncTop.init();
806#endif
807}
808
809// ====================================================================================================================
810// Public member functions
811// ====================================================================================================================
812
813/**
814- create internal class
815- initialize internal variable
816- until the end of input YUV file, call encoding function in TEncTop class
817- delete allocated buffers
818- destroy internal class
819.
820*/
821#if SVC_EXTENSION
822Void TAppEncTop::encode()
823{
824  fstream bitstreamFile(m_pBitstreamFile, fstream::binary | fstream::out);
825  if (!bitstreamFile)
826  {
827    fprintf(stderr, "\nfailed to open bitstream file `%s' for writing\n", m_pBitstreamFile);
828    exit(EXIT_FAILURE);
829  }
830
831  TComPicYuv*       pcPicYuvOrg [MAX_LAYERS];
832  TComPicYuv*       pcPicYuvRec = NULL;
833
834  // initialize internal class & member variables
835  xInitLibCfg();
836  xCreateLib();
837  xInitLib();
838
839  // main encoder loop
840  Int   iNumEncoded = 0, iTotalNumEncoded = 0;
841  Bool  bEos = false;
842
843  list<AccessUnit> outputAccessUnits; ///< list of access units to write out.  is populated by the encoding process
844
845  for(UInt layer=0; layer<m_numLayers; layer++)
846  {
847    // allocate original YUV buffer
848    pcPicYuvOrg[layer] = new TComPicYuv;
849#if SVC_UPSAMPLING
850    pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
851#else
852    pcPicYuvOrg->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
853#endif
854  }
855
856#if AVC_SYNTAX
857  if( !m_BLSyntaxFile )
858  {
859    printf( "Wrong base layer syntax input file\n" );
860    exit(EXIT_FAILURE);
861  }
862  fstream streamSyntaxFile( m_BLSyntaxFile, fstream::in | fstream::binary );
863  if( !streamSyntaxFile.good() )
864  {
865    printf( "Base layer syntax input reading error\n" );
866    exit(EXIT_FAILURE);
867  }
868  m_acTEncTop[0].setBLSyntaxFile( &streamSyntaxFile );
869#endif
870
871  Bool bFirstFrame = true;
872  while ( !bEos )
873  {
874    // Read enough frames
875    Bool bFramesReadyToCode = false;
876    while(!bFramesReadyToCode)
877    {
878      for(UInt layer=0; layer<m_numLayers; layer++)
879      {
880        // get buffers
881        xGetBuffer(pcPicYuvRec, layer);
882
883        // read input YUV file
884        m_acTVideoIOYuvInputFile[layer].read( pcPicYuvOrg[layer], m_acLayerCfg[layer].getPad() );
885
886        if(layer == m_numLayers-1)
887        {
888          // increase number of received frames
889          m_iFrameRcvd++;
890          // check end of file
891          bEos = (m_iFrameRcvd == m_framesToBeEncoded);
892        }
893
894        m_acTEncTop[layer].encodePrep( pcPicYuvOrg[layer] );
895      }
896
897      bFramesReadyToCode = !(!bFirstFrame && ( m_acTEncTop[m_numLayers-1].getNumPicRcvd() != m_iGOPSize && m_iGOPSize ) && !bEos );
898    }
899    Bool flush = 0;
900    // if end of file (which is only detected on a read failure) flush the encoder of any queued pictures
901    if (m_acTVideoIOYuvInputFile[m_numLayers-1].isEof())
902    {
903      flush = true;
904      bEos = true;
905      m_iFrameRcvd--;
906      m_acTEncTop[m_numLayers-1].setFramesToBeEncoded(m_iFrameRcvd);
907    }
908
909    // loop through frames in one GOP
910    for ( UInt iPicIdInGOP=0; iPicIdInGOP < (bFirstFrame? 1:m_iGOPSize); iPicIdInGOP++ )
911    {
912      // layer by layer for each frame
913      for(UInt layer=0; layer<m_numLayers; layer++)
914      {
915        // call encoding function for one frame
916        m_acTEncTop[layer].encode( flush ? 0 : pcPicYuvOrg[layer], m_acListPicYuvRec[layer], outputAccessUnits, iPicIdInGOP );
917      }
918    }
919
920    iTotalNumEncoded = 0;
921    for(UInt layer=0; layer<m_numLayers; layer++)
922    {
923      // write bistream to file if necessary
924      iNumEncoded = m_acTEncTop[layer].getNumPicRcvd();
925      if ( iNumEncoded > 0 )
926      {
927        xWriteRecon(layer, iNumEncoded);
928        iTotalNumEncoded += iNumEncoded;
929      }
930      m_acTEncTop[layer].setNumPicRcvd( 0 );
931    }
932
933    // write bitstream out
934    if(iTotalNumEncoded)
935    {
936      xWriteStream(bitstreamFile, iTotalNumEncoded, outputAccessUnits);
937      outputAccessUnits.clear();
938    }
939
940    // print out summary
941    if (bEos)
942    {
943      printOutSummary();
944    }
945
946    bFirstFrame = false;
947  }
948  // delete original YUV buffer
949  for(UInt layer=0; layer<m_numLayers; layer++)
950  {
951    pcPicYuvOrg[layer]->destroy();
952    delete pcPicYuvOrg[layer];
953    pcPicYuvOrg[layer] = NULL;
954
955    // delete used buffers in encoder class
956    m_acTEncTop[layer].deletePicBuffer();
957  }
958
959#if AVC_SYNTAX
960  if( streamSyntaxFile.is_open() )
961  {
962    streamSyntaxFile.close();
963  }
964#endif
965
966  // delete buffers & classes
967  xDeleteBuffer();
968  xDestroyLib();
969
970  printRateSummary();
971
972  return;
973}
974
975Void TAppEncTop::printOutSummary()
976{
977  UInt layer;
978
979  // set frame rate
980  for(layer = 0; layer < m_numLayers; layer++)
981  {
982    m_gcAnalyzeAll[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate());
983    m_gcAnalyzeI[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
984    m_gcAnalyzeP[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
985    m_gcAnalyzeB[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
986  }
987
988  //-- all
989  printf( "\n\nSUMMARY --------------------------------------------------------\n" );
990  printf( "\tTotal Frames |  "   "Bitrate    "  "Y-PSNR    "  "U-PSNR    "  "V-PSNR \n" );
991  for(layer = 0; layer < m_numLayers; layer++)
992  {
993    m_gcAnalyzeAll[layer].printOut('a', layer);
994  }
995
996  printf( "\n\nI Slices--------------------------------------------------------\n" );
997  printf( "\tTotal Frames |  "   "Bitrate    "  "Y-PSNR    "  "U-PSNR    "  "V-PSNR \n" );
998  for(layer = 0; layer < m_numLayers; layer++)
999  {
1000    m_gcAnalyzeI[layer].printOut('i', layer);
1001  }
1002
1003  printf( "\n\nP Slices--------------------------------------------------------\n" );
1004  printf( "\tTotal Frames |  "   "Bitrate    "  "Y-PSNR    "  "U-PSNR    "  "V-PSNR \n" );
1005  for(layer = 0; layer < m_numLayers; layer++)
1006  {
1007    m_gcAnalyzeP[layer].printOut('p', layer);
1008  }
1009
1010  printf( "\n\nB Slices--------------------------------------------------------\n" );
1011  printf( "\tTotal Frames |  "   "Bitrate    "  "Y-PSNR    "  "U-PSNR    "  "V-PSNR \n" );
1012  for(layer = 0; layer < m_numLayers; layer++)
1013  {
1014    m_gcAnalyzeB[layer].printOut('b', layer);
1015  }
1016}
1017
1018#else
1019Void TAppEncTop::encode()
1020{
1021  fstream bitstreamFile(m_pchBitstreamFile, fstream::binary | fstream::out);
1022  if (!bitstreamFile)
1023  {
1024    fprintf(stderr, "\nfailed to open bitstream file `%s' for writing\n", m_pchBitstreamFile);
1025    exit(EXIT_FAILURE);
1026  }
1027
1028  TComPicYuv*       pcPicYuvOrg = new TComPicYuv;
1029  TComPicYuv*       pcPicYuvRec = NULL;
1030
1031  // initialize internal class & member variables
1032  xInitLibCfg();
1033  xCreateLib();
1034  xInitLib();
1035
1036  // main encoder loop
1037  Int   iNumEncoded = 0;
1038  Bool  bEos = false;
1039
1040  list<AccessUnit> outputAccessUnits; ///< list of access units to write out.  is populated by the encoding process
1041
1042  // allocate original YUV buffer
1043  pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
1044
1045  while ( !bEos )
1046  {
1047    // get buffers
1048    xGetBuffer(pcPicYuvRec);
1049
1050    // read input YUV file
1051    m_cTVideoIOYuvInputFile.read( pcPicYuvOrg, m_aiPad );
1052
1053    // increase number of received frames
1054    m_iFrameRcvd++;
1055
1056    bEos = (m_iFrameRcvd == m_framesToBeEncoded);
1057
1058    Bool flush = 0;
1059    // if end of file (which is only detected on a read failure) flush the encoder of any queued pictures
1060    if (m_cTVideoIOYuvInputFile.isEof())
1061    {
1062      flush = true;
1063      bEos = true;
1064      m_iFrameRcvd--;
1065      m_cTEncTop.setFramesToBeEncoded(m_iFrameRcvd);
1066    }
1067
1068    // call encoding function for one frame
1069    m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded );
1070
1071    // write bistream to file if necessary
1072    if ( iNumEncoded > 0 )
1073    {
1074      xWriteOutput(bitstreamFile, iNumEncoded, outputAccessUnits);
1075      outputAccessUnits.clear();
1076    }
1077  }
1078
1079  m_cTEncTop.printSummary();
1080
1081  // delete original YUV buffer
1082  pcPicYuvOrg->destroy();
1083  delete pcPicYuvOrg;
1084  pcPicYuvOrg = NULL;
1085
1086  // delete used buffers in encoder class
1087  m_cTEncTop.deletePicBuffer();
1088
1089  // delete buffers & classes
1090  xDeleteBuffer();
1091  xDestroyLib();
1092
1093  printRateSummary();
1094
1095  return;
1096}
1097#endif
1098
1099// ====================================================================================================================
1100// Protected member functions
1101// ====================================================================================================================
1102
1103/**
1104- application has picture buffer list with size of GOP
1105- picture buffer list acts as ring buffer
1106- end of the list has the latest picture
1107.
1108*/
1109#if SVC_EXTENSION
1110Void TAppEncTop::xGetBuffer( TComPicYuv*& rpcPicYuvRec, UInt layer)
1111{
1112  assert( m_iGOPSize > 0 );
1113
1114  // org. buffer
1115  if ( m_acListPicYuvRec[layer].size() == (UInt)m_iGOPSize )
1116  {
1117    rpcPicYuvRec = m_acListPicYuvRec[layer].popFront();
1118
1119  }
1120  else
1121  {
1122    rpcPicYuvRec = new TComPicYuv;
1123
1124#if SVC_UPSAMPLING
1125    rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
1126#else
1127    rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
1128#endif
1129
1130  }
1131  m_acListPicYuvRec[layer].pushBack( rpcPicYuvRec );
1132}
1133
1134Void TAppEncTop::xDeleteBuffer( )
1135{
1136  for(UInt layer=0; layer<m_numLayers; layer++)
1137  {
1138    TComList<TComPicYuv*>::iterator iterPicYuvRec  = m_acListPicYuvRec[layer].begin();
1139
1140    Int iSize = Int( m_acListPicYuvRec[layer].size() );
1141
1142    for ( Int i = 0; i < iSize; i++ )
1143    {
1144      TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
1145      pcPicYuvRec->destroy();
1146      delete pcPicYuvRec; pcPicYuvRec = NULL;
1147    }
1148  } 
1149}
1150
1151Void TAppEncTop::xWriteRecon(UInt layer, Int iNumEncoded)
1152{
1153  Int i;
1154
1155  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_acListPicYuvRec[layer].end();
1156
1157  for ( i = 0; i < iNumEncoded; i++ )
1158  {
1159    --iterPicYuvRec;
1160  }
1161
1162  for ( i = 0; i < iNumEncoded; i++ )
1163  {
1164    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
1165    if (!m_acLayerCfg[layer].getReconFile().empty())
1166    {
1167      m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRec, m_acLayerCfg[layer].getConfLeft(), m_acLayerCfg[layer].getConfRight(), 
1168        m_acLayerCfg[layer].getConfTop(), m_acLayerCfg[layer].getConfBottom() );
1169    }
1170  }
1171}
1172
1173Void TAppEncTop::xWriteStream(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits)
1174{
1175  Int i;
1176
1177  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
1178
1179  for ( i = 0; i < iNumEncoded; i++ )
1180  {
1181    const AccessUnit& au = *(iterBitstream++);
1182    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
1183    rateStatsAccum(au, stats);
1184  }
1185}
1186
1187#else // SVC_EXTENSION
1188Void TAppEncTop::xGetBuffer( TComPicYuv*& rpcPicYuvRec)
1189{
1190  assert( m_iGOPSize > 0 );
1191
1192  // org. buffer
1193  if ( m_cListPicYuvRec.size() == (UInt)m_iGOPSize )
1194  {
1195    rpcPicYuvRec = m_cListPicYuvRec.popFront();
1196
1197  }
1198  else
1199  {
1200    rpcPicYuvRec = new TComPicYuv;
1201
1202    rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
1203
1204  }
1205  m_cListPicYuvRec.pushBack( rpcPicYuvRec );
1206}
1207
1208Void TAppEncTop::xDeleteBuffer( )
1209{
1210  TComList<TComPicYuv*>::iterator iterPicYuvRec  = m_cListPicYuvRec.begin();
1211
1212  Int iSize = Int( m_cListPicYuvRec.size() );
1213
1214  for ( Int i = 0; i < iSize; i++ )
1215  {
1216    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
1217    pcPicYuvRec->destroy();
1218    delete pcPicYuvRec; pcPicYuvRec = NULL;
1219  }
1220
1221}
1222
1223/** \param iNumEncoded  number of encoded frames
1224*/
1225Void TAppEncTop::xWriteOutput(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits)
1226{
1227  Int i;
1228
1229  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
1230  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
1231
1232  for ( i = 0; i < iNumEncoded; i++ )
1233  {
1234    --iterPicYuvRec;
1235  }
1236
1237  for ( i = 0; i < iNumEncoded; i++ )
1238  {
1239    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
1240    if (m_pchReconFile)
1241    {
1242      m_cTVideoIOYuvReconFile.write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
1243    }
1244
1245    const AccessUnit& au = *(iterBitstream++);
1246    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
1247    rateStatsAccum(au, stats);
1248  }
1249}
1250#endif
1251
1252/**
1253*
1254*/
1255void TAppEncTop::rateStatsAccum(const AccessUnit& au, const std::vector<UInt>& annexBsizes)
1256{
1257  AccessUnit::const_iterator it_au = au.begin();
1258  vector<UInt>::const_iterator it_stats = annexBsizes.begin();
1259
1260  for (; it_au != au.end(); it_au++, it_stats++)
1261  {
1262    switch ((*it_au)->m_nalUnitType)
1263    {
1264    case NAL_UNIT_CODED_SLICE_TRAIL_R:
1265    case NAL_UNIT_CODED_SLICE_TRAIL_N:
1266    case NAL_UNIT_CODED_SLICE_TLA:
1267    case NAL_UNIT_CODED_SLICE_TSA_N:
1268    case NAL_UNIT_CODED_SLICE_STSA_R:
1269    case NAL_UNIT_CODED_SLICE_STSA_N:
1270    case NAL_UNIT_CODED_SLICE_BLA:
1271    case NAL_UNIT_CODED_SLICE_BLANT:
1272    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
1273    case NAL_UNIT_CODED_SLICE_IDR:
1274    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
1275    case NAL_UNIT_CODED_SLICE_CRA:
1276    case NAL_UNIT_CODED_SLICE_RADL_N:
1277    case NAL_UNIT_CODED_SLICE_DLP:
1278    case NAL_UNIT_CODED_SLICE_RASL_N:
1279    case NAL_UNIT_CODED_SLICE_TFD:
1280    case NAL_UNIT_VPS:
1281    case NAL_UNIT_SPS:
1282    case NAL_UNIT_PPS:
1283      m_essentialBytes += *it_stats;
1284      break;
1285    default:
1286      break;
1287    }
1288
1289    m_totalBytes += *it_stats;
1290  }
1291}
1292
1293void TAppEncTop::printRateSummary()
1294{
1295#if SVC_EXTENSION
1296  Double time = (Double) m_iFrameRcvd / m_acLayerCfg[m_numLayers-1].getFrameRate();
1297#else
1298  Double time = (Double) m_iFrameRcvd / m_iFrameRate;
1299#endif
1300  printf("Bytes written to file: %u (%.3f kbps)\n", m_totalBytes, 0.008 * m_totalBytes / time);
1301#if VERBOSE_RATE
1302  printf("Bytes for SPS/PPS/Slice (Incl. Annex B): %u (%.3f kbps)\n", m_essentialBytes, 0.008 * m_essentialBytes / time);
1303#endif
1304}
1305
1306//! \}
Note: See TracBrowser for help on using the repository browser.