vdr  2.2.0
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 3.4 2014/03/22 14:58:24 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 CATPID 0x0001 // CAT PID (constant 1)
54 #define MAXPID 0x2000 // for arrays that use a PID as the index
55 
56 #define PTSTICKS 90000 // number of PTS ticks per second
57 #define PCRFACTOR 300 // conversion from 27MHz PCR extension to 90kHz PCR base
58 #define MAX33BIT 0x00000001FFFFFFFFLL // max. possible value with 33 bit
59 #define MAX27MHZ ((MAX33BIT + 1) * PCRFACTOR - 1) // max. possible PCR value
60 
61 inline bool TsHasPayload(const uchar *p)
62 {
63  return p[3] & TS_PAYLOAD_EXISTS;
64 }
65 
66 inline bool TsHasAdaptationField(const uchar *p)
67 {
68  return p[3] & TS_ADAPT_FIELD_EXISTS;
69 }
70 
71 inline bool TsPayloadStart(const uchar *p)
72 {
73  return p[1] & TS_PAYLOAD_START;
74 }
75 
76 inline bool TsError(const uchar *p)
77 {
78  return p[1] & TS_ERROR;
79 }
80 
81 inline int TsPid(const uchar *p)
82 {
83  return (p[1] & TS_PID_MASK_HI) * 256 + p[2];
84 }
85 
86 inline bool TsIsScrambled(const uchar *p)
87 {
88  return p[3] & TS_SCRAMBLING_CONTROL;
89 }
90 
92 {
93  return p[3] & TS_CONT_CNT_MASK;
94 }
95 
96 inline void TsSetContinuityCounter(uchar *p, uchar Counter)
97 {
98  p[3] = (p[3] & ~TS_CONT_CNT_MASK) | (Counter & TS_CONT_CNT_MASK);
99 }
100 
101 inline int TsPayloadOffset(const uchar *p)
102 {
103  int o = TsHasAdaptationField(p) ? p[4] + 5 : 4;
104  return o <= TS_SIZE ? o : TS_SIZE;
105 }
106 
107 inline int TsGetPayload(const uchar **p)
108 {
109  if (TsHasPayload(*p)) {
110  int o = TsPayloadOffset(*p);
111  *p += o;
112  return TS_SIZE - o;
113  }
114  return 0;
115 }
116 
117 inline int TsContinuityCounter(const uchar *p)
118 {
119  return p[3] & TS_CONT_CNT_MASK;
120 }
121 
122 inline int64_t TsGetPcr(const uchar *p)
123 {
124  if (TsHasAdaptationField(p)) {
125  if (p[4] >= 7 && (p[5] & TS_ADAPT_PCR)) {
126  return ((((int64_t)p[ 6]) << 25) |
127  (((int64_t)p[ 7]) << 17) |
128  (((int64_t)p[ 8]) << 9) |
129  (((int64_t)p[ 9]) << 1) |
130  (((int64_t)p[10]) >> 7)) * PCRFACTOR +
131  (((((int)p[10]) & 0x01) << 8) |
132  ( ((int)p[11])));
133  }
134  }
135  return -1;
136 }
137 
138 void TsHidePayload(uchar *p);
139 void TsSetPcr(uchar *p, int64_t Pcr);
140 
141 // The following functions all take a pointer to a sequence of complete TS packets.
142 
143 int64_t TsGetPts(const uchar *p, int l);
144 int64_t TsGetDts(const uchar *p, int l);
145 void TsSetPts(uchar *p, int l, int64_t Pts);
146 void TsSetDts(uchar *p, int l, int64_t Dts);
147 
148 // Some PES handling tools:
149 // The following functions that take a pointer to PES data all assume that
150 // there is enough data so that PesLongEnough() returns true.
151 
152 inline bool PesLongEnough(int Length)
153 {
154  return Length >= 6;
155 }
156 
157 inline bool PesHasLength(const uchar *p)
158 {
159  return p[4] | p[5];
160 }
161 
162 inline int PesLength(const uchar *p)
163 {
164  return 6 + p[4] * 256 + p[5];
165 }
166 
167 inline int PesPayloadOffset(const uchar *p)
168 {
169  return 9 + p[8];
170 }
171 
172 inline bool PesHasPts(const uchar *p)
173 {
174  return (p[7] & 0x80) && p[8] >= 5;
175 }
176 
177 inline bool PesHasDts(const uchar *p)
178 {
179  return (p[7] & 0x40) && p[8] >= 10;
180 }
181 
182 inline int64_t PesGetPts(const uchar *p)
183 {
184  return ((((int64_t)p[ 9]) & 0x0E) << 29) |
185  (( (int64_t)p[10]) << 22) |
186  ((((int64_t)p[11]) & 0xFE) << 14) |
187  (( (int64_t)p[12]) << 7) |
188  ((((int64_t)p[13]) & 0xFE) >> 1);
189 }
190 
191 inline int64_t PesGetDts(const uchar *p)
192 {
193  return ((((int64_t)p[14]) & 0x0E) << 29) |
194  (( (int64_t)p[15]) << 22) |
195  ((((int64_t)p[16]) & 0xFE) << 14) |
196  (( (int64_t)p[17]) << 7) |
197  ((((int64_t)p[18]) & 0xFE) >> 1);
198 }
199 
200 void PesSetPts(uchar *p, int64_t Pts);
201 void PesSetDts(uchar *p, int64_t Dts);
202 
203 // PTS handling:
204 
205 inline int64_t PtsAdd(int64_t Pts1, int64_t Pts2) { return (Pts1 + Pts2) & MAX33BIT; }
207 int64_t PtsDiff(int64_t Pts1, int64_t Pts2);
212 
213 // A transprent TS payload handler:
214 
215 class cTsPayload {
216 private:
218  int length;
219  int pid;
220  int index; // points to the next byte to process
221  int numPacketsPid; // the number of TS packets with the given PID (for statistical purposes)
222  int numPacketsOther; // the number of TS packets with other PIDs (for statistical purposes)
223  uchar SetEof(void);
224 protected:
225  void Reset(void);
226 public:
227  cTsPayload(void);
228  cTsPayload(uchar *Data, int Length, int Pid = -1);
230  void Setup(uchar *Data, int Length, int Pid = -1);
238  bool AtTsStart(void) { return index < length && (index % TS_SIZE) == 0; }
241  bool AtPayloadStart(void) { return AtTsStart() && TsPayloadStart(data + index); }
244  int Available(void) { return length - index; }
247  int Used(void) { return (index + TS_SIZE - 1) / TS_SIZE * TS_SIZE; }
251  bool Eof(void) const { return index >= length; }
253  void Statistics(void) const;
257  uchar GetByte(void);
259  bool SkipBytes(int Bytes);
262  bool SkipPesHeader(void);
264  int GetLastIndex(void);
267  void SetByte(uchar Byte, int Index);
272  bool Find(uint32_t Code);
280  };
281 
282 // PAT/PMT Generator:
283 
284 #define MAX_SECTION_SIZE 4096 // maximum size of an SI section
285 #define MAX_PMT_TS (MAX_SECTION_SIZE / TS_SIZE + 1)
286 
288 private:
289  uchar pat[TS_SIZE]; // the PAT always fits into a single TS packet
290  uchar pmt[MAX_PMT_TS][TS_SIZE]; // the PMT may well extend over several TS packets
296  int pmtPid;
298  void IncCounter(int &Counter, uchar *TsPacket);
299  void IncVersion(int &Version);
300  void IncEsInfoLength(int Length);
301 protected:
302  int MakeStream(uchar *Target, uchar Type, int Pid);
303  int MakeAC3Descriptor(uchar *Target, uchar Type);
304  int MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId);
305  int MakeLanguageDescriptor(uchar *Target, const char *Language);
306  int MakeCRC(uchar *Target, const uchar *Data, int Length);
307  void GeneratePmtPid(const cChannel *Channel);
310  void GeneratePat(void);
312  void GeneratePmt(const cChannel *Channel);
315 public:
316  cPatPmtGenerator(const cChannel *Channel = NULL);
317  void SetVersions(int PatVersion, int PmtVersion);
326  void SetChannel(const cChannel *Channel);
328  uchar *GetPat(void);
331  uchar *GetPmt(int &Index);
336  };
337 
338 // PAT/PMT Parser:
339 
340 #define MAX_PMT_PIDS 32
341 
343 private:
345  int pmtSize;
348  int pmtPids[MAX_PMT_PIDS + 1]; // list is zero-terminated
349  int vpid;
350  int ppid;
351  int vtype;
352  int apids[MAXAPIDS + 1]; // list is zero-terminated
353  int atypes[MAXAPIDS + 1]; // list is zero-terminated
354  char alangs[MAXAPIDS][MAXLANGCODE2];
355  int dpids[MAXDPIDS + 1]; // list is zero-terminated
356  int dtypes[MAXDPIDS + 1]; // list is zero-terminated
357  char dlangs[MAXDPIDS][MAXLANGCODE2];
358  int spids[MAXSPIDS + 1]; // list is zero-terminated
359  char slangs[MAXSPIDS][MAXLANGCODE2];
360  uchar subtitlingTypes[MAXSPIDS];
361  uint16_t compositionPageIds[MAXSPIDS];
362  uint16_t ancillaryPageIds[MAXSPIDS];
364 protected:
365  int SectionLength(const uchar *Data, int Length) { return (Length >= 3) ? ((int(Data[1]) & 0x0F) << 8)| Data[2] : 0; }
366 public:
367  cPatPmtParser(bool UpdatePrimaryDevice = false);
368  void Reset(void);
371  void ParsePat(const uchar *Data, int Length);
374  void ParsePmt(const uchar *Data, int Length);
381  bool ParsePatPmt(const uchar *Data, int Length);
385  bool GetVersions(int &PatVersion, int &PmtVersion) const;
388  bool IsPmtPid(int Pid) const { for (int i = 0; pmtPids[i]; i++) if (pmtPids[i] == Pid) return true; return false; }
391  int Vpid(void) const { return vpid; }
394  int Ppid(void) const { return ppid; }
397  int Vtype(void) const { return vtype; }
400  const int *Apids(void) const { return apids; }
401  const int *Dpids(void) const { return dpids; }
402  const int *Spids(void) const { return spids; }
403  int Apid(int i) const { return (0 <= i && i < MAXAPIDS) ? apids[i] : 0; }
404  int Dpid(int i) const { return (0 <= i && i < MAXDPIDS) ? dpids[i] : 0; }
405  int Spid(int i) const { return (0 <= i && i < MAXSPIDS) ? spids[i] : 0; }
406  int Atype(int i) const { return (0 <= i && i < MAXAPIDS) ? atypes[i] : 0; }
407  int Dtype(int i) const { return (0 <= i && i < MAXDPIDS) ? dtypes[i] : 0; }
408  const char *Alang(int i) const { return (0 <= i && i < MAXAPIDS) ? alangs[i] : ""; }
409  const char *Dlang(int i) const { return (0 <= i && i < MAXDPIDS) ? dlangs[i] : ""; }
410  const char *Slang(int i) const { return (0 <= i && i < MAXSPIDS) ? slangs[i] : ""; }
411  uchar SubtitlingType(int i) const { return (0 <= i && i < MAXSPIDS) ? subtitlingTypes[i] : uchar(0); }
412  uint16_t CompositionPageId(int i) const { return (0 <= i && i < MAXSPIDS) ? compositionPageIds[i] : uint16_t(0); }
413  uint16_t AncillaryPageId(int i) const { return (0 <= i && i < MAXSPIDS) ? ancillaryPageIds[i] : uint16_t(0); }
414  };
415 
416 // TS to PES converter:
417 // Puts together the payload of several TS packets that form one PES
418 // packet.
419 
420 class cTsToPes {
421 private:
423  int size;
424  int length;
425  int offset;
429 public:
430  cTsToPes(void);
431  ~cTsToPes();
432  void PutTs(const uchar *Data, int Length);
442  const uchar *GetPes(int &Length);
456  void SetRepeatLast(void);
459  void Reset(void);
463  };
464 
465 // Some helper functions for debugging:
466 
467 void BlockDump(const char *Name, const u_char *Data, int Length);
468 void TsDump(const char *Name, const u_char *Data, int Length);
469 void PesDump(const char *Name, const u_char *Data, int Length);
470 
471 // Frame detector:
472 
473 #define MIN_TS_PACKETS_FOR_FRAME_DETECTOR 100
474 
475 class cFrameParser;
476 
478 private:
479  enum { MaxPtsValues = 150 };
480  int pid;
481  int type;
482  bool synced;
483  bool newFrame;
485  uint32_t ptsValues[MaxPtsValues]; // 32 bit is enough - we only need the delta
488  bool isVideo;
491  int framesPerPayloadUnit; // Some broadcasters send one frame per payload unit (== 1),
492  // while others put an entire GOP into one payload unit (> 1).
493  bool scanning;
495 public:
496  cFrameDetector(int Pid = 0, int Type = 0);
500  void SetPid(int Pid, int Type);
502  int Analyze(const uchar *Data, int Length);
508  bool Synced(void) { return synced; }
510  bool NewFrame(void) { return newFrame; }
513  bool IndependentFrame(void) { return independentFrame; }
517  double FramesPerSecond(void) { return framesPerSecond; }
520  };
521 
522 #endif // __REMUX_H
int framesInPayloadUnit
Definition: remux.h:490
unsigned char uchar
Definition: tools.h:30
int64_t PtsAdd(int64_t Pts1, int64_t Pts2)
Adds the given PTS values, taking into account the 33bit wrap around.
Definition: remux.h:205
uchar * data
Definition: remux.h:217
int Used(void)
Returns the number of raw bytes that have already been used (e.g.
Definition: remux.h:247
bool repeatLast
Definition: remux.h:428
int vpid
Definition: remux.h:349
int index
Definition: remux.h:220
int pid
Definition: remux.h:219
const char * Alang(int i) const
Definition: remux.h:408
bool TsError(const uchar *p)
Definition: remux.h:76
int PesPayloadOffset(const uchar *p)
Definition: remux.h:167
bool TsHasAdaptationField(const uchar *p)
Definition: remux.h:66
bool IsPmtPid(int Pid) const
Returns true if Pid the one of the PMT pids as defined by the current PAT.
Definition: remux.h:388
int framesPerPayloadUnit
Definition: remux.h:491
int pmtSize
Definition: remux.h:345
#define MAX33BIT
Definition: remux.h:58
bool TsPayloadStart(const uchar *p)
Definition: remux.h:71
int64_t TsGetDts(const uchar *p, int l)
Definition: remux.c:160
int Spid(int i) const
Definition: remux.h:405
uchar SubtitlingType(int i) const
Definition: remux.h:411
int64_t PesGetPts(const uchar *p)
Definition: remux.h:182
int numPacketsPid
Definition: remux.h:221
bool TsHasPayload(const uchar *p)
Definition: remux.h:61
void TsHidePayload(uchar *p)
Definition: remux.c:121
#define TS_ERROR
Definition: remux.h:35
#define TS_SCRAMBLING_CONTROL
Definition: remux.h:39
#define TS_ADAPT_FIELD_EXISTS
Definition: remux.h:40
int Vpid(void) const
Returns the video pid as defined by the current PMT, or 0 if no video pid has been detected...
Definition: remux.h:391
void BlockDump(const char *Name, const u_char *Data, int Length)
Definition: remux.c:1033
void TsSetDts(uchar *p, int l, int64_t Dts)
Definition: remux.c:187
bool AtPayloadStart(void)
Returns true if this payload handler is currently pointing to the first byte of a TS packet that star...
Definition: remux.h:241
bool PesHasPts(const uchar *p)
Definition: remux.h:172
int length
Definition: remux.h:424
bool isVideo
Definition: remux.h:488
int pmtVersion
Definition: remux.h:347
int numPtsValues
Definition: remux.h:486
bool independentFrame
Definition: remux.h:484
int lastLength
Definition: remux.h:427
int SectionLength(const uchar *Data, int Length)
Definition: remux.h:365
int Available(void)
Returns the number of raw bytes (including any TS headers) still available in the TS payload handler...
Definition: remux.h:244
int patCounter
Definition: remux.h:292
bool Eof(void) const
Returns true if all available bytes of the TS payload have been processed.
Definition: remux.h:251
uchar * lastData
Definition: remux.h:426
bool Synced(void)
Returns true if the frame detector has synced on the data stream.
Definition: remux.h:508
bool PesLongEnough(int Length)
Definition: remux.h:152
int64_t TsGetPts(const uchar *p, int l)
Definition: remux.c:147
#define MAX_SECTION_SIZE
Definition: remux.h:284
int TsPid(const uchar *p)
Definition: remux.h:81
const char * Slang(int i) const
Definition: remux.h:410
double framesPerSecond
Definition: remux.h:489
#define TS_PAYLOAD_EXISTS
Definition: remux.h:41
int PesLength(const uchar *p)
Definition: remux.h:162
void TsSetPcr(uchar *p, int64_t Pcr)
Definition: remux.c:131
void PesDump(const char *Name, const u_char *Data, int Length)
Definition: remux.c:1059
Definition: remux.h:20
int Vtype(void) const
Returns the video stream type as defined by the current PMT, or 0 if no video stream type has been de...
Definition: remux.h:397
int ppid
Definition: remux.h:350
int TsContinuityCounter(const uchar *p)
Definition: remux.h:117
const int * Spids(void) const
Definition: remux.h:402
bool synced
Definition: remux.h:482
uchar TsGetContinuityCounter(const uchar *p)
Definition: remux.h:91
bool PesHasDts(const uchar *p)
Definition: remux.h:177
void TsDump(const char *Name, const u_char *Data, int Length)
Definition: remux.c:1044
void PesSetDts(uchar *p, int64_t Dts)
Definition: remux.c:212
const int * Dpids(void) const
Definition: remux.h:401
int Apid(int i) const
Definition: remux.h:403
void TsSetContinuityCounter(uchar *p, uchar Counter)
Definition: remux.h:96
int numPmtPackets
Definition: remux.h:291
cSetup Setup
Definition: config.c:372
int Dpid(int i) const
Definition: remux.h:404
int TsGetPayload(const uchar **p)
Definition: remux.h:107
int pmtCounter
Definition: remux.h:293
Definition: remux.h:25
int Dtype(int i) const
Definition: remux.h:407
int length
Definition: remux.h:218
#define TS_CONT_CNT_MASK
Definition: remux.h:42
void PesSetPts(uchar *p, int64_t Pts)
Definition: remux.c:203
#define MAXLANGCODE2
Definition: channels.h:41
const int * Apids(void) const
Definition: remux.h:400
static void SetBrokenLink(uchar *Data, int Length)
Definition: remux.c:102
int64_t TsGetPcr(const uchar *p)
Definition: remux.h:122
int patVersion
Definition: remux.h:294
cFrameParser * parser
Definition: remux.h:494
void TsSetPts(uchar *p, int l, int64_t Pts)
Definition: remux.c:173
int numPacketsOther
Definition: remux.h:222
#define MAXDPIDS
Definition: channels.h:36
#define MAX_PMT_TS
Definition: remux.h:285
int size
Definition: remux.h:423
#define PCRFACTOR
Definition: remux.h:57
#define MAX_PMT_PIDS
Definition: remux.h:340
bool PesHasLength(const uchar *p)
Definition: remux.h:157
uint16_t CompositionPageId(int i) const
Definition: remux.h:412
bool NewFrame(void)
Returns true if the data given to the last call to Analyze() started a new frame. ...
Definition: remux.h:510
bool IndependentFrame(void)
Returns true if a new frame was detected and this is an independent frame (i.e.
Definition: remux.h:513
unsigned char u_char
Definition: headers.h:24
bool TsIsScrambled(const uchar *p)
Definition: remux.h:86
int64_t PesGetDts(const uchar *p)
Definition: remux.h:191
ePesHeader
Definition: remux.h:16
uchar * esInfoLength
Definition: remux.h:297
const char * Dlang(int i) const
Definition: remux.h:409
#define TS_PAYLOAD_START
Definition: remux.h:36
bool updatePrimaryDevice
Definition: remux.h:363
double FramesPerSecond(void)
Returns the number of frames per second, or 0 if this information is not available.
Definition: remux.h:517
#define MAXSPIDS
Definition: channels.h:37
bool newFrame
Definition: remux.h:483
#define TS_SIZE
Definition: remux.h:34
Definition: remux.h:19
uint16_t AncillaryPageId(int i) const
Definition: remux.h:413
int TsPayloadOffset(const uchar *p)
Definition: remux.h:101
bool scanning
Definition: remux.h:493
int offset
Definition: remux.h:425
#define TS_ADAPT_PCR
Definition: remux.h:46
int numIFrames
Definition: remux.h:487
int Ppid(void) const
Returns the PCR pid as defined by the current PMT, or 0 if no PCR pid has been detected, yet.
Definition: remux.h:394
bool AtTsStart(void)
Returns true if this payload handler is currently pointing to first byte of a TS packet.
Definition: remux.h:238
#define TS_PID_MASK_HI
Definition: remux.h:38
int Atype(int i) const
Definition: remux.h:406
int64_t PtsDiff(int64_t Pts1, int64_t Pts2)
Returns the difference between two PTS values.
Definition: remux.c:221
uchar * data
Definition: remux.h:422
ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader=NULL)
Definition: remux.c:32
int patVersion
Definition: remux.h:346
int vtype
Definition: remux.h:351
int pmtVersion
Definition: remux.h:295
#define MAXAPIDS
Definition: channels.h:35