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