Coverage Report

Created: 2025-10-13 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavc/encoder/svc/isvce_cabac_encode.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2022 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
 */
20
21
/**
22
*******************************************************************************
23
* @file
24
*  isvce_cabac.c
25
*
26
* @brief
27
*  Contains all 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
519k
{
119
519k
    UWORD8 u4_ctx_inc;
120
519k
    WORD8 a, b;
121
519k
    a = ((ps_cabac_ctxt->ps_left_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ? 0 : 1);
122
519k
    b = ((ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ? 0 : 1);
123
124
519k
    u4_ctx_inc = a + b;
125
    /* Encode the bin */
126
519k
    isvce_cabac_encode_bin(ps_cabac_ctxt, (UWORD32) u1_mb_skip_flag,
127
519k
                           ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctxidx_offset + u4_ctx_inc);
128
519k
}
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.48M
{
181
2.48M
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
182
2.48M
    bin_ctxt_model *pu1_mb_bin_ctxt, *pu1_bin_ctxt;
183
2.48M
    UWORD8 u1_bin;
184
2.48M
    isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
185
2.48M
    isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
186
2.48M
    UWORD32 u4_bins;
187
2.48M
    UWORD32 u4_ctx_inc;
188
2.48M
    WORD8 i1_bins_len;
189
2.48M
    UWORD32 u4_code_int_range;
190
2.48M
    UWORD32 u4_code_int_low;
191
2.48M
    UWORD16 u2_quant_code_int_range;
192
2.48M
    UWORD16 u4_code_int_range_lps;
193
2.48M
    WORD8 i;
194
2.48M
    UWORD8 u1_ctx_inc;
195
2.48M
    UWORD32 u4_table_val;
196
197
2.48M
    pu1_mb_bin_ctxt = ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset;
198
199
2.48M
    u4_bins = u4_mb_type_intra[u4_intra_mb_type];
200
2.48M
    i1_bins_len = (WORD8) ((u4_bins >> 8) & 0x0f);
201
2.48M
    u4_ctx_inc = u4_mb_ctxinc[(u4_slice_type != ISLICE)][u4_intra_mb_type];
202
2.48M
    u1_ctx_inc = 0;
203
2.48M
    if(u4_slice_type == ISLICE)
204
2.32M
    {
205
2.32M
        if(ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
206
2.28M
            u1_ctx_inc += ((ps_left_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0);
207
2.32M
        if(ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
208
2.26M
            u1_ctx_inc += ((ps_top_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0);
209
210
2.32M
        u4_ctx_inc = (u4_ctx_inc | (u1_ctx_inc << ((i1_bins_len - 1) << 2)));
211
2.32M
    }
212
163k
    else
213
163k
    {
214
163k
        pu1_mb_bin_ctxt += 3;
215
163k
        if(u4_slice_type == BSLICE) pu1_mb_bin_ctxt += 2;
216
163k
    }
217
218
2.48M
    u4_code_int_range = ps_cab_enc_env->u4_code_int_range;
219
2.48M
    u4_code_int_low = ps_cab_enc_env->u4_code_int_low;
220
221
16.5M
    for(i = (i1_bins_len - 1); i >= 0; i--)
222
14.0M
    {
223
14.0M
        WORD32 shift;
224
225
14.0M
        u1_ctx_inc = ((u4_ctx_inc >> (i << 2)) & 0x0f);
226
14.0M
        u1_bin = ((u4_bins >> i) & 0x01);
227
        /* Encode the bin */
228
14.0M
        pu1_bin_ctxt = pu1_mb_bin_ctxt + u1_ctx_inc;
229
14.0M
        if(i != (i1_bins_len - 2))
230
11.7M
        {
231
11.7M
            WORD8 i1_mps = !!((*pu1_bin_ctxt) & (0x40));
232
11.7M
            WORD8 i1_state = (*pu1_bin_ctxt) & 0x3F;
233
234
11.7M
            u2_quant_code_int_range = ((u4_code_int_range >> 6) & 0x03);
235
11.7M
            u4_table_val = gau4_isvc_cabac_table[i1_state][u2_quant_code_int_range];
236
11.7M
            u4_code_int_range_lps = u4_table_val & 0xFF;
237
238
11.7M
            u4_code_int_range -= u4_code_int_range_lps;
239
11.7M
            if(u1_bin != i1_mps)
240
418k
            {
241
418k
                u4_code_int_low += u4_code_int_range;
242
418k
                u4_code_int_range = u4_code_int_range_lps;
243
418k
                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
418k
                i1_state = (u4_table_val >> 15) & 0x3F;
250
418k
            }
251
11.3M
            else
252
11.3M
            {
253
11.3M
                i1_state = (u4_table_val >> 8) & 0x3F;
254
11.3M
            }
255
256
11.7M
            (*pu1_bin_ctxt) = (i1_mps << 6) | i1_state;
257
11.7M
        }
258
2.30M
        else
259
2.30M
        {
260
2.30M
            u4_code_int_range -= 2;
261
2.30M
        }
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
14.0M
        GETRANGE(shift, u4_code_int_range);
269
14.0M
        shift = 9 - shift;
270
14.0M
        u4_code_int_low <<= shift;
271
14.0M
        u4_code_int_range <<= shift;
272
273
        /* bits to be inserted in the bitstream */
274
14.0M
        ps_cab_enc_env->u4_bits_gen += shift;
275
14.0M
        ps_cab_enc_env->u4_code_int_range = u4_code_int_range;
276
14.0M
        ps_cab_enc_env->u4_code_int_low = u4_code_int_low;
277
278
        /* generate stream when a byte is ready */
279
14.0M
        if(ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
280
183k
        {
281
183k
            isvce_cabac_put_byte(ps_cabac_ctxt);
282
183k
            u4_code_int_range = ps_cab_enc_env->u4_code_int_range;
283
183k
            u4_code_int_low = ps_cab_enc_env->u4_code_int_low;
284
183k
        }
285
14.0M
    }
286
2.48M
}
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
182k
{
312
182k
    WORD32 i;
313
182k
    WORD8 byte;
314
1.63M
    for(i = 0; i < 16; i += 2)
315
1.45M
    {
316
        /* sub blk idx 1 */
317
1.45M
        byte = pu1_intra_4x4_modes[i >> 1];
318
1.45M
        if(byte & 0x1)
319
1.27M
        {
320
1.27M
            isvce_cabac_encode_bin(
321
1.27M
                ps_cabac_ctxt, 1,
322
1.27M
                ps_cabac_ctxt->au1_cabac_ctxt_table + PREV_INTRA4X4_PRED_MODE_FLAG);
323
1.27M
        }
324
177k
        else
325
177k
        {
326
            /* Binarization is FL and Cmax=7 */
327
177k
            isvce_encode_decision_bins(
328
177k
                byte & 0xF, 4, 0x05554, 4,
329
177k
                ps_cabac_ctxt->au1_cabac_ctxt_table + REM_INTRA4X4_PRED_MODE - 5, ps_cabac_ctxt);
330
177k
        }
331
        /* sub blk idx 2 */
332
1.45M
        byte >>= 4;
333
1.45M
        if(byte & 0x1)
334
1.28M
        {
335
1.28M
            isvce_cabac_encode_bin(
336
1.28M
                ps_cabac_ctxt, 1,
337
1.28M
                ps_cabac_ctxt->au1_cabac_ctxt_table + PREV_INTRA4X4_PRED_MODE_FLAG);
338
1.28M
        }
339
168k
        else
340
168k
        {
341
168k
            isvce_encode_decision_bins(
342
168k
                byte & 0xF, 4, 0x05554, 4,
343
168k
                ps_cabac_ctxt->au1_cabac_ctxt_table + REM_INTRA4X4_PRED_MODE - 5, ps_cabac_ctxt);
344
168k
        }
345
1.45M
    }
346
182k
}
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.48M
{
370
2.48M
    WORD8 i1_temp;
371
2.48M
    isvce_mb_info_ctxt_t *ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
372
2.48M
    isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
373
2.48M
    isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
374
2.48M
    UWORD32 u4_bins = 0;
375
2.48M
    WORD8 i1_bins_len = 1;
376
2.48M
    UWORD32 u4_ctx_inc = 0;
377
2.48M
    UWORD8 a, b;
378
2.48M
    a = ((ps_left_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0);
379
2.48M
    b = ((ps_top_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0);
380
381
    /* Binarization is TU and Cmax=3 */
382
2.48M
    ps_curr_ctxt->u1_intrapred_chroma_mode = u1_chroma_pred_mode;
383
384
2.48M
    u4_ctx_inc = a + b;
385
2.48M
    u4_ctx_inc = (u4_ctx_inc | 0x330);
386
2.48M
    if(u1_chroma_pred_mode)
387
144k
    {
388
144k
        u4_bins = 1;
389
144k
        i1_temp = u1_chroma_pred_mode;
390
144k
        i1_temp--;
391
        /* Put a stream of 1's of length Chromaps_pred_mode_ctxt value */
392
254k
        while(i1_temp)
393
109k
        {
394
109k
            u4_bins = (u4_bins | (1 << i1_bins_len));
395
109k
            i1_bins_len++;
396
109k
            i1_temp--;
397
109k
        }
398
        /* If Chromaps_pred_mode_ctxt < Cmax i.e 3. Terminate put a zero */
399
144k
        if(u1_chroma_pred_mode < 3)
400
140k
        {
401
140k
            i1_bins_len++;
402
140k
        }
403
144k
    }
404
405
2.48M
    isvce_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 3,
406
2.48M
                               ps_cabac_ctxt->au1_cabac_ctxt_table + INTRA_CHROMA_PRED_MODE,
407
2.48M
                               ps_cabac_ctxt);
408
2.48M
}
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.59M
{
431
1.59M
    isvce_mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
432
1.59M
    isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
433
1.59M
    WORD8 i2_cbp_chroma, i, j;
434
1.59M
    UWORD8 u1_ctxt_inc, u1_bin;
435
1.59M
    UWORD8 a, b;
436
1.59M
    UWORD32 u4_ctx_inc;
437
1.59M
    UWORD32 u4_bins;
438
1.59M
    WORD8 i1_bins_len;
439
440
    /* CBP Luma, FL, Cmax = 15, L = 4 */
441
1.59M
    u4_ctx_inc = 0;
442
1.59M
    u4_bins = 0;
443
1.59M
    i1_bins_len = 5;
444
7.98M
    for(i = 0; i < 4; i++)
445
6.38M
    {
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
6.38M
        if(i >> 1)
452
3.19M
        {
453
3.19M
            j = i - 2;
454
            /* Top is available always and it's current MB */
455
3.19M
            b = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1);
456
3.19M
        }
457
3.19M
        else
458
3.19M
        {
459
            /* for blocks whose top reference is in another MB */
460
3.19M
            {
461
3.19M
                j = i + 2;
462
3.19M
                b = ((ps_top_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1;
463
3.19M
            }
464
3.19M
        }
465
466
        /* Check for Left availability */
467
6.38M
        if(i & 0x01)
468
3.19M
        {
469
            /* Left is available always and it's current MB */
470
3.19M
            j = i - 1;
471
3.19M
            a = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1);
472
3.19M
        }
473
3.19M
        else
474
3.19M
        {
475
3.19M
            {
476
3.19M
                j = i + 1;
477
3.19M
                a = ((ps_left_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1;
478
3.19M
            }
479
3.19M
        }
480
6.38M
        u1_ctxt_inc = a + 2 * b;
481
6.38M
        u1_bin = ((u4_cbp >> i) & 0x01);
482
6.38M
        u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i << 2)));
483
6.38M
        u4_bins = (u4_bins | (u1_bin << i));
484
6.38M
    }
485
486
    /* CBP Chroma, TU, Cmax = 2 */
487
1.59M
    i2_cbp_chroma = u4_cbp >> 4;
488
    /* calulate ctxtInc, depending on neighbour availability */
489
1.59M
    a = (ps_left_ctxt->u1_cbp > 15) ? 1 : 0;
490
1.59M
    b = (ps_top_ctxt->u1_cbp > 15) ? 1 : 0;
491
492
1.59M
    u1_ctxt_inc = a + 2 * b;
493
1.59M
    if(i2_cbp_chroma)
494
173k
    {
495
173k
        u4_ctx_inc = u4_ctx_inc | ((4 + u1_ctxt_inc) << 16);
496
173k
        u4_bins = (u4_bins | 0x10);
497
        /* calulate ctxtInc, depending on neighbour availability */
498
173k
        a = (ps_left_ctxt->u1_cbp > 31) ? 1 : 0;
499
173k
        b = (ps_top_ctxt->u1_cbp > 31) ? 1 : 0;
500
173k
        u1_ctxt_inc = a + 2 * b;
501
173k
        u4_ctx_inc = u4_ctx_inc | ((8 + u1_ctxt_inc) << 20);
502
173k
        u4_bins = (u4_bins | (((i2_cbp_chroma >> 1) & 0x01) << i1_bins_len));
503
173k
        i1_bins_len++;
504
173k
    }
505
1.42M
    else
506
1.42M
    {
507
1.42M
        u4_ctx_inc = (u4_ctx_inc | ((4 + u1_ctxt_inc) << 16));
508
1.42M
    }
509
1.59M
    isvce_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 8,
510
1.59M
                               ps_cabac_ctxt->au1_cabac_ctxt_table + CBP_LUMA, ps_cabac_ctxt);
511
1.59M
}
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.66M
{
534
2.66M
    UWORD8 u1_code_num;
535
2.66M
    UWORD8 u1_ctxt_inc;
536
537
2.66M
    UWORD32 u4_bins;
538
2.66M
    WORD8 i1_bins_len;
539
540
    /* Range of ps_mb_qp_delta_ctxt= -26 to +25 inclusive */
541
2.66M
    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.66M
    u1_code_num = 0;
545
2.66M
    if(i1_mb_qp_delta > 0)
546
28.1k
    {
547
28.1k
        u1_code_num = (i1_mb_qp_delta << 1) - 1;
548
28.1k
    }
549
2.64M
    else if(i1_mb_qp_delta < 0)
550
113k
    {
551
113k
        u1_code_num = (ABS(i1_mb_qp_delta)) << 1;
552
113k
    }
553
554
2.66M
    u4_bins = 0;
555
2.66M
    i1_bins_len = 1;
556
557
2.66M
    u1_ctxt_inc = !!ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt;
558
559
2.66M
    if(u1_code_num == 0)
560
2.52M
    {
561
2.52M
        isvce_encode_decision_bins(u4_bins, i1_bins_len, u1_ctxt_inc, 3,
562
2.52M
                                   ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
563
2.52M
                                   ps_cabac_ctxt);
564
2.52M
    }
565
141k
    else
566
141k
    {
567
141k
        u4_bins = 1;
568
141k
        u1_code_num--;
569
570
141k
        if(u1_code_num == 0)
571
10.9k
        {
572
10.9k
            i1_bins_len++;
573
574
10.9k
            isvce_encode_decision_bins(u4_bins, i1_bins_len, u1_ctxt_inc | 0x20, 3,
575
10.9k
                                       ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
576
10.9k
                                       ps_cabac_ctxt);
577
10.9k
        }
578
130k
        else
579
130k
        {
580
130k
            u4_bins = (u4_bins | (1 << i1_bins_len));
581
130k
            i1_bins_len++;
582
130k
            u1_code_num--;
583
584
            /* BinIdx from b2 onwards */
585
130k
            if(u1_code_num < 30)
586
129k
            {
587
                /* maximum i1_bins_len = 31 */
588
658k
                while(u1_code_num)
589
528k
                {
590
528k
                    u4_bins = (u4_bins | (1 << i1_bins_len));
591
528k
                    i1_bins_len++;
592
528k
                    u1_code_num--;
593
528k
                };
594
595
129k
                i1_bins_len++;
596
597
129k
                isvce_encode_decision_bins(u4_bins, i1_bins_len, u1_ctxt_inc | 0x320, 2,
598
129k
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
599
129k
                                           ps_cabac_ctxt);
600
129k
            }
601
1.30k
            else
602
1.30k
            {
603
                /* maximum i1_bins_len = 53 */
604
1.30k
                u4_bins = 0xffffffff;
605
1.30k
                i1_bins_len = 32;
606
1.30k
                u1_code_num -= 30;
607
608
1.30k
                isvce_encode_decision_bins(u4_bins, i1_bins_len, u1_ctxt_inc | 0x320, 2,
609
1.30k
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
610
1.30k
                                           ps_cabac_ctxt);
611
612
1.30k
                u4_bins = 0;
613
1.30k
                i1_bins_len = 0;
614
615
5.27k
                while(u1_code_num)
616
3.96k
                {
617
3.96k
                    u4_bins = (u4_bins | (1 << i1_bins_len));
618
3.96k
                    i1_bins_len++;
619
3.96k
                    u1_code_num--;
620
3.96k
                };
621
622
1.30k
                i1_bins_len++;
623
624
1.30k
                isvce_encode_decision_bins(u4_bins, i1_bins_len, 0x333, 1,
625
1.30k
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
626
1.30k
                                           ps_cabac_ctxt);
627
1.30k
            }
628
130k
        }
629
141k
    }
630
2.66M
}
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
7.02M
{
671
7.02M
    WORD8 i;
672
7.02M
    WORD16 *pi16_coeffs;
673
7.02M
    UWORD32 u4_sig_coeff, u4_bins;
674
7.02M
    UWORD32 u4_ctx_inc;
675
7.02M
    UWORD8 u1_last_sig_coef_index = (31 - CLZ(u2_sig_coeff_map));
676
677
    /* Always put Coded Block Flag as 1 */
678
679
7.02M
    pi16_coeffs = pi2_res_block;
680
7.02M
    {
681
7.02M
        bin_ctxt_model *pu1_bin_ctxt;
682
7.02M
        UWORD8 u1_bin, uc_last;
683
684
7.02M
        i = 0;
685
7.02M
        pu1_bin_ctxt = pu1_ctxt_sig_coeff;
686
7.02M
        u4_sig_coeff = 0;
687
7.02M
        u1_bin = 1;
688
7.02M
        if((u1_last_sig_coef_index))
689
4.87M
        {
690
4.87M
            u1_bin = !!(u2_sig_coeff_map & 01);
691
4.87M
        }
692
7.02M
        uc_last = 1;
693
694
7.02M
        do
695
70.0M
        {
696
            /* Encode Decision */
697
70.0M
            isvce_cabac_encode_bin(ps_cabac_ctxt, u1_bin, pu1_bin_ctxt);
698
699
70.0M
            if(u1_bin & uc_last)
700
28.0M
            {
701
28.0M
                u4_sig_coeff = (u4_sig_coeff | (1 << i));
702
28.0M
                pu1_bin_ctxt = pu1_ctxt_sig_coeff + i + LAST_SIGNIFICANT_COEFF_FLAG_FRAME -
703
28.0M
                               SIGNIFICANT_COEFF_FLAG_FRAME;
704
28.0M
                u1_bin = (i == u1_last_sig_coef_index);
705
28.0M
                uc_last = 0;
706
28.0M
            }
707
41.9M
            else
708
41.9M
            {
709
41.9M
                i = i + 1;
710
41.9M
                pu1_bin_ctxt = pu1_ctxt_sig_coeff + i;
711
41.9M
                u1_bin = (i == u1_last_sig_coef_index);
712
41.9M
                uc_last = 1;
713
41.9M
                if((i != u1_last_sig_coef_index))
714
37.0M
                {
715
37.0M
                    u1_bin = !!((u2_sig_coeff_map >> i) & 01);
716
37.0M
                }
717
41.9M
            }
718
70.0M
        } while(!((i > u1_last_sig_coef_index) || (i > (u1_max_num_coeffs - 1))));
719
7.02M
    }
720
721
    /* Encode coeff_abs_level_minus1 and coeff_sign_flag */
722
7.02M
    {
723
7.02M
        UWORD8 u1_sign;
724
7.02M
        UWORD16 u2_abs_level;
725
7.02M
        UWORD8 u1_abs_level_equal1 = 1, u1_abs_level_gt1 = 0;
726
7.02M
        UWORD8 u1_ctx_inc;
727
7.02M
        UWORD8 u1_coff;
728
7.02M
        WORD16 i2_sufs;
729
7.02M
        WORD8 i1_bins_len;
730
7.02M
        i = u1_last_sig_coef_index;
731
7.02M
        pi16_coeffs = pi2_res_block + u1_nnz - 1;
732
7.02M
        do
733
29.3M
        {
734
29.3M
            {
735
29.3M
                u4_sig_coeff = u4_sig_coeff & ((1 << i) - 1);
736
29.3M
                u4_bins = 0;
737
29.3M
                u4_ctx_inc = 0;
738
29.3M
                i1_bins_len = 1;
739
                /* Encode the AbsLevelMinus1 */
740
29.3M
                u2_abs_level = ABS(*(pi16_coeffs)) - 1;
741
                /* CtxInc for bin0 */
742
29.3M
                u4_ctx_inc = MIN(u1_abs_level_equal1, 4);
743
                /* CtxInc for remaining */
744
29.3M
                u1_ctx_inc = 5 + MIN(u1_abs_level_gt1, 4);
745
29.3M
                u4_ctx_inc = u4_ctx_inc + (u1_ctx_inc << 4);
746
29.3M
                if(u2_abs_level)
747
21.9M
                {
748
21.9M
                    u1_abs_level_gt1++;
749
21.9M
                    u1_abs_level_equal1 = 0;
750
21.9M
                }
751
29.3M
                if(!u1_abs_level_gt1) u1_abs_level_equal1++;
752
753
29.3M
                u1_coff = 14;
754
29.3M
                if(u2_abs_level >= u1_coff)
755
7.96M
                {
756
                    /* Prefix TU i.e string of 14 1's */
757
7.96M
                    u4_bins = 0x3fff;
758
7.96M
                    i1_bins_len = 14;
759
7.96M
                    isvce_encode_decision_bins(
760
7.96M
                        u4_bins, i1_bins_len, u4_ctx_inc, 1,
761
7.96M
                        ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_cat_offset, ps_cabac_ctxt);
762
763
                    /* Suffix, uses EncodeBypass */
764
7.96M
                    i2_sufs = u2_abs_level - u1_coff;
765
766
7.96M
                    u4_bins = isvce_cabac_UEGk0_binarization(i2_sufs, &i1_bins_len);
767
768
7.96M
                    isvce_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, i1_bins_len);
769
7.96M
                }
770
21.3M
                else
771
21.3M
                {
772
                    /* Prefix only */
773
21.3M
                    u4_bins = (1 << u2_abs_level) - 1;
774
21.3M
                    i1_bins_len = u2_abs_level + 1;
775
                    /* Encode Terminating bit */
776
21.3M
                    isvce_encode_decision_bins(
777
21.3M
                        u4_bins, i1_bins_len, u4_ctx_inc, 1,
778
21.3M
                        ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_cat_offset, ps_cabac_ctxt);
779
21.3M
                }
780
29.3M
            }
781
            /* encode coeff_sign_flag[i] */
782
29.3M
            u1_sign = ((*pi16_coeffs) < 0) ? 1 : 0;
783
29.3M
            isvce_cabac_encode_bypass_bin(ps_cabac_ctxt, u1_sign);
784
29.3M
            i = CLZ(u4_sig_coeff);
785
29.3M
            i = 31 - i;
786
29.3M
            pi16_coeffs--;
787
29.3M
        } while(u4_sig_coeff);
788
7.02M
    }
789
7.02M
}
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.30M
{
808
    /* CABAC context */
809
2.30M
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
810
2.30M
    tu_sblk_coeff_data_t *ps_mb_coeff_data;
811
812
    /* packed residue */
813
2.30M
    void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
814
2.30M
    UWORD16 u2_sig_coeff_map;
815
2.30M
    WORD16 *pi2_res_block;
816
2.30M
    UWORD8 u1_nnz;
817
2.30M
    UWORD8 u1_cbf;
818
2.30M
    isvce_mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
819
2.30M
    isvce_mb_info_ctxt_t *p_CurCtxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
820
821
2.30M
    PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz, u2_sig_coeff_map,
822
2.30M
                               pi2_res_block);
823
824
2.30M
    u1_cbf = !!(u1_nnz);
825
826
2.30M
    {
827
2.30M
        UWORD32 u4_ctx_inc;
828
2.30M
        UWORD8 u1_a, u1_b;
829
830
2.30M
        u1_a = ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] & 0x1;
831
2.30M
        u1_b = ps_top_ctxt->u1_yuv_dc_csbp & 0x1;
832
2.30M
        u4_ctx_inc = u1_a + (u1_b << 1);
833
834
2.30M
        isvce_cabac_encode_bin(
835
2.30M
            ps_cabac_ctxt, u1_cbf,
836
2.30M
            ps_cabac_ctxt->au1_cabac_ctxt_table + CBF + (LUMA_DC_CTXCAT << 2) + u4_ctx_inc);
837
2.30M
    }
838
839
    /* Write coded_block_flag */
840
2.30M
    if(u1_cbf)
841
535k
    {
842
535k
        isvce_cabac_write_coeff4x4(pi2_res_block, u1_nnz, 15, u2_sig_coeff_map,
843
535k
                                   COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_0_OFFSET,
844
535k
                                   ps_cabac_ctxt->au1_cabac_ctxt_table +
845
535k
                                       SIGNIFICANT_COEFF_FLAG_FRAME + SIG_COEFF_CTXT_CAT_0_OFFSET,
846
535k
                                   ps_cabac_ctxt);
847
848
535k
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= 0x1;
849
535k
        p_CurCtxt->u1_yuv_dc_csbp |= 0x1;
850
535k
    }
851
1.76M
    else
852
1.76M
    {
853
1.76M
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
854
1.76M
        p_CurCtxt->u1_yuv_dc_csbp &= 0x6;
855
1.76M
    }
856
857
2.30M
    ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
858
2.30M
}
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
272k
{
881
    /* CABAC context */
882
272k
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
883
272k
    tu_sblk_coeff_data_t *ps_mb_coeff_data;
884
    /* packed residue */
885
272k
    void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
886
272k
    UWORD16 u2_sig_coeff_map;
887
272k
    UWORD8 u1_nnz;
888
272k
    isvce_mb_info_ctxt_t *ps_top_ctxt_mb_info, *ps_curr_ctxt;
889
890
272k
    ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_top_ctxt_mb_info;
891
272k
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
892
893
    /********************/
894
    /* Write Chroma DC */
895
    /********************/
896
272k
    {
897
272k
        WORD16 *pi2_res_block;
898
272k
        UWORD8 u1_left_dc_csbp, u1_top_dc_csbp, u1_uv, u1_cbf;
899
900
272k
        u1_left_dc_csbp = (ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0]) >> 1;
901
272k
        u1_top_dc_csbp = (ps_top_ctxt_mb_info->u1_yuv_dc_csbp) >> 1;
902
903
817k
        for(u1_uv = 0; u1_uv < 2; u1_uv++)
904
545k
        {
905
545k
            PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz, u2_sig_coeff_map,
906
545k
                                       pi2_res_block);
907
545k
            u1_cbf = !!(u1_nnz);
908
545k
            {
909
545k
                UWORD8 u1_a, u1_b;
910
545k
                UWORD32 u4_ctx_inc;
911
545k
                u1_a = (u1_left_dc_csbp >> u1_uv) & 0x01;
912
545k
                u1_b = (u1_top_dc_csbp >> u1_uv) & 0x01;
913
545k
                u4_ctx_inc = (u1_a + (u1_b << 1));
914
915
545k
                isvce_cabac_encode_bin(ps_cabac_ctxt, u1_cbf,
916
545k
                                       ps_cabac_ctxt->au1_cabac_ctxt_table + CBF +
917
545k
                                           (CHROMA_DC_CTXCAT << 2) + u4_ctx_inc);
918
545k
            }
919
920
545k
            if(u1_cbf)
921
453k
            {
922
453k
                isvce_cabac_write_coeff4x4(pi2_res_block, u1_nnz, 3, u2_sig_coeff_map,
923
453k
                                           COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_3_OFFSET,
924
453k
                                           ps_cabac_ctxt->au1_cabac_ctxt_table +
925
453k
                                               SIGNIFICANT_COEFF_FLAG_FRAME +
926
453k
                                               SIG_COEFF_CTXT_CAT_3_OFFSET,
927
453k
                                           ps_cabac_ctxt);
928
929
453k
                SETBIT(u1_top_dc_csbp, u1_uv);
930
453k
                SETBIT(u1_left_dc_csbp, u1_uv);
931
453k
            }
932
91.5k
            else
933
91.5k
            {
934
91.5k
                CLEARBIT(u1_top_dc_csbp, u1_uv);
935
91.5k
                CLEARBIT(u1_left_dc_csbp, u1_uv);
936
91.5k
            }
937
545k
        }
938
        /*************************************************************/
939
        /*      Update the DC csbp                                   */
940
        /*************************************************************/
941
272k
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x1;
942
272k
        ps_curr_ctxt->u1_yuv_dc_csbp &= 0x1;
943
272k
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= (u1_left_dc_csbp << 1);
944
272k
        ps_curr_ctxt->u1_yuv_dc_csbp |= (u1_top_dc_csbp << 1);
945
272k
    }
