MythTV  master
dvbtables.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Copyright (c) 2005, Daniel Thor Kristjansson
3 
4 #include <cmath>
5 
6 #include "dvbtables.h"
7 #include "dvbdescriptors.h"
8 
10 {
12 
13  _ptrs.clear();
14  _ptrs.push_back(_tsc_ptr + 2);
15  for (uint i=0; _ptrs[i] + 6 <= _ptrs[0] + TransportStreamDataLength(); i++)
16  _ptrs.push_back(_ptrs[i] + 6 + TransportDescriptorsLength(i));
17 }
18 
20 {
21  QString str = QString("NIT: NetID(%1) transports(%2)\n")
22  .arg(NetworkID()).arg(TransportStreamCount());
23  str.append(QString("Section (%1) Last Section (%2) IsCurrent (%3)\n")
24  .arg(Section()).arg(LastSection()).arg(IsCurrent()));
25 
26  if (0 != NetworkDescriptorsLength())
27  {
28  str.append(QString("Network descriptors length: %1\n")
29  .arg(NetworkDescriptorsLength()));
30  vector<const unsigned char*> desc =
33  for (size_t i = 0; i < desc.size(); i++)
34  str.append(QString(" %1\n")
35  .arg(MPEGDescriptor(desc[i]).toString()));
36  }
37 
38  for (uint i = 0; i < TransportStreamCount(); i++)
39  {
40  str.append(QString(" Transport #%1 TSID(0x%2) ")
41  .arg(i, 2, 10).arg(TSID(i), 0, 16));
42  str.append(QString("original_network_id(0x%2) desc_len(%3)\n")
43  .arg(OriginalNetworkID(i), 0, 16)
45 
46  if (0 != TransportDescriptorsLength(i))
47  {
48  str.append(QString(" Transport descriptors length: %1\n")
50  vector<const unsigned char*> desc =
53  for (size_t j = 0; j < desc.size(); j++)
54  str.append(QString(" %1\n")
55  .arg(MPEGDescriptor(desc[j]).toString()));
56  }
57  }
58  return str;
59 }
60 
62 {
63  if (_cached_network_name.isEmpty())
64  {
65  desc_list_t parsed =
68 
69  const unsigned char *desc =
71 
72  if (desc)
73  {
74  auto nndesc = NetworkNameDescriptor(desc);
75  if (nndesc.IsValid())
76  _cached_network_name = nndesc.Name();
77  }
78  if (_cached_network_name.isEmpty())
79  _cached_network_name = QString("Net ID 0x%1")
80  .arg(NetworkID(), 0, 16);
81  }
82  return _cached_network_name;
83 }
84 
86 {
87  if (VerifyCRC())
88  {
90  SetCRC(CalcCRC());
91  return true;
92  }
93  return false;
94 }
95 
97 {
98  _ptrs.clear();
99  _ptrs.push_back(pesdata() + 11);
100  uint i = 0;
101  while ((_ptrs[i] + 5) < (pesdata() + Length()))
102  {
103  _ptrs.push_back(_ptrs[i] + 5 + ServiceDescriptorsLength(i));
104  i++;
105  }
106 }
107 
109 {
110  QString str =
111  QString("SDT: TSID(0x%1) original_network_id(0x%2) services(%3)\n")
112  .arg(TSID(), 0, 16).arg(OriginalNetworkID(), 0, 16)
113  .arg(ServiceCount());
114  str.append(QString("Section (%1) Last Section (%2) IsCurrent (%3)\n")
115  .arg(Section()).arg(LastSection()).arg(IsCurrent()));
116 
117  for (uint i = 0; i < ServiceCount(); i++)
118  {
119  str.append(QString(" Service #%1 SID(0x%2) ")
120  .arg(i, 2, 10).arg(ServiceID(i), 0, 16));
121  str.append(QString("eit_schd(%1) eit_pf(%2) encrypted(%3)\n")
122  .arg(HasEITSchedule(i) ? "t" : "f")
123  .arg(HasEITPresentFollowing(i) ? "t" : "f")
124  .arg(IsEncrypted(i) ? "t" : "f"));
125 
126  if (0 != ServiceDescriptorsLength(i))
127  {
128  str.append(QString(" Service descriptors length: %1\n")
129  .arg(ServiceDescriptorsLength(i)));
130  vector<const unsigned char*> desc =
133  for (size_t j = 0; j < desc.size(); j++)
134  {
135  // Descriptors 0x80 to 0xFE are user defined, see
136  // Final draft ETSI EN 300 468 v1.13.1 (2012-04)
137  // Table 12: "Possible location of descriptors", page 33)
138  if (MPEGDescriptor(desc[j]).DescriptorTag() < 0x80 ||
140  {
141  str.append(QString(" %1\n")
142  .arg(MPEGDescriptor(desc[j]).toString()));
143  }
144  else
145  {
146  QString udd = "Invalid Descriptor";
147  if (MPEGDescriptor(desc[j]).IsValid())
148  {
149  udd = QString("User Defined Descriptor (0x%1) length(%2). Dumping\n")
150  .arg(MPEGDescriptor(desc[j]).DescriptorTag(),2,16,QChar('0'))
151  .arg(MPEGDescriptor(desc[j]).DescriptorLength());
152  udd.append(MPEGDescriptor(desc[j]).hexdump());
153  }
154  str.append(QString(" %1\n").arg(udd));
155  }
156  }
157  }
158  }
159  return str;
160 }
161 
163 {
164  desc_list_t parsed =
167 
168  const unsigned char *desc =
170 
171  if (desc)
172  return new ServiceDescriptor(desc);
173 
174  return nullptr;
175 }
176 
178 {
179  if (VerifyCRC())
180  {
182  SetCRC(CalcCRC());
183  return true;
184  }
185  return false;
186 }
187 
189 {
191 
192  _ptrs.clear();
193  _ptrs.push_back(_tsc_ptr + 2);
194  for (uint i=0; _ptrs[i] + 6 <= _ptrs[0] + TransportStreamDataLength(); i++)
195  _ptrs.push_back(_ptrs[i] + 6 + TransportDescriptorsLength(i));
196 }
197 
199 {
200  QString str =
201  QString("BAT: BouquetID(0x%1) transports(%2)\n")
202  .arg(BouquetID(), 0, 16).arg(TransportStreamCount());
203 
204  str.append(QString("Section (%1) Last Section (%2) IsCurrent (%3)\n")
205  .arg(Section()).arg(LastSection()).arg(IsCurrent()));
206 
207  if (0 != BouquetDescriptorsLength())
208  {
209  str.append(QString("Bouquet descriptors length: %1\n")
210  .arg(BouquetDescriptorsLength()));
211  vector<const unsigned char*> desc =
214  for (size_t i = 0; i < desc.size(); i++)
215  str.append(QString(" %1\n")
216  .arg(MPEGDescriptor(desc[i]).toString()));
217  }
218 
219  for (uint i = 0; i < TransportStreamCount(); i++)
220  {
221  str.append(QString(" Transport #%1 TSID(0x%2) ")
222  .arg(i, 2, 10).arg(TSID(i), 0, 16));
223  str.append(QString("original_network_id(0x%2) desc_len(%3)\n")
224  .arg(OriginalNetworkID(i), 0, 16)
225  .arg(TransportDescriptorsLength(i)));
226 
227  if (0 != TransportDescriptorsLength(i))
228  {
229  str.append(QString(" Transport descriptors length: %1\n")
230  .arg(TransportDescriptorsLength(i)));
231  vector<const unsigned char*> desc =
234  for (size_t j = 0; j < desc.size(); j++)
235  str.append(QString(" %1\n")
236  .arg(MPEGDescriptor(desc[j]).toString()));
237  }
238  }
239  return str;
240 }
241 
243 {
244  _ptrs.clear();
245  _ptrs.push_back(psipdata() + 6);
246  uint i = 0;
247  while ((_ptrs[i] + 12) < (pesdata() + Length()))
248  {
249  _ptrs.push_back(_ptrs[i] + 12 + DescriptorsLength(i));
250  i++;
251  }
252 }
253 
255 {
256  bool is_eit = false;
257 
258  // Standard Now/Next Event Information Tables for this transport
259  is_eit |= TableID::PF_EIT == table_id;
260  // Standard Now/Next Event Information Tables for other transport
261  is_eit |= TableID::PF_EITo == table_id;
262  // Standard Future Event Information Tables for this transport
263  is_eit |= (TableID::SC_EITbeg <= table_id &&
264  TableID::SC_EITend >= table_id);
265  // Standard Future Event Information Tables for other transports
266  is_eit |= (TableID::SC_EITbego <= table_id &&
267  TableID::SC_EITendo >= table_id);
268  // Dish Network Long Term Future Event Information for all transports
269  is_eit |= (TableID::DN_EITbego <= table_id &&
270  TableID::DN_EITendo >= table_id);
271 
272  return is_eit;
273 }
274 
278 QDateTime dvbdate2qt(const unsigned char *buf)
279 {
280  /* FIXME what's wrong with QDate::fromJulianDay? */
281  uint mjd = (buf[0] << 8) | buf[1];
282  if (mjd >= 40587)
283  {
284  // Modified Julian date as number of days since 17th November 1858.
285  // 1st Jan 1970 was date 40587.
286  uint secsSince1970 = (mjd - 40587) * 86400;
287  secsSince1970 += byteBCD2int(buf[2]) * 3600;
288  secsSince1970 += byteBCD2int(buf[3]) * 60;
289  secsSince1970 += byteBCD2int(buf[4]);
290 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
291  return MythDate::fromTime_t(secsSince1970);
292 #else
293  return MythDate::fromSecsSinceEpoch(secsSince1970);
294 #endif
295  }
296 
297  // Original function taken from dvbdate.c in linuxtv-apps code
298  // Use the routine specified in ETSI EN 300 468 V1.4.1,
299  // "Specification for Service Information in Digital Video Broadcasting"
300  // to convert from Modified Julian Date to Year, Month, Day.
301 
302  const float tmpA = (float)(1.0 / 365.25);
303  const float tmpB = (float)(1.0 / 30.6001);
304 
305  float mjdf = mjd;
306  int year = (int) truncf((mjdf - 15078.2F) * tmpA);
307  int month = (int) truncf(
308  (mjdf - 14956.1F - truncf(year * 365.25F)) * tmpB);
309  int day = (int) truncf(
310  (mjdf - 14956.0F - truncf(year * 365.25F) - truncf(month * 30.6001F)));
311  int i = (month == 14 || month == 15) ? 1 : 0;
312 
313  QDate date(1900 + year + i, month - 1 - i * 12, day);
314  QTime time(byteBCD2int(buf[2]), byteBCD2int(buf[3]),
315  byteBCD2int(buf[4]));
316 
317  return QDateTime(date, time, Qt::UTC);
318 }
319 
323 time_t dvbdate2unix(const unsigned char *buf)
324 {
325  // Modified Julian date as number of days since 17th November 1858.
326  // The unix epoch, 1st Jan 1970, was day 40587.
327  uint mjd = (buf[0] << 8) | buf[1];
328  if (mjd < 40587)
329  return 0; // we don't handle pre-unix dates..
330 
331  uint secsSince1970 = (mjd - 40587) * 86400;
332  secsSince1970 += byteBCD2int(buf[2]) * 3600;
333  secsSince1970 += byteBCD2int(buf[3]) * 60;
334  secsSince1970 += byteBCD2int(buf[4]);
335  return secsSince1970;
336 }
337 
341 uint32_t dvbdate2key(const unsigned char *buf)
342 {
343  uint dt = (((uint)buf[0]) << 24) | (((uint)buf[1]) << 16); // 16 bits
344  uint tm = ((byteBCD2int(buf[2]) * 3600) +
345  (byteBCD2int(buf[3]) * 60) +
346  (byteBCD2int(buf[4]))); // 17 bits
347  return (dt | (tm>>1)) ^ ((tm & 1)<<31);
348 }
uint BouquetID() const
Definition: dvbtables.h:195
const unsigned char * pesdata() const
Definition: pespacket.h:163
const unsigned char * NetworkDescriptors(void) const
for(i=0; i<N; i++) x 10.0 { descriptor() }
Definition: dvbtables.h:59
const unsigned char * psipdata(void) const
Definition: mpegtables.h:522
const unsigned char * TransportDescriptors(uint i) const
for(j=0;j<N;j++) x 6.0+p { descriptor() }
Definition: dvbtables.h:231
bool Mutate(void)
mutates a SDTo into a SDTa (vice versa) and recalculates the CRC
Definition: dvbtables.cpp:177
QString toString(void) const override
Definition: dvbtables.cpp:108
uint Length() const
Definition: pespacket.h:98
void Parse(void) const
Definition: dvbtables.cpp:242
void Parse(void) const
Definition: dvbtables.cpp:96
uint TransportDescriptorsLength(uint i) const
Definition: dvbtables.h:227
bool HasEITPresentFollowing(uint i) const
Definition: dvbtables.h:146
bool IsEncrypted(uint i) const
free_CA_mode 1 3.3+p
Definition: dvbtables.h:151
time_t dvbdate2unix(const unsigned char *buf)
Definition: dvbtables.cpp:323
uint BouquetDescriptorsLength(void) const
Definition: dvbtables.h:203
void Parse(void) const
Definition: dvbtables.cpp:188
uint ServiceID(uint i) const
service_id 16 0.0+p
Definition: dvbtables.h:141
const unsigned char * _tsc_ptr
Definition: dvbtables.h:93
uint LastSection(void) const
Definition: mpegtables.h:515
vector< const unsigned char * > _ptrs
Definition: dvbtables.h:169
uint ServiceDescriptorsLength(uint i) const
desc_loop_length 12 3.4+p
Definition: dvbtables.h:153
uint DescriptorTag(void) const
bool Mutate(void)
mutates a NITo into a NITa (vice versa) and recalculates the CRC
Definition: dvbtables.cpp:85
uint TSID(uint i) const
Definition: dvbtables.h:221
unsigned int uint
Definition: compat.h:140
uint TSID() const
transport_stream_id 16 3.0 0x0000
Definition: dvbtables.h:129
uint NetworkID(void) const
network_id 16 3.0 0x0000
Definition: dvbtables.h:50
vector< const unsigned char * > desc_list_t
bool IsCurrent(void) const
Definition: mpegtables.h:509
const unsigned char * ServiceDescriptors(uint i) const
for (j=0;j<N;j++) x 5.0+p { descriptor() }
Definition: dvbtables.h:157
uint CalcCRC(void) const
Definition: pespacket.cpp:145
const unsigned char * TransportDescriptors(uint i) const
for(j=0;j<N;j++) x 6.0+p { descriptor() }
Definition: dvbtables.h:80
vector< const unsigned char * > _ptrs
Definition: dvbtables.h:240
uint Section(void) const
Definition: mpegtables.h:512
static bool IsEIT(uint table_id)
Definition: dvbtables.cpp:254
uint OriginalNetworkID() const
original_network_id 16 8.0
Definition: dvbtables.h:132
MBASE_PUBLIC QDateTime fromSecsSinceEpoch(uint seconds)
This function takes the number of seconds since the start of the epoch and returns a QDateTime with t...
Definition: mythdate.cpp:88
vector< const unsigned char * > _ptrs
Definition: dvbtables.h:367
const unsigned char * BouquetDescriptors(void) const
Definition: dvbtables.h:209
void Parse(void) const
Definition: dvbtables.cpp:9
uint DescriptorsLength(uint i) const
Definition: dvbtables.h:353
QString NetworkName(void) const
Definition: dvbtables.cpp:61
uint ServiceCount() const
Number of services.
Definition: dvbtables.h:136
uint TransportStreamDataLength(void) const
trans_stream_loop_len 12 0.4+ndl
Definition: dvbtables.h:64
vector< const unsigned char * > _ptrs
Definition: dvbtables.h:94
uint TransportStreamDataLength(void) const
Definition: dvbtables.h:214
uint OriginalNetworkID(uint i) const
Definition: dvbtables.h:223
bool HasEITSchedule(uint i) const
Definition: dvbtables.h:144
QString _cached_network_name
Definition: dvbtables.h:92
static const unsigned char * Find(const desc_list_t &parsed, uint desc_tag)
uint TransportStreamCount(void) const
Definition: dvbtables.h:216
uint TableID(void) const
Definition: mpegtables.h:479
void SetCRC(uint crc)
Definition: pespacket.h:199
ServiceDescriptor * GetServiceDescriptor(uint i) const
Definition: dvbtables.cpp:162
uint32_t dvbdate2key(const unsigned char *buf)
Definition: dvbtables.cpp:341
uint OriginalNetworkID(uint i) const
original_network_id 16 2.0+p
Definition: dvbtables.h:72
uint TransportStreamCount(void) const
Definition: dvbtables.h:66
QString toString(void) const override
Definition: dvbtables.cpp:19
const unsigned char * _tsc_ptr
Definition: dvbtables.h:239
uint TSID(uint i) const
transport_stream_id 16 0.0+p
Definition: dvbtables.h:70
uint NetworkDescriptorsLength(void) const
network_desc_length 12 8.4 0
Definition: dvbtables.h:54
QString toString(void) const override
Definition: dvbtables.cpp:198
uint TransportDescriptorsLength(uint i) const
trans_desc_length 12 4.4+p
Definition: dvbtables.h:76
bool VerifyCRC(void) const
Definition: pespacket.cpp:153
static desc_list_t Parse(const unsigned char *data, uint len)
QDateTime dvbdate2qt(const unsigned char *buf)
Definition: dvbtables.cpp:278
#define byteBCD2int(i)
void SetTableID(uint id)
Definition: mpegtables.h:528