1、解析TS流PAT和PMT 代码#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流的packetint parse_TS(unsigned char *buffer,int FileSize); /分析TS流,并找出PAT的PID和PAT的tablevoid parse_PAT(unsigned char *buf
2、fer,int len); /分析PAT,并找出所含频道的数目和PMT的PIDvoid pronum_pmtid_printf(); /打印PMT的PIDunsigned char* Find_PMT(unsigned short pmt_pid); /找出PMT的tablevoid parse_PMT(unsigned char *buffer,int len,unsigned short pmt_pid); /解析PMT,找出其中的Video和Audio的PIDvoid printf_program_list(); /打印PMT table中包含的stream的类型和PIDunsigned
3、 char* Find_video_audio(unsigned short program_pid,unsigned char type); /找出Video或者Audio的tabletypedef struct unsigned short program_num; /programs num unsigned short pmt_pid; /PROGRAM;typedef struct unsigned char stream_type; unsigned short elementary_pid;PRO_LIST;PROGRAM programs10 = 0,0; /用来存储PMT的P
4、ID和数量unsigned int num = 0; /total programPRO_LIST program_list10 = 0,0; /用来存储PMT中stream的类型和PIDunsigned int program_list_num = 0;FILE *file_handle; /指向TS流的指针unsigned int FileSize = 0;int main() unsigned char buffer188 = 0; unsigned char *pmt_buffer, *Video_or_Audio_buffer; unsigned int i=0,j=0,ret=0;
5、 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_han
6、dle) /判断是否打开文件 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 = %dn,FileSize); rewind(file_handle); / e
7、quivalent (void) feek(file_handle,0L,SEEK_SET) 将file_handle指针移动到文件头位置 printf(find PAT begin-n); for(i=0;in); for(i=0;in); for(j=0;jn); for(i=0;in); for(j=0;j188;j+) printf(0x%x ,Video_or_Audio_bufferj); /打印elementarys table memset(Video_or_Audio_buffer,0,sizeof(char)*188); printf(n); free(pmt_buffer
8、); free(Video_or_Audio_buffer); pmt_buffer = NULL; Video_or_Audio_buffer = NULL; fclose(file_handle); printf(n); return 1; /* * read one TS packets data * */void Read_Ts_Packet(FILE *file_handle,unsigned char *packet_buf,int len) fread(packet_buf,188,1,file_handle);int parse_TS(unsigned char *buffer
9、,int FileSize) unsigned char *temp = buffer; short pat_pid; int i = 0; if(buffer0 != 0x47) printf(its not a ts packet!n); return 0; while(temp buffer + FileSize) pat_pid = (temp1 & 0x1f)n); for(i=0;i=187;i+) printf(0x%x ,bufferi); printf(n); return 1; temp = temp + 188; return 0;/* * parse PAT table
10、, get the PMTs PID * */void parse_PAT(unsigned char *buffer,int len) unsigned char *temp, *p; char adaptation_control; int adaptation_length,i=0; unsigned short section_length,prg_No,PMT_Pid; temp = buffer; adaptation_control = temp3 & 0x30; if(adaptation_control = 0x10) temp = buffer + 4 + 1; else
11、if (adaptation_control = 0x30) adaptation_length = buffer4; temp = buffer + 4 + 1 +adaptation_length + 1; else return ; section_length = (temp1&0x0f)8 | temp2; p = temp + 1 +section_length; temp = temp + 8; while(temp p - 4) prg_No = (temp08) | (temp1); if(prg_No = 0) temp = temp + 4; continue; else
12、 PMT_Pid = (temp2&0x1f)8 | temp3; programsnum.program_num = prg_No; programsnum.pmt_pid = PMT_Pid;/ printf(pmt_pid is ox%xn, PMT_Pid); num +; temp = temp + 4; void pronum_pmtid_printf() unsigned int i; printf(PAT tables program_num and PMTs PID:n); for(i=0;inum;i+) printf(program_num = 0x%x (%d),PMT
13、_Pid = 0x%x (%d)n, programsi.program_num,programsi.program_num, programsi.pmt_pid,programsi.pmt_pid); void printf_program_list() unsigned int i; printf(All PMT Tables program list: n); for(i=0;iprogram_list_num;i+) printf(stream_type = 0x%x, elementary_pid = 0x%xn,program_listi.stream_type,program_l
14、isti.elementary_pid); printf(n);unsigned char* Find_PMT(unsigned short pmt_pid) unsigned int i=0,j=0; int pid; unsigned char *buffer; buffer = (unsigned char *)malloc(sizeof(char)*188); memset(buffer,0,sizeof(char)*188); rewind(file_handle); for(j=0;jFileSize/188;j+) Read_Ts_Packet(file_handle,buffe
15、r,188); if(buffer0 != 0x47) printf(Its not TS packet !n); else pid = (buffer1 & 0x1f) 8 | buffer2; if(pid = pmt_pid) printf(PMT Table already find!n); return buffer; else printf(finding PMT table.n); unsigned char* Find_video_audio(unsigned short program_pid,unsigned char type) unsigned int i = 0, j
16、 = 0 ; int pid; unsigned char *buffer; buffer = (unsigned char *)malloc(sizeof(char)*188); memset(buffer,0,sizeof(char)*188); rewind(file_handle); for(j=0;jFileSize/188;j+) Read_Ts_Packet(file_handle,buffer,188); if(buffer0 != 0x47) printf(Its not TS packet !n); else pid = (buffer1 & 0x1f) 8 | buffe
17、r2; if(program_pid = pid) if(type = 0x02) printf(Find a program and this program is Video type!n); else if(type = 0x03) printf(Find a program and this program is Audio type!n); else printf(Find a program but this program is other type !n); return buffer; else printf(finding Video or Audio table.n );
18、 void parse_PMT(unsigned char *buffer,int len,unsigned short pmt_pid) unsigned char *temp, *p; char adaptation_control; int adaptation_length,i=0; int program_info_length; int ES_info_length; unsigned short section_length,pid; temp = buffer; adaptation_control = temp3 & 0x30; if(adaptation_control =
19、 0x10) temp = buffer + 4 +1; else if (adaptation_control = 0x30) adaptation_length = buffer4; temp = buffer + 5 + adaptation_length + 1; else return; section_length = (temp1&0x0f)8 | temp2; p = temp + 1 + section_length;/ temp = temp + 10; program_info_length = (temp10 & 0x0f) 8 | temp11; temp = temp + 12 + program_info_length ; for(;temp p - 4;) program_listprogram_list_num.stream_type = temp0, program_listprogram_list_num.elementary_pid = (temp1&0x1f) 8 | temp2; ES_info_length = (temp3&0x0f) 8 | temp4; temp = temp + 4 + ES_info_length + 1; program_list_num + ;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1