Coverage Report

Created: 2026-09-01 06:44

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