H264 SPS中得到宽高的代码(java)

2023-05-16

数据需要去掉头,SPS测试数据 byte[] buffer = new byte[]{103, 66, -64, 12, -38, 5, 7, -56, 7, -124, 2} 得到结果宽320高240.

public class H264SpsParser {
    private final static String TAG = H264SpsParser.class.getCanonicalName();
    private final static int NAL_HEADER = 0;

    private static int nStartBit = 0;


    private static int Ue(byte[] pBuff, int nLen) {
        int nZeroNum = 0;
        while (nStartBit < nLen * 8) {
            if ((pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8))) != 0) {
                break;
            }
            nZeroNum++;
            nStartBit++;
        }
        nStartBit++;

        int dwRet = 0;
        for (int i = 0; i < nZeroNum; i++) {
            dwRet <<= 1;
            if ((pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8))) != 0) {
                dwRet += 1;
            }
            nStartBit++;
        }
        return (1 << nZeroNum) - 1 + dwRet;
    }

    private static int Se(byte[] pBuff, int nLen) {
        int UeVal = Ue(pBuff, nLen);
        double k = UeVal;
        int nValue = (int) Math.ceil(k / 2);
        if (UeVal % 2 == 0) {
            nValue = -nValue;
        }
        return nValue;
    }

    private static int u(int BitCount, byte[] buf) {
        int dwRet = 0;
        for (int i = 0; i < BitCount; i++) {
            dwRet <<= 1;
            if ((buf[nStartBit / 8] & (0x80 >> (nStartBit % 8))) != 0) {
                dwRet += 1;
            }
            nStartBit++;
        }
        return dwRet;
    }

    private static boolean h264_decode_seq_parameter_set(byte[] buf, int nLen, int[] size) {
        nStartBit = 0;
        int forbidden_zero_bit = u(1, buf);
        int nal_ref_idc = u(2, buf);
        int nal_unit_type = u(5, buf);
        if (nal_unit_type == 7) {
            int profile_idc = u(8, buf);
            int constraint_set0_flag = u(1, buf);//(buf[1] & 0x80)>>7;
            int constraint_set1_flag = u(1, buf);//(buf[1] & 0x40)>>6;
            int constraint_set2_flag = u(1, buf);//(buf[1] & 0x20)>>5;
            int constraint_set3_flag = u(1, buf);//(buf[1] & 0x10)>>4;
            int reserved_zero_4bits = u(4, buf);
            int level_idc = u(8, buf);

            int seq_parameter_set_id = Ue(buf, nLen);

            if (profile_idc == 100 || profile_idc == 110 ||
                    profile_idc == 122 || profile_idc == 144) {
                int chroma_format_idc = Ue(buf, nLen);
                if (chroma_format_idc == 3) {
                    int residual_colour_transform_flag = u(1, buf);
                }
                int bit_depth_luma_minus8 = Ue(buf, nLen);
                int bit_depth_chroma_minus8 = Ue(buf, nLen);
                int qpprime_y_zero_transform_bypass_flag = u(1, buf);
                int seq_scaling_matrix_present_flag = u(1, buf);

                int[] seq_scaling_list_present_flag = new int[8];
                if (seq_scaling_matrix_present_flag != 0) {
                    for (int i = 0; i < 8; i++) {
                        seq_scaling_list_present_flag[i] = u(1, buf);
                    }
                }
            }
            int log2_max_frame_num_minus4 = Ue(buf, nLen);
            int pic_order_cnt_type = Ue(buf, nLen);
            if (pic_order_cnt_type == 0) {
                int log2_max_pic_order_cnt_lsb_minus4 = Ue(buf, nLen);
            } else if (pic_order_cnt_type == 1) {
                int delta_pic_order_always_zero_flag = u(1, buf);
                int offset_for_non_ref_pic = Se(buf, nLen);
                int offset_for_top_to_bottom_field = Se(buf, nLen);
                int num_ref_frames_in_pic_order_cnt_cycle = Ue(buf, nLen);

                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);
                }
            }
            int num_ref_frames = Ue(buf, nLen);
            int gaps_in_frame_num_value_allowed_flag = u(1, buf);
            int pic_width_in_mbs_minus1 = Ue(buf, nLen);
            int pic_height_in_map_units_minus1 = Ue(buf, nLen);

            size[0] = (pic_width_in_mbs_minus1 + 1) * 16;
            size[1] = (pic_height_in_map_units_minus1 + 1) * 16;

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

        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 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;
    }

    public static int[] getSizeFromSps(byte[] data) {
//        for (int i=0; i<data.length-4; i++)
//        {
            if (data[i]==0 && data[i+1]==0 && data[i+2]==0 && data[i+3]==1 && data[i+4]==0x67)
            {
                int[] size = new int[2];
                h264_decode_seq_parameter_set(data, data.length, size);
                Log.d(TAG, "Sps=("+size[0]+", "+size[1]+")");
                return size;
            }
//        }
        int[] size = new int[2];
        h264_decode_seq_parameter_set(data, data.length, size);
        Log.e(TAG, "Sps=(" + size[0] + ", " + size[1] + ")");
        return size;
    }

}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

H264 SPS中得到宽高的代码(java) 的相关文章

