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