解码h264sps得到宽高

typedef  unsigned int UINT;
typedef  unsigned char BYTE;
typedef  unsigned long DWORD;

static UINT Ue(BYTE *pBuff, UINT nLen, UINT &nStartBit)
{
    UINT nZeroNum = 0;
    while (nStartBit < nLen * 8)
    {
        if (pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8))) //&:按位与,%取余
        {
            break;
        }
        nZeroNum++;
        nStartBit++;
    }
    nStartBit ++;

    //计算结果
    DWORD dwRet = 0;
    for (UINT i=0; i<nZeroNum; i++)
    {
        dwRet <<= 1;
        if (pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8)))
        {
            dwRet += 1;
        }
        nStartBit++;
    }
    return (1 << nZeroNum) - 1 + dwRet;
}

static int Se(BYTE *pBuff, UINT nLen, UINT &nStartBit)
{
    int UeVal=Ue(pBuff,nLen,nStartBit);
    double k=UeVal;
    int nValue=ceil(k/2);
    if (UeVal % 2==0)
        nValue=-nValue;
    return nValue;
}

static DWORD u(UINT BitCount,BYTE * buf,UINT &nStartBit)
{
    DWORD dwRet = 0;
    for (UINT i=0; i<BitCount; i++)
    {
        dwRet <<= 1;
        if (buf[nStartBit / 8] & (0x80 >> (nStartBit % 8)))
        {
            dwRet += 1;
        }
        nStartBit++;
    }
    return dwRet;
}
static void de_emulation_prevention(BYTE* buf,unsigned int* buf_size)
{
    int i=0,j=0;
    BYTE* tmp_ptr=NULL;
    unsigned int tmp_buf_size=0;
    int val=0;

    tmp_ptr=buf;
    tmp_buf_size=*buf_size;
    for(i=0;i<(tmp_buf_size-2);i++)
    {
        //check for 0x000003
        val=(tmp_ptr[i]^0x00) +(tmp_ptr[i+1]^0x00)+(tmp_ptr[i+2]^0x03);
        if(val==0)
        {
            //kick out 0x03
            for(j=i+2;j<tmp_buf_size-1;j++)
                tmp_ptr[j]=tmp_ptr[j+1];

            //and so we should devrease bufsize
            (*buf_size)--;
        }
    }
}

