Coverage Report

Created: 2026-06-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
713
{
62
713
  SliceType sliceType  = slice.getSliceType();
63
713
  int       qp         = slice.getSliceQp();
64
713
  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
713
  m_BinDecoder.reset( qp, (int)sliceType );
80
713
}
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
64
{
92
64
  if( m_BinDecoder.decodeBinTrm() )
93
8
  {
94
8
    m_BinDecoder.finish();
95
8
    m_Bitstream->readOutTrailingBits();
96
8
    return true;
97
8
  }
98
56
  return false;
99
64
}
100
101
void CABACReader::remaining_bytes( bool noTrailingBytesExpected )
102
3
{
103
3
  if( noTrailingBytesExpected )
104
0
  {
105
//    CHECK( 0 != m_Bitstream->getNumBitsLeft(), "Bits left when not supposed" );
106
0
  }
107
3
  else
108
3
  {
109
24
    while( m_Bitstream->getNumBitsLeft() )
110
21
    {
111
21
      unsigned trailingNullByte = m_Bitstream->readByte();
112
21
      CHECK( trailingNullByte != 0, "Trailing byte should be '0', but has a value of " << std::hex << trailingNullByte << std::dec << "\n" );
113
21
    }
114
3
  }
115
3
}
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
5.43k
{
130
5.43k
  m_slice = slice;
131
132
5.43k
  CUCtx cuCtx( qps[CH_L] );
133
5.43k
  Partitioner &partitioner = m_partL;
134
135
5.43k
  partitioner.initCtu( area, CH_L, cs, *m_slice );
136
5.43k
  partitioner.treeType = TREE_D;
137
5.43k
  partitioner.modeType = MODE_TYPE_ALL;
138
139
5.43k
  sao( cs, ctuRsAddr );
140
141
5.43k
  readAlf(cs, ctuRsAddr, partitioner);
142
143
5.43k
  bool isLast = false;
144
145
5.43k
  if( partitioner.isDualITree && cs.pcv->chrFormat != CHROMA_400 )
146
5.42k
  {
147
5.42k
    CUCtx cuCtxC( qps[CH_C] );
148
5.42k
    Partitioner &partitionerC = m_partC;
149
150
5.42k
    partitionerC.initCtu( area, CH_C, cs, *m_slice );
151
5.42k
    partitionerC.treeType = TREE_D;
152
5.42k
    partitionerC.modeType = MODE_TYPE_ALL;
153
154
5.42k
    dt_implicit_qt_split( cs, partitioner, cuCtx, partitionerC, cuCtxC );
155
156
5.42k
    qps[CH_L] = cuCtx.qp;
157
5.42k
    qps[CH_C] = cuCtxC.qp;
158
5.42k
  }
159
1
  else
160
1
  {
161
1
    isLast = coding_tree( cs, partitioner, cuCtx );
162
1
    qps[CH_L] = cuCtx.qp;
163
1
  }
164
165
5.43k
  DTRACE_COND( ctuRsAddr == 0, g_trace_ctx, D_QP_PER_CTU, "\n%4d %2d", cs.picture->poc, m_slice->getSliceQp() );
166
5.43k
  DTRACE     (                 g_trace_ctx, D_QP_PER_CTU, " %3d",           qps[CH_L] - m_slice->getSliceQp() );
167
168
5.43k
  return isLast;
169
5.43k
}
170
171
172
bool CABACReader::dt_implicit_qt_split( CodingStructure& cs, Partitioner& partitionerL, CUCtx& cuCtxL, Partitioner& partitionerC, CUCtx& cuCtxC )
173
25.8k
{
174
25.8k
  if( partitionerL.currArea().lwidth() > 64 )
175
5.42k
  {
176
5.42k
    const PPS& pps = *cs.pps;
177
    // Reset delta QP coding flag and ChromaQPAdjustemt coding flag
178
5.42k
    if( pps.getUseDQP() && partitionerL.currQgEnable() )
179
5.42k
    {
180
5.42k
      cuCtxL.qgStart    = true;
181
5.42k
      cuCtxL.isDQPCoded = false;
182
      
183
5.42k
      cuCtxC.qgStart    = true;
184
5.42k
      cuCtxC.isDQPCoded = false;
185
5.42k
    }
186
187
5.42k
    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
5.42k
    partitionerL.splitCurrArea( CU_QUAD_SPLIT, cs );
195
5.42k
    partitionerC.splitCurrArea( CU_QUAD_SPLIT, cs );
196
197
5.42k
    bool lastSegment = false;
198
199
5.42k
    do
200
20.6k
    {
201
20.6k
      if( !lastSegment && cs.area.blocks[partitionerL.chType].contains( partitionerL.currArea().blocks[partitionerL.chType].pos() ) )
202
20.4k
      {
203
20.4k
        lastSegment = dt_implicit_qt_split( cs, partitionerL, cuCtxL, partitionerC, cuCtxC );
204
20.4k
      }
205
20.6k
    } while( partitionerL.nextPart( cs ) && partitionerC.nextPart( cs ) );
206
207
5.42k
    return lastSegment;
208
5.42k
  }
209
210
20.4k
  bool isLast = coding_tree( cs, partitionerL, cuCtxL );
211
20.4k
  CHECKD( isLast, "Chroma not parsed but end of slice signalled!" );
212
20.4k
  isLast      = coding_tree( cs, partitionerC, cuCtxC );
213
214
20.4k
  return isLast;
215
25.8k
}
216
217
short CABACReader::readAlfCtuFilterIndex( CodingStructure& cs, unsigned ctuRsAddr )
218
5.29k
{
219
5.29k
  const unsigned numAps               = m_slice->getNumAlfAps();
220
5.29k
  const unsigned numAvailableFiltSets = numAps + NUM_FIXED_FILTER_SETS;
221
5.29k
  uint32_t filtIndex = 0;
222
223
5.29k
  const bool usePrevFilt = numAps > 0
224
5.15k
                        && m_BinDecoder.decodeBin( Ctx::AlfUseTemporalFilt() );
225
226
5.29k
  if( usePrevFilt )
227
4.79k
  {
228
4.79k
    if( numAps > 1 )
229
0
    {
230
0
      xReadTruncBinCode( filtIndex, numAvailableFiltSets - NUM_FIXED_FILTER_SETS );
231
0
    }
232
233
4.79k
    filtIndex += ( unsigned ) ( NUM_FIXED_FILTER_SETS );
234
4.79k
  }
235
500
  else
236
500
  {
237
500
    xReadTruncBinCode( filtIndex, NUM_FIXED_FILTER_SETS );
238
500
  }
239
240
5.29k
  return filtIndex;
241
5.29k
}
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
5.43k
{
251
252
5.43k
  SAOBlkParam&      sao_ctu_pars            = cs.getCtuData( ctuRsAddr ).saoParam;
253
5.43k
  sao_ctu_pars[ COMPONENT_Y  ].modeIdc      = SAO_MODE_OFF;
254
5.43k
  sao_ctu_pars[ COMPONENT_Cb ].modeIdc      = SAO_MODE_OFF;
255
5.43k
  sao_ctu_pars[ COMPONENT_Cr ].modeIdc      = SAO_MODE_OFF;
256
257
5.43k
  const SPS&   sps                          = *cs.sps;
258
5.43k
  const Slice& slice                        = *m_slice;
259
5.43k
  const bool   slice_sao_luma_flag          = ( slice.getSaoEnabledFlag( CHANNEL_TYPE_LUMA ) );
260
5.43k
  const bool   slice_sao_chroma_flag        = ( slice.getSaoEnabledFlag( CHANNEL_TYPE_CHROMA ) && sps.getChromaFormatIdc() != CHROMA_400 );
261
262
5.43k
  if( !slice_sao_luma_flag && !slice_sao_chroma_flag )
263
0
  {
264
0
    return;
265
0
  }
266
267
  // merge
268
5.43k
  int             frame_width_in_ctus     = cs.pcv->widthInCtus;
269
5.43k
  int             ry                      = ctuRsAddr      / frame_width_in_ctus;
270
5.43k
  int             rx                      = ctuRsAddr - ry * frame_width_in_ctus;
271
5.43k
  int             sao_merge_type          = -1;
272
5.43k
  const Position  pos( rx * cs.pcv->maxCUWidth, ry * cs.pcv->maxCUHeight );
273
5.43k
  const unsigned  curSliceIdx = m_slice->getIndependentSliceIdx();
274
5.43k
  const unsigned  curTileIdx  = cs.pps->getTileIdx( pos );
275
276
5.43k
  if( cs.getCURestricted( pos.offset(-(int)cs.pcv->maxCUWidth, 0), pos, curSliceIdx, curTileIdx, CH_L ) )
277
3.75k
  {
278
    // sao_merge_left_flag
279
3.75k
    sao_merge_type  += int( m_BinDecoder.decodeBin( Ctx::SaoMergeFlag() ) );
280
3.75k
  }
281
282
5.43k
  if( sao_merge_type < 0 && cs.getCURestricted( pos.offset(0, -(int)cs.pcv->maxCUHeight), pos, curSliceIdx, curTileIdx, CH_L ) )
283
1.39k
  {
284
    // sao_merge_above_flag
285
1.39k
    sao_merge_type  += int( m_BinDecoder.decodeBin( Ctx::SaoMergeFlag() ) ) << 1;
286
1.39k
  }
287
5.43k
  if( sao_merge_type >= 0 )
288
3.92k
  {
289
3.92k
    if( slice_sao_luma_flag || slice_sao_chroma_flag )
290
3.92k
    {
291
3.92k
      sao_ctu_pars[ COMPONENT_Y  ].modeIdc  = SAO_MODE_MERGE;
292
3.92k
      sao_ctu_pars[ COMPONENT_Y  ].typeIdc  = sao_merge_type;
293
3.92k
    }
294
3.92k
    if( slice_sao_chroma_flag )
295
3.91k
    {
296
3.91k
      sao_ctu_pars[ COMPONENT_Cb ].modeIdc  = SAO_MODE_MERGE;
297
3.91k
      sao_ctu_pars[ COMPONENT_Cr ].modeIdc  = SAO_MODE_MERGE;
298
3.91k
      sao_ctu_pars[ COMPONENT_Cb ].typeIdc  = sao_merge_type;
299
3.91k
      sao_ctu_pars[ COMPONENT_Cr ].typeIdc  = sao_merge_type;
300
3.91k
    }
301
3.92k
    return;
302
3.92k
  }
303
304
  // explicit parameters
305
1.50k
  const ComponentID firstComp = ( slice_sao_luma_flag   ? COMPONENT_Y  : COMPONENT_Cb );
306
1.50k
  const ComponentID lastComp  = ( slice_sao_chroma_flag ? COMPONENT_Cr : COMPONENT_Y  );
307
308
6.01k
  for( ComponentID compID = firstComp; compID <= lastComp; compID = ComponentID( compID + 1 ) )
309
4.50k
  {
310
4.50k
    SAOOffset& sao_pars = sao_ctu_pars[ compID ];
311
312
    // sao_type_idx_luma / sao_type_idx_chroma
313
4.50k
    if( compID != COMPONENT_Cr )
314
3.00k
    {
315
3.00k
      if( m_BinDecoder.decodeBin( Ctx::SaoTypeIdx() ) )
316
1.52k
      {
317
1.52k
        if( m_BinDecoder.decodeBinEP( ) )
318
1.12k
        {
319
          // edge offset
320
1.12k
          sao_pars.modeIdc = SAO_MODE_NEW;
321
1.12k
          sao_pars.typeIdc = SAO_TYPE_START_EO;
322
1.12k
        }
323
399
        else
324
399
        {
325
          // band offset
326
399
          sao_pars.modeIdc = SAO_MODE_NEW;
327
399
          sao_pars.typeIdc = SAO_TYPE_START_BO;
328
399
        }
329
1.52k
      }
330
3.00k
    }
331
1.50k
    else //Cr, follow Cb SAO type
332
1.50k
    {
333
1.50k
      sao_pars.modeIdc = sao_ctu_pars[ COMPONENT_Cb ].modeIdc;
334
1.50k
      sao_pars.typeIdc = sao_ctu_pars[ COMPONENT_Cb ].typeIdc;
335
1.50k
    }
336
4.50k
    if( sao_pars.modeIdc == SAO_MODE_OFF )
337
2.62k
    {
338
2.62k
      continue;
339
2.62k
    }
340
341
    // sao_offset_abs
342
1.87k
    int       offset[4];
343
1.87k
    const int maxOffsetQVal = SampleAdaptiveOffset::getMaxOffsetQVal( sps.getBitDepth() );
344
1.87k
    offset    [0]           = (int)unary_max_eqprob( maxOffsetQVal );
345
1.87k
    offset    [1]           = (int)unary_max_eqprob( maxOffsetQVal );
346
1.87k
    offset    [2]           = (int)unary_max_eqprob( maxOffsetQVal );
347
1.87k
    offset    [3]           = (int)unary_max_eqprob( maxOffsetQVal );
348
349
    // band offset mode
350
1.87k
    if( sao_pars.typeIdc == SAO_TYPE_START_BO )
351
572
    {
352
      // sao_offset_sign
353
2.86k
      for( int k = 0; k < 4; k++ )
354
2.28k
      {
355
2.28k
        if( offset[k] && m_BinDecoder.decodeBinEP( ) )
356
690
        {
357
690
          offset[k] = -offset[k];
358
690
        }
359
2.28k
      }
360
      // sao_band_position
361
572
      sao_pars.typeAuxInfo = m_BinDecoder.decodeBinsEP( NUM_SAO_BO_CLASSES_LOG2 );
362
2.86k
      for( int k = 0; k < 4; k++ )
363
2.28k
      {
364
2.28k
        sao_pars.offset[ ( sao_pars.typeAuxInfo + k ) % MAX_NUM_SAO_CLASSES ] = offset[k];
365
2.28k
      }
366
572
      continue;
367
572
    }
368
369
    // edge offset mode
370
1.30k
    sao_pars.typeAuxInfo = 0;
371
1.30k
    if( compID != COMPONENT_Cr )
372
1.12k
    {
373
      // sao_eo_class_luma / sao_eo_class_chroma
374
1.12k
      sao_pars.typeIdc += m_BinDecoder.decodeBinsEP( NUM_SAO_EO_TYPES_LOG2 );
375
1.12k
    }
376
184
    else
377
184
    {
378
184
      sao_pars.typeIdc  = sao_ctu_pars[ COMPONENT_Cb ].typeIdc;
379
184
    }
380
1.30k
    sao_pars.offset[ SAO_CLASS_EO_FULL_VALLEY ] =  offset[0];
381
1.30k
    sao_pars.offset[ SAO_CLASS_EO_HALF_VALLEY ] =  offset[1];
382
1.30k
    sao_pars.offset[ SAO_CLASS_EO_PLAIN       ] =  0;
383
1.30k
    sao_pars.offset[ SAO_CLASS_EO_HALF_PEAK   ] = -offset[2];
384
1.30k
    sao_pars.offset[ SAO_CLASS_EO_FULL_PEAK   ] = -offset[3];
385
1.30k
  }
386
1.50k
}
387
388
//================================================================================
389
//    void  readAlf( cs, ctuRsAddr, partitioner )
390
//================================================================================
391
void CABACReader::readAlf( CodingStructure& cs, unsigned int ctuRsAddr, const Partitioner& partitioner )
392
5.43k
{
393
5.43k
  const PreCalcValues& pcv                = *cs.pcv;
394
5.43k
  int                 frame_width_in_ctus = pcv.widthInCtus;
395
5.43k
  int                 ry                  = ctuRsAddr / frame_width_in_ctus;
396
5.43k
  int                 rx                  = ctuRsAddr - ry * frame_width_in_ctus;
397
5.43k
  const Position      pos                 ( rx * cs.pcv->maxCUWidth, ry * cs.pcv->maxCUHeight );
398
5.43k
  bool                leftAvail           = cs.getCURestricted( pos.offset( -1, 0 ), pos, partitioner.currSliceIdx, partitioner.currTileIdx, CH_L ) ? true : false;
399
5.43k
  bool                aboveAvail          = cs.getCURestricted( pos.offset( 0, -1 ), pos, partitioner.currSliceIdx, partitioner.currTileIdx, CH_L ) ? true : false;
400
  
401
5.43k
  CtuAlfData& currAlfData = cs.getCtuData( ctuRsAddr ).alfParam;
402
5.43k
  CtuAlfData  leftAlfData, aboveAlfData;
403
404
5.43k
  if( leftAvail )  leftAlfData  = cs.getCtuData( ctuRsAddr -                   1 ).alfParam;
405
5.43k
  if( aboveAvail ) aboveAlfData = cs.getCtuData( ctuRsAddr - frame_width_in_ctus ).alfParam;
406
407
5.43k
  if( m_slice->getAlfEnabledFlag( COMPONENT_Y ) )
408
5.35k
  {
409
21.4k
    for( int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++ )
410
16.0k
    {
411
16.0k
      if( m_slice->getAlfEnabledFlag( ( ComponentID ) compIdx ) )
412
16.0k
      {
413
        //uint8_t* ctbAlfFlag = m_slice->getPic()->getAlfCtuEnableFlag( compIdx );
414
16.0k
        int ctx = 0;
415
16.0k
          ctx += leftAlfData.alfCtuEnableFlag[compIdx];
416
16.0k
          ctx += aboveAlfData.alfCtuEnableFlag[compIdx];
417
      
418
16.0k
        currAlfData.alfCtuEnableFlag[compIdx] = m_BinDecoder.decodeBin( Ctx::ctbAlfFlag( compIdx * 3 + ctx ) );
419
      
420
16.0k
        if( isLuma( ( ComponentID ) compIdx ) && currAlfData.alfCtuEnableFlag[compIdx] )
421
5.29k
        {
422
5.29k
          currAlfData.alfCtbFilterIndex = readAlfCtuFilterIndex( cs, ctuRsAddr );
423
5.29k
        }
424
425
16.0k
        if( isChroma( ( ComponentID ) compIdx ) )
426
10.7k
        {
427
10.7k
          const int apsIdx                  = m_slice->getAlfApsIdChroma();
428
10.7k
          CHECK( m_slice->getAlfAPSs()[apsIdx] == nullptr, "APS not initialized" );
429
10.7k
          const AlfSliceParam& alfParam     = m_slice->getAlfAPSs()[apsIdx]->getAlfAPSParam();
430
10.7k
          const int numAlts                 = alfParam.numAlternativesChroma;
431
10.7k
          currAlfData.alfCtuAlternative[compIdx - 1] = 0;
432
433
10.7k
          if( currAlfData.alfCtuEnableFlag[compIdx] )
434
10.5k
          {
435
10.5k
            uint8_t decoded = 0;
436
13.8k
            while( decoded < numAlts - 1 && m_BinDecoder.decodeBin( Ctx::ctbAlfAlternative( compIdx - 1 ) ) )
437
3.23k
              ++decoded;
438
10.5k
            currAlfData.alfCtuAlternative[compIdx - 1] = decoded;
439
10.5k
          }
440
10.7k
        }
441
16.0k
      }
442
16.0k
    }
443
5.35k
  }
444
16.2k
  for( int compIdx = 1; compIdx < getNumberValidComponents( cs.pcv->chrFormat ); compIdx++ )
445
10.8k
  {
446
10.8k
    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
10.8k
  }
467
5.43k
}
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
277k
{
479
277k
  const PPS      &pps             = *cs.pps;
480
277k
        UnitArea  currArea        = partitioner.currArea();
481
277k
  bool            lastSegment     = false;
482
277k
  bool            chromaNotSplit  = false;
483
277k
  const ModeType modeTypeParent   = partitioner.modeType;
484
485
  // Reset delta QP coding flag and ChromaQPAdjustemt coding flag
486
277k
  if( pps.getUseDQP() && partitioner.currQgEnable() && !isChroma( partitioner.chType ) )
487
20.4k
  {
488
20.4k
    cuCtx.qgStart     = true;
489
20.4k
    cuCtx.isDQPCoded  = false;
490
20.4k
  }
491
277k
  if( m_slice->getUseChromaQpAdj() && partitioner.currQgChromaEnable() )
492
0
  {
493
0
    cuCtx.isChromaQpAdjCoded = false;
494
0
    cs.chromaQpAdj           = 0;
495
0
  }
496
497
277k
  const PartSplit split = split_cu_mode( cs, partitioner );
498
499
277k
  if( split != CU_DONT_SPLIT )
500
94.5k
  {
501
94.5k
    partitioner.modeType = mode_constraint( cs, partitioner, split ); //change for child nodes
502
503
    //decide chroma split or not
504
94.5k
    chromaNotSplit = modeTypeParent == MODE_TYPE_ALL && partitioner.modeType == MODE_TYPE_INTRA;
505
506
94.5k
    CHECK( chromaNotSplit && partitioner.chType != CHANNEL_TYPE_LUMA, "chType must be luma" );
507
508
94.5k
    if( partitioner.treeType == TREE_D )
509
94.5k
    {
510
94.5k
      partitioner.treeType = chromaNotSplit ? TREE_L : TREE_D;
511
94.5k
    }
512
513
94.5k
    partitioner.splitCurrArea( split, cs );
514
515
94.5k
    do
516
237k
    {
517
237k
      if( !lastSegment && cs.area.blocks[partitioner.chType].contains( partitioner.currArea().blocks[partitioner.chType].pos() ) )
518
237k
      {
519
237k
        lastSegment = coding_tree( cs, partitioner, cuCtx );
520
237k
      }
521
237k
    } while( partitioner.nextPart( cs ) );
522
523
94.5k
    partitioner.exitCurrSplit( cs );
524
525
94.5k
    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
94.5k
    partitioner.modeType = modeTypeParent;
546
547
94.5k
    return lastSegment;
548
94.5k
  }
549
550
182k
  TreeType treeType = partitioner.treeType;
551
552
182k
  if( isChroma( partitioner.chType ) )                                 { currArea.Y()                  = CompArea(); treeType = TREE_C; }
553
112k
  else if( partitioner.isDualITree || partitioner.treeType == TREE_L ) { currArea.Cb() = currArea.Cr() = CompArea(); treeType = TREE_L; }
554
555
182k
  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
182k
  partitioner.setCUData( cu );
568
182k
  cu.slice   = m_slice;
569
182k
  cu.pps     = cu.slice->getPPS();
570
182k
  cu.sps     = cu.slice->getSPS();
571
182k
  cu.tileIdx = partitioner.currTileIdx;
572
182k
  int lumaQPinLocalDualTree = -1;
573
574
  // Predict QP on start of quantization group
575
182k
  if( cuCtx.qgStart )
576
25.6k
  {
577
25.6k
    cuCtx.qgStart = false;
578
25.6k
    cuCtx.qp      = CU::predictQP( cu, cuCtx.qp );
579
25.6k
  }
580
581
182k
  if( pps.getUseDQP() && partitioner.isSepTree( cs ) && isChroma( cu.chType() ) )
582
70.0k
  {
583
70.0k
    const Position chromaCentral( cu.chromaPos().offset( cu.chromaSize().width >> 1, cu.chromaSize().height >> 1 ) );
584
70.0k
    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
70.0k
    const CodingUnit* colLumaCu = cs.getCU( lumaRefPos, CHANNEL_TYPE_LUMA );
589
70.0k
    CHECK( colLumaCu == nullptr, "colLumaCU shall exist" );
590
70.0k
    lumaQPinLocalDualTree = cuCtx.qp;
591
592
70.0k
    if( colLumaCu ) cuCtx.qp = colLumaCu->qp;
593
70.0k
  }
594
595
182k
  cu.qp = cuCtx.qp;                 //NOTE: CU QP can be changed by deltaQP signaling at TU level
596
182k
  cu.chromaQpAdj = cs.chromaQpAdj;  //NOTE: CU chroma QP adjustment can be changed by adjustment signaling at TU level
597
598
  // coding unit
599
182k
  bool isLastCtu = coding_unit( cu, partitioner, cuCtx );
600
  //recover cuCtx.qp to luma qp after decoding the chroma CU
601
182k
  if( pps.getUseDQP() && partitioner.isSepTree( cs ) && isChroma( cu.chType() ) )
602
69.8k
  {
603
69.8k
    cuCtx.qp = lumaQPinLocalDualTree;
604
69.8k
  }
605
606
182k
  if( isChromaEnabled( cs.pcv->chrFormat ) )
607
182k
  for( TransformUnit& tu : TUTraverser( &cu.firstTU, cu.lastTU->next ) )
608
202k
  {
609
202k
    if( tu.Cb().valid() )
610
82.9k
    {
611
82.9k
      QpParam cQP( tu, COMPONENT_Cb, false );
612
82.9k
      tu.chromaQp[COMPONENT_Cb - 1] = cQP.Qp( false );
613
82.9k
    }
614
615
202k
    if( tu.Cr().valid() )
616
82.9k
    {
617
82.9k
      QpParam cQP( tu, COMPONENT_Cr, false );
618
82.9k
      tu.chromaQp[COMPONENT_Cr - 1] = cQP.Qp( false );
619
82.9k
    }
620
202k
  }
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
182k
  return isLastCtu;
632
182k
}
633
634
int signalModeCons( const CodingStructure& cs, const Slice* slice, const PartSplit split, const Partitioner &partitioner, const ModeType modeTypeParent )
635
94.5k
{
636
94.5k
  if( partitioner.isDualITree || modeTypeParent != MODE_TYPE_ALL || partitioner.currArea().chromaFormat == CHROMA_444 || partitioner.currArea().chromaFormat == CHROMA_400 )
637
94.5k
    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
94.5k
}
656
657
ModeType CABACReader::mode_constraint( CodingStructure& cs, Partitioner &partitioner, PartSplit splitMode )
658
94.5k
{
659
94.5k
  const int val = signalModeCons( cs, m_slice, splitMode, partitioner, partitioner.modeType );
660
661
94.5k
  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
94.5k
  else if( val == LDT_MODE_TYPE_INFER )
670
0
  {
671
0
    return MODE_TYPE_INTRA;
672
0
  }
673
94.5k
  else
674
94.5k
  {
675
94.5k
    return partitioner.modeType;
676
94.5k
  }
677
94.5k
}
678
679
PartSplit CABACReader::split_cu_mode( CodingStructure& cs, Partitioner &partitioner )
680
277k
{
681
277k
  PartSplit mode = CU_DONT_SPLIT;
682
683
277k
  bool canNo, canQt, canBh, canBv, canTh, canTv;
684
277k
  partitioner.canSplit( cs, canNo, canQt, canBh, canBv, canTh, canTv );
685
686
277k
  const unsigned numHor = canBh + canTh;
687
277k
  const unsigned numVer = canBv + canTv;
688
277k
  unsigned numSplit = ( canQt << 1 ) + numHor + numVer;
689
690
277k
  bool isSplit = !!numSplit;
691
692
277k
#if !ENABLE_TRACING
693
277k
  if( canNo && !isSplit )
694
80.2k
  {
695
80.2k
    DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_mode() ctx=0 split=0\n" );
696
80.2k
    return CU_DONT_SPLIT;
697
80.2k
  }
698
699
197k
#endif
700
197k
  unsigned ctxSplit, ctxQtSplit, ctxBttHV, ctxBtt12;
701
702
197k
  const CodingUnit* cuLeft   = partitioner.currPartLevel().cuLeft;
703
197k
  const CodingUnit* cuAbove  = partitioner.currPartLevel().cuAbove;
704
197k
  const unsigned widthCurr   = partitioner.currArea().blocks[partitioner.chType].width;
705
197k
  const unsigned heightCurr  = partitioner.currArea().blocks[partitioner.chType].height;
706
707
197k
#if !ENABLE_TRACING
708
197k
  if( canNo && isSplit )
709
196k
#endif
710
196k
  {
711
    ///////////////////////
712
    // CTX do split (0-8)
713
    ///////////////////////
714
715
196k
    ctxSplit  = ( cuLeft  && cuLeft ->blocks[partitioner.chType].height < heightCurr );
716
196k
    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
196k
    static constexpr int ctxOffset[] = { 0, 0, 0, 3, 3, 6, 6 };
721
196k
    ctxSplit += ctxOffset[numSplit];
722
723
#if ENABLE_TRACING
724
    if( canNo && isSplit )
725
#endif // endif
726
727
196k
    isSplit = m_BinDecoder.decodeBin( Ctx::SplitFlag( ctxSplit ) );
728
196k
  }
729
730
197k
  DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_mode() ctx=%d split=%d\n", ctxSplit, isSplit );
731
732
197k
  if( !isSplit )
733
102k
  {
734
102k
    return CU_DONT_SPLIT;
735
102k
  }
736
737
94.5k
  const bool canBtt = !!numHor || !!numVer;
738
94.5k
  bool       isQt   = canQt;
739
740
94.5k
#if !ENABLE_TRACING
741
94.5k
  if( isQt && canBtt )
742
48.9k
#endif
743
48.9k
  {
744
    //////////////////////////
745
    // CTX is qt split (0-5)
746
    //////////////////////////
747
48.9k
    ctxQtSplit  = ( cuLeft  && cuLeft->qtDepth  > partitioner.currQtDepth );
748
48.9k
    ctxQtSplit += ( cuAbove && cuAbove->qtDepth > partitioner.currQtDepth );
749
48.9k
    ctxQtSplit += partitioner.currQtDepth < 2 ? 0 : 3;
750
751
#if ENABLE_TRACING
752
    if( isQt && canBtt )
753
#endif
754
48.9k
    isQt = m_BinDecoder.decodeBin( Ctx::SplitQtFlag( ctxQtSplit ) );
755
48.9k
  }
756
757
94.5k
  DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_mode() ctx=%d qt=%d\n", ctxQtSplit, isQt );
758
759
94.5k
  if( isQt )
760
18.4k
  {
761
18.4k
    return CU_QUAD_SPLIT;
762
18.4k
  }
763
764
76.1k
  const bool canHor = !!numHor;
765
76.1k
  bool        isVer = !!numVer;
766
767
76.1k
#if !ENABLE_TRACING
768
76.1k
  if( isVer && canHor )
769
71.5k
#endif
770
71.5k
  {
771
    ////////////////////////////
772
    // CTX is ver split (0-4)
773
    ////////////////////////////
774
71.5k
    ctxBttHV = 0;
775
776
71.5k
    if( numVer == numHor )
777
56.3k
    {
778
56.3k
      if( cuLeft && cuAbove )
779
49.2k
      {
780
49.2k
        const int wIdxAbove = getLog2( cuAbove->blocks[partitioner.chType].width  );
781
49.2k
        const int hIdxLeft  = getLog2( cuLeft ->blocks[partitioner.chType].height );
782
783
49.2k
        const int depAbove  = widthCurr  >> wIdxAbove;
784
49.2k
        const int depLeft   = heightCurr >> hIdxLeft;
785
786
49.2k
        if( depAbove == depLeft ) ctxBttHV = 0;
787
35.3k
        else if( depAbove < depLeft ) ctxBttHV = 1;
788
17.2k
        else ctxBttHV = 2;
789
49.2k
      }
790
56.3k
    }
791
15.1k
    else if( numVer < numHor )
792
9.57k
    {
793
9.57k
      ctxBttHV = 3;
794
9.57k
    }
795
5.59k
    else
796
5.59k
    {
797
5.59k
      ctxBttHV = 4;
798
5.59k
    }
799
800
#if ENABLE_TRACING
801
    if( isVer && canHor )
802
#endif
803
71.5k
    isVer = m_BinDecoder.decodeBin( Ctx::SplitHvFlag( ctxBttHV ) );
804
71.5k
  }
805
806
76.1k
  const bool can14 = isVer ? canTv : canTh;
807
76.1k
  bool        is12 = isVer ? canBv : canBh;
808
809
76.1k
#if !ENABLE_TRACING
810
76.1k
  if( is12 && can14 )
811
58.6k
#endif
812
58.6k
  {
813
    //////////////////////////
814
    // CTX is h/v bt (0-3)
815
    //////////////////////////
816
58.6k
    ctxBtt12 = !!( partitioner.currMtDepth <= 1 ) + ( isVer << 1 );
817
818
#if ENABLE_TRACING
819
    if( is12 && can14 )
820
#endif
821
58.6k
    is12 = m_BinDecoder.decodeBin( Ctx::Split12Flag( ctxBtt12 ) );
822
58.6k
  }
823
824
76.1k
  if     ( isVer && is12 )  mode = CU_VERT_SPLIT;
825
47.5k
  else if( isVer && !is12 ) mode = CU_TRIV_SPLIT;
826
41.4k
  else if( !isVer && is12 ) mode = CU_HORZ_SPLIT;
827
7.22k
  else                      mode = CU_TRIH_SPLIT;
828
829
76.1k
  DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_mode() ctxHv=%d ctx12=%d mode=%d\n", ctxBttHV, ctxBtt12, mode );
830
831
76.1k
  return mode;
832
94.5k
}
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
182k
{
858
182k
  CodingStructure& cs = *cu.cs;
859
860
182k
  DTRACE( g_trace_ctx, D_SYNTAX, "coding_unit() treeType=%d modeType=%d\n", cu.treeType(), cu.modeType() );
861
862
  // skip flag
863
182k
  if( !m_slice->isIntra() || cs.sps->getIBCFlag() )
864
182k
  {
865
182k
    if( cu.Y().valid() )
866
112k
    {
867
112k
      cu_skip_flag( cu );
868
112k
    }
869
870
    // skip data
871
182k
    if( cu.skip() )
872
4.86k
    {
873
4.86k
      cu.setColorTransform( false );
874
4.86k
      cs.addEmptyTUs      ( partitioner, cu );
875
4.86k
      MergeCtx              mrgCtx;
876
4.86k
      prediction_unit     ( cu );
877
4.86k
      return end_of_ctu   ( cu, cuCtx );
878
4.86k
    }
879
177k
    else
880
177k
    {
881
      // prediction mode and partitioning data
882
177k
      pred_mode( cu );
883
177k
    }
884
182k
  }
885
74
  else
886
74
  {
887
74
    cu.setPredMode( MODE_INTRA );
888
74
  }
889
890
  // TODO: palette stuff
891
892
177k
  if( CU::isIntra( cu ) )
893
145k
  {
894
145k
    adaptive_color_transform( cu );
895
145k
  }
896
897
  // prediction data ( intra prediction modes / reference indexes + motion vectors )
898
177k
  cu_pred_data( cu );
899
900
  // residual data ( coded block flags + transform coefficient levels )
901
177k
  cu_residual( cu, partitioner, cuCtx );
902
903
  // check end of cu
904
177k
  return end_of_ctu( cu, cuCtx );
905
182k
}
906
907
908
void CABACReader::cu_skip_flag( CodingUnit& cu )
909
112k
{
910
112k
  bool ibcFlag = cu.slice->getSPS()->getIBCFlag() && cu.lwidth() <= 64 && cu.lheight() <= 64;
911
912
112k
  if( ( cu.slice->isIntra() || CU::isConsIntra( cu ) ) && ibcFlag )
913
112k
  {
914
    //cu.setSkip       ( false );
915
    //cu.setRootCbf    ( false );
916
    //cu.setPredMode   ( MODE_INTRA );
917
    //cu.setMmvdFlag   ( false );
918
919
112k
    const unsigned ctxId = DeriveCtx::CtxSkipFlag( cu );
920
112k
    const unsigned skip  = m_BinDecoder.decodeBin( Ctx::SkipFlag( ctxId ) );
921
922
112k
    DTRACE( g_trace_ctx, D_SYNTAX, "cu_skip_flag() ctx=%d skip=%d\n", ctxId, skip ? 1 : 0 );
923
924
112k
    if( skip )
925
4.86k
    {
926
4.86k
      cu.setSkip       ( true );
927
      //cu.setRootCbf    ( false );
928
4.86k
      cu.setPredMode   ( MODE_IBC );
929
      //cu.setMmvdFlag   ( false );
930
4.86k
      cu.cs->hasIbcBlock[cu.ctuData->lineIdx] = 1;
931
4.86k
    }
932
933
112k
    return;
934
112k
  }
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
19.9k
{
993
19.9k
  const SPS *sps = cu.sps;
994
995
19.9k
  if( !sps->getAMVREnabledFlag() || !CU::hasSubCUNonZeroMVd( cu ) )
996
3.21k
  {
997
3.21k
    return;
998
3.21k
  }
999
16.7k
  unsigned value = 0;
1000
1001
16.7k
  if( CU::isIBC( cu ) )
1002
16.7k
    value = 1;
1003
0
  else
1004
0
    value = m_BinDecoder.decodeBin( Ctx::ImvFlag( 0 ) );
1005
1006
16.7k
  DTRACE( g_trace_ctx, D_SYNTAX, "imv_mode() value=%d ctx=%d\n", value, 0 );
1007
1008
16.7k
  if( value )
1009
16.7k
  {
1010
16.7k
    cu.setImv( value );
1011
1012
16.7k
    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
16.7k
    if( value )
1020
16.7k
    {
1021
16.7k
      value = m_BinDecoder.decodeBin( Ctx::ImvFlag( 1 ) );
1022
16.7k
      value++;
1023
16.7k
      cu.setImv( value );
1024
16.7k
      DTRACE( g_trace_ctx, D_SYNTAX, "imv_mode() value=%d ctx=%d\n", ( value - 1 ), 1 );
1025
16.7k
    }
1026
16.7k
  }
1027
1028
16.7k
  DTRACE( g_trace_ctx, D_SYNTAX, "imv_mode() IMVFlag=%d\n", cu.imv() );
1029
16.7k
}
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
177k
{
1057
177k
  if( CU::isConsInter( cu ) )
1058
0
  {
1059
    //cu.setPredMode( MODE_INTER );
1060
0
    return;
1061
0
  }
1062
1063
177k
  bool ibcAllowed = false;
1064
1065
177k
  if( cu.slice->isIntra() || ( cu.lwidth() == 4 && cu.lheight() == 4 ) || CU::isConsIntra( cu ) )
1066
177k
  {
1067
177k
    ibcAllowed = true;
1068
177k
    cu.setPredMode( MODE_INTRA );
1069
177k
  }
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
177k
  ibcAllowed &= isLuma( cu.chType() ) && cu.sps->getIBCFlag() && cu.lwidth() <= 64 && cu.lheight() <= 64;
1083
1084
177k
  if( ibcAllowed )
1085
107k
  {
1086
107k
    unsigned ctxidx = DeriveCtx::CtxIBCFlag( cu );
1087
1088
107k
    if( m_BinDecoder.decodeBin( Ctx::IBCFlag( ctxidx ) ) )
1089
31.9k
    {
1090
31.9k
      cu.setPredMode( MODE_IBC );
1091
31.9k
      cu.cs->hasIbcBlock[cu.ctuData->lineIdx] = 1;
1092
31.9k
    }
1093
107k
  }
1094
177k
}
1095
1096
void CABACReader::bdpcm_mode( CodingUnit& cu, const ComponentID compID )
1097
145k
{
1098
1099
145k
  if( !CU::bdpcmAllowed( cu, compID ) )
1100
57.9k
  {
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
57.9k
    return;
1112
57.9k
  }
1113
1114
87.9k
  const unsigned ctxId = isLuma( compID ) ? 0 : 2;
1115
87.9k
  int bdpcmMode = m_BinDecoder.decodeBin( Ctx::BDPCMMode( ctxId ) );
1116
1117
87.9k
  if( bdpcmMode )
1118
10.9k
  {
1119
10.9k
    bdpcmMode += m_BinDecoder.decodeBin( Ctx::BDPCMMode( ctxId + 1 ) );
1120
10.9k
  }
1121
1122
87.9k
  if( isLuma( compID ) )
1123
37.5k
  {
1124
37.5k
    cu.setBdpcmMode( bdpcmMode );
1125
37.5k
  }
1126
50.4k
  else
1127
50.4k
  {
1128
50.4k
    cu.setBdpcmModeChroma( bdpcmMode );
1129
50.4k
  }
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
87.9k
}
1142
1143
void CABACReader::cu_pred_data( CodingUnit &cu )
1144
177k
{
1145
177k
  if( CU::isIntra( cu ) )
1146
145k
  {
1147
145k
    if( isLuma( cu.chType() ) )
1148
75.9k
    {
1149
75.9k
      bdpcm_mode( cu, COMPONENT_Y );
1150
75.9k
      intra_luma_pred_mode( cu );
1151
75.9k
    }
1152
145k
    if( ( isChroma( cu.chType() ) || !CU::isSepTree( cu ) ) && isChromaEnabled( cu.chromaFormat ) )
1153
70.0k
    {
1154
70.0k
      bdpcm_mode( cu, ComponentID( CHANNEL_TYPE_CHROMA ) );
1155
70.0k
      intra_chroma_pred_mode( cu );
1156
70.0k
    }
1157
1158
145k
    return;
1159
145k
  }
1160
1161
31.9k
  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
31.9k
  prediction_unit     ( cu );
1169
1170
31.9k
  if( !cu.mergeFlag() )
1171
19.9k
  {
1172
19.9k
    if( cu.affineFlag() )
1173
0
      affine_amvr_mode( cu );
1174
19.9k
    else
1175
19.9k
      amvr_mode       ( cu );
1176
19.9k
    cu_bcw_flag       ( cu );
1177
19.9k
  }
1178
31.9k
}
1179
1180
void CABACReader::cu_bcw_flag(CodingUnit& cu)
1181
19.9k
{
1182
19.9k
  if( !CU::isBcwIdxCoded( cu ) )
1183
19.9k
  {
1184
19.9k
    return;
1185
19.9k
  }
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
22.7k
{
1211
22.7k
  int thresh;
1212
22.7k
  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
22.7k
  else
1224
22.7k
  {
1225
22.7k
    thresh = g_tbMax[maxSymbol];
1226
22.7k
  }
1227
1228
22.7k
  int val = 1 << thresh;
1229
22.7k
  int b = maxSymbol - val;
1230
22.7k
  symbol = m_BinDecoder.decodeBinsEP(thresh);
1231
1232
22.7k
  if (symbol >= val - b)
1233
14.2k
  {
1234
14.2k
    uint32_t altSymbol;
1235
14.2k
    altSymbol = m_BinDecoder.decodeBinEP();
1236
14.2k
    symbol <<= 1;
1237
14.2k
    symbol += altSymbol;
1238
14.2k
    symbol -= (val - b);
1239
14.2k
  }
1240
22.7k
}
1241
1242
void CABACReader::extend_ref_line( CodingUnit& cu )
1243
55.4k
{
1244
55.4k
  if( cu.bdpcmMode() || !cu.sps->getUseMRL() )
1245
31
  {
1246
    //cu.setMultiRefIdx( 0 );
1247
31
    return;
1248
31
  }
1249
1250
55.3k
  const bool isFirstLineOfCtu = ( cu.ly() & cu.cs->pcv->maxCUHeightMask ) == 0;
1251
1252
55.3k
  if( isFirstLineOfCtu )
1253
9.62k
  {
1254
    //cu.setMultiRefIdx( 0 );
1255
9.62k
    return;
1256
9.62k
  }
1257
1258
45.7k
  int multiRefIdx = 0;
1259
1260
45.7k
  multiRefIdx = m_BinDecoder.decodeBin( Ctx::MultiRefLineIdx( 0 ) ) == 1 ? MULTI_REF_LINE_IDX[1] : MULTI_REF_LINE_IDX[0];
1261
1262
45.7k
  if( multiRefIdx != MULTI_REF_LINE_IDX[0] )
1263
4.13k
  {
1264
4.13k
    multiRefIdx = m_BinDecoder.decodeBin( Ctx::MultiRefLineIdx( 1 ) ) == 1 ? MULTI_REF_LINE_IDX[2] : MULTI_REF_LINE_IDX[1];
1265
4.13k
  }
1266
1267
45.7k
  cu.setMultiRefIdx( multiRefIdx );
1268
45.7k
}
1269
1270
void CABACReader::intra_luma_pred_mode( CodingUnit &cu )
1271
75.9k
{
1272
75.9k
  if( cu.bdpcmMode() )
1273
9.57k
  {
1274
9.57k
    cu.intraDir[0] = cu.bdpcmMode() == 2 ? VER_IDX : HOR_IDX;
1275
9.57k
    return;
1276
9.57k
  }
1277
1278
66.3k
  mip_flag( cu );
1279
1280
66.3k
  if( cu.mipFlag() )
1281
10.9k
  {
1282
10.9k
    mip_pred_mode( cu );
1283
10.9k
    return;
1284
10.9k
  }
1285
1286
55.4k
  extend_ref_line( cu );
1287
1288
55.4k
  isp_mode( cu );
1289
1290
  // prev_intra_luma_pred_flag
1291
55.4k
  int mpmFlag;
1292
1293
55.4k
  if( cu.multiRefIdx() )
1294
4.13k
  {
1295
4.13k
    mpmFlag = true;
1296
4.13k
  }
1297
51.2k
  else
1298
51.2k
    mpmFlag = m_BinDecoder.decodeBin( Ctx::IPredMode[0]() );
1299
1300
55.4k
  unsigned mpm_pred[NUM_MOST_PROBABLE_MODES];  // mpm_idx / rem_intra_luma_pred_mode
1301
1302
55.4k
  PU::getIntraMPMs( cu, mpm_pred );
1303
1304
55.4k
  if( mpmFlag )
1305
44.1k
  {
1306
44.1k
    uint32_t ipred_idx = 0;
1307
1308
44.1k
    unsigned ctx = ( cu.ispMode() == NOT_INTRA_SUBPARTITIONS ? 1 : 0 );
1309
1310
44.1k
    if( cu.multiRefIdx() == 0 )
1311
39.9k
      ipred_idx = m_BinDecoder.decodeBin( Ctx::IntraLumaPlanarFlag( ctx ) );
1312
4.13k
    else
1313
4.13k
      ipred_idx = 1;
1314
1315
49.7k
    if( ipred_idx ) while( ipred_idx < 5 && m_BinDecoder.decodeBinEP() ) ipred_idx++;
1316
1317
44.1k
    cu.intraDir[0] = mpm_pred[ipred_idx];
1318
1319
44.1k
    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
44.1k
  }
1321
11.2k
  else
1322
11.2k
  {
1323
11.2k
    unsigned ipred_mode = 0;
1324
1325
11.2k
    xReadTruncBinCode(ipred_mode, NUM_LUMA_MODE - NUM_MOST_PROBABLE_MODES);
1326
1327
    //postponed sorting of MPMs (only in remaining branch)
1328
11.2k
    std::sort( mpm_pred, mpm_pred + NUM_MOST_PROBABLE_MODES );
1329
1330
78.9k
    for( uint32_t i = 0; i < NUM_MOST_PROBABLE_MODES; i++ )
1331
67.6k
    {
1332
67.6k
      ipred_mode += ( ipred_mode >= mpm_pred[i] );
1333
67.6k
    }
1334
1335
11.2k
    cu.intraDir[0] = ipred_mode;
1336
1337
11.2k
    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
11.2k
  }
1339
55.4k
}
1340
1341
void CABACReader::intra_chroma_pred_mode( CodingUnit& cu )
1342
70.0k
{
1343
70.0k
  if( cu.bdpcmModeChroma() )
1344
1.39k
  {
1345
1.39k
    cu.intraDir[1] = cu.bdpcmModeChroma() == 2 ? VER_IDX : HOR_IDX;
1346
1.39k
    return;
1347
1.39k
  }
1348
1349
68.6k
  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
68.6k
  if( cu.sps->getUseLMChroma() && CU::checkCCLMAllowed( cu ) )
1357
58.6k
  {
1358
58.6k
    bool isLMCMode = m_BinDecoder.decodeBin( Ctx::CclmModeFlag( 0 ) );
1359
58.6k
    if( isLMCMode )
1360
31.5k
    {
1361
31.5k
      intra_chroma_lmc_mode( cu );
1362
31.5k
      return;
1363
31.5k
    }
1364
58.6k
  }
1365
1366
37.0k
  if( m_BinDecoder.decodeBin( Ctx::IPredMode[1]( 0 ) ) == 0 )
1367
22.6k
  {
1368
22.6k
    cu.intraDir[1] = DM_CHROMA_IDX;
1369
22.6k
    return;
1370
22.6k
  }
1371
1372
14.4k
  unsigned candId = m_BinDecoder.decodeBinsEP( 2 );
1373
1374
14.4k
  unsigned chromaCandModes[NUM_CHROMA_MODE];
1375
14.4k
  PU::getIntraChromaCandModes( cu, chromaCandModes );
1376
1377
14.4k
  cu.intraDir[1] = chromaCandModes[candId];
1378
1379
14.4k
  CHECKD( candId >= NUM_CHROMA_MODE, "Chroma prediction mode index out of bounds" );
1380
14.4k
  CHECKD( PU::isLMCMode( chromaCandModes[candId] ), "The intra dir cannot be LM_CHROMA for this path" );
1381
14.4k
  CHECKD( chromaCandModes[candId] == DM_CHROMA_IDX, "The intra dir cannot be DM_CHROMA for this path" );
1382
14.4k
}
1383
1384
bool CABACReader::intra_chroma_lmc_mode( CodingUnit& cu )
1385
31.5k
{
1386
31.5k
  int lmModeList[10];
1387
31.5k
  PU::getLMSymbolList( cu, lmModeList );
1388
1389
31.5k
  int symbol = m_BinDecoder.decodeBin( Ctx::CclmModeIdx( 0 ) );
1390
1391
31.5k
  if( symbol == 0 )
1392
21.2k
  {
1393
21.2k
    cu.intraDir[1] = lmModeList[symbol];
1394
21.2k
    CHECKD( cu.intraDir[1] != LM_CHROMA_IDX, "should be LM_CHROMA" );
1395
21.2k
  }
1396
10.3k
  else
1397
10.3k
  {
1398
10.3k
    symbol += m_BinDecoder.decodeBinEP();
1399
10.3k
    cu.intraDir[1] = lmModeList[symbol];
1400
10.3k
  }
1401
31.5k
  return true; //it will only enter this function for LMC modes, so always return true ;
1402
31.5k
}
1403
1404
void CABACReader::cu_residual( CodingUnit& cu, Partitioner &partitioner, CUCtx& cuCtx )
1405
177k
{
1406
177k
  if( !CU::isIntra( cu ) )
1407
31.9k
  {
1408
31.9k
    if( !cu.mergeFlag() )
1409
19.9k
    {
1410
19.9k
      rqt_root_cbf( cu );
1411
19.9k
    }
1412
11.9k
    else
1413
11.9k
    {
1414
11.9k
      cu.setRootCbf( true );
1415
11.9k
    }
1416
31.9k
    if( cu.rootCbf() )
1417
23.6k
    {
1418
23.6k
      sbt_mode( cu );
1419
23.6k
    }
1420
31.9k
    if( !cu.rootCbf() )
1421
8.22k
    {
1422
      //cu.setColorTransform( false );
1423
8.22k
      cu.cs->addEmptyTUs( partitioner, cu );
1424
8.22k
      return;
1425
8.22k
    }
1426
31.9k
  }
1427
145k
  else
1428
145k
  {
1429
145k
    cu.setRootCbf( true );
1430
145k
  }
1431
1432
169k
  if( CU::isInter( cu ) || CU::isIBC( cu ) )
1433
23.6k
  {
1434
23.6k
    adaptive_color_transform( cu );
1435
23.6k
  }
1436
1437
169k
  cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_LUMA]   = false;
1438
169k
  cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_CHROMA] = false;
1439
169k
  cuCtx.lfnstLastScanPos = false;
1440
169k
  cuCtx.violatesMtsCoeffConstraint                    = false;
1441
169k
  cuCtx.mtsLastScanPos                                = false;
1442
1443
169k
  ChromaCbfs chromaCbfs;
1444
169k
  transform_tree( *cu.cs, cu, partitioner, cuCtx );
1445
1446
169k
  residual_lfnst_mode( cu, cuCtx );
1447
169k
  mts_idx            ( cu, cuCtx );
1448
1449
169k
  bool rootCbf = false;
1450
1451
169k
  for( const auto& blk : cu.blocks )
1452
507k
  {
1453
507k
    if( blk.valid() ) rootCbf |= cu.planeCbf( blk.compID() );
1454
507k
  }
1455
1456
169k
  cu.setRootCbf( rootCbf );
1457
169k
}
1458
1459
void CABACReader::rqt_root_cbf( CodingUnit& cu )
1460
19.9k
{
1461
19.9k
  cu.setRootCbf( !!m_BinDecoder.decodeBin( Ctx::QtRootCbf() ) );
1462
1463
19.9k
  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
19.9k
}
1465
1466
void CABACReader::adaptive_color_transform( CodingUnit& cu )
1467
169k
{
1468
169k
  if( !cu.sps->getUseColorTrans() || CU::isSepTree( cu ) )
1469
169k
  {
1470
169k
    return;
1471
169k
  }
1472
1473
0
  cu.setColorTransform( m_BinDecoder.decodeBin( Ctx::ACTFlag() ) );
1474
0
}
1475
1476
void CABACReader::sbt_mode( CodingUnit& cu )
1477
23.6k
{
1478
23.6k
  const uint8_t sbtAllowed = CU::checkAllowedSbt( cu );
1479
1480
23.6k
  if( !sbtAllowed )
1481
23.6k
  {
1482
23.6k
    return;
1483
23.6k
  }
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
182k
{
1533
182k
  const Position rbPos = recalcPosition( cu.chromaFormat, cu.chType(), CHANNEL_TYPE_LUMA, cu.blocks[cu.chType()].bottomRight().offset( 1, 1 ) );
1534
1535
182k
  if( ( ( rbPos.x & cu.cs->pcv->maxCUWidthMask ) == 0 || rbPos.x == cu.pps->getPicWidthInLumaSamples() )
1536
37.1k
  && ( ( rbPos.y & cu.cs->pcv->maxCUHeightMask ) == 0 || rbPos.y == cu.pps->getPicHeightInLumaSamples() )
1537
9.62k
    && ( !CU::isSepTree( cu ) || cu.chromaFormat == CHROMA_400 || isChroma( cu.chType() ) )
1538
182k
      )
1539
4.78k
  {
1540
4.78k
    cuCtx.isDQPCoded = ( cu.pps->getUseDQP() && !cuCtx.isDQPCoded );
1541
1542
4.78k
    return false;
1543
4.78k
  }
1544
1545
177k
  return false;
1546
182k
}
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
36.8k
{
1570
36.8k
  if( cu.skip() )
1571
4.86k
  {
1572
4.86k
    cu.setMergeFlag( true );
1573
4.86k
  }
1574
31.9k
  else
1575
31.9k
  {
1576
31.9k
    general_merge_flag( cu );
1577
31.9k
  }
1578
1579
36.8k
  if( cu.mergeFlag() )
1580
16.7k
  {
1581
16.7k
    merge_data( cu );
1582
16.7k
  }
1583
20.0k
  else if( CU::isIBC( cu ) )
1584
20.0k
  {
1585
20.0k
    cu.setInterDir  ( 1 );
1586
    //cu.setAffineFlag( false );
1587
20.0k
    cu.refIdx[REF_PIC_LIST_0] = MAX_NUM_REF;
1588
20.0k
    mvd_coding( cu.mv[REF_PIC_LIST_0][0] );
1589
1590
20.0k
    if( cu.sps->getMaxNumIBCMergeCand() == 1 )
1591
0
    {
1592
0
      cu.mvpIdx[REF_PIC_LIST_0] = 0;
1593
0
    }
1594
20.0k
    else
1595
20.0k
    {
1596
20.0k
      mvp_flag    ( cu, REF_PIC_LIST_0 );
1597
20.0k
    }
1598
20.0k
  }
1599
1
  else
1600
1
  {
1601
1
    inter_pred_idc( cu );
1602
1
    affine_flag   ( cu );
1603
1
    smvd_mode     ( cu );
1604
1605
1
    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
1
    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
1
  }
1651
1652
36.8k
  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
36.8k
}
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
31.9k
{
1718
31.9k
  cu.setMergeFlag( m_BinDecoder.decodeBin( Ctx::MergeFlag() ) );
1719
1720
31.9k
  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
31.9k
}
1730
1731
1732
void CABACReader::merge_data( CodingUnit& cu )
1733
16.7k
{
1734
16.7k
  if( CU::isIBC( cu ) )
1735
16.7k
  {
1736
16.7k
    merge_idx( cu );
1737
16.7k
    return;
1738
16.7k
  }
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
16.7k
{
1810
16.7k
  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
16.7k
  else
1847
16.7k
  {
1848
16.7k
    int numCandminus1;
1849
16.7k
    int ctxIdx = Ctx::MergeIdx();
1850
1851
16.7k
    if( cu.predMode() == MODE_IBC )
1852
16.7k
    {
1853
16.7k
      numCandminus1 = int( cu.sps->getMaxNumIBCMergeCand() ) - 1;
1854
16.7k
    }
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
16.7k
    int mergeIdx = 0;
1866
16.7k
    if( numCandminus1 > 0 && m_BinDecoder.decodeBin( ctxIdx ) )
1867
3.69k
    {
1868
7.13k
      for( mergeIdx++; mergeIdx < numCandminus1 && m_BinDecoder.decodeBinEP(); mergeIdx++ );
1869
3.69k
    }
1870
1871
16.7k
    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
16.7k
  }
1880
16.7k
}
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
19.9k
{
1990
19.9k
  unsigned mvp_idx = m_BinDecoder.decodeBin( Ctx::MVPIdx() );
1991
19.9k
  DTRACE( g_trace_ctx, D_SYNTAX, "mvp_flag() value=%d pos=(%d,%d)\n", mvp_idx, cu.lumaPos().x, cu.lumaPos().y );
1992
19.9k
  cu.mvpIdx [eRefList] = mvp_idx;
1993
19.9k
  DTRACE( g_trace_ctx, D_SYNTAX, "mvpIdx(refList:%d)=%d\n", eRefList, mvp_idx );
1994
19.9k
}
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
196k
{
2014
196k
  const UnitArea& area    = partitioner.currArea();
2015
                          
2016
  // split_transform_flag
2017
196k
  bool            split   = area.Y().width > partitioner.maxTrSize || area.Y().height > partitioner.maxTrSize;
2018
196k
  const PartSplit ispType = CU::getISPType( cu, getFirstComponentOfChannel( partitioner.chType ) );
2019
2020
196k
  split |= ( cu.sbtInfo() || ispType != TU_NO_ISP ) && partitioner.currTrDepth == 0;
2021
2022
196k
  if( split )
2023
6.97k
  {
2024
6.97k
    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
6.97k
    else if( ispType != TU_NO_ISP )
2035
6.97k
    {
2036
6.97k
      partitioner.splitCurrArea( ispType, cs );
2037
6.97k
    }
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
6.97k
    do
2046
27.2k
    {
2047
27.2k
      transform_tree( cs, cu, partitioner, cuCtx );
2048
27.2k
    } while( partitioner.nextPart( cs ) );
2049
2050
6.97k
    partitioner.exitCurrSplit( cs );
2051
6.97k
  }
2052
189k
  else
2053
189k
  {
2054
189k
    TransformUnit &tu = cs.addTU( getArea( *m_slice, area, partitioner.chType, partitioner.treeType ), partitioner.chType, cu );
2055
189k
    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
189k
    transform_unit( tu, cuCtx, partitioner );
2058
189k
  }
2059
196k
}
2060
2061
bool CABACReader::cbf_comp( CodingUnit& cu, const CompArea& area, unsigned depth, const bool prevCbf, const bool useISP )
2062
234k
{
2063
234k
  const CtxSet&   ctxSet  = Ctx::QtCbf[ area.compID()];
2064
2065
234k
  unsigned  cbf = 0;
2066
234k
  if( ( area.compID() == COMPONENT_Y && cu.bdpcmMode() ) || ( area.compID() != COMPONENT_Y && cu.bdpcmModeChroma() ) )
2067
12.3k
  {
2068
12.3k
    if( area.compID() == COMPONENT_Cr )
2069
1.39k
      cbf = m_BinDecoder.decodeBin( ctxSet( 2 ) );
2070
10.9k
    else
2071
10.9k
      cbf = m_BinDecoder.decodeBin( ctxSet( 1 ) );
2072
12.3k
    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
12.3k
  }
2074
222k
  else
2075
222k
  {
2076
222k
    const unsigned  ctxId = DeriveCtx::CtxQtCbf( area.compID(), prevCbf, useISP && isLuma( area.compID() ) );
2077
222k
    cbf = m_BinDecoder.decodeBin( ctxSet( ctxId ) );
2078
2079
222k
    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
222k
  }
2081
2082
234k
  return cbf;
2083
234k
}
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
20.0k
{
2097
  // abs_mvd_greater0_flag[ 0 | 1 ]
2098
20.0k
  int horAbs = (int)m_BinDecoder.decodeBin(Ctx::Mvd());
2099
20.0k
  int verAbs = (int)m_BinDecoder.decodeBin(Ctx::Mvd());
2100
2101
  // abs_mvd_greater1_flag[ 0 | 1 ]
2102
20.0k
  if (horAbs)
2103
11.8k
  {
2104
11.8k
    horAbs += (int)m_BinDecoder.decodeBin(Ctx::Mvd(1));
2105
11.8k
  }
2106
20.0k
  if (verAbs)
2107
12.9k
  {
2108
12.9k
    verAbs += (int)m_BinDecoder.decodeBin(Ctx::Mvd(1));
2109
12.9k
  }
2110
2111
  // abs_mvd_minus2[ 0 | 1 ] and mvd_sign_flag[ 0 | 1 ]
2112
20.0k
  if (horAbs)
2113
11.8k
  {
2114
11.8k
    if (horAbs > 1)
2115
9.76k
    {
2116
9.76k
      horAbs += m_BinDecoder.decodeRemAbsEP(1, 0, MV_BITS - 1);
2117
9.76k
    }
2118
11.8k
    if (m_BinDecoder.decodeBinEP())
2119
6.56k
    {
2120
6.56k
      horAbs = -horAbs;
2121
6.56k
    }
2122
11.8k
  }
2123
20.0k
  if (verAbs)
2124
12.9k
  {
2125
12.9k
    if (verAbs > 1)
2126
10.9k
    {
2127
10.9k
      verAbs += m_BinDecoder.decodeRemAbsEP(1, 0, MV_BITS - 1);
2128
10.9k
    }
2129
12.9k
    if (m_BinDecoder.decodeBinEP())
2130
6.94k
    {
2131
6.94k
      verAbs = -verAbs;
2132
6.94k
    }
2133
12.9k
  }
2134
20.0k
  rMvd = Mv(horAbs, verAbs);
2135
2136
20.0k
  CHECKD( !( ( horAbs >= MVD_MIN ) && ( horAbs <= MVD_MAX ) ) || !( ( verAbs >= MVD_MIN ) && ( verAbs <= MVD_MAX ) ), "Illegal MVD value" );
2137
20.0k
}
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
189k
{
2150
189k
  const UnitArea& area    = partitioner.currArea();
2151
189k
  const unsigned  trDepth = partitioner.currTrDepth;
2152
2153
189k
  CodingUnit&      cu     = *tu.cu;
2154
189k
  ChromaCbfs       chromaCbfs;
2155
2156
189k
  const bool chromaCbfISP = isChromaEnabled( area.chromaFormat ) && area.blocks[COMPONENT_Cb].valid() && cu.ispMode();
2157
189k
  const bool tuNoResidual = TU::checkTuNoResidual( tu, partitioner.currPartIdx() );
2158
2159
  // cbf_cb & cbf_cr
2160
189k
  if( area.chromaFormat != CHROMA_400 && area.blocks[COMPONENT_Cb].valid() && ( !CU::isSepTree( cu ) || partitioner.chType == CHANNEL_TYPE_CHROMA ) && ( !cu.ispMode() || chromaCbfISP ) )
2161
70.0k
  {
2162
70.0k
    const int cbfDepth = chromaCbfISP ? trDepth - 1 : trDepth;
2163
2164
70.0k
    if( !( cu.sbtInfo() && tuNoResidual ) )
2165
70.0k
    {
2166
70.0k
      chromaCbfs.Cb = cbf_comp( cu, area.blocks[COMPONENT_Cb], cbfDepth );
2167
70.0k
      chromaCbfs.Cr = cbf_comp( cu, area.blocks[COMPONENT_Cr], cbfDepth, chromaCbfs.Cb );
2168
70.0k
    }
2169
70.0k
  }
2170
2171
189k
  if( !isChroma( partitioner.chType ) )
2172
119k
  {
2173
119k
    if( !CU::isIntra( cu ) && trDepth == 0 && !chromaCbfs.sigChroma( area.chromaFormat ) )
2174
23.6k
    {
2175
23.6k
      TU::setCbf( tu, COMPONENT_Y, true );
2176
23.6k
    }
2177
96.1k
    else if( cu.sbtInfo() && tuNoResidual )
2178
0
    {
2179
0
      TU::setCbf( tu, COMPONENT_Y, false );
2180
0
    }
2181
96.1k
    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
96.1k
    else
2187
96.1k
    {
2188
96.1k
      bool cbfY = true;
2189
2190
96.1k
      if( cu.ispMode() )
2191
27.2k
      {
2192
27.2k
        bool lumaCbfIsInferredACT = ( cu.colorTransform() && cu.predMode() == MODE_INTRA && trDepth == 0 && !chromaCbfs.sigChroma( area.chromaFormat ) );
2193
27.2k
        bool lastCbfIsInferred    = lumaCbfIsInferredACT; // ISP and ACT are mutually exclusive
2194
27.2k
        bool rootCbfSoFar         = false;
2195
2196
27.2k
        int nTus = cu.ispMode() == HOR_INTRA_SUBPARTITIONS ? cu.lheight() >> getLog2( tu.lheight() ) : cu.lwidth() >> getLog2( tu.lwidth() );
2197
27.2k
        int idx  = partitioner.currPartIdx();
2198
27.2k
        if( idx == nTus - 1 )
2199
6.93k
        {
2200
6.93k
          for( const TransformUnit &currTu : cTUTraverser( &cu.firstTU, cu.lastTU ) )
2201
20.1k
          {
2202
20.1k
            rootCbfSoFar |= TU::getCbf( currTu, COMPONENT_Y );
2203
20.1k
          }
2204
2205
6.93k
          if( !rootCbfSoFar )
2206
1.54k
          {
2207
1.54k
            lastCbfIsInferred = true;
2208
1.54k
          }
2209
6.93k
        }
2210
2211
27.2k
        if( !lastCbfIsInferred )
2212
25.6k
        {
2213
25.6k
          cbfY = cbf_comp( cu, tu.Y(), trDepth, TU::getPrevTUCbf( tu, COMPONENT_Y ), cu.ispMode() );
2214
25.6k
        }
2215
1.54k
        else
2216
1.54k
        {
2217
1.54k
          cbfY = true;
2218
1.54k
        }
2219
27.2k
      }
2220
68.9k
      else
2221
68.9k
      {
2222
68.9k
        cbfY = cbf_comp( cu, tu.Y(), trDepth, false, NOT_INTRA_SUBPARTITIONS );
2223
68.9k
      }
2224
96.1k
      TU::setCbf( tu, COMPONENT_Y, cbfY ? 1 : 0 );
2225
96.1k
    }
2226
119k
  }
2227
2228
189k
  if( area.chromaFormat != CHROMA_400 && ( !cu.ispMode() || chromaCbfISP ) )
2229
162k
  {
2230
162k
    TU::setCbf( tu, COMPONENT_Cb, chromaCbfs.Cb );
2231
162k
    TU::setCbf( tu, COMPONENT_Cr, chromaCbfs.Cr );
2232
162k
  }
2233
2234
189k
  cu.setPlaneCbf( COMPONENT_Y , cu.planeCbf( COMPONENT_Y  ) || TU::getCbf( tu, COMPONENT_Y )  );
2235
189k
  cu.setPlaneCbf( COMPONENT_Cb, cu.planeCbf( COMPONENT_Cb ) || TU::getCbf( tu, COMPONENT_Cb ) );
2236
189k
  cu.setPlaneCbf( COMPONENT_Cr, cu.planeCbf( COMPONENT_Cr ) || TU::getCbf( tu, COMPONENT_Cr ) );
2237
2238
189k
  const bool lumaOnly   = ( cu.chromaFormat == CHROMA_400 || !tu.blocks[COMPONENT_Cb].valid() );
2239
189k
  const bool cbfLuma    = TU::getCbf( tu, COMPONENT_Y );
2240
189k
  const bool cbfChroma  = ( lumaOnly ? false : ( chromaCbfs.Cb || chromaCbfs.Cr ) );
2241
2242
189k
  if( cu.lwidth() > 64 || cu.lheight() > 64 || cbfLuma || cbfChroma )
2243
130k
  {
2244
130k
    if( cu.pps->getUseDQP() && !cuCtx.isDQPCoded )
2245
55.3k
    {
2246
55.3k
      if( !CU::isSepTree( cu ) || isLuma( tu.chType() ) )
2247
18.3k
      {
2248
18.3k
        cu_qp_delta(cu, cuCtx.qp, cu.qp);
2249
18.3k
        cuCtx.qp = cu.qp;
2250
18.3k
        cuCtx.isDQPCoded = true;
2251
18.3k
      }
2252
55.3k
    }
2253
2254
130k
    if( !CU::isSepTree( cu ) || isChroma( tu.chType() ) )   // !DUAL_TREE_LUMA
2255
36.9k
    {
2256
36.9k
      const SizeType channelWidth  = !CU::isSepTree( cu ) ? cu.lwidth()  : cu.chromaSize().width;
2257
36.9k
      const SizeType channelHeight = !CU::isSepTree( cu ) ? cu.lheight() : cu.chromaSize().height;
2258
2259
36.9k
      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
36.9k
    }
2265
2266
130k
    if( !lumaOnly )
2267
36.9k
    {
2268
36.9k
      joint_cb_cr( tu, ( TU::getCbf( tu, COMPONENT_Cb ) ? 2 : 0 ) + ( TU::getCbf( tu, COMPONENT_Cr ) ? 1 : 0 ) );
2269
2270
36.9k
      if( tu.jointCbCr )
2271
14.2k
      {
2272
14.2k
        cu.setPlaneCbf( COMPONENT_Cb, true );
2273
14.2k
        cu.setPlaneCbf( COMPONENT_Cr, true );
2274
14.2k
      }
2275
36.9k
    }
2276
2277
130k
    if( cbfLuma )
2278
93.1k
    {
2279
93.1k
      residual_coding( tu, COMPONENT_Y, cuCtx );
2280
93.1k
    }
2281
130k
    if( !lumaOnly )
2282
36.9k
    {
2283
110k
      for( ComponentID compID = COMPONENT_Cb; compID <= COMPONENT_Cr; compID = ComponentID( compID + 1 ) )
2284
73.8k
      {
2285
73.8k
        if( TU::getCbf( tu, compID ) )
2286
56.4k
        {
2287
56.4k
          residual_coding( tu, compID, cuCtx );
2288
56.4k
        }
2289
73.8k
      }
2290
36.9k
    }
2291
130k
  }
2292
189k
}
2293
2294
void CABACReader::cu_qp_delta( CodingUnit& cu, int predQP, int8_t& qp )
2295
18.3k
{
2296
18.3k
  CHECK( predQP == std::numeric_limits<int>::max(), "Invalid predicted QP" );
2297
18.3k
  int qpY = predQP;
2298
18.3k
  int DQp = unary_max_symbol( Ctx::DeltaQP(), Ctx::DeltaQP(1), CU_DQP_TU_CMAX );
2299
18.3k
  if( DQp >= CU_DQP_TU_CMAX )
2300
278
  {
2301
278
    DQp += exp_golomb_eqprob( CU_DQP_EG_k  );
2302
278
  }
2303
18.3k
  if( DQp > 0 )
2304
5.43k
  {
2305
5.43k
    if( m_BinDecoder.decodeBinEP( ) )
2306
2.80k
    {
2307
2.80k
      DQp = -DQp;
2308
2.80k
    }
2309
5.43k
    const int qpBdOffsetY = cu.sps->getQpBDOffset();
2310
5.43k
    CHECK_READ_RANGE( DQp, -( 32 + qpBdOffsetY / 2 ), ( 31 + qpBdOffsetY / 2 ), "CuQpDeltaVal" );
2311
5.42k
    qpY = ( (predQP + DQp + (MAX_QP + 1) + 2 * qpBdOffsetY) % ((MAX_QP + 1) + qpBdOffsetY)) - qpBdOffsetY;
2312
5.42k
  }
2313
18.3k
  qp = (int8_t)qpY;
2314
2315
18.3k
  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 );
2316
18.3k
}
2317
2318
2319
void CABACReader::cu_chroma_qp_offset( CodingUnit& cu )
2320
0
{
2321
  // cu_chroma_qp_offset_flag
2322
0
  int       length  = cu.pps->getChromaQpOffsetListLen();
2323
0
  unsigned  qpAdj   = m_BinDecoder.decodeBin( Ctx::ChromaQpAdjFlag() );
2324
0
  if( qpAdj && length > 1 )
2325
0
  {
2326
    // cu_chroma_qp_offset_idx
2327
0
    qpAdj += unary_max_symbol( Ctx::ChromaQpAdjIdc(), Ctx::ChromaQpAdjIdc(), length-1 );
2328
0
  }
2329
  /* NB, symbol = 0 if outer flag is not set,
2330
   *              1 if outer flag is set and there is no inner flag
2331
   *              1+ otherwise */
2332
0
  cu.chromaQpAdj = cu.cs->chromaQpAdj = qpAdj;
2333
0
}
2334
2335
2336
2337
2338
2339
//================================================================================
2340
//  clause 7.3.8.11
2341
//--------------------------------------------------------------------------------
2342
//    void        residual_coding         ( tu, compID )
2343
//    bool        transform_skip_flag     ( tu, compID )
2344
//    RDPCMMode   explicit_rdpcm_mode     ( tu, compID )
2345
//    int         last_sig_coeff          ( coeffCtx )
2346
//    void        residual_coding_subblock( coeffCtx )
2347
//================================================================================
2348
2349
void CABACReader::joint_cb_cr( TransformUnit& tu, const int cbfMask )
2350
36.9k
{
2351
36.9k
  if( !tu.cu->sps->getJointCbCrEnabledFlag() )
2352
0
  {
2353
0
    return;
2354
0
  }
2355
2356
36.9k
  if( ( CU::isIntra( *tu.cu ) && cbfMask ) || ( cbfMask == 3 ) )
2357
36.9k
  {
2358
36.9k
    tu.jointCbCr = ( m_BinDecoder.decodeBin( Ctx::JointCbCrFlag( cbfMask-1 ) ) ? cbfMask : 0 );
2359
36.9k
  }
2360
36.9k
}
2361
2362
void CABACReader::residual_coding( TransformUnit& tu, ComponentID compID, CUCtx& cuCtx )
2363
149k
{
2364
149k
  PROFILER_SCOPE_AND_STAGE_EXT( 1, g_timeProfiler, P_PARSERESIDUALS, *tu.cu->cs, compID );
2365
149k
  const CodingUnit& cu = *tu.cu;
2366
2367
149k
  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() );
2368
2369
149k
  if( compID == COMPONENT_Cr && tu.jointCbCr == 3 )
2370
5.16k
    return;
2371
2372
  // parse transform skip and explicit rdpcm mode
2373
144k
  ts_flag( tu, compID );
2374
2375
144k
  if( tu.mtsIdx( compID ) == MTS_SKIP && !cu.slice->getTSResidualCodingDisabledFlag() )
2376
8.50k
  {
2377
8.50k
    residual_codingTS( tu, compID );
2378
8.50k
    return;
2379
8.50k
  }
2380
2381
  // determine sign hiding
2382
135k
  bool signHiding = m_slice->getSignDataHidingEnabledFlag();
2383
135k
  CoeffCodingContext  cctx( tu, compID, signHiding, m_tplBuf );
2384
  // parse last coeff position
2385
135k
  cctx.setScanPosLast( last_sig_coeff( cctx, tu, compID ) );
2386
135k
  if (tu.mtsIdx( compID ) != MTS_SKIP && tu.blocks[compID].height >= 4 && tu.blocks[compID].width >= 4 )
2387
129k
  {
2388
129k
    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;
2389
129k
    cuCtx.violatesLfnstConstrained[ toChannelType(compID) ] |= cctx.scanPosLast() > maxLfnstPos;
2390
129k
  }
2391
135k
  if( tu.mtsIdx( compID ) != MTS_SKIP && tu.blocks[compID].height >= 4 && tu.blocks[compID].width >= 4 )
2392
129k
  {
2393
129k
    const int lfnstLastScanPosTh = isLuma( compID ) ? LFNST_LAST_SIG_LUMA : LFNST_LAST_SIG_CHROMA;
2394
129k
    cuCtx.lfnstLastScanPos |= cctx.scanPosLast() >= lfnstLastScanPosTh;
2395
129k
  }
2396
135k
  if( isLuma( compID ) && tu.mtsIdx( compID ) != MTS_SKIP )
2397
86.9k
  {
2398
86.9k
    cuCtx.mtsLastScanPos |= cctx.scanPosLast() >= 1;
2399
86.9k
  }
2400
2401
  // parse subblocks
2402
135k
  const int stateTransTab = ( m_slice->getDepQuantEnabledFlag() ? 32040 : 0 );
2403
135k
  int       state         = 0;
2404
2405
135k
  TCoeffSig *coeff = m_cffTmp;
2406
135k
  ::memset( coeff, 0, cctx.maxNumCoeff() * sizeof( TCoeffSig ) );
2407
2408
135k
  int *sigPos      = m_blkPos;
2409
135k
  int  sigSubSetId = 0;
2410
2411
135k
  int maxX = 0;
2412
135k
  int maxY = 0;
2413
2414
135k
  const bool skipBlkPreCond = compID == COMPONENT_Y && tu.cu->sps->getUseMTS() && tu.cu->sbtInfo() != 0 && tu.blocks[ compID ].height <= 32 && tu.blocks[ compID ].width <= 32;
2415
422k
  for( int subSetId = ( cctx.scanPosLast() >> cctx.log2CGSize() ); subSetId >= 0; subSetId--)
2416
286k
  {
2417
286k
    cctx.initSubblock( subSetId );
2418
2419
286k
    if( skipBlkPreCond )
2420
0
    {
2421
0
      if( ( cctx.height() == 32 && cctx.cgPosY() >= ( 16 >> cctx.log2CGHeight() ) ) || ( cctx.width() == 32 && cctx.cgPosX() >= ( 16 >> cctx.log2CGWidth() ) ) )
2422
0
      {
2423
0
        continue;
2424
0
      }
2425
0
    }
2426
2427
286k
    int numSigCoef = residual_coding_subblock( cctx, coeff, stateTransTab, state, m_signVal[sigSubSetId], sigPos, m_sub1[sigSubSetId] );
2428
2429
286k
    if( numSigCoef > 0 )
2430
214k
    {
2431
214k
      m_numSig[sigSubSetId] = numSigCoef;
2432
214k
      sigSubSetId++;
2433
2434
214k
      maxX = std::max( maxX, cctx.cgPosX() );
2435
214k
      maxY = std::max( maxY, cctx.cgPosY() );
2436
214k
    }
2437
286k
    if( isLuma( compID ) && cctx.isSigGroup() && ( cctx.cgPosY() > 3 || cctx.cgPosX() > 3 ) )
2438
5.06k
    {
2439
5.06k
      cuCtx.violatesMtsCoeffConstraint = true;
2440
5.06k
    }
2441
286k
  }
2442
2443
135k
  if( cctx.bdpcm() )
2444
0
  {
2445
0
    maxX = cctx.width();
2446
0
    maxY = cctx.height();
2447
0
  }
2448
135k
  else
2449
135k
  {
2450
135k
    maxX++;
2451
135k
    maxY++;
2452
135k
    maxX <<= cctx.log2CGWidth();
2453
135k
    maxY <<= cctx.log2CGHeight();
2454
135k
  }
2455
2456
  // if not TU split, otherwise already memset
2457
135k
  PelBuf pb = cu.cs->getRecoBuf( CompArea( compID, tu.blocks[compID].pos(), Size( maxX, maxY ) ) );
2458
135k
  pb.memset( 0 );
2459
2460
135k
  const bool depQuant = tu.cu->slice->getDepQuantEnabledFlag() && ( tu.mtsIdx( compID ) != MTS_SKIP );
2461
135k
  CoeffSigBuf dstCff = pb;
2462
2463
349k
  for( sigPos--, sigSubSetId--; sigSubSetId >= 0; sigSubSetId-- )
2464
213k
  {
2465
213k
    unsigned numNonZero  = m_numSig [sigSubSetId];
2466
213k
    unsigned signPattern = m_signVal[sigSubSetId];
2467
213k
    unsigned sub1Pattern = m_sub1   [sigSubSetId];
2468
2469
    //===== set final coefficents =====
2470
1.28M
    for( unsigned k = 0; k < numNonZero; k++, sigPos--, signPattern >>= 1, sub1Pattern >>= 1 )
2471
1.06M
    {
2472
1.06M
      const int blkPos        = *sigPos;
2473
1.06M
      const int posX          = cctx.posX( blkPos );
2474
1.06M
      const int posY          = cctx.posY( blkPos );
2475
2476
1.06M
      int AbsCoeff            = depQuant ? ( coeff[blkPos] << 1 ) - ( ( ( int ) sub1Pattern ) & 1 ) : coeff[blkPos];
2477
1.06M
      dstCff.at( posX, posY ) = ( signPattern & 1u ? -AbsCoeff : AbsCoeff );
2478
1.06M
    }
2479
213k
  }
2480
2481
135k
  if( cctx.scanPosLast() == 0 )
2482
32.7k
  {
2483
32.7k
    tu.maxScanPosX[compID] = 0;
2484
32.7k
    tu.maxScanPosY[compID] = 0;
2485
32.7k
  }
2486
103k
  else
2487
103k
  {
2488
103k
    tu.maxScanPosX[compID] = maxX - 1;
2489
103k
    tu.maxScanPosY[compID] = maxY - 1;
2490
103k
  }
2491
135k
}
2492
2493
void CABACReader::ts_flag( TransformUnit& tu, ComponentID compID )
2494
144k
{
2495
144k
  int tsFlag = ( ( tu.cu->bdpcmMode() && isLuma( compID ) ) || ( tu.cu->bdpcmModeChroma() && isChroma( compID ) ) ) ? 1 : tu.mtsIdx( compID ) == MTS_SKIP ? 1 : 0;
2496
144k
  int ctxIdx = isLuma(compID) ? 4 : 5;
2497
2498
144k
  if( TU::isTSAllowed ( tu, compID ) )
2499
62.1k
  {
2500
62.1k
    tsFlag = m_BinDecoder.decodeBin( Ctx::MTSIndex( ctxIdx ) );
2501
62.1k
  }
2502
  
2503
144k
  tu.setMtsIdx( compID, tsFlag ? MTS_SKIP : MTS_DCT2_DCT2 );
2504
  
2505
144k
  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);
