Coverage Report

Created: 2025-08-26 06:38

/src/libavc/encoder/svc/isvce_cabac_encode.c
Line
Count
Source (jump to first uncovered line)
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 functions to encode in CABAC entropy mode
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 "isvc_defs.h"
59
#include "isvce_defs.h"
60
#include "isvc_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 "isvc_structs.h"
68
#include "isvc_trans_quant_itrans_iquant.h"
69
#include "isvc_inter_pred_filters.h"
70
#include "isvc_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 "isvc_cabac_tables.h"
76
#include "irc_cntrl_param.h"
77
#include "irc_frame_info_collector.h"
78
#include "isvce_rate_control.h"
79
#include "isvce_cabac_structs.h"
80
#include "isvce_structs.h"
81
#include "isvce_cabac.h"
82
#include "isvce_encode_header.h"
83
#include "ih264_cavlc_tables.h"
84
#include "isvce_cavlc.h"
85
#include "ih264e_statistics.h"
86
#include "ih264e_trace.h"
87
#include "isvce_cabac_utils.h"
88
#include "isvce_utils.h"
89
90
/*****************************************************************************/
91
/* Function Definitions                                                      */
92
/*****************************************************************************/
93
94
/**
95
 *******************************************************************************
96
 *
97
 * @brief
98
 *  Encodes mb_skip_flag  using CABAC entropy coding mode.
99
 *
100
 * @param[in] u1_mb_skip_flag
101
 *  mb_skip_flag
102
 *
103
 * @param[in] ps_cabac_ctxt
104
 *  Pointer to cabac context structure
105
 *
106
 * @param[in] u4_ctxidx_offset
107
 *  ctxIdxOffset for mb_skip_flag context
108
 *
109
 * @returns
110
 *
111
 * @remarks
112
 *  None
113
 *
114
 *******************************************************************************
115
 */
116
static void isvce_cabac_enc_mb_skip(UWORD8 u1_mb_skip_flag, isvce_cabac_ctxt_t *ps_cabac_ctxt,
117
                                    UWORD32 u4_ctxidx_offset)
