刚开始学习有关TS、PAT、PMT方面的内容,参考了别人的一些程序,然后写了一个简单的解析TS的小程序。如果有地方错误,请发邮件给我843036544@qq.com。

#include<stdio.h>

#include<stdlib.h>
#include<string.h>

#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流,并找出PAT的PID和PAT的table
void parse_PAT(unsigned char *buffer,int len); //分析PAT,并找出所含频道的数目和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<FileSize/188;i++)
{
Read_Ts_Packet(file_handle,buffer,188); //读TS的packet函数,每次读188个字节到buffer
ret = parse_TS(buffer,188); //解析188个字节的TS's packet,并打印找到的PAT’s table。如果解析成功即找到PAT,则返回 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<num;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);

printf("find Audio and Video begin-------->\n");
for(i=0;i<program_list_num;i++)
{
Video_or_Audio_buffer = Find_video_audio(program_list[i].elementary_pid,
program_list[i].stream_type); //根据PID找到elementary流

printf("the program's PID is 0x%x\n",program_list[i].elementary_pid);
printf("the program's Table --------->\n");
for(j=0;j<188;j++)
{
printf("0x%x ",Video_or_Audio_buffer[j]); //打印elementary's table
}
memset(Video_or_Audio_buffer,0,sizeof(char)*188);
printf("\n");

}

free(pmt_buffer);
free(Video_or_Audio_buffer);
pmt_buffer = NULL;
Video_or_Audio_buffer = NULL;

fclose(file_handle);
printf("\n");

return 1;

}

