Coverage Report

Created: 2026-11-12 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.20M
{
116
8.20M
    WORD32 unary_length;
117
8.20M
    UWORD32 u4_sufs_shiftk_plus1, u4_egk, u4_unary_bins;
118
119
8.20M
    u4_sufs_shiftk_plus1 = i2_sufs + 1;
120
121
8.20M
    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.20M
    u4_unary_bins = (1 << unary_length) - 2;
125
126
    /* insert the symbol prefix of (unary length - 1)  bins */
127
8.20M
    u4_egk = (u4_unary_bins << (unary_length - 1)) |
128
8.20M
             (u4_sufs_shiftk_plus1 & ((1 << (unary_length - 1)) - 1));
129
130
    /* length of the code = 2 *(unary_length - 1) + 1 + k */
131
8.20M
    *pi1_bins_len = (2 * unary_length) - 1;
132
133
8.20M
    return (u4_egk);
134
8.20M
}
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.99M
{
158
    /* CABAC context */
159
4.99M
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
160
4.99M
    isvce_mb_info_ctxt_t *ps_ctx_inc_mb_map;
161
4.99M
    cab_csbp_t *ps_lft_csbp;
162
163
4.99M
    WORD32 i4_lft_avail, i4_top_avail, i4_is_intra;
164
4.99M
    WORD32 i4_mb_x, i4_mb_y;
165
4.99M
    UWORD8 *pu1_slice_idx = ps_ent_ctxt->pu1_slice_idx;
166
167
4.99M
    i4_is_intra = ((u4_mb_type == I16x16) || (u4_mb_type == I8x8) || (u4_mb_type == I4x4));
168
169
    /* derive neighbor availability */
170
4.99M
    i4_mb_x = ps_ent_ctxt->i4_mb_x;
171
4.99M
    i4_mb_y = ps_ent_ctxt->i4_mb_y;
172
4.99M
    pu1_slice_idx += (i4_mb_y * ps_ent_ctxt->i4_wd_mbs);
173
    /* left macroblock availability */
174
4.99M
    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.99M
    i4_top_avail = (i4_mb_y == 0 ||
177
4.71M
                    (pu1_slice_idx[i4_mb_x - ps_ent_ctxt->i4_wd_mbs] != pu1_slice_idx[i4_mb_x]))
178
4.99M
                       ? 0
179
4.99M
                       : 1;
180
4.99M
    i4_mb_x = ps_ent_ctxt->i4_mb_x;
181
4.99M
    ps_ctx_inc_mb_map = ps_cabac_ctxt->ps_mb_map_ctxt_inc;
182
4.99M
    ps_cabac_ctxt->ps_curr_ctxt_mb_info = ps_ctx_inc_mb_map + i4_mb_x;
183
4.99M
    ps_cabac_ctxt->ps_left_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info;
184
4.99M
    ps_cabac_ctxt->ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info;
185
4.99M
    ps_lft_csbp = ps_cabac_ctxt->ps_lft_csbp;
186
4.99M
    ps_cabac_ctxt->pu1_left_y_ac_csbp = &ps_lft_csbp->u1_y_ac_csbp_top_mb;
187
4.99M
    ps_cabac_ctxt->pu1_left_uv_ac_csbp = &ps_lft_csbp->u1_uv_ac_csbp_top_mb;
188
4.99M
    ps_cabac_ctxt->pu1_left_yuv_dc_csbp = &ps_lft_csbp->u1_yuv_dc_csbp_top_mb;
189
4.99M
    ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc = &ps_cabac_ctxt->i1_left_ref_idx_ctx_inc_arr[0][0];
190
4.99M
    ps_cabac_ctxt->pu1_left_mv_ctxt_inc = ps_cabac_ctxt->u1_left_mv_ctxt_inc_arr[0];
191
192
4.99M
    if(i4_lft_avail) ps_cabac_ctxt->ps_left_ctxt_mb_info = ps_cabac_ctxt->ps_curr_ctxt_mb_info - 1;
193
4.99M
    if(i4_top_avail) ps_cabac_ctxt->ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
194
195
4.99M
    if(!i4_lft_avail)
196
101k
    {
197
101k
        UWORD8 u1_def_csbp = i4_is_intra ? 0xf : 0;
198
101k
        *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = u1_def_csbp;
199
101k
        *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = u1_def_csbp;
200
101k
        *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = u1_def_csbp;
201
101k
        *((UWORD32 *) ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc) = 0;
202
101k
        memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16);
203
101k
    }
204
4.99M
    if(!i4_top_avail)
205
280k
    {
206
280k
        UWORD8 u1_def_csbp = i4_is_intra ? 0xff : 0;
207
280k
        ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_ac_csbp = u1_def_csbp;
208
280k
        ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_dc_csbp = u1_def_csbp;
209
280k
        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[0] =
210
280k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[1] =
211
280k
                ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[2] =
212
280k
                    ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[3] = 0;
213
280k
        memset(ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv, 0, 16);
214
280k
    }
