/work/vvdec/source/Lib/DecoderLib/BinDecoder.h
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.h |
44 | | * \brief Low level binary symbol writer |
45 | | */ |
46 | | |
47 | | #pragma once |
48 | | |
49 | | #include "CommonLib/Contexts.h" |
50 | | #include "CommonLib/BitStream.h" // complete type needed to inline readByte/readByteFlag below |
51 | | #include "CommonLib/dtrace_next.h" // DTRACE macros (no-ops in release) used by the inlined bodies |
52 | | |
53 | | namespace vvdec |
54 | | { |
55 | | class InputBitstream; |
56 | | |
57 | | class BinDecoder |
58 | | { |
59 | | public: |
60 | 0 | BinDecoder() = default; |
61 | 0 | ~BinDecoder() = default; |
62 | | |
63 | | void init ( InputBitstream* bitstream ); |
64 | | void uninit (); |
65 | | void start (); |
66 | | void finish (); |
67 | | void reset ( int qp, int initId ); |
68 | | |
69 | 0 | const Ctx& getCtx() const { return m_Ctx; } |
70 | 0 | void setCtx( const Ctx& ctx ) { m_Ctx = ctx; } |
71 | | |
72 | | unsigned decodeBin ( unsigned ctxId ); |
73 | | unsigned decodeBinEP (); |
74 | | unsigned decodeBinsEP ( unsigned numBins ); |
75 | | unsigned decodeRemAbsEP ( unsigned goRicePar, unsigned cutoff, int maxLog2TrDynamicRange ); |
76 | | unsigned decodeBinTrm (); |
77 | | void align (); |
78 | | #if ENABLE_TRACING |
79 | | unsigned getNumBitsRead() const; |
80 | | #endif |
81 | | |
82 | | private: |
83 | | unsigned decodeAlignedBinsEP ( unsigned numBins ); |
84 | | |
85 | | Ctx m_Ctx; |
86 | | |
87 | | protected: |
88 | | InputBitstream* m_Bitstream = nullptr; |
89 | | uint32_t m_Range = 0; |
90 | | uint32_t m_Value = 0; |
91 | | int32_t m_bitsNeeded = 0; |
92 | | }; |
93 | | |
94 | | // --------------------------------------------------------------------------- |
95 | | // Hot-path bin decoders are defined here (not in BinDecoder.cpp) and forced |
96 | | // ALWAYS_INLINE so they inline into the CABAC parser hot loops (residual_coding, |
97 | | // coding_tree, ...) which live in a different translation unit. This keeps |
98 | | // m_Range/m_Value/m_bitsNeeded in registers across consecutive bins instead of |
99 | | // reloading/storing them through `this` on every bin. Pure relocation: bodies |
100 | | // are identical to the previous out-of-line definitions -> bit-exact output. |
101 | | // --------------------------------------------------------------------------- |
102 | | |
103 | | ALWAYS_INLINE unsigned BinDecoder::decodeBinEP() |
104 | 0 | { |
105 | 0 | unsigned value = m_Value << 1; |
106 | 0 | if (++m_bitsNeeded >= 0) { |
107 | 0 | value += m_Bitstream->readByte(); |
108 | 0 | m_bitsNeeded = -8; |
109 | 0 | } |
110 | |
|
111 | 0 | unsigned bin = 0; |
112 | 0 | unsigned SR = m_Range << 7; |
113 | 0 | if (value >= SR) { |
114 | 0 | value -= SR; |
115 | 0 | bin = 1; |
116 | 0 | } |
117 | 0 | m_Value = value; |
118 | 0 | DTRACE(g_trace_ctx, D_CABAC, |
119 | 0 | "%d" |
120 | 0 | " " |
121 | 0 | "%d" |
122 | 0 | " EP=%d \n", |
123 | 0 | DTRACE_GET_COUNTER(g_trace_ctx, D_CABAC), m_Range, bin); |
124 | 0 | return bin; |
125 | 0 | } |
126 | | |
127 | | ALWAYS_INLINE unsigned BinDecoder::decodeBinTrm() |
128 | 0 | { |
129 | 0 | m_Range -= 2; |
130 | 0 | unsigned SR = m_Range << 7; |
131 | |
|
132 | 0 | DTRACE( g_trace_ctx, D_CABAC, "%d" " binTrm range=" "%d" " bin=%d\n" , DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), m_Range, m_Value >= SR ); |
133 | |
|
134 | 0 | if( m_Value >= SR ) |
135 | 0 | { |
136 | 0 | return 1; |
137 | 0 | } |
138 | 0 | else |
139 | 0 | { |
140 | 0 | if( m_Range < 256 ) |
141 | 0 | { |
142 | 0 | m_Range += m_Range; |
143 | 0 | m_Value += m_Value; |
144 | 0 | if( ++m_bitsNeeded == 0 ) |
145 | 0 | { |
146 | 0 | m_Value += m_Bitstream->readByte(); |
147 | 0 | m_bitsNeeded = -8; |
148 | 0 | } |
149 | 0 | } |
150 | 0 | return 0; |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | ALWAYS_INLINE unsigned BinDecoder::decodeBin( unsigned ctxId ) |
155 | 0 | { |
156 | 0 | BinProbModel& rcProbModel = m_Ctx[ctxId]; |
157 | |
|
158 | 0 | unsigned bin, LPS, s0, s1; |
159 | 0 | uint32_t range = m_Range; |
160 | 0 | uint32_t value = m_Value; |
161 | 0 | int32_t bitsNeeded = m_bitsNeeded; |
162 | |
|
163 | 0 | rcProbModel.lpsmps( range, LPS, bin, s0, s1 ); |
164 | |
|
165 | 0 | DTRACE( g_trace_ctx, D_CABAC, "%d" " %d " "%d" " " "[%d:%d]" " " "%2d(MPS=%d)" " " , DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), 666, range, range-LPS, LPS, ( unsigned int )( rcProbModel.state() ), value < ( ( range - LPS ) << 7 ) ); |
166 | |
|
167 | 0 | range -= LPS; |
168 | 0 | uint32_t SR = range << 7; |
169 | |
|
170 | 0 | int b = ~( ( int( value ) - int( SR ) ) >> 31 ); |
171 | 0 | int a = ~b & ( ( int( range ) - 256 ) >> 31 ); |
172 | |
|
173 | 0 | int numBits = ( a & rcProbModel.getRenormBitsRange( range ) ) | ( b & rcProbModel.getRenormBitsLPS( LPS ) ); |
174 | |
|
175 | 0 | value -= b & SR; |
176 | 0 | value <<= numBits; |
177 | |
|
178 | 0 | range &= ~b; |
179 | 0 | range |= ( b & LPS ); |
180 | 0 | range <<= numBits; |
181 | |
|
182 | 0 | bin ^= b; |
183 | 0 | bin &= 1; |
184 | |
|
185 | 0 | bitsNeeded += numBits & ( a | b ); |
186 | |
|
187 | 0 | const int c = ~(bitsNeeded >> 31); |
188 | 0 | value += uint32_t( m_Bitstream->readByteFlag( c ) ) << ( bitsNeeded & 31 ); |
189 | 0 | bitsNeeded -= c & 8; |
190 | |
|
191 | 0 | m_Range = range; |
192 | 0 | m_Value = value; |
193 | 0 | m_bitsNeeded = bitsNeeded; |
194 | |
|
195 | 0 | rcProbModel.update( bin, s0, s1 ); |
196 | 0 | DTRACE_WITHOUT_COUNT( g_trace_ctx, D_CABAC, " - " "%d" "\n", bin ); |
197 | 0 | return bin; |
198 | 0 | } |
199 | | |
200 | | } |