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

Last change on this file since 365 was 365, checked in by tech, 11 years ago

Further minor cleanups.

  • Property svn:eol-style set to native
File size: 34.0 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#if !H_MV
60  m_iFrameRcvd = 0;
61#endif
62  m_totalBytes = 0;
63  m_essentialBytes = 0;
64}
65
66TAppEncTop::~TAppEncTop()
67{
68}
69
70Void TAppEncTop::xInitLibCfg()
71{
72  TComVPS vps;
73 
74#if H_MV
75  Int maxTempLayer = -1; 
76  for (Int j = 0; j < m_numberOfLayers; j++)
77  {
78    maxTempLayer = max( m_maxTempLayerMvc[ j ], maxTempLayer ); 
79  }
80
81  vps.setMaxTLayers                       ( maxTempLayer );
82  if ( maxTempLayer )
83  {
84    vps.setTemporalNestingFlag(true);
85  }
86  vps.setMaxLayers( m_numberOfLayers );
87  for(Int i = 0; i < MAX_TLAYER; i++)
88  {
89    Int maxNumReOrderPics  = 0; 
90    Int maxDecPicBuffering = 0;
91    for (Int j = 0; j < m_numberOfLayers; j++)
92    {
93      maxNumReOrderPics  = max( maxNumReOrderPics,  m_numReorderPicsMvc    [ j ][ i ] );     
94      maxDecPicBuffering = max( maxDecPicBuffering, m_maxDecPicBufferingMvc[ j ][ i ] );     
95    }
96
97    vps.setNumReorderPics                 ( maxNumReOrderPics  ,i );
98    vps.setMaxDecPicBuffering             ( maxDecPicBuffering ,i );
99  }
100#else
101  vps.setMaxTLayers                       ( m_maxTempLayer );
102  if (m_maxTempLayer == 1)
103  {
104    vps.setTemporalNestingFlag(true);
105  }
106  vps.setMaxLayers                        ( 1 );
107  for(Int i = 0; i < MAX_TLAYER; i++)
108  {
109    vps.setNumReorderPics                 ( m_numReorderPics[i], i );
110    vps.setMaxDecPicBuffering             ( m_maxDecPicBuffering[i], i );
111  }
112#endif
113#if H_MV
114  xSetLayerIds             ( vps );   
115  xSetDimensionIdAndLength ( vps );
116  xSetDirectDependencyFlags( vps );
117
118  for(Int layer = 0; layer < m_numberOfLayers; layer++)
119  {
120    m_frameRcvd                 .push_back(0);
121    m_acTEncTopList             .push_back(new TEncTop); 
122    m_acTVideoIOYuvInputFileList.push_back(new TVideoIOYuv);
123    m_acTVideoIOYuvReconFileList.push_back(new TVideoIOYuv);
124    m_picYuvRec                 .push_back(new TComList<TComPicYuv*>) ;
125
126    m_ivPicLists.push_back( m_acTEncTopList[ layer ]->getListPic()  ); 
127    TEncTop& m_cTEncTop = *m_acTEncTopList[ layer ];  // It is not a member, but this name helps avoiding code duplication !!!
128   
129    m_cTEncTop.setLayerIdInVps( layer ); 
130    m_cTEncTop.setLayerId     ( vps.getLayerIdInNuh( layer ) );   
131    m_cTEncTop.setViewId      ( vps.getViewId      ( layer ) );
132#if H_3D
133    m_cTEncTop.setIsDepth     ( vps.getDepthId     ( layer ) != 0 );
134#endif
135    m_cTEncTop.setIvPicLists  ( &m_ivPicLists ); 
136#endif
137  m_cTEncTop.setVPS(&vps);
138
139  m_cTEncTop.setProfile(m_profile);
140  m_cTEncTop.setLevel(m_levelTier, m_level);
141#if L0046_CONSTRAINT_FLAGS
142  m_cTEncTop.setProgressiveSourceFlag(m_progressiveSourceFlag);
143  m_cTEncTop.setInterlacedSourceFlag(m_interlacedSourceFlag);
144  m_cTEncTop.setNonPackedConstraintFlag(m_nonPackedConstraintFlag);
145  m_cTEncTop.setFrameOnlyConstraintFlag(m_frameOnlyConstraintFlag);
146#endif
147 
148  m_cTEncTop.setFrameRate                    ( m_iFrameRate );
149  m_cTEncTop.setFrameSkip                    ( m_FrameSkip );
150  m_cTEncTop.setSourceWidth                  ( m_iSourceWidth );
151  m_cTEncTop.setSourceHeight                 ( m_iSourceHeight );
152  m_cTEncTop.setConformanceWindow            ( m_confLeft, m_confRight, m_confTop, m_confBottom );
153  m_cTEncTop.setFramesToBeEncoded            ( m_framesToBeEncoded );
154 
155  //====== Coding Structure ========
156  m_cTEncTop.setIntraPeriod                  ( m_iIntraPeriod );
157  m_cTEncTop.setDecodingRefreshType          ( m_iDecodingRefreshType );
158  m_cTEncTop.setGOPSize                      ( m_iGOPSize );
159#if H_MV
160  m_cTEncTop.setGopList                      ( m_GOPListMvc[layer] );
161  m_cTEncTop.setExtraRPSs                    ( m_extraRPSsMvc[layer] );
162  for(Int i = 0; i < MAX_TLAYER; i++)
163  {
164    m_cTEncTop.setNumReorderPics             ( m_numReorderPicsMvc[layer][i], i );
165    m_cTEncTop.setMaxDecPicBuffering         ( m_maxDecPicBufferingMvc[layer][i], i );
166  }
167#else
168  m_cTEncTop.setGopList                      ( m_GOPList );
169  m_cTEncTop.setExtraRPSs                    ( m_extraRPSs );
170  for(Int i = 0; i < MAX_TLAYER; i++)
171  {
172    m_cTEncTop.setNumReorderPics             ( m_numReorderPics[i], i );
173    m_cTEncTop.setMaxDecPicBuffering         ( m_maxDecPicBuffering[i], i );
174  }
175#endif
176  for( UInt uiLoop = 0; uiLoop < MAX_TLAYER; ++uiLoop )
177  {
178    m_cTEncTop.setLambdaModifier( uiLoop, m_adLambdaModifier[ uiLoop ] );
179  }
180#if H_MV
181  m_cTEncTop.setQP                           ( m_iQP[layer] );
182#else
183  m_cTEncTop.setQP                           ( m_iQP );
184#endif
185  m_cTEncTop.setPad                          ( m_aiPad );
186#if H_MV
187  m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayerMvc[layer] );
188#else
189  m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayer );
190#endif
191  m_cTEncTop.setUseAMP( m_enableAMP );
192 
193  //===== Slice ========
194 
195  //====== Loop/Deblock Filter ========
196#if H_MV
197  m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable[layer]);
198#else
199  m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable       );
200#endif
201  m_cTEncTop.setLoopFilterOffsetInPPS        ( m_loopFilterOffsetInPPS );
202  m_cTEncTop.setLoopFilterBetaOffset         ( m_loopFilterBetaOffsetDiv2  );
203  m_cTEncTop.setLoopFilterTcOffset           ( m_loopFilterTcOffsetDiv2    );
204  m_cTEncTop.setDeblockingFilterControlPresent( m_DeblockingFilterControlPresent);
205#if L0386_DB_METRIC
206  m_cTEncTop.setDeblockingFilterMetric       ( m_DeblockingFilterMetric );
207#endif
208
209  //====== Motion search ========
210  m_cTEncTop.setFastSearch                   ( m_iFastSearch  );
211  m_cTEncTop.setSearchRange                  ( m_iSearchRange );
212  m_cTEncTop.setBipredSearchRange            ( m_bipredSearchRange );
213
214  //====== Quality control ========
215  m_cTEncTop.setMaxDeltaQP                   ( m_iMaxDeltaQP  );
216  m_cTEncTop.setMaxCuDQPDepth                ( m_iMaxCuDQPDepth  );
217
218  m_cTEncTop.setChromaCbQpOffset               ( m_cbQpOffset     );
219  m_cTEncTop.setChromaCrQpOffset            ( m_crQpOffset  );
220
221#if ADAPTIVE_QP_SELECTION
222  m_cTEncTop.setUseAdaptQpSelect             ( m_bUseAdaptQpSelect   );
223#endif
224
225  Int lowestQP;
226  lowestQP =  - 6*(g_bitDepthY - 8); // XXX: check
227#if H_MV
228  if ((m_iMaxDeltaQP == 0 ) && (m_iQP[layer] == lowestQP) && (m_useLossless == true))
229#else
230  if ((m_iMaxDeltaQP == 0 ) && (m_iQP == lowestQP) && (m_useLossless == true))
231#endif
232  {
233    m_bUseAdaptiveQP = false;
234  }
235  m_cTEncTop.setUseAdaptiveQP                ( m_bUseAdaptiveQP  );
236  m_cTEncTop.setQPAdaptationRange            ( m_iQPAdaptationRange );
237 
238  //====== Tool list ========
239  m_cTEncTop.setUseSBACRD                    ( m_bUseSBACRD   );
240  m_cTEncTop.setDeltaQpRD                    ( m_uiDeltaQpRD  );
241  m_cTEncTop.setUseASR                       ( m_bUseASR      );
242  m_cTEncTop.setUseHADME                     ( m_bUseHADME    );
243  m_cTEncTop.setUseLossless                  ( m_useLossless );
244#if !L0034_COMBINED_LIST_CLEANUP
245  m_cTEncTop.setUseLComb                     ( m_bUseLComb    );
246#endif
247#if H_MV
248  m_cTEncTop.setdQPs                         ( m_aidQP[layer]   );
249#else
250  m_cTEncTop.setdQPs                         ( m_aidQP        );
251#endif
252  m_cTEncTop.setUseRDOQ                      ( m_useRDOQ     );
253  m_cTEncTop.setUseRDOQTS                    ( m_useRDOQTS   );
254#if L0232_RD_PENALTY
255  m_cTEncTop.setRDpenalty                 ( m_rdPenalty );
256#endif
257  m_cTEncTop.setQuadtreeTULog2MaxSize        ( m_uiQuadtreeTULog2MaxSize );
258  m_cTEncTop.setQuadtreeTULog2MinSize        ( m_uiQuadtreeTULog2MinSize );
259  m_cTEncTop.setQuadtreeTUMaxDepthInter      ( m_uiQuadtreeTUMaxDepthInter );
260  m_cTEncTop.setQuadtreeTUMaxDepthIntra      ( m_uiQuadtreeTUMaxDepthIntra );
261  m_cTEncTop.setUseFastEnc                   ( m_bUseFastEnc  );
262  m_cTEncTop.setUseEarlyCU                   ( m_bUseEarlyCU  ); 
263  m_cTEncTop.setUseFastDecisionForMerge      ( m_useFastDecisionForMerge  );
264  m_cTEncTop.setUseCbfFastMode            ( m_bUseCbfFastMode  );
265  m_cTEncTop.setUseEarlySkipDetection            ( m_useEarlySkipDetection );
266
267  m_cTEncTop.setUseTransformSkip             ( m_useTransformSkip      );
268  m_cTEncTop.setUseTransformSkipFast         ( m_useTransformSkipFast  );
269  m_cTEncTop.setUseConstrainedIntraPred      ( m_bUseConstrainedIntraPred );
270  m_cTEncTop.setPCMLog2MinSize          ( m_uiPCMLog2MinSize);
271  m_cTEncTop.setUsePCM                       ( m_usePCM );
272  m_cTEncTop.setPCMLog2MaxSize               ( m_pcmLog2MaxSize);
273  m_cTEncTop.setMaxNumMergeCand              ( m_maxNumMergeCand );
274 
275
276  //====== Weighted Prediction ========
277  m_cTEncTop.setUseWP                   ( m_useWeightedPred      );
278  m_cTEncTop.setWPBiPred                ( m_useWeightedBiPred   );
279  //====== Parallel Merge Estimation ========
280  m_cTEncTop.setLog2ParallelMergeLevelMinus2 ( m_log2ParallelMergeLevel - 2 );
281
282  //====== Slice ========
283  m_cTEncTop.setSliceMode               ( m_sliceMode                );
284  m_cTEncTop.setSliceArgument           ( m_sliceArgument            );
285
286  //====== Dependent Slice ========
287  m_cTEncTop.setSliceSegmentMode        ( m_sliceSegmentMode         );
288  m_cTEncTop.setSliceSegmentArgument    ( m_sliceSegmentArgument     );
289  Int iNumPartInCU = 1<<(m_uiMaxCUDepth<<1);
290  if(m_sliceSegmentMode==FIXED_NUMBER_OF_LCU)
291  {
292    m_cTEncTop.setSliceSegmentArgument ( m_sliceSegmentArgument * iNumPartInCU );
293  }
294  if(m_sliceMode==FIXED_NUMBER_OF_LCU)
295  {
296    m_cTEncTop.setSliceArgument ( m_sliceArgument * iNumPartInCU );
297  }
298  if(m_sliceMode==FIXED_NUMBER_OF_TILES)
299  {
300    m_cTEncTop.setSliceArgument ( m_sliceArgument );
301  }
302 
303  if(m_sliceMode == 0 )
304  {
305    m_bLFCrossSliceBoundaryFlag = true;
306  }
307  m_cTEncTop.setLFCrossSliceBoundaryFlag( m_bLFCrossSliceBoundaryFlag );
308#if H_MV
309  m_cTEncTop.setUseSAO ( m_bUseSAO[layer] );
310#else
311  m_cTEncTop.setUseSAO ( m_bUseSAO );
312#endif
313  m_cTEncTop.setMaxNumOffsetsPerPic (m_maxNumOffsetsPerPic);
314
315  m_cTEncTop.setSaoLcuBoundary (m_saoLcuBoundary);
316  m_cTEncTop.setSaoLcuBasedOptimization (m_saoLcuBasedOptimization);
317  m_cTEncTop.setPCMInputBitDepthFlag  ( m_bPCMInputBitDepthFlag); 
318  m_cTEncTop.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag); 
319
320  m_cTEncTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
321  m_cTEncTop.setRecoveryPointSEIEnabled( m_recoveryPointSEIEnabled );
322  m_cTEncTop.setBufferingPeriodSEIEnabled( m_bufferingPeriodSEIEnabled );
323  m_cTEncTop.setPictureTimingSEIEnabled( m_pictureTimingSEIEnabled );
324#if J0149_TONE_MAPPING_SEI
325  m_cTEncTop.setToneMappingInfoSEIEnabled                 ( m_toneMappingInfoSEIEnabled );
326  m_cTEncTop.setTMISEIToneMapId                           ( m_toneMapId );
327  m_cTEncTop.setTMISEIToneMapCancelFlag                   ( m_toneMapCancelFlag );
328  m_cTEncTop.setTMISEIToneMapPersistenceFlag              ( m_toneMapPersistenceFlag );
329  m_cTEncTop.setTMISEICodedDataBitDepth                   ( m_toneMapCodedDataBitDepth );
330  m_cTEncTop.setTMISEITargetBitDepth                      ( m_toneMapTargetBitDepth );
331  m_cTEncTop.setTMISEIModelID                             ( m_toneMapModelId );
332  m_cTEncTop.setTMISEIMinValue                            ( m_toneMapMinValue );
333  m_cTEncTop.setTMISEIMaxValue                            ( m_toneMapMaxValue );
334  m_cTEncTop.setTMISEISigmoidMidpoint                     ( m_sigmoidMidpoint );
335  m_cTEncTop.setTMISEISigmoidWidth                        ( m_sigmoidWidth );
336  m_cTEncTop.setTMISEIStartOfCodedInterva                 ( m_startOfCodedInterval );
337  m_cTEncTop.setTMISEINumPivots                           ( m_numPivots );
338  m_cTEncTop.setTMISEICodedPivotValue                     ( m_codedPivotValue );
339  m_cTEncTop.setTMISEITargetPivotValue                    ( m_targetPivotValue );
340  m_cTEncTop.setTMISEICameraIsoSpeedIdc                   ( m_cameraIsoSpeedIdc );
341  m_cTEncTop.setTMISEICameraIsoSpeedValue                 ( m_cameraIsoSpeedValue );
342  m_cTEncTop.setTMISEIExposureCompensationValueSignFlag   ( m_exposureCompensationValueSignFlag );
343  m_cTEncTop.setTMISEIExposureCompensationValueNumerator  ( m_exposureCompensationValueNumerator );
344  m_cTEncTop.setTMISEIExposureCompensationValueDenomIdc   ( m_exposureCompensationValueDenomIdc );
345  m_cTEncTop.setTMISEIRefScreenLuminanceWhite             ( m_refScreenLuminanceWhite );
346  m_cTEncTop.setTMISEIExtendedRangeWhiteLevel             ( m_extendedRangeWhiteLevel );
347  m_cTEncTop.setTMISEINominalBlackLevelLumaCodeValue      ( m_nominalBlackLevelLumaCodeValue );
348  m_cTEncTop.setTMISEINominalWhiteLevelLumaCodeValue      ( m_nominalWhiteLevelLumaCodeValue );
349  m_cTEncTop.setTMISEIExtendedWhiteLevelLumaCodeValue     ( m_extendedWhiteLevelLumaCodeValue );
350#endif
351  m_cTEncTop.setFramePackingArrangementSEIEnabled( m_framePackingSEIEnabled );
352  m_cTEncTop.setFramePackingArrangementSEIType( m_framePackingSEIType );
353  m_cTEncTop.setFramePackingArrangementSEIId( m_framePackingSEIId );
354  m_cTEncTop.setFramePackingArrangementSEIQuincunx( m_framePackingSEIQuincunx );
355  m_cTEncTop.setFramePackingArrangementSEIInterpretation( m_framePackingSEIInterpretation );
356  m_cTEncTop.setDisplayOrientationSEIAngle( m_displayOrientationSEIAngle );
357  m_cTEncTop.setTemporalLevel0IndexSEIEnabled( m_temporalLevel0IndexSEIEnabled );
358  m_cTEncTop.setGradualDecodingRefreshInfoEnabled( m_gradualDecodingRefreshInfoEnabled );
359  m_cTEncTop.setDecodingUnitInfoSEIEnabled( m_decodingUnitInfoSEIEnabled );
360#if L0208_SOP_DESCRIPTION_SEI
361  m_cTEncTop.setSOPDescriptionSEIEnabled( m_SOPDescriptionSEIEnabled );
362#endif
363#if K0180_SCALABLE_NESTING_SEI
364  m_cTEncTop.setScalableNestingSEIEnabled( m_scalableNestingSEIEnabled );
365#endif
366  m_cTEncTop.setUniformSpacingIdr          ( m_iUniformSpacingIdr );
367  m_cTEncTop.setNumColumnsMinus1           ( m_iNumColumnsMinus1 );
368  m_cTEncTop.setNumRowsMinus1              ( m_iNumRowsMinus1 );
369  if(m_iUniformSpacingIdr==0)
370  {
371    m_cTEncTop.setColumnWidth              ( m_pColumnWidth );
372    m_cTEncTop.setRowHeight                ( m_pRowHeight );
373  }
374  m_cTEncTop.xCheckGSParameters();
375  Int uiTilesCount          = (m_iNumRowsMinus1+1) * (m_iNumColumnsMinus1+1);
376  if(uiTilesCount == 1)
377  {
378    m_bLFCrossTileBoundaryFlag = true; 
379  }
380  m_cTEncTop.setLFCrossTileBoundaryFlag( m_bLFCrossTileBoundaryFlag );
381  m_cTEncTop.setWaveFrontSynchro           ( m_iWaveFrontSynchro );
382  m_cTEncTop.setWaveFrontSubstreams        ( m_iWaveFrontSubstreams );
383  m_cTEncTop.setTMVPModeId ( m_TMVPModeId );
384  m_cTEncTop.setUseScalingListId           ( m_useScalingListId  );
385  m_cTEncTop.setScalingListFile            ( m_scalingListFile   );
386  m_cTEncTop.setSignHideFlag(m_signHideFlag);
387#if RATE_CONTROL_LAMBDA_DOMAIN
388  m_cTEncTop.setUseRateCtrl         ( m_RCEnableRateControl );
389  m_cTEncTop.setTargetBitrate       ( m_RCTargetBitrate );
390  m_cTEncTop.setKeepHierBit         ( m_RCKeepHierarchicalBit );
391  m_cTEncTop.setLCULevelRC          ( m_RCLCULevelRC );
392  m_cTEncTop.setUseLCUSeparateModel ( m_RCUseLCUSeparateModel );
393  m_cTEncTop.setInitialQP           ( m_RCInitialQP );
394  m_cTEncTop.setForceIntraQP        ( m_RCForceIntraQP );
395#else
396  m_cTEncTop.setUseRateCtrl     ( m_enableRateCtrl);
397  m_cTEncTop.setTargetBitrate   ( m_targetBitrate);
398  m_cTEncTop.setNumLCUInUnit    ( m_numLCUInUnit);
399#endif
400  m_cTEncTop.setTransquantBypassEnableFlag(m_TransquantBypassEnableFlag);
401  m_cTEncTop.setCUTransquantBypassFlagValue(m_CUTransquantBypassFlagValue);
402  m_cTEncTop.setUseRecalculateQPAccordingToLambda( m_recalculateQPAccordingToLambda );
403  m_cTEncTop.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
404  m_cTEncTop.setActiveParameterSetsSEIEnabled ( m_activeParameterSetsSEIEnabled ); 
405  m_cTEncTop.setVuiParametersPresentFlag( m_vuiParametersPresentFlag );
406  m_cTEncTop.setAspectRatioIdc( m_aspectRatioIdc );
407  m_cTEncTop.setSarWidth( m_sarWidth );
408  m_cTEncTop.setSarHeight( m_sarHeight );
409  m_cTEncTop.setOverscanInfoPresentFlag( m_overscanInfoPresentFlag );
410  m_cTEncTop.setOverscanAppropriateFlag( m_overscanAppropriateFlag );
411  m_cTEncTop.setVideoSignalTypePresentFlag( m_videoSignalTypePresentFlag );
412  m_cTEncTop.setVideoFormat( m_videoFormat );
413  m_cTEncTop.setVideoFullRangeFlag( m_videoFullRangeFlag );
414  m_cTEncTop.setColourDescriptionPresentFlag( m_colourDescriptionPresentFlag );
415  m_cTEncTop.setColourPrimaries( m_colourPrimaries );
416  m_cTEncTop.setTransferCharacteristics( m_transferCharacteristics );
417  m_cTEncTop.setMatrixCoefficients( m_matrixCoefficients );
418  m_cTEncTop.setChromaLocInfoPresentFlag( m_chromaLocInfoPresentFlag );
419  m_cTEncTop.setChromaSampleLocTypeTopField( m_chromaSampleLocTypeTopField );
420  m_cTEncTop.setChromaSampleLocTypeBottomField( m_chromaSampleLocTypeBottomField );
421  m_cTEncTop.setNeutralChromaIndicationFlag( m_neutralChromaIndicationFlag );
422  m_cTEncTop.setDefaultDisplayWindow( m_defDispWinLeftOffset, m_defDispWinRightOffset, m_defDispWinTopOffset, m_defDispWinBottomOffset );
423  m_cTEncTop.setFrameFieldInfoPresentFlag( m_frameFieldInfoPresentFlag );
424  m_cTEncTop.setPocProportionalToTimingFlag( m_pocProportionalToTimingFlag );
425  m_cTEncTop.setNumTicksPocDiffOneMinus1   ( m_numTicksPocDiffOneMinus1    );
426  m_cTEncTop.setBitstreamRestrictionFlag( m_bitstreamRestrictionFlag );
427  m_cTEncTop.setTilesFixedStructureFlag( m_tilesFixedStructureFlag );
428  m_cTEncTop.setMotionVectorsOverPicBoundariesFlag( m_motionVectorsOverPicBoundariesFlag );
429  m_cTEncTop.setMinSpatialSegmentationIdc( m_minSpatialSegmentationIdc );
430  m_cTEncTop.setMaxBytesPerPicDenom( m_maxBytesPerPicDenom );
431  m_cTEncTop.setMaxBitsPerMinCuDenom( m_maxBitsPerMinCuDenom );
432  m_cTEncTop.setLog2MaxMvLengthHorizontal( m_log2MaxMvLengthHorizontal );
433  m_cTEncTop.setLog2MaxMvLengthVertical( m_log2MaxMvLengthVertical );
434#if SIGNAL_BITRATE_PICRATE_IN_VPS
435  TComBitRatePicRateInfo *bitRatePicRateInfo = m_cTEncTop.getVPS()->getBitratePicrateInfo();
436  // The number of bit rate/pic rate have to equal to number of sub-layers.
437  if(m_bitRatePicRateMaxTLayers)
438  {
439    assert(m_bitRatePicRateMaxTLayers == m_cTEncTop.getVPS()->getMaxTLayers());
440  }
441  for(Int i = 0; i < m_bitRatePicRateMaxTLayers; i++)
442  {
443    bitRatePicRateInfo->setBitRateInfoPresentFlag( i, m_bitRateInfoPresentFlag[i] );
444    if( bitRatePicRateInfo->getBitRateInfoPresentFlag(i) )
445    {
446      bitRatePicRateInfo->setAvgBitRate(i, m_avgBitRate[i]);
447      bitRatePicRateInfo->setMaxBitRate(i, m_maxBitRate[i]);
448    }
449  }
450  for(Int i = 0; i < m_bitRatePicRateMaxTLayers; i++)
451  {
452    bitRatePicRateInfo->setPicRateInfoPresentFlag( i, m_picRateInfoPresentFlag[i] );
453    if( bitRatePicRateInfo->getPicRateInfoPresentFlag(i) )
454    {
455      bitRatePicRateInfo->setAvgPicRate     (i, m_avgPicRate[i]);
456      bitRatePicRateInfo->setConstantPicRateIdc(i, m_constantPicRateIdc[i]);
457    }
458  }
459#endif
460#if H_MV
461  }
462#endif
463}
464
465Void TAppEncTop::xCreateLib()
466{
467#if H_MV
468  // initialize global variables
469  initROM();
470
471  for( Int layer=0; layer < m_numberOfLayers; layer++)
472  {
473    m_acTVideoIOYuvInputFileList[layer]->open( m_pchInputFileList[layer],     false, m_inputBitDepthY, m_inputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // read  mode
474    m_acTVideoIOYuvInputFileList[layer]->skipFrames( m_FrameSkip, m_iSourceWidth - m_aiPad[0], m_iSourceHeight - m_aiPad[1]);
475
476    if (m_pchReconFileList[layer])
477    {
478      m_acTVideoIOYuvReconFileList[layer]->open( m_pchReconFileList[layer], true, m_outputBitDepthY, m_outputBitDepthC, m_internalBitDepthY, m_internalBitDepthC);  // write mode
479    }
480    m_acTEncTopList[layer]->create();
481  }
482#else
483  // Video I/O
484  m_cTVideoIOYuvInputFile.open( m_pchInputFile,     false, m_inputBitDepthY, m_inputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // read  mode
485  m_cTVideoIOYuvInputFile.skipFrames(m_FrameSkip, m_iSourceWidth - m_aiPad[0], m_iSourceHeight - m_aiPad[1]);
486
487  if (m_pchReconFile)
488    m_cTVideoIOYuvReconFile.open(m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, m_internalBitDepthY, m_internalBitDepthC);  // write mode
489 
490  // Neo Decoder
491  m_cTEncTop.create();
492#endif
493}
494
495Void TAppEncTop::xDestroyLib()
496{
497#if H_MV
498  // destroy ROM
499  destroyROM();
500
501  for(Int layer=0; layer<m_numberOfLayers; layer++)
502  {
503    m_acTVideoIOYuvInputFileList[layer]->close();
504    m_acTVideoIOYuvReconFileList[layer]->close();
505    delete m_acTVideoIOYuvInputFileList[layer] ; 
506    m_acTVideoIOYuvInputFileList[layer] = NULL;
507    delete m_acTVideoIOYuvReconFileList[layer] ; 
508    m_acTVideoIOYuvReconFileList[layer] = NULL;
509    m_acTEncTopList[layer]->deletePicBuffer();
510    m_acTEncTopList[layer]->destroy();
511    delete m_acTEncTopList[layer] ; 
512    m_acTEncTopList[layer] = NULL;
513    delete m_picYuvRec[layer] ; 
514    m_picYuvRec[layer] = NULL;
515  }
516#else
517  // Video I/O
518  m_cTVideoIOYuvInputFile.close();
519  m_cTVideoIOYuvReconFile.close();
520 
521  // Neo Decoder
522  m_cTEncTop.destroy();
523#endif
524}
525
526Void TAppEncTop::xInitLib()
527{
528#if H_MV
529  for(Int layer=0; layer<m_numberOfLayers; layer++)
530  {
531    m_acTEncTopList[layer]->init( );
532  }
533#else
534  m_cTEncTop.init();
535#endif
536}
537
538// ====================================================================================================================
539// Public member functions
540// ====================================================================================================================
541
542/**
543 - create internal class
544 - initialize internal variable
545 - until the end of input YUV file, call encoding function in TEncTop class
546 - delete allocated buffers
547 - destroy internal class
548 .
549 */
550Void TAppEncTop::encode()
551{
552  fstream bitstreamFile(m_pchBitstreamFile, fstream::binary | fstream::out);
553  if (!bitstreamFile)
554  {
555    fprintf(stderr, "\nfailed to open bitstream file `%s' for writing\n", m_pchBitstreamFile);
556    exit(EXIT_FAILURE);
557  }
558
559  TComPicYuv*       pcPicYuvOrg = new TComPicYuv;
560  TComPicYuv*       pcPicYuvRec = NULL;
561 
562  // initialize internal class & member variables
563  xInitLibCfg();
564  xCreateLib();
565  xInitLib();
566 
567  // main encoder loop
568#if H_MV
569  Bool  allEos = false;
570  std::vector<Bool>  eos ;
571  std::vector<Bool>  flush ; 
572 
573  Int gopSize    = 1;
574  Int maxGopSize = 0;
575  maxGopSize = (std::max)(maxGopSize, m_acTEncTopList[0]->getGOPSize()); 
576 
577  for(Int layer=0; layer < m_numberOfLayers; layer++ )
578  {
579    eos  .push_back( false );
580    flush.push_back( false );
581  }
582#else
583  Int   iNumEncoded = 0;
584  Bool  bEos = false;
585#endif
586 
587  list<AccessUnit> outputAccessUnits; ///< list of access units to write out.  is populated by the encoding process
588
589  // allocate original YUV buffer
590  pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
591 
592#if H_MV
593  while ( !allEos )
594  {
595    for(Int layer=0; layer < m_numberOfLayers; layer++ )
596    {
597      Int frmCnt = 0;
598      while ( !eos[layer] && !(frmCnt == gopSize))
599      {
600        // get buffers
601        xGetBuffer(pcPicYuvRec, layer);
602
603        // read input YUV file
604        m_acTVideoIOYuvInputFileList[layer]->read      ( pcPicYuvOrg, m_aiPad );
605        m_acTEncTopList             [layer]->initNewPic( pcPicYuvOrg );
606
607        // increase number of received frames
608        m_frameRcvd[layer]++;
609       
610        frmCnt++;
611
612        eos[layer] = (m_frameRcvd[layer] == m_framesToBeEncoded);
613        allEos = allEos|eos[layer];
614
615        // if end of file (which is only detected on a read failure) flush the encoder of any queued pictures
616        if (m_acTVideoIOYuvInputFileList[layer]->isEof())
617        {
618          flush          [layer] = true;
619          eos            [layer]   = true;
620          m_frameRcvd    [layer]--;
621          m_acTEncTopList[layer]->setFramesToBeEncoded(m_frameRcvd[layer]);
622        }
623      }
624    }
625    for ( Int gopId=0; gopId < gopSize; gopId++ )
626    {
627      for(Int layer=0; layer < m_numberOfLayers; layer++ )
628      {
629        Int   iNumEncoded = 0;
630
631        // call encoding function for one frame         
632        m_acTEncTopList[layer]->encode( eos[layer], flush[layer] ? 0 : pcPicYuvOrg, *m_picYuvRec[layer], outputAccessUnits, iNumEncoded, gopId );       
633        xWriteOutput(bitstreamFile, iNumEncoded, outputAccessUnits, layer);
634        outputAccessUnits.clear();
635      }
636
637    }
638    gopSize = maxGopSize;
639  }
640  for(Int layer=0; layer < m_numberOfLayers; layer++ )
641  {
642    m_acTEncTopList[layer]->printSummary( m_acTEncTopList[layer]->getNumAllPicCoded() );
643  }
644#else
645  while ( !bEos )
646  {
647    // get buffers
648    xGetBuffer(pcPicYuvRec);
649
650    // read input YUV file
651    m_cTVideoIOYuvInputFile.read( pcPicYuvOrg, m_aiPad );
652
653    // increase number of received frames
654    m_iFrameRcvd++;
655
656    bEos = (m_iFrameRcvd == m_framesToBeEncoded);
657
658    Bool flush = 0;
659    // if end of file (which is only detected on a read failure) flush the encoder of any queued pictures
660    if (m_cTVideoIOYuvInputFile.isEof())
661    {
662      flush = true;
663      bEos = true;
664      m_iFrameRcvd--;
665      m_cTEncTop.setFramesToBeEncoded(m_iFrameRcvd);
666    }
667
668    // call encoding function for one frame
669    m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded );
670   
671    // write bistream to file if necessary
672    if ( iNumEncoded > 0 )
673    {
674      xWriteOutput(bitstreamFile, iNumEncoded, outputAccessUnits);
675      outputAccessUnits.clear();
676    }
677  }
678
679  m_cTEncTop.printSummary();
680#endif
681
682  // delete original YUV buffer
683  pcPicYuvOrg->destroy();
684  delete pcPicYuvOrg;
685  pcPicYuvOrg = NULL;
686 
687#if !H_MV
688  // delete used buffers in encoder class
689  m_cTEncTop.deletePicBuffer();
690#endif
691
692  // delete buffers & classes
693  xDeleteBuffer();
694  xDestroyLib();
695 
696  printRateSummary();
697
698  return;
699}
700
701#if H_3D
702TEncTop* TAppEncTop::getTEncTopView( Int viewId, Bool isDepth )
703{
704  TEncTop* encoder = NULL;
705  for( Int layer = 0; layer < m_acTEncTopList.size(); layer++ )
706  {
707    if( m_acTEncTopList[layer]->getViewId()  == viewId &&
708        m_acTEncTopList[layer]->getIsDepth() == isDepth )
709    {
710      encoder = m_acTEncTopList[layer];
711      break;
712    }
713  }
714  return encoder;
715}
716#endif
717// ====================================================================================================================
718// Protected member functions
719// ====================================================================================================================
720
721/**
722 - application has picture buffer list with size of GOP
723 - picture buffer list acts as ring buffer
724 - end of the list has the latest picture
725 .
726 */
727#if H_MV
728Void TAppEncTop::xGetBuffer( TComPicYuv*& rpcPicYuvRec, UInt layer)
729#else
730Void TAppEncTop::xGetBuffer( TComPicYuv*& rpcPicYuvRec)
731#endif
732{
733  assert( m_iGOPSize > 0 );
734 
735  // org. buffer
736#if H_MV
737  if ( m_picYuvRec[layer]->size() == (UInt)m_iGOPSize )
738  {
739    rpcPicYuvRec = m_picYuvRec[layer]->popFront();
740#else
741  if ( m_cListPicYuvRec.size() == (UInt)m_iGOPSize )
742  {
743    rpcPicYuvRec = m_cListPicYuvRec.popFront();
744#endif
745
746  }
747  else
748  {
749    rpcPicYuvRec = new TComPicYuv;
750   
751    rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
752
753  }
754#if H_MV
755  m_picYuvRec[layer]->pushBack( rpcPicYuvRec );
756#else
757  m_cListPicYuvRec.pushBack( rpcPicYuvRec );
758#endif
759}
760
761Void TAppEncTop::xDeleteBuffer( )
762{
763#if H_MV
764  for(Int layer=0; layer<m_picYuvRec.size(); layer++)
765  {
766    if(m_picYuvRec[layer])
767    {
768      TComList<TComPicYuv*>::iterator iterPicYuvRec  = m_picYuvRec[layer]->begin();
769      Int iSize = Int( m_picYuvRec[layer]->size() );
770#else
771  TComList<TComPicYuv*>::iterator iterPicYuvRec  = m_cListPicYuvRec.begin();
772 
773  Int iSize = Int( m_cListPicYuvRec.size() );
774#endif
775
776  for ( Int i = 0; i < iSize; i++ )
777  {
778    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
779    pcPicYuvRec->destroy();
780    delete pcPicYuvRec; pcPicYuvRec = NULL;
781  }
782
783#if H_MV
784    }
785  }
786#endif 
787}
788
789/** \param iNumEncoded  number of encoded frames
790 */
791#if H_MV
792Void TAppEncTop::xWriteOutput(std::ostream& bitstreamFile, Int iNumEncoded, std::list<AccessUnit>& accessUnits, UInt layerId)
793#else
794Void TAppEncTop::xWriteOutput(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits)
795#endif
796{
797  Int i;
798 
799#if H_MV
800  if( iNumEncoded > 0 )
801  {
802    TComList<TComPicYuv*>::iterator iterPicYuvRec = m_picYuvRec[layerId]->end();
803#else
804  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
805  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
806#endif
807
808  for ( i = 0; i < iNumEncoded; i++ )
809  {
810    --iterPicYuvRec;
811  }
812 
813  for ( i = 0; i < iNumEncoded; i++ )
814  {
815    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
816#if H_MV
817      if (m_pchReconFileList[layerId])
818      {
819        m_acTVideoIOYuvReconFileList[layerId]->write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
820      }
821    }
822  }
823  if( ! accessUnits.empty() )
824  {
825    list<AccessUnit>::iterator aUIter;
826    for( aUIter = accessUnits.begin(); aUIter != accessUnits.end(); aUIter++ )
827    {
828      const vector<unsigned>& stats = writeAnnexB(bitstreamFile, *aUIter);
829      rateStatsAccum(*aUIter, stats);
830    }
831  }
832#else
833    if (m_pchReconFile)
834    {
835      m_cTVideoIOYuvReconFile.write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
836    }
837
838    const AccessUnit& au = *(iterBitstream++);
839    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
840    rateStatsAccum(au, stats);
841  }
842#endif
843}
844
845/**
846 *
847 */
848void TAppEncTop::rateStatsAccum(const AccessUnit& au, const std::vector<UInt>& annexBsizes)
849{
850  AccessUnit::const_iterator it_au = au.begin();
851  vector<UInt>::const_iterator it_stats = annexBsizes.begin();
852
853  for (; it_au != au.end(); it_au++, it_stats++)
854  {
855    switch ((*it_au)->m_nalUnitType)
856    {
857    case NAL_UNIT_CODED_SLICE_TRAIL_R:
858    case NAL_UNIT_CODED_SLICE_TRAIL_N:
859    case NAL_UNIT_CODED_SLICE_TLA_R:
860    case NAL_UNIT_CODED_SLICE_TSA_N:
861    case NAL_UNIT_CODED_SLICE_STSA_R:
862    case NAL_UNIT_CODED_SLICE_STSA_N:
863    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
864    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
865    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
866    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
867    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
868    case NAL_UNIT_CODED_SLICE_CRA:
869    case NAL_UNIT_CODED_SLICE_RADL_N:
870    case NAL_UNIT_CODED_SLICE_RADL_R:
871    case NAL_UNIT_CODED_SLICE_RASL_N:
872    case NAL_UNIT_CODED_SLICE_RASL_R:
873    case NAL_UNIT_VPS:
874    case NAL_UNIT_SPS:
875    case NAL_UNIT_PPS:
876      m_essentialBytes += *it_stats;
877      break;
878    default:
879      break;
880    }
881
882    m_totalBytes += *it_stats;
883  }
884}
885
886void TAppEncTop::printRateSummary()
887{
888#if H_MV
889  Double time = (Double) m_frameRcvd[0] / m_iFrameRate;
890  printf("\n");
891#else
892  Double time = (Double) m_iFrameRcvd / m_iFrameRate;
893#endif
894  printf("Bytes written to file: %u (%.3f kbps)\n", m_totalBytes, 0.008 * m_totalBytes / time);
895#if VERBOSE_RATE
896  printf("Bytes for SPS/PPS/Slice (Incl. Annex B): %u (%.3f kbps)\n", m_essentialBytes, 0.008 * m_essentialBytes / time);
897#endif
898}
899
900#if H_MV
901Void TAppEncTop::xSetDimensionIdAndLength( TComVPS& vps )
902{   
903  vps.setScalabilityMask( m_scalabilityMask ); 
904  for( Int dim = 0; dim < m_dimIds.size(); dim++ )
905  {
906      vps.setDimensionIdLen( dim, m_dimensionIdLen[ dim ] );
907      for( Int layer = 1; layer < vps.getMaxLayers(); layer++ )
908      {       
909        vps.setDimensionId( layer, dim, m_dimIds[ dim ][ layer ] );       
910      } 
911  }
912}
913
914Void TAppEncTop::xSetDirectDependencyFlags( TComVPS& vps )
915{
916  for( Int layer = 0; layer < m_numberOfLayers; layer++ )
917  {
918    if( m_GOPListMvc[layer][MAX_GOP].m_POC == -1 )
919    {
920      continue;
921    }
922    for( Int i = 0; i < getGOPSize()+1; i++ ) 
923    {
924      GOPEntry ge = ( i < getGOPSize() ) ? m_GOPListMvc[layer][i] : m_GOPListMvc[layer][MAX_GOP];
925      for( Int j = 0; j < ge.m_numInterViewRefPics; j++ )
926      {
927        Int interLayerRef = layer + ge.m_interViewRefs[j];
928        vps.setDirectDependencyFlag( layer, interLayerRef, true );
929      }
930    }
931  }
932
933  vps.checkVPSExtensionSyntax(); 
934  vps.calcIvRefLayers();
935}
936
937Void TAppEncTop::xSetLayerIds( TComVPS& vps )
938{
939  vps.setSplittingFlag     ( m_splittingFlag );
940
941  Bool nuhLayerIdPresentFlag = !( m_layerIdInNuh.size() == 1 ); 
942  Int  maxNuhLayerId = nuhLayerIdPresentFlag ? xGetMax( m_layerIdInNuh ) : ( m_numberOfLayers - 1 ) ; 
943
944  vps.setMaxNuhLayerId( maxNuhLayerId ); 
945  vps.setVpsNuhLayerIdPresentFlag( nuhLayerIdPresentFlag ); 
946
947  for (Int layer = 0; layer < m_numberOfLayers; layer++ )
948  {
949    vps.setLayerIdInNuh( layer, nuhLayerIdPresentFlag ? m_layerIdInNuh[ layer ] : layer ); 
950    vps.setLayerIdInVps( vps.getLayerIdInNuh( layer ), layer ); 
951  }
952}
953
954Int TAppEncTop::xGetMax( std::vector<Int>& vec )
955{
956  Int maxVec = 0; 
957  for ( Int i = 0; i < vec.size(); i++)   
958    maxVec = max( vec[i], maxVec ); 
959  return maxVec;
960}
961#endif
962
963//! \}
Note: See TracBrowser for help on using the repository browser.