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

Last change on this file since 114 was 114, checked in by qualcomm, 12 years ago

Rename vps_max_layers_minus1 in VPS (MACRO: VPS_RENAME)

Rename variables vps_reserved_zero_6bits to vps_max_layers_minus1.

From: Adarsh K. Ramasubramonian <aramasub@…>

File size: 47.3 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_PROFILE_INFO
712  vps->getPTLForExtnPtr()->resize(vps->getNumLayerSets());
713  for(Int setId = 1; setId < vps->getNumLayerSets(); setId++)
714  {
715    vps->setProfilePresentFlag(setId, true);
716    // Note - may need to be changed for other layer structures.
717    *(vps->getPTLForExtn(setId)) = *(m_acTEncTop[setId].getSPS()->getPTL());
718  }
719#endif
720  // Target output layer
721  vps->setNumOutputLayerSets(1);
722  Int lsIdx = 1;
723  vps->setOutputLayerSetIdx(0, lsIdx); // Because only one layer set
724  // Include the highest layer as output layer
725  for(UInt layer=0; layer <= vps->getMaxLayerId() ; layer++)
726  {
727    if(vps->getLayerIdIncludedFlag(lsIdx, layer))
728    {
729      vps->setOutputLayerFlag(lsIdx, layer, layer == (vps->getMaxLayerId()));
730    }
731  }
732#endif
733#else
734  m_cTEncTop.init();
735#endif
736}
737
738// ====================================================================================================================
739// Public member functions
740// ====================================================================================================================
741
742/**
743- create internal class
744- initialize internal variable
745- until the end of input YUV file, call encoding function in TEncTop class
746- delete allocated buffers
747- destroy internal class
748.
749*/
750#if SVC_EXTENSION
751Void TAppEncTop::encode()
752{
753  fstream bitstreamFile(m_pBitstreamFile, fstream::binary | fstream::out);
754  if (!bitstreamFile)
755  {
756    fprintf(stderr, "\nfailed to open bitstream file `%s' for writing\n", m_pBitstreamFile);
757    exit(EXIT_FAILURE);
758  }
759
760  TComPicYuv*       pcPicYuvOrg [MAX_LAYERS];
761  TComPicYuv*       pcPicYuvRec = NULL;
762
763  // initialize internal class & member variables
764  xInitLibCfg();
765  xCreateLib();
766  xInitLib();
767
768  // main encoder loop
769  Int   iNumEncoded = 0, iTotalNumEncoded = 0;
770  Bool  bEos = false;
771
772  list<AccessUnit> outputAccessUnits; ///< list of access units to write out.  is populated by the encoding process
773
774  for(UInt layer=0; layer<m_numLayers; layer++)
775  {
776    // allocate original YUV buffer
777    pcPicYuvOrg[layer] = new TComPicYuv;
778#if SVC_UPSAMPLING
779    pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
780#else
781    pcPicYuvOrg->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
782#endif
783  }
784
785#if AVC_SYNTAX
786  if( !m_BLSyntaxFile )
787  {
788    printf( "Wrong base layer syntax input file\n" );
789    exit(EXIT_FAILURE);
790  }
791  fstream streamSyntaxFile( m_BLSyntaxFile, fstream::in | fstream::binary );
792  if( !streamSyntaxFile.good() )
793  {
794    printf( "Base layer syntax input reading error\n" );
795    exit(EXIT_FAILURE);
796  }
797  m_acTEncTop[0].setBLSyntaxFile( &streamSyntaxFile );
798#endif
799
800  Bool bFirstFrame = true;
801  while ( !bEos )
802  {
803    // Read enough frames
804    Bool bFramesReadyToCode = false;
805    while(!bFramesReadyToCode)
806    {
807      for(UInt layer=0; layer<m_numLayers; layer++)
808      {
809        // get buffers
810        xGetBuffer(pcPicYuvRec, layer);
811
812        // read input YUV file
813        m_acTVideoIOYuvInputFile[layer].read( pcPicYuvOrg[layer], m_acLayerCfg[layer].getPad() );
814
815        if(layer == m_numLayers-1)
816        {
817          // increase number of received frames
818          m_iFrameRcvd++;
819          // check end of file
820          bEos = (m_iFrameRcvd == m_framesToBeEncoded);
821        }
822
823        m_acTEncTop[layer].encodePrep( pcPicYuvOrg[layer] );
824      }
825
826      bFramesReadyToCode = !(!bFirstFrame && ( m_acTEncTop[m_numLayers-1].getNumPicRcvd() != m_iGOPSize && m_iGOPSize ) && !bEos );
827    }
828    Bool flush = 0;
829    // if end of file (which is only detected on a read failure) flush the encoder of any queued pictures
830    if (m_acTVideoIOYuvInputFile[m_numLayers-1].isEof())
831    {
832      flush = true;
833      bEos = true;
834      m_iFrameRcvd--;
835      m_acTEncTop[m_numLayers-1].setFramesToBeEncoded(m_iFrameRcvd);
836    }
837
838    // loop through frames in one GOP
839    for ( UInt iPicIdInGOP=0; iPicIdInGOP < (bFirstFrame? 1:m_iGOPSize); iPicIdInGOP++ )
840    {
841      // layer by layer for each frame
842      for(UInt layer=0; layer<m_numLayers; layer++)
843      {
844        // call encoding function for one frame
845        m_acTEncTop[layer].encode( flush ? 0 : pcPicYuvOrg[layer], m_acListPicYuvRec[layer], outputAccessUnits, iPicIdInGOP );
846      }
847    }
848
849    iTotalNumEncoded = 0;
850    for(UInt layer=0; layer<m_numLayers; layer++)
851    {
852      // write bistream to file if necessary
853      iNumEncoded = m_acTEncTop[layer].getNumPicRcvd();
854      if ( iNumEncoded > 0 )
855      {
856        xWriteRecon(layer, iNumEncoded);
857        iTotalNumEncoded += iNumEncoded;
858      }
859      m_acTEncTop[layer].setNumPicRcvd( 0 );
860    }
861
862    // write bitstream out
863    if(iTotalNumEncoded)
864    {
865      xWriteStream(bitstreamFile, iTotalNumEncoded, outputAccessUnits);
866      outputAccessUnits.clear();
867    }
868
869    // print out summary
870    if (bEos)
871    {
872      printOutSummary();
873    }
874
875    bFirstFrame = false;
876  }
877  // delete original YUV buffer
878  for(UInt layer=0; layer<m_numLayers; layer++)
879  {
880    pcPicYuvOrg[layer]->destroy();
881    delete pcPicYuvOrg[layer];
882    pcPicYuvOrg[layer] = NULL;
883
884    // delete used buffers in encoder class
885    m_acTEncTop[layer].deletePicBuffer();
886  }
887
888#if AVC_SYNTAX
889  if( streamSyntaxFile.is_open() )
890  {
891    streamSyntaxFile.close();
892  }
893#endif
894
895  // delete buffers & classes
896  xDeleteBuffer();
897  xDestroyLib();
898
899  printRateSummary();
900
901  return;
902}
903
904Void TAppEncTop::printOutSummary()
905{
906  UInt layer;
907
908  // set frame rate
909  for(layer = 0; layer < m_numLayers; layer++)
910  {
911    m_gcAnalyzeAll[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate());
912    m_gcAnalyzeI[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
913    m_gcAnalyzeP[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
914    m_gcAnalyzeB[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
915  }
916
917  //-- all
918  printf( "\n\nSUMMARY --------------------------------------------------------\n" );
919  printf( "\tTotal Frames |  "   "Bitrate    "  "Y-PSNR    "  "U-PSNR    "  "V-PSNR \n" );
920  for(layer = 0; layer < m_numLayers; layer++)
921  {
922    m_gcAnalyzeAll[layer].printOut('a', layer);
923  }
924
925  printf( "\n\nI Slices--------------------------------------------------------\n" );
926  printf( "\tTotal Frames |  "   "Bitrate    "  "Y-PSNR    "  "U-PSNR    "  "V-PSNR \n" );
927  for(layer = 0; layer < m_numLayers; layer++)
928  {
929    m_gcAnalyzeI[layer].printOut('i', layer);
930  }
931
932  printf( "\n\nP Slices--------------------------------------------------------\n" );
933  printf( "\tTotal Frames |  "   "Bitrate    "  "Y-PSNR    "  "U-PSNR    "  "V-PSNR \n" );
934  for(layer = 0; layer < m_numLayers; layer++)
935  {
936    m_gcAnalyzeP[layer].printOut('p', layer);
937  }
938
939  printf( "\n\nB Slices--------------------------------------------------------\n" );
940  printf( "\tTotal Frames |  "   "Bitrate    "  "Y-PSNR    "  "U-PSNR    "  "V-PSNR \n" );
941  for(layer = 0; layer < m_numLayers; layer++)
942  {
943    m_gcAnalyzeB[layer].printOut('b', layer);
944  }
945}
946
947#else
948Void TAppEncTop::encode()
949{
950  fstream bitstreamFile(m_pchBitstreamFile, fstream::binary | fstream::out);
951  if (!bitstreamFile)
952  {
953    fprintf(stderr, "\nfailed to open bitstream file `%s' for writing\n", m_pchBitstreamFile);
954    exit(EXIT_FAILURE);
955  }
956
957  TComPicYuv*       pcPicYuvOrg = new TComPicYuv;
958  TComPicYuv*       pcPicYuvRec = NULL;
959
960  // initialize internal class & member variables
961  xInitLibCfg();
962  xCreateLib();
963  xInitLib();
964
965  // main encoder loop
966  Int   iNumEncoded = 0;
967  Bool  bEos = false;
968
969  list<AccessUnit> outputAccessUnits; ///< list of access units to write out.  is populated by the encoding process
970
971  // allocate original YUV buffer
972  pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
973
974  while ( !bEos )
975  {
976    // get buffers
977    xGetBuffer(pcPicYuvRec);
978
979    // read input YUV file
980    m_cTVideoIOYuvInputFile.read( pcPicYuvOrg, m_aiPad );
981
982    // increase number of received frames
983    m_iFrameRcvd++;
984
985    bEos = (m_iFrameRcvd == m_framesToBeEncoded);
986
987    Bool flush = 0;
988    // if end of file (which is only detected on a read failure) flush the encoder of any queued pictures
989    if (m_cTVideoIOYuvInputFile.isEof())
990    {
991      flush = true;
992      bEos = true;
993      m_iFrameRcvd--;
994      m_cTEncTop.setFramesToBeEncoded(m_iFrameRcvd);
995    }
996
997    // call encoding function for one frame
998    m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded );
999
1000    // write bistream to file if necessary
1001    if ( iNumEncoded > 0 )
1002    {
1003      xWriteOutput(bitstreamFile, iNumEncoded, outputAccessUnits);
1004      outputAccessUnits.clear();
1005    }
1006  }
1007
1008  m_cTEncTop.printSummary();
1009
1010  // delete original YUV buffer
1011  pcPicYuvOrg->destroy();
1012  delete pcPicYuvOrg;
1013  pcPicYuvOrg = NULL;
1014
1015  // delete used buffers in encoder class
1016  m_cTEncTop.deletePicBuffer();
1017
1018  // delete buffers & classes
1019  xDeleteBuffer();
1020  xDestroyLib();
1021
1022  printRateSummary();
1023
1024  return;
1025}
1026#endif
1027
1028// ====================================================================================================================
1029// Protected member functions
1030// ====================================================================================================================
1031
1032/**
1033- application has picture buffer list with size of GOP
1034- picture buffer list acts as ring buffer
1035- end of the list has the latest picture
1036.
1037*/
1038#if SVC_EXTENSION
1039Void TAppEncTop::xGetBuffer( TComPicYuv*& rpcPicYuvRec, UInt layer)
1040{
1041  assert( m_iGOPSize > 0 );
1042
1043  // org. buffer
1044  if ( m_acListPicYuvRec[layer].size() == (UInt)m_iGOPSize )
1045  {
1046    rpcPicYuvRec = m_acListPicYuvRec[layer].popFront();
1047
1048  }
1049  else
1050  {
1051    rpcPicYuvRec = new TComPicYuv;
1052
1053#if SVC_UPSAMPLING
1054    rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
1055#else
1056    rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
1057#endif
1058
1059  }
1060  m_acListPicYuvRec[layer].pushBack( rpcPicYuvRec );
1061}
1062
1063Void TAppEncTop::xDeleteBuffer( )
1064{
1065  for(UInt layer=0; layer<m_numLayers; layer++)
1066  {
1067    TComList<TComPicYuv*>::iterator iterPicYuvRec  = m_acListPicYuvRec[layer].begin();
1068
1069    Int iSize = Int( m_acListPicYuvRec[layer].size() );
1070
1071    for ( Int i = 0; i < iSize; i++ )
1072    {
1073      TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
1074      pcPicYuvRec->destroy();
1075      delete pcPicYuvRec; pcPicYuvRec = NULL;
1076    }
1077  } 
1078}
1079
1080Void TAppEncTop::xWriteRecon(UInt layer, Int iNumEncoded)
1081{
1082  Int i;
1083
1084  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_acListPicYuvRec[layer].end();
1085
1086  for ( i = 0; i < iNumEncoded; i++ )
1087  {
1088    --iterPicYuvRec;
1089  }
1090
1091  for ( i = 0; i < iNumEncoded; i++ )
1092  {
1093    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
1094    if (!m_acLayerCfg[layer].getReconFile().empty())
1095    {
1096      m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRec, m_acLayerCfg[layer].getConfLeft(), m_acLayerCfg[layer].getConfRight(), 
1097        m_acLayerCfg[layer].getConfTop(), m_acLayerCfg[layer].getConfBottom() );
1098    }
1099  }
1100}
1101
1102Void TAppEncTop::xWriteStream(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits)
1103{
1104  Int i;
1105
1106  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
1107
1108  for ( i = 0; i < iNumEncoded; i++ )
1109  {
1110    const AccessUnit& au = *(iterBitstream++);
1111    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
1112    rateStatsAccum(au, stats);
1113  }
1114}
1115
1116#else // SVC_EXTENSION
1117Void TAppEncTop::xGetBuffer( TComPicYuv*& rpcPicYuvRec)
1118{
1119  assert( m_iGOPSize > 0 );
1120
1121  // org. buffer
1122  if ( m_cListPicYuvRec.size() == (UInt)m_iGOPSize )
1123  {
1124    rpcPicYuvRec = m_cListPicYuvRec.popFront();
1125
1126  }
1127  else
1128  {
1129    rpcPicYuvRec = new TComPicYuv;
1130
1131    rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
1132
1133  }
1134  m_cListPicYuvRec.pushBack( rpcPicYuvRec );
1135}
1136
1137Void TAppEncTop::xDeleteBuffer( )
1138{
1139  TComList<TComPicYuv*>::iterator iterPicYuvRec  = m_cListPicYuvRec.begin();
1140
1141  Int iSize = Int( m_cListPicYuvRec.size() );
1142
1143  for ( Int i = 0; i < iSize; i++ )
1144  {
1145    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
1146    pcPicYuvRec->destroy();
1147    delete pcPicYuvRec; pcPicYuvRec = NULL;
1148  }
1149
1150}
1151
1152/** \param iNumEncoded  number of encoded frames
1153*/
1154Void TAppEncTop::xWriteOutput(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits)
1155{
1156  Int i;
1157
1158  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
1159  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
1160
1161  for ( i = 0; i < iNumEncoded; i++ )
1162  {
1163    --iterPicYuvRec;
1164  }
1165
1166  for ( i = 0; i < iNumEncoded; i++ )
1167  {
1168    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
1169    if (m_pchReconFile)
1170    {
1171      m_cTVideoIOYuvReconFile.write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
1172    }
1173
1174    const AccessUnit& au = *(iterBitstream++);
1175    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
1176    rateStatsAccum(au, stats);
1177  }
1178}
1179#endif
1180
1181/**
1182*
1183*/
1184void TAppEncTop::rateStatsAccum(const AccessUnit& au, const std::vector<UInt>& annexBsizes)
1185{
1186  AccessUnit::const_iterator it_au = au.begin();
1187  vector<UInt>::const_iterator it_stats = annexBsizes.begin();
1188
1189  for (; it_au != au.end(); it_au++, it_stats++)
1190  {
1191    switch ((*it_au)->m_nalUnitType)
1192    {
1193    case NAL_UNIT_CODED_SLICE_TRAIL_R:
1194    case NAL_UNIT_CODED_SLICE_TRAIL_N:
1195    case NAL_UNIT_CODED_SLICE_TLA:
1196    case NAL_UNIT_CODED_SLICE_TSA_N:
1197    case NAL_UNIT_CODED_SLICE_STSA_R:
1198    case NAL_UNIT_CODED_SLICE_STSA_N:
1199    case NAL_UNIT_CODED_SLICE_BLA:
1200    case NAL_UNIT_CODED_SLICE_BLANT:
1201    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
1202    case NAL_UNIT_CODED_SLICE_IDR:
1203    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
1204    case NAL_UNIT_CODED_SLICE_CRA:
1205    case NAL_UNIT_CODED_SLICE_RADL_N:
1206    case NAL_UNIT_CODED_SLICE_DLP:
1207    case NAL_UNIT_CODED_SLICE_RASL_N:
1208    case NAL_UNIT_CODED_SLICE_TFD:
1209    case NAL_UNIT_VPS:
1210    case NAL_UNIT_SPS:
1211    case NAL_UNIT_PPS:
1212      m_essentialBytes += *it_stats;
1213      break;
1214    default:
1215      break;
1216    }
1217
1218    m_totalBytes += *it_stats;
1219  }
1220}
1221
1222void TAppEncTop::printRateSummary()
1223{
1224#if SVC_EXTENSION
1225  Double time = (Double) m_iFrameRcvd / m_acLayerCfg[m_numLayers-1].getFrameRate();
1226#else
1227  Double time = (Double) m_iFrameRcvd / m_iFrameRate;
1228#endif
1229  printf("Bytes written to file: %u (%.3f kbps)\n", m_totalBytes, 0.008 * m_totalBytes / time);
1230#if VERBOSE_RATE
1231  printf("Bytes for SPS/PPS/Slice (Incl. Annex B): %u (%.3f kbps)\n", m_essentialBytes, 0.008 * m_essentialBytes / time);
1232#endif
1233}
1234
1235//! \}
Note: See TracBrowser for help on using the repository browser.