转载地址

H264 获取SPS与PPS

在用Android手机进行h264硬编码的时候如果要进行视频流的实时传输与播放,就需要知道视频流的Sequence Parameter Sets (SPS) 和Picture Parameter Set (PPS)。

今天算是看明白如何获取SPS和PPS,在这里记录下来,希望有需要的朋友可以在这里获取到一些些的帮助。

首先说一下大前提,我设置的视频录制参数为:

mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

为了让大家更加明白,我先贴出avcC的数据结构:

    aligned(8) class AVCDecoderConfigurationRecord {  unsigned int(8) configurationVersion = 1;  unsigned int(8) AVCProfileIndication;  unsigned int(8) profile_compatibility;  unsigned int(8) AVCLevelIndication;  bit(6) reserved = '111111'b;  unsigned int(2) lengthSizeMinusOne;  bit(3) reserved = '111'b;  unsigned int(5) numOfSequenceParameterSets;  for (i=0; i< numOfSequenceParameterSets; i++) {  unsigned int(16) sequenceParameterSetLength ;  bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;  }  unsigned int(8) numOfPictureParameterSets;  for (i=0; i< numOfPictureParameterSets; i++) {  unsigned int(16) pictureParameterSetLength;  bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;  }  }  

ok,数据结构贴出来了,我再贴出录制的3gp输出类型的h264码流片断。

阴影部分就是avcC的全部数据了。

其中: 0x61 0x76 0x63 0x43 就是字符avcC

0x01 是configurationVersion

0x42 是AVCProfileIndication

0x00 是profile_compatibility

0x1F是AVCLevelIndication

0xFF 是6bit的reserved 和2bit的lengthSizeMinusOne

0xE1 是3bit的reserved 和5bit的numOfSequenceParameterSets

0x00 0x09是sps的长度为9个字节。

故SPS的内容为接下来的9个字节:67 42 00 1f e9 02 c1 2c 80

接下来的:01为numOfPictureParameterSets

0x00和0x04是pps的长度为4个字节。

故PPS的内容为接下来的4个字节:68 ce 06 f2

通过这段数据片断,就可以获取到SPS和PPS了。