118
531k
{
119
531k
    UWORD8 u4_ctx_inc;
120
531k
    WORD8 a, b;
121
531k
    a = ((ps_cabac_ctxt->ps_left_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ? 0 : 1);
122
531k
    b = ((ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ? 0 : 1);
123
124
531k
    u4_ctx_inc = a + b;
125
    /* Encode the bin */
126
531k
    isvce_cabac_encode_bin(ps_cabac_ctxt, (UWORD32) u1_mb_skip_flag,
127
531k
                           ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctxidx_offset + u4_ctx_inc);
128
531k
}
129
130
/* ! < Table 9-36 – Binarization for macroblock types in I slices  in
131
 * ITU_T_H264-201402 Bits 0-7 : binarised value Bits 8-15: length of binary
132
 * sequence
133
 */
134
static const UWORD32 u4_mb_type_intra[26] = {0x0100, 0x0620, 0x0621, 0x0622, 0x0623, 0x0748, 0x0749,
135
                                             0x074a, 0x074b, 0x074c, 0x074d, 0x074e, 0x074f, 0x0628,
136
                                             0x0629, 0x062a, 0x062b, 0x0758, 0x0759, 0x075a, 0x075b,
137
                                             0x075c, 0x075d, 0x075e, 0x075f, 0x0203};
138
139
/* CtxInc for mb types */
140
static const UWORD32 u4_mb_ctxinc[2][26] = {
141
    /* Intra CtxInc's */
142
    {0x00,     0x03467,  0x03467,  0x03467,  0x03467,  0x034567, 0x034567, 0x034567, 0x034567,
143
     0x034567, 0x034567, 0x034567, 0x034567, 0x03467,  0x03467,  0x03467,  0x03467,  0x034567,
144
     0x034567, 0x034567, 0x034567, 0x034567, 0x034567, 0x034567, 0x034567, 0x00},
145
    /* Inter CtxInc's */
146
    {0x00,      0x001233,  0x001233,  0x001233,  0x001233,  0x0012233, 0x0012233,
147
     0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x001233,
148
     0x001233,  0x001233,  0x001233,  0x0012233, 0x0012233, 0x0012233, 0x0012233,
149
     0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x00}};
150
151
/**
152
 *******************************************************************************
153
 *
154
 * @brief
155
 *  Encodes mb_type for an intra MB.
156
 *
157
 * @param[in] u4_slice_type
158
 *  slice type
159
 *
160
 * @param[in] u4_intra_mb_type
161
 *  MB type (Table 7-11)
162
 *
163
 * @param[in] ps_cabac_ctxt
164
 *  Pointer to cabac context structure
165
 *
166
 ** @param[in] u4_ctxidx_offset
167
 *  ctxIdxOffset for mb_type context
168
 *
169
 * @returns
170
 *
171
 * @remarks
172
 *  None
173
 *
174
 *******************************************************************************
175
 */
176
177
static void isvce_cabac_enc_intra_mb_type(UWORD32 u4_slice_type, UWORD32 u4_intra_mb_type,
178
                                          isvce_cabac_ctxt_t *ps_cabac_ctxt,
179
                                          UWORD32 u4_ctx_idx_offset)
180
2.27M
{
181
2.27M
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
182
2.27M
    bin_ctxt_model *pu1_mb_bin_ctxt, *pu1_bin_ctxt;
183
2.27M
    UWORD8 u1_bin;
184
2.27M
    isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
185
2.27M
    isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
186
2.27M
    UWORD32 u4_bins;
187
2.27M
    UWORD32 u4_ctx_inc;
188
2.27M
    WORD8 i1_bins_len;
189
2.27M
    UWORD32 u4_code_int_range;
190
2.27M
    UWORD32 u4_code_int_low;
191
2.27M
    UWORD16 u2_quant_code_int_range;
192
2.27M
    UWORD16 u4_code_int_range_lps;
193
2.27M
    WORD8 i;
194
2.27M
    UWORD8 u1_ctx_inc;
195
2.27M
    UWORD32 u4_table_val;
196
197
2.27M
    pu1_mb_bin_ctxt = ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset;
198
199
2.27M
    u4_bins = u4_mb_type_intra[u4_intra_mb_type];
200
2.27M
    i1_bins_len = (WORD8) ((u4_bins >> 8) & 0x0f);
201
2.27M
    u4_ctx_inc = u4_mb_ctxinc[(u4_slice_type != ISLICE)][u4_intra_mb_type];
202
2.27M
    u1_ctx_inc = 0;
203
2.27M
    if(u4_slice_type == ISLICE)
204
2.14M
    {
205
2.14M
        if(ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
206
2.10M
            u1_ctx_inc += ((ps_left_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0);
207
2.14M
        if(ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
208
2.08M
            u1_ctx_inc += ((ps_top_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0);
209
210
2.14M
        u4_ctx_inc = (u4_ctx_inc | (u1_ctx_inc << ((i1_bins_len - 1) << 2)));
211
2.14M
    }
212
135k
    else
213
135k
    {
214
135k
        pu1_mb_bin_ctxt += 3;
215
135k
        if(u4_slice_type == BSLICE) pu1_mb_bin_ctxt += 2;
216
135k
    }
217
218
2.27M
    u4_code_int_range = ps_cab_enc_env->u4_code_int_range;
219
2.27M
    u4_code_int_low = ps_cab_enc_env->u4_code_int_low;
220
221
15.4M
    for(i = (i1_bins_len - 1); i >= 0; i--)
222
13.2M
    {
223
13.2M
        WORD32 shift;
224
225
13.2M
        u1_ctx_inc = ((u4_ctx_inc >> (i << 2)) & 0x0f);
226
13.2M
        u1_bin = ((u4_bins >> i) & 0x01);
227
        /* Encode the bin */
228
13.2M
        pu1_bin_ctxt = pu1_mb_bin_ctxt + u1_ctx_inc;
229
13.2M
        if(i != (i1_bins_len - 2))
230
11.0M
        {
231
11.0M
            WORD8 i1_mps = !!((*pu1_bin_ctxt) & (0x40));
232
11.0M
            WORD8 i1_state = (*pu1_bin_ctxt) & 0x3F;
233
234
11.0M
            u2_quant_code_int_range = ((u4_code_int_range >> 6) & 0x03);
235
11.0M
            u4_table_val = gau4_isvc_cabac_table[i1_state][u2_quant_code_int_range];
236
11.0M
            u4_code_int_range_lps = u4_table_val & 0xFF;
237
238
11.0M
            u4_code_int_range -= u4_code_int_range_lps;
239
11.0M
            if(u1_bin != i1_mps)
240
398k
            {
241
398k
                u4_code_int_low += u4_code_int_range;
242
398k
                u4_code_int_range = u4_code_int_range_lps;
243
398k
                if(i1_state == 0)
244
22.8k
                {
245
                    /* MPS(CtxIdx) = 1 - MPS(CtxIdx) */
246
22.8k
                    i1_mps = 1 - i1_mps;
247
22.8k
                }
248
249
398k
                i1_state = (u4_table_val >> 15) & 0x3F;
250
398k
            }
251
10.6M
            else
252
10.6M
            {
253
10.6M
                i1_state = (u4_table_val >> 8) & 0x3F;
254
10.6M
            }
255
256
11.0M
            (*pu1_bin_ctxt) = (i1_mps << 6) | i1_state;
257
11.0M
        }
258
2.16M
        else
259
2.16M
        {
260
2.16M
            u4_code_int_range -= 2;
261
2.16M
        }
262
263
        /* Renormalize */
264
        /*****************************************************************/
265
        /* Renormalization; calculate bits generated based on range(R)   */
266
        /* Note : 6 <= R < 512; R is 2 only for terminating encode       */
267
        /*****************************************************************/
268
13.2M
        GETRANGE(shift, u4_code_int_range);
269
13.2M
        shift = 9 - shift;
270
13.2M
        u4_code_int_low <<= shift;
271
13.2M
        u4_code_int_range <<= shift;
272
273
        /* bits to be inserted in the bitstream */
274
13.2M
        ps_cab_enc_env->u4_bits_gen += shift;
275
13.2M
        ps_cab_enc_env->u4_code_int_range = u4_code_int_range;
276
13.2M
        ps_cab_enc_env->u4_code_int_low = u4_code_int_low;
277
278
        /* generate stream when a byte is ready */
279
13.2M
        if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
280
175k
        {
281
175k
            isvce_cabac_put_byte(ps_cabac_ctxt);
282
175k
            u4_code_int_range = ps_cab_enc_env->u4_code_int_range;
283
175k
            u4_code_int_low = ps_cab_enc_env->u4_code_int_low;
284
175k
        }
285
13.2M
    }
286
2.27M
}
287
288
/**
289
 *******************************************************************************
290
 *
291
 * @brief
292
 *  Encodes prev_intra4x4_pred_mode_flag and
293
 *  rem_intra4x4_pred_mode using CABAC entropy coding mode
294
 *
295
 * @param[in] ps_cabac_ctxt
296
 *  Pointer to cabac context structure
297
 *
298
 *  @param[in] pu1_intra_4x4_modes
299
 *  Pointer to array containing prev_intra4x4_pred_mode_flag and
300
 *  rem_intra4x4_pred_mode
301
 *
302
 * @returns
303
 *
304
 * @remarks
305
 *  None
306
 *
307
 *******************************************************************************
308
 */
309
static void isvce_cabac_enc_4x4mb_modes(isvce_cabac_ctxt_t *ps_cabac_ctxt,
310
                                        UWORD8 *pu1_intra_4x4_modes)
311
108k
{
312
108k
    WORD32 i;
313
108k
    WORD8 byte;
314
977k
    for(i = 0; i < 16; i += 2)
315
868k
    {
316
        /* sub blk idx 1 */
317
868k
        byte = pu1_intra_4x4_modes[i >> 1];
318
868k
        if(byte & 0x1)
319
655k
        {
320
655k
            isvce_cabac_encode_bin(
321
655k
                ps_cabac_ctxt, 1,
322
655k
                ps_cabac_ctxt->au1_cabac_ctxt_table + PREV_INTRA4X4_PRED_MODE_FLAG);
323
655k
        }
324
213k
        else
325
213k
        {
326
            /* Binarization is FL and Cmax=7 */
327
213k
            isvce_encode_decision_bins(
328
213k
                byte & 0xF, 4, 0x05554, 4,
329
213k
                ps_cabac_ctxt->au1_cabac_ctxt_table + REM_INTRA4X4_PRED_MODE - 5, ps_cabac_ctxt);
330
213k
        }
331
        /* sub blk idx 2 */
332
868k
        byte >>= 4;
333
868k
        if(byte & 0x1)
334
665k
        {
335
665k
            isvce_cabac_encode_bin(
336
665k
                ps_cabac_ctxt, 1,
337
665k
                ps_cabac_ctxt->au1_cabac_ctxt_table + PREV_INTRA4X4_PRED_MODE_FLAG);
338
665k
        }
339
203k
        else
340
203k
        {
341
203k
            isvce_encode_decision_bins(
342
203k
                byte & 0xF, 4, 0x05554, 4,
343
203k
                ps_cabac_ctxt->au1_cabac_ctxt_table + REM_INTRA4X4_PRED_MODE - 5, ps_cabac_ctxt);
344
203k
        }
345
868k
    }
346
108k
}
347
348
/**
349
 *******************************************************************************
350
 *
351
 * @brief
352
 *  Encodes chroma  intrapred mode for the MB.
353
 *
354
 * @param[in] u1_chroma_pred_mode
355
 *  Chroma intr prediction mode
356
 *
357
 * @param[in] ps_cabac_ctxt
358
 *  Pointer to cabac context structure
359
 *
360
 * @returns
361
 *
362
 * @remarks
363
 *  None
364
 *
365
 *******************************************************************************
366
 */
367
static void isvce_cabac_enc_chroma_predmode(UWORD8 u1_chroma_pred_mode,
368
                                            isvce_cabac_ctxt_t *ps_cabac_ctxt)
369
2.27M
{
370
2.27M
    WORD8 i1_temp;
371
2.27M
    isvce_mb_info_ctxt_t *ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
372
2.27M
    isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
373
2.27M
    isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
374
2.27M
    UWORD32 u4_bins = 0;
375
2.27M
    WORD8 i1_bins_len = 1;
376
2.27M
    UWORD32 u4_ctx_inc = 0;
377
2.27M
    UWORD8 a, b;
378
2.27M
    a = ((ps_left_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0);
379
2.27M
    b = ((ps_top_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0);
380
381
    /* Binarization is TU and Cmax=3 */
382
2.27M
    ps_curr_ctxt->u1_intrapred_chroma_mode = u1_chroma_pred_mode;
383
384
2.27M
    u4_ctx_inc = a + b;
385
2.27M
    u4_ctx_inc = (u4_ctx_inc | 0x330);
386
2.27M
    if(u1_chroma_pred_mode)
387
125k
    {
388
125k
        u4_bins = 1;
389
125k
        i1_temp = u1_chroma_pred_mode;
390
125k
        i1_temp--;
391
        /* Put a stream of 1's of length Chromaps_pred_mode_ctxt value */
392
214k
        while(i1_temp)
393
89.1k
        {
394
89.1k
            u4_bins = (u4_bins | (1 << i1_bins_len));
395
89.1k
            i1_bins_len++;
396
89.1k
            i1_temp--;
397
89.1k
        }
398
        /* If Chromaps_pred_mode_ctxt < Cmax i.e 3. Terminate put a zero */
399
125k
        if(u1_chroma_pred_mode < 3)
400
121k
        {
401
121k
            i1_bins_len++;
402
121k
        }
403
125k
    }
404
405
2.27M
    isvce_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 3,
406
2.27M
                               ps_cabac_ctxt->au1_cabac_ctxt_table + INTRA_CHROMA_PRED_MODE,
407
2.27M
                               ps_cabac_ctxt);
408
2.27M
}
409
410
/**
411
 *******************************************************************************
412
 *
413
 * @brief
414
 *  Encodes CBP for the MB.
415
 *
416
 * @param[in] u1_cbp
417
 *  CBP for the MB
418
 *
419
 * @param[in] ps_cabac_ctxt
420
 *  Pointer to cabac context structure
421
 *
422
 * @returns
423
 *
424
 * @remarks
425
 *  None
426
 *
427
 *******************************************************************************
428
 */
429
static void isvce_cabac_enc_cbp(UWORD32 u4_cbp, isvce_cabac_ctxt_t *ps_cabac_ctxt)
430
1.77M
{
431
1.77M
    isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
432
1.77M
    isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
433
1.77M
    WORD8 i2_cbp_chroma, i, j;
434
1.77M
    UWORD8 u1_ctxt_inc, u1_bin;
435
1.77M
    UWORD8 a, b;
436
1.77M
    UWORD32 u4_ctx_inc;
437
1.77M
    UWORD32 u4_bins;
438
1.77M
    WORD8 i1_bins_len;
439
440
    /* CBP Luma, FL, Cmax = 15, L = 4 */
441
1.77M
    u4_ctx_inc = 0;
442
1.77M
    u4_bins = 0;
443
1.77M
    i1_bins_len = 5;
444
8.88M
    for(i = 0; i < 4; i++)
445
7.11M
    {
446
        /* calulate ctxtInc, depending on neighbour availability */
447
        /* u1_ctxt_inc = CondTerm(A) + 2 * CondTerm(B);
448
         A: Left block and B: Top block */
449
450
        /* Check for Top availability */
451
7.11M
        if(i >> 1)
452
3.55M
        {
453
3.55M
            j = i - 2;
454
            /* Top is available always and it's current MB */
455
3.55M
            b = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1);
456
3.55M
        }
457
3.55M
        else
458
3.55M
        {
459
            /* for blocks whose top reference is in another MB */
460
3.55M
            {
461
3.55M
                j = i + 2;
462
3.55M
                b = ((ps_top_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1;
463
3.55M
            }
464
3.55M
        }
465
466
        /* Check for Left availability */
467
7.11M
        if(i & 0x01)
468
3.55M
        {
469
            /* Left is available always and it's current MB */
470
3.55M
            j = i - 1;
471
3.55M
            a = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1);
472
3.55M
        }
473
3.55M
        else
474
3.55M
        {
475
3.55M
            {
476
3.55M
                j = i + 1;
477
3.55M
                a = ((ps_left_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1;
478
3.55M
            }
479
3.55M
        }
480
7.11M
        u1_ctxt_inc = a + 2 * b;
481
7.11M
        u1_bin = ((u4_cbp >> i) & 0x01);
482
7.11M
        u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i << 2)));
483
7.11M
        u4_bins = (u4_bins | (u1_bin << i));
484
7.11M
    }
485
486
    /* CBP Chroma, TU, Cmax = 2 */
487
1.77M
    i2_cbp_chroma = u4_cbp >> 4;
488
    /* calulate ctxtInc, depending on neighbour availability */
489
1.77M
    a = (ps_left_ctxt->u1_cbp > 15) ? 1 : 0;
490
1.77M
    b = (ps_top_ctxt->u1_cbp > 15) ? 1 : 0;
491
492
1.77M
    u1_ctxt_inc = a + 2 * b;
493
1.77M
    if(i2_cbp_chroma)
494
157k
    {
495
157k
        u4_ctx_inc = u4_ctx_inc | ((4 + u1_ctxt_inc) << 16);
496
157k
        u4_bins = (u4_bins | 0x10);
497
        /* calulate ctxtInc, depending on neighbour availability */
498
157k
        a = (ps_left_ctxt->u1_cbp > 31) ? 1 : 0;
499
157k
        b = (ps_top_ctxt->u1_cbp > 31) ? 1 : 0;
500
157k
        u1_ctxt_inc = a + 2 * b;
501
157k
        u4_ctx_inc = u4_ctx_inc | ((8 + u1_ctxt_inc) << 20);
502
157k
        u4_bins = (u4_bins | (((i2_cbp_chroma >> 1) & 0x01) << i1_bins_len));
503
157k
        i1_bins_len++;
504
157k
    }
505
1.62M
    else
506
1.62M
    {
507
1.62M
        u4_ctx_inc = (u4_ctx_inc | ((4 + u1_ctxt_inc) << 16));
508
1.62M
    }
509
1.77M
    isvce_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 8,
510
1.77M
                               ps_cabac_ctxt->au1_cabac_ctxt_table + CBP_LUMA, ps_cabac_ctxt);
511
1.77M
}
512
513
/**
514
 *******************************************************************************
515
 *
516
 * @brief
517
 *  Encodes mb_qp_delta for the MB.
518
 *
519
 * @param[in] i1_mb_qp_delta
520
 *  mb_qp_delta
521
 *
522
 * @param[in] ps_cabac_ctxt
523
 *  Pointer to cabac context structure
524
 *
525
 * @returns
526
 *
527
 * @remarks
528
 *  None
529
 *
530
 *******************************************************************************
531
 */
532
static void isvce_cabac_enc_mb_qp_delta(WORD8 i1_mb_qp_delta, isvce_cabac_ctxt_t *ps_cabac_ctxt)
533
2.51M
{
534
2.51M
    UWORD8 u1_code_num;
535
2.51M
    UWORD8 u1_ctxt_inc;
536
537
2.51M
    UWORD32 u4_bins;
538
2.51M
    WORD8 i1_bins_len;
539
540
    /* Range of ps_mb_qp_delta_ctxt= -26 to +25 inclusive */
541
2.51M
    ASSERT((i1_mb_qp_delta < 26) && (i1_mb_qp_delta > -27));
542
543
    /* if ps_mb_qp_delta_ctxt=0, then codeNum=0 */
544
2.51M
    u1_code_num = 0;
545
2.51M
    if(i1_mb_qp_delta > 0)
546
23.8k
    {
547
23.8k
        u1_code_num = (i1_mb_qp_delta << 1) - 1;
548
23.8k
    }
549
2.48M
    else if(i1_mb_qp_delta < 0)
550
82.6k
    {
551
82.6k
        u1_code_num = (ABS(i1_mb_qp_delta)) << 1;
552
82.6k
    }
553
554
2.51M
    u4_bins = 0;
555
2.51M
    i1_bins_len = 1;
556
557
2.51M
    u1_ctxt_inc = !!ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt;
558
559
2.51M
    if(u1_code_num == 0)
560
2.40M
    {
561
2.40M
        isvce_encode_decision_bins(u4_bins, i1_bins_len, u1_ctxt_inc, 3,
562
2.40M
                                   ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
563
2.40M
                                   ps_cabac_ctxt);
564
2.40M
    }
565
106k
    else
566
106k
    {
567
106k
        u4_bins = 1;
568
106k
        u1_code_num--;
569
570
106k
        if(u1_code_num == 0)
571
11.6k
        {
572
11.6k
            i1_bins_len++;
573
574
11.6k
            isvce_encode_decision_bins(u4_bins, i1_bins_len, u1_ctxt_inc | 0x20, 3,
575
11.6k
                                       ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
576
11.6k
                                       ps_cabac_ctxt);
577
11.6k
        }
578
94.9k
        else
579
94.9k
        {
580
94.9k
            u4_bins = (u4_bins | (1 << i1_bins_len));
581
94.9k
            i1_bins_len++;
582
94.9k
            u1_code_num--;
583
584
            /* BinIdx from b2 onwards */
585
94.9k
            if(u1_code_num < 30)
586
93.1k
            {
587
                /* maximum i1_bins_len = 31 */
588
371k
                while(u1_code_num)
589
278k
                {
590
278k
                    u4_bins = (u4_bins | (1 << i1_bins_len));
591
278k
                    i1_bins_len++;
592
278k
                    u1_code_num--;
593
278k
                };
594
595
93.1k
                i1_bins_len++;
596
597
93.1k
                isvce_encode_decision_bins(u4_bins, i1_bins_len, u1_ctxt_inc | 0x320, 2,
598
93.1k
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
599
93.1k
                                           ps_cabac_ctxt);
600
93.1k
            }
601
1.84k
            else
602
1.84k
            {
603
                /* maximum i1_bins_len = 53 */
604
1.84k
                u4_bins = 0xffffffff;
605
1.84k
                i1_bins_len = 32;
606
1.84k
                u1_code_num -= 30;
607
608
1.84k
                isvce_encode_decision_bins(u4_bins, i1_bins_len, u1_ctxt_inc | 0x320, 2,
609
1.84k
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
610
1.84k
                                           ps_cabac_ctxt);
611
612
1.84k
                u4_bins = 0;
613
1.84k
                i1_bins_len = 0;
614
615
4.48k
                while(u1_code_num)
616
2.63k
                {
617
2.63k
                    u4_bins = (u4_bins | (1 << i1_bins_len));
618
2.63k
                    i1_bins_len++;
619
2.63k
                    u1_code_num--;
620
2.63k
                };
621
622
1.84k
                i1_bins_len++;
623
624
1.84k
                isvce_encode_decision_bins(u4_bins, i1_bins_len, 0x333, 1,
625
1.84k
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
626
1.84k
                                           ps_cabac_ctxt);
627
1.84k
            }
628
94.9k
        }
629
106k
    }
630
2.51M
}
631
632
/**
633
 *******************************************************************************
634
 * @brief
635
 *  Encodes 4residual_block_cabac as defined in 7.3.5.3.3.
636
 *
637
 * @param[in] pi2_res_block
638
 *  pointer to the array of residues
639
 *
640
 * @param[in]  u1_nnz
641
 *  Number of non zero coeffs in the block
642
 *
643
 * @param[in] u1_max_num_coeffs
644
 *  Max number of coeffs that can be there in the block
645
 *
646
 * @param[in] u2_sig_coeff_map
647
 *  Significant coeff map
648
 *
649
 * @param[in] u4_ctx_cat_offset
650
 *  ctxIdxOffset for  absolute value contexts
651
 *
652
 * @param[in]  pu1_ctxt_sig_coeff
653
 *  Pointer to residual state variables
654
 *
655
 * @param[in] ps_cabac_ctxt
656
 *  Pointer to cabac context structure
657
 *
658
 * @returns
659
 *
660
 * @remarks
661
 *  None
662
 *
663
 *******************************************************************************
664
 */
665
static void isvce_cabac_write_coeff4x4(WORD16 *pi2_res_block, UWORD8 u1_nnz,
666
                                       UWORD8 u1_max_num_coeffs, UWORD16 u2_sig_coeff_map,
667
                                       UWORD32 u4_ctx_cat_offset,
668
                                       bin_ctxt_model *pu1_ctxt_sig_coeff,
669
                                       isvce_cabac_ctxt_t *ps_cabac_ctxt)
670
6.60M
{
671
6.60M
    WORD8 i;
672
6.60M
    WORD16 *pi16_coeffs;
673
6.60M
    UWORD32 u4_sig_coeff, u4_bins;
674
6.60M
    UWORD32 u4_ctx_inc;
675
6.60M
    UWORD8 u1_last_sig_coef_index = (31 - CLZ(u2_sig_coeff_map));
676
677
    /* Always put Coded Block Flag as 1 */
678
679
6.60M
    pi16_coeffs = pi2_res_block;
680
6.60M
    {
681
6.60M
        bin_ctxt_model *pu1_bin_ctxt;
682
6.60M
        UWORD8 u1_bin, uc_last;
683
684
6.60M
        i = 0;
685
6.60M
        pu1_bin_ctxt = pu1_ctxt_sig_coeff;
686
6.60M
        u4_sig_coeff = 0;
687
6.60M
        u1_bin = 1;
688
6.60M
        if((u1_last_sig_coef_index))
689
4.64M
        {
690
4.64M
            u1_bin = !!(u2_sig_coeff_map & 01);
691
4.64M
        }
692
6.60M
        uc_last = 1;
693
694
6.60M
        do
695
73.9M
        {
696
            /* Encode Decision */
697
73.9M
            isvce_cabac_encode_bin(ps_cabac_ctxt, u1_bin, pu1_bin_ctxt);
698
699
73.9M
            if(u1_bin & uc_last)
700
30.5M
            {
701
30.5M
                u4_sig_coeff = (u4_sig_coeff | (1 << i));
702
30.5M
                pu1_bin_ctxt = pu1_ctxt_sig_coeff + i + LAST_SIGNIFICANT_COEFF_FLAG_FRAME -
703
30.5M
                               SIGNIFICANT_COEFF_FLAG_FRAME;
704
30.5M
                u1_bin = (i == u1_last_sig_coef_index);
705
30.5M
                uc_last = 0;
706
30.5M
            }
707
43.4M
            else
708
43.4M
            {
709
43.4M
                i = i + 1;
710
43.4M
                pu1_bin_ctxt = pu1_ctxt_sig_coeff + i;
711
43.4M
                u1_bin = (i == u1_last_sig_coef_index);
712
43.4M
                uc_last = 1;
713
43.4M
                if((i != u1_last_sig_coef_index))
714
38.7M
                {
715
38.7M
                    u1_bin = !!((u2_sig_coeff_map >> i) & 01);
716
38.7M
                }
717
43.4M
            }
718
73.9M
        } while(!((i > u1_last_sig_coef_index) || (i > (u1_max_num_coeffs - 1))));
719
6.60M
    }
720
721
    /* Encode coeff_abs_level_minus1 and coeff_sign_flag */
722
6.60M
    {
723
6.60M
        UWORD8 u1_sign;
724
6.60M
        UWORD16 u2_abs_level;
725
6.60M
        UWORD8 u1_abs_level_equal1 = 1, u1_abs_level_gt1 = 0;
726
6.60M
        UWORD8 u1_ctx_inc;
727
6.60M
        UWORD8 u1_coff;
728
6.60M
        WORD16 i2_sufs;
729
6.60M
        WORD8 i1_bins_len;
730
6.60M
        i = u1_last_sig_coef_index;
731
6.60M
        pi16_coeffs = pi2_res_block + u1_nnz - 1;
732
6.60M
        do
733
32.0M
        {
734
32.0M
            {
735
32.0M
                u4_sig_coeff = u4_sig_coeff & ((1 << i) - 1);
736
32.0M
                u4_bins = 0;
737
32.0M
                u4_ctx_inc = 0;
738
32.0M
                i1_bins_len = 1;
739
                /* Encode the AbsLevelMinus1 */
740
32.0M
                u2_abs_level = ABS(*(pi16_coeffs)) - 1;
741
                /* CtxInc for bin0 */
742
32.0M
                u4_ctx_inc = MIN(u1_abs_level_equal1, 4);
743
                /* CtxInc for remaining */
744
32.0M
                u1_ctx_inc = 5 + MIN(u1_abs_level_gt1, 4);
745
32.0M
                u4_ctx_inc = u4_ctx_inc + (u1_ctx_inc << 4);
746
32.0M
                if(u2_abs_level)
747
24.7M
                {
748
24.7M
                    u1_abs_level_gt1++;
749
24.7M
                    u1_abs_level_equal1 = 0;
750
24.7M
                }
751
32.0M
                if(!u1_abs_level_gt1) u1_abs_level_equal1++;
752
753
32.0M
                u1_coff = 14;
754
32.0M
                if(u2_abs_level >= u1_coff)
755
10.1M
                {
756
                    /* Prefix TU i.e string of 14 1's */
757
10.1M
                    u4_bins = 0x3fff;
758
10.1M
                    i1_bins_len = 14;
759
10.1M
                    isvce_encode_decision_bins(
760
10.1M
                        u4_bins, i1_bins_len, u4_ctx_inc, 1,
761
10.1M
                        ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_cat_offset, ps_cabac_ctxt);
762
763
                    /* Suffix, uses EncodeBypass */
764
10.1M
                    i2_sufs = u2_abs_level - u1_coff;
765
766
10.1M
                    u4_bins = isvce_cabac_UEGk0_binarization(i2_sufs, &i1_bins_len);
767
768
10.1M
                    isvce_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, i1_bins_len);
769
10.1M
                }
770
21.9M
                else
771
21.9M
                {
772
                    /* Prefix only */
773
21.9M
                    u4_bins = (1 << u2_abs_level) - 1;
774
21.9M
                    i1_bins_len = u2_abs_level + 1;
775
                    /* Encode Terminating bit */
776
21.9M
                    isvce_encode_decision_bins(
777
21.9M
                        u4_bins, i1_bins_len, u4_ctx_inc, 1,
778
21.9M
                        ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_cat_offset, ps_cabac_ctxt);
779
21.9M
                }
780
32.0M
            }
781
            /* encode coeff_sign_flag[i] */
782
32.0M
            u1_sign = ((*pi16_coeffs) < 0) ? 1 : 0;
783
32.0M
            isvce_cabac_encode_bypass_bin(ps_cabac_ctxt, u1_sign);
784
32.0M
            i = CLZ(u4_sig_coeff);
785
32.0M
            i = 31 - i;
786
32.0M
            pi16_coeffs--;
787
32.0M
        } while(u4_sig_coeff);
788
6.60M
    }
789
6.60M
}
790
791
/**
792
 *******************************************************************************
793
 * @brief
794
 * Write DC coeffs for intra predicted luma block
795
 *
796
 * @param[in] ps_ent_ctxt
797
 *  Pointer to entropy context structure
798
 *
799
 * @returns
800
 *
801
 * @remarks
802
 *  None
803
 *
804
 *******************************************************************************
805
 */
806
static void isvce_cabac_encode_residue_luma_dc(isvce_entropy_ctxt_t *ps_ent_ctxt)
807
2.16M
{
808
    /* CABAC context */
809
2.16M
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
810
2.16M
    tu_sblk_coeff_data_t *ps_mb_coeff_data;
811
812
    /* packed residue */
813
2.16M
    void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
814
2.16M
    UWORD16 u2_sig_coeff_map;
815
2.16M
    WORD16 *pi2_res_block;
816
2.16M
    UWORD8 u1_nnz;
817
2.16M
    UWORD8 u1_cbf;
818
2.16M
    isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
819
2.16M
    isvce_mb_info_ctxt_t *p_CurCtxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
820
821
2.16M
    PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz, u2_sig_coeff_map,
822
2.16M
                               pi2_res_block);
823
824
2.16M
    u1_cbf = !!(u1_nnz);
825
826
2.16M
    {
827
2.16M
        UWORD32 u4_ctx_inc;
828
2.16M
        UWORD8 u1_a, u1_b;
829
830
2.16M
        u1_a = ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] & 0x1;
831
2.16M
        u1_b = ps_top_ctxt->u1_yuv_dc_csbp & 0x1;
832
2.16M
        u4_ctx_inc = u1_a + (u1_b << 1);
833
834
2.16M
        isvce_cabac_encode_bin(
835
2.16M
            ps_cabac_ctxt, u1_cbf,
836
2.16M
            ps_cabac_ctxt->au1_cabac_ctxt_table + CBF + (LUMA_DC_CTXCAT << 2) + u4_ctx_inc);
837
2.16M
    }
838
839
    /* Write coded_block_flag */
840
2.16M
    if(u1_cbf)
841
542k
    {
842
542k
        isvce_cabac_write_coeff4x4(pi2_res_block, u1_nnz, 15, u2_sig_coeff_map,
843
542k
                                   COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_0_OFFSET,
844
542k
                                   ps_cabac_ctxt->au1_cabac_ctxt_table +
845
542k
                                       SIGNIFICANT_COEFF_FLAG_FRAME + SIG_COEFF_CTXT_CAT_0_OFFSET,
846
542k
                                   ps_cabac_ctxt);
847
848
542k
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= 0x1;
849
542k
        p_CurCtxt->u1_yuv_dc_csbp |= 0x1;
850
542k
    }
851
1.62M
    else
852
1.62M
    {
853
1.62M
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
854
1.62M
        p_CurCtxt->u1_yuv_dc_csbp &= 0x6;
855
1.62M
    }
856
857
2.16M
    ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
858
2.16M
}
859
860
/**
861
 *******************************************************************************
862
 * @brief
863
 * Write chroma residues to the bitstream
864
 *
865
 * @param[in] ps_ent_ctxt
866
 *  Pointer to entropy context structure
867
 *
868
 * @param[in] u1_chroma_cbp
869
 * coded block pattern, chroma
870
 *
871
 * @returns
872
 *
873
 * @remarks
874
 *  None
875
 *
876
 *******************************************************************************
877
 */
878
static void isvce_cabac_write_chroma_residue(isvce_entropy_ctxt_t *ps_ent_ctxt,
879
                                             UWORD8 u1_chroma_cbp)
880
242k
{
881
    /* CABAC context */
882
242k
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
883
242k
    tu_sblk_coeff_data_t *ps_mb_coeff_data;
884
    /* packed residue */
885
242k
    void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
886
242k
    UWORD16 u2_sig_coeff_map;
887
242k
    UWORD8 u1_nnz;
888
242k
    isvce_mb_info_ctxt_t *ps_top_ctxt_mb_info, *ps_curr_ctxt;
889
890
242k
    ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_top_ctxt_mb_info;
891
242k
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
892
893
    /********************/
894
    /* Write Chroma DC */
895
    /********************/
896
242k
    {
897
242k
        WORD16 *pi2_res_block;
898
242k
        UWORD8 u1_left_dc_csbp, u1_top_dc_csbp, u1_uv, u1_cbf;
899
900
242k
        u1_left_dc_csbp = (ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0]) >> 1;
901
242k
        u1_top_dc_csbp = (ps_top_ctxt_mb_info->u1_yuv_dc_csbp) >> 1;
902
903
728k
        for(u1_uv = 0; u1_uv < 2; u1_uv++)
904
485k
        {
905
485k
            PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz, u2_sig_coeff_map,
906
485k
                                       pi2_res_block);
907
485k
            u1_cbf = !!(u1_nnz);
908
485k
            {
909
485k
                UWORD8 u1_a, u1_b;
910
485k
                UWORD32 u4_ctx_inc;
911
485k
                u1_a = (u1_left_dc_csbp >> u1_uv) & 0x01;
912
485k
                u1_b = (u1_top_dc_csbp >> u1_uv) & 0x01;
913
485k
                u4_ctx_inc = (u1_a + (u1_b << 1));
914
915
485k
                isvce_cabac_encode_bin(ps_cabac_ctxt, u1_cbf,
916
485k
                                       ps_cabac_ctxt->au1_cabac_ctxt_table + CBF +
917
485k
                                           (CHROMA_DC_CTXCAT << 2) + u4_ctx_inc);
918
485k
            }
919
920
485k
            if(u1_cbf)
921
401k
            {
922
401k
                isvce_cabac_write_coeff4x4(pi2_res_block, u1_nnz, 3, u2_sig_coeff_map,
923
401k
                                           COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_3_OFFSET,
924
401k
                                           ps_cabac_ctxt->au1_cabac_ctxt_table +
925
401k
                                               SIGNIFICANT_COEFF_FLAG_FRAME +
926
401k
                                               SIG_COEFF_CTXT_CAT_3_OFFSET,
927
401k
                                           ps_cabac_ctxt);
928
929
401k
                SETBIT(u1_top_dc_csbp, u1_uv);
930
401k
                SETBIT(u1_left_dc_csbp, u1_uv);
931
401k
            }
932
83.8k
            else
933
83.8k
            {
934
83.8k
                CLEARBIT(u1_top_dc_csbp, u1_uv);
935
83.8k
                CLEARBIT(u1_left_dc_csbp, u1_uv);
936
83.8k
            }
937
485k
        }
938
        /*************************************************************/
939
        /*      Update the DC csbp                                   */
940
        /*************************************************************/
941
242k
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x1;
942
242k
        ps_curr_ctxt->u1_yuv_dc_csbp &= 0x1;
943
242k
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= (u1_left_dc_csbp << 1);
944
242k
        ps_curr_ctxt->u1_yuv_dc_csbp |= (u1_top_dc_csbp << 1);
945
242k
    }
946
    /*******************/
947
    /* Write Chroma AC */
948
    /*******************/
949
242k
    {
950
242k
        if(u1_chroma_cbp == 2)
951
196k
        {
952
196k
            UWORD8 u1_uv_blkno, u1_left_ac_csbp, u1_top_ac_csbp;
953
196k
            WORD16 *pi2_res_block;
954
196k
            u1_left_ac_csbp = ps_cabac_ctxt->pu1_left_uv_ac_csbp[0];
955
196k
            u1_top_ac_csbp = ps_top_ctxt_mb_info->u1_yuv_ac_csbp >> 4;
956
957
1.76M
            for(u1_uv_blkno = 0; u1_uv_blkno < 8; u1_uv_blkno++)
958
1.57M
            {
959
1.57M
                UWORD8 u1_cbf;
960
1.57M
                UWORD8 u1_b2b0, u1_b2b1;
961
1.57M
                PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz,
962
1.57M
                                           u2_sig_coeff_map, pi2_res_block);
963
964
1.57M
                u1_cbf = !!(u1_nnz);
965
1.57M
                u1_b2b0 = ((u1_uv_blkno & 0x4) >> 1) | (u1_uv_blkno & 0x1);
966
1.57M
                u1_b2b1 = ((u1_uv_blkno & 0x4) >> 1) | ((u1_uv_blkno & 0x2) >> 1);
967
968
1.57M
                {
969
1.57M
                    UWORD8 u1_a, u1_b;
970
1.57M
                    UWORD32 u4_ctx_inc;
971
                    /* write coded_block_flag */
972
1.57M
                    u1_a = (u1_left_ac_csbp >> u1_b2b1) & 0x1;
973
1.57M
                    u1_b = (u1_top_ac_csbp >> u1_b2b0) & 0x1;
974
1.57M
                    u4_ctx_inc = u1_a + (u1_b << 1);
975
976
1.57M
                    isvce_cabac_encode_bin(ps_cabac_ctxt, u1_cbf,
977
1.57M
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + CBF +
978
1.57M
                                               (CHROMA_AC_CTXCAT << 2) + u4_ctx_inc);
979
1.57M
                }
980
1.57M
                if(u1_cbf)
981
1.07M
                {
982
1.07M
                    isvce_cabac_write_coeff4x4(
983
1.07M
                        pi2_res_block, u1_nnz, 14, u2_sig_coeff_map,
984
1.07M
                        COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_4_OFFSET,
985
1.07M
                        ps_cabac_ctxt->au1_cabac_ctxt_table + +SIGNIFICANT_COEFF_FLAG_FRAME +
986
1.07M
                            SIG_COEFF_CTXT_CAT_4_OFFSET,
987
1.07M
                        ps_cabac_ctxt);
988
989
1.07M
                    SETBIT(u1_left_ac_csbp, u1_b2b1);
990
1.07M
                    SETBIT(u1_top_ac_csbp, u1_b2b0);
991
1.07M
                }
992
491k
                else
993
491k
                {
994
491k
                    CLEARBIT(u1_left_ac_csbp, u1_b2b1);
995
491k
                    CLEARBIT(u1_top_ac_csbp, u1_b2b0);
996
491k
                }
997
1.57M
            }
998
            /*************************************************************/
999
            /*      Update the AC csbp                                   */
1000
            /*************************************************************/
1001
196k
            ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = u1_left_ac_csbp;
1002
196k
            ps_curr_ctxt->u1_yuv_ac_csbp &= 0x0f;
1003
196k
            ps_curr_ctxt->u1_yuv_ac_csbp |= (u1_top_ac_csbp << 4);
1004
196k
        }
1005
46.4k
        else
1006
46.4k
        {
1007
46.4k
            ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0;
1008
46.4k
            ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf;
1009
46.4k
        }
1010
242k
    }
