解析TS流PAT和PMT 代码

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

#include

#include

#define ts_path "/home/huohuo/huangwork/work/birds.ts" //TS文件的绝对路径

void Read_Ts_Packet(FILE *file_handle,unsigned char *packet_buf,int len); //读一个TS流的packet

int parse_TS(unsigned char *buffer,int FileSize); //分析TS流,并找出PA T的PID和PAT的table

void parse_PAT(unsigned char *buffer,int len); //分析PA T,并找出所含频道的数目和PMT的PID

void pronum_pmtid_printf(); //打印PMT的PID unsigned char* Find_PMT(unsigned short pmt_pid); //找出PMT的table

void parse_PMT(unsigned char *buffer,int len,unsigned short pmt_pid); //解析PMT,找出其中的Video和Audio的PID

void printf_program_list(); //打印PMT table中包含的stream的类型和PID

unsigned char* Find_video_audio(unsigned short program_pid,unsigned char type); //找出Video或者Audio的table

typedef struct

{

unsigned short program_num; //program's num

unsigned short pmt_pid; //

}PROGRAM;

typedef struct

{

unsigned char stream_type;

unsigned short elementary_pid;

}PRO_LIST;

PROGRAM programs[10] = {{0,0}}; //用来存储PMT的PID和数量unsigned int num = 0; //total program

PRO_LIST program_list[10] = {{0,0}}; //用来存储PMT中stream的类型和PID

unsigned int program_list_num = 0;

FILE *file_handle; //指向TS流的指针

unsigned int FileSize = 0;

int main()

{

unsigned char buffer[188] = {0};

unsigned char *pmt_buffer, *Video_or_Audio_buffer;

unsigned int i=0,j=0,ret=0;

pmt_buffer = (unsigned char*)malloc(sizeof(char)*188); //给buffer分配空间

memset(pmt_buffer,0,sizeof(char)*188);

//清空buffer

Video_or_Audio_buffer = (unsigned char*)malloc(sizeof(char)*188);

memset(Video_or_Audio_buffer,0,sizeof(char)*188);

file_handle = fopen(ts_path,"rb+"); //以二进制方式打开TS文件

if(NULL == file_handle) //判断是否打开文件

{

perror("fopen");

printf("open file error!\n");

return 0;

}

else

printf("open file success!\n");

fseek(file_handle,0,SEEK_END); //指针file_handle将以SEEK_END位置偏移0个位置,即将指针移动到文件尾

FileSize = ftell(file_handle); // 计算file_handle到文件头的偏移字节数,即计算文件的大小

printf("file size = %d\n",FileSize);

rewind(file_handle); // equivalent (void) feek(file_handle,0L,SEEK_SET) 将file_handle 指针移动到文件头位置

printf("find PAT begin-------->\n");

for(i=0;i

{

Read_Ts_Packet(file_handle,buffer,188); //读TS的packet函数,每次读188个字节到buffer

ret = parse_TS(buffer,188); //解析188个字节的

TS's packet,并打印找到的PA T’s table。如果解析成功即找到PA T,则返回1,否则返回0

if(ret == 1)

{

break;

}

else

{

printf("There is no PAT table!\n");

}

}

if(ret == 1)

{

parse_PAT(buffer,188); //解析PAT,并找出所含频道的数目和PMT的PID }

pronum_pmtid_printf(); //打印PMT的PID

rewind(file_handle);

printf("find PMT begin -------->\n");

for(i=0;i

{

pmt_buffer = Find_PMT(programs[i].pmt_pid); //根据PMT的PID找到PMT's table

printf("PMT table -------->\n");

for(j=0;j<188;j++)

{

printf("0x%x ",pmt_buffer[j]); //打印PMT

}

if(pmt_buffer)

{

parse_PMT(pmt_buffer,188,programs[i].pmt_pid); //解析找到的PMT,得到Video、Audio等的PID

}

memset(pmt_buffer,0,sizeof(char)*188);

printf("\n");

}

printf_program_list(); //打印elementary 流的PID和type。

rewind(file_handle);

相关文档
最新文档