Coverage Report

Created: 2026-08-31 06:22

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