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/DecSlice.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     DecSlice.cpp
44
    \brief    slice decoder class
45
*/
46
47
#include "CABACReader.h"
48
#include "DecSlice.h"
49
#include "Picture.h"
50
#include "Slice.h"
51
52
#include "CommonLib/dtrace_next.h"
53
#include "CommonLib/TimeProfiler.h"
54
#include "CommonLib/AdaptiveLoopFilter.h"
55
56
#include <vector>
57
58
59
namespace vvdec
60
{
61
62
//////////////////////////////////////////////////////////////////////
63
// Construction/Destruction
64
//////////////////////////////////////////////////////////////////////
65
66
void DecSlice::parseSlice( Slice* slice, InputBitstream* bitstream, int threadId )
67
713
{
68
713
  PROFILER_SCOPE_AND_STAGE_EXT( 1, g_timeProfiler, P_CONTROL_PARSE_DERIVE_LL, *slice->getPic()->cs, CH_L );
69
713
  const unsigned numSubstreams = slice->getNumberOfSubstreamSizes() + 1;
70
  // Table of extracted substreams.
71
713
  std::vector<std::unique_ptr<InputBitstream>> ppcSubstreams( numSubstreams );
72
1.42k
  for( unsigned idx = 0; idx < numSubstreams; idx++ )
73
713
  {
74
713
    ppcSubstreams[idx] = bitstream->extractSubstream( idx + 1 < numSubstreams ? ( slice->getSubstreamSize( idx ) << 3 ) : bitstream->getNumBitsLeft() );
75
713
  }
76
77
713
  const SPS* sps = slice->getSPS();
78
713
  Picture*   pic = slice->getPic();
79
80
  // setup coding structure
81
713
  CodingStructure& cs = *pic->cs;
82
713
  cs.chromaQpAdj      = 0;
83
84
713
  const int       startCtuTsAddr              = slice->getFirstCtuRsAddrInSlice();
85
713
  const unsigned  widthInCtus                 = cs.pcv->widthInCtus;
86
713
  const bool      wavefrontsEnabled           = cs.sps->getEntropyCodingSyncEnabledFlag();
87
713
  const bool      entryPointPresent           = cs.sps->getEntryPointsPresentFlag();
88
89
713
  AdaptiveLoopFilter::reconstructCoeffAPSs( *slice );
90
91
713
  CABACReader cabacReader;
92
713
  cabacReader.initBitstream( ppcSubstreams[0].get() );
93
713
  cabacReader.initCtxModels( *slice );
94
95
  // if first slice, finish init of the coding structure
96
713
  if( startCtuTsAddr == 0 )
97
713
  {
98
713
    cs.initStructData();
99
713
  }
100
101
  // Quantization parameter
102
713
  int prevQP[2] = { slice->getSliceQp(), slice->getSliceQp() };
103
713
  CHECK( prevQP[0] == std::numeric_limits<int>::max(), "Invalid previous QP" );
104
105
713
  DTRACE( g_trace_ctx, D_HEADER, "=========== POC: %d ===========\n", slice->getPOC() );
106
107
713
  unsigned subStrmId = 0;
108
6.08k
  for( unsigned ctuIdx = 0; ctuIdx < slice->getNumCtuInSlice(); ctuIdx++ )
109
5.43k
  {
110
5.43k
    const unsigned  ctuRsAddr       = slice->getCtuAddrInSlice(ctuIdx);
111
5.43k
    const unsigned  ctuXPosInCtus   = ctuRsAddr % widthInCtus;
112
5.43k
    const unsigned  ctuYPosInCtus   = ctuRsAddr / widthInCtus;
113
5.43k
    const unsigned  tileColIdx      = slice->getPPS()->ctuToTileCol( ctuXPosInCtus );
114
5.43k
    const unsigned  tileRowIdx      = slice->getPPS()->ctuToTileRow( ctuYPosInCtus );
115
5.43k
    const unsigned  tileXPosInCtus  = slice->getPPS()->getTileColumnBd( tileColIdx );
116
5.43k
    const unsigned  tileYPosInCtus  = slice->getPPS()->getTileRowBd( tileRowIdx );
117
5.43k
    const unsigned  tileColWidth    = slice->getPPS()->getTileColumnWidth( tileColIdx );
118
5.43k
    const unsigned  tileRowHeight   = slice->getPPS()->getTileRowHeight( tileRowIdx );
119
5.43k
    const unsigned  tileIdx         = slice->getPPS()->getTileIdx( ctuXPosInCtus, ctuYPosInCtus);
120
5.43k
    const unsigned  maxCUSize       = sps->getMaxCUWidth();
121
5.43k
    const Position pos( ctuXPosInCtus*maxCUSize, ctuYPosInCtus*maxCUSize) ;
122
5.43k
    const UnitArea ctuArea( cs.area.chromaFormat, Area( pos.x, pos.y, maxCUSize, maxCUSize ) );
123
124
    // these checks only work, since we wait for the previous slice to finish parsing
125
5.43k
    CHECK( ctuXPosInCtus > 0 && cs.getCtuData( ctuRsAddr - 1           ).slice == nullptr, "CTU left not available RS:" << ctuRsAddr - 1 );
126
5.43k
    CHECK( ctuYPosInCtus > 0 && cs.getCtuData( ctuRsAddr - widthInCtus ).slice == nullptr, "CTU above not available RS:" << ctuRsAddr - widthInCtus );
127
128
5.43k
    DTRACE_UPDATE( g_trace_ctx, std::make_pair( "ctu", ctuRsAddr ) );
129
130
5.43k
    cabacReader.initBitstream( ppcSubstreams[subStrmId].get() );
131
132
    // set up CABAC contexts' state for this CTU
133
5.43k
    if( ctuXPosInCtus == tileXPosInCtus && ctuYPosInCtus == tileYPosInCtus )
134
713
    {
135
713
      if( ctuIdx != 0 ) // if it is the first CTU, then the entropy coder has already been reset
136
0
      {
137
0
        cabacReader.initCtxModels( *slice );
138
0
      }
139
713
      prevQP[0] = prevQP[1] = slice->getSliceQp();
140
713
    }
141
4.71k
    else if( ctuXPosInCtus == tileXPosInCtus && wavefrontsEnabled )
142
0
    {
143
      // Synchronize cabac probabilities with upper-right CTU if it's available and at the start of a line.
144
0
      if( ctuIdx != 0 ) // if it is the first CTU, then the entropy coder has already been reset
145
0
      {
146
0
        cabacReader.initCtxModels( *slice );
147
0
      }
148
0
      if( cs.getCURestricted( pos.offset(0, -1), pos, slice->getIndependentSliceIdx(), tileIdx, CH_L ) )
149
0
      {
150
0
        cabacReader.setCtx( m_entropyCodingSyncContextState[threadId] );
151
0
      }
152
0
      prevQP[0] = prevQP[1] = slice->getSliceQp();
153
0
    }
154
155
    //memset( cs.getCtuData( ctuRsAddr ).cuPtr, 0, sizeof( CtuData::cuPtr ) );
156
5.43k
    CtuData& ctuData = cs.getCtuData( ctuRsAddr );
157
5.43k
    ctuData.slice = slice;
158
5.43k
    ctuData.pps   = slice->getPPS();
159
5.43k
    ctuData.sps   = slice->getSPS();
160
5.43k
    ctuData.ph    = slice->getPicHeader();
161
162
5.43k
    cabacReader.coding_tree_unit( cs, slice, ctuArea, prevQP, ctuRsAddr );
163
164
5.43k
    if( ctuXPosInCtus == tileXPosInCtus && wavefrontsEnabled )
165
0
    {
166
0
      m_entropyCodingSyncContextState[threadId] = cabacReader.getCtx();
167
0
    }
168
169
5.43k
    if( ctuIdx == slice->getNumCtuInSlice()-1 )
170
64
    {
171
64
      unsigned binVal = cabacReader.terminating_bit();
172
64
      CHECK( !binVal, "Expecting a terminating bit" );
173
8
#if DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES
174
8
      cabacReader.remaining_bytes( false );
175
8
#endif
176
8
    }
177
5.36k
    else if( ( ctuXPosInCtus + 1 == tileXPosInCtus + tileColWidth ) &&
178
966
             ( ctuYPosInCtus + 1 == tileYPosInCtus + tileRowHeight || wavefrontsEnabled ) )
179
0
    {
180
      // The sub-stream/stream should be terminated after this CTU.
181
      // (end of slice-segment, end of tile, end of wavefront-CTU-row)
182
0
      unsigned binVal = cabacReader.terminating_bit();
183
0
      CHECK( !binVal, "Expecting a terminating bit" );
184
185
0
      if( entryPointPresent )
186
0
      {
187
0
#if DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES
188
0
        cabacReader.remaining_bytes( true );
189
0
#endif
190
0
        subStrmId++;
191
0
      }
192
0
    }
193
194
5.37k
#if RECO_WHILE_PARSE
195
5.37k
    pic->ctuParsedBarrier[ctuRsAddr].unlock();
196
5.37k
#endif
197
198
5.37k
    if( ctuRsAddr + 1 == pic->cs->pcv->sizeInCtus )
199
3
    {
200
3
      Picture::PicStateEnum expected = Picture::parsing;
201
3
      pic->progress.compare_exchange_strong( expected, Picture::parsed );   // if RECO_WHILE_PARSE reconstruction can already have started, so we make sure to not overwrite that state
202
203
3
      pic->parseDone.unlock();
204
3
    }
205
5.37k
  }
206
713
}
207
208
}