最近做国标级联,鼓捣了个简单的ps流封装,做分享做笔记。
#include <stdint.h>
#include <string>
#include <memory.h>
#define H264_ID 0x1b
#define H265_ID 0x24
#define MPEG_ID 0x10
#define SVACV_ID 0x80
#define G711_ID 0x90
#define SVACA_ID 0x9b
const uint8_t PS_HEAD[] = {
/*PS头*/
0x00, 0x00, 0x01, 0xba,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*时间戳*/
0x01, 0x47, 0xb3,
0xf8
};
const uint8_t SYS_MAP_HEAD[] = {
/*PS_SYS头*/
0x00, 0x00, 0x01, 0xbb,
0x00, 0x0c, /*sys头长度,不含自己,6+3*流的数目*/
0x80, 0xa3, 0xd9, /*速率*/
0x04, 0xe1, /*音频流数,视频流数加3个1标识*/
0xff, /**/
0xb9, 0xe0, 0x00, 0xb8, 0xc0, 0x40, /*流信息,b9视频,b8音频*/
/*PS_MAP头*/
0x00, 0x00, 0x01, 0xbc,
0x00, 0x12, /*psm长度*/
0xe1, 0xff, /**/
0x00, 0x00, 0x00, 0x08, /*固定2路流*/
0x1b, 0xe0, 0x00, 0x00, /*视频,第一个字节(0x1b), 跟具不同的视频编码改变即可封装不同的流,见开头宏定义*/
0x90, 0xc0, 0x00, 0x00, /*音频,同视频*/
0x00, 0x00, 0x00, 0x00 /*4b CRC,暂时没设置*/
};
const uint8_t PES_HEAD[] = {
/*PS_PES头*/
0x00, 0x00, 0x01, 0xe0,
0x00, 0x00, /*pes长度*/
0x80, 0xc0, /*附加信息*/
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);