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

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