From f3acd245ae4a1c2e86854137e8e6349fa93439dd Mon Sep 17 00:00:00 2001 From: Marko Viitanen Date: Wed, 20 Mar 2019 15:17:54 +0200 Subject: [PATCH] New cabac coding function: kvz_cabac_encode_trunc_bin --- src/cabac.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/cabac.h | 1 + 2 files changed, 42 insertions(+) diff --git a/src/cabac.c b/src/cabac.c index 95c11616..7d17a500 100644 --- a/src/cabac.c +++ b/src/cabac.c @@ -76,6 +76,17 @@ const uint8_t kvz_g_auc_renorm_table[32] = 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 }; +const uint8_t kvz_tb_max[257] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, +4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, +5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, +6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, +6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, +7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, +7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, +7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, +7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, +7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8 }; + /** * \brief Initialize struct cabac_data. @@ -214,6 +225,36 @@ void kvz_cabac_encode_bin_trm(cabac_data_t * const data, const uint8_t bin_value } } +/** + * \brief encode truncated binary code + */ +void kvz_cabac_encode_trunc_bin(cabac_data_t * const data, const uint32_t bin_value, const uint32_t max_value) { + int thresh; + int symbol = bin_value; + if (max_value > 256) { + int threshVal = 1 << 8; + thresh = 8; + while (threshVal <= max_value) { + thresh++; + threshVal <<= 1; + } + thresh--; + } else { + thresh = kvz_tb_max[max_value]; + } + + int val = 1 << thresh; + + int b = max_value - val; + if (symbol < val - b) { + CABAC_BINS_EP(data, symbol, thresh, "TruncSymbols"); + } else { + symbol += val - b; + CABAC_BINS_EP(data, symbol, thresh + 1, "TruncSymbols"); + } +} + + /** * \brief */ diff --git a/src/cabac.h b/src/cabac.h index 3cbb6f00..bca585f8 100644 --- a/src/cabac.h +++ b/src/cabac.h @@ -104,6 +104,7 @@ extern const uint8_t kvz_g_auc_renorm_table[32]; void kvz_cabac_start(cabac_data_t *data); void kvz_cabac_encode_bin(cabac_data_t *data, uint32_t bin_value); void kvz_cabac_encode_bin_ep(cabac_data_t *data, uint32_t bin_value); +void kvz_cabac_encode_trunc_bin(cabac_data_t *data, uint32_t bin_value, uint32_t max_value); void kvz_cabac_encode_bins_ep(cabac_data_t *data, uint32_t bin_values, int num_bins); void kvz_cabac_encode_bin_trm(cabac_data_t *data, uint8_t bin_value); void kvz_cabac_write(cabac_data_t *data);