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);
}

0 0
原创粉丝点击