946
    /*******************/
947
    /* Write Chroma AC */
948
    /*******************/
949
272k
    {
950
272k
        if(u1_chroma_cbp == 2)
951
223k
        {
952
223k
            UWORD8 u1_uv_blkno, u1_left_ac_csbp, u1_top_ac_csbp;
953
223k
            WORD16 *pi2_res_block;
954
223k
            u1_left_ac_csbp = ps_cabac_ctxt->pu1_left_uv_ac_csbp[0];
955
223k
            u1_top_ac_csbp = ps_top_ctxt_mb_info->u1_yuv_ac_csbp >> 4;
956
957
2.01M
            for(u1_uv_blkno = 0; u1_uv_blkno < 8; u1_uv_blkno++)
958
1.78M
            {
959
1.78M
                UWORD8 u1_cbf;
960
1.78M
                UWORD8 u1_b2b0, u1_b2b1;
961
1.78M
                PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz,
962
1.78M
                                           u2_sig_coeff_map, pi2_res_block);
963
964
1.78M
                u1_cbf = !!(u1_nnz);
965
1.78M
                u1_b2b0 = ((u1_uv_blkno & 0x4) >> 1) | (u1_uv_blkno & 0x1);
966
1.78M
                u1_b2b1 = ((u1_uv_blkno & 0x4) >> 1) | ((u1_uv_blkno & 0x2) >> 1);
967
968
1.78M
                {
969
1.78M
                    UWORD8 u1_a, u1_b;
970
1.78M
                    UWORD32 u4_ctx_inc;
971
                    /* write coded_block_flag */
972
1.78M
                    u1_a = (u1_left_ac_csbp >> u1_b2b1) & 0x1;
973
1.78M
                    u1_b = (u1_top_ac_csbp >> u1_b2b0) & 0x1;
974
1.78M
                    u4_ctx_inc = u1_a + (u1_b << 1);
975
976
1.78M
                    isvce_cabac_encode_bin(ps_cabac_ctxt, u1_cbf,
977
1.78M
                                           ps_cabac_ctxt->au1_cabac_ctxt_table + CBF +
978
1.78M
                                               (CHROMA_AC_CTXCAT << 2) + u4_ctx_inc);
979
1.78M
                }
980
1.78M
                if(u1_cbf)
981
1.27M
                {
982
1.27M
                    isvce_cabac_write_coeff4x4(
983
1.27M
                        pi2_res_block, u1_nnz, 14, u2_sig_coeff_map,
984
1.27M
                        COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_4_OFFSET,
985
1.27M
                        ps_cabac_ctxt->au1_cabac_ctxt_table + +SIGNIFICANT_COEFF_FLAG_FRAME +
986
1.27M
                            SIG_COEFF_CTXT_CAT_4_OFFSET,
987
1.27M
                        ps_cabac_ctxt);
988
989
1.27M
                    SETBIT(u1_left_ac_csbp, u1_b2b1);
990
1.27M
                    SETBIT(u1_top_ac_csbp, u1_b2b0);
991
1.27M
                }
992
514k
                else
993
514k
                {
994
514k
                    CLEARBIT(u1_left_ac_csbp, u1_b2b1);
995
514k
                    CLEARBIT(u1_top_ac_csbp, u1_b2b0);
996
514k
                }
997
1.78M
            }
998
            /*************************************************************/
999
            /*      Update the AC csbp                                   */
1000
            /*************************************************************/
1001
223k
            ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = u1_left_ac_csbp;
1002
223k
            ps_curr_ctxt->u1_yuv_ac_csbp &= 0x0f;
1003
223k
            ps_curr_ctxt->u1_yuv_ac_csbp |= (u1_top_ac_csbp << 4);
1004
223k
        }
