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 *  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 "ih264_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 "ih264_structs.h"
64 #include "ih264_trans_quant_itrans_iquant.h"
65 #include "ih264_inter_pred_filters.h"
66 #include "ih264_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 "ih264_macros.h"
72 #include "ih264_buf_mgr.h"
73 #include "ih264e_error.h"
74 #include "ih264e_bitstream.h"
75 #include "ih264_common_tables.h"
76 #include "ih264_cabac_tables.h"
77 #include "ih264_list.h"
78 #include "ih264e_defs.h"
79 #include "irc_cntrl_param.h"
80 #include "irc_frame_info_collector.h"
81 #include "ih264e_rate_control.h"
82 #include "ih264e_cabac_structs.h"
83 #include "ih264e_structs.h"
84 #include "ih264e_cabac.h"
85 #include "ih264e_process.h"
86 #include "ithread.h"
87 #include "ih264e_intra_modes_eval.h"
88 #include "ih264e_encode_header.h"
89 #include "ih264e_globals.h"
90 #include "ih264e_config.h"
91 #include "ih264e_trace.h"
92 #include "ih264e_statistics.h"
93 #include "ih264_cavlc_tables.h"
94 #include "ih264e_deblk.h"
95 #include "ih264e_me.h"
96 #include "ih264e_debug.h"
97 #include "ih264e_master.h"
98 #include "ih264e_utils.h"
99 #include "irc_mem_req_and_acq.h"
100 #include "irc_rate_control_api.h"
101 #include "ih264e_platform_macros.h"
102 #include "ime_statistics.h"
103 
104 
105 
106 /*****************************************************************************/
107 /*  Function definitions .                                                   */
108 /*****************************************************************************/
109 
110 /**
111  *******************************************************************************
112  *
113  * @brief
114  * Initialize cabac encoding environment
115  *
116  * @param[in] ps_cab_enc_env
117  *  Pointer to encoding_envirnoment_t structure
118  *
119  * @returns
120  *
121  * @remarks
122  *  None
123  *
124  *******************************************************************************
125 */
ih264e_init_cabac_enc_envirnoment(encoding_envirnoment_t * ps_cab_enc_env)126 static void ih264e_init_cabac_enc_envirnoment(encoding_envirnoment_t *ps_cab_enc_env)
127 {
128     ps_cab_enc_env->u4_code_int_low = 0;
129     ps_cab_enc_env->u4_code_int_range = 0x1fe;
130     ps_cab_enc_env->u4_out_standing_bytes = 0;
131     ps_cab_enc_env->u4_bits_gen = 0;
132 }
133 
134 
135 /**
136  *******************************************************************************
137  *
138  * @brief
139  * Initialize default context values and pointers (Called once at the beginning of encoding).
140  *
141  * @param[in] ps_ent_ctxt
142  *  Pointer to entropy context structure
143  *
144  * @returns
145  *
146  * @remarks
147  *  None
148  *
149  *******************************************************************************
150 */
ih264e_init_cabac_table(entropy_ctxt_t * ps_ent_ctxt)151 void ih264e_init_cabac_table(entropy_ctxt_t *ps_ent_ctxt)
152 {
153     /* CABAC context */
154     cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
155     ps_cabac_ctxt->ps_mb_map_ctxt_inc = ps_cabac_ctxt->ps_mb_map_ctxt_inc_base + 1;
156     ps_cabac_ctxt->ps_lft_csbp = &ps_cabac_ctxt->s_lft_csbp;
157     ps_cabac_ctxt->ps_bitstrm = ps_ent_ctxt->ps_bitstrm;
158 
159     {
160         /* 0th entry of mb_map_ctxt_inc will be always be containing default values */
161         /* for CABAC context representing MB not available                       */
162         mb_info_ctxt_t *ps_def_ctxt = ps_cabac_ctxt->ps_mb_map_ctxt_inc - 1;
163         UWORD32 *pu4_temp;
164         WORD8 i;
165 
166         ps_def_ctxt->u1_mb_type = CAB_SKIP;
167         ps_def_ctxt->u1_cbp = 0x0f;
168         ps_def_ctxt->u1_intrapred_chroma_mode = 0;
169         pu4_temp = (UWORD32 *)ps_def_ctxt->i1_ref_idx;
170         pu4_temp[0] = 0;
171         pu4_temp = (UWORD32 *)ps_def_ctxt->u1_mv;
172         for (i = 0; i < 4; i++, pu4_temp++)
173             (*pu4_temp) = 0;
174         ps_cabac_ctxt->ps_def_ctxt_mb_info = ps_def_ctxt;
175     }
176 }
177 
178 
179 /**
180  *******************************************************************************
181  *
182  * @brief
183  * Initialize cabac context: Initialize all contest with init values given in the spec.
184  * Called at the beginning of entropy coding of each slice for CABAC encoding.
185  *
186  * @param[in] ps_ent_ctxt
187  *  Pointer to entropy context structure
188  *
189  * @returns
190  *
191  * @remarks
192  *  None
193  *
194  *******************************************************************************
195  */
ih264e_init_cabac_ctxt(entropy_ctxt_t * ps_ent_ctxt)196 void ih264e_init_cabac_ctxt(entropy_ctxt_t *ps_ent_ctxt)
197 {
198     /* CABAC context */
199     cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
200 
201     /* slice header */
202     slice_header_t *ps_slice_hdr = ps_ent_ctxt->ps_slice_hdr_base;
203     const UWORD8 u1_slice_type = ps_slice_hdr->u1_slice_type;
204     WORD8 i1_cabac_init_idc = 0;
205     bin_ctxt_model *au1_cabac_ctxt_table = ps_cabac_ctxt->au1_cabac_ctxt_table;
206     UWORD8 u1_qp_y = ps_slice_hdr->i1_slice_qp;
207 
208     ih264e_init_cabac_enc_envirnoment(&ps_cabac_ctxt->s_cab_enc_env);
209 
210     ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 0;
211 
212     if (ISLICE != u1_slice_type)
213     {
214         i1_cabac_init_idc = ps_slice_hdr->i1_cabac_init_idc;
215     }
216     else
217     {
218         i1_cabac_init_idc = 3;
219 
220     }
221 
222     memcpy(au1_cabac_ctxt_table,
223            gau1_ih264_cabac_ctxt_init_table[i1_cabac_init_idc][u1_qp_y],
224            NUM_CABAC_CTXTS * sizeof(bin_ctxt_model));
225 
226 }
227