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