1005
49.3k
        else
1006
49.3k
        {
1007
49.3k
            ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0;
1008
49.3k
            ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf;
1009
49.3k
        }
1010
272k
    }
1011
272k
    ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
1012
272k
}
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.66M
{
1038
    /* CABAC context */
1039
2.66M
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
1040
1041
2.66M
    tu_sblk_coeff_data_t *ps_mb_coeff_data;
1042
    /* packed residue */
1043
2.66M
    void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
1044
2.66M
    UWORD16 u2_sig_coeff_map;
1045
2.66M
    UWORD8 u1_nnz;
1046
2.66M
    isvce_mb_info_ctxt_t *ps_curr_ctxt;
1047
2.66M
    isvce_mb_info_ctxt_t *ps_top_ctxt;
1048
2.66M
    UWORD8 u1_left_ac_csbp;
1049
2.66M
    UWORD8 u1_top_ac_csbp;
1050
2.66M
    UWORD32 u4_ctx_idx_offset_sig_coef, u4_ctx_idx_offset_abs_lvl;
1051
2.66M
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
1052
2.66M
    ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
1053
2.66M
    u1_left_ac_csbp = ps_cabac_ctxt->pu1_left_y_ac_csbp[0];
1054
2.66M
    u1_top_ac_csbp = ps_top_ctxt->u1_yuv_ac_csbp;
1055
1056
2.66M
    if(u4_cbp & 0xf)
1057
435k
    {
1058
        /*  Write luma residue  */
1059
435k
        UWORD8 u1_offset;
1060
435k
        WORD16 *pi2_res_block;
1061
435k
        UWORD8 u1_subblk_num;
1062
435k
        if(u1_ctx_cat == LUMA_AC_CTXCAT)
1063
152k
        {
1064
152k
            u1_offset = 1;
1065
152k
            u4_ctx_idx_offset_sig_coef = SIG_COEFF_CTXT_CAT_1_OFFSET;
1066
152k
            u4_ctx_idx_offset_abs_lvl = COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_1_OFFSET;
1067
152k
        }
1068
283k
        else
1069
283k
        {
1070
283k
            u1_offset = 0;
1071
283k
            u4_ctx_idx_offset_sig_coef = SIG_COEFF_CTXT_CAT_2_OFFSET;
1072
283k
            u4_ctx_idx_offset_abs_lvl = COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_2_OFFSET;
1073
283k
        }
1074
1075
6.89M
        for(u1_subblk_num = 0; u1_subblk_num < 16; u1_subblk_num++)
1076
6.45M
        {
1077
6.45M
            UWORD8 u1_b0, u1_b1, u1_b2, u1_b3, u1_b2b0, u1_b3b1, u1_b3b2;
1078
6.45M
            u1_b0 = (u1_subblk_num & 0x1);
1079
6.45M
            u1_b1 = (u1_subblk_num & 0x2) >> 1;
1080
6.45M
            u1_b2 = (u1_subblk_num & 0x4) >> 2;
1081
6.45M
            u1_b3 = (u1_subblk_num & 0x8) >> 3;
1082
6.45M
            u1_b2b0 = (u1_b2 << 1) | (u1_b0);
1083
6.45M
            u1_b3b1 = (u1_b3 << 1) | (u1_b1);
1084
6.45M
            u1_b3b2 = (u1_b3 << 1) | (u1_b2);
1085
1086
6.45M
            if(!((u4_cbp >> u1_b3b2) & 0x1))
1087
172k
            {
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
172k
                CLEARBIT(u1_top_ac_csbp, u1_b2b0);
1093
172k
                CLEARBIT(u1_top_ac_csbp, (u1_b2b0 + 1));
1094
172k
                CLEARBIT(u1_left_ac_csbp, u1_b3b1);
1095
172k
                CLEARBIT(u1_left_ac_csbp, (u1_b3b1 + 1));
1096
1097
172k
                u1_subblk_num += 3;
1098
172k
            }
1099
6.28M
            else
1100
6.28M
            {
1101
6.28M
                UWORD8 u1_csbf;
1102
1103
6.28M
                PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz,
1104
6.28M
                                           u2_sig_coeff_map, pi2_res_block);
1105
1106
6.28M
                u1_csbf = !!(u1_nnz);
1107
6.28M
                {
1108
6.28M
                    UWORD8 u1_a, u1_b;
1109
6.28M
                    UWORD32 u4_ctx_inc;
1110
6.28M
                    u1_b = (u1_top_ac_csbp >> u1_b2b0) & 0x01;
1111
6.28M
                    u1_a = (u1_left_ac_csbp >> u1_b3b1) & 0x01;
1112
6.28M
                    u4_ctx_inc = u1_a + (u1_b << 1);
1113
1114
                    /* Encode the bin */
1115
6.28M
                    isvce_cabac_encode_bin(
1116
6.28M
                        ps_cabac_ctxt, u1_csbf,
1117
6.28M
                        ps_cabac_ctxt->au1_cabac_ctxt_table + CBF + (u1_ctx_cat << 2) + u4_ctx_inc);
1118
6.28M
                }
1119
                /**************************/
1120
                /* Write coded_block_flag */
1121
                /**************************/
1122
6.28M
                if(u1_csbf)
1123
4.75M
                {
1124
4.75M
                    isvce_cabac_write_coeff4x4(pi2_res_block, u1_nnz, (UWORD8) (15 - u1_offset),
1125
4.75M
                                               u2_sig_coeff_map, u4_ctx_idx_offset_abs_lvl,
1126
4.75M
                                               ps_cabac_ctxt->au1_cabac_ctxt_table +
1127
4.75M
                                                   SIGNIFICANT_COEFF_FLAG_FRAME +
1128
4.75M
                                                   u4_ctx_idx_offset_sig_coef,
1129
4.75M
                                               ps_cabac_ctxt);
1130
1131
4.75M
                    SETBIT(u1_top_ac_csbp, u1_b2b0);
1132
4.75M
                    SETBIT(u1_left_ac_csbp, u1_b3b1);
1133
4.75M
                }
1134
1.52M
                else
1135
1.52M
                {
1136
1.52M
                    CLEARBIT(u1_top_ac_csbp, u1_b2b0);
1137
1.52M
                    CLEARBIT(u1_left_ac_csbp, u1_b3b1);
1138
1.52M
                }
1139
6.28M
            }
1140
6.45M
        }
1141
        /**************************************************************************/
1142
        /*                   Update the AC csbp                                   */
1143
        /**************************************************************************/
1144
435k
        ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = u1_left_ac_csbp & 0xf;
1145
435k
        u1_top_ac_csbp &= 0x0f;
1146
435k
        ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf0;
1147
435k
        ps_curr_ctxt->u1_yuv_ac_csbp |= u1_top_ac_csbp;
1148
435k
    }
