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

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

Initial development version for update to latest HM version.
Includes MV-HEVC and basic extensions for 3D-HEVC.

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