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

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

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

mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

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

[cpp]  view plain copy
  1. aligned(8) class AVCDecoderConfigurationRecord {
  2. unsigned int(8) configurationVersion = 1;
  3. unsigned int(8) AVCProfileIndication;
  4. unsigned int(8) profile_compatibility;
  5. unsigned int(8) AVCLevelIndication;
  6. bit(6) reserved = '111111'b;
  7. unsigned int(2) lengthSizeMinusOne;
  8. bit(3) reserved = '111'b;
  9. unsigned int(5) numOfSequenceParameterSets;
  10. for (i=0; i< numOfSequenceParameterSets; i++) {
  11. unsigned int(16) sequenceParameterSetLength ;
  12. bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;
  13. }
  14. unsigned int(8) numOfPictureParameterSets;
  15. for (i=0; i< numOfPictureParameterSets; i++) {
  16. unsigned int(16) pictureParameterSetLength;
  17. bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;
  18. }
  19. }

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代码:

[java]  view plain copy
  1. package cn.edu.xmu.zgy;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. public class ObtainSPSAndPPS {
  6. public void getSPSAndPPS(String fileName) throws IOException {
  7. File file = new File(fileName);
  8. FileInputStream fis = new FileInputStream(file);
  9. int fileLength = (int) file.length();
  10. byte[] fileData = new byte[fileLength];
  11. fis.read(fileData);
  12. // 'a'=0x61, 'v'=0x76, 'c'=0x63, 'C'=0x43
  13. byte[] avcC = new byte[] { 0x61, 0x76, 0x63, 0x43 };
  14. // avcC的起始位置
  15. int avcRecord = 0;
  16. for (int ix = 0; ix < fileLength; ++ix) {
  17. if (fileData[ix] == avcC[0] && fileData[ix + 1] == avcC[1]
  18. && fileData[ix + 2] == avcC[2]
  19. && fileData[ix + 3] == avcC[3]) {
  20. // 找到avcC,则记录avcRecord起始位置,然后退出循环。
  21. avcRecord = ix + 4;
  22. break;
  23. }
  24. }
  25. if (0 == avcRecord) {
  26. System.out.println("没有找到avcC,请检查文件格式是否正确");
  27. return;
  28. }
  29. // 加7的目的是为了跳过
  30. // (1)8字节的 configurationVersion
  31. // (2)8字节的 AVCProfileIndication
  32. // (3)8字节的 profile_compatibility
  33. // (4)8 字节的 AVCLevelIndication
  34. // (5)6 bit 的 reserved
  35. // (6)2 bit 的 lengthSizeMinusOne
  36. // (7)3 bit 的 reserved
  37. // (8)5 bit 的numOfSequenceParameterSets
  38. // 共6个字节,然后到达sequenceParameterSetLength的位置
  39. int spsStartPos = avcRecord + 6;
  40. byte[] spsbt = new byte[] { fileData[spsStartPos],
  41. fileData[spsStartPos + 1] };
  42. int spsLength = bytes2Int(spsbt);
  43. byte[] SPS = new byte[spsLength];
  44. // 跳过2个字节的 sequenceParameterSetLength
  45. spsStartPos += 2;
  46. System.arraycopy(fileData, spsStartPos, SPS, 0, spsLength);
  47. printResult("SPS", SPS, spsLength);
  48. // 底下部分为获取PPS
  49. // spsStartPos + spsLength 可以跳到pps位置
  50. // 再加1的目的是跳过1字节的 numOfPictureParameterSets
  51. int ppsStartPos = spsStartPos + spsLength + 1;
  52. byte[] ppsbt = new byte[] { fileData[ppsStartPos],
  53. fileData[ppsStartPos + 1] };
  54. int ppsLength = bytes2Int(ppsbt);
  55. byte[] PPS = new byte[ppsLength];
  56. ppsStartPos += 2;
  57. System.arraycopy(fileData, ppsStartPos, PPS, 0, ppsLength);
  58. printResult("PPS", PPS, ppsLength);
  59. }
  60. private int bytes2Int(byte[] bt) {
  61. int ret = bt[0];
  62. ret <<= 8;
  63. ret |= bt[1];
  64. return ret;
  65. }
  66. private void printResult(String type, byte[] bt, int len) {
  67. System.out.println(type + "长度为:" + len);
  68. String cont = type + "的内容为:";
  69. System.out.print(cont);
  70. for (int ix = 0; ix < len; ++ix) {
  71. System.out.printf("%02x ", bt[ix]);
  72. }
  73. System.out.println("\n----------");
  74. }
  75. public static void main(String[] args) throws IOException {
  76. new ObtainSPSAndPPS().getSPSAndPPS("c:\\zgy.h264");
  77. }
  78. }

