Coverage Report

Created: 2026-05-16 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libde265/libde265/cabac.h
Line
Count
Source
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifndef DE265_CABAC_H
22
#define DE265_CABAC_H
23
24
#include <stdint.h>
25
#include "contextmodel.h"
26
27
28
class CABAC_decoder {
29
public:
30
  void init(uint8_t* bitstream, int length);
31
  void init_CABAC();
32
  int  decode_bit(context_model* model);
33
  int  decode_TU(int cMax, context_model* model);
34
  int  decode_term_bit();
35
36
  int  decode_bypass();
37
  int  decode_TU_bypass(int cMax);
38
  uint32_t  decode_FL_bypass(int nBits);
39
  int  decode_TR_bypass(int cRiceParam, int cTRMax);
40
  uint32_t  decode_EGk_bypass(int k);
41
42
  uint8_t* bitstream_start = nullptr;
43
  uint8_t* bitstream_curr = nullptr;
44
  uint8_t* bitstream_end = nullptr;
45
46
private:
47
  uint32_t range = 0;
48
  uint32_t value = 0;
49
  int16_t  bits_needed = 0;
50
51
  int  decode_FL_bypass_parallel(int nBits);
52
};
53
54
55
// ---------------------------------------------------------------------------
56
57
class CABAC_encoder
58
{
59
public:
60
0
 CABAC_encoder() : mCtxModels(nullptr) { }
61
0
  virtual ~CABAC_encoder() { }
62
63
  virtual int size() const = 0;
64
  virtual void reset() = 0;
65
66
  // --- VLC ---
67
68
  virtual void write_bits(uint32_t bits,int n) = 0;
69
0
  virtual void write_bit(int bit) { write_bits(bit,1); }
70
  virtual void write_uvlc(int value);
71
  virtual void write_svlc(int value);
72
  virtual bool write_startcode() = 0;
73
  virtual void skip_bits(int nBits) = 0;
74
75
  virtual void add_trailing_bits();
76
  virtual int  number_free_bits_in_byte() const = 0;
77
78
  // output all remaining bits and fill with zeros to next byte boundary
79
0
  virtual void flush_VLC() { }
80
81
82
  // --- CABAC ---
83
84
0
  void set_context_models(context_model_table* models) { mCtxModels=models; }
85
86
0
  virtual void init_CABAC() { }
87
  virtual void write_CABAC_bit(int modelIdx, int bit) = 0;
88
  virtual void write_CABAC_bypass(int bit) = 0;
89
  virtual void write_CABAC_TU_bypass(int value, int cMax);
90
  virtual void write_CABAC_FL_bypass(int value, int nBits);
91
  virtual void write_CABAC_term_bit(int bit) = 0;
92
0
  virtual void flush_CABAC()  { }
93
94
  void write_CABAC_EGk(int absolute_symbol, int k); // absolute_symbol >= 0
95
96
  virtual bool modifies_context() const = 0;
97
98
  float RDBits_for_CABAC_bin(int modelIdx, int bit);
99
100
 protected:
101
  context_model_table* mCtxModels;
102
};
103
104
105
class CABAC_encoder_bitstream : public CABAC_encoder
106
{
107
public:
108
  CABAC_encoder_bitstream();
109
  ~CABAC_encoder_bitstream();
110
111
  void reset() override;
112
113
0
  int size() const override { return data_size; }
114
0
  uint8_t* data() const { return data_mem; }
115
116
  // --- VLC ---
117
118
  void write_bits(uint32_t bits,int n) override;
119
  bool write_startcode() override;
120
  void skip_bits(int nBits) override;
121
122
  int  number_free_bits_in_byte() const override;
123
124
  // output all remaining bits and fill with zeros to next byte boundary
125
  void flush_VLC() override;
126
127
128
  // --- CABAC ---
129
130
  void init_CABAC() override;
131
  void write_CABAC_bit(int modelIdx, int bit) override;
132
  void write_CABAC_bypass(int bit) override;
133
  void write_CABAC_term_bit(int bit) override;
134
  void flush_CABAC() override;
135
136
0
  bool modifies_context() const override { return true; }
137
138
private:
139
  // data buffer
140
141
  uint8_t* data_mem = nullptr;
142
  uint32_t data_capacity = 0;
143
  uint32_t data_size = 0;
144
  char     state = 0; // for inserting emulation-prevention bytes
145
146
  // VLC
147
148
  uint32_t vlc_buffer;
149
  uint32_t vlc_buffer_len = 0;
150
151
152
  // CABAC
153
154
  uint32_t range;
155
  uint32_t low;
156
  int8_t   bits_left;
157
  uint8_t  buffered_byte;
158
  uint16_t num_buffered_bytes;
159
160
161
  bool check_size_and_resize(int nBytes);
162
  void testAndWriteOut();
163
  void write_out();
164
  bool append_byte(int byte);
165
};
166
167
168
class CABAC_encoder_estim : public CABAC_encoder
169
{
170
public:
171
0
  CABAC_encoder_estim() : mFracBits(0) { }
172
173
0
  void reset() override { mFracBits=0; }
174
175
0
  int size() const override { return mFracBits>>(15+3); }
176
177
0
  uint64_t getFracBits() const { return mFracBits; }
178
0
  float    getRDBits() const { return mFracBits / float(1<<15); }
179
180
  // --- VLC ---
181
182
0
  void write_bits(uint32_t bits,int n) override { mFracBits += n<<15; }
183
0
  void write_bit(int bit) override { mFracBits+=1<<15; }
184
0
  bool write_startcode() override { mFracBits += (1<<15)*8*3; return true; }
185
0
  void skip_bits(int nBits) override { mFracBits += nBits<<15; }
186
0
  int  number_free_bits_in_byte() const override { return 0; } // TODO, good enough for now
187
188
  // --- CABAC ---
189
190
  void write_CABAC_bit(int modelIdx, int bit) override;
191
0
  void write_CABAC_bypass(int bit) override {
192
0
    mFracBits += 0x8000;
193
0
  }
194
0
  void write_CABAC_FL_bypass(int value, int nBits) override {
195
0
    mFracBits += nBits<<15;
196
0
  }
197
0
  void write_CABAC_term_bit(int bit) override { /* not implemented (not needed) */ }
198
199
0
  bool modifies_context() const override { return true; }
200
201
 protected:
202
  uint64_t mFracBits;
203
};
204
205
206
class CABAC_encoder_estim_constant : public CABAC_encoder_estim
207
{
208
 public:
209
  void write_CABAC_bit(int modelIdx, int bit) override;
210
211
0
  bool modifies_context() const override { return false; }
212
};
213
214
#endif