/src/vvdec/source/Lib/DecoderLib/CABACReader.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) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC 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 | | /** \file CABACReader.cpp |
44 | | * \brief Reader for low level syntax |
45 | | */ |
46 | | |
47 | | #include "CABACReader.h" |
48 | | |
49 | | #include "CommonLib/CodingStructure.h" |
50 | | #include "CommonLib/TrQuant.h" |
51 | | #include "CommonLib/UnitTools.h" |
52 | | #include "CommonLib/SampleAdaptiveOffset.h" |
53 | | #include "CommonLib/dtrace_next.h" |
54 | | #include "CommonLib/Picture.h" |
55 | | #include "CommonLib/TimeProfiler.h" |
56 | | |
57 | | namespace vvdec |
58 | | { |
59 | | |
60 | | void CABACReader::initCtxModels( Slice& slice ) |
61 | 0 | { |
62 | 0 | SliceType sliceType = slice.getSliceType(); |
63 | 0 | int qp = slice.getSliceQp(); |
64 | 0 | if( slice.getPPS()->getCabacInitPresentFlag() && slice.getCabacInitFlag() ) |
65 | 0 | { |
66 | 0 | switch( sliceType ) |
67 | 0 | { |
68 | 0 | case P_SLICE: // change initialization table to B_SLICE initialization |
69 | 0 | sliceType = B_SLICE; |
70 | 0 | break; |
71 | 0 | case B_SLICE: // change initialization table to P_SLICE initialization |
72 | 0 | sliceType = P_SLICE; |
73 | 0 | break; |
74 | 0 | default : // should not occur |
75 | 0 | THROW_RECOVERABLE( "Invalid slice type" ); |
76 | 0 | break; |
77 | 0 | } |
78 | 0 | } |
79 | 0 | m_BinDecoder.reset( qp, (int)sliceType ); |
80 | 0 | } |
81 | | |
82 | | |
83 | | //================================================================================ |
84 | | // clause 7.3.8.1 |
85 | | //-------------------------------------------------------------------------------- |
86 | | // bool terminating_bit() |
87 | | // void remaining_bytes( noTrailingBytesExpected ) |
88 | | //================================================================================ |
89 | | |
90 | | bool CABACReader::terminating_bit() |
91 | 0 | { |
92 | 0 | if( m_BinDecoder.decodeBinTrm() ) |
93 | 0 | { |
94 | 0 | m_BinDecoder.finish(); |
95 | 0 | m_Bitstream->readOutTrailingBits(); |
96 | 0 | return true; |
97 | 0 | } |
98 | 0 | return false; |
99 | 0 | } |
100 | | |
101 | | void CABACReader::remaining_bytes( bool noTrailingBytesExpected ) |
102 | 0 | { |
103 | 0 | if( noTrailingBytesExpected ) |
104 | 0 | { |
105 | | // CHECK( 0 != m_Bitstream->getNumBitsLeft(), "Bits left when not supposed" ); |
106 | 0 | } |
107 | 0 | else |
108 | 0 | { |
109 | 0 | while( m_Bitstream->getNumBitsLeft() ) |
110 | 0 | { |
111 | 0 | unsigned trailingNullByte = m_Bitstream->readByte(); |
112 | 0 | CHECK( trailingNullByte != 0, "Trailing byte should be '0', but has a value of " << std::hex << trailingNullByte << std::dec << "\n" ); |
113 | 0 | } |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | |
118 | | |
119 | | |
120 | | |
121 | | //================================================================================ |
122 | | // clause 7.3.11.2 |
123 | | //-------------------------------------------------------------------------------- |
124 | | // bool coding_tree_unit ( cs, slice, area, qps[2], ctuRsAddr ) |
125 | | // bool dt_implicit_qt_split( cs, pL, cuCtxL, pC, cuCtxC ) |
126 | | //================================================================================ |
127 | | |
128 | | bool CABACReader::coding_tree_unit( CodingStructure& cs, Slice* slice, const UnitArea& area, int (&qps)[2], unsigned ctuRsAddr ) |
129 | 0 | { |
130 | 0 | m_slice = slice; |
131 | |
|
132 | 0 | CUCtx cuCtx( qps[CH_L] ); |
133 | 0 | Partitioner &partitioner = m_partL; |
134 | |
|
135 | 0 | partitioner.initCtu( area, CH_L, cs, *m_slice ); |
136 | 0 | partitioner.treeType = TREE_D; |
137 | 0 | partitioner.modeType = MODE_TYPE_ALL; |
138 | |
|
139 | 0 | sao( cs, ctuRsAddr ); |
140 | |
|
141 | 0 | readAlf(cs, ctuRsAddr, partitioner); |
142 | |
|
143 | 0 | bool isLast = false; |
144 | |
|
145 | 0 | if( partitioner.isDualITree && cs.pcv->chrFormat != CHROMA_400 ) |
146 | 0 | { |
147 | 0 | CUCtx cuCtxC( qps[CH_C] ); |
148 | 0 | Partitioner &partitionerC = m_partC; |
149 | |
|
150 | 0 | partitionerC.initCtu( area, CH_C, cs, *m_slice ); |
151 | 0 | partitionerC.treeType = TREE_D; |
152 | 0 | partitionerC.modeType = MODE_TYPE_ALL; |
153 | |
|
154 | 0 | dt_implicit_qt_split( cs, partitioner, cuCtx, partitionerC, cuCtxC ); |
155 | |
|
156 | 0 | qps[CH_L] = cuCtx.qp; |
157 | 0 | qps[CH_C] = cuCtxC.qp; |
158 | 0 | } |
159 | 0 | else |
160 | 0 | { |
161 | 0 | isLast = coding_tree( cs, partitioner, cuCtx ); |
162 | 0 | qps[CH_L] = cuCtx.qp; |
163 | 0 | } |
164 | |
|
165 | 0 | DTRACE_COND( ctuRsAddr == 0, g_trace_ctx, D_QP_PER_CTU, "\n%4d %2d", cs.picture->poc, m_slice->getSliceQp() ); |
166 | 0 | DTRACE ( g_trace_ctx, D_QP_PER_CTU, " %3d", qps[CH_L] - m_slice->getSliceQp() ); |
167 | |
|
168 | 0 | return isLast; |
169 | 0 | } |
170 | | |
171 | | |
172 | | bool CABACReader::dt_implicit_qt_split( CodingStructure& cs, Partitioner& partitionerL, CUCtx& cuCtxL, Partitioner& partitionerC, CUCtx& cuCtxC ) |
173 | 0 | { |
174 | 0 | if( partitionerL.currArea().lwidth() > 64 ) |
175 | 0 | { |
176 | 0 | const PPS& pps = *cs.pps; |
177 | | // Reset delta QP coding flag and ChromaQPAdjustemt coding flag |
178 | 0 | if( pps.getUseDQP() && partitionerL.currQgEnable() ) |
179 | 0 | { |
180 | 0 | cuCtxL.qgStart = true; |
181 | 0 | cuCtxL.isDQPCoded = false; |
182 | | |
183 | 0 | cuCtxC.qgStart = true; |
184 | 0 | cuCtxC.isDQPCoded = false; |
185 | 0 | } |
186 | |
|
187 | 0 | if( m_slice->getUseChromaQpAdj() && partitionerL.currQgChromaEnable() ) |
188 | 0 | { |
189 | 0 | cuCtxL.isChromaQpAdjCoded = false; |
190 | 0 | cuCtxC.isChromaQpAdjCoded = false; |
191 | 0 | cs .chromaQpAdj = 0; |
192 | 0 | } |
193 | |
|
194 | 0 | partitionerL.splitCurrArea( CU_QUAD_SPLIT, cs ); |
195 | 0 | partitionerC.splitCurrArea( CU_QUAD_SPLIT, cs ); |
196 | |
|
197 | 0 | bool lastSegment = false; |
198 | |
|
199 | 0 | do |
200 | 0 | { |
201 | 0 | if( !lastSegment && cs.area.blocks[partitionerL.chType].contains( partitionerL.currArea().blocks[partitionerL.chType].pos() ) ) |
202 | 0 | { |
203 | 0 | lastSegment = dt_implicit_qt_split( cs, partitionerL, cuCtxL, partitionerC, cuCtxC ); |
204 | 0 | } |
205 | 0 | } while( partitionerL.nextPart( cs ) && partitionerC.nextPart( cs ) ); |
206 | |
|
207 | 0 | return lastSegment; |
208 | 0 | } |
209 | | |
210 | 0 | bool isLast = coding_tree( cs, partitionerL, cuCtxL ); |
211 | 0 | CHECKD( isLast, "Chroma not parsed but end of slice signalled!" ); |
212 | 0 | isLast = coding_tree( cs, partitionerC, cuCtxC ); |
213 | |
|
214 | 0 | return isLast; |
215 | 0 | } |
216 | | |
217 | | short CABACReader::readAlfCtuFilterIndex( CodingStructure& cs, unsigned ctuRsAddr ) |
218 | 0 | { |
219 | 0 | const unsigned numAps = m_slice->getNumAlfAps(); |
220 | 0 | const unsigned numAvailableFiltSets = numAps + NUM_FIXED_FILTER_SETS; |
221 | 0 | uint32_t filtIndex = 0; |
222 | |
|
223 | 0 | const bool usePrevFilt = numAps > 0 |
224 | 0 | && m_BinDecoder.decodeBin( Ctx::AlfUseTemporalFilt() ); |
225 | |
|
226 | 0 | if( usePrevFilt ) |
227 | 0 | { |
228 | 0 | if( numAps > 1 ) |
229 | 0 | { |
230 | 0 | xReadTruncBinCode( filtIndex, numAvailableFiltSets - NUM_FIXED_FILTER_SETS ); |
231 | 0 | } |
232 | |
|
233 | 0 | filtIndex += ( unsigned ) ( NUM_FIXED_FILTER_SETS ); |
234 | 0 | } |
235 | 0 | else |
236 | 0 | { |
237 | 0 | xReadTruncBinCode( filtIndex, NUM_FIXED_FILTER_SETS ); |
238 | 0 | } |
239 | |
|
240 | 0 | return filtIndex; |
241 | 0 | } |
242 | | |
243 | | //================================================================================ |
244 | | // clause 7.3.11.3 |
245 | | //-------------------------------------------------------------------------------- |
246 | | // void sao( cs, ctuRsAddr ) |
247 | | //================================================================================ |
248 | | |
249 | | void CABACReader::sao( CodingStructure& cs, unsigned ctuRsAddr ) |
250 | 0 | { |
251 | |
|
252 | 0 | SAOBlkParam& sao_ctu_pars = cs.getCtuData( ctuRsAddr ).saoParam; |
253 | 0 | sao_ctu_pars[ COMPONENT_Y ].modeIdc = SAO_MODE_OFF; |
254 | 0 | sao_ctu_pars[ COMPONENT_Cb ].modeIdc = SAO_MODE_OFF; |
255 | 0 | sao_ctu_pars[ COMPONENT_Cr ].modeIdc = SAO_MODE_OFF; |
256 | |
|
257 | 0 | const SPS& sps = *cs.sps; |
258 | 0 | const Slice& slice = *m_slice; |
259 | 0 | const bool slice_sao_luma_flag = ( slice.getSaoEnabledFlag( CHANNEL_TYPE_LUMA ) ); |
260 | 0 | const bool slice_sao_chroma_flag = ( slice.getSaoEnabledFlag( CHANNEL_TYPE_CHROMA ) && sps.getChromaFormatIdc() != CHROMA_400 ); |
261 | |
|
262 | 0 | if( !slice_sao_luma_flag && !slice_sao_chroma_flag ) |
263 | 0 | { |
264 | 0 | return; |
265 | 0 | } |
266 | | |
267 | | // merge |
268 | 0 | int frame_width_in_ctus = cs.pcv->widthInCtus; |
269 | 0 | int ry = ctuRsAddr / frame_width_in_ctus; |
270 | 0 | int rx = ctuRsAddr - ry * frame_width_in_ctus; |
271 | 0 | int sao_merge_type = -1; |
272 | 0 | const Position pos( rx * cs.pcv->maxCUWidth, ry * cs.pcv->maxCUHeight ); |
273 | 0 | const unsigned curSliceIdx = m_slice->getIndependentSliceIdx(); |
274 | 0 | const unsigned curTileIdx = cs.pps->getTileIdx( pos ); |
275 | |
|
276 | 0 | if( cs.getCURestricted( pos.offset(-(int)cs.pcv->maxCUWidth, 0), pos, curSliceIdx, curTileIdx, CH_L ) ) |
277 | 0 | { |
278 | | // sao_merge_left_flag |
279 | 0 | sao_merge_type += int( m_BinDecoder.decodeBin( Ctx::SaoMergeFlag() ) ); |
280 | 0 | } |
281 | |
|
282 | 0 | if( sao_merge_type < 0 && cs.getCURestricted( pos.offset(0, -(int)cs.pcv->maxCUHeight), pos, curSliceIdx, curTileIdx, CH_L ) ) |
283 | 0 | { |
284 | | // sao_merge_above_flag |
285 | 0 | sao_merge_type += int( m_BinDecoder.decodeBin( Ctx::SaoMergeFlag() ) ) << 1; |
286 | 0 | } |
287 | 0 | if( sao_merge_type >= 0 ) |
288 | 0 | { |
289 | 0 | if( slice_sao_luma_flag || slice_sao_chroma_flag ) |
290 | 0 | { |
291 | 0 | sao_ctu_pars[ COMPONENT_Y ].modeIdc = SAO_MODE_MERGE; |
292 | 0 | sao_ctu_pars[ COMPONENT_Y ].typeIdc = sao_merge_type; |
293 | 0 | } |
294 | 0 | if( slice_sao_chroma_flag ) |
295 | 0 | { |
296 | 0 | sao_ctu_pars[ COMPONENT_Cb ].modeIdc = SAO_MODE_MERGE; |
297 | 0 | sao_ctu_pars[ COMPONENT_Cr ].modeIdc = SAO_MODE_MERGE; |
298 | 0 | sao_ctu_pars[ COMPONENT_Cb ].typeIdc = sao_merge_type; |
299 | 0 | sao_ctu_pars[ COMPONENT_Cr ].typeIdc = sao_merge_type; |
300 | 0 | } |
301 | 0 | return; |
302 | 0 | } |
303 | | |
304 | | // explicit parameters |
305 | 0 | const ComponentID firstComp = ( slice_sao_luma_flag ? COMPONENT_Y : COMPONENT_Cb ); |
306 | 0 | const ComponentID lastComp = ( slice_sao_chroma_flag ? COMPONENT_Cr : COMPONENT_Y ); |
307 | |
|
308 | 0 | for( ComponentID compID = firstComp; compID <= lastComp; compID = ComponentID( compID + 1 ) ) |
309 | 0 | { |
310 | 0 | SAOOffset& sao_pars = sao_ctu_pars[ compID ]; |
311 | | |
312 | | // sao_type_idx_luma / sao_type_idx_chroma |
313 | 0 | if( compID != COMPONENT_Cr ) |
314 | 0 | { |
315 | 0 | if( m_BinDecoder.decodeBin( Ctx::SaoTypeIdx() ) ) |
316 | 0 | { |
317 | 0 | if( m_BinDecoder.decodeBinEP( ) ) |
318 | 0 | { |
319 | | // edge offset |
320 | 0 | sao_pars.modeIdc = SAO_MODE_NEW; |
321 | 0 | sao_pars.typeIdc = SAO_TYPE_START_EO; |
322 | 0 | } |
323 | 0 | else |
324 | 0 | { |
325 | | // band offset |
326 | 0 | sao_pars.modeIdc = SAO_MODE_NEW; |
327 | 0 | sao_pars.typeIdc = SAO_TYPE_START_BO; |
328 | 0 | } |
329 | 0 | } |
330 | 0 | } |
331 | 0 | else //Cr, follow Cb SAO type |
332 | 0 | { |
333 | 0 | sao_pars.modeIdc = sao_ctu_pars[ COMPONENT_Cb ].modeIdc; |
334 | 0 | sao_pars.typeIdc = sao_ctu_pars[ COMPONENT_Cb ].typeIdc; |
335 | 0 | } |
336 | 0 | if( sao_pars.modeIdc == SAO_MODE_OFF ) |
337 | 0 | { |
338 | 0 | continue; |
339 | 0 | } |
340 | | |
341 | | // sao_offset_abs |
342 | 0 | int offset[4]; |
343 | 0 | const int maxOffsetQVal = SampleAdaptiveOffset::getMaxOffsetQVal( sps.getBitDepth() ); |
344 | 0 | offset [0] = (int)unary_max_eqprob( maxOffsetQVal ); |
345 | 0 | offset [1] = (int)unary_max_eqprob( maxOffsetQVal ); |
346 | 0 | offset [2] = (int)unary_max_eqprob( maxOffsetQVal ); |
347 | 0 | offset [3] = (int)unary_max_eqprob( maxOffsetQVal ); |
348 | | |
349 | | // band offset mode |
350 | 0 | if( sao_pars.typeIdc == SAO_TYPE_START_BO ) |
351 | 0 | { |
352 | | // sao_offset_sign |
353 | 0 | for( int k = 0; k < 4; k++ ) |
354 | 0 | { |
355 | 0 | if( offset[k] && m_BinDecoder.decodeBinEP( ) ) |
356 | 0 | { |
357 | 0 | offset[k] = -offset[k]; |
358 | 0 | } |
359 | 0 | } |
360 | | // sao_band_position |
361 | 0 | sao_pars.typeAuxInfo = m_BinDecoder.decodeBinsEP( NUM_SAO_BO_CLASSES_LOG2 ); |
362 | 0 | for( int k = 0; k < 4; k++ ) |
363 | 0 | { |
364 | 0 | sao_pars.offset[ ( sao_pars.typeAuxInfo + k ) % MAX_NUM_SAO_CLASSES ] = offset[k]; |
365 | 0 | } |
366 | 0 | continue; |
367 | 0 | } |
368 | | |
369 | | // edge offset mode |
370 | 0 | sao_pars.typeAuxInfo = 0; |
371 | 0 | if( compID != COMPONENT_Cr ) |
372 | 0 | { |
373 | | // sao_eo_class_luma / sao_eo_class_chroma |
374 | 0 | sao_pars.typeIdc += m_BinDecoder.decodeBinsEP( NUM_SAO_EO_TYPES_LOG2 ); |
375 | 0 | } |
376 | 0 | else |
377 | 0 | { |
378 | 0 | sao_pars.typeIdc = sao_ctu_pars[ COMPONENT_Cb ].typeIdc; |
379 | 0 | } |
380 | 0 | sao_pars.offset[ SAO_CLASS_EO_FULL_VALLEY ] = offset[0]; |
381 | 0 | sao_pars.offset[ SAO_CLASS_EO_HALF_VALLEY ] = offset[1]; |
382 | 0 | sao_pars.offset[ SAO_CLASS_EO_PLAIN ] = 0; |
383 | 0 | sao_pars.offset[ SAO_CLASS_EO_HALF_PEAK ] = -offset[2]; |
384 | 0 | sao_pars.offset[ SAO_CLASS_EO_FULL_PEAK ] = -offset[3]; |
385 | 0 | } |
386 | 0 | } |
387 | | |
388 | | //================================================================================ |
389 | | // void readAlf( cs, ctuRsAddr, partitioner ) |
390 | | //================================================================================ |
391 | | void CABACReader::readAlf( CodingStructure& cs, unsigned int ctuRsAddr, const Partitioner& partitioner ) |
392 | 0 | { |
393 | 0 | const PreCalcValues& pcv = *cs.pcv; |
394 | 0 | int frame_width_in_ctus = pcv.widthInCtus; |
395 | 0 | int ry = ctuRsAddr / frame_width_in_ctus; |
396 | 0 | int rx = ctuRsAddr - ry * frame_width_in_ctus; |
397 | 0 | const Position pos ( rx * cs.pcv->maxCUWidth, ry * cs.pcv->maxCUHeight ); |
398 | 0 | bool leftAvail = cs.getCURestricted( pos.offset( -1, 0 ), pos, partitioner.currSliceIdx, partitioner.currTileIdx, CH_L ) ? true : false; |
399 | 0 | bool aboveAvail = cs.getCURestricted( pos.offset( 0, -1 ), pos, partitioner.currSliceIdx, partitioner.currTileIdx, CH_L ) ? true : false; |
400 | | |
401 | 0 | CtuAlfData& currAlfData = cs.getCtuData( ctuRsAddr ).alfParam; |
402 | 0 | CtuAlfData leftAlfData, aboveAlfData; |
403 | |
|
404 | 0 | if( leftAvail ) leftAlfData = cs.getCtuData( ctuRsAddr - 1 ).alfParam; |
405 | 0 | if( aboveAvail ) aboveAlfData = cs.getCtuData( ctuRsAddr - frame_width_in_ctus ).alfParam; |
406 | |
|
407 | 0 | if( m_slice->getAlfEnabledFlag( COMPONENT_Y ) ) |
408 | 0 | { |
409 | 0 | for( int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++ ) |
410 | 0 | { |
411 | 0 | if( m_slice->getAlfEnabledFlag( ( ComponentID ) compIdx ) ) |
412 | 0 | { |
413 | | //uint8_t* ctbAlfFlag = m_slice->getPic()->getAlfCtuEnableFlag( compIdx ); |
414 | 0 | int ctx = 0; |
415 | 0 | ctx += leftAlfData.alfCtuEnableFlag[compIdx]; |
416 | 0 | ctx += aboveAlfData.alfCtuEnableFlag[compIdx]; |
417 | | |
418 | 0 | currAlfData.alfCtuEnableFlag[compIdx] = m_BinDecoder.decodeBin( Ctx::ctbAlfFlag( compIdx * 3 + ctx ) ); |
419 | | |
420 | 0 | if( isLuma( ( ComponentID ) compIdx ) && currAlfData.alfCtuEnableFlag[compIdx] ) |
421 | 0 | { |
422 | 0 | currAlfData.alfCtbFilterIndex = readAlfCtuFilterIndex( cs, ctuRsAddr ); |
423 | 0 | } |
424 | |
|
425 | 0 | if( isChroma( ( ComponentID ) compIdx ) ) |
426 | 0 | { |
427 | 0 | const int apsIdx = m_slice->getAlfApsIdChroma(); |
428 | 0 | CHECK( m_slice->getAlfAPSs()[apsIdx] == nullptr, "APS not initialized" ); |
429 | 0 | const AlfSliceParam& alfParam = m_slice->getAlfAPSs()[apsIdx]->getAlfAPSParam(); |
430 | 0 | const int numAlts = alfParam.numAlternativesChroma; |
431 | 0 | currAlfData.alfCtuAlternative[compIdx - 1] = 0; |
432 | |
|
433 | 0 | if( currAlfData.alfCtuEnableFlag[compIdx] ) |
434 | 0 | { |
435 | 0 | uint8_t decoded = 0; |
436 | 0 | while( decoded < numAlts - 1 && m_BinDecoder.decodeBin( Ctx::ctbAlfAlternative( compIdx - 1 ) ) ) |
437 | 0 | ++decoded; |
438 | 0 | currAlfData.alfCtuAlternative[compIdx - 1] = decoded; |
439 | 0 | } |
440 | 0 | } |
441 | 0 | } |
442 | 0 | } |
443 | 0 | } |
444 | 0 | for( int compIdx = 1; compIdx < getNumberValidComponents( cs.pcv->chrFormat ); compIdx++ ) |
445 | 0 | { |
446 | 0 | if( m_slice->getCcAlfEnabledFlag( compIdx ) ) |
447 | 0 | { |
448 | 0 | int ctxt = 0; |
449 | 0 | ctxt += ( leftAlfData.ccAlfFilterControl[compIdx - 1] ) ? 1 : 0; |
450 | 0 | ctxt += ( aboveAlfData.ccAlfFilterControl[compIdx - 1] ) ? 1 : 0; |
451 | 0 | ctxt += ( compIdx == COMPONENT_Cr ) ? 3 : 0; |
452 | |
|
453 | 0 | int idcVal = m_BinDecoder.decodeBin( Ctx::CcAlfFilterControlFlag( ctxt ) ); |
454 | |
|
455 | 0 | if ( idcVal ) |
456 | 0 | { |
457 | 0 | const int apsIdx = compIdx == 1 ? m_slice->getCcAlfCbApsId() : m_slice->getCcAlfCrApsId(); |
458 | 0 | const int filterCount = m_slice->getAlfAPSs()[apsIdx]->getCcAlfAPSParam().ccAlfFilterCount[compIdx - 1]; |
459 | 0 | while ( ( idcVal != filterCount ) && m_BinDecoder.decodeBinEP() ) |
460 | 0 | { |
461 | 0 | idcVal++; |
462 | 0 | } |
463 | 0 | } |
464 | 0 | currAlfData.ccAlfFilterControl[compIdx - 1] = idcVal; |
465 | 0 | } |
466 | 0 | } |
467 | 0 | } |
468 | | |
469 | | //================================================================================ |
470 | | // clause 7.3.11.4 |
471 | | //-------------------------------------------------------------------------------- |
472 | | // bool coding_tree ( cs, partitioner, cuCtx ) |
473 | | // PartSplit split_cu_mode ( cs, partitioner ) |
474 | | // ModeType mode_constraint ( cs, partitioner, splitMode ) |
475 | | //================================================================================ |
476 | | |
477 | | bool CABACReader::coding_tree( CodingStructure& cs, Partitioner& partitioner, CUCtx& cuCtx ) |
478 | 0 | { |
479 | 0 | const PPS &pps = *cs.pps; |
480 | 0 | UnitArea currArea = partitioner.currArea(); |
481 | 0 | bool lastSegment = false; |
482 | 0 | bool chromaNotSplit = false; |
483 | 0 | const ModeType modeTypeParent = partitioner.modeType; |
484 | | |
485 | | // Reset delta QP coding flag and ChromaQPAdjustemt coding flag |
486 | 0 | if( pps.getUseDQP() && partitioner.currQgEnable() && !isChroma( partitioner.chType ) ) |
487 | 0 | { |
488 | 0 | cuCtx.qgStart = true; |
489 | 0 | cuCtx.isDQPCoded = false; |
490 | 0 | } |
491 | 0 | if( m_slice->getUseChromaQpAdj() && partitioner.currQgChromaEnable() ) |
492 | 0 | { |
493 | 0 | cuCtx.isChromaQpAdjCoded = false; |
494 | 0 | cs.chromaQpAdj = 0; |
495 | 0 | } |
496 | |
|
497 | 0 | const PartSplit split = split_cu_mode( cs, partitioner ); |
498 | |
|
499 | 0 | if( split != CU_DONT_SPLIT ) |
500 | 0 | { |
501 | 0 | partitioner.modeType = mode_constraint( cs, partitioner, split ); //change for child nodes |
502 | | |
503 | | //decide chroma split or not |
504 | 0 | chromaNotSplit = modeTypeParent == MODE_TYPE_ALL && partitioner.modeType == MODE_TYPE_INTRA; |
505 | |
|
506 | 0 | CHECK( chromaNotSplit && partitioner.chType != CHANNEL_TYPE_LUMA, "chType must be luma" ); |
507 | |
|
508 | 0 | if( partitioner.treeType == TREE_D ) |
509 | 0 | { |
510 | 0 | partitioner.treeType = chromaNotSplit ? TREE_L : TREE_D; |
511 | 0 | } |
512 | |
|
513 | 0 | partitioner.splitCurrArea( split, cs ); |
514 | |
|
515 | 0 | do |
516 | 0 | { |
517 | 0 | if( !lastSegment && cs.area.blocks[partitioner.chType].contains( partitioner.currArea().blocks[partitioner.chType].pos() ) ) |
518 | 0 | { |
519 | 0 | lastSegment = coding_tree( cs, partitioner, cuCtx ); |
520 | 0 | } |
521 | 0 | } while( partitioner.nextPart( cs ) ); |
522 | |
|
523 | 0 | partitioner.exitCurrSplit( cs ); |
524 | |
|
525 | 0 | if( chromaNotSplit ) |
526 | 0 | { |
527 | 0 | partitioner.chType = CHANNEL_TYPE_CHROMA; |
528 | 0 | partitioner.treeType = TREE_C; |
529 | 0 | partitioner.updateNeighbors( cs ); |
530 | | |
531 | 0 | if( !lastSegment && cs.picture->blocks[partitioner.chType].contains( partitioner.currArea().blocks[partitioner.chType].pos() ) ) |
532 | 0 | { |
533 | 0 | lastSegment = coding_tree( cs, partitioner, cuCtx ); |
534 | 0 | } |
535 | 0 | else |
536 | 0 | { |
537 | 0 | THROW_RECOVERABLE( "Unexpected behavior, not parsing chroma even though luma data is available!" ); |
538 | 0 | } |
539 | | |
540 | | //recover treeType |
541 | 0 | partitioner.chType = CHANNEL_TYPE_LUMA; |
542 | 0 | partitioner.treeType = TREE_D; |
543 | 0 | } |
544 | | |
545 | 0 | partitioner.modeType = modeTypeParent; |
546 | |
|
547 | 0 | return lastSegment; |
548 | 0 | } |
549 | | |
550 | 0 | TreeType treeType = partitioner.treeType; |
551 | |
|
552 | 0 | if( isChroma( partitioner.chType ) ) { currArea.Y() = CompArea(); treeType = TREE_C; } |
553 | 0 | else if( partitioner.isDualITree || partitioner.treeType == TREE_L ) { currArea.Cb() = currArea.Cr() = CompArea(); treeType = TREE_L; } |
554 | |
|
555 | 0 | CodingUnit& cu = cs.addCU( currArea, partitioner.chType, treeType, partitioner.modeType, partitioner.currPartLevel().cuLeft, partitioner.currPartLevel().cuAbove ); |
556 | |
|
557 | | #if ENABLE_TRACING && 0 |
558 | | if( cu.chType() == CHANNEL_TYPE_CHROMA ) |
559 | | { |
560 | | DTRACE( g_trace_ctx, D_SYNTAX, "[chroma] CU x=%d, y=%d, w=%d, h=%d\n", cu.Cb().x, cu.Cb().y, cu.Cb().width, cu.Cb().height ); |
561 | | } |
562 | | else |
563 | | DTRACE( g_trace_ctx, D_SYNTAX, "CU x=%d, y=%d, w=%d, h=%d\n", cu.Y().x, cu.Y().y, cu.Y().width, cu.Y().height ); |
564 | | |
565 | | #endif |
566 | |
|
567 | 0 | partitioner.setCUData( cu ); |
568 | 0 | cu.slice = m_slice; |
569 | 0 | cu.pps = cu.slice->getPPS(); |
570 | 0 | cu.sps = cu.slice->getSPS(); |
571 | 0 | cu.tileIdx = partitioner.currTileIdx; |
572 | 0 | int lumaQPinLocalDualTree = -1; |
573 | | |
574 | | // Predict QP on start of quantization group |
575 | 0 | if( cuCtx.qgStart ) |
576 | 0 | { |
577 | 0 | cuCtx.qgStart = false; |
578 | 0 | cuCtx.qp = CU::predictQP( cu, cuCtx.qp ); |
579 | 0 | } |
580 | |
|
581 | 0 | if( pps.getUseDQP() && partitioner.isSepTree( cs ) && isChroma( cu.chType() ) ) |
582 | 0 | { |
583 | 0 | const Position chromaCentral( cu.chromaPos().offset( cu.chromaSize().width >> 1, cu.chromaSize().height >> 1 ) ); |
584 | 0 | const Position lumaRefPos( chromaCentral.x << getComponentScaleX( COMPONENT_Cb, cu.chromaFormat ), chromaCentral.y << getComponentScaleY( COMPONENT_Cb, cu.chromaFormat ) ); |
585 | | //derive chroma qp, but the chroma qp is saved in cuCtx.qp which is used for luma qp |
586 | | //therefore, after decoding the chroma CU, the cuCtx.qp shall be recovered to luma qp in order to decode next luma cu qp |
587 | | // const CodingUnit* colLumaCu = cs.getLumaCU( lumaRefPos ); |
588 | 0 | const CodingUnit* colLumaCu = cs.getCU( lumaRefPos, CHANNEL_TYPE_LUMA ); |
589 | 0 | CHECK( colLumaCu == nullptr, "colLumaCU shall exist" ); |
590 | 0 | lumaQPinLocalDualTree = cuCtx.qp; |
591 | |
|
592 | 0 | if( colLumaCu ) cuCtx.qp = colLumaCu->qp; |
593 | 0 | } |
594 | | |
595 | 0 | cu.qp = cuCtx.qp; //NOTE: CU QP can be changed by deltaQP signaling at TU level |
596 | 0 | cu.chromaQpAdj = cs.chromaQpAdj; //NOTE: CU chroma QP adjustment can be changed by adjustment signaling at TU level |
597 | | |
598 | | // coding unit |
599 | 0 | bool isLastCtu = coding_unit( cu, partitioner, cuCtx ); |
600 | | //recover cuCtx.qp to luma qp after decoding the chroma CU |
601 | 0 | if( pps.getUseDQP() && partitioner.isSepTree( cs ) && isChroma( cu.chType() ) ) |
602 | 0 | { |
603 | 0 | cuCtx.qp = lumaQPinLocalDualTree; |
604 | 0 | } |
605 | |
|
606 | 0 | if( isChromaEnabled( cs.pcv->chrFormat ) ) |
607 | 0 | for( TransformUnit& tu : TUTraverser( &cu.firstTU, cu.lastTU->next ) ) |
608 | 0 | { |
609 | 0 | if( tu.Cb().valid() ) |
610 | 0 | { |
611 | 0 | QpParam cQP( tu, COMPONENT_Cb, false ); |
612 | 0 | tu.chromaQp[COMPONENT_Cb - 1] = cQP.Qp( false ); |
613 | 0 | } |
614 | |
|
615 | 0 | if( tu.Cr().valid() ) |
616 | 0 | { |
617 | 0 | QpParam cQP( tu, COMPONENT_Cr, false ); |
618 | 0 | tu.chromaQp[COMPONENT_Cr - 1] = cQP.Qp( false ); |
619 | 0 | } |
620 | 0 | } |
621 | |
|
622 | | #if ENABLE_TRACING |
623 | | if( cu.chType() == CHANNEL_TYPE_CHROMA ) |
624 | | { |
625 | | DTRACE( g_trace_ctx, D_QP, "[chroma CU]x=%d, y=%d, w=%d, h=%d, qp=%d\n", cu.Cb().x, cu.Cb().y, cu.Cb().width, cu.Cb().height, cu.qp ); |
626 | | } |
627 | | else |
628 | | DTRACE( g_trace_ctx, D_QP, "x=%d, y=%d, w=%d, h=%d, qp=%d\n", cu.Y().x, cu.Y().y, cu.Y().width, cu.Y().height, cu.qp ); |
629 | | |
630 | | #endif |
631 | 0 | return isLastCtu; |
632 | 0 | } |
633 | | |
634 | | int signalModeCons( const CodingStructure& cs, const Slice* slice, const PartSplit split, const Partitioner &partitioner, const ModeType modeTypeParent ) |
635 | 0 | { |
636 | 0 | if( partitioner.isDualITree || modeTypeParent != MODE_TYPE_ALL || partitioner.currArea().chromaFormat == CHROMA_444 || partitioner.currArea().chromaFormat == CHROMA_400 ) |
637 | 0 | return LDT_MODE_TYPE_INHERIT; |
638 | | |
639 | 0 | int minLumaArea = partitioner.currArea().lumaSize().area(); |
640 | 0 | if( split == CU_QUAD_SPLIT || split == CU_TRIH_SPLIT || split == CU_TRIV_SPLIT ) // the area is split into 3 or 4 parts |
641 | 0 | { |
642 | 0 | minLumaArea = minLumaArea >> 2; |
643 | 0 | } |
644 | 0 | else if( split == CU_VERT_SPLIT || split == CU_HORZ_SPLIT ) // the area is split into 2 parts |
645 | 0 | { |
646 | 0 | minLumaArea = minLumaArea >> 1; |
647 | 0 | } |
648 | |
|
649 | 0 | const int minChromaBlock = minLumaArea >> ( getChannelTypeScaleX( CHANNEL_TYPE_CHROMA, partitioner.currArea().chromaFormat ) + getChannelTypeScaleY( CHANNEL_TYPE_CHROMA, partitioner.currArea().chromaFormat ) ); |
650 | 0 | const bool is2xNChroma = ( partitioner.currArea().chromaSize().width == 4 && split == CU_VERT_SPLIT ) || ( partitioner.currArea().chromaSize().width == 8 && split == CU_TRIV_SPLIT ); |
651 | 0 | return minChromaBlock >= 16 && |
652 | 0 | !is2xNChroma ? |
653 | 0 | LDT_MODE_TYPE_INHERIT : |
654 | 0 | ( minLumaArea < 32 || slice->isIntra() ) ? LDT_MODE_TYPE_INFER : LDT_MODE_TYPE_SIGNAL; |
655 | 0 | } |
656 | | |
657 | | ModeType CABACReader::mode_constraint( CodingStructure& cs, Partitioner &partitioner, PartSplit splitMode ) |
658 | 0 | { |
659 | 0 | const int val = signalModeCons( cs, m_slice, splitMode, partitioner, partitioner.modeType ); |
660 | |
|
661 | 0 | if( val == LDT_MODE_TYPE_SIGNAL ) |
662 | 0 | { |
663 | 0 | const int ctxIdx = DeriveCtx::CtxModeConsFlag( cs, partitioner ); |
664 | 0 | const bool flag = m_BinDecoder.decodeBin( Ctx::ModeConsFlag( ctxIdx ) ); |
665 | |
|
666 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "mode_cons_flag() flag=%d\n", flag ); |
667 | 0 | return flag ? MODE_TYPE_INTRA : MODE_TYPE_INTER; |
668 | 0 | } |
669 | 0 | else if( val == LDT_MODE_TYPE_INFER ) |
670 | 0 | { |
671 | 0 | return MODE_TYPE_INTRA; |
672 | 0 | } |
673 | 0 | else |
674 | 0 | { |
675 | 0 | return partitioner.modeType; |
676 | 0 | } |
677 | 0 | } |
678 | | |
679 | | PartSplit CABACReader::split_cu_mode( CodingStructure& cs, Partitioner &partitioner ) |
680 | 0 | { |
681 | 0 | PartSplit mode = CU_DONT_SPLIT; |
682 | |
|
683 | 0 | bool canNo, canQt, canBh, canBv, canTh, canTv; |
684 | 0 | partitioner.canSplit( cs, canNo, canQt, canBh, canBv, canTh, canTv ); |
685 | |
|
686 | 0 | const unsigned numHor = canBh + canTh; |
687 | 0 | const unsigned numVer = canBv + canTv; |
688 | 0 | unsigned numSplit = ( canQt << 1 ) + numHor + numVer; |
689 | |
|
690 | 0 | bool isSplit = !!numSplit; |
691 | |
|
692 | 0 | #if !ENABLE_TRACING |
693 | 0 | if( canNo && !isSplit ) |
694 | 0 | { |
695 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_mode() ctx=0 split=0\n" ); |
696 | 0 | return CU_DONT_SPLIT; |
697 | 0 | } |
698 | | |
699 | 0 | #endif |
700 | 0 | unsigned ctxSplit, ctxQtSplit, ctxBttHV, ctxBtt12; |
701 | |
|
702 | 0 | const CodingUnit* cuLeft = partitioner.currPartLevel().cuLeft; |
703 | 0 | const CodingUnit* cuAbove = partitioner.currPartLevel().cuAbove; |
704 | 0 | const unsigned widthCurr = partitioner.currArea().blocks[partitioner.chType].width; |
705 | 0 | const unsigned heightCurr = partitioner.currArea().blocks[partitioner.chType].height; |
706 | |
|
707 | 0 | #if !ENABLE_TRACING |
708 | 0 | if( canNo && isSplit ) |
709 | 0 | #endif |
710 | 0 | { |
711 | | /////////////////////// |
712 | | // CTX do split (0-8) |
713 | | /////////////////////// |
714 | |
|
715 | 0 | ctxSplit = ( cuLeft && cuLeft ->blocks[partitioner.chType].height < heightCurr ); |
716 | 0 | ctxSplit += ( cuAbove && cuAbove->blocks[partitioner.chType].width < widthCurr ); |
717 | | |
718 | | // 0, 1, 2, 3, 4, 5, 6 // split |
719 | | // 0, 0, 1, 2, 3, 4, 5 // split - 1 |
720 | 0 | static constexpr int ctxOffset[] = { 0, 0, 0, 3, 3, 6, 6 }; |
721 | 0 | ctxSplit += ctxOffset[numSplit]; |
722 | |
|
723 | | #if ENABLE_TRACING |
724 | | if( canNo && isSplit ) |
725 | | #endif // endif |
726 | |
|
727 | 0 | isSplit = m_BinDecoder.decodeBin( Ctx::SplitFlag( ctxSplit ) ); |
728 | 0 | } |
729 | |
|
730 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_mode() ctx=%d split=%d\n", ctxSplit, isSplit ); |
731 | |
|
732 | 0 | if( !isSplit ) |
733 | 0 | { |
734 | 0 | return CU_DONT_SPLIT; |
735 | 0 | } |
736 | | |
737 | 0 | const bool canBtt = !!numHor || !!numVer; |
738 | 0 | bool isQt = canQt; |
739 | |
|
740 | 0 | #if !ENABLE_TRACING |
741 | 0 | if( isQt && canBtt ) |
742 | 0 | #endif |
743 | 0 | { |
744 | | ////////////////////////// |
745 | | // CTX is qt split (0-5) |
746 | | ////////////////////////// |
747 | 0 | ctxQtSplit = ( cuLeft && cuLeft->qtDepth > partitioner.currQtDepth ); |
748 | 0 | ctxQtSplit += ( cuAbove && cuAbove->qtDepth > partitioner.currQtDepth ); |
749 | 0 | ctxQtSplit += partitioner.currQtDepth < 2 ? 0 : 3; |
750 | |
|
751 | | #if ENABLE_TRACING |
752 | | if( isQt && canBtt ) |
753 | | #endif |
754 | 0 | isQt = m_BinDecoder.decodeBin( Ctx::SplitQtFlag( ctxQtSplit ) ); |
755 | 0 | } |
756 | |
|
757 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_mode() ctx=%d qt=%d\n", ctxQtSplit, isQt ); |
758 | |
|
759 | 0 | if( isQt ) |
760 | 0 | { |
761 | 0 | return CU_QUAD_SPLIT; |
762 | 0 | } |
763 | | |
764 | 0 | const bool canHor = !!numHor; |
765 | 0 | bool isVer = !!numVer; |
766 | |
|
767 | 0 | #if !ENABLE_TRACING |
768 | 0 | if( isVer && canHor ) |
769 | 0 | #endif |
770 | 0 | { |
771 | | //////////////////////////// |
772 | | // CTX is ver split (0-4) |
773 | | //////////////////////////// |
774 | 0 | ctxBttHV = 0; |
775 | |
|
776 | 0 | if( numVer == numHor ) |
777 | 0 | { |
778 | 0 | if( cuLeft && cuAbove ) |
779 | 0 | { |
780 | 0 | const int wIdxAbove = getLog2( cuAbove->blocks[partitioner.chType].width ); |
781 | 0 | const int hIdxLeft = getLog2( cuLeft ->blocks[partitioner.chType].height ); |
782 | |
|
783 | 0 | const int depAbove = widthCurr >> wIdxAbove; |
784 | 0 | const int depLeft = heightCurr >> hIdxLeft; |
785 | |
|
786 | 0 | if( depAbove == depLeft ) ctxBttHV = 0; |
787 | 0 | else if( depAbove < depLeft ) ctxBttHV = 1; |
788 | 0 | else ctxBttHV = 2; |
789 | 0 | } |
790 | 0 | } |
791 | 0 | else if( numVer < numHor ) |
792 | 0 | { |
793 | 0 | ctxBttHV = 3; |
794 | 0 | } |
795 | 0 | else |
796 | 0 | { |
797 | 0 | ctxBttHV = 4; |
798 | 0 | } |
799 | |
|
800 | | #if ENABLE_TRACING |
801 | | if( isVer && canHor ) |
802 | | #endif |
803 | 0 | isVer = m_BinDecoder.decodeBin( Ctx::SplitHvFlag( ctxBttHV ) ); |
804 | 0 | } |
805 | |
|
806 | 0 | const bool can14 = isVer ? canTv : canTh; |
807 | 0 | bool is12 = isVer ? canBv : canBh; |
808 | |
|
809 | 0 | #if !ENABLE_TRACING |
810 | 0 | if( is12 && can14 ) |
811 | 0 | #endif |
812 | 0 | { |
813 | | ////////////////////////// |
814 | | // CTX is h/v bt (0-3) |
815 | | ////////////////////////// |
816 | 0 | ctxBtt12 = !!( partitioner.currMtDepth <= 1 ) + ( isVer << 1 ); |
817 | |
|
818 | | #if ENABLE_TRACING |
819 | | if( is12 && can14 ) |
820 | | #endif |
821 | 0 | is12 = m_BinDecoder.decodeBin( Ctx::Split12Flag( ctxBtt12 ) ); |
822 | 0 | } |
823 | |
|
824 | 0 | if ( isVer && is12 ) mode = CU_VERT_SPLIT; |
825 | 0 | else if( isVer && !is12 ) mode = CU_TRIV_SPLIT; |
826 | 0 | else if( !isVer && is12 ) mode = CU_HORZ_SPLIT; |
827 | 0 | else mode = CU_TRIH_SPLIT; |
828 | |
|
829 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_mode() ctxHv=%d ctx12=%d mode=%d\n", ctxBttHV, ctxBtt12, mode ); |
830 | |
|
831 | 0 | return mode; |
832 | 0 | } |
833 | | |
834 | | //================================================================================ |
835 | | // clause 7.3.11.5 |
836 | | //-------------------------------------------------------------------------------- |
837 | | // bool coding_unit ( cu, pm, cuCtx ) |
838 | | // void cu_skip_flag ( cu ) |
839 | | // void pred_mode ( cu ) |
840 | | // void bdpcm_mode ( cu, compID ) |
841 | | // void cu_pred_data ( cu ) |
842 | | // void cu_bcw_flag ( cu ) |
843 | | // void extend_ref_line ( cu ) |
844 | | // void intra_luma_pred_mode ( cu ) |
845 | | // bool intra_chroma_lmc_mode ( cu ) |
846 | | // void intra_chroma_pred_mode ( cu ) |
847 | | // void cu_residual ( cu, pm, cuCtx ) |
848 | | // void rqt_root_cbf ( cu ) |
849 | | // void adaptive_color_transform( cu ) |
850 | | // void sbt_mode ( cu ) |
851 | | // void mip_flag ( cu ) |
852 | | // void mip_pred_mode ( cu ) |
853 | | // bool end_of_ctu ( cu, cuCtx ) |
854 | | //================================================================================ |
855 | | |
856 | | bool CABACReader::coding_unit( CodingUnit &cu, Partitioner &partitioner, CUCtx& cuCtx ) |
857 | 0 | { |
858 | 0 | CodingStructure& cs = *cu.cs; |
859 | |
|
860 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "coding_unit() treeType=%d modeType=%d\n", cu.treeType(), cu.modeType() ); |
861 | | |
862 | | // skip flag |
863 | 0 | if( !m_slice->isIntra() || cs.sps->getIBCFlag() ) |
864 | 0 | { |
865 | 0 | if( cu.Y().valid() ) |
866 | 0 | { |
867 | 0 | cu_skip_flag( cu ); |
868 | 0 | } |
869 | | |
870 | | // skip data |
871 | 0 | if( cu.skip() ) |
872 | 0 | { |
873 | 0 | cu.setColorTransform( false ); |
874 | 0 | cs.addEmptyTUs ( partitioner, cu ); |
875 | 0 | MergeCtx mrgCtx; |
876 | 0 | prediction_unit ( cu ); |
877 | 0 | return end_of_ctu ( cu, cuCtx ); |
878 | 0 | } |
879 | 0 | else |
880 | 0 | { |
881 | | // prediction mode and partitioning data |
882 | 0 | pred_mode( cu ); |
883 | 0 | } |
884 | 0 | } |
885 | 0 | else |
886 | 0 | { |
887 | 0 | cu.setPredMode( MODE_INTRA ); |
888 | 0 | } |
889 | | |
890 | | // TODO: palette stuff |
891 | | |
892 | 0 | if( CU::isIntra( cu ) ) |
893 | 0 | { |
894 | 0 | adaptive_color_transform( cu ); |
895 | 0 | } |
896 | | |
897 | | // prediction data ( intra prediction modes / reference indexes + motion vectors ) |
898 | 0 | cu_pred_data( cu ); |
899 | | |
900 | | // residual data ( coded block flags + transform coefficient levels ) |
901 | 0 | cu_residual( cu, partitioner, cuCtx ); |
902 | | |
903 | | // check end of cu |
904 | 0 | return end_of_ctu( cu, cuCtx ); |
905 | 0 | } |
906 | | |
907 | | |
908 | | void CABACReader::cu_skip_flag( CodingUnit& cu ) |
909 | 0 | { |
910 | 0 | bool ibcFlag = cu.slice->getSPS()->getIBCFlag() && cu.lwidth() <= 64 && cu.lheight() <= 64; |
911 | |
|
912 | 0 | if( ( cu.slice->isIntra() || CU::isConsIntra( cu ) ) && ibcFlag ) |
913 | 0 | { |
914 | | //cu.setSkip ( false ); |
915 | | //cu.setRootCbf ( false ); |
916 | | //cu.setPredMode ( MODE_INTRA ); |
917 | | //cu.setMmvdFlag ( false ); |
918 | |
|
919 | 0 | const unsigned ctxId = DeriveCtx::CtxSkipFlag( cu ); |
920 | 0 | const unsigned skip = m_BinDecoder.decodeBin( Ctx::SkipFlag( ctxId ) ); |
921 | |
|
922 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "cu_skip_flag() ctx=%d skip=%d\n", ctxId, skip ? 1 : 0 ); |
923 | |
|
924 | 0 | if( skip ) |
925 | 0 | { |
926 | 0 | cu.setSkip ( true ); |
927 | | //cu.setRootCbf ( false ); |
928 | 0 | cu.setPredMode ( MODE_IBC ); |
929 | | //cu.setMmvdFlag ( false ); |
930 | 0 | cu.cs->hasIbcBlock[cu.ctuData->lineIdx] = 1; |
931 | 0 | } |
932 | |
|
933 | 0 | return; |
934 | 0 | } |
935 | 0 | else if( !ibcFlag && ( ( cu.lwidth() == 4 && cu.lheight() == 4 ) || CU::isConsIntra( cu ) ) ) |
936 | 0 | { |
937 | 0 | return; |
938 | 0 | } |
939 | | |
940 | 0 | unsigned ctxId = DeriveCtx::CtxSkipFlag(cu); |
941 | 0 | unsigned skip = m_BinDecoder.decodeBin( Ctx::SkipFlag( ctxId ) ); |
942 | |
|
943 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "cu_skip_flag() ctx=%d skip=%d\n", ctxId, skip ? 1 : 0 ); |
944 | |
|
945 | 0 | if( skip && ibcFlag ) |
946 | 0 | { |
947 | 0 | if( !CU::isConsInter( cu ) ) // disable IBC mode larger than 64x64 and disable IBC when only allowing inter mode |
948 | 0 | { |
949 | 0 | if( cu.lwidth() == 4 && cu.lheight() == 4 ) |
950 | 0 | { |
951 | 0 | cu.setSkip ( true ); |
952 | | //cu.setRootCbf ( false ); |
953 | 0 | cu.setPredMode ( MODE_IBC ); |
954 | | //cu.setMmvdFlag ( false ); |
955 | 0 | cu.cs->hasIbcBlock[cu.ctuData->lineIdx] = 1; |
956 | 0 | return; |
957 | 0 | } |
958 | | |
959 | 0 | unsigned ctxidx = DeriveCtx::CtxIBCFlag( cu ); |
960 | |
|
961 | 0 | if( m_BinDecoder.decodeBin( Ctx::IBCFlag( ctxidx ) ) ) |
962 | 0 | { |
963 | 0 | cu.setSkip ( true ); |
964 | | //cu.setRootCbf ( false ); |
965 | 0 | cu.setPredMode ( MODE_IBC ); |
966 | | //cu.setMmvdFlag ( false ); |
967 | | //cu.setRegularMergeFlag ( false ); |
968 | 0 | cu.cs->hasIbcBlock[cu.ctuData->lineIdx] = 1; |
969 | 0 | } |
970 | 0 | else |
971 | 0 | { |
972 | | //cu.setPredMode( MODE_INTER ); |
973 | 0 | } |
974 | |
|
975 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "ibc() ctx=%d cu.predMode=%d\n", ctxidx, cu.predMode() ); |
976 | 0 | } |
977 | 0 | else |
978 | 0 | { |
979 | | //cu.setPredMode( MODE_INTER ); |
980 | 0 | } |
981 | 0 | } |
982 | | |
983 | 0 | if( skip ) |
984 | 0 | { |
985 | 0 | cu.setSkip( true ); |
986 | | //cu.setRootCbf ( false ); |
987 | | //cu.setPredMode( MODE_INTER ); |
988 | 0 | } |
989 | 0 | } |
990 | | |
991 | | void CABACReader::amvr_mode( CodingUnit& cu ) |
992 | 0 | { |
993 | 0 | const SPS *sps = cu.sps; |
994 | |
|
995 | 0 | if( !sps->getAMVREnabledFlag() || !CU::hasSubCUNonZeroMVd( cu ) ) |
996 | 0 | { |
997 | 0 | return; |
998 | 0 | } |
999 | 0 | unsigned value = 0; |
1000 | |
|
1001 | 0 | if( CU::isIBC( cu ) ) |
1002 | 0 | value = 1; |
1003 | 0 | else |
1004 | 0 | value = m_BinDecoder.decodeBin( Ctx::ImvFlag( 0 ) ); |
1005 | |
|
1006 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "imv_mode() value=%d ctx=%d\n", value, 0 ); |
1007 | |
|
1008 | 0 | if( value ) |
1009 | 0 | { |
1010 | 0 | cu.setImv( value ); |
1011 | |
|
1012 | 0 | if( !CU::isIBC( cu ) ) |
1013 | 0 | { |
1014 | 0 | value = m_BinDecoder.decodeBin( Ctx::ImvFlag( 4 ) ); |
1015 | 0 | cu.setImv( value ? 1 : IMV_HPEL ); |
1016 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "imv_mode() value=%d ctx=%d\n", value, 4 ); |
1017 | 0 | } |
1018 | |
|
1019 | 0 | if( value ) |
1020 | 0 | { |
1021 | 0 | value = m_BinDecoder.decodeBin( Ctx::ImvFlag( 1 ) ); |
1022 | 0 | value++; |
1023 | 0 | cu.setImv( value ); |
1024 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "imv_mode() value=%d ctx=%d\n", ( value - 1 ), 1 ); |
1025 | 0 | } |
1026 | 0 | } |
1027 | |
|
1028 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "imv_mode() IMVFlag=%d\n", cu.imv() ); |
1029 | 0 | } |
1030 | | |
1031 | | void CABACReader::affine_amvr_mode( CodingUnit& cu ) |
1032 | 0 | { |
1033 | 0 | const SPS* sps = cu.sps; |
1034 | |
|
1035 | 0 | if( !sps->getAffineAmvrEnabledFlag() || !CU::hasSubCUNonZeroAffineMVd( cu ) ) |
1036 | 0 | { |
1037 | 0 | return; |
1038 | 0 | } |
1039 | | |
1040 | 0 | unsigned value = 0; |
1041 | 0 | value = m_BinDecoder.decodeBin( Ctx::ImvFlag( 2 ) ); |
1042 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "affine_amvr_mode() value=%d ctx=%d\n", value, 2 ); |
1043 | |
|
1044 | 0 | if( value ) |
1045 | 0 | { |
1046 | 0 | value = m_BinDecoder.decodeBin( Ctx::ImvFlag( 3 ) ); |
1047 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "affine_amvr_mode() value=%d ctx=%d\n", value, 3 ); |
1048 | 0 | value++; |
1049 | 0 | } |
1050 | |
|
1051 | 0 | cu.setImv( value ); |
1052 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "affine_amvr_mode() IMVFlag=%d\n", cu.imv() ); |
1053 | 0 | } |
1054 | | |
1055 | | void CABACReader::pred_mode( CodingUnit& cu ) |
1056 | 0 | { |
1057 | 0 | if( CU::isConsInter( cu ) ) |
1058 | 0 | { |
1059 | | //cu.setPredMode( MODE_INTER ); |
1060 | 0 | return; |
1061 | 0 | } |
1062 | | |
1063 | 0 | bool ibcAllowed = false; |
1064 | |
|
1065 | 0 | if( cu.slice->isIntra() || ( cu.lwidth() == 4 && cu.lheight() == 4 ) || CU::isConsIntra( cu ) ) |
1066 | 0 | { |
1067 | 0 | ibcAllowed = true; |
1068 | 0 | cu.setPredMode( MODE_INTRA ); |
1069 | 0 | } |
1070 | 0 | else |
1071 | 0 | { |
1072 | 0 | if( m_BinDecoder.decodeBin( Ctx::PredMode( DeriveCtx::CtxPredModeFlag( cu ) ) ) ) |
1073 | 0 | { |
1074 | 0 | cu.setPredMode( MODE_INTRA ); |
1075 | 0 | } |
1076 | 0 | else |
1077 | 0 | { |
1078 | 0 | ibcAllowed = true; |
1079 | 0 | } |
1080 | 0 | } |
1081 | |
|
1082 | 0 | ibcAllowed &= isLuma( cu.chType() ) && cu.sps->getIBCFlag() && cu.lwidth() <= 64 && cu.lheight() <= 64; |
1083 | |
|
1084 | 0 | if( ibcAllowed ) |
1085 | 0 | { |
1086 | 0 | unsigned ctxidx = DeriveCtx::CtxIBCFlag( cu ); |
1087 | |
|
1088 | 0 | if( m_BinDecoder.decodeBin( Ctx::IBCFlag( ctxidx ) ) ) |
1089 | 0 | { |
1090 | 0 | cu.setPredMode( MODE_IBC ); |
1091 | 0 | cu.cs->hasIbcBlock[cu.ctuData->lineIdx] = 1; |
1092 | 0 | } |
1093 | 0 | } |
1094 | 0 | } |
1095 | | |
1096 | | void CABACReader::bdpcm_mode( CodingUnit& cu, const ComponentID compID ) |
1097 | 0 | { |
1098 | |
|
1099 | 0 | if( !CU::bdpcmAllowed( cu, compID ) ) |
1100 | 0 | { |
1101 | | //if( isLuma( compID ) ) |
1102 | | //{ |
1103 | | // cu.setBdpcmMode( 0 ); |
1104 | | // if( !CS::isDualITree( *cu.cs ) ) |
1105 | | // cu.setBdpcmModeChroma( 0 ); |
1106 | | //} |
1107 | | //else |
1108 | | //{ |
1109 | | // cu.setBdpcmModeChroma( 0 ); |
1110 | | //} |
1111 | 0 | return; |
1112 | 0 | } |
1113 | | |
1114 | 0 | const unsigned ctxId = isLuma( compID ) ? 0 : 2; |
1115 | 0 | int bdpcmMode = m_BinDecoder.decodeBin( Ctx::BDPCMMode( ctxId ) ); |
1116 | |
|
1117 | 0 | if( bdpcmMode ) |
1118 | 0 | { |
1119 | 0 | bdpcmMode += m_BinDecoder.decodeBin( Ctx::BDPCMMode( ctxId + 1 ) ); |
1120 | 0 | } |
1121 | |
|
1122 | 0 | if( isLuma( compID ) ) |
1123 | 0 | { |
1124 | 0 | cu.setBdpcmMode( bdpcmMode ); |
1125 | 0 | } |
1126 | 0 | else |
1127 | 0 | { |
1128 | 0 | cu.setBdpcmModeChroma( bdpcmMode ); |
1129 | 0 | } |
1130 | |
|
1131 | | #if ENABLE_TRACING |
1132 | | if( isLuma( compID ) ) |
1133 | | { |
1134 | | DTRACE( g_trace_ctx, D_SYNTAX, "bdpcm_mode(%d) x=%d, y=%d, w=%d, h=%d, bdpcm=%d\n", CHANNEL_TYPE_LUMA, cu.lumaPos().x, cu.lumaPos().y, cu.lwidth(), cu.lheight(), cu.bdpcmMode() ); |
1135 | | } |
1136 | | else |
1137 | | { |
1138 | | DTRACE( g_trace_ctx, D_SYNTAX, "bdpcm_mode(%d) x=%d, y=%d, w=%d, h=%d, bdpcm=%d\n", CHANNEL_TYPE_CHROMA, cu.chromaPos().x, cu.chromaPos().y, cu.chromaSize().width, cu.chromaSize().height, cu.bdpcmModeChroma() ); |
1139 | | } |
1140 | | #endif |
1141 | 0 | } |
1142 | | |
1143 | | void CABACReader::cu_pred_data( CodingUnit &cu ) |
1144 | 0 | { |
1145 | 0 | if( CU::isIntra( cu ) ) |
1146 | 0 | { |
1147 | 0 | if( isLuma( cu.chType() ) ) |
1148 | 0 | { |
1149 | 0 | bdpcm_mode( cu, COMPONENT_Y ); |
1150 | 0 | intra_luma_pred_mode( cu ); |
1151 | 0 | } |
1152 | 0 | if( ( isChroma( cu.chType() ) || !CU::isSepTree( cu ) ) && isChromaEnabled( cu.chromaFormat ) ) |
1153 | 0 | { |
1154 | 0 | bdpcm_mode( cu, ComponentID( CHANNEL_TYPE_CHROMA ) ); |
1155 | 0 | intra_chroma_pred_mode( cu ); |
1156 | 0 | } |
1157 | |
|
1158 | 0 | return; |
1159 | 0 | } |
1160 | | |
1161 | 0 | if( !cu.Y().valid() ) // dual tree chroma CU |
1162 | 0 | { |
1163 | 0 | cu.setPredMode( MODE_IBC ); |
1164 | 0 | cu.cs->hasIbcBlock[cu.ctuData->lineIdx] = 1; |
1165 | 0 | return; |
1166 | 0 | } |
1167 | | |
1168 | 0 | prediction_unit ( cu ); |
1169 | |
|
1170 | 0 | if( !cu.mergeFlag() ) |
1171 | 0 | { |
1172 | 0 | if( cu.affineFlag() ) |
1173 | 0 | affine_amvr_mode( cu ); |
1174 | 0 | else |
1175 | 0 | amvr_mode ( cu ); |
1176 | 0 | cu_bcw_flag ( cu ); |
1177 | 0 | } |
1178 | 0 | } |
1179 | | |
1180 | | void CABACReader::cu_bcw_flag(CodingUnit& cu) |
1181 | 0 | { |
1182 | 0 | if( !CU::isBcwIdxCoded( cu ) ) |
1183 | 0 | { |
1184 | 0 | return; |
1185 | 0 | } |
1186 | | |
1187 | 0 | CHECK(!(BCW_NUM > 1 && (BCW_NUM == 2 || (BCW_NUM & 0x01) == 1)), " !( BCW_NUM > 1 && ( BCW_NUM == 2 || ( BCW_NUM & 0x01 ) == 1 ) ) "); |
1188 | |
|
1189 | 0 | uint32_t idx = 0; |
1190 | 0 | uint32_t symbol = m_BinDecoder.decodeBin( Ctx::BcwIdx( 0 ) ); |
1191 | 0 | int32_t numBcw = ( cu.slice->getCheckLDC() ) ? 5 : 3; |
1192 | |
|
1193 | 0 | if( symbol == 1 ) |
1194 | 0 | { |
1195 | 0 | uint32_t prefixNumBits = numBcw - 2; |
1196 | 0 | uint32_t step = 1; |
1197 | |
|
1198 | 0 | idx = 1; |
1199 | |
|
1200 | 0 | for( int ui = 0; ui < prefixNumBits && m_BinDecoder.decodeBinEP(); ++ui ) idx += step; |
1201 | 0 | } |
1202 | |
|
1203 | 0 | uint8_t bcwIdx = ( uint8_t ) g_BcwParsingOrder[idx]; |
1204 | 0 | CU::setBcwIdx( cu, g_BcwInternFwd[bcwIdx] ); |
1205 | |
|
1206 | 0 | DTRACE(g_trace_ctx, D_SYNTAX, "cu_bcw_flag() bcw_idx=%d\n", cu.BcwIdx() ? 1 : 0); |
1207 | 0 | } |
1208 | | |
1209 | | void CABACReader::xReadTruncBinCode(uint32_t& symbol, uint32_t maxSymbol) |
1210 | 0 | { |
1211 | 0 | int thresh; |
1212 | 0 | if (maxSymbol > 256) |
1213 | 0 | { |
1214 | 0 | int threshVal = 1 << 8; |
1215 | 0 | thresh = 8; |
1216 | 0 | while (threshVal <= maxSymbol) |
1217 | 0 | { |
1218 | 0 | thresh++; |
1219 | 0 | threshVal <<= 1; |
1220 | 0 | } |
1221 | 0 | thresh--; |
1222 | 0 | } |
1223 | 0 | else |
1224 | 0 | { |
1225 | 0 | thresh = g_tbMax[maxSymbol]; |
1226 | 0 | } |
1227 | |
|
1228 | 0 | int val = 1 << thresh; |
1229 | 0 | int b = maxSymbol - val; |
1230 | 0 | symbol = m_BinDecoder.decodeBinsEP(thresh); |
1231 | |
|
1232 | 0 | if (symbol >= val - b) |
1233 | 0 | { |
1234 | 0 | uint32_t altSymbol; |
1235 | 0 | altSymbol = m_BinDecoder.decodeBinEP(); |
1236 | 0 | symbol <<= 1; |
1237 | 0 | symbol += altSymbol; |
1238 | 0 | symbol -= (val - b); |
1239 | 0 | } |
1240 | 0 | } |
1241 | | |
1242 | | void CABACReader::extend_ref_line( CodingUnit& cu ) |
1243 | 0 | { |
1244 | 0 | if( cu.bdpcmMode() || !cu.sps->getUseMRL() ) |
1245 | 0 | { |
1246 | | //cu.setMultiRefIdx( 0 ); |
1247 | 0 | return; |
1248 | 0 | } |
1249 | | |
1250 | 0 | const bool isFirstLineOfCtu = ( cu.ly() & cu.cs->pcv->maxCUHeightMask ) == 0; |
1251 | |
|
1252 | 0 | if( isFirstLineOfCtu ) |
1253 | 0 | { |
1254 | | //cu.setMultiRefIdx( 0 ); |
1255 | 0 | return; |
1256 | 0 | } |
1257 | | |
1258 | 0 | int multiRefIdx = 0; |
1259 | |
|
1260 | 0 | multiRefIdx = m_BinDecoder.decodeBin( Ctx::MultiRefLineIdx( 0 ) ) == 1 ? MULTI_REF_LINE_IDX[1] : MULTI_REF_LINE_IDX[0]; |
1261 | |
|
1262 | 0 | if( multiRefIdx != MULTI_REF_LINE_IDX[0] ) |
1263 | 0 | { |
1264 | 0 | multiRefIdx = m_BinDecoder.decodeBin( Ctx::MultiRefLineIdx( 1 ) ) == 1 ? MULTI_REF_LINE_IDX[2] : MULTI_REF_LINE_IDX[1]; |
1265 | 0 | } |
1266 | |
|
1267 | 0 | cu.setMultiRefIdx( multiRefIdx ); |
1268 | 0 | } |
1269 | | |
1270 | | void CABACReader::intra_luma_pred_mode( CodingUnit &cu ) |
1271 | 0 | { |
1272 | 0 | if( cu.bdpcmMode() ) |
1273 | 0 | { |
1274 | 0 | cu.intraDir[0] = cu.bdpcmMode() == 2 ? VER_IDX : HOR_IDX; |
1275 | 0 | return; |
1276 | 0 | } |
1277 | | |
1278 | 0 | mip_flag( cu ); |
1279 | |
|
1280 | 0 | if( cu.mipFlag() ) |
1281 | 0 | { |
1282 | 0 | mip_pred_mode( cu ); |
1283 | 0 | return; |
1284 | 0 | } |
1285 | | |
1286 | 0 | extend_ref_line( cu ); |
1287 | |
|
1288 | 0 | isp_mode( cu ); |
1289 | | |
1290 | | // prev_intra_luma_pred_flag |
1291 | 0 | int mpmFlag; |
1292 | |
|
1293 | 0 | if( cu.multiRefIdx() ) |
1294 | 0 | { |
1295 | 0 | mpmFlag = true; |
1296 | 0 | } |
1297 | 0 | else |
1298 | 0 | mpmFlag = m_BinDecoder.decodeBin( Ctx::IPredMode[0]() ); |
1299 | |
|
1300 | 0 | unsigned mpm_pred[NUM_MOST_PROBABLE_MODES]; // mpm_idx / rem_intra_luma_pred_mode |
1301 | |
|
1302 | 0 | PU::getIntraMPMs( cu, mpm_pred ); |
1303 | |
|
1304 | 0 | if( mpmFlag ) |
1305 | 0 | { |
1306 | 0 | uint32_t ipred_idx = 0; |
1307 | |
|
1308 | 0 | unsigned ctx = ( cu.ispMode() == NOT_INTRA_SUBPARTITIONS ? 1 : 0 ); |
1309 | |
|
1310 | 0 | if( cu.multiRefIdx() == 0 ) |
1311 | 0 | ipred_idx = m_BinDecoder.decodeBin( Ctx::IntraLumaPlanarFlag( ctx ) ); |
1312 | 0 | else |
1313 | 0 | ipred_idx = 1; |
1314 | |
|
1315 | 0 | if( ipred_idx ) while( ipred_idx < 5 && m_BinDecoder.decodeBinEP() ) ipred_idx++; |
1316 | |
|
1317 | 0 | cu.intraDir[0] = mpm_pred[ipred_idx]; |
1318 | |
|
1319 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "intra_luma_pred_modes() idx=%d pos=(%d,%d) mode=%d\n", 0, cu.lumaPos().x, cu.lumaPos().y, cu.intraDir[0] ); |
1320 | 0 | } |
1321 | 0 | else |
1322 | 0 | { |
1323 | 0 | unsigned ipred_mode = 0; |
1324 | |
|
1325 | 0 | xReadTruncBinCode(ipred_mode, NUM_LUMA_MODE - NUM_MOST_PROBABLE_MODES); |
1326 | | |
1327 | | //postponed sorting of MPMs (only in remaining branch) |
1328 | 0 | std::sort( mpm_pred, mpm_pred + NUM_MOST_PROBABLE_MODES ); |
1329 | |
|
1330 | 0 | for( uint32_t i = 0; i < NUM_MOST_PROBABLE_MODES; i++ ) |
1331 | 0 | { |
1332 | 0 | ipred_mode += ( ipred_mode >= mpm_pred[i] ); |
1333 | 0 | } |
1334 | |
|
1335 | 0 | cu.intraDir[0] = ipred_mode; |
1336 | |
|
1337 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "intra_luma_pred_modes() idx=%d pos=(%d,%d) mode=%d\n", 0, cu.lumaPos().x, cu.lumaPos().y, cu.intraDir[0] ); |
1338 | 0 | } |
1339 | 0 | } |
1340 | | |
1341 | | void CABACReader::intra_chroma_pred_mode( CodingUnit& cu ) |
1342 | 0 | { |
1343 | 0 | if( cu.bdpcmModeChroma() ) |
1344 | 0 | { |
1345 | 0 | cu.intraDir[1] = cu.bdpcmModeChroma() == 2 ? VER_IDX : HOR_IDX; |
1346 | 0 | return; |
1347 | 0 | } |
1348 | | |
1349 | 0 | if( cu.colorTransform() ) |
1350 | 0 | { |
1351 | 0 | cu.intraDir[CHANNEL_TYPE_CHROMA] = DM_CHROMA_IDX; |
1352 | 0 | return; |
1353 | 0 | } |
1354 | | |
1355 | | // LM chroma mode |
1356 | 0 | if( cu.sps->getUseLMChroma() && CU::checkCCLMAllowed( cu ) ) |
1357 | 0 | { |
1358 | 0 | bool isLMCMode = m_BinDecoder.decodeBin( Ctx::CclmModeFlag( 0 ) ); |
1359 | 0 | if( isLMCMode ) |
1360 | 0 | { |
1361 | 0 | intra_chroma_lmc_mode( cu ); |
1362 | 0 | return; |
1363 | 0 | } |
1364 | 0 | } |
1365 | | |
1366 | 0 | if( m_BinDecoder.decodeBin( Ctx::IPredMode[1]( 0 ) ) == 0 ) |
1367 | 0 | { |
1368 | 0 | cu.intraDir[1] = DM_CHROMA_IDX; |
1369 | 0 | return; |
1370 | 0 | } |
1371 | | |
1372 | 0 | unsigned candId = m_BinDecoder.decodeBinsEP( 2 ); |
1373 | |
|
1374 | 0 | unsigned chromaCandModes[NUM_CHROMA_MODE]; |
1375 | 0 | PU::getIntraChromaCandModes( cu, chromaCandModes ); |
1376 | |
|
1377 | 0 | cu.intraDir[1] = chromaCandModes[candId]; |
1378 | |
|
1379 | 0 | CHECKD( candId >= NUM_CHROMA_MODE, "Chroma prediction mode index out of bounds" ); |
1380 | 0 | CHECKD( PU::isLMCMode( chromaCandModes[candId] ), "The intra dir cannot be LM_CHROMA for this path" ); |
1381 | 0 | CHECKD( chromaCandModes[candId] == DM_CHROMA_IDX, "The intra dir cannot be DM_CHROMA for this path" ); |
1382 | 0 | } |
1383 | | |
1384 | | bool CABACReader::intra_chroma_lmc_mode( CodingUnit& cu ) |
1385 | 0 | { |
1386 | 0 | int lmModeList[10]; |
1387 | 0 | PU::getLMSymbolList( cu, lmModeList ); |
1388 | |
|
1389 | 0 | int symbol = m_BinDecoder.decodeBin( Ctx::CclmModeIdx( 0 ) ); |
1390 | |
|
1391 | 0 | if( symbol == 0 ) |
1392 | 0 | { |
1393 | 0 | cu.intraDir[1] = lmModeList[symbol]; |
1394 | 0 | CHECKD( cu.intraDir[1] != LM_CHROMA_IDX, "should be LM_CHROMA" ); |
1395 | 0 | } |
1396 | 0 | else |
1397 | 0 | { |
1398 | 0 | symbol += m_BinDecoder.decodeBinEP(); |
1399 | 0 | cu.intraDir[1] = lmModeList[symbol]; |
1400 | 0 | } |
1401 | 0 | return true; //it will only enter this function for LMC modes, so always return true ; |
1402 | 0 | } |
1403 | | |
1404 | | void CABACReader::cu_residual( CodingUnit& cu, Partitioner &partitioner, CUCtx& cuCtx ) |
1405 | 0 | { |
1406 | 0 | if( !CU::isIntra( cu ) ) |
1407 | 0 | { |
1408 | 0 | if( !cu.mergeFlag() ) |
1409 | 0 | { |
1410 | 0 | rqt_root_cbf( cu ); |
1411 | 0 | } |
1412 | 0 | else |
1413 | 0 | { |
1414 | 0 | cu.setRootCbf( true ); |
1415 | 0 | } |
1416 | 0 | if( cu.rootCbf() ) |
1417 | 0 | { |
1418 | 0 | sbt_mode( cu ); |
1419 | 0 | } |
1420 | 0 | if( !cu.rootCbf() ) |
1421 | 0 | { |
1422 | | //cu.setColorTransform( false ); |
1423 | 0 | cu.cs->addEmptyTUs( partitioner, cu ); |
1424 | 0 | return; |
1425 | 0 | } |
1426 | 0 | } |
1427 | 0 | else |
1428 | 0 | { |
1429 | 0 | cu.setRootCbf( true ); |
1430 | 0 | } |
1431 | | |
1432 | 0 | if( CU::isInter( cu ) || CU::isIBC( cu ) ) |
1433 | 0 | { |
1434 | 0 | adaptive_color_transform( cu ); |
1435 | 0 | } |
1436 | |
|
1437 | 0 | cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_LUMA] = false; |
1438 | 0 | cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_CHROMA] = false; |
1439 | 0 | cuCtx.lfnstLastScanPos = false; |
1440 | 0 | cuCtx.violatesMtsCoeffConstraint = false; |
1441 | 0 | cuCtx.mtsLastScanPos = false; |
1442 | |
|
1443 | 0 | ChromaCbfs chromaCbfs; |
1444 | 0 | transform_tree( *cu.cs, cu, partitioner, cuCtx ); |
1445 | |
|
1446 | 0 | residual_lfnst_mode( cu, cuCtx ); |
1447 | 0 | mts_idx ( cu, cuCtx ); |
1448 | |
|
1449 | 0 | bool rootCbf = false; |
1450 | |
|
1451 | 0 | for( const auto& blk : cu.blocks ) |
1452 | 0 | { |
1453 | 0 | if( blk.valid() ) rootCbf |= cu.planeCbf( blk.compID() ); |
1454 | 0 | } |
1455 | |
|
1456 | 0 | cu.setRootCbf( rootCbf ); |
1457 | 0 | } |
1458 | | |
1459 | | void CABACReader::rqt_root_cbf( CodingUnit& cu ) |
1460 | 0 | { |
1461 | 0 | cu.setRootCbf( !!m_BinDecoder.decodeBin( Ctx::QtRootCbf() ) ); |
1462 | |
|
1463 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "rqt_root_cbf() ctx=0 root_cbf=%d pos=(%d,%d)\n", cu.rootCbf() ? 1 : 0, cu.lumaPos().x, cu.lumaPos().y ); |
1464 | 0 | } |
1465 | | |
1466 | | void CABACReader::adaptive_color_transform( CodingUnit& cu ) |
1467 | 0 | { |
1468 | 0 | if( !cu.sps->getUseColorTrans() || CU::isSepTree( cu ) ) |
1469 | 0 | { |
1470 | 0 | return; |
1471 | 0 | } |
1472 | | |
1473 | 0 | cu.setColorTransform( m_BinDecoder.decodeBin( Ctx::ACTFlag() ) ); |
1474 | 0 | } |
1475 | | |
1476 | | void CABACReader::sbt_mode( CodingUnit& cu ) |
1477 | 0 | { |
1478 | 0 | const uint8_t sbtAllowed = CU::checkAllowedSbt( cu ); |
1479 | |
|
1480 | 0 | if( !sbtAllowed ) |
1481 | 0 | { |
1482 | 0 | return; |
1483 | 0 | } |
1484 | | |
1485 | 0 | SizeType cuWidth = cu.lwidth(); |
1486 | 0 | SizeType cuHeight = cu.lheight(); |
1487 | | |
1488 | | //bin - flag |
1489 | 0 | if( !m_BinDecoder.decodeBin( Ctx::SbtFlag( ( cuWidth * cuHeight <= 256 ) ? 1 : 0 ) ) ) |
1490 | 0 | { |
1491 | 0 | return; |
1492 | 0 | } |
1493 | | |
1494 | 0 | uint8_t sbtVerHalfAllow = CU::targetSbtAllowed( SBT_VER_HALF, sbtAllowed ); |
1495 | 0 | uint8_t sbtHorHalfAllow = CU::targetSbtAllowed( SBT_HOR_HALF, sbtAllowed ); |
1496 | 0 | uint8_t sbtVerQuadAllow = CU::targetSbtAllowed( SBT_VER_QUAD, sbtAllowed ); |
1497 | 0 | uint8_t sbtHorQuadAllow = CU::targetSbtAllowed( SBT_HOR_QUAD, sbtAllowed ); |
1498 | | |
1499 | | //bin - type |
1500 | 0 | bool sbtQuadFlag = false; |
1501 | 0 | if( ( sbtHorHalfAllow || sbtVerHalfAllow ) && ( sbtHorQuadAllow || sbtVerQuadAllow ) ) |
1502 | 0 | { |
1503 | 0 | sbtQuadFlag = m_BinDecoder.decodeBin( Ctx::SbtQuadFlag( 0 ) ); |
1504 | 0 | } |
1505 | 0 | else |
1506 | 0 | { |
1507 | 0 | sbtQuadFlag = 0; |
1508 | 0 | } |
1509 | | |
1510 | | //bin - dir |
1511 | 0 | bool sbtHorFlag = false; |
1512 | 0 | if( ( sbtQuadFlag && sbtVerQuadAllow && sbtHorQuadAllow ) || ( !sbtQuadFlag && sbtVerHalfAllow && sbtHorHalfAllow ) ) //both direction allowed |
1513 | 0 | { |
1514 | 0 | uint8_t ctxIdx = ( cuWidth == cuHeight ) ? 0 : ( cuWidth < cuHeight ? 1 : 2 ); |
1515 | 0 | sbtHorFlag = m_BinDecoder.decodeBin( Ctx::SbtHorFlag( ctxIdx ) ); |
1516 | 0 | } |
1517 | 0 | else |
1518 | 0 | { |
1519 | 0 | sbtHorFlag = ( sbtQuadFlag && sbtHorQuadAllow ) || ( !sbtQuadFlag && sbtHorHalfAllow ); |
1520 | 0 | } |
1521 | |
|
1522 | 0 | CU::setSbtIdx( cu, sbtHorFlag ? ( sbtQuadFlag ? SBT_HOR_QUAD : SBT_HOR_HALF ) : ( sbtQuadFlag ? SBT_VER_QUAD : SBT_VER_HALF ) ); |
1523 | | |
1524 | | //bin - pos |
1525 | 0 | CU::setSbtPos( cu, m_BinDecoder.decodeBin( Ctx::SbtPosFlag() ) ? SBT_POS1 : SBT_POS0 ); |
1526 | |
|
1527 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "sbt_mode() pos=(%d,%d) sbtInfo=%d\n", cu.lx(), cu.ly(), (int) cu.sbtInfo() ); |
1528 | 0 | } |
1529 | | |
1530 | | |
1531 | | bool CABACReader::end_of_ctu( CodingUnit& cu, CUCtx& cuCtx ) |
1532 | 0 | { |
1533 | 0 | const Position rbPos = recalcPosition( cu.chromaFormat, cu.chType(), CHANNEL_TYPE_LUMA, cu.blocks[cu.chType()].bottomRight().offset( 1, 1 ) ); |
1534 | |
|
1535 | 0 | if( ( ( rbPos.x & cu.cs->pcv->maxCUWidthMask ) == 0 || rbPos.x == cu.pps->getPicWidthInLumaSamples() ) |
1536 | 0 | && ( ( rbPos.y & cu.cs->pcv->maxCUHeightMask ) == 0 || rbPos.y == cu.pps->getPicHeightInLumaSamples() ) |
1537 | 0 | && ( !CU::isSepTree( cu ) || cu.chromaFormat == CHROMA_400 || isChroma( cu.chType() ) ) |
1538 | 0 | ) |
1539 | 0 | { |
1540 | 0 | cuCtx.isDQPCoded = ( cu.pps->getUseDQP() && !cuCtx.isDQPCoded ); |
1541 | |
|
1542 | 0 | return false; |
1543 | 0 | } |
1544 | | |
1545 | 0 | return false; |
1546 | 0 | } |
1547 | | |
1548 | | |
1549 | | //================================================================================ |
1550 | | // clause 7.3.11.7 |
1551 | | //-------------------------------------------------------------------------------- |
1552 | | // void prediction_unit ( cu, mrgCtx ) |
1553 | | // void general_merge_flag ( cu ) |
1554 | | // void merge_data ( cu ) |
1555 | | // void affine_flag ( cu ) |
1556 | | // void subblock_merge_flag ( cu ) |
1557 | | // void merge_idx ( cu ) |
1558 | | // void mmvd_merge_idx ( cu ) |
1559 | | // void amvr_mode ( cu ) |
1560 | | // void affine_amvr_mode ( cu ) |
1561 | | // void inter_pred_idc ( cu ) |
1562 | | // void ref_idx ( cu, eRefList ) |
1563 | | // void mvp_flag ( cu, eRefList ) |
1564 | | // void ciip_flag ( cu ) |
1565 | | // void smvd_mode ( cu ) |
1566 | | //================================================================================ |
1567 | | |
1568 | | void CABACReader::prediction_unit( CodingUnit& cu ) |
1569 | 0 | { |
1570 | 0 | if( cu.skip() ) |
1571 | 0 | { |
1572 | 0 | cu.setMergeFlag( true ); |
1573 | 0 | } |
1574 | 0 | else |
1575 | 0 | { |
1576 | 0 | general_merge_flag( cu ); |
1577 | 0 | } |
1578 | |
|
1579 | 0 | if( cu.mergeFlag() ) |
1580 | 0 | { |
1581 | 0 | merge_data( cu ); |
1582 | 0 | } |
1583 | 0 | else if( CU::isIBC( cu ) ) |
1584 | 0 | { |
1585 | 0 | cu.setInterDir ( 1 ); |
1586 | | //cu.setAffineFlag( false ); |
1587 | 0 | cu.refIdx[REF_PIC_LIST_0] = MAX_NUM_REF; |
1588 | 0 | mvd_coding( cu.mv[REF_PIC_LIST_0][0] ); |
1589 | |
|
1590 | 0 | if( cu.sps->getMaxNumIBCMergeCand() == 1 ) |
1591 | 0 | { |
1592 | 0 | cu.mvpIdx[REF_PIC_LIST_0] = 0; |
1593 | 0 | } |
1594 | 0 | else |
1595 | 0 | { |
1596 | 0 | mvp_flag ( cu, REF_PIC_LIST_0 ); |
1597 | 0 | } |
1598 | 0 | } |
1599 | 0 | else |
1600 | 0 | { |
1601 | 0 | inter_pred_idc( cu ); |
1602 | 0 | affine_flag ( cu ); |
1603 | 0 | smvd_mode ( cu ); |
1604 | |
|
1605 | 0 | if( cu.interDir() != 2 /* PRED_L1 */ ) |
1606 | 0 | { |
1607 | 0 | ref_idx ( cu, REF_PIC_LIST_0 ); |
1608 | |
|
1609 | 0 | mvd_coding ( cu.mv[REF_PIC_LIST_0][0] ); |
1610 | 0 | if( cu.affineFlag() ) |
1611 | 0 | { |
1612 | 0 | mvd_coding ( cu.mv[REF_PIC_LIST_0][1] ); |
1613 | 0 | if ( cu.affineType() == AFFINEMODEL_6PARAM ) |
1614 | 0 | { |
1615 | 0 | mvd_coding( cu.mv[REF_PIC_LIST_0][2] ); |
1616 | 0 | } |
1617 | 0 | } |
1618 | |
|
1619 | 0 | mvp_flag ( cu, REF_PIC_LIST_0 ); |
1620 | 0 | } |
1621 | |
|
1622 | 0 | if( cu.interDir() != 1 /* PRED_L0 */ ) |
1623 | 0 | { |
1624 | 0 | if( cu.smvdMode() != 1 ) |
1625 | 0 | { |
1626 | 0 | ref_idx( cu, REF_PIC_LIST_1 ); |
1627 | |
|
1628 | 0 | if( cu.cs->picHeader->getMvdL1ZeroFlag() && cu.interDir() == 3 /* PRED_BI */ ) |
1629 | 0 | { |
1630 | | //cu.mv[REF_PIC_LIST_1][0] = Mv(); |
1631 | | //cu.mv[REF_PIC_LIST_1][1] = Mv(); |
1632 | | //cu.mv[REF_PIC_LIST_1][2] = Mv(); |
1633 | 0 | } |
1634 | 0 | else |
1635 | 0 | { |
1636 | 0 | mvd_coding ( cu.mv[REF_PIC_LIST_1][0] ); |
1637 | 0 | if( cu.affineFlag() ) |
1638 | 0 | { |
1639 | 0 | mvd_coding ( cu.mv[REF_PIC_LIST_1][1] ); |
1640 | 0 | if( cu.affineType() == AFFINEMODEL_6PARAM ) |
1641 | 0 | { |
1642 | 0 | mvd_coding( cu.mv[REF_PIC_LIST_1][2] ); |
1643 | 0 | } |
1644 | 0 | } |
1645 | 0 | } |
1646 | 0 | } |
1647 | |
|
1648 | 0 | mvp_flag ( cu, REF_PIC_LIST_1 ); |
1649 | 0 | } |
1650 | 0 | } |
1651 | |
|
1652 | 0 | if( cu.smvdMode() ) |
1653 | 0 | { |
1654 | 0 | RefPicList eCurRefList = ( RefPicList ) ( cu.smvdMode() - 1 ); |
1655 | 0 | cu.mv [1 - eCurRefList][0] . set( -cu.mv[eCurRefList][0].hor, -cu.mv[eCurRefList][0].ver ); |
1656 | 0 | cu.refIdx[1 - eCurRefList] = cu.slice->getSymRefIdx( 1 - eCurRefList ); |
1657 | |
|
1658 | 0 | CHECKD( !( ( cu.mv[1 - eCurRefList][0].getHor() >= MVD_MIN ) && ( cu.mv[1 - eCurRefList][0].getHor() <= MVD_MAX ) ) || !( ( cu.mv[1 - eCurRefList][0].getVer() >= MVD_MIN ) && ( cu.mv[1 - eCurRefList][0].getVer() <= MVD_MAX ) ), "Illegal MVD value" ); |
1659 | 0 | } |
1660 | 0 | } |
1661 | | |
1662 | | void CABACReader::smvd_mode( CodingUnit& cu ) |
1663 | 0 | { |
1664 | | //cu.setSmvdMode( 0 ); |
1665 | |
|
1666 | 0 | if( cu.interDir() != 3 || cu.affineFlag() || !cu.sps->getUseSMVD() || cu.cs->picHeader->getMvdL1ZeroFlag() ) |
1667 | 0 | { |
1668 | 0 | return; |
1669 | 0 | } |
1670 | | |
1671 | 0 | if( cu.slice->getBiDirPred() == false ) |
1672 | 0 | { |
1673 | 0 | return; |
1674 | 0 | } |
1675 | | |
1676 | 0 | cu.setSmvdMode( m_BinDecoder.decodeBin( Ctx::SmvdFlag() ) ? 1 : 0 ); |
1677 | |
|
1678 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "symmvd_flag() symmvd=%d pos=(%d,%d) size=%dx%d\n", cu.smvdMode() ? 1 : 0, cu.lumaPos().x, cu.lumaPos().y, cu.lumaSize().width, cu.lumaSize().height ); |
1679 | 0 | } |
1680 | | |
1681 | | void CABACReader::subblock_merge_flag( CodingUnit& cu ) |
1682 | 0 | { |
1683 | | //cu.setAffineFlag( false ); |
1684 | |
|
1685 | 0 | if( !cu.slice->isIntra() && ( cu.slice->getPicHeader()->getMaxNumAffineMergeCand() > 0 ) && cu.lwidth() >= 8 && cu.lheight() >= 8 ) |
1686 | 0 | { |
1687 | 0 | unsigned ctxId = DeriveCtx::CtxAffineFlag( cu ); |
1688 | 0 | cu.setAffineFlag( m_BinDecoder.decodeBin( Ctx::SubblockMergeFlag( ctxId ) ) ); |
1689 | |
|
1690 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "subblock_merge_flag() subblock_merge_flag=%d ctx=%d pos=(%d,%d)\n", cu.affineFlag() ? 1 : 0, ctxId, cu.Y().x, cu.Y().y ); |
1691 | 0 | } |
1692 | 0 | } |
1693 | | |
1694 | | void CABACReader::affine_flag( CodingUnit& cu ) |
1695 | 0 | { |
1696 | 0 | if( cu.sps->getUseAffine() && cu.lumaSize().width >=16 && cu.lumaSize().height >= 16 ) |
1697 | 0 | { |
1698 | 0 | unsigned ctxId = DeriveCtx::CtxAffineFlag( cu ); |
1699 | 0 | cu.setAffineFlag( m_BinDecoder.decodeBin( Ctx::AffineFlag( ctxId ) ) ); |
1700 | |
|
1701 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "affine_flag() affine=%d ctx=%d pos=(%d,%d)\n", cu.affineFlag() ? 1 : 0, ctxId, cu.Y().x, cu.Y().y ); |
1702 | |
|
1703 | 0 | if( cu.affineFlag() && cu.sps->getUseAffineType() ) |
1704 | 0 | { |
1705 | 0 | ctxId = 0; |
1706 | 0 | cu.setAffineType( ( AffineModel ) m_BinDecoder.decodeBin( Ctx::AffineType( ctxId ) ) ); |
1707 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "affine_type() affine_type=%d ctx=%d pos=(%d,%d)\n", cu.affineType() ? 1 : 0, ctxId, cu.Y().x, cu.Y().y ); |
1708 | 0 | } |
1709 | 0 | else |
1710 | 0 | { |
1711 | | //cu.setAffineType( AFFINEMODEL_4PARAM ); |
1712 | 0 | } |
1713 | 0 | } |
1714 | 0 | } |
1715 | | |
1716 | | void CABACReader::general_merge_flag( CodingUnit& cu ) |
1717 | 0 | { |
1718 | 0 | cu.setMergeFlag( m_BinDecoder.decodeBin( Ctx::MergeFlag() ) ); |
1719 | |
|
1720 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "merge_flag() merge=%d pos=(%d,%d) size=%dx%d\n", cu.mergeFlag() ? 1 : 0, cu.lumaPos().x, cu.lumaPos().y, cu.lumaSize().width, cu.lumaSize().height ); |
1721 | | |
1722 | | //if( cu.mergeFlag() && CU::isIBC( cu ) ) |
1723 | | //{ |
1724 | | // cu.setMmvdFlag ( false ); |
1725 | | // cu.setRegularMergeFlag( false ); |
1726 | | // |
1727 | | // return; |
1728 | | //} |
1729 | 0 | } |
1730 | | |
1731 | | |
1732 | | void CABACReader::merge_data( CodingUnit& cu ) |
1733 | 0 | { |
1734 | 0 | if( CU::isIBC( cu ) ) |
1735 | 0 | { |
1736 | 0 | merge_idx( cu ); |
1737 | 0 | return; |
1738 | 0 | } |
1739 | 0 | else |
1740 | 0 | { |
1741 | 0 | subblock_merge_flag( cu ); |
1742 | |
|
1743 | 0 | if( cu.affineFlag() ) |
1744 | 0 | { |
1745 | 0 | merge_idx( cu ); |
1746 | 0 | return; |
1747 | 0 | } |
1748 | | |
1749 | 0 | bool regularMerge = true; |
1750 | |
|
1751 | 0 | const bool ciipAvailable = cu.sps->getUseCiip() && !cu.skip() && cu.lwidth() < 128 && cu.lheight() < 128 && cu.Y().area() >= 64; |
1752 | 0 | const bool geoAvailable = cu.sps->getUseGeo() && cu.slice->isInterB() |
1753 | 0 | && cu.lwidth() >= GEO_MIN_CU_SIZE && cu.lheight() >= GEO_MIN_CU_SIZE |
1754 | 0 | && cu.lwidth() <= GEO_MAX_CU_SIZE && cu.lheight() <= GEO_MAX_CU_SIZE |
1755 | 0 | && cu.lwidth() < 8 * cu.lheight() && cu.lheight() < 8 * cu.lwidth(); |
1756 | |
|
1757 | 0 | if( geoAvailable || ciipAvailable ) |
1758 | 0 | { |
1759 | 0 | regularMerge = m_BinDecoder.decodeBin( Ctx::RegularMergeFlag( cu.skip() ? 0 : 1 ) ); |
1760 | 0 | } |
1761 | |
|
1762 | 0 | if( regularMerge ) |
1763 | 0 | { |
1764 | 0 | if( cu.sps->getUseMMVD() ) |
1765 | 0 | { |
1766 | 0 | cu.setMmvdFlag( m_BinDecoder.decodeBin( Ctx::MmvdFlag( 0 ) ) ); |
1767 | 0 | } |
1768 | 0 | else |
1769 | 0 | { |
1770 | | //cu.setMmvdFlag( false ); |
1771 | 0 | } |
1772 | 0 | } |
1773 | 0 | else |
1774 | 0 | { |
1775 | | //cu.setMmvdFlag( false ); |
1776 | 0 | if( geoAvailable && ciipAvailable ) |
1777 | 0 | { |
1778 | 0 | ciip_flag( cu ); |
1779 | 0 | } |
1780 | 0 | else if( ciipAvailable ) |
1781 | 0 | { |
1782 | 0 | cu.setCiipFlag( true ); |
1783 | 0 | } |
1784 | 0 | else |
1785 | 0 | { |
1786 | | //cu.setCiipFlag( false ); |
1787 | 0 | } |
1788 | |
|
1789 | 0 | if( cu.ciipFlag() ) |
1790 | 0 | { |
1791 | 0 | cu.intraDir[0] = PLANAR_IDX; |
1792 | 0 | cu.intraDir[1] = DM_CHROMA_IDX; |
1793 | 0 | } |
1794 | 0 | else |
1795 | 0 | { |
1796 | 0 | cu.setGeoFlag( true ); |
1797 | 0 | } |
1798 | 0 | } |
1799 | 0 | } |
1800 | | |
1801 | 0 | if( cu.mmvdFlag() ) |
1802 | 0 | mmvd_merge_idx( cu ); |
1803 | 0 | else |
1804 | 0 | merge_idx ( cu ); |
1805 | 0 | } |
1806 | | |
1807 | | |
1808 | | void CABACReader::merge_idx( CodingUnit& cu ) |
1809 | 0 | { |
1810 | 0 | if( cu.geoFlag() ) |
1811 | 0 | { |
1812 | 0 | uint32_t splitDir = 0; |
1813 | 0 | xReadTruncBinCode( splitDir, GEO_NUM_PARTITION_MODE ); |
1814 | 0 | cu.geoSplitDir = splitDir; |
1815 | |
|
1816 | 0 | const int maxNumGeoCand = cu.sps->getMaxNumGeoCand(); |
1817 | 0 | const int numCandminus2 = maxNumGeoCand - 2; |
1818 | |
|
1819 | 0 | CHECK( maxNumGeoCand < 2, "Incorrect max number of geo candidates" ); |
1820 | |
|
1821 | 0 | int mergeCand0 = 0; |
1822 | 0 | int mergeCand1 = 0; |
1823 | |
|
1824 | 0 | if( m_BinDecoder.decodeBin( Ctx::MergeIdx() ) ) |
1825 | 0 | { |
1826 | 0 | mergeCand0 += unary_max_eqprob( numCandminus2 ) + 1; |
1827 | 0 | } |
1828 | |
|
1829 | 0 | if( numCandminus2 > 0 ) |
1830 | 0 | { |
1831 | 0 | if( m_BinDecoder.decodeBin( Ctx::MergeIdx() ) ) |
1832 | 0 | { |
1833 | 0 | mergeCand1 += unary_max_eqprob( numCandminus2 - 1 ) + 1; |
1834 | 0 | } |
1835 | 0 | } |
1836 | |
|
1837 | 0 | mergeCand1 += mergeCand1 >= mergeCand0 ? 1 : 0; |
1838 | |
|
1839 | 0 | cu.setGeoMergeIdx0( mergeCand0 ); |
1840 | 0 | cu.setGeoMergeIdx1( mergeCand1 ); |
1841 | |
|
1842 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "merge_idx() geo_split_dir=%d\n", splitDir ); |
1843 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "merge_idx() geo_idx0=%d\n", mergeCand0 ); |
1844 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "merge_idx() geo_idx1=%d\n", mergeCand1 ); |
1845 | 0 | } |
1846 | 0 | else |
1847 | 0 | { |
1848 | 0 | int numCandminus1; |
1849 | 0 | int ctxIdx = Ctx::MergeIdx(); |
1850 | |
|
1851 | 0 | if( cu.predMode() == MODE_IBC ) |
1852 | 0 | { |
1853 | 0 | numCandminus1 = int( cu.sps->getMaxNumIBCMergeCand() ) - 1; |
1854 | 0 | } |
1855 | 0 | else if( cu.affineFlag() ) |
1856 | 0 | { |
1857 | 0 | numCandminus1 = int( cu.cs->picHeader->getMaxNumAffineMergeCand() ) - 1; |
1858 | 0 | ctxIdx = Ctx::AffMergeIdx(); |
1859 | 0 | } |
1860 | 0 | else |
1861 | 0 | { |
1862 | 0 | numCandminus1 = int( cu.sps->getMaxNumMergeCand() ) - 1; |
1863 | 0 | } |
1864 | |
|
1865 | 0 | int mergeIdx = 0; |
1866 | 0 | if( numCandminus1 > 0 && m_BinDecoder.decodeBin( ctxIdx ) ) |
1867 | 0 | { |
1868 | 0 | for( mergeIdx++; mergeIdx < numCandminus1 && m_BinDecoder.decodeBinEP(); mergeIdx++ ); |
1869 | 0 | } |
1870 | |
|
1871 | 0 | cu.setMergeIdx( mergeIdx ); |
1872 | | #if ENABLE_TRACING |
1873 | | |
1874 | | if( cu.affineFlag() ) |
1875 | | DTRACE( g_trace_ctx, D_SYNTAX, "aff_merge_idx() aff_merge_idx=%d\n", cu.mergeIdx() ); |
1876 | | else |
1877 | | DTRACE( g_trace_ctx, D_SYNTAX, "merge_idx() merge_idx=%d\n", cu.mergeIdx() ); |
1878 | | #endif |
1879 | 0 | } |
1880 | 0 | } |
1881 | | |
1882 | | void CABACReader::mmvd_merge_idx( CodingUnit& cu ) |
1883 | 0 | { |
1884 | 0 | int var0 = 0, var1 = 0, var2 = 0; |
1885 | |
|
1886 | 0 | const int numCand = int( cu.sps->getMaxNumMergeCand() ); |
1887 | 0 | const int numCandminus1_base = ( numCand > 1 ) ? MMVD_BASE_MV_NUM - 1 : 0; |
1888 | |
|
1889 | 0 | if( numCandminus1_base > 0 && m_BinDecoder.decodeBin( Ctx::MmvdMergeIdx() ) ) |
1890 | 0 | { |
1891 | 0 | for( var0++; var0 < numCandminus1_base && m_BinDecoder.decodeBinEP(); var0++ ); |
1892 | 0 | } |
1893 | |
|
1894 | 0 | const int numCandminus1_step = MMVD_REFINE_STEP - 1; |
1895 | 0 | if( numCandminus1_step > 0 && m_BinDecoder.decodeBin( Ctx::MmvdStepMvpIdx() ) ) |
1896 | 0 | { |
1897 | 0 | for( var1++; var1 < numCandminus1_step && m_BinDecoder.decodeBinEP(); var1++ ); |
1898 | 0 | } |
1899 | |
|
1900 | 0 | if( m_BinDecoder.decodeBinEP() ) |
1901 | 0 | { |
1902 | 0 | var2 += 2; |
1903 | 0 | } |
1904 | 0 | if( m_BinDecoder.decodeBinEP() ) |
1905 | 0 | { |
1906 | 0 | var2 += 1; |
1907 | 0 | } |
1908 | |
|
1909 | 0 | cu.mmvdIdx = ( var0 * MMVD_MAX_REFINE_NUM + var1 * 4 + var2 ); |
1910 | |
|
1911 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "base_mvp_idx() base_mvp_idx=%d\n", var0 ); |
1912 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "MmvdStepMvpIdx() MmvdStepMvpIdx=%d\n", var1 ); |
1913 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "pos() pos=%d\n", var2 ); |
1914 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "mmvd_merge_idx() mmvd_merge_idx=%d\n", cu.mmvdIdx ); |
1915 | 0 | } |
1916 | | |
1917 | | void CABACReader::inter_pred_idc( CodingUnit& cu ) |
1918 | 0 | { |
1919 | 0 | if( cu.slice->isInterP() ) |
1920 | 0 | { |
1921 | 0 | cu.setInterDir( 1 ); |
1922 | 0 | return; |
1923 | 0 | } |
1924 | | |
1925 | 0 | if( !PU::isBipredRestriction( cu ) ) |
1926 | 0 | { |
1927 | 0 | unsigned ctxId = DeriveCtx::CtxInterDir( cu ); |
1928 | 0 | if( m_BinDecoder.decodeBin( Ctx::InterDir( ctxId ) ) ) |
1929 | 0 | { |
1930 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "inter_pred_idc() ctx=%d value=%d pos=(%d,%d)\n", ctxId, 3, cu.lumaPos().x, cu.lumaPos().y ); |
1931 | 0 | cu.setInterDir( 3 ); |
1932 | 0 | return; |
1933 | 0 | } |
1934 | 0 | } |
1935 | | |
1936 | 0 | if( m_BinDecoder.decodeBin( Ctx::InterDir( 5 ) ) ) |
1937 | 0 | { |
1938 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "inter_pred_idc() ctx=5 value=%d pos=(%d,%d)\n", 2, cu.lumaPos().x, cu.lumaPos().y ); |
1939 | 0 | cu.setInterDir( 2 ); |
1940 | 0 | return; |
1941 | 0 | } |
1942 | | |
1943 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "inter_pred_idc() ctx=5 value=%d pos=(%d,%d)\n", 1, cu.lumaPos().x, cu.lumaPos().y ); |
1944 | 0 | cu.setInterDir( 1 ); |
1945 | 0 | } |
1946 | | |
1947 | | |
1948 | | void CABACReader::ref_idx( CodingUnit &cu, RefPicList eRefList ) |
1949 | 0 | { |
1950 | 0 | if( cu.smvdMode() ) |
1951 | 0 | { |
1952 | 0 | cu.refIdx[eRefList] = cu.slice->getSymRefIdx( eRefList ); |
1953 | 0 | return; |
1954 | 0 | } |
1955 | | |
1956 | 0 | const int numRef = cu.slice->getNumRefIdx( eRefList ); |
1957 | |
|
1958 | 0 | if( numRef <= 1 || !m_BinDecoder.decodeBin( Ctx::RefPic( 0 ) ) ) |
1959 | 0 | { |
1960 | 0 | if( numRef > 1 ) |
1961 | 0 | { |
1962 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "ref_idx() value=%d pos=(%d,%d)\n", 0, cu.lumaPos().x, cu.lumaPos().y ); |
1963 | 0 | } |
1964 | 0 | cu.refIdx[eRefList] = 0; |
1965 | 0 | return; |
1966 | 0 | } |
1967 | | |
1968 | 0 | if( numRef <= 2 || !m_BinDecoder.decodeBin( Ctx::RefPic( 1 ) ) ) |
1969 | 0 | { |
1970 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "ref_idx() value=%d pos=(%d,%d)\n", 1, cu.lumaPos().x, cu.lumaPos().y ); |
1971 | 0 | cu.refIdx[eRefList] = 1; |
1972 | 0 | return; |
1973 | 0 | } |
1974 | | |
1975 | 0 | for( int idx = 3; ; idx++ ) |
1976 | 0 | { |
1977 | 0 | if( numRef <= idx || !m_BinDecoder.decodeBinEP() ) |
1978 | 0 | { |
1979 | 0 | cu.refIdx[eRefList] = ( signed char ) ( idx - 1 ); |
1980 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "ref_idx() value=%d pos=(%d,%d)\n", idx-1, cu.lumaPos().x, cu.lumaPos().y ); |
1981 | 0 | return; |
1982 | 0 | } |
1983 | 0 | } |
1984 | 0 | } |
1985 | | |
1986 | | |
1987 | | |
1988 | | void CABACReader::mvp_flag( CodingUnit& cu, RefPicList eRefList ) |
1989 | 0 | { |
1990 | 0 | unsigned mvp_idx = m_BinDecoder.decodeBin( Ctx::MVPIdx() ); |
1991 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "mvp_flag() value=%d pos=(%d,%d)\n", mvp_idx, cu.lumaPos().x, cu.lumaPos().y ); |
1992 | 0 | cu.mvpIdx [eRefList] = mvp_idx; |
1993 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "mvpIdx(refList:%d)=%d\n", eRefList, mvp_idx ); |
1994 | 0 | } |
1995 | | |
1996 | | void CABACReader::ciip_flag( CodingUnit& cu ) |
1997 | 0 | { |
1998 | 0 | cu.setCiipFlag( m_BinDecoder.decodeBin( Ctx::CiipFlag() ) ); |
1999 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "ciip_flag() Ciip=%d pos=(%d,%d) size=%dx%d\n", cu.ciipFlag() ? 1 : 0, cu.lumaPos().x, cu.lumaPos().y, cu.lumaSize().width, cu.lumaSize().height ); |
2000 | 0 | } |
2001 | | |
2002 | | |
2003 | | |
2004 | | //================================================================================ |
2005 | | // clause 7.3.8.8 |
2006 | | //-------------------------------------------------------------------------------- |
2007 | | // void transform_tree ( cs, area, cuCtx, chromaCbfs ) |
2008 | | // bool split_transform_flag( depth ) |
2009 | | // bool cbf_comp ( area, depth ) |
2010 | | //================================================================================ |
2011 | | |
2012 | | void CABACReader::transform_tree( CodingStructure &cs, CodingUnit &cu, Partitioner &partitioner, CUCtx& cuCtx ) |
2013 | 0 | { |
2014 | 0 | const UnitArea& area = partitioner.currArea(); |
2015 | | |
2016 | | // split_transform_flag |
2017 | 0 | bool split = area.Y().width > partitioner.maxTrSize || area.Y().height > partitioner.maxTrSize; |
2018 | 0 | const PartSplit ispType = CU::getISPType( cu, getFirstComponentOfChannel( partitioner.chType ) ); |
2019 | |
|
2020 | 0 | split |= ( cu.sbtInfo() || ispType != TU_NO_ISP ) && partitioner.currTrDepth == 0; |
2021 | |
|
2022 | 0 | if( split ) |
2023 | 0 | { |
2024 | 0 | if( ispType == TU_NO_ISP && !cu.sbtInfo() ) |
2025 | 0 | { |
2026 | | #if ENABLE_TRACING |
2027 | | const CompArea& tuArea = partitioner.currArea().blocks[partitioner.chType]; |
2028 | | DTRACE( g_trace_ctx, D_SYNTAX, "transform_tree() maxTrSplit chType=%d pos=(%d,%d) size=%dx%d\n", |
2029 | | partitioner.chType, tuArea.x, tuArea.y, tuArea.width, tuArea.height ); |
2030 | | |
2031 | | #endif |
2032 | 0 | partitioner.splitCurrArea( TU_MAX_TR_SPLIT, cs ); |
2033 | 0 | } |
2034 | 0 | else if( ispType != TU_NO_ISP ) |
2035 | 0 | { |
2036 | 0 | partitioner.splitCurrArea( ispType, cs ); |
2037 | 0 | } |
2038 | 0 | else if( cu.sbtInfo() ) |
2039 | 0 | { |
2040 | 0 | partitioner.splitCurrArea( PartSplit( CU::getSbtTuSplit( cu ) ), cs ); |
2041 | 0 | } |
2042 | 0 | else |
2043 | 0 | THROW_RECOVERABLE( "Implicit TU split not available!" ); |
2044 | | |
2045 | 0 | do |
2046 | 0 | { |
2047 | 0 | transform_tree( cs, cu, partitioner, cuCtx ); |
2048 | 0 | } while( partitioner.nextPart( cs ) ); |
2049 | |
|
2050 | 0 | partitioner.exitCurrSplit( cs ); |
2051 | 0 | } |
2052 | 0 | else |
2053 | 0 | { |
2054 | 0 | TransformUnit &tu = cs.addTU( getArea( *m_slice, area, partitioner.chType, partitioner.treeType ), partitioner.chType, cu ); |
2055 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "transform_unit() pos=(%d,%d) size=%dx%d depth=%d trDepth=%d\n", tu.blocks[tu.chType()].x, tu.blocks[tu.chType()].y, tu.blocks[tu.chType()].width, tu.blocks[tu.chType()].height, cu.depth, partitioner.currTrDepth ); |
2056 | |
|
2057 | 0 | transform_unit( tu, cuCtx, partitioner ); |
2058 | 0 | } |
2059 | 0 | } |
2060 | | |
2061 | | bool CABACReader::cbf_comp( CodingUnit& cu, const CompArea& area, unsigned depth, const bool prevCbf, const bool useISP ) |
2062 | 0 | { |
2063 | 0 | const CtxSet& ctxSet = Ctx::QtCbf[ area.compID()]; |
2064 | |
|
2065 | 0 | unsigned cbf = 0; |
2066 | 0 | if( ( area.compID() == COMPONENT_Y && cu.bdpcmMode() ) || ( area.compID() != COMPONENT_Y && cu.bdpcmModeChroma() ) ) |
2067 | 0 | { |
2068 | 0 | if( area.compID() == COMPONENT_Cr ) |
2069 | 0 | cbf = m_BinDecoder.decodeBin( ctxSet( 2 ) ); |
2070 | 0 | else |
2071 | 0 | cbf = m_BinDecoder.decodeBin( ctxSet( 1 ) ); |
2072 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "cbf_comp() etype=%d pos=(%d,%d) ctx=%d cbf=%d\n", area.compID(), area.x, area.y, area.compID() == COMPONENT_Cr ? 2 : 1, cbf ); |
2073 | 0 | } |
2074 | 0 | else |
2075 | 0 | { |
2076 | 0 | const unsigned ctxId = DeriveCtx::CtxQtCbf( area.compID(), prevCbf, useISP && isLuma( area.compID() ) ); |
2077 | 0 | cbf = m_BinDecoder.decodeBin( ctxSet( ctxId ) ); |
2078 | |
|
2079 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "cbf_comp() etype=%d pos=(%d,%d) ctx=%d cbf=%d\n", area.compID(), area.x, area.y, ctxId, cbf ); |
2080 | 0 | } |
2081 | |
|
2082 | 0 | return cbf; |
2083 | 0 | } |
2084 | | |
2085 | | |
2086 | | |
2087 | | |
2088 | | |
2089 | | //================================================================================ |
2090 | | // clause 7.3.8.9 |
2091 | | //-------------------------------------------------------------------------------- |
2092 | | // void mvd_coding( cu, refList ) |
2093 | | //================================================================================ |
2094 | | |
2095 | | void CABACReader::mvd_coding( Mv &rMvd ) |
2096 | 0 | { |
2097 | | // abs_mvd_greater0_flag[ 0 | 1 ] |
2098 | 0 | int horAbs = (int)m_BinDecoder.decodeBin(Ctx::Mvd()); |
2099 | 0 | int verAbs = (int)m_BinDecoder.decodeBin(Ctx::Mvd()); |
2100 | | |
2101 | | // abs_mvd_greater1_flag[ 0 | 1 ] |
2102 | 0 | if (horAbs) |
2103 | 0 | { |
2104 | 0 | horAbs += (int)m_BinDecoder.decodeBin(Ctx::Mvd(1)); |
2105 | 0 | } |
2106 | 0 | if (verAbs) |
2107 | 0 | { |
2108 | 0 | verAbs += (int)m_BinDecoder.decodeBin(Ctx::Mvd(1)); |
2109 | 0 | } |
2110 | | |
2111 | | // abs_mvd_minus2[ 0 | 1 ] and mvd_sign_flag[ 0 | 1 ] |
2112 | 0 | if (horAbs) |
2113 | 0 | { |
2114 | 0 | if (horAbs > 1) |
2115 | 0 | { |
2116 | 0 | horAbs += m_BinDecoder.decodeRemAbsEP(1, 0, MV_BITS - 1); |
2117 | 0 | } |
2118 | 0 | if (m_BinDecoder.decodeBinEP()) |
2119 | 0 | { |
2120 | 0 | horAbs = -horAbs; |
2121 | 0 | } |
2122 | 0 | } |
2123 | 0 | if (verAbs) |
2124 | 0 | { |
2125 | 0 | if (verAbs > 1) |
2126 | 0 | { |
2127 | 0 | verAbs += m_BinDecoder.decodeRemAbsEP(1, 0, MV_BITS - 1); |
2128 | 0 | } |
2129 | 0 | if (m_BinDecoder.decodeBinEP()) |
2130 | 0 | { |
2131 | 0 | verAbs = -verAbs; |
2132 | 0 | } |
2133 | 0 | } |
2134 | 0 | rMvd = Mv(horAbs, verAbs); |
2135 | |
|
2136 | 0 | CHECKD( !( ( horAbs >= MVD_MIN ) && ( horAbs <= MVD_MAX ) ) || !( ( verAbs >= MVD_MIN ) && ( verAbs <= MVD_MAX ) ), "Illegal MVD value" ); |
2137 | 0 | } |
2138 | | |
2139 | | |
2140 | | //================================================================================ |
2141 | | // clause 7.3.8.10 |
2142 | | //-------------------------------------------------------------------------------- |
2143 | | // void transform_unit ( tu, cuCtx, chromaCbfs ) |
2144 | | // void cu_qp_delta ( cu ) |
2145 | | // void cu_chroma_qp_offset ( cu ) |
2146 | | //================================================================================ |
2147 | | |
2148 | | void CABACReader::transform_unit( TransformUnit& tu, CUCtx& cuCtx, Partitioner& partitioner ) |
2149 | 0 | { |
2150 | 0 | const UnitArea& area = partitioner.currArea(); |
2151 | 0 | const unsigned trDepth = partitioner.currTrDepth; |
2152 | |
|
2153 | 0 | CodingUnit& cu = *tu.cu; |
2154 | 0 | ChromaCbfs chromaCbfs; |
2155 | |
|
2156 | 0 | const bool chromaCbfISP = isChromaEnabled( area.chromaFormat ) && area.blocks[COMPONENT_Cb].valid() && cu.ispMode(); |
2157 | 0 | const bool tuNoResidual = TU::checkTuNoResidual( tu, partitioner.currPartIdx() ); |
2158 | | |
2159 | | // cbf_cb & cbf_cr |
2160 | 0 | if( area.chromaFormat != CHROMA_400 && area.blocks[COMPONENT_Cb].valid() && ( !CU::isSepTree( cu ) || partitioner.chType == CHANNEL_TYPE_CHROMA ) && ( !cu.ispMode() || chromaCbfISP ) ) |
2161 | 0 | { |
2162 | 0 | const int cbfDepth = chromaCbfISP ? trDepth - 1 : trDepth; |
2163 | |
|
2164 | 0 | if( !( cu.sbtInfo() && tuNoResidual ) ) |
2165 | 0 | { |
2166 | 0 | chromaCbfs.Cb = cbf_comp( cu, area.blocks[COMPONENT_Cb], cbfDepth ); |
2167 | 0 | chromaCbfs.Cr = cbf_comp( cu, area.blocks[COMPONENT_Cr], cbfDepth, chromaCbfs.Cb ); |
2168 | 0 | } |
2169 | 0 | } |
2170 | |
|
2171 | 0 | if( !isChroma( partitioner.chType ) ) |
2172 | 0 | { |
2173 | 0 | if( !CU::isIntra( cu ) && trDepth == 0 && !chromaCbfs.sigChroma( area.chromaFormat ) ) |
2174 | 0 | { |
2175 | 0 | TU::setCbf( tu, COMPONENT_Y, true ); |
2176 | 0 | } |
2177 | 0 | else if( cu.sbtInfo() && tuNoResidual ) |
2178 | 0 | { |
2179 | 0 | TU::setCbf( tu, COMPONENT_Y, false ); |
2180 | 0 | } |
2181 | 0 | else if( cu.sbtInfo() && !chromaCbfs.sigChroma( area.chromaFormat ) ) |
2182 | 0 | { |
2183 | 0 | CHECKD( tuNoResidual, "wrong" ); |
2184 | 0 | TU::setCbf( tu, COMPONENT_Y, true ); |
2185 | 0 | } |
2186 | 0 | else |
2187 | 0 | { |
2188 | 0 | bool cbfY = true; |
2189 | |
|
2190 | 0 | if( cu.ispMode() ) |
2191 | 0 | { |
2192 | 0 | bool lumaCbfIsInferredACT = ( cu.colorTransform() && cu.predMode() == MODE_INTRA && trDepth == 0 && !chromaCbfs.sigChroma( area.chromaFormat ) ); |
2193 | 0 | bool lastCbfIsInferred = lumaCbfIsInferredACT; // ISP and ACT are mutually exclusive |
2194 | 0 | bool rootCbfSoFar = false; |
2195 | |
|
2196 | 0 | int nTus = cu.ispMode() == HOR_INTRA_SUBPARTITIONS ? cu.lheight() >> getLog2( tu.lheight() ) : cu.lwidth() >> getLog2( tu.lwidth() ); |
2197 | 0 | int idx = partitioner.currPartIdx(); |
2198 | 0 | if( idx == nTus - 1 ) |
2199 | 0 | { |
2200 | 0 | for( const TransformUnit &currTu : cTUTraverser( &cu.firstTU, cu.lastTU ) ) |
2201 | 0 | { |
2202 | 0 | rootCbfSoFar |= TU::getCbf( currTu, COMPONENT_Y ); |
2203 | 0 | } |
2204 | |
|
2205 | 0 | if( !rootCbfSoFar ) |
2206 | 0 | { |
2207 | 0 | lastCbfIsInferred = true; |
2208 | 0 | } |
2209 | 0 | } |
2210 | |
|
2211 | 0 | if( !lastCbfIsInferred ) |
2212 | 0 | { |
2213 | 0 | cbfY = cbf_comp( cu, tu.Y(), trDepth, TU::getPrevTUCbf( tu, COMPONENT_Y ), cu.ispMode() ); |
2214 | 0 | } |
2215 | 0 | else |
2216 | 0 | { |
2217 | 0 | cbfY = true; |
2218 | 0 | } |
2219 | 0 | } |
2220 | 0 | else |
2221 | 0 | { |
2222 | 0 | cbfY = cbf_comp( cu, tu.Y(), trDepth, false, NOT_INTRA_SUBPARTITIONS ); |
2223 | 0 | } |
2224 | 0 | TU::setCbf( tu, COMPONENT_Y, cbfY ? 1 : 0 ); |
2225 | 0 | } |
2226 | 0 | } |
2227 | | |
2228 | 0 | if( area.chromaFormat != CHROMA_400 && ( !cu.ispMode() || chromaCbfISP ) ) |
2229 | 0 | { |
2230 | 0 | TU::setCbf( tu, COMPONENT_Cb, chromaCbfs.Cb ); |
2231 | 0 | TU::setCbf( tu, COMPONENT_Cr, chromaCbfs.Cr ); |
2232 | 0 | } |
2233 | |
|
2234 | 0 | cu.setPlaneCbf( COMPONENT_Y , cu.planeCbf( COMPONENT_Y ) || TU::getCbf( tu, COMPONENT_Y ) ); |
2235 | 0 | cu.setPlaneCbf( COMPONENT_Cb, cu.planeCbf( COMPONENT_Cb ) || TU::getCbf( tu, COMPONENT_Cb ) ); |
2236 | 0 | cu.setPlaneCbf( COMPONENT_Cr, cu.planeCbf( COMPONENT_Cr ) || TU::getCbf( tu, COMPONENT_Cr ) ); |
2237 | |
|
2238 | 0 | const bool lumaOnly = ( cu.chromaFormat == CHROMA_400 || !tu.blocks[COMPONENT_Cb].valid() ); |
2239 | 0 | const bool cbfLuma = TU::getCbf( tu, COMPONENT_Y ); |
2240 | 0 | const bool cbfChroma = ( lumaOnly ? false : ( chromaCbfs.Cb || chromaCbfs.Cr ) ); |
2241 | |
|
2242 | 0 | if( cu.lwidth() > 64 || cu.lheight() > 64 || cbfLuma || cbfChroma ) |
2243 | 0 | { |
2244 | 0 | if( cu.pps->getUseDQP() && !cuCtx.isDQPCoded ) |
2245 | 0 | { |
2246 | 0 | if( !CU::isSepTree( cu ) || isLuma( tu.chType() ) ) |
2247 | 0 | { |
2248 | 0 | cu_qp_delta(cu, cuCtx.qp, cu.qp); |
2249 | 0 | cuCtx.qp = cu.qp; |
2250 | 0 | cuCtx.isDQPCoded = true; |
2251 | 0 | } |
2252 | 0 | } |
2253 | |
|
2254 | 0 | if( !CU::isSepTree( cu ) || isChroma( tu.chType() ) ) // !DUAL_TREE_LUMA |
2255 | 0 | { |
2256 | 0 | const SizeType channelWidth = !CU::isSepTree( cu ) ? cu.lwidth() : cu.chromaSize().width; |
2257 | 0 | const SizeType channelHeight = !CU::isSepTree( cu ) ? cu.lheight() : cu.chromaSize().height; |
2258 | |
|
2259 | 0 | if( cu.slice->getUseChromaQpAdj() && ( channelWidth > 64 || channelHeight > 64 || cbfChroma ) && !cuCtx.isChromaQpAdjCoded ) |
2260 | 0 | { |
2261 | 0 | cu_chroma_qp_offset( cu ); |
2262 | 0 | cuCtx.isChromaQpAdjCoded = true; |
2263 | 0 | } |
2264 | 0 | } |
2265 | |
|
2266 | 0 | if( !lumaOnly ) |
2267 | 0 | { |
2268 | 0 | joint_cb_cr( tu, ( TU::getCbf( tu, COMPONENT_Cb ) ? 2 : 0 ) + ( TU::getCbf( tu, COMPONENT_Cr ) ? 1 : 0 ) ); |
2269 | |
|
2270 | 0 | if( tu.jointCbCr ) |
2271 | 0 | { |
2272 | 0 | cu.setPlaneCbf( COMPONENT_Cb, true ); |
2273 | 0 | cu.setPlaneCbf( COMPONENT_Cr, true ); |
2274 | 0 | } |
2275 | 0 | } |
2276 | |
|
2277 | 0 | if( cbfLuma ) |
2278 | 0 | { |
2279 | 0 | residual_coding( tu, COMPONENT_Y, cuCtx ); |
2280 | 0 | } |
2281 | 0 | if( !lumaOnly ) |
2282 | 0 | { |
2283 | 0 | for( ComponentID compID = COMPONENT_Cb; compID <= COMPONENT_Cr; compID = ComponentID( compID + 1 ) ) |
2284 | 0 | { |
2285 | 0 | if( TU::getCbf( tu, compID ) ) |
2286 | 0 | { |
2287 | 0 | residual_coding( tu, compID, cuCtx ); |
2288 | 0 | } |
2289 | 0 | } |
2290 | 0 | } |
2291 | 0 | } |
2292 | 0 | } |
2293 | | |
2294 | | void CABACReader::cu_qp_delta( CodingUnit& cu, int predQP, int8_t& qp ) |
2295 | 0 | { |
2296 | 0 | CHECK( predQP == std::numeric_limits<int>::max(), "Invalid predicted QP" ); |
2297 | 0 | int qpY = predQP; |
2298 | 0 | int DQp = unary_max_symbol( Ctx::DeltaQP(), Ctx::DeltaQP(1), CU_DQP_TU_CMAX ); |
2299 | 0 | if( DQp >= CU_DQP_TU_CMAX ) |
2300 | 0 | { |
2301 | 0 | DQp += exp_golomb_eqprob( CU_DQP_EG_k ); |
2302 | 0 | } |
2303 | 0 | if( DQp > 0 ) |
2304 | 0 | { |
2305 | 0 | if( m_BinDecoder.decodeBinEP( ) ) |
2306 | 0 | { |
2307 | 0 | DQp = -DQp; |
2308 | 0 | } |
2309 | 0 | int qpBdOffsetY = cu.sps->getQpBDOffset(); |
2310 | 0 | qpY = ( (predQP + DQp + (MAX_QP + 1) + 2 * qpBdOffsetY) % ((MAX_QP + 1) + qpBdOffsetY)) - qpBdOffsetY; |
2311 | 0 | } |
2312 | 0 | qp = (int8_t)qpY; |
2313 | |
|
2314 | 0 | DTRACE( g_trace_ctx, D_DQP, "x=%d, y=%d, d=%d, pred_qp=%d, DQp=%d, qp=%d\n", cu.blocks[cu.chType()].lumaPos( cu.chromaFormat ).x, cu.blocks[cu.chType()].lumaPos( cu.chromaFormat ).y, cu.qtDepth, predQP, DQp, qp ); |
2315 | 0 | } |
2316 | | |
2317 | | |
2318 | | void CABACReader::cu_chroma_qp_offset( CodingUnit& cu ) |
2319 | 0 | { |
2320 | | // cu_chroma_qp_offset_flag |
2321 | 0 | int length = cu.pps->getChromaQpOffsetListLen(); |
2322 | 0 | unsigned qpAdj = m_BinDecoder.decodeBin( Ctx::ChromaQpAdjFlag() ); |
2323 | 0 | if( qpAdj && length > 1 ) |
2324 | 0 | { |
2325 | | // cu_chroma_qp_offset_idx |
2326 | 0 | qpAdj += unary_max_symbol( Ctx::ChromaQpAdjIdc(), Ctx::ChromaQpAdjIdc(), length-1 ); |
2327 | 0 | } |
2328 | | /* NB, symbol = 0 if outer flag is not set, |
2329 | | * 1 if outer flag is set and there is no inner flag |
2330 | | * 1+ otherwise */ |
2331 | 0 | cu.chromaQpAdj = cu.cs->chromaQpAdj = qpAdj; |
2332 | 0 | } |
2333 | | |
2334 | | |
2335 | | |
2336 | | |
2337 | | |
2338 | | //================================================================================ |
2339 | | // clause 7.3.8.11 |
2340 | | //-------------------------------------------------------------------------------- |
2341 | | // void residual_coding ( tu, compID ) |
2342 | | // bool transform_skip_flag ( tu, compID ) |
2343 | | // RDPCMMode explicit_rdpcm_mode ( tu, compID ) |
2344 | | // int last_sig_coeff ( coeffCtx ) |
2345 | | // void residual_coding_subblock( coeffCtx ) |
2346 | | //================================================================================ |
2347 | | |
2348 | | void CABACReader::joint_cb_cr( TransformUnit& tu, const int cbfMask ) |
2349 | 0 | { |
2350 | 0 | if( !tu.cu->sps->getJointCbCrEnabledFlag() ) |
2351 | 0 | { |
2352 | 0 | return; |
2353 | 0 | } |
2354 | | |
2355 | 0 | if( ( CU::isIntra( *tu.cu ) && cbfMask ) || ( cbfMask == 3 ) ) |
2356 | 0 | { |
2357 | 0 | tu.jointCbCr = ( m_BinDecoder.decodeBin( Ctx::JointCbCrFlag( cbfMask-1 ) ) ? cbfMask : 0 ); |
2358 | 0 | } |
2359 | 0 | } |
2360 | | |
2361 | | void CABACReader::residual_coding( TransformUnit& tu, ComponentID compID, CUCtx& cuCtx ) |
2362 | 0 | { |
2363 | 0 | PROFILER_SCOPE_AND_STAGE_EXT( 1, g_timeProfiler, P_PARSERESIDUALS, *tu.cu->cs, compID ); |
2364 | 0 | const CodingUnit& cu = *tu.cu; |
2365 | |
|
2366 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "residual_coding() etype=%d pos=(%d,%d) size=%dx%d predMode=%d\n", tu.blocks[compID].compID(), tu.blocks[compID].x, tu.blocks[compID].y, tu.blocks[compID].width, tu.blocks[compID].height, cu.predMode() ); |
2367 | |
|
2368 | 0 | if( compID == COMPONENT_Cr && tu.jointCbCr == 3 ) |
2369 | 0 | return; |
2370 | | |
2371 | | // parse transform skip and explicit rdpcm mode |
2372 | 0 | ts_flag( tu, compID ); |
2373 | |
|
2374 | 0 | if( tu.mtsIdx( compID ) == MTS_SKIP && !cu.slice->getTSResidualCodingDisabledFlag() ) |
2375 | 0 | { |
2376 | 0 | residual_codingTS( tu, compID ); |
2377 | 0 | return; |
2378 | 0 | } |
2379 | | |
2380 | | // determine sign hiding |
2381 | 0 | bool signHiding = m_slice->getSignDataHidingEnabledFlag(); |
2382 | 0 | CoeffCodingContext cctx( tu, compID, signHiding, m_tplBuf ); |
2383 | | // parse last coeff position |
2384 | 0 | cctx.setScanPosLast( last_sig_coeff( cctx, tu, compID ) ); |
2385 | 0 | if (tu.mtsIdx( compID ) != MTS_SKIP && tu.blocks[compID].height >= 4 && tu.blocks[compID].width >= 4 ) |
2386 | 0 | { |
2387 | 0 | const int maxLfnstPos = ((tu.blocks[compID].height == 4 && tu.blocks[compID].width == 4) || (tu.blocks[compID].height == 8 && tu.blocks[compID].width == 8)) ? 7 : 15; |
2388 | 0 | cuCtx.violatesLfnstConstrained[ toChannelType(compID) ] |= cctx.scanPosLast() > maxLfnstPos; |
2389 | 0 | } |
2390 | 0 | if( tu.mtsIdx( compID ) != MTS_SKIP && tu.blocks[compID].height >= 4 && tu.blocks[compID].width >= 4 ) |
2391 | 0 | { |
2392 | 0 | const int lfnstLastScanPosTh = isLuma( compID ) ? LFNST_LAST_SIG_LUMA : LFNST_LAST_SIG_CHROMA; |
2393 | 0 | cuCtx.lfnstLastScanPos |= cctx.scanPosLast() >= lfnstLastScanPosTh; |
2394 | 0 | } |
2395 | 0 | if( isLuma( compID ) && tu.mtsIdx( compID ) != MTS_SKIP ) |
2396 | 0 | { |
2397 | 0 | cuCtx.mtsLastScanPos |= cctx.scanPosLast() >= 1; |
2398 | 0 | } |
2399 | | |
2400 | | // parse subblocks |
2401 | 0 | const int stateTransTab = ( m_slice->getDepQuantEnabledFlag() ? 32040 : 0 ); |
2402 | 0 | int state = 0; |
2403 | |
|
2404 | 0 | TCoeffSig *coeff = m_cffTmp; |
2405 | 0 | ::memset( coeff, 0, cctx.maxNumCoeff() * sizeof( TCoeffSig ) ); |
2406 | |
|
2407 | 0 | int *sigPos = m_blkPos; |
2408 | 0 | int sigSubSetId = 0; |
2409 | |
|
2410 | 0 | int maxX = 0; |
2411 | 0 | int maxY = 0; |
2412 | |
|
2413 | 0 | const bool skipBlkPreCond = compID == COMPONENT_Y && tu.cu->sps->getUseMTS() && tu.cu->sbtInfo() != 0 && tu.blocks[ compID ].height <= 32 && tu.blocks[ compID ].width <= 32; |
2414 | 0 | for( int subSetId = ( cctx.scanPosLast() >> cctx.log2CGSize() ); subSetId >= 0; subSetId--) |
2415 | 0 | { |
2416 | 0 | cctx.initSubblock( subSetId ); |
2417 | |
|
2418 | 0 | if( skipBlkPreCond ) |
2419 | 0 | { |
2420 | 0 | if( ( cctx.height() == 32 && cctx.cgPosY() >= ( 16 >> cctx.log2CGHeight() ) ) || ( cctx.width() == 32 && cctx.cgPosX() >= ( 16 >> cctx.log2CGWidth() ) ) ) |
2421 | 0 | { |
2422 | 0 | continue; |
2423 | 0 | } |
2424 | 0 | } |
2425 | | |
2426 | 0 | int numSigCoef = residual_coding_subblock( cctx, coeff, stateTransTab, state, m_signVal[sigSubSetId], sigPos, m_sub1[sigSubSetId] ); |
2427 | |
|
2428 | 0 | if( numSigCoef > 0 ) |
2429 | 0 | { |
2430 | 0 | m_numSig[sigSubSetId] = numSigCoef; |
2431 | 0 | sigSubSetId++; |
2432 | |
|
2433 | 0 | maxX = std::max( maxX, cctx.cgPosX() ); |
2434 | 0 | maxY = std::max( maxY, cctx.cgPosY() ); |
2435 | 0 | } |
2436 | 0 | if( isLuma( compID ) && cctx.isSigGroup() && ( cctx.cgPosY() > 3 || cctx.cgPosX() > 3 ) ) |
2437 | 0 | { |
2438 | 0 | cuCtx.violatesMtsCoeffConstraint = true; |
2439 | 0 | } |
2440 | 0 | } |
2441 | |
|
2442 | 0 | if( cctx.bdpcm() ) |
2443 | 0 | { |
2444 | 0 | maxX = cctx.width(); |
2445 | 0 | maxY = cctx.height(); |
2446 | 0 | } |
2447 | 0 | else |
2448 | 0 | { |
2449 | 0 | maxX++; |
2450 | 0 | maxY++; |
2451 | 0 | maxX <<= cctx.log2CGWidth(); |
2452 | 0 | maxY <<= cctx.log2CGHeight(); |
2453 | 0 | } |
2454 | | |
2455 | | // if not TU split, otherwise already memset |
2456 | 0 | PelBuf pb = cu.cs->getRecoBuf( CompArea( compID, tu.blocks[compID].pos(), Size( maxX, maxY ) ) ); |
2457 | 0 | pb.memset( 0 ); |
2458 | |
|
2459 | 0 | const bool depQuant = tu.cu->slice->getDepQuantEnabledFlag() && ( tu.mtsIdx( compID ) != MTS_SKIP ); |
2460 | 0 | CoeffSigBuf dstCff = pb; |
2461 | |
|
2462 | 0 | for( sigPos--, sigSubSetId--; sigSubSetId >= 0; sigSubSetId-- ) |
2463 | 0 | { |
2464 | 0 | unsigned numNonZero = m_numSig [sigSubSetId]; |
2465 | 0 | unsigned signPattern = m_signVal[sigSubSetId]; |
2466 | 0 | unsigned sub1Pattern = m_sub1 [sigSubSetId]; |
2467 | | |
2468 | | //===== set final coefficents ===== |
2469 | 0 | for( unsigned k = 0; k < numNonZero; k++, sigPos--, signPattern >>= 1, sub1Pattern >>= 1 ) |
2470 | 0 | { |
2471 | 0 | const int blkPos = *sigPos; |
2472 | 0 | const int posX = cctx.posX( blkPos ); |
2473 | 0 | const int posY = cctx.posY( blkPos ); |
2474 | |
|
2475 | 0 | int AbsCoeff = depQuant ? ( coeff[blkPos] << 1 ) - ( ( ( int ) sub1Pattern ) & 1 ) : coeff[blkPos]; |
2476 | 0 | dstCff.at( posX, posY ) = ( signPattern & 1u ? -AbsCoeff : AbsCoeff ); |
2477 | 0 | } |
2478 | 0 | } |
2479 | |
|
2480 | 0 | if( cctx.scanPosLast() == 0 ) |
2481 | 0 | { |
2482 | 0 | tu.maxScanPosX[compID] = 0; |
2483 | 0 | tu.maxScanPosY[compID] = 0; |
2484 | 0 | } |
2485 | 0 | else |
2486 | 0 | { |
2487 | 0 | tu.maxScanPosX[compID] = maxX - 1; |
2488 | 0 | tu.maxScanPosY[compID] = maxY - 1; |
2489 | 0 | } |
2490 | 0 | } |
2491 | | |
2492 | | void CABACReader::ts_flag( TransformUnit& tu, ComponentID compID ) |
2493 | 0 | { |
2494 | 0 | int tsFlag = ( ( tu.cu->bdpcmMode() && isLuma( compID ) ) || ( tu.cu->bdpcmModeChroma() && isChroma( compID ) ) ) ? 1 : tu.mtsIdx( compID ) == MTS_SKIP ? 1 : 0; |
2495 | 0 | int ctxIdx = isLuma(compID) ? 4 : 5; |
2496 | |
|
2497 | 0 | if( TU::isTSAllowed ( tu, compID ) ) |
2498 | 0 | { |
2499 | 0 | tsFlag = m_BinDecoder.decodeBin( Ctx::MTSIndex( ctxIdx ) ); |
2500 | 0 | } |
2501 | | |
2502 | 0 | tu.setMtsIdx( compID, tsFlag ? MTS_SKIP : MTS_DCT2_DCT2 ); |
2503 | | |
2504 | 0 | DTRACE(g_trace_ctx, D_SYNTAX, "ts_flag() etype=%d pos=(%d,%d) mtsIdx=%d\n", COMPONENT_Y, tu.cu->lx(), tu.cu->ly(), tsFlag); |
2505 | 0 | } |
2506 | | |
2507 | | void CABACReader::mts_idx( CodingUnit& cu, CUCtx& cuCtx ) |
2508 | 0 | { |
2509 | 0 | TransformUnit &tu = cu.firstTU; |
2510 | 0 | int mtsIdx = tu.mtsIdx( COMPONENT_Y ); // Transform skip flag has already been decoded |
2511 | | |
2512 | 0 | if( CU::isMTSAllowed( cu, COMPONENT_Y ) && !cuCtx.violatesMtsCoeffConstraint && |
2513 | 0 | cuCtx.mtsLastScanPos && cu.lfnstIdx() == 0 && mtsIdx != MTS_SKIP ) |
2514 | 0 | { |
2515 | 0 | int ctxIdx = 0; |
2516 | 0 | int symbol = m_BinDecoder.decodeBin( Ctx::MTSIndex( ctxIdx ) ); |
2517 | | |
2518 | 0 | if( symbol ) |
2519 | 0 | { |
2520 | 0 | ctxIdx = 1; |
2521 | 0 | mtsIdx = MTS_DST7_DST7; // mtsIdx = 2 -- 4 |
2522 | 0 | for( int i = 0; i < 3; i++, ctxIdx++ ) |
2523 | 0 | { |
2524 | 0 | symbol = m_BinDecoder.decodeBin( Ctx::MTSIndex( ctxIdx ) ); |
2525 | 0 | mtsIdx += symbol; |
2526 | | |
2527 | 0 | if( !symbol ) |
2528 | 0 | { |
2529 | 0 | break; |
2530 | 0 | } |
2531 | 0 | } |
2532 | 0 | } |
2533 | 0 | } |
2534 | | |
2535 | 0 | tu.setMtsIdx( COMPONENT_Y, mtsIdx ); |
2536 | | |
2537 | 0 | DTRACE(g_trace_ctx, D_SYNTAX, "mts_idx() etype=%d pos=(%d,%d) mtsIdx=%d\n", COMPONENT_Y, tu.cu->lx(), tu.cu->ly(), mtsIdx); |
2538 | 0 | } |
2539 | | |
2540 | | void CABACReader::isp_mode( CodingUnit& cu ) |
2541 | 0 | { |
2542 | 0 | if( cu.multiRefIdx() || !cu.sps->getUseISP() || cu.bdpcmMode() || cu.colorTransform() ) |
2543 | 0 | { |
2544 | | //cu.setIspMode( NOT_INTRA_SUBPARTITIONS ); |
2545 | 0 | return; |
2546 | 0 | } |
2547 | | |
2548 | 0 | const ISPType allowedSplits = CU::canUseISPSplit( cu, COMPONENT_Y ); |
2549 | |
|
2550 | 0 | if( allowedSplits == NOT_INTRA_SUBPARTITIONS ) |
2551 | 0 | { |
2552 | | //cu.setIspMode( NOT_INTRA_SUBPARTITIONS ); |
2553 | 0 | return; |
2554 | 0 | } |
2555 | | |
2556 | | //cu.setIspMode( NOT_INTRA_SUBPARTITIONS ); |
2557 | 0 | int symbol = m_BinDecoder.decodeBin( Ctx::ISPMode( 0 ) ); |
2558 | |
|
2559 | 0 | if( symbol ) |
2560 | 0 | { |
2561 | 0 | if( allowedSplits == HOR_INTRA_SUBPARTITIONS ) |
2562 | 0 | { |
2563 | 0 | cu.setIspMode( HOR_INTRA_SUBPARTITIONS ); |
2564 | 0 | } |
2565 | 0 | else if( allowedSplits == VER_INTRA_SUBPARTITIONS ) |
2566 | 0 | { |
2567 | 0 | cu.setIspMode( VER_INTRA_SUBPARTITIONS ); |
2568 | 0 | } |
2569 | 0 | else |
2570 | 0 | { |
2571 | 0 | cu.setIspMode( 1 + m_BinDecoder.decodeBin( Ctx::ISPMode( 1 ) ) ); |
2572 | 0 | } |
2573 | 0 | } |
2574 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "intra_subPartitions() etype=%d pos=(%d,%d) ispIdx=%d\n", cu.chType(), cu.blocks[cu.chType()].x, cu.blocks[cu.chType()].y, (int)cu.ispMode() ); |
2575 | 0 | } |
2576 | | |
2577 | | void CABACReader::residual_lfnst_mode( CodingUnit& cu, CUCtx& cuCtx ) |
2578 | 0 | { |
2579 | 0 | if( !cu.sps->getUseLFNST() || !CU::isIntra( cu ) ) |
2580 | 0 | { |
2581 | | //cu.setLfnstIdx( 0 ); |
2582 | 0 | return; |
2583 | 0 | } |
2584 | | |
2585 | 0 | const int chIdx = CU::isSepTree( cu ) && cu.chType() == CHANNEL_TYPE_CHROMA ? 1 : 0; |
2586 | 0 | if( ( cu.ispMode() && !CU::canUseLfnstWithISP( cu, cu.chType() ) ) || |
2587 | 0 | ( cu.mipFlag() && ! allowLfnstWithMip ( cu.lumaSize() ) ) || |
2588 | 0 | ( cu.chType() == CHANNEL_TYPE_CHROMA && std::min( cu.blocks[1].width, cu.blocks[1].height ) < 4 ) |
2589 | 0 | ) |
2590 | 0 | { |
2591 | 0 | return; |
2592 | 0 | } |
2593 | | |
2594 | 0 | const Size lSize = cu.blocks[chIdx].lumaSize( cu.chromaFormat ); |
2595 | 0 | if( lSize.width > cu.sps->getMaxTbSize() || lSize.height > cu.sps->getMaxTbSize() ) return; |
2596 | | |
2597 | 0 | { |
2598 | 0 | const bool lumaFlag = CU::isSepTree( cu ) ? isLuma( cu.chType() ) : true; |
2599 | 0 | const bool chromaFlag = CU::isSepTree( cu ) ? isChroma( cu.chType() ) : true; |
2600 | 0 | const bool nonZeroCoeffNonTsCorner8x8 |
2601 | 0 | = ( lumaFlag && cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_LUMA] ) || (chromaFlag && cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_CHROMA] ); |
2602 | 0 | bool isTrSkip = false; |
2603 | 0 | for( auto &currTU : cTUTraverser( &cu.firstTU, cu.lastTU->next ) ) |
2604 | 0 | { |
2605 | 0 | const uint32_t numValidComp = getNumberValidComponents( cu.chromaFormat ); |
2606 | 0 | for( uint32_t compID = COMPONENT_Y; compID < numValidComp; compID++ ) |
2607 | 0 | { |
2608 | 0 | if( currTU.blocks[compID].valid() && TU::getCbf( currTU, ( ComponentID ) compID ) && currTU.mtsIdx( compID ) == MTS_SKIP ) |
2609 | 0 | { |
2610 | 0 | isTrSkip = true; |
2611 | 0 | break; |
2612 | 0 | } |
2613 | 0 | } |
2614 | 0 | } |
2615 | 0 | if( nonZeroCoeffNonTsCorner8x8 || ( !cuCtx.lfnstLastScanPos && !cu.ispMode() ) || isTrSkip ) |
2616 | 0 | { |
2617 | | //cu.setLfnstIdx( 0 ); |
2618 | 0 | return; |
2619 | 0 | } |
2620 | 0 | } |
2621 | | |
2622 | | |
2623 | 0 | unsigned cctx = 0; |
2624 | |
|
2625 | 0 | if( CU::isSepTree( cu ) ) cctx++; |
2626 | |
|
2627 | 0 | unsigned idxLFNST = m_BinDecoder.decodeBin( Ctx::LFNSTIdx( cctx ) ); |
2628 | |
|
2629 | 0 | if( idxLFNST ) |
2630 | 0 | { |
2631 | 0 | idxLFNST += m_BinDecoder.decodeBin( Ctx::LFNSTIdx(2) ); |
2632 | 0 | } |
2633 | |
|
2634 | 0 | cu.setLfnstIdx( idxLFNST ); |
2635 | |
|
2636 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "residual_lfnst_mode() etype=%d pos=(%d,%d) mode=%d\n", COMPONENT_Y, cu.lx(), cu.ly(), ( int ) cu.lfnstIdx() ); |
2637 | 0 | } |
2638 | | |
2639 | | int CABACReader::last_sig_coeff( CoeffCodingContext& cctx, TransformUnit& tu, ComponentID compID ) |
2640 | 0 | { |
2641 | 0 | unsigned PosLastX = 0, PosLastY = 0; |
2642 | 0 | unsigned maxLastPosX = cctx.maxLastPosX(); |
2643 | 0 | unsigned maxLastPosY = cctx.maxLastPosY(); |
2644 | |
|
2645 | 0 | if( isLuma( compID ) && tu.cu->sps->getUseMTS() && tu.cu->sbtInfo() != 0 && tu.blocks[ compID ].width <= 32 && tu.blocks[ compID ].height <= 32 ) |
2646 | 0 | { |
2647 | 0 | maxLastPosX = ( tu.blocks[ compID ].width == 32 ) ? g_uiGroupIdx[ 15 ] : maxLastPosX; |
2648 | 0 | maxLastPosY = ( tu.blocks[ compID ].height == 32 ) ? g_uiGroupIdx[ 15 ] : maxLastPosY; |
2649 | 0 | } |
2650 | |
|
2651 | 0 | for( ; PosLastX < maxLastPosX; PosLastX++ ) |
2652 | 0 | { |
2653 | 0 | if( !m_BinDecoder.decodeBin( cctx.lastXCtxId( PosLastX ) ) ) |
2654 | 0 | { |
2655 | 0 | break; |
2656 | 0 | } |
2657 | 0 | } |
2658 | 0 | for( ; PosLastY < maxLastPosY; PosLastY++ ) |
2659 | 0 | { |
2660 | 0 | if( !m_BinDecoder.decodeBin( cctx.lastYCtxId( PosLastY ) ) ) |
2661 | 0 | { |
2662 | 0 | break; |
2663 | 0 | } |
2664 | 0 | } |
2665 | 0 | if( PosLastX > 3 ) |
2666 | 0 | { |
2667 | 0 | uint32_t uiTemp = 0; |
2668 | 0 | uint32_t uiCount = ( PosLastX - 2 ) >> 1; |
2669 | 0 | for ( int i = uiCount - 1; i >= 0; i-- ) |
2670 | 0 | { |
2671 | 0 | uiTemp += m_BinDecoder.decodeBinEP( ) << i; |
2672 | 0 | } |
2673 | 0 | PosLastX = g_uiMinInGroup[ PosLastX ] + uiTemp; |
2674 | 0 | } |
2675 | 0 | if( PosLastY > 3 ) |
2676 | 0 | { |
2677 | 0 | uint32_t uiTemp = 0; |
2678 | 0 | uint32_t uiCount = ( PosLastY - 2 ) >> 1; |
2679 | 0 | for ( int i = uiCount - 1; i >= 0; i-- ) |
2680 | 0 | { |
2681 | 0 | uiTemp += m_BinDecoder.decodeBinEP( ) << i; |
2682 | 0 | } |
2683 | 0 | PosLastY = g_uiMinInGroup[ PosLastY ] + uiTemp; |
2684 | 0 | } |
2685 | |
|
2686 | 0 | int blkPos; |
2687 | 0 | { |
2688 | 0 | blkPos = PosLastX + ( PosLastY * cctx.width() ); |
2689 | 0 | } |
2690 | |
|
2691 | 0 | for( int scanPos = 0; scanPos < cctx.maxNumCoeff() - 1; scanPos++ ) |
2692 | 0 | { |
2693 | 0 | if( blkPos == cctx.blockPos( scanPos ) ) |
2694 | 0 | { |
2695 | 0 | return scanPos; |
2696 | 0 | } |
2697 | 0 | } |
2698 | | |
2699 | 0 | return cctx.maxNumCoeff() - 1; |
2700 | 0 | } |
2701 | | |
2702 | | |
2703 | | int CABACReader::residual_coding_subblock(CoeffCodingContext& cctx, TCoeffSig* coeff, const int stateTransTable, int& state, unsigned& signPattern, int *&sigPos, unsigned &stateVal) |
2704 | 0 | { |
2705 | | // NOTE: All coefficients of the subblock must be set to zero before calling this function |
2706 | | |
2707 | | //===== init ===== |
2708 | 0 | const int minSubPos = cctx.minSubPos(); |
2709 | 0 | const bool isLast = cctx.isLast(); |
2710 | 0 | int firstSigPos = (isLast ? cctx.scanPosLast() : cctx.maxSubPos()); |
2711 | 0 | int nextSigPos = firstSigPos; |
2712 | | |
2713 | | //===== decode significant_coeffgroup_flag ===== |
2714 | 0 | bool sigGroup = (isLast || !minSubPos); |
2715 | 0 | if (!sigGroup) |
2716 | 0 | { |
2717 | 0 | sigGroup = m_BinDecoder.decodeBin(cctx.sigGroupCtxId()); |
2718 | 0 | } |
2719 | 0 | if (sigGroup) |
2720 | 0 | { |
2721 | 0 | cctx.setSigGroup(); |
2722 | 0 | } |
2723 | 0 | else |
2724 | 0 | { |
2725 | 0 | return 0; |
2726 | 0 | } |
2727 | | |
2728 | | // make sure only takes up single L1 block |
2729 | 0 | ALIGN_DATA( 64, int gt1Pos[16] ); |
2730 | 0 | int * gt1PosPtr = gt1Pos; |
2731 | | |
2732 | | //===== decode absolute values ===== |
2733 | 0 | const int inferSigPos = nextSigPos != cctx.scanPosLast() ? (cctx.isNotFirst() ? minSubPos : -1) : nextSigPos; |
2734 | 0 | int firstNZPos = nextSigPos; |
2735 | 0 | int lastNZPos = -1; |
2736 | 0 | int numNonZero = 0, numGt1 = 0; |
2737 | 0 | int remRegBins = cctx.regBinLimit(); |
2738 | 0 | int gt1Mode1 = 0; |
2739 | 0 | unsigned gt2Mask = 0; |
2740 | 0 | stateVal = 0; |
2741 | |
|
2742 | 0 | for( ; nextSigPos >= minSubPos && remRegBins >= 4; nextSigPos-- ) |
2743 | 0 | { |
2744 | 0 | int blkPos = cctx.blockPos(nextSigPos); |
2745 | 0 | bool sigFlag = (!numNonZero && nextSigPos == inferSigPos); |
2746 | |
|
2747 | 0 | unsigned absVal = 0; |
2748 | |
|
2749 | 0 | if (!sigFlag) |
2750 | 0 | { |
2751 | 0 | const unsigned sigCtxId = cctx.sigCtxIdAbs( blkPos, state ); |
2752 | 0 | sigFlag = m_BinDecoder.decodeBin( sigCtxId ); |
2753 | 0 | DTRACE(g_trace_ctx, D_SYNTAX_RESI, "sig_bin() bin=%d ctx=%d\n", sigFlag, sigCtxId); |
2754 | 0 | remRegBins--; |
2755 | 0 | } |
2756 | |
|
2757 | 0 | if (sigFlag) |
2758 | 0 | { |
2759 | 0 | uint8_t ctxOff = cctx.ctxOffsetAbs(); |
2760 | 0 | stateVal = ( ( state >> 1 ) & 1 ) | ( stateVal << 1 ); |
2761 | 0 | *sigPos++ = blkPos; |
2762 | 0 | numNonZero++; |
2763 | 0 | firstNZPos = nextSigPos; |
2764 | 0 | lastNZPos = std::max<int>( lastNZPos, nextSigPos ); |
2765 | 0 | unsigned gt1Flag = m_BinDecoder.decodeBin(cctx.greater1CtxIdAbs(ctxOff)); |
2766 | 0 | DTRACE(g_trace_ctx, D_SYNTAX_RESI, "gt1_flag() bin=%d ctx=%d\n", gt1Flag, cctx.greater1CtxIdAbs(ctxOff)); |
2767 | 0 | remRegBins--; |
2768 | |
|
2769 | 0 | unsigned gt2Flag = 0; |
2770 | |
|
2771 | 0 | if (gt1Flag) |
2772 | 0 | { |
2773 | 0 | unsigned parFlag = m_BinDecoder.decodeBin(cctx.parityCtxIdAbs(ctxOff)); |
2774 | 0 | DTRACE(g_trace_ctx, D_SYNTAX_RESI, "par_flag() bin=%d ctx=%d\n", parFlag, cctx.parityCtxIdAbs(ctxOff)); |
2775 | 0 | numGt1++; |
2776 | 0 | remRegBins--; |
2777 | 0 | gt2Flag = m_BinDecoder.decodeBin( cctx.greater2CtxIdAbs( ctxOff ) ); |
2778 | 0 | gt2Mask |= ( gt2Flag << (numGt1-1) ); |
2779 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "gt2_flag() bin=%d ctx=%d\n", gt2Flag, cctx.greater2CtxIdAbs( ctxOff ) ); |
2780 | 0 | remRegBins--; |
2781 | 0 | *gt1PosPtr++ = blkPos; |
2782 | 0 | absVal = 2 + parFlag + (gt2Flag << 1); |
2783 | 0 | state = ( stateTransTable >> ( ( state << 2 ) + ( parFlag << 1 ) ) ) & 3; |
2784 | 0 | } |
2785 | 0 | else |
2786 | 0 | { |
2787 | 0 | absVal = 1; |
2788 | 0 | state = ( stateTransTable >> ( ( state << 2 ) + 2 ) ) & 3; |
2789 | 0 | } |
2790 | |
|
2791 | 0 | cctx.absVal1stPass( blkPos, coeff, absVal ); |
2792 | 0 | } |
2793 | 0 | else |
2794 | 0 | { |
2795 | 0 | state = ( stateTransTable >> ( state << 2 ) ) & 3; |
2796 | 0 | } |
2797 | 0 | } |
2798 | |
|
2799 | 0 | cctx.setRegBinLimit( remRegBins ); |
2800 | |
|
2801 | 0 | gt1Mode1 = numGt1; |
2802 | |
|
2803 | 0 | gt1PosPtr = gt1Pos; |
2804 | | |
2805 | | //===== 3rd PASS: Go-rice codes ===== |
2806 | 0 | for( int k = 0; k < gt1Mode1; k++, gt2Mask >>= 1, gt1PosPtr++ ) |
2807 | 0 | { |
2808 | 0 | if( gt2Mask & 1 ) |
2809 | 0 | { |
2810 | 0 | int sumAll = cctx.templateAbsSum( *gt1PosPtr, coeff, 4 ); |
2811 | 0 | unsigned ricePar = g_auiGoRiceParsCoeff[sumAll]; |
2812 | |
|
2813 | 0 | int rem = m_BinDecoder.decodeRemAbsEP( ricePar, COEF_REMAIN_BIN_REDUCTION, cctx.maxLog2TrDRange() ); |
2814 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "rem_val() bin=%d ctx=%d\n", rem, ricePar ); |
2815 | 0 | coeff[ *gt1PosPtr ] += ( rem << 1 ); |
2816 | 0 | } |
2817 | 0 | } |
2818 | | |
2819 | | |
2820 | | //===== coeff bypass ==== |
2821 | 0 | for( ; nextSigPos >= minSubPos; nextSigPos-- ) |
2822 | 0 | { |
2823 | 0 | int sub1 = ( state >> 1 ) & 1; |
2824 | 0 | int blkPos = cctx.blockPos( nextSigPos ); |
2825 | 0 | int sumAll = cctx.templateAbsSum( blkPos, coeff, 0 ); |
2826 | 0 | int rice = g_auiGoRiceParsCoeff [sumAll]; |
2827 | 0 | int pos0 = g_auiGoRicePosCoeff0(state, rice); |
2828 | 0 | int rem = m_BinDecoder.decodeRemAbsEP( rice, COEF_REMAIN_BIN_REDUCTION, cctx.maxLog2TrDRange() ); |
2829 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "rem_val() bin=%d ctx=%d\n", rem, rice ); |
2830 | 0 | TCoeffSig tcoeff = ( rem == pos0 ? 0 : rem < pos0 ? rem+1 : rem ); |
2831 | 0 | state = ( stateTransTable >> ( ( state << 2 ) + ( ( tcoeff & 1 ) << 1 ) ) ) & 3; |
2832 | 0 | if( tcoeff ) |
2833 | 0 | { |
2834 | 0 | coeff[blkPos] = tcoeff; |
2835 | 0 | stateVal = sub1 | ( stateVal << 1 ); |
2836 | 0 | *sigPos++ = blkPos; |
2837 | 0 | numNonZero++; |
2838 | 0 | firstNZPos = nextSigPos; |
2839 | 0 | lastNZPos = std::max<int>( lastNZPos, nextSigPos ); |
2840 | 0 | } |
2841 | 0 | } |
2842 | | |
2843 | | //===== decode sign's ===== |
2844 | 0 | const unsigned numSigns = ( cctx.hideSign( firstNZPos, lastNZPos ) ? numNonZero - 1 : numNonZero ); |
2845 | 0 | signPattern = m_BinDecoder.decodeBinsEP( numSigns ); |
2846 | 0 | if( numNonZero > numSigns ) |
2847 | 0 | { |
2848 | 0 | sigPos -= numNonZero; |
2849 | 0 | int sumAbs = 0; |
2850 | 0 | for( int i = 0; i < numNonZero; i++ ) |
2851 | 0 | { |
2852 | 0 | const int blockPos = *sigPos; |
2853 | 0 | sumAbs += coeff[blockPos]; |
2854 | 0 | sigPos++; |
2855 | 0 | } |
2856 | 0 | signPattern <<= 1; |
2857 | 0 | signPattern += (sumAbs & 1); |
2858 | 0 | } |
2859 | 0 | return numNonZero; |
2860 | 0 | } |
2861 | | |
2862 | | void CABACReader::residual_codingTS( TransformUnit& tu, ComponentID compID ) |
2863 | 0 | { |
2864 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "residual_codingTS() etype=%d pos=(%d,%d) size=%dx%d\n", tu.blocks[compID].compID(), tu.blocks[compID].x, tu.blocks[compID].y, tu.blocks[compID].width, tu.blocks[compID].height ); |
2865 | | |
2866 | | // if not TU split, otherwise already memset |
2867 | 0 | PelBuf pb = tu.cu->cs->getRecoBuf( tu.blocks[compID] ); |
2868 | 0 | pb.memset( 0 ); |
2869 | | |
2870 | | // init coeff coding context |
2871 | 0 | CoeffCodingContext cctx( tu, compID, false, m_tplBuf ); |
2872 | 0 | TCoeffSig *coeff = m_cffTmp; |
2873 | 0 | ::memset( coeff, 0, cctx.maxNumCoeff() * sizeof( TCoeffSig ) ); |
2874 | |
|
2875 | 0 | int maxCtxBins = ( cctx.maxNumCoeff() * 7 ) >> 2; |
2876 | 0 | cctx.setNumCtxBins( maxCtxBins ); |
2877 | |
|
2878 | 0 | int maxX = 0; |
2879 | 0 | int maxY = 0; |
2880 | |
|
2881 | 0 | for( int subSetId = 0; subSetId <= ( cctx.maxNumCoeff() - 1 ) >> cctx.log2CGSize(); subSetId++ ) |
2882 | 0 | { |
2883 | 0 | cctx.initSubblock ( subSetId ); |
2884 | 0 | residual_coding_subblockTS( cctx, coeff, tu.cu->cs->getRecoBuf( tu.block( compID ) ), maxX, maxY ); |
2885 | 0 | } |
2886 | |
|
2887 | 0 | if( cctx.bdpcm() ) |
2888 | 0 | { |
2889 | 0 | tu.maxScanPosX[compID] = cctx.width(); |
2890 | 0 | tu.maxScanPosY[compID] = cctx.height(); |
2891 | 0 | } |
2892 | 0 | else |
2893 | 0 | { |
2894 | 0 | tu.maxScanPosX[compID] = maxX; |
2895 | 0 | tu.maxScanPosY[compID] = maxY; |
2896 | 0 | } |
2897 | 0 | } |
2898 | | |
2899 | | void CABACReader::residual_coding_subblockTS( CoeffCodingContext& cctx, TCoeffSig* coeff, CoeffSigBuf dstcoeff, int& maxX, int& maxY ) |
2900 | 0 | { |
2901 | | // TODO: awi, profile and optimize similar to residual_coding_subblock(...) |
2902 | | |
2903 | | // NOTE: All coefficients of the subblock must be set to zero before calling this function |
2904 | | //===== init ===== |
2905 | 0 | const int minSubPos = cctx.maxSubPos(); |
2906 | 0 | int firstSigPos = cctx.minSubPos(); |
2907 | 0 | int nextSigPos = firstSigPos; |
2908 | 0 | unsigned signPattern = 0; |
2909 | | |
2910 | | //===== decode significant_coeffgroup_flag ===== |
2911 | 0 | bool sigGroup = cctx.isLastSubSet() && cctx.noneSigGroup(); |
2912 | 0 | if( !sigGroup ) |
2913 | 0 | { |
2914 | 0 | sigGroup = m_BinDecoder.decodeBin( cctx.sigGroupCtxId( true ) ); |
2915 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sigGroup() bin=%d ctx=%d\n", sigGroup, cctx.sigGroupCtxId() ); |
2916 | 0 | } |
2917 | 0 | if( sigGroup ) |
2918 | 0 | { |
2919 | 0 | cctx.setSigGroup(); |
2920 | 0 | } |
2921 | 0 | else |
2922 | 0 | { |
2923 | 0 | return; |
2924 | 0 | } |
2925 | | |
2926 | | //===== decode absolute values ===== |
2927 | 0 | const int inferSigPos = minSubPos; |
2928 | 0 | int numNonZero = 0; |
2929 | 0 | int sigBlkPos[ 1 << MLS_CG_SIZE ]; |
2930 | |
|
2931 | 0 | int lastScanPosPass1 = -1; |
2932 | 0 | int lastScanPosPass2 = -1; |
2933 | 0 | for (; nextSigPos <= minSubPos && cctx.numCtxBins() >= 4; nextSigPos++) |
2934 | 0 | { |
2935 | 0 | int blkPos = cctx.blockPos( nextSigPos ); |
2936 | 0 | unsigned sigFlag = ( !numNonZero && nextSigPos == inferSigPos ); |
2937 | |
|
2938 | 0 | if( !sigFlag ) |
2939 | 0 | { |
2940 | 0 | const unsigned sigCtxId = cctx.sigCtxIdAbsTS( blkPos, coeff ); |
2941 | 0 | sigFlag = m_BinDecoder.decodeBin( sigCtxId ); |
2942 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sig_bin() bin=%d ctx=%d\n", sigFlag, sigCtxId ); |
2943 | 0 | cctx.decNumCtxBins(1); |
2944 | 0 | } |
2945 | |
|
2946 | 0 | if( sigFlag ) |
2947 | 0 | { |
2948 | | //===== decode sign's ===== |
2949 | 0 | int sign; |
2950 | 0 | const unsigned signCtxId = cctx.signCtxIdAbsTS(blkPos, coeff, cctx.bdpcm()); |
2951 | 0 | sign = m_BinDecoder.decodeBin(signCtxId); |
2952 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "sign() bin=%d ctx=%d nextSigPos=%d blkPos=%d\n", sign, signCtxId, nextSigPos, blkPos ); |
2953 | 0 | cctx.decNumCtxBins(1); |
2954 | |
|
2955 | 0 | signPattern += ( sign << numNonZero ); |
2956 | |
|
2957 | 0 | sigBlkPos[numNonZero++] = blkPos; |
2958 | |
|
2959 | 0 | unsigned gt1Flag; |
2960 | 0 | const unsigned gt1CtxId = cctx.lrg1CtxIdAbsTS(blkPos, coeff, cctx.bdpcm()); |
2961 | 0 | gt1Flag = m_BinDecoder.decodeBin(gt1CtxId); |
2962 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_gt1_flag() bin=%d ctx=%d\n", gt1Flag, gt1CtxId ); |
2963 | 0 | cctx.decNumCtxBins(1); |
2964 | |
|
2965 | 0 | unsigned parFlag = 0; |
2966 | 0 | if( gt1Flag ) |
2967 | 0 | { |
2968 | 0 | parFlag = m_BinDecoder.decodeBin( cctx.parityCtxIdAbsTS() ); |
2969 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_par_flag() bin=%d ctx=%d\n", parFlag, cctx.parityCtxIdAbsTS() ); |
2970 | 0 | cctx.decNumCtxBins(1); |
2971 | 0 | } |
2972 | 0 | coeff[ blkPos ] = (sign ? -1 : 1 ) * (1 + parFlag + gt1Flag); |
2973 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "coeff[ blkPos ]=%d blkPos=%d\n", coeff[ blkPos ], blkPos ); |
2974 | 0 | } |
2975 | 0 | lastScanPosPass1 = nextSigPos; |
2976 | 0 | } |
2977 | |
|
2978 | 0 | int cutoffVal = 2; |
2979 | 0 | int numGtBins = 4; |
2980 | | |
2981 | | //===== 2nd PASS: gt2 ===== |
2982 | 0 | for (int scanPos = firstSigPos; scanPos <= minSubPos && cctx.numCtxBins() >= 4; scanPos++) |
2983 | 0 | { |
2984 | 0 | TCoeffSig& tcoeff = coeff[cctx.blockPos( scanPos )]; |
2985 | 0 | cutoffVal = 2; |
2986 | 0 | for( int i = 0; i < numGtBins; i++ ) |
2987 | 0 | { |
2988 | 0 | if( tcoeff < 0 ) |
2989 | 0 | { |
2990 | 0 | tcoeff = -tcoeff; |
2991 | 0 | } |
2992 | |
|
2993 | 0 | if( tcoeff >= cutoffVal ) |
2994 | 0 | { |
2995 | 0 | unsigned gt2Flag; |
2996 | 0 | gt2Flag = m_BinDecoder.decodeBin( cctx.greaterXCtxIdAbsTS( cutoffVal >> 1 ) ); |
2997 | 0 | tcoeff += ( gt2Flag << 1 ); |
2998 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_gt%d_flag() bin=%d ctx=%d sp=%d coeff=%d\n", i, gt2Flag, cctx.greaterXCtxIdAbsTS( cutoffVal >> 1 ), scanPos, tcoeff ); |
2999 | 0 | cctx.decNumCtxBins(1); |
3000 | 0 | } |
3001 | 0 | cutoffVal += 2; |
3002 | 0 | } |
3003 | 0 | lastScanPosPass2 = scanPos; |
3004 | 0 | } |
3005 | | //===== 3rd PASS: Go-rice codes ===== |
3006 | 0 | for( int scanPos = firstSigPos; scanPos <= minSubPos; scanPos++ ) |
3007 | 0 | { |
3008 | 0 | TCoeffSig& tcoeff = coeff[ cctx.blockPos( scanPos ) ]; |
3009 | |
|
3010 | 0 | cutoffVal = (scanPos <= lastScanPosPass2 ? 10 : (scanPos <= lastScanPosPass1 ? 2 : 0)); |
3011 | 0 | if (tcoeff < 0) |
3012 | 0 | { |
3013 | 0 | tcoeff = -tcoeff; |
3014 | 0 | } |
3015 | |
|
3016 | 0 | if( tcoeff >= cutoffVal ) |
3017 | 0 | { |
3018 | 0 | int rice = cctx.templateAbsSumTS( cctx.blockPos( scanPos ), coeff ); |
3019 | 0 | int rem = m_BinDecoder.decodeRemAbsEP( rice, COEF_REMAIN_BIN_REDUCTION, cctx.maxLog2TrDRange() ); |
3020 | 0 | DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_rem_val() bin=%d ctx=%d sp=%d\n", rem, rice, scanPos ); |
3021 | 0 | tcoeff += (scanPos <= lastScanPosPass1) ? (rem << 1) : rem; |
3022 | 0 | if (tcoeff && scanPos > lastScanPosPass1) |
3023 | 0 | { |
3024 | 0 | int blkPos = cctx.blockPos(scanPos); |
3025 | 0 | int sign = m_BinDecoder.decodeBinEP(); |
3026 | 0 | signPattern += (sign << numNonZero); |
3027 | 0 | sigBlkPos[numNonZero++] = blkPos; |
3028 | 0 | } |
3029 | 0 | } |
3030 | 0 | if (!cctx.bdpcm() && cutoffVal) |
3031 | 0 | { |
3032 | 0 | if (tcoeff > 0) |
3033 | 0 | { |
3034 | 0 | int rightPixel, belowPixel; |
3035 | 0 | cctx.neighTS(rightPixel, belowPixel, cctx.blockPos( scanPos ), coeff); |
3036 | 0 | tcoeff = cctx.decDeriveModCoeff(rightPixel, belowPixel, tcoeff); |
3037 | 0 | } |
3038 | 0 | } |
3039 | 0 | } |
3040 | | |
3041 | | //===== set final coefficents ===== |
3042 | 0 | for( unsigned k = 0; k < numNonZero; k++ ) |
3043 | 0 | { |
3044 | 0 | int AbsCoeff = coeff[ sigBlkPos[ k ] ]; |
3045 | 0 | int blkPos = sigBlkPos[ k ]; |
3046 | 0 | const int posX = cctx.posX( blkPos ); |
3047 | 0 | const int posY = cctx.posY( blkPos ); |
3048 | 0 | maxX = std::max<int>( maxX, posX ); |
3049 | 0 | maxY = std::max<int>( maxY, posY ); |
3050 | |
|
3051 | 0 | dstcoeff.at( posX, posY ) = ( signPattern & 1u ? -AbsCoeff : AbsCoeff ); |
3052 | 0 | coeff[ sigBlkPos[ k ] ] = ( signPattern & 1u ? -AbsCoeff : AbsCoeff ); |
3053 | 0 | signPattern >>= 1; |
3054 | 0 | } |
3055 | 0 | } |
3056 | | |
3057 | | |
3058 | | //================================================================================ |
3059 | | // helper functions |
3060 | | //-------------------------------------------------------------------------------- |
3061 | | // unsigned unary_max_symbol ( ctxId0, ctxId1, maxSymbol ) |
3062 | | // unsigned unary_max_eqprob ( maxSymbol ) |
3063 | | // unsigned exp_golomb_eqprob( count ) |
3064 | | //================================================================================ |
3065 | | |
3066 | | unsigned CABACReader::unary_max_symbol( unsigned ctxId0, unsigned ctxIdN, unsigned maxSymbol ) |
3067 | 0 | { |
3068 | 0 | unsigned onesRead = 0; |
3069 | 0 | while( onesRead < maxSymbol && m_BinDecoder.decodeBin( onesRead == 0 ? ctxId0 : ctxIdN ) == 1 ) |
3070 | 0 | { |
3071 | 0 | ++onesRead; |
3072 | 0 | } |
3073 | 0 | return onesRead; |
3074 | 0 | } |
3075 | | |
3076 | | |
3077 | | unsigned CABACReader::unary_max_eqprob( unsigned maxSymbol ) |
3078 | 0 | { |
3079 | 0 | for( unsigned k = 0; k < maxSymbol; k++ ) |
3080 | 0 | { |
3081 | 0 | if( !m_BinDecoder.decodeBinEP() ) |
3082 | 0 | { |
3083 | 0 | return k; |
3084 | 0 | } |
3085 | 0 | } |
3086 | 0 | return maxSymbol; |
3087 | 0 | } |
3088 | | |
3089 | | |
3090 | | unsigned CABACReader::exp_golomb_eqprob( unsigned count ) |
3091 | 0 | { |
3092 | 0 | unsigned symbol = 0; |
3093 | 0 | unsigned bit = 1; |
3094 | 0 | while( bit ) |
3095 | 0 | { |
3096 | 0 | bit = m_BinDecoder.decodeBinEP( ); |
3097 | 0 | symbol += bit << count++; |
3098 | 0 | } |
3099 | 0 | if( --count ) |
3100 | 0 | { |
3101 | 0 | symbol += m_BinDecoder.decodeBinsEP( count ); |
3102 | 0 | } |
3103 | 0 | return symbol; |
3104 | 0 | } |
3105 | | |
3106 | | unsigned CABACReader::code_unary_fixed( unsigned ctxId, unsigned unary_max, unsigned fixed ) |
3107 | 0 | { |
3108 | 0 | unsigned idx; |
3109 | 0 | bool unary = m_BinDecoder.decodeBin( ctxId ); |
3110 | 0 | if( unary ) |
3111 | 0 | { |
3112 | 0 | idx = unary_max_eqprob( unary_max ); |
3113 | 0 | } |
3114 | 0 | else |
3115 | 0 | { |
3116 | 0 | idx = unary_max + 1 + m_BinDecoder.decodeBinsEP( fixed ); |
3117 | 0 | } |
3118 | 0 | return idx; |
3119 | 0 | } |
3120 | | |
3121 | | void CABACReader::mip_flag( CodingUnit& cu ) |
3122 | 0 | { |
3123 | 0 | if( !cu.sps->getUseMIP() ) |
3124 | 0 | { |
3125 | | //cu.setMipFlag( false ); |
3126 | 0 | return; |
3127 | 0 | } |
3128 | | |
3129 | 0 | unsigned ctxId = DeriveCtx::CtxMipFlag( cu ); |
3130 | 0 | cu.setMipFlag( m_BinDecoder.decodeBin( Ctx::MipFlag( ctxId ) ) ); |
3131 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "mip_flag() pos=(%d,%d) mode=%d\n", cu.lumaPos().x, cu.lumaPos().y, cu.mipFlag() ? 1 : 0 ); |
3132 | 0 | } |
3133 | | |
3134 | | void CABACReader::mip_pred_mode( CodingUnit &cu ) |
3135 | 0 | { |
3136 | 0 | cu.setMipTransposedFlag( !!m_BinDecoder.decodeBinEP() ); |
3137 | |
|
3138 | 0 | uint32_t mipMode; |
3139 | 0 | const int numModes = getNumModesMip( cu.Y() ); |
3140 | 0 | xReadTruncBinCode( mipMode, numModes ); |
3141 | 0 | cu.intraDir[CHANNEL_TYPE_LUMA] = mipMode; |
3142 | 0 | CHECKD( cu.intraDir[CHANNEL_TYPE_LUMA] < 0 || cu.intraDir[CHANNEL_TYPE_LUMA] >= numModes, "Invalid MIP mode" ); |
3143 | |
|
3144 | 0 | DTRACE( g_trace_ctx, D_SYNTAX, "mip_pred_mode() pos=(%d,%d) mode=%d transposed=%d\n", cu.lumaPos().x, cu.lumaPos().y, cu.intraDir[CHANNEL_TYPE_LUMA], cu.mipTransposedFlag() ? 1 : 0 ); |
3145 | 0 | } |
3146 | | |
3147 | | } |