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/DepQuantDecide.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
#pragma once
43
44
// The decisions of the dependent quantization trellis for one coefficient, and the loop over a block's coefficients. Only
45
// DepQuant.cpp, x86/DepQuantX86.h and arm/neon/DepQuant_neon.cpp include this: DepQuant.cpp instantiates the templates
46
// with its function pointers (the scalar kernels, and the scaling-list path of every level), every SIMD translation unit
47
// with its own kernels, which then inline. All functions here are templates or have internal linkage, so the copies built
48
// with different instruction sets stay apart.
49
50
#include "DepQuant.h"
51
52
#include <cstring>
53
54
//! \ingroup CommonLib
55
//! \{
56
57
namespace vvenc {
58
59
namespace DQIntern
60
{
61
  static const Decisions startDec[2] =
62
  {
63
    Decisions
64
    {
65
      { rdCostInit >> 2, rdCostInit >> 2, rdCostInit >> 2, rdCostInit >> 2 },
66
      { -1, -1, -1, -1 },
67
      { -2, -2, -2, -2 },
68
    },
69
    Decisions
70
    {
71
      { rdCostInit >> 2, rdCostInit >> 2, rdCostInit >> 2, rdCostInit >> 2 },
72
      { 0, 0, 0, 0 },
73
      { 4, 5, 6, 7 },
74
    }
75
  };
76
77
  template<bool rrgEnsured = false>
78
  static inline void checkRdCosts( const int stateId, const DQIntern::ScanPosType spt, const DQIntern::PQData& pqDataA, const DQIntern::PQData& pqDataB, DQIntern::Decisions& decisions, int idxAZ, int idxB, const DQIntern::StateMem& state )
79
26.9M
  {
80
26.9M
    const int32_t* goRiceTab = DQIntern::g_goRiceBits[state.m_goRicePar[stateId]];
81
26.9M
    int64_t         rdCostA = state.rdCost[stateId] + pqDataA.deltaDist;
82
26.9M
    int64_t         rdCostB = state.rdCost[stateId] + pqDataB.deltaDist;
83
26.9M
    int64_t         rdCostZ = state.rdCost[stateId];
84
85
26.9M
    if( rrgEnsured || state.remRegBins[stateId] >= 4 )
86
26.4M
    {
87
26.4M
      const CoeffFracBits& cffBits = state.m_gtxFracBitsArray[state.ctx.cff[stateId]];
88
26.4M
      const BinFracBits    sigBits = state.m_sigFracBitsArray[stateId][state.ctx.sig[stateId]];
89
90
26.4M
      if( pqDataA.absLevel < 4 )
91
5.72M
        rdCostA += cffBits.bits[pqDataA.absLevel];
92
20.7M
      else
93
20.7M
      {
94
20.7M
        const unsigned value = ( pqDataA.absLevel - 4 ) >> 1;
95
20.7M
        rdCostA += cffBits.bits[pqDataA.absLevel - ( value << 1 )] + goRiceTab[std::min<unsigned>( value, RICEMAX - 1 )];
96
20.7M
      }
97
98
26.4M
      if( pqDataB.absLevel < 4 )
99
7.36M
        rdCostB += cffBits.bits[pqDataB.absLevel];
100
19.1M
      else
101
19.1M
      {
102
19.1M
        const unsigned value = ( pqDataB.absLevel - 4 ) >> 1;
103
19.1M
        rdCostB += cffBits.bits[pqDataB.absLevel - ( value << 1 )] + goRiceTab[std::min<unsigned>( value, RICEMAX - 1 )];
104
19.1M
      }
105
106
26.4M
      if( spt == SCAN_ISCSBB )
107
26.3M
      {
108
26.3M
        rdCostA += sigBits.intBits[1];
109
26.3M
        rdCostB += sigBits.intBits[1];
110
26.3M
        rdCostZ += sigBits.intBits[0];
111
26.3M
      }
112
79.8k
      else if( spt == SCAN_SOCSBB )
113
13.8k
      {
114
13.8k
        rdCostA += state.sbbBits1[stateId] + sigBits.intBits[1];
115
13.8k
        rdCostB += state.sbbBits1[stateId] + sigBits.intBits[1];
116
13.8k
        rdCostZ += state.sbbBits1[stateId] + sigBits.intBits[0];
117
13.8k
      }
118
66.0k
      else if( state.numSig[stateId] )
119
65.1k
      {
120
65.1k
        rdCostA += sigBits.intBits[1];
121
65.1k
        rdCostB += sigBits.intBits[1];
122
65.1k
        rdCostZ += sigBits.intBits[0];
123
65.1k
      }
124
835
      else
125
835
      {
126
835
        rdCostZ = rdCostInit;
127
835
      }
128
26.4M
    }
129
504k
    else
130
504k
    {
131
504k
      rdCostA += ( 1 << SCALE_BITS ) + goRiceTab[pqDataA.absLevel <= state.m_goRiceZero[stateId] ? pqDataA.absLevel - 1 : std::min<int>( pqDataA.absLevel, RICEMAX - 1 )];
132
504k
      rdCostB += ( 1 << SCALE_BITS ) + goRiceTab[pqDataB.absLevel <= state.m_goRiceZero[stateId] ? pqDataB.absLevel - 1 : std::min<int>( pqDataB.absLevel, RICEMAX - 1 )];
133
504k
      rdCostZ += goRiceTab[state.m_goRiceZero[stateId]];
134
504k
    }
135
136
26.9M
    if( rdCostA < rdCostZ && rdCostA < decisions.rdCost[idxAZ] )
137
19.0M
    {
138
19.0M
      decisions.rdCost[idxAZ] = rdCostA;
139
19.0M
      decisions.absLevel[idxAZ] = pqDataA.absLevel;
140
19.0M
      decisions.prevId[idxAZ] = stateId;
141
19.0M
    }
142
7.93M
    else if( rdCostZ < decisions.rdCost[idxAZ] )
143
359k
    {
144
359k
      decisions.rdCost[idxAZ] = rdCostZ;
145
359k
      decisions.absLevel[idxAZ] = 0;
146
359k
      decisions.prevId[idxAZ] = stateId;
147
359k
    }
148
149
26.9M
    if( rdCostB < decisions.rdCost[idxB] )
150
19.3M
    {
151
19.3M
      decisions.rdCost[idxB] = rdCostB;
152
19.3M
      decisions.absLevel[idxB] = pqDataB.absLevel;
153
19.3M
      decisions.prevId[idxB] = stateId;
154
19.3M
    }
155
26.9M
  }
DepQuant.cpp:void vvenc::DQIntern::checkRdCosts<true>(int, vvenc::DQIntern::ScanPosType, vvenc::DQIntern::PQData const&, vvenc::DQIntern::PQData const&, vvenc::DQIntern::Decisions&, int, int, vvenc::DQIntern::StateMem const&)
Line
Count
Source
79
26.4M
  {
80
26.4M
    const int32_t* goRiceTab = DQIntern::g_goRiceBits[state.m_goRicePar[stateId]];
81
26.4M
    int64_t         rdCostA = state.rdCost[stateId] + pqDataA.deltaDist;
82
26.4M
    int64_t         rdCostB = state.rdCost[stateId] + pqDataB.deltaDist;
83
26.4M
    int64_t         rdCostZ = state.rdCost[stateId];
84
85
26.4M
    if( rrgEnsured || state.remRegBins[stateId] >= 4 )
86
26.4M
    {
87
26.4M
      const CoeffFracBits& cffBits = state.m_gtxFracBitsArray[state.ctx.cff[stateId]];
88
26.4M
      const BinFracBits    sigBits = state.m_sigFracBitsArray[stateId][state.ctx.sig[stateId]];
89
90
26.4M
      if( pqDataA.absLevel < 4 )
91
5.72M
        rdCostA += cffBits.bits[pqDataA.absLevel];
92
20.7M
      else
93
20.7M
      {
94
20.7M
        const unsigned value = ( pqDataA.absLevel - 4 ) >> 1;
95
20.7M
        rdCostA += cffBits.bits[pqDataA.absLevel - ( value << 1 )] + goRiceTab[std::min<unsigned>( value, RICEMAX - 1 )];
96
20.7M
      }
97
98
26.4M
      if( pqDataB.absLevel < 4 )
99
7.36M
        rdCostB += cffBits.bits[pqDataB.absLevel];
100
19.0M
      else
101
19.0M
      {
102
19.0M
        const unsigned value = ( pqDataB.absLevel - 4 ) >> 1;
103
19.0M
        rdCostB += cffBits.bits[pqDataB.absLevel - ( value << 1 )] + goRiceTab[std::min<unsigned>( value, RICEMAX - 1 )];
104
19.0M
      }
105
106
26.4M
      if( spt == SCAN_ISCSBB )
107
26.3M
      {
108
26.3M
        rdCostA += sigBits.intBits[1];
109
26.3M
        rdCostB += sigBits.intBits[1];
110
26.3M
        rdCostZ += sigBits.intBits[0];
111
26.3M
      }
112
79.8k
      else if( spt == SCAN_SOCSBB )
113
13.8k
      {
114
13.8k
        rdCostA += state.sbbBits1[stateId] + sigBits.intBits[1];
115
13.8k
        rdCostB += state.sbbBits1[stateId] + sigBits.intBits[1];
116
13.8k
        rdCostZ += state.sbbBits1[stateId] + sigBits.intBits[0];
117
13.8k
      }
118
65.9k
      else if( state.numSig[stateId] )
119
65.1k
      {
120
65.1k
        rdCostA += sigBits.intBits[1];
121
65.1k
        rdCostB += sigBits.intBits[1];
122
65.1k
        rdCostZ += sigBits.intBits[0];
123
65.1k
      }
124
835
      else
125
835
      {
126
835
        rdCostZ = rdCostInit;
127
835
      }
128
26.4M
    }
129
0
    else
130
0
    {
131
0
      rdCostA += ( 1 << SCALE_BITS ) + goRiceTab[pqDataA.absLevel <= state.m_goRiceZero[stateId] ? pqDataA.absLevel - 1 : std::min<int>( pqDataA.absLevel, RICEMAX - 1 )];
132
0
      rdCostB += ( 1 << SCALE_BITS ) + goRiceTab[pqDataB.absLevel <= state.m_goRiceZero[stateId] ? pqDataB.absLevel - 1 : std::min<int>( pqDataB.absLevel, RICEMAX - 1 )];
133
0
      rdCostZ += goRiceTab[state.m_goRiceZero[stateId]];
134
0
    }
135
136
26.4M
    if( rdCostA < rdCostZ && rdCostA < decisions.rdCost[idxAZ] )
137
18.6M
    {
138
18.6M
      decisions.rdCost[idxAZ] = rdCostA;
139
18.6M
      decisions.absLevel[idxAZ] = pqDataA.absLevel;
140
18.6M
      decisions.prevId[idxAZ] = stateId;
141
18.6M
    }
142
7.80M
    else if( rdCostZ < decisions.rdCost[idxAZ] )
143
359k
    {
144
359k
      decisions.rdCost[idxAZ] = rdCostZ;
145
359k
      decisions.absLevel[idxAZ] = 0;
146
359k
      decisions.prevId[idxAZ] = stateId;
147
359k
    }
148
149
26.4M
    if( rdCostB < decisions.rdCost[idxB] )
150
19.0M
    {
151
19.0M
      decisions.rdCost[idxB] = rdCostB;
152
19.0M
      decisions.absLevel[idxB] = pqDataB.absLevel;
153
19.0M
      decisions.prevId[idxB] = stateId;
154
19.0M
    }
155
26.4M
  }
DepQuant.cpp:void vvenc::DQIntern::checkRdCosts<false>(int, vvenc::DQIntern::ScanPosType, vvenc::DQIntern::PQData const&, vvenc::DQIntern::PQData const&, vvenc::DQIntern::Decisions&, int, int, vvenc::DQIntern::StateMem const&)
Line
Count
Source
79
524k
  {
80
524k
    const int32_t* goRiceTab = DQIntern::g_goRiceBits[state.m_goRicePar[stateId]];
81
524k
    int64_t         rdCostA = state.rdCost[stateId] + pqDataA.deltaDist;
82
524k
    int64_t         rdCostB = state.rdCost[stateId] + pqDataB.deltaDist;
83
524k
    int64_t         rdCostZ = state.rdCost[stateId];
84
85
524k
    if( rrgEnsured || state.remRegBins[stateId] >= 4 )
86
19.9k
    {
87
19.9k
      const CoeffFracBits& cffBits = state.m_gtxFracBitsArray[state.ctx.cff[stateId]];
88
19.9k
      const BinFracBits    sigBits = state.m_sigFracBitsArray[stateId][state.ctx.sig[stateId]];
89
90
19.9k
      if( pqDataA.absLevel < 4 )
91
1.70k
        rdCostA += cffBits.bits[pqDataA.absLevel];
92
18.2k
      else
93
18.2k
      {
94
18.2k
        const unsigned value = ( pqDataA.absLevel - 4 ) >> 1;
95
18.2k
        rdCostA += cffBits.bits[pqDataA.absLevel - ( value << 1 )] + goRiceTab[std::min<unsigned>( value, RICEMAX - 1 )];
96
18.2k
      }
97
98
19.9k
      if( pqDataB.absLevel < 4 )
99
1.97k
        rdCostB += cffBits.bits[pqDataB.absLevel];
100
18.0k
      else
101
18.0k
      {
102
18.0k
        const unsigned value = ( pqDataB.absLevel - 4 ) >> 1;
103
18.0k
        rdCostB += cffBits.bits[pqDataB.absLevel - ( value << 1 )] + goRiceTab[std::min<unsigned>( value, RICEMAX - 1 )];
104
18.0k
      }
105
106
19.9k
      if( spt == SCAN_ISCSBB )
107
19.9k
      {
108
19.9k
        rdCostA += sigBits.intBits[1];
109
19.9k
        rdCostB += sigBits.intBits[1];
110
19.9k
        rdCostZ += sigBits.intBits[0];
111
19.9k
      }
112
59
      else if( spt == SCAN_SOCSBB )
113
15
      {
114
15
        rdCostA += state.sbbBits1[stateId] + sigBits.intBits[1];
115
15
        rdCostB += state.sbbBits1[stateId] + sigBits.intBits[1];
116
15
        rdCostZ += state.sbbBits1[stateId] + sigBits.intBits[0];
117
15
      }
118
44
      else if( state.numSig[stateId] )
119
44
      {
120
44
        rdCostA += sigBits.intBits[1];
121
44
        rdCostB += sigBits.intBits[1];
122
44
        rdCostZ += sigBits.intBits[0];
123
44
      }
124
0
      else
125
0
      {
126
0
        rdCostZ = rdCostInit;
127
0
      }
128
19.9k
    }
129
504k
    else
130
504k
    {
131
504k
      rdCostA += ( 1 << SCALE_BITS ) + goRiceTab[pqDataA.absLevel <= state.m_goRiceZero[stateId] ? pqDataA.absLevel - 1 : std::min<int>( pqDataA.absLevel, RICEMAX - 1 )];
132
504k
      rdCostB += ( 1 << SCALE_BITS ) + goRiceTab[pqDataB.absLevel <= state.m_goRiceZero[stateId] ? pqDataB.absLevel - 1 : std::min<int>( pqDataB.absLevel, RICEMAX - 1 )];
133
504k
      rdCostZ += goRiceTab[state.m_goRiceZero[stateId]];
134
504k
    }
135
136
524k
    if( rdCostA < rdCostZ && rdCostA < decisions.rdCost[idxAZ] )
137
392k
    {
138
392k
      decisions.rdCost[idxAZ] = rdCostA;
139
392k
      decisions.absLevel[idxAZ] = pqDataA.absLevel;
140
392k
      decisions.prevId[idxAZ] = stateId;
141
392k
    }
142
131k
    else if( rdCostZ < decisions.rdCost[idxAZ] )
143
540
    {
144
540
      decisions.rdCost[idxAZ] = rdCostZ;
145
540
      decisions.absLevel[idxAZ] = 0;
146
540
      decisions.prevId[idxAZ] = stateId;
147
540
    }
148
149
524k
    if( rdCostB < decisions.rdCost[idxB] )
150
387k
    {
151
387k
      decisions.rdCost[idxB] = rdCostB;
152
387k
      decisions.absLevel[idxB] = pqDataB.absLevel;
153
387k
      decisions.prevId[idxB] = stateId;
154
387k
    }
155
524k
  }
156
157
  template<bool rrgEnsured = false>
158
  static inline void checkRdCostsOdd1( const int stateId, const ScanPosType spt, const int64_t deltaDist, Decisions& decisions, int idxA, int idxZ, const StateMem& state )
159
4.60M
  {
160
4.60M
    int64_t         rdCostA = state.rdCost[stateId] + deltaDist;
161
4.60M
    int64_t         rdCostZ = state.rdCost[stateId];
162
163
4.60M
    if( rrgEnsured || state.remRegBins[stateId] >= 4 )
164
4.60M
    {
165
4.60M
      const BinFracBits sigBits = state.m_sigFracBitsArray[stateId][state.ctx.sig[stateId]];
166
167
4.60M
      rdCostA += state.cffBits1[state.ctx.cff[stateId]];
168
169
4.60M
      if( spt == SCAN_ISCSBB )
170
4.55M
      {
171
4.55M
        rdCostA += sigBits.intBits[1];
172
4.55M
        rdCostZ += sigBits.intBits[0];
173
4.55M
      }
174
48.1k
      else if( spt == SCAN_SOCSBB )
175
45.2k
      {
176
45.2k
        rdCostA += state.sbbBits1[stateId] + sigBits.intBits[1];
177
45.2k
        rdCostZ += state.sbbBits1[stateId] + sigBits.intBits[0];
178
45.2k
      }
179
2.90k
      else if( state.numSig[stateId] )
180
2.25k
      {
181
2.25k
        rdCostA += sigBits.intBits[1];
182
2.25k
        rdCostZ += sigBits.intBits[0];
183
2.25k
      }
184
656
      else
185
656
      {
186
656
        rdCostZ = rdCostInit;
187
656
      }
188
4.60M
    }
189
2.06k
    else
190
2.06k
    {
191
2.06k
      const int32_t* goRiceTab = g_goRiceBits[state.m_goRicePar[stateId]];
192
193
2.06k
      rdCostA += ( 1 << SCALE_BITS ) + goRiceTab[0];
194
2.06k
      rdCostZ += goRiceTab[state.m_goRiceZero[stateId]];
195
2.06k
    }
196
197
4.60M
    if( rdCostA < decisions.rdCost[idxA] )
198
3.00M
    {
199
3.00M
      decisions.rdCost[idxA] = rdCostA;
200
3.00M
      decisions.absLevel[idxA] = 1;
201
3.00M
      decisions.prevId[idxA] = stateId;
202
3.00M
    }
203
204
4.60M
    if( rdCostZ < decisions.rdCost[idxZ] )
205
3.39M
    {
206
3.39M
      decisions.rdCost[idxZ] = rdCostZ;
207
3.39M
      decisions.absLevel[idxZ] = 0;
208
3.39M
      decisions.prevId[idxZ] = stateId;
209
3.39M
    }
210
4.60M
  }
DepQuant.cpp:void vvenc::DQIntern::checkRdCostsOdd1<true>(int, vvenc::DQIntern::ScanPosType, long, vvenc::DQIntern::Decisions&, int, int, vvenc::DQIntern::StateMem const&)
Line
Count
Source
159
4.60M
  {
160
4.60M
    int64_t         rdCostA = state.rdCost[stateId] + deltaDist;
161
4.60M
    int64_t         rdCostZ = state.rdCost[stateId];
162
163
4.60M
    if( rrgEnsured || state.remRegBins[stateId] >= 4 )
164
4.60M
    {
165
4.60M
      const BinFracBits sigBits = state.m_sigFracBitsArray[stateId][state.ctx.sig[stateId]];
166
167
4.60M
      rdCostA += state.cffBits1[state.ctx.cff[stateId]];
168
169
4.60M
      if( spt == SCAN_ISCSBB )
170
4.55M
      {
171
4.55M
        rdCostA += sigBits.intBits[1];
172
4.55M
        rdCostZ += sigBits.intBits[0];
173
4.55M
      }
174
47.9k
      else if( spt == SCAN_SOCSBB )
175
45.0k
      {
176
45.0k
        rdCostA += state.sbbBits1[stateId] + sigBits.intBits[1];
177
45.0k
        rdCostZ += state.sbbBits1[stateId] + sigBits.intBits[0];
178
45.0k
      }
179
2.90k
      else if( state.numSig[stateId] )
180
2.25k
      {
181
2.25k
        rdCostA += sigBits.intBits[1];
182
2.25k
        rdCostZ += sigBits.intBits[0];
183
2.25k
      }
184
656
      else
185
656
      {
186
656
        rdCostZ = rdCostInit;
187
656
      }
188
4.60M
    }
189
0
    else
190
0
    {
191
0
      const int32_t* goRiceTab = g_goRiceBits[state.m_goRicePar[stateId]];
192
193
0
      rdCostA += ( 1 << SCALE_BITS ) + goRiceTab[0];
194
0
      rdCostZ += goRiceTab[state.m_goRiceZero[stateId]];
195
0
    }
196
197
4.60M
    if( rdCostA < decisions.rdCost[idxA] )
198
3.00M
    {
199
3.00M
      decisions.rdCost[idxA] = rdCostA;
200
3.00M
      decisions.absLevel[idxA] = 1;
201
3.00M
      decisions.prevId[idxA] = stateId;
202
3.00M
    }
203
204
4.60M
    if( rdCostZ < decisions.rdCost[idxZ] )
205
3.39M
    {
206
3.39M
      decisions.rdCost[idxZ] = rdCostZ;
207
3.39M
      decisions.absLevel[idxZ] = 0;
208
3.39M
      decisions.prevId[idxZ] = stateId;
209
3.39M
    }
210
4.60M
  }
DepQuant.cpp:void vvenc::DQIntern::checkRdCostsOdd1<false>(int, vvenc::DQIntern::ScanPosType, long, vvenc::DQIntern::Decisions&, int, int, vvenc::DQIntern::StateMem const&)
Line
Count
Source
159
2.51k
  {
160
2.51k
    int64_t         rdCostA = state.rdCost[stateId] + deltaDist;
161
2.51k
    int64_t         rdCostZ = state.rdCost[stateId];
162
163
2.51k
    if( rrgEnsured || state.remRegBins[stateId] >= 4 )
164
450
    {
165
450
      const BinFracBits sigBits = state.m_sigFracBitsArray[stateId][state.ctx.sig[stateId]];
166
167
450
      rdCostA += state.cffBits1[state.ctx.cff[stateId]];
168
169
450
      if( spt == SCAN_ISCSBB )
170
285
      {
171
285
        rdCostA += sigBits.intBits[1];
172
285
        rdCostZ += sigBits.intBits[0];
173
285
      }
174
165
      else if( spt == SCAN_SOCSBB )
175
165
      {
176
165
        rdCostA += state.sbbBits1[stateId] + sigBits.intBits[1];
177
165
        rdCostZ += state.sbbBits1[stateId] + sigBits.intBits[0];
178
165
      }
179
0
      else if( state.numSig[stateId] )
180
0
      {
181
0
        rdCostA += sigBits.intBits[1];
182
0
        rdCostZ += sigBits.intBits[0];
183
0
      }
184
0
      else
185
0
      {
186
0
        rdCostZ = rdCostInit;
187
0
      }
188
450
    }
189
2.06k
    else
190
2.06k
    {
191
2.06k
      const int32_t* goRiceTab = g_goRiceBits[state.m_goRicePar[stateId]];
192
193
2.06k
      rdCostA += ( 1 << SCALE_BITS ) + goRiceTab[0];
194
2.06k
      rdCostZ += goRiceTab[state.m_goRiceZero[stateId]];
195
2.06k
    }
196
197
2.51k
    if( rdCostA < decisions.rdCost[idxA] )
198
1.44k
    {
199
1.44k
      decisions.rdCost[idxA] = rdCostA;
200
1.44k
      decisions.absLevel[idxA] = 1;
201
1.44k
      decisions.prevId[idxA] = stateId;
202
1.44k
    }
203
204
2.51k
    if( rdCostZ < decisions.rdCost[idxZ] )
205
2.23k
    {
206
2.23k
      decisions.rdCost[idxZ] = rdCostZ;
207
2.23k
      decisions.absLevel[idxZ] = 0;
208
2.23k
      decisions.prevId[idxZ] = stateId;
209
2.23k
    }
210
2.51k
  }
211
212
  static inline void checkRdCostStart( int32_t lastOffset, const PQData& pqData, Decisions& decisions, int idx, const StateMem& state )
213
16.4M
  {
214
16.4M
    const CoeffFracBits& cffBits = state.m_gtxFracBitsArray[0];
215
216
16.4M
    int64_t rdCost = pqData.deltaDist + lastOffset;
217
16.4M
    if( pqData.absLevel < 4 )
218
5.41M
    {
219
5.41M
      rdCost += cffBits.bits[pqData.absLevel];
220
5.41M
    }
221
11.0M
    else
222
11.0M
    {
223
11.0M
      const unsigned value = ( pqData.absLevel - 4 ) >> 1;
224
11.0M
      rdCost += cffBits.bits[pqData.absLevel - ( value << 1 )] + g_goRiceBits[0][value < RICEMAX ? value : RICEMAX - 1];
225
11.0M
    }
226
227
16.4M
    if( rdCost < decisions.rdCost[idx] )
228
2.15M
    {
229
2.15M
      decisions.rdCost[idx]   = rdCost;
230
2.15M
      decisions.absLevel[idx] = pqData.absLevel;
231
2.15M
      decisions.prevId[idx]   = -1;
232
2.15M
    }
233
16.4M
  }
234
235
  static inline void checkRdCostSkipSbb( const int stateId, Decisions& decisions, int idx, const StateMem& state )
236
84.8k
  {
237
84.8k
    int64_t rdCost = state.rdCost[stateId] + state.sbbBits0[stateId];
238
84.8k
    if( rdCost < decisions.rdCost[idx] )
239
1.10k
    {
240
1.10k
      decisions.rdCost[idx]   = rdCost;
241
1.10k
      decisions.absLevel[idx] = 0;
242
1.10k
      decisions.prevId[idx]   = 4 | stateId;
243
1.10k
    }
244
84.8k
  }
245
246
  static inline void checkRdCostSkipSbbZeroOut( const int stateId, Decisions& decisions, int idx, const StateMem& state )
247
47.5k
  {
248
47.5k
    int64_t rdCost          = state.rdCost[stateId] + state.sbbBits0[stateId];
249
47.5k
    decisions.rdCost[idx]   = rdCost;
250
47.5k
    decisions.absLevel[idx] = 0;
251
47.5k
    decisions.prevId[idx]   = 4 | stateId;
252
47.5k
  }
253
254
  static inline void setRiceParam( const int stateId, const ScanInfo& scanInfo, StateMem& state, bool ge4 )
255
21.2M
  {
256
21.2M
    if( state.remRegBins[stateId] < 4 || ge4 )
257
21.2M
    {
258
21.2M
      TCoeff  sumAbs = state.sum1st[scanInfo.insidePos][stateId];
259
21.2M
      int sumSub     = state.remRegBins[stateId] < 4 ? 0 : 4 * 5;
260
21.2M
      int sumAll     = std::max( std::min( 31, ( int ) sumAbs - sumSub ), 0 );
261
21.2M
      state.m_goRicePar[stateId]
262
21.2M
                     = g_auiGoRiceParsCoeff[sumAll];
263
264
21.2M
      if( state.remRegBins[stateId] < 4 )
265
506k
      {
266
506k
        state.m_goRiceZero[stateId] = g_auiGoRicePosCoeff0( stateId, state.m_goRicePar[stateId] );
267
506k
      }
268
21.2M
    }
269
21.2M
  }
270
} // namespace DQIntern
271
272
template<class Kernels>
273
inline void DepQuant::xDecide( const DQIntern::ScanInfo& scanInfo, const TCoeff absCoeff, const int lastOffset, DQIntern::Decisions& decisions, bool zeroOut, int quantCoeff )
274
8.84M
{
275
8.84M
  using namespace DQIntern;
276
277
  // Kernels::checkAllRdCosts and checkAllRdCostsOdd1 set all of decisions themselves, and this runs for every coefficient:
278
  // only the paths that compare against it start from startDec
279
8.84M
  StateMem& skip = m_state_skip;
280
281
8.84M
  if( zeroOut )
282
11.8k
  {
283
11.8k
    ::memcpy( &decisions, startDec, sizeof( Decisions ) );
284
11.8k
    if( scanInfo.spt == SCAN_EOCSBB )
285
11.8k
    {
286
11.8k
      checkRdCostSkipSbbZeroOut( 0, decisions, 0, skip );
287
11.8k
      checkRdCostSkipSbbZeroOut( 1, decisions, 1, skip );
288
11.8k
      checkRdCostSkipSbbZeroOut( 2, decisions, 2, skip );
289
11.8k
      checkRdCostSkipSbbZeroOut( 3, decisions, 3, skip );
290
11.8k
    }
291
11.8k
    return;
292
11.8k
  }
293
294
8.83M
  StateMem& prev = m_state_curr;
295
296
  /// start inline prequant
297
8.83M
  int64_t scaledOrg = int64_t( absCoeff ) * quantCoeff;
298
8.83M
  TCoeff  qIdx      = TCoeff( ( scaledOrg + m_quant.m_QAdd ) >> m_quant.m_QShift );
299
300
8.83M
  if( qIdx < 0 )
301
1.24M
  {
302
1.24M
    int64_t scaledAdd = m_quant.m_DistStepAdd - scaledOrg * m_quant.m_DistOrgFact;
303
1.24M
    int64_t pq_a_dist = ( ( scaledAdd + 0 * m_quant.m_DistStepAdd ) * 1 + m_quant.m_DistAdd ) >> m_quant.m_DistShift;
304
1.24M
    int64_t pq_b_dist = ( ( scaledAdd + 1 * m_quant.m_DistStepAdd ) * 2 + m_quant.m_DistAdd ) >> m_quant.m_DistShift;
305
    /// stop inline prequant
306
307
1.24M
    if( prev.anyRemRegBinsLt4 )
308
98.7k
    {
309
98.7k
      ::memcpy( &decisions, startDec, sizeof( Decisions ) );
310
311
      // at a block's first position all states still hold rdCostInit, so none of them can beat startDec's cost
312
98.7k
      if( scanInfo.scanIdx != m_firstScanIdx )
313
628
      {
314
628
        setRiceParam( 0, scanInfo, prev, false );
315
628
        checkRdCostsOdd1( 0, scanInfo.spt, pq_b_dist, decisions, 2, 0, prev );
316
317
628
        setRiceParam( 1, scanInfo, prev, false );
318
628
        checkRdCostsOdd1( 1, scanInfo.spt, pq_b_dist, decisions, 0, 2, prev );
319
320
628
        setRiceParam( 2, scanInfo, prev, false );
321
628
        checkRdCostsOdd1( 2, scanInfo.spt, pq_a_dist, decisions, 3, 1, prev );
322
323
628
        setRiceParam( 3, scanInfo, prev, false );
324
628
        checkRdCostsOdd1( 3, scanInfo.spt, pq_a_dist, decisions, 1, 3, prev );
325
628
      }
326
98.7k
    }
327
1.15M
    else
328
1.15M
    {
329
      // has to be called as a first check, assumes no decision has been made yet
330
1.15M
      Kernels::checkAllRdCostsOdd1( *this, scanInfo.spt, pq_a_dist, pq_b_dist, decisions, prev );
331
1.15M
    }
332
333
1.24M
    checkRdCostStart( lastOffset, PQData{ 1, pq_b_dist }, decisions, 2, prev );
334
1.24M
  }
335
7.58M
  else
336
7.58M
  {
337
    /// start inline prequant
338
    // at least 1. qIdx is not negative here, and how often it is 0 is hard to predict: no branch for it
339
7.58M
    qIdx  = std::min<TCoeff>( m_quant.m_maxQIdx, qIdx );
340
7.58M
    qIdx += TCoeff( qIdx == 0 );
341
7.58M
    int64_t scaledAdd = qIdx * m_quant.m_DistStepAdd - scaledOrg * m_quant.m_DistOrgFact;
342
343
7.58M
    PQData  pqData[4];
344
345
7.58M
    PQData& pq_a = pqData[( qIdx + 0 ) & 3];
346
7.58M
    PQData& pq_b = pqData[( qIdx + 1 ) & 3];
347
7.58M
    PQData& pq_c = pqData[( qIdx + 2 ) & 3];
348
7.58M
    PQData& pq_d = pqData[( qIdx + 3 ) & 3];
349
350
7.58M
    pq_a.deltaDist = ( ( scaledAdd + 0 * m_quant.m_DistStepAdd ) * ( qIdx + 0 ) + m_quant.m_DistAdd ) >> m_quant.m_DistShift;
351
7.58M
    pq_a.absLevel = ( qIdx + 1 ) >> 1;
352
353
7.58M
    pq_b.deltaDist = ( ( scaledAdd + 1 * m_quant.m_DistStepAdd ) * ( qIdx + 1 ) + m_quant.m_DistAdd ) >> m_quant.m_DistShift;
354
7.58M
    pq_b.absLevel = ( qIdx + 2 ) >> 1;
355
356
7.58M
    pq_c.deltaDist = ( ( scaledAdd + 2 * m_quant.m_DistStepAdd ) * ( qIdx + 2 ) + m_quant.m_DistAdd ) >> m_quant.m_DistShift;
357
7.58M
    pq_c.absLevel = ( qIdx + 3 ) >> 1;
358
359
7.58M
    pq_d.deltaDist = ( ( scaledAdd + 3 * m_quant.m_DistStepAdd ) * ( qIdx + 3 ) + m_quant.m_DistAdd ) >> m_quant.m_DistShift;
360
7.58M
    pq_d.absLevel = ( qIdx + 4 ) >> 1;
361
    /// stop inline prequant
362
363
7.58M
    bool cff02ge4 = pqData[0].absLevel >= 4/* || pqData[2].absLevel >= 4 */;
364
7.58M
    bool cff13ge4 = /* pqData[1].absLevel >= 4 || */ pqData[3].absLevel >= 4;
365
366
7.58M
    if( prev.anyRemRegBinsLt4 )
367
974k
    {
368
974k
      ::memcpy( &decisions, startDec, sizeof( Decisions ) );
369
370
      // at a block's first position all states still hold rdCostInit, so none of them can beat startDec's cost
371
974k
      if( scanInfo.scanIdx != m_firstScanIdx )
372
131k
      {
373
131k
        setRiceParam( 0, scanInfo, prev, cff02ge4 );
374
131k
        setRiceParam( 1, scanInfo, prev, cff02ge4 );
375
131k
        setRiceParam( 2, scanInfo, prev, cff13ge4 );
376
131k
        setRiceParam( 3, scanInfo, prev, cff13ge4 );
377
378
131k
        checkRdCosts( 0, scanInfo.spt, pqData[0], pqData[2], decisions, 0, 2, prev );
379
131k
        checkRdCosts( 1, scanInfo.spt, pqData[0], pqData[2], decisions, 2, 0, prev );
380
131k
        checkRdCosts( 2, scanInfo.spt, pqData[3], pqData[1], decisions, 1, 3, prev );
381
131k
        checkRdCosts( 3, scanInfo.spt, pqData[3], pqData[1], decisions, 3, 1, prev );
382
131k
      }
383
974k
    }
384
6.61M
    else if( cff02ge4 || cff13ge4 )
385
5.32M
    {
386
      // Every state has 4 or more regular bins left (and this is not a block's first position, which always takes the
387
      // branch above), so only the Rice code of a level of 4 or more differs from checkAllRdCosts. pqData[2] >= 4 only
388
      // with pqData[0] >= 4, and pqData[1] >= 4 only with pqData[3] >= 4: the states whose Rice parameter is not set
389
      // here only see levels below 4, which don't read it.
390
5.32M
      if( cff02ge4 )
391
5.03M
      {
392
5.03M
        setRiceParam( 0, scanInfo, prev, true );
393
5.03M
        setRiceParam( 1, scanInfo, prev, true );
394
5.03M
      }
395
396
5.32M
      if( cff13ge4 )
397
5.32M
      {
398
5.32M
        setRiceParam( 2, scanInfo, prev, true );
399
5.32M
        setRiceParam( 3, scanInfo, prev, true );
400
5.32M
      }
401
402
      // has to be called as a first check, assumes no decision has been made yet
403
5.32M
      Kernels::checkAllRdCostsGe4( *this, scanInfo.spt, pqData, decisions, prev );
404
5.32M
    }
405
1.28M
    else
406
1.28M
    {
407
      // has to be called as a first check, assumes no decision has been made yet
408
1.28M
      Kernels::checkAllRdCosts( *this, scanInfo.spt, pqData, decisions, prev );
409
1.28M
    }
410
411
7.58M
    checkRdCostStart( lastOffset, pqData[0], decisions, 0, prev );
412
7.58M
    checkRdCostStart( lastOffset, pqData[2], decisions, 2, prev );
413
7.58M
  }
414
415
8.83M
  if( scanInfo.spt == SCAN_EOCSBB )
416
21.2k
  {
417
21.2k
    checkRdCostSkipSbb( 0, decisions, 0, skip );
418
21.2k
    checkRdCostSkipSbb( 1, decisions, 1, skip );
419
21.2k
    checkRdCostSkipSbb( 2, decisions, 2, skip );
420
21.2k
    checkRdCostSkipSbb( 3, decisions, 3, skip );
421
21.2k
  }
422
8.83M
}
423
424
template<class Kernels>
425
inline void DepQuant::xDecideAndUpdate( const TCoeff absCoeff, const DQIntern::ScanInfo& scanInfo, bool zeroOut, int quantCoeff )
426
8.84M
{
427
8.84M
  using namespace DQIntern;
428
429
8.84M
  Decisions* decisions = &m_trellis[scanInfo.scanIdx][0];
430
431
8.84M
  xDecide<Kernels>( scanInfo, absCoeff, lastOffset( scanInfo ), *decisions, zeroOut, quantCoeff );
432
433
8.84M
  if( scanInfo.scanIdx )
434
7.90M
  {
435
7.90M
    if( scanInfo.spt == SCAN_SOCSBB )
436
17.2k
    {
437
17.2k
      xSaveSkipStates();
438
17.2k
    }
439
440
7.90M
    if( scanInfo.insidePos == 0 )
441
34.9k
    {
442
34.9k
      m_commonCtx.swap();
443
34.9k
      Kernels::updateStatesEOS( *this, scanInfo, *decisions, m_state_skip, m_state_curr, m_commonCtx );
444
34.9k
      ::memcpy( decisions + 1, decisions, sizeof( Decisions ) );
445
34.9k
    }
446
7.87M
    else if( !zeroOut )
447
7.87M
    {
448
7.87M
      Kernels::updateStates( *this, scanInfo, *decisions, m_state_curr );
449
7.87M
    }
450
7.90M
  }
451
8.84M
}
452
453
// The trellis of a block without scaling lists: the decisions and state updates of its coefficients from firstTestPos back
454
// to the first. Instantiated per set of kernels, so that a block costs one indirect call (m_populateTrellis) instead of up
455
// to three per coefficient.
456
template<class Kernels>
457
void DepQuant::xPopulateTrellis( DepQuant& dq, const DQIntern::TUParameters& tuPars, const int firstTestPos, const TCoeff* tCoeff,
458
                                 const bool zeroOut, const int effWidth, const int effHeight, const int quantCoeff )
459
941k
{
460
  // All-zero sub-blocks (as RDOQ2's zero groups after its last one, QuantRDOQ2::xRateDistOptQuantFast): a sub-block below
461
  // the one holding firstTestPos, other than the first one (DC), whose coefficients all lie below m_thresSSbb (|coeff| *
462
  // qScale below SbbSkipThr half quantization steps, as RDOQ2 uses it) takes only the skip path, sb_coded_flag 0 for
463
  // every state. It is decided like a zero-out sub-block: xDecide at its first position (insidePos 0) only, after the
464
  // copy of the states that its last position (SCAN_SOCSBB) makes. The backtracking reaches its other positions only
465
  // through that skip decision (prevId 4 | s), which reads their second rows, the constant startDec[1]; their first rows
466
  // stay unread. Not exact: a level 1 can still pay off there through the state transitions; with the limit 1.0 the full
467
  // trellis zeroes 99.7 to 99.9 % of these sub-blocks (preset fast, 1080p). 0 turns the skip off. SCAN_SOCSBB marks the
468
  // last position of every sub-block but the first and the last one; only 4x4 sub-blocks (xAllZeroLevels4x4), so not the
469
  // 2x8 / 8x2 ones of narrow chroma blocks.
470
941k
  const int  log2Sbb   = tuPars.m_log2SbbSize;
471
941k
  const int  firstSbb  = firstTestPos >> log2Sbb;
472
941k
  const bool sbb4x4    = tuPars.m_log2SbbWidth == 2 && tuPars.m_log2SbbHeight == 2;
473
941k
  const int  zeroLimit = dq.m_quant.getSSbbThreshold();
474
941k
  CHECKD( sbb4x4 && log2Sbb != 4, "a 4x4 sub-block has 16 positions" );
475
9.78M
  for( int scanIdx = firstTestPos; scanIdx >= 0; scanIdx-- )
476
8.84M
  {
477
8.84M
    const DQIntern::ScanInfo& scanInfo = tuPars.m_scanInfo[scanIdx];
478
8.84M
    const bool sbbZeroOut = zeroOut && ( scanInfo.posX >= effWidth || scanInfo.posY >= effHeight );
479
8.84M
    if( zeroLimit > 0 && scanInfo.spt == DQIntern::SCAN_SOCSBB && sbb4x4 && !sbbZeroOut && ( scanIdx >> log2Sbb ) < firstSbb
480
29.1k
        && dq.xAllZeroLevels4x4( tCoeff + ( scanInfo.posY & ~3 ) * tuPars.m_width + ( scanInfo.posX & ~3 ), tuPars.m_width, quantCoeff, zeroLimit ) )
481
11.8k
    {
482
11.8k
      CHECKD( ( scanIdx & 15 ) != 15 || scanIdx < 31, "not the last position of a sub-block above the first" );
483
11.8k
      scanIdx &= ~15;
484
11.8k
      dq.xSaveSkipStates();
485
11.8k
      dq.xDecideAndUpdate<Kernels>( 0, tuPars.m_scanInfo[scanIdx], true, quantCoeff );
486
11.8k
      continue;
487
11.8k
    }
488
8.83M
    dq.xDecideAndUpdate<Kernels>( abs( tCoeff[scanInfo.rasterPos] ), scanInfo, sbbZeroOut, quantCoeff );
489
8.83M
  }
490
941k
}
491
492
} // namespace vvenc
493
494
//! \}