自己写了个解析PES头取PTS和DTS的程序但感觉数据流和实际的标准相差很大啊
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<fcntl.h>
#include<sys/stat.h>
#define PSM 0xbc
#define PAD 0xbe
#define PS2 0xbf
#define ECM 0xf0
#define DSMMCC 0xf2
#define TYPE_E 0xf8
int main(int argc, char ** argv)
{
int fd;
char *buf;
int n, i;
unsigned char stream_id,pes_scrambling_control,pes_priority ,data_alignment_indicator ,copyright ,original_or_copy;
unsigned char pts_dts_flags, escr_flag, es_rate_flag, dsm_trick_mode_flag, additional_copy_info_flag, pes_crc_flag, pes_extension_flag,pes_header_data_length;
unsigned int pes_len;
unsigned long long pts, dts;
buf = malloc(sizeof(char) * 20000);
if(buf == NULL)
{
printf("malloc error \n");
return -1;
}
fd = open(argv[1], O_RDONLY);
if(fd == -1)
{
printf("open error:%s\n", strerror(errno));
return -1;
}
while(1)
{
n = read(fd, buf, 20000);
if(n == 0)
{
printf("end of file\n");
return 1;
}
if( n == -1)
{
printf("read error:%s\n", strerror(errno));
return -1;
}
for(i = 0; i < n; i++)
{
if(buf[i] == 0x00 && buf[i+1] == 0x00 && buf[i+2] == 0x01)
{
stream_id = buf[i+3];
pes_len = 0;
pes_len = (buf[i+4] << 8);
pes_len += buf[i+5];
printf("pes_len:%u\n", pes_len);
if(stream_id != PSM &&
stream_id != PAD &&
stream_id != PS2 &&
stream_id != ECM &&
stream_id != DSMMCC &&
stream_id != TYPE_E)
{
if((buf[i+6] & 0xc0 ) != 0x80)
printf("this may be a bad pes head\n");
pes_scrambling_control = ((buf[i+6] >> 4) & 0x03);
pes_priority = ((buf[i+6] >> 3) & 0x01);
data_alignment_indicator = ((buf[i+6] >> 2) & 0x01);
copyright = ((buf[i+6] >> 1) & 0x01);
original_or_copy = (buf[i+6] & 0x01);
pts_dts_flags = ((buf[i+7] >> 6) & 0x03);
escr_flag = ((buf[i+7] >> 5) & 0x01);
es_rate_flag = ((buf[i+7] >> 4) & 0x01);
dsm_trick_mode_flag = ((buf[i+7] >> 3) & 0x01);
additional_copy_info_flag = ((buf[i+7] >> 2) & 0x01);
pes_crc_flag = ((buf[i+7] >> 1) & 0x01);
pes_extension_flag = (buf[i+7] & 0x01);
pes_header_data_length = buf[i+8];
i+=9;
if(pts_dts_flags == 0x10)
{
if((buf[i] & 0xf0) != 0x20)
printf("this may be a bad pts \n");
pts = 0;
pts = ((buf[i] & 0x0e) << 29);
pts += (buf[i+1] << 22);
pts += ((buf[i+2] & 0xfe) << 14);
pts += (buf[i+3] << 7);
pts += (buf[i+4] >> 1) & 0x7f;
printf("PTS:%llu\n", pts);
i+=5;
}
if(pts_dts_flags == 0x11)
{
if((buf[i] & 0xf0) != 0x30)
printf("this may be a bad pts \n");
pts = 0;
pts = ((buf[i] & 0x0e) << 29);
pts += (buf[i+1] << 22);
pts += ((buf[i+2] & 0xfe) << 14);
pts += (buf[i+3] << 7);
pts += ((buf[i+4] >> 1) & 0x7f);
if((buf[i] & 0xf0) != 0x10)
printf("this may be a bad dts \n");
dts = 0;
dts = ((buf[i+5] & 0x0e) << 29);
dts += (buf[i+6] << 22);
dts += ((buf[i+7] & 0xfe) << 14);
dts += (buf[i+8] << 7);
dts += ((buf[i+9] >> 1) & 0x7f);
i+=10;
printf("PTS:%llu\tDTS:%llu\n", pts, dts);
}
}
}
}
}
}
|
|