数字电视pmt表解析C语言程序
时间: 2023-12-17 09:04:30 浏览: 131
以下是一个简单的C语言程序,用于解析数字电视PMT表:
```
#include <stdio.h>
#include <stdlib.h>
#define PMT_START_CODE 0x2
#define PMT_PID 0x1000
#define PMT_SECTION_LENGTH 1024
typedef struct {
unsigned char table_id;
unsigned short section_syntax_indicator;
unsigned short section_length;
unsigned short program_number;
unsigned short version_number;
unsigned short current_next_indicator;
unsigned char section_number;
unsigned char last_section_number;
unsigned short PCR_PID;
unsigned short program_info_length;
} PMT_header;
typedef struct {
unsigned short stream_type;
unsigned short elementary_PID;
unsigned short ES_info_length;
} PMT_stream;
int main() {
FILE *fp;
unsigned char buffer[PMT_SECTION_LENGTH];
int n, i, j, k, l;
PMT_header header;
PMT_stream stream;
fp = fopen("pmt.bin", "rb");
if (fp == NULL) {
printf("Failed to open file.\n");
return 1;
}
n = fread(buffer, 1, PMT_SECTION_LENGTH, fp);
if (n != PMT_SECTION_LENGTH) {
printf("Failed to read file.\n");
fclose(fp);
return 1;
}
if (buffer[0] != PMT_START_CODE || ((buffer[1] << 8) | buffer[2]) != PMT_PID) {
printf("Invalid PMT table.\n");
fclose(fp);
return 1;
}
header.table_id = buffer[0];
header.section_syntax_indicator = (buffer[1] >> 7) & 0x1;
header.section_length = ((buffer[1] & 0x0F) << 8) | buffer[2];
header.program_number = (buffer[3] << 8) | buffer[4];
header.version_number = (buffer[5] >> 1) & 0x1F;
header.current_next_indicator = buffer[5] & 0x1;
header.section_number = buffer[6];
header.last_section_number = buffer[7];
header.PCR_PID = ((buffer[8] & 0x1F) << 8) | buffer[9];
header.program_info_length = ((buffer[10] & 0x0F) << 8) | buffer[11];
printf("Table ID: %d\n", header.table_id);
printf("Section syntax indicator: %d\n", header.section_syntax_indicator);
printf("Section length: %d\n", header.section_length);
printf("Program number: %d\n", header.program_number);
printf("Version number: %d\n", header.version_number);
printf("Current/Next indicator: %d\n", header.current_next_indicator);
printf("Section number: %d\n", header.section_number);
printf("Last section number: %d\n", header.last_section_number);
printf("PCR PID: %d\n", header.PCR_PID);
printf("Program info length: %d\n", header.program_info_length);
i = 12 + header.program_info_length;
while (i < header.section_length - 4) {
stream.stream_type = buffer[i];
stream.elementary_PID = ((buffer[i + 1] & 0x1F) << 8) | buffer[i + 2];
stream.ES_info_length = ((buffer[i + 3] & 0x0F) << 8) | buffer[i + 4];
printf("\nStream Type: %d\n", stream.stream_type);
printf("Elementary PID: %d\n", stream.elementary_PID);
printf("ES Info Length: %d\n", stream.ES_info_length);
i += 5 + stream.ES_info_length;
}
fclose(fp);
return 0;
}
```
这个程序使用了两个结构体,一个用于存储PMT头部信息,另一个用于存储每个流的信息。程序首先打开一个名为“pmt.bin”的文件,读取1024个字节的数据到缓冲区中。接下来,程序检查PMT表的开始码和PID是否正确,如果不正确则退出。然后程序解析头部信息,并打印出来。最后,程序解析每个流的信息,并打印出来。
阅读全文