最近做國標級聯,鼓搗了個簡單的ps流封裝,做分享做筆記。
#include <stdint.h>
#include <string>
#include <memory.h>
const uint8_t PS_HEAD[] = {
/*PS頭*/
0x00, 0x00, 0x01, 0xba,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*時間戳*/
0x00, 0x00, 0x03,
0xf8
};
const uint8_t SYS_MAP_HEAD[] = {
/*PS_SYS頭*/
0x00, 0x00, 0x01, 0xbb,
0x00, 0x0c, /*sys頭長度,不含自己,6+3*流的數目*/
0x80, 0x00, 0x01, /*速率*/
0x01, 0x87, /*音頻流數,視頻流數加3個1標識*/
0xff, /**/
0xb9, 0x17, 0x00, 0xb8, 0x03, 0x40, /*流信息,b9視頻,b8音頻*/
/*PS_MAP頭*/
0x00, 0x00, 0x01, 0xbc,
0x00, 0x12, /*psm長度*/
0x04, 0xff, /**/
0x00, 0x00, 0x00, 0x08, /*固定2路流*/
0x1b, 0xe0, 0x00, 0x00, /*視頻*/
0x90, 0xc0, 0x00, 0x00, /*音頻*/
0x00, 0x00, 0x00, 0x00 /*4b CRC,暫時沒設置*/
};
const uint8_t PES_HEAD[] = {
/*PS_PES頭*/
0x00, 0x00, 0x01, 0xe0,
0x00, 0x00, /*pes長度*/
0x88, 0x80, /*附加信息*/
0x0a, /*附加信息長度*/
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /*pts和pds*/
};
void SetHeaderTimeStamp(uint8_t *dest, uint64_t pts)
{
uint8_t *scr_buf = dest + 4;
scr_buf[0] = 0x40 | (((uint8_t)(pts >> 30) & 0x07) << 3) | 0x04 | ((uint8_t)(pts >> 28) & 0x03);
scr_buf[1] = (uint8_t)((pts >> 20) & 0xff);
scr_buf[2] = (((uint8_t)(pts >> 15) & 0x1f) << 3) | 0x04 | ((uint8_t)(pts >> 13) & 0x03);
scr_buf[3] = (uint8_t)((pts >> 5) & 0xff);
scr_buf[4] = (((uint8_t)pts & 0x1f) << 3) | 0x04;
scr_buf[5] = 1;
}
// 設置PES頭中的PTS和DTS字段
void SetPESTimeStamp(uint8_t *buff, uint64_t ts)
{
buff += 9;
// PTS
buff[0] = (uint8_t)(((ts >> 30) & 0x07) << 1) | 0x30 | 0x01;
buff[1] = (uint8_t)((ts >> 22) & 0xff);
buff[2] = (uint8_t)(((ts >> 15) & 0xff) << 1) | 0x01;
buff[3] = (uint8_t)((ts >> 7) & 0xff);
buff[4] = (uint8_t)((ts & 0xff) << 1) | 0x01;
// DTS
buff[5] = (uint8_t)(((ts >> 30) & 0x07) << 1) | 0x10 | 0x01;
buff[6] = (uint8_t)((ts >> 22) & 0xff);
buff[7] = (uint8_t)(((ts >> 15) & 0xff) << 1) | 0x01;
buff[8] = (uint8_t)((ts >> 7) & 0xff);
buff[9] = (uint8_t)((ts & 0xff) << 1) | 0x01;
}
int GetSinglePESHeader(uint8_t *header, uint64_t mtime, uint16_t farmLen)
{
farmLen += 13;
memcpy(header, PES_HEAD, sizeof(PES_HEAD));
*(header+4) = (uint8_t)(farmLen>>8);
*(header+5) = (uint8_t)farmLen;
SetPESTimeStamp(header, mtime);
return sizeof(PES_HEAD);
}
int GetPSHeader(uint8_t *header, uint64_t mtime, uint16_t farmLen, int streamType, int farmType)
{
if(streamType == 2) //語音包
{
GetSinglePESHeader(header, mtime, farmLen);
*(header+3) = 0xc0;
return sizeof(PES_HEAD);
}
else if(farmType == 1) //I幀
{
memcpy(header, PS_HEAD, sizeof(PS_HEAD));
SetHeaderTimeStamp(header, mtime);
header += sizeof(PS_HEAD);
memcpy(header, SYS_MAP_HEAD, sizeof(SYS_MAP_HEAD));
header += sizeof(SYS_MAP_HEAD);
GetSinglePESHeader(header, mtime, farmLen);
return sizeof(PS_HEAD) + sizeof(SYS_MAP_HEAD) + sizeof(PES_HEAD);
}
else
{
memcpy(header, PS_HEAD, sizeof(PS_HEAD));
SetHeaderTimeStamp(header, mtime);
header += sizeof(PS_HEAD);
GetSinglePESHeader(header, mtime, farmLen);
return sizeof(PS_HEAD) + sizeof(PES_HEAD);
}
}
unsigned char PSFrameBuffer[10*1024*1024]; //轉換後的ps幀緩存區
int TransPSFrame(char *pFrame, int nFrameLength, int nIFrameFlag, int nStreamType, u_int nTimeStamp)
{
if(!pFrame || !nFrameLength)
return 0;
// 每個pes最多65400數據
int PesLenth = nFrameLength > 65400 ? 65400 : nFrameLength;
// 第一個pes需要有ps頭,其它不需要,音頻直接打包pes(00 00 01 c0)
int psHeadLen = GetPSHeader(PSFrameBuffer, nTimeStamp, PesLenth, nStreamType, nIFrameFlag);
memcpy(PSFrameBuffer + psHeadLen, pFrame, PesLenth);
int psSize = psHeadLen + PesLenth;
int pod = PesLenth;
nFrameLength -= PesLenth;
while (nFrameLength > 0)
{
PesLenth = nFrameLength > 65400 ? 65400 : nFrameLength;
psHeadLen = GetSinglePESHeader(PSFrameBuffer + psSize, nTimeStamp, PesLenth);
memcpy(PSFrameBuffer + psSize + psHeadLen, pFrame + pod, PesLenth);
psSize += (PesLenth + psHeadLen);
pod += PesLenth;
nFrameLength -= PesLenth;
}
//static FILE *fp = fopen("my.ps", "wb");
//if(fp)
// fwrite(PSFrameBuffer, 1, psSize, fp);
return psSize;
}
TransPSFrame函數爲測試所用,應用時宜將得到的ps頭部和幀數據直接寫到rtp包中,省去memcpy過程。
SYS_MAP_HEAD中的數據是按國標ps流標準寫死的,如果要封裝格式數據需要修改。