xref: /aosp_15_r20/external/libavc/encoder/svc/isvce_cabac_init.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2022 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19  */
20 
21 /**
22 *******************************************************************************
23 * @file
24 * isvce_cabac_init.c
25 *
26 * @brief
27 *  Contains all initialization functions for cabac contexts
28 *
29 * @author
30 *  Doney Alex
31 *
32 * @par List of Functions:
33 *
34 *
35 * @remarks
36 *  None
37 *
38 *******************************************************************************
39 */
40 
41 /*****************************************************************************/
42 /* File Includes                                                             */
43 /*****************************************************************************/
44 
45 /* System include files */
46 #include <stdio.h>
47 #include <stddef.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <limits.h>
51 #include <assert.h>
52 
53 /* User include files */
54 #include "ih264_typedefs.h"
55 #include "iv2.h"
56 #include "ive2.h"
57 #include "isvc_defs.h"
58 #include "ih264_debug.h"
59 #include "ime_distortion_metrics.h"
60 #include "ime_defs.h"
61 #include "ime_structs.h"
62 #include "ih264_error.h"
63 #include "isvc_structs.h"
64 #include "isvc_trans_quant_itrans_iquant.h"
65 #include "isvc_inter_pred_filters.h"
66 #include "isvc_mem_fns.h"
67 #include "ih264_padding.h"
68 #include "ih264_intra_pred_filters.h"
69 #include "ih264_deblk_edge_filters.h"
70 #include "ih264_platform_macros.h"
71 #include "isvc_macros.h"
72 #include "ih264_buf_mgr.h"
73 #include "ih264e_error.h"
74 #include "ih264e_bitstream.h"
75 #include "isvc_common_tables.h"
76 #include "isvc_cabac_tables.h"
77 #include "ih264_list.h"
78 #include "isvce_defs.h"
79 #include "irc_cntrl_param.h"
80 #include "irc_frame_info_collector.h"
81 #include "isvce_rate_control.h"
82 #include "isvce_cabac_structs.h"
83 #include "isvce_structs.h"
84 #include "isvce_cabac.h"
85 #include "isvce_process.h"
86 #include "ithread.h"
87 #include "isvce_encode_header.h"
88 #include "isvce_globals.h"
89 #include "ih264e_config.h"
90 #include "ih264e_trace.h"
91 #include "ih264e_statistics.h"
92 #include "ih264_cavlc_tables.h"
93 #include "isvce_deblk.h"
94 #include "isvce_me.h"
95 #include "ih264e_debug.h"
96 #include "ih264e_master.h"
97 #include "isvce_utils.h"
98 #include "irc_mem_req_and_acq.h"
99 #include "irc_rate_control_api.h"
100 #include "ih264e_platform_macros.h"
101 #include "ime_statistics.h"
102 
103 /*****************************************************************************/
104 /*  Function definitions .                                                   */
105 /*****************************************************************************/
106 
107 /**
108  *******************************************************************************
109  *
110  * @brief
111  * Initialize cabac encoding environment
112  *
113  * @param[in] ps_cab_enc_env
114  *  Pointer to encoding_envirnoment_t structure
115  *
116  * @returns
117  *
118  * @remarks
119  *  None
120  *
121  *******************************************************************************
122  */
isvce_init_cabac_enc_envirnoment(encoding_envirnoment_t * ps_cab_enc_env)123 static void isvce_init_cabac_enc_envirnoment(encoding_envirnoment_t *ps_cab_enc_env)
124 {
125     ps_cab_enc_env->u4_code_int_low = 0;
126     ps_cab_enc_env->u4_code_int_range = 0x1fe;
127     ps_cab_enc_env->u4_out_standing_bytes = 0;
128     ps_cab_enc_env->u4_bits_gen = 0;
129 }
130 
131 /**
132  *******************************************************************************
133  *
134  * @brief
135  * Initialize default context values and pointers (Called once at the beginning
136  *of encoding).
137  *
138  * @param[in] ps_ent_ctxt
139  *  Pointer to entropy context structure
140  *
141  * @returns
142  *
143  * @remarks
144  *  None
145  *
146  *******************************************************************************
147  */
isvce_init_cabac_table(isvce_entropy_ctxt_t * ps_ent_ctxt)148 void isvce_init_cabac_table(isvce_entropy_ctxt_t *ps_ent_ctxt)
149 {
150     /* CABAC context */
151     isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
152     ps_cabac_ctxt->ps_mb_map_ctxt_inc = ps_cabac_ctxt->ps_mb_map_ctxt_inc_base + 1;
153     ps_cabac_ctxt->ps_lft_csbp = &ps_cabac_ctxt->s_lft_csbp;
154     ps_cabac_ctxt->ps_bitstrm = ps_ent_ctxt->ps_bitstrm;
155 
156     {
157         /* 0th entry of mb_map_ctxt_inc will be always be containing default values
158          */
159         /* for CABAC context representing MB not available                       */
160         isvce_mb_info_ctxt_t *ps_def_ctxt = ps_cabac_ctxt->ps_mb_map_ctxt_inc - 1;
161 
162         ps_def_ctxt->u1_mb_type = CAB_SKIP;
163         ps_def_ctxt->u1_cbp = 0x0f;
164         ps_def_ctxt->u1_intrapred_chroma_mode = 0;
165         ps_def_ctxt->u1_base_mode_flag = 0;
166 
167         memset(ps_def_ctxt->i1_ref_idx, 0, sizeof(ps_def_ctxt->i1_ref_idx));
168         memset(ps_def_ctxt->u1_mv, 0, sizeof(ps_def_ctxt->u1_mv));
169         ps_cabac_ctxt->ps_def_ctxt_mb_info = ps_def_ctxt;
170     }
171 }
172 
173 /**
174  *******************************************************************************
175  *
176  * @brief
177  * Initialize cabac context: Initialize all contest with init values given in
178  *the spec. Called at the beginning of entropy coding of each slice for CABAC
179  *encoding.
180  *
181  * @param[in] ps_ent_ctxt
182  *  Pointer to entropy context structure
183  *
184  * @returns
185  *
186  * @remarks
187  *  None
188  *
189  *******************************************************************************
190  */
isvce_init_cabac_ctxt(isvce_entropy_ctxt_t * ps_ent_ctxt,slice_header_t * ps_slice_hdr)191 void isvce_init_cabac_ctxt(isvce_entropy_ctxt_t *ps_ent_ctxt, slice_header_t *ps_slice_hdr)
192 {
193     isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
194 
195     const UWORD8 u1_slice_type = ps_slice_hdr->u1_slice_type;
196     WORD8 i1_cabac_init_idc = 0;
197     bin_ctxt_model *au1_cabac_ctxt_table = ps_cabac_ctxt->au1_cabac_ctxt_table;
198     UWORD8 u1_qp_y = ps_slice_hdr->i1_slice_qp;
199 
200     isvce_init_cabac_enc_envirnoment(&ps_cabac_ctxt->s_cab_enc_env);
201 
202     ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 0;
203 
204     if(ISLICE != u1_slice_type)
205     {
206         i1_cabac_init_idc = ps_slice_hdr->i1_cabac_init_idc;
207     }
208     else
209     {
210         i1_cabac_init_idc = 3;
211     }
212 
213     memcpy(au1_cabac_ctxt_table, gau1_isvc_cabac_ctxt_init_table[i1_cabac_init_idc][u1_qp_y],
214            NUM_SVC_CABAC_CTXTS * sizeof(bin_ctxt_model));
215 }
216