blob: 2d91058fca09ba947bb58f4758e6f0fe7a5cf950 [file] [log] [blame]
Harinarayanan K K134291e2015-06-18 16:03:38 +05301/******************************************************************************
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*
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_defs.h"
59#include "ih264e_defs.h"
60#include "ih264_macros.h"
61#include "ih264e_error.h"
62#include "ih264e_bitstream.h"
63#include "ime_distortion_metrics.h"
64#include "ime_defs.h"
65#include "ime_structs.h"
66#include "ih264_error.h"
67#include "ih264_structs.h"
68#include "ih264_trans_quant_itrans_iquant.h"
69#include "ih264_inter_pred_filters.h"
70#include "ih264_mem_fns.h"
71#include "ih264_padding.h"
72#include "ih264_platform_macros.h"
73#include "ih264_intra_pred_filters.h"
74#include "ih264_deblk_edge_filters.h"
75#include "ih264_cabac_tables.h"
76#include "irc_cntrl_param.h"
77#include "irc_frame_info_collector.h"
78#include "ih264e_rate_control.h"
79#include "ih264e_cabac_structs.h"
80#include "ih264e_structs.h"
81#include "ih264e_cabac.h"
82#include "ih264e_encode_header.h"
83#include "ih264_cavlc_tables.h"
84#include "ih264e_statistics.h"
85#include "ih264e_trace.h"
86
87
88/*****************************************************************************/
89/* Function Definitions */
90/*****************************************************************************/
91
92
93/**
94 *******************************************************************************
95 *
96 * @brief
97 * k-th order Exp-Golomb (UEGk) binarization process: Implements concatenated
98 * unary/ k-th order Exp-Golomb (UEGk) binarization process,
99 * where k = 0 as defined in 9.3.2.3 of ITU_T_H264-201402
100 *
101 * @param[in] i2_sufs
102 * Suffix bit string
103 *
104 * @param[in] pi1_bins_len
105 * Pointer to length of tthe string
106 *
107 * @returns Binarized value
108 *
109 * @remarks
110 * None
111 *
112 *******************************************************************************
113 */
Doney Alex9c6a2f72015-06-30 17:56:42 +0530114
Harinarayanan K K134291e2015-06-18 16:03:38 +0530115UWORD32 ih264e_cabac_UEGk0_binarization(WORD16 i2_sufs, WORD8 *pi1_bins_len)
116{
Doney Alex9c6a2f72015-06-30 17:56:42 +0530117 WORD32 unary_length;
118 UWORD32 u4_sufs_shiftk_plus1, u4_egk, u4_unary_bins;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530119
Doney Alex9c6a2f72015-06-30 17:56:42 +0530120 u4_sufs_shiftk_plus1 = i2_sufs + 1;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530121
Doney Alex9c6a2f72015-06-30 17:56:42 +0530122 unary_length = (32 - CLZ(u4_sufs_shiftk_plus1) + (0 == u4_sufs_shiftk_plus1));
Harinarayanan K K134291e2015-06-18 16:03:38 +0530123
Doney Alex9c6a2f72015-06-30 17:56:42 +0530124 /* unary code with (unary_length-1) '1's and terminating '0' bin */
125 u4_unary_bins = (1 << unary_length) - 2;
126
127 /* insert the symbol prefix of (unary length - 1) bins */
128 u4_egk = (u4_unary_bins << (unary_length - 1))
129 | (u4_sufs_shiftk_plus1 & ((1 << (unary_length - 1)) - 1));
130
131 /* length of the code = 2 *(unary_length - 1) + 1 + k */
132 *pi1_bins_len = (2 * unary_length) - 1;
133
134 return (u4_egk);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530135}
136
Harinarayanan K K134291e2015-06-18 16:03:38 +0530137/**
138 *******************************************************************************
139 *
140 * @brief
141 * Get cabac context for the MB :calculates the pointers to Top and left
142 * cabac neighbor context depending upon neighbor availability.
143 *
144 * @param[in] ps_ent_ctxt
145 * Pointer to entropy context structure
146 *
147 * @param[in] u4_mb_type
148 * Type of MB
149 *
150 * @returns
151 *
152 * @remarks
153 * None
154 *
155 *******************************************************************************
156 */
157void ih264e_get_cabac_context(entropy_ctxt_t *ps_ent_ctxt, WORD32 u4_mb_type)
158{
159
160 /* CABAC context */
161 cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
162 mb_info_ctxt_t *ps_ctx_inc_mb_map;
163 cab_csbp_t *ps_lft_csbp;
164
165 WORD32 i4_lft_avail, i4_top_avail, i4_is_intra;
166 WORD32 i4_mb_x, i4_mb_y;
167 UWORD8 *pu1_slice_idx = ps_ent_ctxt->pu1_slice_idx;
168
169 i4_is_intra = ((u4_mb_type == I16x16) || (u4_mb_type == I8x8)
170 || (u4_mb_type == I4x4));
171
172 /* derive neighbor availability */
173 i4_mb_x = ps_ent_ctxt->i4_mb_x;
174 i4_mb_y = ps_ent_ctxt->i4_mb_y;
175 pu1_slice_idx += (i4_mb_y * ps_ent_ctxt->i4_wd_mbs);
176 /* left macroblock availability */
177 i4_lft_avail = (i4_mb_x == 0
178 || (pu1_slice_idx[i4_mb_x - 1] != pu1_slice_idx[i4_mb_x])) ?
179 0 : 1;
180 /* top macroblock availability */
181 i4_top_avail = (i4_mb_y == 0
182 || (pu1_slice_idx[i4_mb_x - ps_ent_ctxt->i4_wd_mbs]
183 != pu1_slice_idx[i4_mb_x])) ? 0 : 1;
184 i4_mb_x = ps_ent_ctxt->i4_mb_x;
185 ps_ctx_inc_mb_map = ps_cabac_ctxt->ps_mb_map_ctxt_inc;
186 ps_cabac_ctxt->ps_curr_ctxt_mb_info = ps_ctx_inc_mb_map + i4_mb_x;
187 ps_cabac_ctxt->ps_left_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info;
188 ps_cabac_ctxt->ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info;
189 ps_lft_csbp = ps_cabac_ctxt->ps_lft_csbp;
190 ps_cabac_ctxt->pu1_left_y_ac_csbp = &ps_lft_csbp->u1_y_ac_csbp_top_mb;
191 ps_cabac_ctxt->pu1_left_uv_ac_csbp = &ps_lft_csbp->u1_uv_ac_csbp_top_mb;
192 ps_cabac_ctxt->pu1_left_yuv_dc_csbp = &ps_lft_csbp->u1_yuv_dc_csbp_top_mb;
193 ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc =
194 &ps_cabac_ctxt->i1_left_ref_idx_ctx_inc_arr[0][0];
195 ps_cabac_ctxt->pu1_left_mv_ctxt_inc =
196 ps_cabac_ctxt->u1_left_mv_ctxt_inc_arr[0];
197
198 if (i4_lft_avail)
199 ps_cabac_ctxt->ps_left_ctxt_mb_info =
200 ps_cabac_ctxt->ps_curr_ctxt_mb_info - 1;
201 if (i4_top_avail)
202 ps_cabac_ctxt->ps_top_ctxt_mb_info =
203 ps_cabac_ctxt->ps_curr_ctxt_mb_info;
204
205 if (!i4_lft_avail)
206 {
207 UWORD8 u1_def_csbp = i4_is_intra ? 0xf : 0;
208 *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = u1_def_csbp;
209 *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = u1_def_csbp;
210 *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = u1_def_csbp;
211 *((UWORD32 *) ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc) = 0;
212 memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16);
213 }
214 if (!i4_top_avail)
215 {
216 UWORD8 u1_def_csbp = i4_is_intra ? 0xff : 0;
217 ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_ac_csbp = u1_def_csbp;
218 ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_dc_csbp = u1_def_csbp;
219 ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[0] =
220 ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[1] =
221 ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[2] =
222 ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[3] = 0;
223 memset(ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv, 0, 16);
224 }
225
226}
227
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 *
Doney Alex9c6a2f72015-06-30 17:56:42 +0530238 * @returns none
Harinarayanan K K134291e2015-06-18 16:03:38 +0530239 *
240 * @remarks
241 * None
242 *
243 *******************************************************************************
244 */
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530245IH264E_ERROR_T ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt)
Harinarayanan K K134291e2015-06-18 16:03:38 +0530246{
Harinarayanan K K134291e2015-06-18 16:03:38 +0530247 /* bit stream ptr */
248 bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm;
249 encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
250 UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
251 UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen;
252 UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530253 UWORD32 u4_out_standing_bytes = ps_cab_enc_env->u4_out_standing_bytes;
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530254 IH264E_ERROR_T status = IH264E_SUCCESS;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530255
256 /************************************************************************/
257 /* Insert the carry (propogated in previous byte) along with */
258 /* outstanding bytes (if any) and flush remaining bits */
259 /************************************************************************/
260 {
261 /* carry = 1 => putbit(1); carry propogated due to L renorm */
262 WORD32 carry = (u4_low >> (u4_bits_gen + CABAC_BITS)) & 0x1;
263 WORD32 last_byte;
264 WORD32 bits_left;
265 WORD32 rem_bits;
266
Harinarayanan K K134291e2015-06-18 16:03:38 +0530267 if (carry)
268 {
269 /* CORNER CASE: if the previous data is 0x000003, then EPB will be inserted
270 and the data will become 0x00000303 and if the carry is present, it will
271 be added with the last byte and it will become 0x00000304 which is not correct
272 as per standard */
273 /* so check for previous four bytes and if it is equal to 0x00000303
274 then subtract u4_strm_buf_offset by 1 */
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530275 if (pu1_strm_buf[ps_stream->u4_strm_buf_offset - 1] == 0x03
276 && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 2] == 0x03
277 && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 3] == 0x00
278 && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 4] == 0x00)
Harinarayanan K K134291e2015-06-18 16:03:38 +0530279 {
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530280 ps_stream->u4_strm_buf_offset -= 1;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530281 }
282 /* previous byte carry add will not result in overflow to */
283 /* u4_strm_buf_offset - 2 as we track 0xff as outstanding bytes */
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530284 pu1_strm_buf[ps_stream->u4_strm_buf_offset - 1] += carry;
285 ps_stream->i4_zero_bytes_run = 0;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530286 }
287
288 /* Insert outstanding bytes (if any) */
289 while (u4_out_standing_bytes)
290 {
291 UWORD8 u1_0_or_ff = carry ? 0 : 0xFF;
292
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530293 status |= ih264e_put_byte_epb(ps_stream, u1_0_or_ff);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530294 u4_out_standing_bytes--;
295 }
296
297 /* clear the carry in low */
298 u4_low &= ((1 << (u4_bits_gen + CABAC_BITS)) - 1);
299
300 /* extract the remaining bits; */
301 /* includes additional msb bit of low as per Figure 9-12 */
302 bits_left = u4_bits_gen + 1;
303 rem_bits = (u4_low >> (u4_bits_gen + CABAC_BITS - bits_left));
304
305 if (bits_left >= 8)
306 {
307 last_byte = (rem_bits >> (bits_left - 8)) & 0xFF;
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530308 status |= ih264e_put_byte_epb(ps_stream, last_byte);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530309 bits_left -= 8;
310 }
311
312 /* insert last byte along with rbsp stop bit(1) and 0's in the end */
313 last_byte = (rem_bits << (8 - bits_left))
314 | (1 << (7 - bits_left) | (1 << (7 - bits_left - 1)));
315 last_byte &= 0xFF;
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530316 status |= ih264e_put_byte_epb(ps_stream, last_byte);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530317
318 /* update the state variables and return success */
Harinarayanan K K134291e2015-06-18 16:03:38 +0530319 ps_stream->i4_zero_bytes_run = 0;
320 /* Default init values for scratch variables of bitstream context */
321 ps_stream->u4_cur_word = 0;
322 ps_stream->i4_bits_left_in_cw = WORD_SIZE;
323
Harinarayanan K K134291e2015-06-18 16:03:38 +0530324 }
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530325 return status;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530326}
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 *
343 * @param[in] ps_cabac_ctxt
344 * pointer to cabac context (handle)
345 *
346 * @return
347 *
348 ******************************************************************************
349 */
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530350IH264E_ERROR_T ih264e_cabac_put_byte(cabac_ctxt_t *ps_cabac_ctxt)
Harinarayanan K K134291e2015-06-18 16:03:38 +0530351{
Harinarayanan K K134291e2015-06-18 16:03:38 +0530352 /* bit stream ptr */
353 bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm;
354 encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
355 UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
356 UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen;
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530357 UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530358 WORD32 lead_byte = u4_low >> (u4_bits_gen + CABAC_BITS - 8);
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530359 IH264E_ERROR_T status = IH264E_SUCCESS;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530360
361 /* Sanity checks */
362 ASSERT((ps_cab_enc_env->u4_code_int_range >= 256)
363 && (ps_cab_enc_env->u4_code_int_range < 512));
364 ASSERT((u4_bits_gen >= 8));
365
366 /* update bits generated and low after extracting leading byte */
367 u4_bits_gen -= 8;
368 ps_cab_enc_env->u4_code_int_low &= ((1 << (CABAC_BITS + u4_bits_gen)) - 1);
369 ps_cab_enc_env->u4_bits_gen = u4_bits_gen;
370
371 /************************************************************************/
372 /* 1. Extract the leading byte of low(L) */
373 /* 2. If leading byte=0xff increment outstanding bytes and return */
374 /* (as the actual bits depend on carry propogation later) */
375 /* 3. If leading byte is not 0xff check for any carry propogation */
376 /* 4. Insert the carry (propogated in previous byte) along with */
377 /* outstanding bytes (if any) and leading byte */
378 /************************************************************************/
379 if (lead_byte == 0xff)
380 {
381 /* actual bits depend on carry propogration */
382 ps_cab_enc_env->u4_out_standing_bytes++;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530383 }
384 else
385 {
386 /* carry = 1 => putbit(1); carry propogated due to L renorm */
387 WORD32 carry = (lead_byte >> 8) & 0x1;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530388 UWORD32 u4_out_standing_bytes = ps_cab_enc_env->u4_out_standing_bytes;
389
390
391 /*********************************************************************/
392 /* Insert the carry propogated in previous byte */
393 /* */
394 /* Note : Do not worry about corruption into slice header align byte */
395 /* This is because the first bin cannot result in overflow */
396 /*********************************************************************/
397 if (carry)
398 {
399 /* CORNER CASE: if the previous data is 0x000003, then EPB will be inserted
400 and the data will become 0x00000303 and if the carry is present, it will
401 be added with the last byte and it will become 0x00000304 which is not correct
402 as per standard */
403 /* so check for previous four bytes and if it is equal to 0x00000303
404 then subtract u4_strm_buf_offset by 1 */
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530405 if (pu1_strm_buf[ps_stream->u4_strm_buf_offset - 1] == 0x03
406 && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 2] == 0x03
407 && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 3] == 0x00
408 && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 4] == 0x00)
Harinarayanan K K134291e2015-06-18 16:03:38 +0530409 {
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530410 ps_stream->u4_strm_buf_offset -= 1;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530411 }
412 /* previous byte carry add will not result in overflow to */
413 /* u4_strm_buf_offset - 2 as we track 0xff as outstanding bytes */
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530414 pu1_strm_buf[ps_stream->u4_strm_buf_offset - 1] += carry;
415 ps_stream->i4_zero_bytes_run = 0;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530416 }
417
418 /* Insert outstanding bytes (if any) */
419 while (u4_out_standing_bytes)
420 {
421 UWORD8 u1_0_or_ff = carry ? 0 : 0xFF;
422
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530423 status |= ih264e_put_byte_epb(ps_stream, u1_0_or_ff);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530424
425 u4_out_standing_bytes--;
426 }
427 ps_cab_enc_env->u4_out_standing_bytes = 0;
428
429 /* Insert the leading byte */
430 lead_byte &= 0xFF;
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530431 status |= ih264e_put_byte_epb(ps_stream, lead_byte);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530432 }
Neelkamal Semwalb59de5a2021-03-10 10:03:39 +0530433 return status;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530434}
435
436
437
438
439 /**
440 ******************************************************************************
441 *
442 * @brief Codes a bin based on probablilty and mps packed context model
443 *
444 * @par Description
445 * 1. Apart from encoding bin, context model is updated as per state transition
446 * 2. Range and Low renormalization is done based on bin and original state
447 * 3. After renorm bistream is updated (if required)
448 *
449 * @param[in] ps_cabac
450 * pointer to cabac context (handle)
451 *
452 * @param[in] bin
453 * bin(boolean) to be encoded
454 *
455 * @param[in] pu1_bin_ctxts
456 * index of cabac context model containing pState[bits 5-0] | MPS[bit6]
457 *
458 * @return
459 *
460 ******************************************************************************
461 */
462void ih264e_cabac_encode_bin(cabac_ctxt_t *ps_cabac, WORD32 bin,
463 bin_ctxt_model *pu1_bin_ctxts)
464{
465
466 encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
467 UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
468 UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
469 UWORD32 u4_rlps;
470 UWORD8 state_mps = (*pu1_bin_ctxts) & 0x3F;
471 UWORD8 u1_mps = !!((*pu1_bin_ctxts) & (0x40));
472 WORD32 shift;
473 UWORD32 u4_table_val;
474 /* Sanity checks */
475 ASSERT((bin == 0) || (bin == 1));
476 ASSERT((u4_range >= 256) && (u4_range < 512));
477
478 /* Get the lps range from LUT based on quantized range and state */
479 u4_table_val= gau4_ih264_cabac_table[state_mps][(u4_range >> 6) & 0x3];
480 u4_rlps = u4_table_val & 0xFF;
481 u4_range -= u4_rlps;
482
483 /* check if bin is mps or lps */
484 if (u1_mps ^ bin)
485 {
486 /* lps path; L= L + R; R = RLPS */
487 u4_low += u4_range;
488 u4_range = u4_rlps;
489 if (state_mps == 0)
490 {
491 /* MPS(CtxIdx) = 1 - MPS(CtxIdx) */
492 u1_mps = 1 - u1_mps;
493 } /* update the context model from state transition LUT */
494
495 state_mps = (u4_table_val >> 15) & 0x3F;
496 }
497 else
498 { /* update the context model from state transition LUT */
499 state_mps = (u4_table_val >> 8) & 0x3F;
500 }
501
502 (*pu1_bin_ctxts) = (u1_mps << 6) | state_mps;
503
504 /*****************************************************************/
505 /* Renormalization; calculate bits generated based on range(R) */
506 /* Note : 6 <= R < 512; R is 2 only for terminating encode */
507 /*****************************************************************/
508 GETRANGE(shift, u4_range);
509 shift = 9 - shift;
510 u4_low <<= shift;
511 u4_range <<= shift;
512
513 /* bits to be inserted in the bitstream */
514 ps_cab_enc_env->u4_bits_gen += shift;
515 ps_cab_enc_env->u4_code_int_range = u4_range;
516 ps_cab_enc_env->u4_code_int_low = u4_low;
517
518 /* generate stream when a byte is ready */
519 if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
520 {
521 ih264e_cabac_put_byte(ps_cabac);
522 }
523
524}
525
526
527
528
529 /**
530 *******************************************************************************
531 *
532 * @brief
533 * Encoding process for a binary decision :implements encoding process of a decision
534 * as defined in 9.3.4.2 . This function encodes multiple bins, of a symbol. Implements
535 * flowchart Figure 9-7( ITU_T_H264-201402)
536 *
537 * @param[in] u4_bins
538 * array of bin values
539 *
540 * @param[in] i1_bins_len
541 * Length of bins, maximum 32
542 *
543 * @param[in] u4_ctx_inc
544 * CtxInc, byte0- bin0, byte1-bin1 ..
545 *
546 * @param[in] i1_valid_len
547 * valid length of bins, after that CtxInc is constant
548 *
549 * @param[in] pu1_bin_ctxt_type
550 * Pointer to binary contexts
551
552 * @param[in] ps_cabac
553 * Pointer to cabac_context_structure
554 *
555 * @returns
556 *
557 * @remarks
558 * None
559 *
560 *******************************************************************************
561 */
562void ih264e_encode_decision_bins(UWORD32 u4_bins, WORD8 i1_bins_len,
563 UWORD32 u4_ctx_inc, WORD8 i1_valid_len,
564 bin_ctxt_model *pu1_bin_ctxt_type,
565 cabac_ctxt_t *ps_cabac)
566{
567 WORD8 i;
568 UWORD8 u1_ctx_inc, u1_bin;
569
570 for (i = 0; i < i1_bins_len; i++)
571 {
572 u1_bin = (u4_bins & 0x01);
573 u4_bins = u4_bins >> 1;
574 u1_ctx_inc = u4_ctx_inc & 0x0f;
575 if (i < i1_valid_len)
576 u4_ctx_inc = u4_ctx_inc >> 4;
577 /* Encode the bin */
578 ih264e_cabac_encode_bin(ps_cabac, u1_bin,
579 pu1_bin_ctxt_type + u1_ctx_inc);
580 }
581
582}
583
584
585
586
587
588
589/**
590 *******************************************************************************
591 * @brief
592 * Encoding process for a binary decision before termination:Encoding process
593 * of a termination(9.3.4.5 :ITU_T_H264-201402) . Explained in flowchart 9-11.
594 *
595 * @param[in] ps_cabac
596 * Pointer to cabac structure
597 *
598 * @param[in] term_bin
599 * Symbol value, end of slice or not, term_bin is binary
600 *
601 * @returns
602 *
603 * @remarks
604 * None
605 *
606 *******************************************************************************
607 */
608void ih264e_cabac_encode_terminate(cabac_ctxt_t *ps_cabac, WORD32 term_bin)
609{
610
611 encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
612
613 UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
614 UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
615 UWORD32 u4_rlps;
616 WORD32 shift;
617
618 /* Sanity checks */
619 ASSERT((u4_range >= 256) && (u4_range < 512));
620 ASSERT((term_bin == 0) || (term_bin == 1));
621
622 /* term_bin = 1 has lps range = 2 */
623 u4_rlps = 2;
624 u4_range -= u4_rlps;
625
626 /* if terminate L is incremented by curR and R=2 */
627 if (term_bin)
628 {
629 /* lps path; L= L + R; R = RLPS */
630 u4_low += u4_range;
631 u4_range = u4_rlps;
632 }
633
634 /*****************************************************************/
635 /* Renormalization; calculate bits generated based on range(R) */
636 /* Note : 6 <= R < 512; R is 2 only for terminating encode */
637 /*****************************************************************/
638 GETRANGE(shift, u4_range);
639 shift = 9 - shift;
640 u4_low <<= shift;
641 u4_range <<= shift;
642
643 /* bits to be inserted in the bitstream */
644 ps_cab_enc_env->u4_bits_gen += shift;
645 ps_cab_enc_env->u4_code_int_range = u4_range;
646 ps_cab_enc_env->u4_code_int_low = u4_low;
647
648 /* generate stream when a byte is ready */
649 if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
650 {
651 ih264e_cabac_put_byte(ps_cabac);
652 }
653
654 if (term_bin)
655 {
656 ih264e_cabac_flush(ps_cabac);
657 }
658
659}
660
661
662/**
663 *******************************************************************************
664 * @brief
665 * Bypass encoding process for binary decisions: Explained (9.3.4.4 :ITU_T_H264-201402)
666 * , flowchart 9-10.
667 *
668 * @param[ino] ps_cabac : pointer to cabac context (handle)
669 *
670 * @param[in] bin : bypass bin(0/1) to be encoded
671 *
672 * @returns
673 *
674 * @remarks
675 * None
676 *
677 *******************************************************************************
678 */
679
680void ih264e_cabac_encode_bypass_bin(cabac_ctxt_t *ps_cabac, WORD32 bin)
681{
682
683 encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
684
685 UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
686 UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
687
688 /* Sanity checks */
689 ASSERT((u4_range >= 256) && (u4_range < 512));
690 ASSERT((bin == 0) || (bin == 1));
691
692 u4_low <<= 1;
693 /* add range if bin is 1 */
694 if (bin)
695 {
696 u4_low += u4_range;
697 }
698
699 /* 1 bit to be inserted in the bitstream */
700 ps_cab_enc_env->u4_bits_gen++;
701 ps_cab_enc_env->u4_code_int_low = u4_low;
702
703 /* generate stream when a byte is ready */
704 if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
705 {
706 ih264e_cabac_put_byte(ps_cabac);
707 }
708
709}
710
711
712 /**
713 ******************************************************************************
714 *
715 * @brief Encodes a series of bypass bins (FLC bypass bins)
716 *
717 * @par Description
718 * This function is more optimal than calling ih264e_cabac_encode_bypass_bin()
719 * in a loop as cabac low, renorm and generating the stream (8bins at a time)
720 * can be done in one operation
721 *
722 * @param[inout]ps_cabac
723 * pointer to cabac context (handle)
724 *
725 * @param[in] u4_bins
726 * syntax element to be coded (as FLC bins)
727 *
728 * @param[in] num_bins
729 * This is the FLC length for u4_sym
730 *
731 * @return
732 *
733 ******************************************************************************
734 */
735
736void ih264e_cabac_encode_bypass_bins(cabac_ctxt_t *ps_cabac, UWORD32 u4_bins,
737 WORD32 num_bins)
738{
739
740 encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
741
742 UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
743 WORD32 next_byte;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530744
745 /* Sanity checks */
746 ASSERT((num_bins < 33) && (num_bins > 0));
747 ASSERT((u4_range >= 256) && (u4_range < 512));
748
749 /* Compute bit always to populate the trace */
750 /* increment bits generated by num_bins */
751
752 /* Encode 8bins at a time and put in the bit-stream */
753 while (num_bins > 8)
754 {
755 num_bins -= 8;
756
Doney Alex9c6a2f72015-06-30 17:56:42 +0530757 next_byte = (u4_bins >> (num_bins)) & 0xff;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530758
759 /* L = (L << 8) + (R * next_byte) */
760 ps_cab_enc_env->u4_code_int_low <<= 8;
Doney Alex9c6a2f72015-06-30 17:56:42 +0530761 ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530762 ps_cab_enc_env->u4_bits_gen += 8;
763
764 if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
765 {
766 /* insert the leading byte of low into stream */
767 ih264e_cabac_put_byte(ps_cabac);
768 }
769 }
770
771 /* Update low with remaining bins and return */
772 next_byte = (u4_bins & ((1 << num_bins) - 1));
773
Harinarayanan K K134291e2015-06-18 16:03:38 +0530774 ps_cab_enc_env->u4_code_int_low <<= num_bins;
Doney Alex9c6a2f72015-06-30 17:56:42 +0530775 ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530776 ps_cab_enc_env->u4_bits_gen += num_bins;
777
778 if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
779 {
780 /* insert the leading byte of low into stream */
781 ih264e_cabac_put_byte(ps_cabac);
782 }
783
784}