织梦CMS - 轻松建站从此开始!

罗索

H264 获取SPS与PPS(附源码)

落鹤生 发布于 2012-07-13 15:25 点击:次 
在用Android手机进行h264硬编码的时候如果要进行视频流的实时传输,就需要知道视频流的Sequence Parameter Sets (SPS) 和Picture Parameter Set (PPS)。今天算是看明白如何获取SPS和PPS,在这里记录下来,希望有需要的朋友可以在这里获取到一些些的帮助。
TAG:

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

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

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

mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

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

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

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

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

运行结果如下:

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

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

 

 

(zgyulongfei)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201207/16153.html]
本文出处:CSDN博客 作者:zgyulongfei 原文
顶一下
(2)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容