vdr  2.4.0
remux.c
Go to the documentation of this file.
1 /*
2  * remux.c: 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.c 4.7 2017/04/29 12:25:09 kls Exp $
8  */
9 
10 #include "remux.h"
11 #include "device.h"
12 #include "libsi/si.h"
13 #include "libsi/section.h"
14 #include "libsi/descriptor.h"
15 #include "recording.h"
16 #include "shutdown.h"
17 #include "tools.h"
18 
19 // Set these to 'true' for debug output:
20 static bool DebugPatPmt = false;
21 static bool DebugFrames = false;
22 
23 #define dbgpatpmt(a...) if (DebugPatPmt) fprintf(stderr, a)
24 #define dbgframes(a...) if (DebugFrames) fprintf(stderr, a)
25 
26 #define MAX_TS_PACKETS_FOR_VIDEO_FRAME_DETECTION 6
27 #define WRN_TS_PACKETS_FOR_VIDEO_FRAME_DETECTION (MAX_TS_PACKETS_FOR_VIDEO_FRAME_DETECTION / 2)
28 #define WRN_TS_PACKETS_FOR_FRAME_DETECTOR (MIN_TS_PACKETS_FOR_FRAME_DETECTOR / 2)
29 
30 #define EMPTY_SCANNER (0xFFFFFFFF)
31 
32 ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader)
33 {
34  if (Count < 7)
35  return phNeedMoreData; // too short
36 
37  if ((Data[6] & 0xC0) == 0x80) { // MPEG 2
38  if (Count < 9)
39  return phNeedMoreData; // too short
40 
41  PesPayloadOffset = 6 + 3 + Data[8];
42  if (Count < PesPayloadOffset)
43  return phNeedMoreData; // too short
44 
45  if (ContinuationHeader)
46  *ContinuationHeader = ((Data[6] == 0x80) && !Data[7] && !Data[8]);
47 
48  return phMPEG2; // MPEG 2
49  }
50 
51  // check for MPEG 1 ...
52  PesPayloadOffset = 6;
53 
54  // skip up to 16 stuffing bytes
55  for (int i = 0; i < 16; i++) {
56  if (Data[PesPayloadOffset] != 0xFF)
57  break;
58 
59  if (Count <= ++PesPayloadOffset)
60  return phNeedMoreData; // too short
61  }
62 
63  // skip STD_buffer_scale/size
64  if ((Data[PesPayloadOffset] & 0xC0) == 0x40) {
65  PesPayloadOffset += 2;
66 
67  if (Count <= PesPayloadOffset)
68  return phNeedMoreData; // too short
69  }
70 
71  if (ContinuationHeader)
72  *ContinuationHeader = false;
73 
74  if ((Data[PesPayloadOffset] & 0xF0) == 0x20) {
75  // skip PTS only
76  PesPayloadOffset += 5;
77  }
78  else if ((Data[PesPayloadOffset] & 0xF0) == 0x30) {
79  // skip PTS and DTS
80  PesPayloadOffset += 10;
81  }
82  else if (Data[PesPayloadOffset] == 0x0F) {
83  // continuation header
85 
86  if (ContinuationHeader)
87  *ContinuationHeader = true;
88  }
89  else
90  return phInvalid; // unknown
91 
92  if (Count < PesPayloadOffset)
93  return phNeedMoreData; // too short
94 
95  return phMPEG1; // MPEG 1
96 }
97 
98 #define VIDEO_STREAM_S 0xE0
99 
100 // --- cRemux ----------------------------------------------------------------
101 
102 void cRemux::SetBrokenLink(uchar *Data, int Length)
103 {
104  int PesPayloadOffset = 0;
105  if (AnalyzePesHeader(Data, Length, PesPayloadOffset) >= phMPEG1 && (Data[3] & 0xF0) == VIDEO_STREAM_S) {
106  for (int i = PesPayloadOffset; i < Length - 7; i++) {
107  if (Data[i] == 0 && Data[i + 1] == 0 && Data[i + 2] == 1 && Data[i + 3] == 0xB8) {
108  if (!(Data[i + 7] & 0x40)) // set flag only if GOP is not closed
109  Data[i + 7] |= 0x20;
110  return;
111  }
112  }
113  dsyslog("SetBrokenLink: no GOP header found in video packet");
114  }
115  else
116  dsyslog("SetBrokenLink: no video packet in frame");
117 }
118 
119 // --- Some TS handling tools ------------------------------------------------
120 
122 {
123  p[1] &= ~TS_PAYLOAD_START;
124  p[3] |= TS_ADAPT_FIELD_EXISTS;
125  p[3] &= ~TS_PAYLOAD_EXISTS;
126  p[4] = TS_SIZE - 5;
127  p[5] = 0x00;
128  memset(p + 6, 0xFF, TS_SIZE - 6);
129 }
130 
131 void TsSetPcr(uchar *p, int64_t Pcr)
132 {
133  if (TsHasAdaptationField(p)) {
134  if (p[4] >= 7 && (p[5] & TS_ADAPT_PCR)) {
135  int64_t b = Pcr / PCRFACTOR;
136  int e = Pcr % PCRFACTOR;
137  p[ 6] = b >> 25;
138  p[ 7] = b >> 17;
139  p[ 8] = b >> 9;
140  p[ 9] = b >> 1;
141  p[10] = (b << 7) | (p[10] & 0x7E) | ((e >> 8) & 0x01);
142  p[11] = e;
143  }
144  }
145 }
146 
147 int TsSync(const uchar *Data, int Length, const char *File, const char *Function, int Line)
148 {
149  int Skipped = 0;
150  while (Length > 0 && (*Data != TS_SYNC_BYTE || Length > TS_SIZE && Data[TS_SIZE] != TS_SYNC_BYTE)) {
151  Data++;
152  Length--;
153  Skipped++;
154  }
155  if (Skipped && File && Function && Line)
156  esyslog("ERROR: skipped %d bytes to sync on start of TS packet at %s/%s(%d)", Skipped, File, Function, Line);
157  return Skipped;
158 }
159 
160 int64_t TsGetPts(const uchar *p, int l)
161 {
162  // Find the first packet with a PTS and use it:
163  while (l > 0) {
164  const uchar *d = p;
165  if (TsPayloadStart(d) && TsGetPayload(&d) && PesHasPts(d))
166  return PesGetPts(d);
167  p += TS_SIZE;
168  l -= TS_SIZE;
169  }
170  return -1;
171 }
172 
173 int64_t TsGetDts(const uchar *p, int l)
174 {
175  // Find the first packet with a DTS and use it:
176  while (l > 0) {
177  const uchar *d = p;
178  if (TsPayloadStart(d) && TsGetPayload(&d) && PesHasDts(d))
179  return PesGetDts(d);
180  p += TS_SIZE;
181  l -= TS_SIZE;
182  }
183  return -1;
184 }
185 
186 void TsSetPts(uchar *p, int l, int64_t Pts)
187 {
188  // Find the first packet with a PTS and use it:
189  while (l > 0) {
190  const uchar *d = p;
191  if (TsPayloadStart(d) && TsGetPayload(&d) && PesHasPts(d)) {
192  PesSetPts(const_cast<uchar *>(d), Pts);
193  return;
194  }
195  p += TS_SIZE;
196  l -= TS_SIZE;
197  }
198 }
199 
200 void TsSetDts(uchar *p, int l, int64_t Dts)
201 {
202  // Find the first packet with a DTS and use it:
203  while (l > 0) {
204  const uchar *d = p;
205  if (TsPayloadStart(d) && TsGetPayload(&d) && PesHasDts(d)) {
206  PesSetDts(const_cast<uchar *>(d), Dts);
207  return;
208  }
209  p += TS_SIZE;
210  l -= TS_SIZE;
211  }
212 }
213 
214 // --- Some PES handling tools -----------------------------------------------
215 
216 void PesSetPts(uchar *p, int64_t Pts)
217 {
218  p[ 9] = ((Pts >> 29) & 0x0E) | (p[9] & 0xF1);
219  p[10] = Pts >> 22;
220  p[11] = ((Pts >> 14) & 0xFE) | 0x01;
221  p[12] = Pts >> 7;
222  p[13] = ((Pts << 1) & 0xFE) | 0x01;
223 }
224 
225 void PesSetDts(uchar *p, int64_t Dts)
226 {
227  p[14] = ((Dts >> 29) & 0x0E) | (p[14] & 0xF1);
228  p[15] = Dts >> 22;
229  p[16] = ((Dts >> 14) & 0xFE) | 0x01;
230  p[17] = Dts >> 7;
231  p[18] = ((Dts << 1) & 0xFE) | 0x01;
232 }
233 
234 int64_t PtsDiff(int64_t Pts1, int64_t Pts2)
235 {
236  int64_t d = Pts2 - Pts1;
237  if (d > MAX33BIT / 2)
238  return d - (MAX33BIT + 1);
239  if (d < -MAX33BIT / 2)
240  return d + (MAX33BIT + 1);
241  return d;
242 }
243 
244 // --- cTsPayload ------------------------------------------------------------
245 
247 {
248  data = NULL;
249  length = 0;
250  pid = -1;
251  Reset();
252 }
253 
254 cTsPayload::cTsPayload(uchar *Data, int Length, int Pid)
255 {
256  Setup(Data, Length, Pid);
257 }
258 
260 {
261  length = index; // triggers EOF
262  return 0x00;
263 }
264 
266 {
267  index = 0;
268  numPacketsPid = 0;
269  numPacketsOther = 0;
270 }
271 
272 void cTsPayload::Setup(uchar *Data, int Length, int Pid)
273 {
274  data = Data;
275  length = Length;
276  pid = Pid >= 0 ? Pid : TsPid(Data);
277  Reset();
278 }
279 
281 {
282  if (!Eof()) {
283  if (index % TS_SIZE == 0) { // encountered the next TS header
284  for (;; index += TS_SIZE) {
285  if (data[index] == TS_SYNC_BYTE && index + TS_SIZE <= length) { // to make sure we are at a TS header start and drop incomplete TS packets at the end
286  uchar *p = data + index;
287  if (TsPid(p) == pid) { // only handle TS packets for the initial PID
289  return SetEof();
290  if (TsHasPayload(p)) {
291  if (index > 0 && TsPayloadStart(p)) // checking index to not skip the very first TS packet
292  return SetEof();
293  index += TsPayloadOffset(p);
294  break;
295  }
296  }
297  else if (TsPid(p) == PATPID)
298  return SetEof(); // caller must see PAT packets in case of index regeneration
299  else
300  numPacketsOther++;
301  }
302  else
303  return SetEof();
304  }
305  }
306  return data[index++];
307  }
308  return 0x00;
309 }
310 
311 bool cTsPayload::SkipBytes(int Bytes)
312 {
313  while (Bytes-- > 0)
314  GetByte();
315  return !Eof();
316 }
317 
319 {
321 }
322 
324 {
325  return index - 1;
326 }
327 
328 void cTsPayload::SetByte(uchar Byte, int Index)
329 {
330  if (Index >= 0 && Index < length)
331  data[Index] = Byte;
332 }
333 
334 bool cTsPayload::Find(uint32_t Code)
335 {
336  int OldIndex = index;
337  int OldNumPacketsPid = numPacketsPid;
338  int OldNumPacketsOther = numPacketsOther;
339  uint32_t Scanner = EMPTY_SCANNER;
340  while (!Eof()) {
341  Scanner = (Scanner << 8) | GetByte();
342  if (Scanner == Code)
343  return true;
344  }
345  index = OldIndex;
346  numPacketsPid = OldNumPacketsPid;
347  numPacketsOther = OldNumPacketsOther;
348  return false;
349 }
350 
351 void cTsPayload::Statistics(void) const
352 {
354  dsyslog("WARNING: required (%d+%d) TS packets to determine frame type", numPacketsOther, numPacketsPid);
356  dsyslog("WARNING: required %d video TS packets to determine frame type", numPacketsPid);
357 }
358 
359 void TsExtendAdaptionField(unsigned char *Packet, int ToLength)
360 {
361  // Hint: ExtenAdaptionField(p, TsPayloadOffset(p) - 4) is a null operation
362 
363  int Offset = TsPayloadOffset(Packet); // First byte after existing adaption field
364 
365  if (ToLength <= 0)
366  {
367  // Remove adaption field
368  Packet[3] = Packet[3] & ~TS_ADAPT_FIELD_EXISTS;
369  return;
370  }
371 
372  // Set adaption field present
373  Packet[3] = Packet[3] | TS_ADAPT_FIELD_EXISTS;
374 
375  // Set new length of adaption field:
376  Packet[4] = ToLength <= TS_SIZE-4 ? ToLength-1 : TS_SIZE-4-1;
377 
378  if (Packet[4] == TS_SIZE-4-1)
379  {
380  // No more payload, remove payload flag
381  Packet[3] = Packet[3] & ~TS_PAYLOAD_EXISTS;
382  }
383 
384  int NewPayload = TsPayloadOffset(Packet); // First byte after new adaption field
385 
386  // Fill new adaption field
387  if (Offset == 4 && Offset < NewPayload)
388  Offset++; // skip adaptation_field_length
389  if (Offset == 5 && Offset < NewPayload)
390  Packet[Offset++] = 0; // various flags set to 0
391  while (Offset < NewPayload)
392  Packet[Offset++] = 0xff; // stuffing byte
393 }
394 
395 // --- cPatPmtGenerator ------------------------------------------------------
396 
398 {
399  numPmtPackets = 0;
400  patCounter = pmtCounter = 0;
401  patVersion = pmtVersion = 0;
402  pmtPid = 0;
403  esInfoLength = NULL;
404  SetChannel(Channel);
405 }
406 
407 void cPatPmtGenerator::IncCounter(int &Counter, uchar *TsPacket)
408 {
409  TsPacket[3] = (TsPacket[3] & 0xF0) | Counter;
410  if (++Counter > 0x0F)
411  Counter = 0x00;
412 }
413 
415 {
416  if (++Version > 0x1F)
417  Version = 0x00;
418 }
419 
421 {
422  if (esInfoLength) {
423  Length += ((*esInfoLength & 0x0F) << 8) | *(esInfoLength + 1);
424  *esInfoLength = 0xF0 | (Length >> 8);
425  *(esInfoLength + 1) = Length;
426  }
427 }
428 
429 int cPatPmtGenerator::MakeStream(uchar *Target, uchar Type, int Pid)
430 {
431  int i = 0;
432  Target[i++] = Type; // stream type
433  Target[i++] = 0xE0 | (Pid >> 8); // dummy (3), pid hi (5)
434  Target[i++] = Pid; // pid lo
435  esInfoLength = &Target[i];
436  Target[i++] = 0xF0; // dummy (4), ES info length hi
437  Target[i++] = 0x00; // ES info length lo
438  return i;
439 }
440 
442 {
443  int i = 0;
444  Target[i++] = Type;
445  Target[i++] = 0x01; // length
446  Target[i++] = 0x00;
447  IncEsInfoLength(i);
448  return i;
449 }
450 
451 int cPatPmtGenerator::MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId)
452 {
453  int i = 0;
454  Target[i++] = SI::SubtitlingDescriptorTag;
455  Target[i++] = 0x08; // length
456  Target[i++] = *Language++;
457  Target[i++] = *Language++;
458  Target[i++] = *Language++;
459  Target[i++] = SubtitlingType;
460  Target[i++] = CompositionPageId >> 8;
461  Target[i++] = CompositionPageId & 0xFF;
462  Target[i++] = AncillaryPageId >> 8;
463  Target[i++] = AncillaryPageId & 0xFF;
464  IncEsInfoLength(i);
465  return i;
466 }
467 
468 int cPatPmtGenerator::MakeLanguageDescriptor(uchar *Target, const char *Language)
469 {
470  int i = 0;
471  Target[i++] = SI::ISO639LanguageDescriptorTag;
472  int Length = i++;
473  Target[Length] = 0x00; // length
474  for (const char *End = Language + strlen(Language); Language < End; ) {
475  Target[i++] = *Language++;
476  Target[i++] = *Language++;
477  Target[i++] = *Language++;
478  Target[i++] = 0x00; // audio type
479  Target[Length] += 0x04; // length
480  if (*Language == '+')
481  Language++;
482  }
483  IncEsInfoLength(i);
484  return i;
485 }
486 
487 int cPatPmtGenerator::MakeCRC(uchar *Target, const uchar *Data, int Length)
488 {
489  int crc = SI::CRC32::crc32((const char *)Data, Length, 0xFFFFFFFF);
490  int i = 0;
491  Target[i++] = crc >> 24;
492  Target[i++] = crc >> 16;
493  Target[i++] = crc >> 8;
494  Target[i++] = crc;
495  return i;
496 }
497 
498 #define P_TSID 0x8008 // pseudo TS ID
499 #define P_PMT_PID 0x0084 // pseudo PMT pid
500 #define MAXPID 0x2000 // the maximum possible number of pids
501 
503 {
504  bool Used[MAXPID] = { false };
505 #define SETPID(p) { if ((p) >= 0 && (p) < MAXPID) Used[p] = true; }
506 #define SETPIDS(l) { const int *p = l; while (*p) { SETPID(*p); p++; } }
507  SETPID(Channel->Vpid());
508  SETPID(Channel->Ppid());
509  SETPID(Channel->Tpid());
510  SETPIDS(Channel->Apids());
511  SETPIDS(Channel->Dpids());
512  SETPIDS(Channel->Spids());
513  for (pmtPid = P_PMT_PID; Used[pmtPid]; pmtPid++)
514  ;
515 }
516 
518 {
519  memset(pat, 0xFF, sizeof(pat));
520  uchar *p = pat;
521  int i = 0;
522  p[i++] = TS_SYNC_BYTE; // TS indicator
523  p[i++] = TS_PAYLOAD_START | (PATPID >> 8); // flags (3), pid hi (5)
524  p[i++] = PATPID & 0xFF; // pid lo
525  p[i++] = 0x10; // flags (4), continuity counter (4)
526  p[i++] = 0x00; // pointer field (payload unit start indicator is set)
527  int PayloadStart = i;
528  p[i++] = 0x00; // table id
529  p[i++] = 0xB0; // section syntax indicator (1), dummy (3), section length hi (4)
530  int SectionLength = i;
531  p[i++] = 0x00; // section length lo (filled in later)
532  p[i++] = P_TSID >> 8; // TS id hi
533  p[i++] = P_TSID & 0xFF; // TS id lo
534  p[i++] = 0xC1 | (patVersion << 1); // dummy (2), version number (5), current/next indicator (1)
535  p[i++] = 0x00; // section number
536  p[i++] = 0x00; // last section number
537  p[i++] = pmtPid >> 8; // program number hi
538  p[i++] = pmtPid & 0xFF; // program number lo
539  p[i++] = 0xE0 | (pmtPid >> 8); // dummy (3), PMT pid hi (5)
540  p[i++] = pmtPid & 0xFF; // PMT pid lo
541  pat[SectionLength] = i - SectionLength - 1 + 4; // -1 = SectionLength storage, +4 = length of CRC
542  MakeCRC(pat + i, pat + PayloadStart, i - PayloadStart);
544 }
545 
547 {
548  // generate the complete PMT section:
549  uchar buf[MAX_SECTION_SIZE];
550  memset(buf, 0xFF, sizeof(buf));
551  numPmtPackets = 0;
552  if (Channel) {
553  int Vpid = Channel->Vpid();
554  int Ppid = Channel->Ppid();
555  uchar *p = buf;
556  int i = 0;
557  p[i++] = 0x02; // table id
558  int SectionLength = i;
559  p[i++] = 0xB0; // section syntax indicator (1), dummy (3), section length hi (4)
560  p[i++] = 0x00; // section length lo (filled in later)
561  p[i++] = pmtPid >> 8; // program number hi
562  p[i++] = pmtPid & 0xFF; // program number lo
563  p[i++] = 0xC1 | (pmtVersion << 1); // dummy (2), version number (5), current/next indicator (1)
564  p[i++] = 0x00; // section number
565  p[i++] = 0x00; // last section number
566  p[i++] = 0xE0 | (Ppid >> 8); // dummy (3), PCR pid hi (5)
567  p[i++] = Ppid; // PCR pid lo
568  p[i++] = 0xF0; // dummy (4), program info length hi (4)
569  p[i++] = 0x00; // program info length lo
570 
571  if (Vpid)
572  i += MakeStream(buf + i, Channel->Vtype(), Vpid);
573  for (int n = 0; Channel->Apid(n); n++) {
574  i += MakeStream(buf + i, Channel->Atype(n), Channel->Apid(n));
575  const char *Alang = Channel->Alang(n);
576  i += MakeLanguageDescriptor(buf + i, Alang);
577  }
578  for (int n = 0; Channel->Dpid(n); n++) {
579  i += MakeStream(buf + i, 0x06, Channel->Dpid(n));
580  i += MakeAC3Descriptor(buf + i, Channel->Dtype(n));
581  i += MakeLanguageDescriptor(buf + i, Channel->Dlang(n));
582  }
583  for (int n = 0; Channel->Spid(n); n++) {
584  i += MakeStream(buf + i, 0x06, Channel->Spid(n));
585  i += MakeSubtitlingDescriptor(buf + i, Channel->Slang(n), Channel->SubtitlingType(n), Channel->CompositionPageId(n), Channel->AncillaryPageId(n));
586  }
587 
588  int sl = i - SectionLength - 2 + 4; // -2 = SectionLength storage, +4 = length of CRC
589  buf[SectionLength] |= (sl >> 8) & 0x0F;
590  buf[SectionLength + 1] = sl;
591  MakeCRC(buf + i, buf, i);
592  // split the PMT section into several TS packets:
593  uchar *q = buf;
594  bool pusi = true;
595  while (i > 0) {
596  uchar *p = pmt[numPmtPackets++];
597  int j = 0;
598  p[j++] = TS_SYNC_BYTE; // TS indicator
599  p[j++] = (pusi ? TS_PAYLOAD_START : 0x00) | (pmtPid >> 8); // flags (3), pid hi (5)
600  p[j++] = pmtPid & 0xFF; // pid lo
601  p[j++] = 0x10; // flags (4), continuity counter (4)
602  if (pusi) {
603  p[j++] = 0x00; // pointer field (payload unit start indicator is set)
604  pusi = false;
605  }
606  int l = TS_SIZE - j;
607  memcpy(p + j, q, l);
608  q += l;
609  i -= l;
610  }
612  }
613 }
614 
615 void cPatPmtGenerator::SetVersions(int PatVersion, int PmtVersion)
616 {
617  patVersion = PatVersion & 0x1F;
618  pmtVersion = PmtVersion & 0x1F;
619 }
620 
622 {
623  if (Channel) {
624  GeneratePmtPid(Channel);
625  GeneratePat();
626  GeneratePmt(Channel);
627  }
628 }
629 
631 {
633  return pat;
634 }
635 
637 {
638  if (Index < numPmtPackets) {
639  IncCounter(pmtCounter, pmt[Index]);
640  return pmt[Index++];
641  }
642  return NULL;
643 }
644 
645 // --- cPatPmtParser ---------------------------------------------------------
646 
647 cPatPmtParser::cPatPmtParser(bool UpdatePrimaryDevice)
648 {
649  updatePrimaryDevice = UpdatePrimaryDevice;
650  Reset();
651 }
652 
654 {
655  completed = false;
656  pmtSize = 0;
657  patVersion = pmtVersion = -1;
658  pmtPids[0] = 0;
659  vpid = vtype = 0;
660  ppid = 0;
661 }
662 
663 void cPatPmtParser::ParsePat(const uchar *Data, int Length)
664 {
665  // Unpack the TS packet:
666  int PayloadOffset = TsPayloadOffset(Data);
667  Data += PayloadOffset;
668  Length -= PayloadOffset;
669  // The PAT is always assumed to fit into a single TS packet
670  if ((Length -= Data[0] + 1) <= 0)
671  return;
672  Data += Data[0] + 1; // process pointer_field
673  SI::PAT Pat(Data, false);
674  if (Pat.CheckCRCAndParse()) {
675  dbgpatpmt("PAT: TSid = %d, c/n = %d, v = %d, s = %d, ls = %d\n", Pat.getTransportStreamId(), Pat.getCurrentNextIndicator(), Pat.getVersionNumber(), Pat.getSectionNumber(), Pat.getLastSectionNumber());
676  if (patVersion == Pat.getVersionNumber())
677  return;
678  int NumPmtPids = 0;
679  SI::PAT::Association assoc;
680  for (SI::Loop::Iterator it; Pat.associationLoop.getNext(assoc, it); ) {
681  dbgpatpmt(" isNITPid = %d\n", assoc.isNITPid());
682  if (!assoc.isNITPid()) {
683  if (NumPmtPids <= MAX_PMT_PIDS)
684  pmtPids[NumPmtPids++] = assoc.getPid();
685  dbgpatpmt(" service id = %d, pid = %d\n", assoc.getServiceId(), assoc.getPid());
686  }
687  }
688  pmtPids[NumPmtPids] = 0;
690  }
691  else
692  esyslog("ERROR: can't parse PAT");
693 }
694 
695 void cPatPmtParser::ParsePmt(const uchar *Data, int Length)
696 {
697  // Unpack the TS packet:
698  bool PayloadStart = TsPayloadStart(Data);
699  int PayloadOffset = TsPayloadOffset(Data);
700  Data += PayloadOffset;
701  Length -= PayloadOffset;
702  // The PMT may extend over several TS packets, so we need to assemble them
703  if (PayloadStart) {
704  pmtSize = 0;
705  if ((Length -= Data[0] + 1) <= 0)
706  return;
707  Data += Data[0] + 1; // this is the first packet
708  if (SectionLength(Data, Length) > Length) {
709  if (Length <= int(sizeof(pmt))) {
710  memcpy(pmt, Data, Length);
711  pmtSize = Length;
712  }
713  else
714  esyslog("ERROR: PMT packet length too big (%d byte)!", Length);
715  return;
716  }
717  // the packet contains the entire PMT section, so we run into the actual parsing
718  }
719  else if (pmtSize > 0) {
720  // this is a following packet, so we add it to the pmt storage
721  if (Length <= int(sizeof(pmt)) - pmtSize) {
722  memcpy(pmt + pmtSize, Data, Length);
723  pmtSize += Length;
724  }
725  else {
726  esyslog("ERROR: PMT section length too big (%d byte)!", pmtSize + Length);
727  pmtSize = 0;
728  }
730  return; // more packets to come
731  // the PMT section is now complete, so we run into the actual parsing
732  Data = pmt;
733  }
734  else
735  return; // fragment of broken packet - ignore
736  SI::PMT Pmt(Data, false);
737  if (Pmt.CheckCRCAndParse()) {
738  dbgpatpmt("PMT: sid = %d, c/n = %d, v = %d, s = %d, ls = %d\n", Pmt.getServiceId(), Pmt.getCurrentNextIndicator(), Pmt.getVersionNumber(), Pmt.getSectionNumber(), Pmt.getLastSectionNumber());
739  dbgpatpmt(" pcr = %d\n", Pmt.getPCRPid());
740  if (pmtVersion == Pmt.getVersionNumber())
741  return;
744  int NumApids = 0;
745  int NumDpids = 0;
746  int NumSpids = 0;
747  vpid = vtype = 0;
748  ppid = 0;
749  apids[0] = 0;
750  dpids[0] = 0;
751  spids[0] = 0;
752  atypes[0] = 0;
753  dtypes[0] = 0;
754  SI::PMT::Stream stream;
755  for (SI::Loop::Iterator it; Pmt.streamLoop.getNext(stream, it); ) {
756  dbgpatpmt(" stream type = %02X, pid = %d", stream.getStreamType(), stream.getPid());
757  switch (stream.getStreamType()) {
758  case 0x01: // STREAMTYPE_11172_VIDEO
759  case 0x02: // STREAMTYPE_13818_VIDEO
760  case 0x1B: // H.264
761  case 0x24: // H.265
762  vpid = stream.getPid();
763  vtype = stream.getStreamType();
764  ppid = Pmt.getPCRPid();
765  break;
766  case 0x03: // STREAMTYPE_11172_AUDIO
767  case 0x04: // STREAMTYPE_13818_AUDIO
768  case 0x0F: // ISO/IEC 13818-7 Audio with ADTS transport syntax
769  case 0x11: // ISO/IEC 14496-3 Audio with LATM transport syntax
770  {
771  if (NumApids < MAXAPIDS) {
772  apids[NumApids] = stream.getPid();
773  atypes[NumApids] = stream.getStreamType();
774  *alangs[NumApids] = 0;
775  SI::Descriptor *d;
776  for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
777  switch (d->getDescriptorTag()) {
781  char *s = alangs[NumApids];
782  int n = 0;
783  for (SI::Loop::Iterator it; ld->languageLoop.getNext(l, it); ) {
784  if (*ld->languageCode != '-') { // some use "---" to indicate "none"
785  dbgpatpmt(" '%s'", l.languageCode);
786  if (n > 0)
787  *s++ = '+';
789  s += strlen(s);
790  if (n++ > 1)
791  break;
792  }
793  }
794  }
795  break;
796  default: ;
797  }
798  delete d;
799  }
801  cDevice::PrimaryDevice()->SetAvailableTrack(ttAudio, NumApids, apids[NumApids], alangs[NumApids]);
802  NumApids++;
803  apids[NumApids] = 0;
804  }
805  }
806  break;
807  case 0x06: // STREAMTYPE_13818_PES_PRIVATE
808  {
809  int dpid = 0;
810  int dtype = 0;
811  char lang[MAXLANGCODE1] = "";
812  SI::Descriptor *d;
813  for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
814  switch (d->getDescriptorTag()) {
817  dbgpatpmt(" AC3");
818  dpid = stream.getPid();
819  dtype = d->getDescriptorTag();
820  break;
822  dbgpatpmt(" subtitling");
823  if (NumSpids < MAXSPIDS) {
824  spids[NumSpids] = stream.getPid();
825  *slangs[NumSpids] = 0;
826  subtitlingTypes[NumSpids] = 0;
827  compositionPageIds[NumSpids] = 0;
828  ancillaryPageIds[NumSpids] = 0;
831  char *s = slangs[NumSpids];
832  int n = 0;
833  for (SI::Loop::Iterator it; sd->subtitlingLoop.getNext(sub, it); ) {
834  if (sub.languageCode[0]) {
835  dbgpatpmt(" '%s'", sub.languageCode);
836  subtitlingTypes[NumSpids] = sub.getSubtitlingType();
837  compositionPageIds[NumSpids] = sub.getCompositionPageId();
838  ancillaryPageIds[NumSpids] = sub.getAncillaryPageId();
839  if (n > 0)
840  *s++ = '+';
842  s += strlen(s);
843  if (n++ > 1)
844  break;
845  }
846  }
848  cDevice::PrimaryDevice()->SetAvailableTrack(ttSubtitle, NumSpids, spids[NumSpids], slangs[NumSpids]);
849  NumSpids++;
850  spids[NumSpids] = 0;
851  }
852  break;
855  dbgpatpmt(" '%s'", ld->languageCode);
857  }
858  break;
859  default: ;
860  }
861  delete d;
862  }
863  if (dpid) {
864  if (NumDpids < MAXDPIDS) {
865  dpids[NumDpids] = dpid;
866  dtypes[NumDpids] = dtype;
867  strn0cpy(dlangs[NumDpids], lang, sizeof(dlangs[NumDpids]));
869  cDevice::PrimaryDevice()->SetAvailableTrack(ttDolby, NumDpids, dpid, lang);
870  NumDpids++;
871  dpids[NumDpids] = 0;
872  }
873  }
874  }
875  break;
876  case 0x81: // STREAMTYPE_USER_PRIVATE - AC3 audio for ATSC and BD
877  case 0x82: // STREAMTYPE_USER_PRIVATE - DTS audio for BD
878  {
879  dbgpatpmt(" %s",
880  stream.getStreamType() == 0x81 ? "AC3" :
881  stream.getStreamType() == 0x82 ? "DTS" : "");
882  char lang[MAXLANGCODE1] = { 0 };
883  SI::Descriptor *d;
884  for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
885  switch (d->getDescriptorTag()) {
888  dbgpatpmt(" '%s'", ld->languageCode);
890  }
891  break;
892  default: ;
893  }
894  delete d;
895  }
896  if (NumDpids < MAXDPIDS) {
897  dpids[NumDpids] = stream.getPid();
898  dtypes[NumDpids] = SI::AC3DescriptorTag;
899  strn0cpy(dlangs[NumDpids], lang, sizeof(dlangs[NumDpids]));
901  cDevice::PrimaryDevice()->SetAvailableTrack(ttDolby, NumDpids, stream.getPid(), lang);
902  NumDpids++;
903  dpids[NumDpids] = 0;
904  }
905  }
906  break;
907  case 0x90: // PGS subtitles for BD
908  {
909  dbgpatpmt(" subtitling");
910  char lang[MAXLANGCODE1] = { 0 };
911  SI::Descriptor *d;
912  for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
913  switch (d->getDescriptorTag()) {
916  dbgpatpmt(" '%s'", ld->languageCode);
918  if (NumSpids < MAXSPIDS) {
919  spids[NumSpids] = stream.getPid();
920  *slangs[NumSpids] = 0;
921  subtitlingTypes[NumSpids] = 0;
922  compositionPageIds[NumSpids] = 0;
923  ancillaryPageIds[NumSpids] = 0;
925  cDevice::PrimaryDevice()->SetAvailableTrack(ttSubtitle, NumSpids, stream.getPid(), lang);
926  NumSpids++;
927  spids[NumSpids] = 0;
928  }
929  }
930  break;
931  default: ;
932  }
933  delete d;
934  }
935  }
936  break;
937  default: ;
938  }
939  dbgpatpmt("\n");
940  if (updatePrimaryDevice) {
943  }
944  }
946  completed = true;
947  }
948  else
949  esyslog("ERROR: can't parse PMT");
950  pmtSize = 0;
951 }
952 
953 bool cPatPmtParser::ParsePatPmt(const uchar *Data, int Length)
954 {
955  while (Length >= TS_SIZE) {
956  if (*Data != TS_SYNC_BYTE)
957  break; // just for safety
958  int Pid = TsPid(Data);
959  if (Pid == PATPID)
960  ParsePat(Data, TS_SIZE);
961  else if (IsPmtPid(Pid)) {
962  ParsePmt(Data, TS_SIZE);
963  if (patVersion >= 0 && pmtVersion >= 0)
964  return true;
965  }
966  Data += TS_SIZE;
967  Length -= TS_SIZE;
968  }
969  return false;
970 }
971 
972 bool cPatPmtParser::GetVersions(int &PatVersion, int &PmtVersion) const
973 {
974  PatVersion = patVersion;
975  PmtVersion = pmtVersion;
976  return patVersion >= 0 && pmtVersion >= 0;
977 }
978 
979 // --- cEitGenerator ---------------------------------------------------------
980 
982 {
983  counter = 0;
984  version = 0;
985  if (Sid)
986  Generate(Sid);
987 }
988 
989 uint16_t cEitGenerator::YMDtoMJD(int Y, int M, int D)
990 {
991  int L = (M < 3) ? 1 : 0;
992  return 14956 + D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
993 }
994 
996 {
998  *p++ = 0x04; // descriptor length
999  *p++ = 'D'; // country code
1000  *p++ = 'E';
1001  *p++ = 'U';
1002  *p++ = ParentalRating;
1003  return p;
1004 }
1005 
1007 {
1008  uchar *PayloadStart;
1009  uchar *SectionStart;
1010  uchar *DescriptorsStart;
1011  memset(eit, 0xFF, sizeof(eit));
1012  struct tm tm_r;
1013  time_t t = time(NULL) - 3600; // let's have the event start one hour in the past
1014  tm *tm = localtime_r(&t, &tm_r);
1015  uint16_t MJD = YMDtoMJD(tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
1016  uchar *p = eit;
1017  // TS header:
1018  *p++ = TS_SYNC_BYTE;
1019  *p++ = TS_PAYLOAD_START;
1020  *p++ = EITPID;
1021  *p++ = 0x10 | (counter++ & 0x0F); // continuity counter
1022  *p++ = 0x00; // pointer field (payload unit start indicator is set)
1023  // payload:
1024  PayloadStart = p;
1025  *p++ = 0x4E; // TID present/following event on this transponder
1026  *p++ = 0xF0;
1027  *p++ = 0x00; // section length
1028  SectionStart = p;
1029  *p++ = Sid >> 8;
1030  *p++ = Sid & 0xFF;
1031  *p++ = 0xC1 | (version << 1);
1032  *p++ = 0x00; // section number
1033  *p++ = 0x00; // last section number
1034  *p++ = 0x00; // transport stream id
1035  *p++ = 0x00; // ...
1036  *p++ = 0x00; // original network id
1037  *p++ = 0x00; // ...
1038  *p++ = 0x00; // segment last section number
1039  *p++ = 0x4E; // last table id
1040  *p++ = 0x00; // event id
1041  *p++ = 0x01; // ...
1042  *p++ = MJD >> 8; // start time
1043  *p++ = MJD & 0xFF; // ...
1044  *p++ = tm->tm_hour; // ...
1045  *p++ = tm->tm_min; // ...
1046  *p++ = tm->tm_sec; // ...
1047  *p++ = 0x24; // duration (one day, should cover everything)
1048  *p++ = 0x00; // ...
1049  *p++ = 0x00; // ...
1050  *p++ = 0x90; // running status, free/CA mode
1051  *p++ = 0x00; // descriptors loop length
1052  DescriptorsStart = p;
1054  // fill in lengths:
1055  *(SectionStart - 1) = p - SectionStart + 4; // +4 = length of CRC
1056  *(DescriptorsStart - 1) = p - DescriptorsStart;
1057  // checksum
1058  int crc = SI::CRC32::crc32((char *)PayloadStart, p - PayloadStart, 0xFFFFFFFF);
1059  *p++ = crc >> 24;
1060  *p++ = crc >> 16;
1061  *p++ = crc >> 8;
1062  *p++ = crc;
1063  return eit;
1064 }
1065 
1066 // --- cTsToPes --------------------------------------------------------------
1067 
1069 {
1070  data = NULL;
1071  size = 0;
1072  Reset();
1073 }
1074 
1076 {
1077  free(data);
1078 }
1079 
1080 void cTsToPes::PutTs(const uchar *Data, int Length)
1081 {
1082  if (TsError(Data)) {
1083  Reset();
1084  return; // ignore packets with TEI set, and drop any PES data collected so far
1085  }
1086  if (TsPayloadStart(Data))
1087  Reset();
1088  else if (!size)
1089  return; // skip everything before the first payload start
1090  Length = TsGetPayload(&Data);
1091  if (length + Length > size) {
1092  int NewSize = max(KILOBYTE(2), length + Length);
1093  if (uchar *NewData = (uchar *)realloc(data, NewSize)) {
1094  data = NewData;
1095  size = NewSize;
1096  }
1097  else {
1098  esyslog("ERROR: out of memory");
1099  Reset();
1100  return;
1101  }
1102  }
1103  memcpy(data + length, Data, Length);
1104  length += Length;
1105 }
1106 
1107 #define MAXPESLENGTH 0xFFF0
1108 
1109 const uchar *cTsToPes::GetPes(int &Length)
1110 {
1111  if (repeatLast) {
1112  repeatLast = false;
1113  Length = lastLength;
1114  return lastData;
1115  }
1116  if (offset < length && PesLongEnough(length)) {
1117  if (!PesHasLength(data)) // this is a video PES packet with undefined length
1118  offset = 6; // trigger setting PES length for initial slice
1119  if (offset) {
1120  uchar *p = data + offset - 6;
1121  if (p != data) {
1122  p -= 3;
1123  if (p < data) {
1124  Reset();
1125  return NULL;
1126  }
1127  memmove(p, data, 4);
1128  }
1129  int l = min(length - offset, MAXPESLENGTH);
1130  offset += l;
1131  if (p != data) {
1132  l += 3;
1133  p[6] = 0x80;
1134  p[7] = 0x00;
1135  p[8] = 0x00;
1136  }
1137  p[4] = l / 256;
1138  p[5] = l & 0xFF;
1139  Length = l + 6;
1140  lastLength = Length;
1141  lastData = p;
1142  return p;
1143  }
1144  else {
1145  Length = PesLength(data);
1146  if (Length <= length) {
1147  offset = Length; // to make sure we break out in case of garbage data
1148  lastLength = Length;
1149  lastData = data;
1150  return data;
1151  }
1152  }
1153  }
1154  return NULL;
1155 }
1156 
1158 {
1159  repeatLast = true;
1160 }
1161 
1163 {
1164  length = offset = 0;
1165  lastData = NULL;
1166  lastLength = 0;
1167  repeatLast = false;
1168 }
1169 
1170 // --- Some helper functions for debugging -----------------------------------
1171 
1172 void BlockDump(const char *Name, const u_char *Data, int Length)
1173 {
1174  printf("--- %s\n", Name);
1175  for (int i = 0; i < Length; i++) {
1176  if (i && (i % 16) == 0)
1177  printf("\n");
1178  printf(" %02X", Data[i]);
1179  }
1180  printf("\n");
1181 }
1182 
1183 void TsDump(const char *Name, const u_char *Data, int Length)
1184 {
1185  printf("%s: %04X", Name, Length);
1186  int n = min(Length, 20);
1187  for (int i = 0; i < n; i++)
1188  printf(" %02X", Data[i]);
1189  if (n < Length) {
1190  printf(" ...");
1191  n = max(n, Length - 10);
1192  for (n = max(n, Length - 10); n < Length; n++)
1193  printf(" %02X", Data[n]);
1194  }
1195  printf("\n");
1196 }
1197 
1198 void PesDump(const char *Name, const u_char *Data, int Length)
1199 {
1200  TsDump(Name, Data, Length);
1201 }
1202 
1203 // --- cFrameParser ----------------------------------------------------------
1204 
1206 protected:
1207  bool debug;
1208  bool newFrame;
1211 public:
1212  cFrameParser(void);
1213  virtual ~cFrameParser() {};
1214  virtual int Parse(const uchar *Data, int Length, int Pid) = 0;
1221  void SetDebug(bool Debug) { debug = Debug; }
1222  bool NewFrame(void) { return newFrame; }
1223  bool IndependentFrame(void) { return independentFrame; }
1225  };
1226 
1228 {
1229  debug = true;
1230  newFrame = false;
1231  independentFrame = false;
1233 }
1234 
1235 // --- cAudioParser ----------------------------------------------------------
1236 
1237 class cAudioParser : public cFrameParser {
1238 public:
1239  cAudioParser(void);
1240  virtual int Parse(const uchar *Data, int Length, int Pid);
1241  };
1242 
1244 {
1245 }
1246 
1247 int cAudioParser::Parse(const uchar *Data, int Length, int Pid)
1248 {
1249  if (TsPayloadStart(Data)) {
1250  newFrame = independentFrame = true;
1251  if (debug)
1252  dbgframes("/");
1253  }
1254  else
1255  newFrame = independentFrame = false;
1256  return TS_SIZE;
1257 }
1258 
1259 // --- cMpeg2Parser ----------------------------------------------------------
1260 
1261 class cMpeg2Parser : public cFrameParser {
1262 private:
1263  uint32_t scanner;
1266 public:
1267  cMpeg2Parser(void);
1268  virtual int Parse(const uchar *Data, int Length, int Pid);
1269  };
1270 
1272 {
1274  seenIndependentFrame = false;
1275  lastIFrameTemporalReference = -1; // invalid
1276 }
1277 
1278 int cMpeg2Parser::Parse(const uchar *Data, int Length, int Pid)
1279 {
1280  newFrame = independentFrame = false;
1281  bool SeenPayloadStart = false;
1282  cTsPayload tsPayload(const_cast<uchar *>(Data), Length, Pid);
1283  if (TsPayloadStart(Data)) {
1284  SeenPayloadStart = true;
1285  tsPayload.SkipPesHeader();
1287  if (debug && seenIndependentFrame)
1288  dbgframes("/");
1289  }
1290  uint32_t OldScanner = scanner; // need to remember it in case of multiple frames per payload
1291  for (;;) {
1292  if (!SeenPayloadStart && tsPayload.AtTsStart())
1293  OldScanner = scanner;
1294  scanner = (scanner << 8) | tsPayload.GetByte();
1295  if (scanner == 0x00000100) { // Picture Start Code
1296  if (!SeenPayloadStart && tsPayload.GetLastIndex() > TS_SIZE) {
1297  scanner = OldScanner;
1298  return tsPayload.Used() - TS_SIZE;
1299  }
1300  uchar b1 = tsPayload.GetByte();
1301  uchar b2 = tsPayload.GetByte();
1302  int TemporalReference = (b1 << 2 ) + ((b2 & 0xC0) >> 6);
1303  uchar FrameType = (b2 >> 3) & 0x07;
1304  if (tsPayload.Find(0x000001B5)) { // Extension start code
1305  if (((tsPayload.GetByte() & 0xF0) >> 4) == 0x08) { // Picture coding extension
1306  tsPayload.GetByte();
1307  uchar PictureStructure = tsPayload.GetByte() & 0x03;
1308  if (PictureStructure == 0x02) // bottom field
1309  break;
1310  }
1311  }
1312  newFrame = true;
1313  independentFrame = FrameType == 1; // I-Frame
1314  if (independentFrame) {
1315  if (lastIFrameTemporalReference >= 0)
1317  lastIFrameTemporalReference = TemporalReference;
1318  }
1319  if (debug) {
1321  if (seenIndependentFrame) {
1322  static const char FrameTypes[] = "?IPBD???";
1323  dbgframes("%c", FrameTypes[FrameType]);
1324  }
1325  }
1326  tsPayload.Statistics();
1327  break;
1328  }
1329  if (tsPayload.AtPayloadStart() // stop at any new payload start to have the buffer refilled if necessary
1330  || tsPayload.Eof()) // or if we're out of data
1331  break;
1332  }
1333  return tsPayload.Used();
1334 }
1335 
1336 // --- cH264Parser -----------------------------------------------------------
1337 
1338 class cH264Parser : public cFrameParser {
1339 private:
1345  };
1346  uchar byte; // holds the current byte value in case of bitwise access
1347  int bit; // the bit index into the current byte (-1 if we're not in bit reading mode)
1348  int zeroBytes; // the number of consecutive zero bytes (to detect 0x000003)
1349  // Identifiers written in '_' notation as in "ITU-T H.264":
1353 protected:
1355  uint32_t scanner;
1358  uchar GetByte(bool Raw = false);
1362  uchar GetBit(void);
1363  uint32_t GetBits(int Bits);
1364  uint32_t GetGolombUe(void);
1365  int32_t GetGolombSe(void);
1366  void ParseAccessUnitDelimiter(void);
1367  void ParseSequenceParameterSet(void);
1368  void ParseSliceHeader(void);
1369 public:
1370  cH264Parser(void);
1374  virtual int Parse(const uchar *Data, int Length, int Pid);
1375  };
1376 
1378 {
1379  byte = 0;
1380  bit = -1;
1381  zeroBytes = 0;
1384  log2_max_frame_num = 0;
1385  frame_mbs_only_flag = false;
1386  gotAccessUnitDelimiter = false;
1387  gotSequenceParameterSet = false;
1388 }
1389 
1391 {
1392  uchar b = tsPayload.GetByte();
1393  if (!Raw) {
1394  // If we encounter the byte sequence 0x000003, we need to skip the 0x03:
1395  if (b == 0x00)
1396  zeroBytes++;
1397  else {
1398  if (b == 0x03 && zeroBytes >= 2)
1399  b = tsPayload.GetByte();
1400  zeroBytes = 0;
1401  }
1402  }
1403  else
1404  zeroBytes = 0;
1405  bit = -1;
1406  return b;
1407 }
1408 
1410 {
1411  if (bit < 0) {
1412  byte = GetByte();
1413  bit = 7;
1414  }
1415  return (byte & (1 << bit--)) ? 1 : 0;
1416 }
1417 
1418 uint32_t cH264Parser::GetBits(int Bits)
1419 {
1420  uint32_t b = 0;
1421  while (Bits--)
1422  b |= GetBit() << Bits;
1423  return b;
1424 }
1425 
1427 {
1428  int z = -1;
1429  for (int b = 0; !b && z < 32; z++) // limiting z to no get stuck if GetBit() always returns 0
1430  b = GetBit();
1431  return (1 << z) - 1 + GetBits(z);
1432 }
1433 
1435 {
1436  uint32_t v = GetGolombUe();
1437  if (v) {
1438  if ((v & 0x01) != 0)
1439  return (v + 1) / 2; // fails for v == 0xFFFFFFFF, but that will probably never happen
1440  else
1441  return -int32_t(v / 2);
1442  }
1443  return v;
1444 }
1445 
1446 int cH264Parser::Parse(const uchar *Data, int Length, int Pid)
1447 {
1448  newFrame = independentFrame = false;
1449  tsPayload.Setup(const_cast<uchar *>(Data), Length, Pid);
1450  if (TsPayloadStart(Data)) {
1453  if (debug && gotSequenceParameterSet) {
1454  dbgframes("/");
1455  }
1456  }
1457  for (;;) {
1458  scanner = (scanner << 8) | GetByte(true);
1459  if ((scanner & 0xFFFFFF00) == 0x00000100) { // NAL unit start
1460  uchar NalUnitType = scanner & 0x1F;
1461  switch (NalUnitType) {
1463  gotAccessUnitDelimiter = true;
1464  break;
1467  gotSequenceParameterSet = true;
1468  }
1469  break;
1470  case nutCodedSliceNonIdr:
1472  ParseSliceHeader();
1473  gotAccessUnitDelimiter = false;
1474  if (newFrame)
1476  return tsPayload.Used();
1477  }
1478  break;
1479  default: ;
1480  }
1481  }
1482  if (tsPayload.AtPayloadStart() // stop at any new payload start to have the buffer refilled if necessary
1483  || tsPayload.Eof()) // or if we're out of data
1484  break;
1485  }
1486  return tsPayload.Used();
1487 }
1488 
1490 {
1492  dbgframes("A");
1493  GetByte(); // primary_pic_type
1494 }
1495 
1497 {
1498  uchar profile_idc = GetByte(); // profile_idc
1499  GetByte(); // constraint_set[0-5]_flags, reserved_zero_2bits
1500  GetByte(); // level_idc
1501  GetGolombUe(); // seq_parameter_set_id
1502  if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86 || profile_idc ==118 || profile_idc == 128) {
1503  int chroma_format_idc = GetGolombUe(); // chroma_format_idc
1504  if (chroma_format_idc == 3)
1506  GetGolombUe(); // bit_depth_luma_minus8
1507  GetGolombUe(); // bit_depth_chroma_minus8
1508  GetBit(); // qpprime_y_zero_transform_bypass_flag
1509  if (GetBit()) { // seq_scaling_matrix_present_flag
1510  for (int i = 0; i < ((chroma_format_idc != 3) ? 8 : 12); i++) {
1511  if (GetBit()) { // seq_scaling_list_present_flag
1512  int SizeOfScalingList = (i < 6) ? 16 : 64;
1513  int LastScale = 8;
1514  int NextScale = 8;
1515  for (int j = 0; j < SizeOfScalingList; j++) {
1516  if (NextScale)
1517  NextScale = (LastScale + GetGolombSe() + 256) % 256; // delta_scale
1518  if (NextScale)
1519  LastScale = NextScale;
1520  }
1521  }
1522  }
1523  }
1524  }
1525  log2_max_frame_num = GetGolombUe() + 4; // log2_max_frame_num_minus4
1526  int pic_order_cnt_type = GetGolombUe(); // pic_order_cnt_type
1527  if (pic_order_cnt_type == 0)
1528  GetGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
1529  else if (pic_order_cnt_type == 1) {
1530  GetBit(); // delta_pic_order_always_zero_flag
1531  GetGolombSe(); // offset_for_non_ref_pic
1532  GetGolombSe(); // offset_for_top_to_bottom_field
1533  for (int i = GetGolombUe(); i--; ) // num_ref_frames_in_pic_order_cnt_cycle
1534  GetGolombSe(); // offset_for_ref_frame
1535  }
1536  GetGolombUe(); // max_num_ref_frames
1537  GetBit(); // gaps_in_frame_num_value_allowed_flag
1538  GetGolombUe(); // pic_width_in_mbs_minus1
1539  GetGolombUe(); // pic_height_in_map_units_minus1
1540  frame_mbs_only_flag = GetBit(); // frame_mbs_only_flag
1541  if (debug) {
1543  dbgframes("A"); // just for completeness
1544  dbgframes(frame_mbs_only_flag ? "S" : "s");
1545  }
1546 }
1547 
1549 {
1550  newFrame = true;
1551  GetGolombUe(); // first_mb_in_slice
1552  int slice_type = GetGolombUe(); // slice_type, 0 = P, 1 = B, 2 = I, 3 = SP, 4 = SI
1553  independentFrame = (slice_type % 5) == 2;
1554  if (debug) {
1555  static const char SliceTypes[] = "PBIpi";
1556  dbgframes("%c", SliceTypes[slice_type % 5]);
1557  }
1558  if (frame_mbs_only_flag)
1559  return; // don't need the rest - a frame is complete
1560  GetGolombUe(); // pic_parameter_set_id
1562  GetBits(2); // colour_plane_id
1563  GetBits(log2_max_frame_num); // frame_num
1564  if (!frame_mbs_only_flag) {
1565  if (GetBit()) // field_pic_flag
1566  newFrame = !GetBit(); // bottom_field_flag
1567  if (debug)
1568  dbgframes(newFrame ? "t" : "b");
1569  }
1570 }
1571 
1572 // --- cH265Parser -----------------------------------------------------------
1573 
1574 class cH265Parser : public cH264Parser {
1575 private:
1606  };
1607 public:
1608  cH265Parser(void);
1609  virtual int Parse(const uchar *Data, int Length, int Pid);
1610  };
1611 
1613 :cH264Parser()
1614 {
1615 }
1616 
1617 int cH265Parser::Parse(const uchar *Data, int Length, int Pid)
1618 {
1619  newFrame = independentFrame = false;
1620  tsPayload.Setup(const_cast<uchar *>(Data), Length, Pid);
1621  if (TsPayloadStart(Data)) {
1624  }
1625  for (;;) {
1626  scanner = (scanner << 8) | GetByte(true);
1627  if ((scanner & 0xFFFFFF00) == 0x00000100) { // NAL unit start
1628  uchar NalUnitType = (scanner >> 1) & 0x3F;
1629  GetByte(); // nuh_layer_id + nuh_temporal_id_plus1
1630  if (NalUnitType <= nutSliceSegmentRASLR || (NalUnitType >= nutSliceSegmentBLAWLP && NalUnitType <= nutSliceSegmentCRANUT)) {
1631  if (NalUnitType == nutSliceSegmentIDRWRADL || NalUnitType == nutSliceSegmentIDRNLP || NalUnitType == nutSliceSegmentCRANUT)
1632  independentFrame = true;
1633  if (GetBit()) { // first_slice_segment_in_pic_flag
1634  newFrame = true;
1636  }
1637  break;
1638  }
1639  }
1640  if (tsPayload.AtPayloadStart() // stop at any new payload start to have the buffer refilled if necessary
1641  || tsPayload.Eof()) // or if we're out of data
1642  break;
1643  }
1644  return tsPayload.Used();
1645 }
1646 
1647 // --- cFrameDetector --------------------------------------------------------
1648 
1650 {
1651  parser = NULL;
1652  SetPid(Pid, Type);
1653  synced = false;
1654  newFrame = independentFrame = false;
1655  numPtsValues = 0;
1656  numIFrames = 0;
1657  framesPerSecond = 0;
1659  scanning = false;
1660 }
1661 
1662 static int CmpUint32(const void *p1, const void *p2)
1663 {
1664  if (*(uint32_t *)p1 < *(uint32_t *)p2) return -1;
1665  if (*(uint32_t *)p1 > *(uint32_t *)p2) return 1;
1666  return 0;
1667 }
1668 
1669 void cFrameDetector::SetPid(int Pid, int Type)
1670 {
1671  pid = Pid;
1672  type = Type;
1673  isVideo = type == 0x01 || type == 0x02 || type == 0x1B || type == 0x24; // MPEG 1, 2, H.264 or H.265
1674  delete parser;
1675  parser = NULL;
1676  if (type == 0x01 || type == 0x02)
1677  parser = new cMpeg2Parser;
1678  else if (type == 0x1B)
1679  parser = new cH264Parser;
1680  else if (type == 0x24)
1681  parser = new cH265Parser;
1682  else if (type == 0x03 || type == 0x04 || type == 0x06) // MPEG audio or AC3 audio
1683  parser = new cAudioParser;
1684  else if (type != 0)
1685  esyslog("ERROR: unknown stream type %d (PID %d) in frame detector", type, pid);
1686 }
1687 
1688 int cFrameDetector::Analyze(const uchar *Data, int Length)
1689 {
1690  if (!parser)
1691  return 0;
1692  int Processed = 0;
1693  newFrame = independentFrame = false;
1694  while (Length >= MIN_TS_PACKETS_FOR_FRAME_DETECTOR * TS_SIZE) { // makes sure we are looking at enough data, in case the frame type is not stored in the first TS packet
1695  // Sync on TS packet borders:
1696  if (int Skipped = TS_SYNC(Data, Length))
1697  return Processed + Skipped;
1698  // Handle one TS packet:
1699  int Handled = TS_SIZE;
1700  if (TsHasPayload(Data) && !TsIsScrambled(Data)) {
1701  int Pid = TsPid(Data);
1702  if (Pid == pid) {
1703  if (Processed)
1704  return Processed;
1705  if (TsPayloadStart(Data))
1706  scanning = true;
1707  if (scanning) {
1708  // Detect the beginning of a new frame:
1709  if (TsPayloadStart(Data)) {
1710  if (!framesPerPayloadUnit)
1712  }
1713  int n = parser->Parse(Data, Length, pid);
1714  if (n > 0) {
1715  if (parser->NewFrame()) {
1716  newFrame = true;
1718  if (synced) {
1719  if (framesPerPayloadUnit <= 1)
1720  scanning = false;
1721  }
1722  else {
1724  if (independentFrame)
1725  numIFrames++;
1726  }
1727  }
1728  Handled = n;
1729  }
1730  }
1731  if (TsPayloadStart(Data)) {
1732  // Determine the frame rate from the PTS values in the PES headers:
1733  if (framesPerSecond <= 0.0) {
1734  // frame rate unknown, so collect a sequence of PTS values:
1735  if (numPtsValues < 2 || numPtsValues < MaxPtsValues && numIFrames < 2) { // collect a sequence containing at least two I-frames
1736  if (newFrame) { // only take PTS values at the beginning of a frame (in case if fields!)
1737  const uchar *Pes = Data + TsPayloadOffset(Data);
1738  if (numIFrames && PesHasPts(Pes)) {
1740  // check for rollover:
1741  if (numPtsValues && ptsValues[numPtsValues - 1] > 0xF0000000 && ptsValues[numPtsValues] < 0x10000000) {
1742  dbgframes("#");
1743  numPtsValues = 0;
1744  numIFrames = 0;
1745  }
1746  else
1747  numPtsValues++;
1748  }
1749  }
1750  }
1751  if (numPtsValues >= 2 && numIFrames >= 2) {
1752  // find the smallest PTS delta:
1753  qsort(ptsValues, numPtsValues, sizeof(uint32_t), CmpUint32);
1754  numPtsValues--;
1755  for (int i = 0; i < numPtsValues; i++)
1756  ptsValues[i] = ptsValues[i + 1] - ptsValues[i];
1757  qsort(ptsValues, numPtsValues, sizeof(uint32_t), CmpUint32);
1758  int Div = framesPerPayloadUnit;
1759  if (framesPerPayloadUnit > 1)
1761  if (Div <= 0)
1762  Div = 1;
1763  int Delta = ptsValues[0] / Div;
1764  // determine frame info:
1765  if (isVideo) {
1766  if (Delta == 3753)
1767  framesPerSecond = 24.0 / 1.001;
1768  else if (abs(Delta - 3600) <= 1)
1769  framesPerSecond = 25.0;
1770  else if (Delta % 3003 == 0)
1771  framesPerSecond = 30.0 / 1.001;
1772  else if (abs(Delta - 1800) <= 1)
1773  framesPerSecond = 50.0;
1774  else if (Delta == 1501)
1775  framesPerSecond = 60.0 / 1.001;
1776  else {
1778  dsyslog("unknown frame delta (%d), assuming %5.2f fps", Delta, DEFAULTFRAMESPERSECOND);
1779  }
1780  }
1781  else // audio
1782  framesPerSecond = double(PTSTICKS) / Delta; // PTS of audio frames is always increasing
1783  dbgframes("\nDelta = %d FPS = %5.2f FPPU = %d NF = %d TRO = %d\n", Delta, framesPerSecond, framesPerPayloadUnit, numPtsValues + 1, parser->IFrameTemporalReferenceOffset());
1784  synced = true;
1785  parser->SetDebug(false);
1786  }
1787  }
1788  }
1789  }
1790  else if (Pid == PATPID && synced && Processed)
1791  return Processed; // allow the caller to see any PAT packets
1792  }
1793  Data += Handled;
1794  Length -= Handled;
1795  Processed += Handled;
1796  if (newFrame)
1797  break;
1798  }
1799  return Processed;
1800 }
1801 
1802 // --- cNaluDumper ---------------------------------------------------------
1803 
1805 {
1806  LastContinuityOutput = -1;
1807  reset();
1808 }
1809 
1811 {
1812  LastContinuityInput = -1;
1813  ContinuityOffset = 0;
1814  PesId = -1;
1815  PesOffset = 0;
1817  NaluOffset = 0;
1818  History = 0xffffffff;
1819  DropAllPayload = false;
1820 }
1821 
1822 void cNaluDumper::ProcessPayload(unsigned char *Payload, int size, bool PayloadStart, sPayloadInfo &Info)
1823 {
1824  Info.DropPayloadStartBytes = 0;
1825  Info.DropPayloadEndBytes = 0;
1826  int LastKeepByte = -1;
1827 
1828  if (PayloadStart)
1829  {
1830  History = 0xffffffff;
1831  PesId = -1;
1833  }
1834 
1835  for (int i=0; i<size; i++) {
1836  History = (History << 8) | Payload[i];
1837 
1838  PesOffset++;
1839  NaluOffset++;
1840 
1841  bool DropByte = false;
1842 
1843  if (History >= 0x00000180 && History <= 0x000001FF)
1844  {
1845  // Start of PES packet
1846  PesId = History & 0xff;
1847  PesOffset = 0;
1849  }
1850  else if (PesId >= 0xe0 && PesId <= 0xef // video stream
1851  && History >= 0x00000100 && History <= 0x0000017F) // NALU start code
1852  {
1853  int NaluId = History & 0xff;
1854  NaluOffset = 0;
1855  NaluFillState = ((NaluId & 0x1f) == 0x0c) ? NALU_FILL : NALU_NONE;
1856  }
1857 
1858  if (PesId >= 0xe0 && PesId <= 0xef // video stream
1859  && PesOffset >= 1 && PesOffset <= 2)
1860  {
1861  Payload[i] = 0; // Zero out PES length field
1862  }
1863 
1864  if (NaluFillState == NALU_FILL && NaluOffset > 0) // Within NALU fill data
1865  {
1866  // We expect a series of 0xff bytes terminated by a single 0x80 byte.
1867 
1868  if (Payload[i] == 0xFF)
1869  {
1870  DropByte = true;
1871  }
1872  else if (Payload[i] == 0x80)
1873  {
1874  NaluFillState = NALU_TERM; // Last byte of NALU fill, next byte sets NaluFillEnd=true
1875  DropByte = true;
1876  }
1877  else // Invalid NALU fill
1878  {
1879  dsyslog("cNaluDumper: Unexpected NALU fill data: %02x", Payload[i]);
1881  if (LastKeepByte == -1)
1882  {
1883  // Nalu fill from beginning of packet until last byte
1884  // packet start needs to be dropped
1885  Info.DropPayloadStartBytes = i;
1886  }
1887  }
1888  }
1889  else if (NaluFillState == NALU_TERM) // Within NALU fill data
1890  {
1891  // We are after the terminating 0x80 byte
1893  if (LastKeepByte == -1)
1894  {
1895  // Nalu fill from beginning of packet until last byte
1896  // packet start needs to be dropped
1897  Info.DropPayloadStartBytes = i;
1898  }
1899  }
1900 
1901  if (!DropByte)
1902  LastKeepByte = i; // Last useful byte
1903  }
1904 
1905  Info.DropAllPayloadBytes = (LastKeepByte == -1);
1906  Info.DropPayloadEndBytes = size-1-LastKeepByte;
1907 }
1908 
1909 bool cNaluDumper::ProcessTSPacket(unsigned char *Packet)
1910 {
1911  bool HasAdaption = TsHasAdaptationField(Packet);
1912  bool HasPayload = TsHasPayload(Packet);
1913 
1914  // Check continuity:
1915  int ContinuityInput = TsContinuityCounter(Packet);
1916  if (LastContinuityInput >= 0)
1917  {
1918  int NewContinuityInput = HasPayload ? (LastContinuityInput + 1) & TS_CONT_CNT_MASK : LastContinuityInput;
1919  int Offset = (NewContinuityInput - ContinuityInput) & TS_CONT_CNT_MASK;
1920  if (Offset > 0)
1921  dsyslog("cNaluDumper: TS continuity offset %i", Offset);
1922  if (Offset > ContinuityOffset)
1923  ContinuityOffset = Offset; // max if packets get dropped, otherwise always the current one.
1924  }
1925  LastContinuityInput = ContinuityInput;
1926 
1927  if (HasPayload) {
1928  sPayloadInfo Info;
1929  int Offset = TsPayloadOffset(Packet);
1930  ProcessPayload(Packet + Offset, TS_SIZE - Offset, TsPayloadStart(Packet), Info);
1931 
1932  if (DropAllPayload && !Info.DropAllPayloadBytes)
1933  {
1934  // Return from drop packet mode to normal mode
1935  DropAllPayload = false;
1936 
1937  // Does the packet start with some remaining NALU fill data?
1938  if (Info.DropPayloadStartBytes > 0)
1939  {
1940  // Add these bytes as stuffing to the adaption field.
1941 
1942  // Sample payload layout:
1943  // FF FF FF FF FF 80 00 00 01 xx xx xx xx
1944  // ^DropPayloadStartBytes
1945 
1946  TsExtendAdaptionField(Packet, Offset - 4 + Info.DropPayloadStartBytes);
1947  }
1948  }
1949 
1950  bool DropThisPayload = DropAllPayload;
1951 
1952  if (!DropAllPayload && Info.DropPayloadEndBytes > 0) // Payload ends with 0xff NALU Fill
1953  {
1954  // Last packet of useful data
1955  // Do early termination of NALU fill data
1956  Packet[TS_SIZE-1] = 0x80;
1957  DropAllPayload = true;
1958  // Drop all packets AFTER this one
1959 
1960  // Since we already wrote the 0x80, we have to make sure that
1961  // as soon as we stop dropping packets, any beginning NALU fill of next
1962  // packet gets dumped. (see DropPayloadStartBytes above)
1963  }
1964 
1965  if (DropThisPayload && HasAdaption)
1966  {
1967  // Drop payload data, but keep adaption field data
1968  TsExtendAdaptionField(Packet, TS_SIZE-4);
1969  DropThisPayload = false;
1970  }
1971 
1972  if (DropThisPayload)
1973  {
1974  return true; // Drop packet
1975  }
1976  }
1977 
1978  // Fix Continuity Counter and reproduce incoming offsets:
1979  int NewContinuityOutput = TsHasPayload(Packet) ? (LastContinuityOutput + 1) & TS_CONT_CNT_MASK : LastContinuityOutput;
1980  NewContinuityOutput = (NewContinuityOutput + ContinuityOffset) & TS_CONT_CNT_MASK;
1981  TsSetContinuityCounter(Packet, NewContinuityOutput);
1982  LastContinuityOutput = NewContinuityOutput;
1983  ContinuityOffset = 0;
1984 
1985  return false; // Keep packet
1986 }
1987 
1988 // --- cNaluStreamProcessor ---------------------------------------------------------
1989 
1991 {
1992  pPatPmtParser = NULL;
1993  vpid = -1;
1994  data = NULL;
1995  length = 0;
1996  tempLength = 0;
1997  tempLengthAtEnd = false;
1998  TotalPackets = 0;
1999  DroppedPackets = 0;
2000 }
2001 
2003 {
2004  if (length > 0)
2005  esyslog("cNaluStreamProcessor::PutBuffer: New data before old data was processed!");
2006 
2007  data = Data;
2008  length = Length;
2009 }
2010 
2012 {
2013  if (length <= 0)
2014  {
2015  // Need more data - quick exit
2016  OutLength = 0;
2017  return NULL;
2018  }
2019  if (tempLength > 0) // Data in temp buffer?
2020  {
2021  if (tempLengthAtEnd) // Data is at end, copy to beginning
2022  {
2023  // Overlapping src and dst!
2024  for (int i=0; i<tempLength; i++)
2026  }
2027  // Normalize TempBuffer fill
2028  if (tempLength < TS_SIZE && length > 0)
2029  {
2030  int Size = min(TS_SIZE-tempLength, length);
2031  memcpy(tempBuffer+tempLength, data, Size);
2032  data += Size;
2033  length -= Size;
2034  tempLength += Size;
2035  }
2036  if (tempLength < TS_SIZE)
2037  {
2038  // All incoming data buffered, but need more data
2039  tempLengthAtEnd = false;
2040  OutLength = 0;
2041  return NULL;
2042  }
2043  // Now: TempLength==TS_SIZE
2044  if (tempBuffer[0] != TS_SYNC_BYTE)
2045  {
2046  // Need to sync on TS within temp buffer
2047  int Skipped = 1;
2048  while (Skipped < TS_SIZE && (tempBuffer[Skipped] != TS_SYNC_BYTE || (Skipped < length && data[Skipped] != TS_SYNC_BYTE)))
2049  Skipped++;
2050  esyslog("ERROR: skipped %d bytes to sync on start of TS packet", Skipped);
2051  // Pass through skipped bytes
2052  tempLengthAtEnd = true;
2053  tempLength = TS_SIZE - Skipped; // may be 0, thats ok
2054  OutLength = Skipped;
2055  return tempBuffer;
2056  }
2057  // Now: TempBuffer is a TS packet
2058  int Pid = TsPid(tempBuffer);
2059  if (pPatPmtParser)
2060  {
2061  if (Pid == 0)
2063  else if (pPatPmtParser->IsPmtPid(Pid))
2065  }
2066 
2067  TotalPackets++;
2068  bool Drop = false;
2069  if (Pid == vpid || (pPatPmtParser && Pid == pPatPmtParser->Vpid() && pPatPmtParser->Vtype() == 0x1B))
2071  if (!Drop)
2072  {
2073  // Keep this packet, then continue with new data
2074  tempLength = 0;
2075  OutLength = TS_SIZE;
2076  return tempBuffer;
2077  }
2078  // Drop TempBuffer
2079  DroppedPackets++;
2080  tempLength = 0;
2081  }
2082  // Now: TempLength==0, just process data/length
2083 
2084  // Pointer to processed data / length:
2085  uchar *Out = data;
2086  uchar *OutEnd = Out;
2087 
2088  while (length >= TS_SIZE)
2089  {
2090  if (data[0] != TS_SYNC_BYTE) {
2091  int Skipped = 1;
2092  while (Skipped < length && (data[Skipped] != TS_SYNC_BYTE || (length - Skipped > TS_SIZE && data[Skipped + TS_SIZE] != TS_SYNC_BYTE)))
2093  Skipped++;
2094  esyslog("ERROR: skipped %d bytes to sync on start of TS packet", Skipped);
2095 
2096  // Pass through skipped bytes
2097  if (OutEnd != data)
2098  memcpy(OutEnd, data, Skipped);
2099  OutEnd += Skipped;
2100  continue;
2101  }
2102  // Now: Data starts with complete TS packet
2103 
2104  int Pid = TsPid(data);
2105  if (pPatPmtParser)
2106  {
2107  if (Pid == 0)
2109  else if (pPatPmtParser->IsPmtPid(Pid))
2111  }
2112 
2113  TotalPackets++;
2114  bool Drop = false;
2115  if (Pid == vpid || (pPatPmtParser && Pid == pPatPmtParser->Vpid() && pPatPmtParser->Vtype() == 0x1B))
2117  if (!Drop)
2118  {
2119  if (OutEnd != data)
2120  memcpy(OutEnd, data, TS_SIZE);
2121  OutEnd += TS_SIZE;
2122  }
2123  else
2124  {
2125  DroppedPackets++;
2126  }
2127  data += TS_SIZE;
2128  length -= TS_SIZE;
2129  }
2130  // Now: Less than a packet remains.
2131  if (length > 0)
2132  {
2133  // copy remains into temp buffer
2134  memcpy(tempBuffer, data, length);
2135  tempLength = length;
2136  tempLengthAtEnd = false;
2137  length = 0;
2138  }
2139  OutLength = (OutEnd - Out);
2140  return OutLength > 0 ? Out : NULL;
2141 }
int framesInPayloadUnit
Definition: remux.h:526
#define VIDEO_STREAM_S
Definition: remux.c:98
bool ParsePatPmt(const uchar *Data, int Length)
Parses the given Data (which may consist of several TS packets, typically an entire frame) and extrac...
Definition: remux.c:953
unsigned char uchar
Definition: tools.h:31
void ParsePat(const uchar *Data, int Length)
Parses the PAT data from the single TS packet in Data.
Definition: remux.c:663
uchar * data
Definition: remux.h:234
int Used(void)
Returns the number of raw bytes that have already been used (e.g.
Definition: remux.h:264
uchar GetBit(void)
Definition: remux.c:1409
bool separate_colour_plane_flag
Definition: remux.c:1350
uchar GetByte(void)
Gets the next byte of the TS payload, skipping any intermediate TS header data.
Definition: remux.c:280
bool repeatLast
Definition: remux.h:464
int vpid
Definition: remux.h:366
int index
Definition: remux.h:237
int pid
Definition: remux.h:236
uchar subtitlingTypes[MAXSPIDS]
Definition: remux.h:377
Definition: device.h:64
void SetVersions(int PatVersion, int PmtVersion)
Sets the version numbers for the generated PAT and PMT, in case this generator is used to,...
Definition: remux.c:615
#define dsyslog(a...)
Definition: tools.h:37
int getVersionNumber() const
Definition: si.c:84
bool TsError(const uchar *p)
Definition: remux.h:82
void SetPid(int Pid, int Type)
Sets the Pid and stream Type to detect frames for.
Definition: remux.c:1669
#define MAX_TS_PACKETS_FOR_VIDEO_FRAME_DETECTION
Definition: remux.c:26
#define DEFAULTFRAMESPERSECOND
Definition: recording.h:351
bool newFrame
Definition: remux.c:1208
int PesPayloadOffset(const uchar *p)
Definition: remux.h:184
int Dpid(int i) const
Definition: channels.h:161
void IncCounter(int &Counter, uchar *TsPacket)
Definition: remux.c:407
bool SkipBytes(int Bytes)
Skips the given number of bytes in the payload and returns true if there is still data left to read.
Definition: remux.c:311
bool TsHasAdaptationField(const uchar *p)
Definition: remux.h:72
int Ppid(void) const
Definition: channels.h:155
void ParsePmt(const uchar *Data, int Length)
Parses the PMT data from the single TS packet in Data.
Definition: remux.c:695
uchar eit[TS_SIZE]
Definition: remux.h:440
uint16_t ancillaryPageIds[MAXSPIDS]
Definition: remux.h:379
int framesPerPayloadUnit
Definition: remux.h:527
virtual int Parse(const uchar *Data, int Length, int Pid)
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition: remux.c:1278
int pmtSize
Definition: remux.h:362
cH265Parser(void)
Definition: remux.c:1612
char alangs[MAXAPIDS][MAXLANGCODE2]
Definition: remux.h:371
bool IndependentFrame(void)
Definition: remux.c:1223
bool isNITPid() const
Definition: section.h:31
cNaluDumper NaluDumper
Definition: remux.h:611
#define SETPID(p)
#define MAX33BIT
Definition: remux.h:59
bool getCurrentNextIndicator() const
Definition: si.c:80
int LastContinuityInput
Definition: remux.h:564
bool TsPayloadStart(const uchar *p)
Definition: remux.h:77
uint32_t scanner
Definition: remux.c:1355
bool gotAccessUnitDelimiter
Definition: remux.c:1356
int MakeLanguageDescriptor(uchar *Target, const char *Language)
Definition: remux.c:468
bool IsPmtPid(int Pid) const
Returns true if Pid the one of the PMT pids as defined by the current PAT.
Definition: remux.h:406
void GeneratePmtPid(const cChannel *Channel)
Generates a PMT pid that doesn't collide with any of the actual pids of the Channel.
Definition: remux.c:502
int64_t PesGetPts(const uchar *p)
Definition: remux.h:199
int NaluOffset
Definition: remux.h:573
int Analyze(const uchar *Data, int Length)
Analyzes the TS packets pointed to by Data.
Definition: remux.c:1688
uchar pat[TS_SIZE]
Definition: remux.h:306
uint16_t YMDtoMJD(int Y, int M, int D)
Definition: remux.c:989
int numPacketsPid
Definition: remux.h:238
bool debug
Definition: remux.c:1207
bool TsHasPayload(const uchar *p)
Definition: remux.h:62
bool DropAllPayload
Definition: remux.h:568
StructureLoop< Association > associationLoop
Definition: section.h:39
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:415
const char * Alang(int i) const
Definition: channels.h:163
uint32_t ptsValues[MaxPtsValues]
Definition: remux.h:521
#define esyslog(a...)
Definition: tools.h:35
StructureLoop< Stream > streamLoop
Definition: section.h:71
int Atype(int i) const
Definition: channels.h:166
char slangs[MAXSPIDS][MAXLANGCODE2]
Definition: remux.h:376
#define TS_ADAPT_FIELD_EXISTS
Definition: remux.h:40
char * strn0cpy(char *dest, const char *src, size_t n)
Definition: tools.c:131
static u_int32_t crc32(const char *d, int len, u_int32_t CRCvalue)
Definition: util.c:267
void IncEsInfoLength(int Length)
Definition: remux.c:420
int MakeCRC(uchar *Target, const uchar *Data, int Length)
Definition: remux.c:487
bool SetAvailableTrack(eTrackType Type, int Index, uint16_t Id, const char *Language=NULL, const char *Description=NULL)
Sets the track of the given Type and Index to the given values.
Definition: device.c:1048
int getStreamType() const
Definition: section.c:69
T max(T a, T b)
Definition: tools.h:60
#define WRN_TS_PACKETS_FOR_FRAME_DETECTOR
Definition: remux.c:28
const char * Slang(int i) const
Definition: channels.h:165
void SetChannel(const cChannel *Channel)
Sets the Channel for which the PAT/PMT shall be generated.
Definition: remux.c:621
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:258
bool PesHasPts(const uchar *p)
Definition: remux.h:189
#define PTSTICKS
Definition: remux.h:57
cPatPmtGenerator(const cChannel *Channel=NULL)
Definition: remux.c:397
uchar SetEof(void)
Definition: remux.c:259
int MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId)
Definition: remux.c:451
const int * Spids(void) const
Definition: channels.h:159
int length
Definition: remux.h:460
void Setup(uchar *Data, int Length, int Pid=-1)
Sets up this TS payload handler with the given Data, which points to a sequence of Length bytes of co...
Definition: remux.c:272
int PesOffset
Definition: remux.h:571
StructureLoop< Subtitling > subtitlingLoop
Definition: descriptor.h:331
bool isVideo
Definition: remux.h:524
void ParseSequenceParameterSet(void)
Definition: remux.c:1496
int pmtVersion
Definition: remux.h:364
const char * Dlang(int i) const
Definition: channels.h:164
int64_t TsGetDts(const uchar *p, int l)
Definition: remux.c:173
int numPtsValues
Definition: remux.h:522
void TsExtendAdaptionField(unsigned char *Packet, int ToLength)
Definition: remux.c:359
bool independentFrame
Definition: remux.c:1209
cPatPmtParser * pPatPmtParser
Definition: remux.h:610
T min(T a, T b)
Definition: tools.h:59
bool independentFrame
Definition: remux.h:520
#define TS_SYNC_BYTE
Definition: remux.h:33
int lastLength
Definition: remux.h:463
int MakeAC3Descriptor(uchar *Target, uchar Type)
Definition: remux.c:441
int Vtype(void) const
Definition: channels.h:156
static bool DebugPatPmt
Definition: remux.c:20
void GeneratePat(void)
Generates a PAT section for later use with GetPat().
Definition: remux.c:517
bool Find(uint32_t Code)
Searches for the four byte sequence given in Code and returns true if it was found within the payload...
Definition: remux.c:334
#define dbgpatpmt(a...)
Definition: remux.c:23
cFrameParser(void)
Definition: remux.c:1227
int SectionLength(const uchar *Data, int Length)
Definition: remux.h:383
int MakeStream(uchar *Target, uchar Type, int Pid)
Definition: remux.c:429
uchar GetByte(bool Raw=false)
Gets the next data byte.
Definition: remux.c:1390
int iFrameTemporalReferenceOffset
Definition: remux.c:1210
int Spid(int i) const
Definition: channels.h:162
int patCounter
Definition: remux.h:309
uchar pmt[MAX_PMT_TS][TS_SIZE]
Definition: remux.h:307
uchar * lastData
Definition: remux.h:462
bool PesLongEnough(int Length)
Definition: remux.h:169
const int * Apids(void) const
Definition: channels.h:157
int Dtype(int i) const
Definition: channels.h:167
void TsSetPcr(uchar *p, int64_t Pcr)
Definition: remux.c:131
#define MAX_SECTION_SIZE
Definition: remux.h:301
#define EMPTY_SCANNER
Definition: remux.c:30
int TsPid(const uchar *p)
Definition: remux.h:87
virtual int Parse(const uchar *Data, int Length, int Pid)
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition: remux.c:1446
long long int DroppedPackets
Definition: remux.h:614
cFrameDetector(int Pid=0, int Type=0)
Sets up a frame detector for the given Pid and stream Type.
Definition: remux.c:1649
#define SETPIDS(l)
void ParseSliceHeader(void)
Definition: remux.c:1548
double framesPerSecond
Definition: remux.h:525
void EnsureSubtitleTrack(void)
Makes sure one of the preferred language subtitle tracks is selected.
Definition: device.c:1181
#define TS_PAYLOAD_EXISTS
Definition: remux.h:41
int PesLength(const uchar *p)
Definition: remux.h:179
void PesSetPts(uchar *p, int64_t Pts)
Definition: remux.c:216
void ParseAccessUnitDelimiter(void)
Definition: remux.c:1489
Definition: remux.h:20
int lastIFrameTemporalReference
Definition: remux.c:1265
int zeroBytes
Definition: remux.c:1348
int ppid
Definition: remux.h:367
bool GetVersions(int &PatVersion, int &PmtVersion) const
Returns true if a valid PAT/PMT has been parsed and stores the current version numbers in the given v...
Definition: remux.c:972
int getPid() const
Definition: section.c:65
cH264Parser(void)
Sets up a new H.264 parser.
Definition: remux.c:1377
char dlangs[MAXDPIDS][MAXLANGCODE2]
Definition: remux.h:374
const int * Dpids(void) const
Definition: channels.h:158
void Reset(void)
Resets the converter.
Definition: remux.c:1162
int ContinuityOffset
Definition: remux.h:566
#define MAXPID
Definition: remux.c:500
bool synced
Definition: remux.h:518
bool PesHasDts(const uchar *p)
Definition: remux.h:194
int Tpid(void) const
Definition: channels.h:171
void PesSetDts(uchar *p, int64_t Dts)
Definition: remux.c:225
void BlockDump(const char *Name, const u_char *Data, int Length)
Definition: remux.c:1172
void TsSetDts(uchar *p, int l, int64_t Dts)
Definition: remux.c:200
cPatPmtParser(bool UpdatePrimaryDevice=false)
Definition: remux.c:647
int TsSync(const uchar *Data, int Length, const char *File, const char *Function, int Line)
Definition: remux.c:147
int GetLastIndex(void)
Returns the index into the TS data of the payload byte that has most recently been read.
Definition: remux.c:323
int PesId
Definition: remux.h:570
virtual int Parse(const uchar *Data, int Length, int Pid)=0
Parses the given Data, which is a sequence of Length bytes of TS packets.
cTsPayload(void)
Definition: remux.c:246
int dtypes[MAXDPIDS+1]
Definition: remux.h:373
int dpids[MAXDPIDS+1]
Definition: remux.h:372
void TsDump(const char *Name, const u_char *Data, int Length)
Definition: remux.c:1183
virtual int Parse(const uchar *Data, int Length, int Pid)
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition: remux.c:1247
int64_t PtsDiff(int64_t Pts1, int64_t Pts2)
Returns the difference between two PTS values.
Definition: remux.c:234
void PutTs(const uchar *Data, int Length)
Puts the payload data of the single TS packet at Data into the converter.
Definition: remux.c:1080
int getServiceId() const
Definition: section.c:30
void ClrAvailableTracks(bool DescriptionsOnly=false, bool IdsOnly=false)
Clears the list of currently available tracks.
Definition: device.c:1025
ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader)
Definition: remux.c:32
int IFrameTemporalReferenceOffset(void)
Definition: remux.c:1224
int atypes[MAXAPIDS+1]
Definition: remux.h:370
unsigned int History
Definition: remux.h:562
uchar byte
Definition: remux.c:1346
uchar * GetPmt(int &Index)
Returns a pointer to the Index'th TS packet of the PMT section.
Definition: remux.c:636
void TsSetContinuityCounter(uchar *p, uchar Counter)
Definition: remux.h:108
int numPmtPackets
Definition: remux.h:308
cSetup Setup
Definition: config.c:372
void reset()
Definition: remux.c:1810
void ProcessPayload(unsigned char *Payload, int size, bool PayloadStart, sPayloadInfo &Info)
Definition: remux.c:1822
uchar * Generate(int Sid)
Definition: remux.c:1006
StructureLoop< Language > languageLoop
Definition: descriptor.h:489
int getServiceId() const
Definition: section.c:57
bool Eof(void) const
Returns true if all available bytes of the TS payload have been processed.
Definition: remux.h:268
int Apid(int i) const
Definition: channels.h:160
#define WRN_TS_PACKETS_FOR_VIDEO_FRAME_DETECTION
Definition: remux.c:27
~cTsToPes()
Definition: remux.c:1075
#define MAXLANGCODE1
Definition: channels.h:36
int TsGetPayload(const uchar **p)
Definition: remux.h:119
#define MAXPESLENGTH
Definition: remux.c:1107
int pmtCounter
Definition: remux.h:310
int LastContinuityOutput
Definition: remux.h:565
long long int TotalPackets
Definition: remux.h:613
void GeneratePmt(const cChannel *Channel)
Generates a PMT section for the given Channel, for later use with GetPmt().
Definition: remux.c:546
uchar pmt[MAX_SECTION_SIZE]
Definition: remux.h:361
int32_t GetGolombSe(void)
Definition: remux.c:1434
void TsHidePayload(uchar *p)
Definition: remux.c:121
#define P_TSID
Definition: remux.c:498
int length
Definition: remux.h:235
#define TS_CONT_CNT_MASK
Definition: remux.h:42
uint16_t compositionPageIds[MAXSPIDS]
Definition: remux.h:378
void PesDump(const char *Name, const u_char *Data, int Length)
Definition: remux.c:1198
int counter
Definition: remux.h:441
int pmtPids[MAX_PMT_PIDS+1]
Definition: remux.h:365
virtual int Parse(const uchar *Data, int Length, int Pid)
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition: remux.c:1617
uint16_t CompositionPageId(int i) const
Definition: channels.h:169
int getSectionNumber() const
Definition: si.c:88
bool CheckCRCAndParse()
Definition: si.c:65
uint32_t GetBits(int Bits)
Definition: remux.c:1418
bool frame_mbs_only_flag
Definition: remux.c:1352
static void SetBrokenLink(uchar *Data, int Length)
Definition: remux.c:102
bool completed
Definition: remux.h:381
virtual ~cFrameParser()
Definition: remux.c:1213
static bool DebugFrames
Definition: remux.c:21
Definition: device.h:67
uchar SubtitlingType(int i) const
Definition: channels.h:168
bool seenIndependentFrame
Definition: remux.c:1264
int Vpid(void) const
Definition: channels.h:154
int patVersion
Definition: remux.h:311
int UseDolbyDigital
Definition: config.h:319
cFrameParser * parser
Definition: remux.h:530
bool ProcessTSPacket(unsigned char *Packet)
Definition: remux.c:1909
int numPacketsOther
Definition: remux.h:239
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:409
#define MAXDPIDS
Definition: channels.h:32
int spids[MAXSPIDS+1]
Definition: remux.h:375
#define MIN_TS_PACKETS_FOR_FRAME_DETECTOR
Definition: remux.h:509
int version
Definition: remux.h:442
int size
Definition: remux.h:459
#define PCRFACTOR
Definition: remux.h:58
void EnsureAudioTrack(bool Force=false)
Makes sure an audio track is selected that is actually available.
Definition: device.c:1148
#define PATPID
Definition: remux.h:52
int getTransportStreamId() const
Definition: section.c:26
#define MAX_PMT_PIDS
Definition: remux.h:357
static cDevice * PrimaryDevice(void)
Returns the primary device.
Definition: device.h:146
bool PesHasLength(const uchar *p)
Definition: remux.h:174
#define P_PMT_PID
Definition: remux.c:499
#define TS_SYNC(Data, Length)
Definition: remux.h:154
int64_t TsGetPts(const uchar *p, int l)
Definition: remux.c:160
#define KILOBYTE(n)
Definition: tools.h:44
uchar * AddParentalRatingDescriptor(uchar *p, uchar ParentalRating=0)
Definition: remux.c:995
unsigned char u_char
Definition: headers.h:24
bool TsIsScrambled(const uchar *p)
Definition: remux.h:98
int64_t PesGetDts(const uchar *p)
Definition: remux.h:208
cTsPayload tsPayload
Definition: remux.c:1354
ePesHeader
Definition: remux.h:16
void SetByte(uchar Byte, int Index)
Sets the TS data byte at the given Index to the value Byte.
Definition: remux.c:328
cNaluDumper()
Definition: remux.c:1804
uchar * esInfoLength
Definition: remux.h:314
#define TS_PAYLOAD_START
Definition: remux.h:36
bool updatePrimaryDevice
Definition: remux.h:380
DescriptorLoop streamDescriptors
Definition: section.h:63
void Reset(void)
Definition: remux.c:265
#define MAXSPIDS
Definition: channels.h:33
eNaluFillState NaluFillState
Definition: remux.h:582
bool newFrame
Definition: remux.h:519
bool gotSequenceParameterSet
Definition: remux.c:1357
uchar TsContinuityCounter(const uchar *p)
Definition: remux.h:103
void Statistics(void) const
May be called after a new frame has been detected, and will log a warning if the number of TS packets...
Definition: remux.c:351
uint16_t AncillaryPageId(int i) const
Definition: channels.h:170
DescriptorTag getDescriptorTag() const
Definition: si.c:100
#define TS_SIZE
Definition: remux.h:34
cMpeg2Parser(void)
Definition: remux.c:1271
Definition: remux.h:19
void IncVersion(int &Version)
Definition: remux.c:414
const uchar * GetPes(int &Length)
Gets a pointer to the complete PES packet, or NULL if the packet is not complete yet.
Definition: remux.c:1109
int getPCRPid() const
Definition: section.c:61
uint32_t GetGolombUe(void)
Definition: remux.c:1426
int bit
Definition: remux.c:1347
int getPid() const
Definition: section.c:34
int log2_max_frame_num
Definition: remux.c:1351
#define EITPID
Definition: remux.h:54
void SetDebug(bool Debug)
Definition: remux.c:1221
int TsPayloadOffset(const uchar *p)
Definition: remux.h:113
bool scanning
Definition: remux.h:529
int offset
Definition: remux.h:461
#define TS_ADAPT_PCR
Definition: remux.h:46
int numIFrames
Definition: remux.h:523
bool NewFrame(void)
Definition: remux.c:1222
void SetRepeatLast(void)
Makes the next call to GetPes() return exactly the same data as the last one (provided there was no c...
Definition: remux.c:1157
bool AtTsStart(void)
Returns true if this payload handler is currently pointing to first byte of a TS packet.
Definition: remux.h:255
cEitGenerator(int Sid=0)
Definition: remux.c:981
void TsSetPts(uchar *p, int l, int64_t Pts)
Definition: remux.c:186
const char * I18nNormalizeLanguageCode(const char *Code)
Returns a 3 letter language code that may not be zero terminated.
Definition: i18n.c:238
static int CmpUint32(const void *p1, const void *p2)
Definition: remux.c:1662
void PutBuffer(uchar *Data, int Length)
Definition: remux.c:2002
void Reset(void)
Resets the parser.
Definition: remux.c:653
Descriptor * getNext(Iterator &it)
Definition: si.c:112
uchar * data
Definition: remux.h:458
uchar * GetPat(void)
Returns a pointer to the PAT section, which consists of exactly one TS packet.
Definition: remux.c:630
uchar * GetBuffer(int &OutLength)
Definition: remux.c:2011
int getLastSectionNumber() const
Definition: si.c:92
int patVersion
Definition: remux.h:363
int vtype
Definition: remux.h:368
#define dbgframes(a...)
Definition: remux.c:24
int pmtVersion
Definition: remux.h:312
bool SkipPesHeader(void)
Skips all bytes belonging to the PES header of the payload.
Definition: remux.c:318
uint32_t scanner
Definition: remux.c:1263
cTsToPes(void)
Definition: remux.c:1068
int apids[MAXAPIDS+1]
Definition: remux.h:369
#define MAXAPIDS
Definition: channels.h:31
uchar tempBuffer[TS_SIZE]
Definition: remux.h:607
cAudioParser(void)
Definition: remux.c:1243
bool tempLengthAtEnd
Definition: remux.h:609