随机推荐

  • 用策略模式优化代码的实例

    实例一 xff1a 利用利用策略模式实际开发中 if else 条件判断过多的问题 xff0c 条件少还好 xff0c 一旦 else if 过多这里的逻辑将会比较混乱 xff0c 并很容易出错 比如 xff1a 刚开始条件较少 xff0c
  • 灰度处理与二值化的关系

    当开始接触图像处理的童鞋可能跟我一样对这两个概念迷惑过 xff0c 在图像处理中 xff0c 用RGB三个分量 xff08 R xff1a Red xff0c G xff1a Green xff0c B xff1a Blue xff09 x
  • ucos2历程——信号量集

    信号量集 信号量集由两部分组成 xff1a 标识组和等待任务列表 xff1b 标识组由三部分组成 xff1a 1 OSFlagType 识别是否为信号量集的标志 2 OSFlagWaitList 指向等待任务列表的指针 3 OSFlagFl
  • 人体姿态估计资源大列表(Human Pose Estimation)

    基础 xff1a Human Pose Estimation人体姿态估计综述调研人体姿态估计数据集整理 xff08 Pose Estimation Keypoint xff09 姿态估计的两个数据集COCO和MPII的认识 Human Po
  • DIY小四轴之电路设计(一)

    DIY小四轴之电路设计 xff08 一 xff09 写在前面 前一阵时间一直在做四轴飞行器 xff0c 略有一点收获吧 xff0c 在这里分享出来 xff0c 一方面算是对自己的总结 xff0c 另一方面希望能给想做小四轴的读者一些思路 本
  • DIY小四轴之电路设计(二)

    DIY小四轴之电路设计 xff08 二 xff09 上次我分析了四轴电源的电路 xff0c 这次我们来看电机驱动与传感器电路 三 空心杯电机驱动电路 一般的小型四轴都选用空心杯电机来驱动旋翼 xff0c 空心杯电机不仅节能而且灵敏 xff0
  • ubuntu 18.04 vnc server开机自启动

    转自 xff1a https blog csdn net lixiaotao 1 article details 90140979 1 首先确定vncserver 以正确安装到linux系统 xff1b 2 设置vncserver随系统自启
  • vnc viewer灰屏的解决方法

    vnc能够连接上 xff0c 但是进入界面灰屏 先关闭当前打开的vnc xff1a vncserver kill 88 然后修改权限 xff1a chmod 43 x vnc xstartup 然后重新打开vnc vncserver geo
  • samba 常用命令

    没怎么修改配置 xff0c 但有时需要修改时 xff0c 又是搜索一番 xff0c 故将常用的在此备份一下 修改samba配置 xff1a span class token function sudo span span class tok
  • .rst 语法+简明教程

    reStructuredText 是扩展名为 rst的纯文本文件 xff0c 含义为 34 重新构建的文本 34 xff0c 也被简称为 xff1a RST或reST xff1b 是Python编程语言的Docutils项目的一部分 xff
  • TG_7100b准备开发环境

    请在 64 位 Ubuntu 下搭建开发环境 Win10 系统可以在应用商店下载安装 Ubuntu18 04 LTS 其他用户可以安装虚拟机软件 以下为基于 Ubuntu 环境开发和编译 SDK 时需要用到的库和依赖包 xff0c 请您按顺
  • C++ STL中各容器内存、优劣的分析

    STL有三大核心部分 xff1a 容器 xff08 Container xff09 算法 xff08 Algorithms xff09 迭代器 xff08 Iterator xff09 以下介绍容器相关内容 xff1a 各种容器的元素在内存
  • 给自己时间沉淀下来

    像很多学长学姐当初一样 xff0c 我也到了繁忙的大四 这个尴尬的时间 xff0c 要选择 xff0c 要放弃 开始实习 xff0c 去窥探一下外面的世界 经过一个月的测试工作 xff0c 开始发现自己与别人的差距还是很大 再继续试水 xf
  • docker安装使用系列二之容器、镜像、仓库使用实例分析

    可能大家对docker了解不深 xff0c 这里再简单赘述一下docker这款利器 1 什么是docker Doker是基于GO语言实现的云开源项目 xff0c 通过对应用组件的封装 分发 部署 运行等生命周期的管理 xff0c 达到应用组
  • 图像处理之opencv库使用小结

    OpenCV是一个基于BSD许可 xff08 开源 xff09 发行的跨平台计算机视觉库 xff0c 可以运行在Linux Windows Android和Mac OS操作系统上 它轻量级而且高效 由一系列 C 函数和少量 C 43 43
  • react 启动项目遇到的问题

    当启动react 项目时遇到 xff1a 39 react scripts 39 不是内部或外部命令 xff0c 也不是可运行的程序 npm install npm install 下载依赖遇到安装失败 xff0c 则依赖包删除不干净 xf
  • Android LED电子表时钟字体digital font

    字体效果如下图所示 xff1a 这种类型的字体样式会被一些UI设计用于Android APP中时钟显示 xff0c 比如交通灯倒计时 实现这种字体样式 xff0c 先导入一个字体包 xff1a digital ttf 这个digital t
  • Android音视频处理之MediaCodec

    MediaCodec是Android中媒体编解码器 xff0c 可以对媒体进行编 解码 MediaCodec采用同步 异步方式处理数据 xff0c 并且使用了一组输入输出缓存 xff08 ByteBuffer xff09 通过请求一个空的输
  • 计算相机投影矩阵(含代码)(Python)

    计算相机投影矩阵 xff08 含代码 xff09 xff08 Python xff09 前几天处理点云时 xff0c 需要使用到像片与3D点云的对应关系 在这边找了一圈没有发现直接可用的代码 xff0c 于是去GitHub试了一下 xff0
  • H264 SPS中得到宽高的代码(java)

    数据需要去掉头 xff0c SPS测试数据 byte buffer 61 new byte 103 66 64 12 38 5 7 56 7 124 2 得到结果宽320高240 public class H264SpsParser pri