1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2019, ITU/ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | /** \file DecSlice.cpp |
---|
35 | \brief slice decoder class |
---|
36 | */ |
---|
37 | |
---|
38 | #include "DecSlice.h" |
---|
39 | #include "CommonLib/UnitTools.h" |
---|
40 | #include "CommonLib/dtrace_next.h" |
---|
41 | |
---|
42 | #include <vector> |
---|
43 | |
---|
44 | //! \ingroup DecoderLib |
---|
45 | //! \{ |
---|
46 | |
---|
47 | ////////////////////////////////////////////////////////////////////// |
---|
48 | // Construction/Destruction |
---|
49 | ////////////////////////////////////////////////////////////////////// |
---|
50 | |
---|
51 | DecSlice::DecSlice() |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | DecSlice::~DecSlice() |
---|
56 | { |
---|
57 | } |
---|
58 | |
---|
59 | void DecSlice::create() |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | void DecSlice::destroy() |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | void DecSlice::init( CABACDecoder* cabacDecoder, DecCu* pcCuDecoder ) |
---|
68 | { |
---|
69 | m_CABACDecoder = cabacDecoder; |
---|
70 | m_pcCuDecoder = pcCuDecoder; |
---|
71 | } |
---|
72 | |
---|
73 | void DecSlice::decompressSlice( Slice* slice, InputBitstream* bitstream, int debugCTU ) |
---|
74 | { |
---|
75 | //-- For time output for each slice |
---|
76 | slice->startProcessingTimer(); |
---|
77 | |
---|
78 | const SPS* sps = slice->getSPS(); |
---|
79 | Picture* pic = slice->getPic(); |
---|
80 | const TileMap& tileMap = *pic->tileMap; |
---|
81 | CABACReader& cabacReader = *m_CABACDecoder->getCABACReader( 0 ); |
---|
82 | |
---|
83 | // setup coding structure |
---|
84 | CodingStructure& cs = *pic->cs; |
---|
85 | cs.slice = slice; |
---|
86 | cs.sps = sps; |
---|
87 | cs.pps = slice->getPPS(); |
---|
88 | #if JVET_N0415_CTB_ALF |
---|
89 | memcpy(cs.apss, slice->getAPSs(), sizeof(cs.apss)); |
---|
90 | #else |
---|
91 | cs.aps = slice->getAPS(); |
---|
92 | #endif |
---|
93 | #if HEVC_VPS |
---|
94 | cs.vps = slice->getVPS(); |
---|
95 | #endif |
---|
96 | cs.pcv = slice->getPPS()->pcv; |
---|
97 | cs.chromaQpAdj = 0; |
---|
98 | |
---|
99 | cs.picture->resizeSAO(cs.pcv->sizeInCtus, 0); |
---|
100 | |
---|
101 | if (slice->getSliceCurStartCtuTsAddr() == 0) |
---|
102 | { |
---|
103 | cs.picture->resizeAlfCtuEnableFlag( cs.pcv->sizeInCtus ); |
---|
104 | #if JVET_N0415_CTB_ALF |
---|
105 | cs.picture->resizeAlfCtbFilterIndex(cs.pcv->sizeInCtus); |
---|
106 | #endif |
---|
107 | } |
---|
108 | |
---|
109 | const unsigned numSubstreams = slice->getNumberOfSubstreamSizes() + 1; |
---|
110 | |
---|
111 | // init each couple {EntropyDecoder, Substream} |
---|
112 | // Table of extracted substreams. |
---|
113 | std::vector<InputBitstream*> ppcSubstreams( numSubstreams ); |
---|
114 | for( unsigned idx = 0; idx < numSubstreams; idx++ ) |
---|
115 | { |
---|
116 | ppcSubstreams[idx] = bitstream->extractSubstream( idx+1 < numSubstreams ? ( slice->getSubstreamSize(idx) << 3 ) : bitstream->getNumBitsLeft() ); |
---|
117 | } |
---|
118 | |
---|
119 | #if HEVC_DEPENDENT_SLICES |
---|
120 | const int startCtuTsAddr = slice->getSliceSegmentCurStartCtuTsAddr(); |
---|
121 | #else |
---|
122 | const int startCtuTsAddr = slice->getSliceCurStartCtuTsAddr(); |
---|
123 | #endif |
---|
124 | #if HEVC_DEPENDENT_SLICES |
---|
125 | const int startCtuRsAddr = startCtuTsAddr; |
---|
126 | #else |
---|
127 | const int startCtuRsAddr = tileMap.getCtuTsToRsAddrMap(startCtuTsAddr); |
---|
128 | #endif |
---|
129 | const unsigned numCtusInFrame = cs.pcv->sizeInCtus; |
---|
130 | const unsigned widthInCtus = cs.pcv->widthInCtus; |
---|
131 | #if HEVC_DEPENDENT_SLICES |
---|
132 | const bool depSliceSegmentsEnabled = cs.pps->getDependentSliceSegmentsEnabledFlag(); |
---|
133 | #endif |
---|
134 | const bool wavefrontsEnabled = cs.pps->getEntropyCodingSyncEnabledFlag(); |
---|
135 | |
---|
136 | cabacReader.initBitstream( ppcSubstreams[0] ); |
---|
137 | cabacReader.initCtxModels( *slice ); |
---|
138 | |
---|
139 | // Quantization parameter |
---|
140 | #if HEVC_DEPENDENT_SLICES |
---|
141 | if(!slice->getDependentSliceSegmentFlag()) |
---|
142 | { |
---|
143 | #endif |
---|
144 | pic->m_prevQP[0] = pic->m_prevQP[1] = slice->getSliceQp(); |
---|
145 | #if HEVC_DEPENDENT_SLICES |
---|
146 | } |
---|
147 | #endif |
---|
148 | CHECK( pic->m_prevQP[0] == std::numeric_limits<int>::max(), "Invalid previous QP" ); |
---|
149 | |
---|
150 | DTRACE( g_trace_ctx, D_HEADER, "=========== POC: %d ===========\n", slice->getPOC() ); |
---|
151 | |
---|
152 | // The first CTU of the slice is the first coded substream, but the global substream number, as calculated by getSubstreamForCtuAddr may be higher. |
---|
153 | // This calculates the common offset for all substreams in this slice. |
---|
154 | #if HEVC_DEPENDENT_SLICES |
---|
155 | const unsigned subStreamOffset = tileMap.getSubstreamForCtuAddr( startCtuRsAddr, true, slice ); |
---|
156 | #else |
---|
157 | const unsigned subStreamOffset = tileMap.getSubstreamForCtuAddr(startCtuRsAddr, true, slice); |
---|
158 | #endif |
---|
159 | |
---|
160 | #if HEVC_DEPENDENT_SLICES |
---|
161 | if( depSliceSegmentsEnabled ) |
---|
162 | { |
---|
163 | // modify initial contexts with previous slice segment if this is a dependent slice. |
---|
164 | const unsigned startTileIdx = tileMap.getTileIdxMap(startCtuRsAddr); |
---|
165 | const Tile& currentTile = tileMap.tiles[startTileIdx]; |
---|
166 | const unsigned firstCtuRsAddrOfTile = currentTile.getFirstCtuRsAddr(); |
---|
167 | if( slice->getDependentSliceSegmentFlag() && startCtuRsAddr != firstCtuRsAddrOfTile ) |
---|
168 | { |
---|
169 | if( currentTile.getTileWidthInCtus() >= 2 || !wavefrontsEnabled ) |
---|
170 | { |
---|
171 | cabacReader.getCtx() = m_lastSliceSegmentEndContextState; |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | #endif |
---|
176 | // for every CTU in the slice segment... |
---|
177 | bool isLastCtuOfSliceSegment = false; |
---|
178 | for( unsigned ctuTsAddr = startCtuTsAddr; !isLastCtuOfSliceSegment && ctuTsAddr < numCtusInFrame; ctuTsAddr++ ) |
---|
179 | { |
---|
180 | const unsigned ctuRsAddr = tileMap.getCtuTsToRsAddrMap(ctuTsAddr); |
---|
181 | const Tile& currentTile = tileMap.tiles[ tileMap.getTileIdxMap(ctuRsAddr) ]; |
---|
182 | const unsigned firstCtuRsAddrOfTile = currentTile.getFirstCtuRsAddr(); |
---|
183 | const unsigned tileXPosInCtus = firstCtuRsAddrOfTile % widthInCtus; |
---|
184 | const unsigned tileYPosInCtus = firstCtuRsAddrOfTile / widthInCtus; |
---|
185 | const unsigned ctuXPosInCtus = ctuRsAddr % widthInCtus; |
---|
186 | const unsigned ctuYPosInCtus = ctuRsAddr / widthInCtus; |
---|
187 | const unsigned subStrmId = tileMap.getSubstreamForCtuAddr( ctuRsAddr, true, slice ) - subStreamOffset; |
---|
188 | const unsigned maxCUSize = sps->getMaxCUWidth(); |
---|
189 | Position pos( ctuXPosInCtus*maxCUSize, ctuYPosInCtus*maxCUSize) ; |
---|
190 | UnitArea ctuArea(cs.area.chromaFormat, Area( pos.x, pos.y, maxCUSize, maxCUSize ) ); |
---|
191 | |
---|
192 | DTRACE_UPDATE( g_trace_ctx, std::make_pair( "ctu", ctuRsAddr ) ); |
---|
193 | |
---|
194 | cabacReader.initBitstream( ppcSubstreams[subStrmId] ); |
---|
195 | |
---|
196 | // set up CABAC contexts' state for this CTU |
---|
197 | if( ctuRsAddr == firstCtuRsAddrOfTile ) |
---|
198 | { |
---|
199 | if( ctuTsAddr != startCtuTsAddr ) // if it is the first CTU, then the entropy coder has already been reset |
---|
200 | { |
---|
201 | cabacReader.initCtxModels( *slice ); |
---|
202 | } |
---|
203 | pic->m_prevQP[0] = pic->m_prevQP[1] = slice->getSliceQp(); |
---|
204 | } |
---|
205 | else if( ctuXPosInCtus == tileXPosInCtus && wavefrontsEnabled ) |
---|
206 | { |
---|
207 | // Synchronize cabac probabilities with upper-right CTU if it's available and at the start of a line. |
---|
208 | if( ctuTsAddr != startCtuTsAddr ) // if it is the first CTU, then the entropy coder has already been reset |
---|
209 | { |
---|
210 | cabacReader.initCtxModels( *slice ); |
---|
211 | } |
---|
212 | if( cs.getCURestricted( pos.offset(maxCUSize, -1), slice->getIndependentSliceIdx(), tileMap.getTileIdxMap( pos ), CH_L ) ) |
---|
213 | { |
---|
214 | // Top-right is available, so use it. |
---|
215 | cabacReader.getCtx() = m_entropyCodingSyncContextState; |
---|
216 | } |
---|
217 | pic->m_prevQP[0] = pic->m_prevQP[1] = slice->getSliceQp(); |
---|
218 | } |
---|
219 | |
---|
220 | bool updateGbiCodingOrder = cs.slice->getSliceType() == B_SLICE && ctuTsAddr == startCtuTsAddr; |
---|
221 | if(updateGbiCodingOrder) |
---|
222 | { |
---|
223 | resetGbiCodingOrder(true, cs); |
---|
224 | } |
---|
225 | |
---|
226 | #if FIXBUG_RESET_LUT |
---|
227 | if ((cs.slice->getSliceType() != I_SLICE || cs.sps->getIBCFlag()) && ctuXPosInCtus == tileXPosInCtus) |
---|
228 | #else |
---|
229 | if ((cs.slice->getSliceType() != I_SLICE || cs.sps->getIBCFlag()) && ctuXPosInCtus == 0) |
---|
230 | #endif |
---|
231 | { |
---|
232 | cs.motionLut.lut.resize(0); |
---|
233 | cs.motionLut.lutIbc.resize(0); |
---|
234 | #if !JVET_N0266_SMALL_BLOCKS |
---|
235 | cs.motionLut.lutShare.resize(0); |
---|
236 | #endif |
---|
237 | cs.motionLut.lutShareIbc.resize(0); |
---|
238 | } |
---|
239 | |
---|
240 | if( !cs.slice->isIntra() ) |
---|
241 | { |
---|
242 | pic->mctsInfo.init( &cs, getCtuAddr( ctuArea.lumaPos(), *( cs.pcv ) ) ); |
---|
243 | } |
---|
244 | |
---|
245 | if( ctuRsAddr == debugCTU ) |
---|
246 | { |
---|
247 | isLastCtuOfSliceSegment = true; // get out here |
---|
248 | break; |
---|
249 | } |
---|
250 | isLastCtuOfSliceSegment = cabacReader.coding_tree_unit( cs, ctuArea, pic->m_prevQP, ctuRsAddr ); |
---|
251 | |
---|
252 | m_pcCuDecoder->decompressCtu( cs, ctuArea ); |
---|
253 | |
---|
254 | if( ctuXPosInCtus == tileXPosInCtus+1 && wavefrontsEnabled ) |
---|
255 | { |
---|
256 | m_entropyCodingSyncContextState = cabacReader.getCtx(); |
---|
257 | } |
---|
258 | |
---|
259 | |
---|
260 | if( isLastCtuOfSliceSegment ) |
---|
261 | { |
---|
262 | #if DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES |
---|
263 | cabacReader.remaining_bytes( false ); |
---|
264 | #endif |
---|
265 | #if HEVC_DEPENDENT_SLICES |
---|
266 | if( !slice->getDependentSliceSegmentFlag() ) |
---|
267 | { |
---|
268 | #endif |
---|
269 | slice->setSliceCurEndCtuTsAddr( ctuTsAddr+1 ); |
---|
270 | #if HEVC_DEPENDENT_SLICES |
---|
271 | } |
---|
272 | slice->setSliceSegmentCurEndCtuTsAddr( ctuTsAddr+1 ); |
---|
273 | #endif |
---|
274 | } |
---|
275 | else if( ( ctuXPosInCtus + 1 == tileXPosInCtus + currentTile.getTileWidthInCtus () ) && |
---|
276 | ( ctuYPosInCtus + 1 == tileYPosInCtus + currentTile.getTileHeightInCtus() || wavefrontsEnabled ) ) |
---|
277 | { |
---|
278 | // The sub-stream/stream should be terminated after this CTU. |
---|
279 | // (end of slice-segment, end of tile, end of wavefront-CTU-row) |
---|
280 | unsigned binVal = cabacReader.terminating_bit(); |
---|
281 | CHECK( !binVal, "Expecting a terminating bit" ); |
---|
282 | #if DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES |
---|
283 | cabacReader.remaining_bytes( true ); |
---|
284 | #endif |
---|
285 | } |
---|
286 | } |
---|
287 | CHECK( !isLastCtuOfSliceSegment, "Last CTU of slice segment not signalled as such" ); |
---|
288 | |
---|
289 | #if HEVC_DEPENDENT_SLICES |
---|
290 | if( depSliceSegmentsEnabled ) |
---|
291 | { |
---|
292 | m_lastSliceSegmentEndContextState = cabacReader.getCtx(); //ctx end of dep.slice |
---|
293 | } |
---|
294 | #endif |
---|
295 | // deallocate all created substreams, including internal buffers. |
---|
296 | for( auto substr: ppcSubstreams ) |
---|
297 | { |
---|
298 | delete substr; |
---|
299 | } |
---|
300 | slice->stopProcessingTimer(); |
---|
301 | } |
---|
302 | |
---|
303 | //! \} |
---|