1011
242k
    ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
1012
242k
}
1013
1014
/**
1015
 *******************************************************************************
1016
 * @brief
1017
 * Encodes Residues for the MB as defined in 7.3.5.3
1018
 *
1019
 * @param[in] ps_ent_ctxt
1020
 *  Pointer to entropy context structure
1021
 *
1022
 * @param[in] u1_cbp
1023
 * coded block pattern
1024
 *
1025
 * @param[in] u1_ctx_cat
1026
 * Context category, LUMA_AC_CTXCAT or LUMA_4x4_CTXCAT
1027
 *
1028
 * @returns
1029
 *
1030
 * @remarks
1031
 *  None
1032
 *
1033
 *******************************************************************************
1034
 */
1035
static void isvce_cabac_encode_residue(isvce_entropy_ctxt_t *ps_ent_ctxt, UWORD32 u4_cbp,
1036
                                       UWORD8 u1_ctx_cat)
1037
2.51M
{
1038
    /* CABAC context */
1039
2.51M
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
1040
1041
2.51M
    tu_sblk_coeff_data_t *ps_mb_coeff_data;
1042
    /* packed residue */
1043
2.51M
    void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
1044
2.51M
    UWORD16 u2_sig_coeff_map;
1045
2.51M
    UWORD8 u1_nnz;
1046
2.51M
    isvce_mb_info_ctxt_t *ps_curr_ctxt;
1047
2.51M
    isvce_mb_info_ctxt_t *ps_top_ctxt;
1048
2.51M
    UWORD8 u1_left_ac_csbp;
1049
2.51M
    UWORD8 u1_top_ac_csbp;
1050
2.51M
    UWORD32 u4_ctx_idx_offset_sig_coef, u4_ctx_idx_offset_abs_lvl;
1051
2.51M
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
1052
2.51M
    ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
1053
2.51M
    u1_left_ac_csbp = ps_cabac_ctxt->pu1_left_y_ac_csbp[0];
1054
2.51M
    u1_top_ac_csbp = ps_top_ctxt->u1_yuv_ac_csbp;
1055
1056
2.51M
    if(u4_cbp & 0xf)
1057
404k
    {
1058
        /*  Write luma residue  */
1059
404k
        UWORD8 u1_offset;
1060
404k
        WORD16 *pi2_res_block;
1061
404k
        UWORD8 u1_subblk_num;
1062
404k
        if(u1_ctx_cat == LUMA_AC_CTXCAT)
1063
143k
        {
1064
143k
            u1_offset = 1;
1065
143k
            u4_ctx_idx_offset_sig_coef = SIG_COEFF_CTXT_CAT_1_OFFSET;
1066
143k
            u4_ctx_idx_offset_abs_lvl = COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_1_OFFSET;
1067
143k
        }
1068
260k
        else
1069
260k
        {
1070
260k
            u1_offset = 0;
1071
260k
            u4_ctx_idx_offset_sig_coef = SIG_COEFF_CTXT_CAT_2_OFFSET;
1072
260k
            u4_ctx_idx_offset_abs_lvl = COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_2_OFFSET;
1073
260k
        }
1074
1075
6.43M
        for(u1_subblk_num = 0; u1_subblk_num < 16; u1_subblk_num++)
1076
6.03M
        {
1077
6.03M
            UWORD8 u1_b0, u1_b1, u1_b2, u1_b3, u1_b2b0, u1_b3b1, u1_b3b2;
1078
6.03M
            u1_b0 = (u1_subblk_num & 0x1);
1079
6.03M
            u1_b1 = (u1_subblk_num & 0x2) >> 1;
1080
6.03M
            u1_b2 = (u1_subblk_num & 0x4) >> 2;
1081
6.03M
            u1_b3 = (u1_subblk_num & 0x8) >> 3;
1082
6.03M
            u1_b2b0 = (u1_b2 << 1) | (u1_b0);
1083
6.03M
            u1_b3b1 = (u1_b3 << 1) | (u1_b1);
1084
6.03M
            u1_b3b2 = (u1_b3 << 1) | (u1_b2);
1085
1086
6.03M
            if(!((u4_cbp >> u1_b3b2) & 0x1))
1087
146k
            {
1088
                /* ---------------------------------------------------------- */
1089
                /* The current block is not coded so skip all the sub block */
1090
                /* and set the pointer of scan level, csbp accrodingly      */
1091
                /* ---------------------------------------------------------- */
1092
146k
                CLEARBIT(u1_top_ac_csbp, u1_b2b0);
1093
146k
                CLEARBIT(u1_top_ac_csbp, (u1_b2b0 + 1));
1094
146k
                CLEARBIT(u1_left_ac_csbp, u1_b3b1);
1095
146k
                CLEARBIT(u1_left_ac_csbp, (u1_b3b1 + 1));
1096
1097
146k
                u1_subblk_num += 3;
1098
146k
            }
1099
5.88M
            else
1100
5.88M
            {
1101
5.88M
                UWORD8 u1_csbf;
1102
1103
5.88M
                PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz,
1104
5.88M
                                           u2_sig_coeff_map, pi2_res_block);
1105
1106
5.88M
                u1_csbf = !!(u1_nnz);
1107
5.88M
                {
1108
5.88M
                    UWORD8 u1_a, u1_b;
1109
5.88M
                    UWORD32 u4_ctx_inc;
1110
5.88M
                    u1_b = (u1_top_ac_csbp >> u1_b2b0) & 0x01;
1111
5.88M
                    u1_a = (u1_left_ac_csbp >> u1_b3b1) & 0x01;
1112
5.88M
                    u4_ctx_inc = u1_a + (u1_b << 1);
1113
1114
                    /* Encode the bin */
1115
5.88M
                    isvce_cabac_encode_bin(
1116
5.88M
                        ps_cabac_ctxt, u1_csbf,
1117
5.88M
                        ps_cabac_ctxt->au1_cabac_ctxt_table + CBF + (u1_ctx_cat << 2) + u4_ctx_inc);
1118
5.88M
                }
1119
                /**************************/
1120
                /* Write coded_block_flag */
1121
                /**************************/
1122
5.88M
                if(u1_csbf)
1123
4.57M
                {
1124
4.57M
                    isvce_cabac_write_coeff4x4(pi2_res_block, u1_nnz, (UWORD8) (15 - u1_offset),
1125
4.57M
                                               u2_sig_coeff_map, u4_ctx_idx_offset_abs_lvl,
1126
4.57M
                                               ps_cabac_ctxt->au1_cabac_ctxt_table +
1127
4.57M
                                                   SIGNIFICANT_COEFF_FLAG_FRAME +
1128
4.57M
                                                   u4_ctx_idx_offset_sig_coef,
1129
4.57M
                                               ps_cabac_ctxt);
1130
1131
4.57M
                    SETBIT(u1_top_ac_csbp, u1_b2b0);
1132
4.57M
                    SETBIT(u1_left_ac_csbp, u1_b3b1);
1133
4.57M
                }
1134
1.30M
                else
1135
1.30M
                {
1136
1.30M
                    CLEARBIT(u1_top_ac_csbp, u1_b2b0);
1137
1.30M
                    CLEARBIT(u1_left_ac_csbp, u1_b3b1);
1138
1.30M
                }
1139
5.88M
            }
1140
6.03M
        }
1141
        /**************************************************************************/
1142
        /*                   Update the AC csbp                                   */
1143
        /**************************************************************************/
1144
404k
        ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = u1_left_ac_csbp & 0xf;
1145
404k
        u1_top_ac_csbp &= 0x0f;
1146
404k
        ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf0;
1147
404k
        ps_curr_ctxt->u1_yuv_ac_csbp |= u1_top_ac_csbp;
1148
404k
    }
