/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 | 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 int startCtuTsAddr = slice->getFirstCtuRsAddrInSlice(); |
85 | 0 | const unsigned widthInCtus = cs.pcv->widthInCtus; |
86 | 0 | const bool wavefrontsEnabled = cs.sps->getEntropyCodingSyncEnabledFlag(); |
87 | 0 | const bool entryPointPresent = cs.sps->getEntryPointsPresentFlag(); |
88 | |
|
89 | 0 | AdaptiveLoopFilter::reconstructCoeffAPSs( *slice ); |
90 | |
|
91 | 0 | CABACReader cabacReader; |
92 | 0 | cabacReader.initBitstream( ppcSubstreams[0].get() ); |
93 | 0 | cabacReader.initCtxModels( *slice ); |
94 | | |
95 | | // if first slice, finish init of the coding structure |
96 | 0 | if( startCtuTsAddr == 0 ) |
97 | 0 | { |
98 | 0 | cs.initStructData(); |
99 | 0 | } |
100 | | |
101 | | // Quantization parameter |
102 | 0 | int prevQP[2] = { slice->getSliceQp(), slice->getSliceQp() }; |
103 | 0 | CHECK( prevQP[0] == std::numeric_limits<int>::max(), "Invalid previous QP" ); |
104 | |
|
105 | 0 | DTRACE( g_trace_ctx, D_HEADER, "=========== POC: %d ===========\n", slice->getPOC() ); |
106 | |
|
107 | 0 | unsigned subStrmId = 0; |
108 | 0 | for( unsigned ctuIdx = 0; ctuIdx < slice->getNumCtuInSlice(); ctuIdx++ ) |
109 | 0 | { |
110 | 0 | const unsigned ctuRsAddr = slice->getCtuAddrInSlice(ctuIdx); |
111 | 0 | const unsigned ctuXPosInCtus = ctuRsAddr % widthInCtus; |
112 | 0 | const unsigned ctuYPosInCtus = ctuRsAddr / widthInCtus; |
113 | 0 | const unsigned tileColIdx = slice->getPPS()->ctuToTileCol( ctuXPosInCtus ); |
114 | 0 | const unsigned tileRowIdx = slice->getPPS()->ctuToTileRow( ctuYPosInCtus ); |
115 | 0 | const unsigned tileXPosInCtus = slice->getPPS()->getTileColumnBd( tileColIdx ); |
116 | 0 | const unsigned tileYPosInCtus = slice->getPPS()->getTileRowBd( tileRowIdx ); |
117 | 0 | const unsigned tileColWidth = slice->getPPS()->getTileColumnWidth( tileColIdx ); |
118 | 0 | const unsigned tileRowHeight = slice->getPPS()->getTileRowHeight( tileRowIdx ); |
119 | 0 | const unsigned tileIdx = slice->getPPS()->getTileIdx( ctuXPosInCtus, ctuYPosInCtus); |
120 | 0 | const unsigned maxCUSize = sps->getMaxCUWidth(); |
121 | 0 | const Position pos( ctuXPosInCtus*maxCUSize, ctuYPosInCtus*maxCUSize) ; |
122 | 0 | 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 | 0 | CHECK( ctuXPosInCtus > 0 && cs.getCtuData( ctuRsAddr - 1 ).slice == nullptr, "CTU left not available RS:" << ctuRsAddr - 1 ); |
126 | 0 | CHECK( ctuYPosInCtus > 0 && cs.getCtuData( ctuRsAddr - widthInCtus ).slice == nullptr, "CTU above not available RS:" << ctuRsAddr - widthInCtus ); |
127 | |
|
128 | 0 | DTRACE_UPDATE( g_trace_ctx, std::make_pair( "ctu", ctuRsAddr ) ); |
129 | |
|
130 | 0 | cabacReader.initBitstream( ppcSubstreams[subStrmId].get() ); |
131 | | |
132 | | // set up CABAC contexts' state for this CTU |
133 | 0 | if( ctuXPosInCtus == tileXPosInCtus && ctuYPosInCtus == tileYPosInCtus ) |
134 | 0 | { |
135 | 0 | 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 | 0 | prevQP[0] = prevQP[1] = slice->getSliceQp(); |
140 | 0 | } |
141 | 0 | 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 | 0 | CtuData& ctuData = cs.getCtuData( ctuRsAddr ); |
157 | 0 | ctuData.slice = slice; |
158 | 0 | ctuData.pps = slice->getPPS(); |
159 | 0 | ctuData.sps = slice->getSPS(); |
160 | 0 | ctuData.ph = slice->getPicHeader(); |
161 | |
|
162 | 0 | cabacReader.coding_tree_unit( cs, slice, ctuArea, prevQP, ctuRsAddr ); |
163 | |
|
164 | 0 | if( ctuXPosInCtus == tileXPosInCtus && wavefrontsEnabled ) |
165 | 0 | { |
166 | 0 | m_entropyCodingSyncContextState[threadId] = cabacReader.getCtx(); |
167 | 0 | } |
168 | |
|
169 | 0 | if( ctuIdx == slice->getNumCtuInSlice()-1 ) |
170 | 0 | { |
171 | 0 | unsigned binVal = cabacReader.terminating_bit(); |
172 | 0 | CHECK( !binVal, "Expecting a terminating bit" ); |
173 | 0 | #if DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES |
174 | 0 | cabacReader.remaining_bytes( false ); |
175 | 0 | #endif |
176 | 0 | } |
177 | 0 | else if( ( ctuXPosInCtus + 1 == tileXPosInCtus + tileColWidth ) && |
178 | 0 | ( 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 | 0 | #if RECO_WHILE_PARSE |
195 | 0 | pic->ctuParsedBarrier[ctuRsAddr].unlock(); |
196 | 0 | #endif |
197 | |
|
198 | 0 | if( ctuRsAddr + 1 == pic->cs->pcv->sizeInCtus ) |
199 | 0 | { |
200 | 0 | Picture::PicStateEnum expected = Picture::parsing; |
201 | 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 |
202 | |
|
203 | 0 | pic->parseDone.unlock(); |
204 | 0 | } |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | | } |