/work/vvenc/source/Lib/CommonLib/QuantRDOQ2.cpp
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | The copyright in this software is being made available under the Clear BSD |
3 | | License, included below. No patent rights, trademark rights and/or |
4 | | other Intellectual Property Rights other than the copyrights concerning |
5 | | the Software are granted under this license. |
6 | | |
7 | | The Clear BSD License |
8 | | |
9 | | Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors. |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without modification, |
13 | | are permitted (subject to the limitations in the disclaimer below) provided that |
14 | | the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the copyright holder nor the names of its |
24 | | contributors may be used to endorse or promote products derived from this |
25 | | software without specific prior written permission. |
26 | | |
27 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
28 | | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
29 | | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
31 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
32 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
33 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
34 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
35 | | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
36 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | |
41 | | ------------------------------------------------------------------------------------------- */ |
42 | | |
43 | | |
44 | | /** \file QuantRDOQ2.cpp |
45 | | \brief transform and quantization class |
46 | | */ |
47 | | |
48 | | #include "QuantRDOQ2.h" |
49 | | #include "UnitTools.h" |
50 | | #include "ContextModelling.h" |
51 | | #include "CodingStructure.h" |
52 | | #include "dtrace_next.h" |
53 | | #include "dtrace_buffer.h" |
54 | | |
55 | | #include <stdlib.h> |
56 | | #include <memory.h> |
57 | | |
58 | | #if defined( TARGET_SIMD_X86 ) |
59 | | # include "CommonDefX86.h" |
60 | | # include <simde/x86/sse4.1.h> |
61 | | #endif |
62 | | |
63 | | //! \ingroup CommonLib |
64 | | //! \{ |
65 | | |
66 | | namespace vvenc { |
67 | | |
68 | | struct coeffGroupRDStats |
69 | | { |
70 | | int iNNZbeforePos0; |
71 | | double d64CodedLevelandDist; // distortion and level cost only |
72 | | double d64UncodedDist; // all zero coded block distortion |
73 | | double d64SigCost; |
74 | | double d64SigCost_0; |
75 | | }; |
76 | | |
77 | | |
78 | | //! \ingroup CommonLib |
79 | | //! \{ |
80 | | |
81 | | // ==================================================================================================================== |
82 | | // Constants |
83 | | // ==================================================================================================================== |
84 | 10.1M | #define COEFF_ERR_SCALE_PRECISION_BITS 20 |
85 | | |
86 | | //! \} |
87 | | |
88 | 5.72k | QuantRDOQ2::QuantRDOQ2( const Quant* other, bool useScalingLists, bool enableOpt ) : QuantRDOQ( other, useScalingLists, enableOpt ), m_isErrScaleListOwner( false ), m_iLambda( 0 ), m_lastBitsBuilt{} |
89 | 5.72k | { |
90 | 5.72k | const QuantRDOQ2 *rdoq2 = dynamic_cast<const QuantRDOQ2*>( other ); |
91 | 5.72k | CHECK( other && !rdoq2, "The RDOQ cast must be successfull!" ); |
92 | 5.72k | xInitScalingList( rdoq2 ); |
93 | 5.72k | } |
94 | | |
95 | | QuantRDOQ2::~QuantRDOQ2() |
96 | 5.72k | { |
97 | 5.72k | xDestroyScalingList(); |
98 | 5.72k | } |
99 | | |
100 | | |
101 | | /** initialization process of scaling list array |
102 | | */ |
103 | | void QuantRDOQ2::xInitScalingList( const QuantRDOQ2* other ) |
104 | 5.72k | { |
105 | 5.72k | m_isErrScaleListOwner = other == nullptr; |
106 | | |
107 | 5.72k | const bool useScalingLists = getScalingListEnabled(); |
108 | | |
109 | 45.8k | for(uint32_t sizeIdX = 0; sizeIdX < SCALING_LIST_SIZE_NUM; sizeIdX++) |
110 | 40.0k | { |
111 | 320k | for(uint32_t sizeIdY = 0; sizeIdY < SCALING_LIST_SIZE_NUM; sizeIdY++) |
112 | 280k | { |
113 | 1.96M | for(uint32_t qp = 0; qp < SCALING_LIST_REM_NUM; qp++) |
114 | 1.68M | { |
115 | 11.7M | for(uint32_t listId = 0; listId < SCALING_LIST_NUM; listId++) |
116 | 10.1M | { |
117 | 10.1M | if( m_isErrScaleListOwner ) |
118 | 2.52M | { |
119 | 2.52M | m_errScale[sizeIdX][sizeIdY][listId][qp] = useScalingLists ? new int[g_scalingListSizeX[sizeIdX] * g_scalingListSizeX[sizeIdY]] : nullptr; |
120 | 2.52M | } |
121 | 7.57M | else |
122 | 7.57M | { |
123 | 7.57M | m_errScale[sizeIdX][sizeIdY][listId][qp] = other->m_errScale[sizeIdX][sizeIdY][listId][qp]; |
124 | 7.57M | } |
125 | 10.1M | } // listID loop |
126 | 1.68M | } |
127 | 280k | } |
128 | 40.0k | } |
129 | 5.72k | } |
130 | | |
131 | | /** destroy quantization matrix array |
132 | | */ |
133 | | void QuantRDOQ2::xDestroyScalingList() |
134 | 5.72k | { |
135 | 5.72k | if( !m_isErrScaleListOwner ) return; |
136 | | |
137 | 11.4k | for(uint32_t sizeIdX = 0; sizeIdX < SCALING_LIST_SIZE_NUM; sizeIdX++) |
138 | 10.0k | { |
139 | 80.1k | for(uint32_t sizeIdY = 0; sizeIdY < SCALING_LIST_SIZE_NUM; sizeIdY++) |
140 | 70.1k | { |
141 | 491k | for(uint32_t listId = 0; listId < SCALING_LIST_NUM; listId++) |
142 | 421k | { |
143 | 2.94M | for(uint32_t qp = 0; qp < SCALING_LIST_REM_NUM; qp++) |
144 | 2.52M | { |
145 | 2.52M | if(m_errScale[sizeIdX][sizeIdY][listId][qp]) |
146 | 0 | { |
147 | 0 | delete [] m_errScale[sizeIdX][sizeIdY][listId][qp]; |
148 | 0 | } |
149 | 2.52M | } |
150 | 421k | } |
151 | 70.1k | } |
152 | 10.0k | } |
153 | | // Quant::destroyScalingList(); |
154 | 1.43k | } |
155 | | |
156 | | int QuantRDOQ2::xGetErrScaleCoeff( const bool needsSqrt2, SizeType width, SizeType height, int qp, const int maxLog2TrDynamicRange, const int channelBitDepth ) |
157 | 0 | { |
158 | 0 | const int iTransformShift = getTransformShift(channelBitDepth, Size(width, height), maxLog2TrDynamicRange); |
159 | 0 | double dErrScale = (double)(1 << SCALE_BITS); // Compensate for scaling of bitcount in Lagrange cost function |
160 | 0 | double dTransShift = (double)iTransformShift + (needsSqrt2 ? -0.5 : 0.0); |
161 | 0 | dErrScale = dErrScale * pow(2.0, (-2.0*dTransShift)); // Compensate for scaling through forward transform |
162 | 0 | const int QStep = g_quantScales[needsSqrt2 ? 1 : 0][qp]; |
163 | 0 | double finalErrScale = dErrScale / QStep / QStep / (1 << (DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth) << 1)); |
164 | 0 | return finalErrScale; |
165 | 0 | } |
166 | | |
167 | | /** set error scale coefficients |
168 | | * \param list list ID |
169 | | * \param size |
170 | | * \param qp quantization parameter |
171 | | * \param maxLog2TrDynamicRange |
172 | | * \param bitDepths reference to bit depth array for all channels |
173 | | */ |
174 | | void QuantRDOQ2::xSetErrScaleCoeff( unsigned list, unsigned sizeX, unsigned sizeY, int qp, const int maxLog2TrDynamicRange[MAX_NUM_CH], const BitDepths &bitDepths ) |
175 | 10.1M | { |
176 | 10.1M | const int width = g_scalingListSizeX[sizeX]; |
177 | 10.1M | const int height = g_scalingListSizeX[sizeY]; |
178 | 10.1M | const ChannelType channelType = ((list == 0) || (list == MAX_NUM_COMP)) ? CH_L : CH_C; |
179 | 10.1M | const int channelBitDepth = bitDepths.recon[channelType]; |
180 | 10.1M | const int iTransformShift = getTransformShift( channelBitDepth, Size( width, height ), maxLog2TrDynamicRange[channelType] ); |
181 | 10.1M | const double dTransShift = (double)iTransformShift; |
182 | | |
183 | 10.1M | double dErrScale = pow( 2.0, ( (double)SCALE_BITS / 2.0 ) ); // Compensate for scaling of bitcount in Lagrange cost function |
184 | 10.1M | dErrScale = dErrScale*pow( 2.0, ( -/*2.0**/( dTransShift ) ) ); // Compensate for scaling through forward transform |
185 | | |
186 | 10.1M | if( getScalingListEnabled() ) |
187 | 0 | { |
188 | 0 | const unsigned uiMaxNumCoeff = g_scalingListSizeX[sizeX] * g_scalingListSizeX[sizeY]; |
189 | 0 | const int *piQuantCoeff = getQuantCoeff( list, qp, sizeX, sizeY ); |
190 | 0 | int *piErrScale = xGetErrScaleCoeffSL( list, sizeX, sizeY, qp ); |
191 | |
|
192 | 0 | for( unsigned i = 0; i < uiMaxNumCoeff; i++ ) |
193 | 0 | { |
194 | 0 | int QStep = piQuantCoeff[i]; |
195 | 0 | double errScale = dErrScale / QStep / (1 << (DISTORTION_PRECISION_ADJUSTMENT( channelBitDepth ) /*<< 1*/)); // (1 << ( /*2 **/ (bitDepths.recon[channelType] - 8))); |
196 | 0 | piErrScale[i] = ( int ) (errScale * ( double ) (1 << COEFF_ERR_SCALE_PRECISION_BITS)); |
197 | 0 | } |
198 | 0 | } |
199 | | |
200 | 10.1M | xSetErrScaleCoeffNoScalingList( list, sizeX, sizeY, qp, maxLog2TrDynamicRange, bitDepths ); |
201 | 10.1M | } |
202 | | |
203 | | void QuantRDOQ2::xSetErrScaleCoeffNoScalingList( unsigned list, unsigned wIdx, unsigned hIdx, int qp, const int maxLog2TrDynamicRange[MAX_NUM_CH], const BitDepths &bitDepths ) |
204 | 10.1M | { |
205 | 10.1M | const int width = g_scalingListSizeX[wIdx]; |
206 | 10.1M | const int height = g_scalingListSizeX[hIdx]; |
207 | 10.1M | const ChannelType channelType = ( ( list == 0 ) || ( list == MAX_NUM_COMP ) ) ? CH_L : CH_C; |
208 | 10.1M | const int channelBitDepth = bitDepths.recon[channelType]; |
209 | 10.1M | const int iTransformShift = getTransformShift( channelBitDepth, Size( width, height ), maxLog2TrDynamicRange[channelType] ); |
210 | 10.1M | const bool needsSqrt2 = ((Log2(width*height)) & 1) == 1; |
211 | 10.1M | double dTransShift = (double)iTransformShift + ( needsSqrt2 ? -0.5 : 0.0 ); |
212 | | |
213 | 10.1M | double dErrScale = pow( 2.0, ( (double)SCALE_BITS / 2.0 ) ); // Compensate for scaling of bitcount in Lagrange cost function |
214 | 10.1M | dErrScale = dErrScale*pow( 2.0, ( -( dTransShift ) ) ); // Compensate for scaling through forward transform |
215 | 10.1M | int QStep = g_quantScales[needsSqrt2][qp]; |
216 | | |
217 | 10.1M | double errScale = dErrScale / QStep /*/ QStep*/ / (1 << (DISTORTION_PRECISION_ADJUSTMENT( channelBitDepth ) /*<< 1*/)); |
218 | 10.1M | xGetErrScaleCoeffNoScalingList( list, wIdx, hIdx, qp ) = (int)( errScale * (double)( 1 << COEFF_ERR_SCALE_PRECISION_BITS ) ); |
219 | 10.1M | } |
220 | | |
221 | | |
222 | | /** set flat matrix value to quantized coefficient |
223 | | */ |
224 | | void QuantRDOQ2::setFlatScalingList(const int maxLog2TrDynamicRange[MAX_NUM_CH], const BitDepths &bitDepths) |
225 | 5.72k | { |
226 | 5.72k | QuantRDOQ::setFlatScalingList( maxLog2TrDynamicRange, bitDepths ); |
227 | | |
228 | 5.72k | const int minimumQp = 0; |
229 | 5.72k | const int maximumQp = SCALING_LIST_REM_NUM; |
230 | | |
231 | 45.8k | for(uint32_t sizeX = 0; sizeX < SCALING_LIST_SIZE_NUM; sizeX++) |
232 | 40.0k | { |
233 | 320k | for(uint32_t sizeY = 0; sizeY < SCALING_LIST_SIZE_NUM; sizeY++) |
234 | 280k | { |
235 | 1.96M | for(uint32_t list = 0; list < SCALING_LIST_NUM; list++) |
236 | 1.68M | { |
237 | 11.7M | for(int qp = minimumQp; qp < maximumQp; qp++) |
238 | 10.1M | { |
239 | 10.1M | xSetErrScaleCoeff( list, sizeX, sizeY, qp, maxLog2TrDynamicRange, bitDepths ); |
240 | 10.1M | } |
241 | 1.68M | } |
242 | 280k | } |
243 | 40.0k | } |
244 | 5.72k | } |
245 | | |
246 | | |
247 | | void QuantRDOQ2::quant( TransformUnit &tu, const ComponentID compID, const CCoeffBuf &pSrc, TCoeff &uiAbsSum, const QpParam &cQP, const Ctx& ctx ) |
248 | 120k | { |
249 | 120k | if( m_RDOQ == 1 ) |
250 | 120k | { |
251 | 120k | QuantRDOQ::quant( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
252 | 120k | return; |
253 | 120k | } |
254 | | |
255 | 0 | const CompArea& rect = tu.blocks[compID]; |
256 | 0 | const uint32_t uiWidth = rect.width; |
257 | 0 | const uint32_t uiHeight = rect.height; |
258 | |
|
259 | 0 | const CCoeffBuf& piCoef = pSrc; |
260 | |
|
261 | 0 | const bool useTransformSkip = tu.mtsIdx[compID]==MTS_SKIP; |
262 | |
|
263 | 0 | bool useRDOQ = useTransformSkip ? m_useRDOQTS : m_RDOQ > 0; |
264 | |
|
265 | 0 | if( !tu.cu->ispMode || !isLuma(compID) ) |
266 | 0 | { |
267 | 0 | useRDOQ &= uiWidth > 2; |
268 | 0 | useRDOQ &= uiHeight > 2; |
269 | 0 | } |
270 | |
|
271 | 0 | if( useRDOQ ) |
272 | 0 | { |
273 | 0 | if( !tu.cs->picture->useSelectiveRdoq || xNeedRDOQ( tu, compID, piCoef, cQP ) ) |
274 | 0 | { |
275 | 0 | if( useTransformSkip ) |
276 | 0 | { |
277 | 0 | if( tu.cu->bdpcmM[toChannelType( compID )] ) |
278 | 0 | { |
279 | 0 | forwardRDPCM( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
280 | 0 | } |
281 | 0 | else |
282 | 0 | { |
283 | 0 | rateDistOptQuantTS( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
284 | 0 | } |
285 | 0 | } |
286 | 0 | else |
287 | 0 | { |
288 | 0 | xRateDistOptQuant( tu, compID, pSrc, uiAbsSum, cQP, ctx, getScalingListEnabled() ); |
289 | 0 | } |
290 | 0 | } |
291 | 0 | else |
292 | 0 | { |
293 | 0 | uiAbsSum = 0; |
294 | 0 | tu.lastPos[compID] = -1; |
295 | 0 | } |
296 | 0 | } |
297 | 0 | else |
298 | 0 | { |
299 | 0 | Quant::quant( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
300 | 0 | } |
301 | 0 | } |
302 | | |
303 | | inline cost_t QuantRDOQ2::xiGetICost(int iRate ) const |
304 | 0 | { |
305 | | // lambda as an integer in 1 / 2^LAMBDA_FRAC_BITS (m_iLambda, set per call): no conversions to and from double per rate |
306 | 0 | return ( cost_t( iRate ) * m_iLambda ) >> LAMBDA_FRAC_BITS; |
307 | 0 | } |
308 | | |
309 | | |
310 | | inline cost_t QuantRDOQ2::xGetIEPRate() const |
311 | 0 | { |
312 | 0 | return 1 << SCALE_BITS; |
313 | 0 | } |
314 | | |
315 | | // The rate cost of a level (sign bit included) as the residual coder spends its bins (regular while remRegBins >= 4, else |
316 | | // bypass with the Rice code of goRiceParam), the context bits read from the context states: levels 1..3 |
317 | | // with regular bins left inline, the rest (levels >= 4, bypass coding) out of line (keeps the inlined copies small) |
318 | | inline cost_t QuantRDOQ2::xRateCostLevel( const FracBitsAccess& fracBits, const CtxSet& parSet, const CtxSet& gt1Set, const CtxSet& gt2Set, const int ctxOffset, |
319 | | const uint32_t absLevel, const int remRegBins, const unsigned goRiceZero, const uint16_t goRiceParam ) const |
320 | 0 | { |
321 | 0 | if( remRegBins >= 4 && absLevel < 4 ) |
322 | 0 | { |
323 | 0 | if( absLevel == 0 ) |
324 | 0 | { |
325 | 0 | return 0; |
326 | 0 | } |
327 | 0 | const BinFracBits gt1 = fracBits.getFracBitsArray( gt1Set( ctxOffset ) ); |
328 | 0 | if( absLevel == 1 ) |
329 | 0 | { |
330 | 0 | return xiGetICost( int( xGetIEPRate() + gt1.intBits[0] ) ); |
331 | 0 | } |
332 | 0 | const BinFracBits par = fracBits.getFracBitsArray( parSet( ctxOffset ) ); |
333 | 0 | const BinFracBits gt2 = fracBits.getFracBitsArray( gt2Set( ctxOffset ) ); |
334 | 0 | return xiGetICost( int( xGetIEPRate() + gt1.intBits[1] + par.intBits[absLevel - 2] + gt2.intBits[0] ) ); |
335 | 0 | } |
336 | 0 | return xRateCostLevelRare( fracBits, parSet, gt1Set, gt2Set, ctxOffset, absLevel, remRegBins, goRiceZero, goRiceParam ); |
337 | 0 | } |
338 | | |
339 | | cost_t QuantRDOQ2::xRateCostLevelRare( const FracBitsAccess& fracBits, const CtxSet& parSet, const CtxSet& gt1Set, const CtxSet& gt2Set, const int ctxOffset, |
340 | | const uint32_t absLevel, const int remRegBins, const unsigned goRiceZero, const uint16_t goRiceParam ) const |
341 | 0 | { |
342 | 0 | const int threshold = COEF_REMAIN_BIN_REDUCTION; |
343 | 0 | auto riceBits = [&]( uint32_t symbol ) -> cost_t |
344 | 0 | { |
345 | 0 | if( symbol < ( uint32_t( threshold ) << goRiceParam ) ) |
346 | 0 | { |
347 | 0 | return cost_t( ( symbol >> goRiceParam ) + 1 + goRiceParam ) << SCALE_BITS; |
348 | 0 | } |
349 | 0 | uint32_t length = goRiceParam; |
350 | 0 | symbol -= threshold << goRiceParam; |
351 | 0 | while( symbol >= ( 1u << length ) ) |
352 | 0 | { |
353 | 0 | symbol -= ( 1u << ( length++ ) ); |
354 | 0 | } |
355 | 0 | return cost_t( threshold + length + 1 - goRiceParam + length ) << SCALE_BITS; |
356 | 0 | }; |
357 | 0 | if( remRegBins >= 4 ) |
358 | 0 | { |
359 | | // levels >= 4: the context bins of a level >= 4 plus the Rice code of ( level - 4 ) >> 1 |
360 | 0 | const BinFracBits gt1 = fracBits.getFracBitsArray( gt1Set( ctxOffset ) ); |
361 | 0 | const BinFracBits par = fracBits.getFracBitsArray( parSet( ctxOffset ) ); |
362 | 0 | const BinFracBits gt2 = fracBits.getFracBitsArray( gt2Set( ctxOffset ) ); |
363 | 0 | return xiGetICost( int( xGetIEPRate() + riceBits( ( absLevel - 4 ) >> 1 ) + gt1.intBits[1] + par.intBits[( absLevel - 2 ) & 1] + gt2.intBits[1] ) ); |
364 | 0 | } |
365 | 0 | const uint32_t symbol = absLevel == 0 ? goRiceZero : absLevel <= goRiceZero ? absLevel - 1 : absLevel; |
366 | 0 | return xiGetICost( int( xGetIEPRate() + riceBits( symbol ) ) ); |
367 | 0 | } |
368 | | |
369 | | void QuantRDOQ2::xInitLastPosBitsTab( const CoeffCodingContext& cctx, const uint32_t uiWidth, const uint32_t uiHeight, const ChannelType chType, const FracBitsAccess& fracBits ) |
370 | 0 | { |
371 | 0 | int dim1 = std::min<int>(JVET_C0024_ZERO_OUT_TH, uiWidth); |
372 | 0 | int dim2 = std::min<int>(JVET_C0024_ZERO_OUT_TH, uiHeight); |
373 | |
|
374 | 0 | int bitsX = 0; |
375 | 0 | int bitsY = 0; |
376 | 0 | int ctxId; |
377 | | |
378 | | //X-coordinate |
379 | 0 | for( ctxId = 0; ctxId < g_uiGroupIdx[dim1 - 1]; ctxId++ ) |
380 | 0 | { |
381 | 0 | const BinFracBits fB = fracBits.getFracBitsArray( cctx.lastXCtxId( ctxId ) ); |
382 | 0 | m_lastBitsX[chType][ctxId] = bitsX + fB.intBits[0]; |
383 | 0 | bitsX += fB.intBits[1]; |
384 | 0 | } |
385 | 0 | m_lastBitsX[chType][ctxId] = bitsX; |
386 | | |
387 | | //Y-coordinate |
388 | 0 | for( ctxId = 0; ctxId < g_uiGroupIdx[dim2 - 1]; ctxId++ ) |
389 | 0 | { |
390 | 0 | const BinFracBits fB = fracBits.getFracBitsArray( cctx.lastYCtxId( ctxId ) ); |
391 | 0 | m_lastBitsY[chType][ctxId] = bitsY + fB.intBits[0]; |
392 | 0 | bitsY += fB.intBits[1]; |
393 | 0 | } |
394 | 0 | m_lastBitsY[chType][ctxId] = bitsY; |
395 | 0 | } |
396 | | |
397 | | |
398 | | /** Calculates the cost of signaling the last significant coefficient in the block |
399 | | * \param uiPosX X coordinate of the last significant coefficient |
400 | | * \param uiPosY Y coordinate of the last significant coefficient |
401 | | * \returns cost of last significant coefficient |
402 | | */ |
403 | | /* |
404 | | * \param uiWidth width of the transform unit (TU) |
405 | | */ |
406 | | inline cost_t QuantRDOQ2::xiGetCostLast( const uint32_t uiPosX, const uint32_t uiPosY, const ChannelType chType ) const |
407 | 0 | { |
408 | 0 | uint32_t uiCtxX = g_uiGroupIdx[uiPosX]; |
409 | 0 | uint32_t uiCtxY = g_uiGroupIdx[uiPosY]; |
410 | |
|
411 | 0 | uint32_t uiCost = m_lastBitsX[chType][uiCtxX] + m_lastBitsY[chType][uiCtxY]; |
412 | |
|
413 | 0 | if( uiCtxX > 3 ) |
414 | 0 | { |
415 | 0 | uiCost += xGetIEPRate() * ( ( uiCtxX - 2 ) >> 1 ); |
416 | 0 | } |
417 | 0 | if( uiCtxY > 3 ) |
418 | 0 | { |
419 | 0 | uiCost += xGetIEPRate() * ( ( uiCtxY - 2 ) >> 1 ); |
420 | 0 | } |
421 | 0 | return xiGetICost( (int)uiCost ); |
422 | 0 | } |
423 | | |
424 | | static inline cost_t _dist( cost_t iErr, cost_t iErrScale, int64_t iErrScaleShift ) |
425 | 0 | { |
426 | 0 | int64_t iSqrtErrCost = ( iErr*iErrScale ) >> iErrScaleShift; |
427 | 0 | int64_t iDist = iSqrtErrCost*iSqrtErrCost; |
428 | 0 | return iDist; |
429 | 0 | } |
430 | | |
431 | | |
432 | | template< bool bSBH, bool bUseScalingList > |
433 | | int QuantRDOQ2::xRateDistOptQuantFast( TransformUnit &tu, const ComponentID &compID, const CCoeffBuf &pSrc, TCoeff &uiAbsSum, const QpParam &cQP, const Ctx &ctx ) |
434 | 0 | { |
435 | | // the template buffer is cleared once the last position is known (clearTemplates): blocks that quantize to zero skip it. |
436 | | // The constructor would not clear it for transform skip with TS residual coding; quant() sends those to rateDistOptQuantTS. |
437 | 0 | CHECKD( tu.mtsIdx[compID] == MTS_SKIP && !tu.cu->slice->tsResidualCodingDisabled, "RDOQ2's regular path with TS residual coding" ); |
438 | 0 | CoeffCodingContext cctx( tu, compID, bSBH, false, m_tplBuf, CoeffCodingContext::DeferTplClear ); |
439 | 0 | const FracBitsAccess& fracBits = ctx.getFracBitsAcess(); |
440 | |
|
441 | 0 | const SPS &sps = *tu.cs->sps; |
442 | 0 | const CompArea &rect = tu.blocks[compID]; |
443 | 0 | const uint32_t uiWidth = rect.width; |
444 | 0 | const uint32_t uiHeight = rect.height; |
445 | 0 | const ChannelType chType = toChannelType( compID ); |
446 | 0 | const int channelBitDepth = sps.bitDepths[ chType ]; |
447 | |
|
448 | 0 | const int maxLog2TrDynamicRange = sps.getMaxLog2TrDynamicRange(); |
449 | | |
450 | | // The last position bits table is built when the first last position is priced, so blocks that quantise to zero skip it. |
451 | | // Cr reuses Cb's table (same contexts, same size) when Cb is coded and built the table in the latest call. |
452 | 0 | bool lastBitsBuilt = compID == COMP_Cr && tu.cbf[COMP_Cb] && m_lastBitsBuilt[chType]; |
453 | 0 | m_lastBitsBuilt[chType] = lastBitsBuilt; |
454 | | |
455 | | /* for 422 chroma blocks, the effective scaling applied during transformation is not a power of 2, hence it cannot be |
456 | | * implemented as a bit-shift (the quantised result will be sqrt(2) * larger than required). Alternatively, adjust the |
457 | | * uiLog2TrSize applied in iTransformShift, such that the result is 1/sqrt(2) the required result (i.e. smaller) |
458 | | * Then a QP+3 (sqrt(2)) or QP-3 (1/sqrt(2)) method could be used to get the required result |
459 | | */ |
460 | | |
461 | | // Represents scaling through forward transform |
462 | 0 | const int iTransformShift = getTransformShift(channelBitDepth, rect.size(), maxLog2TrDynamicRange); |
463 | |
|
464 | 0 | const uint32_t uiLog2BlockWidth = Log2(uiWidth); |
465 | 0 | const uint32_t uiLog2BlockHeight = Log2(uiHeight); |
466 | 0 | const uint32_t uiMaxNumCoeff = uiWidth * uiHeight; |
467 | 0 | const uint32_t log2CGSize = cctx.log2CGSize(); |
468 | |
|
469 | 0 | int scalingListType = getScalingListType( tu.cu->predMode, compID ); |
470 | 0 | CHECK(scalingListType >= SCALING_LIST_NUM, "Invalid scaling list"); |
471 | |
|
472 | 0 | const TCoeff *plSrcCoeff = pSrc.buf; |
473 | 0 | TCoeffSig *piDstCoeff = tu.getCoeffs( compID ).buf; |
474 | |
|
475 | 0 | memset( piDstCoeff, 0, sizeof( *piDstCoeff ) * uiMaxNumCoeff ); |
476 | |
|
477 | 0 | const bool needSqrtAdjustment = TU::needsSqrt2Scale( tu, compID ); |
478 | 0 | const bool isTransformSkip = tu.mtsIdx[compID] == MTS_SKIP; |
479 | 0 | const int *quantScaleList = getQuantCoeff( scalingListType, cQP.rem( isTransformSkip ), uiLog2BlockWidth, uiLog2BlockHeight ); |
480 | 0 | const int defaultQuantScale = g_quantScales[ needSqrtAdjustment ?1:0][cQP.rem( isTransformSkip )]; |
481 | 0 | const int defaultErrScale = xGetErrScaleCoeffNoScalingList( scalingListType, uiLog2BlockWidth, uiLog2BlockHeight, cQP.rem( isTransformSkip ) ); |
482 | 0 | const int *piErrScale = xGetErrScaleCoeffSL ( scalingListType, uiLog2BlockWidth, uiLog2BlockHeight, cQP.rem( isTransformSkip ) ); |
483 | 0 | const int iErrScaleShift = COEFF_ERR_SCALE_PRECISION_BITS; |
484 | 0 | int iQBits = QUANT_SHIFT + cQP.per( isTransformSkip ) + iTransformShift + (needSqrtAdjustment?-1:0); // Right shift of non-RDOQ quantizer; level = (coeff*uiQ + offset)>>q_bits |
485 | 0 | int iQOffset = 1 << ( iQBits - 1 ); |
486 | |
|
487 | 0 | cost_t piCostCoeff [16]; |
488 | 0 | cost_t piCostSig [16]; |
489 | 0 | cost_t piCostCoeff0 [16]; |
490 | 0 | cost_t piCostDeltaSBH[16]; |
491 | 0 | int piAddSBH [16]; |
492 | |
|
493 | 0 | cost_t iCodedCostBlock = 0; |
494 | 0 | cost_t iUncodedCostBlock = 0; |
495 | 0 | int iLastScanPos = -1; |
496 | 0 | int lastSubSetId = -1; |
497 | 0 | bool lastOptFinished = false; |
498 | 0 | cost_t bestTotalCost = std::numeric_limits<cost_t>::max() / 2; |
499 | |
|
500 | 0 | int ctxBinSampleRatio = MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT; |
501 | 0 | int remRegBins = ( tu.getTbAreaAfterCoefZeroOut( compID ) * ctxBinSampleRatio ) >> 4; |
502 | 0 | uint32_t goRiceParam = 0; |
503 | |
|
504 | | #if ENABLE_TRACING |
505 | | bool bFirstNZSeen = false; |
506 | | DTRACE( g_trace_ctx, D_RDOQ, "%d: %3d, %3d, %dx%d, comp=%d\n", DTRACE_GET_COUNTER( g_trace_ctx, D_RDOQ ), rect.x, rect.y, rect.width, rect.height, compID ); |
507 | | #endif |
508 | |
|
509 | 0 | uiAbsSum = 0; |
510 | |
|
511 | 0 | const int iCGSize = 1 << log2CGSize; |
512 | 0 | const int iCGSizeM1 = iCGSize - 1; |
513 | |
|
514 | 0 | const uint32_t lfnstIdx = tu.cu->lfnstIdx; |
515 | 0 | const int iCGNum = lfnstIdx > 0 ? 1 : std::min<int>(JVET_C0024_ZERO_OUT_TH, uiWidth) * std::min<int>(JVET_C0024_ZERO_OUT_TH, uiHeight) >> cctx.log2CGSize(); |
516 | 0 | int iScanPos = ( iCGNum << log2CGSize ) - 1; |
517 | |
|
518 | 0 | if( lfnstIdx > 0 && ( ( uiWidth == 4 && uiHeight == 4 ) || ( uiWidth == 8 && uiHeight == 8 ) ) ) |
519 | 0 | { |
520 | 0 | iScanPos = 7; |
521 | 0 | } |
522 | | |
523 | | // Find first non-zero coeff |
524 | 0 | for( ; iScanPos > 0; iScanPos-- ) |
525 | 0 | { |
526 | 0 | uint32_t uiBlkPos = cctx.blockPos( iScanPos ); |
527 | 0 | if( plSrcCoeff[uiBlkPos] ) |
528 | 0 | break; |
529 | 0 | } |
530 | | |
531 | | ////////////////////////////////////////////////////////////////////////// |
532 | | // Loop over sub-sets (coefficient groups) |
533 | | ////////////////////////////////////////////////////////////////////////// |
534 | | |
535 | 0 | TCoeff thres = 0, useThres = 0; |
536 | | |
537 | 0 | if( iQBits ) |
538 | 0 | thres = TCoeff( ( int64_t( m_thrVal ) << ( iQBits - 1 ) ) ); |
539 | 0 | else |
540 | 0 | thres = TCoeff( ( int64_t( m_thrVal >> 1 ) << iQBits ) ); |
541 | |
|
542 | 0 | if( !bUseScalingList ) |
543 | 0 | { |
544 | 0 | useThres = thres / ( defaultQuantScale << 2 ); |
545 | 0 | } |
546 | |
|
547 | 0 | const bool scanFirstBlk = !bUseScalingList && log2CGSize == 4 && cctx.log2CGWidth() == 2; |
548 | | |
549 | | // the rate costs: lambda in 1 / 2^LAMBDA_FRAC_BITS (xiGetICost); the products of the costs must fit, m_iLambda must not |
550 | | // round to 0 |
551 | 0 | m_iLambda = int64_t( m_dLambda * double( 1 << LAMBDA_FRAC_BITS ) + 0.5 ); |
552 | 0 | CHECKD( m_iLambda <= 0, "lambda too small for its integer form" ); |
553 | 0 | bool tplCleared = false; // the template buffer is cleared with the first last position found |
554 | 0 | const CtxSet parSet = Ctx::ParFlag[chType], gt1Set = Ctx::GtxFlag[chType + 2], gt2Set = Ctx::GtxFlag[chType]; // gt1 / gt2 as CoeffCodingContext |
555 | 0 | auto sigCostOf = [&]( unsigned ctxId, int bin ) -> cost_t { return xiGetICost( fracBits.getFracBitsArray( ctxId ).intBits[bin] ); };Unexecuted instantiation: vvenc::QuantRDOQ2::xRateDistOptQuantFast<true, true>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&)::{lambda(unsigned int, int)#1}::operator()(unsigned int, int) constUnexecuted instantiation: vvenc::QuantRDOQ2::xRateDistOptQuantFast<true, false>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&)::{lambda(unsigned int, int)#1}::operator()(unsigned int, int) constUnexecuted instantiation: vvenc::QuantRDOQ2::xRateDistOptQuantFast<false, true>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&)::{lambda(unsigned int, int)#1}::operator()(unsigned int, int) constUnexecuted instantiation: vvenc::QuantRDOQ2::xRateDistOptQuantFast<false, false>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&)::{lambda(unsigned int, int)#1}::operator()(unsigned int, int) const |
556 | 0 | auto levelCost = [&]( int off, uint32_t lvl, int rrb, unsigned grz, uint16_t rice ) -> cost_t { return xRateCostLevel( fracBits, parSet, gt1Set, gt2Set, off, lvl, rrb, grz, rice ); };Unexecuted instantiation: vvenc::QuantRDOQ2::xRateDistOptQuantFast<true, true>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&)::{lambda(int, unsigned int, int, unsigned int, unsigned short)#1}::operator()(int, unsigned int, int, unsigned int, unsigned short) constUnexecuted instantiation: vvenc::QuantRDOQ2::xRateDistOptQuantFast<true, false>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&)::{lambda(int, unsigned int, int, unsigned int, unsigned short)#1}::operator()(int, unsigned int, int, unsigned int, unsigned short) constUnexecuted instantiation: vvenc::QuantRDOQ2::xRateDistOptQuantFast<false, true>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&)::{lambda(int, unsigned int, int, unsigned int, unsigned short)#1}::operator()(int, unsigned int, int, unsigned int, unsigned short) constUnexecuted instantiation: vvenc::QuantRDOQ2::xRateDistOptQuantFast<false, false>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&)::{lambda(int, unsigned int, int, unsigned int, unsigned short)#1}::operator()(int, unsigned int, int, unsigned int, unsigned short) const |
557 | |
|
558 | 0 | int subSetId = iScanPos >> log2CGSize; |
559 | 0 | for( ; subSetId >= 0; subSetId-- ) |
560 | 0 | { |
561 | 0 | int iNZbeforePos0 = 0; |
562 | 0 | int uiAbsSumCG = 0; |
563 | 0 | cost_t iCodedCostCG = 0; |
564 | 0 | cost_t iUncodedCostCG = 0; |
565 | |
|
566 | 0 | int iScanPosinCG = iScanPos & ( iCGSize - 1 ); |
567 | 0 | if( iLastScanPos < 0 ) |
568 | 0 | { |
569 | | // Before the last position, the sub-blocks whose coefficients are all at most useThres (levels of at most 1, which |
570 | | // RDOQ would drop at the end of the scan) are skipped: continue in the next one holding a larger coefficient, |
571 | | // searched from the current one's end (its positions above iScanPos are 0). Sub-block 0 is always searched. |
572 | 0 | if( scanFirstBlk && iScanPos >= 16 ) |
573 | 0 | { |
574 | 0 | int firstPos = iScanPos | iCGSizeM1; |
575 | 0 | xFindFirstPos( firstPos, plSrcCoeff, cctx.scan(), uiWidth, 2, 2, useThres, false, 0, 0 ); |
576 | 0 | const int target = firstPos >= 16 ? firstPos >> log2CGSize : 0; |
577 | 0 | if( target != subSetId ) |
578 | 0 | { |
579 | 0 | subSetId = target; |
580 | 0 | iScanPos = ( target << log2CGSize ) + iCGSizeM1; |
581 | 0 | iScanPosinCG = iCGSizeM1; |
582 | 0 | } |
583 | 0 | } |
584 | |
|
585 | 0 | findlast2: |
586 | | // Fast loop to find last-pos. |
587 | | // No need to add distortion to cost as it would be added to both the coded and uncoded cost |
588 | 0 | for( ; iScanPosinCG >= 0; iScanPosinCG--, iScanPos-- ) |
589 | 0 | { |
590 | 0 | const uint32_t uiBlkPos = cctx.blockPos( iScanPos ); |
591 | | |
592 | | //===== quantization ===== |
593 | 0 | int quantScale; |
594 | 0 | if( bUseScalingList ){ quantScale = quantScaleList[uiBlkPos]; } |
595 | 0 | else{ quantScale = defaultQuantScale; } |
596 | | |
597 | 0 | const uint32_t uiMaxAbsLevel = ( std::abs( plSrcCoeff[uiBlkPos] ) * quantScale + iQOffset ) >> iQBits; |
598 | |
|
599 | 0 | if( uiMaxAbsLevel ) |
600 | 0 | { |
601 | 0 | iLastScanPos = iScanPos; |
602 | 0 | lastSubSetId = subSetId; |
603 | 0 | break; |
604 | 0 | } |
605 | | #if ENABLE_TRACING |
606 | | if( bFirstNZSeen ) |
607 | | { |
608 | | DTRACE( g_trace_ctx, D_RDOQ, "%d [%d][%d][%2d:%2d][%2d:%2d]", DTRACE_GET_COUNTER( g_trace_ctx, D_RDOQ ), iScanPos, cctx.blockPos( iScanPos ), cctx.cgPosX(), cctx.cgPosY(), cctx.posX( iScanPos ), cctx.posY( iScanPos ) ); |
609 | | DTRACE( g_trace_ctx, D_RDOQ, " remRegBins=%d \n", remRegBins ); |
610 | | DTRACE( g_trace_ctx, D_RDOQ, " Lev=%d \n", 0 ); |
611 | | } |
612 | | #endif |
613 | 0 | } |
614 | 0 | } |
615 | | |
616 | | ////////////////////////////////////////////////////////////////////////// |
617 | | // Loop over coefficients |
618 | | ////////////////////////////////////////////////////////////////////////// |
619 | | |
620 | | // A coefficient group after the last one's whose coefficients all lie below m_sbbSkipThrVal half quantization steps is |
621 | | // decided all zero (its coded cost is its uncoded cost plus the cost of sb_coded_flag 0): only the distortions of level |
622 | | // 0; the bin budget stays (a group coded with sb_coded_flag 0 spends no context bins), goRiceParam is reset as at the |
623 | | // group's first position. With the limit 1.0 (the default) these are exactly the groups whose levels all quantize to 0, |
624 | | // where the main loop would code 0 everywhere; a higher limit skips groups where it might code a 1, a lower one fewer |
625 | | // groups. Not for group 0, which has no flag, nor with scaling lists, nor with the limit 0. |
626 | 0 | if( !bUseScalingList && m_sbbSkipThrVal > 0 && iLastScanPos >= 0 && subSetId > 0 && subSetId != lastSubSetId && iScanPosinCG == iCGSizeM1 ) |
627 | 0 | { |
628 | | // groups of fewer positions (2x4, 4x2, 1xN up to 8) only occur in blocks of at most two groups, which never get here |
629 | | // (it needs a group after the last one's and before group 0); the cost arrays hold 16 positions |
630 | 0 | CHECKD( iCGSize != 16, "the zero-group path expects groups of 16 positions" ); |
631 | 0 | CHECKD( iQBits < 7 || iQBits > 30, "the zero-group limit assumes 7 <= iQBits <= 30" ); |
632 | | // a level is 0 iff |coeff| * quantScale + iQOffset < 2^iQBits, i.e. below one half step 2^( iQBits - 1 ) = 2^iQBits - |
633 | | // iQOffset: the limit counts in these half steps (the products fit 32 bits, as the main loop assumes) |
634 | 0 | CHECKD( iQOffset != 1 << ( iQBits - 1 ), "the zero-group limit assumes the rounding offset of one half step" ); |
635 | 0 | const int zeroLimit = sbbSkipLimit( m_sbbSkipThrVal, iQBits - 1 ); |
636 | 0 | const int firstPos = iScanPos - iCGSizeM1; // the group's first position in scan order |
637 | 0 | bool allZero = true; |
638 | 0 | if( scanFirstBlk ) |
639 | 0 | { |
640 | | // scanFirstBlk: the group is a 4x4 block in raster order with the block's width (>= 4) as stride |
641 | 0 | allZero = xAllZeroLevels4x4( plSrcCoeff + cctx.blockPos( firstPos ), uiWidth, defaultQuantScale, zeroLimit ); |
642 | 0 | } |
643 | 0 | else |
644 | 0 | { |
645 | | // from the low frequencies, where a non-zero level is likeliest |
646 | 0 | for( int k = 0; k < iCGSize && allZero; k++ ) |
647 | 0 | { |
648 | 0 | allZero = std::abs( plSrcCoeff[cctx.blockPos( firstPos + k )] ) * defaultQuantScale < zeroLimit; |
649 | 0 | } |
650 | 0 | } |
651 | 0 | if( allZero ) |
652 | 0 | { |
653 | 0 | cost_t uncodedCG = 0; |
654 | 0 | for( int k = 0; k < iCGSize; k++ ) |
655 | 0 | { |
656 | 0 | uncodedCG += _dist( std::abs( plSrcCoeff[cctx.blockPos( firstPos + k )] ) * defaultQuantScale, defaultErrScale, iErrScaleShift ); |
657 | 0 | } |
658 | 0 | cctx.initSubblockRegular( subSetId ); |
659 | 0 | goRiceParam = 0; |
660 | 0 | const cost_t codedCG = uncodedCG + sigCostOf( cctx.sigGroupCtxId(), 0 ); |
661 | 0 | bestTotalCost += codedCG; |
662 | 0 | iCodedCostBlock += codedCG; |
663 | 0 | iUncodedCostBlock += uncodedCG; |
664 | 0 | iScanPos -= iCGSize; |
665 | 0 | continue; |
666 | 0 | } |
667 | 0 | } |
668 | | |
669 | 0 | cctx.initSubblockRegular( subSetId ); |
670 | 0 | if( !tplCleared && iLastScanPos >= 0 ) |
671 | 0 | { |
672 | 0 | cctx.clearTemplates( iLastScanPos ); // the first last position found is the highest |
673 | 0 | tplCleared = true; |
674 | 0 | } |
675 | |
|
676 | 0 | const int remRegBinsStartCG = remRegBins; |
677 | |
|
678 | 0 | for( ; iScanPosinCG >= 0; iScanPosinCG--, iScanPos-- ) |
679 | 0 | { |
680 | 0 | const uint32_t uiBlkPos = cctx.blockPos( iScanPos ); |
681 | 0 | int quantScale; |
682 | 0 | int iErrScale; |
683 | | //===== quantization ===== |
684 | 0 | if( bUseScalingList ){ |
685 | 0 | quantScale = quantScaleList[uiBlkPos]; |
686 | 0 | iErrScale = piErrScale[uiBlkPos]; |
687 | 0 | } |
688 | 0 | else{ |
689 | 0 | quantScale = defaultQuantScale; |
690 | 0 | iErrScale = defaultErrScale; |
691 | 0 | } |
692 | 0 | const int iScaledLevel = std::abs( plSrcCoeff[uiBlkPos] ) * quantScale; |
693 | 0 | const int iAbsLevel = ( iScaledLevel + iQOffset ) >> iQBits; |
694 | | |
695 | | //============ Set context models =============== |
696 | | // the last position has no sig flag and gtx context offset 0 (as the residual coder: no template before it) |
697 | 0 | unsigned ctxIdSig = 0; |
698 | 0 | int ctxOffset = 0; |
699 | 0 | if( iScanPos != iLastScanPos ) |
700 | 0 | { |
701 | 0 | ctxIdSig = cctx.sigCtxIdAndCtxOffset( iScanPos, ctxOffset ); |
702 | 0 | } |
703 | 0 | uint32_t goRiceZero = 0; |
704 | |
|
705 | 0 | if( remRegBins < 4 ) |
706 | 0 | { |
707 | 0 | unsigned sumAbs = cctx.templateAbsSum( iScanPos, piDstCoeff, 0 ); |
708 | 0 | goRiceParam = g_auiGoRiceParsCoeff [ sumAbs ]; |
709 | 0 | goRiceZero = g_auiGoRicePosCoeff0(0, goRiceParam); |
710 | 0 | } |
711 | |
|
712 | | #if ENABLE_TRACING |
713 | | DTRACE( g_trace_ctx, D_RDOQ, "%d [%d][%d][%2d:%2d][%2d:%2d]", DTRACE_GET_COUNTER( g_trace_ctx, D_RDOQ ), iScanPos, cctx.blockPos( iScanPos ), cctx.cgPosX(), cctx.cgPosY(), cctx.posX( iScanPos ), cctx.posY( iScanPos ) ); |
714 | | DTRACE( g_trace_ctx, D_RDOQ, " remRegBins=%d \n", remRegBins ); |
715 | | bFirstNZSeen = true; |
716 | | #endif |
717 | | |
718 | | // Cost for zero coeff |
719 | 0 | piCostCoeff0[iScanPosinCG] = _dist( iScaledLevel, iErrScale, iErrScaleShift ); |
720 | |
|
721 | 0 | uint32_t uiLevel = 0; |
722 | 0 | if( iAbsLevel == 0 ) |
723 | 0 | { |
724 | | // ----------------- ABS LEVEL 0 ---------------- |
725 | 0 | const cost_t sigCost[2] = { sigCostOf( ctxIdSig, 0 ), sigCostOf( ctxIdSig, 1 ) }; |
726 | 0 | piCostSig [iScanPosinCG] = sigCost[0]; |
727 | 0 | piCostCoeff[iScanPosinCG] = piCostCoeff0[iScanPosinCG] + piCostSig[iScanPosinCG]; |
728 | |
|
729 | 0 | if( bSBH ) |
730 | 0 | { |
731 | 0 | cost_t iErr1 = iScaledLevel - ( (int64_t)1 << iQBits ); |
732 | 0 | cost_t iDist1 = _dist( iErr1, iErrScale, iErrScaleShift ); |
733 | | // the cost of level 1 instead of 0 (with remRegBins >= 4 the cost of level 0 is 0); in the cost domain, sign bit included |
734 | 0 | cost_t iRate1 = levelCost( ctxOffset, 1, remRegBins, goRiceZero, goRiceParam ) - |
735 | 0 | ( remRegBins < 4 ? levelCost( ctxOffset, 0, remRegBins, goRiceZero, goRiceParam ) : 0 ); |
736 | |
|
737 | 0 | cost_t iCost1 = iDist1 + iRate1 + sigCost[1]; |
738 | |
|
739 | 0 | piCostDeltaSBH[iScanPosinCG] = iCost1 - piCostCoeff[iScanPosinCG]; |
740 | 0 | piAddSBH [iScanPosinCG] = 1; |
741 | 0 | } |
742 | 0 | DTRACE( g_trace_ctx, D_RDOQ, " Lev=%d \n", 0 ); |
743 | 0 | } |
744 | 0 | else |
745 | 0 | { |
746 | | //===== coefficient level estimation ===== |
747 | 0 | const int iFloor = (int)( iScaledLevel >> iQBits ); |
748 | 0 | const int iCeil = iFloor + 1; |
749 | |
|
750 | 0 | if( remRegBins >= 4 && iScanPos != iLastScanPos && iCeil >= 4 ) |
751 | 0 | { |
752 | 0 | int sumAll = cctx.templateAbsSum( iScanPos, piDstCoeff, 4 ); |
753 | 0 | goRiceParam = g_auiGoRiceParsCoeff[ sumAll ]; |
754 | 0 | } |
755 | |
|
756 | 0 | if( iScanPos == iLastScanPos ) |
757 | 0 | { |
758 | | // ======================= ======================= |
759 | | // ======================= LAST LEVEL ======================= |
760 | | // ======================= ======================= |
761 | 0 | piCostSig[ iScanPosinCG ] = 0; |
762 | | // Floor = 0, Uncoded |
763 | 0 | cost_t iCurrCostF = piCostCoeff0[ iScanPosinCG ]; |
764 | |
|
765 | 0 | if( iFloor ) |
766 | 0 | { |
767 | | // ----------------- LEVEL > 0 ---------------- |
768 | 0 | cost_t iErrF = iScaledLevel - (iFloor << iQBits); |
769 | 0 | cost_t iDistF = _dist( iErrF, iErrScale, iErrScaleShift ); //(iErrF*iErrScale) >> iErrScaleShift; |
770 | 0 | iCurrCostF = iDistF + levelCost( ctxOffset, iFloor, remRegBins, goRiceZero, goRiceParam ); |
771 | 0 | } |
772 | | |
773 | | // ----------------- LEVEL + 1 ---------------- |
774 | 0 | cost_t iErrC = iScaledLevel - (iCeil << iQBits); |
775 | 0 | cost_t iDistC = _dist( iErrC, iErrScale, iErrScaleShift ); //(iErrC*iErrScale) >> iErrScaleShift; |
776 | 0 | cost_t iCurrCostC = iDistC + levelCost( ctxOffset, iCeil, remRegBins, goRiceZero, goRiceParam ); |
777 | |
|
778 | 0 | if( iCurrCostC < iCurrCostF ) |
779 | 0 | { |
780 | 0 | uiLevel = iCeil; |
781 | 0 | piCostCoeff[iScanPosinCG] = iCurrCostC; |
782 | 0 | if( bSBH ){ |
783 | 0 | piCostDeltaSBH[iScanPosinCG] = iCurrCostF - iCurrCostC; |
784 | 0 | piAddSBH [iScanPosinCG] = -1; |
785 | 0 | } |
786 | 0 | } |
787 | 0 | else |
788 | 0 | { |
789 | 0 | if( iFloor == 0 ) |
790 | 0 | { |
791 | 0 | DTRACE( g_trace_ctx, D_RDOQ, " Lev=%d \n", 0 ); |
792 | 0 | DTRACE( g_trace_ctx, D_RDOQ, " CostC0=%lld\n", (int64_t)piCostCoeff0[iScanPosinCG] ); |
793 | 0 | DTRACE( g_trace_ctx, D_RDOQ, " CostC =%lld\n", (int64_t)iCurrCostC ); |
794 | |
|
795 | 0 | iLastScanPos = -1; |
796 | 0 | lastSubSetId = -1; |
797 | 0 | iScanPos--; |
798 | 0 | iScanPosinCG--; |
799 | 0 | goto findlast2; |
800 | 0 | } |
801 | 0 | uiLevel = iFloor; |
802 | 0 | piCostCoeff[iScanPosinCG] = iCurrCostF; |
803 | 0 | if( bSBH ){ |
804 | 0 | piCostDeltaSBH[iScanPosinCG] = iCurrCostC - iCurrCostF; |
805 | 0 | piAddSBH [iScanPosinCG] = 1; |
806 | 0 | } |
807 | 0 | } |
808 | 0 | } |
809 | 0 | else |
810 | 0 | { |
811 | 0 | const cost_t sigCost[2] = { sigCostOf( ctxIdSig, 0 ), sigCostOf( ctxIdSig, 1 ) }; |
812 | 0 | cost_t iCostSig1 = sigCost[1]; |
813 | 0 | if( iCeil < 3 ) |
814 | 0 | { |
815 | | // ======================= ======================= |
816 | | // ======================= LEVELS 0, 1, 2 ======================= |
817 | | // ======================= ======================= |
818 | | |
819 | | // ----------------- BEST LEVEL = 0 ---------------- |
820 | 0 | cost_t iCostSig0 = sigCost[0]; |
821 | 0 | cost_t iBestCost = piCostCoeff0[iScanPosinCG] + iCostSig0; |
822 | 0 | cost_t iBestCostSig = iCostSig0; |
823 | 0 | cost_t iCostF = iBestCost; |
824 | 0 | uiLevel = 0; |
825 | |
|
826 | 0 | if( iFloor == 1 ) |
827 | 0 | { |
828 | | // ----------------- LEVEL = 1 ---------------- |
829 | 0 | cost_t iErrF = iScaledLevel - ( iFloor << iQBits ); |
830 | 0 | cost_t iDistF = _dist( iErrF, iErrScale, iErrScaleShift ); //( iErrF*iErrScale ) >> iErrScaleShift; |
831 | 0 | iCostF = iDistF + iCostSig1 + levelCost( ctxOffset, iFloor, remRegBins, goRiceZero, goRiceParam ); |
832 | | |
833 | | // (the sign hiding alternative is set below, for the level chosen there) |
834 | 0 | if( iCostF < iBestCost ) |
835 | 0 | { |
836 | 0 | uiLevel = iFloor; |
837 | 0 | iBestCost = iCostF; |
838 | 0 | iBestCostSig = iCostSig1; |
839 | 0 | } |
840 | 0 | } |
841 | | |
842 | | // ----------------- LEVELS = 1, 2 ---------------- |
843 | 0 | cost_t iErrC = iScaledLevel - ( iCeil << iQBits ); |
844 | 0 | cost_t iDistC = _dist( iErrC, iErrScale, iErrScaleShift ); //( iErrC*iErrScale ) >> iErrScaleShift; |
845 | 0 | cost_t iCostC = iDistC + iCostSig1 + levelCost( ctxOffset, iCeil, remRegBins, goRiceZero, goRiceParam ); |
846 | |
|
847 | 0 | if( iCostC < iBestCost ) |
848 | 0 | { |
849 | 0 | uiLevel = iCeil; |
850 | 0 | piCostCoeff[iScanPosinCG] = iCostC; |
851 | 0 | piCostSig[iScanPosinCG] = iCostSig1; |
852 | 0 | if( bSBH ) |
853 | 0 | { |
854 | 0 | piCostDeltaSBH[iScanPosinCG] = iCostF - iCostC; |
855 | 0 | piAddSBH[iScanPosinCG] = -1; |
856 | 0 | } |
857 | 0 | } |
858 | 0 | else |
859 | 0 | { |
860 | 0 | piCostCoeff[iScanPosinCG] = iBestCost; |
861 | 0 | piCostSig[iScanPosinCG] = iBestCostSig; |
862 | 0 | if( bSBH ) |
863 | 0 | { |
864 | | // one level up: from 0 to the floor 1 when level 0 won against it, else from the floor to the ceiling |
865 | 0 | piCostDeltaSBH[iScanPosinCG] = uiLevel == 0 && iFloor == 1 ? iCostF - iBestCost : iCostC - iCostF; |
866 | 0 | piAddSBH [iScanPosinCG] = 1; |
867 | 0 | } |
868 | 0 | } |
869 | 0 | } |
870 | 0 | else |
871 | 0 | { |
872 | | // ----------------- LEVEL X, X+1 ---------------- |
873 | 0 | cost_t iErrF = iScaledLevel - (iFloor << iQBits); |
874 | 0 | cost_t iDistF = _dist( iErrF, iErrScale, iErrScaleShift ); //(iErrF*iErrScale) >> iErrScaleShift; |
875 | 0 | cost_t iCostF = iDistF + iCostSig1 + levelCost( ctxOffset, iFloor, remRegBins, goRiceZero, goRiceParam ); |
876 | |
|
877 | 0 | cost_t iErrC = iScaledLevel - ( iCeil << iQBits ); |
878 | 0 | cost_t iDistC = _dist( iErrC, iErrScale, iErrScaleShift ); //( iErrC*iErrScale ) >> iErrScaleShift; |
879 | 0 | cost_t iCostC = iDistC + iCostSig1 + levelCost( ctxOffset, iCeil, remRegBins, goRiceZero, goRiceParam ); |
880 | |
|
881 | 0 | piCostSig[iScanPosinCG] = iCostSig1; |
882 | 0 | if( iCostC < iCostF ) |
883 | 0 | { |
884 | 0 | uiLevel = iCeil; |
885 | 0 | piCostCoeff[iScanPosinCG] = iCostC; |
886 | 0 | if( bSBH ) |
887 | 0 | { |
888 | 0 | piCostDeltaSBH[iScanPosinCG] = iCostF - iCostC; |
889 | 0 | piAddSBH[iScanPosinCG] = -1; |
890 | 0 | } |
891 | 0 | } |
892 | 0 | else |
893 | 0 | { |
894 | 0 | uiLevel = iFloor; |
895 | 0 | piCostCoeff[iScanPosinCG] = iCostF; |
896 | 0 | if( bSBH ) |
897 | 0 | { |
898 | 0 | piCostDeltaSBH[iScanPosinCG] = iCostC - iCostF; |
899 | 0 | piAddSBH[iScanPosinCG] = 1; |
900 | 0 | } |
901 | 0 | } |
902 | 0 | } |
903 | 0 | } |
904 | 0 | piDstCoeff[uiBlkPos] = uiLevel; |
905 | 0 | DTRACE( g_trace_ctx, D_RDOQ, " Lev=%d \n", uiLevel ); |
906 | 0 | DTRACE( g_trace_ctx, D_RDOQ, " CostC0=%lld\n", (int64_t)piCostCoeff0[iScanPosinCG] ); |
907 | 0 | DTRACE( g_trace_ctx, D_RDOQ, " CostC =%lld\n", (int64_t)piCostCoeff [iScanPosinCG] ); |
908 | 0 | if( uiLevel ) |
909 | 0 | { |
910 | 0 | uiAbsSumCG += uiLevel; |
911 | 0 | iNZbeforePos0 += iScanPosinCG; // hack-> just add instead of checking iScanPosinCG >0 and increment |
912 | 0 | cctx.absVal1stPass( iScanPos, std::min<TCoeff>( 4 + ( uiLevel & 1 ), uiLevel ) ); |
913 | 0 | cctx.setSigGroup(); |
914 | 0 | } |
915 | 0 | } |
916 | | |
917 | | |
918 | | // the bins this position spends while the budget lasts (the sig flag except at the last position), as the residual |
919 | | // coder spends them, at a group's first position too; the Rice parameter restarts with each group |
920 | 0 | if( remRegBins >= 4 ) |
921 | 0 | { |
922 | | // the sig flag is coded except at the last position, and at a group's first position when it is the group's only |
923 | | // level in a group that is neither group 0 nor the last one's (inferred, as the residual coder's inferSigPos) |
924 | 0 | const bool sigInferred = iScanPosinCG == 0 && subSetId > 0 && subSetId != lastSubSetId && uiLevel && uiAbsSumCG == int( uiLevel ); |
925 | 0 | remRegBins -= (uiLevel < 2 ? uiLevel : 3) + (iScanPos != iLastScanPos && !sigInferred); |
926 | 0 | } |
927 | 0 | if( ( (iScanPos & iCGSizeM1) == 0 ) && ( iScanPos > 0 ) ) |
928 | 0 | { |
929 | 0 | goRiceParam = 0; |
930 | 0 | } |
931 | |
|
932 | 0 | iUncodedCostCG += piCostCoeff0[iScanPosinCG]; |
933 | 0 | iCodedCostCG += piCostCoeff[iScanPosinCG]; |
934 | 0 | DTRACE( g_trace_ctx, D_RDOQ_MORE, "Uncoded=%lld\n", (long long)( iUncodedCostBlock + iUncodedCostCG ) ); |
935 | 0 | DTRACE( g_trace_ctx, D_RDOQ_MORE, "Coded =%lld\n", (long long)( iCodedCostBlock + iCodedCostCG ) ); |
936 | 0 | } // for (iScanPosinCG) |
937 | | |
938 | | //================== Group sig. flag =================== |
939 | 0 | cost_t iCostCoeffGroupSig = 0; |
940 | 0 | if( lastSubSetId >= 0 ) |
941 | 0 | { |
942 | 0 | if( subSetId ) |
943 | 0 | { |
944 | 0 | const cost_t sigGroupCost[2] = { sigCostOf( cctx.sigGroupCtxId(), 0 ), sigCostOf( cctx.sigGroupCtxId(), 1 ) }; |
945 | 0 | cost_t iCostCoeffGroupSig0 = sigGroupCost[0]; |
946 | | |
947 | | // if no coeff in CG |
948 | 0 | if( !cctx.isSigGroup() ) |
949 | 0 | { |
950 | 0 | iCodedCostCG = iUncodedCostCG + iCostCoeffGroupSig0; |
951 | 0 | iCostCoeffGroupSig = iCostCoeffGroupSig0; |
952 | 0 | remRegBins = remRegBinsStartCG; // sb_coded_flag 0: the residual coder spends no context bins in the group |
953 | 0 | } |
954 | 0 | else |
955 | 0 | { |
956 | | // if not topleft CG |
957 | 0 | if( subSetId < lastSubSetId ) |
958 | 0 | { |
959 | 0 | cost_t iCostCoeffGroupSig1 = sigGroupCost[1]; |
960 | 0 | iCostCoeffGroupSig = iCostCoeffGroupSig1; |
961 | | |
962 | | // if only one coeff in CG |
963 | 0 | if( !iNZbeforePos0 ) { |
964 | 0 | iCodedCostCG -= piCostSig[0]; |
965 | 0 | } |
966 | 0 | cost_t iUncodedCostCGTmp = iUncodedCostCG + iCostCoeffGroupSig0; |
967 | 0 | iCodedCostCG += iCostCoeffGroupSig1; |
968 | | |
969 | | // if we can save cost, change this block to all-zero block |
970 | 0 | if( iUncodedCostCGTmp < iCodedCostCG ) |
971 | 0 | { |
972 | 0 | cctx.resetSigGroup(); |
973 | 0 | iCodedCostCG = iUncodedCostCGTmp; |
974 | 0 | iCostCoeffGroupSig = iCostCoeffGroupSig0; |
975 | 0 | remRegBins = remRegBinsStartCG; |
976 | | |
977 | | // reset coeffs to 0 in this block |
978 | 0 | for( iScanPosinCG = iCGSize - 1; iScanPosinCG >= 0; iScanPosinCG-- ) |
979 | 0 | { |
980 | 0 | int iScanPosTmp = subSetId * iCGSize + iScanPosinCG; |
981 | 0 | uint32_t uiBlkPos = cctx.blockPos( iScanPosTmp ); |
982 | 0 | if( piDstCoeff[uiBlkPos] ) |
983 | 0 | { |
984 | 0 | int absLevel = std::abs( piDstCoeff[uiBlkPos] ); |
985 | 0 | cctx.remAbsVal1stPass( iScanPosTmp, std::min( absLevel, 4 + ( absLevel & 1 ) ) ); |
986 | 0 | piDstCoeff[uiBlkPos] = 0; |
987 | 0 | } |
988 | 0 | } |
989 | 0 | uiAbsSumCG = 0; |
990 | 0 | if( lastSubSetId == subSetId ) { |
991 | 0 | iCodedCostCG = 0; |
992 | 0 | iUncodedCostCG = 0; |
993 | 0 | iLastScanPos = -1; |
994 | 0 | lastSubSetId = -1; |
995 | 0 | } |
996 | 0 | } |
997 | 0 | } |
998 | 0 | else |
999 | 0 | { |
1000 | 0 | cctx.setSigGroup(); |
1001 | 0 | } |
1002 | 0 | } |
1003 | 0 | } |
1004 | 0 | } |
1005 | | |
1006 | | //===== estimate last position cost ===== |
1007 | 0 | bestTotalCost += iCodedCostCG; |
1008 | 0 | if( !lastOptFinished ) |
1009 | 0 | { |
1010 | 0 | if( cctx.isSigGroup( subSetId ) ) |
1011 | 0 | { |
1012 | 0 | if( !lastBitsBuilt ) |
1013 | 0 | { |
1014 | 0 | xInitLastPosBitsTab( cctx, uiWidth, uiHeight, chType, fracBits ); |
1015 | 0 | lastBitsBuilt = m_lastBitsBuilt[chType] = true; |
1016 | 0 | } |
1017 | 0 | cost_t codedCostBlockTmp = iUncodedCostBlock + iCodedCostCG - iCostCoeffGroupSig; |
1018 | 0 | int startPosInCG = subSetId == lastSubSetId ? iLastScanPos % iCGSize: iCGSizeM1; |
1019 | 0 | int newAbsSumCG = uiAbsSumCG; |
1020 | 0 | int bestLastIdxP1 = iLastScanPos + 1; |
1021 | 0 | for( int iScanPosinCGTmp = startPosInCG; iScanPosinCGTmp >= 0; iScanPosinCGTmp-- ) |
1022 | 0 | { |
1023 | 0 | uint32_t iScanPosTmp = ( subSetId << log2CGSize ) + iScanPosinCGTmp; |
1024 | 0 | uint32_t uiBlkPos = cctx.blockPos( iScanPosTmp ); |
1025 | |
|
1026 | 0 | if( piDstCoeff[uiBlkPos] ) |
1027 | 0 | { |
1028 | 0 | uint32_t uiPosY = uiBlkPos >> uiLog2BlockWidth; |
1029 | 0 | uint32_t uiPosX = uiBlkPos - (uiPosY << uiLog2BlockWidth); |
1030 | 0 | const cost_t iCostLast = xiGetCostLast( uiPosX, uiPosY, chType ); |
1031 | 0 | const cost_t totalCost = codedCostBlockTmp + iCostLast - piCostSig[iScanPosinCGTmp]; |
1032 | |
|
1033 | 0 | if( totalCost < bestTotalCost ) |
1034 | 0 | { |
1035 | 0 | bestLastIdxP1 = iScanPosTmp + 1; |
1036 | 0 | bestTotalCost = totalCost; |
1037 | 0 | lastSubSetId = subSetId; |
1038 | 0 | uiAbsSumCG = newAbsSumCG; |
1039 | 0 | uiAbsSum = 0; |
1040 | 0 | } |
1041 | |
|
1042 | 0 | if( piDstCoeff[uiBlkPos] > 1 ) |
1043 | 0 | { |
1044 | 0 | lastOptFinished = true; |
1045 | 0 | break; |
1046 | 0 | } |
1047 | 0 | newAbsSumCG -= 1; |
1048 | 0 | codedCostBlockTmp -= piCostCoeff [ iScanPosinCGTmp ]; |
1049 | 0 | codedCostBlockTmp += piCostCoeff0[ iScanPosinCGTmp ]; |
1050 | 0 | } |
1051 | 0 | else |
1052 | 0 | { |
1053 | 0 | codedCostBlockTmp -= piCostSig[ iScanPosinCGTmp ]; |
1054 | 0 | } |
1055 | 0 | } //end for |
1056 | 0 | for( int iScanPosTmp = bestLastIdxP1; iScanPosTmp <= iLastScanPos; iScanPosTmp++ ) |
1057 | 0 | { |
1058 | 0 | const int uiBlkPos = cctx.blockPos( iScanPosTmp ); |
1059 | 0 | if( piDstCoeff[uiBlkPos] ) |
1060 | 0 | { |
1061 | 0 | int absLevel = std::abs( piDstCoeff[uiBlkPos] ); |
1062 | 0 | cctx.remAbsVal1stPass( iScanPosTmp, std::min( absLevel, 4 + ( absLevel & 1 ) ) ); |
1063 | 0 | piDstCoeff[uiBlkPos] = 0; |
1064 | 0 | } |
1065 | 0 | } |
1066 | 0 | iLastScanPos = bestLastIdxP1 - 1; |
1067 | 0 | } |
1068 | 0 | } |
1069 | | |
1070 | | //=============== estimate Sign Bit Hiding ================ |
1071 | 0 | if( bSBH ) |
1072 | 0 | { |
1073 | 0 | if( uiAbsSumCG >= 2 /*&& cctx.isSigGroup()*/ ) |
1074 | 0 | { |
1075 | 0 | int iSubPos = subSetId*iCGSize; |
1076 | 0 | int iLastNZPosInCG = -1; |
1077 | 0 | int iFirstNZPosInCG = iCGSize; |
1078 | |
|
1079 | 0 | for( int n = 0; n <iCGSize; n++ ) { |
1080 | 0 | if( piDstCoeff[ cctx.blockPos( n + iSubPos ) ] ) { |
1081 | 0 | iFirstNZPosInCG = n; |
1082 | 0 | break; |
1083 | 0 | } |
1084 | 0 | } |
1085 | 0 | if( lastSubSetId == subSetId ){ |
1086 | 0 | iLastNZPosInCG = ( iLastScanPos )%iCGSize; |
1087 | 0 | if( piDstCoeff[ cctx.blockPos( iLastScanPos ) ] == 1 && ( piAddSBH[iLastNZPosInCG] == -1 ) ) |
1088 | 0 | { |
1089 | 0 | piCostDeltaSBH[iLastNZPosInCG] -= xiGetICost( 4 << SCALE_BITS ); // the last position moves down: about 4 bits saved |
1090 | 0 | } |
1091 | 0 | } |
1092 | 0 | else{ |
1093 | 0 | for( int n = iCGSize - 1; n >= 0; n-- ) { |
1094 | 0 | if( piDstCoeff[ cctx.blockPos( n + iSubPos ) ] ) { |
1095 | 0 | iLastNZPosInCG = n; |
1096 | 0 | break; |
1097 | 0 | } |
1098 | 0 | } |
1099 | 0 | } |
1100 | 0 | if( iLastNZPosInCG - iFirstNZPosInCG >= SBH_THRESHOLD ) |
1101 | 0 | { |
1102 | 0 | iCodedCostCG -= xiGetICost( (int)xGetIEPRate() ); //subtract cost for one sign bin |
1103 | 0 | bool bSign = plSrcCoeff[ cctx.blockPos( iSubPos + iFirstNZPosInCG) ] < 0; |
1104 | |
|
1105 | 0 | if( bSign != ( uiAbsSumCG & 0x1 ) ) { |
1106 | 0 | int iLastPosInCG = ( lastSubSetId == subSetId ) ? iLastNZPosInCG : iCGSize - 1; |
1107 | 0 | int64_t iMinCostDelta = std::numeric_limits<int64_t>::max(); |
1108 | 0 | int iMinCostPos = -1; |
1109 | |
|
1110 | 0 | if( piDstCoeff[ cctx.blockPos( iFirstNZPosInCG + iSubPos ) ] >1 ){ |
1111 | 0 | iMinCostDelta = piCostDeltaSBH[iFirstNZPosInCG]; |
1112 | 0 | iMinCostPos = iFirstNZPosInCG; |
1113 | 0 | } |
1114 | |
|
1115 | 0 | for( int n = 0; n<iFirstNZPosInCG; n++ ){ |
1116 | 0 | if( ( plSrcCoeff[ cctx.blockPos( iSubPos + n ) ] < 0 ) == bSign ){ |
1117 | 0 | if( piCostDeltaSBH[n] < iMinCostDelta ){ |
1118 | 0 | iMinCostDelta = piCostDeltaSBH[n]; |
1119 | 0 | iMinCostPos = n; |
1120 | 0 | } |
1121 | 0 | } |
1122 | 0 | } |
1123 | |
|
1124 | 0 | for( int n = iFirstNZPosInCG + 1; n <= iLastPosInCG; n++ ){ |
1125 | 0 | if( piCostDeltaSBH[n] < iMinCostDelta ){ |
1126 | 0 | iMinCostDelta = piCostDeltaSBH[n]; |
1127 | 0 | iMinCostPos = n; |
1128 | 0 | } |
1129 | 0 | } |
1130 | 0 | const int oldAbsVal = std::abs( piDstCoeff[cctx.blockPos( iMinCostPos + iSubPos )] ); |
1131 | 0 | if( oldAbsVal ) cctx.remAbsVal1stPass( iMinCostPos + iSubPos, std::min( oldAbsVal, 4 + ( oldAbsVal & 1 ) ) ); |
1132 | 0 | piDstCoeff[ cctx.blockPos( iMinCostPos + iSubPos ) ] += piAddSBH[iMinCostPos]; |
1133 | 0 | const int absVal = std::abs( piDstCoeff[cctx.blockPos( iMinCostPos + iSubPos )] ); |
1134 | 0 | if( absVal ) cctx.absVal1stPass( iMinCostPos + iSubPos, std::min( absVal, 4 + ( absVal & 1 ) ) ); |
1135 | 0 | uiAbsSumCG += piAddSBH[iMinCostPos]; |
1136 | 0 | iCodedCostCG += iMinCostDelta; |
1137 | 0 | } |
1138 | 0 | } |
1139 | 0 | } |
1140 | 0 | } |
1141 | |
|
1142 | 0 | iCodedCostBlock += iCodedCostCG; |
1143 | 0 | iUncodedCostBlock += iUncodedCostCG; |
1144 | 0 | uiAbsSum += uiAbsSumCG; |
1145 | 0 | DTRACE( g_trace_ctx, D_RDOQ_COST, "%d: [%2d:%2d]\n", DTRACE_GET_COUNTER( g_trace_ctx, D_RDOQ_COST ), cctx.cgPosX(), cctx.cgPosY() ); |
1146 | 0 | DTRACE( g_trace_ctx, D_RDOQ_MORE, "Uncoded=%lld\n", (long long)( iUncodedCostBlock ) ); |
1147 | 0 | DTRACE( g_trace_ctx, D_RDOQ_MORE, "Coded =%lld\n", (long long)( iCodedCostBlock ) ); |
1148 | 0 | } //end for (iCGScanPos) |
1149 | | |
1150 | 0 | iCodedCostBlock = bestTotalCost; |
1151 | |
|
1152 | 0 | if( iLastScanPos < 0 ) |
1153 | 0 | { |
1154 | 0 | CHECK( uiAbsSum != 0, "Illegal" ); |
1155 | 0 | return 0; |
1156 | 0 | } |
1157 | | |
1158 | 0 | if( !CU::isIntra( *tu.cu ) && isLuma( compID ) ) |
1159 | 0 | { |
1160 | 0 | const BinFracBits fracBitsQtRootCbf = fracBits.getFracBitsArray( Ctx::QtRootCbf() ); |
1161 | 0 | iUncodedCostBlock += xiGetICost( fracBitsQtRootCbf.intBits[0] ); |
1162 | 0 | iCodedCostBlock += xiGetICost( fracBitsQtRootCbf.intBits[1] ); |
1163 | 0 | } |
1164 | 0 | else |
1165 | 0 | { |
1166 | 0 | bool previousCbf = tu.cbf[COMP_Cb]; |
1167 | 0 | bool lastCbfIsInferred = false; |
1168 | 0 | const bool useIntraSubPartitions = tu.cu->ispMode && isLuma(compID); |
1169 | 0 | if( useIntraSubPartitions ) |
1170 | 0 | { |
1171 | 0 | bool rootCbfSoFar = false; |
1172 | 0 | bool isLastSubPartition = CU::isISPLast(*tu.cu, tu.Y(), compID); |
1173 | 0 | uint32_t nTus = tu.cu->ispMode == HOR_INTRA_SUBPARTITIONS ? tu.cu->lheight() >> Log2(tu.lheight()) : tu.cu->lwidth() >> Log2(tu.lwidth()); |
1174 | 0 | if( isLastSubPartition ) |
1175 | 0 | { |
1176 | 0 | TransformUnit* tuPointer = tu.cu->firstTU; |
1177 | 0 | for( int tuIdx = 0; tuIdx < nTus - 1; tuIdx++ ) |
1178 | 0 | { |
1179 | 0 | rootCbfSoFar |= TU::getCbfAtDepth(*tuPointer, COMP_Y, tu.depth); |
1180 | 0 | tuPointer = tuPointer->next; |
1181 | 0 | } |
1182 | 0 | if( !rootCbfSoFar ) |
1183 | 0 | { |
1184 | 0 | lastCbfIsInferred = true; |
1185 | 0 | } |
1186 | 0 | } |
1187 | 0 | if( !lastCbfIsInferred ) |
1188 | 0 | { |
1189 | 0 | previousCbf = TU::getPrevTuCbfAtDepth(tu, compID, tu.depth); |
1190 | 0 | } |
1191 | 0 | } |
1192 | 0 | BinFracBits fracBitsQtCbf = fracBits.getFracBitsArray( Ctx::QtCbf[compID]( DeriveCtx::CtxQtCbf( rect.compID, previousCbf, useIntraSubPartitions ) ) ); |
1193 | |
|
1194 | 0 | if( !lastCbfIsInferred ) |
1195 | 0 | { |
1196 | 0 | iUncodedCostBlock += xiGetICost(fracBitsQtCbf.intBits[0]); |
1197 | 0 | iCodedCostBlock += xiGetICost(fracBitsQtCbf.intBits[1]); |
1198 | 0 | } |
1199 | 0 | } |
1200 | |
|
1201 | 0 | if( iUncodedCostBlock <= iCodedCostBlock ) |
1202 | 0 | { |
1203 | 0 | iCodedCostBlock = iUncodedCostBlock; |
1204 | 0 | uiAbsSum = 0; |
1205 | 0 | ::memset( piDstCoeff, 0, uiMaxNumCoeff*sizeof( TCoeffSig ) ); |
1206 | 0 | } |
1207 | 0 | else |
1208 | 0 | { |
1209 | | // Check due to saving of last pos. Sign data hiding can change the position of last coef. |
1210 | 0 | if( bSBH ) |
1211 | 0 | { |
1212 | 0 | if( piDstCoeff[cctx.blockPos( iLastScanPos )] == 0 ) |
1213 | 0 | { |
1214 | 0 | int scanPos = iLastScanPos - 1; |
1215 | 0 | for( ; scanPos >= 0; scanPos-- ) |
1216 | 0 | { |
1217 | 0 | if( piDstCoeff[cctx.blockPos( scanPos )] ) |
1218 | 0 | break; |
1219 | 0 | } |
1220 | 0 | iLastScanPos = scanPos; |
1221 | 0 | } |
1222 | 0 | } |
1223 | | |
1224 | | // the signs, in raster order over the area the coefficient groups up to the last one's diagonal cover, within the |
1225 | | // scanned region (at most 32x32, the zero-out; the levels outside the scan up to the last position are 0); source and |
1226 | | // levels have the block's width as stride |
1227 | 0 | { |
1228 | 0 | CHECKD( iLastScanPos < 0, "a coded block without a last position" ); |
1229 | 0 | const auto& lastGroup = cctx.cgScan( iLastScanPos >> log2CGSize ); |
1230 | 0 | const unsigned diagEnd = lastGroup.x + lastGroup.y + 1u; |
1231 | 0 | const unsigned rows = std::min<unsigned>( std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, uiHeight ), diagEnd << cctx.log2CGHeight() ); |
1232 | 0 | const unsigned cols = std::min<unsigned>( std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, uiWidth ), diagEnd << cctx.log2CGWidth() ); |
1233 | 0 | for( unsigned y = 0; y < rows; y++ ) |
1234 | 0 | { |
1235 | 0 | const TCoeff* src = plSrcCoeff + y * uiWidth; |
1236 | 0 | TCoeffSig* dst = piDstCoeff + y * uiWidth; |
1237 | 0 | for( unsigned x = 0; x < cols; x++ ) |
1238 | 0 | { |
1239 | 0 | const TCoeffSig sign = TCoeffSig( src[x] >> ( sizeof( TCoeff ) * 8 - 1 ) ); |
1240 | 0 | dst[x] = TCoeffSig( ( sign ^ dst[x] ) - sign ); |
1241 | 0 | } |
1242 | 0 | } |
1243 | 0 | } |
1244 | 0 | tu.lastPos[compID] = iLastScanPos; |
1245 | 0 | } |
1246 | | |
1247 | | #if ENABLE_TRACING |
1248 | | for ( int scanPos = iCGNum * iCGSize-1; scanPos >= 0; scanPos-- ) |
1249 | | { |
1250 | | if(( scanPos & iCGSizeM1) == iCGSizeM1 ) |
1251 | | { |
1252 | | DTRACE(g_trace_ctx, D_RDOQ, "%d:", scanPos >> cctx.log2CGSize() ); |
1253 | | } |
1254 | | int blkPos = cctx.blockPos( scanPos ); |
1255 | | DTRACE( g_trace_ctx, D_RDOQ, "%3d ", piDstCoeff[blkPos] ); |
1256 | | if( scanPos % iCGSize == 0 ) |
1257 | | { |
1258 | | DTRACE(g_trace_ctx, D_RDOQ, "\n"); |
1259 | | } |
1260 | | } |
1261 | | #endif |
1262 | | |
1263 | 0 | DTRACE( g_trace_ctx, D_RDOQ_MORE, "Uncoded=%lld\n", (long long)( iUncodedCostBlock ) ); |
1264 | 0 | DTRACE( g_trace_ctx, D_RDOQ_MORE, "Coded =%lld\n", (long long)( iCodedCostBlock ) ); |
1265 | 0 | DTRACE( g_trace_ctx, D_RDOQ, "%d: %3d, %3d, %dx%d, comp=%d, lastScanPos=%d, absSum=%d, cost=%lld \n", DTRACE_GET_COUNTER( g_trace_ctx, D_RDOQ ), rect.x, rect.y, rect.width, rect.height, compID, iLastScanPos, uiAbsSum, (long long)iCodedCostBlock ); |
1266 | 0 | return 0; |
1267 | 0 | } Unexecuted instantiation: int vvenc::QuantRDOQ2::xRateDistOptQuantFast<true, true>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&) Unexecuted instantiation: int vvenc::QuantRDOQ2::xRateDistOptQuantFast<true, false>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&) Unexecuted instantiation: int vvenc::QuantRDOQ2::xRateDistOptQuantFast<false, true>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&) Unexecuted instantiation: int vvenc::QuantRDOQ2::xRateDistOptQuantFast<false, false>(vvenc::TransformUnit&, vvenc::ComponentID const&, vvenc::AreaBuf<int const> const&, int&, vvenc::QpParam const&, vvenc::Ctx const&) |
1268 | | |
1269 | | int QuantRDOQ2::xRateDistOptQuant( TransformUnit &tu, const ComponentID &compID, const CCoeffBuf &pSrc, TCoeff &uiAbsSum, const QpParam &cQP, const Ctx &ctx, bool bUseScalingList ) |
1270 | 0 | { |
1271 | 0 | if( tu.cs->slice->signDataHidingEnabled/*m_bSBH*/ ) |
1272 | 0 | { |
1273 | 0 | if( bUseScalingList ) return xRateDistOptQuantFast<true, true >( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
1274 | 0 | else return xRateDistOptQuantFast<true, false>( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
1275 | 0 | } |
1276 | 0 | else |
1277 | 0 | { |
1278 | 0 | if( bUseScalingList ) return xRateDistOptQuantFast<false, true >( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
1279 | 0 | else return xRateDistOptQuantFast<false, false>( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
1280 | 0 | } |
1281 | |
|
1282 | 0 | } |
1283 | | |
1284 | | |
1285 | | } // namespace vvenc |
1286 | | |
1287 | | //! \} |