/work/vvdec/source/Lib/DecoderLib/BinDecoder.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 BinDecoder.cpp |
44 | | * \brief Low level binary symbol writer |
45 | | */ |
46 | | |
47 | | |
48 | | #include "BinDecoder.h" |
49 | | #include "CommonLib/Rom.h" |
50 | | #include "CommonLib/BitStream.h" |
51 | | |
52 | | #include "CommonLib/dtrace_next.h" |
53 | | |
54 | | #include <limits> |
55 | | |
56 | | #define CNT_OFFSET 0 |
57 | | |
58 | | namespace vvdec |
59 | | { |
60 | | |
61 | | // BinDecoder uses the `signed >> 31` sign-extract idiom, which is implementation-defined |
62 | | // for negative operands before C++20. Require two's-complement representation so the idiom |
63 | | // behaves as intended (mandatory anyway in C++20). |
64 | | static_assert( std::numeric_limits<int>::min() == -std::numeric_limits<int>::max() - 1, |
65 | | "two's complement signed integer representation required" ); |
66 | | static_assert( ( -1 >> 1 ) == -1, |
67 | | "right shift of negative number needs to be implemented as arithmetic shift." ); |
68 | | |
69 | | void BinDecoder::init( InputBitstream* bitstream ) |
70 | 0 | { |
71 | 0 | m_Bitstream = bitstream; |
72 | 0 | } |
73 | | |
74 | | |
75 | | void BinDecoder::uninit() |
76 | 0 | { |
77 | 0 | m_Bitstream = 0; |
78 | 0 | } |
79 | | |
80 | | |
81 | | void BinDecoder::start() |
82 | 0 | { |
83 | 0 | CHECK( m_Bitstream->getNumBitsUntilByteAligned(), "Bitstream is not byte aligned." ); |
84 | 0 | m_Range = 510; |
85 | 0 | m_Value = ( uint32_t( m_Bitstream->readByte() ) << 8 ) + m_Bitstream->readByte(); |
86 | 0 | m_bitsNeeded = -8; |
87 | 0 | } |
88 | | |
89 | | |
90 | | void BinDecoder::finish() |
91 | 0 | { |
92 | 0 | unsigned lastByte = m_Bitstream->peekPreviousByte(); |
93 | 0 | CHECK( ( ( lastByte << ( 8 + m_bitsNeeded ) ) & 0xff ) != 0x80, |
94 | 0 | "No proper stop/alignment pattern at end of CABAC stream." ); |
95 | 0 | } |
96 | | |
97 | | |
98 | | void BinDecoder::reset( int qp, int initId ) |
99 | 0 | { |
100 | 0 | m_Ctx.init( qp, initId ); |
101 | 0 | start(); |
102 | 0 | } |
103 | | |
104 | | |
105 | | // decodeBinEP() is defined ALWAYS_INLINE in BinDecoder.h (moved for inlining into the CABAC hot loops). |
106 | | |
107 | | |
108 | | unsigned BinDecoder::decodeBinsEP( unsigned numBins ) |
109 | 0 | { |
110 | | #if ENABLE_TRACING |
111 | | int numBinsOrig = numBins; |
112 | | #endif |
113 | |
|
114 | 0 | if( m_Range == 256 ) |
115 | 0 | { |
116 | 0 | return decodeAlignedBinsEP( numBins ); |
117 | 0 | } |
118 | 0 | unsigned remBins = numBins; |
119 | 0 | unsigned bins = 0; |
120 | 0 | unsigned value = m_Value; |
121 | 0 | unsigned range = m_Range; |
122 | 0 | int bitsNeeded = m_bitsNeeded; |
123 | 0 | while( remBins > 8 ) |
124 | 0 | { |
125 | 0 | value = ( value << 8 ) + ( m_Bitstream->readByte() << ( 8 + bitsNeeded ) ); |
126 | 0 | unsigned SR = range << 15; |
127 | 0 | for( int i = 0; i < 8; i++ ) |
128 | 0 | { |
129 | 0 | bins += bins; |
130 | 0 | SR >>= 1; |
131 | 0 | if( value >= SR ) |
132 | 0 | { |
133 | 0 | bins ++; |
134 | 0 | value -= SR; |
135 | 0 | } |
136 | 0 | } |
137 | 0 | remBins -= 8; |
138 | 0 | } |
139 | 0 | bitsNeeded += (int)remBins; |
140 | 0 | value <<= remBins; |
141 | 0 | if( bitsNeeded >= 0 ) |
142 | 0 | { |
143 | 0 | value += m_Bitstream->readByte() << bitsNeeded; |
144 | 0 | bitsNeeded -= 8; |
145 | 0 | } |
146 | 0 | unsigned SR = range << ( remBins + 7 ); |
147 | 0 | for ( unsigned i = 0; i < remBins; i++ ) |
148 | 0 | { |
149 | 0 | bins += bins; |
150 | 0 | SR >>= 1; |
151 | 0 | if( value >= SR ) |
152 | 0 | { |
153 | 0 | bins ++; |
154 | 0 | value -= SR; |
155 | 0 | } |
156 | 0 | } |
157 | 0 | m_Value = value; |
158 | 0 | m_Range = range; |
159 | 0 | m_bitsNeeded = bitsNeeded; |
160 | | #if ENABLE_TRACING |
161 | | for( int i = 0; i < numBinsOrig; i++ ) |
162 | | { |
163 | | DTRACE( g_trace_ctx, D_CABAC, "%d" " " "%d" " EP=%d \n", DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), m_Range, ( bins >> ( numBinsOrig - 1 - i ) ) & 1 ); |
164 | | } |
165 | | #endif |
166 | 0 | return bins; |
167 | 0 | } |
168 | | |
169 | | unsigned BinDecoder::decodeRemAbsEP(unsigned goRicePar, unsigned cutoff, int maxLog2TrDynamicRange) |
170 | 0 | { |
171 | 0 | unsigned prefix = 0; |
172 | 0 | { |
173 | 0 | const unsigned maxPrefix = 32 - maxLog2TrDynamicRange; |
174 | 0 | unsigned codeWord = 0; |
175 | 0 | do |
176 | 0 | { |
177 | 0 | prefix++; |
178 | 0 | codeWord = decodeBinEP(); |
179 | 0 | } while (codeWord && prefix < maxPrefix); |
180 | 0 | prefix -= 1 - (int)codeWord; |
181 | 0 | } |
182 | |
|
183 | 0 | unsigned length = goRicePar, offset; |
184 | 0 | if (prefix < cutoff) |
185 | 0 | { |
186 | 0 | offset = prefix << goRicePar; |
187 | 0 | } |
188 | 0 | else |
189 | 0 | { |
190 | 0 | offset = (((1u << (prefix - cutoff)) + cutoff - 1) << goRicePar); |
191 | 0 | { |
192 | 0 | length += (prefix == (32u - maxLog2TrDynamicRange) ? maxLog2TrDynamicRange - goRicePar : prefix - cutoff); |
193 | 0 | } |
194 | 0 | } |
195 | 0 | return offset + decodeBinsEP(length); |
196 | 0 | } |
197 | | |
198 | | // decodeBinTrm() is defined ALWAYS_INLINE in BinDecoder.h (moved for inlining into the CABAC hot loops). |
199 | | |
200 | | |
201 | | void BinDecoder::align() |
202 | 0 | { |
203 | 0 | m_Range = 256; |
204 | 0 | } |
205 | | |
206 | | #if ENABLE_TRACING |
207 | | unsigned int BinDecoder::getNumBitsRead() const |
208 | | { |
209 | | return m_Bitstream->getNumBitsRead() + m_bitsNeeded; |
210 | | } |
211 | | #endif // ENABLE_TRACING |
212 | | |
213 | | unsigned BinDecoder::decodeAlignedBinsEP( unsigned numBins ) |
214 | 0 | { |
215 | | #if ENABLE_TRACING |
216 | | int numBinsOrig = numBins; |
217 | | #endif |
218 | 0 | unsigned remBins = numBins; |
219 | 0 | unsigned bins = 0; |
220 | 0 | while( remBins > 0 ) |
221 | 0 | { |
222 | | // The MSB of m_Value is known to be 0 because range is 256. Therefore: |
223 | | // > The comparison against the symbol range of 128 is simply a test on the next-most-significant bit |
224 | | // > "Subtracting" the symbol range if the decoded bin is 1 simply involves clearing that bit. |
225 | | // As a result, the required bins are simply the <binsToRead> next-most-significant bits of m_Value |
226 | | // (m_Value is stored MSB-aligned in a 16-bit buffer - hence the shift of 15) |
227 | | // |
228 | | // m_Value = |0|V|V|V|V|V|V|V|V|B|B|B|B|B|B|B| (V = usable bit, B = potential buffered bit (buffer refills when m_bitsNeeded >= 0)) |
229 | | // |
230 | 0 | unsigned binsToRead = std::min<unsigned>( remBins, 8 ); //read bytes if able to take advantage of the system's byte-read function |
231 | 0 | unsigned binMask = ( 1 << binsToRead ) - 1; |
232 | 0 | unsigned newBins = ( m_Value >> (15 - binsToRead) ) & binMask; |
233 | 0 | bins = ( bins << binsToRead) | newBins; |
234 | 0 | m_Value = ( m_Value << binsToRead) & 0x7FFF; |
235 | 0 | remBins -= binsToRead; |
236 | 0 | m_bitsNeeded += (int)binsToRead; |
237 | 0 | if( m_bitsNeeded >= 0 ) |
238 | 0 | { |
239 | 0 | m_Value |= m_Bitstream->readByte() << m_bitsNeeded; |
240 | 0 | m_bitsNeeded -= 8; |
241 | 0 | } |
242 | 0 | } |
243 | | #if ENABLE_TRACING |
244 | | for( int i = 0; i < numBinsOrig; i++ ) |
245 | | { |
246 | | DTRACE( g_trace_ctx, D_CABAC, "%d" " " "%d" " " "EP=%d \n", DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), m_Range, ( bins >> ( numBinsOrig - 1 - i ) ) & 1 ); |
247 | | } |
248 | | #endif |
249 | 0 | return bins; |
250 | 0 | } |
251 | | |
252 | | |
253 | | // decodeBin() is defined ALWAYS_INLINE in BinDecoder.h (moved for inlining into the CABAC hot loops). |
254 | | |
255 | | } |