2506
144k
}
2507
2508
void CABACReader::mts_idx( CodingUnit& cu, CUCtx& cuCtx )
2509
169k
{
2510
169k
  TransformUnit &tu = cu.firstTU;
2511
169k
  int        mtsIdx = tu.mtsIdx( COMPONENT_Y ); // Transform skip flag has already been decoded
2512
  
2513
169k
  if( CU::isMTSAllowed( cu, COMPONENT_Y ) && !cuCtx.violatesMtsCoeffConstraint &&
2514
0
      cuCtx.mtsLastScanPos && cu.lfnstIdx() == 0 && mtsIdx != MTS_SKIP )
2515
0
  {
2516
0
    int ctxIdx = 0;
2517
0
    int symbol = m_BinDecoder.decodeBin( Ctx::MTSIndex( ctxIdx ) );
2518
    
2519
0
    if( symbol )
2520
0
    {
2521
0
      ctxIdx = 1;
2522
0
      mtsIdx = MTS_DST7_DST7; // mtsIdx = 2 -- 4
2523
0
      for( int i = 0; i < 3; i++, ctxIdx++ )
2524
0
      {
2525
0
        symbol  = m_BinDecoder.decodeBin( Ctx::MTSIndex( ctxIdx ) );
2526
0
        mtsIdx += symbol;
2527
        
2528
0
        if( !symbol )
2529
0
        {
2530
0
          break;
2531
0
        }
2532
0
      }
2533
0
    }
2534
0
  }
2535
  
2536
169k
  tu.setMtsIdx( COMPONENT_Y, mtsIdx );
2537
  
2538
169k
  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);
