Home | History | Annotate | Download | only in source
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*------------------------------------------------------------------------------
     18 
     19     Table of contents
     20 
     21      1. Include headers
     22      2. External compiler flags
     23      3. Module defines
     24      4. Local function prototypes
     25      5. Functions
     26           h264bsdDecodeSliceHeader
     27           NumSliceGroupChangeCycleBits
     28           RefPicListReordering
     29           DecRefPicMarking
     30           CheckPpsId
     31           CheckFrameNum
     32           CheckIdrPicId
     33           CheckPicOrderCntLsb
     34           CheckDeltaPicOrderCntBottom
     35           CheckDeltaPicOrderCnt
     36           CheckRedundantPicCnt
     37 
     38 ------------------------------------------------------------------------------*/
     39 
     40 /*------------------------------------------------------------------------------
     41     1. Include headers
     42 ------------------------------------------------------------------------------*/
     43 
     44 #include "h264bsd_slice_header.h"
     45 #include "h264bsd_util.h"
     46 #include "h264bsd_vlc.h"
     47 #include "h264bsd_nal_unit.h"
     48 #include "h264bsd_dpb.h"
     49 
     50 #define UNUSED(x) (void)(x)
     51 
     52 /*------------------------------------------------------------------------------
     53     2. External compiler flags
     54 --------------------------------------------------------------------------------
     55 
     56 --------------------------------------------------------------------------------
     57     3. Module defines
     58 ------------------------------------------------------------------------------*/
     59 
     60 /*------------------------------------------------------------------------------
     61     4. Local function prototypes
     62 ------------------------------------------------------------------------------*/
     63 
     64 static u32 RefPicListReordering(strmData_t *, refPicListReordering_t *,
     65     u32, u32);
     66 
     67 static u32 NumSliceGroupChangeCycleBits(u32 picSizeInMbs,
     68     u32 sliceGroupChangeRate);
     69 
     70 static u32 DecRefPicMarking(strmData_t *pStrmData,
     71     decRefPicMarking_t *pDecRefPicMarking, nalUnitType_e nalUnitType,
     72     u32 numRefFrames);
     73 
     74 
     75 /*------------------------------------------------------------------------------
     76 
     77     Function name: h264bsdDecodeSliceHeader
     78 
     79         Functional description:
     80             Decode slice header data from the stream.
     81 
     82         Inputs:
     83             pStrmData       pointer to stream data structure
     84             pSeqParamSet    pointer to active sequence parameter set
     85             pPicParamSet    pointer to active picture parameter set
     86             pNalUnit        pointer to current NAL unit structure
     87 
     88         Outputs:
     89             pSliceHeader    decoded data is stored here
     90 
     91         Returns:
     92             HANTRO_OK       success
     93             HANTRO_NOK      invalid stream data or end of stream
     94 
     95 ------------------------------------------------------------------------------*/
     96 
     97 u32 h264bsdDecodeSliceHeader(strmData_t *pStrmData, sliceHeader_t *pSliceHeader,
     98     seqParamSet_t *pSeqParamSet, picParamSet_t *pPicParamSet,
     99     nalUnit_t *pNalUnit)
    100 {
    101 
    102 /* Variables */
    103 
    104     u32 tmp, i, value;
    105     i32 itmp;
    106     u32 picSizeInMbs;
    107 
    108 /* Code */
    109 
    110     ASSERT(pStrmData);
    111     ASSERT(pSliceHeader);
    112     ASSERT(pSeqParamSet);
    113     ASSERT(pPicParamSet);
    114     ASSERT( pNalUnit->nalUnitType == NAL_CODED_SLICE ||
    115             pNalUnit->nalUnitType == NAL_CODED_SLICE_IDR );
    116 
    117 
    118     H264SwDecMemset(pSliceHeader, 0, sizeof(sliceHeader_t));
    119 
    120     picSizeInMbs = pSeqParamSet->picWidthInMbs * pSeqParamSet->picHeightInMbs;
    121     tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    122     if (tmp != HANTRO_OK)
    123         return(tmp);
    124     pSliceHeader->firstMbInSlice = value;
    125     if (value >= picSizeInMbs)
    126     {
    127         EPRINT("first_mb_in_slice");
    128         return(HANTRO_NOK);
    129     }
    130 
    131     tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    132     if (tmp != HANTRO_OK)
    133         return(tmp);
    134     pSliceHeader->sliceType = value;
    135     /* slice type has to be either I or P slice. P slice is not allowed when
    136      * current NAL unit is an IDR NAL unit or num_ref_frames is 0 */
    137     if ( !IS_I_SLICE(pSliceHeader->sliceType) &&
    138          ( !IS_P_SLICE(pSliceHeader->sliceType) ||
    139            IS_IDR_NAL_UNIT(pNalUnit) ||
    140            !pSeqParamSet->numRefFrames ) )
    141     {
    142         EPRINT("slice_type");
    143         return(HANTRO_NOK);
    144     }
    145 
    146     tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    147     if (tmp != HANTRO_OK)
    148         return(tmp);
    149     pSliceHeader->picParameterSetId = value;
    150     if (pSliceHeader->picParameterSetId != pPicParamSet->picParameterSetId)
    151     {
    152         EPRINT("pic_parameter_set_id");
    153         return(HANTRO_NOK);
    154     }
    155 
    156     /* log2(maxFrameNum) -> num bits to represent frame_num */
    157     i = 0;
    158     while (pSeqParamSet->maxFrameNum >> i)
    159         i++;
    160     i--;
    161 
    162     tmp = h264bsdGetBits(pStrmData, i);
    163     if (tmp == END_OF_STREAM)
    164         return(HANTRO_NOK);
    165     if (IS_IDR_NAL_UNIT(pNalUnit) && tmp != 0)
    166     {
    167         EPRINT("frame_num");
    168         return(HANTRO_NOK);
    169     }
    170     pSliceHeader->frameNum = tmp;
    171 
    172     if (IS_IDR_NAL_UNIT(pNalUnit))
    173     {
    174         tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    175         if (tmp != HANTRO_OK)
    176             return(tmp);
    177         pSliceHeader->idrPicId = value;
    178         if (value > 65535)
    179         {
    180             EPRINT("idr_pic_id");
    181             return(HANTRO_NOK);
    182         }
    183     }
    184 
    185     if (pSeqParamSet->picOrderCntType == 0)
    186     {
    187         /* log2(maxPicOrderCntLsb) -> num bits to represent pic_order_cnt_lsb */
    188         i = 0;
    189         while (pSeqParamSet->maxPicOrderCntLsb >> i)
    190             i++;
    191         i--;
    192 
    193         tmp = h264bsdGetBits(pStrmData, i);
    194         if (tmp == END_OF_STREAM)
    195             return(HANTRO_NOK);
    196         pSliceHeader->picOrderCntLsb = tmp;
    197 
    198         if (pPicParamSet->picOrderPresentFlag)
    199         {
    200             tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
    201             if (tmp != HANTRO_OK)
    202                 return(tmp);
    203             pSliceHeader->deltaPicOrderCntBottom = itmp;
    204         }
    205 
    206         /* check that picOrderCnt for IDR picture will be zero. See
    207          * DecodePicOrderCnt function to understand the logic here */
    208         if ( IS_IDR_NAL_UNIT(pNalUnit) &&
    209              ( (pSliceHeader->picOrderCntLsb >
    210                 pSeqParamSet->maxPicOrderCntLsb/2) ||
    211                 MIN((i32)pSliceHeader->picOrderCntLsb,
    212                     (i32)pSliceHeader->picOrderCntLsb +
    213                     pSliceHeader->deltaPicOrderCntBottom) != 0 ) )
    214         {
    215             return(HANTRO_NOK);
    216         }
    217     }
    218 
    219     if ( (pSeqParamSet->picOrderCntType == 1) &&
    220          !pSeqParamSet->deltaPicOrderAlwaysZeroFlag )
    221     {
    222         tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
    223         if (tmp != HANTRO_OK)
    224             return(tmp);
    225         pSliceHeader->deltaPicOrderCnt[0] = itmp;
    226 
    227         if (pPicParamSet->picOrderPresentFlag)
    228         {
    229             tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
    230             if (tmp != HANTRO_OK)
    231                 return(tmp);
    232             pSliceHeader->deltaPicOrderCnt[1] = itmp;
    233         }
    234 
    235         /* check that picOrderCnt for IDR picture will be zero. See
    236          * DecodePicOrderCnt function to understand the logic here */
    237         if ( IS_IDR_NAL_UNIT(pNalUnit) &&
    238              MIN(pSliceHeader->deltaPicOrderCnt[0],
    239                  pSliceHeader->deltaPicOrderCnt[0] +
    240                  pSeqParamSet->offsetForTopToBottomField +
    241                  pSliceHeader->deltaPicOrderCnt[1]) != 0)
    242         {
    243             return(HANTRO_NOK);
    244         }
    245     }
    246 
    247     if (pPicParamSet->redundantPicCntPresentFlag)
    248     {
    249         tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    250         if (tmp != HANTRO_OK)
    251             return(tmp);
    252         pSliceHeader->redundantPicCnt = value;
    253         if (value > 127)
    254         {
    255             EPRINT("redundant_pic_cnt");
    256             return(HANTRO_NOK);
    257         }
    258     }
    259 
    260     if (IS_P_SLICE(pSliceHeader->sliceType))
    261     {
    262         tmp = h264bsdGetBits(pStrmData, 1);
    263         if (tmp == END_OF_STREAM)
    264             return(HANTRO_NOK);
    265         pSliceHeader->numRefIdxActiveOverrideFlag = tmp;
    266 
    267         if (pSliceHeader->numRefIdxActiveOverrideFlag)
    268         {
    269             tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    270             if (tmp != HANTRO_OK)
    271                 return(tmp);
    272             if (value > 15)
    273             {
    274                 EPRINT("num_ref_idx_l0_active_minus1");
    275                 return(HANTRO_NOK);
    276             }
    277             pSliceHeader->numRefIdxL0Active = value + 1;
    278         }
    279         /* set numRefIdxL0Active from pic param set */
    280         else
    281         {
    282             /* if value (minus1) in picture parameter set exceeds 15 it should
    283              * have been overridden here */
    284             if (pPicParamSet->numRefIdxL0Active > 16)
    285             {
    286                 EPRINT("num_ref_idx_active_override_flag");
    287                 return(HANTRO_NOK);
    288             }
    289             pSliceHeader->numRefIdxL0Active = pPicParamSet->numRefIdxL0Active;
    290         }
    291     }
    292 
    293     if (IS_P_SLICE(pSliceHeader->sliceType))
    294     {
    295         tmp = RefPicListReordering(pStrmData,
    296             &pSliceHeader->refPicListReordering,
    297             pSliceHeader->numRefIdxL0Active,
    298             pSeqParamSet->maxFrameNum);
    299         if (tmp != HANTRO_OK)
    300             return(tmp);
    301     }
    302 
    303     if (pNalUnit->nalRefIdc != 0)
    304     {
    305         tmp = DecRefPicMarking(pStrmData, &pSliceHeader->decRefPicMarking,
    306             pNalUnit->nalUnitType, pSeqParamSet->numRefFrames);
    307         if (tmp != HANTRO_OK)
    308             return(tmp);
    309     }
    310 
    311     /* decode sliceQpDelta and check that initial QP for the slice will be on
    312      * the range [0, 51] */
    313     tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
    314     if (tmp != HANTRO_OK)
    315         return(tmp);
    316     pSliceHeader->sliceQpDelta = itmp;
    317     itmp += (i32)pPicParamSet->picInitQp;
    318     if ( (itmp < 0) || (itmp > 51) )
    319     {
    320         EPRINT("slice_qp_delta");
    321         return(HANTRO_NOK);
    322     }
    323 
    324     if (pPicParamSet->deblockingFilterControlPresentFlag)
    325     {
    326         tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    327         if (tmp != HANTRO_OK)
    328             return(tmp);
    329         pSliceHeader->disableDeblockingFilterIdc = value;
    330         if (pSliceHeader->disableDeblockingFilterIdc > 2)
    331         {
    332             EPRINT("disable_deblocking_filter_idc");
    333             return(HANTRO_NOK);
    334         }
    335 
    336         if (pSliceHeader->disableDeblockingFilterIdc != 1)
    337         {
    338             tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
    339             if (tmp != HANTRO_OK)
    340                 return(tmp);
    341             if ( (itmp < -6) || (itmp > 6) )
    342             {
    343                EPRINT("slice_alpha_c0_offset_div2");
    344                return(HANTRO_NOK);
    345             }
    346             pSliceHeader->sliceAlphaC0Offset = itmp * 2;
    347 
    348             tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
    349             if (tmp != HANTRO_OK)
    350                 return(tmp);
    351             if ( (itmp < -6) || (itmp > 6) )
    352             {
    353                EPRINT("slice_beta_offset_div2");
    354                return(HANTRO_NOK);
    355             }
    356             pSliceHeader->sliceBetaOffset = itmp * 2;
    357         }
    358     }
    359 
    360     if ( (pPicParamSet->numSliceGroups > 1) &&
    361          (pPicParamSet->sliceGroupMapType >= 3) &&
    362          (pPicParamSet->sliceGroupMapType <= 5) )
    363     {
    364         /* set tmp to number of bits used to represent slice_group_change_cycle
    365          * in the stream */
    366         tmp = NumSliceGroupChangeCycleBits(picSizeInMbs,
    367             pPicParamSet->sliceGroupChangeRate);
    368         value = h264bsdGetBits(pStrmData, tmp);
    369         if (value == END_OF_STREAM)
    370             return(HANTRO_NOK);
    371         pSliceHeader->sliceGroupChangeCycle = value;
    372 
    373         /* corresponds to tmp = Ceil(picSizeInMbs / sliceGroupChangeRate) */
    374         tmp = (picSizeInMbs + pPicParamSet->sliceGroupChangeRate - 1) /
    375               pPicParamSet->sliceGroupChangeRate;
    376         if (pSliceHeader->sliceGroupChangeCycle > tmp)
    377         {
    378             EPRINT("slice_group_change_cycle");
    379             return(HANTRO_NOK);
    380         }
    381     }
    382 
    383     return(HANTRO_OK);
    384 
    385 }
    386 
    387 /*------------------------------------------------------------------------------
    388 
    389     Function: NumSliceGroupChangeCycleBits
    390 
    391         Functional description:
    392             Determine number of bits needed to represent
    393             slice_group_change_cycle in the stream. The standard states that
    394             slice_group_change_cycle is represented by
    395                 Ceil( Log2( (picSizeInMbs / sliceGroupChangeRate) + 1) )
    396 
    397             bits. Division "/" in the equation is non-truncating division.
    398 
    399         Inputs:
    400             picSizeInMbs            picture size in macroblocks
    401             sliceGroupChangeRate
    402 
    403         Outputs:
    404             none
    405 
    406         Returns:
    407             number of bits needed
    408 
    409 ------------------------------------------------------------------------------*/
    410 
    411 u32 NumSliceGroupChangeCycleBits(u32 picSizeInMbs, u32 sliceGroupChangeRate)
    412 {
    413 
    414 /* Variables */
    415 
    416     u32 tmp,numBits,mask;
    417 
    418 /* Code */
    419 
    420     ASSERT(picSizeInMbs);
    421     ASSERT(sliceGroupChangeRate);
    422     ASSERT(sliceGroupChangeRate <= picSizeInMbs);
    423 
    424     /* compute (picSizeInMbs / sliceGroupChangeRate + 1), rounded up */
    425     if (picSizeInMbs % sliceGroupChangeRate)
    426         tmp = 2 + picSizeInMbs/sliceGroupChangeRate;
    427     else
    428         tmp = 1 + picSizeInMbs/sliceGroupChangeRate;
    429 
    430     numBits = 0;
    431     mask = ~0U;
    432 
    433     /* set numBits to position of right-most non-zero bit */
    434     while (tmp & (mask<<++numBits))
    435         ;
    436     numBits--;
    437 
    438     /* add one more bit if value greater than 2^numBits */
    439     if (tmp & ((1<<numBits)-1))
    440         numBits++;
    441 
    442     return(numBits);
    443 
    444 }
    445 
    446 /*------------------------------------------------------------------------------
    447 
    448     Function: RefPicListReordering
    449 
    450         Functional description:
    451             Decode reference picture list reordering syntax elements from
    452             the stream. Max number of reordering commands is numRefIdxActive.
    453 
    454         Inputs:
    455             pStrmData       pointer to stream data structure
    456             numRefIdxActive number of active reference indices to be used for
    457                             current slice
    458             maxPicNum       maxFrameNum from the active SPS
    459 
    460         Outputs:
    461             pRefPicListReordering   decoded data is stored here
    462 
    463         Returns:
    464             HANTRO_OK       success
    465             HANTRO_NOK      invalid stream data
    466 
    467 ------------------------------------------------------------------------------*/
    468 
    469 u32 RefPicListReordering(strmData_t *pStrmData,
    470     refPicListReordering_t *pRefPicListReordering, u32 numRefIdxActive,
    471     u32 maxPicNum)
    472 {
    473 
    474 /* Variables */
    475 
    476     u32 tmp, value, i;
    477     u32 command;
    478 
    479 /* Code */
    480 
    481     ASSERT(pStrmData);
    482     ASSERT(pRefPicListReordering);
    483     ASSERT(numRefIdxActive);
    484     ASSERT(maxPicNum);
    485 
    486 
    487     tmp = h264bsdGetBits(pStrmData, 1);
    488     if (tmp == END_OF_STREAM)
    489         return(HANTRO_NOK);
    490 
    491     pRefPicListReordering->refPicListReorderingFlagL0 = tmp;
    492 
    493     if (pRefPicListReordering->refPicListReorderingFlagL0)
    494     {
    495         i = 0;
    496 
    497         do
    498         {
    499             if (i > numRefIdxActive)
    500             {
    501                 EPRINT("Too many reordering commands");
    502                 return(HANTRO_NOK);
    503             }
    504 
    505             tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &command);
    506             if (tmp != HANTRO_OK)
    507                 return(tmp);
    508             if (command > 3)
    509             {
    510                 EPRINT("reordering_of_pic_nums_idc");
    511                 return(HANTRO_NOK);
    512             }
    513 
    514             pRefPicListReordering->command[i].reorderingOfPicNumsIdc = command;
    515 
    516             if ((command == 0) || (command == 1))
    517             {
    518                 tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    519                 if (tmp != HANTRO_OK)
    520                     return(tmp);
    521                 if (value >= maxPicNum)
    522                 {
    523                     EPRINT("abs_diff_pic_num_minus1");
    524                     return(HANTRO_NOK);
    525                 }
    526                 pRefPicListReordering->command[i].absDiffPicNum = value + 1;
    527                             }
    528             else if (command == 2)
    529             {
    530                 tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    531                 if (tmp != HANTRO_OK)
    532                     return(tmp);
    533                 pRefPicListReordering->command[i].longTermPicNum = value;
    534                             }
    535             i++;
    536         } while (command != 3);
    537 
    538         /* there shall be at least one reordering command if
    539          * refPicListReorderingFlagL0 was set */
    540         if (i == 1)
    541         {
    542             EPRINT("ref_pic_list_reordering");
    543             return(HANTRO_NOK);
    544         }
    545     }
    546 
    547     return(HANTRO_OK);
    548 
    549 }
    550 
    551 /*------------------------------------------------------------------------------
    552 
    553     Function: DecRefPicMarking
    554 
    555         Functional description:
    556             Decode decoded reference picture marking syntax elements from
    557             the stream.
    558 
    559         Inputs:
    560             pStrmData       pointer to stream data structure
    561             nalUnitType     type of the current NAL unit
    562             numRefFrames    max number of reference frames from the active SPS
    563 
    564         Outputs:
    565             pDecRefPicMarking   decoded data is stored here
    566 
    567         Returns:
    568             HANTRO_OK       success
    569             HANTRO_NOK      invalid stream data
    570 
    571 ------------------------------------------------------------------------------*/
    572 
    573 u32 DecRefPicMarking(strmData_t *pStrmData,
    574     decRefPicMarking_t *pDecRefPicMarking, nalUnitType_e nalUnitType,
    575     u32 numRefFrames)
    576 {
    577 
    578 /* Variables */
    579 
    580     u32 tmp, value;
    581     u32 i;
    582     u32 operation;
    583     /* variables for error checking purposes, store number of memory
    584      * management operations of certain type */
    585     u32 num4 = 0, num5 = 0, num6 = 0, num1to3 = 0;
    586 
    587 /* Code */
    588 
    589     ASSERT( nalUnitType == NAL_CODED_SLICE_IDR ||
    590             nalUnitType == NAL_CODED_SLICE ||
    591             nalUnitType == NAL_SEI );
    592 
    593 
    594     if (nalUnitType == NAL_CODED_SLICE_IDR)
    595     {
    596         tmp = h264bsdGetBits(pStrmData, 1);
    597         if (tmp == END_OF_STREAM)
    598             return(HANTRO_NOK);
    599         pDecRefPicMarking->noOutputOfPriorPicsFlag = tmp;
    600 
    601         tmp = h264bsdGetBits(pStrmData, 1);
    602         if (tmp == END_OF_STREAM)
    603             return(HANTRO_NOK);
    604         pDecRefPicMarking->longTermReferenceFlag = tmp;
    605         if (!numRefFrames && pDecRefPicMarking->longTermReferenceFlag)
    606         {
    607             EPRINT("long_term_reference_flag");
    608             return(HANTRO_NOK);
    609         }
    610     }
    611     else
    612     {
    613         tmp = h264bsdGetBits(pStrmData, 1);
    614         if (tmp == END_OF_STREAM)
    615             return(HANTRO_NOK);
    616         pDecRefPicMarking->adaptiveRefPicMarkingModeFlag = tmp;
    617         if (pDecRefPicMarking->adaptiveRefPicMarkingModeFlag)
    618         {
    619             i = 0;
    620             do
    621             {
    622                 /* see explanation of the MAX_NUM_MMC_OPERATIONS in
    623                  * slice_header.h */
    624                 if (i > (2 * numRefFrames + 2))
    625                 {
    626                     EPRINT("Too many management operations");
    627                     return(HANTRO_NOK);
    628                 }
    629 
    630                 tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &operation);
    631                 if (tmp != HANTRO_OK)
    632                     return(tmp);
    633                 if (operation > 6)
    634                 {
    635                     EPRINT("memory_management_control_operation");
    636                     return(HANTRO_NOK);
    637                 }
    638 
    639                 pDecRefPicMarking->operation[i].
    640                     memoryManagementControlOperation = operation;
    641                 if ((operation == 1) || (operation == 3))
    642                 {
    643                     tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    644                     if (tmp != HANTRO_OK)
    645                         return(tmp);
    646                     pDecRefPicMarking->operation[i].differenceOfPicNums =
    647                         value + 1;
    648                 }
    649                 if (operation == 2)
    650                 {
    651                     tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    652                     if (tmp != HANTRO_OK)
    653                         return(tmp);
    654                     pDecRefPicMarking->operation[i].longTermPicNum = value;
    655                 }
    656                 if ((operation == 3) || (operation == 6))
    657                 {
    658                     tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    659                     if (tmp != HANTRO_OK)
    660                         return(tmp);
    661                     pDecRefPicMarking->operation[i].longTermFrameIdx =
    662                         value;
    663                 }
    664                 if (operation == 4)
    665                 {
    666                     tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
    667                     if (tmp != HANTRO_OK)
    668                         return(tmp);
    669                     /* value shall be in range [0, numRefFrames] */
    670                     if (value > numRefFrames)
    671                     {
    672                         EPRINT("max_long_term_frame_idx_plus1");
    673                         return(HANTRO_NOK);
    674                     }
    675                     if (value == 0)
    676                     {
    677                         pDecRefPicMarking->operation[i].
    678                             maxLongTermFrameIdx =
    679                             NO_LONG_TERM_FRAME_INDICES;
    680                     }
    681                     else
    682                     {
    683                         pDecRefPicMarking->operation[i].
    684                             maxLongTermFrameIdx = value - 1;
    685                     }
    686                     num4++;
    687                 }
    688                 if (operation == 5)
    689                 {
    690                     num5++;
    691                 }
    692                 if (operation && operation <= 3)
    693                     num1to3++;
    694                 if (operation == 6)
    695                     num6++;
    696 
    697                 i++;
    698             } while (operation != 0);
    699 
    700             /* error checking */
    701             if (num4 > 1 || num5 > 1 || num6 > 1 || (num1to3 && num5))
    702                 return(HANTRO_NOK);
    703 
    704         }
    705     }
    706 
    707     return(HANTRO_OK);
    708 }
    709 
    710 /*------------------------------------------------------------------------------
    711 
    712     Function name: h264bsdCheckPpsId
    713 
    714         Functional description:
    715             Peek value of pic_parameter_set_id from the slice header. Function
    716             does not modify current stream positions but copies the stream
    717             data structure to tmp structure which is used while accessing
    718             stream data.
    719 
    720         Inputs:
    721             pStrmData       pointer to stream data structure
    722 
    723         Outputs:
    724             picParamSetId   value is stored here
    725 
    726         Returns:
    727             HANTRO_OK       success
    728             HANTRO_NOK      invalid stream data
    729 
    730 ------------------------------------------------------------------------------*/
    731 
    732 u32 h264bsdCheckPpsId(strmData_t *pStrmData, u32 *picParamSetId)
    733 {
    734 
    735 /* Variables */
    736 
    737     u32 tmp, value;
    738     strmData_t tmpStrmData[1];
    739 
    740 /* Code */
    741 
    742     ASSERT(pStrmData);
    743 
    744     /* don't touch original stream position params */
    745     *tmpStrmData = *pStrmData;
    746 
    747     /* first_mb_in_slice */
    748     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    749     if (tmp != HANTRO_OK)
    750         return(tmp);
    751 
    752     /* slice_type */
    753     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    754     if (tmp != HANTRO_OK)
    755         return(tmp);
    756 
    757     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    758     if (tmp != HANTRO_OK)
    759         return(tmp);
    760     if (value >= MAX_NUM_PIC_PARAM_SETS)
    761         return(HANTRO_NOK);
    762 
    763     *picParamSetId = value;
    764 
    765     return(HANTRO_OK);
    766 
    767 }
    768 
    769 /*------------------------------------------------------------------------------
    770 
    771     Function: h264bsdCheckFrameNum
    772 
    773         Functional description:
    774             Peek value of frame_num from the slice header. Function does not
    775             modify current stream positions but copies the stream data
    776             structure to tmp structure which is used while accessing stream
    777             data.
    778 
    779         Inputs:
    780             pStrmData       pointer to stream data structure
    781             maxFrameNum
    782 
    783         Outputs:
    784             frameNum        value is stored here
    785 
    786         Returns:
    787             HANTRO_OK       success
    788             HANTRO_NOK      invalid stream data
    789 
    790 ------------------------------------------------------------------------------*/
    791 
    792 u32 h264bsdCheckFrameNum(
    793   strmData_t *pStrmData,
    794   u32 maxFrameNum,
    795   u32 *frameNum)
    796 {
    797 
    798 /* Variables */
    799 
    800     u32 tmp, value, i;
    801     strmData_t tmpStrmData[1];
    802 
    803 /* Code */
    804 
    805     ASSERT(pStrmData);
    806     ASSERT(maxFrameNum);
    807     ASSERT(frameNum);
    808 
    809     /* don't touch original stream position params */
    810     *tmpStrmData = *pStrmData;
    811 
    812     /* skip first_mb_in_slice */
    813     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    814     if (tmp != HANTRO_OK)
    815         return(tmp);
    816 
    817     /* skip slice_type */
    818     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    819     if (tmp != HANTRO_OK)
    820         return(tmp);
    821 
    822     /* skip pic_parameter_set_id */
    823     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    824     if (tmp != HANTRO_OK)
    825         return(tmp);
    826 
    827     /* log2(maxFrameNum) -> num bits to represent frame_num */
    828     i = 0;
    829     while (maxFrameNum >> i)
    830         i++;
    831     i--;
    832 
    833     /* frame_num */
    834     tmp = h264bsdGetBits(tmpStrmData, i);
    835     if (tmp == END_OF_STREAM)
    836         return(HANTRO_NOK);
    837     *frameNum = tmp;
    838 
    839     return(HANTRO_OK);
    840 
    841 }
    842 
    843 /*------------------------------------------------------------------------------
    844 
    845     Function: h264bsdCheckIdrPicId
    846 
    847         Functional description:
    848             Peek value of idr_pic_id from the slice header. Function does not
    849             modify current stream positions but copies the stream data
    850             structure to tmp structure which is used while accessing stream
    851             data.
    852 
    853         Inputs:
    854             pStrmData       pointer to stream data structure
    855             maxFrameNum     max frame number from active SPS
    856             nalUnitType     type of the current NAL unit
    857 
    858         Outputs:
    859             idrPicId        value is stored here
    860 
    861         Returns:
    862             HANTRO_OK       success
    863             HANTRO_NOK      invalid stream data
    864 
    865 ------------------------------------------------------------------------------*/
    866 
    867 u32 h264bsdCheckIdrPicId(
    868   strmData_t *pStrmData,
    869   u32 maxFrameNum,
    870   nalUnitType_e nalUnitType,
    871   u32 *idrPicId)
    872 {
    873 
    874 /* Variables */
    875 
    876     u32 tmp, value, i;
    877     strmData_t tmpStrmData[1];
    878 
    879 /* Code */
    880 
    881     ASSERT(pStrmData);
    882     ASSERT(maxFrameNum);
    883     ASSERT(idrPicId);
    884 
    885     /* nalUnitType must be equal to 5 because otherwise idrPicId is not
    886      * present */
    887     if (nalUnitType != NAL_CODED_SLICE_IDR)
    888         return(HANTRO_NOK);
    889 
    890     /* don't touch original stream position params */
    891     *tmpStrmData = *pStrmData;
    892 
    893     /* skip first_mb_in_slice */
    894     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    895     if (tmp != HANTRO_OK)
    896         return(tmp);
    897 
    898     /* skip slice_type */
    899     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    900     if (tmp != HANTRO_OK)
    901         return(tmp);
    902 
    903     /* skip pic_parameter_set_id */
    904     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    905     if (tmp != HANTRO_OK)
    906         return(tmp);
    907 
    908     /* log2(maxFrameNum) -> num bits to represent frame_num */
    909     i = 0;
    910     while (maxFrameNum >> i)
    911         i++;
    912     i--;
    913 
    914     /* skip frame_num */
    915     tmp = h264bsdGetBits(tmpStrmData, i);
    916     if (tmp == END_OF_STREAM)
    917         return(HANTRO_NOK);
    918 
    919     /* idr_pic_id */
    920     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, idrPicId);
    921     if (tmp != HANTRO_OK)
    922         return(tmp);
    923 
    924     return(HANTRO_OK);
    925 
    926 }
    927 
    928 /*------------------------------------------------------------------------------
    929 
    930     Function: h264bsdCheckPicOrderCntLsb
    931 
    932         Functional description:
    933             Peek value of pic_order_cnt_lsb from the slice header. Function
    934             does not modify current stream positions but copies the stream
    935             data structure to tmp structure which is used while accessing
    936             stream data.
    937 
    938         Inputs:
    939             pStrmData       pointer to stream data structure
    940             pSeqParamSet    pointer to active SPS
    941             nalUnitType     type of the current NAL unit
    942 
    943         Outputs:
    944             picOrderCntLsb  value is stored here
    945 
    946         Returns:
    947             HANTRO_OK       success
    948             HANTRO_NOK      invalid stream data
    949 
    950 ------------------------------------------------------------------------------*/
    951 
    952 u32 h264bsdCheckPicOrderCntLsb(
    953   strmData_t *pStrmData,
    954   seqParamSet_t *pSeqParamSet,
    955   nalUnitType_e nalUnitType,
    956   u32 *picOrderCntLsb)
    957 {
    958 
    959 /* Variables */
    960 
    961     u32 tmp, value, i;
    962     strmData_t tmpStrmData[1];
    963 
    964 /* Code */
    965 
    966     ASSERT(pStrmData);
    967     ASSERT(pSeqParamSet);
    968     ASSERT(picOrderCntLsb);
    969 
    970     /* picOrderCntType must be equal to 0 */
    971     ASSERT(pSeqParamSet->picOrderCntType == 0);
    972     ASSERT(pSeqParamSet->maxFrameNum);
    973     ASSERT(pSeqParamSet->maxPicOrderCntLsb);
    974 
    975     /* don't touch original stream position params */
    976     *tmpStrmData = *pStrmData;
    977 
    978     /* skip first_mb_in_slice */
    979     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    980     if (tmp != HANTRO_OK)
    981         return(tmp);
    982 
    983     /* skip slice_type */
    984     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    985     if (tmp != HANTRO_OK)
    986         return(tmp);
    987 
    988     /* skip pic_parameter_set_id */
    989     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
    990     if (tmp != HANTRO_OK)
    991         return(tmp);
    992 
    993     /* log2(maxFrameNum) -> num bits to represent frame_num */
    994     i = 0;
    995     while (pSeqParamSet->maxFrameNum >> i)
    996         i++;
    997     i--;
    998 
    999     /* skip frame_num */
   1000     tmp = h264bsdGetBits(tmpStrmData, i);
   1001     if (tmp == END_OF_STREAM)
   1002         return(HANTRO_NOK);
   1003 
   1004     /* skip idr_pic_id when necessary */
   1005     if (nalUnitType == NAL_CODED_SLICE_IDR)
   1006     {
   1007         tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1008         if (tmp != HANTRO_OK)
   1009             return(tmp);
   1010     }
   1011 
   1012     /* log2(maxPicOrderCntLsb) -> num bits to represent pic_order_cnt_lsb */
   1013     i = 0;
   1014     while (pSeqParamSet->maxPicOrderCntLsb >> i)
   1015         i++;
   1016     i--;
   1017 
   1018     /* pic_order_cnt_lsb */
   1019     tmp = h264bsdGetBits(tmpStrmData, i);
   1020     if (tmp == END_OF_STREAM)
   1021         return(HANTRO_NOK);
   1022     *picOrderCntLsb = tmp;
   1023 
   1024     return(HANTRO_OK);
   1025 
   1026 }
   1027 
   1028 /*------------------------------------------------------------------------------
   1029 
   1030     Function: h264bsdCheckDeltaPicOrderCntBottom
   1031 
   1032         Functional description:
   1033             Peek value of delta_pic_order_cnt_bottom from the slice header.
   1034             Function does not modify current stream positions but copies the
   1035             stream data structure to tmp structure which is used while
   1036             accessing stream data.
   1037 
   1038         Inputs:
   1039             pStrmData       pointer to stream data structure
   1040             pSeqParamSet    pointer to active SPS
   1041             nalUnitType     type of the current NAL unit
   1042 
   1043         Outputs:
   1044             deltaPicOrderCntBottom  value is stored here
   1045 
   1046         Returns:
   1047             HANTRO_OK       success
   1048             HANTRO_NOK      invalid stream data
   1049 
   1050 ------------------------------------------------------------------------------*/
   1051 
   1052 u32 h264bsdCheckDeltaPicOrderCntBottom(
   1053   strmData_t *pStrmData,
   1054   seqParamSet_t *pSeqParamSet,
   1055   nalUnitType_e nalUnitType,
   1056   i32 *deltaPicOrderCntBottom)
   1057 {
   1058 
   1059 /* Variables */
   1060 
   1061     u32 tmp, value, i;
   1062     strmData_t tmpStrmData[1];
   1063 
   1064 /* Code */
   1065 
   1066     ASSERT(pStrmData);
   1067     ASSERT(pSeqParamSet);
   1068     ASSERT(deltaPicOrderCntBottom);
   1069 
   1070     /* picOrderCntType must be equal to 0 and picOrderPresentFlag must be TRUE
   1071      * */
   1072     ASSERT(pSeqParamSet->picOrderCntType == 0);
   1073     ASSERT(pSeqParamSet->maxFrameNum);
   1074     ASSERT(pSeqParamSet->maxPicOrderCntLsb);
   1075 
   1076     /* don't touch original stream position params */
   1077     *tmpStrmData = *pStrmData;
   1078 
   1079     /* skip first_mb_in_slice */
   1080     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1081     if (tmp != HANTRO_OK)
   1082         return(tmp);
   1083 
   1084     /* skip slice_type */
   1085     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1086     if (tmp != HANTRO_OK)
   1087         return(tmp);
   1088 
   1089     /* skip pic_parameter_set_id */
   1090     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1091     if (tmp != HANTRO_OK)
   1092         return(tmp);
   1093 
   1094     /* log2(maxFrameNum) -> num bits to represent frame_num */
   1095     i = 0;
   1096     while (pSeqParamSet->maxFrameNum >> i)
   1097         i++;
   1098     i--;
   1099 
   1100     /* skip frame_num */
   1101     tmp = h264bsdGetBits(tmpStrmData, i);
   1102     if (tmp == END_OF_STREAM)
   1103         return(HANTRO_NOK);
   1104 
   1105     /* skip idr_pic_id when necessary */
   1106     if (nalUnitType == NAL_CODED_SLICE_IDR)
   1107     {
   1108         tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1109         if (tmp != HANTRO_OK)
   1110             return(tmp);
   1111     }
   1112 
   1113     /* log2(maxPicOrderCntLsb) -> num bits to represent pic_order_cnt_lsb */
   1114     i = 0;
   1115     while (pSeqParamSet->maxPicOrderCntLsb >> i)
   1116         i++;
   1117     i--;
   1118 
   1119     /* skip pic_order_cnt_lsb */
   1120     tmp = h264bsdGetBits(tmpStrmData, i);
   1121     if (tmp == END_OF_STREAM)
   1122         return(HANTRO_NOK);
   1123 
   1124     /* delta_pic_order_cnt_bottom */
   1125     tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, deltaPicOrderCntBottom);
   1126     if (tmp != HANTRO_OK)
   1127         return(tmp);
   1128 
   1129     return(HANTRO_OK);
   1130 
   1131 }
   1132 
   1133 /*------------------------------------------------------------------------------
   1134 
   1135     Function: h264bsdCheckDeltaPicOrderCnt
   1136 
   1137         Functional description:
   1138             Peek values delta_pic_order_cnt[0] and delta_pic_order_cnt[1]
   1139             from the slice header. Function does not modify current stream
   1140             positions but copies the stream data structure to tmp structure
   1141             which is used while accessing stream data.
   1142 
   1143         Inputs:
   1144             pStrmData               pointer to stream data structure
   1145             pSeqParamSet            pointer to active SPS
   1146             nalUnitType             type of the current NAL unit
   1147             picOrderPresentFlag     flag indicating if delta_pic_order_cnt[1]
   1148                                     is present in the stream
   1149 
   1150         Outputs:
   1151             deltaPicOrderCnt        values are stored here
   1152 
   1153         Returns:
   1154             HANTRO_OK               success
   1155             HANTRO_NOK              invalid stream data
   1156 
   1157 ------------------------------------------------------------------------------*/
   1158 
   1159 u32 h264bsdCheckDeltaPicOrderCnt(
   1160   strmData_t *pStrmData,
   1161   seqParamSet_t *pSeqParamSet,
   1162   nalUnitType_e nalUnitType,
   1163   u32 picOrderPresentFlag,
   1164   i32 *deltaPicOrderCnt)
   1165 {
   1166 
   1167 /* Variables */
   1168 
   1169     u32 tmp, value, i;
   1170     strmData_t tmpStrmData[1];
   1171 
   1172 /* Code */
   1173 
   1174     ASSERT(pStrmData);
   1175     ASSERT(pSeqParamSet);
   1176     ASSERT(deltaPicOrderCnt);
   1177 
   1178     /* picOrderCntType must be equal to 1 and deltaPicOrderAlwaysZeroFlag must
   1179      * be FALSE */
   1180     ASSERT(pSeqParamSet->picOrderCntType == 1);
   1181     ASSERT(!pSeqParamSet->deltaPicOrderAlwaysZeroFlag);
   1182     ASSERT(pSeqParamSet->maxFrameNum);
   1183 
   1184     /* don't touch original stream position params */
   1185     *tmpStrmData = *pStrmData;
   1186 
   1187     /* skip first_mb_in_slice */
   1188     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1189     if (tmp != HANTRO_OK)
   1190         return(tmp);
   1191 
   1192     /* skip slice_type */
   1193     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1194     if (tmp != HANTRO_OK)
   1195         return(tmp);
   1196 
   1197     /* skip pic_parameter_set_id */
   1198     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1199     if (tmp != HANTRO_OK)
   1200         return(tmp);
   1201 
   1202     /* log2(maxFrameNum) -> num bits to represent frame_num */
   1203     i = 0;
   1204     while (pSeqParamSet->maxFrameNum >> i)
   1205         i++;
   1206     i--;
   1207 
   1208     /* skip frame_num */
   1209     tmp = h264bsdGetBits(tmpStrmData, i);
   1210     if (tmp == END_OF_STREAM)
   1211         return(HANTRO_NOK);
   1212 
   1213     /* skip idr_pic_id when necessary */
   1214     if (nalUnitType == NAL_CODED_SLICE_IDR)
   1215     {
   1216         tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1217         if (tmp != HANTRO_OK)
   1218             return(tmp);
   1219     }
   1220 
   1221     /* delta_pic_order_cnt[0] */
   1222     tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, &deltaPicOrderCnt[0]);
   1223     if (tmp != HANTRO_OK)
   1224         return(tmp);
   1225 
   1226     /* delta_pic_order_cnt[1] if present */
   1227     if (picOrderPresentFlag)
   1228     {
   1229         tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, &deltaPicOrderCnt[1]);
   1230         if (tmp != HANTRO_OK)
   1231             return(tmp);
   1232     }
   1233 
   1234     return(HANTRO_OK);
   1235 
   1236 }
   1237 
   1238 /*------------------------------------------------------------------------------
   1239 
   1240     Function: h264bsdCheckRedundantPicCnt
   1241 
   1242         Functional description:
   1243             Peek value of redundant_pic_cnt from the slice header. Function
   1244             does not modify current stream positions but copies the stream
   1245             data structure to tmp structure which is used while accessing
   1246             stream data.
   1247 
   1248         Inputs:
   1249             pStrmData       pointer to stream data structure
   1250             pSeqParamSet    pointer to active SPS
   1251             pPicParamSet    pointer to active PPS
   1252             nalUnitType     type of the current NAL unit
   1253 
   1254         Outputs:
   1255             redundantPicCnt value is stored here
   1256 
   1257         Returns:
   1258             HANTRO_OK       success
   1259             HANTRO_NOK      invalid stream data
   1260 
   1261 ------------------------------------------------------------------------------*/
   1262 
   1263 u32 h264bsdCheckRedundantPicCnt(
   1264   strmData_t *pStrmData,
   1265   seqParamSet_t *pSeqParamSet,
   1266   picParamSet_t *pPicParamSet,
   1267   nalUnitType_e nalUnitType,
   1268   u32 *redundantPicCnt)
   1269 {
   1270 
   1271 /* Variables */
   1272 
   1273     u32 tmp, value, i;
   1274     i32 ivalue;
   1275     strmData_t tmpStrmData[1];
   1276 
   1277 /* Code */
   1278 
   1279     ASSERT(pStrmData);
   1280     ASSERT(pSeqParamSet);
   1281     ASSERT(pPicParamSet);
   1282     ASSERT(redundantPicCnt);
   1283 
   1284     /* redundant_pic_cnt_flag must be TRUE */
   1285     ASSERT(pPicParamSet->redundantPicCntPresentFlag);
   1286     ASSERT(pSeqParamSet->maxFrameNum);
   1287     ASSERT(pSeqParamSet->picOrderCntType > 0 ||
   1288            pSeqParamSet->maxPicOrderCntLsb);
   1289 
   1290     /* don't touch original stream position params */
   1291     *tmpStrmData = *pStrmData;
   1292 
   1293     /* skip first_mb_in_slice */
   1294     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1295     if (tmp != HANTRO_OK)
   1296         return(tmp);
   1297 
   1298     /* skip slice_type */
   1299     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1300     if (tmp != HANTRO_OK)
   1301         return(tmp);
   1302 
   1303     /* skip pic_parameter_set_id */
   1304     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1305     if (tmp != HANTRO_OK)
   1306         return(tmp);
   1307 
   1308     /* log2(maxFrameNum) -> num bits to represent frame_num */
   1309     i = 0;
   1310     while (pSeqParamSet->maxFrameNum >> i)
   1311         i++;
   1312     i--;
   1313 
   1314     /* skip frame_num */
   1315     tmp = h264bsdGetBits(tmpStrmData, i);
   1316     if (tmp == END_OF_STREAM)
   1317         return(HANTRO_NOK);
   1318 
   1319     /* skip idr_pic_id when necessary */
   1320     if (nalUnitType == NAL_CODED_SLICE_IDR)
   1321     {
   1322         tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1323         if (tmp != HANTRO_OK)
   1324             return(tmp);
   1325     }
   1326 
   1327     if (pSeqParamSet->picOrderCntType == 0)
   1328     {
   1329         /* log2(maxPicOrderCntLsb) -> num bits to represent pic_order_cnt_lsb */
   1330         i = 0;
   1331         while (pSeqParamSet->maxPicOrderCntLsb >> i)
   1332             i++;
   1333         i--;
   1334 
   1335         /* pic_order_cnt_lsb */
   1336         tmp = h264bsdGetBits(tmpStrmData, i);
   1337         if (tmp == END_OF_STREAM)
   1338             return(HANTRO_NOK);
   1339 
   1340         if (pPicParamSet->picOrderPresentFlag)
   1341         {
   1342             /* skip delta_pic_order_cnt_bottom */
   1343             tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, &ivalue);
   1344             if (tmp != HANTRO_OK)
   1345                 return(tmp);
   1346         }
   1347     }
   1348 
   1349     if (pSeqParamSet->picOrderCntType == 1 &&
   1350       !pSeqParamSet->deltaPicOrderAlwaysZeroFlag)
   1351     {
   1352         /* delta_pic_order_cnt[0] */
   1353         tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, &ivalue);
   1354         if (tmp != HANTRO_OK)
   1355             return(tmp);
   1356 
   1357         /* delta_pic_order_cnt[1] if present */
   1358         if (pPicParamSet->picOrderPresentFlag)
   1359         {
   1360             tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, &ivalue);
   1361             if (tmp != HANTRO_OK)
   1362                 return(tmp);
   1363         }
   1364     }
   1365 
   1366     /* redundant_pic_cnt */
   1367     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, redundantPicCnt);
   1368     if (tmp != HANTRO_OK)
   1369         return(tmp);
   1370 
   1371     return(HANTRO_OK);
   1372 
   1373 }
   1374 
   1375 
   1376 /*------------------------------------------------------------------------------
   1377 
   1378     Function: h264bsdCheckPriorPicsFlag
   1379 
   1380         Functional description:
   1381             Peek value of no_output_of_prior_pics_flag from the slice header.
   1382             Function does not modify current stream positions but copies
   1383             the stream data structure to tmp structure which is used while
   1384             accessing stream data.
   1385 
   1386         Inputs:
   1387             pStrmData       pointer to stream data structure
   1388             pSeqParamSet    pointer to active SPS
   1389             pPicParamSet    pointer to active PPS
   1390             nalUnitType     type of the current NAL unit
   1391 
   1392         Outputs:
   1393             noOutputOfPriorPicsFlag value is stored here
   1394 
   1395         Returns:
   1396             HANTRO_OK       success
   1397             HANTRO_NOK      invalid stream data
   1398 
   1399 ------------------------------------------------------------------------------*/
   1400 /*lint -e715 disable lint info nalUnitType not referenced */
   1401 u32 h264bsdCheckPriorPicsFlag(u32 * noOutputOfPriorPicsFlag,
   1402                               const strmData_t * pStrmData,
   1403                               const seqParamSet_t * pSeqParamSet,
   1404                               const picParamSet_t * pPicParamSet,
   1405                               nalUnitType_e nalUnitType)
   1406 {
   1407 /* Variables */
   1408 
   1409     u32 tmp, value, i;
   1410     i32 ivalue;
   1411     strmData_t tmpStrmData[1];
   1412     UNUSED(nalUnitType);
   1413 
   1414 /* Code */
   1415 
   1416     ASSERT(pStrmData);
   1417     ASSERT(pSeqParamSet);
   1418     ASSERT(pPicParamSet);
   1419     ASSERT(noOutputOfPriorPicsFlag);
   1420 
   1421     /* must be IDR lsice */
   1422     ASSERT(nalUnitType == NAL_CODED_SLICE_IDR);
   1423 
   1424     /* don't touch original stream position params */
   1425     *tmpStrmData = *pStrmData;
   1426 
   1427     /* skip first_mb_in_slice */
   1428     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1429     if(tmp != HANTRO_OK)
   1430         return (tmp);
   1431 
   1432     /* slice_type */
   1433     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1434     if(tmp != HANTRO_OK)
   1435         return (tmp);
   1436 
   1437     /* skip pic_parameter_set_id */
   1438     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1439     if(tmp != HANTRO_OK)
   1440         return (tmp);
   1441 
   1442     /* log2(maxFrameNum) -> num bits to represent frame_num */
   1443     i = 0;
   1444     while(pSeqParamSet->maxFrameNum >> i)
   1445         i++;
   1446     i--;
   1447 
   1448     /* skip frame_num */
   1449     tmp = h264bsdGetBits(tmpStrmData, i);
   1450     if(tmp == END_OF_STREAM)
   1451         return (HANTRO_NOK);
   1452 
   1453     /* skip idr_pic_id */
   1454     tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1455     if(tmp != HANTRO_OK)
   1456         return (tmp);
   1457 
   1458     if(pSeqParamSet->picOrderCntType == 0)
   1459     {
   1460         /* log2(maxPicOrderCntLsb) -> num bits to represent pic_order_cnt_lsb */
   1461         i = 0;
   1462         while(pSeqParamSet->maxPicOrderCntLsb >> i)
   1463             i++;
   1464         i--;
   1465 
   1466         /* skip pic_order_cnt_lsb */
   1467         tmp = h264bsdGetBits(tmpStrmData, i);
   1468         if(tmp == END_OF_STREAM)
   1469             return (HANTRO_NOK);
   1470 
   1471         if(pPicParamSet->picOrderPresentFlag)
   1472         {
   1473             /* skip delta_pic_order_cnt_bottom */
   1474             tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, &ivalue);
   1475             if(tmp != HANTRO_OK)
   1476                 return (tmp);
   1477         }
   1478     }
   1479 
   1480     if(pSeqParamSet->picOrderCntType == 1 &&
   1481        !pSeqParamSet->deltaPicOrderAlwaysZeroFlag)
   1482     {
   1483         /* skip delta_pic_order_cnt[0] */
   1484         tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, &ivalue);
   1485         if(tmp != HANTRO_OK)
   1486             return (tmp);
   1487 
   1488         /* skip delta_pic_order_cnt[1] if present */
   1489         if(pPicParamSet->picOrderPresentFlag)
   1490         {
   1491             tmp = h264bsdDecodeExpGolombSigned(tmpStrmData, &ivalue);
   1492             if(tmp != HANTRO_OK)
   1493                 return (tmp);
   1494         }
   1495     }
   1496 
   1497     /* skip redundant_pic_cnt */
   1498     if(pPicParamSet->redundantPicCntPresentFlag)
   1499     {
   1500         tmp = h264bsdDecodeExpGolombUnsigned(tmpStrmData, &value);
   1501         if(tmp != HANTRO_OK)
   1502             return (tmp);
   1503     }
   1504 
   1505     *noOutputOfPriorPicsFlag = h264bsdGetBits(tmpStrmData, 1);
   1506     if(*noOutputOfPriorPicsFlag == END_OF_STREAM)
   1507         return (HANTRO_NOK);
   1508 
   1509     return (HANTRO_OK);
   1510 
   1511 }
   1512 /*lint +e715 */
   1513 
   1514 
   1515