1149
2.23M
    else
1150
2.23M
    {
1151
2.23M
        ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = 0;
1152
2.23M
        ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf0;
1153
2.23M
    }
1154
1155
    /*     Write chroma residue */
1156
1157
2.66M
    ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
1158
2.66M
    {
1159
2.66M
        UWORD8 u1_cbp_chroma;
1160
2.66M
        u1_cbp_chroma = u4_cbp >> 4;
1161
2.66M
        if(u1_cbp_chroma)
1162
272k
        {
1163
272k
            isvce_cabac_write_chroma_residue(ps_ent_ctxt, u1_cbp_chroma);
1164
272k
        }
1165
2.39M
        else
1166
2.39M
        {
1167
2.39M
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x1;
1168
2.39M
            ps_curr_ctxt->u1_yuv_dc_csbp &= 0x1;
1169
2.39M
            ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0;
1170
2.39M
            ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf;
1171
2.39M
        }
1172
2.66M
    }
1173
2.66M
}
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
238k
{
1202
238k
    UWORD8 u1_bin, u1_ctxt_inc;
1203
238k
    WORD8 k = 3, u1_coff = 9;
1204
238k
    WORD16 i2_abs_mvd, i2_sufs;
1205
238k
    UWORD32 u4_ctx_inc;
1206
238k
    UWORD32 u4_bins;
1207
238k
    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
238k
    if(ui2_abs_mvd < 3)
1220
183k
        u4_ctx_inc = 0;
1221
55.3k
    else if(ui2_abs_mvd > 32)
1222
14.5k
        u4_ctx_inc = 2;
1223
40.7k
    else
1224
40.7k
        u4_ctx_inc = 1;
1225
1226
238k
    u4_bins = 0;
1227
238k
    i1_bins_len = 1;
1228
1229
238k
    if(u1_mvd == 0)
1230
163k
    {
1231
163k
        isvce_cabac_encode_bin(
1232
163k
            ps_cabac_ctxt, 0, ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset + u4_ctx_inc);
1233
163k
    }
1234
75.1k
    else
1235
75.1k
    {
1236
75.1k
        i2_abs_mvd = ABS(u1_mvd);
1237
75.1k
        if(i2_abs_mvd >= u1_coff)
1238
31.7k
        {
1239
            /* Prefix TU i.e string of 9 1's */
1240
31.7k
            u4_bins = 0x1ff;
1241
31.7k
            i1_bins_len = 9;
1242
31.7k
            u4_ctx_inc = (u4_ctx_inc | 0x065430);
1243
1244
31.7k
            isvce_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 4,
1245
31.7k
                                       ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset,
1246
31.7k
                                       ps_cabac_ctxt);
1247
1248
            /* Suffix, uses EncodeBypass */
1249
31.7k
            u4_bins = 0;
1250
31.7k
            i1_bins_len = 0;
1251
31.7k
            i2_sufs = i2_abs_mvd - u1_coff;
1252
78.3k
            while(1)
1253
78.3k
            {
1254
78.3k
                if(i2_sufs >= (1 << k))
1255
46.6k
                {
1256
46.6k
                    u4_bins = (u4_bins | (1 << (31 - i1_bins_len)));
1257
46.6k
                    i1_bins_len++;
1258
46.6k
                    i2_sufs = i2_sufs - (1 << k);
1259
46.6k
                    k++;
1260
46.6k
                }
1261
31.7k
                else
1262
31.7k
                {
1263
31.7k
                    i1_bins_len++;
1264
173k
                    while(k--)
1265
141k
                    {
1266
141k
                        u1_bin = ((i2_sufs >> k) & 0x01);
1267
141k
                        u4_bins = (u4_bins | (u1_bin << (31 - i1_bins_len)));
1268
141k
                        i1_bins_len++;
1269
141k
                    }
1270
31.7k
                    break;
1271
31.7k
                }
1272
78.3k
            }
1273
31.7k
            u4_bins >>= (32 - i1_bins_len);
1274
31.7k
            isvce_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, i1_bins_len);
1275
31.7k
        }