1149
2.10M
    else
1150
2.10M
    {
1151
2.10M
        ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = 0;
1152
2.10M
        ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf0;
1153
2.10M
    }
1154
1155
    /*     Write chroma residue */
1156
1157
2.51M
    ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
1158
2.51M
    {
1159
2.51M
        UWORD8 u1_cbp_chroma;
1160
2.51M
        u1_cbp_chroma = u4_cbp >> 4;
1161
2.51M
        if(u1_cbp_chroma)
1162
242k
        {
1163
242k
            isvce_cabac_write_chroma_residue(ps_ent_ctxt, u1_cbp_chroma);
1164
242k
        }
1165
2.27M
        else
1166
2.27M
        {
1167
2.27M
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x1;
1168
2.27M
            ps_curr_ctxt->u1_yuv_dc_csbp &= 0x1;
1169
2.27M
            ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0;
1170
2.27M
            ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf;
1171
2.27M
        }
1172
2.51M
    }
1173
2.51M
}
1174
1175
/**
1176
 *******************************************************************************
1177
 * @brief
1178
 * Encodes a Motion vector (9.3.3.1.1.7 )
1179
 *
1180
 * @param[in] u1_mvd
1181
 *  Motion vector to be encoded
1182
 *
1183
 * @param[in] u4_ctx_idx_offset
1184
 * *  ctxIdxOffset for MV_X or MV_Ycontext
1185
 *
1186
 * @param[in]  ui2_abs_mvd
1187
 * sum of absolute value of corresponding neighboring motion vectors
1188
 *
1189
 * @param[in] ps_cabac_ctxt
1190
 *  Pointer to cabac context structure
1191
 *
1192
 * @returns
1193
 *
1194
 * @remarks
1195
 *  None
1196
 *
1197
 *******************************************************************************
1198
 */
1199
static void isvce_cabac_enc_ctx_mvd(WORD16 u1_mvd, UWORD32 u4_ctx_idx_offset, UWORD16 ui2_abs_mvd,
1200
                                    isvce_cabac_ctxt_t *ps_cabac_ctxt)
