forked from berolinaro/dvbv5-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgramMapTable.cpp
219 lines (208 loc) · 7.42 KB
/
ProgramMapTable.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Format of the Program Map Table (PMT) is defined in
// ISO/IEC 13818-1, available here: http://www.ece.cmu.edu/~ece796/documents/MPEG-2_Systems_IS.doc
#include "ProgramMapTable.h"
#include "Util.h"
#include <cxxabi.h>
#include <cassert>
void ProgramMapTable::dump(std::ostream &where, std::string const &indent) const {
Util::SaveIOState s(where);
DVBTable::dump(where, indent);
uint16_t pcrPid = ((_data[0]&0b00011111)<<8)|_data[1];
uint16_t programInfoLength = ((_data[2]&0b00001111)<<8)|_data[3];
where << indent << "PCR Pid " << pcrPid << std::endl;
where << indent << "Program info length" << programInfoLength << std::endl;
if(programInfoLength)
Util::hexdump(_data+4, programInfoLength, where);
unsigned char *pos = _data+4+programInfoLength;
while(pos < _data+_dataLength) {
uint8_t streamType = pos[0];
uint16_t pid = ((pos[1]&0b00011111)<<8)|pos[2];
uint16_t esInfoLength = ((pos[3]&0b00001111)<<8)|pos[4];
where << indent << " Stream type: ";
if(streamType == 0x00)
where << "Reserved";
else if(streamType == 0x01)
where << "ISO/IEC 11172-2 Video";
else if(streamType == 0x02)
where << "H.262 Video";
else if(streamType == 0x03)
where << "ISO/IEC 11172-3 Audio";
else if(streamType == 0x04)
where << "ISO/IEC 13818-3 Audio";
else if(streamType == 0x05)
where << "ISO/IEC 13818-1 reserved";
else if(streamType == 0x06)
where << "Private data";
else if(streamType == 0x07)
where << "ISO/IEC 13522 MHEG";
else if(streamType == 0x08)
where << "IEC 13818-1 Annex A DSM CC";
else if(streamType == 0x09)
where << "ITU-T Rec. H.222.1";
else if(streamType == 0x0a)
where << "ISO/IEC 13818-6 type A";
else if(streamType == 0x0a)
where << "ISO/IEC 13818-6 type B";
else if(streamType == 0x0a)
where << "ISO/IEC 13818-6 type C";
else if(streamType == 0x0a)
where << "ISO/IEC 13818-6 type D";
else if(streamType == 0x0a)
where << "ISO/IEC 13818-1 auxiliary";
else if(streamType >= 0x0f && streamType <= 0x7f)
where << "ISO/IEC 13818-1 reserved";
else
where << "User Private";
where << " (" << std::hex << static_cast<int>(streamType) << ")" << std::endl;
where << indent << " PID " << pid << std::endl;
if(esInfoLength)
where << indent << " ES Info:" << std::endl;
pos += 5;
unsigned char *end=pos+esInfoLength;
while(pos<end) {
DVBDescriptor *d=DVBDescriptor::get(pos);
d->dump(std::cerr, indent + "\t\t");
delete d;
}
}
}
Program::Program(ProgramMapTables const &pmts) {
_programNumber = (*pmts.begin())->programNumber();
_pcrPid = (((*pmts.begin())->_data[0]&0b00011111)<<8)|(*pmts.begin())->_data[1];
int i=0;
for(auto const &pmt: pmts) {
if(_programNumber != pmt->programNumber()) {
std::cerr << "Program number" << ++i << ": " << pmt->programNumber() << std::endl;
std::cerr << "Found a program map table with inconsistent program numbers! Ignoring..." << std::endl;
assert(false);
continue;
}
if(_pcrPid != (((pmt->_data[0]&0b00011111)<<8)|pmt->_data[1])) {
std::cerr << "Found a program map table with inconsistent program numbers! Ignoring..." << std::endl;
assert(false);
continue;
}
uint16_t programInfoLength = ((pmt->_data[2]&0b00001111)<<8)|pmt->_data[3];
unsigned char *pos = pmt->_data+4;
while(pos < pmt->_data+4+programInfoLength) {
_descriptors.push_back(DVBDescriptor::get(pos));
}
pos = pmt->_data+4+programInfoLength;
while(pos < pmt->_data+pmt->_dataLength) {
uint8_t streamType = pos[0];
uint16_t pid = ((pos[1]&0b00011111)<<8)|pos[2];
uint16_t esInfoLength = ((pos[3]&0b00001111)<<8)|pos[4];
pos += 5;
unsigned char *end=pos+esInfoLength;
std::vector<DVBDescriptor*> descriptors;
while(pos<end) {
DVBDescriptor *d=DVBDescriptor::get(pos);
descriptors.push_back(d);
}
_streams.push_back(Stream(streamType, pid, descriptors));
}
}
}
void Stream::dump(std::ostream &where, std::string const &indent) const {
Util::SaveIOState s(where);
where << std::hex << std::setfill('0');
where << indent << "Stream Type: " << std::setw(2) << static_cast<int>(_streamType) << " ";
if(_streamType == 0x00)
where << "Reserved";
else if(_streamType == 0x01)
where << "ISO/IEC 11172-2 Video";
else if(_streamType == 0x02)
where << "H.262 Video";
else if(_streamType == 0x03)
where << "ISO/IEC 11172-3 Audio";
else if(_streamType == 0x04)
where << "ISO/IEC 13818-3 Audio";
else if(_streamType == 0x05)
where << "ISO/IEC 13818-1 reserved";
else if(_streamType == 0x06)
where << "Private data";
else if(_streamType == 0x07)
where << "ISO/IEC 13522 MHEG";
else if(_streamType == 0x08)
where << "IEC 13818-1 Annex A DSM CC";
else if(_streamType == 0x09)
where << "ITU-T Rec. H.222.1";
else if(_streamType == 0x0a)
where << "ISO/IEC 13818-6 type A";
else if(_streamType == 0x0a)
where << "ISO/IEC 13818-6 type B";
else if(_streamType == 0x0a)
where << "ISO/IEC 13818-6 type C";
else if(_streamType == 0x0a)
where << "ISO/IEC 13818-6 type D";
else if(_streamType == 0x0a)
where << "ISO/IEC 13818-1 auxiliary";
else if(_streamType == 0x11) // This is a guess -- not documented in ISO/IEC 13818-1, seen on "Das Erste HD" in Berlin DVB-T2
where << "AAC-LATM audio";
else if(_streamType == 0x1b)
where << "HD Video"; // This is a guess -- not documented in ISO/IEC 13818-1, seen on "SRF info HD" in EWGoms DVB-C
else if(_streamType == 0x24)
where << "UHD Video"; // This is a guess -- not documented in ISO/IEC 13818-1, seen on "UHD Demo Channel" in EWGoms DVB-C
else if(_streamType >= 0x0f && _streamType <= 0x7f)
where << "ISO/IEC 13818-1 reserved";
else
where << "User Private";
where << std::endl;
where << indent << "PID: " << std::setw(4) << _pid << std::endl;
if(!_descriptors.empty()) {
where << indent << "Descriptors:" << std::endl;
for(auto const &d: _descriptors)
d->dump(where, indent+"\t");
}
}
bool Stream::isAudio() const {
return _streamType == 0x03 || _streamType == 0x04 || _streamType == 0x11;
}
bool Stream::isVideo() const {
return _streamType == 0x01 || _streamType == 0x02 || _streamType == 0x1b || _streamType == 0x24;
}
bool Stream::isTeletext() const {
// FIXME identify the streamType for Teletext
return false;
}
bool Stream::isSubtitle() const {
// FIXME identify the streamType for subtitles
return false;
}
bool Stream::isPcr() const {
// FIXME do we ever get a PCR in the stream list?
return false;
}
Stream::StreamType Stream::type() const {
if(isAudio())
return Audio;
else if(isVideo())
return Video;
else if(isTeletext())
return Teletext;
else if(isSubtitle())
return Subtitle;
else if(isPcr())
return PCR;
return Other;
}
void Program::dump(std::ostream &where, std::string const &indent) const {
Util::SaveIOState s(where);
where << std::hex << std::setfill('0');
where << indent << "Program number: " << std::setw(4) << _programNumber << std::endl;
where << indent << "\t" << "PCR Pid: " << std::setw(4) << _pcrPid << std::endl;
where << indent << "\t" << "Number of descriptors: " << _descriptors.size() << std::endl;
for(auto const &d: _descriptors) {
std::cerr << "Dumping descriptor" << std::endl;
std::cerr << "Type " << static_cast<int>(d->tag()) << std::endl;
std::cerr << abi::__cxa_demangle(typeid(d).name(), nullptr, nullptr, nullptr) << std::endl;
d->dump(where, indent+"\t");
std::cerr << "Done dumping" << std::endl;
}
where << indent << "\t" << "Streams:" << std::endl;
for(auto const &s: _streams)
s.dump(where, indent+"\t\t");
}
Program ProgramMapTables::program() const {
return Program(*this);
}