下面我将贴上用java代码获取输出格式为3gp的h264码流的SPS与PPS代码:

    package cn.edu.xmu.zgy;  import java.io.File;  import java.io.FileInputStream;  import java.io.IOException;  public class ObtainSPSAndPPS {  public void getSPSAndPPS(String fileName) throws IOException {  File file = new File(fileName);  FileInputStream fis = new FileInputStream(file);  int fileLength = (int) file.length();  byte[] fileData = new byte[fileLength];  fis.read(fileData);  // 'a'=0x61, 'v'=0x76, 'c'=0x63, 'C'=0x43  byte[] avcC = new byte[] { 0x61, 0x76, 0x63, 0x43 };  // avcC的起始位置  int avcRecord = 0;  for (int ix = 0; ix < fileLength; ++ix) {  if (fileData[ix] == avcC[0] && fileData[ix + 1] == avcC[1]  && fileData[ix + 2] == avcC[2]  && fileData[ix + 3] == avcC[3]) {  // 找到avcC,则记录avcRecord起始位置,然后退出循环。  avcRecord = ix + 4;  break;  }  }  if (0 == avcRecord) {  System.out.println("没有找到avcC,请检查文件格式是否正确");  return;  }  // 加6的目的是为了跳过  // (1)8字节的 configurationVersion  // (2)8字节的 AVCProfileIndication  // (3)8字节的 profile_compatibility  // (4)8 字节的 AVCLevelIndication  // (5)6 bit 的 reserved  // (6)2 bit 的 lengthSizeMinusOne  // (7)3 bit 的 reserved  // (8)5 bit 的numOfSequenceParameterSets  // 共6个字节,然后到达sequenceParameterSetLength的位置  int spsStartPos = avcRecord + 6;  byte[] spsbt = new byte[] { fileData[spsStartPos],  fileData[spsStartPos + 1] };  int spsLength = bytes2Int(spsbt);  byte[] SPS = new byte[spsLength];  // 跳过2个字节的 sequenceParameterSetLength  spsStartPos += 2;  System.arraycopy(fileData, spsStartPos, SPS, 0, spsLength);  printResult("SPS", SPS, spsLength);  // 底下部分为获取PPS  // spsStartPos + spsLength 可以跳到pps位置  // 再加1的目的是跳过1字节的 numOfPictureParameterSets  int ppsStartPos = spsStartPos + spsLength + 1;  byte[] ppsbt = new byte[] { fileData[ppsStartPos],  fileData[ppsStartPos + 1] };  int ppsLength = bytes2Int(ppsbt);  byte[] PPS = new byte[ppsLength];  ppsStartPos += 2;  System.arraycopy(fileData, ppsStartPos, PPS, 0, ppsLength);  printResult("PPS", PPS, ppsLength);  }  private int bytes2Int(byte[] bt) {  int ret = bt[0];  ret <<= 8;  ret |= bt[1];  return ret;  }  private void printResult(String type, byte[] bt, int len) {  System.out.println(type + "长度为:" + len);  String cont = type + "的内容为:";  System.out.print(cont);  for (int ix = 0; ix < len; ++ix) {  System.out.printf("%02x ", bt[ix]);  }  System.out.println("\n----------");  }  public static void main(String[] args) throws IOException {  new ObtainSPSAndPPS().getSPSAndPPS("c:\\zgy.h264");  }  }  

运行结果如下:

    SPS长度为:9  SPS的内容为:67 42 00 1f e9 02 c1 2c 80   ----------
    PPS长度为:4  PPS的内容为:68 ce 06 f2   ----------  

H264 获取SPS与PPS相关推荐

  1. H264 获取SPS与PPS(附源码)

    在用Android手机进行h264硬编码的时候如果要进行视频流的实时传输与播放,就需要知道视频流的Sequence Parameter Sets (SPS) 和Picture Parameter Se ...

  2. MP4文件中h264的 SPS、PPS获取

    SkySeraph 博客园 首页 博问 闪存 新随笔 联系 订阅 管理 随笔- 190 文章- 0 评论- 407  [流媒體]H264-MP4格式及在MP4文件中提取H264的SPS.PPS及码流 ...

  3. H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流

    SkySeraph Apr 1st 2012 Email:skyseraph00@163.com 一.MP4格式基本概念 MP4格式对应标准MPEG-4标准(ISO/IEC14496) 二.MP4封装 ...

  4. 【流媒体】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流

    一.MP4格式基本概念 MP4格式对应标准MPEG-4标准(ISO/IEC14496) 二.MP4封装格式核心概念 MP4封装格式对应标准为 ISO/IEC 14496-12(信息技术 视听对象编码的 ...

  5. MP4格式及在MP4文件中提取H264的SPS、PPS及码流

    一.MP4格式基本概念 MP4格式对应标准MPEG-4标准(ISO/IEC14496) 二.MP4封装格式核心概念 1  MP4封装格式对应标准为 ISO/IEC 14496-12(信息技术 视听对象 ...

  6. 【流媒體】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流

    原文地址为: [流媒體]H264-MP4格式及在MP4文件中提取H264的SPS.PPS及码流 [流媒體]H264-MP4格式及在MP4文件中提取H264的SPS.PPS及码流 SkySeraph A ...

  7. [转]【流媒體】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流

    [流媒體]H264-MP4格式及在MP4文件中提取H264的SPS.PPS及码流 SkySeraph Apr 1st 2012  Email:skyseraph00@163 .com 一.MP4格式基 ...

  8. 流媒體】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流

    源地址:http://www.cnblogs.com/skyseraph/archive/2012/04/01/2429384.html [流媒體]H264-MP4格式及在MP4文件中提取H264的S ...

  9. 【转载】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流

    [流媒體]H264-MP4格式及在MP4文件中提取H264的SPS.PPS及码流 SkySeraph Apr 1st 2012 Email:skyseraph00@163.com 一.MP4格式基本概 ...

最新文章

  1. Top100论文导读:深入理解卷积神经网络CNN(Part Ⅱ)
  2. 人脸妆容迁移---研究和思考
  3. matlab 多项式的相关运算
  4. addEventListener 的事件函数的传递【转载】
  5. 没有Science也没有娃,上海交大博士谈科研经历爆笑全场
  6. ironpython2.7.8相当于python3.7吗_IronPython与numpy的Python速度之比较
  7. MXNET:深度学习计算-模型参数
  8. Linux使用fsck修复文件系统
  9. DB2常用错误代码大全
  10. 基于Django 文档1.11 自解+补完 学习django ---part1
  11. 肖博数学高考数学快速解题法及秒杀向量问题总结
  12. 删除子文件夹[字典树 + go变量 + strings.builder的copyCheck()]
  13. 《那些年啊,那些事——一个程序员的奋斗史》——24
  14. 几何画板自定义工具_可能是全网最详细:12 款 iPad 绘画工具横评,总有一款适合你...
  15. STC单片机波特率计算
  16. hackthebox-Tracks-Beginner_Track-Jerry
  17. KVM详细介绍及搭建KVM虚拟化平台构建Centos7系统
  18. 爬取7160网站总是不成功。。。求大神分析分析
  19. 郑州计算机学校排名2019年排行榜,2021年郑州大学最新排名 全国最新排名
  20. SW2021软件安装教程

热门文章

  1. js|BMI指数计算(含测试源码)
  2. 一键解析还原全景图krpano分析工具
  3. opporeno5k加密相册方法分享
  4. 利用MS-SAMR协议修改/重置用户密码
  5. 新标韩语第一册 单词-句子
  6. Web开发学习困难问题 西安尚学堂
  7. Ubuntu开机报错:could not update ICEauthority file /home/lstanding/.ICEauthority 并且不能进入开机页面
  8. 春松客服的运维管理 | 春松客服
  9. 屌丝逆袭,渣本毕业1年多进阿里
  10. CTP量化之路一(CTP接口篇)