2539
169k
}
2540
2541
void CABACReader::isp_mode( CodingUnit& cu )
2542
55.4k
{
2543
55.4k
  if( cu.multiRefIdx() || !cu.sps->getUseISP() || cu.bdpcmMode() || cu.colorTransform() )
2544
4.16k
  {
2545
    //cu.setIspMode( NOT_INTRA_SUBPARTITIONS );
2546
4.16k
    return;
2547
4.16k
  }
2548
2549
51.2k
  const ISPType allowedSplits = CU::canUseISPSplit( cu, COMPONENT_Y );
2550
2551
51.2k
  if( allowedSplits == NOT_INTRA_SUBPARTITIONS )
2552
1.16k
  {
2553
    //cu.setIspMode( NOT_INTRA_SUBPARTITIONS );
2554
1.16k
    return;
2555
1.16k
  }
2556
2557
  //cu.setIspMode( NOT_INTRA_SUBPARTITIONS );
2558
50.0k
  int symbol = m_BinDecoder.decodeBin( Ctx::ISPMode( 0 ) );
2559
2560
50.0k
  if( symbol )
2561
6.97k
  {
2562
6.97k
    if( allowedSplits == HOR_INTRA_SUBPARTITIONS )
2563
0
    {
2564
0
      cu.setIspMode( HOR_INTRA_SUBPARTITIONS );
2565
0
    }
2566
6.97k
    else if( allowedSplits == VER_INTRA_SUBPARTITIONS )
2567
0
    {
2568
0
      cu.setIspMode( VER_INTRA_SUBPARTITIONS );
2569
0
    }
2570
6.97k
    else
2571
6.97k
    {
2572
6.97k
      cu.setIspMode( 1 + m_BinDecoder.decodeBin( Ctx::ISPMode( 1 ) ) );
2573
6.97k
    }
2574
6.97k
  }
2575
50.0k
  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() );