1276
43.4k
        else
1277
43.4k
        {
1278
            /* Prefix only */
1279
            /* b0 */
1280
43.4k
            u4_bins = 1;
1281
43.4k
            i2_abs_mvd--;
1282
43.4k
            u1_ctxt_inc = 3;
1283
171k
            while(i2_abs_mvd)
1284
128k
            {
1285
128k
                i2_abs_mvd--;
1286
128k
                u4_bins = (u4_bins | (1 << i1_bins_len));
1287
128k
                if(u1_ctxt_inc <= 6)
1288
103k
                {
1289
103k
                    u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i1_bins_len << 2)));
1290
103k
                    u1_ctxt_inc++;
1291
103k
                }
1292
128k
                i1_bins_len++;
1293
128k
            }
1294
            /* Encode Terminating bit */
1295
43.4k
            if(i1_bins_len <= 4) u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i1_bins_len << 2)));
1296
43.4k
            i1_bins_len++;
1297
43.4k
            isvce_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 4,
1298
43.4k
                                       ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset,
1299
43.4k
                                       ps_cabac_ctxt);
1300
43.4k
        }
1301
        /* sign bit, uses EncodeBypass */
1302
75.1k
        if(u1_mvd > 0)
1303
36.7k
            isvce_cabac_encode_bypass_bin(ps_cabac_ctxt, 0);
