source: SHVCSoftware/branches/HM-10.0-dev-SHM/source/App/TAppEncoder/TAppEncTop.cpp @ 115

Last change on this file since 115 was 115, checked in by vidyo, 12 years ago

Adding missing syntax elements for VPS extension; by default this code is disabled. Turn on VPS_EXTN_MASK_AND_DIM_INFO to enable this code.

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