2576
50.0k
}
2577
2578
void CABACReader::residual_lfnst_mode( CodingUnit& cu,  CUCtx& cuCtx  )
2579
169k
{
2580
169k
  if( !cu.sps->getUseLFNST() || !CU::isIntra( cu ) )
2581
23.6k
  {
2582
    //cu.setLfnstIdx( 0 );
2583
23.6k
    return;
2584
23.6k
  }
2585
2586
145k
  const int chIdx  = CU::isSepTree( cu ) && cu.chType() == CHANNEL_TYPE_CHROMA ? 1 : 0;
2587
145k
  if( ( cu.ispMode() && !CU::canUseLfnstWithISP( cu, cu.chType() ) ) ||
2588
143k
      ( cu.mipFlag() && !    allowLfnstWithMip ( cu.lumaSize() ) ) ||
2589
138k
      ( cu.chType() == CHANNEL_TYPE_CHROMA && std::min( cu.blocks[1].width, cu.blocks[1].height ) < 4 )
2590
145k
    )
2591
10.0k
  {
2592
10.0k
    return;
2593
10.0k
  }
2594
2595
135k
  const Size lSize = cu.blocks[chIdx].lumaSize( cu.chromaFormat );
2596
135k
  if( lSize.width > cu.sps->getMaxTbSize() || lSize.height > cu.sps->getMaxTbSize() ) return;
2597
2598
135k
  {
2599
135k
    const bool lumaFlag   = CU::isSepTree( cu ) ?   isLuma( cu.chType() ) : true;
2600
135k
    const bool chromaFlag = CU::isSepTree( cu ) ? isChroma( cu.chType() ) : true;
2601
135k
    const bool nonZeroCoeffNonTsCorner8x8
2602
135k
                          = ( lumaFlag && cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_LUMA] ) || (chromaFlag && cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_CHROMA] );