215
4.99M
}
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
11.1k
{
234
    /* bit stream ptr */
235
11.1k
    bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm;
236
11.1k
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
237
11.1k
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
238
11.1k
    UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen;
239
11.1k
    UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer;
240
11.1k
    UWORD32 u4_strm_buf_offset = ps_stream->u4_strm_buf_offset;
241
11.1k
    WORD32 zero_run = ps_stream->i4_zero_bytes_run;
242
11.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
11.1k
    {
249
        /* carry = 1 => putbit(1); carry propogated due to L renorm */
250
11.1k
        WORD32 carry = (u4_low >> (u4_bits_gen + CABAC_BITS)) & 0x1;
251
11.1k
        WORD32 last_byte;
252
11.1k
        WORD32 bits_left;
253
11.1k
        WORD32 rem_bits;
254
255
        /* carry exists only if pu1_strm_buf has at least 1 byte of data */
256
11.1k
        carry = carry && (u4_strm_buf_offset > 0);
257
258
11.1k
        if(carry)
259
439
        {
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
439
            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
439
            pu1_strm_buf[u4_strm_buf_offset - 1] += carry;
276
439
            zero_run = 0;
277
439
        }
278
279
        /*        Insert outstanding bytes (if any)         */
280
13.8k
        while(u4_out_standing_bytes)
281
2.69k
        {
282
2.69k
            UWORD8 u1_0_or_ff = carry ? 0 : 0xFF;
283
284
2.69k
            PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, u1_0_or_ff, zero_run);
285
2.69k
            u4_out_standing_bytes--;
286
2.69k
        }
287
288
        /*  clear the carry in low */
289
11.1k
        if(carry)
290
439
        {
291
439
            u4_low &= ((1 << (u4_bits_gen + CABAC_BITS)) - 1);
292
439
        }
293
294
        /* extract the remaining bits;                                   */
295
        /* includes additional msb bit of low as per Figure 9-12      */
296
11.1k
        bits_left = u4_bits_gen + 1;
297
11.1k
        rem_bits = (u4_low >> (u4_bits_gen + CABAC_BITS - bits_left));
298
299
11.1k
        if(bits_left >= 8)
300
5.13k
        {
301
5.13k
            last_byte = (rem_bits >> (bits_left - 8)) & 0xFF;
302
5.13k
            PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, last_byte, zero_run);
303
5.13k
            bits_left -= 8;
304
5.13k
        }
305
306
        /* insert last byte along with rbsp stop bit(1) and 0's in the end */
307
11.1k
        last_byte =
308
11.1k
            (rem_bits << (8 - bits_left)) | (1 << (7 - bits_left) | (1 << (7 - bits_left - 1)));
309
11.1k
        last_byte &= 0xFF;
310
11.1k
        PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, last_byte, zero_run);
311
312
        /* update the state variables and return success */
313
11.1k
        ps_stream->u4_strm_buf_offset = u4_strm_buf_offset;
314
11.1k
        ps_stream->i4_zero_bytes_run = 0;
315
        /* Default init values for scratch variables of bitstream context */
316
11.1k
        ps_stream->u4_cur_word = 0;
317
11.1k
        ps_stream->i4_bits_left_in_cw = WORD_SIZE;
318
11.1k
    }
