| 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 TEncGOP.cpp |
|---|
| 35 | \brief GOP encoder class |
|---|
| 36 | */ |
|---|
| 37 | |
|---|
| 38 | #include <list> |
|---|
| 39 | #include <algorithm> |
|---|
| 40 | #include <functional> |
|---|
| 41 | |
|---|
| 42 | #include "TEncTop.h" |
|---|
| 43 | #include "TEncGOP.h" |
|---|
| 44 | #include "TEncAnalyze.h" |
|---|
| 45 | #include "libmd5/MD5.h" |
|---|
| 46 | #include "TLibCommon/SEI.h" |
|---|
| 47 | #include "TLibCommon/NAL.h" |
|---|
| 48 | #include "NALwrite.h" |
|---|
| 49 | #include <time.h> |
|---|
| 50 | #include <math.h> |
|---|
| 51 | |
|---|
| 52 | using namespace std; |
|---|
| 53 | //! \ingroup TLibEncoder |
|---|
| 54 | //! \{ |
|---|
| 55 | |
|---|
| 56 | // ==================================================================================================================== |
|---|
| 57 | // Constructor / destructor / initialization / destroy |
|---|
| 58 | // ==================================================================================================================== |
|---|
| 59 | Int getLSB(Int poc, Int maxLSB) |
|---|
| 60 | { |
|---|
| 61 | if (poc >= 0) |
|---|
| 62 | { |
|---|
| 63 | return poc % maxLSB; |
|---|
| 64 | } |
|---|
| 65 | else |
|---|
| 66 | { |
|---|
| 67 | return (maxLSB - ((-poc) % maxLSB)) % maxLSB; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | TEncGOP::TEncGOP() |
|---|
| 72 | { |
|---|
| 73 | m_iLastIDR = 0; |
|---|
| 74 | m_iGopSize = 0; |
|---|
| 75 | m_iNumPicCoded = 0; //Niko |
|---|
| 76 | m_bFirst = true; |
|---|
| 77 | |
|---|
| 78 | m_pcCfg = NULL; |
|---|
| 79 | m_pcSliceEncoder = NULL; |
|---|
| 80 | m_pcListPic = NULL; |
|---|
| 81 | |
|---|
| 82 | m_pcEntropyCoder = NULL; |
|---|
| 83 | m_pcCavlcCoder = NULL; |
|---|
| 84 | m_pcSbacCoder = NULL; |
|---|
| 85 | m_pcBinCABAC = NULL; |
|---|
| 86 | |
|---|
| 87 | m_bSeqFirst = true; |
|---|
| 88 | |
|---|
| 89 | m_bRefreshPending = 0; |
|---|
| 90 | m_pocCRA = 0; |
|---|
| 91 | m_numLongTermRefPicSPS = 0; |
|---|
| 92 | ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps)); |
|---|
| 93 | ::memset(m_ltRefPicUsedByCurrPicFlag, 0, sizeof(m_ltRefPicUsedByCurrPicFlag)); |
|---|
| 94 | m_cpbRemovalDelay = 0; |
|---|
| 95 | m_lastBPSEI = 0; |
|---|
| 96 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 97 | xResetNonNestedSEIPresentFlags(); |
|---|
| 98 | #if K0180_SCALABLE_NESTING_SEI |
|---|
| 99 | xResetNestedSEIPresentFlags(); |
|---|
| 100 | #endif |
|---|
| 101 | #endif |
|---|
| 102 | #if SVC_UPSAMPLING |
|---|
| 103 | m_pcPredSearch = NULL; |
|---|
| 104 | #endif |
|---|
| 105 | return; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | TEncGOP::~TEncGOP() |
|---|
| 109 | { |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** Create list to contain pointers to LCU start addresses of slice. |
|---|
| 113 | */ |
|---|
| 114 | #if SVC_EXTENSION |
|---|
| 115 | Void TEncGOP::create( UInt layerId ) |
|---|
| 116 | { |
|---|
| 117 | m_bLongtermTestPictureHasBeenCoded = 0; |
|---|
| 118 | m_bLongtermTestPictureHasBeenCoded2 = 0; |
|---|
| 119 | m_layerId = layerId; |
|---|
| 120 | } |
|---|
| 121 | #else |
|---|
| 122 | Void TEncGOP::create() |
|---|
| 123 | { |
|---|
| 124 | m_bLongtermTestPictureHasBeenCoded = 0; |
|---|
| 125 | m_bLongtermTestPictureHasBeenCoded2 = 0; |
|---|
| 126 | } |
|---|
| 127 | #endif |
|---|
| 128 | |
|---|
| 129 | Void TEncGOP::destroy() |
|---|
| 130 | { |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | Void TEncGOP::init ( TEncTop* pcTEncTop ) |
|---|
| 134 | { |
|---|
| 135 | m_pcEncTop = pcTEncTop; |
|---|
| 136 | m_pcCfg = pcTEncTop; |
|---|
| 137 | m_pcSliceEncoder = pcTEncTop->getSliceEncoder(); |
|---|
| 138 | m_pcListPic = pcTEncTop->getListPic(); |
|---|
| 139 | |
|---|
| 140 | m_pcEntropyCoder = pcTEncTop->getEntropyCoder(); |
|---|
| 141 | m_pcCavlcCoder = pcTEncTop->getCavlcCoder(); |
|---|
| 142 | m_pcSbacCoder = pcTEncTop->getSbacCoder(); |
|---|
| 143 | m_pcBinCABAC = pcTEncTop->getBinCABAC(); |
|---|
| 144 | m_pcLoopFilter = pcTEncTop->getLoopFilter(); |
|---|
| 145 | m_pcBitCounter = pcTEncTop->getBitCounter(); |
|---|
| 146 | |
|---|
| 147 | //--Adaptive Loop filter |
|---|
| 148 | m_pcSAO = pcTEncTop->getSAO(); |
|---|
| 149 | m_pcRateCtrl = pcTEncTop->getRateCtrl(); |
|---|
| 150 | m_lastBPSEI = 0; |
|---|
| 151 | m_totalCoded = 0; |
|---|
| 152 | |
|---|
| 153 | #if SVC_EXTENSION |
|---|
| 154 | m_ppcTEncTop = pcTEncTop->getLayerEnc(); |
|---|
| 155 | #endif |
|---|
| 156 | #if SVC_UPSAMPLING |
|---|
| 157 | m_pcPredSearch = pcTEncTop->getPredSearch(); ///< encoder search class |
|---|
| 158 | #endif |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | SEIActiveParameterSets* TEncGOP::xCreateSEIActiveParameterSets (TComSPS *sps) |
|---|
| 162 | { |
|---|
| 163 | SEIActiveParameterSets *seiActiveParameterSets = new SEIActiveParameterSets(); |
|---|
| 164 | seiActiveParameterSets->activeVPSId = m_pcCfg->getVPS()->getVPSId(); |
|---|
| 165 | #if L0047_APS_FLAGS |
|---|
| 166 | seiActiveParameterSets->m_fullRandomAccessFlag = false; |
|---|
| 167 | seiActiveParameterSets->m_noParamSetUpdateFlag = false; |
|---|
| 168 | #endif |
|---|
| 169 | seiActiveParameterSets->numSpsIdsMinus1 = 0; |
|---|
| 170 | seiActiveParameterSets->activeSeqParamSetId.resize(seiActiveParameterSets->numSpsIdsMinus1 + 1); |
|---|
| 171 | seiActiveParameterSets->activeSeqParamSetId[0] = sps->getSPSId(); |
|---|
| 172 | return seiActiveParameterSets; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | SEIFramePacking* TEncGOP::xCreateSEIFramePacking() |
|---|
| 176 | { |
|---|
| 177 | SEIFramePacking *seiFramePacking = new SEIFramePacking(); |
|---|
| 178 | seiFramePacking->m_arrangementId = m_pcCfg->getFramePackingArrangementSEIId(); |
|---|
| 179 | seiFramePacking->m_arrangementCancelFlag = 0; |
|---|
| 180 | seiFramePacking->m_arrangementType = m_pcCfg->getFramePackingArrangementSEIType(); |
|---|
| 181 | #if L0444_FPA_TYPE |
|---|
| 182 | assert((seiFramePacking->m_arrangementType > 2) && (seiFramePacking->m_arrangementType < 6) ); |
|---|
| 183 | #endif |
|---|
| 184 | seiFramePacking->m_quincunxSamplingFlag = m_pcCfg->getFramePackingArrangementSEIQuincunx(); |
|---|
| 185 | seiFramePacking->m_contentInterpretationType = m_pcCfg->getFramePackingArrangementSEIInterpretation(); |
|---|
| 186 | seiFramePacking->m_spatialFlippingFlag = 0; |
|---|
| 187 | seiFramePacking->m_frame0FlippedFlag = 0; |
|---|
| 188 | seiFramePacking->m_fieldViewsFlag = (seiFramePacking->m_arrangementType == 2); |
|---|
| 189 | seiFramePacking->m_currentFrameIsFrame0Flag = ((seiFramePacking->m_arrangementType == 5) && m_iNumPicCoded&1); |
|---|
| 190 | seiFramePacking->m_frame0SelfContainedFlag = 0; |
|---|
| 191 | seiFramePacking->m_frame1SelfContainedFlag = 0; |
|---|
| 192 | seiFramePacking->m_frame0GridPositionX = 0; |
|---|
| 193 | seiFramePacking->m_frame0GridPositionY = 0; |
|---|
| 194 | seiFramePacking->m_frame1GridPositionX = 0; |
|---|
| 195 | seiFramePacking->m_frame1GridPositionY = 0; |
|---|
| 196 | seiFramePacking->m_arrangementReservedByte = 0; |
|---|
| 197 | #if L0045_PERSISTENCE_FLAGS |
|---|
| 198 | seiFramePacking->m_arrangementPersistenceFlag = true; |
|---|
| 199 | #else |
|---|
| 200 | seiFramePacking->m_arrangementRepetetionPeriod = 1; |
|---|
| 201 | #endif |
|---|
| 202 | seiFramePacking->m_upsampledAspectRatio = 0; |
|---|
| 203 | return seiFramePacking; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | SEIDisplayOrientation* TEncGOP::xCreateSEIDisplayOrientation() |
|---|
| 207 | { |
|---|
| 208 | SEIDisplayOrientation *seiDisplayOrientation = new SEIDisplayOrientation(); |
|---|
| 209 | seiDisplayOrientation->cancelFlag = false; |
|---|
| 210 | seiDisplayOrientation->horFlip = false; |
|---|
| 211 | seiDisplayOrientation->verFlip = false; |
|---|
| 212 | seiDisplayOrientation->anticlockwiseRotation = m_pcCfg->getDisplayOrientationSEIAngle(); |
|---|
| 213 | return seiDisplayOrientation; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | #if J0149_TONE_MAPPING_SEI |
|---|
| 217 | SEIToneMappingInfo* TEncGOP::xCreateSEIToneMappingInfo() |
|---|
| 218 | { |
|---|
| 219 | SEIToneMappingInfo *seiToneMappingInfo = new SEIToneMappingInfo(); |
|---|
| 220 | seiToneMappingInfo->m_toneMapId = m_pcCfg->getTMISEIToneMapId(); |
|---|
| 221 | seiToneMappingInfo->m_toneMapCancelFlag = m_pcCfg->getTMISEIToneMapCancelFlag(); |
|---|
| 222 | seiToneMappingInfo->m_toneMapPersistenceFlag = m_pcCfg->getTMISEIToneMapPersistenceFlag(); |
|---|
| 223 | |
|---|
| 224 | seiToneMappingInfo->m_codedDataBitDepth = m_pcCfg->getTMISEICodedDataBitDepth(); |
|---|
| 225 | assert(seiToneMappingInfo->m_codedDataBitDepth >= 8 && seiToneMappingInfo->m_codedDataBitDepth <= 14); |
|---|
| 226 | seiToneMappingInfo->m_targetBitDepth = m_pcCfg->getTMISEITargetBitDepth(); |
|---|
| 227 | assert( (seiToneMappingInfo->m_targetBitDepth >= 1 && seiToneMappingInfo->m_targetBitDepth <= 17) || (seiToneMappingInfo->m_targetBitDepth == 255) ); |
|---|
| 228 | seiToneMappingInfo->m_modelId = m_pcCfg->getTMISEIModelID(); |
|---|
| 229 | assert(seiToneMappingInfo->m_modelId >=0 &&seiToneMappingInfo->m_modelId<=4); |
|---|
| 230 | |
|---|
| 231 | switch( seiToneMappingInfo->m_modelId) |
|---|
| 232 | { |
|---|
| 233 | case 0: |
|---|
| 234 | { |
|---|
| 235 | seiToneMappingInfo->m_minValue = m_pcCfg->getTMISEIMinValue(); |
|---|
| 236 | seiToneMappingInfo->m_maxValue = m_pcCfg->getTMISEIMaxValue(); |
|---|
| 237 | break; |
|---|
| 238 | } |
|---|
| 239 | case 1: |
|---|
| 240 | { |
|---|
| 241 | seiToneMappingInfo->m_sigmoidMidpoint = m_pcCfg->getTMISEISigmoidMidpoint(); |
|---|
| 242 | seiToneMappingInfo->m_sigmoidWidth = m_pcCfg->getTMISEISigmoidWidth(); |
|---|
| 243 | break; |
|---|
| 244 | } |
|---|
| 245 | case 2: |
|---|
| 246 | { |
|---|
| 247 | UInt num = 1u<<(seiToneMappingInfo->m_targetBitDepth); |
|---|
| 248 | seiToneMappingInfo->m_startOfCodedInterval.resize(num); |
|---|
| 249 | Int* ptmp = m_pcCfg->getTMISEIStartOfCodedInterva(); |
|---|
| 250 | if(ptmp) |
|---|
| 251 | { |
|---|
| 252 | for(int i=0; i<num;i++) |
|---|
| 253 | { |
|---|
| 254 | seiToneMappingInfo->m_startOfCodedInterval[i] = ptmp[i]; |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | break; |
|---|
| 258 | } |
|---|
| 259 | case 3: |
|---|
| 260 | { |
|---|
| 261 | seiToneMappingInfo->m_numPivots = m_pcCfg->getTMISEINumPivots(); |
|---|
| 262 | seiToneMappingInfo->m_codedPivotValue.resize(seiToneMappingInfo->m_numPivots); |
|---|
| 263 | seiToneMappingInfo->m_targetPivotValue.resize(seiToneMappingInfo->m_numPivots); |
|---|
| 264 | Int* ptmpcoded = m_pcCfg->getTMISEICodedPivotValue(); |
|---|
| 265 | Int* ptmptarget = m_pcCfg->getTMISEITargetPivotValue(); |
|---|
| 266 | if(ptmpcoded&&ptmptarget) |
|---|
| 267 | { |
|---|
| 268 | for(int i=0; i<(seiToneMappingInfo->m_numPivots);i++) |
|---|
| 269 | { |
|---|
| 270 | seiToneMappingInfo->m_codedPivotValue[i]=ptmpcoded[i]; |
|---|
| 271 | seiToneMappingInfo->m_targetPivotValue[i]=ptmptarget[i]; |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | break; |
|---|
| 275 | } |
|---|
| 276 | case 4: |
|---|
| 277 | { |
|---|
| 278 | seiToneMappingInfo->m_cameraIsoSpeedIdc = m_pcCfg->getTMISEICameraIsoSpeedIdc(); |
|---|
| 279 | seiToneMappingInfo->m_cameraIsoSpeedValue = m_pcCfg->getTMISEICameraIsoSpeedValue(); |
|---|
| 280 | assert( seiToneMappingInfo->m_cameraIsoSpeedValue !=0 ); |
|---|
| 281 | seiToneMappingInfo->m_exposureCompensationValueSignFlag = m_pcCfg->getTMISEIExposureCompensationValueSignFlag(); |
|---|
| 282 | seiToneMappingInfo->m_exposureCompensationValueNumerator = m_pcCfg->getTMISEIExposureCompensationValueNumerator(); |
|---|
| 283 | seiToneMappingInfo->m_exposureCompensationValueDenomIdc = m_pcCfg->getTMISEIExposureCompensationValueDenomIdc(); |
|---|
| 284 | seiToneMappingInfo->m_refScreenLuminanceWhite = m_pcCfg->getTMISEIRefScreenLuminanceWhite(); |
|---|
| 285 | seiToneMappingInfo->m_extendedRangeWhiteLevel = m_pcCfg->getTMISEIExtendedRangeWhiteLevel(); |
|---|
| 286 | assert( seiToneMappingInfo->m_extendedRangeWhiteLevel >= 100 ); |
|---|
| 287 | seiToneMappingInfo->m_nominalBlackLevelLumaCodeValue = m_pcCfg->getTMISEINominalBlackLevelLumaCodeValue(); |
|---|
| 288 | seiToneMappingInfo->m_nominalWhiteLevelLumaCodeValue = m_pcCfg->getTMISEINominalWhiteLevelLumaCodeValue(); |
|---|
| 289 | assert( seiToneMappingInfo->m_nominalWhiteLevelLumaCodeValue > seiToneMappingInfo->m_nominalBlackLevelLumaCodeValue ); |
|---|
| 290 | seiToneMappingInfo->m_extendedWhiteLevelLumaCodeValue = m_pcCfg->getTMISEIExtendedWhiteLevelLumaCodeValue(); |
|---|
| 291 | assert( seiToneMappingInfo->m_extendedWhiteLevelLumaCodeValue >= seiToneMappingInfo->m_nominalWhiteLevelLumaCodeValue ); |
|---|
| 292 | break; |
|---|
| 293 | } |
|---|
| 294 | default: |
|---|
| 295 | { |
|---|
| 296 | assert(!"Undefined SEIToneMapModelId"); |
|---|
| 297 | break; |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | return seiToneMappingInfo; |
|---|
| 301 | } |
|---|
| 302 | #endif |
|---|
| 303 | Void TEncGOP::xCreateLeadingSEIMessages (/*SEIMessages seiMessages,*/ AccessUnit &accessUnit, TComSPS *sps) |
|---|
| 304 | { |
|---|
| 305 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI); |
|---|
| 306 | |
|---|
| 307 | if(m_pcCfg->getActiveParameterSetsSEIEnabled()) |
|---|
| 308 | { |
|---|
| 309 | SEIActiveParameterSets *sei = xCreateSEIActiveParameterSets (sps); |
|---|
| 310 | |
|---|
| 311 | //nalu = NALUnit(NAL_UNIT_SEI); |
|---|
| 312 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 313 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps); |
|---|
| 314 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 315 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 316 | delete sei; |
|---|
| 317 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 318 | m_activeParameterSetSEIPresentInAU = true; |
|---|
| 319 | #endif |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | if(m_pcCfg->getFramePackingArrangementSEIEnabled()) |
|---|
| 323 | { |
|---|
| 324 | SEIFramePacking *sei = xCreateSEIFramePacking (); |
|---|
| 325 | |
|---|
| 326 | nalu = NALUnit(NAL_UNIT_PREFIX_SEI); |
|---|
| 327 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 328 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps); |
|---|
| 329 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 330 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 331 | delete sei; |
|---|
| 332 | } |
|---|
| 333 | if (m_pcCfg->getDisplayOrientationSEIAngle()) |
|---|
| 334 | { |
|---|
| 335 | SEIDisplayOrientation *sei = xCreateSEIDisplayOrientation(); |
|---|
| 336 | |
|---|
| 337 | nalu = NALUnit(NAL_UNIT_PREFIX_SEI); |
|---|
| 338 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 339 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps); |
|---|
| 340 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 341 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 342 | delete sei; |
|---|
| 343 | } |
|---|
| 344 | #if J0149_TONE_MAPPING_SEI |
|---|
| 345 | if(m_pcCfg->getToneMappingInfoSEIEnabled()) |
|---|
| 346 | { |
|---|
| 347 | SEIToneMappingInfo *sei = xCreateSEIToneMappingInfo (); |
|---|
| 348 | |
|---|
| 349 | nalu = NALUnit(NAL_UNIT_PREFIX_SEI); |
|---|
| 350 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 351 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, *sei, sps); |
|---|
| 352 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 353 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 354 | delete sei; |
|---|
| 355 | } |
|---|
| 356 | #endif |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | // ==================================================================================================================== |
|---|
| 360 | // Public member functions |
|---|
| 361 | // ==================================================================================================================== |
|---|
| 362 | #if SVC_EXTENSION |
|---|
| 363 | Void TEncGOP::compressGOP( Int iPicIdInGOP, Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP) |
|---|
| 364 | #else |
|---|
| 365 | Void TEncGOP::compressGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP) |
|---|
| 366 | #endif |
|---|
| 367 | { |
|---|
| 368 | TComPic* pcPic; |
|---|
| 369 | TComPicYuv* pcPicYuvRecOut; |
|---|
| 370 | TComSlice* pcSlice; |
|---|
| 371 | TComOutputBitstream *pcBitstreamRedirect; |
|---|
| 372 | pcBitstreamRedirect = new TComOutputBitstream; |
|---|
| 373 | AccessUnit::iterator itLocationToPushSliceHeaderNALU; // used to store location where NALU containing slice header is to be inserted |
|---|
| 374 | UInt uiOneBitstreamPerSliceLength = 0; |
|---|
| 375 | TEncSbac* pcSbacCoders = NULL; |
|---|
| 376 | TComOutputBitstream* pcSubstreamsOut = NULL; |
|---|
| 377 | |
|---|
| 378 | xInitGOP( iPOCLast, iNumPicRcvd, rcListPic, rcListPicYuvRecOut ); |
|---|
| 379 | |
|---|
| 380 | m_iNumPicCoded = 0; |
|---|
| 381 | SEIPictureTiming pictureTimingSEI; |
|---|
| 382 | #if L0208_SOP_DESCRIPTION_SEI |
|---|
| 383 | Bool writeSOP = m_pcCfg->getSOPDescriptionSEIEnabled(); |
|---|
| 384 | #endif |
|---|
| 385 | #if K0180_SCALABLE_NESTING_SEI |
|---|
| 386 | // Initialize Scalable Nesting SEI with single layer values |
|---|
| 387 | SEIScalableNesting scalableNestingSEI; |
|---|
| 388 | scalableNestingSEI.m_bitStreamSubsetFlag = 1; // If the nested SEI messages are picture buffereing SEI mesages, picure timing SEI messages or sub-picture timing SEI messages, bitstream_subset_flag shall be equal to 1 |
|---|
| 389 | scalableNestingSEI.m_nestingOpFlag = 0; |
|---|
| 390 | scalableNestingSEI.m_nestingNumOpsMinus1 = 0; //nesting_num_ops_minus1 |
|---|
| 391 | scalableNestingSEI.m_allLayersFlag = 0; |
|---|
| 392 | scalableNestingSEI.m_nestingNoOpMaxTemporalIdPlus1 = 6 + 1; //nesting_no_op_max_temporal_id_plus1 |
|---|
| 393 | scalableNestingSEI.m_nestingNumLayersMinus1 = 1 - 1; //nesting_num_layers_minus1 |
|---|
| 394 | scalableNestingSEI.m_nestingLayerId[0] = 0; |
|---|
| 395 | scalableNestingSEI.m_callerOwnsSEIs = true; |
|---|
| 396 | #endif |
|---|
| 397 | #if L0044_DU_DPB_OUTPUT_DELAY_HRD |
|---|
| 398 | Int picSptDpbOutputDuDelay = 0; |
|---|
| 399 | #endif |
|---|
| 400 | UInt *accumBitsDU = NULL; |
|---|
| 401 | UInt *accumNalsDU = NULL; |
|---|
| 402 | SEIDecodingUnitInfo decodingUnitInfoSEI; |
|---|
| 403 | #if SVC_EXTENSION |
|---|
| 404 | for ( Int iGOPid=iPicIdInGOP; iGOPid < iPicIdInGOP+1; iGOPid++ ) |
|---|
| 405 | #else |
|---|
| 406 | for ( Int iGOPid=0; iGOPid < m_iGopSize; iGOPid++ ) |
|---|
| 407 | #endif |
|---|
| 408 | { |
|---|
| 409 | UInt uiColDir = 1; |
|---|
| 410 | //-- For time output for each slice |
|---|
| 411 | long iBeforeTime = clock(); |
|---|
| 412 | |
|---|
| 413 | //select uiColDir |
|---|
| 414 | Int iCloseLeft=1, iCloseRight=-1; |
|---|
| 415 | for(Int i = 0; i<m_pcCfg->getGOPEntry(iGOPid).m_numRefPics; i++) |
|---|
| 416 | { |
|---|
| 417 | Int iRef = m_pcCfg->getGOPEntry(iGOPid).m_referencePics[i]; |
|---|
| 418 | if(iRef>0&&(iRef<iCloseRight||iCloseRight==-1)) |
|---|
| 419 | { |
|---|
| 420 | iCloseRight=iRef; |
|---|
| 421 | } |
|---|
| 422 | else if(iRef<0&&(iRef>iCloseLeft||iCloseLeft==1)) |
|---|
| 423 | { |
|---|
| 424 | iCloseLeft=iRef; |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | if(iCloseRight>-1) |
|---|
| 428 | { |
|---|
| 429 | iCloseRight=iCloseRight+m_pcCfg->getGOPEntry(iGOPid).m_POC-1; |
|---|
| 430 | } |
|---|
| 431 | if(iCloseLeft<1) |
|---|
| 432 | { |
|---|
| 433 | iCloseLeft=iCloseLeft+m_pcCfg->getGOPEntry(iGOPid).m_POC-1; |
|---|
| 434 | while(iCloseLeft<0) |
|---|
| 435 | { |
|---|
| 436 | iCloseLeft+=m_iGopSize; |
|---|
| 437 | } |
|---|
| 438 | } |
|---|
| 439 | Int iLeftQP=0, iRightQP=0; |
|---|
| 440 | for(Int i=0; i<m_iGopSize; i++) |
|---|
| 441 | { |
|---|
| 442 | if(m_pcCfg->getGOPEntry(i).m_POC==(iCloseLeft%m_iGopSize)+1) |
|---|
| 443 | { |
|---|
| 444 | iLeftQP= m_pcCfg->getGOPEntry(i).m_QPOffset; |
|---|
| 445 | } |
|---|
| 446 | if (m_pcCfg->getGOPEntry(i).m_POC==(iCloseRight%m_iGopSize)+1) |
|---|
| 447 | { |
|---|
| 448 | iRightQP=m_pcCfg->getGOPEntry(i).m_QPOffset; |
|---|
| 449 | } |
|---|
| 450 | } |
|---|
| 451 | if(iCloseRight>-1&&iRightQP<iLeftQP) |
|---|
| 452 | { |
|---|
| 453 | uiColDir=0; |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | /////////////////////////////////////////////////////////////////////////////////////////////////// Initial to start encoding |
|---|
| 457 | Int pocCurr = iPOCLast -iNumPicRcvd+ m_pcCfg->getGOPEntry(iGOPid).m_POC; |
|---|
| 458 | Int iTimeOffset = m_pcCfg->getGOPEntry(iGOPid).m_POC; |
|---|
| 459 | if(iPOCLast == 0) |
|---|
| 460 | { |
|---|
| 461 | pocCurr=0; |
|---|
| 462 | iTimeOffset = 1; |
|---|
| 463 | } |
|---|
| 464 | if(pocCurr>=m_pcCfg->getFramesToBeEncoded()) |
|---|
| 465 | { |
|---|
| 466 | continue; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | if( getNalUnitType(pocCurr, m_iLastIDR) == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType(pocCurr, m_iLastIDR) == NAL_UNIT_CODED_SLICE_IDR_N_LP ) |
|---|
| 470 | { |
|---|
| 471 | m_iLastIDR = pocCurr; |
|---|
| 472 | } |
|---|
| 473 | // start a new access unit: create an entry in the list of output access units |
|---|
| 474 | accessUnitsInGOP.push_back(AccessUnit()); |
|---|
| 475 | AccessUnit& accessUnit = accessUnitsInGOP.back(); |
|---|
| 476 | xGetBuffer( rcListPic, rcListPicYuvRecOut, iNumPicRcvd, iTimeOffset, pcPic, pcPicYuvRecOut, pocCurr ); |
|---|
| 477 | |
|---|
| 478 | // Slice data initialization |
|---|
| 479 | pcPic->clearSliceBuffer(); |
|---|
| 480 | assert(pcPic->getNumAllocatedSlice() == 1); |
|---|
| 481 | m_pcSliceEncoder->setSliceIdx(0); |
|---|
| 482 | pcPic->setCurrSliceIdx(0); |
|---|
| 483 | #if SVC_EXTENSION |
|---|
| 484 | pcPic->setLayerId( m_layerId ); |
|---|
| 485 | #endif |
|---|
| 486 | m_pcSliceEncoder->initEncSlice ( pcPic, iPOCLast, pocCurr, iNumPicRcvd, iGOPid, pcSlice, m_pcEncTop->getSPS(), m_pcEncTop->getPPS() ); |
|---|
| 487 | pcSlice->setLastIDR(m_iLastIDR); |
|---|
| 488 | pcSlice->setSliceIdx(0); |
|---|
| 489 | //set default slice level flag to the same as SPS level flag |
|---|
| 490 | pcSlice->setLFCrossSliceBoundaryFlag( pcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() ); |
|---|
| 491 | pcSlice->setScalingList ( m_pcEncTop->getScalingList() ); |
|---|
| 492 | pcSlice->getScalingList()->setUseTransformSkip(m_pcEncTop->getPPS()->getUseTransformSkip()); |
|---|
| 493 | if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_OFF) |
|---|
| 494 | { |
|---|
| 495 | m_pcEncTop->getTrQuant()->setFlatScalingList(); |
|---|
| 496 | m_pcEncTop->getTrQuant()->setUseScalingList(false); |
|---|
| 497 | m_pcEncTop->getSPS()->setScalingListPresentFlag(false); |
|---|
| 498 | m_pcEncTop->getPPS()->setScalingListPresentFlag(false); |
|---|
| 499 | } |
|---|
| 500 | else if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_DEFAULT) |
|---|
| 501 | { |
|---|
| 502 | pcSlice->setDefaultScalingList (); |
|---|
| 503 | m_pcEncTop->getSPS()->setScalingListPresentFlag(false); |
|---|
| 504 | m_pcEncTop->getPPS()->setScalingListPresentFlag(false); |
|---|
| 505 | m_pcEncTop->getTrQuant()->setScalingList(pcSlice->getScalingList()); |
|---|
| 506 | m_pcEncTop->getTrQuant()->setUseScalingList(true); |
|---|
| 507 | } |
|---|
| 508 | else if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_FILE_READ) |
|---|
| 509 | { |
|---|
| 510 | if(pcSlice->getScalingList()->xParseScalingList(m_pcCfg->getScalingListFile())) |
|---|
| 511 | { |
|---|
| 512 | pcSlice->setDefaultScalingList (); |
|---|
| 513 | } |
|---|
| 514 | pcSlice->getScalingList()->checkDcOfMatrix(); |
|---|
| 515 | m_pcEncTop->getSPS()->setScalingListPresentFlag(pcSlice->checkDefaultScalingList()); |
|---|
| 516 | m_pcEncTop->getPPS()->setScalingListPresentFlag(false); |
|---|
| 517 | m_pcEncTop->getTrQuant()->setScalingList(pcSlice->getScalingList()); |
|---|
| 518 | m_pcEncTop->getTrQuant()->setUseScalingList(true); |
|---|
| 519 | } |
|---|
| 520 | else |
|---|
| 521 | { |
|---|
| 522 | printf("error : ScalingList == %d no support\n",m_pcEncTop->getUseScalingListId()); |
|---|
| 523 | assert(0); |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | if(pcSlice->getSliceType()==B_SLICE&&m_pcCfg->getGOPEntry(iGOPid).m_sliceType=='P') |
|---|
| 527 | { |
|---|
| 528 | pcSlice->setSliceType(P_SLICE); |
|---|
| 529 | } |
|---|
| 530 | // Set the nal unit type |
|---|
| 531 | pcSlice->setNalUnitType(getNalUnitType(pocCurr, m_iLastIDR)); |
|---|
| 532 | #if IDR_ALIGNMENT |
|---|
| 533 | if (m_layerId > 0) |
|---|
| 534 | { |
|---|
| 535 | #if VPS_EXTN_DIRECT_REF_LAYERS_CONTINUE |
|---|
| 536 | TComList<TComPic*> *cListPic = m_ppcTEncTop[m_layerId]->getRefLayerEnc(m_layerId)->getListPic(); |
|---|
| 537 | #else |
|---|
| 538 | TComList<TComPic*> *cListPic = m_ppcTEncTop[m_layerId-1]->getListPic(); |
|---|
| 539 | #endif |
|---|
| 540 | pcSlice->setBaseColPic (*cListPic, m_layerId ); |
|---|
| 541 | } |
|---|
| 542 | #endif |
|---|
| 543 | #if REF_IDX_FRAMEWORK |
|---|
| 544 | |
|---|
| 545 | if( m_layerId > 0 && (pocCurr % m_pcCfg->getIntraPeriod() == 0) ) |
|---|
| 546 | { |
|---|
| 547 | #if IDR_ALIGNMENT |
|---|
| 548 | if( pcSlice->getBaseColPic()->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || pcSlice->getBaseColPic()->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) |
|---|
| 549 | { |
|---|
| 550 | pcSlice->setNalUnitType(pcSlice->getBaseColPic()->getSlice(0)->getNalUnitType()); |
|---|
| 551 | } |
|---|
| 552 | else |
|---|
| 553 | #endif |
|---|
| 554 | pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_CRA); |
|---|
| 555 | } |
|---|
| 556 | if( m_layerId > 0 && !m_pcEncTop->getElRapSliceTypeB() ) |
|---|
| 557 | { |
|---|
| 558 | if( (pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP) && |
|---|
| 559 | (pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA) && |
|---|
| 560 | pcSlice->getSliceType() == B_SLICE ) |
|---|
| 561 | { |
|---|
| 562 | pcSlice->setSliceType(P_SLICE); |
|---|
| 563 | } |
|---|
| 564 | } |
|---|
| 565 | #endif |
|---|
| 566 | if(pcSlice->getTemporalLayerNonReferenceFlag()) |
|---|
| 567 | { |
|---|
| 568 | if(pcSlice->getNalUnitType()==NAL_UNIT_CODED_SLICE_TRAIL_R) |
|---|
| 569 | { |
|---|
| 570 | pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TRAIL_N); |
|---|
| 571 | } |
|---|
| 572 | if(pcSlice->getNalUnitType()==NAL_UNIT_CODED_SLICE_RADL_R) |
|---|
| 573 | { |
|---|
| 574 | pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_RADL_N); |
|---|
| 575 | } |
|---|
| 576 | if(pcSlice->getNalUnitType()==NAL_UNIT_CODED_SLICE_RASL_R) |
|---|
| 577 | { |
|---|
| 578 | pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_RASL_N); |
|---|
| 579 | } |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | // Do decoding refresh marking if any |
|---|
| 583 | pcSlice->decodingRefreshMarking(m_pocCRA, m_bRefreshPending, rcListPic); |
|---|
| 584 | m_pcEncTop->selectReferencePictureSet(pcSlice, pocCurr, iGOPid); |
|---|
| 585 | pcSlice->getRPS()->setNumberOfLongtermPictures(0); |
|---|
| 586 | |
|---|
| 587 | if(pcSlice->checkThatAllRefPicsAreAvailable(rcListPic, pcSlice->getRPS(), false) != 0) |
|---|
| 588 | { |
|---|
| 589 | pcSlice->createExplicitReferencePictureSetFromReference(rcListPic, pcSlice->getRPS()); |
|---|
| 590 | } |
|---|
| 591 | pcSlice->applyReferencePictureSet(rcListPic, pcSlice->getRPS()); |
|---|
| 592 | |
|---|
| 593 | if(pcSlice->getTLayer() > 0) |
|---|
| 594 | { |
|---|
| 595 | if(pcSlice->isTemporalLayerSwitchingPoint(rcListPic) || pcSlice->getSPS()->getTemporalIdNestingFlag()) |
|---|
| 596 | { |
|---|
| 597 | if(pcSlice->getTemporalLayerNonReferenceFlag()) |
|---|
| 598 | { |
|---|
| 599 | pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TSA_N); |
|---|
| 600 | } |
|---|
| 601 | else |
|---|
| 602 | { |
|---|
| 603 | pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TLA_R); |
|---|
| 604 | } |
|---|
| 605 | } |
|---|
| 606 | else if(pcSlice->isStepwiseTemporalLayerSwitchingPointCandidate(rcListPic)) |
|---|
| 607 | { |
|---|
| 608 | Bool isSTSA=true; |
|---|
| 609 | for(Int ii=iGOPid+1;(ii<m_pcCfg->getGOPSize() && isSTSA==true);ii++) |
|---|
| 610 | { |
|---|
| 611 | Int lTid= m_pcCfg->getGOPEntry(ii).m_temporalId; |
|---|
| 612 | if(lTid==pcSlice->getTLayer()) |
|---|
| 613 | { |
|---|
| 614 | TComReferencePictureSet* nRPS = pcSlice->getSPS()->getRPSList()->getReferencePictureSet(ii); |
|---|
| 615 | for(Int jj=0;jj<nRPS->getNumberOfPictures();jj++) |
|---|
| 616 | { |
|---|
| 617 | if(nRPS->getUsed(jj)) |
|---|
| 618 | { |
|---|
| 619 | Int tPoc=m_pcCfg->getGOPEntry(ii).m_POC+nRPS->getDeltaPOC(jj); |
|---|
| 620 | Int kk=0; |
|---|
| 621 | for(kk=0;kk<m_pcCfg->getGOPSize();kk++) |
|---|
| 622 | { |
|---|
| 623 | if(m_pcCfg->getGOPEntry(kk).m_POC==tPoc) |
|---|
| 624 | break; |
|---|
| 625 | } |
|---|
| 626 | Int tTid=m_pcCfg->getGOPEntry(kk).m_temporalId; |
|---|
| 627 | if(tTid >= pcSlice->getTLayer()) |
|---|
| 628 | { |
|---|
| 629 | isSTSA=false; |
|---|
| 630 | break; |
|---|
| 631 | } |
|---|
| 632 | } |
|---|
| 633 | } |
|---|
| 634 | } |
|---|
| 635 | } |
|---|
| 636 | if(isSTSA==true) |
|---|
| 637 | { |
|---|
| 638 | if(pcSlice->getTemporalLayerNonReferenceFlag()) |
|---|
| 639 | { |
|---|
| 640 | pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_N); |
|---|
| 641 | } |
|---|
| 642 | else |
|---|
| 643 | { |
|---|
| 644 | pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_R); |
|---|
| 645 | } |
|---|
| 646 | } |
|---|
| 647 | } |
|---|
| 648 | } |
|---|
| 649 | arrangeLongtermPicturesInRPS(pcSlice, rcListPic); |
|---|
| 650 | TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification(); |
|---|
| 651 | refPicListModification->setRefPicListModificationFlagL0(0); |
|---|
| 652 | refPicListModification->setRefPicListModificationFlagL1(0); |
|---|
| 653 | pcSlice->setNumRefIdx(REF_PIC_LIST_0,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures())); |
|---|
| 654 | pcSlice->setNumRefIdx(REF_PIC_LIST_1,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures())); |
|---|
| 655 | |
|---|
| 656 | #if REF_IDX_FRAMEWORK |
|---|
| 657 | if(m_layerId > 0) |
|---|
| 658 | { |
|---|
| 659 | if( pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA ) |
|---|
| 660 | { |
|---|
| 661 | pcSlice->setNumRefIdx(REF_PIC_LIST_0, pcSlice->getNumILRRefIdx()); |
|---|
| 662 | pcSlice->setNumRefIdx(REF_PIC_LIST_1, pcSlice->getNumILRRefIdx()); |
|---|
| 663 | } |
|---|
| 664 | else |
|---|
| 665 | { |
|---|
| 666 | pcSlice->setNumRefIdx(REF_PIC_LIST_0, pcSlice->getNumRefIdx(REF_PIC_LIST_0)+pcSlice->getNumILRRefIdx()); |
|---|
| 667 | pcSlice->setNumRefIdx(REF_PIC_LIST_1, pcSlice->getNumRefIdx(REF_PIC_LIST_1)+pcSlice->getNumILRRefIdx()); |
|---|
| 668 | } |
|---|
| 669 | } |
|---|
| 670 | #endif |
|---|
| 671 | |
|---|
| 672 | #if ADAPTIVE_QP_SELECTION |
|---|
| 673 | pcSlice->setTrQuant( m_pcEncTop->getTrQuant() ); |
|---|
| 674 | #endif |
|---|
| 675 | |
|---|
| 676 | #if SVC_EXTENSION |
|---|
| 677 | if(m_layerId > 0) |
|---|
| 678 | { |
|---|
| 679 | #if !IDR_ALIGNMENT |
|---|
| 680 | TComList<TComPic*> *cListPic = m_ppcTEncTop[m_layerId-1]->getListPic(); |
|---|
| 681 | pcSlice->setBaseColPic (*cListPic, m_layerId ); |
|---|
| 682 | #endif |
|---|
| 683 | #if SVC_UPSAMPLING |
|---|
| 684 | if ( pcPic->isSpatialEnhLayer()) |
|---|
| 685 | { |
|---|
| 686 | m_pcPredSearch->upsampleBasePic( pcPic->getFullPelBaseRec(), pcSlice->getBaseColPic()->getPicYuvRec(), pcPic->getPicYuvRec() ); |
|---|
| 687 | } |
|---|
| 688 | else |
|---|
| 689 | { |
|---|
| 690 | pcPic->setFullPelBaseRec( pcSlice->getBaseColPic()->getPicYuvRec() ); |
|---|
| 691 | } |
|---|
| 692 | pcSlice->setFullPelBaseRec ( pcPic->getFullPelBaseRec() ); |
|---|
| 693 | #endif |
|---|
| 694 | } |
|---|
| 695 | #endif |
|---|
| 696 | |
|---|
| 697 | #if REF_IDX_FRAMEWORK |
|---|
| 698 | if( pcSlice->getSliceType() == B_SLICE ) |
|---|
| 699 | { |
|---|
| 700 | pcSlice->setColFromL0Flag(1-uiColDir); |
|---|
| 701 | } |
|---|
| 702 | #endif |
|---|
| 703 | |
|---|
| 704 | // Set reference list |
|---|
| 705 | #if REF_IDX_FRAMEWORK |
|---|
| 706 | if(m_layerId == 0) |
|---|
| 707 | { |
|---|
| 708 | pcSlice->setRefPicList( rcListPic); |
|---|
| 709 | } |
|---|
| 710 | #else |
|---|
| 711 | pcSlice->setRefPicList ( rcListPic ); |
|---|
| 712 | #endif |
|---|
| 713 | #if REF_IDX_FRAMEWORK |
|---|
| 714 | if(m_layerId > 0) |
|---|
| 715 | { |
|---|
| 716 | m_pcEncTop->setILRPic(pcPic); |
|---|
| 717 | |
|---|
| 718 | #if REF_IDX_MFM |
|---|
| 719 | if( pcSlice->getSPS()->getMFMEnabledFlag() ) |
|---|
| 720 | { |
|---|
| 721 | pcSlice->setRefPOCListILP(m_pcEncTop->getIlpList(), pcSlice->getBaseColPic()); |
|---|
| 722 | } |
|---|
| 723 | #endif |
|---|
| 724 | pcSlice->setRefPicListModificationSvc(); |
|---|
| 725 | pcSlice->setRefPicList( rcListPic, false, m_pcEncTop->getIlpList()); |
|---|
| 726 | |
|---|
| 727 | #if REF_IDX_MFM |
|---|
| 728 | if( pcSlice->getSPS()->getMFMEnabledFlag() ) |
|---|
| 729 | { |
|---|
| 730 | Bool found = false; |
|---|
| 731 | UInt ColFromL0Flag = pcSlice->getColFromL0Flag(); |
|---|
| 732 | UInt ColRefIdx = pcSlice->getColRefIdx(); |
|---|
| 733 | for(Int colIdx = 0; colIdx < pcSlice->getNumRefIdx( RefPicList(1 - ColFromL0Flag) ); colIdx++) |
|---|
| 734 | { |
|---|
| 735 | if( pcSlice->getRefPic( RefPicList(1 - ColFromL0Flag), colIdx)->isILR(m_layerId) ) |
|---|
| 736 | { |
|---|
| 737 | ColRefIdx = colIdx; |
|---|
| 738 | found = true; |
|---|
| 739 | break; |
|---|
| 740 | } |
|---|
| 741 | } |
|---|
| 742 | |
|---|
| 743 | if( found == false ) |
|---|
| 744 | { |
|---|
| 745 | ColFromL0Flag = 1 - ColFromL0Flag; |
|---|
| 746 | for(Int colIdx = 0; colIdx < pcSlice->getNumRefIdx( RefPicList(1 - ColFromL0Flag) ); colIdx++) |
|---|
| 747 | { |
|---|
| 748 | if( pcSlice->getRefPic( RefPicList(1 - ColFromL0Flag), colIdx)->isILR(m_layerId) ) |
|---|
| 749 | { |
|---|
| 750 | ColRefIdx = colIdx; |
|---|
| 751 | found = true; |
|---|
| 752 | break; |
|---|
| 753 | } |
|---|
| 754 | } |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| 757 | if(found == true) |
|---|
| 758 | { |
|---|
| 759 | pcSlice->setColFromL0Flag(ColFromL0Flag); |
|---|
| 760 | pcSlice->setColRefIdx(ColRefIdx); |
|---|
| 761 | } |
|---|
| 762 | } |
|---|
| 763 | #endif |
|---|
| 764 | } |
|---|
| 765 | #endif |
|---|
| 766 | |
|---|
| 767 | // Slice info. refinement |
|---|
| 768 | if ( (pcSlice->getSliceType() == B_SLICE) && (pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0) ) |
|---|
| 769 | { |
|---|
| 770 | pcSlice->setSliceType ( P_SLICE ); |
|---|
| 771 | } |
|---|
| 772 | #if !L0034_COMBINED_LIST_CLEANUP |
|---|
| 773 | if (pcSlice->getSliceType() != B_SLICE || !pcSlice->getSPS()->getUseLComb()) |
|---|
| 774 | { |
|---|
| 775 | pcSlice->setNumRefIdx(REF_PIC_LIST_C, 0); |
|---|
| 776 | pcSlice->setRefPicListCombinationFlag(false); |
|---|
| 777 | pcSlice->setRefPicListModificationFlagLC(false); |
|---|
| 778 | } |
|---|
| 779 | else |
|---|
| 780 | { |
|---|
| 781 | pcSlice->setRefPicListCombinationFlag(pcSlice->getSPS()->getUseLComb()); |
|---|
| 782 | pcSlice->setNumRefIdx(REF_PIC_LIST_C, pcSlice->getNumRefIdx(REF_PIC_LIST_0)); |
|---|
| 783 | } |
|---|
| 784 | #endif |
|---|
| 785 | |
|---|
| 786 | if (pcSlice->getSliceType() == B_SLICE) |
|---|
| 787 | { |
|---|
| 788 | #if !REF_IDX_FRAMEWORK |
|---|
| 789 | pcSlice->setColFromL0Flag(1-uiColDir); |
|---|
| 790 | #endif |
|---|
| 791 | Bool bLowDelay = true; |
|---|
| 792 | Int iCurrPOC = pcSlice->getPOC(); |
|---|
| 793 | Int iRefIdx = 0; |
|---|
| 794 | |
|---|
| 795 | for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++) |
|---|
| 796 | { |
|---|
| 797 | if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC ) |
|---|
| 798 | { |
|---|
| 799 | bLowDelay = false; |
|---|
| 800 | } |
|---|
| 801 | } |
|---|
| 802 | for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++) |
|---|
| 803 | { |
|---|
| 804 | if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC ) |
|---|
| 805 | { |
|---|
| 806 | bLowDelay = false; |
|---|
| 807 | } |
|---|
| 808 | } |
|---|
| 809 | |
|---|
| 810 | pcSlice->setCheckLDC(bLowDelay); |
|---|
| 811 | } |
|---|
| 812 | else |
|---|
| 813 | { |
|---|
| 814 | pcSlice->setCheckLDC(true); |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | uiColDir = 1-uiColDir; |
|---|
| 818 | |
|---|
| 819 | //------------------------------------------------------------- |
|---|
| 820 | pcSlice->setRefPOCList(); |
|---|
| 821 | |
|---|
| 822 | #if L0034_COMBINED_LIST_CLEANUP |
|---|
| 823 | pcSlice->setList1IdxToList0Idx(); |
|---|
| 824 | #else |
|---|
| 825 | pcSlice->setNoBackPredFlag( false ); |
|---|
| 826 | if ( pcSlice->getSliceType() == B_SLICE && !pcSlice->getRefPicListCombinationFlag()) |
|---|
| 827 | { |
|---|
| 828 | if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) ) |
|---|
| 829 | { |
|---|
| 830 | pcSlice->setNoBackPredFlag( true ); |
|---|
| 831 | Int i; |
|---|
| 832 | for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ ) |
|---|
| 833 | { |
|---|
| 834 | if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) ) |
|---|
| 835 | { |
|---|
| 836 | pcSlice->setNoBackPredFlag( false ); |
|---|
| 837 | break; |
|---|
| 838 | } |
|---|
| 839 | } |
|---|
| 840 | } |
|---|
| 841 | } |
|---|
| 842 | |
|---|
| 843 | if(pcSlice->getNoBackPredFlag()) |
|---|
| 844 | { |
|---|
| 845 | pcSlice->setNumRefIdx(REF_PIC_LIST_C, 0); |
|---|
| 846 | } |
|---|
| 847 | pcSlice->generateCombinedList(); |
|---|
| 848 | #endif |
|---|
| 849 | |
|---|
| 850 | if (m_pcEncTop->getTMVPModeId() == 2) |
|---|
| 851 | { |
|---|
| 852 | if (iGOPid == 0) // first picture in SOP (i.e. forward B) |
|---|
| 853 | { |
|---|
| 854 | pcSlice->setEnableTMVPFlag(0); |
|---|
| 855 | } |
|---|
| 856 | else |
|---|
| 857 | { |
|---|
| 858 | // Note: pcSlice->getColFromL0Flag() is assumed to be always 0 and getcolRefIdx() is always 0. |
|---|
| 859 | pcSlice->setEnableTMVPFlag(1); |
|---|
| 860 | } |
|---|
| 861 | pcSlice->getSPS()->setTMVPFlagsPresent(1); |
|---|
| 862 | } |
|---|
| 863 | else if (m_pcEncTop->getTMVPModeId() == 1) |
|---|
| 864 | { |
|---|
| 865 | pcSlice->getSPS()->setTMVPFlagsPresent(1); |
|---|
| 866 | pcSlice->setEnableTMVPFlag(1); |
|---|
| 867 | } |
|---|
| 868 | else |
|---|
| 869 | { |
|---|
| 870 | pcSlice->getSPS()->setTMVPFlagsPresent(0); |
|---|
| 871 | pcSlice->setEnableTMVPFlag(0); |
|---|
| 872 | } |
|---|
| 873 | /////////////////////////////////////////////////////////////////////////////////////////////////// Compress a slice |
|---|
| 874 | // Slice compression |
|---|
| 875 | if (m_pcCfg->getUseASR()) |
|---|
| 876 | { |
|---|
| 877 | m_pcSliceEncoder->setSearchRange(pcSlice); |
|---|
| 878 | } |
|---|
| 879 | |
|---|
| 880 | Bool bGPBcheck=false; |
|---|
| 881 | if ( pcSlice->getSliceType() == B_SLICE) |
|---|
| 882 | { |
|---|
| 883 | if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) ) |
|---|
| 884 | { |
|---|
| 885 | bGPBcheck=true; |
|---|
| 886 | Int i; |
|---|
| 887 | for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ ) |
|---|
| 888 | { |
|---|
| 889 | if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) ) |
|---|
| 890 | { |
|---|
| 891 | bGPBcheck=false; |
|---|
| 892 | break; |
|---|
| 893 | } |
|---|
| 894 | } |
|---|
| 895 | } |
|---|
| 896 | } |
|---|
| 897 | if(bGPBcheck) |
|---|
| 898 | { |
|---|
| 899 | pcSlice->setMvdL1ZeroFlag(true); |
|---|
| 900 | } |
|---|
| 901 | else |
|---|
| 902 | { |
|---|
| 903 | pcSlice->setMvdL1ZeroFlag(false); |
|---|
| 904 | } |
|---|
| 905 | pcPic->getSlice(pcSlice->getSliceIdx())->setMvdL1ZeroFlag(pcSlice->getMvdL1ZeroFlag()); |
|---|
| 906 | |
|---|
| 907 | #if RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 908 | Int sliceQP = pcSlice->getSliceQp(); |
|---|
| 909 | Double lambda = 0.0; |
|---|
| 910 | Int actualHeadBits = 0; |
|---|
| 911 | Int actualTotalBits = 0; |
|---|
| 912 | Int estimatedBits = 0; |
|---|
| 913 | Int tmpBitsBeforeWriting = 0; |
|---|
| 914 | if ( m_pcCfg->getUseRateCtrl() ) |
|---|
| 915 | { |
|---|
| 916 | Int frameLevel = m_pcRateCtrl->getRCSeq()->getGOPID2Level( iGOPid ); |
|---|
| 917 | if ( pcPic->getSlice(0)->getSliceType() == I_SLICE ) |
|---|
| 918 | { |
|---|
| 919 | frameLevel = 0; |
|---|
| 920 | } |
|---|
| 921 | m_pcRateCtrl->initRCPic( frameLevel ); |
|---|
| 922 | estimatedBits = m_pcRateCtrl->getRCPic()->getTargetBits(); |
|---|
| 923 | |
|---|
| 924 | if ( ( pcSlice->getPOC() == 0 && m_pcCfg->getInitialQP() > 0 ) || ( frameLevel == 0 && m_pcCfg->getForceIntraQP() ) ) // QP is specified |
|---|
| 925 | { |
|---|
| 926 | sliceQP = m_pcCfg->getInitialQP(); |
|---|
| 927 | Int NumberBFrames = ( m_pcCfg->getGOPSize() - 1 ); |
|---|
| 928 | Double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(Double)NumberBFrames ); |
|---|
| 929 | Double dQPFactor = 0.57*dLambda_scale; |
|---|
| 930 | Int SHIFT_QP = 12; |
|---|
| 931 | Int bitdepth_luma_qp_scale = 0; |
|---|
| 932 | Double qp_temp = (Double) sliceQP + bitdepth_luma_qp_scale - SHIFT_QP; |
|---|
| 933 | lambda = dQPFactor*pow( 2.0, qp_temp/3.0 ); |
|---|
| 934 | } |
|---|
| 935 | else if ( frameLevel == 0 ) // intra case, but use the model |
|---|
| 936 | { |
|---|
| 937 | if ( m_pcCfg->getIntraPeriod() != 1 ) // do not refine allocated bits for all intra case |
|---|
| 938 | { |
|---|
| 939 | Int bits = m_pcRateCtrl->getRCSeq()->getLeftAverageBits(); |
|---|
| 940 | bits = m_pcRateCtrl->getRCSeq()->getRefineBitsForIntra( bits ); |
|---|
| 941 | if ( bits < 200 ) |
|---|
| 942 | { |
|---|
| 943 | bits = 200; |
|---|
| 944 | } |
|---|
| 945 | m_pcRateCtrl->getRCPic()->setTargetBits( bits ); |
|---|
| 946 | } |
|---|
| 947 | |
|---|
| 948 | list<TEncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList(); |
|---|
| 949 | lambda = m_pcRateCtrl->getRCPic()->estimatePicLambda( listPreviousPicture ); |
|---|
| 950 | sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture ); |
|---|
| 951 | } |
|---|
| 952 | else // normal case |
|---|
| 953 | { |
|---|
| 954 | list<TEncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList(); |
|---|
| 955 | lambda = m_pcRateCtrl->getRCPic()->estimatePicLambda( listPreviousPicture ); |
|---|
| 956 | sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture ); |
|---|
| 957 | } |
|---|
| 958 | |
|---|
| 959 | sliceQP = Clip3( -pcSlice->getSPS()->getQpBDOffsetY(), MAX_QP, sliceQP ); |
|---|
| 960 | m_pcRateCtrl->getRCPic()->setPicEstQP( sliceQP ); |
|---|
| 961 | |
|---|
| 962 | m_pcSliceEncoder->resetQP( pcPic, sliceQP, lambda ); |
|---|
| 963 | } |
|---|
| 964 | #endif |
|---|
| 965 | |
|---|
| 966 | UInt uiNumSlices = 1; |
|---|
| 967 | |
|---|
| 968 | #if SIMPLIFIED_MV_POS_SCALING |
|---|
| 969 | if (m_layerId > 0) |
|---|
| 970 | { |
|---|
| 971 | const Window &confBL = pcSlice->getBaseColPic()->getPicYuvRec()->getConformanceWindow(); |
|---|
| 972 | const Window &confEL = pcPic->getPicYuvRec()->getConformanceWindow(); |
|---|
| 973 | |
|---|
| 974 | Int widthBL = pcSlice->getBaseColPic()->getPicYuvRec()->getWidth () - confBL.getWindowLeftOffset() - confBL.getWindowRightOffset(); |
|---|
| 975 | Int heightBL = pcSlice->getBaseColPic()->getPicYuvRec()->getHeight() - confBL.getWindowTopOffset() - confBL.getWindowBottomOffset(); |
|---|
| 976 | |
|---|
| 977 | Int widthEL = pcPic->getPicYuvRec()->getWidth() - confEL.getWindowLeftOffset() - confEL.getWindowRightOffset(); |
|---|
| 978 | Int heightEL = pcPic->getPicYuvRec()->getHeight() - confEL.getWindowTopOffset() - confEL.getWindowBottomOffset(); |
|---|
| 979 | |
|---|
| 980 | g_mvScalingFactor[m_layerId][0] = Clip3(-4096, 4095, ((widthEL << 8) + (widthBL >> 1)) / widthBL); |
|---|
| 981 | g_mvScalingFactor[m_layerId][1] = Clip3(-4096, 4095, ((heightEL << 8) + (heightBL >> 1)) / heightBL); |
|---|
| 982 | |
|---|
| 983 | g_posScalingFactor[m_layerId][0] = ((widthBL << 16) + (widthEL >> 1)) / widthEL; |
|---|
| 984 | g_posScalingFactor[m_layerId][1] = ((heightBL << 16) + (heightEL >> 1)) / heightEL; |
|---|
| 985 | } |
|---|
| 986 | #endif |
|---|
| 987 | |
|---|
| 988 | UInt uiInternalAddress = pcPic->getNumPartInCU()-4; |
|---|
| 989 | UInt uiExternalAddress = pcPic->getPicSym()->getNumberOfCUsInFrame()-1; |
|---|
| 990 | UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
|---|
| 991 | UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
|---|
| 992 | UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples(); |
|---|
| 993 | UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples(); |
|---|
| 994 | while(uiPosX>=uiWidth||uiPosY>=uiHeight) |
|---|
| 995 | { |
|---|
| 996 | uiInternalAddress--; |
|---|
| 997 | uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
|---|
| 998 | uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
|---|
| 999 | } |
|---|
| 1000 | uiInternalAddress++; |
|---|
| 1001 | if(uiInternalAddress==pcPic->getNumPartInCU()) |
|---|
| 1002 | { |
|---|
| 1003 | uiInternalAddress = 0; |
|---|
| 1004 | uiExternalAddress++; |
|---|
| 1005 | } |
|---|
| 1006 | UInt uiRealEndAddress = uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress; |
|---|
| 1007 | |
|---|
| 1008 | UInt uiCummulativeTileWidth; |
|---|
| 1009 | UInt uiCummulativeTileHeight; |
|---|
| 1010 | Int p, j; |
|---|
| 1011 | UInt uiEncCUAddr; |
|---|
| 1012 | |
|---|
| 1013 | //set NumColumnsMinus1 and NumRowsMinus1 |
|---|
| 1014 | pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() ); |
|---|
| 1015 | pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() ); |
|---|
| 1016 | |
|---|
| 1017 | //create the TComTileArray |
|---|
| 1018 | pcPic->getPicSym()->xCreateTComTileArray(); |
|---|
| 1019 | |
|---|
| 1020 | if( pcSlice->getPPS()->getUniformSpacingFlag() == 1 ) |
|---|
| 1021 | { |
|---|
| 1022 | //set the width for each tile |
|---|
| 1023 | for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++) |
|---|
| 1024 | { |
|---|
| 1025 | for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++) |
|---|
| 1026 | { |
|---|
| 1027 | pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )-> |
|---|
| 1028 | setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) |
|---|
| 1029 | - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) ); |
|---|
| 1030 | } |
|---|
| 1031 | } |
|---|
| 1032 | |
|---|
| 1033 | //set the height for each tile |
|---|
| 1034 | for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++) |
|---|
| 1035 | { |
|---|
| 1036 | for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++) |
|---|
| 1037 | { |
|---|
| 1038 | pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )-> |
|---|
| 1039 | setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) |
|---|
| 1040 | - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) ); |
|---|
| 1041 | } |
|---|
| 1042 | } |
|---|
| 1043 | } |
|---|
| 1044 | else |
|---|
| 1045 | { |
|---|
| 1046 | //set the width for each tile |
|---|
| 1047 | for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++) |
|---|
| 1048 | { |
|---|
| 1049 | uiCummulativeTileWidth = 0; |
|---|
| 1050 | for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1(); p++) |
|---|
| 1051 | { |
|---|
| 1052 | pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->setTileWidth( pcSlice->getPPS()->getColumnWidth(p) ); |
|---|
| 1053 | uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(p); |
|---|
| 1054 | } |
|---|
| 1055 | pcPic->getPicSym()->getTComTile(j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth ); |
|---|
| 1056 | } |
|---|
| 1057 | |
|---|
| 1058 | //set the height for each tile |
|---|
| 1059 | for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++) |
|---|
| 1060 | { |
|---|
| 1061 | uiCummulativeTileHeight = 0; |
|---|
| 1062 | for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1(); p++) |
|---|
| 1063 | { |
|---|
| 1064 | pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->setTileHeight( pcSlice->getPPS()->getRowHeight(p) ); |
|---|
| 1065 | uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(p); |
|---|
| 1066 | } |
|---|
| 1067 | pcPic->getPicSym()->getTComTile(p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight ); |
|---|
| 1068 | } |
|---|
| 1069 | } |
|---|
| 1070 | //intialize each tile of the current picture |
|---|
| 1071 | pcPic->getPicSym()->xInitTiles(); |
|---|
| 1072 | |
|---|
| 1073 | // Allocate some coders, now we know how many tiles there are. |
|---|
| 1074 | Int iNumSubstreams = pcSlice->getPPS()->getNumSubstreams(); |
|---|
| 1075 | |
|---|
| 1076 | //generate the Coding Order Map and Inverse Coding Order Map |
|---|
| 1077 | for(p=0, uiEncCUAddr=0; p<pcPic->getPicSym()->getNumberOfCUsInFrame(); p++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr)) |
|---|
| 1078 | { |
|---|
| 1079 | pcPic->getPicSym()->setCUOrderMap(p, uiEncCUAddr); |
|---|
| 1080 | pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, p); |
|---|
| 1081 | } |
|---|
| 1082 | pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame()); |
|---|
| 1083 | pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame()); |
|---|
| 1084 | |
|---|
| 1085 | // Allocate some coders, now we know how many tiles there are. |
|---|
| 1086 | m_pcEncTop->createWPPCoders(iNumSubstreams); |
|---|
| 1087 | pcSbacCoders = m_pcEncTop->getSbacCoders(); |
|---|
| 1088 | pcSubstreamsOut = new TComOutputBitstream[iNumSubstreams]; |
|---|
| 1089 | |
|---|
| 1090 | UInt startCUAddrSliceIdx = 0; // used to index "m_uiStoredStartCUAddrForEncodingSlice" containing locations of slice boundaries |
|---|
| 1091 | UInt startCUAddrSlice = 0; // used to keep track of current slice's starting CU addr. |
|---|
| 1092 | pcSlice->setSliceCurStartCUAddr( startCUAddrSlice ); // Setting "start CU addr" for current slice |
|---|
| 1093 | m_storedStartCUAddrForEncodingSlice.clear(); |
|---|
| 1094 | |
|---|
| 1095 | UInt startCUAddrSliceSegmentIdx = 0; // used to index "m_uiStoredStartCUAddrForEntropyEncodingSlice" containing locations of slice boundaries |
|---|
| 1096 | UInt startCUAddrSliceSegment = 0; // used to keep track of current Dependent slice's starting CU addr. |
|---|
| 1097 | pcSlice->setSliceSegmentCurStartCUAddr( startCUAddrSliceSegment ); // Setting "start CU addr" for current Dependent slice |
|---|
| 1098 | |
|---|
| 1099 | m_storedStartCUAddrForEncodingSliceSegment.clear(); |
|---|
| 1100 | UInt nextCUAddr = 0; |
|---|
| 1101 | m_storedStartCUAddrForEncodingSlice.push_back (nextCUAddr); |
|---|
| 1102 | startCUAddrSliceIdx++; |
|---|
| 1103 | m_storedStartCUAddrForEncodingSliceSegment.push_back(nextCUAddr); |
|---|
| 1104 | startCUAddrSliceSegmentIdx++; |
|---|
| 1105 | #if AVC_BASE |
|---|
| 1106 | if( m_layerId == 0 && m_pcEncTop->getVPS()->getAvcBaseLayerFlag() ) |
|---|
| 1107 | { |
|---|
| 1108 | pcPic->getPicYuvOrg()->copyToPic( pcPic->getPicYuvRec() ); |
|---|
| 1109 | #if AVC_SYNTAX |
|---|
| 1110 | pcPic->readBLSyntax( m_ppcTEncTop[0]->getBLSyntaxFile(), SYNTAX_BYTES ); |
|---|
| 1111 | #endif |
|---|
| 1112 | return; |
|---|
| 1113 | } |
|---|
| 1114 | #endif |
|---|
| 1115 | |
|---|
| 1116 | while(nextCUAddr<uiRealEndAddress) // determine slice boundaries |
|---|
| 1117 | { |
|---|
| 1118 | pcSlice->setNextSlice ( false ); |
|---|
| 1119 | pcSlice->setNextSliceSegment( false ); |
|---|
| 1120 | assert(pcPic->getNumAllocatedSlice() == startCUAddrSliceIdx); |
|---|
| 1121 | m_pcSliceEncoder->precompressSlice( pcPic ); |
|---|
| 1122 | m_pcSliceEncoder->compressSlice ( pcPic ); |
|---|
| 1123 | |
|---|
| 1124 | Bool bNoBinBitConstraintViolated = (!pcSlice->isNextSlice() && !pcSlice->isNextSliceSegment()); |
|---|
| 1125 | if (pcSlice->isNextSlice() || (bNoBinBitConstraintViolated && m_pcCfg->getSliceMode()==FIXED_NUMBER_OF_LCU)) |
|---|
| 1126 | { |
|---|
| 1127 | startCUAddrSlice = pcSlice->getSliceCurEndCUAddr(); |
|---|
| 1128 | // Reconstruction slice |
|---|
| 1129 | m_storedStartCUAddrForEncodingSlice.push_back(startCUAddrSlice); |
|---|
| 1130 | startCUAddrSliceIdx++; |
|---|
| 1131 | // Dependent slice |
|---|
| 1132 | if (startCUAddrSliceSegmentIdx>0 && m_storedStartCUAddrForEncodingSliceSegment[startCUAddrSliceSegmentIdx-1] != startCUAddrSlice) |
|---|
| 1133 | { |
|---|
| 1134 | m_storedStartCUAddrForEncodingSliceSegment.push_back(startCUAddrSlice); |
|---|
| 1135 | startCUAddrSliceSegmentIdx++; |
|---|
| 1136 | } |
|---|
| 1137 | |
|---|
| 1138 | if (startCUAddrSlice < uiRealEndAddress) |
|---|
| 1139 | { |
|---|
| 1140 | pcPic->allocateNewSlice(); |
|---|
| 1141 | pcPic->setCurrSliceIdx ( startCUAddrSliceIdx-1 ); |
|---|
| 1142 | m_pcSliceEncoder->setSliceIdx ( startCUAddrSliceIdx-1 ); |
|---|
| 1143 | pcSlice = pcPic->getSlice ( startCUAddrSliceIdx-1 ); |
|---|
| 1144 | pcSlice->copySliceInfo ( pcPic->getSlice(0) ); |
|---|
| 1145 | pcSlice->setSliceIdx ( startCUAddrSliceIdx-1 ); |
|---|
| 1146 | pcSlice->setSliceCurStartCUAddr ( startCUAddrSlice ); |
|---|
| 1147 | pcSlice->setSliceSegmentCurStartCUAddr ( startCUAddrSlice ); |
|---|
| 1148 | pcSlice->setSliceBits(0); |
|---|
| 1149 | uiNumSlices ++; |
|---|
| 1150 | } |
|---|
| 1151 | } |
|---|
| 1152 | else if (pcSlice->isNextSliceSegment() || (bNoBinBitConstraintViolated && m_pcCfg->getSliceSegmentMode()==FIXED_NUMBER_OF_LCU)) |
|---|
| 1153 | { |
|---|
| 1154 | startCUAddrSliceSegment = pcSlice->getSliceSegmentCurEndCUAddr(); |
|---|
| 1155 | m_storedStartCUAddrForEncodingSliceSegment.push_back(startCUAddrSliceSegment); |
|---|
| 1156 | startCUAddrSliceSegmentIdx++; |
|---|
| 1157 | pcSlice->setSliceSegmentCurStartCUAddr( startCUAddrSliceSegment ); |
|---|
| 1158 | } |
|---|
| 1159 | else |
|---|
| 1160 | { |
|---|
| 1161 | startCUAddrSlice = pcSlice->getSliceCurEndCUAddr(); |
|---|
| 1162 | startCUAddrSliceSegment = pcSlice->getSliceSegmentCurEndCUAddr(); |
|---|
| 1163 | } |
|---|
| 1164 | |
|---|
| 1165 | nextCUAddr = (startCUAddrSlice > startCUAddrSliceSegment) ? startCUAddrSlice : startCUAddrSliceSegment; |
|---|
| 1166 | } |
|---|
| 1167 | m_storedStartCUAddrForEncodingSlice.push_back( pcSlice->getSliceCurEndCUAddr()); |
|---|
| 1168 | startCUAddrSliceIdx++; |
|---|
| 1169 | m_storedStartCUAddrForEncodingSliceSegment.push_back(pcSlice->getSliceCurEndCUAddr()); |
|---|
| 1170 | startCUAddrSliceSegmentIdx++; |
|---|
| 1171 | |
|---|
| 1172 | pcSlice = pcPic->getSlice(0); |
|---|
| 1173 | |
|---|
| 1174 | // SAO parameter estimation using non-deblocked pixels for LCU bottom and right boundary areas |
|---|
| 1175 | if( m_pcCfg->getSaoLcuBasedOptimization() && m_pcCfg->getSaoLcuBoundary() ) |
|---|
| 1176 | { |
|---|
| 1177 | m_pcSAO->resetStats(); |
|---|
| 1178 | m_pcSAO->calcSaoStatsCu_BeforeDblk( pcPic ); |
|---|
| 1179 | } |
|---|
| 1180 | |
|---|
| 1181 | //-- Loop filter |
|---|
| 1182 | Bool bLFCrossTileBoundary = pcSlice->getPPS()->getLoopFilterAcrossTilesEnabledFlag(); |
|---|
| 1183 | m_pcLoopFilter->setCfg(bLFCrossTileBoundary); |
|---|
| 1184 | #if L0386_DB_METRIC |
|---|
| 1185 | if ( m_pcCfg->getDeblockingFilterMetric() ) |
|---|
| 1186 | { |
|---|
| 1187 | dblMetric(pcPic, uiNumSlices); |
|---|
| 1188 | } |
|---|
| 1189 | #endif |
|---|
| 1190 | m_pcLoopFilter->loopFilterPic( pcPic ); |
|---|
| 1191 | |
|---|
| 1192 | pcSlice = pcPic->getSlice(0); |
|---|
| 1193 | if(pcSlice->getSPS()->getUseSAO()) |
|---|
| 1194 | { |
|---|
| 1195 | std::vector<Bool> LFCrossSliceBoundaryFlag; |
|---|
| 1196 | for(Int s=0; s< uiNumSlices; s++) |
|---|
| 1197 | { |
|---|
| 1198 | LFCrossSliceBoundaryFlag.push_back( ((uiNumSlices==1)?true:pcPic->getSlice(s)->getLFCrossSliceBoundaryFlag()) ); |
|---|
| 1199 | } |
|---|
| 1200 | m_storedStartCUAddrForEncodingSlice.resize(uiNumSlices+1); |
|---|
| 1201 | pcPic->createNonDBFilterInfo(m_storedStartCUAddrForEncodingSlice, 0, &LFCrossSliceBoundaryFlag ,pcPic->getPicSym()->getNumTiles() ,bLFCrossTileBoundary); |
|---|
| 1202 | } |
|---|
| 1203 | |
|---|
| 1204 | |
|---|
| 1205 | pcSlice = pcPic->getSlice(0); |
|---|
| 1206 | |
|---|
| 1207 | if(pcSlice->getSPS()->getUseSAO()) |
|---|
| 1208 | { |
|---|
| 1209 | m_pcSAO->createPicSaoInfo(pcPic); |
|---|
| 1210 | } |
|---|
| 1211 | |
|---|
| 1212 | /////////////////////////////////////////////////////////////////////////////////////////////////// File writing |
|---|
| 1213 | // Set entropy coder |
|---|
| 1214 | m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice ); |
|---|
| 1215 | |
|---|
| 1216 | /* write various header sets. */ |
|---|
| 1217 | if ( m_bSeqFirst ) |
|---|
| 1218 | { |
|---|
| 1219 | #if SVC_EXTENSION |
|---|
| 1220 | OutputNALUnit nalu(NAL_UNIT_VPS, 0, m_layerId); |
|---|
| 1221 | #if AVC_BASE |
|---|
| 1222 | if( ( m_layerId == 1 && m_pcEncTop->getVPS()->getAvcBaseLayerFlag() ) || ( m_layerId == 0 && !m_pcEncTop->getVPS()->getAvcBaseLayerFlag() ) ) |
|---|
| 1223 | #else |
|---|
| 1224 | if( m_layerId == 0 ) |
|---|
| 1225 | #endif |
|---|
| 1226 | { |
|---|
| 1227 | #else |
|---|
| 1228 | OutputNALUnit nalu(NAL_UNIT_VPS); |
|---|
| 1229 | #endif |
|---|
| 1230 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1231 | m_pcEntropyCoder->encodeVPS(m_pcEncTop->getVPS()); |
|---|
| 1232 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1233 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1234 | #if RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 1235 | actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8; |
|---|
| 1236 | #endif |
|---|
| 1237 | #if SVC_EXTENSION |
|---|
| 1238 | } |
|---|
| 1239 | #endif |
|---|
| 1240 | |
|---|
| 1241 | #if SVC_EXTENSION |
|---|
| 1242 | nalu = NALUnit(NAL_UNIT_SPS, 0, m_layerId); |
|---|
| 1243 | #else |
|---|
| 1244 | nalu = NALUnit(NAL_UNIT_SPS); |
|---|
| 1245 | #endif |
|---|
| 1246 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1247 | if (m_bSeqFirst) |
|---|
| 1248 | { |
|---|
| 1249 | pcSlice->getSPS()->setNumLongTermRefPicSPS(m_numLongTermRefPicSPS); |
|---|
| 1250 | for (Int k = 0; k < m_numLongTermRefPicSPS; k++) |
|---|
| 1251 | { |
|---|
| 1252 | pcSlice->getSPS()->setLtRefPicPocLsbSps(k, m_ltRefPicPocLsbSps[k]); |
|---|
| 1253 | pcSlice->getSPS()->setUsedByCurrPicLtSPSFlag(k, m_ltRefPicUsedByCurrPicFlag[k]); |
|---|
| 1254 | } |
|---|
| 1255 | } |
|---|
| 1256 | if( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) |
|---|
| 1257 | { |
|---|
| 1258 | UInt maxCU = m_pcCfg->getSliceArgument() >> ( pcSlice->getSPS()->getMaxCUDepth() << 1); |
|---|
| 1259 | UInt numDU = ( m_pcCfg->getSliceMode() == 1 ) ? ( pcPic->getNumCUsInFrame() / maxCU ) : ( 0 ); |
|---|
| 1260 | if( pcPic->getNumCUsInFrame() % maxCU != 0 ) |
|---|
| 1261 | { |
|---|
| 1262 | numDU ++; |
|---|
| 1263 | } |
|---|
| 1264 | pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->setNumDU( numDU ); |
|---|
| 1265 | pcSlice->getSPS()->setHrdParameters( m_pcCfg->getFrameRate(), numDU, m_pcCfg->getTargetBitrate(), ( m_pcCfg->getIntraPeriod() > 0 ) ); |
|---|
| 1266 | } |
|---|
| 1267 | if( m_pcCfg->getBufferingPeriodSEIEnabled() || m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) |
|---|
| 1268 | { |
|---|
| 1269 | pcSlice->getSPS()->getVuiParameters()->setHrdParametersPresentFlag( true ); |
|---|
| 1270 | } |
|---|
| 1271 | m_pcEntropyCoder->encodeSPS(pcSlice->getSPS()); |
|---|
| 1272 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1273 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1274 | #if RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 1275 | actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8; |
|---|
| 1276 | #endif |
|---|
| 1277 | |
|---|
| 1278 | #if SVC_EXTENSION |
|---|
| 1279 | nalu = NALUnit(NAL_UNIT_PPS, 0, m_layerId); |
|---|
| 1280 | #else |
|---|
| 1281 | nalu = NALUnit(NAL_UNIT_PPS); |
|---|
| 1282 | #endif |
|---|
| 1283 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1284 | m_pcEntropyCoder->encodePPS(pcSlice->getPPS()); |
|---|
| 1285 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1286 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1287 | #if RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 1288 | actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8; |
|---|
| 1289 | #endif |
|---|
| 1290 | |
|---|
| 1291 | xCreateLeadingSEIMessages(accessUnit, pcSlice->getSPS()); |
|---|
| 1292 | |
|---|
| 1293 | m_bSeqFirst = false; |
|---|
| 1294 | } |
|---|
| 1295 | |
|---|
| 1296 | #if L0208_SOP_DESCRIPTION_SEI |
|---|
| 1297 | if (writeSOP) // write SOP description SEI (if enabled) at the beginning of GOP |
|---|
| 1298 | { |
|---|
| 1299 | Int SOPcurrPOC = pocCurr; |
|---|
| 1300 | |
|---|
| 1301 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI); |
|---|
| 1302 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 1303 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1304 | |
|---|
| 1305 | SEISOPDescription SOPDescriptionSEI; |
|---|
| 1306 | SOPDescriptionSEI.m_sopSeqParameterSetId = pcSlice->getSPS()->getSPSId(); |
|---|
| 1307 | |
|---|
| 1308 | UInt i = 0; |
|---|
| 1309 | UInt prevEntryId = iGOPid; |
|---|
| 1310 | for (j = iGOPid; j < m_iGopSize; j++) |
|---|
| 1311 | { |
|---|
| 1312 | Int deltaPOC = m_pcCfg->getGOPEntry(j).m_POC - m_pcCfg->getGOPEntry(prevEntryId).m_POC; |
|---|
| 1313 | if ((SOPcurrPOC + deltaPOC) < m_pcCfg->getFramesToBeEncoded()) |
|---|
| 1314 | { |
|---|
| 1315 | SOPcurrPOC += deltaPOC; |
|---|
| 1316 | SOPDescriptionSEI.m_sopDescVclNaluType[i] = getNalUnitType(SOPcurrPOC, m_iLastIDR); |
|---|
| 1317 | SOPDescriptionSEI.m_sopDescTemporalId[i] = m_pcCfg->getGOPEntry(j).m_temporalId; |
|---|
| 1318 | SOPDescriptionSEI.m_sopDescStRpsIdx[i] = m_pcEncTop->getReferencePictureSetIdxForSOP(pcSlice, SOPcurrPOC, j); |
|---|
| 1319 | SOPDescriptionSEI.m_sopDescPocDelta[i] = deltaPOC; |
|---|
| 1320 | |
|---|
| 1321 | prevEntryId = j; |
|---|
| 1322 | i++; |
|---|
| 1323 | } |
|---|
| 1324 | } |
|---|
| 1325 | |
|---|
| 1326 | SOPDescriptionSEI.m_numPicsInSopMinus1 = i - 1; |
|---|
| 1327 | |
|---|
| 1328 | m_seiWriter.writeSEImessage( nalu.m_Bitstream, SOPDescriptionSEI, pcSlice->getSPS()); |
|---|
| 1329 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1330 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1331 | |
|---|
| 1332 | writeSOP = false; |
|---|
| 1333 | } |
|---|
| 1334 | #endif |
|---|
| 1335 | |
|---|
| 1336 | if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) && |
|---|
| 1337 | ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) && |
|---|
| 1338 | ( ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNalHrdParametersPresentFlag() ) |
|---|
| 1339 | || ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getVclHrdParametersPresentFlag() ) ) ) |
|---|
| 1340 | { |
|---|
| 1341 | if( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getSubPicCpbParamsPresentFlag() ) |
|---|
| 1342 | { |
|---|
| 1343 | UInt numDU = pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNumDU(); |
|---|
| 1344 | pictureTimingSEI.m_numDecodingUnitsMinus1 = ( numDU - 1 ); |
|---|
| 1345 | pictureTimingSEI.m_duCommonCpbRemovalDelayFlag = false; |
|---|
| 1346 | |
|---|
| 1347 | if( pictureTimingSEI.m_numNalusInDuMinus1 == NULL ) |
|---|
| 1348 | { |
|---|
| 1349 | pictureTimingSEI.m_numNalusInDuMinus1 = new UInt[ numDU ]; |
|---|
| 1350 | } |
|---|
| 1351 | if( pictureTimingSEI.m_duCpbRemovalDelayMinus1 == NULL ) |
|---|
| 1352 | { |
|---|
| 1353 | pictureTimingSEI.m_duCpbRemovalDelayMinus1 = new UInt[ numDU ]; |
|---|
| 1354 | } |
|---|
| 1355 | if( accumBitsDU == NULL ) |
|---|
| 1356 | { |
|---|
| 1357 | accumBitsDU = new UInt[ numDU ]; |
|---|
| 1358 | } |
|---|
| 1359 | if( accumNalsDU == NULL ) |
|---|
| 1360 | { |
|---|
| 1361 | accumNalsDU = new UInt[ numDU ]; |
|---|
| 1362 | } |
|---|
| 1363 | } |
|---|
| 1364 | pictureTimingSEI.m_auCpbRemovalDelay = std::max<Int>(1, m_totalCoded - m_lastBPSEI); // Syntax element signalled as minus, hence the . |
|---|
| 1365 | pictureTimingSEI.m_picDpbOutputDelay = pcSlice->getSPS()->getNumReorderPics(0) + pcSlice->getPOC() - m_totalCoded; |
|---|
| 1366 | #if L0044_DU_DPB_OUTPUT_DELAY_HRD |
|---|
| 1367 | Int factor = pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getTickDivisorMinus2() + 2; |
|---|
| 1368 | pictureTimingSEI.m_picDpbOutputDuDelay = factor * pictureTimingSEI.m_picDpbOutputDelay; |
|---|
| 1369 | if( m_pcCfg->getDecodingUnitInfoSEIEnabled() ) |
|---|
| 1370 | { |
|---|
| 1371 | picSptDpbOutputDuDelay = factor * pictureTimingSEI.m_picDpbOutputDelay; |
|---|
| 1372 | } |
|---|
| 1373 | #endif |
|---|
| 1374 | } |
|---|
| 1375 | |
|---|
| 1376 | if( ( m_pcCfg->getBufferingPeriodSEIEnabled() ) && ( pcSlice->getSliceType() == I_SLICE ) && |
|---|
| 1377 | ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) && |
|---|
| 1378 | ( ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNalHrdParametersPresentFlag() ) |
|---|
| 1379 | || ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getVclHrdParametersPresentFlag() ) ) ) |
|---|
| 1380 | { |
|---|
| 1381 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI); |
|---|
| 1382 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 1383 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1384 | |
|---|
| 1385 | SEIBufferingPeriod sei_buffering_period; |
|---|
| 1386 | |
|---|
| 1387 | UInt uiInitialCpbRemovalDelay = (90000/2); // 0.5 sec |
|---|
| 1388 | sei_buffering_period.m_initialCpbRemovalDelay [0][0] = uiInitialCpbRemovalDelay; |
|---|
| 1389 | sei_buffering_period.m_initialCpbRemovalDelayOffset[0][0] = uiInitialCpbRemovalDelay; |
|---|
| 1390 | sei_buffering_period.m_initialCpbRemovalDelay [0][1] = uiInitialCpbRemovalDelay; |
|---|
| 1391 | sei_buffering_period.m_initialCpbRemovalDelayOffset[0][1] = uiInitialCpbRemovalDelay; |
|---|
| 1392 | |
|---|
| 1393 | #if L0043_TIMING_INFO |
|---|
| 1394 | Double dTmp = (Double)pcSlice->getSPS()->getVuiParameters()->getTimingInfo()->getNumUnitsInTick() / (Double)pcSlice->getSPS()->getVuiParameters()->getTimingInfo()->getTimeScale(); |
|---|
| 1395 | #else |
|---|
| 1396 | Double dTmp = (Double)pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNumUnitsInTick() / (Double)pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getTimeScale(); |
|---|
| 1397 | #endif |
|---|
| 1398 | |
|---|
| 1399 | UInt uiTmp = (UInt)( dTmp * 90000.0 ); |
|---|
| 1400 | uiInitialCpbRemovalDelay -= uiTmp; |
|---|
| 1401 | uiInitialCpbRemovalDelay -= uiTmp / ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getTickDivisorMinus2() + 2 ); |
|---|
| 1402 | sei_buffering_period.m_initialAltCpbRemovalDelay [0][0] = uiInitialCpbRemovalDelay; |
|---|
| 1403 | sei_buffering_period.m_initialAltCpbRemovalDelayOffset[0][0] = uiInitialCpbRemovalDelay; |
|---|
| 1404 | sei_buffering_period.m_initialAltCpbRemovalDelay [0][1] = uiInitialCpbRemovalDelay; |
|---|
| 1405 | sei_buffering_period.m_initialAltCpbRemovalDelayOffset[0][1] = uiInitialCpbRemovalDelay; |
|---|
| 1406 | |
|---|
| 1407 | sei_buffering_period.m_rapCpbParamsPresentFlag = 0; |
|---|
| 1408 | #if L0328_SPLICING |
|---|
| 1409 | //for the concatenation, it can be set to one during splicing. |
|---|
| 1410 | sei_buffering_period.m_concatenationFlag = 0; |
|---|
| 1411 | //since the temporal layer HRD is not ready, we assumed it is fixed |
|---|
| 1412 | sei_buffering_period.m_auCpbRemovalDelayDelta = 1; |
|---|
| 1413 | #endif |
|---|
| 1414 | #if L0044_CPB_DPB_DELAY_OFFSET |
|---|
| 1415 | sei_buffering_period.m_cpbDelayOffset = 0; |
|---|
| 1416 | sei_buffering_period.m_dpbDelayOffset = 0; |
|---|
| 1417 | #endif |
|---|
| 1418 | |
|---|
| 1419 | m_seiWriter.writeSEImessage( nalu.m_Bitstream, sei_buffering_period, pcSlice->getSPS()); |
|---|
| 1420 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1421 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 1422 | { |
|---|
| 1423 | UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit); |
|---|
| 1424 | UInt offsetPosition = m_activeParameterSetSEIPresentInAU; // Insert BP SEI after APS SEI |
|---|
| 1425 | AccessUnit::iterator it; |
|---|
| 1426 | for(j = 0, it = accessUnit.begin(); j < seiPositionInAu + offsetPosition; j++) |
|---|
| 1427 | { |
|---|
| 1428 | it++; |
|---|
| 1429 | } |
|---|
| 1430 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 1431 | m_bufferingPeriodSEIPresentInAU = true; |
|---|
| 1432 | } |
|---|
| 1433 | #else |
|---|
| 1434 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1435 | #endif |
|---|
| 1436 | |
|---|
| 1437 | #if K0180_SCALABLE_NESTING_SEI |
|---|
| 1438 | if (m_pcCfg->getScalableNestingSEIEnabled()) |
|---|
| 1439 | { |
|---|
| 1440 | OutputNALUnit naluTmp(NAL_UNIT_PREFIX_SEI); |
|---|
| 1441 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 1442 | m_pcEntropyCoder->setBitstream(&naluTmp.m_Bitstream); |
|---|
| 1443 | scalableNestingSEI.m_nestedSEIs.clear(); |
|---|
| 1444 | scalableNestingSEI.m_nestedSEIs.push_back(&sei_buffering_period); |
|---|
| 1445 | m_seiWriter.writeSEImessage( naluTmp.m_Bitstream, scalableNestingSEI, pcSlice->getSPS()); |
|---|
| 1446 | writeRBSPTrailingBits(naluTmp.m_Bitstream); |
|---|
| 1447 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 1448 | UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit); |
|---|
| 1449 | UInt offsetPosition = m_activeParameterSetSEIPresentInAU + m_bufferingPeriodSEIPresentInAU + m_pictureTimingSEIPresentInAU; // Insert BP SEI after non-nested APS, BP and PT SEIs |
|---|
| 1450 | AccessUnit::iterator it; |
|---|
| 1451 | for(j = 0, it = accessUnit.begin(); j < seiPositionInAu + offsetPosition; j++) |
|---|
| 1452 | { |
|---|
| 1453 | it++; |
|---|
| 1454 | } |
|---|
| 1455 | accessUnit.insert(it, new NALUnitEBSP(naluTmp)); |
|---|
| 1456 | m_nestedBufferingPeriodSEIPresentInAU = true; |
|---|
| 1457 | #else |
|---|
| 1458 | accessUnit.push_back(new NALUnitEBSP(naluTmp)); |
|---|
| 1459 | #endif |
|---|
| 1460 | } |
|---|
| 1461 | #endif |
|---|
| 1462 | |
|---|
| 1463 | m_lastBPSEI = m_totalCoded; |
|---|
| 1464 | m_cpbRemovalDelay = 0; |
|---|
| 1465 | } |
|---|
| 1466 | m_cpbRemovalDelay ++; |
|---|
| 1467 | if( ( m_pcEncTop->getRecoveryPointSEIEnabled() ) && ( pcSlice->getSliceType() == I_SLICE ) ) |
|---|
| 1468 | { |
|---|
| 1469 | if( m_pcEncTop->getGradualDecodingRefreshInfoEnabled() && !pcSlice->getRapPicFlag() ) |
|---|
| 1470 | { |
|---|
| 1471 | // Gradual decoding refresh SEI |
|---|
| 1472 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI); |
|---|
| 1473 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 1474 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1475 | |
|---|
| 1476 | SEIGradualDecodingRefreshInfo seiGradualDecodingRefreshInfo; |
|---|
| 1477 | seiGradualDecodingRefreshInfo.m_gdrForegroundFlag = true; // Indicating all "foreground" |
|---|
| 1478 | |
|---|
| 1479 | m_seiWriter.writeSEImessage( nalu.m_Bitstream, seiGradualDecodingRefreshInfo, pcSlice->getSPS() ); |
|---|
| 1480 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1481 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1482 | } |
|---|
| 1483 | // Recovery point SEI |
|---|
| 1484 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI); |
|---|
| 1485 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 1486 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1487 | |
|---|
| 1488 | SEIRecoveryPoint sei_recovery_point; |
|---|
| 1489 | sei_recovery_point.m_recoveryPocCnt = 0; |
|---|
| 1490 | sei_recovery_point.m_exactMatchingFlag = ( pcSlice->getPOC() == 0 ) ? (true) : (false); |
|---|
| 1491 | sei_recovery_point.m_brokenLinkFlag = false; |
|---|
| 1492 | |
|---|
| 1493 | m_seiWriter.writeSEImessage( nalu.m_Bitstream, sei_recovery_point, pcSlice->getSPS() ); |
|---|
| 1494 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1495 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1496 | } |
|---|
| 1497 | |
|---|
| 1498 | /* use the main bitstream buffer for storing the marshalled picture */ |
|---|
| 1499 | m_pcEntropyCoder->setBitstream(NULL); |
|---|
| 1500 | |
|---|
| 1501 | startCUAddrSliceIdx = 0; |
|---|
| 1502 | startCUAddrSlice = 0; |
|---|
| 1503 | |
|---|
| 1504 | startCUAddrSliceSegmentIdx = 0; |
|---|
| 1505 | startCUAddrSliceSegment = 0; |
|---|
| 1506 | nextCUAddr = 0; |
|---|
| 1507 | pcSlice = pcPic->getSlice(startCUAddrSliceIdx); |
|---|
| 1508 | |
|---|
| 1509 | Int processingState = (pcSlice->getSPS()->getUseSAO())?(EXECUTE_INLOOPFILTER):(ENCODE_SLICE); |
|---|
| 1510 | Bool skippedSlice=false; |
|---|
| 1511 | while (nextCUAddr < uiRealEndAddress) // Iterate over all slices |
|---|
| 1512 | { |
|---|
| 1513 | switch(processingState) |
|---|
| 1514 | { |
|---|
| 1515 | case ENCODE_SLICE: |
|---|
| 1516 | { |
|---|
| 1517 | pcSlice->setNextSlice ( false ); |
|---|
| 1518 | pcSlice->setNextSliceSegment( false ); |
|---|
| 1519 | if (nextCUAddr == m_storedStartCUAddrForEncodingSlice[startCUAddrSliceIdx]) |
|---|
| 1520 | { |
|---|
| 1521 | pcSlice = pcPic->getSlice(startCUAddrSliceIdx); |
|---|
| 1522 | if(startCUAddrSliceIdx > 0 && pcSlice->getSliceType()!= I_SLICE) |
|---|
| 1523 | { |
|---|
| 1524 | pcSlice->checkColRefIdx(startCUAddrSliceIdx, pcPic); |
|---|
| 1525 | } |
|---|
| 1526 | pcPic->setCurrSliceIdx(startCUAddrSliceIdx); |
|---|
| 1527 | m_pcSliceEncoder->setSliceIdx(startCUAddrSliceIdx); |
|---|
| 1528 | assert(startCUAddrSliceIdx == pcSlice->getSliceIdx()); |
|---|
| 1529 | // Reconstruction slice |
|---|
| 1530 | pcSlice->setSliceCurStartCUAddr( nextCUAddr ); // to be used in encodeSlice() + context restriction |
|---|
| 1531 | pcSlice->setSliceCurEndCUAddr ( m_storedStartCUAddrForEncodingSlice[startCUAddrSliceIdx+1 ] ); |
|---|
| 1532 | // Dependent slice |
|---|
| 1533 | pcSlice->setSliceSegmentCurStartCUAddr( nextCUAddr ); // to be used in encodeSlice() + context restriction |
|---|
| 1534 | pcSlice->setSliceSegmentCurEndCUAddr ( m_storedStartCUAddrForEncodingSliceSegment[startCUAddrSliceSegmentIdx+1 ] ); |
|---|
| 1535 | |
|---|
| 1536 | pcSlice->setNextSlice ( true ); |
|---|
| 1537 | |
|---|
| 1538 | startCUAddrSliceIdx++; |
|---|
| 1539 | startCUAddrSliceSegmentIdx++; |
|---|
| 1540 | } |
|---|
| 1541 | else if (nextCUAddr == m_storedStartCUAddrForEncodingSliceSegment[startCUAddrSliceSegmentIdx]) |
|---|
| 1542 | { |
|---|
| 1543 | // Dependent slice |
|---|
| 1544 | pcSlice->setSliceSegmentCurStartCUAddr( nextCUAddr ); // to be used in encodeSlice() + context restriction |
|---|
| 1545 | pcSlice->setSliceSegmentCurEndCUAddr ( m_storedStartCUAddrForEncodingSliceSegment[startCUAddrSliceSegmentIdx+1 ] ); |
|---|
| 1546 | |
|---|
| 1547 | pcSlice->setNextSliceSegment( true ); |
|---|
| 1548 | |
|---|
| 1549 | startCUAddrSliceSegmentIdx++; |
|---|
| 1550 | } |
|---|
| 1551 | |
|---|
| 1552 | pcSlice->setRPS(pcPic->getSlice(0)->getRPS()); |
|---|
| 1553 | pcSlice->setRPSidx(pcPic->getSlice(0)->getRPSidx()); |
|---|
| 1554 | UInt uiDummyStartCUAddr; |
|---|
| 1555 | UInt uiDummyBoundingCUAddr; |
|---|
| 1556 | m_pcSliceEncoder->xDetermineStartAndBoundingCUAddr(uiDummyStartCUAddr,uiDummyBoundingCUAddr,pcPic,true); |
|---|
| 1557 | |
|---|
| 1558 | uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU(); |
|---|
| 1559 | uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU(); |
|---|
| 1560 | uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
|---|
| 1561 | uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
|---|
| 1562 | uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples(); |
|---|
| 1563 | uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples(); |
|---|
| 1564 | while(uiPosX>=uiWidth||uiPosY>=uiHeight) |
|---|
| 1565 | { |
|---|
| 1566 | uiInternalAddress--; |
|---|
| 1567 | uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
|---|
| 1568 | uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
|---|
| 1569 | } |
|---|
| 1570 | uiInternalAddress++; |
|---|
| 1571 | if(uiInternalAddress==pcPic->getNumPartInCU()) |
|---|
| 1572 | { |
|---|
| 1573 | uiInternalAddress = 0; |
|---|
| 1574 | uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1); |
|---|
| 1575 | } |
|---|
| 1576 | UInt endAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress); |
|---|
| 1577 | if(endAddress<=pcSlice->getSliceSegmentCurStartCUAddr()) |
|---|
| 1578 | { |
|---|
| 1579 | UInt boundingAddrSlice, boundingAddrSliceSegment; |
|---|
| 1580 | boundingAddrSlice = m_storedStartCUAddrForEncodingSlice[startCUAddrSliceIdx]; |
|---|
| 1581 | boundingAddrSliceSegment = m_storedStartCUAddrForEncodingSliceSegment[startCUAddrSliceSegmentIdx]; |
|---|
| 1582 | nextCUAddr = min(boundingAddrSlice, boundingAddrSliceSegment); |
|---|
| 1583 | if(pcSlice->isNextSlice()) |
|---|
| 1584 | { |
|---|
| 1585 | skippedSlice=true; |
|---|
| 1586 | } |
|---|
| 1587 | continue; |
|---|
| 1588 | } |
|---|
| 1589 | if(skippedSlice) |
|---|
| 1590 | { |
|---|
| 1591 | pcSlice->setNextSlice ( true ); |
|---|
| 1592 | pcSlice->setNextSliceSegment( false ); |
|---|
| 1593 | } |
|---|
| 1594 | skippedSlice=false; |
|---|
| 1595 | pcSlice->allocSubstreamSizes( iNumSubstreams ); |
|---|
| 1596 | for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ ) |
|---|
| 1597 | { |
|---|
| 1598 | pcSubstreamsOut[ui].clear(); |
|---|
| 1599 | } |
|---|
| 1600 | |
|---|
| 1601 | m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice ); |
|---|
| 1602 | m_pcEntropyCoder->resetEntropy (); |
|---|
| 1603 | /* start slice NALunit */ |
|---|
| 1604 | #if SVC_EXTENSION |
|---|
| 1605 | OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->getTLayer(), m_layerId ); |
|---|
| 1606 | #else |
|---|
| 1607 | OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->getTLayer() ); |
|---|
| 1608 | #endif |
|---|
| 1609 | Bool sliceSegment = (!pcSlice->isNextSlice()); |
|---|
| 1610 | if (!sliceSegment) |
|---|
| 1611 | { |
|---|
| 1612 | uiOneBitstreamPerSliceLength = 0; // start of a new slice |
|---|
| 1613 | } |
|---|
| 1614 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1615 | #if RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 1616 | tmpBitsBeforeWriting = m_pcEntropyCoder->getNumberOfWrittenBits(); |
|---|
| 1617 | #endif |
|---|
| 1618 | m_pcEntropyCoder->encodeSliceHeader(pcSlice); |
|---|
| 1619 | #if RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 1620 | actualHeadBits += ( m_pcEntropyCoder->getNumberOfWrittenBits() - tmpBitsBeforeWriting ); |
|---|
| 1621 | #endif |
|---|
| 1622 | |
|---|
| 1623 | // is it needed? |
|---|
| 1624 | { |
|---|
| 1625 | if (!sliceSegment) |
|---|
| 1626 | { |
|---|
| 1627 | pcBitstreamRedirect->writeAlignOne(); |
|---|
| 1628 | } |
|---|
| 1629 | else |
|---|
| 1630 | { |
|---|
| 1631 | // We've not completed our slice header info yet, do the alignment later. |
|---|
| 1632 | } |
|---|
| 1633 | m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC ); |
|---|
| 1634 | m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder, pcSlice ); |
|---|
| 1635 | m_pcEntropyCoder->resetEntropy (); |
|---|
| 1636 | for ( UInt ui = 0 ; ui < pcSlice->getPPS()->getNumSubstreams() ; ui++ ) |
|---|
| 1637 | { |
|---|
| 1638 | m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[ui], pcSlice ); |
|---|
| 1639 | m_pcEntropyCoder->resetEntropy (); |
|---|
| 1640 | } |
|---|
| 1641 | } |
|---|
| 1642 | |
|---|
| 1643 | if(pcSlice->isNextSlice()) |
|---|
| 1644 | { |
|---|
| 1645 | // set entropy coder for writing |
|---|
| 1646 | m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC ); |
|---|
| 1647 | { |
|---|
| 1648 | for ( UInt ui = 0 ; ui < pcSlice->getPPS()->getNumSubstreams() ; ui++ ) |
|---|
| 1649 | { |
|---|
| 1650 | m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[ui], pcSlice ); |
|---|
| 1651 | m_pcEntropyCoder->resetEntropy (); |
|---|
| 1652 | } |
|---|
| 1653 | pcSbacCoders[0].load(m_pcSbacCoder); |
|---|
| 1654 | m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[0], pcSlice ); //ALF is written in substream #0 with CABAC coder #0 (see ALF param encoding below) |
|---|
| 1655 | } |
|---|
| 1656 | m_pcEntropyCoder->resetEntropy (); |
|---|
| 1657 | // File writing |
|---|
| 1658 | if (!sliceSegment) |
|---|
| 1659 | { |
|---|
| 1660 | m_pcEntropyCoder->setBitstream(pcBitstreamRedirect); |
|---|
| 1661 | } |
|---|
| 1662 | else |
|---|
| 1663 | { |
|---|
| 1664 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1665 | } |
|---|
| 1666 | // for now, override the TILES_DECODER setting in order to write substreams. |
|---|
| 1667 | m_pcEntropyCoder->setBitstream ( &pcSubstreamsOut[0] ); |
|---|
| 1668 | |
|---|
| 1669 | } |
|---|
| 1670 | pcSlice->setFinalized(true); |
|---|
| 1671 | |
|---|
| 1672 | m_pcSbacCoder->load( &pcSbacCoders[0] ); |
|---|
| 1673 | |
|---|
| 1674 | pcSlice->setTileOffstForMultES( uiOneBitstreamPerSliceLength ); |
|---|
| 1675 | pcSlice->setTileLocationCount ( 0 ); |
|---|
| 1676 | m_pcSliceEncoder->encodeSlice(pcPic, pcSubstreamsOut); |
|---|
| 1677 | |
|---|
| 1678 | { |
|---|
| 1679 | // Construct the final bitstream by flushing and concatenating substreams. |
|---|
| 1680 | // The final bitstream is either nalu.m_Bitstream or pcBitstreamRedirect; |
|---|
| 1681 | UInt* puiSubstreamSizes = pcSlice->getSubstreamSizes(); |
|---|
| 1682 | UInt uiTotalCodedSize = 0; // for padding calcs. |
|---|
| 1683 | UInt uiNumSubstreamsPerTile = iNumSubstreams; |
|---|
| 1684 | if (iNumSubstreams > 1) |
|---|
| 1685 | { |
|---|
| 1686 | uiNumSubstreamsPerTile /= pcPic->getPicSym()->getNumTiles(); |
|---|
| 1687 | } |
|---|
| 1688 | for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ ) |
|---|
| 1689 | { |
|---|
| 1690 | // Flush all substreams -- this includes empty ones. |
|---|
| 1691 | // Terminating bit and flush. |
|---|
| 1692 | m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[ui], pcSlice ); |
|---|
| 1693 | m_pcEntropyCoder->setBitstream ( &pcSubstreamsOut[ui] ); |
|---|
| 1694 | m_pcEntropyCoder->encodeTerminatingBit( 1 ); |
|---|
| 1695 | m_pcEntropyCoder->encodeSliceFinish(); |
|---|
| 1696 | |
|---|
| 1697 | pcSubstreamsOut[ui].writeByteAlignment(); // Byte-alignment in slice_data() at end of sub-stream |
|---|
| 1698 | // Byte alignment is necessary between tiles when tiles are independent. |
|---|
| 1699 | uiTotalCodedSize += pcSubstreamsOut[ui].getNumberOfWrittenBits(); |
|---|
| 1700 | |
|---|
| 1701 | Bool bNextSubstreamInNewTile = ((ui+1) < iNumSubstreams)&& ((ui+1)%uiNumSubstreamsPerTile == 0); |
|---|
| 1702 | if (bNextSubstreamInNewTile) |
|---|
| 1703 | { |
|---|
| 1704 | pcSlice->setTileLocation(ui/uiNumSubstreamsPerTile, pcSlice->getTileOffstForMultES()+(uiTotalCodedSize>>3)); |
|---|
| 1705 | } |
|---|
| 1706 | if (ui+1 < pcSlice->getPPS()->getNumSubstreams()) |
|---|
| 1707 | { |
|---|
| 1708 | puiSubstreamSizes[ui] = pcSubstreamsOut[ui].getNumberOfWrittenBits() + (pcSubstreamsOut[ui].countStartCodeEmulations()<<3); |
|---|
| 1709 | } |
|---|
| 1710 | } |
|---|
| 1711 | |
|---|
| 1712 | // Complete the slice header info. |
|---|
| 1713 | m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice ); |
|---|
| 1714 | m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream); |
|---|
| 1715 | m_pcEntropyCoder->encodeTilesWPPEntryPoint( pcSlice ); |
|---|
| 1716 | |
|---|
| 1717 | // Substreams... |
|---|
| 1718 | TComOutputBitstream *pcOut = pcBitstreamRedirect; |
|---|
| 1719 | Int offs = 0; |
|---|
| 1720 | Int nss = pcSlice->getPPS()->getNumSubstreams(); |
|---|
| 1721 | if (pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()) |
|---|
| 1722 | { |
|---|
| 1723 | // 1st line present for WPP. |
|---|
| 1724 | offs = pcSlice->getSliceSegmentCurStartCUAddr()/pcSlice->getPic()->getNumPartInCU()/pcSlice->getPic()->getFrameWidthInCU(); |
|---|
| 1725 | nss = pcSlice->getNumEntryPointOffsets()+1; |
|---|
| 1726 | } |
|---|
| 1727 | for ( UInt ui = 0 ; ui < nss; ui++ ) |
|---|
| 1728 | { |
|---|
| 1729 | pcOut->addSubstream(&pcSubstreamsOut[ui+offs]); |
|---|
| 1730 | } |
|---|
| 1731 | } |
|---|
| 1732 | |
|---|
| 1733 | UInt boundingAddrSlice, boundingAddrSliceSegment; |
|---|
| 1734 | boundingAddrSlice = m_storedStartCUAddrForEncodingSlice[startCUAddrSliceIdx]; |
|---|
| 1735 | boundingAddrSliceSegment = m_storedStartCUAddrForEncodingSliceSegment[startCUAddrSliceSegmentIdx]; |
|---|
| 1736 | nextCUAddr = min(boundingAddrSlice, boundingAddrSliceSegment); |
|---|
| 1737 | // If current NALU is the first NALU of slice (containing slice header) and more NALUs exist (due to multiple dependent slices) then buffer it. |
|---|
| 1738 | // If current NALU is the last NALU of slice and a NALU was buffered, then (a) Write current NALU (b) Update an write buffered NALU at approproate location in NALU list. |
|---|
| 1739 | Bool bNALUAlignedWrittenToList = false; // used to ensure current NALU is not written more than once to the NALU list. |
|---|
| 1740 | xAttachSliceDataToNalUnit(nalu, pcBitstreamRedirect); |
|---|
| 1741 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1742 | #if RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 1743 | actualTotalBits += UInt(accessUnit.back()->m_nalUnitData.str().size()) * 8; |
|---|
| 1744 | #endif |
|---|
| 1745 | bNALUAlignedWrittenToList = true; |
|---|
| 1746 | uiOneBitstreamPerSliceLength += nalu.m_Bitstream.getNumberOfWrittenBits(); // length of bitstream after byte-alignment |
|---|
| 1747 | |
|---|
| 1748 | if (!bNALUAlignedWrittenToList) |
|---|
| 1749 | { |
|---|
| 1750 | { |
|---|
| 1751 | nalu.m_Bitstream.writeAlignZero(); |
|---|
| 1752 | } |
|---|
| 1753 | accessUnit.push_back(new NALUnitEBSP(nalu)); |
|---|
| 1754 | uiOneBitstreamPerSliceLength += nalu.m_Bitstream.getNumberOfWrittenBits() + 24; // length of bitstream after byte-alignment + 3 byte startcode 0x000001 |
|---|
| 1755 | } |
|---|
| 1756 | |
|---|
| 1757 | if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) && |
|---|
| 1758 | ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) && |
|---|
| 1759 | ( ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNalHrdParametersPresentFlag() ) |
|---|
| 1760 | || ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getVclHrdParametersPresentFlag() ) ) && |
|---|
| 1761 | ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getSubPicCpbParamsPresentFlag() ) ) |
|---|
| 1762 | { |
|---|
| 1763 | UInt numNalus = 0; |
|---|
| 1764 | UInt numRBSPBytes = 0; |
|---|
| 1765 | for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++) |
|---|
| 1766 | { |
|---|
| 1767 | UInt numRBSPBytes_nal = UInt((*it)->m_nalUnitData.str().size()); |
|---|
| 1768 | if ((*it)->m_nalUnitType != NAL_UNIT_PREFIX_SEI && (*it)->m_nalUnitType != NAL_UNIT_SUFFIX_SEI) |
|---|
| 1769 | { |
|---|
| 1770 | numRBSPBytes += numRBSPBytes_nal; |
|---|
| 1771 | numNalus ++; |
|---|
| 1772 | } |
|---|
| 1773 | } |
|---|
| 1774 | accumBitsDU[ pcSlice->getSliceIdx() ] = ( numRBSPBytes << 3 ); |
|---|
| 1775 | accumNalsDU[ pcSlice->getSliceIdx() ] = numNalus; // SEI not counted for bit count; hence shouldn't be counted for # of NALUs - only for consistency |
|---|
| 1776 | } |
|---|
| 1777 | processingState = ENCODE_SLICE; |
|---|
| 1778 | } |
|---|
| 1779 | break; |
|---|
| 1780 | case EXECUTE_INLOOPFILTER: |
|---|
| 1781 | { |
|---|
| 1782 | // set entropy coder for RD |
|---|
| 1783 | m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder, pcSlice ); |
|---|
| 1784 | if ( pcSlice->getSPS()->getUseSAO() ) |
|---|
| 1785 | { |
|---|
| 1786 | m_pcEntropyCoder->resetEntropy(); |
|---|
| 1787 | m_pcEntropyCoder->setBitstream( m_pcBitCounter ); |
|---|
| 1788 | m_pcSAO->startSaoEnc(pcPic, m_pcEntropyCoder, m_pcEncTop->getRDSbacCoder(), m_pcEncTop->getRDGoOnSbacCoder()); |
|---|
| 1789 | SAOParam& cSaoParam = *pcSlice->getPic()->getPicSym()->getSaoParam(); |
|---|
| 1790 | |
|---|
| 1791 | #if SAO_CHROMA_LAMBDA |
|---|
| 1792 | #if SAO_ENCODING_CHOICE |
|---|
| 1793 | m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), pcPic->getSlice(0)->getDepth()); |
|---|
| 1794 | #else |
|---|
| 1795 | m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma()); |
|---|
| 1796 | #endif |
|---|
| 1797 | #else |
|---|
| 1798 | m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambda()); |
|---|
| 1799 | #endif |
|---|
| 1800 | m_pcSAO->endSaoEnc(); |
|---|
| 1801 | m_pcSAO->PCMLFDisableProcess(pcPic); |
|---|
| 1802 | } |
|---|
| 1803 | #if SAO_RDO |
|---|
| 1804 | m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice ); |
|---|
| 1805 | #endif |
|---|
| 1806 | processingState = ENCODE_SLICE; |
|---|
| 1807 | |
|---|
| 1808 | for(Int s=0; s< uiNumSlices; s++) |
|---|
| 1809 | { |
|---|
| 1810 | if (pcSlice->getSPS()->getUseSAO()) |
|---|
| 1811 | { |
|---|
| 1812 | pcPic->getSlice(s)->setSaoEnabledFlag((pcSlice->getPic()->getPicSym()->getSaoParam()->bSaoFlag[0]==1)?true:false); |
|---|
| 1813 | } |
|---|
| 1814 | } |
|---|
| 1815 | } |
|---|
| 1816 | break; |
|---|
| 1817 | default: |
|---|
| 1818 | { |
|---|
| 1819 | printf("Not a supported encoding state\n"); |
|---|
| 1820 | assert(0); |
|---|
| 1821 | exit(-1); |
|---|
| 1822 | } |
|---|
| 1823 | } |
|---|
| 1824 | } // end iteration over slices |
|---|
| 1825 | |
|---|
| 1826 | if(pcSlice->getSPS()->getUseSAO()) |
|---|
| 1827 | { |
|---|
| 1828 | if(pcSlice->getSPS()->getUseSAO()) |
|---|
| 1829 | { |
|---|
| 1830 | m_pcSAO->destroyPicSaoInfo(); |
|---|
| 1831 | } |
|---|
| 1832 | pcPic->destroyNonDBFilterInfo(); |
|---|
| 1833 | } |
|---|
| 1834 | |
|---|
| 1835 | pcPic->compressMotion(); |
|---|
| 1836 | |
|---|
| 1837 | //-- For time output for each slice |
|---|
| 1838 | Double dEncTime = (Double)(clock()-iBeforeTime) / CLOCKS_PER_SEC; |
|---|
| 1839 | |
|---|
| 1840 | const Char* digestStr = NULL; |
|---|
| 1841 | if (m_pcCfg->getDecodedPictureHashSEIEnabled()) |
|---|
| 1842 | { |
|---|
| 1843 | /* calculate MD5sum for entire reconstructed picture */ |
|---|
| 1844 | SEIDecodedPictureHash sei_recon_picture_digest; |
|---|
| 1845 | if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 1) |
|---|
| 1846 | { |
|---|
| 1847 | sei_recon_picture_digest.method = SEIDecodedPictureHash::MD5; |
|---|
| 1848 | calcMD5(*pcPic->getPicYuvRec(), sei_recon_picture_digest.digest); |
|---|
| 1849 | digestStr = digestToString(sei_recon_picture_digest.digest, 16); |
|---|
| 1850 | } |
|---|
| 1851 | else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 2) |
|---|
| 1852 | { |
|---|
| 1853 | sei_recon_picture_digest.method = SEIDecodedPictureHash::CRC; |
|---|
| 1854 | calcCRC(*pcPic->getPicYuvRec(), sei_recon_picture_digest.digest); |
|---|
| 1855 | digestStr = digestToString(sei_recon_picture_digest.digest, 2); |
|---|
| 1856 | } |
|---|
| 1857 | else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 3) |
|---|
| 1858 | { |
|---|
| 1859 | sei_recon_picture_digest.method = SEIDecodedPictureHash::CHECKSUM; |
|---|
| 1860 | calcChecksum(*pcPic->getPicYuvRec(), sei_recon_picture_digest.digest); |
|---|
| 1861 | digestStr = digestToString(sei_recon_picture_digest.digest, 4); |
|---|
| 1862 | } |
|---|
| 1863 | #if SVC_EXTENSION |
|---|
| 1864 | OutputNALUnit nalu(NAL_UNIT_SUFFIX_SEI, pcSlice->getTLayer(), m_layerId); |
|---|
| 1865 | #else |
|---|
| 1866 | OutputNALUnit nalu(NAL_UNIT_SUFFIX_SEI, pcSlice->getTLayer()); |
|---|
| 1867 | #endif |
|---|
| 1868 | |
|---|
| 1869 | /* write the SEI messages */ |
|---|
| 1870 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 1871 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, sei_recon_picture_digest, pcSlice->getSPS()); |
|---|
| 1872 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1873 | |
|---|
| 1874 | accessUnit.insert(accessUnit.end(), new NALUnitEBSP(nalu)); |
|---|
| 1875 | } |
|---|
| 1876 | if (m_pcCfg->getTemporalLevel0IndexSEIEnabled()) |
|---|
| 1877 | { |
|---|
| 1878 | SEITemporalLevel0Index sei_temporal_level0_index; |
|---|
| 1879 | if (pcSlice->getRapPicFlag()) |
|---|
| 1880 | { |
|---|
| 1881 | m_tl0Idx = 0; |
|---|
| 1882 | m_rapIdx = (m_rapIdx + 1) & 0xFF; |
|---|
| 1883 | } |
|---|
| 1884 | else |
|---|
| 1885 | { |
|---|
| 1886 | m_tl0Idx = (m_tl0Idx + (pcSlice->getTLayer() ? 0 : 1)) & 0xFF; |
|---|
| 1887 | } |
|---|
| 1888 | sei_temporal_level0_index.tl0Idx = m_tl0Idx; |
|---|
| 1889 | sei_temporal_level0_index.rapIdx = m_rapIdx; |
|---|
| 1890 | |
|---|
| 1891 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI); |
|---|
| 1892 | |
|---|
| 1893 | /* write the SEI messages */ |
|---|
| 1894 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 1895 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, sei_temporal_level0_index, pcSlice->getSPS()); |
|---|
| 1896 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 1897 | |
|---|
| 1898 | /* insert the SEI message NALUnit before any Slice NALUnits */ |
|---|
| 1899 | AccessUnit::iterator it = find_if(accessUnit.begin(), accessUnit.end(), mem_fun(&NALUnit::isSlice)); |
|---|
| 1900 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 1901 | } |
|---|
| 1902 | |
|---|
| 1903 | xCalculateAddPSNR( pcPic, pcPic->getPicYuvRec(), accessUnit, dEncTime ); |
|---|
| 1904 | |
|---|
| 1905 | if (digestStr) |
|---|
| 1906 | { |
|---|
| 1907 | if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 1) |
|---|
| 1908 | { |
|---|
| 1909 | printf(" [MD5:%s]", digestStr); |
|---|
| 1910 | } |
|---|
| 1911 | else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 2) |
|---|
| 1912 | { |
|---|
| 1913 | printf(" [CRC:%s]", digestStr); |
|---|
| 1914 | } |
|---|
| 1915 | else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 3) |
|---|
| 1916 | { |
|---|
| 1917 | printf(" [Checksum:%s]", digestStr); |
|---|
| 1918 | } |
|---|
| 1919 | } |
|---|
| 1920 | #if RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 1921 | if ( m_pcCfg->getUseRateCtrl() ) |
|---|
| 1922 | { |
|---|
| 1923 | Double effectivePercentage = m_pcRateCtrl->getRCPic()->getEffectivePercentage(); |
|---|
| 1924 | Double avgQP = m_pcRateCtrl->getRCPic()->calAverageQP(); |
|---|
| 1925 | Double avgLambda = m_pcRateCtrl->getRCPic()->calAverageLambda(); |
|---|
| 1926 | if ( avgLambda < 0.0 ) |
|---|
| 1927 | { |
|---|
| 1928 | avgLambda = lambda; |
|---|
| 1929 | } |
|---|
| 1930 | m_pcRateCtrl->getRCPic()->updateAfterPicture( actualHeadBits, actualTotalBits, avgQP, avgLambda, effectivePercentage ); |
|---|
| 1931 | m_pcRateCtrl->getRCPic()->addToPictureLsit( m_pcRateCtrl->getPicList() ); |
|---|
| 1932 | |
|---|
| 1933 | m_pcRateCtrl->getRCSeq()->updateAfterPic( actualTotalBits ); |
|---|
| 1934 | if ( pcSlice->getSliceType() != I_SLICE ) |
|---|
| 1935 | { |
|---|
| 1936 | m_pcRateCtrl->getRCGOP()->updateAfterPicture( actualTotalBits ); |
|---|
| 1937 | } |
|---|
| 1938 | else // for intra picture, the estimated bits are used to update the current status in the GOP |
|---|
| 1939 | { |
|---|
| 1940 | m_pcRateCtrl->getRCGOP()->updateAfterPicture( estimatedBits ); |
|---|
| 1941 | } |
|---|
| 1942 | } |
|---|
| 1943 | #else |
|---|
| 1944 | if(m_pcCfg->getUseRateCtrl()) |
|---|
| 1945 | { |
|---|
| 1946 | UInt frameBits = m_vRVM_RP[m_vRVM_RP.size()-1]; |
|---|
| 1947 | m_pcRateCtrl->updataRCFrameStatus((Int)frameBits, pcSlice->getSliceType()); |
|---|
| 1948 | } |
|---|
| 1949 | #endif |
|---|
| 1950 | if( ( m_pcCfg->getPictureTimingSEIEnabled() || m_pcCfg->getDecodingUnitInfoSEIEnabled() ) && |
|---|
| 1951 | ( pcSlice->getSPS()->getVuiParametersPresentFlag() ) && |
|---|
| 1952 | ( ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getNalHrdParametersPresentFlag() ) |
|---|
| 1953 | || ( pcSlice->getSPS()->getVuiParameters()->getHrdParameters()->getVclHrdParametersPresentFlag() ) ) ) |
|---|
| 1954 | { |
|---|
| 1955 | TComVUI *vui = pcSlice->getSPS()->getVuiParameters(); |
|---|
| 1956 | TComHRD *hrd = vui->getHrdParameters(); |
|---|
| 1957 | |
|---|
| 1958 | if( hrd->getSubPicCpbParamsPresentFlag() ) |
|---|
| 1959 | { |
|---|
| 1960 | Int i; |
|---|
| 1961 | UInt64 ui64Tmp; |
|---|
| 1962 | UInt uiPrev = 0; |
|---|
| 1963 | UInt numDU = ( pictureTimingSEI.m_numDecodingUnitsMinus1 + 1 ); |
|---|
| 1964 | UInt *pCRD = &pictureTimingSEI.m_duCpbRemovalDelayMinus1[0]; |
|---|
| 1965 | UInt maxDiff = ( hrd->getTickDivisorMinus2() + 2 ) - 1; |
|---|
| 1966 | |
|---|
| 1967 | for( i = 0; i < numDU; i ++ ) |
|---|
| 1968 | { |
|---|
| 1969 | pictureTimingSEI.m_numNalusInDuMinus1[ i ] = ( i == 0 ) ? ( accumNalsDU[ i ] - 1 ) : ( accumNalsDU[ i ] - accumNalsDU[ i - 1] - 1 ); |
|---|
| 1970 | } |
|---|
| 1971 | |
|---|
| 1972 | if( numDU == 1 ) |
|---|
| 1973 | { |
|---|
| 1974 | pCRD[ 0 ] = 0; /* don't care */ |
|---|
| 1975 | } |
|---|
| 1976 | else |
|---|
| 1977 | { |
|---|
| 1978 | pCRD[ numDU - 1 ] = 0;/* by definition */ |
|---|
| 1979 | UInt tmp = 0; |
|---|
| 1980 | UInt accum = 0; |
|---|
| 1981 | |
|---|
| 1982 | for( i = ( numDU - 2 ); i >= 0; i -- ) |
|---|
| 1983 | { |
|---|
| 1984 | #if L0043_TIMING_INFO |
|---|
| 1985 | ui64Tmp = ( ( ( accumBitsDU[ numDU - 1 ] - accumBitsDU[ i ] ) * ( vui->getTimingInfo()->getTimeScale() / vui->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) ); |
|---|
| 1986 | #else |
|---|
| 1987 | ui64Tmp = ( ( ( accumBitsDU[ numDU - 1 ] - accumBitsDU[ i ] ) * ( hrd->getTimeScale() / hrd->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) ); |
|---|
| 1988 | #endif |
|---|
| 1989 | if( (UInt)ui64Tmp > maxDiff ) |
|---|
| 1990 | { |
|---|
| 1991 | tmp ++; |
|---|
| 1992 | } |
|---|
| 1993 | } |
|---|
| 1994 | uiPrev = 0; |
|---|
| 1995 | |
|---|
| 1996 | UInt flag = 0; |
|---|
| 1997 | for( i = ( numDU - 2 ); i >= 0; i -- ) |
|---|
| 1998 | { |
|---|
| 1999 | flag = 0; |
|---|
| 2000 | #if L0043_TIMING_INFO |
|---|
| 2001 | ui64Tmp = ( ( ( accumBitsDU[ numDU - 1 ] - accumBitsDU[ i ] ) * ( vui->getTimingInfo()->getTimeScale() / vui->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) ); |
|---|
| 2002 | #else |
|---|
| 2003 | ui64Tmp = ( ( ( accumBitsDU[ numDU - 1 ] - accumBitsDU[ i ] ) * ( hrd->getTimeScale() / hrd->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) ); |
|---|
| 2004 | #endif |
|---|
| 2005 | |
|---|
| 2006 | if( (UInt)ui64Tmp > maxDiff ) |
|---|
| 2007 | { |
|---|
| 2008 | if(uiPrev >= maxDiff - tmp) |
|---|
| 2009 | { |
|---|
| 2010 | ui64Tmp = uiPrev + 1; |
|---|
| 2011 | flag = 1; |
|---|
| 2012 | } |
|---|
| 2013 | else ui64Tmp = maxDiff - tmp + 1; |
|---|
| 2014 | } |
|---|
| 2015 | pCRD[ i ] = (UInt)ui64Tmp - uiPrev - 1; |
|---|
| 2016 | if( (Int)pCRD[ i ] < 0 ) |
|---|
| 2017 | { |
|---|
| 2018 | pCRD[ i ] = 0; |
|---|
| 2019 | } |
|---|
| 2020 | else if (tmp > 0 && flag == 1) |
|---|
| 2021 | { |
|---|
| 2022 | tmp --; |
|---|
| 2023 | } |
|---|
| 2024 | accum += pCRD[ i ] + 1; |
|---|
| 2025 | uiPrev = accum; |
|---|
| 2026 | } |
|---|
| 2027 | } |
|---|
| 2028 | } |
|---|
| 2029 | if( m_pcCfg->getPictureTimingSEIEnabled() ) |
|---|
| 2030 | { |
|---|
| 2031 | { |
|---|
| 2032 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI, pcSlice->getTLayer()); |
|---|
| 2033 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 2034 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, pictureTimingSEI, pcSlice->getSPS()); |
|---|
| 2035 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 2036 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 2037 | UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit); |
|---|
| 2038 | UInt offsetPosition = m_activeParameterSetSEIPresentInAU |
|---|
| 2039 | + m_bufferingPeriodSEIPresentInAU; // Insert PT SEI after APS and BP SEI |
|---|
| 2040 | AccessUnit::iterator it; |
|---|
| 2041 | for(j = 0, it = accessUnit.begin(); j < seiPositionInAu + offsetPosition; j++) |
|---|
| 2042 | { |
|---|
| 2043 | it++; |
|---|
| 2044 | } |
|---|
| 2045 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 2046 | m_pictureTimingSEIPresentInAU = true; |
|---|
| 2047 | #else |
|---|
| 2048 | AccessUnit::iterator it = find_if(accessUnit.begin(), accessUnit.end(), mem_fun(&NALUnit::isSlice)); |
|---|
| 2049 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 2050 | #endif |
|---|
| 2051 | } |
|---|
| 2052 | #if K0180_SCALABLE_NESTING_SEI |
|---|
| 2053 | if ( m_pcCfg->getScalableNestingSEIEnabled() ) // put picture timing SEI into scalable nesting SEI |
|---|
| 2054 | { |
|---|
| 2055 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI, pcSlice->getTLayer()); |
|---|
| 2056 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 2057 | scalableNestingSEI.m_nestedSEIs.clear(); |
|---|
| 2058 | scalableNestingSEI.m_nestedSEIs.push_back(&pictureTimingSEI); |
|---|
| 2059 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, scalableNestingSEI, pcSlice->getSPS()); |
|---|
| 2060 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 2061 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 2062 | UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit); |
|---|
| 2063 | UInt offsetPosition = m_activeParameterSetSEIPresentInAU |
|---|
| 2064 | + m_bufferingPeriodSEIPresentInAU + m_pictureTimingSEIPresentInAU + m_nestedBufferingPeriodSEIPresentInAU; // Insert PT SEI after APS and BP SEI |
|---|
| 2065 | AccessUnit::iterator it; |
|---|
| 2066 | for(j = 0, it = accessUnit.begin(); j < seiPositionInAu + offsetPosition; j++) |
|---|
| 2067 | { |
|---|
| 2068 | it++; |
|---|
| 2069 | } |
|---|
| 2070 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 2071 | m_nestedPictureTimingSEIPresentInAU = true; |
|---|
| 2072 | #else |
|---|
| 2073 | AccessUnit::iterator it = find_if(accessUnit.begin(), accessUnit.end(), mem_fun(&NALUnit::isSlice)); |
|---|
| 2074 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 2075 | #endif |
|---|
| 2076 | } |
|---|
| 2077 | #endif |
|---|
| 2078 | |
|---|
| 2079 | } |
|---|
| 2080 | if( m_pcCfg->getDecodingUnitInfoSEIEnabled() && hrd->getSubPicCpbParamsPresentFlag() ) |
|---|
| 2081 | { |
|---|
| 2082 | m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice); |
|---|
| 2083 | for( Int i = 0; i < ( pictureTimingSEI.m_numDecodingUnitsMinus1 + 1 ); i ++ ) |
|---|
| 2084 | { |
|---|
| 2085 | OutputNALUnit nalu(NAL_UNIT_PREFIX_SEI, pcSlice->getTLayer()); |
|---|
| 2086 | |
|---|
| 2087 | SEIDecodingUnitInfo tempSEI; |
|---|
| 2088 | tempSEI.m_decodingUnitIdx = i; |
|---|
| 2089 | tempSEI.m_duSptCpbRemovalDelay = pictureTimingSEI.m_duCpbRemovalDelayMinus1[i] + 1; |
|---|
| 2090 | #if L0044_DU_DPB_OUTPUT_DELAY_HRD |
|---|
| 2091 | tempSEI.m_dpbOutputDuDelayPresentFlag = false; |
|---|
| 2092 | tempSEI.m_picSptDpbOutputDuDelay = picSptDpbOutputDuDelay; |
|---|
| 2093 | #endif |
|---|
| 2094 | |
|---|
| 2095 | AccessUnit::iterator it; |
|---|
| 2096 | // Insert the first one in the right location, before the first slice |
|---|
| 2097 | if(i == 0) |
|---|
| 2098 | { |
|---|
| 2099 | // Insert before the first slice. |
|---|
| 2100 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, tempSEI, pcSlice->getSPS()); |
|---|
| 2101 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 2102 | |
|---|
| 2103 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 2104 | UInt seiPositionInAu = xGetFirstSeiLocation(accessUnit); |
|---|
| 2105 | UInt offsetPosition = m_activeParameterSetSEIPresentInAU |
|---|
| 2106 | + m_bufferingPeriodSEIPresentInAU |
|---|
| 2107 | + m_pictureTimingSEIPresentInAU; // Insert DU info SEI after APS, BP and PT SEI |
|---|
| 2108 | for(j = 0, it = accessUnit.begin(); j < seiPositionInAu + offsetPosition; j++) |
|---|
| 2109 | { |
|---|
| 2110 | it++; |
|---|
| 2111 | } |
|---|
| 2112 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 2113 | #else |
|---|
| 2114 | it = find_if(accessUnit.begin(), accessUnit.end(), mem_fun(&NALUnit::isSlice)); |
|---|
| 2115 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 2116 | #endif |
|---|
| 2117 | } |
|---|
| 2118 | else |
|---|
| 2119 | { |
|---|
| 2120 | Int ctr; |
|---|
| 2121 | // For the second decoding unit onwards we know how many NALUs are present |
|---|
| 2122 | for (ctr = 0, it = accessUnit.begin(); it != accessUnit.end(); it++) |
|---|
| 2123 | { |
|---|
| 2124 | if(ctr == accumNalsDU[ i - 1 ]) |
|---|
| 2125 | { |
|---|
| 2126 | // Insert before the first slice. |
|---|
| 2127 | m_seiWriter.writeSEImessage(nalu.m_Bitstream, tempSEI, pcSlice->getSPS()); |
|---|
| 2128 | writeRBSPTrailingBits(nalu.m_Bitstream); |
|---|
| 2129 | |
|---|
| 2130 | accessUnit.insert(it, new NALUnitEBSP(nalu)); |
|---|
| 2131 | break; |
|---|
| 2132 | } |
|---|
| 2133 | if ((*it)->m_nalUnitType != NAL_UNIT_PREFIX_SEI && (*it)->m_nalUnitType != NAL_UNIT_SUFFIX_SEI) |
|---|
| 2134 | { |
|---|
| 2135 | ctr++; |
|---|
| 2136 | } |
|---|
| 2137 | } |
|---|
| 2138 | } |
|---|
| 2139 | } |
|---|
| 2140 | } |
|---|
| 2141 | } |
|---|
| 2142 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 2143 | xResetNonNestedSEIPresentFlags(); |
|---|
| 2144 | #if K0180_SCALABLE_NESTING_SEI |
|---|
| 2145 | xResetNestedSEIPresentFlags(); |
|---|
| 2146 | #endif |
|---|
| 2147 | #endif |
|---|
| 2148 | pcPic->getPicYuvRec()->copyToPic(pcPicYuvRecOut); |
|---|
| 2149 | |
|---|
| 2150 | pcPic->setReconMark ( true ); |
|---|
| 2151 | m_bFirst = false; |
|---|
| 2152 | m_iNumPicCoded++; |
|---|
| 2153 | m_totalCoded ++; |
|---|
| 2154 | /* logging: insert a newline at end of picture period */ |
|---|
| 2155 | printf("\n"); |
|---|
| 2156 | fflush(stdout); |
|---|
| 2157 | |
|---|
| 2158 | delete[] pcSubstreamsOut; |
|---|
| 2159 | } |
|---|
| 2160 | #if !RATE_CONTROL_LAMBDA_DOMAIN |
|---|
| 2161 | if(m_pcCfg->getUseRateCtrl()) |
|---|
| 2162 | { |
|---|
| 2163 | m_pcRateCtrl->updateRCGOPStatus(); |
|---|
| 2164 | } |
|---|
| 2165 | #endif |
|---|
| 2166 | delete pcBitstreamRedirect; |
|---|
| 2167 | |
|---|
| 2168 | if( accumBitsDU != NULL) delete accumBitsDU; |
|---|
| 2169 | if( accumNalsDU != NULL) delete accumNalsDU; |
|---|
| 2170 | |
|---|
| 2171 | #if SVC_EXTENSION |
|---|
| 2172 | assert ( m_iNumPicCoded <= 1 ); |
|---|
| 2173 | #else |
|---|
| 2174 | assert ( m_iNumPicCoded == iNumPicRcvd ); |
|---|
| 2175 | #endif |
|---|
| 2176 | } |
|---|
| 2177 | |
|---|
| 2178 | #if !SVC_EXTENSION |
|---|
| 2179 | Void TEncGOP::printOutSummary(UInt uiNumAllPicCoded) |
|---|
| 2180 | { |
|---|
| 2181 | assert (uiNumAllPicCoded == m_gcAnalyzeAll.getNumPic()); |
|---|
| 2182 | |
|---|
| 2183 | |
|---|
| 2184 | //--CFG_KDY |
|---|
| 2185 | m_gcAnalyzeAll.setFrmRate( m_pcCfg->getFrameRate() ); |
|---|
| 2186 | m_gcAnalyzeI.setFrmRate( m_pcCfg->getFrameRate() ); |
|---|
| 2187 | m_gcAnalyzeP.setFrmRate( m_pcCfg->getFrameRate() ); |
|---|
| 2188 | m_gcAnalyzeB.setFrmRate( m_pcCfg->getFrameRate() ); |
|---|
| 2189 | |
|---|
| 2190 | //-- all |
|---|
| 2191 | printf( "\n\nSUMMARY --------------------------------------------------------\n" ); |
|---|
| 2192 | m_gcAnalyzeAll.printOut('a'); |
|---|
| 2193 | |
|---|
| 2194 | printf( "\n\nI Slices--------------------------------------------------------\n" ); |
|---|
| 2195 | m_gcAnalyzeI.printOut('i'); |
|---|
| 2196 | |
|---|
| 2197 | printf( "\n\nP Slices--------------------------------------------------------\n" ); |
|---|
| 2198 | m_gcAnalyzeP.printOut('p'); |
|---|
| 2199 | |
|---|
| 2200 | printf( "\n\nB Slices--------------------------------------------------------\n" ); |
|---|
| 2201 | m_gcAnalyzeB.printOut('b'); |
|---|
| 2202 | |
|---|
| 2203 | #if _SUMMARY_OUT_ |
|---|
| 2204 | m_gcAnalyzeAll.printSummaryOut(); |
|---|
| 2205 | #endif |
|---|
| 2206 | #if _SUMMARY_PIC_ |
|---|
| 2207 | m_gcAnalyzeI.printSummary('I'); |
|---|
| 2208 | m_gcAnalyzeP.printSummary('P'); |
|---|
| 2209 | m_gcAnalyzeB.printSummary('B'); |
|---|
| 2210 | #endif |
|---|
| 2211 | |
|---|
| 2212 | printf("\nRVM: %.3lf\n" , xCalculateRVM()); |
|---|
| 2213 | } |
|---|
| 2214 | #endif |
|---|
| 2215 | |
|---|
| 2216 | Void TEncGOP::preLoopFilterPicAll( TComPic* pcPic, UInt64& ruiDist, UInt64& ruiBits ) |
|---|
| 2217 | { |
|---|
| 2218 | TComSlice* pcSlice = pcPic->getSlice(pcPic->getCurrSliceIdx()); |
|---|
| 2219 | Bool bCalcDist = false; |
|---|
| 2220 | m_pcLoopFilter->setCfg(m_pcCfg->getLFCrossTileBoundaryFlag()); |
|---|
| 2221 | m_pcLoopFilter->loopFilterPic( pcPic ); |
|---|
| 2222 | |
|---|
| 2223 | m_pcEntropyCoder->setEntropyCoder ( m_pcEncTop->getRDGoOnSbacCoder(), pcSlice ); |
|---|
| 2224 | m_pcEntropyCoder->resetEntropy (); |
|---|
| 2225 | m_pcEntropyCoder->setBitstream ( m_pcBitCounter ); |
|---|
| 2226 | pcSlice = pcPic->getSlice(0); |
|---|
| 2227 | if(pcSlice->getSPS()->getUseSAO()) |
|---|
| 2228 | { |
|---|
| 2229 | std::vector<Bool> LFCrossSliceBoundaryFlag(1, true); |
|---|
| 2230 | std::vector<Int> sliceStartAddress; |
|---|
| 2231 | sliceStartAddress.push_back(0); |
|---|
| 2232 | sliceStartAddress.push_back(pcPic->getNumCUsInFrame()* pcPic->getNumPartInCU()); |
|---|
| 2233 | pcPic->createNonDBFilterInfo(sliceStartAddress, 0, &LFCrossSliceBoundaryFlag); |
|---|
| 2234 | } |
|---|
| 2235 | |
|---|
| 2236 | if( pcSlice->getSPS()->getUseSAO()) |
|---|
| 2237 | { |
|---|
| 2238 | pcPic->destroyNonDBFilterInfo(); |
|---|
| 2239 | } |
|---|
| 2240 | |
|---|
| 2241 | m_pcEntropyCoder->resetEntropy (); |
|---|
| 2242 | ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits(); |
|---|
| 2243 | |
|---|
| 2244 | if (!bCalcDist) |
|---|
| 2245 | ruiDist = xFindDistortionFrame(pcPic->getPicYuvOrg(), pcPic->getPicYuvRec()); |
|---|
| 2246 | } |
|---|
| 2247 | |
|---|
| 2248 | // ==================================================================================================================== |
|---|
| 2249 | // Protected member functions |
|---|
| 2250 | // ==================================================================================================================== |
|---|
| 2251 | |
|---|
| 2252 | Void TEncGOP::xInitGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut ) |
|---|
| 2253 | { |
|---|
| 2254 | assert( iNumPicRcvd > 0 ); |
|---|
| 2255 | // Exception for the first frame |
|---|
| 2256 | if ( iPOCLast == 0 ) |
|---|
| 2257 | { |
|---|
| 2258 | m_iGopSize = 1; |
|---|
| 2259 | } |
|---|
| 2260 | else |
|---|
| 2261 | m_iGopSize = m_pcCfg->getGOPSize(); |
|---|
| 2262 | |
|---|
| 2263 | assert (m_iGopSize > 0); |
|---|
| 2264 | |
|---|
| 2265 | return; |
|---|
| 2266 | } |
|---|
| 2267 | |
|---|
| 2268 | Void TEncGOP::xGetBuffer( TComList<TComPic*>& rcListPic, |
|---|
| 2269 | TComList<TComPicYuv*>& rcListPicYuvRecOut, |
|---|
| 2270 | Int iNumPicRcvd, |
|---|
| 2271 | Int iTimeOffset, |
|---|
| 2272 | TComPic*& rpcPic, |
|---|
| 2273 | TComPicYuv*& rpcPicYuvRecOut, |
|---|
| 2274 | Int pocCurr ) |
|---|
| 2275 | { |
|---|
| 2276 | Int i; |
|---|
| 2277 | // Rec. output |
|---|
| 2278 | TComList<TComPicYuv*>::iterator iterPicYuvRec = rcListPicYuvRecOut.end(); |
|---|
| 2279 | for ( i = 0; i < iNumPicRcvd - iTimeOffset + 1; i++ ) |
|---|
| 2280 | { |
|---|
| 2281 | iterPicYuvRec--; |
|---|
| 2282 | } |
|---|
| 2283 | |
|---|
| 2284 | rpcPicYuvRecOut = *(iterPicYuvRec); |
|---|
| 2285 | |
|---|
| 2286 | // Current pic. |
|---|
| 2287 | TComList<TComPic*>::iterator iterPic = rcListPic.begin(); |
|---|
| 2288 | while (iterPic != rcListPic.end()) |
|---|
| 2289 | { |
|---|
| 2290 | rpcPic = *(iterPic); |
|---|
| 2291 | rpcPic->setCurrSliceIdx(0); |
|---|
| 2292 | if (rpcPic->getPOC() == pocCurr) |
|---|
| 2293 | { |
|---|
| 2294 | break; |
|---|
| 2295 | } |
|---|
| 2296 | iterPic++; |
|---|
| 2297 | } |
|---|
| 2298 | |
|---|
| 2299 | assert (rpcPic->getPOC() == pocCurr); |
|---|
| 2300 | |
|---|
| 2301 | return; |
|---|
| 2302 | } |
|---|
| 2303 | |
|---|
| 2304 | UInt64 TEncGOP::xFindDistortionFrame (TComPicYuv* pcPic0, TComPicYuv* pcPic1) |
|---|
| 2305 | { |
|---|
| 2306 | Int x, y; |
|---|
| 2307 | Pel* pSrc0 = pcPic0 ->getLumaAddr(); |
|---|
| 2308 | Pel* pSrc1 = pcPic1 ->getLumaAddr(); |
|---|
| 2309 | UInt uiShift = 2 * DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8); |
|---|
| 2310 | Int iTemp; |
|---|
| 2311 | |
|---|
| 2312 | Int iStride = pcPic0->getStride(); |
|---|
| 2313 | Int iWidth = pcPic0->getWidth(); |
|---|
| 2314 | Int iHeight = pcPic0->getHeight(); |
|---|
| 2315 | |
|---|
| 2316 | UInt64 uiTotalDiff = 0; |
|---|
| 2317 | |
|---|
| 2318 | for( y = 0; y < iHeight; y++ ) |
|---|
| 2319 | { |
|---|
| 2320 | for( x = 0; x < iWidth; x++ ) |
|---|
| 2321 | { |
|---|
| 2322 | iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift; |
|---|
| 2323 | } |
|---|
| 2324 | pSrc0 += iStride; |
|---|
| 2325 | pSrc1 += iStride; |
|---|
| 2326 | } |
|---|
| 2327 | |
|---|
| 2328 | uiShift = 2 * DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthC-8); |
|---|
| 2329 | iHeight >>= 1; |
|---|
| 2330 | iWidth >>= 1; |
|---|
| 2331 | iStride >>= 1; |
|---|
| 2332 | |
|---|
| 2333 | pSrc0 = pcPic0->getCbAddr(); |
|---|
| 2334 | pSrc1 = pcPic1->getCbAddr(); |
|---|
| 2335 | |
|---|
| 2336 | for( y = 0; y < iHeight; y++ ) |
|---|
| 2337 | { |
|---|
| 2338 | for( x = 0; x < iWidth; x++ ) |
|---|
| 2339 | { |
|---|
| 2340 | iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift; |
|---|
| 2341 | } |
|---|
| 2342 | pSrc0 += iStride; |
|---|
| 2343 | pSrc1 += iStride; |
|---|
| 2344 | } |
|---|
| 2345 | |
|---|
| 2346 | pSrc0 = pcPic0->getCrAddr(); |
|---|
| 2347 | pSrc1 = pcPic1->getCrAddr(); |
|---|
| 2348 | |
|---|
| 2349 | for( y = 0; y < iHeight; y++ ) |
|---|
| 2350 | { |
|---|
| 2351 | for( x = 0; x < iWidth; x++ ) |
|---|
| 2352 | { |
|---|
| 2353 | iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift; |
|---|
| 2354 | } |
|---|
| 2355 | pSrc0 += iStride; |
|---|
| 2356 | pSrc1 += iStride; |
|---|
| 2357 | } |
|---|
| 2358 | |
|---|
| 2359 | return uiTotalDiff; |
|---|
| 2360 | } |
|---|
| 2361 | |
|---|
| 2362 | #if VERBOSE_RATE |
|---|
| 2363 | static const Char* nalUnitTypeToString(NalUnitType type) |
|---|
| 2364 | { |
|---|
| 2365 | switch (type) |
|---|
| 2366 | { |
|---|
| 2367 | case NAL_UNIT_CODED_SLICE_TRAIL_R: return "TRAIL_R"; |
|---|
| 2368 | case NAL_UNIT_CODED_SLICE_TRAIL_N: return "TRAIL_N"; |
|---|
| 2369 | case NAL_UNIT_CODED_SLICE_TLA_R: return "TLA_R"; |
|---|
| 2370 | case NAL_UNIT_CODED_SLICE_TSA_N: return "TSA_N"; |
|---|
| 2371 | case NAL_UNIT_CODED_SLICE_STSA_R: return "STSA_R"; |
|---|
| 2372 | case NAL_UNIT_CODED_SLICE_STSA_N: return "STSA_N"; |
|---|
| 2373 | case NAL_UNIT_CODED_SLICE_BLA_W_LP: return "BLA_W_LP"; |
|---|
| 2374 | case NAL_UNIT_CODED_SLICE_BLA_W_RADL: return "BLA_W_RADL"; |
|---|
| 2375 | case NAL_UNIT_CODED_SLICE_BLA_N_LP: return "BLA_N_LP"; |
|---|
| 2376 | case NAL_UNIT_CODED_SLICE_IDR_W_RADL: return "IDR_W_RADL"; |
|---|
| 2377 | case NAL_UNIT_CODED_SLICE_IDR_N_LP: return "IDR_N_LP"; |
|---|
| 2378 | case NAL_UNIT_CODED_SLICE_CRA: return "CRA"; |
|---|
| 2379 | case NAL_UNIT_CODED_SLICE_RADL_R: return "RADL_R"; |
|---|
| 2380 | case NAL_UNIT_CODED_SLICE_RASL_R: return "RASL_R"; |
|---|
| 2381 | case NAL_UNIT_VPS: return "VPS"; |
|---|
| 2382 | case NAL_UNIT_SPS: return "SPS"; |
|---|
| 2383 | case NAL_UNIT_PPS: return "PPS"; |
|---|
| 2384 | case NAL_UNIT_ACCESS_UNIT_DELIMITER: return "AUD"; |
|---|
| 2385 | case NAL_UNIT_EOS: return "EOS"; |
|---|
| 2386 | case NAL_UNIT_EOB: return "EOB"; |
|---|
| 2387 | case NAL_UNIT_FILLER_DATA: return "FILLER"; |
|---|
| 2388 | case NAL_UNIT_PREFIX_SEI: return "SEI"; |
|---|
| 2389 | case NAL_UNIT_SUFFIX_SEI: return "SEI"; |
|---|
| 2390 | default: return "UNK"; |
|---|
| 2391 | } |
|---|
| 2392 | } |
|---|
| 2393 | #endif |
|---|
| 2394 | |
|---|
| 2395 | Void TEncGOP::xCalculateAddPSNR( TComPic* pcPic, TComPicYuv* pcPicD, const AccessUnit& accessUnit, Double dEncTime ) |
|---|
| 2396 | { |
|---|
| 2397 | Int x, y; |
|---|
| 2398 | UInt64 uiSSDY = 0; |
|---|
| 2399 | UInt64 uiSSDU = 0; |
|---|
| 2400 | UInt64 uiSSDV = 0; |
|---|
| 2401 | |
|---|
| 2402 | Double dYPSNR = 0.0; |
|---|
| 2403 | Double dUPSNR = 0.0; |
|---|
| 2404 | Double dVPSNR = 0.0; |
|---|
| 2405 | |
|---|
| 2406 | //===== calculate PSNR ===== |
|---|
| 2407 | Pel* pOrg = pcPic ->getPicYuvOrg()->getLumaAddr(); |
|---|
| 2408 | Pel* pRec = pcPicD->getLumaAddr(); |
|---|
| 2409 | Int iStride = pcPicD->getStride(); |
|---|
| 2410 | |
|---|
| 2411 | Int iWidth; |
|---|
| 2412 | Int iHeight; |
|---|
| 2413 | |
|---|
| 2414 | iWidth = pcPicD->getWidth () - m_pcEncTop->getPad(0); |
|---|
| 2415 | iHeight = pcPicD->getHeight() - m_pcEncTop->getPad(1); |
|---|
| 2416 | |
|---|
| 2417 | Int iSize = iWidth*iHeight; |
|---|
| 2418 | |
|---|
| 2419 | for( y = 0; y < iHeight; y++ ) |
|---|
| 2420 | { |
|---|
| 2421 | for( x = 0; x < iWidth; x++ ) |
|---|
| 2422 | { |
|---|
| 2423 | Int iDiff = (Int)( pOrg[x] - pRec[x] ); |
|---|
| 2424 | uiSSDY += iDiff * iDiff; |
|---|
| 2425 | } |
|---|
| 2426 | pOrg += iStride; |
|---|
| 2427 | pRec += iStride; |
|---|
| 2428 | } |
|---|
| 2429 | |
|---|
| 2430 | iHeight >>= 1; |
|---|
| 2431 | iWidth >>= 1; |
|---|
| 2432 | iStride >>= 1; |
|---|
| 2433 | pOrg = pcPic ->getPicYuvOrg()->getCbAddr(); |
|---|
| 2434 | pRec = pcPicD->getCbAddr(); |
|---|
| 2435 | |
|---|
| 2436 | for( y = 0; y < iHeight; y++ ) |
|---|
| 2437 | { |
|---|
| 2438 | for( x = 0; x < iWidth; x++ ) |
|---|
| 2439 | { |
|---|
| 2440 | Int iDiff = (Int)( pOrg[x] - pRec[x] ); |
|---|
| 2441 | uiSSDU += iDiff * iDiff; |
|---|
| 2442 | } |
|---|
| 2443 | pOrg += iStride; |
|---|
| 2444 | pRec += iStride; |
|---|
| 2445 | } |
|---|
| 2446 | |
|---|
| 2447 | pOrg = pcPic ->getPicYuvOrg()->getCrAddr(); |
|---|
| 2448 | pRec = pcPicD->getCrAddr(); |
|---|
| 2449 | |
|---|
| 2450 | for( y = 0; y < iHeight; y++ ) |
|---|
| 2451 | { |
|---|
| 2452 | for( x = 0; x < iWidth; x++ ) |
|---|
| 2453 | { |
|---|
| 2454 | Int iDiff = (Int)( pOrg[x] - pRec[x] ); |
|---|
| 2455 | uiSSDV += iDiff * iDiff; |
|---|
| 2456 | } |
|---|
| 2457 | pOrg += iStride; |
|---|
| 2458 | pRec += iStride; |
|---|
| 2459 | } |
|---|
| 2460 | |
|---|
| 2461 | Int maxvalY = 255 << (g_bitDepthY-8); |
|---|
| 2462 | Int maxvalC = 255 << (g_bitDepthC-8); |
|---|
| 2463 | Double fRefValueY = (Double) maxvalY * maxvalY * iSize; |
|---|
| 2464 | Double fRefValueC = (Double) maxvalC * maxvalC * iSize / 4.0; |
|---|
| 2465 | dYPSNR = ( uiSSDY ? 10.0 * log10( fRefValueY / (Double)uiSSDY ) : 99.99 ); |
|---|
| 2466 | dUPSNR = ( uiSSDU ? 10.0 * log10( fRefValueC / (Double)uiSSDU ) : 99.99 ); |
|---|
| 2467 | dVPSNR = ( uiSSDV ? 10.0 * log10( fRefValueC / (Double)uiSSDV ) : 99.99 ); |
|---|
| 2468 | |
|---|
| 2469 | /* calculate the size of the access unit, excluding: |
|---|
| 2470 | * - any AnnexB contributions (start_code_prefix, zero_byte, etc.,) |
|---|
| 2471 | * - SEI NAL units |
|---|
| 2472 | */ |
|---|
| 2473 | UInt numRBSPBytes = 0; |
|---|
| 2474 | for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++) |
|---|
| 2475 | { |
|---|
| 2476 | UInt numRBSPBytes_nal = UInt((*it)->m_nalUnitData.str().size()); |
|---|
| 2477 | #if VERBOSE_RATE |
|---|
| 2478 | printf("*** %6s numBytesInNALunit: %u\n", nalUnitTypeToString((*it)->m_nalUnitType), numRBSPBytes_nal); |
|---|
| 2479 | #endif |
|---|
| 2480 | if ((*it)->m_nalUnitType != NAL_UNIT_PREFIX_SEI && (*it)->m_nalUnitType != NAL_UNIT_SUFFIX_SEI) |
|---|
| 2481 | { |
|---|
| 2482 | numRBSPBytes += numRBSPBytes_nal; |
|---|
| 2483 | } |
|---|
| 2484 | } |
|---|
| 2485 | |
|---|
| 2486 | UInt uibits = numRBSPBytes * 8; |
|---|
| 2487 | m_vRVM_RP.push_back( uibits ); |
|---|
| 2488 | |
|---|
| 2489 | //===== add PSNR ===== |
|---|
| 2490 | #if SVC_EXTENSION |
|---|
| 2491 | m_gcAnalyzeAll[m_layerId].addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits); |
|---|
| 2492 | TComSlice* pcSlice = pcPic->getSlice(0); |
|---|
| 2493 | if (pcSlice->isIntra()) |
|---|
| 2494 | { |
|---|
| 2495 | m_gcAnalyzeI[m_layerId].addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits); |
|---|
| 2496 | } |
|---|
| 2497 | if (pcSlice->isInterP()) |
|---|
| 2498 | { |
|---|
| 2499 | m_gcAnalyzeP[m_layerId].addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits); |
|---|
| 2500 | } |
|---|
| 2501 | if (pcSlice->isInterB()) |
|---|
| 2502 | { |
|---|
| 2503 | m_gcAnalyzeB[m_layerId].addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits); |
|---|
| 2504 | } |
|---|
| 2505 | #else |
|---|
| 2506 | m_gcAnalyzeAll.addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits); |
|---|
| 2507 | TComSlice* pcSlice = pcPic->getSlice(0); |
|---|
| 2508 | if (pcSlice->isIntra()) |
|---|
| 2509 | { |
|---|
| 2510 | m_gcAnalyzeI.addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits); |
|---|
| 2511 | } |
|---|
| 2512 | if (pcSlice->isInterP()) |
|---|
| 2513 | { |
|---|
| 2514 | m_gcAnalyzeP.addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits); |
|---|
| 2515 | } |
|---|
| 2516 | if (pcSlice->isInterB()) |
|---|
| 2517 | { |
|---|
| 2518 | m_gcAnalyzeB.addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits); |
|---|
| 2519 | } |
|---|
| 2520 | #endif |
|---|
| 2521 | |
|---|
| 2522 | Char c = (pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B'); |
|---|
| 2523 | if (!pcSlice->isReferenced()) c += 32; |
|---|
| 2524 | |
|---|
| 2525 | #if SVC_EXTENSION |
|---|
| 2526 | #if ADAPTIVE_QP_SELECTION |
|---|
| 2527 | printf("POC %4d LId: %1d TId: %1d ( %c-SLICE, nQP %d QP %d ) %10d bits", |
|---|
| 2528 | pcSlice->getPOC(), |
|---|
| 2529 | pcSlice->getLayerId(), |
|---|
| 2530 | pcSlice->getTLayer(), |
|---|
| 2531 | c, |
|---|
| 2532 | pcSlice->getSliceQpBase(), |
|---|
| 2533 | pcSlice->getSliceQp(), |
|---|
| 2534 | uibits ); |
|---|
| 2535 | #else |
|---|
| 2536 | printf("POC %4d LId: %1d TId: %1d ( %c-SLICE, QP %d ) %10d bits", |
|---|
| 2537 | pcSlice->getPOC()-pcSlice->getLastIDR(), |
|---|
| 2538 | pcSlice->getLayerId(), |
|---|
| 2539 | pcSlice->getTLayer(), |
|---|
| 2540 | c, |
|---|
| 2541 | pcSlice->getSliceQp(), |
|---|
| 2542 | uibits ); |
|---|
| 2543 | #endif |
|---|
| 2544 | #else |
|---|
| 2545 | #if ADAPTIVE_QP_SELECTION |
|---|
| 2546 | printf("POC %4d TId: %1d ( %c-SLICE, nQP %d QP %d ) %10d bits", |
|---|
| 2547 | pcSlice->getPOC(), |
|---|
| 2548 | pcSlice->getTLayer(), |
|---|
| 2549 | c, |
|---|
| 2550 | pcSlice->getSliceQpBase(), |
|---|
| 2551 | pcSlice->getSliceQp(), |
|---|
| 2552 | uibits ); |
|---|
| 2553 | #else |
|---|
| 2554 | printf("POC %4d TId: %1d ( %c-SLICE, QP %d ) %10d bits", |
|---|
| 2555 | pcSlice->getPOC()-pcSlice->getLastIDR(), |
|---|
| 2556 | pcSlice->getTLayer(), |
|---|
| 2557 | c, |
|---|
| 2558 | pcSlice->getSliceQp(), |
|---|
| 2559 | uibits ); |
|---|
| 2560 | #endif |
|---|
| 2561 | #endif |
|---|
| 2562 | |
|---|
| 2563 | printf(" [Y %6.4lf dB U %6.4lf dB V %6.4lf dB]", dYPSNR, dUPSNR, dVPSNR ); |
|---|
| 2564 | printf(" [ET %5.0f ]", dEncTime ); |
|---|
| 2565 | |
|---|
| 2566 | for (Int iRefList = 0; iRefList < 2; iRefList++) |
|---|
| 2567 | { |
|---|
| 2568 | printf(" [L%d ", iRefList); |
|---|
| 2569 | for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(RefPicList(iRefList)); iRefIndex++) |
|---|
| 2570 | { |
|---|
| 2571 | #if REF_IDX_FRAMEWORK && VPS_EXTN_DIRECT_REF_LAYERS_CONTINUE |
|---|
| 2572 | if( pcSlice->getRefPic(RefPicList(iRefList), iRefIndex)->isILR(m_layerId) ) |
|---|
| 2573 | { |
|---|
| 2574 | printf( "%d(%d) ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex)-pcSlice->getLastIDR(), pcSlice->getRefPic(RefPicList(iRefList), iRefIndex)->getLayerId() ); |
|---|
| 2575 | } |
|---|
| 2576 | else |
|---|
| 2577 | #endif |
|---|
| 2578 | printf ("%d ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex)-pcSlice->getLastIDR()); |
|---|
| 2579 | } |
|---|
| 2580 | printf("]"); |
|---|
| 2581 | } |
|---|
| 2582 | } |
|---|
| 2583 | |
|---|
| 2584 | /** Function for deciding the nal_unit_type. |
|---|
| 2585 | * \param pocCurr POC of the current picture |
|---|
| 2586 | * \returns the nal unit type of the picture |
|---|
| 2587 | * This function checks the configuration and returns the appropriate nal_unit_type for the picture. |
|---|
| 2588 | */ |
|---|
| 2589 | NalUnitType TEncGOP::getNalUnitType(Int pocCurr, Int lastIDR) |
|---|
| 2590 | { |
|---|
| 2591 | if (pocCurr == 0) |
|---|
| 2592 | { |
|---|
| 2593 | return NAL_UNIT_CODED_SLICE_IDR_W_RADL; |
|---|
| 2594 | } |
|---|
| 2595 | if (pocCurr % m_pcCfg->getIntraPeriod() == 0) |
|---|
| 2596 | { |
|---|
| 2597 | if (m_pcCfg->getDecodingRefreshType() == 1) |
|---|
| 2598 | { |
|---|
| 2599 | return NAL_UNIT_CODED_SLICE_CRA; |
|---|
| 2600 | } |
|---|
| 2601 | else if (m_pcCfg->getDecodingRefreshType() == 2) |
|---|
| 2602 | { |
|---|
| 2603 | return NAL_UNIT_CODED_SLICE_IDR_W_RADL; |
|---|
| 2604 | } |
|---|
| 2605 | } |
|---|
| 2606 | if(m_pocCRA>0) |
|---|
| 2607 | { |
|---|
| 2608 | if(pocCurr<m_pocCRA) |
|---|
| 2609 | { |
|---|
| 2610 | // All leading pictures are being marked as TFD pictures here since current encoder uses all |
|---|
| 2611 | // reference pictures while encoding leading pictures. An encoder can ensure that a leading |
|---|
| 2612 | // picture can be still decodable when random accessing to a CRA/CRANT/BLA/BLANT picture by |
|---|
| 2613 | // controlling the reference pictures used for encoding that leading picture. Such a leading |
|---|
| 2614 | // picture need not be marked as a TFD picture. |
|---|
| 2615 | return NAL_UNIT_CODED_SLICE_RASL_R; |
|---|
| 2616 | } |
|---|
| 2617 | } |
|---|
| 2618 | if (lastIDR>0) |
|---|
| 2619 | { |
|---|
| 2620 | if (pocCurr < lastIDR) |
|---|
| 2621 | { |
|---|
| 2622 | return NAL_UNIT_CODED_SLICE_RADL_R; |
|---|
| 2623 | } |
|---|
| 2624 | } |
|---|
| 2625 | return NAL_UNIT_CODED_SLICE_TRAIL_R; |
|---|
| 2626 | } |
|---|
| 2627 | |
|---|
| 2628 | Double TEncGOP::xCalculateRVM() |
|---|
| 2629 | { |
|---|
| 2630 | Double dRVM = 0; |
|---|
| 2631 | |
|---|
| 2632 | if( m_pcCfg->getGOPSize() == 1 && m_pcCfg->getIntraPeriod() != 1 && m_pcCfg->getFramesToBeEncoded() > RVM_VCEGAM10_M * 2 ) |
|---|
| 2633 | { |
|---|
| 2634 | // calculate RVM only for lowdelay configurations |
|---|
| 2635 | std::vector<Double> vRL , vB; |
|---|
| 2636 | size_t N = m_vRVM_RP.size(); |
|---|
| 2637 | vRL.resize( N ); |
|---|
| 2638 | vB.resize( N ); |
|---|
| 2639 | |
|---|
| 2640 | Int i; |
|---|
| 2641 | Double dRavg = 0 , dBavg = 0; |
|---|
| 2642 | vB[RVM_VCEGAM10_M] = 0; |
|---|
| 2643 | for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ ) |
|---|
| 2644 | { |
|---|
| 2645 | vRL[i] = 0; |
|---|
| 2646 | for( Int j = i - RVM_VCEGAM10_M ; j <= i + RVM_VCEGAM10_M - 1 ; j++ ) |
|---|
| 2647 | vRL[i] += m_vRVM_RP[j]; |
|---|
| 2648 | vRL[i] /= ( 2 * RVM_VCEGAM10_M ); |
|---|
| 2649 | vB[i] = vB[i-1] + m_vRVM_RP[i] - vRL[i]; |
|---|
| 2650 | dRavg += m_vRVM_RP[i]; |
|---|
| 2651 | dBavg += vB[i]; |
|---|
| 2652 | } |
|---|
| 2653 | |
|---|
| 2654 | dRavg /= ( N - 2 * RVM_VCEGAM10_M ); |
|---|
| 2655 | dBavg /= ( N - 2 * RVM_VCEGAM10_M ); |
|---|
| 2656 | |
|---|
| 2657 | Double dSigamB = 0; |
|---|
| 2658 | for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ ) |
|---|
| 2659 | { |
|---|
| 2660 | Double tmp = vB[i] - dBavg; |
|---|
| 2661 | dSigamB += tmp * tmp; |
|---|
| 2662 | } |
|---|
| 2663 | dSigamB = sqrt( dSigamB / ( N - 2 * RVM_VCEGAM10_M ) ); |
|---|
| 2664 | |
|---|
| 2665 | Double f = sqrt( 12.0 * ( RVM_VCEGAM10_M - 1 ) / ( RVM_VCEGAM10_M + 1 ) ); |
|---|
| 2666 | |
|---|
| 2667 | dRVM = dSigamB / dRavg * f; |
|---|
| 2668 | } |
|---|
| 2669 | |
|---|
| 2670 | return( dRVM ); |
|---|
| 2671 | } |
|---|
| 2672 | |
|---|
| 2673 | /** Attaches the input bitstream to the stream in the output NAL unit |
|---|
| 2674 | Updates rNalu to contain concatenated bitstream. rpcBitstreamRedirect is cleared at the end of this function call. |
|---|
| 2675 | * \param codedSliceData contains the coded slice data (bitstream) to be concatenated to rNalu |
|---|
| 2676 | * \param rNalu target NAL unit |
|---|
| 2677 | */ |
|---|
| 2678 | Void TEncGOP::xAttachSliceDataToNalUnit (OutputNALUnit& rNalu, TComOutputBitstream*& codedSliceData) |
|---|
| 2679 | { |
|---|
| 2680 | // Byte-align |
|---|
| 2681 | rNalu.m_Bitstream.writeByteAlignment(); // Slice header byte-alignment |
|---|
| 2682 | |
|---|
| 2683 | // Perform bitstream concatenation |
|---|
| 2684 | if (codedSliceData->getNumberOfWrittenBits() > 0) |
|---|
| 2685 | { |
|---|
| 2686 | rNalu.m_Bitstream.addSubstream(codedSliceData); |
|---|
| 2687 | } |
|---|
| 2688 | |
|---|
| 2689 | m_pcEntropyCoder->setBitstream(&rNalu.m_Bitstream); |
|---|
| 2690 | |
|---|
| 2691 | codedSliceData->clear(); |
|---|
| 2692 | } |
|---|
| 2693 | |
|---|
| 2694 | // Function will arrange the long-term pictures in the decreasing order of poc_lsb_lt, |
|---|
| 2695 | // and among the pictures with the same lsb, it arranges them in increasing delta_poc_msb_cycle_lt value |
|---|
| 2696 | Void TEncGOP::arrangeLongtermPicturesInRPS(TComSlice *pcSlice, TComList<TComPic*>& rcListPic) |
|---|
| 2697 | { |
|---|
| 2698 | TComReferencePictureSet *rps = pcSlice->getRPS(); |
|---|
| 2699 | if(!rps->getNumberOfLongtermPictures()) |
|---|
| 2700 | { |
|---|
| 2701 | return; |
|---|
| 2702 | } |
|---|
| 2703 | |
|---|
| 2704 | // Arrange long-term reference pictures in the correct order of LSB and MSB, |
|---|
| 2705 | // and assign values for pocLSBLT and MSB present flag |
|---|
| 2706 | Int longtermPicsPoc[MAX_NUM_REF_PICS], longtermPicsLSB[MAX_NUM_REF_PICS], indices[MAX_NUM_REF_PICS]; |
|---|
| 2707 | Int longtermPicsMSB[MAX_NUM_REF_PICS]; |
|---|
| 2708 | Bool mSBPresentFlag[MAX_NUM_REF_PICS]; |
|---|
| 2709 | ::memset(longtermPicsPoc, 0, sizeof(longtermPicsPoc)); // Store POC values of LTRP |
|---|
| 2710 | ::memset(longtermPicsLSB, 0, sizeof(longtermPicsLSB)); // Store POC LSB values of LTRP |
|---|
| 2711 | ::memset(longtermPicsMSB, 0, sizeof(longtermPicsMSB)); // Store POC LSB values of LTRP |
|---|
| 2712 | ::memset(indices , 0, sizeof(indices)); // Indices to aid in tracking sorted LTRPs |
|---|
| 2713 | ::memset(mSBPresentFlag , 0, sizeof(mSBPresentFlag)); // Indicate if MSB needs to be present |
|---|
| 2714 | |
|---|
| 2715 | // Get the long-term reference pictures |
|---|
| 2716 | Int offset = rps->getNumberOfNegativePictures() + rps->getNumberOfPositivePictures(); |
|---|
| 2717 | Int i, ctr = 0; |
|---|
| 2718 | Int maxPicOrderCntLSB = 1 << pcSlice->getSPS()->getBitsForPOC(); |
|---|
| 2719 | for(i = rps->getNumberOfPictures() - 1; i >= offset; i--, ctr++) |
|---|
| 2720 | { |
|---|
| 2721 | longtermPicsPoc[ctr] = rps->getPOC(i); // LTRP POC |
|---|
| 2722 | longtermPicsLSB[ctr] = getLSB(longtermPicsPoc[ctr], maxPicOrderCntLSB); // LTRP POC LSB |
|---|
| 2723 | indices[ctr] = i; |
|---|
| 2724 | longtermPicsMSB[ctr] = longtermPicsPoc[ctr] - longtermPicsLSB[ctr]; |
|---|
| 2725 | } |
|---|
| 2726 | Int numLongPics = rps->getNumberOfLongtermPictures(); |
|---|
| 2727 | assert(ctr == numLongPics); |
|---|
| 2728 | |
|---|
| 2729 | // Arrange pictures in decreasing order of MSB; |
|---|
| 2730 | for(i = 0; i < numLongPics; i++) |
|---|
| 2731 | { |
|---|
| 2732 | for(Int j = 0; j < numLongPics - 1; j++) |
|---|
| 2733 | { |
|---|
| 2734 | if(longtermPicsMSB[j] < longtermPicsMSB[j+1]) |
|---|
| 2735 | { |
|---|
| 2736 | std::swap(longtermPicsPoc[j], longtermPicsPoc[j+1]); |
|---|
| 2737 | std::swap(longtermPicsLSB[j], longtermPicsLSB[j+1]); |
|---|
| 2738 | std::swap(longtermPicsMSB[j], longtermPicsMSB[j+1]); |
|---|
| 2739 | std::swap(indices[j] , indices[j+1] ); |
|---|
| 2740 | } |
|---|
| 2741 | } |
|---|
| 2742 | } |
|---|
| 2743 | |
|---|
| 2744 | for(i = 0; i < numLongPics; i++) |
|---|
| 2745 | { |
|---|
| 2746 | // Check if MSB present flag should be enabled. |
|---|
| 2747 | // Check if the buffer contains any pictures that have the same LSB. |
|---|
| 2748 | TComList<TComPic*>::iterator iterPic = rcListPic.begin(); |
|---|
| 2749 | TComPic* pcPic; |
|---|
| 2750 | while ( iterPic != rcListPic.end() ) |
|---|
| 2751 | { |
|---|
| 2752 | pcPic = *iterPic; |
|---|
| 2753 | if( (getLSB(pcPic->getPOC(), maxPicOrderCntLSB) == longtermPicsLSB[i]) && // Same LSB |
|---|
| 2754 | (pcPic->getSlice(0)->isReferenced()) && // Reference picture |
|---|
| 2755 | (pcPic->getPOC() != longtermPicsPoc[i]) ) // Not the LTRP itself |
|---|
| 2756 | { |
|---|
| 2757 | mSBPresentFlag[i] = true; |
|---|
| 2758 | break; |
|---|
| 2759 | } |
|---|
| 2760 | iterPic++; |
|---|
| 2761 | } |
|---|
| 2762 | } |
|---|
| 2763 | |
|---|
| 2764 | // tempArray for usedByCurr flag |
|---|
| 2765 | Bool tempArray[MAX_NUM_REF_PICS]; ::memset(tempArray, 0, sizeof(tempArray)); |
|---|
| 2766 | for(i = 0; i < numLongPics; i++) |
|---|
| 2767 | { |
|---|
| 2768 | tempArray[i] = rps->getUsed(indices[i]); |
|---|
| 2769 | } |
|---|
| 2770 | // Now write the final values; |
|---|
| 2771 | ctr = 0; |
|---|
| 2772 | Int currMSB = 0, currLSB = 0; |
|---|
| 2773 | // currPicPoc = currMSB + currLSB |
|---|
| 2774 | currLSB = getLSB(pcSlice->getPOC(), maxPicOrderCntLSB); |
|---|
| 2775 | currMSB = pcSlice->getPOC() - currLSB; |
|---|
| 2776 | |
|---|
| 2777 | for(i = rps->getNumberOfPictures() - 1; i >= offset; i--, ctr++) |
|---|
| 2778 | { |
|---|
| 2779 | rps->setPOC (i, longtermPicsPoc[ctr]); |
|---|
| 2780 | rps->setDeltaPOC (i, - pcSlice->getPOC() + longtermPicsPoc[ctr]); |
|---|
| 2781 | rps->setUsed (i, tempArray[ctr]); |
|---|
| 2782 | rps->setPocLSBLT (i, longtermPicsLSB[ctr]); |
|---|
| 2783 | rps->setDeltaPocMSBCycleLT (i, (currMSB - (longtermPicsPoc[ctr] - longtermPicsLSB[ctr])) / maxPicOrderCntLSB); |
|---|
| 2784 | rps->setDeltaPocMSBPresentFlag(i, mSBPresentFlag[ctr]); |
|---|
| 2785 | |
|---|
| 2786 | assert(rps->getDeltaPocMSBCycleLT(i) >= 0); // Non-negative value |
|---|
| 2787 | } |
|---|
| 2788 | for(i = rps->getNumberOfPictures() - 1, ctr = 1; i >= offset; i--, ctr++) |
|---|
| 2789 | { |
|---|
| 2790 | for(Int j = rps->getNumberOfPictures() - 1 - ctr; j >= offset; j--) |
|---|
| 2791 | { |
|---|
| 2792 | // Here at the encoder we know that we have set the full POC value for the LTRPs, hence we |
|---|
| 2793 | // don't have to check the MSB present flag values for this constraint. |
|---|
| 2794 | assert( rps->getPOC(i) != rps->getPOC(j) ); // If assert fails, LTRP entry repeated in RPS!!! |
|---|
| 2795 | } |
|---|
| 2796 | } |
|---|
| 2797 | } |
|---|
| 2798 | |
|---|
| 2799 | #if L0045_NON_NESTED_SEI_RESTRICTIONS |
|---|
| 2800 | /** Function for finding the position to insert the first of APS and non-nested BP, PT, DU info SEI messages. |
|---|
| 2801 | * \param accessUnit Access Unit of the current picture |
|---|
| 2802 | * This function finds the position to insert the first of APS and non-nested BP, PT, DU info SEI messages. |
|---|
| 2803 | */ |
|---|
| 2804 | Int TEncGOP::xGetFirstSeiLocation(AccessUnit &accessUnit) |
|---|
| 2805 | { |
|---|
| 2806 | // Find the location of the first SEI message |
|---|
| 2807 | AccessUnit::iterator it; |
|---|
| 2808 | Int seiStartPos = 0; |
|---|
| 2809 | for(it = accessUnit.begin(); it != accessUnit.end(); it++, seiStartPos++) |
|---|
| 2810 | { |
|---|
| 2811 | if ((*it)->isSei() || (*it)->isVcl()) |
|---|
| 2812 | { |
|---|
| 2813 | break; |
|---|
| 2814 | } |
|---|
| 2815 | } |
|---|
| 2816 | // assert(it != accessUnit.end()); // Triggers with some legit configurations |
|---|
| 2817 | return seiStartPos; |
|---|
| 2818 | } |
|---|
| 2819 | #endif |
|---|
| 2820 | |
|---|
| 2821 | #if L0386_DB_METRIC |
|---|
| 2822 | Void TEncGOP::dblMetric( TComPic* pcPic, UInt uiNumSlices ) |
|---|
| 2823 | { |
|---|
| 2824 | TComPicYuv* pcPicYuvRec = pcPic->getPicYuvRec(); |
|---|
| 2825 | Pel* Rec = pcPicYuvRec->getLumaAddr( 0 ); |
|---|
| 2826 | Pel* tempRec = Rec; |
|---|
| 2827 | Int stride = pcPicYuvRec->getStride(); |
|---|
| 2828 | UInt log2maxTB = pcPic->getSlice(0)->getSPS()->getQuadtreeTULog2MaxSize(); |
|---|
| 2829 | UInt maxTBsize = (1<<log2maxTB); |
|---|
| 2830 | const UInt minBlockArtSize = 8; |
|---|
| 2831 | const UInt picWidth = pcPicYuvRec->getWidth(); |
|---|
| 2832 | const UInt picHeight = pcPicYuvRec->getHeight(); |
|---|
| 2833 | const UInt noCol = (picWidth>>log2maxTB); |
|---|
| 2834 | const UInt noRows = (picHeight>>log2maxTB); |
|---|
| 2835 | UInt64 *colSAD = (UInt64*)malloc(noCol*sizeof(UInt64)); |
|---|
| 2836 | UInt64 *rowSAD = (UInt64*)malloc(noRows*sizeof(UInt64)); |
|---|
| 2837 | UInt colIdx = 0; |
|---|
| 2838 | UInt rowIdx = 0; |
|---|
| 2839 | Pel p0, p1, p2, q0, q1, q2; |
|---|
| 2840 | |
|---|
| 2841 | Int qp = pcPic->getSlice(0)->getSliceQp(); |
|---|
| 2842 | Int bitdepthScale = 1 << (g_bitDepthY-8); |
|---|
| 2843 | Int beta = TComLoopFilter::getBeta( qp ) * bitdepthScale; |
|---|
| 2844 | const Int thr2 = (beta>>2); |
|---|
| 2845 | const Int thr1 = 2*bitdepthScale; |
|---|
| 2846 | UInt a = 0; |
|---|
| 2847 | |
|---|
| 2848 | memset(colSAD, 0, noCol*sizeof(UInt64)); |
|---|
| 2849 | memset(rowSAD, 0, noRows*sizeof(UInt64)); |
|---|
| 2850 | |
|---|
| 2851 | if (maxTBsize > minBlockArtSize) |
|---|
| 2852 | { |
|---|
| 2853 | // Analyze vertical artifact edges |
|---|
| 2854 | for(Int c = maxTBsize; c < picWidth; c += maxTBsize) |
|---|
| 2855 | { |
|---|
| 2856 | for(Int r = 0; r < picHeight; r++) |
|---|
| 2857 | { |
|---|
| 2858 | p2 = Rec[c-3]; |
|---|
| 2859 | p1 = Rec[c-2]; |
|---|
| 2860 | p0 = Rec[c-1]; |
|---|
| 2861 | q0 = Rec[c]; |
|---|
| 2862 | q1 = Rec[c+1]; |
|---|
| 2863 | q2 = Rec[c+2]; |
|---|
| 2864 | a = ((abs(p2-(p1<<1)+p0)+abs(q0-(q1<<1)+q2))<<1); |
|---|
| 2865 | if ( thr1 < a && a < thr2) |
|---|
| 2866 | { |
|---|
| 2867 | colSAD[colIdx] += abs(p0 - q0); |
|---|
| 2868 | } |
|---|
| 2869 | Rec += stride; |
|---|
| 2870 | } |
|---|
| 2871 | colIdx++; |
|---|
| 2872 | Rec = tempRec; |
|---|
| 2873 | } |
|---|
| 2874 | |
|---|
| 2875 | // Analyze horizontal artifact edges |
|---|
| 2876 | for(Int r = maxTBsize; r < picHeight; r += maxTBsize) |
|---|
| 2877 | { |
|---|
| 2878 | for(Int c = 0; c < picWidth; c++) |
|---|
| 2879 | { |
|---|
| 2880 | p2 = Rec[c + (r-3)*stride]; |
|---|
| 2881 | p1 = Rec[c + (r-2)*stride]; |
|---|
| 2882 | p0 = Rec[c + (r-1)*stride]; |
|---|
| 2883 | q0 = Rec[c + r*stride]; |
|---|
| 2884 | q1 = Rec[c + (r+1)*stride]; |
|---|
| 2885 | q2 = Rec[c + (r+2)*stride]; |
|---|
| 2886 | a = ((abs(p2-(p1<<1)+p0)+abs(q0-(q1<<1)+q2))<<1); |
|---|
| 2887 | if (thr1 < a && a < thr2) |
|---|
| 2888 | { |
|---|
| 2889 | rowSAD[rowIdx] += abs(p0 - q0); |
|---|
| 2890 | } |
|---|
| 2891 | } |
|---|
| 2892 | rowIdx++; |
|---|
| 2893 | } |
|---|
| 2894 | } |
|---|
| 2895 | |
|---|
| 2896 | UInt64 colSADsum = 0; |
|---|
| 2897 | UInt64 rowSADsum = 0; |
|---|
| 2898 | for(Int c = 0; c < noCol-1; c++) |
|---|
| 2899 | { |
|---|
| 2900 | colSADsum += colSAD[c]; |
|---|
| 2901 | } |
|---|
| 2902 | for(Int r = 0; r < noRows-1; r++) |
|---|
| 2903 | { |
|---|
| 2904 | rowSADsum += rowSAD[r]; |
|---|
| 2905 | } |
|---|
| 2906 | |
|---|
| 2907 | colSADsum <<= 10; |
|---|
| 2908 | rowSADsum <<= 10; |
|---|
| 2909 | colSADsum /= (noCol-1); |
|---|
| 2910 | colSADsum /= picHeight; |
|---|
| 2911 | rowSADsum /= (noRows-1); |
|---|
| 2912 | rowSADsum /= picWidth; |
|---|
| 2913 | |
|---|
| 2914 | UInt64 avgSAD = ((colSADsum + rowSADsum)>>1); |
|---|
| 2915 | avgSAD >>= (g_bitDepthY-8); |
|---|
| 2916 | |
|---|
| 2917 | if ( avgSAD > 2048 ) |
|---|
| 2918 | { |
|---|
| 2919 | avgSAD >>= 9; |
|---|
| 2920 | Int offset = Clip3(2,6,(Int)avgSAD); |
|---|
| 2921 | for (Int i=0; i<uiNumSlices; i++) |
|---|
| 2922 | { |
|---|
| 2923 | pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(true); |
|---|
| 2924 | pcPic->getSlice(i)->setDeblockingFilterDisable(false); |
|---|
| 2925 | pcPic->getSlice(i)->setDeblockingFilterBetaOffsetDiv2( offset ); |
|---|
| 2926 | pcPic->getSlice(i)->setDeblockingFilterTcOffsetDiv2( offset ); |
|---|
| 2927 | } |
|---|
| 2928 | } |
|---|
| 2929 | else |
|---|
| 2930 | { |
|---|
| 2931 | for (Int i=0; i<uiNumSlices; i++) |
|---|
| 2932 | { |
|---|
| 2933 | pcPic->getSlice(i)->setDeblockingFilterOverrideFlag(false); |
|---|
| 2934 | pcPic->getSlice(i)->setDeblockingFilterDisable( pcPic->getSlice(i)->getPPS()->getPicDisableDeblockingFilterFlag() ); |
|---|
| 2935 | pcPic->getSlice(i)->setDeblockingFilterBetaOffsetDiv2( pcPic->getSlice(i)->getPPS()->getDeblockingFilterBetaOffsetDiv2() ); |
|---|
| 2936 | pcPic->getSlice(i)->setDeblockingFilterTcOffsetDiv2( pcPic->getSlice(i)->getPPS()->getDeblockingFilterTcOffsetDiv2() ); |
|---|
| 2937 | } |
|---|
| 2938 | } |
|---|
| 2939 | |
|---|
| 2940 | free(colSAD); |
|---|
| 2941 | free(rowSAD); |
|---|
| 2942 | } |
|---|
| 2943 | #endif |
|---|
| 2944 | //! \} |
|---|