#ifndef DE265_CABAC_H
#define DE265_CABAC_H
#include <stdint.h>
#include "contextmodel.h"
typedef struct {
uint8_t* bitstream_start;
uint8_t* bitstream_curr;
uint8_t* bitstream_end;
uint32_t range;
uint32_t value;
int16_t bits_needed;
} CABAC_decoder;
void init_CABAC_decoder(CABAC_decoder* decoder, uint8_t* bitstream, int length);
void init_CABAC_decoder_2(CABAC_decoder* decoder);
int decode_CABAC_bit(CABAC_decoder* decoder, context_model* model);
int decode_CABAC_TU(CABAC_decoder* decoder, int cMax, context_model* model);
int decode_CABAC_term_bit(CABAC_decoder* decoder);
int decode_CABAC_bypass(CABAC_decoder* decoder);
int decode_CABAC_TU_bypass(CABAC_decoder* decoder, int cMax);
int decode_CABAC_FL_bypass(CABAC_decoder* decoder, int nBits);
int decode_CABAC_TR_bypass(CABAC_decoder* decoder, int cRiceParam, int cTRMax);
int decode_CABAC_EGk_bypass(CABAC_decoder* decoder, int k);
class CABAC_encoder
{
public:
CABAC_encoder() : mCtxModels(NULL) { }
virtual ~CABAC_encoder() { }
virtual int size() const = 0;
virtual void reset() = 0;
virtual void write_bits(uint32_t bits,int n) = 0;
virtual void write_bit(int bit) { write_bits(bit,1); }
virtual void write_uvlc(int value);
virtual void write_svlc(int value);
virtual void write_startcode() = 0;
virtual void skip_bits(int nBits) = 0;
virtual void add_trailing_bits();
virtual int number_free_bits_in_byte() const = 0;
virtual void flush_VLC() { }
void set_context_models(context_model_table* models) { mCtxModels=models; }
virtual void init_CABAC() { }
virtual void write_CABAC_bit(int modelIdx, int bit) = 0;
virtual void write_CABAC_bypass(int bit) = 0;
virtual void write_CABAC_TU_bypass(int value, int cMax);
virtual void write_CABAC_FL_bypass(int value, int nBits);
virtual void write_CABAC_term_bit(int bit) = 0;
virtual void flush_CABAC() { }
void write_CABAC_EGk(int absolute_symbol, int k);
virtual bool modifies_context() const = 0;
float RDBits_for_CABAC_bin(int modelIdx, int bit);
protected:
context_model_table* mCtxModels;
};
class CABAC_encoder_bitstream : public CABAC_encoder
{
public:
CABAC_encoder_bitstream();
~CABAC_encoder_bitstream();
virtual void reset();
virtual int size() const { return data_size; }
uint8_t* data() const { return data_mem; }
virtual void write_bits(uint32_t bits,int n);
virtual void write_startcode();
virtual void skip_bits(int nBits);
virtual int number_free_bits_in_byte() const;
virtual void flush_VLC();
virtual void init_CABAC();
virtual void write_CABAC_bit(int modelIdx, int bit);
virtual void write_CABAC_bypass(int bit);
virtual void write_CABAC_term_bit(int bit);
virtual void flush_CABAC();
virtual bool modifies_context() const { return true; }
private:
uint8_t* data_mem;
uint32_t data_capacity;
uint32_t data_size;
char state;
uint32_t vlc_buffer;
uint32_t vlc_buffer_len;
uint32_t range;
uint32_t low;
int8_t bits_left;
uint8_t buffered_byte;
uint16_t num_buffered_bytes;
void check_size_and_resize(int nBytes);
void testAndWriteOut();
void write_out();
void append_byte(int byte);
};
class CABAC_encoder_estim : public CABAC_encoder
{
public:
CABAC_encoder_estim() : mFracBits(0) { }
virtual void reset() { mFracBits=0; }
virtual int size() const { return mFracBits>>(15+3); }
uint64_t getFracBits() const { return mFracBits; }
float getRDBits() const { return mFracBits / float(1<<15); }
virtual void write_bits(uint32_t bits,int n) { mFracBits += n<<15; }
virtual void write_bit(int bit) { mFracBits+=1<<15; }
virtual void write_startcode() { mFracBits += (1<<15)*8*3; }
virtual void skip_bits(int nBits) { mFracBits += nBits<<15; }
virtual int number_free_bits_in_byte() const { return 0; }
virtual void write_CABAC_bit(int modelIdx, int bit);
virtual void write_CABAC_bypass(int bit) {
mFracBits += 0x8000;
}
virtual void write_CABAC_FL_bypass(int value, int nBits) {
mFracBits += nBits<<15;
}
virtual void write_CABAC_term_bit(int bit) { }
virtual bool modifies_context() const { return true; }
protected:
uint64_t mFracBits;
};
class CABAC_encoder_estim_constant : public CABAC_encoder_estim
{
public:
void write_CABAC_bit(int modelIdx, int bit);
virtual bool modifies_context() const { return false; }
};
#endif