source: 3DVCSoftware/branches/HTM-DEV-0.1-dev/source/App/TAppEncoder/TAppEncTop.cpp @ 367

Last change on this file since 367 was 367, checked in by tech, 12 years ago

Further minor cleanups.

  • Property svn:eol-style set to native
File size: 24.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}
63
64TAppEncTop::~TAppEncTop()
65{
66}
67
68Void TAppEncTop::xInitLibCfg()
69{
70  TComVPS vps;
71 
72  vps.setMaxTLayers                       ( m_maxTempLayer );
73  if (m_maxTempLayer == 1)
74  {
75    vps.setTemporalNestingFlag(true);
76  }
77  vps.setMaxLayers                        ( 1 );
78  for(Int i = 0; i < MAX_TLAYER; i++)
79  {
80    vps.setNumReorderPics                 ( m_numReorderPics[i], i );
81    vps.setMaxDecPicBuffering             ( m_maxDecPicBuffering[i], i );
82  }
83  m_cTEncTop.setVPS(&vps);
84
85  m_cTEncTop.setProfile(m_profile);
86  m_cTEncTop.setLevel(m_levelTier, m_level);
87#if L0046_CONSTRAINT_FLAGS
88  m_cTEncTop.setProgressiveSourceFlag(m_progressiveSourceFlag);
89  m_cTEncTop.setInterlacedSourceFlag(m_interlacedSourceFlag);
90  m_cTEncTop.setNonPackedConstraintFlag(m_nonPackedConstraintFlag);
91  m_cTEncTop.setFrameOnlyConstraintFlag(m_frameOnlyConstraintFlag);
92#endif
93 
94  m_cTEncTop.setFrameRate                    ( m_iFrameRate );
95  m_cTEncTop.setFrameSkip                    ( m_FrameSkip );
96  m_cTEncTop.setSourceWidth                  ( m_iSourceWidth );
97  m_cTEncTop.setSourceHeight                 ( m_iSourceHeight );
98  m_cTEncTop.setConformanceWindow            ( m_confLeft, m_confRight, m_confTop, m_confBottom );
99  m_cTEncTop.setFramesToBeEncoded            ( m_framesToBeEncoded );
100 
101  //====== Coding Structure ========
102  m_cTEncTop.setIntraPeriod                  ( m_iIntraPeriod );
103  m_cTEncTop.setDecodingRefreshType          ( m_iDecodingRefreshType );
104  m_cTEncTop.setGOPSize                      ( m_iGOPSize );
105  m_cTEncTop.setGopList                      ( m_GOPList );
106  m_cTEncTop.setExtraRPSs                    ( m_extraRPSs );
107  for(Int i = 0; i < MAX_TLAYER; i++)
108  {
109    m_cTEncTop.setNumReorderPics             ( m_numReorderPics[i], i );
110    m_cTEncTop.setMaxDecPicBuffering         ( m_maxDecPicBuffering[i], i );
111  }
112  for( UInt uiLoop = 0; uiLoop < MAX_TLAYER; ++uiLoop )
113  {
114    m_cTEncTop.setLambdaModifier( uiLoop, m_adLambdaModifier[ uiLoop ] );
115  }
116  m_cTEncTop.setQP                           ( m_iQP );
117
118  m_cTEncTop.setPad                          ( m_aiPad );
119
120  m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayer );
121  m_cTEncTop.setUseAMP( m_enableAMP );
122 
123  //===== Slice ========
124 
125  //====== Loop/Deblock Filter ========
126  m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable       );
127  m_cTEncTop.setLoopFilterOffsetInPPS        ( m_loopFilterOffsetInPPS );
128  m_cTEncTop.setLoopFilterBetaOffset         ( m_loopFilterBetaOffsetDiv2  );
129  m_cTEncTop.setLoopFilterTcOffset           ( m_loopFilterTcOffsetDiv2    );
130  m_cTEncTop.setDeblockingFilterControlPresent( m_DeblockingFilterControlPresent);
131#if L0386_DB_METRIC
132  m_cTEncTop.setDeblockingFilterMetric       ( m_DeblockingFilterMetric );
133#endif
134
135  //====== Motion search ========
136  m_cTEncTop.setFastSearch                   ( m_iFastSearch  );
137  m_cTEncTop.setSearchRange                  ( m_iSearchRange );
138  m_cTEncTop.setBipredSearchRange            ( m_bipredSearchRange );
139
140  //====== Quality control ========
141  m_cTEncTop.setMaxDeltaQP                   ( m_iMaxDeltaQP  );
142  m_cTEncTop.setMaxCuDQPDepth                ( m_iMaxCuDQPDepth  );
143
144  m_cTEncTop.setChromaCbQpOffset               ( m_cbQpOffset     );
145  m_cTEncTop.setChromaCrQpOffset            ( m_crQpOffset  );
146
147#if ADAPTIVE_QP_SELECTION
148  m_cTEncTop.setUseAdaptQpSelect             ( m_bUseAdaptQpSelect   );
149#endif
150
151  Int lowestQP;
152  lowestQP =  - 6*(g_bitDepthY - 8); // XXX: check
153  if ((m_iMaxDeltaQP == 0 ) && (m_iQP == lowestQP) && (m_useLossless == true))
154  {
155    m_bUseAdaptiveQP = false;
156  }
157  m_cTEncTop.setUseAdaptiveQP                ( m_bUseAdaptiveQP  );
158  m_cTEncTop.setQPAdaptationRange            ( m_iQPAdaptationRange );
159 
160  //====== Tool list ========
161  m_cTEncTop.setUseSBACRD                    ( m_bUseSBACRD   );
162  m_cTEncTop.setDeltaQpRD                    ( m_uiDeltaQpRD  );
163  m_cTEncTop.setUseASR                       ( m_bUseASR      );
164  m_cTEncTop.setUseHADME                     ( m_bUseHADME    );
165  m_cTEncTop.setUseLossless                  ( m_useLossless );
166#if !L0034_COMBINED_LIST_CLEANUP
167  m_cTEncTop.setUseLComb                     ( m_bUseLComb    );
168#endif
169  m_cTEncTop.setdQPs                         ( m_aidQP        );
170  m_cTEncTop.setUseRDOQ                      ( m_useRDOQ     );
171  m_cTEncTop.setUseRDOQTS                    ( m_useRDOQTS   );
172#if L0232_RD_PENALTY
173  m_cTEncTop.setRDpenalty                 ( m_rdPenalty );
174#endif
175  m_cTEncTop.setQuadtreeTULog2MaxSize        ( m_uiQuadtreeTULog2MaxSize );
176  m_cTEncTop.setQuadtreeTULog2MinSize        ( m_uiQuadtreeTULog2MinSize );
177  m_cTEncTop.setQuadtreeTUMaxDepthInter      ( m_uiQuadtreeTUMaxDepthInter );
178  m_cTEncTop.setQuadtreeTUMaxDepthIntra      ( m_uiQuadtreeTUMaxDepthIntra );
179  m_cTEncTop.setUseFastEnc                   ( m_bUseFastEnc  );
180  m_cTEncTop.setUseEarlyCU                   ( m_bUseEarlyCU  ); 
181  m_cTEncTop.setUseFastDecisionForMerge      ( m_useFastDecisionForMerge  );
182  m_cTEncTop.setUseCbfFastMode            ( m_bUseCbfFastMode  );
183  m_cTEncTop.setUseEarlySkipDetection            ( m_useEarlySkipDetection );
184
185  m_cTEncTop.setUseTransformSkip             ( m_useTransformSkip      );
186  m_cTEncTop.setUseTransformSkipFast         ( m_useTransformSkipFast  );
187  m_cTEncTop.setUseConstrainedIntraPred      ( m_bUseConstrainedIntraPred );
188  m_cTEncTop.setPCMLog2MinSize          ( m_uiPCMLog2MinSize);
189  m_cTEncTop.setUsePCM                       ( m_usePCM );
190  m_cTEncTop.setPCMLog2MaxSize               ( m_pcmLog2MaxSize);
191  m_cTEncTop.setMaxNumMergeCand              ( m_maxNumMergeCand );
192 
193
194  //====== Weighted Prediction ========
195  m_cTEncTop.setUseWP                   ( m_useWeightedPred      );
196  m_cTEncTop.setWPBiPred                ( m_useWeightedBiPred   );
197  //====== Parallel Merge Estimation ========
198  m_cTEncTop.setLog2ParallelMergeLevelMinus2 ( m_log2ParallelMergeLevel - 2 );
199
200  //====== Slice ========
201  m_cTEncTop.setSliceMode               ( m_sliceMode                );
202  m_cTEncTop.setSliceArgument           ( m_sliceArgument            );
203
204  //====== Dependent Slice ========
205  m_cTEncTop.setSliceSegmentMode        ( m_sliceSegmentMode         );
206  m_cTEncTop.setSliceSegmentArgument    ( m_sliceSegmentArgument     );
207  Int iNumPartInCU = 1<<(m_uiMaxCUDepth<<1);
208  if(m_sliceSegmentMode==FIXED_NUMBER_OF_LCU)
209  {
210    m_cTEncTop.setSliceSegmentArgument ( m_sliceSegmentArgument * iNumPartInCU );
211  }
212  if(m_sliceMode==FIXED_NUMBER_OF_LCU)
213  {
214    m_cTEncTop.setSliceArgument ( m_sliceArgument * iNumPartInCU );
215  }
216  if(m_sliceMode==FIXED_NUMBER_OF_TILES)
217  {
218    m_cTEncTop.setSliceArgument ( m_sliceArgument );
219  }
220 
221  if(m_sliceMode == 0 )
222  {
223    m_bLFCrossSliceBoundaryFlag = true;
224  }
225  m_cTEncTop.setLFCrossSliceBoundaryFlag( m_bLFCrossSliceBoundaryFlag );
226  m_cTEncTop.setUseSAO ( m_bUseSAO );
227  m_cTEncTop.setMaxNumOffsetsPerPic (m_maxNumOffsetsPerPic);
228
229  m_cTEncTop.setSaoLcuBoundary (m_saoLcuBoundary);
230  m_cTEncTop.setSaoLcuBasedOptimization (m_saoLcuBasedOptimization);
231  m_cTEncTop.setPCMInputBitDepthFlag  ( m_bPCMInputBitDepthFlag); 
232  m_cTEncTop.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag); 
233
234  m_cTEncTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
235  m_cTEncTop.setRecoveryPointSEIEnabled( m_recoveryPointSEIEnabled );
236  m_cTEncTop.setBufferingPeriodSEIEnabled( m_bufferingPeriodSEIEnabled );
237  m_cTEncTop.setPictureTimingSEIEnabled( m_pictureTimingSEIEnabled );
238#if J0149_TONE_MAPPING_SEI
239  m_cTEncTop.setToneMappingInfoSEIEnabled                 ( m_toneMappingInfoSEIEnabled );
240  m_cTEncTop.setTMISEIToneMapId                           ( m_toneMapId );
241  m_cTEncTop.setTMISEIToneMapCancelFlag                   ( m_toneMapCancelFlag );
242  m_cTEncTop.setTMISEIToneMapPersistenceFlag              ( m_toneMapPersistenceFlag );
243  m_cTEncTop.setTMISEICodedDataBitDepth                   ( m_toneMapCodedDataBitDepth );
244  m_cTEncTop.setTMISEITargetBitDepth                      ( m_toneMapTargetBitDepth );
245  m_cTEncTop.setTMISEIModelID                             ( m_toneMapModelId );
246  m_cTEncTop.setTMISEIMinValue                            ( m_toneMapMinValue );
247  m_cTEncTop.setTMISEIMaxValue                            ( m_toneMapMaxValue );
248  m_cTEncTop.setTMISEISigmoidMidpoint                     ( m_sigmoidMidpoint );
249  m_cTEncTop.setTMISEISigmoidWidth                        ( m_sigmoidWidth );
250  m_cTEncTop.setTMISEIStartOfCodedInterva                 ( m_startOfCodedInterval );
251  m_cTEncTop.setTMISEINumPivots                           ( m_numPivots );
252  m_cTEncTop.setTMISEICodedPivotValue                     ( m_codedPivotValue );
253  m_cTEncTop.setTMISEITargetPivotValue                    ( m_targetPivotValue );
254  m_cTEncTop.setTMISEICameraIsoSpeedIdc                   ( m_cameraIsoSpeedIdc );
255  m_cTEncTop.setTMISEICameraIsoSpeedValue                 ( m_cameraIsoSpeedValue );
256  m_cTEncTop.setTMISEIExposureCompensationValueSignFlag   ( m_exposureCompensationValueSignFlag );
257  m_cTEncTop.setTMISEIExposureCompensationValueNumerator  ( m_exposureCompensationValueNumerator );
258  m_cTEncTop.setTMISEIExposureCompensationValueDenomIdc   ( m_exposureCompensationValueDenomIdc );
259  m_cTEncTop.setTMISEIRefScreenLuminanceWhite             ( m_refScreenLuminanceWhite );
260  m_cTEncTop.setTMISEIExtendedRangeWhiteLevel             ( m_extendedRangeWhiteLevel );
261  m_cTEncTop.setTMISEINominalBlackLevelLumaCodeValue      ( m_nominalBlackLevelLumaCodeValue );
262  m_cTEncTop.setTMISEINominalWhiteLevelLumaCodeValue      ( m_nominalWhiteLevelLumaCodeValue );
263  m_cTEncTop.setTMISEIExtendedWhiteLevelLumaCodeValue     ( m_extendedWhiteLevelLumaCodeValue );
264#endif
265  m_cTEncTop.setFramePackingArrangementSEIEnabled( m_framePackingSEIEnabled );
266  m_cTEncTop.setFramePackingArrangementSEIType( m_framePackingSEIType );
267  m_cTEncTop.setFramePackingArrangementSEIId( m_framePackingSEIId );
268  m_cTEncTop.setFramePackingArrangementSEIQuincunx( m_framePackingSEIQuincunx );
269  m_cTEncTop.setFramePackingArrangementSEIInterpretation( m_framePackingSEIInterpretation );
270  m_cTEncTop.setDisplayOrientationSEIAngle( m_displayOrientationSEIAngle );
271  m_cTEncTop.setTemporalLevel0IndexSEIEnabled( m_temporalLevel0IndexSEIEnabled );
272  m_cTEncTop.setGradualDecodingRefreshInfoEnabled( m_gradualDecodingRefreshInfoEnabled );
273  m_cTEncTop.setDecodingUnitInfoSEIEnabled( m_decodingUnitInfoSEIEnabled );
274#if L0208_SOP_DESCRIPTION_SEI
275  m_cTEncTop.setSOPDescriptionSEIEnabled( m_SOPDescriptionSEIEnabled );
276#endif
277#if K0180_SCALABLE_NESTING_SEI
278  m_cTEncTop.setScalableNestingSEIEnabled( m_scalableNestingSEIEnabled );
279#endif
280  m_cTEncTop.setUniformSpacingIdr          ( m_iUniformSpacingIdr );
281  m_cTEncTop.setNumColumnsMinus1           ( m_iNumColumnsMinus1 );
282  m_cTEncTop.setNumRowsMinus1              ( m_iNumRowsMinus1 );
283  if(m_iUniformSpacingIdr==0)
284  {
285    m_cTEncTop.setColumnWidth              ( m_pColumnWidth );
286    m_cTEncTop.setRowHeight                ( m_pRowHeight );
287  }
288  m_cTEncTop.xCheckGSParameters();
289  Int uiTilesCount          = (m_iNumRowsMinus1+1) * (m_iNumColumnsMinus1+1);
290  if(uiTilesCount == 1)
291  {
292    m_bLFCrossTileBoundaryFlag = true; 
293  }
294  m_cTEncTop.setLFCrossTileBoundaryFlag( m_bLFCrossTileBoundaryFlag );
295  m_cTEncTop.setWaveFrontSynchro           ( m_iWaveFrontSynchro );
296  m_cTEncTop.setWaveFrontSubstreams        ( m_iWaveFrontSubstreams );
297  m_cTEncTop.setTMVPModeId ( m_TMVPModeId );
298  m_cTEncTop.setUseScalingListId           ( m_useScalingListId  );
299  m_cTEncTop.setScalingListFile            ( m_scalingListFile   );
300  m_cTEncTop.setSignHideFlag(m_signHideFlag);
301#if RATE_CONTROL_LAMBDA_DOMAIN
302  m_cTEncTop.setUseRateCtrl         ( m_RCEnableRateControl );
303  m_cTEncTop.setTargetBitrate       ( m_RCTargetBitrate );
304  m_cTEncTop.setKeepHierBit         ( m_RCKeepHierarchicalBit );
305  m_cTEncTop.setLCULevelRC          ( m_RCLCULevelRC );
306  m_cTEncTop.setUseLCUSeparateModel ( m_RCUseLCUSeparateModel );
307  m_cTEncTop.setInitialQP           ( m_RCInitialQP );
308  m_cTEncTop.setForceIntraQP        ( m_RCForceIntraQP );
309#else
310  m_cTEncTop.setUseRateCtrl     ( m_enableRateCtrl);
311  m_cTEncTop.setTargetBitrate   ( m_targetBitrate);
312  m_cTEncTop.setNumLCUInUnit    ( m_numLCUInUnit);
313#endif
314  m_cTEncTop.setTransquantBypassEnableFlag(m_TransquantBypassEnableFlag);
315  m_cTEncTop.setCUTransquantBypassFlagValue(m_CUTransquantBypassFlagValue);
316  m_cTEncTop.setUseRecalculateQPAccordingToLambda( m_recalculateQPAccordingToLambda );
317  m_cTEncTop.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
318  m_cTEncTop.setActiveParameterSetsSEIEnabled ( m_activeParameterSetsSEIEnabled ); 
319  m_cTEncTop.setVuiParametersPresentFlag( m_vuiParametersPresentFlag );
320  m_cTEncTop.setAspectRatioIdc( m_aspectRatioIdc );
321  m_cTEncTop.setSarWidth( m_sarWidth );
322  m_cTEncTop.setSarHeight( m_sarHeight );
323  m_cTEncTop.setOverscanInfoPresentFlag( m_overscanInfoPresentFlag );
324  m_cTEncTop.setOverscanAppropriateFlag( m_overscanAppropriateFlag );
325  m_cTEncTop.setVideoSignalTypePresentFlag( m_videoSignalTypePresentFlag );
326  m_cTEncTop.setVideoFormat( m_videoFormat );
327  m_cTEncTop.setVideoFullRangeFlag( m_videoFullRangeFlag );
328  m_cTEncTop.setColourDescriptionPresentFlag( m_colourDescriptionPresentFlag );
329  m_cTEncTop.setColourPrimaries( m_colourPrimaries );
330  m_cTEncTop.setTransferCharacteristics( m_transferCharacteristics );
331  m_cTEncTop.setMatrixCoefficients( m_matrixCoefficients );
332  m_cTEncTop.setChromaLocInfoPresentFlag( m_chromaLocInfoPresentFlag );
333  m_cTEncTop.setChromaSampleLocTypeTopField( m_chromaSampleLocTypeTopField );
334  m_cTEncTop.setChromaSampleLocTypeBottomField( m_chromaSampleLocTypeBottomField );
335  m_cTEncTop.setNeutralChromaIndicationFlag( m_neutralChromaIndicationFlag );
336  m_cTEncTop.setDefaultDisplayWindow( m_defDispWinLeftOffset, m_defDispWinRightOffset, m_defDispWinTopOffset, m_defDispWinBottomOffset );
337  m_cTEncTop.setFrameFieldInfoPresentFlag( m_frameFieldInfoPresentFlag );
338  m_cTEncTop.setPocProportionalToTimingFlag( m_pocProportionalToTimingFlag );
339  m_cTEncTop.setNumTicksPocDiffOneMinus1   ( m_numTicksPocDiffOneMinus1    );
340  m_cTEncTop.setBitstreamRestrictionFlag( m_bitstreamRestrictionFlag );
341  m_cTEncTop.setTilesFixedStructureFlag( m_tilesFixedStructureFlag );
342  m_cTEncTop.setMotionVectorsOverPicBoundariesFlag( m_motionVectorsOverPicBoundariesFlag );
343  m_cTEncTop.setMinSpatialSegmentationIdc( m_minSpatialSegmentationIdc );
344  m_cTEncTop.setMaxBytesPerPicDenom( m_maxBytesPerPicDenom );
345  m_cTEncTop.setMaxBitsPerMinCuDenom( m_maxBitsPerMinCuDenom );
346  m_cTEncTop.setLog2MaxMvLengthHorizontal( m_log2MaxMvLengthHorizontal );
347  m_cTEncTop.setLog2MaxMvLengthVertical( m_log2MaxMvLengthVertical );
348#if SIGNAL_BITRATE_PICRATE_IN_VPS
349  TComBitRatePicRateInfo *bitRatePicRateInfo = m_cTEncTop.getVPS()->getBitratePicrateInfo();
350  // The number of bit rate/pic rate have to equal to number of sub-layers.
351  if(m_bitRatePicRateMaxTLayers)
352  {
353    assert(m_bitRatePicRateMaxTLayers == m_cTEncTop.getVPS()->getMaxTLayers());
354  }
355  for(Int i = 0; i < m_bitRatePicRateMaxTLayers; i++)
356  {
357    bitRatePicRateInfo->setBitRateInfoPresentFlag( i, m_bitRateInfoPresentFlag[i] );
358    if( bitRatePicRateInfo->getBitRateInfoPresentFlag(i) )
359    {
360      bitRatePicRateInfo->setAvgBitRate(i, m_avgBitRate[i]);
361      bitRatePicRateInfo->setMaxBitRate(i, m_maxBitRate[i]);
362    }
363  }
364  for(Int i = 0; i < m_bitRatePicRateMaxTLayers; i++)
365  {
366    bitRatePicRateInfo->setPicRateInfoPresentFlag( i, m_picRateInfoPresentFlag[i] );
367    if( bitRatePicRateInfo->getPicRateInfoPresentFlag(i) )
368    {
369      bitRatePicRateInfo->setAvgPicRate     (i, m_avgPicRate[i]);
370      bitRatePicRateInfo->setConstantPicRateIdc(i, m_constantPicRateIdc[i]);
371    }
372  }
373#endif
374}
375
376Void TAppEncTop::xCreateLib()
377{
378  // Video I/O
379  m_cTVideoIOYuvInputFile.open( m_pchInputFile,     false, m_inputBitDepthY, m_inputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // read  mode
380  m_cTVideoIOYuvInputFile.skipFrames(m_FrameSkip, m_iSourceWidth - m_aiPad[0], m_iSourceHeight - m_aiPad[1]);
381
382  if (m_pchReconFile)
383    m_cTVideoIOYuvReconFile.open(m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, m_internalBitDepthY, m_internalBitDepthC);  // write mode
384 
385  // Neo Decoder
386  m_cTEncTop.create();
387}
388
389Void TAppEncTop::xDestroyLib()
390{
391  // Video I/O
392  m_cTVideoIOYuvInputFile.close();
393  m_cTVideoIOYuvReconFile.close();
394 
395  // Neo Decoder
396  m_cTEncTop.destroy();
397}
398
399Void TAppEncTop::xInitLib()
400{
401  m_cTEncTop.init();
402}
403
404// ====================================================================================================================
405// Public member functions
406// ====================================================================================================================
407
408/**
409 - create internal class
410 - initialize internal variable
411 - until the end of input YUV file, call encoding function in TEncTop class
412 - delete allocated buffers
413 - destroy internal class
414 .
415 */
416Void TAppEncTop::encode()
417{
418  fstream bitstreamFile(m_pchBitstreamFile, fstream::binary | fstream::out);
419  if (!bitstreamFile)
420  {
421    fprintf(stderr, "\nfailed to open bitstream file `%s' for writing\n", m_pchBitstreamFile);
422    exit(EXIT_FAILURE);
423  }
424
425  TComPicYuv*       pcPicYuvOrg = new TComPicYuv;
426  TComPicYuv*       pcPicYuvRec = NULL;
427 
428  // initialize internal class & member variables
429  xInitLibCfg();
430  xCreateLib();
431  xInitLib();
432 
433  // main encoder loop
434  Int   iNumEncoded = 0;
435  Bool  bEos = false;
436 
437  list<AccessUnit> outputAccessUnits; ///< list of access units to write out.  is populated by the encoding process
438
439  // allocate original YUV buffer
440  pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
441 
442  while ( !bEos )
443  {
444    // get buffers
445    xGetBuffer(pcPicYuvRec);
446
447    // read input YUV file
448    m_cTVideoIOYuvInputFile.read( pcPicYuvOrg, m_aiPad );
449
450    // increase number of received frames
451    m_iFrameRcvd++;
452
453    bEos = (m_iFrameRcvd == m_framesToBeEncoded);
454
455    Bool flush = 0;
456    // if end of file (which is only detected on a read failure) flush the encoder of any queued pictures
457    if (m_cTVideoIOYuvInputFile.isEof())
458    {
459      flush = true;
460      bEos = true;
461      m_iFrameRcvd--;
462      m_cTEncTop.setFramesToBeEncoded(m_iFrameRcvd);
463    }
464
465    // call encoding function for one frame
466    m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded );
467   
468    // write bistream to file if necessary
469    if ( iNumEncoded > 0 )
470    {
471      xWriteOutput(bitstreamFile, iNumEncoded, outputAccessUnits);
472      outputAccessUnits.clear();
473    }
474  }
475
476  m_cTEncTop.printSummary();
477
478  // delete original YUV buffer
479  pcPicYuvOrg->destroy();
480  delete pcPicYuvOrg;
481  pcPicYuvOrg = NULL;
482 
483  // delete used buffers in encoder class
484  m_cTEncTop.deletePicBuffer();
485
486  // delete buffers & classes
487  xDeleteBuffer();
488  xDestroyLib();
489 
490  printRateSummary();
491
492  return;
493}
494
495// ====================================================================================================================
496// Protected member functions
497// ====================================================================================================================
498
499/**
500 - application has picture buffer list with size of GOP
501 - picture buffer list acts as ring buffer
502 - end of the list has the latest picture
503 .
504 */
505Void TAppEncTop::xGetBuffer( TComPicYuv*& rpcPicYuvRec)
506{
507  assert( m_iGOPSize > 0 );
508 
509  // org. buffer
510  if ( m_cListPicYuvRec.size() == (UInt)m_iGOPSize )
511  {
512    rpcPicYuvRec = m_cListPicYuvRec.popFront();
513
514  }
515  else
516  {
517    rpcPicYuvRec = new TComPicYuv;
518   
519    rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
520
521  }
522  m_cListPicYuvRec.pushBack( rpcPicYuvRec );
523}
524
525Void TAppEncTop::xDeleteBuffer( )
526{
527  TComList<TComPicYuv*>::iterator iterPicYuvRec  = m_cListPicYuvRec.begin();
528 
529  Int iSize = Int( m_cListPicYuvRec.size() );
530
531  for ( Int i = 0; i < iSize; i++ )
532  {
533    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
534    pcPicYuvRec->destroy();
535    delete pcPicYuvRec; pcPicYuvRec = NULL;
536  }
537
538}
539
540/** \param iNumEncoded  number of encoded frames
541 */
542Void TAppEncTop::xWriteOutput(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits)
543{
544  Int i;
545 
546  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
547  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
548
549  for ( i = 0; i < iNumEncoded; i++ )
550  {
551    --iterPicYuvRec;
552  }
553 
554  for ( i = 0; i < iNumEncoded; i++ )
555  {
556    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
557    if (m_pchReconFile)
558    {
559      m_cTVideoIOYuvReconFile.write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
560    }
561
562    const AccessUnit& au = *(iterBitstream++);
563    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
564    rateStatsAccum(au, stats);
565  }
566}
567
568/**
569 *
570 */
571void TAppEncTop::rateStatsAccum(const AccessUnit& au, const std::vector<UInt>& annexBsizes)
572{
573  AccessUnit::const_iterator it_au = au.begin();
574  vector<UInt>::const_iterator it_stats = annexBsizes.begin();
575
576  for (; it_au != au.end(); it_au++, it_stats++)
577  {
578    switch ((*it_au)->m_nalUnitType)
579    {
580    case NAL_UNIT_CODED_SLICE_TRAIL_R:
581    case NAL_UNIT_CODED_SLICE_TRAIL_N:
582    case NAL_UNIT_CODED_SLICE_TLA_R:
583    case NAL_UNIT_CODED_SLICE_TSA_N:
584    case NAL_UNIT_CODED_SLICE_STSA_R:
585    case NAL_UNIT_CODED_SLICE_STSA_N:
586    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
587    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
588    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
589    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
590    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
591    case NAL_UNIT_CODED_SLICE_CRA:
592    case NAL_UNIT_CODED_SLICE_RADL_N:
593    case NAL_UNIT_CODED_SLICE_RADL_R:
594    case NAL_UNIT_CODED_SLICE_RASL_N:
595    case NAL_UNIT_CODED_SLICE_RASL_R:
596    case NAL_UNIT_VPS:
597    case NAL_UNIT_SPS:
598    case NAL_UNIT_PPS:
599      m_essentialBytes += *it_stats;
600      break;
601    default:
602      break;
603    }
604
605    m_totalBytes += *it_stats;
606  }
607}
608
609void TAppEncTop::printRateSummary()
610{
611  Double time = (Double) m_iFrameRcvd / m_iFrameRate;
612  printf("Bytes written to file: %u (%.3f kbps)\n", m_totalBytes, 0.008 * m_totalBytes / time);
613#if VERBOSE_RATE
614  printf("Bytes for SPS/PPS/Slice (Incl. Annex B): %u (%.3f kbps)\n", m_essentialBytes, 0.008 * m_essentialBytes / time);
615#endif
616}
617
618
619//! \}
Note: See TracBrowser for help on using the repository browser.