319
11.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
28.8M
{
345
    /* bit stream ptr */
346
28.8M
    bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm;
347
28.8M
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
348
28.8M
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
349
28.8M
    UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen;
350
28.8M
    WORD32 lead_byte = u4_low >> (u4_bits_gen + CABAC_BITS - 8);
351
352
    /* Sanity checks */
353
28.8M
    ASSERT((ps_cab_enc_env->u4_code_int_range >= 256) && (ps_cab_enc_env->u4_code_int_range < 512));
354
28.8M
    ASSERT((u4_bits_gen >= 8));
355
356
    /* update bits generated and low after extracting leading byte */
357
28.8M
    u4_bits_gen -= 8;
358
28.8M
    ps_cab_enc_env->u4_code_int_low &= ((1 << (CABAC_BITS + u4_bits_gen)) - 1);
359
28.8M
    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
28.8M
    if(lead_byte == 0xff)
370
145k
    {
371
        /* actual bits depend on carry propogration     */
372
145k
        ps_cab_enc_env->u4_out_standing_bytes++;
373
145k
        return;
374
145k
    }
375
28.6M
    else
376
28.6M
    {
377
28.6M
        UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer;
378
28.6M
        UWORD32 u4_strm_buf_offset = ps_stream->u4_strm_buf_offset;
379
        /* carry = 1 => putbit(1); carry propogated due to L renorm */
380
28.6M
        WORD32 carry = (lead_byte >> 8) & 0x1;
381
28.6M
        WORD32 zero_run = ps_stream->i4_zero_bytes_run;
382
28.6M
        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
28.6M
        if(carry)
391
1.68M
        {
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.68M
            if((u4_strm_buf_offset > 3) && (pu1_strm_buf[u4_strm_buf_offset - 1] == 0x03) &&
399
7.94k
               (pu1_strm_buf[u4_strm_buf_offset - 2] == 0x03) &&
400
1.02k
               (pu1_strm_buf[u4_strm_buf_offset - 3] == 0x00) &&
401
117
               (pu1_strm_buf[u4_strm_buf_offset - 4] == 0x00))
402
77
            {
403
77
                u4_strm_buf_offset -= 1;
404
77
            }
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.68M
            if(u4_strm_buf_offset > 0)
409
1.68M
            {
410
1.68M
                pu1_strm_buf[u4_strm_buf_offset - 1] += carry;
411
1.68M
                zero_run = 0;
412
1.68M
            }
413
1.68M
        }
414
415
        /*        Insert outstanding bytes (if any)         */
416
28.8M
        while(u4_out_standing_bytes)
417
142k
        {
418
142k
            UWORD8 u1_0_or_ff = carry ? 0 : 0xFF;
419
420
142k
            PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, u1_0_or_ff, zero_run);
421
422
142k
            u4_out_standing_bytes--;
423
142k
        }
424
28.6M
        ps_cab_enc_env->u4_out_standing_bytes = 0;
425
426
        /*        Insert the leading byte                   */
427
28.6M
        lead_byte &= 0xFF;
428
28.6M
        PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, lead_byte, zero_run);
429
430
        /* update the state variables and return success */
431
28.6M
        ps_stream->u4_strm_buf_offset = u4_strm_buf_offset;
432
28.6M
        ps_stream->i4_zero_bytes_run = zero_run;
433
28.6M
    }
