1. 无符号指数哥伦布编码
private static int Ue(byte[] pBuff, int nLen) {
int nZeroNum = 0;
while (nStartBit < nLen * 8) {
if ((pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8))) != 0) {
break;
}
nZeroNum++;
nStartBit++;
}
nStartBit++;
int dwRet = 0;
for (int i = 0; i < nZeroNum; i++) {
dwRet <<= 1;
if ((pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8))) != 0) {
dwRet += 1;
}
nStartBit++;
}
return (1 << nZeroNum) - 1 + dwRet;
}
2 有符号指数哥伦布编码
private static int Se(byte[] pBuff, int nLen) {
int UeVal = Ue(pBuff, nLen);
double k = UeVal; int nValue = (int) Math.ceil(k / 2);
if (UeVal % 2 == 0) {
nValue = -nValue;
}
return nValue;
}
3 读取bits
private static int u(int BitCount, byte[] buf) {
int dwRet = 0;
for (int i = 0; i < BitCount; i++) {
dwRet <<= 1;
if ((buf[nStartBit / 8] & (0x80 >> (nStartBit % 8))) != 0) {
dwRet += 1;
}
nStartBit++;
}
return dwRet;
}
4 全局变量 ,记录读取数据的偏移量 单位为bit
private static int nStartBit = 0;
5 sps解析
public static boolean h264_decode_sep_parameter_set(byte[] spsBuf, int nLen, int[] size);
//如果 spsBuf 包含 0x00 0x00 0x01 则 nStartBit += 4*8;需要偏移32位 ; nLen 为 spsBuf的长度;size[] 用来存放解析出来的宽和高
//其他的参照H264官方文档即可
6 注意:h264在编码时为了防止有效数据中出现0x00 0x00 0x01,会在有效数据的0x00 0x00 后插入0x03,在解析sps时需要 先将0x03去掉,然后再进行解析。