在用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);
-
-
- byte[] avcC = new byte[] { 0x61, 0x76, 0x63, 0x43 };
-
-
- 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]) {
-
- avcRecord = ix + 4;
- break;
- }
- }
- if (0 == avcRecord) {
- System.out.println("没有找到avcC,请检查文件格式是否正确");
- return;
- }
-
-
-
-
-
-
-
-
-
-
-
- int spsStartPos = avcRecord + 6;
- byte[] spsbt = new byte[] { fileData[spsStartPos],
- fileData[spsStartPos + 1] };
- int spsLength = bytes2Int(spsbt);
- byte[] SPS = new byte[spsLength];
-
- spsStartPos += 2;
- System.arraycopy(fileData, spsStartPos, SPS, 0, spsLength);
- printResult("SPS", SPS, spsLength);
-
-
-
-
- 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
- ----------
转载链接:http://blog.csdn.net/zgyulongfei/article/details/7538523