/work/vvenc/source/Lib/CommonLib/DepQuant.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 | | #include "DepQuant.h" |
44 | | #include "DepQuantDecide.h" |
45 | | #include "TrQuant.h" |
46 | | #include "CodingStructure.h" |
47 | | #include "UnitTools.h" |
48 | | |
49 | | #include <bitset> |
50 | | |
51 | | //! \ingroup CommonLib |
52 | | //! \{ |
53 | | |
54 | | namespace vvenc { |
55 | | |
56 | | |
57 | | namespace DQIntern |
58 | | { |
59 | | void Rom::xInitScanArrays() |
60 | 1.43k | { |
61 | 1.43k | if( m_scansInitialized ) |
62 | 0 | { |
63 | 0 | return; |
64 | 0 | } |
65 | 1.43k | ::memset( m_scanId2NbInfoSbbArray, 0, sizeof(m_scanId2NbInfoSbbArray) ); |
66 | 1.43k | ::memset( m_scanId2NbInfoOutArray, 0, sizeof(m_scanId2NbInfoOutArray) ); |
67 | 1.43k | ::memset( m_scanSbb2NbSbbStartsArray, 0, sizeof(m_scanSbb2NbSbbStartsArray) ); |
68 | 1.43k | ::memset( m_nbOutShuffle16.idx, 0x80, sizeof(m_nbOutShuffle16.idx) ); |
69 | 1.43k | ::memset( m_tuParameters, 0, sizeof(m_tuParameters) ); |
70 | | |
71 | 1.43k | uint32_t raster2id[ MAX_CU_SIZE * MAX_CU_SIZE ]; |
72 | 1.43k | ::memset(raster2id, 0, sizeof(raster2id)); |
73 | | |
74 | 11.4k | for( int hd = 0; hd < MAX_TU_SIZE_IDX; hd++ ) |
75 | 10.0k | { |
76 | 80.1k | for( int vd = 0; vd < MAX_TU_SIZE_IDX; vd++ ) |
77 | 70.1k | { |
78 | 70.1k | if( (hd == 0 && vd <= 1) || (hd <= 1 && vd == 0) ) |
79 | 4.29k | { |
80 | 4.29k | continue; |
81 | 4.29k | } |
82 | 65.8k | const uint32_t blockWidth = (1 << hd); |
83 | 65.8k | const uint32_t blockHeight = (1 << vd); |
84 | 65.8k | const uint32_t log2CGWidth = g_log2SbbSize[hd][vd][0]; |
85 | 65.8k | const uint32_t log2CGHeight = g_log2SbbSize[hd][vd][1]; |
86 | 65.8k | const uint32_t groupWidth = 1 << log2CGWidth; |
87 | 65.8k | const uint32_t groupHeight = 1 << log2CGHeight; |
88 | 65.8k | const uint32_t groupSize = groupWidth * groupHeight; |
89 | 65.8k | const SizeType blkWidthIdx = Log2( blockWidth ); |
90 | 65.8k | const SizeType blkHeightIdx = Log2( blockHeight ); |
91 | 65.8k | const ScanElement * scanId2RP = getScanOrder( SCAN_GROUPED_4x4, blkWidthIdx, blkHeightIdx ); |
92 | 65.8k | NbInfoSbb*& sId2NbSbb = m_scanId2NbInfoSbbArray[hd][vd]; |
93 | 65.8k | NbInfoOut*& sId2NbOut = m_scanId2NbInfoOutArray[hd][vd]; |
94 | | // consider only non-zero-out region |
95 | 65.8k | const uint32_t blkWidthNZOut = std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, blockWidth ); |
96 | 65.8k | const uint32_t blkHeightNZOut= std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, blockHeight ); |
97 | 65.8k | const uint32_t totalValues = blkWidthNZOut * blkHeightNZOut; |
98 | | |
99 | 65.8k | sId2NbSbb = new NbInfoSbb[ totalValues ]; |
100 | 65.8k | sId2NbOut = new NbInfoOut[ totalValues ]; |
101 | | |
102 | 65.8k | NbSbbStarts*& sbb2NbStarts = m_scanSbb2NbSbbStartsArray[hd][vd]; |
103 | 65.8k | if( groupWidth == 4 && groupHeight == 4 ) |
104 | 35.8k | { |
105 | 35.8k | sbb2NbStarts = new NbSbbStarts[ totalValues / groupSize ]; |
106 | 793k | for( uint32_t sbb = 0; sbb < totalValues / groupSize; sbb++ ) |
107 | 757k | { |
108 | 757k | std::fill_n( sbb2NbStarts[sbb].start, NUM_NB_SBB, -1 ); |
109 | 757k | } |
110 | 35.8k | } |
111 | | |
112 | 12.9M | for( uint32_t scanId = 0; scanId < totalValues; scanId++ ) |
113 | 12.9M | { |
114 | 12.9M | raster2id[scanId2RP[scanId].idx] = scanId; |
115 | 12.9M | sId2NbSbb[scanId].numInv = 0; |
116 | 12.9M | sId2NbSbb[scanId].invInMask = 0; |
117 | 12.9M | } |
118 | | |
119 | 12.9M | for( unsigned scanId = 0; scanId < totalValues; scanId++ ) |
120 | 12.9M | { |
121 | 12.9M | const int posX = scanId2RP[scanId].x; |
122 | 12.9M | const int posY = scanId2RP[scanId].y; |
123 | 12.9M | const int rpos = scanId2RP[scanId].idx; |
124 | 12.9M | { |
125 | | //===== inside subband neighbours ===== |
126 | 12.9M | const int begSbb = scanId - ( scanId & (groupSize-1) ); // first pos in current subblock |
127 | 12.9M | int cpos[5]; |
128 | | |
129 | 12.9M | cpos[0] = ( posX + 1 < blkWidthNZOut ? ( raster2id[rpos+1 ] < groupSize + begSbb ? raster2id[rpos+1 ] - begSbb : 0 ) : 0 ); |
130 | 12.9M | cpos[1] = ( posX + 2 < blkWidthNZOut ? ( raster2id[rpos+2 ] < groupSize + begSbb ? raster2id[rpos+2 ] - begSbb : 0 ) : 0 ); |
131 | 12.9M | cpos[2] = ( posX + 1 < blkWidthNZOut && posY + 1 < blkHeightNZOut ? ( raster2id[rpos+1+blockWidth] < groupSize + begSbb ? raster2id[rpos+1+blockWidth] - begSbb : 0 ) : 0 ); |
132 | 12.9M | cpos[3] = ( posY + 1 < blkHeightNZOut ? ( raster2id[rpos+ blockWidth] < groupSize + begSbb ? raster2id[rpos+ blockWidth] - begSbb : 0 ) : 0 ); |
133 | 12.9M | cpos[4] = ( posY + 2 < blkHeightNZOut ? ( raster2id[rpos+2*blockWidth] < groupSize + begSbb ? raster2id[rpos+2*blockWidth] - begSbb : 0 ) : 0 ); |
134 | | |
135 | 12.9M | int num = 0; |
136 | 12.9M | int inPos[5] = { 0, }; |
137 | | |
138 | 51.8M | while( true ) |
139 | 51.8M | { |
140 | 51.8M | int nk = -1; |
141 | 310M | for( int k = 0; k < 5; k++ ) |
142 | 259M | { |
143 | 259M | if( cpos[k] != 0 && ( nk < 0 || cpos[k] < cpos[nk] ) ) |
144 | 58.2M | { |
145 | 58.2M | nk = k; |
146 | 58.2M | } |
147 | 259M | } |
148 | 51.8M | if( nk < 0 ) |
149 | 12.9M | { |
150 | 12.9M | break; |
151 | 12.9M | } |
152 | 38.9M | inPos[ num++ ] = uint8_t( cpos[nk] ); |
153 | 38.9M | cpos[nk] = 0; |
154 | 38.9M | } |
155 | 38.5M | for( int k = num; k < 5; k++ ) |
156 | 25.6M | { |
157 | 25.6M | inPos[k] = 0; |
158 | 25.6M | } |
159 | 51.8M | for( int k = 0; k < num; k++ ) |
160 | 38.9M | { |
161 | 38.9M | CHECK( sId2NbSbb[begSbb + inPos[k]].numInv >= 5, "" ); |
162 | 38.9M | sId2NbSbb[begSbb + inPos[k]].invInPos[sId2NbSbb[begSbb + inPos[k]].numInv++] = scanId & ( groupSize - 1 ); |
163 | 38.9M | sId2NbSbb[begSbb + inPos[k]].invInMask |= uint16_t( 1 << ( scanId & ( groupSize - 1 ) ) ); |
164 | 38.9M | } |
165 | 12.9M | } |
166 | 12.9M | { |
167 | | //===== outside subband neighbours ===== |
168 | 12.9M | NbInfoOut& nbOut = sId2NbOut[ scanId ]; |
169 | 12.9M | const int begSbb = scanId - ( scanId & (groupSize-1) ); // first pos in current subblock |
170 | 12.9M | int cpos[5]; |
171 | | |
172 | 12.9M | cpos[0] = ( posX + 1 < blkWidthNZOut ? ( raster2id[rpos+1 ] >= groupSize + begSbb ? raster2id[rpos+1 ] : 0 ) : 0 ); |
173 | 12.9M | cpos[1] = ( posX + 2 < blkWidthNZOut ? ( raster2id[rpos+2 ] >= groupSize + begSbb ? raster2id[rpos+2 ] : 0 ) : 0 ); |
174 | 12.9M | cpos[2] = ( posX + 1 < blkWidthNZOut && posY + 1 < blkHeightNZOut ? ( raster2id[rpos+1+blockWidth] >= groupSize + begSbb ? raster2id[rpos+1+blockWidth] : 0 ) : 0 ); |
175 | 12.9M | cpos[3] = ( posY + 1 < blkHeightNZOut ? ( raster2id[rpos+ blockWidth] >= groupSize + begSbb ? raster2id[rpos+ blockWidth] : 0 ) : 0 ); |
176 | 12.9M | cpos[4] = ( posY + 2 < blkHeightNZOut ? ( raster2id[rpos+2*blockWidth] >= groupSize + begSbb ? raster2id[rpos+2*blockWidth] : 0 ) : 0 ); |
177 | | |
178 | 12.9M | if( sbb2NbStarts ) |
179 | 12.1M | { |
180 | 12.1M | xSetNbSbb16( sbb2NbStarts[scanId / groupSize], cpos, posX & 3, posY & 3, scanId & 15, begSbb ); |
181 | 12.1M | } |
182 | | |
183 | 31.3M | for( nbOut.num = 0; true; ) |
184 | 31.3M | { |
185 | 31.3M | int nk = -1; |
186 | 188M | for( int k = 0; k < 5; k++ ) |
187 | 156M | { |
188 | 156M | if( cpos[k] != 0 && ( nk < 0 || cpos[k] < cpos[nk] ) ) |
189 | 27.0M | { |
190 | 27.0M | nk = k; |
191 | 27.0M | } |
192 | 156M | } |
193 | 31.3M | if( nk < 0 ) |
194 | 12.9M | { |
195 | 12.9M | break; |
196 | 12.9M | } |
197 | 18.4M | nbOut.outPos[ nbOut.num++ ] = uint16_t( cpos[nk] ); |
198 | 18.4M | cpos[nk] = 0; |
199 | 18.4M | } |
200 | 59.0M | for( int k = nbOut.num; k < 5; k++ ) |
201 | 46.1M | { |
202 | 46.1M | nbOut.outPos[k] = 0; |
203 | 46.1M | } |
204 | 12.9M | nbOut.maxDist = ( scanId == 0 ? 0 : sId2NbOut[scanId-1].maxDist ); |
205 | 31.3M | for( int k = 0; k < nbOut.num; k++ ) |
206 | 18.4M | { |
207 | 18.4M | if( nbOut.outPos[k] > nbOut.maxDist ) |
208 | 1.99M | { |
209 | 1.99M | nbOut.maxDist = nbOut.outPos[k]; |
210 | 1.99M | } |
211 | 18.4M | } |
212 | 12.9M | } |
213 | 12.9M | } |
214 | | |
215 | | // make it relative |
216 | 12.9M | for( unsigned scanId = 0; scanId < totalValues; scanId++ ) |
217 | 12.9M | { |
218 | 12.9M | NbInfoOut& nbOut = sId2NbOut[scanId]; |
219 | 12.9M | const int begSbb = scanId - ( scanId & (groupSize-1) ); // first pos in current subblock |
220 | 31.3M | for( int k = 0; k < nbOut.num; k++ ) |
221 | 18.4M | { |
222 | 18.4M | CHECK(begSbb > nbOut.outPos[k], "Position must be past sub block begin"); |
223 | 18.4M | nbOut.outPos[k] -= begSbb; |
224 | 18.4M | } |
225 | 12.9M | nbOut.maxDist -= scanId; |
226 | 12.9M | } |
227 | | |
228 | 197k | for( int chId = 0; chId < MAX_NUM_CH; chId++ ) |
229 | 131k | { |
230 | 131k | m_tuParameters[hd][vd][chId] = new TUParameters( *this, blockWidth, blockHeight, ChannelType(chId) ); |
231 | 131k | } |
232 | 65.8k | } |
233 | 10.0k | } |
234 | | |
235 | | // the shuffle table is complete only now |
236 | 11.4k | for( int hd = 0; hd < MAX_TU_SIZE_IDX; hd++ ) |
237 | 10.0k | { |
238 | 80.1k | for( int vd = 0; vd < MAX_TU_SIZE_IDX; vd++ ) |
239 | 70.1k | { |
240 | 70.1k | if( m_scanSbb2NbSbbStartsArray[hd][vd] ) |
241 | 35.8k | { |
242 | 35.8k | const unsigned numCoeff = std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, 1 << hd ) * std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, 1 << vd ); |
243 | 35.8k | xCheckNbSbb16( m_scanSbb2NbSbbStartsArray[hd][vd], m_scanId2NbInfoOutArray[hd][vd], numCoeff ); |
244 | 35.8k | } |
245 | 70.1k | } |
246 | 10.0k | } |
247 | 1.43k | m_scansInitialized = true; |
248 | 1.43k | } |
249 | | |
250 | | // Records for a position of a 4x4 SBB (x, y and insidePos inside it) in which neighbouring SBB each of its outside |
251 | | // neighbours cpos[k] lies (absolute scan positions, 0 = none) and where inside it. |
252 | | void Rom::xSetNbSbb16( NbSbbStarts& starts, const int cpos[5], int x, int y, int insidePos, int begSbb ) |
253 | 12.1M | { |
254 | 12.1M | static const int dx[5] = { 1, 2, 1, 0, 0 }; // the order of cpos[] |
255 | 12.1M | static const int dy[5] = { 0, 0, 1, 1, 2 }; |
256 | 72.7M | for( int k = 0; k < 5; k++ ) |
257 | 60.6M | { |
258 | 60.6M | if( !cpos[k] ) |
259 | 42.3M | { |
260 | 42.3M | continue; |
261 | 42.3M | } |
262 | 18.2M | const bool right = x + dx[k] >= 4; |
263 | 18.2M | const bool below = y + dy[k] >= 4; |
264 | 18.2M | CHECK( !right && !below, "An outside neighbour must lie in another SBB" ); |
265 | 18.2M | const int nbSbb = right && below ? NB_SBB_DIAG : ( right ? NB_SBB_RIGHT : NB_SBB_BELOW ); |
266 | 18.2M | const int start = ( cpos[k] & ~15 ) - begSbb; |
267 | 18.2M | CHECK( starts.start[nbSbb] >= 0 && starts.start[nbSbb] != start, "A neighbouring SBB must have one start" ); |
268 | 18.2M | starts.start[nbSbb] = int16_t( start ); |
269 | 18.2M | uint8_t& idx = m_nbOutShuffle16.idx[k][nbSbb][insidePos]; |
270 | 18.2M | CHECK( idx != 0x80 && idx != ( cpos[k] & 15 ), "The scan inside a 4x4 SBB must be the same for every TU shape" ); |
271 | 18.2M | idx = uint8_t( cpos[k] & 15 ); |
272 | 18.2M | } |
273 | 12.1M | } |
274 | | |
275 | | // Checks that the starts and the shuffle table give exactly the outside neighbours of NbInfoOut for every position, |
276 | | // and that only the neighbours the SIMD code reads are used: (x+1,y) and (x+2,y) from the right SBB, (x,y+1) and |
277 | | // (x,y+2) from the one below, (x+1,y+1) from any of the three. |
278 | | void Rom::xCheckNbSbb16( const NbSbbStarts* starts, const NbInfoOut* nbOut, unsigned numCoeff ) const |
279 | 35.8k | { |
280 | 35.8k | static const bool used[5][NUM_NB_SBB] = { { true, false, false }, { true, false, false }, { true, true, true }, |
281 | 35.8k | { false, true, false }, { false, true, false } }; |
282 | 214k | for( int k = 0; k < 5; k++ ) |
283 | 179k | { |
284 | 716k | for( int nbSbb = 0; nbSbb < NUM_NB_SBB; nbSbb++ ) |
285 | 537k | { |
286 | 5.11M | for( int p = 0; p < 16 && !used[k][nbSbb]; p++ ) |
287 | 4.58M | { |
288 | 4.58M | CHECK( m_nbOutShuffle16.idx[k][nbSbb][p] != 0x80, "Neighbour lies in an SBB the SIMD code does not read" ); |
289 | 4.58M | } |
290 | 537k | } |
291 | 179k | } |
292 | 12.1M | for( unsigned scanId = 0; scanId < numCoeff; scanId++ ) |
293 | 12.1M | { |
294 | 12.1M | const NbSbbStarts& sbb = starts[scanId >> 4]; |
295 | 12.1M | std::vector<int> fromTable; |
296 | 72.7M | for( int k = 0; k < 5; k++ ) |
297 | 60.6M | { |
298 | 242M | for( int nbSbb = 0; nbSbb < NUM_NB_SBB; nbSbb++ ) |
299 | 181M | { |
300 | 181M | const uint8_t idx = m_nbOutShuffle16.idx[k][nbSbb][scanId & 15]; |
301 | 181M | if( sbb.start[nbSbb] >= 0 && idx != 0x80 ) |
302 | 18.2M | { |
303 | 18.2M | fromTable.push_back( sbb.start[nbSbb] + idx ); |
304 | 18.2M | } |
305 | 181M | } |
306 | 60.6M | } |
307 | 12.1M | std::sort( fromTable.begin(), fromTable.end() ); |
308 | 12.1M | const std::vector<int> expected( nbOut[scanId].outPos, nbOut[scanId].outPos + nbOut[scanId].num ); |
309 | 12.1M | CHECK( fromTable != expected, "The neighbouring SBBs must give the outside neighbours" ); |
310 | 12.1M | } |
311 | 35.8k | } |
312 | | |
313 | | void Rom::xUninitScanArrays() |
314 | 1.43k | { |
315 | 1.43k | if( !m_scansInitialized ) |
316 | 0 | { |
317 | 0 | return; |
318 | 0 | } |
319 | 11.4k | for( int hd = 0; hd < MAX_TU_SIZE_IDX; hd++ ) |
320 | 10.0k | { |
321 | 80.1k | for( int vd = 0; vd < MAX_TU_SIZE_IDX; vd++ ) |
322 | 70.1k | { |
323 | 70.1k | NbInfoSbb*& sId2NbSbb = m_scanId2NbInfoSbbArray[hd][vd]; |
324 | 70.1k | NbInfoOut*& sId2NbOut = m_scanId2NbInfoOutArray[hd][vd]; |
325 | 70.1k | if( sId2NbSbb ) |
326 | 65.8k | { |
327 | 65.8k | delete [] sId2NbSbb; |
328 | 65.8k | } |
329 | 70.1k | if( sId2NbOut ) |
330 | 65.8k | { |
331 | 65.8k | delete [] sId2NbOut; |
332 | 65.8k | } |
333 | 70.1k | delete [] m_scanSbb2NbSbbStartsArray[hd][vd]; |
334 | 210k | for( int chId = 0; chId < MAX_NUM_CH; chId++ ) |
335 | 140k | { |
336 | 140k | TUParameters*& tuPars = m_tuParameters[hd][vd][chId]; |
337 | 140k | if( tuPars ) |
338 | 131k | { |
339 | 131k | delete tuPars; |
340 | 131k | } |
341 | 140k | } |
342 | 70.1k | } |
343 | 10.0k | } |
344 | 1.43k | m_scansInitialized = false; |
345 | 1.43k | } |
346 | | |
347 | | |
348 | | TUParameters::TUParameters( const Rom& rom, const unsigned width, const unsigned height, const ChannelType chType ) |
349 | 131k | { |
350 | 131k | m_chType = chType; |
351 | 131k | m_width = width; |
352 | 131k | m_height = height; |
353 | 131k | const uint32_t nonzeroWidth = std::min<uint32_t>(JVET_C0024_ZERO_OUT_TH, m_width); |
354 | 131k | const uint32_t nonzeroHeight = std::min<uint32_t>(JVET_C0024_ZERO_OUT_TH, m_height); |
355 | 131k | m_numCoeff = nonzeroWidth * nonzeroHeight; |
356 | 131k | m_log2SbbWidth = g_log2SbbSize[ Log2(m_width) ][ Log2(m_height) ][0]; |
357 | 131k | m_log2SbbHeight = g_log2SbbSize[ Log2(m_width) ][ Log2(m_height) ][1]; |
358 | 131k | m_log2SbbSize = m_log2SbbWidth + m_log2SbbHeight; |
359 | 131k | m_sbbSize = ( 1 << m_log2SbbSize ); |
360 | 131k | m_sbbMask = m_sbbSize - 1; |
361 | 131k | m_widthInSbb = nonzeroWidth >> m_log2SbbWidth; |
362 | 131k | m_heightInSbb = nonzeroHeight >> m_log2SbbHeight; |
363 | 131k | m_numSbb = m_widthInSbb * m_heightInSbb; |
364 | 131k | SizeType hsbb = Log2( m_widthInSbb ); |
365 | 131k | SizeType vsbb = Log2( m_heightInSbb ); |
366 | 131k | SizeType hsId = Log2( m_width ); |
367 | 131k | SizeType vsId = Log2( m_height ); |
368 | 131k | m_scanSbbId2SbbPos = getScanOrder( SCAN_UNGROUPED , hsbb , vsbb ); |
369 | 131k | m_scanId2BlkPos = getScanOrder( SCAN_GROUPED_4x4 , hsId , vsId ); |
370 | 131k | int log2W = Log2( m_width ); |
371 | 131k | int log2H = Log2( m_height ); |
372 | 131k | m_scanId2NbInfoSbb = rom.getNbInfoSbb( log2W, log2H ); |
373 | 131k | m_scanId2NbInfoOut = rom.getNbInfoOut( log2W, log2H ); |
374 | 131k | m_scanSbb2NbSbbStarts = rom.getNbSbbStarts( log2W, log2H ); |
375 | 131k | m_nbOutShuffle16 = &rom.getNbOutShuffle16(); |
376 | 131k | m_scanInfo = new ScanInfo[ m_numCoeff ]; |
377 | 25.9M | for( int scanIdx = 0; scanIdx < m_numCoeff; scanIdx++ ) |
378 | 25.8M | { |
379 | 25.8M | xSetScanInfo( m_scanInfo[scanIdx], scanIdx ); |
380 | 25.8M | } |
381 | 131k | } |
382 | | |
383 | | |
384 | | void TUParameters::xSetScanInfo( ScanInfo& scanInfo, int scanIdx ) |
385 | 25.8M | { |
386 | 25.8M | scanInfo.sbbSize = m_sbbSize; |
387 | 25.8M | scanInfo.numSbb = m_numSbb; |
388 | 25.8M | scanInfo.scanIdx = scanIdx; |
389 | 25.8M | scanInfo.rasterPos = m_scanId2BlkPos[scanIdx].idx; |
390 | 25.8M | scanInfo.sbbPos = m_scanSbbId2SbbPos[scanIdx >> m_log2SbbSize].idx; |
391 | 25.8M | scanInfo.insidePos = scanIdx & m_sbbMask; |
392 | 25.8M | scanInfo.spt = SCAN_ISCSBB; |
393 | 25.8M | if( scanInfo.insidePos == m_sbbMask && scanIdx > scanInfo.sbbSize && scanIdx < m_numCoeff - 1 ) |
394 | 1.39M | scanInfo.spt = SCAN_SOCSBB; |
395 | 24.4M | else if( scanInfo.insidePos == 0 && scanIdx > 0 && scanIdx < m_numCoeff - m_sbbSize ) |
396 | 1.39M | scanInfo.spt = SCAN_EOCSBB; |
397 | 25.8M | scanInfo.posX = m_scanId2BlkPos[scanIdx].x; |
398 | 25.8M | scanInfo.posY = m_scanId2BlkPos[scanIdx].y; |
399 | 25.8M | if( scanIdx ) |
400 | 25.7M | { |
401 | 25.7M | const int nextScanIdx = scanIdx - 1; |
402 | 25.7M | const int diag = m_scanId2BlkPos[nextScanIdx].x + m_scanId2BlkPos[nextScanIdx].y; |
403 | | // the context offsets of the diagonal, as CoeffCodingContext derives them (the gtx offsets here count from 1) |
404 | 25.7M | CHECKD( diag >= 128, "diagonal beyond the context offset tables" ); |
405 | 25.7M | scanInfo.sigCtxOffsetNext = g_sigCtxOfsDiag[m_chType][diag]; |
406 | 25.7M | scanInfo.gtxCtxOffsetNext = 1 + g_absCtxOfsDiag[m_chType][diag]; |
407 | 25.7M | scanInfo.nextInsidePos = nextScanIdx & m_sbbMask; |
408 | 25.7M | scanInfo.currNbInfoSbb = m_scanId2NbInfoSbb[ scanIdx ]; |
409 | 25.7M | if( scanInfo.insidePos == 0 ) |
410 | 1.50M | { |
411 | 1.50M | const int nextSbbPos = m_scanSbbId2SbbPos[nextScanIdx >> m_log2SbbSize].idx; |
412 | 1.50M | const int nextSbbPosY = nextSbbPos / m_widthInSbb; |
413 | 1.50M | const int nextSbbPosX = nextSbbPos - nextSbbPosY * m_widthInSbb; |
414 | 1.50M | scanInfo.nextSbbRight = ( nextSbbPosX < m_widthInSbb - 1 ? nextSbbPos + 1 : 0 ); |
415 | 1.50M | scanInfo.nextSbbBelow = ( nextSbbPosY < m_heightInSbb - 1 ? nextSbbPos + m_widthInSbb : 0 ); |
416 | | // the templates of the next SBB reach maxDist past its last position; the first sbbSize levels of that reach are |
417 | | // this SBB's own, which updateStatesEOS stores, the rest a context copies from its previous one |
418 | 1.50M | scanInfo.levelsCopySize = std::max<int>( m_scanId2NbInfoOut[nextScanIdx].maxDist - int( m_sbbSize ), 0 ); |
419 | 1.50M | } |
420 | 25.7M | } |
421 | 25.8M | } |
422 | | |
423 | | RateTablesMemo::RateTablesMemo() |
424 | 5.72k | { |
425 | | // the coefficient-coding contexts must lie back to back in the context store, in the span initCtx compares |
426 | 5.72k | const unsigned first = Ctx::SigCoeffGroup[ 0 ].Offset; |
427 | 5.72k | const unsigned end = Ctx::GtxFlag[ 3 ].Offset + Ctx::GtxFlag[ 3 ].Size; |
428 | 5.72k | CHECK( end - first != RateEstimator::sm_numCoeffCtx, "coefficient-coding contexts are not one span" ); |
429 | 5.72k | #if _DEBUG |
430 | 5.72k | for( const CtxSet* set : { &Ctx::SigCoeffGroup[ 0 ], &Ctx::SigCoeffGroup[ 1 ], &Ctx::SigFlag[ 0 ], &Ctx::SigFlag[ 1 ], |
431 | 5.72k | &Ctx::SigFlag[ 2 ], &Ctx::SigFlag[ 3 ], &Ctx::SigFlag[ 4 ], &Ctx::SigFlag[ 5 ], &Ctx::ParFlag[ 0 ], |
432 | 5.72k | &Ctx::ParFlag[ 1 ], &Ctx::GtxFlag[ 0 ], &Ctx::GtxFlag[ 1 ], &Ctx::GtxFlag[ 2 ], &Ctx::GtxFlag[ 3 ] } ) |
433 | 80.1k | { |
434 | 80.1k | CHECKD( set->Offset < first || set->Offset + set->Size > end, "a coefficient-coding context lies outside the span" ); |
435 | 80.1k | } |
436 | 5.72k | #endif |
437 | | // no real context has these bytes (a state's high byte is at most 0x7f), so the first call per channel type builds |
438 | | // the tables (and a kept table set is only read after it was built) |
439 | 11.4k | for( Channel& c : channel ) memset( c.ctxStates, 0xff, sizeof( c.ctxStates ) ); |
440 | | |
441 | | // where equalCoeffCtxStates expects each set of contexts (the end of the last one is checked above) |
442 | 5.72k | CHECKD( Ctx::SigCoeffGroup[ 1 ].Offset - first != 2 || Ctx::SigFlag[ 0 ].Offset - first != 4 || Ctx::SigFlag[ 1 ].Offset - first != 16 |
443 | 5.72k | || Ctx::SigFlag[ 2 ].Offset - first != 24 || Ctx::SigFlag[ 3 ].Offset - first != 36 || Ctx::SigFlag[ 4 ].Offset - first != 44 |
444 | 5.72k | || Ctx::SigFlag[ 5 ].Offset - first != 56 || Ctx::ParFlag[ 0 ].Offset - first != 64 || Ctx::ParFlag[ 1 ].Offset - first != 85 |
445 | 5.72k | || Ctx::GtxFlag[ 0 ].Offset - first != 96 || Ctx::GtxFlag[ 1 ].Offset - first != 117 || Ctx::GtxFlag[ 2 ].Offset - first != 128 |
446 | 5.72k | || Ctx::GtxFlag[ 3 ].Offset - first != 149, "the coefficient-coding contexts are not where equalCoeffCtxStates reads them" ); |
447 | 5.72k | } |
448 | | |
449 | | // The OR of the XORed 64-bit words of numCtx context models from firstCtx on (0 when they are equal), which the compiler |
450 | | // can vectorize; unlike memcmp it needs no ordering and no library call (whose speed differs between C runtimes). The |
451 | | // last word may overlap the one before it. |
452 | | template<unsigned firstCtx, unsigned numCtx> |
453 | | static inline uint64_t diffOfCtxs( const uint8_t* a, const uint8_t* b ) |
454 | 6.58M | { |
455 | 6.58M | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); |
456 | 6.58M | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); |
457 | 6.58M | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; |
458 | 6.58M | uint64_t diff = 0, x, y; |
459 | 45.2M | for( unsigned i = first; i < last8; i += 8 ) |
460 | 38.7M | { |
461 | 38.7M | memcpy( &x, a + i, 8 ); |
462 | 38.7M | memcpy( &y, b + i, 8 ); |
463 | 38.7M | diff |= x ^ y; |
464 | 38.7M | } |
465 | 6.58M | memcpy( &x, a + last8, 8 ); |
466 | 6.58M | memcpy( &y, b + last8, 8 ); |
467 | 6.58M | return diff | ( x ^ y ); |
468 | 6.58M | } DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<0u, 2u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 35.3k | { | 455 | 35.3k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 35.3k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 35.3k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 35.3k | uint64_t diff = 0, x, y; | 459 | 70.6k | for( unsigned i = first; i < last8; i += 8 ) | 460 | 35.3k | { | 461 | 35.3k | memcpy( &x, a + i, 8 ); | 462 | 35.3k | memcpy( &y, b + i, 8 ); | 463 | 35.3k | diff |= x ^ y; | 464 | 35.3k | } | 465 | 35.3k | memcpy( &x, a + last8, 8 ); | 466 | 35.3k | memcpy( &y, b + last8, 8 ); | 467 | 35.3k | return diff | ( x ^ y ); | 468 | 35.3k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<4u, 12u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 35.3k | { | 455 | 35.3k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 35.3k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 35.3k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 35.3k | uint64_t diff = 0, x, y; | 459 | 318k | for( unsigned i = first; i < last8; i += 8 ) | 460 | 282k | { | 461 | 282k | memcpy( &x, a + i, 8 ); | 462 | 282k | memcpy( &y, b + i, 8 ); | 463 | 282k | diff |= x ^ y; | 464 | 282k | } | 465 | 35.3k | memcpy( &x, a + last8, 8 ); | 466 | 35.3k | memcpy( &y, b + last8, 8 ); | 467 | 35.3k | return diff | ( x ^ y ); | 468 | 35.3k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<24u, 12u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 35.3k | { | 455 | 35.3k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 35.3k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 35.3k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 35.3k | uint64_t diff = 0, x, y; | 459 | 318k | for( unsigned i = first; i < last8; i += 8 ) | 460 | 282k | { | 461 | 282k | memcpy( &x, a + i, 8 ); | 462 | 282k | memcpy( &y, b + i, 8 ); | 463 | 282k | diff |= x ^ y; | 464 | 282k | } | 465 | 35.3k | memcpy( &x, a + last8, 8 ); | 466 | 35.3k | memcpy( &y, b + last8, 8 ); | 467 | 35.3k | return diff | ( x ^ y ); | 468 | 35.3k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<44u, 12u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 35.3k | { | 455 | 35.3k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 35.3k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 35.3k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 35.3k | uint64_t diff = 0, x, y; | 459 | 318k | for( unsigned i = first; i < last8; i += 8 ) | 460 | 282k | { | 461 | 282k | memcpy( &x, a + i, 8 ); | 462 | 282k | memcpy( &y, b + i, 8 ); | 463 | 282k | diff |= x ^ y; | 464 | 282k | } | 465 | 35.3k | memcpy( &x, a + last8, 8 ); | 466 | 35.3k | memcpy( &y, b + last8, 8 ); | 467 | 35.3k | return diff | ( x ^ y ); | 468 | 35.3k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<64u, 21u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 35.3k | { | 455 | 35.3k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 35.3k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 35.3k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 35.3k | uint64_t diff = 0, x, y; | 459 | 565k | for( unsigned i = first; i < last8; i += 8 ) | 460 | 530k | { | 461 | 530k | memcpy( &x, a + i, 8 ); | 462 | 530k | memcpy( &y, b + i, 8 ); | 463 | 530k | diff |= x ^ y; | 464 | 530k | } | 465 | 35.3k | memcpy( &x, a + last8, 8 ); | 466 | 35.3k | memcpy( &y, b + last8, 8 ); | 467 | 35.3k | return diff | ( x ^ y ); | 468 | 35.3k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<96u, 21u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 35.3k | { | 455 | 35.3k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 35.3k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 35.3k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 35.3k | uint64_t diff = 0, x, y; | 459 | 565k | for( unsigned i = first; i < last8; i += 8 ) | 460 | 530k | { | 461 | 530k | memcpy( &x, a + i, 8 ); | 462 | 530k | memcpy( &y, b + i, 8 ); | 463 | 530k | diff |= x ^ y; | 464 | 530k | } | 465 | 35.3k | memcpy( &x, a + last8, 8 ); | 466 | 35.3k | memcpy( &y, b + last8, 8 ); | 467 | 35.3k | return diff | ( x ^ y ); | 468 | 35.3k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<128u, 21u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 35.3k | { | 455 | 35.3k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 35.3k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 35.3k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 35.3k | uint64_t diff = 0, x, y; | 459 | 565k | for( unsigned i = first; i < last8; i += 8 ) | 460 | 530k | { | 461 | 530k | memcpy( &x, a + i, 8 ); | 462 | 530k | memcpy( &y, b + i, 8 ); | 463 | 530k | diff |= x ^ y; | 464 | 530k | } | 465 | 35.3k | memcpy( &x, a + last8, 8 ); | 466 | 35.3k | memcpy( &y, b + last8, 8 ); | 467 | 35.3k | return diff | ( x ^ y ); | 468 | 35.3k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<2u, 2u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 905k | { | 455 | 905k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 905k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 905k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 905k | uint64_t diff = 0, x, y; | 459 | 1.81M | for( unsigned i = first; i < last8; i += 8 ) | 460 | 905k | { | 461 | 905k | memcpy( &x, a + i, 8 ); | 462 | 905k | memcpy( &y, b + i, 8 ); | 463 | 905k | diff |= x ^ y; | 464 | 905k | } | 465 | 905k | memcpy( &x, a + last8, 8 ); | 466 | 905k | memcpy( &y, b + last8, 8 ); | 467 | 905k | return diff | ( x ^ y ); | 468 | 905k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<16u, 8u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 905k | { | 455 | 905k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 905k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 905k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 905k | uint64_t diff = 0, x, y; | 459 | 5.43M | for( unsigned i = first; i < last8; i += 8 ) | 460 | 4.52M | { | 461 | 4.52M | memcpy( &x, a + i, 8 ); | 462 | 4.52M | memcpy( &y, b + i, 8 ); | 463 | 4.52M | diff |= x ^ y; | 464 | 4.52M | } | 465 | 905k | memcpy( &x, a + last8, 8 ); | 466 | 905k | memcpy( &y, b + last8, 8 ); | 467 | 905k | return diff | ( x ^ y ); | 468 | 905k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<36u, 8u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 905k | { | 455 | 905k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 905k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 905k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 905k | uint64_t diff = 0, x, y; | 459 | 5.43M | for( unsigned i = first; i < last8; i += 8 ) | 460 | 4.52M | { | 461 | 4.52M | memcpy( &x, a + i, 8 ); | 462 | 4.52M | memcpy( &y, b + i, 8 ); | 463 | 4.52M | diff |= x ^ y; | 464 | 4.52M | } | 465 | 905k | memcpy( &x, a + last8, 8 ); | 466 | 905k | memcpy( &y, b + last8, 8 ); | 467 | 905k | return diff | ( x ^ y ); | 468 | 905k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<56u, 8u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 905k | { | 455 | 905k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 905k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 905k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 905k | uint64_t diff = 0, x, y; | 459 | 5.43M | for( unsigned i = first; i < last8; i += 8 ) | 460 | 4.52M | { | 461 | 4.52M | memcpy( &x, a + i, 8 ); | 462 | 4.52M | memcpy( &y, b + i, 8 ); | 463 | 4.52M | diff |= x ^ y; | 464 | 4.52M | } | 465 | 905k | memcpy( &x, a + last8, 8 ); | 466 | 905k | memcpy( &y, b + last8, 8 ); | 467 | 905k | return diff | ( x ^ y ); | 468 | 905k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<85u, 11u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 905k | { | 455 | 905k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 905k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 905k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 905k | uint64_t diff = 0, x, y; | 459 | 8.15M | for( unsigned i = first; i < last8; i += 8 ) | 460 | 7.24M | { | 461 | 7.24M | memcpy( &x, a + i, 8 ); | 462 | 7.24M | memcpy( &y, b + i, 8 ); | 463 | 7.24M | diff |= x ^ y; | 464 | 7.24M | } | 465 | 905k | memcpy( &x, a + last8, 8 ); | 466 | 905k | memcpy( &y, b + last8, 8 ); | 467 | 905k | return diff | ( x ^ y ); | 468 | 905k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<117u, 11u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 905k | { | 455 | 905k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 905k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 905k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 905k | uint64_t diff = 0, x, y; | 459 | 8.15M | for( unsigned i = first; i < last8; i += 8 ) | 460 | 7.24M | { | 461 | 7.24M | memcpy( &x, a + i, 8 ); | 462 | 7.24M | memcpy( &y, b + i, 8 ); | 463 | 7.24M | diff |= x ^ y; | 464 | 7.24M | } | 465 | 905k | memcpy( &x, a + last8, 8 ); | 466 | 905k | memcpy( &y, b + last8, 8 ); | 467 | 905k | return diff | ( x ^ y ); | 468 | 905k | } |
DepQuant.cpp:unsigned long vvenc::DQIntern::diffOfCtxs<149u, 11u>(unsigned char const*, unsigned char const*) Line | Count | Source | 454 | 905k | { | 455 | 905k | static_assert( numCtx * sizeof( BinProbModel ) >= 8, "diffOfCtxs compares at least one 64-bit word" ); | 456 | 905k | const unsigned first = firstCtx * unsigned( sizeof( BinProbModel ) ); | 457 | 905k | const unsigned last8 = first + numCtx * unsigned( sizeof( BinProbModel ) ) - 8; | 458 | 905k | uint64_t diff = 0, x, y; | 459 | 8.15M | for( unsigned i = first; i < last8; i += 8 ) | 460 | 7.24M | { | 461 | 7.24M | memcpy( &x, a + i, 8 ); | 462 | 7.24M | memcpy( &y, b + i, 8 ); | 463 | 7.24M | diff |= x ^ y; | 464 | 7.24M | } | 465 | 905k | memcpy( &x, a + last8, 8 ); | 466 | 905k | memcpy( &y, b + last8, 8 ); | 467 | 905k | return diff | ( x ^ y ); | 468 | 905k | } |
|
469 | | |
470 | | // true when the context bytes the tables of this channel type are built from are equal. The contexts of the other |
471 | | // channel type take turns with these ones in the store, so each set is read on its own (at the indices checked in |
472 | | // RateTablesMemo's constructor). |
473 | | static inline bool equalCoeffCtxStates( const uint8_t* a, const uint8_t* b, const ChannelType chType ) |
474 | 941k | { |
475 | 941k | if( chType == CH_L ) |
476 | 35.3k | { |
477 | 35.3k | return ( diffOfCtxs< 0, 2>( a, b ) // SigCoeffGroup[0] |
478 | 35.3k | | diffOfCtxs< 4, 12>( a, b ) // SigFlag[0] |
479 | 35.3k | | diffOfCtxs< 24, 12>( a, b ) // SigFlag[2] |
480 | 35.3k | | diffOfCtxs< 44, 12>( a, b ) // SigFlag[4] |
481 | 35.3k | | diffOfCtxs< 64, 21>( a, b ) // ParFlag[0] |
482 | 35.3k | | diffOfCtxs< 96, 21>( a, b ) // GtxFlag[0] |
483 | 35.3k | | diffOfCtxs<128, 21>( a, b ) ) == 0; // GtxFlag[2] |
484 | 35.3k | } |
485 | 905k | return ( diffOfCtxs< 2, 2>( a, b ) // SigCoeffGroup[1] |
486 | 905k | | diffOfCtxs< 16, 8>( a, b ) // SigFlag[1] |
487 | 905k | | diffOfCtxs< 36, 8>( a, b ) // SigFlag[3] |
488 | 905k | | diffOfCtxs< 56, 8>( a, b ) // SigFlag[5] |
489 | 905k | | diffOfCtxs< 85, 11>( a, b ) // ParFlag[1] |
490 | 905k | | diffOfCtxs<117, 11>( a, b ) // GtxFlag[1] |
491 | 905k | | diffOfCtxs<149, 11>( a, b ) ) == 0; // GtxFlag[3] |
492 | 941k | } |
493 | | |
494 | | bool RateEstimator::initCtx( const TUParameters& tuPars, const TransformUnit& tu, const ComponentID compID, const FracBitsAccess& fracBitsAccess, |
495 | | RateTablesMemo& memo ) |
496 | 941k | { |
497 | | // The sig and gtx tables depend only on the channel type and the states of the coefficient-coding contexts. The |
498 | | // memo keeps, per channel type, the last tables built and the raw context bytes they came from: when those are |
499 | | // unchanged, as they mostly are, the kept tables are still valid. The result is identical to building them every time. |
500 | 941k | const ChannelType chType = tuPars.m_chType; |
501 | 941k | RateTablesMemo::Channel& kept = memo.channel[ chType ]; |
502 | 941k | const uint8_t* ctxStates = reinterpret_cast<const uint8_t*>( &fracBitsAccess[ Ctx::SigCoeffGroup[ 0 ].Offset ] ); |
503 | 941k | const bool built = !equalCoeffCtxStates( kept.ctxStates, ctxStates, chType ); |
504 | 941k | if( built ) |
505 | 602k | { |
506 | 602k | xSetSigSbbFracBits( fracBitsAccess, chType, kept.tables ); |
507 | 602k | xSetSigFlagBits ( fracBitsAccess, chType, kept.tables ); |
508 | 602k | xSetGtxFlagBits ( fracBitsAccess, chType, kept.tables ); |
509 | 602k | memcpy( kept.ctxStates, ctxStates, sizeof( kept.ctxStates ) ); |
510 | 602k | } |
511 | 941k | m_tables = &kept.tables; |
512 | 941k | xSetLastCoeffOffset( fracBitsAccess, tuPars, tu, compID ); |
513 | 941k | return built; |
514 | 941k | } |
515 | | |
516 | | void RateEstimator::xSetZeroProofRates( ZeroProofRates& r ) const |
517 | 0 | { |
518 | | // chroma context ranges: sig_coeff_flag 4..7 for a diagonal < 2, else 0..3; gtx 6..10 for diagonal 0, else 1..5 |
519 | 0 | for( int d = 0; d < 2; d++ ) |
520 | 0 | { |
521 | 0 | const int sigFirst = d == 0 ? 4 : 0; |
522 | 0 | r.sig0[d] = r.sig1[d] = std::numeric_limits<int32_t>::max(); |
523 | 0 | for( unsigned set = 0; set < sm_numCtxSetsSig; set++ ) |
524 | 0 | { |
525 | 0 | for( int c = sigFirst; c < sigFirst + 4; c++ ) |
526 | 0 | { |
527 | 0 | r.sig0[d] = std::min<int32_t>( r.sig0[d], m_tables->sig[set][c].intBits[0] ); |
528 | 0 | r.sig1[d] = std::min<int32_t>( r.sig1[d], m_tables->sig[set][c].intBits[1] ); |
529 | 0 | } |
530 | 0 | } |
531 | 0 | const int gtxFirst = d == 0 ? 6 : 1; |
532 | 0 | r.level[d] = std::numeric_limits<int32_t>::max(); |
533 | 0 | for( int c = gtxFirst; c < gtxFirst + 5; c++ ) |
534 | 0 | { |
535 | 0 | r.level[d] = std::min( r.level[d], *std::min_element( m_tables->gtx[c].bits + 1, m_tables->gtx[c].bits + 6 ) ); |
536 | 0 | } |
537 | 0 | } |
538 | 0 | r.lastLevel = *std::min_element( m_tables->gtx[0].bits + 1, m_tables->gtx[0].bits + 6 ); |
539 | 0 | r.sbb0 = std::min<int32_t>( m_tables->sigSbb[0].intBits[0], m_tables->sigSbb[1].intBits[0] ); |
540 | 0 | r.sbb1 = std::min<int32_t>( m_tables->sigSbb[0].intBits[1], m_tables->sigSbb[1].intBits[1] ); |
541 | 0 | } |
542 | | |
543 | | void RateEstimator::xSetLastCoeffOffset( const FracBitsAccess& fracBitsAccess, const TUParameters& tuPars, const TransformUnit& tu, const ComponentID compID ) |
544 | 941k | { |
545 | 941k | const ChannelType chType = ( compID == COMP_Y ? CH_L : CH_C ); |
546 | 941k | int32_t cbfDeltaBits = 0; |
547 | 941k | if( compID == COMP_Y && !CU::isIntra(*tu.cu) && !tu.depth ) |
548 | 268 | { |
549 | 268 | const BinFracBits bits = fracBitsAccess.getFracBitsArray( Ctx::QtRootCbf() ); |
550 | 268 | cbfDeltaBits = int32_t( bits.intBits[1] ) - int32_t( bits.intBits[0] ); |
551 | 268 | } |
552 | 940k | else |
553 | 940k | { |
554 | 940k | BinFracBits bits; |
555 | 940k | bool prevLumaCbf = false; |
556 | 940k | bool lastCbfIsInferred = false; |
557 | 940k | bool useIntraSubPartitions = tu.cu->ispMode && isLuma(chType); |
558 | 940k | if( useIntraSubPartitions ) |
559 | 13.0k | { |
560 | 13.0k | bool rootCbfSoFar = false; |
561 | 13.0k | bool isLastSubPartition = CU::isISPLast(*tu.cu, tu.Y(), compID); |
562 | 13.0k | uint32_t nTus = tu.cu->ispMode == HOR_INTRA_SUBPARTITIONS ? tu.cu->lheight() >> Log2(tu.lheight()) : tu.cu->lwidth() >> Log2(tu.lwidth()); |
563 | 13.0k | if( isLastSubPartition ) |
564 | 202 | { |
565 | 202 | TransformUnit* tuPointer = tu.cu->firstTU; |
566 | 808 | for( int tuIdx = 0; tuIdx < nTus - 1; tuIdx++ ) |
567 | 606 | { |
568 | 606 | rootCbfSoFar |= TU::getCbfAtDepth(*tuPointer, COMP_Y, tu.depth); |
569 | 606 | tuPointer = tuPointer->next; |
570 | 606 | } |
571 | 202 | if( !rootCbfSoFar ) |
572 | 0 | { |
573 | 0 | lastCbfIsInferred = true; |
574 | 0 | } |
575 | 202 | } |
576 | 13.0k | if( !lastCbfIsInferred ) |
577 | 13.0k | { |
578 | 13.0k | prevLumaCbf = TU::getPrevTuCbfAtDepth(tu, compID, tu.depth); |
579 | 13.0k | } |
580 | 13.0k | bits = fracBitsAccess.getFracBitsArray(Ctx::QtCbf[compID](DeriveCtx::CtxQtCbf(compID, prevLumaCbf, true))); |
581 | 13.0k | } |
582 | 927k | else |
583 | 927k | { |
584 | 927k | bits = fracBitsAccess.getFracBitsArray(Ctx::QtCbf[compID](DeriveCtx::CtxQtCbf(compID, tu.cbf[COMP_Cb]))); |
585 | 927k | } |
586 | 940k | cbfDeltaBits = lastCbfIsInferred ? 0 : int32_t(bits.intBits[1]) - int32_t(bits.intBits[0]); |
587 | 940k | } |
588 | | |
589 | 941k | uint32_t ctxBits [ LAST_SIGNIFICANT_GROUPS ]; |
590 | 2.82M | for( unsigned xy = 0; xy < 2; xy++ ) |
591 | 1.88M | { |
592 | 1.88M | int32_t bitOffset = ( xy ? cbfDeltaBits : 0 ); |
593 | 1.88M | int32_t* lastBits = ( xy ? m_lastBitsY : m_lastBitsX ); |
594 | 1.88M | const unsigned size = ( xy ? tuPars.m_height : tuPars.m_width ); |
595 | 1.88M | const unsigned log2Size = Log2( size ); |
596 | 1.88M | const bool useYCtx = ( xy != 0 ); |
597 | 1.88M | const CtxSet& ctxSetLast = ( useYCtx ? Ctx::LastY : Ctx::LastX )[ chType ]; |
598 | | // the context derivation shared with CoeffCodingContext |
599 | 1.88M | const unsigned lastShift = CoeffCodingContext::lastPosCtxShift ( chType, log2Size ); |
600 | 1.88M | const unsigned lastOffset = CoeffCodingContext::lastPosCtxOffset( chType, log2Size ); |
601 | 1.88M | uint32_t sumFBits = 0; |
602 | 1.88M | const unsigned numPos = std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, size ); |
603 | 1.88M | const unsigned maxCtxId = CoeffCodingContext::lastPosMaxGroup( log2Size ); |
604 | 12.7M | for( unsigned ctxId = 0; ctxId < maxCtxId; ctxId++ ) |
605 | 10.8M | { |
606 | 10.8M | const BinFracBits bits = fracBitsAccess.getFracBitsArray( ctxSetLast( lastOffset + ( ctxId >> lastShift ) ) ); |
607 | 10.8M | ctxBits[ ctxId ] = sumFBits + bits.intBits[0] + ( ctxId>3 ? ((ctxId-2)>>1)<<SCALE_BITS : 0 ) + bitOffset; |
608 | 10.8M | sumFBits += bits.intBits[1]; |
609 | 10.8M | } |
610 | 1.88M | ctxBits [ maxCtxId ] = sumFBits + ( maxCtxId>3 ? ((maxCtxId-2)>>1)<<SCALE_BITS : 0 ) + bitOffset; |
611 | | |
612 | | // lastBits[ pos ] = ctxBits[ g_uiGroupIdx[ pos ] ] for the first numPos positions, written as the runs of |
613 | | // g_uiGroupIdx (Rom.cpp: groups 0 to 3 one position each, 4 and 5 two, 6 and 7 four, 8 and 9 eight), which end at |
614 | | // positions 4, 8, 16 and 32; numPos is 1 (ISP's 1-wide sub-TUs), 2, 4, 8, 16 or 32 |
615 | 1.88M | CHECKD( ( numPos & ( numPos - 1 ) ) != 0, "a last-position run would be cut" ); |
616 | 9.37M | for( unsigned pos = 0; pos < std::min( numPos, 4u ); pos++ ) |
617 | 7.49M | { |
618 | 7.49M | lastBits[ pos ] = ctxBits[ pos ]; |
619 | 7.49M | } |
620 | 1.88M | if( numPos >= 8 ) |
621 | 1.46M | { |
622 | 1.46M | std::fill_n( lastBits + 4, 2, int32_t( ctxBits[ 4 ] ) ); |
623 | 1.46M | std::fill_n( lastBits + 6, 2, int32_t( ctxBits[ 5 ] ) ); |
624 | 1.46M | } |
625 | 1.88M | if( numPos >= 16 ) |
626 | 853k | { |
627 | 853k | std::fill_n( lastBits + 8, 4, int32_t( ctxBits[ 6 ] ) ); |
628 | 853k | std::fill_n( lastBits + 12, 4, int32_t( ctxBits[ 7 ] ) ); |
629 | 853k | } |
630 | 1.88M | if( numPos >= 32 ) |
631 | 290k | { |
632 | 290k | std::fill_n( lastBits + 16, 8, int32_t( ctxBits[ 8 ] ) ); |
633 | 290k | std::fill_n( lastBits + 24, 8, int32_t( ctxBits[ 9 ] ) ); |
634 | 290k | } |
635 | 1.88M | } |
636 | 941k | } |
637 | | |
638 | | void RateEstimator::xSetSigSbbFracBits( const FracBitsAccess& fracBitsAccess, ChannelType chType, RateTables& tables ) |
639 | 602k | { |
640 | 602k | const CtxSet& ctxSet = Ctx::SigCoeffGroup[ chType ]; |
641 | 1.80M | for( unsigned ctxId = 0; ctxId < sm_maxNumSigSbbCtx; ctxId++ ) |
642 | 1.20M | { |
643 | 1.20M | tables.sigSbb[ ctxId ] = fracBitsAccess.getFracBitsArray( ctxSet( ctxId ) ); |
644 | 1.20M | } |
645 | 602k | } |
646 | | |
647 | | void RateEstimator::xSetSigFlagBits( const FracBitsAccess& fracBitsAccess, ChannelType chType, RateTables& tables ) |
648 | 602k | { |
649 | 2.40M | for( unsigned ctxSetId = 0; ctxSetId < sm_numCtxSetsSig; ctxSetId++ ) |
650 | 1.80M | { |
651 | 1.80M | BinFracBits* bits = tables.sig [ ctxSetId ]; |
652 | 1.80M | const CtxSet& ctxSet = Ctx::SigFlag [ chType + 2*ctxSetId ]; |
653 | 1.80M | const unsigned numCtx = ( chType == CH_L ? 12 : 8 ); |
654 | 16.3M | for( unsigned ctxId = 0; ctxId < numCtx; ctxId++ ) |
655 | 14.4M | { |
656 | 14.4M | bits[ ctxId ] = fracBitsAccess.getFracBitsArray( ctxSet( ctxId ) ); |
657 | 14.4M | } |
658 | 1.80M | } |
659 | 602k | } |
660 | | |
661 | | void RateEstimator::xSetGtxFlagBits( const FracBitsAccess& fracBitsAccess, ChannelType chType, RateTables& tables ) |
662 | 602k | { |
663 | 602k | const CtxSet& ctxSetPar = Ctx::ParFlag [ chType ]; |
664 | 602k | const CtxSet& ctxSetGt1 = Ctx::GtxFlag [ 2 + chType ]; |
665 | 602k | const CtxSet& ctxSetGt2 = Ctx::GtxFlag [ chType ]; |
666 | 602k | const unsigned numCtx = ( chType == CH_L ? 21 : 11 ); |
667 | 7.26M | for( unsigned ctxId = 0; ctxId < numCtx; ctxId++ ) |
668 | 6.66M | { |
669 | 6.66M | BinFracBits fbPar = fracBitsAccess.getFracBitsArray( ctxSetPar( ctxId ) ); |
670 | 6.66M | BinFracBits fbGt1 = fracBitsAccess.getFracBitsArray( ctxSetGt1( ctxId ) ); |
671 | 6.66M | BinFracBits fbGt2 = fracBitsAccess.getFracBitsArray( ctxSetGt2( ctxId ) ); |
672 | 6.66M | CoeffFracBits& cb = tables.gtx[ ctxId ]; |
673 | 6.66M | int32_t par0 = (1<<SCALE_BITS) + int32_t(fbPar.intBits[0]); |
674 | 6.66M | int32_t par1 = (1<<SCALE_BITS) + int32_t(fbPar.intBits[1]); |
675 | 6.66M | cb.bits[0] = 0; |
676 | 6.66M | cb.bits[1] = fbGt1.intBits[0] + (1 << SCALE_BITS); |
677 | 6.66M | cb.bits[2] = fbGt1.intBits[1] + par0 + fbGt2.intBits[0]; |
678 | 6.66M | cb.bits[3] = fbGt1.intBits[1] + par1 + fbGt2.intBits[0]; |
679 | 6.66M | cb.bits[4] = fbGt1.intBits[1] + par0 + fbGt2.intBits[1]; |
680 | 6.66M | cb.bits[5] = fbGt1.intBits[1] + par1 + fbGt2.intBits[1]; |
681 | 6.66M | } |
682 | 602k | } |
683 | | |
684 | | void CommonCtx::updateTemplates( const ScanInfo& scanInfo, const uint8_t* absLevels, int stateId, StateMem& curr ) const |
685 | 84.7k | { |
686 | 84.7k | const NbInfoOut* nbOut = m_nbInfo + scanInfo.scanIdx - scanInfo.sbbSize; |
687 | | |
688 | 1.44M | for( int id = 0; id < scanInfo.sbbSize; id++, nbOut++ ) |
689 | 1.35M | { |
690 | 1.35M | if( nbOut->num ) |
691 | 913k | { |
692 | 913k | TCoeff sumAbs = 0, sumAbs1 = 0, sumNum = 0; |
693 | 2.21M | #define UPDATE(k) {TCoeff t=absLevels[nbOut->outPos[k]]; sumAbs+=t; sumAbs1+=std::min<TCoeff>(4+(t&1),t); sumNum+=!!t; } |
694 | 913k | switch( nbOut->num ) |
695 | 913k | { |
696 | 0 | default: |
697 | 59.2k | case 5: |
698 | 59.2k | UPDATE( 4 ); |
699 | 177k | case 4: |
700 | 177k | UPDATE( 3 ); |
701 | 490k | case 3: |
702 | 490k | UPDATE( 2 ); |
703 | 575k | case 2: |
704 | 575k | UPDATE( 1 ); |
705 | 913k | case 1: |
706 | 913k | UPDATE( 0 ); |
707 | 913k | } |
708 | 913k | #undef UPDATE |
709 | 913k | curr.tplAcc[id][stateId] = ( sumNum << 5 ) | sumAbs1; |
710 | 913k | curr.sum1st[id][stateId] = ( uint8_t ) std::min( 255, sumAbs ); |
711 | 913k | } |
712 | 1.35M | } |
713 | 84.7k | } |
714 | | |
715 | | // The rows of the dequantized buffer dequantBlock clears. A block of 64 rows has coefficients only in its first 32 |
716 | | // (JVET-C0024 zero-out), and nothing reads the dequantized rows past them: neither the inverse transform (TrQuant::xIT) |
717 | | // nor the encoder's next forward transform and quantization of the buffer. The LFNST cut (xIT reads 4 or 8 rows) can't |
718 | | // be used the same way: the encoder reuses the dequantized buffer in its next forward LFNST pass, and the streams change |
719 | | // without the full clear there. |
720 | | static int rowsToClear( const int height ) |
721 | 925k | { |
722 | 925k | return std::min( height, JVET_C0024_ZERO_OUT_TH ); |
723 | 925k | } |
724 | | |
725 | | // The rows of the level buffer DepQuant::xQuantDQ clears: the 32 that can hold levels, plus the 2 below them. The |
726 | | // residual coder's Rice parameter template (CoeffCodingContext::templateAbsSum) reads the levels one and two rows below |
727 | | // a coefficient up to the block's full height, so for the last coded rows it reads rows 32 and 33. The decoder sees |
728 | | // zeros there; levels left over from an earlier block would give a different Rice parameter and a stream it can't parse. |
729 | | static int levelRowsToClear( const int height ) |
730 | 2.25M | { |
731 | 2.25M | return std::min( height, JVET_C0024_ZERO_OUT_TH + 2 ); |
732 | 2.25M | } |
733 | | |
734 | | void Quantizer::initQuantBlock(const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, const double lambda, int gValue) |
735 | 223k | { |
736 | 223k | CHECKD( lambda <= 0.0, "Lambda must be greater than 0" ); |
737 | | |
738 | 223k | const int qpDQ = cQP.Qp(tu.mtsIdx[compID]==MTS_SKIP) + 1; |
739 | 223k | const int qpPer = qpDQ / 6; |
740 | 223k | const int qpRem = qpDQ - 6 * qpPer; |
741 | 223k | const SPS& sps = *tu.cs->sps; |
742 | 223k | const CompArea& area = tu.blocks[ compID ]; |
743 | 223k | const ChannelType chType = toChannelType( compID ); |
744 | 223k | const int channelBitDepth = sps.bitDepths[ chType ]; |
745 | 223k | const int maxLog2TrDynamicRange = sps.getMaxLog2TrDynamicRange(); |
746 | 223k | const int nomTransformShift = getTransformShift( channelBitDepth, area.size(), maxLog2TrDynamicRange ); |
747 | 223k | const bool needsSqrt2ScaleAdjustment = TU::needsSqrt2Scale(tu, compID); |
748 | 223k | const int transformShift = nomTransformShift + (needsSqrt2ScaleAdjustment?-1:0); |
749 | | // quant parameters |
750 | 223k | m_QShift = QUANT_SHIFT - 1 + qpPer + transformShift; |
751 | 223k | m_QAdd = -( ( 3 << m_QShift ) >> 1 ); |
752 | 223k | Intermediate_Int invShift = IQUANT_SHIFT + 1 - qpPer - transformShift; |
753 | 223k | m_QScale = g_quantScales[needsSqrt2ScaleAdjustment?1:0][ qpRem ]; |
754 | 223k | const unsigned qIdxBD = std::min<unsigned>( maxLog2TrDynamicRange + 1, 8*sizeof(Intermediate_Int) + invShift - IQUANT_SHIFT - 1 ); |
755 | 223k | m_maxQIdx = ( 1 << (qIdxBD-1) ) - 4; |
756 | 223k | if( m_QShift ) |
757 | 223k | m_thresLast = TCoeff((int64_t(m_DqThrVal) << (m_QShift-1))); |
758 | 0 | else |
759 | 0 | m_thresLast = TCoeff((int64_t(m_DqThrVal>>1) << m_QShift)); |
760 | | // the all-zero sub-block limit of the trellis (xPopulateTrellis), in half quantization steps of 1 << m_QShift; |
761 | | // m_QShift = QUANT_SHIFT - 1 + qpPer + transformShift is at least 10 (transformShift >= -3) |
762 | 223k | m_thresSSbb = Quant::sbbSkipLimit( m_SbbSkipThrVal, m_QShift ); |
763 | | // distortion calculation parameters |
764 | 223k | const int64_t qScale = (gValue==-1) ? m_QScale : gValue; |
765 | 223k | const int nomDShift = |
766 | 223k | SCALE_BITS - 2 * (nomTransformShift + DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth)) + m_QShift + (needsSqrt2ScaleAdjustment ? 1 : 0); |
767 | 223k | const double qScale2 = double( qScale * qScale ); |
768 | 223k | const double nomDistFactor = ( nomDShift < 0 ? 1.0/(double(int64_t(1)<<(-nomDShift))*qScale2*lambda) : double(int64_t(1)<<nomDShift)/(qScale2*lambda) ); |
769 | 223k | const uint32_t pow2dfShift = (uint32_t)( nomDistFactor * qScale2 ) + 1; |
770 | 223k | const int dfShift = ceilLog2( pow2dfShift ); |
771 | 223k | m_DistShift = 62 + m_QShift - 2*maxLog2TrDynamicRange - dfShift; |
772 | 223k | m_DistAdd = (int64_t(1) << m_DistShift) >> 1; |
773 | 223k | m_DistStepAdd = ((m_DistShift+m_QShift)>=64 ? (int64_t)( nomDistFactor * pow(2,m_DistShift+m_QShift) + .5 ) : (int64_t)( nomDistFactor * double(int64_t(1)<<(m_DistShift+m_QShift)) + .5 )); |
774 | 223k | m_DistOrgFact = (int64_t)( nomDistFactor * double(int64_t(1)<<(m_DistShift+1 )) + .5 ); |
775 | 223k | } |
776 | | |
777 | | void Quantizer::initQuantBlock( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, const double lambda, QuantBlockMemo& memo ) |
778 | 2.25M | { |
779 | 2.25M | CHECKD( tu.mtsIdx[compID] == MTS_SKIP, "the memo does not cover transform skip" ); |
780 | | |
781 | 2.25M | const int qpDQ = cQP.Qp( false ) + 1; |
782 | 2.25M | const CompArea& area = tu.blocks[compID]; |
783 | 2.25M | QuantBlockMemo::Entry& kept = memo.entry[toChannelType( compID )][Log2( area.width ) + Log2( area.height )]; |
784 | 2.25M | if( kept.qpDQ == qpDQ && kept.lambda == lambda ) |
785 | 2.03M | { |
786 | 2.03M | static_cast<QuantBlockParams&>( *this ) = kept.params; |
787 | 2.03M | return; |
788 | 2.03M | } |
789 | 223k | initQuantBlock( tu, compID, cQP, lambda ); |
790 | 223k | m_thresDefault = TCoeff( m_thresLast / ( TCoeff( m_QScale ) << 2 ) ); |
791 | 223k | kept.qpDQ = qpDQ; |
792 | 223k | kept.lambda = lambda; |
793 | 223k | kept.params = *this; |
794 | 223k | } |
795 | | |
796 | | void dequantCoeffs( const TCoeffSig* qCoeff, TCoeff* tCoeff, const ScanElement* scan, int lastScanIdx, int /*width*/, int /*height*/, |
797 | | int shift, Intermediate_Int invQScale, const int* piDequantCoef, TCoeff minTCoeff, TCoeff maxTCoeff ) |
798 | 925k | { |
799 | 925k | const Intermediate_Int add = (shift < 0) ? 0 : ((1 << shift) >> 1); |
800 | 9.84M | for( int state = 0, scanIdx = lastScanIdx; scanIdx >= 0; scanIdx-- ) |
801 | 8.92M | { |
802 | 8.92M | const unsigned rasterPos = scan[scanIdx].idx; |
803 | 8.92M | const TCoeffSig& level = qCoeff[ rasterPos ]; |
804 | | // zero levels are not skipped by a (often mispredicted) branch: their qIdx is 0, so they give the 0 of the memset |
805 | 8.92M | if (piDequantCoef) |
806 | 0 | invQScale = piDequantCoef[rasterPos];//scalingfactor*levelScale |
807 | 8.92M | if (shift < 0 && level && (piDequantCoef || scanIdx == lastScanIdx)) |
808 | 480k | { |
809 | 480k | invQScale <<= -shift; |
810 | 480k | } |
811 | 8.92M | const int sign = (level > 0) - (level < 0); // -1, 0 or 1 |
812 | 8.92M | Intermediate_Int qIdx = 2 * level - sign * (state >> 1); |
813 | 8.92M | int64_t nomTCoeff = ((int64_t)qIdx * (int64_t)invQScale + add) >> ((shift < 0) ? 0 : shift); |
814 | 8.92M | tCoeff[rasterPos] = (TCoeff)Clip3<int64_t>(minTCoeff, maxTCoeff, nomTCoeff); |
815 | 8.92M | state = ( 32040 >> ((state<<2)+((level&1)<<1)) ) & 3; // the 16-bit value "32040" represent the state transition table |
816 | 8.92M | } |
817 | 925k | } |
818 | | |
819 | | void Quantizer::dequantBlock( const TransformUnit& tu, const ComponentID compID, const QpParam& cQP, CoeffBuf& recCoeff, bool enableScalingLists, int* piDequantCoef, DequantCoeffsFunc dequant ) const |
820 | 925k | { |
821 | | |
822 | | //----- set basic parameters ----- |
823 | 925k | const CompArea& area = tu.blocks[ compID ]; |
824 | 925k | const SizeType hsId = Log2( area.width ); |
825 | 925k | const SizeType vsId = Log2( area.height ); |
826 | 925k | const ScanElement *scan = getScanOrder( SCAN_GROUPED_4x4, hsId, vsId ); |
827 | 925k | const TCoeffSig* qCoeff = tu.getCoeffs( compID ).buf; |
828 | 925k | TCoeff* tCoeff = recCoeff.buf; |
829 | | |
830 | | //----- reset the coefficients that are read and get last scan index ----- |
831 | 925k | ::memset( tCoeff, 0, rowsToClear( area.height ) * area.width * sizeof( TCoeff ) ); |
832 | 925k | int lastScanIdx = tu.lastPos[compID]; |
833 | 925k | if( lastScanIdx < 0 ) |
834 | 0 | { |
835 | 0 | return; |
836 | 0 | } |
837 | | |
838 | | //----- set dequant parameters ----- |
839 | 925k | const int qpDQ = cQP.Qp(tu.mtsIdx[compID]==MTS_SKIP) + 1; |
840 | 925k | const int qpPer = qpDQ / 6; |
841 | 925k | const int qpRem = qpDQ - 6 * qpPer; |
842 | 925k | const SPS& sps = *tu.cs->sps; |
843 | 925k | const ChannelType chType = toChannelType( compID ); |
844 | 925k | const int channelBitDepth = sps.bitDepths[ chType ]; |
845 | 925k | const int maxLog2TrDynamicRange = sps.getMaxLog2TrDynamicRange(); |
846 | 925k | const TCoeff minTCoeff = -( 1 << maxLog2TrDynamicRange ); |
847 | 925k | const TCoeff maxTCoeff = ( 1 << maxLog2TrDynamicRange ) - 1; |
848 | 925k | const int nomTransformShift = getTransformShift( channelBitDepth, area.size(), maxLog2TrDynamicRange ); |
849 | 925k | const bool needsSqrt2ScaleAdjustment = TU::needsSqrt2Scale(tu, compID); |
850 | 925k | const int transformShift = nomTransformShift + (needsSqrt2ScaleAdjustment?-1:0); |
851 | 925k | const int shift = IQUANT_SHIFT + 1 - qpPer - transformShift + (enableScalingLists ? LOG2_SCALING_LIST_NEUTRAL_VALUE : 0); |
852 | 925k | const int invQScale = g_invQuantScales[needsSqrt2ScaleAdjustment?1:0][ qpRem ]; |
853 | | //----- dequant coefficients ----- |
854 | 925k | dequant( qCoeff, tCoeff, scan, lastScanIdx, area.width, area.height, shift, invQScale, enableScalingLists ? piDequantCoef : nullptr, minTCoeff, maxTCoeff ); |
855 | 925k | } |
856 | | |
857 | | bool Quantizer::preQuantCoeff( const TCoeff absCoeff, PQData* pqData, int quanCoeff ) const |
858 | 0 | { |
859 | 0 | int64_t scaledOrg = int64_t( absCoeff ) * quanCoeff; |
860 | 0 | TCoeff qIdx = TCoeff( ( scaledOrg + m_QAdd ) >> m_QShift ); |
861 | |
|
862 | 0 | if( qIdx < 0 ) |
863 | 0 | { |
864 | 0 | int64_t scaledAdd = m_DistStepAdd - scaledOrg * m_DistOrgFact; |
865 | 0 | PQData& pq_a = pqData[1]; |
866 | 0 | PQData& pq_b = pqData[2]; |
867 | |
|
868 | 0 | pq_a.deltaDist = ( ( scaledAdd + 0 * m_DistStepAdd ) * 1 + m_DistAdd ) >> m_DistShift; |
869 | 0 | pq_a.absLevel = 1; |
870 | |
|
871 | 0 | pq_b.deltaDist = ( ( scaledAdd + 1 * m_DistStepAdd ) * 2 + m_DistAdd ) >> m_DistShift; |
872 | 0 | pq_b.absLevel = 1; |
873 | | |
874 | 0 | return true; |
875 | 0 | } |
876 | | |
877 | 0 | qIdx = std::max<TCoeff>( 1, std::min<TCoeff>( m_maxQIdx, qIdx ) ); |
878 | 0 | int64_t scaledAdd = qIdx * m_DistStepAdd - scaledOrg * m_DistOrgFact; |
879 | |
|
880 | 0 | PQData& pq_a = pqData[( qIdx + 0 ) & 3]; |
881 | 0 | PQData& pq_b = pqData[( qIdx + 1 ) & 3]; |
882 | 0 | PQData& pq_c = pqData[( qIdx + 2 ) & 3]; |
883 | 0 | PQData& pq_d = pqData[( qIdx + 3 ) & 3]; |
884 | |
|
885 | 0 | pq_a.deltaDist = ( ( scaledAdd + 0 * m_DistStepAdd ) * ( qIdx + 0 ) + m_DistAdd ) >> m_DistShift; |
886 | 0 | pq_a.absLevel = ( qIdx + 1 ) >> 1; |
887 | |
|
888 | 0 | pq_b.deltaDist = ( ( scaledAdd + 1 * m_DistStepAdd ) * ( qIdx + 1 ) + m_DistAdd ) >> m_DistShift; |
889 | 0 | pq_b.absLevel = ( qIdx + 2 ) >> 1; |
890 | |
|
891 | 0 | pq_c.deltaDist = ( ( scaledAdd + 2 * m_DistStepAdd ) * ( qIdx + 2 ) + m_DistAdd ) >> m_DistShift; |
892 | 0 | pq_c.absLevel = ( qIdx + 3 ) >> 1; |
893 | |
|
894 | 0 | pq_d.deltaDist = ( ( scaledAdd + 3 * m_DistStepAdd ) * ( qIdx + 3 ) + m_DistAdd ) >> m_DistShift; |
895 | 0 | pq_d.absLevel = ( qIdx + 4 ) >> 1; |
896 | |
|
897 | 0 | return false; |
898 | 0 | } |
899 | | |
900 | | const int32_t g_goRiceBits[4][RICEMAX] = |
901 | | { |
902 | | { 32768, 65536, 98304, 131072, 163840, 196608, 262144, 262144, 327680, 327680, 327680, 327680, 393216, 393216, 393216, 393216, 393216, 393216, 393216, 393216, 458752, 458752, 458752, 458752, 458752, 458752, 458752, 458752, 458752, 458752, 458752, 458752}, |
903 | | { 65536, 65536, 98304, 98304, 131072, 131072, 163840, 163840, 196608, 196608, 229376, 229376, 294912, 294912, 294912, 294912, 360448, 360448, 360448, 360448, 360448, 360448, 360448, 360448, 425984, 425984, 425984, 425984, 425984, 425984, 425984, 425984}, |
904 | | { 98304, 98304, 98304, 98304, 131072, 131072, 131072, 131072, 163840, 163840, 163840, 163840, 196608, 196608, 196608, 196608, 229376, 229376, 229376, 229376, 262144, 262144, 262144, 262144, 327680, 327680, 327680, 327680, 327680, 327680, 327680, 327680}, |
905 | | {131072, 131072, 131072, 131072, 131072, 131072, 131072, 131072, 163840, 163840, 163840, 163840, 163840, 163840, 163840, 163840, 196608, 196608, 196608, 196608, 196608, 196608, 196608, 196608, 229376, 229376, 229376, 229376, 229376, 229376, 229376, 229376} |
906 | | }; |
907 | | |
908 | | static inline void initStates( const int stateId, DQIntern::StateMem& state ) |
909 | 7.52M | { |
910 | 7.52M | state.rdCost[stateId] = DQIntern::rdCostInit; |
911 | 7.52M | state.ctx.cff[stateId] = 0; |
912 | 7.52M | state.ctx.sig[stateId] = 0; |
913 | 7.52M | state.numSig[stateId] = 0; |
914 | 7.52M | state.refSbbCtxId[stateId] = -1; |
915 | 7.52M | state.remRegBins[stateId] = 4; |
916 | 7.52M | state.cffBitsCtxOffset = 0; |
917 | 7.52M | state.m_goRicePar[stateId] = 0; |
918 | 7.52M | state.m_goRiceZero[stateId] = 0; |
919 | 7.52M | state.sbbBits0[stateId] = 0; |
920 | 7.52M | state.sbbBits1[stateId] = 0; |
921 | 7.52M | } |
922 | | |
923 | | void checkAllRdCosts( const DQIntern::ScanPosType spt, const DQIntern::PQData* pqData, DQIntern::Decisions& decisions, const DQIntern::StateMem& state ) |
924 | 6.61M | { |
925 | 6.61M | ::memcpy( &decisions, startDec, sizeof( Decisions ) ); |
926 | 6.61M | checkRdCosts<true>( 0, spt, pqData[0], pqData[2], decisions, 0, 2, state ); |
927 | 6.61M | checkRdCosts<true>( 1, spt, pqData[0], pqData[2], decisions, 2, 0, state ); |
928 | 6.61M | checkRdCosts<true>( 2, spt, pqData[3], pqData[1], decisions, 1, 3, state ); |
929 | 6.61M | checkRdCosts<true>( 3, spt, pqData[3], pqData[1], decisions, 3, 1, state ); |
930 | 6.61M | } |
931 | | |
932 | | static void checkAllRdCostsOdd1( const DQIntern::ScanPosType spt, const int64_t pq_a_dist, const int64_t pq_b_dist, DQIntern::Decisions& decisions, const DQIntern::StateMem& state ) |
933 | 1.15M | { |
934 | 1.15M | ::memcpy( &decisions, startDec, sizeof( Decisions ) ); |
935 | 1.15M | checkRdCostsOdd1<true>( 0, spt, pq_b_dist, decisions, 2, 0, state ); |
936 | 1.15M | checkRdCostsOdd1<true>( 1, spt, pq_b_dist, decisions, 0, 2, state ); |
937 | 1.15M | checkRdCostsOdd1<true>( 2, spt, pq_a_dist, decisions, 3, 1, state ); |
938 | 1.15M | checkRdCostsOdd1<true>( 3, spt, pq_a_dist, decisions, 1, 3, state ); |
939 | 1.15M | } |
940 | | |
941 | | static void update1State( int stateId, const DQIntern::ScanInfo& scanInfo, const DQIntern::Decisions& decisions, DQIntern::StateMem& curr, DQIntern::StateMem& prev ) |
942 | 31.4M | { |
943 | 31.4M | curr.rdCost[stateId] = decisions.rdCost[stateId]; |
944 | 31.4M | if( decisions.prevId[stateId] > -2 ) |
945 | 30.1M | { |
946 | 30.1M | if( decisions.prevId[stateId] >= 0 ) |
947 | 28.6M | { |
948 | 28.6M | const int prevId = decisions.prevId[stateId]; |
949 | 28.6M | curr.numSig[stateId] = prev.numSig[prevId] + !!decisions.absLevel[stateId]; |
950 | 28.6M | curr.refSbbCtxId[stateId] = prev.refSbbCtxId[prevId]; |
951 | 28.6M | curr.sbbBits0[stateId] = prev.sbbBits0[prevId]; |
952 | 28.6M | curr.sbbBits1[stateId] = prev.sbbBits1[prevId]; |
953 | 28.6M | curr.remRegBins[stateId] = prev.remRegBins[prevId] - 1; |
954 | | |
955 | 28.6M | if( curr.remRegBins[stateId] >= 4 ) |
956 | 28.3M | { |
957 | 28.3M | curr.remRegBins[stateId] -= ( decisions.absLevel[stateId] < 2 ? decisions.absLevel[stateId] : 3 ); |
958 | 28.3M | } |
959 | | |
960 | 487M | for( int i = 0; i < 16; i++ ) |
961 | 458M | { |
962 | 458M | curr.tplAcc[i][stateId] = prev.tplAcc[i][prevId]; |
963 | 458M | curr.sum1st[i][stateId] = prev.sum1st[i][prevId]; |
964 | 458M | curr.absVal[i][stateId] = prev.absVal[i][prevId]; |
965 | 458M | } |
966 | 28.6M | } |
967 | 1.50M | else |
968 | 1.50M | { |
969 | 1.50M | curr.numSig[stateId] = 1; |
970 | 1.50M | curr.refSbbCtxId[stateId] = -1; |
971 | 1.50M | curr.remRegBins[stateId] = prev.initRemRegBins; |
972 | 1.50M | curr.remRegBins[stateId] -= ( decisions.absLevel[stateId] < 2 ? decisions.absLevel[stateId] : 3 ); |
973 | | |
974 | 25.5M | for( int i = 0; i < 16; i++ ) |
975 | 24.0M | { |
976 | 24.0M | curr.tplAcc[i][stateId] = 0; |
977 | 24.0M | curr.sum1st[i][stateId] = 0; |
978 | 24.0M | curr.absVal[i][stateId] = 0; |
979 | 24.0M | } |
980 | 1.50M | } |
981 | | |
982 | 30.1M | if( decisions.absLevel[stateId] ) |
983 | 27.4M | { |
984 | 27.4M | curr.absVal[scanInfo.insidePos][stateId] = ( uint8_t ) std::min<TCoeff>( 126 + ( decisions.absLevel[stateId] & 1 ), decisions.absLevel[stateId] ); |
985 | | |
986 | 27.4M | if( scanInfo.currNbInfoSbb.numInv ) |
987 | 27.4M | { |
988 | 27.4M | int min4_or_5 = std::min<TCoeff>( 4 + ( decisions.absLevel[stateId] & 1 ), decisions.absLevel[stateId] ); |
989 | | |
990 | 27.4M | auto adds8 = []( uint8_t a, uint8_t b ) |
991 | 80.3M | { |
992 | 80.3M | uint8_t c = a + b; |
993 | 80.3M | if( c < a ) c = -1; |
994 | 80.3M | return c; |
995 | 80.3M | }; |
996 | | |
997 | 27.4M | auto update_deps = [&]( int k ) |
998 | 80.3M | { |
999 | 80.3M | curr.tplAcc[scanInfo.currNbInfoSbb.invInPos[k]][stateId] += 32 + min4_or_5; |
1000 | 80.3M | curr.sum1st[scanInfo.currNbInfoSbb.invInPos[k]][stateId] = adds8( curr.sum1st[scanInfo.currNbInfoSbb.invInPos[k]][stateId], decisions.absLevel[stateId] ); |
1001 | 80.3M | }; |
1002 | | |
1003 | 27.4M | switch( scanInfo.currNbInfoSbb.numInv ) |
1004 | 27.4M | { |
1005 | 0 | default: |
1006 | 4.59M | case 5: |
1007 | 4.59M | update_deps( 4 ); |
1008 | 11.4M | case 4: |
1009 | 11.4M | update_deps( 3 ); |
1010 | 13.9M | case 3: |
1011 | 13.9M | update_deps( 2 ); |
1012 | 22.8M | case 2: |
1013 | 22.8M | update_deps( 1 ); |
1014 | 27.4M | case 1: |
1015 | 27.4M | update_deps( 0 ); |
1016 | 27.4M | } |
1017 | 27.4M | } |
1018 | 27.4M | } |
1019 | | |
1020 | 30.1M | if( curr.remRegBins[stateId] >= 4 ) |
1021 | 29.6M | { |
1022 | 29.6M | TCoeff sumAbs1 = curr.tplAcc[scanInfo.nextInsidePos][stateId] & 31; |
1023 | 29.6M | TCoeff sumNum = curr.tplAcc[scanInfo.nextInsidePos][stateId] >> 5u; |
1024 | 29.6M | int sumGt1 = sumAbs1 - sumNum; |
1025 | | |
1026 | 29.6M | curr.ctx.sig[stateId] = scanInfo.sigCtxOffsetNext + std::min( ( sumAbs1 + 1 ) >> 1, 3 ); |
1027 | 29.6M | curr.ctx.cff[stateId] = scanInfo.gtxCtxOffsetNext + std::min( sumGt1, 4 ); |
1028 | 29.6M | } |
1029 | 490k | else |
1030 | 490k | { |
1031 | 490k | curr.anyRemRegBinsLt4 = true; |
1032 | 490k | } |
1033 | 30.1M | } |
1034 | 31.4M | } |
1035 | | |
1036 | | static void update1StateEOS( const int stateId, const DQIntern::ScanInfo& scanInfo, const DQIntern::Decisions& decisions, const DQIntern::StateMem& skip, DQIntern::StateMem& curr, DQIntern::StateMem& prev, DQIntern::CommonCtx& commonCtx ) |
1037 | 139k | { |
1038 | 139k | curr.rdCost[stateId] = decisions.rdCost[stateId]; |
1039 | | |
1040 | 139k | if( decisions.prevId[stateId] > -2 ) |
1041 | 138k | { |
1042 | 138k | if( decisions.prevId[stateId] >= 4 ) |
1043 | 48.6k | { |
1044 | 48.6k | CHECK( decisions.absLevel[stateId] != 0, "cannot happen" ); |
1045 | | |
1046 | 48.6k | const int prevId = decisions.prevId[stateId] - 4; |
1047 | 48.6k | curr.numSig [stateId] = 0; |
1048 | 48.6k | curr.remRegBins[stateId] = skip.remRegBins[prevId]; |
1049 | 48.6k | curr.refSbbCtxId[stateId] = prevId; |
1050 | | |
1051 | 826k | for( int i = 0; i < 16; i++ ) |
1052 | 777k | { |
1053 | 777k | curr.absVal[i][stateId] = 0; |
1054 | 777k | } |
1055 | 48.6k | } |
1056 | 89.7k | else if( decisions.prevId[stateId] >= 0 ) |
1057 | 87.9k | { |
1058 | 87.9k | const int prevId = decisions.prevId[stateId]; |
1059 | 87.9k | curr.numSig[stateId] = prev.numSig[prevId] + !!decisions.absLevel[stateId]; |
1060 | 87.9k | curr.refSbbCtxId[stateId] = prev.refSbbCtxId[prevId]; |
1061 | 87.9k | curr.remRegBins[stateId] = prev.remRegBins[prevId] - 1; |
1062 | | |
1063 | 87.9k | if( curr.remRegBins[stateId] >= 4 ) |
1064 | 72.3k | { |
1065 | 72.3k | curr.remRegBins[stateId] -= ( decisions.absLevel[stateId] < 2 ? decisions.absLevel[stateId] : 3 ); |
1066 | 72.3k | } |
1067 | | |
1068 | 1.49M | for( int i = 0; i < 16; i++ ) |
1069 | 1.40M | { |
1070 | 1.40M | curr.absVal[i][stateId] = prev.absVal[i][prevId]; |
1071 | 1.40M | } |
1072 | 87.9k | } |
1073 | 1.84k | else |
1074 | 1.84k | { |
1075 | 1.84k | curr.numSig[stateId] = 1; |
1076 | 1.84k | curr.refSbbCtxId[stateId] = -1; |
1077 | 1.84k | curr.remRegBins[stateId] = prev.initRemRegBins; |
1078 | 1.84k | curr.remRegBins[stateId] -= ( decisions.absLevel[stateId] < 2 ? decisions.absLevel[stateId] : 3 ); |
1079 | | |
1080 | 31.2k | for( int i = 0; i < 16; i++ ) |
1081 | 29.4k | { |
1082 | 29.4k | curr.absVal[i][stateId] = 0; |
1083 | 29.4k | } |
1084 | 1.84k | } |
1085 | | |
1086 | 138k | curr.absVal[scanInfo.insidePos][stateId] = ( uint8_t ) std::min<TCoeff>( 126 + ( decisions.absLevel[stateId] & 1 ), decisions.absLevel[stateId] ); |
1087 | | |
1088 | 138k | uint8_t* levels[4]; |
1089 | 138k | commonCtx.getLevelPtrs( scanInfo, levels[0], levels[1], levels[2], levels[3] ); |
1090 | 2.35M | for( int i = 0; i < 16; i++ ) |
1091 | 2.21M | { |
1092 | | // save abs levels to commonCtx |
1093 | 2.21M | levels[stateId][i] = curr.absVal[i][stateId]; |
1094 | | // clean the SBB ctx |
1095 | 2.21M | curr.tplAcc[i][stateId] = 0; |
1096 | 2.21M | curr.sum1st[i][stateId] = 0; |
1097 | 2.21M | curr.absVal[i][stateId] = 0; |
1098 | 2.21M | } |
1099 | | |
1100 | 138k | commonCtx.update( scanInfo, curr.refSbbCtxId[stateId], stateId, curr ); |
1101 | | |
1102 | 138k | curr.numSig[stateId] = 0; |
1103 | | |
1104 | 138k | if( curr.remRegBins[stateId] >= 4 ) |
1105 | 122k | { |
1106 | 122k | TCoeff sumAbs1 = curr.tplAcc[scanInfo.nextInsidePos][stateId] & 31; |
1107 | 122k | TCoeff sumNum = curr.tplAcc[scanInfo.nextInsidePos][stateId] >> 5u; |
1108 | 122k | int sumGt1 = sumAbs1 - sumNum; |
1109 | | |
1110 | 122k | curr.ctx.sig[stateId] = scanInfo.sigCtxOffsetNext + std::min( ( sumAbs1 + 1 ) >> 1, 3 ); |
1111 | 122k | curr.ctx.cff[stateId] = scanInfo.gtxCtxOffsetNext + std::min( sumGt1, 4 ); |
1112 | 122k | } |
1113 | 15.7k | else |
1114 | 15.7k | { |
1115 | 15.7k | curr.anyRemRegBinsLt4 = true; |
1116 | 15.7k | } |
1117 | 138k | } |
1118 | 139k | } |
1119 | | |
1120 | | static void updateStates( const DQIntern::ScanInfo& scanInfo, const DQIntern::Decisions& decisions, DQIntern::StateMem& curr ) |
1121 | 7.87M | { |
1122 | 7.87M | DQIntern::StateMem prev = curr; |
1123 | 7.87M | curr.anyRemRegBinsLt4 = false; |
1124 | | |
1125 | 7.87M | update1State( 0, scanInfo, decisions, curr, prev ); |
1126 | 7.87M | update1State( 1, scanInfo, decisions, curr, prev ); |
1127 | 7.87M | update1State( 2, scanInfo, decisions, curr, prev ); |
1128 | 7.87M | update1State( 3, scanInfo, decisions, curr, prev ); |
1129 | | |
1130 | 7.87M | curr.cffBitsCtxOffset = scanInfo.gtxCtxOffsetNext; |
1131 | 7.87M | } |
1132 | | |
1133 | | static void updateStatesEOS( const DQIntern::ScanInfo& scanInfo, const DQIntern::Decisions& decisions, const DQIntern::StateMem& skip, DQIntern::StateMem& curr, DQIntern::CommonCtx& commonCtx ) |
1134 | 34.9k | { |
1135 | 34.9k | DQIntern::StateMem prev = curr; |
1136 | 34.9k | curr.anyRemRegBinsLt4 = false; |
1137 | | |
1138 | 34.9k | update1StateEOS( 0, scanInfo, decisions, skip, curr, prev, commonCtx ); |
1139 | 34.9k | update1StateEOS( 1, scanInfo, decisions, skip, curr, prev, commonCtx ); |
1140 | 34.9k | update1StateEOS( 2, scanInfo, decisions, skip, curr, prev, commonCtx ); |
1141 | 34.9k | update1StateEOS( 3, scanInfo, decisions, skip, curr, prev, commonCtx ); |
1142 | | |
1143 | 34.9k | curr.cffBitsCtxOffset = scanInfo.gtxCtxOffsetNext; |
1144 | 34.9k | } |
1145 | | }; // namespace DQIntern |
1146 | | |
1147 | | // The per-coefficient kernels through DepQuant's function pointers: the scalar trellis, and every scaling-list trellis |
1148 | | struct DepQuant::PointerKernels |
1149 | | { |
1150 | | static void checkAllRdCosts( const DepQuant& dq, const DQIntern::ScanPosType spt, const DQIntern::PQData* pqData, DQIntern::Decisions& decisions, const DQIntern::StateMem& state ) |
1151 | 1.28M | { |
1152 | 1.28M | dq.m_checkAllRdCosts( spt, pqData, decisions, state ); |
1153 | 1.28M | } |
1154 | | static void checkAllRdCostsGe4( const DepQuant& dq, const DQIntern::ScanPosType spt, const DQIntern::PQData* pqData, DQIntern::Decisions& decisions, const DQIntern::StateMem& state ) |
1155 | 5.32M | { |
1156 | 5.32M | dq.m_checkAllRdCostsGe4( spt, pqData, decisions, state ); |
1157 | 5.32M | } |
1158 | | static void checkAllRdCostsOdd1( const DepQuant& dq, const DQIntern::ScanPosType spt, const int64_t pq_a_dist, const int64_t pq_b_dist, DQIntern::Decisions& decisions, const DQIntern::StateMem& state ) |
1159 | 1.15M | { |
1160 | 1.15M | dq.m_checkAllRdCostsOdd1( spt, pq_a_dist, pq_b_dist, decisions, state ); |
1161 | 1.15M | } |
1162 | | static void updateStates( const DepQuant& dq, const DQIntern::ScanInfo& scanInfo, const DQIntern::Decisions& decisions, DQIntern::StateMem& curr ) |
1163 | 7.87M | { |
1164 | 7.87M | dq.m_updateStates( scanInfo, decisions, curr ); |
1165 | 7.87M | } |
1166 | | static void updateStatesEOS( const DepQuant& dq, const DQIntern::ScanInfo& scanInfo, const DQIntern::Decisions& decisions, const DQIntern::StateMem& skip, DQIntern::StateMem& curr, DQIntern::CommonCtx& commonCtx ) |
1167 | 34.9k | { |
1168 | 34.9k | dq.m_updateStatesEOS( scanInfo, decisions, skip, curr, commonCtx ); |
1169 | 34.9k | } |
1170 | | }; |
1171 | | |
1172 | | void DepQuant::xQuantDQ( TransformUnit& tu, const CCoeffBuf& srcCoeff, const ComponentID compID, const QpParam& cQP, const double lambda, const Ctx& ctx, TCoeff& absSum, bool enableScalingLists, int* quantCoeff ) |
1173 | 2.25M | { |
1174 | 2.25M | using namespace DQIntern; |
1175 | | |
1176 | | //===== reset / pre-init ===== |
1177 | 2.25M | const TUParameters& tuPars = *m_scansRom->getTUPars( tu.blocks[compID], compID ); |
1178 | 2.25M | m_quant.initQuantBlock ( tu, compID, cQP, lambda, m_quantBlockMemo ); |
1179 | 2.25M | TCoeffSig* qCoeff = tu.getCoeffs( compID ).buf; |
1180 | 2.25M | const TCoeff* tCoeff = srcCoeff.buf; |
1181 | 2.25M | ::memset( qCoeff, 0x00, levelRowsToClear( tu.blocks[compID].height ) * tu.blocks[compID].width * sizeof( TCoeffSig ) ); |
1182 | 2.25M | absSum = 0; |
1183 | | |
1184 | 2.25M | const CompArea& area = tu.blocks[ compID ]; |
1185 | 2.25M | const uint32_t width = area.width; |
1186 | 2.25M | const uint32_t height = area.height; |
1187 | 2.25M | const uint32_t lfnstIdx = tu.cu->lfnstIdx; |
1188 | | //===== scaling matrix ==== |
1189 | | //const int qpDQ = cQP.Qp + 1; |
1190 | | //const int qpPer = qpDQ / 6; |
1191 | | //const int qpRem = qpDQ - 6 * qpPer; |
1192 | | |
1193 | | //TCoeff thresTmp = thres; |
1194 | 2.25M | bool zeroOut = false; |
1195 | 2.25M | bool zeroOutforThres = false; |
1196 | 2.25M | int effWidth = tuPars.m_width, effHeight = tuPars.m_height; |
1197 | 2.25M | if( ( tu.mtsIdx[compID] > MTS_SKIP || ( tu.cs->sps->MTS && tu.cu->sbtInfo != 0 && tuPars.m_height <= 32 && tuPars.m_width <= 32 ) ) && compID == COMP_Y ) |
1198 | 0 | { |
1199 | 0 | effHeight = ( tuPars.m_height == 32 ) ? 16 : tuPars.m_height; |
1200 | 0 | effWidth = ( tuPars.m_width == 32 ) ? 16 : tuPars.m_width; |
1201 | 0 | zeroOut = ( effHeight < tuPars.m_height || effWidth < tuPars.m_width ); |
1202 | 0 | } |
1203 | 2.25M | zeroOutforThres = zeroOut || ( 32 < tuPars.m_height || 32 < tuPars.m_width ); |
1204 | | //===== find first test position ===== |
1205 | 2.25M | int firstTestPos = std::min<int>( tuPars.m_width, JVET_C0024_ZERO_OUT_TH ) * std::min<int>( tuPars.m_height, JVET_C0024_ZERO_OUT_TH ) - 1; |
1206 | 2.25M | if( lfnstIdx > 0 && tu.mtsIdx[compID] != MTS_SKIP && width >= 4 && height >= 4 ) |
1207 | 1.40M | { |
1208 | 1.40M | firstTestPos = ( ( width == 4 && height == 4 ) || ( width == 8 && height == 8 ) ) ? 7 : 15 ; |
1209 | 1.40M | } |
1210 | | |
1211 | 2.25M | const TCoeff defaultQuantisationCoefficient = (TCoeff)m_quant.getQScale(); |
1212 | 2.25M | const TCoeff thres = m_quant.getLastThreshold(); |
1213 | 2.25M | const int zeroOutWidth = ( tuPars.m_width == 32 && zeroOut ) ? 16 : 32; |
1214 | 2.25M | const int zeroOutHeight = ( tuPars.m_height == 32 && zeroOut ) ? 16 : 32; |
1215 | | |
1216 | 2.25M | if( enableScalingLists ) |
1217 | 0 | { |
1218 | 0 | for( ; firstTestPos >= 0; firstTestPos-- ) |
1219 | 0 | { |
1220 | 0 | if( zeroOutforThres && ( tuPars.m_scanId2BlkPos[firstTestPos].x >= zeroOutWidth || tuPars.m_scanId2BlkPos[firstTestPos].y >= zeroOutHeight ) ) continue; |
1221 | | |
1222 | 0 | const TCoeff thresTmp = TCoeff( thres / ( 4 * quantCoeff[tuPars.m_scanId2BlkPos[firstTestPos].idx] ) ); |
1223 | |
|
1224 | 0 | if( abs( tCoeff[tuPars.m_scanId2BlkPos[firstTestPos].idx] ) > thresTmp ) break; |
1225 | 0 | } |
1226 | 0 | } |
1227 | 2.25M | else |
1228 | 2.25M | { |
1229 | 2.25M | xFindFirstPos( firstTestPos, tCoeff, tuPars.m_scanId2BlkPos, tuPars.m_width, tuPars.m_log2SbbWidth, tuPars.m_log2SbbHeight, |
1230 | 2.25M | m_quant.m_thresDefault, zeroOutforThres, zeroOutWidth, zeroOutHeight ); |
1231 | 2.25M | } |
1232 | | |
1233 | 2.25M | if( firstTestPos < 0 ) |
1234 | 1.31M | { |
1235 | 1.31M | tu.lastPos[compID] = -1; |
1236 | 1.31M | return; |
1237 | 1.31M | } |
1238 | | |
1239 | | //===== real init ===== |
1240 | 941k | if( RateEstimator::initCtx( tuPars, tu, compID, ctx.getFracBitsAcess(), m_rateTablesMemo ) && isChroma( compID ) ) |
1241 | 598k | { |
1242 | 598k | m_zeroProofRatesStale = true; // xProvesAllZero's rate minima come from the chroma tables |
1243 | 598k | } |
1244 | | |
1245 | | // Chroma in P and B slices: when no path can beat the all-zero TU, skip the trellis (qCoeff and absSum are already 0). |
1246 | | // Not in I slices, where the proof rarely succeeds and costs more than it saves. |
1247 | 941k | if( isChroma( compID ) && !enableScalingLists && !tu.cs->slice->isIntra() |
1248 | 0 | && xProvesAllZero( tuPars, tCoeff, firstTestPos, defaultQuantisationCoefficient ) ) |
1249 | 0 | { |
1250 | 0 | tu.lastPos[compID] = -1; |
1251 | 0 | return; |
1252 | 0 | } |
1253 | | |
1254 | 941k | m_commonCtx.reset( tuPars, *this ); |
1255 | 4.70M | for( int k = 0; k < 4; k++ ) |
1256 | 3.76M | { |
1257 | 3.76M | DQIntern::initStates( k, m_state_curr ); |
1258 | 3.76M | DQIntern::initStates( k, m_state_skip ); |
1259 | 3.76M | m_state_curr.m_sigFracBitsArray[k] = RateEstimator::sigFlagBits(k); |
1260 | 3.76M | } |
1261 | | |
1262 | 941k | m_state_curr.m_gtxFracBitsArray = RateEstimator::gtxFracBits(); |
1263 | | //memset( m_state_curr.tplAcc, 0, sizeof( m_state_curr.tplAcc ) ); // will be set in updateStates{,EOS} before first access |
1264 | 941k | memset( m_state_curr.sum1st, 0, sizeof( m_state_curr.sum1st ) ); // will be accessed in setRiceParam before updateState{,EOS} |
1265 | | //memset( m_state_curr.absVal, 0, sizeof( m_state_curr.absVal ) ); // will be set in updateStates{,EOS} before first access |
1266 | | |
1267 | 941k | const int numCtx = isLuma( compID ) ? 21 : 11; |
1268 | 941k | const CoeffFracBits* const cffBits = gtxFracBits(); |
1269 | 11.6M | for( int i = 0; i < numCtx; i++ ) |
1270 | 10.7M | { |
1271 | 10.7M | m_state_curr.cffBits1[i] = cffBits[i].bits[1]; |
1272 | 10.7M | } |
1273 | | |
1274 | 941k | int effectWidth = std::min( 32, effWidth ); |
1275 | 941k | int effectHeight = std::min( 32, effHeight ); |
1276 | 941k | m_state_curr.initRemRegBins = ( effectWidth * effectHeight * MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT ) / 16; |
1277 | 941k | m_state_curr.anyRemRegBinsLt4 = true; // for the first coeff use scalar impl., because it check against the init state, which |
1278 | | // prohibits some paths |
1279 | | |
1280 | | //===== populate trellis ===== |
1281 | 941k | m_firstScanIdx = firstTestPos; |
1282 | 941k | if( enableScalingLists ) |
1283 | 0 | { |
1284 | 0 | for( int scanIdx = firstTestPos; scanIdx >= 0; scanIdx-- ) |
1285 | 0 | { |
1286 | 0 | const ScanInfo& scanInfo = tuPars.m_scanInfo[ scanIdx ]; |
1287 | 0 | m_quant.initQuantBlock( tu, compID, cQP, lambda, quantCoeff[scanInfo.rasterPos] ); |
1288 | 0 | xDecideAndUpdate<PointerKernels>( abs( tCoeff[scanInfo.rasterPos] ), scanInfo, zeroOut && ( scanInfo.posX >= effWidth || scanInfo.posY >= effHeight ), quantCoeff[scanInfo.rasterPos] ); |
1289 | 0 | } |
1290 | 0 | } |
1291 | 941k | else |
1292 | 941k | { |
1293 | 941k | m_populateTrellis( *this, tuPars, firstTestPos, tCoeff, zeroOut, effWidth, effHeight, defaultQuantisationCoefficient ); |
1294 | 941k | } |
1295 | | |
1296 | | //===== find best path ===== |
1297 | 941k | int prevId = -1; |
1298 | 941k | int64_t minPathCost = 0; |
1299 | 4.70M | for( int stateId = 0; stateId < 4; stateId++ ) |
1300 | 3.76M | { |
1301 | | // No branch, as which state wins is close to random. It does the same as |
1302 | | // if( rdCost[stateId] < minPathCost ) { prevId = stateId; minPathCost = rdCost[stateId]; } |
1303 | 3.76M | const int64_t costDiff = m_trellis[0][0].rdCost[stateId] - minPathCost; |
1304 | 3.76M | const int64_t lower = costDiff >> 63; |
1305 | 3.76M | prevId += int( ( stateId - prevId ) & lower ); |
1306 | 3.76M | minPathCost += costDiff & lower; |
1307 | 3.76M | } |
1308 | | |
1309 | | //===== backward scanning ===== |
1310 | 941k | const Decisions* row = m_trellis[0]; |
1311 | 941k | TCoeff sum = 0; |
1312 | 941k | int scanIdx = 0; |
1313 | 9.86M | for( ; prevId >= 0; scanIdx++, row += 2 ) |
1314 | 8.92M | { |
1315 | | // Not row[prevId >> 2]: that multiplies by sizeof(Decisions) on the prevId load chain, and the walk runs 10-12 % slower. |
1316 | 8.92M | const Decisions& dec = prevId < 4 ? row[0] : row[1]; |
1317 | 8.92M | TCoeffSig absLevel = dec.absLevel[prevId & 3]; |
1318 | 8.92M | int32_t blkpos = tuPars.m_scanId2BlkPos[scanIdx].idx; |
1319 | | // apply the coefficient's sign without a branch: (absLevel ^ m) - m is -absLevel for m = -1 and absLevel for m = 0 |
1320 | 8.92M | const int signMask = -int( tCoeff[blkpos] < 0 ); |
1321 | 8.92M | qCoeff[ blkpos ] = TCoeffSig( ( absLevel ^ signMask ) - signMask ); |
1322 | 8.92M | sum += absLevel; |
1323 | 8.92M | prevId = dec.prevId[prevId & 3]; |
1324 | 8.92M | } |
1325 | 941k | absSum = sum; |
1326 | | |
1327 | 941k | tu.lastPos[compID] = scanIdx - 1; |
1328 | 941k | } |
1329 | | |
1330 | | void DepQuant::xDequantDQ( const TransformUnit& tu, CoeffBuf& recCoeff, const ComponentID compID, const QpParam& cQP, bool enableScalingLists, int* piDequantCoef ) |
1331 | 925k | { |
1332 | 925k | m_quant.dequantBlock( tu, compID, cQP, recCoeff, enableScalingLists, piDequantCoef, m_dequantCoeffs ); |
1333 | 925k | } |
1334 | | |
1335 | | // Whether no trellis path can cost less than the all-zero TU (cost 0): then the trellis returns all zero, since it keeps |
1336 | | // a nonzero level only for a path cost strictly below 0 (the best-path search starts at minPathCost 0, the test is <). |
1337 | | // For a chroma TU without scaling lists, with the rate tables initCtx loaded. A path is fixed by its start (its last |
1338 | | // nonzero position p) and the levels below p; its cost, in the trellis's own integers, is at least |
1339 | | // minDelta( p ) + lastOffset( p ) + lastLevel the start (checkRdCostStart) |
1340 | | // + the sum of reg( i ) over the positions i below p in p's sub-block (its sb_coded_flag is inferred) |
1341 | | // + the sum over the sub-blocks below of min( sbb0, sbb1 + the sum of their positions' terms ) (sub-block 0: no flag) |
1342 | | // minDelta( i ): the smallest deltaDist among xDecide's candidate levels at i; reg( i ) = min( sig0, minDelta( i ) + sig1 |
1343 | | // + level ) where the sig flag is coded; at a coded sub-block's first position (insidePos 0) it may be inferred, so the |
1344 | | // term there leaves out sig1. The rate minima come from ZeroProofRates; all other rates (Rice and remainder bits) are >= 0. |
1345 | | // Bypass guard: remRegBins starts at initRemRegBins; the start spends at most 3 bins and every later position at most 4, |
1346 | | // so the j-th position below the start still has >= 4 bins for j <= jmax. Further down, a position may code no sig / |
1347 | | // sb_coded_flag / gtx bins: a zero there costs >= 0, a nonzero >= minDelta + 1 bit (the sign), and its sub-block's term |
1348 | | // becomes min( sbb0, the sum of those terms ). |
1349 | | // The start costs are evaluated upward from DC, each from the sums of the positions below it, so the first negative one |
1350 | | // ends the proof at once (TUs that are not all zero mostly end it near DC). The loop is scalar; the early |
1351 | | // exit would have to go if it were ever vectorised. |
1352 | | bool DepQuant::xProvesAllZero( const DQIntern::TUParameters& tuPars, const TCoeff* tCoeff, const int firstTestPos, const TCoeff quantCoeff ) |
1353 | 0 | { |
1354 | 0 | using namespace DQIntern; |
1355 | | |
1356 | | // from the chroma tables initCtx just built or restored: refreshed here, not in xQuantDQ, once they were built again |
1357 | 0 | if( m_zeroProofRatesStale ) |
1358 | 0 | { |
1359 | 0 | xSetZeroProofRates( m_zeroProofRates ); |
1360 | 0 | m_zeroProofRatesStale = false; |
1361 | 0 | } |
1362 | 0 | const ZeroProofRates& rate = m_zeroProofRates; |
1363 | 0 | const int64_t signBit = int64_t( 1 ) << SCALE_BITS; |
1364 | 0 | const int log2Sbb = tuPars.m_log2SbbSize; |
1365 | 0 | const int sbbSize = 1 << log2Sbb; |
1366 | | // the trellis's initRemRegBins (xQuantDQ; chroma has no zero-out) |
1367 | 0 | const int initBins = ( std::min<int>( 32, tuPars.m_width ) * std::min<int>( 32, tuPars.m_height ) * MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT ) / 16; |
1368 | 0 | const int jmax = initBins >= 7 ? ( initBins - 7 ) / 4 + 1 : 0; |
1369 | | |
1370 | | // the sums of the sub-block terms below sub-block u (index u): with every position regular, or with bypass allowed |
1371 | | // (at most 32 x 32 positions are scanned, in sub-blocks of at least 4); each entry is written before it is read |
1372 | 0 | int64_t sbbSumReg [MAX_TB_SIZEY * MAX_TB_SIZEY / 16 + 1]; |
1373 | 0 | int64_t sbbSumSafe[MAX_TB_SIZEY * MAX_TB_SIZEY / 16 + 1]; |
1374 | | // in the current sub-block, the sums of the terms of its positions below insidePos k (index k); MLS_CG_SIZE is the |
1375 | | // log2 of the largest sub-block's number of positions |
1376 | 0 | CHECKD( sbbSize > ( 1 << MLS_CG_SIZE ), "sub-block larger than the position sums hold" ); |
1377 | 0 | int64_t posSumReg [( 1 << MLS_CG_SIZE ) + 1]; |
1378 | 0 | int64_t posSumSafe[( 1 << MLS_CG_SIZE ) + 1]; |
1379 | 0 | sbbSumReg[0] = sbbSumSafe[0] = posSumReg[0] = posSumSafe[0] = 0; |
1380 | 0 | int64_t sbbReg = 0, sbbSafe = 0; // the current sub-block's own sums so far |
1381 | |
|
1382 | 0 | for( int i = 0; i <= firstTestPos; i++ ) |
1383 | 0 | { |
1384 | 0 | const ScanInfo& scanInfo = tuPars.m_scanInfo[i]; |
1385 | 0 | const int u = i >> log2Sbb; |
1386 | 0 | const int k = scanInfo.insidePos; |
1387 | 0 | CHECKD( k != i - ( u << log2Sbb ), "a sub-block's positions must be consecutive in the scan" ); |
1388 | | |
1389 | | // the smallest deltaDist of xDecide's candidate levels, with its integers |
1390 | 0 | const int64_t scaledOrg = int64_t( abs( tCoeff[scanInfo.rasterPos] ) ) * quantCoeff; |
1391 | 0 | TCoeff qIdx = TCoeff( ( scaledOrg + m_quant.m_QAdd ) >> m_quant.m_QShift ); |
1392 | 0 | int64_t minDelta; |
1393 | 0 | if( qIdx < 0 ) |
1394 | 0 | { |
1395 | 0 | const int64_t scaledAdd = m_quant.m_DistStepAdd - scaledOrg * m_quant.m_DistOrgFact; |
1396 | 0 | minDelta = std::min( ( scaledAdd + m_quant.m_DistAdd ) >> m_quant.m_DistShift, |
1397 | 0 | ( ( scaledAdd + m_quant.m_DistStepAdd ) * 2 + m_quant.m_DistAdd ) >> m_quant.m_DistShift ); |
1398 | 0 | } |
1399 | 0 | else |
1400 | 0 | { |
1401 | 0 | qIdx = std::max<TCoeff>( 1, std::min<TCoeff>( m_quant.m_maxQIdx, qIdx ) ); |
1402 | 0 | const int64_t scaledAdd = qIdx * m_quant.m_DistStepAdd - scaledOrg * m_quant.m_DistOrgFact; |
1403 | 0 | minDelta = std::numeric_limits<int64_t>::max(); |
1404 | 0 | for( int j = 0; j < 4; j++ ) |
1405 | 0 | { |
1406 | 0 | minDelta = std::min( minDelta, ( ( scaledAdd + j * m_quant.m_DistStepAdd ) * ( qIdx + j ) + m_quant.m_DistAdd ) >> m_quant.m_DistShift ); |
1407 | 0 | } |
1408 | 0 | } |
1409 | | |
1410 | | // the least cost of a path starting here; positions >= i - jmax are surely regular |
1411 | 0 | const int q = i - jmax; |
1412 | 0 | const int kq = std::min( std::max( q - ( u << log2Sbb ), 0 ), k ); |
1413 | 0 | const int ur = std::min( q <= 0 ? 0 : ( q + sbbSize - 1 ) >> log2Sbb, u ); |
1414 | 0 | const int64_t startCost = minDelta + lastOffset( scanInfo ) + rate.lastLevel + ( posSumReg[k] - posSumReg[kq] ) + posSumSafe[kq] |
1415 | 0 | + ( sbbSumReg[u] - sbbSumReg[ur] ) + sbbSumSafe[ur]; |
1416 | 0 | if( startCost < 0 ) |
1417 | 0 | { |
1418 | 0 | return false; |
1419 | 0 | } |
1420 | | |
1421 | | // the position's terms for the starts above it |
1422 | 0 | const int diag = scanInfo.posX + scanInfo.posY; |
1423 | 0 | const int d = diag < 2 ? 0 : 1; |
1424 | 0 | const int dl = diag < 1 ? 0 : 1; |
1425 | 0 | const int64_t bypass = std::min<int64_t>( 0, minDelta + signBit ); |
1426 | 0 | const int64_t reg = std::min<int64_t>( rate.sig0[d], minDelta + rate.sig1[d] + rate.level[dl] ); |
1427 | 0 | const int64_t safe = std::min( reg, bypass ); |
1428 | 0 | posSumReg [k + 1] = posSumReg [k] + reg; |
1429 | 0 | posSumSafe[k + 1] = posSumSafe[k] + safe; |
1430 | 0 | if( u > 0 && k == 0 ) |
1431 | 0 | { |
1432 | 0 | const int64_t regInferable = std::min<int64_t>( rate.sig0[d], minDelta + rate.level[dl] ); |
1433 | 0 | sbbReg += regInferable; |
1434 | 0 | sbbSafe += std::min( regInferable, bypass ); |
1435 | 0 | } |
1436 | 0 | else |
1437 | 0 | { |
1438 | 0 | sbbReg += reg; |
1439 | 0 | sbbSafe += safe; |
1440 | 0 | } |
1441 | 0 | if( k == sbbSize - 1 ) |
1442 | 0 | { |
1443 | 0 | sbbSumReg [u + 1] = sbbSumReg [u] + ( u == 0 ? sbbReg : std::min<int64_t>( rate.sbb0, rate.sbb1 + sbbReg ) ); |
1444 | 0 | sbbSumSafe[u + 1] = sbbSumSafe[u] + ( u == 0 ? sbbSafe : std::min<int64_t>( rate.sbb0, sbbSafe ) ); |
1445 | 0 | sbbReg = sbbSafe = 0; |
1446 | 0 | } |
1447 | 0 | } |
1448 | 0 | return true; |
1449 | 0 | } |
1450 | | |
1451 | 5.72k | DepQuant::DepQuant( const Quant* other, bool enc, bool useScalingLists, bool enableOpt ) : QuantRDOQ2( other, useScalingLists, enableOpt ), RateEstimator(), m_commonCtx() |
1452 | 5.72k | { |
1453 | 5.72k | const DepQuant* dq = dynamic_cast<const DepQuant*>( other ); |
1454 | 5.72k | CHECK( other && !dq, "The DepQuant cast must be successfull!" ); |
1455 | | |
1456 | 5.72k | if( !dq ) |
1457 | 1.43k | { |
1458 | 1.43k | m_scansRom = std::make_shared<DQIntern::Rom>(); |
1459 | 1.43k | m_scansRom->init(); |
1460 | 1.43k | } |
1461 | 4.29k | else |
1462 | 4.29k | { |
1463 | 4.29k | m_scansRom = dq->m_scansRom; |
1464 | 4.29k | } |
1465 | | |
1466 | 23.4M | for( int t = 0; t < ( MAX_TB_SIZEY * MAX_TB_SIZEY ); t++ ) |
1467 | 23.4M | { |
1468 | 23.4M | memcpy( m_trellis[t], DQIntern::startDec, sizeof( DQIntern::startDec ) ); |
1469 | 23.4M | } |
1470 | | |
1471 | 5.72k | m_checkAllRdCosts = DQIntern::checkAllRdCosts; |
1472 | 5.72k | m_checkAllRdCostsGe4 = DQIntern::checkAllRdCosts; // the scalar checks add the Rice code's bits themselves |
1473 | 5.72k | m_checkAllRdCostsOdd1 = DQIntern::checkAllRdCostsOdd1; |
1474 | 5.72k | m_updateStatesEOS = DQIntern::updateStatesEOS; |
1475 | 5.72k | m_dequantCoeffs = DQIntern::dequantCoeffs; |
1476 | 5.72k | m_updateStates = DQIntern::updateStates; |
1477 | 5.72k | m_populateTrellis = xPopulateTrellis<PointerKernels>; |
1478 | | |
1479 | 5.72k | if( enableOpt ) |
1480 | 5.72k | { |
1481 | | #if defined( TARGET_SIMD_X86 ) && ENABLE_SIMD_OPT_QUANT |
1482 | | initDepQuantX86(); |
1483 | | #endif |
1484 | | #if defined( TARGET_SIMD_ARM ) && ENABLE_SIMD_OPT_QUANT |
1485 | | initDepQuantARM(); |
1486 | | #endif |
1487 | 5.72k | } |
1488 | 5.72k | } |
1489 | | |
1490 | | DepQuant::~DepQuant() |
1491 | 5.72k | { |
1492 | 5.72k | } |
1493 | | |
1494 | | void DepQuant::quant( TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, TCoeff& uiAbsSum, const QpParam& cQP, const Ctx& ctx ) |
1495 | 2.37M | { |
1496 | 2.37M | if( tu.cs->picture->useSelectiveRdoq && !xNeedRDOQ( tu, compID, pSrc, cQP ) ) |
1497 | 0 | { |
1498 | 0 | tu.lastPos[compID] = -1; |
1499 | 0 | uiAbsSum = 0; |
1500 | 0 | } |
1501 | 2.37M | else if( tu.cs->slice->depQuantEnabled && tu.mtsIdx[compID] != MTS_SKIP ) |
1502 | 2.25M | { |
1503 | | //===== scaling matrix ==== |
1504 | | // without scaling lists there is no table to find, and this runs for every transform block |
1505 | 2.25M | bool enableScalingLists = false; |
1506 | 2.25M | int* quantCoeff = nullptr; |
1507 | 2.25M | if( getScalingListEnabled() ) |
1508 | 0 | { |
1509 | 0 | const int qpDQ = cQP.Qp(tu.mtsIdx[compID]==MTS_SKIP) + 1; |
1510 | 0 | const int qpPer = qpDQ / 6; |
1511 | 0 | const int qpRem = qpDQ - 6 * qpPer; |
1512 | 0 | const CompArea &rect = tu.blocks[compID]; |
1513 | 0 | const int width = rect.width; |
1514 | 0 | const int height = rect.height; |
1515 | 0 | uint32_t scalingListType = getScalingListType(tu.cu->predMode, compID); |
1516 | 0 | CHECK(scalingListType >= SCALING_LIST_NUM, "Invalid scaling list"); |
1517 | 0 | const uint32_t log2TrWidth = Log2(width); |
1518 | 0 | const uint32_t log2TrHeight = Log2(height); |
1519 | 0 | const bool isLfnstApplied = tu.cu->lfnstIdx > 0 && (CU::isSepTree(*tu.cu) ? true : isLuma(compID)); |
1520 | 0 | enableScalingLists = getUseScalingList(width, height, (tu.mtsIdx[compID] == MTS_SKIP), isLfnstApplied); |
1521 | 0 | quantCoeff = Quant::getQuantCoeff(scalingListType, qpRem, log2TrWidth, log2TrHeight); |
1522 | 0 | } |
1523 | 2.25M | xQuantDQ( tu, pSrc, compID, cQP, Quant::m_dLambda, ctx, uiAbsSum, enableScalingLists, quantCoeff ); |
1524 | 2.25M | } |
1525 | 120k | else |
1526 | 120k | { |
1527 | 120k | QuantRDOQ2::quant( tu, compID, pSrc, uiAbsSum, cQP, ctx ); |
1528 | 120k | } |
1529 | 2.37M | } |
1530 | | |
1531 | | void DepQuant::dequant( const TransformUnit& tu, CoeffBuf& dstCoeff, const ComponentID compID, const QpParam& cQP ) |
1532 | 982k | { |
1533 | 982k | if( tu.cs->slice->depQuantEnabled && (tu.mtsIdx[compID] != MTS_SKIP) ) |
1534 | 925k | { |
1535 | | // without scaling lists there is no table to find, and this runs for every transform block |
1536 | 925k | bool enableScalingLists = false; |
1537 | 925k | int* dequantCoeff = nullptr; |
1538 | 925k | if( getScalingListEnabled() ) |
1539 | 0 | { |
1540 | 0 | const int qpDQ = cQP.Qp(tu.mtsIdx[compID]==MTS_SKIP) + 1; |
1541 | 0 | const int qpPer = qpDQ / 6; |
1542 | 0 | const int qpRem = qpDQ - 6 * qpPer; |
1543 | 0 | const CompArea &rect = tu.blocks[compID]; |
1544 | 0 | const int width = rect.width; |
1545 | 0 | const int height = rect.height; |
1546 | 0 | uint32_t scalingListType = getScalingListType(tu.cu->predMode, compID); |
1547 | 0 | CHECK(scalingListType >= SCALING_LIST_NUM, "Invalid scaling list"); |
1548 | 0 | const uint32_t log2TrWidth = Log2(width); |
1549 | 0 | const uint32_t log2TrHeight = Log2(height); |
1550 | 0 | const bool isLfnstApplied = tu.cu->lfnstIdx > 0 && (CU::isSepTree(*tu.cu) ? true : isLuma(compID)); |
1551 | 0 | enableScalingLists = getUseScalingList(width, height, (tu.mtsIdx[compID] == MTS_SKIP), isLfnstApplied); |
1552 | 0 | dequantCoeff = Quant::getDequantCoeff(scalingListType, qpRem, log2TrWidth, log2TrHeight); |
1553 | 0 | } |
1554 | 925k | xDequantDQ( tu, dstCoeff, compID, cQP, enableScalingLists, dequantCoeff ); |
1555 | 925k | } |
1556 | 56.9k | else |
1557 | 56.9k | { |
1558 | 56.9k | QuantRDOQ::dequant( tu, dstCoeff, compID, cQP ); |
1559 | 56.9k | } |
1560 | 982k | } |
1561 | | |
1562 | | void DepQuant::init( int rdoq, bool useRDOQTS, int thrVal, int sbbSkipThrVal ) |
1563 | 5.72k | { |
1564 | 5.72k | QuantRDOQ2::init( rdoq, useRDOQTS, thrVal, sbbSkipThrVal ); |
1565 | 5.72k | m_quant.init( thrVal, sbbSkipThrVal ); |
1566 | 5.72k | m_quantBlockMemo = DQIntern::QuantBlockMemo(); // the threshold value is part of what it keeps |
1567 | 5.72k | } |
1568 | | |
1569 | | } // namespace vvenc |
1570 | | |
1571 | | //! \} |
1572 | | |