运行结果如下:

[plain]  view plain copy
  1. SPS长度为:9
  2. SPS的内容为:67 42 00 1f e9 02 c1 2c 80
  3. ----------
  4. PPS长度为:4
  5. PPS的内容为:68 ce 06 f2
  6. ----------

需要下载源码以及我使用的h264码流片断的朋友可以 点击这里下载 !

看完希望您能有所收获 ^_^

H264 获取SPS与PPS(附源码)相关推荐

  1. H264 获取SPS与PPS

    转载地址 H264 获取SPS与PPS 在用Android手机进行h264硬编码的时候如果要进行视频流的实时传输与播放,就需要知道视频流的Sequence Parameter Sets (SPS) 和 ...

  2. VC++获取磁盘剩余空间(附源码)

      VC++开发常用功能一系列文章 (欢迎订阅,持续更新...) 第23章:VC++获取磁盘剩余空间(附源码) 源代码demo已上传到百度网盘:永久生效  ,代码实现了获取任一磁盘的剩余空间,返回MB ...

  3. 【转】百度API获取城市名地名(附源码)

    在做一个软件时,用到了定位功能.网上有很多关于google 的GPS定位,但网上关于google定位都没有用, 搜索下原因:(这里建议大家在中国就尽量不使用系统自带的定位) 因为Google的服务器不 ...

  4. 【Android App】获取照片里的位置信息及使用全球卫星导航系统(GNSS)获取位置实战(附源码和演示 超详细)

    需要全部代码请点赞关注收藏后评论区留言私信~~~ 一.获取照片里的位置信息 手机拍摄的相片还保存着时间.地点.镜头参数等信息,这些信息由相片接口工具ExifInterface管理,它的常用方法说明如下 ...

  5. SkeyeRTSPLive高效转码之SkeyeVideoEncoder高效硬件编码解决方案(附源码)

    在之前的<SkeyeRTSPLive高效转码之SkeyeVideoDecoder高效解码>系列文章中我们已经将视频解码成了原始图像数据(YUV/RGB),然后根据不同的转码需求进行编码.如 ...

  6. ESP32运行MicroPython通过MQTT上报温湿度到中移OneNET物联网平台(附源码)

    前言:MQTT是当下物联网用的比较多的协议,本篇聊一聊用esp32通过MQTT连接到中移OneNET物联网平台. OneNET平台创建产品和设备 1.​创建产品:开发者中心->全部产品-> ...

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

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

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

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

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

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

最新文章

  1. python 生产消费者_python之生产者消费者模型实现详解
  2. 人工神经网络理论、设计及应用_TensorFlow深度学习应用实践:教你如何掌握深度学习模型及应用...
  3. JavaScript与Asp.net传值
  4. 什么是用户智能,它与数据有什么关系?
  5. 递归-汉诺塔(代码、分析、汇编)
  6. 部署Docker前必须问自己的四个问题
  7. 永不消逝的缓存数据:Adaptec 5445Z RAID卡评测(连载之一)
  8. python模拟鼠标点击linux_Python模拟实现Linux系统unix2dos功能
  9. 【报告分享】 百度2021国潮骄傲搜索大数据报告-百度x人民网(附下载)
  10. 使用python+Pyqt5来写一个简易串口调试助手
  11. Java工程师学习指南(完结篇)
  12. html5化妆品网站源码,织梦响应式化妆美妆品类展示网站模板dedecms移动手机端HTML5自适应整站源码...
  13. ubuntu18.04在状态栏显示网速
  14. DL | DeepDream过程和原理概要
  15. ctrlaltdel命令手册
  16. Mac安装jekyll踩坑
  17. 人和摩托最快达到目的地
  18. Plague Inc
  19. linux系统如何安装mtk驱动程序,模块编译问题 给MTK芯片的wifi网卡编译linux驱动 系统是mint...
  20. 数据探查平台-元数据对标专利 -- 普帝

热门文章

  1. 【2015 SACC】手机淘宝性能优化全记录
  2. 数字化转型为PDCA管理赋能
  3. GreenDao的使用以及断点续传
  4. IB考试结束就想溜?这些后续工作可要注意
  5. 博弈论——非完全信息博弈
  6. mysql 索引长度限制_修改Mysql索引长度限制
  7. java五大框架有哪些_Java五大框架
  8. 【Geomagic】定义全局坐标系
  9. 程序员屌丝逆袭之路不是炒股
  10. GBDT与xgb区别,以及梯度下降法和牛顿法的数学推导