vdr  1.7.31
include/vdr/remux.h
Go to the documentation of this file.
1 /*
2  * remux.h: Tools for detecting frames and handling PAT/PMT
3  *
4  * See the main source file 'vdr.c' for copyright information and
5  * how to reach the author.
6  *
7  * $Id: remux.h 2.32 2011/09/04 12:48:26 kls Exp $
8  */
9 
10 #ifndef __REMUX_H
11 #define __REMUX_H
12 
13 #include "channels.h"
14 #include "tools.h"
15 
16 enum ePesHeader {
18  phInvalid = 0,
19  phMPEG1 = 1,
20  phMPEG2 = 2
21  };
22 
23 ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader = NULL);
24 
25 class cRemux {
26 public:
27  static void SetBrokenLink(uchar *Data, int Length);
28  };
29 
30 // Some TS handling tools.
31 // The following functions all take a pointer to one complete TS packet.
32 
33 #define TS_SYNC_BYTE 0x47
34 #define TS_SIZE 188
35 #define TS_ERROR 0x80
36 #define TS_PAYLOAD_START 0x40
37 #define TS_TRANSPORT_PRIORITY 0x20
38 #define TS_PID_MASK_HI 0x1F
39 #define TS_SCRAMBLING_CONTROL 0xC0
40 #define TS_ADAPT_FIELD_EXISTS 0x20
41 #define TS_PAYLOAD_EXISTS 0x10
42 #define TS_CONT_CNT_MASK 0x0F
43 #define TS_ADAPT_DISCONT 0x80
44 #define TS_ADAPT_RANDOM_ACC 0x40 // would be perfect for detecting independent frames, but unfortunately not used by all broadcasters
45 #define TS_ADAPT_ELEM_PRIO 0x20
46 #define TS_ADAPT_PCR 0x10
47 #define TS_ADAPT_OPCR 0x08
48 #define TS_ADAPT_SPLICING 0x04
49 #define TS_ADAPT_TP_PRIVATE 0x02
50 #define TS_ADAPT_EXTENSION 0x01
51 
52 #define PATPID 0x0000 // PAT PID (constant 0)
53 #define MAXPID 0x2000 // for arrays that use a PID as the index
54 
55 inline bool TsHasPayload(const uchar *p)
56 {
57  return p[3] & TS_PAYLOAD_EXISTS;
58 }
59 
60 inline bool TsHasAdaptationField(const uchar *p)
61 {
62  return p[3] & TS_ADAPT_FIELD_EXISTS;
63 }
64 
65 inline bool TsPayloadStart(const uchar *p)
66 {
67  return p[1] & TS_PAYLOAD_START;
68 }
69 
70 inline bool TsError(const uchar *p)
71 {
72  return p[1] & TS_ERROR;
73 }
74 
75 inline int TsPid(const uchar *p)
76 {
77  return (p[1] & TS_PID_MASK_HI) * 256 + p[2];
78 }
79 
80 inline bool TsIsScrambled(const uchar *p)
81 {
82  return p[3] & TS_SCRAMBLING_CONTROL;
83 }
84 
85 inline int TsPayloadOffset(const uchar *p)
86 {
87  int o = TsHasAdaptationField(p) ? p[4] + 5 : 4;
88  return o <= TS_SIZE ? o : TS_SIZE;
89 }
90 
91 inline int TsGetPayload(const uchar **p)
92 {
93  if (TsHasPayload(*p)) {
94  int o = TsPayloadOffset(*p);
95  *p += o;
96  return TS_SIZE - o;
97  }
98  return 0;
99 }
100 
101 inline int TsContinuityCounter(const uchar *p)
102 {
103  return p[3] & TS_CONT_CNT_MASK;
104 }
105 
106 inline int TsGetAdaptationField(const uchar *p)
107 {
108  return TsHasAdaptationField(p) ? p[5] : 0x00;
109 }
110 
111 // The following functions all take a pointer to a sequence of complete TS packets.
112 
113 int64_t TsGetPts(const uchar *p, int l);
114 void TsSetTeiOnBrokenPackets(uchar *p, int l);
115 
116 // Some PES handling tools:
117 // The following functions that take a pointer to PES data all assume that
118 // there is enough data so that PesLongEnough() returns true.
119 
120 inline bool PesLongEnough(int Length)
121 {
122  return Length >= 6;
123 }
124 
125 inline bool PesHasLength(const uchar *p)
126 {
127  return p[4] | p[5];
128 }
129 
130 inline int PesLength(const uchar *p)
131 {
132  return 6 + p[4] * 256 + p[5];
133 }
134 
135 inline int PesPayloadOffset(const uchar *p)
136 {
137  return 9 + p[8];
138 }
139 
140 inline bool PesHasPts(const uchar *p)
141 {
142  return (p[7] & 0x80) && p[8] >= 5;
143 }
144 
145 inline int64_t PesGetPts(const uchar *p)
146 {
147  return ((((int64_t)p[ 9]) & 0x0E) << 29) |
148  (( (int64_t)p[10]) << 22) |
149  ((((int64_t)p[11]) & 0xFE) << 14) |
150  (( (int64_t)p[12]) << 7) |
151  ((((int64_t)p[13]) & 0xFE) >> 1);
152 }
153 
154 // PAT/PMT Generator:
155 
156 #define MAX_SECTION_SIZE 4096 // maximum size of an SI section
157 #define MAX_PMT_TS (MAX_SECTION_SIZE / TS_SIZE + 1)
158 
160 private:
161  uchar pat[TS_SIZE]; // the PAT always fits into a single TS packet
162  uchar pmt[MAX_PMT_TS][TS_SIZE]; // the PMT may well extend over several TS packets
168  int pmtPid;
170  void IncCounter(int &Counter, uchar *TsPacket);
171  void IncVersion(int &Version);
172  void IncEsInfoLength(int Length);
173 protected:
174  int MakeStream(uchar *Target, uchar Type, int Pid);
175  int MakeAC3Descriptor(uchar *Target, uchar Type);
176  int MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId);
177  int MakeTeletextDescriptor(uchar *Target, const tTeletextSubtitlePage *pages, int pageCount);
178  int MakeLanguageDescriptor(uchar *Target, const char *Language);
179  int MakeCRC(uchar *Target, const uchar *Data, int Length);
180  void GeneratePmtPid(const cChannel *Channel);
183  void GeneratePat(void);
185  void GeneratePmt(const cChannel *Channel);
188 public:
189  cPatPmtGenerator(const cChannel *Channel = NULL);
190  void SetVersions(int PatVersion, int PmtVersion);
199  void SetChannel(const cChannel *Channel);
201  uchar *GetPat(void);
204  uchar *GetPmt(int &Index);
209  };
210 
211 // PAT/PMT Parser:
212 
214 private:
216  int pmtSize;
219  int pmtPid;
220  int vpid;
221  int ppid;
222  int vtype;
223  int tpid;
224  int apids[MAXAPIDS + 1]; // list is zero-terminated
225  int atypes[MAXAPIDS + 1]; // list is zero-terminated
227  int dpids[MAXDPIDS + 1]; // list is zero-terminated
228  int dtypes[MAXDPIDS + 1]; // list is zero-terminated
230  int spids[MAXSPIDS + 1]; // list is zero-terminated
238 protected:
239  int SectionLength(const uchar *Data, int Length) { return (Length >= 3) ? ((int(Data[1]) & 0x0F) << 8)| Data[2] : 0; }
240 public:
241  cPatPmtParser(bool UpdatePrimaryDevice = false);
242  void Reset(void);
245  void ParsePat(const uchar *Data, int Length);
248  void ParsePmt(const uchar *Data, int Length);
255  bool GetVersions(int &PatVersion, int &PmtVersion) const;
258  int PmtPid(void) const { return pmtPid; }
261  int Vpid(void) const { return vpid; }
264  int Ppid(void) const { return ppid; }
267  int Vtype(void) const { return vtype; }
270  int Tpid(void) { return tpid; }
273  const int *Apids(void) const { return apids; }
274  const int *Dpids(void) const { return dpids; }
275  const int *Spids(void) const { return spids; }
276  int Apid(int i) const { return (0 <= i && i < MAXAPIDS) ? apids[i] : 0; }
277  int Dpid(int i) const { return (0 <= i && i < MAXDPIDS) ? dpids[i] : 0; }
278  int Spid(int i) const { return (0 <= i && i < MAXSPIDS) ? spids[i] : 0; }
279  int Atype(int i) const { return (0 <= i && i < MAXAPIDS) ? atypes[i] : 0; }
280  int Dtype(int i) const { return (0 <= i && i < MAXDPIDS) ? dtypes[i] : 0; }
281  const char *Alang(int i) const { return (0 <= i && i < MAXAPIDS) ? alangs[i] : ""; }
282  const char *Dlang(int i) const { return (0 <= i && i < MAXDPIDS) ? dlangs[i] : ""; }
283  const char *Slang(int i) const { return (0 <= i && i < MAXSPIDS) ? slangs[i] : ""; }
284  uchar SubtitlingType(int i) const { return (0 <= i && i < MAXSPIDS) ? subtitlingTypes[i] : uchar(0); }
285  uint16_t CompositionPageId(int i) const { return (0 <= i && i < MAXSPIDS) ? compositionPageIds[i] : uint16_t(0); }
286  uint16_t AncillaryPageId(int i) const { return (0 <= i && i < MAXSPIDS) ? ancillaryPageIds[i] : uint16_t(0); }
289  };
290 
291 // TS to PES converter:
292 // Puts together the payload of several TS packets that form one PES
293 // packet.
294 
295 class cTsToPes {
296 private:
298  int size;
299  int length;
300  int offset;
304 public:
305  cTsToPes(void);
306  ~cTsToPes();
307  void PutTs(const uchar *Data, int Length);
317  const uchar *GetPes(int &Length);
331  void SetRepeatLast(void);
334  void Reset(void);
338  };
339 
340 // Some helper functions for debugging:
341 
342 void BlockDump(const char *Name, const u_char *Data, int Length);
343 void TsDump(const char *Name, const u_char *Data, int Length);
344 void PesDump(const char *Name, const u_char *Data, int Length);
345 
346 // Frame detector:
347 
348 #define MIN_TS_PACKETS_FOR_FRAME_DETECTOR 5
349 
351 private:
352  enum { MaxPtsValues = 150 };
353  int pid;
354  int type;
355  bool synced;
356  bool newFrame;
358  uint32_t ptsValues[MaxPtsValues]; // 32 bit is enough - we only need the delta
362  bool isVideo;
365  int framesPerPayloadUnit; // Some broadcasters send one frame per payload unit (== 1),
366  // some put an entire GOP into one payload unit (> 1), and
367  // some spread a single frame over several payload units (< 0).
369  bool scanning;
370  uint32_t scanner;
371  int SkipPackets(const uchar *&Data, int &Length, int &Processed, int &FrameTypeOffset);
372 public:
373  cFrameDetector(int Pid = 0, int Type = 0);
377  void SetPid(int Pid, int Type);
379  void Reset(void);
382  int Analyze(const uchar *Data, int Length);
388  bool Synced(void) { return synced; }
390  bool NewFrame(void) { return newFrame; }
393  bool IndependentFrame(void) { return independentFrame; }
397  double FramesPerSecond(void) { return framesPerSecond; }
400  };
401 
402 #endif // __REMUX_H