xref: /aosp_15_r20/external/libavc/encoder/ih264e_cabac_init.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2015 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 *  ih264e_cabac_init.c
25 *
26 * @brief
27 *  Contains all initialization functions for cabac contexts
28 *
29 * @author
30 *  ittiam
31 *
32 * @par List of Functions
33 *  - ih264e_init_cabac_enc_envirnoment
34 *  - ih264e_init_cabac_table
35 *  - ih264e_init_cabac_ctxt
36 *
37 * @remarks
38 *  none
39 *
40 *******************************************************************************
41 */
42 
43 /*****************************************************************************/
44 /* File Includes                                                             */
45 /*****************************************************************************/
46 
47 /* System Include Files */
48 #include <stdio.h>
49 #include <stddef.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <limits.h>
53 #include <assert.h>
54 
55 /* User Include Files */
56 #include "ih264e_config.h"
57 #include "ih264_typedefs.h"
58 #include "iv2.h"
59 #include "ive2.h"
60 
61 #include "ih264_debug.h"
62 #include "ih264_macros.h"
63 #include "ih264_error.h"
64 #include "ih264_defs.h"
65 #include "ih264_mem_fns.h"
66 #include "ih264_padding.h"
67 #include "ih264_structs.h"
68 #include "ih264_trans_quant_itrans_iquant.h"
69 #include "ih264_inter_pred_filters.h"
70 #include "ih264_intra_pred_filters.h"
71 #include "ih264_deblk_edge_filters.h"
72 #include "ih264_cavlc_tables.h"
73 #include "ih264_cabac_tables.h"
74 #include "ih264_platform_macros.h"
75 
76 #include "ime_defs.h"
77 #include "ime_distortion_metrics.h"
78 #include "ime_structs.h"
79 
80 #include "irc_cntrl_param.h"
81 #include "irc_frame_info_collector.h"
82 
83 #include "ih264e_error.h"
84 #include "ih264e_defs.h"
85 #include "ih264e_bitstream.h"
86 #include "ih264e_cabac_structs.h"
87 #include "ih264e_structs.h"
88 #include "ih264e_encode_header.h"
89 #include "ih264e_cabac.h"
90 #include "ih264e_statistics.h"
91 #include "ih264e_trace.h"
92 
93 
94 /*****************************************************************************/
95 /*  Function definitions .                                                   */
96 /*****************************************************************************/
97 
98 /**
99 *******************************************************************************
100 *
101 * @brief Initialize cabac encoding environment
102 *
103 * @param[in] ps_cab_enc_env
104 *  Pointer to encoding_envirnoment_t structure
105 *
106 * @returns none
107 *
108 * @remarks none
109 *
110 *******************************************************************************
111 */
ih264e_init_cabac_enc_envirnoment(encoding_envirnoment_t * ps_cab_enc_env)112 static void ih264e_init_cabac_enc_envirnoment(encoding_envirnoment_t *ps_cab_enc_env)
113 {
114     ps_cab_enc_env->u4_code_int_low = 0;
115     ps_cab_enc_env->u4_code_int_range = 0x1fe;
116     ps_cab_enc_env->u4_out_standing_bytes = 0;
117     ps_cab_enc_env->u4_bits_gen = 0;
118 }
119 
120 /**
121 *******************************************************************************
122 *
123 * @brief Initialize default context values and pointers. Called once at the
124 *  beginning of encoding.
125 *
126 * @param[in] ps_ent_ctxt
127 *  Pointer to entropy context structure
128 *
129 * @returns none
130 *
131 * @remarks none
132 *
133 *******************************************************************************
134 */
ih264e_init_cabac_table(entropy_ctxt_t * ps_ent_ctxt)135 void ih264e_init_cabac_table(entropy_ctxt_t *ps_ent_ctxt)
136 {
137     cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
138     ps_cabac_ctxt->ps_mb_map_ctxt_inc = ps_cabac_ctxt->ps_mb_map_ctxt_inc_base + 1;
139     ps_cabac_ctxt->ps_lft_csbp = &ps_cabac_ctxt->s_lft_csbp;
140     ps_cabac_ctxt->ps_bitstrm = ps_ent_ctxt->ps_bitstrm;
141 
142     {
143         /* 0th entry of mb_map_ctxt_inc will be always be containing default values */
144         /* for CABAC context representing MB not available */
145         mb_info_ctxt_t *ps_def_ctxt = ps_cabac_ctxt->ps_mb_map_ctxt_inc - 1;
146 
147         ps_def_ctxt->u1_mb_type = CAB_SKIP;
148         ps_def_ctxt->u1_cbp = 0x0f;
149         ps_def_ctxt->u1_intrapred_chroma_mode = 0;
150 
151         memset(ps_def_ctxt->i1_ref_idx, 0, sizeof(ps_def_ctxt->i1_ref_idx));
152         memset(ps_def_ctxt->u1_mv, 0, sizeof(ps_def_ctxt->u1_mv));
153         ps_cabac_ctxt->ps_def_ctxt_mb_info = ps_def_ctxt;
154     }
155 }
156 
157 /**
158 *******************************************************************************
159 *
160 * @brief Initialize cabac context: Initialize all contest with init values given
161 *  in the spec. Called at the beginning of entropy coding of each slice for
162 *  CABAC encoding.
163 *
164 * @param[in] ps_ent_ctxt
165 *  Pointer to entropy context structure
166 *
167 * @returns none
168 *
169 * @remarks none
170 *
171 *******************************************************************************
172 */
ih264e_init_cabac_ctxt(entropy_ctxt_t * ps_ent_ctxt)173 void ih264e_init_cabac_ctxt(entropy_ctxt_t *ps_ent_ctxt)
174 {
175     cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
176     slice_header_t *ps_slice_hdr = ps_ent_ctxt->ps_slice_hdr_base;
177     const UWORD8 u1_slice_type = ps_slice_hdr->u1_slice_type;
178     WORD8 i1_cabac_init_idc = 0;
179     bin_ctxt_model *au1_cabac_ctxt_table = ps_cabac_ctxt->au1_cabac_ctxt_table;
180     UWORD8 u1_qp_y = ps_slice_hdr->i1_slice_qp;
181 
182     ih264e_init_cabac_enc_envirnoment(&ps_cabac_ctxt->s_cab_enc_env);
183 
184     ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt = 0;
185 
186     if (ISLICE != u1_slice_type)
187     {
188         i1_cabac_init_idc = ps_slice_hdr->i1_cabac_init_idc;
189     }
190     else
191     {
192         i1_cabac_init_idc = 3;
193     }
194 
195     memcpy(au1_cabac_ctxt_table,
196            gau1_ih264_cabac_ctxt_init_table[i1_cabac_init_idc][u1_qp_y],
197            NUM_CABAC_CTXTS * sizeof(bin_ctxt_model));
198 }
199