1304
38.3k
        else
1305
38.3k
            isvce_cabac_encode_bypass_bin(ps_cabac_ctxt, 1);
1306
75.1k
    }
1307
238k
}
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
119k
{
1329
119k
    UWORD8 u1_abs_mvd_x, u1_abs_mvd_y;
1330
119k
    UWORD8 *pu1_top_mv_ctxt, *pu1_lft_mv_ctxt;
1331
119k
    WORD16 u2_mv;
1332
119k
    u1_abs_mvd_x = 0;
1333
119k
    u1_abs_mvd_y = 0;
1334
119k
    pu1_top_mv_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv[0];
1335
119k
    pu1_lft_mv_ctxt = ps_cabac_ctxt->pu1_left_mv_ctxt_inc[0];
1336
119k
    {
1337
119k
        UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, u2_abs_mvd_y_b;
1338
119k
        u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[0];
1339
119k
        u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[1];
1340
119k
        u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[0];
1341
119k
        u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[1];
1342
119k
        u2_mv = *(pi2_mv_ptr++);
1343
1344
119k
        isvce_cabac_enc_ctx_mvd(u2_mv, MVD_X, (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b),
1345
119k
                                ps_cabac_ctxt);
1346
1347
119k
        u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv));
1348
119k
        u2_mv = *(pi2_mv_ptr++);
1349
1350
119k
        isvce_cabac_enc_ctx_mvd(u2_mv, MVD_Y, (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b),
1351
119k
                                ps_cabac_ctxt);
1352
1353
119k
        u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv));
1354
119k
    }
1355
    /***************************************************************/
1356
    /* Store abs_mvd_values cabac contexts                         */
1357
    /***************************************************************/
1358
119k
    pu1_top_mv_ctxt[0] = pu1_lft_mv_ctxt[0] = u1_abs_mvd_x;
1359
119k
    pu1_top_mv_ctxt[1] = pu1_lft_mv_ctxt[1] = u1_abs_mvd_y;
1360
119k
}
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.10M
{
1456
4.10M
    UWORD8 u1_is_intra_mb = (e_mb_type == I16x16) || (e_mb_type == I8x8) || (e_mb_type == I4x4);
1457
4.10M
    UWORD8 u1_is_skip_mb = (e_mb_type == PSKIP) || (e_mb_type == BSKIP);
1458
4.10M
    UWORD8 u1_is_direct_mb = (e_mb_type == BDIRECT);
1459
1460
4.10M
    ps_curr_ctxt->u1_cbp = u1_cbp;
1461
4.10M
    ps_curr_ctxt->u1_base_mode_flag = u1_base_mode_flag;
1462
1463
4.10M
    if(u1_is_intra_mb || u1_is_skip_mb || u1_is_direct_mb || u1_base_mode_flag)
1464
3.99M
    {
1465
3.99M
        memset(ps_curr_ctxt->u1_mv, 0, 16);
1466
3.99M
        memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16);
1467
3.99M
    }
1468
1469
4.10M
    if((0 == u1_cbp) && (e_mb_type != I16x16))
1470
1.44M
    {
1471
1.44M
        ps_curr_ctxt->u1_yuv_ac_csbp = 0;
1472
1.44M
        ps_curr_ctxt->u1_yuv_dc_csbp = 0;
1473
1474
1.44M
        ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0;
1475
1.44M
        ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = 0;
1476
1.44M
        ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] = 0;
1477
1.44M
    }
1478
1479
4.10M
    if(u1_is_skip_mb)
1480
209k
    {
1481
209k
        ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 0;
1482
209k
    }
1483
3.90M
    else if((I16x16 != e_mb_type) && (0 == u1_cbp))
1484
1.23M
    {
1485
1.23M
        ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 0;
1486
1.23M
    }
1487
2.66M
    else if(0 == i1_mb_qp_delta)
1488
2.52M
    {
1489
2.52M
        ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 0;
1490
2.52M
    }
1491
141k
    else
1492
141k
    {
1493
141k
        ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = 1;
1494
141k
    }
1495
1496
4.10M
    if(!u1_is_intra_mb || u1_base_mode_flag)
1497
1.62M
    {
1498
1.62M
        ps_curr_ctxt->u1_intrapred_chroma_mode = 0;
1499
1.62M
    }
1500
4.10M
}
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.59M
{
1524
3.59M
    isvce_mb_info_ctxt_t *ps_curr_ctxt;
1525
1526
3.59M
    WORD32 mb_tpm, mb_type, chroma_intra_mode, luma_intra_mode;
1527
3.59M
    UWORD8 u1_cbp, u1_cbp_l, u1_cbp_c;
1528
3.59M
    WORD8 mb_qp_delta;
1529
3.59M
    WORD32 bitstream_start_offset, bitstream_end_offset;
1530
3.59M
    UWORD8 u1_base_mode_flag;
1531
1532
3.59M
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1533
3.59M
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
1534
3.59M
    svc_slice_header_t *ps_svc_slice_header =
1535
3.59M
        ps_ent_ctxt->ps_svc_slice_hdr_base +
1536
3.59M
        (ps_ent_ctxt->i4_cur_slice_idx % SVC_MAX_SLICE_HDR_CNT);
1537
3.59M
    isvce_mb_hdr_common_t *ps_mb_hdr = (isvce_mb_hdr_common_t *) ps_ent_ctxt->pv_mb_header_data;
1538
1539
3.59M
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1540
1541
3.59M
    if((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) >= ps_bitstream->u4_max_strm_size)
1542
156
    {
1543
        /* return without corrupting the buffer beyond its size */
1544
156
        return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
1545
156
    }
1546
1547
3.59M
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
1548
3.59M
    u1_base_mode_flag = ps_mb_hdr->u1_base_mode_flag;
1549
3.59M
    u1_cbp = ps_mb_hdr->u1_cbp;
1550
3.59M
    u1_cbp_c = (u1_cbp >> 4);
1551
3.59M
    u1_cbp_l = (u1_cbp & 0xF);
1552
3.59M
    mb_type = mb_tpm & 0xF;
1553
1554
3.59M
    isvce_get_cabac_context(ps_ent_ctxt, mb_type);
1555
3.59M
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
1556
1557
3.59M
    bitstream_start_offset = isvce_get_num_bits(ps_bitstream);
1558
1559
3.59M
    if(mb_type == I16x16)
1560
2.14M
    {
1561
2.14M
        luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u1_cbp_c << 2) + (u1_cbp_l == 15) * 12;
1562
2.14M
    }
