vdr  2.0.4
nit.c
Go to the documentation of this file.
1 /*
2  * nit.c: NIT section filter
3  *
4  * See the main source file 'vdr.c' for copyright information and
5  * how to reach the author.
6  *
7  * $Id: nit.c 2.10 2013/03/07 09:42:29 kls Exp $
8  */
9 
10 #include "nit.h"
11 #include <linux/dvb/frontend.h>
12 #include "channels.h"
13 #include "dvbdevice.h"
14 #include "eitscan.h"
15 #include "libsi/section.h"
16 #include "libsi/descriptor.h"
17 #include "tools.h"
18 
19 #define DVB_SYSTEM_1 0 // see also dvbdevice.c
20 #define DVB_SYSTEM_2 1
21 
23 {
24  numNits = 0;
25  networkId = 0;
26  Set(0x10, 0x40); // NIT
27 }
28 
29 void cNitFilter::SetStatus(bool On)
30 {
32  numNits = 0;
33  networkId = 0;
35 }
36 
37 void cNitFilter::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
38 {
39  SI::NIT nit(Data, false);
40  if (!nit.CheckCRCAndParse())
41  return;
42  // Some broadcasters send more than one NIT, with no apparent way of telling which
43  // one is the right one to use. This is an attempt to find the NIT that contains
44  // the transponder it was transmitted on and use only that one:
45  int ThisNIT = -1;
46  if (!networkId) {
47  for (int i = 0; i < numNits; i++) {
48  if (nits[i].networkId == nit.getNetworkId()) {
49  if (nit.getSectionNumber() == 0) {
50  // all NITs have passed by
51  for (int j = 0; j < numNits; j++) {
52  if (nits[j].hasTransponder) {
54  //printf("taking NIT with network ID %d\n", networkId);
55  //XXX what if more than one NIT contains this transponder???
56  break;
57  }
58  }
59  if (!networkId) {
60  //printf("none of the NITs contains transponder %d\n", Transponder());
61  return;
62  }
63  }
64  else {
65  ThisNIT = i;
66  break;
67  }
68  }
69  }
70  if (!networkId && ThisNIT < 0 && numNits < MAXNITS) {
71  if (nit.getSectionNumber() == 0) {
72  *nits[numNits].name = 0;
73  SI::Descriptor *d;
74  for (SI::Loop::Iterator it; (d = nit.commonDescriptors.getNext(it)); ) {
75  switch (d->getDescriptorTag()) {
78  nnd->name.getText(nits[numNits].name, MAXNETWORKNAME);
79  }
80  break;
81  default: ;
82  }
83  delete d;
84  }
86  nits[numNits].hasTransponder = false;
87  //printf("NIT[%d] %5d '%s'\n", numNits, nits[numNits].networkId, nits[numNits].name);
88  ThisNIT = numNits;
89  numNits++;
90  }
91  }
92  }
93  else if (networkId != nit.getNetworkId())
94  return; // ignore all other NITs
96  return;
97  if (!Channels.Lock(true, 10))
98  return;
100  for (SI::Loop::Iterator it; nit.transportStreamLoop.getNext(ts, it); ) {
101  SI::Descriptor *d;
102 
103  SI::Loop::Iterator it2;
105  int NumFrequencies = fld ? fld->frequencies.getCount() + 1 : 1;
106  int Frequencies[NumFrequencies];
107  if (fld) {
108  int ct = fld->getCodingType();
109  if (ct > 0) {
110  int n = 1;
111  for (SI::Loop::Iterator it3; fld->frequencies.hasNext(it3); ) {
112  int f = fld->frequencies.getNext(it3);
113  switch (ct) {
114  case 1: f = BCD2INT(f) / 100; break;
115  case 2: f = BCD2INT(f) / 10; break;
116  case 3: f = f * 10; break;
117  default: ;
118  }
119  Frequencies[n++] = f;
120  }
121  }
122  else
123  NumFrequencies = 1;
124  }
125  delete fld;
126 
127  for (SI::Loop::Iterator it2; (d = ts.transportStreamDescriptors.getNext(it2)); ) {
128  switch (d->getDescriptorTag()) {
133  int Frequency = Frequencies[0] = BCD2INT(sd->getFrequency()) / 100;
134  static char Polarizations[] = { 'H', 'V', 'L', 'R' };
135  dtp.SetPolarization(Polarizations[sd->getPolarization()]);
136  static int CodeRates[] = { FEC_NONE, FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_7_8, FEC_8_9, FEC_3_5, FEC_4_5, FEC_9_10, FEC_AUTO, FEC_AUTO, FEC_AUTO, FEC_AUTO, FEC_AUTO, FEC_NONE };
137  dtp.SetCoderateH(CodeRates[sd->getFecInner()]);
138  static int Modulations[] = { QAM_AUTO, QPSK, PSK_8, QAM_16 };
139  dtp.SetModulation(Modulations[sd->getModulationType()]);
141  static int RollOffs[] = { ROLLOFF_35, ROLLOFF_25, ROLLOFF_20, ROLLOFF_AUTO };
142  dtp.SetRollOff(sd->getModulationSystem() ? RollOffs[sd->getRollOff()] : ROLLOFF_AUTO);
143  int SymbolRate = BCD2INT(sd->getSymbolRate()) / 10;
144  if (ThisNIT >= 0) {
145  for (int n = 0; n < NumFrequencies; n++) {
146  if (ISTRANSPONDER(cChannel::Transponder(Frequencies[n], dtp.Polarization()), Transponder())) {
147  nits[ThisNIT].hasTransponder = true;
148  //printf("has transponder %d\n", Transponder());
149  break;
150  }
151  }
152  break;
153  }
154  if (Setup.UpdateChannels >= 5) {
155  bool found = false;
156  bool forceTransponderUpdate = false;
158  if (!Channel->GroupSep() && Channel->Source() == Source && Channel->Nid() == ts.getOriginalNetworkId() && Channel->Tid() == ts.getTransportStreamId()) {
159  int transponder = Channel->Transponder();
160  found = true;
161  if (!ISTRANSPONDER(cChannel::Transponder(Frequency, dtp.Polarization()), transponder)) {
162  for (int n = 0; n < NumFrequencies; n++) {
163  if (ISTRANSPONDER(cChannel::Transponder(Frequencies[n], dtp.Polarization()), transponder)) {
164  Frequency = Frequencies[n];
165  break;
166  }
167  }
168  }
169  if (ISTRANSPONDER(cChannel::Transponder(Frequency, dtp.Polarization()), Transponder())) // only modify channels if we're actually receiving this transponder
170  Channel->SetTransponderData(Source, Frequency, SymbolRate, dtp.ToString('S'));
171  else if (Channel->Srate() != SymbolRate || strcmp(Channel->Parameters(), dtp.ToString('S')))
172  forceTransponderUpdate = true; // get us receiving this transponder
173  }
174  }
175  if (!found || forceTransponderUpdate) {
176  for (int n = 0; n < NumFrequencies; n++) {
177  cChannel *Channel = new cChannel;
178  Channel->SetId(ts.getOriginalNetworkId(), ts.getTransportStreamId(), 0, 0);
179  if (Channel->SetTransponderData(Source, Frequencies[n], SymbolRate, dtp.ToString('S')))
180  EITScanner.AddTransponder(Channel);
181  else
182  delete Channel;
183  }
184  }
185  }
186  }
187  break;
189  if (Setup.UpdateChannels >= 5) {
194  dtp.SetSystem(DVB_SYSTEM_2);
195  dtp.SetStreamId(sd->getInputStreamIdentifier());
196  Channel->SetTransponderData(Channel->Source(), Channel->Frequency(), Channel->Srate(), dtp.ToString('S'));
197  break;
198  }
199  }
200  }
201  }
202  break;
207  int Frequency = Frequencies[0] = BCD2INT(sd->getFrequency()) / 10;
208  //XXX FEC_outer???
209  static int CodeRates[] = { FEC_NONE, FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_7_8, FEC_8_9, FEC_3_5, FEC_4_5, FEC_9_10, FEC_AUTO, FEC_AUTO, FEC_AUTO, FEC_AUTO, FEC_AUTO, FEC_NONE };
210  dtp.SetCoderateH(CodeRates[sd->getFecInner()]);
211  static int Modulations[] = { QPSK, QAM_16, QAM_32, QAM_64, QAM_128, QAM_256, QAM_AUTO };
212  dtp.SetModulation(Modulations[min(sd->getModulation(), 6)]);
213  int SymbolRate = BCD2INT(sd->getSymbolRate()) / 10;
214  if (ThisNIT >= 0) {
215  for (int n = 0; n < NumFrequencies; n++) {
216  if (ISTRANSPONDER(Frequencies[n] / 1000, Transponder())) {
217  nits[ThisNIT].hasTransponder = true;
218  //printf("has transponder %d\n", Transponder());
219  break;
220  }
221  }
222  break;
223  }
224  if (Setup.UpdateChannels >= 5) {
225  bool found = false;
226  bool forceTransponderUpdate = false;
228  if (!Channel->GroupSep() && Channel->Source() == Source && Channel->Nid() == ts.getOriginalNetworkId() && Channel->Tid() == ts.getTransportStreamId()) {
229  int transponder = Channel->Transponder();
230  found = true;
231  if (!ISTRANSPONDER(Frequency / 1000, transponder)) {
232  for (int n = 0; n < NumFrequencies; n++) {
233  if (ISTRANSPONDER(Frequencies[n] / 1000, transponder)) {
234  Frequency = Frequencies[n];
235  break;
236  }
237  }
238  }
239  if (ISTRANSPONDER(Frequency / 1000, Transponder())) // only modify channels if we're actually receiving this transponder
240  Channel->SetTransponderData(Source, Frequency, SymbolRate, dtp.ToString('C'));
241  else if (Channel->Srate() != SymbolRate || strcmp(Channel->Parameters(), dtp.ToString('C')))
242  forceTransponderUpdate = true; // get us receiving this transponder
243  }
244  }
245  if (!found || forceTransponderUpdate) {
246  for (int n = 0; n < NumFrequencies; n++) {
247  cChannel *Channel = new cChannel;
248  Channel->SetId(ts.getOriginalNetworkId(), ts.getTransportStreamId(), 0, 0);
249  if (Channel->SetTransponderData(Source, Frequencies[n], SymbolRate, dtp.ToString('C')))
250  EITScanner.AddTransponder(Channel);
251  else
252  delete Channel;
253  }
254  }
255  }
256  }
257  break;
262  int Frequency = Frequencies[0] = sd->getFrequency() * 10;
263  static int Bandwidths[] = { 8000000, 7000000, 6000000, 5000000, 0, 0, 0, 0 };
264  dtp.SetBandwidth(Bandwidths[sd->getBandwidth()]);
265  static int Constellations[] = { QPSK, QAM_16, QAM_64, QAM_AUTO };
266  dtp.SetModulation(Constellations[sd->getConstellation()]);
267  dtp.SetSystem(DVB_SYSTEM_1);
268  static int Hierarchies[] = { HIERARCHY_NONE, HIERARCHY_1, HIERARCHY_2, HIERARCHY_4, HIERARCHY_AUTO, HIERARCHY_AUTO, HIERARCHY_AUTO, HIERARCHY_AUTO };
269  dtp.SetHierarchy(Hierarchies[sd->getHierarchy()]);
270  static int CodeRates[] = { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_7_8, FEC_AUTO, FEC_AUTO, FEC_AUTO };
271  dtp.SetCoderateH(CodeRates[sd->getCodeRateHP()]);
272  dtp.SetCoderateL(CodeRates[sd->getCodeRateLP()]);
273  static int GuardIntervals[] = { GUARD_INTERVAL_1_32, GUARD_INTERVAL_1_16, GUARD_INTERVAL_1_8, GUARD_INTERVAL_1_4 };
274  dtp.SetGuard(GuardIntervals[sd->getGuardInterval()]);
275  static int TransmissionModes[] = { TRANSMISSION_MODE_2K, TRANSMISSION_MODE_8K, TRANSMISSION_MODE_4K, TRANSMISSION_MODE_AUTO };
276  dtp.SetTransmission(TransmissionModes[sd->getTransmissionMode()]);
277  if (ThisNIT >= 0) {
278  for (int n = 0; n < NumFrequencies; n++) {
279  if (ISTRANSPONDER(Frequencies[n] / 1000000, Transponder())) {
280  nits[ThisNIT].hasTransponder = true;
281  //printf("has transponder %d\n", Transponder());
282  break;
283  }
284  }
285  break;
286  }
287  if (Setup.UpdateChannels >= 5) {
288  bool found = false;
289  bool forceTransponderUpdate = false;
291  if (!Channel->GroupSep() && Channel->Source() == Source && Channel->Nid() == ts.getOriginalNetworkId() && Channel->Tid() == ts.getTransportStreamId()) {
292  int transponder = Channel->Transponder();
293  found = true;
294  if (!ISTRANSPONDER(Frequency / 1000000, transponder)) {
295  for (int n = 0; n < NumFrequencies; n++) {
296  if (ISTRANSPONDER(Frequencies[n] / 1000000, transponder)) {
297  Frequency = Frequencies[n];
298  break;
299  }
300  }
301  }
302  if (ISTRANSPONDER(Frequency / 1000000, Transponder())) // only modify channels if we're actually receiving this transponder
303  Channel->SetTransponderData(Source, Frequency, 0, dtp.ToString('T'));
304  else if (strcmp(Channel->Parameters(), dtp.ToString('T')))
305  forceTransponderUpdate = true; // get us receiving this transponder
306  }
307  }
308  if (!found || forceTransponderUpdate) {
309  for (int n = 0; n < NumFrequencies; n++) {
310  cChannel *Channel = new cChannel;
311  Channel->SetId(ts.getOriginalNetworkId(), ts.getTransportStreamId(), 0, 0);
312  if (Channel->SetTransponderData(Source, Frequencies[n], 0, dtp.ToString('T')))
313  EITScanner.AddTransponder(Channel);
314  else
315  delete Channel;
316  }
317  }
318  }
319  }
320  break;
323  switch (sd->getExtensionDescriptorTag()) {
325  if (Setup.UpdateChannels >= 5) {
328  if (!Channel->GroupSep() && Channel->Source() == Source && Channel->Nid() == ts.getOriginalNetworkId() && Channel->Tid() == ts.getTransportStreamId()) {
330  int Frequency = Channel->Frequency();
331  int SymbolRate = Channel->Srate();
332  //int SystemId = td->getSystemId();
334  dtp.SetSystem(DVB_SYSTEM_2);
335  dtp.SetStreamId(td->getPlpId());
336  if (td->getExtendedDataFlag()) {
337  static int T2Bandwidths[] = { 8000000, 7000000, 6000000, 5000000, 10000000, 1712000, 0, 0 };
338  dtp.SetBandwidth(T2Bandwidths[td->getBandwidth()]);
339  static int T2GuardIntervals[] = { GUARD_INTERVAL_1_32, GUARD_INTERVAL_1_16, GUARD_INTERVAL_1_8, GUARD_INTERVAL_1_4, GUARD_INTERVAL_1_128, GUARD_INTERVAL_19_128, GUARD_INTERVAL_19_256, 0 };
340  dtp.SetGuard(T2GuardIntervals[td->getGuardInterval()]);
341  static int T2TransmissionModes[] = { TRANSMISSION_MODE_2K, TRANSMISSION_MODE_8K, TRANSMISSION_MODE_4K, TRANSMISSION_MODE_1K, TRANSMISSION_MODE_16K, TRANSMISSION_MODE_32K, TRANSMISSION_MODE_AUTO, TRANSMISSION_MODE_AUTO };
342  dtp.SetTransmission(T2TransmissionModes[td->getTransmissionMode()]);
343  //TODO add parsing of frequencies
344  }
345  Channel->SetTransponderData(Source, Frequency, SymbolRate, dtp.ToString('T'));
346  }
347  }
348  }
349  }
350  break;
351  default: ;
352  }
353  }
354  break;
355  default: ;
356  }
357  delete d;
358  }
359  }
360  Channels.Unlock();
361 }
int Tid(void) const
Definition: channels.h:188
void SetId(int Nid, int Tid, int Sid, int Rid=0)
Definition: channels.c:231
void SetTransmission(int Transmission)
Definition: dvbdevice.h:145
cChannels Channels
Definition: channels.c:845
void SetPolarization(char Polarization)
Definition: dvbdevice.h:138
char * getText()
Definition: si.c:221
int Nid(void) const
Definition: channels.h:187
int getTransportStreamId() const
Definition: section.c:102
cEITScanner EITScanner
Definition: eitscan.c:90
u_short networkId
Definition: nit.h:23
u_short networkId
Definition: nit.h:30
int numNits
Definition: nit.h:31
#define MAXNITS
Definition: nit.h:15
int Srate(void) const
Definition: channels.h:164
const cChannel * Channel(void)
Returns the channel of the data delivered to this filter.
Definition: filter.c:99
int BCD2INT(int x)
Definition: tools.c:45
bool GroupSep(void) const
Definition: channels.h:193
DescriptorTag getDescriptorTag() const
Definition: si.c:100
int getExtensionDescriptorTag() const
Definition: descriptor.c:875
cSectionSyncer sectionSyncer
Definition: nit.h:28
T min(T a, T b)
Definition: tools.h:54
int Transponder(void) const
Returns the transponder frequency in MHz, plus the polarization in case of sat.
Definition: channels.c:156
cString ToString(char Type) const
Definition: dvbdevice.c:217
void SetCoderateH(int CoderateH)
Definition: dvbdevice.h:141
const char * Parameters(void) const
Definition: channels.h:194
void SetRollOff(int RollOff)
Definition: dvbdevice.h:148
DescriptorLoop transportStreamDescriptors
Definition: section.h:100
bool SetTransponderData(int Source, int Frequency, int Srate, const char *Parameters, bool Quiet=false)
Definition: channels.c:195
#define DVB_SYSTEM_2
Definition: nit.c:20
void Unlock(void)
Definition: thread.c:170
DescriptorLoop commonDescriptors
Definition: section.h:106
bool hasTransponder
Definition: nit.h:25
int getSectionNumber() const
Definition: si.c:88
#define MAXNETWORKNAME
Definition: nit.h:16
bool Sync(uchar Version, int Number, int LastNumber)
Definition: filter.c:26
char Polarization(void) const
Definition: dvbdevice.h:126
T * Next(const T *object) const
Definition: tools.h:485
virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
Processes the data delivered to this filter.
Definition: nit.c:37
int Source(void) const
Definition: channels.h:163
#define ISTRANSPONDER(f1, f2)
Definition: channels.h:18
void SetSystem(int System)
Definition: dvbdevice.h:144
int Source(void)
Returns the source of the data delivered to this filter.
Definition: filter.c:89
cNit nits[MAXNITS]
Definition: nit.h:29
int getOriginalNetworkId() const
Definition: section.c:106
StructureLoop< TransportStream > transportStreamLoop
Definition: section.h:107
static bool IsSat(int Code)
Definition: sources.h:42
cSetup Setup
Definition: config.c:373
bool Lock(bool Write, int TimeoutMs=0)
Definition: thread.c:155
virtual void SetStatus(bool On)
Turns this filter on or off, depending on the value of On.
Definition: nit.c:29
int Frequency(void) const
Returns the actual frequency, as given in &#39;channels.conf&#39;.
Definition: channels.h:160
bool CheckCRCAndParse()
Definition: si.c:65
bool hasNext(Iterator &it)
Definition: si.h:442
T getNext(Iterator &it) const
Definition: si.h:436
virtual void SetStatus(bool On)
Turns this filter on or off, depending on the value of On.
Definition: filter.c:104
T * First(void) const
Definition: tools.h:482
int getVersionNumber() const
Definition: si.c:84
unsigned char u_char
Definition: headers.h:24
int getLastSectionNumber() const
Definition: si.c:92
int UpdateChannels
Definition: config.h:307
char name[MAXNETWORKNAME]
Definition: nit.h:24
void SetHierarchy(int Hierarchy)
Definition: dvbdevice.h:147
void SetGuard(int Guard)
Definition: dvbdevice.h:146
int Transponder(void)
Returns the transponder of the data delivered to this filter.
Definition: filter.c:94
void Set(u_short Pid, u_char Tid, u_char Mask=0xFF)
Sets the given filter data by calling Add() with Sticky = true.
Definition: filter.c:137
void SetCoderateL(int CoderateL)
Definition: dvbdevice.h:142
cNitFilter(void)
Definition: nit.c:22
int getNetworkId() const
Definition: section.c:89
void AddTransponder(cChannel *Channel)
Definition: eitscan.c:106
#define DVB_SYSTEM_1
Definition: nit.c:19
static int FromData(eSourceType SourceType, int Position=0, bool East=false)
Definition: sources.c:92
void Reset(void)
Definition: filter.c:20
int getCount()
Definition: si.h:419
TypeLoop< ThirtyTwoBit > frequencies
Definition: descriptor.h:350
Descriptor * getNext(Iterator &it)
Definition: si.c:112
void SetBandwidth(int Bandwidth)
Definition: dvbdevice.h:140
void SetModulation(int Modulation)
Definition: dvbdevice.h:143