2603
135k
    bool isTrSkip = false;
2604
135k
    for( auto &currTU : cTUTraverser( &cu.firstTU, cu.lastTU->next ) )
2605
149k
    {
2606
149k
      const uint32_t numValidComp = getNumberValidComponents( cu.chromaFormat );
2607
580k
      for( uint32_t compID = COMPONENT_Y; compID < numValidComp; compID++ )
2608
437k
      {
2609
437k
        if( currTU.blocks[compID].valid() && TU::getCbf( currTU, ( ComponentID ) compID ) && currTU.mtsIdx( compID ) == MTS_SKIP )
2610
7.09k
        {
2611
7.09k
          isTrSkip = true;
2612
7.09k
          break;
2613
7.09k
        }
2614
437k
      }
2615
149k
    }
2616
135k
    if( nonZeroCoeffNonTsCorner8x8 || ( !cuCtx.lfnstLastScanPos && !cu.ispMode() ) || isTrSkip )
2617
94.2k
    {
2618
      //cu.setLfnstIdx( 0 );
2619
94.2k
      return;
2620
94.2k
    }
2621
135k
  }
2622
2623
2624
41.2k
  unsigned cctx = 0;
2625
2626
41.2k
  if( CU::isSepTree( cu ) ) cctx++;
2627
2628
41.2k
  unsigned idxLFNST = m_BinDecoder.decodeBin( Ctx::LFNSTIdx( cctx ) );
