2013-09-19 07:35:34 +00:00
|
|
|
#ifndef CABAC_H_
|
|
|
|
#define CABAC_H_
|
2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
2021-11-23 06:46:06 +00:00
|
|
|
* This file is part of uvg266 VVC encoder.
|
2014-03-06 16:14:01 +00:00
|
|
|
*
|
2021-10-07 08:32:59 +00:00
|
|
|
* Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
* other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
|
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
|
|
|
|
{
|
2019-03-20 07:06:26 +00:00
|
|
|
uint16_t state[2];
|
|
|
|
uint8_t rate;
|
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 {
|
2019-07-04 12:26:01 +00:00
|
|
|
cabac_ctx_t alf_ctb_flag_model[9];
|
2019-08-15 08:01:38 +00:00
|
|
|
cabac_ctx_t alf_latest_filt;
|
|
|
|
cabac_ctx_t alf_temporal_filt;
|
2019-10-02 09:25:02 +00:00
|
|
|
cabac_ctx_t alf_ctb_alternatives[2];
|
2019-08-15 08:01:38 +00:00
|
|
|
cabac_ctx_t alf_luma_coeff_delta_prediction_flag;
|
2021-01-05 09:43:15 +00:00
|
|
|
cabac_ctx_t alf_cc_filter_control_flag[6];
|
2015-03-04 11:26:48 +00:00
|
|
|
cabac_ctx_t sao_merge_flag_model;
|
|
|
|
cabac_ctx_t sao_type_idx_model;
|
2020-12-18 16:35:11 +00:00
|
|
|
cabac_ctx_t mts_idx_model[4];
|
2019-03-20 07:06:26 +00:00
|
|
|
cabac_ctx_t split_flag_model[9]; //!< \brief split flag context models
|
|
|
|
cabac_ctx_t qt_split_flag_model[6]; //!< \brief qt split flag context models
|
2019-08-19 12:17:25 +00:00
|
|
|
cabac_ctx_t intra_luma_mpm_flag_model; //!< \brief intra mode context models
|
2019-03-20 07:06:26 +00:00
|
|
|
cabac_ctx_t intra_subpart_model[2]; //!< \brief intra sub part context models
|
2019-08-22 07:44:47 +00:00
|
|
|
cabac_ctx_t chroma_pred_model;
|
2019-11-14 11:46:51 +00:00
|
|
|
cabac_ctx_t inter_dir[6];
|
2021-11-16 15:18:29 +00:00
|
|
|
cabac_ctx_t imv_flag[5];
|
2019-08-19 06:39:59 +00:00
|
|
|
cabac_ctx_t qt_cbf_model_luma[4];
|
2020-03-30 11:34:50 +00:00
|
|
|
cabac_ctx_t qt_cbf_model_cr[3];
|
|
|
|
cabac_ctx_t qt_cbf_model_cb[2];
|
2019-08-19 06:39:59 +00:00
|
|
|
cabac_ctx_t cu_qp_delta_abs[2];
|
2015-03-04 11:26:48 +00:00
|
|
|
cabac_ctx_t part_size_model[4];
|
2019-08-19 06:39:59 +00:00
|
|
|
cabac_ctx_t cu_sig_model_luma[3][12];
|
|
|
|
cabac_ctx_t cu_sig_model_chroma[3][8];
|
2018-08-29 12:57:40 +00:00
|
|
|
cabac_ctx_t cu_parity_flag_model_luma[21];
|
|
|
|
cabac_ctx_t cu_parity_flag_model_chroma[11];
|
|
|
|
cabac_ctx_t cu_gtx_flag_model_luma[2][21];
|
|
|
|
cabac_ctx_t cu_gtx_flag_model_chroma[2][11];
|
2019-08-19 06:39:59 +00:00
|
|
|
cabac_ctx_t cu_ctx_last_y_luma[20];
|
|
|
|
cabac_ctx_t cu_ctx_last_y_chroma[3];
|
|
|
|
cabac_ctx_t cu_ctx_last_x_luma[20];
|
|
|
|
cabac_ctx_t cu_ctx_last_x_chroma[3];
|
2020-10-13 09:04:31 +00:00
|
|
|
cabac_ctx_t cu_pred_mode_model[2];
|
2015-03-04 11:26:48 +00:00
|
|
|
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];
|
2019-07-05 11:10:11 +00:00
|
|
|
cabac_ctx_t mvp_idx_model;
|
2015-03-04 11:26:48 +00:00
|
|
|
cabac_ctx_t cu_qt_root_cbf_model;
|
2019-08-19 06:39:59 +00:00
|
|
|
cabac_ctx_t sig_coeff_group_model[4];
|
2019-05-21 09:28:05 +00:00
|
|
|
cabac_ctx_t luma_planar_model[2];
|
2019-08-19 06:39:59 +00:00
|
|
|
cabac_ctx_t multi_ref_line[2];
|
2020-10-13 09:51:20 +00:00
|
|
|
cabac_ctx_t bdpcm_mode[4];
|
2021-08-13 12:37:23 +00:00
|
|
|
cabac_ctx_t joint_cb_cr[3];
|
2021-04-27 11:13:44 +00:00
|
|
|
cabac_ctx_t transform_skip_model_luma;
|
|
|
|
cabac_ctx_t transform_skip_model_chroma;
|
2021-12-03 15:17:06 +00:00
|
|
|
cabac_ctx_t transform_skip_sig_coeff_group[3];
|
2021-04-27 11:13:44 +00:00
|
|
|
cabac_ctx_t transform_skip_sig[3];
|
|
|
|
cabac_ctx_t transform_skip_res_sign[6];
|
|
|
|
cabac_ctx_t transform_skip_gt1[4];
|
|
|
|
cabac_ctx_t transform_skip_par;
|
|
|
|
cabac_ctx_t transform_skip_gt2[5];
|
2021-11-17 10:41:42 +00:00
|
|
|
cabac_ctx_t cclm_flag;
|
|
|
|
cabac_ctx_t cclm_model;
|
2021-04-27 11:13:44 +00:00
|
|
|
|
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_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);
|
2019-03-20 13:17:54 +00:00
|
|
|
void kvz_cabac_encode_trunc_bin(cabac_data_t *data, uint32_t bin_value, uint32_t max_value);
|
2015-08-26 08:50:27 +00:00
|
|
|
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,
|
2019-11-19 12:32:38 +00:00
|
|
|
uint32_t r_param, const unsigned int cutoff);
|
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,
|
2019-11-19 11:27:36 +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
|
|
|
|
2019-03-20 07:06:26 +00:00
|
|
|
#define CTX_PROB_BITS 15
|
|
|
|
#define CTX_PROB_BITS_0 10
|
|
|
|
#define CTX_PROB_BITS_1 14
|
|
|
|
#define CTX_MASK_0 (~(~0u << CTX_PROB_BITS_0) << (CTX_PROB_BITS - CTX_PROB_BITS_0))
|
|
|
|
#define CTX_MASK_1 (~(~0u << CTX_PROB_BITS_1) << (CTX_PROB_BITS - CTX_PROB_BITS_1))
|
2013-09-19 09:37:27 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
// Macros
|
2019-03-20 07:06:26 +00:00
|
|
|
#define CTX_GET_STATE(ctx) ( (ctx)->state[0]+(ctx)->state[1] )
|
|
|
|
#define CTX_STATE(ctx) ( CTX_GET_STATE(ctx)>>8 )
|
|
|
|
#define CTX_SET_STATE(ctx, state) {\
|
2019-03-27 10:38:19 +00:00
|
|
|
(ctx)->state[0]=(state >> 1) & (int)CTX_MASK_0;\
|
|
|
|
(ctx)->state[1]=(state >> 1) & (int)CTX_MASK_1;\
|
2019-03-20 07:06:26 +00:00
|
|
|
}
|
|
|
|
#define CTX_MPS(ctx) (CTX_STATE(ctx)>>7)
|
2019-05-29 13:17:02 +00:00
|
|
|
#define CTX_LPS(ctx,range) ((uint8_t)( ((((CTX_STATE(ctx)&0x80) ? (CTX_STATE(ctx)^0xff) : (CTX_STATE(ctx))) >>2)*(range>>5)>>1)+4 ))
|
2019-03-20 07:06:26 +00:00
|
|
|
#define CTX_UPDATE(ctx,bin) { \
|
|
|
|
int rate0 = (ctx)->rate >> 4;\
|
|
|
|
int rate1 = (ctx)->rate & 15;\
|
|
|
|
\
|
2019-03-27 10:38:19 +00:00
|
|
|
(ctx)->state[0] -= ((ctx)->state[0] >> rate0) & (int)CTX_MASK_0;\
|
|
|
|
(ctx)->state[1] -= ((ctx)->state[1] >> rate1) & (int)CTX_MASK_1;\
|
2019-03-20 07:06:26 +00:00
|
|
|
if (bin) {\
|
2019-03-27 10:38:19 +00:00
|
|
|
(ctx)->state[0] += (0x7fffu >> rate0) & (int)CTX_MASK_0;\
|
|
|
|
(ctx)->state[1] += (0x7fffu >> rate1) & (int)CTX_MASK_1;\
|
2019-03-20 07:06:26 +00:00
|
|
|
}\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CTX_SET_LOG2_WIN(ctx, size) { \
|
|
|
|
int rate0 = 2 + ((size >> 2) & 3); \
|
|
|
|
int rate1 = 3 + rate0 + (size & 3);\
|
|
|
|
(ctx)->rate = 16 * rate0 + rate1;\
|
|
|
|
}
|
2014-01-16 15:13:48 +00:00
|
|
|
|
2018-09-26 09:01:23 +00:00
|
|
|
#ifdef KVZ_DEBUG_PRINT_CABAC
|
2018-09-10 08:00:00 +00:00
|
|
|
extern uint32_t kvz_cabac_bins_count;
|
2018-09-26 09:01:23 +00:00
|
|
|
extern bool kvz_cabac_bins_verbose;
|
2018-07-27 08:03:16 +00:00
|
|
|
#define CABAC_BIN(data, value, name) { \
|
2019-03-20 07:06:26 +00:00
|
|
|
uint32_t prev_state = CTX_STATE(data->cur_ctx); \
|
2020-10-13 09:04:31 +00:00
|
|
|
if(kvz_cabac_bins_verbose && !data->only_count) {printf("%d %d [%d:%d] %s = %u, range = %u LPS = %u state = %u -> ", \
|
|
|
|
kvz_cabac_bins_count++, (data)->range, (data)->range-CTX_LPS(data->cur_ctx,(data)->range), CTX_LPS(data->cur_ctx,(data)->range), (name), (uint32_t)(value), (data)->range, CTX_LPS(data->cur_ctx,(data)->range), prev_state); }\
|
2018-07-27 08:03:16 +00:00
|
|
|
kvz_cabac_encode_bin((data), (value)); \
|
2020-10-09 05:45:43 +00:00
|
|
|
if(kvz_cabac_bins_verbose && !data->only_count) printf("%u\n", CTX_STATE(data->cur_ctx)); }
|
2018-07-27 10:59:54 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
|
|
|
|
#define CABAC_BINS_EP(data, value, bins, name) { \
|
2019-03-20 07:06:26 +00:00
|
|
|
uint32_t prev_state = CTX_STATE(data->cur_ctx); \
|
2016-06-22 02:38:36 +00:00
|
|
|
kvz_cabac_encode_bins_ep((data), (value), (bins)); \
|
2020-10-13 09:04:31 +00:00
|
|
|
if(kvz_cabac_bins_verbose && !data->only_count) { printf("%d %s = %u(%u bins), state = %u -> %u\n", \
|
|
|
|
kvz_cabac_bins_count, (name), (uint32_t)(value), (bins), prev_state, CTX_STATE(data->cur_ctx)); kvz_cabac_bins_count+=bins;}}
|
2013-09-19 09:28:57 +00:00
|
|
|
|
|
|
|
#define CABAC_BIN_EP(data, value, name) { \
|
2019-03-20 07:06:26 +00:00
|
|
|
uint32_t prev_state = CTX_STATE(data->cur_ctx); \
|
2016-06-22 02:38:36 +00:00
|
|
|
kvz_cabac_encode_bin_ep((data), (value)); \
|
2020-10-13 09:04:31 +00:00
|
|
|
if(kvz_cabac_bins_verbose && !data->only_count) {printf("%d %s = %u, state = %u -> %u\n", \
|
|
|
|
kvz_cabac_bins_count++, (name), (uint32_t)(value), prev_state, CTX_STATE(data->cur_ctx)); }}
|
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
|