TS流解析之TS包頭解析

來源:互聯網
上載者:User

 

根據前一篇中各資料的定義及資料結構,對資料進行分別解析如下:

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-15
} TS_packet_header;

 

TS包頭解析代碼:

HRESULT CTS_Stream_Parse::adjust_TS_packet_header( TS_packet_header* TS_header )
{
 unsigned char buf[4];
 
    memcpy(buf, TS_header, 4);
    TS_header->transport_error_indicator        = buf[1] >> 7;
    TS_header->payload_unit_start_indicator    = buf[1] >> 6 & 0x01;
    TS_header->transport_priority                = buf[1] >> 5 & 0x01;
    TS_header->PID                            = (buf[1] & 0x1F) << 8 | buf[2];
    TS_header->transport_scrambling_control    = buf[3] >> 6;
    TS_header->adaption_field_control            = buf[3] >> 4 & 0x03;
    TS_header->continuity_counter                = buf[3] & 0x0F; // 四位元據,應為0x0F xyy 09.03.18

 return 0;
}

如下為一個TS包資料:

0x47 0x40 0x00 0x12 0x00 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包頭資料,解析如下:

sync_byte   :0x47
transport_error_indicator: 0x00
payload_unit_start_indicator: 0x01
transport_priority  : 0x00

  PID                     :0x0000
transport_scrambling_control  :0x00
adaptation_field_control  :0x01                                     

continuity_counter   :0x02

PID = 0x0000,表示此TS包的內容為PSI資訊表格的PAT表格式資料,在4位元組的TS包頭之後的第一個位元組的Point_field = 0x00, 表示位移量為0,即緊隨其後的即為PAT的資料資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.