2629
2630
41.2k
  if( idxLFNST )
2631
29.1k
  {
2632
29.1k
    idxLFNST += m_BinDecoder.decodeBin( Ctx::LFNSTIdx(2) );
2633
29.1k
  }
2634
2635
41.2k
  cu.setLfnstIdx( idxLFNST );
2636
2637
41.2k
  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() );
2638
41.2k
}
2639
2640
int CABACReader::last_sig_coeff( CoeffCodingContext& cctx, TransformUnit& tu, ComponentID compID )
2641
135k
{
2642
135k
  unsigned PosLastX = 0, PosLastY = 0;
2643
135k
  unsigned maxLastPosX = cctx.maxLastPosX();
2644
135k
  unsigned maxLastPosY = cctx.maxLastPosY();
2645
2646
135k
  if( isLuma( compID ) && tu.cu->sps->getUseMTS() && tu.cu->sbtInfo() != 0 && tu.blocks[ compID ].width <= 32 && tu.blocks[ compID ].height <= 32 )
2647
0
  {
2648
0
    maxLastPosX = ( tu.blocks[ compID ].width  == 32 ) ? g_uiGroupIdx[ 15 ] : maxLastPosX;
2649
0
    maxLastPosY = ( tu.blocks[ compID ].height == 32 ) ? g_uiGroupIdx[ 15 ] : maxLastPosY;
2650
0
  }
2651
2652
316k
  for( ; PosLastX < maxLastPosX; PosLastX++ )
2653
312k
  {
2654
312k
    if( !m_BinDecoder.decodeBin( cctx.lastXCtxId( PosLastX ) ) )
2655
131k
    {
2656
131k
      break;
2657
131k
    }
2658
312k
  }
2659
312k
  for( ; PosLastY < maxLastPosY; PosLastY++ )
2660
305k
  {
2661
305k
    if( !m_BinDecoder.decodeBin( cctx.lastYCtxId( PosLastY ) ) )
2662
128k
    {
2663
128k
      break;
2664
128k
    }
2665
305k
  }
2666
135k
  if( PosLastX > 3 )
2667
19.8k
  {
2668
19.8k
    uint32_t uiTemp  = 0;
2669
19.8k
    uint32_t uiCount = ( PosLastX - 2 ) >> 1;
2670
46.1k
    for ( int i = uiCount - 1; i >= 0; i-- )
2671
26.2k
    {
2672
26.2k
      uiTemp += m_BinDecoder.decodeBinEP( ) << i;
2673
26.2k
    }
2674
19.8k
    PosLastX = g_uiMinInGroup[ PosLastX ] + uiTemp;
2675
19.8k
  }
2676
135k
  if( PosLastY > 3 )
2677
16.8k
  {
2678
16.8k
    uint32_t uiTemp  = 0;
2679
16.8k
    uint32_t uiCount = ( PosLastY - 2 ) >> 1;
2680
41.9k
    for ( int i = uiCount - 1; i >= 0; i-- )
2681
25.0k
    {
2682
25.0k
      uiTemp += m_BinDecoder.decodeBinEP( ) << i;
2683
25.0k
    }
2684
16.8k
    PosLastY = g_uiMinInGroup[ PosLastY ] + uiTemp;
2685
16.8k
  }
2686
2687
135k
  int blkPos;
2688
135k
  {
2689
135k
    blkPos = PosLastX + ( PosLastY * cctx.width() );
2690
135k
  }
2691
2692
3.05M
  for( int scanPos = 0; scanPos < cctx.maxNumCoeff() - 1; scanPos++ )
2693
3.05M
  {
2694
3.05M
    if( blkPos == cctx.blockPos( scanPos ) )
2695
135k
    {
2696
135k
      return scanPos;
2697
135k
    }
2698
3.05M
  }
2699
2700
184
  return cctx.maxNumCoeff() - 1;
2701
135k
}
2702
2703
2704
int CABACReader::residual_coding_subblock(CoeffCodingContext& cctx, TCoeffSig* coeff, const int stateTransTable, int& state, unsigned& signPattern, int *&sigPos, unsigned &stateVal)
2705
286k
{
2706
  // NOTE: All coefficients of the subblock must be set to zero before calling this function
2707
2708
  //===== init =====
2709
286k
  const int   minSubPos = cctx.minSubPos();
2710
286k
  const bool  isLast = cctx.isLast();
2711
286k
  int         firstSigPos = (isLast ? cctx.scanPosLast() : cctx.maxSubPos());
2712
286k
  int         nextSigPos = firstSigPos;
2713
2714
  //===== decode significant_coeffgroup_flag =====
2715
286k
  bool sigGroup = (isLast || !minSubPos);
2716
286k
  if (!sigGroup)
2717
117k
  {
2718
117k
    sigGroup = m_BinDecoder.decodeBin(cctx.sigGroupCtxId());
2719
117k
  }
2720
286k
  if (sigGroup)
2721
215k
  {
2722
215k
    cctx.setSigGroup();
2723
215k
  }
2724
71.2k
  else
2725
71.2k
  {
2726
71.2k
    return 0;
2727
71.2k
  }
2728
2729
  // make sure only takes up single L1 block
2730
215k
  ALIGN_DATA( 64, int     gt1Pos[16] );
2731
215k
  int *    gt1PosPtr    = gt1Pos;
2732
2733
  //===== decode absolute values =====
2734
215k
  const int inferSigPos   = nextSigPos != cctx.scanPosLast() ? (cctx.isNotFirst() ? minSubPos : -1) : nextSigPos;
2735
215k
  int       firstNZPos    = nextSigPos;
2736
215k
  int       lastNZPos     = -1;
2737
215k
  int       numNonZero    = 0, numGt1 = 0;
2738
215k
  int       remRegBins    = cctx.regBinLimit();
2739
215k
  int       gt1Mode1      = 0;
2740
215k
  unsigned  gt2Mask       = 0;
2741
215k
            stateVal      = 0;
2742
2743
2.11M
  for( ; nextSigPos >= minSubPos && remRegBins >= 4; nextSigPos-- )
2744
1.90M
  {
2745
1.90M
    int  blkPos  = cctx.blockPos(nextSigPos);
2746
1.90M
    bool sigFlag = (!numNonZero && nextSigPos == inferSigPos);
2747
2748
1.90M
    unsigned absVal = 0;
2749
2750
1.90M
    if (!sigFlag)
2751
1.76M
    {
2752
1.76M
      const unsigned sigCtxId = cctx.sigCtxIdAbs( blkPos, state );
2753
1.76M
      sigFlag = m_BinDecoder.decodeBin( sigCtxId );
2754
1.76M
      DTRACE(g_trace_ctx, D_SYNTAX_RESI, "sig_bin() bin=%d ctx=%d\n", sigFlag, sigCtxId);
2755
1.76M
      remRegBins--;
2756
1.76M
    }
2757
2758
1.90M
    if (sigFlag)
2759
1.07M
    {
2760
1.07M
      uint8_t ctxOff = cctx.ctxOffsetAbs();
2761
1.07M
      stateVal  = ( ( state >> 1 ) & 1 ) | ( stateVal << 1 );
2762
1.07M
      *sigPos++ = blkPos;
2763
1.07M
      numNonZero++;
2764
1.07M
      firstNZPos = nextSigPos;
2765
1.07M
      lastNZPos  = std::max<int>( lastNZPos, nextSigPos );
2766
1.07M
      unsigned gt1Flag = m_BinDecoder.decodeBin(cctx.greater1CtxIdAbs(ctxOff));
2767
1.07M
      DTRACE(g_trace_ctx, D_SYNTAX_RESI, "gt1_flag() bin=%d ctx=%d\n", gt1Flag, cctx.greater1CtxIdAbs(ctxOff));
2768
1.07M
      remRegBins--;
2769
2770
1.07M
      unsigned gt2Flag = 0;
2771
2772
1.07M
      if (gt1Flag)
2773
382k
      {
2774
382k
        unsigned parFlag = m_BinDecoder.decodeBin(cctx.parityCtxIdAbs(ctxOff));
2775
382k
        DTRACE(g_trace_ctx, D_SYNTAX_RESI, "par_flag() bin=%d ctx=%d\n", parFlag, cctx.parityCtxIdAbs(ctxOff));
2776
382k
        numGt1++;
2777
382k
        remRegBins--;
2778
382k
        gt2Flag = m_BinDecoder.decodeBin( cctx.greater2CtxIdAbs( ctxOff ) );
2779
382k
        gt2Mask               |= ( gt2Flag << (numGt1-1) );
2780
382k
        DTRACE( g_trace_ctx, D_SYNTAX_RESI, "gt2_flag() bin=%d ctx=%d\n", gt2Flag, cctx.greater2CtxIdAbs( ctxOff ) );
2781
382k
        remRegBins--;
2782
382k
        *gt1PosPtr++    = blkPos;
2783
382k
        absVal          = 2 + parFlag + (gt2Flag << 1);
2784
382k
        state           = ( stateTransTable >> ( ( state << 2 ) + ( parFlag << 1 ) ) ) & 3;
2785
382k
      }
2786
691k
      else
2787
691k
      {
2788
691k
        absVal = 1;
2789
691k
        state  = ( stateTransTable >> ( ( state << 2 ) + 2 ) ) & 3;
2790
691k
      }
2791
2792
1.07M
      cctx.absVal1stPass( blkPos, coeff, absVal );
2793
1.07M
    }
2794
828k
    else
2795
828k
    {
2796
828k
      state = ( stateTransTable >> ( state << 2 ) ) & 3;
2797
828k
    }
2798
1.90M
  }
2799
2800
215k
  cctx.setRegBinLimit( remRegBins );
2801
2802
215k
  gt1Mode1 = numGt1;
2803
2804
215k
  gt1PosPtr = gt1Pos;
2805
2806
  //===== 3rd PASS: Go-rice codes =====
2807
597k
  for( int k = 0; k < gt1Mode1; k++, gt2Mask >>= 1, gt1PosPtr++ )
2808
382k
  {
2809
382k
    if( gt2Mask & 1 )
2810
111k
    {
2811
111k
      int      sumAll  = cctx.templateAbsSum( *gt1PosPtr, coeff, 4 );
2812
111k
      unsigned ricePar = g_auiGoRiceParsCoeff[sumAll];
2813
2814
111k
      int rem = m_BinDecoder.decodeRemAbsEP( ricePar, COEF_REMAIN_BIN_REDUCTION, cctx.maxLog2TrDRange() );
2815
111k
      DTRACE( g_trace_ctx, D_SYNTAX_RESI, "rem_val() bin=%d ctx=%d\n", rem, ricePar );
2816
111k
      coeff[ *gt1PosPtr ] += ( rem << 1 );
2817
111k
    }
2818
382k
  }
2819
2820
2821
  //===== coeff bypass ====
2822
218k
  for( ; nextSigPos >= minSubPos; nextSigPos-- )
2823
3.35k
  {
2824
3.35k
    int       sub1      = ( state >> 1 ) & 1;
2825
3.35k
    int       blkPos    = cctx.blockPos( nextSigPos );
2826
3.35k
    int       sumAll    = cctx.templateAbsSum( blkPos, coeff, 0 );
2827
3.35k
    int       rice      = g_auiGoRiceParsCoeff                        [sumAll];
2828
3.35k
    int       pos0      = g_auiGoRicePosCoeff0(state, rice);
2829
3.35k
    int       rem       = m_BinDecoder.decodeRemAbsEP( rice, COEF_REMAIN_BIN_REDUCTION, cctx.maxLog2TrDRange() );
2830
3.35k
    DTRACE( g_trace_ctx, D_SYNTAX_RESI, "rem_val() bin=%d ctx=%d\n", rem, rice );
2831
3.35k
    TCoeffSig tcoeff    = ( rem == pos0 ? 0 : rem < pos0 ? rem+1 : rem );
2832
3.35k
    state               = ( stateTransTable >> ( ( state << 2 ) + ( ( tcoeff & 1 ) << 1 ) ) ) & 3;
2833
3.35k
    if( tcoeff )
2834
3.05k
    {
2835
3.05k
      coeff[blkPos] = tcoeff;
2836
3.05k
      stateVal      = sub1 | ( stateVal << 1 );
2837
3.05k
      *sigPos++     = blkPos;
2838
3.05k
      numNonZero++;
2839
3.05k
      firstNZPos = nextSigPos;
2840
3.05k
      lastNZPos  = std::max<int>( lastNZPos, nextSigPos );
2841
3.05k
    }
2842
3.35k
  }
2843
2844
  //===== decode sign's =====
2845
215k
  const unsigned  numSigns    = ( cctx.hideSign( firstNZPos, lastNZPos ) ? numNonZero - 1 : numNonZero );
2846
215k
  signPattern = m_BinDecoder.decodeBinsEP( numSigns );
2847
215k
  if( numNonZero > numSigns )
2848
131
  {
2849
131
    sigPos -= numNonZero;
2850
131
    int sumAbs = 0;
2851
1.36k
    for( int i = 0; i < numNonZero; i++ )
2852
1.23k
    {
2853
1.23k
      const int blockPos = *sigPos;
2854
1.23k
      sumAbs += coeff[blockPos];
2855
1.23k
      sigPos++;
2856
1.23k
    }
2857
131
    signPattern <<= 1;
2858
131
    signPattern += (sumAbs & 1);
2859
131
  }
2860
215k
  return numNonZero;
2861
286k
}
2862
2863
void CABACReader::residual_codingTS( TransformUnit& tu, ComponentID compID )
2864
8.50k
{
2865
8.50k
  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 );