1201
192k
{
1202
192k
    UWORD8 u1_bin, u1_ctxt_inc;
1203
192k
    WORD8 k = 3, u1_coff = 9;
1204
192k
    WORD16 i2_abs_mvd, i2_sufs;
1205
192k
    UWORD32 u4_ctx_inc;
1206
192k
    UWORD32 u4_bins;
1207
192k
    WORD8 i1_bins_len;
1208
1209
    /* if mvd < u1_coff
1210
     only Prefix
1211
     else
1212
     Prefix + Suffix
1213
1214
     encode sign bit
1215
1216
     Prefix TU encoding Cmax =u1_coff and Suffix 3rd order Exp-Golomb
1217
     */
1218
1219
192k
    if(ui2_abs_mvd < 3)
1220
145k
        u4_ctx_inc = 0;
1221
46.9k
    else if(ui2_abs_mvd > 32)
1222
15.2k
        u4_ctx_inc = 2;
1223
31.7k
    else
1224
31.7k
        u4_ctx_inc = 1;
1225
1226
192k
    u4_bins = 0;
1227
192k
    i1_bins_len = 1;
1228
1229
192k
    if(u1_mvd == 0)
1230
129k
    {
1231
129k
        isvce_cabac_encode_bin(
1232
129k
            ps_cabac_ctxt, 0, ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset + u4_ctx_inc);
1233
129k
    }
1234
62.5k
    else
1235
62.5k
    {
1236
62.5k
        i2_abs_mvd = ABS(u1_mvd);
1237
62.5k
        if(i2_abs_mvd >= u1_coff)
1238
29.0k
        {
1239
            /* Prefix TU i.e string of 9 1's */
1240
29.0k
            u4_bins = 0x1ff;
1241
29.0k
            i1_bins_len = 9;
1242
29.0k
            u4_ctx_inc = (u4_ctx_inc | 0x065430);
1243
1244
29.0k
            isvce_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 4,
1245
29.0k
                                       ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset,
1246
29.0k
                                       ps_cabac_ctxt);
1247
1248
            /* Suffix, uses EncodeBypass */
1249
29.0k
            u4_bins = 0;
1250
29.0k
            i1_bins_len = 0;
1251
29.0k
            i2_sufs = i2_abs_mvd - u1_coff;
1252
75.1k
            while(1)
1253
75.1k
            {
1254
75.1k
                if(i2_sufs >= (1 << k))
1255
46.0k
                {
1256
46.0k
                    u4_bins = (u4_bins | (1 << (31 - i1_bins_len)));
1257
46.0k
                    i1_bins_len++;
1258
46.0k
                    i2_sufs = i2_sufs - (1 << k);
1259
46.0k
                    k++;
1260
46.0k
                }
1261
29.0k
                else
1262
29.0k
                {
1263
29.0k
                    i1_bins_len++;
1264
162k
                    while(k--)
1265
133k
                    {
1266
133k
                        u1_bin = ((i2_sufs >> k) & 0x01);
1267
133k
                        u4_bins = (u4_bins | (u1_bin << (31 - i1_bins_len)));
1268
133k
                        i1_bins_len++;
1269
133k
                    }
1270
29.0k
                    break;
1271
29.0k
                }
1272
75.1k
            }
1273
29.0k
            u4_bins >>= (32 - i1_bins_len);
1274
29.0k
            isvce_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, i1_bins_len);
1275
29.0k
        }
1276
33.5k
        else
1277
33.5k
        {
1278
            /* Prefix only */
1279
            /* b0 */
1280
33.5k
            u4_bins = 1;
1281
33.5k
            i2_abs_mvd--;
1282
33.5k
            u1_ctxt_inc = 3;
1283
129k
            while(i2_abs_mvd)
1284
95.6k
            {
1285
95.6k
                i2_abs_mvd--;
1286
95.6k
                u4_bins = (u4_bins | (1 << i1_bins_len));
1287
95.6k
                if(u1_ctxt_inc <= 6)
1288
77.2k
                {
1289
77.2k
                    u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i1_bins_len << 2)));
1290
77.2k
                    u1_ctxt_inc++;
1291
77.2k
                }
1292
95.6k
                i1_bins_len++;
1293
95.6k
            }
1294
            /* Encode Terminating bit */
1295
33.5k
            if(i1_bins_len <= 4) u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i1_bins_len << 2)));
1296
33.5k
            i1_bins_len++;
1297
33.5k
            isvce_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 4,
1298
33.5k
                                       ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset,
1299
33.5k
                                       ps_cabac_ctxt);
1300
33.5k
        }
1301
        /* sign bit, uses EncodeBypass */
1302
62.5k
        if(u1_mvd > 0)
1303
30.2k
            isvce_cabac_encode_bypass_bin(ps_cabac_ctxt, 0);
1304
32.2k
        else
1305
32.2k
            isvce_cabac_encode_bypass_bin(ps_cabac_ctxt, 1);
1306
62.5k
    }
1307
192k
}
1308
1309
/**
1310
 *******************************************************************************
1311
 * @brief
1312
 * Encodes all motion vectors for a P16x16 MB
1313
 *
1314
 * @param[in] ps_cabac_ctxt
1315
 *  Pointer to cabac context structure
1316
 *
1317
 * @param[in] pi2_mv_ptr
1318
 * Pointer to array of motion vectors
1319
 *
1320
 * @returns
1321
 *
1322
 * @remarks
1323
 *  None
1324
 *
1325
 *******************************************************************************
1326
 */
1327
static void isvce_cabac_enc_mvds_p16x16(isvce_cabac_ctxt_t *ps_cabac_ctxt, WORD16 *pi2_mv_ptr)
1328
96.1k
{
1329
96.1k
    UWORD8 u1_abs_mvd_x, u1_abs_mvd_y;
1330
96.1k
    UWORD8 *pu1_top_mv_ctxt, *pu1_lft_mv_ctxt;
1331
96.1k
    WORD16 u2_mv;
1332
96.1k
    u1_abs_mvd_x = 0;
1333
96.1k
    u1_abs_mvd_y = 0;
1334
96.1k
    pu1_top_mv_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv[0];
1335
96.1k
    pu1_lft_mv_ctxt = ps_cabac_ctxt->pu1_left_mv_ctxt_inc[0];
1336
96.1k
    {
1337
96.1k
        UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, u2_abs_mvd_y_b;
1338
96.1k
        u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[0];
1339
96.1k
        u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[1];
1340
96.1k
        u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[0];
1341
96.1k
        u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[1];
1342
96.1k
        u2_mv = *(pi2_mv_ptr++);
1343
1344
96.1k
        isvce_cabac_enc_ctx_mvd(u2_mv, MVD_X, (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b),
1345
96.1k
                                ps_cabac_ctxt);
1346
1347
96.1k
        u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv));
1348
96.1k
        u2_mv = *(pi2_mv_ptr++);
1349
1350
96.1k
        isvce_cabac_enc_ctx_mvd(u2_mv, MVD_Y, (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b),
1351
96.1k
                                ps_cabac_ctxt);
1352
1353
96.1k
        u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv));
1354
96.1k
    }
1355
    /***************************************************************/
1356
    /* Store abs_mvd_values cabac contexts                         */
1357
    /***************************************************************/
1358
96.1k
    pu1_top_mv_ctxt[0] = pu1_lft_mv_ctxt[0] = u1_abs_mvd_x;
1359
96.1k
    pu1_top_mv_ctxt[1] = pu1_lft_mv_ctxt[1] = u1_abs_mvd_y;
1360
96.1k
}
1361
1362
/**
1363
 *******************************************************************************
1364
 * @brief
1365
 * Encodes all motion vectors for a B MB (Assues that mbype is B_L0_16x16,
1366
 *B_L1_16x16 or B_Bi_16x16
1367
 *
1368
 * @param[in] ps_cabac_ctxt
1369
 *  Pointer to cabac context structure
1370
 *
1371
 * @param[in] pi2_mv_ptr
1372
 * Pointer to array of motion vectors
1373
 *
1374
 * @returns
1375
 *
1376
 * @remarks
1377
 *  None
1378
 *
1379
 *******************************************************************************
1380
 */
1381
static void isvce_cabac_enc_mvds_b16x16(isvce_cabac_ctxt_t *ps_cabac_ctxt, WORD16 *pi2_mv_ptr,
1382
                                        WORD32 i4_mb_part_pred_mode)
1383
0
{
1384
    /* Encode the differential component of the motion vectors */
1385
1386
0
    {
1387
0
        UWORD8 u1_abs_mvd_x, u1_abs_mvd_y;
1388
0
        UWORD8 *pu1_top_mv_ctxt, *pu1_lft_mv_ctxt;
1389
0
        WORD16 u2_mv;
1390
0
        u1_abs_mvd_x = 0;
1391
0
        u1_abs_mvd_y = 0;
1392
0
        pu1_top_mv_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv[0];
1393
0
        pu1_lft_mv_ctxt = ps_cabac_ctxt->pu1_left_mv_ctxt_inc[0];
1394
0
        if(i4_mb_part_pred_mode != L1)
1395
0
        {
1396
0
            UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, u2_abs_mvd_y_b;
1397
0
            u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[0];
1398
0
            u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[1];
1399
0
            u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[0];
1400
0
            u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[1];
1401
0
            u2_mv = pi2_mv_ptr[0];
1402
1403
0
            isvce_cabac_enc_ctx_mvd(u2_mv, MVD_X, (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b),
1404
0
                                    ps_cabac_ctxt);
1405
1406
0
            u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv));
1407
0
            u2_mv = pi2_mv_ptr[1];
1408
1409
0
            isvce_cabac_enc_ctx_mvd(u2_mv, MVD_Y, (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b),
1410
0
                                    ps_cabac_ctxt);
1411
1412
0
            u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv));
1413
0
        }
1414
1415
        /***************************************************************/
1416
        /* Store abs_mvd_values cabac contexts                         */
1417
        /***************************************************************/
1418
0
        pu1_top_mv_ctxt[0] = pu1_lft_mv_ctxt[0] = u1_abs_mvd_x;
1419
0
        pu1_top_mv_ctxt[1] = pu1_lft_mv_ctxt[1] = u1_abs_mvd_y;
1420
1421
0
        u1_abs_mvd_x = 0;
1422
0
        u1_abs_mvd_y = 0;
1423
0
        if(i4_mb_part_pred_mode != L0)
1424
0
        {
1425
0
            UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, u2_abs_mvd_y_b;
1426
0
            u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[2];
1427
0
            u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[3];
1428
0
            u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[2];
1429
0
            u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[3];
1430
0
            u2_mv = pi2_mv_ptr[2];
1431
1432
0
            isvce_cabac_enc_ctx_mvd(u2_mv, MVD_X, (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b),
1433
0
                                    ps_cabac_ctxt);
1434
1435
0
            u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv));
1436
0
            u2_mv = pi2_mv_ptr[3];
1437
1438
0
            isvce_cabac_enc_ctx_mvd(u2_mv, MVD_Y, (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b),
1439
0
                                    ps_cabac_ctxt);
1440
1441
0
            u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv));
1442
0
        }
1443
        /***************************************************************/
1444
        /* Store abs_mvd_values cabac contexts                         */
1445
        /***************************************************************/
1446
0
        pu1_top_mv_ctxt[2] = pu1_lft_mv_ctxt[2] = u1_abs_mvd_x;
1447
0
        pu1_top_mv_ctxt[3] = pu1_lft_mv_ctxt[3] = u1_abs_mvd_y;
1448
0
    }
1449
0
}
1450
1451
static FORCEINLINE void isvce_mb_ctxt_update(isvce_cabac_ctxt_t *ps_cabac_ctxt,
1452
                                             isvce_mb_info_ctxt_t *ps_curr_ctxt,
1453
                                             WORD8 i1_mb_qp_delta, UWORD8 u1_cbp,
1454
                                             UWORD8 u1_base_mode_flag, MBTYPES_T e_mb_type)
1455
4.21M
{
1456
4.21M
    UWORD8 u1_is_intra_mb = (e_mb_type == I16x16) || (e_mb_type == I8x8) || (e_mb_type == I4x4);
1457
4.21M
    UWORD8 u1_is_skip_mb = (e_mb_type == PSKIP) || (e_mb_type == BSKIP);
1458
4.21M
    UWORD8 u1_is_direct_mb = (e_mb_type == BDIRECT);
1459
1460
4.21M
    ps_curr_ctxt->u1_cbp = u1_cbp;
1461
4.21M
    ps_curr_ctxt->u1_base_mode_flag = u1_base_mode_flag;
1462
1463
4.21M
    if(u1_is_intra_mb || u1_is_skip_mb || u1_is_direct_mb || u1_base_mode_flag)
1464
4.11M
    {
1465
4.11M
        memset(ps_curr_ctxt->u1_mv, 0, 16);
1466
4.11M
        memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16);
1467
4.11M
    }
1468
1469
4.21M
    if((0 == u1_cbp) && (e_mb_type != I16x16))
1470
1.69M
    {
1471
1.69M
        ps_curr_ctxt->u1_yuv_ac_csbp = 0;
1472
1.69M
        ps_curr_ctxt->u1_yuv_dc_csbp = 0;
1473
1474
1.69M
        ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0;
1475
1.69M
        ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = 0;
1476
1.69M
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] = 0;
1477
1.69M
    }
1478
1479
4.21M
    if(u1_is_skip_mb)
1480
265k
    {
1481
265k
        ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 0;
1482
265k
    }
1483
3.94M
    else if((I16x16 != e_mb_type) && (0 == u1_cbp))
1484
1.43M
    {
1485
1.43M
        ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 0;
1486
1.43M
    }
1487
2.51M
    else if(0 == i1_mb_qp_delta)
