H264 获取SPS与PPS(附源码)

原文链接http://blog.csdn.net/zgyulongfei/article/details/7538523


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

		// 加7的目的是为了跳过
		// (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码流片断的朋友可以 点击这里下载






  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值