2866
2867
  // if not TU split, otherwise already memset
2868
8.50k
  PelBuf pb = tu.cu->cs->getRecoBuf( tu.blocks[compID] );
2869
8.50k
  pb.memset( 0 );
2870
2871
  // init coeff coding context
2872
8.50k
  CoeffCodingContext  cctx( tu, compID, false, m_tplBuf );
2873
8.50k
  TCoeffSig *coeff = m_cffTmp;
2874
8.50k
  ::memset( coeff, 0, cctx.maxNumCoeff() * sizeof( TCoeffSig ) );
2875
2876
8.50k
  int maxCtxBins = ( cctx.maxNumCoeff() * 7 ) >> 2;
2877
8.50k
  cctx.setNumCtxBins( maxCtxBins );
2878
2879
8.50k
  int maxX = 0;
2880
8.50k
  int maxY = 0;
2881
2882
71.0k
  for( int subSetId = 0; subSetId <= ( cctx.maxNumCoeff() - 1 ) >> cctx.log2CGSize(); subSetId++ )
2883
62.5k
  {
2884
62.5k
    cctx.initSubblock         ( subSetId );
2885
62.5k
    residual_coding_subblockTS( cctx, coeff, tu.cu->cs->getRecoBuf( tu.block( compID ) ), maxX, maxY );
2886
62.5k
  }