1488
2.40M
    {
1489
2.40M
        ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 0;
1490
2.40M
    }
1491
106k
    else
1492
106k
    {
1493
106k
        ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 1;
1494
106k
    }
1495
1496
4.21M
    if(!u1_is_intra_mb || u1_base_mode_flag)
1497
1.93M
    {
1498
1.93M
        ps_curr_ctxt->u1_intrapred_chroma_mode = 0;
1499
1.93M
    }
1500
4.21M
}
1501
1502
/**
1503
 *******************************************************************************
1504
 *
1505
 * @brief
1506
 *  This function generates CABAC coded bit stream for an Intra Slice.
1507
 *
1508
 * @description
1509
 *  The mb syntax layer for intra slices constitutes luma mb mode, mb qp delta,
1510
 *coded block pattern, chroma mb mode and luma/chroma residue. These syntax
1511
 *elements are written as directed by table 7.3.5 of h264 specification.
1512
 *
1513
 * @param[in] ps_ent_ctxt
1514
 *  pointer to entropy context
1515
 *
1516
 * @returns error code
1517
 *
1518
 * @remarks none
1519
 *
1520
 *******************************************************************************
1521
 */
1522
IH264E_ERROR_T isvce_write_islice_mb_cabac(isvce_entropy_ctxt_t *ps_ent_ctxt)
1523
3.68M
{
1524
3.68M
    isvce_mb_info_ctxt_t *ps_curr_ctxt;
1525
1526
3.68M
    WORD32 mb_tpm, mb_type, chroma_intra_mode, luma_intra_mode;
1527
3.68M
    UWORD8 u1_cbp, u1_cbp_l, u1_cbp_c;
1528
3.68M
    WORD8 mb_qp_delta;
1529
3.68M
    WORD32 bitstream_start_offset, bitstream_end_offset;
1530
3.68M
    UWORD8 u1_base_mode_flag;
1531
1532
3.68M
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1533
3.68M
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
1534
3.68M
    svc_slice_header_t *ps_svc_slice_header =
1535
3.68M
        ps_ent_ctxt->ps_svc_slice_hdr_base +
1536
3.68M
        (ps_ent_ctxt->i4_cur_slice_idx % SVC_MAX_SLICE_HDR_CNT);
1537
3.68M
    isvce_mb_hdr_common_t *ps_mb_hdr = (isvce_mb_hdr_common_t *) ps_ent_ctxt->pv_mb_header_data;
1538
1539
3.68M
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1540
1541
3.68M
    if((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) >= ps_bitstream->u4_max_strm_size)
1542
151
    {
1543
        /* return without corrupting the buffer beyond its size */
1544
151
        return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
1545
151
    }
1546
1547
3.68M
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
1548
3.68M
    u1_base_mode_flag = ps_mb_hdr->u1_base_mode_flag;
1549
3.68M
    u1_cbp = ps_mb_hdr->u1_cbp;
1550
3.68M
    u1_cbp_c = (u1_cbp >> 4);
1551
3.68M
    u1_cbp_l = (u1_cbp & 0xF);
1552
3.68M
    mb_type = mb_tpm & 0xF;
1553
1554
3.68M
    isvce_get_cabac_context(ps_ent_ctxt, mb_type);
1555
3.68M
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
1556
1557
3.68M
    bitstream_start_offset = isvce_get_num_bits(ps_bitstream);
1558
1559
3.68M
    if(mb_type == I16x16)
1560
2.04M
    {
1561
2.04M
        luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u1_cbp_c << 2) + (u1_cbp_l == 15) * 12;
1562
2.04M
    }
1563
1.63M
    else
1564
1.63M
    {
1565
1.63M
        luma_intra_mode = 0;
1566
1.63M
    }
1567
1568
3.68M
    chroma_intra_mode = (mb_tpm >> 6);
1569
1570
3.68M
    if(ps_ent_ctxt->u1_spatial_layer_id && ps_svc_slice_header->i1_adaptive_base_mode_flag)
1571
3.17M
    {
1572
3.17M
        isvce_cabac_enc_base_mode_flag(ps_cabac_ctxt, u1_base_mode_flag);
1573
3.17M
    }
1574
1575
3.68M
    if(!u1_base_mode_flag)
1576
2.14M
    {
1577
2.14M
        isvce_cabac_enc_intra_mb_type(ISLICE, luma_intra_mode, ps_cabac_ctxt, MB_TYPE_I_SLICE);
1578
1579
2.14M
        if(mb_type == I4x4)
1580
100k
        {
1581
100k
            isvce_mb_hdr_i4x4_t *ps_mb_hdr_i4x4 =
1582
100k
                (isvce_mb_hdr_i4x4_t *) ps_ent_ctxt->pv_mb_header_data;
1583
1584
100k
            isvce_cabac_enc_4x4mb_modes(ps_cabac_ctxt, ps_mb_hdr_i4x4->au1_sub_blk_modes);
1585
100k
        }
1586
1587
2.14M
        isvce_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt);
1588
2.14M
    }
1589
1590
3.68M
    if(u1_base_mode_flag || (mb_type != I16x16))
1591
1.63M
    {
1592
1.63M
        isvce_cabac_enc_cbp(u1_cbp, ps_cabac_ctxt);
1593
1.63M
    }
1594
1595
3.68M
    if((u1_cbp > 0) || (mb_type == I16x16))
1596
2.25M
    {
1597
2.25M
        mb_qp_delta =
1598
2.25M
            ((WORD16) ps_mb_hdr->u1_mb_qp) - ((WORD16) ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp);
1599
1600
2.25M
        isvce_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt);
1601
2.25M
        ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp = ps_mb_hdr->u1_mb_qp;
1602
1603
2.25M
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1604
2.25M
        ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset - bitstream_start_offset;
1605
2.25M
        bitstream_start_offset = bitstream_end_offset;
1606
1607
2.25M
        if(mb_type == I16x16)
1608
2.04M
        {
1609
2.04M
            ps_curr_ctxt->u1_mb_type = CAB_I16x16;
1610
1611
2.04M
            isvce_cabac_encode_residue_luma_dc(ps_ent_ctxt);
1612
1613
2.04M
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_AC_CTXCAT);
1614
1615
2.04M
            pu1_byte += sizeof(isvce_mb_hdr_i16x16_t);
1616
2.04M
        }
1617
215k
        else if(mb_type == I4x4)
1618
63.5k
        {
1619
63.5k
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
1620
1621
63.5k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1622
1623
63.5k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1624
63.5k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1625
1626
63.5k
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
1627
63.5k
        }
1628
152k
        else if(mb_type == BASE_MODE)
1629
152k
        {
1630
152k
            ps_curr_ctxt->u1_mb_type = CAB_P | CAB_NON_BD16x16;
1631
1632
152k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1633
1634
152k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1635
152k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1636
1637
152k
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
1638
152k
        }
1639
1640
2.25M
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1641
2.25M
        ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset - bitstream_start_offset;
1642
2.25M
    }
1643
1.42M
    else
1644
1.42M
    {
1645
1.42M
        mb_qp_delta = 0;
1646
1647
1.42M
        if(mb_type == I16x16)
1648
0
        {
1649
0
            ps_curr_ctxt->u1_mb_type = CAB_I16x16;
1650
1651
0
            pu1_byte += sizeof(isvce_mb_hdr_i16x16_t);
1652
0
        }
1653
1.42M
        else if(mb_type == I4x4)
1654
37.3k
        {
1655
37.3k
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
1656
1657
37.3k
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
1658
37.3k
        }
1659
1.38M
        else if(mb_type == BASE_MODE)
1660
1.38M
        {
1661
1.38M
            ps_curr_ctxt->u1_mb_type = CAB_P | CAB_NON_BD16x16;
1662
1663
1.38M
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
1664
1.38M
        }
1665
1666
1.42M
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1667
1.42M
        ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset - bitstream_start_offset;
1668
1.42M
    }
1669
1670
3.68M
    isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, mb_qp_delta, u1_cbp, u1_base_mode_flag,
1671
3.68M
                         mb_type);
1672
1673
3.68M
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1674
1675
3.68M
    return IH264E_SUCCESS;
1676
3.68M
}
1677
1678
/**
1679
 *******************************************************************************
1680
 *
1681
 * @brief
1682
 *  This function generates CABAC coded bit stream for Inter slices
1683
 *
1684
 * @description
1685
 *  The mb syntax layer for inter slices constitutes luma mb mode, mb qp delta,
1686
 *coded block pattern, chroma mb mode and luma/chroma residue. These syntax
1687
 *elements are written as directed by table 7.3.5 of h264 specification
1688
 *
1689
 * @param[in] ps_ent_ctxt
1690
 *  pointer to entropy context
1691
 *
1692
 * @returns error code
1693
 *
1694
 * @remarks none
1695
 *
1696
 *******************************************************************************
1697
 */
1698
IH264E_ERROR_T isvce_write_pslice_mb_cabac(isvce_entropy_ctxt_t *ps_ent_ctxt)
1699
490k
{
1700
490k
    isvce_mb_info_ctxt_t *ps_curr_ctxt;
1701
1702
490k
    WORD32 mb_tpm, mb_type, chroma_intra_mode, luma_intra_mode;
1703
490k
    UWORD8 u1_cbp, u1_cbp_l, u1_cbp_c;
1704
490k
    WORD8 mb_qp_delta;
1705
490k
    WORD32 bitstream_start_offset, bitstream_end_offset;
1706
490k
    UWORD8 u1_base_mode_flag;
1707
490k
    UWORD8 u1_is_intra_mb;
1708
1709
490k
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1710
490k
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
1711
490k
    svc_slice_header_t *ps_svc_slice_header =
1712
490k
        ps_ent_ctxt->ps_svc_slice_hdr_base +
1713
490k
        (ps_ent_ctxt->i4_cur_slice_idx % SVC_MAX_SLICE_HDR_CNT);
1714
490k
    isvce_mb_hdr_common_t *ps_mb_hdr = (isvce_mb_hdr_common_t *) ps_ent_ctxt->pv_mb_header_data;
1715
1716
490k
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1717
1718
490k
    if((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) >= ps_bitstream->u4_max_strm_size)
1719
26
    {
1720
        /* return without corrupting the buffer beyond its size */
1721
26
        return IH264E_BITSTREAM_BUFFER_OVERFLOW;
1722
26
    }
1723
1724
    /* mb header info */
1725
490k
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
1726
490k
    u1_base_mode_flag = ps_mb_hdr->u1_base_mode_flag;
1727
490k
    u1_cbp = ps_mb_hdr->u1_cbp;
1728
490k
    u1_cbp_c = (u1_cbp >> 4);
1729
490k
    u1_cbp_l = (u1_cbp & 0xF);
1730
1731
    /* mb type */
1732
490k
    mb_type = mb_tpm & 0xF;
1733
490k
    u1_is_intra_mb = (mb_type == I16x16) || (mb_type == I8x8) || (mb_type == I4x4);
1734
1735
    /* CABAC contexts for the MB */
1736
490k
    isvce_get_cabac_context(ps_ent_ctxt, mb_type);
1737
490k
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
1738
1739
    /* Starting bitstream offset for header in bits */
1740
490k
    bitstream_start_offset = isvce_get_num_bits(ps_bitstream);
1741
1742
    /* Encode mb_skip_flag */
1743
490k
    isvce_cabac_enc_mb_skip(mb_type == PSKIP, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE);
1744
1745
490k
    if(mb_type == PSKIP)
1746
224k
    {
1747
224k
        ps_curr_ctxt->u1_mb_type = CAB_P_SKIP;
1748
1749
224k
        ps_ent_ctxt->pi4_mb_skip_run[0]++;
1750
1751
224k
        isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, 0, 0, 0, PSKIP);
1752
1753
224k
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1754
224k
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
1755
224k
            bitstream_end_offset - bitstream_start_offset;
1756
1757
224k
        pu1_byte += sizeof(isvce_mb_hdr_pskip_t);
1758
224k
        ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1759
1760
224k
        return IH264E_SUCCESS;
1761
224k
    }
1762
1763
266k
    if(ps_ent_ctxt->u1_spatial_layer_id && ps_svc_slice_header->i1_adaptive_base_mode_flag)
1764
262k
    {
1765
262k
        isvce_cabac_enc_base_mode_flag(ps_cabac_ctxt, u1_base_mode_flag);
1766
262k
    }
1767
1768
266k
    if(!u1_base_mode_flag)
1769
231k
    {
1770
231k
        if(u1_is_intra_mb)
1771
135k
        {
1772
135k
            if(mb_type == I16x16)
1773
128k
            {
1774
128k
                luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u1_cbp_c << 2) + (u1_cbp_l == 15) * 12;
1775
128k
            }
1776
7.76k
            else
1777
7.76k
            {
1778
7.76k
                luma_intra_mode = 0;
1779
7.76k
            }
1780
1781
135k
            isvce_cabac_encode_bin(ps_cabac_ctxt, 1,
1782
135k
                                   ps_cabac_ctxt->au1_cabac_ctxt_table + MB_TYPE_P_SLICE);
1783
1784
135k
            isvce_cabac_enc_intra_mb_type(PSLICE, (UWORD8) luma_intra_mode, ps_cabac_ctxt,
1785
135k
                                          MB_TYPE_P_SLICE);
1786
1787
135k
            if(mb_type == I4x4)
1788
7.76k
            {
1789
7.76k
                isvce_mb_hdr_i4x4_t *ps_mb_hdr_i4x4 =
1790
7.76k
                    (isvce_mb_hdr_i4x4_t *) ps_ent_ctxt->pv_mb_header_data;
1791
1792
7.76k
                isvce_cabac_enc_4x4mb_modes(ps_cabac_ctxt, ps_mb_hdr_i4x4->au1_sub_blk_modes);
1793
7.76k
            }
1794
1795
135k
            chroma_intra_mode = (mb_tpm >> 6);
1796
1797
135k
            isvce_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt);
