00001
00002
00003
00004 #include <cmath>
00005
00006 #include "dvbtables.h"
00007 #include "dvbdescriptors.h"
00008 #include "qstring.h"
00009
00010 void NetworkInformationTable::Parse(void) const
00011 {
00012 _tsc_ptr = pesdata() + 10 + NetworkDescriptorsLength();
00013
00014 _ptrs.clear();
00015 _ptrs.push_back(_tsc_ptr + 2);
00016 for (uint i=0; _ptrs[i] + 6 <= _ptrs[0] + TransportStreamDataLength(); i++)
00017 _ptrs.push_back(_ptrs[i] + 6 + TransportDescriptorsLength(i));
00018 }
00019
00020 QString NetworkInformationTable::toString(void) const
00021 {
00022 QString str = QString("NIT: NetID(%1) tranports(%2)\n")
00023 .arg(NetworkID()).arg(TransportStreamCount());
00024
00025 if (0 != NetworkDescriptorsLength())
00026 {
00027 str.append(QString("Network descriptors length: %1\n")
00028 .arg(NetworkDescriptorsLength()));
00029 vector<const unsigned char*> desc =
00030 MPEGDescriptor::Parse(NetworkDescriptors(),
00031 NetworkDescriptorsLength());
00032 for (uint i = 0; i < desc.size(); i++)
00033 str.append(QString(" %1\n")
00034 .arg(MPEGDescriptor(desc[i]).toString()));
00035 }
00036
00037 for (uint i = 0; i < TransportStreamCount(); i++)
00038 {
00039 str.append(QString(" Transport #%1 TSID(0x%1) ")
00040 .arg(i, 2, 10).arg(TSID(i), 0, 16));
00041 str.append(QString("original_network_id(0x%2) desc_len(%3)\n")
00042 .arg(OriginalNetworkID(i), 0, 16)
00043 .arg(TransportDescriptorsLength(i)));
00044
00045 if (0 != TransportDescriptorsLength(i))
00046 {
00047 str.append(QString(" Transport descriptors length: %1\n")
00048 .arg(TransportDescriptorsLength(i)));
00049 vector<const unsigned char*> desc =
00050 MPEGDescriptor::Parse(TransportDescriptors(i),
00051 TransportDescriptorsLength(i));
00052 for (uint i = 0; i < desc.size(); i++)
00053 str.append(QString(" %1\n")
00054 .arg(MPEGDescriptor(desc[i]).toString()));
00055 }
00056 }
00057 return str;
00058 }
00059
00060 QString NetworkInformationTable::NetworkName() const
00061 {
00062 if (_cached_network_name == QString::null)
00063 {
00064 desc_list_t parsed =
00065 MPEGDescriptor::Parse(NetworkDescriptors(),
00066 NetworkDescriptorsLength());
00067
00068 const unsigned char *desc =
00069 MPEGDescriptor::Find(parsed, DescriptorID::network_name);
00070
00071 if (desc)
00072 _cached_network_name = NetworkNameDescriptor(desc).Name();
00073 else
00074 _cached_network_name = QString("Net ID 0x%1")
00075 .arg(NetworkID(), 0, 16);
00076 }
00077 return _cached_network_name;
00078 }
00079
00080
00081 void ServiceDescriptionTable::Parse(void) const
00082 {
00083 _ptrs.clear();
00084 _ptrs.push_back(pesdata() + 11);
00085 uint i = 0;
00086 while ((_ptrs[i] + 5) < (pesdata() + Length()))
00087 {
00088 _ptrs.push_back(_ptrs[i] + 5 + ServiceDescriptorsLength(i));
00089 i++;
00090 }
00091 }
00092
00093 QString ServiceDescriptionTable::toString(void) const
00094 {
00095 QString str =
00096 QString("SDT: TSID(0x%1) original_network_id(0x%2) services(%3)\n")
00097 .arg(OriginalNetworkID(), 0, 16).arg(TSID(), 0, 16)
00098 .arg(ServiceCount());
00099
00100 for (uint i = 0; i < ServiceCount(); i++)
00101 {
00102 str.append(QString(" Service #%1 SID(0x%2) ")
00103 .arg(i, 2, 10).arg(ServiceID(i), 0, 16));
00104 str.append(QString("eit_schd(%1) eit_pf(%2) encrypted(%3)\n")
00105 .arg(HasEITSchedule(i) ? "t" : "f")
00106 .arg(HasEITPresentFollowing(i) ? "t" : "f")
00107 .arg(IsEncrypted(i) ? "t" : "f"));
00108
00109 if (0 != ServiceDescriptorsLength(i))
00110 {
00111 str.append(QString(" Service descriptors length: %1\n")
00112 .arg(ServiceDescriptorsLength(i)));
00113 vector<const unsigned char*> desc =
00114 MPEGDescriptor::Parse(ServiceDescriptors(i),
00115 ServiceDescriptorsLength(i));
00116 for (uint i = 0; i < desc.size(); i++)
00117 str.append(QString(" %1\n")
00118 .arg(MPEGDescriptor(desc[i]).toString()));
00119 }
00120 }
00121 return str;
00122 }
00123
00124 ServiceDescriptor *ServiceDescriptionTable::GetServiceDescriptor(uint i) const
00125 {
00126 desc_list_t parsed =
00127 MPEGDescriptor::Parse(ServiceDescriptors(i),
00128 ServiceDescriptorsLength(i));
00129
00130 const unsigned char *desc =
00131 MPEGDescriptor::Find(parsed, DescriptorID::service);
00132
00133 if (desc)
00134 return new ServiceDescriptor(desc);
00135
00136 return NULL;
00137 }
00138
00139 bool ServiceDescriptionTable::Mutate(void)
00140 {
00141 if (VerifyCRC())
00142 {
00143 SetTableID((TableID() == TableID::SDTo) ? TableID::SDT : TableID::SDTo);
00144 SetCRC(CalcCRC());
00145 return true;
00146 }
00147 else
00148 return false;
00149 }
00150
00151 void DVBEventInformationTable::Parse(void) const
00152 {
00153 _ptrs.clear();
00154 _ptrs.push_back(psipdata() + 6);
00155 uint i = 0;
00156 while ((_ptrs[i] + 12) < (pesdata() + Length()))
00157 {
00158 _ptrs.push_back(_ptrs[i] + 12 + DescriptorsLength(i));
00159 i++;
00160 }
00161 }
00162
00163 bool DVBEventInformationTable::IsEIT(uint table_id)
00164 {
00165 bool is_eit = false;
00166
00167
00168 is_eit |= TableID::PF_EIT == table_id;
00169
00170 is_eit |= TableID::PF_EITo == table_id;
00171
00172 is_eit |= (TableID::SC_EITbeg <= table_id &&
00173 TableID::SC_EITend >= table_id);
00174
00175 is_eit |= (TableID::SC_EITbego <= table_id &&
00176 TableID::SC_EITendo >= table_id);
00177
00178 is_eit |= (TableID::DN_EITbego <= table_id &&
00179 TableID::DN_EITendo >= table_id);
00180
00181 return is_eit;
00182 }
00183
00187 QDateTime dvbdate2qt(const unsigned char *buf)
00188 {
00189 uint mjd = (buf[0] << 8) | buf[1];
00190 if (mjd >= 40587)
00191 {
00192 QDateTime result;
00193
00194
00195 uint secsSince1970 = (mjd - 40587) * 86400;
00196 secsSince1970 += byteBCD2int(buf[2]) * 3600;
00197 secsSince1970 += byteBCD2int(buf[3]) * 60;
00198 secsSince1970 += byteBCD2int(buf[4]);
00199 result.setTime_t(secsSince1970, Qt::UTC);
00200 return result;
00201 }
00202
00203
00204
00205
00206
00207
00208 const float tmpA = 1.0 / 365.25;
00209 const float tmpB = 1.0 / 30.6001;
00210
00211 float mjdf = mjd;
00212 int year = (int) truncf((mjdf - 15078.2f) * tmpA);
00213 int month = (int) truncf(
00214 (mjdf - 14956.1f - truncf(year * 365.25f)) * tmpB);
00215 int day = (int) truncf(
00216 (mjdf - 14956.0f - truncf(year * 365.25f) - truncf(month * 30.6001f)));
00217 int i = (month == 14 || month == 15) ? 1 : 0;
00218
00219 QDate date(1900 + year + i, month - 1 - i * 12, day);
00220 QTime time(byteBCD2int(buf[2]), byteBCD2int(buf[3]),
00221 byteBCD2int(buf[4]));
00222
00223 return QDateTime(date, time);
00224 }
00225
00229 time_t dvbdate2unix(const unsigned char *buf)
00230 {
00231
00232
00233 uint mjd = (buf[0] << 8) | buf[1];
00234 if (mjd < 40587)
00235 return 0;
00236
00237 uint secsSince1970 = (mjd - 40587) * 86400;
00238 secsSince1970 += byteBCD2int(buf[2]) * 3600;
00239 secsSince1970 += byteBCD2int(buf[3]) * 60;
00240 secsSince1970 += byteBCD2int(buf[4]);
00241 return secsSince1970;
00242 }
00243
00247 uint32_t dvbdate2key(const unsigned char *buf)
00248 {
00249 uint dt = (((uint)buf[0]) << 24) | (((uint)buf[1]) << 16);
00250 uint tm = ((byteBCD2int(buf[2]) * 3600) +
00251 (byteBCD2int(buf[3]) * 60) +
00252 (byteBCD2int(buf[4])));
00253 return (dt | (tm>>1)) ^ ((tm & 1)<<31);
00254 }