bool h264_decode_sps(uint8_t *buf, unsigned int nLen, int &width, int &height, int &fps)
{
    UINT StartBit = 0;
    int chroma_format_idc = 0;
    fps = 0;
    de_emulation_prevention(buf,&nLen);

    int forbidden_zero_bit = u(1,buf,StartBit);
    int nal_ref_idc = u(2,buf,StartBit);
    int nal_unit_type = u(5,buf,StartBit);

    log_print(LOG_ERR,"nal unit type %d", nal_unit_type);

    if(7 == nal_unit_type)
    {
        int profile_idc = u(8,buf,StartBit);
        int constraint_set0_flag=u(1,buf,StartBit);//(buf[1] & 0x80)>>7;
        int constraint_set1_flag=u(1,buf,StartBit);//(buf[1] & 0x40)>>6;
        int constraint_set2_flag=u(1,buf,StartBit);//(buf[1] & 0x20)>>5;
        int constraint_set3_flag=u(1,buf,StartBit);//(buf[1] & 0x10)>>4;
        int reserved_zero_4bits=u(4,buf,StartBit);
        int level_idc = u(8,buf,StartBit);

        int seq_parameter_set_id = Ue(buf,nLen,StartBit);

        log_print(LOG_ERR,"profile %d, level %d, sps %d", profile_idc, level_idc, seq_parameter_set_id);

        if(profile_idc == 100 ||  // High profile
           profile_idc == 110 ||  // High10 profile
           profile_idc == 122 ||  // High422 profile
           profile_idc == 244 ||  // High444 Predictive profile
           profile_idc ==  44 ||  // Cavlc444 profile
           profile_idc ==  83 ||  // Scalable Constrained High profile (SVC)
           profile_idc ==  86 ||  // Scalable High Intra profile (SVC)
           profile_idc == 118 ||  // Stereo High profile (MVC)
           profile_idc == 128 ||  // Multiview High profile (MVC)
           profile_idc == 138 ||  // Multiview Depth High profile (MVCD)
           profile_idc == 144	 // old High444 profile
            )
        {
            chroma_format_idc = Ue(buf,nLen,StartBit);

            if( chroma_format_idc == 3 )
            {
                int residual_colour_transform_flag=u(1,buf,StartBit);
            }

            int bit_depth_luma_minus8=Ue(buf,nLen,StartBit);
            int bit_depth_chroma_minus8=Ue(buf,nLen,StartBit);
            int qpprime_y_zero_transform_bypass_flag=u(1,buf,StartBit);
            int seq_scaling_matrix_present_flag=u(1,buf,StartBit);

            log_print(LOG_ERR,"chroma_format_idc %d, bit_depth_luma_minus8 %d, bit_depth_chroma_minus8 %d", chroma_format_idc, bit_depth_luma_minus8, bit_depth_chroma_minus8);

            int seq_scaling_list_present_flag[8];
            if( seq_scaling_matrix_present_flag )
            {
                for( int i = 0; i < 8; i++ ) {
                    seq_scaling_list_present_flag[i]=u(1,buf,StartBit);
                }
            }
        }
        else
        {
            chroma_format_idc = 1;
            //bit_depth_luma_minus8 = 8;

        }

        int log2_max_frame_num_minus4 = Ue(buf,nLen,StartBit);
        int pic_order_cnt_type = Ue(buf,nLen,StartBit);
        if( pic_order_cnt_type == 0 )
            int log2_max_pic_order_cnt_lsb_minus4=Ue(buf,nLen,StartBit);
        else if( pic_order_cnt_type == 1 )
        {
            int delta_pic_order_always_zero_flag=u(1,buf,StartBit);
            int offset_for_non_ref_pic=Se(buf,nLen,StartBit);
            int offset_for_top_to_bottom_field=Se(buf,nLen,StartBit);
            int num_ref_frames_in_pic_order_cnt_cycle=Ue(buf,nLen,StartBit);

            int *offset_for_ref_frame=new int[num_ref_frames_in_pic_order_cnt_cycle];
            for( int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )
                offset_for_ref_frame[i]=Se(buf,nLen,StartBit);
            delete [] offset_for_ref_frame;
        }

        int num_ref_frames=Ue(buf,nLen,StartBit);
        int gaps_in_frame_num_value_allowed_flag=u(1,buf,StartBit);
        int pic_width_in_mbs_minus1=Ue(buf,nLen,StartBit) + 1;
        int pic_height_in_map_units_minus1=Ue(buf,nLen,StartBit) + 1;

        log_print(LOG_ERR,"num_ref_frames %d, mb_width %d, mb_height %d", num_ref_frames, pic_width_in_mbs_minus1, pic_height_in_map_units_minus1);

        int frame_mbs_only_flag=u(1,buf,StartBit);
        if(!frame_mbs_only_flag)
            int mb_adaptive_frame_field_flag=u(1,buf,StartBit);

        pic_height_in_map_units_minus1 *= 2 - frame_mbs_only_flag;

        int direct_8x8_inference_flag=u(1,buf,StartBit);
        int frame_cropping_flag=u(1,buf,StartBit);
        if(frame_cropping_flag)
        {
            int frame_crop_left_offset=Ue(buf,nLen,StartBit);
            int frame_crop_right_offset=Ue(buf,nLen,StartBit);
            int frame_crop_top_offset=Ue(buf,nLen,StartBit);
            int frame_crop_bottom_offset=Ue(buf,nLen,StartBit);

            int vsub   = (1 == chroma_format_idc) ? 1 : 0;
            int hsub   = ( (1 == chroma_format_idc) || (2 == chroma_format_idc) ) ? 1 : 0;
            int step_x = 1 << hsub;
            int step_y = (2 - frame_mbs_only_flag) << vsub;

            int crop_left = frame_crop_left_offset * step_x;
            int crop_right = frame_crop_right_offset * step_x;
            int crop_top = frame_crop_top_offset * step_y;
            int crop_bottom = frame_crop_bottom_offset * step_y;

            log_print(LOG_ERR,"step_x %d, step_y %d, crop_left %d, crop_right %d, crop_top %d, crop_bottom %d",
                step_x, step_y, crop_left, crop_right, crop_top, crop_bottom);

            width = pic_width_in_mbs_minus1 * 16 - (crop_right + crop_left);
            height = pic_height_in_map_units_minus1 * 16 - (crop_top + crop_bottom);
        }
        else
        {
            width=pic_width_in_mbs_minus1*16;
            height=pic_height_in_map_units_minus1*16;
        }

        int vui_parameter_present_flag=u(1,buf,StartBit);
        if(vui_parameter_present_flag)
        {
            int aspect_ratio_info_present_flag=u(1,buf,StartBit);
            if(aspect_ratio_info_present_flag)
            {
                int aspect_ratio_idc=u(8,buf,StartBit);
                if(aspect_ratio_idc==255)
                {
                    int sar_width=u(16,buf,StartBit);
                    int sar_height=u(16,buf,StartBit);
                }
            }
            int overscan_info_present_flag=u(1,buf,StartBit);
            if(overscan_info_present_flag)
                int overscan_appropriate_flagu=u(1,buf,StartBit);
            int video_signal_type_present_flag=u(1,buf,StartBit);
            if(video_signal_type_present_flag)
            {
                int video_format=u(3,buf,StartBit);
                int video_full_range_flag=u(1,buf,StartBit);
                int colour_description_present_flag=u(1,buf,StartBit);
                if(colour_description_present_flag)
                {
                    int colour_primaries=u(8,buf,StartBit);
                    int transfer_characteristics=u(8,buf,StartBit);
                    int matrix_coefficients=u(8,buf,StartBit);
                }
            }
            int chroma_loc_info_present_flag=u(1,buf,StartBit);
            if(chroma_loc_info_present_flag)
            {
                int chroma_sample_loc_type_top_field=Ue(buf,nLen,StartBit);
                int chroma_sample_loc_type_bottom_field=Ue(buf,nLen,StartBit);
            }
            int timing_info_present_flag=u(1,buf,StartBit);

            if(timing_info_present_flag)
            {
                int num_units_in_tick=u(32,buf,StartBit);
                int time_scale=u(32,buf,StartBit);
                fps=time_scale/num_units_in_tick;
                int fixed_frame_rate_flag=u(1,buf,StartBit);
                if(fixed_frame_rate_flag)
                {
                    fps=fps/2;
                }
            }
        }

        return true;
    }

    return false;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值