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 
9 static uint GetPrivateDataSpecifier(const unsigned char *desc, uint priv_dsid)
10 {
12  {
14  if (pd.IsValid())
15  priv_dsid = pd.PrivateDataSpecifier();
16  }
17  return priv_dsid;
18 }
19 
21 {
23 
24  m_ptrs.clear();
25  m_ptrs.push_back(m_tscPtr + 2);
26  for (uint i=0; m_ptrs[i] + 6 <= m_ptrs[0] + TransportStreamDataLength(); i++)
27  m_ptrs.push_back(m_ptrs[i] + 6 + TransportDescriptorsLength(i));
28 }
29 
31 {
32  QString str = QString("NIT: NetID(%1) transports(%2)\n")
33  .arg(NetworkID()).arg(TransportStreamCount());
34  str.append(QString("Section (%1) Last Section (%2) IsCurrent (%3)\n")
35  .arg(Section()).arg(LastSection()).arg(static_cast<int>(IsCurrent())));
36 
37  if (0 != NetworkDescriptorsLength())
38  {
39  str.append(QString("Network descriptors length: %1\n")
40  .arg(NetworkDescriptorsLength()));
41  std::vector<const unsigned char*> desc =
44  uint priv_dsid = 0;
45  for (auto & i : desc)
46  {
47  priv_dsid = GetPrivateDataSpecifier(i, priv_dsid);
48  str.append(QString(" %1\n")
49  .arg(MPEGDescriptor(i).toStringPD(priv_dsid)));
50  }
51  }
52 
53  for (uint i = 0; i < TransportStreamCount(); i++)
54  {
55  str.append(QString(" Transport #%1 TSID(0x%2) ")
56  .arg(i, 2, 10).arg(TSID(i), 0, 16));
57  str.append(QString("original_network_id(0x%2) desc_len(%3)\n")
58  .arg(OriginalNetworkID(i), 0, 16)
60 
61  if (0 != TransportDescriptorsLength(i))
62  {
63  str.append(QString(" Transport descriptors length: %1\n")
65  std::vector<const unsigned char*> desc =
68  uint priv_dsid = 0;
69  for (auto & j : desc)
70  {
71  priv_dsid = GetPrivateDataSpecifier(j, priv_dsid);
72  str.append(QString(" %1\n")
73  .arg(MPEGDescriptor(j).toStringPD(priv_dsid)));
74  }
75  }
76  }
77  return str;
78 }
79 
81 {
82  if (m_cachedNetworkName.isEmpty())
83  {
84  desc_list_t parsed =
87 
88  const unsigned char *desc =
90 
91  if (desc)
92  {
93  auto nndesc = NetworkNameDescriptor(desc);
94  if (nndesc.IsValid())
95  m_cachedNetworkName = nndesc.Name();
96  }
97  if (m_cachedNetworkName.isEmpty())
98  m_cachedNetworkName = QString("Net ID 0x%1")
99  .arg(NetworkID(), 0, 16);
100  }
101  return m_cachedNetworkName;
102 }
103 
105 {
106  if (VerifyCRC())
107  {
109  SetCRC(CalcCRC());
110  return true;
111  }
112  return false;
113 }
114 
116 {
117  m_ptrs.clear();
118  m_ptrs.push_back(pesdata() + 11);
119  uint i = 0;
120  while ((m_ptrs[i] + 5) < (pesdata() + Length()))
121  {
122  m_ptrs.push_back(m_ptrs[i] + 5 + ServiceDescriptorsLength(i));
123  i++;
124  }
125 }
126 
128 {
129  QString str =
130  QString("SDT: TSID(0x%1) original_network_id(0x%2) services(%3)\n")
131  .arg(TSID(), 0, 16).arg(OriginalNetworkID(), 0, 16)
132  .arg(ServiceCount());
133  str.append(QString("Section (%1) Last Section (%2) IsCurrent (%3)\n")
134  .arg(Section()).arg(LastSection()).arg(static_cast<int>(IsCurrent())));
135 
136  for (uint i = 0; i < ServiceCount(); i++)
137  {
138  str.append(QString(" Service #%1 SID(0x%2) ")
139  .arg(i, 2, 10).arg(ServiceID(i), 0, 16));
140  str.append(QString("eit_schd(%1) eit_pf(%2) encrypted(%3)\n")
141  .arg(HasEITSchedule(i) ? "t" : "f",
142  HasEITPresentFollowing(i) ? "t" : "f",
143  IsEncrypted(i) ? "t" : "f"));
144 
145  if (0 != ServiceDescriptorsLength(i))
146  {
147  str.append(QString(" Service descriptors length: %1\n")
148  .arg(ServiceDescriptorsLength(i)));
149  std::vector<const unsigned char*> desc =
152  uint priv_dsid = 0;
153  for (auto & j : desc)
154  {
155  priv_dsid = GetPrivateDataSpecifier(j, priv_dsid);
156  str.append(QString(" %1\n")
157  .arg(MPEGDescriptor(j).toStringPD(priv_dsid)));
158  }
159  }
160  }
161  return str;
162 }
163 
165 {
166  desc_list_t parsed =
169 
170  const unsigned char *desc =
172 
173  if (desc)
174  return new ServiceDescriptor(desc);
175 
176  return nullptr;
177 }
178 
180 {
181  desc_list_t parsed =
184 
185  const unsigned char *desc =
187 
188  if (desc)
189  return new ServiceRelocatedDescriptor(desc);
190 
191  return nullptr;
192 }
193 
195 {
196  if (VerifyCRC())
197  {
199  SetCRC(CalcCRC());
200  return true;
201  }
202  return false;
203 }
204 
206 {
208 
209  m_ptrs.clear();
210  m_ptrs.push_back(m_tscPtr + 2);
211  for (uint i=0; m_ptrs[i] + 6 <= m_ptrs[0] + TransportStreamDataLength(); i++)
212  m_ptrs.push_back(m_ptrs[i] + 6 + TransportDescriptorsLength(i));
213 }
214 
216 {
217  QString str =
218  QString("BAT: BouquetID(0x%1) transports(%2)\n")
219  .arg(BouquetID(), 0, 16).arg(TransportStreamCount());
220 
221  str.append(QString("Section (%1) Last Section (%2) IsCurrent (%3)\n")
222  .arg(Section()).arg(LastSection()).arg(static_cast<int>(IsCurrent())));
223 
224  if (0 != BouquetDescriptorsLength())
225  {
226  str.append(QString("Bouquet descriptors length: %1\n")
227  .arg(BouquetDescriptorsLength()));
228  std::vector<const unsigned char*> desc =
231  uint priv_dsid = 0;
232  for (auto & i : desc)
233  {
234  priv_dsid = GetPrivateDataSpecifier(i, priv_dsid);
235  str.append(QString(" %1\n")
236  .arg(MPEGDescriptor(i).toStringPD(priv_dsid)));
237  }
238  }
239 
240  for (uint i = 0; i < TransportStreamCount(); i++)
241  {
242  str.append(QString(" Transport #%1 TSID(0x%2) ")
243  .arg(i, 2, 10).arg(TSID(i), 0, 16));
244  str.append(QString("original_network_id(0x%2) desc_len(%3)\n")
245  .arg(OriginalNetworkID(i), 0, 16)
246  .arg(TransportDescriptorsLength(i)));
247 
248  if (0 != TransportDescriptorsLength(i))
249  {
250  str.append(QString(" Transport descriptors length: %1\n")
251  .arg(TransportDescriptorsLength(i)));
252  std::vector<const unsigned char*> desc =
255  uint priv_dsid = 0;
256  for (auto & j : desc)
257  {
258  priv_dsid = GetPrivateDataSpecifier(j, priv_dsid);
259  str.append(QString(" %1\n")
260  .arg(MPEGDescriptor(j).toStringPD(priv_dsid)));
261  }
262  }
263  }
264  return str;
265 }
266 
267 
269 {
270  m_ptrs.clear();
271  m_ptrs.push_back(psipdata() + 6);
272  uint i = 0;
273  while ((m_ptrs[i] + 12) < (pesdata() + Length()))
274  {
275  m_ptrs.push_back(m_ptrs[i] + 12 + DescriptorsLength(i));
276  i++;
277  }
278 }
279 
281 {
282  bool is_eit = false;
283 
284  // Standard Now/Next Event Information Tables for this transport
285  is_eit |= TableID::PF_EIT == table_id;
286  // Standard Now/Next Event Information Tables for other transport
287  is_eit |= TableID::PF_EITo == table_id;
288  // Standard Future Event Information Tables for this transport
289  is_eit |= (TableID::SC_EITbeg <= table_id &&
290  TableID::SC_EITend >= table_id);
291  // Standard Future Event Information Tables for other transports
292  is_eit |= (TableID::SC_EITbego <= table_id &&
293  TableID::SC_EITendo >= table_id);
294  // Dish Network Long Term Future Event Information for all transports
295  is_eit |= (TableID::DN_EITbego <= table_id &&
296  TableID::DN_EITendo >= table_id);
297 
298  return is_eit;
299 }
300 
304 QDateTime dvbdate2qt(const unsigned char *buf)
305 {
306  /* FIXME what's wrong with QDate::fromJulianDay? */
307  uint mjd = (buf[0] << 8) | buf[1];
308  if (mjd >= 40587)
309  {
310  // Modified Julian date as number of days since 17th November 1858.
311  // 1st Jan 1970 was date 40587.
312  uint secsSince1970 = (mjd - 40587) * 86400;
313  secsSince1970 += byteBCD2int(buf[2]) * 3600;
314  secsSince1970 += byteBCD2int(buf[3]) * 60;
315  secsSince1970 += byteBCD2int(buf[4]);
316  return MythDate::fromSecsSinceEpoch(secsSince1970);
317  }
318 
319  // Original function taken from dvbdate.c in linuxtv-apps code
320  // Use the routine specified in ETSI EN 300 468 V1.4.1,
321  // "Specification for Service Information in Digital Video Broadcasting"
322  // to convert from Modified Julian Date to Year, Month, Day.
323 
324  const auto tmpA = (float)(1.0 / 365.25);
325  const auto tmpB = (float)(1.0 / 30.6001);
326 
327  float mjdf = mjd;
328  int year = (int) truncf((mjdf - 15078.2F) * tmpA);
329  int month = (int) truncf(
330  (mjdf - 14956.1F - truncf(year * 365.25F)) * tmpB);
331  int day = (int) truncf(
332  (mjdf - 14956.0F - truncf(year * 365.25F) - truncf(month * 30.6001F)));
333  int i = (month == 14 || month == 15) ? 1 : 0;
334 
335  QDate date(1900 + year + i, month - 1 - (i * 12), day);
336  QTime time(byteBCD2int(buf[2]), byteBCD2int(buf[3]),
337  byteBCD2int(buf[4]));
338 
339  return {date, time, Qt::UTC};
340 }
341 
345 time_t dvbdate2unix(const unsigned char *buf)
346 {
347  // Modified Julian date as number of days since 17th November 1858.
348  // The unix epoch, 1st Jan 1970, was day 40587.
349  uint mjd = (buf[0] << 8) | buf[1];
350  if (mjd < 40587)
351  return 0; // we don't handle pre-unix dates..
352 
353  uint secsSince1970 = (mjd - 40587) * 86400;
354  secsSince1970 += byteBCD2int(buf[2]) * 3600;
355  secsSince1970 += byteBCD2int(buf[3]) * 60;
356  secsSince1970 += byteBCD2int(buf[4]);
357  return secsSince1970;
358 }
359 
363 uint32_t dvbdate2key(const unsigned char *buf)
364 {
365  uint dt = (((uint)buf[0]) << 24) | (((uint)buf[1]) << 16); // 16 bits
366  uint tm = ((byteBCD2int(buf[2]) * 3600) +
367  (byteBCD2int(buf[3]) * 60) +
368  (byteBCD2int(buf[4]))); // 17 bits
369  return (dt | (tm>>1)) ^ ((tm & 1)<<31);
370 }
ServiceRelocatedDescriptor
Definition: dvbdescriptors.h:1457
NetworkInformationTable::TransportDescriptorsLength
uint TransportDescriptorsLength(uint i) const
trans_desc_length 12 4.4+p
Definition: dvbtables.h:83
PSIPTable::SetTableID
void SetTableID(uint id)
Definition: mpegtables.h:566
NetworkInformationTable::TSID
uint TSID(uint i) const
transport_stream_id 16 0.0+p
Definition: dvbtables.h:77
NetworkInformationTable::NetworkDescriptorsLength
uint NetworkDescriptorsLength(void) const
network_desc_length 12 8.4 0
Definition: dvbtables.h:61
DVBEventInformationTable::m_ptrs
std::vector< const unsigned char * > m_ptrs
Definition: dvbtables.h:375
NetworkInformationTable::m_tscPtr
const unsigned char * m_tscPtr
Definition: dvbtables.h:100
TableID::SC_EITbeg
@ SC_EITbeg
Definition: mpegtables.h:280
NetworkInformationTable::NetworkID
uint NetworkID(void) const
network_id 16 3.0 0x0000
Definition: dvbtables.h:57
DescriptorID::service_relocated
@ service_relocated
Definition: mpegdescriptors.h:151
PSIPTable::IsCurrent
bool IsCurrent(void) const
Definition: mpegtables.h:547
DescriptorID::network_name
@ network_name
Definition: mpegdescriptors.h:72
BouquetAssociationTable::BouquetID
uint BouquetID() const
Definition: dvbtables.h:203
NetworkInformationTable::TransportDescriptors
const unsigned char * TransportDescriptors(uint i) const
for(j=0;j<N;j++) x 6.0+p { descriptor() }
Definition: dvbtables.h:87
BouquetAssociationTable::m_tscPtr
const unsigned char * m_tscPtr
Definition: dvbtables.h:247
BouquetAssociationTable::TransportDescriptors
const unsigned char * TransportDescriptors(uint i) const
for(j=0;j<N;j++) x 6.0+p { descriptor() }
Definition: dvbtables.h:239
PESPacket::pesdata
const unsigned char * pesdata() const
Definition: pespacket.h:166
BouquetAssociationTable::TransportDescriptorsLength
uint TransportDescriptorsLength(uint i) const
Definition: dvbtables.h:235
PESPacket::SetCRC
void SetCRC(uint crc)
Definition: pespacket.h:202
BouquetAssociationTable::BouquetDescriptorsLength
uint BouquetDescriptorsLength(void) const
Definition: dvbtables.h:211
ServiceDescriptionTable::ServiceID
uint ServiceID(uint i) const
service_id 16 0.0+p
Definition: dvbtables.h:148
BouquetAssociationTable::Parse
void Parse(void) const
Definition: dvbtables.cpp:205
PESPacket::CalcCRC
uint CalcCRC(void) const
Definition: pespacket.cpp:161
NetworkInformationTable::NetworkName
QString NetworkName(void) const
Definition: dvbtables.cpp:80
NetworkInformationTable::m_ptrs
std::vector< const unsigned char * > m_ptrs
Definition: dvbtables.h:101
DVBEventInformationTable::Parse
void Parse(void) const
Definition: dvbtables.cpp:268
TableID::NIT
@ NIT
Definition: mpegtables.h:270
NetworkInformationTable::TransportStreamDataLength
uint TransportStreamDataLength(void) const
trans_stream_loop_len 12 0.4+ndl
Definition: dvbtables.h:71
BouquetAssociationTable::TSID
uint TSID(uint i) const
Definition: dvbtables.h:229
PSIPTable::Section
uint Section(void) const
Definition: mpegtables.h:550
NetworkInformationTable::Parse
void Parse(void) const
Definition: dvbtables.cpp:20
BouquetAssociationTable::m_ptrs
std::vector< const unsigned char * > m_ptrs
Definition: dvbtables.h:248
TableID::DN_EITendo
@ DN_EITendo
Definition: mpegtables.h:315
MPEGDescriptor::FindExtension
static const unsigned char * FindExtension(const desc_list_t &parsed, uint desc_tag)
Definition: mpegdescriptors.cpp:86
BouquetAssociationTable::BouquetDescriptors
const unsigned char * BouquetDescriptors(void) const
Definition: dvbtables.h:217
DVBEventInformationTable::IsEIT
static bool IsEIT(uint table_id)
Definition: dvbtables.cpp:280
MPEGDescriptor::IsValid
bool IsValid(void) const
Definition: mpegdescriptors.h:342
ServiceDescriptionTable::ServiceCount
uint ServiceCount() const
Number of services.
Definition: dvbtables.h:143
MythDate::fromSecsSinceEpoch
MBASE_PUBLIC QDateTime fromSecsSinceEpoch(int64_t seconds)
This function takes the number of seconds since the start of the epoch and returns a QDateTime with t...
Definition: mythdate.cpp:72
ServiceDescriptionTable::GetServiceDescriptor
ServiceDescriptor * GetServiceDescriptor(uint i) const
Definition: dvbtables.cpp:164
dvbdate2key
uint32_t dvbdate2key(const unsigned char *buf)
Definition: dvbtables.cpp:363
MPEGDescriptor::Parse
static desc_list_t Parse(const unsigned char *data, uint len)
Definition: mpegdescriptors.cpp:17
TableID::NITo
@ NITo
Definition: mpegtables.h:276
ServiceDescriptionTable::toString
QString toString(void) const override
Definition: dvbtables.cpp:127
DVBEventInformationTable::DescriptorsLength
uint DescriptorsLength(uint i) const
Definition: dvbtables.h:361
ServiceDescriptionTable::OriginalNetworkID
uint OriginalNetworkID() const
original_network_id 16 8.0
Definition: dvbtables.h:139
MPEGDescriptor
Definition: mpegdescriptors.h:302
TableID::DN_EITbego
@ DN_EITbego
Definition: mpegtables.h:314
BouquetAssociationTable::TransportStreamDataLength
uint TransportStreamDataLength(void) const
Definition: dvbtables.h:222
NetworkInformationTable::Mutate
bool Mutate(void)
mutates a NITo into a NITa (vice versa) and recalculates the CRC
Definition: dvbtables.cpp:104
desc_list_t
std::vector< const unsigned char * > desc_list_t
Definition: mpegdescriptors.h:18
BouquetAssociationTable::OriginalNetworkID
uint OriginalNetworkID(uint i) const
Definition: dvbtables.h:231
byteBCD2int
static constexpr uint8_t byteBCD2int(uint8_t i)
Definition: dvbdescriptors.h:56
TableID::SC_EITend
@ SC_EITend
Definition: mpegtables.h:281
GetPrivateDataSpecifier
static uint GetPrivateDataSpecifier(const unsigned char *desc, uint priv_dsid)
Definition: dvbtables.cpp:9
dvbtables.h
PrivateDataSpecifierDescriptor::PrivateDataSpecifier
uint32_t PrivateDataSpecifier(void) const
Definition: dvbdescriptors.h:1936
PESPacket::Length
uint Length() const
Definition: pespacket.h:101
PESPacket::VerifyCRC
bool VerifyCRC(void) const
Definition: pespacket.cpp:169
ServiceDescriptionTable::Mutate
bool Mutate(void)
mutates a SDTo into a SDTa (vice versa) and recalculates the CRC
Definition: dvbtables.cpp:194
NetworkInformationTable::OriginalNetworkID
uint OriginalNetworkID(uint i) const
original_network_id 16 2.0+p
Definition: dvbtables.h:79
uint
unsigned int uint
Definition: compat.h:81
TableID::SDT
@ SDT
Definition: mpegtables.h:271
PSIPTable::LastSection
uint LastSection(void) const
Definition: mpegtables.h:553
DescriptorID::service
@ service
Definition: mpegdescriptors.h:80
NetworkInformationTable::toString
QString toString(void) const override
Definition: dvbtables.cpp:30
PrivateDataSpecifierDescriptor
Definition: dvbdescriptors.h:1924
ServiceDescriptionTable::IsEncrypted
bool IsEncrypted(uint i) const
free_CA_mode 1 3.3+p
Definition: dvbtables.h:158
ServiceDescriptionTable::m_ptrs
std::vector< const unsigned char * > m_ptrs
Definition: dvbtables.h:177
TableID::SC_EITendo
@ SC_EITendo
Definition: mpegtables.h:283
BouquetAssociationTable::TransportStreamCount
uint TransportStreamCount(void) const
Definition: dvbtables.h:224
ServiceDescriptionTable::HasEITPresentFollowing
bool HasEITPresentFollowing(uint i) const
Definition: dvbtables.h:153
BouquetAssociationTable::toString
QString toString(void) const override
Definition: dvbtables.cpp:215
dvbdate2qt
QDateTime dvbdate2qt(const unsigned char *buf)
Definition: dvbtables.cpp:304
PSIPTable::TableID
uint TableID(void) const
Definition: mpegtables.h:515
ServiceDescriptor
Definition: dvbdescriptors.h:2067
NetworkInformationTable::NetworkDescriptors
const unsigned char * NetworkDescriptors(void) const
for(i=0; i<N; i++) x 10.0 { descriptor() }
Definition: dvbtables.h:66
ServiceDescriptionTable::GetServiceRelocatedDescriptor
ServiceRelocatedDescriptor * GetServiceRelocatedDescriptor(uint i) const
Definition: dvbtables.cpp:179
dvbdate2unix
time_t dvbdate2unix(const unsigned char *buf)
Definition: dvbtables.cpp:345
ServiceDescriptionTable::Parse
void Parse(void) const
Definition: dvbtables.cpp:115
ServiceDescriptionTable::ServiceDescriptorsLength
uint ServiceDescriptorsLength(uint i) const
desc_loop_length 12 3.4+p
Definition: dvbtables.h:160
TableID::PF_EIT
@ PF_EIT
Definition: mpegtables.h:272
TableID::PF_EITo
@ PF_EITo
Definition: mpegtables.h:279
NetworkInformationTable::m_cachedNetworkName
QString m_cachedNetworkName
Definition: dvbtables.h:99
ServiceDescriptionTable::HasEITSchedule
bool HasEITSchedule(uint i) const
Definition: dvbtables.h:151
DescriptorID::private_data_specifier
@ private_data_specifier
Definition: mpegdescriptors.h:104
NetworkInformationTable::TransportStreamCount
uint TransportStreamCount(void) const
Definition: dvbtables.h:73
MPEGDescriptor::Find
static const unsigned char * Find(const desc_list_t &parsed, uint desc_tag)
Definition: mpegdescriptors.cpp:78
TableID::SDTo
@ SDTo
Definition: mpegtables.h:277
ServiceDescriptionTable::ServiceDescriptors
const unsigned char * ServiceDescriptors(uint i) const
for (j=0;j<N;j++) x 5.0+p { descriptor() }
Definition: dvbtables.h:164
TableID::SC_EITbego
@ SC_EITbego
Definition: mpegtables.h:282
ServiceDescriptionTable::TSID
uint TSID() const
transport_stream_id 16 3.0 0x0000
Definition: dvbtables.h:136
PSIPTable::psipdata
const unsigned char * psipdata(void) const
Definition: mpegtables.h:560
dvbdescriptors.h
NetworkNameDescriptor
Definition: dvbdescriptors.h:80