/**************************************************
* read one TS packet's 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,int FileSize)
{
unsigned char *temp = buffer;
short pat_pid;
int i = 0;

if(buffer[0] != 0x47)
{
printf("it's not a ts packet!\n");
return 0;
}

while(temp < buffer + FileSize)
{
pat_pid = (temp[1] & 0x1f)<<8 | temp[2];

if(pat_pid != 0)
printf("finding PAT table ....\n");
else
{
printf("already find the PAT table\n");
printf("pat_pid = 0x%x\n",pat_pid);
printf("pat table ------->\n");
for(i=0;i<=187;i++)
{
printf("0x%x ",buffer[i]);
}
printf("\n");
return 1;
}

temp = temp + 188;

}

return 0;
}

/*******************************************************
* parse PAT table, get the PMT's 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 = temp[3] & 0x30;

if(adaptation_control == 0x10)
temp = buffer + 4 + 1;

else if (adaptation_control == 0x30)
{
adaptation_length = buffer[4];
temp = buffer + 4 + 1 +adaptation_length + 1;

}

else
{
return ;
}

section_length = (temp[1]&0x0f)<<8 | temp[2];

p = temp + 1 +section_length;
temp = temp + 8;

while(temp < p - 4)
{
prg_No = (temp[0]<<8) | (temp[1]);

if(prg_No == 0)
{
temp = temp + 4;
continue;
}

else
{
PMT_Pid = (temp[2]&0x1f)<<8 | temp[3];

programs[num].program_num = prg_No;
programs[num].pmt_pid = PMT_Pid;
// printf("pmt_pid is ox%x\n", PMT_Pid);
num ++;

temp = temp + 4;
}
}

}

void pronum_pmtid_printf()
{
unsigned int i;
printf("PAT table's program_num and PMT's PID:\n");
for(i=0;i<num;i++)
{
printf("program_num = 0x%x (%d),PMT_Pid = 0x%x (%d)\n",
programs[i].program_num,programs[i].program_num,
programs[i].pmt_pid,programs[i].pmt_pid);
}
}

void printf_program_list()
{
unsigned int i;
printf("All PMT Table's program list: \n");

for(i=0;i<program_list_num;i++)
{
printf("stream_type = 0x%x, elementary_pid = 0x%x\n",program_list[i].stream_type,program_list[i].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;j<FileSize/188;j++)
{
Read_Ts_Packet(file_handle,buffer,188);
if(buffer[0] != 0x47)
{
printf("It's not TS packet !\n");

}
else
{
pid = (buffer[1] & 0x1f)<< 8 | buffer[2];
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 = 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;j<FileSize/188;j++)
{
Read_Ts_Packet(file_handle,buffer,188);
if(buffer[0] != 0x47)
{
printf("It's not TS packet !\n");
}
else
{
pid = (buffer[1] & 0x1f)<< 8 | buffer[2];
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 ");

}

}

}
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 = temp[3] & 0x30;
if(adaptation_control == 0x10)
{
temp = buffer + 4 +1;
}
else if (adaptation_control == 0x30)
{
adaptation_length = buffer[4];
temp = buffer + 5 + adaptation_length + 1;
}
else
return;

section_length = (temp[1]&0x0f)<<8 | temp[2];
p = temp + 1 + section_length;

// temp = temp + 10;

program_info_length = (temp[10] & 0x0f) << 8 | temp[11];

temp = temp + 12 + program_info_length ;

for(;temp < p - 4;)
{
program_list[program_list_num].stream_type = temp[0],
program_list[program_list_num].elementary_pid = (temp[1]&0x1f) << 8 | temp[2];
ES_info_length = (temp[3]&0x0f) << 8 | temp[4];

temp = temp + 4 + ES_info_length + 1;

program_list_num ++ ;

}

}

非常感谢给予我帮助的大神们。

简单解析PAT、PMT的程序的更多相关文章

  1. 参考MySQL Internals手册,使用Golang写一个简单解析binlog的程序

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. MySQL作为最流行的开源关系型数据库,有大量的拥趸.其生态已经相当完善,各项特性在圈内都有大量研究.每次新特性发布,都会 ...

  2. TS流PAT/PMT详解

    一 从TS流开始 从MPEG-2到DVB,看着看着突然就出现了一大堆表格,什么PAT.PMT.CAT……如此多的表该怎样深入了解呢? 我们知道,数字电视机顶盒接收到的是一段段的码流,我们称之为TS(T ...

  3. 对 cloudwu 简单的 cstring 进行简单解析

    题外话 以前也用C写过字符串,主要应用的领域是,大字符串,文件读取方面.写的很粗暴,用的凑合着.那时候看见云风前辈的一个开源的 cstring 串. 当时简单观摩了一下,觉得挺好的.也没细看.过了较长 ...

  4. 第一个OC类、解析第一个OC程序

    01第一个OC 类 本文目录 • 一.语法简介 • 二.用Xcode创建第一个OC的类 • 三.第一个类的代码解析 • 四.添加成员变量 • 五.添加方法 • 六.跟Java的比较 • 七.创建对象 ...

  5. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  6. [ 转载 ] Java基础10--关于Object类下所有方法的简单解析

    关于Object类下所有方法的简单解析 类Object是类层次结构的根类,是每一个类的父类,所有的对象包括数组,String,Integer等包装类,所以了解Object是很有必要的,话不多说,我们直 ...

  7. 深度优先搜索DFS和广度优先搜索BFS简单解析

    转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...

  8. Netty学习——基于netty实现简单的客户端聊天小程序

    Netty学习——基于netty实现简单的客户端聊天小程序 效果图,聊天程序展示 (TCP编程实现) 后端代码: package com.dawa.netty.chatexample; import ...

  9. ThreadLocal 简单解析

    ThreadLocal 简单解析 基于jdk1.8 ThreadLocal一定不陌生,开发中常用,也是面试里的常客了,但是往往我们可能只是知道该类的作用.学习该类对于个人的多线程编码能力是大有裨益的, ...

随机推荐

  1. for循环实现Fibonacci数列

    Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n. 输出格 ...

  2. redis配置密码

    一. 更改配置文件 找到requirepass这行, [soft@node5 redis-3.0.6]$ grep 'requirepass' redis.conf#requirepass fooba ...

  3. ✨Shell脚本实现Base64 加密解密

    加密算法 # !/bin/bash # 全局变量 str="" base64_encode_string(){ # 源数据 source_string=$1 echo " ...

  4. oracle SCN推进恢复数据库 简单记录

    由于是在内网专用机器上操作,没有日志记录,下面做个简单记录:   前几天某供电局的的一个老数据库存储挂了,数据全部丢失,该库没有开归档,没接备份,怎么恢复? 由于存储损坏严重,从存储恢复不好搞. 好在 ...

  5. 11-13 模块_collections(不太重要)&time&random&os

    random:随机数模块 os:和操作系统打交道的模块 sys:和Python解释器打交道的模块 序列化模块:Python中的数据类型和str转换的模块 http://www.cnblogs.com/ ...

  6. 4.17 省选模拟赛 远行 LCT 启发式合并 倍增

    容易写出nQ的暴力 由于数据是期望的时间 所以直接dfs可以跑的很快 可以拿到70分. 当然 可以进一步优化暴力 使用换根dp 然后可以将暴力优化到n^2. const int MAXN=300010 ...

  7. CF 题目选做

    写省选的题目对noip没什么大用 关键是 细节题或者是思考题比较重要 练思维自然是CF比较好了 把我见到的比较好的CF题放上来刷一刷. LINK:Complete the projects 就是说一个 ...

  8. luogu P4884 多少个1?

    LINK:多少个1? 题目要求:\(\sum_{i=0}^{n-1}10^i \equiv k \mod m\) 最小的n. 看起来很难求的样子 这个同余式 看起来只能暴力枚举. 不过既然是同余 我们 ...

  9. C语言中的数据转换和定义常量

    一.数据转换 1.数据类型转换:C 语言中如果一个表达式中含有不同类型的常量和变量,在计算时,会将它们自动转换为同一种类型:在 C 语言中也可以对数据类型进行强制转换: 2.自动转换规则: a)浮点数 ...

  10. 转)JVM Internals

    http://blog.jamesdbloom.com/JVMInternals.html (基于 Java 7)