-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse-pmt.cpp
More file actions
133 lines (100 loc) · 3.09 KB
/
parse-pmt.cpp
File metadata and controls
133 lines (100 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <iostream>
#include <arpa/inet.h>
uint32_t ntoh(uint32_t t) {
return ntohl(t);
}
uint16_t ntoh(uint16_t t) {
return ntohs(t);
}
uint8_t ntoh(uint8_t t) {
return t;
}
template<typename T>
struct ReadWrapper {
ReadWrapper(T& t) : t_(t) {}
T& t_;
};
template<typename T>
std::istream& operator >> (std::istream& is, ReadWrapper<T> const& r) {
is.read(reinterpret_cast<char*>(&r.t_), sizeof(r.t_));
r.t_ = ntoh(r.t_);
return is;
}
template<typename T>
ReadWrapper<T> read(T& t) {
return ReadWrapper<T>(t);
}
struct PmtSectionHeader {
uint8_t tableid_;
uint16_t length_;
static const size_t header_length = 9;
uint16_t program_number_;
uint8_t version_;
bool current_next_;
uint8_t section_number_;
uint8_t last_section_number_;
uint16_t pcr_pid_;
uint16_t program_info_length_;
};
std::istream& operator >> (std::istream& is, PmtSectionHeader& h) {
uint16_t d;
uint8_t v;
is >> read(h.tableid_) >> read(d) >> read(h.program_number_) >> read(v) >> read(h.section_number_)
>> read(h.last_section_number_) >> read(h.pcr_pid_) >> read(h.program_info_length_);
h.length_ = d & ((1 << 12) - 1);
h.version_ = (v & ((1 << 5) -1)) >> 1;
h.current_next_ = v & 1;
h.pcr_pid_ &= ((1 << 12) - 1);
h.program_info_length_ &= ((1 << 9) - 1);
return is;
}
int main(int argc, char* argv[]) {
std::cin.rdbuf()->pubsetbuf(0,0);
PmtSectionHeader hdr = {0};
if(std::cin >> hdr) {
std::cout << "{";
std::cout << "\"tableid\":" << (uint32_t)hdr.tableid_ << ",";
std::cout << "\"program_number\":" << hdr.program_number_ << ",";
std::cout << "\"version\":" << (uint32_t)hdr.version_ << ",";
std::cout << "\"number\":" << (uint32_t)hdr.section_number_ << ",";
std::cout << "\"lastnumber\":" << (uint32_t)hdr.last_section_number_ << ",";
std::cout << "\"pcrpid\":" << hdr.pcr_pid_ << ",";
std::cout << "\"descriptors\":[";
for(size_t count = 0; count != hdr.program_info_length_;) {
if(count) std::cout << ",";
uint8_t tag;
uint8_t length;
std::cin >> read(tag) >> read(length);
std::cin.ignore(length);
std::cout << "{\"tag\":" << (uint32_t)tag << "}";
count += sizeof(tag) + sizeof(length) + length;
}
std::cout << "], \"streams\": [";
for(size_t count = 0; count != hdr.length_ - PmtSectionHeader::header_length - hdr.program_info_length_ - sizeof(uint32_t);) {
if(count) std::cout << ',';
uint8_t type;
uint16_t pid;
uint16_t len;
std::cin >> read(type) >> read(pid) >> read(len);
pid &= ((1 << 13)-1);
len &= ((1 << 10)-1);
count += sizeof(type) + sizeof(pid) + sizeof(len) + len;
std::cout << "{" << "\"type\":" << (uint32_t)type << ",\"pid\":" << pid << ",\"descriptors\":[";
for(size_t count = 0; count < len;) {
if(count) std::cout << ",";
uint8_t tag;
uint8_t length;
std::cin >> read(tag) >> read(length);
std::cin.ignore(length);
count += sizeof(tag) + sizeof(length) + length;
std::cout << "{\"tag\":" << (uint32_t)tag << "}";
}
std::cout << "]}";
}
std::cout << "]";
uint32_t crc;
std::cin >> read(crc);
std::cout << "}" << std::endl;
}
}