2887
2888
8.50k
  if( cctx.bdpcm() )
2889
5.31k
  {
2890
5.31k
    tu.maxScanPosX[compID] = cctx.width();
2891
5.31k
    tu.maxScanPosY[compID] = cctx.height();
2892
5.31k
  }
2893
3.19k
  else
2894
3.19k
  {
2895
3.19k
    tu.maxScanPosX[compID] = maxX;
2896
3.19k
    tu.maxScanPosY[compID] = maxY;
2897
3.19k
  }
2898
8.50k
}
2899
2900
void CABACReader::residual_coding_subblockTS( CoeffCodingContext& cctx, TCoeffSig* coeff, CoeffSigBuf dstcoeff, int& maxX, int& maxY )
2901
62.5k
{
2902
  // TODO: awi, profile and optimize similar to residual_coding_subblock(...)
2903
2904
  // NOTE: All coefficients of the subblock must be set to zero before calling this function
2905
  //===== init =====
2906
62.5k
  const int   minSubPos   = cctx.maxSubPos();
2907
62.5k
  int         firstSigPos = cctx.minSubPos();
2908
62.5k
  int         nextSigPos  = firstSigPos;
2909
62.5k
  unsigned    signPattern = 0;
2910
2911
  //===== decode significant_coeffgroup_flag =====
2912
62.5k
  bool sigGroup = cctx.isLastSubSet() && cctx.noneSigGroup();
2913
62.5k
  if( !sigGroup )
2914
58.3k
  {
2915
58.3k
    sigGroup = m_BinDecoder.decodeBin( cctx.sigGroupCtxId( true ) );
2916
58.3k
    DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sigGroup() bin=%d ctx=%d\n", sigGroup, cctx.sigGroupCtxId() );
2917
58.3k
  }
2918
62.5k
  if( sigGroup )
2919
19.9k
  {
2920
19.9k
    cctx.setSigGroup();
2921
19.9k
  }
2922
42.6k
  else
2923
42.6k
  {
2924
42.6k
    return;
2925
42.6k
  }
2926
2927
  //===== decode absolute values =====
2928
19.9k
  const int inferSigPos   = minSubPos;
2929
19.9k
  int       numNonZero    =  0;
2930
19.9k
  int       sigBlkPos[ 1 << MLS_CG_SIZE ];
2931
2932
19.9k
  int lastScanPosPass1 = -1;
2933
19.9k
  int lastScanPosPass2 = -1;
2934
332k
  for (; nextSigPos <= minSubPos && cctx.numCtxBins() >= 4; nextSigPos++)
2935
312k
  {
2936
312k
    int      blkPos     = cctx.blockPos( nextSigPos );
2937
312k
    unsigned sigFlag    = ( !numNonZero && nextSigPos == inferSigPos );
2938
2939
312k
    if( !sigFlag )
2940
309k
    {
2941
309k
        const unsigned sigCtxId = cctx.sigCtxIdAbsTS( blkPos, coeff );
2942
309k
        sigFlag = m_BinDecoder.decodeBin( sigCtxId );
2943
309k
        DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sig_bin() bin=%d ctx=%d\n", sigFlag, sigCtxId );
2944
309k
        cctx.decNumCtxBins(1);
2945
309k
    }
2946
2947
312k
    if( sigFlag )
2948
92.9k
    {
2949
      //===== decode sign's =====
2950
92.9k
      int sign;
2951
92.9k
        const unsigned signCtxId = cctx.signCtxIdAbsTS(blkPos, coeff, cctx.bdpcm());
2952
92.9k
        sign = m_BinDecoder.decodeBin(signCtxId);
2953
92.9k
        DTRACE( g_trace_ctx, D_SYNTAX_RESI, "sign() bin=%d ctx=%d  nextSigPos=%d  blkPos=%d\n", sign, signCtxId, nextSigPos, blkPos );
2954
92.9k
        cctx.decNumCtxBins(1);
2955
2956
92.9k
      signPattern += ( sign << numNonZero );
2957
2958
92.9k
      sigBlkPos[numNonZero++] = blkPos;
2959
2960
92.9k
      unsigned gt1Flag;
2961
92.9k
      const unsigned gt1CtxId = cctx.lrg1CtxIdAbsTS(blkPos, coeff, cctx.bdpcm());
2962
92.9k
        gt1Flag = m_BinDecoder.decodeBin(gt1CtxId);
2963
92.9k
        DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_gt1_flag() bin=%d ctx=%d\n", gt1Flag, gt1CtxId );
2964
92.9k
        cctx.decNumCtxBins(1);
2965
2966
92.9k
      unsigned parFlag = 0;
2967
92.9k
      if( gt1Flag )
2968
55.0k
      {
2969
55.0k
          parFlag = m_BinDecoder.decodeBin( cctx.parityCtxIdAbsTS() );
2970
55.0k
          DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_par_flag() bin=%d ctx=%d\n", parFlag, cctx.parityCtxIdAbsTS() );
2971
55.0k
          cctx.decNumCtxBins(1);
2972
55.0k
      }
2973
92.9k
      coeff[ blkPos ] = (sign ? -1 : 1 ) * (1 + parFlag + gt1Flag);
2974
92.9k
      DTRACE( g_trace_ctx, D_SYNTAX_RESI, "coeff[ blkPos ]=%d  blkPos=%d\n", coeff[ blkPos ], blkPos  );
2975
92.9k
    }
2976
312k
    lastScanPosPass1 = nextSigPos;
2977
312k
  }
2978
2979
19.9k
  int cutoffVal = 2;
2980
19.9k
  int numGtBins = 4;
2981
2982
  //===== 2nd PASS: gt2 =====
2983
321k
  for (int scanPos = firstSigPos; scanPos <= minSubPos && cctx.numCtxBins() >= 4; scanPos++)
2984
301k
  {
2985
301k
    TCoeffSig& tcoeff = coeff[cctx.blockPos( scanPos )];
2986
301k
    cutoffVal = 2;
2987
1.50M
    for( int i = 0; i < numGtBins; i++ )
2988
1.20M
    {
2989
1.20M
      if( tcoeff < 0 )
2990
40.3k
      {
2991
40.3k
        tcoeff = -tcoeff;
2992
40.3k
      }
2993
2994
1.20M
      if( tcoeff >= cutoffVal )
2995
59.7k
      {
2996
59.7k
        unsigned gt2Flag;
2997
59.7k
          gt2Flag = m_BinDecoder.decodeBin( cctx.greaterXCtxIdAbsTS( cutoffVal >> 1 ) );
2998
59.7k
          tcoeff += ( gt2Flag << 1 );
2999
59.7k
          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 );
3000
59.7k
            cctx.decNumCtxBins(1);
3001
59.7k
      }
3002
1.20M
      cutoffVal += 2;
3003
1.20M
    }
3004
301k
    lastScanPosPass2 = scanPos;
3005
301k
  }
3006
  //===== 3rd PASS: Go-rice codes =====
3007
338k
  for( int scanPos = firstSigPos; scanPos <= minSubPos; scanPos++ )
3008
318k
  {
3009
318k
    TCoeffSig& tcoeff = coeff[ cctx.blockPos( scanPos ) ];
3010
3011
318k
    cutoffVal = (scanPos <= lastScanPosPass2 ? 10 : (scanPos <= lastScanPosPass1 ? 2 : 0));
3012
318k
    if (tcoeff < 0)
3013
2.29k
    {
3014
2.29k
      tcoeff = -tcoeff;
3015
2.29k
    }
3016
3017
318k
    if( tcoeff >= cutoffVal )
3018
9.96k
    {
3019
9.96k
      int       rice = cctx.templateAbsSumTS( cctx.blockPos( scanPos ), coeff );
3020
9.96k
      int       rem  = m_BinDecoder.decodeRemAbsEP( rice, COEF_REMAIN_BIN_REDUCTION, cctx.maxLog2TrDRange() );
3021
9.96k
      DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_rem_val() bin=%d ctx=%d sp=%d\n", rem, rice, scanPos );
3022
9.96k
      tcoeff += (scanPos <= lastScanPosPass1) ? (rem << 1) : rem;
3023
9.96k
      if (tcoeff && scanPos > lastScanPosPass1)
3024
4.85k
      {
3025
4.85k
        int      blkPos = cctx.blockPos(scanPos);
3026
4.85k
        int sign = m_BinDecoder.decodeBinEP();
3027
4.85k
        signPattern += (sign << numNonZero);
3028
4.85k
        sigBlkPos[numNonZero++] = blkPos;
3029
4.85k
      }
3030
9.96k
    }
3031
318k
    if (!cctx.bdpcm() && cutoffVal)
3032
117k
    {
3033
117k
      if (tcoeff > 0)
3034
34.7k
      {
3035
34.7k
        int rightPixel, belowPixel;
3036
34.7k
        cctx.neighTS(rightPixel, belowPixel, cctx.blockPos( scanPos ), coeff);
3037
34.7k
        tcoeff = cctx.decDeriveModCoeff(rightPixel, belowPixel, tcoeff);
3038
34.7k
      }
3039
117k
    }
3040
318k
  }
3041
3042
  //===== set final coefficents =====
3043
117k
  for( unsigned k = 0; k < numNonZero; k++ )
3044
97.5k
  {
3045
97.5k
    int AbsCoeff              = coeff[ sigBlkPos[ k ] ];
3046
97.5k
    int blkPos                = sigBlkPos[ k ];
3047
97.5k
    const int posX            = cctx.posX( blkPos );
3048
97.5k
    const int posY            = cctx.posY( blkPos );
3049
97.5k
    maxX                      = std::max<int>( maxX, posX );
3050
97.5k
    maxY                      = std::max<int>( maxY, posY );
3051
3052
97.5k
    dstcoeff.at( posX, posY ) = ( signPattern & 1u ? -AbsCoeff : AbsCoeff );
3053
97.5k
    coeff[ sigBlkPos[ k ] ]   = ( signPattern & 1u ? -AbsCoeff : AbsCoeff );
3054
97.5k
    signPattern         >>= 1;
3055
97.5k
  }
3056
19.9k
}
3057
3058
3059
//================================================================================
3060
//  helper functions
3061
//--------------------------------------------------------------------------------
3062
//    unsigned  unary_max_symbol ( ctxId0, ctxId1, maxSymbol )
3063
//    unsigned  unary_max_eqprob (                 maxSymbol )
3064
//    unsigned  exp_golomb_eqprob( count )
3065
//================================================================================
3066
3067
unsigned CABACReader::unary_max_symbol( unsigned ctxId0, unsigned ctxIdN, unsigned maxSymbol  )
3068
18.3k
{
3069
18.3k
  unsigned onesRead = 0;
3070
27.9k
  while( onesRead < maxSymbol && m_BinDecoder.decodeBin( onesRead == 0 ? ctxId0 : ctxIdN ) == 1 )
3071
9.56k
  {
3072
9.56k
    ++onesRead;
3073
9.56k
  }
3074
18.3k
  return onesRead;
3075
18.3k
}
3076
3077
3078
unsigned CABACReader::unary_max_eqprob( unsigned maxSymbol )
3079
7.50k
{
3080
13.7k
  for( unsigned k = 0; k < maxSymbol; k++ )
3081
13.7k
  {
3082
13.7k
    if( !m_BinDecoder.decodeBinEP() )
3083
7.44k
    {
3084
7.44k
      return k;
3085
7.44k
    }
3086
13.7k
  }
3087
62
  return maxSymbol;
3088
7.50k
}
3089
3090
3091
unsigned CABACReader::exp_golomb_eqprob( unsigned count )
3092
278
{
3093
278
  unsigned symbol = 0;
3094
278
  unsigned bit    = 1;
3095
2.25k
  while( bit )
3096
1.97k
  {
3097
1.97k
    bit     = m_BinDecoder.decodeBinEP( );
3098
1.97k
    symbol += bit << count++;
3099
1.97k
  }
3100
278
  if( --count )
3101
130
  {
3102
130
    symbol += m_BinDecoder.decodeBinsEP( count );
3103
130
  }
3104
278
  return symbol;
3105
278
}
3106
3107
unsigned CABACReader::code_unary_fixed( unsigned ctxId, unsigned unary_max, unsigned fixed )
3108
0
{
3109
0
  unsigned idx;
3110
0
  bool unary = m_BinDecoder.decodeBin( ctxId );
3111
0
  if( unary )
3112
0
  {
3113
0
    idx = unary_max_eqprob( unary_max );
3114
0
  }
3115
0
  else
3116
0
  {
3117
0
    idx = unary_max + 1 + m_BinDecoder.decodeBinsEP( fixed );
3118
0
  }
3119
0
  return idx;
3120
0
}
3121
3122
void CABACReader::mip_flag( CodingUnit& cu )
3123
66.3k
{
3124
66.3k
  if( !cu.sps->getUseMIP() )
3125
617
  {
3126
    //cu.setMipFlag( false );
3127
617
    return;
3128
617
  }
3129
3130
65.7k
  unsigned ctxId = DeriveCtx::CtxMipFlag( cu );
3131
65.7k
  cu.setMipFlag( m_BinDecoder.decodeBin( Ctx::MipFlag( ctxId ) ) );
3132
65.7k
  DTRACE( g_trace_ctx, D_SYNTAX, "mip_flag() pos=(%d,%d) mode=%d\n", cu.lumaPos().x, cu.lumaPos().y, cu.mipFlag() ? 1 : 0 );
3133
65.7k
}
3134
3135
void CABACReader::mip_pred_mode( CodingUnit &cu )
3136
10.9k
{
3137
10.9k
  cu.setMipTransposedFlag( !!m_BinDecoder.decodeBinEP() );
3138
3139
10.9k
  uint32_t mipMode;
3140
10.9k
  const int numModes = getNumModesMip( cu.Y() );
3141
10.9k
  xReadTruncBinCode( mipMode, numModes );
3142
10.9k
  cu.intraDir[CHANNEL_TYPE_LUMA] = mipMode;
3143
10.9k
  CHECKD( cu.intraDir[CHANNEL_TYPE_LUMA] < 0 || cu.intraDir[CHANNEL_TYPE_LUMA] >= numModes, "Invalid MIP mode" );
3144
3145
10.9k
  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 );
3146
10.9k
}
3147
3148
}