收藏 分享(赏)

解析TS流PAT和PMT 代码.doc

上传人:精品资料 文档编号:10132740 上传时间:2019-10-12 格式:DOC 页数:10 大小:58KB
下载 相关 举报
解析TS流PAT和PMT 代码.doc_第1页
第1页 / 共10页
解析TS流PAT和PMT 代码.doc_第2页
第2页 / 共10页
解析TS流PAT和PMT 代码.doc_第3页
第3页 / 共10页
解析TS流PAT和PMT 代码.doc_第4页
第4页 / 共10页
解析TS流PAT和PMT 代码.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

1、#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 *buffe

2、r,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 中包含的strea

3、m 的类型和 PIDunsigned char* Find_video_audio(unsigned short program_pid,unsigned char type); /找出Video 或者 Audio 的 tabletypedef structunsigned short program_num; /programs numunsigned short pmt_pid; /PROGRAM;typedef structunsigned char stream_type;unsigned short elementary_pid;PRO_LIST;PROGRAM programs10

4、 = 0,0; /用来存储 PMT 的 PID 和数量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; u

5、nsigned int i=0,j=0,ret=0;pmt_buffer = (unsigned char*)malloc(sizeof(char)*188); /给 buffer 分配空间memset(pmt_buffer,0,sizeof(char)*188);/清空 bufferVideo_or_Audio_buffer = (unsigned char*)malloc(sizeof(char)*188);memset(Video_or_Audio_buffer,0,sizeof(char)*188);file_handle = fopen(ts_path,“rb+“); /以二进制方式

6、打开 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 = %dn“,

7、FileSize);rewind(file_handle); / equivalent (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;jn“);for(i=0;i=187;i+)printf(“0x%x “,bufferi);printf(“n“);return 1;temp = temp + 188;return 0;/* parse PAT ta

8、ble, 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 if(adaptation_control = 0x10)temp = buffer + 4 + 1;else if (adaptation

9、_control = 0x30)adaptation_length = buffer4;temp = buffer + 4 + 1 +adaptation_length + 1;elsereturn ;section_length = (temp1p = temp + 1 +section_length;temp = temp + 8;while(temp p - 4)prg_No = (temp08) | (temp1);if(prg_No = 0)temp = temp + 4;continue;elsePMT_Pid = (temp2programsnum.program_num = p

10、rg_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_Pid = 0x%x (%d)n“,programsi.program_num,programsi.program_num,p

11、rogramsi.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_listi.elementary_pid);printf(“n“);unsigned char* Find_PMT(unsigned

12、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,buffer,188);if(buffer0 != 0x47)printf(“Its not TS packet !n“);elsepid = (buffe

13、r1 if(pid = pmt_pid)printf(“PMT Table already find!n“);return buffer;elseprintf(“finding PMT table.n“);unsigned char* Find_video_audio(unsigned short program_pid,unsigned char type)unsigned int i = 0, j = 0 ;int pid;unsigned char *buffer;buffer = (unsigned char *)malloc(sizeof(char)*188);memset(buff

14、er,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“);elsepid = (buffer1 if(program_pid = pid)if(type = 0x02)printf(“Find a program and this program is Video type!n“);else if(type = 0x03)printf(“Fi

15、nd a program and this program is Audio type!n“);elseprintf(“Find a program but this program is other type !n“);return buffer;elseprintf(“finding Video or Audio table.n “);void parse_PMT(unsigned char *buffer,int len,unsigned short pmt_pid)unsigned char *temp, *p;char adaptation_control;int adaptatio

16、n_length,i=0;int program_info_length;int ES_info_length;unsigned short section_length,pid;temp = buffer;adaptation_control = temp3 if(adaptation_control = 0x10)temp = buffer + 4 +1;else if (adaptation_control = 0x30)adaptation_length = buffer4;temp = buffer + 5 + adaptation_length + 1; elsereturn;se

17、ction_length = (temp1p = temp + 1 + section_length;/ temp = temp + 10;program_info_length = (temp10 temp = temp + 12 + program_info_length ;for(;temp p - 4;)program_listprogram_list_num.stream_type = temp0,program_listprogram_list_num.elementary_pid = (temp1ES_info_length = (temp3temp = temp + 4 + ES_info_length + 1;program_list_num + ;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报