434
28.8M
}
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
312M
{
461
312M
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
462
312M
    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
463
312M
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
464
312M
    UWORD32 u4_rlps;
465
312M
    UWORD8 state_mps = (*pu1_bin_ctxts) & 0x3F;
466
312M
    UWORD8 u1_mps = !!((*pu1_bin_ctxts) & (0x40));
467
312M
    WORD32 shift;
468
312M
    UWORD32 u4_table_val;
469
    /* Sanity checks */
470
312M
    ASSERT((bin == 0) || (bin == 1));
471
312M
    ASSERT((u4_range >= 256) && (u4_range < 512));
472
473
    /* Get the lps range from LUT based on quantized range and state */
474
312M
    u4_table_val = gau4_isvc_cabac_table[state_mps][(u4_range >> 6) & 0x3];
475
312M
    u4_rlps = u4_table_val & 0xFF;
476
312M
    u4_range -= u4_rlps;
477
478
    /* check if bin is mps or lps */
479
312M
    if(u1_mps ^ bin)
480
30.7M
    {
481
        /* lps path;  L= L + R; R = RLPS */
482
30.7M
        u4_low += u4_range;
483
30.7M
        u4_range = u4_rlps;
484
30.7M
        if(state_mps == 0)
485
1.38M
        {
486
            /* MPS(CtxIdx) = 1 - MPS(CtxIdx) */
487
1.38M
            u1_mps = 1 - u1_mps;
488
1.38M
        } /* update the context model from state transition LUT */
489
490
30.7M
        state_mps = (u4_table_val >> 15) & 0x3F;
491
30.7M
    }
492
281M
    else
493
281M
    { /* update the context model from state transition LUT */
494
281M
        state_mps = (u4_table_val >> 8) & 0x3F;
495
281M
    }
496
497
312M
    (*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
312M
    GETRANGE(shift, u4_range);
504
312M
    shift = 9 - shift;
505
312M
    u4_low <<= shift;
506
312M
    u4_range <<= shift;
507
508
    /* bits to be inserted in the bitstream */
509
312M
    ps_cab_enc_env->u4_bits_gen += shift;
510
312M
    ps_cab_enc_env->u4_code_int_range = u4_range;
511
312M
    ps_cab_enc_env->u4_code_int_low = u4_low;
512
513
    /* generate stream when a byte is ready */
514
312M
    if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
515
13.8M
    {
516
13.8M
        isvce_cabac_put_byte(ps_cabac);
517
13.8M
    }
518
312M
}
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
39.2M
{
559
39.2M
    WORD8 i;
560
39.2M
    UWORD8 u1_ctx_inc, u1_bin;
561
562
260M
    for(i = 0; i < i1_bins_len; i++)
563
220M
    {
564
220M
        u1_bin = (u4_bins & 0x01);
565
220M
        u4_bins = u4_bins >> 1;
566
220M
        u1_ctx_inc = u4_ctx_inc & 0x0f;
567
220M
        if(i < i1_valid_len) u4_ctx_inc = u4_ctx_inc >> 4;
568
        /* Encode the bin */
569
220M
        isvce_cabac_encode_bin(ps_cabac, u1_bin, pu1_bin_ctxt_type + u1_ctx_inc);
570
220M
    }
571
39.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.99M
{
594
4.99M
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
595
596
4.99M
    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
597
4.99M
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
598
4.99M
    UWORD32 u4_rlps;
599
4.99M
    WORD32 shift;
600
601
    /* Sanity checks */
602
4.99M
    ASSERT((u4_range >= 256) && (u4_range < 512));
603
4.99M
    ASSERT((term_bin == 0) || (term_bin == 1));
604
605
    /*  term_bin = 1 has lps range = 2 */
606
4.99M
    u4_rlps = 2;
607
4.99M
    u4_range -= u4_rlps;
608
609
    /* if terminate L is incremented by curR and R=2 */
610
4.99M
    if(term_bin)
611
11.1k
    {
612
        /* lps path;  L= L + R; R = RLPS */
613
11.1k
        u4_low += u4_range;
614
11.1k
        u4_range = u4_rlps;
615
11.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.99M
    GETRANGE(shift, u4_range);
622
4.99M
    shift = 9 - shift;
623
4.99M
    u4_low <<= shift;
624
4.99M
    u4_range <<= shift;
625
626
    /* bits to be inserted in the bitstream */
627
4.99M
    ps_cab_enc_env->u4_bits_gen += shift;
628
4.99M
    ps_cab_enc_env->u4_code_int_range = u4_range;
629
4.99M
    ps_cab_enc_env->u4_code_int_low = u4_low;
630
631
    /* generate stream when a byte is ready */
632
4.99M
    if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
633
20.7k
    {
634
20.7k
        isvce_cabac_put_byte(ps_cabac);
635
20.7k
    }
636
637
4.99M
    if(term_bin)
638
11.1k
    {
639
11.1k
        isvce_cabac_flush(ps_cabac);
640
11.1k
    }
641
4.99M
}
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
30.7M
{
663
30.7M
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
664
665
30.7M
    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
666
30.7M
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
667
668
    /* Sanity checks */
669
30.7M
    ASSERT((u4_range >= 256) && (u4_range < 512));
670
30.7M
    ASSERT((bin == 0) || (bin == 1));
671
672
30.7M
    u4_low <<= 1;
673
    /* add range if bin is 1 */
674
30.7M
    if(bin)
675
16.5M
    {
676
16.5M
        u4_low += u4_range;
677
16.5M
    }
678
679
    /* 1 bit to be inserted in the bitstream */
680
30.7M
    ps_cab_enc_env->u4_bits_gen++;
681
30.7M
    ps_cab_enc_env->u4_code_int_low = u4_low;
682
683
    /* generate stream when a byte is ready */
684
30.7M
    if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
685
3.84M
    {
686
3.84M
        isvce_cabac_put_byte(ps_cabac);
687
3.84M
    }
688
30.7M
}
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.25M
{
716
8.25M
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
717
718
8.25M
    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
719
8.25M
    WORD32 next_byte;
720
721
    /* Sanity checks */
722
8.25M
    ASSERT((num_bins < 33) && (num_bins > 0));
723
8.25M
    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
14.9M
    while(num_bins > 8)
730
6.70M
    {
731
6.70M
        num_bins -= 8;
732
733
6.70M
        next_byte = (u4_bins >> (num_bins)) & 0xff;
734
735
        /*  L = (L << 8) +  (R * next_byte) */
736
6.70M
        ps_cab_enc_env->u4_code_int_low <<= 8;
737
6.70M
        ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
738
6.70M
        ps_cab_enc_env->u4_bits_gen += 8;
739
740
6.70M
        if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
741
6.70M
        {
742
            /*  insert the leading byte of low into stream */
743
6.70M
            isvce_cabac_put_byte(ps_cabac);
744
6.70M
        }
745
6.70M
    }
746
747
    /* Update low with remaining bins and return */
748
8.25M
    next_byte = (u4_bins & ((1 << num_bins) - 1));
749
750
8.25M
    ps_cab_enc_env->u4_code_int_low <<= num_bins;
751
8.25M
    ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
752
8.25M
    ps_cab_enc_env->u4_bits_gen += num_bins;
753
754
8.25M
    if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
755
4.17M
    {
756
        /*  insert the leading byte of low into stream */
757
4.17M
        isvce_cabac_put_byte(ps_cabac);
758
4.17M
    }
759
8.25M
}