/src/libavc/encoder/svc/isvce_cabac.c
Line | Count | Source |
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.c |
25 | | * |
26 | | * @brief |
27 | | * Contains all leaf level functions for CABAC entropy coding. |
28 | | * |
29 | | * |
30 | | * @author |
31 | | * Doney Alex |
32 | | * |
33 | | * @par List of Functions: |
34 | | * |
35 | | * |
36 | | * @remarks |
37 | | * None |
38 | | * |
39 | | ******************************************************************************* |
40 | | */ |
41 | | |
42 | | /*****************************************************************************/ |
43 | | /* File Includes */ |
44 | | /*****************************************************************************/ |
45 | | |
46 | | /* System include files */ |
47 | | #include <stdio.h> |
48 | | #include <assert.h> |
49 | | #include <limits.h> |
50 | | #include <string.h> |
51 | | |
52 | | /* User include files */ |
53 | | #include "ih264e_config.h" |
54 | | #include "ih264_typedefs.h" |
55 | | #include "iv2.h" |
56 | | #include "ive2.h" |
57 | | #include "ih264_debug.h" |
58 | | #include "ih264_macros.h" |
59 | | #include "isvc_defs.h" |
60 | | #include "isvce_defs.h" |
61 | | #include "isvc_macros.h" |
62 | | #include "ih264e_error.h" |
63 | | #include "ih264e_bitstream.h" |
64 | | #include "ime_distortion_metrics.h" |
65 | | #include "ime_defs.h" |
66 | | #include "ime_structs.h" |
67 | | #include "ih264_error.h" |
68 | | #include "isvc_structs.h" |
69 | | #include "isvc_trans_quant_itrans_iquant.h" |
70 | | #include "isvc_inter_pred_filters.h" |
71 | | #include "isvc_mem_fns.h" |
72 | | #include "ih264_padding.h" |
73 | | #include "ih264_platform_macros.h" |
74 | | #include "ih264_intra_pred_filters.h" |
75 | | #include "ih264_deblk_edge_filters.h" |
76 | | #include "isvc_cabac_tables.h" |
77 | | #include "irc_cntrl_param.h" |
78 | | #include "irc_frame_info_collector.h" |
79 | | #include "isvce_rate_control.h" |
80 | | #include "isvce_cabac_structs.h" |
81 | | #include "isvce_structs.h" |
82 | | #include "isvce_cabac.h" |
83 | | #include "isvce_encode_header.h" |
84 | | #include "ih264_cavlc_tables.h" |
85 | | #include "ih264e_statistics.h" |
86 | | #include "ih264e_trace.h" |
87 | | |
88 | | /*****************************************************************************/ |
89 | | /* Function Definitions */ |
90 | | /*****************************************************************************/ |
91 | | |
92 | | /** |
93 | | ******************************************************************************* |
94 | | * |
95 | | * @brief |
96 | | * k-th order Exp-Golomb (UEGk) binarization process: Implements concatenated |
97 | | * unary/ k-th order Exp-Golomb (UEGk) binarization process, |
98 | | * where k = 0 as defined in 9.3.2.3 of ITU_T_H264-201402 |
99 | | * |
100 | | * @param[in] i2_sufs |
101 | | * Suffix bit string |
102 | | * |
103 | | * @param[in] pi1_bins_len |
104 | | * Pointer to length of tthe string |
105 | | * |
106 | | * @returns Binarized value |
107 | | * |
108 | | * @remarks |
109 | | * None |
110 | | * |
111 | | ******************************************************************************* |
112 | | */ |
113 | | |
114 | | UWORD32 isvce_cabac_UEGk0_binarization(WORD16 i2_sufs, WORD8 *pi1_bins_len) |
115 | 8.37M | { |
116 | 8.37M | WORD32 unary_length; |
117 | 8.37M | UWORD32 u4_sufs_shiftk_plus1, u4_egk, u4_unary_bins; |
118 | | |
119 | 8.37M | u4_sufs_shiftk_plus1 = i2_sufs + 1; |
120 | | |
121 | 8.37M | unary_length = (32 - CLZ(u4_sufs_shiftk_plus1) + (0 == u4_sufs_shiftk_plus1)); |
122 | | |
123 | | /* unary code with (unary_length-1) '1's and terminating '0' bin */ |
124 | 8.37M | u4_unary_bins = (1 << unary_length) - 2; |
125 | | |
126 | | /* insert the symbol prefix of (unary length - 1) bins */ |
127 | 8.37M | u4_egk = (u4_unary_bins << (unary_length - 1)) | |
128 | 8.37M | (u4_sufs_shiftk_plus1 & ((1 << (unary_length - 1)) - 1)); |
129 | | |
130 | | /* length of the code = 2 *(unary_length - 1) + 1 + k */ |
131 | 8.37M | *pi1_bins_len = (2 * unary_length) - 1; |
132 | | |
133 | 8.37M | return (u4_egk); |
134 | 8.37M | } |
135 | | |
136 | | /** |
137 | | ******************************************************************************* |
138 | | * |
139 | | * @brief |
140 | | * Get cabac context for the MB :calculates the pointers to Top and left |
141 | | * cabac neighbor context depending upon neighbor availability. |
142 | | * |
143 | | * @param[in] ps_ent_ctxt |
144 | | * Pointer to entropy context structure |
145 | | * |
146 | | * @param[in] u4_mb_type |
147 | | * Type of MB |
148 | | * |
149 | | * @returns |
150 | | * |
151 | | * @remarks |
152 | | * None |
153 | | * |
154 | | ******************************************************************************* |
155 | | */ |
156 | | void isvce_get_cabac_context(isvce_entropy_ctxt_t *ps_ent_ctxt, WORD32 u4_mb_type) |
157 | 4.95M | { |
158 | | /* CABAC context */ |
159 | 4.95M | isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; |
160 | 4.95M | isvce_mb_info_ctxt_t *ps_ctx_inc_mb_map; |
161 | 4.95M | cab_csbp_t *ps_lft_csbp; |
162 | | |
163 | 4.95M | WORD32 i4_lft_avail, i4_top_avail, i4_is_intra; |
164 | 4.95M | WORD32 i4_mb_x, i4_mb_y; |
165 | 4.95M | UWORD8 *pu1_slice_idx = ps_ent_ctxt->pu1_slice_idx; |
166 | | |
167 | 4.95M | i4_is_intra = ((u4_mb_type == I16x16) || (u4_mb_type == I8x8) || (u4_mb_type == I4x4)); |
168 | | |
169 | | /* derive neighbor availability */ |
170 | 4.95M | i4_mb_x = ps_ent_ctxt->i4_mb_x; |
171 | 4.95M | i4_mb_y = ps_ent_ctxt->i4_mb_y; |
172 | 4.95M | pu1_slice_idx += (i4_mb_y * ps_ent_ctxt->i4_wd_mbs); |
173 | | /* left macroblock availability */ |
174 | 4.95M | i4_lft_avail = (i4_mb_x == 0 || (pu1_slice_idx[i4_mb_x - 1] != pu1_slice_idx[i4_mb_x])) ? 0 : 1; |
175 | | /* top macroblock availability */ |
176 | 4.95M | i4_top_avail = (i4_mb_y == 0 || |
177 | 4.63M | (pu1_slice_idx[i4_mb_x - ps_ent_ctxt->i4_wd_mbs] != pu1_slice_idx[i4_mb_x])) |
178 | 4.95M | ? 0 |
179 | 4.95M | : 1; |
180 | 4.95M | i4_mb_x = ps_ent_ctxt->i4_mb_x; |
181 | 4.95M | ps_ctx_inc_mb_map = ps_cabac_ctxt->ps_mb_map_ctxt_inc; |
182 | 4.95M | ps_cabac_ctxt->ps_curr_ctxt_mb_info = ps_ctx_inc_mb_map + i4_mb_x; |
183 | 4.95M | ps_cabac_ctxt->ps_left_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info; |
184 | 4.95M | ps_cabac_ctxt->ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info; |
185 | 4.95M | ps_lft_csbp = ps_cabac_ctxt->ps_lft_csbp; |
186 | 4.95M | ps_cabac_ctxt->pu1_left_y_ac_csbp = &ps_lft_csbp->u1_y_ac_csbp_top_mb; |
187 | 4.95M | ps_cabac_ctxt->pu1_left_uv_ac_csbp = &ps_lft_csbp->u1_uv_ac_csbp_top_mb; |
188 | 4.95M | ps_cabac_ctxt->pu1_left_yuv_dc_csbp = &ps_lft_csbp->u1_yuv_dc_csbp_top_mb; |
189 | 4.95M | ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc = &ps_cabac_ctxt->i1_left_ref_idx_ctx_inc_arr[0][0]; |
190 | 4.95M | ps_cabac_ctxt->pu1_left_mv_ctxt_inc = ps_cabac_ctxt->u1_left_mv_ctxt_inc_arr[0]; |
191 | | |
192 | 4.95M | if(i4_lft_avail) ps_cabac_ctxt->ps_left_ctxt_mb_info = ps_cabac_ctxt->ps_curr_ctxt_mb_info - 1; |
193 | 4.95M | if(i4_top_avail) ps_cabac_ctxt->ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_curr_ctxt_mb_info; |
194 | | |
195 | 4.95M | if(!i4_lft_avail) |
196 | 97.5k | { |
197 | 97.5k | UWORD8 u1_def_csbp = i4_is_intra ? 0xf : 0; |
198 | 97.5k | *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = u1_def_csbp; |
199 | 97.5k | *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = u1_def_csbp; |
200 | 97.5k | *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = u1_def_csbp; |
201 | 97.5k | *((UWORD32 *) ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc) = 0; |
202 | 97.5k | memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); |
203 | 97.5k | } |
204 | 4.95M | if(!i4_top_avail) |
205 | 317k | { |
206 | 317k | UWORD8 u1_def_csbp = i4_is_intra ? 0xff : 0; |
207 | 317k | ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_ac_csbp = u1_def_csbp; |
208 | 317k | ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_dc_csbp = u1_def_csbp; |
209 | 317k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[0] = |
210 | 317k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[1] = |
211 | 317k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[2] = |
212 | 317k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[3] = 0; |
213 | 317k | memset(ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv, 0, 16); |
214 | 317k | } |
215 | 4.95M | } |
216 | | |
217 | | /** |
218 | | ******************************************************************************* |
219 | | * @brief |
220 | | * flushing at termination: Explained in flowchart 9-12(ITU_T_H264-201402). |
221 | | * |
222 | | * @param[in] ps_cabac_ctxt |
223 | | * pointer to cabac context (handle) |
224 | | * |
225 | | * @returns none |
226 | | * |
227 | | * @remarks |
228 | | * None |
229 | | * |
230 | | ******************************************************************************* |
231 | | */ |
232 | | void isvce_cabac_flush(isvce_cabac_ctxt_t *ps_cabac_ctxt) |
233 | 13.1k | { |
234 | | /* bit stream ptr */ |
235 | 13.1k | bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm; |
236 | 13.1k | encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env); |
237 | 13.1k | UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low; |
238 | 13.1k | UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen; |
239 | 13.1k | UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer; |
240 | 13.1k | UWORD32 u4_strm_buf_offset = ps_stream->u4_strm_buf_offset; |
241 | 13.1k | WORD32 zero_run = ps_stream->i4_zero_bytes_run; |
242 | 13.1k | UWORD32 u4_out_standing_bytes = ps_cab_enc_env->u4_out_standing_bytes; |
243 | | |
244 | | /************************************************************************/ |
245 | | /* Insert the carry (propogated in previous byte) along with */ |
246 | | /* outstanding bytes (if any) and flush remaining bits */ |
247 | | /************************************************************************/ |
248 | 13.1k | { |
249 | | /* carry = 1 => putbit(1); carry propogated due to L renorm */ |
250 | 13.1k | WORD32 carry = (u4_low >> (u4_bits_gen + CABAC_BITS)) & 0x1; |
251 | 13.1k | WORD32 last_byte; |
252 | 13.1k | WORD32 bits_left; |
253 | 13.1k | WORD32 rem_bits; |
254 | | |
255 | | /* carry exists only if pu1_strm_buf has at least 1 byte of data */ |
256 | 13.1k | carry = carry && (u4_strm_buf_offset > 0); |
257 | | |
258 | 13.1k | if(carry) |
259 | 537 | { |
260 | | /* CORNER CASE: if the previous data is 0x000003, then EPB will be |
261 | | inserted and the data will become 0x00000303 and if the carry is present, |
262 | | it will be added with the last byte and it will become 0x00000304 which |
263 | | is not correct as per standard */ |
264 | | /* so check for previous four bytes and if it is equal to 0x00000303 |
265 | | then subtract u4_strm_buf_offset by 1 */ |
266 | 537 | if((u4_strm_buf_offset >= 4) && pu1_strm_buf[u4_strm_buf_offset - 1] == 0x03 && |
267 | 137 | pu1_strm_buf[u4_strm_buf_offset - 2] == 0x03 && |
268 | 14 | pu1_strm_buf[u4_strm_buf_offset - 3] == 0x00 && |
269 | 4 | pu1_strm_buf[u4_strm_buf_offset - 4] == 0x00) |
270 | 3 | { |
271 | 3 | u4_strm_buf_offset -= 1; |
272 | 3 | } |
273 | | /* previous byte carry add will not result in overflow to */ |
274 | | /* u4_strm_buf_offset - 2 as we track 0xff as outstanding bytes */ |
275 | 537 | pu1_strm_buf[u4_strm_buf_offset - 1] += carry; |
276 | 537 | zero_run = 0; |
277 | 537 | } |
278 | | |
279 | | /* Insert outstanding bytes (if any) */ |
280 | 16.0k | while(u4_out_standing_bytes) |
281 | 2.90k | { |
282 | 2.90k | UWORD8 u1_0_or_ff = carry ? 0 : 0xFF; |
283 | | |
284 | 2.90k | PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, u1_0_or_ff, zero_run); |
285 | 2.90k | u4_out_standing_bytes--; |
286 | 2.90k | } |
287 | | |
288 | | /* clear the carry in low */ |
289 | 13.1k | if(carry) |
290 | 537 | { |
291 | 537 | u4_low &= ((1 << (u4_bits_gen + CABAC_BITS)) - 1); |
292 | 537 | } |
293 | | |
294 | | /* extract the remaining bits; */ |
295 | | /* includes additional msb bit of low as per Figure 9-12 */ |
296 | 13.1k | bits_left = u4_bits_gen + 1; |
297 | 13.1k | rem_bits = (u4_low >> (u4_bits_gen + CABAC_BITS - bits_left)); |
298 | | |
299 | 13.1k | if(bits_left >= 8) |
300 | 6.16k | { |
301 | 6.16k | last_byte = (rem_bits >> (bits_left - 8)) & 0xFF; |
302 | 6.16k | PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, last_byte, zero_run); |
303 | 6.16k | bits_left -= 8; |
304 | 6.16k | } |
305 | | |
306 | | /* insert last byte along with rbsp stop bit(1) and 0's in the end */ |
307 | 13.1k | last_byte = |
308 | 13.1k | (rem_bits << (8 - bits_left)) | (1 << (7 - bits_left) | (1 << (7 - bits_left - 1))); |
309 | 13.1k | last_byte &= 0xFF; |
310 | 13.1k | PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, last_byte, zero_run); |
311 | | |
312 | | /* update the state variables and return success */ |
313 | 13.1k | ps_stream->u4_strm_buf_offset = u4_strm_buf_offset; |
314 | 13.1k | ps_stream->i4_zero_bytes_run = 0; |
315 | | /* Default init values for scratch variables of bitstream context */ |
316 | 13.1k | ps_stream->u4_cur_word = 0; |
317 | 13.1k | ps_stream->i4_bits_left_in_cw = WORD_SIZE; |
318 | 13.1k | } |
319 | 13.1k | } |
320 | | |
321 | | /** |
322 | | ****************************************************************************** |
323 | | * |
324 | | * @brief Puts new byte (and outstanding bytes) into bitstream after cabac |
325 | | * renormalization |
326 | | * |
327 | | * @par Description |
328 | | * 1. Extract the leading byte of low(L) |
329 | | * 2. If leading byte=0xff increment outstanding bytes and return |
330 | | * (as the actual bits depend on carry propogation later) |
331 | | * 3. If leading byte is not 0xff check for any carry propogation |
332 | | * 4. Insert the carry (propogated in previous byte) along with outstanding |
333 | | * bytes (if any) and leading byte |
334 | | * |
335 | | * |
336 | | * @param[in] ps_cabac_ctxt |
337 | | * pointer to cabac context (handle) |
338 | | * |
339 | | * @return |
340 | | * |
341 | | ****************************************************************************** |
342 | | */ |
343 | | void isvce_cabac_put_byte(isvce_cabac_ctxt_t *ps_cabac_ctxt) |
344 | 30.6M | { |
345 | | /* bit stream ptr */ |
346 | 30.6M | bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm; |
347 | 30.6M | encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env); |
348 | 30.6M | UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low; |
349 | 30.6M | UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen; |
350 | 30.6M | WORD32 lead_byte = u4_low >> (u4_bits_gen + CABAC_BITS - 8); |
351 | | |
352 | | /* Sanity checks */ |
353 | 30.6M | ASSERT((ps_cab_enc_env->u4_code_int_range >= 256) && (ps_cab_enc_env->u4_code_int_range < 512)); |
354 | 30.6M | ASSERT((u4_bits_gen >= 8)); |
355 | | |
356 | | /* update bits generated and low after extracting leading byte */ |
357 | 30.6M | u4_bits_gen -= 8; |
358 | 30.6M | ps_cab_enc_env->u4_code_int_low &= ((1 << (CABAC_BITS + u4_bits_gen)) - 1); |
359 | 30.6M | ps_cab_enc_env->u4_bits_gen = u4_bits_gen; |
360 | | |
361 | | /************************************************************************/ |
362 | | /* 1. Extract the leading byte of low(L) */ |
363 | | /* 2. If leading byte=0xff increment outstanding bytes and return */ |
364 | | /* (as the actual bits depend on carry propogation later) */ |
365 | | /* 3. If leading byte is not 0xff check for any carry propogation */ |
366 | | /* 4. Insert the carry (propogated in previous byte) along with */ |
367 | | /* outstanding bytes (if any) and leading byte */ |
368 | | /************************************************************************/ |
369 | 30.6M | if(lead_byte == 0xff) |
370 | 152k | { |
371 | | /* actual bits depend on carry propogration */ |
372 | 152k | ps_cab_enc_env->u4_out_standing_bytes++; |
373 | 152k | return; |
374 | 152k | } |
375 | 30.4M | else |
376 | 30.4M | { |
377 | 30.4M | UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer; |
378 | 30.4M | UWORD32 u4_strm_buf_offset = ps_stream->u4_strm_buf_offset; |
379 | | /* carry = 1 => putbit(1); carry propogated due to L renorm */ |
380 | 30.4M | WORD32 carry = (lead_byte >> 8) & 0x1; |
381 | 30.4M | WORD32 zero_run = ps_stream->i4_zero_bytes_run; |
382 | 30.4M | UWORD32 u4_out_standing_bytes = ps_cab_enc_env->u4_out_standing_bytes; |
383 | | |
384 | | /*********************************************************************/ |
385 | | /* Insert the carry propogated in previous byte */ |
386 | | /* */ |
387 | | /* Note : Do not worry about corruption into slice header align byte */ |
388 | | /* This is because the first bin cannot result in overflow */ |
389 | | /*********************************************************************/ |
390 | 30.4M | if(carry) |
391 | 1.84M | { |
392 | | /* CORNER CASE: if the previous data is 0x000003, then EPB will be |
393 | | inserted and the data will become 0x00000303 and if the carry is present, |
394 | | it will be added with the last byte and it will become 0x00000304 which |
395 | | is not correct as per standard */ |
396 | | /* so check for previous four bytes and if it is equal to 0x00000303 |
397 | | then subtract u4_strm_buf_offset by 1 */ |
398 | 1.84M | if((u4_strm_buf_offset > 3) && (pu1_strm_buf[u4_strm_buf_offset - 1] == 0x03) && |
399 | 7.92k | (pu1_strm_buf[u4_strm_buf_offset - 2] == 0x03) && |
400 | 981 | (pu1_strm_buf[u4_strm_buf_offset - 3] == 0x00) && |
401 | 111 | (pu1_strm_buf[u4_strm_buf_offset - 4] == 0x00)) |
402 | 70 | { |
403 | 70 | u4_strm_buf_offset -= 1; |
404 | 70 | } |
405 | | |
406 | | /* previous byte carry add will not result in overflow to */ |
407 | | /* u4_strm_buf_offset - 2 as we track 0xff as outstanding bytes */ |
408 | 1.84M | if(u4_strm_buf_offset > 0) |
409 | 1.84M | { |
410 | 1.84M | pu1_strm_buf[u4_strm_buf_offset - 1] += carry; |
411 | 1.84M | zero_run = 0; |
412 | 1.84M | } |
413 | 1.84M | } |
414 | | |
415 | | /* Insert outstanding bytes (if any) */ |
416 | 30.6M | while(u4_out_standing_bytes) |
417 | 149k | { |
418 | 149k | UWORD8 u1_0_or_ff = carry ? 0 : 0xFF; |
419 | | |
420 | 149k | PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, u1_0_or_ff, zero_run); |
421 | | |
422 | 149k | u4_out_standing_bytes--; |
423 | 149k | } |
424 | 30.4M | ps_cab_enc_env->u4_out_standing_bytes = 0; |
425 | | |
426 | | /* Insert the leading byte */ |
427 | 30.4M | lead_byte &= 0xFF; |
428 | 30.4M | PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, lead_byte, zero_run); |
429 | | |
430 | | /* update the state variables and return success */ |
431 | 30.4M | ps_stream->u4_strm_buf_offset = u4_strm_buf_offset; |
432 | 30.4M | ps_stream->i4_zero_bytes_run = zero_run; |
433 | 30.4M | } |
434 | 30.6M | } |
435 | | |
436 | | /** |
437 | | ****************************************************************************** |
438 | | * |
439 | | * @brief Codes a bin based on probablilty and mps packed context model |
440 | | * |
441 | | * @par Description |
442 | | * 1. Apart from encoding bin, context model is updated as per state transition |
443 | | * 2. Range and Low renormalization is done based on bin and original state |
444 | | * 3. After renorm bistream is updated (if required) |
445 | | * |
446 | | * @param[in] ps_cabac |
447 | | * pointer to cabac context (handle) |
448 | | * |
449 | | * @param[in] bin |
450 | | * bin(boolean) to be encoded |
451 | | * |
452 | | * @param[in] pu1_bin_ctxts |
453 | | * index of cabac context model containing pState[bits 5-0] | MPS[bit6] |
454 | | * |
455 | | * @return |
456 | | * |
457 | | ****************************************************************************** |
458 | | */ |
459 | | void isvce_cabac_encode_bin(isvce_cabac_ctxt_t *ps_cabac, WORD32 bin, bin_ctxt_model *pu1_bin_ctxts) |
460 | 332M | { |
461 | 332M | encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env); |
462 | 332M | UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range; |
463 | 332M | UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low; |
464 | 332M | UWORD32 u4_rlps; |
465 | 332M | UWORD8 state_mps = (*pu1_bin_ctxts) & 0x3F; |
466 | 332M | UWORD8 u1_mps = !!((*pu1_bin_ctxts) & (0x40)); |
467 | 332M | WORD32 shift; |
468 | 332M | UWORD32 u4_table_val; |
469 | | /* Sanity checks */ |
470 | 332M | ASSERT((bin == 0) || (bin == 1)); |
471 | 332M | ASSERT((u4_range >= 256) && (u4_range < 512)); |
472 | | |
473 | | /* Get the lps range from LUT based on quantized range and state */ |
474 | 332M | u4_table_val = gau4_isvc_cabac_table[state_mps][(u4_range >> 6) & 0x3]; |
475 | 332M | u4_rlps = u4_table_val & 0xFF; |
476 | 332M | u4_range -= u4_rlps; |
477 | | |
478 | | /* check if bin is mps or lps */ |
479 | 332M | if(u1_mps ^ bin) |
480 | 34.5M | { |
481 | | /* lps path; L= L + R; R = RLPS */ |
482 | 34.5M | u4_low += u4_range; |
483 | 34.5M | u4_range = u4_rlps; |
484 | 34.5M | if(state_mps == 0) |
485 | 1.59M | { |
486 | | /* MPS(CtxIdx) = 1 - MPS(CtxIdx) */ |
487 | 1.59M | u1_mps = 1 - u1_mps; |
488 | 1.59M | } /* update the context model from state transition LUT */ |
489 | | |
490 | 34.5M | state_mps = (u4_table_val >> 15) & 0x3F; |
491 | 34.5M | } |
492 | 297M | else |
493 | 297M | { /* update the context model from state transition LUT */ |
494 | 297M | state_mps = (u4_table_val >> 8) & 0x3F; |
495 | 297M | } |
496 | | |
497 | 332M | (*pu1_bin_ctxts) = (u1_mps << 6) | state_mps; |
498 | | |
499 | | /*****************************************************************/ |
500 | | /* Renormalization; calculate bits generated based on range(R) */ |
501 | | /* Note : 6 <= R < 512; R is 2 only for terminating encode */ |
502 | | /*****************************************************************/ |
503 | 332M | GETRANGE(shift, u4_range); |
504 | 332M | shift = 9 - shift; |
505 | 332M | u4_low <<= shift; |
506 | 332M | u4_range <<= shift; |
507 | | |
508 | | /* bits to be inserted in the bitstream */ |
509 | 332M | ps_cab_enc_env->u4_bits_gen += shift; |
510 | 332M | ps_cab_enc_env->u4_code_int_range = u4_range; |
511 | 332M | ps_cab_enc_env->u4_code_int_low = u4_low; |
512 | | |
513 | | /* generate stream when a byte is ready */ |
514 | 332M | if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS) |
515 | 15.2M | { |
516 | 15.2M | isvce_cabac_put_byte(ps_cabac); |
517 | 15.2M | } |
518 | 332M | } |
519 | | |
520 | | /** |
521 | | ******************************************************************************* |
522 | | * |
523 | | * @brief |
524 | | * Encoding process for a binary decision :implements encoding process of a |
525 | | decision |
526 | | * as defined in 9.3.4.2 . This function encodes multiple bins, of a symbol. |
527 | | Implements |
528 | | * flowchart Figure 9-7( ITU_T_H264-201402) |
529 | | * |
530 | | * @param[in] u4_bins |
531 | | * array of bin values |
532 | | * |
533 | | * @param[in] i1_bins_len |
534 | | * Length of bins, maximum 32 |
535 | | * |
536 | | * @param[in] u4_ctx_inc |
537 | | * CtxInc, byte0- bin0, byte1-bin1 .. |
538 | | * |
539 | | * @param[in] i1_valid_len |
540 | | * valid length of bins, after that CtxInc is constant |
541 | | * |
542 | | * @param[in] pu1_bin_ctxt_type |
543 | | * Pointer to binary contexts |
544 | | |
545 | | * @param[in] ps_cabac |
546 | | * Pointer to cabac_context_structure |
547 | | * |
548 | | * @returns |
549 | | * |
550 | | * @remarks |
551 | | * None |
552 | | * |
553 | | ******************************************************************************* |
554 | | */ |
555 | | void isvce_encode_decision_bins(UWORD32 u4_bins, WORD8 i1_bins_len, UWORD32 u4_ctx_inc, |
556 | | WORD8 i1_valid_len, bin_ctxt_model *pu1_bin_ctxt_type, |
557 | | isvce_cabac_ctxt_t *ps_cabac) |
558 | 42.2M | { |
559 | 42.2M | WORD8 i; |
560 | 42.2M | UWORD8 u1_ctx_inc, u1_bin; |
561 | | |
562 | 271M | for(i = 0; i < i1_bins_len; i++) |
563 | 229M | { |
564 | 229M | u1_bin = (u4_bins & 0x01); |
565 | 229M | u4_bins = u4_bins >> 1; |
566 | 229M | u1_ctx_inc = u4_ctx_inc & 0x0f; |
567 | 229M | if(i < i1_valid_len) u4_ctx_inc = u4_ctx_inc >> 4; |
568 | | /* Encode the bin */ |
569 | 229M | isvce_cabac_encode_bin(ps_cabac, u1_bin, pu1_bin_ctxt_type + u1_ctx_inc); |
570 | 229M | } |
571 | 42.2M | } |
572 | | |
573 | | /** |
574 | | ******************************************************************************* |
575 | | * @brief |
576 | | * Encoding process for a binary decision before termination:Encoding process |
577 | | * of a termination(9.3.4.5 :ITU_T_H264-201402) . Explained in flowchart 9-11. |
578 | | * |
579 | | * @param[in] ps_cabac |
580 | | * Pointer to cabac structure |
581 | | * |
582 | | * @param[in] term_bin |
583 | | * Symbol value, end of slice or not, term_bin is binary |
584 | | * |
585 | | * @returns |
586 | | * |
587 | | * @remarks |
588 | | * None |
589 | | * |
590 | | ******************************************************************************* |
591 | | */ |
592 | | void isvce_cabac_encode_terminate(isvce_cabac_ctxt_t *ps_cabac, WORD32 term_bin) |
593 | 4.95M | { |
594 | 4.95M | encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env); |
595 | | |
596 | 4.95M | UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range; |
597 | 4.95M | UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low; |
598 | 4.95M | UWORD32 u4_rlps; |
599 | 4.95M | WORD32 shift; |
600 | | |
601 | | /* Sanity checks */ |
602 | 4.95M | ASSERT((u4_range >= 256) && (u4_range < 512)); |
603 | 4.95M | ASSERT((term_bin == 0) || (term_bin == 1)); |
604 | | |
605 | | /* term_bin = 1 has lps range = 2 */ |
606 | 4.95M | u4_rlps = 2; |
607 | 4.95M | u4_range -= u4_rlps; |
608 | | |
609 | | /* if terminate L is incremented by curR and R=2 */ |
610 | 4.95M | if(term_bin) |
611 | 13.1k | { |
612 | | /* lps path; L= L + R; R = RLPS */ |
613 | 13.1k | u4_low += u4_range; |
614 | 13.1k | u4_range = u4_rlps; |
615 | 13.1k | } |
616 | | |
617 | | /*****************************************************************/ |
618 | | /* Renormalization; calculate bits generated based on range(R) */ |
619 | | /* Note : 6 <= R < 512; R is 2 only for terminating encode */ |
620 | | /*****************************************************************/ |
621 | 4.95M | GETRANGE(shift, u4_range); |
622 | 4.95M | shift = 9 - shift; |
623 | 4.95M | u4_low <<= shift; |
624 | 4.95M | u4_range <<= shift; |
625 | | |
626 | | /* bits to be inserted in the bitstream */ |
627 | 4.95M | ps_cab_enc_env->u4_bits_gen += shift; |
628 | 4.95M | ps_cab_enc_env->u4_code_int_range = u4_range; |
629 | 4.95M | ps_cab_enc_env->u4_code_int_low = u4_low; |
630 | | |
631 | | /* generate stream when a byte is ready */ |
632 | 4.95M | if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS) |
633 | 22.0k | { |
634 | 22.0k | isvce_cabac_put_byte(ps_cabac); |
635 | 22.0k | } |
636 | | |
637 | 4.95M | if(term_bin) |
638 | 13.1k | { |
639 | 13.1k | isvce_cabac_flush(ps_cabac); |
640 | 13.1k | } |
641 | 4.95M | } |
642 | | |
643 | | /** |
644 | | ******************************************************************************* |
645 | | * @brief |
646 | | * Bypass encoding process for binary decisions: Explained (9.3.4.4 |
647 | | *:ITU_T_H264-201402) , flowchart 9-10. |
648 | | * |
649 | | * @param[ino] ps_cabac : pointer to cabac context (handle) |
650 | | * |
651 | | * @param[in] bin : bypass bin(0/1) to be encoded |
652 | | * |
653 | | * @returns |
654 | | * |
655 | | * @remarks |
656 | | * None |
657 | | * |
658 | | ******************************************************************************* |
659 | | */ |
660 | | |
661 | | void isvce_cabac_encode_bypass_bin(isvce_cabac_ctxt_t *ps_cabac, WORD32 bin) |
662 | 33.4M | { |
663 | 33.4M | encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env); |
664 | | |
665 | 33.4M | UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range; |
666 | 33.4M | UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low; |
667 | | |
668 | | /* Sanity checks */ |
669 | 33.4M | ASSERT((u4_range >= 256) && (u4_range < 512)); |
670 | 33.4M | ASSERT((bin == 0) || (bin == 1)); |
671 | | |
672 | 33.4M | u4_low <<= 1; |
673 | | /* add range if bin is 1 */ |
674 | 33.4M | if(bin) |
675 | 17.9M | { |
676 | 17.9M | u4_low += u4_range; |
677 | 17.9M | } |
678 | | |
679 | | /* 1 bit to be inserted in the bitstream */ |
680 | 33.4M | ps_cab_enc_env->u4_bits_gen++; |
681 | 33.4M | ps_cab_enc_env->u4_code_int_low = u4_low; |
682 | | |
683 | | /* generate stream when a byte is ready */ |
684 | 33.4M | if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS) |
685 | 4.18M | { |
686 | 4.18M | isvce_cabac_put_byte(ps_cabac); |
687 | 4.18M | } |
688 | 33.4M | } |
689 | | |
690 | | /** |
691 | | ****************************************************************************** |
692 | | * |
693 | | * @brief Encodes a series of bypass bins (FLC bypass bins) |
694 | | * |
695 | | * @par Description |
696 | | * This function is more optimal than calling isvce_cabac_encode_bypass_bin() |
697 | | * in a loop as cabac low, renorm and generating the stream (8bins at a time) |
698 | | * can be done in one operation |
699 | | * |
700 | | * @param[inout]ps_cabac |
701 | | * pointer to cabac context (handle) |
702 | | * |
703 | | * @param[in] u4_bins |
704 | | * syntax element to be coded (as FLC bins) |
705 | | * |
706 | | * @param[in] num_bins |
707 | | * This is the FLC length for u4_sym |
708 | | * |
709 | | * @return |
710 | | * |
711 | | ****************************************************************************** |
712 | | */ |
713 | | |
714 | | void isvce_cabac_encode_bypass_bins(isvce_cabac_ctxt_t *ps_cabac, UWORD32 u4_bins, WORD32 num_bins) |
715 | 8.43M | { |
716 | 8.43M | encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env); |
717 | | |
718 | 8.43M | UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range; |
719 | 8.43M | WORD32 next_byte; |
720 | | |
721 | | /* Sanity checks */ |
722 | 8.43M | ASSERT((num_bins < 33) && (num_bins > 0)); |
723 | 8.43M | ASSERT((u4_range >= 256) && (u4_range < 512)); |
724 | | |
725 | | /* Compute bit always to populate the trace */ |
726 | | /* increment bits generated by num_bins */ |
727 | | |
728 | | /* Encode 8bins at a time and put in the bit-stream */ |
729 | 15.0M | while(num_bins > 8) |
730 | 6.58M | { |
731 | 6.58M | num_bins -= 8; |
732 | | |
733 | 6.58M | next_byte = (u4_bins >> (num_bins)) & 0xff; |
734 | | |
735 | | /* L = (L << 8) + (R * next_byte) */ |
736 | 6.58M | ps_cab_enc_env->u4_code_int_low <<= 8; |
737 | 6.58M | ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range); |
738 | 6.58M | ps_cab_enc_env->u4_bits_gen += 8; |
739 | | |
740 | 6.58M | if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS) |
741 | 6.58M | { |
742 | | /* insert the leading byte of low into stream */ |
743 | 6.58M | isvce_cabac_put_byte(ps_cabac); |
744 | 6.58M | } |
745 | 6.58M | } |
746 | | |
747 | | /* Update low with remaining bins and return */ |
748 | 8.43M | next_byte = (u4_bins & ((1 << num_bins) - 1)); |
749 | | |
750 | 8.43M | ps_cab_enc_env->u4_code_int_low <<= num_bins; |
751 | 8.43M | ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range); |
752 | 8.43M | ps_cab_enc_env->u4_bits_gen += num_bins; |
753 | | |
754 | 8.43M | if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS) |
755 | 4.31M | { |
756 | | /* insert the leading byte of low into stream */ |
757 | 4.31M | isvce_cabac_put_byte(ps_cabac); |
758 | 4.31M | } |
759 | 8.43M | } |