PMT(Program Map Table):节目映射表,该表的PID是由PAT提供给出的。通过该表可以得到一路节目中包含的信息,例如,该路节目由哪些流构成和这些流的类型(视频,音频,数据),指定节目中各流对应的PID,以及该节目的PCR所对应的PID。
PMT表中包含的数据如下:
(1) 当前频道中包含的所有Video数据的PID
(2) 当前频道中包含的所有Audio数据的PID
(3) 和当前频道关联在一起的其他数据的PID(如数字广播,数据通讯等使用的PID)
PMT结构定义:
typedef struct TS_PMT_Stream { unsigned stream_type : 8; //指示特定PID的节目元素包的类型。该处PID由elementary PID指定 unsigned elementary_PID : 13; //该域指示TS包的PID值。这些TS包含有相关的节目元素 unsigned ES_info_length : 12; //前两位bit为00。该域指示跟随其后的描述相关节目元素的byte数 unsigned descriptor; }TS_PMT_Stream;
PMT表结构体:
typedef struct TS_PMT { unsigned table_id : 8; //固定为0x02, 表示PMT表 unsigned section_syntax_indicator : 1; //固定为0x01 unsigned zero : 1; //0x01 unsigned reserved_1 : 2; //0x03 unsigned section_length : 12;//首先两位bit置为00,它指示段的byte数,由段长度域开始,包含CRC unsigned program_number : 16;// 指出该节目对应于可应用的Program map PID unsigned reserved_2 : 2; //0x03 unsigned version_number : 5; //指出TS流中Program map section的版本号 unsigned current_next_indicator : 1; //当该位置1时,当前传送的Program map section可用 //当该位置0时,指示当前传送的Program map section不可用,下一个TS流的Program map section有效 unsigned section_number : 8; //固定为0x00 unsigned last_section_number : 8; //固定为0x00 unsigned reserved_3 : 3; //0x07 nsigned PCR_PID : 13; //指明TS包的PID值,该TS包含有PCR域, //该PCR值对应于由节目号指定的对应节目,如果对于私有数据流的节目定义与PCR无关,这个域的值将为0x1FFF。 unsigned reserved_4 : 4; //预留为0x0F unsigned program_info_length : 12; //前两位bit为00。该域指出跟随其后对节目信息的描述的byte数。 std::vector<TS_PMT_Stream> PMT_Stream; //每个元素包含8位, 指示特定PID的节目元素包的类型。该处PID由elementary PID指定 unsigned reserved_5 : 3; //0x07 unsigned reserved_6 : 4; //0x0F unsigned CRC_32 : 32; } TS_PMT;
解析代码:
HRESULT CTS_Stream_Parse::adjust_PMT_table ( TS_PMT * packet, unsigned char * buffer ) { packet->table_id = buffer[0]; packet->section_syntax_indicator = buffer[1] >> 7; packet->zero = buffer[1] >> 6 & 0x01; packet->reserved_1 = buffer[1] >> 4 & 0x03; packet->section_length = (buffer[1] & 0x0F) << 8 | buffer[2]; packet->program_number = 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]; packet->reserved_3 = buffer[8] >> 5; packet->PCR_PID = ((buffer[8] << 8) | buffer[9]) & 0x1FFF; PCRID = packet->PCR_PID; packet->reserved_4 = buffer[10] >> 4; packet->program_info_length = (buffer[10] & 0x0F) << 8 | buffer[11]; // Get CRC_32 int len = 0; len = packet->section_length + 3; packet->CRC_32 = (buffer[len-4] & 0x000000FF) << 24 | (buffer[len-3] & 0x000000FF) << 16 | (buffer[len-2] & 0x000000FF) << 8 | (buffer[len-1] & 0x000000FF); int pos = 12; // program info descriptor if ( packet->program_info_length != 0 ) pos += packet->program_info_length; // Get stream type and PID for ( ; pos <= (packet->section_length + 2 ) - 4; ) { TS_PMT_Stream pmt_stream; pmt_stream.stream_type = buffer[pos]; packet->reserved_5 = buffer[pos+1] >> 5; pmt_stream.elementary_PID = ((buffer[pos+1] << 8) | buffer[pos+2]) & 0x1FFF; packet->reserved_6 = buffer[pos+3] >> 4; pmt_stream.ES_info_length = (buffer[pos+3] & 0x0F) << 8 | buffer[pos+4]; pmt_stream.descriptor = 0x00; if (pmt_stream.ES_info_length != 0) { pmt_stream.descriptor = buffer[pos + 5]; for( int len = 2; len <= pmt_stream.ES_info_length; len ++ ) { pmt_stream.descriptor = pmt_stream.descriptor<< 8 | buffer[pos + 4 + len]; } pos += pmt_stream.ES_info_length; } pos += 5; packet->PMT_Stream.push_back( pmt_stream ); TS_Stream_type.push_back( pmt_stream ); } return 0; }
下面分析一段TS流的一个pachet:
包头:47 41 00 10
数据: 00 02 b0 29 00 01 c1 00 00 f001 f0 0c 05 04 48 44 4d 56 88 04 0f ff fc fc 1b f0 11 f0 06 28 04 42 c028 bf 90 f2 00 f0 00 f8 d6 bd 6b
先分析包头:
第一个的包头为47 40 00 10,比较可知只是PID不同
它的PID为0x100,即为256
所以第二个包的数据表示的是PMT
再分析具体数据:
同理第一个字节00是调整字节
02 b0 29 00 01 c1 00 00 f001 f0 0c 05 04 48 44 4d 56 88 04 0f ff fc fc 1b f0 11 f0 06 28 04 42 c028 bf 90 f2 00 f0 00 f8 d6 bd 6b
PMT
table_id |
8 |
1个字节 |
section_syntax_indicator |
1 |
2个字节 |
‘0’ |
1 |
|
reserved |
2 |
|
section_length |
12 |
|
program_number |
16 |
2个字节 |
reserved |
2 |
1个字节 |
version_number |
5 |
|
current_next_indicator |
1 |
|
section_number |
8 |
1个字节 |
last_section_number |
8 |
1个字节 |
reserved |
3 |
2个字节 |
PCR_PID |
13 |
|
reserved |
4 |
2个字节 |
program_info_length |
12 |
|
循环:descriptor()(0-N) |
||
循环开始(0-N1) |
||
stream_type |
8 |
1个字节 |
reserved |
3 |
2个字节 |
elementary_PID |
13 |
|
reserved |
4 |
2个字节 |
ES_info_length |
12 |
|
循环:descriptor()(0-N2) |
||
循环结束 |
||
CRC_32 |
32 |
4个字节 |
02 b0 29 00 01 c1 00 00 f0 01 f0 0c 05 04 48 44 4d 5688 04 0f ff fc fc 1b f0 11 f0 06 28 04 42 c0 28 bf 90 f200 f0 00 f8 d6 bd 6b
table_id为02,对于PMT必须为0x02
b0 29二进制为1011 0000 0010 1001
section_syntax_indicator为1,对于PMT必须为1
section_length为0x029,即为32+9=41
00 01
program_number为0x0001
c1二进制为1100 0001
version_number为00000
current_next_indicator为1,表示当前传送的program_map_section可用
00 00
分别表示section_number和last_section_number
f0 01二进制为1111 0000 0000 0001
PCR_PID为0x1001,即为2的12次方+1=4097,该字段只是TS包的PID值
f0 0c二进制为11110000 0000 1100
program_info_length为0x00c,即为12