digiKam
cabac.h
Go to the documentation of this file.
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 typedef struct {
29  uint8_t* bitstream_start;
30  uint8_t* bitstream_curr;
31  uint8_t* bitstream_end;
32 
33  uint32_t range;
34  uint32_t value;
35  int16_t bits_needed;
37 
38 
39 void init_CABAC_decoder(CABAC_decoder* decoder, uint8_t* bitstream, int length);
42 int decode_CABAC_TU(CABAC_decoder* decoder, int cMax, context_model* model);
44 
46 int decode_CABAC_TU_bypass(CABAC_decoder* decoder, int cMax);
47 int decode_CABAC_FL_bypass(CABAC_decoder* decoder, int nBits);
48 int decode_CABAC_TR_bypass(CABAC_decoder* decoder, int cRiceParam, int cTRMax);
50 
51 
52 // ---------------------------------------------------------------------------
53 
55 {
56 public:
57  CABAC_encoder() : mCtxModels(NULL) { }
58  virtual ~CABAC_encoder() { }
59 
60  virtual int size() const = 0;
61  virtual void reset() = 0;
62 
63  // --- VLC ---
64 
65  virtual void write_bits(uint32_t bits,int n) = 0;
66  virtual void write_bit(int bit) { write_bits(bit,1); }
67  virtual void write_uvlc(int value);
68  virtual void write_svlc(int value);
69  virtual void write_startcode() = 0;
70  virtual void skip_bits(int nBits) = 0;
71 
72  virtual void add_trailing_bits();
73  virtual int number_free_bits_in_byte() const = 0;
74 
75  // output all remaining bits and fill with zeros to next byte boundary
76  virtual void flush_VLC() { }
77 
78 
79  // --- CABAC ---
80 
82 
83  virtual void init_CABAC() { }
84  virtual void write_CABAC_bit(int modelIdx, int bit) = 0;
85  virtual void write_CABAC_bypass(int bit) = 0;
86  virtual void write_CABAC_TU_bypass(int value, int cMax);
87  virtual void write_CABAC_FL_bypass(int value, int nBits);
88  virtual void write_CABAC_term_bit(int bit) = 0;
89  virtual void flush_CABAC() { }
90 
91  void write_CABAC_EGk(int absolute_symbol, int k); // absolute_symbol >= 0
92 
93  virtual bool modifies_context() const = 0;
94 
95  float RDBits_for_CABAC_bin(int modelIdx, int bit);
96 
97  protected:
99 };
100 
101 
103 {
104 public:
107 
108  virtual void reset();
109 
110  virtual int size() const { return data_size; }
111  uint8_t* data() const { return data_mem; }
112 
113  // --- VLC ---
114 
115  virtual void write_bits(uint32_t bits,int n);
116  virtual void write_startcode();
117  virtual void skip_bits(int nBits);
118 
119  virtual int number_free_bits_in_byte() const;
120 
121  // output all remaining bits and fill with zeros to next byte boundary
122  virtual void flush_VLC();
123 
124 
125  // --- CABAC ---
126 
127  virtual void init_CABAC();
128  virtual void write_CABAC_bit(int modelIdx, int bit);
129  virtual void write_CABAC_bypass(int bit);
130  virtual void write_CABAC_term_bit(int bit);
131  virtual void flush_CABAC();
132 
133  virtual bool modifies_context() const { return true; }
134 
135 private:
136  // data buffer
137 
138  uint8_t* data_mem;
139  uint32_t data_capacity;
140  uint32_t data_size;
141  char state; // for inserting emulation-prevention bytes
142 
143  // VLC
144 
145  uint32_t vlc_buffer;
146  uint32_t vlc_buffer_len;
147 
148 
149  // CABAC
150 
151  uint32_t range;
152  uint32_t low;
153  int8_t bits_left;
154  uint8_t buffered_byte;
155  uint16_t num_buffered_bytes;
156 
157 
158  void check_size_and_resize(int nBytes);
159  void testAndWriteOut();
160  void write_out();
161  void append_byte(int byte);
162 };
163 
164 
166 {
167 public:
169 
170  virtual void reset() { mFracBits=0; }
171 
172  virtual int size() const { return mFracBits>>(15+3); }
173 
174  uint64_t getFracBits() const { return mFracBits; }
175  float getRDBits() const { return mFracBits / float(1<<15); }
176 
177  // --- VLC ---
178 
179  virtual void write_bits(uint32_t bits,int n) { mFracBits += n<<15; }
180  virtual void write_bit(int bit) { mFracBits+=1<<15; }
181  virtual void write_startcode() { mFracBits += (1<<15)*8*3; }
182  virtual void skip_bits(int nBits) { mFracBits += nBits<<15; }
183  virtual int number_free_bits_in_byte() const { return 0; } // TODO, good enough for now
184 
185  // --- CABAC ---
186 
187  virtual void write_CABAC_bit(int modelIdx, int bit);
188  virtual void write_CABAC_bypass(int bit) {
189  mFracBits += 0x8000;
190  }
191  virtual void write_CABAC_FL_bypass(int value, int nBits) {
192  mFracBits += nBits<<15;
193  }
194  virtual void write_CABAC_term_bit(int bit) { /* not implemented (not needed) */ }
195 
196  virtual bool modifies_context() const { return true; }
197 
198  protected:
199  uint64_t mFracBits;
200 };
201 
202 
204 {
205  public:
206  void write_CABAC_bit(int modelIdx, int bit);
207 
208  virtual bool modifies_context() const { return false; }
209 };
210 
211 #endif
int decode_CABAC_TR_bypass(CABAC_decoder *decoder, int cRiceParam, int cTRMax)
void init_CABAC_decoder_2(CABAC_decoder *decoder)
int decode_CABAC_term_bit(CABAC_decoder *decoder)
void init_CABAC_decoder(CABAC_decoder *decoder, uint8_t *bitstream, int length)
int decode_CABAC_bypass(CABAC_decoder *decoder)
int decode_CABAC_TU(CABAC_decoder *decoder, int cMax, context_model *model)
int decode_CABAC_EGk_bypass(CABAC_decoder *decoder, int k)
int decode_CABAC_FL_bypass(CABAC_decoder *decoder, int nBits)
int decode_CABAC_bit(CABAC_decoder *decoder, context_model *model)
int decode_CABAC_TU_bypass(CABAC_decoder *decoder, int cMax)
Definition: cabac.h:103
virtual void write_CABAC_bypass(int bit)
virtual void init_CABAC()
virtual void flush_VLC()
virtual void flush_CABAC()
virtual int size() const
Definition: cabac.h:110
virtual void write_CABAC_term_bit(int bit)
virtual bool modifies_context() const
Definition: cabac.h:133
virtual void reset()
virtual void write_bits(uint32_t bits, int n)
uint8_t * data() const
Definition: cabac.h:111
virtual void write_startcode()
virtual void skip_bits(int nBits)
virtual int number_free_bits_in_byte() const
virtual void write_CABAC_bit(int modelIdx, int bit)
Definition: cabac.h:204
virtual bool modifies_context() const
Definition: cabac.h:208
void write_CABAC_bit(int modelIdx, int bit)
Definition: cabac.h:166
virtual void reset()
Definition: cabac.h:170
virtual int number_free_bits_in_byte() const
Definition: cabac.h:183
virtual void write_startcode()
Definition: cabac.h:181
virtual void write_CABAC_bypass(int bit)
Definition: cabac.h:188
virtual void write_CABAC_bit(int modelIdx, int bit)
virtual bool modifies_context() const
Definition: cabac.h:196
virtual void write_bits(uint32_t bits, int n)
Definition: cabac.h:179
virtual void write_bit(int bit)
Definition: cabac.h:180
float getRDBits() const
Definition: cabac.h:175
CABAC_encoder_estim()
Definition: cabac.h:168
virtual void write_CABAC_FL_bypass(int value, int nBits)
Definition: cabac.h:191
virtual int size() const
Definition: cabac.h:172
virtual void skip_bits(int nBits)
Definition: cabac.h:182
uint64_t getFracBits() const
Definition: cabac.h:174
uint64_t mFracBits
Definition: cabac.h:199
virtual void write_CABAC_term_bit(int bit)
Definition: cabac.h:194
Definition: cabac.h:55
virtual ~CABAC_encoder()
Definition: cabac.h:58
virtual int number_free_bits_in_byte() const =0
virtual void write_bit(int bit)
Definition: cabac.h:66
virtual void write_CABAC_TU_bypass(int value, int cMax)
virtual bool modifies_context() const =0
virtual void reset()=0
virtual void write_startcode()=0
CABAC_encoder()
Definition: cabac.h:57
virtual void init_CABAC()
Definition: cabac.h:83
context_model_table * mCtxModels
Definition: cabac.h:98
virtual void flush_VLC()
Definition: cabac.h:76
void write_CABAC_EGk(int absolute_symbol, int k)
virtual int size() const =0
virtual void skip_bits(int nBits)=0
virtual void write_CABAC_FL_bypass(int value, int nBits)
virtual void write_CABAC_bit(int modelIdx, int bit)=0
virtual void write_svlc(int value)
virtual void flush_CABAC()
Definition: cabac.h:89
virtual void add_trailing_bits()
virtual void write_bits(uint32_t bits, int n)=0
virtual void write_CABAC_term_bit(int bit)=0
virtual void write_CABAC_bypass(int bit)=0
virtual void write_uvlc(int value)
void set_context_models(context_model_table *models)
Definition: cabac.h:81
float RDBits_for_CABAC_bin(int modelIdx, int bit)
Definition: contextmodel.h:100
qulonglong value
Definition: itemviewutilities.cpp:592
Definition: cabac.h:28
uint32_t range
Definition: cabac.h:33
uint8_t * bitstream_end
Definition: cabac.h:31
uint8_t * bitstream_start
Definition: cabac.h:29
uint8_t * bitstream_curr
Definition: cabac.h:30
uint32_t value
Definition: cabac.h:34
int16_t bits_needed
Definition: cabac.h:35
Definition: contextmodel.h:34