1 /*****************************************************************************
2  * This file is part of Kvazaar HEVC encoder.
3  *
4  * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  *   list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice, this
14  *   list of conditions and the following disclaimer in the documentation and/or
15  *   other materials provided with the distribution.
16  *
17  * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
28  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
31  ****************************************************************************/
32 
33 #include "context.h"
34 
35 #include "tables.h"
36 
37 
38 static const uint8_t INIT_SAO_MERGE_FLAG[3] = { 153, 153, 153 };
39 static const uint8_t INIT_SAO_TYPE_IDX[3] = { 160, 185, 200 };
40 
41 static const uint8_t INIT_QT_ROOT_CBF[3][1] = {
42   {  79, },
43   {  79, },
44   { CNU, },
45 };
46 
47 static const uint8_t INIT_MVP_IDX[3][2] = {
48   { 168,  CNU, },
49   { 168,  CNU, },
50   { CNU,  CNU, },
51 };
52 
53 static const uint8_t INIT_REF_PIC[3][2] = {
54   { 153,  153 },
55   { 153,  153 },
56   { CNU,  CNU },
57 };
58 
59 static const uint8_t INIT_MVD[3][2] = {
60   { 169,  198, },
61   { 140,  198, },
62   { CNU,  CNU, },
63 };
64 
65 static const uint8_t INIT_MERGE_FLAG_EXT[3][1] = {
66   { 154, },
67   { 110, },
68   { CNU, },
69 };
70 
71 static const uint8_t INIT_MERGE_IDX_EXT[3][1] = {
72   { 137, },
73   { 122, },
74   { CNU, },
75 };
76 
77 static const uint8_t INIT_CU_TRANSQUANT_BYPASS[3][1] = {
78   { 154, },
79   { 154, },
80   { 154, },
81 };
82 
83 static const uint8_t INIT_SKIP_FLAG[3][3] =  {
84   { 197,  185,  201, },
85   { 197,  185,  201, },
86   { CNU,  CNU,  CNU, },
87 };
88 
89 static const uint8_t INIT_PRED_MODE[3][1] = {
90   { 134, },
91   { 149, },
92   { CNU, },
93 };
94 
95 
96 static const uint8_t INIT_PART_SIZE[3][4] = {
97   { 154,  139,  CNU,  CNU, },
98   { 154,  139,  CNU,  CNU, },
99   { 184,  CNU,  CNU,  CNU, },
100 };
101 
102 static const uint8_t  INIT_SPLIT_FLAG[3][3] = {
103   { 107,  139,  126 },
104   { 107,  139,  126 },
105   { 139,  141,  157 },
106 };
107 
108 static const uint8_t INIT_INTRA_PRED_MODE[3] = {
109   183, 154, 184
110 };
111 
112 static const uint8_t INIT_CHROMA_PRED_MODE[3][2] = {
113   { 152,  139 },
114   { 152,  139 },
115   {  63,  139 },
116 };
117 
118 static const uint8_t INIT_INTER_DIR[3][5] = {
119   {  95,  79,  63,  31,  31, },
120   {  95,  79,  63,  31,  31, },
121   { CNU, CNU, CNU, CNU, CNU, },
122 };
123 
124 static const uint8_t INIT_TRANS_SUBDIV_FLAG[3][3] = {
125   { 224,  167,  122 },
126   { 124,  138,   94 },
127   { 153,  138,  138 },
128 };
129 
130 static const uint8_t INIT_QT_CBF[3][8] = {
131   { 153,  111,  CNU,  CNU,   149,   92,  167,  154 },
132   { 153,  111,  CNU,  CNU,   149,  107,  167,  154 },
133   { 111,  141,  CNU,  CNU,    94,  138,  182,  154 },
134 };
135 
136 static const uint8_t INIT_CU_QP_DELTA_ABS[3][2] = {
137   { 154, 154 },
138   { 154, 154 },
139   { 154, 154 },
140 };
141 
142 static const uint8_t INIT_SIG_CG_FLAG[3][4] = {
143   { 121,  140,  61,  154  },
144   { 121,  140,  61,  154 },
145   {  91,  171,  134,  141  },
146 };
147 
148 static const uint8_t INIT_SIG_FLAG[3][42] = {
149    {170,154,139,153,139,123,123, 63,124,166,
150     183,140,136,153,154,166,183,140,136,153,
151     154,166,183,140,136,153,154,170,153,138,
152     138,122,121,122,121,167,151,183,140,151,
153     183,140},
154    {155,154,139,153,139,123,123,63,153,166,
155    183,140,136,153,154,166,183,140,136,153,
156    154,166,183,140,136,153,154,170,153,123,
157    123,107,121,107,121,167,151,183,140,151,
158    183,140},
159    {111,111,125,110,110,94,124,108,124,107,
160    125,141,179,153,125,107,125,141,179,153,
161    125,107,125,141,179,153,125,140,139,182,
162    182,152,136,152,136,153,136,139,111,136,
163    139,111},
164 };
165 
166 static const uint8_t INIT_LAST[3][30] = {
167   { 125,  110,  124,  110,   95,   94,  125,  111,  111,   79,  125,  126,  111,  111,   79,
168     108,  123,   93,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU  },
169   { 125,  110,   94,  110,   95,   79,  125,  111,  110,   78,  110,  111,  111,   95,   94,
170     108,  123,  108,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU  },
171   { 110,  110,  124,  125,  140,  153,  125,  127,  140,  109,  111,  143,  127,  111,   79,
172     108,  123,   63,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU  },
173 };
174 
175 static const uint8_t INIT_ONE_FLAG[3][24] =
176 {
177   {154,196,167,167,154,152,167,182,182,134,149,136,153,121,136,122,169,208,166,167,154,152,167,182},
178   {154,196,196,167,154,152,167,182,182,134,149,136,153,121,136,137,169,194,166,167,154,167,137,182},
179   {140, 92,137,138,140,152,138,139,153, 74,149, 92,139,107,122,152,140,179,166,182,140,227,122,197},
180 };
181 
182 static const uint8_t INIT_ABS_FLAG[3][6] =
183 {
184   { 107,167, 91,107,107,167},
185   { 107,167, 91,122,107,167},
186   { 138,153,136,167,152,152},
187 };
188 
189 static const uint8_t INIT_TRANSFORMSKIP_FLAG[3][2] =
190 {
191   { 139,  139},
192   { 139,  139},
193   { 139,  139},
194 };
195 
196 
197 
198 
199 /**
200  * \brief Initialize struct cabac_ctx.
201  */
kvz_ctx_init(cabac_ctx_t * ctx,uint32_t qp,uint32_t init_value)202 void kvz_ctx_init(cabac_ctx_t *ctx, uint32_t qp, uint32_t init_value)
203 {
204   int slope = (init_value >> 4) * 5 - 45;
205   int offset = ((init_value & 15) << 3) - 16;
206   int init_state = MIN(MAX(1, ((slope * (int)qp) >> 4) + offset), 126);
207 
208   if (init_state >= 64) {
209     ctx->uc_state = ((init_state - 64) << 1) + 1;
210   } else {
211     ctx->uc_state = (63 - init_state) << 1;
212   }
213 }
214 
215 /**
216  * \brief Initialize cabac context to be used for coding
217  * \param encoder encoder control struct
218  * \param slice type of slice we are coding (P/B/I)
219  */
220 
kvz_init_contexts(encoder_state_t * state,int8_t QP,int8_t slice)221 void kvz_init_contexts(encoder_state_t *state, int8_t QP, int8_t slice)
222 {
223   cabac_data_t * const cabac = &state->cabac;
224   uint16_t i;
225 
226   // Initialize contexts
227   kvz_ctx_init(&cabac->ctx.transform_skip_model_luma, QP, INIT_TRANSFORMSKIP_FLAG[slice][0]);
228   kvz_ctx_init(&cabac->ctx.transform_skip_model_chroma, QP, INIT_TRANSFORMSKIP_FLAG[slice][1]);
229 
230   kvz_ctx_init(&cabac->ctx.sao_merge_flag_model, QP, INIT_SAO_MERGE_FLAG[slice]);
231   kvz_ctx_init(&cabac->ctx.sao_type_idx_model, QP, INIT_SAO_TYPE_IDX[slice]);
232 
233   kvz_ctx_init(&cabac->ctx.cu_merge_flag_ext_model, QP, INIT_MERGE_FLAG_EXT[slice][0]);
234   kvz_ctx_init(&cabac->ctx.cu_merge_idx_ext_model, QP, INIT_MERGE_IDX_EXT[slice][0]);
235   kvz_ctx_init(&cabac->ctx.cu_pred_mode_model, QP, INIT_PRED_MODE[slice][0]);
236   kvz_ctx_init(&cabac->ctx.cu_transquant_bypass, QP, INIT_CU_TRANSQUANT_BYPASS[slice][0]);
237 
238   kvz_ctx_init(&cabac->ctx.cu_skip_flag_model[0], QP, INIT_SKIP_FLAG[slice][0]);
239   kvz_ctx_init(&cabac->ctx.cu_skip_flag_model[1], QP, INIT_SKIP_FLAG[slice][1]);
240   kvz_ctx_init(&cabac->ctx.cu_skip_flag_model[2], QP, INIT_SKIP_FLAG[slice][2]);
241 
242   kvz_ctx_init(&cabac->ctx.split_flag_model[0], QP, INIT_SPLIT_FLAG[slice][0]);
243   kvz_ctx_init(&cabac->ctx.split_flag_model[1], QP, INIT_SPLIT_FLAG[slice][1]);
244   kvz_ctx_init(&cabac->ctx.split_flag_model[2], QP, INIT_SPLIT_FLAG[slice][2]);
245 
246   kvz_ctx_init(&cabac->ctx.intra_mode_model, QP, INIT_INTRA_PRED_MODE[slice]);
247 
248   kvz_ctx_init(&cabac->ctx.chroma_pred_model[0], QP, INIT_CHROMA_PRED_MODE[slice][0]);
249   kvz_ctx_init(&cabac->ctx.chroma_pred_model[1], QP, INIT_CHROMA_PRED_MODE[slice][1]);
250 
251   kvz_ctx_init(&cabac->ctx.cu_abs_model_chroma[0], QP, INIT_ABS_FLAG[slice][4]);
252   kvz_ctx_init(&cabac->ctx.cu_abs_model_chroma[1], QP, INIT_ABS_FLAG[slice][5]);
253 
254   //TODO: ignore P/B contexts on intra frame
255   kvz_ctx_init(&cabac->ctx.cu_qt_root_cbf_model, QP, INIT_QT_ROOT_CBF[slice][0]);
256 
257   kvz_ctx_init(&cabac->ctx.cu_mvd_model[0], QP, INIT_MVD[slice][0]);
258   kvz_ctx_init(&cabac->ctx.cu_mvd_model[1], QP, INIT_MVD[slice][1]);
259   kvz_ctx_init(&cabac->ctx.cu_ref_pic_model[0], QP, INIT_REF_PIC[slice][0]);
260   kvz_ctx_init(&cabac->ctx.cu_ref_pic_model[1], QP, INIT_REF_PIC[slice][1]);
261   kvz_ctx_init(&cabac->ctx.mvp_idx_model[0], QP, INIT_MVP_IDX[slice][0]);
262   kvz_ctx_init(&cabac->ctx.mvp_idx_model[1], QP, INIT_MVP_IDX[slice][1]);
263 
264   kvz_ctx_init(&cabac->ctx.cu_qp_delta_abs[0], QP, INIT_CU_QP_DELTA_ABS[slice][0]);
265   kvz_ctx_init(&cabac->ctx.cu_qp_delta_abs[1], QP, INIT_CU_QP_DELTA_ABS[slice][1]);
266 
267   for (i = 0; i < 4; i++) {
268     kvz_ctx_init(&cabac->ctx.cu_sig_coeff_group_model[i], QP, INIT_SIG_CG_FLAG[slice][i]);
269     kvz_ctx_init(&cabac->ctx.cu_abs_model_luma[i], QP, INIT_ABS_FLAG[slice][i]);
270     kvz_ctx_init(&cabac->ctx.part_size_model[i], QP, INIT_PART_SIZE[slice][i]);
271   }
272   for (i = 0; i < 3; i++) {
273     kvz_ctx_init(&cabac->ctx.trans_subdiv_model[i], QP, INIT_TRANS_SUBDIV_FLAG[slice][i]);
274   }
275   for (i = 0; i < 4; i++) {
276     kvz_ctx_init(&cabac->ctx.qt_cbf_model_luma[i], QP, INIT_QT_CBF[slice][i]);
277     kvz_ctx_init(&cabac->ctx.qt_cbf_model_chroma[i], QP, INIT_QT_CBF[slice][i + 4]);
278   }
279 
280   for (i = 0; i < 5; i++) {
281     kvz_ctx_init(&cabac->ctx.inter_dir[i], QP, INIT_INTER_DIR[slice][i]);
282   }
283 
284   for (i = 0; i < 8; i++) {
285     kvz_ctx_init(&cabac->ctx.cu_one_model_chroma[i], QP, INIT_ONE_FLAG[slice][i+16]);
286   }
287 
288   for (i = 0; i < 15; i++) {
289     kvz_ctx_init(&cabac->ctx.cu_ctx_last_y_luma[i], QP, INIT_LAST[slice][i] );
290     kvz_ctx_init(&cabac->ctx.cu_ctx_last_x_luma[i], QP, INIT_LAST[slice][i] );
291 
292     kvz_ctx_init(&cabac->ctx.cu_ctx_last_y_chroma[i], QP, INIT_LAST[slice][i+15] );
293     kvz_ctx_init(&cabac->ctx.cu_ctx_last_x_chroma[i], QP, INIT_LAST[slice][i+15] );
294 
295     kvz_ctx_init(&cabac->ctx.cu_one_model_luma[i], QP, INIT_ONE_FLAG[slice][i]);
296   }
297   kvz_ctx_init(&cabac->ctx.cu_one_model_luma[15], QP, INIT_ONE_FLAG[slice][15]);
298 
299   for (i = 0; i < 27; i++) {
300     kvz_ctx_init(&cabac->ctx.cu_sig_model_luma[i], QP, INIT_SIG_FLAG[slice][i]);
301     if(i < 15) kvz_ctx_init(&cabac->ctx.cu_sig_model_chroma[i], QP, INIT_SIG_FLAG[slice][i+27]);
302   }
303 }
304 
kvz_context_copy(encoder_state_t * const target_state,const encoder_state_t * const source_state)305 void kvz_context_copy(encoder_state_t * const target_state, const encoder_state_t * const source_state) {
306   cabac_data_t * const target_cabac = &target_state->cabac;
307   const cabac_data_t * const source_cabac = &source_state->cabac;
308 
309   if (target_cabac == source_cabac) return;
310 
311   target_cabac->ctx = source_cabac->ctx;
312 }
313 
314 
kvz_context_get_sig_coeff_group(uint32_t * sig_coeff_group_flag,uint32_t pos_x,uint32_t pos_y,int32_t width)315 uint32_t kvz_context_get_sig_coeff_group( uint32_t *sig_coeff_group_flag,
316                                       uint32_t pos_x,
317                                       uint32_t pos_y,
318                                       int32_t width)
319 {
320   uint32_t uiRight = 0;
321   uint32_t uiLower = 0;
322   width >>= 2;
323   if (pos_x < (uint32_t)width - 1) uiRight = (sig_coeff_group_flag[pos_y * width + pos_x + 1] != 0);
324   if (pos_y < (uint32_t)width - 1) uiLower = (sig_coeff_group_flag[(pos_y  + 1 ) * width + pos_x] != 0);
325 
326   return uiRight || uiLower;
327 }
328 
329 
330 /**
331  * \brief Pattern decision for context derivation process of significant_coeff_flag
332  * \param sig_coeff_group_flag pointer to prior coded significant coeff group
333  * \param pos_x column of current coefficient group
334  * \param pos_y row of current coefficient group
335  * \param width width of the block
336  * \returns pattern for current coefficient group
337  */
338 
kvz_context_calc_pattern_sig_ctx(const uint32_t * sig_coeff_group_flag,uint32_t pos_x,uint32_t pos_y,int32_t width)339 int32_t kvz_context_calc_pattern_sig_ctx(const uint32_t *sig_coeff_group_flag, uint32_t pos_x, uint32_t pos_y, int32_t width)
340 {
341   uint32_t sigRight = 0;
342   uint32_t sigLower = 0;
343 
344   if (width == 4) return -1;
345 
346   width >>= 2;
347   if (pos_x < (uint32_t)width - 1) sigRight = (sig_coeff_group_flag[pos_y * width + pos_x + 1] != 0);
348   if (pos_y < (uint32_t)width - 1) sigLower = (sig_coeff_group_flag[(pos_y  + 1 ) * width + pos_x] != 0);
349 
350   return sigRight + (sigLower<<1);
351 }
352 
353 
354 /**
355  * \brief Context derivation process of coeff_abs_significant_flag
356  * \param pattern_sig_ctx pattern for current coefficient group
357  * \param scan_idx pixel scan type in use
358  * \param pos_x column of current scan position
359  * \param pos_y row of current scan position
360  * \param block_type log2 value of block size if square block, or 4 otherwise
361  * \param width width of the block
362  * \param texture_type texture type (TEXT_LUMA...)
363  * \returns ctx_inc for current scan position
364  */
365 
kvz_context_get_sig_ctx_inc(int32_t pattern_sig_ctx,uint32_t scan_idx,int32_t pos_x,int32_t pos_y,int32_t block_type,int8_t texture_type)366 int32_t kvz_context_get_sig_ctx_inc(int32_t pattern_sig_ctx, uint32_t scan_idx, int32_t pos_x,
367                                 int32_t pos_y, int32_t block_type, int8_t texture_type)
368 {
369   const int32_t ctx_ind_map[16] =
370   {
371     0, 1, 4, 5,
372     2, 3, 4, 5,
373     6, 6, 8, 8,
374     7, 7, 8, 8
375   };
376 
377   int32_t cnt,offset,pos_x_in_subset,pos_y_in_subset;
378 
379   if (pos_x + pos_y == 0) return 0;
380 
381   if (block_type == 2) return ctx_ind_map[4 * pos_y + pos_x];
382 
383   offset = (block_type == 3) ? ((scan_idx == SCAN_DIAG) ? 9 : 15) : ((texture_type == 0) ? 21 : 12);
384   pos_x_in_subset = pos_x - ((pos_x>>2)<<2);
385   pos_y_in_subset = pos_y - ((pos_y>>2)<<2);
386 
387   if (pattern_sig_ctx == 0) {
388     cnt = (pos_x_in_subset + pos_y_in_subset <= 2) ? ((pos_x_in_subset + pos_y_in_subset==0) ? 2 : 1) : 0;
389   } else if (pattern_sig_ctx==1) {
390     cnt = (pos_y_in_subset <= 1) ? ((pos_y_in_subset == 0) ? 2 : 1) : 0;
391   } else if (pattern_sig_ctx==2) {
392     cnt = (pos_x_in_subset <= 1) ? ((pos_x_in_subset == 0) ? 2 : 1) : 0;
393   } else {
394     cnt = 2;
395   }
396   return (( texture_type == 0 && ((pos_x>>2) + (pos_y>>2)) > 0 ) ? 3 : 0) + offset + cnt;
397 }
398