1 #ifndef CABAC_H_
2 #define CABAC_H_
3 /*****************************************************************************
4  * This file is part of Kvazaar HEVC encoder.
5  *
6  * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * * Redistributions of source code must retain the above copyright notice, this
13  *   list of conditions and the following disclaimer.
14  *
15  * * Redistributions in binary form must reproduce the above copyright notice, this
16  *   list of conditions and the following disclaimer in the documentation and/or
17  *   other materials provided with the distribution.
18  *
19  * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
20  *   contributors may be used to endorse or promote products derived from
21  *   this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
33  ****************************************************************************/
34 
35 /**
36  * \ingroup CABAC
37  * \file
38  * Coding bins using CABAC.
39  */
40 
41 #include "global.h" // IWYU pragma: keep
42 
43 #include "bitstream.h"
44 
45 struct encoder_state_t;
46 
47 // Types
48 typedef struct
49 {
50   uint8_t  uc_state;
51 } cabac_ctx_t;
52 
53 typedef struct
54 {
55   cabac_ctx_t *cur_ctx;
56   uint32_t   low;
57   uint32_t   range;
58   uint32_t   buffered_byte;
59   int32_t    num_buffered_bytes;
60   int32_t    bits_left;
61   int8_t     only_count;
62   bitstream_t *stream;
63 
64   // CONTEXTS
65   struct {
66     cabac_ctx_t sao_merge_flag_model;
67     cabac_ctx_t sao_type_idx_model;
68     cabac_ctx_t split_flag_model[3]; //!< \brief split flag context models
69     cabac_ctx_t intra_mode_model;    //!< \brief intra mode context models
70     cabac_ctx_t chroma_pred_model[2];
71     cabac_ctx_t inter_dir[5];
72     cabac_ctx_t trans_subdiv_model[3]; //!< \brief intra mode context models
73     cabac_ctx_t qt_cbf_model_luma[4];
74     cabac_ctx_t qt_cbf_model_chroma[4];
75     cabac_ctx_t cu_qp_delta_abs[4];
76     cabac_ctx_t part_size_model[4];
77     cabac_ctx_t cu_sig_coeff_group_model[4];
78     cabac_ctx_t cu_sig_model_luma[27];
79     cabac_ctx_t cu_sig_model_chroma[15];
80     cabac_ctx_t cu_ctx_last_y_luma[15];
81     cabac_ctx_t cu_ctx_last_y_chroma[15];
82     cabac_ctx_t cu_ctx_last_x_luma[15];
83     cabac_ctx_t cu_ctx_last_x_chroma[15];
84     cabac_ctx_t cu_one_model_luma[16];
85     cabac_ctx_t cu_one_model_chroma[8];
86     cabac_ctx_t cu_abs_model_luma[4];
87     cabac_ctx_t cu_abs_model_chroma[2];
88     cabac_ctx_t cu_pred_mode_model;
89     cabac_ctx_t cu_skip_flag_model[3];
90     cabac_ctx_t cu_merge_idx_ext_model;
91     cabac_ctx_t cu_merge_flag_ext_model;
92     cabac_ctx_t cu_transquant_bypass;
93     cabac_ctx_t cu_mvd_model[2];
94     cabac_ctx_t cu_ref_pic_model[2];
95     cabac_ctx_t mvp_idx_model[2];
96     cabac_ctx_t cu_qt_root_cbf_model;
97     cabac_ctx_t transform_skip_model_luma;
98     cabac_ctx_t transform_skip_model_chroma;
99   } ctx;
100 } cabac_data_t;
101 
102 
103 // Globals
104 extern const uint8_t kvz_g_auc_next_state_mps[128];
105 extern const uint8_t kvz_g_auc_next_state_lps[128];
106 extern const uint8_t kvz_g_auc_lpst_table[64][4];
107 extern const uint8_t kvz_g_auc_renorm_table[32];
108 
109 
110 // Functions
111 void kvz_cabac_start(cabac_data_t *data);
112 void kvz_cabac_encode_bin(cabac_data_t *data, uint32_t bin_value);
113 void kvz_cabac_encode_bin_ep(cabac_data_t *data, uint32_t bin_value);
114 void kvz_cabac_encode_bins_ep(cabac_data_t *data, uint32_t bin_values, int num_bins);
115 void kvz_cabac_encode_bin_trm(cabac_data_t *data, uint8_t bin_value);
116 void kvz_cabac_write(cabac_data_t *data);
117 void kvz_cabac_finish(cabac_data_t *data);
118 void kvz_cabac_write_coeff_remain(cabac_data_t *cabac, uint32_t symbol,
119                               uint32_t r_param);
120 void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, cabac_data_t * const cabac, const uint32_t symbol,
121                                         const uint32_t r_param, int32_t base_level);
122 void kvz_cabac_write_ep_ex_golomb(struct encoder_state_t * const state, cabac_data_t *data,
123                                   uint32_t symbol, uint32_t count);
124 void kvz_cabac_write_unary_max_symbol(cabac_data_t *data, cabac_ctx_t *ctx,
125                                   uint32_t symbol, int32_t offset,
126                                   uint32_t max_symbol);
127 void kvz_cabac_write_unary_max_symbol_ep(cabac_data_t *data, unsigned int symbol, unsigned int max_symbol);
128 
129 
130 // Macros
131 #define CTX_STATE(ctx) ((ctx)->uc_state >> 1)
132 #define CTX_MPS(ctx) ((ctx)->uc_state & 1)
133 #define CTX_UPDATE_LPS(ctx) { (ctx)->uc_state = kvz_g_auc_next_state_lps[ (ctx)->uc_state ]; }
134 #define CTX_UPDATE_MPS(ctx) { (ctx)->uc_state = kvz_g_auc_next_state_mps[ (ctx)->uc_state ]; }
135 
136 #ifdef VERBOSE
137   #define CABAC_BIN(data, value, name) { \
138     uint32_t prev_state = (data)->ctx->uc_state; \
139     kvz_cabac_encode_bin((data), (value)) \
140     printf("%s = %u, state = %u -> %u\n", \
141            (name), (uint32_t)(value), prev_state, (data)->ctx->uc_state); }
142 
143   #define CABAC_BINS_EP(data, value, bins, name) { \
144     uint32_t prev_state = (data)->ctx->uc_state; \
145     kvz_cabac_encode_bins_ep((data), (value), (bins)); \
146     printf("%s = %u(%u bins), state = %u -> %u\n", \
147            (name), (uint32_t)(value), (bins), prev_state, (data)->ctx->uc_state); }
148 
149   #define CABAC_BIN_EP(data, value, name) { \
150     uint32_t prev_state = (data)->ctx->uc_state; \
151     kvz_cabac_encode_bin_ep((data), (value)); \
152     printf("%s = %u, state = %u -> %u\n", \
153            (name), (uint32_t)(value), prev_state, (data)->ctx->uc_state); }
154 #else
155   #define CABAC_BIN(data, value, name) \
156     kvz_cabac_encode_bin((data), (value));
157   #define CABAC_BINS_EP(data, value, bins, name) \
158     kvz_cabac_encode_bins_ep((data), (value), (bins));
159   #define CABAC_BIN_EP(data, value, name) \
160     kvz_cabac_encode_bin_ep((data), (value));
161 #endif
162 
163 #endif
164