10c1bc742181ded4930842b46e9507372f0b1b963James Dong/*
20c1bc742181ded4930842b46e9507372f0b1b963James Dong * Copyright (C) 2009 The Android Open Source Project
30c1bc742181ded4930842b46e9507372f0b1b963James Dong *
40c1bc742181ded4930842b46e9507372f0b1b963James Dong * Licensed under the Apache License, Version 2.0 (the "License");
50c1bc742181ded4930842b46e9507372f0b1b963James Dong * you may not use this file except in compliance with the License.
60c1bc742181ded4930842b46e9507372f0b1b963James Dong * You may obtain a copy of the License at
70c1bc742181ded4930842b46e9507372f0b1b963James Dong *
80c1bc742181ded4930842b46e9507372f0b1b963James Dong *      http://www.apache.org/licenses/LICENSE-2.0
90c1bc742181ded4930842b46e9507372f0b1b963James Dong *
100c1bc742181ded4930842b46e9507372f0b1b963James Dong * Unless required by applicable law or agreed to in writing, software
110c1bc742181ded4930842b46e9507372f0b1b963James Dong * distributed under the License is distributed on an "AS IS" BASIS,
120c1bc742181ded4930842b46e9507372f0b1b963James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130c1bc742181ded4930842b46e9507372f0b1b963James Dong * See the License for the specific language governing permissions and
140c1bc742181ded4930842b46e9507372f0b1b963James Dong * limitations under the License.
150c1bc742181ded4930842b46e9507372f0b1b963James Dong */
160c1bc742181ded4930842b46e9507372f0b1b963James Dong
170c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
180c1bc742181ded4930842b46e9507372f0b1b963James Dong
190c1bc742181ded4930842b46e9507372f0b1b963James Dong    Table of contents
200c1bc742181ded4930842b46e9507372f0b1b963James Dong
210c1bc742181ded4930842b46e9507372f0b1b963James Dong     1. Include headers
220c1bc742181ded4930842b46e9507372f0b1b963James Dong     2. External compiler flags
230c1bc742181ded4930842b46e9507372f0b1b963James Dong     3. Module defines
240c1bc742181ded4930842b46e9507372f0b1b963James Dong     4. Local function prototypes
250c1bc742181ded4930842b46e9507372f0b1b963James Dong     5. Functions
260c1bc742181ded4930842b46e9507372f0b1b963James Dong          h264bsdDecodeSeqParamSet
270c1bc742181ded4930842b46e9507372f0b1b963James Dong          GetDpbSize
280c1bc742181ded4930842b46e9507372f0b1b963James Dong          h264bsdCompareSeqParamSets
290c1bc742181ded4930842b46e9507372f0b1b963James Dong
300c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
310c1bc742181ded4930842b46e9507372f0b1b963James Dong
320c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
330c1bc742181ded4930842b46e9507372f0b1b963James Dong    1. Include headers
340c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
350c1bc742181ded4930842b46e9507372f0b1b963James Dong
360c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_seq_param_set.h"
370c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_util.h"
380c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_vlc.h"
390c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_vui.h"
400c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_cfg.h"
410c1bc742181ded4930842b46e9507372f0b1b963James Dong
420c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
430c1bc742181ded4930842b46e9507372f0b1b963James Dong    2. External compiler flags
440c1bc742181ded4930842b46e9507372f0b1b963James Dong--------------------------------------------------------------------------------
450c1bc742181ded4930842b46e9507372f0b1b963James Dong
460c1bc742181ded4930842b46e9507372f0b1b963James Dong--------------------------------------------------------------------------------
470c1bc742181ded4930842b46e9507372f0b1b963James Dong    3. Module defines
480c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
490c1bc742181ded4930842b46e9507372f0b1b963James Dong
500c1bc742181ded4930842b46e9507372f0b1b963James Dong/* enumeration to indicate invalid return value from the GetDpbSize function */
510c1bc742181ded4930842b46e9507372f0b1b963James Dongenum {INVALID_DPB_SIZE = 0x7FFFFFFF};
520c1bc742181ded4930842b46e9507372f0b1b963James Dong
530c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
540c1bc742181ded4930842b46e9507372f0b1b963James Dong    4. Local function prototypes
550c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
560c1bc742181ded4930842b46e9507372f0b1b963James Dong
570c1bc742181ded4930842b46e9507372f0b1b963James Dongstatic u32 GetDpbSize(u32 picSizeInMbs, u32 levelIdc);
580c1bc742181ded4930842b46e9507372f0b1b963James Dong
590c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
600c1bc742181ded4930842b46e9507372f0b1b963James Dong
610c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function name: h264bsdDecodeSeqParamSet
620c1bc742181ded4930842b46e9507372f0b1b963James Dong
630c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
640c1bc742181ded4930842b46e9507372f0b1b963James Dong            Decode sequence parameter set information from the stream.
650c1bc742181ded4930842b46e9507372f0b1b963James Dong
660c1bc742181ded4930842b46e9507372f0b1b963James Dong            Function allocates memory for offsetForRefFrame array if
670c1bc742181ded4930842b46e9507372f0b1b963James Dong            picture order count type is 1 and numRefFramesInPicOrderCntCycle
680c1bc742181ded4930842b46e9507372f0b1b963James Dong            is greater than zero.
690c1bc742181ded4930842b46e9507372f0b1b963James Dong
700c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
710c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStrmData       pointer to stream data structure
720c1bc742181ded4930842b46e9507372f0b1b963James Dong
730c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
740c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSeqParamSet    decoded information is stored here
750c1bc742181ded4930842b46e9507372f0b1b963James Dong
760c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
770c1bc742181ded4930842b46e9507372f0b1b963James Dong            HANTRO_OK       success
780c1bc742181ded4930842b46e9507372f0b1b963James Dong            HANTRO_NOK      failure, invalid information or end of stream
790c1bc742181ded4930842b46e9507372f0b1b963James Dong            MEMORY_ALLOCATION_ERROR for memory allocation failure
800c1bc742181ded4930842b46e9507372f0b1b963James Dong
810c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
820c1bc742181ded4930842b46e9507372f0b1b963James Dong
830c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdDecodeSeqParamSet(strmData_t *pStrmData, seqParamSet_t *pSeqParamSet)
840c1bc742181ded4930842b46e9507372f0b1b963James Dong{
850c1bc742181ded4930842b46e9507372f0b1b963James Dong
860c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Variables */
870c1bc742181ded4930842b46e9507372f0b1b963James Dong
880c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 tmp, i, value;
890c1bc742181ded4930842b46e9507372f0b1b963James Dong
900c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Code */
910c1bc742181ded4930842b46e9507372f0b1b963James Dong
920c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pStrmData);
930c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pSeqParamSet);
940c1bc742181ded4930842b46e9507372f0b1b963James Dong
950c1bc742181ded4930842b46e9507372f0b1b963James Dong    H264SwDecMemset(pSeqParamSet, 0, sizeof(seqParamSet_t));
960c1bc742181ded4930842b46e9507372f0b1b963James Dong
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* profile_idc */
980c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 8);
990c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp != 66)
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEBUG(("NOT BASELINE PROFILE %d\n", tmp));
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->profileIdc = tmp;
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* constrained_set0_flag */
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 1);
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* constrained_set1_flag */
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 1);
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* constrained_set2_flag */
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 1);
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* reserved_zero_5bits, values of these bits shall be ignored */
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 5);
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 8);
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->levelIdc = tmp;
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong        &pSeqParamSet->seqParameterSetId);
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp != HANTRO_OK)
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(tmp);
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pSeqParamSet->seqParameterSetId >= MAX_NUM_SEQ_PARAM_SETS)
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong        EPRINT("seq_param_set_id");
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* log2_max_frame_num_minus4 */
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp != HANTRO_OK)
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(tmp);
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (value > 12)
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong        EPRINT("log2_max_frame_num_minus4");
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* maxFrameNum = 2^(log2_max_frame_num_minus4 + 4) */
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->maxFrameNum = 1 << (value+4);
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* valid POC types are 0, 1 and 2 */
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp != HANTRO_OK)
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(tmp);
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (value > 2)
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong        EPRINT("pic_order_cnt_type");
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->picOrderCntType = value;
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pSeqParamSet->picOrderCntType == 0)
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* log2_max_pic_order_cnt_lsb_minus4 */
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (value > 12)
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong            EPRINT("log2_max_pic_order_cnt_lsb_minus4");
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(HANTRO_NOK);
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* maxPicOrderCntLsb = 2^(log2_max_pic_order_cnt_lsb_minus4 + 4) */
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSeqParamSet->maxPicOrderCntLsb = 1 << (value+4);
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong    else if (pSeqParamSet->picOrderCntType == 1)
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdGetBits(pStrmData, 1);
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp == END_OF_STREAM)
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(HANTRO_NOK);
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSeqParamSet->deltaPicOrderAlwaysZeroFlag = (tmp == 1) ?
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong                                        HANTRO_TRUE : HANTRO_FALSE;
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeExpGolombSigned(pStrmData,
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong            &pSeqParamSet->offsetForNonRefPic);
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeExpGolombSigned(pStrmData,
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong            &pSeqParamSet->offsetForTopToBottomField);
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong            &pSeqParamSet->numRefFramesInPicOrderCntCycle);
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (pSeqParamSet->numRefFramesInPicOrderCntCycle > 255)
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong            EPRINT("num_ref_frames_in_pic_order_cnt_cycle");
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(HANTRO_NOK);
2000c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (pSeqParamSet->numRefFramesInPicOrderCntCycle)
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
2040c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* NOTE: This has to be freed somewhere! */
2050c1bc742181ded4930842b46e9507372f0b1b963James Dong            ALLOCATE(pSeqParamSet->offsetForRefFrame,
2060c1bc742181ded4930842b46e9507372f0b1b963James Dong                     pSeqParamSet->numRefFramesInPicOrderCntCycle, i32);
2070c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (pSeqParamSet->offsetForRefFrame == NULL)
2080c1bc742181ded4930842b46e9507372f0b1b963James Dong                return(MEMORY_ALLOCATION_ERROR);
2090c1bc742181ded4930842b46e9507372f0b1b963James Dong
2100c1bc742181ded4930842b46e9507372f0b1b963James Dong            for (i = 0; i < pSeqParamSet->numRefFramesInPicOrderCntCycle; i++)
2110c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
2120c1bc742181ded4930842b46e9507372f0b1b963James Dong                tmp =  h264bsdDecodeExpGolombSigned(pStrmData,
2130c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSeqParamSet->offsetForRefFrame + i);
2140c1bc742181ded4930842b46e9507372f0b1b963James Dong                if (tmp != HANTRO_OK)
2150c1bc742181ded4930842b46e9507372f0b1b963James Dong                    return(tmp);
2160c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
2170c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
2180c1bc742181ded4930842b46e9507372f0b1b963James Dong        else
2190c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
2200c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSeqParamSet->offsetForRefFrame = NULL;
2210c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
2220c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2230c1bc742181ded4930842b46e9507372f0b1b963James Dong
2240c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
2250c1bc742181ded4930842b46e9507372f0b1b963James Dong        &pSeqParamSet->numRefFrames);
2260c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp != HANTRO_OK)
2270c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(tmp);
2280c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pSeqParamSet->numRefFrames > MAX_NUM_REF_PICS)
2290c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2300c1bc742181ded4930842b46e9507372f0b1b963James Dong        EPRINT("num_ref_frames");
2310c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
2320c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2330c1bc742181ded4930842b46e9507372f0b1b963James Dong
2340c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 1);
2350c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
2360c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
2370c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->gapsInFrameNumValueAllowedFlag = (tmp == 1) ?
2380c1bc742181ded4930842b46e9507372f0b1b963James Dong                                        HANTRO_TRUE : HANTRO_FALSE;
2390c1bc742181ded4930842b46e9507372f0b1b963James Dong
2400c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
2410c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp != HANTRO_OK)
2420c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(tmp);
2430c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->picWidthInMbs = value + 1;
2440c1bc742181ded4930842b46e9507372f0b1b963James Dong
2450c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
2460c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp != HANTRO_OK)
2470c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(tmp);
2480c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->picHeightInMbs = value + 1;
2490c1bc742181ded4930842b46e9507372f0b1b963James Dong
2500c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* frame_mbs_only_flag, shall be 1 for baseline profile */
2510c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 1);
2520c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
2530c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
2540c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (!tmp)
2550c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2560c1bc742181ded4930842b46e9507372f0b1b963James Dong        EPRINT("frame_mbs_only_flag");
2570c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
2580c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2590c1bc742181ded4930842b46e9507372f0b1b963James Dong
2600c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* direct_8x8_inference_flag */
2610c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 1);
2620c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
2630c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
2640c1bc742181ded4930842b46e9507372f0b1b963James Dong
2650c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 1);
2660c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
2670c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
2680c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->frameCroppingFlag = (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
2690c1bc742181ded4930842b46e9507372f0b1b963James Dong
2700c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pSeqParamSet->frameCroppingFlag)
2710c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2720c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
2730c1bc742181ded4930842b46e9507372f0b1b963James Dong            &pSeqParamSet->frameCropLeftOffset);
2740c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
2750c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
2760c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
2770c1bc742181ded4930842b46e9507372f0b1b963James Dong            &pSeqParamSet->frameCropRightOffset);
2780c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
2790c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
2800c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
2810c1bc742181ded4930842b46e9507372f0b1b963James Dong            &pSeqParamSet->frameCropTopOffset);
2820c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
2830c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
2840c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
2850c1bc742181ded4930842b46e9507372f0b1b963James Dong            &pSeqParamSet->frameCropBottomOffset);
2860c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
2870c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
2880c1bc742181ded4930842b46e9507372f0b1b963James Dong
2890c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* check that frame cropping params are valid, parameters shall
2900c1bc742181ded4930842b46e9507372f0b1b963James Dong         * specify non-negative area within the original picture */
2910c1bc742181ded4930842b46e9507372f0b1b963James Dong        if ( ( (i32)pSeqParamSet->frameCropLeftOffset >
2920c1bc742181ded4930842b46e9507372f0b1b963James Dong               ( 8 * (i32)pSeqParamSet->picWidthInMbs -
2930c1bc742181ded4930842b46e9507372f0b1b963James Dong                 ((i32)pSeqParamSet->frameCropRightOffset + 1) ) ) ||
2940c1bc742181ded4930842b46e9507372f0b1b963James Dong             ( (i32)pSeqParamSet->frameCropTopOffset >
2950c1bc742181ded4930842b46e9507372f0b1b963James Dong               ( 8 * (i32)pSeqParamSet->picHeightInMbs -
2960c1bc742181ded4930842b46e9507372f0b1b963James Dong                 ((i32)pSeqParamSet->frameCropBottomOffset + 1) ) ) )
2970c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
2980c1bc742181ded4930842b46e9507372f0b1b963James Dong            EPRINT("frame_cropping");
2990c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(HANTRO_NOK);
3000c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3010c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3020c1bc742181ded4930842b46e9507372f0b1b963James Dong
3030c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* check that image dimensions and levelIdc match */
3040c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = pSeqParamSet->picWidthInMbs * pSeqParamSet->picHeightInMbs;
3050c1bc742181ded4930842b46e9507372f0b1b963James Dong    value = GetDpbSize(tmp, pSeqParamSet->levelIdc);
3060c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (value == INVALID_DPB_SIZE || pSeqParamSet->numRefFrames > value)
3070c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3080c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEBUG(("WARNING! Invalid DPB size based on SPS Level!\n"));
3090c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEBUG(("WARNING! Using num_ref_frames =%d for DPB size!\n",
3100c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pSeqParamSet->numRefFrames));
3110c1bc742181ded4930842b46e9507372f0b1b963James Dong        value = pSeqParamSet->numRefFrames;
3120c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3130c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->maxDpbSize = value;
3140c1bc742181ded4930842b46e9507372f0b1b963James Dong
3150c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdGetBits(pStrmData, 1);
3160c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (tmp == END_OF_STREAM)
3170c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
3180c1bc742181ded4930842b46e9507372f0b1b963James Dong    pSeqParamSet->vuiParametersPresentFlag = (tmp == 1) ?
3190c1bc742181ded4930842b46e9507372f0b1b963James Dong                                HANTRO_TRUE : HANTRO_FALSE;
3200c1bc742181ded4930842b46e9507372f0b1b963James Dong
3210c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* VUI */
3220c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pSeqParamSet->vuiParametersPresentFlag)
3230c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3240c1bc742181ded4930842b46e9507372f0b1b963James Dong        ALLOCATE(pSeqParamSet->vuiParameters, 1, vuiParameters_t);
3250c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (pSeqParamSet->vuiParameters == NULL)
3260c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(MEMORY_ALLOCATION_ERROR);
3270c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeVuiParameters(pStrmData,
3280c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSeqParamSet->vuiParameters);
3290c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
3300c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
3310c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* check numReorderFrames and maxDecFrameBuffering */
3320c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (pSeqParamSet->vuiParameters->bitstreamRestrictionFlag)
3330c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3340c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (pSeqParamSet->vuiParameters->numReorderFrames >
3350c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSeqParamSet->vuiParameters->maxDecFrameBuffering ||
3360c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSeqParamSet->vuiParameters->maxDecFrameBuffering <
3370c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSeqParamSet->numRefFrames ||
3380c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSeqParamSet->vuiParameters->maxDecFrameBuffering >
3390c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSeqParamSet->maxDpbSize)
3400c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
3410c1bc742181ded4930842b46e9507372f0b1b963James Dong                return(HANTRO_NOK);
3420c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
3430c1bc742181ded4930842b46e9507372f0b1b963James Dong
3440c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* standard says that "the sequence shall not require a DPB with
3450c1bc742181ded4930842b46e9507372f0b1b963James Dong             * size of more than max(1, maxDecFrameBuffering) */
3460c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSeqParamSet->maxDpbSize =
3470c1bc742181ded4930842b46e9507372f0b1b963James Dong                MAX(1, pSeqParamSet->vuiParameters->maxDecFrameBuffering);
3480c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3490c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3500c1bc742181ded4930842b46e9507372f0b1b963James Dong
3510c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp = h264bsdRbspTrailingBits(pStrmData);
3520c1bc742181ded4930842b46e9507372f0b1b963James Dong
3530c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* ignore possible errors in trailing bits of parameters sets */
3540c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(HANTRO_OK);
3550c1bc742181ded4930842b46e9507372f0b1b963James Dong
3560c1bc742181ded4930842b46e9507372f0b1b963James Dong}
3570c1bc742181ded4930842b46e9507372f0b1b963James Dong
3580c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
3590c1bc742181ded4930842b46e9507372f0b1b963James Dong
3600c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function: GetDpbSize
3610c1bc742181ded4930842b46e9507372f0b1b963James Dong
3620c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
3630c1bc742181ded4930842b46e9507372f0b1b963James Dong            Get size of the DPB in frames. Size is determined based on the
3640c1bc742181ded4930842b46e9507372f0b1b963James Dong            picture size and MaxDPB for the specified level. These determine
3650c1bc742181ded4930842b46e9507372f0b1b963James Dong            how many pictures may fit into to the buffer. However, the size
3660c1bc742181ded4930842b46e9507372f0b1b963James Dong            is also limited to a maximum of 16 frames and therefore function
3670c1bc742181ded4930842b46e9507372f0b1b963James Dong            returns the minimum of the determined size and 16.
3680c1bc742181ded4930842b46e9507372f0b1b963James Dong
3690c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
3700c1bc742181ded4930842b46e9507372f0b1b963James Dong            picSizeInMbs    number of macroblocks in the picture
3710c1bc742181ded4930842b46e9507372f0b1b963James Dong            levelIdc        indicates the level
3720c1bc742181ded4930842b46e9507372f0b1b963James Dong
3730c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
3740c1bc742181ded4930842b46e9507372f0b1b963James Dong            none
3750c1bc742181ded4930842b46e9507372f0b1b963James Dong
3760c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
3770c1bc742181ded4930842b46e9507372f0b1b963James Dong            size of the DPB in frames
3780c1bc742181ded4930842b46e9507372f0b1b963James Dong            INVALID_DPB_SIZE when invalid levelIdc specified or picSizeInMbs
3790c1bc742181ded4930842b46e9507372f0b1b963James Dong            is higher than supported by the level in question
3800c1bc742181ded4930842b46e9507372f0b1b963James Dong
3810c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
3820c1bc742181ded4930842b46e9507372f0b1b963James Dong
3830c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 GetDpbSize(u32 picSizeInMbs, u32 levelIdc)
3840c1bc742181ded4930842b46e9507372f0b1b963James Dong{
3850c1bc742181ded4930842b46e9507372f0b1b963James Dong
3860c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Variables */
3870c1bc742181ded4930842b46e9507372f0b1b963James Dong
3880c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 tmp;
3890c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 maxPicSizeInMbs;
3900c1bc742181ded4930842b46e9507372f0b1b963James Dong
3910c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Code */
3920c1bc742181ded4930842b46e9507372f0b1b963James Dong
3930c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(picSizeInMbs);
3940c1bc742181ded4930842b46e9507372f0b1b963James Dong
3950c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* use tmp as the size of the DPB in bytes, computes as 1024 * MaxDPB
3960c1bc742181ded4930842b46e9507372f0b1b963James Dong     * (from table A-1 in Annex A) */
3970c1bc742181ded4930842b46e9507372f0b1b963James Dong    switch (levelIdc)
3980c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3990c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 10:
4000c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 152064;
4010c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 99;
4020c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4030c1bc742181ded4930842b46e9507372f0b1b963James Dong
4040c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 11:
4050c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 345600;
4060c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 396;
4070c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4080c1bc742181ded4930842b46e9507372f0b1b963James Dong
4090c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 12:
4100c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 912384;
4110c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 396;
4120c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4130c1bc742181ded4930842b46e9507372f0b1b963James Dong
4140c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 13:
4150c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 912384;
4160c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 396;
4170c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4180c1bc742181ded4930842b46e9507372f0b1b963James Dong
4190c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 20:
4200c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 912384;
4210c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 396;
4220c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4230c1bc742181ded4930842b46e9507372f0b1b963James Dong
4240c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 21:
4250c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 1824768;
4260c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 792;
4270c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4280c1bc742181ded4930842b46e9507372f0b1b963James Dong
4290c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 22:
4300c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 3110400;
4310c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 1620;
4320c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4330c1bc742181ded4930842b46e9507372f0b1b963James Dong
4340c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 30:
4350c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 3110400;
4360c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 1620;
4370c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4380c1bc742181ded4930842b46e9507372f0b1b963James Dong
4390c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 31:
4400c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 6912000;
4410c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 3600;
4420c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4430c1bc742181ded4930842b46e9507372f0b1b963James Dong
4440c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 32:
4450c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 7864320;
4460c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 5120;
4470c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4480c1bc742181ded4930842b46e9507372f0b1b963James Dong
4490c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 40:
4500c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 12582912;
4510c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 8192;
4520c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4530c1bc742181ded4930842b46e9507372f0b1b963James Dong
4540c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 41:
4550c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 12582912;
4560c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 8192;
4570c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4580c1bc742181ded4930842b46e9507372f0b1b963James Dong
4590c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 42:
4600c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 34816*384;
4610c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 8704;
4620c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4630c1bc742181ded4930842b46e9507372f0b1b963James Dong
4640c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 50:
4650c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* standard says 42301440 here, but corrigendum "corrects" this to
4660c1bc742181ded4930842b46e9507372f0b1b963James Dong             * 42393600 */
4670c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 42393600;
4680c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 22080;
4690c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4700c1bc742181ded4930842b46e9507372f0b1b963James Dong
4710c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 51:
4720c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = 70778880;
4730c1bc742181ded4930842b46e9507372f0b1b963James Dong            maxPicSizeInMbs = 36864;
4740c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4750c1bc742181ded4930842b46e9507372f0b1b963James Dong
4760c1bc742181ded4930842b46e9507372f0b1b963James Dong        default:
4770c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(INVALID_DPB_SIZE);
4780c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
4790c1bc742181ded4930842b46e9507372f0b1b963James Dong
4800c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* this is not "correct" return value! However, it results in error in
4810c1bc742181ded4930842b46e9507372f0b1b963James Dong     * decoding and this was easiest place to check picture size */
4820c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (picSizeInMbs > maxPicSizeInMbs)
4830c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(INVALID_DPB_SIZE);
4840c1bc742181ded4930842b46e9507372f0b1b963James Dong
4850c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp /= (picSizeInMbs*384);
4860c1bc742181ded4930842b46e9507372f0b1b963James Dong
4870c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(MIN(tmp, 16));
4880c1bc742181ded4930842b46e9507372f0b1b963James Dong
4890c1bc742181ded4930842b46e9507372f0b1b963James Dong}
4900c1bc742181ded4930842b46e9507372f0b1b963James Dong
4910c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
4920c1bc742181ded4930842b46e9507372f0b1b963James Dong
4930c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function name: h264bsdCompareSeqParamSets
4940c1bc742181ded4930842b46e9507372f0b1b963James Dong
4950c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
4960c1bc742181ded4930842b46e9507372f0b1b963James Dong            Compare two sequence parameter sets.
4970c1bc742181ded4930842b46e9507372f0b1b963James Dong
4980c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
4990c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSps1   pointer to a sequence parameter set
5000c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSps2   pointer to another sequence parameter set
5010c1bc742181ded4930842b46e9507372f0b1b963James Dong
5020c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
5030c1bc742181ded4930842b46e9507372f0b1b963James Dong            0       sequence parameter sets are equal
5040c1bc742181ded4930842b46e9507372f0b1b963James Dong            1       otherwise
5050c1bc742181ded4930842b46e9507372f0b1b963James Dong
5060c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
5070c1bc742181ded4930842b46e9507372f0b1b963James Dong
5080c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdCompareSeqParamSets(seqParamSet_t *pSps1, seqParamSet_t *pSps2)
5090c1bc742181ded4930842b46e9507372f0b1b963James Dong{
5100c1bc742181ded4930842b46e9507372f0b1b963James Dong
5110c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Variables */
5120c1bc742181ded4930842b46e9507372f0b1b963James Dong
5130c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 i;
5140c1bc742181ded4930842b46e9507372f0b1b963James Dong
5150c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Code */
5160c1bc742181ded4930842b46e9507372f0b1b963James Dong
5170c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pSps1);
5180c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pSps2);
5190c1bc742181ded4930842b46e9507372f0b1b963James Dong
5200c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* first compare parameters whose existence does not depend on other
5210c1bc742181ded4930842b46e9507372f0b1b963James Dong     * parameters and only compare the rest of the params if these are equal */
5220c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pSps1->profileIdc        == pSps2->profileIdc &&
5230c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->levelIdc          == pSps2->levelIdc &&
5240c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->maxFrameNum       == pSps2->maxFrameNum &&
5250c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->picOrderCntType   == pSps2->picOrderCntType &&
5260c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->numRefFrames      == pSps2->numRefFrames &&
5270c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->gapsInFrameNumValueAllowedFlag ==
5280c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSps2->gapsInFrameNumValueAllowedFlag &&
5290c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->picWidthInMbs     == pSps2->picWidthInMbs &&
5300c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->picHeightInMbs    == pSps2->picHeightInMbs &&
5310c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->frameCroppingFlag == pSps2->frameCroppingFlag &&
5320c1bc742181ded4930842b46e9507372f0b1b963James Dong        pSps1->vuiParametersPresentFlag == pSps2->vuiParametersPresentFlag)
5330c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5340c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (pSps1->picOrderCntType == 0)
5350c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
5360c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (pSps1->maxPicOrderCntLsb != pSps2->maxPicOrderCntLsb)
5370c1bc742181ded4930842b46e9507372f0b1b963James Dong                return 1;
5380c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
5390c1bc742181ded4930842b46e9507372f0b1b963James Dong        else if (pSps1->picOrderCntType == 1)
5400c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
5410c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (pSps1->deltaPicOrderAlwaysZeroFlag !=
5420c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSps2->deltaPicOrderAlwaysZeroFlag ||
5430c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSps1->offsetForNonRefPic != pSps2->offsetForNonRefPic ||
5440c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSps1->offsetForTopToBottomField !=
5450c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSps2->offsetForTopToBottomField ||
5460c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSps1->numRefFramesInPicOrderCntCycle !=
5470c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pSps2->numRefFramesInPicOrderCntCycle)
5480c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
5490c1bc742181ded4930842b46e9507372f0b1b963James Dong                return 1;
5500c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
5510c1bc742181ded4930842b46e9507372f0b1b963James Dong            else
5520c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
5530c1bc742181ded4930842b46e9507372f0b1b963James Dong                for (i = 0; i < pSps1->numRefFramesInPicOrderCntCycle; i++)
5540c1bc742181ded4930842b46e9507372f0b1b963James Dong                    if (pSps1->offsetForRefFrame[i] !=
5550c1bc742181ded4930842b46e9507372f0b1b963James Dong                        pSps2->offsetForRefFrame[i])
5560c1bc742181ded4930842b46e9507372f0b1b963James Dong                    {
5570c1bc742181ded4930842b46e9507372f0b1b963James Dong                        return 1;
5580c1bc742181ded4930842b46e9507372f0b1b963James Dong                    }
5590c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
5600c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
5610c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (pSps1->frameCroppingFlag)
5620c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
5630c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (pSps1->frameCropLeftOffset   != pSps2->frameCropLeftOffset ||
5640c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSps1->frameCropRightOffset  != pSps2->frameCropRightOffset ||
5650c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSps1->frameCropTopOffset    != pSps2->frameCropTopOffset ||
5660c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSps1->frameCropBottomOffset != pSps2->frameCropBottomOffset)
5670c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
5680c1bc742181ded4930842b46e9507372f0b1b963James Dong                return 1;
5690c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
5700c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
5710c1bc742181ded4930842b46e9507372f0b1b963James Dong
5720c1bc742181ded4930842b46e9507372f0b1b963James Dong        return 0;
5730c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
5740c1bc742181ded4930842b46e9507372f0b1b963James Dong
5750c1bc742181ded4930842b46e9507372f0b1b963James Dong    return 1;
5760c1bc742181ded4930842b46e9507372f0b1b963James Dong}
5770c1bc742181ded4930842b46e9507372f0b1b963James Dong
578