TS流解析之TS包头解析

来源:互联网 发布:淘宝退货多久自动退款 编辑:程序博客网 时间:2024/05/03 11:31

根据前一篇中各数据的定义及数据结构,对数据进行分别解析如下:

TS包头定义:

[cpp] view plaincopyprint?
  1. typedef struct TS_packet_header  
  2. {  
  3.     unsigned sync_byte                        : 8; //同步字节, 固定为0x47,表示后面的是一个TS分组  
  4.     unsigned transport_error_indicator        : 1; //传输误码指示符  
  5.     unsigned payload_unit_start_indicator     : 1; //有效荷载单元起始指示符     
  6.     unsigned transport_priority               : 1; //传输优先, 1表示高优先级,传输机制可能用到,解码用不着  
  7.     unsigned PID                              : 13; //PID  
  8.     unsigned transport_scrambling_control     : 2; //传输加扰控制  
  9.     unsigned adaption_field_control           : 2; //自适应控制 01仅含有效负载,10仅含调整字段,  
  10.                                                      //11含有调整字段和有效负载。为00解码器不进行处理  
  11.     unsigned continuity_counter               : 4; //连续计数器 一个4bit的计数器,范围0-15  
  12. } TS_packet_header;  

TS包头解析代码:

[cpp] view plaincopyprint?
  1. HRESULT CTS_Stream_Parse::adjust_TS_packet_header( TS_packet_header* TS_header )  
  2. {  
  3.  unsigned char buf[4];  
  4.    
  5.     memcpy(buf, TS_header, 4);  
  6.     TS_header->transport_error_indicator        = buf[1] >> 7;  
  7.     TS_header->payload_unit_start_indicator     = buf[1] >> 6 & 0x01;  
  8.     TS_header->transport_priority               = buf[1] >> 5 & 0x01;  
  9.     TS_header->PID                              = (buf[1] & 0x1F) << 8 | buf[2];  
  10.     TS_header->transport_scrambling_control     = buf[3] >> 6;  
  11.     TS_header->adaption_field_control           = buf[3] >> 4 & 0x03;  
  12.     TS_header->continuity_counter               = buf[3] & 0x0F; // 四位数据,应为0x0F xyy 09.03.18  
  13.   
  14.  return 0;  
  15. }  

如下为一个TS包数据:

0x47 0x40 0x00 0x120x00 0x00 0xb0 0x0d 0x00 0x00 0xc1 0x00 0x00 0x00 0x01 0xe3 0xe8 0xf0 0x0b 0xd7 0x79 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff

分析知道前四位0x47 0x40 0x00 0x12TS头部即为TS包头数据,解析如下:

[cpp] view plaincopyprint?
  1. sync_byte:                  0x47  
  2. transport_error_indicator:    0x00  
  3. payload_unit_start_indicator: 0x01  
  4. transport_priority:           0x00  
  5.   
  6. PID:                          0x0000  
  7. transport_scrambling_control: 0x00  
  8. adaptation_field_control:     0x01  
  9. continuity_counter:           0x02  


PID = 0x0000,表示此TS包的内容为PSI信息表格的PAT表格数据,在4字节的TS包头之后的第一个字节的Point_field = 0x00, 表示偏移量为0,即紧随其后的即为PAT的数据信息

原创粉丝点击