2013-09-19 07:35:34 +00:00
|
|
|
#ifndef CABAC_H_
|
|
|
|
#define CABAC_H_
|
2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-03-06 16:14:01 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
2014-01-24 10:37:15 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
2014-01-24 10:37:15 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
|
|
|
* more details.
|
2014-01-24 10:37:15 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-24 10:37:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-12-17 11:42:57 +00:00
|
|
|
/**
|
|
|
|
* \ingroup CABAC
|
2013-09-18 14:29:30 +00:00
|
|
|
* \file
|
2015-12-17 11:42:57 +00:00
|
|
|
* Coding bins using CABAC.
|
2012-06-04 10:47:12 +00:00
|
|
|
*/
|
|
|
|
|
2016-03-30 09:41:37 +00:00
|
|
|
#include "global.h" // IWYU pragma: keep
|
2013-09-18 09:16:03 +00:00
|
|
|
|
|
|
|
#include "bitstream.h"
|
|
|
|
|
2016-06-07 08:28:30 +00:00
|
|
|
struct encoder_state_t;
|
2013-09-18 09:16:03 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
// Types
|
2014-04-04 08:50:07 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t uc_state;
|
2015-03-04 11:26:48 +00:00
|
|
|
} cabac_ctx_t;
|
2014-04-04 08:50:07 +00:00
|
|
|
|
2012-06-04 10:47:12 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2015-03-04 11:26:48 +00:00
|
|
|
cabac_ctx_t *cur_ctx;
|
2013-09-19 08:05:42 +00:00
|
|
|
uint32_t low;
|
|
|
|
uint32_t range;
|
|
|
|
uint32_t buffered_byte;
|
2013-09-18 11:06:45 +00:00
|
|
|
int32_t num_buffered_bytes;
|
|
|
|
int32_t bits_left;
|
2014-04-04 08:49:31 +00:00
|
|
|
int8_t only_count;
|
2015-03-04 11:20:18 +00:00
|
|
|
bitstream_t *stream;
|
2014-04-04 13:04:44 +00:00
|
|
|
|
2014-04-04 08:50:07 +00:00
|
|
|
// CONTEXTS
|
2014-09-23 21:58:17 +00:00
|
|
|
struct {
|
2015-03-04 11:26:48 +00:00
|
|
|
cabac_ctx_t sao_merge_flag_model;
|
|
|
|
cabac_ctx_t sao_type_idx_model;
|
|
|
|
cabac_ctx_t split_flag_model[3]; //!< \brief split flag context models
|
|
|
|
cabac_ctx_t intra_mode_model; //!< \brief intra mode context models
|
|
|
|
cabac_ctx_t chroma_pred_model[2];
|
2015-03-10 07:37:25 +00:00
|
|
|
cabac_ctx_t inter_dir[5];
|
2015-03-04 11:26:48 +00:00
|
|
|
cabac_ctx_t trans_subdiv_model[3]; //!< \brief intra mode context models
|
|
|
|
cabac_ctx_t qt_cbf_model_luma[4];
|
|
|
|
cabac_ctx_t qt_cbf_model_chroma[4];
|
2016-08-21 04:36:13 +00:00
|
|
|
cabac_ctx_t cu_qp_delta_abs[4];
|
2015-03-04 11:26:48 +00:00
|
|
|
cabac_ctx_t part_size_model[4];
|
|
|
|
cabac_ctx_t cu_sig_coeff_group_model[4];
|
|
|
|
cabac_ctx_t cu_sig_model_luma[27];
|
|
|
|
cabac_ctx_t cu_sig_model_chroma[15];
|
|
|
|
cabac_ctx_t cu_ctx_last_y_luma[15];
|
|
|
|
cabac_ctx_t cu_ctx_last_y_chroma[15];
|
|
|
|
cabac_ctx_t cu_ctx_last_x_luma[15];
|
|
|
|
cabac_ctx_t cu_ctx_last_x_chroma[15];
|
|
|
|
cabac_ctx_t cu_one_model_luma[16];
|
|
|
|
cabac_ctx_t cu_one_model_chroma[8];
|
|
|
|
cabac_ctx_t cu_abs_model_luma[4];
|
|
|
|
cabac_ctx_t cu_abs_model_chroma[2];
|
|
|
|
cabac_ctx_t cu_pred_mode_model;
|
|
|
|
cabac_ctx_t cu_skip_flag_model[3];
|
|
|
|
cabac_ctx_t cu_merge_idx_ext_model;
|
|
|
|
cabac_ctx_t cu_merge_flag_ext_model;
|
2016-05-30 07:41:53 +00:00
|
|
|
cabac_ctx_t cu_transquant_bypass;
|
2015-03-04 11:26:48 +00:00
|
|
|
cabac_ctx_t cu_mvd_model[2];
|
|
|
|
cabac_ctx_t cu_ref_pic_model[2];
|
|
|
|
cabac_ctx_t mvp_idx_model[2];
|
|
|
|
cabac_ctx_t cu_qt_root_cbf_model;
|
|
|
|
cabac_ctx_t transform_skip_model_luma;
|
|
|
|
cabac_ctx_t transform_skip_model_chroma;
|
2014-09-23 21:58:17 +00:00
|
|
|
} ctx;
|
2015-03-04 11:27:32 +00:00
|
|
|
} cabac_data_t;
|
2012-06-04 10:47:12 +00:00
|
|
|
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
// Globals
|
2015-08-26 08:50:27 +00:00
|
|
|
extern const uint8_t kvz_g_auc_next_state_mps[128];
|
|
|
|
extern const uint8_t kvz_g_auc_next_state_lps[128];
|
|
|
|
extern const uint8_t kvz_g_auc_lpst_table[64][4];
|
|
|
|
extern const uint8_t kvz_g_auc_renorm_table[32];
|
2012-06-04 10:47:12 +00:00
|
|
|
|
2012-06-13 15:08:15 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
// Functions
|
2015-08-26 08:50:27 +00:00
|
|
|
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_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);
|
|
|
|
void kvz_cabac_finish(cabac_data_t *data);
|
|
|
|
void kvz_cabac_write_coeff_remain(cabac_data_t *cabac, uint32_t symbol,
|
2013-09-19 09:28:57 +00:00
|
|
|
uint32_t r_param);
|
2016-06-07 12:10:27 +00:00
|
|
|
void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, cabac_data_t * const cabac, const uint32_t symbol,
|
2020-01-27 11:11:31 +00:00
|
|
|
const uint32_t r_param, int32_t base_level);
|
2016-06-07 08:28:30 +00:00
|
|
|
void kvz_cabac_write_ep_ex_golomb(struct encoder_state_t * const state, cabac_data_t *data,
|
2020-01-27 11:11:31 +00:00
|
|
|
uint32_t symbol, uint32_t count);
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_cabac_write_unary_max_symbol(cabac_data_t *data, cabac_ctx_t *ctx,
|
2014-03-06 16:14:01 +00:00
|
|
|
uint32_t symbol, int32_t offset,
|
2013-09-19 09:28:57 +00:00
|
|
|
uint32_t max_symbol);
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_cabac_write_unary_max_symbol_ep(cabac_data_t *data, unsigned int symbol, unsigned int max_symbol);
|
2013-09-19 09:28:57 +00:00
|
|
|
|
2013-09-19 09:37:27 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
// Macros
|
2016-06-22 02:38:36 +00:00
|
|
|
#define CTX_STATE(ctx) ((ctx)->uc_state >> 1)
|
|
|
|
#define CTX_MPS(ctx) ((ctx)->uc_state & 1)
|
2015-08-26 08:50:27 +00:00
|
|
|
#define CTX_UPDATE_LPS(ctx) { (ctx)->uc_state = kvz_g_auc_next_state_lps[ (ctx)->uc_state ]; }
|
|
|
|
#define CTX_UPDATE_MPS(ctx) { (ctx)->uc_state = kvz_g_auc_next_state_mps[ (ctx)->uc_state ]; }
|
2014-01-16 15:13:48 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
#ifdef VERBOSE
|
|
|
|
#define CABAC_BIN(data, value, name) { \
|
|
|
|
uint32_t prev_state = (data)->ctx->uc_state; \
|
2016-06-22 02:38:36 +00:00
|
|
|
kvz_cabac_encode_bin((data), (value)) \
|
2014-05-15 19:55:06 +00:00
|
|
|
printf("%s = %u, state = %u -> %u\n", \
|
2016-06-22 02:38:36 +00:00
|
|
|
(name), (uint32_t)(value), prev_state, (data)->ctx->uc_state); }
|
2013-09-19 09:28:57 +00:00
|
|
|
|
|
|
|
#define CABAC_BINS_EP(data, value, bins, name) { \
|
|
|
|
uint32_t prev_state = (data)->ctx->uc_state; \
|
2016-06-22 02:38:36 +00:00
|
|
|
kvz_cabac_encode_bins_ep((data), (value), (bins)); \
|
2014-05-15 19:55:06 +00:00
|
|
|
printf("%s = %u(%u bins), state = %u -> %u\n", \
|
2016-06-22 02:38:36 +00:00
|
|
|
(name), (uint32_t)(value), (bins), prev_state, (data)->ctx->uc_state); }
|
2013-09-19 09:28:57 +00:00
|
|
|
|
|
|
|
#define CABAC_BIN_EP(data, value, name) { \
|
|
|
|
uint32_t prev_state = (data)->ctx->uc_state; \
|
2016-06-22 02:38:36 +00:00
|
|
|
kvz_cabac_encode_bin_ep((data), (value)); \
|
2014-05-15 19:55:06 +00:00
|
|
|
printf("%s = %u, state = %u -> %u\n", \
|
2016-06-22 02:38:36 +00:00
|
|
|
(name), (uint32_t)(value), prev_state, (data)->ctx->uc_state); }
|
2012-06-07 14:38:28 +00:00
|
|
|
#else
|
2013-09-19 09:28:57 +00:00
|
|
|
#define CABAC_BIN(data, value, name) \
|
2016-06-22 02:38:36 +00:00
|
|
|
kvz_cabac_encode_bin((data), (value));
|
2013-09-19 09:28:57 +00:00
|
|
|
#define CABAC_BINS_EP(data, value, bins, name) \
|
2016-06-22 02:38:36 +00:00
|
|
|
kvz_cabac_encode_bins_ep((data), (value), (bins));
|
2013-09-19 09:28:57 +00:00
|
|
|
#define CABAC_BIN_EP(data, value, name) \
|
2016-06-22 02:38:36 +00:00
|
|
|
kvz_cabac_encode_bin_ep((data), (value));
|
2012-06-07 14:38:28 +00:00
|
|
|
#endif
|
|
|
|
|
2012-06-04 10:47:12 +00:00
|
|
|
#endif
|