Refactor: cabac.c/.h full cleanup.

This commit is contained in:
Ari Koivula 2013-09-19 12:28:57 +03:00
parent 32c984159f
commit b25abe7c0b
2 changed files with 209 additions and 207 deletions

View file

@ -57,42 +57,60 @@ const uint8_t g_auc_lpst_table[64][4] =
{ 6, 8, 9, 11}, { 6, 7, 9, 10}, { 6, 7, 8, 9}, { 2, 2, 2, 2} { 6, 8, 9, 11}, { 6, 7, 9, 10}, { 6, 7, 8, 9}, { 2, 2, 2, 2}
}; };
const uint8_t g_auc_renorm_table[32] = { 6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, const uint8_t g_auc_renorm_table[32] =
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; {
6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
uint8_t g_next_state[128][2]; uint8_t g_next_state[128][2];
cabac_data cabac; cabac_data cabac;
/**
* \brief Initialize struct cabac_ctx.
*/
void ctx_init(cabac_ctx *ctx, uint32_t qp, uint32_t init_value) void ctx_init(cabac_ctx *ctx, uint32_t qp, uint32_t init_value)
{ {
int slope = (init_value >> 4) * 5 - 45; int slope = (init_value >> 4) * 5 - 45;
int offset = ((init_value & 15) << 3) - 16; int offset = ((init_value & 15) << 3) - 16;
int init_state = MIN( MAX( 1, ( ( ( slope * (int)qp ) >> 4 ) + offset ) ), 126 ); int init_state = MIN(MAX(1, ((slope * (int)qp) >> 4) + offset), 126);
uint8_t mp_state = (init_state >= 64) ? 1 : 0; uint8_t mp_state = (init_state >= 64) ? 1 : 0;
ctx->uc_state = ( (mp_state? (init_state - 64):(63 - init_state)) <<1) + mp_state;
if (mp_state) {
ctx->uc_state = (init_state - 64) << 1 + mp_state;
} else {
ctx->uc_state = (63 - init_state) << 1;
}
ctx->bins_coded = 0; ctx->bins_coded = 0;
} }
/**
* \brief Initialize global g_next_state array.
*/
void ctx_build_next_state_table() void ctx_build_next_state_table()
{ {
int i, j; int i, j;
for (i = 0; i < 128; i++)
{ for (i = 0; i < 128; i++) {
for (j = 0; j < 2; j++) for (j = 0; j < 2; j++) {
{ if ((i & 1) == j) {
g_next_state[i][j] = ((i&1) == j) ? g_auc_next_state_mps[i] : g_auc_next_state_lps[i]; g_next_state[i][j] = g_auc_next_state_mps[i];
} else {
g_next_state[i][j] = g_auc_next_state_lps[i];
}
} }
} }
} }
INLINE void ctx_update(cabac_ctx* ctx, int val ) { ctx->uc_state = g_next_state[ctx->uc_state][val]; } INLINE void ctx_update(cabac_ctx *ctx, int val)
//void ctx_update_LPS(cabac_ctx* ctx) { ctx->ucState = g_aucNextStateLPS[ ctx->ucState ]; } {
//void ctx_update_MPS(cabac_ctx* ctx) { ctx->ucState = g_aucNextStateMPS[ ctx->ucState ]; } ctx->uc_state = g_next_state[ctx->uc_state][val];
}
/**
* \brief Initialize struct cabac_data.
*/
void cabac_init(cabac_data* data) void cabac_init(cabac_data* data)
{ {
data->frac_bits = 0; data->frac_bits = 0;
@ -101,6 +119,9 @@ void cabac_init(cabac_data* data)
ctx_build_next_state_table(); ctx_build_next_state_table();
} }
/**
* \brief Initialize struct cabac_data.
*/
void cabac_start(cabac_data *data) void cabac_start(cabac_data *data)
{ {
data->low = 0; data->low = 0;
@ -110,109 +131,89 @@ void cabac_start(cabac_data* data)
data->buffered_byte = 0xff; data->buffered_byte = 0xff;
} }
/**
* \brief
*/
void cabac_encode_bin(cabac_data *data, uint32_t bin_value) void cabac_encode_bin(cabac_data *data, uint32_t bin_value)
{ {
uint32_t lps; uint32_t lps;
//printf("\tdecodeBin m_uiRange %d uivalue %d\n", data->uiRange, data->uiLow);
data->bins_coded += data->bin_count_increment; data->bins_coded += data->bin_count_increment;
data->ctx->bins_coded = 1; data->ctx->bins_coded = 1;
lps = g_auc_lpst_table[CTX_STATE(data->ctx)][(data->range >> 6) & 3]; lps = g_auc_lpst_table[CTX_STATE(data->ctx)][(data->range >> 6) & 3];
data->range -= lps; data->range -= lps;
#ifdef _DEBUG
//printf("\tencodeBin m_uiRange %d uiLPS %d m_uiValue %d ", data->uiRange,uiLPS,data->uiLow);
#endif
// Not the Most Probable Symbol? // Not the Most Probable Symbol?
if( bin_value != CTX_MPS(data->ctx) ) if (bin_value != CTX_MPS(data->ctx)) {
{
int num_bits = g_auc_renorm_table[lps >> 3]; int num_bits = g_auc_renorm_table[lps >> 3];
data->low = (data->low + data->range) << num_bits; data->low = (data->low + data->range) << num_bits;
data->range = lps << num_bits; data->range = lps << num_bits;
ctx_update_LPS(data->ctx); CTX_UPDATE_LPS(data->ctx);
data->bits_left -= num_bits; data->bits_left -= num_bits;
} } else {
else CTX_UPDATE_MPS(data->ctx);
{ if (data->range >= 256) return;
ctx_update_MPS(data->ctx);
if ( data->range >= 256 )
{
#ifdef _DEBUG
//printf("enduiValue %d \n",data->uiLow);
#endif
return;
}
data->low <<= 1; data->low <<= 1;
data->range <<= 1; data->range <<= 1;
data->bits_left--; data->bits_left--;
} }
if(data->bits_left < 12) if (data->bits_left < 12) {
{
cabac_write(data); cabac_write(data);
} }
#ifdef _DEBUG
//printf("enduiValue %d \n",data->uiLow);
#endif
} }
/**
* \brief
*/
void cabac_write(cabac_data *data) void cabac_write(cabac_data *data)
{ {
uint32_t lead_byte = data->low >> (24 - data->bits_left); uint32_t lead_byte = data->low >> (24 - data->bits_left);
data->bits_left += 8; data->bits_left += 8;
data->low &= 0xffffffffu >> data->bits_left; data->low &= 0xffffffffu >> data->bits_left;
if ( lead_byte == 0xff ) if (lead_byte == 0xff) {
{
data->num_buffered_bytes++; data->num_buffered_bytes++;
} } else {
else if (data->num_buffered_bytes > 0) {
{
if ( data->num_buffered_bytes > 0 )
{
uint32_t carry = lead_byte >> 8; uint32_t carry = lead_byte >> 8;
uint32_t byte = data->buffered_byte + carry; uint32_t byte = data->buffered_byte + carry;
data->buffered_byte = lead_byte & 0xff; data->buffered_byte = lead_byte & 0xff;
bitstream_put(data->stream, byte, 8); bitstream_put(data->stream, byte, 8);
byte = (0xff + carry) & 0xff; byte = (0xff + carry) & 0xff;
while ( data->num_buffered_bytes > 1 ) while (data->num_buffered_bytes > 1) {
{
bitstream_put(data->stream, byte, 8); bitstream_put(data->stream, byte, 8);
data->num_buffered_bytes--; data->num_buffered_bytes--;
} }
} } else {
else
{
data->num_buffered_bytes = 1; data->num_buffered_bytes = 1;
data->buffered_byte = lead_byte; data->buffered_byte = lead_byte;
} }
} }
} }
/**
* \brief
*/
void cabac_finish(cabac_data *data) void cabac_finish(cabac_data *data)
{ {
if ( data->low >> ( 32 - data->bits_left ) ) if (data->low >> (32 - data->bits_left)) {
{
bitstream_put(data->stream,data->buffered_byte + 1, 8); bitstream_put(data->stream,data->buffered_byte + 1, 8);
while ( data->num_buffered_bytes > 1 ) while (data->num_buffered_bytes > 1) {
{
bitstream_put(data->stream, 0, 8); bitstream_put(data->stream, 0, 8);
data->num_buffered_bytes--; data->num_buffered_bytes--;
} }
data->low -= 1 << (32 - data->bits_left); data->low -= 1 << (32 - data->bits_left);
} } else {
else if (data->num_buffered_bytes > 0) {
{
if ( data->num_buffered_bytes > 0 )
{
bitstream_put(data->stream,data->buffered_byte, 8); bitstream_put(data->stream,data->buffered_byte, 8);
} }
while ( data->num_buffered_bytes > 1 ) while (data->num_buffered_bytes > 1) {
{
bitstream_put(data->stream, 0xff, 8); bitstream_put(data->stream, 0xff, 8);
data->num_buffered_bytes--; data->num_buffered_bytes--;
} }
@ -226,35 +227,29 @@ void cabac_finish(cabac_data* data)
*/ */
void cabac_encode_bin_trm(cabac_data *data, uint8_t bin_value) void cabac_encode_bin_trm(cabac_data *data, uint8_t bin_value)
{ {
#ifdef _DEBUG
//printf("\tencodeBinTrm m_uiRange %d uivalue %d\n", data->uiRange, data->uiLow);
#endif
data->bins_coded += data->bin_count_increment; data->bins_coded += data->bin_count_increment;
data->range -= 2; data->range -= 2;
if( bin_value ) if(bin_value) {
{
data->low += data->range; data->low += data->range;
data->low <<= 7; data->low <<= 7;
data->range = 2 << 7; data->range = 2 << 7;
data->bits_left -= 7; data->bits_left -= 7;
} } else if (data->range >= 256) {
else if ( data->range >= 256 )
{
return; return;
} } else {
else
{
data->low <<= 1; data->low <<= 1;
data->range <<= 1; data->range <<= 1;
data->bits_left--; data->bits_left--;
} }
if(data->bits_left < 12) if (data->bits_left < 12) {
{
cabac_write(data); cabac_write(data);
} }
} }
/**
* \brief
*/
void cabac_flush(cabac_data *data) void cabac_flush(cabac_data *data)
{ {
cabac_encode_bin_trm(data, 1); cabac_encode_bin_trm(data, 1);
@ -264,29 +259,32 @@ void cabac_flush(cabac_data* data)
cabac_start(data); cabac_start(data);
} }
/**
* \brief
*/
void cabac_encode_bin_ep(cabac_data *data, uint32_t bin_value) void cabac_encode_bin_ep(cabac_data *data, uint32_t bin_value)
{ {
data->bins_coded += data->bin_count_increment; data->bins_coded += data->bin_count_increment;
data->low <<= 1; data->low <<= 1;
if( bin_value ) if (bin_value) {
{
data->low += data->range; data->low += data->range;
} }
data->bits_left--; data->bits_left--;
if(data->bits_left < 12) if (data->bits_left < 12) {
{
cabac_write(data); cabac_write(data);
} }
} }
/**
* \brief
*/
void cabac_encode_bins_ep(cabac_data *data, uint32_t bin_values, int num_bins) void cabac_encode_bins_ep(cabac_data *data, uint32_t bin_values, int num_bins)
{ {
uint32_t pattern; uint32_t pattern;
data->bins_coded += num_bins & -data->bin_count_increment; data->bins_coded += num_bins & -data->bin_count_increment;
while ( num_bins > 8 ) while (num_bins > 8) {
{
num_bins -= 8; num_bins -= 8;
pattern = bin_values >> num_bins; pattern = bin_values >> num_bins;
data->low <<= 8; data->low <<= 8;
@ -294,8 +292,7 @@ void cabac_encode_bins_ep(cabac_data* data, uint32_t bin_values, int num_bins )
bin_values -= pattern << num_bins; bin_values -= pattern << num_bins;
data->bits_left -= 8; data->bits_left -= 8;
if(data->bits_left < 12) if(data->bits_left < 12) {
{
cabac_write(data); cabac_write(data);
} }
} }
@ -304,84 +301,76 @@ void cabac_encode_bins_ep(cabac_data* data, uint32_t bin_values, int num_bins )
data->low += data->range * bin_values; data->low += data->range * bin_values;
data->bits_left -= num_bins; data->bits_left -= num_bins;
if(data->bits_left < 12) if (data->bits_left < 12) {
{
cabac_write(data); cabac_write(data);
} }
} }
/**
* \brief Coding of coeff_abs_level_minus3.
/*! * \param symbol Value of coeff_abs_level_minus3.
\brief Coding of coeff_abs_level_minus3 * \param r_param Reference to Rice parameter.
\param uiSymbol value of coeff_abs_level_minus3
\param ruiGoRiceParam reference to Rice parameter
\returns Void
*/ */
void cabac_write_coeff_remain(cabac_data *cabac, uint32_t symbol, uint32_t r_param) void cabac_write_coeff_remain(cabac_data *cabac, uint32_t symbol, uint32_t r_param)
{ {
int32_t code_number = symbol; int32_t code_number = symbol;
uint32_t length; uint32_t length;
if (code_number < (3 << r_param)) if (code_number < (3 << r_param)) {
{
length = code_number >> r_param; length = code_number >> r_param;
cabac_encode_bins_ep(cabac, (1 << (length + 1)) - 2 , length + 1); cabac_encode_bins_ep(cabac, (1 << (length + 1)) - 2 , length + 1);
cabac_encode_bins_ep(cabac, (code_number % (1 << r_param)), r_param); cabac_encode_bins_ep(cabac, (code_number % (1 << r_param)), r_param);
} } else {
else
{
length = r_param; length = r_param;
code_number = code_number - (3 << r_param); code_number = code_number - (3 << r_param);
while (code_number >= (1<<length)) while (code_number >= (1 << length)) {
{ code_number -= 1 << length;
code_number -= (1<<(length++)); ++length;
} }
cabac_encode_bins_ep(cabac, (1 << (3 + length + 1 - r_param)) - 2, 3 + length + 1 - r_param); cabac_encode_bins_ep(cabac, (1 << (3 + length + 1 - r_param)) - 2, 3 + length + 1 - r_param);
cabac_encode_bins_ep(cabac, code_number, length); cabac_encode_bins_ep(cabac, code_number, length);
} }
} }
/**
* \brief
*/
void cabac_write_unary_max_symbol(cabac_data *data, cabac_ctx *ctx, uint32_t symbol, int32_t offset, uint32_t max_symbol) void cabac_write_unary_max_symbol(cabac_data *data, cabac_ctx *ctx, uint32_t symbol, int32_t offset, uint32_t max_symbol)
{ {
int8_t code_last = ( max_symbol > symbol ); int8_t code_last = max_symbol > symbol;
if (!max_symbol) if (!max_symbol) return;
return;
data->ctx = &ctx[0]; data->ctx = &ctx[0];
cabac_encode_bin(data, symbol ? 1 : 0); cabac_encode_bin(data, symbol ? 1 : 0);
if (!symbol) if (!symbol) return;
return;
while( --symbol ) while (--symbol) {
{
data->ctx = &ctx[offset]; data->ctx = &ctx[offset];
cabac_encode_bin(data, 1); cabac_encode_bin(data, 1);
} }
if( code_last ) if (code_last) {
{
data->ctx = &ctx[offset]; data->ctx = &ctx[offset];
cabac_encode_bin(data, 0); cabac_encode_bin(data, 0);
} }
return;
} }
/**
* \brief
*/
void cabac_write_ep_ex_golomb(cabac_data *data, uint32_t symbol, uint32_t count) void cabac_write_ep_ex_golomb(cabac_data *data, uint32_t symbol, uint32_t count)
{ {
uint32_t bins = 0; uint32_t bins = 0;
int32_t num_bins = 0; int32_t num_bins = 0;
while( symbol >= (uint32_t)(1<<count) ) while (symbol >= (uint32_t)(1 << count)) {
{
bins = 2 * bins + 1; bins = 2 * bins + 1;
num_bins++; ++num_bins;
symbol -= 1 << count; symbol -= 1 << count;
count ++; ++count;
} }
bins = 2 * bins + 0; bins = 2 * bins;
num_bins++; ++num_bins;
bins = (bins << count) | symbol; bins = (bins << count) | symbol;
num_bins += count; num_bins += count;

View file

@ -17,31 +17,13 @@
#include "bitstream.h" #include "bitstream.h"
extern const uint8_t g_auc_next_state_mps[ 128 ]; // Types
extern const uint8_t g_auc_next_state_lps[ 128 ];
extern uint8_t g_next_state[128][2];
extern const uint8_t g_auc_lpst_table[64][4];
extern const uint8_t g_auc_renorm_table[32];
typedef struct typedef struct
{ {
uint8_t uc_state; uint8_t uc_state;
uint32_t bins_coded; uint32_t bins_coded;
} cabac_ctx; } cabac_ctx;
#define CTX_STATE(ctx) (ctx->uc_state >> 1)
#define CTX_MPS(ctx) (ctx->uc_state & 1)
void ctx_init(cabac_ctx* ctx,uint32_t qp, uint32_t init_value );
void ctx_build_next_state_table();
void ctx_update(cabac_ctx* ctx, int val );
//void ctx_update_LPS(cabac_ctx* ctx);
//void ctx_update_MPS(cabac_ctx* ctx);
#define ctx_update_LPS(ctx) { (ctx)->uc_state = g_auc_next_state_lps[ (ctx)->uc_state ]; }
#define ctx_update_MPS(ctx) { (ctx)->uc_state = g_auc_next_state_mps[ (ctx)->uc_state ]; }
typedef struct typedef struct
{ {
cabac_ctx *ctx; cabac_ctx *ctx;
@ -56,8 +38,25 @@ typedef struct
bitstream *stream; bitstream *stream;
} cabac_data; } cabac_data;
// Globals
extern const uint8_t g_auc_next_state_mps[ 128 ];
extern const uint8_t g_auc_next_state_lps[ 128 ];
extern uint8_t g_next_state[128][2];
extern const uint8_t g_auc_lpst_table[64][4];
extern const uint8_t g_auc_renorm_table[32];
extern cabac_data cabac; extern cabac_data cabac;
// Functions
#define CTX_STATE(ctx) (ctx->uc_state >> 1)
#define CTX_MPS(ctx) (ctx->uc_state & 1)
void ctx_init(cabac_ctx* ctx, uint32_t qp, uint32_t init_value);
void ctx_build_next_state_table();
#define CTX_UPDATE_LPS(ctx) { (ctx)->uc_state = g_auc_next_state_lps[ (ctx)->uc_state ]; }
#define CTX_UPDATE_MPS(ctx) { (ctx)->uc_state = g_auc_next_state_mps[ (ctx)->uc_state ]; }
void cabac_start(cabac_data *data); void cabac_start(cabac_data *data);
void cabac_init(cabac_data *data); void cabac_init(cabac_data *data);
void cabac_encode_bin(cabac_data *data, uint32_t bin_value); void cabac_encode_bin(cabac_data *data, uint32_t bin_value);
@ -67,26 +66,40 @@ void cabac_encode_bin_trm(cabac_data* data, uint8_t bin_value );
void cabac_write(cabac_data *data); void cabac_write(cabac_data *data);
void cabac_finish(cabac_data *data); void cabac_finish(cabac_data *data);
void cabac_flush(cabac_data *data); void cabac_flush(cabac_data *data);
void cabac_write_coeff_remain(cabac_data* cabac,uint32_t symbol, uint32_t r_param ); void cabac_write_coeff_remain(cabac_data *cabac, uint32_t symbol,
void cabac_write_ep_ex_golomb(cabac_data* data, uint32_t symbol, uint32_t count ); uint32_t r_param);
void cabac_write_unary_max_symbol(cabac_data* data,cabac_ctx* ctx, uint32_t symbol,int32_t offset, uint32_t max_symbol); void cabac_write_ep_ex_golomb(cabac_data *data, uint32_t symbol,
uint32_t count);
void cabac_write_unary_max_symbol(cabac_data *data, cabac_ctx *ctx,
uint32_t symbol, int32_t offset,
uint32_t max_symbol);
// Macros
#ifdef VERBOSE #ifdef VERBOSE
#define CABAC_BIN(data, value, name) { uint32_t prev_state = (data)->ctx->uc_state;\ #define CABAC_BIN(data, value, name) { \
cabac_encodeBin(data, value); \ uint32_t prev_state = (data)->ctx->uc_state; \
printf("%s = %d prev_state=%d state=%d\n",name,value,prev_state, (data)->ctx->uc_state);} cabac_encode_bin(data, value); \
printf("%s = %d prev_state=%d state=%d\n", \
name, value, prev_state, (data)->ctx->uc_state); }
#define CABAC_BINS_EP(data, value,bins, name) { uint32_t prev_state = (data)->ctx->uc_state;\ #define CABAC_BINS_EP(data, value, bins, name) { \
cabac_encodeBinsEP(data, value,bins); \ uint32_t prev_state = (data)->ctx->uc_state; \
printf("%s = %d prev_state=%d state=%d\n",name,value,prev_state, (data)->ctx->uc_state);} cabac_encode_bins_ep(data, value, bins); \
printf("%s = %d prev_state=%d state=%d\n", \
name, value, prev_state, (data)->ctx->uc_state); }
#define CABAC_BIN_EP(data, value, name) { uint32_t prev_state = (data)->ctx->uc_state;\ #define CABAC_BIN_EP(data, value, name) { \
cabac_encodeBinEP(data, value); \ uint32_t prev_state = (data)->ctx->uc_state; \
printf("%s = %d prev_state=%d state=%d\n",name,value,prev_state, (data)->ctx->uc_state);} cabac_encode_bin_ep(data, value); \
printf("%s = %d prev_state=%d state=%d\n", \
name, value, prev_state, (data)->ctx->uc_state); }
#else #else
#define CABAC_BIN(data, value, name) cabac_encode_bin(data, value); #define CABAC_BIN(data, value, name) \
#define CABAC_BINS_EP(data, value,bins, name) cabac_encode_bins_ep(data, value,bins); cabac_encode_bin(data, value);
#define CABAC_BIN_EP(data, value, name) cabac_encode_bin_ep(data, value); #define CABAC_BINS_EP(data, value, bins, name) \
cabac_encode_bins_ep(data, value, bins);
#define CABAC_BIN_EP(data, value, name) \
cabac_encode_bin_ep(data, value);
#endif #endif
#endif #endif