Newer
Older

Karsten Suehring
committed
/* The copyright in this software is being made available under the BSD
* License, included below. This software may be subject to other third party
* and contributor rights, including patent rights, and no such rights are
* granted under this license.
*
* Copyright (c) 2010-2019, ITU/ISO/IEC

Karsten Suehring
committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/** \file EncModeCtrl.cpp
\brief Encoder controller for trying out specific modes
*/
#include "EncModeCtrl.h"
#include "AQp.h"
#include "RateCtrl.h"
#include "CommonLib/RdCost.h"
#include "CommonLib/CodingStructure.h"
#include "CommonLib/Picture.h"
#include "CommonLib/UnitTools.h"
#include "CommonLib/dtrace_next.h"
#include <cmath>
void EncModeCtrl::init( EncCfg *pCfg, RateCtrl *pRateCtrl, RdCost* pRdCost )
{
m_pcEncCfg = pCfg;
m_pcRateCtrl = pRateCtrl;
m_pcRdCost = pRdCost;
m_fastDeltaQP = false;
#if SHARP_LUMA_DELTA_QP
m_lumaQPOffset = 0;
initLumaDeltaQpLUT();
#endif
}
bool EncModeCtrl::tryModeMaster( const EncTestMode& encTestmode, const CodingStructure &cs, Partitioner& partitioner )
{
#if ENABLE_SPLIT_PARALLELISM
if( m_ComprCUCtxList.back().isLevelSplitParallel )
{
if( !parallelJobSelector( encTestmode, cs, partitioner ) )
{
return false;
}
}
#endif
return tryMode( encTestmode, cs, partitioner );
}
void EncModeCtrl::setEarlySkipDetected()
{
m_ComprCUCtxList.back().earlySkip = true;
}
void EncModeCtrl::xExtractFeatures( const EncTestMode encTestmode, CodingStructure& cs )
{
CHECK( cs.features.size() < NUM_ENC_FEATURES, "Features vector is not initialized" );
cs.features[ENC_FT_DISTORTION ] = double( cs.dist );
cs.features[ENC_FT_FRAC_BITS ] = double( cs.fracBits );
cs.features[ENC_FT_RD_COST ] = double( cs.cost );
cs.features[ENC_FT_ENC_MODE_TYPE ] = double( encTestmode.type );
cs.features[ENC_FT_ENC_MODE_OPTS ] = double( encTestmode.opts );
}
bool EncModeCtrl::nextMode( const CodingStructure &cs, Partitioner &partitioner )
{
m_ComprCUCtxList.back().lastTestMode = m_ComprCUCtxList.back().testModes.back();
m_ComprCUCtxList.back().testModes.pop_back();
while( !m_ComprCUCtxList.back().testModes.empty() && !tryModeMaster( currTestMode(), cs, partitioner ) )
{
m_ComprCUCtxList.back().testModes.pop_back();
}
return !m_ComprCUCtxList.back().testModes.empty();
}
EncTestMode EncModeCtrl::currTestMode() const
{
return m_ComprCUCtxList.back().testModes.back();
}
EncTestMode EncModeCtrl::lastTestMode() const
{
return m_ComprCUCtxList.back().lastTestMode;
}
bool EncModeCtrl::anyMode() const
{
return !m_ComprCUCtxList.back().testModes.empty();
}
void EncModeCtrl::setBest( CodingStructure& cs )
{
if( cs.cost != MAX_DOUBLE && !cs.cus.empty() )
{
m_ComprCUCtxList.back().bestCS = &cs;
m_ComprCUCtxList.back().bestCU = cs.cus[0];
m_ComprCUCtxList.back().bestTU = cs.cus[0]->firstTU;
m_ComprCUCtxList.back().lastTestMode = getCSEncMode( cs );
}
}
void EncModeCtrl::xGetMinMaxQP( int& minQP, int& maxQP, const CodingStructure& cs, const Partitioner &partitioner, const int baseQP, const SPS& sps, const PPS& pps, const PartSplit splitMode )

Karsten Suehring
committed
{
if( m_pcEncCfg->getUseRateCtrl() )
{
minQP = m_pcRateCtrl->getRCQP();
maxQP = m_pcRateCtrl->getRCQP();
return;
}
const unsigned subdivIncr = (splitMode == CU_QUAD_SPLIT) ? 2 : (splitMode == CU_BT_SPLIT) ? 1 : 0;
const bool qgEnable = partitioner.currQgEnable(); // QG possible at current level
const bool qgEnableChildren = qgEnable && ((partitioner.currSubdiv + subdivIncr) <= pps.getCuQpDeltaSubdiv()) && (subdivIncr > 0); // QG possible at next level
const bool isLeafQG = (qgEnable && !qgEnableChildren);
if( isLeafQG ) // QG at deepest level
{
int deltaQP = m_pcEncCfg->getMaxDeltaQP();
minQP = Clip3( -sps.getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, baseQP - deltaQP );
maxQP = Clip3( -sps.getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, baseQP + deltaQP );
}
else if( qgEnableChildren ) // more splits and not the deepest QG level
{
minQP = baseQP;
maxQP = baseQP;
}
else // deeper than QG
{
minQP = cs.currQP[partitioner.chType];
maxQP = cs.currQP[partitioner.chType];
}

Karsten Suehring
committed
}
int EncModeCtrl::xComputeDQP( const CodingStructure &cs, const Partitioner &partitioner )
{
Picture* picture = cs.picture;
unsigned uiAQDepth = std::min( partitioner.currSubdiv/2, ( uint32_t ) picture->aqlayer.size() - 1 );

Karsten Suehring
committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
AQpLayer* pcAQLayer = picture->aqlayer[uiAQDepth];
double dMaxQScale = pow( 2.0, m_pcEncCfg->getQPAdaptationRange() / 6.0 );
double dAvgAct = pcAQLayer->getAvgActivity();
double dCUAct = pcAQLayer->getActivity( cs.area.Y().topLeft() );
double dNormAct = ( dMaxQScale*dCUAct + dAvgAct ) / ( dCUAct + dMaxQScale*dAvgAct );
double dQpOffset = log( dNormAct ) / log( 2.0 ) * 6.0;
int iQpOffset = int( floor( dQpOffset + 0.49999 ) );
return iQpOffset;
}
#if SHARP_LUMA_DELTA_QP
void EncModeCtrl::initLumaDeltaQpLUT()
{
const LumaLevelToDeltaQPMapping &mapping = m_pcEncCfg->getLumaLevelToDeltaQPMapping();
if( !mapping.isEnabled() )
{
return;
}
// map the sparse LumaLevelToDeltaQPMapping.mapping to a fully populated linear table.
int lastDeltaQPValue = 0;
std::size_t nextSparseIndex = 0;
for( int index = 0; index < LUMA_LEVEL_TO_DQP_LUT_MAXSIZE; index++ )
{
while( nextSparseIndex < mapping.mapping.size() && index >= mapping.mapping[nextSparseIndex].first )
Loading
Loading full blame...