TS包结构
来源:互联网 发布:算法 pdf 编辑:程序博客网 时间:2024/05/16 11:29
TS包头定义:
typedef struct TS_packet_header
{
unsigned sync_byte; 8 //同步字节,固定为0x47,表示后面是一个TS分组
unsigned transport_error_indicator; 1//传输误码指示符
unsigned payload_unit_start_indicator; 1//有效荷载单元起始指示符
unsigned transport_priority; 1//传输优先,1,表示高优先级,传输机制可能用到,解码用不着
unsigned PID; 13//PID
unsigned transport_scrambling_control; 2//传输加扰控制
unsigned adaption_field_control; 2//自适应控制 01仅含有效负载,10仅含调整字段,11含有调整字段和有效负载。为00解码器不进行处理
unsigned continuity_counter; 4//连续计数器 一个4bit的计数器,范围0-1
}
以下结合实例展示对TS包的操作,下面程序是基于libdvbcsa开源代码实现对TS包进行CSA2.0算法的加扰。
int main (void)
{
FILE *in_file;
int read_size;
unsigned char temp[184] = {0};
unsigned char desData[188] = {0};
unsigned char transport_scrambling_control = 0;
unsigned char adaptation_field_control = 0;
unsigned int byPayloadLen = 0;
unsigned int byCurrentKeySel = 0;
unsigned int count = 0;
unsigned int i = 0;
int result = 0;
FILE *out_file;
unsigned int write_count = 0;
struct timeval tm = {0};
unsigned int tmptm1 = 0;
unsigned int tmptm2 = 0;
dvbcsa_cw_t odd_cw = { 0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A};
dvbcsa_cw_t even_cw = { 0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A};
int filter_pids[] = {0x0101, 0x0102};
struct dvbcsa_key_s*key = dvbcsa_key_alloc();
in_file = fopen ("src.ts", "rb");
if (NULL==in_file)
{
printf("Can't open file for reading\n");
return -1;
}
out_file = fopen("test.ts","wb");
if (out_file < 0)
{
printf("can't open test.ts!\n");
return -1;
}
while (!feof (in_file))
{
int curPid;
read_size = fread (buffer, 1,TS_PACKET_LENGTH, in_file);
if (read_size != TS_PACKET_LENGTH)
{
printf("Read Len error\n");
continue;
}
if (buffer[0] != 0x47)//TS包头,判断是否TS包
{
printf("Not 0x47\n");
continue;
}
curPid = getword(&buffer[1]) & 0x1fff;//获取PID
if (curPid != filter_pids[0]
&& curPid != filter_pids[1])
{
goto exit1;
}
transport_scrambling_control = buffer[3] & 0xC0;//第3个字节的前两位表示加扰控制,&0xC0得到它
adaptation_field_control = buffer[3] & 0x30;//第三个字节的三四位表示自适应控制, &0x30得到它
if (adaptation_field_control == 0x20) // 这个包没有有效数据 为10时
{
//return -1;
goto exit1;
}
else if (adaptation_field_control == 0x30)//为11时
{
byPayloadLen = 184 - 1 - buffer[4];
}
else
{
byPayloadLen = 184;
}
if (transport_scrambling_control == 0xC0) // 奇密钥加扰
{
byCurrentKeySel = 1;
}
else if (transport_scrambling_control == 0x80) // 偶密钥加扰
{
byCurrentKeySel = 2;
}
else // 不加扰
{
//return -1;
goto exit1;
}
buffer[3] &= 0x3F; // 清除加扰控制字段
if ( byCurrentKeySel == 1)
{
dvbcsa_key_set(odd_cw, key);
}
else
{
dvbcsa_key_set(even_cw, key);
}
dvbcsa_decrypt(key, buffer+ 188 - byPayloadLen, byPayloadLen);
result = fwrite(buffer, 1, 188, out_file);
if (result < 0)
{
printf("======write file failed=====\n");
return -1;
}
goto exit2;
exit1:
//memcpy(desData,buffer,188);
result = fwrite(buffer, 1, 188, out_file);
if (result < 0)
{
printf("======exit write file failed=====\n");
return -1;
}
exit2:
;
}
fclose(out_file);
fclose(in_file);
return (0);
}
- TS包结构
- MPEG2/TS 包结构简述
- MPEG2/TS 包结构简述
- TS包大小
- TS 中的SDT包
- TS包得数据结构
- TS抓包分析
- TS结构解析
- TS结构相关说明
- TS流结构学习
- ts文件结构
- TS包头语法结构
- TS包头语法结构
- ts packet结构分析
- TS科普4 寻找下一个TS包
- TS科普6 TS包关系
- TS包的包头描述
- TS流分析-PAT包
- HDU 3870Catch the Theves
- Contiki与IoT
- Linux内核源代码情景分析-强制性调度
- C语言——字符串查找字符的两个函数
- IOS UI_音频AVAudioPlayer
- TS包结构
- [hihoCoder]#1062 : 最近公共祖先·一
- 1输出x^2的值
- 关于多线程常见问题
- linux 下用oss播放音乐
- 机器学习 Logistic Regression
- java基础之I/O
- 建造者模式
- 交叉验证(cross-validation)