1798
135k
        }
1799
96.1k
        else
1800
96.1k
        {
1801
96.1k
            UWORD32 u4_ctx_inc_p;
1802
1803
96.1k
            isvce_mb_hdr_p16x16_t *ps_mb_hdr_p16x16 =
1804
96.1k
                (isvce_mb_hdr_p16x16_t *) ps_ent_ctxt->pv_mb_header_data;
1805
1806
96.1k
            WORD16 *pi2_mv_ptr = (WORD16 *) ps_mb_hdr_p16x16->ai2_mvd;
1807
1808
            /* Encoding mb_type as P16x16 */
1809
96.1k
            u4_ctx_inc_p = (0x010 + ((2) << 8));
1810
1811
96.1k
            isvce_encode_decision_bins(0, 3, u4_ctx_inc_p, 3,
1812
96.1k
                                       &(ps_cabac_ctxt->au1_cabac_ctxt_table[MB_TYPE_P_SLICE]),
1813
96.1k
                                       ps_cabac_ctxt);
1814
1815
96.1k
            if(ps_ent_ctxt->u1_spatial_layer_id &&
1816
96.1k
               ps_svc_slice_header->i1_adaptive_motion_prediction_flag)
1817
93.8k
            {
1818
93.8k
                isvce_cabac_enc_motion_prediction_flag(ps_cabac_ctxt, ps_mb_hdr_p16x16->u1_mvp_idx,
1819
93.8k
                                                       1);
1820
93.8k
            }
1821
1822
96.1k
            isvce_cabac_enc_mvds_p16x16(ps_cabac_ctxt, pi2_mv_ptr);
1823
96.1k
        }
1824
231k
    }
1825
1826
266k
    if(ps_ent_ctxt->u1_spatial_layer_id && (u1_base_mode_flag || !u1_is_intra_mb) &&
1827
266k
       ps_svc_slice_header->i1_adaptive_residual_prediction_flag)
1828
128k
    {
1829
128k
        isvce_cabac_enc_residual_prediction_flag(ps_cabac_ctxt, u1_base_mode_flag,
1830
128k
                                                 ps_mb_hdr->u1_residual_prediction_flag);
1831
128k
    }
1832
1833
266k
    if(u1_base_mode_flag || (mb_type != I16x16))
1834
138k
    {
1835
138k
        isvce_cabac_enc_cbp(u1_cbp, ps_cabac_ctxt);
1836
138k
    }
1837
1838
266k
    if((u1_cbp > 0) || (mb_type == I16x16))
1839
256k
    {
1840
256k
        mb_qp_delta =
1841
256k
            ((WORD16) ps_mb_hdr->u1_mb_qp) - ((WORD16) ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp);
1842
1843
256k
        isvce_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt);
1844
256k
        ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp = ps_mb_hdr->u1_mb_qp;
1845
1846
256k
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1847
256k
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
1848
256k
            bitstream_end_offset - bitstream_start_offset;
1849
1850
256k
        bitstream_start_offset = bitstream_end_offset;
1851
1852
256k
        if(mb_type == I16x16)
1853
128k
        {
1854
128k
            ps_curr_ctxt->u1_mb_type = CAB_I16x16;
1855
1856
128k
            isvce_cabac_encode_residue_luma_dc(ps_ent_ctxt);
1857
1858
128k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_AC_CTXCAT);
1859
1860
128k
            pu1_byte += sizeof(isvce_mb_hdr_i16x16_t);
1861
128k
        }
1862
128k
        else if(mb_type == I4x4)
1863
7.49k
        {
1864
7.49k
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
1865
1866
7.49k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1867
1868
7.49k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1869
7.49k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1870
1871
7.49k
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
1872
7.49k
        }
1873
120k
        else if(mb_type == P16x16)
1874
88.4k
        {
1875
88.4k
            ps_curr_ctxt->u1_mb_type = (CAB_P | CAB_NON_BD16x16);
1876
1877
88.4k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1878
1879
88.4k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1880
88.4k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1881
1882
88.4k
            pu1_byte += sizeof(isvce_mb_hdr_p16x16_t);
1883
88.4k
        }
1884
32.1k
        else if(mb_type == BASE_MODE)
1885
32.1k
        {
1886
32.1k
            ps_curr_ctxt->u1_mb_type = (CAB_P | CAB_NON_BD16x16);
1887
1888
32.1k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1889
1890
32.1k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1891
32.1k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1892
1893
32.1k
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
1894
32.1k
        }
1895
1896
256k
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1897
256k
        ps_ent_ctxt->u4_residue_bits[!u1_is_intra_mb] +=
1898
256k
            bitstream_end_offset - bitstream_start_offset;
1899
256k
    }
1900
10.0k
    else
1901
10.0k
    {
1902
10.0k
        mb_qp_delta = 0;
1903
1904
10.0k
        if(mb_type == I16x16)
1905
0
        {
1906
0
            ps_curr_ctxt->u1_mb_type = CAB_I16x16;
1907
1908
0
            pu1_byte += sizeof(isvce_mb_hdr_i16x16_t);
1909
0
        }
1910
10.0k
        else if(mb_type == I4x4)
1911
277
        {
1912
277
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
1913
1914
277
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
1915
277
        }
1916
9.79k
        else if(mb_type == P16x16)
1917
7.67k
        {
1918
7.67k
            ps_curr_ctxt->u1_mb_type = (CAB_P | CAB_NON_BD16x16);
1919
1920
7.67k
            pu1_byte += sizeof(isvce_mb_hdr_p16x16_t);
1921
7.67k
        }
1922
2.12k
        else if(mb_type == BASE_MODE)
1923
2.12k
        {
1924
2.12k
            ps_curr_ctxt->u1_mb_type = (CAB_P | CAB_NON_BD16x16);
1925
1926
2.12k
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
1927
2.12k
        }
1928
1929
10.0k
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1930
10.0k
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
1931
10.0k
            bitstream_end_offset - bitstream_start_offset;
1932
10.0k
    }
1933
1934
266k
    isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, mb_qp_delta, u1_cbp, u1_base_mode_flag,
1935
266k
                         mb_type);
1936
1937
266k
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1938
1939
266k
    return IH264E_SUCCESS;
1940
490k
}
1941
1942
/* ! < Table 9-37 – Binarization for macroblock types in B slices  in
1943
 * ITU_T_H264-201402 Bits 0-7 : binarised value Bits 8-15: length of binary
1944
 * sequence */
1945
1946
static const UWORD32 u4_b_mb_type[27] = {
1947
    0x0100, 0x0301, 0x0305, 0x0603, 0x0623, 0x0613, 0x0633, 0x060b, 0x062b, 0x061b, 0x063b, 0x061f,
1948
    0x0707, 0x0747, 0x0727, 0x0767, 0x0717, 0x0757, 0x0737, 0x0777, 0x070f, 0x074f, 0x063f};
1949
/* CtxInc for mb types in B slices */
1950
static const UWORD32 ui_b_mb_type_ctx_inc[27] = {
1951
    0x00,       0x0530,     0x0530,     0x0555430,  0x0555430,  0x0555430,  0x0555430,  0x0555430,
1952
    0x0555430,  0x0555430,  0x0555430,  0x0555430,  0x05555430, 0x05555430, 0x05555430, 0x05555430,
1953
    0x05555430, 0x05555430, 0x05555430, 0x05555430, 0x05555430, 0x05555430, 0x0555430};
1954
1955
/**
1956
 *******************************************************************************
1957
 *
1958
 * @brief
1959
 *  This function generates CABAC coded bit stream for B slices
1960
 *
1961
 * @description
1962
 *  The mb syntax layer for inter slices constitutes luma mb mode,
1963
 *  mb qp delta, coded block pattern, chroma mb mode and
1964
 *  luma/chroma residue. These syntax elements are written as directed by table
1965
 *  7.3.5 of h264 specification
1966
 *
1967
 * @param[in] ps_ent_ctxt
1968
 *  pointer to entropy context
1969
 *
1970
 * @returns error code
1971
 *
1972
 * @remarks none
1973
 *
1974
 *******************************************************************************
1975
 */
1976
IH264E_ERROR_T isvce_write_bslice_mb_cabac(isvce_entropy_ctxt_t *ps_ent_ctxt)
1977
0
{
1978
0
    isvce_mb_info_ctxt_t *ps_curr_ctxt;
1979
1980
0
    WORD32 mb_tpm, mb_type, chroma_intra_mode, luma_intra_mode;
1981
0
    UWORD8 u1_cbp, u1_cbp_l, u1_cbp_c;
1982
0
    WORD8 mb_qp_delta;
1983
0
    WORD32 bitstream_start_offset, bitstream_end_offset;
1984
0
    UWORD8 u1_base_mode_flag;
1985
0
    UWORD8 u1_is_intra_mb;
1986
1987
0
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1988
0
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
1989
0
    svc_slice_header_t *ps_svc_slice_header =
1990
0
        ps_ent_ctxt->ps_svc_slice_hdr_base +
1991
0
        (ps_ent_ctxt->i4_cur_slice_idx % SVC_MAX_SLICE_HDR_CNT);
1992
0
    isvce_mb_hdr_common_t *ps_mb_hdr = (isvce_mb_hdr_common_t *) ps_ent_ctxt->pv_mb_header_data;
1993
1994
0
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1995
1996
0
    if((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) >= ps_bitstream->u4_max_strm_size)
1997
0
    {
1998
        /* return without corrupting the buffer beyond its size */
1999
0
        return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
2000
0
    }
2001
2002
    /* mb header info */
2003
0
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
2004
0
    u1_base_mode_flag = ps_mb_hdr->u1_base_mode_flag;
2005
0
    u1_cbp = ps_mb_hdr->u1_cbp;
2006
0
    u1_cbp_c = (u1_cbp >> 4);
2007
0
    u1_cbp_l = (u1_cbp & 0xF);
2008
2009
    /* mb type */
2010
0
    mb_type = mb_tpm & 0xF;
2011
0
    u1_is_intra_mb = (mb_type == I16x16) || (mb_type == I8x8) || (mb_type == I4x4);
2012
2013
    /* CABAC contexts for the MB */
2014
0
    isvce_get_cabac_context(ps_ent_ctxt, mb_type);
2015
0
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
2016
2017
    /* Starting bitstream offset for header in bits */
2018
0
    bitstream_start_offset = isvce_get_num_bits(ps_bitstream);
2019
2020
    /* Encode mb_skip_flag */
2021
0
    isvce_cabac_enc_mb_skip(mb_type == BSKIP, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE);
2022
2023
0
    if(mb_type == BSKIP)
2024
0
    {
2025
0
        ps_curr_ctxt->u1_mb_type = CAB_B_SKIP;
2026
2027
0
        ps_ent_ctxt->pi4_mb_skip_run[0]++;
2028
2029
0
        isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, 0, 0, 0, BSKIP);
2030
2031
0
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
2032
0
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
2033
0
            bitstream_end_offset - bitstream_start_offset;
2034
2035
0
        pu1_byte += sizeof(isvce_mb_hdr_bskip_t);
2036
0
        ps_ent_ctxt->pv_mb_header_data = pu1_byte;
2037
2038
0
        return IH264E_SUCCESS;
2039
0
    }
2040
2041
0
    if(ps_ent_ctxt->u1_spatial_layer_id && ps_svc_slice_header->i1_adaptive_base_mode_flag)
2042
0
    {
2043
0
        isvce_cabac_enc_base_mode_flag(ps_cabac_ctxt, u1_base_mode_flag);
2044
0
    }
2045
2046
0
    if(!u1_base_mode_flag)
2047
0
    {
2048
0
        if(u1_is_intra_mb)
2049
0
        {
2050
0
            if(mb_type == I16x16)
2051
0
            {
2052
0
                luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u1_cbp_c << 2) + (u1_cbp_l == 15) * 12;
2053
0
            }
2054
0
            else
2055
0
            {
2056
0
                luma_intra_mode = 0;
2057
0
            }
2058
2059
0
            {
2060
0
                isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
2061
0
                isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
2062
2063
0
                UWORD32 u4_ctx_inc = 0;
2064
2065
0
                if(ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
2066
0
                {
2067
0
                    u4_ctx_inc +=
2068
0
                        ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) != CAB_BD16x16) ? 1 : 0;
2069
0
                }
2070
2071
0
                if(ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
2072
0
                {
2073
0
                    u4_ctx_inc +=
2074
0
                        ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) != CAB_BD16x16) ? 1 : 0;
2075
0
                }
2076
2077
                /* Intra Prefix Only "111101" */
2078
0
                u4_ctx_inc = (u4_ctx_inc | 0x05555430);
2079
0
                isvce_encode_decision_bins(0x2f, 6, u4_ctx_inc, 3,
2080
0
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + MB_TYPE_B_SLICE,
2081
0
                                           ps_cabac_ctxt);
2082
2083
0
                isvce_cabac_enc_intra_mb_type(BSLICE, (UWORD8) luma_intra_mode, ps_cabac_ctxt,
2084
0
                                              MB_TYPE_B_SLICE);
