TS流解析之PAT表格解析

來源:互聯網
上載者:User

 

PAT表格定義如下:

typedef struct TS_PAT_Program
{
 unsigned program_number    :16; //節目號
 unsigned program_map_PID   :13;   //節目映射表的PID,節目號大於0時對應的PID,每個節目對應一個
}TS_PAT_Program;

//PAT表結構體
typedef struct TS_PAT
{
    unsigned table_id                        : 8; //固定為0x00 ,標誌是該表是PAT
    unsigned section_syntax_indicator        : 1; //段文法標誌位,固定為1
    unsigned zero                            : 1; //0
    unsigned reserved_1                        : 2; // 保留位
    unsigned section_length                    : 12; //表示這個位元組後面有用的位元組數,包括CRC32
    unsigned transport_stream_id            : 16; //該傳輸串流的ID,區別於一個網路中其它多工流
    unsigned reserved_2                        : 2;// 保留位
    unsigned version_number                    : 5; //範圍0-31,表示PAT的版本號碼
    unsigned current_next_indicator            : 1; //發送的PAT是當前有效還是下一個PAT有效
    unsigned section_number                    : 8; //分段的號碼。PAT可能分為多段傳輸,第一段為00,以後每個分段加1,最多可能有256個分段
    unsigned last_section_number            : 8;  //最後一個分段的號碼
 
 std::vector<TS_PAT_Program> program;
    unsigned reserved_3                        : 3; // 保留位
    unsigned network_PID                    : 13; //網路資訊表(NIT)的PID,節目號為0時對應的PID為network_PID

    unsigned CRC_32                            : 32;  //CRC32校正碼
} TS_PAT;

 

解析代碼如下:

HRESULT CTS_Stream_Parse::adjust_PAT_table( TS_PAT * packet, unsigned char * buffer)
{
    packet->table_id                    = buffer[0];
    packet->section_syntax_indicator    = buffer[1] >> 7;
    packet->zero                        = buffer[1] >> 6 & 0x1;
    packet->reserved_1                    = buffer[1] >> 4 & 0x3;
    packet->section_length                = (buffer[1] & 0x0F) << 8 | buffer[2];
 
    packet->transport_stream_id            = buffer[3] << 8 | buffer[4];
 
    packet->reserved_2                    = buffer[5] >> 6;
    packet->version_number                = buffer[5] >> 1 &  0x1F;
    packet->current_next_indicator        = (buffer[5] << 7) >> 7;
    packet->section_number                = buffer[6];
    packet->last_section_number            = buffer[7];

    int len = 0;
    len = 3 + packet->section_length;
    packet->CRC_32                        = (buffer[len-4] & 0x000000FF) << 24
  | (buffer[len-3] & 0x000000FF) << 16
  | (buffer[len-2] & 0x000000FF) << 8
  | (buffer[len-1] & 0x000000FF);
 
  
 int n = 0;
    for ( n = 0; n < packet->section_length - 12; n += 4 )
    {
  unsigned  program_num = buffer[8 + n ] << 8 | buffer[9 + n ];  
        packet->reserved_3                = buffer[10 + n ] >> 5;
  
  packet->network_PID = 0x00;
  if ( program_num == 0x00)
  {  
            packet->network_PID = (buffer[10 + n ] & 0x1F) << 8 | buffer[11 + n ];

   TS_network_Pid = packet->network_PID; //記錄該TS流的網路PID

   TRACE(" packet->network_PID %0x /n/n", packet->network_PID );
  }
        else
        {
   TS_PAT_Program PAT_program;
   PAT_program.program_map_PID = (buffer[10 + n] & 0x1F) << 8 | buffer[11 + n];
   PAT_program.program_number = program_num;
   packet->program.push_back( PAT_program );
   
   TS_program.push_back( PAT_program );//向全域PAT節目數組中添加PAT節目資訊     
        }        
    }
 return 0;
}

因此,PAT資料解析結果如下:

PAT資料

table_id    :0x00                            //8  
section_syntax_indicator   :0x01           // 1
'0'              :0x00                       // 1
reserved             0x03                // 2
section_length      :0x00d                    // 12
transport_stream_id    :0x0000                 // 16
reserved                        :0x03        // 2
version_number            :0x00              // 5
current_next_indicator   :0x01               // 1
section_number              :0x00            // 8
last_section_number         :0x00            // 8
program_number     :0x0001                    // 16
  reserved                :0x07               // 3
program_map_PID      :0x03e8             // 13
CRC         :0x f0 0b d7 79

由解析結構可知,該PAT表格中沒有網路資訊包資訊,只包含一個節目,其PID為0x03e8

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.