Home | History | Annotate | Download | only in decoder
      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  * \file ih264d_parse_mb_header.c
     23  *
     24  * \brief
     25  *    This file contains context identifier encoding routines.
     26  *
     27  * \date
     28  *    04/02/2003
     29  *
     30  * \author  NS
     31  ***************************************************************************
     32  */
     33 #include <string.h>
     34 #include "ih264d_structs.h"
     35 #include "ih264d_bitstrm.h"
     36 #include "ih264d_cabac.h"
     37 #include "ih264_typedefs.h"
     38 #include "ih264_macros.h"
     39 #include "ih264_platform_macros.h"
     40 #include "ih264d_defs.h"
     41 #include "ih264d_error_handler.h"
     42 #include "ih264d_tables.h"
     43 #include "ih264d_debug.h"
     44 #include "ih264d_defs.h"
     45 #include "ih264d_defs.h"
     46 #include "ih264d_mb_utils.h"
     47 #include "ih264d_parse_mb_header.h"
     48 #include "ih264d_defs.h"
     49 
     50 /*! < CtxtInc index 0 - CtxMbTypeI, CtxMbTypeSISuffix
     51  index 1 - CtxMbTypePSuffix, CtxMbTypeBSuffix
     52  */
     53 
     54 
     55 
     56 /*!
     57  **************************************************************************
     58  * \if Function name : ih264d_parse_mb_type_intra_cabac \endif
     59  *
     60  * \brief
     61  *    This function decodes MB type using CABAC entropy coding mode.
     62  *
     63  * \return
     64  *    MBType.
     65  *
     66  **************************************************************************
     67  */
     68 UWORD8 ih264d_parse_mb_type_intra_cabac(UWORD8 u1_inter,
     69                                         struct _DecStruct * ps_dec)
     70 {
     71     decoding_envirnoment_t * ps_cab_env = &ps_dec->s_cab_dec_env;
     72     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
     73     ctxt_inc_mb_info_t * ps_left_ctxt = ps_dec->p_left_ctxt_mb_info;
     74     ctxt_inc_mb_info_t * ps_top_ctxt = ps_dec->p_top_ctxt_mb_info;
     75     bin_ctxt_model_t *ps_mb_bin_ctxt = ps_dec->p_mb_type_t;
     76     WORD8 u1_mb_type, u1_bin;
     77     UWORD32 u4_cxt_inc;
     78 
     79     u4_cxt_inc = 0;
     80     if(!u1_inter)
     81     {
     82         if(ps_left_ctxt != ps_dec->ps_def_ctxt_mb_info)
     83             u4_cxt_inc += ((ps_left_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0);
     84         if(ps_top_ctxt != ps_dec->ps_def_ctxt_mb_info)
     85             u4_cxt_inc += ((ps_top_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0);
     86     }
     87     else
     88     {
     89         ps_mb_bin_ctxt = ps_mb_bin_ctxt + 3 + (ps_dec->u1_B << 1);
     90     }
     91 
     92     /* b0 */
     93     u1_mb_type = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt, ps_bitstrm,
     94                                           ps_cab_env);
     95     if(u1_mb_type)
     96     {
     97         /* I16x16 or I_PCM mode */
     98         /* b1 */
     99         u1_bin = ih264d_decode_terminate(ps_cab_env, ps_bitstrm);
    100         if(u1_bin == 0)
    101         {
    102             /* I16x16 mode */
    103             /* Read b2 and b3 */
    104             u4_cxt_inc = (u1_inter) ? 0x021 : 0x043;
    105 
    106             u1_bin = ih264d_decode_bins(2, u4_cxt_inc, ps_mb_bin_ctxt, ps_bitstrm,
    107                                         ps_cab_env);
    108 
    109             if(u1_bin & 0x01)
    110                 u1_mb_type += 4;
    111 
    112             if(u1_bin & 0x02)
    113                 u1_mb_type += 12;
    114 
    115             if(u1_bin & 0x01)
    116             {
    117                 /* since b3=1, Read three bins */
    118                 u4_cxt_inc = (u1_inter) ? 0x0332 : 0x0765;
    119                 u1_bin = (UWORD8)ih264d_decode_bins(3, u4_cxt_inc, ps_mb_bin_ctxt,
    120                                                     ps_bitstrm, ps_cab_env);
    121 
    122             }
    123             else
    124             {
    125                 /* Read two bins */
    126                 u4_cxt_inc = (u1_inter) ? 0x033 : 0x076;
    127                 u1_bin = (UWORD8)ih264d_decode_bins(2, u4_cxt_inc, ps_mb_bin_ctxt,
    128                                                     ps_bitstrm, ps_cab_env);
    129             }
    130             u1_mb_type += u1_bin;
    131         }
    132         else
    133         {
    134             /* I_PCM mode */
    135             /* b1=1 */
    136             u1_mb_type = 25;
    137         }
    138     }
    139     return (u1_mb_type);
    140 }
    141 
    142 /*!
    143  **************************************************************************
    144  * \if Function name : ih264d_parse_mb_type_cabac \endif
    145  *
    146  * \brief
    147  *    This function decodes MB type using CABAC entropy coding mode.
    148  *
    149  * \return
    150  *    MBType.
    151  *
    152  **************************************************************************
    153  */
    154 UWORD32 ih264d_parse_mb_type_cabac(struct _DecStruct * ps_dec)
    155 {
    156     const UWORD8 uc_slice_type = ps_dec->ps_cur_slice->u1_slice_type;
    157     decoding_envirnoment_t *ps_cab_env = &ps_dec->s_cab_dec_env;
    158     dec_bit_stream_t *ps_bitstrm = ps_dec->ps_bitstrm;
    159     ctxt_inc_mb_info_t *ps_left_ctxt = ps_dec->p_left_ctxt_mb_info;
    160     ctxt_inc_mb_info_t *ps_top_ctxt = ps_dec->p_top_ctxt_mb_info;
    161     WORD8 c_ctxt_inc;
    162     bin_ctxt_model_t *ps_mb_bin_ctxt = ps_dec->p_mb_type_t;
    163     WORD8 u1_mb_type = 0, u1_bin;
    164     UWORD32 u4_cxt_inc;
    165 
    166     INC_SYM_COUNT(ps_cab_env);
    167 
    168     c_ctxt_inc = 0;
    169 
    170     if(uc_slice_type == SI_SLICE)
    171     {
    172         /* b0 */
    173         if(ps_left_ctxt != ps_dec->ps_def_ctxt_mb_info)
    174             c_ctxt_inc += ((ps_left_ctxt->u1_mb_type != CAB_SI4x4) ? 1 : 0);
    175         if(ps_top_ctxt != ps_dec->ps_def_ctxt_mb_info)
    176             c_ctxt_inc += ((ps_top_ctxt->u1_mb_type != CAB_SI4x4) ? 1 : 0);
    177 
    178         u4_cxt_inc = c_ctxt_inc;
    179         u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt, ps_bitstrm,
    180                                            ps_cab_env);
    181         if(u1_bin == 0)
    182         {
    183             /* SI MB */
    184             u1_mb_type = 0;
    185         }
    186         else
    187         {
    188             u1_mb_type = 1 + ih264d_parse_mb_type_intra_cabac(0, ps_dec);
    189         }
    190     }
    191     else if(uc_slice_type == P_SLICE)
    192     {
    193         /* P Slice */
    194         /* b0 */
    195         u4_cxt_inc = 0;
    196         u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt, ps_bitstrm,
    197                                            ps_cab_env);
    198         if(!u1_bin)
    199         {
    200             /* Inter MB types */
    201             /* b1 */
    202             u4_cxt_inc = 0x01;
    203             u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt,
    204                                                ps_bitstrm, ps_cab_env);
    205             /* b2 */
    206             u4_cxt_inc = u1_bin + 2;
    207             u1_mb_type = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt,
    208                                                   ps_bitstrm, ps_cab_env);
    209             u1_mb_type = (u1_bin << 1) + u1_mb_type;
    210             if(u1_mb_type)
    211                 u1_mb_type = 4 - u1_mb_type;
    212         }
    213         else
    214         {
    215             /* Intra Prefix 1 found */
    216             /* Intra MB type */
    217             u1_mb_type = 5 + ih264d_parse_mb_type_intra_cabac(1, ps_dec);
    218         }
    219     }
    220     else if(uc_slice_type == B_SLICE)
    221     {
    222         WORD8 a, b;
    223         /* B Slice */
    224         /* b0 */
    225         /* a = b = 0, if B slice and MB is a SKIP or B_DIRECT16x16 */
    226         a = 0;
    227         b = 0;
    228         u1_mb_type = 0;
    229         if(ps_left_ctxt != ps_dec->ps_def_ctxt_mb_info)
    230             a = ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) != CAB_BD16x16);
    231         if(ps_top_ctxt != ps_dec->ps_def_ctxt_mb_info)
    232             b = ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) != CAB_BD16x16);
    233 
    234         u4_cxt_inc = a + b;
    235 
    236         u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt, ps_bitstrm,
    237                                            ps_cab_env);
    238 
    239         if(u1_bin)
    240         {
    241 
    242             /* b1 */
    243             u4_cxt_inc = 0x03;
    244             u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt,
    245                                                ps_bitstrm, ps_cab_env);
    246 
    247             if(!u1_bin)
    248             {
    249                 /* b2 */
    250                 u4_cxt_inc = 0x05;
    251                 u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt,
    252                                                    ps_bitstrm, ps_cab_env);
    253 
    254                 u1_mb_type = u1_bin + 1;
    255             }
    256             else
    257             {
    258                 u1_mb_type = 3;
    259                 /* b2 */
    260                 u4_cxt_inc = 0x04;
    261                 u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt,
    262                                                    ps_bitstrm, ps_cab_env);
    263 
    264                 if(u1_bin)
    265                 {
    266                     u1_mb_type += 8;
    267                     /* b3 */
    268                     u4_cxt_inc = 0x05;
    269                     u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc, ps_mb_bin_ctxt,
    270                                                        ps_bitstrm, ps_cab_env);
    271 
    272                     if(!u1_bin)
    273                     {
    274                         u1_mb_type++;
    275                         /* b4, b5, b6 */
    276                         u4_cxt_inc = 0x0555;
    277                         u1_bin = (UWORD8)ih264d_decode_bins(3, u4_cxt_inc,
    278                                                             ps_mb_bin_ctxt,
    279                                                             ps_bitstrm,
    280                                                             ps_cab_env);
    281 
    282 
    283 
    284                         u1_mb_type += u1_bin;
    285                     }
    286                     else
    287                     {
    288                         /* b4 */
    289                         u4_cxt_inc = 0x05;
    290                         u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc,
    291                                                            ps_mb_bin_ctxt,
    292                                                            ps_bitstrm,
    293                                                            ps_cab_env);
    294 
    295                         if(u1_bin)
    296                         {
    297                             /* b5 */
    298                             u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc,
    299                                                                ps_mb_bin_ctxt,
    300                                                                ps_bitstrm,
    301                                                                ps_cab_env);
    302 
    303                             u1_mb_type += (u1_bin ? 11 : 0);
    304                         }
    305                         else
    306                         {
    307                             u1_mb_type = 20;
    308                             /* b5 */
    309                             u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc,
    310                                                                ps_mb_bin_ctxt,
    311                                                                ps_bitstrm,
    312                                                                ps_cab_env);
    313 
    314                             if(!u1_bin)
    315                             {
    316                                 /* b6 */
    317                                 u1_bin = (UWORD8)ih264d_decode_bin(u4_cxt_inc,
    318                                                                    ps_mb_bin_ctxt,
    319                                                                    ps_bitstrm,
    320                                                                    ps_cab_env);
    321 
    322                                 u1_mb_type += u1_bin;
    323                             }
    324                             else
    325                             {
    326                                 /* Intra Prefix 111101 found */
    327                                 /* Intra MB type */
    328                                 u1_mb_type =
    329                                                 23
    330                                                                 + ih264d_parse_mb_type_intra_cabac(
    331                                                                                 1,
    332                                                                                 ps_dec);
    333                             }
    334                         }
    335                     }
    336                 }
    337                 else
    338                 {
    339                     /* b3, b4, b5 */
    340                     u4_cxt_inc = 0x0555;
    341                     u1_bin = (UWORD8)ih264d_decode_bins(3, u4_cxt_inc,
    342                                                         ps_mb_bin_ctxt, ps_bitstrm,
    343                                                         ps_cab_env);
    344 
    345 
    346 
    347 
    348                     u1_mb_type += u1_bin;
    349                 }
    350             }
    351         }
    352     }
    353     return ((UWORD32)u1_mb_type);
    354 }
    355 
    356 /*!
    357  **************************************************************************
    358  * \if Function name : DecSubMBType \endif
    359  *
    360  * \brief
    361  *    This function decodes MB type using CABAC entropy coding mode.
    362  *
    363  * \return
    364  *    MBType.
    365  *
    366  **************************************************************************
    367  */
    368 UWORD32 ih264d_parse_submb_type_cabac(const UWORD8 u1_slc_type_b,
    369                                       decoding_envirnoment_t * ps_cab_env,
    370                                       dec_bit_stream_t * ps_bitstrm,
    371                                       bin_ctxt_model_t * ps_sub_mb_cxt)
    372 {
    373     WORD8 u1_sub_mb_type, u1_bin;
    374 
    375     INC_SYM_COUNT(ps_cab_env);
    376 
    377     u1_sub_mb_type = 0;
    378     u1_bin = (UWORD8)ih264d_decode_bin(0, ps_sub_mb_cxt, ps_bitstrm,
    379                                        ps_cab_env);
    380 
    381     if(u1_slc_type_b ^ u1_bin)
    382         return 0;
    383 
    384     if(!u1_slc_type_b)
    385     {
    386         /* P Slice */
    387         u1_sub_mb_type = 1;
    388         u1_bin = (UWORD8)ih264d_decode_bin(1, ps_sub_mb_cxt, ps_bitstrm,
    389                                            ps_cab_env);
    390         if(u1_bin == 1)
    391         {
    392             u1_bin = (UWORD8)ih264d_decode_bin(2, ps_sub_mb_cxt, ps_bitstrm,
    393                                                ps_cab_env);
    394             u1_sub_mb_type = (2 + (!u1_bin));
    395         }
    396 
    397         return u1_sub_mb_type;
    398     }
    399     else
    400     {
    401         /* B Slice */
    402 
    403         /* b1 */
    404         u1_bin = (UWORD8)ih264d_decode_bin(1, ps_sub_mb_cxt, ps_bitstrm,
    405                                            ps_cab_env);
    406         if(u1_bin)
    407         {
    408             /* b2 */
    409             u1_bin = (UWORD8)ih264d_decode_bin(2, ps_sub_mb_cxt, ps_bitstrm,
    410                                                ps_cab_env);
    411             if(u1_bin)
    412             {
    413                 /* b3 */
    414                 u1_sub_mb_type = 7;
    415                 u1_bin = (UWORD8)ih264d_decode_bin(3, ps_sub_mb_cxt, ps_bitstrm,
    416                                                    ps_cab_env);
    417                 u1_sub_mb_type += u1_bin << 2;
    418                 u1_bin = !u1_bin;
    419                 /* b4 */
    420                 if(u1_bin == 0)
    421                 {
    422                     u1_bin = ih264d_decode_bin(3, ps_sub_mb_cxt, ps_bitstrm,
    423                                                ps_cab_env);
    424                 }
    425                 else
    426                 {
    427                     u1_bin = (UWORD8)ih264d_decode_bins(2, 0x33, ps_sub_mb_cxt,
    428                                                         ps_bitstrm, ps_cab_env);
    429                 }
    430 
    431                 return (u1_sub_mb_type + u1_bin);
    432             }
    433             else
    434             {
    435                 /* b3 */
    436                 u1_bin = (UWORD8)ih264d_decode_bins(2, 0x33, ps_sub_mb_cxt,
    437                                                     ps_bitstrm, ps_cab_env);
    438                 return (3 + u1_bin);
    439             }
    440         }
    441         else
    442         {
    443             /* b2 */
    444             u1_bin = (UWORD8)ih264d_decode_bin(3, ps_sub_mb_cxt, ps_bitstrm,
    445                                                ps_cab_env);
    446             return (1 + u1_bin);
    447         }
    448     }
    449 }
    450 
    451 /*!
    452  **************************************************************************
    453  * \if Function name : ih264d_parse_ref_idx_cabac \endif
    454  *
    455  * \brief
    456  *    This function decodes Reference Index using CABAC entropy coding mode.
    457  *
    458  * \return
    459  *    None
    460  *
    461  **************************************************************************
    462  */
    463 WORD32 ih264d_parse_ref_idx_cabac(const UWORD8 u1_num_part,
    464                                 const UWORD8 u1_b2,
    465                                 const UWORD8 u1_max_ref_minus1,
    466                                 const UWORD8 u1_mb_mode,
    467                                 WORD8 * pi1_ref_idx,
    468                                 WORD8 * const pi1_lft_cxt,
    469                                 WORD8 * const pi1_top_cxt,
    470                                 decoding_envirnoment_t * const ps_cab_env,
    471                                 dec_bit_stream_t * const ps_bitstrm,
    472                                 bin_ctxt_model_t * const ps_ref_cxt)
    473 {
    474     UWORD8 u1_a, u1_b;
    475     UWORD32 u4_cxt_inc;
    476     UWORD8 u1_blk_no, u1_i, u1_idx_lft, u1_idx_top;
    477     WORD8 i1_ref_idx;
    478 
    479     for(u1_blk_no = 0, u1_i = 0; u1_i < u1_num_part; u1_i++, pi1_ref_idx++)
    480     {
    481         u1_idx_lft = ((u1_blk_no & 0x02) >> 1) + u1_b2;
    482         u1_idx_top = (u1_blk_no & 0x01) + u1_b2;
    483         i1_ref_idx = *pi1_ref_idx;
    484 
    485         if(i1_ref_idx > 0)
    486         {
    487             u1_a = pi1_lft_cxt[u1_idx_lft] > 0;
    488             u1_b = pi1_top_cxt[u1_idx_top] > 0;
    489 
    490             u4_cxt_inc = u1_a + (u1_b << 1);
    491             u4_cxt_inc = (u4_cxt_inc | 0x55540);
    492 
    493             i1_ref_idx = (WORD8)ih264d_decode_bins_unary(32, u4_cxt_inc,
    494                                                          ps_ref_cxt, ps_bitstrm,
    495                                                          ps_cab_env);
    496 
    497             if((i1_ref_idx > u1_max_ref_minus1) || (i1_ref_idx < 0))
    498             {
    499                 return ERROR_REF_IDX;
    500             }
    501 
    502             *pi1_ref_idx = i1_ref_idx;
    503 
    504             INC_SYM_COUNT(ps_cab_env);
    505 
    506         }
    507 
    508         /* Storing Reference Idx Information */
    509         pi1_lft_cxt[u1_idx_lft] = i1_ref_idx;
    510         pi1_top_cxt[u1_idx_top] = i1_ref_idx;
    511         u1_blk_no = u1_blk_no + 1 + (u1_mb_mode & 0x01);
    512     }
    513     /* if(!u1_sub_mb) */
    514     if(u1_num_part != 4)
    515     {
    516         pi1_lft_cxt[(!(u1_mb_mode & 0x1)) + u1_b2] = pi1_lft_cxt[u1_b2];
    517         pi1_top_cxt[(!(u1_mb_mode & 0x2)) + u1_b2] = pi1_top_cxt[u1_b2];
    518     }
    519     return OK;
    520 }
    521 
    522 /*!
    523  **************************************************************************
    524  * \if Function name : ih264d_parse_mb_qp_delta_cabac \endif
    525  *
    526  * \brief
    527  *    This function decodes MB Qp delta using CABAC entropy coding mode.
    528  *
    529  * \return
    530  *    None
    531  *
    532  **************************************************************************
    533  */
    534 WORD32 ih264d_parse_mb_qp_delta_cabac(struct _DecStruct * ps_dec,
    535                                       WORD8 *pi1_mb_qp_delta)
    536 {
    537     decoding_envirnoment_t * ps_cab_env = &ps_dec->s_cab_dec_env;
    538     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
    539 
    540     UWORD8 u1_code_num;
    541     bin_ctxt_model_t *ps_mb_qp_delta_ctxt = ps_dec->p_mb_qp_delta_t;
    542     UWORD32 u4_cxt_inc;
    543 
    544     INC_SYM_COUNT(ps_cab_env);
    545 
    546     u4_cxt_inc = (!(!(ps_dec->i1_prev_mb_qp_delta)));
    547 
    548     u1_code_num = 0;
    549     u4_cxt_inc = (u4_cxt_inc | 0x33320);
    550     /* max number of bins = 53,
    551      since Range for MbQpDelta= -26 to +25 inclusive, UNARY code */
    552     u1_code_num = ih264d_decode_bins_unary(32, u4_cxt_inc, ps_mb_qp_delta_ctxt,
    553                                           ps_bitstrm, ps_cab_env);
    554     if(u1_code_num == 32)
    555     {
    556         /* Read remaining 21 bins */
    557         UWORD8 uc_codeNumX;
    558         u4_cxt_inc = 0x33333;
    559         uc_codeNumX = ih264d_decode_bins_unary(21, u4_cxt_inc, ps_mb_qp_delta_ctxt,
    560                                                ps_bitstrm, ps_cab_env);
    561         u1_code_num = u1_code_num + uc_codeNumX;
    562     }
    563 
    564     *pi1_mb_qp_delta = (u1_code_num + 1) >> 1;
    565     /* Table 9.3: If code_num is even Syntax Element has -ve value */
    566     if(!(u1_code_num & 0x01))
    567         *pi1_mb_qp_delta = -(*pi1_mb_qp_delta);
    568 
    569     /* Range of MbQpDelta= -26 to +25 inclusive */
    570     if((*pi1_mb_qp_delta < -26) || (*pi1_mb_qp_delta > 25))
    571         return ERROR_INV_RANGE_QP_T;
    572     ps_dec->i1_prev_mb_qp_delta = *pi1_mb_qp_delta;
    573     return OK;
    574 }
    575 /*!
    576  **************************************************************************
    577  * \if Function name : ih264d_parse_chroma_pred_mode_cabac \endif
    578  *
    579  * \brief
    580  *    This function decodes Chroma Pred mode using CABAC entropy coding mode.
    581  *
    582  * \return
    583  *    None
    584  *
    585  **************************************************************************
    586  */
    587 WORD8 ih264d_parse_chroma_pred_mode_cabac(struct _DecStruct * ps_dec)
    588 {
    589     decoding_envirnoment_t * ps_cab_env = &ps_dec->s_cab_dec_env;
    590     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
    591     ctxt_inc_mb_info_t * ps_left_ctxt = ps_dec->p_left_ctxt_mb_info;
    592     ctxt_inc_mb_info_t * ps_top_ctxt = ps_dec->p_top_ctxt_mb_info;
    593     WORD8 i1_chroma_pred_mode, a, b;
    594     UWORD32 u4_cxt_inc;
    595 
    596     INC_SYM_COUNT(ps_cab_env);
    597 
    598     /* Binarization is TU and Cmax=3 */
    599     i1_chroma_pred_mode = 0;
    600     a = 0;
    601     b = 0;
    602 
    603     a = ((ps_left_ctxt->u1_intra_chroma_pred_mode != 0) ? 1 : 0);
    604 
    605     b = ((ps_top_ctxt->u1_intra_chroma_pred_mode != 0) ? 1 : 0);
    606     u4_cxt_inc = a + b;
    607 
    608     u4_cxt_inc = (u4_cxt_inc | 0x330);
    609 
    610     i1_chroma_pred_mode = ih264d_decode_bins_tunary(
    611                     3, u4_cxt_inc, ps_dec->p_intra_chroma_pred_mode_t,
    612                     ps_bitstrm, ps_cab_env);
    613 
    614     return (i1_chroma_pred_mode);
    615 }
    616 
    617 /*****************************************************************************/
    618 /*                                                                           */
    619 /*  Function Name : ih264d_parse_transform8x8flag_cabac                                     */
    620 /*                                                                           */
    621 /*  Description   :                                                          */
    622 /*  Inputs        :                                                          */
    623 /*                                                                           */
    624 /*                                                                           */
    625 /*  Returns       :                                                          */
    626 /*                                                                           */
    627 /*  Revision History:                                                        */
    628 /*                                                                           */
    629 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    630 /*                      Rajasekhar      Creation                             */
    631 /*                                                                           */
    632 /*****************************************************************************/
    633 UWORD8 ih264d_parse_transform8x8flag_cabac(struct _DecStruct * ps_dec,
    634                                            dec_mb_info_t * ps_cur_mb_info)
    635 {
    636     decoding_envirnoment_t * ps_cab_env = &ps_dec->s_cab_dec_env;
    637     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
    638     ctxt_inc_mb_info_t * ps_left_ctxt = ps_dec->p_left_ctxt_mb_info;
    639     ctxt_inc_mb_info_t * ps_top_ctxt = ps_dec->p_top_ctxt_mb_info;
    640     UWORD8 u1_transform_8x8flag;
    641     UWORD8 u1_mb_ngbr_avail = ps_cur_mb_info->u1_mb_ngbr_availablity;
    642 
    643     WORD8 a, b;
    644     UWORD32 u4_cxt_inc;
    645 
    646     /* for calculating the context increment for transform8x8 u4_flag */
    647     /* it reads transform8x8 u4_flag of the neighbors through */
    648 
    649     /* Binarization is FLC */
    650     a = 0;
    651     b = 0;
    652 
    653     if(u1_mb_ngbr_avail & LEFT_MB_AVAILABLE_MASK)
    654     {
    655         a = ps_left_ctxt->u1_transform8x8_ctxt;
    656     }
    657     if(u1_mb_ngbr_avail & TOP_MB_AVAILABLE_MASK)
    658     {
    659         b = ps_top_ctxt->u1_transform8x8_ctxt;
    660 
    661     }
    662 
    663     u4_cxt_inc = a + b;
    664 
    665     u1_transform_8x8flag = ih264d_decode_bin(
    666                     u4_cxt_inc, ps_dec->s_high_profile.ps_transform8x8_flag,
    667                     ps_bitstrm, ps_cab_env);
    668 
    669     return (u1_transform_8x8flag);
    670 }
    671 
    672 /*!
    673  **************************************************************************
    674  * \if Function name : ih264d_read_intra_pred_modes_cabac \endif
    675  *
    676  * \brief
    677  *    Reads the intra pred mode related values of I4x4 MB from bitstream.
    678  *
    679  *    This function will read the prev intra pred mode flags and
    680  *    stores it in pu1_prev_intra4x4_pred_mode_flag. If the u4_flag
    681  *    indicates that most probable mode is not intra pred mode, then
    682  *    the rem_intra4x4_pred_mode is read and stored in
    683  *    pu1_rem_intra4x4_pred_mode array.
    684  *
    685  *
    686  * \return
    687  *    0 on success and Error code otherwise
    688  *
    689  **************************************************************************
    690  */
    691 WORD32 ih264d_read_intra_pred_modes_cabac(dec_struct_t * ps_dec,
    692                                           UWORD8 * pu1_prev_intra4x4_pred_mode_flag,
    693                                           UWORD8 * pu1_rem_intra4x4_pred_mode,
    694                                           UWORD8 u1_tran_form8x8)
    695 {
    696     WORD32 i4x4_luma_blk_idx = 0;
    697     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
    698     decoding_envirnoment_t * ps_cab_env = &ps_dec->s_cab_dec_env;
    699     bin_ctxt_model_t *ps_ctxt_ipred_luma_mpm, *ps_ctx_ipred_luma_rm;
    700     WORD32 i4_rem_intra4x4_pred_mode;
    701     UWORD32 u4_prev_intra4x4_pred_mode_flag;
    702     UWORD32 u4_code_int_range, u4_code_int_val_ofst;
    703     const UWORD32 *pu4_table = (const UWORD32 *)ps_cab_env->cabac_table;
    704 
    705     ps_ctxt_ipred_luma_mpm = ps_dec->p_prev_intra4x4_pred_mode_flag_t;
    706     ps_ctx_ipred_luma_rm = ps_dec->p_rem_intra4x4_pred_mode_t;
    707     SWITCHOFFTRACE;
    708 
    709     i4x4_luma_blk_idx = (0 == u1_tran_form8x8) ? 16 : 4;
    710 
    711     u4_code_int_range = ps_cab_env->u4_code_int_range;
    712     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
    713 
    714     do
    715     {
    716 
    717         DECODE_ONE_BIN_MACRO(ps_ctxt_ipred_luma_mpm, u4_code_int_range,
    718                              u4_code_int_val_ofst, pu4_table, ps_bitstrm,
    719                              u4_prev_intra4x4_pred_mode_flag)
    720         *pu1_prev_intra4x4_pred_mode_flag = u4_prev_intra4x4_pred_mode_flag;
    721 
    722         i4_rem_intra4x4_pred_mode = -1;
    723         if(!u4_prev_intra4x4_pred_mode_flag)
    724         {
    725 
    726             /*inlining DecodeDecisionBins_FLC*/
    727 
    728             {
    729 
    730                 UWORD8 u1_max_bins = 3;
    731                 UWORD32 u4_value;
    732                 UWORD32 u4_symbol, i;
    733 
    734                 i = 0;
    735                 u4_value = 0;
    736 
    737                 do
    738                 {
    739 
    740                     DECODE_ONE_BIN_MACRO(ps_ctx_ipred_luma_rm, u4_code_int_range,
    741                                          u4_code_int_val_ofst, pu4_table,
    742                                          ps_bitstrm, u4_symbol)
    743 
    744                     INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(ps_cab_env);
    745 
    746                     u4_value = u4_value | (u4_symbol << i);
    747 
    748                     i++;
    749                 }
    750                 while(i < u1_max_bins);
    751 
    752                 i4_rem_intra4x4_pred_mode = (u4_value);
    753 
    754             }
    755 
    756         }
    757 
    758         (*pu1_rem_intra4x4_pred_mode) = i4_rem_intra4x4_pred_mode;
    759 
    760         COPYTHECONTEXT("intra4x4_pred_mode", i4_rem_intra4x4_pred_mode);
    761 
    762         pu1_prev_intra4x4_pred_mode_flag++;
    763         pu1_rem_intra4x4_pred_mode++;
    764 
    765         i4x4_luma_blk_idx--;
    766     }
    767     while(i4x4_luma_blk_idx);
    768 
    769     ps_cab_env->u4_code_int_range = u4_code_int_range;
    770     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
    771 
    772     return (0);
    773 
    774 }
    775 
    776 /*!
    777  **************************************************************************
    778  * \if Function name : ih264d_parse_ctx_cbp_cabac \endif
    779  *
    780  * \brief
    781  *    This function decodes CtxCbpLuma and CtxCbpChroma (CBP of a Macroblock).
    782  *    using CABAC entropy coding mode.
    783  *
    784  * \return
    785  *    CBP of a MB.
    786  *
    787  **************************************************************************
    788  */
    789 UWORD32 ih264d_parse_ctx_cbp_cabac(struct _DecStruct * ps_dec)
    790 {
    791 
    792     UWORD32 u4_cxt_inc;
    793     decoding_envirnoment_t * ps_cab_env = &ps_dec->s_cab_dec_env;
    794     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
    795     ctxt_inc_mb_info_t * ps_left_ctxt = ps_dec->p_left_ctxt_mb_info;
    796     ctxt_inc_mb_info_t * ps_top_ctxt = ps_dec->p_top_ctxt_mb_info;
    797     bin_ctxt_model_t *ps_ctxt_cbp_luma = ps_dec->p_cbp_luma_t, *ps_bin_ctxt;
    798     WORD8 c_Cbp; //,i,j;
    799     UWORD32 u4_code_int_range, u4_code_int_val_ofst;
    800     UWORD32 u4_offset, *pu4_buffer;
    801     const UWORD32 *pu4_table = (const UWORD32 *)ps_cab_env->cabac_table;
    802 
    803     INC_SYM_COUNT(ps_cab_env);
    804 
    805 
    806 
    807     /* CBP Luma, FL, Cmax = 15, L = 4 */
    808     u4_cxt_inc = (!((ps_top_ctxt->u1_cbp >> 2) & 0x01)) << 1;
    809     u4_cxt_inc += !((ps_left_ctxt->u1_cbp >> 1) & 0x01);
    810 
    811     u4_offset = ps_bitstrm->u4_ofst;
    812     pu4_buffer = ps_bitstrm->pu4_buffer;
    813 
    814     u4_code_int_range = ps_cab_env->u4_code_int_range;
    815     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
    816     /*renormalize to ensure there 23 bits more in the u4_code_int_val_ofst*/
    817     {
    818         UWORD32 u4_clz, read_bits;
    819 
    820         u4_clz = CLZ(u4_code_int_range);
    821         FLUSHBITS(u4_offset, u4_clz)
    822         NEXTBITS(read_bits, u4_offset, pu4_buffer, 23)
    823         u4_code_int_range = u4_code_int_range << u4_clz;
    824         u4_code_int_val_ofst = (u4_code_int_val_ofst << u4_clz) | read_bits;
    825     }
    826 
    827     ps_bin_ctxt = ps_ctxt_cbp_luma + u4_cxt_inc;
    828 
    829     /*inlining DecodeDecision_onebin without renorm*/
    830     {
    831 
    832         UWORD32 u4_qnt_int_range, u4_int_range_lps;
    833         UWORD32 u4_symbol, u1_mps_state;
    834         UWORD32 table_lookup;
    835         UWORD32 u4_clz;
    836 
    837         u1_mps_state = (ps_bin_ctxt->u1_mps_state);
    838 
    839         u4_clz = CLZ(u4_code_int_range);
    840         u4_qnt_int_range = u4_code_int_range << u4_clz;
    841         u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
    842 
    843         table_lookup = pu4_table[(u1_mps_state << 2) + u4_qnt_int_range];
    844         u4_int_range_lps = table_lookup & 0xff;
    845 
    846         u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
    847         u4_code_int_range = u4_code_int_range - u4_int_range_lps;
    848 
    849         u4_symbol = ((u1_mps_state >> 6) & 0x1);
    850 
    851         /*if mps*/
    852         u1_mps_state = (table_lookup >> 8) & 0x7F;
    853 
    854         CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst, u4_symbol,
    855                      u4_int_range_lps, u1_mps_state, table_lookup)
    856 
    857         INC_BIN_COUNT(ps_cab_env);
    858 
    859         ps_bin_ctxt->u1_mps_state = u1_mps_state;
    860 
    861         c_Cbp = u4_symbol;
    862 
    863     }
    864 
    865     u4_cxt_inc = (!((ps_top_ctxt->u1_cbp >> 3) & 0x01)) << 1;
    866     u4_cxt_inc += !(c_Cbp & 0x01);
    867     ps_bin_ctxt = ps_ctxt_cbp_luma + u4_cxt_inc;
    868     /*inlining DecodeDecision_onebin without renorm*/
    869 
    870     {
    871 
    872         UWORD32 u4_qnt_int_range, u4_int_range_lps;
    873         UWORD32 u4_symbol, u1_mps_state;
    874         UWORD32 table_lookup;
    875         UWORD32 u4_clz;
    876 
    877         u1_mps_state = (ps_bin_ctxt->u1_mps_state);
    878 
    879         u4_clz = CLZ(u4_code_int_range);
    880         u4_qnt_int_range = u4_code_int_range << u4_clz;
    881         u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
    882 
    883         table_lookup = pu4_table[(u1_mps_state << 2) + u4_qnt_int_range];
    884         u4_int_range_lps = table_lookup & 0xff;
    885 
    886         u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
    887         u4_code_int_range = u4_code_int_range - u4_int_range_lps;
    888 
    889         u4_symbol = ((u1_mps_state >> 6) & 0x1);
    890 
    891         /*if mps*/
    892         u1_mps_state = (table_lookup >> 8) & 0x7F;
    893 
    894         CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst, u4_symbol,
    895                      u4_int_range_lps, u1_mps_state, table_lookup)
    896 
    897         INC_BIN_COUNT(ps_cab_env);
    898 
    899         ps_bin_ctxt->u1_mps_state = u1_mps_state;
    900 
    901         c_Cbp |= u4_symbol << 1;
    902 
    903     }
    904 
    905     u4_cxt_inc = (!(c_Cbp & 0x01)) << 1;
    906     u4_cxt_inc += !((ps_left_ctxt->u1_cbp >> 3) & 0x01);
    907     ps_bin_ctxt = ps_ctxt_cbp_luma + u4_cxt_inc;
    908     /*inlining DecodeDecision_onebin without renorm*/
    909 
    910     {
    911 
    912         UWORD32 u4_qnt_int_range, u4_int_range_lps;
    913         UWORD32 u4_symbol, u1_mps_state;
    914         UWORD32 table_lookup;
    915         UWORD32 u4_clz;
    916 
    917         u1_mps_state = (ps_bin_ctxt->u1_mps_state);
    918 
    919         u4_clz = CLZ(u4_code_int_range);
    920         u4_qnt_int_range = u4_code_int_range << u4_clz;
    921         u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
    922 
    923         table_lookup = pu4_table[(u1_mps_state << 2) + u4_qnt_int_range];
    924         u4_int_range_lps = table_lookup & 0xff;
    925 
    926         u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
    927         u4_code_int_range = u4_code_int_range - u4_int_range_lps;
    928 
    929         u4_symbol = ((u1_mps_state >> 6) & 0x1);
    930 
    931         /*if mps*/
    932         u1_mps_state = (table_lookup >> 8) & 0x7F;
    933 
    934         CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst, u4_symbol,
    935                      u4_int_range_lps, u1_mps_state, table_lookup)
    936 
    937         INC_BIN_COUNT(ps_cab_env);
    938 
    939         ps_bin_ctxt->u1_mps_state = u1_mps_state;
    940 
    941         c_Cbp |= u4_symbol << 2;
    942 
    943     }
    944 
    945     u4_cxt_inc = (!((c_Cbp >> 1) & 0x01)) << 1;
    946     u4_cxt_inc += !((c_Cbp >> 2) & 0x01);
    947     ps_bin_ctxt = ps_ctxt_cbp_luma + u4_cxt_inc;
    948     /*inlining DecodeDecision_onebin without renorm*/
    949 
    950     {
    951 
    952         UWORD32 u4_qnt_int_range, u4_int_range_lps;
    953         UWORD32 u4_symbol, u1_mps_state;
    954         UWORD32 table_lookup;
    955         UWORD32 u4_clz;
    956 
    957         u1_mps_state = (ps_bin_ctxt->u1_mps_state);
    958 
    959         u4_clz = CLZ(u4_code_int_range);
    960         u4_qnt_int_range = u4_code_int_range << u4_clz;
    961         u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
    962 
    963         table_lookup = pu4_table[(u1_mps_state << 2) + u4_qnt_int_range];
    964         u4_int_range_lps = table_lookup & 0xff;
    965 
    966         u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
    967         u4_code_int_range = u4_code_int_range - u4_int_range_lps;
    968 
    969         u4_symbol = ((u1_mps_state >> 6) & 0x1);
    970 
    971         /*if mps*/
    972         u1_mps_state = (table_lookup >> 8) & 0x7F;
    973 
    974         CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst, u4_symbol,
    975                      u4_int_range_lps, u1_mps_state, table_lookup)
    976 
    977         INC_BIN_COUNT(ps_cab_env);
    978 
    979         ps_bin_ctxt->u1_mps_state = u1_mps_state;
    980 
    981         c_Cbp |= u4_symbol << 3;
    982 
    983     }
    984 
    985     if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_8)
    986     {
    987 
    988         RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst, u4_offset,
    989                             pu4_buffer)
    990 
    991     }
    992 
    993     {
    994         UWORD32 u4_cxt_inc;
    995         WORD8 a, b, c, d;
    996         bin_ctxt_model_t *p_CtxtCbpChroma = ps_dec->p_cbp_chroma_t;
    997 
    998         /* CBP Chroma, TU, Cmax = 2 */
    999         a = 0;
   1000         b = 0;
   1001         c = 0;
   1002         d = 0;
   1003 
   1004         {
   1005             a = (ps_top_ctxt->u1_cbp > 15) ? 2 : 0;
   1006             c = (ps_top_ctxt->u1_cbp > 31) ? 2 : 0;
   1007         }
   1008 
   1009         {
   1010             b = (ps_left_ctxt->u1_cbp > 15) ? 1 : 0;
   1011             d = (ps_left_ctxt->u1_cbp > 31) ? 1 : 0;
   1012         }
   1013         u4_cxt_inc = a + b;
   1014         u4_cxt_inc = (u4_cxt_inc | ((4 + c + d) << 4));
   1015 
   1016         /*inlining ih264d_decode_bins_tunary */
   1017 
   1018         {
   1019 
   1020             UWORD8 u1_max_bins = 2;
   1021             UWORD32 u4_ctx_inc = u4_cxt_inc;
   1022 
   1023             UWORD32 u4_value;
   1024             UWORD32 u4_symbol;
   1025             UWORD8 u4_ctx_Inc;
   1026             bin_ctxt_model_t *ps_bin_ctxt;
   1027             u4_value = 0;
   1028 
   1029             do
   1030             {
   1031                 u4_ctx_Inc = u4_ctx_inc & 0xF;
   1032                 u4_ctx_inc = u4_ctx_inc >> 4;
   1033 
   1034                 ps_bin_ctxt = p_CtxtCbpChroma + u4_ctx_Inc;
   1035                 /*inlining DecodeDecision_onebin*/
   1036                 {
   1037 
   1038                     UWORD32 u4_qnt_int_range, u4_int_range_lps;
   1039 
   1040                     UWORD32 u1_mps_state;
   1041                     UWORD32 table_lookup;
   1042                     UWORD32 u4_clz;
   1043 
   1044                     u1_mps_state = (ps_bin_ctxt->u1_mps_state);
   1045 
   1046                     u4_clz = CLZ(u4_code_int_range);
   1047                     u4_qnt_int_range = u4_code_int_range << u4_clz;
   1048                     u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
   1049 
   1050                     table_lookup = pu4_table[(u1_mps_state << 2)
   1051                                     + u4_qnt_int_range];
   1052                     u4_int_range_lps = table_lookup & 0xff;
   1053 
   1054                     u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
   1055                     u4_code_int_range = u4_code_int_range - u4_int_range_lps;
   1056 
   1057                     u4_symbol = ((u1_mps_state >> 6) & 0x1);
   1058 
   1059                     /*if mps*/
   1060                     u1_mps_state = (table_lookup >> 8) & 0x7F;
   1061 
   1062                     CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst,
   1063                                  u4_symbol, u4_int_range_lps, u1_mps_state,
   1064                                  table_lookup)
   1065 
   1066                     if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_8)
   1067                     {
   1068                         RENORM_RANGE_OFFSET(u4_code_int_range,
   1069                                             u4_code_int_val_ofst, u4_offset,
   1070                                             pu4_buffer)
   1071                     }
   1072                     ps_bin_ctxt->u1_mps_state = u1_mps_state;
   1073                 }
   1074 
   1075                 INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(
   1076                                 ps_cab_env);
   1077 
   1078                 u4_value++;
   1079             }
   1080             while((u4_value < u1_max_bins) & (u4_symbol));
   1081 
   1082             u4_value = u4_value - 1 + u4_symbol;
   1083 
   1084             a = (u4_value);
   1085 
   1086         }
   1087 
   1088 c_Cbp = (c_Cbp | (a << 4));
   1089 }
   1090 
   1091 ps_bitstrm->u4_ofst = u4_offset;
   1092 
   1093 ps_cab_env->u4_code_int_range = u4_code_int_range;
   1094 ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
   1095 
   1096 return (c_Cbp);
   1097 }
   1098 
   1099 /*!
   1100  **************************************************************************
   1101  * \if Function name : ih264d_get_mvd_cabac \endif
   1102  *
   1103  * \brief
   1104  *    This function decodes Horz and Vert mvd_l0 and mvd_l1 using CABAC entropy
   1105  *    coding mode as defined in 9.3.2.3.
   1106  *
   1107  * \return
   1108  *    None
   1109  *
   1110  **************************************************************************
   1111  */
   1112 void ih264d_get_mvd_cabac(UWORD8 u1_sub_mb,
   1113                           UWORD8 u1_b2,
   1114                           UWORD8 u1_part_wd,
   1115                           UWORD8 u1_part_ht,
   1116                           UWORD8 u1_dec_mvd,
   1117                           dec_struct_t *ps_dec,
   1118                           mv_pred_t *ps_mv)
   1119 {
   1120     UWORD8 u1_abs_mvd_x = 0, u1_abs_mvd_y = 0;
   1121     UWORD8 u1_sub_mb_x, u1_sub_mb_y;
   1122     UWORD8 *pu1_top_mv_ctxt, *pu1_lft_mv_ctxt;
   1123     WORD16 *pi2_mv;
   1124 
   1125     u1_sub_mb_x = (UWORD8)(u1_sub_mb & 0x03);
   1126     u1_sub_mb_y = (UWORD8)(u1_sub_mb >> 2);
   1127     pu1_top_mv_ctxt = &ps_dec->ps_curr_ctxt_mb_info->u1_mv[u1_sub_mb_x][u1_b2];
   1128     pu1_lft_mv_ctxt = &ps_dec->pu1_left_mv_ctxt_inc[u1_sub_mb_y][u1_b2];
   1129     pi2_mv = &ps_mv->i2_mv[u1_b2];
   1130 
   1131     if(u1_dec_mvd)
   1132     {
   1133         WORD16 i2_mv_x, i2_mv_y;
   1134         WORD32 i2_temp;
   1135         {
   1136             decoding_envirnoment_t * ps_cab_env = &ps_dec->s_cab_dec_env;
   1137             dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
   1138             UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a,
   1139                             u2_abs_mvd_y_b;
   1140 
   1141             u2_abs_mvd_x_b = (UWORD16)pu1_top_mv_ctxt[0];
   1142             u2_abs_mvd_y_b = (UWORD16)pu1_top_mv_ctxt[1];
   1143             u2_abs_mvd_x_a = (UWORD16)pu1_lft_mv_ctxt[0];
   1144             u2_abs_mvd_y_a = (UWORD16)pu1_lft_mv_ctxt[1];
   1145 
   1146             i2_temp = u2_abs_mvd_x_a + u2_abs_mvd_x_b;
   1147 
   1148             i2_mv_x = ih264d_parse_mvd_cabac(ps_bitstrm, ps_cab_env,
   1149                                              ps_dec->p_mvd_x_t, i2_temp);
   1150 
   1151             i2_temp = u2_abs_mvd_y_a + u2_abs_mvd_y_b;
   1152 
   1153             i2_mv_y = ih264d_parse_mvd_cabac(ps_bitstrm, ps_cab_env,
   1154                                              ps_dec->p_mvd_y_t, i2_temp);
   1155         }
   1156 
   1157         /***********************************************************************/
   1158         /* Store the abs_mvd_values in cabac contexts                          */
   1159         /* The follownig code can be easily optimzed if mvX, mvY clip values   */
   1160         /* are packed in 16 bits follwed by memcpy                             */
   1161         /***********************************************************************/
   1162         u1_abs_mvd_x = CLIP3(0, 127, ABS(i2_mv_x));
   1163         u1_abs_mvd_y = CLIP3(0, 127, ABS(i2_mv_y));
   1164 
   1165         COPYTHECONTEXT("MVD", i2_mv_x);COPYTHECONTEXT("MVD", i2_mv_y);
   1166 
   1167         /* Storing Mv residuals */
   1168         pi2_mv[0] = i2_mv_x;
   1169         pi2_mv[1] = i2_mv_y;
   1170     }
   1171 
   1172     /***************************************************************/
   1173     /* Store abs_mvd_values cabac contexts                         */
   1174     /***************************************************************/
   1175     {
   1176         UWORD8 u1_i;
   1177         for(u1_i = 0; u1_i < u1_part_wd; u1_i++, pu1_top_mv_ctxt += 4)
   1178         {
   1179             pu1_top_mv_ctxt[0] = u1_abs_mvd_x;
   1180             pu1_top_mv_ctxt[1] = u1_abs_mvd_y;
   1181         }
   1182 
   1183         for(u1_i = 0; u1_i < u1_part_ht; u1_i++, pu1_lft_mv_ctxt += 4)
   1184         {
   1185             pu1_lft_mv_ctxt[0] = u1_abs_mvd_x;
   1186             pu1_lft_mv_ctxt[1] = u1_abs_mvd_y;
   1187         }
   1188     }
   1189 }
   1190 
   1191 /*****************************************************************************/
   1192 /*                                                                           */
   1193 /*  Function Name : ih264d_parse_mvd_cabac                                                  */
   1194 /*                                                                           */
   1195 /*  Description   : This cabac function decodes the mvd in a given direction */
   1196 /*                  direction ( x or y ) as defined in 9.3.2.3.              */
   1197 /*                                                                           */
   1198 /*  Inputs        : 1. pointer to Bitstream                                  */
   1199 /*                  2. pointer to cabac decoding environmnet                 */
   1200 /*                  3. pointer to Mvd context                                */
   1201 /*                  4. abs(Top mvd) = u2_abs_mvd_b                           */
   1202 /*                  5. abs(left mvd)= u2_abs_mvd_a                           */
   1203 /*                                                                           */
   1204 /*  Processing    : see section 9.3.2.3 of the standard                      */
   1205 /*                                                                           */
   1206 /*  Outputs       : i2_mvd                                                   */
   1207 /*  Returns       : i2_mvd                                                   */
   1208 /*                                                                           */
   1209 /*  Issues        : none                                                     */
   1210 /*                                                                           */
   1211 /*  Revision History:                                                        */
   1212 /*                                                                           */
   1213 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
   1214 /*         16 06 2005   Jay          Draft                                   */
   1215 /*                                                                           */
   1216 /*****************************************************************************/
   1217 WORD16 ih264d_parse_mvd_cabac(dec_bit_stream_t * ps_bitstrm,
   1218                               decoding_envirnoment_t * ps_cab_env,
   1219                               bin_ctxt_model_t * p_ctxt_mvd,
   1220                               UWORD32 i4_temp)
   1221 
   1222 {
   1223     WORD8 k;
   1224     WORD16 i2_suf;
   1225     WORD16 i2_mvd;
   1226     UWORD16 u2_abs_mvd;
   1227     UWORD32 u4_ctx_inc;
   1228     UWORD32 u4_prefix;
   1229     const UWORD32 *pu4_table = (const UWORD32 *)ps_cab_env->cabac_table;
   1230     UWORD32 u4_code_int_range, u4_code_int_val_ofst;
   1231 
   1232     /*  if mvd < 9                                                  */
   1233     /*  mvd =  Prefix                                                   */
   1234     /*  else                                                            */
   1235     /*  mvd = Prefix + Suffix                                           */
   1236     /*  decode sign bit                                                 */
   1237     /*  Prefix TU decoding Cmax =Ucoff and Suffix 3rd order Exp-Golomb  */
   1238 
   1239     u2_abs_mvd = (UWORD16)i4_temp;
   1240     u4_ctx_inc = 1;
   1241 
   1242     if(u2_abs_mvd < 3)
   1243         u4_ctx_inc = 0;
   1244     else if(u2_abs_mvd > 32)
   1245         u4_ctx_inc = 2;
   1246 
   1247     u4_ctx_inc = (u4_ctx_inc | 0x65430);
   1248 
   1249     /*inlining modified version of ih264d_decode_bins_unary*/
   1250 
   1251     {
   1252         UWORD8 u1_max_bins = 9;
   1253         UWORD32 u4_value;
   1254         UWORD32 u4_symbol;
   1255         bin_ctxt_model_t *ps_bin_ctxt;
   1256         UWORD32 u4_ctx_Inc;
   1257 
   1258         u4_value = 0;
   1259         u4_code_int_range = ps_cab_env->u4_code_int_range;
   1260         u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
   1261 
   1262         do
   1263         {
   1264             u4_ctx_Inc = u4_ctx_inc & 0xf;
   1265             u4_ctx_inc = u4_ctx_inc >> 4;
   1266 
   1267             ps_bin_ctxt = p_ctxt_mvd + u4_ctx_Inc;
   1268 
   1269             DECODE_ONE_BIN_MACRO(ps_bin_ctxt, u4_code_int_range,
   1270                                  u4_code_int_val_ofst, pu4_table, ps_bitstrm,
   1271                                  u4_symbol)
   1272 
   1273             INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(ps_cab_env);
   1274 
   1275             u4_value++;
   1276 
   1277         }
   1278         while(u4_symbol && u4_value < 5);
   1279 
   1280         ps_bin_ctxt = p_ctxt_mvd + 6;
   1281 
   1282         if(u4_symbol && (u4_value < u1_max_bins))
   1283         {
   1284 
   1285             do
   1286             {
   1287 
   1288                 DECODE_ONE_BIN_MACRO(ps_bin_ctxt, u4_code_int_range,
   1289                                      u4_code_int_val_ofst, pu4_table,
   1290                                      ps_bitstrm, u4_symbol)
   1291 
   1292                 INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(ps_cab_env);
   1293                 u4_value++;
   1294             }
   1295             while(u4_symbol && (u4_value < u1_max_bins));
   1296 
   1297         }
   1298 
   1299         ps_cab_env->u4_code_int_range = u4_code_int_range;
   1300         ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
   1301         u4_value = u4_value - 1 + u4_symbol;
   1302         u4_prefix = (u4_value);
   1303     }
   1304 
   1305     i2_mvd = u4_prefix;
   1306 
   1307     if(i2_mvd == 9)
   1308     {
   1309         /* Read Suffix */
   1310         k = ih264d_decode_bypass_bins_unary(ps_cab_env, ps_bitstrm);
   1311         i2_suf = (1 << k) - 1;
   1312         k = k + 3;
   1313         i2_suf = (i2_suf << 3);
   1314         i2_mvd += i2_suf;
   1315         i2_suf = ih264d_decode_bypass_bins(ps_cab_env, k, ps_bitstrm);
   1316         i2_mvd += i2_suf;
   1317     }
   1318     /* Read Sign bit */
   1319     if(!i2_mvd)
   1320         return (i2_mvd);
   1321 
   1322     else
   1323     {
   1324         UWORD32 u4_code_int_val_ofst, u4_code_int_range;
   1325 
   1326         u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
   1327         u4_code_int_range = ps_cab_env->u4_code_int_range;
   1328 
   1329         if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_9)
   1330         {
   1331             UWORD32 *pu4_buffer, u4_offset;
   1332 
   1333             pu4_buffer = ps_bitstrm->pu4_buffer;
   1334             u4_offset = ps_bitstrm->u4_ofst;
   1335 
   1336             RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst,
   1337                                 u4_offset, pu4_buffer)
   1338             ps_bitstrm->u4_ofst = u4_offset;
   1339         }
   1340 
   1341         u4_code_int_range = u4_code_int_range >> 1;
   1342 
   1343         if(u4_code_int_val_ofst >= u4_code_int_range)
   1344         {
   1345             /* S=1 */
   1346             u4_code_int_val_ofst -= u4_code_int_range;
   1347             i2_mvd = (-i2_mvd);
   1348         }
   1349 
   1350         ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
   1351         ps_cab_env->u4_code_int_range = u4_code_int_range;
   1352 
   1353         return (i2_mvd);
   1354 
   1355     }
   1356 }
   1357