2085
0
            }
2086
2087
0
            if(mb_type == I4x4)
2088
0
            {
2089
0
                isvce_mb_hdr_i4x4_t *ps_mb_hdr_i4x4 =
2090
0
                    (isvce_mb_hdr_i4x4_t *) ps_ent_ctxt->pv_mb_header_data;
2091
2092
0
                isvce_cabac_enc_4x4mb_modes(ps_cabac_ctxt, ps_mb_hdr_i4x4->au1_sub_blk_modes);
2093
0
            }
2094
2095
0
            chroma_intra_mode = (mb_tpm >> 6);
2096
2097
0
            isvce_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt);
2098
0
        }
2099
0
        else if(mb_type == BDIRECT)
2100
0
        {
2101
            /* Encoding mb_type as B_Direct_16x16 */
2102
0
            {
2103
0
                isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
2104
0
                isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
2105
2106
0
                UWORD32 u4_ctx_inc = 0;
2107
2108
0
                if(ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
2109
0
                {
2110
0
                    u4_ctx_inc +=
2111
0
                        ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) != CAB_BD16x16) ? 1 : 0;
2112
0
                }
2113
2114
0
                if(ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
2115
0
                {
2116
0
                    u4_ctx_inc +=
2117
0
                        ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) != CAB_BD16x16) ? 1 : 0;
2118
0
                }
2119
2120
                /* Encode the bin */
2121
0
                isvce_cabac_encode_bin(
2122
0
                    ps_cabac_ctxt, 0,
2123
0
                    ps_cabac_ctxt->au1_cabac_ctxt_table + MB_TYPE_B_SLICE + u4_ctx_inc);
2124
0
            }
2125
0
        }
2126
0
        else
2127
0
        {
2128
0
            WORD32 i;
2129
2130
0
            isvce_mb_hdr_b16x16_t *ps_mb_hdr_b16x16 =
2131
0
                (isvce_mb_hdr_b16x16_t *) ps_ent_ctxt->pv_mb_header_data;
2132
2133
0
            WORD16 *pi2_mv_ptr = (WORD16 *) ps_mb_hdr_b16x16->ai2_mvd;
2134
0
            WORD32 i4_mb_part_pred_mode = (mb_tpm >> 4);
2135
0
            UWORD32 u4_mb_type = mb_type - B16x16 + B_L0_16x16 + i4_mb_part_pred_mode;
2136
2137
            /* Encoding mb_type as B16x16 */
2138
0
            {
2139
0
                isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
2140
0
                isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
2141
0
                UWORD32 u4_ctx_inc = 0;
2142
2143
0
                UWORD32 u4_mb_type_bins = u4_b_mb_type[u4_mb_type];
2144
0
                UWORD32 u4_bin_len = (u4_mb_type_bins >> 8) & 0x0F;
2145
0
                u4_mb_type_bins = u4_mb_type_bins & 0xFF;
2146
2147
0
                if(ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
2148
0
                    u4_ctx_inc +=
2149
0
                        ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) != CAB_BD16x16) ? 1 : 0;
2150
0
                if(ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
2151
0
                    u4_ctx_inc +=
2152
0
                        ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) != CAB_BD16x16) ? 1 : 0;
2153
2154
0
                u4_ctx_inc = u4_ctx_inc | ui_b_mb_type_ctx_inc[u4_mb_type];
2155
2156
0
                isvce_encode_decision_bins(u4_mb_type_bins, u4_bin_len, u4_ctx_inc, u4_bin_len,
2157
0
                                           &(ps_cabac_ctxt->au1_cabac_ctxt_table[MB_TYPE_B_SLICE]),
2158
0
                                           ps_cabac_ctxt);
2159
0
            }
2160
2161
0
            for(i = 0; i < NUM_PRED_DIRS; i++)
2162
0
            {
2163
0
                PRED_MODE_T e_pred_mode = (PRED_MODE_T) i;
2164
0
                PRED_MODE_T e_cmpl_pred_mode = (e_pred_mode == L0) ? L1 : L0;
2165
2166
0
                if(((PRED_MODE_T) i4_mb_part_pred_mode) != e_pred_mode)
2167
0
                {
2168
0
                    if(ps_svc_slice_header->i1_adaptive_motion_prediction_flag &&
2169
0
                       ps_ent_ctxt->u1_spatial_layer_id)
2170
0
                    {
2171
0
                        isvce_cabac_enc_motion_prediction_flag(
2172
0
                            ps_cabac_ctxt, ps_mb_hdr_b16x16->au1_mvp_idx[e_cmpl_pred_mode],
2173
0
                            e_cmpl_pred_mode == L0);
2174
0
                    }
2175
0
                }
2176
0
            }
2177
2178
0
            isvce_cabac_enc_mvds_b16x16(ps_cabac_ctxt, pi2_mv_ptr, i4_mb_part_pred_mode);
2179
0
        }
2180
0
    }
2181
2182
0
    if(ps_svc_slice_header->i1_adaptive_residual_prediction_flag &&
2183
0
       ps_ent_ctxt->u1_spatial_layer_id && (u1_base_mode_flag || !u1_is_intra_mb))
2184
0
    {
2185
0
        isvce_cabac_enc_residual_prediction_flag(ps_cabac_ctxt, u1_base_mode_flag,
2186
0
                                                 ps_mb_hdr->u1_residual_prediction_flag);
2187
0
    }
2188
2189
0
    if(u1_base_mode_flag || (mb_type != I16x16))
2190
0
    {
2191
0
        isvce_cabac_enc_cbp(u1_cbp, ps_cabac_ctxt);
2192
0
    }
2193
2194
0
    if((u1_cbp > 0) || (mb_type == I16x16))
2195
0
    {
2196
0
        mb_qp_delta =
2197
0
            ((WORD16) ps_mb_hdr->u1_mb_qp) - ((WORD16) ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp);
2198
2199
0
        isvce_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt);
2200
0
        ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp = ps_mb_hdr->u1_mb_qp;
2201
2202
0
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
2203
0
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
2204
0
            bitstream_end_offset - bitstream_start_offset;
2205
0
        bitstream_start_offset = bitstream_end_offset;
2206
2207
0
        if(mb_type == I16x16)
2208
0
        {
2209
0
            ps_curr_ctxt->u1_mb_type = CAB_I16x16;
2210
2211
0
            isvce_cabac_encode_residue_luma_dc(ps_ent_ctxt);
2212
2213
0
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_AC_CTXCAT);
2214
2215
0
            pu1_byte += sizeof(isvce_mb_hdr_i16x16_t);
2216
0
        }
2217
0
        else if(mb_type == I4x4)
2218
0
        {
2219
0
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
2220
2221
0
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
2222
2223
0
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
2224
0
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
2225
2226
0
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
2227
0
        }
2228
0
        else if(mb_type == B16x16)
2229
0
        {
2230
0
            ps_curr_ctxt->u1_mb_type = CAB_NON_BD16x16;
2231
2232
0
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
2233
2234
0
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
2235
0
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
2236
2237
0
            pu1_byte += sizeof(isvce_mb_hdr_b16x16_t);
2238
0
        }
2239
0
        else if(mb_type == BDIRECT)
2240
0
        {
2241
0
            ps_curr_ctxt->u1_mb_type = CAB_BD16x16;
2242
2243
0
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
2244
2245
0
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
2246
0
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
2247
2248
0
            pu1_byte += sizeof(isvce_mb_hdr_b16x16_t);
2249
0
        }
2250
0
        else if(mb_type == BASE_MODE)
2251
0
        {
2252
0
            ps_curr_ctxt->u1_mb_type = CAB_NON_BD16x16;
2253
2254
0
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
2255
2256
0
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
2257
0
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
2258
2259
0
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
2260
0
        }
2261
2262
0
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
2263
0
        ps_ent_ctxt->u4_residue_bits[!u1_is_intra_mb] +=
2264
0
            bitstream_end_offset - bitstream_start_offset;
2265
0
    }
2266
0
    else
2267
0
    {
2268
0
        mb_qp_delta = 0;
2269
2270
0
        if(mb_type == I16x16)
2271
0
        {
2272
0
            ps_curr_ctxt->u1_mb_type = CAB_I16x16;
2273
2274
0
            pu1_byte += sizeof(isvce_mb_hdr_i16x16_t);
2275
0
        }
2276
0
        else if(mb_type == I4x4)
2277
0
        {
2278
0
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
2279
2280
0
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
2281
0
        }
2282
0
        else if(mb_type == B16x16)
2283
0
        {
2284
0
            ps_curr_ctxt->u1_mb_type = CAB_NON_BD16x16;
2285
2286
0
            pu1_byte += sizeof(isvce_mb_hdr_b16x16_t);
2287
0
        }
2288
0
        else if(mb_type == BDIRECT)
2289
0
        {
2290
0
            ps_curr_ctxt->u1_mb_type = CAB_BD16x16;
2291
2292
0
            pu1_byte += sizeof(isvce_mb_hdr_b16x16_t);
2293
0
        }
2294
0
        else if(mb_type == BDIRECT)
2295
0
        {
2296
0
            ps_curr_ctxt->u1_mb_type = CAB_NON_BD16x16;
2297
2298
0
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
2299
0
        }
2300
2301
0
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
2302
0
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
2303
0
            bitstream_end_offset - bitstream_start_offset;
2304
0
    }
2305
2306
0
    isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, mb_qp_delta, u1_cbp, u1_base_mode_flag,
2307
0
                         mb_type);
2308
2309
0
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
2310
2311
0
    return IH264E_SUCCESS;
2312
0
}
2313
2314
#if ENABLE_RE_ENC_AS_SKIP
2315
IH264E_ERROR_T isvce_reencode_as_skip_frame_cabac(isvce_entropy_ctxt_t *ps_ent_ctxt)
2316
2.10k
{
2317
2.10k
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
2318
2.10k
    bitstrm_t *ps_bitstrm = ps_ent_ctxt->ps_bitstrm;
2319
2.10k
    bitstrm_t *ps_bitstrm_after_slice_hdr = ps_ent_ctxt->ps_bitstrm_after_slice_hdr;
2320
2321
2.10k
    isvce_mb_info_ctxt_t *ps_curr_ctxt;
2322
2323
2.10k
    slice_header_t *ps_slice_header =
2324
2.10k
        (ps_ent_ctxt->u1_spatial_layer_id == 0)
2325
2.10k
            ? &ps_ent_ctxt->ps_slice_hdr_base[ps_ent_ctxt->i4_cur_slice_idx % SVC_MAX_SLICE_HDR_CNT]
2326
2.10k
            : &ps_ent_ctxt
2327
557
                   ->ps_svc_slice_hdr_base[ps_ent_ctxt->i4_cur_slice_idx % SVC_MAX_SLICE_HDR_CNT]
2328
557
                   .s_slice_header;
2329
2330
    /* total mb cnt */
2331
2.10k
    UWORD32 i4_wd_mbs = ps_ent_ctxt->i4_wd_mbs;
2332
2.10k
    UWORD32 i4_ht_mbs = ps_ent_ctxt->i4_ht_mbs;
2333
2.10k
    UWORD8 i, j;
2334
2335
2.10k
    isvce_init_cabac_ctxt(ps_ent_ctxt, ps_slice_header);
2336
2337
2.10k
    ps_bitstrm->i4_bits_left_in_cw = ps_bitstrm_after_slice_hdr->i4_bits_left_in_cw;
2338
2.10k
    ps_bitstrm->u4_cur_word = ps_bitstrm_after_slice_hdr->u4_cur_word;
2339
2.10k
    ps_bitstrm->u4_strm_buf_offset = ps_bitstrm_after_slice_hdr->u4_strm_buf_offset;
2340
2.10k
    ps_bitstrm->i4_zero_bytes_run = ps_bitstrm_after_slice_hdr->i4_zero_bytes_run;
2341
2342
5.62k
    for(i = 0; i < i4_ht_mbs; i++)
2343
3.52k
    {
2344
44.4k
        for(j = 0; j < i4_wd_mbs; j++)
2345
40.8k
        {
2346
40.8k
            MBTYPES_T mb_type = PSKIP;
2347
2348
40.8k
            ps_ent_ctxt->i4_mb_x = j;
2349
40.8k
            ps_ent_ctxt->i4_mb_y = i;
2350
2351
40.8k
            isvce_get_cabac_context(ps_ent_ctxt, mb_type);
2352
40.8k
            ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
2353
2354
40.8k
            isvce_cabac_enc_mb_skip(mb_type == PSKIP, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE);
2355
2356
40.8k
            if(mb_type == PSKIP)
2357
40.8k
            {
2358
40.8k
                ps_curr_ctxt->u1_mb_type = CAB_P_SKIP;
2359
40.8k
                isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, 0, 0, 0, PSKIP);
2360
40.8k
            }
2361
2362
40.8k
            if(j == i4_wd_mbs - 1 && i == i4_ht_mbs - 1)
2363
2.10k
            {
2364
2.10k
                isvce_cabac_encode_terminate(ps_cabac_ctxt, 1);
2365
2.10k
            }
2366
38.7k
            else
2367
38.7k
            {
2368
38.7k
                isvce_cabac_encode_terminate(ps_cabac_ctxt, 0);
2369
38.7k
            }
2370
40.8k
        }
2371
3.52k
    }
2372
2.10k
    return IH264E_SUCCESS;
2373
2.10k
}
2374
#endif