1563
1.44M
    else
1564
1.44M
    {
1565
1.44M
        luma_intra_mode = 0;
1566
1.44M
    }
1567
1568
3.59M
    chroma_intra_mode = (mb_tpm >> 6);
1569
1570
3.59M
    if(ps_ent_ctxt->u1_spatial_layer_id && ps_svc_slice_header->i1_adaptive_base_mode_flag)
1571
3.09M
    {
1572
3.09M
        isvce_cabac_enc_base_mode_flag(ps_cabac_ctxt, u1_base_mode_flag);
1573
3.09M
    }
1574
1575
3.59M
    if(!u1_base_mode_flag)
1576
2.32M
    {
1577
2.32M
        isvce_cabac_enc_intra_mb_type(ISLICE, luma_intra_mode, ps_cabac_ctxt, MB_TYPE_I_SLICE);
1578
1579
2.32M
        if(mb_type == I4x4)
1580
172k
        {
1581
172k
            isvce_mb_hdr_i4x4_t *ps_mb_hdr_i4x4 =
1582
172k
                (isvce_mb_hdr_i4x4_t *) ps_ent_ctxt->pv_mb_header_data;
1583
1584
172k
            isvce_cabac_enc_4x4mb_modes(ps_cabac_ctxt, ps_mb_hdr_i4x4->au1_sub_blk_modes);
1585
172k
        }
1586
1587
2.32M
        isvce_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt);
1588
2.32M
    }
1589
1590
3.59M
    if(u1_base_mode_flag || (mb_type != I16x16))
1591
1.44M
    {
1592
1.44M
        isvce_cabac_enc_cbp(u1_cbp, ps_cabac_ctxt);
1593
1.44M
    }
1594
1595
3.59M
    if((u1_cbp > 0) || (mb_type == I16x16))
1596
2.36M
    {
1597
2.36M
        mb_qp_delta =
1598
2.36M
            ((WORD16) ps_mb_hdr->u1_mb_qp) - ((WORD16) ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp);
1599
1600
2.36M
        isvce_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt);
1601
2.36M
        ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp = ps_mb_hdr->u1_mb_qp;
1602
1603
2.36M
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1604
2.36M
        ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset - bitstream_start_offset;
1605
2.36M
        bitstream_start_offset = bitstream_end_offset;
1606
1607
2.36M
        if(mb_type == I16x16)
1608
2.14M
        {
1609
2.14M
            ps_curr_ctxt->u1_mb_type = CAB_I16x16;
1610
1611
2.14M
            isvce_cabac_encode_residue_luma_dc(ps_ent_ctxt);
1612
1613
2.14M
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_AC_CTXCAT);
1614
1615
2.14M
            pu1_byte += sizeof(isvce_mb_hdr_i16x16_t);
1616
2.14M
        }
1617
220k
        else if(mb_type == I4x4)
1618
61.5k
        {
1619
61.5k
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
1620
1621
61.5k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1622
1623
61.5k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1624
61.5k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1625
1626
61.5k
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
1627
61.5k
        }
1628
159k
        else if(mb_type == BASE_MODE)
1629
159k
        {
1630
159k
            ps_curr_ctxt->u1_mb_type = CAB_P | CAB_NON_BD16x16;
1631
1632
159k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1633
1634
159k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1635
159k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1636
1637
159k
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
1638
159k
        }
1639
1640
2.36M
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1641
2.36M
        ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset - bitstream_start_offset;
1642
2.36M
    }
1643
1.22M
    else
1644
1.22M
    {
1645
1.22M
        mb_qp_delta = 0;
1646
1647
1.22M
        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.22M
        else if(mb_type == I4x4)
1654
110k
        {
1655
110k
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
1656
1657
110k
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
1658
110k
        }
1659
1.11M
        else if(mb_type == BASE_MODE)
1660
1.11M
        {
1661
1.11M
            ps_curr_ctxt->u1_mb_type = CAB_P | CAB_NON_BD16x16;
1662
1663
1.11M
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
1664
1.11M
        }
1665
1666
1.22M
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1667
1.22M
        ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset - bitstream_start_offset;
1668
1.22M
    }
1669
1670
3.59M
    isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, mb_qp_delta, u1_cbp, u1_base_mode_flag,
1671
3.59M
                         mb_type);
1672
1673
3.59M
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1674
1675
3.59M
    return IH264E_SUCCESS;
1676
3.59M
}
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
469k
{
1700
469k
    isvce_mb_info_ctxt_t *ps_curr_ctxt;
1701
1702
469k
    WORD32 mb_tpm, mb_type, chroma_intra_mode, luma_intra_mode;
1703
469k
    UWORD8 u1_cbp, u1_cbp_l, u1_cbp_c;
1704
469k
    WORD8 mb_qp_delta;
1705
469k
    WORD32 bitstream_start_offset, bitstream_end_offset;
1706
469k
    UWORD8 u1_base_mode_flag;
1707
469k
    UWORD8 u1_is_intra_mb;
1708
1709
469k
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1710
469k
    isvce_cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
1711
469k
    svc_slice_header_t *ps_svc_slice_header =
1712
469k
        ps_ent_ctxt->ps_svc_slice_hdr_base +
1713
469k
        (ps_ent_ctxt->i4_cur_slice_idx % SVC_MAX_SLICE_HDR_CNT);
1714
469k
    isvce_mb_hdr_common_t *ps_mb_hdr = (isvce_mb_hdr_common_t *) ps_ent_ctxt->pv_mb_header_data;
1715
1716
469k
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1717
1718
469k
    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
469k
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
1726
469k
    u1_base_mode_flag = ps_mb_hdr->u1_base_mode_flag;
1727
469k
    u1_cbp = ps_mb_hdr->u1_cbp;
1728
469k
    u1_cbp_c = (u1_cbp >> 4);
1729
469k
    u1_cbp_l = (u1_cbp & 0xF);
1730
1731
    /* mb type */
1732
469k
    mb_type = mb_tpm & 0xF;
1733
469k
    u1_is_intra_mb = (mb_type == I16x16) || (mb_type == I8x8) || (mb_type == I4x4);
1734
1735
    /* CABAC contexts for the MB */
1736
469k
    isvce_get_cabac_context(ps_ent_ctxt, mb_type);
1737
469k
    ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
1738
1739
    /* Starting bitstream offset for header in bits */
1740
469k
    bitstream_start_offset = isvce_get_num_bits(ps_bitstream);
1741
1742
    /* Encode mb_skip_flag */
1743
469k
    isvce_cabac_enc_mb_skip(mb_type == PSKIP, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE);
1744
1745
469k
    if(mb_type == PSKIP)
1746
159k
    {
1747
159k
        ps_curr_ctxt->u1_mb_type = CAB_P_SKIP;
1748
1749
159k
        ps_ent_ctxt->pi4_mb_skip_run[0]++;
1750
1751
159k
        isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, 0, 0, 0, PSKIP);
1752
1753
159k
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1754
159k
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
1755
159k
            bitstream_end_offset - bitstream_start_offset;
1756
1757
159k
        pu1_byte += sizeof(isvce_mb_hdr_pskip_t);
1758
159k
        ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1759
1760
159k
        return IH264E_SUCCESS;
1761
159k
    }
1762
1763
309k
    if(ps_ent_ctxt->u1_spatial_layer_id && ps_svc_slice_header->i1_adaptive_base_mode_flag)
1764
303k
    {
1765
303k
        isvce_cabac_enc_base_mode_flag(ps_cabac_ctxt, u1_base_mode_flag);
1766
303k
    }
1767
1768
309k
    if(!u1_base_mode_flag)
1769
283k
    {
1770
283k
        if(u1_is_intra_mb)
1771
163k
        {
1772
163k
            if(mb_type == I16x16)
1773
154k
            {
1774
154k
                luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u1_cbp_c << 2) + (u1_cbp_l == 15) * 12;
1775
154k
            }
1776
9.79k
            else
1777
9.79k
            {
1778
9.79k
                luma_intra_mode = 0;
1779
9.79k
            }
1780
1781
163k
            isvce_cabac_encode_bin(ps_cabac_ctxt, 1,
1782
163k
                                   ps_cabac_ctxt->au1_cabac_ctxt_table + MB_TYPE_P_SLICE);
1783
1784
163k
            isvce_cabac_enc_intra_mb_type(PSLICE, (UWORD8) luma_intra_mode, ps_cabac_ctxt,
1785
163k
                                          MB_TYPE_P_SLICE);
1786
1787
163k
            if(mb_type == I4x4)
1788
9.79k
            {
1789
9.79k
                isvce_mb_hdr_i4x4_t *ps_mb_hdr_i4x4 =
1790
9.79k
                    (isvce_mb_hdr_i4x4_t *) ps_ent_ctxt->pv_mb_header_data;
1791
1792
9.79k
                isvce_cabac_enc_4x4mb_modes(ps_cabac_ctxt, ps_mb_hdr_i4x4->au1_sub_blk_modes);
1793
9.79k
            }
1794
1795
163k
            chroma_intra_mode = (mb_tpm >> 6);
1796
1797
163k
            isvce_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt);
