vdr  1.7.31
libsi/si.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2003 by Marcel Wiesweg *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  * $Id: si.h 2.5 2012/01/11 11:35:17 kls Exp $
10  * *
11  ***************************************************************************/
12 
13 #ifndef LIBSI_SI_H
14 #define LIBSI_SI_H
15 
16 #include <stdint.h>
17 
18 #include "util.h"
19 #include "headers.h"
20 
21 namespace SI {
22 
23 enum TableId { TableIdPAT = 0x00, //program association section
24  TableIdCAT = 0x01, //conditional access section
25  TableIdPMT = 0x02, //program map section
26  TableIdTSDT = 0x03,//transport stream description section
27  TableIdNIT = 0x40, //network information, actual network section
28  TableIdNIT_other = 0x41, //network information section, other network
29  TableIdSDT = 0x42, //service description section
30  TableIdSDT_other = 0x46,
31  TableIdBAT = 0x4A, //bouquet association section
32  TableIdEIT_presentFollowing = 0x4E, //event information section
34  //range from 0x50 to 0x5F
37  //range from 0x60 to 0x6F
40  TableIdTDT = 0x70, //time date section
41  TableIdRST = 0x71, //running status section
42  TableIdST = 0x72, //stuffing section
43  TableIdTOT = 0x73, //time offset section
44  TableIdDIT = 0x7E, //discontinuity information section
45  TableIdSIT = 0x7F, //service information section
46  TableIdAIT = 0x74, //application information section
47  TableIdPremiereCIT = 0xA0 //premiere content information section
48  };
49 
50 
52  // defined by ISO/IEC 13818-1
60  CaDescriptorTag = 0x09,
68  STDDescriptorTag = 0x11,
69  IBPDescriptorTag = 0x12,
70  // defined by ISO-13818-6 (DSM-CC)
72  // 0x14 - 0x3F Reserved
73  // defined by ETSI (EN 300 468)
76  StuffingDescriptorTag = 0x42,
79  VBIDataDescriptorTag = 0x45,
82  ServiceDescriptorTag = 0x48,
84  LinkageDescriptorTag = 0x4A,
91  MocaicDescriptorTag = 0x51,
94  ContentDescriptorTag = 0x54,
96  TeletextDescriptorTag = 0x56,
114  DSNGDescriptorTag = 0x68,
115  PDCDescriptorTag = 0x69,
116  AC3DescriptorTag = 0x6A,
118  CellListDescriptorTag = 0x6C,
125  // defined by ETSI (EN 300 468) v 1.7.1
128  TVAIdDescriptorTag = 0x75,
134  DTSDescriptorTag = 0x7B,
135  AACDescriptorTag = 0x7C,
136  ExtensionDescriptorTag = 0x7F,
137  // Extension descriptors
138  ImageIconDescriptorTag = 0x00,
140  CPDescriptorTag = 0x02,
146  MessageDescriptorTag = 0x08,
150 
151  // Defined by ETSI TS 102 812 (MHP)
152  // They once again start with 0x00 (see page 234, MHP specification)
158  // 0x05 - 0x0A is unimplemented this library
171  // Premiere private Descriptor Tags
173 
174  //a descriptor currently unimplemented in this library
175  //the actual value 0xFF is "forbidden" according to the spec.
177 };
178 
180 
186  };
187 
189  LinkageTypeEPGService = 0x02,
194  LinkageTypeRCSMap = 0x07,
198  };
199 
201  AudioTypeCleanEffects = 0x01,
204  };
205 
206 /* Some principles:
207  - Objects that return references to other objects contained in their data must make sure
208  that the returned objects have been parsed.
209  (the Loop subclasses take care of that.)
210  Note that this does not apply to Loops and Strings (their are never returned by reference, BTW).
211 */
212 
213 class Object : public Parsable {
214 public:
215  Object();
216  Object(CharArray &d);
217  //can only be called once since data is immutable
218  void setData(const unsigned char*data, int size, bool doCopy=true);
219  CharArray getData() { return data; }
220  //returns the valid flag which indicates if data is all right or errors have been encountered
221  bool isValid() { return data.isValid(); }
222  virtual int getLength() = 0;
223 protected:
224  CharArray data;
225  //is protected - not used for sections
226  template <class T> friend class StructureLoop;
227  void setData(CharArray &d);
228  //returns whether the given offset fits within the limits of the actual data
229  //The valid flag will be set accordingly
230  bool checkSize(int offset);
231 };
232 
233 class Section : public Object {
234 public:
235  //convenience: sets data and parses if doParse
236  Section(const unsigned char *data, bool doCopy=true);
237  Section() {}
238  TableId getTableId() const;
239  virtual int getLength();
240 
241  static int getLength(const unsigned char *d);
242  static TableId getTableId(const unsigned char *d);
243 };
244 
245 class CRCSection : public Section {
246 public:
247  //convenience: sets data and parses if doParse
248  CRCSection(const unsigned char *data, bool doCopy=true) : Section(data, doCopy) {}
250  bool isCRCValid();
251  //convenience: isValid+CheckParse
252  bool CheckCRCAndParse();
253 };
254 
255 /* A section which has the ExtendedSectionHeader
256  (section_syntax_indicator==1) */
257 class NumberedSection : public CRCSection {
258 public:
259  NumberedSection(const unsigned char *data, bool doCopy=true) : CRCSection(data, doCopy) {}
261  int getTableIdExtension() const;
262  bool getCurrentNextIndicator() const;
263  int getVersionNumber() const;
264  int getSectionNumber() const;
265  int getLastSectionNumber() const;
266  bool moreThanOneSection() const { return getLastSectionNumber()>0; }
267 
268  static int getTableIdExtension(const unsigned char *d);
269 };
270 
271 class VariableLengthPart : public Object {
272 public:
273  //never forget to call this
274  void setData(CharArray d, int l) { Object::setData(d); checkSize(l); length=l; }
275  //convenience method
276  void setDataAndOffset(CharArray d, int l, int &offset) { Object::setData(d); checkSize(l); length=l; offset+=l; }
277  virtual int getLength() { return length; }
278 private:
279  int length;
280 };
281 
282 class LoopElement : public Object {
283 };
284 
285 class Descriptor : public LoopElement {
286 public:
287  virtual int getLength();
289 
290  static int getLength(const unsigned char *d);
291  static DescriptorTag getDescriptorTag(const unsigned char *d);
292 protected:
293  friend class DescriptorLoop;
294  //returns a subclass of descriptor according to the data given.
295  //The object is allocated with new and must be delete'd.
296  //setData() will have been called, CheckParse() not.
297  //if returnUnimplemetedDescriptor==true:
298  // Never returns null - maybe the UnimplementedDescriptor.
299  //if returnUnimplemetedDescriptor==false:
300  // Never returns the UnimplementedDescriptor - maybe null
301  static Descriptor *getDescriptor(CharArray d, DescriptorTagDomain domain, bool returnUnimplemetedDescriptor);
302 };
303 
304 class Loop : public VariableLengthPart {
305 public:
306  class Iterator {
307  public:
308  Iterator() { i=0; }
309  void reset() { i=0; }
310  private:
311  template <class T> friend class StructureLoop;
312  friend class DescriptorLoop;
313  template <class T> friend class TypeLoop;
314  friend class ExtendedEventDescriptors;
315  int i;
316  };
317 protected:
318  virtual void Parse() {}
319 };
320 
321 //contains LoopElements of one type only
322 template <class T> class StructureLoop : public Loop {
323 public:
324  //currently you must use a while-loop testing for hasNext()
325  //i must be 0 to get the first descriptor (with the first call)
326  bool getNext(T &obj, Iterator &it)
327  {
328  if (!isValid() || it.i >= getLength())
329  return false;
330  CharArray d=data;
331  d.addOffset(it.i);
332  T ret;
333  ret.setData(d);
334  ret.CheckParse();
335  if (!checkSize(ret.getLength()))
336  return false;
337  it.i+=ret.getLength();
338  obj=ret;
339  return true;
340  }
342  {
343  if (!isValid() || it.i >= getLength())
344  return 0;
345  CharArray d=data;
346  d.addOffset(it.i);
347  T *ret=new T();
348  ret->setData(d);
349  ret->CheckParse();
350  if (!checkSize(ret->getLength()))
351  return 0;
352  it.i+=ret->getLength();
353  return ret;
354  }
355  //bool hasNext(Iterator &it) { return getLength() > it.i; }
356 };
357 
358 //contains descriptors of different types
359 class DescriptorLoop : public Loop {
360 public:
362  //i must be 0 to get the first descriptor (with the first call)
363  //All returned descriptors must be delete'd.
364  //returns null if no more descriptors available
365  Descriptor *getNext(Iterator &it);
366  //return the next descriptor with given tag, or 0 if not available.
367  //if returnUnimplemetedDescriptor==true:
368  // an UnimplementedDescriptor may be returned if the next matching descriptor is unimplemented,
369  // 0 will be returned if and only if no matching descriptor is found.
370  //if returnUnimplemetedDescriptor==false:
371  // if 0 is returned, either no descriptor with the given tag was found,
372  // or descriptors were found, but the descriptor type is not implemented
373  //In either case, a return value of 0 indicates that no further calls to this method
374  //with the iterator shall be made.
375  Descriptor *getNext(Iterator &it, DescriptorTag tag, bool returnUnimplemetedDescriptor=false);
376  //return the next descriptor with one of the given tags, or 0 if not available.
377  //if returnUnimplemetedDescriptor==true:
378  // returns 0 if and only if no descriptor with one of the given tags was found.
379  // The UnimplementedDescriptor may be returned.
380  //if returnUnimplemetedDescriptor==false:
381  // if 0 is returned, either no descriptor with one of the given tags was found,
382  // or descriptors were found, but none of them are implemented.
383  // The UnimplementedDescriptor will never be returned.
384  //In either case, a return value of 0 indicates that no further calls to this method
385  //with the iterator shall be made.
386  Descriptor *getNext(Iterator &it, DescriptorTag *tags, int arrayLength, bool returnUnimplemetedDescriptor=false);
387  //returns the number of descriptors in this loop
389  //writes the tags of the descriptors in this loop in the array,
390  // which must at least have the size getNumberOfDescriptors().
391  //The number of descriptors, i.e. getNumberOfDescriptors(), is returned.
392  // You can specify the array type (Descriptor tags are 8 Bit,
393  // you might e.g. choose a char, short, int or DescriptorTag array)
394  template <typename T> int getDescriptorTags(T *tags)
395  {
396  const unsigned char *p=data.getData();
397  const unsigned char *end=p+getLength();
398  int count=0;
399  while (p < end) {
400  tags[count++]=(T)Descriptor::getDescriptorTag(p);
401  p+=Descriptor::getLength(p);
402  }
403  return count;
404  }
405 protected:
406  Descriptor *createDescriptor(int &i, bool returnUnimplemetedDescriptor);
408 };
409 
410 typedef uint8_t EightBit;
411 typedef uint16_t SixteenBit;
412 typedef uint32_t ThirtyTwoBit;
413 typedef uint64_t SixtyFourBit;
414 
415 template <typename T> class TypeLoop : public Loop {
416 public:
417  int getCount() { return getLength()/sizeof(T); }
418  T operator[](const int index) const
419  {
420  switch (sizeof(T)) {
421  case 1:
422  return data[index];
423  case 2:
424  return data.TwoBytes(index);
425  case 4:
426  return data.FourBytes(index);
427  case 8:
428  return (SixtyFourBit(data.FourBytes(index)) << 32) | data.FourBytes(index+4);
429  default:
430  return 0; // just to avoid a compiler warning
431  }
432  return 0; // just to avoid a compiler warning
433  }
434  T getNext(Iterator &it) const
435  {
436  T ret=operator[](it.i);
437  it.i+=sizeof(T);
438  return ret;
439  }
440  bool hasNext(Iterator &it) { return isValid() && (getLength() > it.i); }
441 };
442 
443 class MHP_DescriptorLoop : public DescriptorLoop {
444 public:
446 };
447 
448 //Premiere Content Information Table
449 class PCIT_DescriptorLoop : public DescriptorLoop {
450 public:
452 };
453 
454 //The content of the ExtendedEventDescriptor may be split over several
455 //descriptors if the text is longer than 256 bytes.
456 //The following classes provide base functionality to handle this case.
457 class GroupDescriptor : public Descriptor {
458 public:
459  virtual int getDescriptorNumber() = 0;
460  virtual int getLastDescriptorNumber() = 0;
461 };
462 
463 class DescriptorGroup {
464 public:
467  void Add(GroupDescriptor *d);
468  void Delete();
469  int getLength() { return length; }
471  bool isComplete(); //if all descriptors have been added
472 protected:
473  int length;
476 };
477 
478 class String : public VariableLengthPart {
479 public:
480  //A note to the length: getLength() returns the length of the raw data.
481  //The text may be shorter. Its length can be obtained with one of the
482  //getText functions and strlen.
483 
484  //returns text. Data is allocated with new and must be delete'd by the user.
485  char *getText();
486  //copies text into given buffer.
487  //a buffer of size getLength()+1 is guaranteed to be sufficiently large.
488  //In most descriptors the string length is an 8-bit field,
489  //so the maximum there is 256.
490  //returns the given buffer for convenience.
491  //The emphasis marks 0x86 and 0x87 are still available.
492  char *getText(char *buffer, int size);
493  //The same semantics as for getText(char*) apply.
494  //The short version of the text according to ETSI TR 101 211 (chapter 4.6)
495  //will be written into the shortVersion buffer (which should, therefore, have the same
496  //length as buffer). If no shortVersion is available, shortVersion will contain
497  //an empty string.
498  //The emphasis marks 0x86 and 0x87 are still available in buffer, but not in shortVersion.
499  char *getText(char *buffer, char *shortVersion, int sizeBuffer, int sizeShortVersion);
500 protected:
501  virtual void Parse() {}
502  void decodeText(char *buffer, int size);
503  void decodeText(char *buffer, char *shortVersion, int sizeBuffer, int sizeShortVersion);
504 };
505 
506 // Call this function to set the system character table. CharacterTable is a string
507 // like "iso8859-15" or "utf-8" (case insensitive).
508 // Returns true if the character table was recognized.
509 bool SetSystemCharacterTable(const char *CharacterTable);
510 // Determines the character table used in the given buffer and returns
511 // a string indicating that table. If no table can be determined, the
512 // default ISO6937 is returned. If a table can be determined, the buffer
513 // and length are adjusted accordingly.
514 const char *getCharacterTable(const unsigned char *&buffer, int &length, bool *isSingleByte = NULL);
515 bool convertCharacterTable(const char *from, size_t fromLength, char *to, size_t toLength, const char *fromCode);
517 
518 } //end of namespace
519 
520 #endif //LIBSI_SI_H