Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/CommonLib/ContextModelling.h
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
/** \file     ContextModelling.h
43
 *  \brief    Classes providing probability descriptions and contexts (header)
44
 */
45
46
#pragma once
47
48
#include "CommonDef.h"
49
#include "Contexts.h"
50
#include "Slice.h"
51
#include "Unit.h"
52
#include "UnitPartitioner.h"
53
54
#include <bitset>
55
56
//! \ingroup CommonLib
57
//! \{
58
59
namespace vvenc
60
{
61
62
struct CtxTpl
63
{
64
  // lower 5 bits are absSum1, upper 3 bits are numPos
65
  uint8_t ctxTpl;
66
};
67
68
// The part of the context offset of sig_coeff_flag that comes from the template sum (5 bits) and from the diagonal, by channel type; g_absCtxOfsDiag: the diagonal's part for the parity and greater flags
69
extern const uint8_t g_sigCtxOfsSum [32];
70
extern const uint8_t g_sigCtxOfsDiag[MAX_NUM_CH][128];
71
extern const uint8_t g_absCtxOfsDiag[MAX_NUM_CH][128];
72
73
// A template buffer holds a block and two more rows' room, since CoeffCodingContext::absVal1stPass writes neighbours without testing for the block's edge
74
static const int CTX_TPL_BUF_SIZE = MAX_TB_SIZEY * MAX_TB_SIZEY + 2 * MAX_TB_SIZEY;
75
76
struct CoeffCodingContext
77
{
78
public:
79
  // scanPosLast: the last position if known (bounds the template buffer rows cleared), -1 if not, DeferTplClear to clear
80
  // nothing here and call clearTemplates once the last position is known (RDOQ2)
81
  static const int DeferTplClear = -2;
82
  CoeffCodingContext( const TransformUnit& tu, ComponentID component, bool signHide, bool bdpcm = false, CtxTpl* tplBuf = nullptr, int scanPosLast = -1 );
83
  void clearTemplates( int scanPosLast );
84
  // The context derivation of last_sig_coeff_x / y prefix for a block side of 1 << log2Size: its largest prefix group
85
  // (of the side after the zero-out) and the context offset and shift of a group. Shared with DepQuant's rate tables.
86
1.88M
  static unsigned lastPosMaxGroup ( unsigned log2Size )                     { return g_uiGroupIdx[std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, 1u << log2Size ) - 1]; }
87
1.88M
  static int      lastPosCtxOffset( ChannelType chType, unsigned log2Size ) { static const uint8_t prefixCtx[8] = { 0, 0, 0, 3, 6, 10, 15, 21 }; return chType == CH_C ? 0 : prefixCtx[log2Size]; }
88
1.88M
  static int      lastPosCtxShift ( ChannelType chType, unsigned log2Size ) { return chType == CH_C ? Clip3( 0, 2, int( ( 1u << log2Size ) >> 3 ) ) : int( log2Size + 1 ) >> 2; }
89
  // The first part of initSubblock, without the sig group flag and the transform-skip context (RDOQ2's regular path
90
  // never reads it)
91
  void initSubblockRegular( int SubsetId )
92
2.27M
  {
93
2.27M
    m_subSetId          = SubsetId;
94
2.27M
    m_subSetPos         = m_scanCG[m_subSetId].idx;
95
2.27M
    m_subSetPosY        = m_subSetPos >> m_log2WidthInGroups;
96
2.27M
    m_subSetPosX        = m_subSetPos - ( m_subSetPosY << m_log2WidthInGroups );
97
2.27M
    m_minSubPos         = m_subSetId << m_log2CGSize;
98
2.27M
    m_maxSubPos         = m_minSubPos + ( 1 << m_log2CGSize ) - 1;
99
2.27M
    const unsigned sigRight = unsigned( ( m_subSetPosX + 1 ) < int( m_widthInGroups  ) ? m_sigCoeffGroupFlag[m_subSetPos + 1               ] : false );
100
2.27M
    const unsigned sigLower = unsigned( ( m_subSetPosY + 1 ) < int( m_heightInGroups ) ? m_sigCoeffGroupFlag[m_subSetPos + m_widthInGroups ] : false );
101
2.27M
    m_sigGroupCtxId     = Ctx::SigCoeffGroup[m_chType]( sigRight | sigLower );
102
2.27M
    m_sigGroupCtxIdTS   = -1;   // not derived
103
2.27M
  }
104
public:
105
  void  initSubblock    ( int SubsetId, bool sigGroupFlag = false );
106
public:
107
12.5k
  void  resetSigGroup   ()                      { m_sigCoeffGroupFlag.reset( m_subSetPos ); }
108
537k
  void  setSigGroup     ()                      { m_sigCoeffGroupFlag.set( m_subSetPos ); }
109
0
  bool  noneSigGroup    ()                      { return m_sigCoeffGroupFlag.none(); }
110
537k
  int   lastSubSet      ()                      { return ( maxNumCoeff() - 1 ) >> log2CGSize(); }
111
458k
  bool  isLastSubSet    ()                      { return lastSubSet() == m_subSetId; }
112
78.5k
  bool  only1stSigGroup ()                      { return m_sigCoeffGroupFlag.count()-m_sigCoeffGroupFlag[lastSubSet()]==0; }
113
1.04M
  void  setScanPosLast  ( int       posLast )   { m_scanPosLast = posLast; }
114
public:
115
0
  ComponentID     compID          ()                        const { return m_compID; }
116
748k
  int             subSetId        ()                        const { return m_subSetId; }
117
0
  int             subSetPos       ()                        const { return m_subSetPos; }
118
40.1k
  int             cgPosY          ()                        const { return m_subSetPosY; }
119
40.1k
  int             cgPosX          ()                        const { return m_subSetPosX; }
120
3.12M
  unsigned        width           ()                        const { return m_width; }
121
0
  unsigned        height          ()                        const { return m_height; }
122
1.04M
  unsigned        log2CGWidth     ()                        const { return m_log2CGWidth; }
123
1.01M
  unsigned        log2CGHeight    ()                        const { return m_log2CGHeight; }
124
0
  const ScanElement& cgScan      ( int scanPosCG )         const { return m_scanCG[scanPosCG]; }
125
4.31M
  unsigned        log2CGSize      ()                        const { return m_log2CGSize; }
126
6.19M
  int             maxLog2TrDRange ()                        const { return m_maxLog2TrDynamicRange; }
127
8.68M
  unsigned        maxNumCoeff     ()                        const { return m_maxNumCoeff; }
128
21.6M
  int             scanPosLast     ()                        const { return m_scanPosLast; }
129
13.5M
  int             minSubPos       ()                        const { return m_minSubPos; }
130
488k
  int             maxSubPos       ()                        const { return m_maxSubPos; }
131
1.07M
  bool            isLast          ()                        const { return ( ( m_scanPosLast >> m_log2CGSize ) == m_subSetId ); }
132
50.4k
  bool            isNotFirst      ()                        const { return ( m_subSetId != 0 ); }
133
0
  bool            isSigGroup      ( int scanPosCG )         const { return m_sigCoeffGroupFlag[m_scanCG[scanPosCG].idx]; }
134
1.26M
  bool            isSigGroup      ()                        const { return m_sigCoeffGroupFlag[ m_subSetPos ]; }
135
10.4k
  bool            signHiding      ()                        const { return m_signHiding; }
136
  bool            hideSign        ( int       posFirst,
137
1.06M
                                    int       posLast   )   const { return ( m_signHiding && ( posLast - posFirst >= SBH_THRESHOLD ) ); }
138
0
  const ScanElement* scan          ()                        const { return m_scan; }
139
56.9M
  unsigned        blockPos        ( int scanPos )           const { return m_scan[scanPos].idx; }
140
11.5M
  unsigned        posX            ( int scanPos )           const { return m_scan[scanPos].x; }
141
11.5M
  unsigned        posY            ( int scanPos )           const { return m_scan[scanPos].y; }
142
1.04M
  unsigned        maxLastPosX     ()                        const { return m_maxLastPosX; }
143
1.04M
  unsigned        maxLastPosY     ()                        const { return m_maxLastPosY; }
144
2.37M
  unsigned        lastXCtxId      ( unsigned  posLastX  )   const { return m_CtxSetLastX( m_lastOffsetX + ( posLastX >> m_lastShiftX ) ); }
145
2.44M
  unsigned        lastYCtxId      ( unsigned  posLastY  )   const { return m_CtxSetLastY( m_lastOffsetY + ( posLastY >> m_lastShiftY ) ); }
146
1.20M
  unsigned        sigGroupCtxId   ( bool ts = false     )   const { return ts ? m_sigGroupCtxIdTS : m_sigGroupCtxId; }
147
7.55M
  bool            bdpcm           ()                        const { return m_bdpcm; }
148
149
  // only RDOQ calls this, with state 0
150
  unsigned sigCtxIdAbs( int scanPos, const TCoeffSig* coeff, const int state )
151
0
  {
152
0
    const uint32_t   posY      = m_scan[scanPos].y;
153
0
    const uint32_t   posX      = m_scan[scanPos].x;
154
0
    const TCoeffSig* pData     = coeff + posX + posY * m_width;
155
0
    const int        diag      = posX + posY;
156
0
    int              numPos    = 0;
157
0
    int              sumAbs    = 0;
158
0
#define UPDATE(x) {int a=abs(x);sumAbs+=std::min(4+(a&1),a);numPos+=!!a;}
159
0
    if( posX < m_width-1 )
160
0
    {
161
0
      UPDATE( pData[1] );
162
0
      if( posX < m_width-2 )
163
0
      {
164
0
        UPDATE( pData[2] );
165
0
      }
166
0
      if( posY < m_height-1 )
167
0
      {
168
0
        UPDATE( pData[m_width+1] );
169
0
      }
170
0
    }
171
0
    if( posY < m_height-1 )
172
0
    {
173
0
      UPDATE( pData[m_width] );
174
0
      if( posY < m_height-2 )
175
0
      {
176
0
        UPDATE( pData[m_width<<1] );
177
0
      }
178
0
    }
179
0
#undef UPDATE
180
181
0
    int ctxOfs = std::min((sumAbs+1)>>1, 3) + ( diag < 2 ? 4 : 0 );
182
183
0
    if( m_chType == CH_L )
184
0
    {
185
0
      ctxOfs += diag < 5 ? 4 : 0;
186
0
    }
187
188
0
    m_tmplCpDiag = diag;
189
0
    m_tmplCpSum1 = sumAbs - numPos;
190
0
    return m_sigFlagCtxSet[state]( ctxOfs );
191
0
  }
192
193
  // sigCtxIdAbsOnly and absVal1stPass run for every coefficient position of residual coding
194
  // For RDOQ2 (state 0): the sig context and the gtx context offset (as sigCtxIdAbs + ctxOffsetAbs) from one template read. The
195
  // offset comes from g_absCtxOfsDiag, which holds what ctxOffsetAbs works out per diagonal (luma 15 / 10 / 5 / 0 from
196
  // diagonals 0 / 1 / 3 / 10, chroma 5 / 0 from 0 / 1); sumAbs >= numPos holds, as each significant neighbour adds at least 1.
197
  ALWAYS_INLINE unsigned sigCtxIdAndCtxOffset( const int scanPos, int& ctxOffset ) const
198
0
  {
199
0
    const auto scanEl = m_scan[scanPos];
200
0
    const int  diag   = scanEl.x + scanEl.y;
201
0
    const int  tplVal = m_tplBuf[-int( scanEl.idx )].ctxTpl;
202
0
    const int  sumAbs = tplVal & 31;
203
0
    CHECKD( diag >= 128, "diagonal beyond the context offset tables" );
204
0
    CHECKD( sumAbs < int( tplVal >> 5u ), "template sum below the number of significant neighbours" );
205
0
    ctxOffset = std::min( sumAbs - int( tplVal >> 5u ), 4 ) + 1 + g_absCtxOfsDiag[m_chType][diag];
206
0
    return m_sigFlagCtxSet[0]( g_sigCtxOfsSum[sumAbs] + g_sigCtxOfsDiag[m_chType][diag] );
207
0
  }
208
209
  // The sig context alone, for residual_coding_subblock: nothing is stored per position
210
  ALWAYS_INLINE unsigned sigCtxIdAbsOnly( const int scanPos, const int state ) const
211
7.39M
  {
212
7.39M
    const auto scanEl       = m_scan[scanPos];
213
7.39M
    const int  diag         = scanEl.x + scanEl.y;
214
7.39M
    const int  sumAbs       = m_tplBuf[-int( scanEl.idx )].ctxTpl & 31;
215
7.39M
    return m_sigFlagCtxSet[state]( g_sigCtxOfsSum[sumAbs] + g_sigCtxOfsDiag[m_chType][diag] );
216
7.39M
  }
217
218
  ALWAYS_INLINE uint8_t ctxOffsetAbsAt( const int scanPos ) const
219
6.75M
  {
220
6.75M
    const auto scanEl       = m_scan[scanPos];
221
6.75M
    const int  tplVal       = m_tplBuf[-int( scanEl.idx )].ctxTpl;
222
6.75M
    return uint8_t( std::min( ( tplVal & 31 ) - ( tplVal >> 5u ), 4 ) + 1 + g_absCtxOfsDiag[m_chType][scanEl.x + scanEl.y] );
223
6.75M
  }
224
225
  ALWAYS_INLINE void absVal1stPass( const int scanPos, const TCoeffSig absLevel1 )
226
7.79M
  {
227
7.79M
    CHECKD( !absLevel1, "Should not be called if '0'!" );
228
229
7.79M
    const auto scanEl     = m_scan[scanPos];
230
7.79M
    const int  posY       = scanEl.y;
231
7.79M
    const int  posX       = scanEl.x;
232
7.79M
    const int  add        = 32 + absLevel1;
233
234
    // all ones if the neighbour exists; one that doesn't gets 0 added, in the block or in the room left by CTX_TPL_BUF_SIZE
235
7.79M
    const int  hasX1      = ( -posX ) >> 31;
236
7.79M
    const int  hasX2      = ( 1 - posX ) >> 31;
237
7.79M
    const int  hasY1      = ( -posY ) >> 31;
238
7.79M
    const int  hasY2      = ( 1 - posY ) >> 31;
239
7.79M
    CtxTpl*    tpl        = m_tplBuf - int( scanEl.idx );
240
241
7.79M
    tpl[2 * m_width].ctxTpl += uint8_t( add & hasY2 );
242
7.79M
    tpl[m_width + 1].ctxTpl += uint8_t( add & hasY1 & hasX1 );
243
7.79M
    tpl[m_width    ].ctxTpl += uint8_t( add & hasY1 );
244
7.79M
    tpl[2          ].ctxTpl += uint8_t( add & hasX2 );
245
7.79M
    tpl[1          ].ctxTpl += uint8_t( add & hasX1 );
246
7.79M
  }
247
  
248
249
  void remAbsVal1stPass( const int scanPos, const TCoeffSig absLevel1 )
250
0
  {
251
0
    CHECKD( !absLevel1, "Should not be called if '0'!" );
252
253
0
    const auto scanEl     = m_scan[scanPos];
254
0
    const uint32_t posY   = scanEl.y;
255
0
    const uint32_t posX   = scanEl.x;
256
0
    const int32_t  blkPos = scanEl.idx;
257
258
0
    auto update_deps = [&]( int offset )
259
0
    {
260
0
      auto& ctx   = m_tplBuf[-blkPos + offset];
261
0
      ctx.ctxTpl -= uint8_t( 32 + absLevel1 );
262
0
    };
263
264
0
    if( posY > 1 ) update_deps( 2 * m_width );
265
0
    if( posY > 0
266
0
     && posX > 0 ) update_deps( m_width + 1 );
267
0
    if( posY > 0 ) update_deps( m_width );
268
0
    if( posX > 1 ) update_deps( 2 );
269
0
    if( posX > 0 ) update_deps( 1 );
270
0
  }
271
272
  uint8_t ctxOffsetAbs()
273
0
  {
274
0
    int offset = 0;
275
0
    if( m_tmplCpDiag != -1 )
276
0
    {
277
0
      offset  = std::min( m_tmplCpSum1, 4 ) + 1;
278
0
      offset += ( !m_tmplCpDiag ? ( m_chType == CH_L ? 15 : 5 ) : m_chType == CH_L ? m_tmplCpDiag < 3 ? 10 : ( m_tmplCpDiag < 10 ? 5 : 0 ) : 0 );
279
0
    }
280
0
    return uint8_t(offset);
281
0
  }
282
283
6.79M
  unsigned parityCtxIdAbs   ( uint8_t offset )  const { return m_parFlagCtxSet   ( offset ); }
284
7.79M
  unsigned greater1CtxIdAbs ( uint8_t offset )  const { return m_gtxFlagCtxSet[1]( offset ); }
285
6.79M
  unsigned greater2CtxIdAbs ( uint8_t offset )  const { return m_gtxFlagCtxSet[0]( offset ); }
286
287
  unsigned templateAbsSum( int scanPos, const TCoeffSig* coeff, int baseLevel )
288
5.56M
  {
289
5.56M
    const uint32_t   posY = m_scan[scanPos].y;
290
5.56M
    const uint32_t   posX = m_scan[scanPos].x;
291
5.56M
    const TCoeffSig* pData = coeff + posX + posY * m_width;
292
5.56M
    int              sum   = 0;
293
5.56M
    if (posX < m_width - 1)
294
5.46M
    {
295
5.46M
      sum += abs(pData[1]);
296
5.46M
      if (posX < m_width - 2)
297
5.25M
      {
298
5.25M
        sum += abs(pData[2]);
299
5.25M
      }
300
5.46M
      if (posY < m_height - 1)
301
5.29M
      {
302
5.29M
        sum += abs(pData[m_width + 1]);
303
5.29M
      }
304
5.46M
    }
305
5.56M
    if (posY < m_height - 1)
306
5.40M
    {
307
5.40M
      sum += abs(pData[m_width]);
308
5.40M
      if (posY < m_height - 2)
309
5.14M
      {
310
5.14M
        sum += abs(pData[m_width << 1]);
311
5.14M
      }
312
5.40M
    }
313
5.56M
    return std::max(std::min(sum - 5 * baseLevel, 31), 0);
314
5.56M
  }
315
316
  unsigned sigCtxIdAbsTS( int scanPos, const TCoeffSig* coeff )
317
14.5M
  {
318
14.5M
    const uint32_t   posY   = m_scan[scanPos].y;
319
14.5M
    const uint32_t   posX   = m_scan[scanPos].x;
320
14.5M
    const TCoeffSig* posC   = coeff + posX + posY * m_width;
321
14.5M
    int              numPos = 0;
322
25.8M
#define UPDATE(x) {int a=abs(x);numPos+=!!a;}
323
14.5M
    if( posX > 0 )
324
13.1M
    {
325
13.1M
      UPDATE( posC[-1] );
326
13.1M
    }
327
14.5M
    if( posY > 0 )
328
12.7M
    {
329
12.7M
      UPDATE( posC[-(int)m_width] );
330
12.7M
    }
331
14.5M
#undef UPDATE
332
333
14.5M
    return m_tsSigFlagCtxSet( numPos );
334
14.5M
  }
335
336
12.0M
  unsigned parityCtxIdAbsTS   ()                  const { return m_tsParFlagCtxSet(      0 ); }
337
4.42M
  unsigned greaterXCtxIdAbsTS ( uint8_t offset )  const { return m_tsGtxFlagCtxSet( offset ); }
338
339
  unsigned lrg1CtxIdAbsTS(int scanPos, const TCoeffSig* coeff, int bdpcm)
340
12.0M
  {
341
12.0M
    const uint32_t   posY = m_scan[scanPos].y;
342
12.0M
    const uint32_t   posX = m_scan[scanPos].x;
343
12.0M
    const TCoeffSig* posC = coeff + posX + posY * m_width;
344
345
12.0M
    int             numPos = 0;
346
12.0M
#define UPDATE(x) {int a=abs(x);numPos+=!!a;}
347
348
12.0M
    if (bdpcm)
349
12.0M
    {
350
12.0M
      numPos = 3;
351
12.0M
    }
352
33.8k
    else
353
33.8k
    {
354
33.8k
      if (posX > 0)
355
33.8k
      {
356
33.8k
        UPDATE(posC[-1]);
357
33.8k
      }
358
33.8k
      if (posY > 0)
359
33.8k
      {
360
33.8k
        UPDATE(posC[-(int)m_width]);
361
33.8k
      }
362
33.8k
    }
363
364
12.0M
#undef UPDATE
365
12.0M
    return m_tsLrg1FlagCtxSet(numPos);
366
12.0M
  }
367
368
  unsigned signCtxIdAbsTS(int scanPos, const TCoeffSig* coeff, int bdpcm)
369
12.0M
  {
370
12.0M
    const uint32_t   posY  = m_scan[scanPos].y;
371
12.0M
    const uint32_t   posX  = m_scan[scanPos].x;
372
12.0M
    const TCoeffSig* pData = coeff + posX + posY * m_width;
373
374
12.0M
    int rightSign = 0, belowSign = 0;
375
12.0M
    unsigned signCtx = 0;
376
377
12.0M
    if (posX > 0)
378
10.8M
    {
379
10.8M
      rightSign = pData[-1];
380
10.8M
    }
381
12.0M
    if (posY > 0)
382
10.2M
    {
383
10.2M
      belowSign = pData[-(int)m_width];
384
10.2M
    }
385
386
12.0M
    if ((rightSign == 0 && belowSign == 0) || ((rightSign*belowSign) < 0))
387
10.4M
    {
388
10.4M
      signCtx = 0;
389
10.4M
    }
390
1.62M
    else if (rightSign >= 0 && belowSign >= 0)
391
499
    {
392
499
      signCtx = 1;
393
499
    }
394
1.62M
    else
395
1.62M
    {
396
1.62M
      signCtx = 2;
397
1.62M
    }
398
12.0M
    if (bdpcm)
399
12.0M
    {
400
12.0M
      signCtx += 3;
401
12.0M
    }
402
12.0M
    return m_tsSignFlagCtxSet(signCtx);
403
12.0M
  }
404
405
  void neighTS(int& rightPixel, int& belowPixel, int scanPos, const TCoeffSig* coeff)
406
17.4M
  {
407
17.4M
    const uint32_t   posY = m_scan[scanPos].y;
408
17.4M
    const uint32_t   posX = m_scan[scanPos].x;
409
17.4M
    const TCoeffSig* data = coeff + posX + posY * m_width;
410
411
17.4M
    rightPixel = belowPixel = 0;
412
413
17.4M
    if (posX > 0)
414
15.7M
    {
415
15.7M
      rightPixel = data[-1];
416
15.7M
    }
417
17.4M
    if (posY > 0)
418
14.3M
    {
419
14.3M
      belowPixel = data[-(int)m_width];
420
14.3M
    }
421
17.4M
  }
422
423
  int deriveModCoeff(int rightPixel, int belowPixel, int absCoeff, int bdpcm = 0)
424
8.76M
  {
425
8.76M
    if (absCoeff == 0)
426
4.59M
      return 0;
427
428
4.17M
    int pred1, absBelow = abs(belowPixel), absRight = abs(rightPixel);
429
430
4.17M
    int absCoeffMod = absCoeff;
431
432
4.17M
    if (bdpcm == 0)
433
67.7k
    {
434
67.7k
      pred1 = std::max(absBelow, absRight);
435
436
67.7k
      if (absCoeff == pred1)
437
0
      {
438
0
        absCoeffMod = 1;
439
0
      }
440
67.7k
      else
441
67.7k
      {
442
67.7k
        absCoeffMod = absCoeff < pred1 ? absCoeff + 1 : absCoeff;
443
67.7k
      }
444
67.7k
    }
445
446
4.17M
    return(absCoeffMod);
447
8.76M
  }
448
449
  int decDeriveModCoeff(int rightPixel, int belowPixel, int absCoeff)
450
0
  {
451
0
    if (absCoeff == 0)
452
0
      return 0;
453
0
454
0
    int pred1, absBelow = abs(belowPixel), absRight = abs(rightPixel);
455
0
    pred1 = std::max(absBelow, absRight);
456
0
457
0
    int absCoeffMod;
458
0
459
0
    if (absCoeff == 1 && pred1 > 0)
460
0
    {
461
0
      absCoeffMod = pred1;
462
0
    }
463
0
    else
464
0
    {
465
0
      absCoeffMod = absCoeff - (absCoeff <= pred1);
466
0
    }
467
0
    return(absCoeffMod);
468
0
  }
469
470
  //unsigned templateAbsSumTS( int scanPos, const TCoeffSig* coeff )
471
  //{
472
  //  return 1;
473
  //}
474
475
  int                       remRegBins;
476
477
private:
478
  struct SizeInfo;
479
  struct SizeInfoTable;
480
  static const SizeInfoTable s_sizeInfo;
481
482
  CoeffCodingContext( const TransformUnit& tu, ComponentID component, bool signHide, bool bdpcm, CtxTpl* tplBuf, int scanPosLast, const SizeInfo& size );
483
484
  // constant
485
  const ComponentID         m_compID;
486
  const ChannelType         m_chType;
487
  const unsigned            m_width;
488
  const unsigned            m_height;
489
  const unsigned            m_log2CGWidth;
490
  const unsigned            m_log2CGHeight;
491
  const unsigned            m_log2CGSize;
492
  const unsigned            m_widthInGroups;
493
  const unsigned            m_heightInGroups;
494
  const unsigned            m_log2WidthInGroups;
495
  const unsigned            m_log2BlockWidth;
496
  const unsigned            m_log2BlockHeight;
497
  const unsigned            m_maxNumCoeff;
498
  const bool                m_signHiding;
499
  const int                 m_maxLog2TrDynamicRange;
500
  const ScanElement *       m_scan;
501
  const ScanElement *       m_scanCG;
502
  const CtxSet              m_CtxSetLastX;
503
  const CtxSet              m_CtxSetLastY;
504
  const unsigned            m_maxLastPosX;
505
  const unsigned            m_maxLastPosY;
506
  const int                 m_lastOffsetX;
507
  const int                 m_lastOffsetY;
508
  const int                 m_lastShiftX;
509
  const int                 m_lastShiftY;
510
  // modified
511
  int                       m_scanPosLast;
512
  int                       m_subSetId;
513
  int                       m_subSetPos;
514
  int                       m_subSetPosX;
515
  int                       m_subSetPosY;
516
  int                       m_minSubPos;
517
  int                       m_maxSubPos;
518
  int                       m_sigGroupCtxId;
519
  int                       m_tmplCpSum1;
520
  int                       m_tmplCpDiag;
521
  CtxSet                    m_sigFlagCtxSet[4];   // by state
522
  CtxSet                    m_parFlagCtxSet;
523
  CtxSet                    m_gtxFlagCtxSet[2];
524
  int                       m_sigGroupCtxIdTS;
525
  CtxSet                    m_tsSigFlagCtxSet;
526
  CtxSet                    m_tsParFlagCtxSet;
527
  CtxSet                    m_tsGtxFlagCtxSet;
528
  CtxSet                    m_tsLrg1FlagCtxSet;
529
  CtxSet                    m_tsSignFlagCtxSet;
530
  std::bitset<MLS_GRP_NUM>  m_sigCoeffGroupFlag;
531
  const bool                m_bdpcm;
532
  CtxTpl*                   m_tplBuf;
533
};
534
535
536
struct CUCtx
537
{
538
553k
  CUCtx()              : isDQPCoded(false), isChromaQpAdjCoded(false),
539
553k
                         qgStart(false)
540
553k
                         {
541
553k
                           violatesLfnstConstrained[CH_L] = false;
542
553k
                           violatesLfnstConstrained[CH_C] = false;
543
553k
                           violatesMtsCoeffConstraint      = false;
544
553k
                           mtsLastScanPos                  = false;
545
553k
                           lfnstLastScanPos                = false;
546
553k
                         }
547
18.6k
  CUCtx(int _qp)       : isDQPCoded(false), isChromaQpAdjCoded(false),
548
18.6k
                         qgStart(false),
549
18.6k
                         qp(_qp)
550
18.6k
                         {
551
18.6k
                           violatesLfnstConstrained[CH_L] = false;
552
18.6k
                           violatesLfnstConstrained[CH_C] = false;
553
18.6k
                           violatesMtsCoeffConstraint      = false;
554
18.6k
                           mtsLastScanPos                  = false;
555
18.6k
                           lfnstLastScanPos                = false;
556
18.6k
                         }
557
558
  // adds the flags residual_coding sets: other holds those it set for the same residual, counted before
559
  void addResidualFlags( const CUCtx& other )
560
16.9k
  {
561
16.9k
    lfnstLastScanPos               |= other.lfnstLastScanPos;
562
16.9k
    violatesLfnstConstrained[CH_L] |= other.violatesLfnstConstrained[CH_L];
563
16.9k
    violatesLfnstConstrained[CH_C] |= other.violatesLfnstConstrained[CH_C];
564
16.9k
    violatesMtsCoeffConstraint     |= other.violatesMtsCoeffConstraint;
565
16.9k
    mtsLastScanPos                 |= other.mtsLastScanPos;
566
16.9k
  }
567
568
  bool      isDQPCoded;
569
  bool      isChromaQpAdjCoded;
570
  bool      qgStart;
571
  bool      lfnstLastScanPos;
572
  int8_t    qp;                   // used as a previous(last) QP and for QP prediction
573
  bool      violatesLfnstConstrained[MAX_NUM_CH];
574
  bool      violatesMtsCoeffConstraint;
575
  bool      mtsLastScanPos;
576
};
577
578
struct MergeCtx
579
{
580
711k
  MergeCtx() : numValidMergeCand( 0 ), hasMergedCandList( false ) { for( unsigned i = 0; i < MRG_MAX_NUM_CANDS; i++ ) mrgTypeNeighbours[i] = MRG_TYPE_DEFAULT_N; }
581
582
  void          setMmvdMergeCandiInfo ( CodingUnit& cu, const MmvdIdx candIdx) const;
583
  void          setMergeInfo          ( CodingUnit& cu, const int     candIdx) const;
584
  void          getMmvdDeltaMv        ( const Slice& slice, const MmvdIdx candIdx, Mv deltaMv[NUM_REF_PIC_LIST_01] ) const;
585
586
  MvField       mvFieldNeighbours [MRG_MAX_NUM_CANDS][NUM_REF_PIC_LIST_01];
587
  uint8_t       BcwIdx            [MRG_MAX_NUM_CANDS];
588
  unsigned char interDirNeighbours[MRG_MAX_NUM_CANDS];
589
  MergeType     mrgTypeNeighbours [MRG_MAX_NUM_CANDS];
590
  int           numValidMergeCand;
591
  bool          hasMergedCandList;
592
593
  MvField       mmvdBaseMv        [MMVD_BASE_MV_NUM][NUM_REF_PIC_LIST_01];
594
  bool          mmvdUseAltHpelIf  [MMVD_BASE_MV_NUM];
595
  bool          useAltHpelIf      [MRG_MAX_NUM_CANDS];
596
};
597
598
struct AffineMergeCtx
599
{
600
0
  AffineMergeCtx() : numValidMergeCand( 0 ) { for ( unsigned i = 0; i < AFFINE_MRG_MAX_NUM_CANDS; i++ ) affineType[i] = AFFINEMODEL_4PARAM; }
601
602
  MvField       mvFieldNeighbours [AFFINE_MRG_MAX_NUM_CANDS][NUM_REF_PIC_LIST_01][3];
603
  unsigned char interDirNeighbours[AFFINE_MRG_MAX_NUM_CANDS];
604
  EAffineModel  affineType        [AFFINE_MRG_MAX_NUM_CANDS];
605
  uint8_t       BcwIdx            [AFFINE_MRG_MAX_NUM_CANDS];
606
  int           numValidMergeCand;
607
  int           maxNumMergeCand;
608
609
  MergeType     mergeType[AFFINE_MRG_MAX_NUM_CANDS];
610
  MotionBuf     subPuMvpMiBuf;
611
};
612
613
614
struct DeriveCtx
615
{
616
  const CodingUnit* cuRestrictedLeft[MAX_NUM_CH];
617
  const CodingUnit* cuRestrictedAbove[MAX_NUM_CH];
618
619
  void determineNeighborCus       ( const CodingStructure& cs, const UnitArea& ua, const ChannelType ch, const TreeType treeType );
620
621
4.38M
  inline static unsigned CtxQtCbf ( const ComponentID compID, const bool prevCbf = false, const int ispIdx = 0 )   { return ( ispIdx && isLuma( compID ) ) ? (2 + (int)prevCbf) : (((compID == COMP_Cr) && prevCbf) ? 1 : 0); }
622
  void     CtxSplit               ( const Partitioner& partitioner, unsigned& ctxSpl, unsigned& ctxQt, unsigned& ctxHv, unsigned& ctxHorBt, unsigned& ctxVerBt, const bool canSplit[6] ) const;
623
  unsigned CtxMipFlag             ( const CodingUnit& cu ) const;
624
0
  unsigned CtxInterDir            ( const CodingUnit& cu ) const { return ( 7 - ((Log2(cu.lumaSize().area()) + 1) >> 1) ); }
625
626
  unsigned CtxModeConsFlag() const
627
0
  {
628
0
    const CodingUnit* cuLeft  = cuRestrictedLeft[CH_L]; const CodingUnit* cuAbove = cuRestrictedAbove[CH_L];
629
0
    return ((cuAbove && cuAbove->predMode == MODE_INTRA) || (cuLeft && cuLeft->predMode == MODE_INTRA)) ? 1 : 0;
630
0
  }
631
632
  unsigned CtxAffineFlag() const
633
0
  {
634
0
    const CodingUnit *cuLeft = cuRestrictedLeft[CH_L]; const CodingUnit *cuAbove = cuRestrictedAbove[CH_L];
635
0
    return (( cuLeft && cuLeft->affine ) ? 1 : 0) + (( cuAbove && cuAbove->affine ) ? 1 : 0);
636
0
  }
637
638
  unsigned CtxSkipFlag() const
639
113k
  {
640
113k
    const CodingUnit *cuLeft = cuRestrictedLeft[CH_L]; const CodingUnit *cuAbove = cuRestrictedAbove[CH_L];
641
113k
    return (( cuLeft && cuLeft->skip ) ? 1 : 0) + (( cuAbove && cuAbove->skip ) ? 1 : 0);
642
113k
  }
643
644
  unsigned CtxPredModeFlag() const
645
0
  {
646
0
    const CodingUnit *cuLeft = cuRestrictedLeft[CH_L]; const CodingUnit *cuAbove = cuRestrictedAbove[CH_L];
647
0
    return ((cuAbove && cuAbove->predMode == MODE_INTRA) || (cuLeft && cuLeft->predMode == MODE_INTRA)) ? 1 : 0;
648
0
  }
649
650
  unsigned CtxIBCFlag(const CodingUnit& cu) const
651
319k
  {
652
319k
    const CodingUnit *cuLeft = cuRestrictedLeft[cu.chType]; const CodingUnit *cuAbove = cuRestrictedAbove[cu.chType];
653
319k
    return ((cuLeft && cuLeft->predMode == MODE_IBC) ? 1 : 0) + ((cuAbove && cuAbove->predMode == MODE_IBC) ? 1 : 0);
654
319k
  }
655
656
};
657
658
} // namespace vvenc
659
660
//! \}
661