1798
163k
        }
1799
119k
        else
1800
119k
        {
1801
119k
            UWORD32 u4_ctx_inc_p;
1802
1803
119k
            isvce_mb_hdr_p16x16_t *ps_mb_hdr_p16x16 =
1804
119k
                (isvce_mb_hdr_p16x16_t *) ps_ent_ctxt->pv_mb_header_data;
1805
1806
119k
            WORD16 *pi2_mv_ptr = (WORD16 *) ps_mb_hdr_p16x16->ai2_mvd;
1807
1808
            /* Encoding mb_type as P16x16 */
1809
119k
            u4_ctx_inc_p = (0x010 + ((2) << 8));
1810
1811
119k
            isvce_encode_decision_bins(0, 3, u4_ctx_inc_p, 3,
1812
119k
                                       &(ps_cabac_ctxt->au1_cabac_ctxt_table[MB_TYPE_P_SLICE]),
1813
119k
                                       ps_cabac_ctxt);
1814
1815
119k
            if(ps_ent_ctxt->u1_spatial_layer_id &&
1816
117k
               ps_svc_slice_header->i1_adaptive_motion_prediction_flag)
1817
117k
            {
1818
117k
                isvce_cabac_enc_motion_prediction_flag(ps_cabac_ctxt, ps_mb_hdr_p16x16->u1_mvp_idx,
1819
117k
                                                       1);
1820
117k
            }
1821
1822
119k
            isvce_cabac_enc_mvds_p16x16(ps_cabac_ctxt, pi2_mv_ptr);
1823
119k
        }
1824
283k
    }
1825
1826
309k
    if(ps_ent_ctxt->u1_spatial_layer_id && (u1_base_mode_flag || !u1_is_intra_mb) &&
1827
143k
       ps_svc_slice_header->i1_adaptive_residual_prediction_flag)
1828
143k
    {
1829
143k
        isvce_cabac_enc_residual_prediction_flag(ps_cabac_ctxt, u1_base_mode_flag,
1830
143k
                                                 ps_mb_hdr->u1_residual_prediction_flag);
1831
143k
    }
1832
1833
309k
    if(u1_base_mode_flag || (mb_type != I16x16))
1834
155k
    {
1835
155k
        isvce_cabac_enc_cbp(u1_cbp, ps_cabac_ctxt);
1836
155k
    }
1837
1838
309k
    if((u1_cbp > 0) || (mb_type == I16x16))
1839
299k
    {
1840
299k
        mb_qp_delta =
1841
299k
            ((WORD16) ps_mb_hdr->u1_mb_qp) - ((WORD16) ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp);
1842
1843
299k
        isvce_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt);
1844
299k
        ps_ent_ctxt->ps_mb_qp_ctxt->u1_cur_mb_qp = ps_mb_hdr->u1_mb_qp;
1845
1846
299k
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1847
299k
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
1848
299k
            bitstream_end_offset - bitstream_start_offset;
1849
1850
299k
        bitstream_start_offset = bitstream_end_offset;
1851
1852
299k
        if(mb_type == I16x16)
1853
154k
        {
1854
154k
            ps_curr_ctxt->u1_mb_type = CAB_I16x16;
1855
1856
154k
            isvce_cabac_encode_residue_luma_dc(ps_ent_ctxt);
1857
1858
154k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_AC_CTXCAT);
1859
1860
154k
            pu1_byte += sizeof(isvce_mb_hdr_i16x16_t);
1861
154k
        }
1862
144k
        else if(mb_type == I4x4)
1863
9.24k
        {
1864
9.24k
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
1865
1866
9.24k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1867
1868
9.24k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1869
9.24k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1870
1871
9.24k
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
1872
9.24k
        }
1873
135k
        else if(mb_type == P16x16)
1874
111k
        {
1875
111k
            ps_curr_ctxt->u1_mb_type = (CAB_P | CAB_NON_BD16x16);
1876
1877
111k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1878
1879
111k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1880
111k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1881
1882
111k
            pu1_byte += sizeof(isvce_mb_hdr_p16x16_t);
1883
111k
        }
1884
24.4k
        else if(mb_type == BASE_MODE)
1885
24.4k
        {
1886
24.4k
            ps_curr_ctxt->u1_mb_type = (CAB_P | CAB_NON_BD16x16);
1887
1888
24.4k
            isvce_cabac_encode_residue(ps_ent_ctxt, u1_cbp, LUMA_4X4_CTXCAT);
1889
1890
24.4k
            ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
1891
24.4k
            ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6;
1892
1893
24.4k
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
1894
24.4k
        }
1895
1896
299k
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1897
299k
        ps_ent_ctxt->u4_residue_bits[!u1_is_intra_mb] +=
1898
299k
            bitstream_end_offset - bitstream_start_offset;
1899
299k
    }
1900
10.5k
    else
1901
10.5k
    {
1902
10.5k
        mb_qp_delta = 0;
1903
1904
10.5k
        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.5k
        else if(mb_type == I4x4)
1911
550
        {
1912
550
            ps_curr_ctxt->u1_mb_type = CAB_I4x4;
1913
1914
550
            pu1_byte += sizeof(isvce_mb_hdr_i4x4_t);
1915
550
        }
1916
10.0k
        else if(mb_type == P16x16)
1917
8.27k
        {
1918
8.27k
            ps_curr_ctxt->u1_mb_type = (CAB_P | CAB_NON_BD16x16);
1919
1920
8.27k
            pu1_byte += sizeof(isvce_mb_hdr_p16x16_t);
1921
8.27k
        }
1922
1.76k
        else if(mb_type == BASE_MODE)
1923
1.76k
        {
1924
1.76k
            ps_curr_ctxt->u1_mb_type = (CAB_P | CAB_NON_BD16x16);
1925
1926
1.76k
            pu1_byte += sizeof(isvce_mb_hdr_base_mode_t);
1927
1.76k
        }
1928
1929
10.5k
        bitstream_end_offset = isvce_get_num_bits(ps_bitstream);
1930
10.5k
        ps_ent_ctxt->u4_header_bits[!u1_is_intra_mb] +=
1931
10.5k
            bitstream_end_offset - bitstream_start_offset;
1932
10.5k
    }
1933
1934
309k
    isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, mb_qp_delta, u1_cbp, u1_base_mode_flag,
1935
309k
                         mb_type);
1936
1937
309k
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1938
1939
309k
    return IH264E_SUCCESS;
1940
469k
}
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
707
                   ->ps_svc_slice_hdr_base[ps_ent_ctxt->i4_cur_slice_idx % SVC_MAX_SLICE_HDR_CNT]
2328
707
                   .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
6.15k
    for(i = 0; i < i4_ht_mbs; i++)
2343
4.05k
    {
2344
54.2k
        for(j = 0; j < i4_wd_mbs; j++)
2345
50.1k
        {
2346
50.1k
            MBTYPES_T mb_type = PSKIP;
2347
2348
50.1k
            ps_ent_ctxt->i4_mb_x = j;
2349
50.1k
            ps_ent_ctxt->i4_mb_y = i;
2350
2351
50.1k
            isvce_get_cabac_context(ps_ent_ctxt, mb_type);
2352
50.1k
            ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
2353
2354
50.1k
            isvce_cabac_enc_mb_skip(mb_type == PSKIP, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE);
2355
2356
50.1k
            if(mb_type == PSKIP)
2357
50.1k
            {
2358
50.1k
                ps_curr_ctxt->u1_mb_type = CAB_P_SKIP;
2359
50.1k
                isvce_mb_ctxt_update(ps_cabac_ctxt, ps_curr_ctxt, 0, 0, 0, PSKIP);
2360
50.1k
            }
2361
2362
50.1k
            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
48.0k
            else
2367
48.0k
            {
2368
48.0k
                isvce_cabac_encode_terminate(ps_cabac_ctxt, 0);
2369
48.0k
            }
2370
50.1k
        }
2371
4.05k
    }
2372
2.10k
    return IH264E_SUCCESS;
2373
2.10k
}
2374
#endif